@keemakr/ui-sdk 1.6.0 → 1.7.0-dev-20261105172501

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.
@@ -137,9 +137,14 @@ export declare const CHAT_BLOCK_EXAMPLES: {
137
137
  };
138
138
  export type ChatBlockType = keyof typeof CHAT_BLOCK_EXAMPLES;
139
139
  export declare const CHAT_BLOCK_TYPES: ChatBlockType[];
140
+ /**
141
+ * `repaired` lists what was mended so the blocks still draw: a tone folded to one the schema
142
+ * takes, a table cell that became plain text. Empty when Kee's blocks passed as written.
143
+ */
140
144
  export type ChatBlocksCheck = {
141
145
  ok: true;
142
146
  blocks: Record<string, unknown>[];
147
+ repaired: string[];
143
148
  } | {
144
149
  ok: false;
145
150
  issue: string;
@@ -161,12 +166,15 @@ export declare function findChatBlockFences(text: string): ChatBlockFence[];
161
166
  /**
162
167
  * A finished reply with every `kee` fence checked. A fence that passes is
163
168
  * rewritten with the checked blocks (defaults filled in); one that does not,
164
- * or is never closed, becomes `note`. `kept` and `dropped` count them.
169
+ * or is never closed, becomes `note`. `kept` and `dropped` count them;
170
+ * `repaired` lists what was mended in a kept fence (a folded tone, a cell
171
+ * drawn as text), for the caller to log.
165
172
  */
166
173
  export declare function settleChatBlocks(text: string, note?: string): {
167
174
  text: string;
168
175
  kept: number;
169
176
  dropped: string[];
177
+ repaired: string[];
170
178
  };
171
179
  /**
172
180
  * What Kee reads about chat blocks: when to draw, the rules, and one example
@@ -12,6 +12,9 @@
12
12
  * place once its JSON is whole, with the same renderer a module screen uses.
13
13
  * Core checks every block when the reply finishes and replaces one that does
14
14
  * not pass with a short note, so a stored reply never holds a broken block.
15
+ * One bad value does not cost the block: a tone the schema does not take is
16
+ * folded to the nearest one ("danger" is destructive), and a table cell that
17
+ * still fails is drawn as plain text, so the rest of the table shows.
15
18
  *
16
19
  * Read-only. A chat block shows; it never acts. No press, no form, no select:
17
20
  * anything with an `onPress`, `onChange`, `onSort`, `onFilter`, `onPage` or an
@@ -93,6 +96,83 @@ export const CHAT_BLOCK_EXAMPLES = {
93
96
  export const CHAT_BLOCK_TYPES = Object.keys(CHAT_BLOCK_EXAMPLES);
94
97
  const ALLOWED = new Set(CHAT_BLOCK_TYPES);
95
98
  const PRESSES = new Set(['onPress', 'onChange', 'onSort', 'onFilter', 'onPage', 'action']);
99
+ /**
100
+ * Tone words Kee writes for a tone the schema holds under its role name (DESIGN.md: name by role).
101
+ * `danger` and `error` are the red `destructive`; `neutral` is the plain `default` badge.
102
+ */
103
+ const TONE_ALIASES = {
104
+ danger: 'destructive',
105
+ error: 'destructive',
106
+ critical: 'destructive',
107
+ negative: 'destructive',
108
+ neutral: 'default',
109
+ muted: 'default',
110
+ secondary: 'default',
111
+ positive: 'success',
112
+ caution: 'warning',
113
+ };
114
+ /** Parses a reply's blocks may take while one bad tone or cell is mended at a time. */
115
+ const REPAIR_PASSES = 4;
116
+ const isJson = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
117
+ function at(root, path) {
118
+ let here = root;
119
+ for (const part of path) {
120
+ if (!here || typeof here !== 'object')
121
+ return undefined;
122
+ here = here[part];
123
+ }
124
+ return here;
125
+ }
126
+ /** The table cell a path points into (`…rows[r].cells.<column>`), or null. */
127
+ function cellPath(path) {
128
+ for (let i = path.length - 2; i >= 2; i--) {
129
+ if (path[i] === 'cells' && typeof path[i - 1] === 'number' && path[i - 2] === 'rows') {
130
+ return path.slice(0, i + 2);
131
+ }
132
+ }
133
+ return null;
134
+ }
135
+ /**
136
+ * Mend one schema issue in place, so one bad value never costs the whole block. A tone the
137
+ * schema does not take is folded to its alias, else `default`, else the first tone it lists. A
138
+ * table cell that still fails becomes a text cell with the words it held. Anything else is left
139
+ * for the caller to refuse. True when something changed.
140
+ */
141
+ function repairIssue(tree, issue, mended, repaired) {
142
+ const path = issue.path;
143
+ const where = path.map(String).join('.');
144
+ const holder = at(tree, path.slice(0, -1));
145
+ const field = path[path.length - 1];
146
+ if ((field === 'tone' || field === 'badgeTone') &&
147
+ issue.code === 'invalid_value' &&
148
+ isJson(holder)) {
149
+ const values = issue.values ?? [];
150
+ const was = holder[field];
151
+ const alias = typeof was === 'string' ? TONE_ALIASES[was.toLowerCase()] : undefined;
152
+ const next = alias && values.includes(alias) ? alias : values.includes('default') ? 'default' : values[0];
153
+ if (typeof next !== 'string' || was === next)
154
+ return false;
155
+ holder[field] = next;
156
+ repaired.push(`${where}: ${field} ${JSON.stringify(was)} drawn as "${next}"`);
157
+ return true;
158
+ }
159
+ const cell = cellPath(path);
160
+ if (!cell)
161
+ return false;
162
+ const key = cell.map(String).join('.');
163
+ if (mended.has(key))
164
+ return false;
165
+ const row = at(tree, cell.slice(0, -1));
166
+ if (!isJson(row))
167
+ return false;
168
+ const old = row[cell[cell.length - 1]];
169
+ const value = isJson(old) ? old.value : old;
170
+ const text = typeof value === 'string' || typeof value === 'number' ? String(value).slice(0, 500) : '';
171
+ row[cell[cell.length - 1]] = { kind: 'text', value: text };
172
+ mended.add(key);
173
+ repaired.push(`${key}: ${issue.code}, drawn as plain text`);
174
+ return true;
175
+ }
96
176
  function walk(value, path, out) {
97
177
  if (out.length > 0)
98
178
  return;
@@ -161,19 +241,28 @@ export function checkChatBlocks(input) {
161
241
  };
162
242
  }
163
243
  }
