harnesstrim 0.0.5 → 0.0.7
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/LICENSE +21 -0
- package/assets/adapter-hermes/plugin/__init__.py +5 -0
- package/assets/adapter-pi/extension/{harnesstrim.ts → index.ts} +35 -17
- package/dist/cli.mjs +915 -105
- package/package.json +10 -12
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HarnessTrim contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -225,14 +225,19 @@ def _write_metric(tool: str, reducer: str | None, before: int, after: int) -> No
|
|
|
225
225
|
directory lazily and swallows any error — telemetry must never crash the plugin.
|
|
226
226
|
"""
|
|
227
227
|
import datetime as _dt
|
|
228
|
+
import uuid as _uuid
|
|
228
229
|
|
|
229
230
|
event = {
|
|
231
|
+
"schemaVersion": 1,
|
|
232
|
+
"eventId": str(_uuid.uuid4()),
|
|
230
233
|
"ts": _dt.datetime.now(_dt.timezone.utc).isoformat(),
|
|
231
234
|
"harness": "hermes",
|
|
232
235
|
"tool": tool,
|
|
233
236
|
"reducer": reducer,
|
|
234
237
|
"beforeChars": before,
|
|
235
238
|
"afterChars": after,
|
|
239
|
+
"beforeTokens": None,
|
|
240
|
+
"afterTokens": None,
|
|
236
241
|
}
|
|
237
242
|
try:
|
|
238
243
|
METRICS_PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
//
|
|
3
3
|
// Pi fires `tool_result` after a tool finishes and before the result reaches the model;
|
|
4
4
|
// handlers chain like middleware and may return a patch ({ content, details, isError }).
|
|
5
|
-
// This extension reduces
|
|
5
|
+
// This extension reduces text chunks in structured tool results (test runners, git diffs, ...)
|
|
6
6
|
// by shelling out to `harnesstrim reduce`, so it is self-contained (no workspace imports)
|
|
7
7
|
// and loads from `~/.pi/agent/extensions/` or `<project>/.pi/extensions/`.
|
|
8
8
|
//
|
|
@@ -12,19 +12,28 @@
|
|
|
12
12
|
// HARNESSTRIM_MINLENGTH=<chars> (default 400)
|
|
13
13
|
import { spawnSync } from "node:child_process";
|
|
14
14
|
|
|
15
|
+
type TextContent = { type: "text"; text: string };
|
|
16
|
+
type ToolContent = TextContent | { type: string; [key: string]: unknown };
|
|
17
|
+
|
|
15
18
|
interface ToolResultEvent {
|
|
16
|
-
content?:
|
|
19
|
+
content?: ToolContent[];
|
|
17
20
|
isError?: boolean;
|
|
18
21
|
}
|
|
19
22
|
interface ExtensionAPI {
|
|
20
23
|
on(event: string, handler: (event: ToolResultEvent, ctx: unknown) => unknown): void;
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
const
|
|
26
|
+
const runtime = globalThis as typeof globalThis & { process?: NodeJS.Process };
|
|
27
|
+
const env = runtime.process?.env ?? {};
|
|
24
28
|
const MODE = env.HARNESSTRIM_MODE ?? "dryrun";
|
|
25
29
|
const MIN_LENGTH = Number(env.HARNESSTRIM_MINLENGTH ?? "400") || 400;
|
|
26
30
|
const MARKER = "[harnesstrim";
|
|
27
31
|
|
|
32
|
+
/** True when a text chunk should not be reduced: too short, or already reduced. */
|
|
33
|
+
export function shouldSkip(text: string, minLength: number): boolean {
|
|
34
|
+
return text.length < minLength || text.includes(MARKER);
|
|
35
|
+
}
|
|
36
|
+
|
|
28
37
|
function reduceViaCli(text: string): string | null {
|
|
29
38
|
try {
|
|
30
39
|
const r = spawnSync("harnesstrim", ["reduce", "--min-length", String(MIN_LENGTH)], {
|
|
@@ -44,19 +53,28 @@ function reduceViaCli(text: string): string | null {
|
|
|
44
53
|
export default function harnesstrim(pi: ExtensionAPI): void {
|
|
45
54
|
if (MODE === "off") return;
|
|
46
55
|
pi.on("tool_result", async (event) => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
if (!Array.isArray(event.content)) return;
|
|
57
|
+
|
|
58
|
+
let changed = false;
|
|
59
|
+
const content = event.content.map((chunk) => {
|
|
60
|
+
if (chunk.type !== "text" || typeof chunk.text !== "string") return chunk;
|
|
61
|
+
const text = chunk.text;
|
|
62
|
+
if (shouldSkip(text, MIN_LENGTH)) return chunk;
|
|
63
|
+
|
|
64
|
+
const reduced = reduceViaCli(text);
|
|
65
|
+
if (!reduced || reduced.length >= text.length) return chunk;
|
|
66
|
+
|
|
67
|
+
if (MODE === "dryrun") {
|
|
68
|
+
runtime.process?.stderr?.write(
|
|
69
|
+
`[harnesstrim] dryrun tool_result: ${text.length} -> ${reduced.length} chars\n`
|
|
70
|
+
);
|
|
71
|
+
return chunk;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
changed = true;
|
|
75
|
+
return { ...chunk, text: reduced };
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return changed ? { content } : undefined;
|
|
61
79
|
});
|
|
62
80
|
}
|