Skip to content

Commit 8421174

Browse files
committed
fix: address parser model review feedback
1 parent 52d8946 commit 8421174

5 files changed

Lines changed: 46 additions & 81 deletions

File tree

‎scripts/run-native-tool-call-parser-scoping.mjs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { rm } from "node:fs/promises"
1+
import { mkdtemp, rm } from "node:fs/promises"
22
import { tmpdir } from "node:os"
33
import { join } from "node:path"
44
import { fileURLToPath, pathToFileURL } from "node:url"
55

66
import { build } from "esbuild"
77

88
const entryPoint = fileURLToPath(new URL("./check-native-tool-call-parser-scoping.ts", import.meta.url))
9-
const outfile = join(tmpdir(), `zoo-parser-scope-model-${process.pid}.cjs`)
9+
const temporaryDirectory = await mkdtemp(join(tmpdir(), "zoo-parser-scope-model-"))
10+
const outfile = join(temporaryDirectory, "model.cjs")
1011

1112
try {
1213
await build({
@@ -20,5 +21,5 @@ try {
2021
})
2122
await import(pathToFileURL(outfile).href)
2223
} finally {
23-
await rm(outfile, { force: true })
24+
await rm(temporaryDirectory, { recursive: true, force: true })
2425
}

‎src/api/providers/__tests__/lmstudio-native-tools.spec.ts‎

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Mock OpenAI client - must come before other imports
44
const mockCreate = vi.fn()
55
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
6+
import { collectStreamAndParseToolCalls } from "../../../test-utils/native-tool-call-stream"
67
import { clearAllMocks } from "../../../test-utils/reset"
78
vi.mock("openai", () => {
89
return {
@@ -359,35 +360,11 @@ describe("LmStudioHandler Native Tools", () => {
359360
])
360361
mockCreate.mockImplementationOnce(() => firstStream()).mockImplementationOnce(() => secondStream)
361362

362-
const collectAndTrack = async (stream: ReturnType<LmStudioHandler["createMessage"]>) => {
363-
const chunks = []
364-
const parserEvents = []
365-
const parserScope = NativeToolCallParser.createScope()
366-
for await (const chunk of stream) {
367-
if (chunk.type === "tool_call_partial") {
368-
parserEvents.push(
369-
...NativeToolCallParser.processRawChunk(
370-
{
371-
index: chunk.index,
372-
id: chunk.id,
373-
name: chunk.name,
374-
arguments: chunk.arguments,
375-
},
376-
parserScope,
377-
),
378-
)
379-
}
380-
chunks.push(chunk)
381-
}
382-
NativeToolCallParser.clearRawChunkState(parserScope)
383-
return { chunks, parserEvents }
384-
}
385-
386-
const firstChunksPromise = collectAndTrack(
363+
const firstChunksPromise = collectStreamAndParseToolCalls(
387364
handler.createMessage("first", [], { taskId: "task-a", tools: testTools }),
388365
)
389366
await firstStreamPaused
390-
const secondChunks = await collectAndTrack(
367+
const secondChunks = await collectStreamAndParseToolCalls(
391368
handler.createMessage("second", [], { taskId: "task-b", tools: testTools }),
392369
)
393370
releaseFirstStream?.()

‎src/api/providers/__tests__/openrouter.spec.ts‎

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { OpenRouterHandler } from "../openrouter"
2323
import { Package } from "../../../shared/package"
2424
import { makeApiHandlerOptions } from "../../../test-utils/api"
2525
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
26+
import { collectStreamAndParseToolCalls } from "../../../test-utils/native-tool-call-stream"
2627
import { clearAllMocks } from "../../../test-utils/reset"
2728

2829
vitest.mock("openai")
@@ -660,33 +661,9 @@ describe("OpenRouterHandler", () => {
660661
})
661662
const handler = new OpenRouterHandler(mockOptions)
662663

663-
const collectAndTrack = async (stream: ReturnType<OpenRouterHandler["createMessage"]>) => {
664-
const chunks = []
665-
const parserEvents = []
666-
const parserScope = NativeToolCallParser.createScope()
667-
for await (const chunk of stream) {
668-
if (chunk.type === "tool_call_partial") {
669-
parserEvents.push(
670-
...NativeToolCallParser.processRawChunk(
671-
{
672-
index: chunk.index,
673-
id: chunk.id,
674-
name: chunk.name,
675-
arguments: chunk.arguments,
676-
},
677-
parserScope,
678-
),
679-
)
680-
}
681-
chunks.push(chunk)
682-
}
683-
NativeToolCallParser.clearRawChunkState(parserScope)
684-
return { chunks, parserEvents }
685-
}
686-
687-
const firstChunksPromise = collectAndTrack(handler.createMessage("first", []))
664+
const firstChunksPromise = collectStreamAndParseToolCalls(handler.createMessage("first", []))
688665
await firstStreamPaused
689-
const secondChunks = await collectAndTrack(handler.createMessage("second", []))
666+
const secondChunks = await collectStreamAndParseToolCalls(handler.createMessage("second", []))
690667
releaseFirstStream?.()
691668
const firstChunks = await firstChunksPromise
692669

‎src/api/providers/__tests__/qwen-code-native-tools.spec.ts‎

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ vi.mock("node:fs", () => ({
1010

1111
const mockCreate = vi.fn()
1212
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
13+
import { collectStreamAndParseToolCalls } from "../../../test-utils/native-tool-call-stream"
1314
import { clearAllMocks } from "../../../test-utils/reset"
1415
vi.mock("openai", () => {
1516
return {
@@ -373,35 +374,11 @@ describe("QwenCodeHandler Native Tools", () => {
373374
])
374375
mockCreate.mockImplementationOnce(() => firstStream()).mockImplementationOnce(() => secondStream)
375376

376-
const collectAndTrack = async (stream: ReturnType<QwenCodeHandler["createMessage"]>) => {
377-
const chunks = []
378-
const parserEvents = []
379-
const parserScope = NativeToolCallParser.createScope()
380-
for await (const chunk of stream) {
381-
if (chunk.type === "tool_call_partial") {
382-
parserEvents.push(
383-
...NativeToolCallParser.processRawChunk(
384-
{
385-
index: chunk.index,
386-
id: chunk.id,
387-
name: chunk.name,
388-
arguments: chunk.arguments,
389-
},
390-
parserScope,
391-
),
392-
)
393-
}
394-
chunks.push(chunk)
395-
}
396-
NativeToolCallParser.clearRawChunkState(parserScope)
397-
return { chunks, parserEvents }
398-
}
399-
400-
const firstChunksPromise = collectAndTrack(
377+
const firstChunksPromise = collectStreamAndParseToolCalls(
401378
handler.createMessage("first", [], { taskId: "task-a", tools: testTools }),
402379
)
403380
await firstStreamPaused
404-
const secondChunks = await collectAndTrack(
381+
const secondChunks = await collectStreamAndParseToolCalls(
405382
handler.createMessage("second", [], { taskId: "task-b", tools: testTools }),
406383
)
407384
releaseFirstStream?.()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { ApiStreamChunk } from "../api/transform/stream"
2+
import { NativeToolCallParser, type ToolCallStreamEvent } from "../core/assistant-message/NativeToolCallParser"
3+
4+
export async function collectStreamAndParseToolCalls(stream: AsyncIterable<ApiStreamChunk>): Promise<{
5+
chunks: ApiStreamChunk[]
6+
parserEvents: ToolCallStreamEvent[]
7+
}> {
8+
const chunks: ApiStreamChunk[] = []
9+
const parserEvents: ToolCallStreamEvent[] = []
10+
const parserScope = NativeToolCallParser.createScope()
11+
12+
try {
13+
for await (const chunk of stream) {
14+
if (chunk.type === "tool_call_partial") {
15+
parserEvents.push(
16+
...NativeToolCallParser.processRawChunk(
17+
{
18+
index: chunk.index,
19+
id: chunk.id,
20+
name: chunk.name,
21+
arguments: chunk.arguments,
22+
},
23+
parserScope,
24+
),
25+
)
26+
}
27+
chunks.push(chunk)
28+
}
29+
return { chunks, parserEvents }
30+
} finally {
31+
NativeToolCallParser.clearRawChunkState(parserScope)
32+
}
33+
}

0 commit comments

Comments
 (0)