@humain/humain-code 0.4.9 → 0.4.10

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.
Files changed (112) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/bundle/agent-monitor.js +1 -1
  3. package/dist/bundle/chunks/bedrock-converse-stream.js +39 -45
  4. package/dist/bundle/chunks/{chunk-6FVNSW6B.js → chunk-2QTTNAD4.js} +3353 -3938
  5. package/dist/bundle/chunks/chunk-76X5KOIA.js +28 -0
  6. package/dist/bundle/chunks/{chunk-KRMTBTWU.js → chunk-77EWF2NW.js} +1 -1
  7. package/dist/bundle/chunks/{chunk-RBKDBF5G.js → chunk-AS2Y2MO6.js} +36 -36
  8. package/dist/bundle/chunks/{chunk-4LAKI5IL.js → chunk-AUFEF24K.js} +1 -1
  9. package/dist/bundle/chunks/{chunk-P4YMUUSK.js → chunk-BCF7KZ3R.js} +739 -730
  10. package/dist/bundle/chunks/{chunk-W2QRCQBX.js → chunk-FF26YENK.js} +3 -4
  11. package/dist/bundle/chunks/{chunk-UZ5FVVCP.js → chunk-HMWAH7ZY.js} +1 -1
  12. package/dist/bundle/chunks/{chunk-F7UMX5BV.js → chunk-HSDTSUZE.js} +13 -2
  13. package/dist/bundle/chunks/chunk-I2ZWZAIV.js +751 -0
  14. package/dist/bundle/chunks/{chunk-GUUAEK6G.js → chunk-LCOHFEZZ.js} +1 -1
  15. package/dist/bundle/chunks/{chunk-Z5LYSSTY.js → chunk-QSPDLT2R.js} +2 -2
  16. package/dist/bundle/chunks/{chunk-UBQSONVR.js → chunk-R5VIAWGA.js} +103 -145
  17. package/dist/bundle/chunks/{chunk-EMYNPLY5.js → chunk-V5ZF7Q7L.js} +1 -1
  18. package/dist/bundle/chunks/{chunk-LQ4S7CRR.js → chunk-VYIIPF57.js} +1 -1
  19. package/dist/bundle/chunks/chunk-WBJN6PEI.js +43 -0
  20. package/dist/bundle/chunks/{chunk-SWRU4BOC.js → chunk-XGO43DKL.js} +1 -1
  21. package/dist/bundle/chunks/{chunk-PMISFIBM.js → chunk-XMHLSV3I.js} +1 -1
  22. package/dist/bundle/chunks/{chunk-3E4SMILM.js → chunk-YJBE3IGR.js} +14 -14
  23. package/dist/bundle/chunks/{cli-ZKT34WPW.js → cli-5ACW3WND.js} +2 -2
  24. package/dist/bundle/chunks/{compat-HEUVP6UJ.js → compat-TQKA4I7M.js} +1 -1
  25. package/dist/bundle/chunks/{dist-NU2EIVT6.js → dist-WHC67QRM.js} +676 -550
  26. package/dist/bundle/chunks/dist-cjs-RL2OCAP2.js +179 -0
  27. package/dist/bundle/chunks/{easter-egg-3d-5KKDWEIA.js → easter-egg-3d-3L3IKAT6.js} +1 -1
  28. package/dist/bundle/chunks/{esm-NCUV2DF7.js → esm-763X2IA7.js} +1 -1
  29. package/dist/bundle/chunks/event-streams-R5OECII4.js +2 -0
  30. package/dist/bundle/chunks/{execute-OSBDNQU5.js → execute-4X6G65NZ.js} +1 -1
  31. package/dist/bundle/chunks/{extract-PYRMDATF.js → extract-DLZ425VI.js} +4 -4
  32. package/dist/bundle/chunks/github-copilot.js +32 -31
  33. package/dist/bundle/chunks/{google-generative-ai-H2F5Y5H2.js → google-generative-ai-JD2FVOIX.js} +1 -1
  34. package/dist/bundle/chunks/{google-vertex-LFWD2AOO.js → google-vertex-PFBMDCBW.js} +1 -1
  35. package/dist/bundle/chunks/{host-6ZA6LK4C.js → host-4J7H7KDS.js} +1 -1
  36. package/dist/bundle/chunks/{node-XGNY74WT.js → node-W47VUEGE.js} +1 -1
  37. package/dist/bundle/chunks/{runtime-JFLZUNYP.js → runtime-SI76JZNE.js} +1 -1
  38. package/dist/bundle/chunks/virtual-modules-ID2GA4GE.js +5 -0
  39. package/dist/bundle/cli-runtime.js +3 -3
  40. package/dist/bundle/flow/package.json +1 -1
  41. package/dist/bundle/index.js +17 -17
  42. package/dist/bundle/rpc-entry.js +3 -2
  43. package/dist/core/account-reporting.d.ts +109 -0
  44. package/dist/core/account-reporting.d.ts.map +1 -0
  45. package/dist/core/account-reporting.js +328 -0
  46. package/dist/core/account-reporting.js.map +1 -0
  47. package/dist/core/agent-session.d.ts +22 -2
  48. package/dist/core/agent-session.d.ts.map +1 -1
  49. package/dist/core/agent-session.js +131 -0
  50. package/dist/core/agent-session.js.map +1 -1
  51. package/dist/core/anthropic-account-reporting.d.ts +76 -0
  52. package/dist/core/anthropic-account-reporting.d.ts.map +1 -0
  53. package/dist/core/anthropic-account-reporting.js +380 -0
  54. package/dist/core/anthropic-account-reporting.js.map +1 -0
  55. package/dist/core/bedrock-account-reporting.d.ts +82 -0
  56. package/dist/core/bedrock-account-reporting.d.ts.map +1 -0
  57. package/dist/core/bedrock-account-reporting.js +402 -0
  58. package/dist/core/bedrock-account-reporting.js.map +1 -0
  59. package/dist/core/model-runtime.d.ts +17 -0
  60. package/dist/core/model-runtime.d.ts.map +1 -1
  61. package/dist/core/model-runtime.js +64 -10
  62. package/dist/core/model-runtime.js.map +1 -1
  63. package/dist/core/oauth-account-pool.d.ts +4 -0
  64. package/dist/core/oauth-account-pool.d.ts.map +1 -1
  65. package/dist/core/oauth-account-pool.js +10 -3
  66. package/dist/core/oauth-account-pool.js.map +1 -1
  67. package/dist/humain/flow-accounts-host.d.ts +120 -0
  68. package/dist/humain/flow-accounts-host.d.ts.map +1 -0
  69. package/dist/humain/flow-accounts-host.js +346 -0
  70. package/dist/humain/flow-accounts-host.js.map +1 -0
  71. package/dist/humain/flow-command.d.ts +28 -0
  72. package/dist/humain/flow-command.d.ts.map +1 -0
  73. package/dist/humain/flow-command.js +226 -0
  74. package/dist/humain/flow-command.js.map +1 -0
  75. package/dist/humain/flow-loader.d.ts +4 -2
  76. package/dist/humain/flow-loader.d.ts.map +1 -1
  77. package/dist/humain/flow-loader.js +120 -91
  78. package/dist/humain/flow-loader.js.map +1 -1
  79. package/dist/humain/node-account-failure.d.ts +18 -0
  80. package/dist/humain/node-account-failure.d.ts.map +1 -0
  81. package/dist/humain/node-account-failure.js +73 -0
  82. package/dist/humain/node-account-failure.js.map +1 -0
  83. package/dist/humain/node-account-fallback.d.ts +85 -0
  84. package/dist/humain/node-account-fallback.d.ts.map +1 -0
  85. package/dist/humain/node-account-fallback.js +175 -0
  86. package/dist/humain/node-account-fallback.js.map +1 -0
  87. package/dist/humain/node-http.d.ts.map +1 -1
  88. package/dist/humain/node-http.js +11 -0
  89. package/dist/humain/node-http.js.map +1 -1
  90. package/dist/humain/node-provider.d.ts +4 -0
  91. package/dist/humain/node-provider.d.ts.map +1 -1
  92. package/dist/humain/node-provider.js +11 -0
  93. package/dist/humain/node-provider.js.map +1 -1
  94. package/dist/index.d.ts +4 -0
  95. package/dist/index.d.ts.map +1 -1
  96. package/dist/index.js.map +1 -1
  97. package/dist/modes/interactive/components/login-dialog.d.ts +2 -0
  98. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
  99. package/dist/modes/interactive/components/login-dialog.js +5 -0
  100. package/dist/modes/interactive/components/login-dialog.js.map +1 -1
  101. package/dist/modes/interactive/interactive-mode.d.ts +26 -0
  102. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  103. package/dist/modes/interactive/interactive-mode.js +242 -19
  104. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  105. package/docs/flow-profiles.md +57 -5
  106. package/docs/flow.md +119 -37
  107. package/docs/providers.md +37 -0
  108. package/npm-shrinkwrap.json +48 -6
  109. package/package.json +3 -2
  110. package/dist/bundle/chunks/chunk-JOKJ3CEL.js +0 -27
  111. package/dist/bundle/chunks/event-streams-CGCQQL5M.js +0 -2
  112. package/dist/bundle/chunks/virtual-modules-NPWQ6NBS.js +0 -5
