Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { z } from 'zod'
3import { defineAgent } from '@tanstack/ai'
4import { memoryPersistence } from '@tanstack/ai-persistence'
5import { createHarnessHost, defineHarness, definePlugin } from '../src'
6import { mockAdapter, text, toolCall } from './helpers'
7
8const agentNamed = (name: string, reply: string) =>
9 defineAgent({
10 name,
11 description: `The ${name} agent`,
12 inputSchema: z.object({ task: z.string() }),
13 run: async (ctx) => `${reply}: ${ctx.input.task}`,
14 })
15
16const toolNames = (call: any): Array<string> =>
17 (call.tools ?? []).map((tool: { name: string }) => tool.name)
18
19describe('plugin subagents', () => {
20 it('lets the model call agents that session and run plugins give, after the harness ones', async () => {
21 const lead = agentNamed('lead_helper', 'lead')
22 const fromSession = definePlugin({
23 name: 'test/session-agents',
24 setup: () => ({ subagents: [agentNamed('painter', 'painted')] }),
25 })
26 const fromRun = definePlugin({
27 name: 'test/run-agents',
28 lifetime: 'turn',
29 setup: () => ({ subagents: [agentNamed('writer', 'wrote')] }),
30 })
31 const { adapter, calls } = mockAdapter([
32 () => toolCall('painter', { task: 'a fox' }),
33 () => text('done'),
34 ])
35 const host = createHarnessHost({ persistence: memoryPersistence() })
36 const session = await host.open(
37 defineHarness({
38 name: 'test/plugin-subagents',
39 adapter,
40 subagents: { agents: [lead] },
41 plugins: () => [fromSession, fromRun],
42 }),
43 {
44 threadId: 't',
45 },
46 )
47
48 const turn = await session.send('paint a fox')
49 expect(turn.text).toBe('done')
50 expect(toolNames(calls[0])).toEqual(['lead_helper', 'painter', 'writer'])
51 expect(JSON.stringify(calls[1].messages)).toContain('painted: a fox')
52 // Plugin subagents are session agents too, so commands and clients can run them.
53 expect(await session.agent('painter')?.run({ task: 'a cat' })).toBe(
54 'painted: a cat',
55 )
56 await host.close()
57 })
58
59 it('gives no agent tools when neither the harness nor a plugin has any', async () => {
60 const { adapter, calls } = mockAdapter([() => text('hi')])
61 const host = createHarnessHost({ persistence: memoryPersistence() })
62 const session = await host.open(
63 defineHarness({
64 name: 'test/no-subagents',
65 adapter,
66 plugins: () => [
67 definePlugin({ name: 'test/empty', setup: () => ({}) }),
68 ],
69 }),
70 {
71 threadId: 't',
72 },
73 )
74 await session.send('hello')
75 expect(toolNames(calls[0])).toEqual([])
76 await host.close()
77 })
78})