-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput-transform-streaming.ts
More file actions
39 lines (34 loc) · 1.18 KB
/
input-transform-streaming.ts
File metadata and controls
39 lines (34 loc) · 1.18 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
/**
* Streaming-Aware Input Gate
*
* Demonstrates `event.streamingBehavior` to skip expensive pre-processing
* during mid-stream steering, where low latency matters.
*
* This extension prepends `git diff --stat` output when the user mentions
* file changes, giving the model immediate context. During steering the
* exec call is skipped so the correction reaches the model without delay.
*
* Start pi with this extension:
* pi -e ./examples/extensions/input-transform-streaming.ts
*/
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
const TRIGGER = /\b(changes?|diff|modified)\b/i;
export default function (pi: ExtensionAPI) {
pi.on("input", async (event) => {
// During steering, skip the exec call — corrections should be fast
if (event.streamingBehavior === "steer") {
return { action: "continue" };
}
if (!TRIGGER.test(event.text)) {
return { action: "continue" };
}
const { stdout, code } = await pi.exec("git", ["diff", "--stat"]);
if (code !== 0 || !stdout.trim()) {
return { action: "continue" };
}
return {
action: "transform",
text: `${event.text}\n\nCurrent uncommitted changes:\n\`\`\`\n${stdout.trim()}\n\`\`\``,
};
});
}