@cortexkit/opencode-openai-auth 1.23.0 → 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 +8 -8
- package/dist/{index-897br01q.js → index-49ncs7dr.js} +1 -1
- package/dist/{index-s7dwe5n8.js → index-by5wfvqm.js} +20 -20
- package/dist/{index-bbrpg1ms.js → index-drm4dk5y.js} +1 -1
- package/dist/index-nd2ks7er.js +2 -0
- package/dist/index-t876xhkr.js +60 -0
- package/dist/{index-fasy4end.js → index-td4g5dc9.js} +1 -1
- package/dist/{index-7hcv94as.js → index-v8w8j1wx.js} +1 -1
- package/dist/{index-r31f36mn.js → index-v8wrmhyp.js} +1 -1
- package/dist/{index-bpx4spyr.js → index-wjp3q15r.js} +1 -1
- package/dist/{index-f340dq90.js → index-x00sxt2w.js} +10 -10
- package/dist/index.d.ts +4 -3
- 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/rpc/protocol.js +1 -1
- package/dist/rpc/rpc-client.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-preferences.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 +6 -6
- 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-2z4dbnsf.js +0 -60
- package/dist/index-x25m9spd.js +0 -2
- package/dist/map-D5PHhv4z.d.ts +0 -72
- /package/dist/{index-sx9jjkry.js → index-4ktepxn1.js} +0 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import {
|
|
2
|
+
askRouting,
|
|
3
|
+
askRoutingUntilCapacity,
|
|
4
|
+
reportRouting
|
|
5
|
+
} from "./bd5977eca210d2b8-index.js";
|
|
6
|
+
import { windowsFromQuotaMap } from "./bfbdd0f9aae56b47-pool-quota.js";
|
|
7
|
+
import { planOrdered } from "./abd062d633e29fd2-pool-routing.js";
|
|
8
|
+
import { routingHoldWaitMaxS } from "./f607353c4126272a-routing-wait.js";
|
|
9
|
+
import { isRecord } from "./775431e21f469203-record.js";
|
|
10
|
+
export const VAULT_ROUTING_REFUSAL_MESSAGE = "Thalamus routing declined this request, so it was not sent.";
|
|
11
|
+
export function vaultRoutingPlacementRetryMs(answer) {
|
|
12
|
+
if (!("refused" in answer))
|
|
13
|
+
return;
|
|
14
|
+
const { code, retryAfterSecs } = answer.refused;
|
|
15
|
+
if (code === "account_selection_refused" && typeof retryAfterSecs === "number" && retryAfterSecs > 0 && retryAfterSecs <= 5)
|
|
16
|
+
return retryAfterSecs * 1000;
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
export function hasVaultRoutingAccessor() {
|
|
20
|
+
try {
|
|
21
|
+
return typeof Reflect.get(globalThis, Symbol.for("cortexkit.routing")) === "function";
|
|
22
|
+
} catch {
|
|
23
|
+
return !1;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export const ROUTING_WAIT_MESSAGES = {
|
|
27
|
+
account_limit: "Waiting for the account limit to reset",
|
|
28
|
+
account_free: "Waiting for an account to come free",
|
|
29
|
+
quota_reading: "Checking account quota"
|
|
30
|
+
};
|
|
31
|
+
export function startVaultRoutingWait(input) {
|
|
32
|
+
const boundMs = input.nextMs === void 0 ? input.boundMs ?? 0 : input.nextMs - input.now;
|
|
33
|
+
if (!input.sessionId || !hasVaultRoutingAccessor() || boundMs < 2000)
|
|
34
|
+
return;
|
|
35
|
+
const report = {
|
|
36
|
+
sessionId: input.sessionId,
|
|
37
|
+
kind: "wait",
|
|
38
|
+
waitId: crypto.randomUUID(),
|
|
39
|
+
reason: input.reason,
|
|
40
|
+
...input.nextMs === void 0 ? {} : { nextMs: input.nextMs },
|
|
41
|
+
...input.attempt === void 0 ? {} : { attempt: input.attempt },
|
|
42
|
+
message: ROUTING_WAIT_MESSAGES[input.reason]
|
|
43
|
+
};
|
|
44
|
+
reportRouting({ ...report, phase: "start" });
|
|
45
|
+
let ended = !1;
|
|
46
|
+
return (outcome) => {
|
|
47
|
+
if (ended)
|
|
48
|
+
return;
|
|
49
|
+
ended = !0;
|
|
50
|
+
reportRouting({ ...report, phase: "end", outcome });
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export async function chooseVaultRouting(input) {
|
|
54
|
+
const { request, sessionId, continuation } = input;
|
|
55
|
+
if (!input.vaultMode || !hasVaultRoutingAccessor() || !sessionId || !request?.modelId || !continuation && (!input.fresh || request.retry))
|
|
56
|
+
return;
|
|
57
|
+
const eligible = input.eligibleIds && new Set(input.eligibleIds), rows = input.routing.rows.filter((row) => !eligible || eligible.has(row.id)), plan = continuation && planOrdered({
|
|
58
|
+
...input.routing,
|
|
59
|
+
rows,
|
|
60
|
+
placement: "roster",
|
|
61
|
+
mainId: null
|
|
62
|
+
}), admitted = new Set(!plan ? rows.filter((row) => input.routing.killswitch.get(row.id) !== !1).map((row) => row.id) : plan.kind === "send" ? plan.order.filter((id) => plan.tiers[id] !== "last-path") : []), candidates = input.routes.flatMap((route) => route.id !== continuation?.previousId && admitted.has(route.id) && route.identity && route.credentialId ? [
|
|
63
|
+
{
|
|
64
|
+
accountId: route.identity,
|
|
65
|
+
credentialId: route.credentialId,
|
|
66
|
+
id: route.id
|
|
67
|
+
}
|
|
68
|
+
] : []);
|
|
69
|
+
if (continuation) {
|
|
70
|
+
const { accountId, credentialId } = continuation.exhausted;
|
|
71
|
+
candidates.push({ accountId, credentialId, id: continuation.previousId });
|
|
72
|
+
}
|
|
73
|
+
if (candidates.length === 0)
|
|
74
|
+
return;
|
|
75
|
+
const holdWaitMaxS = routingHoldWaitMaxS({
|
|
76
|
+
...input.wait,
|
|
77
|
+
sessionId,
|
|
78
|
+
now: input.routing.now
|
|
79
|
+
}), ask = {
|
|
80
|
+
sessionId,
|
|
81
|
+
provider: "openai",
|
|
82
|
+
modelId: request.modelId,
|
|
83
|
+
candidates,
|
|
84
|
+
warmCredentialId: input.routes.find((route) => route.id === input.warmAccountId)?.credentialId,
|
|
85
|
+
refusals: !0,
|
|
86
|
+
holdWaitMaxS,
|
|
87
|
+
signal: input.signal
|
|
88
|
+
}, answer = continuation ? await askRouting({
|
|
89
|
+
...ask,
|
|
90
|
+
mode: "continue",
|
|
91
|
+
exhausted: continuation.exhausted,
|
|
92
|
+
requestId: crypto.randomUUID()
|
|
93
|
+
}) : input.capacityWait ? await askRoutingUntilCapacity({ ...ask, requestId: crypto.randomUUID() }, input.capacityWait) : await askRouting(ask);
|
|
94
|
+
if (!answer || "refused" in answer)
|
|
95
|
+
return answer;
|
|
96
|
+
if (answer.candidate.id !== continuation?.previousId)
|
|
97
|
+
return answer.candidate.id;
|
|
98
|
+
if (!answer.hold)
|
|
99
|
+
return { holdMs: 0 };
|
|
100
|
+
const holdMs = answer.hold.retry_after_ms;
|
|
101
|
+
if (!holdWaitMaxS || holdMs > holdWaitMaxS * 1000)
|
|
102
|
+
return { refused: { code: "account_selection_refused" } };
|
|
103
|
+
return { holdMs };
|
|
104
|
+
}
|
|
105
|
+
const RATE_LIMIT_HEADERS = [
|
|
106
|
+
"x-codex-primary-used-percent",
|
|
107
|
+
"x-codex-primary-window-minutes",
|
|
108
|
+
"x-codex-primary-reset-at",
|
|
109
|
+
"x-codex-secondary-used-percent",
|
|
110
|
+
"x-codex-secondary-window-minutes",
|
|
111
|
+
"x-codex-secondary-reset-at",
|
|
112
|
+
"retry-after",
|
|
113
|
+
"retry-after-ms"
|
|
114
|
+
], USAGE_LIMIT_CODES = new Set([
|
|
115
|
+
"usage_limit_reached",
|
|
116
|
+
"insufficient_quota",
|
|
117
|
+
"quota_exceeded"
|
|
118
|
+
]);
|
|
119
|
+
function resetFromRetryAfter(headers, now) {
|
|
120
|
+
const milliseconds = headers.get("retry-after-ms");
|
|
121
|
+
if (milliseconds?.trim() && Number.isFinite(Number(milliseconds)) && Number(milliseconds) >= 0)
|
|
122
|
+
return now + Number(milliseconds);
|
|
123
|
+
const value = headers.get("retry-after");
|
|
124
|
+
if (!value?.trim())
|
|
125
|
+
return;
|
|
126
|
+
const seconds = Number(value);
|
|
127
|
+
if (Number.isFinite(seconds) && seconds >= 0)
|
|
128
|
+
return now + seconds * 1000;
|
|
129
|
+
const date = Date.parse(value);
|
|
130
|
+
return Number.isFinite(date) ? date : void 0;
|
|
131
|
+
}
|
|
132
|
+
function providerResetAt(absolute, relative, now) {
|
|
133
|
+
return typeof absolute === "number" && Number.isFinite(absolute) && absolute > 0 ? absolute * 1000 : typeof relative === "number" && Number.isFinite(relative) && relative > 0 ? now + relative * 1000 : void 0;
|
|
134
|
+
}
|
|
135
|
+
export function reportVaultRoutingLimit(sessionId, receipt, limit, options = {}) {
|
|
136
|
+
if (!hasVaultRoutingAccessor() || !sessionId || !receipt?.accountIdentity)
|
|
137
|
+
return;
|
|
138
|
+
if (!USAGE_LIMIT_CODES.has(limit.window) && !["primary", "secondary", "5h", "7d"].includes(limit.window))
|
|
139
|
+
return;
|
|
140
|
+
const body = isRecord(options.body) ? options.body : void 0, response = isRecord(body?.response) ? body.response : body, detail = isRecord(response?.error) ? response.error : response, failed = isRecord(response?.failed) ? response.failed : void 0, named = typeof failed?.rate_limit_reached_type === "string" ? failed.rate_limit_reached_type : typeof detail?.rate_limit_reached_type === "string" ? detail.rate_limit_reached_type : typeof detail?.window === "string" ? detail.window : limit.window, snapshot = windowsFromQuotaMap(options.quota), slot = named === "primary" || named === "secondary" ? named : void 0, minutes = slot ? Number(options.headers?.get(`x-codex-${slot}-window-minutes`) ?? snapshot?.[slot]?.windowMinutes) : void 0, window = minutes === 300 ? "5h" : minutes === 10080 ? "7d" : named;
|
|
141
|
+
if (window !== "5h" && window !== "7d")
|
|
142
|
+
return;
|
|
143
|
+
const headerReset = slot ? Number(options.headers?.get(`x-codex-${slot}-reset-at`)) * 1000 : NaN, absolute = detail?.resets_at ?? failed?.resets_at, relative = detail?.resets_in_seconds ?? failed?.resets_in_seconds, now = options.now ?? Date.now(), providerReset = providerResetAt(absolute, relative, now), reset = limit.resetAt ?? providerReset, resetsAt = reset !== void 0 && Number.isFinite(reset) && reset > 0 ? reset : Number.isFinite(headerReset) && headerReset > 0 ? headerReset : options.headers ? resetFromRetryAfter(options.headers, now) : void 0;
|
|
144
|
+
reportRouting({
|
|
145
|
+
sessionId,
|
|
146
|
+
accountId: receipt.accountIdentity,
|
|
147
|
+
credentialId: receipt.credentialId,
|
|
148
|
+
outcome: "exhausted",
|
|
149
|
+
window,
|
|
150
|
+
...resetsAt !== void 0 ? { resetsAt } : {}
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
accountId: receipt.accountIdentity,
|
|
154
|
+
credentialId: receipt.credentialId,
|
|
155
|
+
limitWindow: window === "5h" ? "five_hour" : "seven_day",
|
|
156
|
+
observedAtMs: now,
|
|
157
|
+
...resetsAt !== void 0 ? { resetsAtMs: resetsAt } : {}
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
export function reportVaultRouting(sessionId, receipt, response) {
|
|
161
|
+
if (!sessionId || !receipt || response.failed || !hasVaultRoutingAccessor())
|
|
162
|
+
return;
|
|
163
|
+
const { status, headers } = response, identity = receipt.accountIdentity ? {
|
|
164
|
+
sessionId,
|
|
165
|
+
accountId: receipt.accountIdentity,
|
|
166
|
+
credentialId: receipt.credentialId
|
|
167
|
+
} : void 0;
|
|
168
|
+
if (status === 429 || status === 403) {
|
|
169
|
+
const envelope = isRecord(response.body) ? response.body : void 0, body = isRecord(envelope?.response) ? envelope.response : envelope, detail = isRecord(body?.error) ? body.error : body, reason = response.limit?.window, code = typeof detail?.code === "string" ? detail.code : void 0, type = typeof detail?.type === "string" && !["error", "response.failed"].includes(detail.type) ? detail.type : void 0, usage = [reason, code, type].find((value) => value !== void 0 && USAGE_LIMIT_CODES.has(value)), frameWindow = envelope?.type === "response.failed" && isRecord(body?.failed) && typeof body.failed.rate_limit_reached_type === "string" ? body.failed.rate_limit_reached_type : void 0, provedWindow = frameWindow ?? usage;
|
|
170
|
+
if (status === 429 && provedWindow) {
|
|
171
|
+
const named = frameWindow ?? (typeof detail?.rate_limit_reached_type === "string" ? detail.rate_limit_reached_type : typeof detail?.window === "string" ? detail.window : provedWindow), exhausted = reportVaultRoutingLimit(sessionId, receipt, { window: named, resetAt: response.limit?.resetAt }, { headers, quota: response.quota, now: response.now, body: envelope });
|
|
172
|
+
if (exhausted)
|
|
173
|
+
return { kind: "exhausted", exhausted };
|
|
174
|
+
}
|
|
175
|
+
if (response.backoff) {
|
|
176
|
+
const now = response.now ?? Date.now(), retryAtMs = Math.ceil(resetFromRetryAfter(headers, now) ?? response.limit?.resetAt ?? providerResetAt(detail?.resets_at, detail?.resets_in_seconds, now) ?? now + 5000);
|
|
177
|
+
if (identity)
|
|
178
|
+
reportRouting({
|
|
179
|
+
...identity,
|
|
180
|
+
kind: "transient_backoff",
|
|
181
|
+
retryAtMs,
|
|
182
|
+
providerCode: code || type || `http_${status}`
|
|
183
|
+
});
|
|
184
|
+
return {
|
|
185
|
+
kind: "backoff",
|
|
186
|
+
retryAtMs,
|
|
187
|
+
credentialId: receipt.credentialId,
|
|
188
|
+
...receipt.accountIdentity !== void 0 ? { accountIdentity: receipt.accountIdentity } : {}
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (!identity || status < 200 || status >= 300)
|
|
194
|
+
return;
|
|
195
|
+
const rateHeaders = {};
|
|
196
|
+
for (const name of RATE_LIMIT_HEADERS) {
|
|
197
|
+
const value = headers.get(name);
|
|
198
|
+
if (value !== null)
|
|
199
|
+
rateHeaders[name] = value;
|
|
200
|
+
}
|
|
201
|
+
reportRouting({
|
|
202
|
+
...identity,
|
|
203
|
+
outcome: "served",
|
|
204
|
+
headers: rateHeaders
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
export function vaultRoutingStreamFailed(event) {
|
|
208
|
+
return isRecord(event) && (event.type === "error" || event.type === "response.failed" || isRecord(event.response) && event.response.status === "failed");
|
|
209
|
+
}
|
|
210
|
+
export function vaultRoutingStreamMayFail(data) {
|
|
211
|
+
return data.includes('"response.failed"') || data.includes('"type":"error"') || data.includes('"error"') && /"type"\s*:\s*"error"/.test(data) || data.includes('"failed"') && /"status"\s*:\s*"failed"/.test(data);
|
|
212
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// Registries can be shared by independently bundled copies of common-auth.
|
|
2
|
+
const envelopeError = Symbol.for('@cortexkit/common-auth/limits-envelope-error');
|
|
3
|
+
/** Another writer replaced an envelope after this adapter already replaced one. */
|
|
4
|
+
export class LimitsEnvelopeConflictError extends Error {
|
|
5
|
+
[envelopeError] = 'conflict';
|
|
6
|
+
constructor() {
|
|
7
|
+
super('Conflicting limits envelope writers');
|
|
8
|
+
this.name = 'LimitsEnvelopeConflictError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** The envelope belongs to a schema this version must never overwrite. */
|
|
12
|
+
export class LimitsEnvelopeUnsupportedError extends Error {
|
|
13
|
+
[envelopeError] = 'unsupported';
|
|
14
|
+
constructor() {
|
|
15
|
+
super('Invalid or unsupported limits namespace schema');
|
|
16
|
+
this.name = 'LimitsEnvelopeUnsupportedError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function limitsEnvelopeAbandonReason(error) {
|
|
20
|
+
const reason = error && typeof error === 'object' && Reflect.get(error, envelopeError);
|
|
21
|
+
return reason === 'conflict' || reason === 'unsupported' ? reason : undefined;
|
|
22
|
+
}
|
|
23
|
+
function record(value) {
|
|
24
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
const revision = (value) => Number.isSafeInteger(value) && value >= 0;
|
|
27
|
+
const finite = (value) => typeof value === 'number' && Number.isFinite(value);
|
|
28
|
+
const cause = (value) => value === undefined || value === 'rate-limit' || value === 'payment-balance';
|
|
29
|
+
const arrayOf = (value, valid) => Array.isArray(value) && value.every(valid);
|
|
30
|
+
function account(value, namespace) {
|
|
31
|
+
return (record(value) &&
|
|
32
|
+
value.namespace === namespace &&
|
|
33
|
+
typeof value.accountId === 'string' &&
|
|
34
|
+
typeof value.incarnation === 'string');
|
|
35
|
+
}
|
|
36
|
+
function key(value, namespace) {
|
|
37
|
+
if (!record(value) || !record(value.scope))
|
|
38
|
+
return false;
|
|
39
|
+
const scope = value.scope;
|
|
40
|
+
return (account(value.account, namespace) &&
|
|
41
|
+
typeof value.window === 'string' &&
|
|
42
|
+
(scope.kind === 'pool' ||
|
|
43
|
+
(scope.kind === 'family' && typeof scope.family === 'string') ||
|
|
44
|
+
(scope.kind === 'model' && typeof scope.model === 'string')));
|
|
45
|
+
}
|
|
46
|
+
function validEnvelope(value, namespace) {
|
|
47
|
+
return (value.namespace === namespace &&
|
|
48
|
+
typeof value.generation === 'string' &&
|
|
49
|
+
revision(value.revision) &&
|
|
50
|
+
revision(value.lowWatermark) &&
|
|
51
|
+
arrayOf(value.marks, (mark) => record(mark) &&
|
|
52
|
+
key(mark.key, namespace) &&
|
|
53
|
+
['latest-evidence', 'max-until', 'overwrite'].includes(mark.mergeLaw) &&
|
|
54
|
+
arrayOf(mark.entries, (entry) => record(entry) &&
|
|
55
|
+
cause(entry.cause) &&
|
|
56
|
+
finite(entry.until) &&
|
|
57
|
+
finite(entry.evidenceAt) &&
|
|
58
|
+
revision(entry.baseRevision) &&
|
|
59
|
+
revision(entry.committedRevision))) &&
|
|
60
|
+
arrayOf(value.tombstones, (clear) => record(clear) &&
|
|
61
|
+
key(clear.key, namespace) &&
|
|
62
|
+
revision(clear.revision) &&
|
|
63
|
+
cause(clear.cause)) &&
|
|
64
|
+
arrayOf(value.operationIds, (op) => record(op) &&
|
|
65
|
+
typeof op.producerId === 'string' &&
|
|
66
|
+
typeof op.operationId === 'string' &&
|
|
67
|
+
revision(op.revision)) &&
|
|
68
|
+
arrayOf(value.access, (access) => record(access) &&
|
|
69
|
+
account(access.account, namespace) &&
|
|
70
|
+
finite(access.eligibilityStateUpdatedAt) &&
|
|
71
|
+
revision(access.committedRevision)));
|
|
72
|
+
}
|
|
73
|
+
/** Public calls and background envelope-loss repairs share one replacement allowance. */
|
|
74
|
+
export class EnvelopeGuard {
|
|
75
|
+
namespace;
|
|
76
|
+
logger;
|
|
77
|
+
replaced = false;
|
|
78
|
+
terminal;
|
|
79
|
+
constructor(namespace, logger) {
|
|
80
|
+
this.namespace = namespace;
|
|
81
|
+
this.logger = logger;
|
|
82
|
+
}
|
|
83
|
+
assertWritable() {
|
|
84
|
+
if (this.terminal)
|
|
85
|
+
throw this.terminal;
|
|
86
|
+
}
|
|
87
|
+
decode(value) {
|
|
88
|
+
this.assertWritable();
|
|
89
|
+
if (value === undefined)
|
|
90
|
+
return {};
|
|
91
|
+
let replacement;
|
|
92
|
+
if (!record(value))
|
|
93
|
+
replacement = 'malformed';
|
|
94
|
+
else if (value.schemaVersion !== 1) {
|
|
95
|
+
this.terminal = new LimitsEnvelopeUnsupportedError();
|
|
96
|
+
throw this.terminal;
|
|
97
|
+
}
|
|
98
|
+
else if (typeof value.namespace === 'string' &&
|
|
99
|
+
value.namespace !== this.namespace)
|
|
100
|
+
replacement = 'foreign-namespace';
|
|
101
|
+
else if (!validEnvelope(value, this.namespace))
|
|
102
|
+
replacement = 'malformed';
|
|
103
|
+
if (!replacement)
|
|
104
|
+
return { snapshot: value };
|
|
105
|
+
if (this.replaced) {
|
|
106
|
+
this.terminal = new LimitsEnvelopeConflictError();
|
|
107
|
+
throw this.terminal;
|
|
108
|
+
}
|
|
109
|
+
return { replacement };
|
|
110
|
+
}
|
|
111
|
+
didCommit(replacement) {
|
|
112
|
+
if (!replacement || this.replaced)
|
|
113
|
+
return false;
|
|
114
|
+
this.replaced = true;
|
|
115
|
+
try {
|
|
116
|
+
this.logger?.warn(`limits envelope replaced: ${replacement}`);
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
// Diagnostics cannot change a durable replacement's result.
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { resolveReset } from "./b3c11de5f36e617a-reset.js";
|
|
2
|
+
const delta = /^(?:\d+(?:\.\d*)?|\.\d+)$/;
|
|
3
|
+
const httpDate = /^(?:[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT|[A-Za-z]+, \d{2}-[A-Za-z]{3}-\d{2} \d{2}:\d{2}:\d{2} GMT|[A-Za-z]{3} [A-Za-z]{3} {1,2}\d{1,2} \d{2}:\d{2}:\d{2} \d{4})$/;
|
|
4
|
+
/** HTTP dates include a month name; Date.parse alone also accepts numbers and junk. */
|
|
5
|
+
function retryAfter(value, now) {
|
|
6
|
+
if (value === null)
|
|
7
|
+
return undefined;
|
|
8
|
+
const text = value.trim();
|
|
9
|
+
if (delta.test(text)) {
|
|
10
|
+
const ms = Number(text) * 1000;
|
|
11
|
+
return Number.isFinite(ms) ? ms : undefined;
|
|
12
|
+
}
|
|
13
|
+
if (!httpDate.test(text))
|
|
14
|
+
return undefined;
|
|
15
|
+
const date = Date.parse(text);
|
|
16
|
+
return Number.isFinite(date) ? Math.max(0, date - now) : undefined;
|
|
17
|
+
}
|
|
18
|
+
function resetDuration(value) {
|
|
19
|
+
const text = value.trim();
|
|
20
|
+
if (delta.test(text)) {
|
|
21
|
+
const ms = Number(text) * 1000;
|
|
22
|
+
return Number.isFinite(ms) ? ms : undefined;
|
|
23
|
+
}
|
|
24
|
+
const parts = text.matchAll(/(\d+(?:\.\d*)?|\.\d+)(ms|s|m|h|d)/g);
|
|
25
|
+
let consumed = '';
|
|
26
|
+
let ms = 0;
|
|
27
|
+
const units = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 };
|
|
28
|
+
for (const part of parts) {
|
|
29
|
+
consumed += part[0];
|
|
30
|
+
ms += Number(part[1]) * units[part[2]];
|
|
31
|
+
}
|
|
32
|
+
return consumed === text && consumed !== '' && Number.isFinite(ms)
|
|
33
|
+
? ms
|
|
34
|
+
: undefined;
|
|
35
|
+
}
|
|
36
|
+
export function resolveRetryAfter(headers, now) {
|
|
37
|
+
return resolveReset([
|
|
38
|
+
{
|
|
39
|
+
source: 'retry-after',
|
|
40
|
+
value: retryAfter(headers.get('retry-after'), now),
|
|
41
|
+
unit: 'delay-ms',
|
|
42
|
+
zero: 'accept',
|
|
43
|
+
},
|
|
44
|
+
], now);
|
|
45
|
+
}
|
|
46
|
+
/** Importing this module alone does not enable its header reset sources. */
|
|
47
|
+
export function resolveHeaderReset(headers, now) {
|
|
48
|
+
const retry = resolveRetryAfter(headers, now);
|
|
49
|
+
if (retry)
|
|
50
|
+
return retry;
|
|
51
|
+
let earliest;
|
|
52
|
+
for (const [name, value] of headers) {
|
|
53
|
+
if (!name.startsWith('x-ratelimit-reset-'))
|
|
54
|
+
continue;
|
|
55
|
+
const reset = resolveReset([
|
|
56
|
+
{
|
|
57
|
+
source: name,
|
|
58
|
+
value: resetDuration(value),
|
|
59
|
+
unit: 'delay-ms',
|
|
60
|
+
zero: 'skip',
|
|
61
|
+
},
|
|
62
|
+
], now);
|
|
63
|
+
if (reset && (!earliest || reset.resetAt < earliest.resetAt))
|
|
64
|
+
earliest = reset;
|
|
65
|
+
}
|
|
66
|
+
return earliest;
|
|
67
|
+
}
|
|
68
|
+
function hintSignal(input, reset) {
|
|
69
|
+
return {
|
|
70
|
+
account: input.account,
|
|
71
|
+
kind: input.status === 429 ? 'rate-limit' : 'capacity',
|
|
72
|
+
reason: input.status === 429 ? 'rate-limit' : 'capacity',
|
|
73
|
+
status: input.status,
|
|
74
|
+
scope: { kind: 'pool' },
|
|
75
|
+
observedAt: input.now,
|
|
76
|
+
windows: [
|
|
77
|
+
{
|
|
78
|
+
window: 'retry',
|
|
79
|
+
confirmed: false,
|
|
80
|
+
confirmedByReading: false,
|
|
81
|
+
observedAt: input.now,
|
|
82
|
+
provenance: 'http-status',
|
|
83
|
+
...(reset ? { resetAt: reset.resetAt, resetSource: reset.source } : {}),
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
...(reset
|
|
87
|
+
? {
|
|
88
|
+
retryAfterMs: reset.resetAt - input.now,
|
|
89
|
+
retryAfterSource: reset.source,
|
|
90
|
+
}
|
|
91
|
+
: {}),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** Generic status hints never assert quota exhaustion, even on a 429. */
|
|
95
|
+
export function classifyHeaders(input) {
|
|
96
|
+
if (input.status !== 429 && input.status !== 503)
|
|
97
|
+
return undefined;
|
|
98
|
+
return hintSignal(input, resolveHeaderReset(input.headers, input.now));
|
|
99
|
+
}
|
|
100
|
+
/** Retry-After-only classification leaves default timers to the marker. */
|
|
101
|
+
export function classifyRetryAfter(input) {
|
|
102
|
+
if (input.status !== 429)
|
|
103
|
+
return undefined;
|
|
104
|
+
const raw = input.headers.get('retry-after')?.trim() ?? '';
|
|
105
|
+
const numeric = raw === '' ? Number.NaN : Number(raw);
|
|
106
|
+
const parsed = Number.isFinite(numeric) && numeric >= 0
|
|
107
|
+
? resolveReset([
|
|
108
|
+
{
|
|
109
|
+
source: 'retry-after',
|
|
110
|
+
value: numeric * 1000,
|
|
111
|
+
unit: 'delay-ms',
|
|
112
|
+
zero: 'accept',
|
|
113
|
+
},
|
|
114
|
+
], input.now)
|
|
115
|
+
: resolveRetryAfter(input.headers, input.now);
|
|
116
|
+
const usable = parsed && (Number.isFinite(numeric) || parsed.resetAt > input.now)
|
|
117
|
+
? parsed
|
|
118
|
+
: undefined;
|
|
119
|
+
// The marker supplies its default timer; that timer is not a known reset.
|
|
120
|
+
return hintSignal(input, usable);
|
|
121
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { spawn as defaultSpawn } from "node:child_process";
|
|
2
|
+
import { win32 } from "node:path";
|
|
3
|
+
/** Opens a URL with the platform's opener; false when it could not start. */
|
|
4
|
+
export function openBrowserForMenu(url, platform = process.platform, spawn = defaultSpawn) {
|
|
5
|
+
try {
|
|
6
|
+
if (platform === 'win32') {
|
|
7
|
+
// Start-Process opens the URL with its associated app. Base64 UTF-16LE
|
|
8
|
+
// keeps cmd from parsing the URL; Microsoft documents both options:
|
|
9
|
+
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.5
|
|
10
|
+
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1#-encodedcommand-base64encodedcommand
|
|
11
|
+
// PowerShell is launched without waiting for it: a cold Windows
|
|
12
|
+
// PowerShell start took about 6 s on a Windows Server 2022 runner, and a
|
|
13
|
+
// waiting caller with a short timeout killed it before the browser
|
|
14
|
+
// opened. It is not detached: a detached PowerShell has no console and
|
|
15
|
+
// never ran the command on any Windows runner.
|
|
16
|
+
const command = `Start-Process '${url.replaceAll("'", "''")}'`;
|
|
17
|
+
// Spawn PowerShell by its system path: a bare name can resolve to a
|
|
18
|
+
// program planted in the current directory or earlier on PATH.
|
|
19
|
+
const child = spawn(win32.resolve(process.env.SystemRoot ?? process.env.windir ?? 'C:\\Windows', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'), [
|
|
20
|
+
'-NoProfile',
|
|
21
|
+
'-NonInteractive',
|
|
22
|
+
'-WindowStyle',
|
|
23
|
+
'Hidden',
|
|
24
|
+
'-EncodedCommand',
|
|
25
|
+
Buffer.from(command, 'utf16le').toString('base64'),
|
|
26
|
+
], { stdio: 'ignore', windowsHide: true });
|
|
27
|
+
// A failed launch reports asynchronously; without a listener it would
|
|
28
|
+
// become an uncaught exception after this function has returned.
|
|
29
|
+
child.on('error', () => { });
|
|
30
|
+
child.unref();
|
|
31
|
+
// Node leaves pid undefined when the process could not be started.
|
|
32
|
+
return child.pid !== undefined;
|
|
33
|
+
}
|
|
34
|
+
const child = spawn(platform === 'darwin' ? 'open' : 'xdg-open', [url], {
|
|
35
|
+
stdio: 'ignore',
|
|
36
|
+
});
|
|
37
|
+
// A failed launch reports asynchronously; without a listener it would
|
|
38
|
+
// become an uncaught exception after this function has returned.
|
|
39
|
+
child.on('error', () => { });
|
|
40
|
+
child.unref();
|
|
41
|
+
// The URL handler's process may outlive the caller; a pid means launch began.
|
|
42
|
+
return child.pid !== undefined;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// Browser launch is best effort; the printed URL remains the fallback.
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function printFlow(context, flow) {
|
|
50
|
+
context.print('');
|
|
51
|
+
context.print('Open this URL in your browser and complete sign-in:');
|
|
52
|
+
context.print('');
|
|
53
|
+
context.print(flow.url);
|
|
54
|
+
context.print('');
|
|
55
|
+
if (flow.instructions) {
|
|
56
|
+
context.print(flow.instructions);
|
|
57
|
+
context.print('');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Runs a login for the menu: a browser login first and, when no browser can
|
|
62
|
+
* be started, a device-code login instead, so a headless machine can still
|
|
63
|
+
* add an account. The URL is always printed, so an operator can open it by
|
|
64
|
+
* hand when the opener reports success but nothing appears.
|
|
65
|
+
*/
|
|
66
|
+
export async function runMenuLogin(login, context) {
|
|
67
|
+
const abort = new AbortController();
|
|
68
|
+
let flow = await login.begin({ headless: false, signal: abort.signal });
|
|
69
|
+
// Attached before the opener runs: an opener failure aborts this flow in
|
|
70
|
+
// the same turn, and its rejection must not surface as unhandled.
|
|
71
|
+
void flow.completion.catch(() => { });
|
|
72
|
+
printFlow(context, flow);
|
|
73
|
+
let opened = false;
|
|
74
|
+
try {
|
|
75
|
+
opened =
|
|
76
|
+
(await (login.openBrowser ?? openBrowserForMenu)(flow.url)) !== false;
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
opened = false;
|
|
80
|
+
}
|
|
81
|
+
if (!opened) {
|
|
82
|
+
abort.abort();
|
|
83
|
+
context.print('Could not open a browser. Switching to device authorization.');
|
|
84
|
+
context.print('');
|
|
85
|
+
flow = await login.begin({ headless: true });
|
|
86
|
+
printFlow(context, flow);
|
|
87
|
+
}
|
|
88
|
+
return flow.completion;
|
|
89
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { isMessageWithRole } from "./fda579e5a0a54165-codex-effort.js";
|
|
3
|
+
import { uuidV7 } from "./e8fda84e90176436-uuid-v7.js";
|
|
4
|
+
export const MAX_FRAME_TURN_SESSIONS = 1024;
|
|
5
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value), isUserMessage = (item) => isMessageWithRole(item, "user"), isModelOrToolItem = (item) => isRecord(item) && (item.role === "assistant" || typeof item.type === "string" && item.type !== "message" && item.type !== "configuration_update");
|
|
6
|
+
export function userMessageFingerprint(item) {
|
|
7
|
+
return createHash("sha256").update(JSON.stringify(item)).digest("hex");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class FrameTurnState {
|
|
11
|
+
runtime;
|
|
12
|
+
maxSessions;
|
|
13
|
+
#sessions = new Map;
|
|
14
|
+
constructor(runtime, maxSessions = MAX_FRAME_TURN_SESSIONS) {
|
|
15
|
+
this.runtime = runtime;
|
|
16
|
+
this.maxSessions = maxSessions;
|
|
17
|
+
}
|
|
18
|
+
advance(sessionID, frame, runtime = this.runtime) {
|
|
19
|
+
const prior = this.#sessions.get(sessionID), input = Array.isArray(frame.input) ? frame.input : [], users = input.filter(isUserMessage), lastUser = input.findLastIndex(isUserMessage), lastFingerprint = lastUser < 0 ? void 0 : userMessageFingerprint(input[lastUser]), chained = typeof frame.previous_response_id === "string" && frame.previous_response_id.length > 0;
|
|
20
|
+
let startsTurn, fingerprint, userCount;
|
|
21
|
+
if (chained) {
|
|
22
|
+
startsTurn = users.length > 0;
|
|
23
|
+
fingerprint = lastFingerprint ?? prior?.fingerprint;
|
|
24
|
+
userCount = prior?.userCount === void 0 ? void 0 : prior.userCount + users.length;
|
|
25
|
+
} else {
|
|
26
|
+
fingerprint = lastFingerprint;
|
|
27
|
+
userCount = users.length;
|
|
28
|
+
const opensTurn = lastUser >= 0 && lastUser > input.findLastIndex(isModelOrToolItem), resend = prior !== void 0 && !prior.completed && prior.fingerprint === fingerprint && prior.userCount === userCount;
|
|
29
|
+
startsTurn = opensTurn && !resend;
|
|
30
|
+
}
|
|
31
|
+
const turn = startsTurn || !prior ? {
|
|
32
|
+
turnID: (runtime?.uuid ?? uuidV7)(),
|
|
33
|
+
turnStartedAt: (runtime?.now ?? Date.now)()
|
|
34
|
+
} : { turnID: prior.turnID, turnStartedAt: prior.turnStartedAt };
|
|
35
|
+
this.#set(sessionID, {
|
|
36
|
+
...turn,
|
|
37
|
+
completed: !1,
|
|
38
|
+
fingerprint,
|
|
39
|
+
userCount
|
|
40
|
+
});
|
|
41
|
+
return turn;
|
|
42
|
+
}
|
|
43
|
+
completed(sessionID, expected) {
|
|
44
|
+
const record = this.#sessions.get(sessionID);
|
|
45
|
+
if (record && (!expected || record === expected))
|
|
46
|
+
this.#set(sessionID, { ...record, completed: !0 });
|
|
47
|
+
}
|
|
48
|
+
get(sessionID) {
|
|
49
|
+
return this.#sessions.get(sessionID);
|
|
50
|
+
}
|
|
51
|
+
forget(sessionID) {
|
|
52
|
+
this.#sessions.delete(sessionID);
|
|
53
|
+
}
|
|
54
|
+
#set(sessionID, record) {
|
|
55
|
+
this.#sessions.delete(sessionID);
|
|
56
|
+
this.#sessions.set(sessionID, record);
|
|
57
|
+
while (this.#sessions.size > this.maxSessions) {
|
|
58
|
+
const oldest = this.#sessions.keys().next().value;
|
|
59
|
+
if (oldest === void 0)
|
|
60
|
+
break;
|
|
61
|
+
this.#sessions.delete(oldest);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export function frameRequestMetadata(session, turn) {
|
|
66
|
+
return {
|
|
67
|
+
threadID: session.threadID,
|
|
68
|
+
windowID: session.windowID,
|
|
69
|
+
turnID: turn.turnID,
|
|
70
|
+
turnStartedAt: turn.turnStartedAt
|
|
71
|
+
};
|
|
72
|
+
}
|