From 8b443bb34fcfe713dd8b9b353a2b2ac2213f3eee Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 5 Aug 2026 02:44:19 +0800 Subject: [PATCH] test(llama-cpp): split inference mapping coverage Punchcard-Session: frost-brook-timber-mx --- .../src/inference-provider.mapping.test.ts | 101 ++++++++++++++++++ .../llama-cpp/src/inference-provider.test.ts | 95 ---------------- 2 files changed, 101 insertions(+), 95 deletions(-) create mode 100644 extensions/llama-cpp/src/inference-provider.mapping.test.ts diff --git a/extensions/llama-cpp/src/inference-provider.mapping.test.ts b/extensions/llama-cpp/src/inference-provider.mapping.test.ts new file mode 100644 index 000000000000..c70dd6b3b401 --- /dev/null +++ b/extensions/llama-cpp/src/inference-provider.mapping.test.ts @@ -0,0 +1,101 @@ +import type { Context } from "openclaw/plugin-sdk/llm"; +import { describe, expect, it } from "vitest"; +import "./inference-provider.js"; + +const { mapContextToLlamaChatHistory, mapToolsToLlamaFunctions } = ( + globalThis as Record +)[Symbol.for("openclaw.llamaCppInferenceTestApi")] as { + mapContextToLlamaChatHistory: (context: Context) => unknown[]; + mapToolsToLlamaFunctions: (context: Context) => Record | undefined; +}; + +describe("llama.cpp inference mappings", () => { + it("maps OpenClaw history and tool results into the model chat template history", () => { + const context = { + systemPrompt: "Be concise.", + messages: [ + { role: "user" as const, content: "weather?", timestamp: 1 }, + { + role: "assistant" as const, + api: "openai-completions", + provider: "test", + model: "test", + stopReason: "toolUse" as const, + usage: { + input: 1, + output: 1, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 2, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + timestamp: 2, + content: [ + { type: "text" as const, text: "Checking." }, + { + type: "toolCall" as const, + id: "call-1", + name: "weather", + arguments: { city: "Berlin" }, + }, + ], + }, + { + role: "toolResult" as const, + toolCallId: "call-1", + toolName: "weather", + content: [{ type: "text" as const, text: "Sunny" }], + isError: false, + timestamp: 3, + }, + { role: "user" as const, content: "thanks", timestamp: 4 }, + ], + }; + + expect(mapContextToLlamaChatHistory(context)).toEqual([ + { type: "system", text: "Be concise." }, + { type: "user", text: "weather?" }, + { + type: "model", + response: [ + "Checking.", + { + type: "functionCall", + name: "weather", + params: { city: "Berlin" }, + result: "Sunny", + }, + ], + }, + { type: "user", text: "thanks" }, + ]); + }); + + it("maps JSON-schema tools to native node-llama-cpp function definitions", () => { + expect( + mapToolsToLlamaFunctions({ + messages: [], + tools: [ + { + name: "weather", + description: "Get weather", + parameters: { + type: "object", + properties: { city: { type: "string" } }, + required: ["city"], + }, + }, + ], + }), + ).toEqual({ + weather: { + description: "Get weather", + params: { + type: "object", + properties: { city: { type: "string" } }, + required: ["city"], + }, + }, + }); + }); +}); diff --git a/extensions/llama-cpp/src/inference-provider.test.ts b/extensions/llama-cpp/src/inference-provider.test.ts index e14857caa969..63bc58a09c21 100644 --- a/extensions/llama-cpp/src/inference-provider.test.ts +++ b/extensions/llama-cpp/src/inference-provider.test.ts @@ -56,12 +56,6 @@ vi.mock("node-llama-cpp", () => ({ import { createLlamaCppInferenceRuntime } from "./inference-provider.js"; -const { mapContextToLlamaChatHistory, mapToolsToLlamaFunctions } = ( - globalThis as Record -)[Symbol.for("openclaw.llamaCppInferenceTestApi")] as { - mapContextToLlamaChatHistory: (context: Context) => unknown[]; - mapToolsToLlamaFunctions: (context: Context) => Record | undefined; -}; type LlamaCppInferenceRuntime = ReturnType; let inferenceRuntime: LlamaCppInferenceRuntime; @@ -165,95 +159,6 @@ afterEach(async () => { }); describe("llama.cpp inference provider", () => { - it("maps OpenClaw history and tool results into the model chat template history", () => { - const context = { - systemPrompt: "Be concise.", - messages: [ - { role: "user" as const, content: "weather?", timestamp: 1 }, - { - role: "assistant" as const, - api: "openai-completions", - provider: "test", - model: "test", - stopReason: "toolUse" as const, - usage: { - input: 1, - output: 1, - cacheRead: 0, - cacheWrite: 0, - totalTokens: 2, - cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, - }, - timestamp: 2, - content: [ - { type: "text" as const, text: "Checking." }, - { - type: "toolCall" as const, - id: "call-1", - name: "weather", - arguments: { city: "Berlin" }, - }, - ], - }, - { - role: "toolResult" as const, - toolCallId: "call-1", - toolName: "weather", - content: [{ type: "text" as const, text: "Sunny" }], - isError: false, - timestamp: 3, - }, - { role: "user" as const, content: "thanks", timestamp: 4 }, - ], - }; - - expect(mapContextToLlamaChatHistory(context)).toEqual([ - { type: "system", text: "Be concise." }, - { type: "user", text: "weather?" }, - { - type: "model", - response: [ - "Checking.", - { - type: "functionCall", - name: "weather", - params: { city: "Berlin" }, - result: "Sunny", - }, - ], - }, - { type: "user", text: "thanks" }, - ]); - }); - - it("maps JSON-schema tools to native node-llama-cpp function definitions", () => { - expect( - mapToolsToLlamaFunctions({ - messages: [], - tools: [ - { - name: "weather", - description: "Get weather", - parameters: { - type: "object", - properties: { city: { type: "string" } }, - required: ["city"], - }, - }, - ], - }), - ).toEqual({ - weather: { - description: "Get weather", - params: { - type: "object", - properties: { city: { type: "string" } }, - required: ["city"], - }, - }, - }); - }); - it("streams text deltas and reports native token-meter usage", async () => { mocks.generateResponse.mockImplementationOnce(async (_history, options) => { options.onTextChunk("Hel");