@standardagents/builder 0.27.6 → 0.27.7

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/index.js CHANGED
@@ -899,6 +899,42 @@ var init_ProviderRegistry = __esm({
899
899
  this.providerOverrideFn = fn;
900
900
  this.providerOverrideRevision += 1;
901
901
  }
902
+ /**
903
+ * The env a provider resolves against for a given execution, scoped to the
904
+ * THREAD rather than just the instance: thread env overrides win, then the
905
+ * thread OWNER's user env, then instance/Worker env. This is what makes
906
+ * per-user BYOK providers work — a credential a user installs in their own
907
+ * account env (e.g. an OAuth token a companion service rotates) is honored
908
+ * for that user's threads no matter which authorized actor plays the turn.
909
+ *
910
+ * Best-effort by design: a missing user id (super-admin/local threads) or
911
+ * an older AgentBuilder DO without user-env storage simply contributes
912
+ * nothing, leaving instance-env behavior untouched.
913
+ */
914
+ async buildThreadScopedEnv(state) {
915
+ const threadEnv = state.thread.metadata?.env ?? state.thread.metadata?.tenvs ?? null;
916
+ const userId = state.thread.metadata?.user_id ?? null;
917
+ let userEnv = null;
918
+ if (userId) {
919
+ try {
920
+ const agentBuilderId = state.env.AGENT_BUILDER.idFromName("singleton");
921
+ const agentBuilder = state.env.AGENT_BUILDER.get(agentBuilderId);
922
+ if (typeof agentBuilder.getUserEnvEntries === "function") {
923
+ const entries = await agentBuilder.getUserEnvEntries(userId);
924
+ if (Array.isArray(entries) && entries.length > 0) {
925
+ userEnv = {};
926
+ for (const entry of entries) {
927
+ if (entry?.name) userEnv[entry.name] = entry.value;
928
+ }
929
+ }
930
+ }
931
+ } catch (error) {
932
+ console.warn("buildThreadScopedEnv: user env unavailable:", error instanceof Error ? error.message : String(error));
933
+ }
934
+ }
935
+ if (!userEnv && !threadEnv) return state.env;
936
+ return { ...state.env, ...userEnv ?? {}, ...threadEnv ?? {} };
937
+ }
902
938
  /**
903
939
  * Get provider for a model.
904
940
  *
@@ -1810,7 +1846,8 @@ var init_LLMRequest = __esm({
1810
1846
  */
1811
1847
  static async callModel(modelId, context, state, logId) {
1812
1848
  const { ProviderRegistry: ProviderRegistry2 } = await Promise.resolve().then(() => (init_providers(), providers_exports));
1813
- const { provider, modelName, modelDef, viaPlatform, platformProxyOrigin } = await ProviderRegistry2.getProvider(modelId, state.env, state.thread.instance);
1849
+ const providerEnv2 = await ProviderRegistry2.buildThreadScopedEnv(state);
1850
+ const { provider, modelName, modelDef, viaPlatform, platformProxyOrigin } = await ProviderRegistry2.getProvider(modelId, providerEnv2, state.thread.instance);
1814
1851
  const streamAbort = new AbortController();
1815
1852
  const parentSignal = state.abortController?.signal;
1816
1853
  let onParentAbort;