| 1 | // A tool that waits until cancelled, and an async disposer that takes 300 ms, |
| 2 | // so teardown ordering and cancellation are observable. |
| 3 | export const name = 'slow-tool' |
| 4 | export const inject = ['tools'] |
| 5 | |
| 6 | export function apply(ctx) { |
| 7 | let inflight |
| 8 | ctx.effect(() => async () => { |
| 9 | await new Promise((resolve) => setTimeout(resolve, 300)) |
| 10 | await inflight?.catch(() => undefined) |
| 11 | }, 'slow-tool drain') |
| 12 | ctx.tools.register({ |
| 13 | name: 'slow_wait', |
| 14 | description: 'Wait for `ms` milliseconds (default 30000), or until cancelled.', |
| 15 | parameters: { type: 'object', properties: { ms: { type: 'number' } } }, |
| 16 | execute(args, exec) { |
| 17 | inflight = new Promise((resolve, reject) => { |
| 18 | const timer = setTimeout(() => resolve({ waited: args.ms ?? 30000 }), args.ms ?? 30000) |
| 19 | exec.signal?.addEventListener('abort', () => { |
| 20 | clearTimeout(timer) |
| 21 | reject(new Error('aborted')) |
| 22 | }) |
| 23 | }) |
| 24 | return inflight |
| 25 | }, |
| 26 | }) |
| 27 | } |
| 28 |