@mrclrchtr/supi-settings 6.4.0 → 7.0.1
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/node_modules/@mrclrchtr/supi-core/README.md +8 -0
- package/node_modules/@mrclrchtr/supi-core/package.json +1 -1
- package/node_modules/@mrclrchtr/supi-core/src/footer-registry.ts +10 -3
- package/node_modules/@mrclrchtr/supi-core/src/index.ts +2 -0
- package/node_modules/@mrclrchtr/supi-core/src/llm.ts +141 -16
- package/package.json +2 -2
|
@@ -21,6 +21,7 @@ pnpm add @mrclrchtr/supi-core
|
|
|
21
21
|
## Package surfaces
|
|
22
22
|
|
|
23
23
|
- `@mrclrchtr/supi-core/api` — reusable helpers for other packages and extensions
|
|
24
|
+
- `@mrclrchtr/supi-core/llm` — PI-owned direct model requests and JSON helpers
|
|
24
25
|
- `@mrclrchtr/supi-core/report` — shared text/report rendering helpers for TUI and plain-text summaries
|
|
25
26
|
|
|
26
27
|
## What you get from the API
|
|
@@ -48,6 +49,13 @@ Config file locations:
|
|
|
48
49
|
|
|
49
50
|
- `wrapExtensionContext()` — wrap injected text in SuPi's `<extension-context>` tag
|
|
50
51
|
|
|
52
|
+
### Model requests
|
|
53
|
+
|
|
54
|
+
- `completeModelRequest(ctx, model, context, options)` — complete through PI's model registry with stable feature affinity. PI owns auth and endpoint resolution.
|
|
55
|
+
- `callWithJsonResponse()` — retry a registry request, extract JSON, and validate it with TypeBox.
|
|
56
|
+
|
|
57
|
+
`completeModelRequest()` requires a stable `affinityScope`. It keeps cache retention defaults, does not include prompt content in the affinity ID, and adds OpenCode headers only when the provider or exact model endpoint matches OpenCode. Pass `maxTokens: model.maxTokens` when a caller needs the model's declared output cap without using PI private modules.
|
|
58
|
+
|
|
51
59
|
### Shared registries
|
|
52
60
|
|
|
53
61
|
- context-provider registry for `/supi-context`
|
|
@@ -2,17 +2,24 @@
|
|
|
2
2
|
//
|
|
3
3
|
// Extensions register pre-styled text chunks with a placement hint
|
|
4
4
|
// ("stats" for the metrics line, "status" for the extension status line).
|
|
5
|
-
// The custom footer in supi-extras
|
|
6
|
-
//
|
|
5
|
+
// The custom footer in supi-extras reads these contributions and renders them
|
|
6
|
+
// alongside the built-in metrics. Extensions can use PI's status API as a
|
|
7
|
+
// fallback when the custom footer is not installed.
|
|
7
8
|
|
|
8
9
|
import { createRegistry } from "./registry-utils.ts";
|
|
9
10
|
|
|
11
|
+
/** Event emitted when a dynamic footer contribution needs a new render. */
|
|
12
|
+
export const FOOTER_INVALIDATE_EVENT = "supi:footer:invalidate";
|
|
13
|
+
|
|
10
14
|
/** Where the contribution should appear in the footer. */
|
|
11
15
|
export type FooterPlacement = "stats" | "stats-end" | "status";
|
|
12
16
|
|
|
13
17
|
/** A single footer contribution registered by an extension. */
|
|
14
18
|
export interface FooterContribution {
|
|
15
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Unique key for this contribution. Re-registering with the same key replaces it.
|
|
21
|
+
* A same-key Pi status is treated as this contribution's built-in-footer fallback.
|
|
22
|
+
*/
|
|
16
23
|
key: string;
|
|
17
24
|
/** Which footer line this belongs on. */
|
|
18
25
|
placement: FooterPlacement;
|
|
@@ -15,6 +15,8 @@ export * from "./debug.ts";
|
|
|
15
15
|
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
16
16
|
export * from "./footer-registry.ts";
|
|
17
17
|
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
18
|
+
export * from "./llm.ts";
|
|
19
|
+
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
18
20
|
export * from "./model-selection.ts";
|
|
19
21
|
// biome-ignore lint/performance/noReExportAll: intentional convenience barrel
|
|
20
22
|
export * from "./path.ts";
|
|
@@ -1,12 +1,131 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import type {
|
|
3
|
+
Api,
|
|
4
|
+
AssistantMessage,
|
|
5
|
+
Context,
|
|
6
|
+
Model,
|
|
7
|
+
ModelsApiStreamOptions,
|
|
8
|
+
ProviderHeaders,
|
|
9
|
+
} from "@earendil-works/pi-ai";
|
|
2
10
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
11
|
import type { TSchema } from "typebox";
|
|
4
12
|
import { Value } from "typebox/value";
|
|
5
13
|
|
|
6
14
|
// Shared LLM utilities for SuPi extensions.
|
|
7
15
|
//
|
|
8
|
-
// Provides retry logic, structured LLM call helpers,
|
|
9
|
-
// common patterns for extensions that interact with AI models.
|
|
16
|
+
// Provides PI-owned model requests, retry logic, structured LLM call helpers,
|
|
17
|
+
// and other common patterns for extensions that interact with AI models.
|
|
18
|
+
|
|
19
|
+
const MODEL_REQUEST_NAMESPACE = "supi-direct-model-request-v1";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Options for {@link completeModelRequest}.
|
|
23
|
+
*
|
|
24
|
+
* Authentication, provider environment, and session identity stay under PI
|
|
25
|
+
* control. The feature supplies a stable scope for its prompt stream.
|
|
26
|
+
*/
|
|
27
|
+
export type CompleteModelRequestOptions<TApi extends Api = Api> = Omit<
|
|
28
|
+
ModelsApiStreamOptions<TApi>,
|
|
29
|
+
"apiKey" | "env" | "sessionId"
|
|
30
|
+
> & {
|
|
31
|
+
/** Stable feature scope. Do not include prompt, turn, or retry data. */
|
|
32
|
+
affinityScope: string;
|
|
33
|
+
/** PI owns these fields, including for APIs with open-ended option types. */
|
|
34
|
+
apiKey?: never;
|
|
35
|
+
env?: never;
|
|
36
|
+
sessionId?: never;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function createModelRequestAffinityId(
|
|
40
|
+
sessionId: string,
|
|
41
|
+
affinityScope: string,
|
|
42
|
+
model: Model<Api>,
|
|
43
|
+
): string {
|
|
44
|
+
const material = JSON.stringify([
|
|
45
|
+
MODEL_REQUEST_NAMESPACE,
|
|
46
|
+
sessionId,
|
|
47
|
+
affinityScope,
|
|
48
|
+
model.provider,
|
|
49
|
+
model.id,
|
|
50
|
+
]);
|
|
51
|
+
const digest = createHash("sha256").update(material, "utf8").digest("hex");
|
|
52
|
+
return `supi-${digest.slice(0, 56)}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isOpenCodeModel(model: Model<Api>): boolean {
|
|
56
|
+
if (model.provider === "opencode" || model.provider === "opencode-go") return true;
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
return new URL(model.baseUrl).hostname === "opencode.ai";
|
|
60
|
+
} catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function hasHeader(headers: ProviderHeaders, name: string): boolean {
|
|
66
|
+
const lowerName = name.toLowerCase();
|
|
67
|
+
return Object.keys(headers).some((headerName) => headerName.toLowerCase() === lowerName);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function addOpenCodeDefaultHeaders(
|
|
71
|
+
model: Model<Api>,
|
|
72
|
+
affinityId: string,
|
|
73
|
+
headers: ProviderHeaders,
|
|
74
|
+
): ProviderHeaders {
|
|
75
|
+
if (!isOpenCodeModel(model)) return headers;
|
|
76
|
+
|
|
77
|
+
const result = { ...headers };
|
|
78
|
+
if (!hasHeader(result, "x-opencode-session")) {
|
|
79
|
+
result["x-opencode-session"] = affinityId;
|
|
80
|
+
}
|
|
81
|
+
if (!hasHeader(result, "x-opencode-client")) {
|
|
82
|
+
result["x-opencode-client"] = "pi";
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Complete a direct request through PI's model registry.
|
|
89
|
+
*
|
|
90
|
+
* PI resolves authentication, provider headers, environment, and the
|
|
91
|
+
* effective endpoint. This helper adds one stable opaque session identity for
|
|
92
|
+
* the feature prompt stream and applies the OpenCode compatibility defaults.
|
|
93
|
+
* It does not retry, validate output, or present errors.
|
|
94
|
+
*
|
|
95
|
+
* When `maxTokens` is omitted, the underlying registry receives no explicit
|
|
96
|
+
* output cap. A caller that needs the selected model's declared cap can pass
|
|
97
|
+
* `maxTokens: model.maxTokens` without importing PI internals.
|
|
98
|
+
*/
|
|
99
|
+
export async function completeModelRequest<TApi extends Api>(
|
|
100
|
+
ctx: ExtensionContext,
|
|
101
|
+
model: Model<TApi>,
|
|
102
|
+
context: Context,
|
|
103
|
+
options: CompleteModelRequestOptions<TApi>,
|
|
104
|
+
): Promise<AssistantMessage> {
|
|
105
|
+
const { affinityScope, transformHeaders: callerTransformHeaders, ...requestOptions } = options;
|
|
106
|
+
const safeRequestOptions = { ...requestOptions };
|
|
107
|
+
delete safeRequestOptions.apiKey;
|
|
108
|
+
delete safeRequestOptions.env;
|
|
109
|
+
delete safeRequestOptions.sessionId;
|
|
110
|
+
|
|
111
|
+
const affinityId = createModelRequestAffinityId(
|
|
112
|
+
ctx.sessionManager.getSessionId(),
|
|
113
|
+
affinityScope,
|
|
114
|
+
model,
|
|
115
|
+
);
|
|
116
|
+
const transformHeaders = async (headers: ProviderHeaders): Promise<ProviderHeaders> => {
|
|
117
|
+
const transformed = callerTransformHeaders ? await callerTransformHeaders(headers) : headers;
|
|
118
|
+
return addOpenCodeDefaultHeaders(model, affinityId, transformed);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// Restore PI's conditional provider-option type after removing owned fields.
|
|
122
|
+
return ctx.modelRegistry.complete(model, context, {
|
|
123
|
+
...safeRequestOptions,
|
|
124
|
+
signal: safeRequestOptions.signal ?? ctx.signal,
|
|
125
|
+
sessionId: affinityId,
|
|
126
|
+
transformHeaders,
|
|
127
|
+
} as unknown as ModelsApiStreamOptions<TApi>);
|
|
128
|
+
}
|
|
10
129
|
|
|
11
130
|
/**
|
|
12
131
|
* Options for {@link withRetry}.
|
|
@@ -122,6 +241,8 @@ export function extractJsonFromResponse<T extends TSchema>(
|
|
|
122
241
|
export interface CallWithJsonResponseOptions {
|
|
123
242
|
/** The prompt to send to the LLM. */
|
|
124
243
|
prompt: string;
|
|
244
|
+
/** Stable feature scope for request affinity. Do not include prompt or retry data. */
|
|
245
|
+
affinityScope: string;
|
|
125
246
|
/** Optional data context appended to the prompt. */
|
|
126
247
|
dataContext?: string;
|
|
127
248
|
/** Maximum tokens for the response. Default: 4096 */
|
|
@@ -135,8 +256,9 @@ export interface CallWithJsonResponseOptions {
|
|
|
135
256
|
/**
|
|
136
257
|
* Call the LLM with a prompt and validate the JSON response against a TypeBox schema.
|
|
137
258
|
*
|
|
138
|
-
* Handles model resolution,
|
|
139
|
-
*
|
|
259
|
+
* Handles model resolution, retry via `withRetry`, text extraction, JSON
|
|
260
|
+
* matching, and TypeBox validation. The request itself stays under PI
|
|
261
|
+
* registry authority through {@link completeModelRequest}.
|
|
140
262
|
*
|
|
141
263
|
* Returns `null` when:
|
|
142
264
|
* - No model is available
|
|
@@ -145,7 +267,7 @@ export interface CallWithJsonResponseOptions {
|
|
|
145
267
|
* - JSON doesn't match the schema
|
|
146
268
|
* - The request is aborted
|
|
147
269
|
*
|
|
148
|
-
* @param ctx - The extension context for model
|
|
270
|
+
* @param ctx - The extension context for model selection and PI registry access.
|
|
149
271
|
* @param options - Call options including prompt, schema, and retry config.
|
|
150
272
|
* @param schema - TypeBox schema to validate the JSON response against.
|
|
151
273
|
* @returns The parsed and validated result, or `null`.
|
|
@@ -155,14 +277,18 @@ export async function callWithJsonResponse<T extends TSchema>(
|
|
|
155
277
|
options: CallWithJsonResponseOptions,
|
|
156
278
|
schema: T,
|
|
157
279
|
): Promise<{ parsed: import("typebox").Static<T> } | null> {
|
|
158
|
-
const {
|
|
280
|
+
const {
|
|
281
|
+
prompt,
|
|
282
|
+
affinityScope,
|
|
283
|
+
dataContext,
|
|
284
|
+
maxTokens = 4096,
|
|
285
|
+
systemPrompt = "",
|
|
286
|
+
retries = 2,
|
|
287
|
+
} = options;
|
|
159
288
|
|
|
160
289
|
const model = ctx.model ?? ctx.modelRegistry.getAvailable()[0] ?? null;
|
|
161
290
|
if (!model) return null;
|
|
162
291
|
|
|
163
|
-
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
|
|
164
|
-
if (!auth.ok || !auth.apiKey) return null;
|
|
165
|
-
|
|
166
292
|
const fullPrompt = dataContext
|
|
167
293
|
? `${prompt}
|
|
168
294
|
|
|
@@ -171,8 +297,9 @@ ${dataContext}`
|
|
|
171
297
|
: prompt;
|
|
172
298
|
|
|
173
299
|
const response = await withRetry(
|
|
174
|
-
async () =>
|
|
175
|
-
|
|
300
|
+
async () =>
|
|
301
|
+
completeModelRequest(
|
|
302
|
+
ctx,
|
|
176
303
|
model,
|
|
177
304
|
{
|
|
178
305
|
systemPrompt,
|
|
@@ -185,13 +312,11 @@ ${dataContext}`
|
|
|
185
312
|
],
|
|
186
313
|
},
|
|
187
314
|
{
|
|
188
|
-
|
|
189
|
-
headers: auth.headers,
|
|
315
|
+
affinityScope,
|
|
190
316
|
signal: ctx.signal,
|
|
191
317
|
maxTokens,
|
|
192
318
|
},
|
|
193
|
-
)
|
|
194
|
-
},
|
|
319
|
+
),
|
|
195
320
|
{ retries, baseDelayMs: 1000, signal: ctx.signal },
|
|
196
321
|
);
|
|
197
322
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mrclrchtr/supi-settings",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.1",
|
|
4
4
|
"description": "One project/global settings UI for SuPi packages",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"!__tests__"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@mrclrchtr/supi-core": "
|
|
33
|
+
"@mrclrchtr/supi-core": "7.0.1"
|
|
34
34
|
},
|
|
35
35
|
"bundledDependencies": [
|
|
36
36
|
"@mrclrchtr/supi-core"
|