import { tool } from "@opencode-ai/plugin"; import { ToolSessionContextStore } from "../../src/session/toolContextStore.js"; const internalBaseUrl = process.env.TJWATER_AGENT_INTERNAL_BASE_URL ?? "http://127.0.0.1:8787"; const internalToken = process.env.TJWATER_AGENT_INTERNAL_TOKEN ?? ""; const toolContextStore = new ToolSessionContextStore(); const initializePromise = toolContextStore.initialize(); export default tool({ description: "把本地 JSON 渲染文件迁移成受控的 render_ref。仅适用于需要通过链接引用传递的大型 junction render payload。", args: { reason: tool.schema .string() .describe("Why this local render payload should be persisted as a render_ref."), file_path: tool.schema .string() .describe( "Absolute path to a local JSON file containing the raw render payload, or a wrapper object with data, metadata, and location. If wrapper metadata/location is missing or stale, the resolver will normalize and write it back before storing the render_ref.", ), }, async execute(args, context) { await initializePromise; const sessionContext = await toolContextStore.read(context.sessionID); if (!sessionContext) { throw new Error(`session context not found for ${context.sessionID}`); } const response = await fetch(`${internalBaseUrl}/internal/tools/store-render-ref`, { method: "POST", headers: { "Content-Type": "application/json", "x-agent-internal-token": internalToken, }, body: JSON.stringify({ sessionScopeKey: sessionContext.sessionScopeKey, file_path: args.file_path, }), }); const text = await response.text(); if (!response.ok) { throw new Error(text); } return text; }, });