@aglyn/plugins-ai 1.26.6 → 1.28.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.
Files changed (46) hide show
  1. package/package.json +17 -17
  2. package/src/lib/components/ai-credits-prompt.component.js +4 -1
  3. package/src/lib/components/ai-credits-prompt.component.js.map +1 -1
  4. package/src/lib/components/ai-site-import-job.component.js +5 -2
  5. package/src/lib/components/ai-site-import-job.component.js.map +1 -1
  6. package/src/lib/components/ai-site-start-card.component.js +6 -3
  7. package/src/lib/components/ai-site-start-card.component.js.map +1 -1
  8. package/src/lib/components/assist-build-card.component.js +4 -1
  9. package/src/lib/components/assist-build-card.component.js.map +1 -1
  10. package/src/lib/components/assist-edit-canvas.d.ts +12 -0
  11. package/src/lib/components/assist-edit-canvas.js +38 -7
  12. package/src/lib/components/assist-edit-canvas.js.map +1 -1
  13. package/src/lib/components/assist-jobs-drawer.component.js +4 -1
  14. package/src/lib/components/assist-jobs-drawer.component.js.map +1 -1
  15. package/src/lib/components/assist-panel.component.js +5 -2
  16. package/src/lib/components/assist-panel.component.js.map +1 -1
  17. package/src/lib/declarations.server.js +17 -0
  18. package/src/lib/declarations.server.js.map +1 -1
  19. package/src/lib/jobs/ai-job-drafts.d.ts +5 -11
  20. package/src/lib/jobs/ai-job-drafts.js +8 -25
  21. package/src/lib/jobs/ai-job-drafts.js.map +1 -1
  22. package/src/lib/jobs/ai-job-edit-step.d.ts +95 -5
  23. package/src/lib/jobs/ai-job-edit-step.js +88 -17
  24. package/src/lib/jobs/ai-job-edit-step.js.map +1 -1
  25. package/src/lib/jobs/ai-job-settings-step.d.ts +1 -1
  26. package/src/lib/jobs/ai-job-settings-step.js +14 -23
  27. package/src/lib/jobs/ai-job-settings-step.js.map +1 -1
  28. package/src/lib/model/ai-edit-job.d.ts +27 -0
  29. package/src/lib/model/ai-edit-job.js +45 -23
  30. package/src/lib/model/ai-edit-job.js.map +1 -1
  31. package/src/lib/model/assist-edit.d.ts +2 -0
  32. package/src/lib/model/assist-edit.js.map +1 -1
  33. package/src/lib/runtime/ai-node-tree.d.ts +25 -1
  34. package/src/lib/runtime/ai-node-tree.js +39 -14
  35. package/src/lib/runtime/ai-node-tree.js.map +1 -1
  36. package/src/lib/server/ai-page-content.d.ts +83 -0
  37. package/src/lib/server/ai-page-content.js +765 -0
  38. package/src/lib/server/ai-page-content.js.map +1 -0
  39. package/src/lib/server/ai-usage.js +7 -1
  40. package/src/lib/server/ai-usage.js.map +1 -1
  41. package/src/lib/server/assist-edit.d.ts +136 -1
  42. package/src/lib/server/assist-edit.js +124 -47
  43. package/src/lib/server/assist-edit.js.map +1 -1
  44. package/src/lib/usage/ai-usage-wire.d.ts +15 -0
  45. package/src/lib/usage/ai-usage-wire.js +21 -0
  46. package/src/lib/usage/ai-usage-wire.js.map +1 -1
@@ -48,6 +48,13 @@ export interface OrgAiUsageTableWire {
48
48
  /** The workspace's own credits for the month — the pool the shares are of. */
49
49
  orgCredits: number;
50
50
  rows: OrgAiUsageRowWire[];
51
+ /**
52
+ * `view=prepared` with a site and `uids`: each asked person's credits on
53
+ * that site this month, 0 for someone with none — the figure the site's
54
+ * collaborators table prints per row ("AI credits (site, month)"), so an app
55
+ * reads one cell per row without walking the table.
56
+ */
57
+ hostCells?: Record<string, number>;
51
58
  }
