@humain/humain-code 0.4.8 → 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 (143) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/dist/bundle/agent-monitor.js +1 -1
  3. package/dist/bundle/chunks/{anthropic-messages-YMPB2HJ7.js → anthropic-messages-UNHXXOD4.js} +3 -3
  4. package/dist/bundle/chunks/{azure-openai-responses-PDHOT7UC.js → azure-openai-responses-4QRYJCC2.js} +2 -2
  5. package/dist/bundle/chunks/bedrock-converse-stream.js +109 -92
  6. package/dist/bundle/chunks/{chunk-GNOKILVU.js → chunk-2QTTNAD4.js} +3385 -3947
  7. package/dist/bundle/chunks/{chunk-II2442A2.js → chunk-5CDES42D.js} +1 -1
  8. package/dist/bundle/chunks/{chunk-IMR3ZJM5.js → chunk-5XD3DHYL.js} +1 -1
  9. package/dist/bundle/chunks/{chunk-RXHSJSYY.js → chunk-6KAMTNN3.js} +1 -1
  10. package/dist/bundle/chunks/chunk-76X5KOIA.js +28 -0
  11. package/dist/bundle/chunks/{chunk-KRMTBTWU.js → chunk-77EWF2NW.js} +1 -1
  12. package/dist/bundle/chunks/{chunk-SKIP6KJV.js → chunk-7Z5MWPYE.js} +1 -1
  13. package/dist/bundle/chunks/{chunk-PBBQBIHY.js → chunk-AS2Y2MO6.js} +37 -37
  14. package/dist/bundle/chunks/{chunk-TIAXGIMN.js → chunk-AUFEF24K.js} +10 -10
  15. package/dist/bundle/chunks/{chunk-ILRBME2Y.js → chunk-BCF7KZ3R.js} +757 -745
  16. package/dist/bundle/chunks/{chunk-WPLW452J.js → chunk-DZ5C66IU.js} +1 -1
  17. package/dist/bundle/chunks/{chunk-2W5PWUPO.js → chunk-EDKZRYWJ.js} +2 -2
  18. package/dist/bundle/chunks/{chunk-W2QRCQBX.js → chunk-FF26YENK.js} +3 -4
  19. package/dist/bundle/chunks/{chunk-UZ5FVVCP.js → chunk-HMWAH7ZY.js} +1 -1
  20. package/dist/bundle/chunks/{chunk-F7UMX5BV.js → chunk-HSDTSUZE.js} +13 -2
  21. package/dist/bundle/chunks/chunk-I2ZWZAIV.js +751 -0
  22. package/dist/bundle/chunks/{chunk-GUUAEK6G.js → chunk-LCOHFEZZ.js} +1 -1
  23. package/dist/bundle/chunks/chunk-MVYV6MJY.js +23 -0
  24. package/dist/bundle/chunks/{chunk-Z5LYSSTY.js → chunk-QSPDLT2R.js} +2 -2
  25. package/dist/bundle/chunks/{chunk-UBQSONVR.js → chunk-R5VIAWGA.js} +103 -145
  26. package/dist/bundle/chunks/{chunk-YURQPD6Y.js → chunk-RYC5BYVE.js} +1 -2
  27. package/dist/bundle/chunks/{chunk-ROSZII6E.js → chunk-V5ZF7Q7L.js} +1 -1
  28. package/dist/bundle/chunks/chunk-VEYTRNQC.js +5 -0
  29. package/dist/bundle/chunks/{chunk-LQ4S7CRR.js → chunk-VYIIPF57.js} +1 -1
  30. package/dist/bundle/chunks/chunk-WBJN6PEI.js +43 -0
  31. package/dist/bundle/chunks/{chunk-SWRU4BOC.js → chunk-XGO43DKL.js} +1 -1
  32. package/dist/bundle/chunks/{chunk-PMISFIBM.js → chunk-XMHLSV3I.js} +1 -1
  33. package/dist/bundle/chunks/{chunk-3E4SMILM.js → chunk-YJBE3IGR.js} +14 -14
  34. package/dist/bundle/chunks/{chunk-OTXURGIO.js → chunk-YKZGL4MB.js} +1 -1
  35. package/dist/bundle/chunks/chunk-Z33NNMNB.js +6 -0
  36. package/dist/bundle/chunks/{cli-ZKT34WPW.js → cli-5ACW3WND.js} +2 -2
  37. package/dist/bundle/chunks/{cloudflare-workers-ai-system-one-YPOLSCPT.js → cloudflare-workers-ai-system-one-FZ3UQCZ7.js} +1 -1
  38. package/dist/bundle/chunks/{compat-J5FTRJ6S.js → compat-TQKA4I7M.js} +3 -3
  39. package/dist/bundle/chunks/{dist-N7XDUAAO.js → dist-VNSMKBZ6.js} +3 -3
  40. package/dist/bundle/chunks/{dist-6FQCCUSC.js → dist-WHC67QRM.js} +1077 -912
  41. package/dist/bundle/chunks/dist-cjs-RL2OCAP2.js +179 -0
  42. package/dist/bundle/chunks/{easter-egg-3d-5KKDWEIA.js → easter-egg-3d-3L3IKAT6.js} +1 -1
  43. package/dist/bundle/chunks/{esm-NCUV2DF7.js → esm-763X2IA7.js} +1 -1
  44. package/dist/bundle/chunks/event-streams-R5OECII4.js +2 -0
  45. package/dist/bundle/chunks/{execute-OSBDNQU5.js → execute-4X6G65NZ.js} +1 -1
  46. package/dist/bundle/chunks/{extract-56PXVZVW.js → extract-DLZ425VI.js} +4 -4
  47. package/dist/bundle/chunks/github-copilot.js +32 -31
  48. package/dist/bundle/chunks/{google-generative-ai-TS4ZE2RO.js → google-generative-ai-JD2FVOIX.js} +2 -2
  49. package/dist/bundle/chunks/{google-vertex-MJK2SMNE.js → google-vertex-PFBMDCBW.js} +3 -3
  50. package/dist/bundle/chunks/host-4J7H7KDS.js +2 -0
  51. package/dist/bundle/chunks/{llama-cpp-classify-5KAR326J.js → llama-cpp-classify-AGCW7HYN.js} +1 -1
  52. package/dist/bundle/chunks/{mistral-conversations-XA2ZGSAH.js → mistral-conversations-D74NZ65O.js} +2 -2
  53. package/dist/bundle/chunks/{node-XGNY74WT.js → node-W47VUEGE.js} +1 -1
  54. package/dist/bundle/chunks/{openai-codex-responses-Y6NRWMOI.js → openai-codex-responses-JAAA7MSS.js} +3 -3
  55. package/dist/bundle/chunks/{openai-completions-FY77MIZH.js → openai-completions-PGPMGQPU.js} +34 -33
  56. package/dist/bundle/chunks/{openai-decisions-MKH4OJHH.js → openai-decisions-QQ4MEABI.js} +1 -1
  57. package/dist/bundle/chunks/openai-responses-FG2R2M4L.js +23 -0
  58. package/dist/bundle/chunks/{openrouter-images-R3QMMLPH.js → openrouter-images-NTBP3UUE.js} +1 -1
  59. package/dist/bundle/chunks/{pi-messages-LYJZPRST.js → pi-messages-JD4O7KIP.js} +1 -1
  60. package/dist/bundle/chunks/{runtime-JFLZUNYP.js → runtime-SI76JZNE.js} +1 -1
  61. package/dist/bundle/chunks/{typesafe-system-one-PAYJECQK.js → typesafe-system-one-TTC7YCO3.js} +1 -1
  62. package/dist/bundle/chunks/virtual-modules-ID2GA4GE.js +5 -0
  63. package/dist/bundle/cli-runtime.js +3 -3
  64. package/dist/bundle/flow/assets/contract.json +1 -1
  65. package/dist/bundle/flow/package.json +1 -1
  66. package/dist/bundle/index.js +17 -17
  67. package/dist/bundle/rpc-entry.js +3 -2
  68. package/dist/core/account-reporting.d.ts +109 -0
  69. package/dist/core/account-reporting.d.ts.map +1 -0
  70. package/dist/core/account-reporting.js +328 -0
  71. package/dist/core/account-reporting.js.map +1 -0
  72. package/dist/core/agent-session.d.ts +22 -2
  73. package/dist/core/agent-session.d.ts.map +1 -1
  74. package/dist/core/agent-session.js +131 -0
  75. package/dist/core/agent-session.js.map +1 -1
  76. package/dist/core/anthropic-account-reporting.d.ts +76 -0
  77. package/dist/core/anthropic-account-reporting.d.ts.map +1 -0
  78. package/dist/core/anthropic-account-reporting.js +380 -0
  79. package/dist/core/anthropic-account-reporting.js.map +1 -0
  80. package/dist/core/bedrock-account-reporting.d.ts +82 -0
  81. package/dist/core/bedrock-account-reporting.d.ts.map +1 -0
  82. package/dist/core/bedrock-account-reporting.js +402 -0
  83. package/dist/core/bedrock-account-reporting.js.map +1 -0
  84. package/dist/core/model-runtime.d.ts +17 -0
  85. package/dist/core/model-runtime.d.ts.map +1 -1
  86. package/dist/core/model-runtime.js +64 -10
  87. package/dist/core/model-runtime.js.map +1 -1
  88. package/dist/core/oauth-account-pool.d.ts +4 -0
  89. package/dist/core/oauth-account-pool.d.ts.map +1 -1
  90. package/dist/core/oauth-account-pool.js +10 -3
  91. package/dist/core/oauth-account-pool.js.map +1 -1
  92. package/dist/humain/bedrock-provider/vendor/index.bundle.mjs +353 -29
  93. package/dist/humain/flow-accounts-host.d.ts +120 -0
  94. package/dist/humain/flow-accounts-host.d.ts.map +1 -0
  95. package/dist/humain/flow-accounts-host.js +346 -0
  96. package/dist/humain/flow-accounts-host.js.map +1 -0
  97. package/dist/humain/flow-command.d.ts +28 -0
  98. package/dist/humain/flow-command.d.ts.map +1 -0
  99. package/dist/humain/flow-command.js +226 -0
  100. package/dist/humain/flow-command.js.map +1 -0
  101. package/dist/humain/flow-loader.d.ts +4 -2
  102. package/dist/humain/flow-loader.d.ts.map +1 -1
  103. package/dist/humain/flow-loader.js +120 -91
  104. package/dist/humain/flow-loader.js.map +1 -1
  105. package/dist/humain/node-account-failure.d.ts +18 -0
  106. package/dist/humain/node-account-failure.d.ts.map +1 -0
  107. package/dist/humain/node-account-failure.js +73 -0
  108. package/dist/humain/node-account-failure.js.map +1 -0
  109. package/dist/humain/node-account-fallback.d.ts +85 -0
  110. package/dist/humain/node-account-fallback.d.ts.map +1 -0
  111. package/dist/humain/node-account-fallback.js +175 -0
  112. package/dist/humain/node-account-fallback.js.map +1 -0
  113. package/dist/humain/node-http.d.ts.map +1 -1
  114. package/dist/humain/node-http.js +11 -0
  115. package/dist/humain/node-http.js.map +1 -1
  116. package/dist/humain/node-provider.d.ts +4 -0
  117. package/dist/humain/node-provider.d.ts.map +1 -1
  118. package/dist/humain/node-provider.js +11 -0
  119. package/dist/humain/node-provider.js.map +1 -1
  120. package/dist/index.d.ts +4 -0
  121. package/dist/index.d.ts.map +1 -1
  122. package/dist/index.js.map +1 -1
  123. package/dist/modes/interactive/components/login-dialog.d.ts +2 -0
  124. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
  125. package/dist/modes/interactive/components/login-dialog.js +5 -0
  126. package/dist/modes/interactive/components/login-dialog.js.map +1 -1
  127. package/dist/modes/interactive/interactive-mode.d.ts +26 -0
  128. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  129. package/dist/modes/interactive/interactive-mode.js +242 -19
  130. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  131. package/docs/flow-profiles.md +57 -5
  132. package/docs/flow.md +130 -45
  133. package/docs/providers.md +37 -0
  134. package/npm-shrinkwrap.json +48 -6
  135. package/package.json +3 -2
  136. package/dist/bundle/chunks/chunk-AKA7HNO3.js +0 -4
  137. package/dist/bundle/chunks/chunk-JYWVRJYG.js +0 -6
  138. package/dist/bundle/chunks/chunk-MEIYGK5O.js +0 -6
  139. package/dist/bundle/chunks/chunk-PYU5BR3Z.js +0 -27
  140. package/dist/bundle/chunks/event-streams-CGCQQL5M.js +0 -2
  141. package/dist/bundle/chunks/host-2ARI7PQ5.js +0 -2
  142. package/dist/bundle/chunks/openai-responses-LNSCERKH.js +0 -23
  143. package/dist/bundle/chunks/virtual-modules-TT2VTOSY.js +0 -5
