This repository was archived by the owner on May 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtoolResultCompressor.ts
More file actions
183 lines (161 loc) · 7.02 KB
/
toolResultCompressor.ts
File metadata and controls
183 lines (161 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type * as vscode from 'vscode';
import { ConfigKey, IConfigurationService } from '../../../platform/configuration/common/configurationService';
import { ILogService } from '../../../platform/log/common/logService';
import { ITelemetryService } from '../../../platform/telemetry/common/telemetry';
import { createServiceIdentifier } from '../../../util/common/services';
import { LanguageModelTextPart, LanguageModelTextPart2 } from '../../../vscodeTypes';
export const IToolResultCompressor = createServiceIdentifier<IToolResultCompressor>('IToolResultCompressor');
/**
* Result of running a {@link IToolResultFilter}.
*
* `text` is the new text to substitute back into the corresponding text part.
* `compressed` is `true` if any compression actually happened — used purely
* for telemetry / accounting.
*/
export interface IToolResultFilterOutput {
readonly text: string;
readonly compressed: boolean;
}
/**
* A pure function that compresses a single text part of a tool result.
*
* Implementations MUST never make output worse than the input. If a filter
* cannot improve a piece of text, it should return the original `text` and
* `compressed: false`.
*/
export interface IToolResultFilter {
readonly id: string;
/** Tool names this filter applies to. */
readonly toolNames: readonly string[];
/**
* Decide whether this filter wants to handle the result. May inspect tool
* input (e.g. for `run_in_terminal`, the command being run).
*/
matches(toolName: string, input: unknown): boolean;
apply(text: string, input: unknown): IToolResultFilterOutput;
}
export interface IToolResultCompressor {
readonly _serviceBrand: undefined;
registerFilter(filter: IToolResultFilter): void;
/**
* Returns a possibly-compressed copy of `result`, or `undefined` if no
* compression was applied (caller should pass through the original).
*/
maybeCompress(toolName: string, input: unknown, result: vscode.LanguageModelToolResult | vscode.LanguageModelToolResult2): vscode.LanguageModelToolResult | undefined;
}
/**
* Outputs at or below this many characters (UTF-16 code units, i.e.
* `string.length`) are not worth compressing. Mirrors ztk's 80-character
* minimum.
*/
const MIN_COMPRESSIBLE_LENGTH = 80;
export class ToolResultCompressorService implements IToolResultCompressor {
declare readonly _serviceBrand: undefined;
private readonly _filters = new Map<string, IToolResultFilter[]>();
constructor(
@IConfigurationService private readonly _configurationService: IConfigurationService,
@ITelemetryService private readonly _telemetryService: ITelemetryService,
@ILogService private readonly _logService: ILogService,
) { }
registerFilter(filter: IToolResultFilter): void {
for (const name of filter.toolNames) {
let bucket = this._filters.get(name);
if (!bucket) {
bucket = [];
this._filters.set(name, bucket);
}
bucket.push(filter);
}
}
maybeCompress(toolName: string, input: unknown, result: vscode.LanguageModelToolResult | vscode.LanguageModelToolResult2): vscode.LanguageModelToolResult | undefined {
if (!this._configurationService.getConfig(ConfigKey.ToolResultCompressionEnabled)) {
return undefined;
}
const filters = this._filters.get(toolName);
if (!filters || filters.length === 0) {
return undefined;
}
const matchingFilters = filters.filter(f => f.matches(toolName, input));
if (matchingFilters.length === 0) {
return undefined;
}
// Mutable copy: filters that throw get spliced out so we don't repeatedly
// invoke a broken filter on every subsequent text part in this pass.
const activeFilters = matchingFilters.slice();
const disabledFilterIds = new Set<string>();
let totalBefore = 0;
let totalAfter = 0;
let anyCompressed = false;
const usedFilterIds = new Set<string>();
const newContent = result.content.map(part => {
if (!(part instanceof LanguageModelTextPart)) {
return part;
}
const original = part.value;
if (original.length < MIN_COMPRESSIBLE_LENGTH) {
return part;
}
let current = original;
for (let i = 0; i < activeFilters.length; /* manual increment */) {
const filter = activeFilters[i];
try {
const out = filter.apply(current, input);
if (out.compressed && out.text.length < current.length) {
current = out.text;
usedFilterIds.add(filter.id);
}
i++;
} catch (err) {
// "Never make it worse." Disable the filter for the rest of this
// compression pass so it can't repeatedly throw on later text parts,
// and warn at most once per filter.
activeFilters.splice(i, 1);
if (!disabledFilterIds.has(filter.id)) {
disabledFilterIds.add(filter.id);
this._logService.warn(`[ToolResultCompressor] filter ${filter.id} threw on tool ${toolName}; disabled for this pass: ${err}`);
}
}
}
totalBefore += original.length;
totalAfter += current.length;
if (current !== original) {
anyCompressed = true;
// Preserve LanguageModelTextPart2 audience metadata if present.
if (part instanceof LanguageModelTextPart2) {
return new LanguageModelTextPart2(current, part.audience);
}
return new LanguageModelTextPart(current);
}
return part;
});
if (!anyCompressed) {
return undefined;
}
this._sendTelemetry(toolName, [...usedFilterIds], totalBefore, totalAfter);
// Preserve `toolResultMessage`/`toolResultDetails` if present (ExtendedLanguageModelToolResult shape).
const compressed: vscode.LanguageModelToolResult & { toolResultMessage?: unknown; toolResultDetails?: unknown } =
Object.assign(Object.create(Object.getPrototypeOf(result)), result, { content: newContent });
return compressed as vscode.LanguageModelToolResult;
}
private _sendTelemetry(toolName: string, filterIds: string[], beforeChars: number, afterChars: number) {
/* __GDPR__
"toolResultCompressed" : {
"owner": "meganrogge",
"comment": "Reports tool output compression savings.",
"toolName": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "The tool whose output was compressed." },
"filters": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "Comma-separated filter ids that fired." },
"beforeChars": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true, "comment": "Total text part length in UTF-16 code units before compression." },
"afterChars": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true, "comment": "Total text part length in UTF-16 code units after compression." }
}
*/
this._telemetryService.sendMSFTTelemetryEvent(
'toolResultCompressed',
{ toolName, filters: filterIds.join(',') },
{ beforeChars, afterChars },
);
}
}