index.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. const message = `tool call timed out after ${timeoutMs}ms`
  32. return {
  33. content: [{ type: 'text', text: `Error: ${message}` }],
  34. isError: true,
  35. error: { message, info: { name: 'ToolTimeoutError', code: TOOL_TIMEOUT } },
  36. }
  37. }
  38. /**
  39. * Register the timeout wrapper. It resolves the caller-visible tool definition,
  40. * temporarily replaces `exec.signal`, delegates, restores the upstream signal,
  41. * and replaces the result only when this wrapper's own timer fired.
  42. */
  43. export function apply(ctx: Context): void {
  44. ctx.on('tools/execute', async (exec, next): Promise<ToolExecutionResult> => {
  45. const timeoutMs = ctx.tools.get(exec.name, exec.agent)?.timeoutMs
  46. // A tool that declares no budget: no deadline, delegate unchanged.
  47. if (timeoutMs === undefined) return next()
  48. using d = deadline(exec.signal, timeoutMs, TOOL_TIMEOUT)
  49. // Swap the derived deadline onto exec for dispatch, then restore the
  50. // caller's own signal so post-execute listeners never see this plugin's
  51. // (possibly already-aborted) timeout signal.
  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. exec.signal = upstream
  66. }
  67. })
  68. }