Repository navigation
serve: a failed MCP tool result goes back to the model; before, wire … #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -612,7 +612,7 @@ <h3>This computer <small id="threads"></small></h3> | |
| // Export and import. | ||
| const download = (name, text, type) => { const a = el('a'); a.href = URL.createObjectURL(new Blob([text], { type })); a.download = name; a.click(); setTimeout(() => URL.revokeObjectURL(a.href), 1000); }; | ||
| const pack = list => JSON.stringify({ version: 1, chats: list }, null, 1); | ||
| const chatMd = c => `# ${c.title}\n\n` + c.msgs.filter(m => !m.error && (m.content || m.role !== 'assistant')).map(m => m.role === 'tool' ? `**tool ${m.name}:**\n\n~~~\n${m.content}\n~~~` : `**${m.role === 'user' ? 'you' : 'model'}:**\n\n${m.content}`).join('\n\n---\n\n') + '\n'; | ||
| const chatMd = c => `# ${c.title}\n\n` + c.msgs.filter(m => (m.role === 'tool' || !m.error) && (m.content || m.role !== 'assistant')).map(m => m.role === 'tool' ? `**${m.error ? 'tool failed' : 'tool'} ${m.name}:**\n\n~~~\n${m.content}\n~~~` : `**${m.role === 'user' ? 'you' : 'model'}:**\n\n${m.content}`).join('\n\n---\n\n') + '\n'; | ||
| // Takes chats from an export file; an existing chat is replaced only by a newer one. Throws on any other file. | ||
| function merge(text) { | ||
| const j = JSON.parse(text), inc = Array.isArray(j) ? j : j?.chats; | ||
|
|
@@ -881,10 +881,14 @@ <h3>This computer <small id="threads"></small></h3> | |
| // end of the request as the start of its answer and returns only the new text. | ||
| // The chat as the API takes it. `open` is the reply being continued (its thinking goes along). | ||
| function wire(msgs, open) { | ||
| return msgs.filter(x => !x.error).map(x => { | ||
| // `error` means two different things. On an assistant message the turn never happened, so there is nothing to send. | ||
| // On a tool message it means the tool failed, and that failure is the answer the model is waiting for: dropping it | ||
| // leaves the call unanswered, and a model with an unanswered call repeats the same call instead of noticing. | ||
| return msgs.filter(x => !x.error || x.role === 'tool').map(x => { | ||
| const imgs = x.images || []; | ||
| if (imgs.length) return { role: x.role, content: [{ type: 'text', text: x.content }, ...imgs.map(url => ({ type: 'image_url', image_url: { url } }))] }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · Correctness · 2 of 4 Image branch returns before the tool branch, so failed tool results with images lose tool_call_id and the error prefix. A tool message that has images and error set hits the earlier Prompt for your AI agentReply |
||
| if (x.role === 'tool') return { role: 'tool', tool_call_id: x.id, content: x.content }; | ||
| // Say it is an error in the text too: a failed result otherwise looks like a payload that happens to be prose. | ||
| if (x.role === 'tool') return { role: 'tool', tool_call_id: x.id, content: x.error && !/^(Error: |The tool failed: )/.test(x.content) ? `Error: ${x.content}` : x.content }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low · Maintainability · 3 of 4 Error status is recovered by sniffing the content string with a regex instead of being carried as data. Whether a failed result gets the 'Error: ' prefix depends on the text matching /^(Error: |The tool failed: )/. Any upstream change to how tool failures are worded silently changes what the model receives, and a tool whose output happens to begin with 'Error: ' is treated as already formatted. The prefix rule is duplicated across code that produces these strings and this regex. Prompt for your AI agentReply |
||
| if (x.calls?.length) return { role: 'assistant', content: x.content, tool_calls: x.calls.map(c => ({ id: c.id, type: 'function', function: { name: c.name, arguments: c.args || '{}' } })) }; | ||
| return x === open && x.think ? { role: x.role, content: x.content, reasoning_content: x.think } : { role: x.role, content: x.content }; | ||
| }); | ||
|
|
@@ -1296,6 +1300,13 @@ <h3>This computer <small id="threads"></small></h3> | |
| { role: 'tool', id: 'call_0_0', name: 'a__x', content: 'r' }, { role: 'assistant', content: 'done' }]); | ||
| return w[1].tool_calls[0].function.arguments === '{"k":1}' && w[1].tool_calls[0].id === 'call_0_0' && w[2].role === 'tool' && w[2].tool_call_id === 'call_0_0' && w[2].content === 'r' && !w[3].tool_calls; | ||
| }], | ||
| // `error` on a tool message is the tool's answer, so it goes; on an assistant message the turn never happened. | ||
| ['tool errors go to the model, transport errors do not', () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 Low · Maintainability · 4 of 4 New test covers only the text-tool-error path, not images, missing error text, or assistant-with-calls errors. The added test passes with the current code but does not exercise the image branch, a tool error whose content is undefined (which yields 'Error: undefined'), or an errored assistant message that carries calls. Regressions in those paths, including the orphaned-tool-message problem, would ship unnoticed. Prompt for your AI agentReply |
||
| const w = wire([{ role: 'user', content: 'q' }, { role: 'assistant', content: '', calls: [{ id: 'c1', name: 'a__x', args: '{}' }, { id: 'c2', name: 'a__y', args: '{}' }] }, | ||
| { role: 'tool', id: 'c1', name: 'a__x', content: 'boom', error: true }, | ||
| { role: 'tool', id: 'c2', name: 'a__y', content: 'The tool failed: Bad Gateway', error: true }, { role: 'assistant', content: 'gone', error: true }]); | ||
| return w.length === 4 && w[1].tool_calls.length === 2 && w[2].role === 'tool' && w[2].tool_call_id === 'c1' && w[3].tool_call_id === 'c2' && w[2].content === 'Error: boom' && w[3].content === 'The tool failed: Bad Gateway'; | ||
| }], | ||
| ['tool messages export and import', () => { | ||
| const c = { id: 't1', title: 't', at: 1, msgs: [{ role: 'user', content: 'q' }, { role: 'assistant', content: '', calls: [{ id: 'c1', name: 'a__x', args: '{}' }] }, { role: 'tool', id: 'c1', name: 'a__x', content: 'r' }, { role: 'assistant', content: 'done' }] }; | ||
| const bad = { ...c, msgs: [{ role: 'assistant', content: '', calls: 'x' }] }, md = chatMd(c); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High · Correctness · 1 of 4
Tool results are kept even when their assistant turn with the calls is dropped as an error, which orphans them. An assistant message that issued tool calls is flagged error (for example, the stream dies after the calls were parsed) while tool results for those calls were already appended. The filter now drops the assistant message with its tool_calls but keeps the role 'tool' messages. The request then contains tool messages with no preceding assistant tool_calls, which the chat API rejects, so the conversation cannot continue.
Prompt for your AI agent
Reply
/dismiss(optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.