52
59
  /** One person's month, as the member card reads it. */
53
60
  export interface UserAiUsageMonthWire {
@@ -71,6 +78,14 @@ export declare function toAiUsageWireRow(row: AiUsageByUserMonth & {
71
78
  role: string | null;
72
79
  share: number;
73
80
  }, hostId: string): OrgAiUsageRowWire;
81
+ /** The most people one `view=prepared` request answers for. */
82
+ export declare const AI_USAGE_HOST_CELLS_MAX = 200;
83
+ /**
84
+ * Each asked uid's credits on the site (`hostCredits`), 0 where the month
85
+ * holds nothing for them — what `AiCollaboratorCreditsCell` reads from
86
+ * `hostCreditsByUid` (`?? 0`). Blank uids are skipped and the list is capped.
87
+ */
88
+ export declare function aiUsageHostCells(rows: readonly Pick<OrgAiUsageRowWire, 'uid' | 'hostCredits'>[], uids: readonly string[]): Record<string, number>;
74
89
  /** The CSV's columns, in order. */
75
90
  export declare const AI_USAGE_CSV_HEADER: readonly ["Member", "Email", "Role", "Credits", "Share", "Requests", "Refusals", "Top kinds", "Sites"];
76
91
  /** A share as the table and the file print it: one decimal of a percent. */
@@ -41,6 +41,27 @@ import { csvCell } from "@aglyn/aglyn/app-utils/csv-import";
41
41
  if (hostId) wire.hostCredits = (_row_byHost_hostId = row.byHost[hostId]) != null ? _row_byHost_hostId : 0;
42
42
  return wire;
43
43
  }
