fix(llm): 保留原始协议历史并验证跨协议无损往返
This commit is contained in:
1 parent
c368939151
commit
30aa47281d
7 files changed
+210
-111
No files matched your search
@@ -54,8 +54,8 @@
|
||||
en: e9aa65cd3417fc05
|
||||
zh: 4568471e712b055e
|
||||
/deepseek-ai-dsh-llm-pi-ai/understand-the-implementation/replay-and-vocabulary:
|
||||
en: b88ed50779cbee2a
|
||||
zh: 082d8a281f330ce7
|
||||
en: 73242ca9b8a98c76
|
||||
zh: 4ab98526ae70bd23
|
||||
/deepseek-ai-dsh-llm-pi-ai/further-exploration:
|
||||
en: a2a2ccdeae59d408
|
||||
zh: c47ee091aa90ad91
|
||||
|
||||
@@ -163,7 +163,7 @@ The plugin declares every installed catalog provider it can authenticate in the
|
||||
|
||||
### Replay and vocabulary
|
||||
|
||||
Successful assistant responses store a versioned, lossless-JSON replay state beside the provider and model that produced them — response-level facts plus one per-block entry per streamed block. At request time, `LlmRuntime` passes replay state only when the same adapter instance owns both routes; the adapter validates it and restores native response ids, provider signatures, and optional `providerThinkingLevel` effort metadata, keeping absent effort metadata absent. Replay validates the requested model identity against the assistant source and uses it to retain same-model signatures; provider-reported response models remain diagnostic metadata. Absent or unusable replay state degrades to provider-neutral content while preserving the assistant source’s required provider and model. pi-ai tool-call arguments are parsed objects, so the adapter parses input and re-stringifies output to the harness raw-JSON convention; pi-ai in-stream error events map to terminal `finish` chunks. Text items that emit no content are omitted from assistant streams and replay metadata; whitespace and non-empty text remain intact. A response containing only omitted text items fails with `EMPTY_RESPONSE`.
|
||||
Successful assistant responses store a versioned, lossless-JSON replay state beside the provider and model that produced them — response-level facts plus one per-block entry per streamed block. At request time, `LlmRuntime` passes replay state only when the same adapter instance owns both routes; the adapter validates it and restores native response ids, provider signatures, and optional `providerThinkingLevel` effort metadata, keeping absent effort metadata absent. Replay validates the requested model identity against the assistant source and uses it to retain same-model signatures; provider-reported response models remain diagnostic metadata. Absent or unusable replay state degrades to provider-neutral content while preserving the assistant source’s required provider and model. pi-ai tool-call arguments are parsed objects, so the adapter parses input and re-stringifies output to the harness raw-JSON convention; pi-ai in-stream error events map to terminal `finish` chunks. Protocol conversion operates on request-local copies. Switching from A to B and back to A preserves the stored content, including empty text blocks, tool ids, original block order, and native replay metadata; unsupported signatures are omitted only from the foreign request.
|
||||
|
||||
</details>
|
||||
|
||||
|
||||
@@ -163,7 +163,7 @@ Config 更新严格验证发生变化的 provider。初始加载将已存储的
|
||||
|
||||
### 回放与词汇
|
||||
|
||||
成功 assistant 响应会存储带版本的、无损 JSON 回放状态,与产生它们的提供方和模型放在一起——响应级事实加每个流式块一条逐块条目。请求时,`LlmRuntime` 仅当同一适配器实例拥有两条路由时才传递回放状态;适配器校验它并恢复原生响应 id、提供方签名与可选的 `providerThinkingLevel` effort 元数据,缺失的 effort 元数据仍保持缺失。回放会对照 assistant 来源校验请求模型身份,并用它保留同模型签名;提供方报告的响应模型仅作为诊断元数据。回放状态缺失或无法使用时会降级为提供方无关内容,并保留 assistant 来源中必需的提供方和模型。pi-ai 工具调用参数是解析后的对象,因此适配器解析输入并重新字符串化输出,以符合 harness 原始 JSON 约定;pi-ai 流内错误事件映射为终止 `finish` 分片。 没有输出任何内容的文本项会从 assistant 流和回放元数据中省略;空白字符和非空文本保持原样。仅包含被省略文本项的响应以 `EMPTY_RESPONSE` 失败。
|
||||
成功 assistant 响应会存储带版本的、无损 JSON 回放状态,与产生它们的提供方和模型放在一起——响应级事实加每个流式块一条逐块条目。请求时,`LlmRuntime` 仅当同一适配器实例拥有两条路由时才传递回放状态;适配器校验它并恢复原生响应 id、提供方签名与可选的 `providerThinkingLevel` effort 元数据,缺失的 effort 元数据仍保持缺失。回放会对照 assistant 来源校验请求模型身份,并用它保留同模型签名;提供方报告的响应模型仅作为诊断元数据。回放状态缺失或无法使用时会降级为提供方无关内容,并保留 assistant 来源中必需的提供方和模型。pi-ai 工具调用参数是解析后的对象,因此适配器解析输入并重新字符串化输出,以符合 harness 原始 JSON 约定;pi-ai 流内错误事件映射为终止 `finish` 分片。 协议转换仅作用于请求副本。从 A 切换到 B 再切回 A 时,存储的内容保持完整,包括空文本块、工具 id、原始块顺序和原生回放元数据;不受支持的签名仅从发给其他协议的请求中省略。
|
||||
|
||||
</details>
|
||||
|
||||
|
||||
@@ -128,8 +128,7 @@ export function mapStopReason(message: AssistantMessage, contextWindow?: number)
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate the pi-ai event stream into StreamChunks, omitting text items that
|
||||
* never emit content. Replay metadata follows the emitted block order. pi-ai never throws
|
||||
* Translate the pi-ai event stream into StreamChunks. pi-ai never throws
|
||||
* mid-stream — failures arrive as `error` events, which become error/aborted
|
||||
* `finish` chunks (the harness protocol's other error-delivery style).
|
||||
* @param events - one assistant turn's pi-ai event stream.
|
||||
@@ -146,8 +145,8 @@ export async function* toStreamChunks(
|
||||
callerSignal?: AbortSignal,
|
||||
requestedModel?: string,
|
||||
): AsyncGenerator<StreamChunk> {
|
||||
// Keep provider indices; deferred text starts can leave gaps or change arrival order.
|
||||
const started = new Set<number>()
|
||||
// pi-ai contentIndex ↔ our block index map 1:1 (both count blocks from 0
|
||||
// in stream order), but we track ids per index for tool calls.
|
||||
const toolIds = new Map<number, { id: string; name: string }>()
|
||||
|
||||
for await (const event of events) {
|
||||
@@ -155,25 +154,15 @@ export async function* toStreamChunks(
|
||||
case 'start':
|
||||
break
|
||||
case 'text_start':
|
||||
yield { type: 'block-start', index: event.contentIndex, blockType: 'text' }
|
||||
break
|
||||
case 'text_delta':
|
||||
if (event.delta.length === 0) break
|
||||
if (!started.has(event.contentIndex)) {
|
||||
started.add(event.contentIndex)
|
||||
yield { type: 'block-start', index: event.contentIndex, blockType: 'text' }
|
||||
}
|
||||
yield { type: 'text-delta', index: event.contentIndex, text: event.delta }
|
||||
break
|
||||
case 'text_end':
|
||||
if (!started.has(event.contentIndex)) {
|
||||
if (event.content.length === 0) break
|
||||
started.add(event.contentIndex)
|
||||
yield { type: 'block-start', index: event.contentIndex, blockType: 'text' }
|
||||
}
|
||||
yield { type: 'block-end', index: event.contentIndex, block: { type: 'text', text: event.content } }
|
||||
break
|
||||
case 'thinking_start':
|
||||
started.add(event.contentIndex)
|
||||
yield { type: 'block-start', index: event.contentIndex, blockType: 'reasoning' }
|
||||
break
|
||||
case 'thinking_delta':
|
||||
@@ -183,7 +172,6 @@ export async function* toStreamChunks(
|
||||
yield { type: 'block-end', index: event.contentIndex, block: { type: 'reasoning', text: event.content } }
|
||||
break
|
||||
case 'toolcall_start': {
|
||||
started.add(event.contentIndex)
|
||||
// The id/name live on the partial's content at this index.
|
||||
const partial = event.partial.content[event.contentIndex]
|
||||
const id = partial?.type === 'toolCall' ? partial.id : ''
|
||||
@@ -217,23 +205,14 @@ export async function* toStreamChunks(
|
||||
},
|
||||
}
|
||||
break
|
||||
case 'done': {
|
||||
const indices = new Set([...started, ...event.message.content.keys()])
|
||||
const message = {
|
||||
...event.message,
|
||||
content: [...indices].flatMap((index) => {
|
||||
const block = event.message.content[index]
|
||||
return block === undefined || (block.type === 'text' && block.text.length === 0 && !started.has(index)) ? [] : [block]
|
||||
}),
|
||||
}
|
||||
case 'done':
|
||||
yield { type: 'usage', usage: mapUsage(event.message.usage) }
|
||||
yield {
|
||||
type: 'finish',
|
||||
reason: mapStopReason(message, contextWindow),
|
||||
replayState: toPiReplayState(message, requestedModel),
|
||||
reason: mapStopReason(event.message, contextWindow),
|
||||
replayState: toPiReplayState(event.message, requestedModel),
|
||||
}
|
||||
return
|
||||
}
|
||||
case 'error':
|
||||
// In-stream error delivery (pi-ai's style) → error finish chunk
|
||||
// (the harness's other sanctioned error path besides throwing).
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { AttachmentId, ImageVariantId } from '@deepseek-ai/dsh-attachment'
|
||||
import type { AttachmentStore, ImageAttachmentRef, ImageRequestTarget, RequestImageAttachment } from '@deepseek-ai/dsh-attachment'
|
||||
import { BlockAssembler, createToolResultMessage, createUserMessage, ToolCallId, CONTEXT_WINDOW_EXCEEDED_CODE, EMPTY_RESPONSE_CODE, createMessage } from '@deepseek-ai/dsh-llm'
|
||||
import { createToolResultMessage, createUserMessage, ToolCallId, CONTEXT_WINDOW_EXCEEDED_CODE, EMPTY_RESPONSE_CODE, createMessage } from '@deepseek-ai/dsh-llm'
|
||||
import type { ContentBlock, StreamChunk } from '@deepseek-ai/dsh-llm'
|
||||
import type { AssistantMessage, AssistantMessageEvent, Usage } from '@earendil-works/pi-ai'
|
||||
import { transformMessages } from '@earendil-works/pi-ai/api/transform-messages'
|
||||
@@ -737,15 +737,13 @@ describe('toStreamChunks', () => {
|
||||
const chunks = await collect(toStreamChunks(feed(
|
||||
{ type: 'start', partial: assistant() },
|
||||
{ type: 'text_start', contentIndex: 0, partial: assistant() },
|
||||
{ type: 'text_delta', contentIndex: 0, delta: 'h', partial: assistant() },
|
||||
{ type: 'text_delta', contentIndex: 0, delta: 'i', partial: assistant() },
|
||||
{ type: 'text_delta', contentIndex: 0, delta: 'hi', partial: assistant() },
|
||||
{ type: 'text_end', contentIndex: 0, content: 'hi', partial: assistant() },
|
||||
{ type: 'done', reason: 'stop', message: done },
|
||||
)))
|
||||
expect(chunks).toEqual([
|
||||
{ type: 'block-start', index: 0, blockType: 'text' },
|
||||
{ type: 'text-delta', index: 0, text: 'h' },
|
||||
{ type: 'text-delta', index: 0, text: 'i' },
|
||||
{ type: 'text-delta', index: 0, text: 'hi' },
|
||||
{ type: 'block-end', index: 0, block: { type: 'text', text: 'hi' } },
|
||||
{ type: 'usage', usage: { inputTokens: 3, outputTokens: 2, totalTokens: 5 } },
|
||||
{
|
||||
@@ -766,77 +764,6 @@ describe('toStreamChunks', () => {
|
||||
])
|
||||
})
|
||||
|
||||
it.each(['', ' ', 'finished'])('settles a final text item %j beside tool calls with aligned replay metadata', async (text) => {
|
||||
const tool = { type: 'toolCall' as const, id: 'call|item', name: 'read', arguments: {} }
|
||||
const native = assistant({ api: 'openai-responses', stopReason: 'toolUse', content: [
|
||||
tool, { type: 'text', text, textSignature: 'message-id' },
|
||||
] })
|
||||
const chunks = await collect(toStreamChunks(feed(
|
||||
{ type: 'toolcall_start', contentIndex: 0, partial: native },
|
||||
{ type: 'toolcall_end', contentIndex: 0, toolCall: tool, partial: native },
|
||||
{ type: 'text_start', contentIndex: 1, partial: native },
|
||||
{ type: 'text_delta', contentIndex: 1, delta: '', partial: native },
|
||||
{ type: 'text_end', contentIndex: 1, content: text, partial: native },
|
||||
{ type: 'done', reason: 'toolUse', message: native },
|
||||
)))
|
||||
const assembler = new BlockAssembler()
|
||||
for (const chunk of chunks) assembler.push(chunk)
|
||||
expect(assembler.blocks()).toEqual([
|
||||
{ type: 'tool-call', id: tool.id, name: tool.name, arguments: '{}' },
|
||||
...text.length === 0 ? [] : [{ type: 'text', text }],
|
||||
])
|
||||
expect(assembler.replayState?.blocks).toEqual([
|
||||
{ type: 'tool-call' }, ...text.length === 0 ? [] : [{ type: 'text', textSignature: 'message-id' }],
|
||||
])
|
||||
expect(chunks.filter(chunk => 'index' in chunk && chunk.index === 1)).toHaveLength(text.length === 0 ? 0 : 2)
|
||||
expect(native.content).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('keeps replay aligned if a provider replaces streamed text with an empty final item', async () => {
|
||||
const native = assistant({ content: [{ type: 'text', text: '', textSignature: 'final-id' }] })
|
||||
const assembler = new BlockAssembler()
|
||||
for await (const chunk of toStreamChunks(feed(
|
||||
{ type: 'text_start', contentIndex: 0, partial: native },
|
||||
{ type: 'text_delta', contentIndex: 0, delta: 'draft', partial: native },
|
||||
{ type: 'text_end', contentIndex: 0, content: '', partial: native },
|
||||
{ type: 'done', reason: 'stop', message: native },
|
||||
))) assembler.push(chunk)
|
||||
expect(assembler.blocks()).toEqual([{ type: 'text', text: '' }])
|
||||
expect(assembler.replayState?.blocks).toEqual([{ type: 'text', textSignature: 'final-id' }])
|
||||
})
|
||||
|
||||
it('reports an empty response when the only text item has no content', async () => {
|
||||
const native = assistant({ content: [{ type: 'text', text: '' }] })
|
||||
const chunks = await collect(toStreamChunks(feed(
|
||||
{ type: 'text_start', contentIndex: 0, partial: native },
|
||||
{ type: 'text_end', contentIndex: 0, content: '', partial: native },
|
||||
{ type: 'done', reason: 'stop', message: native },
|
||||
)))
|
||||
expect(chunks).toHaveLength(2)
|
||||
expect(chunks.at(-1)).toMatchObject({ type: 'finish', reason: { kind: 'error', failure: { code: EMPTY_RESPONSE_CODE } }, replayState: { blocks: [] } })
|
||||
})
|
||||
|
||||
it('aligns replay signatures when deferred text starts after an interleaved thinking block', async () => {
|
||||
const native = assistant({ content: [
|
||||
{ type: 'text', text: 'answer', textSignature: 'text-id' },
|
||||
{ type: 'thinking', thinking: 'reason', thinkingSignature: 'thinking-id' },
|
||||
] })
|
||||
const chunks = await collect(toStreamChunks(feed(
|
||||
{ type: 'text_start', contentIndex: 0, partial: native },
|
||||
{ type: 'thinking_start', contentIndex: 1, partial: native },
|
||||
{ type: 'thinking_end', contentIndex: 1, content: 'reason', partial: native },
|
||||
{ type: 'text_delta', contentIndex: 0, delta: 'answer', partial: native },
|
||||
{ type: 'text_end', contentIndex: 0, content: 'answer', partial: native },
|
||||
{ type: 'done', reason: 'stop', message: native },
|
||||
)))
|
||||
const assembler = new BlockAssembler()
|
||||
for (const chunk of chunks) assembler.push(chunk)
|
||||
expect(assembler.blocks()).toEqual([{ type: 'reasoning', text: 'reason' }, { type: 'text', text: 'answer' }])
|
||||
expect(assembler.replayState?.blocks).toEqual([
|
||||
{ type: 'reasoning', thinkingSignature: 'thinking-id' }, { type: 'text', textSignature: 'text-id' },
|
||||
])
|
||||
})
|
||||
|
||||
it('maps thinking events to reasoning blocks', async () => {
|
||||
const chunks = await collect(toStreamChunks(feed(
|
||||
{ type: 'thinking_start', contentIndex: 0, partial: assistant() },
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
/** Cross-protocol requests must not overwrite the history needed by its original provider. */
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { Context } from '@deepseek-ai/cordis'
|
||||
import LlmRuntime, { createToolResultMessage, createUserMessage } from '@deepseek-ai/dsh-llm'
|
||||
import type { Message } from '@deepseek-ai/dsh-llm'
|
||||
import * as LlmPiAi from '@deepseek-ai/dsh-llm-pi-ai'
|
||||
import * as DeepSeek from '@deepseek-ai/dsh-llm-deepseek-api-key'
|
||||
import { assemble } from './assemble.ts'
|
||||
import { anthropicFrame, closeMockServers, mockServer } from './mock-server.ts'
|
||||
|
||||
type Api = 'anthropic-messages' | 'openai-responses' | 'openai-completions'
|
||||
const apis: Api[] = ['anthropic-messages', 'openai-responses', 'openai-completions']
|
||||
const contexts: Context[] = []
|
||||
afterEach(async () => {
|
||||
await Promise.all(contexts.splice(0).map(ctx => ctx.fiber.dispose()))
|
||||
await closeMockServers()
|
||||
vi.unstubAllEnvs()
|
||||
})
|
||||
|
||||
function responsesFrames(tools: boolean): string[] {
|
||||
const items = tools ? [
|
||||
{ type: 'reasoning', id: 'rs_original', encrypted_content: 'encrypted-reasoning', summary: [{ type: 'summary_text', text: 'source reasoning' }] },
|
||||
{ type: 'message', id: 'msg_original', role: 'assistant', content: [{ type: 'output_text', text: 'source text', annotations: [] }] },
|
||||
...[0, 1].map(index => ({ type: 'function_call', id: `fc_${index}`, call_id: `call_${index}`, name: 'lookup', arguments: JSON.stringify({ key: index }) })),
|
||||
{ type: 'message', id: 'msg_empty', role: 'assistant', content: [{ type: 'output_text', text: '', annotations: [] }] },
|
||||
] : [{ type: 'message', id: 'msg_reply', role: 'assistant', content: [{ type: 'output_text', text: 'reply', annotations: [] }] }]
|
||||
return [
|
||||
JSON.stringify({ type: 'response.created', response: { id: 'resp_original' } }),
|
||||
...items.flatMap((item, output_index) => [
|
||||
JSON.stringify({ type: 'response.output_item.added', output_index, item }),
|
||||
JSON.stringify({ type: 'response.output_item.done', output_index, item }),
|
||||
]),
|
||||
JSON.stringify({ type: 'response.completed', response: {
|
||||
id: 'resp_original', status: 'completed', output: items, usage: { input_tokens: 3, output_tokens: 1, total_tokens: 4 },
|
||||
} }),
|
||||
]
|
||||
}
|
||||
|
||||
function anthropicFrames(tools: boolean): ReturnType<typeof anthropicFrame>[] {
|
||||
const blocks = tools ? [
|
||||
{ type: 'thinking', thinking: 'source reasoning', signature: 'signed-reasoning' },
|
||||
{ type: 'redacted_thinking', data: 'encrypted-redacted-reasoning' },
|
||||
{ type: 'text', text: 'source text' },
|
||||
...[0, 1].map(index => ({ type: 'tool_use', id: `toolu_${index}`, name: 'lookup', input: { key: index } })),
|
||||
{ type: 'text', text: '' },
|
||||
] : [{ type: 'text', text: 'reply' }]
|
||||
return [
|
||||
anthropicFrame({ type: 'message_start', message: {
|
||||
id: 'msg_original', type: 'message', role: 'assistant', model: 'model', content: [],
|
||||
stop_reason: null, stop_sequence: null, usage: { input_tokens: 3, output_tokens: 1 },
|
||||
} }),
|
||||
...blocks.flatMap((content_block, index) => [
|
||||
anthropicFrame({ type: 'content_block_start', index, content_block }),
|
||||
anthropicFrame({ type: 'content_block_stop', index }),
|
||||
]),
|
||||
anthropicFrame({ type: 'message_delta', delta: { stop_reason: tools ? 'tool_use' : 'end_turn', stop_sequence: null }, usage: { output_tokens: 1 } }),
|
||||
anthropicFrame({ type: 'message_stop' }),
|
||||
]
|
||||
}
|
||||
|
||||
function frames(api: Api, tools = false): (string | ReturnType<typeof anthropicFrame>)[] {
|
||||
if (api === 'openai-responses') return responsesFrames(tools)
|
||||
if (api === 'anthropic-messages') return anthropicFrames(tools)
|
||||
return [
|
||||
JSON.stringify({ choices: [{ index: 0, delta: {
|
||||
role: 'assistant', content: tools ? 'source text' : 'reply',
|
||||
...tools ? {
|
||||
reasoning_content: 'source reasoning',
|
||||
tool_calls: [0, 1].map(index => ({ index, id: `call_${index}`, type: 'function', function: { name: 'lookup', arguments: JSON.stringify({ key: index }) } })),
|
||||
} : {},
|
||||
}, finish_reason: null }] }),
|
||||
JSON.stringify({ choices: [{ index: 0, delta: {}, finish_reason: tools ? 'tool_calls' : 'stop' }], usage: { prompt_tokens: 3, completion_tokens: 1 } }),
|
||||
'[DONE]',
|
||||
]
|
||||
}
|
||||
|
||||
function wireHistory(request: unknown, api: Api): unknown[] {
|
||||
expect(request).toBeTypeOf('object')
|
||||
const history = (request as Record<string, unknown>)[api === 'openai-responses' ? 'input' : 'messages']
|
||||
if (!Array.isArray(history)) throw new Error('Expected a wire message array')
|
||||
return history
|
||||
}
|
||||
|
||||
function expectToolPairs(request: unknown, api: Api): void {
|
||||
const messages = wireHistory(request, api) as Record<string, unknown>[]
|
||||
const calls: unknown[] = []
|
||||
const results: unknown[] = []
|
||||
for (const message of messages) {
|
||||
if (api === 'openai-responses') {
|
||||
if (message['type'] === 'function_call') calls.push(message['call_id'])
|
||||
if (message['type'] === 'function_call_output') results.push(message['call_id'])
|
||||
} else if (api === 'openai-completions') {
|
||||
for (const call of (message['tool_calls'] ?? []) as Record<string, unknown>[]) calls.push(call['id'])
|
||||
if (message['role'] === 'tool') results.push(message['tool_call_id'])
|
||||
} else if (Array.isArray(message['content'])) {
|
||||
for (const block of message['content'] as Record<string, unknown>[]) {
|
||||
if (block['type'] === 'tool_use') calls.push(block['id'])
|
||||
if (block['type'] === 'tool_result') results.push(block['tool_use_id'])
|
||||
}
|
||||
}
|
||||
}
|
||||
expect(calls).toHaveLength(2)
|
||||
expect(calls.every(id => typeof id === 'string' && id.length > 0)).toBe(true)
|
||||
expect(new Set(calls).size).toBe(2)
|
||||
expect(results).toEqual(calls)
|
||||
}
|
||||
|
||||
const tool = { name: 'lookup', description: 'Look up a value.', parameters: { type: 'object', properties: { key: { type: 'number' } }, required: ['key'] } }
|
||||
const pairs: { a: Api; b: Api | 'deepseek-messages' }[] = [
|
||||
...apis.flatMap(a => apis.filter(b => b !== a).map(b => ({ a, b }))),
|
||||
...apis.map(a => ({ a, b: 'deepseek-messages' as const })),
|
||||
]
|
||||
|
||||
describe('protocol history round trips', () => {
|
||||
it.each(pairs)('$a → $b → $a preserves saved content and restores native replay', async ({ a, b }) => {
|
||||
vi.stubEnv('PROTOCOL_ROUNDTRIP_KEY', 'test-key')
|
||||
const source = await mockServer([{ events: frames(a, true) }, { events: frames(a) }, { events: frames(a) }])
|
||||
const targetApi = b === 'deepseek-messages' ? 'anthropic-messages' : b
|
||||
const targetProvider = b === 'deepseek-messages' ? 'deepseek-official' : 'route-b'
|
||||
const target = await mockServer([{ events: frames(targetApi) }])
|
||||
const ctx = new Context()
|
||||
contexts.push(ctx)
|
||||
await ctx.plugin(LlmRuntime)
|
||||
await ctx.plugin(LlmPiAi, { providers: {
|
||||
'route-a': { api: a, baseURL: source.url, apiKeyEnv: 'PROTOCOL_ROUNDTRIP_KEY', cacheRetention: 'none', models: [{ id: 'model' }] },
|
||||
...b === 'deepseek-messages' ? {} : {
|
||||
'route-b': { api: b, baseURL: target.url, apiKeyEnv: 'PROTOCOL_ROUNDTRIP_KEY', cacheRetention: 'none', models: [{ id: 'model' }] },
|
||||
},
|
||||
} })
|
||||
if (b === 'deepseek-messages') await ctx.plugin(DeepSeek, {
|
||||
baseURL: target.url, apiKeyEnv: 'PROTOCOL_ROUNDTRIP_KEY', models: [{ id: 'model' }], thinking: 'disabled',
|
||||
})
|
||||
let history: Message[] = [createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text: 'Look up both values.' }] })]
|
||||
const original = await assemble(ctx, { provider: 'route-a', model: 'model', messages: history, tools: [tool] })
|
||||
expect(original.finish).toEqual({ kind: 'tool-calls' })
|
||||
const calls = original.message.content.filter(block => block.type === 'tool-call')
|
||||
expect(calls).toHaveLength(2)
|
||||
if (a !== 'openai-completions') expect(original.message.content.at(-1)).toEqual({ type: 'text', text: '' })
|
||||
expect(original.message.source).toMatchObject({ replayState: { response: { api: a, provider: 'route-a', model: 'model' } } })
|
||||
if (a === 'anthropic-messages') {
|
||||
const blocks: unknown = expect.arrayContaining([
|
||||
{ type: 'reasoning', thinkingSignature: 'signed-reasoning' },
|
||||
{ type: 'reasoning', thinkingSignature: 'encrypted-redacted-reasoning', redacted: true },
|
||||
])
|
||||
expect(original.message.source).toMatchObject({ replayState: { blocks } })
|
||||
} else if (a === 'openai-responses') {
|
||||
const thinkingSignature: unknown = expect.stringContaining('encrypted-reasoning')
|
||||
const blocks: unknown = expect.arrayContaining([
|
||||
{ type: 'reasoning', thinkingSignature },
|
||||
{ type: 'text', textSignature: JSON.stringify({ v: 1, id: 'msg_empty' }) },
|
||||
])
|
||||
expect(original.message.source).toMatchObject({ replayState: { response: { responseId: 'resp_original' }, blocks } })
|
||||
}
|
||||
history.push(original.message, ...calls.map(call => createToolResultMessage({
|
||||
callId: call.id, content: [{ type: 'text', text: 'value' }], isError: false,
|
||||
})))
|
||||
const saved = JSON.stringify(history)
|
||||
history = JSON.parse(saved) as Message[]
|
||||
|
||||
// Capture A's request before any foreign conversion, then use the same saved history for B.
|
||||
expect((await assemble(ctx, { provider: 'route-a', model: 'model', messages: history, tools: [tool] })).finish).toEqual({ kind: 'stop' })
|
||||
const baseline = wireHistory(source.requests[1], a)
|
||||
expectToolPairs(source.requests[1], a)
|
||||
if (a === 'anthropic-messages') {
|
||||
expect(JSON.stringify(baseline)).toContain('signed-reasoning')
|
||||
expect(JSON.stringify(baseline)).toContain('encrypted-redacted-reasoning')
|
||||
} else if (a === 'openai-responses') {
|
||||
expect(JSON.stringify(baseline)).toContain('encrypted-reasoning')
|
||||
expect(JSON.stringify(baseline)).toContain('fc_0')
|
||||
}
|
||||
const foreign = await assemble(ctx, { provider: targetProvider, model: 'model', messages: history, tools: [tool] })
|
||||
expect(foreign.finish).toEqual({ kind: 'stop' })
|
||||
expect(JSON.stringify(history)).toBe(saved)
|
||||
expectToolPairs(target.requests[0], targetApi)
|
||||
expect(JSON.stringify(target.requests[0])).not.toContain('encrypted-reasoning')
|
||||
expect(JSON.stringify(target.requests[0])).not.toContain('encrypted-redacted-reasoning')
|
||||
expect(JSON.stringify(target.requests[0])).not.toContain('signed-reasoning')
|
||||
history.push(foreign.message, createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text: 'Continue with the original model.' }] }))
|
||||
history = JSON.parse(JSON.stringify(history)) as Message[]
|
||||
expect(history.slice(0, -2)).toEqual(JSON.parse(saved))
|
||||
expect((await assemble(ctx, { provider: 'route-a', model: 'model', messages: history, tools: [tool] })).finish).toEqual({ kind: 'stop' })
|
||||
expect(wireHistory(source.requests[2], a).slice(0, baseline.length)).toEqual(baseline)
|
||||
expect(history.slice(0, -2)).toEqual(JSON.parse(saved))
|
||||
expect(source.requests).toHaveLength(3)
|
||||
expect(target.requests).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
@@ -42,7 +42,7 @@ async function collect(events: AsyncIterable<AssistantMessageEvent>): Promise<{
|
||||
const context = normalizeContext({ messages: [{ role: 'user', content: 'hi', timestamp: 0 }] })
|
||||
|
||||
describe('streamed tool-call arguments (patched pi-ai)', () => {
|
||||
it('omits an empty Responses message after parallel tool calls before assembly', async () => {
|
||||
it('preserves an empty Responses message after parallel tool calls with its replay metadata', async () => {
|
||||
const calls = [0, 1, 2].map(index => ({ type: 'function_call', call_id: `call_${index}`, id: `item_${index}`, name: 'read', arguments: '{}' }))
|
||||
const empty = { type: 'message', id: 'message_empty', role: 'assistant', content: [{ type: 'output_text', text: '', annotations: [] }] }
|
||||
const server = await mockServer([{ events: [
|
||||
@@ -59,8 +59,14 @@ describe('streamed tool-call arguments (patched pi-ai)', () => {
|
||||
for await (const chunk of toStreamChunks(streamResponses(model('openai-responses', server.url), context, { apiKey: 'test-key' }))) {
|
||||
assembler.push(chunk)
|
||||
}
|
||||
expect(assembler.blocks()).toEqual(calls.map(call => ({ type: 'tool-call', id: `${call.call_id}|${call.id}`, name: 'read', arguments: '{}' })))
|
||||
expect(assembler.replayState?.blocks).toEqual(calls.map(() => ({ type: 'tool-call' })))
|
||||
expect(assembler.blocks()).toEqual([
|
||||
...calls.map(call => ({ type: 'tool-call', id: `${call.call_id}|${call.id}`, name: 'read', arguments: '{}' })),
|
||||
{ type: 'text', text: '' },
|
||||
])
|
||||
expect(assembler.replayState?.blocks).toEqual([
|
||||
...calls.map(() => ({ type: 'tool-call' })),
|
||||
{ type: 'text', textSignature: JSON.stringify({ v: 1, id: 'message_empty' }) },
|
||||
])
|
||||
expect(assembler.finish).toEqual({ kind: 'tool-calls' })
|
||||
expect(server.requests).toHaveLength(1)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user