@narumitw/pi-btw 0.60.2 → 0.60.3
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/dist/index.ts +31 -85
- package/dist/index.ts.map +3 -3
- package/package.json +1 -1
- package/src/btw.ts +26 -81
- package/src/side-thread.ts +40 -37
package/package.json
CHANGED
package/src/btw.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
clampThinkingLevel,
|
|
4
|
-
getSupportedThinkingLevels,
|
|
5
|
-
type Model,
|
|
6
|
-
type ProviderHeaders,
|
|
7
|
-
} from "@earendil-works/pi-ai";
|
|
8
|
-
import { BorderedLoader, type ExtensionAPI, type ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
1
|
+
import { type Api, clampThinkingLevel, getSupportedThinkingLevels, type Model } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
9
3
|
import type { MenuContext, RunMenuResult } from "@narumitw/pi-tui-kit";
|
|
10
4
|
import {
|
|
11
5
|
type BtwBringToMainSegment,
|
|
@@ -76,12 +70,15 @@ interface LoadBtwThinkingLevelOptions {
|
|
|
76
70
|
warn?: (message: string) => void;
|
|
77
71
|
}
|
|
78
72
|
|
|
79
|
-
type BtwModelRegistry = Pick<ExtensionCommandContext["modelRegistry"], "find" | "
|
|
73
|
+
type BtwModelRegistry = Pick<ExtensionCommandContext["modelRegistry"], "find" | "getAvailable">;
|
|
80
74
|
|
|
81
75
|
type BtwCompletionRegistry = Pick<ExtensionCommandContext["modelRegistry"], "streamSimple">;
|
|
82
76
|
|
|
83
77
|
export function createModelRegistryCompleteSimple(modelRegistry: BtwCompletionRegistry): CompleteSimpleFunction {
|
|
84
|
-
|
|
78
|
+
const completeSimple: CompleteSimpleFunction = async (model, context, options) =>
|
|
79
|
+
modelRegistry.streamSimple(model, context, options).result();
|
|
80
|
+
completeSimple.appliesRequestHeaderTransforms = true;
|
|
81
|
+
return completeSimple;
|
|
85
82
|
}
|
|
86
83
|
|
|
87
84
|
interface ResolveBtwModelOptions {
|
|
@@ -93,7 +90,8 @@ interface ResolveBtwModelOptions {
|
|
|
93
90
|
|
|
94
91
|
export interface ResolvedBtwModel {
|
|
95
92
|
model: Model<Api>;
|
|
96
|
-
|
|
93
|
+
/** @deprecated Pi resolves request authentication through modelRegistry.streamSimple(). */
|
|
94
|
+
auth?: SideQuestionAuth;
|
|
97
95
|
}
|
|
98
96
|
|
|
99
97
|
export interface BtwThreadState {
|
|
@@ -112,6 +110,10 @@ export async function resolveBtwModel({
|
|
|
112
110
|
warn,
|
|
113
111
|
}: ResolveBtwModelOptions): Promise<ResolvedBtwModel | undefined> {
|
|
114
112
|
const reportWarning = (message: string) => warn?.(sanitizeSingleLine(message));
|
|
113
|
+
const availableModels = modelRegistry.getAvailable();
|
|
114
|
+
const isAvailable = (model: Model<Api>): boolean =>
|
|
115
|
+
availableModels.some((candidate) => candidate.provider === model.provider && candidate.id === model.id);
|
|
116
|
+
|
|
115
117
|
if (settings.model) {
|
|
116
118
|
const fallback = currentModel ? `${currentModel.provider}/${currentModel.id}` : "the current model";
|
|
117
119
|
const reference = parseBtwModelReference(settings.model);
|
|
@@ -122,52 +124,19 @@ export async function resolveBtwModel({
|
|
|
122
124
|
const configuredModel = modelRegistry.find(reference.provider, reference.modelId);
|
|
123
125
|
if (!configuredModel) {
|
|
124
126
|
reportWarning(`pi-btw model ${settings.model} was not found; falling back to ${fallback}.`);
|
|
127
|
+
} else if (isAvailable(configuredModel)) {
|
|
128
|
+
return { model: configuredModel };
|
|
125
129
|
} else {
|
|
126
130
|
const sameAsCurrent =
|
|
127
131
|
configuredModel === currentModel ||
|
|
128
132
|
(configuredModel.provider === currentModel?.provider && configuredModel.id === currentModel.id);
|
|
129
133
|
const fallbackAction = sameAsCurrent ? "no distinct current model is available" : `falling back to ${fallback}`;
|
|
130
|
-
|
|
131
|
-
const auth = await modelRegistry.getApiKeyAndHeaders(configuredModel);
|
|
132
|
-
if (auth.ok && hasRequestAuth(auth)) {
|
|
133
|
-
return {
|
|
134
|
-
model: auth.baseUrl ? { ...configuredModel, baseUrl: auth.baseUrl } : configuredModel,
|
|
135
|
-
auth,
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
const reason = auth.ok ? "has no request credentials" : auth.error;
|
|
139
|
-
reportWarning(`pi-btw model ${settings.model} is unavailable (${reason}); ${fallbackAction}.`);
|
|
140
|
-
} catch (error: unknown) {
|
|
141
|
-
reportWarning(`pi-btw model ${settings.model} credentials failed (${formatError(error)}); ${fallbackAction}.`);
|
|
142
|
-
}
|
|
134
|
+
reportWarning(`pi-btw model ${settings.model} is unavailable; ${fallbackAction}.`);
|
|
143
135
|
if (sameAsCurrent) return undefined;
|
|
144
136
|
}
|
|
145
137
|
}
|
|
146
138
|
|
|
147
|
-
|
|
148
|
-
try {
|
|
149
|
-
const auth = await modelRegistry.getApiKeyAndHeaders(currentModel);
|
|
150
|
-
if (auth.ok && hasRequestAuth(auth)) {
|
|
151
|
-
// Direct provider streams bypass Pi's request preparation, including OAuth routing.
|
|
152
|
-
return {
|
|
153
|
-
model: auth.baseUrl ? { ...currentModel, baseUrl: auth.baseUrl } : currentModel,
|
|
154
|
-
auth,
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
} catch {
|
|
158
|
-
// The caller reports the final lack of an available model.
|
|
159
|
-
}
|
|
160
|
-
return undefined;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function hasRequestAuth(auth: SideQuestionAuth): boolean {
|
|
164
|
-
return Boolean(
|
|
165
|
-
auth.apiKey || providerHeadersHaveValue(auth.headers) || (auth.env && Object.keys(auth.env).length > 0),
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function providerHeadersHaveValue(headers: ProviderHeaders | undefined): boolean {
|
|
170
|
-
return headers !== undefined && Object.values(headers).some((value) => value !== null);
|
|
139
|
+
return currentModel && isAvailable(currentModel) ? { model: currentModel } : undefined;
|
|
171
140
|
}
|
|
172
141
|
|
|
173
142
|
export async function loadBtwThinkingLevel(
|
|
@@ -222,7 +191,7 @@ export interface BtwExtensionDependencies {
|
|
|
222
191
|
) => Promise<BtwCommandMenuResult>;
|
|
223
192
|
pickMainEntry?: typeof pickMainEntry;
|
|
224
193
|
loadSettings?: typeof loadSettingsForCommand;
|
|
225
|
-
resolveModel?: typeof
|
|
194
|
+
resolveModel?: typeof resolveBtwModelForCommand;
|
|
226
195
|
runThread?: typeof runBtwThread;
|
|
227
196
|
runFullscreen?: RunBtwFullscreen;
|
|
228
197
|
}
|
|
@@ -231,7 +200,7 @@ export default function btw(pi: ExtensionAPI, dependencies: BtwExtensionDependen
|
|
|
231
200
|
const showCommandMenu = dependencies.showCommandMenu ?? showCommandMenuForBtw;
|
|
232
201
|
const pickEntry = dependencies.pickMainEntry ?? pickMainEntry;
|
|
233
202
|
const loadSettings = dependencies.loadSettings ?? loadSettingsForCommand;
|
|
234
|
-
const resolveModel = dependencies.resolveModel ??
|
|
203
|
+
const resolveModel = dependencies.resolveModel ?? resolveBtwModelForCommand;
|
|
235
204
|
const runThread = dependencies.runThread ?? runBtwThread;
|
|
236
205
|
const runFullscreen = dependencies.runFullscreen ?? runBtwFullscreen;
|
|
237
206
|
// Pi creates a fresh extension instance after session replacement or reload.
|
|
@@ -373,40 +342,17 @@ type ModelResolutionOutcome =
|
|
|
373
342
|
| { kind: "unavailable" }
|
|
374
343
|
| { kind: "selected"; selected: ResolvedBtwModel };
|
|
375
344
|
|
|
376
|
-
async function
|
|
345
|
+
async function resolveBtwModelForCommand(
|
|
377
346
|
settings: BtwSettings,
|
|
378
347
|
ctx: ExtensionCommandContext,
|
|
379
348
|
): Promise<ModelResolutionOutcome> {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
settled = true;
|
|
386
|
-
done({ kind: "cancelled" });
|
|
387
|
-
};
|
|
388
|
-
|
|
389
|
-
resolveBtwModel({
|
|
390
|
-
settings,
|
|
391
|
-
currentModel: ctx.model,
|
|
392
|
-
modelRegistry: ctx.modelRegistry,
|
|
393
|
-
warn: (message) => {
|
|
394
|
-
if (!settled) notifySafely(ctx, message, "warning");
|
|
395
|
-
},
|
|
396
|
-
})
|
|
397
|
-
.then((selected) => {
|
|
398
|
-
if (settled) return;
|
|
399
|
-
settled = true;
|
|
400
|
-
done(selected ? { kind: "selected", selected } : { kind: "unavailable" });
|
|
401
|
-
})
|
|
402
|
-
.catch(() => {
|
|
403
|
-
if (settled) return;
|
|
404
|
-
settled = true;
|
|
405
|
-
done({ kind: "unavailable" });
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
return loader;
|
|
349
|
+
const selected = await resolveBtwModel({
|
|
350
|
+
settings,
|
|
351
|
+
currentModel: ctx.model,
|
|
352
|
+
modelRegistry: ctx.modelRegistry,
|
|
353
|
+
warn: (message) => notifySafely(ctx, message, "warning"),
|
|
409
354
|
});
|
|
355
|
+
return selected ? { kind: "selected", selected } : { kind: "unavailable" };
|
|
410
356
|
}
|
|
411
357
|
|
|
412
358
|
interface RunBtwThreadDependencies {
|
|
@@ -859,7 +805,6 @@ async function askThreadQuestion(
|
|
|
859
805
|
question,
|
|
860
806
|
model: selected.model,
|
|
861
807
|
thinkingLevel,
|
|
862
|
-
auth: selected.auth,
|
|
863
808
|
signal: view.signal,
|
|
864
809
|
completeSimple: createModelRegistryCompleteSimple(ctx.modelRegistry),
|
|
865
810
|
sessionId: readBtwSessionId(ctx),
|
package/src/side-thread.ts
CHANGED
|
@@ -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
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
|
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(
|
|
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
|
|
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(
|
|
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-
|
|
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
|
-
//
|
|
202
|
-
//
|
|
203
|
-
|
|
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"
|
|
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"
|
|
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
|
-
|
|
243
|
+
applyRequestHeaderTransforms: boolean,
|
|
244
|
+
): ModelsSimpleStreamOptions {
|
|
245
245
|
const sessionHeaders = model ? getOpencodeSessionHeaders(model, sessionId) : undefined;
|
|
246
|
-
const options:
|
|
247
|
-
apiKey: auth
|
|
248
|
-
headers: mergeSessionHeaders(auth
|
|
249
|
-
env: auth
|
|
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
|
}
|