@remnic/plugin-openclaw 1.0.0 → 1.0.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.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  FallbackLlmClient
3
- } from "./chunk-TJZ7KBCC.js";
3
+ } from "./chunk-BIBYVWVY.js";
4
4
  import {
5
5
  listJsonFiles
6
6
  } from "./chunk-5LE4HTVL.js";
@@ -7,7 +7,7 @@ import {
7
7
  } from "./chunk-JGEKL3WH.js";
8
8
  import {
9
9
  FallbackLlmClient
10
- } from "./chunk-TJZ7KBCC.js";
10
+ } from "./chunk-BIBYVWVY.js";
11
11
  import {
12
12
  listJsonFiles,
13
13
  readJsonFile
@@ -139,6 +139,7 @@ function mergeEnv(overrides) {
139
139
  import path from "path";
140
140
  import os2 from "os";
141
141
  var _resolveApiKeyForProvider = null;
142
+ var _getRuntimeAuthForModel = null;
142
143
  var _resolverLoaded = false;
143
144
  var _resolverNextRetryAt = 0;
144
145
  var RESOLVER_RETRY_BACKOFF_MS = 6e4;
@@ -162,6 +163,10 @@ async function getGatewayResolver() {
162
163
  const mod = await import(importUrl);
163
164
  if (typeof mod.resolveApiKeyForProvider === "function") {
164
165
  _resolveApiKeyForProvider = mod.resolveApiKeyForProvider;
166
+ if (typeof mod.getRuntimeAuthForModel === "function") {
167
+ _getRuntimeAuthForModel = mod.getRuntimeAuthForModel;
168
+ log.debug("loaded gateway getRuntimeAuthForModel from runtime module");
169
+ }
165
170
  _resolverLoaded = true;
166
171
  log.debug("loaded gateway resolveApiKeyForProvider from runtime module");
167
172
  return _resolveApiKeyForProvider;
@@ -268,6 +273,39 @@ function resolveFromEnv(providerId) {
268
273
  }
269
274
  return void 0;
270
275
  }
276
+ async function getGatewayRuntimeAuthForModel() {
277
+ await getGatewayResolver();
278
+ return _getRuntimeAuthForModel;
279
+ }
280
+
281
+ // ../remnic-core/src/models-json.ts
282
+ import { readFileSync } from "fs";
283
+ import { join } from "path";
284
+ import { homedir } from "os";
285
+ var _cachedProviders = null;
286
+ var _loadAttempted = false;
287
+ function loadModelsJsonProviders() {
288
+ if (_loadAttempted) {
289
+ return _cachedProviders ?? {};
290
+ }
291
+ _loadAttempted = true;
292
+ try {
293
+ const modelsPath = join(homedir(), ".openclaw", "agents", "main", "agent", "models.json");
294
+ const raw = readFileSync(modelsPath, "utf-8");
295
+ const parsed = JSON.parse(raw);
296
+ const providers = parsed?.providers;
297
+ if (providers && typeof providers === "object" && !Array.isArray(providers)) {
298
+ _cachedProviders = providers;
299
+ log.debug(`loaded ${Object.keys(_cachedProviders).length} providers from models.json`);
300
+ return _cachedProviders;
301
+ }
302
+ } catch (err) {
303
+ log.debug(
304
+ `could not load models.json: ${err instanceof Error ? err.message : String(err)}`
305
+ );
306
+ }
307
+ return {};
308
+ }
271
309
 
272
310
  // ../remnic-core/src/fallback-llm.ts
273
311
  var FallbackLlmClient = class {
@@ -379,8 +417,7 @@ var FallbackLlmClient = class {
379
417
  */
380
418
  getModelChain(agentId) {
381
419
  const chain = [];
382
- const providers = this.gatewayConfig?.models?.providers;
383
- if (!providers) return chain;
420
+ const providers = this.gatewayConfig?.models?.providers ?? {};
384
421
  let modelConfig;
385
422
  if (agentId) {
386
423
  const persona = this.gatewayConfig?.agents?.list?.find(
@@ -428,7 +465,7 @@ var FallbackLlmClient = class {
428
465
  }
429
466
  const providerId = parts[0];
430
467
  const modelId = parts.slice(1).join("/");
431
- const providerConfig = providers[providerId];
468
+ const providerConfig = providers[providerId] ?? this.resolveFromModelsJson(providerId);
432
469
  if (!providerConfig) {
433
470
  log.warn(`fallback LLM: provider not found: ${providerId}`);
434
471
  return null;
@@ -436,36 +473,91 @@ var FallbackLlmClient = class {
436
473
  return { providerId, modelId, providerConfig, modelString };
437
474
  }
438
475
  /**
439
- * Resolve the API key for a provider, handling OpenClaw secret ref formats.
440
- * Results are cached per provider so exec calls only happen once.
476
+ * Look up a provider from the gateway's materialized models.json, which
477
+ * contains all providers including built-in ones (openai-codex, google-vertex,
478
+ * etc.) that aren't in the user's openclaw.json but are registered by
479
+ * gateway plugins. Returns null if the provider isn't found there either.
441
480
  */
442
- async resolveApiKey(providerId, providerConfig) {
443
- return resolveProviderApiKey(providerId, providerConfig.apiKey, this.gatewayConfig);
481
+ resolveFromModelsJson(providerId) {
482
+ const allProviders = loadModelsJsonProviders();
483
+ const config = allProviders[providerId];
484
+ if (config) {
485
+ log.debug(`fallback LLM: resolved provider "${providerId}" from models.json (api: ${config.api ?? "default"})`);
486
+ return config;
487
+ }
488
+ return null;
444
489
  }
445
490
  /**
446
491
  * Try to call a single model.
492
+ *
493
+ * Uses the gateway's native getRuntimeAuthForModel when available — this
494
+ * handles all provider-specific auth transforms (OAuth token exchange,
495
+ * base URL overrides for codex/copilot/etc.) through the same codepath
496
+ * the gateway itself uses. Falls back to resolveProviderApiKey for
497
+ * simpler providers or when the runtime module isn't loaded.
447
498
  */
448
499
  async tryModel(model, messages, options) {
500
+ const runtimeAuth = await this.resolveRuntimeAuth(model);
501
+ const effectiveBaseUrl = runtimeAuth?.baseUrl ?? model.providerConfig.baseUrl;
502
+ const resolvedApiKey = runtimeAuth?.apiKey ?? await this.resolveFallbackApiKey(model);
449
503
  const rawKey = model.providerConfig.apiKey;
450
504
  const needsResolution = rawKey === "secretref-managed" || typeof rawKey === "object" && rawKey !== null;
451
- const resolvedApiKey = await this.resolveApiKey(model.providerId, model.providerConfig);
452
505
  if (needsResolution && !resolvedApiKey) {
453
506
  throw new Error(`API key for provider "${model.providerId}" could not be resolved from secret ref`);
454
507
  }
455
- const configWithResolvedKey = resolvedApiKey ? { ...model.providerConfig, apiKey: resolvedApiKey } : model.providerConfig;
456
- switch (model.providerConfig.api) {
457
- case "anthropic-messages":
458
- return await this.callAnthropic(configWithResolvedKey, model.modelId, messages, options);
459
- case "openai-completions":
460
- default:
461
- return await this.callOpenAI(
462
- configWithResolvedKey,
463
- model.modelId,
464
- messages,
465
- options,
466
- shouldAssumeOpenAiChatCompletions(model.providerConfig.baseUrl)
508
+ const effectiveConfig = {
509
+ ...model.providerConfig,
510
+ baseUrl: effectiveBaseUrl,
511
+ ...resolvedApiKey ? { apiKey: resolvedApiKey } : {}
512
+ };
513
+ if (model.providerConfig.api === "anthropic-messages") {
514
+ return await this.callAnthropic(effectiveConfig, model.modelId, messages, options);
515
+ }
516
+ return await this.callOpenAI(
517
+ effectiveConfig,
518
+ model.modelId,
519
+ messages,
520
+ options,
521
+ shouldAssumeOpenAiChatCompletions(effectiveConfig.baseUrl)
522
+ );
523
+ }
524
+ /**
525
+ * Resolve request-ready auth through the gateway's native runtime, which
526
+ * handles provider-specific transforms (OAuth token exchange for codex/copilot,
527
+ * base URL rewrite, etc.). Returns null if the runtime isn't available.
528
+ */
529
+ async resolveRuntimeAuth(model) {
530
+ try {
531
+ const getRuntimeAuth = await getGatewayRuntimeAuthForModel();
532
+ if (!getRuntimeAuth) return null;
533
+ const result = await getRuntimeAuth({
534
+ model: {
535
+ provider: model.providerId,
536
+ id: model.modelId,
537
+ api: model.providerConfig.api,
538
+ baseUrl: model.providerConfig.baseUrl
539
+ },
540
+ cfg: this.gatewayConfig
541
+ });
542
+ if (result?.apiKey || result?.baseUrl) {
543
+ log.debug(
544
+ `fallback LLM: resolved runtime auth for "${model.modelString}" (source: ${result.source ?? "unknown"}, mode: ${result.mode ?? "unknown"})`
467
545
  );
546
+ return { apiKey: result.apiKey, baseUrl: result.baseUrl };
547
+ }
548
+ } catch (err) {
549
+ log.debug(
550
+ `fallback LLM: gateway runtime auth failed for "${model.modelString}": ${err instanceof Error ? err.message : String(err)}`
551
+ );
468
552
  }
553
+ return null;
554
+ }
555
+ /**
556
+ * Resolve API key through the existing provider-level resolution (env vars,
557
+ * secret refs, etc.). Used as fallback when gateway runtime auth isn't available.
558
+ */
559
+ async resolveFallbackApiKey(model) {
560
+ return resolveProviderApiKey(model.providerId, model.providerConfig.apiKey, this.gatewayConfig);
469
561
  }
470
562
  /**
471
563
  * Call OpenAI-compatible API.
@@ -22,7 +22,7 @@ import { mkdir, readFile, readdir, appendFile, writeFile, stat } from "fs/promis
22
22
  import path from "path";
23
23
  import os from "os";
24
24
 
25
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/external.js
25
+ // ../../node_modules/zod/v3/external.js
26
26
  var external_exports = {};
27
27
  __export(external_exports, {
28
28
  BRAND: () => BRAND,
@@ -134,7 +134,7 @@ __export(external_exports, {
134
134
  void: () => voidType
135
135
  });
136
136
 
137
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/helpers/util.js
137
+ // ../../node_modules/zod/v3/helpers/util.js
138
138
  var util;
139
139
  (function(util2) {
140
140
  util2.assertEqual = (_) => {
@@ -268,7 +268,7 @@ var getParsedType = (data) => {
268
268
  }
269
269
  };
270
270
 
271
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/ZodError.js
271
+ // ../../node_modules/zod/v3/ZodError.js
272
272
  var ZodIssueCode = util.arrayToEnum([
273
273
  "invalid_type",
274
274
  "invalid_literal",
@@ -386,7 +386,7 @@ ZodError.create = (issues) => {
386
386
  return error;
387
387
  };
388
388
 
389
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/locales/en.js
389
+ // ../../node_modules/zod/v3/locales/en.js
390
390
  var errorMap = (issue, _ctx) => {
391
391
  let message;
392
392
  switch (issue.code) {
@@ -489,7 +489,7 @@ var errorMap = (issue, _ctx) => {
489
489
  };
490
490
  var en_default = errorMap;
491
491
 
492
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/errors.js
492
+ // ../../node_modules/zod/v3/errors.js
493
493
  var overrideErrorMap = en_default;
494
494
  function setErrorMap(map) {
495
495
  overrideErrorMap = map;
@@ -498,7 +498,7 @@ function getErrorMap() {
498
498
  return overrideErrorMap;
499
499
  }
500
500
 
501
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/helpers/parseUtil.js
501
+ // ../../node_modules/zod/v3/helpers/parseUtil.js
502
502
  var makeIssue = (params) => {
503
503
  const { data, path: path3, errorMaps, issueData } = params;
504
504
  const fullPath = [...path3, ...issueData.path || []];
@@ -608,14 +608,14 @@ var isDirty = (x) => x.status === "dirty";
608
608
  var isValid = (x) => x.status === "valid";
609
609
  var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
610
610
 
611
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/helpers/errorUtil.js
611
+ // ../../node_modules/zod/v3/helpers/errorUtil.js
612
612
  var errorUtil;
613
613
  (function(errorUtil2) {
614
614
  errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
615
615
  errorUtil2.toString = (message) => typeof message === "string" ? message : message?.message;
616
616
  })(errorUtil || (errorUtil = {}));
617
617
 
618
- // ../../node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/types.js
618
+ // ../../node_modules/zod/v3/types.js
619
619
  var ParseInputLazyPath = class {
620
620
  constructor(parent, value, path3, key) {
621
621
  this._cachedPath = [];
@@ -4881,7 +4881,7 @@ var CompoundingEngine = class {
4881
4881
  let promotionCandidates = this.config.compoundingSemanticEnabled ? this.derivePromotionCandidates(outcomeSummary, mistakes.registry, rubrics) : [];
4882
4882
  if (this.config.cmcConsolidationEnabled) {
4883
4883
  try {
4884
- const { deriveCausalPromotionCandidates } = await import("./causal-consolidation-WINYJQJ4.js");
4884
+ const { deriveCausalPromotionCandidates } = await import("./causal-consolidation-YZLBOC7J.js");
4885
4885
  const causalCandidates = await deriveCausalPromotionCandidates({
4886
4886
  memoryDir: this.config.memoryDir,
4887
4887
  causalTrajectoryStoreDir: this.config.causalTrajectoryStoreDir,
@@ -4902,7 +4902,7 @@ var CompoundingEngine = class {
4902
4902
  }
4903
4903
  if (this.config.calibrationEnabled) {
4904
4904
  try {
4905
- const { runCalibrationConsolidation } = await import("./calibration-KQXCC77L.js");
4905
+ const { runCalibrationConsolidation } = await import("./calibration-WZXRJMVP.js");
4906
4906
  const calRules = await runCalibrationConsolidation({
4907
4907
  memoryDir: this.config.memoryDir,
4908
4908
  gatewayConfig: this.config.gatewayConfig,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  CompoundingEngine,
3
3
  defaultTierMigrationCycleBudget
4
- } from "./chunk-WLR4WL6B.js";
4
+ } from "./chunk-L46G4NGI.js";
5
5
  import "./chunk-Y7JG2Q3V.js";
6
6
  import "./chunk-DMGIUDBO.js";
7
7
  import "./chunk-MLKGABMK.js";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  FallbackLlmClient
3
- } from "./chunk-TJZ7KBCC.js";
3
+ } from "./chunk-BIBYVWVY.js";
4
4
  import "./chunk-DMGIUDBO.js";
5
5
  import "./chunk-MLKGABMK.js";
6
6
  export {