Files
next-tjwater-agent/.opencode/tools/frontend_action.ts
T
jiang 7383ed6d13 feat(agent): migrate OpenCode 2 runtime and sandbox tools
Upgrade the runtime and native plugin API to OpenCode 2.0.24. Restore
session-scoped learning, credential renewal, typed project queries and
controlled frontend actions from the legacy Agent.

Replace host Shell execution with an approved Landlock/seccomp executor
and persistent per-session workspaces. Add typed Bocha search and Tianditu
geocoding bridges using the owning session credential. Require a working
kernel sandbox before startup and persist workspace files in deployments.

Validation completed before this commit: TypeScript checks, 150 Bun tests,
Docker build and UID 10001 sandbox execution, native Shell approval and
session recovery. Tianditu succeeded; Bocha currently reports exhausted
account quota. Unrelated investigation-report contract work is excluded.
2026-10-08 15:46:37 +08:00

76 lines
2.6 KiB
TypeScript

import { getInternalBridgeConfig } from "../internalBridge.js";
type FrontendActionArgs = Record<string, unknown> & {
__frontend_action_call_id?: string;
reason?: string;
};
type FrontendActionBridgeResult = {
version: "frontend-action-result@1";
status: "succeeded" | "failed" | "rejected" | "cancelled" | "expired";
output?: unknown;
error?: { code?: string; message?: string };
};
export const executeFrontendAction = async (
name: "render_scada_analysis" | "clear_scada_analysis" | "locate_features" | "zoom_to_map" | "show_chart" | "view_history",
args: FrontendActionArgs,
context: { sessionID: string; callID?: string },
) => {
const { baseUrl: internalBaseUrl, internalToken } = getInternalBridgeConfig();
const callId = context.callID ?? args.__frontend_action_call_id;
if (!callId) {
throw new Error("frontend action bridge did not inject the OpenCode call ID");
}
const { __frontend_action_call_id: _callId, reason, ...params } = args;
let response: Response;
try {
response = await fetch(`${internalBaseUrl}/internal/frontend-actions/request`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-agent-internal-token": internalToken,
},
body: JSON.stringify({
session_id: context.sessionID,
call_id: callId,
name,
params,
fallback_text: reason,
}),
});
} catch (error) {
throw new Error(
`frontend action bridge unavailable: ${error instanceof Error ? error.message : String(error)}`,
);
}
const text = await response.text();
if (!response.ok) {
throw new Error(
`frontend action bridge rejected request (${response.status}): ${text}`,
);
}
return unwrapFrontendActionResult(text);
};
export const unwrapFrontendActionResult = (text: string) => {
let result: FrontendActionBridgeResult;
try {
result = JSON.parse(text) as FrontendActionBridgeResult;
} catch {
throw new Error("frontend action bridge returned invalid JSON");
}
if (result.version !== "frontend-action-result@1" || typeof result.status !== "string") {
throw new Error("frontend action bridge returned an invalid result");
}
if (result.status !== "succeeded") {
const code = result.error?.code?.trim() || `ACTION_${result.status.toUpperCase()}`;
const message = result.error?.message?.trim() || `browser action ${result.status}`;
throw new Error(`${code}: ${message}`);
}
if (result.output === undefined) {
throw new Error("frontend action bridge returned no output");
}
return JSON.stringify(result.output);
};