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

session-surface.test.ts

148 lines4.7 KB

1import { describe, expect, it } from 'vitest'
2import { chat, defineAgent } from '@tanstack/ai'
3import { memoryPersistence } from '@tanstack/ai-persistence'
4import { createHarnessHost, defineHarness } from '../src'
5import { after, gate, mockAdapter } from './helpers'
6import type { Reply } from './helpers'
7​
8/** Open a session whose model calls answer with `replies`. */
9async function open(replies: Array<Reply> = []) {
10 const { adapter } = mockAdapter(replies)
11 const host = createHarnessHost({ persistence: memoryPersistence() })
12 const session = await host.open(
13 defineHarness({ name: 'test/session-surface', adapter }),
14 {
15 threadId: 't1',
16 },
17 )
18 return { host, session }
20​
21/** A session with one turn that runs until `release` opens. */
22async function openBusy() {
23 const release = gate()
24 const { host, session } = await open([after(release.opened, 'Done.')])
25 const turn = session.send('Hello.')
26 await turn.receipt
27 return { host, session, turn, release }
29​
30describe('session.whenIdle', () => {
31 it('resolves on an idle session', async () => {
32 const { host, session } = await open()
33​
34 await expect(session.whenIdle()).resolves.toBeUndefined()
35 await host.close()
36 })
37​
38 it('resolves after the running turn settles', async () => {
39 const { host, session, turn, release } = await openBusy()
40​
41 const statusWhenIdle = session.whenIdle().then(() => turn.status())
42 release.open()
43​
44 expect(await statusWhenIdle).toBe('completed')
45 await host.close()
46 })
47​
48 it('rejects at once with the reason of a signal that aborted before the call', async () => {
49 const { host, session, turn, release } = await openBusy()
50 const reason = new Error('Stop waiting.')
51​
52 // The turn waits for `release`, so a wait for idle would never end.
53 await expect(
54 session.whenIdle({ signal: AbortSignal.abort(reason) }),
55 ).rejects.toBe(reason)
56 release.open()
57 await turn
58 await host.close()
59 })
60​
61 it('rejects with the reason when the signal aborts during the wait', async () => {
62 const { host, session, turn, release } = await openBusy()
63 const controller = new AbortController()
64 const reason = new Error('Stop waiting.')
65​
66 const waiting = session.whenIdle({ signal: controller.signal })
67 controller.abort(reason)
68​
69 await expect(waiting).rejects.toBe(reason)
70 release.open()
71 await turn
72 await host.close()
73 })
74})
75​
76describe('the HarnessSession type', () => {
77 it('does not have the members that only the host and the handler call', async () => {
78 const { host, session } = await open()
79​
80 // @ts-expect-error `host.open` opens the session.
81 void session.open
82 // @ts-expect-error `host.fork` adopts the fork.
83 void session.adoptFork
84 // @ts-expect-error `session.whenIdle` is the public wait.
85 void session.onIdle
86 // @ts-expect-error the handler resolves a `POST run` as its run id.
87 void session.resolveForRun
88 const answers: Parameters<typeof session.resolve>[0] = {
89 resume: [],
90 // @ts-expect-error `resolve` takes no run id.
91 runId: 'run-1',
92 }
93 void answers
94 // @ts-expect-error the handler sends a `POST run` as its run id.
95 void session.sendForRun
96 // @ts-expect-error a sweep takes over the log of the session.
97 void session.takeOver
98 // @ts-expect-error the handler scrubs an error frame.
99 void session.scrubError
100 // @ts-expect-error the access check of a schedule reads the log.
101 void session.loggedRun
102 const sent: Parameters<typeof session.send>[1] = {
103 // @ts-expect-error `send` takes no run id.
104 runId: 'run-1',
105 }
106 void sent
107 const config: Parameters<typeof session.setPluginSetting>[0] = {
108 key: 'model',
109 value: 'fast',
110 // @ts-expect-error only an offered input has its own control id.
111 inputId: 'in-1',
112 }
113 void config
114 const answer: Parameters<typeof session.answer>[0] = {
115 questionId: 'q-1',
116 value: 'yes',
117 // @ts-expect-error only an offered answer has a secret ref.
118 ref: 'harness:answer:q-1:in-1',
119 }
120 void answer
121 await host.close()
122 })
123})
124​
125describe('ctx.harness in a run that a plain chat() starts', () => {
126 it('is undefined, since no harness runs the agent', async () => {
127 const seen: Array<unknown> = []
128 const probe = defineAgent({
129 name: 'probe',
130 description: 'Reads ctx.harness with no harness session',
131 run: async (ctx) => {
132 seen.push(ctx.harness)
133 return 'done'
134 },
135 })
136​
137 for await (const _chunk of chat({
138 adapter: mockAdapter([]).adapter,
139 threadId: 'thread-1',
140 messages: [{ role: 'user', content: 'Go' }],
141 subagents: { agents: [probe], router: () => 'probe' },
142 })) {
143 // drain
144 }
145​
146 expect(seen).toEqual([undefined])
147 })
148})