@juicesharp/rpiv-btw 2.6.0 → 2.6.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.
Files changed (3) hide show
  1. package/btw.ts +18 -8
  2. package/package.json +2 -1
  3. package/pi-compat.ts +28 -0
package/btw.ts CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  import { type CappedHistory, capHistory, type FitBranchResult, fitBranch } from "./btw-budget.js";
21
21
  import { assistantMessageText, type BtwTurn, userMessageText } from "./btw-messages.js";
22
22
  import { showBtwOverlay } from "./btw-ui.js";
23
- import { loadCompleteSimple, loadIsContextOverflow } from "./pi-compat.js";
23
+ import { getRuntimeCompleteSimple, loadCompleteSimple, loadIsContextOverflow } from "./pi-compat.js";
24
24
 
25
25
  // ---------------------------------------------------------------------------
26
26
  // Constants — flat named consts, grouped by concern (advisor pattern, b9428e9)
@@ -259,7 +259,12 @@ export async function executeBtw(
259
259
  if (!auth.ok) {
260
260
  return { kind: "error", error: errMisconfigured(modelLabel, auth.error) };
261
261
  }
262
- if (!auth.apiKey) {
262
+ // OAuth-backed providers resolve `{ ok: true }` with no literal apiKey — their
263
+ // credentials are applied inside Pi's runtime facade. A missing key is only
264
+ // fatal on legacy hosts without that facade, where the global completion
265
+ // fallback needs the key passed explicitly.
266
+ const runtimeCompleteSimple = getRuntimeCompleteSimple(ctx.modelRegistry);
267
+ if (!auth.apiKey && !runtimeCompleteSimple) {
263
268
  return { kind: "error", error: errNoApiKey(modelLabel) };
264
269
  }
265
270
 
@@ -274,7 +279,16 @@ export async function executeBtw(
274
279
  let built = buildBtwMessages(ctx, userMessage);
275
280
 
276
281
  try {
277
- const completeSimple = await loadCompleteSimple();
282
+ // Prefer Pi's auth-aware runtime facade (resolved once above, before the
283
+ // missing-key guard). Unlike the global compatibility function, it runs
284
+ // request preparation and applies credential-derived fields — OAuth
285
+ // tokens, GitHub Copilot's OAuth-specific baseUrl. Do not pass the
286
+ // preflight key/headers to that path: explicit overrides would bypass
287
+ // that resolution.
288
+ const completeSimple = runtimeCompleteSimple ?? (await loadCompleteSimple());
289
+ const requestOptions = runtimeCompleteSimple
290
+ ? { signal: controller.signal } // own AbortController, NOT ctx.signal (Decision 8)
291
+ : { apiKey: auth.apiKey, headers: auth.headers, signal: controller.signal };
278
292
  const overflowFn = await loadIsContextOverflow();
279
293
  let retried = false;
280
294
  const callCompleteSimple = async (
@@ -283,11 +297,7 @@ export async function executeBtw(
283
297
  const response = await completeSimple(
284
298
  model,
285
299
  { systemPrompt: built.systemPrompt, messages: built.messages, tools: [] },
286
- {
287
- apiKey: auth.apiKey,
288
- headers: auth.headers,
289
- signal: controller.signal, // own AbortController, NOT ctx.signal (Decision 8)
290
- },
300
+ requestOptions,
291
301
  );
292
302
  if (response.stopReason === "aborted") {
293
303
  return { kind: "aborted", stopReason: response.stopReason };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juicesharp/rpiv-btw",
3
- "version": "2.6.0",
3
+ "version": "2.6.2",
4
4
  "description": "Pi extension. The /btw slash command, for putting a one-off side question to the same primary model without polluting the main conversation.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -44,6 +44,7 @@
44
44
  "LICENSE"
45
45
  ],
46
46
  "pi": {
47
+ "image": "https://raw.githubusercontent.com/juicesharp/rpiv-mono/main/packages/rpiv-btw/docs/cover.png",
47
48
  "extensions": [
48
49
  "./index.ts"
49
50
  ]
package/pi-compat.ts CHANGED
@@ -19,6 +19,34 @@
19
19
  type CompleteSimpleFn = typeof import("@earendil-works/pi-ai/compat").completeSimple;
20
20
  type IsContextOverflowFn = typeof import("@earendil-works/pi-ai/compat").isContextOverflow;
21
21
 
22
+ /**
23
+ * Resolve Pi's auth-aware completion facade when the host exposes one.
24
+ *
25
+ * Current Pi hosts keep the canonical `ModelRuntime` behind the extension
26
+ * `ModelRegistry`'s runtime-private `runtime` slot. This structural bridge is
27
+ * intentionally isolated here: calling that facade is what lets Pi apply
28
+ * credential-derived request fields — OAuth tokens for keyless providers,
29
+ * GitHub Copilot's OAuth-specific `baseUrl`. Older hosts, and future hosts
30
+ * that move the slot, simply fall through to the legacy global completion
31
+ * path below. Mirrors rpiv-advisor's bridge — duplicated because siblings
32
+ * never import each other at runtime.
33
+ *
34
+ * The returned method is bound because `ModelRuntime.completeSimple()` calls
35
+ * other runtime methods through `this`.
36
+ */
37
+ export function getRuntimeCompleteSimple(modelRegistry: unknown): CompleteSimpleFn | undefined {
38
+ try {
39
+ if (modelRegistry === null || typeof modelRegistry !== "object") return undefined;
40
+ const runtime = (modelRegistry as { runtime?: unknown }).runtime;
41
+ if (runtime === null || typeof runtime !== "object") return undefined;
42
+ const completeSimple = (runtime as { completeSimple?: unknown }).completeSimple;
43
+ return typeof completeSimple === "function" ? (completeSimple.bind(runtime) as CompleteSimpleFn) : undefined;
44
+ } catch {
45
+ // A malformed/private host shape should retain the version-tolerant fallback.
46
+ return undefined;
47
+ }
48
+ }
49
+
22
50
  /**
23
51
  * Error codes meaning "the /compat entrypoint is not resolvable on this host":
24
52
  * - `ERR_PACKAGE_PATH_NOT_EXPORTED` — Node's ESM resolver when the installed