@@ -18,6 +18,9 @@ import { basename, dirname } from "node:path";
18
18
  import { runToolCall, } from "@earendil-works/pi-agent-core";
19
19
  import { contentText, getAssistantRetryDelayMs, getCurrentSystemMessage, retryDelayMs } from "@earendil-works/pi-ai";
20
20
  import { clampThinkingLevel, cleanupSessionResources, getSupportedThinkingLevels, isContextOverflow, isRecoverableLength, isRetryableAssistantError, modelsAreEqual, resetApiProviders, streamSimple, } from "@earendil-works/pi-ai/compat";
21
+ import { getNodeAccountFailure, } from "../humain/node-account-failure.js";
22
+ import { formatNodeSwitchNotice, isNodeFallbackTarget, NODE_FALLBACK_NONE_MESSAGE, nodeFailureExhaustedUntil, } from "../humain/node-account-fallback.js";
23
+ import { HUMAIN_NODE_BASE_URL_ENV, HUMAIN_NODE_PROVIDER } from "../humain/node-provider.js";
21
24
  import { closeSessionFlowControl, rebindSessionFlowControl } from "../humain/trace-capture/control-host.js";
22
25
  import { getThemeByName, theme } from "../modes/interactive/theme/theme.js";
23
26
  import { stripFrontmatter } from "../utils/frontmatter.js";
