50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
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:
|
|
"回读由 dynamic_http_call 生成的持久化 result_ref。适用于大结果只返回 preview 时,再按需读取完整或截断后的数据。",
|
|
args: {
|
|
reason: tool.schema
|
|
.string()
|
|
.describe("Why the stored result needs to be read for the current user request."),
|
|
result_ref: tool.schema.string().describe("The result_ref returned by dynamic_http_call."),
|
|
max_items: tool.schema
|
|
.number()
|
|
.int()
|
|
.positive()
|
|
.optional()
|
|
.describe("Optional maximum number of top-level items or fields to return."),
|
|
},
|
|
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/fetch-result-ref`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"x-agent-internal-token": internalToken,
|
|
},
|
|
body: JSON.stringify({
|
|
sessionScopeKey: sessionContext.sessionScopeKey,
|
|
result_ref: args.result_ref,
|
|
max_items: args.max_items,
|
|
}),
|
|
});
|
|
|
|
const text = await response.text();
|
|
if (!response.ok) {
|
|
throw new Error(text);
|
|
}
|
|
return text;
|
|
},
|
|
});
|