@kabmo-labs/kabmo 0.24.0 → 0.25.0
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/dist/client/index.html +1 -1
- package/dist/client/index.js +579 -510
- package/dist/client/static/js/index-bc9aa4mx.css +1 -0
- package/dist/client/static/js/{index-jcr5twtp.js → index-ga8cbpt4.js} +1072 -1174
- package/package.json +2 -4
- package/src/cli/lib/args.js +24 -1
- package/src/cli/lib/args.test.js +16 -0
- package/src/cli/lib/commands/session.js +233 -0
- package/src/cli/lib/commands/session.test.js +103 -0
- package/src/cli/main.js +1 -1
- package/src/client/components/common/ComposerPicker.tsx +3 -0
- package/src/client/components/common/code-surface/shiki.test.ts +82 -0
- package/src/client/components/common/code-surface/shiki.ts +28 -7
- package/src/client/components/common/session-actions-menu/NewChildSessionDialog.tsx +113 -0
- package/src/client/components/common/session-actions-menu/index.tsx +33 -2
- package/src/client/components/common/session-row/StatusSlot.tsx +4 -4
- package/src/client/components/common/session-row/index.tsx +27 -1
- package/src/client/components/layout/desktop-layout/resizer/separator-drag.ts +3 -3
- package/src/client/components/layout/session-sidebar/CategoryItem.tsx +25 -12
- package/src/client/components/layout/session-sidebar/ChildSessionList.tsx +66 -0
- package/src/client/components/layout/session-sidebar/SessionItem.tsx +28 -3
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +36 -5
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTableLog.test.tsx +94 -0
- package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.test.tsx +130 -0
- package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.tsx +120 -0
- package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +47 -0
- package/src/client/components/layout/session-sidebar/about-modal/index.tsx +28 -35
- package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +18 -5
- package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +21 -4
- package/src/client/components/mobile/mobile-session-sidebar/SessionRow.tsx +27 -5
- package/src/client/components/workspace/chat-timeline/ComposerDock.tsx +64 -70
- package/src/client/components/workspace/chat-timeline/ComposerDockFrame.tsx +35 -0
- package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +135 -118
- package/src/client/components/workspace/chat-timeline/MessageItem.tsx +22 -1
- package/src/client/components/workspace/chat-timeline/MessageList.tsx +4 -0
- package/src/client/components/workspace/chat-timeline/TimelineBody.tsx +3 -0
- package/src/client/components/workspace/chat-timeline/composer-dock-frame.test.tsx +155 -0
- package/src/client/components/workspace/chat-timeline/index.tsx +3 -1
- package/src/client/components/workspace/terminal-panel/KeyBar.tsx +10 -8
- package/src/client/components/workspace/terminal-panel/{RealtimeXtermView.tsx → RealtimeTerminalView.tsx} +51 -58
- package/src/client/components/workspace/terminal-panel/fit.test.ts +37 -0
- package/src/client/components/workspace/terminal-panel/fit.ts +64 -0
- package/src/client/components/workspace/terminal-panel/fonts.test.ts +161 -0
- package/src/client/components/workspace/terminal-panel/fonts.ts +164 -0
- package/src/client/components/workspace/terminal-panel/grid.test.ts +52 -0
- package/src/client/components/workspace/terminal-panel/grid.ts +118 -0
- package/src/client/components/workspace/terminal-panel/index.tsx +25 -21
- package/src/client/components/workspace/terminal-panel/mount.ts +247 -223
- package/src/client/components/workspace/terminal-panel/navigation.test.ts +54 -0
- package/src/client/components/workspace/terminal-panel/navigation.ts +47 -0
- package/src/client/data/settings/omp-schema.json +1 -1
- package/src/client/data/theme/terminal.test.ts +25 -0
- package/src/client/data/theme/terminal.ts +96 -79
- package/src/client/hooks/chat/composer/items.test.ts +12 -26
- package/src/client/hooks/chat/composer/pick-mapping.test.ts +42 -0
- package/src/client/hooks/chat/omp/prompt-send.test.ts +7 -7
- package/src/client/hooks/chat/omp/prompt-send.ts +26 -14
- package/src/client/hooks/chat/timeline/prompt-text.test.ts +34 -1
- package/src/client/hooks/chat/timeline/prompt-text.ts +47 -20
- package/src/client/hooks/chat/timeline/send-execute.test.ts +39 -0
- package/src/client/hooks/chat/timeline/send-helpers.ts +30 -1
- package/src/client/hooks/chat/timeline/send-steer.test.ts +3 -1
- package/src/client/hooks/chat/timeline/send.test-util.ts +7 -1
- package/src/client/hooks/chat/timeline/send.test.ts +3 -1
- package/src/client/hooks/chat/timeline/send.ts +51 -50
- package/src/client/hooks/chat/timeline/use-fork-session.ts +49 -0
- package/src/client/hooks/models/use-harness-update.test.ts +193 -0
- package/src/client/hooks/models/use-harness-update.ts +202 -0
- package/src/client/hooks/workspace/child-expansion.ts +33 -0
- package/src/client/hooks/workspace/terminal/index.ts +17 -3
- package/src/client/hooks/workspace/terminal/socket.ts +9 -4
- package/src/client/lib/session-tree.test.ts +50 -0
- package/src/client/lib/session-tree.ts +59 -0
- package/src/client/tailwind.css +17 -11
- package/src/server/index.ts +2 -0
- package/src/server/lib/auth/guard.ts +10 -0
- package/src/server/lib/btw/session-copy.server.ts +6 -44
- package/src/server/lib/btw/session-copy.test.ts +2 -1
- package/src/server/lib/db/schema.ts +4 -0
- package/src/server/lib/db/seed-terminal.test.ts +1 -1
- package/src/server/lib/db/seed.test.ts +1 -1
- package/src/server/lib/harness/acp/background-work.test.ts +262 -0
- package/src/server/lib/harness/acp/background-work.ts +320 -0
- package/src/server/lib/harness/acp/client.ts +2 -2
- package/src/server/lib/harness/acp/gateway-env.test.ts +64 -0
- package/src/server/lib/harness/acp/gateway-env.ts +70 -0
- package/src/server/lib/harness/acp/prompt.test.ts +20 -0
- package/src/server/lib/harness/acp/prompt.ts +19 -3
- package/src/server/lib/harness/acp/session-background.test.ts +129 -0
- package/src/server/lib/harness/acp/session-exit.ts +31 -0
- package/src/server/lib/harness/acp/session-handshake.test.ts +108 -0
- package/src/server/lib/harness/acp/session-handshake.ts +27 -1
- package/src/server/lib/harness/acp/session-listeners.ts +31 -0
- package/src/server/lib/harness/acp/session-requests.ts +47 -0
- package/src/server/lib/harness/acp/session-turns.ts +20 -5
- package/src/server/lib/harness/acp/session.test-util.ts +23 -1
- package/src/server/lib/harness/acp/session.ts +59 -59
- package/src/server/lib/harness/acp/turn.test.ts +81 -0
- package/src/server/lib/harness/acp/turn.ts +26 -9
- package/src/server/lib/harness/acp/verbs.ts +12 -2
- package/src/server/lib/harness/launch.ts +3 -1
- package/src/server/lib/harness/spawn.server.ts +15 -2
- package/src/server/lib/harness/update-job-store.test.ts +126 -0
- package/src/server/lib/harness/update-job-store.ts +109 -0
- package/src/server/lib/harness/update-job.server.ts +203 -0
- package/src/server/lib/harness/update-worker.test.ts +153 -0
- package/src/server/lib/harness/update-worker.ts +197 -0
- package/src/server/lib/lifecycle/paths.ts +23 -0
- package/src/server/lib/omp/rpc/manager.ts +5 -5
- package/src/server/lib/omp/rpc/prewarm-pool.ts +12 -3
- package/src/server/lib/omp/rpc/session-registry.ts +14 -2
- package/src/server/lib/omp/rpc/terminal-settle.ts +53 -5
- package/src/server/lib/omp/session/delete-flow.server.ts +47 -0
- package/src/server/lib/omp/session/delete.server.ts +5 -0
- package/src/server/lib/omp/session/rewind-file.ts +39 -21
- package/src/server/lib/omp/session/sidebar-data.server.ts +6 -6
- package/src/server/lib/omp/session/transcript-copy.ts +123 -0
- package/src/server/lib/sessions/agent-token.server.test.ts +64 -0
- package/src/server/lib/sessions/agent-token.server.ts +134 -0
- package/src/server/lib/sessions/answers.server.test.ts +33 -0
- package/src/server/lib/sessions/answers.server.ts +37 -0
- package/src/server/lib/sessions/dispatch.server.test.ts +143 -0
- package/src/server/lib/sessions/dispatch.server.ts +128 -0
- package/src/server/lib/sessions/fork.server.test.ts +209 -0
- package/src/server/lib/sessions/fork.server.ts +304 -0
- package/src/server/lib/sessions/lifecycle.server.test.ts +75 -0
- package/src/server/lib/sessions/lifecycle.server.ts +71 -0
- package/src/server/lib/sessions/notify.server.test.ts +112 -0
- package/src/server/lib/sessions/notify.server.ts +212 -0
- package/src/server/lib/sessions/relation-types.ts +76 -0
- package/src/server/lib/sessions/relations.server.test.ts +207 -0
- package/src/server/lib/sessions/relations.server.ts +337 -0
- package/src/server/lib/sessions/resolve.server.ts +106 -0
- package/src/server/lib/sessions/schema.server.ts +82 -0
- package/src/server/lib/sessions/sidebar-relations.server.test.ts +61 -0
- package/src/server/lib/sessions/sidebar-relations.server.ts +47 -0
- package/src/server/lib/sessions/spawn.server.test.ts +196 -0
- package/src/server/lib/sessions/spawn.server.ts +310 -0
- package/src/server/lib/terminal/attach-replay.test.ts +2 -2
- package/src/server/lib/terminal/output.test.ts +2 -2
- package/src/server/lib/terminal/output.ts +1 -1
- package/src/server/lib/terminal/resize.test.ts +99 -0
- package/src/server/lib/terminal/runtime.server.ts +26 -6
- package/src/server/lib/terminal/send-result.test.ts +18 -1
- package/src/server/lib/terminal/send-result.ts +26 -2
- package/src/server/lib/terminal/session.server.ts +6 -6
- package/src/server/lib/usage/new-api.test.ts +92 -0
- package/src/server/lib/usage/new-api.ts +131 -0
- package/src/server/lib/usage/provider-endpoint.test.ts +106 -0
- package/src/server/lib/usage/provider-key.ts +33 -0
- package/src/server/lib/usage/providers.server.ts +10 -3
- package/src/server/lib/usage/vendor-balance.server.ts +4 -53
- package/src/server/lib/usage/vendor-http.ts +41 -0
- package/src/server/lib/usage/window.ts +55 -0
- package/src/server/routes/agent/command.ts +14 -1
- package/src/server/routes/agent/harness-command.ts +6 -0
- package/src/server/routes/agent/new.ts +15 -0
- package/src/server/routes/harness/add-provider.test.ts +9 -1
- package/src/server/routes/harness/import-action.ts +4 -0
- package/src/server/routes/harness/import.test.ts +1 -1
- package/src/server/routes/harness/index.ts +5 -1
- package/src/server/routes/harness/update-status.ts +26 -0
- package/src/server/routes/harness/update.test.ts +119 -0
- package/src/server/routes/harness/update.ts +59 -147
- package/src/server/routes/sessions/answer.ts +24 -0
- package/src/server/routes/sessions/batch-delete.ts +11 -5
- package/src/server/routes/sessions/delete.ts +4 -2
- package/src/server/routes/sessions/fork.ts +74 -0
- package/src/server/routes/sessions/index.ts +14 -0
- package/src/server/routes/sessions/relations.ts +85 -0
- package/src/server/routes/sessions/session.ts +4 -0
- package/src/server/routes/sessions/spawn.ts +134 -0
- package/src/server/routes/terminal/ws.ts +3 -3
- package/src/shared/lib/chat/composer/client.ts +31 -2
- package/src/shared/lib/chat/composer/delegation.test.ts +93 -0
- package/src/shared/lib/chat/composer/delegation.ts +206 -0
- package/src/shared/lib/chat/notice-text.ts +1 -1
- package/src/shared/lib/chat/tool/summary-wait.test.ts +11 -0
- package/src/shared/lib/code/ansi.ts +1 -1
- package/src/shared/lib/fonts/nerd-symbols.css +4 -3
- package/src/shared/lib/fonts/nerd-symbols.test.ts +9 -9
- package/src/shared/lib/harness/provider-facts.test.ts +27 -0
- package/src/shared/lib/harness/providers.ts +35 -1
- package/src/shared/lib/harness/update-job.ts +33 -0
- package/src/shared/lib/harness/update-plan.test.ts +21 -6
- package/src/shared/lib/harness/update-plan.ts +12 -4
- package/src/shared/lib/omp/session/wait-idle.test.ts +11 -0
- package/src/shared/lib/omp/session/wait-idle.ts +12 -2
- package/src/shared/lib/theme/catalog.test.ts +13 -9
- package/src/shared/lib/theme/color.test.ts +2 -2
- package/src/shared/lib/theme/color.ts +4 -4
- package/src/shared/lib/theme/theme.test.ts +1 -1
- package/src/shared/lib/workspace/terminal/input.test.ts +27 -0
- package/src/shared/lib/workspace/terminal/input.ts +33 -0
- package/src/shared/lib/workspace/terminal/keys.test.ts +1 -1
- package/src/shared/lib/workspace/terminal/keys.ts +7 -7
- package/src/shared/types/chat.ts +1 -1
- package/src/shared/types/harness-update.ts +65 -0
- package/src/shared/types/omp/agent.ts +19 -2
- package/src/shared/types/workspace.ts +4 -0
- package/src/test-support/fake-omp-fork.ts +16 -2
- package/src/test-support/harness-table.test-util.ts +15 -1
- package/dist/client/static/js/index-z3w7n85q.css +0 -1
- package/src/client/components/workspace/terminal-panel/webgl.test.ts +0 -75
- package/src/client/components/workspace/terminal-panel/webgl.ts +0 -108
|
@@ -55,10 +55,20 @@ const resizeTimers = registry.resizeTimers;
|
|
|
55
55
|
export async function attachTerminal(viewer: TerminalViewer, request: AttachRequest): Promise<TerminalAttachment> {
|
|
56
56
|
const session = await openTerminal(request);
|
|
57
57
|
|
|
58
|
+
// The attach states the viewer's grid, and that has to be the PTY's size: a
|
|
59
|
+
// shell only learns a size from `resize`, and a page that just fitted to the
|
|
60
|
+
// grid the session already had sends no resize frame at all. Without this, a
|
|
61
|
+
// shell left at a squeezed size by an earlier mount (the panel opens with a
|
|
62
|
+
// width transition; measured `resize 5x37` while it was still animating) kept
|
|
63
|
+
// drawing every later prompt hard-wrapped, and that garbage is replayed on
|
|
64
|
+
// every reconnect. Equal sizes early-return in `resizeTerminal`, so the
|
|
65
|
+
// common attach sends no signal.
|
|
66
|
+
resizeTerminal(session.id, request.cols, request.rows);
|
|
67
|
+
|
|
58
68
|
// Pending bytes are already in history, so they have to leave the coalescer
|
|
59
69
|
// BEFORE the replay is captured: delivered after, the viewer would receive
|
|
60
70
|
// them twice; captured first, they would be missing from the replay and then
|
|
61
|
-
// arrive out of order. Either way
|
|
71
|
+
// arrive out of order. Either way the terminal shows duplicated or shuffled lines.
|
|
62
72
|
session.output.flush();
|
|
63
73
|
const replay = session.history.replay();
|
|
64
74
|
const snapshot = snapshotOf(session);
|
|
@@ -170,6 +180,16 @@ export function resizeTerminal(id: string, cols: number, rows: number): void {
|
|
|
170
180
|
if (size.cols === session.cols && size.rows === session.rows) return;
|
|
171
181
|
session.cols = size.cols;
|
|
172
182
|
session.rows = size.rows;
|
|
183
|
+
// The replay grid follows the PTY, it does not stay at the spawn size.
|
|
184
|
+
//
|
|
185
|
+
// The retained history is the shell's screen stream, and after a resize the
|
|
186
|
+
// shell's screen IS this grid: replaying at the size the session was created
|
|
187
|
+
// with re-wraps every line written since into the old width. Measured after a
|
|
188
|
+
// panel resize: each reload laid the history out at the old 40×31 and then
|
|
189
|
+
// fitted to 41×47, so the transcript shifted and the shell redrew its prompt
|
|
190
|
+
// on the resize that followed — one more block of history per reload.
|
|
191
|
+
session.replayCols = size.cols;
|
|
192
|
+
session.replayRows = size.rows;
|
|
173
193
|
session.lastActivity = Date.now();
|
|
174
194
|
try {
|
|
175
195
|
session.term.resize(size.cols, size.rows);
|
|
@@ -184,11 +204,11 @@ export function resizeTerminal(id: string, cols: number, rows: number): void {
|
|
|
184
204
|
/**
|
|
185
205
|
* Debounce for `resizeTerminal`.
|
|
186
206
|
*
|
|
187
|
-
* A panel drag produces a burst of sizes — the pointer moves in steps and
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
207
|
+
* A panel drag produces a burst of sizes — the pointer moves in steps and the
|
|
208
|
+
* host's ResizeObserver reports most of them — and each accepted size sends
|
|
209
|
+
* SIGWINCH. The shell answers every one of them by redrawing its prompt, so a
|
|
210
|
+
* single drag used to print the prompt a dozen times (measured: 10 SIGWINCH in
|
|
211
|
+
* one second for one drag).
|
|
192
212
|
*
|
|
193
213
|
* Only the trailing size matters: the PTY ends at the final grid either way,
|
|
194
214
|
* and a repaint of an intermediate size is invisible work. A single timeout is
|
|
@@ -11,7 +11,24 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import { describe, expect, test } from 'bun:test';
|
|
14
|
-
import { frameAccepted } from '@/server/lib/terminal/send-result';
|
|
14
|
+
import { asBinaryFrame, frameAccepted } from '@/server/lib/terminal/send-result';
|
|
15
|
+
|
|
16
|
+
describe('asBinaryFrame', () => {
|
|
17
|
+
test('a Uint8Array becomes a Buffer VIEW over the same bytes', () => {
|
|
18
|
+
const source = new Uint8Array([0x1b, 0x5b, 0x4d, 0x20, 0x20, 0x20]);
|
|
19
|
+
const frame = asBinaryFrame(source);
|
|
20
|
+
expect(Buffer.isBuffer(frame)).toBe(true);
|
|
21
|
+
expect([...(frame as Buffer)]).toEqual([...source]);
|
|
22
|
+
// Elysia only keeps bytes binary for Buffer; a copy would be correct but
|
|
23
|
+
// would double the cost of every PTY frame, so this pins the view.
|
|
24
|
+
(frame as Buffer)[0] = 0x41;
|
|
25
|
+
expect(source[0]).toBe(0x41);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('a string stays text', () => {
|
|
29
|
+
expect(asBinaryFrame('{"t":"ready"}')).toBe('{"t":"ready"}');
|
|
30
|
+
});
|
|
31
|
+
});
|
|
15
32
|
|
|
16
33
|
describe('frameAccepted', () => {
|
|
17
34
|
test('bytes queued is a delivery', () => {
|
|
@@ -18,15 +18,39 @@
|
|
|
18
18
|
* more than `backpressureLimit` (16 MiB by default). The bytes are gone.
|
|
19
19
|
*
|
|
20
20
|
* Reading `-1` as failure closes a healthy viewer at the first burst; reading
|
|
21
|
-
* `0` as success is worse, because the client keeps
|
|
22
|
-
* with a hole in it — exactly the mid-sequence corruption the drop rule exists
|
|
21
|
+
* `0` as success is worse, because the client keeps the terminal attached to a
|
|
22
|
+
* stream with a hole in it — exactly the mid-sequence corruption the drop rule exists
|
|
23
23
|
* to prevent. Measured against Bun 1.4.4, 20,000 frames of 4 KiB: the old
|
|
24
24
|
* `!== -1` rule stopped at frame 160 (655 KiB, 0.8 %) while the socket was
|
|
25
25
|
* still accepting, and a 40,000 × 1 KiB burst returned `-1` 17,870 times and
|
|
26
26
|
* `0` 20,851 times.
|
|
27
27
|
*
|
|
28
28
|
* Never call this with an empty frame: `send('')` succeeds but reports `0`.
|
|
29
|
+
*
|
|
30
|
+
* The module also owns `asBinaryFrame`, the other half of handing a frame to
|
|
31
|
+
* Bun's socket: Elysia only keeps bytes binary when they arrive as a Buffer.
|
|
29
32
|
*/
|
|
30
33
|
export function frameAccepted(result: number): boolean {
|
|
31
34
|
return result !== 0;
|
|
32
35
|
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Hand raw terminal bytes to Elysia as a Buffer.
|
|
39
|
+
*
|
|
40
|
+
* Elysia's `ws.send` JSON-stringifies every object that is not a Buffer
|
|
41
|
+
* (`node_modules/elysia/dist/ws/index.js`: `Buffer.isBuffer(data) ? raw.send(data)
|
|
42
|
+
* : … typeof data == "object" && (data = JSON.stringify(data))`). A
|
|
43
|
+
* `Uint8Array` therefore reaches the browser as TEXT — measured: the shell's
|
|
44
|
+
* `echo` arrived as `{"0":101,"1":99,…}` and the client dropped it as an
|
|
45
|
+
* unknown control frame. That is a correctness bug first, and a throughput bug
|
|
46
|
+
* second: every output byte expands to ~4 JSON bytes and the client must parse
|
|
47
|
+
* them back.
|
|
48
|
+
*
|
|
49
|
+
* `Buffer.from(arrayBuffer, offset, length)` is a VIEW over the same memory, so
|
|
50
|
+
* this does not copy a PTY frame. No cast is needed: Buffer is a Uint8Array,
|
|
51
|
+
* and `Buffer.isBuffer` is what selects Elysia's binary branch.
|
|
52
|
+
*/
|
|
53
|
+
export function asBinaryFrame(frame: string | Uint8Array): string | Buffer {
|
|
54
|
+
if (typeof frame === 'string') return frame;
|
|
55
|
+
return Buffer.isBuffer(frame) ? frame : Buffer.from(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
56
|
+
}
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
* SIGWINCH itself.
|
|
29
29
|
*
|
|
30
30
|
* A fourth: a PTY read may split a multi-byte character, so bytes are forwarded
|
|
31
|
-
* verbatim and
|
|
31
|
+
* verbatim and the renderer's own streaming decoder reassembles them.
|
|
32
32
|
*
|
|
33
33
|
* A fifth, and the reason `output.ts` exists: **reads are coalesced into frames,
|
|
34
34
|
* never forwarded one-per-read**. One read is one line for a line-oriented
|
|
@@ -82,7 +82,7 @@ export interface TerminalSession {
|
|
|
82
82
|
viewers: Set<TerminalViewer>;
|
|
83
83
|
cols: number;
|
|
84
84
|
rows: number;
|
|
85
|
-
/** Grid the retained scrollback
|
|
85
|
+
/** Grid the retained scrollback is replayed at: the PTY's current size. */
|
|
86
86
|
replayCols: number;
|
|
87
87
|
replayRows: number;
|
|
88
88
|
status: 'running' | 'exited';
|
|
@@ -96,8 +96,8 @@ export interface TerminalSession {
|
|
|
96
96
|
*
|
|
97
97
|
* A viewer reports false only for a real drop (`send-result.ts` owns that
|
|
98
98
|
* contract: Bun's `-1` means "queued, draining" and `0` means "dropped"). A
|
|
99
|
-
* dropped viewer is closed rather than skipped — skipping would leave
|
|
100
|
-
* parser mid-sequence, so the honest outcome is a reconnect that replays the
|
|
99
|
+
* dropped viewer is closed rather than skipped — skipping would leave the
|
|
100
|
+
* renderer's parser mid-sequence, so the honest outcome is a reconnect that replays the
|
|
101
101
|
* scrollback. The PTY cannot be paused, so this is the only backpressure a
|
|
102
102
|
* terminal has; coalescing in `output.ts` is what keeps it from firing on a
|
|
103
103
|
* producer that simply emits many small reads.
|
|
@@ -201,8 +201,8 @@ export async function createSession(request: AttachRequest): Promise<TerminalSes
|
|
|
201
201
|
session.exitCode = code ?? null;
|
|
202
202
|
session.signal = signalName(proc.signalCode);
|
|
203
203
|
session.lastActivity = Date.now();
|
|
204
|
-
// Queued output is older than this frame. Flushing first keeps
|
|
205
|
-
// order honest: otherwise the shell's last bytes would print after the
|
|
204
|
+
// Queued output is older than this frame. Flushing first keeps the
|
|
205
|
+
// renderer's order honest: otherwise the shell's last bytes would print after the
|
|
206
206
|
// notice that the shell is gone.
|
|
207
207
|
session.output.flush();
|
|
208
208
|
const frame = JSON.stringify({ t: 'exit', exitCode: session.exitCode, signal: session.signal });
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* New API gateways report a dollar amount only through their OpenAI-compatible
|
|
8
|
+
* billing endpoints, in cents, with a sentinel instead of "unlimited". Each
|
|
9
|
+
* payload below is the verbatim shape aihub.brisnets.com returned for the
|
|
10
|
+
* stored `sk-` key (New API v1.0.0-rc.26-aihub.3.12).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, expect, test } from 'bun:test';
|
|
14
|
+
|
|
15
|
+
import { isNewApiGatewayBaseUrl, newApiBillingUrl, parseNewApiBilling } from '@/server/lib/usage/new-api';
|
|
16
|
+
|
|
17
|
+
describe('isNewApiGatewayBaseUrl', () => {
|
|
18
|
+
test('matches the gateway hosts at any path or subdomain', () => {
|
|
19
|
+
expect(isNewApiGatewayBaseUrl('https://aihub.brisnets.com/v1')).toBe(true);
|
|
20
|
+
expect(isNewApiGatewayBaseUrl('https://aihub.brisnets.com/v1/')).toBe(true);
|
|
21
|
+
expect(isNewApiGatewayBaseUrl('https://api.aihub.brisnets.com/v1')).toBe(true);
|
|
22
|
+
expect(isNewApiGatewayBaseUrl('https://easyaihub.ai/v1')).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('a vendor host, a look-alike host and a malformed URL are not gateways', () => {
|
|
26
|
+
expect(isNewApiGatewayBaseUrl('https://api.deepseek.com/v1')).toBe(false);
|
|
27
|
+
expect(isNewApiGatewayBaseUrl('https://notbrisnets.com/v1')).toBe(false);
|
|
28
|
+
expect(isNewApiGatewayBaseUrl('aihub.brisnets.com')).toBe(false);
|
|
29
|
+
expect(isNewApiGatewayBaseUrl('')).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('newApiBillingUrl', () => {
|
|
34
|
+
test('billing endpoints hang off the provider base URL', () => {
|
|
35
|
+
expect(newApiBillingUrl('https://aihub.brisnets.com/v1', 'usage'))
|
|
36
|
+
.toBe('https://aihub.brisnets.com/v1/dashboard/billing/usage');
|
|
37
|
+
expect(newApiBillingUrl('https://aihub.brisnets.com/v1/', 'subscription'))
|
|
38
|
+
.toBe('https://aihub.brisnets.com/v1/dashboard/billing/subscription');
|
|
39
|
+
expect(newApiBillingUrl('https://aihub.brisnets.com', 'usage'))
|
|
40
|
+
.toBe('https://aihub.brisnets.com/v1/dashboard/billing/usage');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('parseNewApiBilling', () => {
|
|
45
|
+
test('an unlimited token reports spend only, never the sentinel as a limit', () => {
|
|
46
|
+
const windows = parseNewApiBilling(
|
|
47
|
+
'aihub-brisnets-jatis',
|
|
48
|
+
{ object: 'list', total_usage: 1365.635 },
|
|
49
|
+
{ object: 'billing_subscription', hard_limit_usd: 100_000_000, access_until: 0 },
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
expect(windows).toHaveLength(1);
|
|
53
|
+
expect(windows[0].id).toBe('aihub-brisnets-jatis:spent');
|
|
54
|
+
expect(windows[0].unit).toBe('usd');
|
|
55
|
+
expect(windows[0].used).toBeCloseTo(13.65635, 6);
|
|
56
|
+
expect(windows[0].limit).toBeUndefined();
|
|
57
|
+
expect(windows[0].usedFraction).toBeUndefined();
|
|
58
|
+
expect(windows[0].notes?.join(' ')).toContain('gateway console');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('a limited token derives the remaining credit from remain+used', () => {
|
|
62
|
+
const [window] = parseNewApiBilling(
|
|
63
|
+
'relay',
|
|
64
|
+
{ object: 'list', total_usage: 500 },
|
|
65
|
+
{ hard_limit_usd: 20 },
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
expect(window.id).toBe('relay:credit');
|
|
69
|
+
expect(window.used).toBe(5);
|
|
70
|
+
expect(window.limit).toBe(20);
|
|
71
|
+
expect(window.remaining).toBe(15);
|
|
72
|
+
expect(window.usedFraction).toBeCloseTo(0.25, 6);
|
|
73
|
+
expect(window.status).toBe('ok');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('an exhausted token clamps the remaining credit at zero', () => {
|
|
77
|
+
const [window] = parseNewApiBilling('relay', { total_usage: 2500 }, { hard_limit_usd: 20 });
|
|
78
|
+
|
|
79
|
+
expect(window.remaining).toBe(0);
|
|
80
|
+
expect(window.status).toBe('exhausted');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('a missing subscription still reports the spend', () => {
|
|
84
|
+
const windows = parseNewApiBilling('relay', { total_usage: 100 }, undefined);
|
|
85
|
+
expect(windows[0].used).toBe(1);
|
|
86
|
+
expect(windows[0].limit).toBeUndefined();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('a body without total_usage is a schema change, not an empty balance', () => {
|
|
90
|
+
expect(() => parseNewApiBilling('relay', { object: 'list' })).toThrow('unexpected billing response');
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* New API gateways (github.com/Calcium-Ion/new-api, a one-api fork).
|
|
8
|
+
*
|
|
9
|
+
* A gateway is not a vendor with one fixed endpoint: every deployment lives at
|
|
10
|
+
* its own base URL, and the provider is registered under the user's own slug
|
|
11
|
+
* (`aihub-brisnets-jatis`), so the URL has to come from the provider entry.
|
|
12
|
+
* The host is the only thing that identifies the family.
|
|
13
|
+
*
|
|
14
|
+
* Verified against aihub.brisnets.com (New API `v1.0.0-rc.26-aihub.3.12`,
|
|
15
|
+
* `quota_display_type` USD, `quota_per_unit` 500000) with the stored `sk-` key:
|
|
16
|
+
*
|
|
17
|
+
* GET <base>/dashboard/billing/usage → {"object":"list","total_usage":1365.635}
|
|
18
|
+
* GET <base>/dashboard/billing/subscription → {"hard_limit_usd":100000000,…}
|
|
19
|
+
* GET /api/user/self → 401 "invalid access token"
|
|
20
|
+
*
|
|
21
|
+
* `total_usage` is USD **cents** (`amount * 100` in controller/billing.go), and
|
|
22
|
+
* `hard_limit_usd` is `remaining + used`. The literal 100000000 is the sentinel
|
|
23
|
+
* New API writes for a token with `unlimited_quota`, not a real balance. Real
|
|
24
|
+
* account balance needs a console **access token** on `/api/user/self`; an
|
|
25
|
+
* `sk-` key cannot read it, so an unlimited key shows spend only.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { resolveProviderEndpoint, type ProviderEndpoint } from '@/server/lib/usage/provider-key';
|
|
29
|
+
import { fetchProviderJson } from '@/server/lib/usage/vendor-http';
|
|
30
|
+
import type { VendorBalance } from '@/server/lib/usage/vendor-balance.server';
|
|
31
|
+
import { num, usdWindow } from '@/server/lib/usage/window';
|
|
32
|
+
import { isRecord } from '@/shared/lib/util/guards';
|
|
33
|
+
import type { UsageLimitWindow } from '@/shared/types';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Hosts that serve New API. `easyaihub.ai` is the same operator's China domain
|
|
37
|
+
* (`/api/status` advertises it next to `aihub.brisnets.com`).
|
|
38
|
+
*/
|
|
39
|
+
const GATEWAY_HOSTS: readonly string[] = ['aihub.brisnets.com', 'easyaihub.ai'];
|
|
40
|
+
|
|
41
|
+
/** New API's "no limit on this token" marker, not a dollar amount. */
|
|
42
|
+
const UNLIMITED_SENTINEL = 100_000_000;
|
|
43
|
+
|
|
44
|
+
/** Whether a provider's base URL is a New API gateway. */
|
|
45
|
+
export function isNewApiGatewayBaseUrl(baseUrl: string): boolean {
|
|
46
|
+
let host: string;
|
|
47
|
+
try {
|
|
48
|
+
host = new URL(baseUrl).hostname.toLowerCase();
|
|
49
|
+
} catch {
|
|
50
|
+
return false; // a malformed URL is not a gateway
|
|
51
|
+
}
|
|
52
|
+
return GATEWAY_HOSTS.some((known) => host === known || host.endsWith(`.${known}`));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A billing endpoint under the provider's base URL (`…/v1` is implied). */
|
|
56
|
+
export function newApiBillingUrl(baseUrl: string, path: 'usage' | 'subscription'): string {
|
|
57
|
+
const trimmed = baseUrl.trim().replace(/\/+$/, '');
|
|
58
|
+
const base = /\/v1$/i.test(trimmed) ? trimmed : `${trimmed}/v1`;
|
|
59
|
+
return `${base}/dashboard/billing/${path}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Map the two billing payloads into quota windows. `usage` is required — a body
|
|
64
|
+
* without `total_usage` is a schema change, not an empty balance — while the
|
|
65
|
+
* subscription is best-effort because the spend alone is still useful.
|
|
66
|
+
*/
|
|
67
|
+
export function parseNewApiBilling(
|
|
68
|
+
slug: string,
|
|
69
|
+
usage: unknown,
|
|
70
|
+
subscription?: unknown,
|
|
71
|
+
): UsageLimitWindow[] {
|
|
72
|
+
const spentCents = isRecord(usage) ? num(usage.total_usage) : undefined;
|
|
73
|
+
if (spentCents === undefined) throw new Error('unexpected billing response');
|
|
74
|
+
const used = spentCents / 100;
|
|
75
|
+
const limit = isRecord(subscription) ? num(subscription.hard_limit_usd) : undefined;
|
|
76
|
+
|
|
77
|
+
if (limit === undefined || limit >= UNLIMITED_SENTINEL) {
|
|
78
|
+
return [
|
|
79
|
+
usdWindow({
|
|
80
|
+
id: `${slug}:spent`,
|
|
81
|
+
label: 'Spent (all time)',
|
|
82
|
+
used,
|
|
83
|
+
note: 'This key has no spending limit; the remaining balance is in the gateway console.',
|
|
84
|
+
}),
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return [
|
|
89
|
+
usdWindow({
|
|
90
|
+
id: `${slug}:credit`,
|
|
91
|
+
label: 'Key credit',
|
|
92
|
+
used,
|
|
93
|
+
limit,
|
|
94
|
+
remaining: Math.max(0, limit - used),
|
|
95
|
+
}),
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function fetchGatewayBilling(slug: string, endpoint: ProviderEndpoint): Promise<VendorBalance> {
|
|
100
|
+
try {
|
|
101
|
+
const [usage, subscription] = await Promise.all([
|
|
102
|
+
fetchProviderJson(newApiBillingUrl(endpoint.baseUrl, 'usage'), endpoint.apiKey, slug),
|
|
103
|
+
// A failed subscription leaves the spend intact; only the limit is lost.
|
|
104
|
+
fetchProviderJson(newApiBillingUrl(endpoint.baseUrl, 'subscription'), endpoint.apiKey, slug)
|
|
105
|
+
.catch(() => undefined),
|
|
106
|
+
]);
|
|
107
|
+
return { limits: parseNewApiBilling(slug, usage, subscription) };
|
|
108
|
+
} catch (error) {
|
|
109
|
+
return {
|
|
110
|
+
limits: [],
|
|
111
|
+
error: error instanceof Error ? error.message : `${slug} usage request failed`,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Billing reports for every credentialed slug whose stored endpoint is a New
|
|
118
|
+
* API gateway. Providers with another endpoint are silently skipped, and one
|
|
119
|
+
* provider's failure never rejects the batch.
|
|
120
|
+
*/
|
|
121
|
+
export async function buildNewApiReports(slugs: Iterable<string>): Promise<Map<string, VendorBalance>> {
|
|
122
|
+
const reports = new Map<string, VendorBalance>();
|
|
123
|
+
const targets = await Promise.all(
|
|
124
|
+
[...slugs].map(async (slug) => [slug, await resolveProviderEndpoint(slug)] as const),
|
|
125
|
+
);
|
|
126
|
+
await Promise.all(targets.map(async ([slug, endpoint]) => {
|
|
127
|
+
if (!endpoint || !isNewApiGatewayBaseUrl(endpoint.baseUrl)) return;
|
|
128
|
+
reports.set(slug, await fetchGatewayBilling(slug, endpoint));
|
|
129
|
+
}));
|
|
130
|
+
return reports;
|
|
131
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `resolveProviderEndpoint` is the half `resolveApiKey` cannot answer: a relay
|
|
8
|
+
* serves its usage API at the base URL the user registered, so the endpoint has
|
|
9
|
+
* to be read from the same store as the key. These tests pin which store wins
|
|
10
|
+
* and what counts as a usable pair.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { afterAll, afterEach, beforeAll, describe, expect, test } from 'bun:test';
|
|
14
|
+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
15
|
+
import { tmpdir } from 'node:os';
|
|
16
|
+
import { join } from 'node:path';
|
|
17
|
+
|
|
18
|
+
import { getDb } from '@/server/db.server';
|
|
19
|
+
import { resolveProviderEndpoint } from '@/server/lib/usage/provider-key';
|
|
20
|
+
import { removeDir } from '@/test-support/remove-dir';
|
|
21
|
+
|
|
22
|
+
const dirs: string[] = [];
|
|
23
|
+
let agentDir = '';
|
|
24
|
+
let realAgentDir: string | undefined;
|
|
25
|
+
|
|
26
|
+
function tempDir(): string {
|
|
27
|
+
const dir = mkdtempSync(join(tmpdir(), 'kabmo-endpoint-'));
|
|
28
|
+
dirs.push(dir);
|
|
29
|
+
return dir;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
beforeAll(() => {
|
|
33
|
+
agentDir = tempDir();
|
|
34
|
+
realAgentDir = Bun.env.PI_CODING_AGENT_DIR;
|
|
35
|
+
Bun.env.PI_CODING_AGENT_DIR = agentDir;
|
|
36
|
+
writeFileSync(join(agentDir, 'models.yml'), 'providers: {}\n');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
afterAll(() => {
|
|
40
|
+
if (realAgentDir === undefined) delete Bun.env.PI_CODING_AGENT_DIR;
|
|
41
|
+
else Bun.env.PI_CODING_AGENT_DIR = realAgentDir;
|
|
42
|
+
for (const dir of dirs.splice(0)) removeDir(dir);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
afterEach(() => {
|
|
46
|
+
globalThis.__kabmoDb?.resolved?.raw.close();
|
|
47
|
+
globalThis.__kabmoDb = undefined;
|
|
48
|
+
delete Bun.env.KABMO_DB_PATH;
|
|
49
|
+
writeFileSync(join(agentDir, 'models.yml'), 'providers: {}\n');
|
|
50
|
+
rmSync(join(agentDir, 'agent.db'), { force: true });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
function useTempDb(): void {
|
|
54
|
+
Bun.env.KABMO_DB_PATH = join(tempDir(), 'db.sqlite');
|
|
55
|
+
Bun.env.SYNC_WORKSPACE = 'false';
|
|
56
|
+
delete Bun.env.MOCK;
|
|
57
|
+
globalThis.__kabmoDb = undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function writeOverlayValue(value: string): Promise<void> {
|
|
61
|
+
const db = await getDb();
|
|
62
|
+
await db.run('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)', ['omp_providers_config', value]);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
describe('resolveProviderEndpoint', () => {
|
|
66
|
+
test('reads the key and endpoint from the provider own models.yml entry', async () => {
|
|
67
|
+
useTempDb();
|
|
68
|
+
writeFileSync(
|
|
69
|
+
join(agentDir, 'models.yml'),
|
|
70
|
+
'providers:\n aihub-brisnets-jatis:\n baseUrl: https://aihub.brisnets.com/v1\n apiKey: sk-live\n',
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
expect(await resolveProviderEndpoint('aihub-brisnets-jatis')).toEqual({
|
|
74
|
+
apiKey: 'sk-live',
|
|
75
|
+
baseUrl: 'https://aihub.brisnets.com/v1',
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('the Kabmo overlay wins when it holds both a key and an endpoint', async () => {
|
|
80
|
+
useTempDb();
|
|
81
|
+
await writeOverlayValue(JSON.stringify([
|
|
82
|
+
{ name: 'AI HUB', slug: 'aihub', baseUrl: 'https://aihub.brisnets.com/v1', apiKey: 'overlay-key' },
|
|
83
|
+
]));
|
|
84
|
+
writeFileSync(
|
|
85
|
+
join(agentDir, 'models.yml'),
|
|
86
|
+
'providers:\n aihub:\n baseUrl: https://aihub.brisnets.com/v1\n apiKey: yml-key\n',
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
expect(await resolveProviderEndpoint('aihub')).toEqual({
|
|
90
|
+
apiKey: 'overlay-key',
|
|
91
|
+
baseUrl: 'https://aihub.brisnets.com/v1',
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('a masked overlay key is not a credential', async () => {
|
|
96
|
+
useTempDb();
|
|
97
|
+
await writeOverlayValue(JSON.stringify([
|
|
98
|
+
{ name: 'AI HUB', slug: 'aihub', baseUrl: 'https://aihub.brisnets.com/v1', apiKey: '••••••••' },
|
|
99
|
+
]));
|
|
100
|
+
writeFileSync(join(agentDir, 'models.yml'), 'providers:\n aihub:\n apiKey: yml-key\n');
|
|
101
|
+
|
|
102
|
+
// The overlay row cannot supply a key, and models.yml carries no baseUrl,
|
|
103
|
+
// so there is no endpoint to resolve — not a half-valid pair.
|
|
104
|
+
expect(await resolveProviderEndpoint('aihub')).toBeNull();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
@@ -117,6 +117,39 @@ export function resolveDeepSeekApiKey(): Promise<string | null> {
|
|
|
117
117
|
);
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
+
/** A provider's key AND the endpoint it was registered with. */
|
|
121
|
+
export interface ProviderEndpoint {
|
|
122
|
+
apiKey: string;
|
|
123
|
+
baseUrl: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The provider's key plus its own base URL. `resolveApiKey` alone cannot locate
|
|
128
|
+
* a gateway's billing API: a self-hosted relay (New API, one-api) lives at the
|
|
129
|
+
* URL the user configured, so the endpoint has to come from the provider entry
|
|
130
|
+
* rather than from a vendor constant. agent.db credentials carry no URL, so a
|
|
131
|
+
* provider whose only store is agent.db resolves to null.
|
|
132
|
+
*/
|
|
133
|
+
export async function resolveProviderEndpoint(slug: string): Promise<ProviderEndpoint | null> {
|
|
134
|
+
const target = slug.trim().toLowerCase();
|
|
135
|
+
for (const entry of await loadProviderEntries()) {
|
|
136
|
+
const own = (entry.slug || entry.name).trim().toLowerCase();
|
|
137
|
+
if (own !== target) continue;
|
|
138
|
+
const apiKey = entry.apiKey.trim();
|
|
139
|
+
const baseUrl = entry.baseUrl.trim();
|
|
140
|
+
if (apiKey.length === 0 || MASKED_KEY_PATTERN.test(apiKey) || baseUrl.length === 0) continue;
|
|
141
|
+
return { apiKey, baseUrl };
|
|
142
|
+
}
|
|
143
|
+
for (const info of await readNativeProviders()) {
|
|
144
|
+
if (info.slug.trim().toLowerCase() !== target) continue;
|
|
145
|
+
const baseUrl = info.baseUrl?.trim();
|
|
146
|
+
if (!baseUrl) continue;
|
|
147
|
+
const apiKey = await readOmpProviderApiKey(target);
|
|
148
|
+
if (apiKey) return { apiKey, baseUrl };
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
|
|
120
153
|
/** A provider slug with at least one usable credential, and where it came from. */
|
|
121
154
|
export interface CredentialedProvider {
|
|
122
155
|
slug: string;
|
|
@@ -23,6 +23,7 @@ import { fetchOmpRegistrySnapshot } from '@/server/lib/models/provider-registry.
|
|
|
23
23
|
import { readProviderIconUrls } from '@/server/lib/models/provider-icon.server';
|
|
24
24
|
import { buildDeepSeekReport } from '@/server/lib/usage/deepseek';
|
|
25
25
|
import { buildKenariReport } from '@/server/lib/usage/kenari';
|
|
26
|
+
import { buildNewApiReports } from '@/server/lib/usage/new-api';
|
|
26
27
|
import { fetchOmpUsageSnapshot, type OmpUsageSnapshot } from '@/server/lib/usage/omp-usage.server';
|
|
27
28
|
import { fetchVendorBalance, type VendorBalance } from '@/server/lib/usage/vendor-balance.server';
|
|
28
29
|
import { kenariProviderSlugs, listCredentialedProviders, type CredentialedProvider } from '@/server/lib/usage/provider-key';
|
|
@@ -123,10 +124,13 @@ export async function buildUsageProviders(options: { force?: boolean } = {}): Pr
|
|
|
123
124
|
// one (`kenari2`) with no quota card while its key was never read. DeepSeek
|
|
124
125
|
// still resolves by its canonical slug.
|
|
125
126
|
const kenariSlugs = await kenariProviderSlugs(credentials.keys());
|
|
126
|
-
const [kenariReports, deepseek, balances] = await Promise.all([
|
|
127
|
+
const [kenariReports, deepseek, balances, gateways] = await Promise.all([
|
|
127
128
|
Promise.all([...kenariSlugs].map(async (slug) => [slug, await buildKenariReport(slug)] as const)),
|
|
128
129
|
credentials.has('deepseek') ? buildDeepSeekReport() : Promise.resolve(null),
|
|
129
130
|
loadVendorBalances([...credentials.keys()]),
|
|
131
|
+
// Self-hosted relays answer on the provider's OWN base URL, so they cannot
|
|
132
|
+
// live in `vendor-balance`'s fixed-URL table.
|
|
133
|
+
buildNewApiReports(credentials.keys()),
|
|
130
134
|
]);
|
|
131
135
|
const kenariBySlug = new Map<string, KenariUsageReport>();
|
|
132
136
|
for (const [slug, report] of kenariReports) {
|
|
@@ -137,6 +141,7 @@ export async function buildUsageProviders(options: { force?: boolean } = {}): Pr
|
|
|
137
141
|
for (const [slug, sources] of credentials) {
|
|
138
142
|
const report = snapshot.reports.get(slug);
|
|
139
143
|
const balance = balances.get(slug);
|
|
144
|
+
const gateway = gateways.get(slug);
|
|
140
145
|
const { tracked, limitsUnavailable } = resolveUsageTracking(slug, snapshot);
|
|
141
146
|
const summary: UsageProviderSummary = {
|
|
142
147
|
id: slug,
|
|
@@ -144,8 +149,8 @@ export async function buildUsageProviders(options: { force?: boolean } = {}): Pr
|
|
|
144
149
|
...(iconUrls.get(slug) ? { iconUrl: iconUrls.get(slug) } : {}),
|
|
145
150
|
credentialSources: sources,
|
|
146
151
|
// A vendor balance is real quota data even when omp has no adapter for it.
|
|
147
|
-
tracked: tracked || Boolean(balance
|
|
148
|
-
limits: [...(report?.limits ?? []), ...(balance?.limits ?? [])],
|
|
152
|
+
tracked: tracked || Boolean(balance?.limits.length) || Boolean(gateway?.limits.length),
|
|
153
|
+
limits: [...(report?.limits ?? []), ...(balance?.limits ?? []), ...(gateway?.limits ?? [])],
|
|
149
154
|
...(limitsUnavailable ? { limitsUnavailable: true } : {}),
|
|
150
155
|
...(report?.notes ? { notes: report.notes } : {}),
|
|
151
156
|
};
|
|
@@ -157,6 +162,8 @@ export async function buildUsageProviders(options: { force?: boolean } = {}): Pr
|
|
|
157
162
|
} else if (slug === 'deepseek' && deepseek) {
|
|
158
163
|
summary.deepseek = deepseek;
|
|
159
164
|
if (deepseek.error) summary.error = deepseek.error;
|
|
165
|
+
} else if (gateway?.error) {
|
|
166
|
+
summary.error = gateway.error;
|
|
160
167
|
} else if (balance?.error && !summary.error) {
|
|
161
168
|
// Only surface a balance failure when nothing richer already reported one.
|
|
162
169
|
summary.error = balance.error;
|
|
@@ -24,10 +24,11 @@
|
|
|
24
24
|
|
|
25
25
|
import { readOmpProviderApiKey } from '@/server/lib/omp/core/auth-credentials';
|
|
26
26
|
import { loadProviderEntries } from '@/server/lib/usage/provider-key';
|
|
27
|
+
import { fetchProviderJson } from '@/server/lib/usage/vendor-http';
|
|
28
|
+
import { num, usdWindow } from '@/server/lib/usage/window';
|
|
27
29
|
import { isRecord } from '@/shared/lib/util/guards';
|
|
28
|
-
import type { UsageLimitWindow
|
|
30
|
+
import type { UsageLimitWindow } from '@/shared/types';
|
|
29
31
|
|
|
30
|
-
const FETCH_TIMEOUT_MS = 10_000;
|
|
31
32
|
const MASKED_KEY_PATTERN = /•{3,}/;
|
|
32
33
|
|
|
33
34
|
/** A vendor balance adapter: where to ask, and how to read the answer. */
|
|
@@ -41,46 +42,6 @@ interface BalanceAdapter {
|
|
|
41
42
|
parse: (payload: unknown) => UsageLimitWindow[];
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
function num(value: unknown): number | undefined {
|
|
45
|
-
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
46
|
-
if (typeof value === 'string' && value.trim() !== '') {
|
|
47
|
-
const parsed = Number(value);
|
|
48
|
-
if (Number.isFinite(parsed)) return parsed;
|
|
49
|
-
}
|
|
50
|
-
return undefined;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Status for a credit window: exhausted at zero remaining, else ok. */
|
|
54
|
-
function balanceStatus(remaining: number | undefined): UsageWindowStatus {
|
|
55
|
-
if (remaining === undefined) return 'unknown';
|
|
56
|
-
return remaining <= 0 ? 'exhausted' : 'ok';
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** One USD window from a balance; `limit` is omitted when the vendor has none. */
|
|
60
|
-
function usdWindow(input: {
|
|
61
|
-
id: string;
|
|
62
|
-
label: string;
|
|
63
|
-
used?: number;
|
|
64
|
-
limit?: number;
|
|
65
|
-
remaining?: number;
|
|
66
|
-
note?: string;
|
|
67
|
-
}): UsageLimitWindow {
|
|
68
|
-
const usedFraction = input.limit && input.limit > 0 && input.used !== undefined
|
|
69
|
-
? input.used / input.limit
|
|
70
|
-
: undefined;
|
|
71
|
-
return {
|
|
72
|
-
id: input.id,
|
|
73
|
-
label: input.label,
|
|
74
|
-
unit: 'usd',
|
|
75
|
-
status: balanceStatus(input.remaining),
|
|
76
|
-
...(input.used !== undefined ? { used: input.used } : {}),
|
|
77
|
-
...(input.limit !== undefined ? { limit: input.limit } : {}),
|
|
78
|
-
...(input.remaining !== undefined ? { remaining: input.remaining } : {}),
|
|
79
|
-
...(usedFraction !== undefined ? { usedFraction } : {}),
|
|
80
|
-
...(input.note ? { notes: [input.note] } : {}),
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
45
|
/** Pull `data` (or the root) out of a vendor envelope. */
|
|
85
46
|
function envelope(payload: unknown): Record<string, unknown> {
|
|
86
47
|
if (!isRecord(payload)) throw new Error('unexpected balance response');
|
|
@@ -263,17 +224,7 @@ export async function fetchVendorBalance(slug: string): Promise<VendorBalance |
|
|
|
263
224
|
const apiKey = await resolveVendorKey(adapter);
|
|
264
225
|
if (!apiKey) return null;
|
|
265
226
|
try {
|
|
266
|
-
const
|
|
267
|
-
headers: { Authorization: `Bearer ${apiKey}`, Accept: 'application/json' },
|
|
268
|
-
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
|
269
|
-
});
|
|
270
|
-
if (response.status === 401 || response.status === 403) {
|
|
271
|
-
return { limits: [], error: `${adapter.slug} rejected the API key (HTTP ${response.status}).` };
|
|
272
|
-
}
|
|
273
|
-
if (response.status < 200 || response.status >= 300) {
|
|
274
|
-
return { limits: [], error: `${adapter.slug} balance request failed (HTTP ${response.status}).` };
|
|
275
|
-
}
|
|
276
|
-
const limits = adapter.parse(await response.json());
|
|
227
|
+
const limits = adapter.parse(await fetchProviderJson(adapter.url, apiKey, adapter.slug));
|
|
277
228
|
return { limits };
|
|
278
229
|
} catch (error) {
|
|
279
230
|
return {
|