@aglyn/aglyn 1.0.0-beta.261 → 1.0.0-beta.263

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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../libs/aglyn/src/lib/plugin-manager/plugin-figures.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\nimport { getRegisteringPluginId } from '../app-utils/registering-plugin'\nimport {\n definePluginServiceContract,\n registerPluginService,\n resolvePluginServices,\n} from './plugin-services'\n\n/**\n * Figures one plugin publishes for another (AGL-2915).\n *\n * The records behind a figure belong to the plugin that models them: orders\n * to commerce, bookings to bookings, campaigns and experiments to marketing.\n * The owner knows which documents count, which are test data, what a refund\n * does to revenue and which fields could name a person. A different plugin\n * that wants the figure — a weekly report, an export, an assistant answering\n * a question about the numbers — must not read those documents itself,\n * because it would restate the owner's rules and drift from them the day the\n * owner changes one. Nor may it import the owner, which the package map\n * forbids.\n *\n * So the owner registers a READER from its server surfaces, and a caller asks\n * for one by id. A reader answers with one compact table:\n *\n * - read-only: a reader writes nothing, schedules nothing and sends nothing;\n * - aggregate: counts, sums, rates and labels, never the records themselves;\n * - about nobody: no row names a person, and no cell carries a name, an email\n * address, a phone number or a street address — a customer is counted,\n * never listed;\n * - bounded: at most `PLUGIN_FIGURE_MAX_ROWS` rows, each labelled with where\n * in the console the figures come from.\n *\n * Built on the typed service registry (`plugin-services`): one contract, every\n * reader registered under its id as the service's key.\n *\n * ## The registry carries no authorization of the CALLER\n *\n * Like `plugin-resource-drafts`, a reader is a pre-authorized operation: it\n * reads the site or the workspace it is handed. A caller establishes, in its\n * own terms and before it asks, who is asking, that the org owns the site, that\n * the person may see that site's figures, and that the owning plugin is\n * switched on for it.\n *\n * ## A table is checked on the way out as well as on the way in\n *\n * `normalizePluginFigureTable` holds whatever a reader returned to the shape\n * above — row and cell bounds, column kinds, and personal details masked in\n * every text cell — so a consumer that shows or forwards a table relies on the\n * contract rather than on every reader having kept it.\n */\n\n/** Whether a reader's figures are about one site or about the whole workspace. */\nexport type PluginFigureScope = 'site' | 'org'\n\n/**\n * How a column's cells read:\n *\n * - `text` — a label: a page path, a form's name, a region;\n * - `count` — a whole number of things;\n * - `number` — any other figure, such as an average per day;\n * - `money` — an amount in major units of the column's `currency`;\n * - `percent` — a share or a rate, from 0 to 100;\n * - `change` — the percent change from the window before; `null` where there\n * is no earlier window to compare with, never 0 or 100;\n * - `duration` — seconds;\n * - `date` — a calendar day, `YYYY-MM-DD`.\n */\nexport type PluginFigureColumnKind =\n | 'text'\n | 'count'\n | 'number'\n | 'money'\n | 'percent'\n | 'change'\n | 'duration'\n | 'date'\n\nexport const PLUGIN_FIGURE_COLUMN_KINDS: readonly PluginFigureColumnKind[] = [\n 'text',\n 'count',\n 'number',\n 'money',\n 'percent',\n 'change',\n 'duration',\n 'date',\n]\n\nexport interface PluginFigureColumn {\n /** The row field the column reads. */\n key: string\n label: string\n kind: PluginFigureColumnKind\n /** The ISO 4217 code of a `money` column. */\n currency?: string\n}\n\nexport type PluginFigureCell = string | number | null\n\nexport type PluginFigureRow = Record<string, PluginFigureCell>\n\nexport interface PluginFigureTable {\n title: string\n /**\n * Where the figures come from: the name of the console page, and its path\n * under the site's console root for a site reader, or under the workspace's\n * for an org reader (`commerce/orders`), so a consumer can link to it.\n */\n source: { label: string; path: string | null }\n /** The days the figures cover, inclusive; `null` for a current count. */\n period: { from: string; to: string; days: number } | null\n columns: PluginFigureColumn[]\n rows: PluginFigureRow[]\n /** Rows the reader left out past its bound. */\n omitted: number\n /** Short plain sentences a reader of the figures needs, such as a window with nothing earlier to compare with. */\n notes: string[]\n}\n\nexport interface PluginFigureRequest {\n orgId: string\n /** The site a `site` reader reads; for an `org` reader, the site the figures are narrowed to, or `null`. */\n hostId: string | null\n /** The window, in days, ending at `now`: one of the reader's `windows`, or 0 for a reader with none. */\n days: number\n now: Date\n /**\n * The member asking. A reader of records a member may see only part of —\n * a dataset shared with some sites — reads what this member may see; with\n * `null`, nobody asked, and it reads only what every member of the site may.\n */\n uid: string | null\n /** The reader's own parameters, by the names its `params` declare; each a string. */\n params: Readonly<Record<string, string>>\n}\n\n/** One parameter a reader takes beyond its window. */\nexport interface PluginFigureParam {\n /** The name a request carries it under: letters, digits and underscores. */\n name: string\n /** What it selects, in one plain sentence. */\n description: string\n required: boolean\n}\n\nexport type PluginFigureRead =\n | { ok: true; table: PluginFigureTable }\n | {\n ok: false\n status: 400 | 403 | 404\n /** Customer-safe: a caller shows it as it stands. */\n error: string\n }\n\nexport interface PluginFigureReader {\n /** Stable and unique across plugins: lowercase words joined by dots (`commerce.orders`). */\n id: string\n /** What a person calls the figures: \"Orders\". */\n label: string\n /**\n * What the reader answers, in one or two plain sentences. The same bytes for\n * every site, so a list of readers can be shown to anyone who may use them.\n */\n description: string\n scope: PluginFigureScope\n /** The windows the reader covers, in days; empty for a reader of current counts. */\n windows: readonly number[]\n /**\n * The plugin whose records the figures are read from, which must run on the\n * site — or on the workspace, for an `org` reader — before anyone is offered\n * the reader. Absent, the plugin that registered it; `null` for figures the\n * platform itself keeps, such as page views.\n */\n plugin?: string | null\n /** The plan feature the figures are sold under; a workspace without it is not offered the reader. */\n feature?: string\n /** The parameters the reader takes beyond its window; absent for none. */\n params?: readonly PluginFigureParam[]\n read(request: PluginFigureRequest): Promise<PluginFigureRead>\n}\n\nexport const PLUGIN_FIGURES = definePluginServiceContract<PluginFigureReader>('core.figures', {\n multiple: true,\n})\n\n/** The most rows one table carries; the rest are counted in `omitted`. */\nexport const PLUGIN_FIGURE_MAX_ROWS = 25\n\n/** The most columns one table carries. */\nexport const PLUGIN_FIGURE_MAX_COLUMNS = 8\n\n/** The longest text cell, title or label a table carries, in characters. */\nexport const PLUGIN_FIGURE_MAX_TEXT = 120\n\n/** The most notes one table carries. */\nexport const PLUGIN_FIGURE_MAX_NOTES = 3\n\nconst READER_ID = /^[a-z][a-z0-9-]*(?:\\.[a-z][a-z0-9-]*)+$/\nconst PARAM_NAME = /^[A-Za-z][A-Za-z0-9_]{0,39}$/\nconst COLUMN_KEY = /^[A-Za-z][A-Za-z0-9_]{0,39}$/\nconst CONSOLE_PATH = /^[a-z0-9][a-z0-9/_-]{0,119}$/\nconst CALENDAR_DAY = /^\\d{4}-\\d{2}-\\d{2}$/\nconst CURRENCY = /^[A-Z]{3}$/\n\n/**\n * Registers a reader. The owner is the loader's marker when a register fn is\n * running, else `options.pluginId`; with neither the registration throws. A\n * reader id another plugin already registered throws naming both, and the\n * incumbent keeps serving; the same plugin registering again replaces its own.\n */\nexport function registerPluginFigureReader(\n reader: PluginFigureReader,\n options?: { pluginId?: string },\n): void {\n const key = reader.id.trim()\n if (!READER_ID.test(key)) {\n throw new Error(`a figure reader id is lowercase words joined by dots; refused \"${reader.id}\"`)\n }\n for (const param of reader.params ?? []) {\n if (!PARAM_NAME.test(param.name)) {\n throw new Error(`figure reader \"${key}\" declares a parameter named \"${param.name}\", which is not a name`)\n }\n }\n const pluginId = (getRegisteringPluginId() ?? options?.pluginId ?? '').trim()\n const incumbent = resolvePluginServices(PLUGIN_FIGURES).find((entry) => entry.key === key)\n if (incumbent && pluginId && incumbent.pluginId !== pluginId) {\n throw new Error(\n `figure reader \"${key}\" is already registered by \"${incumbent.pluginId}\"; refused \"${pluginId}\"`,\n )\n }\n registerPluginService(PLUGIN_FIGURES, reader, {\n ...(options?.pluginId ? { pluginId: options.pluginId } : {}),\n key,\n })\n}\n\nexport interface ResolvedPluginFigureReader {\n /** The plugin that owns the records behind the figures. */\n pluginId: string\n reader: PluginFigureReader\n}\n\n/** The reader registered under an id, with its owner, or `null`. */\nexport function pluginFigureReader(id: string): ResolvedPluginFigureReader | null {\n const key = id.trim()\n const entry = resolvePluginServices(PLUGIN_FIGURES).find((one) => one.key === key)\n return entry ? { pluginId: entry.pluginId, reader: entry.impl } : null\n}\n\n/** Every registered reader with its owner, ordered by id so a list of them is the same bytes on every call. */\nexport function listPluginFigureReaders(): ResolvedPluginFigureReader[] {\n return resolvePluginServices(PLUGIN_FIGURES)\n .map((entry) => ({ pluginId: entry.pluginId, reader: entry.impl }))\n .sort((a, b) => a.reader.id.localeCompare(b.reader.id))\n}\n\n/** The plugin that must run for a reader to be offered: its own `plugin`, else its owner; `null` for platform figures. */\nexport function pluginFigureReaderPlugin(resolved: ResolvedPluginFigureReader): string | null {\n return resolved.reader.plugin === undefined ? resolved.pluginId : resolved.reader.plugin\n}\n\n/**\n * The parameters a request carries that the reader declares, each trimmed to\n * a short string, or the name of the first required one that is missing.\n */\nexport function pluginFigureParams(\n reader: Pick<PluginFigureReader, 'params'>,\n raw: Readonly<Record<string, unknown>> | null | undefined,\n): { ok: true; params: Record<string, string> } | { ok: false; missing: string } {\n const params: Record<string, string> = {}\n for (const param of reader.params ?? []) {\n const value = raw?.[param.name]\n const text = typeof value === 'string' ? value.trim().slice(0, PLUGIN_FIGURE_MAX_TEXT) : ''\n if (text) params[param.name] = text\n else if (param.required) return { ok: false, missing: param.name }\n }\n return { ok: true, params }\n}\n\n// ── Personal details ────────────────────────────────────────────────────\n\nconst EMAIL = /[\\p{L}\\p{N}._%+-]+@[\\p{L}\\p{N}-]+(?:\\.[\\p{L}\\p{N}-]+)+/gu\n/** A run of digits with the separators a phone number is written with; its digits are counted apart. */\nconst PHONE_LIKE = /(?<![\\p{L}\\p{N}/_])\\+?\\(?\\d[\\d\\s().-]{6,}\\d(?![\\p{L}\\p{N}/_])/gu\n\n/** Nine to fifteen digits: a phone number's length anywhere, and longer than any calendar day or year. */\nfunction looksLikePhone(match: string): boolean {\n const digits = match.replace(/\\D/g, '').length\n return digits >= 9 && digits <= 15\n}\n\n/**\n * The text with every email address and phone number replaced by a marker.\n * A figure's label is never meant to carry either, so a match is masked\n * rather than trusted: a campaign named after a person's address says\n * nothing about the campaign that the marker does not.\n */\nexport function maskPersonalDetails(text: string): string {\n return text\n .replace(EMAIL, '[email]')\n .replace(PHONE_LIKE, (match) => (looksLikePhone(match) ? '[phone]' : match))\n}\n\n/** Whether a text carries an email address or a phone number. */\nexport function hasPersonalDetails(text: string): boolean {\n return maskPersonalDetails(text) !== text\n}\n\n// ── The table, held to the contract ─────────────────────────────────────\n\nfunction cut(value: unknown, max = PLUGIN_FIGURE_MAX_TEXT): string {\n const text = typeof value === 'string' ? value.replace(/\\s+/g, ' ').trim() : ''\n return text.length > max ? `${text.slice(0, max - 1)}…` : text\n}\n\nfunction finite(value: unknown): number | null {\n return typeof value === 'number' && Number.isFinite(value) ? value : null\n}\n\nfunction day(value: unknown): string | null {\n return typeof value === 'string' && CALENDAR_DAY.test(value) && !Number.isNaN(Date.parse(value))\n ? value\n : null\n}\n\nfunction cellFor(column: PluginFigureColumn, value: unknown): PluginFigureCell {\n switch (column.kind) {\n case 'text':\n return typeof value === 'string' ? maskPersonalDetails(cut(value)) : null\n case 'date':\n return day(value)\n case 'count': {\n const number = finite(value)\n return number === null ? null : Math.round(number)\n }\n default:\n return finite(value)\n }\n}\n\n/**\n * A reader's table held to the contract: bounded, typed and masked. Columns\n * with a key, a kind or a currency the contract does not know are dropped, and\n * every cell is read through its column, so a cell of the wrong type is `null`\n * rather than passed on. `null` for a value that is not a table at all.\n */\nexport function normalizePluginFigureTable(value: unknown): PluginFigureTable | null {\n if (!value || typeof value !== 'object' || Array.isArray(value)) return null\n const table = value as Partial<PluginFigureTable>\n const title = cut(table.title)\n if (!title) return null\n\n const seen = new Set<string>()\n const columns: PluginFigureColumn[] = []\n for (const raw of Array.isArray(table.columns) ? table.columns : []) {\n if (columns.length === PLUGIN_FIGURE_MAX_COLUMNS) break\n const column = raw as Partial<PluginFigureColumn> | null\n const key = typeof column?.key === 'string' ? column.key : ''\n const kind = column?.kind as PluginFigureColumnKind\n if (!COLUMN_KEY.test(key) || seen.has(key) || !PLUGIN_FIGURE_COLUMN_KINDS.includes(kind)) continue\n if (kind === 'money' && !(typeof column?.currency === 'string' && CURRENCY.test(column.currency))) {\n continue\n }\n seen.add(key)\n columns.push({\n key,\n label: cut(column?.label, 60) || key,\n kind,\n ...(kind === 'money' ? { currency: column?.currency as string } : {}),\n })\n }\n if (!columns.length) return null\n\n const rawRows = Array.isArray(table.rows) ? table.rows : []\n const rows = rawRows.slice(0, PLUGIN_FIGURE_MAX_ROWS).map((raw) => {\n const source = raw && typeof raw === 'object' ? (raw as Record<string, unknown>) : {}\n return Object.fromEntries(columns.map((column) => [column.key, cellFor(column, source[column.key])]))\n })\n const omitted =\n Math.max(0, rawRows.length - PLUGIN_FIGURE_MAX_ROWS) +\n (typeof table.omitted === 'number' && Number.isFinite(table.omitted) && table.omitted > 0\n ? Math.floor(table.omitted)\n : 0)\n\n const period = table.period\n const from = day(period?.from)\n const to = day(period?.to)\n const days = finite(period?.days)\n const path = typeof table.source?.path === 'string' ? table.source.path.trim() : ''\n\n return {\n title,\n source: {\n label: cut(table.source?.label, 80) || title,\n path: CONSOLE_PATH.test(path) && !path.includes('..') ? path : null,\n },\n period:\n from && to && days !== null && days >= 1 && days <= 366\n ? { from, to, days: Math.round(days) }\n : null,\n columns,\n rows,\n omitted,\n notes: (Array.isArray(table.notes) ? table.notes : [])\n .map((note) => maskPersonalDetails(cut(note, 200)))\n .filter(Boolean)\n .slice(0, PLUGIN_FIGURE_MAX_NOTES),\n }\n}\n\n// ── Windows ─────────────────────────────────────────────────────────────\n\n/** A calendar day in UTC. */\nexport function pluginFigureDay(at: Date): string {\n return at.toISOString().slice(0, 10)\n}\n\n/**\n * The window of `days` ending on the day of `now`, inclusive, and the window\n * of the same length immediately before it: what every `change` column\n * compares, so a figure from one reader compares like a figure from another.\n */\nexport function pluginFigureWindows(\n now: Date,\n days: number,\n): {\n current: { from: string; to: string; startMs: number; endMs: number }\n previous: { from: string; to: string; startMs: number; endMs: number }\n} {\n const span = Math.max(1, Math.floor(days))\n const dayMs = 86_400_000\n const endOfToday = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()) + dayMs\n const currentStart = endOfToday - span * dayMs\n const previousStart = currentStart - span * dayMs\n return {\n current: {\n from: pluginFigureDay(new Date(currentStart)),\n to: pluginFigureDay(new Date(endOfToday - dayMs)),\n startMs: currentStart,\n endMs: endOfToday,\n },\n previous: {\n from: pluginFigureDay(new Date(previousStart)),\n to: pluginFigureDay(new Date(currentStart - dayMs)),\n startMs: previousStart,\n endMs: currentStart,\n },\n }\n}\n\n/**\n * The percent change from `previous` to `current`, to one decimal place.\n * `null` when there is nothing earlier to compare with, so a first window\n * reads as no change figure at all rather than as a rise from zero.\n */\nexport function pluginFigureChange(current: number, previous: number | null): number | null {\n if (previous === null || !Number.isFinite(previous) || previous <= 0) return null\n if (!Number.isFinite(current)) return null\n return Math.round(((current - previous) / previous) * 1_000) / 10\n}\n"],"names":["getRegisteringPluginId","definePluginServiceContract","registerPluginService","resolvePluginServices","PLUGIN_FIGURE_COLUMN_KINDS","PLUGIN_FIGURES","multiple","PLUGIN_FIGURE_MAX_ROWS","PLUGIN_FIGURE_MAX_COLUMNS","PLUGIN_FIGURE_MAX_TEXT","PLUGIN_FIGURE_MAX_NOTES","READER_ID","PARAM_NAME","COLUMN_KEY","CONSOLE_PATH","CALENDAR_DAY","CURRENCY","registerPluginFigureReader","reader","options","key","id","trim","test","Error","param","params","name","pluginId","incumbent","find","entry","pluginFigureReader","one","impl","listPluginFigureReaders","map","sort","a","b","localeCompare","pluginFigureReaderPlugin","resolved","plugin","undefined","pluginFigureParams","raw","value","text","slice","required","ok","missing","EMAIL","PHONE_LIKE","looksLikePhone","match","digits","replace","length","maskPersonalDetails","hasPersonalDetails","cut","max","finite","Number","isFinite","day","isNaN","Date","parse","cellFor","column","kind","number","Math","round","normalizePluginFigureTable","table","Array","isArray","title","seen","Set","columns","has","includes","currency","add","push","label","rawRows","rows","source","Object","fromEntries","omitted","floor","period","from","to","days","path","notes","note","filter","Boolean","pluginFigureDay","at","toISOString","pluginFigureWindows","now","span","dayMs","endOfToday","UTC","getUTCFullYear","getUTCMonth","getUTCDate","currentStart","previousStart","current","startMs","endMs","previous","pluginFigureChange"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,sBAAsB,QAAQ,qCAAiC;AACxE,SACEC,2BAA2B,EAC3BC,qBAAqB,EACrBC,qBAAqB,QAChB,uBAAmB;AAuE1B,OAAO,MAAMC,6BAAgE;IAC3E;IACA;IACA;IACA;IACA;IACA;IACA;IACA;CACD,CAAA;AA+FD,OAAO,MAAMC,iBAAiBJ,4BAAgD,gBAAgB;IAC5FK,UAAU;AACZ,GAAE;AAEF,wEAAwE,GACxE,OAAO,MAAMC,yBAAyB,GAAE;AAExC,wCAAwC,GACxC,OAAO,MAAMC,4BAA4B,EAAC;AAE1C,0EAA0E,GAC1E,OAAO,MAAMC,yBAAyB,IAAG;AAEzC,sCAAsC,GACtC,OAAO,MAAMC,0BAA0B,EAAC;AAExC,MAAMC,YAAY;AAClB,MAAMC,aAAa;AACnB,MAAMC,aAAa;AACnB,MAAMC,eAAe;AACrB,MAAMC,eAAe;AACrB,MAAMC,WAAW;AAEjB;;;;;CAKC,GACD,OAAO,SAASC,2BACdC,MAA0B,EAC1BC,OAA+B;QAMXD,gBAKFlB,MAAAA;IATlB,MAAMoB,MAAMF,OAAOG,EAAE,CAACC,IAAI;IAC1B,IAAI,CAACX,UAAUY,IAAI,CAACH,MAAM;QACxB,MAAM,IAAII,MAAM,CAAC,+DAA+D,EAAEN,OAAOG,EAAE,CAAC,CAAC,CAAC;IAChG;IACA,KAAK,MAAMI,UAASP,iBAAAA,OAAOQ,MAAM,YAAbR,iBAAiB,EAAE,CAAE;QACvC,IAAI,CAACN,WAAWW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8C,GAAG,CAACJ,IAAIK,cAAc,IAAIL,IAAIM,WAAW,IAAIN,IAAIO,UAAU,MAAML;IACzF,MAAMM,eAAeL,aAAaF,OAAOC;IACzC,MAAMO,gBAAgBD,eAAeP,OAAOC;IAC5C,OAAO;QACLQ,SAAS;YACPtB,MAAMQ,gBAAgB,IAAItC,KAAKkD;YAC/BnB,IAAIO,gBAAgB,IAAItC,KAAK6C,aAAaD;YAC1CS,SAASH;YACTI,OAAOT;QACT;QACAU,UAAU;YACRzB,MAAMQ,gBAAgB,IAAItC,KAAKmD;YAC/BpB,IAAIO,gBAAgB,IAAItC,KAAKkD,eAAeN;YAC5CS,SAASF;YACTG,OAAOJ;QACT;IACF;AACF;AAEA;;;;CAIC,GACD,OAAO,SAASM,mBAAmBJ,OAAe,EAAEG,QAAuB;IACzE,IAAIA,aAAa,QAAQ,CAAC3D,OAAOC,QAAQ,CAAC0D,aAAaA,YAAY,GAAG,OAAO;IAC7E,IAAI,CAAC3D,OAAOC,QAAQ,CAACuD,UAAU,OAAO;IACtC,OAAO9C,KAAKC,KAAK,CAAC,AAAE6C,CAAAA,UAAUG,QAAO,IAAKA,WAAY,QAAS;AACjE"}
1
+ {"version":3,"sources":["../../../../../../libs/aglyn/src/lib/plugin-manager/plugin-figures.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\nimport { getRegisteringPluginId } from '../app-utils/registering-plugin'\nimport {\n definePluginServiceContract,\n registerPluginService,\n resolvePluginServices,\n} from './plugin-services'\n\n/**\n * Figures one plugin publishes for another (AGL-2915).\n *\n * The records behind a figure belong to the plugin that models them: orders\n * to commerce, bookings to bookings, campaigns and experiments to marketing.\n * The owner knows which documents count, which are test data, what a refund\n * does to revenue and which fields could name a person. A different plugin\n * that wants the figure — a weekly report, an export, an assistant answering\n * a question about the numbers — must not read those documents itself,\n * because it would restate the owner's rules and drift from them the day the\n * owner changes one. Nor may it import the owner, which the package map\n * forbids.\n *\n * So the owner registers a READER from its server surfaces, and a caller asks\n * for one by id. A reader answers with one compact table:\n *\n * - read-only: a reader writes nothing, schedules nothing and sends nothing;\n * - aggregate: counts, sums, rates and labels, never the records themselves;\n * - about nobody: no row names a person, and no cell carries a name, an email\n * address, a phone number or a street address — a customer is counted,\n * never listed;\n * - bounded: at most `PLUGIN_FIGURE_MAX_ROWS` rows, each labelled with where\n * in the console the figures come from.\n *\n * Built on the typed service registry (`plugin-services`): one contract, every\n * reader registered under its id as the service's key.\n *\n * ## The registry carries no authorization of the CALLER\n *\n * Like `plugin-resource-drafts`, a reader is a pre-authorized operation: it\n * reads the site or the workspace it is handed. A caller establishes, in its\n * own terms and before it asks, who is asking, that the org owns the site, that\n * the person may see that site's figures, and that the owning plugin is\n * switched on for it.\n *\n * ## A table is checked on the way out as well as on the way in\n *\n * `normalizePluginFigureTable` holds whatever a reader returned to the shape\n * above — row and cell bounds, column kinds, and personal details masked in\n * every text cell — so a consumer that shows or forwards a table relies on the\n * contract rather than on every reader having kept it.\n */\n\n/** Whether a reader's figures are about one site or about the whole workspace. */\nexport type PluginFigureScope = 'site' | 'org'\n\n/**\n * How a column's cells read:\n *\n * - `text` — a label: a page path, a form's name, a region;\n * - `count` — a whole number of things;\n * - `number` — any other figure, such as an average per day;\n * - `money` — an amount in major units of the column's `currency`;\n * - `percent` — a share or a rate, from 0 to 100;\n * - `change` — the percent change from the window before; `null` where there\n * is no earlier window to compare with, never 0 or 100;\n * - `duration` — seconds;\n * - `date` — a calendar day, `YYYY-MM-DD`.\n */\nexport type PluginFigureColumnKind =\n | 'text'\n | 'count'\n | 'number'\n | 'money'\n | 'percent'\n | 'change'\n | 'duration'\n | 'date'\n\nexport const PLUGIN_FIGURE_COLUMN_KINDS: readonly PluginFigureColumnKind[] = [\n 'text',\n 'count',\n 'number',\n 'money',\n 'percent',\n 'change',\n 'duration',\n 'date',\n]\n\nexport interface PluginFigureColumn {\n /** The row field the column reads. */\n key: string\n label: string\n kind: PluginFigureColumnKind\n /** The ISO 4217 code of a `money` column. */\n currency?: string\n}\n\nexport type PluginFigureCell = string | number | null\n\nexport type PluginFigureRow = Record<string, PluginFigureCell>\n\nexport interface PluginFigureTable {\n title: string\n /**\n * Where the figures come from: the name of the console page, and its path\n * under the site's console root for a site reader, or under the workspace's\n * for an org reader (`commerce/orders`), so a consumer can link to it.\n */\n source: { label: string; path: string | null }\n /** The days the figures cover, inclusive; `null` for a current count. */\n period: { from: string; to: string; days: number } | null\n columns: PluginFigureColumn[]\n rows: PluginFigureRow[]\n /** Rows the reader left out past its bound. */\n omitted: number\n /** Short plain sentences a reader of the figures needs, such as a window with nothing earlier to compare with. */\n notes: string[]\n}\n\nexport interface PluginFigureRequest {\n orgId: string\n /** The site a `site` reader reads; for an `org` reader, the site the figures are narrowed to, or `null`. */\n hostId: string | null\n /** The window, in days, ending at `now`: one of the reader's `windows`, or 0 for a reader with none. */\n days: number\n now: Date\n /**\n * The member asking. A reader of records a member may see only part of —\n * a dataset shared with some sites — reads what this member may see; with\n * `null`, nobody asked, and it reads only what every member of the site may.\n */\n uid: string | null\n /** The reader's own parameters, by the names its `params` declare; each a string. */\n params: Readonly<Record<string, string>>\n}\n\n/** One parameter a reader takes beyond its window. */\nexport interface PluginFigureParam {\n /** The name a request carries it under: letters, digits and underscores. */\n name: string\n /** What it selects, in one plain sentence. */\n description: string\n required: boolean\n}\n\nexport type PluginFigureRead =\n | { ok: true; table: PluginFigureTable }\n | {\n ok: false\n status: 400 | 403 | 404\n /** Customer-safe: a caller shows it as it stands. */\n error: string\n }\n\nexport interface PluginFigureReader {\n /** Stable and unique across plugins: lowercase words joined by dots (`commerce.orders`). */\n id: string\n /** What a person calls the figures: \"Orders\". */\n label: string\n /**\n * What the reader answers, in one or two plain sentences. The same bytes for\n * every site, so a list of readers can be shown to anyone who may use them.\n */\n description: string\n scope: PluginFigureScope\n /** The windows the reader covers, in days; empty for a reader of current counts. */\n windows: readonly number[]\n /**\n * The plugin whose records the figures are read from, which must run on the\n * site — or on the workspace, for an `org` reader — before anyone is offered\n * the reader. Absent, the plugin that registered it; `null` for figures the\n * platform itself keeps, such as page views.\n */\n plugin?: string | null\n /** The plan feature the figures are sold under; a workspace without it is not offered the reader. */\n feature?: string\n /** The parameters the reader takes beyond its window; absent for none. */\n params?: readonly PluginFigureParam[]\n read(request: PluginFigureRequest): Promise<PluginFigureRead>\n /**\n * How the reader's table reads in the weekly summary email (AGL-3867), for\n * a reader that belongs in it; absent, the summary leaves it out. The owner\n * words its own figures, because only the owner knows which of its columns\n * say what happened: the summary reads it over 7 days with no parameters\n * and lists what `lines` answers.\n */\n weeklySummary?: PluginFigureWeeklySummary\n}\n\n/** A headline figure, drawn as a tile: \"577 Visitors ▼ 27.9%\". */\nexport interface PluginFigureSummaryStat {\n /** What it counts, as a tile names it: \"Visitors\". */\n label: string\n /** The figure as a person reads it: \"577\", \"$1,240.00\". */\n value: string\n /** The percent change on the week before; `null` or absent when there is nothing to compare with. */\n change?: number | null\n /**\n * A count the workspace overview adds up across sites and the subject line\n * may lead with, and its noun: `{ count: 577, one: 'visitor', many:\n * 'visitors' }`. Stats with the same `one` are summed.\n */\n total?: { count: number; one: string; many: string }\n}\n\n/** One reader's part of a weekly summary: a titled block of tiles, ranked rows, or both. */\nexport interface PluginFigureSummaryPart {\n /** The block's heading: \"Traffic\", \"Top pages\". */\n title: string\n stats?: readonly PluginFigureSummaryStat[]\n /** A ranked list, most first: a page and its views, a form and its submissions. */\n rows?: ReadonlyArray<{ label: string; value: string }>\n /** One plain sentence the figures need. */\n note?: string\n}\n\n/** A reader's part in the weekly summary. */\nexport interface PluginFigureWeeklySummary {\n /**\n * Which email lists it: `site`, each site's own; `workspace`, the\n * workspace overview. A workspace reader may say `site` to be read\n * narrowed to each site. Absent, a site reader is `site` and a workspace\n * reader is `workspace`.\n */\n per?: 'site' | 'workspace'\n /** Where its block sits: lower first. */\n order: number\n /** What the table says this week, or `null` when it has nothing to report. */\n summarize(table: PluginFigureTable): PluginFigureSummaryPart | null\n}\n\nexport const PLUGIN_FIGURES = definePluginServiceContract<PluginFigureReader>('core.figures', {\n multiple: true,\n})\n\n/** The most rows one table carries; the rest are counted in `omitted`. */\nexport const PLUGIN_FIGURE_MAX_ROWS = 25\n\n/** The most columns one table carries. */\nexport const PLUGIN_FIGURE_MAX_COLUMNS = 8\n\n/** The longest text cell, title or label a table carries, in characters. */\nexport const PLUGIN_FIGURE_MAX_TEXT = 120\n\n/** The most notes one table carries. */\nexport const PLUGIN_FIGURE_MAX_NOTES = 3\n\nconst READER_ID = /^[a-z][a-z0-9-]*(?:\\.[a-z][a-z0-9-]*)+$/\nconst PARAM_NAME = /^[A-Za-z][A-Za-z0-9_]{0,39}$/\nconst COLUMN_KEY = /^[A-Za-z][A-Za-z0-9_]{0,39}$/\nconst CONSOLE_PATH = /^[a-z0-9][a-z0-9/_-]{0,119}$/\nconst CALENDAR_DAY = /^\\d{4}-\\d{2}-\\d{2}$/\nconst CURRENCY = /^[A-Z]{3}$/\n\n/**\n * Registers a reader. The owner is the loader's marker when a register fn is\n * running, else `options.pluginId`; with neither the registration throws. A\n * reader id another plugin already registered throws naming both, and the\n * incumbent keeps serving; the same plugin registering again replaces its own.\n */\nexport function registerPluginFigureReader(\n reader: PluginFigureReader,\n options?: { pluginId?: string },\n): void {\n const key = reader.id.trim()\n if (!READER_ID.test(key)) {\n throw new Error(`a figure reader id is lowercase words joined by dots; refused \"${reader.id}\"`)\n }\n for (const param of reader.params ?? []) {\n if (!PARAM_NAME.test(param.name)) {\n throw new Error(`figure reader \"${key}\" declares a parameter named \"${param.name}\", which is not a name`)\n }\n }\n const pluginId = (getRegisteringPluginId() ?? options?.pluginId ?? '').trim()\n const incumbent = resolvePluginServices(PLUGIN_FIGURES).find((entry) => entry.key === key)\n if (incumbent && pluginId && incumbent.pluginId !== pluginId) {\n throw new Error(\n `figure reader \"${key}\" is already registered by \"${incumbent.pluginId}\"; refused \"${pluginId}\"`,\n )\n }\n registerPluginService(PLUGIN_FIGURES, reader, {\n ...(options?.pluginId ? { pluginId: options.pluginId } : {}),\n key,\n })\n}\n\nexport interface ResolvedPluginFigureReader {\n /** The plugin that owns the records behind the figures. */\n pluginId: string\n reader: PluginFigureReader\n}\n\n/** The reader registered under an id, with its owner, or `null`. */\nexport function pluginFigureReader(id: string): ResolvedPluginFigureReader | null {\n const key = id.trim()\n const entry = resolvePluginServices(PLUGIN_FIGURES).find((one) => one.key === key)\n return entry ? { pluginId: entry.pluginId, reader: entry.impl } : null\n}\n\n/** Every registered reader with its owner, ordered by id so a list of them is the same bytes on every call. */\nexport function listPluginFigureReaders(): ResolvedPluginFigureReader[] {\n return resolvePluginServices(PLUGIN_FIGURES)\n .map((entry) => ({ pluginId: entry.pluginId, reader: entry.impl }))\n .sort((a, b) => a.reader.id.localeCompare(b.reader.id))\n}\n\n/** The plugin that must run for a reader to be offered: its own `plugin`, else its owner; `null` for platform figures. */\nexport function pluginFigureReaderPlugin(resolved: ResolvedPluginFigureReader): string | null {\n return resolved.reader.plugin === undefined ? resolved.pluginId : resolved.reader.plugin\n}\n\n/**\n * The parameters a request carries that the reader declares, each trimmed to\n * a short string, or the name of the first required one that is missing.\n */\nexport function pluginFigureParams(\n reader: Pick<PluginFigureReader, 'params'>,\n raw: Readonly<Record<string, unknown>> | null | undefined,\n): { ok: true; params: Record<string, string> } | { ok: false; missing: string } {\n const params: Record<string, string> = {}\n for (const param of reader.params ?? []) {\n const value = raw?.[param.name]\n const text = typeof value === 'string' ? value.trim().slice(0, PLUGIN_FIGURE_MAX_TEXT) : ''\n if (text) params[param.name] = text\n else if (param.required) return { ok: false, missing: param.name }\n }\n return { ok: true, params }\n}\n\n// ── Personal details ────────────────────────────────────────────────────\n\nconst EMAIL = /[\\p{L}\\p{N}._%+-]+@[\\p{L}\\p{N}-]+(?:\\.[\\p{L}\\p{N}-]+)+/gu\n/** A run of digits with the separators a phone number is written with; its digits are counted apart. */\nconst PHONE_LIKE = /(?<![\\p{L}\\p{N}/_])\\+?\\(?\\d[\\d\\s().-]{6,}\\d(?![\\p{L}\\p{N}/_])/gu\n\n/** Nine to fifteen digits: a phone number's length anywhere, and longer than any calendar day or year. */\nfunction looksLikePhone(match: string): boolean {\n const digits = match.replace(/\\D/g, '').length\n return digits >= 9 && digits <= 15\n}\n\n/**\n * The text with every email address and phone number replaced by a marker.\n * A figure's label is never meant to carry either, so a match is masked\n * rather than trusted: a campaign named after a person's address says\n * nothing about the campaign that the marker does not.\n */\nexport function maskPersonalDetails(text: string): string {\n return text\n .replace(EMAIL, '[email]')\n .replace(PHONE_LIKE, (match) => (looksLikePhone(match) ? '[phone]' : match))\n}\n\n/** Whether a text carries an email address or a phone number. */\nexport function hasPersonalDetails(text: string): boolean {\n return maskPersonalDetails(text) !== text\n}\n\n// ── The table, held to the contract ─────────────────────────────────────\n\nfunction cut(value: unknown, max = PLUGIN_FIGURE_MAX_TEXT): string {\n const text = typeof value === 'string' ? value.replace(/\\s+/g, ' ').trim() : ''\n return text.length > max ? `${text.slice(0, max - 1)}…` : text\n}\n\nfunction finite(value: unknown): number | null {\n return typeof value === 'number' && Number.isFinite(value) ? value : null\n}\n\nfunction day(value: unknown): string | null {\n return typeof value === 'string' && CALENDAR_DAY.test(value) && !Number.isNaN(Date.parse(value))\n ? value\n : null\n}\n\nfunction cellFor(column: PluginFigureColumn, value: unknown): PluginFigureCell {\n switch (column.kind) {\n case 'text':\n return typeof value === 'string' ? maskPersonalDetails(cut(value)) : null\n case 'date':\n return day(value)\n case 'count': {\n const number = finite(value)\n return number === null ? null : Math.round(number)\n }\n default:\n return finite(value)\n }\n}\n\n/**\n * A reader's table held to the contract: bounded, typed and masked. Columns\n * with a key, a kind or a currency the contract does not know are dropped, and\n * every cell is read through its column, so a cell of the wrong type is `null`\n * rather than passed on. `null` for a value that is not a table at all.\n */\nexport function normalizePluginFigureTable(value: unknown): PluginFigureTable | null {\n if (!value || typeof value !== 'object' || Array.isArray(value)) return null\n const table = value as Partial<PluginFigureTable>\n const title = cut(table.title)\n if (!title) return null\n\n const seen = new Set<string>()\n const columns: PluginFigureColumn[] = []\n for (const raw of Array.isArray(table.columns) ? table.columns : []) {\n if (columns.length === PLUGIN_FIGURE_MAX_COLUMNS) break\n const column = raw as Partial<PluginFigureColumn> | null\n const key = typeof column?.key === 'string' ? column.key : ''\n const kind = column?.kind as PluginFigureColumnKind\n if (!COLUMN_KEY.test(key) || seen.has(key) || !PLUGIN_FIGURE_COLUMN_KINDS.includes(kind)) continue\n if (kind === 'money' && !(typeof column?.currency === 'string' && CURRENCY.test(column.currency))) {\n continue\n }\n seen.add(key)\n columns.push({\n key,\n label: cut(column?.label, 60) || key,\n kind,\n ...(kind === 'money' ? { currency: column?.currency as string } : {}),\n })\n }\n if (!columns.length) return null\n\n const rawRows = Array.isArray(table.rows) ? table.rows : []\n const rows = rawRows.slice(0, PLUGIN_FIGURE_MAX_ROWS).map((raw) => {\n const source = raw && typeof raw === 'object' ? (raw as Record<string, unknown>) : {}\n return Object.fromEntries(columns.map((column) => [column.key, cellFor(column, source[column.key])]))\n })\n const omitted =\n Math.max(0, rawRows.length - PLUGIN_FIGURE_MAX_ROWS) +\n (typeof table.omitted === 'number' && Number.isFinite(table.omitted) && table.omitted > 0\n ? Math.floor(table.omitted)\n : 0)\n\n const period = table.period\n const from = day(period?.from)\n const to = day(period?.to)\n const days = finite(period?.days)\n const path = typeof table.source?.path === 'string' ? table.source.path.trim() : ''\n\n return {\n title,\n source: {\n label: cut(table.source?.label, 80) || title,\n path: CONSOLE_PATH.test(path) && !path.includes('..') ? path : null,\n },\n period:\n from && to && days !== null && days >= 1 && days <= 366\n ? { from, to, days: Math.round(days) }\n : null,\n columns,\n rows,\n omitted,\n notes: (Array.isArray(table.notes) ? table.notes : [])\n .map((note) => maskPersonalDetails(cut(note, 200)))\n .filter(Boolean)\n .slice(0, PLUGIN_FIGURE_MAX_NOTES),\n }\n}\n\n// ── Windows ─────────────────────────────────────────────────────────────\n\n/** A calendar day in UTC. */\nexport function pluginFigureDay(at: Date): string {\n return at.toISOString().slice(0, 10)\n}\n\n/**\n * The window of `days` ending on the day of `now`, inclusive, and the window\n * of the same length immediately before it: what every `change` column\n * compares, so a figure from one reader compares like a figure from another.\n */\nexport function pluginFigureWindows(\n now: Date,\n days: number,\n): {\n current: { from: string; to: string; startMs: number; endMs: number }\n previous: { from: string; to: string; startMs: number; endMs: number }\n} {\n const span = Math.max(1, Math.floor(days))\n const dayMs = 86_400_000\n const endOfToday = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()) + dayMs\n const currentStart = endOfToday - span * dayMs\n const previousStart = currentStart - span * dayMs\n return {\n current: {\n from: pluginFigureDay(new Date(currentStart)),\n to: pluginFigureDay(new Date(endOfToday - dayMs)),\n startMs: currentStart,\n endMs: endOfToday,\n },\n previous: {\n from: pluginFigureDay(new Date(previousStart)),\n to: pluginFigureDay(new Date(currentStart - dayMs)),\n startMs: previousStart,\n endMs: currentStart,\n },\n }\n}\n\n/**\n * The percent change from `previous` to `current`, to one decimal place.\n * `null` when there is nothing earlier to compare with, so a first window\n * reads as no change figure at all rather than as a rise from zero.\n */\nexport function pluginFigureChange(current: number, previous: number | null): number | null {\n if (previous === null || !Number.isFinite(previous) || previous <= 0) return null\n if (!Number.isFinite(current)) return null\n return Math.round(((current - previous) / previous) * 1_000) / 10\n}\n\n/**\n * How a cell reads to a person, by its column's kind; `null` is \"—\". Every\n * consumer that shows a table to a person words its cells here, so an insight\n * answer and a weekly summary print the same figure the same way.\n */\nexport function pluginFigureCellText(\n table: Pick<PluginFigureTable, 'columns'>,\n key: string,\n value: PluginFigureCell | undefined,\n): string {\n if (value === null || value === undefined || value === '') return '—'\n const column = table.columns.find((entry) => entry.key === key)\n if (typeof value === 'string' || !column) return String(value)\n switch (column.kind) {\n case 'money':\n return new Intl.NumberFormat('en-US', {\n style: 'currency',\n currency: column.currency ?? 'USD',\n }).format(value)\n case 'percent':\n return `${value.toLocaleString('en-US', { maximumFractionDigits: 1 })}%`\n case 'change':\n return `${value > 0 ? '+' : ''}${value.toLocaleString('en-US', { maximumFractionDigits: 1 })}%`\n case 'duration': {\n const seconds = Math.round(value)\n if (seconds < 60) return `${seconds}s`\n const minutes = Math.floor(seconds / 60)\n if (minutes < 60) return `${minutes}m ${String(seconds % 60).padStart(2, '0')}s`\n return `${Math.floor(minutes / 60)}h ${String(minutes % 60).padStart(2, '0')}m`\n }\n default:\n return value.toLocaleString('en-US', { maximumFractionDigits: 2 })\n }\n}\n\n/** The ISO 8601 week an instant falls in, in UTC (`2026-W38`). */\nexport function pluginFigureIsoWeek(at: Date): string {\n const date = new Date(Date.UTC(at.getUTCFullYear(), at.getUTCMonth(), at.getUTCDate()))\n // Thursday decides the week's year.\n const weekday = date.getUTCDay() || 7\n date.setUTCDate(date.getUTCDate() + 4 - weekday)\n const yearStart = Date.UTC(date.getUTCFullYear(), 0, 1)\n const week = Math.ceil(((date.getTime() - yearStart) / 86_400_000 + 1) / 7)\n return `${date.getUTCFullYear()}-W${String(week).padStart(2, '0')}`\n}\n\n/** The section of the weekly summary a reader is listed in, or `null` for a reader that is not in it. */\nexport function pluginFigureSummarySection(reader: PluginFigureReader): 'site' | 'workspace' | null {\n if (!reader.weeklySummary) return null\n return reader.weeklySummary.per ?? (reader.scope === 'site' ? 'site' : 'workspace')\n}\n"],"names":["getRegisteringPluginId","definePluginServiceContract","registerPluginService","resolvePluginServices","PLUGIN_FIGURE_COLUMN_KINDS","PLUGIN_FIGURES","multiple","PLUGIN_FIGURE_MAX_ROWS","PLUGIN_FIGURE_MAX_COLUMNS","PLUGIN_FIGURE_MAX_TEXT","PLUGIN_FIGURE_MAX_NOTES","READER_ID","PARAM_NAME","COLUMN_KEY","CONSOLE_PATH","CALENDAR_DAY","CURRENCY","registerPluginFigureReader","reader","options","key","id","trim","test","Error","param","params","name","pluginId","incumbent","find","entry","pluginFigureReader","one","impl","listPluginFigureReaders","map","sort","a","b","localeCompare","pluginFigureReaderPlugin","resolved","plugin","undefined","pluginFigureParams","raw","value","text","slice","required","ok","missing","EMAIL","PHONE_LIKE","looksLikePhone","match","digits","replace","length","maskPersonalDetails","hasPersonalDetails","cut","max","finite","Number","isFinite","day","isNaN","Date","parse","cellFor","column","kind","number","Math","round","normalizePluginFigureTable","table","Array","isArray","title","seen","Set","columns","has","includes","currency","add","push","label","rawRows","rows","source","Object","fromEntries","omitted","floor","period","from","to","days","path","notes","note","filter","Boolean","pluginFigureDay","at","toISOString","pluginFigureWindows","now","span","dayMs","endOfToday","UTC","getUTCFullYear","getUTCMonth","getUTCDate","currentStart","previousStart","current","startMs","endMs","previous","pluginFigureChange","pluginFigureCellText","String","Intl","NumberFormat","style","format","toLocaleString","maximumFractionDigits","seconds","minutes","padStart","pluginFigureIsoWeek","date","weekday","getUTCDay","setUTCDate","yearStart","week","ceil","getTime","pluginFigureSummarySection","weeklySummary","per","scope"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,sBAAsB,QAAQ,qCAAiC;AACxE,SACEC,2BAA2B,EAC3BC,qBAAqB,EACrBC,qBAAqB,QAChB,uBAAmB;AAuE1B,OAAO,MAAMC,6BAAgE;IAC3E;IACA;IACA;IACA;IACA;IACA;IACA;IACA;CACD,CAAA;AAiJD,OAAO,MAAMC,iBAAiBJ,4BAAgD,gBAAgB;IAC5FK,UAAU;AACZ,GAAE;AAEF,wEAAwE,GACxE,OAAO,MAAMC,yBAAyB,GAAE;AAExC,wCAAwC,GACxC,OAAO,MAAMC,4BAA4B,EAAC;AAE1C,0EAA0E,GAC1E,OAAO,MAAMC,yBAAyB,IAAG;AAEzC,sCAAsC,GACtC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