@strands-agents/sdk 1.16.0 → 1.17.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 +1 -1
- package/dist/src/agent/agent.d.ts.map +1 -1
- package/dist/src/agent/agent.js +10 -0
- package/dist/src/agent/agent.js.map +1 -1
- package/dist/src/context-manager/context-manager.d.ts +22 -1
- package/dist/src/context-manager/context-manager.d.ts.map +1 -1
- package/dist/src/context-manager/context-manager.js +57 -3
- package/dist/src/context-manager/context-manager.js.map +1 -1
- package/dist/src/context-manager/methods/summarize.d.ts +2 -2
- package/dist/src/context-manager/methods/summarize.d.ts.map +1 -1
- package/dist/src/context-manager/methods/summarize.js +0 -2
- package/dist/src/context-manager/methods/summarize.js.map +1 -1
- package/dist/src/context-manager/methods/truncate.d.ts +2 -2
- package/dist/src/context-manager/methods/truncate.d.ts.map +1 -1
- package/dist/src/context-manager/methods/truncate.js +0 -2
- package/dist/src/context-manager/methods/truncate.js.map +1 -1
- package/dist/src/context-manager/modes/agentic/agentic-context.d.ts +2 -2
- package/dist/src/context-manager/retrieval-tool.d.ts +29 -0
- package/dist/src/context-manager/retrieval-tool.d.ts.map +1 -0
- package/dist/src/context-manager/retrieval-tool.js +98 -0
- package/dist/src/context-manager/retrieval-tool.js.map +1 -0
- package/dist/src/context-manager/stash.d.ts +107 -0
- package/dist/src/context-manager/stash.d.ts.map +1 -0
- package/dist/src/context-manager/stash.js +186 -0
- package/dist/src/context-manager/stash.js.map +1 -0
- package/dist/src/context-manager/strategies/offload/base.d.ts +10 -6
- package/dist/src/context-manager/strategies/offload/base.d.ts.map +1 -1
- package/dist/src/context-manager/strategies/offload/base.js +21 -9
- package/dist/src/context-manager/strategies/offload/base.js.map +1 -1
- package/dist/src/context-manager/strategies/offload/drop.d.ts +2 -2
- package/dist/src/context-manager/strategies/offload/drop.d.ts.map +1 -1
- package/dist/src/context-manager/strategies/offload/drop.js +6 -4
- package/dist/src/context-manager/strategies/offload/drop.js.map +1 -1
- package/dist/src/context-manager/strategies/offload/index.d.ts +1 -1
- package/dist/src/context-manager/strategies/offload/index.js +1 -1
- package/dist/src/context-manager/strategies/offload/summarize.d.ts +2 -2
- package/dist/src/context-manager/strategies/offload/summarize.d.ts.map +1 -1
- package/dist/src/context-manager/strategies/offload/summarize.js +16 -18
- package/dist/src/context-manager/strategies/offload/summarize.js.map +1 -1
- package/dist/src/context-manager/strategies/offload/truncate.d.ts +2 -2
- package/dist/src/context-manager/strategies/offload/truncate.d.ts.map +1 -1
- package/dist/src/context-manager/strategies/offload/truncate.js +33 -4
- package/dist/src/context-manager/strategies/offload/truncate.js.map +1 -1
- package/dist/src/context-manager/types.d.ts +35 -1
- package/dist/src/context-manager/types.d.ts.map +1 -1
- package/dist/src/experimental/index.d.ts +6 -0
- package/dist/src/experimental/index.d.ts.map +1 -1
- package/dist/src/experimental/index.js +3 -0
- package/dist/src/experimental/index.js.map +1 -1
- package/dist/src/index.d.ts +3 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcp/client.d.ts +3 -2
- package/dist/src/mcp/client.d.ts.map +1 -1
- package/dist/src/mcp/client.js +3 -2
- package/dist/src/mcp/client.js.map +1 -1
- package/dist/src/mcp/config.d.ts +11 -1
- package/dist/src/mcp/config.d.ts.map +1 -1
- package/dist/src/mcp/config.js.map +1 -1
- package/dist/src/mcp/config.node.d.ts +3 -2
- package/dist/src/mcp/config.node.d.ts.map +1 -1
- package/dist/src/mcp/config.node.js +6 -3
- package/dist/src/mcp/config.node.js.map +1 -1
- package/dist/src/mcp/index.d.ts +1 -1
- package/dist/src/mcp/index.d.ts.map +1 -1
- package/dist/src/models/openai/cache.d.ts +28 -1
- package/dist/src/models/openai/cache.d.ts.map +1 -1
- package/dist/src/models/openai/cache.js +37 -13
- package/dist/src/models/openai/cache.js.map +1 -1
- package/dist/src/models/routing/classifier-strategy.d.ts +87 -0
- package/dist/src/models/routing/classifier-strategy.d.ts.map +1 -0
- package/dist/src/models/routing/classifier-strategy.js +285 -0
- package/dist/src/models/routing/classifier-strategy.js.map +1 -0
- package/dist/src/models/routing/index.d.ts +2 -0
- package/dist/src/models/routing/index.d.ts.map +1 -1
- package/dist/src/models/routing/index.js +1 -0
- package/dist/src/models/routing/index.js.map +1 -1
- package/dist/src/models/routing/router.d.ts +14 -2
- package/dist/src/models/routing/router.d.ts.map +1 -1
- package/dist/src/models/routing/router.js +43 -8
- package/dist/src/models/routing/router.js.map +1 -1
- package/dist/src/models/vercel.d.ts +9 -1
- package/dist/src/models/vercel.d.ts.map +1 -1
- package/dist/src/models/vercel.js +248 -7
- package/dist/src/models/vercel.js.map +1 -1
- package/dist/src/session/session-manager.d.ts +5 -1
- package/dist/src/session/session-manager.d.ts.map +1 -1
- package/dist/src/session/session-manager.js +51 -1
- package/dist/src/session/session-manager.js.map +1 -1
- package/dist/src/session/snapshot-storage-adapter.d.ts.map +1 -1
- package/dist/src/session/snapshot-storage-adapter.js +1 -0
- package/dist/src/session/snapshot-storage-adapter.js.map +1 -1
- package/dist/src/storage/in-memory-storage.d.ts +2 -0
- package/dist/src/storage/in-memory-storage.d.ts.map +1 -1
- package/dist/src/storage/in-memory-storage.js +2 -1
- package/dist/src/storage/in-memory-storage.js.map +1 -1
- package/dist/src/storage/storage.d.ts +8 -0
- package/dist/src/storage/storage.d.ts.map +1 -1
- package/dist/src/storage/storage.js +18 -0
- package/dist/src/storage/storage.js.map +1 -1
- package/dist/src/telemetry/tracer.d.ts.map +1 -1
- package/dist/src/telemetry/tracer.js +4 -2
- package/dist/src/telemetry/tracer.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/src/types/agent.d.ts +9 -0
- package/dist/src/types/agent.d.ts.map +1 -1
- package/dist/src/types/agent.js.map +1 -1
- package/package.json +2 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"truncate.js","sourceRoot":"","sources":["../../../../src/context-manager/methods/truncate.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"truncate.js","sourceRoot":"","sources":["../../../../src/context-manager/methods/truncate.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AAG/E,MAAM,CAAC,MAAM,gBAAgB,GAAG,aAAa,CAAA;AAE7C,MAAM,sBAAsB,GAAG,IAAI,CAAA;AACnC,MAAM,eAAe,GAAG,CAAC,CAAA;AAezB;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,QAAgB,EAAE,UAAkB,EAAE,MAAuB;IACxF,MAAM,aAAa,GACjB,OAAO,MAAM,EAAE,aAAa,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,aAAa,CAAC;QAChF,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,aAAa,CAAC;QACnC,CAAC,CAAC,sBAAsB,CAAA;IAC5B,MAAM,YAAY,GAAG,aAAa,GAAG,eAAe,CAAA;IACpD,MAAM,WAAW,GAAG,MAAM,EAAE,OAAO,IAAI,UAAU,CAAA;IACjD,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAA;IAElC,IAAI,UAAU,IAAI,YAAY,EAAE,CAAC;QAC/B,OAAO,QAAQ,CAAA;IACjB,CAAC;IAED,MAAM,SAAS,GAAG,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,CAAA;IAClE,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,SAAS,CAAC,CAAA;IACtD,MAAM,SAAS,GAAG,YAAY,GAAG,SAAS,CAAA;IAC1C,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,CAAA;IACzC,MA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AAI,CAAC,OAAO,CAAC,CAAA;QACtB,CAAC;IACH,CAAC;IAED,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,KAAK,CAAA;IACd,CAAC;IAED,MAAM,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACnC,MAAM,OAAO,GAAG,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAE9D,IAAI,OAAO,KAAK,QAAQ,EAAE,CAAC;QACzB,OAAO,KAAK,CAAA;IACd,CAAC;IAED,OAAO,IAAI,eAAe,CAAC;QACzB,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,OAAO,EAAE,CAAC,IAAI,SAAS,CAAC,OAAO,CAAC,EAAE,GAAG,MAAM,CAAC;KAC7C,CAAC,CAAA;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAgB,EAAE,MAAuB;IACzE,MAAM,OAAO,GAAG,YAAY,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,EAAE,MAAM,CAAC,CAAA;IACnD,IAAI,OAAO,KAAK,KAAK,CAAC,IAAI,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAA;IACd,CAAC;IACD,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC,CAAA;AAC/B,CAAC"}
|
|
@@ -8,11 +8,11 @@ import type { MiddlewareInputHandler } from '../../../middleware/types.js';
|
|
|
8
8
|
export declare const summarizeContextTool: import("../../../index.js").InvokableTool<{
|
|
9
9
|
keepRecent?: number | undefined;
|
|
10
10
|
summaryRatio?: number | undefined;
|
|
11
|
-
messageType?: "tools" | "
|
|
11
|
+
messageType?: "tools" | "all" | "messages" | undefined;
|
|
12
12
|
}, string>;
|
|
13
13
|
export declare const truncateContextTool: import("../../../index.js").InvokableTool<{
|
|
14
14
|
keepRecent?: number | undefined;
|
|
15
|
-
messageType?: "tools" | "
|
|
15
|
+
messageType?: "tools" | "all" | "messages" | undefined;
|
|
16
16
|
}, string>;
|
|
17
17
|
export declare function createTokenUsageMiddleware(): MiddlewareInputHandler<InvokeModelContext>;
|
|
18
18
|
export declare const pinContextTool: import("../../../index.js").InvokableTool<{
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retrieval tool for accessing stashed (L1) content.
|
|
3
|
+
*
|
|
4
|
+
* Registered automatically when the ContextManager has storage configured.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
import type { Tool } from '../tools/tool.js';
|
|
9
|
+
import { Message } from '../types/messages.js';
|
|
10
|
+
import type { Stash } from './stash.js';
|
|
11
|
+
export declare const RETRIEVAL_TOOL_NAME = "retrieve_context";
|
|
12
|
+
/**
|
|
13
|
+
* Creates the retrieval tool for the given stash.
|
|
14
|
+
*
|
|
15
|
+
* @param stash - The stash instance to retrieve from
|
|
16
|
+
* @param maxResultTokens - Maximum tokens in the retrieval result
|
|
17
|
+
* @returns A Tool that can retrieve stashed content
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export declare function createRetrievalTool(stash: Stash, maxResultTokens?: number): Tool;
|
|
22
|
+
/**
|
|
23
|
+
* Tracks toolUseIds from retrieval tool invocations so their results
|
|
24
|
+
* can be excluded from stashing (prevents retrieval loops).
|
|
25
|
+
*
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export declare function trackRetrievalToolUseIds(message: Message, skipSet: Set<string>): void;
|
|
29
|
+
//# sourceMappingURL=retrieval-tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retrieval-tool.d.ts","sourceRoot":"","sources":["../../../src/context-manager/retrieval-tool.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAMH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AAC5C,OAAO,EAAE,OAAO,EAAgB,MAAM,sBAAsB,CAAA;AAC5D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAEvC,eAAO,MAAM,mBAAmB,qBAAqB,CAAA;AAkBrD;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,KAAK,EAAE,eAAe,CAAC,EAAE,MAAM,GAAG,IAAI,CA2ChF;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI,CAOrF"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retrieval tool for accessing stashed (L1) content.
|
|
3
|
+
*
|
|
4
|
+
* Registered automatically when the ContextManager has storage configured.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import { tool } from '../tools/tool-factory.js';
|
|
10
|
+
import { isSearchableContent, searchContent } from '../vended-plugins/context-offloader/search.js';
|
|
11
|
+
import { Message, ToolUseBlock } from '../types/messages.js';
|
|
12
|
+
export const RETRIEVAL_TOOL_NAME = 'retrieve_context';
|
|
13
|
+
const DEFAULT_MAX_RESULT_TOKENS = 10_000;
|
|
14
|
+
const CHARS_PER_TOKEN = 4;
|
|
15
|
+
const retrievalInputSchema = z.object({
|
|
16
|
+
reference: z.string().describe('The reference key from the offload placeholder.'),
|
|
17
|
+
pattern: z.string().optional().describe('Regex or keyword to grep for. Returns matching lines with context.'),
|
|
18
|
+
line_range: z
|
|
19
|
+
.object({
|
|
20
|
+
start: z.number().int().min(1).describe('First line to return (1-indexed).'),
|
|
21
|
+
end: z.number().int().min(1).describe('Last line to return (1-indexed).'),
|
|
22
|
+
})
|
|
23
|
+
.optional()
|
|
24
|
+
.describe('Return only this span of lines.'),
|
|
25
|
+
context_lines: z.number().int().min(0).optional().describe('Lines of context around each match (default: 5).'),
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* Creates the retrieval tool for the given stash.
|
|
29
|
+
*
|
|
30
|
+
* @param stash - The stash instance to retrieve from
|
|
31
|
+
* @param maxResultTokens - Maximum tokens in the retrieval result
|
|
32
|
+
* @returns A Tool that can retrieve stashed content
|
|
33
|
+
*
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
export function createRetrievalTool(stash, maxResultTokens) {
|
|
37
|
+
const maxChars = (maxResultTokens ?? DEFAULT_MAX_RESULT_TOKENS) * CHARS_PER_TOKEN;
|
|
38
|
+
return tool({
|
|
39
|
+
name: RETRIEVAL_TOOL_NAME,
|
|
40
|
+
description: 'Retrieve content that was offloaded from context.\n\n' +
|
|
41
|
+
'When content is offloaded (truncated, dropped, or summarized), the original is ' +
|
|
42
|
+
'persisted and a reference key is left in its place. Use this tool with that ' +
|
|
43
|
+
'reference to access the original content.\n\n' +
|
|
44
|
+
'Options:\n' +
|
|
45
|
+
' - pattern: regex/keyword to find matching lines with context\n' +
|
|
46
|
+
' - line_range: { start, end } to read a specific span\n' +
|
|
47
|
+
' - Without pattern/line_range: returns the full original content\n\n' +
|
|
48
|
+
'Examples:\n' +
|
|
49
|
+
' { reference: "1_toolu_abc_0", pattern: "error" }\n' +
|
|
50
|
+
' { reference: "1_toolu_abc_0", line_range: { start: 10, end: 25 } }\n' +
|
|
51
|
+
' { reference: "1_toolu_abc_0" }',
|
|
52
|
+
inputSchema: retrievalInputSchema,
|
|
53
|
+
callback: async (input) => {
|
|
54
|
+
const result = await stash.retrieve(input.reference);
|
|
55
|
+
if (!result) {
|
|
56
|
+
return `Error: reference not found: ${input.reference}`;
|
|
57
|
+
}
|
|
58
|
+
if (!input.pattern && !input.line_range) {
|
|
59
|
+
return result.data;
|
|
60
|
+
}
|
|
61
|
+
const text = extractText(result.data);
|
|
62
|
+
if (!text || !isSearchableContent('text/plain')) {
|
|
63
|
+
return `Error: cannot search non-text content. Omit pattern/line_range to retrieve full content.`;
|
|
64
|
+
}
|
|
65
|
+
const contextLines = input.context_lines ?? 5;
|
|
66
|
+
return searchContent(text, { pattern: input.pattern, line_range: input.line_range, context_lines: contextLines }, maxChars);
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Tracks toolUseIds from retrieval tool invocations so their results
|
|
72
|
+
* can be excluded from stashing (prevents retrieval loops).
|
|
73
|
+
*
|
|
74
|
+
* @internal
|
|
75
|
+
*/
|
|
76
|
+
export function trackRetrievalToolUseIds(message, skipSet) {
|
|
77
|
+
if (message.role !== 'assistant')
|
|
78
|
+
return;
|
|
79
|
+
for (const block of message.content) {
|
|
80
|
+
if (block instanceof ToolUseBlock && block.name === RETRIEVAL_TOOL_NAME) {
|
|
81
|
+
skipSet.add(block.toolUseId);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function extractText(data) {
|
|
86
|
+
if (typeof data === 'string')
|
|
87
|
+
return data;
|
|
88
|
+
if (data && typeof data === 'object') {
|
|
89
|
+
if ('text' in data && typeof data.text === 'string') {
|
|
90
|
+
return data.text;
|
|
91
|
+
}
|
|
92
|
+
if ('json' in data) {
|
|
93
|
+
return JSON.stringify(data.json, null, 2);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=retrieval-tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retrieval-tool.js","sourceRoot":"","sources":["../../../src/context-manager/retrieval-tool.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAA;AAC/C,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,+CAA+C,CAAA;AAGlG,OAAO,EAAE,OAAO,EAA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,CAAC;AAED,SAAS,WAAW,CAAC,IAAa;IAChC,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAA;IACzC,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QACrC,IAAI,MAAM,IAAI,IAAI,IAAI,OAAQ,IAAgC,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACjF,OAAQ,IAAgC,CAAC,IAAc,CAAA;QACzD,CAAC;QACD,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC,SAAS,CAAE,IAAgC,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;QACxE,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L1 stash: durable storage for offloaded context content.
|
|
3
|
+
*
|
|
4
|
+
* When the ContextManager offloads content from the context window, the
|
|
5
|
+
* original is persisted here so the agent can retrieve it on demand via the
|
|
6
|
+
* retrieval tool.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
import { type Storage } from '../storage/storage.js';
|
|
11
|
+
import { Message } from '../types/messages.js';
|
|
12
|
+
import type { ContentBlock } from '../types/messages.js';
|
|
13
|
+
import type { JSONValue } from '../types/json.js';
|
|
14
|
+
/** @internal */
|
|
15
|
+
export declare const STASH_PREFIX = "context";
|
|
16
|
+
/** Format stash refs for display in placeholders. Returns '' when refs is empty. */
|
|
17
|
+
export declare function formatStashRefs(refs: string[]): string;
|
|
18
|
+
/**
|
|
19
|
+
* Wraps a Storage backend with key management and content framing for the
|
|
20
|
+
* ContextManager's L1 stash.
|
|
21
|
+
*
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export declare class Stash {
|
|
25
|
+
private readonly _storage;
|
|
26
|
+
private readonly _baseStorage;
|
|
27
|
+
private readonly _sessionId;
|
|
28
|
+
/** Name of the base storage constructor, for diagnostic logging. */
|
|
29
|
+
readonly storageTypeName: string;
|
|
30
|
+
constructor(storage: Storage, sessionId: string, agentId: string);
|
|
31
|
+
/**
|
|
32
|
+
* Store a serialized block and return a deterministic reference key.
|
|
33
|
+
*
|
|
34
|
+
* Keys are deterministic (`<id>_<blockIndex>`) so they can be recomputed
|
|
35
|
+
* via {@link refsFor} after a process restart or snapshot restore.
|
|
36
|
+
*
|
|
37
|
+
* @param id - Identifier component for the key (e.g. toolUseId)
|
|
38
|
+
* @param blockIndex - Index of the block within the tool result
|
|
39
|
+
* @param data - Serialized bytes to persist
|
|
40
|
+
* @returns Reference key for retrieval
|
|
41
|
+
*/
|
|
42
|
+
store(id: string, blockIndex: number, data: Uint8Array): Promise<string>;
|
|
43
|
+
/**
|
|
44
|
+
* Compute the deterministic reference keys for a content block.
|
|
45
|
+
*
|
|
46
|
+
* For a {@link ToolResultBlock}, returns one key per inner content item
|
|
47
|
+
* (`<toolUseId>_<i>`). For any other block, returns a single key
|
|
48
|
+
* (`<trackingId>_<blockIndex>`).
|
|
49
|
+
*
|
|
50
|
+
* @param block - The content block
|
|
51
|
+
* @param message - The message containing the block
|
|
52
|
+
* @param blockIndex - Index of the block within the message
|
|
53
|
+
* @returns Array of reference keys
|
|
54
|
+
*/
|
|
55
|
+
refsFor(block: ContentBlock, message: Message, blockIndex: number): string[];
|
|
56
|
+
/**
|
|
57
|
+
* Eagerly stash all content from a message on arrival.
|
|
58
|
+
* Called via MessageAddedEvent so content is persisted before any strategy runs.
|
|
59
|
+
*
|
|
60
|
+
* @param message - The message whose content should be persisted
|
|
61
|
+
* @param skipToolUseIds - ToolUseIds to skip (e.g. retrieval tool results)
|
|
62
|
+
*/
|
|
63
|
+
storeMessage(message: Message, skipToolUseIds?: ReadonlySet<string>): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Retrieve previously stashed content as its serialized JSON form.
|
|
66
|
+
*
|
|
67
|
+
* @param reference - Key returned by a previous store call
|
|
68
|
+
* @returns The deserialized block data (from `toJSON()`), or null if not found
|
|
69
|
+
*/
|
|
70
|
+
retrieve(reference: string): Promise<{
|
|
71
|
+
data: unknown;
|
|
72
|
+
} | null>;
|
|
73
|
+
/**
|
|
74
|
+
* List all stashed references.
|
|
75
|
+
*/
|
|
76
|
+
list(): Promise<string[]>;
|
|
77
|
+
/**
|
|
78
|
+
* Delete a stashed entry.
|
|
79
|
+
*/
|
|
80
|
+
delete(reference: string): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Delete all entries in this stash instance.
|
|
83
|
+
*/
|
|
84
|
+
clear(): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Delete all stash data for this session across all agents.
|
|
87
|
+
*
|
|
88
|
+
* Unlike {@link clear}, which is scoped to this agent's namespace,
|
|
89
|
+
* this scans `context/<sessionId>/` on the base storage to catch data
|
|
90
|
+
* from every agent that wrote to the session.
|
|
91
|
+
*/
|
|
92
|
+
clearSession(): Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Serialize all stash entries into a plain object for snapshot persistence.
|
|
95
|
+
*
|
|
96
|
+
* @returns Map of reference keys to their stored JSON values
|
|
97
|
+
*/
|
|
98
|
+
takeSnapshot(): Promise<Record<string, JSONValue>>;
|
|
99
|
+
/**
|
|
100
|
+
* Restore stash entries from a previously captured snapshot.
|
|
101
|
+
*
|
|
102
|
+
* @param entries - Map of reference keys to their JSON values (from {@link takeSnapshot})
|
|
103
|
+
*/
|
|
104
|
+
loadSnapshot(entries: Record<string, JSONValue>): Promise<void>;
|
|
105
|
+
private _storeToolResult;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=stash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stash.d.ts","sourceRoot":"","sources":["../../../src/context-manager/stash.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAiC,KAAK,OAAO,EAAE,MAAM,uBAAuB,CAA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MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK9C;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAK5B;;;;;;OAMG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAMnC;;;;OAIG;IACG,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAYxD;;;;OAIG;IACG,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;YAIvD,gBAAgB;CAM/B"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L1 stash: durable storage for offloaded context content.
|
|
3
|
+
*
|
|
4
|
+
* When the ContextManager offloads content from the context window, the
|
|
5
|
+
* original is persisted here so the agent can retrieve it on demand via the
|
|
6
|
+
* retrieval tool.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
import { namespace as namespaceStorage } from '../storage/storage.js';
|
|
11
|
+
import { Message, ToolResultBlock, ToolUseBlock, CachePointBlock, ReasoningBlock } from '../types/messages.js';
|
|
12
|
+
import { logger } from '../logging/logger.js';
|
|
13
|
+
/** @internal */
|
|
14
|
+
export const STASH_PREFIX = 'context';
|
|
15
|
+
function encode(value) {
|
|
16
|
+
return new TextEncoder().encode(JSON.stringify(value));
|
|
17
|
+
}
|
|
18
|
+
function decode(bytes) {
|
|
19
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
20
|
+
}
|
|
21
|
+
/** Format stash refs for display in placeholders. Returns '' when refs is empty. */
|
|
22
|
+
export function formatStashRefs(refs) {
|
|
23
|
+
if (refs.length === 0)
|
|
24
|
+
return '';
|
|
25
|
+
if (refs.length === 1)
|
|
26
|
+
return ` ref: ${refs[0]}`;
|
|
27
|
+
return ` refs: ${refs.join(', ')}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Wraps a Storage backend with key management and content framing for the
|
|
31
|
+
* ContextManager's L1 stash.
|
|
32
|
+
*
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
export class Stash {
|
|
36
|
+
_storage;
|
|
37
|
+
_baseStorage;
|
|
38
|
+
_sessionId;
|
|
39
|
+
/** Name of the base storage constructor, for diagnostic logging. */
|
|
40
|
+
storageTypeName;
|
|
41
|
+
constructor(storage, sessionId, agentId) {
|
|
42
|
+
this._baseStorage = storage;
|
|
43
|
+
this._sessionId = sessionId;
|
|
44
|
+
this._storage = namespaceStorage(storage, `${STASH_PREFIX}/${sessionId}/scopes/agent/${agentId}`);
|
|
45
|
+
this.storageTypeName = storage.constructor.name || 'unknown';
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Store a serialized block and return a deterministic reference key.
|
|
49
|
+
*
|
|
50
|
+
* Keys are deterministic (`<id>_<blockIndex>`) so they can be recomputed
|
|
51
|
+
* via {@link refsFor} after a process restart or snapshot restore.
|
|
52
|
+
*
|
|
53
|
+
* @param id - Identifier component for the key (e.g. toolUseId)
|
|
54
|
+
* @param blockIndex - Index of the block within the tool result
|
|
55
|
+
* @param data - Serialized bytes to persist
|
|
56
|
+
* @returns Reference key for retrieval
|
|
57
|
+
*/
|
|
58
|
+
async store(id, blockIndex, data) {
|
|
59
|
+
const key = `${id}_${blockIndex}`;
|
|
60
|
+
await this._storage.write(key, data);
|
|
61
|
+
return key;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Compute the deterministic reference keys for a content block.
|
|
65
|
+
*
|
|
66
|
+
* For a {@link ToolResultBlock}, returns one key per inner content item
|
|
67
|
+
* (`<toolUseId>_<i>`). For any other block, returns a single key
|
|
68
|
+
* (`<trackingId>_<blockIndex>`).
|
|
69
|
+
*
|
|
70
|
+
* @param block - The content block
|
|
71
|
+
* @param message - The message containing the block
|
|
72
|
+
* @param blockIndex - Index of the block within the message
|
|
73
|
+
* @returns Array of reference keys
|
|
74
|
+
*/
|
|
75
|
+
refsFor(block, message, blockIndex) {
|
|
76
|
+
if (block instanceof ToolResultBlock) {
|
|
77
|
+
return Array.from({ length: block.content.length }, (_, index) => `${block.toolUseId}_${index}`);
|
|
78
|
+
}
|
|
79
|
+
return [`${message.trackingId}_${blockIndex}`];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Eagerly stash all content from a message on arrival.
|
|
83
|
+
* Called via MessageAddedEvent so content is persisted before any strategy runs.
|
|
84
|
+
*
|
|
85
|
+
* @param message - The message whose content should be persisted
|
|
86
|
+
* @param skipToolUseIds - ToolUseIds to skip (e.g. retrieval tool results)
|
|
87
|
+
*/
|
|
88
|
+
async storeMessage(message, skipToolUseIds) {
|
|
89
|
+
for (let blockIndex = 0; blockIndex < message.content.length; blockIndex++) {
|
|
90
|
+
const block = message.content[blockIndex];
|
|
91
|
+
if (block instanceof ToolResultBlock) {
|
|
92
|
+
if (skipToolUseIds?.has(block.toolUseId))
|
|
93
|
+
continue;
|
|
94
|
+
await this._storeToolResult(block).catch((error) => {
|
|
95
|
+
logger.debug(`toolUseId=<${block.toolUseId}>, error=<${error}> | failed to stash tool result`);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
else if (block instanceof ToolUseBlock || block instanceof CachePointBlock || block instanceof ReasoningBlock) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
try {
|
|
103
|
+
await this.store(message.trackingId, blockIndex, encode(block.toJSON()));
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
logger.debug(`trackingId=<${message.trackingId}>, error=<${error}> | failed to stash block`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Retrieve previously stashed content as its serialized JSON form.
|
|
113
|
+
*
|
|
114
|
+
* @param reference - Key returned by a previous store call
|
|
115
|
+
* @returns The deserialized block data (from `toJSON()`), or null if not found
|
|
116
|
+
*/
|
|
117
|
+
async retrieve(reference) {
|
|
118
|
+
const bytes = await this._storage.read(reference);
|
|
119
|
+
if (bytes === null)
|
|
120
|
+
return null;
|
|
121
|
+
return { data: decode(bytes) };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* List all stashed references.
|
|
125
|
+
*/
|
|
126
|
+
async list() {
|
|
127
|
+
return this._storage.list('');
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Delete a stashed entry.
|
|
131
|
+
*/
|
|
132
|
+
async delete(reference) {
|
|
133
|
+
await this._storage.delete(reference);
|
|
134
|
+
logger.debug(`reference=<${reference}> | stash entry deleted`);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Delete all entries in this stash instance.
|
|
138
|
+
*/
|
|
139
|
+
async clear() {
|
|
140
|
+
const keys = await this.list();
|
|
141
|
+
await Promise.all(keys.map((key) => this.delete(key)));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Delete all stash data for this session across all agents.
|
|
145
|
+
*
|
|
146
|
+
* Unlike {@link clear}, which is scoped to this agent's namespace,
|
|
147
|
+
* this scans `context/<sessionId>/` on the base storage to catch data
|
|
148
|
+
* from every agent that wrote to the session.
|
|
149
|
+
*/
|
|
150
|
+
async clearSession() {
|
|
151
|
+
const prefix = `${STASH_PREFIX}/${this._sessionId}/`;
|
|
152
|
+
const keys = await this._baseStorage.list(prefix);
|
|
153
|
+
await Promise.all(keys.map((key) => this._baseStorage.delete(key)));
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Serialize all stash entries into a plain object for snapshot persistence.
|
|
157
|
+
*
|
|
158
|
+
* @returns Map of reference keys to their stored JSON values
|
|
159
|
+
*/
|
|
160
|
+
async takeSnapshot() {
|
|
161
|
+
const keys = await this.list();
|
|
162
|
+
const results = await Promise.all(keys.map((key) => this.retrieve(key).then((result) => [key, result])));
|
|
163
|
+
const entries = {};
|
|
164
|
+
for (const [key, result] of results) {
|
|
165
|
+
if (result) {
|
|
166
|
+
entries[key] = result.data;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return entries;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Restore stash entries from a previously captured snapshot.
|
|
173
|
+
*
|
|
174
|
+
* @param entries - Map of reference keys to their JSON values (from {@link takeSnapshot})
|
|
175
|
+
*/
|
|
176
|
+
async loadSnapshot(entries) {
|
|
177
|
+
await Promise.all(Object.entries(entries).map(([key, data]) => this._storage.write(key, encode(data))));
|
|
178
|
+
}
|
|
179
|
+
async _storeToolResult(block) {
|
|
180
|
+
for (let blockIndex = 0; blockIndex < block.content.length; blockIndex++) {
|
|
181
|
+
const item = block.content[blockIndex];
|
|
182
|
+
await this.store(block.toolUseId, blockIndex, encode(item.toJSON()));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
//# sourceMappingURL=stash.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stash.js","sourceRoot":"","sources":["../../../src/context-manager/stash.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,SAAS,IAAI,gBAAgB,EAAgB,MAAM,uBAAuB,CAAA;AACnF,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAG9G,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAE7C,gBAAgB;AAChB,MAAM,CAAC,MAAM,YAAY,GAAG,SAAS,CAAA;AAErC,SAAS,MAAM,CAAC,KAAc;IAC5B,OAAO,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAA;AACxD,CAAC;AAED,SAAS,MAAM,CAAC,KAAiB;IAC/B,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CA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AC,CAAC,CAAA;QACtE,CAAC;IACH,CAAC;CACF"}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Base offload strategy and shared infrastructure.
|
|
3
|
-
*
|
|
4
|
-
* @internal
|
|
5
3
|
*/
|
|
6
|
-
import { Message,
|
|
4
|
+
import { Message, ToolResultBlock } from '../../../types/messages.js';
|
|
7
5
|
import type { ContentBlock } from '../../../types/messages.js';
|
|
8
6
|
import type { LocalAgent } from '../../../types/agent.js';
|
|
9
7
|
import type { ContextStrategy, ContextState } from '../../types.js';
|
|
8
|
+
import type { Stash } from '../../stash.js';
|
|
10
9
|
/**
|
|
11
10
|
* Target for offload operations. This union is intentionally extensible — new
|
|
12
11
|
* string-literal members can be added freely as new content categories emerge.
|
|
@@ -17,6 +16,8 @@ import type { ContextStrategy, ContextState } from '../../types.js';
|
|
|
17
16
|
* - `"userText"` — text blocks in user messages (excluding tool results)
|
|
18
17
|
* - `string[]` — tool results from specific tools, namespaced with `tool::` (e.g. `['tool::bash']`); prefix with `!` to exclude
|
|
19
18
|
* - `"*"` — all content in the context window (tool results + text blocks)
|
|
19
|
+
*
|
|
20
|
+
* @experimental
|
|
20
21
|
*/
|
|
21
22
|
export type OffloadTarget = '*' | 'toolResults' | 'toolResultErrors' | 'assistantText' | 'userText' | string[];
|
|
22
23
|
/**
|
|
@@ -30,6 +31,8 @@ export type OffloadTarget = '*' | 'toolResults' | 'toolResultErrors' | 'assistan
|
|
|
30
31
|
* When multiple strategies target the same content, they don't conflict — strategies
|
|
31
32
|
* run as an ordered pipeline, and once an earlier strategy shrinks a block, it falls
|
|
32
33
|
* below the next strategy's threshold and gets skipped automatically.
|
|
34
|
+
*
|
|
35
|
+
* @experimental
|
|
33
36
|
*/
|
|
34
37
|
export interface OffloadConditions {
|
|
35
38
|
/** Token threshold above which individual blocks are offloaded. */
|
|
@@ -100,10 +103,11 @@ export declare abstract class BaseOffloadStrategy implements ContextStrategy {
|
|
|
100
103
|
protected readonly _removalRatio: number;
|
|
101
104
|
protected readonly _includeFilter: Set<string> | undefined;
|
|
102
105
|
protected readonly _excludeFilter: Set<string> | undefined;
|
|
106
|
+
protected _stash: Stash | undefined;
|
|
103
107
|
constructor(target?: OffloadTarget, conditions?: OffloadConditions);
|
|
104
108
|
/** Whether this strategy operates at message-level (batch) vs per-block. */
|
|
105
109
|
protected get _isMessageLevel(): boolean;
|
|
106
|
-
init(agent: LocalAgent): void;
|
|
110
|
+
init(agent: LocalAgent, stash?: Stash): void;
|
|
107
111
|
apply(context: ContextState): Promise<boolean>;
|
|
108
112
|
/** Per-block execution: walk each message, transform individual blocks above threshold. */
|
|
109
113
|
private _applyPerBlock;
|
|
@@ -118,7 +122,7 @@ export declare abstract class BaseOffloadStrategy implements ContextStrategy {
|
|
|
118
122
|
/** Collect eligible messages for message-level operations, respecting preserveRecent, head-pin, and threshold. */
|
|
119
123
|
protected _getEligibleMessages(context: ContextState): Promise<Message[]>;
|
|
120
124
|
/** Transform a block. Return the replacement, or null to skip. */
|
|
121
|
-
protected abstract _replaceBlock(block:
|
|
125
|
+
protected abstract _replaceBlock(block: ContentBlock, tokens: number, message: Message, agent: LocalAgent, stashRefs: string[]): Promise<ContentBlock | null>;
|
|
122
126
|
}
|
|
123
127
|
/**
|
|
124
128
|
* Last-resort strategy that recomputes utilization and drops the oldest 20% of messages
|
|
@@ -133,7 +137,7 @@ export declare class EmergencyTruncateStrategy extends BaseOffloadStrategy {
|
|
|
133
137
|
readonly name = "offload:emergency-truncate";
|
|
134
138
|
protected readonly _removalRatio = 0.2;
|
|
135
139
|
constructor();
|
|
136
|
-
init(): void;
|
|
140
|
+
init(_agent: LocalAgent, stash?: Stash): void;
|
|
137
141
|
apply(context: ContextState): Promise<boolean>;
|
|
138
142
|
protected _replaceBlock(): Promise<ContentBlock | null>;
|
|
139
143
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../src/context-manager/strategies/offload/base.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../src/context-manager/strategies/offload/base.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,EACL,OAAO,EAEP,eAAe,EAIhB,MAAM,4BAA4B,CAAA;AACnC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAA;AAC9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AACnE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAG3C;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,aAAa,GAAG,GAAG,GAAG,aAAa,GAAG,kBAAkB,GAAG,eAAe,GAAG,UAAU,GAAG,MAAM,EAAE,CAAA;AAE9G;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,iBAAiB;IAChC,mEAAmE;IACnE,SAAS,CAAC,EAAE,MAAM,CAAA;IAElB,uEAAuE;IACvE,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB,kEAAkE;IAClE,cAAc,CAAC,EAAE,MAAM,CAAA;CACxB;AAED;;;GAGG;AACH,MAAM,WAAW,sBAAuB,SAAQ,eAAe;IAC7D,8DAA8D;IAC9D,IAAI,CAAC,UAAU,EAAE,iBAAiB,GAAG,eAAe,CAAA;CACrD;AAQD;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAWzE;AAED,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,eAAe,EACtB,MAAM,EAAE,aAAa,EACrB,WAAW,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAChC,iBAAiB,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,SAAS,EAC1C,iBAAiB,EA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FAAoF;IACpF,SAAS,CAAC,mBAAmB,CAAC,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO;IAgB/G,4CAA4C;cAC5B,gBAAgB,CAC9B,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,OAAO,EAAE,EACnB,WAAW,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAChC,KAAK,EAAE,UAAU,GAChB,OAAO,CAAC,OAAO,CAAC;IAoBnB,kHAAkH;cAClG,oBAAoB,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAyC/E,kEAAkE;IAClE,SAAS,CAAC,QAAQ,CAAC,aAAa,CAC9B,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,UAAU,EACjB,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;CAChC;AAID;;;;;;;;GAQG;AACH,qBAAa,yBAA0B,SAAQ,mBAAmB;IAChE,QAAQ,CAAC,IAAI,gCAA+B;IAC5C,mBAA4B,aAAa,OAAM;;IAMtC,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI;IAIvC,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC;cAQ7C,aAAa,IAAI,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;CAG9D"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Base offload strategy and shared infrastructure.
|
|
3
|
-
*
|
|
4
|
-
* @internal
|
|
5
3
|
*/
|
|
6
4
|
import { logger } from '../../../logging/logger.js';
|
|
7
5
|
import { MessageAddedEvent } from '../../../hooks/events.js';
|
|
8
|
-
import { Message, TextBlock, ToolResultBlock, ToolUseBlock } from '../../../types/messages.js';
|
|
6
|
+
import { Message, TextBlock, ToolResultBlock, ToolUseBlock, CachePointBlock, ReasoningBlock, } from '../../../types/messages.js';
|
|
7
|
+
import { RETRIEVAL_TOOL_NAME } from '../../retrieval-tool.js';
|
|
9
8
|
// --- Shared helpers ---
|
|
10
9
|
function finiteOrUndefined(value) {
|
|
11
10
|
return typeof value === 'number' && Number.isFinite(value) ? Math.max(0, value) : undefined;
|
|
@@ -202,6 +201,7 @@ export class BaseOffloadStrategy {
|
|
|
202
201
|
_removalRatio = 0.3;
|
|
203
202
|
_includeFilter;
|
|
204
203
|
_excludeFilter;
|
|
204
|
+
_stash;
|
|
205
205
|
constructor(target, conditions) {
|
|
206
206
|
if (Array.isArray(target) && target.length === 0) {
|
|
207
207
|
throw new Error('Empty array target matches nothing — provide at least one target');
|
|
@@ -218,7 +218,8 @@ export class BaseOffloadStrategy {
|
|
|
218
218
|
get _isMessageLevel() {
|
|
219
219
|
return this._utilizationThreshold !== undefined;
|
|
220
220
|
}
|
|
221
|
-
init(agent) {
|
|
221
|
+
init(agent, stash) {
|
|
222
|
+
this._stash = stash;
|
|
222
223
|
if (this._isMessageLevel)
|
|
223
224
|
return;
|
|
224
225
|
if (this._preserveRecent > 0)
|
|
@@ -230,6 +231,8 @@ export class BaseOffloadStrategy {
|
|
|
230
231
|
});
|
|
231
232
|
}
|
|
232
233
|
async apply(context) {
|
|
234
|
+
if (context.stash)
|
|
235
|
+
this._stash = context.stash;
|
|
233
236
|
if (this._isMessageLevel) {
|
|
234
237
|
if (context.utilization < this._utilizationThreshold)
|
|
235
238
|
return false;
|
|
@@ -279,12 +282,22 @@ export class BaseOffloadStrategy {
|
|
|
279
282
|
}
|
|
280
283
|
/** Whether a block is eligible for offload given the current target and filters. */
|
|
281
284
|
_blockMatchesTarget(block, message, toolNameMap) {
|
|
285
|
+
if (block instanceof ToolUseBlock)
|
|
286
|
+
return false;
|
|
287
|
+
if (block instanceof CachePointBlock)
|
|
288
|
+
return false;
|
|
289
|
+
if (block instanceof ReasoningBlock)
|
|
290
|
+
return false;
|
|
282
291
|
if (block instanceof TextBlock)
|
|
283
292
|
return targetMatchesMessage(this._target, message);
|
|
284
293
|
if (block instanceof ToolResultBlock) {
|
|
294
|
+
if (this._stash && toolNameMap.get(block.toolUseId) === RETRIEVAL_TOOL_NAME)
|
|
295
|
+
return false;
|
|
285
296
|
return (this._target === undefined ||
|
|
286
297
|
toolMatchesTarget(block, this._target, toolNameMap, this._includeFilter, this._excludeFilter));
|
|
287
298
|
}
|
|
299
|
+
if (this._target === '*' || this._target === undefined)
|
|
300
|
+
return true;
|
|
288
301
|
return false;
|
|
289
302
|
}
|
|
290
303
|
/** Process eligible blocks in a message. */
|
|
@@ -298,10 +311,9 @@ export class BaseOffloadStrategy {
|
|
|
298
311
|
const tokens = await agent.model.countTokens([new Message({ role: message.role, content: [block] })]);
|
|
299
312
|
if (tokens <= effectiveThreshold)
|
|
300
313
|
continue;
|
|
301
|
-
const
|
|
314
|
+
const stashRefs = this._stash?.refsFor(block, message, blockIndex) ?? [];
|
|
315
|
+
const replacement = await this._replaceBlock(block, tokens, message, agent, stashRefs);
|
|
302
316
|
if (replacement && replacement !== block) {
|
|
303
|
-
// Intentional in-place mutation: per-block replacement shrinks existing message content
|
|
304
|
-
// rather than constructing a new Message (unlike message-level removal which uses new objects).
|
|
305
317
|
;
|
|
306
318
|
message.content[blockIndex] = replacement;
|
|
307
319
|
acted = true;
|
|
@@ -357,8 +369,8 @@ export class EmergencyTruncateStrategy extends BaseOffloadStrategy {
|
|
|
357
369
|
constructor() {
|
|
358
370
|
super('*');
|
|
359
371
|
}
|
|
360
|
-
init() {
|
|
361
|
-
|
|
372
|
+
init(_agent, stash) {
|
|
373
|
+
this._stash = stash;
|
|
362
374
|
}
|
|
363
375
|
async apply(context) {
|
|
364
376
|
if (context.messages.length <= 3)
|