mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 05:20:43 +00:00
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import process from "node:process";
|
|
import type {
|
|
AgentToolResultMiddleware,
|
|
AgentToolResultMiddlewareEvent,
|
|
OpenClawAgentToolResult,
|
|
} from "openclaw/plugin-sdk/agent-harness";
|
|
import { createTokenjuiceOpenClawEmbeddedExtension } from "./runtime-api.js";
|
|
|
|
type TokenjuiceToolResultHandler = (
|
|
event: {
|
|
toolName: string;
|
|
input: Record<string, unknown>;
|
|
content: OpenClawAgentToolResult["content"];
|
|
details: unknown;
|
|
isError?: boolean;
|
|
},
|
|
ctx: { cwd: string },
|
|
) => Promise<Partial<OpenClawAgentToolResult> | void> | Partial<OpenClawAgentToolResult> | void;
|
|
|
|
function readCwd(event: AgentToolResultMiddlewareEvent): string {
|
|
if (event.cwd?.trim()) {
|
|
return event.cwd;
|
|
}
|
|
const workdir = event.args.workdir;
|
|
if (typeof workdir === "string" && workdir.trim()) {
|
|
return workdir;
|
|
}
|
|
return process.cwd();
|
|
}
|
|
|
|
export function createTokenjuiceAgentToolResultMiddleware(): AgentToolResultMiddleware {
|
|
const handlers: TokenjuiceToolResultHandler[] = [];
|
|
createTokenjuiceOpenClawEmbeddedExtension()({
|
|
on(event, handler) {
|
|
if (event === "tool_result") {
|
|
handlers.push(handler as TokenjuiceToolResultHandler);
|
|
}
|
|
},
|
|
});
|
|
|
|
return async (event) => {
|
|
let current = event.result;
|
|
for (const handler of handlers) {
|
|
const next = await handler(
|
|
{
|
|
toolName: event.toolName,
|
|
input: event.args,
|
|
content: current.content,
|
|
details: current.details,
|
|
isError: event.isError,
|
|
},
|
|
{ cwd: readCwd(event) },
|
|
);
|
|
if (next) {
|
|
current = Object.assign({}, current, {
|
|
content: next.content ?? current.content,
|
|
details: next.details ?? current.details,
|
|
});
|
|
}
|
|
}
|
|
return current === event.result ? undefined : { result: current };
|
|
};
|
|
}
|