@aliou/pi-synthetic 0.15.0 → 0.17.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aliou/pi-synthetic",
3
- "version": "0.15.0",
3
+ "version": "0.17.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
@@ -32,18 +32,18 @@
32
32
  "README.md"
33
33
  ],
34
34
  "peerDependencies": {
35
- "@mariozechner/pi-coding-agent": "0.72.0",
36
- "@mariozechner/pi-tui": "0.72.0"
35
+ "@earendil-works/pi-coding-agent": "0.74.0",
36
+ "@earendil-works/pi-tui": "0.74.0"
37
37
  },
38
38
  "dependencies": {
39
- "@aliou/pi-utils-settings": "^0.13.0",
40
- "@aliou/pi-utils-ui": "^0.1.2"
39
+ "@aliou/pi-utils-settings": "^0.15.0",
40
+ "@aliou/pi-utils-ui": "^0.4.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@aliou/biome-plugins": "^0.7.0",
44
44
  "@biomejs/biome": "^2.4.2",
45
45
  "@changesets/cli": "^2.27.11",
46
- "@mariozechner/pi-coding-agent": "0.72.0",
46
+ "@earendil-works/pi-coding-agent": "0.74.0",
47
47
  "typebox": "^1.1.37",
48
48
  "@types/node": "^25.0.10",
49
49
  "husky": "^9.1.7",
@@ -51,10 +51,10 @@
51
51
  "vitest": "^4.0.18"
52
52
  },
