This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathvscode-lm-format.spec.ts
More file actions
348 lines (295 loc) · 9.93 KB
/
Copy pathvscode-lm-format.spec.ts
File metadata and controls
348 lines (295 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// npx vitest run src/api/transform/__tests__/vscode-lm-format.spec.ts
import { Anthropic } from "@anthropic-ai/sdk"
import * as vscode from "vscode"
import { convertToVsCodeLmMessages, convertToAnthropicRole, extractTextCountFromMessage } from "../vscode-lm-format"
// Mock crypto using Vitest
vitest.stubGlobal("crypto", {
randomUUID: () => "test-uuid",
})
// Define types for our mocked classes
interface MockLanguageModelTextPart {
type: "text"
value: string
}
interface MockLanguageModelToolCallPart {
type: "tool_call"
callId: string
name: string
input: any
}
interface MockLanguageModelToolResultPart {
type: "tool_result"
callId: string
content: MockLanguageModelTextPart[]
}
// Mock vscode namespace
vitest.mock("vscode", () => {
const LanguageModelChatMessageRole = {
Assistant: "assistant",
User: "user",
}
class MockLanguageModelTextPart {
type = "text"
constructor(public value: string) {}
}
class MockLanguageModelToolCallPart {
type = "tool_call"
constructor(
public callId: string,
public name: string,
public input: any,
) {}
}
class MockLanguageModelToolResultPart {
type = "tool_result"
constructor(
public callId: string,
public content: MockLanguageModelTextPart[],
) {}
}
return {
LanguageModelChatMessage: {
Assistant: vitest.fn((content) => ({
role: LanguageModelChatMessageRole.Assistant,
name: "assistant",
content: Array.isArray(content) ? content : [new MockLanguageModelTextPart(content)],
})),
User: vitest.fn((content) => ({
role: LanguageModelChatMessageRole.User,
name: "user",
content: Array.isArray(content) ? content : [new MockLanguageModelTextPart(content)],
})),
},
LanguageModelChatMessageRole,
LanguageModelTextPart: MockLanguageModelTextPart,
LanguageModelToolCallPart: MockLanguageModelToolCallPart,
LanguageModelToolResultPart: MockLanguageModelToolResultPart,
}
})
describe("convertToVsCodeLmMessages", () => {
it("should convert simple string messages", () => {
const messages: Anthropic.Messages.MessageParam[] = [
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi there" },
]
const result = convertToVsCodeLmMessages(messages)
expect(result).toHaveLength(2)
expect(result[0].role).toBe("user")
expect((result[0].content[0] as MockLanguageModelTextPart).value).toBe("Hello")
expect(result[1].role).toBe("assistant")
expect((result[1].content[0] as MockLanguageModelTextPart).value).toBe("Hi there")
})
it("should handle complex user messages with tool results", () => {
const messages: Anthropic.Messages.MessageParam[] = [
{
role: "user",
content: [
{ type: "text", text: "Here is the result:" },
{
type: "tool_result",
tool_use_id: "tool-1",
content: "Tool output",
},
],
},
]
const result = convertToVsCodeLmMessages(messages)
expect(result).toHaveLength(1)
expect(result[0].role).toBe("user")
expect(result[0].content).toHaveLength(2)
const [toolResult, textContent] = result[0].content as [
MockLanguageModelToolResultPart,
MockLanguageModelTextPart,
]
expect(toolResult.type).toBe("tool_result")
expect(textContent.type).toBe("text")
})
it("should handle complex assistant messages with tool calls", () => {
const messages: Anthropic.Messages.MessageParam[] = [
{
role: "assistant",
content: [
{ type: "text", text: "Let me help you with that." },
{
type: "tool_use",
id: "tool-1",
name: "calculator",
input: { operation: "add", numbers: [2, 2] },
},
],
},
]
const result = convertToVsCodeLmMessages(messages)
expect(result).toHaveLength(1)
expect(result[0].role).toBe("assistant")
expect(result[0].content).toHaveLength(2)
// Text must come before tool calls so that tool calls are at the end,
// properly followed by user message with tool results
const [textContent, toolCall] = result[0].content as [MockLanguageModelTextPart, MockLanguageModelToolCallPart]
expect(textContent.type).toBe("text")
expect(toolCall.type).toBe("tool_call")
})
it("should handle image blocks with appropriate placeholders", () => {
const messages: Anthropic.Messages.MessageParam[] = [
{
role: "user",
content: [
{ type: "text", text: "Look at this:" },
{
type: "image",
source: {
type: "base64",
media_type: "image/png",
data: "base64data",
},
},
],
},
]
const result = convertToVsCodeLmMessages(messages)
expect(result).toHaveLength(1)
const imagePlaceholder = result[0].content[1] as MockLanguageModelTextPart
expect(imagePlaceholder.value).toContain("[Image (base64): image/png not supported by VSCode LM API]")
})
})
describe("convertToAnthropicRole", () => {
it("should convert assistant role correctly", () => {
const result = convertToAnthropicRole("assistant" as any)
expect(result).toBe("assistant")
})
it("should convert user role correctly", () => {
const result = convertToAnthropicRole("user" as any)
expect(result).toBe("user")
})
it("should return null for unknown roles", () => {
const result = convertToAnthropicRole("unknown" as any)
expect(result).toBeNull()
})
})
describe("extractTextCountFromMessage", () => {
it("should extract text from simple string content", () => {
const message = {
role: "user",
content: "Hello world",
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe("Hello world")
})
it("should extract text from LanguageModelTextPart", () => {
const mockTextPart = new (vitest.mocked(vscode).LanguageModelTextPart)("Text content")
const message = {
role: "user",
content: [mockTextPart],
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe("Text content")
})
it("should extract text from multiple LanguageModelTextParts", () => {
const mockTextPart1 = new (vitest.mocked(vscode).LanguageModelTextPart)("First part")
const mockTextPart2 = new (vitest.mocked(vscode).LanguageModelTextPart)("Second part")
const message = {
role: "user",
content: [mockTextPart1, mockTextPart2],
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe("First partSecond part")
})
it("should extract text from LanguageModelToolResultPart", () => {
const mockTextPart = new (vitest.mocked(vscode).LanguageModelTextPart)("Tool result content")
const mockToolResultPart = new (vitest.mocked(vscode).LanguageModelToolResultPart)("tool-result-id", [
mockTextPart,
])
const message = {
role: "user",
content: [mockToolResultPart],
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe("tool-result-idTool result content")
})
it("should extract text from LanguageModelToolCallPart without input", () => {
const mockToolCallPart = new (vitest.mocked(vscode).LanguageModelToolCallPart)("call-id", "tool-name", {})
const message = {
role: "assistant",
content: [mockToolCallPart],
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe("tool-namecall-id")
})
it("should extract text from LanguageModelToolCallPart with input", () => {
const mockInput = { operation: "add", numbers: [1, 2, 3] }
const mockToolCallPart = new (vitest.mocked(vscode).LanguageModelToolCallPart)(
"call-id",
"calculator",
mockInput,
)
const message = {
role: "assistant",
content: [mockToolCallPart],
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe(`calculatorcall-id${JSON.stringify(mockInput)}`)
})
it("should extract text from LanguageModelToolCallPart with empty input", () => {
const mockToolCallPart = new (vitest.mocked(vscode).LanguageModelToolCallPart)("call-id", "tool-name", {})
const message = {
role: "assistant",
content: [mockToolCallPart],
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe("tool-namecall-id")
})
it("should extract text from mixed content types", () => {
const mockTextPart = new (vitest.mocked(vscode).LanguageModelTextPart)("Text content")
const mockToolResultTextPart = new (vitest.mocked(vscode).LanguageModelTextPart)("Tool result")
const mockToolResultPart = new (vitest.mocked(vscode).LanguageModelToolResultPart)("result-id", [
mockToolResultTextPart,
])
const mockInput = { param: "value" }
const mockToolCallPart = new (vitest.mocked(vscode).LanguageModelToolCallPart)("call-id", "tool", mockInput)
const message = {
role: "assistant",
content: [mockTextPart, mockToolResultPart, mockToolCallPart],
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe(`Text contentresult-idTool resulttoolcall-id${JSON.stringify(mockInput)}`)
})
it("should handle empty array content", () => {
const message = {
role: "user",
content: [],
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe("")
})
it("should handle undefined content", () => {
const message = {
role: "user",
content: undefined,
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe("")
})
it("should handle ToolResultPart with multiple text parts", () => {
const mockTextPart1 = new (vitest.mocked(vscode).LanguageModelTextPart)("Part 1")
const mockTextPart2 = new (vitest.mocked(vscode).LanguageModelTextPart)("Part 2")
const mockToolResultPart = new (vitest.mocked(vscode).LanguageModelToolResultPart)("result-id", [
mockTextPart1,
mockTextPart2,
])
const message = {
role: "user",
content: [mockToolResultPart],
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe("result-idPart 1Part 2")
})
it("should handle ToolResultPart with empty parts array", () => {
const mockToolResultPart = new (vitest.mocked(vscode).LanguageModelToolResultPart)("result-id", [])
const message = {
role: "user",
content: [mockToolResultPart],
} as any
const result = extractTextCountFromMessage(message)
expect(result).toBe("result-id")
})
})