Vendor OpenClaw source as Adolf fork baseline
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
This commit is contained in:
587
extensions/zai/index.test.ts
Normal file
587
extensions/zai/index.test.ts
Normal file
@@ -0,0 +1,587 @@
|
||||
// Zai tests cover index plugin behavior.
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import type { StreamFn } from "openclaw/plugin-sdk/agent-core";
|
||||
import type { Context, Model } from "openclaw/plugin-sdk/llm";
|
||||
import { registerSingleProviderPlugin } from "openclaw/plugin-sdk/plugin-test-runtime";
|
||||
import { buildOpenAICompletionsParams } from "openclaw/plugin-sdk/provider-transport-runtime";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import plugin from "./index.js";
|
||||
|
||||
function createGlm47Template() {
|
||||
return {
|
||||
id: "glm-4.7",
|
||||
name: "GLM-4.7",
|
||||
provider: "zai",
|
||||
api: "openai-completions",
|
||||
baseUrl: "https://api.z.ai/api/paas/v4",
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: { input: 0.6, output: 2.2, cacheRead: 0.11, cacheWrite: 0 },
|
||||
contextWindow: 204800,
|
||||
maxTokens: 131072,
|
||||
};
|
||||
}
|
||||
|
||||
function expectReplayPolicyFields(
|
||||
policy: Record<string, unknown> | undefined,
|
||||
fields: Record<string, unknown>,
|
||||
): void {
|
||||
if (!policy) {
|
||||
throw new Error("Expected replay policy");
|
||||
}
|
||||
for (const [key, value] of Object.entries(fields)) {
|
||||
expect(policy[key]).toEqual(value);
|
||||
}
|
||||
}
|
||||
|
||||
function expectModelFields(
|
||||
model: Record<string, unknown> | undefined,
|
||||
fields: Record<string, unknown>,
|
||||
): void {
|
||||
if (!model) {
|
||||
throw new Error("Expected provider model");
|
||||
}
|
||||
for (const [key, value] of Object.entries(fields)) {
|
||||
expect(model[key]).toEqual(value);
|
||||
}
|
||||
}
|
||||
|
||||
describe("zai provider plugin", () => {
|
||||
it("owns replay policy for OpenAI-compatible Z.ai transports", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
|
||||
expectReplayPolicyFields(
|
||||
provider.buildReplayPolicy?.({
|
||||
provider: "zai",
|
||||
modelApi: "openai-completions",
|
||||
modelId: "glm-5.1",
|
||||
} as never) as Record<string, unknown> | undefined,
|
||||
{
|
||||
sanitizeToolCallIds: true,
|
||||
toolCallIdMode: "strict",
|
||||
applyAssistantFirstOrderingFix: true,
|
||||
validateGeminiTurns: true,
|
||||
validateAnthropicTurns: true,
|
||||
},
|
||||
);
|
||||
|
||||
expectReplayPolicyFields(
|
||||
provider.buildReplayPolicy?.({
|
||||
provider: "zai",
|
||||
modelApi: "openai-responses",
|
||||
modelId: "glm-5.1",
|
||||
} as never) as Record<string, unknown> | undefined,
|
||||
{
|
||||
sanitizeToolCallIds: true,
|
||||
toolCallIdMode: "strict",
|
||||
applyAssistantFirstOrderingFix: false,
|
||||
validateGeminiTurns: false,
|
||||
validateAnthropicTurns: false,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("resolves persisted GLM-5 family models with provider-owned metadata", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
const template = createGlm47Template();
|
||||
|
||||
const cases = [
|
||||
{
|
||||
modelId: "glm-5.2",
|
||||
providerBaseUrl: "https://api.z.ai/api/coding/paas/v4",
|
||||
expected: {
|
||||
baseUrl: "https://api.z.ai/api/coding/paas/v4",
|
||||
input: ["text"],
|
||||
reasoning: true,
|
||||
contextWindow: 1_000_000,
|
||||
maxTokens: 131_072,
|
||||
},
|
||||
},
|
||||
{
|
||||
modelId: "glm-5.1",
|
||||
providerBaseUrl: "https://api.z.ai/api/paas/v4",
|
||||
expected: {
|
||||
baseUrl: "https://api.z.ai/api/paas/v4",
|
||||
input: ["text"],
|
||||
reasoning: true,
|
||||
contextWindow: 202800,
|
||||
maxTokens: 131100,
|
||||
},
|
||||
},
|
||||
{
|
||||
modelId: "glm-5v-turbo",
|
||||
providerBaseUrl: "https://api.z.ai/api/paas/v4",
|
||||
expected: {
|
||||
baseUrl: "https://api.z.ai/api/paas/v4",
|
||||
input: ["text", "image"],
|
||||
reasoning: true,
|
||||
contextWindow: 202800,
|
||||
maxTokens: 131100,
|
||||
},
|
||||
},
|
||||
] as const;
|
||||
|
||||
for (const testCase of cases) {
|
||||
const resolved = provider.resolveDynamicModel?.({
|
||||
provider: "zai",
|
||||
modelId: testCase.modelId,
|
||||
modelRegistry: {
|
||||
find: (_provider: string, modelId: string) => (modelId === "glm-4.7" ? template : null),
|
||||
},
|
||||
providerConfig: { baseUrl: testCase.providerBaseUrl },
|
||||
} as never) as Record<string, unknown> | undefined;
|
||||
expectModelFields(resolved, {
|
||||
provider: "zai",
|
||||
api: "openai-completions",
|
||||
id: testCase.modelId,
|
||||
...testCase.expected,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps selected Coding Plan and proxy endpoints for dynamic GLM-5 models", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
const template = createGlm47Template();
|
||||
|
||||
for (const baseUrl of [
|
||||
"https://open.bigmodel.cn/api/coding/paas/v4",
|
||||
"https://proxy.example.test/zai",
|
||||
]) {
|
||||
const resolved = provider.resolveDynamicModel?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5.2",
|
||||
modelRegistry: {
|
||||
find: (_provider: string, modelId: string) => (modelId === "glm-4.7" ? template : null),
|
||||
},
|
||||
providerConfig: { baseUrl },
|
||||
} as never) as Record<string, unknown> | undefined;
|
||||
expect(resolved?.baseUrl).toBe(baseUrl);
|
||||
}
|
||||
});
|
||||
|
||||
it("returns an already-registered GLM-5 variant as-is", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
const registered = {
|
||||
id: "glm-5-turbo",
|
||||
name: "GLM-5-Turbo",
|
||||
provider: "zai",
|
||||
api: "openai-completions",
|
||||
baseUrl: "https://api.z.ai/api/paas/v4",
|
||||
reasoning: false,
|
||||
input: ["text"],
|
||||
cost: { input: 0.1, output: 0.2, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 123456,
|
||||
maxTokens: 54321,
|
||||
};
|
||||
const template = createGlm47Template();
|
||||
|
||||
expect(
|
||||
provider.resolveDynamicModel?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5-turbo",
|
||||
modelRegistry: {
|
||||
find: (_provider: string, modelId: string) =>
|
||||
modelId === "glm-5-turbo" ? registered : modelId === "glm-4.7" ? template : null,
|
||||
},
|
||||
} as never),
|
||||
).toEqual(registered);
|
||||
});
|
||||
|
||||
it("falls back to manifest baseUrl when both providerConfig and template model are unavailable", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
|
||||
const resolved = provider.resolveDynamicModel?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5-turbo",
|
||||
modelRegistry: {
|
||||
find: () => null,
|
||||
},
|
||||
} as never) as Record<string, unknown> | undefined;
|
||||
expectModelFields(resolved, {
|
||||
id: "glm-5-turbo",
|
||||
provider: "zai",
|
||||
api: "openai-completions",
|
||||
baseUrl: "https://api.z.ai/api/paas/v4",
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
});
|
||||
});
|
||||
|
||||
it("still synthesizes unknown GLM-5 variants from the GLM-4.7 template", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
const template = createGlm47Template();
|
||||
|
||||
const resolved = provider.resolveDynamicModel?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5-turbo",
|
||||
modelRegistry: {
|
||||
find: (_provider: string, modelId: string) => (modelId === "glm-4.7" ? template : null),
|
||||
},
|
||||
} as never) as Record<string, unknown> | undefined;
|
||||
expectModelFields(resolved, {
|
||||
id: "glm-5-turbo",
|
||||
name: "GLM-5 Turbo",
|
||||
provider: "zai",
|
||||
api: "openai-completions",
|
||||
baseUrl: "https://api.z.ai/api/paas/v4",
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
});
|
||||
});
|
||||
|
||||
it("wires tool-stream defaults through the shared stream family hook", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
let capturedPayload: Record<string, unknown> | undefined;
|
||||
const baseStreamFn: StreamFn = (model, _context, options) => {
|
||||
const payload: Record<string, unknown> = {};
|
||||
options?.onPayload?.(payload as never, model as never);
|
||||
capturedPayload = payload;
|
||||
return {} as ReturnType<StreamFn>;
|
||||
};
|
||||
|
||||
const defaultWrapped = provider.wrapStreamFn?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5.1",
|
||||
extraParams: {},
|
||||
streamFn: baseStreamFn,
|
||||
} as never);
|
||||
|
||||
void defaultWrapped?.(
|
||||
{
|
||||
api: "openai-completions",
|
||||
provider: "zai",
|
||||
id: "glm-5.1",
|
||||
} as Model<"openai-completions">,
|
||||
{ messages: [] } as Context,
|
||||
{},
|
||||
);
|
||||
|
||||
expect(capturedPayload?.tool_stream).toBe(true);
|
||||
|
||||
const disabledWrapped = provider.wrapStreamFn?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5.1",
|
||||
extraParams: { tool_stream: false },
|
||||
streamFn: baseStreamFn,
|
||||
} as never);
|
||||
|
||||
void disabledWrapped?.(
|
||||
{
|
||||
api: "openai-completions",
|
||||
provider: "zai",
|
||||
id: "glm-5.1",
|
||||
} as Model<"openai-completions">,
|
||||
{ messages: [] } as Context,
|
||||
{},
|
||||
);
|
||||
|
||||
expect(capturedPayload).not.toHaveProperty("tool_stream");
|
||||
});
|
||||
|
||||
it("exposes full GLM-5.2 thinking levels while keeping older GLM models binary", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
|
||||
expect(
|
||||
provider.resolveThinkingProfile?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5.2",
|
||||
reasoning: true,
|
||||
} as never),
|
||||
).toEqual({
|
||||
levels: [
|
||||
{ id: "off", label: "off" },
|
||||
{ id: "low", label: "low" },
|
||||
{ id: "high", label: "high" },
|
||||
{ id: "max", label: "max" },
|
||||
],
|
||||
defaultLevel: "off",
|
||||
});
|
||||
|
||||
expect(
|
||||
provider.resolveThinkingProfile?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5.1",
|
||||
reasoning: true,
|
||||
} as never),
|
||||
).toEqual({
|
||||
levels: [
|
||||
{ id: "off", label: "off" },
|
||||
{ id: "low", label: "on" },
|
||||
],
|
||||
defaultLevel: "off",
|
||||
});
|
||||
});
|
||||
|
||||
it("maps thinking off to Z.AI thinking disabled", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
let capturedPayload: Record<string, unknown> | undefined;
|
||||
const baseStreamFn: StreamFn = (model, _context, options) => {
|
||||
const payload: Record<string, unknown> = {};
|
||||
options?.onPayload?.(payload as never, model as never);
|
||||
capturedPayload = payload;
|
||||
return {} as ReturnType<StreamFn>;
|
||||
};
|
||||
|
||||
const wrapped = provider.wrapStreamFn?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5.1",
|
||||
extraParams: {},
|
||||
thinkingLevel: "off",
|
||||
streamFn: baseStreamFn,
|
||||
} as never);
|
||||
|
||||
void wrapped?.(
|
||||
{
|
||||
api: "openai-completions",
|
||||
provider: "zai",
|
||||
id: "glm-5.1",
|
||||
} as Model<"openai-completions">,
|
||||
{ messages: [] } as Context,
|
||||
{},
|
||||
);
|
||||
|
||||
expect(capturedPayload?.tool_stream).toBe(true);
|
||||
expect(capturedPayload?.thinking).toEqual({ type: "disabled" });
|
||||
});
|
||||
|
||||
it("keeps minimal thinking enabled for binary GLM models", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
let capturedPayload: Record<string, unknown> | undefined;
|
||||
const baseStreamFn: StreamFn = (model, _context, options) => {
|
||||
const payload: Record<string, unknown> = {};
|
||||
options?.onPayload?.(payload as never, model as never);
|
||||
capturedPayload = payload;
|
||||
return {} as ReturnType<StreamFn>;
|
||||
};
|
||||
|
||||
const wrapped = provider.wrapStreamFn?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5.1",
|
||||
extraParams: {},
|
||||
thinkingLevel: "minimal",
|
||||
streamFn: baseStreamFn,
|
||||
} as never);
|
||||
|
||||
void wrapped?.(
|
||||
{
|
||||
api: "openai-completions",
|
||||
provider: "zai",
|
||||
id: "glm-5.1",
|
||||
} as Model<"openai-completions">,
|
||||
{ messages: [] } as Context,
|
||||
{},
|
||||
);
|
||||
|
||||
expect(capturedPayload).not.toHaveProperty("thinking");
|
||||
});
|
||||
|
||||
it("maps GLM-5.2 thinking levels to Z.AI reasoning effort", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
const baseStreamFn: StreamFn = (model, _context, options) => {
|
||||
const payload: Record<string, unknown> = {};
|
||||
options?.onPayload?.(payload as never, model as never);
|
||||
return { payload } as never;
|
||||
};
|
||||
|
||||
for (const [thinkingLevel, expectedEffort] of [
|
||||
["low", "high"],
|
||||
["high", "high"],
|
||||
["max", "max"],
|
||||
] as const) {
|
||||
const wrapped = provider.wrapStreamFn?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5.2",
|
||||
extraParams: {},
|
||||
thinkingLevel,
|
||||
streamFn: baseStreamFn,
|
||||
} as never);
|
||||
|
||||
const result = wrapped?.(
|
||||
{
|
||||
api: "openai-completions",
|
||||
provider: "zai",
|
||||
id: "glm-5.2",
|
||||
} as Model<"openai-completions">,
|
||||
{ messages: [] } as Context,
|
||||
{},
|
||||
) as unknown as { payload: Record<string, unknown> };
|
||||
|
||||
expect(result.payload.reasoning_effort).toBe(expectedEffort);
|
||||
expect(result.payload).not.toHaveProperty("thinking");
|
||||
expect(result.payload.tool_stream).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("enables Z.AI preserved thinking only when requested", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
let capturedPayload: Record<string, unknown> | undefined;
|
||||
const baseStreamFn: StreamFn = (model, _context, options) => {
|
||||
const payload: Record<string, unknown> = {};
|
||||
options?.onPayload?.(payload as never, model as never);
|
||||
capturedPayload = payload;
|
||||
return {} as ReturnType<StreamFn>;
|
||||
};
|
||||
|
||||
const wrappedWithoutPreserve = provider.wrapStreamFn?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5.1",
|
||||
extraParams: {},
|
||||
thinkingLevel: "low",
|
||||
streamFn: baseStreamFn,
|
||||
} as never);
|
||||
|
||||
void wrappedWithoutPreserve?.(
|
||||
{
|
||||
api: "openai-completions",
|
||||
provider: "zai",
|
||||
id: "glm-5.1",
|
||||
} as Model<"openai-completions">,
|
||||
{ messages: [] } as Context,
|
||||
{},
|
||||
);
|
||||
|
||||
expect(capturedPayload?.tool_stream).toBe(true);
|
||||
expect(capturedPayload).not.toHaveProperty("thinking");
|
||||
|
||||
const wrappedWithPreserve = provider.wrapStreamFn?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5.1",
|
||||
extraParams: { preserveThinking: true },
|
||||
thinkingLevel: "low",
|
||||
streamFn: baseStreamFn,
|
||||
} as never);
|
||||
|
||||
void wrappedWithPreserve?.(
|
||||
{
|
||||
api: "openai-completions",
|
||||
provider: "zai",
|
||||
id: "glm-5.1",
|
||||
} as Model<"openai-completions">,
|
||||
{ messages: [] } as Context,
|
||||
{},
|
||||
);
|
||||
|
||||
expect(capturedPayload?.tool_stream).toBe(true);
|
||||
expect(capturedPayload?.thinking).toEqual({ type: "enabled", clear_thinking: false });
|
||||
});
|
||||
|
||||
it("preserves replayed reasoning_content for Z.AI preserved thinking", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
let capturedPayload: Record<string, unknown> | undefined;
|
||||
const model = {
|
||||
provider: "zai",
|
||||
id: "glm-5.1",
|
||||
name: "GLM 5.1",
|
||||
api: "openai-completions",
|
||||
baseUrl: "https://api.z.ai/api/paas/v4",
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 200_000,
|
||||
maxTokens: 131_072,
|
||||
} as Model<"openai-completions">;
|
||||
const context = {
|
||||
messages: [
|
||||
{ role: "user", content: "hi", timestamp: 1 },
|
||||
{
|
||||
role: "assistant",
|
||||
api: "openai-completions",
|
||||
provider: "zai",
|
||||
model: "glm-5.1",
|
||||
content: [
|
||||
{
|
||||
type: "thinking",
|
||||
thinking: "prior reasoning",
|
||||
thinkingSignature: "reasoning_content",
|
||||
},
|
||||
{ type: "text", text: "visible reply" },
|
||||
],
|
||||
usage: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
totalTokens: 0,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
||||
},
|
||||
stopReason: "stop",
|
||||
timestamp: 2,
|
||||
},
|
||||
{ role: "user", content: "continue", timestamp: 3 },
|
||||
],
|
||||
} as Context;
|
||||
const baseStreamFn: StreamFn = (streamModel, streamContext, options) => {
|
||||
const payload = buildOpenAICompletionsParams(streamModel as never, streamContext, {
|
||||
reasoning: "high",
|
||||
} as never);
|
||||
options?.onPayload?.(payload as never, streamModel as never);
|
||||
capturedPayload = payload;
|
||||
return {} as ReturnType<StreamFn>;
|
||||
};
|
||||
|
||||
const wrapped = provider.wrapStreamFn?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-5.1",
|
||||
extraParams: { preserve_thinking: true },
|
||||
thinkingLevel: "low",
|
||||
streamFn: baseStreamFn,
|
||||
} as never);
|
||||
|
||||
void wrapped?.(model, context, {});
|
||||
|
||||
expect(capturedPayload?.thinking).toEqual({ type: "enabled", clear_thinking: false });
|
||||
const assistantMessage = (capturedPayload!.messages as Array<Record<string, unknown>>)[1];
|
||||
expect(assistantMessage?.role).toBe("assistant");
|
||||
expect(assistantMessage?.content).toBe("visible reply");
|
||||
expect(assistantMessage?.reasoning_content).toBe("prior reasoning");
|
||||
});
|
||||
|
||||
it("defaults tool_stream extra params but preserves explicit values", async () => {
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
|
||||
expect(
|
||||
provider.prepareExtraParams?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-4.7",
|
||||
extraParams: { endpoint: "global" },
|
||||
} as never),
|
||||
).toEqual({
|
||||
endpoint: "global",
|
||||
tool_stream: true,
|
||||
});
|
||||
|
||||
const explicit = { endpoint: "global", tool_stream: false };
|
||||
expect(
|
||||
provider.prepareExtraParams?.({
|
||||
provider: "zai",
|
||||
modelId: "glm-4.7",
|
||||
extraParams: explicit,
|
||||
} as never),
|
||||
).toBe(explicit);
|
||||
});
|
||||
|
||||
it("uses deprecated pi agent auth.json for usage auth when modern sources are empty", async () => {
|
||||
const home = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-zai-legacy-auth-"));
|
||||
try {
|
||||
const authDir = path.join(home, ".pi", "agent");
|
||||
await fs.mkdir(authDir, { recursive: true });
|
||||
await fs.writeFile(
|
||||
path.join(authDir, "auth.json"),
|
||||
`${JSON.stringify({ "z-ai": { access: "legacy-zai-token" } }, null, 2)}\n`,
|
||||
"utf-8",
|
||||
);
|
||||
const provider = await registerSingleProviderPlugin(plugin);
|
||||
|
||||
await expect(
|
||||
provider.resolveUsageAuth?.({
|
||||
env: { HOME: home },
|
||||
resolveApiKeyFromConfigAndStore: () => undefined,
|
||||
} as never),
|
||||
).resolves.toEqual({ token: "legacy-zai-token" });
|
||||
} finally {
|
||||
await fs.rm(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user