zcode-acp-server 0.73.0 → 0.75.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/README.md +1 -1
- package/dist/bin/quota.d.ts.map +1 -1
- package/dist/bin/quota.js +11 -6
- package/dist/bin/quota.js.map +1 -1
- package/dist/code-fingerprint.json +1 -1
- package/dist/config/user-config.d.ts +5 -1
- package/dist/config/user-config.d.ts.map +1 -1
- package/dist/config/user-config.js +1 -0
- package/dist/config/user-config.js.map +1 -1
- package/dist/handlers/replay.d.ts.map +1 -1
- package/dist/handlers/replay.js +7 -0
- package/dist/handlers/replay.js.map +1 -1
- package/dist/handlers/session.d.ts +16 -0
- package/dist/handlers/session.d.ts.map +1 -1
- package/dist/handlers/session.js +21 -14
- package/dist/handlers/session.js.map +1 -1
- package/dist/quota/cliproxy/client.d.ts.map +1 -1
- package/dist/quota/cliproxy/client.js +10 -4
- package/dist/quota/cliproxy/client.js.map +1 -1
- package/dist/quota/opencode-go/client.d.ts +28 -7
- package/dist/quota/opencode-go/client.d.ts.map +1 -1
- package/dist/quota/opencode-go/client.js +39 -7
- package/dist/quota/opencode-go/client.js.map +1 -1
- package/dist/quota/opencode-go/config.d.ts +12 -7
- package/dist/quota/opencode-go/config.d.ts.map +1 -1
- package/dist/quota/opencode-go/config.js +12 -7
- package/dist/quota/opencode-go/config.js.map +1 -1
- package/dist/quota/opencode-go/format.js +1 -1
- package/dist/quota/opencode-go/format.js.map +1 -1
- package/dist/quota/opencode-go/index.d.ts +18 -10
- package/dist/quota/opencode-go/index.d.ts.map +1 -1
- package/dist/quota/opencode-go/index.js +105 -63
- package/dist/quota/opencode-go/index.js.map +1 -1
- package/dist/quota/opencode-go/parse.d.ts +10 -0
- package/dist/quota/opencode-go/parse.d.ts.map +1 -1
- package/dist/quota/opencode-go/parse.js +41 -0
- package/dist/quota/opencode-go/parse.js.map +1 -1
- package/dist/remote/hub-server.d.ts.map +1 -1
- package/dist/remote/hub-server.js +68 -26
- package/dist/remote/hub-server.js.map +1 -1
- package/dist/remote/session-close-endpoint.d.ts +8 -0
- package/dist/remote/session-close-endpoint.d.ts.map +1 -1
- package/dist/remote/session-close-endpoint.js +62 -4
- package/dist/remote/session-close-endpoint.js.map +1 -1
- package/docs/BACKLOG.md +34 -0
- package/docs/CLI.md +31 -28
- package/docs/adr/0006-remote-session-close.md +14 -0
- package/package.json +1 -1
|
@@ -1,19 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Opencode Go status HTTP client.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* URL now bounces EVERY cookie to the login page, observed 2026-09-19) onto
|
|
6
|
-
* the console at `https://opencode.ai/console/<org>/go`, backed by this JSON
|
|
7
|
-
* API. Usage arrives as micro-cents meter pairs per window; the parser turns
|
|
8
|
-
* them into percent + countdown.
|
|
4
|
+
* Two sources, in preference order (the orchestrator picks):
|
|
9
5
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
6
|
+
* 1. The console status API (`/console/api/go/status`) — cookie auth, reports
|
|
7
|
+
* usage in 0.1-percent steps. The Go dashboard moved off the legacy SSR
|
|
8
|
+
* page (`/workspace/<id>/go` — that URL now bounces EVERY cookie to the
|
|
9
|
+
* login page, observed 2026-09-19) onto the console at
|
|
10
|
+
* `https://opencode.ai/console/<org>/go`, backed by this JSON API. Usage
|
|
11
|
+
* arrives as micro-cents meter pairs per window; the parser turns them
|
|
12
|
+
* into percent + countdown.
|
|
13
|
+
*
|
|
14
|
+
* 2. The public usage API (`/zen/go/v1/usage`, opencode PR #16513) — a single
|
|
15
|
+
* service-account API key (`oc_sk_…`) as a Bearer token, no cookies, no
|
|
16
|
+
* org header. Reports integer percents + absolute reset timestamps; the
|
|
17
|
+
* cookie source keeps 0.1 precision, so it stays preferred and this is the
|
|
18
|
+
* fallback for setups without (or with expired) cookies.
|
|
19
|
+
*
|
|
20
|
+
* Console auth needs THREE pieces (verified against the live API 2026-09-19):
|
|
21
|
+
* the `auth` cookie, the `__Host-console_session` cookie, and the `x-org-id`
|
|
12
22
|
* header. Missing session cookie → 401, missing org header → 400; any
|
|
13
23
|
* User-Agent works (one is sent for parity with the console's own requests).
|
|
14
24
|
*/
|
|
15
25
|
/** The console status API endpoint (the org rides the `x-org-id` header). */
|
|
16
26
|
export declare function goStatusUrl(): string;
|
|
27
|
+
/** The public usage API endpoint (key auth; the console /zen path proxies it). */
|
|
28
|
+
export declare function zenUsageUrl(): string;
|
|
17
29
|
/**
|
|
18
30
|
* Fetch the Go status JSON.
|
|
19
31
|
*
|
|
@@ -26,4 +38,13 @@ export declare function fetchGoStatus(workspaceId: string, authCookie: string, s
|
|
|
26
38
|
status: number;
|
|
27
39
|
text: string;
|
|
28
40
|
}>;
|
|
41
|
+
/**
|
|
42
|
+
* Fetch the public usage JSON with an API key. Same no-throw contract as
|
|
43
|
+
* {@link fetchGoStatus}: network errors throw (→ `unavailable`), auth
|
|
44
|
+
* failures come back as HTTP 401.
|
|
45
|
+
*/
|
|
46
|
+
export declare function fetchZenUsage(apiKey: string, fetchImpl?: typeof globalThis.fetch): Promise<{
|
|
47
|
+
status: number;
|
|
48
|
+
text: string;
|
|
49
|
+
}>;
|
|
29
50
|
//# sourceMappingURL=client.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/quota/opencode-go/client.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/quota/opencode-go/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAQH,6EAA6E;AAC7E,wBAAgB,WAAW,IAAI,MAAM,CAEpC;AAED,kFAAkF;AAClF,wBAAgB,WAAW,IAAI,MAAM,CAEpC;AAED;;;;;;;GAOG;AACH,wBAAsB,aAAa,CACjC,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,EACpB,SAAS,GAAE,OAAO,UAAU,CAAC,KAAwB,GACpD,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CAc3C;AAED;;;;GAIG;AACH,wBAAsB,aAAa,CACjC,MAAM,EAAE,MAAM,EACd,SAAS,GAAE,OAAO,UAAU,CAAC,KAAwB,GACpD,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CAY3C"}
|
|
@@ -1,14 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Opencode Go status HTTP client.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* URL now bounces EVERY cookie to the login page, observed 2026-09-19) onto
|
|
6
|
-
* the console at `https://opencode.ai/console/<org>/go`, backed by this JSON
|
|
7
|
-
* API. Usage arrives as micro-cents meter pairs per window; the parser turns
|
|
8
|
-
* them into percent + countdown.
|
|
4
|
+
* Two sources, in preference order (the orchestrator picks):
|
|
9
5
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
6
|
+
* 1. The console status API (`/console/api/go/status`) — cookie auth, reports
|
|
7
|
+
* usage in 0.1-percent steps. The Go dashboard moved off the legacy SSR
|
|
8
|
+
* page (`/workspace/<id>/go` — that URL now bounces EVERY cookie to the
|
|
9
|
+
* login page, observed 2026-09-19) onto the console at
|
|
10
|
+
* `https://opencode.ai/console/<org>/go`, backed by this JSON API. Usage
|
|
11
|
+
* arrives as micro-cents meter pairs per window; the parser turns them
|
|
12
|
+
* into percent + countdown.
|
|
13
|
+
*
|
|
14
|
+
* 2. The public usage API (`/zen/go/v1/usage`, opencode PR #16513) — a single
|
|
15
|
+
* service-account API key (`oc_sk_…`) as a Bearer token, no cookies, no
|
|
16
|
+
* org header. Reports integer percents + absolute reset timestamps; the
|
|
17
|
+
* cookie source keeps 0.1 precision, so it stays preferred and this is the
|
|
18
|
+
* fallback for setups without (or with expired) cookies.
|
|
19
|
+
*
|
|
20
|
+
* Console auth needs THREE pieces (verified against the live API 2026-09-19):
|
|
21
|
+
* the `auth` cookie, the `__Host-console_session` cookie, and the `x-org-id`
|
|
12
22
|
* header. Missing session cookie → 401, missing org header → 400; any
|
|
13
23
|
* User-Agent works (one is sent for parity with the console's own requests).
|
|
14
24
|
*/
|
|
@@ -20,6 +30,10 @@ const USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Gecko/201001
|
|
|
20
30
|
export function goStatusUrl() {
|
|
21
31
|
return "https://opencode.ai/console/api/go/status";
|
|
22
32
|
}
|
|
33
|
+
/** The public usage API endpoint (key auth; the console /zen path proxies it). */
|
|
34
|
+
export function zenUsageUrl() {
|
|
35
|
+
return "https://opencode.ai/zen/go/v1/usage";
|
|
36
|
+
}
|
|
23
37
|
/**
|
|
24
38
|
* Fetch the Go status JSON.
|
|
25
39
|
*
|
|
@@ -43,4 +57,22 @@ export async function fetchGoStatus(workspaceId, authCookie, sessionToken, fetch
|
|
|
43
57
|
const text = await resp.text();
|
|
44
58
|
return { status: resp.status, text };
|
|
45
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Fetch the public usage JSON with an API key. Same no-throw contract as
|
|
62
|
+
* {@link fetchGoStatus}: network errors throw (→ `unavailable`), auth
|
|
63
|
+
* failures come back as HTTP 401.
|
|
64
|
+
*/
|
|
65
|
+
export async function fetchZenUsage(apiKey, fetchImpl = globalThis.fetch) {
|
|
66
|
+
const resp = await fetchImpl(zenUsageUrl(), {
|
|
67
|
+
method: "GET",
|
|
68
|
+
headers: {
|
|
69
|
+
Authorization: `Bearer ${apiKey}`,
|
|
70
|
+
Accept: "application/json",
|
|
71
|
+
"User-Agent": USER_AGENT,
|
|
72
|
+
},
|
|
73
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
74
|
+
});
|
|
75
|
+
const text = await resp.text();
|
|
76
|
+
return { status: resp.status, text };
|
|
77
|
+
}
|
|
46
78
|
//# sourceMappingURL=client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/quota/opencode-go/client.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/quota/opencode-go/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,4BAA4B;AAC5B,MAAM,UAAU,GAAG,MAAM,CAAC;AAE1B,yEAAyE;AACzE,MAAM,UAAU,GAAG,8EAA8E,CAAC;AAElG,6EAA6E;AAC7E,MAAM,UAAU,WAAW;IACzB,OAAO,2CAA2C,CAAC;AACrD,CAAC;AAED,kFAAkF;AAClF,MAAM,UAAU,WAAW;IACzB,OAAO,qCAAqC,CAAC;AAC/C,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,WAAmB,EACnB,UAAkB,EAClB,YAAoB,EACpB,YAAqC,UAAU,CAAC,KAAK;IAErD,MAAM,IAAI,GAAG,MAAM,SAAS,CAAC,WAAW,EAAE,EAAE;QAC1C,MAAM,EAAE,KAAK;QACb,OAAO,EAAE;YACP,MAAM,EAAE,QAAQ,UAAU,4BAA4B,YAAY,EAAE;YACpE,UAAU,EAAE,WAAW;YACvB,MAAM,EAAE,kBAAkB;YAC1B,YAAY,EAAE,UAAU;SACzB;QACD,QAAQ,EAAE,QAAQ;QAClB,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,UAAU,CAAC;KACxC,CAAC,CAAC;IACH,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC;IAC/B,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC;AACvC,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,MAAc,EACd,YAAqC,UAAU,CAAC,KAAK;IAErD,MAAM,IAAI,GAAG,MAAM,SAAS,CAAC,WAAW,EAAE,EAAE;QAC1C,MAAM,EAAE,KAAK;QACb,OAAO,EAAE;YACP,aAAa,EAAE,UAAU,MAAM,EAAE;YACjC,MAAM,EAAE,kBAAkB;YAC1B,YAAY,EAAE,UAAU;SACzB;QACD,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,UAAU,CAAC;KACxC,CAAC,CAAC;IACH,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC;IAC/B,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC;AACvC,CAAC"}
|
|
@@ -4,23 +4,28 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Full precedence (highest first), resolved in ../index.ts:
|
|
6
6
|
* 1. `quota.opencodeGoWorkspaceId` / `opencodeGoAuthCookie` /
|
|
7
|
-
* `opencodeGoSessionToken`
|
|
8
|
-
* config).
|
|
7
|
+
* `opencodeGoSessionToken` / `opencodeGoApiKey` in
|
|
8
|
+
* `~/.config/zcode-acp/config.json` (our own config).
|
|
9
9
|
* 2. `OPENCODE_GO_WORKSPACE_ID` / `OPENCODE_GO_AUTH_COOKIE` /
|
|
10
|
-
* `OPENCODE_GO_SESSION_TOKEN` env vars (best for
|
|
11
|
-
* temporary overrides).
|
|
10
|
+
* `OPENCODE_GO_SESSION_TOKEN` / `OPENCODE_GO_API_KEY` env vars (best for
|
|
11
|
+
* CI / scripts / temporary overrides).
|
|
12
12
|
* 3. `~/.pi/agent/opencode-go.json` —
|
|
13
13
|
* `{ workspaceId, authCookie, sessionToken }` (the convention used by
|
|
14
14
|
* the @beyona/pi-zai-usage Pi extension, kept so users who already
|
|
15
|
-
* configured it there get reuse for free).
|
|
15
|
+
* configured it there get reuse for free). No API-key field here — the
|
|
16
|
+
* key postdates that convention.
|
|
16
17
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
18
|
+
* The cookie triple and the API key are two SOURCES for the same data: the
|
|
19
|
+
* triple drives the console status API (0.1-percent precision), the key the
|
|
20
|
+
* public usage API (integer percents, cookie-free). A missing/invalid
|
|
21
|
+
* credential on its own does not yield `not_configured` when the other
|
|
22
|
+
* source resolves — see ../index.ts.
|
|
19
23
|
*/
|
|
20
24
|
/** Env var names — documented in the CLI help and README. */
|
|
21
25
|
export declare const ENV_WORKSPACE_ID = "OPENCODE_GO_WORKSPACE_ID";
|
|
22
26
|
export declare const ENV_AUTH_COOKIE = "OPENCODE_GO_AUTH_COOKIE";
|
|
23
27
|
export declare const ENV_SESSION_TOKEN = "OPENCODE_GO_SESSION_TOKEN";
|
|
28
|
+
export declare const ENV_API_KEY = "OPENCODE_GO_API_KEY";
|
|
24
29
|
/** Config file path (matches the @beyona/pi-zai-usage convention). */
|
|
25
30
|
export declare const CONFIG_PATH: string;
|
|
26
31
|
/** Shape of the JSON config file. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/quota/opencode-go/config.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/quota/opencode-go/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAQH,6DAA6D;AAC7D,eAAO,MAAM,gBAAgB,6BAA6B,CAAC;AAC3D,eAAO,MAAM,eAAe,4BAA4B,CAAC;AACzD,eAAO,MAAM,iBAAiB,8BAA8B,CAAC;AAC7D,eAAO,MAAM,WAAW,wBAAwB,CAAC;AAEjD,sEAAsE;AACtE,eAAO,MAAM,WAAW,QAKvB,CAAC;AAEF,qCAAqC;AACrC,UAAU,gBAAgB;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,QAAQ,GAAE,MAAoB,GAAG,gBAAgB,CAuB/E"}
|
|
@@ -4,18 +4,22 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Full precedence (highest first), resolved in ../index.ts:
|
|
6
6
|
* 1. `quota.opencodeGoWorkspaceId` / `opencodeGoAuthCookie` /
|
|
7
|
-
* `opencodeGoSessionToken`
|
|
8
|
-
* config).
|
|
7
|
+
* `opencodeGoSessionToken` / `opencodeGoApiKey` in
|
|
8
|
+
* `~/.config/zcode-acp/config.json` (our own config).
|
|
9
9
|
* 2. `OPENCODE_GO_WORKSPACE_ID` / `OPENCODE_GO_AUTH_COOKIE` /
|
|
10
|
-
* `OPENCODE_GO_SESSION_TOKEN` env vars (best for
|
|
11
|
-
* temporary overrides).
|
|
10
|
+
* `OPENCODE_GO_SESSION_TOKEN` / `OPENCODE_GO_API_KEY` env vars (best for
|
|
11
|
+
* CI / scripts / temporary overrides).
|
|
12
12
|
* 3. `~/.pi/agent/opencode-go.json` —
|
|
13
13
|
* `{ workspaceId, authCookie, sessionToken }` (the convention used by
|
|
14
14
|
* the @beyona/pi-zai-usage Pi extension, kept so users who already
|
|
15
|
-
* configured it there get reuse for free).
|
|
15
|
+
* configured it there get reuse for free). No API-key field here — the
|
|
16
|
+
* key postdates that convention.
|
|
16
17
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
18
|
+
* The cookie triple and the API key are two SOURCES for the same data: the
|
|
19
|
+
* triple drives the console status API (0.1-percent precision), the key the
|
|
20
|
+
* public usage API (integer percents, cookie-free). A missing/invalid
|
|
21
|
+
* credential on its own does not yield `not_configured` when the other
|
|
22
|
+
* source resolves — see ../index.ts.
|
|
19
23
|
*/
|
|
20
24
|
import { readFileSync } from "node:fs";
|
|
21
25
|
import path from "node:path";
|
|
@@ -25,6 +29,7 @@ import { log } from "../../utils.js";
|
|
|
25
29
|
export const ENV_WORKSPACE_ID = "OPENCODE_GO_WORKSPACE_ID";
|
|
26
30
|
export const ENV_AUTH_COOKIE = "OPENCODE_GO_AUTH_COOKIE";
|
|
27
31
|
export const ENV_SESSION_TOKEN = "OPENCODE_GO_SESSION_TOKEN";
|
|
32
|
+
export const ENV_API_KEY = "OPENCODE_GO_API_KEY";
|
|
28
33
|
/** Config file path (matches the @beyona/pi-zai-usage convention). */
|
|
29
34
|
export const CONFIG_PATH = path.join(process.env.HOME || process.env.USERPROFILE || "~", ".pi", "agent", "opencode-go.json");
|
|
30
35
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../../src/quota/opencode-go/config.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../../src/quota/opencode-go/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAErC,6DAA6D;AAC7D,MAAM,CAAC,MAAM,gBAAgB,GAAG,0BAA0B,CAAC;AAC3D,MAAM,CAAC,MAAM,eAAe,GAAG,yBAAyB,CAAC;AACzD,MAAM,CAAC,MAAM,iBAAiB,GAAG,2BAA2B,CAAC;AAC7D,MAAM,CAAC,MAAM,WAAW,GAAG,qBAAqB,CAAC;AAEjD,sEAAsE;AACtE,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAClC,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,GAAG,EAClD,KAAK,EACL,OAAO,EACP,kBAAkB,CACnB,CAAC;AASF;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,WAAmB,WAAW;IAC3D,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAC3C,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAY,CAAC;QAC1C,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YACnE,GAAG,CAAC,kDAAkD,QAAQ,GAAG,CAAC,CAAC;YACnE,OAAO,EAAE,CAAC;QACZ,CAAC;QACD,MAAM,GAAG,GAAG,MAAiC,CAAC;QAC9C,OAAO;YACL,WAAW,EAAE,OAAO,GAAG,CAAC,WAAW,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS;YAC9E,UAAU,EAAE,OAAO,GAAG,CAAC,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS;YAC3E,YAAY,EAAE,OAAO,GAAG,CAAC,YAAY,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC,SAAS;SAClF,CAAC;IACJ,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,uEAAuE;QACvE,oDAAoD;QACpD,MAAM,GAAG,GAAG,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACvD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YACxB,GAAG,CAAC,oCAAoC,QAAQ,MAAM,GAAG,EAAE,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC"}
|
|
@@ -57,7 +57,7 @@ export function formatGoSection(result, windows, now = Date.now(), color = false
|
|
|
57
57
|
const msg = result.kind === "not_configured"
|
|
58
58
|
? "not configured (set quota.opencodeGo* or OPENCODE_GO_* — see zcode-acp quota --help)"
|
|
59
59
|
: result.kind === "auth_error"
|
|
60
|
-
? "auth expired — refresh your opencode.ai cookie + session token"
|
|
60
|
+
? "auth expired — refresh your opencode.ai cookie + session token, or set quota.opencodeGoApiKey"
|
|
61
61
|
: "unavailable";
|
|
62
62
|
return { header, body: [msg] };
|
|
63
63
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.js","sourceRoot":"","sources":["../../../src/quota/opencode-go/format.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAG5C,qDAAqD;AACrD,MAAM,WAAW,GAA+C;IAC9D,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE;IAC/B,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE;IAChC,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE;CACnC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,GAAW;IACxC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,GAAG,GAAG,EAAE;QAAE,OAAO,KAAK,CAAC;IACpD,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC1B,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;IACpC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC;IAC/C,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC;IAC1C,IAAI,IAAI,GAAG,CAAC;Q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,mEAAmE;QACnE,MAAM,GAAG,GAAG,UAAU,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;QACvC,yEAAyE;QACzE,0EAA0E;QAC1E,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,GAAG,KAAK,UAAU,CAAC,GAAG,CAAC,OAAO,KAAK,EAAE,CAAC;IACvE,CAAC,CAAC,CAAC;IAEH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AAC1B,CAAC"}
|
|
@@ -6,25 +6,33 @@
|
|
|
6
6
|
* check → parse → cache write. Any thrown error degrades to `unavailable`
|
|
7
7
|
* rather than propagating, so the CLI always produces output.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* Two fetch sources (cookie console API preferred for its 0.1-percent
|
|
10
|
+
* precision; the API-key usage API as the cookie-free fallback — see
|
|
11
|
+
* queryGoUsage). A missing/invalid credential on every source yields
|
|
12
|
+
* `not_configured`, which the combined view silently skips (vs.
|
|
13
|
+
* `unavailable`, which renders an error line) — so users who only care about
|
|
14
|
+
* GLM see no noise.
|
|
12
15
|
*/
|
|
13
16
|
import type { GoQueryResult } from "./types.js";
|
|
14
17
|
/**
|
|
15
|
-
* Query
|
|
16
|
-
* {@link GoQueryResult}.
|
|
18
|
+
* Query Opencode Go usage and return a normalised {@link GoQueryResult}.
|
|
17
19
|
*
|
|
18
|
-
*
|
|
20
|
+
* Source selection: the COOKIE triple is preferred (0.1-percent precision);
|
|
21
|
+
* when it is absent — or the console API rejects it (400/401/403: expired
|
|
22
|
+
* cookies) while an API key is configured — the public usage API serves the
|
|
23
|
+
* fallback (integer percents). This keeps long-lived cookie setups on the
|
|
24
|
+
* finer source while key-only setups, and cookie expiry, degrade seamlessly.
|
|
25
|
+
*
|
|
26
|
+
* - No usable credential → `not_configured`.
|
|
19
27
|
* - Serves a cached result when fresh (< 10s).
|
|
20
|
-
* -
|
|
28
|
+
* - Auth rejected on BOTH paths → `auth_error`.
|
|
21
29
|
* - Network/timeout/parse failure → `unavailable`.
|
|
22
30
|
*/
|
|
23
31
|
export declare function queryGoUsage(): Promise<GoQueryResult>;
|
|
24
32
|
export { clearCache, clearCache as clearGoCache } from "./cache.js";
|
|
25
|
-
export { fetchGoStatus, goStatusUrl } from "./client.js";
|
|
26
|
-
export { parseGoStatus, looksLikeGoStatus } from "./parse.js";
|
|
33
|
+
export { fetchGoStatus, fetchZenUsage, goStatusUrl, zenUsageUrl } from "./client.js";
|
|
34
|
+
export { parseGoStatus, parseZenUsage, looksLikeGoStatus, looksLikeZenUsage } from "./parse.js";
|
|
27
35
|
export { formatDuration, formatGoSection } from "./format.js";
|
|
28
|
-
export { readConfigFile, CONFIG_PATH, ENV_WORKSPACE_ID, ENV_AUTH_COOKIE, ENV_SESSION_TOKEN, } from "./config.js";
|
|
36
|
+
export { readConfigFile, CONFIG_PATH, ENV_WORKSPACE_ID, ENV_AUTH_COOKIE, ENV_SESSION_TOKEN, ENV_API_KEY, } from "./config.js";
|
|
29
37
|
export type { GoQueryResult, GoWindow, GoWindowKey, GoStatusResponse } from "./types.js";
|
|
30
38
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/quota/opencode-go/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/quota/opencode-go/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAcH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AA2FhD;;;;;;;;;;;;;GAaG;AACH,wBAAsB,YAAY,IAAI,OAAO,CAAC,aAAa,CAAC,CA4C3D;AAoBD,OAAO,EAAE,UAAU,EAAE,UAAU,IAAI,YAAY,EAAE,MAAM,YAAY,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACrF,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAChG,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EACL,cAAc,EACd,WAAW,EACX,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EACjB,WAAW,GACZ,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -6,26 +6,31 @@
|
|
|
6
6
|
* check → parse → cache write. Any thrown error degrades to `unavailable`
|
|
7
7
|
* rather than propagating, so the CLI always produces output.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* Two fetch sources (cookie console API preferred for its 0.1-percent
|
|
10
|
+
* precision; the API-key usage API as the cookie-free fallback — see
|
|
11
|
+
* queryGoUsage). A missing/invalid credential on every source yields
|
|
12
|
+
* `not_configured`, which the combined view silently skips (vs.
|
|
13
|
+
* `unavailable`, which renders an error line) — so users who only care about
|
|
14
|
+
* GLM see no noise.
|
|
12
15
|
*/
|
|
13
16
|
import { loadUserConfig } from "../../config/user-config.js";
|
|
14
17
|
import { log } from "../../utils.js";
|
|
15
18
|
import { getCached, setCached } from "./cache.js";
|
|
16
|
-
import { ENV_AUTH_COOKIE, ENV_SESSION_TOKEN, ENV_WORKSPACE_ID, readConfigFile } from "./config.js";
|
|
17
|
-
import { fetchGoStatus } from "./client.js";
|
|
18
|
-
import { parseGoStatus } from "./parse.js";
|
|
19
|
+
import { ENV_API_KEY, ENV_AUTH_COOKIE, ENV_SESSION_TOKEN, ENV_WORKSPACE_ID, readConfigFile, } from "./config.js";
|
|
20
|
+
import { fetchGoStatus, fetchZenUsage } from "./client.js";
|
|
21
|
+
import { parseGoStatus, parseZenUsage } from "./parse.js";
|
|
19
22
|
/** Format validators (match pi-go-bars conventions). */
|
|
20
23
|
const RE_WORKSPACE = /^wrk_[A-Za-z0-9]+$/;
|
|
21
24
|
const COOKIE_PREFIX = "Fe26.2**";
|
|
22
25
|
/** Minimum plausible session-token length (console `st_…` values). */
|
|
23
26
|
const MIN_SESSION_TOKEN = 8;
|
|
27
|
+
/** Minimum plausible API-key length (`oc_sk_…` service keys). */
|
|
28
|
+
const MIN_API_KEY = 16;
|
|
24
29
|
/**
|
|
25
30
|
* Resolve & validate credentials.
|
|
26
31
|
*
|
|
27
|
-
*
|
|
28
|
-
* precedence (highest first):
|
|
32
|
+
* The cookie triple merges three sources field-by-field with the standard
|
|
33
|
+
* user-config precedence (highest first):
|
|
29
34
|
* 1. `quota.opencodeGoWorkspaceId` / `opencodeGoAuthCookie` /
|
|
30
35
|
* `opencodeGoSessionToken` in `~/.config/zcode-acp/config.json` (our own
|
|
31
36
|
* config — preferred home).
|
|
@@ -34,11 +39,11 @@ const MIN_SESSION_TOKEN = 8;
|
|
|
34
39
|
* 3. `~/.pi/agent/opencode-go.json` (legacy — the @beyona/pi-zai-usage Pi
|
|
35
40
|
* extension convention, kept so existing setups keep working).
|
|
36
41
|
*
|
|
37
|
-
* The
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
+
* The API key (`quota.opencodeGoApiKey` / `OPENCODE_GO_API_KEY`) has no legacy
|
|
43
|
+
* file source. The session token is REQUIRED since the 2026-09 console
|
|
44
|
+
* migration: the status API answers 401 to an `auth` cookie alone. A field
|
|
45
|
+
* present in a higher-precedence source overrides the same field below it; a
|
|
46
|
+
* field present only lower down still counts.
|
|
42
47
|
*/
|
|
43
48
|
function loadCredentials() {
|
|
44
49
|
const own = loadUserConfig().quota;
|
|
@@ -46,29 +51,57 @@ function loadCredentials() {
|
|
|
46
51
|
const workspaceId = own?.opencodeGoWorkspaceId ?? process.env[ENV_WORKSPACE_ID] ?? file.workspaceId;
|
|
47
52
|
const authCookie = own?.opencodeGoAuthCookie ?? process.env[ENV_AUTH_COOKIE] ?? file.authCookie;
|
|
48
53
|
const sessionToken = own?.opencodeGoSessionToken ?? process.env[ENV_SESSION_TOKEN] ?? file.sessionToken;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
let consoleCreds = null;
|
|
55
|
+
if (workspaceId && authCookie && sessionToken) {
|
|
56
|
+
if (!RE_WORKSPACE.test(workspaceId)) {
|
|
57
|
+
log(`opencode-go: invalid workspaceId format (expected wrk_…)`);
|
|
58
|
+
}
|
|
59
|
+
else if (!authCookie.startsWith(COOKIE_PREFIX)) {
|
|
60
|
+
log(`opencode-go: authCookie does not start with ${COOKIE_PREFIX}`);
|
|
61
|
+
}
|
|
62
|
+
else if (sessionToken.trim().length < MIN_SESSION_TOKEN) {
|
|
63
|
+
log(`opencode-go: sessionToken looks malformed (__Host-console_session value, st_…)`);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
consoleCreds = { workspaceId, authCookie, sessionToken };
|
|
67
|
+
}
|
|
54
68
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
69
|
+
const rawKey = own?.opencodeGoApiKey ?? process.env[ENV_API_KEY] ?? "";
|
|
70
|
+
const apiKey = rawKey.trim().length >= MIN_API_KEY ? rawKey.trim() : null;
|
|
71
|
+
return { console: consoleCreds, apiKey };
|
|
72
|
+
}
|
|
73
|
+
/** Map a parsed payload to the success/unavailable distinction. */
|
|
74
|
+
function resultFromParsed(parsed) {
|
|
75
|
+
if (parsed.parserOutdated) {
|
|
76
|
+
log("opencode-go: status JSON recognised but no windows parsed (parser outdated)");
|
|
77
|
+
return { kind: "unavailable" };
|
|
58
78
|
}
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
return
|
|
79
|
+
if (!parsed.rolling && !parsed.weekly && !parsed.monthly) {
|
|
80
|
+
// Not a status payload at all (e.g. an HTML error page).
|
|
81
|
+
return { kind: "unavailable" };
|
|
62
82
|
}
|
|
63
|
-
return {
|
|
83
|
+
return {
|
|
84
|
+
kind: "success",
|
|
85
|
+
// Rolling and weekly are the two windows the CLI shows by default; fall
|
|
86
|
+
// back to zeroes if somehow absent so the type stays simple.
|
|
87
|
+
rolling: parsed.rolling ?? { usagePercent: 0, resetInSec: 0 },
|
|
88
|
+
weekly: parsed.weekly ?? { usagePercent: 0, resetInSec: 0 },
|
|
89
|
+
monthly: parsed.monthly,
|
|
90
|
+
fetchedAt: Date.now(),
|
|
91
|
+
};
|
|
64
92
|
}
|
|
65
93
|
/**
|
|
66
|
-
* Query
|
|
67
|
-
* {@link GoQueryResult}.
|
|
94
|
+
* Query Opencode Go usage and return a normalised {@link GoQueryResult}.
|
|
68
95
|
*
|
|
69
|
-
*
|
|
96
|
+
* Source selection: the COOKIE triple is preferred (0.1-percent precision);
|
|
97
|
+
* when it is absent — or the console API rejects it (400/401/403: expired
|
|
98
|
+
* cookies) while an API key is configured — the public usage API serves the
|
|
99
|
+
* fallback (integer percents). This keeps long-lived cookie setups on the
|
|
100
|
+
* finer source while key-only setups, and cookie expiry, degrade seamlessly.
|
|
101
|
+
*
|
|
102
|
+
* - No usable credential → `not_configured`.
|
|
70
103
|
* - Serves a cached result when fresh (< 10s).
|
|
71
|
-
* -
|
|
104
|
+
* - Auth rejected on BOTH paths → `auth_error`.
|
|
72
105
|
* - Network/timeout/parse failure → `unavailable`.
|
|
73
106
|
*/
|
|
74
107
|
export async function queryGoUsage() {
|
|
@@ -78,54 +111,63 @@ export async function queryGoUsage() {
|
|
|
78
111
|
return cached;
|
|
79
112
|
}
|
|
80
113
|
const creds = loadCredentials();
|
|
81
|
-
if (!creds)
|
|
114
|
+
if (!creds.console && !creds.apiKey)
|
|
82
115
|
return { kind: "not_configured" };
|
|
83
116
|
let result;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (parsed.parserOutdated) {
|
|
98
|
-
log("opencode-go: status JSON recognised but no windows parsed (parser outdated)");
|
|
99
|
-
result = { kind: "unavailable" };
|
|
117
|
+
if (creds.console) {
|
|
118
|
+
try {
|
|
119
|
+
const resp = await fetchGoStatus(creds.console.workspaceId, creds.console.authCookie, creds.console.sessionToken);
|
|
120
|
+
// 401/403 = bad cookie pair; 400 = missing/unknown org id (x-org-id).
|
|
121
|
+
// The remedy is the same for all three — re-grab credentials — so they
|
|
122
|
+
// share auth_error… unless an API key is available, in which case the
|
|
123
|
+
// cookie-free endpoint takes over instead of failing the card.
|
|
124
|
+
if ((resp.status === 400 || resp.status === 401 || resp.status === 403) && creds.apiKey) {
|
|
125
|
+
log("opencode-go: console cookies rejected — falling back to the usage API key");
|
|
126
|
+
result = await queryViaApiKey(creds.apiKey);
|
|
127
|
+
}
|
|
128
|
+
else if (resp.status === 400 || resp.status === 401 || resp.status === 403) {
|
|
129
|
+
result = { kind: "auth_error" };
|
|
100
130
|
}
|
|
101
|
-
else if (
|
|
102
|
-
// Not a status payload at all (e.g. an HTML error page).
|
|
131
|
+
else if (resp.status < 200 || resp.status >= 300) {
|
|
103
132
|
result = { kind: "unavailable" };
|
|
104
133
|
}
|
|
105
134
|
else {
|
|
106
|
-
result =
|
|
107
|
-
kind: "success",
|
|
108
|
-
// Rolling and weekly are the two windows the CLI shows by default;
|
|
109
|
-
// fall back to zeroes if somehow absent so the type stays simple.
|
|
110
|
-
rolling: parsed.rolling ?? { usagePercent: 0, resetInSec: 0 },
|
|
111
|
-
weekly: parsed.weekly ?? { usagePercent: 0, resetInSec: 0 },
|
|
112
|
-
monthly: parsed.monthly,
|
|
113
|
-
fetchedAt: Date.now(),
|
|
114
|
-
};
|
|
135
|
+
result = resultFromParsed(parseGoStatus(resp.text));
|
|
115
136
|
}
|
|
116
137
|
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
log(`opencode-go: fetch failed (${e instanceof Error ? e.message : String(e)})`);
|
|
140
|
+
result = { kind: "unavailable" };
|
|
141
|
+
}
|
|
117
142
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
result =
|
|
143
|
+
else {
|
|
144
|
+
// Key-only setup — no cookie triple to try first.
|
|
145
|
+
result = await queryViaApiKey(creds.apiKey);
|
|
121
146
|
}
|
|
122
147
|
setCached(result);
|
|
123
148
|
return result;
|
|
124
149
|
}
|
|
150
|
+
/** Query the public usage API (`/zen/go/v1/usage`) with the service key. */
|
|
151
|
+
async function queryViaApiKey(apiKey) {
|
|
152
|
+
try {
|
|
153
|
+
const resp = await fetchZenUsage(apiKey);
|
|
154
|
+
if (resp.status === 401) {
|
|
155
|
+
return { kind: "auth_error" };
|
|
156
|
+
}
|
|
157
|
+
if (resp.status < 200 || resp.status >= 300) {
|
|
158
|
+
return { kind: "unavailable" };
|
|
159
|
+
}
|
|
160
|
+
return resultFromParsed(parseZenUsage(resp.text));
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
log(`opencode-go: usage API fetch failed (${e instanceof Error ? e.message : String(e)})`);
|
|
164
|
+
return { kind: "unavailable" };
|
|
165
|
+
}
|
|
166
|
+
}
|
|
125
167
|
// Re-exports for consumers (CLI + tests).
|
|
126
168
|
export { clearCache, clearCache as clearGoCache } from "./cache.js";
|
|
127
|
-
export { fetchGoStatus, goStatusUrl } from "./client.js";
|
|
128
|
-
export { parseGoStatus, looksLikeGoStatus } from "./parse.js";
|
|
169
|
+
export { fetchGoStatus, fetchZenUsage, goStatusUrl, zenUsageUrl } from "./client.js";
|
|
170
|
+
export { parseGoStatus, parseZenUsage, looksLikeGoStatus, looksLikeZenUsage } from "./parse.js";
|
|
129
171
|
export { formatDuration, formatGoSection } from "./format.js";
|
|
130
|
-
export { readConfigFile, CONFIG_PATH, ENV_WORKSPACE_ID, ENV_AUTH_COOKIE, ENV_SESSION_TOKEN, } from "./config.js";
|
|
172
|
+
export { readConfigFile, CONFIG_PATH, ENV_WORKSPACE_ID, ENV_AUTH_COOKIE, ENV_SESSION_TOKEN, ENV_API_KEY, } from "./config.js";
|
|
131
173
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/quota/opencode-go/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/quota/opencode-go/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAC7D,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EACL,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,gBAAgB,EAChB,cAAc,GACf,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,aAAa,EAAuB,MAAM,YAAY,CAAC;AAG/E,wDAAwD;AACxD,MAAM,YAAY,GAAG,oBAAoB,CAAC;AAC1C,MAAM,aAAa,GAAG,UAAU,CAAC;AAEjC,sEAAsE;AACtE,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAE5B,iEAAiE;AACjE,MAAM,WAAW,GAAG,EAAE,CAAC;AAavB;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAS,eAAe;IACtB,MAAM,GAAG,GAAG,cAAc,EAAE,CAAC,KAAK,CAAC;IACnC,MAAM,IAAI,GAAG,cAAc,EAAE,CAAC;IAC9B,MAAM,WAAW,GACf,GAAG,EAAE,qBAAqB,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,IAAI,CAAC,WAAW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shape (`access.meters`) — the parser-rot guard. */
|
|
28
28
|
export declare function looksLikeGoStatus(parsed: unknown): boolean;
|
|
29
29
|
export declare function parseGoStatus(text: string, now?: number): ParsedGoStatus;
|
|
30
|
+
/** Detect the public-usage payload shape (`usage.rolling`) — parser-rot guard. */
|
|
31
|
+
export declare function looksLikeZenUsage(parsed: unknown): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Parse the public usage payload
|
|
34
|
+
* (`{"usage":{"rolling|weekly|monthly":{"status","percent","resetsAt"}}}`).
|
|
35
|
+
* The percent is server-rounded (integer as of 2026-10 — finer granularity
|
|
36
|
+
* would pass through untouched); a missing/invalid percent drops the window
|
|
37
|
+
* rather than pinning a fake 0.
|
|
38
|
+
*/
|
|
39
|
+
export declare function parseZenUsage(text: string, now?: number): ParsedGoStatus;
|
|
30
40
|
//# sourceMappingURL=parse.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../src/quota/opencode-go/parse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAU3C,oFAAoF;AACpF,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,QAAQ,GAAG,IAAI,CAAC;IACzB,MAAM,EAAE,QAAQ,GAAG,IAAI,CAAC;IACxB,OAAO,EAAE,QAAQ,GAAG,IAAI,CAAC;IACzB;;;OAGG;IACH,cAAc,EAAE,OAAO,CAAC;CACzB;AAuBD,gFAAgF;AAChF,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAG1D;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,GAAE,MAAmB,GAAG,cAAc,CAsBpF"}
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../src/quota/opencode-go/parse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;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