@howaboua/pi-subagent-review 0.2.3 → 0.2.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@howaboua/pi-subagent-review",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Pi extension that adds /review via an isolated review subagent.",
5
5
  "type": "module",
6
6
  "main": "index.ts",
@@ -38,8 +38,8 @@
38
38
  ]
39
39
  },
40
40
  "peerDependencies": {
41
- "@earendil-works/pi-ai": "^0.79.0",
42
- "@earendil-works/pi-coding-agent": "^0.79.0"
41
+ "@earendil-works/pi-ai": "*",
42
+ "@earendil-works/pi-coding-agent": "*"
43
43
  },
44
44
  "scripts": {
45
45
  "check": "bun run lint && bun run typecheck",
@@ -49,8 +49,8 @@
49
49
  "format": "biome check --write ."
50
50
  },
51
51
  "devDependencies": {
52
- "@earendil-works/pi-ai": "^0.79.0",
53
- "@earendil-works/pi-coding-agent": "^0.79.0",
52
+ "@earendil-works/pi-ai": "0.80.1",
53
+ "@earendil-works/pi-coding-agent": "0.80.1",
54
54
  "@types/node": "^24.10.0",
55
55
  "typescript": "^5.9.3",
56
56
  "@biomejs/biome": "^2.4.16"
@@ -1,10 +1,122 @@
1
- import { completeSimple } from "@earendil-works/pi-ai";
1
+ import type {
2
+ Api,
3
+ AssistantMessage,
4
+ AssistantMessageEventStream,
5
+ Context,
6
+ Model,
7
+ SimpleStreamOptions,
8
+ } from "@earendil-works/pi-ai";
9
+ import * as piAiRuntime from "@earendil-works/pi-ai";
2
10
  import type {
3
11
  ExtensionCommandContext,
4
12
  SessionEntry,
5
13
  } from "@earendil-works/pi-coding-agent";
6
14
  import type { ResolvedReviewConfig } from "./types.js";
7
15
 
16
+ type RegisteredProviderConfig = {
17
+ api?: Api;
18
+ authHeader?: boolean;
19
+ streamSimple?: (
20
+ model: Model<Api>,
21
+ context: Context,
22
+ options?: SimpleStreamOptions,
23
+ ) => AssistantMessageEventStream;
24
+ };
25
+
26
+ type ModelRegistryInternals = {
27
+ providerRequestConfigs?: Map<
28
+ string,
29
+ Pick<RegisteredProviderConfig, "authHeader">
30
+ >;
31
+ registeredProviders?: Map<string, RegisteredProviderConfig>;
32
+ };
33
+
34
+ type PiAiRuntime = {
35
+ completeSimple(
36
+ model: Model<Api>,
37
+ context: Context,
38
+ options?: SimpleStreamOptions,
39
+ ): Promise<AssistantMessage>;
40
+ };
41
+
42
+ const { completeSimple: completeWithPiRuntime } =
43
+ piAiRuntime as unknown as PiAiRuntime;
44
+
45
+ function getModelRegistryInternals(
46
+ modelRegistry: ExtensionCommandContext["modelRegistry"],
47
+ ): ModelRegistryInternals {
48
+ return modelRegistry as unknown as ModelRegistryInternals;
49
+ }
50
+
51
+ function getRegisteredStreamSimple(
52
+ modelRegistry: ExtensionCommandContext["modelRegistry"],
53
+ model: Model<Api>,
54
+ ): RegisteredProviderConfig["streamSimple"] {
55
+ const providerConfig = getModelRegistryInternals(
56
+ modelRegistry,
57
+ ).registeredProviders?.get(model.provider);
58
+ if (providerConfig?.api !== model.api) return undefined;
59
+ return providerConfig.streamSimple;
60
+ }
61
+
62
+ function isMissingApiKeyError(error: string, provider: string): boolean {
63
+ return error === `No API key found for "${provider}"`;
64
+ }
65
+
66
+ async function getSummaryOptions(
67
+ ctx: ExtensionCommandContext,
68
+ model: Model<Api>,
69
+ config: ResolvedReviewConfig,
70
+ ): Promise<SimpleStreamOptions> {
71
+ const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
72
+ const fallbackApiKey =
73
+ auth.ok && auth.apiKey
74
+ ? undefined
75
+ : await ctx.modelRegistry.getApiKeyForProvider(model.provider);
76
+ if (!auth.ok && !isMissingApiKeyError(auth.error, model.provider)) {
77
+ throw new Error(`Summary model unavailable: ${auth.error}`);
78
+ }
79
+ if (!auth.ok && !fallbackApiKey) {
80
+ throw new Error(`Summary model unavailable: ${auth.error}`);
81
+ }
82
+
83
+ let headers = auth.ok ? auth.headers : undefined;
84
+ if (
85
+ fallbackApiKey &&
86
+ getModelRegistryInternals(ctx.modelRegistry).providerRequestConfigs?.get(
87
+ model.provider,
88
+ )?.authHeader
89
+ ) {
90
+ headers = { ...headers, Authorization: `Bearer ${fallbackApiKey}` };
91
+ }
92
+
93
+ const options: SimpleStreamOptions = {};
94
+ const apiKey = auth.ok && auth.apiKey ? auth.apiKey : fallbackApiKey;
95
+ const env = auth.ok
96
+ ? auth.env
97
+ : ctx.modelRegistry.authStorage.getProviderEnv(model.provider);
98
+ if (apiKey) options.apiKey = apiKey;
99
+ if (headers) options.headers = headers;
100
+ if (env) options.env = env;
101
+ if (model.reasoning && config.summary.thinking !== "off") {
102
+ options.reasoning = config.summary.thinking;
103
+ }
104
+ if (ctx.signal) options.signal = ctx.signal;
105
+ return options;
106
+ }
107
+
108
+ async function completeSummaryWithModelApi(
109
+ model: Model<Api>,
110
+ context: Context,
111
+ options: SimpleStreamOptions,
112
+ registeredStreamSimple: RegisteredProviderConfig["streamSimple"],
113
+ ): Promise<AssistantMessage> {
114
+ if (registeredStreamSimple) {
115
+ return registeredStreamSimple(model, context, options).result();
116
+ }
117
+ return completeWithPiRuntime(model, context, options);
118
+ }
119
+
8
120
  function textFromContent(content: unknown): string {
9
121
  if (typeof content === "string") return content;
10
122
  if (!Array.isArray(content)) return "";
@@ -113,19 +225,9 @@ export async function buildReviewConversationSummary(
113
225
  const model = ctx.modelRegistry.find(parsed.provider, parsed.modelId);
114
226
  if (!model)
115
227
  throw new Error(`Summary model not found: ${config.summary.model}`);
116
- const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
117
- if (!auth.ok) throw new Error(`Summary model unavailable: ${auth.error}`);
118
-
119
- const options = {
120
- ...(auth.apiKey ? { apiKey: auth.apiKey } : {}),
121
- ...(auth.headers ? { headers: auth.headers } : {}),
122
- ...(model.reasoning && config.summary.thinking !== "off"
123
- ? { reasoning: config.summary.thinking }
124
- : {}),
125
- ...(ctx.signal ? { signal: ctx.signal } : {}),
126
- };
127
-
128
- const response = await completeSimple(
228
+ const options = await getSummaryOptions(ctx, model, config);
229
+
230
+ const response = await completeSummaryWithModelApi(
129
231
  model,
130
232
  {
131
233
  messages: [
@@ -137,6 +239,7 @@ export async function buildReviewConversationSummary(
137
239
  ],
138
240
  },
139
241
  options,
242
+ getRegisteredStreamSimple(ctx.modelRegistry, model),
140
243
  );
141
244
 
142
245
  const summary = response.content