@router-for-me/pi-cliproxyapi-provider 1.4.23 → 1.4.25

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.
@@ -19,6 +19,7 @@ import { homedir, tmpdir } from "node:os";
19
19
  import { dirname, join, resolve } from "node:path";
20
20
  import { fileURLToPath, pathToFileURL } from "node:url";
21
21
  import type { Api, AssistantMessageEventStream, Context, Model, SimpleStreamOptions } from "@earendil-works/pi-ai";
22
+ import { getPackageDir } from "@earendil-works/pi-coding-agent";
22
23
 
23
24
  export const CLIPROXYAPI_CODEX_API = "cliproxyapi-codex-responses" as const;
24
25
 
@@ -788,6 +789,19 @@ const CODEX_MODULE_RELATIVE = join(
788
789
  "openai-codex-responses.js",
789
790
  );
790
791
 
792
+ const CODEX_MODULE_FLAT_RELATIVE = join(
793
+ "node_modules",
794
+ "@earendil-works",
795
+ "pi-ai",
796
+ "dist",
797
+ "openai-codex-responses.js",
798
+ );
799
+
800
+ const CODEX_DIST_SUBPATHS = [
801
+ join("dist", "api", "openai-codex-responses.js"),
802
+ join("dist", "openai-codex-responses.js"),
803
+ ] as const;
804
+
791
805
  /**
792
806
  * Well-known filesystem locations for openai-codex-responses.js on bundled hosts.
793
807
  *
@@ -817,6 +831,26 @@ export function wellKnownCodexModuleCandidates(homeDirectory: string): string[]
817
831
  "pi-coding-agent",
818
832
  CODEX_MODULE_RELATIVE,
819
833
  ),
834
+ join(homeDirectory, ".pi", "runtime", CODEX_MODULE_RELATIVE),
835
+ join(homeDirectory, ".pi", "runtime", CODEX_MODULE_FLAT_RELATIVE),
836
+ join(
837
+ homeDirectory,
838
+ ".pi",
839
+ "runtime",
840
+ "node_modules",
841
+ "@earendil-works",
842
+ "pi-coding-agent",
843
+ CODEX_MODULE_RELATIVE,
844
+ ),
845
+ join(
846
+ homeDirectory,
847
+ ".pi",
848
+ "runtime",
849
+ "node_modules",
850
+ "@earendil-works",
851
+ "pi-coding-agent",
852
+ CODEX_MODULE_FLAT_RELATIVE,
853
+ ),
820
854
  join(homeDirectory, ".bun", "install", "global", CODEX_MODULE_RELATIVE),
821
855
  join(
822
856
  homeDirectory,
@@ -1047,16 +1081,33 @@ export function resolveCodexModuleFromNodeEntry(entryPath: string): string | und
1047
1081
  const real = realpathSync(current);
1048
1082
  const require = createRequire(pathToFileURL(real));
1049
1083
  for (const nodeModulesDir of require.resolve.paths("@earendil-works/pi-ai") ?? []) {
1050
- const candidate = join(
1051
- nodeModulesDir,
1052
- "@earendil-works",
1053
- "pi-ai",
1054
- "dist",
1055
- "api",
1056
- "openai-codex-responses.js",
1057
- );
1058
- if (existsSync(candidate)) {
1059
- return candidate;
1084
+ for (const distSubpath of CODEX_DIST_SUBPATHS) {
1085
+ const directCandidate = join(nodeModulesDir, "@earendil-works", "pi-ai", distSubpath);
1086
+ if (existsSync(directCandidate)) {
1087
+ return directCandidate;
1088
+ }
1089
+ const nestedCandidate = join(
1090
+ nodeModulesDir,
1091
+ "@earendil-works",
1092
+ "pi-coding-agent",
1093
+ "node_modules",
1094
+ "@earendil-works",
1095
+ "pi-ai",
1096
+ distSubpath,
1097
+ );
1098
+ if (existsSync(nestedCandidate)) {
1099
+ return nestedCandidate;
1100
+ }
1101
+ }
1102
+ }
1103
+
1104
+ // Managed Pi launcher (<agentDir>/bin/pi -> <agentDir>/install/releases/<version>)
1105
+ const managedInstallDir = join(dirname(dirname(real)), "install");
1106
+ const managedCurrentVersionPath = join(managedInstallDir, "current-version");
1107
+ if (existsSync(managedCurrentVersionPath)) {
1108
+ const currentVersion = readFileSync(managedCurrentVersionPath, "utf8").trim();
1109
+ if (currentVersion) {
1110
+ queue.push(join(managedInstallDir, "releases", currentVersion, "package.json"));
1060
1111
  }
1061
1112
  }
1062
1113
 
@@ -1071,12 +1122,32 @@ export function resolveCodexModuleFromNodeEntry(entryPath: string): string | und
1071
1122
  queue.push(resolve(dirname(real), ref));
1072
1123
  }
1073
1124
  }
1074
- const homeJoinMatch = content.match(
1075
- /join\s*\(\s*(?:homedir\(\)|process\.env\.HOME)[^)]*["']([^"']+)["']\s*\)/,
1076
- );
1077
- if (homeJoinMatch) {
1078
- const home = process.env.HOME || process.env.USERPROFILE || homedir();
1079
- queue.push(join(home, homeJoinMatch[1]));
1125
+ const defaultHome = process.env.HOME || process.env.USERPROFILE || homedir();
1126
+ for (const homeJoinMatch of content.matchAll(
1127
+ /join\s*\(\s*(homedir\(\)|process\.env\.HOME\b|process\.env\.USERPROFILE\b)\s*,([^)]*)\)/g,
1128
+ )) {
1129
+ const baseExpr = homeJoinMatch[1] ?? "";
1130
+ const baseHomes =
1131
+ baseExpr === "process.env.USERPROFILE"
1132
+ ? [process.env.USERPROFILE || defaultHome]
1133
+ : baseExpr === "process.env.HOME"
1134
+ ? [process.env.HOME || defaultHome]
1135
+ : Array.from(new Set([homedir(), defaultHome].filter(Boolean)));
1136
+ const argsText = homeJoinMatch[2] ?? "";
1137
+ const segments = [...argsText.matchAll(/["'`]([^"'`\r\n]+)["'`]/g)]
1138
+ .map((m) => m[1]?.trim())
1139
+ .filter((s): s is string => Boolean(s));
1140
+ if (segments.length > 0) {
1141
+ for (const baseHome of baseHomes) {
1142
+ queue.push(join(baseHome, ...segments));
1143
+ }
1144
+ }
1145
+ }
1146
+ for (const shellHomeMatch of content.matchAll(/["'`]?(?:~|\$HOME|\$\{HOME\})([\\/][^"'`\s\r\n]+)["'`]?/g)) {
1147
+ const rel = shellHomeMatch[1]?.trim();
1148
+ if (rel) {
1149
+ queue.push(join(defaultHome, rel.replace(/^[\\/]+/, "")));
1150
+ }
1080
1151
  }
1081
1152
  }
1082
1153
  } catch {
@@ -1086,6 +1157,38 @@ export function resolveCodexModuleFromNodeEntry(entryPath: string): string | und
1086
1157
  return undefined;
1087
1158
  }
1088
1159
 
1160
+ function resolveManagedInstallCandidates(homeDirectory: string): string[] {
1161
+ const installDirs = [join(homeDirectory, ".pi", "agent", "install")];
1162
+ const candidates: string[] = [];
1163
+ for (const installDir of installDirs) {
1164
+ try {
1165
+ const currentVersionPath = join(installDir, "current-version");
1166
+ if (!existsSync(currentVersionPath)) continue;
1167
+ const currentVersion = readFileSync(currentVersionPath, "utf8").trim();
1168
+ if (!currentVersion) continue;
1169
+ const releaseDir = join(installDir, "releases", currentVersion);
1170
+ candidates.push(
1171
+ join(releaseDir, CODEX_MODULE_RELATIVE),
1172
+ join(releaseDir, CODEX_MODULE_FLAT_RELATIVE),
1173
+ join(releaseDir, "node_modules", "@earendil-works", "pi-coding-agent", CODEX_MODULE_RELATIVE),
1174
+ join(releaseDir, "node_modules", "@earendil-works", "pi-coding-agent", CODEX_MODULE_FLAT_RELATIVE),
1175
+ );
1176
+ } catch {
1177
+ // Ignore unreadable managed install state.
1178
+ }
1179
+ }
1180
+ return candidates;
1181
+ }
1182
+
1183
+ function expandCodexCandidateVariants(candidate: string): string[] {
1184
+ const apiSuffix = join("dist", "api", "openai-codex-responses.js");
1185
+ if (candidate.endsWith(apiSuffix)) {
1186
+ const flatCandidate = `${candidate.slice(0, -apiSuffix.length)}${join("dist", "openai-codex-responses.js")}`;
1187
+ return [candidate, flatCandidate];
1188
+ }
1189
+ return [candidate];
1190
+ }
1191
+
1089
1192
  export function computePatchedModuleCachePath(
1090
1193
  originalPath: string,
1091
1194
  patchedSource: string,
@@ -1143,9 +1246,30 @@ export function resolveOriginalCodexModulePath(options: ResolveOriginalCodexModu
1143
1246
  }
1144
1247
  }
1145
1248
 
1146
- // Also check well-known CLI binaries if nodeEntry wasn't explicitly provided in options
1249
+ // Also check the active pi-coding-agent package root and well-known CLI binaries
1250
+ // if nodeEntry wasn't explicitly provided in options (e.g. inside subagent runners).
1251
+ if (!("nodeEntry" in options) && !("resolveSpecifier" in options)) {
1252
+ try {
1253
+ if (typeof getPackageDir === "function") {
1254
+ const packageDir = getPackageDir();
1255
+ if (packageDir) {
1256
+ const bundledHostModule = resolveCodexModuleFromNodeEntry(join(packageDir, "package.json"));
1257
+ if (bundledHostModule && existsSync(bundledHostModule)) {
1258
+ return { path: bundledHostModule, dir: dirname(bundledHostModule) };
1259
+ }
1260
+ }
1261
+ }
1262
+ } catch {
1263
+ // Ignore when host does not expose a resolvable package directory.
1264
+ }
1265
+ }
1266
+
1147
1267
  if (!("nodeEntry" in options) && homeDirectory) {
1148
1268
  const wellKnownEntries = [
1269
+ join(homeDirectory, ".pi", "bin", "pi"),
1270
+ join(homeDirectory, ".pi", "agent", "bin", "pi"),
1271
+ join(homeDirectory, ".pi", "runtime", "bin", "pi"),
1272
+ join(homeDirectory, ".pi", "runtime", "node_modules", ".bin", "pi"),
1149
1273
  join(homeDirectory, ".bun", "bin", "pi"),
1150
1274
  join(homeDirectory, ".local", "bin", "pi"),
1151
1275
  join(homeDirectory, ".npm-global", "bin", "pi"),
@@ -1172,19 +1296,45 @@ export function resolveOriginalCodexModulePath(options: ResolveOriginalCodexModu
1172
1296
  try {
1173
1297
  const main = fileURLToPath(resolveSpecifier("@earendil-works/pi-ai"));
1174
1298
  const distDir = dirname(main);
1175
- candidates.push(join(distDir, "api/openai-codex-responses.js"));
1176
- candidates.push(join(distDir, "openai-codex-responses.js"));
1299
+ const directCandidates = [
1300
+ join(distDir, "api/openai-codex-responses.js"),
1301
+ join(distDir, "openai-codex-responses.js"),
1302
+ ];
1303
+ candidates.push(...directCandidates);
1304
+ // Pi >=1.5 maps @earendil-works/pi-ai to pi-coding-agent's extension view
1305
+ // rather than pi-ai/dist. Resolve the physical pi-ai module from that host entry.
1306
+ if (!directCandidates.some((candidate) => existsSync(candidate))) {
1307
+ const hostModule = resolveCodexModuleFromNodeEntry(main);
1308
+ if (hostModule) {
1309
+ candidates.push(hostModule);
1310
+ }
1311
+ }
1177
1312
  } catch {
1178
1313
  // ignore
1179
1314
  }
1180
1315
 
1316
+ if (!candidates.some((candidate) => candidate && existsSync(candidate))) {
1317
+ try {
1318
+ const hostEntry = fileURLToPath(resolveSpecifier("@earendil-works/pi-coding-agent"));
1319
+ const hostModule = resolveCodexModuleFromNodeEntry(hostEntry);
1320
+ if (hostModule) {
1321
+ candidates.push(hostModule);
1322
+ }
1323
+ } catch {
1324
+ // ignore
1325
+ }
1326
+ }
1327
+
1181
1328
  if (homeDirectory) {
1329
+ candidates.push(...resolveManagedInstallCandidates(homeDirectory));
1182
1330
  candidates.push(...wellKnownCodexModuleCandidates(homeDirectory));
1183
1331
  }
1184
1332
 
1185
- for (const path of candidates) {
1186
- if (path && existsSync(path)) {
1187
- return { path, dir: dirname(path) };
1333
+ for (const candidate of candidates) {
1334
+ for (const path of expandCodexCandidateVariants(candidate)) {
1335
+ if (path && existsSync(path)) {
1336
+ return { path, dir: dirname(path) };
1337
+ }
1188
1338
  }
1189
1339
  }
1190
1340
 
@@ -1203,11 +1353,19 @@ export async function loadCliproxyCodexStreams(
1203
1353
  // older hosts keep the classic request-level effort instead of pinning it
1204
1354
  // to the baseline without carrying the real level.
1205
1355
  const sharedSource = (() => {
1206
- try {
1207
- return readFileSync(join(dirname(originalPath), "openai-responses-shared.js"), "utf8");
1208
- } catch {
1209
- return "";
1356
+ for (const candidate of [
1357
+ join(dirname(originalPath), "openai-responses-shared.js"),
1358
+ join(dirname(originalPath), "api", "openai-responses-shared.js"),
1359
+ ]) {
1360
+ try {
1361
+ if (existsSync(candidate)) {
1362
+ return readFileSync(candidate, "utf8");
1363
+ }
1364
+ } catch {
1365
+ // try next
1366
+ }
1210
1367
  }
1368
+ return "";
1211
1369
  })();
1212
1370
  const supportsConfigurationUpdate = sharedSupportsConfigurationUpdate(sharedSource);
1213
1371
  const patched = patchCodexSource(originalSource, providerIds, { supportsConfigurationUpdate });
@@ -796,9 +796,11 @@ export default async function (pi: ExtensionAPI): Promise<void> {
796
796
  const fastMode = new FastModeController(fastEnabled);
797
797
  const modelRefreshCoordinator = new ModelRefreshCoordinator();
798
798
 
799
+ let streamLoadError: string | undefined;
799
800
  let streamSimple: CliproxyCodexStreamSimple = () => {
801
+ const suffix = streamLoadError ? `: ${streamLoadError}` : "";
800
802
  throw new Error(
801
- `Codex protocol stream is unavailable for provider: ${identity.providerId} (api: ${CLIPROXYAPI_CODEX_API})`,
803
+ `Codex protocol stream is unavailable for provider: ${identity.providerId} (api: ${CLIPROXYAPI_CODEX_API})${suffix}`,
802
804
  );
803
805
  };
804
806
  try {
@@ -891,6 +893,7 @@ export default async function (pi: ExtensionAPI): Promise<void> {
891
893
  }
892
894
  } catch (error) {
893
895
  const message = error instanceof Error ? error.message : String(error);
896
+ streamLoadError = message;
894
897
  logWarn(`failed to load patched codex protocol: ${message}`);
895
898
  }
896
899
 
@@ -1049,9 +1052,9 @@ export default async function (pi: ExtensionAPI): Promise<void> {
1049
1052
  }
1050
1053
  }
1051
1054
  if (!options.forceRefresh) {
1052
- void registerConfiguredProvider(currentConnection, { forceRefresh: true }).catch((error) => {
1053
- const message = error instanceof Error ? error.message : String(error);
1054
- logWarn(`failed to refresh cached models (${message}); keeping the cached model list.`);
1055
+ void registerConfiguredProvider(currentConnection, { forceRefresh: true }).catch(() => {
1056
+ // Suppressed: logging this into the TUI corrupts the display.
1057
+ // The refresh path already reschedules the next attempt.
1055
1058
  });
1056
1059
  }
1057
1060
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@router-for-me/pi-cliproxyapi-provider",
3
- "version": "1.4.23",
3
+ "version": "1.4.25",
4
4
  "description": "Pi provider extension that auto-registers CLIProxyAPI models, plus TUI elapsed/TPS status",
5
5
  "type": "module",
6
6
  "keywords": [