index.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Cooperative tool-call timeout enforcer. A tool declares `timeoutMs` and
  3. * promises to honor `exec.signal`; this wrapper arms that deadline and maps its
  4. * own expiry to `TOOL_TIMEOUT` without racing or abandoning the tool promise.
  5. * @module @deepseek-ai/dsh-timeout-policy
  6. */
  7. import type { Context } from 'cordis'
  8. import { deadline, timeoutOf } from '@deepseek-ai/dsh-timeout'
  9. import type { ToolExecutionResult } from '@deepseek-ai/dsh-tools'
  10. /**
  11. * The code owned by this plugin, used BOTH as the internal {@link deadline}
  12. * classification code AND as the structured error `code` on the replacement
  13. * tool result. Scoping {@link timeoutOf} to it keeps a nested outer deadline
  14. * (another `tools/execute` wrapper's timer that fired first) from being misread
  15. * as this plugin's own timeout — it reads as an ordinary upstream cancel.
  16. */
  17. export const TOOL_TIMEOUT = 'TOOL_TIMEOUT'
  18. /** Cordis plugin name used by loader diagnostics. */
  19. export const name = 'timeout-policy'
  20. /** The tool registry seam this plugin wraps (`tools/execute`) and reads (`get`). */
  21. export const inject = ['tools']
  22. /**
  23. * The structured result substituted when this plugin's deadline wins. `content`
  24. * is the model-facing message; `error.code` is the same {@link TOOL_TIMEOUT}
  25. * this plugin owns, so a retry/sandbox plugin (and replay) can route on it.
  26. *
  27. * @param timeoutMs - the elapsed budget, rendered into the model-facing message.
  28. * @returns the `isError` {@link ToolExecutionResult} with a `TOOL_TIMEOUT` error.
  29. */
  30. function toolTimeoutResult(timeoutMs: number): ToolExecutionResult {
  31. return {
  32. content: [{ type: 'text', text: `Error: tool call timed out after ${timeoutMs}ms` }],
  33. isError: true,
  34. error: { name: 'ToolTimeoutError', code: TOOL_TIMEOUT },
  35. }
  36. }
  37. /**
  38. * Register the timeout wrapper. It resolves the caller-visible tool definition,
  39. * temporarily replaces `exec.signal`, delegates, restores the upstream signal,
  40. * and replaces the result only when this wrapper's own timer fired.
  41. */
  42. export function apply(ctx: Context): void {
  43. ctx.on('tools/execute', async (exec, next): Promise<ToolExecutionResult> => {
  44. const timeoutMs = ctx.tools.get(exec.name, exec.agent)?.timeoutMs
  45. // A tool that declares no budget: no deadline, delegate unchanged.
  46. if (timeoutMs === undefined) return next()
  47. using d = deadline(exec.signal, timeoutMs, TOOL_TIMEOUT)
  48. // Swap the derived deadline onto exec for dispatch, then restore the
  49. // caller's own signal so post-execute listeners never see this plugin's
  50. // (possibly already-aborted) timeout signal. `undefined` is not assignable to
  51. // the optional `signal` under exactOptionalPropertyTypes, so branch on it.
  52. const upstream = exec.signal
  53. exec.signal = d.signal
  54. try {
  55. const result = await next()
  56. // If OUR timer fired (scoped by code — a nested outer deadline reads as
  57. // undefined here), the tool/capability saw the abort and reached
  58. // quiescence; replace whatever it returned (its own abort result) with the
  59. // structured TOOL_TIMEOUT the model sees.
  60. if (timeoutOf(d.signal, TOOL_TIMEOUT) !== undefined) {
  61. return toolTimeoutResult(timeoutMs)
  62. }
  63. return result
  64. } finally {
  65. if (upstream === undefined) delete exec.signal
  66. else exec.signal = upstream
  67. }
  68. })
  69. }