TagSpaces is an offline, open source, document manager with tagging support
feat: added support for connecting lmstudio, lamma.cpp and other openai compatible services as ai providers in tagspaces
Ilian Sapundshievdea2af8beafe
diff --git a/scripts/extconfig-ai-mock-openai.js b/scripts/extconfig-ai-mock-openai.jsnew file mode 100644index 000000000..76c46fe4d--- /dev/null+++ b/scripts/extconfig-ai-mock-openai.js@@ -0,0 +1,25 @@+window.ExtIsFirstRun = false;+window.ExtCheckForUpdatesOnStartup = false;+window.ExtUseGenerateThumbnails = true;+// A single enabled OpenAI-compatible provider (LM Studio / llama.cpp / vLLM+// shape) pointing at a host the e2e suite intercepts via Playwright route()+// (see armOpenAIMock in tests/e2e/ai.helpers.js). No real service is+// contacted. The renderer talks OpenAI HTTP: GET {url}/models for the model+// list, POST {url}/chat/completions for generation. `url` already carries the+// /v1 base, so the client appends only the endpoint suffix.+// The location is created by the spec via createPwLocation(), so no+// ExtLocations here. The model ids MUST match those armOpenAIMock returns.+window.ExtAI = {+ defaultEngine: 'e2eMockOpenAIEngine',+ engines: [+ {+ id: 'e2eMockOpenAIEngine',+ engine: 'openai-compatible',+ name: 'E2E Mock LM Studio',+ url: 'http://127.0.0.1:1234/v1',+ defaultTextModel: 'mock-text-model',+ defaultImageModel: 'mock-vision-model',+ enable: true,+ },+ ],+};diff --git a/scripts/extconfig-ai-ui.js b/scripts/extconfig-ai-ui.jsnew file mode 100644index 000000000..5cfc23d20--- /dev/null+++ b/scripts/extconfig-ai-ui.js@@ -0,0 +1,8 @@+window.ExtIsFirstRun = false;+window.ExtCheckForUpdatesOnStartup = false;+window.ExtUseGenerateThumbnails = true;+// Intentionally NO window.ExtAI: with ExtAI defined the Settings → AI provider+// editing UI is disabled (externalConfig), which would block adding a provider.+// Leaving it undefined makes getDefaultAIProvider() read redux (empty on a+// freshly-cleared store), so the add-engine menu and preset flow are enabled+// and deterministic. The location is created by the spec via createPwLocation().diff --git a/src/renderer/components/chat/AiClient.ts b/src/renderer/components/chat/AiClient.tsnew file mode 100644index 000000000..728184568--- /dev/null+++ b/src/renderer/components/chat/AiClient.ts@@ -0,0 +1,119 @@+/**+ * TagSpaces - universal file and folder organizer+ * Copyright (C) 2024-present TagSpaces GmbH+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License (version 3) as+ * published by the Free Software Foundation.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ */++/**+ * Engine-agnostic AI client used by ChatProvider. It hides the difference+ * between the Ollama-native SDK (`engine: 'ollama'`) and any OpenAI-compatible+ * server (`engine: 'openai-compatible'` — LM Studio, llama.cpp, vLLM, …).+ *+ * `pull`/`delete` are Ollama-only and therefore optional; OpenAI-compatible+ * servers manage their models externally, so those members are undefined and+ * the UI hides the corresponding controls.+ */+import { AIProvider } from '-/components/chat/ChatTypes';+import {+ deleteOllamaModel,+ getOllamaModels,+ newOllamaMessage,+ pullOllamaModel,+} from '-/components/chat/OllamaClient';+import {+ getOpenAIModels,+ newOpenAIMessage,+} from '-/components/chat/OpenAIClient';+import { ChatRequest, ModelResponse, Ollama } from 'ollama';++export interface AiClient {+ engine: AIProvider['engine'];+ list: () => Promise<ModelResponse[]>;+ chat: (+ msg: ChatRequest,+ chatMessageHandler?: (msgContent: string) => void,+ ) => Promise<string | undefined>;+ abort: () => void;+ /** Ollama-only: download a model with progress. Undefined elsewhere. */+ pull?: (model: string, progress: (part: any) => void) => Promise<boolean>;+ /** Ollama-only: delete an installed model. Undefined elsewhere. */+ delete?: (model: string) => Promise<string>;+}++async function getOllamaInstance(url: string): Promise<Ollama> {+ if (url) {+ try {+ //@ts-ignore - browser build has no bundled types entry+ const { Ollama } = await import('ollama/browser');+ return new Ollama({ host: url });+ } catch (error) {+ console.error('Failed to load Ollama module:', error);+ }+ }+ return undefined;+}++/**+ * Build the right AiClient for a provider. Returns undefined only when the+ * Ollama SDK fails to load; OpenAI-compatible clients are always constructable.+ */+export async function getAiClient(provider: AIProvider): Promise<AiClient> {+ if (!provider || !provider.url) {+ return undefined;+ }+ if (provider.engine === 'openai-compatible') {+ let controller: AbortController | undefined;+ return {+ engine: provider.engine,+ list: () => getOpenAIModels(provider.url, provider.authKey),+ chat: (msg, handler) => {+ controller = new AbortController();+ return newOpenAIMessage(+ provider.url,+ msg,+ handler,+ provider.authKey,+ controller.signal,+ );+ },+ abort: () => {+ if (controller) {+ controller.abort();+ }+ },+ };+ }+ // default: Ollama+ const ollama = await getOllamaInstance(provider.url);+ if (!ollama) {+ return undefined;+ }+ return {+ engine: 'ollama',+ list: () => getOllamaModels(ollama),+ chat: (msg, handler) => newOllamaMessage(ollama, msg, handler),+ abort: () => ollama.abort(),+ pull: (model, progress) => pullOllamaModel(ollama, model, progress),+ delete: (model) => deleteOllamaModel(ollama, model),+ };+}++/** Probe whether a provider's endpoint is reachable (models list succeeds). */+export function checkProviderAlive(provider: AIProvider): Promise<boolean> {+ return getAiClient(provider)+ .then((client) => (client ? client.list() : undefined))+ .then((m) => !!m)+ .catch(() => false);+}diff --git a/src/renderer/components/chat/ChatTypes.ts b/src/renderer/components/chat/ChatTypes.tsindex 0d6e234a9..3d4bbbabf 100644--- a/src/renderer/components/chat/ChatTypes.ts+++ b/src/renderer/components/chat/ChatTypes.ts@@ -16,7 +16,11 @@**/-export type AIProviders = 'ollama' | 'node-lama' | 'chatgpt';+export type AIProviders =+ | 'ollama'+ | 'openai-compatible'+ | 'node-lama'+ | 'chatgpt';export type ChatRole = 'user' | 'system' | 'assistant' | 'tool';export type ChatMode =| 'summary'@@ -32,6 +36,7 @@ export type AIProvider = {enable: boolean;url: string;//alive?: boolean;+ authKey?: string; // optional bearer token for OpenAI-compatible/cloud endpointsdefaultImageModel?: string;defaultTextModel?: string;};diff --git a/src/renderer/components/chat/OpenAIClient.ts b/src/renderer/components/chat/OpenAIClient.tsnew file mode 100644index 000000000..c9937cac7--- /dev/null+++ b/src/renderer/components/chat/OpenAIClient.ts@@ -0,0 +1,227 @@+/**+ * TagSpaces - universal file and folder organizer+ * Copyright (C) 2024-present TagSpaces GmbH+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License (version 3) as+ * published by the Free Software Foundation.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ */++/**+ * Generic client for any OpenAI-compatible local/remote LLM server+ * (LM Studio, llama.cpp's llama-server, vLLM, Ollama's /v1 endpoint, …).+ *+ * It speaks only `GET /v1/models` and `POST /v1/chat/completions`, so it has+ * no notion of pulling/deleting models — those are Ollama-native and handled+ * by OllamaClient. The function surface mirrors OllamaClient so ChatProvider+ * can dispatch to either by engine.+ */+import { ChatRequest, ModelResponse } from 'ollama';++/**+ * Resolve the OpenAI base URL. The configured URL is treated as the base that+ * precedes `/v1` (the standard OpenAI `baseURL` convention), so both styles work:+ * http://localhost:1234 → http://localhost:1234/v1+ * http://localhost:1234/v1 → http://localhost:1234/v1 (no double /v1)+ * https://openrouter.ai/api/v1 → https://openrouter.ai/api/v1+ * Callers append only the endpoint suffix (`/chat/completions`, `/models`).+ */+function apiBase(url: string): string {+ if (!url) {+ return url;+ }+ // drop trailing slashes, then a trailing /v1 so we can re-add it uniformly+ const base = url.replace(/\/+$/, '').replace(/\/v1$/, '');+ return `${base}/v1`;+}++/**+ * @param withBody set Content-Type for requests that carry a JSON body (POST).+ * Omit it on GET so a no-auth request stays CORS-"simple" (no preflight).+ */+function buildHeaders(+ authKey?: string,+ withBody = false,+): Record<string, string> {+ const headers: Record<string, string> = {};+ if (withBody) {+ headers['Content-Type'] = 'application/json';+ }+ if (authKey) {+ headers.Authorization = `Bearer ${authKey}`;+ }+ return headers;+}++/**+ * List models via `GET /v1/models`. The OpenAI shape is `{ data: [{ id }] }`;+ * we map it onto the Ollama `ModelResponse` shape the UI already consumes+ * (only `name` is meaningful — size/dates are unknown for these servers).+ */+export async function getOpenAIModels(+ url: string,+ authKey?: string,+): Promise<ModelResponse[]> {+ if (!url) {+ return undefined;+ }+ try {+ const response = await fetch(`${apiBase(url)}/models`, {+ method: 'GET',+ headers: buildHeaders(authKey),+ });+ if (!response.ok) {+ return undefined;+ }+ const json = await response.json();+ const data = Array.isArray(json?.data) ? json.data : [];+ return data+ .filter((m) => m && m.id)+ .map(+ (m) =>+ ({+ name: m.id,+ model: m.id,+ modified_at: undefined,+ size: 0,+ digest: '',+ details: {+ family: 'openai-compatible',+ format: '',+ families: [],+ parameter_size: '',+ quantization_level: '',+ },+ }) as unknown as ModelResponse,+ );+ } catch (e) {+ console.log('getOpenAIModels', e);+ return undefined;+ }+}++/**+ * Translate the internal (Ollama-native) ChatRequest into an OpenAI+ * chat-completions body:+ * - `messages[].images: [base64]` → `content: [{type:'text'}, {type:'image_url', ...}]`+ * - Ollama `format: <jsonSchema>` → `response_format: { type:'json_schema', json_schema }`+ * - `keep_alive` is dropped (no OpenAI equivalent)+ */+function toOpenAIRequest(msg: ChatRequest): Record<string, any> {+ const messages = (msg.messages || []).map((m: any) => {+ if (Array.isArray(m.images) && m.images.length > 0) {+ return {+ role: m.role,+ content: [+ ...(m.content ? [{ type: 'text', text: m.content }] : []),+ ...m.images.map((base64: string) => ({+ type: 'image_url',+ image_url: { url: 'data:image/jpeg;base64,' + base64 },+ })),+ ],+ };+ }+ return { role: m.role, content: m.content };+ });++ const body: Record<string, any> = {+ model: msg.model,+ messages,+ stream: !!msg.stream,+ };++ // Ollama puts the raw JSON schema in `format`; OpenAI wants response_format.+ const { format } = msg as any;+ if (format && typeof format === 'object') {+ body.response_format = {+ type: 'json_schema',+ json_schema: { name: 'result', schema: format },+ };+ }+ return body;+}++/**+ * Send a chat message via `POST /v1/chat/completions`. When `msg.stream` is+ * true the response is parsed as SSE and `chatMessageHandler` receives each+ * delta; otherwise the full assistant content is returned.+ */+export async function newOpenAIMessage(+ url: string,+ msg: ChatRequest,+ chatMessageHandler?: (msgContent: string) => void,+ authKey?: string,+ signal?: AbortSignal,+): Promise<string | undefined> {+ if (!url) {+ return undefined;+ }+ const body = toOpenAIRequest(msg);+ try {+ const response = await fetch(`${apiBase(url)}/chat/completions`, {+ method: 'POST',+ headers: buildHeaders(authKey, true),+ body: JSON.stringify(body),+ signal,+ });+ if (!response.ok) {+ console.error('newOpenAIMessage HTTP ' + response.status);+ return undefined;+ }++ if (msg.stream && response.body) {+ const reader = response.body.getReader();+ const decoder = new TextDecoder();+ let buffer = '';+ // eslint-disable-next-line no-constant-condition+ while (true) {+ const { done, value } = await reader.read();+ if (done) {+ break;+ }+ buffer += decoder.decode(value, { stream: true });+ const lines = buffer.split('\n');+ // keep the last (possibly partial) line in the buffer+ buffer = lines.pop() || '';+ for (const line of lines) {+ const trimmed = line.trim();+ if (!trimmed.startsWith('data:')) {+ continue;+ }+ const data = trimmed.replace(/^data:\s*/, '');+ if (data === '[DONE]') {+ continue;+ }+ try {+ const json = JSON.parse(data);+ const delta = json?.choices?.[0]?.delta?.content;+ if (delta && chatMessageHandler) {+ chatMessageHandler(delta);+ }+ } catch (e) {+ // ignore keep-alive / non-JSON lines+ }+ }+ }+ return undefined;+ }++ const json = await response.json();+ return json?.choices?.[0]?.message?.content;+ } catch (e) {+ if ((e as any)?.name === 'AbortError') {+ return undefined;+ }+ console.error('newOpenAIMessage error', e);+ return undefined;+ }+}diff --git a/src/renderer/components/chat/SelectChatModel.tsx b/src/renderer/components/chat/SelectChatModel.tsxindex ea3b4142f..cf140d3a8 100644--- a/src/renderer/components/chat/SelectChatModel.tsx+++ b/src/renderer/components/chat/SelectChatModel.tsx@@ -16,11 +16,15 @@**/-import { DownloadIcon, OllamaIcon, RemoveIcon } from '-/components/CommonIcons';+import {+ AIIcon,+ DownloadIcon,+ OllamaIcon,+ RemoveIcon,+} from '-/components/CommonIcons';import TsIconButton from '-/components/TsIconButton';import TsSelect from '-/components/TsSelect';import { AIProvider } from '-/components/chat/ChatTypes';-import { getOllamaModels } from '-/components/chat/OllamaClient';import { useChatContext } from '-/hooks/useChatContext';import { useNotificationContext } from '-/hooks/useNotificationContext';import { getDefaultAIProvider } from '-/reducers/settings';@@ -50,7 +54,7 @@ function SelectChatModel(props: Props) {const { t } = useTranslation();const { id, label, aiProvider, chosenModel, handleChangeModel, disabled } =props;- const { removeModel, getOllamaClient, models } = useChatContext();+ const { removeModel, getAiClient, models } = useChatContext();const { openConfirmDialog } = useNotificationContext();const defaultAiProvider: AIProvider = useSelector(getDefaultAIProvider);@@ -58,10 +62,17 @@ function SelectChatModel(props: Props) {aiProvider?.id === defaultAiProvider?.id ? models : [],);+ // Ollama supports in-app model download/delete; OpenAI-compatible servers+ // (LM Studio, llama.cpp, …) manage models externally, so those controls hide.+ const isOllama = aiProvider?.engine === 'ollama';+useEffect(() => {- if (aiProvider && aiProvider.engine === 'ollama') {- getOllamaClient(aiProvider.url).then((client) => {- getOllamaModels(client).then((m) => {+ if (aiProvider) {+ getAiClient(aiProvider).then((client) => {+ if (!client) {+ return;+ }+ client.list().then((m) => {if (!m || JSON.stringify(m) !== JSON.stringify(installedModels)) {setModels(m ? m : []);}@@ -190,7 +201,10 @@ function SelectChatModel(props: Props) {removeModel(chosenModel);};function getTitle(model) {- return model ? format(parseISO(model.modified_at), 'yyyy-MM-dd') : '';+ // OpenAI-compatible servers don't report a modified date.+ return model && model.modified_at+ ? format(parseISO(model.modified_at), 'yyyy-MM-dd')+ : '';}return (@@ -203,7 +217,7 @@ function SelectChatModel(props: Props) {id={id ? id : 'selectChatModelId'}slotProps={{input: {- endAdornment: chosenModel && (+ endAdornment: chosenModel && isOllama && (<InputAdornmentposition="end"sx={{ marginLeft: '-30px', marginRight: '10px' }}@@ -234,9 +248,23 @@ function SelectChatModel(props: Props) {paddingLeft: '3px',}}>- <OllamaIcon- sx={{ width: '24px', height: '24px', verticalAlign: 'middle' }}- />+ {isOllama ? (+ <OllamaIcon+ sx={{+ width: '24px',+ height: '24px',+ verticalAlign: 'middle',+ }}+ />+ ) : (+ <AIIcon+ sx={{+ width: '24px',+ height: '24px',+ verticalAlign: 'middle',+ }}+ />+ )}</ListItemIcon><ListItemTextsx={{@@ -244,7 +272,9 @@ function SelectChatModel(props: Props) {alignItems: 'center',}}>- {model.name} {(model.size / (1024 * 1024 * 1024)).toFixed(2)} GB+ {model.name}+ {model.size > 0 &&+ ' ' + (model.size / (1024 * 1024 * 1024)).toFixed(2) + ' GB'}</ListItemText></MenuItem>))@@ -253,26 +283,31 @@ function SelectChatModel(props: Props) {{t('core:noAIModelsInstaller')}</MenuItem>)}- <ListSubheader>{t('core:exampleInstallableModels')}</ListSubheader>- {ollamaAvailableModels.map((model) => (- <MenuItem- key={model.name}- value={model.name}- title={model.details.format}- >+ {isOllama && (+ <ListSubheader>{t('core:exampleInstallableModels')}</ListSubheader>+ )}+ {isOllama &&+ ollamaAvailableModels.map((model) => (+ <MenuItem+ key={model.name}+ value={model.name}+ title={model.details.format}+ >+ <ListItemIcon>+ <DownloadIcon />+ </ListItemIcon>+ {model.name}+ </MenuItem>+ ))}+ {isOllama && <ListSubheader>{t('core:moreActions')}</ListSubheader>}+ {isOllama && (+ <MenuItem value="customModel"><ListItemIcon><DownloadIcon /></ListItemIcon>- {model.name}+ {t('core:installCustomModel')}</MenuItem>- ))}- <ListSubheader>{t('core:moreActions')}</ListSubheader>- <MenuItem value="customModel">- <ListItemIcon>- <DownloadIcon />- </ListItemIcon>- {t('core:installCustomModel')}- </MenuItem>+ )}</TsSelect>);}diff --git a/src/renderer/components/chat/aiPresets.ts b/src/renderer/components/chat/aiPresets.tsnew file mode 100644index 000000000..0cd2b4bf1--- /dev/null+++ b/src/renderer/components/chat/aiPresets.ts@@ -0,0 +1,77 @@+/**+ * TagSpaces - universal file and folder organizer+ * Copyright (C) 2024-present TagSpaces GmbH+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License (version 3) as+ * published by the Free Software Foundation.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ */++/**+ * Convenience presets shown in the "add AI engine" menu. A preset is pure+ * metadata over an engine type — it pre-fills name/URL/icon/setup-link but the+ * created provider only carries the generic `engine`. Adding a new+ * OpenAI-compatible backend (vLLM, Jan, …) means adding a row here, not a new+ * engine type or any client code.+ */+import { AIProviders } from '-/components/chat/ChatTypes';++export type AiPresetIcon = 'ollama' | 'ai';++export type AiPreset = {+ key: string;+ label: string;+ engine: AIProviders;+ defaultUrl: string;+ icon: AiPresetIcon;+ setupUrl?: string;+};++export const aiPresets: AiPreset[] = [+ {+ key: 'ollama',+ label: 'Ollama',+ engine: 'ollama',+ defaultUrl: 'http://localhost:11434',+ icon: 'ollama',+ setupUrl: 'https://ollama.com/download',+ },+ {+ key: 'lmstudio',+ label: 'LM Studio',+ engine: 'openai-compatible',+ // OpenAI base URL convention; the client tolerates a bare host too+ defaultUrl: 'http://localhost:1234/v1',+ icon: 'ai',+ setupUrl: 'https://lmstudio.ai',+ },+ {+ key: 'llamacpp',+ label: 'llama.cpp',+ engine: 'openai-compatible',+ defaultUrl: 'http://localhost:8080/v1',+ icon: 'ai',+ setupUrl: 'https://github.com/ggml-org/llama.cpp',+ },+ {+ key: 'openai-compatible',+ label: 'OpenAI-compatible',+ engine: 'openai-compatible',+ defaultUrl: '',+ icon: 'ai',+ },+];++/** Resolve the icon kind for an existing provider by its engine. */+export function presetIconForEngine(engine: AIProviders): AiPresetIcon {+ return engine === 'ollama' ? 'ollama' : 'ai';+}diff --git a/src/renderer/components/dialogs/components/SettingsAI.tsx b/src/renderer/components/dialogs/components/SettingsAI.tsxindex 261ccb99f..da77e8efc 100644--- a/src/renderer/components/dialogs/components/SettingsAI.tsx+++ b/src/renderer/components/dialogs/components/SettingsAI.tsx@@ -18,6 +18,7 @@import AppConfig from '-/AppConfig';import {+ AIIcon,CreateFileIcon,ExpandIcon,OllamaIcon,@@ -31,7 +32,12 @@ import TsMenuList from '-/components/TsMenuList';import TsSelect from '-/components/TsSelect';import TsSwitch from '-/components/TsSwitch';import TsTextField from '-/components/TsTextField';-import { AIProvider, AIProviders } from '-/components/chat/ChatTypes';+import { AIProvider } from '-/components/chat/ChatTypes';+import {+ AiPreset,+ aiPresets,+ presetIconForEngine,+} from '-/components/chat/aiPresets';import SelectChatModel from '-/components/chat/SelectChatModel';import { useChatContext } from '-/hooks/useChatContext';import { Pro } from '-/pro';@@ -111,9 +117,17 @@ function SettingsAI(props: Props) {setOpenedNewAIMenu(false);};+ function engineIcon(engine: AIProvider['engine'], sx?: any) {+ return presetIconForEngine(engine) === 'ollama' ? (+ <OllamaIcon sx={sx} />+ ) : (+ <AIIcon sx={sx} />+ );+ }+function checkOllamaAlive() {aiProviders.map((provider) =>- checkProviderAlive(provider.url).then((alive) => {+ checkProviderAlive(provider).then((alive) => {providersAlive.current = {...providersAlive.current,[provider.id]: alive,@@ -143,23 +157,20 @@ function SettingsAI(props: Props) {dispatch(SettingsActions.setAiProvider(providerId));};- const addAiProvider = (provider: AIProviders) => {- //event: ChangeEvent<HTMLInputElement>) => {- //const provider: AIProviders = event.target.value as AIProviders;- const providerUrl = provider === 'ollama' ? 'http://localhost:11434' : '';- checkProviderAlive(providerUrl).then((isAlive) => {- const providerId = getUuid();+ const addAiProvider = (preset: AiPreset) => {+ const providerId = getUuid();+ const aiProvider: AIProvider = {+ id: providerId,+ engine: preset.engine,+ name: preset.label,+ url: preset.defaultUrl,+ enable: true,+ };+ checkProviderAlive(aiProvider).then((isAlive) => {providersAlive.current = {...providersAlive.current,[providerId]: isAlive,};- const aiProvider: AIProvider = {- id: providerId,- engine: provider,- name: provider,- url: providerUrl,- enable: true,- };dispatch(SettingsActions.addAiProvider(aiProvider));});};@@ -309,19 +320,25 @@ function SettingsAI(props: Props) {><Paper><TsMenuList id="split-button-menu" autoFocusItem>- <MenuItem- key="createNewTextFileTID"- data-tid="aiCreateNewTextFileTID"- onClick={() => {- addAiProvider('ollama');- setOpenedNewAIMenu(false);- }}- >- <ListItemIcon>- <OllamaIcon />- </ListItemIcon>- <ListItemText primary="Ollama" />- </MenuItem>+ {aiPresets.map((preset) => (+ <MenuItem+ key={preset.key}+ data-tid={'aiAddProvider_' + preset.key + 'TID'}+ onClick={() => {+ addAiProvider(preset);+ setOpenedNewAIMenu(false);+ }}+ >+ <ListItemIcon>+ {preset.icon === 'ollama' ? (+ <OllamaIcon />+ ) : (+ <AIIcon />+ )}+ </ListItemIcon>+ <ListItemText primary={preset.label} />+ </MenuItem>+ ))}</TsMenuList></Paper></Grow>@@ -340,7 +357,7 @@ function SettingsAI(props: Props) {.filter((p) => p.enable).map((provider) => (<MenuItem key={provider.id} value={provider.id}>- <OllamaIcon />+ {engineIcon(provider.engine)}<Box sx={{ display: 'inline-block', marginLeft: '5px' }}>{provider.name}</Box>@@ -369,7 +386,7 @@ function SettingsAI(props: Props) {'& .MuiAccordionSummary-content': { alignItems: 'center' },}}>- <OllamaIcon />+ {engineIcon(provider.engine)}<Typography sx={{ marginLeft: '5px' }}>{provider.name}</Typography><TsTooltiptitle={diff --git a/src/renderer/hooks/ChatProvider.tsx b/src/renderer/hooks/ChatProvider.tsxindex 2fb9ac7d3..d4ee0c8f3 100644--- a/src/renderer/hooks/ChatProvider.tsx+++ b/src/renderer/hooks/ChatProvider.tsx@@ -27,11 +27,10 @@ import {PullModelResponse,} from '-/components/chat/ChatTypes';import {- deleteOllamaModel,- getOllamaModels,- newOllamaMessage,- pullOllamaModel,-} from '-/components/chat/OllamaClient';+ AiClient,+ checkProviderAlive as checkAiProviderAlive,+ getAiClient,+} from '-/components/chat/AiClient';import { generateOptionType } from '-/components/dialogs/hooks/AiGenerationDialogContextProvider';import { useFileUploadDialogContext } from '-/components/dialogs/hooks/useFileUploadDialogContext';import { TabNames } from '-/hooks/EntryPropsTabsContextProvider';@@ -73,7 +72,7 @@ import {loadJSONString,} from '@tagspaces/tagspaces-common/utils-io';import { format } from 'date-fns';-import { ChatRequest, ModelResponse, Ollama } from 'ollama';+import { ChatRequest, ModelResponse } from 'ollama';import React, {createContext,useContext,@@ -131,8 +130,8 @@ type ChatData = {) => Promise<string>;initHistory: () => void;deleteHistory: () => Promise<boolean>;- checkProviderAlive: (providerUrl: string) => Promise<boolean>;- getOllamaClient: (ollamaApiUrl: string) => Promise<Ollama>;+ checkProviderAlive: (provider: AIProvider) => Promise<boolean>;+ getAiClient: (provider: AIProvider) => Promise<AiClient>;getEntryModel: (entryName: string, aiProvider: AIProvider) => ModelResponse;tagsGenerate: (entry: TS.FileSystemEntry,@@ -172,7 +171,7 @@ export const ChatContext = createContext<ChatData>({initHistory: undefined,deleteHistory: undefined,checkProviderAlive: undefined,- getOllamaClient: undefined,+ getAiClient: undefined,getEntryModel: undefined,tagsGenerate: undefined,descriptionGenerate: undefined,@@ -226,7 +225,8 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {): undefined;const isTyping = useRef<boolean>(false);- const ollamaClient = useRef<Ollama>(undefined);+ const aiClient = useRef<AiClient>(undefined);+ const lastProviderId = useRef<string>(defaultAiProvider?.id);const dispatch: AppDispatch = useDispatch();const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);const currentLocation = findLocation();@@ -234,6 +234,14 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {useEffect(() => {images.current = [];+ // When the active provider changes (e.g. Ollama → LM Studio), drop the+ // cached client + model list so they are rebuilt for the new engine/url.+ if (lastProviderId.current !== defaultAiProvider?.id) {+ lastProviderId.current = defaultAiProvider?.id;+ aiClient.current = undefined;+ models.current = [];+ currentModel.current = undefined;+ }if (!firstRender &&defaultAiProvider &&@@ -266,20 +274,6 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {};}- async function getOllamaClient(ollamaApiUrl: string) {- if (ollamaApiUrl) {- try {- //@ts-ignore- const { Ollama } = await import('ollama/browser');-- return new Ollama({ host: ollamaApiUrl });- } catch (error) {- console.error('Failed to load Ollama module:', error);- }- }- return undefined;- }-function initHistory() {if (openedEntry) {if (@@ -356,12 +350,8 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {return Promise.resolve([]);}- function checkProviderAlive(providerUrl: string): Promise<boolean> {- return getOllamaClient(providerUrl).then((client) =>- getOllamaModels(client).then((m) => {- return !!m;- }),- );+ function checkProviderAlive(provider: AIProvider): Promise<boolean> {+ return checkAiProviderAlive(provider);}/**@@ -373,17 +363,24 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {return Promise.resolve(false);}if (!models.current || models.current.length === 0) {- return getOllamaClient(defaultAiProvider.url).then((client) => {- ollamaClient.current = client;+ return getAiClient(defaultAiProvider).then((client) => {+ aiClient.current = client;return refreshOllamaModels();});}return Promise.resolve(true);}- function refreshOllamaModels(modelName = undefined): Promise<boolean> {+ async function refreshOllamaModels(modelName = undefined): Promise<boolean> {if (defaultAiProvider) {- return getOllamaModels(ollamaClient.current)+ if (!aiClient.current) {+ aiClient.current = await getAiClient(defaultAiProvider);+ }+ if (!aiClient.current) {+ return false;+ }+ return aiClient.current+ .list().then((m) => {if (m) {models.current = m;@@ -419,6 +416,11 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {) {currentModel.current = model;forceUpdate();+ // Preloading a model with an empty-messages request is an Ollama warm-up+ // trick; OpenAI-compatible servers would reject it, so skip it there.+ if (defaultAiProvider?.engine !== 'ollama') {+ return Promise.resolve(true);+ }//load modelreturn newChatMessage().then(() => {showNotification(@@ -474,6 +476,12 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {function unloadCurrentModel() {if (currentModel.current) {+ // Unloading via keep_alive:0 is Ollama-only; for other engines just drop+ // the local reference without an HTTP round-trip.+ if (defaultAiProvider?.engine !== 'ollama') {+ currentModel.current = undefined;+ return;+ }//unload modelnewChatMessage(undefined, true).then(() => (currentModel.current = undefined),@@ -483,14 +491,16 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {function removeModel(modelName: string) {const model = findModel(modelName);- if (model && defaultAiProvider) {+ // Model deletion is Ollama-only; OpenAI-compatible servers manage models+ // externally and expose no delete endpoint.+ if (model && defaultAiProvider && aiClient.current?.delete) {const result = confirm(t('core:confirmRemoveModel', { name: model.name }),);if (result) {//addTimeLineRequest('deleting ' + model.name, 'system');showNotification(t('core:modelDeletedSuccess', { name: model.name }));- deleteOllamaModel(ollamaClient.current, model.name).then((response) => {+ aiClient.current.delete(model.name).then((response) => {console.log('deleteOllamaModel response:' + response);if (response) {if (model.name === currentModel.current?.name) {@@ -513,11 +523,17 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {* check if model is installed and return details* @param modelName*/- function getModel(modelName: string): Promise<ModelResponse> {+ async function getModel(modelName: string): Promise<ModelResponse> {if (!modelName || !defaultAiProvider) {- return Promise.resolve(undefined);+ return undefined;+ }+ if (!aiClient.current) {+ aiClient.current = await getAiClient(defaultAiProvider);+ }+ if (!aiClient.current) {+ return undefined;}- return getOllamaModels(ollamaClient.current).then((m) => {+ return aiClient.current.list().then((m) => {if (m && m.length > 0) {return m.find((mm) => mm.name === modelName || mm.name === modelName + ':latest',@@ -532,10 +548,10 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {confirmCallback?,): Promise<boolean> {let model;- if (!ollamaClient.current) {- const client = await getOllamaClient(defaultAiProvider.url);- ollamaClient.current = client;- const m = await getOllamaModels(client);+ if (!aiClient.current) {+ const client = await getAiClient(defaultAiProvider);+ aiClient.current = client;+ const m = client ? await client.list() : undefined;if (m) {models.current = m;model = models.current.find(@@ -545,7 +561,9 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {} else {model = findModel(newModelName);}- if (!model && defaultAiProvider) {+ // Downloading a model on demand is Ollama-only; other engines manage+ // their models externally, so just bail out without a pull prompt.+ if (!model && defaultAiProvider && aiClient.current?.pull) {// return setModel(model);// } else {const result = confirm(@@ -572,14 +590,12 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {);}};- return pullOllamaModel(- ollamaClient.current,- newModelName,- onProgressHandler,- ).then((response) => {- console.log('pullOllamaModel response:' + response);- return refreshOllamaModels(newModelName);- });+ return aiClient.current+ .pull(newModelName, onProgressHandler)+ .then((response) => {+ console.log('pullOllamaModel response:' + response);+ return refreshOllamaModels(newModelName);+ });/*return window.electronIO.ipcRenderer.invoke('pullOllamaModel', defaultAiProvider.url, {name: newModelName,@@ -598,7 +614,7 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {function addHistoryItem(txt: string, role: ChatRole) {if (txt) {const newItem: ChatItem = {- engine: 'ollama',+ engine: defaultAiProvider?.engine || 'ollama',modelName: currentModel.current?.name,role: role,request: txt,@@ -923,126 +939,130 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {...(format && format),};// console.log('AI question: ' + JSON.stringify(messages));- return newOllamaMessage(- ollamaClient.current,- request,- chatMessageHandler,- ).then((apiResponse) => {- // console.log('apiResponse:' + apiResponse);+ if (!aiClient.current) {+ showNotification(t('core:ollamaServiceNotAlive'));isTyping.current = false;forceUpdate();- if (apiResponse === undefined && stream === false) {- showNotification(t('core:ollamaServiceNotAlive'));- return undefined;- }- if (msg && includeHistory) {- saveHistoryItems();- }- if (mode === 'tags') {- if (imgArray.length > 0) {- const response = JSON.parse(apiResponse); // ImageDescription.parse(JSON.parse(apiResponse));- const tags = response.objects- ? response.objects.map((obj) => obj.name)- : [];- if (response.time_of_day && response.time_of_day !== 'Unknown') {- tags.push(response.time_of_day);- }- if (response.setting && response.setting !== 'Unknown') {- tags.push(response.setting);- }- if (response.topics) {- tags.push(response.topics);- }- return [...tags, ...response.colors];- } else {- let tags: string[] = [];- try {- const response = JSON.parse(apiResponse);- tags = response.topics;- } catch (e) {- console.log('JSON.parse error for:' + apiResponse, e);- if (apiResponse) {- tags = apiResponse.split(' ');+ return Promise.resolve(undefined);+ }+ return aiClient.current+ .chat(request, chatMessageHandler)+ .then((apiResponse) => {+ // console.log('apiResponse:' + apiResponse);+ isTyping.current = false;+ forceUpdate();+ if (apiResponse === undefined && stream === false) {+ showNotification(t('core:ollamaServiceNotAlive'));+ return undefined;+ }+ if (msg && includeHistory) {+ saveHistoryItems();+ }+ if (mode === 'tags') {+ if (imgArray.length > 0) {+ const response = JSON.parse(apiResponse); // ImageDescription.parse(JSON.parse(apiResponse));+ const tags = response.objects+ ? response.objects.map((obj) => obj.name)+ : [];+ if (response.time_of_day && response.time_of_day !== 'Unknown') {+ tags.push(response.time_of_day);+ }+ if (response.setting && response.setting !== 'Unknown') {+ tags.push(response.setting);+ }+ if (response.topics) {+ tags.push(response.topics);+ }+ return [...tags, ...response.colors];+ } else {+ let tags: string[] = [];+ try {+ const response = JSON.parse(apiResponse);+ tags = response.topics;+ } catch (e) {+ console.log('JSON.parse error for:' + apiResponse, e);+ if (apiResponse) {+ tags = apiResponse.split(' ');+ }}- }- return tags.slice(0, generationSettings.current.maxTags);- }- } else if (mode === 'description' || mode === 'summary') {- if (format) {- const response = JSON.parse(apiResponse);- const arrReturn = [];- if (- generationSettings.current.structuredDataProps.name &&- response.name- ) {- arrReturn.push('**Name:** ' + response.name);- }- if (- generationSettings.current.structuredDataProps.objects &&- response.objects- ) {- arrReturn.push(- response.objects.map(- (obj) =>- (obj.name ? '\n\n> > **Object Name:** ' + obj.name : '') +- (obj.attributes- ? '\n\n> > **Attributes:** ' +- JSON.stringify(obj.attributes, null, 4).replace(- /[{}\[\]]/g,- '',- )- : ''),- ),- );+ return tags.slice(0, generationSettings.current.maxTags);}+ } else if (mode === 'description' || mode === 'summary') {+ if (format) {+ const response = JSON.parse(apiResponse);+ const arrReturn = [];+ if (+ generationSettings.current.structuredDataProps.name &&+ response.name+ ) {+ arrReturn.push('**Name:** ' + response.name);+ }+ if (+ generationSettings.current.structuredDataProps.objects &&+ response.objects+ ) {+ arrReturn.push(+ response.objects.map(+ (obj) =>+ (obj.name ? '\n\n> > **Object Name:** ' + obj.name : '') ++ (obj.attributes+ ? '\n\n> > **Attributes:** ' ++ JSON.stringify(obj.attributes, null, 4).replace(+ /[{}\[\]]/g,+ '',+ )+ : ''),+ ),+ );+ }- if (- generationSettings.current.structuredDataProps.scene &&- response.scene- ) {- arrReturn.push('**Scene:** ' + response.scene);- }- if (- generationSettings.current.structuredDataProps.colors &&- response.colors- ) {- arrReturn.push('**Colors:** ' + response.colors);- }- if (- generationSettings.current.structuredDataProps.summary &&- response.summary- ) {- arrReturn.push('**Summary:** ' + response.summary);- }- if (- generationSettings.current.structuredDataProps.time_of_day &&- response.time_of_day- ) {- arrReturn.push('**Day Time:** ' + response.time_of_day);- }- if (- generationSettings.current.structuredDataProps.settings &&- response.setting- ) {- arrReturn.push('**Setting:** ' + response.setting);- }- if (- response.text_content &&- generationSettings.current.structuredDataProps.text_content- ) {- arrReturn.push('**Content:** ' + response.text_content);+ if (+ generationSettings.current.structuredDataProps.scene &&+ response.scene+ ) {+ arrReturn.push('**Scene:** ' + response.scene);+ }+ if (+ generationSettings.current.structuredDataProps.colors &&+ response.colors+ ) {+ arrReturn.push('**Colors:** ' + response.colors);+ }+ if (+ generationSettings.current.structuredDataProps.summary &&+ response.summary+ ) {+ arrReturn.push('**Summary:** ' + response.summary);+ }+ if (+ generationSettings.current.structuredDataProps.time_of_day &&+ response.time_of_day+ ) {+ arrReturn.push('**Day Time:** ' + response.time_of_day);+ }+ if (+ generationSettings.current.structuredDataProps.settings &&+ response.setting+ ) {+ arrReturn.push('**Setting:** ' + response.setting);+ }+ if (+ response.text_content &&+ generationSettings.current.structuredDataProps.text_content+ ) {+ arrReturn.push('**Content:** ' + response.text_content);+ }+ return arrReturn.join('\n\n') + '\n\n';}- return arrReturn.join('\n\n') + '\n\n';}- }- return stream ? true : apiResponse;- });+ return stream ? true : apiResponse;+ });}function cancelMessage() {- if (ollamaClient.current) {- ollamaClient.current.abort();+ if (aiClient.current) {+ aiClient.current.abort();isTyping.current = false;saveHistoryItems();forceUpdate();@@ -1334,7 +1354,7 @@ export const ChatContextProvider = ({ children }: ChatContextProviderProps) => {initHistory,deleteHistory,checkProviderAlive,- getOllamaClient,+ getAiClient,getEntryModel,tagsGenerate,descriptionGenerate,diff --git a/tests/e2e/ai-generation.pw.e2e.js b/tests/e2e/ai-generation.pw.e2e.jsindex 60e79b6dd..49beab1d2 100644--- a/tests/e2e/ai-generation.pw.e2e.js+++ b/tests/e2e/ai-generation.pw.e2e.js@@ -8,10 +8,15 @@// supported file re-enables it// TST7003 "Max tags" cannot be set to 0 (clamps to >=1)//-// Tier 2 — generation flow against a mocked Ollama (Playwright route):-// TST7004 tags generated -> file renamed by tag embedding -> the per-file-// "processed" check stays visible (regression: entry uuid churns-// on the tag-rename, the dialog must not lose the check)+// Tier 2 — generation flow against a mocked LLM (Playwright route):+// TST7004 Ollama-native: tags generated -> file renamed by tag embedding+// -> the per-file "processed" check stays visible (regression:+// entry uuid churns on the tag-rename, the dialog must not lose it)+// TST7005 OpenAI-compatible (LM Studio/llama.cpp): tags generated via+// /v1/chat/completions; asserts the request adapter emitted+// OpenAI-shaped messages + response_format.json_schema+// TST7006 Settings → AI add-engine menu offers all presets; picking+// LM Studio fills the OpenAI base URL :1234/v1 (UI only, no net)//// All AI features are Pro and don't apply to S3 — tagged [electron,_pro].@@ -29,7 +34,9 @@ import { startTestingApp, stopApp, testDataRefresh } from './hook';import { clearDataStorage, closeWelcomePlaywright } from './welcome.helpers';import {armOllamaMock,+ armOpenAIMock,disarmOllamaMock,+ disarmOpenAIMock,openAiGenerationDialog,} from './ai.helpers';@@ -156,6 +163,135 @@ test.describe('TST70 - AI generation: supported-files guard [electron,_pro]', ()});});+test.describe('TST70 - AI generation: mocked OpenAI-compatible [electron,_pro]', () => {+ test.afterEach(async ({ isS3, testDataDir }) => {+ await testDataRefresh(isS3, testDataDir);+ await clearDataStorage();+ await stopApp();+ });++ test('TST7005 - OpenAI-compatible: tags generated via /v1, response_format adapter [electron,_pro]', async ({+ isWeb,+ isS3,+ webServerPort,+ testDataDir,+ }, testInfo) => {+ // Same end-to-end flow as TST7004 but against the generic OpenAI-compatible+ // client (LM Studio / llama.cpp): the model list comes from GET /v1/models+ // and generation from POST /v1/chat/completions. This exercises the engine+ // dispatch + the request adapter that Ollama's native path never touches.+ await bootWithLocation(+ { isWeb, isS3, webServerPort, testInfo },+ testDataDir,+ 'extconfig-ai-mock-openai.js',+ );++ const capture = {+ models: 0,+ chat: 0,+ requests: [],+ lastChatBody: null,+ };+ await armOpenAIMock(capture, ['e2ealpha', 'e2ebeta', 'e2egamma']);+ try {+ await selectFile('sample.txt');+ await openAiGenerationDialog();+ await clickOn('[data-tid=startTagsGenTID]');++ // Mock exercised: model list (/v1/models) + at least one chat call.+ await expect+ .poll(() => capture.chat, { timeout: 30000, intervals: [300] })+ .toBeGreaterThan(0);+ expect(capture.models).toBeGreaterThan(0);++ // The renderer hit the OpenAI endpoints, not Ollama's /api/*.+ expect(+ capture.requests.some((r) => r.includes('/v1/chat/completions')),+ ).toBe(true);+ expect(capture.requests.every((r) => !r.includes('/api/chat'))).toBe(true);++ // The adapter translated the internal request: OpenAI-shaped messages and+ // the tags-mode JSON schema surfaced as response_format.json_schema.+ expect(Array.isArray(capture.lastChatBody?.messages)).toBe(true);+ expect(capture.lastChatBody?.response_format?.type).toBe('json_schema');+ expect(+ capture.lastChatBody?.response_format?.json_schema?.schema,+ ).toBeTruthy();++ // Wait for the per-file processed check so the tag-apply + rename has+ // actually landed before we close and read the on-disk state.+ await expectElementExist('[data-tid=aiEntryProcessedTID]', true, 40000);++ // Tags applied -> file renamed by tag embedding (same assertion as 7004).+ await clickOn('[data-tid=cancelTagsGenTID]');+ await reloadDirectory();+ await expectElementExist(+ getGridFileSelector('sample[e2ealpha e2ebeta e2egamma].txt'),+ true,+ 15000,+ );+ await expectElementExist(getGridFileSelector('sample.txt'), false, 3000);+ } finally {+ await disarmOpenAIMock();+ }+ });+});++test.describe('TST70 - AI settings: OpenAI-compatible presets [electron,_pro]', () => {+ test.afterEach(async ({ isS3, testDataDir }) => {+ await testDataRefresh(isS3, testDataDir);+ await clearDataStorage();+ await stopApp();+ });++ test('TST7006 - Add-engine menu offers presets; LM Studio fills :1234/v1 [electron,_pro]', async ({+ isWeb,+ isS3,+ webServerPort,+ testDataDir,+ }, testInfo) => {+ // Booted WITHOUT ExtAI so the provider-editing UI is enabled (see+ // extconfig-ai-ui.js). Pure UI/redux assertion — no network — covering the+ // presets table and the per-preset default-URL logic.+ await bootWithLocation(+ { isWeb, isS3, webServerPort, testInfo },+ testDataDir,+ 'extconfig-ai-ui.js',+ );++ await clickOn('[data-tid=settings]');+ await clickOn('[data-tid=aiSettingsDialogTID]');+ await global.client.waitForSelector('[data-tid=createNewAIButtonTID]', {+ state: 'visible',+ timeout: 10000,+ });++ // Open the add-engine menu and confirm every preset is offered.+ await clickOn('[data-tid=createNewAIButtonTID]');+ await expectElementExist('[data-tid=aiAddProvider_ollamaTID]', true, 5000);+ await expectElementExist('[data-tid=aiAddProvider_lmstudioTID]', true, 3000);+ await expectElementExist('[data-tid=aiAddProvider_llamacppTID]', true, 3000);+ await expectElementExist(+ '[data-tid=aiAddProvider_openai-compatibleTID]',+ true,+ 3000,+ );++ // Pick LM Studio -> a provider accordion appears, pre-filled with the+ // OpenAI base URL and the preset label.+ await clickOn('[data-tid=aiAddProvider_lmstudioTID]');+ await expect+ .poll(() => global.client.inputValue('[data-tid=ollamaEngineTID] input'), {+ timeout: 8000,+ intervals: [200],+ })+ .toBe('http://localhost:1234/v1');+ expect(+ await global.client.inputValue('[data-tid=engineTID] input'),+ ).toBe('LM Studio');+ });+});+test.describe('TST70 - AI generation: mocked Ollama [electron,_pro]', () => {test.afterEach(async ({ isS3, testDataDir }) => {await testDataRefresh(isS3, testDataDir);diff --git a/tests/e2e/ai.helpers.js b/tests/e2e/ai.helpers.jsindex 4cbb57c11..8fa3f2fff 100644--- a/tests/e2e/ai.helpers.js+++ b/tests/e2e/ai.helpers.js@@ -120,6 +120,132 @@ export async function disarmOllamaMock() {await global.client.unroute(OLLAMA_HOST_PATTERN);}+/* ------------------------------------------------------------------ *+ * OpenAI-compatible mock (LM Studio / llama.cpp / vLLM / OpenAI)+ *+ * The renderer's generic client (src/renderer/components/chat/OpenAIClient.ts)+ * talks plain OpenAI HTTP from the browser context: model listing hits+ * `GET {base}/models`, generation hits `POST {base}/chat/completions`. Unlike+ * the Ollama-native mock above this must also speak the OpenAI request/response+ * shapes, so it doubles as coverage for the request adapter — the captured+ * `lastChatBody` lets a test assert that `format` was translated to+ * `response_format.json_schema` and that messages are OpenAI-shaped.+ *+ * Matches either localhost or 127.0.0.1 on port 1234 (the LM Studio default+ * and the preset URL), so it covers both the mock config host and the UI+ * preset default.+ * ------------------------------------------------------------------ */+const OPENAI_HOST_PATTERN = /(?:localhost|127\.0\.0\.1):1234\//;++function openAiModel(id) {+ return { id, object: 'model', owned_by: 'e2e-mock' };+}++/** OpenAI non-streaming chat completion envelope. */+function openAiChatResponse(content) {+ return {+ id: 'chatcmpl-e2e',+ object: 'chat.completion',+ created: 1,+ model: 'mock-text-model',+ choices: [+ {+ index: 0,+ message: { role: 'assistant', content },+ finish_reason: 'stop',+ },+ ],+ };+}++/** OpenAI SSE stream body that assembles to `content`, split in two chunks. */+function openAiStreamBody(content) {+ const mid = Math.ceil(content.length / 2);+ const parts = [content.slice(0, mid), content.slice(mid)];+ const frames = parts.map(+ (p) => `data: ${JSON.stringify({ choices: [{ delta: { content: p } }] })}`,+ );+ return [...frames, 'data: [DONE]'].join('\n\n') + '\n\n';+}++/**+ * Arm the OpenAI-compatible interceptor.+ *+ * @param capture mutable bucket — { models, chat, requests, lastChatBody } —+ * updated in place so a test can assert call counts and inspect the+ * translated request body the renderer actually sent.+ * @param tags tag titles the mocked chat returns; tags mode parses the+ * assistant content as JSON and reads `.topics` (same as Ollama).+ */+export async function armOpenAIMock(+ capture = { models: 0, chat: 0, requests: [], lastChatBody: null },+ tags = ['e2ealpha', 'e2ebeta', 'e2egamma'],+) {+ await global.client.route(OPENAI_HOST_PATTERN, async (route) => {+ const request = route.request();+ const url = request.url();+ const method = request.method();+ capture.requests.push(method + ' ' + url);++ if (method === 'OPTIONS') {+ await route.fulfill({ status: 204, headers: CORS_HEADERS, body: '' });+ return;+ }+ if (url.includes('/models')) {+ capture.models += 1;+ await route.fulfill({+ status: 200,+ contentType: 'application/json',+ headers: CORS_HEADERS,+ body: JSON.stringify({+ object: 'list',+ data: [openAiModel('mock-text-model'), openAiModel('mock-vision-model')],+ }),+ });+ return;+ }+ if (url.includes('/chat/completions')) {+ capture.chat += 1;+ let body = {};+ try {+ body = JSON.parse(request.postData() || '{}');+ } catch (e) {+ body = {};+ }+ capture.lastChatBody = body;+ const content = JSON.stringify({ topics: tags });+ if (body.stream) {+ await route.fulfill({+ status: 200,+ contentType: 'text/event-stream',+ headers: CORS_HEADERS,+ body: openAiStreamBody(content),+ });+ } else {+ await route.fulfill({+ status: 200,+ contentType: 'application/json',+ headers: CORS_HEADERS,+ body: JSON.stringify(openAiChatResponse(content)),+ });+ }+ return;+ }+ // Anything else the client might probe — keep it 200 so nothing throws.+ await route.fulfill({+ status: 200,+ contentType: 'application/json',+ headers: CORS_HEADERS,+ body: '{}',+ });+ });+ return capture;+}++export async function disarmOpenAIMock() {+ await global.client.unroute(OPENAI_HOST_PATTERN);+}+/*** Open the AI generation dialog from the grid perspective toolbar and wait* for it to render. Assumes a location is open and at least one entry is