|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { describe, expect, it } from 'vitest'; |
| 7 | +import type { IConfigurationService } from '../../../../platform/configuration/common/configurationService'; |
| 8 | +import type { ILogService } from '../../../../platform/log/common/logService'; |
| 9 | +import type { ITelemetryService } from '../../../../platform/telemetry/common/telemetry'; |
| 10 | +import { |
| 11 | + LanguageModelDataPart, |
| 12 | + LanguageModelPartAudience, |
| 13 | + LanguageModelTextPart, |
| 14 | + LanguageModelTextPart2, |
| 15 | + LanguageModelToolResult, |
| 16 | +} from '../../../../vscodeTypes'; |
| 17 | +import { IToolResultFilter, ToolResultCompressorService } from '../toolResultCompressor'; |
| 18 | + |
| 19 | +const TOOL = 'run_in_terminal'; |
| 20 | + |
| 21 | +function makeService(opts: { |
| 22 | + enabled: boolean; |
| 23 | + warnings?: string[]; |
| 24 | +}): ToolResultCompressorService { |
| 25 | + const config = { |
| 26 | + getConfig: (_key: unknown) => opts.enabled, |
| 27 | + } as unknown as IConfigurationService; |
| 28 | + const telemetry = { |
| 29 | + sendMSFTTelemetryEvent: () => { /* noop */ }, |
| 30 | + } as unknown as ITelemetryService; |
| 31 | + const log = { |
| 32 | + warn: (msg: string) => { opts.warnings?.push(msg); }, |
| 33 | + } as unknown as ILogService; |
| 34 | + return new ToolResultCompressorService(config, telemetry, log); |
| 35 | +} |
| 36 | + |
| 37 | +function longText(prefix: string): string { |
| 38 | + // Must exceed MIN_COMPRESSIBLE_LENGTH (80) so filters get a chance to run. |
| 39 | + return prefix + ' ' + 'x'.repeat(200); |
| 40 | +} |
| 41 | + |
| 42 | +const replaceWithFooFilter: IToolResultFilter = { |
| 43 | + id: 'test.replaceWithFoo', |
| 44 | + toolNames: [TOOL], |
| 45 | + matches: () => true, |
| 46 | + apply: () => ({ text: 'foo', compressed: true }), |
| 47 | +}; |
| 48 | + |
| 49 | +describe('ToolResultCompressorService', () => { |
| 50 | + it('returns undefined when disabled', () => { |
| 51 | + const svc = makeService({ enabled: false }); |
| 52 | + svc.registerFilter(replaceWithFooFilter); |
| 53 | + const result = new LanguageModelToolResult([new LanguageModelTextPart(longText('hello'))]); |
| 54 | + expect(svc.maybeCompress(TOOL, {}, result)).toBeUndefined(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns undefined when no filters registered', () => { |
| 58 | + const svc = makeService({ enabled: true }); |
| 59 | + const result = new LanguageModelToolResult([new LanguageModelTextPart(longText('hello'))]); |
| 60 | + expect(svc.maybeCompress(TOOL, {}, result)).toBeUndefined(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns undefined when no filters match', () => { |
| 64 | + const svc = makeService({ enabled: true }); |
| 65 | + svc.registerFilter({ |
| 66 | + id: 'no-match', |
| 67 | + toolNames: [TOOL], |
| 68 | + matches: () => false, |
| 69 | + apply: () => ({ text: 'foo', compressed: true }), |
| 70 | + }); |
| 71 | + const result = new LanguageModelToolResult([new LanguageModelTextPart(longText('hello'))]); |
| 72 | + expect(svc.maybeCompress(TOOL, {}, result)).toBeUndefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('disables a throwing filter for the rest of the pass and warns once', () => { |
| 76 | + const warnings: string[] = []; |
| 77 | + const svc = makeService({ enabled: true, warnings }); |
| 78 | + let calls = 0; |
| 79 | + svc.registerFilter({ |
| 80 | + id: 'thrower', |
| 81 | + toolNames: [TOOL], |
| 82 | + matches: () => true, |
| 83 | + apply: () => { calls++; throw new Error('boom'); }, |
| 84 | + }); |
| 85 | + svc.registerFilter(replaceWithFooFilter); |
| 86 | + const result = new LanguageModelToolResult([ |
| 87 | + new LanguageModelTextPart(longText('a')), |
| 88 | + new LanguageModelTextPart(longText('b')), |
| 89 | + new LanguageModelTextPart(longText('c')), |
| 90 | + ]); |
| 91 | + const out = svc.maybeCompress(TOOL, {}, result)!; |
| 92 | + expect(out).toBeDefined(); |
| 93 | + // Throwing filter is invoked exactly once on the first text part, then disabled. |
| 94 | + expect(calls).toBe(1); |
| 95 | + expect(warnings.length).toBe(1); |
| 96 | + expect(warnings[0]).toContain('thrower'); |
| 97 | + // The other filter still rewrites every text part. |
| 98 | + for (const part of out.content) { |
| 99 | + expect(part).toBeInstanceOf(LanguageModelTextPart); |
| 100 | + expect((part as LanguageModelTextPart).value).toBe('foo'); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + it('preserves non-text parts unchanged', () => { |
| 105 | + const svc = makeService({ enabled: true }); |
| 106 | + svc.registerFilter(replaceWithFooFilter); |
| 107 | + const dataPart = new LanguageModelDataPart(new Uint8Array([1, 2, 3]), 'application/octet-stream'); |
| 108 | + const textPart = new LanguageModelTextPart(longText('hello')); |
| 109 | + const result = new LanguageModelToolResult([dataPart, textPart]); |
| 110 | + const out = svc.maybeCompress(TOOL, {}, result)!; |
| 111 | + expect(out.content[0]).toBe(dataPart); |
| 112 | + expect(out.content[1]).toBeInstanceOf(LanguageModelTextPart); |
| 113 | + expect((out.content[1] as LanguageModelTextPart).value).toBe('foo'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('preserves LanguageModelTextPart2 audience metadata when rewriting', () => { |
| 117 | + const svc = makeService({ enabled: true }); |
| 118 | + svc.registerFilter(replaceWithFooFilter); |
| 119 | + const audience = [LanguageModelPartAudience.Assistant, LanguageModelPartAudience.User]; |
| 120 | + const part = new LanguageModelTextPart2(longText('hello'), audience); |
| 121 | + const result = new LanguageModelToolResult([part]); |
| 122 | + const out = svc.maybeCompress(TOOL, {}, result)!; |
| 123 | + expect(out.content[0]).toBeInstanceOf(LanguageModelTextPart2); |
| 124 | + const rewritten = out.content[0] as LanguageModelTextPart2; |
| 125 | + expect(rewritten.value).toBe('foo'); |
| 126 | + expect(rewritten.audience).toEqual(audience); |
| 127 | + }); |
| 128 | + |
| 129 | + it('skips text parts shorter than the minimum compressible length', () => { |
| 130 | + const svc = makeService({ enabled: true }); |
| 131 | + svc.registerFilter(replaceWithFooFilter); |
| 132 | + const result = new LanguageModelToolResult([new LanguageModelTextPart('tiny')]); |
| 133 | + // Nothing was compressed because the part was below the threshold. |
| 134 | + expect(svc.maybeCompress(TOOL, {}, result)).toBeUndefined(); |
| 135 | + }); |
| 136 | +}); |
0 commit comments