@aglyn/plugins-ai 1.20.3 → 1.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aglyn/plugins-ai",
3
- "version": "1.20.3",
3
+ "version": "1.22.0",
4
4
  "license": "Apache-2.0",
5
5
  "homepage": "https://aglyn.com",
6
6
  "repository": {
@@ -25,23 +25,23 @@
25
25
  "./package.json": "./package.json"
26
26
  },
27
27
  "dependencies": {
28
- "@aglyn/aglyn": "1.20.3",
29
- "@aglyn/shared-data-enums": "1.20.3",
30
- "@aglyn/shared-data-mdi": "1.20.3",
31
- "@aglyn/shared-data-types": "1.20.3",
32
- "@aglyn/shared-ui-email-campaigns": "1.20.3",
33
- "@aglyn/shared-ui-jsx": "1.20.3",
34
- "@aglyn/shared-ui-snackstack": "1.20.3",
35
- "@aglyn/shared-ui-theme": "1.20.3",
36
- "@aglyn/shared-util-email": "1.20.3",
37
- "@aglyn/shared-util-errors": "1.20.3",
38
- "@aglyn/shared-util-http": "1.20.3",
39
- "@aglyn/shared-util-timestamp": "1.20.3",
40
- "@aglyn/shared-util-tools": "1.20.3",
41
- "@aglyn/tenant-data-admin": "1.20.3",
42
- "@aglyn/tenant-feature-instance": "1.20.3",
43
- "@aglyn/tenant-runtime": "1.20.3",
44
- "@swc/helpers": "0.5.23"
28
+ "@aglyn/aglyn": "1.22.0",
29
+ "@aglyn/shared-data-enums": "1.22.0",
30
+ "@aglyn/shared-data-mdi": "1.22.0",
31
+ "@aglyn/shared-data-types": "1.22.0",
32
+ "@aglyn/shared-ui-email-campaigns": "1.22.0",
33
+ "@aglyn/shared-ui-jsx": "1.22.0",
34
+ "@aglyn/shared-ui-snackstack": "1.22.0",
35
+ "@aglyn/shared-ui-theme": "1.22.0",
36
+ "@aglyn/shared-util-email": "1.22.0",
37
+ "@aglyn/shared-util-errors": "1.22.0",
38
+ "@aglyn/shared-util-http": "1.22.0",
39
+ "@aglyn/shared-util-timestamp": "1.22.0",
40
+ "@aglyn/shared-util-tools": "1.22.0",
41
+ "@aglyn/tenant-data-admin": "1.22.0",
42
+ "@aglyn/tenant-feature-instance": "1.22.0",
43
+ "@aglyn/tenant-runtime": "1.22.0",
44
+ "@swc/helpers": "0.5.24"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "@mui/material": "^9.3.1",
@@ -27,8 +27,9 @@ import { aiLayoutPromisedCopyViolations, aiLayoutPromisedSections } from "../lay
27
27
  import { aiLayoutStoredTree } from "../layout-language/ai-layout-store.js";
28
28
  import { AI_GENERATION_MAX_ATTEMPTS } from "../runtime/ai-generation-bounds.js";
29
29
  import { AI_STEP_TIERS } from "../providers/catalog.js";
30
+ import { AI_ROUTING_TABLE } from "../providers/routing.js";
30
31
  import { aiCacheWriteHourSum } from "../providers/cache-write-hour.js";
31
- import { aiDoctrineNeedsInputMessage, runValidatedGeneration } from "../runtime/ai-doctrine.js";
32
+ import { aiDoctrineNeedsInputMessage, aiDoctrineScopeFor, aiDoctrineSystemBlocks, runValidatedGeneration } from "../runtime/ai-doctrine.js";
32
33
  import { aiSettleMechanicalRules, aiSettleUnrelatedScreenLinks, validateAiDoctrineTree } from "../runtime/ai-doctrine-validators.js";
33
34
  import { aiSiteKindDesignLines, aiSiteKindOfInputs } from "../model/ai-site-kinds.js";
34
35
  import { aiSiteSeed } from "../model/ai-site-look.js";
@@ -45,7 +46,7 @@ import { AI_SITE_IMPORT_DESIGN_MAX_CHARS, aiCompileImportedPage, aiSiteImportDes
45
46
  import { aiSiteImportPageIsLegal, AI_SITE_IMPORT_LAYOUT_KEPT, AI_SITE_IMPORT_PROPOSED_NOT_WRITTEN } from "../model/ai-site-import.js";
46
47
  import { siteImportFidelity } from "../model/ai-site-import-fidelity.js";
47
48
  import { aiGenerationMaxTokensWithin, aiGenerationWorstCaseMs } from "./ai-job-budget.js";
48
- import { AI_SITE_IMPORT_IMPROVE_KIND, AI_SITE_IMPORT_IMPROVE_MOST_TOKENS, AI_SITE_IMPORT_IMPROVE_TOOL, aiSiteImportImproveCheck, aiSiteImportImproveDestinations, aiSiteImportImproveDigest, aiSiteImportImproveInstructions, aiSiteImportImproveLedger, aiSiteImportImprovePrompt, aiSiteImportImproveScope, aiSiteImportImprovedMisses, aiSiteImportImproveInputUsage, aiSiteImportImproveReaskTokens, aiSiteImportImproveRoleLines, aiSiteImportImproveTokensWithin, aiSiteImportImproveOnlySoft, aiSiteImportNewSectionsWanted, aiSiteImportPageRoleOf, aiSiteImportMeaningShare, aiSiteImportRedesignWhy } from "./ai-site-import-improve.js";
49
+ import { AI_SITE_IMPORT_IMPROVE_KIND, AI_SITE_IMPORT_IMPROVE_MOST_TOKENS, AI_SITE_IMPORT_IMPROVE_TOOL, aiSiteImportImproveCheck, aiSiteImportImproveDestinations, aiSiteImportImproveDigest, aiSiteImportImproveInstructions, aiSiteImportImproveLedger, aiSiteImportImprovePrompt, aiSiteImportImproveScope, aiSiteImportImprovedMisses, aiSiteImportImproveCredits, aiSiteImportImproveReaskNeeds, aiSiteImportImproveReaskTokens, aiSiteImportImproveRoleLines, aiSiteImportImproveRoom, aiSiteImportImproveSizeLines, aiSiteImportImproveOnlySoft, AI_SITE_IMPORT_IMPROVE_CUT_OFF_SMALLER, aiSiteImportNewSectionsWanted, aiSiteImportPageRoleOf, aiSiteImportMeaningShare, aiSiteImportRedesignWhy } from "./ai-site-import-improve.js";
49
50
  import { AI_SITE_IMPORT_WORDING_INSTRUCTIONS, AI_SITE_IMPORT_WORDING_KIND, AI_SITE_IMPORT_WORDING_TOOL, aiSiteImportWordingCheck, aiSiteImportWordingPage, aiSiteImportWordingPrompt, aiSiteImportWordingTokens } from "./ai-site-import-wording.js";
50
51
  /**
51
52
  * A page built in the compact layout language (AGL-3660): the page step's
@@ -1346,26 +1347,55 @@ const NO_USAGE = {
1346
1347
  kind: (_ref = (_doc_plan1 = doc.plan) == null ? void 0 : _doc_plan1.site.kind) != null ? _ref : null,
1347
1348
  seed: aiSiteSeed(input.job.$id)
1348
1349
  }));
1349
- const prompt = aiSiteImportImprovePrompt({
1350
- screen: input.screen,
1351
- doc,
1352
- ledger,
1353
- destinations: aiSiteImportImproveDestinations(doc, ledger),
1354
- kindLines: aiSiteKindDesignLines(input.job.inputs),
1355
- seed: input.job.$id.slice(-6),
1356
- roleLines
1357
- });
1350
+ const promptOf = (sizeLines)=>aiSiteImportImprovePrompt({
1351
+ screen: input.screen,
1352
+ doc,
1353
+ ledger,
1354
+ destinations: aiSiteImportImproveDestinations(doc, ledger),
1355
+ kindLines: aiSiteKindDesignLines(input.job.inputs),
1356
+ seed: input.job.$id.slice(-6),
1357
+ roleLines,
1358
+ sizeLines
1359
+ });
1358
1360
  // The page's ceiling holds in code: the answer asks no more than it buys, and a re-ask it cannot pay for is not asked.
1361
+ // Its input is priced from every byte the request sends, as the doctrine's loop sends it (AGL-4152).
1359
1362
  const most = aiSiteImportImproveMaxTokens(input.model);
1360
- const first = aiSiteImportImproveTokensWithin({
1361
- model: input.model,
1362
- spent: spend.usage,
1363
- input: aiSiteImportImproveInputUsage({
1364
- systemChars: instructions.reduce((sum, block)=>sum + block.text.length, 0),
1365
- promptChars: prompt.length
1366
- }),
1367
- most
1363
+ const system = aiDoctrineSystemBlocks(undefined, {
1364
+ instructions,
1365
+ scope: aiDoctrineScopeFor(AI_SITE_IMPORT_IMPROVE_KIND)
1368
1366
  });
1367
+ const roomOf = (prompt)=>aiSiteImportImproveRoom({
1368
+ model: input.model,
1369
+ spent: spend.usage,
1370
+ system,
1371
+ toolChars: JSON.stringify(AI_SITE_IMPORT_IMPROVE_TOOL).length,
1372
+ promptChars: prompt.length,
1373
+ hour: AI_ROUTING_TABLE['job.page'].cacheTtl === '1h',
1374
+ most,
1375
+ ledger,
1376
+ proposed: !!doc.proposed
1377
+ });
1378
+ // The answer is told its room in sections and words, so it plans a page that fits rather than being cut off. The
1379
+ // lines are part of the request they size, so they are priced over it until they say what its room is.
1380
+ const groups = ledger.items.some((item)=>item.kind === 'group');
1381
+ let sizeLines = [];
1382
+ let prompt = promptOf(sizeLines);
1383
+ let room = roomOf(prompt);
1384
+ for(let pass = 0; pass < 3; pass += 1){
1385
+ const lines = aiSiteImportImproveSizeLines(room, {
1386
+ most,
1387
+ groups
1388
+ });
1389
+ if (lines.join('\n') === sizeLines.join('\n')) break;
1390
+ sizeLines = lines;
1391
+ prompt = promptOf(sizeLines);
1392
+ room = roomOf(prompt);
1393
+ }
1394
+ const first = room.first;
1395
+ // What each answer had and wrote, for the log a fallback leaves (AGL-4152).
1396
+ const track = {
1397
+ reask: null
1398
+ };
1369
1399
  const checkOf = (lenient)=>{
1370
1400
  var _ref, _input_context_framing, _doc_plan;
1371
1401
  var _input_targets_forms_;
@@ -1436,15 +1466,25 @@ const NO_USAGE = {
1436
1466
  tool: AI_SITE_IMPORT_IMPROVE_TOOL,
1437
1467
  maxTokens: Math.max(1, first),
1438
1468
  // Nothing is spent before it: a page brought in is compiled with no model first.
1439
- reaskTokens: (spent, last)=>aiSiteImportImproveReaskTokens({
1469
+ reaskTokens: (spent, last)=>{
1470
+ const tokens = aiSiteImportImproveReaskTokens({
1440
1471
  model: input.model,
1441
1472
  spent,
1442
1473
  lastOutput: last.outputTokens,
1474
+ cutOff: last.cutOff,
1443
1475
  most
1444
- }),
1476
+ });
1477
+ track.reask = {
1478
+ room: tokens,
1479
+ needs: aiSiteImportImproveReaskNeeds(last.outputTokens, last.cutOff),
1480
+ cutOff: last.cutOff,
1481
+ firstOutput: last.outputTokens
1482
+ };
1483
+ return tokens;
1484
+ },
1445
1485
  cutOff: {
1446
1486
  noun: 'page',
1447
- smaller: 'Write shorter copy, and fewer sections.'
1487
+ smaller: AI_SITE_IMPORT_IMPROVE_CUT_OFF_SMALLER
1448
1488
  }
1449
1489
  }, AI_LAYOUT_LANGUAGE_THINKING, {
1450
1490
  check: observe(checkOf(false))
@@ -1452,13 +1492,53 @@ const NO_USAGE = {
1452
1492
  signal: input.signal
1453
1493
  } : {}));
1454
1494
  const spent = spentTogether(spend, answered);
1495
+ // Each answer's room and what it wrote, whether a re-ask ran, and its input priced before and as billed (AGL-4152):
1496
+ // what a later read of a fallback needs to tell a page cut off from one starved, never a word of the page.
1497
+ const asked = track.reask;
1498
+ const firstOutput = asked ? asked.firstOutput : answered.usage.outputTokens;
1499
+ const sizes = {
1500
+ answers: [
1501
+ {
1502
+ room: first,
1503
+ outputTokens: firstOutput
1504
+ },
1505
+ ...answered.attempts > 1 && asked ? [
1506
+ {
1507
+ room: Math.min(first, asked.room),
1508
+ outputTokens: answered.usage.outputTokens - firstOutput
1509
+ }
1510
+ ] : []
1511
+ ],
1512
+ reask: asked ? {
1513
+ ran: answered.attempts > 1,
1514
+ room: asked.room,
1515
+ needs: asked.needs,
1516
+ cutOff: asked.cutOff
1517
+ } : {
1518
+ ran: false
1519
+ },
1520
+ planned: _extends({
1521
+ sections: room.sections,
1522
+ words: room.words,
1523
+ target: room.target
1524
+ }, room.thin !== null ? {
1525
+ thinWords: room.thin
1526
+ } : {}),
1527
+ inputCredits: {
1528
+ estimated: aiSiteImportImproveCredits(room.estimated, input.model),
1529
+ billed: aiSiteImportImproveCredits(_extends({}, answered.usage, {
1530
+ outputTokens: 0
1531
+ }), input.model)
1532
+ },
1533
+ credits: aiSiteImportImproveCredits(answered.usage, input.model)
1534
+ };
1455
1535
  // Its last answer, where no re-ask was left and it missed only how it is planned or drawn: taken, its misses on its row (AGL-4152).
1456
1536
  const taken = answered.status === 'needs_input' && answered.answer && aiSiteImportImproveOnlySoft(answered.violations) ? checkOf(true)(answered.answer) : null;
1457
1537
  if (taken == null ? void 0 : taken.value) {
1458
1538
  const { built, page, missed } = taken.value;
1459
- console.info('ai imported page: its redesign taken with design lines missed', {
1539
+ console.info('ai imported page: its redesign taken with design lines missed', _extends({
1460
1540
  codes: missed != null ? missed : []
1461
- });
1541
+ }, sizes));
1462
1542
  return _extends({}, spent, {
1463
1543
  status: 'ok',
1464
1544
  value: _extends({}, built, {
@@ -1482,11 +1562,11 @@ const NO_USAGE = {
1482
1562
  ];
1483
1563
  // Logged with each miss's line, cut short (AGL-4152): the answer is not kept, so this is all a later read of the job has to go on.
1484
1564
  const why = answered.status === 'needs_input' ? aiSiteImportRedesignWhy(answered.violations) : [];
1485
- console.info('ai imported page: its redesign did not pass, its layout kept', {
1565
+ console.info('ai imported page: its redesign did not pass, its layout kept', _extends({
1486
1566
  status: answered.status,
1487
1567
  codes,
1488
1568
  why
1489
- });
1569
+ }, sizes));
1490
1570
  // A new page has no words of its own to fall back on: it is left out, and its row says why.
1491
1571
  if (doc.proposed) return _extends({}, spent, {
1492
1572
  status: 'needs_input',