@goodandready/dsh-ollama-cloud-provider 0.1.37 → 0.1.39
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.
- package/CHANGELOG.md +21 -0
- package/README.md +13 -6
- package/README.ru.md +13 -6
- package/README.zh.md +13 -6
- package/lib/account-rules.js +26 -0
- package/lib/adapter.d.ts +92 -0
- package/lib/adapter.js +566 -0
- package/lib/analytics.d.ts +81 -0
- package/lib/analytics.js +314 -0
- package/lib/attempt.js +29 -0
- package/lib/best-effort.js +20 -0
- package/lib/catalog-cache.js +81 -0
- package/lib/catalog.d.ts +10 -0
- package/lib/catalog.js +309 -0
- package/lib/commands.d.ts +14 -0
- package/lib/commands.js +228 -0
- package/lib/config.d.ts +173 -0
- package/lib/config.js +280 -0
- package/lib/context-override.js +38 -0
- package/lib/credentials.d.ts +23 -0
- package/lib/credentials.js +105 -0
- package/lib/discovery.js +108 -0
- package/lib/endpoints.js +58 -0
- package/lib/errors.d.ts +9 -0
- package/lib/errors.js +72 -0
- package/lib/files.js +13 -0
- package/lib/images.js +78 -0
- package/lib/index.d.ts +10 -521
- package/lib/index.js +20 -5921
- package/lib/local-host.js +47 -0
- package/lib/messages.js +263 -0
- package/lib/models.generated.d.ts +26 -0
- package/lib/models.generated.js +499 -0
- package/lib/plugin-updater.js +320 -0
- package/lib/pricing.js +78 -0
- package/lib/provider-ids.d.ts +4 -0
- package/lib/provider-ids.js +9 -0
- package/lib/quota-model.js +66 -0
- package/lib/reasoning.js +71 -0
- package/lib/request-log.d.ts +38 -0
- package/lib/request-log.js +203 -0
- package/lib/retry.js +24 -0
- package/lib/route-guard.js +25 -0
- package/lib/routes-public.js +59 -0
- package/lib/routes-values.js +129 -0
- package/lib/routes.d.ts +28 -0
- package/lib/routes.js +733 -0
- package/lib/routing.js +20 -0
- package/lib/sanitize.js +43 -0
- package/lib/status.js +60 -0
- package/lib/tool-schema.js +109 -0
- package/lib/tools.js +38 -0
- package/lib/transports/native.js +156 -0
- package/lib/transports/ndjson.js +73 -0
- package/lib/transports/openai-chat.js +278 -0
- package/lib/transports/sse.js +57 -0
- package/lib/transports/tool-call-id.js +17 -0
- package/lib/tui-settings.js +246 -0
- package/lib/usage.d.ts +69 -0
- package/lib/usage.js +277 -0
- package/lib/web.js +143 -0
- package/package.json +6 -5
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { classifyStreamError } from "../errors.js";
|
|
2
|
+
import { sseData } from "./sse.js";
|
|
3
|
+
import { uniqueToolCallId } from "./tool-call-id.js";
|
|
4
|
+
import { LlmError, ToolCallId } from "@deepseek-ai/dsh-llm";
|
|
5
|
+
// src/transports/openai-chat.ts
|
|
6
|
+
function mapUsage(usage) {
|
|
7
|
+
const prompt = usage.prompt_tokens ?? 0;
|
|
8
|
+
const cached = usage.prompt_tokens_details?.cached_tokens ?? 0;
|
|
9
|
+
const output = usage.completion_tokens ?? 0;
|
|
10
|
+
const reasoning = usage.completion_tokens_details?.reasoning_tokens;
|
|
11
|
+
return {
|
|
12
|
+
inputTokens: Math.max(0, prompt - cached),
|
|
13
|
+
outputTokens: output,
|
|
14
|
+
...usage.total_tokens === void 0 ? {} : { totalTokens: usage.total_tokens },
|
|
15
|
+
...cached > 0 ? { cacheReadTokens: cached } : {},
|
|
16
|
+
...reasoning === void 0 ? {} : { reasoningTokens: reasoning }
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function finishReasonOf(raw, hasToolCalls) {
|
|
20
|
+
if (raw === "length") return { kind: "max-tokens" };
|
|
21
|
+
if (raw === "tool_calls" || raw === "function_call") return { kind: "tool-calls" };
|
|
22
|
+
return hasToolCalls ? { kind: "tool-calls" } : { kind: "stop" };
|
|
23
|
+
}
|
|
24
|
+
function reasoningOf(delta) {
|
|
25
|
+
if (typeof delta.reasoning_content === "string") return delta.reasoning_content;
|
|
26
|
+
if (typeof delta.reasoning === "string") return delta.reasoning;
|
|
27
|
+
if (typeof delta.thinking === "string") return delta.thinking;
|
|
28
|
+
return "";
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Parse an OpenAI Chat SSE stream from Ollama Cloud. The cloud translates
|
|
32
|
+
* provider thinking traces into one of `reasoning_content`, `reasoning`, or
|
|
33
|
+
* `thinking`; all three are accepted because the field name is undocumented.
|
|
34
|
+
*/
|
|
35
|
+
async function* parseChatStream(response, idleTimeoutMs, signal) {
|
|
36
|
+
if (response.body === null) throw new LlmError("Ollama Cloud returned an empty response body", "EMPTY_RESPONSE");
|
|
37
|
+
if (!(response.headers.get("content-type") ?? "").includes("text/event-stream")) {
|
|
38
|
+
yield* parseCompletion(await response.json());
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
let nextIndex = 0;
|
|
42
|
+
let textIndex;
|
|
43
|
+
let text = "";
|
|
44
|
+
let reasoningIndex;
|
|
45
|
+
let reasoning = "";
|
|
46
|
+
const toolCalls = /* @__PURE__ */ new Map();
|
|
47
|
+
let finishReason;
|
|
48
|
+
let usage;
|
|
49
|
+
let sawContent = false;
|
|
50
|
+
let terminated = false;
|
|
51
|
+
for await (const data of sseData(response.body, idleTimeoutMs, signal)) {
|
|
52
|
+
if (data === "[DONE]") {
|
|
53
|
+
terminated = true;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
let chunk;
|
|
57
|
+
try {
|
|
58
|
+
chunk = JSON.parse(data);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw new LlmError("Ollama Cloud sent an invalid SSE event", "PROVIDER_HTTP_ERROR", { cause: error });
|
|
61
|
+
}
|
|
62
|
+
const streamError = classifyStreamError(chunk);
|
|
63
|
+
if (streamError !== void 0) throw streamError;
|
|
64
|
+
if (chunk.usage !== void 0 && chunk.usage !== null) usage = mapUsage(chunk.usage);
|
|
65
|
+
for (const choice of chunk.choices ?? []) {
|
|
66
|
+
const delta = choice.delta ?? choice.message;
|
|
67
|
+
if (delta === void 0) continue;
|
|
68
|
+
const content = delta.content;
|
|
69
|
+
if (typeof content === "string" && content !== "") {
|
|
70
|
+
if (textIndex === void 0) {
|
|
71
|
+
textIndex = nextIndex++;
|
|
72
|
+
yield {
|
|
73
|
+
type: "block-start",
|
|
74
|
+
index: textIndex,
|
|
75
|
+
blockType: "text"
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
text += content;
|
|
79
|
+
sawContent = true;
|
|
80
|
+
yield {
|
|
81
|
+
type: "text-delta",
|
|
82
|
+
index: textIndex,
|
|
83
|
+
text: content
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const reasoningDelta = reasoningOf(delta);
|
|
87
|
+
if (reasoningDelta !== "") {
|
|
88
|
+
if (reasoningIndex === void 0) {
|
|
89
|
+
reasoningIndex = nextIndex++;
|
|
90
|
+
yield {
|
|
91
|
+
type: "block-start",
|
|
92
|
+
index: reasoningIndex,
|
|
93
|
+
blockType: "reasoning"
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
reasoning += reasoningDelta;
|
|
97
|
+
sawContent = true;
|
|
98
|
+
yield {
|
|
99
|
+
type: "reasoning-delta",
|
|
100
|
+
index: reasoningIndex,
|
|
101
|
+
text: reasoningDelta
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
for (const [position, toolCall] of (delta.tool_calls ?? []).entries()) {
|
|
105
|
+
const key = toolCall.index ?? position;
|
|
106
|
+
let entry = toolCalls.get(key);
|
|
107
|
+
if (entry === void 0) {
|
|
108
|
+
entry = {
|
|
109
|
+
dshIndex: nextIndex++,
|
|
110
|
+
id: uniqueToolCallId(toolCall.id),
|
|
111
|
+
name: "",
|
|
112
|
+
args: "",
|
|
113
|
+
started: false
|
|
114
|
+
};
|
|
115
|
+
toolCalls.set(key, entry);
|
|
116
|
+
}
|
|
117
|
+
if (typeof toolCall.id === "string" && toolCall.id.trim() !== "") entry.id = ToolCallId(toolCall.id.trim());
|
|
118
|
+
if (toolCall.function?.name !== void 0 && toolCall.function.name !== "") entry.name = toolCall.function.name;
|
|
119
|
+
if (!entry.started) {
|
|
120
|
+
entry.started = true;
|
|
121
|
+
yield {
|
|
122
|
+
type: "block-start",
|
|
123
|
+
index: entry.dshIndex,
|
|
124
|
+
blockType: "tool-call"
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const argumentsDelta = toolCall.function?.arguments ?? "";
|
|
128
|
+
if (argumentsDelta !== "") entry.args += argumentsDelta;
|
|
129
|
+
sawContent = true;
|
|
130
|
+
yield {
|
|
131
|
+
type: "tool-call-delta",
|
|
132
|
+
index: entry.dshIndex,
|
|
133
|
+
id: ToolCallId(entry.id),
|
|
134
|
+
...entry.name === "" ? {} : { name: entry.name },
|
|
135
|
+
argumentsDelta
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (choice.finish_reason !== void 0 && choice.finish_reason !== null) {
|
|
139
|
+
finishReason = choice.finish_reason;
|
|
140
|
+
terminated = true;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (!sawContent) throw new LlmError("Ollama Cloud completed the request without content", "EMPTY_RESPONSE");
|
|
145
|
+
if (!terminated) throw new LlmError("Ollama Cloud stream ended before [DONE] or finish_reason", "TRANSPORT");
|
|
146
|
+
if (textIndex !== void 0) yield {
|
|
147
|
+
type: "block-end",
|
|
148
|
+
index: textIndex,
|
|
149
|
+
block: {
|
|
150
|
+
type: "text",
|
|
151
|
+
text
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
if (reasoningIndex !== void 0) yield {
|
|
155
|
+
type: "block-end",
|
|
156
|
+
index: reasoningIndex,
|
|
157
|
+
block: {
|
|
158
|
+
type: "reasoning",
|
|
159
|
+
text: reasoning
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
for (const entry of toolCalls.values()) {
|
|
163
|
+
if (!entry.started) continue;
|
|
164
|
+
yield {
|
|
165
|
+
type: "block-end",
|
|
166
|
+
index: entry.dshIndex,
|
|
167
|
+
block: {
|
|
168
|
+
type: "tool-call",
|
|
169
|
+
id: ToolCallId(entry.id),
|
|
170
|
+
name: entry.name,
|
|
171
|
+
arguments: entry.args === "" ? "{}" : entry.args
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
if (usage !== void 0) yield {
|
|
176
|
+
type: "usage",
|
|
177
|
+
usage
|
|
178
|
+
};
|
|
179
|
+
yield {
|
|
180
|
+
type: "finish",
|
|
181
|
+
reason: finishReasonOf(finishReason, toolCalls.size > 0)
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/** Non-SSE fallback: one completed response object. */
|
|
185
|
+
async function* parseCompletion(chunk) {
|
|
186
|
+
const streamError = classifyStreamError(chunk);
|
|
187
|
+
if (streamError !== void 0) throw streamError;
|
|
188
|
+
const choice = chunk.choices?.[0];
|
|
189
|
+
const message = choice?.message;
|
|
190
|
+
let index = 0;
|
|
191
|
+
let sawContent = false;
|
|
192
|
+
const reasoning = message === void 0 ? "" : reasoningOf(message);
|
|
193
|
+
if (reasoning !== "") {
|
|
194
|
+
yield {
|
|
195
|
+
type: "block-start",
|
|
196
|
+
index,
|
|
197
|
+
blockType: "reasoning"
|
|
198
|
+
};
|
|
199
|
+
yield {
|
|
200
|
+
type: "reasoning-delta",
|
|
201
|
+
index,
|
|
202
|
+
text: reasoning
|
|
203
|
+
};
|
|
204
|
+
yield {
|
|
205
|
+
type: "block-end",
|
|
206
|
+
index,
|
|
207
|
+
block: {
|
|
208
|
+
type: "reasoning",
|
|
209
|
+
text: reasoning
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
index += 1;
|
|
213
|
+
sawContent = true;
|
|
214
|
+
}
|
|
215
|
+
if (typeof message?.content === "string" && message.content !== "") {
|
|
216
|
+
yield {
|
|
217
|
+
type: "block-start",
|
|
218
|
+
index,
|
|
219
|
+
blockType: "text"
|
|
220
|
+
};
|
|
221
|
+
yield {
|
|
222
|
+
type: "text-delta",
|
|
223
|
+
index,
|
|
224
|
+
text: message.content
|
|
225
|
+
};
|
|
226
|
+
yield {
|
|
227
|
+
type: "block-end",
|
|
228
|
+
index,
|
|
229
|
+
block: {
|
|
230
|
+
type: "text",
|
|
231
|
+
text: message.content
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
index += 1;
|
|
235
|
+
sawContent = true;
|
|
236
|
+
}
|
|
237
|
+
for (const call of message?.tool_calls ?? []) {
|
|
238
|
+
const id = uniqueToolCallId(call.id);
|
|
239
|
+
const name = call.function?.name ?? "";
|
|
240
|
+
const args = call.function?.arguments ?? "{}";
|
|
241
|
+
yield {
|
|
242
|
+
type: "block-start",
|
|
243
|
+
index,
|
|
244
|
+
blockType: "tool-call"
|
|
245
|
+
};
|
|
246
|
+
yield {
|
|
247
|
+
type: "tool-call-delta",
|
|
248
|
+
index,
|
|
249
|
+
id,
|
|
250
|
+
...name === "" ? {} : { name },
|
|
251
|
+
argumentsDelta: args
|
|
252
|
+
};
|
|
253
|
+
yield {
|
|
254
|
+
type: "block-end",
|
|
255
|
+
index,
|
|
256
|
+
block: {
|
|
257
|
+
type: "tool-call",
|
|
258
|
+
id,
|
|
259
|
+
name,
|
|
260
|
+
arguments: args
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
index += 1;
|
|
264
|
+
sawContent = true;
|
|
265
|
+
}
|
|
266
|
+
if (chunk.usage !== void 0 && chunk.usage !== null) yield {
|
|
267
|
+
type: "usage",
|
|
268
|
+
usage: mapUsage(chunk.usage)
|
|
269
|
+
};
|
|
270
|
+
if (!sawContent) throw new LlmError("Ollama Cloud completed the request without content", "EMPTY_RESPONSE");
|
|
271
|
+
yield {
|
|
272
|
+
type: "finish",
|
|
273
|
+
reason: finishReasonOf(choice?.finish_reason, (message?.tool_calls?.length ?? 0) > 0)
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
export { parseChatStream };
|
|
277
|
+
|
|
278
|
+
//# sourceMappingURL=openai-chat.js.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { bestEffortFailed } from "../best-effort.js";
|
|
2
|
+
import { LlmError } from "@deepseek-ai/dsh-llm";
|
|
3
|
+
// src/transports/sse.ts
|
|
4
|
+
async function readWithIdleTimeout(reader, idleTimeoutMs, signal) {
|
|
5
|
+
if (signal?.aborted === true) throw signal.reason;
|
|
6
|
+
return await new Promise((resolve, reject) => {
|
|
7
|
+
let settled = false;
|
|
8
|
+
const finish = (action) => {
|
|
9
|
+
if (settled) return;
|
|
10
|
+
settled = true;
|
|
11
|
+
clearTimeout(timer);
|
|
12
|
+
signal?.removeEventListener("abort", onAbort);
|
|
13
|
+
action();
|
|
14
|
+
};
|
|
15
|
+
const timer = setTimeout(() => {
|
|
16
|
+
reader.cancel().catch((err) => bestEffortFailed("sse:cancel", err));
|
|
17
|
+
finish(() => reject(new LlmError(`Ollama Cloud stream went idle for ${idleTimeoutMs}ms`, "TIMEOUT")));
|
|
18
|
+
}, idleTimeoutMs);
|
|
19
|
+
const onAbort = () => {
|
|
20
|
+
reader.cancel().catch((err) => bestEffortFailed("sse:cancel", err));
|
|
21
|
+
finish(() => reject(signal?.reason ?? new DOMException("aborted", "AbortError")));
|
|
22
|
+
};
|
|
23
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
24
|
+
reader.read().then((value) => finish(() => resolve(value)), (error) => finish(() => reject(error)));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/** Yield the `data:` payload of every SSE event, honoring cancellation and idle timeouts. */
|
|
28
|
+
async function* sseData(body, idleTimeoutMs, signal) {
|
|
29
|
+
const reader = body.getReader();
|
|
30
|
+
const decoder = new TextDecoder();
|
|
31
|
+
let buffer = "";
|
|
32
|
+
try {
|
|
33
|
+
for (;;) {
|
|
34
|
+
const result = await readWithIdleTimeout(reader, idleTimeoutMs, signal);
|
|
35
|
+
if (result.done) break;
|
|
36
|
+
buffer = (buffer + decoder.decode(result.value, { stream: true })).replace(/\r\n/g, "\n");
|
|
37
|
+
for (;;) {
|
|
38
|
+
const boundary = buffer.indexOf("\n\n");
|
|
39
|
+
if (boundary < 0) break;
|
|
40
|
+
const event = buffer.slice(0, boundary);
|
|
41
|
+
buffer = buffer.slice(boundary + 2);
|
|
42
|
+
const data = event.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join("\n");
|
|
43
|
+
if (data !== "") yield data;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const tail = buffer.trim();
|
|
47
|
+
if (tail !== "") {
|
|
48
|
+
const data = tail.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join("\n");
|
|
49
|
+
if (data !== "") yield data;
|
|
50
|
+
}
|
|
51
|
+
} finally {
|
|
52
|
+
reader.releaseLock();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export { sseData };
|
|
56
|
+
|
|
57
|
+
//# sourceMappingURL=sse.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { ToolCallId } from "@deepseek-ai/dsh-llm";
|
|
3
|
+
// src/transports/tool-call-id.ts
|
|
4
|
+
/**
|
|
5
|
+
* Ensures tool-call ids are globally unique across steps and turns in a session.
|
|
6
|
+
* Reusing ids breaks DSH session log validation with "PTC dispatch repeats subCallId".
|
|
7
|
+
*/
|
|
8
|
+
function uniqueToolCallId(raw) {
|
|
9
|
+
if (typeof raw === "string") {
|
|
10
|
+
const trimmed = raw.trim();
|
|
11
|
+
if (trimmed !== "") return ToolCallId(trimmed);
|
|
12
|
+
}
|
|
13
|
+
return ToolCallId(`call_${randomUUID()}`);
|
|
14
|
+
}
|
|
15
|
+
export { uniqueToolCallId };
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=tool-call-id.js.map
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { bestEffortFailed } from "./best-effort.js";
|
|
2
|
+
// src/tui-settings.ts
|
|
3
|
+
const TUI_ACCOUNT_AUTO = "auto";
|
|
4
|
+
function setOrClear(text) {
|
|
5
|
+
const trimmed = text.trim();
|
|
6
|
+
return trimmed === "" ? { kind: "clear" } : {
|
|
7
|
+
kind: "set",
|
|
8
|
+
value: trimmed
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function numberOrInvalid(text) {
|
|
12
|
+
const trimmed = text.trim();
|
|
13
|
+
if (trimmed === "") return { kind: "clear" };
|
|
14
|
+
const value = Number(trimmed);
|
|
15
|
+
return Number.isInteger(value) && value > 0 ? {
|
|
16
|
+
kind: "set",
|
|
17
|
+
value
|
|
18
|
+
} : void 0;
|
|
19
|
+
}
|
|
20
|
+
function parseModels(text) {
|
|
21
|
+
const ids = text.split(/[,\n]/).map((id) => id.trim()).filter((id) => id !== "");
|
|
22
|
+
return ids.length === 0 ? { kind: "clear" } : {
|
|
23
|
+
kind: "set",
|
|
24
|
+
value: ids
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** Build the terminal settings section without touching a TUI host. */
|
|
28
|
+
function buildOllamaTuiSection(deps) {
|
|
29
|
+
const slots = deps.accountSlots();
|
|
30
|
+
return {
|
|
31
|
+
ns: deps.ns,
|
|
32
|
+
title: deps.title ?? "Ollama Cloud",
|
|
33
|
+
groups: [
|
|
34
|
+
{
|
|
35
|
+
id: "connection",
|
|
36
|
+
title: "Connection"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: "models",
|
|
40
|
+
title: "Models"
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
id: "advanced",
|
|
44
|
+
title: "Advanced"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
id: "sidebar",
|
|
48
|
+
title: "Sidebar"
|
|
49
|
+
}
|
|
50
|
+
],
|
|
51
|
+
fields: [
|
|
52
|
+
{
|
|
53
|
+
path: ["apiKey"],
|
|
54
|
+
group: "connection",
|
|
55
|
+
kind: "text",
|
|
56
|
+
label: "API key",
|
|
57
|
+
secret: { ref: deps.apiKeyRef() },
|
|
58
|
+
hint: `Stored in the credential store as ${deps.apiKeyRef()}, never in settings.yaml.`
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
path: ["apiBase"],
|
|
62
|
+
group: "connection",
|
|
63
|
+
kind: "text",
|
|
64
|
+
label: "Native base",
|
|
65
|
+
placeholder: "https://ollama.com",
|
|
66
|
+
parse: setOrClear
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
path: ["openAiBase"],
|
|
70
|
+
group: "connection",
|
|
71
|
+
kind: "text",
|
|
72
|
+
label: "OpenAI base",
|
|
73
|
+
placeholder: "https://ollama.com/v1",
|
|
74
|
+
parse: setOrClear
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
path: ["usageUrl"],
|
|
78
|
+
group: "connection",
|
|
79
|
+
kind: "text",
|
|
80
|
+
label: "Usage endpoint",
|
|
81
|
+
placeholder: "https://ollama.com/api/usage",
|
|
82
|
+
parse: setOrClear
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
path: ["transport"],
|
|
86
|
+
group: "connection",
|
|
87
|
+
kind: "select",
|
|
88
|
+
label: "Default transport",
|
|
89
|
+
options: [{
|
|
90
|
+
value: "native",
|
|
91
|
+
label: "Native /api/chat"
|
|
92
|
+
}, {
|
|
93
|
+
value: "openai-chat",
|
|
94
|
+
label: "OpenAI /v1/chat/completions"
|
|
95
|
+
}],
|
|
96
|
+
format: (value) => String(value ?? "native"),
|
|
97
|
+
parse: (text) => ({
|
|
98
|
+
kind: "set",
|
|
99
|
+
value: text.trim() === "openai-chat" ? "openai-chat" : "native"
|
|
100
|
+
})
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
path: ["visibleModels"],
|
|
104
|
+
group: "models",
|
|
105
|
+
kind: "text",
|
|
106
|
+
label: "Visible models",
|
|
107
|
+
hint: "Comma-separated model ids. Empty shows every model.",
|
|
108
|
+
format: () => deps.visibleModels().join(", "),
|
|
109
|
+
parse: parseModels
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
path: ["stickySession"],
|
|
113
|
+
group: "advanced",
|
|
114
|
+
kind: "boolean",
|
|
115
|
+
label: "Sticky account per conversation",
|
|
116
|
+
format: (value) => String(value === void 0 ? deps.stickySession() : value !== false),
|
|
117
|
+
parse: (text) => ({
|
|
118
|
+
kind: "set",
|
|
119
|
+
value: text.trim() === "true"
|
|
120
|
+
})
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
path: ["showSidebarQuota"],
|
|
124
|
+
group: "sidebar",
|
|
125
|
+
kind: "boolean",
|
|
126
|
+
label: "Show quota card in sidebar",
|
|
127
|
+
format: (value) => String(value === void 0 ? deps.showSidebarQuota() : value === true),
|
|
128
|
+
parse: (text) => ({
|
|
129
|
+
kind: "set",
|
|
130
|
+
value: text.trim() === "true"
|
|
131
|
+
})
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
path: ["planLabel"],
|
|
135
|
+
group: "sidebar",
|
|
136
|
+
kind: "text",
|
|
137
|
+
label: "Plan label",
|
|
138
|
+
hint: "Optional label shown on the sidebar card, for example PRO.",
|
|
139
|
+
format: (value) => String(value === void 0 ? deps.planLabel() : value),
|
|
140
|
+
parse: (text) => text.trim() === "" ? { kind: "clear" } : {
|
|
141
|
+
kind: "set",
|
|
142
|
+
value: text.trim()
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
path: ["activeAccount"],
|
|
147
|
+
group: "advanced",
|
|
148
|
+
kind: "text",
|
|
149
|
+
label: "Active account",
|
|
150
|
+
hint: "A pinned account label, or auto to follow rotation order.",
|
|
151
|
+
options: [
|
|
152
|
+
{
|
|
153
|
+
value: TUI_ACCOUNT_AUTO,
|
|
154
|
+
label: "Automatic (rotation order)"
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
value: "default",
|
|
158
|
+
label: "default"
|
|
159
|
+
},
|
|
160
|
+
...slots.map((slot) => ({
|
|
161
|
+
value: slot.id,
|
|
162
|
+
label: slot.label
|
|
163
|
+
}))
|
|
164
|
+
],
|
|
165
|
+
format: () => deps.activeAccount() === "" ? TUI_ACCOUNT_AUTO : deps.activeAccount(),
|
|
166
|
+
parse: (text) => {
|
|
167
|
+
const trimmed = text.trim();
|
|
168
|
+
return trimmed === "" || trimmed === "auto" ? { kind: "clear" } : {
|
|
169
|
+
kind: "set",
|
|
170
|
+
value: trimmed
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
path: ["requestTimeoutMs"],
|
|
176
|
+
group: "advanced",
|
|
177
|
+
kind: "number",
|
|
178
|
+
label: "Header timeout (ms)",
|
|
179
|
+
parse: numberOrInvalid
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
path: ["streamIdleTimeoutMs"],
|
|
183
|
+
group: "advanced",
|
|
184
|
+
kind: "number",
|
|
185
|
+
label: "Stream idle timeout (ms)",
|
|
186
|
+
parse: numberOrInvalid
|
|
187
|
+
}
|
|
188
|
+
]
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function tuiService(ctx) {
|
|
192
|
+
let candidate;
|
|
193
|
+
try {
|
|
194
|
+
candidate = ctx.get("tuiSettingsSections");
|
|
195
|
+
} catch {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (typeof candidate !== "object" || candidate === null) return void 0;
|
|
199
|
+
const register = candidate.register;
|
|
200
|
+
if (typeof register !== "function") return void 0;
|
|
201
|
+
return { register: register.bind(candidate) };
|
|
202
|
+
}
|
|
203
|
+
/** Register the section and return a refresh function, or undefined without TUI. */
|
|
204
|
+
function applyOllamaTuiSettings(ctx, deps) {
|
|
205
|
+
const service = tuiService(ctx);
|
|
206
|
+
if (service === void 0) return void 0;
|
|
207
|
+
let disposed = false;
|
|
208
|
+
let remove;
|
|
209
|
+
const signature = () => JSON.stringify({
|
|
210
|
+
apiKeyRef: deps.apiKeyRef(),
|
|
211
|
+
accounts: deps.accountSlots().map((slot) => [slot.id, slot.label]),
|
|
212
|
+
visibleModels: deps.visibleModels(),
|
|
213
|
+
activeAccount: deps.activeAccount(),
|
|
214
|
+
stickySession: deps.stickySession(),
|
|
215
|
+
showSidebarQuota: deps.showSidebarQuota(),
|
|
216
|
+
planLabel: deps.planLabel()
|
|
217
|
+
});
|
|
218
|
+
let lastSignature = signature();
|
|
219
|
+
const refresh = () => {
|
|
220
|
+
if (disposed) return;
|
|
221
|
+
remove?.();
|
|
222
|
+
try {
|
|
223
|
+
remove = service.register(buildOllamaTuiSection(deps));
|
|
224
|
+
} catch (error) {
|
|
225
|
+
remove = void 0;
|
|
226
|
+
bestEffortFailed("tui section registration failed", error);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
refresh();
|
|
230
|
+
const timer = setInterval(() => {
|
|
231
|
+
const next = signature();
|
|
232
|
+
if (next === lastSignature) return;
|
|
233
|
+
lastSignature = next;
|
|
234
|
+
refresh();
|
|
235
|
+
}, deps.refreshMs ?? 2e3);
|
|
236
|
+
ctx.effect(() => () => {
|
|
237
|
+
disposed = true;
|
|
238
|
+
clearInterval(timer);
|
|
239
|
+
remove?.();
|
|
240
|
+
remove = void 0;
|
|
241
|
+
}, "dsh-ollama-cloud-provider: tui settings section");
|
|
242
|
+
return refresh;
|
|
243
|
+
}
|
|
244
|
+
export { TUI_ACCOUNT_AUTO, applyOllamaTuiSettings, buildOllamaTuiSection };
|
|
245
|
+
|
|
246
|
+
//# sourceMappingURL=tui-settings.js.map
|
package/lib/usage.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { ResolvedConfig } from "./config.js";
|
|
2
|
+
import { KeyResolver } from "./credentials.js";
|
|
3
|
+
type UsageRange = '24h' | '7d' | '30d';
|
|
4
|
+
interface UsageTotals {
|
|
5
|
+
requests: number;
|
|
6
|
+
usd: number;
|
|
7
|
+
inputTokens: number;
|
|
8
|
+
cachedInputTokens: number;
|
|
9
|
+
outputTokens: number;
|
|
10
|
+
}
|
|
11
|
+
interface UsageWindow {
|
|
12
|
+
percentUsed: number;
|
|
13
|
+
resetsAt?: string;
|
|
14
|
+
status?: string;
|
|
15
|
+
}
|
|
16
|
+
interface CreditBalance {
|
|
17
|
+
includedRemaining: number;
|
|
18
|
+
includedAllowance: number;
|
|
19
|
+
purchased: number;
|
|
20
|
+
periodFrom?: string;
|
|
21
|
+
periodUntil?: string;
|
|
22
|
+
}
|
|
23
|
+
interface UsageReport {
|
|
24
|
+
kind: 'credits' | 'legacy';
|
|
25
|
+
credits?: CreditBalance;
|
|
26
|
+
legacy?: {
|
|
27
|
+
fiveHour?: UsageWindow;
|
|
28
|
+
weekly?: UsageWindow;
|
|
29
|
+
};
|
|
30
|
+
windows: {
|
|
31
|
+
d24?: UsageTotals;
|
|
32
|
+
d7?: UsageTotals;
|
|
33
|
+
d30?: UsageTotals;
|
|
34
|
+
session?: UsageTotals;
|
|
35
|
+
weekly?: UsageTotals;
|
|
36
|
+
monthly?: UsageTotals;
|
|
37
|
+
};
|
|
38
|
+
byModel: Array<{
|
|
39
|
+
model: string;
|
|
40
|
+
} & UsageTotals>;
|
|
41
|
+
byWindow?: Partial<Record<UsageRange, Array<{
|
|
42
|
+
model: string;
|
|
43
|
+
} & UsageTotals>>>;
|
|
44
|
+
range: UsageRange;
|
|
45
|
+
}
|
|
46
|
+
interface UsageResult {
|
|
47
|
+
report?: UsageReport;
|
|
48
|
+
error?: string;
|
|
49
|
+
checkedAt: number;
|
|
50
|
+
stale?: boolean;
|
|
51
|
+
}
|
|
52
|
+
interface UsageReader {
|
|
53
|
+
/** Balance plus the 7-day spend window for one key. */
|
|
54
|
+
read(key?: string): Promise<UsageResult | undefined>;
|
|
55
|
+
/** One spend window; the plugin caches each range separately. */
|
|
56
|
+
usage(range: UsageRange, key?: string): Promise<UsageResult | undefined>;
|
|
57
|
+
clear(): void;
|
|
58
|
+
}
|
|
59
|
+
declare function normalizeBalance(payload: unknown): Pick<UsageReport, 'kind' | 'credits' | 'legacy'>;
|
|
60
|
+
declare function normalizeUsage(payload: unknown, range: UsageRange): {
|
|
61
|
+
totals: UsageTotals;
|
|
62
|
+
byModel: Array<{
|
|
63
|
+
model: string;
|
|
64
|
+
} & UsageTotals>;
|
|
65
|
+
};
|
|
66
|
+
/** Cached reader for Ollama Cloud balance and usage. Fail-open by design. */
|
|
67
|
+
declare function createUsageReader(config: () => ResolvedConfig, resolveKey: KeyResolver, fetchImpl?: typeof fetch): UsageReader;
|
|
68
|
+
export { CreditBalance, UsageRange, UsageReader, UsageReport, UsageResult, UsageTotals, UsageWindow, createUsageReader, normalizeBalance, normalizeUsage };
|
|
69
|
+
//# sourceMappingURL=usage.d.ts.map
|