@keemakr/ui-sdk 1.4.0-dev-20261014072722 → 1.4.0-dev-20261014091336

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,174 @@
1
+ export declare const CHAT_BLOCK_FENCE = "kee";
2
+ export declare const CHAT_BLOCK_LIMITS: {
3
+ /** Blocks in one fence. */
4
+ readonly blocks: 8;
5
+ /** Characters of JSON in one fence. */
6
+ readonly json: 20000;
7
+ /** Rows in one table. */
8
+ readonly rows: 25;
9
+ };
10
+ /** The blocks a reply may hold, with the example the guide teaches for each. */
11
+ export declare const CHAT_BLOCK_EXAMPLES: {
12
+ readonly stat: {
13
+ readonly type: "stat";
14
+ readonly label: "Open pipeline";
15
+ readonly value: "$1.2M";
16
+ readonly hint: "31 deals";
17
+ };
18
+ readonly chart: {
19
+ readonly type: "chart";
20
+ readonly chartType: "bar";
21
+ readonly title: "Deals by stage";
22
+ readonly showValues: true;
23
+ readonly categories: readonly ["New", "Qualified", "Won"];
24
+ readonly series: readonly [{
25
+ readonly name: "Deals";
26
+ readonly values: readonly [14, 11, 6];
27
+ }];
28
+ };
29
+ readonly table: {
30
+ readonly type: "table";
31
+ readonly columns: readonly [{
32
+ readonly key: "deal";
33
+ readonly label: "Deal";
34
+ }, {
35
+ readonly key: "stage";
36
+ readonly label: "Stage";
37
+ }];
38
+ readonly rows: readonly [{
39
+ readonly key: "d1";
40
+ readonly cells: {
41
+ readonly deal: {
42
+ readonly kind: "text";
43
+ readonly value: "Acme — 50 seats";
44
+ };
45
+ readonly stage: {
46
+ readonly kind: "badge";
47
+ readonly value: "Proposal";
48
+ readonly tone: "warning";
49
+ };
50
+ };
51
+ }];
52
+ readonly empty: "No deals";
53
+ };
54
+ readonly timeline: {
55
+ readonly type: "timeline";
56
+ readonly items: readonly [{
57
+ readonly label: "Contract sent";
58
+ readonly status: "done";
59
+ readonly at: "2026-10-12T09:00:00Z";
60
+ }, {
61
+ readonly label: "Acme demo";
62
+ readonly status: "pending";
63
+ readonly detail: "Tuesday, 10:00";
64
+ }];
65
+ };
66
+ readonly tags: {
67
+ readonly type: "tags";
68
+ readonly label: "Sources";
69
+ readonly items: readonly [{
70
+ readonly label: "Referral";
71
+ readonly tone: "success";
72
+ }];
73
+ };
74
+ readonly progress_bar: {
75
+ readonly type: "progress_bar";
76
+ readonly label: "Quota";
77
+ readonly value: 62;
78
+ readonly valueLabel: "62 of 100";
79
+ };
80
+ readonly details: {
81
+ readonly type: "details";
82
+ readonly items: readonly [{
83
+ readonly label: "Owner";
84
+ readonly value: {
85
+ readonly kind: "text";
86
+ readonly value: "Ben";
87
+ };
88
+ }, {
89
+ readonly label: "Close by";
90
+ readonly value: {
91
+ readonly kind: "text";
92
+ readonly value: "Dec 20, 2026";
93
+ };
94
+ }, {
95
+ readonly label: "Last contact";
96
+ readonly value: {
97
+ readonly kind: "date";
98
+ readonly value: "2026-10-13T15:30:00Z";
99
+ };
100
+ }];
101
+ };
102
+ readonly alert: {
103
+ readonly type: "alert";
104
+ readonly tone: "warning";
105
+ readonly text: "Two deals close this week with no next step.";
106
+ };
107
+ readonly link: {
108
+ readonly type: "link";
109
+ readonly label: "Pricing page";
110
+ readonly href: "https://example.com/pricing";
111
+ };
112
+ readonly heading: {
113
+ readonly type: "heading";
114
+ readonly text: "This week";
115
+ };
116
+ readonly text: {
117
+ readonly type: "text";
118
+ readonly value: "Numbers are from the last 30 days.";
119
+ readonly muted: true;
120
+ };
121
+ readonly divider: {
122
+ readonly type: "divider";
123
+ };
124
+ readonly section: {
125
+ readonly type: "section";
126
+ readonly layout: "row";
127
+ readonly children: readonly [{
128
+ readonly type: "stat";
129
+ readonly label: "Won";
130
+ readonly value: "6";
131
+ }, {
132
+ readonly type: "stat";
133
+ readonly label: "Lost";
134
+ readonly value: "4";
135
+ }];
136
+ };
137
+ };
138
+ export type ChatBlockType = keyof typeof CHAT_BLOCK_EXAMPLES;
139
+ export declare const CHAT_BLOCK_TYPES: ChatBlockType[];
140
+ export type ChatBlocksCheck = {
141
+ ok: true;
142
+ blocks: Record<string, unknown>[];
143
+ } | {
144
+ ok: false;
145
+ issue: string;
146
+ };
147
+ /** Check blocks Kee wrote: allowed types, nothing pressable, within limits, and the real schema. */
148
+ export declare function checkChatBlocks(input: unknown): ChatBlocksCheck;
149
+ /** Parse one fence body. */
150
+ export declare function parseChatBlocks(body: string): ChatBlocksCheck;
151
+ /** One `kee` fence in a reply: where it starts and ends, and its body. Unclosed: `end` is -1. */
152
+ export interface ChatBlockFence {
153
+ start: number;
154
+ end: number;
155
+ body: string;
156
+ }
157
+ /** Every `kee` fence in a reply, in order. The last may be unclosed while the reply streams. */
158
+ export declare function findChatBlockFences(text: string): ChatBlockFence[];
159
+ /**
160
+ * A finished reply with every `kee` fence checked. A fence that passes is
161
+ * rewritten with the checked blocks (defaults filled in); one that does not,
162
+ * or is never closed, becomes `note`. `kept` and `dropped` count them.
163
+ */
164
+ export declare function settleChatBlocks(text: string, note?: string): {
165
+ text: string;
166
+ kept: number;
167
+ dropped: string[];
168
+ };
169
+ /**
170
+ * What Kee reads about chat blocks: when to draw, the rules, and one example
171
+ * per block. Built from CHAT_BLOCK_EXAMPLES, which a test parses with the
172
+ * real schema.
173
+ */
174
+ export declare function chatBlocksGuide(): string;
@@ -0,0 +1,264 @@
1
+ /**
2
+ * Chat blocks — UI SDK blocks Kee draws inside its own reply.
3
+ *
4
+ * Kee writes them in the answer it is already writing, as a fenced block
5
+ * whose info string is `kee` and whose body is a JSON array of blocks:
6
+ *
7
+ * ```kee
8
+ * [{ "type": "stat", "label": "Open deals", "value": "31" }]
9
+ * ```
10
+ *
11
+ * No extra model call. The reply streams as usual; an app draws the block in
12
+ * place once its JSON is whole, with the same renderer a module screen uses.
13
+ * Core checks every block when the reply finishes and replaces one that does
14
+ * not pass with a short note, so a stored reply never holds a broken block.
15
+ *
16
+ * Read-only. A chat block shows; it never acts. No press, no form, no select:
17
+ * anything with an `onPress`, `onChange`, `onSort`, `onFilter`, `onPage` or an
18
+ * `action` is refused. Things a person acts on stay in views and approvals.
19
+ *
20
+ * The guide Kee reads (`chatBlocksGuide`) is built from the examples below,
21
+ * and a test parses every example with the real schema, so the prompt cannot
22
+ * teach a block the renderer would refuse.
23
+ */
24
+ import { screenTreeSchema } from './schema.js';
25
+ export const CHAT_BLOCK_FENCE = 'kee';
26
+ export const CHAT_BLOCK_LIMITS = {
27
+ /** Blocks in one fence. */
28
+ blocks: 8,
29
+ /** Characters of JSON in one fence. */
30
+ json: 20_000,
31
+ /** Rows in one table. */
32
+ rows: 25,
33
+ };
34
+ /** The blocks a reply may hold, with the example the guide teaches for each. */
35
+ export const CHAT_BLOCK_EXAMPLES = {
36
+ stat: { type: 'stat', label: 'Open pipeline', value: '$1.2M', hint: '31 deals' },
37
+ chart: {
38
+ type: 'chart',
39
+ chartType: 'bar',
40
+ title: 'Deals by stage',
41
+ showValues: true,
42
+ categories: ['New', 'Qualified', 'Won'],
43
+ series: [{ name: 'Deals', values: [14, 11, 6] }],
44
+ },
45
+ table: {
46
+ type: 'table',
47
+ columns: [
48
+ { key: 'deal', label: 'Deal' },
49
+ { key: 'stage', label: 'Stage' },
50
+ ],
51
+ rows: [
52
+ {
53
+ key: 'd1',
54
+ cells: {
55
+ deal: { kind: 'text', value: 'Acme — 50 seats' },
56
+ stage: { kind: 'badge', value: 'Proposal', tone: 'warning' },
57
+ },
58
+ },
59
+ ],
60
+ empty: 'No deals',
61
+ },
62
+ timeline: {
63
+ type: 'timeline',
64
+ items: [
65
+ { label: 'Contract sent', status: 'done', at: '2026-10-12T09:00:00Z' },
66
+ { label: 'Acme demo', status: 'pending', detail: 'Tuesday, 10:00' },
67
+ ],
68
+ },
69
+ tags: { type: 'tags', label: 'Sources', items: [{ label: 'Referral', tone: 'success' }] },
70
+ progress_bar: { type: 'progress_bar', label: 'Quota', value: 62, valueLabel: '62 of 100' },
71
+ details: {
72
+ type: 'details',
73
+ items: [
74
+ { label: 'Owner', value: { kind: 'text', value: 'Ben' } },
75
+ { label: 'Close by', value: { kind: 'text', value: 'Dec 20, 2026' } },
76
+ { label: 'Last contact', value: { kind: 'date', value: '2026-10-13T15:30:00Z' } },
77
+ ],
78
+ },
79
+ alert: { type: 'alert', tone: 'warning', text: 'Two deals close this week with no next step.' },
80
+ link: { type: 'link', label: 'Pricing page', href: 'https://example.com/pricing' },
81
+ heading: { type: 'heading', text: 'This week' },
82
+ text: { type: 'text', value: 'Numbers are from the last 30 days.', muted: true },
83
+ divider: { type: 'divider' },
84
+ section: {
85
+ type: 'section',
86
+ layout: 'row',
87
+ children: [
88
+ { type: 'stat', label: 'Won', value: '6' },
89
+ { type: 'stat', label: 'Lost', value: '4' },
90
+ ],
91
+ },
92
+ };
93
+ export const CHAT_BLOCK_TYPES = Object.keys(CHAT_BLOCK_EXAMPLES);
94
+ const ALLOWED = new Set(CHAT_BLOCK_TYPES);
95
+ const PRESSES = new Set(['onPress', 'onChange', 'onSort', 'onFilter', 'onPage', 'action']);
96
+ function walk(value, path, out) {
97
+ if (out.length > 0)
98
+ return;
99
+ if (Array.isArray(value)) {
100
+ value.forEach((item, index) => walk(item, `${path}[${index}]`, out));
101
+ return;
102
+ }
103
+ if (!value || typeof value !== 'object')
104
+ return;
105
+ const node = value;
106
+ if (typeof node.type === 'string' && 'type' in node && !('kind' in node)) {
107
+ const isBlock = path === '' || /children\[\d+\]$|^\[\d+\]$/.test(path);
108
+ if (isBlock && !ALLOWED.has(node.type)) {
109
+ out.push(`a chat block cannot be a ${node.type}`);
110
+ return;
111
+ }
112
+ }
113
+ for (const [key, child] of Object.entries(node)) {
114
+ if (PRESSES.has(key)) {
115
+ out.push(`a chat block is read-only: "${key}" at ${path || 'the top'}`);
116
+ return;
117
+ }
118
+ walk(child, path ? `${path}.${key}` : key, out);
119
+ }
120
+ }
121
+ /** Check blocks Kee wrote: allowed types, nothing pressable, within limits, and the real schema. */
122
+ export function checkChatBlocks(input) {
123
+ if (!Array.isArray(input))
124
+ return { ok: false, issue: 'a chat block fence holds a JSON array' };
125
+ if (input.length === 0)
126
+ return { ok: false, issue: 'a chat block fence is empty' };
127
+ if (input.length > CHAT_BLOCK_LIMITS.blocks) {
128
+ return { ok: false, issue: `a reply holds at most ${CHAT_BLOCK_LIMITS.blocks} blocks at once` };
129
+ }
130
+ const issues = [];
131
+ input.forEach((block, index) => walk(block, `[${index}]`, issues));
132
+ if (issues[0])
133
+ return { ok: false, issue: issues[0] };
134
+ for (const block of input) {
135
+ if (block.type === 'table' &&
136
+ Array.isArray(block.rows) &&
137
+ block.rows.length > CHAT_BLOCK_LIMITS.rows) {
138
+ return {
139
+ ok: false,
140
+ issue: `a table in a reply shows at most ${CHAT_BLOCK_LIMITS.rows} rows`,
141
+ };
142
+ }
143
+ }
144
+ const parsed = screenTreeSchema.safeParse({ version: 1, title: 'reply', children: input });
145
+ if (!parsed.success) {
146
+ const issue = parsed.error.issues[0];
147
+ return {
148
+ ok: false,
149
+ issue: issue ? `${issue.path.join('.')}: ${issue.message}` : 'invalid block',
150
+ };
151
+ }
152
+ return { ok: true, blocks: parsed.data.children };
153
+ }
154
+ /** Parse one fence body. */
155
+ export function parseChatBlocks(body) {
156
+ if (body.length > CHAT_BLOCK_LIMITS.json) {
157
+ return {
158
+ ok: false,
159
+ issue: `a chat block fence takes at most ${CHAT_BLOCK_LIMITS.json} characters`,
160
+ };
161
+ }
162
+ let json;
163
+ try {
164
+ json = JSON.parse(body);
165
+ }
166
+ catch {
167
+ return { ok: false, issue: 'a chat block fence is not valid JSON' };
168
+ }
169
+ return checkChatBlocks(json);
170
+ }
171
+ const OPEN = /^[ \t]*(`{3,})[ \t]*kee[ \t]*$/gm;
172
+ /** Every `kee` fence in a reply, in order. The last may be unclosed while the reply streams. */
173
+ export function findChatBlockFences(text) {
174
+ const out = [];
175
+ OPEN.lastIndex = 0;
176
+ let match;
177
+ while ((match = OPEN.exec(text))) {
178
+ const ticks = match[1];
179
+ const bodyStart = match.index + match[0].length + 1;
180
+ const close = new RegExp(`^[ \\t]*${ticks}[ \\t]*$`, 'm');
181
+ const rest = text.slice(bodyStart);
182
+ const closing = close.exec(rest);
183
+ if (!closing) {
184
+ out.push({ start: match.index, end: -1, body: rest });
185
+ break;
186
+ }
187
+ const end = bodyStart + closing.index + closing[0].length;
188
+ out.push({ start: match.index, end, body: rest.slice(0, closing.index) });
189
+ OPEN.lastIndex = end;
190
+ }
191
+ return out;
192
+ }
193
+ /**
194
+ * A finished reply with every `kee` fence checked. A fence that passes is
195
+ * rewritten with the checked blocks (defaults filled in); one that does not,
196
+ * or is never closed, becomes `note`. `kept` and `dropped` count them.
197
+ */
198
+ export function settleChatBlocks(text, note = '_(A chart or table could not be shown here.)_') {
199
+ const fences = findChatBlockFences(text);
200
+ if (fences.length === 0)
201
+ return { text, kept: 0, dropped: [] };
202
+ let out = '';
203
+ let at = 0;
204
+ let kept = 0;
205
+ const dropped = [];
206
+ for (const fence of fences) {
207
+ out += text.slice(at, fence.start);
208
+ const checked = fence.end === -1
209
+ ? { ok: false, issue: 'the fence is never closed' }
210
+ : parseChatBlocks(fence.body);
211
+ if (checked.ok) {
212
+ out += `\`\`\`${CHAT_BLOCK_FENCE}\n${JSON.stringify(checked.blocks)}\n\`\`\``;
213
+ kept += 1;
214
+ }
215
+ else {
216
+ out += note;
217
+ dropped.push(checked.issue);
218
+ }
219
+ at = fence.end === -1 ? text.length : fence.end;
220
+ }
221
+ out += text.slice(at);
222
+ return { text: out, kept, dropped };
223
+ }
224
+ /**
225
+ * What Kee reads about chat blocks: when to draw, the rules, and one example
226
+ * per block. Built from CHAT_BLOCK_EXAMPLES, which a test parses with the
227
+ * real schema.
228
+ */
229
+ export function chatBlocksGuide() {
230
+ const examples = CHAT_BLOCK_TYPES.map((type) => `- ${type}: ${JSON.stringify(CHAT_BLOCK_EXAMPLES[type])}`).join('\n');
231
+ return [
232
+ 'You can draw inside your reply. Write a fenced block with the info string `kee` and a JSON array of blocks, on its own lines:',
233
+ '',
234
+ '```kee',
235
+ '[{"type":"stat","label":"Open deals","value":"31"}]',
236
+ '```',
237
+ '',
238
+ 'Use blocks to make an answer easy to take in at a glance. A person should get the point from the shape before they read a word. Reach for a block whenever the answer is structured, not only for charts:',
239
+ '- A status or a summary ("how are we doing", "where does X stand"): a row section of 2 to 4 stats, then one line on what matters.',
240
+ '- A comparison or a breakdown ("by stage", "this month vs last"): a bar chart, values shown.',
241
+ '- Several records ("my open deals", "who replied"): a table of the few columns that matter, not a bulleted list.',
242
+ '- One record ("tell me about Acme"): details, with tags for its labels.',
243
+ '- Steps, a plan or a history ("what happens next", "what did we do"): a timeline.',
244
+ '- Progress toward a goal: a progress bar.',
245
+ '- Something that needs attention (a risk, a blocker, a missing piece): an alert, at the top.',
246
+ 'Put every fact in the block: the day, the owner, the amount, the status. Around it, write at most one short line before and two sentences after. The sentences after give the takeaway (what stands out, what to do next), never the numbers again.',
247
+ ' Bad, after a chart by stage: "15 New ($191.6K), 11 Qualified ($551.3K), 11 Proposal ($817K)..."',
248
+ ' Good: "Proposal holds the most value. Two of those close this month with no next step."',
249
+ 'A plain conversational reply, a question back, or one fact needs no block: write a sentence.',
250
+ '',
251
+ 'Rules:',
252
+ `- At most ${CHAT_BLOCK_LIMITS.blocks} blocks in one fence, and at most ${CHAT_BLOCK_LIMITS.rows} rows in a table. Valid JSON only: double quotes, no comments, no trailing commas.`,
253
+ '- Read-only: no buttons, no forms, no presses. To let the person act, or to show data they will come back to, make a view instead.',
254
+ '- Only numbers you have from a tool result in this conversation. Never invent or estimate a number to fill a chart.',
255
+ '- One chart, one unit: never put a count and an amount in the same chart. Put totals and counts in a row of stats, and chart the one measure that answers the question.',
256
+ '- In a table, a status or stage column is a badge cell with a tone; amounts and dates read as a person writes them ($42,000, Nov 14).',
257
+ "- A date cell shows a date AND a time, in the reader's zone: use it only for a moment (2026-10-14T09:00:00Z). A day with no time (a close date, a due date) is a text cell written as a person writes it (Nov 14, 2026). A chart has one value per category in each series.",
258
+ '- For one record, the words after its details are only the next step or what needs attention, never its fields again.',
259
+ '- Never in a spoken turn.',
260
+ '',
261
+ 'The blocks, one example each:',
262
+ examples,
263
+ ].join('\n');
264
+ }
package/dist/index.d.ts CHANGED
@@ -14,6 +14,8 @@ export { DATASET_FIELD_ROLES, DATASET_FIELD_TYPES, DATASET_LIMITS, answerDataset
14
14
  export type { DatasetCell, DatasetCreateCheck, DatasetCreateHandler, DatasetCreateRequest, DatasetCreateResponse, DatasetCreateResponseInput, DatasetDeclaration, DatasetDeclarationInput, DatasetDefinition, DatasetField, DatasetFieldRole, DatasetFieldType, DatasetQueryHandler, DatasetQueryRequest, DatasetQueryRequestInput, DatasetQueryResponse, DatasetQueryResponseInput, DatasetRow, } from './dataset.js';
15
15
  export { DATASET_CREATE_ACTION, DATASET_LAYOUTS, DATASET_MAX_PAGE, DATASET_NEW_PARAM, DATASET_RECORD_SECTION, compileDatasetCreate, compileDatasetNode, compileDatasetRecord, compileDatasetRecordPanel, datasetCell, datasetNodeIssues, datasetNodeSchema, datasetPage, datasetQueryFor, datasetRecordKey, datasetRecordQuery, } from './dataset-view.js';
16
16
  export type { DatasetLayout, DatasetNode, DatasetNodeInput, DatasetScreenContext, } from './dataset-view.js';
17
+ export { CHAT_BLOCK_EXAMPLES, CHAT_BLOCK_FENCE, CHAT_BLOCK_LIMITS, CHAT_BLOCK_TYPES, chatBlocksGuide, checkChatBlocks, findChatBlockFences, parseChatBlocks, settleChatBlocks, } from './chat-blocks.js';
18
+ export type { ChatBlockFence, ChatBlockType, ChatBlocksCheck } from './chat-blocks.js';
17
19
  export { defineScreen, defineSurface } from './define.js';
18
20
  export type { ScreenDefinition, ScreenParams, LoadHandler, ActionHandler, JobHandler, } from './define.js';
19
21
  export { loadWebsiteChat, websiteChatSnippet } from './website-chat.js';
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ export { composerPress, composerShortcut } from './composer.js';
15
15
  export { addDays, calendarDays, dayStartMs, entriesOnDay, placeEntries, shiftCalendarDate, slotIsFree, slotTimes, todayIn, } from './calendar.js';
16
16
  export { DATASET_FIELD_ROLES, DATASET_FIELD_TYPES, DATASET_LIMITS, answerDatasetCreate, answerDatasetQuery, datasetCreateInput, datasetCreateRequestSchema, datasetCreateResponseSchema, datasetDeclarationSchema, datasetFieldSchema, datasetManifestSchema, datasetQueryRequestSchema, datasetQueryResponseSchema, datasetRequestIssues, datasetResponseIssues, datasetRowSchema, defineDataset, } from './dataset.js';
17
17
  export { DATASET_CREATE_ACTION, DATASET_LAYOUTS, DATASET_MAX_PAGE, DATASET_NEW_PARAM, DATASET_RECORD_SECTION, compileDatasetCreate, compileDatasetNode, compileDatasetRecord, compileDatasetRecordPanel, datasetCell, datasetNodeIssues, datasetNodeSchema, datasetPage, datasetQueryFor, datasetRecordKey, datasetRecordQuery, } from './dataset-view.js';
18
+ export { CHAT_BLOCK_EXAMPLES, CHAT_BLOCK_FENCE, CHAT_BLOCK_LIMITS, CHAT_BLOCK_TYPES, chatBlocksGuide, checkChatBlocks, findChatBlockFences, parseChatBlocks, settleChatBlocks, } from './chat-blocks.js';
18
19
  export { defineScreen, defineSurface } from './define.js';
19
20
  export { loadWebsiteChat, websiteChatSnippet } from './website-chat.js';
20
21
  export { createWidgetClient } from './widget.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keemakr/ui-sdk",
3
- "version": "1.4.0-dev-20261014072722",
3
+ "version": "1.4.0-dev-20261014091336",
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",