@narumitw/pi-btw 0.60.2 → 0.61.0

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.
@@ -4,8 +4,8 @@ import type {
4
4
  Context,
5
5
  Message,
6
6
  Model,
7
+ ModelsSimpleStreamOptions,
7
8
  ProviderHeaders,
8
- SimpleStreamOptions,
9
9
  UserMessage,
10
10
  } from "@earendil-works/pi-ai";
11
11
 
@@ -19,11 +19,15 @@ export interface SideQuestionAuth {
19
19
  env?: Record<string, string>;
20
20
  }
21
21
 
22
- export type CompleteSimpleFunction = <TApi extends Api>(
23
- model: Model<TApi>,
24
- context: Context,
25
- options?: SimpleStreamOptions,
26
- ) => Promise<AssistantMessage>;
22
+ export interface CompleteSimpleFunction {
23
+ <TApi extends Api>(
24
+ model: Model<TApi>,
25
+ context: Context,
26
+ options?: ModelsSimpleStreamOptions,
27
+ ): Promise<AssistantMessage>;
28
+ /** True when the callback applies Models-only header transforms after request-time authentication. */
29
+ appliesRequestHeaderTransforms?: boolean;
30
+ }
27
31
 
28
32
  export type SideThreadTurn =
29
33
  | {
@@ -72,7 +76,7 @@ export interface CompleteSideThreadTurnOptions {
72
76
  model: Model<Api>;
73
77
  question: string;
74
78
  thinkingLevel: BtwThinkingLevel;
75
- auth: SideQuestionAuth;
79
+ auth?: SideQuestionAuth;
76
80
  signal?: AbortSignal;
77
81
  completeSimple: CompleteSimpleFunction;
78
82
  sessionId?: string;
@@ -98,7 +102,11 @@ export async function completeSideThreadTurn({
98
102
  const response = await completeSimple(
99
103
  model,
100
104
  { systemPrompt: SYSTEM_PROMPT, messages: buildSideThreadMessages(thread, question) },
101
- buildStreamOptions(auth, { thinkingLevel, signal, model, sessionId }),
105
+ buildStreamOptions(
106
+ auth,
107
+ { thinkingLevel, signal, model, sessionId },
108
+ completeSimple.appliesRequestHeaderTransforms === true,
109
+ ),
102
110
  );
103
111
  if (signal?.aborted || response?.stopReason === "aborted") return { kind: "aborted" };
104
112
  if (!isAssistantMessage(response)) {
@@ -125,7 +133,7 @@ export interface CompleteSideQuestionOptions {
125
133
  question: string;
126
134
  conversationContext: string;
127
135
  thinkingLevel: BtwThinkingLevel;
128
- auth: SideQuestionAuth;
136
+ auth?: SideQuestionAuth;
129
137
  signal?: AbortSignal;
130
138
  completeSimple: CompleteSimpleFunction;
131
139
  sessionId?: string;
@@ -147,7 +155,11 @@ export async function completeSideQuestion({
147
155
  systemPrompt: SYSTEM_PROMPT,
148
156
  messages: [createUserMessage(buildUserPrompt(question, conversationContext))],
149
157
  },
150
- buildStreamOptions(auth, { thinkingLevel, signal, model, sessionId }),
158
+ buildStreamOptions(
159
+ auth,
160
+ { thinkingLevel, signal, model, sessionId },
161
+ completeSimple.appliesRequestHeaderTransforms === true,
162
+ ),
151
163
  );
152
164
  }
153
165
 
@@ -194,31 +206,18 @@ function createUserMessage(text: string): UserMessage {
194
206
  };
195
207
  }
196
208
 
197
- // Minimal session-headers fork of Pi core provider-attribution
198
- // (pinned to @earendil-works/pi-coding-agent@0.85.0 src/core/provider-attribution.ts:getSessionHeaders).
209
+ // Minimal session-header mirror of Pi core provider attribution.
199
210
  // Core does not export this helper and extensions have no SettingsManager, so only session
200
211
  // headers are mirrored here. Default attribution headers are intentionally out of scope.
201
- // Keep semantics bug-compatible with core: case-sensitive Object.assign, explicit auth
202
- // headers win on exact-case match.
203
- const OPENCODE_HOST = "opencode.ai";
204
-
205
- function matchesOpencodeHost(baseUrl: string | undefined): boolean {
206
- if (!baseUrl) return false;
207
- try {
208
- return new URL(baseUrl).hostname === OPENCODE_HOST;
209
- } catch {
210
- return false;
211
- }
212
- }
213
-
212
+ // Request-time auth can replace a custom provider's base URL after these options are built,
213
+ // so only canonical provider IDs are safe attribution signals. Keep merge semantics
214
+ // bug-compatible with core: case-sensitive Object.assign, explicit request headers win on
215
+ // exact-case match.
214
216
  function getOpencodeSessionHeaders(
215
- model: Pick<Model<Api>, "provider" | "baseUrl">,
217
+ model: Pick<Model<Api>, "provider">,
216
218
  sessionId?: string,
217
219
  ): ProviderHeaders | undefined {
218
- if (!sessionId) return undefined;
219
- if (model.provider !== "opencode" && model.provider !== "opencode-go" && !matchesOpencodeHost(model.baseUrl)) {
220
- return undefined;
221
- }
220
+ if (!sessionId || (model.provider !== "opencode" && model.provider !== "opencode-go")) return undefined;
222
221
  return { "x-opencode-session": sessionId, "x-opencode-client": "pi" };
223
222
  }
224
223
 
@@ -234,21 +233,25 @@ function mergeSessionHeaders(
234
233
  interface BuildSideThreadStreamOptions {
235
234
  thinkingLevel: BtwThinkingLevel;
236
235
  signal?: AbortSignal;
237
- model?: Pick<Model<Api>, "provider" | "baseUrl">;
236
+ model?: Pick<Model<Api>, "provider">;
238
237
  sessionId?: string;
239
238
  }
240
239
 
241
240
  function buildStreamOptions(
242
- auth: SideQuestionAuth,
241
+ auth: SideQuestionAuth | undefined,
243
242
  { thinkingLevel, signal, model, sessionId }: BuildSideThreadStreamOptions,
244
- ): SimpleStreamOptions {
243
+ applyRequestHeaderTransforms: boolean,
244
+ ): ModelsSimpleStreamOptions {
245
245
  const sessionHeaders = model ? getOpencodeSessionHeaders(model, sessionId) : undefined;
246
- const options: SimpleStreamOptions = {
247
- apiKey: auth.apiKey,
248
- headers: mergeSessionHeaders(auth.headers, sessionHeaders),
249
- env: auth.env,
246
+ const options: ModelsSimpleStreamOptions = {
247
+ apiKey: auth?.apiKey,
248
+ headers: applyRequestHeaderTransforms ? auth?.headers : mergeSessionHeaders(auth?.headers, sessionHeaders),
249
+ env: auth?.env,
250
250
  signal,
251
251
  };
252
+ if (applyRequestHeaderTransforms && sessionHeaders) {
253
+ options.transformHeaders = (headers) => mergeSessionHeaders(headers, sessionHeaders) ?? {};
254
+ }
252
255
  if (thinkingLevel !== "off") options.reasoning = thinkingLevel;
253
256
  return options;
254
257
  }
@@ -23,10 +23,10 @@ import {
23
23
  VStack,
24
24
  visibleWidth,
25
25
  } from "@earendil-works/pi-tui";
26
- import type { BtwFullscreenLayoutComponent } from "./fullscreen-ui.js";
27
26
  import { BtwPasteGuard, type BtwShortcuts, getBtwShortcuts } from "./keybindings.js";
28
27
  import type { BtwThinkingLevel, SideThreadTurn } from "./side-thread.js";
29
28
  import { sanitizeSingleLine } from "./text.js";
29
+ import type { BtwFullscreenLayoutComponent } from "./workspace-layout.js";
30
30
 
31
31
  const TRANSCRIPT_CHROME_LINES = 2;
32
32
  const MAX_STEERING_DISPLAY_LINES = 3;
@@ -146,6 +146,10 @@ export class BtwTranscriptPager implements BtwFullscreenLayoutComponent, Focusab
146
146
  return this.layoutRoot;
147
147
  }
148
148
 
149
+ getPrimaryScrollView(): ScrollView {
150
+ return this.scrollView;
151
+ }
152
+
149
153
  render(width: number): string[] {
150
154
  if (width <= 0) return [];
151
155
  const safeWidth = Math.max(1, width);
@@ -397,6 +401,10 @@ export class BtwAnsweringView implements BtwFullscreenLayoutComponent, Focusable
397
401
  return this.layoutRoot;
398
402
  }
399
403
 
404
+ getPrimaryScrollView(): ScrollView {
405
+ return this.scrollView;
406
+ }
407
+
400
408
  render(width: number): string[] {
401
409
  if (width <= 0) return [];
402
410
  const safeWidth = Math.max(1, width);