index.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * `@deepseek-ai/dsh-web-search-perplexity`: registers a Perplexity-backed
  3. * `WebSearchProvider` with `ctx.web`. A function/namespace plugin (NOT a
  4. * default-export service): it registers INTO the seam's provider registry, like
  5. * `@deepseek-ai/dsh-llm-deepseek` registers an adapter into `ctx.llm`.
  6. *
  7. * @module @deepseek-ai/dsh-web-search-perplexity
  8. */
  9. import type { Context } from 'cordis'
  10. import z from 'schemastery'
  11. import type {} from '@deepseek-ai/dsh-web'
  12. import { PerplexitySearchProvider, PERPLEXITY_DEFAULT_BASE_URL, PERPLEXITY_DEFAULT_MAX_TOKENS, PERPLEXITY_DEFAULT_MODEL } from './provider.ts'
  13. export {
  14. PERPLEXITY_DEFAULT_BASE_URL,
  15. PERPLEXITY_DEFAULT_MAX_TOKENS,
  16. PERPLEXITY_DEFAULT_MODEL,
  17. PERPLEXITY_PROVIDER_ID,
  18. PerplexitySearchProvider,
  19. } from './provider.ts'
  20. export type { PerplexityRecency, PerplexitySearchProviderOptions } from './provider.ts'
  21. /** Cordis plugin name used by loader diagnostics. */
  22. export const name = 'web-search-perplexity'
  23. /** The web seam this provider registers into. */
  24. export const inject = ['web']
  25. /** Plugin config (all optional — `apply` fills env-var and constant defaults). */
  26. export interface Config {
  27. /** Perplexity API key. Falls back to `$PERPLEXITY_API_KEY`. Empty → unavailable. */
  28. apiKey?: string
  29. /** Endpoint base; `/chat/completions` is appended. Defaults to the public API. */
  30. baseURL?: string
  31. /** Search model name. Defaults to `sonar`. */
  32. model?: string
  33. /** Upper bound on generated answer tokens. Defaults to 1024. */
  34. maxTokens?: number
  35. /** Recency window sent as `search_recency_filter`. Omitted = no filter. */
  36. searchRecency?: 'day' | 'week' | 'month' | 'year'
  37. }
  38. export const Config: z<Config> = z.object({
  39. apiKey: z.string(),
  40. baseURL: z.string(),
  41. model: z.string(),
  42. maxTokens: z.number().step(1).min(1),
  43. searchRecency: z.union(['day', 'week', 'month', 'year'] as const),
  44. })
  45. /** Register the Perplexity search provider with `ctx.web`. */
  46. export function apply(ctx: Context, config: Config): void {
  47. ctx.web.registerSearchProvider(new PerplexitySearchProvider({
  48. apiKey: config.apiKey ?? process.env.PERPLEXITY_API_KEY ?? '',
  49. baseURL: config.baseURL ?? PERPLEXITY_DEFAULT_BASE_URL,
  50. model: config.model ?? PERPLEXITY_DEFAULT_MODEL,
  51. maxTokens: config.maxTokens ?? PERPLEXITY_DEFAULT_MAX_TOKENS,
  52. ...config.searchRecency !== undefined ? { searchRecency: config.searchRecency } : {},
  53. }))
  54. }