tool result fixing

This commit is contained in:
dal 2025-09-02 16:49:08 -06:00
parent 017971b776
commit 497598afe1
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
3 changed files with 41 additions and 6 deletions

View File

@ -33,7 +33,8 @@ export function createMessageUserClarifyingQuestionRawLlmMessageEntry(
state: MessageUserClarifyingQuestionState,
toolCallId: string
): ModelMessage | undefined {
if (!state.args) {
// Follow the same pattern as done-tool - use the extracted value, not raw args
if (!state.clarifyingQuestion) {
return undefined;
}
@ -44,7 +45,7 @@ export function createMessageUserClarifyingQuestionRawLlmMessageEntry(
type: 'tool-call',
toolCallId,
toolName: MESSAGE_USER_CLARIFYING_QUESTION_TOOL_NAME,
input: state.args,
input: { clarifying_question: state.clarifyingQuestion },
},
],
};

View File

@ -25,7 +25,8 @@ export function createRespondWithoutAssetCreationRawLlmMessageEntry(
state: RespondWithoutAssetCreationState,
toolCallId: string
): ModelMessage | undefined {
if (!state.args) {
// Follow the same pattern as done-tool - use the extracted value, not raw args
if (!state.final_response) {
return undefined;
}
@ -36,7 +37,7 @@ export function createRespondWithoutAssetCreationRawLlmMessageEntry(
type: 'tool-call',
toolCallId,
toolName: RESPOND_WITHOUT_ASSET_CREATION_TOOL_NAME,
input: state.args,
input: { final_response: state.final_response },
},
],
};

View File

@ -44,6 +44,39 @@ export async function updateMessageEntries({
throw new Error(`Message not found: ${messageId}`);
}
// Fix any stringified JSON inputs in rawLlmMessages before merging
const fixedRawLlmMessages = rawLlmMessages?.map((msg) => {
if (msg.role === 'assistant' && Array.isArray(msg.content)) {
const fixedContent = msg.content.map((item) => {
if (
typeof item === 'object' &&
'type' in item &&
item.type === 'tool-call' &&
'input' in item &&
typeof item.input === 'string'
) {
try {
// Try to parse the stringified JSON
const parsedInput = JSON.parse(item.input);
return {
...item,
input: parsedInput,
};
} catch {
// If parsing fails, keep the original
return item;
}
}
return item;
});
return {
...msg,
content: fixedContent,
};
}
return msg;
});
// Merge with new entries
const mergedEntries = {
responseMessages: responseMessages
@ -52,8 +85,8 @@ export async function updateMessageEntries({
reasoning: reasoningMessages
? mergeReasoningMessages(existingEntries.reasoning, reasoningMessages)
: existingEntries.reasoning,
rawLlmMessages: rawLlmMessages
? mergeRawLlmMessages(existingEntries.rawLlmMessages, rawLlmMessages)
rawLlmMessages: fixedRawLlmMessages
? mergeRawLlmMessages(existingEntries.rawLlmMessages, fixedRawLlmMessages)
: existingEntries.rawLlmMessages,
};