@booki-aui/readiness 0.0.0-stage → 0.2.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.
@@ -0,0 +1,229 @@
1
+ import { Page } from 'playwright-core';
2
+
3
+ /**
4
+ * Code that runs inside the page. Each function is serialised by Playwright,
5
+ * so it must not use anything from module scope.
6
+ */
7
+ /** What the report says about one element. */
8
+ type ElementInfo = {
9
+ tag: string;
10
+ selector: string;
11
+ html: string;
12
+ /** Visible text near the element: a column header, a preceding label. */
13
+ context: string;
14
+ type: string | null;
15
+ /** True when the element or an ancestor carries data-agent-confirm. */
16
+ confirm: string | null;
17
+ /** True when the element or an ancestor carries data-agent-secret. */
18
+ secret: boolean;
19
+ /** The element or a descendant uses an icon font, by family or class. */
20
+ iconFont: boolean;
21
+ disabled: boolean;
22
+ /**
23
+ * Out of reach of a person and an agent: aria-hidden or inert on it or an
24
+ * ancestor, type="hidden", not rendered, or visually hidden and out of the
25
+ * tab order (the native input a UI library puts beside a custom control).
26
+ */
27
+ hidden: boolean;
28
+ };
29
+
30
+ /**
31
+ * Every check the readiness report runs, with the Agent Contract section it
32
+ * enforces (spec/agent-contract.md), why it matters to an agent, the fix, and
33
+ * how much it can take off the score.
34
+ */
35
+ type RuleId = "bridge-missing" | "bridge-unnamed-control" | "bridge-person-action" | "field-unnamed" | "field-broken-name" | "field-ambiguous" | "field-not-on-bridge" | "focusable-without-role" | "button-unnamed" | "button-icon-name" | "chart-pixels-only" | "irreversible-ungated" | "secret-visible" | "snapshot-size";
36
+ type Severity = "critical" | "serious" | "moderate" | "minor";
37
+ type Rule = {
38
+ id: RuleId;
39
+ title: string;
40
+ /** Section of the Agent Contract, e.g. "§3.1". */
41
+ contract: string;
42
+ severity: Severity;
43
+ why: string;
44
+ fix: string;
45
+ /** The Booki control that does this right, when there is one. */
46
+ booki?: string;
47
+ /** Points taken off the score for each finding. */
48
+ points: number;
49
+ /** Most points this rule can take off, however many findings it has. */
50
+ cap: number;
51
+ };
52
+ declare const RULES: Record<RuleId, Rule>;
53
+ /** Snapshot size thresholds, in tokens (characters ÷ 4). */
54
+ declare const SNAPSHOT_TOKENS: {
55
+ warn: number;
56
+ high: number;
57
+ };
58
+
59
+ type Finding = {
60
+ rule: RuleId;
61
+ /** What was found, in one line. */
62
+ message: string;
63
+ element?: {
64
+ role?: string;
65
+ name?: string;
66
+ ref?: string | null;
67
+ selector?: string;
68
+ html?: string;
69
+ context?: string;
70
+ };
71
+ /** The Booki control for this element, when one fits better than the rule's general one. */
72
+ booki?: string;
73
+ /** For a finding that covers several elements. */
74
+ count?: number;
75
+ };
76
+ type BridgeReport = {
77
+ source: "window.agent" | "webmcp" | null;
78
+ components: number;
79
+ named: number;
80
+ actions: number;
81
+ /** Action names per component label, for the report. */
82
+ controls: Array<{
83
+ label: string;
84
+ component: string;
85
+ actions: string[];
86
+ confirm?: string;
87
+ }>;
88
+ webmcpTools: string[];
89
+ error: string | null;
90
+ };
91
+ type Report = {
92
+ tool: {
93
+ name: "@booki-aui/readiness";
94
+ version: string;
95
+ };
96
+ url: string;
97
+ title: string;
98
+ checkedAt: string;
99
+ score: number;
100
+ grade: "ready" | "partly ready" | "not ready";
101
+ /** Points lost per rule. */
102
+ deductions: Array<{
103
+ rule: RuleId;
104
+ findings: number;
105
+ points: number;
106
+ }>;
107
+ findings: Finding[];
108
+ metrics: {
109
+ snapshotChars: number;
110
+ snapshotTokens: number;
111
+ fields: number;
112
+ buttons: number;
113
+ charts: number;
114
+ };
115
+ bridge: BridgeReport;
116
+ snapshot: string;
117
+ /**
118
+ * The address asked for, when the page sent the browser somewhere else (a
119
+ * sign-in page, say): `url` is then the page that was measured.
120
+ */
121
+ redirectedFrom?: string;
122
+ };
123
+ type AuditOptions = {
124
+ /** Playwright storage state (cookies, localStorage) of a signed-in session. */
125
+ storageState?: string;
126
+ /** A browser profile directory to run in (signed in already). */
127
+ profile?: string;
128
+ /** "chrome" (default), "msedge", "chromium" or any Playwright channel. */
129
+ channel?: string;
130
+ /** A browser binary, instead of a channel. */
131
+ executablePath?: string;
132
+ /** Milliseconds to wait after the page has loaded, for late rendering. Default 500. */
133
+ wait?: number;
134
+ /** A selector to wait for before checking. */
135
+ waitFor?: string;
136
+ headed?: boolean;
137
+ viewport?: {
138
+ width: number;
139
+ height: number;
140
+ };
141
+ /**
142
+ * Milliseconds to wait for the page to load (navigation, the load event)
143
+ * and for `waitFor`, before giving up. Default 30000; 0 waits without a limit.
144
+ */
145
+ timeout?: number;
146
+ };
147
+ declare const VERSION = "0.1.0";
148
+ declare function looksLikeIconName(name: string, iconFont: boolean): boolean;
149
+ /** Buttons that post, pay, send, approve or otherwise cannot be taken back. */
150
+ declare const IRREVERSIBLE: RegExp;
151
+ /** A Booki control that fits the field, guessed from its role, type and the text near it. */
152
+ declare function bookiControlFor(role: string, info: ElementInfo | null): string | undefined;
153
+ /** Opens the page and checks what stops an agent there. */
154
+ declare function audit(input: string, options?: AuditOptions): Promise<Report>;
155
+ /** Checks an open page. Exposed for callers that drive their own browser. */
156
+ declare function check(page: Page, url?: string): Promise<Report>;
157
+
158
+ /** Action names that approve, release or finalize: a person's, never offered to an agent. */
159
+ declare const PERSON_ACTION: RegExp;
160
+
161
+ type Scored = {
162
+ rule: RuleId;
163
+ count?: number;
164
+ };
165
+ /**
166
+ * 100 minus each rule's points per finding, capped per rule, never below 0.
167
+ * The caps keep one kind of problem from hiding the others: a page with
168
+ * forty unnamed fields and nothing else wrong still scores 70.
169
+ */
170
+ declare function score(findings: Scored[]): {
171
+ score: number;
172
+ grade: "ready" | "partly ready" | "not ready";
173
+ deductions: Array<{
174
+ rule: RuleId;
175
+ findings: number;
176
+ points: number;
177
+ }>;
178
+ };
179
+
180
+ /**
181
+ * Finding secrets in the text an agent reads: bank account numbers (Norwegian
182
+ * kontonummer, mod 11), IBANs (mod 97), payment references (KID, mod 10 or
183
+ * 11, next to the word KID), US routing numbers (ABA checksum, next to the
184
+ * word routing) and card numbers (Luhn, next to the word card or kort).
185
+ * A number counts only when its check digits are valid, so amounts, dates and
186
+ * invoice numbers are not reported.
187
+ */
188
+ type SecretKind = "bank-account" | "iban" | "kid" | "routing-number" | "card";
189
+ type SecretHit = {
190
+ kind: SecretKind;
191
+ value: string;
192
+ masked: string;
193
+ index: number;
194
+ };
195
+ declare function isKontonummer(value: string): boolean;
196
+ declare function isIban(value: string): boolean;
197
+ declare function isLuhn(value: string): boolean;
198
+ declare function isKid(value: string): boolean;
199
+ declare function isRoutingNumber(value: string): boolean;
200
+ declare function findSecrets(text: string): SecretHit[];
201
+
202
+ /**
203
+ * Reading Playwright's AI-mode accessibility snapshot: the same text a
204
+ * browser agent (Playwright MCP, browser tools) gets for the page.
205
+ */
206
+ type SnapshotNode = {
207
+ role: string;
208
+ /** Accessible name; "" when the element has none. */
209
+ name: string;
210
+ ref: string | null;
211
+ /** Index of the line in the snapshot. */
212
+ line: number;
213
+ /** Attributes in brackets, e.g. ["checked", "disabled", "level=2"]. */
214
+ flags: string[];
215
+ /** Text after the colon on the same line (a textbox's value, a cell's text). */
216
+ value: string;
217
+ };
218
+ declare function parseSnapshot(snapshot: string): SnapshotNode[];
219
+ /** The cost indicator: characters ÷ 4, the usual rough token count. */
220
+ declare function estimateTokens(text: string): number;
221
+
222
+ /** The report as JSON, without the raw snapshot unless asked. */
223
+ declare function toJson(report: Report, { snapshot }?: {
224
+ snapshot?: boolean | undefined;
225
+ }): string;
226
+ declare function toConsole(report: Report): string;
227
+ declare function toHtml(report: Report): string;
228
+
229
+ export { type AuditOptions, type BridgeReport, type Finding, IRREVERSIBLE, PERSON_ACTION, RULES, type Report, type Rule, type RuleId, SNAPSHOT_TOKENS, type Severity, VERSION, audit, bookiControlFor, check, estimateTokens, findSecrets, isIban, isKid, isKontonummer, isLuhn, isRoutingNumber, looksLikeIconName, parseSnapshot, score, toConsole, toHtml, toJson };
package/dist/index.js ADDED
@@ -0,0 +1,48 @@
1
+ import {
2
+ IRREVERSIBLE,
3
+ RULES,
4
+ SNAPSHOT_TOKENS,
5
+ VERSION,
6
+ audit,
7
+ bookiControlFor,
8
+ check,
9
+ estimateTokens,
10
+ looksLikeIconName,
11
+ parseSnapshot,
12
+ score,
13
+ toConsole,
14
+ toHtml,
15
+ toJson
16
+ } from "./chunk-SSFV5SRX.js";
17
+ import {
18
+ PERSON_ACTION,
19
+ findSecrets,
20
+ isIban,
21
+ isKid,
22
+ isKontonummer,
23
+ isLuhn,
24
+ isRoutingNumber
25
+ } from "./chunk-5SCOA3ZP.js";
26
+ export {
27
+ IRREVERSIBLE,
28
+ PERSON_ACTION,
29
+ RULES,
30
+ SNAPSHOT_TOKENS,
31
+ VERSION,
32
+ audit,
33
+ bookiControlFor,
34
+ check,
35
+ estimateTokens,
36
+ findSecrets,
37
+ isIban,
38
+ isKid,
39
+ isKontonummer,
40
+ isLuhn,
41
+ isRoutingNumber,
42
+ looksLikeIconName,
43
+ parseSnapshot,
44
+ score,
45
+ toConsole,
46
+ toHtml,
47
+ toJson
48
+ };
@@ -0,0 +1,33 @@
1
+ import { d as BridgeComponent, a as ActStep, c as ActResult, B as BridgeEvent, S as ServerActor, b as ServerAnswer } from './types-Ba_gnEks.js';
2
+
3
+ /**
4
+ * A small in-memory implementation of the Agent Contract. It shows how little an
5
+ * implementer needs: four controls, a bridge, an audit log and a server check.
6
+ * It imports nothing but the contract's own types and permission list, and the
7
+ * conformance suite is run against it.
8
+ */
9
+ type Locale = "en" | "nb";
10
+ type ReferenceOptions = {
11
+ agentId?: string;
12
+ onBehalfOf?: string;
13
+ locale?: Locale;
14
+ };
15
+ type SyncBridge = {
16
+ snapshot(): BridgeComponent[];
17
+ act(input: {
18
+ steps: ActStep[];
19
+ }): ActResult;
20
+ subscribe(listener: (event: BridgeEvent) => void): () => void;
21
+ };
22
+ type Reference = {
23
+ bridge: SyncBridge;
24
+ person: {
25
+ click(ref: string, action: string, args?: unknown[]): ActResult;
26
+ id: string;
27
+ };
28
+ audit: BridgeEvent[];
29
+ server(actor: ServerActor | null, permission: string): ServerAnswer;
30
+ };
31
+ declare function createReference(options?: ReferenceOptions): Reference;
32
+
33
+ export { type Locale, type Reference, type ReferenceOptions, type SyncBridge, createReference };
@@ -0,0 +1,175 @@
1
+ import {
2
+ PERSON_ONLY_PERMISSIONS
3
+ } from "./chunk-IZ57MB25.js";
4
+
5
+ // src/reference/index.ts
6
+ var PERSON_ID = "person:anna";
7
+ var ACCOUNTS = ["1920", "6800"];
8
+ var MAX_MINOR = 99999999999;
9
+ var REDACTED = "[redacted]";
10
+ var MESSAGES = {
11
+ en: {
12
+ noSteps: "Send a list of steps.",
13
+ unknownRef: (ref, refs) => `No control named "${ref}". Controls: ${refs.join(", ")}.`,
14
+ unknownAction: (ref, action, actions) => `"${ref}" has no action "${action}". Actions: ${actions.join(", ")}.`,
15
+ minorUnits: "Amount must be a whole number of minor units (ore), for example 155000 for 1550.00, not 12.5.",
16
+ unknownAccount: (code) => `Unknown account "${code}". Valid accounts: ${ACCOUNTS.join(", ")}.`,
17
+ bankText: "Bank account must be a text value.",
18
+ pending: "Approval is still pending.",
19
+ notSignedIn: "Sign in first.",
20
+ personOnly: (permission) => `An agent cannot do "${permission}". A person must do it.`,
21
+ role: (permission) => `Your role does not allow "${permission}".`
22
+ },
23
+ nb: {
24
+ noSteps: "Send en liste med steg.",
25
+ unknownRef: (ref, refs) => `Finner ingen kontroll som heter "${ref}". Kontroller: ${refs.join(", ")}.`,
26
+ unknownAction: (ref, action, actions) => `"${ref}" har ingen handling "${action}". Handlinger: ${actions.join(", ")}.`,
27
+ minorUnits: "Bel\xF8pet m\xE5 v\xE6re et helt antall minorenheter (\xF8re), for eksempel 155000 for 1550,00, ikke 12,5.",
28
+ unknownAccount: (code) => `Ukjent konto "${code}". Gyldige kontoer: ${ACCOUNTS.join(", ")}.`,
29
+ bankText: "Bankkontoen m\xE5 v\xE6re en tekstverdi.",
30
+ pending: "Godkjenningen venter fortsatt.",
31
+ notSignedIn: "Logg inn f\xF8rst.",
32
+ personOnly: (permission) => `En agent kan ikke gj\xF8re "${permission}". En person m\xE5 gj\xF8re det.`,
33
+ role: (permission) => `Rollen din gir ikke tilgang til "${permission}".`
34
+ }
35
+ };
36
+ var CONTROLS = [
37
+ { ref: "Amount", component: "amount-field", actions: ["clear", "setMinor"] },
38
+ { ref: "Account", component: "account-picker", actions: ["clear", "setAccount"] },
39
+ { ref: "Bank account", component: "bank-account", actions: ["clear", "setAccount"] },
40
+ { ref: "Approval", component: "approval-chain", actions: ["assertApproved"] }
41
+ ];
42
+ var isMinor = (value) => typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= MAX_MINOR;
43
+ var isAccount = (value) => typeof value === "string" && ACCOUNTS.includes(value);
44
+ function createReference(options = {}) {
45
+ const agentId = options.agentId ?? "reference-agent";
46
+ const locale = options.locale ?? "en";
47
+ const msg = MESSAGES[locale];
48
+ const state = { minor: null, account: null, bankAccount: null, status: "pending" };
49
+ const audit = [];
50
+ const listeners = /* @__PURE__ */ new Set();
51
+ function apply(ref, action, args, by) {
52
+ const def = CONTROLS.find((c) => c.ref === ref);
53
+ if (!def)
54
+ return msg.unknownRef(
55
+ ref,
56
+ CONTROLS.map((c) => c.ref)
57
+ );
58
+ const personApprove = by === "person" && ref === "Approval" && action === "approve";
59
+ if (!personApprove && !def.actions.includes(action)) {
60
+ return msg.unknownAction(ref, action, def.actions);
61
+ }
62
+ const [value] = args;
63
+ switch (ref) {
64
+ case "Amount":
65
+ if (action === "clear") state.minor = null;
66
+ else if (!isMinor(value)) return msg.minorUnits;
67
+ else state.minor = value;
68
+ return null;
69
+ case "Account":
70
+ if (action === "clear") state.account = null;
71
+ else if (!isAccount(value)) return msg.unknownAccount(String(value));
72
+ else state.account = value;
73
+ return null;
74
+ case "Bank account":
75
+ if (action === "clear") state.bankAccount = null;
76
+ else if (typeof value !== "string" || value.trim() === "") return msg.bankText;
77
+ else state.bankAccount = value;
78
+ return null;
79
+ case "Approval":
80
+ if (personApprove) {
81
+ state.status = "approved";
82
+ return null;
83
+ }
84
+ return state.status === "pending" ? msg.pending : null;
85
+ }
86
+ return null;
87
+ }
88
+ function perform(ref, action, args, by) {
89
+ const refusal = apply(ref, action, args, by);
90
+ if (refusal !== null) return { ok: false, error: { message: refusal } };
91
+ const event = by === "agent" ? {
92
+ source: "agent",
93
+ ref,
94
+ action,
95
+ actor: `agent:${agentId}`,
96
+ ...options.onBehalfOf !== void 0 ? { onBehalfOf: options.onBehalfOf } : {}
97
+ } : { source: "person", ref, action, actor: PERSON_ID };
98
+ audit.push(event);
99
+ for (const listener of listeners) listener(event);
100
+ return { ok: true };
101
+ }
102
+ function snapshot() {
103
+ return [
104
+ {
105
+ ref: "Amount",
106
+ component: "amount-field",
107
+ label: "Amount",
108
+ actions: [...CONTROLS[0].actions],
109
+ minor: state.minor,
110
+ currency: "NOK",
111
+ unit: "minor"
112
+ },
113
+ {
114
+ ref: "Account",
115
+ component: "account-picker",
116
+ label: "Account",
117
+ actions: [...CONTROLS[1].actions],
118
+ account: state.account,
119
+ options: [...ACCOUNTS]
120
+ },
121
+ {
122
+ ref: "Bank account",
123
+ component: "bank-account",
124
+ label: "Bank account",
125
+ actions: [...CONTROLS[2].actions],
126
+ account: REDACTED,
127
+ isSet: state.bankAccount !== null
128
+ },
129
+ {
130
+ ref: "Approval",
131
+ component: "approval-chain",
132
+ label: "Approval",
133
+ actions: [...CONTROLS[3].actions],
134
+ status: state.status
135
+ }
136
+ ];
137
+ }
138
+ const bridge = {
139
+ snapshot,
140
+ act(input) {
141
+ const steps = input?.steps;
142
+ if (!Array.isArray(steps)) return { ok: false, error: { message: msg.noSteps } };
143
+ for (const step of steps) {
144
+ const args = Array.isArray(step?.args) ? step?.args : [];
145
+ const result = perform(String(step?.ref ?? ""), String(step?.action ?? ""), args, "agent");
146
+ if (!result.ok) return result;
147
+ }
148
+ return { ok: true };
149
+ },
150
+ subscribe(listener) {
151
+ listeners.add(listener);
152
+ return () => {
153
+ listeners.delete(listener);
154
+ };
155
+ }
156
+ };
157
+ const person = {
158
+ id: PERSON_ID,
159
+ click: (ref, action, args = []) => perform(ref, action, args, "person")
160
+ };
161
+ function server(actor, permission) {
162
+ if (actor === null) return { status: 401, code: "not-signed-in", message: msg.notSignedIn };
163
+ if (actor.kind === "agent" && PERSON_ONLY_PERMISSIONS.includes(permission)) {
164
+ return { status: 403, code: "person-only", message: msg.personOnly(permission) };
165
+ }
166
+ if (actor.kind === "person" && !actor.roles.includes(permission)) {
167
+ return { status: 403, code: "role", message: msg.role(permission) };
168
+ }
169
+ return { status: 200 };
170
+ }
171
+ return { bridge, person, audit, server };
172
+ }
173
+ export {
174
+ createReference
175
+ };
@@ -0,0 +1,49 @@
1
+ /**
2
+ * The bridge shape the conformance suite drives. It matches what ui-sdk exposes
3
+ * (`snapshot`, `act`, `subscribe`), so an implementer can run the suite against
4
+ * their own bridge without an adapter. Types only: nothing is imported at runtime.
5
+ */
6
+ type BridgeComponent = {
7
+ ref: string;
8
+ component: string;
9
+ label: string;
10
+ actions: string[];
11
+ [state: string]: unknown;
12
+ };
13
+ type ActStep = {
14
+ ref: string;
15
+ action: string;
16
+ args?: unknown[];
17
+ };
18
+ type ActResult = {
19
+ ok: boolean;
20
+ error?: {
21
+ message: string;
22
+ };
23
+ };
24
+ type BridgeEvent = {
25
+ source: "agent" | "person";
26
+ ref: string;
27
+ action: string;
28
+ actor: string;
29
+ onBehalfOf?: string;
30
+ };
31
+ type AgentBridge = {
32
+ snapshot(): BridgeComponent[] | Promise<BridgeComponent[]>;
33
+ act(input: {
34
+ steps: ActStep[];
35
+ }): ActResult | Promise<ActResult>;
36
+ subscribe(listener: (event: BridgeEvent) => void): () => void;
37
+ };
38
+ type ServerActor = {
39
+ kind: "agent" | "person";
40
+ id: string;
41
+ roles: string[];
42
+ };
43
+ type ServerAnswer = {
44
+ status: number;
45
+ code?: string;
46
+ message?: string;
47
+ };
48
+
49
+ export type { AgentBridge as A, BridgeEvent as B, ServerActor as S, ActStep as a, ServerAnswer as b, ActResult as c, BridgeComponent as d };
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
2
  "name": "@booki-aui/readiness",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "description": "Agent readiness check for accounting screens: what stops an AI agent on a page, scored 0-100 against the Agent Contract, with fixes.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/Walgermo/booki.git",
