@cortexkit/opencode-openai-auth 1.23.1 → 1.23.2
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/bin.js +7 -7
- package/dist/{index-7hy72nfy.js → index-by5wfvqm.js} +2 -2
- package/dist/index-nd2ks7er.js +2 -0
- package/dist/index-t876xhkr.js +60 -0
- package/dist/{index-p81hxfe6.js → index-v8w8j1wx.js} +1 -1
- package/dist/{index-3ya2s179.js → index-v8wrmhyp.js} +1 -1
- package/dist/{index-bd18sfjj.js → index-x00sxt2w.js} +10 -10
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/{runtime-layout-B07ApQZL.d.ts → internal-DacVgEWs.d.ts} +86 -663
- package/dist/rpc/port-file.js +1 -1
- package/dist/runtime-layout-Dcbj_Rq3.d.ts +650 -0
- package/dist/{sidebar-state--OTpRzFd.d.ts → sidebar-state-L--CdbLi.d.ts} +4 -15
- package/dist/sidebar-state.d.ts +1 -1
- package/dist/sidebar-state.js +1 -1
- package/dist/tui.d.ts +5 -2
- package/dist/v2/server.d.ts +2 -1
- package/dist/v2/server.js +4 -4
- package/package.json +1 -1
- package/src/tui-compiled/raw/shared/054118b2705fdc20-ansi.js +98 -0
- package/src/tui-compiled/raw/shared/0895e780489c494d-window.js +33 -0
- package/src/tui-compiled/raw/shared/0b7d24474e2f33e9-credential-redaction.js +424 -0
- package/src/tui-compiled/raw/shared/0c9533222cb4380c-namespace.js +224 -0
- package/src/tui-compiled/raw/shared/112607e46a5b5f58-accounts.js +393 -0
- package/src/tui-compiled/raw/shared/136fa4abe48ad69d-cachekeep.js +411 -0
- package/src/tui-compiled/raw/shared/1547bd74f4214d2d-index.js +28 -0
- package/src/tui-compiled/raw/shared/1567207f323dc733-codex-http.js +44 -0
- package/src/tui-compiled/raw/shared/15f402885cbb2fb3-envelope-loss.js +151 -0
- package/src/tui-compiled/raw/shared/18df43cab4563acf-families.js +149 -0
- package/src/tui-compiled/raw/shared/1a4c289eec261e04-codex-instructions.js +59 -0
- package/src/tui-compiled/raw/shared/1afd446e190ef85c-vault-messages.js +12 -0
- package/src/tui-compiled/raw/shared/1b06ec96856ff8b9-codex-endpoint.js +17 -0
- package/src/tui-compiled/raw/shared/1e2f1a89bb34a773-vault-adapter.js +110 -0
- package/src/tui-compiled/raw/shared/2194fb8e8b42d7d5-insula-quota.js +73 -0
- package/src/tui-compiled/raw/shared/22b2bce42f2d6806-handle.js +10 -0
- package/src/tui-compiled/raw/shared/2577ec69f9973ae9-quota-normalize.js +13 -14
- package/src/tui-compiled/raw/shared/26055d4e5ad5e71f-codex-request.js +198 -0
- package/src/tui-compiled/raw/shared/26b57ac4c3b0c3f6-unconfirmed-retain-counter.js +41 -0
- package/src/tui-compiled/raw/shared/2bb681e99c2721d7-registry.js +538 -0
- package/src/tui-compiled/raw/shared/2dce6e9613993329-quota-manager.js +13 -20
- package/src/tui-compiled/raw/shared/2ec33b3e2b8ac38a-terminal.js +168 -0
- package/src/tui-compiled/raw/shared/30bcca1d1b4240a0-pool-session-pins.js +32 -0
- package/src/tui-compiled/raw/shared/3af9a210aa2ccdf2-codex-call-status.js +21 -0
- package/src/tui-compiled/raw/shared/3b31b36bf0877602-wording.js +120 -0
- package/src/tui-compiled/raw/shared/3c4c1a2d3fc92726-failure.js +59 -0
- package/src/tui-compiled/raw/shared/3dfe71eec6e0487d-presets.js +240 -0
- package/src/tui-compiled/raw/shared/43de3a5c4845cfc1-pool-adapter.js +149 -0
- package/src/tui-compiled/raw/shared/49353b63a833c8fb-session-cache-policy.js +60 -0
- package/src/tui-compiled/raw/shared/4adf46e07e025363-axes.js +7 -0
- package/src/tui-compiled/raw/shared/4ba847b77580b1eb-confirm.js +21 -0
- package/src/tui-compiled/raw/shared/4d05c2d7a531aece-oauth.js +48 -36
- package/src/tui-compiled/raw/shared/5018d4665e969588-limit-policy.js +159 -0
- package/src/tui-compiled/raw/shared/595bdf3fff13ba79-limit-identity.js +49 -0
- package/src/tui-compiled/raw/shared/59def9e82b61204a-session-cache-activity.js +35 -0
- package/src/tui-compiled/raw/shared/5d806e1a305d14f5-commands.js +879 -0
- package/src/tui-compiled/raw/shared/5fc39320fa8bd3a0-limit-registry.js +473 -0
- package/src/tui-compiled/raw/shared/63c1ee7e70e85e8d-vault.js +1 -0
- package/src/tui-compiled/raw/shared/6545ab7f757314b7-refresh-all-quota.js +237 -0
- package/src/tui-compiled/raw/shared/66937534c260d20b-stable-json.js +11 -0
- package/src/tui-compiled/raw/shared/7056acef1b021f19-bridge.js +10 -0
- package/src/tui-compiled/raw/shared/72d2ca42883809b4-marker.js +107 -0
- package/src/tui-compiled/raw/shared/775431e21f469203-record.js +3 -0
- package/src/tui-compiled/raw/shared/799f3ade4efbd1ea-opencode-v1.js +47 -0
- package/src/tui-compiled/raw/shared/7b2a0471b29e4f5e-wake.js +47 -0
- package/src/tui-compiled/raw/shared/7b5d6afff4ff32b9-select.js +159 -0
- package/src/tui-compiled/raw/shared/7bf96bf4d9249b3d-vault-billing.js +108 -0
- package/src/tui-compiled/raw/shared/7c98bde36aad4819-input-transformations.js +34 -0
- package/src/tui-compiled/raw/shared/7fb0418303f20a10-index.js +3 -0
- package/src/tui-compiled/raw/shared/806e12ccbad34388-pi-wait.js +51 -0
- package/src/tui-compiled/raw/shared/81ae201b724cac14-menu.js +73 -0
- package/src/tui-compiled/raw/shared/8349ed25dc629e97-scope.js +10 -0
- package/src/tui-compiled/raw/shared/86da3379973780b9-limit-provenance.js +60 -0
- package/src/tui-compiled/raw/shared/8aba66ed486d30a7-decide.js +425 -0
- package/src/tui-compiled/raw/shared/8c0c8df25a5dca1f-manager.js +735 -0
- package/src/tui-compiled/raw/shared/8c42bbec02f59636-vault-routing.js +212 -0
- package/src/tui-compiled/raw/shared/8ca6175bfae53e13-envelope.js +123 -0
- package/src/tui-compiled/raw/shared/8d0245e41049d83f-header-classifier.js +121 -0
- package/src/tui-compiled/raw/shared/9c162809179c7560-login.js +89 -0
- package/src/tui-compiled/raw/shared/a1f495296a4b756f-frame-turn.js +72 -0
- package/src/tui-compiled/raw/shared/a37138dcbd893f55-hold-bounds.js +5 -0
- package/src/tui-compiled/raw/shared/aa7fb10d77a2cccd-vault-account-menu.js +133 -0
- package/src/tui-compiled/raw/shared/adc5eeb5a8d28280-persistence.js +9 -0
- package/src/tui-compiled/raw/shared/b301c79e6378d1fb-vault.js +678 -0
- package/src/tui-compiled/raw/shared/b3c11de5f36e617a-reset.js +44 -0
- package/src/tui-compiled/raw/shared/ba6535c610d1edb3-pool-source-state.js +100 -0
- package/src/tui-compiled/raw/shared/bd4aef685681048c-duration.js +20 -0
- package/src/tui-compiled/raw/shared/bd5977eca210d2b8-index.js +662 -0
- package/src/tui-compiled/raw/shared/bfc46d34f4c9db33-limit-recovery.js +127 -0
- package/src/tui-compiled/raw/shared/c66495424ad84aee-hold.js +25 -0
- package/src/tui-compiled/raw/shared/c6a67b836bd5afcd-vault-command-menu.js +300 -0
- package/src/tui-compiled/raw/shared/c9340fb545c4e5de-internal.js +73 -0
- package/src/tui-compiled/raw/shared/c954c6447b7b9b64-activity.js +52 -0
- package/src/tui-compiled/raw/shared/cba8b0aee46b8526-codex-lite.js +85 -0
- package/src/tui-compiled/raw/shared/cec39ccddab4fe9c-labels.js +11 -0
- package/src/tui-compiled/raw/shared/cef0f2346957e83c-index.js +552 -0
- package/src/tui-compiled/raw/shared/cf7dedf24f902d68-dump-size.js +41 -0
- package/src/tui-compiled/raw/shared/d422d6dbc4e35199-background-quota-refresh.js +107 -0
- package/src/tui-compiled/raw/shared/d5b268767d330507-doctor.js +81 -0
- package/src/tui-compiled/raw/shared/d7ac5ca41f49121b-index.js +12 -0
- package/src/tui-compiled/raw/shared/d7fa77560855dee2-jitter.js +9 -0
- package/src/tui-compiled/raw/shared/de7f7d8803624369-path.js +31 -0
- package/src/tui-compiled/raw/shared/df0bdc74fb0c0db0-limit-mark.js +27 -0
- package/src/tui-compiled/raw/shared/e26c2bb5722ab047-routing-hold.js +23 -0
- package/src/tui-compiled/raw/shared/e2ae081583adfce3-dump.js +80 -0
- package/src/tui-compiled/raw/shared/e44ba1b3a9903f5d-reset-credits.js +483 -0
- package/src/tui-compiled/raw/shared/e5229952fc12f647-codex-models.js +34 -0
- package/src/tui-compiled/raw/shared/e8fda84e90176436-uuid-v7.js +13 -0
- package/src/tui-compiled/raw/shared/ea56007991dbf068-terminal.js +19 -0
- package/src/tui-compiled/raw/shared/ec6f1d96e7d70174-quota-refusal.js +145 -0
- package/src/tui-compiled/raw/shared/f03d7b43a214a4a1-canonical-input.js +42 -0
- package/src/tui-compiled/raw/shared/f1012265b3a768c7-merge-laws.js +31 -0
- package/src/tui-compiled/raw/shared/f2ca864fe967415a-cache-policy.js +198 -0
- package/src/tui-compiled/raw/shared/f317a4740f9d76f8-identity.js +21 -0
- package/src/tui-compiled/raw/shared/f607353c4126272a-routing-wait.js +19 -0
- package/src/tui-compiled/raw/shared/f6189f5c9aee31eb-decision.js +55 -0
- package/src/tui-compiled/raw/shared/f6d728f5e601f98b-request-history.js +120 -0
- package/src/tui-compiled/raw/shared/f981e6b15fb76cdb-codex-body.js +28 -0
- package/src/tui-compiled/raw/shared/fda579e5a0a54165-codex-effort.js +60 -0
- package/src/tui-compiled/raw/sidebar-state.js +54 -4
- package/src/tui-compiled/raw/tui/v2-dialog.js +1 -1
- package/src/tui-compiled/raw/tui.tsx +27 -5
- package/src/tui-compiled/runtime/shared/054118b2705fdc20-ansi.js +98 -0
- package/src/tui-compiled/runtime/shared/0895e780489c494d-window.js +33 -0
- package/src/tui-compiled/runtime/shared/0b7d24474e2f33e9-credential-redaction.js +424 -0
- package/src/tui-compiled/runtime/shared/0c9533222cb4380c-namespace.js +224 -0
- package/src/tui-compiled/runtime/shared/112607e46a5b5f58-accounts.js +393 -0
- package/src/tui-compiled/runtime/shared/136fa4abe48ad69d-cachekeep.js +411 -0
- package/src/tui-compiled/runtime/shared/1547bd74f4214d2d-index.js +28 -0
- package/src/tui-compiled/runtime/shared/1567207f323dc733-codex-http.js +44 -0
- package/src/tui-compiled/runtime/shared/15f402885cbb2fb3-envelope-loss.js +151 -0
- package/src/tui-compiled/runtime/shared/18df43cab4563acf-families.js +149 -0
- package/src/tui-compiled/runtime/shared/1a4c289eec261e04-codex-instructions.js +59 -0
- package/src/tui-compiled/runtime/shared/1afd446e190ef85c-vault-messages.js +12 -0
- package/src/tui-compiled/runtime/shared/1b06ec96856ff8b9-codex-endpoint.js +17 -0
- package/src/tui-compiled/runtime/shared/1e2f1a89bb34a773-vault-adapter.js +110 -0
- package/src/tui-compiled/runtime/shared/2194fb8e8b42d7d5-insula-quota.js +73 -0
- package/src/tui-compiled/runtime/shared/22b2bce42f2d6806-handle.js +10 -0
- package/src/tui-compiled/runtime/shared/2577ec69f9973ae9-quota-normalize.js +13 -14
- package/src/tui-compiled/runtime/shared/26055d4e5ad5e71f-codex-request.js +198 -0
- package/src/tui-compiled/runtime/shared/26b57ac4c3b0c3f6-unconfirmed-retain-counter.js +41 -0
- package/src/tui-compiled/runtime/shared/2bb681e99c2721d7-registry.js +538 -0
- package/src/tui-compiled/runtime/shared/2dce6e9613993329-quota-manager.js +13 -20
- package/src/tui-compiled/runtime/shared/2ec33b3e2b8ac38a-terminal.js +168 -0
- package/src/tui-compiled/runtime/shared/30bcca1d1b4240a0-pool-session-pins.js +32 -0
- package/src/tui-compiled/runtime/shared/3af9a210aa2ccdf2-codex-call-status.js +21 -0
- package/src/tui-compiled/runtime/shared/3b31b36bf0877602-wording.js +120 -0
- package/src/tui-compiled/runtime/shared/3c4c1a2d3fc92726-failure.js +59 -0
- package/src/tui-compiled/runtime/shared/3dfe71eec6e0487d-presets.js +240 -0
- package/src/tui-compiled/runtime/shared/43de3a5c4845cfc1-pool-adapter.js +149 -0
- package/src/tui-compiled/runtime/shared/49353b63a833c8fb-session-cache-policy.js +60 -0
- package/src/tui-compiled/runtime/shared/4adf46e07e025363-axes.js +7 -0
- package/src/tui-compiled/runtime/shared/4ba847b77580b1eb-confirm.js +21 -0
- package/src/tui-compiled/runtime/shared/4d05c2d7a531aece-oauth.js +48 -36
- package/src/tui-compiled/runtime/shared/5018d4665e969588-limit-policy.js +159 -0
- package/src/tui-compiled/runtime/shared/595bdf3fff13ba79-limit-identity.js +49 -0
- package/src/tui-compiled/runtime/shared/59def9e82b61204a-session-cache-activity.js +35 -0
- package/src/tui-compiled/runtime/shared/5d806e1a305d14f5-commands.js +879 -0
- package/src/tui-compiled/runtime/shared/5fc39320fa8bd3a0-limit-registry.js +473 -0
- package/src/tui-compiled/runtime/shared/63c1ee7e70e85e8d-vault.js +1 -0
- package/src/tui-compiled/runtime/shared/6545ab7f757314b7-refresh-all-quota.js +237 -0
- package/src/tui-compiled/runtime/shared/66937534c260d20b-stable-json.js +11 -0
- package/src/tui-compiled/runtime/shared/7056acef1b021f19-bridge.js +10 -0
- package/src/tui-compiled/runtime/shared/72d2ca42883809b4-marker.js +107 -0
- package/src/tui-compiled/runtime/shared/775431e21f469203-record.js +3 -0
- package/src/tui-compiled/runtime/shared/799f3ade4efbd1ea-opencode-v1.js +47 -0
- package/src/tui-compiled/runtime/shared/7b2a0471b29e4f5e-wake.js +47 -0
- package/src/tui-compiled/runtime/shared/7b5d6afff4ff32b9-select.js +159 -0
- package/src/tui-compiled/runtime/shared/7bf96bf4d9249b3d-vault-billing.js +108 -0
- package/src/tui-compiled/runtime/shared/7c98bde36aad4819-input-transformations.js +34 -0
- package/src/tui-compiled/runtime/shared/7fb0418303f20a10-index.js +3 -0
- package/src/tui-compiled/runtime/shared/806e12ccbad34388-pi-wait.js +51 -0
- package/src/tui-compiled/runtime/shared/81ae201b724cac14-menu.js +73 -0
- package/src/tui-compiled/runtime/shared/8349ed25dc629e97-scope.js +10 -0
- package/src/tui-compiled/runtime/shared/86da3379973780b9-limit-provenance.js +60 -0
- package/src/tui-compiled/runtime/shared/8aba66ed486d30a7-decide.js +425 -0
- package/src/tui-compiled/runtime/shared/8c0c8df25a5dca1f-manager.js +735 -0
- package/src/tui-compiled/runtime/shared/8c42bbec02f59636-vault-routing.js +212 -0
- package/src/tui-compiled/runtime/shared/8ca6175bfae53e13-envelope.js +123 -0
- package/src/tui-compiled/runtime/shared/8d0245e41049d83f-header-classifier.js +121 -0
- package/src/tui-compiled/runtime/shared/9c162809179c7560-login.js +89 -0
- package/src/tui-compiled/runtime/shared/a1f495296a4b756f-frame-turn.js +72 -0
- package/src/tui-compiled/runtime/shared/a37138dcbd893f55-hold-bounds.js +5 -0
- package/src/tui-compiled/runtime/shared/aa7fb10d77a2cccd-vault-account-menu.js +133 -0
- package/src/tui-compiled/runtime/shared/adc5eeb5a8d28280-persistence.js +9 -0
- package/src/tui-compiled/runtime/shared/b301c79e6378d1fb-vault.js +678 -0
- package/src/tui-compiled/runtime/shared/b3c11de5f36e617a-reset.js +44 -0
- package/src/tui-compiled/runtime/shared/ba6535c610d1edb3-pool-source-state.js +100 -0
- package/src/tui-compiled/runtime/shared/bd4aef685681048c-duration.js +20 -0
- package/src/tui-compiled/runtime/shared/bd5977eca210d2b8-index.js +662 -0
- package/src/tui-compiled/runtime/shared/bfc46d34f4c9db33-limit-recovery.js +127 -0
- package/src/tui-compiled/runtime/shared/c66495424ad84aee-hold.js +25 -0
- package/src/tui-compiled/runtime/shared/c6a67b836bd5afcd-vault-command-menu.js +300 -0
- package/src/tui-compiled/runtime/shared/c9340fb545c4e5de-internal.js +73 -0
- package/src/tui-compiled/runtime/shared/c954c6447b7b9b64-activity.js +52 -0
- package/src/tui-compiled/runtime/shared/cba8b0aee46b8526-codex-lite.js +85 -0
- package/src/tui-compiled/runtime/shared/cec39ccddab4fe9c-labels.js +11 -0
- package/src/tui-compiled/runtime/shared/cef0f2346957e83c-index.js +552 -0
- package/src/tui-compiled/runtime/shared/cf7dedf24f902d68-dump-size.js +41 -0
- package/src/tui-compiled/runtime/shared/d422d6dbc4e35199-background-quota-refresh.js +107 -0
- package/src/tui-compiled/runtime/shared/d5b268767d330507-doctor.js +81 -0
- package/src/tui-compiled/runtime/shared/d7ac5ca41f49121b-index.js +12 -0
- package/src/tui-compiled/runtime/shared/d7fa77560855dee2-jitter.js +9 -0
- package/src/tui-compiled/runtime/shared/de7f7d8803624369-path.js +31 -0
- package/src/tui-compiled/runtime/shared/df0bdc74fb0c0db0-limit-mark.js +27 -0
- package/src/tui-compiled/runtime/shared/e26c2bb5722ab047-routing-hold.js +23 -0
- package/src/tui-compiled/runtime/shared/e2ae081583adfce3-dump.js +80 -0
- package/src/tui-compiled/runtime/shared/e44ba1b3a9903f5d-reset-credits.js +483 -0
- package/src/tui-compiled/runtime/shared/e5229952fc12f647-codex-models.js +34 -0
- package/src/tui-compiled/runtime/shared/e8fda84e90176436-uuid-v7.js +13 -0
- package/src/tui-compiled/runtime/shared/ea56007991dbf068-terminal.js +19 -0
- package/src/tui-compiled/runtime/shared/ec6f1d96e7d70174-quota-refusal.js +145 -0
- package/src/tui-compiled/runtime/shared/f03d7b43a214a4a1-canonical-input.js +42 -0
- package/src/tui-compiled/runtime/shared/f1012265b3a768c7-merge-laws.js +31 -0
- package/src/tui-compiled/runtime/shared/f2ca864fe967415a-cache-policy.js +198 -0
- package/src/tui-compiled/runtime/shared/f317a4740f9d76f8-identity.js +21 -0
- package/src/tui-compiled/runtime/shared/f607353c4126272a-routing-wait.js +19 -0
- package/src/tui-compiled/runtime/shared/f6189f5c9aee31eb-decision.js +55 -0
- package/src/tui-compiled/runtime/shared/f6d728f5e601f98b-request-history.js +120 -0
- package/src/tui-compiled/runtime/shared/f981e6b15fb76cdb-codex-body.js +28 -0
- package/src/tui-compiled/runtime/shared/fda579e5a0a54165-codex-effort.js +60 -0
- package/src/tui-compiled/runtime/sidebar-state.js +54 -4
- package/src/tui-compiled/runtime/tui/v2-dialog.js +1 -1
- package/src/tui-compiled/runtime/tui.js +11 -4
- package/dist/index-0p8r7cpy.js +0 -60
- package/dist/index-px1t650b.js +0 -2
- package/dist/map-D5PHhv4z.d.ts +0 -72
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
buildRefusal,
|
|
4
|
+
formatReset,
|
|
5
|
+
readMaxSameAccountWait,
|
|
6
|
+
registerLimitMessages,
|
|
7
|
+
sameAccount,
|
|
8
|
+
scanTerminalReset
|
|
9
|
+
} from "./1547bd74f4214d2d-index.js";
|
|
10
|
+
import {
|
|
11
|
+
decideOpenAiLimit,
|
|
12
|
+
freshQuotaEvidence,
|
|
13
|
+
QUOTA_STALENESS_MS,
|
|
14
|
+
quotaResetAt
|
|
15
|
+
} from "./5018d4665e969588-limit-policy.js";
|
|
16
|
+
import { hasVaultRoutingAccessor } from "./8c42bbec02f59636-vault-routing.js";
|
|
17
|
+
export const OPENAI_QUOTA_MINIMUM_REFUSAL_MESSAGE = "No OpenAI account meets the configured minimum remaining quota.", OPENAI_FINAL_CAUSES = {
|
|
18
|
+
quota: "Every OpenAI account has used up its quota.",
|
|
19
|
+
killswitch: "Every OpenAI account is below its killswitch quota floor (see the Limits section of `/openai`).",
|
|
20
|
+
"timed-mark": "Every OpenAI account is paused by OpenAI for now."
|
|
21
|
+
}, OPENAI_RETRYABLE_CAUSES = {
|
|
22
|
+
quota: "OpenAI quota exhausted \u2014 retrying on another account.",
|
|
23
|
+
killswitch: "Killswitch: all OpenAI accounts are below their configured quota threshold.",
|
|
24
|
+
"timed-mark": "OpenAI rate limit reached \u2014 retrying on another account.",
|
|
25
|
+
hold: "OpenAI usage limit reached on this account. Waiting for it keeps the conversation's prompt cache."
|
|
26
|
+
};
|
|
27
|
+
function template(cause) {
|
|
28
|
+
return ({ retryIn, reset }) => `${cause}${retryIn ? ` Retry in ${retryIn}.` : ""}${reset ? ` Resets in ${reset}.` : ""}`;
|
|
29
|
+
}
|
|
30
|
+
export const openAiLimitMessages = registerLimitMessages({
|
|
31
|
+
refusals: Object.fromEntries(Object.entries(OPENAI_FINAL_CAUSES).map(([key, cause]) => [
|
|
32
|
+
key,
|
|
33
|
+
template(cause)
|
|
34
|
+
])),
|
|
35
|
+
retryable: Object.fromEntries(Object.entries(OPENAI_RETRYABLE_CAUSES).map(([key, cause]) => [
|
|
36
|
+
key,
|
|
37
|
+
template(cause)
|
|
38
|
+
])),
|
|
39
|
+
formatReset
|
|
40
|
+
});
|
|
41
|
+
export function openAiResetWait(enabled, primary, sessionId) {
|
|
42
|
+
if (!enabled || !primary || hasVaultRoutingAccessor())
|
|
43
|
+
return;
|
|
44
|
+
return { jitterMs: createHash("sha256").update(sessionId ?? "").digest().readUInt32BE(0) % 21 * 1000 };
|
|
45
|
+
}
|
|
46
|
+
export function openAiTerminalFacts(input) {
|
|
47
|
+
const unproven = [];
|
|
48
|
+
let quota = !1, killswitch = !1;
|
|
49
|
+
const familyOf = input.familyOf ?? (() => {
|
|
50
|
+
return;
|
|
51
|
+
}), candidates = input.accounts.flatMap((row) => {
|
|
52
|
+
if (row.usable === !1)
|
|
53
|
+
return [];
|
|
54
|
+
const readingMatches = sameAccount(row.account, row.readingAccount ?? row.account) && !(row.identity && row.readingIdentity && row.identity !== row.readingIdentity), windows = [];
|
|
55
|
+
if (readingMatches) {
|
|
56
|
+
for (const name of ["primary", "secondary"]) {
|
|
57
|
+
const window = row.reading?.[name];
|
|
58
|
+
if (!window)
|
|
59
|
+
continue;
|
|
60
|
+
const fresh = freshQuotaEvidence(window.checkedAt, input.now), floor = row.floors?.[name], blockedBy = floor !== void 0 && window.remainingPercent < floor ? "killswitch-floor" : fresh && window.usedPercent >= 100 ? "exhausted" : void 0;
|
|
61
|
+
if (!blockedBy)
|
|
62
|
+
continue;
|
|
63
|
+
if (blockedBy === "killswitch-floor")
|
|
64
|
+
killswitch = !0;
|
|
65
|
+
else
|
|
66
|
+
quota = !0;
|
|
67
|
+
const at = fresh ? quotaResetAt(window.resetsAt) : void 0;
|
|
68
|
+
windows.push({
|
|
69
|
+
window: name,
|
|
70
|
+
scope: { kind: "pool" },
|
|
71
|
+
blockedBy,
|
|
72
|
+
...at === void 0 ? {} : { reset: { at, checkedAt: window.checkedAt } }
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
for (const blocker of row.confirmedBlockers ?? []) {
|
|
76
|
+
if (!blocker.confirmed || !freshQuotaEvidence(blocker.observedAt, input.now))
|
|
77
|
+
continue;
|
|
78
|
+
quota = !0;
|
|
79
|
+
windows.push({
|
|
80
|
+
window: blocker.window,
|
|
81
|
+
scope: { kind: "pool" },
|
|
82
|
+
blockedBy: "exhausted",
|
|
83
|
+
...blocker.resetAt === void 0 ? {} : {
|
|
84
|
+
reset: { at: blocker.resetAt, observedAt: blocker.observedAt }
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
for (const mark of row.marks ?? []) {
|
|
90
|
+
if (!Number.isFinite(mark.until) || mark.until <= input.now)
|
|
91
|
+
continue;
|
|
92
|
+
const annotation = input.provenance?.read(row.account, mark.until, mark.evidenceAt, input.now), proven = annotation && annotation.branch !== "unproven" && freshQuotaEvidence(annotation.observedAt, input.now);
|
|
93
|
+
if (!proven)
|
|
94
|
+
unproven.push(mark.until);
|
|
95
|
+
windows.push({
|
|
96
|
+
window: "account",
|
|
97
|
+
scope: { kind: "pool" },
|
|
98
|
+
blockedBy: "exhausted",
|
|
99
|
+
...proven ? { reset: { at: mark.until, observedAt: annotation.observedAt } } : {}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return [{ account: row.account, readingAccount: row.account, windows }];
|
|
103
|
+
}), activity = input.activity && input.currentAccountKey && sameAccount(input.activity.accountKey, input.currentAccountKey) ? input.activity : void 0;
|
|
104
|
+
let scan = {
|
|
105
|
+
candidates,
|
|
106
|
+
effectiveModel: input.effectiveModel,
|
|
107
|
+
familyOf,
|
|
108
|
+
maxAgeMs: QUOTA_STALENESS_MS,
|
|
109
|
+
maxSameAccountWait: readMaxSameAccountWait(input.maxSameAccountWait),
|
|
110
|
+
...input.waitForReset && { waitForReset: input.waitForReset },
|
|
111
|
+
activity,
|
|
112
|
+
accountKey: activity?.accountKey
|
|
113
|
+
};
|
|
114
|
+
const selected = scanTerminalReset(scan, input.now);
|
|
115
|
+
if (selected && unproven.some((until) => until < selected.resetAt))
|
|
116
|
+
scan = { ...scan, candidates: [] };
|
|
117
|
+
return {
|
|
118
|
+
source: "pool",
|
|
119
|
+
reason: "quota",
|
|
120
|
+
messageKey: killswitch ? "killswitch" : quota ? "quota" : "timed-mark",
|
|
121
|
+
scan
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
export function decideOpenAiTerminal(facts, terminal) {
|
|
125
|
+
const normalized = facts.phase === "pre-send" ? {
|
|
126
|
+
...facts,
|
|
127
|
+
attempted: void 0,
|
|
128
|
+
selection: void 0,
|
|
129
|
+
hold: void 0
|
|
130
|
+
} : facts, decision = decideOpenAiLimit({ ...normalized, terminal }, { mode: "sticky" });
|
|
131
|
+
if (!decision)
|
|
132
|
+
throw Error("Missing OpenAI terminal decision");
|
|
133
|
+
return decision;
|
|
134
|
+
}
|
|
135
|
+
export function buildOpenAiQuotaRefusal(input) {
|
|
136
|
+
const decision = input.decision, hold = input.host !== "pi" && decision.action === "hold" && decision.waitKind !== "terminal", retryable = input.host !== "pi" && !input.requestThrow && (hold || readMaxSameAccountWait(input.maxSameAccountWait) !== 0 && decision.retryable), key = hold ? "hold" : input.cause ?? (decision.action === "refuse" ? decision.messageKey : "quota");
|
|
137
|
+
return buildRefusal({
|
|
138
|
+
family: input.host === "pi" ? "pi-final" : input.requestThrow ? "json-412" : hold ? "openai-429" : decision.action === "refuse" ? decision.family === "pi-final" ? "pi-final" : retryable ? "openai-429" : "json-412" : retryable ? "openai-429" : "pi-final",
|
|
139
|
+
messageKey: key,
|
|
140
|
+
messages: openAiLimitMessages,
|
|
141
|
+
now: input.now,
|
|
142
|
+
...retryable && "retryAfterMs" in decision ? { retryAfterMs: decision.retryAfterMs } : {},
|
|
143
|
+
..."resetAt" in decision && decision.resetAt !== void 0 ? { resetAt: decision.resetAt } : {}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { stableStringify } from "./66937534c260d20b-stable-json.js";
|
|
2
|
+
const MAX_CACHED_ITEMS = 512, MAX_CACHED_CHARS = 2097152;
|
|
3
|
+
function sameJson(snapshot, value) {
|
|
4
|
+
if (typeof snapshot === "function" || typeof value === "function")
|
|
5
|
+
return !1;
|
|
6
|
+
if (value !== null && typeof value === "object" && Object.hasOwn(value, "toJSON") && typeof value.toJSON === "function")
|
|
7
|
+
return !1;
|
|
8
|
+
if (snapshot === value)
|
|
9
|
+
return !0;
|
|
10
|
+
if (Array.isArray(snapshot))
|
|
11
|
+
return Array.isArray(value) && snapshot.length === value.length && snapshot.every((item, index) => sameJson(item, value[index]));
|
|
12
|
+
if (snapshot === null || value === null || typeof snapshot !== "object" || typeof value !== "object" || Array.isArray(value))
|
|
13
|
+
return !1;
|
|
14
|
+
const left = snapshot, right = value, keys = Object.keys(left);
|
|
15
|
+
return keys.length === Object.keys(right).length && keys.every((key) => Object.prototype.propertyIsEnumerable.call(right, key) && sameJson(left[key], right[key]));
|
|
16
|
+
}
|
|
17
|
+
export function canonicalInput(input, prior) {
|
|
18
|
+
const texts = [], snapshots = [];
|
|
19
|
+
let chars = 0;
|
|
20
|
+
for (let index = 0;index < input.length; index++) {
|
|
21
|
+
const oldText = prior?.texts[index], unchanged = oldText !== void 0 && sameJson(prior?.snapshots[index], input[index]), text = unchanged ? oldText : stableStringify(input[index]);
|
|
22
|
+
texts.push(text);
|
|
23
|
+
snapshots.push(unchanged ? prior?.snapshots[index] : text === "undefined" ? void 0 : JSON.parse(text));
|
|
24
|
+
chars += text.length;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
texts,
|
|
28
|
+
snapshots,
|
|
29
|
+
retainable: input.length <= MAX_CACHED_ITEMS && chars <= MAX_CACHED_CHARS
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export function retainedInput(input) {
|
|
33
|
+
return input.retainable ? input : void 0;
|
|
34
|
+
}
|
|
35
|
+
export function canonicalPrefixLength(prefix, input) {
|
|
36
|
+
if (prefix.texts.length > input.texts.length)
|
|
37
|
+
return;
|
|
38
|
+
for (let index = 0;index < prefix.texts.length; index++)
|
|
39
|
+
if (prefix.texts[index] !== input.texts[index])
|
|
40
|
+
return;
|
|
41
|
+
return prefix.texts.length;
|
|
42
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Apply supersession before expiry, so a short or zero overwrite removes a long mark. */
|
|
2
|
+
export function mergeTimedMarkEntries(law, entries, now, clearRevision = 0) {
|
|
3
|
+
const candidates = entries.filter((e) => e.baseRevision >= clearRevision);
|
|
4
|
+
if (law !== 'max-until') {
|
|
5
|
+
const winner = candidates.reduce((best, e) => {
|
|
6
|
+
if (!best)
|
|
7
|
+
return e;
|
|
8
|
+
if (law === 'latest-evidence' && e.evidenceAt !== best.evidenceAt) {
|
|
9
|
+
return e.evidenceAt > best.evidenceAt ? e : best;
|
|
10
|
+
}
|
|
11
|
+
return e.committedRevision > best.committedRevision ? e : best;
|
|
12
|
+
}, undefined);
|
|
13
|
+
return winner && now < winner.until ? [winner] : [];
|
|
14
|
+
}
|
|
15
|
+
return candidates
|
|
16
|
+
.filter((e, i) => now < e.until &&
|
|
17
|
+
!candidates.some((other, j) => j !== i &&
|
|
18
|
+
other.until >= e.until &&
|
|
19
|
+
other.baseRevision >= e.baseRevision &&
|
|
20
|
+
(other.until > e.until ||
|
|
21
|
+
other.baseRevision > e.baseRevision ||
|
|
22
|
+
other.committedRevision > e.committedRevision ||
|
|
23
|
+
(other.committedRevision === e.committedRevision && j < i))))
|
|
24
|
+
.sort((a, b) => a.committedRevision - b.committedRevision);
|
|
25
|
+
}
|
|
26
|
+
export function mergeAccessState(a, b) {
|
|
27
|
+
if (a.eligibilityStateUpdatedAt !== b.eligibilityStateUpdatedAt) {
|
|
28
|
+
return a.eligibilityStateUpdatedAt > b.eligibilityStateUpdatedAt ? a : b;
|
|
29
|
+
}
|
|
30
|
+
return a.committedRevision > b.committedRevision ? a : b;
|
|
31
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { routingHandle } from "./22b2bce42f2d6806-handle.js";
|
|
2
|
+
export const CACHE_WARM_STOP_REASONS = [
|
|
3
|
+
'cost-cap',
|
|
4
|
+
'count-cap',
|
|
5
|
+
'deadline-missed',
|
|
6
|
+
'outside-hours',
|
|
7
|
+
'session-ended',
|
|
8
|
+
'unsupported',
|
|
9
|
+
];
|
|
10
|
+
/** Billing rejects unknown stop reasons; omit them rather than guessing. */
|
|
11
|
+
export function isCacheWarmStopReason(value) {
|
|
12
|
+
return CACHE_WARM_STOP_REASONS.some((reason) => reason === value);
|
|
13
|
+
}
|
|
14
|
+
const warned = new Set();
|
|
15
|
+
// Inspect descriptors rather than values so warning deduplication cannot invoke
|
|
16
|
+
// a broken bridge's getters a second time. Do not print policy contents.
|
|
17
|
+
function shape(value, seen = new Set()) {
|
|
18
|
+
if (typeof value !== 'object' || value === null) {
|
|
19
|
+
return `${typeof value}:${String(value)}`;
|
|
20
|
+
}
|
|
21
|
+
if (seen.has(value))
|
|
22
|
+
return 'cycle';
|
|
23
|
+
seen.add(value);
|
|
24
|
+
return `${Array.isArray(value) ? 'array' : 'object'}:{${Object.entries(Object.getOwnPropertyDescriptors(value))
|
|
25
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
26
|
+
.map(([key, descriptor]) => `${key}:${'value' in descriptor ? shape(descriptor.value, seen) : 'accessor'}`)
|
|
27
|
+
.join(',')}}`;
|
|
28
|
+
}
|
|
29
|
+
function invalid(kind, value) {
|
|
30
|
+
let fingerprint = 'unreadable';
|
|
31
|
+
try {
|
|
32
|
+
fingerprint = shape(value);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
// Uninspectable proxies share one diagnostic shape, without invoking getters.
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const key = `${kind}:${fingerprint}`;
|
|
39
|
+
if (!warned.has(key)) {
|
|
40
|
+
warned.add(key);
|
|
41
|
+
console.warn(`[common-auth] Ignoring invalid session ${kind} policy`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// A broken warning sink must not break a request either.
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
function record(value) {
|
|
50
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
51
|
+
}
|
|
52
|
+
function rejectAsync(value) {
|
|
53
|
+
if (typeof value.then !== 'function')
|
|
54
|
+
return false;
|
|
55
|
+
// Absorb a rejected promise from a bridge that broke the synchronous contract.
|
|
56
|
+
void Promise.resolve(value).catch(() => { });
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
function onlyKeys(value, keys) {
|
|
60
|
+
return Reflect.ownKeys(value).every((key) => typeof key === 'string' && keys.includes(key));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Read the session's current plan on every decision; never retain it across
|
|
64
|
+
* requests. Null means the plugin keeps its own settings. No I/O or exceptions.
|
|
65
|
+
*/
|
|
66
|
+
export function sessionCachePolicy(sessionId) {
|
|
67
|
+
let answer;
|
|
68
|
+
try {
|
|
69
|
+
const handle = routingHandle(sessionId);
|
|
70
|
+
if (!handle)
|
|
71
|
+
return null;
|
|
72
|
+
const read = handle.cache;
|
|
73
|
+
if (typeof read !== 'function')
|
|
74
|
+
return null;
|
|
75
|
+
answer = read.call(handle);
|
|
76
|
+
if (answer === null)
|
|
77
|
+
return null;
|
|
78
|
+
if (!record(answer) || rejectAsync(answer))
|
|
79
|
+
return invalid('cache', answer);
|
|
80
|
+
const { ttl, keep_warm, hours } = answer;
|
|
81
|
+
if (!onlyKeys(answer, ['ttl', 'keep_warm', 'hours']) ||
|
|
82
|
+
(ttl !== undefined &&
|
|
83
|
+
ttl !== 'none' &&
|
|
84
|
+
ttl !== 'short' &&
|
|
85
|
+
ttl !== 'long') ||
|
|
86
|
+
(keep_warm !== undefined &&
|
|
87
|
+
keep_warm !== 'off' &&
|
|
88
|
+
keep_warm !== 'hours' &&
|
|
89
|
+
keep_warm !== 'always' &&
|
|
90
|
+
keep_warm !== 'auto') ||
|
|
91
|
+
(ttl === 'none' && keep_warm !== undefined && keep_warm !== 'off') ||
|
|
92
|
+
(keep_warm !== 'hours' && 'hours' in answer)) {
|
|
93
|
+
return invalid('cache', answer);
|
|
94
|
+
}
|
|
95
|
+
const policy = {
|
|
96
|
+
...(ttl !== undefined ? { ttl } : {}),
|
|
97
|
+
...(keep_warm !== undefined ? { keep_warm } : {}),
|
|
98
|
+
};
|
|
99
|
+
if (keep_warm === 'hours') {
|
|
100
|
+
if (!record(hours) || !onlyKeys(hours, ['from', 'to', 'tz'])) {
|
|
101
|
+
return invalid('cache', answer);
|
|
102
|
+
}
|
|
103
|
+
const { from, to, tz } = hours;
|
|
104
|
+
const time = /^(?:[01]\d|2[0-3]):[0-5]\d$/;
|
|
105
|
+
if (typeof from !== 'string' ||
|
|
106
|
+
!time.test(from) ||
|
|
107
|
+
typeof to !== 'string' ||
|
|
108
|
+
!time.test(to) ||
|
|
109
|
+
from === to ||
|
|
110
|
+
typeof tz !== 'string' ||
|
|
111
|
+
tz.length === 0) {
|
|
112
|
+
return invalid('cache', answer);
|
|
113
|
+
}
|
|
114
|
+
new Intl.DateTimeFormat('en', { timeZone: tz });
|
|
115
|
+
policy.hours = { from, to, tz };
|
|
116
|
+
}
|
|
117
|
+
return policy;
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return invalid('cache', answer);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Read the oldest running plan tool afresh, without I/O or exceptions. */
|
|
124
|
+
export function sessionToolWait(sessionId) {
|
|
125
|
+
let answer;
|
|
126
|
+
try {
|
|
127
|
+
const handle = routingHandle(sessionId);
|
|
128
|
+
if (!handle)
|
|
129
|
+
return null;
|
|
130
|
+
const read = handle.tool_wait;
|
|
131
|
+
if (typeof read !== 'function')
|
|
132
|
+
return null;
|
|
133
|
+
answer = read.call(handle);
|
|
134
|
+
if (answer === null)
|
|
135
|
+
return null;
|
|
136
|
+
if (!record(answer) || rejectAsync(answer))
|
|
137
|
+
return invalid('tool_wait', answer);
|
|
138
|
+
const { since } = answer;
|
|
139
|
+
if (!onlyKeys(answer, ['since']) ||
|
|
140
|
+
typeof since !== 'number' ||
|
|
141
|
+
!Number.isSafeInteger(since) ||
|
|
142
|
+
since < 0) {
|
|
143
|
+
return invalid('tool_wait', answer);
|
|
144
|
+
}
|
|
145
|
+
return { since };
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return invalid('tool_wait', answer);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function report(sessionId, payload) {
|
|
152
|
+
const handle = routingHandle(sessionId);
|
|
153
|
+
if (!handle)
|
|
154
|
+
return;
|
|
155
|
+
const send = handle.report;
|
|
156
|
+
if (typeof send !== 'function')
|
|
157
|
+
return;
|
|
158
|
+
const pending = send.call(handle, payload);
|
|
159
|
+
if (record(pending) && typeof pending.then === 'function') {
|
|
160
|
+
void Promise.resolve(pending).catch(() => { });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Call only after the reply has been handed to the host, never on the awaited
|
|
165
|
+
* request path. Synchronous fire-and-forget, with no retries; the bridge owns
|
|
166
|
+
* redelivery of the newest touch. Accessor and report failures are absorbed.
|
|
167
|
+
*/
|
|
168
|
+
export function reportCacheTouch(sessionId, input) {
|
|
169
|
+
try {
|
|
170
|
+
const { at, ttlMs, source } = input;
|
|
171
|
+
if (!Number.isSafeInteger(at) ||
|
|
172
|
+
at < 0 ||
|
|
173
|
+
(ttlMs !== 'implicit' &&
|
|
174
|
+
(typeof ttlMs !== 'number' || !Number.isFinite(ttlMs) || ttlMs < 0)) ||
|
|
175
|
+
(source !== 'response' && source !== 'warm'))
|
|
176
|
+
return;
|
|
177
|
+
report(sessionId, { kind: 'cache_touch', at, ttl_ms: ttlMs, source });
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
// Optional reporting must never change the result of an auth request.
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Call off the request path, after handing the reply to the host. Report only
|
|
185
|
+
* a session that actually warmed, never one that was off from the start.
|
|
186
|
+
* Synchronous fire-and-forget; failures are absorbed and never retried here.
|
|
187
|
+
*/
|
|
188
|
+
export function reportCacheWarmStopped(sessionId, input) {
|
|
189
|
+
try {
|
|
190
|
+
const { at, reason } = input;
|
|
191
|
+
if (!Number.isSafeInteger(at) || at < 0 || typeof reason !== 'string')
|
|
192
|
+
return;
|
|
193
|
+
report(sessionId, { kind: 'cache_warm_stopped', at, reason });
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
// Optional reporting must never change the result of an auth request.
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { sameAccount } from "./f6d728f5e601f98b-request-history.js";
|
|
2
|
+
/** Compatibility is a wildcard test, not a transitive identity equivalence. */
|
|
3
|
+
export function isIdentityCompatible(a, b) {
|
|
4
|
+
return (sameAccount(a.account, b.account) &&
|
|
5
|
+
(a.identity === undefined ||
|
|
6
|
+
b.identity === undefined ||
|
|
7
|
+
a.identity === b.identity));
|
|
8
|
+
}
|
|
9
|
+
/** An unknown observation must not erase a previously verified identity. */
|
|
10
|
+
export function retainVerifiedIdentity(previous, current) {
|
|
11
|
+
if (!isIdentityCompatible(previous, current))
|
|
12
|
+
return current;
|
|
13
|
+
return { ...current, identity: current.identity ?? previous.identity };
|
|
14
|
+
}
|
|
15
|
+
export function validateIdentityFence(observed, authoritative, authorizeEpoch) {
|
|
16
|
+
if (!isIdentityCompatible(observed, authoritative))
|
|
17
|
+
return 'identity-replaced';
|
|
18
|
+
if (!authorizeEpoch(observed, authoritative))
|
|
19
|
+
return 'credential-epoch';
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
holdWaitMaxS,
|
|
3
|
+
readMaxSameAccountWait
|
|
4
|
+
} from "./1547bd74f4214d2d-index.js";
|
|
5
|
+
|
|
6
|
+
export { holdWaitMaxS, readMaxSameAccountWait };
|
|
7
|
+
export function maxSameAccountWait(storage) {
|
|
8
|
+
return readMaxSameAccountWait(storage?.routing?.maxSameAccountWait);
|
|
9
|
+
}
|
|
10
|
+
export function routingHoldWaitMaxS(input) {
|
|
11
|
+
if (input.accountKey)
|
|
12
|
+
return holdWaitMaxS({ ...input, accountKey: input.accountKey });
|
|
13
|
+
try {
|
|
14
|
+
const accessor = Reflect.get(globalThis, Symbol.for("cortexkit.routing"));
|
|
15
|
+
return typeof (typeof accessor === "function" ? accessor(input.sessionId) : void 0)?.accountFor === "function" ? 0 : void 0;
|
|
16
|
+
} catch {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
function withinHours(hours, at) {
|
|
2
|
+
const parts = new Intl.DateTimeFormat('en-GB', {
|
|
3
|
+
timeZone: hours.tz,
|
|
4
|
+
hour: '2-digit',
|
|
5
|
+
minute: '2-digit',
|
|
6
|
+
hourCycle: 'h23',
|
|
7
|
+
}).formatToParts(at);
|
|
8
|
+
const hour = parts.find((part) => part.type === 'hour')?.value;
|
|
9
|
+
const minute = parts.find((part) => part.type === 'minute')?.value;
|
|
10
|
+
const time = `${hour}:${minute}`;
|
|
11
|
+
return hours.from < hours.to
|
|
12
|
+
? time >= hours.from && time < hours.to
|
|
13
|
+
: time >= hours.from || time < hours.to;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Decide the next idle or tool-wait warm using a validated mode/window. The
|
|
17
|
+
* caller owns timers, session liveness and resetting counts/costs on real
|
|
18
|
+
* requests. A warm is due before expiry, never in its final safety margin.
|
|
19
|
+
*
|
|
20
|
+
* Auto applies a cost cap only when all three plugin-priced costs are supplied;
|
|
21
|
+
* without them this is timing only, and the plugin must apply its idle bounds.
|
|
22
|
+
* No provider prices are inferred. Off returns unsupported; callers must not
|
|
23
|
+
* report a stop for a session that never warmed.
|
|
24
|
+
*/
|
|
25
|
+
export function decideCacheKeep(input) {
|
|
26
|
+
const { mode, now, ttlMs, leadMs = 30_000, lastRealRequestAt, lastWarmAt, warmsSinceLastRealRequest, warmCostSoFar, nextWarmCost, rewriteCost, } = input;
|
|
27
|
+
if (mode === 'off')
|
|
28
|
+
return { action: 'stop', reason: 'unsupported' };
|
|
29
|
+
if (!Number.isFinite(ttlMs) || ttlMs <= 0) {
|
|
30
|
+
return { action: 'stop', reason: 'unsupported' };
|
|
31
|
+
}
|
|
32
|
+
if ((mode === 'hours' || mode === 'always') &&
|
|
33
|
+
warmsSinceLastRealRequest >= 24)
|
|
34
|
+
return { action: 'stop', reason: 'count-cap' };
|
|
35
|
+
if (mode === 'auto' &&
|
|
36
|
+
warmCostSoFar !== undefined &&
|
|
37
|
+
nextWarmCost !== undefined &&
|
|
38
|
+
rewriteCost !== undefined &&
|
|
39
|
+
warmCostSoFar + nextWarmCost >= rewriteCost)
|
|
40
|
+
return { action: 'stop', reason: 'cost-cap' };
|
|
41
|
+
const last = Math.max(lastRealRequestAt, lastWarmAt ?? lastRealRequestAt);
|
|
42
|
+
const expiresAt = last + ttlMs;
|
|
43
|
+
const lead = Math.min(Number.isFinite(leadMs) && leadMs > 0 ? leadMs : 30_000, ttlMs / 2);
|
|
44
|
+
const due = expiresAt - lead;
|
|
45
|
+
const at = Math.max(now, due);
|
|
46
|
+
// Replaying inside the expiry margin risks paying for a cold cache rewrite,
|
|
47
|
+
// so a warm that can no longer start in time stops as a missed deadline.
|
|
48
|
+
if (at >= expiresAt - Math.min(5_000, ttlMs / 10)) {
|
|
49
|
+
return { action: 'stop', reason: 'deadline-missed' };
|
|
50
|
+
}
|
|
51
|
+
if (mode === 'hours' &&
|
|
52
|
+
(!withinHours(input.hours, due) || !withinHours(input.hours, at)))
|
|
53
|
+
return { action: 'stop', reason: 'outside-hours' };
|
|
54
|
+
return now < due ? { action: 'wait', until: due } : { action: 'warm', at };
|
|
55
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
export function sameAccount(a, b) {
|
|
2
|
+
return (a.namespace === b.namespace &&
|
|
3
|
+
a.accountId === b.accountId &&
|
|
4
|
+
a.incarnation === b.incarnation);
|
|
5
|
+
}
|
|
6
|
+
function sameScope(a, b) {
|
|
7
|
+
if (a.kind !== b.kind)
|
|
8
|
+
return false;
|
|
9
|
+
if (a.kind === 'model')
|
|
10
|
+
return b.kind === 'model' && a.model === b.model;
|
|
11
|
+
if (a.kind === 'family')
|
|
12
|
+
return b.kind === 'family' && a.family === b.family;
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
export function createRequestHistory(input) {
|
|
16
|
+
return {
|
|
17
|
+
requestId: input.requestId,
|
|
18
|
+
originalUrl: input.originalUrl,
|
|
19
|
+
requestStartedAt: input.clockNow,
|
|
20
|
+
saw429: false,
|
|
21
|
+
tried: [],
|
|
22
|
+
excludedForRequest: [],
|
|
23
|
+
retains: [],
|
|
24
|
+
retainedAttempts: [],
|
|
25
|
+
excludedAttempts: [],
|
|
26
|
+
switchedAttempts: [],
|
|
27
|
+
switches: 0,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function isRequestExcluded(history, account, scope) {
|
|
31
|
+
return history.excludedForRequest.some((entry) => sameAccount(entry.account, account) && sameScope(entry.scope, scope));
|
|
32
|
+
}
|
|
33
|
+
/** Retain/exclude/switch events are committed by the executor, not by polling. */
|
|
34
|
+
export function reduceRequestHistory(history, event) {
|
|
35
|
+
switch (event.type) {
|
|
36
|
+
case 'response': {
|
|
37
|
+
const tried = history.tried.some((entry) => sameAccount(entry.account, event.account) &&
|
|
38
|
+
sameScope(entry.scope, event.scope))
|
|
39
|
+
? history.tried
|
|
40
|
+
: [
|
|
41
|
+
...history.tried,
|
|
42
|
+
{ account: { ...event.account }, scope: { ...event.scope } },
|
|
43
|
+
];
|
|
44
|
+
return {
|
|
45
|
+
...history,
|
|
46
|
+
tried,
|
|
47
|
+
saw429: history.saw429 || event.status === 429,
|
|
48
|
+
...(event.kind === 'payment-balance'
|
|
49
|
+
? { sawPaymentBalance: true }
|
|
50
|
+
: {}),
|
|
51
|
+
lastStatus: event.status,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
case 'retain': {
|
|
55
|
+
if (event.cause === 'credential-recovery' ||
|
|
56
|
+
history.retainedAttempts.includes(event.attemptId))
|
|
57
|
+
return history;
|
|
58
|
+
const previous = history.retains.find((entry) => sameAccount(entry.account, event.account));
|
|
59
|
+
return {
|
|
60
|
+
...history,
|
|
61
|
+
retainedAttempts: [...history.retainedAttempts, event.attemptId],
|
|
62
|
+
retains: [
|
|
63
|
+
...history.retains.filter((entry) => !sameAccount(entry.account, event.account)),
|
|
64
|
+
{
|
|
65
|
+
account: { ...event.account },
|
|
66
|
+
count: (previous?.count ?? 0) + 1,
|
|
67
|
+
startedAt: previous?.startedAt ?? event.clockNow,
|
|
68
|
+
lastRetainedAt: event.clockNow,
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
case 'exclude':
|
|
74
|
+
if (history.excludedAttempts.includes(event.attemptId))
|
|
75
|
+
return history;
|
|
76
|
+
return {
|
|
77
|
+
...history,
|
|
78
|
+
excludedAttempts: [...history.excludedAttempts, event.attemptId],
|
|
79
|
+
excludedForRequest: isRequestExcluded(history, event.account, event.scope)
|
|
80
|
+
? history.excludedForRequest
|
|
81
|
+
: [
|
|
82
|
+
...history.excludedForRequest,
|
|
83
|
+
{ account: { ...event.account }, scope: { ...event.scope } },
|
|
84
|
+
],
|
|
85
|
+
};
|
|
86
|
+
case 'switch':
|
|
87
|
+
return history.switchedAttempts.includes(event.attemptId)
|
|
88
|
+
? history
|
|
89
|
+
: {
|
|
90
|
+
...history,
|
|
91
|
+
switchedAttempts: [...history.switchedAttempts, event.attemptId],
|
|
92
|
+
switches: history.switches + 1,
|
|
93
|
+
};
|
|
94
|
+
case 'blocked':
|
|
95
|
+
return history.blockedSince === undefined
|
|
96
|
+
? { ...history, blockedSince: event.clockNow }
|
|
97
|
+
: history;
|
|
98
|
+
case 'selected': {
|
|
99
|
+
const { blockedSince: _, ...selected } = history;
|
|
100
|
+
return selected;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export const REQUEST_EXCLUSION_EFFECTS = Object.freeze({
|
|
105
|
+
refundHybridToken: true,
|
|
106
|
+
cancelDiscardedBody: true,
|
|
107
|
+
addMark: false,
|
|
108
|
+
addCooldown: false,
|
|
109
|
+
penalizeHealth: false,
|
|
110
|
+
chargeSwitch: false,
|
|
111
|
+
});
|
|
112
|
+
/** Run the returned effects once, then reselect with the returned history. */
|
|
113
|
+
export function commitRequestExclusion(history, entry) {
|
|
114
|
+
if (history.excludedAttempts.includes(entry.attemptId))
|
|
115
|
+
return { history };
|
|
116
|
+
return {
|
|
117
|
+
history: reduceRequestHistory(history, { type: 'exclude', ...entry }),
|
|
118
|
+
effects: REQUEST_EXCLUSION_EFFECTS,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export const CODEX_BODY_KEY_ORDER = [
|
|
2
|
+
"type",
|
|
3
|
+
"model",
|
|
4
|
+
"instructions",
|
|
5
|
+
"previous_response_id",
|
|
6
|
+
"input",
|
|
7
|
+
"tools",
|
|
8
|
+
"tool_choice",
|
|
9
|
+
"parallel_tool_calls",
|
|
10
|
+
"reasoning",
|
|
11
|
+
"store",
|
|
12
|
+
"stream",
|
|
13
|
+
"include",
|
|
14
|
+
"prompt_cache_key",
|
|
15
|
+
"text",
|
|
16
|
+
"generate",
|
|
17
|
+
"client_metadata"
|
|
18
|
+
];
|
|
19
|
+
export function orderCodexBody(body) {
|
|
20
|
+
const next = {};
|
|
21
|
+
for (const key of CODEX_BODY_KEY_ORDER)
|
|
22
|
+
if (key in body)
|
|
23
|
+
next[key] = body[key];
|
|
24
|
+
for (const key of Object.keys(body))
|
|
25
|
+
if (!(key in next))
|
|
26
|
+
next[key] = body[key];
|
|
27
|
+
return next;
|
|
28
|
+
}
|