@@ -5,7 +5,9 @@ import { findEnvKeys } from "@earendil-works/pi-ai/compat";
5
5
  import * as builtinProviderCatalog from "@earendil-works/pi-ai/providers/all";
6
6
  import { assertChatModel, assertClassifierInputSupported, assertClassifierModel, assertImageModel, classifierErrorResult, imageErrorResult, } from "@earendil-works/pi-ai/utils/model-operations";
7
7
  import { getAgentDir } from "../config.js";
8
+ import { FileNodeAccountMetadataStore, InMemoryNodeAccountMetadataStore, } from "../humain/node-account-fallback.js";
8
9
  import { operationSignal, raceWithAbortSignal } from "../utils/abort.js";
10
+ import { ANTHROPIC_PROVIDER_ID, AnthropicRateLimitStore, isOfficialAnthropicBaseUrl, parseAnthropicRateLimits, } from "./anthropic-account-reporting.js";
9
11
  import { AccountSourceReplacementError, AuthStorage as DefaultAuthStorage } from "./auth-storage.js";
10
12
  import { filterBedrockCatalog } from "./model-catalog-filter.js";
11
13
  import { ModelConfig } from "./model-config.js";
@@ -48,6 +50,17 @@ function mergeHeaders(base, override) {
48
50
  }