164
- const parsed = screenTreeSchema.safeParse({
165
- version: 1,
166
- title: 'reply',
167
- children: input.map(withTableEmpty),
168
- });
169
- if (!parsed.success) {
170
- const issue = parsed.error.issues[0];
171
- return {
172
- ok: false,
173
- issue: issue ? `${issue.path.join('.')}: ${issue.message}` : 'invalid block',
174
- };
244
+ // A copy: mending a tone or a cell must never change the blocks the caller holds.
245
+ const children = structuredClone(input).map(withTableEmpty);
246
+ const tree = { version: 1, title: 'reply', children };
247
+ const repaired = [];
248
+ const mended = new Set();
249
+ for (let pass = 0;; pass++) {
250
+ const parsed = screenTreeSchema.safeParse(tree);
251
+ if (parsed.success) {
252
+ return { ok: true, blocks: parsed.data.children, repaired };
253
+ }
254
+ const issues = parsed.error.issues;
255
+ // Every issue is mended before the next parse, so a pass that mends nothing ends the loop.
256
+ const mendedAny = pass < REPAIR_PASSES &&
257
+ issues.map((issue) => repairIssue(tree, issue, mended, repaired)).some(Boolean);
258
+ if (!mendedAny) {
259
+ const issue = issues[0];
260
+ return {
261
+ ok: false,
262
+ issue: issue ? `${issue.path.join('.')}: ${issue.message}` : 'invalid block',
263
+ };
264
+ }
175
265
  }
176
- return { ok: true, blocks: parsed.data.children };
177
266
  }
178
267
  /** Parse one fence body. */
179
268
  export function parseChatBlocks(body) {
@@ -217,16 +306,19 @@ export function findChatBlockFences(text) {
217
306
  /**
218
307
  * A finished reply with every `kee` fence checked. A fence that passes is
219
308
  * rewritten with the checked blocks (defaults filled in); one that does not,
220
- * or is never closed, becomes `note`. `kept` and `dropped` count them.
309
+ * or is never closed, becomes `note`. `kept` and `dropped` count them;
310
+ * `repaired` lists what was mended in a kept fence (a folded tone, a cell
311
+ * drawn as text), for the caller to log.
221
312
  */
222
313
  export function settleChatBlocks(text, note = '_(A chart or table could not be shown here.)_') {
223
314
  const fences = findChatBlockFences(text);
224
315
  if (fences.length === 0)
225
- return { text, kept: 0, dropped: [] };
316
+ return { text, kept: 0, dropped: [], repaired: [] };
226
317
  let out = '';
227
318
  let at = 0;
228
319
  let kept = 0;
229
320
  const dropped = [];
321
+ const repaired = [];
230
322
  for (const fence of fences) {
231
323
  out += text.slice(at, fence.start);
232
324
  const checked = fence.end === -1
@@ -235,6 +327,7 @@ export function settleChatBlocks(text, note = '_(A chart or table could not be s
235
327
  if (checked.ok) {
236
328
  out += `\`\`\`${CHAT_BLOCK_FENCE}\n${JSON.stringify(checked.blocks)}\n\`\`\``;
237
329
  kept += 1;
330
+ repaired.push(...checked.repaired);
238
331
  }
239
332
  else {
240
333
  out += note;
@@ -243,7 +336,7 @@ export function settleChatBlocks(text, note = '_(A chart or table could not be s
243
336
  at = fence.end === -1 ? text.length : fence.end;
244
337
  }
245
338
  out += text.slice(at);
246
- return { text: out, kept, dropped };
339
+ return { text: out, kept, dropped, repaired };
247
340
  }
248
341
  /**
249
342
  * What Kee reads about chat blocks: when to draw, the rules, and one example
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keemakr/ui-sdk",
3
- "version": "1.6.0",
3
+ "version": "1.7.0-dev-20261105172501",
4
4
  "description": "Product screens for keemakr marketplace modules: a versioned, server-driven UI tree schema plus defineScreen() handlers. Module code never ships to the browser — keemakr-core validates the tree and renders it with platform components.",
5
5
  "license": "MIT",
6
6
  "type": "module",