mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-11 07:00:48 +08:00
同时收敛启动与运维脚本默认行为(含 wiki worker)、更新 Admin 可观测性与相关测试,降低首轮时延并提高运行稳定性。 Made-with: Cursor
208 lines
6 KiB
TypeScript
208 lines
6 KiB
TypeScript
import type { StreamFn } from "@mariozechner/pi-agent-core";
|
|
import type { Context, Model } from "@mariozechner/pi-ai";
|
|
import { describe, expect, it } from "vitest";
|
|
import { registerSingleProviderPlugin } from "../../test/helpers/plugins/plugin-registration.js";
|
|
import plugin from "./index.js";
|
|
|
|
function createProviderModel(overrides: {
|
|
id: string;
|
|
api?: string;
|
|
baseUrl?: string;
|
|
provider?: string;
|
|
}) {
|
|
return {
|
|
id: overrides.id,
|
|
name: overrides.id,
|
|
api: overrides.api ?? "openai-completions",
|
|
provider: overrides.provider ?? "xai",
|
|
baseUrl: overrides.baseUrl ?? "https://api.x.ai/v1",
|
|
reasoning: true,
|
|
input: ["text"],
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
contextWindow: 200_000,
|
|
maxTokens: 8_192,
|
|
};
|
|
}
|
|
|
|
describe("xai provider plugin", () => {
|
|
it("owns replay policy for xAI OpenAI-compatible transports", async () => {
|
|
const provider = await registerSingleProviderPlugin(plugin);
|
|
|
|
expect(
|
|
provider.buildReplayPolicy?.({
|
|
provider: "xai",
|
|
modelApi: "openai-completions",
|
|
modelId: "grok-3",
|
|
} as never),
|
|
).toMatchObject({
|
|
sanitizeToolCallIds: true,
|
|
toolCallIdMode: "strict",
|
|
applyAssistantFirstOrderingFix: true,
|
|
validateGeminiTurns: true,
|
|
validateAnthropicTurns: true,
|
|
});
|
|
|
|
expect(
|
|
provider.buildReplayPolicy?.({
|
|
provider: "xai",
|
|
modelApi: "openai-responses",
|
|
modelId: "grok-4-fast",
|
|
} as never),
|
|
).toMatchObject({
|
|
sanitizeToolCallIds: true,
|
|
toolCallIdMode: "strict",
|
|
applyAssistantFirstOrderingFix: false,
|
|
validateGeminiTurns: false,
|
|
validateAnthropicTurns: false,
|
|
});
|
|
});
|
|
|
|
it("wires provider stream shaping for fast mode and tool-stream defaults", async () => {
|
|
const provider = await registerSingleProviderPlugin(plugin);
|
|
let capturedModelId = "";
|
|
let capturedPayload: Record<string, unknown> | undefined;
|
|
const baseStreamFn: StreamFn = (model, _context, options) => {
|
|
capturedModelId = model.id;
|
|
const payload: Record<string, unknown> = {
|
|
reasoning: { effort: "high" },
|
|
tools: [
|
|
{
|
|
type: "function",
|
|
function: {
|
|
name: "write",
|
|
parameters: { type: "object", properties: {} },
|
|
strict: true,
|
|
},
|
|
},
|
|
],
|
|
};
|
|
options?.onPayload?.(payload as never, model as never);
|
|
capturedPayload = payload;
|
|
return {
|
|
result: async () => ({}) as never,
|
|
async *[Symbol.asyncIterator]() {},
|
|
} as unknown as ReturnType<StreamFn>;
|
|
};
|
|
|
|
const wrapped = provider.wrapStreamFn?.({
|
|
provider: "xai",
|
|
modelId: "grok-4",
|
|
extraParams: { fastMode: true },
|
|
streamFn: baseStreamFn,
|
|
} as never);
|
|
|
|
void wrapped?.(
|
|
{
|
|
api: "openai-responses",
|
|
provider: "xai",
|
|
id: "grok-4",
|
|
} as Model<"openai-responses">,
|
|
{ messages: [] } as Context,
|
|
{},
|
|
);
|
|
|
|
expect(capturedModelId).toBe("grok-4-fast");
|
|
expect(capturedPayload).toMatchObject({ tool_stream: true });
|
|
expect(capturedPayload).not.toHaveProperty("reasoning");
|
|
expect(
|
|
(capturedPayload?.tools as Array<{ function?: Record<string, unknown> }>)[0]?.function,
|
|
).not.toHaveProperty("strict");
|
|
});
|
|
|
|
it("defaults tool_stream extra params but preserves explicit values", async () => {
|
|
const provider = await registerSingleProviderPlugin(plugin);
|
|
|
|
expect(
|
|
provider.prepareExtraParams?.({
|
|
provider: "xai",
|
|
modelId: "grok-4",
|
|
extraParams: { fastMode: true },
|
|
} as never),
|
|
).toEqual({
|
|
fastMode: true,
|
|
tool_stream: true,
|
|
});
|
|
|
|
const explicit = { fastMode: true, tool_stream: false };
|
|
expect(
|
|
provider.prepareExtraParams?.({
|
|
provider: "xai",
|
|
modelId: "grok-4",
|
|
extraParams: explicit,
|
|
} as never),
|
|
).toBe(explicit);
|
|
});
|
|
|
|
it("owns forward-compatible Grok model resolution", async () => {
|
|
const provider = await registerSingleProviderPlugin(plugin);
|
|
|
|
expect(
|
|
provider.resolveDynamicModel?.({
|
|
provider: "xai",
|
|
modelId: "grok-4-1-fast-reasoning",
|
|
modelRegistry: { find: () => null } as never,
|
|
providerConfig: {
|
|
api: "openai-completions",
|
|
baseUrl: "https://api.x.ai/v1",
|
|
},
|
|
} as never),
|
|
).toMatchObject({
|
|
id: "grok-4-1-fast-reasoning",
|
|
provider: "xai",
|
|
api: "openai-completions",
|
|
baseUrl: "https://api.x.ai/v1",
|
|
reasoning: true,
|
|
contextWindow: 2_000_000,
|
|
});
|
|
});
|
|
|
|
it("marks modern Grok refs without accepting multi-agent ids", async () => {
|
|
const provider = await registerSingleProviderPlugin(plugin);
|
|
|
|
expect(
|
|
provider.isModernModelRef?.({
|
|
provider: "xai",
|
|
modelId: "grok-4-1-fast-reasoning",
|
|
} as never),
|
|
).toBe(true);
|
|
expect(
|
|
provider.isModernModelRef?.({
|
|
provider: "xai",
|
|
modelId: "grok-4.20-multi-agent-experimental-beta-0304",
|
|
} as never),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("owns xai compat flags for direct and downstream routed models", async () => {
|
|
const provider = await registerSingleProviderPlugin(plugin);
|
|
|
|
expect(
|
|
provider.normalizeResolvedModel?.({
|
|
provider: "xai",
|
|
modelId: "grok-4-1-fast",
|
|
model: createProviderModel({ id: "grok-4-1-fast" }),
|
|
} as never),
|
|
).toMatchObject({
|
|
compat: {
|
|
toolSchemaProfile: "xai",
|
|
nativeWebSearchTool: true,
|
|
toolCallArgumentsEncoding: "html-entities",
|
|
},
|
|
});
|
|
expect(
|
|
provider.contributeResolvedModelCompat?.({
|
|
provider: "openrouter",
|
|
modelId: "x-ai/grok-4-1-fast",
|
|
model: createProviderModel({
|
|
id: "x-ai/grok-4-1-fast",
|
|
provider: "openrouter",
|
|
baseUrl: "https://openrouter.ai/api/v1",
|
|
}),
|
|
} as never),
|
|
).toMatchObject({
|
|
toolSchemaProfile: "xai",
|
|
nativeWebSearchTool: true,
|
|
toolCallArgumentsEncoding: "html-entities",
|
|
});
|
|
});
|
|
});
|