Files
oh-my-pi/packages/ai/test/openai-codex.test.ts
T
can1357 8e3e0ebf9e feat: introduced Effort enum and ThinkingConfig for model-aware reasoning
- Introduced Effort enum and ThinkingConfig metadata for per-model reasoning capabilities with min/max effort levels.
- Migrated thinking level API from string-based ThinkingLevel to structured Effort enum across agent and AI packages.
- Added model-thinking module with effort mapping, policy application, and semantic versioning utilities for provider-specific thinking modes.
- Removed supportsXhigh() function and replaced effort clamping with model-aware validation using ThinkingConfig metadata.
- Expanded models.json with thinking configuration objects for 50+ models including Claude, Gemini, and OpenAI variants.
- Added Python analysis scripts for edit tool usage patterns and tool invocation stream processing.
2026-03-06 12:35:01 +01:00

110 lines
3.9 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { enrichModelThinking } from "@oh-my-pi/pi-ai/model-thinking";
import { type RequestBody, transformRequestBody } from "@oh-my-pi/pi-ai/providers/openai-codex/request-transformer";
import { parseCodexError } from "@oh-my-pi/pi-ai/providers/openai-codex/response-handler";
import type { Model } from "@oh-my-pi/pi-ai/types";
const DEFAULT_PROMPT_PREFIX =
"You are an expert coding assistant. You help users with coding tasks by reading files, executing commands";
function createCodexModel(id: string): Model<"openai-codex-responses"> {
return enrichModelThinking({
id,
name: id,
api: "openai-codex-responses",
provider: "openai-codex",
baseUrl: "https://api.openai.com/v1",
reasoning: true,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 272000,
maxTokens: 128000,
});
}
describe("openai-codex request transformer", () => {
it("filters item_reference and strips ids", async () => {
const body: RequestBody = {
model: "gpt-5.1-codex",
input: [
{
type: "message",
role: "developer",
id: "sys-1",
content: [{ type: "input_text", text: `${DEFAULT_PROMPT_PREFIX}...` }],
},
{
type: "message",
role: "user",
id: "user-1",
content: [{ type: "input_text", text: "hello" }],
},
{ type: "item_reference", id: "ref-1" },
{ type: "function_call_output", call_id: "missing", name: "tool", output: "result" },
],
tools: [{ type: "function", name: "tool", description: "", parameters: {} }],
};
const transformed = await transformRequestBody(body, createCodexModel(body.model), {});
expect(transformed.store).toBe(false);
expect(transformed.stream).toBe(true);
expect(transformed.include).toEqual(["reasoning.encrypted_content"]);
const input = transformed.input || [];
expect(input.some(item => item.type === "item_reference")).toBe(false);
expect(input.some(item => "id" in item)).toBe(false);
const first = input[0];
expect(first?.type).toBe("message");
expect(first?.role).toBe("developer");
expect(first?.content).toEqual([{ type: "input_text", text: `${DEFAULT_PROMPT_PREFIX}...` }]);
const orphaned = input.find(item => item.type === "message" && item.role === "assistant");
expect(orphaned?.content).toMatch(/Previous tool result/);
});
});
describe("openai-codex reasoning effort validation", () => {
it("rejects gpt-5.1 xhigh when metadata does not list it", async () => {
const body: RequestBody = { model: "gpt-5.1", input: [] };
await expect(
transformRequestBody(body, createCodexModel(body.model), { reasoningEffort: "xhigh" }),
).rejects.toThrow(/Supported efforts: minimal, low, medium, high/);
});
it("rejects unsupported Codex mini efforts instead of clamping", async () => {
const body: RequestBody = { model: "gpt-5.1-codex-mini", input: [] };
await expect(
transformRequestBody({ ...body }, createCodexModel(body.model), { reasoningEffort: "low" }),
).rejects.toThrow(/Supported efforts: medium, high/);
await expect(
transformRequestBody({ ...body }, createCodexModel(body.model), { reasoningEffort: "xhigh" }),
).rejects.toThrow(/Supported efforts: medium, high/);
});
});
describe("openai-codex error parsing", () => {
it("produces friendly usage-limit messages and rate limits", async () => {
const resetAt = Math.floor(Date.now() / 1000) + 600;
const response = new Response(
JSON.stringify({
error: { code: "usage_limit_reached", plan_type: "Plus", resets_at: resetAt },
}),
{
status: 429,
headers: {
"x-codex-primary-used-percent": "99",
"x-codex-primary-window-minutes": "60",
"x-codex-primary-reset-at": String(resetAt),
},
},
);
const info = await parseCodexError(response);
expect(info.friendlyMessage?.toLowerCase()).toContain("usage limit");
expect(info.rateLimits?.primary?.used_percent).toBe(99);
});
});