49
51
  return merged;
50
52
  }
53
+ const CREDENTIAL_HEADERS = ["x-api-key", "authorization"];
54
+ function headerValue(headers, lowerName) {
55
+ for (const [name, value] of Object.entries(headers ?? {}))
56
+ if (name.toLowerCase() === lowerName)
57
+ return value;
58
+ return undefined;
59
+ }
60
+ /** True when the final request headers replace or add a credential header the resolved auth did not supply. */
61
+ function overridesCredentialHeaders(resolved, final) {
62
+ return CREDENTIAL_HEADERS.some((name) => headerValue(final, name) !== headerValue(resolved, name));
63
+ }
51
64
  function swallow(fn) {
52
65
  try {
53
66
  const result = fn();
@@ -140,6 +153,10 @@ export class ModelRuntime {
140
153
  models;
141
154
  credentials;
142
155
  oauthAccountPool;
156
+ /** Secret-free HUMAIN Node fallback policy and user labels; keys stay in the credential pool. */
157
+ nodeAccountMetadata;
158
+ /** Latest Anthropic API rate-limit headers per account, observed from this runtime's own responses. */
159
+ anthropicRateLimits = new AnthropicRateLimitStore();
143
160
  defaultBuiltins;
144
161
  builtins = new Map();
145
162
  nativeExtensionProviders = new Map();
@@ -166,9 +183,10 @@ export class ModelRuntime {
166
183
  requestScope = new AsyncLocalStorage();
167
184
  /** Observers of sessions that own this runtime; they attribute direct calls made outside any request scope. */
168
185
  ownerObservers = new Set();
169
- constructor(credentials, oauthAccountPool, config, modelsPath, modelsStore, providers, modelNetworkEnabled) {
186
+ constructor(credentials, oauthAccountPool, nodeAccountMetadata, config, modelsPath, modelsStore, providers, modelNetworkEnabled) {
170
187
  this.credentials = credentials;
171
188
  this.oauthAccountPool = oauthAccountPool;
189
+ this.nodeAccountMetadata = nodeAccountMetadata;
172
190
  this.config = config;
173
191
  this.modelsPath = modelsPath;
174
192
  this.modelNetworkEnabled = modelNetworkEnabled;
@@ -184,6 +202,10 @@ export class ModelRuntime {
184
202
  (options.credentials
185
203
  ? OAuthAccountPool.inMemory(credentials.accountStore)
186
204
  : OAuthAccountPool.create(credentials.accountStore, join(dirname(options.authPath ?? join(getAgentDir(), "auth.json")), "oauth-accounts.json")));
205
+ const nodeAccountMetadata = options.nodeAccountMetadata ??
206
+ (options.credentials
207
+ ? new InMemoryNodeAccountMetadataStore()
208
+ : new FileNodeAccountMetadataStore(join(dirname(options.authPath ?? join(getAgentDir(), "auth.json")), "humain-node-accounts.json")));
187
209
  const modelsPath = options.modelsPath === null ? undefined : (options.modelsPath ?? join(getAgentDir(), "models.json"));
188
210
  const config = await ModelConfig.load(modelsPath);
189
211
  const modelsStore = options.modelsStore ??
@@ -202,7 +224,7 @@ export class ModelRuntime {
202
224
  const getModels = provider.getModels.bind(provider);
203
225
  return { ...provider, getModels: () => filterBedrockCatalog(getModels()) };
204
226
  });
205
- const runtime = new ModelRuntime(credentials, oauthAccountPool, config, modelsPath, modelsStore, providers, process.env.PI_OFFLINE === undefined);
227
+ const runtime = new ModelRuntime(credentials, oauthAccountPool, nodeAccountMetadata, config, modelsPath, modelsStore, providers, process.env.PI_OFFLINE === undefined);
206
228
  runtime.configureRadiusProviders();
207
229
  runtime.rebuildProviders();
208
230
  const refreshFromNetwork = runtime.modelNetworkEnabled && options.allowModelNetwork === true;
@@ -638,6 +660,13 @@ export class ModelRuntime {
638
660
  await this.synchronizeCredentialState(providerId, "removeRuntimeApiKey", signal);
639
661
  });
640
662
  }
663
+ /**
664
+ * Raw stored credential for host-owned reporting: no command or environment resolution, so reading it runs nothing.
665
+ * Sensitive; callers must never display, log or persist it.
666
+ */
667
+ readStoredCredential(providerId, options) {
668
+ return this.protectCredentialOperation(this.credentials.readStored(providerId, options), options?.signal, false);
669
+ }
641
670
  listCredentials(options) {
642
671
  return this.protectCredentialOperation(this.credentials.list(options), options?.signal, false);
643
672
  }
@@ -842,11 +871,13 @@ export class ModelRuntime {
842
871
  if (!provider)
843
872
  throw new ModelsError("provider", `Unknown provider: ${model.provider}`);
844
873
  let resolvedAccount;
874
+ // Anthropic rate-limit headers are only recorded for a credential whose account is identified for this request.
875
+ const observesAnthropicLimits = model.provider === ANTHROPIC_PROVIDER_ID;
845
876
  const resolution = await this.getAuth(model, {
846
877
  apiKey: options?.apiKey,
847
878
  env: options?.env,
848
879
  signal: options?.signal,
849
- onOAuthAccount: options?.onOAuthAccount
880
+ onOAuthAccount: options?.onOAuthAccount || observesAnthropicLimits
850
881
  ? (provider, key) => {
851
882
  resolvedAccount = { provider, key };
852
883
  }
@@ -864,17 +895,29 @@ export class ModelRuntime {
864
895
  : undefined;
865
896
  const requestModel = resolution.auth.baseUrl ? { ...model, baseUrl: resolution.auth.baseUrl } : model;
866
897
  const apiKey = providerOptions.apiKey ?? resolution.auth.apiKey;
867
- if (options?.onOAuthAccount && this.oauthAccountPool.isPooled(model.provider)) {
898
+ let attributedAccountKey;
899
+ if ((options?.onOAuthAccount || observesAnthropicLimits) && this.oauthAccountPool.isPooled(model.provider)) {
868
900
  // OAuth-only providers still resolve stored auth even with an explicit request key. That
869
901
  // credential is not necessarily dispatched: preserve the key override and attribute it,
870
902
  // failing closed if its account is unknown. A resolved/refreshed key keeps its identity.
871
- const key = resolvedAccount?.provider === model.provider && apiKey === resolution.auth.apiKey
872
- ? resolvedAccount.key
873
- : apiKey
874
- ? this.oauthAccountPool.getAccountKeyForAccessToken(model.provider, apiKey)
875
- : undefined;
876
- options.onOAuthAccount(model.provider, key);
903
+ attributedAccountKey =
904
+ resolvedAccount?.provider === model.provider && apiKey === resolution.auth.apiKey
905
+ ? resolvedAccount.key
906
+ : apiKey
907
+ ? this.oauthAccountPool.getAccountKeyForAccessToken(model.provider, apiKey)
908
+ : undefined;
909
+ options?.onOAuthAccount?.(model.provider, attributedAccountKey);
877
910
  }
911
+ const callerOnResponse = providerOptions.onResponse;
912
+ const onResponse = observesAnthropicLimits &&
913
+ attributedAccountKey !== undefined &&
914
+ !overridesCredentialHeaders(resolution.auth.headers, headers) &&
915
+ isOfficialAnthropicBaseUrl(requestModel.baseUrl)
916
+ ? async (response, responseModel) => {
917
+ this.observeAnthropicLimits(attributedAccountKey, response);
918
+ await callerOnResponse?.(response, responseModel);
919
+ }
920
+ : callerOnResponse;
878
921
  // The one request boundary: every chat, image, classifier and deferred call is prepared here, so
879
922
  // the scope bound by the owning session observes it exactly once per real submission.
880
923
  const scope = this.currentScope();
@@ -893,11 +936,22 @@ export class ModelRuntime {
893
936
  headers,
894
937
  env,
895
938
  ...(onPayload ? { onPayload } : {}),
939
+ ...(onResponse ? { onResponse } : {}),
896
940
  ...(tracker ? { onRequest: tracker.hook(providerOptions.onRequest) } : {}),
897
941
  },
898
942
  tracker,
899
943
  };
900
944
  }
945
+ observeAnthropicLimits(accountKey, response) {
946
+ try {
947
+ const snapshot = parseAnthropicRateLimits(response.headers, Date.now());
948
+ if (snapshot)
949
+ this.anthropicRateLimits.record(accountKey, snapshot);
950
+ }
951
+ catch {
952
+ // Observation is best effort and never affects the request.
953
+ }
954
+ }
901
955
  /**
902
956
  * Run `fn` so every request it starts (including background work it schedules) is reported to
903
957
  * `observer`. The binding follows async context, so concurrent sessions sharing this runtime keep