File size: 4,365 Bytes
5448d8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it } from "node:test";
import assert from "node:assert/strict";

import {
  detectCommandType,
  loadRtkFilters,
  matchRtkFilter,
  processRtkText,
  applyRtkCompression,
  rtkEngine,
} from "../../../open-sse/services/compression/index.ts";

describe("RTK compression engine", () => {
  it("detects TypeScript build output", () => {
    const output = "src/a.ts:1:1 - error TS2322: Type 'string' is not assignable";
    const detection = detectCommandType(output, "tsc --noEmit");
    assert.equal(detection.type, "build-typescript");
    assert.equal(detection.category, "build");
  });

  it("loads builtin declarative filters", () => {
    const filters = loadRtkFilters({ refresh: true });
    assert.ok(filters.length >= 10);
    assert.ok(filters.some((filter) => filter.id === "git-diff"));
  });

  it("matches filters by detected output type", () => {
    const filter = matchRtkFilter("diff --git a/a.ts b/a.ts\n@@ -1,1 +1,1 @@", "git diff");
    assert.equal(filter?.id, "git-diff");
  });

  it("compresses repeated tool output", () => {
    const output = Array.from({ length: 20 }, () => "same noisy line").join("\n");
    const result = processRtkText(output);
    assert.equal(result.compressed, true);
    assert.ok(result.text.includes("[rtk:dropped"));
  });

  it("honors disabled and empty-message runtime branches", () => {
    const disabled = applyRtkCompression(
      { messages: [{ role: "tool", content: "same\nsame\nsame" }] },
      { config: { enabled: false } }
    );
    assert.equal(disabled.compressed, false);
    assert.equal(disabled.stats, null);

    const empty = applyRtkCompression({ messages: [] });
    assert.equal(empty.compressed, false);
    assert.equal(empty.stats, null);
  });

  it("respects enabled and disabled filter lists", () => {
    const output = "diff --git a/a.ts b/a.ts\n@@ -1,1 +1,1 @@\n-error\n+ok";

    const disabled = processRtkText(output, {
      command: "git diff",
      config: { disabledFilters: ["git-diff"] },
    });
    assert.ok(!disabled.rulesApplied.includes("git-diff:keep"));

    const enabledMismatch = processRtkText(output, {
      command: "git diff",
      config: { enabledFilters: ["git-status"] },
    });
    assert.ok(!enabledMismatch.rulesApplied.includes("git-diff:keep"));
  });

  it("exposes RTK engine schema, validation, apply and compress contracts", () => {
    assert.ok(rtkEngine.getConfigSchema().some((field) => field.key === "rawOutputRetention"));
    assert.equal(rtkEngine.validateConfig({ intensity: "invalid" }).valid, false);
    assert.equal(rtkEngine.validateConfig({ rawOutputRetention: "always" }).valid, true);

    const body = { messages: [{ role: "tool", content: "same\nsame\nsame\nsame" }] };
    assert.equal(
      rtkEngine.apply(body, { config: { rtkConfig: { enabled: true } } }).stats?.engine,
      "rtk"
    );
    assert.equal(rtkEngine.compress(body, { enabled: true }).stats?.engine, "rtk");
  });

  it("applies to chat tool messages", () => {
    const body = {
      messages: [{ role: "tool", content: Array.from({ length: 20 }, () => "same").join("\n") }],
    };
    const result = applyRtkCompression(body);
    assert.equal(result.compressed, true);
    assert.equal(result.stats?.mode, "rtk");
  });

  it("compresses multipart text parts independently without duplicating output", () => {
    const imagePart = { type: "image_url", image_url: { url: "data:image/png;base64,abc" } };
    const body = {
      messages: [
        {
          role: "tool",
          content: [
            { type: "text", text: Array.from({ length: 12 }, () => "alpha noisy line").join("\n") },
            imagePart,
            { type: "text", text: Array.from({ length: 12 }, () => "beta noisy line").join("\n") },
          ],
        },
      ],
    };

    const result = applyRtkCompression(body);
    const content = (result.body.messages as typeof body.messages)[0].content;

    assert.equal(result.compressed, true);
    assert.ok(Array.isArray(content));
    assert.match(content[0].text ?? "", /alpha noisy line/);
    assert.match(content[2].text ?? "", /beta noisy line/);
    assert.notEqual(content[0].text, content[2].text);
    assert.deepEqual(content[1], imagePart);
  });
});