UNPKG

@openai/agents-core

Version:

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

186 lines • 5.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CompactionModelInfo = exports.DynamicCompactionPolicy = exports.StaticCompactionPolicy = exports.CompactionPolicy = void 0; exports.compaction = compaction; const errors_1 = require("../../errors.js"); const base_1 = require("./base.js"); const transport_1 = require("./transport.js"); class CompactionPolicy { } exports.CompactionPolicy = CompactionPolicy; class StaticCompactionPolicy extends CompactionPolicy { threshold; constructor(threshold = 240000) { super(); this.threshold = threshold; } compactThreshold() { return this.threshold; } } exports.StaticCompactionPolicy = StaticCompactionPolicy; class DynamicCompactionPolicy extends CompactionPolicy { thresholdRatio; fallbackThreshold; constructor(thresholdRatio = 0.9, fallbackThreshold = 240000) { super(); if (!Number.isFinite(thresholdRatio) || thresholdRatio < 0 || thresholdRatio > 1) { throw new errors_1.UserError('DynamicCompactionPolicy.thresholdRatio must be a finite number between 0 and 1.'); } this.thresholdRatio = thresholdRatio; this.fallbackThreshold = fallbackThreshold; } compactThreshold(model) { if (!model) { return this.fallbackThreshold; } const modelInfo = CompactionModelInfo.forModel(model); if (!modelInfo) { return this.fallbackThreshold; } return Math.floor(modelInfo.contextWindow * this.thresholdRatio); } } exports.DynamicCompactionPolicy = DynamicCompactionPolicy; class CompactionModelInfo { contextWindow; constructor(contextWindow) { this.contextWindow = contextWindow; } static forModel(model) { for (const candidate of getSandboxCompactionModelCandidates(model)) { if (SANDBOX_CONTEXT_WINDOWS_1M.has(candidate)) { return new CompactionModelInfo(1047576); } if (SANDBOX_CONTEXT_WINDOWS_400K.has(candidate)) { return new CompactionModelInfo(400000); } if (SANDBOX_CONTEXT_WINDOWS_200K.has(candidate)) { return new CompactionModelInfo(200000); } if (SANDBOX_CONTEXT_WINDOWS_128K.has(candidate)) { return new CompactionModelInfo(128000); } } return undefined; } } exports.CompactionModelInfo = CompactionModelInfo; class CompactionCapability extends base_1.Capability { type = 'compaction'; policy; constructor(policy = new DynamicCompactionPolicy()) { super(); this.policy = policy; } samplingParams(samplingParams) { if (samplingParams.modelInstance && !(0, transport_1.supportsResponsesCompactionTransport)(samplingParams.modelInstance)) { return {}; } const model = typeof samplingParams.model === 'string' ? samplingParams.model : undefined; return { context_management: [ { type: 'compaction', compact_threshold: this.policy.compactThreshold(model), }, ], }; } processContext(context) { let lastCompactionIndex = -1; for (let index = 0; index < context.length; index += 1) { if (context[index].type === 'compaction') { lastCompactionIndex = index; } } if (lastCompactionIndex === -1) { return context; } return context.slice(lastCompactionIndex); } } function compaction(args = {}) { return new CompactionCapability(args.policy); } function getSandboxCompactionModelCandidates(model) { const trimmed = model.trim(); if (!trimmed) { return []; } const withoutProviderPrefix = trimmed.includes('/') ? trimmed.slice(trimmed.lastIndexOf('/') + 1) : trimmed; const normalized = withoutProviderPrefix.toLowerCase(); const withoutPinnedDate = normalized.replace(/-\d{4}-\d{2}-\d{2}$/u, ''); return [...new Set([normalized, withoutPinnedDate])]; } const SANDBOX_CONTEXT_WINDOWS_1M = new Set([ 'gpt-6-astra', 'gpt-6-sol', 'gpt-6-luna', 'gpt-6.1-sol', 'gpt-5.6', 'gpt-5.6-sol', 'gpt-5.6-terra', 'gpt-5.6-luna', 'gpt-5.4', 'gpt-5.4-2026-03-05', 'gpt-5.4-pro', 'gpt-5.4-pro-2026-03-05', 'gpt-4.1', 'gpt-4.1-2025-04-14', 'gpt-4.1-mini', 'gpt-4.1-mini-2025-04-14', 'gpt-4.1-nano', 'gpt-4.1-nano-2025-04-14', ]); const SANDBOX_CONTEXT_WINDOWS_400K = new Set([ 'gpt-5', 'gpt-5-codex', 'gpt-5-mini', 'gpt-5-nano', 'gpt-5-pro', 'gpt-5.1', 'gpt-5.1-codex', 'gpt-5.1-codex-max', 'gpt-5.1-codex-mini', 'gpt-5.2', 'gpt-5.2-codex', 'gpt-5.2-codex-cyber', 'gpt-5.2-pro', 'gpt-5.3-codex', 'gpt-5.3-codex-spark', 'gpt-5.4-mini', 'gpt-5.4-nano', ]); const SANDBOX_CONTEXT_WINDOWS_200K = new Set([ 'codex-mini-latest', 'o1', 'o1-2024-12-17', 'o1-pro', 'o3', 'o3-2025-04-16', 'o3-mini', 'o3-pro', 'o3-deep-research', 'o4-mini', 'o4-mini-2025-04-16', 'o4-mini-deep-research', ]); const SANDBOX_CONTEXT_WINDOWS_128K = new Set([ 'gpt-4o', 'gpt-4o-2024-08-06', 'gpt-4o-mini', 'gpt-4o-mini-2024-07-18', 'gpt-5-chat-latest', 'gpt-5.1-chat-latest', 'gpt-5.2-chat-latest', 'gpt-5.3-chat-latest', ]); //# sourceMappingURL=compaction.js.map