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

plugins.test.ts

186 lines5.4 KB

1import { describe, expect, it } from 'vitest'
2import { createCapability, defineAgent } from '@tanstack/ai'
3import { AgentRegistry } from '../src/agents'
4import { definePlugin, mountPlugins } from '../src/plugins'
5import { ResourceScope } from '../src/resources'
6​
7const env = () => ({
8 threadId: 't1',
9 registry: new AgentRegistry(),
10 harnessTools: [],
11 harnessProvides: [],
12})
13​
14describe('ResourceScope', () => {
15 it('closes resources newest first and reports every close error', async () => {
16 const scope = new ResourceScope()
17 const log: Array<string> = []
18 await scope.acquire(
19 () => 'a',
20 () => log.push('close a'),
21 )
22 await scope.acquire(
23 () => 'b',
24 () => {
25 log.push('close b')
26 throw new Error('b broke')
27 },
28 )
29 await scope.acquire(
30 () => 'c',
31 () => {
32 log.push('close c')
33 throw new Error('c broke')
34 },
35 )
36 await expect(scope.dispose()).rejects.toBeInstanceOf(AggregateError)
37 expect(log).toEqual(['close c', 'close b', 'close a'])
38 expect(scope.signal.aborted).toBe(true)
39 })
40​
41 it('disposes while an open still runs, and closes that resource when it arrives', async () => {
42 const scope = new ResourceScope()
43 const log: Array<string> = []
44 let finishOpen!: () => void
45 const opening = scope.acquire(
46 () =>
47 new Promise<string>((resolve) => {
48 finishOpen = () => resolve('late')
49 }),
50 () => log.push('closed late'),
51 )
52 // Resolves before the open ends.
53 await scope.dispose()
54 expect(log).toEqual([])
55 finishOpen()
56 await expect(opening).rejects.toThrow('Resource scope disposed')
57 expect(log).toEqual(['closed late'])
58 })
59​
60 it('closes a resource that finishes opening after dispose', async () => {
61 const scope = new ResourceScope()
62 const log: Array<string> = []
63 let finishOpen!: () => void
64 const opening = scope.acquire(
65 () =>
66 new Promise<string>((resolve) => {
67 finishOpen = () => resolve('late')
68 }),
69 () => log.push('closed late'),
70 )
71 const disposed = scope.dispose()
72 finishOpen()
73 await expect(opening).rejects.toThrow('Resource scope disposed')
74 await disposed
75 expect(log).toEqual(['closed late'])
76 await expect(
77 scope.acquire(
78 () => 'x',
79 () => {},
80 ),
81 ).rejects.toThrow()
82 })
83})
84​
85describe('mountPlugins', () => {
86 const store = createCapability<{ items: Array<string> }>()('test.store')
87​
88 it('fails before any setup when a required capability has no provider', async () => {
89 let ran = false
90 const reader = definePlugin({
91 name: 'test/reader',
92 requires: [store],
93 setup: () => {
94 ran = true
95 },
96 })
97 await expect(mountPlugins([reader], env())).rejects.toThrow(
98 'requires capability "test.store"',
99 )
100 expect(ran).toBe(false)
101 })
102​
103 it('lets a later plugin read what an earlier plugin provides', async () => {
104 const provider = definePlugin({
105 name: 'test/provider',
106 provides: [store],
107 setup: ({ provide }) => provide(store, { items: ['x'] }),
108 })
109 let read: Array<string> = []
110 const reader = definePlugin({
111 name: 'test/reader',
112 requires: [store],
113 setup: ({ get }) => {
114 read = get(store).items
115 },
116 })
117 const mounted = await mountPlugins([provider, reader], env())
118 expect(read).toEqual(['x'])
119 expect(mounted.capabilityBridge?.provides).toEqual([store])
120 })
121​
122 it('rejects a provide that the plugin did not declare', async () => {
123 const sneaky = definePlugin({
124 name: 'test/sneaky',
125 setup: ({ provide }) => provide(store, { items: [] }),
126 })
127 await expect(mountPlugins([sneaky], env())).rejects.toThrow(
128 'without declaring it in provides',
129 )
130 })
131​
132 it('disposes earlier plugins newest first when a setup throws', async () => {
133 const log: Array<string> = []
134 const opener = (name: string) =>
135 definePlugin({
136 name,
137 setup: async ({ resources }) => {
138 await resources.acquire(
139 () => name,
140 () => log.push(`close ${name}`),
141 )
142 },
143 })
144 const broken = definePlugin({
145 name: 'test/broken',
146 setup: () => {
147 throw new Error('setup failed')
148 },
149 })
150 await expect(
151 mountPlugins([opener('test/one'), opener('test/two'), broken], env()),
152 ).rejects.toThrow('setup failed')
153 expect(log).toEqual(['close test/two', 'close test/one'])
154 })
155​
156 it('checks needs.produces against the agents after setup', async () => {
157 const needsImages = definePlugin({
158 name: 'test/gallery',
159 needs: { produces: ['image'] },
160 })
161 await expect(mountPlugins([needsImages], env())).rejects.toThrow(
162 'needs an agent that produces "image"',
163 )
164​
165 const painter = defineAgent({
166 name: 'painter',
167 description: 'Paints',
168 produces: 'image',
169 run: async () => 'painted',
170 })
171 const withPainter = definePlugin({
172 name: 'test/painter',
173 setup: () => ({ agents: [painter] }),
174 })
175 const environment = env()
176 await mountPlugins([withPainter, needsImages], environment)
177 expect(environment.registry.find({ produces: 'image' })).toBe(painter)
178 })
179​
180 it('rejects duplicate plugin names', async () => {
181 const a = definePlugin({ name: 'test/same' })
182 await expect(mountPlugins([a, a], env())).rejects.toThrow(
183 'Duplicate plugin: test/same',
184 )
185 })
186})