@aliou/pi-synthetic 0.8.4 → 0.9.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/README.md +16 -7
- package/package.json +5 -3
- package/src/extensions/command-quotas/command.ts +65 -0
- package/src/extensions/command-quotas/components/quotas-display.ts +314 -0
- package/src/extensions/command-quotas/index.ts +8 -0
- package/src/{hooks → extensions/command-quotas}/sub-integration.ts +45 -23
- package/src/{providers → extensions/provider}/index.ts +4 -0
- package/src/{providers → extensions/provider}/models.test.ts +1 -1
- package/src/{providers → extensions/provider}/models.ts +1 -1
- package/src/extensions/web-search/index.ts +6 -0
- package/src/{tools/search.ts → extensions/web-search/tool.ts} +6 -3
- package/src/lib/env.ts +18 -0
- package/src/utils/quotas.ts +7 -6
- package/src/commands/quotas.ts +0 -68
- package/src/components/quotas-display.ts +0 -240
- package/src/components/quotas-error.ts +0 -32
- package/src/components/quotas-loading.ts +0 -30
- package/src/components/tabbed-panel.ts +0 -161
- package/src/hooks/search-tool-availability.ts +0 -104
- package/src/index.ts +0 -17
package/src/utils/quotas.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import type { QuotasResponse } from "../types/quotas";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (!
|
|
3
|
+
export async function fetchQuotas(
|
|
4
|
+
apiKey: string,
|
|
5
|
+
): Promise<QuotasResponse | null> {
|
|
6
|
+
if (!apiKey) return null;
|
|
7
7
|
|
|
8
8
|
try {
|
|
9
9
|
const response = await fetch("https://api.synthetic.new/v2/quotas", {
|
|
10
|
-
headers: { Authorization: `Bearer ${
|
|
10
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
if (!response.ok) return null;
|
|
14
|
-
|
|
14
|
+
const data: QuotasResponse = await response.json();
|
|
15
|
+
return data;
|
|
15
16
|
} catch {
|
|
16
17
|
return null;
|
|
17
18
|
}
|
package/src/commands/quotas.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
|
-
import type { Component } from "@mariozechner/pi-tui";
|
|
3
|
-
import { QuotasDisplayComponent } from "../components/quotas-display";
|
|
4
|
-
import { QuotasErrorComponent } from "../components/quotas-error";
|
|
5
|
-
import { QuotasLoadingComponent } from "../components/quotas-loading";
|
|
6
|
-
import { fetchQuotas } from "../utils/quotas";
|
|
7
|
-
|
|
8
|
-
export function registerQuotasCommand(pi: ExtensionAPI): void {
|
|
9
|
-
pi.registerCommand("synthetic:quotas", {
|
|
10
|
-
description: "Display Synthetic API usage quotas",
|
|
11
|
-
handler: async (_args, ctx) => {
|
|
12
|
-
const result = await ctx.ui.custom<null>((tui, theme, _kb, done) => {
|
|
13
|
-
let currentComponent: Component = new QuotasLoadingComponent(theme);
|
|
14
|
-
|
|
15
|
-
fetchQuotas()
|
|
16
|
-
.then((quotas) => {
|
|
17
|
-
if (!quotas) {
|
|
18
|
-
currentComponent = new QuotasErrorComponent(
|
|
19
|
-
theme,
|
|
20
|
-
"Failed to fetch quotas",
|
|
21
|
-
);
|
|
22
|
-
} else {
|
|
23
|
-
currentComponent = new QuotasDisplayComponent(
|
|
24
|
-
theme,
|
|
25
|
-
quotas,
|
|
26
|
-
() => {
|
|
27
|
-
done(null);
|
|
28
|
-
},
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
tui.requestRender();
|
|
32
|
-
})
|
|
33
|
-
.catch(() => {
|
|
34
|
-
currentComponent = new QuotasErrorComponent(
|
|
35
|
-
theme,
|
|
36
|
-
"Failed to fetch quotas",
|
|
37
|
-
);
|
|
38
|
-
tui.requestRender();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
return {
|
|
42
|
-
render: (width: number) => currentComponent.render(width),
|
|
43
|
-
invalidate: () => currentComponent.invalidate(),
|
|
44
|
-
handleInput: (data: string) => {
|
|
45
|
-
if (currentComponent.handleInput) {
|
|
46
|
-
return currentComponent.handleInput(data);
|
|
47
|
-
}
|
|
48
|
-
done(null);
|
|
49
|
-
return true;
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
// RPC fallback: return JSON
|
|
55
|
-
if (result === undefined) {
|
|
56
|
-
const quotas = await fetchQuotas();
|
|
57
|
-
if (!quotas) {
|
|
58
|
-
ctx.ui.notify(
|
|
59
|
-
JSON.stringify({ error: "Failed to fetch quotas" }),
|
|
60
|
-
"error",
|
|
61
|
-
);
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
ctx.ui.notify(JSON.stringify(quotas, null, 2), "info");
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
}
|
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import type { Theme } from "@mariozechner/pi-coding-agent";
|
|
2
|
-
import type { Component, TUI } from "@mariozechner/pi-tui";
|
|
3
|
-
import { matchesKey } from "@mariozechner/pi-tui";
|
|
4
|
-
import type { QuotasResponse } from "../types/quotas";
|
|
5
|
-
import { TabbedScrollablePanel } from "./tabbed-panel";
|
|
6
|
-
|
|
7
|
-
export class QuotasDisplayComponent implements Component {
|
|
8
|
-
private panel: TabbedScrollablePanel;
|
|
9
|
-
private onClose: () => void;
|
|
10
|
-
|
|
11
|
-
constructor(theme: Theme, quotas: QuotasResponse, onClose: () => void) {
|
|
12
|
-
this.onClose = onClose;
|
|
13
|
-
|
|
14
|
-
this.panel = new TabbedScrollablePanel(
|
|
15
|
-
{
|
|
16
|
-
title: "Synthetic API Quotas",
|
|
17
|
-
tabs: [
|
|
18
|
-
{
|
|
19
|
-
label: "Completions",
|
|
20
|
-
buildContent: () =>
|
|
21
|
-
buildHybridLayout(theme, quotas.subscription, 5), // 5-hour window
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
label: "Search",
|
|
25
|
-
buildContent: () =>
|
|
26
|
-
buildHybridLayout(theme, quotas.search.hourly, 1), // 1 hour
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
label: "Free tool call",
|
|
30
|
-
buildContent: () =>
|
|
31
|
-
buildToolCallsLayout(theme, quotas.freeToolCalls, 24), // 24 hours (daily)
|
|
32
|
-
},
|
|
33
|
-
],
|
|
34
|
-
onClose: onClose,
|
|
35
|
-
},
|
|
36
|
-
null as unknown as TUI,
|
|
37
|
-
theme,
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
handleInput(data: string): boolean {
|
|
42
|
-
if (matchesKey(data, "escape") || data === "q") {
|
|
43
|
-
this.onClose();
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
return this.panel.handleInput(data);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
render(width: number): string[] {
|
|
50
|
-
return this.panel.render(width);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
invalidate(): void {
|
|
54
|
-
this.panel.invalidate();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Layout: bar + pct/cur-total + pace/reset
|
|
59
|
-
function buildHybridLayout(
|
|
60
|
-
theme: Theme,
|
|
61
|
-
quota: { limit: number; requests: number; renewsAt: string },
|
|
62
|
-
periodHours: number,
|
|
63
|
-
): string[] {
|
|
64
|
-
const percentUsed = Math.round((quota.requests / quota.limit) * 100);
|
|
65
|
-
const renewsAt = new Date(quota.renewsAt);
|
|
66
|
-
const now = new Date();
|
|
67
|
-
|
|
68
|
-
const lines: string[] = [];
|
|
69
|
-
const barWidth = 50;
|
|
70
|
-
|
|
71
|
-
const usedStr = quota.requests.toLocaleString();
|
|
72
|
-
const limitStr = quota.limit.toLocaleString();
|
|
73
|
-
|
|
74
|
-
// Color based on usage
|
|
75
|
-
const usedColor =
|
|
76
|
-
percentUsed >= 100 ? "error" : percentUsed > 75 ? "warning" : "success";
|
|
77
|
-
|
|
78
|
-
// Calculate pace with known period
|
|
79
|
-
const totalPeriod = periodHours * 60 * 60 * 1000;
|
|
80
|
-
const timeUntilReset = Math.max(0, renewsAt.getTime() - now.getTime());
|
|
81
|
-
const timeElapsed = totalPeriod - timeUntilReset;
|
|
82
|
-
const percentTimeElapsed = (timeElapsed / totalPeriod) * 100;
|
|
83
|
-
const paceDiff = percentUsed - percentTimeElapsed;
|
|
84
|
-
|
|
85
|
-
let paceStr: string;
|
|
86
|
-
let paceColor: "success" | "dim" | "error";
|
|
87
|
-
if (paceDiff < -10) {
|
|
88
|
-
paceStr = `${Math.round(Math.abs(paceDiff))}% behind pace`;
|
|
89
|
-
paceColor = "success";
|
|
90
|
-
} else if (paceDiff > 10) {
|
|
91
|
-
paceStr = `${Math.round(paceDiff)}% ahead of pace`;
|
|
92
|
-
paceColor = "error";
|
|
93
|
-
} else {
|
|
94
|
-
paceStr = "within pace";
|
|
95
|
-
paceColor = "dim";
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Row above bar: pct left, cur/total right
|
|
99
|
-
const pctStr = `${percentUsed}% used`;
|
|
100
|
-
const totalDisplay = `${usedStr}/${limitStr}`;
|
|
101
|
-
const spacing = " ".repeat(
|
|
102
|
-
Math.max(1, barWidth - pctStr.length - totalDisplay.length),
|
|
103
|
-
);
|
|
104
|
-
lines.push(
|
|
105
|
-
` ${theme.fg(usedColor, pctStr)}${spacing}${theme.fg("dim", totalDisplay)}`,
|
|
106
|
-
);
|
|
107
|
-
|
|
108
|
-
// Bar
|
|
109
|
-
const usedWidth = Math.round((percentUsed / 100) * barWidth);
|
|
110
|
-
let bar: string;
|
|
111
|
-
if (usedWidth >= barWidth) {
|
|
112
|
-
bar = theme.fg("error", "█".repeat(barWidth));
|
|
113
|
-
} else if (percentUsed > 75) {
|
|
114
|
-
bar =
|
|
115
|
-
theme.fg("warning", "█".repeat(usedWidth)) +
|
|
116
|
-
theme.fg("dim", "█".repeat(barWidth - usedWidth));
|
|
117
|
-
} else {
|
|
118
|
-
bar =
|
|
119
|
-
theme.fg("success", "█".repeat(usedWidth)) +
|
|
120
|
-
theme.fg("dim", "█".repeat(barWidth - usedWidth));
|
|
121
|
-
}
|
|
122
|
-
lines.push(` ${bar}`);
|
|
123
|
-
|
|
124
|
-
// Row below bar: pace left, reset right
|
|
125
|
-
const resetStr = formatShortTime(renewsAt);
|
|
126
|
-
const paceSpacing = " ".repeat(
|
|
127
|
-
Math.max(1, barWidth - paceStr.length - resetStr.length),
|
|
128
|
-
);
|
|
129
|
-
lines.push(
|
|
130
|
-
` ${theme.fg(paceColor, paceStr)}${paceSpacing}${theme.fg("dim", resetStr)}`,
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
return lines;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Layout for free tool calls - shows remaining, not usage
|
|
137
|
-
function buildToolCallsLayout(
|
|
138
|
-
theme: Theme,
|
|
139
|
-
quota: { limit: number; requests: number; renewsAt: string },
|
|
140
|
-
periodHours: number,
|
|
141
|
-
): string[] {
|
|
142
|
-
const remaining = quota.limit - quota.requests;
|
|
143
|
-
const percentRemaining = Math.round((remaining / quota.limit) * 100);
|
|
144
|
-
const renewsAt = new Date(quota.renewsAt);
|
|
145
|
-
const now = new Date();
|
|
146
|
-
|
|
147
|
-
const lines: string[] = [];
|
|
148
|
-
const barWidth = 50;
|
|
149
|
-
|
|
150
|
-
const remainingStr = remaining.toLocaleString();
|
|
151
|
-
|
|
152
|
-
// Color based on remaining (inverse of usage)
|
|
153
|
-
const remainingColor =
|
|
154
|
-
remaining <= 0 ? "error" : percentRemaining < 25 ? "warning" : "success";
|
|
155
|
-
|
|
156
|
-
// Calculate pace (how fast you're consuming free calls)
|
|
157
|
-
const totalPeriod = periodHours * 60 * 60 * 1000;
|
|
158
|
-
const timeUntilReset = Math.max(0, renewsAt.getTime() - now.getTime());
|
|
159
|
-
const timeElapsed = totalPeriod - timeUntilReset;
|
|
160
|
-
const percentTimeElapsed = (timeElapsed / totalPeriod) * 100;
|
|
161
|
-
const expectedRemaining = Math.round(
|
|
162
|
-
quota.limit * (1 - percentTimeElapsed / 100),
|
|
163
|
-
);
|
|
164
|
-
const remainingDiff = remaining - expectedRemaining;
|
|
165
|
-
|
|
166
|
-
let paceStr: string;
|
|
167
|
-
let paceColor: "success" | "dim" | "error";
|
|
168
|
-
if (remainingDiff > quota.limit * 0.1) {
|
|
169
|
-
paceStr = `${remainingDiff.toLocaleString()} more than expected`;
|
|
170
|
-
paceColor = "success";
|
|
171
|
-
} else if (remainingDiff < -quota.limit * 0.1) {
|
|
172
|
-
paceStr = `${Math.abs(remainingDiff).toLocaleString()} fewer than expected`;
|
|
173
|
-
paceColor = "error";
|
|
174
|
-
} else {
|
|
175
|
-
paceStr = "on track";
|
|
176
|
-
paceColor = "dim";
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// Row above bar: pct remaining left, ratio right (like other tabs)
|
|
180
|
-
const pctStr = `${percentRemaining}% remaining`;
|
|
181
|
-
const ratioStr = `${remainingStr}/${quota.limit.toLocaleString()}`;
|
|
182
|
-
const spacing = " ".repeat(
|
|
183
|
-
Math.max(1, barWidth - pctStr.length - ratioStr.length),
|
|
184
|
-
);
|
|
185
|
-
lines.push(
|
|
186
|
-
` ${theme.fg(remainingColor, pctStr)}${spacing}${theme.fg("dim", ratioStr)}`,
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
// Bar (shows remaining, not used - so full bar = all remaining)
|
|
190
|
-
const remainingWidth = Math.round((percentRemaining / 100) * barWidth);
|
|
191
|
-
let bar: string;
|
|
192
|
-
if (remaining <= 0) {
|
|
193
|
-
bar = theme.fg("dim", "█".repeat(barWidth));
|
|
194
|
-
} else if (percentRemaining < 25) {
|
|
195
|
-
bar =
|
|
196
|
-
theme.fg("warning", "█".repeat(remainingWidth)) +
|
|
197
|
-
theme.fg("dim", "█".repeat(barWidth - remainingWidth));
|
|
198
|
-
} else {
|
|
199
|
-
bar =
|
|
200
|
-
theme.fg("success", "█".repeat(remainingWidth)) +
|
|
201
|
-
theme.fg("dim", "█".repeat(barWidth - remainingWidth));
|
|
202
|
-
}
|
|
203
|
-
lines.push(` ${bar}`);
|
|
204
|
-
|
|
205
|
-
// Row below bar: pace left, reset right
|
|
206
|
-
const resetStr = formatShortTime(renewsAt);
|
|
207
|
-
const paceSpacing = " ".repeat(
|
|
208
|
-
Math.max(1, barWidth - paceStr.length - resetStr.length),
|
|
209
|
-
);
|
|
210
|
-
lines.push(
|
|
211
|
-
` ${theme.fg(paceColor, paceStr)}${paceSpacing}${theme.fg("dim", resetStr)}`,
|
|
212
|
-
);
|
|
213
|
-
|
|
214
|
-
// If depleted, show note about paid calls
|
|
215
|
-
if (remaining <= 0) {
|
|
216
|
-
lines.push(` ${theme.fg("dim", "Additional calls will be charged")}`);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return lines;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function formatShortTime(date: Date): string {
|
|
223
|
-
const now = new Date();
|
|
224
|
-
const diffMs = date.getTime() - now.getTime();
|
|
225
|
-
|
|
226
|
-
if (diffMs <= 0) {
|
|
227
|
-
return "soon";
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
const diffHours = Math.ceil(diffMs / (1000 * 60 * 60));
|
|
231
|
-
const diffDays = Math.ceil(diffMs / (1000 * 60 * 60 * 24));
|
|
232
|
-
|
|
233
|
-
if (diffHours < 24) {
|
|
234
|
-
return `in ${diffHours}h`;
|
|
235
|
-
} else if (diffDays < 7) {
|
|
236
|
-
return `in ${diffDays}d`;
|
|
237
|
-
} else {
|
|
238
|
-
return date.toLocaleDateString("en-US", { month: "short", day: "numeric" });
|
|
239
|
-
}
|
|
240
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { Theme } from "@mariozechner/pi-coding-agent";
|
|
2
|
-
import { DynamicBorder } from "@mariozechner/pi-coding-agent";
|
|
3
|
-
import { type Component, Container, Text } from "@mariozechner/pi-tui";
|
|
4
|
-
|
|
5
|
-
export class QuotasErrorComponent implements Component {
|
|
6
|
-
private container: Container;
|
|
7
|
-
|
|
8
|
-
constructor(theme: Theme, message: string) {
|
|
9
|
-
this.container = new Container();
|
|
10
|
-
const border = new DynamicBorder((s: string) => theme.fg("accent", s));
|
|
11
|
-
|
|
12
|
-
this.container.addChild(border);
|
|
13
|
-
this.container.addChild(
|
|
14
|
-
new Text(theme.fg("accent", theme.bold(" Synthetic API Quotas ")), 1, 0),
|
|
15
|
-
);
|
|
16
|
-
this.container.addChild(new Text("", 0, 0));
|
|
17
|
-
this.container.addChild(new Text(theme.fg("error", ` ${message}`), 1, 0));
|
|
18
|
-
this.container.addChild(new Text("", 0, 0));
|
|
19
|
-
this.container.addChild(
|
|
20
|
-
new Text(theme.fg("dim", " Press any key to close"), 1, 0),
|
|
21
|
-
);
|
|
22
|
-
this.container.addChild(border);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
render(width: number): string[] {
|
|
26
|
-
return this.container.render(width);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
invalidate(): void {
|
|
30
|
-
this.container.invalidate();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { Theme } from "@mariozechner/pi-coding-agent";
|
|
2
|
-
import { DynamicBorder } from "@mariozechner/pi-coding-agent";
|
|
3
|
-
import { type Component, Container, Text } from "@mariozechner/pi-tui";
|
|
4
|
-
|
|
5
|
-
export class QuotasLoadingComponent implements Component {
|
|
6
|
-
private container: Container;
|
|
7
|
-
|
|
8
|
-
constructor(theme: Theme) {
|
|
9
|
-
this.container = new Container();
|
|
10
|
-
const border = new DynamicBorder((s: string) => theme.fg("accent", s));
|
|
11
|
-
|
|
12
|
-
this.container.addChild(border);
|
|
13
|
-
this.container.addChild(
|
|
14
|
-
new Text(theme.fg("accent", theme.bold(" Synthetic API Quotas ")), 1, 0),
|
|
15
|
-
);
|
|
16
|
-
this.container.addChild(new Text("", 0, 0));
|
|
17
|
-
this.container.addChild(
|
|
18
|
-
new Text(theme.fg("dim", " Loading quotas..."), 1, 0),
|
|
19
|
-
);
|
|
20
|
-
this.container.addChild(border);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
render(width: number): string[] {
|
|
24
|
-
return this.container.render(width);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
invalidate(): void {
|
|
28
|
-
this.container.invalidate();
|
|
29
|
-
}
|
|
30
|
-
}
|
|
@@ -1,161 +0,0 @@
|
|
|
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 { matchesKey, truncateToWidth } from "@mariozechner/pi-tui";
|
|
5
|
-
|
|
6
|
-
export interface PanelTab {
|
|
7
|
-
label: string;
|
|
8
|
-
buildContent: () => string[];
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface TabbedScrollablePanelOptions {
|
|
12
|
-
title: string;
|
|
13
|
-
tabs: PanelTab[];
|
|
14
|
-
onClose: () => void;
|
|
15
|
-
maxVisible?: number;
|
|
16
|
-
keymap?: "vim" | "default";
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class TabbedScrollablePanel implements Component {
|
|
20
|
-
private activeTab = 0;
|
|
21
|
-
private scrollOffset = 0;
|
|
22
|
-
private cachedLines: string[] | null = null;
|
|
23
|
-
private cachedWidth = 0;
|
|
24
|
-
private border: DynamicBorder;
|
|
25
|
-
private options: TabbedScrollablePanelOptions;
|
|
26
|
-
private theme: Theme;
|
|
27
|
-
|
|
28
|
-
constructor(options: TabbedScrollablePanelOptions, _tui: TUI, theme: Theme) {
|
|
29
|
-
this.options = options;
|
|
30
|
-
this.theme = theme;
|
|
31
|
-
this.border = new DynamicBorder((segment) => theme.fg("border", segment));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
handleInput(data: string): boolean {
|
|
35
|
-
if (matchesKey(data, "escape") || data === "q") {
|
|
36
|
-
this.options.onClose();
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (matchesKey(data, "tab")) {
|
|
41
|
-
this.activeTab = (this.activeTab + 1) % this.options.tabs.length;
|
|
42
|
-
this.scrollOffset = 0;
|
|
43
|
-
this.invalidate();
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (matchesKey(data, "shift+tab")) {
|
|
48
|
-
this.activeTab =
|
|
49
|
-
(this.activeTab - 1 + this.options.tabs.length) %
|
|
50
|
-
this.options.tabs.length;
|
|
51
|
-
this.scrollOffset = 0;
|
|
52
|
-
this.invalidate();
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const maxVisible = this.options.maxVisible ?? 16;
|
|
57
|
-
const totalLines = this.cachedLines?.length ?? 0;
|
|
58
|
-
const maxScroll = Math.max(0, totalLines - maxVisible);
|
|
59
|
-
|
|
60
|
-
if (data === "j" || matchesKey(data, "down")) {
|
|
61
|
-
if (this.scrollOffset < maxScroll) {
|
|
62
|
-
this.scrollOffset++;
|
|
63
|
-
}
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (data === "k" || matchesKey(data, "up")) {
|
|
68
|
-
if (this.scrollOffset > 0) {
|
|
69
|
-
this.scrollOffset--;
|
|
70
|
-
}
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (data === " " || matchesKey(data, "pageDown")) {
|
|
75
|
-
this.scrollOffset = Math.min(this.scrollOffset + maxVisible, maxScroll);
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (matchesKey(data, "pageUp")) {
|
|
80
|
-
this.scrollOffset = Math.max(0, this.scrollOffset - maxVisible);
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (matchesKey(data, "home")) {
|
|
85
|
-
this.scrollOffset = 0;
|
|
86
|
-
return true;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (matchesKey(data, "end")) {
|
|
90
|
-
this.scrollOffset = maxScroll;
|
|
91
|
-
return true;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
invalidate(): void {
|
|
98
|
-
this.cachedLines = null;
|
|
99
|
-
this.cachedWidth = 0;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
render(width: number): string[] {
|
|
103
|
-
const tab = this.options.tabs[this.activeTab];
|
|
104
|
-
|
|
105
|
-
if (!this.cachedLines || this.cachedWidth !== width) {
|
|
106
|
-
this.cachedLines = tab ? tab.buildContent() : [];
|
|
107
|
-
this.cachedWidth = width;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const lines: string[] = [];
|
|
111
|
-
|
|
112
|
-
lines.push(...this.border.render(width));
|
|
113
|
-
lines.push(
|
|
114
|
-
truncateToWidth(
|
|
115
|
-
` ${this.theme.fg("accent", this.theme.bold(this.options.title))}`,
|
|
116
|
-
width,
|
|
117
|
-
),
|
|
118
|
-
);
|
|
119
|
-
lines.push(this.renderTabBar(width));
|
|
120
|
-
lines.push("");
|
|
121
|
-
|
|
122
|
-
// Content - no forced padding, just render what we have
|
|
123
|
-
for (const line of this.cachedLines) {
|
|
124
|
-
lines.push(truncateToWidth(` ${line}`, width));
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Footer directly after content
|
|
128
|
-
lines.push("");
|
|
129
|
-
lines.push(
|
|
130
|
-
truncateToWidth(
|
|
131
|
-
this.theme.fg("dim", " Tab/S-Tab switch tabs q/Esc close"),
|
|
132
|
-
width,
|
|
133
|
-
),
|
|
134
|
-
);
|
|
135
|
-
lines.push(...this.border.render(width));
|
|
136
|
-
|
|
137
|
-
return lines;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private renderTabBar(width: number): string {
|
|
141
|
-
const parts: string[] = [];
|
|
142
|
-
|
|
143
|
-
for (let i = 0; i < this.options.tabs.length; i++) {
|
|
144
|
-
const tab = this.options.tabs[i];
|
|
145
|
-
if (!tab) continue;
|
|
146
|
-
const active = i === this.activeTab;
|
|
147
|
-
|
|
148
|
-
if (active) {
|
|
149
|
-
parts.push(this.theme.fg("accent", this.theme.bold(` ${tab.label} `)));
|
|
150
|
-
} else {
|
|
151
|
-
parts.push(this.theme.fg("dim", ` ${tab.label} `));
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (i < this.options.tabs.length - 1) {
|
|
155
|
-
parts.push(this.theme.fg("borderMuted", "│"));
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return truncateToWidth(` ${parts.join("")}`, width);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
|
-
import { SYNTHETIC_WEB_SEARCH_TOOL } from "../tools/search";
|
|
3
|
-
|
|
4
|
-
async function checkSubscriptionAccess(
|
|
5
|
-
apiKey: string,
|
|
6
|
-
): Promise<{ ok: true } | { ok: false; reason: string }> {
|
|
7
|
-
try {
|
|
8
|
-
const response = await fetch("https://api.synthetic.new/v2/quotas", {
|
|
9
|
-
method: "GET",
|
|
10
|
-
headers: {
|
|
11
|
-
Authorization: `Bearer ${apiKey}`,
|
|
12
|
-
},
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
if (!response.ok) {
|
|
16
|
-
return {
|
|
17
|
-
ok: false,
|
|
18
|
-
reason: `Quotas check failed (HTTP ${response.status})`,
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const data = await response.json();
|
|
23
|
-
if (data?.subscription?.limit > 0) {
|
|
24
|
-
return { ok: true };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return {
|
|
28
|
-
ok: false,
|
|
29
|
-
reason: "No active subscription (search requires a subscription plan)",
|
|
30
|
-
};
|
|
31
|
-
} catch (error) {
|
|
32
|
-
const message =
|
|
33
|
-
error instanceof Error ? error.message : "Unknown error occurred";
|
|
34
|
-
return { ok: false, reason: `Quotas check failed: ${message}` };
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function registerSyntheticWebSearchHooks(pi: ExtensionAPI): void {
|
|
39
|
-
let accessCheckPromise:
|
|
40
|
-
| Promise<{ ok: true } | { ok: false; reason: string }>
|
|
41
|
-
| undefined;
|
|
42
|
-
let hasAccess = false;
|
|
43
|
-
let deniedReason: string | undefined;
|
|
44
|
-
let didNotifyDenied = false;
|
|
45
|
-
|
|
46
|
-
// Keep tool inactive at session start. Availability is decided before each agent run.
|
|
47
|
-
pi.on("session_start", () => {
|
|
48
|
-
const current = pi.getActiveTools();
|
|
49
|
-
if (current.includes(SYNTHETIC_WEB_SEARCH_TOOL)) {
|
|
50
|
-
pi.setActiveTools(
|
|
51
|
-
current.filter((toolName) => toolName !== SYNTHETIC_WEB_SEARCH_TOOL),
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
// Verify subscription only when user starts agent execution.
|
|
57
|
-
pi.on("before_agent_start", async (_event, ctx) => {
|
|
58
|
-
const apiKey = process.env.SYNTHETIC_API_KEY;
|
|
59
|
-
if (!apiKey) {
|
|
60
|
-
hasAccess = false;
|
|
61
|
-
deniedReason = "SYNTHETIC_API_KEY is not configured";
|
|
62
|
-
accessCheckPromise = undefined;
|
|
63
|
-
} else {
|
|
64
|
-
if (deniedReason === "SYNTHETIC_API_KEY is not configured") {
|
|
65
|
-
deniedReason = undefined;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (!hasAccess && !deniedReason) {
|
|
69
|
-
accessCheckPromise ??= checkSubscriptionAccess(apiKey);
|
|
70
|
-
const access = await accessCheckPromise;
|
|
71
|
-
|
|
72
|
-
if (!access.ok) {
|
|
73
|
-
deniedReason = access.reason;
|
|
74
|
-
} else {
|
|
75
|
-
hasAccess = true;
|
|
76
|
-
didNotifyDenied = false;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (deniedReason) {
|
|
82
|
-
const current = pi.getActiveTools();
|
|
83
|
-
if (current.includes(SYNTHETIC_WEB_SEARCH_TOOL)) {
|
|
84
|
-
pi.setActiveTools(
|
|
85
|
-
current.filter((toolName) => toolName !== SYNTHETIC_WEB_SEARCH_TOOL),
|
|
86
|
-
);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (ctx.hasUI && !didNotifyDenied) {
|
|
90
|
-
ctx.ui.notify(
|
|
91
|
-
`Synthetic web search disabled: ${deniedReason}`,
|
|
92
|
-
"warning",
|
|
93
|
-
);
|
|
94
|
-
didNotifyDenied = true;
|
|
95
|
-
}
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const current = pi.getActiveTools();
|
|
100
|
-
if (!current.includes(SYNTHETIC_WEB_SEARCH_TOOL)) {
|
|
101
|
-
pi.setActiveTools([...current, SYNTHETIC_WEB_SEARCH_TOOL]);
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
|
-
import { registerQuotasCommand } from "./commands/quotas";
|
|
3
|
-
import { registerSyntheticWebSearchHooks } from "./hooks/search-tool-availability";
|
|
4
|
-
import { registerSubIntegration } from "./hooks/sub-integration";
|
|
5
|
-
import { registerSyntheticProvider } from "./providers/index";
|
|
6
|
-
import { registerSyntheticWebSearchTool } from "./tools/search";
|
|
7
|
-
|
|
8
|
-
export default async function (pi: ExtensionAPI) {
|
|
9
|
-
registerSyntheticProvider(pi);
|
|
10
|
-
registerSyntheticWebSearchTool(pi);
|
|
11
|
-
registerSyntheticWebSearchHooks(pi);
|
|
12
|
-
|
|
13
|
-
if (process.env.SYNTHETIC_API_KEY) {
|
|
14
|
-
registerQuotasCommand(pi);
|
|
15
|
-
registerSubIntegration(pi);
|
|
16
|
-
}
|
|
17
|
-
}
|