UNPKG

@openai/agents-core

Version:

The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.

146 lines • 5.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rememberLivePreservedOwnedSessions = rememberLivePreservedOwnedSessions; exports.forgetLivePreservedOwnedSessions = forgetLivePreservedOwnedSessions; exports.hasRejectedLivePreservedOwnedSessions = hasRejectedLivePreservedOwnedSessions; exports.forgetLivePreservedOwnedSessionHandle = forgetLivePreservedOwnedSessionHandle; exports.rememberRejectedLivePreservedOwnedSessionHandle = rememberRejectedLivePreservedOwnedSessionHandle; exports.rejectLivePreservedOwnedSessionHandle = rejectLivePreservedOwnedSessionHandle; exports.livePreservedOwnedSessionEntries = livePreservedOwnedSessionEntries; exports.preservedOwnedSessionAgentKeysWithoutLiveReuse = preservedOwnedSessionAgentKeysWithoutLiveReuse; exports.livePreservedOwnedSession = livePreservedOwnedSession; const livePreservedOwnedSessionsByRunState = new WeakMap(); function rememberLivePreservedOwnedSessions(args) { const rejectedEntries = livePreservedOwnedSessionEntries(args.state).filter((entry) => entry.reuseRejected); const rejectedSessions = new Set(rejectedEntries.map((entry) => entry.session)); const liveSessions = new Set(rejectedEntries); for (const [agentKey, entry] of Object.entries(args.serializedState.sessionsByAgent ?? {})) { if (!entry.preservedOwnedSession) { continue; } const reuseRejected = entry.reuseLiveSession === false && entry.requiresFreshCreation === true; if (entry.reuseLiveSession === false && !reuseRejected) { continue; } const session = args.sessionsByAgentKey.get(agentKey); if (!session) { continue; } const existing = [...liveSessions].find((liveEntry) => liveEntry.agentKey === agentKey && liveEntry.session === session); if (existing) { existing.reuseRejected ||= rejectedSessions.has(session); continue; } liveSessions.add({ agentKey, backendId: entry.backendId, currentAgentName: entry.currentAgentName, session, ...(reuseRejected || rejectedSessions.has(session) ? { reuseRejected: true } : {}), }); } if (liveSessions.size > 0) { livePreservedOwnedSessionsByRunState.set(args.state, liveSessions); } else { livePreservedOwnedSessionsByRunState.delete(args.state); } } function forgetLivePreservedOwnedSessions(state) { const liveSessions = livePreservedOwnedSessionsByRunState.get(state); if (!liveSessions) { return; } for (const entry of liveSessions) { if (!entry.reuseRejected) { liveSessions.delete(entry); } } if (liveSessions.size === 0) { livePreservedOwnedSessionsByRunState.delete(state); } } function hasRejectedLivePreservedOwnedSessions(state) { return livePreservedOwnedSessionEntries(state).some((entry) => entry.reuseRejected); } function forgetLivePreservedOwnedSessionHandle(args) { if (!args.state) { return; } const liveSessions = livePreservedOwnedSessionsByRunState.get(args.state); if (!liveSessions) { return; } for (const entry of liveSessions) { if (entry.session === args.session) { liveSessions.delete(entry); } } if (liveSessions.size === 0) { livePreservedOwnedSessionsByRunState.delete(args.state); } } function rememberRejectedLivePreservedOwnedSessionHandle(args) { if (!args.state) { return; } const liveSessions = livePreservedOwnedSessionsByRunState.get(args.state) ?? new Set(); const existing = [...liveSessions].find((entry) => entry.session === args.session); if (existing) { existing.reuseRejected = true; } else { liveSessions.add({ agentKey: args.source.agentKey, backendId: args.source.backendId, currentAgentName: args.source.currentAgentName, session: args.session, reuseRejected: true, }); } livePreservedOwnedSessionsByRunState.set(args.state, liveSessions); } function rejectLivePreservedOwnedSessionHandle(args) { if (!args.state) { return; } const liveSessions = livePreservedOwnedSessionsByRunState.get(args.state); if (!liveSessions) { return; } for (const entry of liveSessions.values()) { if (entry.session === args.session) { entry.reuseRejected = true; } } } function livePreservedOwnedSessionEntries(state) { return [...(livePreservedOwnedSessionsByRunState.get(state)?.values() ?? [])]; } function preservedOwnedSessionAgentKeysWithoutLiveReuse(serializedState) { return new Set(Object.entries(serializedState.sessionsByAgent ?? {}) .filter(([, entry]) => entry.preservedOwnedSession && entry.reuseLiveSession === false) .map(([agentKey]) => agentKey)); } function livePreservedOwnedSession(args) { if (!args.serializedEntry?.preservedOwnedSession || !args.runState) { return undefined; } const liveSessions = livePreservedOwnedSessionsByRunState.get(args.runState); const matchingEntries = [...(liveSessions ?? [])].filter((entry) => entry.agentKey === args.agentKey && entry.backendId === args.client.backendId); const rejectedSessions = new Set([...(liveSessions ?? [])] .filter((entry) => entry.reuseRejected) .map((entry) => entry.session)); const liveEntry = matchingEntries.find((entry) => rejectedSessions.has(entry.session)) ?? matchingEntries[0]; if (!liveEntry) { return undefined; } return rejectedSessions.has(liveEntry.session) ? { ...liveEntry, reuseRejected: true } : liveEntry; } //# sourceMappingURL=livePreservedSessions.js.map