diff --git a/server/src/nest/ai-connections/ai-chat-client.service.ts b/server/src/nest/ai-connections/ai-chat-client.service.ts index 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(/(? 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.ts index 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);