Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

feat: add Moondream hosted vision provider (#1307)

Tashfeeneadbeb5bfe94
diff --git a/client/src/components/keys/shared.tsx b/client/src/components/keys/shared.tsx
index 0e9aee18..013276b2 100644
--- a/client/src/components/keys/shared.tsx
+++ b/client/src/components/keys/shared.tsx
@@ -27,6 +27,7 @@ export function GetKeyLink({ url }: { url: string }) {
// routing treats the platform as configured.
export const PLATFORMS: { value: Platform; label: string; url: string; keyless?: boolean }[] = [
{ value: 'aclide', label: 'ACLIDE (shared monthly credits)', url: 'https://aclide.com/en/dashboard/api-keys' },
+ { value: 'moondream', label: 'Moondream ($5 shared monthly credits)', url: 'https://moondream.ai/c/cloud/api-keys' },
{ value: 'google', label: 'Google AI Studio', url: 'https://aistudio.google.com/apikey' },
{ value: 'groq', label: 'Groq', url: 'https://console.groq.com/keys' },
{ value: 'cerebras', label: 'Cerebras', url: 'https://cloud.cerebras.ai' },
diff --git a/client/src/lib/routing.ts b/client/src/lib/routing.ts
index d83eb6ed..952f3218 100644
--- a/client/src/lib/routing.ts
+++ b/client/src/lib/routing.ts
@@ -404,6 +404,7 @@ export const platformColors: Record<string, string> = {
cerebras: '#8b5cf6',
sail: '#0ea5e9',
aclide: '#6366f1',
+ moondream: '#6d5dfc',
electronhub: '#6366f1',
experiential: '#14b8a6',
router9: '#8b5cf6',
diff --git a/docs/moondream.md b/docs/moondream.md
new file mode 100644
index 00000000..1ddd70cb
--- /dev/null
+++ b/docs/moondream.md
@@ -0,0 +1,41 @@
+# Moondream
+
+Moondream Cloud provides a shared **$5/workspace monthly usage allowance** on
+the Free plan. This is not $5 per model and not unlimited free inference.
+Hosted usage beyond the allowance is metered. We route to its hosted API with
+the user's own key; we do not host or redistribute model weights.
+
+Sources checked September 22, 2026:
+
+- [Pricing](https://moondream.ai/pricing)
+- [OpenAI compatibility](https://docs.moondream.ai/openai/)
+
+Add the key under **Moondream** on the Keys page, or import
+`MOONDREAM_API_KEY=...`. The adapter calls
+`https://api.moondream.ai/v1/chat/completions` with Bearer authentication.
+
+Both `moondream3.1-9B-A2B` and `moondream3-preview` passed real image-question
+and native text-streaming requests through this adapter, with matching returned
+model IDs and usage. Model rows are published separately
+in the signed Oracle catalog, not seeded in migrations. Existing Premium-now /
+Free-after-30-days delivery remains unchanged.
+
+## API differences
+
+- Images must be base64 data URLs. Remote image URLs are rejected locally with
+ a retryable provider error; the adapter does not fetch arbitrary URLs.
+- Tool calling, tool history, stop sequences and structured output are not
+ supported. Requests requiring them fail over rather than silently losing
+ their meaning.
+- `max_tokens` maps to `max_completion_tokens`, respecting the user's cap and
+ Moondream's documented 4096-token output maximum (including reasoning).
+- `reasoning_effort: none` disables reasoning; other effort levels enable it.
+ The API exposes a boolean, not distinct effort levels.
+- Native SSE is streamed, with optional usage and `reasoning` normalized to
+ `reasoning_content`. Timeouts, disconnects and truncation use the shared
+ provider infrastructure.
+- Key checks send Bearer authentication to `/v1/models` without spending
+ inference credits. Missing auth returns a public roster, but invalid Bearer
+ credentials returned 401 in live tests; empty keys are rejected locally.
+- No context-window or request-rate values are guessed. The public model list
+ currently omits one working model and is not used to populate catalog rows.
diff --git a/server/src/__tests__/providers/moondream.test.ts b/server/src/__tests__/providers/moondream.test.ts
new file mode 100644
index 00000000..1c711e7f
--- /dev/null
+++ b/server/src/__tests__/providers/moondream.test.ts
@@ -0,0 +1,157 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import type { ChatCompletionChunk, ChatMessage } from '@freellmapi/shared/types.js';
+import { MoondreamProvider } from '../../providers/moondream.js';
+import { getProvider } from '../../providers/index.js';
+import { AUTH_JSON_PROVIDER_MAP, detectPlatform, parseKeysFromFile } from '../../lib/key-parser.js';
+import { supportedParametersFor } from '../../lib/sampling-params.js';
+import { isRetryableError } from '../../lib/error-classify.js';
+
+const model = 'moondream3.1-9B-A2B';
+const messages: ChatMessage[] = [{ role: 'user', content: 'Say OK' }];
+const image = 'data:image/jpeg;base64,YWJj';
+const json = (body: unknown, status = 200) => new Response(JSON.stringify(body), { status });
+const completion = () => ({ id: 'chatcmpl-test', object: 'chat.completion', created: 1, model,
+ choices: [{ index: 0, message: { role: 'assistant', content: 'OK', reasoning: 'A thought.' }, finish_reason: 'stop' }],
+ usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 } });
+const chunk = (delta: unknown, finish_reason: string | null = null) => ({ id: 'chatcmpl-test',
+ object: 'chat.completion.chunk', created: 1, model, choices: [{ index: 0, delta, finish_reason }] });
+const sse = (frames: unknown[], done = true) => new Response(frames.map(frame => `data: ${JSON.stringify(frame)}\n\n`).join('') + (done ? 'data: [DONE]\n\n' : ''),
+ { headers: { 'Content-Type': 'text/event-stream' } });
+async function collect(source: AsyncGenerator<ChatCompletionChunk>) {
+ const result: ChatCompletionChunk[] = [];
+ for await (const value of source) result.push(value);
+ return result;
+}
+
+describe('Moondream hosted adapter', () => {
+ afterEach(() => vi.restoreAllMocks());
+
+ it('registers the provider and key import, and advertises only implemented parameters', () => {
+ expect(getProvider('moondream')).toBeInstanceOf(MoondreamProvider);
+ expect(detectPlatform('MOONDREAM_')).toBe('moondream');
+ expect(parseKeysFromFile('MOONDREAM_API_KEY=test-key', 'keys.env').keys[0]?.platform).toBe('moondream');
+ expect(AUTH_JSON_PROVIDER_MAP.moondream).toBe('moondream');
+ expect(supportedParametersFor('moondream', { tools: true })).toEqual([
+ 'temperature', 'top_p', 'max_tokens', 'max_completion_tokens', 'stream', 'reasoning_effort',
+ ]);
+ });
+
+ it('maps token/reasoning options, preserves zero sampling and normalizes output', async () => {
+ const fetch = vi.spyOn(global, 'fetch').mockResolvedValue(json(completion()));
+ const result = await new MoondreamProvider().chatCompletion('test-key', messages, model,
+ { max_tokens: 32, temperature: 0, top_p: 0, reasoning_effort: 'none', seed: 1 });
+ expect(fetch.mock.calls[0][0]).toBe('https://api.moondream.ai/v1/chat/completions');
+ expect(fetch.mock.calls[0][1]?.headers).toMatchObject({ Authorization: 'Bearer test-key' });
+ expect(JSON.parse(String(fetch.mock.calls[0][1]?.body))).toEqual({ model, messages, stream: false,
+ max_completion_tokens: 32, temperature: 0, top_p: 0, reasoning: false });
+ expect(result.choices[0].message).toEqual({ role: 'assistant', content: 'OK', reasoning_content: 'A thought.' });
+ expect(result.usage.total_tokens).toBe(15);
+ expect(result._routed_via).toEqual({ platform: 'moondream', model });
+ });
+
+ it('preserves vision input, strips replay-only fields and obeys upstream output ceiling', async () => {
+ const fetch = vi.spyOn(global, 'fetch').mockResolvedValue(json(completion()));
+ await new MoondreamProvider().chatCompletion('k', [
+ { role: 'system', content: 'Be concise.' },
+ { role: 'user', content: ['Look', { type: 'image_url', image_url: { url: image, detail: 'high' } }] },
+ { role: 'assistant', content: 'OK', reasoning_content: 'private', partial: true },
+ ...messages,
+ ], model, { max_tokens: 8000, reasoning_effort: 'high' });
+ const body = JSON.parse(String(fetch.mock.calls[0][1]?.body));
+ expect(body.max_completion_tokens).toBe(4096);
+ expect(body.reasoning).toBe(true);
+ expect(body.messages[1].content).toEqual([{ type: 'text', text: 'Look' }, { type: 'image_url', image_url: { url: image } }]);
+ expect(body.messages[2]).toEqual({ role: 'assistant', content: 'OK' });
+ });
+
+ it.each([
+ { stop: ['end'] },
+ { tools: [{ type: 'function' as const, function: { name: 'f' } }] },
+ { tool_choice: 'required' as const },
+ { response_format: { type: 'json_object' as const } },
+ ])('fails over unsupported request options before fetching: %j', async options => {
+ const fetch = vi.spyOn(global, 'fetch');
+ const error = await new MoondreamProvider().chatCompletion('k', messages, model, options).catch(e => e);
+ expect(error.status).toBe(422);
+ expect(isRetryableError(error)).toBe(true);
+ expect(fetch).not.toHaveBeenCalled();
+ });
+
+ it.each<ChatMessage[]>([
+ [{ role: 'tool', tool_call_id: 'a', content: 'OK' }],
+ [{ role: 'assistant', content: null, tool_calls: [{ id: 'a', type: 'function', function: { name: 'f', arguments: '{}' } }] }],
+ [{ role: 'user', content: [{ type: 'image_url', image_url: { url: 'https://example.com/image.png' } }] }],
+ [{ role: 'user', content: [{ type: 'input_audio', text: 'not a text part' }] }],
+ ])('rejects incompatible history/media without downloading URLs: %j', async message => {
+ const fetch = vi.spyOn(global, 'fetch');
+ await expect(new MoondreamProvider().chatCompletion('k', [message], model)).rejects.toMatchObject({ status: 422 });
+ expect(fetch).not.toHaveBeenCalled();
+ });
+
+ it('validates only authenticated keys without inference, keeping outages inconclusive', async () => {
+ const fetch = vi.spyOn(global, 'fetch').mockResolvedValueOnce(json({ data: [{ id: model }] }))
+ .mockResolvedValueOnce(json({ error: 'Invalid API key' }, 401))
+ .mockResolvedValueOnce(json({}, 503)).mockResolvedValueOnce(json({}));
+ const provider = new MoondreamProvider();
+ await expect(provider.validateKey('')).resolves.toMatchObject({ valid: false });
+ expect(fetch).not.toHaveBeenCalled();
+ await expect(provider.validateKey('key')).resolves.toBe(true);
+ expect(fetch.mock.calls[0][0]).toBe('https://api.moondream.ai/v1/models');
+ expect(fetch.mock.calls[0][1]?.headers).toMatchObject({ Authorization: 'Bearer key' });
+ await expect(provider.validateKey('bad')).resolves.toMatchObject({ valid: false });
+ await expect(provider.validateKey('key')).rejects.toMatchObject({ status: 503 });
+ await expect(provider.validateKey('key')).rejects.toMatchObject({ status: 502 });
+ });
+
+ it.each([
+ { model: 'wrong' }, { model: undefined }, { choices: [] },
+ { choices: [{ message: { content: '' }, finish_reason: 'stop' }] },
+ { choices: [{ message: { content: 'unfinished' }, finish_reason: null }] },
+ ])('rejects substituted or invalid completions: %j', async override => {
+ vi.spyOn(global, 'fetch').mockResolvedValue(json({ ...completion(), ...override }));
+ await expect(new MoondreamProvider().chatCompletion('k', messages, model)).rejects.toMatchObject({ status: 502 });
+ });
+
+ it('streams native SSE with normalized reasoning and opt-in usage', async () => {
+ const fetch = vi.spyOn(global, 'fetch').mockImplementation(async () => sse([
+ chunk({ role: 'assistant' }), chunk({ reasoning: 'Think' }), chunk({ content: 'OK' }), chunk({}, 'stop'),
+ { ...chunk({}), choices: [], usage: completion().usage },
+ ]));
+ const provider = new MoondreamProvider();
+ const frames = await collect(provider.streamChatCompletion('k', messages, model, { max_tokens: 32, stream_options: { include_usage: true } }));
+ expect(JSON.parse(String(fetch.mock.calls[0][1]?.body))).toMatchObject({ stream: true, stream_options: { include_usage: true }, max_completion_tokens: 32 });
+ expect(frames[1].choices[0].delta).toEqual({ reasoning_content: 'Think' });
+ expect(frames.at(-1)?.usage?.total_tokens).toBe(15);
+ expect((await collect(provider.streamChatCompletion('k', messages, model))).every(frame => frame.choices.length > 0)).toBe(true);
+ });
+
+ it('rejects substituted streaming identities before yielding', async () => {
+ vi.spyOn(global, 'fetch').mockResolvedValue(sse([{ ...chunk({ content: 'oops' }), model: 'wrong' }]));
+ await expect(new MoondreamProvider().streamChatCompletion('k', messages, model).next()).rejects.toMatchObject({ status: 502 });
+ });
+
+ it.each([true, false])('detects missing terminal completion, DONE=%s', async done => {
+ vi.spyOn(global, 'fetch').mockResolvedValue(sse([chunk({ content: 'partial' })], done));
+ await expect(collect(new MoondreamProvider().streamChatCompletion('k', messages, model))).rejects.toThrow(/stream ended/);
+ });
+
+ it('preserves 429 status/backoff even for non-JSON errors', async () => {
+ vi.spyOn(global, 'fetch').mockImplementation(async () => new Response('Rate limited', { status: 429, headers: { 'Retry-After': '12' } }));
+ const provider = new MoondreamProvider();
+ await expect(provider.chatCompletion('k', messages, model)).rejects.toMatchObject({ status: 429, retryAfterMs: 12000 });
+ await expect(provider.streamChatCompletion('k', messages, model).next()).rejects.toMatchObject({ status: 429, retryAfterMs: 12000 });
+ });
+
+ it('cancels a stalled response body on disconnect', async () => {
+ vi.spyOn(global, 'fetch').mockImplementation(async (_url, init) => new Response(new ReadableStream({
+ start(controller) {
+ if (init?.signal?.aborted) controller.error(init.signal.reason);
+ else init?.signal?.addEventListener('abort', () => controller.error(init.signal?.reason), { once: true });
+ },
+ })));
+ const controller = new AbortController();
+ const pending = new MoondreamProvider().chatCompletion('k', messages, model, { signal: controller.signal });
+ controller.abort();
+ await expect(pending).rejects.toThrow();
+ });
+});
diff --git a/server/src/__tests__/routes/keys-providers-checklist.test.ts b/server/src/__tests__/routes/keys-providers-checklist.test.ts
index 6f527ed9..48b68979 100644
--- a/server/src/__tests__/routes/keys-providers-checklist.test.ts
+++ b/server/src/__tests__/routes/keys-providers-checklist.test.ts
@@ -53,6 +53,8 @@ describe('GET /api/keys/providers — provider checklist (#543)', () => {
expect(groq).toMatchObject({ platform: 'groq', configured: false, keyCount: 0 });
expect(typeof groq.name).toBe('string');
expect(typeof groq.keyless).toBe('boolean');
+ expect(body.providers.find((p: { platform: string }) => p.platform === 'moondream'))
+ .toMatchObject({ platform: 'moondream', name: 'Moondream', configured: false, keyless: false });
expect(body.summary).toEqual({
total: body.providers.length,
configured: 0,
diff --git a/server/src/lib/key-parser.ts b/server/src/lib/key-parser.ts
index 90db2f09..dcc02676 100644
--- a/server/src/lib/key-parser.ts
+++ b/server/src/lib/key-parser.ts
@@ -39,6 +39,7 @@ export interface ParseResult {
export const PREFIX_MAP: Record<string, string> = {
ACLIDE_: 'aclide',
+ MOONDREAM_: 'moondream',
GOOGLE_: 'google',
GEMINI_: 'google',
GROQ_: 'groq',
@@ -133,6 +134,7 @@ export const AUTH_JSON_PROVIDER_MAP: Record<string, string> = {
groq: 'groq',
sail: 'sail',
aclide: 'aclide',
+ moondream: 'moondream',
'sail-research': 'sail',
sailresearch: 'sail',
electronhub: 'electronhub',
diff --git a/server/src/lib/sampling-params.ts b/server/src/lib/sampling-params.ts
index fed20215..390ae338 100644
--- a/server/src/lib/sampling-params.ts
+++ b/server/src/lib/sampling-params.ts
@@ -242,6 +242,12 @@ export const GITHUB_MAX_OUTPUT_TOKENS = 400;
// silently no-op'ing the policy; the string-typed accessors below cast at the
// boundary since routes carry platform ids as plain strings.
export const PLATFORM_PARAM_POLICIES: Partial<Record<Platform, PlatformParamPolicy>> = {
+ // Moondream maps reasoning effort to a boolean and max_tokens to
+ // max_completion_tokens. 4096 is the documented upstream output ceiling.
+ moondream: {
+ drop: ['top_k', 'min_p', 'seed', 'presence_penalty', 'frequency_penalty', 'repetition_penalty', 'logit_bias', 'logprobs', 'top_logprobs', 'response_format'],
+ maxTokensCap: 4096,
+ },
// ACLIDE uses Responses; these Chat Completions parameters have no mapping.
aclide: {
drop: ['top_k', 'min_p', 'seed', 'presence_penalty', 'frequency_penalty', 'repetition_penalty', 'logit_bias', 'logprobs', 'top_logprobs'],
@@ -434,6 +440,8 @@ export function platformDropsResponseFormat(platform: string): boolean {
* every surface supports, plus tools when the model does, minus the
* platform's droplist. */
export function supportedParametersFor(platform: string, caps: { tools?: boolean } = {}): string[] {
+ // Unlike the generic base set, Moondream has neither stop nor tools.
+ if (platform === 'moondream') return ['temperature', 'top_p', 'max_tokens', 'max_completion_tokens', 'stream', 'reasoning_effort'];
const policy = PLATFORM_PARAM_POLICIES[platform as Platform];
const dropped = new Set<string>(policy?.drop ?? []);
const params = [
diff --git a/server/src/providers/index.ts b/server/src/providers/index.ts
index 69901970..f099a587 100644
--- a/server/src/providers/index.ts
+++ b/server/src/providers/index.ts
@@ -10,6 +10,7 @@ import { PollinationsProvider } from './pollinations.js';
import { ZhipuProvider } from './zhipu.js';
import { SailProvider } from './sail.js';
import { AclideProvider } from './aclide.js';
+import { MoondreamProvider } from './moondream.js';
import { ElectronHubProvider } from './electronhub.js';
import { ExperientialProvider } from './experiential.js';
import { Router9Provider } from './router9.js';
@@ -56,6 +57,7 @@ register(new OpenAICompatProvider({
// stay in Oracle so the existing Premium-now / Free-after-30-days gate applies.
register(new SailProvider());
register(new AclideProvider());
+register(new MoondreamProvider());
// Free-plan grants are shared wallets, not free credits per model. Eligibility
// and tested model rows belong in Oracle, never in bundled DB migrations.
diff --git a/server/src/providers/moondream.ts b/server/src/providers/moondream.ts
new file mode 100644
index 00000000..5eaac226
--- /dev/null
+++ b/server/src/providers/moondream.ts
@@ -0,0 +1,129 @@
+import type { ChatCompletionChunk, ChatCompletionResponse, ChatMessage, Platform } from '@freellmapi/shared/types.js';
+import { BaseProvider, providerHttpError, type CompletionOptions, type KeyValidationResult } from './base.js';
+import { resolveMaxTokens } from '../lib/sampling-params.js';
+import { providerTimeoutMs } from '../lib/provider-timeout.js';
+import { recordQuotaObservationsFromResponse, type QuotaObservationContext } from '../services/provider-quota.js';
+
+const BASE_URL = 'https://api.moondream.ai/v1';
+const unsupported = (message: string) => Object.assign(new Error(`Moondream ${message}`), { status: 422 });
+const upstreamError = (message: string) => Object.assign(new Error(`Moondream ${message}`), { status: 502 });
+
+function checkModel(requested: string, returned: unknown): void {
+ if (returned !== requested) throw upstreamError('returned a different or missing model identity');
+}
+
+/** Hosted BYOK API, not self-hosted weights. Model rows belong exclusively in
+ * the signed catalog to preserve Premium-now / Free-after-30-days delivery. */
+export class MoondreamProvider extends BaseProvider {
+ readonly platform: Platform = 'moondream';
+ readonly name = 'Moondream';
+
+ private headers(apiKey: string): Record<string, string> {
+ return { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' };
+ }
+
+ private body(messages: ChatMessage[], modelId: string, options: CompletionOptions | undefined, stream: boolean): Record<string, unknown> {
+ if (options?.tools?.length || options?.tool_choice && options.tool_choice !== 'none') {
+ throw unsupported('does not support tool calling');
+ }
+ if (options?.stop?.length) throw unsupported('does not support stop sequences');
+ if (options?.response_format) {
+ throw unsupported('does not support structured output');
+ }
+ const input = messages.map(message => {
+ if (message.role === 'tool' || message.tool_calls?.length) throw unsupported('does not support tool history');
+ const content = Array.isArray(message.content) ? message.content.map(part => {
+ if (typeof part === 'string') return { type: 'text', text: part };
+ if ((!part.type || part.type === 'text') && typeof part.text === 'string') return { type: 'text', text: part.text };
+ if (part.type === 'image_url') {
+ const image = part.image_url as { url?: unknown } | undefined;
+ if (typeof image?.url === 'string' && /^data:image\/[a-z0-9.+-]+;base64,[a-z0-9+/]+={0,2}$/i.test(image.url)) {
+ return { type: 'image_url', image_url: { url: image.url } };
+ }
+ // Never download arbitrary URLs here (SSRF); callers can supply
+ // base64 images or fail over to a provider supporting remote URLs.
+ throw unsupported('requires base64 image data URLs, not remote URLs');
+ }
+ throw unsupported('does not support this content block');
+ }) : message.content ?? '';
+ // Omit internal reasoning/signature/name/prefill fields on replay.
+ return { role: message.role, content };
+ });
+ return {
+ model: modelId, messages: input, stream,
+ temperature: options?.temperature, top_p: options?.top_p,
+ max_completion_tokens: resolveMaxTokens(this.platform, options?.max_tokens),
+ ...(options?.reasoning_effort !== undefined ? { reasoning: options.reasoning_effort !== 'none' } : {}),
+ ...(stream && options?.stream_options ? { stream_options: options.stream_options } : {}),
+ };
+ }
+
+ private record(res: Response, endpoint: string, context?: QuotaObservationContext, modelId?: string): void {
+ recordQuotaObservationsFromResponse(res, { ...context, platform: this.platform, endpoint, modelId });
+ }
+
+ async validateKey(apiKey: string, quotaContext?: QuotaObservationContext): Promise<KeyValidationResult> {
+ // /models is public without auth, but rejects invalid Bearer keys (live
+ // verified 2026-09-22). Always send auth and reject empty keys locally.
+ if (!apiKey.trim()) return { valid: false, error: 'Moondream API key is required' };
+ const res = await this.fetchWithTimeout(`${BASE_URL}/models`, { headers: this.headers(apiKey) },
+ providerTimeoutMs(this.platform, 30_000), { timeoutBounds: 'request' });
+ this.record(res, 'models', quotaContext);
+ if ([401, 403].includes(res.status)) return this.validationResult(res);
+ if (!res.ok) throw providerHttpError(res, 'Moondream key validation is temporarily inconclusive');
+ const roster = await res.json() as { data?: unknown };
+ if (!Array.isArray(roster.data)) throw upstreamError('returned an invalid model roster');
+ return true;
+ }
+
+ private async request(apiKey: string, messages: ChatMessage[], modelId: string,
+ options: CompletionOptions | undefined, stream: boolean, quotaContext?: QuotaObservationContext): Promise<Response> {
+ const res = await this.fetchWithTimeout(`${BASE_URL}/chat/completions`, {
+ method: 'POST', headers: this.headers(apiKey), body: JSON.stringify(this.body(messages, modelId, options, stream)),
+ }, options?.timeoutMs ?? providerTimeoutMs(this.platform, 60_000),
+ { signal: options?.signal, timeoutBounds: stream ? 'headers' : 'request' });
+ this.record(res, 'chat/completions', quotaContext, modelId);
+ if (!res.ok) {
+ const text = await res.text();
+ let detail: unknown = text;
+ try { detail = JSON.parse(text); } catch { /* Preserve non-JSON backoff diagnostics. */ }
+ throw providerHttpError(res, `Moondream API error ${res.status}: ${text.slice(0, 500)}`, detail);
+ }
+ return res;
+ }
+
+ async chatCompletion(apiKey: string, messages: ChatMessage[], modelId: string,
+ options?: CompletionOptions, quotaContext?: QuotaObservationContext): Promise<ChatCompletionResponse> {
+ const res = await this.request(apiKey, messages, modelId, options, false, quotaContext);
+ const data = await res.json() as ChatCompletionResponse;
+ checkModel(modelId, data?.model);
+ if (!data.choices?.length) throw upstreamError('returned no completion choices');
+ for (const choice of data.choices) {
+ const message = choice.message as ChatMessage & { reasoning?: string };
+ if (!message || typeof message.content !== 'string' || !choice.finish_reason) throw upstreamError('returned an invalid completion');
+ if (message.reasoning) { message.reasoning_content = message.reasoning; delete message.reasoning; }
+ if (!message.content && choice.finish_reason === 'stop') throw upstreamError('returned an empty completion');
+ }
+ data._routed_via = { platform: this.platform, model: modelId };
+ return data;
+ }
+
+ async *streamChatCompletion(apiKey: string, messages: ChatMessage[], modelId: string,
+ options?: CompletionOptions, quotaContext?: QuotaObservationContext): AsyncGenerator<ChatCompletionChunk> {
+ const res = await this.request(apiKey, messages, modelId, options, true, quotaContext);
+ let finished = false;
+ for await (const chunk of this.readSseStream(res, { firstByteTimeoutMs: options?.timeoutMs ?? providerTimeoutMs(this.platform, 60_000) })) {
+ checkModel(modelId, chunk.model);
+ if (!Array.isArray(chunk.choices)) throw upstreamError('returned an invalid stream chunk');
+ for (const choice of chunk.choices) {
+ if (!choice.delta) throw upstreamError('returned a stream chunk without delta');
+ const delta = choice.delta as typeof choice.delta & { reasoning?: string };
+ if (delta.reasoning) { delta.reasoning_content = delta.reasoning; delete delta.reasoning; }
+ if (choice.finish_reason) finished = true;
+ }
+ if (!chunk.choices.length && !options?.stream_options?.include_usage) continue;
+ yield chunk;
+ }
+ if (!finished) throw upstreamError('stream ended without a completion finish reason');
+ }
+}
diff --git a/server/src/routes/keys.ts b/server/src/routes/keys.ts
index ec109ee4..60500516 100644
--- a/server/src/routes/keys.ts
+++ b/server/src/routes/keys.ts
@@ -31,6 +31,7 @@ export const keysRouter = Router();
// SambaNova was dropped in V23 (free tier permanently retired).
const PLATFORMS = [
'aclide',
+ 'moondream',
'google', 'groq', 'cerebras', 'sail', 'electronhub', 'experiential', 'router9', 'septor', 'clod', 'speechify', 'blaze', 'lucidity', 'airforce', 'dreamprompting', 'waterfall', 'logfare', 'bai', 'radeon', 'nvidia', 'mistral',
'openrouter', 'github', 'cohere', 'cloudflare', 'zhipu', 'ollama',
'kilo', 'pollinations', 'llm7', 'huggingface', 'opencode', 'ovh', 'agnes', 'reka', 'siliconflow',
diff --git a/shared/types.ts b/shared/types.ts
index 6a215430..e4bc2a86 100644
--- a/shared/types.ts
+++ b/shared/types.ts
@@ -70,6 +70,9 @@ export type Platform =
| 'sail'
// Responses-only gateway; a shared monthly free allowance, not per model.
| 'aclide'
+ // Hosted vision API: $5/workspace in recurring monthly credits, shared
+ // across models. Signed catalog only; no bundled model seeds.
+ | 'moondream'
// Hosted gateways; model rows are delivered by the signed catalog only.
// ElectronHub renews weekly credits; Experiential renews monthly credits.
| 'electronhub'