Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { extractEndpointSchemas } from '#/server/ingest/bundle.ts'
4import { responseMedia } from '#/server/provider-openapi.ts'
5import type { OpenApiDocument } from '../types.ts'
6import { provider } from './ollama.ts'
7
8/** The shape of `/api/chat` in ollama/ollama docs/openapi.yaml. */
9const spec = {
10 openapi: '3.1.0',
11 paths: {
12 '/api/chat': {
13 post: {
14 requestBody: {
15 content: {
16 'application/json': {
17 schema: { $ref: '#/components/schemas/ChatRequest' },
18 },
19 },
20 },
21 responses: {
22 '200': {
23 content: {
24 'application/json': {
25 schema: { $ref: '#/components/schemas/ChatResponse' },
26 },
27 'application/x-ndjson': {
28 schema: { $ref: '#/components/schemas/ChatStreamEvent' },
29 },
30 },
31 },
32 },
33 },
34 },
35 },
36 components: {
37 schemas: {
38 ChatRequest: {
39 type: 'object',
40 properties: { model: { type: 'string' }, stream: { type: 'boolean' } },
41 },
42 ChatResponse: { type: 'object', properties: { done: { const: true } } },
43 ChatStreamEvent: {
44 type: 'object',
45 properties: { done: { type: 'boolean' } },
46 },
47 },
48 },
49} as unknown as OpenApiDocument
50
51describe('ollama', () => {
52 it('classifies the generation routes and drops management ones', () => {
53 expect(provider.classify('/api/chat', {})).toBe('chat')
54 expect(provider.classify('/api/generate', {})).toBe('chat')
55 expect(provider.classify('/api/embed', {})).toBe('embeddings')
56 expect(provider.classify('/api/tags', {})).toBeNull()
57 expect(provider.classify('/api/pull', {})).toBeNull()
58 })
59
60 it('takes a configurable server, keyless locally', () => {
61 const server = provider.connect?.servers[0]
62 expect(server?.url).toBe('{baseUrl}')
63 expect(server?.variables?.baseUrl?.default).toBe('http://localhost:11434')
64 expect(provider.connect?.security).toEqual([{}, { bearerAuth: [] }])
65 })
66
67 it('keeps the NDJSON stream beside the JSON body', () => {
68 const extracted = extractEndpointSchemas(spec, '/api/chat')
69 expect(extracted.outputContentTypes).toEqual([
70 'application/json',
71 'application/x-ndjson',
72 ])
73 expect(extracted.output?.properties).toEqual({ done: { const: true } })
74 expect(extracted.streamOutput?.properties).toEqual({
75 done: { type: 'boolean' },
76 })
77 expect(
78 responseMedia(
79 extracted.output,
80 extracted.outputContentTypes,
81 extracted.streamOutput,
82 )?.content,
83 ).toEqual({
84 'application/json': { schema: extracted.output },
85 'application/x-ndjson': { schema: extracted.streamOutput },
86 })
87 })
88})