1
0

index.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * The model-facing web tool suite (`web_search`, `web_fetch`) over the `ctx.web`
  3. * seam. This root plugin registers the tools the product has ENABLED, composing
  4. * the per-tool registration helpers (`applyWebSearchTool`, `applyWebFetchTool`).
  5. *
  6. * The package owns model-facing concerns only — tool names, JSON schemas,
  7. * argument validation, prompt sections, result-cap constants, result formatting,
  8. * HTML→markdown presentation. All web access goes through `ctx.web`; this
  9. * package never imports a concrete provider package.
  10. *
  11. * Tool registration follows product/app ENABLEMENT, not backend availability: a
  12. * tool stays visible even when its selected provider is missing/misconfigured,
  13. * and execution fails with a structured `WebError` (resolved by the seam at call
  14. * time). That keeps the model schema stable without making plugin load order,
  15. * credential state, or HMR timing part of the model-facing contract.
  16. *
  17. * @module @deepseek-ai/dsh-tool-web
  18. */
  19. import type { Context } from 'cordis'
  20. import z from 'schemastery'
  21. import type {} from '@deepseek-ai/dsh-web'
  22. import { applyWebSearchTool, WEB_SEARCH_MAX_RESULTS } from './search.ts'
  23. import { applyWebFetchTool } from './fetch.ts'
  24. export { WEB_SEARCH_MAX_RESULTS, applyWebSearchTool, formatSearchOutput, parseSearchArgs, presentSearchCall } from './search.ts'
  25. export { applyWebFetchTool, formatFetchOutput, parseFetchArgs, presentFetchCall, renderBody } from './fetch.ts'
  26. export { htmlToMarkdown } from './html.ts'
  27. /** Cordis plugin name used by loader diagnostics. */
  28. export const name = 'tool-web'
  29. /** Services required by the web tool suite. */
  30. export const inject = ['tools', 'web', 'systemPrompt']
  31. /** Plugin config: which web tools to register, and the `web_search` source cap. */
  32. export interface Config {
  33. /** Register `web_search`. Defaults to true. */
  34. search?: boolean
  35. /** Register `web_fetch`. Defaults to true. */
  36. fetch?: boolean
  37. /** Upper bound on sources returned by one `web_search` call. */
  38. searchMaxResults?: number
  39. }
  40. export const Config: z<Config> = z.object({
  41. search: z.boolean().default(true),
  42. fetch: z.boolean().default(true),
  43. searchMaxResults: z.number().default(WEB_SEARCH_MAX_RESULTS),
  44. })
  45. /** The shape after schemastery applies its defaults to every field. */
  46. type ResolvedConfig = Required<Config>
  47. /** The result cap must be a positive integer (it bounds a provider's source list). */
  48. function assertPositiveInteger(name: string, value: number): void {
  49. if (!Number.isInteger(value) || value < 1) {
  50. throw new Error(`tool-web: ${name} must be a positive integer`)
  51. }
  52. }
  53. /**
  54. * Register the enabled web tools. `search`/`fetch` default to true; a product
  55. * that wants only one disables the other in config. 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. if (resolved.search) applyWebSearchTool(ctx, resolved.searchMaxResults)
  64. if (resolved.fetch) applyWebFetchTool(ctx)
  65. }