@aliou/pi-ts-aperture 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 +1 -1
- package/src/index.ts +26 -8
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -62,17 +62,37 @@ function applyAperture(
|
|
|
62
62
|
return providers;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Re-resolve the active model from the registry and update it via pi.setModel().
|
|
67
|
+
* Call this after updating the registry to ensure the active model uses the new configuration.
|
|
68
|
+
* Returns true if the model was updated.
|
|
69
|
+
*/
|
|
70
|
+
function refreshActiveModel(pi: ExtensionAPI, ctx: ExtensionContext): boolean {
|
|
71
|
+
if (!ctx.model) return false;
|
|
72
|
+
|
|
73
|
+
const updated = ctx.modelRegistry.find(ctx.model.provider, ctx.model.id);
|
|
74
|
+
if (!updated) return false;
|
|
75
|
+
|
|
76
|
+
pi.setModel(updated);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
|
|
65
80
|
export default async function (pi: ExtensionAPI): Promise<void> {
|
|
66
|
-
console.log(`[pi-ts-aperture] loaded on pi ${VERSION}`);
|
|
67
81
|
await configLoader.load();
|
|
68
82
|
|
|
69
83
|
let lastRegisteredProviders = [...configLoader.getConfig().providers];
|
|
70
84
|
|
|
71
85
|
// Apply after all extensions have registered their providers and models.
|
|
72
|
-
pi.
|
|
73
|
-
const ctx = data as ExtensionContext;
|
|
86
|
+
pi.on("before_agent_start", async (_event, ctx) => {
|
|
74
87
|
if (!ctx?.modelRegistry) return;
|
|
75
|
-
|
|
88
|
+
|
|
89
|
+
const overriddenProviders = applyAperture(pi, ctx.modelRegistry);
|
|
90
|
+
|
|
91
|
+
// Re-resolve active model if it belongs to a reconfigured provider.
|
|
92
|
+
// The model was selected before before_agent_start fired, so we need to update it.
|
|
93
|
+
if (ctx.model && overriddenProviders.includes(ctx.model.provider)) {
|
|
94
|
+
refreshActiveModel(pi, ctx);
|
|
95
|
+
}
|
|
76
96
|
});
|
|
77
97
|
|
|
78
98
|
const onSetupComplete = (ctx: ExtensionContext) => {
|
|
@@ -86,13 +106,11 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
86
106
|
|
|
87
107
|
// Re-resolve active model if it belongs to a reconfigured provider.
|
|
88
108
|
if (ctx.model && providers.includes(ctx.model.provider)) {
|
|
89
|
-
|
|
90
|
-
if (updated) {
|
|
109
|
+
if (refreshActiveModel(pi, ctx)) {
|
|
91
110
|
ctx.ui.notify(
|
|
92
|
-
`[aperture] re-routing ${ctx.model.id} through ${
|
|
111
|
+
`[aperture] re-routing ${ctx.model.id} through ${ctx.model.baseUrl}`,
|
|
93
112
|
"info",
|
|
94
113
|
);
|
|
95
|
-
pi.setModel(updated);
|
|
96
114
|
}
|
|
97
115
|
}
|
|
98
116
|
|