index.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 'cordis'
  9. import z from 'schemastery'
  10. import type {} from '@deepseek-ai/dsh-web'
  11. import { applyWebSearchTool, WEB_SEARCH_MAX_RESULTS } from './search.ts'
  12. import { applyWebFetchTool } from './fetch.ts'
  13. export { WEB_SEARCH_MAX_RESULTS, applyWebSearchTool, formatSearchOutput, parseSearchArgs, presentSearchCall } from './search.ts'
  14. export { applyWebFetchTool, formatFetchOutput, parseFetchArgs, presentFetchCall, renderBody } from './fetch.ts'
  15. export { htmlToMarkdown } from './html.ts'
  16. /** Cordis plugin name used by loader diagnostics. */
  17. export const name = 'tool-web'
  18. /** Services required by the web tool suite. */
  19. export const inject = ['tools', 'web', 'systemPrompt']
  20. /** Default cooperative tool-call timeout budget (ms) for the web tools. */
  21. export const DEFAULT_WEB_TOOL_TIMEOUT_MS = 30_000
  22. /** Plugin config: which web tools to register, the source cap, and per-tool budgets. */
  23. export interface Config {
  24. /** Register `web_search`. Defaults to true. */
  25. search?: boolean
  26. /** Register `web_fetch`. Defaults to true. */
  27. fetch?: boolean
  28. /** Upper bound on sources returned by one `web_search` call. */
  29. searchMaxResults?: number
  30. /** Cooperative timeout budget (ms) for `web_fetch`. Defaults to 30000. */
  31. fetchTimeoutMs?: number
  32. /** Cooperative timeout budget (ms) for `web_search`. Defaults to 30000. */
  33. searchTimeoutMs?: number
  34. }
  35. export const Config: z<Config> = z.object({
  36. search: z.boolean().default(true),
  37. fetch: z.boolean().default(true),
  38. searchMaxResults: z.number().default(WEB_SEARCH_MAX_RESULTS),
  39. fetchTimeoutMs: z.number().default(DEFAULT_WEB_TOOL_TIMEOUT_MS),
  40. searchTimeoutMs: z.number().default(DEFAULT_WEB_TOOL_TIMEOUT_MS),
  41. })
  42. /** The shape after schemastery applies its defaults to every field. */
  43. type ResolvedConfig = Required<Config>
  44. /** The result cap must be a positive integer (it bounds a provider's source list). */
  45. function assertPositiveInteger(name: string, value: number): void {
  46. if (!Number.isInteger(value) || value < 1) {
  47. throw new Error(`tool-web: ${name} must be a positive integer`)
  48. }
  49. }
  50. /**
  51. * Register the enabled web tools. `search`/`fetch` default to true; a product
  52. * that wants only one disables the other in config. Each tool's cooperative
  53. * timeout budget (`fetchTimeoutMs`/`searchTimeoutMs`, default 30000) is resolved
  54. * here and attached to the tool as `ToolDefinition.timeoutMs` for
  55. * `@deepseek-ai/dsh-timeout-policy` to enforce. The tools' disposers are
  56. * fiber-scoped (the effect-based registries clean up on dispose), so no manual
  57. * teardown is needed.
  58. */
  59. export function apply(ctx: Context, config: Config): void {
  60. // schemastery (Config) has already filled every defaulted field.
  61. const resolved = config as ResolvedConfig
  62. assertPositiveInteger('searchMaxResults', resolved.searchMaxResults)
  63. assertPositiveInteger('fetchTimeoutMs', resolved.fetchTimeoutMs)
  64. assertPositiveInteger('searchTimeoutMs', resolved.searchTimeoutMs)
  65. if (resolved.search) applyWebSearchTool(ctx, resolved.searchMaxResults, resolved.searchTimeoutMs)
  66. if (resolved.fetch) applyWebFetchTool(ctx, resolved.fetchTimeoutMs)
  67. }