-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat(llm): pluggable web search providers (Exa, Tavily, SearXNG) #9556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tgonzalezc5
wants to merge
1
commit into
TriliumNext:main
Choose a base branch
from
tgonzalezc5:feat/search-provider-registry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { useTriliumOption, useTriliumOptionBool } from "../../react/hooks"; | |
| import OptionsRow, { OptionsRowWithToggle } from "./components/OptionsRow"; | ||
| import OptionsSection from "./components/OptionsSection"; | ||
| import AddProviderModal, { type LlmProviderConfig, PROVIDER_TYPES } from "./llm/AddProviderModal"; | ||
| import AddSearchProviderModal, { type SearchProviderConfig, SEARCH_PROVIDER_TYPES } from "./llm/AddSearchProviderModal"; | ||
|
|
||
| export default function LlmSettings() { | ||
| if (!isExperimentalFeatureEnabled("llm")) { | ||
|
|
@@ -22,6 +23,7 @@ export default function LlmSettings() { | |
| return ( | ||
| <> | ||
| <ProviderSettings /> | ||
| <SearchProviderSettings /> | ||
| <McpSettings /> | ||
| </> | ||
| ); | ||
|
|
@@ -80,6 +82,102 @@ function ProviderSettings() { | |
| ); | ||
| } | ||
|
|
||
| function SearchProviderSettings() { | ||
| const [providersJson, setProvidersJson] = useTriliumOption("searchProviders"); | ||
| const providers = useMemo<SearchProviderConfig[]>(() => { | ||
| try { | ||
| return providersJson ? JSON.parse(providersJson) : []; | ||
| } catch { | ||
| return []; | ||
| } | ||
| }, [providersJson]); | ||
| const setProviders = useCallback((newProviders: SearchProviderConfig[]) => { | ||
| setProvidersJson(JSON.stringify(newProviders)); | ||
| }, [setProvidersJson]); | ||
| const [showAddModal, setShowAddModal] = useState(false); | ||
|
|
||
| const handleAddProvider = useCallback((newProvider: SearchProviderConfig) => { | ||
| setProviders([...providers, newProvider]); | ||
| }, [providers, setProviders]); | ||
|
|
||
| const handleDeleteProvider = useCallback(async (providerId: string, providerName: string) => { | ||
| if (!(await dialog.confirm(t("llm.delete_search_provider_confirmation", { name: providerName })))) { | ||
| return; | ||
| } | ||
| setProviders(providers.filter(p => p.id !== providerId)); | ||
| }, [providers, setProviders]); | ||
|
|
||
| return ( | ||
| <OptionsSection title={t("llm.search_provider_title")}> | ||
| <p className="form-text">{t("llm.search_provider_description")}</p> | ||
|
|
||
| <Button | ||
| size="small" | ||
| icon="bx bx-plus" | ||
| text={t("llm.add_search_provider")} | ||
| onClick={() => setShowAddModal(true)} | ||
| /> | ||
|
|
||
| <hr /> | ||
|
|
||
| <h5>{t("llm.configured_search_providers")}</h5> | ||
| <SearchProviderList | ||
| providers={providers} | ||
| onDelete={handleDeleteProvider} | ||
| /> | ||
|
|
||
| <AddSearchProviderModal | ||
| show={showAddModal} | ||
| onHidden={() => setShowAddModal(false)} | ||
| onSave={handleAddProvider} | ||
| /> | ||
| </OptionsSection> | ||
| ); | ||
| } | ||
|
|
||
| interface SearchProviderListProps { | ||
| providers: SearchProviderConfig[]; | ||
| onDelete: (providerId: string, providerName: string) => Promise<void>; | ||
| } | ||
|
|
||
| function SearchProviderList({ providers, onDelete }: SearchProviderListProps) { | ||
| if (!providers.length) { | ||
| return <div>{t("llm.no_search_providers_configured")}</div>; | ||
| } | ||
|
|
||
| return ( | ||
| <div style={{ overflow: "auto" }}> | ||
| <table className="table table-stripped"> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| <thead> | ||
| <tr> | ||
| <th>{t("llm.provider_name")}</th> | ||
| <th>{t("llm.provider_type")}</th> | ||
| <th>{t("llm.actions")}</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| {providers.map((provider) => { | ||
| const providerType = SEARCH_PROVIDER_TYPES.find(p => p.id === provider.provider); | ||
| return ( | ||
| <tr key={provider.id}> | ||
| <td>{provider.name}</td> | ||
| <td>{providerType?.name || provider.provider}</td> | ||
| <td> | ||
| <ActionButton | ||
| icon="bx bx-trash" | ||
| text={t("llm.delete_search_provider")} | ||
| onClick={() => onDelete(provider.id, provider.name)} | ||
| /> | ||
| </td> | ||
| </tr> | ||
| ); | ||
| })} | ||
| </tbody> | ||
| </table> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function getMcpEndpointUrl() { | ||
| const port = window.location.port || (window.location.protocol === "https:" ? "443" : "80"); | ||
| return `${window.location.protocol}//localhost:${port}/mcp`; | ||
|
|
||
155 changes: 155 additions & 0 deletions
155
apps/client/src/widgets/type_widgets/options/llm/AddSearchProviderModal.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import { createPortal } from "preact/compat"; | ||
| import { useRef, useState } from "preact/hooks"; | ||
|
|
||
| import { t } from "../../../../services/i18n"; | ||
| import FormGroup from "../../../react/FormGroup"; | ||
| import FormSelect from "../../../react/FormSelect"; | ||
| import FormTextBox from "../../../react/FormTextBox"; | ||
| import Modal from "../../../react/Modal"; | ||
|
|
||
| export interface SearchProviderConfig { | ||
| id: string; | ||
| name: string; | ||
| provider: string; | ||
| apiKey?: string; | ||
| baseUrl?: string; | ||
| } | ||
|
|
||
| export interface SearchProviderType { | ||
| id: string; | ||
| name: string; | ||
| /** Whether this provider requires an API key. */ | ||
| requiresApiKey: boolean; | ||
| /** Whether this provider requires a base URL (e.g. self-hosted). */ | ||
| requiresBaseUrl: boolean; | ||
| apiKeyPlaceholder?: string; | ||
| baseUrlPlaceholder?: string; | ||
| } | ||
|
|
||
| export const SEARCH_PROVIDER_TYPES: SearchProviderType[] = [ | ||
| { | ||
| id: "exa", | ||
| name: "Exa", | ||
| requiresApiKey: true, | ||
| requiresBaseUrl: false, | ||
| apiKeyPlaceholder: "..." | ||
| }, | ||
| { | ||
| id: "tavily", | ||
| name: "Tavily", | ||
| requiresApiKey: true, | ||
| requiresBaseUrl: false, | ||
| apiKeyPlaceholder: "tvly-..." | ||
| }, | ||
| { | ||
| id: "searxng", | ||
| name: "SearXNG", | ||
| requiresApiKey: false, | ||
| requiresBaseUrl: true, | ||
| baseUrlPlaceholder: "http://localhost:8888" | ||
| } | ||
| ]; | ||
|
|
||
| interface AddSearchProviderModalProps { | ||
| show: boolean; | ||
| onHidden: () => void; | ||
| onSave: (provider: SearchProviderConfig) => void; | ||
| } | ||
|
|
||
| export default function AddSearchProviderModal({ show, onHidden, onSave }: AddSearchProviderModalProps) { | ||
| const [selectedProvider, setSelectedProvider] = useState(SEARCH_PROVIDER_TYPES[0].id); | ||
| const [apiKey, setApiKey] = useState(""); | ||
| const [baseUrl, setBaseUrl] = useState(""); | ||
| const formRef = useRef<HTMLFormElement>(null); | ||
|
|
||
| const providerType = SEARCH_PROVIDER_TYPES.find(p => p.id === selectedProvider) ?? SEARCH_PROVIDER_TYPES[0]; | ||
| const canSubmit = | ||
| (!providerType.requiresApiKey || apiKey.trim().length > 0) && | ||
| (!providerType.requiresBaseUrl || baseUrl.trim().length > 0); | ||
|
|
||
| function handleSubmit() { | ||
| if (!canSubmit) { | ||
| return; | ||
| } | ||
|
|
||
| const newProvider: SearchProviderConfig = { | ||
| id: `${selectedProvider}_${Date.now()}`, | ||
| name: providerType.name, | ||
| provider: selectedProvider, | ||
| ...(providerType.requiresApiKey && { apiKey: apiKey.trim() }), | ||
| ...(providerType.requiresBaseUrl && { baseUrl: baseUrl.trim() }) | ||
| }; | ||
|
|
||
| onSave(newProvider); | ||
| resetForm(); | ||
| onHidden(); | ||
| } | ||
|
|
||
| function resetForm() { | ||
| setSelectedProvider(SEARCH_PROVIDER_TYPES[0].id); | ||
| setApiKey(""); | ||
| setBaseUrl(""); | ||
| } | ||
|
|
||
| function handleCancel() { | ||
| resetForm(); | ||
| onHidden(); | ||
| } | ||
|
|
||
| return createPortal( | ||
| <Modal | ||
| show={show} | ||
| onHidden={handleCancel} | ||
| onSubmit={handleSubmit} | ||
| formRef={formRef} | ||
| title={t("llm.add_search_provider_title")} | ||
| className="add-search-provider-modal" | ||
| size="md" | ||
| footer={ | ||
| <> | ||
| <button type="button" className="btn btn-secondary" onClick={handleCancel}> | ||
| {t("llm.cancel")} | ||
| </button> | ||
| <button type="submit" className="btn btn-primary" disabled={!canSubmit}> | ||
| {t("llm.add_search_provider")} | ||
| </button> | ||
| </> | ||
| } | ||
| > | ||
| <FormGroup name="search-provider-type" label={t("llm.search_provider_type")}> | ||
| <FormSelect | ||
| values={SEARCH_PROVIDER_TYPES} | ||
| keyProperty="id" | ||
| titleProperty="name" | ||
| currentValue={selectedProvider} | ||
| onChange={setSelectedProvider} | ||
| /> | ||
| </FormGroup> | ||
|
|
||
| {providerType.requiresApiKey && ( | ||
| <FormGroup name="search-api-key" label={t("llm.api_key")}> | ||
| <FormTextBox | ||
| type="password" | ||
| currentValue={apiKey} | ||
| onChange={setApiKey} | ||
| placeholder={providerType.apiKeyPlaceholder ?? t("llm.api_key_placeholder")} | ||
| autoFocus | ||
| /> | ||
| </FormGroup> | ||
| )} | ||
|
|
||
| {providerType.requiresBaseUrl && ( | ||
| <FormGroup name="search-base-url" label={t("llm.search_provider_base_url")}> | ||
| <FormTextBox | ||
| type="url" | ||
| currentValue={baseUrl} | ||
| onChange={setBaseUrl} | ||
| placeholder={providerType.baseUrlPlaceholder ?? ""} | ||
| autoFocus | ||
| /> | ||
| </FormGroup> | ||
| )} | ||
| </Modal>, | ||
| document.body | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
apps/server/src/services/search_providers/base_search_provider.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /** | ||
| * Shared interface and types for pluggable web search providers used by the LLM agent. | ||
| * | ||
| * Each search provider implementation wraps a third-party search API and returns a | ||
| * unified {@link SearchResult} array so the LLM tool layer can remain provider-agnostic. | ||
| */ | ||
|
|
||
| /** Normalised search result returned by all providers. */ | ||
| export interface SearchResult { | ||
| title: string; | ||
| url: string; | ||
| /** Short extract of the page (provider-chosen: highlights, summary or truncated body). */ | ||
| snippet: string; | ||
| publishedDate?: string; | ||
| author?: string; | ||
| } | ||
|
|
||
| /** Optional search parameters understood by all providers. Providers ignore unsupported fields. */ | ||
| export interface SearchOptions { | ||
| numResults?: number; | ||
| includeDomains?: string[]; | ||
| excludeDomains?: string[]; | ||
| /** ISO-8601 date, e.g. "2025-01-01T00:00:00.000Z" */ | ||
| startPublishedDate?: string; | ||
| /** ISO-8601 date */ | ||
| endPublishedDate?: string; | ||
| /** Provider-specific category hint (Exa: company, research paper, news, ...). */ | ||
| category?: string; | ||
| } | ||
|
|
||
| /** Implemented by every concrete search provider. */ | ||
| export interface SearchProvider { | ||
| /** Human-readable provider name shown to the LLM and in logs (e.g. "Exa", "Tavily"). */ | ||
| name: string; | ||
| search(query: string, options?: SearchOptions): Promise<SearchResult[]>; | ||
| } | ||
|
|
||
| /** | ||
| * User-supplied configuration for one search-provider instance, stored as JSON in the | ||
| * {@code searchProviders} option. Shape mirrors {@code LlmProviderSetup} so the same UI | ||
| * patterns (multiple named instances, optional API key and base URL) can be reused. | ||
| */ | ||
| export interface SearchProviderSetup { | ||
| id: string; | ||
| name: string; | ||
| /** Provider type id, e.g. "exa", "tavily", "searxng". */ | ||
| provider: string; | ||
| /** API key, required by providers like Exa and Tavily. */ | ||
| apiKey?: string; | ||
| /** Custom endpoint, required by providers like SearXNG. */ | ||
| baseUrl?: string; | ||
| } | ||
|
|
||
| export const DEFAULT_MAX_RESULTS = 5; | ||
| export const DEFAULT_TIMEOUT_MS = 15_000; | ||
|
|
||
| export abstract class BaseSearchProvider implements SearchProvider { | ||
| abstract name: string; | ||
| abstract search(query: string, options?: SearchOptions): Promise<SearchResult[]>; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the server-side registry, the parsed JSON should be validated as an array. If the option contains a non-array value, the
useMemohook will return a value that causesproviders.filterorproviders.mapto crash the UI.