@diyor28/context 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +270 -0
- package/dist/__tests__/attachment-selector.test.d.ts +11 -0
- package/dist/__tests__/attachment-selector.test.d.ts.map +1 -0
- package/dist/__tests__/attachment-selector.test.js +449 -0
- package/dist/__tests__/attachment-selector.test.js.map +1 -0
- package/dist/__tests__/cache-breakpoints.test.d.ts +11 -0
- package/dist/__tests__/cache-breakpoints.test.d.ts.map +1 -0
- package/dist/__tests__/cache-breakpoints.test.js +398 -0
- package/dist/__tests__/cache-breakpoints.test.js.map +1 -0
- package/dist/__tests__/codecs.test.d.ts +7 -0
- package/dist/__tests__/codecs.test.d.ts.map +1 -0
- package/dist/__tests__/codecs.test.js +331 -0
- package/dist/__tests__/codecs.test.js.map +1 -0
- package/dist/__tests__/compactor.test.d.ts +11 -0
- package/dist/__tests__/compactor.test.d.ts.map +1 -0
- package/dist/__tests__/compactor.test.js +519 -0
- package/dist/__tests__/compactor.test.js.map +1 -0
- package/dist/__tests__/context-graph.test.d.ts +7 -0
- package/dist/__tests__/context-graph.test.d.ts.map +1 -0
- package/dist/__tests__/context-graph.test.js +262 -0
- package/dist/__tests__/context-graph.test.js.map +1 -0
- package/dist/__tests__/hash.test.d.ts +7 -0
- package/dist/__tests__/hash.test.d.ts.map +1 -0
- package/dist/__tests__/hash.test.js +228 -0
- package/dist/__tests__/hash.test.js.map +1 -0
- package/dist/__tests__/integration.test.d.ts +15 -0
- package/dist/__tests__/integration.test.d.ts.map +1 -0
- package/dist/__tests__/integration.test.js +728 -0
- package/dist/__tests__/integration.test.js.map +1 -0
- package/dist/__tests__/kind-order.test.d.ts +7 -0
- package/dist/__tests__/kind-order.test.d.ts.map +1 -0
- package/dist/__tests__/kind-order.test.js +243 -0
- package/dist/__tests__/kind-order.test.js.map +1 -0
- package/dist/__tests__/phase2-integration.test.d.ts +5 -0
- package/dist/__tests__/phase2-integration.test.d.ts.map +1 -0
- package/dist/__tests__/phase2-integration.test.js +222 -0
- package/dist/__tests__/phase2-integration.test.js.map +1 -0
- package/dist/__tests__/queries.test.d.ts +7 -0
- package/dist/__tests__/queries.test.d.ts.map +1 -0
- package/dist/__tests__/queries.test.js +254 -0
- package/dist/__tests__/queries.test.js.map +1 -0
- package/dist/__tests__/token-estimator.test.d.ts +7 -0
- package/dist/__tests__/token-estimator.test.d.ts.map +1 -0
- package/dist/__tests__/token-estimator.test.js +267 -0
- package/dist/__tests__/token-estimator.test.js.map +1 -0
- package/dist/adapters/anthropic-estimator.d.ts +38 -0
- package/dist/adapters/anthropic-estimator.d.ts.map +1 -0
- package/dist/adapters/anthropic-estimator.js +108 -0
- package/dist/adapters/anthropic-estimator.js.map +1 -0
- package/dist/adapters/attachment-resolver.d.ts +96 -0
- package/dist/adapters/attachment-resolver.d.ts.map +1 -0
- package/dist/adapters/attachment-resolver.js +176 -0
- package/dist/adapters/attachment-resolver.js.map +1 -0
- package/dist/adapters/attachment-selector.d.ts +59 -0
- package/dist/adapters/attachment-selector.d.ts.map +1 -0
- package/dist/adapters/attachment-selector.js +163 -0
- package/dist/adapters/attachment-selector.js.map +1 -0
- package/dist/adapters/gemini-estimator.d.ts +27 -0
- package/dist/adapters/gemini-estimator.d.ts.map +1 -0
- package/dist/adapters/gemini-estimator.js +80 -0
- package/dist/adapters/gemini-estimator.js.map +1 -0
- package/dist/adapters/index.d.ts +12 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +28 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/memory-store.d.ts +139 -0
- package/dist/adapters/memory-store.d.ts.map +1 -0
- package/dist/adapters/memory-store.js +187 -0
- package/dist/adapters/memory-store.js.map +1 -0
- package/dist/adapters/openai-estimator.d.ts +35 -0
- package/dist/adapters/openai-estimator.d.ts.map +1 -0
- package/dist/adapters/openai-estimator.js +89 -0
- package/dist/adapters/openai-estimator.js.map +1 -0
- package/dist/adapters/summarizer.d.ts +121 -0
- package/dist/adapters/summarizer.d.ts.map +1 -0
- package/dist/adapters/summarizer.js +121 -0
- package/dist/adapters/summarizer.js.map +1 -0
- package/dist/adapters/token-estimator.d.ts +63 -0
- package/dist/adapters/token-estimator.d.ts.map +1 -0
- package/dist/adapters/token-estimator.js +37 -0
- package/dist/adapters/token-estimator.js.map +1 -0
- package/dist/builder/context-builder.d.ts +186 -0
- package/dist/builder/context-builder.d.ts.map +1 -0
- package/dist/builder/context-builder.js +305 -0
- package/dist/builder/context-builder.js.map +1 -0
- package/dist/builder/context-fork.d.ts +166 -0
- package/dist/builder/context-fork.d.ts.map +1 -0
- package/dist/builder/context-fork.js +282 -0
- package/dist/builder/context-fork.js.map +1 -0
- package/dist/builder/index.d.ts +6 -0
- package/dist/builder/index.d.ts.map +1 -0
- package/dist/builder/index.js +22 -0
- package/dist/builder/index.js.map +1 -0
- package/dist/codecs/base.d.ts +18 -0
- package/dist/codecs/base.d.ts.map +1 -0
- package/dist/codecs/base.js +39 -0
- package/dist/codecs/base.js.map +1 -0
- package/dist/codecs/conversation-history.codec.d.ts +81 -0
- package/dist/codecs/conversation-history.codec.d.ts.map +1 -0
- package/dist/codecs/conversation-history.codec.js +89 -0
- package/dist/codecs/conversation-history.codec.js.map +1 -0
- package/dist/codecs/index.d.ts +31 -0
- package/dist/codecs/index.d.ts.map +1 -0
- package/dist/codecs/index.js +71 -0
- package/dist/codecs/index.js.map +1 -0
- package/dist/codecs/redacted-stub.codec.d.ts +32 -0
- package/dist/codecs/redacted-stub.codec.d.ts.map +1 -0
- package/dist/codecs/redacted-stub.codec.js +64 -0
- package/dist/codecs/redacted-stub.codec.js.map +1 -0
- package/dist/codecs/structured-reference.codec.d.ts +40 -0
- package/dist/codecs/structured-reference.codec.d.ts.map +1 -0
- package/dist/codecs/structured-reference.codec.js +81 -0
- package/dist/codecs/structured-reference.codec.js.map +1 -0
- package/dist/codecs/system-rules.codec.d.ts +32 -0
- package/dist/codecs/system-rules.codec.d.ts.map +1 -0
- package/dist/codecs/system-rules.codec.js +62 -0
- package/dist/codecs/system-rules.codec.js.map +1 -0
- package/dist/codecs/tool-output.codec.d.ts +66 -0
- package/dist/codecs/tool-output.codec.d.ts.map +1 -0
- package/dist/codecs/tool-output.codec.js +95 -0
- package/dist/codecs/tool-output.codec.js.map +1 -0
- package/dist/codecs/tool-schema.codec.d.ts +36 -0
- package/dist/codecs/tool-schema.codec.d.ts.map +1 -0
- package/dist/codecs/tool-schema.codec.js +74 -0
- package/dist/codecs/tool-schema.codec.js.map +1 -0
- package/dist/codecs/unsafe-text.codec.d.ts +28 -0
- package/dist/codecs/unsafe-text.codec.d.ts.map +1 -0
- package/dist/codecs/unsafe-text.codec.js +63 -0
- package/dist/codecs/unsafe-text.codec.js.map +1 -0
- package/dist/graph/context-graph.d.ts +121 -0
- package/dist/graph/context-graph.d.ts.map +1 -0
- package/dist/graph/context-graph.js +166 -0
- package/dist/graph/context-graph.js.map +1 -0
- package/dist/graph/index.d.ts +8 -0
- package/dist/graph/index.d.ts.map +1 -0
- package/dist/graph/index.js +24 -0
- package/dist/graph/index.js.map +1 -0
- package/dist/graph/kind-order.d.ts +60 -0
- package/dist/graph/kind-order.d.ts.map +1 -0
- package/dist/graph/kind-order.js +113 -0
- package/dist/graph/kind-order.js.map +1 -0
- package/dist/graph/queries.d.ts +68 -0
- package/dist/graph/queries.d.ts.map +1 -0
- package/dist/graph/queries.js +240 -0
- package/dist/graph/queries.js.map +1 -0
- package/dist/graph/views.d.ts +90 -0
- package/dist/graph/views.d.ts.map +1 -0
- package/dist/graph/views.js +173 -0
- package/dist/graph/views.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/pipeline/compactor.d.ts +128 -0
- package/dist/pipeline/compactor.d.ts.map +1 -0
- package/dist/pipeline/compactor.js +346 -0
- package/dist/pipeline/compactor.js.map +1 -0
- package/dist/pipeline/index.d.ts +6 -0
- package/dist/pipeline/index.d.ts.map +1 -0
- package/dist/pipeline/index.js +22 -0
- package/dist/pipeline/index.js.map +1 -0
- package/dist/pipeline/summarizer.d.ts +18 -0
- package/dist/pipeline/summarizer.d.ts.map +1 -0
- package/dist/pipeline/summarizer.js +68 -0
- package/dist/pipeline/summarizer.js.map +1 -0
- package/dist/policies/default-policy.d.ts +29 -0
- package/dist/policies/default-policy.d.ts.map +1 -0
- package/dist/policies/default-policy.js +58 -0
- package/dist/policies/default-policy.js.map +1 -0
- package/dist/policies/index.d.ts +5 -0
- package/dist/policies/index.d.ts.map +1 -0
- package/dist/policies/index.js +21 -0
- package/dist/policies/index.js.map +1 -0
- package/dist/providers/anthropic-compiler.d.ts +58 -0
- package/dist/providers/anthropic-compiler.d.ts.map +1 -0
- package/dist/providers/anthropic-compiler.js +182 -0
- package/dist/providers/anthropic-compiler.js.map +1 -0
- package/dist/providers/capabilities.d.ts +54 -0
- package/dist/providers/capabilities.d.ts.map +1 -0
- package/dist/providers/capabilities.js +87 -0
- package/dist/providers/capabilities.js.map +1 -0
- package/dist/providers/gemini-compiler.d.ts +51 -0
- package/dist/providers/gemini-compiler.d.ts.map +1 -0
- package/dist/providers/gemini-compiler.js +206 -0
- package/dist/providers/gemini-compiler.js.map +1 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +24 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai-compiler.d.ts +46 -0
- package/dist/providers/openai-compiler.d.ts.map +1 -0
- package/dist/providers/openai-compiler.js +149 -0
- package/dist/providers/openai-compiler.js.map +1 -0
- package/dist/types/attachment.d.ts +62 -0
- package/dist/types/attachment.d.ts.map +1 -0
- package/dist/types/attachment.js +6 -0
- package/dist/types/attachment.js.map +1 -0
- package/dist/types/block.d.ts +61 -0
- package/dist/types/block.d.ts.map +1 -0
- package/dist/types/block.js +8 -0
- package/dist/types/block.js.map +1 -0
- package/dist/types/codec.d.ts +58 -0
- package/dist/types/codec.d.ts.map +1 -0
- package/dist/types/codec.js +6 -0
- package/dist/types/codec.js.map +1 -0
- package/dist/types/compiled.d.ts +91 -0
- package/dist/types/compiled.d.ts.map +1 -0
- package/dist/types/compiled.js +6 -0
- package/dist/types/compiled.js.map +1 -0
- package/dist/types/hash.d.ts +24 -0
- package/dist/types/hash.d.ts.map +1 -0
- package/dist/types/hash.js +49 -0
- package/dist/types/hash.js.map +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +26 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/policy.d.ts +128 -0
- package/dist/types/policy.d.ts.map +1 -0
- package/dist/types/policy.js +55 -0
- package/dist/types/policy.js.map +1 -0
- package/package.json +55 -0
- package/postcss.config.js +4 -0
- package/src/__tests__/attachment-selector.test.ts +559 -0
- package/src/__tests__/cache-breakpoints.test.ts +566 -0
- package/src/__tests__/codecs.test.ts +417 -0
- package/src/__tests__/compactor.test.ts +608 -0
- package/src/__tests__/context-graph.test.ts +383 -0
- package/src/__tests__/hash.test.ts +274 -0
- package/src/__tests__/integration.test.ts +866 -0
- package/src/__tests__/kind-order.test.ts +312 -0
- package/src/__tests__/phase2-integration.test.ts +253 -0
- package/src/__tests__/queries.test.ts +387 -0
- package/src/__tests__/token-estimator.test.ts +326 -0
- package/src/adapters/anthropic-estimator.ts +125 -0
- package/src/adapters/attachment-resolver.ts +295 -0
- package/src/adapters/attachment-selector.ts +218 -0
- package/src/adapters/gemini-estimator.ts +93 -0
- package/src/adapters/index.ts +12 -0
- package/src/adapters/memory-store.ts +299 -0
- package/src/adapters/openai-estimator.ts +105 -0
- package/src/adapters/summarizer.ts +250 -0
- package/src/adapters/token-estimator.ts +74 -0
- package/src/builder/context-builder.ts +467 -0
- package/src/builder/context-fork.ts +471 -0
- package/src/builder/index.ts +6 -0
- package/src/codecs/base.ts +36 -0
- package/src/codecs/conversation-history.codec.ts +108 -0
- package/src/codecs/index.ts +57 -0
- package/src/codecs/redacted-stub.codec.ts +76 -0
- package/src/codecs/structured-reference.codec.ts +96 -0
- package/src/codecs/system-rules.codec.ts +74 -0
- package/src/codecs/tool-output.codec.ts +109 -0
- package/src/codecs/tool-schema.codec.ts +87 -0
- package/src/codecs/unsafe-text.codec.ts +74 -0
- package/src/graph/context-graph.ts +205 -0
- package/src/graph/index.ts +8 -0
- package/src/graph/kind-order.ts +125 -0
- package/src/graph/queries.ts +306 -0
- package/src/graph/views.ts +255 -0
- package/src/index.ts +31 -0
- package/src/pipeline/compactor.ts +563 -0
- package/src/pipeline/index.ts +6 -0
- package/src/pipeline/summarizer.ts +76 -0
- package/src/policies/default-policy.ts +69 -0
- package/src/policies/index.ts +5 -0
- package/src/providers/anthropic-compiler.ts +294 -0
- package/src/providers/capabilities.ts +144 -0
- package/src/providers/gemini-compiler.ts +272 -0
- package/src/providers/index.ts +8 -0
- package/src/providers/openai-compiler.ts +191 -0
- package/src/types/attachment.ts +86 -0
- package/src/types/block.ts +84 -0
- package/src/types/codec.ts +68 -0
- package/src/types/compiled.ts +109 -0
- package/src/types/hash.ts +58 -0
- package/src/types/index.ts +10 -0
- package/src/types/policy.ts +194 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +21 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Compactor: View-based context compaction with provenance tracking.
|
|
4
|
+
*
|
|
5
|
+
* Responsibilities:
|
|
6
|
+
* - Prune tool outputs (keep errors, truncate large outputs)
|
|
7
|
+
* - Deduplicate identical blocks
|
|
8
|
+
* - Trim conversation history (keep recent + errors)
|
|
9
|
+
* - Summarize history (optional, needs Summarizer)
|
|
10
|
+
*
|
|
11
|
+
* IMPORTANT: View-based compaction never mutates the input graph.
|
|
12
|
+
* Creates replacement blocks with full provenance (derivedFrom, method, version).
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.DEFAULT_TOOL_OUTPUT_PRUNING = void 0;
|
|
16
|
+
exports.compactView = compactView;
|
|
17
|
+
const hash_js_1 = require("../types/hash.js");
|
|
18
|
+
/**
|
|
19
|
+
* Default tool output pruning configuration.
|
|
20
|
+
*/
|
|
21
|
+
exports.DEFAULT_TOOL_OUTPUT_PRUNING = {
|
|
22
|
+
maxRawTailChars: 500,
|
|
23
|
+
preserveErrorTail: true,
|
|
24
|
+
maxOutputsPerTool: 3,
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Compact a context view.
|
|
28
|
+
* Returns a new set of blocks with replacements tracked via provenance.
|
|
29
|
+
*
|
|
30
|
+
* @param view - Context view to compact
|
|
31
|
+
* @param config - Compaction configuration
|
|
32
|
+
* @param estimator - Token estimator for calculating savings
|
|
33
|
+
* @returns Compaction result
|
|
34
|
+
*/
|
|
35
|
+
async function compactView(view, config, estimator) {
|
|
36
|
+
let blocks = [...view.blocks];
|
|
37
|
+
const removedBlocks = [];
|
|
38
|
+
const stepReports = [];
|
|
39
|
+
// Apply compaction steps in order
|
|
40
|
+
for (const step of config.steps) {
|
|
41
|
+
const beforeCount = blocks.length;
|
|
42
|
+
const beforeTokens = view.tokenEstimate?.tokens ?? 0;
|
|
43
|
+
switch (step) {
|
|
44
|
+
case 'dedupe': {
|
|
45
|
+
const result = deduplicateBlocks(blocks);
|
|
46
|
+
// Calculate token savings from removed duplicates
|
|
47
|
+
const removedEstimate = result.removed.length > 0
|
|
48
|
+
? await estimator.estimate(result.removed)
|
|
49
|
+
: { tokens: 0 };
|
|
50
|
+
blocks = result.blocks;
|
|
51
|
+
removedBlocks.push(...result.removed);
|
|
52
|
+
stepReports.push({
|
|
53
|
+
step,
|
|
54
|
+
blocksRemoved: result.removed.length,
|
|
55
|
+
blocksReplaced: 0,
|
|
56
|
+
tokensSaved: removedEstimate.tokens,
|
|
57
|
+
lossy: false,
|
|
58
|
+
description: `Removed ${result.removed.length} duplicate blocks`,
|
|
59
|
+
});
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case 'tool_output_prune': {
|
|
63
|
+
const pruningConfig = config.toolOutputPruning ?? exports.DEFAULT_TOOL_OUTPUT_PRUNING;
|
|
64
|
+
// Estimate tokens before pruning
|
|
65
|
+
const toolOutputBlocks = blocks.filter((b) => b.meta.kind === 'tool_output');
|
|
66
|
+
const beforeEstimate = toolOutputBlocks.length > 0
|
|
67
|
+
? await estimator.estimate(toolOutputBlocks)
|
|
68
|
+
: { tokens: 0 };
|
|
69
|
+
const result = pruneToolOutputs(blocks, pruningConfig);
|
|
70
|
+
// Estimate tokens after pruning (only tool output blocks)
|
|
71
|
+
const keptToolOutputs = result.blocks.filter((b) => b.meta.kind === 'tool_output');
|
|
72
|
+
const afterEstimate = keptToolOutputs.length > 0
|
|
73
|
+
? await estimator.estimate(keptToolOutputs)
|
|
74
|
+
: { tokens: 0 };
|
|
75
|
+
blocks = result.blocks;
|
|
76
|
+
removedBlocks.push(...result.removed);
|
|
77
|
+
stepReports.push({
|
|
78
|
+
step,
|
|
79
|
+
blocksRemoved: result.removed.length,
|
|
80
|
+
blocksReplaced: result.replaced.length,
|
|
81
|
+
tokensSaved: beforeEstimate.tokens - afterEstimate.tokens,
|
|
82
|
+
lossy: result.replaced.length > 0,
|
|
83
|
+
description: `Pruned ${result.replaced.length} tool outputs, removed ${result.removed.length}`,
|
|
84
|
+
});
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case 'history_trim': {
|
|
88
|
+
const trimConfig = config.historyTrim ?? {
|
|
89
|
+
keepRecentMessages: 20,
|
|
90
|
+
keepErrorMessages: true,
|
|
91
|
+
};
|
|
92
|
+
const result = trimHistory(blocks, trimConfig);
|
|
93
|
+
// Calculate token savings from removed history blocks
|
|
94
|
+
const removedEstimate = result.removed.length > 0
|
|
95
|
+
? await estimator.estimate(result.removed)
|
|
96
|
+
: { tokens: 0 };
|
|
97
|
+
blocks = result.blocks;
|
|
98
|
+
removedBlocks.push(...result.removed);
|
|
99
|
+
stepReports.push({
|
|
100
|
+
step,
|
|
101
|
+
blocksRemoved: result.removed.length,
|
|
102
|
+
blocksReplaced: 0,
|
|
103
|
+
tokensSaved: removedEstimate.tokens,
|
|
104
|
+
lossy: result.removed.length > 0,
|
|
105
|
+
description: `Trimmed ${result.removed.length} old history messages`,
|
|
106
|
+
});
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
case 'summarize_history': {
|
|
110
|
+
const summaryConfig = config.summarization;
|
|
111
|
+
if (!summaryConfig?.summarizer) {
|
|
112
|
+
stepReports.push({
|
|
113
|
+
step,
|
|
114
|
+
blocksRemoved: 0,
|
|
115
|
+
blocksReplaced: 0,
|
|
116
|
+
tokensSaved: 0,
|
|
117
|
+
lossy: false,
|
|
118
|
+
description: 'Summarizer not configured, skipping',
|
|
119
|
+
});
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
const historyBlocks = blocks.filter((b) => b.meta.kind === 'history');
|
|
123
|
+
if (historyBlocks.length < summaryConfig.minMessages) {
|
|
124
|
+
stepReports.push({
|
|
125
|
+
step,
|
|
126
|
+
blocksRemoved: 0,
|
|
127
|
+
blocksReplaced: 0,
|
|
128
|
+
tokensSaved: 0,
|
|
129
|
+
lossy: false,
|
|
130
|
+
description: `Insufficient history (${historyBlocks.length} < ${summaryConfig.minMessages})`,
|
|
131
|
+
});
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
// Split into recent (keep) and old (summarize)
|
|
135
|
+
const keepCount = 10; // Keep last 10 messages raw
|
|
136
|
+
const recent = historyBlocks.slice(-keepCount);
|
|
137
|
+
const old = historyBlocks.slice(0, -keepCount);
|
|
138
|
+
if (old.length === 0) {
|
|
139
|
+
stepReports.push({
|
|
140
|
+
step,
|
|
141
|
+
blocksRemoved: 0,
|
|
142
|
+
blocksReplaced: 0,
|
|
143
|
+
tokensSaved: 0,
|
|
144
|
+
lossy: false,
|
|
145
|
+
description: 'No old history to summarize',
|
|
146
|
+
});
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
// Estimate tokens before summarization
|
|
150
|
+
const beforeSummaryEstimate = await estimator.estimate(old);
|
|
151
|
+
const targetTokens = Math.floor(beforeSummaryEstimate.tokens * 0.3); // Compress to 30%
|
|
152
|
+
// Create summary block
|
|
153
|
+
const summaryBlock = await summaryConfig.summarizer.summarize(old, targetTokens);
|
|
154
|
+
// Estimate tokens for summary block
|
|
155
|
+
const afterSummaryEstimate = await estimator.estimateBlock(summaryBlock);
|
|
156
|
+
// Replace old blocks with summary
|
|
157
|
+
const nonHistoryBlocks = blocks.filter((b) => b.meta.kind !== 'history');
|
|
158
|
+
blocks = [...nonHistoryBlocks, summaryBlock, ...recent];
|
|
159
|
+
removedBlocks.push(...old);
|
|
160
|
+
stepReports.push({
|
|
161
|
+
step,
|
|
162
|
+
blocksRemoved: old.length,
|
|
163
|
+
blocksReplaced: 1,
|
|
164
|
+
tokensSaved: beforeSummaryEstimate.tokens - afterSummaryEstimate.tokens,
|
|
165
|
+
lossy: true,
|
|
166
|
+
description: `Summarized ${old.length} old messages into 1 summary block`,
|
|
167
|
+
});
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
// Calculate total savings - recalculate tokens for compacted blocks
|
|
173
|
+
const beforeTokens = view.tokenEstimate?.tokens ?? 0;
|
|
174
|
+
// Recalculate tokens for compacted blocks
|
|
175
|
+
const afterEstimate = blocks.length > 0
|
|
176
|
+
? await estimator.estimate(blocks)
|
|
177
|
+
: { tokens: 0 };
|
|
178
|
+
const afterTokens = afterEstimate.tokens;
|
|
179
|
+
const savedTokens = beforeTokens - afterTokens;
|
|
180
|
+
return {
|
|
181
|
+
blocks,
|
|
182
|
+
removedBlocks,
|
|
183
|
+
report: {
|
|
184
|
+
beforeTokens,
|
|
185
|
+
afterTokens,
|
|
186
|
+
savedTokens,
|
|
187
|
+
stepsApplied: config.steps,
|
|
188
|
+
stepReports,
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Deduplicate identical blocks.
|
|
194
|
+
* Keeps first occurrence of each unique blockHash.
|
|
195
|
+
*
|
|
196
|
+
* @param blocks - Input blocks
|
|
197
|
+
* @returns Deduplicated blocks and removed duplicates
|
|
198
|
+
*/
|
|
199
|
+
function deduplicateBlocks(blocks) {
|
|
200
|
+
const seen = new Set();
|
|
201
|
+
const deduplicated = [];
|
|
202
|
+
const removed = [];
|
|
203
|
+
for (const block of blocks) {
|
|
204
|
+
if (seen.has(block.blockHash)) {
|
|
205
|
+
removed.push(block);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
seen.add(block.blockHash);
|
|
209
|
+
deduplicated.push(block);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return { blocks: deduplicated, removed };
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Prune tool outputs.
|
|
216
|
+
* Keeps:
|
|
217
|
+
* - All error outputs (preserveErrorTail = true)
|
|
218
|
+
* - Last N outputs per tool type
|
|
219
|
+
* - Truncated version of large outputs (tail only)
|
|
220
|
+
*
|
|
221
|
+
* @param blocks - Input blocks
|
|
222
|
+
* @param config - Pruning configuration
|
|
223
|
+
* @returns Pruned blocks, removed blocks, and replaced blocks
|
|
224
|
+
*/
|
|
225
|
+
function pruneToolOutputs(blocks, config) {
|
|
226
|
+
const toolOutputBlocks = blocks.filter((b) => b.meta.kind === 'tool_output');
|
|
227
|
+
const otherBlocks = blocks.filter((b) => b.meta.kind !== 'tool_output');
|
|
228
|
+
// Group by tool type (codecId)
|
|
229
|
+
const byTool = new Map();
|
|
230
|
+
for (const block of toolOutputBlocks) {
|
|
231
|
+
const toolId = block.meta.codecId;
|
|
232
|
+
if (!byTool.has(toolId)) {
|
|
233
|
+
byTool.set(toolId, []);
|
|
234
|
+
}
|
|
235
|
+
byTool.get(toolId).push(block);
|
|
236
|
+
}
|
|
237
|
+
const kept = [];
|
|
238
|
+
const removed = [];
|
|
239
|
+
const replaced = [];
|
|
240
|
+
// For each tool type, keep recent outputs and prune old/large ones
|
|
241
|
+
for (const [toolId, toolBlocks] of byTool.entries()) {
|
|
242
|
+
// Sort by createdAt (most recent last)
|
|
243
|
+
const sorted = [...toolBlocks].sort((a, b) => a.meta.createdAt - b.meta.createdAt);
|
|
244
|
+
// Keep last N outputs
|
|
245
|
+
const toKeep = sorted.slice(-config.maxOutputsPerTool);
|
|
246
|
+
const toRemove = sorted.slice(0, -config.maxOutputsPerTool);
|
|
247
|
+
// Check if any outputs need truncation
|
|
248
|
+
for (const block of toKeep) {
|
|
249
|
+
const payload = block.payload;
|
|
250
|
+
// Check if output is large (needs truncation)
|
|
251
|
+
const isLarge = payload.output && typeof payload.output === 'string' && payload.output.length > config.maxRawTailChars;
|
|
252
|
+
const isError = payload.error || payload.status === 'error';
|
|
253
|
+
if (isLarge && (!isError || !config.preserveErrorTail)) {
|
|
254
|
+
// Create truncated replacement block
|
|
255
|
+
const truncatedPayload = {
|
|
256
|
+
...payload,
|
|
257
|
+
output: truncateTail(payload.output, config.maxRawTailChars),
|
|
258
|
+
_truncated: true,
|
|
259
|
+
};
|
|
260
|
+
const replacementBlock = createReplacementBlock(block, truncatedPayload, 'tool_output_prune', '1.0.0');
|
|
261
|
+
kept.push(replacementBlock);
|
|
262
|
+
replaced.push(block);
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
kept.push(block);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
removed.push(...toRemove);
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
blocks: [...otherBlocks, ...kept],
|
|
272
|
+
removed,
|
|
273
|
+
replaced,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Trim conversation history.
|
|
278
|
+
* Keeps recent messages and optionally error messages.
|
|
279
|
+
*
|
|
280
|
+
* @param blocks - Input blocks
|
|
281
|
+
* @param config - Trim configuration
|
|
282
|
+
* @returns Trimmed blocks and removed blocks
|
|
283
|
+
*/
|
|
284
|
+
function trimHistory(blocks, config) {
|
|
285
|
+
const historyBlocks = blocks.filter((b) => b.meta.kind === 'history');
|
|
286
|
+
const otherBlocks = blocks.filter((b) => b.meta.kind !== 'history');
|
|
287
|
+
if (historyBlocks.length === 0) {
|
|
288
|
+
return { blocks, removed: [] };
|
|
289
|
+
}
|
|
290
|
+
// Sort by createdAt
|
|
291
|
+
const sorted = [...historyBlocks].sort((a, b) => a.meta.createdAt - b.meta.createdAt);
|
|
292
|
+
// Keep recent messages
|
|
293
|
+
const recent = sorted.slice(-config.keepRecentMessages);
|
|
294
|
+
const old = sorted.slice(0, -config.keepRecentMessages);
|
|
295
|
+
// Optionally keep error messages from old
|
|
296
|
+
const errorMessages = config.keepErrorMessages
|
|
297
|
+
? old.filter((b) => {
|
|
298
|
+
const payload = b.payload;
|
|
299
|
+
return payload.messages?.some((m) => m.error);
|
|
300
|
+
})
|
|
301
|
+
: [];
|
|
302
|
+
const kept = [...recent, ...errorMessages];
|
|
303
|
+
const removed = old.filter((b) => !errorMessages.includes(b));
|
|
304
|
+
return {
|
|
305
|
+
blocks: [...otherBlocks, ...kept],
|
|
306
|
+
removed,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Truncate text to tail characters with ellipsis.
|
|
311
|
+
*
|
|
312
|
+
* @param text - Text to truncate
|
|
313
|
+
* @param maxChars - Maximum characters to keep
|
|
314
|
+
* @returns Truncated text
|
|
315
|
+
*/
|
|
316
|
+
function truncateTail(text, maxChars) {
|
|
317
|
+
if (text.length <= maxChars) {
|
|
318
|
+
return text;
|
|
319
|
+
}
|
|
320
|
+
const tail = text.slice(-maxChars);
|
|
321
|
+
return `... [truncated ${text.length - maxChars} chars] ...\n${tail}`;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Create a replacement block with provenance.
|
|
325
|
+
*
|
|
326
|
+
* @param original - Original block
|
|
327
|
+
* @param newPayload - New payload
|
|
328
|
+
* @param method - Compaction method
|
|
329
|
+
* @param version - Compaction version
|
|
330
|
+
* @returns Replacement block
|
|
331
|
+
*/
|
|
332
|
+
function createReplacementBlock(original, newPayload, method, version) {
|
|
333
|
+
// Compute new hash
|
|
334
|
+
const blockHash = (0, hash_js_1.computeBlockHash)(original.meta, newPayload);
|
|
335
|
+
// Create replacement block with provenance metadata
|
|
336
|
+
return {
|
|
337
|
+
blockHash,
|
|
338
|
+
meta: {
|
|
339
|
+
...original.meta,
|
|
340
|
+
source: `${original.meta.source ?? 'unknown'}:compacted`,
|
|
341
|
+
tags: [...(original.meta.tags ?? []), `compacted:${method}`],
|
|
342
|
+
},
|
|
343
|
+
payload: newPayload,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
//# sourceMappingURL=compactor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compactor.js","sourceRoot":"","sources":["../../src/pipeline/compactor.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;AA2JH,kCA6LC;AApVD,8CAAoD;AAqIpD;;GAEG;AACU,QAAA,2BAA2B,GAA4B;IAClE,eAAe,EAAE,GAAG;IACpB,iBAAiB,EAAE,IAAI;IACvB,iBAAiB,EAAE,CAAC;CACrB,CAAC;AAEF;;;;;;;;GAQG;AACI,KAAK,UAAU,WAAW,CAC/B,IAAiB,EACjB,MAAgC,EAChC,SAAyB;IAEzB,IAAI,MAAM,GAAG,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;IAC9B,MAAM,aAAa,GAAmB,EAAE,CAAC;IACzC,MAAM,WAAW,GAAiB,EAAE,CAAC;IAErC,kCAAkC;IAClC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;QAChC,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC;QAClC,MAAM,YAAY,GAAG,IAAI,CAAC,aAAa,EAAE,MAAM,I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AE,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC,EAAE,aAAa,MAAM,EAAE,CAAC;SAC7D;QACD,OAAO,EAAE,UAAU;KACpB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/pipeline/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Pipeline exports (compaction, fork, etc.).
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
17
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
__exportStar(require("./compactor.js"), exports);
|
|
21
|
+
__exportStar(require("./summarizer.js"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/pipeline/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;AAEH,iDAA+B;AAC/B,kDAAgC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Summarizer implementations for history compaction.
|
|
3
|
+
*
|
|
4
|
+
* Uses LLM to condense old conversation history into compact summaries.
|
|
5
|
+
*/
|
|
6
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
7
|
+
import type { ContextBlock } from '../types/block.js';
|
|
8
|
+
import type { HistorySummarizer } from './compactor.js';
|
|
9
|
+
/**
|
|
10
|
+
* Anthropic-based history summarizer.
|
|
11
|
+
* Uses Claude Haiku for cost-effective summarization.
|
|
12
|
+
*/
|
|
13
|
+
export declare class AnthropicSummarizer implements HistorySummarizer {
|
|
14
|
+
private anthropicClient;
|
|
15
|
+
constructor(anthropicClient: Anthropic);
|
|
16
|
+
summarize(blocks: ContextBlock[], targetTokens: number): Promise<ContextBlock>;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=summarizer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"summarizer.d.ts","sourceRoot":"","sources":["../../src/pipeline/summarizer.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAC1C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAGxD;;;GAGG;AACH,qBAAa,mBAAoB,YAAW,iBAAiB;IAC/C,OAAO,CAAC,eAAe;gBAAf,eAAe,EAAE,SAAS;IAExC,SAAS,CAAC,MAAM,EAAE,YAAY,EAAE,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;CAyDrF"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Summarizer implementations for history compaction.
|
|
4
|
+
*
|
|
5
|
+
* Uses LLM to condense old conversation history into compact summaries.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.AnthropicSummarizer = void 0;
|
|
9
|
+
const hash_js_1 = require("../types/hash.js");
|
|
10
|
+
/**
|
|
11
|
+
* Anthropic-based history summarizer.
|
|
12
|
+
* Uses Claude Haiku for cost-effective summarization.
|
|
13
|
+
*/
|
|
14
|
+
class AnthropicSummarizer {
|
|
15
|
+
anthropicClient;
|
|
16
|
+
constructor(anthropicClient) {
|
|
17
|
+
this.anthropicClient = anthropicClient;
|
|
18
|
+
}
|
|
19
|
+
async summarize(blocks, targetTokens) {
|
|
20
|
+
// Render blocks to text (simplified - just stringify payload)
|
|
21
|
+
const messages = blocks.map((block) => {
|
|
22
|
+
const payload = block.payload;
|
|
23
|
+
if (payload.messages && Array.isArray(payload.messages)) {
|
|
24
|
+
return payload.messages
|
|
25
|
+
.map((m) => `${m.role}: ${JSON.stringify(m.content)}`)
|
|
26
|
+
.join('\n');
|
|
27
|
+
}
|
|
28
|
+
return JSON.stringify(payload);
|
|
29
|
+
});
|
|
30
|
+
const text = messages.join('\n\n');
|
|
31
|
+
// Call Anthropic with summarization prompt
|
|
32
|
+
const response = await this.anthropicClient.messages.create({
|
|
33
|
+
model: 'claude-haiku-4-5',
|
|
34
|
+
max_tokens: targetTokens,
|
|
35
|
+
messages: [
|
|
36
|
+
{
|
|
37
|
+
role: 'user',
|
|
38
|
+
content: `Summarize the following conversation history concisely, preserving key context and decisions:\n\n${text}`,
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
const summary = response.content[0].type === 'text' ? response.content[0].text : '';
|
|
43
|
+
// Create summary block with provenance
|
|
44
|
+
const summaryPayload = {
|
|
45
|
+
summary,
|
|
46
|
+
originalBlockCount: blocks.length,
|
|
47
|
+
summarizedAt: Date.now(),
|
|
48
|
+
};
|
|
49
|
+
const firstBlock = blocks[0];
|
|
50
|
+
const blockHash = (0, hash_js_1.computeBlockHash)({
|
|
51
|
+
...firstBlock.meta,
|
|
52
|
+
source: `${firstBlock.meta.source ?? 'unknown'}:summarized`,
|
|
53
|
+
}, summaryPayload);
|
|
54
|
+
return {
|
|
55
|
+
blockHash,
|
|
56
|
+
meta: {
|
|
57
|
+
...firstBlock.meta,
|
|
58
|
+
kind: 'history',
|
|
59
|
+
source: `${firstBlock.meta.source ?? 'unknown'}:summarized`,
|
|
60
|
+
tags: [...(firstBlock.meta.tags ?? []), 'summarized'],
|
|
61
|
+
createdAt: Date.now(),
|
|
62
|
+
},
|
|
63
|
+
payload: summaryPayload,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.AnthropicSummarizer = AnthropicSummarizer;
|
|
68
|
+
//# sourceMappingURL=summarizer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"summarizer.js","sourceRoot":"","sources":["../../src/pipeline/summarizer.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAKH,8CAAoD;AAEpD;;;GAGG;AACH,MAAa,mBAAmB;IACV;IAApB,YAAoB,eAA0B;QAA1B,oBAAe,GAAf,eAAe,CAAW;IAAG,CAAC;IAElD,KAAK,CAAC,SAAS,CAAC,MAAsB,EAAE,YAAoB;QAC1D,8DAA8D;QAC9D,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;YACpC,MAAM,OAAO,GAAG,KAAK,CAAC,OAAc,CAAC;YACrC,IAAI,OAAO,CAAC,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;gBACxD,OAAO,OAAO,CAAC,QAAQ;qBACpB,GAAG,CAAC,CAAC,CAAM,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC;qBAC1D,IAAI,CAAC,IAA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AI,EAAE,CAAC,EAAE,YAAY,CAAC;gBACrD,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;aACtB;YACD,OAAO,EAAE,cAAc;SACxB,CAAC;IACJ,CAAC;CACF;AA5DD,kDA4DC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default context policy for general-purpose LLM conversations.
|
|
3
|
+
*
|
|
4
|
+
* This policy provides sensible defaults for:
|
|
5
|
+
* - Token budget: 180K context window with 8K completion reserve
|
|
6
|
+
* - Caching: Provider-native caching with breakpoints for pinned/reference content
|
|
7
|
+
* - Compaction: Tool output pruning + history summarization
|
|
8
|
+
* - Attachments: 10K token budget with purpose-based selection
|
|
9
|
+
* - Sensitivity: Public content only with redaction for restricted content
|
|
10
|
+
*/
|
|
11
|
+
import type { ContextPolicy } from '../types/policy.js';
|
|
12
|
+
/**
|
|
13
|
+
* Default context policy configuration.
|
|
14
|
+
*
|
|
15
|
+
* Use this as a starting point for most conversational workflows.
|
|
16
|
+
* Override specific properties as needed for your use case.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { DEFAULT_POLICY, ContextBuilder } from '@foundry/context';
|
|
21
|
+
*
|
|
22
|
+
* const builder = new ContextBuilder({
|
|
23
|
+
* ...DEFAULT_POLICY,
|
|
24
|
+
* contextWindow: 200_000, // Override for larger context
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare const DEFAULT_POLICY: ContextPolicy;
|
|
29
|
+
//# sourceMappingURL=default-policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-policy.d.ts","sourceRoot":"","sources":["../../src/policies/default-policy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAQxD;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,cAAc,EAAE,aAiC5B,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Default context policy for general-purpose LLM conversations.
|
|
4
|
+
*
|
|
5
|
+
* This policy provides sensible defaults for:
|
|
6
|
+
* - Token budget: 180K context window with 8K completion reserve
|
|
7
|
+
* - Caching: Provider-native caching with breakpoints for pinned/reference content
|
|
8
|
+
* - Compaction: Tool output pruning + history summarization
|
|
9
|
+
* - Attachments: 10K token budget with purpose-based selection
|
|
10
|
+
* - Sensitivity: Public content only with redaction for restricted content
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.DEFAULT_POLICY = void 0;
|
|
14
|
+
const policy_js_1 = require("../types/policy.js");
|
|
15
|
+
/**
|
|
16
|
+
* Default context policy configuration.
|
|
17
|
+
*
|
|
18
|
+
* Use this as a starting point for most conversational workflows.
|
|
19
|
+
* Override specific properties as needed for your use case.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { DEFAULT_POLICY, ContextBuilder } from '@foundry/context';
|
|
24
|
+
*
|
|
25
|
+
* const builder = new ContextBuilder({
|
|
26
|
+
* ...DEFAULT_POLICY,
|
|
27
|
+
* contextWindow: 200_000, // Override for larger context
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
exports.DEFAULT_POLICY = {
|
|
32
|
+
// Provider configuration
|
|
33
|
+
provider: 'anthropic',
|
|
34
|
+
modelId: 'claude-sonnet-4-5',
|
|
35
|
+
// Token budget
|
|
36
|
+
contextWindow: 180_000,
|
|
37
|
+
completionReserve: 8_000,
|
|
38
|
+
// Overflow handling
|
|
39
|
+
overflowStrategy: 'compact',
|
|
40
|
+
// Block priorities
|
|
41
|
+
kindPriorities: policy_js_1.DEFAULT_KIND_PRIORITIES,
|
|
42
|
+
// Compaction configuration
|
|
43
|
+
compaction: {
|
|
44
|
+
...policy_js_1.DEFAULT_COMPACTION_CONFIG,
|
|
45
|
+
// Tool output pruning
|
|
46
|
+
pruneToolOutputs: true,
|
|
47
|
+
maxToolOutputAge: 3600, // 1 hour
|
|
48
|
+
maxToolOutputsPerKind: 10,
|
|
49
|
+
// History summarization
|
|
50
|
+
summarizeHistory: true,
|
|
51
|
+
maxHistoryMessages: 20,
|
|
52
|
+
},
|
|
53
|
+
// Sensitivity filtering
|
|
54
|
+
sensitivity: policy_js_1.DEFAULT_SENSITIVITY_CONFIG,
|
|
55
|
+
// Attachment selection
|
|
56
|
+
attachments: policy_js_1.DEFAULT_ATTACHMENT_POLICY,
|
|
57
|
+
};
|
|
58
|
+
//# sourceMappingURL=default-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-policy.js","sourceRoot":"","sources":["../../src/policies/default-policy.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAGH,kDAK4B;AAE5B;;;;;;;;;;;;;;;GAeG;AACU,QAAA,cAAc,GAAkB;IAC3C,yBAAyB;IACzB,QAAQ,EAAE,WAAW;IACrB,OAAO,EAAE,mBAAmB;IAE5B,eAAe;IACf,aAAa,EAAE,OAAO;IACtB,iBAAiB,EAAE,KAAK;IAExB,oBAAoB;IACpB,gBAAgB,EAAE,SAAS;IAE3B,mBAAmB;IACnB,cAAc,EAAE,mCAAuB;IAEvC,2BAA2B;IAC3B,UAAU,EAAE;QACV,GAAG,qCAAyB;QAC5B,sBAAsB;QACtB,gBAAgB,EAAE,IAAI;QACtB,gBAAgB,EAAE,IAAI,EAAE,SAAS;QACjC,qBAAqB,EAAE,EAAE;QAEzB,wBAAwB;QACxB,gBAAgB,EAAE,IAAI;QACtB,kBAAkB,EAAE,EAAE;KACvB;IAED,wBAAwB;IACxB,WAAW,EAAE,sCAA0B;IAEvC,uBAAuB;IACvB,WAAW,EAAE,qCAAyB;CACvC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/policies/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Policy configuration exports.
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
17
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
__exportStar(require("./default-policy.js"), exports);
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/policies/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;AAEH,sDAAoC"}
|