@compilr-dev/agents 0.3.17 → 0.3.18
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/dist/agent.d.ts +28 -0
- package/dist/agent.js +43 -2
- package/dist/context/index.d.ts +3 -0
- package/dist/context/index.js +4 -0
- package/dist/context/observation-masker.d.ts +80 -0
- package/dist/context/observation-masker.js +192 -0
- package/dist/context/result-compactor.d.ts +15 -0
- package/dist/context/result-compactor.js +165 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +5 -1
- package/package.json +1 -1
package/dist/agent.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ import type { DelegationConfig } from './context/delegation-types.js';
|
|
|
15
15
|
import { PermissionManager } from './permissions/manager.js';
|
|
16
16
|
import { ContextManager } from './context/manager.js';
|
|
17
17
|
import { FileAccessTracker } from './context/file-tracker.js';
|
|
18
|
+
import type { ObservationMaskConfig } from './context/observation-masker.js';
|
|
18
19
|
import { AnchorManager } from './anchors/manager.js';
|
|
19
20
|
import { GuardrailManager } from './guardrails/manager.js';
|
|
20
21
|
import { type RetryConfig } from './utils/index.js';
|
|
@@ -278,6 +279,17 @@ export interface AgentConfig {
|
|
|
278
279
|
* Requires contextManager to be set. Default: true when contextManager is provided.
|
|
279
280
|
*/
|
|
280
281
|
autoContextManagement?: boolean;
|
|
282
|
+
/**
|
|
283
|
+
* Observation masking configuration. Masks old tool results in history to reduce tokens.
|
|
284
|
+
* Enabled by default when contextManager is provided. Set to `false` to disable.
|
|
285
|
+
*/
|
|
286
|
+
observationMask?: Partial<ObservationMaskConfig> | false;
|
|
287
|
+
/**
|
|
288
|
+
* Use compact text format for tool results in LLM messages.
|
|
289
|
+
* Strips JSON wrappers and metadata, reducing token usage.
|
|
290
|
+
* Enabled by default when contextManager is provided. Set to `false` to disable.
|
|
291
|
+
*/
|
|
292
|
+
compactToolResults?: boolean;
|
|
281
293
|
/**
|
|
282
294
|
* Event handler for monitoring agent execution
|
|
283
295
|
*/
|
|
@@ -878,6 +890,14 @@ export declare class Agent {
|
|
|
878
890
|
* File restoration options for post-compaction content injection
|
|
879
891
|
*/
|
|
880
892
|
private readonly fileRestorationConfig?;
|
|
893
|
+
/**
|
|
894
|
+
* Observation masker for reducing token usage by masking old tool results
|
|
895
|
+
*/
|
|
896
|
+
private readonly observationMasker?;
|
|
897
|
+
/**
|
|
898
|
+
* Whether to use compact text format for tool results in LLM messages
|
|
899
|
+
*/
|
|
900
|
+
private readonly compactToolResults;
|
|
881
901
|
constructor(config: AgentConfig);
|
|
882
902
|
/**
|
|
883
903
|
* Create an agent with project memory loaded from files.
|
|
@@ -1280,6 +1300,14 @@ export declare class Agent {
|
|
|
1280
1300
|
* Get context statistics
|
|
1281
1301
|
*/
|
|
1282
1302
|
getContextStats(): ContextStats | undefined;
|
|
1303
|
+
/**
|
|
1304
|
+
* Get observation masking statistics (tokens saved, observations masked).
|
|
1305
|
+
*/
|
|
1306
|
+
getObservationMaskStats(): {
|
|
1307
|
+
maskedCount: number;
|
|
1308
|
+
tokensSaved: number;
|
|
1309
|
+
activeStamps: number;
|
|
1310
|
+
} | undefined;
|
|
1283
1311
|
/**
|
|
1284
1312
|
* Get current verbosity level based on context pressure
|
|
1285
1313
|
*/
|
package/dist/agent.js
CHANGED
|
@@ -10,6 +10,8 @@ import { ContextManager } from './context/manager.js';
|
|
|
10
10
|
import { FileAccessTracker } from './context/file-tracker.js';
|
|
11
11
|
import { createFileTrackingHook } from './context/file-tracking-hook.js';
|
|
12
12
|
import { ToolResultDelegator, DELEGATION_SYSTEM_PROMPT } from './context/tool-result-delegator.js';
|
|
13
|
+
import { ObservationMasker } from './context/observation-masker.js';
|
|
14
|
+
import { compactToolResult } from './context/result-compactor.js';
|
|
13
15
|
import { createRecallResultTool } from './tools/builtin/recall-result.js';
|
|
14
16
|
import { AnchorManager } from './anchors/manager.js';
|
|
15
17
|
import { GuardrailManager } from './guardrails/manager.js';
|
|
@@ -97,6 +99,14 @@ export class Agent {
|
|
|
97
99
|
* File restoration options for post-compaction content injection
|
|
98
100
|
*/
|
|
99
101
|
fileRestorationConfig;
|
|
102
|
+
/**
|
|
103
|
+
* Observation masker for reducing token usage by masking old tool results
|
|
104
|
+
*/
|
|
105
|
+
observationMasker;
|
|
106
|
+
/**
|
|
107
|
+
* Whether to use compact text format for tool results in LLM messages
|
|
108
|
+
*/
|
|
109
|
+
compactToolResults;
|
|
100
110
|
constructor(config) {
|
|
101
111
|
this.provider = config.provider;
|
|
102
112
|
this.systemPrompt = config.systemPrompt ?? '';
|
|
@@ -112,6 +122,12 @@ export class Agent {
|
|
|
112
122
|
this.contextManager = config.contextManager;
|
|
113
123
|
this.autoContextManagement =
|
|
114
124
|
config.autoContextManagement ?? config.contextManager !== undefined;
|
|
125
|
+
// Observation masking: enabled by default when contextManager is provided, unless explicitly false
|
|
126
|
+
if (config.observationMask !== false && this.contextManager) {
|
|
127
|
+
this.observationMasker = new ObservationMasker(config.observationMask === undefined ? undefined : config.observationMask);
|
|
128
|
+
}
|
|
129
|
+
// Compact tool results: enabled by default when contextManager is provided
|
|
130
|
+
this.compactToolResults = config.compactToolResults ?? this.contextManager !== undefined;
|
|
115
131
|
this.onEvent = config.onEvent;
|
|
116
132
|
this.onIterationLimitReached = config.onIterationLimitReached;
|
|
117
133
|
// State management
|
|
@@ -752,6 +768,7 @@ export class Agent {
|
|
|
752
768
|
clearHistory() {
|
|
753
769
|
this.conversationHistory = [];
|
|
754
770
|
this.contextManager?.reset();
|
|
771
|
+
this.observationMasker?.reset();
|
|
755
772
|
return this;
|
|
756
773
|
}
|
|
757
774
|
/**
|
|
@@ -807,6 +824,12 @@ export class Agent {
|
|
|
807
824
|
getContextStats() {
|
|
808
825
|
return this.contextManager?.getStats(this.conversationHistory.length);
|
|
809
826
|
}
|
|
827
|
+
/**
|
|
828
|
+
* Get observation masking statistics (tokens saved, observations masked).
|
|
829
|
+
*/
|
|
830
|
+
getObservationMaskStats() {
|
|
831
|
+
return this.observationMasker?.getStats();
|
|
832
|
+
}
|
|
810
833
|
/**
|
|
811
834
|
* Get current verbosity level based on context pressure
|
|
812
835
|
*/
|
|
@@ -1915,7 +1938,9 @@ export class Agent {
|
|
|
1915
1938
|
type: 'tool_result',
|
|
1916
1939
|
toolUseId: toolUse.id,
|
|
1917
1940
|
content: result.success
|
|
1918
|
-
?
|
|
1941
|
+
? this.compactToolResults
|
|
1942
|
+
? compactToolResult(toolUse.name, result.result, toolUse.input)
|
|
1943
|
+
: JSON.stringify(result.result)
|
|
1919
1944
|
: `Error: ${result.error ?? 'Unknown error'}`,
|
|
1920
1945
|
isError: !result.success,
|
|
1921
1946
|
},
|
|
@@ -1998,7 +2023,9 @@ export class Agent {
|
|
|
1998
2023
|
emit({ type: 'tool_end', name: toolUse.name, result, toolUseId: toolUse.id });
|
|
1999
2024
|
// Build tool result content
|
|
2000
2025
|
let toolResultContent = result.success
|
|
2001
|
-
?
|
|
2026
|
+
? this.compactToolResults
|
|
2027
|
+
? compactToolResult(toolUse.name, result.result, toolUse.input)
|
|
2028
|
+
: JSON.stringify(result.result)
|
|
2002
2029
|
: `Error: ${result.error ?? 'Unknown error'}`;
|
|
2003
2030
|
// Context management (only for sequential - parallel handles this after)
|
|
2004
2031
|
if (!inParallelGroup && this.contextManager && this.autoContextManagement) {
|
|
@@ -2071,6 +2098,11 @@ export class Agent {
|
|
|
2071
2098
|
iterationToolCalls.push(toolCallEntry);
|
|
2072
2099
|
messages.push(toolResultMsg);
|
|
2073
2100
|
newMessages.push(toolResultMsg);
|
|
2101
|
+
// Stamp for observation masking
|
|
2102
|
+
if (this.observationMasker) {
|
|
2103
|
+
const block = toolResultMsg.content[0];
|
|
2104
|
+
this.observationMasker.stamp(toolUse.id, toolUse.name, toolUse.input, block.content.length, this.contextManager?.getTurnCount() ?? 0);
|
|
2105
|
+
}
|
|
2074
2106
|
}
|
|
2075
2107
|
}
|
|
2076
2108
|
else {
|
|
@@ -2105,6 +2137,11 @@ export class Agent {
|
|
|
2105
2137
|
iterationToolCalls.push(toolCallEntry);
|
|
2106
2138
|
messages.push(toolResultMsg);
|
|
2107
2139
|
newMessages.push(toolResultMsg);
|
|
2140
|
+
// Stamp for observation masking
|
|
2141
|
+
if (this.observationMasker) {
|
|
2142
|
+
const block = toolResultMsg.content[0];
|
|
2143
|
+
this.observationMasker.stamp(toolUse.id, toolUse.name, toolUse.input, block.content.length, this.contextManager?.getTurnCount() ?? 0);
|
|
2144
|
+
}
|
|
2108
2145
|
if (skipped) {
|
|
2109
2146
|
continue;
|
|
2110
2147
|
}
|
|
@@ -2223,6 +2260,10 @@ export class Agent {
|
|
|
2223
2260
|
// Context management: increment turn count and update token count
|
|
2224
2261
|
if (this.contextManager) {
|
|
2225
2262
|
this.contextManager.incrementTurn();
|
|
2263
|
+
// Observation masking: mask old tool results in-place before token update
|
|
2264
|
+
if (this.observationMasker) {
|
|
2265
|
+
this.observationMasker.maskHistory(messages, this.contextManager.getTurnCount());
|
|
2266
|
+
}
|
|
2226
2267
|
await this.contextManager.updateTokenCount(messages);
|
|
2227
2268
|
}
|
|
2228
2269
|
// Update internal state tracking
|
package/dist/context/index.d.ts
CHANGED
|
@@ -20,3 +20,6 @@ export { ToolResultDelegator, DELEGATION_SYSTEM_PROMPT } from './tool-result-del
|
|
|
20
20
|
export type { ToolResultDelegatorOptions } from './tool-result-delegator.js';
|
|
21
21
|
export { DEFAULT_DELEGATION_CONFIG } from './delegation-types.js';
|
|
22
22
|
export type { DelegationConfig, StoredResult, DelegationEvent } from './delegation-types.js';
|
|
23
|
+
export { compactToolResult } from './result-compactor.js';
|
|
24
|
+
export { ObservationMasker, DEFAULT_MASK_CONFIG, extractInputSummary, buildMaskText, isMasked, } from './observation-masker.js';
|
|
25
|
+
export type { ObservationMaskConfig, MaskResult, ObservationMaskStats, } from './observation-masker.js';
|
package/dist/context/index.js
CHANGED
|
@@ -15,3 +15,7 @@ export { createFileTrackingHook, TRACKED_TOOLS } from './file-tracking-hook.js';
|
|
|
15
15
|
export { DelegatedResultStore } from './delegated-result-store.js';
|
|
16
16
|
export { ToolResultDelegator, DELEGATION_SYSTEM_PROMPT } from './tool-result-delegator.js';
|
|
17
17
|
export { DEFAULT_DELEGATION_CONFIG } from './delegation-types.js';
|
|
18
|
+
// Compact Tool Result Formatting (Phase 2 Token Optimization)
|
|
19
|
+
export { compactToolResult } from './result-compactor.js';
|
|
20
|
+
// Observation Masking (Phase 1 Token Optimization)
|
|
21
|
+
export { ObservationMasker, DEFAULT_MASK_CONFIG, extractInputSummary, buildMaskText, isMasked, } from './observation-masker.js';
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Observation Masker — Phase 1 of Advanced Token Optimization
|
|
3
|
+
*
|
|
4
|
+
* Masks old tool results (observations) in conversation history to reduce token usage.
|
|
5
|
+
* Based on JetBrains Research (Dec 2025): observation masking outperforms LLM summarization,
|
|
6
|
+
* achieving 52% cost reduction with +2.6% higher solve rates.
|
|
7
|
+
*
|
|
8
|
+
* Strategy: In-place masking of conversationHistory after N turns.
|
|
9
|
+
* The agent can re-read from the environment if needed (files, git, etc.).
|
|
10
|
+
*/
|
|
11
|
+
import type { Message } from '../providers/types.js';
|
|
12
|
+
export interface ObservationMaskConfig {
|
|
13
|
+
/** Turns after which tool results are masked (default: 6) */
|
|
14
|
+
maskAfterTurns: number;
|
|
15
|
+
/** Minimum content length (chars) to mask — skip tiny results (default: 400 ≈ 100 tokens) */
|
|
16
|
+
minCharsToMask: number;
|
|
17
|
+
/** Tool names to NEVER mask (e.g., recall tools, state queries) */
|
|
18
|
+
neverMask: string[];
|
|
19
|
+
/** Tool names to mask after just 1 turn (large reads, bash output) */
|
|
20
|
+
alwaysMaskEarly: string[];
|
|
21
|
+
}
|
|
22
|
+
export declare const DEFAULT_MASK_CONFIG: ObservationMaskConfig;
|
|
23
|
+
interface TurnStamp {
|
|
24
|
+
turn: number;
|
|
25
|
+
toolName: string;
|
|
26
|
+
/** Short summary of input for mask text (e.g., file path, command) */
|
|
27
|
+
inputSummary: string;
|
|
28
|
+
/** Original content length in chars */
|
|
29
|
+
contentLength: number;
|
|
30
|
+
}
|
|
31
|
+
export interface MaskResult {
|
|
32
|
+
maskedCount: number;
|
|
33
|
+
tokensSaved: number;
|
|
34
|
+
}
|
|
35
|
+
export interface ObservationMaskStats {
|
|
36
|
+
/** Total observations masked this session */
|
|
37
|
+
maskedCount: number;
|
|
38
|
+
/** Estimated tokens saved this session */
|
|
39
|
+
tokensSaved: number;
|
|
40
|
+
/** Active stamps (pending masking) */
|
|
41
|
+
activeStamps: number;
|
|
42
|
+
}
|
|
43
|
+
export declare class ObservationMasker {
|
|
44
|
+
private readonly stamps;
|
|
45
|
+
private readonly config;
|
|
46
|
+
private stats;
|
|
47
|
+
constructor(config?: Partial<ObservationMaskConfig>);
|
|
48
|
+
/**
|
|
49
|
+
* Register a tool result with its turn number and input context.
|
|
50
|
+
* Called immediately after a tool result is added to the messages array.
|
|
51
|
+
*/
|
|
52
|
+
stamp(toolUseId: string, toolName: string, input: Record<string, unknown>, contentLength: number, turn: number): void;
|
|
53
|
+
/**
|
|
54
|
+
* Mask old tool results in-place in the messages array.
|
|
55
|
+
* Modifies ToolResultBlock.content directly.
|
|
56
|
+
*/
|
|
57
|
+
maskHistory(messages: Message[], currentTurn: number): MaskResult;
|
|
58
|
+
getStats(): ObservationMaskStats;
|
|
59
|
+
/**
|
|
60
|
+
* Reset all state (stamps and stats). Used when clearing history.
|
|
61
|
+
*/
|
|
62
|
+
reset(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Get current configuration (for testing/inspection).
|
|
65
|
+
*/
|
|
66
|
+
getConfig(): ObservationMaskConfig;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Extract a short summary from tool input for the mask text.
|
|
70
|
+
*/
|
|
71
|
+
export declare function extractInputSummary(toolName: string, input: Record<string, unknown>): string;
|
|
72
|
+
/**
|
|
73
|
+
* Build the compact mask text that replaces the original content.
|
|
74
|
+
*/
|
|
75
|
+
export declare function buildMaskText(stamp: TurnStamp): string;
|
|
76
|
+
/**
|
|
77
|
+
* Check if a tool result content string is already masked.
|
|
78
|
+
*/
|
|
79
|
+
export declare function isMasked(content: string): boolean;
|
|
80
|
+
export {};
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Observation Masker — Phase 1 of Advanced Token Optimization
|
|
3
|
+
*
|
|
4
|
+
* Masks old tool results (observations) in conversation history to reduce token usage.
|
|
5
|
+
* Based on JetBrains Research (Dec 2025): observation masking outperforms LLM summarization,
|
|
6
|
+
* achieving 52% cost reduction with +2.6% higher solve rates.
|
|
7
|
+
*
|
|
8
|
+
* Strategy: In-place masking of conversationHistory after N turns.
|
|
9
|
+
* The agent can re-read from the environment if needed (files, git, etc.).
|
|
10
|
+
*/
|
|
11
|
+
export const DEFAULT_MASK_CONFIG = {
|
|
12
|
+
maskAfterTurns: 6,
|
|
13
|
+
minCharsToMask: 400,
|
|
14
|
+
neverMask: ['recall_full_result', 'recall_work'],
|
|
15
|
+
alwaysMaskEarly: ['read_file', 'bash', 'bash_output', 'grep', 'glob'],
|
|
16
|
+
};
|
|
17
|
+
// ============================================================
|
|
18
|
+
// ObservationMasker
|
|
19
|
+
// ============================================================
|
|
20
|
+
export class ObservationMasker {
|
|
21
|
+
stamps = new Map();
|
|
22
|
+
config;
|
|
23
|
+
stats = { maskedCount: 0, tokensSaved: 0 };
|
|
24
|
+
constructor(config) {
|
|
25
|
+
this.config = { ...DEFAULT_MASK_CONFIG, ...config };
|
|
26
|
+
}
|
|
27
|
+
// ----------------------------------------------------------
|
|
28
|
+
// Stamping — called when tool results are added to history
|
|
29
|
+
// ----------------------------------------------------------
|
|
30
|
+
/**
|
|
31
|
+
* Register a tool result with its turn number and input context.
|
|
32
|
+
* Called immediately after a tool result is added to the messages array.
|
|
33
|
+
*/
|
|
34
|
+
stamp(toolUseId, toolName, input, contentLength, turn) {
|
|
35
|
+
this.stamps.set(toolUseId, {
|
|
36
|
+
turn,
|
|
37
|
+
toolName,
|
|
38
|
+
inputSummary: extractInputSummary(toolName, input),
|
|
39
|
+
contentLength,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
// ----------------------------------------------------------
|
|
43
|
+
// Masking — called after incrementTurn()
|
|
44
|
+
// ----------------------------------------------------------
|
|
45
|
+
/**
|
|
46
|
+
* Mask old tool results in-place in the messages array.
|
|
47
|
+
* Modifies ToolResultBlock.content directly.
|
|
48
|
+
*/
|
|
49
|
+
maskHistory(messages, currentTurn) {
|
|
50
|
+
let tokensSaved = 0;
|
|
51
|
+
let maskedCount = 0;
|
|
52
|
+
for (const msg of messages) {
|
|
53
|
+
if (msg.role !== 'user' || typeof msg.content === 'string')
|
|
54
|
+
continue;
|
|
55
|
+
for (const block of msg.content) {
|
|
56
|
+
if (block.type !== 'tool_result')
|
|
57
|
+
continue;
|
|
58
|
+
if (isMasked(block.content))
|
|
59
|
+
continue;
|
|
60
|
+
const stamp = this.stamps.get(block.toolUseId);
|
|
61
|
+
if (!stamp)
|
|
62
|
+
continue;
|
|
63
|
+
if (this.config.neverMask.includes(stamp.toolName))
|
|
64
|
+
continue;
|
|
65
|
+
const age = currentTurn - stamp.turn;
|
|
66
|
+
const threshold = this.config.alwaysMaskEarly.includes(stamp.toolName)
|
|
67
|
+
? 1
|
|
68
|
+
: this.config.maskAfterTurns;
|
|
69
|
+
if (age < threshold)
|
|
70
|
+
continue;
|
|
71
|
+
if (stamp.contentLength < this.config.minCharsToMask)
|
|
72
|
+
continue;
|
|
73
|
+
// Build mask and calculate savings
|
|
74
|
+
const maskText = buildMaskText(stamp);
|
|
75
|
+
const savedChars = stamp.contentLength - maskText.length;
|
|
76
|
+
const savedTokens = Math.max(0, Math.ceil(savedChars / 4));
|
|
77
|
+
// Mask in-place
|
|
78
|
+
block.content = maskText;
|
|
79
|
+
tokensSaved += savedTokens;
|
|
80
|
+
maskedCount++;
|
|
81
|
+
// Clean up stamp
|
|
82
|
+
this.stamps.delete(block.toolUseId);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
this.stats.maskedCount += maskedCount;
|
|
86
|
+
this.stats.tokensSaved += tokensSaved;
|
|
87
|
+
return { maskedCount, tokensSaved };
|
|
88
|
+
}
|
|
89
|
+
// ----------------------------------------------------------
|
|
90
|
+
// Stats
|
|
91
|
+
// ----------------------------------------------------------
|
|
92
|
+
getStats() {
|
|
93
|
+
return {
|
|
94
|
+
...this.stats,
|
|
95
|
+
activeStamps: this.stamps.size,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Reset all state (stamps and stats). Used when clearing history.
|
|
100
|
+
*/
|
|
101
|
+
reset() {
|
|
102
|
+
this.stamps.clear();
|
|
103
|
+
this.stats = { maskedCount: 0, tokensSaved: 0 };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get current configuration (for testing/inspection).
|
|
107
|
+
*/
|
|
108
|
+
getConfig() {
|
|
109
|
+
return { ...this.config };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// ============================================================
|
|
113
|
+
// Pure functions (exported for testing)
|
|
114
|
+
// ============================================================
|
|
115
|
+
/**
|
|
116
|
+
* Extract a short summary from tool input for the mask text.
|
|
117
|
+
*/
|
|
118
|
+
export function extractInputSummary(toolName, input) {
|
|
119
|
+
// File operations — use path
|
|
120
|
+
if (toolName === 'read_file' || toolName === 'edit' || toolName === 'write_file') {
|
|
121
|
+
const path = input.path ?? input.file_path;
|
|
122
|
+
if (typeof path === 'string')
|
|
123
|
+
return path;
|
|
124
|
+
}
|
|
125
|
+
// Shell commands — use command (truncated)
|
|
126
|
+
if (toolName === 'bash' || toolName === 'bash_output') {
|
|
127
|
+
const cmd = input.command;
|
|
128
|
+
if (typeof cmd === 'string') {
|
|
129
|
+
return cmd.length > 40 ? cmd.slice(0, 40) + '...' : cmd;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// Search — use pattern
|
|
133
|
+
if (toolName === 'grep') {
|
|
134
|
+
const pattern = input.pattern;
|
|
135
|
+
if (typeof pattern === 'string')
|
|
136
|
+
return `"${pattern}"`;
|
|
137
|
+
}
|
|
138
|
+
// Glob — use pattern
|
|
139
|
+
if (toolName === 'glob') {
|
|
140
|
+
const pattern = input.pattern;
|
|
141
|
+
if (typeof pattern === 'string')
|
|
142
|
+
return pattern;
|
|
143
|
+
}
|
|
144
|
+
// Git tools — use subcommand or operation
|
|
145
|
+
if (toolName.startsWith('git_')) {
|
|
146
|
+
return toolName.slice(4); // "git_diff" → "diff"
|
|
147
|
+
}
|
|
148
|
+
// Web fetch — use URL
|
|
149
|
+
if (toolName === 'web_fetch') {
|
|
150
|
+
const url = input.url;
|
|
151
|
+
if (typeof url === 'string') {
|
|
152
|
+
return url.length > 60 ? url.slice(0, 60) + '...' : url;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Default — just the tool name
|
|
156
|
+
return toolName;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Build the compact mask text that replaces the original content.
|
|
160
|
+
*/
|
|
161
|
+
export function buildMaskText(stamp) {
|
|
162
|
+
const { toolName, inputSummary, turn, contentLength } = stamp;
|
|
163
|
+
const lines = String(Math.ceil(contentLength / 80));
|
|
164
|
+
const t = String(turn);
|
|
165
|
+
if (toolName === 'read_file') {
|
|
166
|
+
return `[file:${inputSummary} ~${lines}L read@turn:${t}]`;
|
|
167
|
+
}
|
|
168
|
+
if (toolName === 'bash' || toolName === 'bash_output') {
|
|
169
|
+
return `[cmd:${inputSummary} ~${lines}L@turn:${t}]`;
|
|
170
|
+
}
|
|
171
|
+
if (toolName === 'grep') {
|
|
172
|
+
return `[search:${inputSummary}@turn:${t}]`;
|
|
173
|
+
}
|
|
174
|
+
if (toolName === 'glob') {
|
|
175
|
+
return `[glob:${inputSummary}@turn:${t}]`;
|
|
176
|
+
}
|
|
177
|
+
if (toolName.startsWith('git_')) {
|
|
178
|
+
return `[git:${inputSummary}@turn:${t}]`;
|
|
179
|
+
}
|
|
180
|
+
if (toolName === 'edit' || toolName === 'write_file') {
|
|
181
|
+
return `[write:${inputSummary}@turn:${t}]`;
|
|
182
|
+
}
|
|
183
|
+
// Generic
|
|
184
|
+
const tokens = String(Math.ceil(contentLength / 4));
|
|
185
|
+
return `[tool:${toolName} ${tokens}tok@turn:${t}]`;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Check if a tool result content string is already masked.
|
|
189
|
+
*/
|
|
190
|
+
export function isMasked(content) {
|
|
191
|
+
return content.startsWith('[') && content.endsWith(']') && content.includes('@turn:');
|
|
192
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compact Tool Result Formatting — Phase 2 of Advanced Token Optimization
|
|
3
|
+
*
|
|
4
|
+
* Replaces JSON.stringify for tool results in the LLM messages array.
|
|
5
|
+
* Per-tool formatters strip JSON overhead (escaped newlines, metadata fields)
|
|
6
|
+
* and produce a text format the LLM can parse just as well.
|
|
7
|
+
*
|
|
8
|
+
* Only affects what the LLM reads in its message history.
|
|
9
|
+
* CLI formatters, events, and hooks still receive the raw ToolExecutionResult.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Format a tool result as compact text for LLM context.
|
|
13
|
+
* Falls back to JSON.stringify for unknown tools or non-object results.
|
|
14
|
+
*/
|
|
15
|
+
export declare function compactToolResult(toolName: string, result: unknown, input?: Record<string, unknown>): string;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compact Tool Result Formatting — Phase 2 of Advanced Token Optimization
|
|
3
|
+
*
|
|
4
|
+
* Replaces JSON.stringify for tool results in the LLM messages array.
|
|
5
|
+
* Per-tool formatters strip JSON overhead (escaped newlines, metadata fields)
|
|
6
|
+
* and produce a text format the LLM can parse just as well.
|
|
7
|
+
*
|
|
8
|
+
* Only affects what the LLM reads in its message history.
|
|
9
|
+
* CLI formatters, events, and hooks still receive the raw ToolExecutionResult.
|
|
10
|
+
*/
|
|
11
|
+
// ============================================================
|
|
12
|
+
// Helpers
|
|
13
|
+
// ============================================================
|
|
14
|
+
/** Safely extract a string field from an unknown record. */
|
|
15
|
+
function str(value, fallback = '') {
|
|
16
|
+
return typeof value === 'string' ? value : fallback;
|
|
17
|
+
}
|
|
18
|
+
/** Safely extract a number field from an unknown record. */
|
|
19
|
+
function num(value, fallback = 0) {
|
|
20
|
+
return typeof value === 'number' ? value : fallback;
|
|
21
|
+
}
|
|
22
|
+
// ============================================================
|
|
23
|
+
// Public API
|
|
24
|
+
// ============================================================
|
|
25
|
+
/**
|
|
26
|
+
* Format a tool result as compact text for LLM context.
|
|
27
|
+
* Falls back to JSON.stringify for unknown tools or non-object results.
|
|
28
|
+
*/
|
|
29
|
+
export function compactToolResult(toolName, result, input) {
|
|
30
|
+
// String results — already compact
|
|
31
|
+
if (typeof result === 'string')
|
|
32
|
+
return result;
|
|
33
|
+
if (result === null || result === undefined)
|
|
34
|
+
return '';
|
|
35
|
+
// Per-tool formatters
|
|
36
|
+
const formatter = TOOL_FORMATTERS.get(toolName);
|
|
37
|
+
if (formatter)
|
|
38
|
+
return formatter(result, input);
|
|
39
|
+
// Fallback: JSON.stringify (current behavior)
|
|
40
|
+
return JSON.stringify(result);
|
|
41
|
+
}
|
|
42
|
+
const TOOL_FORMATTERS = new Map([
|
|
43
|
+
['read_file', formatReadFile],
|
|
44
|
+
['bash', formatBash],
|
|
45
|
+
['bash_output', formatBash],
|
|
46
|
+
['grep', formatGrep],
|
|
47
|
+
['glob', formatGlob],
|
|
48
|
+
['edit', formatEdit],
|
|
49
|
+
['write_file', formatWriteFile],
|
|
50
|
+
]);
|
|
51
|
+
/**
|
|
52
|
+
* read_file → [path 200L] + content
|
|
53
|
+
*/
|
|
54
|
+
function formatReadFile(r) {
|
|
55
|
+
const path = str(r.path);
|
|
56
|
+
const content = str(r.content);
|
|
57
|
+
const totalLines = typeof r.totalLines === 'number' ? r.totalLines : undefined;
|
|
58
|
+
const linesReturned = typeof r.linesReturned === 'number' ? r.linesReturned : undefined;
|
|
59
|
+
const truncated = r.truncated === true;
|
|
60
|
+
// Header: [path NL] or [path M/NL] if truncated
|
|
61
|
+
let header;
|
|
62
|
+
if (truncated && linesReturned !== undefined && totalLines !== undefined) {
|
|
63
|
+
header = `[${path} ${String(linesReturned)}/${String(totalLines)}L]`;
|
|
64
|
+
}
|
|
65
|
+
else if (totalLines !== undefined) {
|
|
66
|
+
header = `[${path} ${String(totalLines)}L]`;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
header = `[${path}]`;
|
|
70
|
+
}
|
|
71
|
+
return `${header}\n${content}`;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* bash → $ command\nstdout\n[stderr]\nstderr
|
|
75
|
+
* Non-zero exit: [exit:N] $ command\n...
|
|
76
|
+
* Background: shell_id message
|
|
77
|
+
*/
|
|
78
|
+
function formatBash(r, input) {
|
|
79
|
+
// Background execution has different shape
|
|
80
|
+
if (r.shell_id) {
|
|
81
|
+
return `[bg:${str(r.shell_id)}] ${str(r.message)}`;
|
|
82
|
+
}
|
|
83
|
+
const stdout = str(r.stdout);
|
|
84
|
+
const stderr = str(r.stderr);
|
|
85
|
+
const exitCode = num(r.exitCode);
|
|
86
|
+
const command = input ? str(input.command) : '';
|
|
87
|
+
const parts = [];
|
|
88
|
+
// Command line
|
|
89
|
+
if (command) {
|
|
90
|
+
const prefix = exitCode !== 0 ? `[exit:${String(exitCode)}] ` : '';
|
|
91
|
+
parts.push(`${prefix}$ ${command}`);
|
|
92
|
+
}
|
|
93
|
+
else if (exitCode !== 0) {
|
|
94
|
+
parts.push(`[exit:${String(exitCode)}]`);
|
|
95
|
+
}
|
|
96
|
+
// stdout
|
|
97
|
+
if (stdout) {
|
|
98
|
+
parts.push(stdout);
|
|
99
|
+
}
|
|
100
|
+
// stderr (only if non-empty)
|
|
101
|
+
if (stderr) {
|
|
102
|
+
parts.push('[stderr]');
|
|
103
|
+
parts.push(stderr);
|
|
104
|
+
}
|
|
105
|
+
return parts.join('\n');
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* grep → N matches in M files:\n...matches
|
|
109
|
+
* Files-only mode → N files matching:\n...files
|
|
110
|
+
*/
|
|
111
|
+
function formatGrep(r) {
|
|
112
|
+
const count = num(r.count);
|
|
113
|
+
// Files-only mode (has `files` array)
|
|
114
|
+
if (Array.isArray(r.files)) {
|
|
115
|
+
const files = r.files;
|
|
116
|
+
if (files.length === 0)
|
|
117
|
+
return '0 files matching';
|
|
118
|
+
return `${String(count)} files matching:\n${files.join('\n')}`;
|
|
119
|
+
}
|
|
120
|
+
// Matches mode
|
|
121
|
+
const matches = Array.isArray(r.matches) ? r.matches : undefined;
|
|
122
|
+
const filesSearched = typeof r.filesSearched === 'number' ? r.filesSearched : undefined;
|
|
123
|
+
if (!matches || matches.length === 0) {
|
|
124
|
+
return filesSearched ? `0 matches in ${String(filesSearched)} files` : '0 matches';
|
|
125
|
+
}
|
|
126
|
+
const summary = filesSearched
|
|
127
|
+
? `${String(count)} matches in ${String(filesSearched)} files:`
|
|
128
|
+
: `${String(count)} matches:`;
|
|
129
|
+
return `${summary}\n${matches.join('\n')}`;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* glob → N files:\n...files
|
|
133
|
+
*/
|
|
134
|
+
function formatGlob(r) {
|
|
135
|
+
const files = Array.isArray(r.files) ? r.files : undefined;
|
|
136
|
+
const count = num(r.count);
|
|
137
|
+
if (!files || files.length === 0)
|
|
138
|
+
return '0 files';
|
|
139
|
+
return `${String(count)} files:\n${files.join('\n')}`;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* edit → OK path (N replacements)
|
|
143
|
+
*/
|
|
144
|
+
function formatEdit(r) {
|
|
145
|
+
const filePath = str(r.filePath);
|
|
146
|
+
const replacements = num(r.replacements);
|
|
147
|
+
const created = r.created === true;
|
|
148
|
+
if (created) {
|
|
149
|
+
return `OK created ${filePath}`;
|
|
150
|
+
}
|
|
151
|
+
return `OK ${filePath} (${String(replacements)} replacement${replacements !== 1 ? 's' : ''})`;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* write_file → OK wrote path (NB)
|
|
155
|
+
*/
|
|
156
|
+
function formatWriteFile(r) {
|
|
157
|
+
const path = str(r.path);
|
|
158
|
+
const bytesWritten = typeof r.bytesWritten === 'number' ? r.bytesWritten : undefined;
|
|
159
|
+
const mode = str(r.mode);
|
|
160
|
+
const action = mode === 'appended' ? 'appended' : 'wrote';
|
|
161
|
+
if (bytesWritten !== undefined) {
|
|
162
|
+
return `OK ${action} ${path} (${String(bytesWritten)}B)`;
|
|
163
|
+
}
|
|
164
|
+
return `OK ${action} ${path}`;
|
|
165
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -39,8 +39,8 @@ export type { ToolPairingValidation } from './messages/index.js';
|
|
|
39
39
|
export { generateId, sleep, retry, truncate, withRetryGenerator, calculateBackoffDelay, DEFAULT_RETRY_CONFIG, countTokens, countMessageTokens, } from './utils/index.js';
|
|
40
40
|
export type { RetryConfig as LLMRetryConfig, WithRetryOptions } from './utils/index.js';
|
|
41
41
|
export { AgentError, ProviderError, ToolError, ToolTimeoutError, ToolLoopError, ValidationError, MaxIterationsError, AbortError, ContextOverflowError, isAgentError, isProviderError, isToolError, isToolTimeoutError, isToolLoopError, isContextOverflowError, wrapError, } from './errors.js';
|
|
42
|
-
export { ContextManager, DEFAULT_CONTEXT_CONFIG, FileAccessTracker, createFileTrackingHook, TRACKED_TOOLS, DelegatedResultStore, ToolResultDelegator, DELEGATION_SYSTEM_PROMPT, DEFAULT_DELEGATION_CONFIG, } from './context/index.js';
|
|
43
|
-
export type { ContextManagerOptions, ContextCategory, BudgetAllocation, CategoryBudgetInfo, PreflightResult, VerbosityLevel, VerbosityConfig, ContextConfig, FilteringConfig, CompactionConfig, SummarizationConfig, CompactionResult, SummarizationResult, FilteringResult, ContextEvent, ContextEventHandler, ContextStats, FileAccessType, FileAccess, FileAccessTrackerOptions, FormatHintsOptions, FileAccessStats, RestorationHintMessage, DelegatedResultStoreStats, ToolResultDelegatorOptions, DelegationConfig, StoredResult, DelegationEvent, } from './context/index.js';
|
|
42
|
+
export { ContextManager, DEFAULT_CONTEXT_CONFIG, FileAccessTracker, createFileTrackingHook, TRACKED_TOOLS, DelegatedResultStore, ToolResultDelegator, DELEGATION_SYSTEM_PROMPT, DEFAULT_DELEGATION_CONFIG, compactToolResult, ObservationMasker, DEFAULT_MASK_CONFIG, extractInputSummary, buildMaskText, isMasked, } from './context/index.js';
|
|
43
|
+
export type { ContextManagerOptions, ContextCategory, BudgetAllocation, CategoryBudgetInfo, PreflightResult, VerbosityLevel, VerbosityConfig, ContextConfig, FilteringConfig, CompactionConfig, SummarizationConfig, CompactionResult, SummarizationResult, FilteringResult, ContextEvent, ContextEventHandler, ContextStats, FileAccessType, FileAccess, FileAccessTrackerOptions, FormatHintsOptions, FileAccessStats, RestorationHintMessage, DelegatedResultStoreStats, ToolResultDelegatorOptions, DelegationConfig, StoredResult, DelegationEvent, ObservationMaskConfig, MaskResult, ObservationMaskStats, } from './context/index.js';
|
|
44
44
|
export { SkillRegistry, defineSkill, createSkillRegistry, builtinSkills, getDefaultSkillRegistry, resetDefaultSkillRegistry, } from './skills/index.js';
|
|
45
45
|
export type { Skill, SkillInvocationResult, SkillInvokeOptions } from './skills/index.js';
|
|
46
46
|
export { JsonSerializer, CompactJsonSerializer, defaultSerializer, MemoryCheckpointer, FileCheckpointer, StateError, StateErrorCode, CURRENT_STATE_VERSION, } from './state/index.js';
|
package/dist/index.js
CHANGED
|
@@ -47,7 +47,11 @@ export { AgentError, ProviderError, ToolError, ToolTimeoutError, ToolLoopError,
|
|
|
47
47
|
// Context management
|
|
48
48
|
export { ContextManager, DEFAULT_CONTEXT_CONFIG, FileAccessTracker, createFileTrackingHook, TRACKED_TOOLS,
|
|
49
49
|
// Tool result delegation
|
|
50
|
-
DelegatedResultStore, ToolResultDelegator, DELEGATION_SYSTEM_PROMPT, DEFAULT_DELEGATION_CONFIG,
|
|
50
|
+
DelegatedResultStore, ToolResultDelegator, DELEGATION_SYSTEM_PROMPT, DEFAULT_DELEGATION_CONFIG,
|
|
51
|
+
// Compact tool result formatting (Phase 2 Token Optimization)
|
|
52
|
+
compactToolResult,
|
|
53
|
+
// Observation masking (Phase 1 Token Optimization)
|
|
54
|
+
ObservationMasker, DEFAULT_MASK_CONFIG, extractInputSummary, buildMaskText, isMasked, } from './context/index.js';
|
|
51
55
|
// Skills system
|
|
52
56
|
export { SkillRegistry, defineSkill, createSkillRegistry, builtinSkills, getDefaultSkillRegistry, resetDefaultSkillRegistry, } from './skills/index.js';
|
|
53
57
|
// State management
|