refactor(xai): move bundled xai runtime into plugin

Co-authored-by: Harold Hunt <harold@pwrdrvr.com>
This commit is contained in:
Peter Steinberger
2026-03-28 05:01:59 +00:00
parent 85064256a2
commit c4e6fdf94d
25 changed files with 655 additions and 527 deletions

View File

@@ -1,6 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import type { StreamFn } from "@mariozechner/pi-agent-core";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import type { ProviderPlugin, ProviderRuntimeModel } from "../../../src/plugins/types.js";
import {
@@ -676,6 +677,68 @@ export function describeXAIProviderRuntimeContract() {
},
});
});
it("owns xai tool_stream defaults", () => {
const provider = requireProviderContractProvider("xai");
expect(
provider.prepareExtraParams?.({
provider: "xai",
modelId: "grok-4-1-fast-reasoning",
extraParams: { temperature: 0.2 },
}),
).toEqual({
temperature: 0.2,
tool_stream: true,
});
expect(
provider.prepareExtraParams?.({
provider: "xai",
modelId: "grok-4-1-fast-reasoning",
extraParams: { tool_stream: false },
}),
).toEqual({
tool_stream: false,
});
});
it("owns xai fast-mode model rewriting through the plugin stream hook", () => {
const provider = requireProviderContractProvider("xai");
let capturedModelId = "";
const baseStreamFn: StreamFn = (model) => {
capturedModelId = model.id;
return {
push() {},
async result() {
return undefined;
},
async *[Symbol.asyncIterator]() {
// Minimal async stream surface for xAI decode wrappers.
},
} as unknown as ReturnType<StreamFn>;
};
const streamFn = provider.wrapStreamFn?.({
provider: "xai",
modelId: "grok-4",
extraParams: { fastMode: true },
streamFn: baseStreamFn,
});
expect(streamFn).toBeTypeOf("function");
void streamFn?.(
createModel({
id: "grok-4",
provider: "xai",
api: "openai-completions",
baseUrl: "https://api.x.ai/v1",
}) as never,
{ messages: [] } as never,
{},
);
expect(capturedModelId).toBe("grok-4-fast");
});
});
}