|
13 | 13 | */ |
14 | 14 | import { describe, it, expect } from 'vitest'; |
15 | 15 | import { aiMessageRowsToServerMessages, toUIMessages } from './useChatConversation'; |
| 16 | +import type { HydratedUIMessage } from './useChatConversation'; |
16 | 17 |
|
17 | 18 | describe('toUIMessages — merging tool-results onto the call (refresh survival)', () => { |
18 | 19 | const draftEnvelope = { |
@@ -338,3 +339,133 @@ describe('toUIMessages — the turn the server stamped (objectstack#31865)', () |
338 | 339 | expect('turnId' in out[3]).toBe(false); |
339 | 340 | }); |
340 | 341 | }); |
| 342 | + |
| 343 | +/** |
| 344 | + * objectstack#39283 — the store writes one assistant row per step of an |
| 345 | + * answer, each stamped with the turn's `turnId`, plus a `tool` row with each |
| 346 | + * step's results. The rows below are that shape, as the conversation routes |
| 347 | + * return it: a step row's `content` is the store's `(called …)` placeholder |
| 348 | + * text and its tool calls; the answer row is text only. |
| 349 | + */ |
| 350 | +interface StoredRow { |
| 351 | + id: string; |
| 352 | + role: string; |
| 353 | + content: unknown; |
| 354 | + turnId?: string; |
| 355 | + turnEnd?: unknown; |
| 356 | +} |
| 357 | + |
| 358 | +function storedTurn(turnId: string | undefined, rounds: string[][], answer: string | undefined, tag = 's') { |
| 359 | + const stamp = turnId === undefined ? {} : { turnId }; |
| 360 | + const rows: StoredRow[] = []; |
| 361 | + rounds.forEach((tools, r) => { |
| 362 | + const calls = tools.map((toolName, c) => ({ |
| 363 | + type: 'tool-call', |
| 364 | + toolCallId: `${tag}${r}-${c}`, |
| 365 | + toolName, |
| 366 | + input: { round: r }, |
| 367 | + })); |
| 368 | + rows.push({ |
| 369 | + id: `${tag}-a${r}`, |
| 370 | + role: 'assistant', |
| 371 | + content: [{ type: 'text', text: `(called ${tools.join(', ')})` }, ...calls], |
| 372 | + ...stamp, |
| 373 | + }); |
| 374 | + rows.push({ |
| 375 | + id: `${tag}-t${r}`, |
| 376 | + role: 'tool', |
| 377 | + content: calls.map((call) => ({ |
| 378 | + type: 'tool-result', |
| 379 | + toolCallId: call.toolCallId, |
| 380 | + toolName: call.toolName, |
| 381 | + output: { type: 'json', value: { round: r, id: call.toolCallId } }, |
| 382 | + })), |
| 383 | + ...stamp, |
| 384 | + }); |
| 385 | + }); |
| 386 | + if (answer !== undefined) rows.push({ id: `${tag}-answer`, role: 'assistant', content: answer, ...stamp }); |
| 387 | + return rows; |
| 388 | +} |
| 389 | + |
| 390 | +/** The card's turn: one `query_records` round, one round of five parallel `aggregate_data`, then thirteen single ones. */ |
| 391 | +const FIFTEEN_ROUNDS = [ |
| 392 | + ['query_records'], |
| 393 | + Array.from({ length: 5 }, () => 'aggregate_data'), |
| 394 | + ...Array.from({ length: 13 }, () => ['aggregate_data']), |
| 395 | +]; |
| 396 | + |
| 397 | +const toolParts = (message: HydratedUIMessage) => |
| 398 | + message.parts.filter((part) => part.type.startsWith('tool-')); |
| 399 | +const textParts = (message: HydratedUIMessage) => |
| 400 | + message.parts.filter((part) => part.type === 'text').map((part) => part.text); |
| 401 | + |
| 402 | +describe('toUIMessages — one stored turn is one answer (objectstack#39283)', () => { |
| 403 | + it('folds 15 step rows of one turn and their answer into ONE assistant message whose tools fold to 2 steps', () => { |
| 404 | + const out = toUIMessages([ |
| 405 | + { id: 'u1', role: 'user', content: 'How are the deals spread?', turnId: 'turn-1' }, |
| 406 | + ...storedTurn('turn-1', FIFTEEN_ROUNDS, 'Deals are spread across 4 stages.'), |
| 407 | + ]); |
| 408 | + expect(out.map((m) => [m.role, m.id, m.turnId])).toEqual([ |
| 409 | + ['user', 'u1', 'turn-1'], |
| 410 | + // The first stored row's id: the answer starts where its turn's rows start. |
| 411 | + ['assistant', 's-a0', 'turn-1'], |
| 412 | + ]); |
| 413 | + const answer = out[1]; |
| 414 | + const tools = toolParts(answer); |
| 415 | + // Every call of the turn, in call order, each with the result its `tool` row carried. |
| 416 | + expect(tools).toHaveLength(19); |
| 417 | + expect(tools.map((part) => part.toolCallId)).toEqual( |
| 418 | + FIFTEEN_ROUNDS.flatMap((round, r) => round.map((_, c) => `s${r}-${c}`)), |
| 419 | + ); |
| 420 | + for (const part of tools) { |
| 421 | + expect(part.state).toBe('output-available'); |
| 422 | + expect(part.output).toEqual({ type: 'json', value: expect.objectContaining({ id: part.toolCallId }) }); |
| 423 | + } |
| 424 | + // The step line folds by tool name: 2 steps, the repeated one ×18. |
| 425 | + const byName = new Map<string, number>(); |
| 426 | + for (const part of tools) byName.set(String(part.toolName), (byName.get(String(part.toolName)) ?? 0) + 1); |
| 427 | + expect([...byName]).toEqual([ |
| 428 | + ['query_records', 1], |
| 429 | + ['aggregate_data', 18], |
| 430 | + ]); |
| 431 | + // The body is the answer alone, after the tools: no store placeholder rides along. |
| 432 | + expect(textParts(answer)).toEqual(['Deals are spread across 4 stages.']); |
| 433 | + expect(answer.parts[answer.parts.length - 1]).toEqual({ type: 'text', text: 'Deals are spread across 4 stages.' }); |
| 434 | + }); |
| 435 | + |
| 436 | + it('keeps a resumed turn\'s continuation a second message: the answer row ends the first', () => { |
| 437 | + const turnEnd = { reason: 'answered', wrapUp: false, resumable: false, resumesLeft: 1, turnId: 'turn-1' }; |
| 438 | + const first = storedTurn('turn-1', [['query_records'], ['aggregate_data']], 'Partial: two of four stages read.', 'p'); |
| 439 | + const rest = storedTurn('turn-1', [['aggregate_data'], ['aggregate_data']], 'All four stages read.', 'r'); |
| 440 | + rest[rest.length - 1].turnEnd = turnEnd; |
| 441 | + const out = toUIMessages([{ id: 'u1', role: 'user', content: 'q', turnId: 'turn-1' }, ...first, ...rest]); |
| 442 | + expect(out.map((m) => m.id)).toEqual(['u1', 'p-a0', 'r-a0']); |
| 443 | + expect(textParts(out[1])).toEqual(['Partial: two of four stages read.']); |
| 444 | + expect(toolParts(out[2]).map((part) => part.toolCallId)).toEqual(['r0-0', 'r1-0']); |
| 445 | + expect(out[2].parts.at(-1)).toMatchObject({ type: 'data-turn-end', data: turnEnd }); |
| 446 | + }); |
| 447 | + |
| 448 | + it('folds nothing across turns, and nothing for rows stored with no turnId', () => { |
| 449 | + const out = toUIMessages([ |
| 450 | + ...storedTurn('turn-1', [['query_records']], undefined, 'x'), |
| 451 | + ...storedTurn('turn-2', [['aggregate_data']], 'Two.', 'y'), |
| 452 | + ...storedTurn(undefined, [['query_records'], ['aggregate_data']], 'Three.', 'z'), |
| 453 | + ]); |
| 454 | + expect(out.map((m) => m.id)).toEqual(['x-a0', 'y-a0', 'z-a0', 'z-a1', 'z-answer']); |
| 455 | + // A row that folded into nothing is byte-for-byte what it was: its placeholder stays. |
| 456 | + expect(textParts(out[2])).toEqual(['(called query_records)']); |
| 457 | + }); |
| 458 | + |
| 459 | + it('a turn that stopped before its answer keeps ONE placeholder, and a step\'s own words stay in order', () => { |
| 460 | + const stopped = toUIMessages(storedTurn('turn-1', [['query_records'], ['aggregate_data', 'aggregate_data']], undefined)); |
| 461 | + expect(stopped).toHaveLength(1); |
| 462 | + expect(textParts(stopped[0])).toEqual(['(called query_records)']); |
| 463 | + expect(toolParts(stopped[0])).toHaveLength(3); |
| 464 | + |
| 465 | + const rows = storedTurn('turn-1', [['query_records'], ['aggregate_data']], 'Done.'); |
| 466 | + rows[2].content = [{ type: 'text', text: 'Now the totals. ' }, ...(rows[2].content as unknown[]).slice(1)]; |
| 467 | + const narrated = toUIMessages(rows); |
| 468 | + expect(narrated).toHaveLength(1); |
| 469 | + expect(textParts(narrated[0])).toEqual(['Now the totals. ', 'Done.']); |
| 470 | + }); |
| 471 | +}); |
0 commit comments