Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

capabilities.test.ts

129 lines3.4 KB

1import { describe, expect, it } from 'vitest'
2import { EventType, chat, defineChatMiddleware } from '@tanstack/ai'
3import {
4 InMemoryLockStore,
5 LocksCapability,
6 getLocks,
7 withLocks,
8} from '@tanstack/ai/locks'
9import type {
10 AnyTextAdapter,
11 ChatMiddlewareContext,
12 StreamChunk,
13} from '@tanstack/ai'
14import type { LockStore } from '@tanstack/ai/locks'
15import { memoryPersistence } from '../src/memory'
16import { withPersistence } from '../src/middleware'
17import {
18 InterruptsCapability,
19 PersistenceCapability,
20 getInterrupts,
21 getPersistence,
22} from '../src/capabilities'
23import type { AIPersistence, InterruptStore } from '../src'
24​
25function mockAdapter(chunks: Array<StreamChunk>) {
26 return {
27 kind: 'text',
28 name: 'mock',
29 model: 'test-model',
30 '~types': {},
31 chatStream: () =>
32 (async function* () {
33 for (const c of chunks) yield c
34 })(),
35 structuredOutput: async () => ({ data: {}, rawText: '{}' }),
36 } as unknown as AnyTextAdapter
38​
39async function collect(stream: AsyncIterable<StreamChunk>) {
40 const out: Array<StreamChunk> = []
41 for await (const c of stream) out.push(c)
42 return out
44​
45describe('persistence capabilities', () => {
46 it('provides persistence and interrupts from withPersistence', async () => {
47 const persistence = memoryPersistence()
48 const seen: {
49 persistence?: AIPersistence
50 interrupts?: InterruptStore
51 } = {}
52​
53 const consumer = defineChatMiddleware({
54 name: 'capability-consumer',
55 requires: [PersistenceCapability, InterruptsCapability],
56 setup(ctx: ChatMiddlewareContext) {
57 seen.persistence = getPersistence(ctx)
58 seen.interrupts = getInterrupts(ctx)
59 },
60 })
61​
62 await collect(
63 chat({
64 adapter: mockAdapter([
65 {
66 type: EventType.RUN_STARTED,
67 runId: 'r1',
68 threadId: 't1',
69 timestamp: 1,
70 },
71 {
72 type: EventType.RUN_FINISHED,
73 runId: 'r1',
74 threadId: 't1',
75 finishReason: 'stop',
76 timestamp: 1,
77 },
78 ]),
79 messages: [{ role: 'user', content: 'hi' }],
80 runId: 'r1',
81 threadId: 't1',
82 middleware: [withPersistence(persistence), consumer],
83 }) as AsyncIterable<StreamChunk>,
84 )
85​
86 expect(seen.persistence).toBe(persistence)
87 expect(seen.interrupts).toBe(persistence.stores.interrupts)
88 })
89​
90 it('provides locks from withLocks (separate from state persistence)', async () => {
91 const persistence = memoryPersistence()
92 const locks = new InMemoryLockStore()
93 const seen: { locks?: LockStore } = {}
94​
95 const consumer = defineChatMiddleware({
96 name: 'lock-consumer',
97 requires: [LocksCapability],
98 setup(ctx: ChatMiddlewareContext) {
99 seen.locks = getLocks(ctx)
100 },
101 })
102​
103 await collect(
104 chat({
105 adapter: mockAdapter([
106 {
107 type: EventType.RUN_STARTED,
108 runId: 'r1',
109 threadId: 't1',
110 timestamp: 1,
111 },
112 {
113 type: EventType.RUN_FINISHED,
114 runId: 'r1',
115 threadId: 't1',
116 finishReason: 'stop',
117 timestamp: 1,
118 },
119 ]),
120 messages: [{ role: 'user', content: 'hi' }],
121 runId: 'r1',
122 threadId: 't1',
123 middleware: [withPersistence(persistence), withLocks(locks), consumer],
124 }) as AsyncIterable<StreamChunk>,
125 )
126​
127 expect(seen.locks).toBe(locks)
128 })
129})