test: optimize redundant suites for faster runtime

This commit is contained in:
Peter Steinberger
2026-02-23 13:57:34 +00:00
parent 5196565f19
commit 3f03cdea56
7 changed files with 288 additions and 309 deletions

View File

@@ -30,10 +30,6 @@ import {
} from "../media/input-files.js";
import { defaultRuntime } from "../runtime.js";
import { resolveAssistantStreamDeltaText } from "./agent-event-assistant-text.js";
import {
buildAgentMessageFromConversationEntries,
type ConversationEntry,
} from "./agent-prompt.js";
import type { AuthRateLimiter } from "./auth-rate-limit.js";
import type { ResolvedGatewayAuth } from "./auth.js";
import { sendJson, setSseHeaders, writeDone } from "./http-common.js";
@@ -41,14 +37,13 @@ import { handleGatewayPostJsonEndpoint } from "./http-endpoint-helpers.js";
import { resolveAgentIdForRequest, resolveSessionKey } from "./http-utils.js";
import {
CreateResponseBodySchema,
type ContentPart,
type CreateResponseBody,
type ItemParam,
type OutputItem,
type ResponseResource,
type StreamingEvent,
type Usage,
} from "./open-responses.schema.js";
import { buildAgentPrompt } from "./openresponses-prompt.js";
type OpenResponsesHttpOptions = {
auth: ResolvedGatewayAuth;
@@ -67,24 +62,6 @@ function writeSseEvent(res: ServerResponse, event: StreamingEvent) {
res.write(`data: ${JSON.stringify(event)}\n\n`);
}
function extractTextContent(content: string | ContentPart[]): string {
if (typeof content === "string") {
return content;
}
return content
.map((part) => {
if (part.type === "input_text") {
return part.text;
}
if (part.type === "output_text") {
return part.text;
}
return "";
})
.filter(Boolean)
.join("\n");
}
type ResolvedResponsesLimits = {
maxBodyBytes: number;
maxUrlParts: number;
@@ -172,52 +149,7 @@ function applyToolChoice(params: {
return { tools };
}
export function buildAgentPrompt(input: string | ItemParam[]): {
message: string;
extraSystemPrompt?: string;
} {
if (typeof input === "string") {
return { message: input };
}
const systemParts: string[] = [];
const conversationEntries: ConversationEntry[] = [];
for (const item of input) {
if (item.type === "message") {
const content = extractTextContent(item.content).trim();
if (!content) {
continue;
}
if (item.role === "system" || item.role === "developer") {
systemParts.push(content);
continue;
}
const normalizedRole = item.role === "assistant" ? "assistant" : "user";
const sender = normalizedRole === "assistant" ? "Assistant" : "User";
conversationEntries.push({
role: normalizedRole,
entry: { sender, body: content },
});
} else if (item.type === "function_call_output") {
conversationEntries.push({
role: "tool",
entry: { sender: `Tool:${item.call_id}`, body: item.output },
});
}
// Skip reasoning and item_reference for prompt building (Phase 1)
}
const message = buildAgentMessageFromConversationEntries(conversationEntries);
return {
message,
extraSystemPrompt: systemParts.length > 0 ? systemParts.join("\n\n") : undefined,
};
}
export { buildAgentPrompt } from "./openresponses-prompt.js";
function resolveOpenResponsesSessionKey(params: {
req: IncomingMessage;

View File

@@ -12,7 +12,7 @@ let InputFileContentPartSchema: typeof import("./open-responses.schema.js").Inpu
let ToolDefinitionSchema: typeof import("./open-responses.schema.js").ToolDefinitionSchema;
let CreateResponseBodySchema: typeof import("./open-responses.schema.js").CreateResponseBodySchema;
let OutputItemSchema: typeof import("./open-responses.schema.js").OutputItemSchema;
let buildAgentPrompt: typeof import("./openresponses-http.js").buildAgentPrompt;
let buildAgentPrompt: typeof import("./openresponses-prompt.js").buildAgentPrompt;
describe("OpenResponses Feature Parity", () => {
beforeAll(async () => {
@@ -23,7 +23,7 @@ describe("OpenResponses Feature Parity", () => {
CreateResponseBodySchema,
OutputItemSchema,
} = await import("./open-responses.schema.js"));
({ buildAgentPrompt } = await import("./openresponses-http.js"));
({ buildAgentPrompt } = await import("./openresponses-prompt.js"));
});
describe("Schema Validation", () => {

View File

@@ -0,0 +1,70 @@
import {
buildAgentMessageFromConversationEntries,
type ConversationEntry,
} from "./agent-prompt.js";
import type { ContentPart, ItemParam } from "./open-responses.schema.js";
function extractTextContent(content: string | ContentPart[]): string {
if (typeof content === "string") {
return content;
}
return content
.map((part) => {
if (part.type === "input_text") {
return part.text;
}
if (part.type === "output_text") {
return part.text;
}
return "";
})
.filter(Boolean)
.join("\n");
}
export function buildAgentPrompt(input: string | ItemParam[]): {
message: string;
extraSystemPrompt?: string;
} {
if (typeof input === "string") {
return { message: input };
}
const systemParts: string[] = [];
const conversationEntries: ConversationEntry[] = [];
for (const item of input) {
if (item.type === "message") {
const content = extractTextContent(item.content).trim();
if (!content) {
continue;
}
if (item.role === "system" || item.role === "developer") {
systemParts.push(content);
continue;
}
const normalizedRole = item.role === "assistant" ? "assistant" : "user";
const sender = normalizedRole === "assistant" ? "Assistant" : "User";
conversationEntries.push({
role: normalizedRole,
entry: { sender, body: content },
});
} else if (item.type === "function_call_output") {
conversationEntries.push({
role: "tool",
entry: { sender: `Tool:${item.call_id}`, body: item.output },
});
}
// Skip reasoning and item_reference for prompt building (Phase 1)
}
const message = buildAgentMessageFromConversationEntries(conversationEntries);
return {
message,
extraSystemPrompt: systemParts.length > 0 ? systemParts.join("\n\n") : undefined,
};
}