迁移到 bun 框架,移除 js 文件依赖;增加对 tools 参数的校验
This commit is contained in:
+30
-2
@@ -230,6 +230,27 @@ const getErrorMessage = (error: {
|
||||
data?: { message?: string };
|
||||
}) => error.data?.message ?? error.name;
|
||||
|
||||
const isObjectRecord = (value: unknown): value is Record<string, unknown> =>
|
||||
typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
|
||||
const normalizeToolParams = (value: unknown): Record<string, unknown> => {
|
||||
if (isObjectRecord(value)) {
|
||||
return value;
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
try {
|
||||
const parsed = JSON.parse(value) as unknown;
|
||||
return isObjectRecord(parsed) ? parsed : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
const hasToolParams = (params: Record<string, unknown>) =>
|
||||
Object.keys(params).length > 0;
|
||||
|
||||
type StreamPromptOptions = {
|
||||
runtime: OpencodeRuntimeAdapter;
|
||||
opencodeSessionId: string;
|
||||
@@ -386,6 +407,10 @@ const streamPromptResponse = async ({
|
||||
});
|
||||
}
|
||||
if (part.type === "tool") {
|
||||
const toolParams = normalizeToolParams(part.state.input);
|
||||
const isToolFinalState =
|
||||
part.state.status === "completed" || part.state.status === "error";
|
||||
|
||||
write("progress", {
|
||||
session_id: clientSessionId,
|
||||
id: part.id,
|
||||
@@ -394,12 +419,15 @@ const streamPromptResponse = async ({
|
||||
title: getToolProgressTitle(part.tool, part.state.status),
|
||||
detail: part.state.status === "error" ? part.state.error : undefined,
|
||||
});
|
||||
if (!emittedToolParts.has(part.id)) {
|
||||
if (
|
||||
!emittedToolParts.has(part.id) &&
|
||||
(hasToolParams(toolParams) || isToolFinalState)
|
||||
) {
|
||||
emittedToolParts.add(part.id);
|
||||
write("tool_call", {
|
||||
session_id: clientSessionId,
|
||||
tool: part.tool,
|
||||
params: part.state.input,
|
||||
params: toolParams,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user