Repository navigation
Expand file tree
/
Copy pathtrusted_rpc.py
More file actions
95 lines (83 loc) · 3.33 KB
/
Copy pathtrusted_rpc.py
File metadata and controls
95 lines (83 loc) · 3.33 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
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2026 KeithCu (modifications and relicensing)
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Shared host RPC for trusted venv worker actions (zero user-code AST path)."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from collections.abc import Callable
from plugin.framework.send_cancellation import SendCancellation
from plugin.framework.constants import WORKER_POOL_DEFAULT
from plugin.framework.deal_shim import DEAL_MAX_SHAPE_DIM, DEAL_MAX_TOKEN, str_bounded, deal
from plugin.framework.errors import ToolExecutionError
from plugin.scripting.venv_worker import run_code_in_user_venv
@deal.pre(
lambda response, error_code="", error_label="": isinstance(response, dict)
and len(response) <= DEAL_MAX_SHAPE_DIM
and str_bounded(error_code, DEAL_MAX_TOKEN)
and str_bounded(error_label, DEAL_MAX_TOKEN)
)
@deal.post(lambda result: isinstance(result, dict))
@deal.raises(ToolExecutionError)
def parse_worker_dict_result(
response: dict[str, Any],
*,
error_code: str,
error_label: str,
) -> dict[str, Any]:
"""Normalize a warm-worker response into a result dict or raise ToolExecutionError."""
if response.get("status") != "ok":
message = str(response.get("message") or f"{error_label} worker failed.")
raise ToolExecutionError(message, code=error_code, details={"worker": response})
result = response.get("result")
if not isinstance(result, dict):
raise ToolExecutionError(
f"{error_label} worker returned an unexpected result.",
code=error_code,
details={"result_type": type(result).__name__},
)
return result
def run_trusted_worker_action(
ctx: object | None,
*,
domain: str,
helper: str = "",
params: dict[str, Any] | None = None,
data_range: Any = None,
context: dict[str, Any] | None = None,
session_id: str | None = None,
timeout_sec: int,
worker_pool: str = WORKER_POOL_DEFAULT,
additional_data: dict[str, Any] | None = None,
allow_heartbeat: bool = False,
heartbeat_fn: Callable[[dict[str, Any]], None] | None = None,
error_code: str = "TRUSTED_ACTION_ERROR",
error_label: str = "Trusted action",
cancel: SendCancellation | None = None,
) -> dict[str, Any]:
"""Execute a trusted action in the warm venv worker without user code strings.
*ctx* may be a ToolContext, a UNO component context or None; only its optional ``send_cancellation`` is read, as the Stop scope when *cancel* is not given.
"""
if cancel is None and ctx is not None:
cancel = getattr(ctx, "send_cancellation", None)
payload: dict[str, Any] = {
**(additional_data or {}),
"domain": domain,
"helper": helper,
"params": dict(params or {}),
"data_range": data_range,
"context": context or {},
}
response = run_code_in_user_venv(
code=None,
data=payload,
timeout_sec=timeout_sec,
session_id=session_id,
worker_pool=worker_pool,
action="run_trusted_action",
allow_heartbeat=allow_heartbeat,
on_heartbeat=heartbeat_fn if allow_heartbeat else None,
cancel=cancel,
)
return parse_worker_dict_result(response, error_code=error_code, error_label=error_label)