forked from KeithCu/writeragent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_function.py
More file actions
302 lines (263 loc) · 11.4 KB
/
Copy pathpython_function.py
File metadata and controls
302 lines (263 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2024 John Balis
# Copyright (c) 2026 KeithCu (modifications and relicensing)
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""=PYTHON() execution and return helpers (venv worker); no LLM imports."""
from __future__ import annotations
import logging
import math
import threading
from typing import Any, cast
from plugin.calc.calc_addin_data import (
calc_addin_args_from_split,
check_python_data_size,
check_python_multi_data_size,
count_cells,
pack_calc_data_for_wire,
pack_calc_multi_data_for_wire,
split_python_addin_data_args,
)
from plugin.calc.python_image_egress import insert_image_result_on_sheet
from plugin.framework.errors import format_error_payload
from plugin.framework.i18n import _
from plugin.scripting.config_limits import configured_python_max_data_cells
from plugin.scripting.payload_codec import is_image_payload, is_split_grid
# Optional: reset worker init/cell sessions on workbook close (see python_workbook_lifecycle.py).
# from plugin.calc.python_workbook_lifecycle import ensure_calc_workbook_unload_resets_python
from plugin.scripting.document_scripts import build_python_eval_init_kwargs, get_calc_document_from_ctx
from plugin.scripting.session_manager import workbook_session_id
from plugin.scripting.venv_worker import run_code_in_user_venv
log = logging.getLogger(__name__)
# Calc legacy add-in bridge accepts scalar double/string returns only. List results are
# emitted one scalar per formula evaluation (matrix block or repeated recalc).
MATRIX_SCALAR_SESSIONS = threading.local()
def flatten_result_values(result: Any) -> list:
"""Row-major flattening for list / nested list worker results."""
if not isinstance(result, (list, tuple)):
return [result]
if not result:
return []
if isinstance(result[0], (list, tuple)):
flat: list = []
for row in result:
flat.extend(row)
return flat
return list(result)
def is_scalar_index_arg(py_data: list | list[list] | None) -> bool:
"""True when arg 1 is one number (matrix index), not a data range."""
if py_data is None:
return False
if count_cells(py_data) != 1:
return False
first = py_data[0]
return not isinstance(first, (list, tuple))
# Tests and legacy imports
_is_scalar_index_arg = is_scalar_index_arg
def coerce_index(value: Any) -> int:
if value is None:
return 0
if isinstance(value, bool):
return int(value)
if isinstance(value, int):
return value
if isinstance(value, float):
return int(value)
if isinstance(value, str) and value.strip():
return int(float(value))
raise ValueError(f"index must be numeric, got {value!r}")
def to_calc_compatible(val: Any) -> float | str | bool | tuple:
"""Recursively convert Python values into LibreOffice Calc supported types.
Calc cells only support float (UNO double), str (UNO string), and bool (UNO boolean).
Crucially, Calc matrix formulas do NOT support integer (UNO long) types and will
throw #VALUE! if a sequence contains integers/longs.
"""
if val is None:
return ""
if isinstance(val, bool):
return val
if isinstance(val, int):
return float(val)
if isinstance(val, float):
# NaN from Python/NumPy egress must become an empty cell, not a raw double (#NUM! / #VALUE!).
if math.isnan(val):
return ""
return val
if isinstance(val, str):
return val
if isinstance(val, (list, tuple)):
inner = val[0] if val else None
if isinstance(inner, (list, tuple)):
return tuple(tuple(to_calc_compatible(cell) for cell in row) for row in val)
return tuple(to_calc_compatible(item) for item in val)
return str(val)
def session_key(ctx: Any, code: str) -> tuple:
doc_url = ""
sheet_name = ""
try:
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
doc = desktop.getCurrentComponent()
if doc is not None:
doc_url = getattr(doc, "getURL", lambda: "")() or ""
ctrl = getattr(doc, "getCurrentController", lambda: None)()
if ctrl is not None:
sheet = ctrl.getActiveSheet()
if sheet is not None:
sheet_name = sheet.getName()
except Exception:
pass
return (doc_url, sheet_name, code)
class WorkerResultSession:
"""Caches one worker list result across multiple =PYTHON() calls in a recalc pass."""
__slots__ = ("raw", "flat", "next_index")
def __init__(self, raw: Any, flat: list) -> None:
self.raw = raw
self.flat = tuple(flat)
self.next_index = 0
# Legacy alias for tests
_WorkerResultSession = WorkerResultSession
def scalar_for_list_result(ctx: Any, code: str, result: Any, *, worker_data: Any = None) -> float | str | bool:
"""Return one Calc scalar per invocation when the worker produced a list."""
flat: list = [to_calc_compatible(v) for v in flatten_result_values(result)]
if not flat:
return ""
key = (session_key(ctx, code), repr(worker_data))
sessions = getattr(MATRIX_SCALAR_SESSIONS, "sessions", None)
if sessions is None:
sessions = {}
MATRIX_SCALAR_SESSIONS.sessions = sessions
state = sessions.get(key)
if not isinstance(state, WorkerResultSession) or state.flat != tuple(flat):
state = WorkerResultSession(result, flat)
sessions[key] = state
idx = state.next_index
state.next_index = idx + 1
if state.next_index >= len(state.flat):
sessions.pop(key, None)
if 0 <= idx < len(state.flat):
return state.flat[idx]
return state.flat[-1] if state.flat else ""
def finalize_python_return(
ctx: Any,
code: str,
result: Any,
*,
index_arg: Any = None,
worker_data: Any = None,
) -> float | str | bool | tuple:
"""Map worker result to a single value Calc's add-in bridge accepts."""
import numpy as np
if isinstance(result, np.ndarray):
res_list = result.tolist()
if result.ndim == 1:
result = [[x] for x in res_list]
else:
result = res_list
if isinstance(result, (list, tuple)):
if index_arg is not None:
flat = flatten_result_values(result)
idx = coerce_index(index_arg)
if idx < 0 or idx >= len(flat):
return f"Error: index {idx} out of range (result length {len(flat)})"
return to_calc_compatible(flat[idx])
return scalar_for_list_result(ctx, code, result, worker_data=worker_data)
return to_calc_compatible(result)
# Backward-compatible alias for tests and callers.
_insert_image_result_on_sheet = insert_image_result_on_sheet
def _format_error_for_display(exc: BaseException) -> str:
"""Cell-safe error text without importing ``plugin.framework.client`` (loads LLM stack)."""
err: Exception = exc if isinstance(exc, Exception) else RuntimeError(str(exc))
payload = format_error_payload(err)
return _("Error: {0}").format(payload.get("message", str(exc)))
def _code_uses_indexed_multi_data(code: str) -> bool:
"""True when inline code references ``data[n]`` (all PY args are data ranges, not a matrix index)."""
return "data[" in (code or "")
def execute_python_addin(
ctx: Any,
code: str,
data: Any = None,
true_strings: set[str] | None = None,
false_strings: set[str] | None = None,
) -> Any:
"""Run *code* in the user venv and return a Calc-compatible scalar (or error string)."""
log.debug("=== PYTHON(%r, data=%r) ===", code, data)
try:
args = split_python_addin_data_args(data)
py_data = calc_addin_args_from_split(args, true_strings, false_strings)
log.debug("PYTHON parsed py_data: %r", py_data)
is_multi = len(args) > 1
index_arg = None
if py_data is not None:
if is_multi and not _code_uses_indexed_multi_data(code):
last_arg = args[-1]
if not isinstance(last_arg, (list, tuple)) or count_cells(py_data[-1]) == 1:
idx_val = py_data[-1]
while isinstance(idx_val, list) and idx_val:
idx_val = idx_val[0]
index_arg = idx_val
py_data = py_data[:-1]
args = args[:-1]
is_multi = len(args) > 1
if py_data:
if not is_multi:
py_data = py_data[0]
else:
py_data = None
elif is_scalar_index_arg(py_data) and not is_split_grid(py_data):
index_arg = py_data[0]
max_cells = configured_python_max_data_cells(ctx)
if py_data is not None:
if is_multi:
size_err = check_python_multi_data_size(py_data, max_cells=max_cells)
else:
size_err = check_python_data_size(py_data, max_cells=max_cells)
if size_err:
ret = f"Error: {size_err}"
log.debug("PYTHON returning size error: %r", ret)
return ret
worker_data = pack_calc_multi_data_for_wire(py_data) if is_multi else pack_calc_data_for_wire(py_data)
else:
worker_data = None
# Synchronous: =PYTHON() runs during Calc recalc; UI event pumping from
# run_blocking_in_thread can re-enter the formula engine and yield #VALUE!.
sessions = getattr(MATRIX_SCALAR_SESSIONS, "sessions", None)
if sessions is None:
sessions = {}
MATRIX_SCALAR_SESSIONS.sessions = sessions
cache_key = (session_key(ctx, code), repr(worker_data))
cached = sessions.get(cache_key)
if isinstance(cached, WorkerResultSession) and cached.next_index < len(cached.flat):
res = {"status": "ok", "result": cached.raw}
else:
session_id = workbook_session_id(ctx)
init_kwargs: dict[str, Any] = {}
doc = get_calc_document_from_ctx(ctx)
if doc is not None:
# ensure_calc_workbook_unload_resets_python(ctx, doc)
init_kwargs = build_python_eval_init_kwargs(doc)
res = run_code_in_user_venv(
ctx,
code,
data=worker_data,
session_id=session_id,
**init_kwargs,
)
log.debug("PYTHON res from worker: %r", res)
if res.get("status") == "ok":
result = res.get("result")
log.debug("PYTHON raw result: %r (type: %s)", result, type(result).__name__)
if is_image_payload(result):
insert_image_result_on_sheet(ctx, cast("dict[str, Any]", result))
return _("Image inserted")
final_ret = finalize_python_return(ctx, code, result, index_arg=index_arg, worker_data=worker_data)
log.debug("PYTHON returning scalar: %r (type: %s)", final_ret, type(final_ret).__name__)
return final_ret
err_msg = f"Error: {res.get('message') or res.get('error')}"
log.debug("PYTHON returning worker error: %r", err_msg)
return err_msg
except Exception as e:
log.exception("PYTHON unexpected error during execution")
err_msg = _format_error_for_display(e)
log.debug("PYTHON returning exception wrapper: %r", err_msg)
return err_msg