fix: earlier tool calls vanish from older chats when the conversation continues

Chats saved before v0.12.0, or imported from such an instance, where the model gave its tool calls the same ids in every round, lost every tool round but the last once the conversation continued, so the model no longer saw what its earlier calls returned. A tool call still waiting for approval or still running was also sent to the model as finished when a result had been saved next to it. Calls are now matched only with results from their own round and keep the status they were saved with, so the model gets the same history as before. New replies on current versions were not affected, since repeated ids are already renamed while the reply streams.
This commit is contained in:
Classic298 2026-10-26 11:03:27 +01:00
parent 8889e8d6af
commit 4b5de9251f

View file

@ -33,33 +33,36 @@ def normalize_tool_output(output):
return output
items = [dict(item) for item in output if isinstance(item, dict)]
deduped = []
positions = {}
round_items = {}
previous_kind = None
for item in items:
kind = item.get('type')
# Some providers restart call ids every round.
if kind != 'function_call_output' and previous_kind == 'function_call_output':
pair_results(round_items)
round_items = {}
previous_kind = kind
call_id = item.get('call_id') or item.get('id')
if kind in {'function_call', 'function_call_output'} and call_id:
key = (kind, call_id)
if key in positions:
deduped[positions[key]] = {**deduped[positions[key]], **item}
if key in round_items:
round_items[key].update(item)
continue
positions[key] = len(deduped)
round_items[key] = item
deduped.append(item)
return pair_results(deduped)
pair_results(round_items)
return deduped
def pair_results(deduped):
results = {item.get('call_id'): item for item in deduped if item.get('type') == 'function_call_output'}
for item in deduped:
if item.get('type') != 'function_call':
def pair_results(round_items):
for (kind, call_id), item in round_items.items():
if kind != 'function_call':
continue
result = results.get(item.get('call_id'))
result = round_items.get(('function_call_output', call_id))
if result:
if result.get('status'):
item['status'] = result['status']
for key in ('started_at', 'ended_at', 'duration', 'error_type'):
if key in result and key not in item:
item[key] = result[key]
elif item.get('status') == 'completed':
item['status'] = 'unknown'
return deduped