| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- /**
- * Perplexity-backed `WebSearchProvider` plugin. It contributes to the
- * `ctx.web` registry without owning the service.
- *
- * @module @deepseek-ai/dsh-web-search-perplexity
- */
- import type { Context } from '@deepseek-ai/cordis'
- import { launchEnvironmentOf } from '@deepseek-ai/dsh-launch-environment'
- import z from '@deepseek-ai/schemastery'
- import type {} from '@deepseek-ai/dsh-web'
- import { PerplexitySearchProvider, PERPLEXITY_DEFAULT_BASE_URL, PERPLEXITY_DEFAULT_MAX_TOKENS, PERPLEXITY_DEFAULT_MODEL } from './provider.ts'
- export {
- PERPLEXITY_DEFAULT_BASE_URL,
- PERPLEXITY_DEFAULT_MAX_TOKENS,
- PERPLEXITY_DEFAULT_MODEL,
- PERPLEXITY_PROVIDER_ID,
- PerplexitySearchProvider,
- } from './provider.ts'
- export type { PerplexityRecency, PerplexitySearchProviderOptions } from './provider.ts'
- /** Cordis plugin name used by loader diagnostics. */
- export const name = 'web-search-perplexity'
- /** The web seam this provider registers into. */
- export const inject = ['web']
- /** Plugin config (all optional — `apply` fills env-var and constant defaults). */
- export interface Config {
- /** Perplexity API key. Falls back to `$PERPLEXITY_API_KEY`. Empty → unavailable. */
- apiKey?: string
- /** Endpoint base; `/chat/completions` is appended. Defaults to the public API. */
- baseURL?: string
- /** Search model name. Defaults to `sonar`. */
- model?: string
- /** Upper bound on generated answer tokens. Defaults to 1024. */
- maxTokens?: number
- /** Recency window sent as `search_recency_filter`. Omitted = no filter. */
- searchRecency?: 'day' | 'week' | 'month' | 'year'
- }
- export const Config: z<Config> = z.object({
- apiKey: z.string(),
- baseURL: z.string(),
- model: z.string(),
- maxTokens: z.number().step(1).min(1),
- searchRecency: z.union(['day', 'week', 'month', 'year'] as const),
- })
- /** Register the Perplexity search provider with `ctx.web`. */
- export function apply(ctx: Context, config: Config): void {
- ctx.web.registerSearchProvider(new PerplexitySearchProvider({
- // Every environment layer may name this key: the product trusts the
- // project it is launched in, and the managed store is not involved here.
- apiKey: config.apiKey ?? launchEnvironmentOf(ctx).get('PERPLEXITY_API_KEY')?.value ?? '',
- baseURL: config.baseURL ?? PERPLEXITY_DEFAULT_BASE_URL,
- model: config.model ?? PERPLEXITY_DEFAULT_MODEL,
- maxTokens: config.maxTokens ?? PERPLEXITY_DEFAULT_MAX_TOKENS,
- ...config.searchRecency !== undefined ? { searchRecency: config.searchRecency } : {},
- }))
- }
|