44
+ /** The most people one `view=prepared` request answers for. */ export const AI_USAGE_HOST_CELLS_MAX = 200;
45
+ /**
46
+ * Each asked uid's credits on the site (`hostCredits`), 0 where the month
47
+ * holds nothing for them — what `AiCollaboratorCreditsCell` reads from
48
+ * `hostCreditsByUid` (`?? 0`). Blank uids are skipped and the list is capped.
49
+ */ export function aiUsageHostCells(rows, uids) {
50
+ const byUid = new Map(rows.map((row)=>{
51
+ var _row_hostCredits;
52
+ return [
53
+ row.uid,
54
+ (_row_hostCredits = row.hostCredits) != null ? _row_hostCredits : 0
55
+ ];
56
+ }));
57
+ const cells = {};
58
+ for (const raw of uids.slice(0, AI_USAGE_HOST_CELLS_MAX)){
59
+ var _byUid_get;
60
+ const uid = raw.trim();
61
+ if (uid) cells[uid] = (_byUid_get = byUid.get(uid)) != null ? _byUid_get : 0;
62
+ }
63
+ return cells;
64
+ }
44
65
  /** The CSV's columns, in order. */ export const AI_USAGE_CSV_HEADER = [
45
66
  'Member',
46
67
  'Email',
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../libs/plugins/ai/src/lib/usage/ai-usage-wire.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n// By their own entry points rather than the barrel: the route that serves\n// this shape imports it on the server, where the whole library is dead weight.\nimport {\n topAiUsageKinds,\n type AiUsageByUserMonth,\n} from '../model/ai-usage-by-user'\nimport {\n AI_ALLOTMENT_WARN_SHARE,\n type AiAllotmentMode,\n type AiCreditAllotmentScope,\n} from '../model/ai-allotments'\nimport { csvCell } from '@aglyn/aglyn/app-utils/csv-import'\n\n/**\n * What `/api/ai/usage` answers (AGL-2928), shared by the route and the\n * console surfaces that read it — the Usage table, the roster column, the\n * site collaborators card and the member card. Dependency-free on purpose:\n * the route imports the Admin SDK and a client component may not.\n */\n\n/** Header naming the row count the export undertook to send. */\nexport const AI_USAGE_EXPORT_ROWS_HEADER = 'X-Aglyn-Export-Rows'\n\n/** A row as a customer sees it — credits and a share, never `estCostUsd`. */\nexport interface OrgAiUsageRowWire {\n uid: string\n /** The roster's name — display name, else email, else the uid. */\n name: string\n email: string | null\n role: string | null\n credits: number\n /** Of the workspace's measured spend for the month, in `[0, 1]`. */\n share: number\n requests: number\n refusals: number\n byKind: Record<string, number>\n byHost: Record<string, number>\n /** Credits on the site the request named, when it named one. */\n hostCredits?: number\n}\n\nexport interface OrgAiUsageTableWire {\n month: string\n /** The months a reader may ask for, newest first. */\n months: string[]\n /** The workspace's own credits for the month — the pool the shares are of. */\n orgCredits: number\n rows: OrgAiUsageRowWire[]\n}\n\n/** One person's month, as the member card reads it. */\nexport interface UserAiUsageMonthWire {\n month: string\n credits: number\n share: number\n requests: number\n refusals: number\n byKind: Record<string, number>\n byHost: Record<string, number>\n}\n\nexport interface UserAiUsageWire {\n uid: string\n /** Newest first. */\n months: UserAiUsageMonthWire[]\n}\n\n/** The roster's month, joined and shared, projected onto the wire. */\nexport function toAiUsageWireRow(\n row: AiUsageByUserMonth & {\n name: string\n email: string | null\n role: string | null\n share: number\n },\n hostId: string,\n): OrgAiUsageRowWire {\n const wire: OrgAiUsageRowWire = {\n uid: row.uid,\n name: row.name,\n email: row.email,\n role: row.role,\n credits: row.credits,\n share: row.share,\n requests: row.requests,\n refusals: row.refusals,\n byKind: row.byKind,\n byHost: row.byHost,\n }\n if (hostId) wire.hostCredits = row.byHost[hostId] ?? 0\n return wire\n}\n\n/** The CSV's columns, in order. */\nexport const AI_USAGE_CSV_HEADER = [\n 'Member',\n 'Email',\n 'Role',\n 'Credits',\n 'Share',\n 'Requests',\n 'Refusals',\n 'Top kinds',\n 'Sites',\n] as const\n\n/** A share as the table and the file print it: one decimal of a percent. */\nexport function formatAiUsageShare(share: number): string {\n return `${Math.round(share * 1000) / 10}%`\n}\n\nexport function aiUsageCsvLine(row: OrgAiUsageRowWire): string {\n const kinds = topAiUsageKinds(row.byKind, 3)\n .map((entry) => `${entry.kind} ${entry.credits}`)\n .join('; ')\n const sites = Object.entries(row.byHost)\n .sort((a, b) => b[1] - a[1])\n .map(([hostId, credits]) => `${hostId} ${credits}`)\n .join('; ')\n return [\n row.name,\n row.email ?? '',\n row.role ?? '',\n row.credits,\n formatAiUsageShare(row.share),\n row.requests,\n row.refusals,\n kinds,\n sites,\n ]\n .map(csvCell)\n .join(',')\n}\n\n/**\n * THE USAGE STRIP'S ENVELOPE (AGL-2942): what every AI door adds to the\n * answer it already sends — the chat door's `done` event, the copy\n * assistant's JSON, a job's create response, and each door's quota refusal —\n * so the strip in the panel and the generation dialogs updates with no read\n * of its own.\n *\n * Credits only. `pool` is the workspace's month against its band; `mine` is\n * the caller's own month, measured against the allotment that binds them\n * when one does — for a site allotment, the site's month, because that is\n * the line the next request meets.\n */\nexport type AiUsageMeterState = 'ok' | 'warn' | 'capped'\n\nexport interface AiUsageMeterWire {\n month: string\n pool: { used: number; limit: number | null }\n mine: {\n used: number\n limit: number | null\n mode: AiAllotmentMode | null\n scope: AiCreditAllotmentScope | null\n /**\n * `mine` is the person's Free allowance (AGL-3722): `used` of the 300\n * free credits a month every Free workspace they hold draws on, net of\n * give-backs. Absent on a paid workspace.\n */\n free?: boolean\n }\n /** Credits the exchange this envelope answers cost; `null` when no model ran. */\n last: number | null\n /** True when the envelope rides a refusal. */\n refused: boolean\n state: AiUsageMeterState\n /** The model that answered, as the catalog names it. */\n model: { id: string; label: string; auto: boolean } | null\n}\n\n/**\n * `capped` when a hard allotment is spent or the request was refused, `warn`\n * from 80% of the allotment that binds or of the pool, `ok` otherwise.\n */\nexport function aiUsageMeterState(\n meter: Pick<AiUsageMeterWire, 'pool' | 'mine' | 'refused'>,\n): AiUsageMeterState {\n const { mine, pool } = meter\n if (meter.refused) return 'capped'\n if (mine.limit !== null && mine.mode === 'hard' && mine.used >= mine.limit) return 'capped'\n const past = (used: number, limit: number | null) =>\n limit !== null && limit > 0 && used >= limit * AI_ALLOTMENT_WARN_SHARE\n return past(mine.used, mine.limit) || past(pool.used, pool.limit) ? 'warn' : 'ok'\n}\n\n/**\n * What `/api/ai/allotments` answers (AGL-2942): the allotments a reader may\n * see, each with what its subject drew this month, and the roster, sites\n * and models the editors pick from. Credits only.\n */\nexport interface AiAllotmentRowWire {\n subject: string\n scope: 'member' | 'collab' | 'host' | 'org'\n uid: string | null\n hostId: string | null\n credits: number | null\n mode: AiAllotmentMode\n models: string[] | null\n /** Credits the subject drew this month — the figure the gate measures. */\n used: number\n}\n\nexport interface AiAllotmentsMemberWire {\n uid: string\n name: string\n email: string | null\n role: string | null\n /** False for a site collaborator, whose allotments are per site. */\n orgWide: boolean\n /** Credits this month, every site. */\n credits: number\n /** Credits this month by site. */\n byHost: Record<string, number>\n}\n\nexport interface AiAllotmentsHostWire {\n hostId: string\n name: string\n /** The site's credits this month, everyone on it. */\n credits: number\n}\n\nexport interface AiAllotmentsWire {\n /** The org the answer is about — resolved from the site when only one was named. */\n orgId: string\n month: string\n pool: { used: number; limit: number | null }\n allotments: AiAllotmentRowWire[]\n members: AiAllotmentsMemberWire[]\n hosts: AiAllotmentsHostWire[]\n /** The catalog models this deployment serves, for an allowlist. */\n models: Array<{ id: string; label: string; tier: string }>\n /** Whether the plan lets the workspace restrict models org-wide. */\n restrictionAvailable: boolean\n canEdit: {\n /** Members, sites and the restriction: `billing.manage`. */\n billing: boolean\n /** Collaborators on the site asked about: `billing.manage`, or its admin. */\n collaborators: boolean\n }\n /** Who is reading, so an editor can refuse to offer a self-raise. */\n callerUid: string\n}\n\n/**\n * `2026-09` as a reader says it, on the UTC calendar the key is cut from, in\n * the reader's locale (the staff apps' prepared views pass `en-US`).\n */\nexport function aiUsageMonthLabel(month: string, locale?: string): string {\n const [year, monthOfYear] = month.split('-').map(Number)\n if (!year || !monthOfYear) return month\n try {\n return new Date(Date.UTC(year, monthOfYear - 1, 1)).toLocaleDateString(locale, {\n month: 'long',\n year: 'numeric',\n timeZone: 'UTC',\n })\n } catch {\n return month\n }\n}\n"],"names":["topAiUsageKinds","AI_ALLOTMENT_WARN_SHARE","csvCell","AI_USAGE_EXPORT_ROWS_HEADER","toAiUsageWireRow","row","hostId","wire","uid","name","email","role","credits","share","requests","refusals","byKind","byHost","hostCredits","AI_USAGE_CSV_HEADER","formatAiUsageShare","Math","round","aiUsageCsvLine","kinds","map","entry","kind","join","sites","Object","entries","sort","a","b","aiUsageMeterState","meter","mine","pool","refused","limit","mode","used","past","aiUsageMonthLabel","month","locale","year","monthOfYear","split","Number","Date","UTC","toLocaleDateString","timeZone"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,0EAA0E;AAC1E,+EAA+E;AAC/E,SACEA,eAAe,QAEV,+BAA2B;AAClC,SACEC,uBAAuB,QAGlB,4BAAwB;AAC/B,SAASC,OAAO,QAAQ,oCAAmC;AAE3D;;;;;CAKC,GAED,8DAA8D,GAC9D,OAAO,MAAMC,8BAA8B,sBAAqB;AA8ChE,oEAAoE,GACpE,OAAO,SAASC,iBACdC,GAKC,EACDC,MAAc;QAciBD;IAZ/B,MAAME,OAA0B;QAC9BC,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IAAIS,QAAQ;QACZT,IAAIU,QAAQ;QACZS;QACAK;KACD,CACEJ,GAAG,CAACvB,SACJ0B,IAAI,CAAC;AACV;AAwCA;;;CAGC,GACD,OAAO,SAASO,kBACdC,KAA0D;IAE1D,MAAM,EAAEC,IAAI,EAAEC,IAAI,EAAE,GAAGF;IACvB,IAAIA,MAAMG,OAAO,EAAE,OAAO;IAC1B,IAAIF,KAAKG,KAAK,KAAK,QAAQH,KAAKI,IAAI,KAAK,UAAUJ,KAAKK,IAAI,IAAIL,KAAKG,KAAK,EAAE,OAAO;IACnF,MAAMG,OAAO,CAACD,MAAcF,QAC1BA,UAAU,QAAQA,QAAQ,KAAKE,QAAQF,QAAQvC;IACjD,OAAO0C,KAAKN,KAAKK,IAAI,EAAEL,KAAKG,KAAK,KAAKG,KAAKL,KAAKI,IAAI,EAAEJ,KAAKE,KAAK,IAAI,SAAS;AAC/E;AA6DA;;;CAGC,GACD,OAAO,SAASI,kBAAkBC,KAAa,EAAEC,MAAe;IAC9D,MAAM,CAACC,MAAMC,YAAY,GAAGH,MAAMI,KAAK,CAAC,KAAKxB,GAAG,CAACyB;IACjD,IAAI,CAACH,QAAQ,CAACC,aAAa,OAAOH;IAClC,IAAI;QACF,OAAO,IAAIM,KAAKA,KAAKC,GAAG,CAACL,MAAMC,cAAc,GAAG,IAAIK,kBAAkB,CAACP,QAAQ;YAC7ED,OAAO;YACPE,MAAM;YACNO,UAAU;QACZ;IACF,EAAE,eAAM;QACN,OAAOT;IACT;AACF"}
1
+ {"version":3,"sources":["../../../../../../../libs/plugins/ai/src/lib/usage/ai-usage-wire.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n// By their own entry points rather than the barrel: the route that serves\n// this shape imports it on the server, where the whole library is dead weight.\nimport {\n topAiUsageKinds,\n type AiUsageByUserMonth,\n} from '../model/ai-usage-by-user'\nimport {\n AI_ALLOTMENT_WARN_SHARE,\n type AiAllotmentMode,\n type AiCreditAllotmentScope,\n} from '../model/ai-allotments'\nimport { csvCell } from '@aglyn/aglyn/app-utils/csv-import'\n\n/**\n * What `/api/ai/usage` answers (AGL-2928), shared by the route and the\n * console surfaces that read it — the Usage table, the roster column, the\n * site collaborators card and the member card. Dependency-free on purpose:\n * the route imports the Admin SDK and a client component may not.\n */\n\n/** Header naming the row count the export undertook to send. */\nexport const AI_USAGE_EXPORT_ROWS_HEADER = 'X-Aglyn-Export-Rows'\n\n/** A row as a customer sees it — credits and a share, never `estCostUsd`. */\nexport interface OrgAiUsageRowWire {\n uid: string\n /** The roster's name — display name, else email, else the uid. */\n name: string\n email: string | null\n role: string | null\n credits: number\n /** Of the workspace's measured spend for the month, in `[0, 1]`. */\n share: number\n requests: number\n refusals: number\n byKind: Record<string, number>\n byHost: Record<string, number>\n /** Credits on the site the request named, when it named one. */\n hostCredits?: number\n}\n\nexport interface OrgAiUsageTableWire {\n month: string\n /** The months a reader may ask for, newest first. */\n months: string[]\n /** The workspace's own credits for the month — the pool the shares are of. */\n orgCredits: number\n rows: OrgAiUsageRowWire[]\n /**\n * `view=prepared` with a site and `uids`: each asked person's credits on\n * that site this month, 0 for someone with none — the figure the site's\n * collaborators table prints per row (\"AI credits (site, month)\"), so an app\n * reads one cell per row without walking the table.\n */\n hostCells?: Record<string, number>\n}\n\n/** One person's month, as the member card reads it. */\nexport interface UserAiUsageMonthWire {\n month: string\n credits: number\n share: number\n requests: number\n refusals: number\n byKind: Record<string, number>\n byHost: Record<string, number>\n}\n\nexport interface UserAiUsageWire {\n uid: string\n /** Newest first. */\n months: UserAiUsageMonthWire[]\n}\n\n/** The roster's month, joined and shared, projected onto the wire. */\nexport function toAiUsageWireRow(\n row: AiUsageByUserMonth & {\n name: string\n email: string | null\n role: string | null\n share: number\n },\n hostId: string,\n): OrgAiUsageRowWire {\n const wire: OrgAiUsageRowWire = {\n uid: row.uid,\n name: row.name,\n email: row.email,\n role: row.role,\n credits: row.credits,\n share: row.share,\n requests: row.requests,\n refusals: row.refusals,\n byKind: row.byKind,\n byHost: row.byHost,\n }\n if (hostId) wire.hostCredits = row.byHost[hostId] ?? 0\n return wire\n}\n\n/** The most people one `view=prepared` request answers for. */\nexport const AI_USAGE_HOST_CELLS_MAX = 200\n\n/**\n * Each asked uid's credits on the site (`hostCredits`), 0 where the month\n * holds nothing for them — what `AiCollaboratorCreditsCell` reads from\n * `hostCreditsByUid` (`?? 0`). Blank uids are skipped and the list is capped.\n */\nexport function aiUsageHostCells(\n rows: readonly Pick<OrgAiUsageRowWire, 'uid' | 'hostCredits'>[],\n uids: readonly string[],\n): Record<string, number> {\n const byUid = new Map(rows.map((row) => [row.uid, row.hostCredits ?? 0]))\n const cells: Record<string, number> = {}\n for (const raw of uids.slice(0, AI_USAGE_HOST_CELLS_MAX)) {\n const uid = raw.trim()\n if (uid) cells[uid] = byUid.get(uid) ?? 0\n }\n return cells\n}\n\n/** The CSV's columns, in order. */\nexport const AI_USAGE_CSV_HEADER = [\n 'Member',\n 'Email',\n 'Role',\n 'Credits',\n 'Share',\n 'Requests',\n 'Refusals',\n 'Top kinds',\n 'Sites',\n] as const\n\n/** A share as the table and the file print it: one decimal of a percent. */\nexport function formatAiUsageShare(share: number): string {\n return `${Math.round(share * 1000) / 10}%`\n}\n\nexport function aiUsageCsvLine(row: OrgAiUsageRowWire): string {\n const kinds = topAiUsageKinds(row.byKind, 3)\n .map((entry) => `${entry.kind} ${entry.credits}`)\n .join('; ')\n const sites = Object.entries(row.byHost)\n .sort((a, b) => b[1] - a[1])\n .map(([hostId, credits]) => `${hostId} ${credits}`)\n .join('; ')\n return [\n row.name,\n row.email ?? '',\n row.role ?? '',\n row.credits,\n formatAiUsageShare(row.share),\n row.requests,\n row.refusals,\n kinds,\n sites,\n ]\n .map(csvCell)\n .join(',')\n}\n\n/**\n * THE USAGE STRIP'S ENVELOPE (AGL-2942): what every AI door adds to the\n * answer it already sends — the chat door's `done` event, the copy\n * assistant's JSON, a job's create response, and each door's quota refusal —\n * so the strip in the panel and the generation dialogs updates with no read\n * of its own.\n *\n * Credits only. `pool` is the workspace's month against its band; `mine` is\n * the caller's own month, measured against the allotment that binds them\n * when one does — for a site allotment, the site's month, because that is\n * the line the next request meets.\n */\nexport type AiUsageMeterState = 'ok' | 'warn' | 'capped'\n\nexport interface AiUsageMeterWire {\n month: string\n pool: { used: number; limit: number | null }\n mine: {\n used: number\n limit: number | null\n mode: AiAllotmentMode | null\n scope: AiCreditAllotmentScope | null\n /**\n * `mine` is the person's Free allowance (AGL-3722): `used` of the 300\n * free credits a month every Free workspace they hold draws on, net of\n * give-backs. Absent on a paid workspace.\n */\n free?: boolean\n }\n /** Credits the exchange this envelope answers cost; `null` when no model ran. */\n last: number | null\n /** True when the envelope rides a refusal. */\n refused: boolean\n state: AiUsageMeterState\n /** The model that answered, as the catalog names it. */\n model: { id: string; label: string; auto: boolean } | null\n}\n\n/**\n * `capped` when a hard allotment is spent or the request was refused, `warn`\n * from 80% of the allotment that binds or of the pool, `ok` otherwise.\n */\nexport function aiUsageMeterState(\n meter: Pick<AiUsageMeterWire, 'pool' | 'mine' | 'refused'>,\n): AiUsageMeterState {\n const { mine, pool } = meter\n if (meter.refused) return 'capped'\n if (mine.limit !== null && mine.mode === 'hard' && mine.used >= mine.limit) return 'capped'\n const past = (used: number, limit: number | null) =>\n limit !== null && limit > 0 && used >= limit * AI_ALLOTMENT_WARN_SHARE\n return past(mine.used, mine.limit) || past(pool.used, pool.limit) ? 'warn' : 'ok'\n}\n\n/**\n * What `/api/ai/allotments` answers (AGL-2942): the allotments a reader may\n * see, each with what its subject drew this month, and the roster, sites\n * and models the editors pick from. Credits only.\n */\nexport interface AiAllotmentRowWire {\n subject: string\n scope: 'member' | 'collab' | 'host' | 'org'\n uid: string | null\n hostId: string | null\n credits: number | null\n mode: AiAllotmentMode\n models: string[] | null\n /** Credits the subject drew this month — the figure the gate measures. */\n used: number\n}\n\nexport interface AiAllotmentsMemberWire {\n uid: string\n name: string\n email: string | null\n role: string | null\n /** False for a site collaborator, whose allotments are per site. */\n orgWide: boolean\n /** Credits this month, every site. */\n credits: number\n /** Credits this month by site. */\n byHost: Record<string, number>\n}\n\nexport interface AiAllotmentsHostWire {\n hostId: string\n name: string\n /** The site's credits this month, everyone on it. */\n credits: number\n}\n\nexport interface AiAllotmentsWire {\n /** The org the answer is about — resolved from the site when only one was named. */\n orgId: string\n month: string\n pool: { used: number; limit: number | null }\n allotments: AiAllotmentRowWire[]\n members: AiAllotmentsMemberWire[]\n hosts: AiAllotmentsHostWire[]\n /** The catalog models this deployment serves, for an allowlist. */\n models: Array<{ id: string; label: string; tier: string }>\n /** Whether the plan lets the workspace restrict models org-wide. */\n restrictionAvailable: boolean\n canEdit: {\n /** Members, sites and the restriction: `billing.manage`. */\n billing: boolean\n /** Collaborators on the site asked about: `billing.manage`, or its admin. */\n collaborators: boolean\n }\n /** Who is reading, so an editor can refuse to offer a self-raise. */\n callerUid: string\n}\n\n/**\n * `2026-09` as a reader says it, on the UTC calendar the key is cut from, in\n * the reader's locale (the staff apps' prepared views pass `en-US`).\n */\nexport function aiUsageMonthLabel(month: string, locale?: string): string {\n const [year, monthOfYear] = month.split('-').map(Number)\n if (!year || !monthOfYear) return month\n try {\n return new Date(Date.UTC(year, monthOfYear - 1, 1)).toLocaleDateString(locale, {\n month: 'long',\n year: 'numeric',\n timeZone: 'UTC',\n })\n } catch {\n return month\n }\n}\n"],"names":["topAiUsageKinds","AI_ALLOTMENT_WARN_SHARE","csvCell","AI_USAGE_EXPORT_ROWS_HEADER","toAiUsageWireRow","row","hostId","wire","uid","name","email","role","credits","share","requests","refusals","byKind","byHost","hostCredits","AI_USAGE_HOST_CELLS_MAX","aiUsageHostCells","rows","uids","byUid","Map","map","cells","raw","slice","trim","get","AI_USAGE_CSV_HEADER","formatAiUsageShare","Math","round","aiUsageCsvLine","kinds","entry","kind","join","sites","Object","entries","sort","a","b","aiUsageMeterState","meter","mine","pool","refused","limit","mode","used","past","aiUsageMonthLabel","month","locale","year","monthOfYear","split","Number","Date","UTC","toLocaleDateString","timeZone"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,0EAA0E;AAC1E,+EAA+E;AAC/E,SACEA,eAAe,QAEV,+BAA2B;AAClC,SACEC,uBAAuB,QAGlB,4BAAwB;AAC/B,SAASC,OAAO,QAAQ,oCAAmC;AAE3D;;;;;CAKC,GAED,8DAA8D,GAC9D,OAAO,MAAMC,8BAA8B,sBAAqB;AAqDhE,oEAAoE,GACpE,OAAO,SAASC,iBACdC,GAKC,EACDC,MAAc;QAciBD;IAZ/B,MAAME,OAA0B;QAC9BC,KAAKH,IAAIG,GAAG;QACZC,MAAMJ,IAAII,IAAI;QACdC,OAAOL,IAAIK,KAAK;QAChBC,MAAMN,IAAIM,IAAI;QACdC,SAASP,IAAIO,OAAO;QACpBC,OAAOR,IAAIQ,KAAK;QAChBC,UAAUT,IAAIS,QAAQ;QACtBC,UAAUV,IAAIU,QAAQ;QACtBC,QAAQX,IAAIW,MAAM;QAClBC,QAAQZ,IAAIY,MAAM;IACpB;IACA,IAAIX,QAAQC,KAAKW,WAAW,IAAGb,qBAAAA,IAAIY,MAAM,CAACX,OAAO,YAAlBD,qBAAsB;IACrD,OAAOE;AACT;AAEA,6DAA6D,GAC7D,OAAO,MAAMY,0BAA0B,IAAG;AAE1C;;;;CAIC,GACD,OAAO,SAASC,iBACdC,IAA+D,EAC/DC,IAAuB;IAEvB,MAAMC,QAAQ,IAAIC,IAAIH,KAAKI,GAAG,CAAC,CAACpB;YAAkBA;eAAV;YAACA,IAAIG,GAAG;aAAEH,mBAAAA,IAAIa,W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