index.ts 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Model-facing `web_search` and `web_fetch` tools over `ctx.web`. This package owns schemas,
  3. * validation, prompt guidance, limits, and presentation, never concrete providers. Enablement
  4. * controls tool registration; an enabled tool remains visible when its provider is unavailable
  5. * and fails with a structured error at execution time.
  6. * @module @deepseek-ai/dsh-tool-web
  7. */
  8. import type { Context } from '@deepseek-ai/cordis'
  9. import z from '@deepseek-ai/schemastery'
  10. import type {} from '@deepseek-ai/dsh-web'
  11. import { applyWebSearchTool, WEB_SEARCH_MAX_QUERIES, WEB_SEARCH_MAX_RESULTS } from './search.ts'
  12. import { applyWebFetchTool } from './fetch.ts'
  13. export { WEB_SEARCH_MAX_QUERIES, WEB_SEARCH_MAX_RESULTS, applyWebSearchTool, formatSearchOutput, presentSearchCall, presentSearchResult, searchMetaFromValue, searchMetaFromResult } from './search.ts'
  14. export type { WebSearchMeta } from './search.ts'
  15. export { applyWebFetchTool, formatFetchOutput, parseFetchArgs, presentFetchCall, presentFetchResult, fetchMetaFromValue, fetchMetaFromResult } from './fetch.ts'
  16. export type { WebFetchMeta } from './fetch.ts'
  17. /** Cordis plugin name used by loader diagnostics. */
  18. export const name = 'tool-web'
  19. /** Services required by the web tool suite. */
  20. export const inject = ['tools', 'web', 'systemPrompt']
  21. /** Default cooperative tool-call timeout budget (ms) for the web tools. */
  22. export const DEFAULT_WEB_TOOL_TIMEOUT_MS = 30_000
  23. /**
  24. * Default cap on one `web_fetch` output and on source characters converted
  25. * synchronously. This leaves headroom above the local provider's default
  26. * 100,000-character body cap while bounding custom providers and rendered output.
  27. */
  28. export const DEFAULT_FETCH_MAX_OUTPUT_CHARS = 200_000
  29. /** Plugin config: which web tools to register, search bounds, per-tool budgets, and the fetch output cap. */
  30. export interface Config {
  31. /** Register `web_search`. Defaults to true. */
  32. search?: boolean
  33. /** Register `web_fetch`. Defaults to true. */
  34. fetch?: boolean
  35. /** Upper bound on sources returned by one `web_search` call. */
  36. searchMaxResults?: number
  37. /** Upper bound on queries accepted by one `web_search` call. */
  38. searchMaxQueries?: number
  39. /** Cooperative timeout budget (ms) for `web_fetch`. Defaults to 30000. */
  40. fetchTimeoutMs?: number
  41. /** Cooperative timeout budget (ms) for `web_search`. Defaults to 30000. */
  42. searchTimeoutMs?: number
  43. /** Cap on source characters converted and complete `web_fetch` output characters. Defaults to 200000. */
  44. fetchMaxOutputChars?: number
  45. }
  46. export const Config: z<Config> = z.object({
  47. search: z.boolean().default(true),
  48. fetch: z.boolean().default(true),
  49. searchMaxResults: z.number().default(WEB_SEARCH_MAX_RESULTS),
  50. searchMaxQueries: z.number().default(WEB_SEARCH_MAX_QUERIES),
  51. fetchTimeoutMs: z.number().default(DEFAULT_WEB_TOOL_TIMEOUT_MS),
  52. searchTimeoutMs: z.number().default(DEFAULT_WEB_TOOL_TIMEOUT_MS),
  53. fetchMaxOutputChars: z.number().default(DEFAULT_FETCH_MAX_OUTPUT_CHARS),
  54. })
  55. /** Complete config after schemastery applies every field default. */
  56. type ResolvedConfig = Required<Config>
  57. /** Configured count, timeout, and character caps must be positive integers. */
  58. function assertPositiveInteger(name: string, value: number): void {
  59. if (!Number.isInteger(value) || value < 1) {
  60. throw new Error(`tool-web: ${name} must be a positive integer`)
  61. }
  62. }
  63. /**
  64. * Register the enabled web tools. `search`/`fetch` default to true; a product
  65. * that wants only one disables the other in config. Each tool's cooperative
  66. * timeout budget (`fetchTimeoutMs`/`searchTimeoutMs`, default 30000) is resolved
  67. * here and attached to the tool as `ToolDefinition.timeoutMs` for
  68. * `@deepseek-ai/dsh-tool-call-timeout-policy` to enforce. The tools' disposers are
  69. * fiber-scoped (the effect-based registries clean up on dispose), so no manual
  70. * teardown is needed.
  71. */
  72. export function apply(ctx: Context, config: Config): void {
  73. // schemastery (Config) has already filled every defaulted field.
  74. const resolved = config as ResolvedConfig
  75. assertPositiveInteger('searchMaxResults', resolved.searchMaxResults)
  76. assertPositiveInteger('searchMaxQueries', resolved.searchMaxQueries)
  77. assertPositiveInteger('fetchTimeoutMs', resolved.fetchTimeoutMs)
  78. assertPositiveInteger('searchTimeoutMs', resolved.searchTimeoutMs)
  79. assertPositiveInteger('fetchMaxOutputChars', resolved.fetchMaxOutputChars)
  80. if (resolved.search) {
  81. applyWebSearchTool(ctx, resolved.searchMaxResults, resolved.searchMaxQueries, resolved.searchTimeoutMs, resolved.fetch)
  82. }
  83. if (resolved.fetch) applyWebFetchTool(ctx, resolved.fetchTimeoutMs, resolved.fetchMaxOutputChars)
  84. }