The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
fix(assistant): chat with an own server entered without /v1 at /v1
Maurice186dd94b6199
diff --git a/server/src/nest/ai-connections/ai-chat-client.service.ts b/server/src/nest/ai-connections/ai-chat-client.service.tsindex 73f0011f9..3a9db9a90 100644--- a/server/src/nest/ai-connections/ai-chat-client.service.ts+++ b/server/src/nest/ai-connections/ai-chat-client.service.ts@@ -1,6 +1,6 @@import { safeFetch, safeFetchLlm } from '../../utils/ssrfGuard';import type { AiChatClient } from './ai-chat-client.interface';-import { AI_BASE_URLS } from './ai-connections.constants';+import { AI_BASE_URLS, ownServerChatBase } from './ai-connections.constants';import type { ResolvedAiConnection } from './ai-connections.types';import { ChatgptAuthService } from './chatgpt-auth.service';import { AnthropicMessagesClient } from './providers/anthropic-messages.client';@@ -67,7 +67,7 @@ export class AiChatClientService {return new OpenAiChatClient(kind,model,- (connection.baseUrl ?? '').replace(/(?<!\/)\/+$/, ''),+ ownServerChatBase(connection.baseUrl),credential.apiKey,adminServerFetch,);diff --git a/server/src/nest/ai-connections/ai-connections.constants.ts b/server/src/nest/ai-connections/ai-connections.constants.tsindex 72b6f0cdb..c016f2cfc 100644--- a/server/src/nest/ai-connections/ai-connections.constants.ts+++ b/server/src/nest/ai-connections/ai-connections.constants.ts@@ -22,6 +22,26 @@ export function aiHostOf(kind: AiConnectionKind, baseUrl: string | null): string}}+/**+ * Where an own server's Chat Completions live. An address with a path, such as+ * `https://api.groq.com/openai/v1` or `http://ollama.lan:11434/v1`, is used as+ * entered. A bare server address gets `/v1`: Ollama, LM Studio, vLLM,+ * llama.cpp and LiteLLM all answer there, while Ollama answers+ * `/chat/completions` at its root with a 404. Connecting already found the+ * model list of such an address under the root, so the chat failed where the+ * connection had worked.+ */+export function ownServerChatBase(baseUrl: string | null): string {+ const entered = (baseUrl ?? '').trim().replace(/(?<!\/)\/+$/, '');+ try {+ const url = new URL(entered);+ if (url.pathname === '/' && !url.search && !url.hash) return `${entered}/v1`;+ } catch {+ // An address that does not parse fails on the request, as it did before.+ }+ return entered;+}+/** Whether OpenRouter's OAuth can send the browser back here: it only returns to an https address. */export function openrouterOAuthAvailable(): boolean {return getAppUrl().startsWith('https://');diff --git a/server/tests/e2e/assistant-home.e2e.test.ts b/server/tests/e2e/assistant-home.e2e.test.tsindex 67994279c..9b97f689c 100644--- a/server/tests/e2e/assistant-home.e2e.test.ts+++ b/server/tests/e2e/assistant-home.e2e.test.ts@@ -100,6 +100,16 @@ async function answer(req: IncomingMessage, res: ServerResponse): Promise<void>let nestApp: INestApplication;let orm: MikroORM;let fake: HttpServer;+let adminId: number;++/** The instance connection, pointed at the fake under `path`: `/v1`, or nothing as an Ollama address is often entered. */+function connectAt(path: string) {+ const { port } = fake.address() as AddressInfo;+ return request(nestApp.getHttpServer())+ .put('/api/admin/ai/connection')+ .set('Cookie', sessionCookie(adminId))+ .send({ kind: 'custom', baseUrl: `http://127.0.0.1:${port}${path}`, model: 'fake-model' });+}beforeAll(async () => {fake = createServer((req, res) => {@@ -110,13 +120,8 @@ beforeAll(async () => {orm = nestApp.get(MikroORM);await setAddonEnabled(orm, 'ai_assistant', true);await setAddonEnabled(orm, 'atlas', true);- const adminId = (await makeUser(orm, { role: 'admin' })).user.id;- const { port } = fake.address() as AddressInfo;- const res = await request(nestApp.getHttpServer())- .put('/api/admin/ai/connection')- .set('Cookie', sessionCookie(adminId))- .send({ kind: 'custom', baseUrl: `http://127.0.0.1:${port}/v1`, model: 'fake-model' });- expect(res.status).toBe(200);+ adminId = (await makeUser(orm, { role: 'admin' })).user.id;+ expect((await connectAt('/v1')).status).toBe(200);});beforeEach(async () => {@@ -342,4 +347,23 @@ describe('Home assistant e2e', () => {expect(await countRows(orm, AssistantThreads, { id: homeThread })).toBe(0);expect(await countRows(orm, AssistantThreads, { id: tripThread })).toBe(1);});++ it('ASTHE2E-007: an Ollama address entered without /v1 chats at /v1/chat/completions, where Ollama answers', async () => {+ const h = await ownerWithTrips();+ expect((await connectAt('')).status).toBe(200);+ try {+ provider.script = [say('Hello from Ollama.')];+ const res = await post(`${BASE}/chat`, h.userId, { ...CTX, threadId: null, text: 'hi' });+ expect(res.status).toBe(200);+ expect(provider.requests).toHaveLength(1);+ expect(+ ofType(res.chunks, 'text-delta')+ .map((c) => c.delta)+ .join(''),+ ).toBe('Hello from Ollama.');+ expect(ofType(res.chunks, 'error')).toEqual([]);+ } finally {+ expect((await connectAt('/v1')).status).toBe(200);+ }+ });});diff --git a/server/tests/unit/nest/ai-connections/ai-connections.helpers.test.ts b/server/tests/unit/nest/ai-connections/ai-connections.helpers.test.tsindex 0cb33cfdd..069aa9364 100644--- a/server/tests/unit/nest/ai-connections/ai-connections.helpers.test.ts+++ b/server/tests/unit/nest/ai-connections/ai-connections.helpers.test.ts@@ -7,6 +7,7 @@ import {aiHostOf,aiRefusal,openrouterOAuthAvailable,+ ownServerChatBase,providerRefusal,} from '../../../../src/nest/ai-connections/ai-connections.constants';import { InMemoryAiRefreshLock } from '../../../../src/nest/ai-connections/ai-refresh-lock';@@ -145,6 +146,16 @@ describe('constants', () => {expect(aiHostOf('custom', null)).toBe('');});+ it('AIHLP-032b: a bare own server address chats under /v1, an address with a path as entered', () => {+ expect(ownServerChatBase('http://ollama.lan:11434')).toBe('http://ollama.lan:11434/v1');+ expect(ownServerChatBase(' http://ollama.lan:11434/ ')).toBe('http://ollama.lan:11434/v1');+ expect(ownServerChatBase('http://ollama.lan:11434/v1/')).toBe('http://ollama.lan:11434/v1');+ expect(ownServerChatBase('https://api.groq.com/openai/v1')).toBe('https://api.groq.com/openai/v1');+ expect(ownServerChatBase('https://llm.example.org/proxy')).toBe('https://llm.example.org/proxy');+ expect(ownServerChatBase('not a url')).toBe('not a url');+ expect(ownServerChatBase(null)).toBe('');+ });+it('AIHLP-033: OpenRouter sign-in is offered only on an https app URL', () => {vi.stubEnv('APP_URL', 'https://trip.example.org');expect(openrouterOAuthAvailable()).toBe(true);