@aliou/pi-ts-aperture 0.2.4 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +26 -7
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aliou/pi-ts-aperture",
3
3
  "description": "Route Pi LLM providers through Tailscale Aperture",
4
- "version": "0.2.4",
4
+ "version": "0.2.5",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "private": false,
package/src/index.ts CHANGED
@@ -62,16 +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
81
  await configLoader.load();
67
82
 
68
83
  let lastRegisteredProviders = [...configLoader.getConfig().providers];
69
84
 
70
85
  // Apply after all extensions have registered their providers and models.
71
- pi.events.on("before_agent_start", async (data) => {
72
- const ctx = data as ExtensionContext;
86
+ pi.on("before_agent_start", async (_event, ctx) => {
73
87
  if (!ctx?.modelRegistry) return;
74
- applyAperture(pi, ctx.modelRegistry);
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
+ }
75
96
  });
76
97
 
77
98
  const onSetupComplete = (ctx: ExtensionContext) => {
@@ -85,13 +106,11 @@ export default async function (pi: ExtensionAPI): Promise<void> {
85
106
 
86
107
  // Re-resolve active model if it belongs to a reconfigured provider.
87
108
  if (ctx.model && providers.includes(ctx.model.provider)) {
88
- const updated = ctx.modelRegistry.find(ctx.model.provider, ctx.model.id);
89
- if (updated) {
109
+ if (refreshActiveModel(pi, ctx)) {
90
110
  ctx.ui.notify(
91
- `[aperture] re-routing ${ctx.model.id} through ${updated.baseUrl}`,
111
+ `[aperture] re-routing ${ctx.model.id} through ${ctx.model.baseUrl}`,
92
112
  "info",
93
113
  );
94
- pi.setModel(updated);
95
114
  }
96
115
  }
97
116