Created
January 23, 2026 17:26
-
-
Save aliou/e41e3a9d5abf46682e41e4b2c8797d1d to your computer and use it in GitHub Desktop.
Sleep tool for testing abort behavior with multiple queued tools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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); | |
| }); | |
| }, | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "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