mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
fix(assistant): send the earlier answer back to the provider in the shape it saw it (#3094)
Follow-up messages failed with HTTP 400: a whitespace-only text block, two model messages in a row where the turn had pushed for an answer, or a tool call renamed after its provider state was recorded. The rebuilt history now matches what the provider saw during the first answer.
This commit is contained in:
@@ -60,10 +60,10 @@ export function toAnthropicMessages(messages: AiChatMessage[]): AnthropicMessage
|
||||
if (message.role === 'user') {
|
||||
push('user', [...(message.images ?? []).map(imageBlock), { type: 'text', text: message.text }]);
|
||||
} else if (message.role === 'assistant' && message.providerState?.adapter === 'anthropic-messages') {
|
||||
push('assistant', message.providerState.blocks.map(replayed));
|
||||
push('assistant', message.providerState.blocks.filter((block) => !isBlankText(block)).map(replayed));
|
||||
} else if (message.role === 'assistant') {
|
||||
push('assistant', [
|
||||
...(message.text ? [{ type: 'text', text: message.text }] : []),
|
||||
...(message.text.trim() ? [{ type: 'text', text: message.text }] : []),
|
||||
...message.toolCalls.map((call) => ({ type: 'tool_use', id: call.id, name: call.name, input: call.args })),
|
||||
]);
|
||||
} else {
|
||||
@@ -82,6 +82,15 @@ export function toAnthropicMessages(messages: AiChatMessage[]): AnthropicMessage
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* A text block of nothing but whitespace, which the Messages API refuses in a
|
||||
* request. A model that came back blank streams one now and then; it is left
|
||||
* out of the replay, which changes nothing the model said.
|
||||
*/
|
||||
function isBlankText(block: ContentBlock): boolean {
|
||||
return block.type === 'text' && !asText(block.text).trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* A block of an answer as it goes back to the provider: as it came, but a text
|
||||
* block without the citations it carried, which the provider checks against
|
||||
|
||||
@@ -261,6 +261,51 @@ function uniqueCallId(call: AiToolCall, used: Set<string>, newId: () => string):
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* The provider's own record of a step under the ids its calls go on with. A
|
||||
* call whose id the thread had used already is renamed (uniqueCallId), and
|
||||
* the record has to follow: Gemini finds a thought signature by the call's
|
||||
* id, Anthropic and the Responses API pair each answer with the call of the
|
||||
* same id, and a record left on the old id was refused on the next request
|
||||
* that replayed it, which for the last step of an answer is every later
|
||||
* message of the thread (#3094).
|
||||
*/
|
||||
function withRenamedCalls(state: AiProviderState | null, renamed: ReadonlyMap<string, string>): AiProviderState | null {
|
||||
if (!state || renamed.size === 0) return state;
|
||||
const idOf = (id: unknown) => (typeof id === 'string' ? (renamed.get(id) ?? id) : id);
|
||||
if (state.adapter === 'anthropic-messages') {
|
||||
return {
|
||||
...state,
|
||||
blocks: state.blocks.map((block) => (block.type === 'tool_use' ? { ...block, id: idOf(block.id) } : block)),
|
||||
};
|
||||
}
|
||||
if (state.adapter === 'openai-responses') {
|
||||
return {
|
||||
...state,
|
||||
items: state.items.map((item) =>
|
||||
item.type === 'function_call' ? { ...item, call_id: idOf(item.call_id) } : item,
|
||||
),
|
||||
};
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
signatures: Object.fromEntries(
|
||||
Object.entries(state.signatures).map(([id, signature]) => [renamed.get(id) ?? id, signature]),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
/** The ids the step's calls were renamed from, to the ones they go on with; an id one of its calls kept stays. */
|
||||
function renamedCalls(received: readonly AiToolCall[], kept: readonly AiToolCall[]): Map<string, string> {
|
||||
const keptIds = new Set(kept.map((call) => call.id));
|
||||
const renamed = new Map<string, string>();
|
||||
received.forEach((call, index) => {
|
||||
const id = kept[index]?.id;
|
||||
if (id && call.id !== id && !keptIds.has(call.id) && !renamed.has(call.id)) renamed.set(call.id, id);
|
||||
});
|
||||
return renamed;
|
||||
}
|
||||
|
||||
/** What every step of one turn shares. */
|
||||
interface Turn {
|
||||
deps: AgentDeps;
|
||||
@@ -576,8 +621,9 @@ async function runStep(turn: Turn, step: number): Promise<AgentOutcome | null> {
|
||||
w.startStep();
|
||||
const answer = await streamAnswer(turn, rowId);
|
||||
if ('end' in answer) return answer;
|
||||
const { text, providerState, cutOff } = answer;
|
||||
const { text, cutOff } = answer;
|
||||
const all = answer.received.map((call) => ({ ...call, id: uniqueCallId(call, input.usedCallIds, deps.newId) }));
|
||||
const providerState = withRenamedCalls(answer.providerState, renamedCalls(answer.received, all));
|
||||
// The follow-ups the model offers are the server's to answer: they never run, and alone they end the turn.
|
||||
const asks = all.filter((call) => call.name === ASSISTANT_FOLLOW_UPS_TOOL);
|
||||
const received = all.filter((call) => call.name !== ASSISTANT_FOLLOW_UPS_TOOL);
|
||||
|
||||
@@ -57,6 +57,15 @@ const NOT_APPLIED = JSON.stringify({ applied: false, reason: 'The user did not a
|
||||
/** What the model is told of a call it made that never ran, because the answer was stopped before it. */
|
||||
const NOT_RUN = 'This call did not run: the answer was stopped before it.';
|
||||
|
||||
/**
|
||||
* What stands between two steps of one answer where the first called nothing:
|
||||
* the turn pushed the model on there (an answer that only announced its
|
||||
* lookups, one cut off), and the push is never stored. Without it the replay
|
||||
* has one model message follow another, which Gemini refuses for a step that
|
||||
* makes calls, and with it every later message of the thread (#3094).
|
||||
*/
|
||||
const WENT_ON = 'Go on.';
|
||||
|
||||
/**
|
||||
* The calls a provider's own record of a step holds, which it replays as they
|
||||
* came and wants answered: Anthropic's tool uses, the Responses API's function
|
||||
@@ -330,8 +339,13 @@ export function toChatHistory(
|
||||
name: part.toolName,
|
||||
args: part.args,
|
||||
}));
|
||||
const text = textOf(parts);
|
||||
// A step that wrote only blank lines wrote nothing: the turn sent it on as empty, and Anthropic refuses
|
||||
// a text block of whitespace, so replaying it as text broke every later message of the thread (#3094).
|
||||
const written = textOf(parts);
|
||||
const text = written.trim() ? written : '';
|
||||
if (!text && toolCalls.length === 0) return;
|
||||
const before = history.at(-1);
|
||||
if (before?.role === 'assistant' && before.toolCalls.length === 0) history.push({ role: 'user', text: WENT_ON });
|
||||
const providerState = decodeJson(ASSISTANT_PROVIDER_STATE, row.provider_state, `message ${row.id}`);
|
||||
// The recorded calls that never ran go into the step's calls as well: an adapter that does not
|
||||
// replay this record rebuilds the step from them, and would otherwise find answers to calls it never made.
|
||||
|
||||
Reference in New Issue
Block a user