@andares/pi-toolkits 0.3.0 → 0.4.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.
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Shared model-config helpers for features that persist a "provider/id"
3
+ * model key and resolve it against the currently available model list
4
+ * (review, compact-model — favorites keeps its own store-local key logic).
5
+ *
6
+ * Types are derived from pi-coding-agent's own exports (ModelRegistry)
7
+ * rather than imported from @earendil-works/pi-ai: pi-ai 0.87's index.d.ts
8
+ * re-exports `Model`/`Api` via extensioned `./types.ts` specifiers, which
9
+ * tsc 5.9 bundler resolution substitutes to `.d.ts` but some tsserver
10
+ * builds do not. Deriving from ModelRegistry keeps every resolver in
11
+ * agreement with the exact type `ctx.modelRegistry.getAvailable()` returns.
12
+ */
13
+ import type {
14
+ ExtensionContext,
15
+ ModelRegistry,
16
+ } from "@earendil-works/pi-coding-agent";
17
+
18
+ /** The model type pi's ModelRegistry exposes (`Model<Api>`). */
19
+ export type AvailableModel = ReturnType<ModelRegistry["getAvailable"]>[number];
20
+
21
+ /** Model identity used as a persisted config key: "provider/id". */
22
+ export function modelKey(model: { provider: string; id: string }): string {
23
+ return `${model.provider}/${model.id}`;
24
+ }
25
+
26
+ /**
27
+ * Resolve a persisted "provider/id" key against the available model list.
28
+ * Ids may themselves contain slashes (e.g. openrouter/openai/gpt-5), so only
29
+ * the first slash splits provider from id. Returns undefined for missing,
30
+ * malformed, or currently-unavailable keys.
31
+ */
32
+ export function resolveConfiguredModel(
33
+ ctx: ExtensionContext,
34
+ key: string | undefined,
35
+ ): AvailableModel | undefined {
36
+ if (!key) return undefined;
37
+ const slash = key.indexOf("/");
38
+ if (slash <= 0 || slash === key.length - 1) return undefined;
39
+ const provider = key.slice(0, slash);
40
+ const id = key.slice(slash + 1);
41
+ return ctx.modelRegistry
42
+ .getAvailable()
43
+ .find((m) => m.provider === provider && m.id === id);
44
+ }