index.ts 2.5 KB

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