|
| 1 | +// Gateway tests cover cheap-model tool-call title generation and its SQLite cache. |
| 2 | +import fs from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +const completeWithPreparedSimpleCompletionModel = vi.hoisted(() => vi.fn()); |
| 8 | +const prepareSimpleCompletionModelForAgent = vi.hoisted(() => vi.fn()); |
| 9 | +const resolveSimpleCompletionSelectionForAgent = vi.hoisted(() => vi.fn()); |
| 10 | + |
| 11 | +vi.mock("../agents/simple-completion-runtime.js", () => ({ |
| 12 | + completeWithPreparedSimpleCompletionModel, |
| 13 | + prepareSimpleCompletionModelForAgent, |
| 14 | + resolveSimpleCompletionSelectionForAgent, |
| 15 | +})); |
| 16 | + |
| 17 | +import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 18 | +import { closeOpenClawAgentDatabases } from "../state/openclaw-agent-db.js"; |
| 19 | +import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js"; |
| 20 | +import { generateToolCallTitles } from "./chat-tool-titles.js"; |
| 21 | + |
| 22 | +const AGENT_ID = "main"; |
| 23 | + |
| 24 | +function mockPreparedModel(): void { |
| 25 | + prepareSimpleCompletionModelForAgent.mockResolvedValue({ |
| 26 | + selection: { provider: "openai", modelId: "gpt-test", agentDir: "/tmp/openclaw-agent" }, |
| 27 | + model: { provider: "openai", id: "gpt-test", maxTokens: 8192 }, |
| 28 | + auth: { apiKey: "k", mode: "api-key" }, |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +function mockCompletionTitles(titles: Record<string, string>): void { |
| 33 | + completeWithPreparedSimpleCompletionModel.mockResolvedValue({ |
| 34 | + stopReason: "stop", |
| 35 | + content: [{ type: "text", text: JSON.stringify({ titles }) }], |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +describe("generateToolCallTitles", () => { |
| 40 | + let stateDir: string; |
| 41 | + let previousStateDir: string | undefined; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + completeWithPreparedSimpleCompletionModel.mockReset(); |
| 45 | + prepareSimpleCompletionModelForAgent.mockReset(); |
| 46 | + resolveSimpleCompletionSelectionForAgent.mockReset(); |
| 47 | + // Default: the agent's primary model already routes to OpenAI, so the |
| 48 | + // Luna fallback is permitted by the egress gate. |
| 49 | + resolveSimpleCompletionSelectionForAgent.mockReturnValue({ |
| 50 | + provider: "openai", |
| 51 | + modelId: "gpt-5.5", |
| 52 | + agentDir: "/tmp/openclaw-agent", |
| 53 | + }); |
| 54 | + // realpath: macOS tmpdir is a /var -> /private/var symlink and DB paths resolve canonically. |
| 55 | + stateDir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-tool-titles-"))); |
| 56 | + previousStateDir = process.env.OPENCLAW_STATE_DIR; |
| 57 | + process.env.OPENCLAW_STATE_DIR = stateDir; |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + closeOpenClawAgentDatabases(); |
| 62 | + closeOpenClawStateDatabaseForTest(); |
| 63 | + if (previousStateDir === undefined) { |
| 64 | + delete process.env.OPENCLAW_STATE_DIR; |
| 65 | + } else { |
| 66 | + process.env.OPENCLAW_STATE_DIR = previousStateDir; |
| 67 | + } |
| 68 | + fs.rmSync(stateDir, { recursive: true, force: true }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("generates titles keyed by item id", async () => { |
| 72 | + mockPreparedModel(); |
| 73 | + mockCompletionTitles({ "item-1": "Checked repo status", "item-2": "Listed source files" }); |
| 74 | + |
| 75 | + const result = await generateToolCallTitles({ |
| 76 | + cfg: {} satisfies OpenClawConfig, |
| 77 | + agentId: AGENT_ID, |
| 78 | + items: [ |
| 79 | + { id: "item-1", name: "bash", input: "git status --short" }, |
| 80 | + { id: "item-2", name: "bash", input: "ls -la src" }, |
| 81 | + ], |
| 82 | + }); |
| 83 | + |
| 84 | + expect(result).toEqual({ |
| 85 | + "item-1": "Checked repo status", |
| 86 | + "item-2": "Listed source files", |
| 87 | + }); |
| 88 | + expect(completeWithPreparedSimpleCompletionModel).toHaveBeenCalledTimes(1); |
| 89 | + }); |
| 90 | + |
| 91 | + it("serves repeated items from the SQLite cache without a second completion", async () => { |
| 92 | + mockPreparedModel(); |
| 93 | + mockCompletionTitles({ "item-1": "Checked repo status" }); |
| 94 | + const params = { |
| 95 | + cfg: {} satisfies OpenClawConfig, |
| 96 | + agentId: AGENT_ID, |
| 97 | + items: [{ id: "item-1", name: "bash", input: "git status --short" }], |
| 98 | + }; |
| 99 | + |
| 100 | + const first = await generateToolCallTitles(params); |
| 101 | + const second = await generateToolCallTitles(params); |
| 102 | + |
| 103 | + expect(first).toEqual({ "item-1": "Checked repo status" }); |
| 104 | + expect(second).toEqual(first); |
| 105 | + expect(completeWithPreparedSimpleCompletionModel).toHaveBeenCalledTimes(1); |
| 106 | + }); |
| 107 | + |
| 108 | + it("fails closed to an empty result when model preparation errors", async () => { |
| 109 | + prepareSimpleCompletionModelForAgent.mockResolvedValue({ |
| 110 | + error: 'No API key resolved for provider "openai".', |
| 111 | + }); |
| 112 | + |
| 113 | + await expect( |
| 114 | + generateToolCallTitles({ |
| 115 | + cfg: {} satisfies OpenClawConfig, |
| 116 | + agentId: AGENT_ID, |
| 117 | + items: [{ id: "item-1", name: "bash", input: "git status --short" }], |
| 118 | + }), |
| 119 | + ).resolves.toEqual({}); |
| 120 | + expect(completeWithPreparedSimpleCompletionModel).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + it("prepares the configured utility model when one is set", async () => { |
| 124 | + mockPreparedModel(); |
| 125 | + mockCompletionTitles({ "item-1": "Checked repo status" }); |
| 126 | + const cfg = { agents: { defaults: { utilityModel: "openai/gpt-test" } } }; |
| 127 | + |
| 128 | + await generateToolCallTitles({ |
| 129 | + cfg, |
| 130 | + agentId: AGENT_ID, |
| 131 | + items: [{ id: "item-1", name: "bash", input: "git status --short" }], |
| 132 | + }); |
| 133 | + |
| 134 | + expect(prepareSimpleCompletionModelForAgent).toHaveBeenCalledWith({ |
| 135 | + cfg, |
| 136 | + agentId: AGENT_ID, |
| 137 | + useUtilityModel: true, |
| 138 | + useAsyncModelResolution: true, |
| 139 | + allowMissingApiKeyModes: ["aws-sdk"], |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it("falls back to the Luna default model ref without a utility model", async () => { |
| 144 | + mockPreparedModel(); |
| 145 | + mockCompletionTitles({ "item-1": "Checked repo status" }); |
| 146 | + const cfg = {} satisfies OpenClawConfig; |
| 147 | + |
| 148 | + await generateToolCallTitles({ |
| 149 | + cfg, |
| 150 | + agentId: AGENT_ID, |
| 151 | + items: [{ id: "item-1", name: "bash", input: "git status --short" }], |
| 152 | + }); |
| 153 | + |
| 154 | + expect(prepareSimpleCompletionModelForAgent).toHaveBeenCalledWith({ |
| 155 | + cfg, |
| 156 | + agentId: AGENT_ID, |
| 157 | + modelRef: "openai/gpt-5.6-luna", |
| 158 | + useAsyncModelResolution: true, |
| 159 | + allowMissingApiKeyModes: ["aws-sdk"], |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + it("skips the Luna default when the agent's primary provider is not OpenAI", async () => { |
| 164 | + resolveSimpleCompletionSelectionForAgent.mockReturnValue({ |
| 165 | + provider: "anthropic", |
| 166 | + modelId: "claude-test", |
| 167 | + agentDir: "/tmp/openclaw-agent", |
| 168 | + }); |
| 169 | + |
| 170 | + await expect( |
| 171 | + generateToolCallTitles({ |
| 172 | + cfg: {} satisfies OpenClawConfig, |
| 173 | + agentId: AGENT_ID, |
| 174 | + items: [{ id: "item-1", name: "bash", input: "git status --short" }], |
| 175 | + }), |
| 176 | + ).resolves.toEqual({}); |
| 177 | + expect(prepareSimpleCompletionModelForAgent).not.toHaveBeenCalled(); |
| 178 | + expect(completeWithPreparedSimpleCompletionModel).not.toHaveBeenCalled(); |
| 179 | + }); |
| 180 | +}); |
0 commit comments