@howaboua/pi-codex-web-run 0.0.6 → 0.0.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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # @howaboua/pi-codex-web-run
2
2
 
3
+ ## 0.0.7
4
+
5
+ - Explain when image generation and web search need a separate legacy OpenAI Codex login rather than ChatGPT sign-in. Access failures now give concise guidance without exposing backend responses or addresses. Existing provider routing is unchanged.
6
+
3
7
  ## 0.0.6
4
8
 
5
9
  - Requires Pi 1.0.0 or later.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@howaboua/pi-codex-web-run",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Codex web search and navigation for Pi, Code Mode and Notebook Mode.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -53,8 +53,8 @@
53
53
  },
54
54
  "devDependencies": {
55
55
  "@biomejs/biome": "^2.5.10",
56
- "@earendil-works/pi-coding-agent": "1.0.0",
57
- "@earendil-works/pi-tui": "1.0.0",
56
+ "@earendil-works/pi-coding-agent": "1.0.4",
57
+ "@earendil-works/pi-tui": "1.0.4",
58
58
  "@types/node": "^26.2.0",
59
59
  "tsx": "^4.23.12",
60
60
  "typebox": "1.3.18",
@@ -0,0 +1,47 @@
1
+ import { readStoredCredential } from "@earendil-works/pi-coding-agent";
2
+
3
+ /** Inspect stored metadata only after an authentication or route failure. */
4
+ export function codexLoginDiagnostic(rejected = false): string {
5
+ try {
6
+ const legacy = readStoredCredential("openai-codex");
7
+ const current = readStoredCredential("openai");
8
+ if (legacy) {
9
+ if (rejected || (legacy.type === "oauth" && legacy.expires <= Date.now()))
10
+ return "Legacy Codex login expired or was rejected. Explain to the user that they need to renew their separate OpenAI Codex login.";
11
+ return "This tool needs a Codex-compatible route. A separate OpenAI Codex login is stored, but unavailable for this request.";
12
+ }
13
+ if (current?.type === "oauth")
14
+ return "ChatGPT sign-in does not authorize this tool. Explain to the user that they need a separate legacy OpenAI Codex login.";
15
+ return "This tool needs a separate OpenAI Codex login. Ask the user to sign in.";
16
+ } catch {
17
+ return "Login status could not be read. Ask the user to check their OpenAI Codex login.";
18
+ }
19
+ }
20
+
21
+ /** Preserve the failure class without exposing backend details. */
22
+ export function codexProviderFailure(error: unknown, legacy = true): Error {
23
+ const message = error instanceof Error ? error.message : String(error);
24
+ if (/timeout|timed out/i.test(message))
25
+ return new Error("Provider request timed out. Try again later.");
26
+ if (/quota|rate.limit|\b429\b/i.test(message))
27
+ return new Error("Provider quota or rate limit reached. Try again later.");
28
+ if (/server|\b5\d\d\b/i.test(message))
29
+ return new Error("Provider service failed. Try again later.");
30
+ if (/network|fetch|connect|socket|dns/i.test(message))
31
+ return new Error("Provider connection failed. Try again later.");
32
+ if (
33
+ /auth|credential|login|token|api.?key|accountId|Codex-compatible/i.test(
34
+ message,
35
+ )
36
+ )
37
+ return new Error(
38
+ legacy
39
+ ? codexLoginDiagnostic(
40
+ /expired|rejected|invalid.grant|unauthorized|accountId/i.test(
41
+ message,
42
+ ),
43
+ )
44
+ : "Provider authentication unavailable. Ask the user to check their configured provider access.",
45
+ );
46
+ return new Error("Provider request failed.");
47
+ }
@@ -1,8 +1,11 @@
1
1
  import type { Model, ProviderHeaders } from "@earendil-works/pi-ai";
2
2
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
3
+ import {
4
+ codexLoginDiagnostic,
5
+ codexProviderFailure,
6
+ } from "./auth-diagnostics.js";
3
7
  import {
4
8
  type AllowConfiguredCodexToolProvider,
5
- CODEX_TOOL_PROVIDER_UNSUPPORTED_MESSAGE,
6
9
  type CodexToolProvider,
7
10
  OPENAI_CODEX_PROVIDER,
8
11
  } from "./types.js";
@@ -56,7 +59,7 @@ function headerValue(
56
59
  return undefined;
57
60
  }
58
61
 
59
- function extractAccountId(token: string): string {
62
+ function extractAccountId(token: string, legacy: boolean): string {
60
63
  try {
61
64
  const payload = token.split(".")[1];
62
65
  if (!payload) throw new Error("Invalid token");
@@ -72,7 +75,11 @@ function extractAccountId(token: string): string {
72
75
  throw new Error("No account ID in token");
73
76
  return accountId;
74
77
  } catch {
75
- throw new Error("Failed to extract accountId from token");
78
+ throw new Error(
79
+ legacy
80
+ ? codexLoginDiagnostic(true)
81
+ : "Account identity unavailable. Ask the user to check their provider access.",
82
+ );
76
83
  }
77
84
  }
78
85
 
@@ -112,10 +119,7 @@ export function resolveAuthModel(
112
119
  return ctx.model as Model<any>;
113
120
  const fallback = resolveOpenAICodexAuthModel(ctx);
114
121
  if (fallback) return fallback;
115
- throw new Error(
116
- CODEX_TOOL_PROVIDER_UNSUPPORTED_MESSAGE +
117
- "; run /login openai-codex or select an OpenAI Codex-compatible provider",
118
- );
122
+ throw new Error(codexLoginDiagnostic());
119
123
  }
120
124
 
121
125
  function resolveConfiguredResponsesUrl(
@@ -137,8 +141,17 @@ export async function resolveCodexToolProvider(
137
141
  allowConfiguredProvider,
138
142
  isConfiguredCodexTransport,
139
143
  );
140
- const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
141
- if (!auth.ok) throw new Error(auth.error);
144
+ const auth = await ctx.modelRegistry
145
+ .getApiKeyAndHeaders(model)
146
+ .catch((error) => {
147
+ throw codexProviderFailure(error, isOpenAICodexModel(model));
148
+ });
149
+ if (!auth.ok) {
150
+ throw codexProviderFailure(
151
+ new Error(auth.error),
152
+ isOpenAICodexModel(model),
153
+ );
154
+ }
142
155
  const resolvedBaseUrl = auth.baseUrl ?? model.baseUrl;
143
156
  const codexTransport = isCodexTransportModel(
144
157
  model,
@@ -150,7 +163,12 @@ export async function resolveCodexToolProvider(
150
163
  const token = codexTransport
151
164
  ? (auth.apiKey ?? authorization)
152
165
  : (authorization ?? auth.apiKey);
153
- if (!token) throw new Error(CODEX_TOOL_PROVIDER_UNSUPPORTED_MESSAGE);
166
+ if (!token)
167
+ throw new Error(
168
+ isOpenAICodexModel(model)
169
+ ? codexLoginDiagnostic()
170
+ : "Provider authentication unavailable. Ask the user to check their configured provider access.",
171
+ );
154
172
  const baseUrl = codexTransport
155
173
  ? resolveCodexApiProviderBaseUrl(resolvedBaseUrl)
156
174
  : resolvedBaseUrl?.trim().replace(/\/+$/, "");
@@ -160,6 +178,7 @@ export async function resolveCodexToolProvider(
160
178
  ? resolveCodexResponsesUrl(baseUrl)
161
179
  : resolveConfiguredResponsesUrl(baseUrl);
162
180
  return {
181
+ authProvider: model.provider,
163
182
  route: codexTransport ? "openai-codex" : "configured-responses",
164
183
  baseUrl,
165
184
  responsesUrl,
@@ -168,6 +187,8 @@ export async function resolveCodexToolProvider(
168
187
  token,
169
188
  accountId:
170
189
  headerValue(auth.headers, "chatgpt-account-id") ??
171
- (codexTransport ? extractAccountId(token) : ""),
190
+ (codexTransport
191
+ ? extractAccountId(token, isOpenAICodexModel(model))
192
+ : ""),
172
193
  };
173
194
  }
@@ -1,12 +1,12 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
 
3
- export const CODEX_TOOL_PROVIDER_UNSUPPORTED_MESSAGE =
4
- "Codex-backed tool requires an OpenAI Codex-compatible Responses provider or /login openai-codex";
5
3
  export const CODEX_TOOL_ORIGINATOR = "codex_cli_rs";
6
4
  export const OPENAI_CODEX_PROVIDER = "openai-codex";
7
5
  export const DEFAULT_CODEX_BASE_URL = "https://chatgpt.com/backend-api";
8
6
 
9
7
  export interface CodexToolProvider {
8
+ /** Provider whose credentials were selected, when resolved locally. */
9
+ authProvider?: string;
10
10
  route: "openai-codex" | "configured-responses";
11
11
  baseUrl: string;
12
12
  responsesUrl: string;
package/src/contract.ts CHANGED
@@ -3,8 +3,6 @@ import { Type } from "typebox";
3
3
  import type { CodexToolProvider } from "./codex-runtime/types.js";
4
4
 
5
5
  export const WEB_SEARCH_TOOL_NAME = "web_run";
6
- export const WEB_SEARCH_UNSUPPORTED_MESSAGE =
7
- "web_run requires an OpenAI Codex-compatible Responses provider or /login openai-codex";
8
6
  export const DEFAULT_WEB_SEARCH_MODEL = "gpt-6-luna";
9
7
  export const WEB_SEARCH_MAX_OUTPUT_TOKENS = 2_500;
10
8
  export const WEB_SEARCH_MAX_RESPONSE_BYTES = 8 * 1024 * 1024;
package/src/execute.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
3
+ import {
4
+ codexLoginDiagnostic,
5
+ codexProviderFailure,
6
+ } from "./codex-runtime/auth-diagnostics.js";
3
7
  import type { CodexToolRouteConfig } from "./codex-runtime/config.js";
4
8
  import {
5
9
  DEFAULT_WEB_SEARCH_MODEL,
@@ -58,7 +62,18 @@ async function resolveProvider(
58
62
  isConfiguredCodexTransport,
59
63
  );
60
64
  }
61
- const hosted = await options.resolveProvider?.(ctx);
65
+ let hosted;
66
+ try {
67
+ hosted = await options.resolveProvider?.(ctx);
68
+ } catch (error) {
69
+ const legacy =
70
+ ctx.model?.provider === "openai-codex" ||
71
+ !(
72
+ ctx.model?.api === "openai-codex-responses" ||
73
+ options.allowConfiguredProvider?.(ctx.model)
74
+ );
75
+ throw codexProviderFailure(error, legacy);
76
+ }
62
77
  if (hosted) return hosted;
63
78
  const { resolveCodexToolProvider } = await import(
64
79
  "./codex-runtime/resolve.js"
@@ -115,24 +130,22 @@ export async function executeCodexWebSearch(
115
130
  response.text.toLowerCase().includes("cloudflare"))
116
131
  )
117
132
  throw new Error(
118
- "web_run search failed for " +
119
- provider.searchUrl +
120
- ": HTTP 403 Cloudflare challenge",
133
+ "Web search was blocked by an access challenge. Try again later.",
121
134
  );
122
135
  if (response.status === 404 && response.text.includes('"Not Found"'))
136
+ throw new Error("Web search is unavailable on this route.");
137
+ if (
138
+ response.status === 401 ||
139
+ (response.status === 403 &&
140
+ /auth|token|credential|access_enforcement|oauth/i.test(response.text))
141
+ )
123
142
  throw new Error(
124
- "web_run search failed for " +
125
- provider.searchUrl +
126
- ": HTTP 404 Not Found (Codex endpoint unavailable for this account/backend)",
143
+ provider.route === "openai-codex" &&
144
+ provider.authProvider === "openai-codex"
145
+ ? codexLoginDiagnostic(true)
146
+ : "Web search access was rejected. Ask the user to check access for their configured provider.",
127
147
  );
128
- throw new Error(
129
- "web_run search failed for " +
130
- provider.searchUrl +
131
- ": HTTP " +
132
- response.status +
133
- " " +
134
- response.text,
135
- );
148
+ throw new Error("Web search failed: HTTP " + response.status);
136
149
  }
137
150
  let parsed: unknown;
138
151
  try {
package/src/tool.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import type { ToolDefinition } from "@earendil-works/pi-coding-agent";
3
3
  import { Container, Text } from "@earendil-works/pi-tui";
4
+ import { codexLoginDiagnostic } from "./codex-runtime/auth-diagnostics.js";
4
5
  import {
5
6
  WEB_SEARCH_PARAMETERS,
6
7
  WEB_SEARCH_TOOL_NAME,
7
- WEB_SEARCH_UNSUPPORTED_MESSAGE,
8
8
  type WebSearchToolOptions,
9
9
  } from "./contract.js";
10
10
  import {
@@ -62,7 +62,7 @@ export function createWebSearchTool(
62
62
  args && typeof args === "object" ? (args as Record<string, unknown>) : {},
63
63
  async execute(_toolCallId, params, signal, _onUpdate, ctx) {
64
64
  if (!supportsExecutableWebSearch(ctx.model, toolOptions))
65
- throw new Error(WEB_SEARCH_UNSUPPORTED_MESSAGE);
65
+ throw new Error(codexLoginDiagnostic());
66
66
  const output = await executeCodexWebSearch(
67
67
  params,
68
68
  ctx,