53
53
  "peerDependenciesMeta": {
54
- "@mariozechner/pi-coding-agent": {
54
+ "@earendil-works/pi-coding-agent": {
55
55
  "optional": true
56
56
  },
57
- "@mariozechner/pi-tui": {
57
+ "@earendil-works/pi-tui": {
58
58
  "optional": true
59
59
  }
60
60
  },
package/src/config.ts CHANGED
@@ -4,8 +4,8 @@ import {
4
4
  registerSettingsCommand,
5
5
  type SettingsSection,
6
6
  } from "@aliou/pi-utils-settings";
7
- import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
8
- import type { SettingItem } from "@mariozechner/pi-tui";
7
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
8
+ import type { SettingItem } from "@earendil-works/pi-tui";
9
9
  import pkg from "../package.json" with { type: "json" };
10
10
 
11
11
  export type SyntheticFeatureId =
@@ -1,4 +1,4 @@
1
- import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
2
  import { configLoader } from "../../config";
3
3
  import { getSyntheticApiKey } from "../../lib/env";
4
4
  import { fetchQuotas } from "../../utils/quotas";
@@ -1,7 +1,7 @@
1
- import type { Theme } from "@mariozechner/pi-coding-agent";
2
- import { DynamicBorder } from "@mariozechner/pi-coding-agent";
3
- import type { Component, TUI } from "@mariozechner/pi-tui";
4
- import { Loader, matchesKey, truncateToWidth } from "@mariozechner/pi-tui";
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import { DynamicBorder } from "@earendil-works/pi-coding-agent";
3
+ import type { Component, TUI } from "@earendil-works/pi-tui";
4
+ import { Loader, matchesKey, truncateToWidth } from "@earendil-works/pi-tui";
5
5
  import type { QuotasResponse } from "../../../types/quotas";
6
6
  import {
7
7
  assessWindow,
@@ -1,4 +1,4 @@
1
- import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
2
  import {
3
3
  configLoader,
4
4
  SYNTHETIC_EXTENSIONS_REGISTER_EVENT,
@@ -1,4 +1,7 @@
1
- import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
1
+ import type {
2
+ AuthStorage,
3
+ ExtensionAPI,
4
+ } from "@earendil-works/pi-coding-agent";
2
5
  import {
3
6
  configLoader,
4
7
  emitSyntheticConfigUpdated,
@@ -12,6 +15,18 @@ import {
12
15
  type SyntheticFeatureId,
13
16
  seedSyntheticConfigIfMissing,
14
17
  } from "../../config";
18
+ import { getSyntheticApiKey } from "../../lib/env";
19
+ import { QuotaStore } from "../../services/quota-store";
20
+ import {
21
+ parseQuotaHeader,
22
+ type QuotasResponse,
23
+ SYNTHETIC_QUOTAS_READ_EVENT,
24
+ SYNTHETIC_QUOTAS_REQUEST_EVENT,
25
+ SYNTHETIC_QUOTAS_UPDATED_EVENT,
26
+ type SyntheticQuotasReadPayload,
27
+ type SyntheticQuotasRequestPayload,
28
+ } from "../../types/quotas";
29
+ import { fetchQuotas } from "../../utils/quotas";
15
30
  import { SYNTHETIC_MODELS } from "./models";
16
31
 
17
32
  export function buildSyntheticProviderModels(includeProxiedModels: boolean) {
@@ -71,6 +86,54 @@ export default async function (pi: ExtensionAPI) {
71
86
  getLoadedFeatures: () => loadedFeatures,
72
87
  });
73
88
 
89
+ const quotaStore = new QuotaStore();
90
+ let currentAuthStorage: AuthStorage | undefined;
91
+
92
+ async function fetchQuotasFromAuth(): Promise<QuotasResponse | undefined> {
93
+ if (!currentAuthStorage) return undefined;
94
+ const apiKey = await getSyntheticApiKey(currentAuthStorage);
95
+ if (!apiKey) return undefined;
96
+ const result = await fetchQuotas(apiKey);
97
+ return result.success ? result.data.quotas : undefined;
98
+ }
99
+
100
+ quotaStore.subscribe((snapshot) => {
101
+ pi.events.emit(SYNTHETIC_QUOTAS_UPDATED_EVENT, {
102
+ quotas: snapshot.quotas,
103
+ source: snapshot.source,
104
+ updatedAt: snapshot.updatedAt,
105
+ });
106
+ });
107
+
108
+ pi.on("after_provider_response", (event, ctx) => {
109
+ if (ctx.model?.provider !== "synthetic") return;
110
+ const quotas = parseQuotaHeader(event.headers);
111
+ if (quotas) quotaStore.ingest(quotas, "header");
112
+ });
113
+
114
+ pi.events.on(SYNTHETIC_QUOTAS_REQUEST_EVENT, async (data: unknown) => {
115
+ const payload = data as SyntheticQuotasRequestPayload | undefined;
116
+ const snapshot = await quotaStore.refreshFromApi(fetchQuotasFromAuth);
117
+ if (payload?.respond) {
118
+ payload.respond(snapshot);
119
+ }
120
+ });
121
+
122
+ pi.events.on(SYNTHETIC_QUOTAS_READ_EVENT, (data: unknown) => {
123
+ const { respond } = data as SyntheticQuotasReadPayload;
124
+ respond(quotaStore.getSnapshot());
125
+ });
126
+
127
+ pi.on("session_before_switch", () => {
128
+ quotaStore.clear();
129
+ currentAuthStorage = undefined;
130
+ });
131
+
132
+ pi.on("session_shutdown", () => {
133
+ quotaStore.clear();
134
+ currentAuthStorage = undefined;
135
+ });
136
+
74
137
  pi.on("session_start", async (_event, ctx) => {
75
138
  const messages = pendingMessages.splice(0).map((m) => `- ${m}`);
76
139
  if (messages.length > 0) {
@@ -81,7 +144,13 @@ export default async function (pi: ExtensionAPI) {
81
144
  }
82
145
 
83
146
  loadedFeatures.clear();
147
+ quotaStore.clear();
148
+ currentAuthStorage = ctx.modelRegistry.authStorage;
84
149
  pi.events.emit(SYNTHETIC_EXTENSIONS_REQUEST_EVENT, undefined);
85
150
  emitSyntheticConfigUpdated(pi);
151
+
152
+ if (ctx.model?.provider === "synthetic") {
153
+ await quotaStore.refreshFromApi(fetchQuotasFromAuth);
154
+ }
86
155
  });
87
156
  }
@@ -2,7 +2,7 @@
2
2
  // Source: https://api.synthetic.new/openai/v1/models
3
3
  // maxTokens sourced from https://models.dev/api.json (synthetic provider)
4
4
 
5
- import type { ProviderModelConfig } from "@mariozechner/pi-coding-agent";
5
+ import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
6
6
 
7
7
  export interface SyntheticModelConfig extends ProviderModelConfig {
8
8
  /** Upstream backend Synthetic proxies this model through (e.g. "fireworks", "together", "synthetic"). */
@@ -1,4 +1,7 @@
1
- import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
1
+ import type {
2
+ ExtensionAPI,
3
+ ExtensionContext,
4
+ } from "@earendil-works/pi-coding-agent";
2
5
  import {
3
6
  configLoader,
4
7
  SYNTHETIC_CONFIG_UPDATED_EVENT,
@@ -6,48 +9,100 @@ import {
6
9
  SYNTHETIC_EXTENSIONS_REQUEST_EVENT,
7
10
  type SyntheticConfigUpdatedPayload,
8
11
  } from "../../config";
9
- import { clearAlertState, triggerCheck } from "./notifier";
12
+ import { QuotaWarningNotifier } from "../../services/quota-warnings";
13
+ import {
14
+ SYNTHETIC_QUOTAS_READ_EVENT,
15
+ SYNTHETIC_QUOTAS_REQUEST_EVENT,
16
+ type SyntheticQuotasReadPayload,
17
+ type SyntheticQuotasRequestPayload,
18
+ type SyntheticQuotasSnapshotPayload,
19
+ } from "../../types/quotas";
10
20
 
11
21
  export default async function (pi: ExtensionAPI) {
12
22
  await configLoader.load();
13
23
 
14
24
  let enabled = configLoader.getConfig().quotaWarnings;
15
- let currentModel: { provider: string; id: string } | undefined;
16
- let currentContext: Parameters<typeof triggerCheck>[0] | undefined;
25
+
26
+ const notifier = new QuotaWarningNotifier();
27
+
28
+ function requestQuotas(
29
+ respond?: (snapshot: SyntheticQuotasSnapshotPayload | undefined) => void,
30
+ ): void {
31
+ pi.events.emit(SYNTHETIC_QUOTAS_REQUEST_EVENT, {
32
+ respond,
33
+ } satisfies SyntheticQuotasRequestPayload);
34
+ }
35
+
36
+ function readQuotas(
37
+ respond: (snapshot: SyntheticQuotasSnapshotPayload | undefined) => void,
38
+ ): void {
39
+ pi.events.emit(SYNTHETIC_QUOTAS_READ_EVENT, {
40
+ respond,
41
+ } satisfies SyntheticQuotasReadPayload);
42
+ }
43
+
44
+ function evaluateFromStoreOrRefresh(ctx: ExtensionContext): void {
45
+ if (!enabled || ctx.model?.provider !== "synthetic") return;
46
+ readQuotas((snapshot) => {
47
+ if (snapshot) {
48
+ notifier.evaluate(
49
+ snapshot.quotas,
50
+ snapshot.source === "header",
51
+ (message, level) => {
52
+ ctx.ui.notify(message, level);
53
+ },
54
+ );
55
+ } else {
56
+ requestQuotas((refreshed) => {
57
+ if (!refreshed) return;
58
+ notifier.evaluate(
59
+ refreshed.quotas,
60
+ refreshed.source === "header",
61
+ (message, level) => {
62
+ ctx.ui.notify(message, level);
63
+ },
64
+ );
65
+ });
66
+ }
67
+ });
68
+ }
17
69
 
18
70
  pi.events.on(SYNTHETIC_CONFIG_UPDATED_EVENT, (data: unknown) => {
19
71
  enabled = (data as SyntheticConfigUpdatedPayload).config.quotaWarnings;
20
72
 
21
73
  if (!enabled) {
22
- clearAlertState();
74
+ notifier.clearAlertState();
23
75
  return;
24
76
  }
25
77
 
26
- if (currentContext && currentModel?.provider === "synthetic") {
27
- clearAlertState();
28
- triggerCheck(currentContext, currentModel, false);
29
- }
78
+ notifier.clearAlertState();
79
+ // In config updates we don't have ctx, so we just clear. The next lifecycle event will refresh.
30
80
  });
31
81
 
32
- pi.on("session_start", async (_event, ctx) => {
33
- currentContext = ctx;
34
- currentModel = ctx.model;
35
- if (!enabled || ctx.model?.provider !== "synthetic") return;
36
- clearAlertState();
37
- triggerCheck(ctx, ctx.model, false);
82
+ pi.on("session_start", (_event, ctx) => {
83
+ notifier.clearAlertState();
84
+ evaluateFromStoreOrRefresh(ctx);
38
85
  });
39
86
 
40
- pi.on("agent_end", async (_event, ctx) => {
41
- currentContext = ctx;
42
- currentModel = ctx.model;
43
- if (!enabled || ctx.model?.provider !== "synthetic") return;
44
- triggerCheck(ctx, ctx.model, true);
87
+ pi.on("model_select", (_event, ctx) => {
88
+ notifier.clearAlertState();
89
+ evaluateFromStoreOrRefresh(ctx);
90
+ });
91
+
92
+ pi.on("agent_end", (_event, ctx) => {
93
+ evaluateFromStoreOrRefresh(ctx);
94
+ });
95
+
96
+ pi.on("turn_end", (_event, ctx) => {
97
+ evaluateFromStoreOrRefresh(ctx);
98
+ });
99
+
100
+ pi.on("session_before_switch", () => {
101
+ notifier.clearAlertState();
45
102
  });
46
103
 
47
- pi.on("session_shutdown", async () => {
48
- currentContext = undefined;
49
- currentModel = undefined;
50
- clearAlertState();
104
+ pi.on("session_shutdown", () => {
105
+ notifier.clearAlertState();
51
106
  });
52
107
 
53
108
  pi.events.on(SYNTHETIC_EXTENSIONS_REQUEST_EVENT, () => {
@@ -1,4 +1,4 @@
1
- import type { AuthStorage, ExtensionAPI } from "@mariozechner/pi-coding-agent";
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
2
  import {
3
3
  configLoader,
4
4
  SYNTHETIC_CONFIG_UPDATED_EVENT,
@@ -6,9 +6,13 @@ import {
6
6
  SYNTHETIC_EXTENSIONS_REQUEST_EVENT,
7
7
  type SyntheticConfigUpdatedPayload,
8
8
  } from "../../config";
9
- import { getSyntheticApiKey } from "../../lib/env";
10
- import type { QuotasResponse } from "../../types/quotas";
11
- import { fetchQuotas, formatResetTime } from "../../utils/quotas";
9
+ import {
10
+ type QuotasResponse,
11
+ SYNTHETIC_QUOTAS_REQUEST_EVENT,
12
+ SYNTHETIC_QUOTAS_UPDATED_EVENT,
13
+ type SyntheticQuotasUpdatedPayload,
14
+ } from "../../types/quotas";
15
+ import { formatResetTime } from "../../utils/quotas";
12
16
 
13
17
  interface RateWindow {
14
18
  label: string;
@@ -24,14 +28,6 @@ interface UsageSnapshot {
24
28
  lastSuccessAt?: number;
25
29
  }
26
30
 
27
- interface SubCoreSettingsPayload {
28
- settings?: {
29
- behavior?: {
30
- refreshInterval: number;
31
- };
32
- };
33
- }
34
-
35
31
  function toUsageSnapshot(quotas: QuotasResponse): UsageSnapshot {
36
32
  const windows: RateWindow[] = [];
37
33
 
@@ -105,64 +101,42 @@ function toUsageSnapshot(quotas: QuotasResponse): UsageSnapshot {
105
101
  };
106
102
  }
107
103
 
108
- async function emitCurrentUsage(
109
- pi: ExtensionAPI,
110
- authStorage: AuthStorage,
111
- ): Promise<void> {
112
- const apiKey = await getSyntheticApiKey(authStorage);
113
- if (!apiKey) return;
114
- const result = await fetchQuotas(apiKey);
115
- if (!result.success) return;
116
- pi.events.emit("sub-core:update-current", {
117
- state: {
118
- provider: "synthetic",
119
- usage: toUsageSnapshot(result.data.quotas),
120
- },
121
- });
122
- }
123
-
124
104
  export function registerSubBarIntegration(pi: ExtensionAPI): void {
125
- let interval: NodeJS.Timeout | undefined;
126
- let refreshMs = 60000;
127
105
  let subCoreReady = false;
128
106
  let currentProvider: string | undefined;
129
- let currentAuthStorage: AuthStorage | undefined;
130
107
  let enabled = configLoader.getConfig().subBarIntegration;
131
108
 
132
109
  function isSynthetic(): boolean {
133
110
  return enabled && currentProvider === "synthetic";
134
111
  }
135
112
 
136
- function stop(): void {
137
- if (interval) {
138
- clearInterval(interval);
139
- interval = undefined;
140
- }
113
+ function emitUsage(quotas: QuotasResponse): void {
114
+ pi.events.emit("sub-core:update-current", {
115
+ state: {
116
+ provider: "synthetic",
117
+ usage: toUsageSnapshot(quotas),
118
+ },
119
+ });
141
120
  }
142
121
 
143
- function startPolling(authStorage: AuthStorage): void {
144
- stop();
145
- currentAuthStorage = authStorage;
146
- void emitCurrentUsage(pi, authStorage);
147
- const ms = Math.max(10000, refreshMs);
148
- interval = setInterval(() => {
149
- if (isSynthetic() && currentAuthStorage) {
150
- void emitCurrentUsage(pi, currentAuthStorage);
151
- }
152
- }, ms);
153
- interval.unref?.();
122
+ function requestQuotas(): void {
123
+ pi.events.emit(SYNTHETIC_QUOTAS_REQUEST_EVENT, undefined);
154
124
  }
155
125
 
126
+ // Receive quota updates from the provider extension
127
+ pi.events.on(SYNTHETIC_QUOTAS_UPDATED_EVENT, (data: unknown) => {
128
+ if (!isSynthetic() || !subCoreReady) return;
129
+ const { quotas } = data as SyntheticQuotasUpdatedPayload;
130
+ emitUsage(quotas);
131
+ });
132
+
156
133
  pi.events.on(SYNTHETIC_CONFIG_UPDATED_EVENT, (data: unknown) => {
157
134
  enabled = (data as SyntheticConfigUpdatedPayload).config.subBarIntegration;
158
135
 
159
- if (!enabled) {
160
- stop();
161
- return;
162
- }
136
+ if (!enabled) return;
163
137
 
164
- if (subCoreReady && currentAuthStorage && currentProvider === "synthetic") {
165
- startPolling(currentAuthStorage);
138
+ if (subCoreReady && currentProvider === "synthetic") {
139
+ requestQuotas();
166
140
  }
167
141
  });
168
142
 
@@ -170,48 +144,24 @@ export function registerSubBarIntegration(pi: ExtensionAPI): void {
170
144
  subCoreReady = true;
171
145
  });
172
146
 
173
- pi.events.on("sub-core:settings:updated", (data: unknown) => {
174
- const payload = data as SubCoreSettingsPayload;
175
- if (payload.settings?.behavior?.refreshInterval) {
176
- refreshMs = payload.settings.behavior.refreshInterval * 1000;
177
- if (interval && isSynthetic() && currentAuthStorage) {
178
- startPolling(currentAuthStorage);
179
- }
180
- }
147
+ pi.on("session_start", async (_event, ctx) => {
148
+ currentProvider = ctx.model?.provider;
181
149
  });
182
150
 
183
- pi.on("session_start", async (_event, ctx) => {
151
+ pi.on("model_select", async (_event, ctx) => {
184
152
  currentProvider = ctx.model?.provider;
185
- currentAuthStorage = ctx.modelRegistry.authStorage;
186
153
 
187
154
  if (subCoreReady && isSynthetic()) {
188
- const apiKey = await getSyntheticApiKey(currentAuthStorage);
189
- if (apiKey) {
190
- startPolling(currentAuthStorage);
191
- }
155
+ requestQuotas();
192
156
  }
193
157
  });
194
158
 
195
- pi.on("model_select", async (event, ctx) => {
196
- currentProvider = event.model?.provider;
197
- currentAuthStorage = ctx.modelRegistry.authStorage;
198
-
199
- if (subCoreReady && isSynthetic()) {
200
- const apiKey = await getSyntheticApiKey(currentAuthStorage);
201
- if (apiKey) {
202
- startPolling(currentAuthStorage);
203
- } else {
204
- stop();
205
- }
206
- } else {
207
- stop();
208
- }
159
+ pi.on("session_before_switch", (_event, ctx) => {
160
+ currentProvider = ctx.model?.provider;
209
161
  });
210
162
 
211
163
  pi.on("session_shutdown", () => {
212
164
  currentProvider = undefined;
213
- currentAuthStorage = undefined;
214
- stop();
215
165
  });
216
166
  }
217
167