Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { loadChildTranscript, memoryPersistence } from '../src'
3import type { ModelMessage } from '@tanstack/ai'
4
5/** A stored child message whose head marker names `parentThreadId`. */
6function child(parentThreadId: string, content: string): ModelMessage {
7 return {
8 role: 'assistant',
9 content,
10 metadata: { 'tanstack:subagentChild': { parentThreadId, prompt: 'Go' } },
11 }
12}
13
14describe('loadChildTranscript', () => {
15 it('loads a child and a nested child of the thread, and no other', async () => {
16 const { messages } = memoryPersistence().stores
17 await messages.saveThread('subagent:a', [child('t', 'From a')])
18 await messages.saveThread('subagent:b', [child('subagent:a', 'From b')])
19 await messages.saveThread('subagent:c', [child('other', 'From c')])
20
21 const nested = await loadChildTranscript(messages, 'b', 't')
22 expect(nested?.map((message) => message.content)).toEqual(['Go', 'From b'])
23 expect(await loadChildTranscript(messages, 'a', 't')).toHaveLength(2)
24 expect(await loadChildTranscript(messages, 'c', 't')).toBeUndefined()
25 expect(await loadChildTranscript(messages, 'none', 't')).toBeUndefined()
26 })
27
28 it('keeps the card of a child with no messages', async () => {
29 const { messages } = memoryPersistence().stores
30 const card = { name: 'writer', status: 'error', placeholder: true }
31 await messages.saveThread('subagent:p', [
32 {
33 role: 'assistant',
34 content: '',
35 metadata: {
36 tanstack: { subagent: card },
37 'tanstack:subagentChild': { parentThreadId: 't' },
38 },
39 },
40 ])
41
42 const loaded = await loadChildTranscript(messages, 'p', 't')
43
44 expect(loaded?.[0]?.metadata?.tanstack).toEqual({ subagent: card })
45 })
46
47 it('stops at a loop in the stored parents', async () => {
48 const { messages } = memoryPersistence().stores
49 await messages.saveThread('subagent:x', [child('subagent:y', 'x')])
50 await messages.saveThread('subagent:y', [child('subagent:x', 'y')])
51
52 expect(await loadChildTranscript(messages, 'x', 't')).toBeUndefined()
53 })
54})