mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 22:21:23 +00:00
test(agents): add comprehensive kimi regressions
This commit is contained in:
47
src/agents/moonshot.live.test.ts
Normal file
47
src/agents/moonshot.live.test.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { completeSimple, type Model } from "@mariozechner/pi-ai";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isTruthyEnvValue } from "../infra/env.js";
|
||||
|
||||
const MOONSHOT_KEY = process.env.MOONSHOT_API_KEY ?? "";
|
||||
const MOONSHOT_BASE_URL = process.env.MOONSHOT_BASE_URL?.trim() || "https://api.moonshot.ai/v1";
|
||||
const MOONSHOT_MODEL = process.env.MOONSHOT_MODEL?.trim() || "kimi-k2.5";
|
||||
const LIVE = isTruthyEnvValue(process.env.MOONSHOT_LIVE_TEST) || isTruthyEnvValue(process.env.LIVE);
|
||||
|
||||
const describeLive = LIVE && MOONSHOT_KEY ? describe : describe.skip;
|
||||
|
||||
describeLive("moonshot live", () => {
|
||||
it("returns assistant text", async () => {
|
||||
const model: Model<"openai-completions"> = {
|
||||
id: MOONSHOT_MODEL,
|
||||
name: `Moonshot ${MOONSHOT_MODEL}`,
|
||||
api: "openai-completions",
|
||||
provider: "moonshot",
|
||||
baseUrl: MOONSHOT_BASE_URL,
|
||||
reasoning: false,
|
||||
input: ["text", "image"],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 256000,
|
||||
maxTokens: 8192,
|
||||
};
|
||||
|
||||
const res = await completeSimple(
|
||||
model,
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: "Reply with the word ok.",
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
],
|
||||
},
|
||||
{ apiKey: MOONSHOT_KEY, maxTokens: 64 },
|
||||
);
|
||||
|
||||
const text = res.content
|
||||
.filter((block) => block.type === "text")
|
||||
.map((block) => block.text.trim())
|
||||
.join(" ");
|
||||
expect(text.length).toBeGreaterThan(0);
|
||||
}, 30000);
|
||||
});
|
||||
@@ -5,6 +5,16 @@ import "./test-helpers/fast-coding-tools.js";
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
|
||||
type PiAiMockState = {
|
||||
lastModel: { provider?: string; id?: string; compat?: unknown } | null;
|
||||
};
|
||||
|
||||
const piAiMockState = vi.hoisted(
|
||||
(): PiAiMockState => ({
|
||||
lastModel: null,
|
||||
}),
|
||||
);
|
||||
|
||||
function createMockUsage(input: number, output: number) {
|
||||
return {
|
||||
input,
|
||||
@@ -88,6 +98,7 @@ vi.mock("@mariozechner/pi-ai", async () => {
|
||||
return buildAssistantMessage(model);
|
||||
},
|
||||
streamSimple: (model: { api: string; provider: string; id: string }) => {
|
||||
piAiMockState.lastModel = model as { provider?: string; id?: string; compat?: unknown };
|
||||
const stream = actual.createAssistantMessageEventStream();
|
||||
queueMicrotask(() => {
|
||||
stream.push({
|
||||
@@ -233,7 +244,63 @@ const runDefaultEmbeddedTurn = async (sessionFile: string, prompt: string, sessi
|
||||
});
|
||||
};
|
||||
|
||||
const makeMoonshotConfig = (modelIds: string[]) =>
|
||||
({
|
||||
models: {
|
||||
providers: {
|
||||
moonshot: {
|
||||
api: "openai-completions",
|
||||
apiKey: "sk-test",
|
||||
baseUrl: "https://api.moonshot.ai/v1",
|
||||
models: modelIds.map((id) => ({
|
||||
id,
|
||||
name: `Moonshot ${id}`,
|
||||
reasoning: false,
|
||||
input: ["text", "image"],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 256_000,
|
||||
maxTokens: 8_192,
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
}) satisfies OpenClawConfig;
|
||||
|
||||
describe("runEmbeddedPiAgent", () => {
|
||||
it("normalizes moonshot models to disable developer-role payloads in runner calls", async () => {
|
||||
piAiMockState.lastModel = null;
|
||||
const sessionFile = nextSessionFile();
|
||||
const sessionKey = nextSessionKey();
|
||||
const cfg = makeMoonshotConfig(["kimi-k2.5"]);
|
||||
|
||||
await runEmbeddedPiAgent({
|
||||
sessionId: "session:test",
|
||||
sessionKey,
|
||||
sessionFile,
|
||||
workspaceDir,
|
||||
config: cfg,
|
||||
prompt: "reply with ok",
|
||||
provider: "moonshot",
|
||||
model: "kimi-k2.5",
|
||||
timeoutMs: 5_000,
|
||||
agentDir,
|
||||
runId: nextRunId("moonshot-compat"),
|
||||
enqueue: immediateEnqueue,
|
||||
});
|
||||
|
||||
const capturedModel = piAiMockState.lastModel as {
|
||||
provider?: string;
|
||||
id?: string;
|
||||
compat?: unknown;
|
||||
} | null;
|
||||
expect(capturedModel?.provider).toBe("moonshot");
|
||||
expect(capturedModel?.id).toBe("kimi-k2.5");
|
||||
expect(
|
||||
(capturedModel?.compat as { supportsDeveloperRole?: boolean } | undefined)
|
||||
?.supportsDeveloperRole,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("handles prompt error paths without dropping user state", async () => {
|
||||
for (const testCase of [
|
||||
{
|
||||
|
||||
@@ -62,6 +62,51 @@ function stubMinimaxOkFetch() {
|
||||
return fetch;
|
||||
}
|
||||
|
||||
function stubOpenAiCompletionsOkFetch(text = "ok") {
|
||||
const fetch = vi.fn().mockResolvedValue(
|
||||
new Response(
|
||||
new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
const encoder = new TextEncoder();
|
||||
const chunks = [
|
||||
`data: ${JSON.stringify({
|
||||
id: "chatcmpl-moonshot-test",
|
||||
object: "chat.completion.chunk",
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
model: "kimi-k2.5",
|
||||
choices: [
|
||||
{
|
||||
index: 0,
|
||||
delta: { role: "assistant", content: text },
|
||||
finish_reason: null,
|
||||
},
|
||||
],
|
||||
})}\n\n`,
|
||||
`data: ${JSON.stringify({
|
||||
id: "chatcmpl-moonshot-test",
|
||||
object: "chat.completion.chunk",
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
model: "kimi-k2.5",
|
||||
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
|
||||
})}\n\n`,
|
||||
"data: [DONE]\n\n",
|
||||
];
|
||||
for (const chunk of chunks) {
|
||||
controller.enqueue(encoder.encode(chunk));
|
||||
}
|
||||
controller.close();
|
||||
},
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: { "content-type": "text/event-stream" },
|
||||
},
|
||||
),
|
||||
);
|
||||
global.fetch = withFetchPreconnect(fetch);
|
||||
return fetch;
|
||||
}
|
||||
|
||||
function createMinimaxImageConfig(): OpenClawConfig {
|
||||
return {
|
||||
agents: {
|
||||
@@ -270,6 +315,71 @@ describe("image tool implicit imageModel config", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("sends moonshot image requests with user+image payloads only", async () => {
|
||||
await withTempAgentDir(async (agentDir) => {
|
||||
vi.stubEnv("MOONSHOT_API_KEY", "moonshot-test");
|
||||
const fetch = stubOpenAiCompletionsOkFetch("ok moonshot");
|
||||
const cfg: OpenClawConfig = {
|
||||
agents: {
|
||||
defaults: {
|
||||
model: { primary: "moonshot/kimi-k2.5" },
|
||||
imageModel: { primary: "moonshot/kimi-k2.5" },
|
||||
},
|
||||
},
|
||||
models: {
|
||||
providers: {
|
||||
moonshot: {
|
||||
api: "openai-completions",
|
||||
baseUrl: "https://api.moonshot.ai/v1",
|
||||
models: [makeModelDefinition("kimi-k2.5", ["text", "image"])],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const tool = requireImageTool(createImageTool({ config: cfg, agentDir }));
|
||||
const result = await tool.execute("t1", {
|
||||
prompt: "Describe this image in one word.",
|
||||
image: `data:image/png;base64,${ONE_PIXEL_PNG_B64}`,
|
||||
});
|
||||
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
const [url, init] = fetch.mock.calls[0] as [unknown, { body?: unknown }];
|
||||
expect(String(url)).toBe("https://api.moonshot.ai/v1/chat/completions");
|
||||
expect(typeof init?.body).toBe("string");
|
||||
const bodyRaw = typeof init?.body === "string" ? init.body : "";
|
||||
const payload = JSON.parse(bodyRaw) as {
|
||||
messages?: Array<{
|
||||
role?: string;
|
||||
content?: Array<{
|
||||
type?: string;
|
||||
text?: string;
|
||||
image_url?: { url?: string };
|
||||
}>;
|
||||
}>;
|
||||
};
|
||||
|
||||
expect(payload.messages?.map((message) => message.role)).toEqual(["user"]);
|
||||
const userContent = payload.messages?.[0]?.content ?? [];
|
||||
expect(userContent).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
type: "text",
|
||||
text: "Describe this image in one word.",
|
||||
}),
|
||||
expect.objectContaining({ type: "image_url" }),
|
||||
]),
|
||||
);
|
||||
expect(userContent.find((block) => block.type === "image_url")?.image_url?.url).toContain(
|
||||
"data:image/png;base64,",
|
||||
);
|
||||
expect(bodyRaw).not.toContain('"role":"developer"');
|
||||
expect(result.content).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ type: "text", text: "ok moonshot" })]),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it("exposes an Anthropic-safe image schema without union keywords", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-"));
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user