|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import type { |
| 8 | + AsyncPipelineDef, |
| 9 | + ContextManagementConfig, |
| 10 | + PipelineDef, |
| 11 | +} from './types.js'; |
| 12 | +import type { ContextEnvironment } from '../pipeline/environment.js'; |
| 13 | + |
| 14 | +// Import factories |
| 15 | +import { createToolMaskingProcessor } from '../processors/toolMaskingProcessor.js'; |
| 16 | +import { createBlobDegradationProcessor } from '../processors/blobDegradationProcessor.js'; |
| 17 | +import { createNodeTruncationProcessor } from '../processors/nodeTruncationProcessor.js'; |
| 18 | +import { createNodeDistillationProcessor } from '../processors/nodeDistillationProcessor.js'; |
| 19 | +import { createStateSnapshotProcessor } from '../processors/stateSnapshotProcessor.js'; |
| 20 | +import { createStateSnapshotAsyncProcessor } from '../processors/stateSnapshotAsyncProcessor.js'; |
| 21 | + |
| 22 | +/** |
| 23 | + * Helper to safely merge static default options with dynamically loaded |
| 24 | + * JSON overrides from the SidecarConfig. |
| 25 | + * |
| 26 | + * Why the unsafe cast is acceptable here: |
| 27 | + * Before the \`config\` object ever reaches this function, \`SidecarLoader.ts\` |
| 28 | + * passes the raw JSON through \`SchemaValidator\`. The schema dynamically generates |
| 29 | + * a \`oneOf\` map linking every \`type\` discriminator to its corresponding processor |
| 30 | + * schema definition. By the time we access \`options\` here, its shape has been |
| 31 | + * strictly validated against the corresponding Zod/JSONSchema definition at runtime, |
| 32 | + * making the generic cast to \`<T>\` structurally safe. |
| 33 | + */ |
| 34 | +function resolveProcessorOptions<T>( |
| 35 | + config: ContextManagementConfig | undefined, |
| 36 | + id: string, |
| 37 | + defaultOptions: T, |
| 38 | +): T { |
| 39 | + if (config?.processorOptions && config.processorOptions[id]) { |
| 40 | + return { |
| 41 | + ...defaultOptions, |
| 42 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion |
| 43 | + ...(config.processorOptions[id].options as T), |
| 44 | + }; |
| 45 | + } |
| 46 | + return defaultOptions; |
| 47 | +} |
| 48 | + |
| 49 | +export interface ContextProfile { |
| 50 | + config: ContextManagementConfig; |
| 51 | + buildPipelines: ( |
| 52 | + env: ContextEnvironment, |
| 53 | + config?: ContextManagementConfig, |
| 54 | + ) => PipelineDef[]; |
| 55 | + buildAsyncPipelines: ( |
| 56 | + env: ContextEnvironment, |
| 57 | + config?: ContextManagementConfig, |
| 58 | + ) => AsyncPipelineDef[]; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * The standard default context management profile. |
| 63 | + * Optimized for safety, precision, and reliable summarization. |
| 64 | + */ |
| 65 | +export const defaultContextProfile: ContextProfile = { |
| 66 | + config: { |
| 67 | + budget: { |
| 68 | + retainedTokens: 65000, |
| 69 | + maxTokens: 150000, |
| 70 | + }, |
| 71 | + }, |
| 72 | + |
| 73 | + buildPipelines: ( |
| 74 | + env: ContextEnvironment, |
| 75 | + config?: ContextManagementConfig, |
| 76 | + ): PipelineDef[] => |
| 77 | + // Helper to merge default options with dynamically loaded processorOptions by ID |
| 78 | + [ |
| 79 | + { |
| 80 | + name: 'Immediate Sanitization', |
| 81 | + triggers: ['new_message'], |
| 82 | + processors: [ |
| 83 | + createToolMaskingProcessor( |
| 84 | + 'ToolMasking', |
| 85 | + env, |
| 86 | + resolveProcessorOptions(config, 'ToolMasking', { |
| 87 | + stringLengthThresholdTokens: 8000, |
| 88 | + }), |
| 89 | + ), |
| 90 | + createBlobDegradationProcessor('BlobDegradation', env), // No options |
| 91 | + ], |
| 92 | + }, |
| 93 | + { |
| 94 | + name: 'Normalization', |
| 95 | + triggers: ['retained_exceeded'], |
| 96 | + processors: [ |
| 97 | + createNodeTruncationProcessor( |
| 98 | + 'NodeTruncation', |
| 99 | + env, |
| 100 | + resolveProcessorOptions(config, 'NodeTruncation', { |
| 101 | + maxTokensPerNode: 3000, |
| 102 | + }), |
| 103 | + ), |
| 104 | + createNodeDistillationProcessor( |
| 105 | + 'NodeDistillation', |
| 106 | + env, |
| 107 | + resolveProcessorOptions(config, 'NodeDistillation', { |
| 108 | + nodeThresholdTokens: 5000, |
| 109 | + }), |
| 110 | + ), |
| 111 | + ], |
| 112 | + }, |
| 113 | + { |
| 114 | + name: 'Emergency Backstop', |
| 115 | + triggers: ['gc_backstop'], |
| 116 | + processors: [ |
| 117 | + createStateSnapshotProcessor( |
| 118 | + 'StateSnapshotSync', |
| 119 | + env, |
| 120 | + resolveProcessorOptions(config, 'StateSnapshotSync', { |
| 121 | + target: 'max', |
| 122 | + }), |
| 123 | + ), |
| 124 | + ], |
| 125 | + }, |
| 126 | + ], |
| 127 | + buildAsyncPipelines: ( |
| 128 | + env: ContextEnvironment, |
| 129 | + config?: ContextManagementConfig, |
| 130 | + ): AsyncPipelineDef[] => [ |
| 131 | + { |
| 132 | + name: 'Async Background GC', |
| 133 | + triggers: ['nodes_aged_out'], |
| 134 | + processors: [ |
| 135 | + createStateSnapshotAsyncProcessor( |
| 136 | + 'StateSnapshotAsync', |
| 137 | + env, |
| 138 | + resolveProcessorOptions(config, 'StateSnapshotAsync', { |
| 139 | + type: 'accumulate', |
| 140 | + }), |
| 141 | + ), |
| 142 | + ], |
| 143 | + }, |
| 144 | + ], |
| 145 | +}; |
0 commit comments