@openchamber/web 1.14.1 → 1.16.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/bin/cli.test.js +8 -0
- package/bin/lib/DOCUMENTATION.md +3 -0
- package/bin/lib/cli-args.js +10 -0
- package/bin/lib/commands-connect-url.js +169 -22
- package/dist/assets/{DiagramView-Cb58svCq.js → DiagramView-he_RFVx5.js} +1 -1
- package/dist/assets/MarkdownRendererImpl-CdBIHSPZ.js +11 -0
- package/dist/assets/{MultiRunWindow-BPSxV_B7.js → MultiRunWindow-B_C-tvVq.js} +1 -1
- package/dist/assets/OnboardingScreen-BBnTt-9w.js +1 -0
- package/dist/assets/SessionSwitcherDropdown-CWWrCFzr.js +1 -0
- package/dist/assets/SettingsWindow-CjqLdpTE.js +1 -0
- package/dist/assets/{TerminalView-fTYjnQkh.js → TerminalView-DQcdegHp.js} +1 -1
- package/dist/assets/es-r0_jT91q.js +15 -0
- package/dist/assets/fr-Bo8rtAJ1.js +13 -0
- package/dist/assets/index-8J6waVO6.js +2 -0
- package/dist/assets/index-DEMqM4Lz.css +1 -0
- package/dist/assets/ja-BPWtkG-c.js +15 -0
- package/dist/assets/ko-Bv0k-0E3.js +15 -0
- package/dist/assets/main-B-GA5Tg5.js +237 -0
- package/dist/assets/main-Dfm-VKZ9.js +2 -0
- package/dist/assets/miniChat-CfmCFFfu.js +2 -0
- package/dist/assets/mobile-CXJqeDlm.js +2 -0
- package/dist/assets/pl-CiTP50bz.js +15 -0
- package/dist/assets/pt-BR-DadIjX5L.js +15 -0
- package/dist/assets/renderElectronMiniChatApp-DS7Dy2ii.js +2 -0
- package/dist/assets/renderMobileApp-o7Fs9EBx.js +16 -0
- package/dist/assets/runtimeEndpointReset-MTaPPm4F.js +167 -0
- package/dist/assets/uk-2RpsWGoZ.js +15 -0
- package/dist/assets/{useAppFontEffects-BUUJYbAZ.js → useAppFontEffects-DkPuAMXD.js} +196 -133
- package/dist/assets/{vendor-.bun-C6iL44L9.js → vendor-.bun-BE757pvp.js} +1 -1
- package/dist/assets/zh-CN-BwKWaTuz.js +15 -0
- package/dist/assets/zh-TW--f_BqBpD.js +15 -0
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +4 -4
- package/dist/mobile.html +4 -4
- package/package.json +2 -2
- package/server/index.js +236 -2
- package/server/lib/client-auth/pairing.js +308 -0
- package/server/lib/client-auth/pairing.test.js +142 -0
- package/server/lib/client-auth/remote-clients.js +65 -3
- package/server/lib/notifications/DOCUMENTATION.md +1 -1
- package/server/lib/notifications/apns-runtime.js +11 -16
- package/server/lib/notifications/runtime.js +135 -33
- package/server/lib/opencode/DOCUMENTATION.md +1 -0
- package/server/lib/opencode/agents.js +3 -0
- package/server/lib/opencode/bootstrap-runtime.js +15 -0
- package/server/lib/opencode/core-routes.js +321 -8
- package/server/lib/opencode/core-routes.test.js +329 -9
- package/server/lib/opencode/feature-routes-runtime.js +6 -0
- package/server/lib/opencode/network-runtime.js +14 -1
- package/server/lib/opencode/network-runtime.test.js +29 -9
- package/server/lib/opencode/proxy.js +47 -6
- package/server/lib/opencode/settings-helpers.js +24 -0
- package/server/lib/opencode/settings-helpers.test.js +25 -0
- package/server/lib/opencode/settings-normalization-runtime.js +21 -4
- package/server/lib/opencode/settings-normalization-runtime.test.js +21 -0
- package/server/lib/opencode/settings-runtime.js +25 -0
- package/server/lib/opencode/shutdown-runtime.js +2 -0
- package/server/lib/opencode/tunnel-auth.js +4 -9
- package/server/lib/package-manager.js +18 -5
- package/server/lib/permission-auto-accept/DOCUMENTATION.md +34 -0
- package/server/lib/permission-auto-accept/runtime.js +263 -0
- package/server/lib/permission-auto-accept/runtime.test.js +137 -0
- package/server/lib/projects/project-config.js +8 -0
- package/server/lib/quota/DOCUMENTATION.md +5 -2
- package/server/lib/quota/credentials/providers.js +43 -0
- package/server/lib/quota/credentials/store.js +48 -0
- package/server/lib/quota/credentials/store.test.js +26 -0
- package/server/lib/quota/opencode-go-credentials.js +11 -0
- package/server/lib/quota/opencode-go-credentials.test.js +32 -0
- package/server/lib/quota/providers/codex.js +7 -4
- package/server/lib/quota/providers/codex.test.js +48 -0
- package/server/lib/quota/providers/cursor.js +28 -39
- package/server/lib/quota/providers/index.js +7 -0
- package/server/lib/quota/providers/ollama-cloud.js +23 -42
- package/server/lib/quota/providers/ollama-cloud.test.js +12 -0
- package/server/lib/quota/providers/opencode-go.js +70 -0
- package/server/lib/quota/providers/opencode-go.test.js +17 -0
- package/server/lib/quota/routes.js +78 -7
- package/server/lib/quota/routes.test.js +40 -0
- package/server/lib/relay/DOCUMENTATION.md +98 -0
- package/server/lib/relay/cross-compat.test.js +135 -0
- package/server/lib/relay/e2ee.js +341 -0
- package/server/lib/relay/e2ee.test.js +124 -0
- package/server/lib/relay/host-client.js +389 -0
- package/server/lib/relay/host-client.test.js +280 -0
- package/server/lib/relay/host-lock.js +101 -0
- package/server/lib/relay/host-lock.test.js +129 -0
- package/server/lib/relay/identity.js +93 -0
- package/server/lib/relay/identity.test.js +78 -0
- package/server/lib/relay/service.js +334 -0
- package/server/lib/relay/signing-key.js +73 -0
- package/server/lib/relay/tunnel-codec.js +373 -0
- package/server/lib/relay/tunnel-codec.test.js +58 -0
- package/server/lib/relay/tunnel-host.js +462 -0
- package/server/lib/scheduled-tasks/runtime.js +104 -0
- package/server/lib/session-goal/DOCUMENTATION.md +176 -0
- package/server/lib/session-goal/objectives.js +61 -0
- package/server/lib/session-goal/routes.js +36 -0
- package/server/lib/session-goal/runtime.js +764 -0
- package/server/lib/ui-auth/DOCUMENTATION.md +6 -0
- package/server/lib/ui-auth/ui-auth.js +10 -0
- package/server/opencode-proxy.test.js +38 -0
- package/dist/assets/MarkdownRendererImpl-D2yCDL5A.js +0 -7
- package/dist/assets/OnboardingScreen-Sl-6JgaP.js +0 -1
- package/dist/assets/SessionSwitcherDropdown-4FWg9xMC.js +0 -1
- package/dist/assets/SettingsWindow-CY8q4msv.js +0 -1
- package/dist/assets/es-bWcuDy_2.js +0 -15
- package/dist/assets/fr-DzRs3OzV.js +0 -13
- package/dist/assets/index-KnWk9fmM.css +0 -1
- package/dist/assets/index-ZJTzZ0_U.js +0 -2
- package/dist/assets/ja-BqUX_UD3.js +0 -15
- package/dist/assets/ko-BQo10SNx.js +0 -15
- package/dist/assets/main-BDoqLlQB.js +0 -236
- package/dist/assets/main-DUGyTrDt.js +0 -2
- package/dist/assets/miniChat-DNDiX8GA.js +0 -2
- package/dist/assets/mobile-DnghtOCb.js +0 -2
- package/dist/assets/pl-Bgx_ATJT.js +0 -15
- package/dist/assets/pt-BR-B1yXRvoX.js +0 -15
- package/dist/assets/renderElectronMiniChatApp-BB4C1K7t.js +0 -2
- package/dist/assets/renderMobileApp-CZgaXKi5.js +0 -16
- package/dist/assets/runtimeEndpointReset-By39StyB.js +0 -167
- package/dist/assets/uk-C9k8SP8q.js +0 -15
- package/dist/assets/zh-CN-Bri-Vo5y.js +0 -15
- package/dist/assets/zh-TW-DAUGzmYG.js +0 -15
|
@@ -213,6 +213,12 @@ const normalizeExecution = (value) => {
|
|
|
213
213
|
const modelID = asNonEmptyString(value.modelID);
|
|
214
214
|
const variant = asNonEmptyString(value.variant);
|
|
215
215
|
const agent = asNonEmptyString(value.agent);
|
|
216
|
+
const goalEnabled = value.goalEnabled === true;
|
|
217
|
+
const goalTokenBudget = typeof value.goalTokenBudget === 'number'
|
|
218
|
+
&& Number.isFinite(value.goalTokenBudget)
|
|
219
|
+
&& value.goalTokenBudget > 0
|
|
220
|
+
? Math.floor(value.goalTokenBudget)
|
|
221
|
+
: undefined;
|
|
216
222
|
|
|
217
223
|
if (!prompt) {
|
|
218
224
|
throw new Error('execution.prompt is required');
|
|
@@ -230,6 +236,8 @@ const normalizeExecution = (value) => {
|
|
|
230
236
|
modelID,
|
|
231
237
|
...(variant ? { variant } : {}),
|
|
232
238
|
...(agent ? { agent } : {}),
|
|
239
|
+
...(goalEnabled ? { goalEnabled: true } : {}),
|
|
240
|
+
...(goalEnabled && goalTokenBudget ? { goalTokenBudget } : {}),
|
|
233
241
|
};
|
|
234
242
|
};
|
|
235
243
|
|
|
@@ -18,7 +18,7 @@ These provider IDs are currently dispatchable via `fetchQuotaForProvider(provide
|
|
|
18
18
|
| --- | --- | --- | --- |
|
|
19
19
|
| `claude` | Claude | `providers/claude.js` | `anthropic`, `claude` |
|
|
20
20
|
| `codex` | Codex | `providers/codex.js` | `openai`, `codex`, `chatgpt` |
|
|
21
|
-
| `cursor` | Cursor | `providers/cursor.js` |
|
|
21
|
+
| `cursor` | Cursor | `providers/cursor.js` | Environment/token files, OpenChamber-managed credentials, or explicit one-time Cursor import |
|
|
22
22
|
| `google` | Google | `providers/google/index.js` | `google`, `google.oauth`, Antigravity accounts file |
|
|
23
23
|
| `github-copilot` | GitHub Copilot | `providers/copilot.js` | `github-copilot`, `copilot` |
|
|
24
24
|
| `github-copilot-addon` | GitHub Copilot Add-on | `providers/copilot.js` | `github-copilot`, `copilot` |
|
|
@@ -29,8 +29,9 @@ These provider IDs are currently dispatchable via `fetchQuotaForProvider(provide
|
|
|
29
29
|
| `zhipuai-coding-plan` | Zhipu AI Coding Plan | `providers/zhipuai-coding-plan.js` | `zhipuai-coding-plan`, `zhipuai`, `zhipu` |
|
|
30
30
|
| `minimax-coding-plan` | MiniMax Coding Plan (minimax.io) | `providers/minimax-coding-plan.js` / `providers/minimax-shared.js` | `minimax-coding-plan` |
|
|
31
31
|
| `minimax-cn-coding-plan` | MiniMax Coding Plan (minimaxi.com) | `providers/minimax-cn-coding-plan.js` / `providers/minimax-shared.js` | `minimax-cn-coding-plan` |
|
|
32
|
-
| `ollama-cloud` | Ollama Cloud | `providers/ollama-cloud.js` |
|
|
32
|
+
| `ollama-cloud` | Ollama Cloud | `providers/ollama-cloud.js` | Manual cookie stored under `~/.config/openchamber/quota/` |
|
|
33
33
|
| `wafer` | Wafer.ai | `providers/wafer.js` | `wafer`, `wafer-ai`, `wafer_ai`, `wafer.ai` |
|
|
34
|
+
| `opencode-go` | OpenCode Go | `providers/opencode-go.js` | Manual workspace ID and auth cookie stored under `~/.config/openchamber/quota/` |
|
|
34
35
|
|
|
35
36
|
## Internal-only provider module
|
|
36
37
|
- `providers/openai.js` exists for logic parity/reuse but is intentionally not registered for dispatcher ID routing.
|
|
@@ -44,6 +45,8 @@ All providers should return results via shared helpers to preserve API shape:
|
|
|
44
45
|
Provider modules must export `providerId`, `providerName`, `aliases`, `isConfigured(auth?)`, and `fetchQuota()`.
|
|
45
46
|
`fetchQuota()` should return a quota result with `usage.windows` keyed by window name (for example `5h`, `7d`, `daily`) and optional provider-specific `usage.models` data.
|
|
46
47
|
|
|
48
|
+
OpenCode Go, Ollama Cloud, and Cursor credentials are explicitly managed through Settings. The server validates credentials before atomic `0600` writes and never returns secrets through its API. OpenChamber never scans browser cookie stores or automatically reads Cursor storage; Cursor import is an explicit one-time user action and never modifies Cursor's database.
|
|
49
|
+
|
|
47
50
|
## Add a new provider (quick steps)
|
|
48
51
|
1. Choose module shape based on complexity:
|
|
49
52
|
- Simple providers: create `packages/web/server/lib/quota/providers/<provider>.js`.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { deleteQuotaCredential, readQuotaCredential, writeQuotaCredential } from './store.js';
|
|
2
|
+
|
|
3
|
+
const clean = (value) => typeof value === 'string' && !/[\r\n]/.test(value) ? value.trim() : '';
|
|
4
|
+
|
|
5
|
+
export const normalizers = {
|
|
6
|
+
'opencode-go': (value) => {
|
|
7
|
+
const workspaceId = clean(value?.workspaceId);
|
|
8
|
+
let authCookie = clean(value?.authCookie);
|
|
9
|
+
if (authCookie.startsWith('auth=')) authCookie = authCookie.slice(5).trim();
|
|
10
|
+
return workspaceId && authCookie ? { workspaceId, authCookie } : null;
|
|
11
|
+
},
|
|
12
|
+
'ollama-cloud': (value) => {
|
|
13
|
+
const cookie = clean(value?.cookie);
|
|
14
|
+
return cookie ? { cookie } : null;
|
|
15
|
+
},
|
|
16
|
+
cursor: (value) => {
|
|
17
|
+
const accessToken = clean(value?.accessToken);
|
|
18
|
+
const refreshToken = clean(value?.refreshToken);
|
|
19
|
+
return accessToken || refreshToken ? { accessToken, refreshToken } : null;
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const readManagedCredential = (providerId) => {
|
|
24
|
+
const normalize = normalizers[providerId];
|
|
25
|
+
return normalize ? readQuotaCredential(providerId, normalize) : null;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const writeManagedCredential = (providerId, value) => {
|
|
29
|
+
const credential = normalizers[providerId]?.(value);
|
|
30
|
+
if (!credential) throw new Error('Invalid credential');
|
|
31
|
+
writeQuotaCredential(providerId, credential);
|
|
32
|
+
return getManagedCredentialStatus(providerId);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const getManagedCredentialStatus = (providerId) => {
|
|
36
|
+
const credential = readManagedCredential(providerId);
|
|
37
|
+
if (!credential) return { configured: false };
|
|
38
|
+
if (providerId === 'opencode-go') return { configured: true, workspaceId: credential.workspaceId, secretMasked: '••••••••' };
|
|
39
|
+
if (providerId === 'cursor') return { configured: true, hasRefreshToken: Boolean(credential.refreshToken), secretMasked: '••••••••' };
|
|
40
|
+
return { configured: true, secretMasked: '••••••••' };
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const deleteManagedCredential = (providerId) => deleteQuotaCredential(providerId);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
const MANAGED_QUOTA_PROVIDERS = new Set(['opencode-go', 'ollama-cloud', 'cursor']);
|
|
6
|
+
|
|
7
|
+
const credentialsDirectory = () => path.join(
|
|
8
|
+
process.env.OPENCHAMBER_DATA_DIR
|
|
9
|
+
? path.resolve(process.env.OPENCHAMBER_DATA_DIR)
|
|
10
|
+
: path.join(os.homedir(), '.config', 'openchamber'),
|
|
11
|
+
'quota',
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const credentialPath = (providerId) => {
|
|
15
|
+
if (!MANAGED_QUOTA_PROVIDERS.has(providerId)) throw new Error('Unsupported credential provider');
|
|
16
|
+
return path.join(credentialsDirectory(), `${providerId}.json`);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const readQuotaCredential = (providerId, normalize) => {
|
|
20
|
+
try {
|
|
21
|
+
return normalize(JSON.parse(fs.readFileSync(credentialPath(providerId), 'utf8')));
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (error?.code !== 'ENOENT') console.warn(`Failed to read ${providerId} quota credentials`);
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const writeQuotaCredential = (providerId, credential) => {
|
|
29
|
+
const target = credentialPath(providerId);
|
|
30
|
+
const directory = path.dirname(target);
|
|
31
|
+
const temporary = `${target}.${process.pid}.${Date.now()}.tmp`;
|
|
32
|
+
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
33
|
+
fs.chmodSync(directory, 0o700);
|
|
34
|
+
try {
|
|
35
|
+
fs.writeFileSync(temporary, `${JSON.stringify(credential, null, 2)}\n`, { mode: 0o600 });
|
|
36
|
+
fs.chmodSync(temporary, 0o600);
|
|
37
|
+
fs.renameSync(temporary, target);
|
|
38
|
+
fs.chmodSync(target, 0o600);
|
|
39
|
+
} finally {
|
|
40
|
+
try { fs.unlinkSync(temporary); } catch {}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const deleteQuotaCredential = (providerId) => {
|
|
45
|
+
try { fs.unlinkSync(credentialPath(providerId)); } catch (error) {
|
|
46
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { afterAll, describe, expect, it } from 'bun:test';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { deleteQuotaCredential, readQuotaCredential, writeQuotaCredential } from './store.js';
|
|
6
|
+
|
|
7
|
+
const previousDataDir = process.env.OPENCHAMBER_DATA_DIR;
|
|
8
|
+
const temporaryDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'openchamber-quota-store-'));
|
|
9
|
+
process.env.OPENCHAMBER_DATA_DIR = temporaryDirectory;
|
|
10
|
+
|
|
11
|
+
describe('quota credential store', () => {
|
|
12
|
+
it('uses owner-only permissions and rejects arbitrary provider paths', () => {
|
|
13
|
+
writeQuotaCredential('ollama-cloud', { cookie: 'secret' });
|
|
14
|
+
expect(fs.statSync(path.join(temporaryDirectory, 'quota')).mode & 0o777).toBe(0o700);
|
|
15
|
+
expect(fs.statSync(path.join(temporaryDirectory, 'quota', 'ollama-cloud.json')).mode & 0o777).toBe(0o600);
|
|
16
|
+
expect(readQuotaCredential('ollama-cloud', (value) => value)).toEqual({ cookie: 'secret' });
|
|
17
|
+
expect(() => writeQuotaCredential('../escape', {})).toThrow('Unsupported credential provider');
|
|
18
|
+
deleteQuotaCredential('ollama-cloud');
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterAll(() => {
|
|
23
|
+
if (previousDataDir === undefined) delete process.env.OPENCHAMBER_DATA_DIR;
|
|
24
|
+
else process.env.OPENCHAMBER_DATA_DIR = previousDataDir;
|
|
25
|
+
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
|
|
26
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { deleteManagedCredential, getManagedCredentialStatus, normalizers, readManagedCredential, writeManagedCredential } from './credentials/providers.js';
|
|
2
|
+
|
|
3
|
+
export const normalizeOpenCodeGoCredential = normalizers['opencode-go'];
|
|
4
|
+
|
|
5
|
+
export const readOpenCodeGoCredential = () => readManagedCredential('opencode-go');
|
|
6
|
+
|
|
7
|
+
export const getOpenCodeGoCredentialStatus = () => getManagedCredentialStatus('opencode-go');
|
|
8
|
+
|
|
9
|
+
export const writeOpenCodeGoCredential = (value) => writeManagedCredential('opencode-go', value);
|
|
10
|
+
|
|
11
|
+
export const deleteOpenCodeGoCredential = () => deleteManagedCredential('opencode-go');
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { afterAll, afterEach, describe, expect, it } from 'bun:test';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { deleteOpenCodeGoCredential, getOpenCodeGoCredentialStatus, readOpenCodeGoCredential, writeOpenCodeGoCredential } from './opencode-go-credentials.js';
|
|
6
|
+
|
|
7
|
+
const previousDataDir = process.env.OPENCHAMBER_DATA_DIR;
|
|
8
|
+
const temporaryDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'openchamber-go-'));
|
|
9
|
+
process.env.OPENCHAMBER_DATA_DIR = temporaryDirectory;
|
|
10
|
+
|
|
11
|
+
afterEach(() => deleteOpenCodeGoCredential());
|
|
12
|
+
|
|
13
|
+
describe('OpenCode Go credential store', () => {
|
|
14
|
+
it('normalizes, masks, and stores credentials with owner-only permissions', () => {
|
|
15
|
+
const status = writeOpenCodeGoCredential({ workspaceId: ' wrk_test ', authCookie: ' auth=secret ' });
|
|
16
|
+
expect(status).toEqual({ configured: true, workspaceId: 'wrk_test', secretMasked: '••••••••' });
|
|
17
|
+
expect(readOpenCodeGoCredential()).toEqual({ workspaceId: 'wrk_test', authCookie: 'secret' });
|
|
18
|
+
expect(fs.statSync(path.join(temporaryDirectory, 'quota', 'opencode-go.json')).mode & 0o777).toBe(0o600);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('removes credentials without exposing prior values', () => {
|
|
22
|
+
writeOpenCodeGoCredential({ workspaceId: 'wrk_test', authCookie: 'secret' });
|
|
23
|
+
deleteOpenCodeGoCredential();
|
|
24
|
+
expect(getOpenCodeGoCredentialStatus()).toEqual({ configured: false });
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterAll(() => {
|
|
29
|
+
if (previousDataDir === undefined) delete process.env.OPENCHAMBER_DATA_DIR;
|
|
30
|
+
else process.env.OPENCHAMBER_DATA_DIR = previousDataDir;
|
|
31
|
+
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
|
|
32
|
+
});
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
toUsageWindow,
|
|
7
7
|
toNumber,
|
|
8
8
|
toTimestamp,
|
|
9
|
+
resolveWindowLabel,
|
|
9
10
|
formatMoney
|
|
10
11
|
} from '../utils/index.js';
|
|
11
12
|
|
|
@@ -65,16 +66,18 @@ export const fetchQuota = async () => {
|
|
|
65
66
|
|
|
66
67
|
const windows = {};
|
|
67
68
|
if (primary) {
|
|
68
|
-
|
|
69
|
+
const windowSeconds = toNumber(primary.limit_window_seconds);
|
|
70
|
+
windows[resolveWindowLabel(windowSeconds)] = toUsageWindow({
|
|
69
71
|
usedPercent: toNumber(primary.used_percent),
|
|
70
|
-
windowSeconds
|
|
72
|
+
windowSeconds,
|
|
71
73
|
resetAt: toTimestamp(primary.reset_at)
|
|
72
74
|
});
|
|
73
75
|
}
|
|
74
76
|
if (secondary) {
|
|
75
|
-
|
|
77
|
+
const windowSeconds = toNumber(secondary.limit_window_seconds);
|
|
78
|
+
windows[resolveWindowLabel(windowSeconds)] = toUsageWindow({
|
|
76
79
|
usedPercent: toNumber(secondary.used_percent),
|
|
77
|
-
windowSeconds
|
|
80
|
+
windowSeconds,
|
|
78
81
|
resetAt: toTimestamp(secondary.reset_at)
|
|
79
82
|
});
|
|
80
83
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
vi.mock('../../opencode/auth.js', () => ({
|
|
4
|
+
readAuthFile: () => ({ openai: { access: 'test-token' } }),
|
|
5
|
+
}));
|
|
6
|
+
|
|
7
|
+
import { fetchQuota } from './codex.js';
|
|
8
|
+
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
vi.unstubAllGlobals();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const mockUsage = (rateLimit) => {
|
|
14
|
+
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
|
|
15
|
+
ok: true,
|
|
16
|
+
json: async () => ({ rate_limit: rateLimit }),
|
|
17
|
+
}));
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
describe('Codex quota windows', () => {
|
|
21
|
+
it('labels a weekly-only primary window from its duration', async () => {
|
|
22
|
+
mockUsage({
|
|
23
|
+
primary_window: {
|
|
24
|
+
used_percent: 3,
|
|
25
|
+
limit_window_seconds: 604800,
|
|
26
|
+
reset_at: 1784491827,
|
|
27
|
+
},
|
|
28
|
+
secondary_window: null,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const result = await fetchQuota();
|
|
32
|
+
|
|
33
|
+
expect(result.usage.windows.weekly.usedPercent).toBe(3);
|
|
34
|
+
expect(result.usage.windows['5h']).toBeUndefined();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('labels five-hour and weekly windows from their durations', async () => {
|
|
38
|
+
mockUsage({
|
|
39
|
+
primary_window: { used_percent: 10, limit_window_seconds: 18000 },
|
|
40
|
+
secondary_window: { used_percent: 20, limit_window_seconds: 604800 },
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const result = await fetchQuota();
|
|
44
|
+
|
|
45
|
+
expect(result.usage.windows['5h'].usedPercent).toBe(10);
|
|
46
|
+
expect(result.usage.windows.weekly.usedPercent).toBe(20);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { existsSync, readFileSync
|
|
1
|
+
import { existsSync, readFileSync } from 'fs';
|
|
2
2
|
import { homedir } from 'os';
|
|
3
3
|
import { join } from 'path';
|
|
4
4
|
import { execFileSync } from 'child_process';
|
|
5
|
+
import { readManagedCredential, writeManagedCredential } from '../credentials/providers.js';
|
|
5
6
|
import {
|
|
6
7
|
buildResult,
|
|
7
8
|
formatMoney,
|
|
@@ -54,25 +55,6 @@ const readStateValue = (key) => {
|
|
|
54
55
|
}
|
|
55
56
|
};
|
|
56
57
|
|
|
57
|
-
const writeStateValue = (key, value) => {
|
|
58
|
-
if (!existsSync(STATE_DB)) return false;
|
|
59
|
-
try {
|
|
60
|
-
const escaped = String(value).replace(/'/g, "''");
|
|
61
|
-
const escapedKey = String(key).replace(/'/g, "''");
|
|
62
|
-
execFileSync('sqlite3', [
|
|
63
|
-
STATE_DB,
|
|
64
|
-
`INSERT OR REPLACE INTO ItemTable (key, value) VALUES ('${escapedKey}', '${escaped}');`
|
|
65
|
-
], {
|
|
66
|
-
encoding: 'utf8',
|
|
67
|
-
windowsHide: true,
|
|
68
|
-
stdio: ['ignore', 'ignore', 'ignore']
|
|
69
|
-
});
|
|
70
|
-
return true;
|
|
71
|
-
} catch {
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
|
|
76
58
|
const readFileToken = (path) => {
|
|
77
59
|
try {
|
|
78
60
|
if (!path || !existsSync(path)) return null;
|
|
@@ -83,16 +65,6 @@ const readFileToken = (path) => {
|
|
|
83
65
|
}
|
|
84
66
|
};
|
|
85
67
|
|
|
86
|
-
const writeFileToken = (path, value) => {
|
|
87
|
-
try {
|
|
88
|
-
if (!path) return false;
|
|
89
|
-
writeFileSync(path, `${value}\n`, { encoding: 'utf8', mode: 0o600 });
|
|
90
|
-
return true;
|
|
91
|
-
} catch {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
68
|
const loadAuthState = () => {
|
|
97
69
|
const envAccessToken = process.env.CURSOR_TOKEN || process.env.CURSOR_ACCESS_TOKEN || null;
|
|
98
70
|
const envRefreshToken = process.env.CURSOR_REFRESH_TOKEN || null;
|
|
@@ -113,15 +85,15 @@ const loadAuthState = () => {
|
|
|
113
85
|
return {
|
|
114
86
|
accessToken: fileAccessToken,
|
|
115
87
|
refreshToken: fileRefreshToken,
|
|
116
|
-
source: 'file'
|
|
117
|
-
accessTokenPath
|
|
88
|
+
source: 'file'
|
|
118
89
|
};
|
|
119
90
|
}
|
|
120
91
|
|
|
92
|
+
const managed = readManagedCredential(providerId);
|
|
121
93
|
return {
|
|
122
|
-
accessToken:
|
|
123
|
-
refreshToken:
|
|
124
|
-
source: '
|
|
94
|
+
accessToken: managed?.accessToken || null,
|
|
95
|
+
refreshToken: managed?.refreshToken || null,
|
|
96
|
+
source: 'managed'
|
|
125
97
|
};
|
|
126
98
|
};
|
|
127
99
|
|
|
@@ -133,8 +105,18 @@ const tokenNeedsRefresh = (token) => {
|
|
|
133
105
|
};
|
|
134
106
|
|
|
135
107
|
const persistAccessToken = (auth, accessToken) => {
|
|
136
|
-
if (auth.source === '
|
|
137
|
-
|
|
108
|
+
if (auth.source === 'managed') writeManagedCredential(providerId, { accessToken, refreshToken: auth.refreshToken || '' });
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export const importCursorCredential = async () => {
|
|
112
|
+
const credential = {
|
|
113
|
+
accessToken: readStateValue('cursorAuth/accessToken') || '',
|
|
114
|
+
refreshToken: readStateValue('cursorAuth/refreshToken') || '',
|
|
115
|
+
};
|
|
116
|
+
if (!credential.accessToken && !credential.refreshToken) throw new Error('Cursor credentials are unavailable');
|
|
117
|
+
const accessToken = await resolveCredentialAccessToken({ ...credential, source: 'import' });
|
|
118
|
+
if (!accessToken) throw new Error('Cursor credentials are invalid');
|
|
119
|
+
return writeManagedCredential(providerId, { ...credential, accessToken });
|
|
138
120
|
};
|
|
139
121
|
|
|
140
122
|
const refreshAccessToken = async (auth) => {
|
|
@@ -165,13 +147,20 @@ const refreshAccessToken = async (auth) => {
|
|
|
165
147
|
return body.access_token;
|
|
166
148
|
};
|
|
167
149
|
|
|
168
|
-
const
|
|
169
|
-
const auth = loadAuthState();
|
|
150
|
+
const resolveCredentialAccessToken = async (auth) => {
|
|
170
151
|
if (!auth.accessToken && !auth.refreshToken) return null;
|
|
171
152
|
if (!tokenNeedsRefresh(auth.accessToken)) return auth.accessToken;
|
|
172
153
|
return refreshAccessToken(auth);
|
|
173
154
|
};
|
|
174
155
|
|
|
156
|
+
const resolveAccessToken = async () => resolveCredentialAccessToken(loadAuthState());
|
|
157
|
+
|
|
158
|
+
export const validateCursorCredential = async (credential) => {
|
|
159
|
+
const accessToken = await resolveCredentialAccessToken({ ...credential, source: 'validation' });
|
|
160
|
+
if (!accessToken) throw new Error('Cursor credentials are invalid');
|
|
161
|
+
await connectPost(USAGE_URL, accessToken);
|
|
162
|
+
};
|
|
163
|
+
|
|
175
164
|
const connectPost = async (url, accessToken) => {
|
|
176
165
|
const response = await fetch(url, {
|
|
177
166
|
method: 'POST',
|
|
@@ -22,6 +22,7 @@ import * as minimaxCodingPlan from './minimax-coding-plan.js';
|
|
|
22
22
|
import * as minimaxCnCodingPlan from './minimax-cn-coding-plan.js';
|
|
23
23
|
import * as ollamaCloud from './ollama-cloud.js';
|
|
24
24
|
import * as wafer from './wafer.js';
|
|
25
|
+
import * as opencodeGo from './opencode-go.js';
|
|
25
26
|
|
|
26
27
|
const registry = {
|
|
27
28
|
claude: {
|
|
@@ -113,6 +114,12 @@ const registry = {
|
|
|
113
114
|
providerName: wafer.providerName,
|
|
114
115
|
isConfigured: wafer.isConfigured,
|
|
115
116
|
fetchQuota: wafer.fetchQuota
|
|
117
|
+
},
|
|
118
|
+
'opencode-go': {
|
|
119
|
+
providerId: opencodeGo.providerId,
|
|
120
|
+
providerName: opencodeGo.providerName,
|
|
121
|
+
isConfigured: opencodeGo.isConfigured,
|
|
122
|
+
fetchQuota: opencodeGo.fetchQuota
|
|
116
123
|
}
|
|
117
124
|
};
|
|
118
125
|
|
|
@@ -1,26 +1,11 @@
|
|
|
1
|
-
import { homedir } from 'os';
|
|
2
|
-
import { readFileSync, existsSync } from 'fs';
|
|
3
|
-
import { join } from 'path';
|
|
4
1
|
import { buildResult, toUsageWindow, toNumber } from '../utils/index.js';
|
|
5
|
-
|
|
6
|
-
const COOKIE_PATH = join(homedir(), '.config', 'ollama-quota', 'cookie');
|
|
2
|
+
import { readManagedCredential } from '../credentials/providers.js';
|
|
7
3
|
|
|
8
4
|
export const providerId = 'ollama-cloud';
|
|
9
5
|
export const providerName = 'Ollama Cloud';
|
|
10
6
|
const aliases = ['ollama-cloud', 'ollamacloud'];
|
|
11
7
|
|
|
12
|
-
const
|
|
13
|
-
try {
|
|
14
|
-
if (!existsSync(COOKIE_PATH)) return null;
|
|
15
|
-
const content = readFileSync(COOKIE_PATH, 'utf-8');
|
|
16
|
-
const trimmed = content.trim();
|
|
17
|
-
return trimmed || null;
|
|
18
|
-
} catch {
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const parseOllamaSettingsHtml = (html) => {
|
|
8
|
+
export const parseOllamaSettingsHtml = (html) => {
|
|
24
9
|
const windows = {};
|
|
25
10
|
const sessionMatch = html.match(/Session\s+usage[^0-9]*([0-9.]+)%/i);
|
|
26
11
|
if (sessionMatch) {
|
|
@@ -54,14 +39,29 @@ const parseOllamaSettingsHtml = (html) => {
|
|
|
54
39
|
};
|
|
55
40
|
|
|
56
41
|
export const isConfigured = () => {
|
|
57
|
-
|
|
58
|
-
|
|
42
|
+
return Boolean(readManagedCredential(providerId));
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const fetchOllamaCloudUsage = async (credential, fetchImpl = fetch) => {
|
|
46
|
+
const response = await fetchImpl('https://ollama.com/settings', {
|
|
47
|
+
method: 'GET',
|
|
48
|
+
headers: { Cookie: credential.cookie, 'User-Agent': 'OpenChamber quota provider' },
|
|
49
|
+
redirect: 'manual',
|
|
50
|
+
signal: AbortSignal.timeout(15_000),
|
|
51
|
+
});
|
|
52
|
+
if (response.status === 401 || response.status === 403 || (response.status >= 300 && response.status < 400)) {
|
|
53
|
+
throw new Error('Ollama Cloud authentication failed');
|
|
54
|
+
}
|
|
55
|
+
if (!response.ok) throw new Error(`Ollama Cloud returned HTTP ${response.status}`);
|
|
56
|
+
const windows = parseOllamaSettingsHtml(await response.text());
|
|
57
|
+
if (Object.keys(windows).length === 0) throw new Error('Ollama Cloud usage data could not be parsed');
|
|
58
|
+
return windows;
|
|
59
59
|
};
|
|
60
60
|
|
|
61
61
|
export const fetchQuota = async () => {
|
|
62
|
-
const
|
|
62
|
+
const credential = readManagedCredential(providerId);
|
|
63
63
|
|
|
64
|
-
if (!
|
|
64
|
+
if (!credential) {
|
|
65
65
|
return buildResult({
|
|
66
66
|
providerId,
|
|
67
67
|
providerName,
|
|
@@ -72,26 +72,7 @@ export const fetchQuota = async () => {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
try {
|
|
75
|
-
const
|
|
76
|
-
method: 'GET',
|
|
77
|
-
headers: {
|
|
78
|
-
Cookie: cookie,
|
|
79
|
-
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
if (!response.ok) {
|
|
84
|
-
return buildResult({
|
|
85
|
-
providerId,
|
|
86
|
-
providerName,
|
|
87
|
-
ok: false,
|
|
88
|
-
configured: true,
|
|
89
|
-
error: `API error: ${response.status}`
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const html = await response.text();
|
|
94
|
-
const windows = parseOllamaSettingsHtml(html);
|
|
75
|
+
const windows = await fetchOllamaCloudUsage(credential);
|
|
95
76
|
|
|
96
77
|
return buildResult({
|
|
97
78
|
providerId,
|
|
@@ -109,4 +90,4 @@ export const fetchQuota = async () => {
|
|
|
109
90
|
error: error instanceof Error ? error.message : 'Request failed'
|
|
110
91
|
});
|
|
111
92
|
}
|
|
112
|
-
};
|
|
93
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { fetchOllamaCloudUsage } from './ollama-cloud.js';
|
|
3
|
+
|
|
4
|
+
describe('Ollama Cloud quota provider', () => {
|
|
5
|
+
it('rejects redirects without forwarding credentials', async () => {
|
|
6
|
+
await expect(fetchOllamaCloudUsage({ cookie: 'session=secret' }, async () => new Response('', { status: 302 }))).rejects.toThrow('authentication failed');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('rejects successful pages without usage data', async () => {
|
|
10
|
+
await expect(fetchOllamaCloudUsage({ cookie: 'session=secret' }, async () => new Response('<html></html>'))).rejects.toThrow('could not be parsed');
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { readOpenCodeGoCredential } from '../opencode-go-credentials.js';
|
|
2
|
+
import { buildResult, toUsageWindow } from '../utils/index.js';
|
|
3
|
+
|
|
4
|
+
export const providerId = 'opencode-go';
|
|
5
|
+
export const providerName = 'OpenCode Go';
|
|
6
|
+
export const aliases = ['opencode-go'];
|
|
7
|
+
|
|
8
|
+
const patterns = {
|
|
9
|
+
'5h': 'rollingUsage',
|
|
10
|
+
weekly: 'weeklyUsage',
|
|
11
|
+
monthly: 'monthlyUsage',
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const captureNumber = (name, body) => {
|
|
15
|
+
const match = body.match(new RegExp(`["']?${name}["']?\\s*:\\s*["']?(-?\\d+(?:\\.\\d+)?)`));
|
|
16
|
+
const value = match ? Number(match[1]) : null;
|
|
17
|
+
return Number.isFinite(value) ? value : null;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const parseOpenCodeGoUsage = (html, now = Date.now()) => {
|
|
21
|
+
if (typeof html !== 'string') return {};
|
|
22
|
+
const normalized = html.replaceAll('"', '"').replaceAll('"', '"').replaceAll('\\u0022', '"').replaceAll('\\"', '"');
|
|
23
|
+
const windows = {};
|
|
24
|
+
for (const [key, field] of Object.entries(patterns)) {
|
|
25
|
+
const escaped = field.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
26
|
+
const match = normalized.match(new RegExp(`["']?${escaped}["']?\\s*:\\s*(?:\\$R\\[\\d+\\]\\s*=\\s*)?\\{([^{}]*)\\}`, 's'));
|
|
27
|
+
if (!match) continue;
|
|
28
|
+
const usedPercent = captureNumber('usagePercent', match[1]);
|
|
29
|
+
const resetInSec = captureNumber('resetInSec', match[1]);
|
|
30
|
+
if (usedPercent === null || resetInSec === null) continue;
|
|
31
|
+
windows[key] = toUsageWindow({
|
|
32
|
+
usedPercent: Math.min(100, Math.max(0, usedPercent)),
|
|
33
|
+
resetAt: now + Math.max(0, resetInSec) * 1000,
|
|
34
|
+
windowSeconds: null,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return windows;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const fetchOpenCodeGoUsage = async (credential, fetchImpl = fetch) => {
|
|
41
|
+
const response = await fetchImpl(`https://opencode.ai/workspace/${encodeURIComponent(credential.workspaceId)}/go`, {
|
|
42
|
+
headers: {
|
|
43
|
+
Accept: 'text/html,application/xhtml+xml',
|
|
44
|
+
Cookie: `auth=${credential.authCookie}`,
|
|
45
|
+
'User-Agent': 'OpenChamber quota provider',
|
|
46
|
+
},
|
|
47
|
+
redirect: 'manual',
|
|
48
|
+
signal: AbortSignal.timeout(15_000),
|
|
49
|
+
});
|
|
50
|
+
if (response.status === 401 || response.status === 403 || (response.status >= 300 && response.status < 400)) {
|
|
51
|
+
throw new Error('OpenCode Go authentication failed');
|
|
52
|
+
}
|
|
53
|
+
if (!response.ok) throw new Error(`OpenCode Go dashboard returned HTTP ${response.status}`);
|
|
54
|
+
const windows = parseOpenCodeGoUsage(await response.text());
|
|
55
|
+
if (Object.keys(windows).length === 0) throw new Error('OpenCode Go usage data could not be parsed');
|
|
56
|
+
return windows;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const isConfigured = () => Boolean(readOpenCodeGoCredential());
|
|
60
|
+
|
|
61
|
+
export const fetchQuota = async () => {
|
|
62
|
+
const credential = readOpenCodeGoCredential();
|
|
63
|
+
if (!credential) return buildResult({ providerId, providerName, ok: false, configured: false, error: 'Not configured' });
|
|
64
|
+
try {
|
|
65
|
+
const windows = await fetchOpenCodeGoUsage(credential);
|
|
66
|
+
return buildResult({ providerId, providerName, ok: true, configured: true, usage: { windows } });
|
|
67
|
+
} catch (error) {
|
|
68
|
+
return buildResult({ providerId, providerName, ok: false, configured: true, error: error instanceof Error ? error.message : 'Request failed' });
|
|
69
|
+
}
|
|
70
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { fetchOpenCodeGoUsage, parseOpenCodeGoUsage } from './opencode-go.js';
|
|
3
|
+
|
|
4
|
+
describe('OpenCode Go quota provider', () => {
|
|
5
|
+
it('parses partial SSR usage windows in either field order', () => {
|
|
6
|
+
const windows = parseOpenCodeGoUsage('rollingUsage:$R[1]={usagePercent:25,resetInSec:60} weeklyUsage:$R[2]={resetInSec:120,usagePercent:40}', 1_000);
|
|
7
|
+
expect(windows['5h'].usedPercent).toBe(25);
|
|
8
|
+
expect(windows['5h'].resetAt).toBe(61_000);
|
|
9
|
+
expect(windows.weekly.usedPercent).toBe(40);
|
|
10
|
+
expect(windows.monthly).toBeUndefined();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('does not expose credentials in authentication errors', async () => {
|
|
14
|
+
const credential = { workspaceId: 'wrk_test', authCookie: 'secret' };
|
|
15
|
+
await expect(fetchOpenCodeGoUsage(credential, async () => new Response('', { status: 403 }))).rejects.toThrow('authentication failed');
|
|
16
|
+
});
|
|
17
|
+
});
|