@@ -1011,6 +1014,9 @@ export class AgentSession {
1011
1014
  * view. `agent_end.willRetry` must be computed before the async failover runs.
1012
1015
  */
1013
1016
  _predictOAuthAccountFailover(message) {
1017
+ const node = this._nodeFallbackTrigger(message, this._oauthFailoverServedKey);
1018
+ if (node)
1019
+ return this._predictNodeAccountFallback(message, node);
1014
1020
  if (!getUsageLimitSignal(message) ||
1015
1021
  !this.settingsManager.getOAuthAccountsAutoSwitch(message.provider) ||
1016
1022
  !this._canReplayAccountFailure(message))
@@ -3574,6 +3580,9 @@ export class AgentSession {
3574
3580
  const signal = getQuotaExhaustionSignal(message);
3575
3581
  const servedKey = this._oauthFailoverServedKey;
3576
3582
  this._oauthFailoverServedKey = undefined;
3583
+ const node = this._nodeFallbackTrigger(message, servedKey);
3584
+ if (node)
3585
+ return this._attemptNodeAccountFallback(message, node);
3577
3586
  if (signal?.kind !== "usage_limit" || !this.settingsManager.getOAuthAccountsAutoSwitch(message.provider))
3578
3587
  return false;
3579
3588
  if (!this._canReplayAccountFailure(message)) {
@@ -3755,6 +3764,128 @@ export class AgentSession {
3755
3764
  this._modelRuntime.getProvider(providerId)?.auth.apiKey?.isSubscription === true
3756
3765
  : from.credential.type === "oauth" && to.credential.type === "api_key" && to.key === policy.apiKeyFallback));
3757
3766
  }
3767
+ /**
3768
+ * Opt-in HUMAIN Node key fallback applies only to a replayable, classified Node failure served by an identified
3769
+ * pool key. Anything else keeps the existing handling.
3770
+ */
3771
+ _nodeFallbackTrigger(message, servedKey) {
3772
+ if (message.provider !== HUMAIN_NODE_PROVIDER || servedKey === undefined)
3773
+ return undefined;
3774
+ const failure = getNodeAccountFailure(message.diagnostics);
3775
+ if (!failure || !this._canReplayAccountFailure(message))
3776
+ return undefined;
3777
+ let policy;
3778
+ try {
3779
+ policy = this._modelRuntime.nodeAccountMetadata.read().policy;
3780
+ }
3781
+ catch {
3782
+ // Lock contention or I/O errors must not break agent_end handling: no switch.
3783
+ return undefined;
3784
+ }
3785
+ return policy.enabled ? { failure, policy, servedKey } : undefined;
3786
+ }
3787
+ _predictNodeAccountFallback(message, node) {
3788
+ if (this._oauthRecoveryLimitReached(message.provider))
3789
+ return false;
3790
+ const accounts = this._modelRuntime.oauthAccounts.getCachedPoolAccounts(message.provider);
3791
+ const served = accounts?.find((account) => account.key === node.servedKey);
3792
+ if (!accounts || !served)
3793
+ return false;
3794
+ const now = Date.now();
3795
+ const ambient = process.env[HUMAIN_NODE_BASE_URL_ENV];
3796
+ return accounts.some((account) => !this._switchedOAuthAccountKeys.has(account.key) &&
3797
+ isOAuthAccountUsable(account, now) &&
3798
+ isNodeFallbackTarget(node.policy, served, account, ambient));
3799
+ }
3800
+ /**
3801
+ * Retry the same request, same provider and model, on the next usable configured Node key. Each key is tried at
3802
+ * most once per prompt (the shared tried set), and the pool applies Retry-After through `exhaustedUntil`.
3803
+ * Without a usable key the original failure continues through normal error handling.
3804
+ */
3805
+ async _attemptNodeAccountFallback(message, node) {
3806
+ const providerId = message.provider;
3807
+ const pool = this._modelRuntime.oauthAccounts;
3808
+ const runSignal = this._agentRunAbortController?.signal;
3809
+ const tried = this._switchedOAuthAccountKeys;
3810
+ const ambient = process.env[HUMAIN_NODE_BASE_URL_ENV];
3811
+ // Reads can fail on lock contention (e.g. another process saving setup while the pool lock is held) or I/O errors.
3812
+ // That is transient, not a pool failure: treat it as "no switch" without a switch-failed message.
3813
+ let policyUnavailable = false;
3814
+ const livePolicy = () => {
3815
+ try {
3816
+ return this._modelRuntime.nodeAccountMetadata.read().policy;
3817
+ }
3818
+ catch {
3819
+ policyUnavailable = true;
3820
+ return undefined;
3821
+ }
3822
+ };
3823
+ const targetAllowed = (source, target) => {
3824
+ const policy = livePolicy();
3825
+ return policy !== undefined && isNodeFallbackTarget(policy, source, target, ambient);
3826
+ };
3827
+ const from = pool.getCachedPoolAccounts(providerId)?.find((account) => account.key === node.servedKey);
3828
+ if (!from || this._oauthRecoveryLimitReached(providerId))
3829
+ return false;
3830
+ tried.add(node.servedKey);
3831
+ try {
3832
+ const result = await pool.failoverAccounts(providerId, {
3833
+ expectedActiveKey: node.servedKey,
3834
+ exhaustedUntil: nodeFailureExhaustedUntil(node.failure, Date.now()),
3835
+ skipKeys: tried,
3836
+ signal: runSignal,
3837
+ // Explicit Node policy only; Node stays a non-subscription provider everywhere else.
3838
+ allowApiKeyRotation: true,
3839
+ trackPreferred: false,
3840
+ candidateKeys: node.policy.accountKeys,
3841
+ candidateFilter: (source, target) => isNodeFallbackTarget(node.policy, source, target, ambient),
3842
+ // Staying on the failed key is not an activation; any other target must still be authorized live.
3843
+ canActivate: (source, target) => source.key === target.key || targetAllowed(source, target),
3844
+ });
3845
+ if (this._agentRunAbortRequested || runSignal?.aborted)
3846
+ return false;
3847
+ if (policyUnavailable)
3848
+ return false;
3849
+ if (!result)
3850
+ throw new Error("Account recovery unavailable");
3851
+ const to = result.switched ? result.to : result.reason === "active_changed" ? result.active : undefined;
3852
+ const switchAllowed = to !== undefined && !tried.has(to.key) && targetAllowed(from, to);
3853
+ if (policyUnavailable)
3854
+ return false;
3855
+ if (to && switchAllowed) {
3856
+ // The Node catalog is scoped to the selected key; a local refresh here would discard it, so none runs.
3857
+ this._emit({
3858
+ type: "oauth_account_switched",
3859
+ provider: providerId,
3860
+ fromAccountKey: node.servedKey,
3861
+ toAccountKey: to.key,
3862
+ fromLabel: from.label,
3863
+ toLabel: to.label,
3864
+ reason: node.failure.reason,
3865
+ notice: formatNodeSwitchNotice(from.label, node.failure.reason, to.label),
3866
+ });
3867
+ if (this._agentRunAbortRequested || runSignal?.aborted)
3868
+ return false;
3869
+ this._omitRecoveryAttempt(message);
3870
+ return true;
3871
+ }
3872
+ this._emit({
3873
+ type: "oauth_accounts_exhausted",
3874
+ provider: providerId,
3875
+ accountKey: node.servedKey,
3876
+ label: from.label,
3877
+ total: result.total,
3878
+ earliestResetAt: result.switched ? undefined : result.earliestResetAt,
3879
+ notice: NODE_FALLBACK_NONE_MESSAGE,
3880
+ });
3881
+ }
3882
+ catch {
3883
+ if (this._agentRunAbortRequested || runSignal?.aborted || policyUnavailable)
3884
+ return false;
3885
+ this._emitOAuthAccountSwitchFailed(providerId, node.servedKey);
3886
+ }
3887
+ return false;
3888
+ }
3758
3889
  /** Generic subscription-key recovery and explicitly consented OAuth-to-key fallback. */
3759
3890
  async _attemptGeneralAccountFailover(message, servedKey, markExhausted = true) {
3760
3891
  const providerId = message.provider;