chore(vendor): import pi 1.3.0

Imported as a linear vendor commit so forge rebases do not replay the upstream root tree.

git-subtree-dir: third_party/pi

git-subtree-split: 71023aec1546d6d677a65669c5665c0927ce0dd0
This commit is contained in:
alexion committed 2026-10-15 17:58:48 -04:00
1 parent 719370c8f7
commit 6081e3e325
1743 files changed
+205459 -131704

No files matched your search

+66 -20
View File
@@ -1,9 +1,10 @@
import { arch, platform, release } from "node:os";
import type { ResponseStreamEvent } from "openai/resources/responses/responses.js";
import { Type } from "typebox";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { stream as streamAzureOpenAIResponses } from "../src/api/azure-openai-responses.ts";
import { getModel } from "../src/compat.ts";
import type { Context, Model } from "../src/types.ts";
import { getModel, normalizeContext } from "../src/compat.ts";
import type { Api, Context, Model } from "../src/types.ts";
interface CapturedAzureClientOptions {
apiKey: string;
@@ -22,6 +23,7 @@ interface CapturedAzureResponsesPayload {
const azureMock = vi.hoisted(() => ({
constructorCalls: [] as CapturedAzureClientOptions[],
lastParams: undefined as CapturedAzureResponsesPayload | undefined,
streamEvents: undefined as ResponseStreamEvent[] | undefined,
}));
vi.mock("openai", () => {
@@ -29,7 +31,16 @@ vi.mock("openai", () => {
responses = {
create: (params: CapturedAzureResponsesPayload) => {
azureMock.lastParams = params;
throw new Error("mock create");
const events = azureMock.streamEvents;
if (!events) throw new Error("mock create");
return {
withResponse: async () => ({
data: (async function* () {
for (const event of events) yield event;
})(),
response: { status: 200, headers: new Headers() },
}),
};
},
};
@@ -55,6 +66,7 @@ const originalAzureOpenAIApiKey = process.env.AZURE_OPENAI_API_KEY;
beforeEach(() => {
azureMock.constructorCalls.length = 0;
azureMock.lastParams = undefined;
azureMock.streamEvents = undefined;
delete process.env.AZURE_OPENAI_BASE_URL;
delete process.env.AZURE_OPENAI_RESOURCE_NAME;
delete process.env.AZURE_OPENAI_API_VERSION;
@@ -89,15 +101,15 @@ afterEach(() => {
async function captureClientBaseUrl(baseUrl: string): Promise<string> {
process.env.AZURE_OPENAI_BASE_URL = baseUrl;
const model = getModel("azure-openai-responses", "gpt-4o-mini");
await streamAzureOpenAIResponses(model, context, { apiKey: "test-api-key" }).result();
const model = getModel("azure", "gpt-4o-mini");
await streamAzureOpenAIResponses(model, normalizeContext(context), { apiKey: "test-api-key" }).result();
expect(azureMock.constructorCalls).toHaveLength(1);
return azureMock.constructorCalls[0].baseURL;
}
async function captureClientHeaders(headers?: Record<string, string>): Promise<Record<string, string>> {
const model = getModel("azure-openai-responses", "gpt-4o-mini");
await streamAzureOpenAIResponses(model, context, {
const model = getModel("azure", "gpt-4o-mini");
await streamAzureOpenAIResponses(model, normalizeContext(context), {
apiKey: "test-api-key",
azureBaseUrl: "https://my-resource.openai.azure.com",
headers,
@@ -106,7 +118,7 @@ async function captureClientHeaders(headers?: Record<string, string>): Promise<R
return azureMock.constructorCalls[0].defaultHeaders ?? {};
}
describe("azure-openai-responses base URL normalization", () => {
describe("azure base URL normalization", () => {
it("normalizes Cognitive Services root endpoints to /openai/v1", async () => {
const baseURL = await captureClientBaseUrl("https://marc-quicktests-resource.cognitiveservices.azure.com");
expect(baseURL).toBe("https://marc-quicktests-resource.cognitiveservices.azure.com/openai/v1");
@@ -154,15 +166,17 @@ describe("azure-openai-responses base URL normalization", () => {
it("throws on invalid URLs", async () => {
process.env.AZURE_OPENAI_BASE_URL = "not-a-url";
const model = getModel("azure-openai-responses", "gpt-4o-mini");
const result = await streamAzureOpenAIResponses(model, context, { apiKey: "test-api-key" }).result();
const model = getModel("azure", "gpt-4o-mini");
const result = await streamAzureOpenAIResponses(model, normalizeContext(context), {
apiKey: "test-api-key",
}).result();
expect(result.stopReason).toBe("error");
expect(result.errorMessage).toContain("Invalid Azure OpenAI base URL");
});
it("clamps prompt_cache_key to OpenAI's 64-character limit", async () => {
const model = getModel("azure-openai-responses", "gpt-4o-mini");
await streamAzureOpenAIResponses(model, context, {
const model = getModel("azure", "gpt-4o-mini");
await streamAzureOpenAIResponses(model, normalizeContext(context), {
apiKey: "test-api-key",
azureBaseUrl: "https://my-resource.openai.azure.com",
sessionId: "x".repeat(67),
@@ -172,8 +186,8 @@ describe("azure-openai-responses base URL normalization", () => {
});
it("disables server-side response storage", async () => {
const model = getModel("azure-openai-responses", "gpt-4o-mini");
await streamAzureOpenAIResponses(model, context, {
const model = getModel("azure", "gpt-4o-mini");
await streamAzureOpenAIResponses(model, normalizeContext(context), {
apiKey: "test-api-key",
azureBaseUrl: "https://my-resource.openai.azure.com",
}).result();
@@ -182,7 +196,7 @@ describe("azure-openai-responses base URL normalization", () => {
});
it("honors supportsStrictMode: false", async () => {
const baseModel = getModel("azure-openai-responses", "gpt-4o-mini");
const baseModel = getModel("azure", "gpt-4o-mini");
const model: Model<"azure-openai-responses"> = {
...baseModel,
compat: { ...baseModel.compat, supportsStrictMode: false },
@@ -190,7 +204,7 @@ describe("azure-openai-responses base URL normalization", () => {
await streamAzureOpenAIResponses(
model,
{
normalizeContext({
...context,
tools: [
{
@@ -200,7 +214,7 @@ describe("azure-openai-responses base URL normalization", () => {
constrainedSampling: { type: "json_schema", strict: "prefer" },
},
],
},
}),
{ apiKey: "test-api-key", azureBaseUrl: "https://my-resource.openai.azure.com" },
).result();
@@ -209,14 +223,46 @@ describe("azure-openai-responses base URL normalization", () => {
it("builds correct default URL from AZURE_OPENAI_RESOURCE_NAME", async () => {
process.env.AZURE_OPENAI_RESOURCE_NAME = "my-resource";
const model = getModel("azure-openai-responses", "gpt-4o-mini");
await streamAzureOpenAIResponses(model, context, { apiKey: "test-api-key" }).result();
const model = getModel("azure", "gpt-4o-mini");
await streamAzureOpenAIResponses(model, normalizeContext(context), { apiKey: "test-api-key" }).result();
expect(azureMock.constructorCalls).toHaveLength(1);
expect(azureMock.constructorCalls[0].baseURL).toBe("https://my-resource.openai.azure.com/openai/v1");
});
});
describe("azure-openai-responses user agent", () => {
describe("azure provider stream events", () => {
it("forwards parsed events in order before normalizing the response", async () => {
azureMock.streamEvents = [
{ type: "response.created", sequence_number: 0, response: { id: "resp_azure" } } as ResponseStreamEvent,
{
type: "response.completed",
sequence_number: 1,
response: { id: "resp_azure", status: "completed" },
} as ResponseStreamEvent,
];
const model = getModel("azure", "gpt-4o-mini");
const received: unknown[] = [];
const eventModels: Model<Api>[] = [];
const result = await streamAzureOpenAIResponses(model, normalizeContext(context), {
apiKey: "test-api-key",
azureBaseUrl: "https://my-resource.openai.azure.com",
onProviderStreamEvent: async (event, eventModel) => {
await Promise.resolve();
received.push(event);
eventModels.push(eventModel);
},
}).result();
expect(received).toEqual(azureMock.streamEvents);
expect(received[0]).toBe(azureMock.streamEvents[0]);
expect(received[1]).toBe(azureMock.streamEvents[1]);
expect(eventModels).toEqual([model, model]);
expect(result.stopReason).toBe("stop");
expect(result.responseId).toBe("resp_azure");
});
});
describe("azure user agent", () => {
it("uses pi's User-Agent by default", async () => {
expect((await captureClientHeaders())["User-Agent"]).toBe(PI_USER_AGENT);
});