Files
next-tjwater-agent/.opencode/tools/frontend_action.ts
T

28 lines
1.3 KiB
TypeScript

type FrontendActionArgs = Record<string, unknown> & { __frontend_action_call_id?: string; reason?: string };
const internalBaseUrl = process.env.TJWATER_AGENT_INTERNAL_BASE_URL ?? "http://127.0.0.1:8787";
const internalToken = process.env.TJWATER_AGENT_INTERNAL_TOKEN ?? "";
export const executeFrontendAction = async (
name: string,
args: FrontendActionArgs,
context: { sessionID: string },
) => {
const 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 text;
};