heyio 4.2.7 → 4.3.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.
@@ -79,10 +79,10 @@ var init_constants = __esm({
79
79
  "packages/shared/dist/constants.js"() {
80
80
  "use strict";
81
81
  APP_NAME = "io";
82
- APP_VERSION = "4.2.7";
82
+ APP_VERSION = "4.3.0";
83
83
  API_PORT = 7777;
84
84
  API_HOST = "0.0.0.0";
85
- DEFAULT_MODEL = "gpt-4o";
85
+ DEFAULT_MODEL = "gpt-4.1-mini";
86
86
  SESSION_RESET_THRESHOLD = 50;
87
87
  SCHEDULER_INTERVAL_MS = 6e4;
88
88
  QA_MAX_REVISIONS = 3;
@@ -152,6 +152,349 @@ var init_paths = __esm({
152
152
  }
153
153
  });
154
154
 
155
+ // packages/daemon/src/store/db.ts
156
+ import { mkdir } from "node:fs/promises";
157
+ import { dirname } from "node:path";
158
+ import { pathToFileURL } from "node:url";
159
+ import { createClient } from "@libsql/client";
160
+ function asString(value) {
161
+ if (typeof value === "string") {
162
+ return value;
163
+ }
164
+ if (value === null || value === void 0) {
165
+ throw new Error("Expected string value but received null or undefined");
166
+ }
167
+ return String(value);
168
+ }
169
+ function asNullableString(value) {
170
+ if (value === null || value === void 0) {
171
+ return null;
172
+ }
173
+ return typeof value === "string" ? value : String(value);
174
+ }
175
+ function asNumber(value) {
176
+ if (typeof value === "number") {
177
+ return value;
178
+ }
179
+ if (typeof value === "bigint") {
180
+ return Number(value);
181
+ }
182
+ if (typeof value === "string") {
183
+ return Number(value);
184
+ }
185
+ throw new Error(`Expected numeric value but received ${typeof value}`);
186
+ }
187
+ function asNullableNumber(value) {
188
+ if (value === null || value === void 0) {
189
+ return null;
190
+ }
191
+ return asNumber(value);
192
+ }
193
+ function asBoolean(value) {
194
+ return asNumber(value) === 1;
195
+ }
196
+ function toSqliteBoolean(value) {
197
+ return value ? 1 : 0;
198
+ }
199
+ function parseJson(value) {
200
+ if (typeof value !== "string") {
201
+ return value;
202
+ }
203
+ return JSON.parse(value);
204
+ }
205
+ function serializeJson(value) {
206
+ return JSON.stringify(value);
207
+ }
208
+ async function initDatabase() {
209
+ if (client && !client.closed) {
210
+ return client;
211
+ }
212
+ if (initPromise) {
213
+ return initPromise;
214
+ }
215
+ initPromise = initializeDatabase(defaultConnectionOptions);
216
+ try {
217
+ client = await initPromise;
218
+ return client;
219
+ } finally {
220
+ initPromise = null;
221
+ }
222
+ }
223
+ async function getDatabase() {
224
+ return initDatabase();
225
+ }
226
+ async function initializeDatabase(options2 = {}) {
227
+ const url2 = await resolveDatabaseUrl(options2);
228
+ const databaseClient = createClient({ url: url2 });
229
+ await databaseClient.execute("PRAGMA foreign_keys = ON");
230
+ await databaseClient.execute("PRAGMA journal_mode = WAL");
231
+ await ensureMigrationTable(databaseClient);
232
+ await applyMigrations(databaseClient);
233
+ return databaseClient;
234
+ }
235
+ async function resolveDatabaseUrl(options2) {
236
+ if (options2.url) {
237
+ return options2.url;
238
+ }
239
+ const databasePath = options2.path ?? DB_PATH;
240
+ await mkdir(dirname(databasePath), { recursive: true });
241
+ return pathToFileURL(databasePath).href;
242
+ }
243
+ async function ensureMigrationTable(databaseClient) {
244
+ await databaseClient.executeMultiple(`
245
+ CREATE TABLE IF NOT EXISTS schema_migrations (
246
+ version INTEGER PRIMARY KEY,
247
+ name TEXT NOT NULL,
248
+ applied_at TEXT NOT NULL
249
+ );
250
+ `);
251
+ }
252
+ async function applyMigrations(databaseClient) {
253
+ const appliedVersions = await getAppliedVersions(databaseClient);
254
+ for (const migration of MIGRATIONS) {
255
+ if (appliedVersions.has(migration.version)) {
256
+ continue;
257
+ }
258
+ const transaction = await databaseClient.transaction("write");
259
+ try {
260
+ for (const statement of migration.statements) {
261
+ await transaction.execute(statement);
262
+ }
263
+ await transaction.execute({
264
+ sql: "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",
265
+ args: [migration.version, migration.name, nowIso()]
266
+ });
267
+ await transaction.commit();
268
+ } catch (error51) {
269
+ if (!transaction.closed) {
270
+ await transaction.rollback();
271
+ }
272
+ throw error51;
273
+ } finally {
274
+ if (!transaction.closed) {
275
+ transaction.close();
276
+ }
277
+ }
278
+ }
279
+ }
280
+ async function getAppliedVersions(databaseClient) {
281
+ const result = await databaseClient.execute(
282
+ "SELECT version FROM schema_migrations ORDER BY version ASC"
283
+ );
284
+ return new Set(result.rows.map((row) => asNumber(row.version)));
285
+ }
286
+ var MIGRATIONS, client, initPromise, defaultConnectionOptions, generateId, nowIso;
287
+ var init_db = __esm({
288
+ "packages/daemon/src/store/db.ts"() {
289
+ "use strict";
290
+ init_paths();
291
+ MIGRATIONS = [
292
+ {
293
+ version: 1,
294
+ name: "create-store-schema",
295
+ statements: [
296
+ `CREATE TABLE IF NOT EXISTS squads (
297
+ id TEXT PRIMARY KEY,
298
+ name TEXT NOT NULL,
299
+ repo_url TEXT NOT NULL,
300
+ repo_owner TEXT NOT NULL,
301
+ repo_name TEXT NOT NULL,
302
+ status TEXT NOT NULL,
303
+ config TEXT NOT NULL,
304
+ created_at TEXT NOT NULL,
305
+ updated_at TEXT NOT NULL,
306
+ UNIQUE (repo_owner, repo_name)
307
+ )`,
308
+ `CREATE TABLE IF NOT EXISTS squad_members (
309
+ id TEXT PRIMARY KEY,
310
+ squad_id TEXT NOT NULL REFERENCES squads(id) ON DELETE CASCADE,
311
+ role TEXT NOT NULL,
312
+ name TEXT NOT NULL,
313
+ system_prompt TEXT NOT NULL,
314
+ model TEXT,
315
+ created_at TEXT NOT NULL
316
+ )`,
317
+ `CREATE TABLE IF NOT EXISTS objectives (
318
+ id TEXT PRIMARY KEY,
319
+ squad_id TEXT NOT NULL REFERENCES squads(id) ON DELETE CASCADE,
320
+ description TEXT NOT NULL,
321
+ status TEXT NOT NULL,
322
+ plan TEXT,
323
+ revision_count INTEGER NOT NULL DEFAULT 0,
324
+ branch TEXT,
325
+ pr_url TEXT,
326
+ created_at TEXT NOT NULL,
327
+ updated_at TEXT NOT NULL
328
+ )`,
329
+ `CREATE TABLE IF NOT EXISTS tasks (
330
+ id TEXT PRIMARY KEY,
331
+ objective_id TEXT NOT NULL REFERENCES objectives(id) ON DELETE CASCADE,
332
+ assignee_id TEXT REFERENCES squad_members(id) ON DELETE SET NULL,
333
+ title TEXT NOT NULL,
334
+ description TEXT NOT NULL,
335
+ status TEXT NOT NULL,
336
+ result TEXT,
337
+ created_at TEXT NOT NULL,
338
+ updated_at TEXT NOT NULL
339
+ )`,
340
+ `CREATE TABLE IF NOT EXISTS conversations (
341
+ id TEXT PRIMARY KEY,
342
+ title TEXT,
343
+ source TEXT NOT NULL,
344
+ created_at TEXT NOT NULL,
345
+ updated_at TEXT NOT NULL
346
+ )`,
347
+ `CREATE TABLE IF NOT EXISTS messages (
348
+ id TEXT PRIMARY KEY,
349
+ conversation_id TEXT NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
350
+ role TEXT NOT NULL,
351
+ content TEXT NOT NULL,
352
+ model TEXT,
353
+ input_tokens INTEGER,
354
+ output_tokens INTEGER,
355
+ created_at TEXT NOT NULL
356
+ )`,
357
+ `CREATE TABLE IF NOT EXISTS inbox (
358
+ id TEXT PRIMARY KEY,
359
+ squad_id TEXT REFERENCES squads(id) ON DELETE SET NULL,
360
+ objective_id TEXT REFERENCES objectives(id) ON DELETE SET NULL,
361
+ type TEXT NOT NULL,
362
+ title TEXT NOT NULL,
363
+ content TEXT NOT NULL,
364
+ status TEXT NOT NULL,
365
+ reply TEXT,
366
+ created_at TEXT NOT NULL,
367
+ updated_at TEXT NOT NULL
368
+ )`,
369
+ `CREATE TABLE IF NOT EXISTS schedules (
370
+ id TEXT PRIMARY KEY,
371
+ name TEXT NOT NULL,
372
+ cron_expression TEXT NOT NULL,
373
+ prompt TEXT NOT NULL,
374
+ enabled INTEGER NOT NULL DEFAULT 1,
375
+ last_run_at TEXT,
376
+ next_run_at TEXT,
377
+ created_at TEXT NOT NULL,
378
+ updated_at TEXT NOT NULL
379
+ )`,
380
+ `CREATE TABLE IF NOT EXISTS token_usage (
381
+ id TEXT PRIMARY KEY,
382
+ squad_id TEXT REFERENCES squads(id) ON DELETE SET NULL,
383
+ agent_id TEXT,
384
+ model TEXT NOT NULL,
385
+ input_tokens INTEGER NOT NULL,
386
+ output_tokens INTEGER NOT NULL,
387
+ cost REAL NOT NULL,
388
+ created_at TEXT NOT NULL
389
+ )`,
390
+ `CREATE TABLE IF NOT EXISTS activity (
391
+ id TEXT PRIMARY KEY,
392
+ squad_id TEXT REFERENCES squads(id) ON DELETE SET NULL,
393
+ objective_id TEXT REFERENCES objectives(id) ON DELETE SET NULL,
394
+ event TEXT NOT NULL,
395
+ description TEXT NOT NULL,
396
+ metadata TEXT,
397
+ created_at TEXT NOT NULL
398
+ )`,
399
+ `CREATE TABLE IF NOT EXISTS agent_history (
400
+ id TEXT PRIMARY KEY,
401
+ agent_id TEXT NOT NULL,
402
+ squad_id TEXT REFERENCES squads(id) ON DELETE SET NULL,
403
+ content TEXT NOT NULL,
404
+ created_at TEXT NOT NULL
405
+ )`,
406
+ `CREATE TABLE IF NOT EXISTS settings (
407
+ key TEXT PRIMARY KEY,
408
+ value TEXT
409
+ )`,
410
+ "CREATE INDEX IF NOT EXISTS idx_squad_members_squad_id ON squad_members(squad_id)",
411
+ "CREATE INDEX IF NOT EXISTS idx_objectives_squad_id ON objectives(squad_id)",
412
+ "CREATE INDEX IF NOT EXISTS idx_objectives_status ON objectives(status)",
413
+ "CREATE INDEX IF NOT EXISTS idx_tasks_objective_id ON tasks(objective_id)",
414
+ "CREATE INDEX IF NOT EXISTS idx_tasks_assignee_id ON tasks(assignee_id)",
415
+ "CREATE INDEX IF NOT EXISTS idx_messages_conversation_id_created_at ON messages(conversation_id, created_at)",
416
+ "CREATE INDEX IF NOT EXISTS idx_inbox_status_created_at ON inbox(status, created_at)",
417
+ "CREATE INDEX IF NOT EXISTS idx_inbox_squad_id ON inbox(squad_id)",
418
+ "CREATE INDEX IF NOT EXISTS idx_inbox_objective_id ON inbox(objective_id)",
419
+ "CREATE INDEX IF NOT EXISTS idx_schedules_enabled_next_run_at ON schedules(enabled, next_run_at)",
420
+ "CREATE INDEX IF NOT EXISTS idx_token_usage_created_at ON token_usage(created_at)",
421
+ "CREATE INDEX IF NOT EXISTS idx_token_usage_squad_id ON token_usage(squad_id)",
422
+ "CREATE INDEX IF NOT EXISTS idx_token_usage_agent_id ON token_usage(agent_id)",
423
+ "CREATE INDEX IF NOT EXISTS idx_token_usage_model ON token_usage(model)",
424
+ "CREATE INDEX IF NOT EXISTS idx_activity_created_at ON activity(created_at)",
425
+ "CREATE INDEX IF NOT EXISTS idx_activity_squad_id ON activity(squad_id)",
426
+ "CREATE INDEX IF NOT EXISTS idx_activity_objective_id ON activity(objective_id)",
427
+ "CREATE INDEX IF NOT EXISTS idx_agent_history_agent_id_created_at ON agent_history(agent_id, created_at)"
428
+ ]
429
+ },
430
+ {
431
+ version: 2,
432
+ name: "add-model-pricing-and-dual-costs",
433
+ statements: [
434
+ `CREATE TABLE IF NOT EXISTS model_pricing (
435
+ id TEXT PRIMARY KEY,
436
+ display_name TEXT NOT NULL,
437
+ premium_multiplier REAL,
438
+ token_input_multiplier REAL,
439
+ token_output_multiplier REAL,
440
+ cached_input_multiplier REAL,
441
+ tier TEXT NOT NULL,
442
+ available INTEGER NOT NULL DEFAULT 1,
443
+ updated_at TEXT NOT NULL
444
+ )`,
445
+ "CREATE INDEX IF NOT EXISTS idx_model_pricing_tier ON model_pricing(tier)",
446
+ "CREATE INDEX IF NOT EXISTS idx_model_pricing_available ON model_pricing(available)",
447
+ "ALTER TABLE token_usage ADD COLUMN premium_request_cost REAL",
448
+ "ALTER TABLE token_usage ADD COLUMN token_unit_cost REAL"
449
+ ]
450
+ },
451
+ {
452
+ version: 3,
453
+ name: "add-squad-instances",
454
+ statements: [
455
+ `CREATE TABLE IF NOT EXISTS squad_instances (
456
+ id TEXT PRIMARY KEY,
457
+ squad_id TEXT NOT NULL REFERENCES squads(id) ON DELETE CASCADE,
458
+ objective_id TEXT REFERENCES objectives(id) ON DELETE SET NULL,
459
+ status TEXT NOT NULL DEFAULT 'queued',
460
+ branch TEXT,
461
+ worktree_path TEXT,
462
+ created_at TEXT NOT NULL,
463
+ started_at TEXT,
464
+ completed_at TEXT,
465
+ error TEXT
466
+ )`,
467
+ "CREATE INDEX IF NOT EXISTS idx_squad_instances_squad_status ON squad_instances(squad_id, status)",
468
+ "CREATE INDEX IF NOT EXISTS idx_squad_instances_status ON squad_instances(status)",
469
+ "CREATE INDEX IF NOT EXISTS idx_squad_instances_objective_id ON squad_instances(objective_id)"
470
+ ]
471
+ },
472
+ {
473
+ version: 4,
474
+ name: "denormalize-usage-names",
475
+ statements: [
476
+ "ALTER TABLE token_usage ADD COLUMN squad_name TEXT",
477
+ "ALTER TABLE token_usage ADD COLUMN agent_name TEXT",
478
+ `UPDATE token_usage SET
479
+ squad_name = (SELECT s.name FROM squads s WHERE s.id = token_usage.squad_id),
480
+ agent_name = (SELECT sm.name FROM squad_members sm WHERE sm.id = token_usage.agent_id)
481
+ WHERE squad_id IS NOT NULL OR agent_id IS NOT NULL`
482
+ ]
483
+ },
484
+ {
485
+ version: 5,
486
+ name: "add-squad-soft-delete",
487
+ statements: ["ALTER TABLE squads ADD COLUMN deleted_at TEXT"]
488
+ }
489
+ ];
490
+ client = null;
491
+ initPromise = null;
492
+ defaultConnectionOptions = {};
493
+ generateId = () => crypto.randomUUID();
494
+ nowIso = () => (/* @__PURE__ */ new Date()).toISOString();
495
+ }
496
+ });
497
+
155
498
  // node_modules/cron-parser/dist/fields/types.js
156
499
  var require_types = __commonJS({
157
500
  "node_modules/cron-parser/dist/fields/types.js"(exports2) {
@@ -62206,6 +62549,493 @@ var require_websocket_server = __commonJS({
62206
62549
  }
62207
62550
  });
62208
62551
 
62552
+ // packages/daemon/src/models/catalog.ts
62553
+ import { execFileSync } from "node:child_process";
62554
+ function resolveGitHubToken() {
62555
+ const envToken = process.env.GITHUB_TOKEN?.trim();
62556
+ if (envToken && envToken.length > 0) {
62557
+ return envToken;
62558
+ }
62559
+ try {
62560
+ const token = execFileSync("gh", ["auth", "token"], {
62561
+ encoding: "utf8",
62562
+ stdio: ["ignore", "pipe", "pipe"]
62563
+ }).trim();
62564
+ return token.length > 0 ? token : void 0;
62565
+ } catch {
62566
+ return void 0;
62567
+ }
62568
+ }
62569
+ async function fetchModelCatalog() {
62570
+ const token = resolveGitHubToken();
62571
+ if (!token) {
62572
+ throw new Error("No GitHub token available for model catalog fetch");
62573
+ }
62574
+ const response = await fetch(CATALOG_URL, {
62575
+ headers: {
62576
+ Accept: "application/json",
62577
+ Authorization: `Bearer ${token}`,
62578
+ "X-GitHub-Api-Version": "2024-12-01"
62579
+ }
62580
+ });
62581
+ if (!response.ok) {
62582
+ throw new Error(`Model catalog fetch failed: ${response.status} ${response.statusText}`);
62583
+ }
62584
+ const data = await response.json();
62585
+ if (!Array.isArray(data)) {
62586
+ throw new Error("Model catalog response is not an array");
62587
+ }
62588
+ const models = [];
62589
+ for (const entry of data) {
62590
+ const id = entry.id ?? entry.name;
62591
+ const displayName = entry.friendly_name ?? entry.name ?? id;
62592
+ if (id && typeof id === "string") {
62593
+ models.push({ id, displayName: displayName ?? id });
62594
+ }
62595
+ }
62596
+ return models;
62597
+ }
62598
+ var CATALOG_URL;
62599
+ var init_catalog = __esm({
62600
+ "packages/daemon/src/models/catalog.ts"() {
62601
+ "use strict";
62602
+ CATALOG_URL = "https://models.github.ai/catalog/models";
62603
+ }
62604
+ });
62605
+
62606
+ // packages/daemon/src/models/pricing-scraper.ts
62607
+ async function scrapeTokenUnitPricing() {
62608
+ const html = await fetchPage(TOKEN_UNIT_COSTS_URL);
62609
+ return parseTokenUnitTable(html);
62610
+ }
62611
+ async function scrapeCopilotPricing() {
62612
+ const markdown = await fetchMarkdown(COPILOT_PRICING_URL);
62613
+ return parseCopilotPricingMarkdown(markdown);
62614
+ }
62615
+ async function scrapePremiumRequestPricing() {
62616
+ const html = await fetchPage(PREMIUM_MULTIPLIERS_URL);
62617
+ return parsePremiumMultiplierTable(html);
62618
+ }
62619
+ async function fetchPage(url2) {
62620
+ const response = await fetch(url2, {
62621
+ headers: {
62622
+ Accept: "text/html",
62623
+ "User-Agent": "IO-Daemon/4.0 (pricing-refresh)"
62624
+ },
62625
+ redirect: "follow"
62626
+ });
62627
+ if (!response.ok) {
62628
+ throw new Error(`Failed to fetch ${url2}: ${response.status} ${response.statusText}`);
62629
+ }
62630
+ return response.text();
62631
+ }
62632
+ async function fetchMarkdown(url2) {
62633
+ const response = await fetch(url2, {
62634
+ headers: {
62635
+ Accept: "text/markdown, text/plain, */*",
62636
+ "User-Agent": "IO-Daemon/4.0 (pricing-refresh)"
62637
+ },
62638
+ redirect: "follow"
62639
+ });
62640
+ if (!response.ok) {
62641
+ throw new Error(`Failed to fetch ${url2}: ${response.status} ${response.statusText}`);
62642
+ }
62643
+ return response.text();
62644
+ }
62645
+ function parseTokenUnitTable(html) {
62646
+ const results = [];
62647
+ const tableRowPattern = /<tr[^>]*>\s*<td[^>]*>([^<]+)<\/td>\s*<td[^>]*>([^<]+)<\/td>\s*<td[^>]*>([^<]*)<\/td>\s*<td[^>]*>([^<]+)<\/td>/gi;
62648
+ for (const match of html.matchAll(tableRowPattern)) {
62649
+ const modelName = cleanCellText(match[1]);
62650
+ const inputMultiplier = Number.parseFloat(match[2]);
62651
+ const cachedInput = match[3].trim().toLowerCase();
62652
+ const outputMultiplier = Number.parseFloat(match[4]);
62653
+ if (modelName && !Number.isNaN(inputMultiplier) && !Number.isNaN(outputMultiplier)) {
62654
+ results.push({
62655
+ modelName,
62656
+ inputMultiplier,
62657
+ cachedInputMultiplier: cachedInput === "n/a" || cachedInput === "" ? null : Number.parseFloat(cachedInput) || null,
62658
+ outputMultiplier
62659
+ });
62660
+ }
62661
+ }
62662
+ return results;
62663
+ }
62664
+ function parsePremiumMultiplierTable(html) {
62665
+ const results = [];
62666
+ const tableRowPattern = /<tr[^>]*>\s*<td[^>]*>([^<]+)<\/td>\s*<td[^>]*>([^<]+)<\/td>\s*<\/tr>/gi;
62667
+ for (const match of html.matchAll(tableRowPattern)) {
62668
+ const modelName = cleanCellText(match[1]);
62669
+ const multiplier = Number.parseFloat(match[2]);
62670
+ if (modelName && !Number.isNaN(multiplier) && multiplier > 0) {
62671
+ results.push({ modelName, multiplier });
62672
+ }
62673
+ }
62674
+ return results;
62675
+ }
62676
+ function cleanCellText(text) {
62677
+ return text.replace(/<[^>]*>/g, "").replace(/&[^;]+;/g, " ").trim();
62678
+ }
62679
+ function parseCopilotPricingMarkdown(markdown) {
62680
+ const results = [];
62681
+ const seenModels = /* @__PURE__ */ new Set();
62682
+ const lines = markdown.split("\n");
62683
+ let columnIndices = null;
62684
+ for (const line of lines) {
62685
+ const trimmed = line.trim();
62686
+ if (!trimmed.startsWith("|")) continue;
62687
+ if (/\|\s*Model\s/i.test(trimmed)) {
62688
+ columnIndices = parseHeaderColumns(trimmed);
62689
+ continue;
62690
+ }
62691
+ if (/^[\s|:-]+$/.test(trimmed) || !columnIndices) continue;
62692
+ const entry = parseDataRow(trimmed, columnIndices);
62693
+ if (entry && !seenModels.has(entry.modelName.toLowerCase())) {
62694
+ seenModels.add(entry.modelName.toLowerCase());
62695
+ results.push(entry);
62696
+ }
62697
+ }
62698
+ return results;
62699
+ }
62700
+ function parseDataRow(line, columnIndices) {
62701
+ const cells = splitMarkdownRow(line);
62702
+ if (cells.length <= columnIndices.output) return null;
62703
+ const modelName = cells[columnIndices.model].trim();
62704
+ const inputPrice = parseDollarValue(cells[columnIndices.input]);
62705
+ const cachedPrice = columnIndices.cached >= 0 ? parseDollarValue(cells[columnIndices.cached]) : null;
62706
+ const outputPrice = parseDollarValue(cells[columnIndices.output]);
62707
+ if (!modelName || inputPrice === null || outputPrice === null) return null;
62708
+ return {
62709
+ modelName,
62710
+ inputMultiplier: inputPrice * PRICE_TO_MULTIPLIER,
62711
+ cachedInputMultiplier: cachedPrice !== null ? cachedPrice * PRICE_TO_MULTIPLIER : null,
62712
+ outputMultiplier: outputPrice * PRICE_TO_MULTIPLIER
62713
+ };
62714
+ }
62715
+ function parseHeaderColumns(headerLine) {
62716
+ const cells = splitMarkdownRow(headerLine).map((c) => c.trim().toLowerCase());
62717
+ const model = cells.findIndex((c) => c === "model" || c === "model name");
62718
+ const input = cells.findIndex(
62719
+ (c) => (c === "input" || c === "input multiplier") && !c.includes("cached")
62720
+ );
62721
+ const cached2 = cells.findIndex(
62722
+ (c) => c.includes("cached input") || c === "cached input multiplier"
62723
+ );
62724
+ const output = cells.findIndex(
62725
+ (c) => (c === "output" || c === "output multiplier") && !c.includes("cached")
62726
+ );
62727
+ if (model < 0 || input < 0 || output < 0) {
62728
+ return null;
62729
+ }
62730
+ return { model, input, cached: cached2 >= 0 ? cached2 : -1, output };
62731
+ }
62732
+ function splitMarkdownRow(line) {
62733
+ const parts = line.split("|");
62734
+ if (parts[0].trim() === "") parts.shift();
62735
+ if (parts[parts.length - 1]?.trim() === "") parts.pop();
62736
+ return parts;
62737
+ }
62738
+ function parseDollarValue(cell) {
62739
+ if (!cell) return null;
62740
+ const cleaned = cell.trim().replace(/[$,]/g, "");
62741
+ if (cleaned.toLowerCase() === "n/a" || cleaned === "" || cleaned === "-") {
62742
+ return null;
62743
+ }
62744
+ const value = Number.parseFloat(cleaned);
62745
+ return Number.isNaN(value) ? null : value;
62746
+ }
62747
+ var TOKEN_UNIT_COSTS_URL, PREMIUM_MULTIPLIERS_URL, COPILOT_PRICING_URL, PRICE_TO_MULTIPLIER;
62748
+ var init_pricing_scraper = __esm({
62749
+ "packages/daemon/src/models/pricing-scraper.ts"() {
62750
+ "use strict";
62751
+ TOKEN_UNIT_COSTS_URL = "https://docs.github.com/en/billing/reference/costs-for-github-models";
62752
+ PREMIUM_MULTIPLIERS_URL = "https://docs.github.com/en/copilot/reference/copilot-billing/request-based-billing-legacy/model-multipliers-for-annual-plans";
62753
+ COPILOT_PRICING_URL = "https://docs.github.com/api/article/body?pathname=/en/copilot/reference/copilot-billing/models-and-pricing";
62754
+ PRICE_TO_MULTIPLIER = 0.1;
62755
+ }
62756
+ });
62757
+
62758
+ // packages/daemon/src/models/types.ts
62759
+ function computeTierFromMultiplier(premiumMultiplier) {
62760
+ if (premiumMultiplier === null) {
62761
+ return "standard";
62762
+ }
62763
+ for (const [tier, range] of Object.entries(TIER_RANGES)) {
62764
+ if (premiumMultiplier >= range.min && premiumMultiplier <= range.max) {
62765
+ return tier;
62766
+ }
62767
+ }
62768
+ return "ultra";
62769
+ }
62770
+ var TIER_RANGES, TOKEN_UNIT_PRICE;
62771
+ var init_types = __esm({
62772
+ "packages/daemon/src/models/types.ts"() {
62773
+ "use strict";
62774
+ TIER_RANGES = {
62775
+ trivial: { min: 0, max: 0.33 },
62776
+ fast: { min: 0.34, max: 1 },
62777
+ standard: { min: 1.1, max: 5 },
62778
+ premium: { min: 5.1, max: 15 },
62779
+ ultra: { min: 15.1, max: Number.POSITIVE_INFINITY }
62780
+ };
62781
+ TOKEN_UNIT_PRICE = 1e-5;
62782
+ }
62783
+ });
62784
+
62785
+ // packages/daemon/src/models/registry.ts
62786
+ function normalizeModelName(name) {
62787
+ return name.toLowerCase().replace(/^openai\s+/i, "").replace(/\s+/g, "-").replace(/[^a-z0-9.\-]/g, "").trim();
62788
+ }
62789
+ async function fetchCatalogIntoMap(modelMap, result, logger2) {
62790
+ try {
62791
+ const catalogModels = await fetchModelCatalog();
62792
+ result.catalogFetched = true;
62793
+ for (const m of catalogModels) {
62794
+ const key = normalizeModelName(m.id);
62795
+ modelMap.set(key, { id: m.id, displayName: m.displayName, available: true });
62796
+ }
62797
+ } catch (error51) {
62798
+ const msg = error51 instanceof Error ? error51.message : String(error51);
62799
+ result.errors.push(`Catalog fetch failed: ${msg}`);
62800
+ logger2?.warn(`Model catalog fetch failed: ${msg}`);
62801
+ }
62802
+ }
62803
+ async function scrapeTokenPricingIntoMap(modelMap, result, logger2) {
62804
+ try {
62805
+ const tokenPricing = await scrapeTokenUnitPricing();
62806
+ result.tokenPricingScraped = true;
62807
+ for (const tp of tokenPricing) {
62808
+ const key = normalizeModelName(tp.modelName);
62809
+ const existing = modelMap.get(key) ?? findClosestKey(modelMap, key);
62810
+ if (existing) {
62811
+ existing.tokenInputMultiplier = tp.inputMultiplier;
62812
+ existing.tokenOutputMultiplier = tp.outputMultiplier;
62813
+ existing.cachedInputMultiplier = tp.cachedInputMultiplier;
62814
+ } else {
62815
+ modelMap.set(key, {
62816
+ id: key,
62817
+ displayName: tp.modelName,
62818
+ tokenInputMultiplier: tp.inputMultiplier,
62819
+ tokenOutputMultiplier: tp.outputMultiplier,
62820
+ cachedInputMultiplier: tp.cachedInputMultiplier,
62821
+ available: true
62822
+ });
62823
+ }
62824
+ }
62825
+ } catch (error51) {
62826
+ const msg = error51 instanceof Error ? error51.message : String(error51);
62827
+ result.errors.push(`Token pricing scrape failed: ${msg}`);
62828
+ logger2?.warn(`Token pricing scrape failed: ${msg}`);
62829
+ }
62830
+ }
62831
+ async function scrapePremiumPricingIntoMap(modelMap, result, logger2) {
62832
+ try {
62833
+ const premiumPricing = await scrapePremiumRequestPricing();
62834
+ result.premiumPricingScraped = true;
62835
+ for (const pp of premiumPricing) {
62836
+ const key = normalizeModelName(pp.modelName);
62837
+ const existing = modelMap.get(key) ?? findClosestKey(modelMap, key);
62838
+ if (existing) {
62839
+ existing.premiumMultiplier = pp.multiplier;
62840
+ } else {
62841
+ modelMap.set(key, {
62842
+ id: key,
62843
+ displayName: pp.modelName,
62844
+ premiumMultiplier: pp.multiplier,
62845
+ available: true
62846
+ });
62847
+ }
62848
+ }
62849
+ } catch (error51) {
62850
+ const msg = error51 instanceof Error ? error51.message : String(error51);
62851
+ result.errors.push(`Premium pricing scrape failed: ${msg}`);
62852
+ logger2?.warn(`Premium pricing scrape failed: ${msg}`);
62853
+ }
62854
+ }
62855
+ async function scrapeCopilotPricingIntoMap(modelMap, result, logger2) {
62856
+ try {
62857
+ const copilotPricing = await scrapeCopilotPricing();
62858
+ result.copilotPricingScraped = true;
62859
+ for (const cp of copilotPricing) {
62860
+ const key = normalizeModelName(cp.modelName);
62861
+ const existing = modelMap.get(key) ?? findClosestKey(modelMap, key);
62862
+ if (existing) {
62863
+ existing.tokenInputMultiplier = cp.inputMultiplier;
62864
+ existing.tokenOutputMultiplier = cp.outputMultiplier;
62865
+ if (cp.cachedInputMultiplier !== null) {
62866
+ existing.cachedInputMultiplier = cp.cachedInputMultiplier;
62867
+ }
62868
+ } else {
62869
+ modelMap.set(key, {
62870
+ id: key,
62871
+ displayName: cp.modelName,
62872
+ tokenInputMultiplier: cp.inputMultiplier,
62873
+ tokenOutputMultiplier: cp.outputMultiplier,
62874
+ cachedInputMultiplier: cp.cachedInputMultiplier,
62875
+ available: true
62876
+ });
62877
+ }
62878
+ }
62879
+ } catch (error51) {
62880
+ const msg = error51 instanceof Error ? error51.message : String(error51);
62881
+ result.errors.push(`Copilot pricing scrape failed: ${msg}`);
62882
+ logger2?.warn(`Copilot pricing scrape failed: ${msg}`);
62883
+ }
62884
+ }
62885
+ async function refreshModelPricing(logger2) {
62886
+ const result = {
62887
+ modelsUpdated: 0,
62888
+ catalogFetched: false,
62889
+ tokenPricingScraped: false,
62890
+ premiumPricingScraped: false,
62891
+ copilotPricingScraped: false,
62892
+ errors: []
62893
+ };
62894
+ const modelMap = /* @__PURE__ */ new Map();
62895
+ await fetchCatalogIntoMap(modelMap, result, logger2);
62896
+ await scrapeTokenPricingIntoMap(modelMap, result, logger2);
62897
+ await scrapeCopilotPricingIntoMap(modelMap, result, logger2);
62898
+ await scrapePremiumPricingIntoMap(modelMap, result, logger2);
62899
+ if (!result.catalogFetched && !result.tokenPricingScraped && !result.premiumPricingScraped && !result.copilotPricingScraped) {
62900
+ logger2?.warn("All pricing sources failed, no models available");
62901
+ return result;
62902
+ }
62903
+ const db = await getDatabase();
62904
+ const now = nowIso();
62905
+ for (const model of modelMap.values()) {
62906
+ if (model.tokenInputMultiplier == null) {
62907
+ continue;
62908
+ }
62909
+ const tier = computeTierFromMultiplier(model.premiumMultiplier ?? null);
62910
+ await upsertModel(db, {
62911
+ id: model.id,
62912
+ displayName: model.displayName,
62913
+ premiumMultiplier: model.premiumMultiplier ?? null,
62914
+ tokenInputMultiplier: model.tokenInputMultiplier,
62915
+ tokenOutputMultiplier: model.tokenOutputMultiplier ?? null,
62916
+ cachedInputMultiplier: model.cachedInputMultiplier ?? null,
62917
+ tier,
62918
+ available: model.available ?? true,
62919
+ updatedAt: now
62920
+ });
62921
+ result.modelsUpdated++;
62922
+ }
62923
+ return result;
62924
+ }
62925
+ async function getModelsForTier(tier) {
62926
+ const db = await getDatabase();
62927
+ const result = await db.execute({
62928
+ sql: "SELECT * FROM model_pricing WHERE tier = ? AND available = 1 ORDER BY premium_multiplier ASC NULLS LAST",
62929
+ args: [tier]
62930
+ });
62931
+ return result.rows.map(rowToModelPricing);
62932
+ }
62933
+ async function getCheapestInTier(tier) {
62934
+ const models = await getModelsForTier(tier);
62935
+ return models[0] ?? null;
62936
+ }
62937
+ async function getModelPricing(modelId) {
62938
+ const db = await getDatabase();
62939
+ const result = await db.execute({
62940
+ sql: "SELECT * FROM model_pricing WHERE id = ?",
62941
+ args: [modelId]
62942
+ });
62943
+ if (result.rows.length > 0) {
62944
+ return rowToModelPricing(result.rows[0]);
62945
+ }
62946
+ const allModels = await db.execute(
62947
+ "SELECT * FROM model_pricing WHERE token_input_multiplier IS NOT NULL ORDER BY length(id) DESC"
62948
+ );
62949
+ for (const row of allModels.rows) {
62950
+ const storedId = asString(row.id);
62951
+ if (modelId.startsWith(storedId) || storedId.startsWith(modelId)) {
62952
+ return rowToModelPricing(row);
62953
+ }
62954
+ }
62955
+ return null;
62956
+ }
62957
+ async function getCheapestAvailableModel() {
62958
+ const tiers = ["trivial", "fast", "standard", "premium", "ultra"];
62959
+ for (const tier of tiers) {
62960
+ const model = await getCheapestInTier(tier);
62961
+ if (model) {
62962
+ return model;
62963
+ }
62964
+ }
62965
+ return null;
62966
+ }
62967
+ function calculateTokenUnitCost(inputTokens, outputTokens, inputMultiplier, outputMultiplier) {
62968
+ if (inputMultiplier === null || outputMultiplier === null) {
62969
+ return 0;
62970
+ }
62971
+ const tokenUnits = inputTokens * inputMultiplier + outputTokens * outputMultiplier;
62972
+ return tokenUnits * TOKEN_UNIT_PRICE;
62973
+ }
62974
+ function getNextTierUp(tier) {
62975
+ const order = ["trivial", "fast", "standard", "premium", "ultra"];
62976
+ const idx = order.indexOf(tier);
62977
+ if (idx < 0 || idx >= order.length - 1) {
62978
+ return null;
62979
+ }
62980
+ return order[idx + 1];
62981
+ }
62982
+ async function upsertModel(db, model) {
62983
+ await db.execute({
62984
+ sql: `INSERT INTO model_pricing (id, display_name, premium_multiplier, token_input_multiplier, token_output_multiplier, cached_input_multiplier, tier, available, updated_at)
62985
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
62986
+ ON CONFLICT(id) DO UPDATE SET
62987
+ display_name = excluded.display_name,
62988
+ premium_multiplier = COALESCE(excluded.premium_multiplier, model_pricing.premium_multiplier),
62989
+ token_input_multiplier = COALESCE(excluded.token_input_multiplier, model_pricing.token_input_multiplier),
62990
+ token_output_multiplier = COALESCE(excluded.token_output_multiplier, model_pricing.token_output_multiplier),
62991
+ cached_input_multiplier = COALESCE(excluded.cached_input_multiplier, model_pricing.cached_input_multiplier),
62992
+ tier = excluded.tier,
62993
+ available = excluded.available,
62994
+ updated_at = excluded.updated_at`,
62995
+ args: [
62996
+ model.id,
62997
+ model.displayName,
62998
+ model.premiumMultiplier,
62999
+ model.tokenInputMultiplier,
63000
+ model.tokenOutputMultiplier,
63001
+ model.cachedInputMultiplier,
63002
+ model.tier,
63003
+ model.available ? 1 : 0,
63004
+ model.updatedAt
63005
+ ]
63006
+ });
63007
+ }
63008
+ function rowToModelPricing(row) {
63009
+ return {
63010
+ id: asString(row.id),
63011
+ displayName: asString(row.display_name),
63012
+ premiumMultiplier: asNullableNumber(row.premium_multiplier),
63013
+ tokenInputMultiplier: asNullableNumber(row.token_input_multiplier),
63014
+ tokenOutputMultiplier: asNullableNumber(row.token_output_multiplier),
63015
+ cachedInputMultiplier: asNullableNumber(row.cached_input_multiplier),
63016
+ tier: asString(row.tier),
63017
+ available: asNumber(row.available) === 1,
63018
+ updatedAt: asString(row.updated_at)
63019
+ };
63020
+ }
63021
+ function findClosestKey(map2, targetKey) {
63022
+ for (const [key, value] of map2) {
63023
+ if (key.includes(targetKey) || targetKey.includes(key)) {
63024
+ return value;
63025
+ }
63026
+ }
63027
+ return void 0;
63028
+ }
63029
+ var init_registry = __esm({
63030
+ "packages/daemon/src/models/registry.ts"() {
63031
+ "use strict";
63032
+ init_db();
63033
+ init_catalog();
63034
+ init_pricing_scraper();
63035
+ init_types();
63036
+ }
63037
+ });
63038
+
62209
63039
  // node_modules/grammy/out/filter.js
62210
63040
  var require_filter = __commonJS({
62211
63041
  "node_modules/grammy/out/filter.js"(exports2) {
@@ -76932,344 +77762,11 @@ var TypedEventBus = class extends EventEmitter {
76932
77762
  };
76933
77763
  var eventBus = new TypedEventBus();
76934
77764
 
76935
- // packages/daemon/src/store/db.ts
76936
- init_paths();
76937
- import { mkdir } from "node:fs/promises";
76938
- import { dirname } from "node:path";
76939
- import { pathToFileURL } from "node:url";
76940
- import { createClient } from "@libsql/client";
76941
- var MIGRATIONS = [
76942
- {
76943
- version: 1,
76944
- name: "create-store-schema",
76945
- statements: [
76946
- `CREATE TABLE IF NOT EXISTS squads (
76947
- id TEXT PRIMARY KEY,
76948
- name TEXT NOT NULL,
76949
- repo_url TEXT NOT NULL,
76950
- repo_owner TEXT NOT NULL,
76951
- repo_name TEXT NOT NULL,
76952
- status TEXT NOT NULL,
76953
- config TEXT NOT NULL,
76954
- created_at TEXT NOT NULL,
76955
- updated_at TEXT NOT NULL,
76956
- UNIQUE (repo_owner, repo_name)
76957
- )`,
76958
- `CREATE TABLE IF NOT EXISTS squad_members (
76959
- id TEXT PRIMARY KEY,
76960
- squad_id TEXT NOT NULL REFERENCES squads(id) ON DELETE CASCADE,
76961
- role TEXT NOT NULL,
76962
- name TEXT NOT NULL,
76963
- system_prompt TEXT NOT NULL,
76964
- model TEXT,
76965
- created_at TEXT NOT NULL
76966
- )`,
76967
- `CREATE TABLE IF NOT EXISTS objectives (
76968
- id TEXT PRIMARY KEY,
76969
- squad_id TEXT NOT NULL REFERENCES squads(id) ON DELETE CASCADE,
76970
- description TEXT NOT NULL,
76971
- status TEXT NOT NULL,
76972
- plan TEXT,
76973
- revision_count INTEGER NOT NULL DEFAULT 0,
76974
- branch TEXT,
76975
- pr_url TEXT,
76976
- created_at TEXT NOT NULL,
76977
- updated_at TEXT NOT NULL
76978
- )`,
76979
- `CREATE TABLE IF NOT EXISTS tasks (
76980
- id TEXT PRIMARY KEY,
76981
- objective_id TEXT NOT NULL REFERENCES objectives(id) ON DELETE CASCADE,
76982
- assignee_id TEXT REFERENCES squad_members(id) ON DELETE SET NULL,
76983
- title TEXT NOT NULL,
76984
- description TEXT NOT NULL,
76985
- status TEXT NOT NULL,
76986
- result TEXT,
76987
- created_at TEXT NOT NULL,
76988
- updated_at TEXT NOT NULL
76989
- )`,
76990
- `CREATE TABLE IF NOT EXISTS conversations (
76991
- id TEXT PRIMARY KEY,
76992
- title TEXT,
76993
- source TEXT NOT NULL,
76994
- created_at TEXT NOT NULL,
76995
- updated_at TEXT NOT NULL
76996
- )`,
76997
- `CREATE TABLE IF NOT EXISTS messages (
76998
- id TEXT PRIMARY KEY,
76999
- conversation_id TEXT NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
77000
- role TEXT NOT NULL,
77001
- content TEXT NOT NULL,
77002
- model TEXT,
77003
- input_tokens INTEGER,
77004
- output_tokens INTEGER,
77005
- created_at TEXT NOT NULL
77006
- )`,
77007
- `CREATE TABLE IF NOT EXISTS inbox (
77008
- id TEXT PRIMARY KEY,
77009
- squad_id TEXT REFERENCES squads(id) ON DELETE SET NULL,
77010
- objective_id TEXT REFERENCES objectives(id) ON DELETE SET NULL,
77011
- type TEXT NOT NULL,
77012
- title TEXT NOT NULL,
77013
- content TEXT NOT NULL,
77014
- status TEXT NOT NULL,
77015
- reply TEXT,
77016
- created_at TEXT NOT NULL,
77017
- updated_at TEXT NOT NULL
77018
- )`,
77019
- `CREATE TABLE IF NOT EXISTS schedules (
77020
- id TEXT PRIMARY KEY,
77021
- name TEXT NOT NULL,
77022
- cron_expression TEXT NOT NULL,
77023
- prompt TEXT NOT NULL,
77024
- enabled INTEGER NOT NULL DEFAULT 1,
77025
- last_run_at TEXT,
77026
- next_run_at TEXT,
77027
- created_at TEXT NOT NULL,
77028
- updated_at TEXT NOT NULL
77029
- )`,
77030
- `CREATE TABLE IF NOT EXISTS token_usage (
77031
- id TEXT PRIMARY KEY,
77032
- squad_id TEXT REFERENCES squads(id) ON DELETE SET NULL,
77033
- agent_id TEXT,
77034
- model TEXT NOT NULL,
77035
- input_tokens INTEGER NOT NULL,
77036
- output_tokens INTEGER NOT NULL,
77037
- cost REAL NOT NULL,
77038
- created_at TEXT NOT NULL
77039
- )`,
77040
- `CREATE TABLE IF NOT EXISTS activity (
77041
- id TEXT PRIMARY KEY,
77042
- squad_id TEXT REFERENCES squads(id) ON DELETE SET NULL,
77043
- objective_id TEXT REFERENCES objectives(id) ON DELETE SET NULL,
77044
- event TEXT NOT NULL,
77045
- description TEXT NOT NULL,
77046
- metadata TEXT,
77047
- created_at TEXT NOT NULL
77048
- )`,
77049
- `CREATE TABLE IF NOT EXISTS agent_history (
77050
- id TEXT PRIMARY KEY,
77051
- agent_id TEXT NOT NULL,
77052
- squad_id TEXT REFERENCES squads(id) ON DELETE SET NULL,
77053
- content TEXT NOT NULL,
77054
- created_at TEXT NOT NULL
77055
- )`,
77056
- `CREATE TABLE IF NOT EXISTS settings (
77057
- key TEXT PRIMARY KEY,
77058
- value TEXT
77059
- )`,
77060
- "CREATE INDEX IF NOT EXISTS idx_squad_members_squad_id ON squad_members(squad_id)",
77061
- "CREATE INDEX IF NOT EXISTS idx_objectives_squad_id ON objectives(squad_id)",
77062
- "CREATE INDEX IF NOT EXISTS idx_objectives_status ON objectives(status)",
77063
- "CREATE INDEX IF NOT EXISTS idx_tasks_objective_id ON tasks(objective_id)",
77064
- "CREATE INDEX IF NOT EXISTS idx_tasks_assignee_id ON tasks(assignee_id)",
77065
- "CREATE INDEX IF NOT EXISTS idx_messages_conversation_id_created_at ON messages(conversation_id, created_at)",
77066
- "CREATE INDEX IF NOT EXISTS idx_inbox_status_created_at ON inbox(status, created_at)",
77067
- "CREATE INDEX IF NOT EXISTS idx_inbox_squad_id ON inbox(squad_id)",
77068
- "CREATE INDEX IF NOT EXISTS idx_inbox_objective_id ON inbox(objective_id)",
77069
- "CREATE INDEX IF NOT EXISTS idx_schedules_enabled_next_run_at ON schedules(enabled, next_run_at)",
77070
- "CREATE INDEX IF NOT EXISTS idx_token_usage_created_at ON token_usage(created_at)",
77071
- "CREATE INDEX IF NOT EXISTS idx_token_usage_squad_id ON token_usage(squad_id)",
77072
- "CREATE INDEX IF NOT EXISTS idx_token_usage_agent_id ON token_usage(agent_id)",
77073
- "CREATE INDEX IF NOT EXISTS idx_token_usage_model ON token_usage(model)",
77074
- "CREATE INDEX IF NOT EXISTS idx_activity_created_at ON activity(created_at)",
77075
- "CREATE INDEX IF NOT EXISTS idx_activity_squad_id ON activity(squad_id)",
77076
- "CREATE INDEX IF NOT EXISTS idx_activity_objective_id ON activity(objective_id)",
77077
- "CREATE INDEX IF NOT EXISTS idx_agent_history_agent_id_created_at ON agent_history(agent_id, created_at)"
77078
- ]
77079
- },
77080
- {
77081
- version: 2,
77082
- name: "add-model-pricing-and-dual-costs",
77083
- statements: [
77084
- `CREATE TABLE IF NOT EXISTS model_pricing (
77085
- id TEXT PRIMARY KEY,
77086
- display_name TEXT NOT NULL,
77087
- premium_multiplier REAL,
77088
- token_input_multiplier REAL,
77089
- token_output_multiplier REAL,
77090
- cached_input_multiplier REAL,
77091
- tier TEXT NOT NULL,
77092
- available INTEGER NOT NULL DEFAULT 1,
77093
- updated_at TEXT NOT NULL
77094
- )`,
77095
- "CREATE INDEX IF NOT EXISTS idx_model_pricing_tier ON model_pricing(tier)",
77096
- "CREATE INDEX IF NOT EXISTS idx_model_pricing_available ON model_pricing(available)",
77097
- "ALTER TABLE token_usage ADD COLUMN premium_request_cost REAL",
77098
- "ALTER TABLE token_usage ADD COLUMN token_unit_cost REAL"
77099
- ]
77100
- },
77101
- {
77102
- version: 3,
77103
- name: "add-squad-instances",
77104
- statements: [
77105
- `CREATE TABLE IF NOT EXISTS squad_instances (
77106
- id TEXT PRIMARY KEY,
77107
- squad_id TEXT NOT NULL REFERENCES squads(id) ON DELETE CASCADE,
77108
- objective_id TEXT REFERENCES objectives(id) ON DELETE SET NULL,
77109
- status TEXT NOT NULL DEFAULT 'queued',
77110
- branch TEXT,
77111
- worktree_path TEXT,
77112
- created_at TEXT NOT NULL,
77113
- started_at TEXT,
77114
- completed_at TEXT,
77115
- error TEXT
77116
- )`,
77117
- "CREATE INDEX IF NOT EXISTS idx_squad_instances_squad_status ON squad_instances(squad_id, status)",
77118
- "CREATE INDEX IF NOT EXISTS idx_squad_instances_status ON squad_instances(status)",
77119
- "CREATE INDEX IF NOT EXISTS idx_squad_instances_objective_id ON squad_instances(objective_id)"
77120
- ]
77121
- },
77122
- {
77123
- version: 4,
77124
- name: "denormalize-usage-names",
77125
- statements: [
77126
- "ALTER TABLE token_usage ADD COLUMN squad_name TEXT",
77127
- "ALTER TABLE token_usage ADD COLUMN agent_name TEXT",
77128
- `UPDATE token_usage SET
77129
- squad_name = (SELECT s.name FROM squads s WHERE s.id = token_usage.squad_id),
77130
- agent_name = (SELECT sm.name FROM squad_members sm WHERE sm.id = token_usage.agent_id)
77131
- WHERE squad_id IS NOT NULL OR agent_id IS NOT NULL`
77132
- ]
77133
- },
77134
- {
77135
- version: 5,
77136
- name: "add-squad-soft-delete",
77137
- statements: ["ALTER TABLE squads ADD COLUMN deleted_at TEXT"]
77138
- }
77139
- ];
77140
- var client = null;
77141
- var initPromise = null;
77142
- var defaultConnectionOptions = {};
77143
- var generateId = () => crypto.randomUUID();
77144
- var nowIso = () => (/* @__PURE__ */ new Date()).toISOString();
77145
- function asString(value) {
77146
- if (typeof value === "string") {
77147
- return value;
77148
- }
77149
- if (value === null || value === void 0) {
77150
- throw new Error("Expected string value but received null or undefined");
77151
- }
77152
- return String(value);
77153
- }
77154
- function asNullableString(value) {
77155
- if (value === null || value === void 0) {
77156
- return null;
77157
- }
77158
- return typeof value === "string" ? value : String(value);
77159
- }
77160
- function asNumber(value) {
77161
- if (typeof value === "number") {
77162
- return value;
77163
- }
77164
- if (typeof value === "bigint") {
77165
- return Number(value);
77166
- }
77167
- if (typeof value === "string") {
77168
- return Number(value);
77169
- }
77170
- throw new Error(`Expected numeric value but received ${typeof value}`);
77171
- }
77172
- function asNullableNumber(value) {
77173
- if (value === null || value === void 0) {
77174
- return null;
77175
- }
77176
- return asNumber(value);
77177
- }
77178
- function asBoolean(value) {
77179
- return asNumber(value) === 1;
77180
- }
77181
- function toSqliteBoolean(value) {
77182
- return value ? 1 : 0;
77183
- }
77184
- function parseJson(value) {
77185
- if (typeof value !== "string") {
77186
- return value;
77187
- }
77188
- return JSON.parse(value);
77189
- }
77190
- function serializeJson(value) {
77191
- return JSON.stringify(value);
77192
- }
77193
- async function initDatabase() {
77194
- if (client && !client.closed) {
77195
- return client;
77196
- }
77197
- if (initPromise) {
77198
- return initPromise;
77199
- }
77200
- initPromise = initializeDatabase(defaultConnectionOptions);
77201
- try {
77202
- client = await initPromise;
77203
- return client;
77204
- } finally {
77205
- initPromise = null;
77206
- }
77207
- }
77208
- async function getDatabase() {
77209
- return initDatabase();
77210
- }
77211
- async function initializeDatabase(options2 = {}) {
77212
- const url2 = await resolveDatabaseUrl(options2);
77213
- const databaseClient = createClient({ url: url2 });
77214
- await databaseClient.execute("PRAGMA foreign_keys = ON");
77215
- await databaseClient.execute("PRAGMA journal_mode = WAL");
77216
- await ensureMigrationTable(databaseClient);
77217
- await applyMigrations(databaseClient);
77218
- return databaseClient;
77219
- }
77220
- async function resolveDatabaseUrl(options2) {
77221
- if (options2.url) {
77222
- return options2.url;
77223
- }
77224
- const databasePath = options2.path ?? DB_PATH;
77225
- await mkdir(dirname(databasePath), { recursive: true });
77226
- return pathToFileURL(databasePath).href;
77227
- }
77228
- async function ensureMigrationTable(databaseClient) {
77229
- await databaseClient.executeMultiple(`
77230
- CREATE TABLE IF NOT EXISTS schema_migrations (
77231
- version INTEGER PRIMARY KEY,
77232
- name TEXT NOT NULL,
77233
- applied_at TEXT NOT NULL
77234
- );
77235
- `);
77236
- }
77237
- async function applyMigrations(databaseClient) {
77238
- const appliedVersions = await getAppliedVersions(databaseClient);
77239
- for (const migration of MIGRATIONS) {
77240
- if (appliedVersions.has(migration.version)) {
77241
- continue;
77242
- }
77243
- const transaction = await databaseClient.transaction("write");
77244
- try {
77245
- for (const statement of migration.statements) {
77246
- await transaction.execute(statement);
77247
- }
77248
- await transaction.execute({
77249
- sql: "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",
77250
- args: [migration.version, migration.name, nowIso()]
77251
- });
77252
- await transaction.commit();
77253
- } catch (error51) {
77254
- if (!transaction.closed) {
77255
- await transaction.rollback();
77256
- }
77257
- throw error51;
77258
- } finally {
77259
- if (!transaction.closed) {
77260
- transaction.close();
77261
- }
77262
- }
77263
- }
77264
- }
77265
- async function getAppliedVersions(databaseClient) {
77266
- const result = await databaseClient.execute(
77267
- "SELECT version FROM schema_migrations ORDER BY version ASC"
77268
- );
77269
- return new Set(result.rows.map((row) => asNumber(row.version)));
77270
- }
77765
+ // packages/daemon/src/store/index.ts
77766
+ init_db();
77271
77767
 
77272
77768
  // packages/daemon/src/store/squads.ts
77769
+ init_db();
77273
77770
  async function createSquad(data, db) {
77274
77771
  const database = db ?? await getDatabase();
77275
77772
  const timestamp = nowIso();
@@ -77525,6 +78022,7 @@ function mapMember(row) {
77525
78022
  }
77526
78023
 
77527
78024
  // packages/daemon/src/store/conversations.ts
78025
+ init_db();
77528
78026
  async function createConversation(source, title, db) {
77529
78027
  const database = db ?? await getDatabase();
77530
78028
  const createdAt = nowIso();
@@ -77643,6 +78141,7 @@ function mapMessage(row) {
77643
78141
  }
77644
78142
 
77645
78143
  // packages/daemon/src/store/inbox.ts
78144
+ init_db();
77646
78145
  async function createInboxItem(data, db) {
77647
78146
  const database = db ?? await getDatabase();
77648
78147
  const timestamp = nowIso();
@@ -77738,6 +78237,7 @@ function mapInboxItem(row) {
77738
78237
 
77739
78238
  // packages/daemon/src/store/schedules.ts
77740
78239
  var import_cron_parser = __toESM(require_dist(), 1);
78240
+ init_db();
77741
78241
  var parseExpression = import_cron_parser.CronExpressionParser.parse;
77742
78242
  async function createSchedule(data, db) {
77743
78243
  const database = db ?? await getDatabase();
@@ -77876,6 +78376,7 @@ function mapSchedule(row) {
77876
78376
  }
77877
78377
 
77878
78378
  // packages/daemon/src/store/token-usage.ts
78379
+ init_db();
77879
78380
  async function recordUsage(data, db) {
77880
78381
  const database = db ?? await getDatabase();
77881
78382
  const usage = {
@@ -78005,6 +78506,7 @@ async function getUsageRecords(params = {}, db) {
78005
78506
  }
78006
78507
 
78007
78508
  // packages/daemon/src/store/activity.ts
78509
+ init_db();
78008
78510
  async function logActivity(data, db) {
78009
78511
  const database = db ?? await getDatabase();
78010
78512
  const activity = {
@@ -78060,6 +78562,7 @@ function mapActivity(row) {
78060
78562
  }
78061
78563
 
78062
78564
  // packages/daemon/src/store/agent-history.ts
78565
+ init_db();
78063
78566
  async function appendHistory(agentId, squadId, content, db) {
78064
78567
  const database = db ?? await getDatabase();
78065
78568
  const entry = {
@@ -78095,6 +78598,7 @@ function mapAgentHistory(row) {
78095
78598
  }
78096
78599
 
78097
78600
  // packages/daemon/src/store/objectives.ts
78601
+ init_db();
78098
78602
  async function createObjective(squadId, description, db) {
78099
78603
  const database = db ?? await getDatabase();
78100
78604
  const timestamp = nowIso();
@@ -78287,6 +78791,7 @@ function mapTask(row) {
78287
78791
  }
78288
78792
 
78289
78793
  // packages/daemon/src/store/instances.ts
78794
+ init_db();
78290
78795
  async function createInstance(input, db) {
78291
78796
  const database = db ?? await getDatabase();
78292
78797
  const instance = {
@@ -79580,6 +80085,7 @@ function emitInstanceEvent(event, instance) {
79580
80085
  }
79581
80086
 
79582
80087
  // packages/daemon/src/api/routes/squads.ts
80088
+ init_db();
79583
80089
  var router7 = (0, import_express7.Router)();
79584
80090
  var DEFAULT_CONFIG = {
79585
80091
  prMode: "draft-pr",
@@ -80838,432 +81344,10 @@ function ensureDataDirectories() {
80838
81344
  }
80839
81345
  }
80840
81346
 
80841
- // packages/daemon/src/models/catalog.ts
80842
- import { execFileSync } from "node:child_process";
80843
- var CATALOG_URL = "https://models.github.ai/catalog/models";
80844
- function resolveGitHubToken() {
80845
- const envToken = process.env.GITHUB_TOKEN?.trim();
80846
- if (envToken && envToken.length > 0) {
80847
- return envToken;
80848
- }
80849
- try {
80850
- const token = execFileSync("gh", ["auth", "token"], {
80851
- encoding: "utf8",
80852
- stdio: ["ignore", "pipe", "pipe"]
80853
- }).trim();
80854
- return token.length > 0 ? token : void 0;
80855
- } catch {
80856
- return void 0;
80857
- }
80858
- }
80859
- async function fetchModelCatalog() {
80860
- const token = resolveGitHubToken();
80861
- if (!token) {
80862
- throw new Error("No GitHub token available for model catalog fetch");
80863
- }
80864
- const response = await fetch(CATALOG_URL, {
80865
- headers: {
80866
- Accept: "application/json",
80867
- Authorization: `Bearer ${token}`,
80868
- "X-GitHub-Api-Version": "2024-12-01"
80869
- }
80870
- });
80871
- if (!response.ok) {
80872
- throw new Error(`Model catalog fetch failed: ${response.status} ${response.statusText}`);
80873
- }
80874
- const data = await response.json();
80875
- if (!Array.isArray(data)) {
80876
- throw new Error("Model catalog response is not an array");
80877
- }
80878
- const models = [];
80879
- for (const entry of data) {
80880
- const id = entry.id ?? entry.name;
80881
- const displayName = entry.friendly_name ?? entry.name ?? id;
80882
- if (id && typeof id === "string") {
80883
- models.push({ id, displayName: displayName ?? id });
80884
- }
80885
- }
80886
- return models;
80887
- }
80888
-
80889
- // packages/daemon/src/models/pricing-scraper.ts
80890
- var TOKEN_UNIT_COSTS_URL = "https://docs.github.com/en/billing/reference/costs-for-github-models";
80891
- var PREMIUM_MULTIPLIERS_URL = "https://docs.github.com/en/copilot/reference/copilot-billing/request-based-billing-legacy/model-multipliers-for-annual-plans";
80892
- var COPILOT_PRICING_URL = "https://docs.github.com/api/article/body?pathname=/en/copilot/reference/copilot-billing/models-and-pricing";
80893
- var PRICE_TO_MULTIPLIER = 0.1;
80894
- async function scrapeTokenUnitPricing() {
80895
- const html = await fetchPage(TOKEN_UNIT_COSTS_URL);
80896
- return parseTokenUnitTable(html);
80897
- }
80898
- async function scrapeCopilotPricing() {
80899
- const markdown = await fetchMarkdown(COPILOT_PRICING_URL);
80900
- return parseCopilotPricingMarkdown(markdown);
80901
- }
80902
- async function scrapePremiumRequestPricing() {
80903
- const html = await fetchPage(PREMIUM_MULTIPLIERS_URL);
80904
- return parsePremiumMultiplierTable(html);
80905
- }
80906
- async function fetchPage(url2) {
80907
- const response = await fetch(url2, {
80908
- headers: {
80909
- Accept: "text/html",
80910
- "User-Agent": "IO-Daemon/4.0 (pricing-refresh)"
80911
- },
80912
- redirect: "follow"
80913
- });
80914
- if (!response.ok) {
80915
- throw new Error(`Failed to fetch ${url2}: ${response.status} ${response.statusText}`);
80916
- }
80917
- return response.text();
80918
- }
80919
- async function fetchMarkdown(url2) {
80920
- const response = await fetch(url2, {
80921
- headers: {
80922
- Accept: "text/markdown, text/plain, */*",
80923
- "User-Agent": "IO-Daemon/4.0 (pricing-refresh)"
80924
- },
80925
- redirect: "follow"
80926
- });
80927
- if (!response.ok) {
80928
- throw new Error(`Failed to fetch ${url2}: ${response.status} ${response.statusText}`);
80929
- }
80930
- return response.text();
80931
- }
80932
- function parseTokenUnitTable(html) {
80933
- const results = [];
80934
- const tableRowPattern = /<tr[^>]*>\s*<td[^>]*>([^<]+)<\/td>\s*<td[^>]*>([^<]+)<\/td>\s*<td[^>]*>([^<]*)<\/td>\s*<td[^>]*>([^<]+)<\/td>/gi;
80935
- for (const match of html.matchAll(tableRowPattern)) {
80936
- const modelName = cleanCellText(match[1]);
80937
- const inputMultiplier = Number.parseFloat(match[2]);
80938
- const cachedInput = match[3].trim().toLowerCase();
80939
- const outputMultiplier = Number.parseFloat(match[4]);
80940
- if (modelName && !Number.isNaN(inputMultiplier) && !Number.isNaN(outputMultiplier)) {
80941
- results.push({
80942
- modelName,
80943
- inputMultiplier,
80944
- cachedInputMultiplier: cachedInput === "n/a" || cachedInput === "" ? null : Number.parseFloat(cachedInput) || null,
80945
- outputMultiplier
80946
- });
80947
- }
80948
- }
80949
- return results;
80950
- }
80951
- function parsePremiumMultiplierTable(html) {
80952
- const results = [];
80953
- const tableRowPattern = /<tr[^>]*>\s*<td[^>]*>([^<]+)<\/td>\s*<td[^>]*>([^<]+)<\/td>\s*<\/tr>/gi;
80954
- for (const match of html.matchAll(tableRowPattern)) {
80955
- const modelName = cleanCellText(match[1]);
80956
- const multiplier = Number.parseFloat(match[2]);
80957
- if (modelName && !Number.isNaN(multiplier) && multiplier > 0) {
80958
- results.push({ modelName, multiplier });
80959
- }
80960
- }
80961
- return results;
80962
- }
80963
- function cleanCellText(text) {
80964
- return text.replace(/<[^>]*>/g, "").replace(/&[^;]+;/g, " ").trim();
80965
- }
80966
- function parseCopilotPricingMarkdown(markdown) {
80967
- const results = [];
80968
- const seenModels = /* @__PURE__ */ new Set();
80969
- const lines = markdown.split("\n");
80970
- let columnIndices = null;
80971
- for (const line of lines) {
80972
- const trimmed = line.trim();
80973
- if (!trimmed.startsWith("|")) continue;
80974
- if (/\|\s*Model\s/i.test(trimmed)) {
80975
- columnIndices = parseHeaderColumns(trimmed);
80976
- continue;
80977
- }
80978
- if (/^[\s|:-]+$/.test(trimmed) || !columnIndices) continue;
80979
- const entry = parseDataRow(trimmed, columnIndices);
80980
- if (entry && !seenModels.has(entry.modelName.toLowerCase())) {
80981
- seenModels.add(entry.modelName.toLowerCase());
80982
- results.push(entry);
80983
- }
80984
- }
80985
- return results;
80986
- }
80987
- function parseDataRow(line, columnIndices) {
80988
- const cells = splitMarkdownRow(line);
80989
- if (cells.length <= columnIndices.output) return null;
80990
- const modelName = cells[columnIndices.model].trim();
80991
- const inputPrice = parseDollarValue(cells[columnIndices.input]);
80992
- const cachedPrice = columnIndices.cached >= 0 ? parseDollarValue(cells[columnIndices.cached]) : null;
80993
- const outputPrice = parseDollarValue(cells[columnIndices.output]);
80994
- if (!modelName || inputPrice === null || outputPrice === null) return null;
80995
- return {
80996
- modelName,
80997
- inputMultiplier: inputPrice * PRICE_TO_MULTIPLIER,
80998
- cachedInputMultiplier: cachedPrice !== null ? cachedPrice * PRICE_TO_MULTIPLIER : null,
80999
- outputMultiplier: outputPrice * PRICE_TO_MULTIPLIER
81000
- };
81001
- }
81002
- function parseHeaderColumns(headerLine) {
81003
- const cells = splitMarkdownRow(headerLine).map((c) => c.trim().toLowerCase());
81004
- const model = cells.findIndex((c) => c === "model" || c === "model name");
81005
- const input = cells.findIndex(
81006
- (c) => (c === "input" || c === "input multiplier") && !c.includes("cached")
81007
- );
81008
- const cached2 = cells.findIndex(
81009
- (c) => c.includes("cached input") || c === "cached input multiplier"
81010
- );
81011
- const output = cells.findIndex(
81012
- (c) => (c === "output" || c === "output multiplier") && !c.includes("cached")
81013
- );
81014
- if (model < 0 || input < 0 || output < 0) {
81015
- return null;
81016
- }
81017
- return { model, input, cached: cached2 >= 0 ? cached2 : -1, output };
81018
- }
81019
- function splitMarkdownRow(line) {
81020
- const parts = line.split("|");
81021
- if (parts[0].trim() === "") parts.shift();
81022
- if (parts[parts.length - 1]?.trim() === "") parts.pop();
81023
- return parts;
81024
- }
81025
- function parseDollarValue(cell) {
81026
- if (!cell) return null;
81027
- const cleaned = cell.trim().replace(/[$,]/g, "");
81028
- if (cleaned.toLowerCase() === "n/a" || cleaned === "" || cleaned === "-") {
81029
- return null;
81030
- }
81031
- const value = Number.parseFloat(cleaned);
81032
- return Number.isNaN(value) ? null : value;
81033
- }
81034
-
81035
- // packages/daemon/src/models/types.ts
81036
- var TIER_RANGES = {
81037
- trivial: { min: 0, max: 0.33 },
81038
- fast: { min: 0.34, max: 1 },
81039
- standard: { min: 1.1, max: 5 },
81040
- premium: { min: 5.1, max: 15 },
81041
- ultra: { min: 15.1, max: Number.POSITIVE_INFINITY }
81042
- };
81043
- function computeTierFromMultiplier(premiumMultiplier) {
81044
- if (premiumMultiplier === null) {
81045
- return "standard";
81046
- }
81047
- for (const [tier, range] of Object.entries(TIER_RANGES)) {
81048
- if (premiumMultiplier >= range.min && premiumMultiplier <= range.max) {
81049
- return tier;
81050
- }
81051
- }
81052
- return "ultra";
81053
- }
81054
- var TOKEN_UNIT_PRICE = 1e-5;
81055
-
81056
- // packages/daemon/src/models/registry.ts
81057
- function normalizeModelName(name) {
81058
- return name.toLowerCase().replace(/^openai\s+/i, "").replace(/\s+/g, "-").replace(/[^a-z0-9.\-]/g, "").trim();
81059
- }
81060
- async function fetchCatalogIntoMap(modelMap, result, logger2) {
81061
- try {
81062
- const catalogModels = await fetchModelCatalog();
81063
- result.catalogFetched = true;
81064
- for (const m of catalogModels) {
81065
- const key = normalizeModelName(m.id);
81066
- modelMap.set(key, { id: m.id, displayName: m.displayName, available: true });
81067
- }
81068
- } catch (error51) {
81069
- const msg = error51 instanceof Error ? error51.message : String(error51);
81070
- result.errors.push(`Catalog fetch failed: ${msg}`);
81071
- logger2?.warn(`Model catalog fetch failed: ${msg}`);
81072
- }
81073
- }
81074
- async function scrapeTokenPricingIntoMap(modelMap, result, logger2) {
81075
- try {
81076
- const tokenPricing = await scrapeTokenUnitPricing();
81077
- result.tokenPricingScraped = true;
81078
- for (const tp of tokenPricing) {
81079
- const key = normalizeModelName(tp.modelName);
81080
- const existing = modelMap.get(key) ?? findClosestKey(modelMap, key);
81081
- if (existing) {
81082
- existing.tokenInputMultiplier = tp.inputMultiplier;
81083
- existing.tokenOutputMultiplier = tp.outputMultiplier;
81084
- existing.cachedInputMultiplier = tp.cachedInputMultiplier;
81085
- } else {
81086
- modelMap.set(key, {
81087
- id: key,
81088
- displayName: tp.modelName,
81089
- tokenInputMultiplier: tp.inputMultiplier,
81090
- tokenOutputMultiplier: tp.outputMultiplier,
81091
- cachedInputMultiplier: tp.cachedInputMultiplier,
81092
- available: true
81093
- });
81094
- }
81095
- }
81096
- } catch (error51) {
81097
- const msg = error51 instanceof Error ? error51.message : String(error51);
81098
- result.errors.push(`Token pricing scrape failed: ${msg}`);
81099
- logger2?.warn(`Token pricing scrape failed: ${msg}`);
81100
- }
81101
- }
81102
- async function scrapePremiumPricingIntoMap(modelMap, result, logger2) {
81103
- try {
81104
- const premiumPricing = await scrapePremiumRequestPricing();
81105
- result.premiumPricingScraped = true;
81106
- for (const pp of premiumPricing) {
81107
- const key = normalizeModelName(pp.modelName);
81108
- const existing = modelMap.get(key) ?? findClosestKey(modelMap, key);
81109
- if (existing) {
81110
- existing.premiumMultiplier = pp.multiplier;
81111
- } else {
81112
- modelMap.set(key, {
81113
- id: key,
81114
- displayName: pp.modelName,
81115
- premiumMultiplier: pp.multiplier,
81116
- available: true
81117
- });
81118
- }
81119
- }
81120
- } catch (error51) {
81121
- const msg = error51 instanceof Error ? error51.message : String(error51);
81122
- result.errors.push(`Premium pricing scrape failed: ${msg}`);
81123
- logger2?.warn(`Premium pricing scrape failed: ${msg}`);
81124
- }
81125
- }
81126
- async function scrapeCopilotPricingIntoMap(modelMap, result, logger2) {
81127
- try {
81128
- const copilotPricing = await scrapeCopilotPricing();
81129
- result.copilotPricingScraped = true;
81130
- for (const cp of copilotPricing) {
81131
- const key = normalizeModelName(cp.modelName);
81132
- const existing = modelMap.get(key) ?? findClosestKey(modelMap, key);
81133
- if (existing) {
81134
- existing.tokenInputMultiplier = cp.inputMultiplier;
81135
- existing.tokenOutputMultiplier = cp.outputMultiplier;
81136
- if (cp.cachedInputMultiplier !== null) {
81137
- existing.cachedInputMultiplier = cp.cachedInputMultiplier;
81138
- }
81139
- } else {
81140
- modelMap.set(key, {
81141
- id: key,
81142
- displayName: cp.modelName,
81143
- tokenInputMultiplier: cp.inputMultiplier,
81144
- tokenOutputMultiplier: cp.outputMultiplier,
81145
- cachedInputMultiplier: cp.cachedInputMultiplier,
81146
- available: true
81147
- });
81148
- }
81149
- }
81150
- } catch (error51) {
81151
- const msg = error51 instanceof Error ? error51.message : String(error51);
81152
- result.errors.push(`Copilot pricing scrape failed: ${msg}`);
81153
- logger2?.warn(`Copilot pricing scrape failed: ${msg}`);
81154
- }
81155
- }
81156
- async function refreshModelPricing(logger2) {
81157
- const result = {
81158
- modelsUpdated: 0,
81159
- catalogFetched: false,
81160
- tokenPricingScraped: false,
81161
- premiumPricingScraped: false,
81162
- copilotPricingScraped: false,
81163
- errors: []
81164
- };
81165
- const modelMap = /* @__PURE__ */ new Map();
81166
- await fetchCatalogIntoMap(modelMap, result, logger2);
81167
- await scrapeTokenPricingIntoMap(modelMap, result, logger2);
81168
- await scrapeCopilotPricingIntoMap(modelMap, result, logger2);
81169
- await scrapePremiumPricingIntoMap(modelMap, result, logger2);
81170
- if (!result.catalogFetched && !result.tokenPricingScraped && !result.premiumPricingScraped && !result.copilotPricingScraped) {
81171
- logger2?.warn("All pricing sources failed, no models available");
81172
- return result;
81173
- }
81174
- const db = await getDatabase();
81175
- const now = nowIso();
81176
- for (const model of modelMap.values()) {
81177
- const tier = computeTierFromMultiplier(model.premiumMultiplier ?? null);
81178
- await upsertModel(db, {
81179
- id: model.id,
81180
- displayName: model.displayName,
81181
- premiumMultiplier: model.premiumMultiplier ?? null,
81182
- tokenInputMultiplier: model.tokenInputMultiplier ?? null,
81183
- tokenOutputMultiplier: model.tokenOutputMultiplier ?? null,
81184
- cachedInputMultiplier: model.cachedInputMultiplier ?? null,
81185
- tier,
81186
- available: model.available ?? true,
81187
- updatedAt: now
81188
- });
81189
- result.modelsUpdated++;
81190
- }
81191
- return result;
81192
- }
81193
- async function getModelPricing(modelId) {
81194
- const db = await getDatabase();
81195
- const result = await db.execute({
81196
- sql: "SELECT * FROM model_pricing WHERE id = ?",
81197
- args: [modelId]
81198
- });
81199
- if (result.rows.length > 0) {
81200
- return rowToModelPricing(result.rows[0]);
81201
- }
81202
- const allModels = await db.execute(
81203
- "SELECT * FROM model_pricing WHERE token_input_multiplier IS NOT NULL ORDER BY length(id) DESC"
81204
- );
81205
- for (const row of allModels.rows) {
81206
- const storedId = asString(row.id);
81207
- if (modelId.startsWith(storedId) || storedId.startsWith(modelId)) {
81208
- return rowToModelPricing(row);
81209
- }
81210
- }
81211
- return null;
81212
- }
81213
- function calculateTokenUnitCost(inputTokens, outputTokens, inputMultiplier, outputMultiplier) {
81214
- if (inputMultiplier === null || outputMultiplier === null) {
81215
- return 0;
81216
- }
81217
- const tokenUnits = inputTokens * inputMultiplier + outputTokens * outputMultiplier;
81218
- return tokenUnits * TOKEN_UNIT_PRICE;
81219
- }
81220
- async function upsertModel(db, model) {
81221
- await db.execute({
81222
- sql: `INSERT INTO model_pricing (id, display_name, premium_multiplier, token_input_multiplier, token_output_multiplier, cached_input_multiplier, tier, available, updated_at)
81223
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
81224
- ON CONFLICT(id) DO UPDATE SET
81225
- display_name = excluded.display_name,
81226
- premium_multiplier = COALESCE(excluded.premium_multiplier, model_pricing.premium_multiplier),
81227
- token_input_multiplier = COALESCE(excluded.token_input_multiplier, model_pricing.token_input_multiplier),
81228
- token_output_multiplier = COALESCE(excluded.token_output_multiplier, model_pricing.token_output_multiplier),
81229
- cached_input_multiplier = COALESCE(excluded.cached_input_multiplier, model_pricing.cached_input_multiplier),
81230
- tier = excluded.tier,
81231
- available = excluded.available,
81232
- updated_at = excluded.updated_at`,
81233
- args: [
81234
- model.id,
81235
- model.displayName,
81236
- model.premiumMultiplier,
81237
- model.tokenInputMultiplier,
81238
- model.tokenOutputMultiplier,
81239
- model.cachedInputMultiplier,
81240
- model.tier,
81241
- model.available ? 1 : 0,
81242
- model.updatedAt
81243
- ]
81244
- });
81245
- }
81246
- function rowToModelPricing(row) {
81247
- return {
81248
- id: asString(row.id),
81249
- displayName: asString(row.display_name),
81250
- premiumMultiplier: asNullableNumber(row.premium_multiplier),
81251
- tokenInputMultiplier: asNullableNumber(row.token_input_multiplier),
81252
- tokenOutputMultiplier: asNullableNumber(row.token_output_multiplier),
81253
- cachedInputMultiplier: asNullableNumber(row.cached_input_multiplier),
81254
- tier: asString(row.tier),
81255
- available: asNumber(row.available) === 1,
81256
- updatedAt: asString(row.updated_at)
81257
- };
81258
- }
81259
- function findClosestKey(map2, targetKey) {
81260
- for (const [key, value] of map2) {
81261
- if (key.includes(targetKey) || targetKey.includes(key)) {
81262
- return value;
81263
- }
81264
- }
81265
- return void 0;
81266
- }
81347
+ // packages/daemon/src/models/index.ts
81348
+ init_catalog();
81349
+ init_registry();
81350
+ init_types();
81267
81351
 
81268
81352
  // packages/daemon/src/orchestrator/system-prompt.ts
81269
81353
  function formatSquadRoster(squads) {
@@ -81597,6 +81681,9 @@ async function sendMessage(session, message2, onChunk) {
81597
81681
  return operation;
81598
81682
  }
81599
81683
 
81684
+ // packages/daemon/src/orchestrator/orchestrator.ts
81685
+ init_registry();
81686
+
81600
81687
  // packages/daemon/src/skills/loader.ts
81601
81688
  var import_gray_matter2 = __toESM(require_gray_matter(), 1);
81602
81689
  init_paths();
@@ -81948,6 +82035,7 @@ var executeCodingToolCall = async (toolName, rawArgs) => {
81948
82035
  // packages/daemon/src/orchestrator/tools/inbox.ts
81949
82036
  init_dist();
81950
82037
  init_zod();
82038
+ init_db();
81951
82039
  var inboxReplySchema = external_exports.object({
81952
82040
  itemId: external_exports.string().trim().min(1),
81953
82041
  reply: external_exports.string().trim().min(1)
@@ -82219,17 +82307,80 @@ async function isSquadAvailable(squadId) {
82219
82307
  }
82220
82308
 
82221
82309
  // packages/daemon/src/execution/agent.ts
82222
- init_dist();
82310
+ init_registry();
82223
82311
  import { exec as exec3 } from "node:child_process";
82224
82312
  import { mkdir as mkdir9, readFile as readFile7, readdir as readdir5, stat as stat3, writeFile as writeFile6 } from "node:fs/promises";
82225
82313
  import { dirname as dirname8, extname as extname3, isAbsolute, join as join11, relative as relative3, resolve as resolve4 } from "node:path";
82226
82314
  import { promisify as promisify3 } from "node:util";
82227
82315
  import {
82228
- CopilotClient as CopilotClient2,
82229
- approveAll as approveAll2,
82316
+ CopilotClient as CopilotClient3,
82317
+ approveAll as approveAll3,
82230
82318
  defineTool
82231
82319
  } from "@github/copilot-sdk";
82232
82320
 
82321
+ // packages/daemon/src/squad/model-selector.ts
82322
+ init_registry();
82323
+ import { CopilotClient as CopilotClient2, approveAll as approveAll2 } from "@github/copilot-sdk";
82324
+ var VALID_TIERS = ["trivial", "fast", "standard", "premium", "ultra"];
82325
+ var CLASSIFICATION_PROMPT = `You are a task complexity classifier. Given a task description, classify its complexity into exactly one tier.
82326
+
82327
+ Tiers (from simplest to most complex):
82328
+ - trivial: Typos, renames, comment changes, config tweaks, formatting
82329
+ - fast: Simple bug fixes, small features, documentation updates, single-file changes
82330
+ - standard: Feature implementation, multi-file changes, moderate refactoring
82331
+ - premium: Architecture changes, complex refactoring, security work, performance optimization
82332
+ - ultra: System-wide redesigns, critical infrastructure, cross-cutting concerns
82333
+
82334
+ Reply with ONLY the tier name (one word, lowercase). Nothing else.`;
82335
+ async function selectModelForTask(taskDescription) {
82336
+ const classifierModel = await getCheapestAvailableModel();
82337
+ if (!classifierModel) {
82338
+ throw new Error("No models available in pricing database");
82339
+ }
82340
+ let tier;
82341
+ try {
82342
+ tier = await classifyTaskComplexity(taskDescription, classifierModel.id);
82343
+ } catch {
82344
+ return classifierModel.id;
82345
+ }
82346
+ const selectedModel = await getCheapestInTier(tier);
82347
+ if (selectedModel) {
82348
+ return selectedModel.id;
82349
+ }
82350
+ const nextTier = getNextTierUp(tier);
82351
+ if (nextTier) {
82352
+ const escalatedModel = await getCheapestInTier(nextTier);
82353
+ if (escalatedModel) {
82354
+ return escalatedModel.id;
82355
+ }
82356
+ }
82357
+ return classifierModel.id;
82358
+ }
82359
+ async function classifyTaskComplexity(taskDescription, modelId) {
82360
+ let client2 = null;
82361
+ try {
82362
+ client2 = new CopilotClient2();
82363
+ await client2.start();
82364
+ const session = await client2.createSession({
82365
+ model: modelId,
82366
+ onPermissionRequest: approveAll2,
82367
+ systemMessage: { content: CLASSIFICATION_PROMPT }
82368
+ });
82369
+ try {
82370
+ const response = await session.sendAndWait({ prompt: `Task: ${taskDescription}` }, 15e3);
82371
+ const raw = (response.text ?? "").trim().toLowerCase();
82372
+ const tier = VALID_TIERS.find((t) => raw.includes(t));
82373
+ return tier ?? "standard";
82374
+ } finally {
82375
+ await session.disconnect().catch(() => void 0);
82376
+ }
82377
+ } finally {
82378
+ if (client2) {
82379
+ await client2.stop().catch(() => void 0);
82380
+ }
82381
+ }
82382
+ }
82383
+
82233
82384
  // packages/daemon/src/execution/history.ts
82234
82385
  var DEFAULT_CONTEXT_LIMIT = 5;
82235
82386
  var MAX_LEARNING_LENGTH = 1e3;
@@ -82515,14 +82666,15 @@ async function executeAgentTask(member, task, worktreePath, options2) {
82515
82666
  const mcpServerNote = options2?.mcpServers?.length ? `Available MCP server labels: ${options2.mcpServers.join(", ")}.` : "No additional MCP servers were configured for this run.";
82516
82667
  let client2 = null;
82517
82668
  try {
82518
- client2 = new CopilotClient2({ workingDirectory: worktreePath });
82669
+ client2 = new CopilotClient3({ workingDirectory: worktreePath });
82519
82670
  await client2.start();
82671
+ const model = member.model ?? await selectModelForTask(task.description);
82520
82672
  const session = await client2.createSession({
82521
- model: member.model ?? DEFAULT_MODEL,
82673
+ model,
82522
82674
  workingDirectory: worktreePath,
82523
82675
  tools,
82524
82676
  availableTools: ["custom:*"],
82525
- onPermissionRequest: approveAll2,
82677
+ onPermissionRequest: approveAll3,
82526
82678
  systemMessage: {
82527
82679
  content: `${member.systemPrompt}
82528
82680
 
@@ -82599,12 +82751,11 @@ async function buildInstanceSystemPromptSuffix(squadId, instanceId) {
82599
82751
  }
82600
82752
 
82601
82753
  // packages/daemon/src/execution/planning.ts
82602
- init_dist();
82603
82754
  import { exec as exec4 } from "node:child_process";
82604
82755
  import { access, readFile as readFile8 } from "node:fs/promises";
82605
82756
  import { join as join12 } from "node:path";
82606
82757
  import { promisify as promisify4 } from "node:util";
82607
- import { CopilotClient as CopilotClient3, approveAll as approveAll3 } from "@github/copilot-sdk";
82758
+ import { CopilotClient as CopilotClient4, approveAll as approveAll4 } from "@github/copilot-sdk";
82608
82759
 
82609
82760
  // packages/daemon/src/squad/roles.ts
82610
82761
  var ROLE_GUIDELINES = [
@@ -82747,12 +82898,13 @@ Return strict JSON in this shape:
82747
82898
  }`;
82748
82899
  let client2 = null;
82749
82900
  try {
82750
- client2 = new CopilotClient3({ workingDirectory: repoPath });
82901
+ client2 = new CopilotClient4({ workingDirectory: repoPath });
82751
82902
  await client2.start();
82903
+ const model = await selectModelForTask(`Create implementation plan: ${objective.description}`);
82752
82904
  const session = await client2.createSession({
82753
- model: DEFAULT_MODEL,
82905
+ model,
82754
82906
  workingDirectory: repoPath,
82755
- onPermissionRequest: approveAll3,
82907
+ onPermissionRequest: approveAll4,
82756
82908
  systemMessage: {
82757
82909
  content: `${TEAM_LEAD_PROMPT}
82758
82910
 
@@ -82855,7 +83007,7 @@ async function createPullRequest(options2) {
82855
83007
  init_dist();
82856
83008
  import { exec as exec6 } from "node:child_process";
82857
83009
  import { promisify as promisify6 } from "node:util";
82858
- import { CopilotClient as CopilotClient4, approveAll as approveAll4 } from "@github/copilot-sdk";
83010
+ import { CopilotClient as CopilotClient5, approveAll as approveAll5 } from "@github/copilot-sdk";
82859
83011
  var execAsync6 = promisify6(exec6);
82860
83012
  var GIT_DIFF_MAX_BUFFER = 10 * 1024 * 1024;
82861
83013
  function extractJsonObject2(content) {
@@ -82895,12 +83047,13 @@ Return strict JSON:
82895
83047
  }`;
82896
83048
  let client2 = null;
82897
83049
  try {
82898
- client2 = new CopilotClient4({ workingDirectory: worktreePath });
83050
+ client2 = new CopilotClient5({ workingDirectory: worktreePath });
82899
83051
  await client2.start();
83052
+ const model = qaMember.model ?? await selectModelForTask(`QA review: ${objective.description}`);
82900
83053
  const session = await client2.createSession({
82901
- model: qaMember.model ?? DEFAULT_MODEL,
83054
+ model,
82902
83055
  workingDirectory: worktreePath,
82903
- onPermissionRequest: approveAll4,
83056
+ onPermissionRequest: approveAll5,
82904
83057
  systemMessage: {
82905
83058
  content: QA_PROMPT
82906
83059
  }
@@ -82970,8 +83123,7 @@ async function handleQARejection(objectiveId, feedback) {
82970
83123
  }
82971
83124
 
82972
83125
  // packages/daemon/src/execution/review.ts
82973
- init_dist();
82974
- import { CopilotClient as CopilotClient5, approveAll as approveAll5 } from "@github/copilot-sdk";
83126
+ import { CopilotClient as CopilotClient6, approveAll as approveAll6 } from "@github/copilot-sdk";
82975
83127
  function extractJsonObject3(content) {
82976
83128
  const fenced = content.match(/```(?:json)?\s*([\s\S]*?)```/i);
82977
83129
  if (fenced?.[1]) {
@@ -83016,11 +83168,12 @@ Return strict JSON:
83016
83168
  }`;
83017
83169
  let client2 = null;
83018
83170
  try {
83019
- client2 = new CopilotClient5();
83171
+ client2 = new CopilotClient6();
83020
83172
  await client2.start();
83173
+ const model = teamLead.model ?? await selectModelForTask(`Code review: ${objective.description}`);
83021
83174
  const session = await client2.createSession({
83022
- model: teamLead.model ?? DEFAULT_MODEL,
83023
- onPermissionRequest: approveAll5,
83175
+ model,
83176
+ onPermissionRequest: approveAll6,
83024
83177
  systemMessage: {
83025
83178
  content: `${TEAM_LEAD_PROMPT}
83026
83179
 
@@ -84334,13 +84487,13 @@ var Orchestrator = class {
84334
84487
  });
84335
84488
  this.activeModel = model;
84336
84489
  } catch (error51) {
84337
- if (model !== DEFAULT_MODEL) {
84490
+ if (model !== this.config.defaultModel) {
84338
84491
  this.activeSession = await createSession({
84339
- model: DEFAULT_MODEL,
84492
+ model: this.config.defaultModel,
84340
84493
  systemPrompt,
84341
84494
  tools: createBoundOrchestratorTools(this.config)
84342
84495
  });
84343
- this.activeModel = DEFAULT_MODEL;
84496
+ this.activeModel = this.config.defaultModel;
84344
84497
  } else {
84345
84498
  throw error51;
84346
84499
  }
@@ -84389,6 +84542,7 @@ function createOrchestrator(config2, eventBus2) {
84389
84542
  // packages/daemon/src/scheduler/engine.ts
84390
84543
  init_dist();
84391
84544
  var import_cron_parser2 = __toESM(require_dist(), 1);
84545
+ init_db();
84392
84546
  var Scheduler = class {
84393
84547
  orchestrator;
84394
84548
  eventBus;
@@ -84476,6 +84630,9 @@ function createScheduler(orchestrator2, eventBus2) {
84476
84630
  return new Scheduler(orchestrator2, eventBus2);
84477
84631
  }
84478
84632
 
84633
+ // packages/daemon/src/index.ts
84634
+ init_db();
84635
+
84479
84636
  // packages/daemon/src/telegram/bot.ts
84480
84637
  var import_grammy = __toESM(require_mod2(), 1);
84481
84638
  var TelegramBot = class {
@@ -84496,9 +84653,16 @@ var TelegramBot = class {
84496
84653
  if (this.started) {
84497
84654
  return;
84498
84655
  }
84656
+ this.bot.catch((err) => {
84657
+ this.logger.error({ err: err.error }, "Telegram bot error: %s", err.message);
84658
+ });
84499
84659
  this.registerHandlers();
84500
- this.bot.start().catch((error51) => {
84501
- this.logger.error({ err: error51 }, "Telegram polling failed");
84660
+ this.bot.start({
84661
+ onStart: () => {
84662
+ this.logger.info("Telegram bot connected and polling");
84663
+ }
84664
+ }).catch((error51) => {
84665
+ this.logger.error({ err: error51 }, "Telegram polling failed to start");
84502
84666
  });
84503
84667
  this.started = true;
84504
84668
  }
@@ -84508,11 +84672,26 @@ var TelegramBot = class {
84508
84672
  }
84509
84673
  this.bot.stop();
84510
84674
  this.started = false;
84675
+ this.logger.info("Telegram bot stopped");
84511
84676
  }
84512
84677
  async sendText(chatId, text) {
84513
84678
  await this.bot.api.sendMessage(chatId, text);
84514
84679
  }
84680
+ isAuthorized(userId) {
84681
+ if (!this.config.telegramUserId) {
84682
+ return false;
84683
+ }
84684
+ return String(userId) === this.config.telegramUserId;
84685
+ }
84515
84686
  registerHandlers() {
84687
+ this.bot.use(async (ctx, next) => {
84688
+ const userId = ctx.from?.id;
84689
+ if (!userId || !this.isAuthorized(userId)) {
84690
+ this.logger.warn({ userId }, "Unauthorized Telegram message, ignoring");
84691
+ return;
84692
+ }
84693
+ await next();
84694
+ });
84516
84695
  this.bot.command("start", async (ctx) => {
84517
84696
  await ctx.reply(
84518
84697
  "Hello from Io. Send me a message and I will route it through the daemon orchestrator."
@@ -84707,8 +84886,13 @@ async function main() {
84707
84886
  setChatOrchestrator(orchestrator2);
84708
84887
  const apiServer = createApiServer(config2);
84709
84888
  const telegramBot = createTelegramBot(config2, orchestrator2);
84710
- telegramBot?.start();
84711
- createTelegramNotifier(telegramBot, config2, eventBus);
84889
+ if (telegramBot) {
84890
+ telegramBot.start();
84891
+ createTelegramNotifier(telegramBot, config2, eventBus);
84892
+ logger2.info("Telegram bot initialized");
84893
+ } else {
84894
+ logger2.info("Telegram bot disabled (no token configured)");
84895
+ }
84712
84896
  registerShutdownHandlers(logger2, async () => {
84713
84897
  if (pricingRefreshTimer) {
84714
84898
  clearInterval(pricingRefreshTimer);