pi-tool-duration 0.1.0
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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +72 -0
- package/extensions/tool-duration/index.ts +65 -0
- package/package.json +54 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.1.0] - 2026-06-24
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Initial `pi-tool-duration` package.
|
|
8
|
+
- Appends `[duration: Xs]` to model-visible Pi tool results when execution meets the configured threshold.
|
|
9
|
+
- Supports `--tool-duration-threshold-ms` and `PI_TOOL_DURATION_THRESHOLD_MS` overrides.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 pi-tool-duration 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.
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# pi-tool-duration
|
|
2
|
+
|
|
3
|
+
Appends `[duration: Xs]` to slow Pi tool results so the model can tell when a tool actually took time.
|
|
4
|
+
|
|
5
|
+
```text
|
|
6
|
+
hi
|
|
7
|
+
[duration: 5.0s]
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Why
|
|
11
|
+
|
|
12
|
+
Pi already shows tool timing in the TUI (`Took Xs`), but that timing is UI-only. This extension adds the elapsed time to the model-visible tool result for slow calls.
|
|
13
|
+
|
|
14
|
+
## How it works
|
|
15
|
+
|
|
16
|
+
The extension matches Pi `tool_call` and `tool_result` events by `toolCallId`. When elapsed time is at or above the configured threshold, it appends one text block:
|
|
17
|
+
|
|
18
|
+
```text
|
|
19
|
+
[duration: 5.0s]
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Scope: Pi tools that emit `tool_result` events, including built-ins and extension tools. Direct `!` / `!!` shell commands and RPC `bash` command messages are not tool results and are not annotated.
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pi install . # local, global settings
|
|
28
|
+
pi install -l --approve . # local, project settings
|
|
29
|
+
pi install npm:pi-tool-duration # after npm publish
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Try without installing
|
|
33
|
+
|
|
34
|
+
From this repo:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pi -e .
|
|
38
|
+
# or
|
|
39
|
+
pi -e ./extensions/tool-duration/index.ts
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Configure
|
|
43
|
+
|
|
44
|
+
Default threshold: `1000` ms.
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
PI_TOOL_DURATION_THRESHOLD_MS=0 pi -e . # annotate every tool result
|
|
48
|
+
pi -e . --tool-duration-threshold-ms 500 # annotate tools taking >= 500ms
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Invalid threshold values fall back to the default.
|
|
52
|
+
|
|
53
|
+
## Verify
|
|
54
|
+
|
|
55
|
+
In a session running the extension, ask Pi to use bash:
|
|
56
|
+
|
|
57
|
+
```text
|
|
58
|
+
Use bash to run: sleep 5; echo hi
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The model sees:
|
|
62
|
+
|
|
63
|
+
```text
|
|
64
|
+
hi
|
|
65
|
+
[duration: 5.0s]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
A fast command below the threshold stays unchanged.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-tool-duration
|
|
3
|
+
*
|
|
4
|
+
* Appends `[duration: Xs]` to slow tool results so the model sees how long a
|
|
5
|
+
* call actually took. pi already measures this for the TUI ("Took Xs") but the
|
|
6
|
+
* model does not see that timing.
|
|
7
|
+
*/
|
|
8
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
|
|
10
|
+
const DEFAULT_THRESHOLD_MS = 1000;
|
|
11
|
+
const DURATION_RE = /^\[duration: \d+(?:\.\d+)?s\]$/;
|
|
12
|
+
|
|
13
|
+
function parseThreshold(value: unknown): number | undefined {
|
|
14
|
+
if (typeof value !== "string" && typeof value !== "number") return undefined;
|
|
15
|
+
if (typeof value === "string" && value.trim() === "") return undefined;
|
|
16
|
+
const parsed = Number(value);
|
|
17
|
+
return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function thresholdMs(pi: ExtensionAPI): number {
|
|
21
|
+
return (
|
|
22
|
+
parseThreshold(pi.getFlag("tool-duration-threshold-ms")) ??
|
|
23
|
+
parseThreshold(process.env.PI_TOOL_DURATION_THRESHOLD_MS) ??
|
|
24
|
+
DEFAULT_THRESHOLD_MS
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function alreadyAnnotated(content: Array<{ type: string; text?: string }>): boolean {
|
|
29
|
+
const last = content.at(-1);
|
|
30
|
+
return last?.type === "text" && typeof last.text === "string" && DURATION_RE.test(last.text);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default function (pi: ExtensionAPI) {
|
|
34
|
+
const starts = new Map<string, number>();
|
|
35
|
+
|
|
36
|
+
pi.registerFlag("tool-duration-threshold-ms", {
|
|
37
|
+
description: "Minimum tool duration in milliseconds before appending [duration: Xs] to the model-visible result",
|
|
38
|
+
type: "string",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
pi.on("tool_call", async (event) => {
|
|
42
|
+
starts.set(event.toolCallId, performance.now());
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
pi.on("tool_result", async (event) => {
|
|
46
|
+
const startedAt = starts.get(event.toolCallId);
|
|
47
|
+
starts.delete(event.toolCallId);
|
|
48
|
+
if (startedAt === undefined) return;
|
|
49
|
+
|
|
50
|
+
const ms = performance.now() - startedAt;
|
|
51
|
+
if (ms < thresholdMs(pi) || alreadyAnnotated(event.content)) return;
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
content: [
|
|
55
|
+
...event.content,
|
|
56
|
+
{ type: "text" as const, text: `[duration: ${(ms / 1000).toFixed(1)}s]` },
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const clearStarts = () => starts.clear();
|
|
62
|
+
pi.on("session_start", clearStarts);
|
|
63
|
+
pi.on("session_shutdown", clearStarts);
|
|
64
|
+
pi.on("agent_end", clearStarts);
|
|
65
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-tool-duration",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Append model-visible durations to slow Pi tool results",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": "Mitch Fultz (https://github.com/fitchmultz)",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"pi",
|
|
10
|
+
"pi-package",
|
|
11
|
+
"pi-extension",
|
|
12
|
+
"tool-duration",
|
|
13
|
+
"observability"
|
|
14
|
+
],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/fitchmultz/pi-tool-duration.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/fitchmultz/pi-tool-duration/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/fitchmultz/pi-tool-duration#readme",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=22.19.0"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"extensions/tool-duration/index.ts",
|
|
28
|
+
"README.md",
|
|
29
|
+
"CHANGELOG.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"typecheck": "tsc --noEmit",
|
|
34
|
+
"test": "node --test test/*.test.mjs",
|
|
35
|
+
"check": "npm run typecheck && npm test && npm pack --dry-run"
|
|
36
|
+
},
|
|
37
|
+
"pi": {
|
|
38
|
+
"extensions": [
|
|
39
|
+
"./extensions/tool-duration"
|
|
40
|
+
]
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@earendil-works/pi-coding-agent": "^0.80.2",
|
|
44
|
+
"typescript": "^5.9.3"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"@earendil-works/pi-coding-agent": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|