Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 createServerToolContext,
4 ToolInputRequiredError,
5} from '../../src/server/context'
6import type { SampleRequest, ToolInputRequest } from '../../src/server/context'
7
8const cityRequest: ToolInputRequest = { message: 'Which city?' }
9const summaryRequest: SampleRequest = {
10 messages: [{ role: 'user', content: 'Draft a summary' }],
11}
12
13function textCallback(text: string) {
14 const requests: Array<SampleRequest> = []
15 return {
16 requests,
17 fn: async (request: SampleRequest) => {
18 requests.push(request)
19 return text
20 },
21 }
22}
23
24async function rejectionOf(promise: Promise<unknown>) {
25 try {
26 await promise
27 } catch (error) {
28 return error
29 }
30 throw new Error('The call resolved')
31}
32
33describe('createServerToolContext', () => {
34 describe('requestInput', () => {
35 it('waits on the supplied function and returns that answer in the same call', async () => {
36 let runs = 0
37 let resolveInput: ((answer: string) => void) | undefined
38 const seen: Array<ToolInputRequest> = []
39 const ctx = createServerToolContext({
40 era: '2025',
41 waitForInput: (request) => {
42 seen.push(request)
43 return new Promise((resolve) => {
44 resolveInput = resolve
45 })
46 },
47 clientSample: async () => 'from-client',
48 })
49
50 const pending = (async () => {
51 runs += 1
52 return ctx.requestInput(cityRequest)
53 })()
54
55 expect(runs).toBe(1)
56 expect(seen).toEqual([cityRequest])
57 if (resolveInput === undefined) {
58 throw new Error('requestInput did not wait on the supplied function')
59 }
60 resolveInput('Paris')
61
62 await expect(pending).resolves.toBe('Paris')
63 expect(runs).toBe(1)
64 })
65
66 it('ends the first 2026 call as input required and returns the answer on retry', async () => {
67 let runs = 0
68 let finished = 0
69 const tool = async (ctx: {
70 requestInput: (request: ToolInputRequest) => Promise<unknown>
71 }) => {
72 runs += 1
73 const answer = await ctx.requestInput(cityRequest)
74 finished += 1
75 return answer
76 }
77
78 const first = createServerToolContext({ era: '2026' })
79 const error = await rejectionOf(tool(first))
80
81 expect(error).toBeInstanceOf(ToolInputRequiredError)
82 if (!(error instanceof ToolInputRequiredError)) {
83 throw new Error('expected ToolInputRequiredError')
84 }
85 expect(error.resultType).toBe('input_required')
86 expect(error.request).toBe(cityRequest)
87 expect(runs).toBe(1)
88 expect(finished).toBe(0)
89
90 const second = createServerToolContext({
91 era: '2026',
92 inputAnswer: 'Paris',
93 })
94
95 await expect(tool(second)).resolves.toBe('Paris')
96 expect(runs).toBe(2)
97 expect(finished).toBe(1)
98 })
99
100 it('throws on a second 2026 question in one call instead of reusing the answer', async () => {
101 const ctx = createServerToolContext({ era: '2026', inputAnswer: 'Paris' })
102
103 await expect(ctx.requestInput(cityRequest)).resolves.toBe('Paris')
104 await expect(ctx.requestInput({ message: 'Which day?' })).rejects.toThrow(
105 'only one question per tool call',
106 )
107 })
108 })
109
110 describe('sample', () => {
111 it('calls the client sample function and does not call the local adapter', async () => {
112 const client = textCallback('from-client')
113 const adapter = textCallback('from-adapter')
114 let inputWaits = 0
115 const ctx = createServerToolContext({
116 era: '2025',
117 waitForInput: async () => {
118 inputWaits += 1
119 return 'Paris'
120 },
121 clientSample: client.fn,
122 sample: adapter.fn,
123 })
124
125 await expect(ctx.sample(summaryRequest)).resolves.toBe('from-client')
126 expect(client.requests).toEqual([summaryRequest])
127 expect(adapter.requests).toEqual([])
128 expect(inputWaits).toBe(0)
129 })
130
131 it('calls the sample adapter and does not ask the client', async () => {
132 const client = textCallback('from-client')
133 const adapter = textCallback('from-adapter')
134 // Era 2026 has no client sampling. The adapter is the only source.
135 const ctx = createServerToolContext({
136 era: '2026',
137 sample: adapter.fn,
138 })
139
140 await expect(ctx.sample(summaryRequest)).resolves.toBe('from-adapter')
141 expect(adapter.requests).toEqual([summaryRequest])
142 expect(client.requests).toEqual([])
143 })
144
145 it('throws when the 2026 sample adapter is missing', async () => {
146 const ctx = createServerToolContext({
147 era: '2026',
148 })
149
150 const error = await rejectionOf(ctx.sample(summaryRequest))
151
152 expect(error).toBeInstanceOf(Error)
153 if (!(error instanceof Error)) {
154 throw new Error('expected an Error')
155 }
156 expect(error.message).toContain('sample')
157 })
158 })
159})