omnitrix 0.0.0-stage → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ // Ollama's own API knows what each local model can do. The OpenAI-style model list does not say.
2
+ import { makeModel } from '../models.js';
3
+ import { getJson, postJson } from './http.js';
4
+ /** The given models with capabilities, size and version from Ollama; a model it cannot describe gets plain local scores. */
5
+ export async function localModels(baseUrl, ids) {
6
+ const root = baseUrl.replace(/\/+$/, '').replace(/\/v1$/, '');
7
+ let tags = [];
8
+ try {
9
+ const list = await getJson(`${root}/api/tags`, {}, 5000);
10
+ if (Array.isArray(list?.models))
11
+ tags = list.models;
12
+ }
13
+ catch {
14
+ // not an Ollama server, or it is not running
15
+ }
16
+ const byName = new Map(tags.filter((t) => typeof t.name === 'string').map((t) => [t.name, t]));
17
+ return Promise.all(ids.map(async (id) => {
18
+ const tag = byName.get(id);
19
+ if (!tag)
20
+ return makeModel(id, {}, { local: true });
21
+ let capabilities;
22
+ try {
23
+ const shown = (await postJson(`${root}/api/show`, {}, { model: id }, 10_000)).json;
24
+ if (Array.isArray(shown?.capabilities))
25
+ capabilities = shown.capabilities.filter((c) => typeof c === 'string');
26
+ }
27
+ catch {
28
+ // older Ollama versions do not report capabilities; vision then stays unclaimed
29
+ }
30
+ const digest = typeof tag.digest === 'string' && /^[0-9a-f]{12,}$/i.test(tag.digest) ? tag.digest : undefined;
31
+ return makeModel(id, digest ? { digest } : {}, { local: true, capabilities, parameterSize: tag.details?.parameter_size });
32
+ }));
33
+ }
@@ -0,0 +1,64 @@
1
+ import { resolveKey } from '../store.js';
2
+ import { DriverError } from '../types.js';
3
+ import { getJson, joinUrl, postJson, readRateHeaders } from './http.js';
4
+ function authHeaders(account) {
5
+ const key = resolveKey(account);
6
+ if (!key && !account.local)
7
+ throw new DriverError(`no API key for ${account.id} (${account.apiKey?.startsWith('env:') ? `set ${account.apiKey.slice(4)}` : account.apiKey === 'secret' ? 'no saved key' : 'not set'})`, 'auth');
8
+ const h = key ? { authorization: `Bearer ${key}` } : {};
9
+ if (account.provider === 'openrouter') {
10
+ h['x-title'] = 'omnitrix';
11
+ }
12
+ return h;
13
+ }
14
+ /**
15
+ * The answer without a thinking block that some models put before it, such as Gemma 4's
16
+ * "<thought>...</thought>" or DeepSeek R1's "<think>...</think>". Only a closed block at the very start
17
+ * goes; a block later on is part of the answer, and an answer that is only thinking is kept whole.
18
+ */
19
+ export function withoutThinking(text) {
20
+ const m = /^\s*<(think|thought|thinking)>[\s\S]*?<\/\1>\s*/i.exec(text);
21
+ return m && m[0].length < text.length ? text.slice(m[0].length) : text;
22
+ }
23
+ /** Chat Completions: the format almost every provider and local runtime speaks. */
24
+ export async function runOpenAI(req) {
25
+ const { account } = req;
26
+ if (!account.baseUrl)
27
+ throw new DriverError(`account ${account.id} has no base URL`, 'bad_request');
28
+ const messages = [];
29
+ if (req.system)
30
+ messages.push({ role: 'system', content: req.system });
31
+ messages.push({
32
+ role: 'user',
33
+ content: req.images?.length
34
+ ? [{ type: 'text', text: req.prompt }, ...req.images.map((i) => ({ type: 'image_url', image_url: { url: `data:${i.mime};base64,${i.data}` } }))]
35
+ : req.prompt,
36
+ });
37
+ const body = { model: req.model, messages, stream: false };
38
+ if (req.maxOutputTokens)
39
+ body.max_tokens = req.maxOutputTokens;
40
+ // OpenAI, Gemini and other compatible APIs read this; it is sent only when a task asks for it
41
+ if (req.effort)
42
+ body.reasoning_effort = req.effort;
43
+ const { json, headers } = await postJson(joinUrl(account.baseUrl, 'chat/completions'), authHeaders(account), body, req.timeoutMs, req.signal);
44
+ const choice = json?.choices?.[0];
45
+ const content = choice?.message?.content;
46
+ const text = withoutThinking(typeof content === 'string' ? content : Array.isArray(content) ? content.map((p) => p?.text ?? '').join('') : '');
47
+ if (!text && !choice)
48
+ throw new DriverError(`unexpected response: ${JSON.stringify(json).slice(0, 200)}`, 'server');
49
+ const usage = json?.usage ?? {};
50
+ return {
51
+ text,
52
+ inputTokens: Number(usage.prompt_tokens ?? usage.input_tokens ?? 0),
53
+ outputTokens: Number(usage.completion_tokens ?? usage.output_tokens ?? 0),
54
+ costUsd: typeof usage.cost === 'number' ? usage.cost : undefined, // OpenRouter reports this
55
+ rate: readRateHeaders(headers),
56
+ };
57
+ }
58
+ export async function listOpenAIModels(account) {
59
+ if (!account.baseUrl)
60
+ return [];
61
+ const json = await getJson(joinUrl(account.baseUrl, 'models'), authHeaders(account));
62
+ const list = Array.isArray(json?.data) ? json.data : Array.isArray(json?.models) ? json.models : [];
63
+ return list.map((m) => String(m.id ?? m.name ?? '')).filter(Boolean);
64
+ }