|
| 1 | +import { createAssistantMessageEventStream } from "openclaw/plugin-sdk/llm"; |
| 2 | +// Tests for wrapOllamaCompatNumCtx — verifies FIX #96441: |
| 3 | +// OpenAI-compatible tool_calls.function.arguments must remain as JSON strings |
| 4 | +// (not be normalized back to objects). |
| 5 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +// Track every payload patcher that wrapOllamaCompatNumCtx registers. |
| 8 | +const capturedPatchers: Array<(payload: Record<string, unknown>) => void> = []; |
| 9 | + |
| 10 | +vi.mock("openclaw/plugin-sdk/provider-stream-shared", () => ({ |
| 11 | + streamWithPayloadPatch: vi.fn( |
| 12 | + ( |
| 13 | + _streamFn: unknown, |
| 14 | + _model: unknown, |
| 15 | + _context: unknown, |
| 16 | + _options: unknown, |
| 17 | + patcher: (payload: Record<string, unknown>) => void, |
| 18 | + ) => { |
| 19 | + capturedPatchers.push(patcher); |
| 20 | + return createAssistantMessageEventStream(); |
| 21 | + }, |
| 22 | + ), |
| 23 | +})); |
| 24 | + |
| 25 | +import { wrapOllamaCompatNumCtx } from "./stream.js"; |
| 26 | + |
| 27 | +describe("wrapOllamaCompatNumCtx — FIX #96441", () => { |
| 28 | + beforeEach(() => { |
| 29 | + capturedPatchers.length = 0; |
| 30 | + }); |
| 31 | + |
| 32 | + it("injects num_ctx into payload options", async () => { |
| 33 | + const wrapped = wrapOllamaCompatNumCtx(undefined, 4096); |
| 34 | + await wrapped({} as any, { messages: [], systemPrompt: "" } as any); |
| 35 | + |
| 36 | + expect(capturedPatchers.length).toBe(1); |
| 37 | + const patcher = capturedPatchers[0]; |
| 38 | + |
| 39 | + const payload: Record<string, unknown> = { options: {}, messages: [] }; |
| 40 | + patcher(payload); |
| 41 | + |
| 42 | + expect((payload.options as Record<string, unknown>).num_ctx).toBe(4096); |
| 43 | + }); |
| 44 | + |
| 45 | + it("preserves tool_calls[*].function.arguments as strings (FIX #96441)", async () => { |
| 46 | + const wrapped = wrapOllamaCompatNumCtx(undefined, 4096); |
| 47 | + await wrapped({} as any, { messages: [], systemPrompt: "" } as any); |
| 48 | + |
| 49 | + expect(capturedPatchers.length).toBe(1); |
| 50 | + const patcher = capturedPatchers[0]; |
| 51 | + |
| 52 | + // Simulate the payload that openai-completions.ts produces: |
| 53 | + // arguments is a JSON string per the OpenAI API spec. |
| 54 | + const payload: Record<string, unknown> = { |
| 55 | + options: {}, |
| 56 | + messages: [ |
| 57 | + { |
| 58 | + role: "assistant", |
| 59 | + tool_calls: [ |
| 60 | + { |
| 61 | + id: "call_1", |
| 62 | + type: "function", |
| 63 | + function: { |
| 64 | + name: "config_get", |
| 65 | + arguments: '{"action":"config.get","path":"gateway.port"}', |
| 66 | + }, |
| 67 | + }, |
| 68 | + ], |
| 69 | + }, |
| 70 | + ], |
| 71 | + }; |
| 72 | + |
| 73 | + patcher(payload); |
| 74 | + |
| 75 | + // Read back the tool_calls and verify arguments stayed as a string. |
| 76 | + const messages = payload.messages as Array<Record<string, unknown>>; |
| 77 | + const toolCalls = messages[0].tool_calls as Array<Record<string, unknown>>; |
| 78 | + const fnSpec = toolCalls[0].function as Record<string, unknown>; |
| 79 | + |
| 80 | + expect(typeof fnSpec.arguments).toBe("string"); |
| 81 | + expect(fnSpec.arguments).toBe('{"action":"config.get","path":"gateway.port"}'); |
| 82 | + }); |
| 83 | + |
| 84 | + it("preserves function_call.arguments as strings (legacy format)", async () => { |
| 85 | + const wrapped = wrapOllamaCompatNumCtx(undefined, 4096); |
| 86 | + await wrapped({} as any, { messages: [], systemPrompt: "" } as any); |
| 87 | + |
| 88 | + expect(capturedPatchers.length).toBe(1); |
| 89 | + const patcher = capturedPatchers[0]; |
| 90 | + |
| 91 | + // Some older models use the legacy function_call format. |
| 92 | + const payload: Record<string, unknown> = { |
| 93 | + options: {}, |
| 94 | + messages: [ |
| 95 | + { |
| 96 | + role: "assistant", |
| 97 | + function_call: { |
| 98 | + name: "get_weather", |
| 99 | + arguments: '{"city":"London"}', |
| 100 | + }, |
| 101 | + }, |
| 102 | + ], |
| 103 | + }; |
| 104 | + |
| 105 | + patcher(payload); |
| 106 | + |
| 107 | + const messages = payload.messages as Array<Record<string, unknown>>; |
| 108 | + const fnCall = messages[0].function_call as Record<string, unknown>; |
| 109 | + |
| 110 | + expect(typeof fnCall.arguments).toBe("string"); |
| 111 | + expect(fnCall.arguments).toBe('{"city":"London"}'); |
| 112 | + }); |
| 113 | + |
| 114 | + it("handles messages without tool_calls or function_call gracefully", async () => { |
| 115 | + const wrapped = wrapOllamaCompatNumCtx(undefined, 4096); |
| 116 | + await wrapped({} as any, { messages: [], systemPrompt: "" } as any); |
| 117 | + |
| 118 | + expect(capturedPatchers.length).toBe(1); |
| 119 | + const patcher = capturedPatchers[0]; |
| 120 | + |
| 121 | + // Payload with a plain user message — no tool-related fields. |
| 122 | + const payload: Record<string, unknown> = { |
| 123 | + options: {}, |
| 124 | + messages: [ |
| 125 | + { role: "user", content: "Hello" }, |
| 126 | + { role: "assistant", content: "Hi there" }, |
| 127 | + ], |
| 128 | + }; |
| 129 | + |
| 130 | + // Should not throw or modify anything. |
| 131 | + expect(() => patcher(payload)).not.toThrow(); |
| 132 | + expect((payload.messages as Array<Record<string, unknown>>)[1].content).toBe("Hi there"); |
| 133 | + }); |
| 134 | +}); |
0 commit comments