10
+ "directory": "packages/readiness"
11
+ },
12
+ "keywords": [
13
+ "accessibility",
14
+ "agents",
15
+ "ai",
16
+ "accounting",
17
+ "audit",
18
+ "playwright",
19
+ "readiness"
20
+ ],
21
+ "bin": {
22
+ "booki-readiness": "./dist/cli.js",
23
+ "readiness": "./dist/cli.js"
24
+ },
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.js"
29
+ },
30
+ "./reference": {
31
+ "types": "./dist/reference.d.ts",
32
+ "default": "./dist/reference.js"
33
+ },
34
+ "./conformance": {
35
+ "types": "./dist/conformance.d.ts",
36
+ "default": "./dist/conformance.js"
37
+ }
38
+ },
39
+ "files": [
40
+ "dist",
41
+ "CHANGELOG.md"
42
+ ],
43
+ "dependencies": {
44
+ "playwright-core": "^1.56.0"
45
+ },
46
+ "devDependencies": {
47
+ "@types/node": "^22.10.0",
48
+ "tsup": "^8.5.0",
49
+ "tsx": "^4.20.6",
50
+ "typescript": "^5.9.3",
51
+ "vitest": "^3.2.7"
52
+ },
53
+ "engines": {
54
+ "node": ">=20"
55
+ },
56
+ "publishConfig": {
57
+ "access": "public"
58
+ },
59
+ "scripts": {
60
+ "build": "tsup",
61
+ "typecheck": "tsc --noEmit",
62
+ "test": "vitest run",
63
+ "check": "tsx src/cli.ts"
64
+ }
6
65
  }