UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 8.27 kB
import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{a as AgentHarnessSessionSupersededError,i as AgentHarnessSessionCleanupError,n as AgentHarnessPreflightError}from"./errors-BBEItQ0m.mjs";import{F as wrapNativeSessionDeletionMutation}from"./session-accessor.sqlite-entry-store-Cxqr6m6F.mjs";import{t as abortAndDrainEmbeddedAgentRun}from"./runs-CZphmRD3.mjs";import{v as toolPolicy}from"./agent-harness-runtime-BNh6LFIo.mjs";import{c as combinePreparedNativeSessionBindingAuthority,o as retainNativeSessionGenerationAuthority,u as createNativeSessionBindingScope}from"./agent-harness-session-runtime-Di8eP1ES.mjs";import{a as resolveApiKeyForProvider}from"./provider-auth-runtime-NQRl8Lp4.mjs";import{t as AgentsApiClient}from"./agentsapi-client-pd5HyeyZ.mjs";import{t as requireAgentsApiSessionTarget}from"./agentsapi-target-gO4Gy3m-.mjs";import{t as runAgentsApiAttempt}from"./agentsapi-attempt-6r1GuNlN.mjs";import{t as migrateAgentsApiBinding}from"./agentsapi-binding-migration-ADvNP7k9.mjs";import{t as createAgentsApiBindings}from"./agentsapi-bindings-DZiwLic0.mjs";import{t as runAgentsApiIsolatedCompletion}from"./agentsapi-isolated-completion-CcQu2b7N.mjs"; //!extensions/agentsapi/agentsapi-harness.ts const AGENTS_API_NATIVE_TOOL_REQUIREMENTS=[`exec`,`process`,`read`,`write`,`edit`,`apply_patch`,`web_search`];function createAgentsApiHarness(e){let t=false;let n=false;const r=/* @__PURE__ */ new Map;const i=/* @__PURE__ */ new Map;const a=/* @__PURE__ */ new WeakMap;const o=/* @__PURE__ */ new Map;let s;const getBindings=()=>s??=createAgentsApiBindings(e,{settle:async(t,n,r,i)=>{let a=o.get(t);if(a?.nativeSessionId!==n.sessionId||a.configFingerprint!==n.configFingerprint){assertCurrent();r();const o=e.config.current({clone:true});const s=i;if(!s)throw new Error(`Agents API session cleanup requires its owning agent`);const c=await resolveApiKeyForProvider({provider:`openai`,cfg:o,agentDir:e.agent.resolveAgentDir(o,s),workspaceDir:e.agent.resolveAgentWorkspaceDir(o,s),signal:AbortSignal.timeout(3e4)});assertCurrent();r();if(c.mode!==`api-key`||!c.apiKey?.trim())throw new Error(`Agents API session cleanup requires an OpenAI API key; restore API-key authentication and retry reset or deletion`);a=prepareNativeCleanup(t,n,c.apiKey)}try{await a.settle(r)}catch(e){o.delete(t);throw e}}});const assertCurrent=()=>{if(t)throw new Error(`Agents API harness is disposed`)};return{id:`agentsapi`,label:`OpenAI Agents API (MVP)`,autoSelection:{providerIds:[]},contextManagement:`native`,deliveryDefaults:{visibleReplies:`automatic`},conversationToolPolicySupport:`exact`,conversationToolPolicyNativeTools:AGENTS_API_NATIVE_TOOL_REQUIREMENTS,conversationToolPolicySafeDenyTools:[`gateway`,`agents_list`,`openclaw`,`session_status`,`progress_card`,`automations`,`message`,`sessions_send`,`conversations_list`,`conversations_send`,`conversations_turn`,`subagents`,`sessions_list`,`sessions_history`,`sessions_search`,`sessions_spawn`],supports:e=>{if(e.provider!==`openai`)return{supported:false,reason:`Agents API requires the OpenAI provider`};if(e.modelProvider?.preparedAuth?.requirement===`subscription`||e.modelProvider?.api&&e.modelProvider.api!==`openai-responses`||e.modelProvider?.requestTransportOverrides===`present`||e.modelProvider?.baseUrl&&e.modelProvider.baseUrl!==`https://api.openai.com/v1`)return{supported:false,reason:`Agents API MVP requires the official API-key route`};return{supported:true}},runIsolatedCompletionV2:e=>{assertCurrent();if(n)throw new Error(`Agents API harness is closing`);const t=new AbortController;const r=runAgentsApiIsolatedCompletion({...e,abortSignal:e.abortSignal?AbortSignal.any([e.abortSignal,t.signal]):t.signal},assertCurrent);i.set(t,r);return r.finally(()=>i.delete(t))},runAttempt:async t=>{try{var i=_usingCtx();assertCurrent();if(n)throw new Error(`Agents API harness is closing`);const o=validateAgentsApiInput(t);const s=await retainNativeSessionGenerationAuthority({target:o,config:t.config,storePath:o.storePath,assertCurrent:()=>{assertCurrent();t.hostCapabilities.assertActive()},createSupersededError:e=>new AgentHarnessSessionSupersededError(`Agents API session generation is no longer current: ${e}`)});i.u({[Symbol.dispose]:s.release});const c=s.authority;c.assertLegacyCurrent();r.set(t.sessionId,(r.get(t.sessionId)??0)+1);try{return await getBindings().withSession(t.sessionId,c.assertLegacyCurrent,(r,i,s)=>{if(n)throw new Error(`Agents API harness is closing`);return runAgentsApiAttempt(t,r,i,combinePreparedNativeSessionBindingAuthority(c,createNativeSessionBindingScope(s)).assertLegacyCurrent,()=>{assertCurrent();s()},o,()=>e.config.current().plugins?.entries?.agentsapi?.config,a,(e,n)=>prepareNativeCleanup(t.sessionId,e,n))},(n,r)=>migrateAgentsApiBinding(n,t,()=>e.config.current().plugins?.entries?.agentsapi?.config,r))}finally{const e=r.get(t.sessionId)-1;if(e>0)r.set(t.sessionId,e);else r.delete(t.sessionId)}}catch(e){i.e=e}finally{i.d()}},reset:async e=>{try{assertCurrent();if(e.sessionId){if(r.has(e.sessionId)||o.has(e.sessionId)){await drainForNativeCleanup(e.sessionId);assertCurrent()}await getBindings().reset(e.sessionId,assertCurrent,e.agentId);o.delete(e.sessionId)}}catch(e){throw new AgentHarnessSessionCleanupError(formatErrorMessage(e),{cause:e})}},withSessionDeletion,withSessionContextReset:async(e,t)=>{e.assertCurrent();assertCurrent();const n=await getBindings().resolveContextResetSessionId(e.sessionId,e.previousSessionId);return withSessionDeletion({...e,sessionId:n},t)},dispose:async()=>{n=true;for(const e of i.keys())e.abort();await Promise.allSettled(i.values());await Promise.all([...r.keys()].map(e=>abortAndDrainEmbeddedAgentRun({sessionId:e,settleMs:95e3})));if(s)await s.withExclusiveMutationFence(async()=>{t=true});else t=true;o.clear()}};async function withSessionDeletion(e,t){e.assertCurrent();assertCurrent();if(r.has(e.sessionId)||o.has(e.sessionId)){await drainForNativeCleanup(e.sessionId);e.assertCurrent();assertCurrent()}let n=false;try{return await getBindings().withSessionDeletion({...e,assertCurrent:()=>{e.assertCurrent();assertCurrent()}},r=>t(wrapNativeSessionDeletionMutation(r,{assertCurrent:()=>{e.assertCurrent();assertCurrent()},committed:()=>{n=true},rolledBack:()=>{n=false}})))}finally{if(n)o.delete(e.sessionId)}}async function drainForNativeCleanup(e){const t=await abortAndDrainEmbeddedAgentRun({sessionId:e,settleMs:95e3});assertCurrent();if(t.aborted&&!t.drained||r.has(e))throw new Error(`Agents API session is still settling; retry reset or deletion after its active turn finishes`)}function prepareNativeCleanup(e,t,n){assertCurrent();const r={nativeSessionId:t.sessionId,configFingerprint:t.configFingerprint,settle:async e=>{const assertCleanup=()=>{assertCurrent();e()};assertCleanup();const r=new AgentsApiClient(n,assertCleanup);const i=AbortSignal.timeout(3e4);const a=await r.session(t.sessionId,i);assertCleanup();if(a.status!==`idle`&&a.status!==`failed`){await r.cancel(t.sessionId,i);assertCleanup()}}};o.set(e,r);return r}}function validateAgentsApiInput(e){const t=toolPolicy.createToolPolicyMatcher({allow:e.toolsAllow});if(e.disableTools||e.pluginHarnessToolPolicyRestricted||e.toolExecutionAllow!==void 0||e.toolsAllow?.length===0||AGENTS_API_NATIVE_TOOL_REQUIREMENTS.some(e=>!t(e)))throw new AgentHarnessPreflightError(`Agents API cannot enforce this run's restrictions on native shell, file, or web-search tools.`,{scope:`harness`,userMessage:`Agents API cannot run with this chat's tool restrictions because it cannot enforce them on native tools. Choose a harness that supports these restrictions or update the tool settings.`});const n=requireAgentsApiSessionTarget(e);if(!e.resolvedApiKey)throw new Error(`Agents API MVP requires an OpenAI API key`);if(e.sandbox)throw new AgentHarnessPreflightError(`Agents API does not support Gateway sandbox placement.`,{scope:`harness`,userMessage:`Agents API cannot run in the configured Gateway sandbox. Choose a harness that supports Gateway sandbox placement before retrying.`});if(e.contextEngine&&e.contextEngine.info.id!==`legacy`)throw new Error(`Agents API MVP currently supports only the default legacy context engine`);return n}export{createAgentsApiHarness as t};