|
| 1 | +import { type ModelClientService, type ModelEvent, type ModelRequest } from '@kilocode/harness-sdk'; |
| 2 | +import { Effect, Either, Stream } from 'effect'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { backendFailureKey } from './backend-target'; |
| 6 | +import { type NativeAvailability, nativeModelClient } from './native-model-client'; |
| 7 | +import { |
| 8 | + answerWith, |
| 9 | + AVAILABLE, |
| 10 | + collect, |
| 11 | + fakeBridge, |
| 12 | + request, |
| 13 | + type Script, |
| 14 | +} from './native-model-client.test-helpers'; |
| 15 | + |
| 16 | +vi.mock('@/i18n', () => ({ i18n: { t: (key: string) => key } })); |
| 17 | + |
| 18 | +describe('native on-device model client with tools', () => { |
| 19 | + const TOOLS: NativeAvailability = { ...AVAILABLE, tools: true }; |
| 20 | + const call = { id: 'fm-call-1', name: 'time', arguments: '{}' }; |
| 21 | + |
| 22 | + /** The request the harness sends after it ran the round's calls. */ |
| 23 | + const answered = (results: readonly { callId: string; body: string }[]): ModelRequest => ({ |
| 24 | + ...request, |
| 25 | + prompt: { |
| 26 | + system: request.prompt.system, |
| 27 | + messages: [ |
| 28 | + ...request.prompt.messages, |
| 29 | + { |
| 30 | + role: 'assistant', |
| 31 | + cache: false, |
| 32 | + parts: [{ kind: 'toolCall', callId: call.id, name: 'time', arguments: '{}' }], |
| 33 | + }, |
| 34 | + { |
| 35 | + role: 'user', |
| 36 | + cache: true, |
| 37 | + parts: results.map(result => ({ kind: 'toolResult', ...result, failed: false }) as const), |
| 38 | + }, |
| 39 | + ], |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + async function said(client: ModelClientService, asked = request): Promise<ModelEvent[]> { |
| 44 | + const result = await Effect.runPromise(Effect.either(Stream.runCollect(client.stream(asked)))); |
| 45 | + if (Either.isLeft(result)) { |
| 46 | + throw new Error('Expected the answer to finish'); |
| 47 | + } |
| 48 | + return [...result.right]; |
| 49 | + } |
| 50 | + |
| 51 | + function toolBridge(script: Script) { |
| 52 | + const fake = fakeBridge(script); |
| 53 | + fake.bridge.availability.mockResolvedValue(TOOLS); |
| 54 | + return fake; |
| 55 | + } |
| 56 | + |
| 57 | + it('sends paired tool calls and outputs, and the tools Foundation Models can take', async () => { |
| 58 | + const { bridge } = toolBridge(answerWith({})); |
| 59 | + const asked: ModelRequest = { |
| 60 | + ...request, |
| 61 | + tools: [ |
| 62 | + ...(request.tools ?? []), |
| 63 | + { |
| 64 | + name: 'lookup', |
| 65 | + description: 'Looks something up', |
| 66 | + parameters: { |
| 67 | + type: 'object', |
| 68 | + properties: { query: { type: 'string', description: 'What to find' } }, |
| 69 | + required: ['query'], |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: 'merge', |
| 74 | + description: 'Needs a union', |
| 75 | + parameters: { |
| 76 | + type: 'object', |
| 77 | + properties: { value: { anyOf: [{ type: 'string' }, { type: 'number' }] } }, |
| 78 | + required: ['value'], |
| 79 | + }, |
| 80 | + }, |
| 81 | + ], |
| 82 | + prompt: { |
| 83 | + ...request.prompt, |
| 84 | + messages: [ |
| 85 | + { |
| 86 | + role: 'assistant', |
| 87 | + cache: false, |
| 88 | + parts: [{ kind: 'toolCall', callId: 'never-answered', name: 'time', arguments: '{}' }], |
| 89 | + }, |
| 90 | + ...request.prompt.messages, |
| 91 | + ], |
| 92 | + }, |
| 93 | + }; |
| 94 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined); |
| 95 | + await said(nativeModelClient(bridge), asked); |
| 96 | + expect(warn).toHaveBeenCalledExactlyOnceWith( |
| 97 | + '[native-model] tool left out: merge.value: anyOf' |
| 98 | + ); |
| 99 | + warn.mockRestore(); |
| 100 | + expect(bridge.generate).toHaveBeenCalledExactlyOnceWith({ |
| 101 | + id: expect.any(String), |
| 102 | + system: 'Be brief.\n\nAnswer in English.', |
| 103 | + messages: [ |
| 104 | + { role: 'user', text: 'What is Kilo?' }, |
| 105 | + { role: 'assistant', text: 'A coding agent.' }, |
| 106 | + { role: 'toolCalls', calls: [{ id: 'call', name: 'time', arguments: '{}' }] }, |
| 107 | + { role: 'toolOutput', callId: 'call', name: 'time', text: 'noon' }, |
| 108 | + { role: 'user', text: 'Thanks' }, |
| 109 | + ], |
| 110 | + maxTokens: 1000, |
| 111 | + tools: [ |
| 112 | + { |
| 113 | + name: 'time', |
| 114 | + description: 'Current time', |
| 115 | + parameters: JSON.stringify({ type: 'object', properties: [] }), |
| 116 | + }, |
| 117 | + { |
| 118 | + name: 'lookup', |
| 119 | + description: 'Looks something up', |
| 120 | + parameters: JSON.stringify({ |
| 121 | + type: 'object', |
| 122 | + properties: [ |
| 123 | + { |
| 124 | + name: 'query', |
| 125 | + description: 'What to find', |
| 126 | + optional: false, |
| 127 | + schema: { type: 'string', description: 'What to find' }, |
| 128 | + }, |
| 129 | + ], |
| 130 | + }), |
| 131 | + }, |
| 132 | + ], |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + it('ends the round on the calls, then resumes the same generation with their results', async () => { |
| 137 | + const { bridge, emit } = toolBridge((native, send) => { |
| 138 | + send({ id: native.id, kind: 'toolCalls', calls: [call] }); |
| 139 | + }); |
| 140 | + const client = nativeModelClient(bridge); |
| 141 | + expect(await said(client)).toEqual([ |
| 142 | + { kind: 'toolCall', call }, |
| 143 | + { |
| 144 | + kind: 'done', |
| 145 | + stop: 'tools', |
| 146 | + usage: expect.objectContaining({ cacheReadTokens: 0, cacheWriteTokens: 0 }), |
| 147 | + }, |
| 148 | + ]); |
| 149 | + // The generation waits for the results; nothing stops it. |
| 150 | + expect(bridge.cancel).not.toHaveBeenCalled(); |
| 151 | + const id = bridge.generate.mock.calls[0]?.[0].id ?? ''; |
| 152 | + |
| 153 | + bridge.resume.mockImplementation(async resumed => { |
| 154 | + // The native module sends its events after it receives the results. |
| 155 | + await Promise.resolve(); |
| 156 | + emit({ id: resumed, kind: 'delta', text: 'It is noon.' }); |
| 157 | + emit({ |
| 158 | + id: resumed, |
| 159 | + kind: 'done', |
| 160 | + stop: 'end', |
| 161 | + usageSource: 'counted', |
| 162 | + inputTokens: 90, |
| 163 | + outputTokens: 4, |
| 164 | + }); |
| 165 | + }); |
| 166 | + const second = await said(client, answered([{ callId: call.id, body: '12:00' }])); |
| 167 | + expect(bridge.resume).toHaveBeenCalledExactlyOnceWith(id, [{ callId: call.id, body: '12:00' }]); |
| 168 | + expect(bridge.generate).toHaveBeenCalledOnce(); |
| 169 | + expect(second).toEqual([ |
| 170 | + { kind: 'delta', text: 'It is noon.' }, |
| 171 | + { |
| 172 | + kind: 'done', |
| 173 | + stop: 'end', |
| 174 | + usage: { inputTokens: 90, outputTokens: 4, cacheReadTokens: 0, cacheWriteTokens: 0 }, |
| 175 | + }, |
| 176 | + ]); |
| 177 | + }); |
| 178 | + |
| 179 | + it('stops a waiting generation that a new question leaves behind', async () => { |
| 180 | + let round = 0; |
| 181 | + const { bridge } = toolBridge((native, send) => { |
| 182 | + round += 1; |
| 183 | + if (round === 1) { |
| 184 | + send({ id: native.id, kind: 'toolCalls', calls: [call] }); |
| 185 | + } else { |
| 186 | + answerWith({})(native, send); |
| 187 | + } |
| 188 | + }); |
| 189 | + const client = nativeModelClient(bridge); |
| 190 | + await said(client); |
| 191 | + const waiting = bridge.generate.mock.calls[0]?.[0].id; |
| 192 | + const next = await said(client); |
| 193 | + expect(next.at(-1)).toMatchObject({ kind: 'done', stop: 'end' }); |
| 194 | + expect(bridge.cancel).toHaveBeenCalledExactlyOnceWith(waiting); |
| 195 | + expect(bridge.resume).not.toHaveBeenCalled(); |
| 196 | + expect(bridge.generate).toHaveBeenCalledTimes(2); |
| 197 | + }); |
| 198 | + |
| 199 | + it('fails explicitly on results that answer no waiting generation', async () => { |
| 200 | + const { bridge } = toolBridge((native, send) => { |
| 201 | + send({ id: native.id, kind: 'toolCalls', calls: [call] }); |
| 202 | + }); |
| 203 | + const client = nativeModelClient(bridge); |
| 204 | + await said(client); |
| 205 | + const waiting = bridge.generate.mock.calls[0]?.[0].id; |
| 206 | + const result = await collect(bridge, client.stream(answered([{ callId: 'other', body: 'x' }]))); |
| 207 | + expect(Either.isLeft(result) && backendFailureKey(result.left)).toBe( |
| 208 | + 'modelChat.localModels.failed' |
| 209 | + ); |
| 210 | + expect(bridge.cancel).toHaveBeenCalledExactlyOnceWith(waiting); |
| 211 | + expect(bridge.resume).not.toHaveBeenCalled(); |
| 212 | + expect(bridge.generate).toHaveBeenCalledOnce(); |
| 213 | + }); |
| 214 | + |
| 215 | + it('keeps a provider without the tool loop text-only', async () => { |
| 216 | + const { bridge } = toolBridge(answerWith({})); |
| 217 | + await said(nativeModelClient({ ...bridge, resume: undefined })); |
| 218 | + const sent = bridge.generate.mock.calls[0]?.[0]; |
| 219 | + expect(sent).not.toHaveProperty('tools'); |
| 220 | + expect(sent?.messages.map(message => message.role)).toEqual(['user', 'assistant', 'user']); |
| 221 | + }); |
| 222 | +}); |
0 commit comments