Skip to content

fix: synthesize assistant function_call before orphaned function results (422) - #5

Open
PetrIIvanov wants to merge 1 commit into
Overman775:mainfrom
PetrIIvanov:fix/function-result-pairing
Open

PetrIIvanov wants to merge 1 commit into
Overman775:mainfrom
PetrIIvanov:fix/function-result-pairing

Conversation

@PetrIIvanov

Copy link
Copy Markdown

Problem

GigaChat requires every function result in the history to be immediately preceded by an assistant message carrying a function_call. opencode can send orphaned tool results in long sessions — after compaction or when the assistant produced text between tool calls — which the API rejects with 422 INVALID_PARAMS (every function result must have an assistant function call in history).

Fix

translateMessages in src/plugin/messages.ts now tracks whether the last appended message is an assistant carrying a function_call. When appending a function/tool result that is not preceded by one, the plugin synthesizes a matching assistant message with an empty function_call so the pair is valid.

Tests

Added a unit test with an orphaned tool result that asserts a synthesized assistant function_call is inserted before the function result. All 77 tests pass.

GigaChat requires every 'function' result in the history to be immediately
preceded by an 'assistant' message carrying a function_call. opencode can
send orphaned tool results in long sessions (after compaction, or when the
assistant produced text between tool calls), which the API rejects with 422
('every function result must have an assistant function call in history').
translateMessages now tracks whether the last appended message is an
assistant with a function_call and synthesizes a matching assistant message
when one is missing.
@PetrIIvanov

Copy link
Copy Markdown
Author

Локальная проверка (Node 20, эквивалент всех шагов CI):

  • npm test → 77/77 passed (включая новый тест на парность function result)
  • npm run build:release → OK (tsc без ошибок, бандл собирается)
  • npm run test:release → standalone release passes without node_modules

Воспроизведение бага: GigaChat-API возвращал 422 INVALID_PARAMS (every function result must have an assistant function call in history) в длинных сессиях, когда opencode присылал tool-результат без предшествующего assistant-сообщения с function_call (например, после compaction контекста или когда ассистент выдавал текст между вызовами инструментов).

Фикс в translateMessages гарантирует парность assistant(function_call) → function(result): если перед результатом нет парного вызова, плагин вставляет синтетический assistant с пустым function_call. Реальный агентный цикл (несколько инструментов подряд) проверен и работает.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant