mirror of
https://github.com/earendil-works/pi.git
synced 2026-06-18 15:54:04 +08:00
bcea4b2e27
Add streamingBehavior to InputEvent so extensions can distinguish idle prompts from mid-stream steers and queued follow-ups. - Add streamingBehavior field to InputEvent type - Thread it through ExtensionRunner.emitInput() and AgentSession.prompt() - Add streaming-aware input gate example with tests - Document in extensions.md
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
/**
|
|
* 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\`\`\``,
|
|
};
|
|
});
|
|
}
|