fix: stabilize custom widget authoring
This commit is contained in:
1 parent
d80e883479
commit
a1c2559541
11 files changed
+796
-27
No files matched your search
@@ -3,6 +3,14 @@ interface AssistantToolCallInput {
|
||||
input: string;
|
||||
}
|
||||
|
||||
const customWidgetNoInputToolNames = new Set([
|
||||
"customWidget_schema",
|
||||
"customWidget_getAuthoringPrompt",
|
||||
"customWidget_getSkill",
|
||||
"customWidget_getComponentCatalog",
|
||||
"customWidget_list",
|
||||
]);
|
||||
|
||||
const escapeControlCharactersInsideJsonStrings = (value: string) => {
|
||||
let result = "";
|
||||
let insideString = false;
|
||||
@@ -38,8 +46,15 @@ const escapeControlCharactersInsideJsonStrings = (value: string) => {
|
||||
case "\t":
|
||||
result += "\\t";
|
||||
break;
|
||||
case "\b":
|
||||
result += "\\b";
|
||||
break;
|
||||
case "\f":
|
||||
result += "\\f";
|
||||
break;
|
||||
default:
|
||||
result += character;
|
||||
result +=
|
||||
character.charCodeAt(0) < 0x20 ? `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}` : character;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -47,6 +62,18 @@ const escapeControlCharactersInsideJsonStrings = (value: string) => {
|
||||
|
||||
export const repairCustomWidgetToolInput = <T extends AssistantToolCallInput>(toolCall: T): T | null => {
|
||||
if (!toolCall.toolName.startsWith("customWidget_")) return null;
|
||||
if (customWidgetNoInputToolNames.has(toolCall.toolName)) {
|
||||
try {
|
||||
const input = JSON.parse(toolCall.input) as unknown;
|
||||
if (typeof input === "object" && input !== null && !Array.isArray(input) && Object.keys(input).length === 0) {
|
||||
return null;
|
||||
}
|
||||
} catch {
|
||||
// These read-only resource tools accept no input, so malformed provider arguments are
|
||||
// unambiguous and can be replaced without guessing at user data.
|
||||
}
|
||||
return { ...toolCall, input: "{}" };
|
||||
}
|
||||
const repairedInput = escapeControlCharactersInsideJsonStrings(toolCall.input);
|
||||
if (repairedInput === toolCall.input) return null;
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user