UNPKG

@openai/agents-core

Version:

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

135 lines • 5.11 kB
const livePreservedOwnedSessionsByRunState = new WeakMap(); export 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); } } export 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); } } export function hasRejectedLivePreservedOwnedSessions(state) { return livePreservedOwnedSessionEntries(state).some((entry) => entry.reuseRejected); } export 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); } } export 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); } export 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; } } } export function livePreservedOwnedSessionEntries(state) { return [...(livePreservedOwnedSessionsByRunState.get(state)?.values() ?? [])]; } export function preservedOwnedSessionAgentKeysWithoutLiveReuse(serializedState) { return new Set(Object.entries(serializedState.sessionsByAgent ?? {}) .filter(([, entry]) => entry.preservedOwnedSession && entry.reuseLiveSession === false) .map(([agentKey]) => agentKey)); } export 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.mjs.map