/** * The "Firecrawl" settings page contributed to the DSH Settings shell. * * The API key is write-only in the wire direction: the host schema marks it * `role('secret')`, so this page never receives the literal. It shows * whether a key is configured, accepts a replacement (blank = keep the * current key), and offers an explicit clear button. All commits apply live: * the host provider starts using the new values on the next search. */ import { useCallback, useEffect, useState } from 'react' import { Button, Input } from '@deepseek-ai/dsh-client-ui-primitives' // Type-only: pulls the settings shell's SlotMap merge for 'settings.section'. import type {} from '@deepseek-ai/dsh-client-ui-settings/client' import type { PropsRuntime } from '@deepseek-ai/dsh-client-ui-slots' import { FIRECRAWL_DEFAULT_BASE_URL, FIRECRAWL_DEFAULT_MAX_SNIPPET_CHARS, } from '../settings-shared.ts' import { FirecrawlSettingsApiError, getFirecrawlSettings, isApiKeyConfigured, mutateFirecrawlSettings, type FirecrawlSettingsOp, type FirecrawlSettingsView, } from './api.ts' import css from './FirecrawlSettingsSection.module.css' /** Props the settings shell binds for this section. */ export type FirecrawlSettingsSectionProps = PropsRuntime<'settings.section'> /** Local drafts for the four settings controls. */ interface Drafts { useFirecrawl: boolean apiKey: string baseURL: string limit: string maxSnippetChars: string } const INITIAL_DRAFTS: Drafts = { useFirecrawl: true, apiKey: '', baseURL: FIRECRAWL_DEFAULT_BASE_URL, limit: '', maxSnippetChars: String(FIRECRAWL_DEFAULT_MAX_SNIPPET_CHARS), } /** Read the redacted resolved value into editable draft strings. */ function draftsOf(view: FirecrawlSettingsView): Drafts { const value = view.value !== null && typeof view.value === 'object' ? view.value as Record : {} return { useFirecrawl: typeof value.useFirecrawl === 'boolean' ? value.useFirecrawl : true, apiKey: '', baseURL: typeof value.baseURL === 'string' && value.baseURL.length > 0 ? value.baseURL : FIRECRAWL_DEFAULT_BASE_URL, limit: typeof value.limit === 'number' ? String(value.limit) : '', maxSnippetChars: typeof value.maxSnippetChars === 'number' ? String(value.maxSnippetChars) : String(FIRECRAWL_DEFAULT_MAX_SNIPPET_CHARS), } } /** Parse a positive whole number, or return null for an invalid draft. */ function parsePositiveInteger(raw: string): number | null { const value = Number(raw) return Number.isInteger(value) && value > 0 ? value : null } /** * Inline eye icon used by the API-key reveal toggle (no eye glyph ships in * the rc.6 primitives icon set). */ function EyeIcon({ crossed }: { crossed: boolean }) { return ( ) } /** * Native-checkbox switch with the DSH settings-row visual treatment. */ function ToggleSwitch({ checked, onChange, disabled, label }: { checked: boolean onChange: (next: boolean) => void disabled: boolean label: string }) { return ( ) } /** Human-readable error copy for one settings-route failure. */ function messageOf(error: unknown): string { if (error instanceof FirecrawlSettingsApiError && error.code === 'settings-conflict') { return '设置已在其他窗口被修改,已重新载入;请再次保存。' } return error instanceof Error ? error.message : String(error) } /** * Render the Firecrawl settings page. * @param props - the runtime share of a `settings.section` entry. * @returns the section element tree. */ export function FirecrawlSettingsSection(_props: FirecrawlSettingsSectionProps) { const [view, setView] = useState(null) const [drafts, setDrafts] = useState(INITIAL_DRAFTS) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const [notice, setNotice] = useState(null) const [showApiKey, setShowApiKey] = useState(false) const load = useCallback(async () => { setLoading(true) setError(null) try { const next = await getFirecrawlSettings() setView(next) setDrafts(draftsOf(next)) } catch (caught) { setError(messageOf(caught)) } finally { setLoading(false) } }, []) useEffect(() => { let cancelled = false void getFirecrawlSettings().then((next) => { if (cancelled) return setView(next) setDrafts(draftsOf(next)) setLoading(false) }).catch((caught) => { if (cancelled) return setError(messageOf(caught)) setLoading(false) }) return () => { cancelled = true } }, []) const applyOps = async (ops: FirecrawlSettingsOp[]): Promise => { if (view === null || view.writable === false) return setSaving(true) setError(null) setNotice(null) try { const next = await mutateFirecrawlSettings(ops, view.revision) setView(next) setDrafts({ ...draftsOf(next), apiKey: '' }) setNotice('已保存,新配置立即用于下一次搜索。') } catch (caught) { setError(messageOf(caught)) // A conflict means another surface committed since this page read the // revision; adopt the fresh redacted view so the next save can proceed. if (caught instanceof FirecrawlSettingsApiError && caught.code === 'settings-conflict') { void getFirecrawlSettings().then((next) => { setView(next) setDrafts(draftsOf(next)) }).catch(() => {}) } } finally { setSaving(false) } } /** Persist the master switch immediately (the provider reads it live). */ const toggleUseFirecrawl = (next: boolean): void => { setDrafts({ ...drafts, useFirecrawl: next }) setError(null) setNotice(null) void applyOps([{ op: 'set', path: ['useFirecrawl'], value: next }]) } const save = (): void => { const baseURL = drafts.baseURL.trim() const limitRaw = drafts.limit.trim() const maxRaw = drafts.maxSnippetChars.trim() const limit = limitRaw === '' ? null : parsePositiveInteger(limitRaw) const maxSnippetChars = maxRaw === '' ? null : parsePositiveInteger(maxRaw) if (limitRaw !== '' && limit === null) { setError('limit 必须是正整数。') return } if (maxRaw !== '' && maxSnippetChars === null) { setError('maxSnippetChars 必须是正整数。') return } const ops: FirecrawlSettingsOp[] = [] ops.push({ op: 'set', path: ['useFirecrawl'], value: drafts.useFirecrawl }) const apiKey = drafts.apiKey.trim() if (apiKey !== '') ops.push({ op: 'set', path: ['apiKey'], value: apiKey }) if (baseURL !== '') ops.push({ op: 'set', path: ['baseURL'], value: baseURL }) else ops.push({ op: 'unset', path: ['baseURL'] }) if (limit === null) ops.push({ op: 'unset', path: ['limit'] }) else ops.push({ op: 'set', path: ['limit'], value: limit }) if (maxSnippetChars === null) ops.push({ op: 'unset', path: ['maxSnippetChars'] }) else ops.push({ op: 'set', path: ['maxSnippetChars'], value: maxSnippetChars }) if (ops.length === 0) return void applyOps(ops) } const clearKey = (): void => { void applyOps([{ op: 'unset', path: ['apiKey'] }]) } const configured = view !== null && isApiKeyConfigured(view) const disabled = view === null || view.writable === false || saving if (loading) { return

正在加载 Firecrawl 配置…

} if (view === null) { return (

Firecrawl 搜索提供方的 API 密钥与请求参数。

{error ?? '无法读取 Firecrawl 配置。'}

) } return (

Firecrawl 搜索提供方的 API 密钥与请求参数。保存后立即生效。

使用 Firecrawl 关闭后切换到原版 DeepSeek 网页搜索,立即生效。
写入设置文档的密钥不会回显;留空保存表示保持当前值,未配置时回退 $FIRECRAWL_API_KEY。
{ setDrafts({ ...drafts, apiKey: event.currentTarget.value }) }} /> )}
端点基址,搜索时追加 /v1/search。
{ setDrafts({ ...drafts, baseURL: event.currentTarget.value }) }} />
请求未携带 maxResults 时的默认结果数;留空表示不发送。
{ setDrafts({ ...drafts, limit: event.currentTarget.value }) }} />
单个 description 映射为 snippet 时保留的字符上限。
{ setDrafts({ ...drafts, maxSnippetChars: event.currentTarget.value }) }} />
{error !== null &&

{error}

} {notice !== null &&

{notice}

}
) }