Skip to content

Instantly share code, notes, and snippets.

@aliou
Created January 23, 2026 17:26
Show Gist options
  • Select an option

  • Save aliou/e41e3a9d5abf46682e41e4b2c8797d1d to your computer and use it in GitHub Desktop.

Select an option

Save aliou/e41e3a9d5abf46682e41e4b2c8797d1d to your computer and use it in GitHub Desktop.
Sleep tool for testing abort behavior with multiple queued tools
/**
* Sleep Tool - For testing abort behavior with multiple queued tools
*/
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
import { Type } from "@sinclair/typebox";
export default function (pi: ExtensionAPI) {
pi.registerTool({
name: "sleep",
label: "Sleep",
description:
"Sleep for a specified number of seconds. Use this to test tool execution timing.",
parameters: Type.Object({
seconds: Type.Number({
description: "Number of seconds to sleep",
minimum: 1,
maximum: 60,
}),
}),
async execute(_toolCallId, params, onUpdate, _ctx, signal) {
const { seconds } = params as { seconds: number };
const startTime = Date.now();
const endTime = startTime + seconds * 1000;
// Check if already aborted
if (signal?.aborted) {
return {
content: [{ type: "text", text: "Aborted before starting" }],
details: { seconds, completed: false, aborted: true },
};
}
return new Promise((resolve) => {
let intervalId: NodeJS.Timeout;
let resolved = false;
const cleanup = () => {
if (intervalId) clearInterval(intervalId);
};
const finish = (aborted: boolean) => {
if (resolved) return;
resolved = true;
cleanup();
const elapsed = Math.round((Date.now() - startTime) / 1000);
resolve({
content: [
{
type: "text",
text: aborted
? `Aborted after ${elapsed}s (of ${seconds}s)`
: `Slept for ${seconds} seconds`,
},
],
details: { seconds, completed: !aborted, elapsed, aborted },
});
};
// Handle abort signal
if (signal) {
signal.addEventListener("abort", () => finish(true), { once: true });
}
// Update progress every 500ms
intervalId = setInterval(() => {
if (resolved) return;
const elapsed = Math.round((Date.now() - startTime) / 1000);
const remaining = seconds - elapsed;
onUpdate?.({
content: [
{
type: "text",
text: `Sleeping... ${elapsed}s elapsed, ${remaining}s remaining`,
},
],
details: { seconds, elapsed, remaining },
});
}, 500);
// Complete after duration
setTimeout(() => finish(false), seconds * 1000);
});
},
});
}
{
"name": "sleep-test",
"version": "1.0.0",
"description": "Sleep tool for testing abort behavior with multiple queued tools",
"pi": {
"extensions": ["./index.ts"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment