feat(agent): add frontend action bridge
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import type { Plugin } from "@opencode-ai/plugin";
|
||||
|
||||
export const FRONTEND_ACTION_TOOLS = new Set(["zoom_to_map", "locate_features", "view_history", "view_scada"]);
|
||||
|
||||
export const frontendActionCallIdPlugin: Plugin = async () => ({
|
||||
"tool.execute.before": async (input, output) => {
|
||||
if (!FRONTEND_ACTION_TOOLS.has(input.tool)) return;
|
||||
output.args = { ...(output.args ?? {}), __frontend_action_call_id: input.callID };
|
||||
},
|
||||
});
|
||||
|
||||
export default frontendActionCallIdPlugin;
|
||||
@@ -0,0 +1,27 @@
|
||||
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;
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
import { tool } from "@opencode-ai/plugin";
|
||||
import { executeFrontendAction } from "./frontend_action.js";
|
||||
|
||||
export default tool({
|
||||
description: "在前端地图上定位并高亮指定的管网要素。",
|
||||
@@ -9,14 +10,13 @@ export default tool({
|
||||
"Why this map positioning action is needed for the user request.",
|
||||
),
|
||||
ids: tool.schema
|
||||
.array(tool.schema.string())
|
||||
.array(tool.schema.string().min(1)).min(1).max(100)
|
||||
.describe("Feature ids to locate."),
|
||||
feature_type: tool.schema
|
||||
.enum(["junction", "pipe", "valve", "reservoir", "pump", "tank"])
|
||||
.enum(["junction", "pipe", "valve", "reservoir", "pump", "tank", "conduit", "orifice", "outfall"])
|
||||
.describe("Type of feature to locate."),
|
||||
},
|
||||
async execute() {
|
||||
// 前端工具只负责生成 tool part,真正的地图动作由 Agent SSE 适配层转发给浏览器执行。
|
||||
return "已在地图上定位到指定要素。";
|
||||
async execute(args, context) {
|
||||
return executeFrontendAction("locate_features", args, context);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { tool } from "@opencode-ai/plugin";
|
||||
import { executeFrontendAction } from "./frontend_action.js";
|
||||
|
||||
export default tool({
|
||||
description: "为选定的管网要素打开前端的历史记录或计算结果面板。",
|
||||
@@ -23,8 +24,7 @@ export default tool({
|
||||
.optional()
|
||||
.describe("Optional ISO8601 end time."),
|
||||
},
|
||||
async execute() {
|
||||
// 返回短确认即可;面板打开动作由前端根据 tool_call 参数完成。
|
||||
return "已打开计算结果面板。";
|
||||
async execute(args, context) {
|
||||
return executeFrontendAction("view_history", args, context);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { tool } from "@opencode-ai/plugin";
|
||||
import { executeFrontendAction } from "./frontend_action.js";
|
||||
|
||||
export default tool({
|
||||
description: "打开前端的 SCADA 监测数据历史面板。",
|
||||
@@ -23,8 +24,7 @@ export default tool({
|
||||
.optional()
|
||||
.describe("Optional ISO8601 end time."),
|
||||
},
|
||||
async execute() {
|
||||
// SCADA 面板仍在浏览器侧执行,工具结果不承载实际监测数据。
|
||||
return "已打开 SCADA 监测面板。";
|
||||
async execute(args, context) {
|
||||
return executeFrontendAction("view_scada", args, context);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { tool } from "@opencode-ai/plugin";
|
||||
import { executeFrontendAction } from "./frontend_action.js";
|
||||
|
||||
export default tool({
|
||||
description:
|
||||
@@ -26,7 +27,7 @@ export default tool({
|
||||
.optional()
|
||||
.describe("Optional animation duration in milliseconds. Defaults to 1000."),
|
||||
},
|
||||
async execute() {
|
||||
return "已缩放到指定地图坐标。";
|
||||
async execute(args, context) {
|
||||
return executeFrontendAction("zoom_to_map", args, context);
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user