openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
89 lines (88 loc) • 4 kB
JavaScript
import { d as normalizeUsage } from "./usage-CHLnzpJG.mjs";
import "./agent-harness-runtime-BCklzcv7.mjs";
import { n as createAgentHarnessAssistantMessage } from "./agent-harness-attempt-runtime-DFFgKWgF.mjs";
import { t as AgentsApiClient } from "./agentsapi-client-BkGGOio6.mjs";
import { t as readAgentsApiFinalText } from "./agentsapi-text-BTHJuIKD.mjs";
import { t as aggregateAgentsApiUsage } from "./agentsapi-usage-DUzNo9KK.mjs";
import { t as resolveAgentsApiReasoningEffort } from "./agentsapi-reasoning-DM_CoYmp.mjs";
import { t as createAgentsApiSession } from "./agentsapi-session-DT7X3oBR.mjs";
//#region extensions/agentsapi/agentsapi-isolated-completion.ts
/** Fresh native inference without an executor, supplied functions, or attached credentials. */
async function runAgentsApiIsolatedCompletion(e, t) {
t();
e.assertCurrent?.();
const n = e.authorization;
const r = n.owner === "host" ? n.auth.apiKey : void 0;
if (n.owner !== "host" || n.auth.mode !== "api-key" || !r) throw new Error("Agents API isolated completion requires the host-prepared API key");
const { model: i } = n;
if (e.provider !== "openai" || i.provider !== "openai" || i.api !== "openai-responses" || i.baseUrl !== "https://api.openai.com/v1" || Object.keys(i.headers ?? {}).length > 0) throw new Error("Agents API isolated completion requires the official OpenAI API-key route");
const a = AbortSignal.timeout(e.timeoutMs);
const o = e.abortSignal ? AbortSignal.any([e.abortSignal, a]) : a;
const assertCurrent = () => {
t();
e.assertCurrent?.();
o.throwIfAborted();
};
const s = new AgentsApiClient(r, assertCurrent);
const c = new AgentsApiClient(r, t);
const l = new AgentsApiClient(r, t, assertCurrent);
let u;
let d;
try {
assertCurrent();
const t = await l.createIsolated(AbortSignal.any([a, AbortSignal.timeout(6e4)]), e.systemPrompt, e.prompt, i.id, e.thinkLevel === void 0 ? {} : { effort: resolveAgentsApiReasoningEffort({
model: i,
thinkLevel: e.thinkLevel
}) });
u = t.id;
d = createAgentsApiSession({
client: s,
cleanupClient: c,
sessionId: u,
signal: o,
assertCurrent,
initialInputSubmitted: true,
onEvent: (e) => {
if (e.item) assertRestrictedItem(e.item);
}
});
assertCurrent();
if (t.environment.type !== "none" || t.agent.tools.some((e) => e.type !== "programmatic_tool_calling") || t.agent.multi_agent.enabled) throw new Error("Agents API did not create the requested restricted completion session");
const n = await d.run(e.prompt, async () => {}, () => {});
assertCurrent();
if (n.cancelled) throw new Error("Agents API isolated completion was cancelled");
const r = await s.items(u, n.turn.id, o);
for (const e of r) assertRestrictedItem(e);
const f = r.filter((e) => e.type === "message" && e.role === "user");
if (f.length !== 1 || f[0]?.content?.length !== 1 || f[0].content[0]?.text !== e.prompt) throw new Error("Agents API isolated completion returned unexpected input");
const p = await d.readUsageTurns();
assertCurrent();
const { usage: m, assistantUsage: h } = aggregateAgentsApiUsage(i, p.flatMap((e) => {
const t = normalizeUsage(e.usage);
return t ? [t] : [];
}));
const g = createAgentHarnessAssistantMessage({
api: "openai-agents",
provider: e.provider,
modelId: i.id
}, readAgentsApiFinalText(e.outputTextPolicy === "strict-visible" ? r.filter((e) => e.phase === "final_answer") : r), {
tokenUsage: m,
aborted: false
});
g.usage = h;
return { assistant: g };
} finally {
try {
await d?.close();
} finally {
if (u && (!d || !d.wasSubmitted() || d.isSettled())) await c.deleteSession(u, AbortSignal.timeout(3e4));
}
}
}
function assertRestrictedItem(e) {
if (e.type === "reasoning") return;
if (e.type === "message" && (e.role === "user" || e.role === "assistant")) return;
throw new Error(`Agents API isolated completion returned unexpected native item: ${e.type}`);
}
//#endregion
export { runAgentsApiIsolatedCompletion as t };