@prplkane/roundtable 0.6.6 → 0.7.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/CHANGELOG.md +604 -565
- package/LICENSE +21 -21
- package/README.md +184 -195
- package/SECURITY.md +324 -318
- package/docs/workflow.md +1475 -1439
- package/package.json +2 -2
- package/scripts/agent.mjs +2 -2
- package/scripts/mcp.mjs +179 -177
- package/skills/roundtable/SKILL.md +62 -59
- package/src/adapters/acp.js +318 -201
- package/src/adapters/agy.js +7 -7
- package/src/adapters/catalogue.js +37 -18
- package/src/adapters/claude.js +6 -6
- package/src/adapters/codex.js +6 -6
- package/src/adapters/common.js +1 -1
- package/src/adapters/config.js +8 -9
- package/src/adapters/models.js +32 -19
- package/src/adapters/spawn.js +3 -1
- package/src/agent-client.js +131 -120
- package/src/cli.js +21 -3
- package/src/diagnostics.js +1 -0
- package/src/engine.js +9 -5
- package/src/export.js +307 -307
- package/src/local-http.js +2 -2
- package/src/local-server.js +110 -110
- package/src/preflight.js +26 -6
- package/src/review-view.js +11 -0
- package/src/seats.js +368 -287
- package/src/server.js +1010 -945
- package/src/ui/app.js +2142 -1977
- package/src/ui/design/app.css +2 -1
- package/src/ui/design/preset-dusk.css +91 -91
- package/src/ui/design/tokens.css +129 -129
- package/src/ui/index.html +276 -272
- package/src/validate.js +10 -3
package/src/seats.js
CHANGED
|
@@ -1,287 +1,368 @@
|
|
|
1
|
-
// Seats are instances: one vendor CLI, one session, one lens, one mood, one model, one effort.
|
|
2
|
-
// This module turns what the setup screen posts into the seat list the engine runs, and
|
|
3
|
-
// rejects what would break anonymity or the CLIs. Pure, so it is unit tested.
|
|
4
|
-
|
|
5
|
-
export const MOODS = {
|
|
6
|
-
grill: 'Your default posture is sceptical: attack assumptions, demand evidence, and do not agree until the case is made.',
|
|
7
|
-
steelman: 'Before judging an option, state its strongest version; then say where even that version fails.',
|
|
8
|
-
builder: 'Judge by what it takes to ship: effort, sequencing, what breaks first.',
|
|
9
|
-
contrarian: 'Take the stance the other seats are least likely to take, and defend it with evidence.',
|
|
10
|
-
};
|
|
11
|
-
export const DEFAULT_MOOD = 'grill';
|
|
12
|
-
|
|
13
|
-
// A custom mood matching a persona code uses its prompt, avatar and light model settings.
|
|
14
|
-
// The server opts board requests into these shortcuts; ordinary API requests keep custom text.
|
|
15
|
-
import { HIDDEN_MOODS } from './personas.js';
|
|
16
|
-
import { modelEfforts } from './adapters/models.js';
|
|
17
|
-
export { HIDDEN_MOODS } from './personas.js';
|
|
18
|
-
export function hiddenMood(text) {
|
|
19
|
-
const word = String(text || '').trim().toLowerCase();
|
|
20
|
-
return word ? HIDDEN_MOODS.find((h) => h.codes.includes(word)) || null : null;
|
|
21
|
-
}
|
|
22
|
-
// Remove complete parentheticals in one pass, preserving unmatched openings. A regex that
|
|
23
|
-
// searches again after every unmatched opening or whitespace character can stall the server.
|
|
24
|
-
export function filterReply(mood, text) {
|
|
25
|
-
const h = mood?.hidden && HIDDEN_MOODS.find((x) => x.art === mood.hidden);
|
|
26
|
-
if (!h?.stripParentheticals) return text;
|
|
27
|
-
const body = String(text), parts = [];
|
|
28
|
-
let from = 0, search = 0;
|
|
29
|
-
while (search < body.length) {
|
|
30
|
-
const open = body.indexOf('(', search);
|
|
31
|
-
if (open < 0) break;
|
|
32
|
-
const close = body.indexOf(')', open + 1);
|
|
33
|
-
if (close < 0) break;
|
|
34
|
-
let start = open;
|
|
35
|
-
while (start > from && /\s/.test(body[start - 1])) start--;
|
|
36
|
-
parts.push(body.slice(from, start));
|
|
37
|
-
from = search = close + 1;
|
|
38
|
-
}
|
|
39
|
-
return parts.join('') + body.slice(from);
|
|
40
|
-
}
|
|
41
|
-
// The cheapest model each CLI offers (adapters/models.js marks claude's and codex's lightest).
|
|
42
|
-
// agy: Flash-low over gpt-oss-120b-medium, measured on a hidden-mood prompt: the same input,
|
|
43
|
-
// but gpt-oss wrote 1.7k-4k output tokens to Flash-low's ~190, and took two to four times longer.
|
|
44
|
-
export const LIGHTEST = { claude:
|
|
45
|
-
// agy's list depends on the account (`agy models`): a Flash-Lite model, where offered, is lighter
|
|
46
|
-
// than Flash, and a family's "-low" level is its lightest. LIGHTEST.agy when the list has neither.
|
|
47
|
-
export function lightestModel(vendor, models = []) {
|
|
48
|
-
if (vendor
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return name;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return name;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
//
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (
|
|
194
|
-
if (
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
//
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
if (
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
1
|
+
// Seats are instances: one vendor CLI, one session, one lens, one mood, one model, one effort.
|
|
2
|
+
// This module turns what the setup screen posts into the seat list the engine runs, and
|
|
3
|
+
// rejects what would break anonymity or the CLIs. Pure, so it is unit tested.
|
|
4
|
+
|
|
5
|
+
export const MOODS = {
|
|
6
|
+
grill: 'Your default posture is sceptical: attack assumptions, demand evidence, and do not agree until the case is made.',
|
|
7
|
+
steelman: 'Before judging an option, state its strongest version; then say where even that version fails.',
|
|
8
|
+
builder: 'Judge by what it takes to ship: effort, sequencing, what breaks first.',
|
|
9
|
+
contrarian: 'Take the stance the other seats are least likely to take, and defend it with evidence.',
|
|
10
|
+
};
|
|
11
|
+
export const DEFAULT_MOOD = 'grill';
|
|
12
|
+
|
|
13
|
+
// A custom mood matching a persona code uses its prompt, avatar and light model settings.
|
|
14
|
+
// The server opts board requests into these shortcuts; ordinary API requests keep custom text.
|
|
15
|
+
import { HIDDEN_MOODS } from './personas.js';
|
|
16
|
+
import { modelEfforts, MODELS, effortSuffix } from './adapters/models.js';
|
|
17
|
+
export { HIDDEN_MOODS } from './personas.js';
|
|
18
|
+
export function hiddenMood(text) {
|
|
19
|
+
const word = String(text || '').trim().toLowerCase();
|
|
20
|
+
return word ? HIDDEN_MOODS.find((h) => h.codes.includes(word)) || null : null;
|
|
21
|
+
}
|
|
22
|
+
// Remove complete parentheticals in one pass, preserving unmatched openings. A regex that
|
|
23
|
+
// searches again after every unmatched opening or whitespace character can stall the server.
|
|
24
|
+
export function filterReply(mood, text) {
|
|
25
|
+
const h = mood?.hidden && HIDDEN_MOODS.find((x) => x.art === mood.hidden);
|
|
26
|
+
if (!h?.stripParentheticals) return text;
|
|
27
|
+
const body = String(text), parts = [];
|
|
28
|
+
let from = 0, search = 0;
|
|
29
|
+
while (search < body.length) {
|
|
30
|
+
const open = body.indexOf('(', search);
|
|
31
|
+
if (open < 0) break;
|
|
32
|
+
const close = body.indexOf(')', open + 1);
|
|
33
|
+
if (close < 0) break;
|
|
34
|
+
let start = open;
|
|
35
|
+
while (start > from && /\s/.test(body[start - 1])) start--;
|
|
36
|
+
parts.push(body.slice(from, start));
|
|
37
|
+
from = search = close + 1;
|
|
38
|
+
}
|
|
39
|
+
return parts.join('') + body.slice(from);
|
|
40
|
+
}
|
|
41
|
+
// The cheapest model each CLI offers (adapters/models.js marks claude's and codex's lightest).
|
|
42
|
+
// agy: Flash-low over gpt-oss-120b-medium, measured on a hidden-mood prompt: the same input,
|
|
43
|
+
// but gpt-oss wrote 1.7k-4k output tokens to Flash-low's ~190, and took two to four times longer.
|
|
44
|
+
export const LIGHTEST = { claude: MODELS.claude.find((m) => m.note === 'lightest').id, codex: MODELS.codex.find((m) => m.note === 'lightest').id, agy: 'gemini-3.8-flash-low' };
|
|
45
|
+
// agy's list depends on the account (`agy models`): a Flash-Lite model, where offered, is lighter
|
|
46
|
+
// than Flash, and a family's "-low" level is its lightest. LIGHTEST.agy when the list has neither.
|
|
47
|
+
export function lightestModel(vendor, models = []) {
|
|
48
|
+
if (vendor === 'codex' && models.length) return models.find((m) => m.note === 'lightest')?.id;
|
|
49
|
+
if (vendor !== 'agy') return LIGHTEST[vendor];
|
|
50
|
+
const rank = (id) => (/flash-lite/.test(id) ? 0 : 3) + (/-low$/.test(id) ? 0 : /-medium$/.test(id) ? 1 : 2);
|
|
51
|
+
const ids = models.map((m) => m?.id || m).filter((id) => /^gemini-.*flash/.test(String(id)));
|
|
52
|
+
return ids.sort((a, b) => rank(a) - rank(b))[0] || (models.length ? undefined : LIGHTEST.agy);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const STARTER_LENSES = ['risk and failure modes', 'cost and effort', 'evidence and assumptions',
|
|
56
|
+
'long-term effects', 'practical steps', 'alternatives', 'people affected', 'opportunities'];
|
|
57
|
+
export function nextSeatLens(seats) {
|
|
58
|
+
const used = new Set(seats.map((s) => String(s?.lens || '').trim().toLowerCase()));
|
|
59
|
+
const starter = STARTER_LENSES.find((lens) => !used.has(lens));
|
|
60
|
+
if (starter) return starter;
|
|
61
|
+
let n = seats.length + 1;
|
|
62
|
+
while (used.has(`perspective ${n}`)) n++;
|
|
63
|
+
return `perspective ${n}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function fillMissingSeatLenses(seats) {
|
|
67
|
+
const out = seats.map((s) => ({ ...s }));
|
|
68
|
+
for (const seat of out) if (!String(seat.lens || '').trim()) seat.lens = nextSeatLens(out);
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Shared by the form and API, with field coordinates rather than private input in errors.
|
|
73
|
+
export function seatSetupIssue(seats) {
|
|
74
|
+
const seen = new Map();
|
|
75
|
+
for (const [seatIndex, seat] of seats.entries()) {
|
|
76
|
+
if (!seat) continue;
|
|
77
|
+
const lens = String(seat.lens || '').trim().toLowerCase();
|
|
78
|
+
if (!lens) return { seatIndex, field: 'lens', code: 'SEAT_LENS_REQUIRED',
|
|
79
|
+
message: `Seat ${seatIndex + 1} needs a lens. Give it a perspective, such as risk, evidence or long-term effects.` };
|
|
80
|
+
if (seen.has(lens)) return { seatIndex, field: 'lens', code: 'SEAT_LENS_DUPLICATE',
|
|
81
|
+
message: `Seats ${seen.get(lens) + 1} and ${seatIndex + 1} share the lens. Give each seat a different perspective.` };
|
|
82
|
+
seen.set(lens, seatIndex);
|
|
83
|
+
const preset = typeof seat.mood === 'string' ? seat.mood : seat.mood?.preset;
|
|
84
|
+
const text = typeof seat.mood === 'object' ? seat.mood?.text : seat.moodText;
|
|
85
|
+
if (preset === 'custom' && !String(text || '').trim()) return { seatIndex, field: 'moodText', code: 'SEAT_MOOD_REQUIRED',
|
|
86
|
+
message: `Seat ${seatIndex + 1} needs a custom mood sentence. Describe how it should argue, or choose a preset.` };
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Seat colours from the signature-design accent family, cycled when a table has more seats.
|
|
92
|
+
export const COLOURS = ['rust', 'olive', 'teal', 'dusk', 'steel'];
|
|
93
|
+
|
|
94
|
+
// A profile name is a directory name under the roundtable data dir, so it is kept plain.
|
|
95
|
+
// (Here rather than in adapters/spawn.js because this module also runs in the browser.)
|
|
96
|
+
export const PROFILE_RE = /^[a-z0-9][a-z0-9_.-]{0,39}$/i;
|
|
97
|
+
|
|
98
|
+
// The quota pool a seat draws from, as a key: the vendor's own login is `codex`; a named profile
|
|
99
|
+
// with its own login is `codex@work`. A vendor whose CLI has no separate state directory
|
|
100
|
+
// (`profiles` false: agy) has one pool whatever the seat says. Quota snapshots, calibration and
|
|
101
|
+
// the board's pool rows are all keyed by it.
|
|
102
|
+
export function poolKey(vendor, account, profiles = true) {
|
|
103
|
+
return account && profiles ? `${vendor}@${account}` : vendor;
|
|
104
|
+
}
|
|
105
|
+
export const poolVendor = (key) => String(key).split('@')[0].split(':')[0];
|
|
106
|
+
export const poolAccount = (key) => String(key).split(':')[0].split('@')[1] || null;
|
|
107
|
+
|
|
108
|
+
// An optional profile name: the seat then runs with its own CLI state directory under the OS
|
|
109
|
+
// user-data dir (adapters/spawn.js profileHome), which the user logged in themselves. Empty
|
|
110
|
+
// means the vendor's real state directory, shared with the terminal.
|
|
111
|
+
export function normaliseAccount(raw) {
|
|
112
|
+
const name = String(raw || '').trim();
|
|
113
|
+
if (!name) return undefined;
|
|
114
|
+
if (!PROFILE_RE.test(name)) throw new Error(`Account profile "${name}" must be 1-40 letters, digits, dots, dashes or underscores.`);
|
|
115
|
+
return name;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function agentLinkProfile(raw) {
|
|
119
|
+
const name = typeof raw === 'string' ? raw.trim() : '';
|
|
120
|
+
if (!/^[a-z0-9][a-z0-9_-]{0,63}$/.test(name) || /^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/.test(name)) {
|
|
121
|
+
throw new Error('An ACP seat needs a valid saved AgentLink profile: 1-64 lowercase letters, digits, underscores or hyphens; no reserved device names.');
|
|
122
|
+
}
|
|
123
|
+
return name;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Provider shortcuts use AgentLink's conventional profile names. Custom profiles are always
|
|
127
|
+
// selectable by their exact name, including multiple accounts for the same provider.
|
|
128
|
+
export const PROFILE_PROVIDERS = { codex: 'codex', claude: 'claude', agy: 'antigravity' };
|
|
129
|
+
export function providerProfile(vendor, profiles) {
|
|
130
|
+
const matches = profiles.filter((p) => p.provider === PROFILE_PROVIDERS[vendor]);
|
|
131
|
+
return matches.find((p) => p.name === vendor) || (matches.length === 1 ? matches[0] : null);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Conventional provider choices come first. A missing profile is offered for setup only
|
|
135
|
+
// if its name is free. A profile named "codex" can legitimately belong to another provider.
|
|
136
|
+
export function profileChoicesFor(profiles) {
|
|
137
|
+
const choices = [];
|
|
138
|
+
for (const vendor of Object.keys(PROFILE_PROVIDERS)) {
|
|
139
|
+
const selected = providerProfile(vendor, profiles);
|
|
140
|
+
if (selected) choices.push({ name: selected.name, vendor, configured: true });
|
|
141
|
+
else if (!profiles.some((p) => p.name === vendor || p.provider === PROFILE_PROVIDERS[vendor])) {
|
|
142
|
+
choices.push({ name: vendor, vendor, configured: false });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
for (const p of profiles) {
|
|
146
|
+
if (!choices.some((c) => c.name === p.name)) choices.push({ name: p.name, configured: true });
|
|
147
|
+
}
|
|
148
|
+
return choices;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function routeReviewProfiles(raw, profiles) {
|
|
152
|
+
if (!raw || typeof raw !== 'object') return raw;
|
|
153
|
+
const routed = new Map();
|
|
154
|
+
function card(seat) {
|
|
155
|
+
if (!seat || typeof seat !== 'object') return seat;
|
|
156
|
+
if (seat.transport != null && !['cli', 'agentlink'].includes(seat.transport)) throw new Error('Seat transport must be cli or agentlink.');
|
|
157
|
+
if (!Object.hasOwn(PROFILE_PROVIDERS, seat.vendor)) {
|
|
158
|
+
if (seat.transport != null) throw new Error('Set transport only on a named CLI provider.');
|
|
159
|
+
return seat;
|
|
160
|
+
}
|
|
161
|
+
if (seat.transport === 'cli') return seat;
|
|
162
|
+
const name = seat.account ? agentLinkProfile(seat.account) : providerProfile(seat.vendor, profiles)?.name;
|
|
163
|
+
if (!name || !profiles.some((p) => p.name === name && (!p.provider || p.provider === PROFILE_PROVIDERS[seat.vendor]))) {
|
|
164
|
+
throw new Error(`Choose a saved AgentLink profile for ${seat.vendor}, or create one with agentlink add ${seat.vendor} --provider ${PROFILE_PROVIDERS[seat.vendor]}. For a direct CLI seat set transport to "cli".`);
|
|
165
|
+
}
|
|
166
|
+
const names = routed.get(seat.vendor) || new Set();
|
|
167
|
+
names.add(name); routed.set(seat.vendor, names);
|
|
168
|
+
return { ...seat, vendor: 'acp', account: name };
|
|
169
|
+
}
|
|
170
|
+
const seats = Array.isArray(raw.seats) ? raw.seats.map(card) : raw.seats;
|
|
171
|
+
const chairman = card(raw.chairman);
|
|
172
|
+
const options = raw.options;
|
|
173
|
+
if (!options?.budget || typeof options.budget !== 'object' || Array.isArray(options.budget)) return { ...raw, seats, chairman };
|
|
174
|
+
const budget = { ...options.budget };
|
|
175
|
+
for (const [vendor, names] of routed) {
|
|
176
|
+
if (!Object.hasOwn(budget, vendor)) continue;
|
|
177
|
+
const direct = [...(Array.isArray(seats) ? seats : []), chairman].some((s) => s?.vendor === vendor)
|
|
178
|
+
|| (options.research && (options.research.vendor || 'codex') === vendor);
|
|
179
|
+
if (direct && [...names].every((name) => Object.hasOwn(budget, 'acp@' + name))) continue;
|
|
180
|
+
if (direct || names.size !== 1) {
|
|
181
|
+
throw new Error(`Budget for ${vendor} covers different connections. Set separate caps using acp@PROFILE for each AgentLink profile.`);
|
|
182
|
+
}
|
|
183
|
+
const key = 'acp@' + [...names][0];
|
|
184
|
+
if (Object.hasOwn(budget, key)) throw new Error(`Duplicate budget for ${key}.`);
|
|
185
|
+
budget[key] = budget[vendor]; delete budget[vendor];
|
|
186
|
+
}
|
|
187
|
+
return { ...raw, seats, chairman, options: { ...options, budget } };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// The one line a seat sees about how to approach the question. Custom text wins when given.
|
|
191
|
+
export function moodLine(mood) {
|
|
192
|
+
if (!mood) return MOODS[DEFAULT_MOOD];
|
|
193
|
+
if (typeof mood === 'string') return MOODS[mood] || MOODS[DEFAULT_MOOD];
|
|
194
|
+
if (mood.hidden) { const h = HIDDEN_MOODS.find((x) => x.art === mood.hidden); if (h) return h.text; }
|
|
195
|
+
if (mood.preset === 'custom' && mood.text) return String(mood.text).trim();
|
|
196
|
+
return MOODS[mood.preset] || MOODS[DEFAULT_MOOD];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// hidden: whether this request may unlock a hidden mood (only the board's own requests may).
|
|
200
|
+
function normaliseMood(m, { hidden = false } = {}) {
|
|
201
|
+
if (!m) return { preset: DEFAULT_MOOD };
|
|
202
|
+
if (typeof m === 'string') return MOODS[m] ? { preset: m } : { preset: DEFAULT_MOOD };
|
|
203
|
+
if (m.preset === 'custom') {
|
|
204
|
+
const text = String(m.text || '').trim();
|
|
205
|
+
if (!text) throw new Error('A custom mood needs its sentence.');
|
|
206
|
+
const h = hidden ? hiddenMood(text) : null;
|
|
207
|
+
return { preset: 'custom', text: text.slice(0, 400), ...(h ? { hidden: h.art } : {}) };
|
|
208
|
+
}
|
|
209
|
+
return { preset: MOODS[m.preset] ? m.preset : DEFAULT_MOOD };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ---------- API endpoint seats ----------
|
|
213
|
+
// A seat on an OpenAI-compatible endpoint names a base URL, a model and, for a hosted API, the
|
|
214
|
+
// NAME of the environment variable that holds the key (never the key). These rules are shared
|
|
215
|
+
// with the browser, so the card can say the same thing the server will.
|
|
216
|
+
|
|
217
|
+
export const KEY_ENV_RE = /^[A-Z_][A-Z0-9_]{0,63}$/; // an identifier: a pasted key is refused on shape alone
|
|
218
|
+
// Browser preferences must never retain an invalid value while the form reports its error.
|
|
219
|
+
export function keyEnvName(value) {
|
|
220
|
+
const name = typeof value === 'string' ? value.trim() : '';
|
|
221
|
+
return KEY_ENV_RE.test(name) ? name : '';
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// On this machine or on a private network: loopback, link-local, RFC 1918, ULA, a bare LAN
|
|
225
|
+
// name, or the local-only suffixes. Plain http is allowed only here, so a key never crosses the
|
|
226
|
+
// open internet in clear.
|
|
227
|
+
export function privateHost(hostname) {
|
|
228
|
+
const h = String(hostname || '').toLowerCase().replace(/^\[|\]$/g, '');
|
|
229
|
+
if (h === 'localhost' || h === '::1' || h === '0.0.0.0' || h === '::') return true;
|
|
230
|
+
if (/\.(localhost|local|test|internal|lan|home|home\.arpa)$/.test(h)) return true;
|
|
231
|
+
if (!h.includes('.') && !h.includes(':')) return true; // a bare LAN hostname
|
|
232
|
+
// URL callers already canonicalize alternate IPv4 spellings. Check the whole address:
|
|
233
|
+
// a DNS name such as 10.api.example.com must not inherit a private address's HTTP policy.
|
|
234
|
+
const ipv4 = /^(?:\d{1,3}\.){3}\d{1,3}$/.test(h) ? h.split('.').map(Number) : null;
|
|
235
|
+
if (ipv4 && ipv4.every((n) => n <= 255)) {
|
|
236
|
+
const [a, b] = ipv4;
|
|
237
|
+
return a === 127 || a === 10 || (a === 192 && b === 168) || (a === 169 && b === 254) || (a === 172 && b >= 16 && b <= 31);
|
|
238
|
+
}
|
|
239
|
+
if (/^(fc|fd)[0-9a-f]{2}:/.test(h) || /^fe80:/.test(h)) return true;
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// The base URL as the adapter will use it ("…/v1", no trailing slash), or a thrown reason.
|
|
244
|
+
export function checkEndpointUrl(raw) {
|
|
245
|
+
const text = String(raw || '').trim();
|
|
246
|
+
if (!text) throw new Error('An API seat needs its endpoint: the base URL, for example http://localhost:11434/v1.');
|
|
247
|
+
let u;
|
|
248
|
+
try { u = new URL(text); } catch { throw new Error(`Endpoint "${text}" is not a URL.`); }
|
|
249
|
+
if (u.protocol !== 'https:' && u.protocol !== 'http:') throw new Error(`Endpoint ${text}: http or https only.`);
|
|
250
|
+
if (u.protocol === 'http:' && !privateHost(u.hostname)) throw new Error(`Endpoint ${u.host}: plain http only to this machine or a private network; a hosted API must be https.`);
|
|
251
|
+
if (u.username || u.password) throw new Error('Endpoint: credentials belong in an environment variable, not in the URL.');
|
|
252
|
+
if (u.search || u.hash) throw new Error('Endpoint: a base URL, without a query or a fragment.');
|
|
253
|
+
return u.origin + u.pathname.replace(/\/+$/, '');
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// The pool identity of an endpoint: its host, as a profile-shaped name (poolKey, PROFILE_RE):
|
|
257
|
+
// api.deepseek.com, localhost-11434. One pool per host, so a host that fails benches only itself.
|
|
258
|
+
export function hostKey(url) {
|
|
259
|
+
let u; try { u = new URL(String(url || '').trim()); } catch { return ''; }
|
|
260
|
+
const name = `${u.hostname.replace(/^\[|\]$/g, '')}${u.port ? `-${u.port}` : ''}`.toLowerCase().replace(/[^a-z0-9_.-]+/g, '-').replace(/^[^a-z0-9]+/, '');
|
|
261
|
+
return name.slice(0, 40);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// What the card and the record say about where a seat runs: the CLI on a subscription, an API
|
|
265
|
+
// on this machine, or an API that bills per token.
|
|
266
|
+
export function seatKind(vendorRow, endpoint = null) {
|
|
267
|
+
if (vendorRow?.kind === 'acp') return 'ACP · profile billing';
|
|
268
|
+
if (vendorRow?.kind !== 'api') return 'CLI · subscription';
|
|
269
|
+
let u; try { u = new URL(String(endpoint || '')); } catch { return 'API'; }
|
|
270
|
+
return privateHost(u.hostname) ? 'API · local' : 'API · pay-per-token';
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function normaliseEndpoint(raw, label) {
|
|
274
|
+
const endpoint = checkEndpointUrl(raw.endpoint);
|
|
275
|
+
const keyEnv = String(raw.keyEnv || '').trim();
|
|
276
|
+
if (keyEnv && !KEY_ENV_RE.test(keyEnv)) throw new Error(`${label}: the key variable must be the NAME of an environment variable (letters, digits, underscores), not the key itself.`);
|
|
277
|
+
if (!keyEnv && !privateHost(new URL(endpoint).hostname)) throw new Error(`${label}: a hosted API needs the name of the environment variable that holds its key.`);
|
|
278
|
+
const model = raw.model ? String(raw.model).trim() : '';
|
|
279
|
+
if (!model) throw new Error(`${label}: an API seat needs a model id (the endpoint has no default).`);
|
|
280
|
+
return { endpoint, keyEnv: keyEnv || undefined, model, account: hostKey(endpoint) };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// For ACP the existing account field selects an AgentLink profile, not a login
|
|
284
|
+
// directory or a command. Keep pool identity and saved review records consistent.
|
|
285
|
+
function seatAccount(raw, row) {
|
|
286
|
+
return row.kind === 'acp' ? agentLinkProfile(raw.account) : normaliseAccount(raw.account);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// vendors: { [vendor]: { label, effortLevels } } (the adapter table). efforts may widen a
|
|
290
|
+
// vendor's levels from the catalogue (agy's come from its model ids); models is the
|
|
291
|
+
// catalogue's model list per vendor, for the lightest model a hidden mood runs on.
|
|
292
|
+
// An ACP agent advertises its reasoning levels when a session opens, so the adapter checks a
|
|
293
|
+
// seat's effort against that list; here it is only a bounded identifier.
|
|
294
|
+
|
|
295
|
+
function checkEffort(raw, vendor, allowed, label) {
|
|
296
|
+
const effort = raw.effort ? String(raw.effort).trim() : null;
|
|
297
|
+
if (!effort) return null;
|
|
298
|
+
if (vendor.kind === 'acp') {
|
|
299
|
+
if (effort.length > 200 || /[\u0000-\u001f\u007f]/.test(effort)) throw new Error(`${label}: an ACP effort must be an advertised ID of at most 200 characters without control characters.`);
|
|
300
|
+
return effort;
|
|
301
|
+
}
|
|
302
|
+
if (!allowed.includes(effort)) throw new Error(`${label} does not accept effort "${effort}"${allowed.length ? ` (allowed: ${allowed.join(', ')})` : ''}.`);
|
|
303
|
+
return effort;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function validateAgyModel(vendor, model, models, label) {
|
|
307
|
+
if (vendor !== 'agy' || !model) return;
|
|
308
|
+
const id = String(model).trim();
|
|
309
|
+
const offered = models.agy || [];
|
|
310
|
+
if (offered.some((m) => (m.id || m) === id)) return;
|
|
311
|
+
const level = effortSuffix.exec(id);
|
|
312
|
+
if (offered.length || (level && effortSuffix.test(id.slice(0, level.index)))) {
|
|
313
|
+
throw new Error(`${label}: Antigravity model "${id}" is unavailable. Choose a model and one effort level from the picker.`);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export function normaliseSeats(input, { vendors, efforts = {}, models = {}, hidden = false }) {
|
|
318
|
+
const issue = seatSetupIssue((input || []).map((raw) => raw && vendors[raw.vendor] ? raw : null));
|
|
319
|
+
if (issue) throw Object.assign(new Error(issue.message), issue, { status: 400 });
|
|
320
|
+
const seats = [];
|
|
321
|
+
const perVendor = {};
|
|
322
|
+
for (const raw of input || []) {
|
|
323
|
+
if (!raw || !vendors[raw.vendor]) continue;
|
|
324
|
+
const lens = String(raw.lens || '').trim();
|
|
325
|
+
validateAgyModel(raw.vendor, raw.model, models, `Seat "${lens}"`);
|
|
326
|
+
const allowed = modelEfforts(raw.vendor, raw.model, efforts[raw.vendor] || vendors[raw.vendor].effortLevels || [], models[raw.vendor]);
|
|
327
|
+
const effort = checkEffort(raw, vendors[raw.vendor], allowed, `${vendors[raw.vendor].label}${raw.model ? ` ${String(raw.model).trim()}` : ''}`);
|
|
328
|
+
perVendor[raw.vendor] = (perVendor[raw.vendor] || 0) + 1;
|
|
329
|
+
// an endpoint seat: URL, model and key variable checked; its account is its host
|
|
330
|
+
const ep = vendors[raw.vendor].kind === 'api' ? normaliseEndpoint(raw, `Seat "${lens}"`) : null;
|
|
331
|
+
const mood = normaliseMood(raw.mood, { hidden });
|
|
332
|
+
// a hidden mood always runs as cheaply as the seat's vendor allows
|
|
333
|
+
const locked = mood.hidden ? { model: ep ? ep.model : vendors[raw.vendor].kind === 'acp' ? raw.model || undefined : lightestModel(raw.vendor, models[raw.vendor]), effort: allowed[0] || undefined } : null;
|
|
334
|
+
seats.push({
|
|
335
|
+
id: `seat-${seats.length}`,
|
|
336
|
+
vendor: raw.vendor,
|
|
337
|
+
n: perVendor[raw.vendor],
|
|
338
|
+
lens,
|
|
339
|
+
mood,
|
|
340
|
+
model: locked ? locked.model : ep ? ep.model : raw.model ? String(raw.model).trim() : undefined,
|
|
341
|
+
effort: locked ? locked.effort : effort || undefined,
|
|
342
|
+
account: ep ? ep.account : seatAccount(raw, vendors[raw.vendor]),
|
|
343
|
+
...(ep ? { endpoint: ep.endpoint, keyEnv: ep.keyEnv } : {}),
|
|
344
|
+
// the vendor's colour (adapters config) unless the board sent one; positions as a fallback
|
|
345
|
+
colour: COLOURS.includes(raw.colour) ? raw.colour : (vendors[raw.vendor].colour || COLOURS[seats.length % COLOURS.length]),
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
if (seats.length < 2) throw new Error('Seat at least two instances, each with a lens.');
|
|
349
|
+
// Human-side label only: "Codex 2" when a vendor is seated more than once; an endpoint seat
|
|
350
|
+
// is named by its host ("API localhost-11434"). Never in a prompt.
|
|
351
|
+
const perBase = {};
|
|
352
|
+
for (const s of seats) { s.base = s.endpoint ? `API ${s.account}` : vendors[s.vendor].kind === 'acp' ? `ACP ${s.account}` : vendors[s.vendor].label; perBase[s.base] = (perBase[s.base] || 0) + 1; }
|
|
353
|
+
const seen2 = {};
|
|
354
|
+
for (const s of seats) { seen2[s.base] = (seen2[s.base] || 0) + 1; s.label = perBase[s.base] > 1 ? `${s.base} ${seen2[s.base]}` : s.base; delete s.base; }
|
|
355
|
+
return seats;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// role: who the error names; the searcher's settings are checked here too
|
|
359
|
+
export function normaliseChairman(raw, { vendors, efforts = {}, models = {}, role = 'Chairman' }) {
|
|
360
|
+
const vendor = raw?.vendor && vendors[raw.vendor] ? raw.vendor : null;
|
|
361
|
+
if (!vendor) return null;
|
|
362
|
+
validateAgyModel(vendor, raw.model, models, role);
|
|
363
|
+
const allowed = modelEfforts(vendor, raw.model, efforts[vendor] || vendors[vendor].effortLevels || [], models[vendor]);
|
|
364
|
+
const effort = checkEffort(raw, vendors[vendor], allowed, `${role}: ${vendors[vendor].label}${raw.model ? ` ${String(raw.model).trim()}` : ''}`);
|
|
365
|
+
const ep = vendors[vendor].kind === 'api' ? normaliseEndpoint(raw, role) : null;
|
|
366
|
+
return { vendor, colour: vendors[vendor].colour || undefined, model: ep ? ep.model : raw.model ? String(raw.model).trim() : undefined, effort: effort || undefined,
|
|
367
|
+
account: ep ? ep.account : seatAccount(raw, vendors[vendor]), ...(ep ? { endpoint: ep.endpoint, keyEnv: ep.keyEnv } : {}) };
|
|
368
|
+
}
|