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

plugin-apis.test.ts

103 lines3.2 KB

1import { describe, expect, it, vi } from 'vitest'
2import { memoryPersistence } from '@tanstack/ai-persistence'
3import { createHarnessHost, defineHarness, definePlugin } from '../src'
4import { mockAdapter, text, wakeMessages } from './helpers'
5import type { AnyTextAdapter } from '@tanstack/ai'
6import type { PluginSessionApi } from '../src'
7​
8/** Open a session with a plugin that hands out its session API. */
9async function openWithApi(adapter: AnyTextAdapter) {
10 const apis: Array<PluginSessionApi> = []
11 const probe = definePlugin({
12 name: 'test/probe',
13 setup: (ctx) => {
14 apis.push(ctx.session)
15 },
16 })
17 const host = createHarnessHost({ persistence: memoryPersistence() })
18 const session = await host.open(
19 defineHarness({
20 name: 'test/plugin-apis',
21 adapter,
22 plugins: () => [probe],
23 }),
24 {
25 threadId: 't',
26 },
27 )
28 const api = apis[0]
29 if (!api) throw new Error('The probe plugin was not set up.')
30 return { host, session, api }
32​
33describe('ctx.session.note', () => {
34 it('adds an assistant note to the transcript and starts no turn', async () => {
35 const { adapter, calls } = mockAdapter([() => text('Nice.')])
36 const { host, session, api } = await openWithApi(adapter)
37​
38 await api.note('Build finished.')
39 expect(await api.transcript()).toMatchObject([
40 { role: 'assistant', content: 'Build finished.' },
41 ])
42​
43 // The next model call is the user's prompt, after the note.
44 await session.send('Any news?')
45 expect(calls).toHaveLength(1)
46 expect(calls[0].messages).toMatchObject([
47 { role: 'assistant', content: 'Build finished.' },
48 { role: 'user', content: 'Any news?' },
49 ])
50 await host.close()
51 })
52​
53 it('starts a turn after the note with wake', async () => {
54 const { adapter, calls } = mockAdapter([() => text('On it.')])
55 const { host, session, api } = await openWithApi(adapter)
56​
57 await api.note('Build finished.', { wake: true })
58 await vi.waitFor(() => expect(calls).toHaveLength(1))
59 expect(calls[0].messages).toMatchObject([
60 { role: 'assistant', content: 'Build finished.' },
61 { role: 'user', content: 'Build finished.' },
62 ])
63 await vi.waitFor(() => expect(session.snapshot().status).toBe('idle'))
64 expect(wakeMessages(await session.transcript())).toEqual([
65 'Build finished.',
66 ])
67 await host.close()
68 })
69})
70​
71describe('prepareTools', () => {
72 it('gets the model id of the adapter the turn runs with', async () => {
73 const { adapter } = mockAdapter([])
74 const picked = mockAdapter([() => text('ok')])
75 const models: Array<string> = []
76 const plugin = definePlugin({
77 name: 'test/pick',
78 setup: () => ({
79 adapter: () => ({ ...picked.adapter, model: 'picked-model' }),
80 prepareTools: ({ tools, model }) => {
81 models.push(model)
82 return tools
83 },
84 }),
85 })
86 const host = createHarnessHost({ persistence: memoryPersistence() })
87 const session = await host.open(
88 defineHarness({
89 name: 'test/model',
90 adapter,
91 plugins: () => [plugin],
92 }),
93 {
94 threadId: 't',
95 },
96 )
97​
98 await session.send('go')
99 expect(models).toEqual(['picked-model'])
100 expect(picked.calls).toHaveLength(1)
101 await host.close()
102 })
103})