create-nisc 0.9.3 → 0.9.4
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.
- package/CHANGELOG.md +13 -0
- package/README.md +20 -22
- package/dist/{chunk-IJ5E5SA2.js → chunk-2W7KLXCO.js} +3 -3
- package/dist/{chunk-IJ5E5SA2.js.map → chunk-2W7KLXCO.js.map} +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/templates/base/files/src/ui/kit.css +8 -41
- package/templates/dom/files/src/ui/kit.ts +2 -8
- package/templates/dom/files/src/ui/registry.ts +2 -2
- package/templates/model-first/agents-section.md +31 -25
- package/templates/model-first/files/src/app/action-catalog.ts +4 -3
- package/templates/model-first/files/src/app/actions/answer/turn.action.ts +23 -0
- package/templates/model-first/files/src/app/actions/ask/dock.action.ts +10 -33
- package/templates/model-first/files/src/app/actions/ask/dock.layout.ts +7 -39
- package/templates/model-first/files/src/app/actions/guide/guide.actions.ts +16 -19
- package/templates/model-first/files/src/app/app.ts +4 -3
- package/templates/model-first/files/src/app/charter/charter.ts +1 -1
- package/templates/model-first/files/src/app/screens.ts +27 -48
- package/templates/model-first/files/src/app/shell/frame.layout.ts +5 -1
- package/templates/model-first/files/src/app/shell/shell.ts +7 -10
- package/templates/model-first/files/src/dev/all-checks.ts +3 -3
- package/templates/model-first/files/src/dev/artifacts-check.ts +14 -25
- package/templates/model-first/files/src/dev/compose-check.ts +87 -136
- package/templates/model-first/files/src/dev/guide-check.ts +41 -58
- package/templates/model-first/files/src/dev/live-ask.ts +43 -138
- package/templates/model-first/files/src/dev/scripted.ts +24 -102
- package/templates/model-first/files/src/dev/shape-check.ts +2 -6
- package/templates/model-first/files/src/dev/strata.ts +2 -2
- package/templates/model-first/files/src/server/boot.ts +2 -2
- package/templates/model-first/files/src/server/compose/catalog.ts +10 -28
- package/templates/model-first/files/src/server/compose/composer.agent.ts +13 -11
- package/templates/model-first/files/src/server/compose/index.ts +55 -132
- package/templates/model-first/files/src/server/compose/knowledge.ts +22 -43
- package/templates/model-first/files/src/server/compose/turn.schema.ts +55 -0
- package/templates/model-first/files/src/server/models/index.ts +5 -23
- package/templates/model-first/item.json +28 -38
- package/templates/react/files/src/ui/kit.tsx +14 -25
- package/templates/react/files/src/ui/registry.ts +2 -2
- package/templates/model-first/files/src/app/arrangements.ts +0 -17
- package/templates/model-first/files/src/app/shell/areas.ts +0 -3
- package/templates/model-first/files/src/server/compose/answer.ts +0 -145
- package/templates/model-first/files/src/server/compose/history.ts +0 -45
- package/templates/model-first/files/src/server/compose/layout.ts +0 -70
- package/templates/model-first/files/src/server/compose/pick.ts +0 -44
- package/templates/model-first/files/src/server/compose/plan.ts +0 -45
- package/templates/model-first/files/src/server/compose/reply.schema.ts +0 -70
- package/templates/model-first/files/src/server/compose/screen.ts +0 -40
|
@@ -2,11 +2,11 @@ import { createComponentRegistry } from '@niscorp/nova';
|
|
|
2
2
|
import type { ComponentRegistry } from '@niscorp/nova';
|
|
3
3
|
import { ActionSlot, CanvasSlot } from '@niscorp/nova/adapters/react/components';
|
|
4
4
|
import type { NovaComponent } from '@niscorp/nova/adapters/react';
|
|
5
|
-
import {
|
|
5
|
+
import { Button, Card, Check, Code, Dock, Frame, Grid, Heading, Input, Page, Points, Prompt, Row, Steps, Talk, Text } from './kit';
|
|
6
6
|
|
|
7
7
|
export const buildRegistry = (): ComponentRegistry<NovaComponent> => {
|
|
8
8
|
const registry = createComponentRegistry<NovaComponent>();
|
|
9
9
|
registry.registerAll({ CanvasSlot, ActionSlot });
|
|
10
|
-
registry.registerAll({ Page, Row, Heading, Text, Button, Frame, Dock,
|
|
10
|
+
registry.registerAll({ Page, Row, Heading, Text, Button, Frame, Dock, Talk, Grid, Card, Code, Points, Steps, Check, Input, Prompt });
|
|
11
11
|
return registry;
|
|
12
12
|
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { LayoutNode } from '@niscorp/nova';
|
|
2
|
-
import { AREAS } from './shell/areas';
|
|
3
|
-
import type { AreaId } from './shell/areas';
|
|
4
|
-
|
|
5
|
-
export type Arrangement = { id: string; description: string; areaCount: number; layout: LayoutNode };
|
|
6
|
-
|
|
7
|
-
const area = (name: AreaId, span = 1): LayoutNode => ({ component: 'Area', props: { name, span }, children: [{ component: 'CanvasSlot', props: { canvasId: name } }] });
|
|
8
|
-
const answers = (columns: number, areas: LayoutNode[]): LayoutNode => ({ component: 'Grid', props: { columns }, children: areas });
|
|
9
|
-
|
|
10
|
-
export const ARRANGEMENTS: readonly Arrangement[] = [
|
|
11
|
-
{ id: 'single', description: 'One thing, the whole width.', areaCount: 1, layout: answers(1, [area('a')]) },
|
|
12
|
-
{ id: 'pair', description: 'Two things side by side, to compare them.', areaCount: 2, layout: answers(2, [area('a'), area('b')]) },
|
|
13
|
-
{ id: 'hero', description: 'One thing that matters most, the whole width, and the rest two to a row beneath it.', areaCount: 5, layout: answers(2, [area('a', 2), area('b'), area('c'), area('d'), area('e')]) },
|
|
14
|
-
{ id: 'grid', description: 'Things of equal weight, two to a row.', areaCount: AREAS.length, layout: answers(2, AREAS.map((name) => area(name))) },
|
|
15
|
-
{ id: 'trio', description: 'Three things of equal weight in one row.', areaCount: 3, layout: answers(3, [area('a'), area('b'), area('c')]) },
|
|
16
|
-
{ id: 'strip', description: 'Everything one under the other, each the whole width, to read down.', areaCount: AREAS.length, layout: answers(1, AREAS.map((name) => area(name))) },
|
|
17
|
-
];
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import type { Arrangement } from '../../app/arrangements';
|
|
2
|
-
import { AREAS } from '../../app/shell/areas';
|
|
3
|
-
import type { AreaId } from '../../app/shell/areas';
|
|
4
|
-
import { opened } from './catalog';
|
|
5
|
-
import type { Catalog } from './catalog';
|
|
6
|
-
import type { ComposerDeps } from './knowledge';
|
|
7
|
-
import { areasOfArrangement, gridLayout } from './layout';
|
|
8
|
-
import type { WrittenLayout } from './layout';
|
|
9
|
-
import { wordsCanvas } from './plan';
|
|
10
|
-
import type { AreaCanvases } from './plan';
|
|
11
|
-
import { isRecord, recordOf } from './record';
|
|
12
|
-
import { entriesOf, EntrySchema, entryOf } from './reply.schema';
|
|
13
|
-
import type { Entry, Reply } from './reply.schema';
|
|
14
|
-
|
|
15
|
-
// No arrangement keeps the one on screen.
|
|
16
|
-
export type Answer = { title: string; screen?: string; canvases: AreaCanvases; arrangement?: string | WrittenLayout };
|
|
17
|
-
|
|
18
|
-
export type AnswerContext = ComposerDeps & { open: AreaCanvases; current: string | WrittenLayout };
|
|
19
|
-
|
|
20
|
-
const arrangementOfReply = (reply: Reply, arrangements: readonly Arrangement[]): string | WrittenLayout | undefined => {
|
|
21
|
-
if (reply.grid) return gridLayout(reply.grid);
|
|
22
|
-
return arrangements.find((arrangement) => arrangement.id === reply.arrangement)?.id;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const canvasRefusals = (entry: Entry, where: string, catalog: Catalog): string[] => {
|
|
26
|
-
const action = catalog.actions.find((candidate) => candidate.id === entry.action);
|
|
27
|
-
if (action === undefined) return [`${where}: there is no action "${String(entry.action)}".`];
|
|
28
|
-
const input = entry.input ?? {};
|
|
29
|
-
const parsed = action.check.safeParse(input);
|
|
30
|
-
if (!parsed.success) return parsed.error.issues.map((issue) => `${where}: input.${issue.path.join('.')}: ${issue.message}`);
|
|
31
|
-
const refusals: string[] = [];
|
|
32
|
-
const read = input['read'];
|
|
33
|
-
if (typeof read === 'string') {
|
|
34
|
-
const known = catalog.reads.find((candidate) => candidate.fingerprint === read);
|
|
35
|
-
const params = isRecord(input['params']) ? input['params'] : {};
|
|
36
|
-
if (known === undefined) refusals.push(`${where}: there is no read "${read}".`);
|
|
37
|
-
else {
|
|
38
|
-
for (const key of known.contextKeys) if (params[key] === undefined || params[key] === '') refusals.push(`${where}: the read "${read}" is asked with params.${key}, and it is missing.`);
|
|
39
|
-
for (const key of Object.keys(params)) if (!known.contextKeys.includes(key)) refusals.push(`${where}: the read "${read}" takes no params.${key}; it takes ${known.contextKeys.length > 0 ? known.contextKeys.join(', ') : 'none'}.`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
const write = input['write'];
|
|
43
|
-
if (typeof write === 'string') {
|
|
44
|
-
const known = catalog.writes.find((candidate) => candidate.fingerprint === write);
|
|
45
|
-
const keys = (Array.isArray(input['fields']) ? input['fields'] : []).map((field: unknown) => (isRecord(field) ? field['key'] : undefined));
|
|
46
|
-
if (known === undefined) refusals.push(`${where}: there is no write "${write}".`);
|
|
47
|
-
else for (const key of known.contextKeys) if (!keys.includes(key)) refusals.push(`${where}: the write "${write}" saves a field "${key}", and the form has none.`);
|
|
48
|
-
}
|
|
49
|
-
return refusals;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const entryRefusals = (entry: Entry, where: string, room: readonly AreaId[], named: string, open: AreaCanvases, catalog: Catalog): string[] => {
|
|
53
|
-
const refusals = room.includes(entry.area) ? [] : [`${where}: the arrangement ${named} has no area ${entry.area}; its areas are ${room.join(', ')}.`];
|
|
54
|
-
const kinds = [entry.keep !== undefined, entry.action !== undefined || entry.input !== undefined, entry.say !== undefined, entry.drop].filter(Boolean).length;
|
|
55
|
-
if (kinds !== 1) return [...refusals, `${where}: give exactly one of "keep", "action" with "input", "say", or "drop".`];
|
|
56
|
-
if (entry.keep !== undefined) return open[entry.keep] === undefined ? [...refusals, `${where}: there is nothing in area ${entry.keep} to keep.`] : refusals;
|
|
57
|
-
if (entry.action !== undefined || entry.input !== undefined) return [...refusals, ...canvasRefusals(entry, where, catalog)];
|
|
58
|
-
return refusals;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const nameOf = (arrangement: string | WrittenLayout): string => (typeof arrangement === 'object' ? 'you wrote' : `"${arrangement}"`);
|
|
62
|
-
|
|
63
|
-
// What was there stays, in the areas the arrangement has, unless an entry drops it, moves it (an area another entry
|
|
64
|
-
// keeps from is empty unless it is given something), or puts another canvas or words there.
|
|
65
|
-
const applyEntries = (entries: readonly Entry[], open: AreaCanvases, room: readonly AreaId[], catalog: Catalog): AreaCanvases => {
|
|
66
|
-
const canvases: AreaCanvases = Object.fromEntries(room.flatMap((area) => (open[area] !== undefined ? [[area, open[area]]] : [])));
|
|
67
|
-
for (const entry of entries) if (entry.keep !== undefined && entry.keep !== entry.area && !entries.some((other) => other.area === entry.keep)) delete canvases[entry.keep];
|
|
68
|
-
for (const entry of entries) {
|
|
69
|
-
delete canvases[entry.area];
|
|
70
|
-
if (entry.keep !== undefined) {
|
|
71
|
-
const kept = open[entry.keep];
|
|
72
|
-
if (kept !== undefined) canvases[entry.area] = kept;
|
|
73
|
-
} else if (entry.say !== undefined) canvases[entry.area] = wordsCanvas(entry.say);
|
|
74
|
-
else if (entry.action !== undefined) {
|
|
75
|
-
const intent = entry.intent?.trim() ?? '';
|
|
76
|
-
canvases[entry.area] = opened({ action: entry.action, input: entry.input ?? {}, intent: intent !== '' ? intent : (catalog.actions.find((action) => action.id === entry.action)?.description ?? '') }, catalog);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return canvases;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
// A read shown again with the same values is the same thing under another heading; so is a second form for one write.
|
|
83
|
-
const sameThingKey = (canvas: { action: string; input: Record<string, unknown> }): string => {
|
|
84
|
-
const { read, params, write } = canvas.input;
|
|
85
|
-
return JSON.stringify(read !== undefined ? [canvas.action, read, params] : write !== undefined ? [canvas.action, write] : [canvas.action, canvas.input]);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export const refusalsOf = (reply: Reply, context: AnswerContext): string[] => {
|
|
89
|
-
const { open, current, catalog, screens, arrangements } = context;
|
|
90
|
-
if (reply.screen) return screens.some((screen) => screen.id === reply.screen) ? [] : [`"${reply.screen}" is not one of the app's screens.`];
|
|
91
|
-
const refusals: string[] = [];
|
|
92
|
-
if (reply.arrangement && !reply.grid && !arrangements.some((arrangement) => arrangement.id === reply.arrangement)) refusals.push(`there is no arrangement "${reply.arrangement}".`);
|
|
93
|
-
if (reply.grid && new Set(reply.grid.areas.map(({ name }) => name)).size !== reply.grid.areas.length) refusals.push('the grid names an area twice.');
|
|
94
|
-
const arrangement = arrangementOfReply(reply, arrangements) ?? current;
|
|
95
|
-
const room = areasOfArrangement(arrangement, arrangements);
|
|
96
|
-
const entries = entriesOf(reply);
|
|
97
|
-
const seen = new Set<AreaId>();
|
|
98
|
-
entries.forEach((entry, at) => {
|
|
99
|
-
const where = `areas[${at}] (area ${entry.area})`;
|
|
100
|
-
if (seen.has(entry.area)) refusals.push(`${where}: area ${entry.area} is given twice.`);
|
|
101
|
-
seen.add(entry.area);
|
|
102
|
-
refusals.push(...entryRefusals(entry, where, room, nameOf(arrangement), open, catalog));
|
|
103
|
-
});
|
|
104
|
-
const shown = new Map<string, AreaId>();
|
|
105
|
-
const canvases = applyEntries(entries, open, room, catalog);
|
|
106
|
-
for (const area of AREAS) {
|
|
107
|
-
const canvas = canvases[area];
|
|
108
|
-
if (canvas === undefined) continue;
|
|
109
|
-
const there = shown.get(sameThingKey(canvas));
|
|
110
|
-
if (there !== undefined) refusals.push(`areas ${there} and ${area} would show the same thing; show it once.`);
|
|
111
|
-
else shown.set(sameThingKey(canvas), area);
|
|
112
|
-
}
|
|
113
|
-
return refusals;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
export const answerOf = (reply: Reply, context: AnswerContext): Answer => {
|
|
117
|
-
const title = reply.title.trim();
|
|
118
|
-
if (reply.screen) return { title, screen: reply.screen, canvases: {} };
|
|
119
|
-
const arrangement = arrangementOfReply(reply, context.arrangements);
|
|
120
|
-
const canvases = applyEntries(entriesOf(reply), context.open, areasOfArrangement(arrangement ?? context.current, context.arrangements), context.catalog);
|
|
121
|
-
return { title, canvases, ...(arrangement !== undefined ? { arrangement } : {}) };
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
// While the reply streams, solid writes it left to right: an entry is finished once the next one has begun, or once the
|
|
125
|
-
// reply has gone past the areas to its room (solid holds every key from the start; a key the model has reached has a
|
|
126
|
-
// value, null included). A finished entry is placed in the room on screen if its own checks pass;
|
|
127
|
-
// another room, or a screen, comes with the whole reply, which is the run's to check when it ends.
|
|
128
|
-
export const answerSoFar = (output: unknown, context: AnswerContext): Answer | undefined => {
|
|
129
|
-
const data = recordOf(recordOf(output)['data']);
|
|
130
|
-
const written = data['areas'];
|
|
131
|
-
if (!Array.isArray(written) || typeof data['screen'] === 'string') return undefined;
|
|
132
|
-
const pastTheAreas = ['arrangement', 'grid', 'screen'].some((key) => data[key] !== undefined);
|
|
133
|
-
const room = areasOfArrangement(context.current, context.arrangements);
|
|
134
|
-
const seen = new Set<AreaId>();
|
|
135
|
-
const entries = (pastTheAreas ? written : written.slice(0, -1)).flatMap((raw: unknown) => {
|
|
136
|
-
const parsed = EntrySchema.safeParse(raw);
|
|
137
|
-
if (!parsed.success) return [];
|
|
138
|
-
const entry = entryOf(parsed.data);
|
|
139
|
-
if (seen.has(entry.area) || entryRefusals(entry, '', room, nameOf(context.current), context.open, context.catalog).length > 0) return [];
|
|
140
|
-
seen.add(entry.area);
|
|
141
|
-
return [entry];
|
|
142
|
-
});
|
|
143
|
-
const title = typeof data['title'] === 'string' ? data['title'].trim() : '';
|
|
144
|
-
return { title, canvases: applyEntries(entries, context.open, room, context.catalog) };
|
|
145
|
-
};
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import type { Plan, ShownPlan } from './plan';
|
|
2
|
-
|
|
3
|
-
export type History = {
|
|
4
|
-
current: () => ShownPlan;
|
|
5
|
-
add: (plan: Plan) => ShownPlan;
|
|
6
|
-
next: (plan: Plan) => ShownPlan;
|
|
7
|
-
indexOf: (test: (plan: Plan) => boolean) => number;
|
|
8
|
-
go: (position: number) => ShownPlan;
|
|
9
|
-
back: () => ShownPlan;
|
|
10
|
-
forward: () => ShownPlan;
|
|
11
|
-
keep: (canvases: Plan['canvases']) => void;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const NOTHING: ShownPlan = { question: '', label: '', position: 0, total: 0, stack: [], canvases: {}, arrangement: '', layout: { component: 'Grid' } };
|
|
15
|
-
|
|
16
|
-
const stackOf = (plans: readonly Plan[]): ShownPlan['stack'] => plans.map((plan, offset) => ({ position: offset + 1, label: plan.label }));
|
|
17
|
-
|
|
18
|
-
export const createHistory = (first: Plan | undefined): History => {
|
|
19
|
-
const plans: Plan[] = first === undefined ? [] : [first];
|
|
20
|
-
let at = plans.length - 1;
|
|
21
|
-
const shown = (index: number): ShownPlan => {
|
|
22
|
-
const plan = plans[index];
|
|
23
|
-
return plan === undefined ? NOTHING : { ...plan, position: index + 1, total: plans.length, stack: stackOf(plans) };
|
|
24
|
-
};
|
|
25
|
-
const showAt = (index: number): ShownPlan => {
|
|
26
|
-
at = index;
|
|
27
|
-
return shown(at);
|
|
28
|
-
};
|
|
29
|
-
return {
|
|
30
|
-
current: () => shown(at),
|
|
31
|
-
add: (plan) => {
|
|
32
|
-
plans.push(plan);
|
|
33
|
-
return showAt(plans.length - 1);
|
|
34
|
-
},
|
|
35
|
-
next: (plan) => ({ ...plan, position: plans.length + 1, total: plans.length + 1, stack: stackOf([...plans, plan]) }),
|
|
36
|
-
indexOf: (test) => plans.findIndex(test),
|
|
37
|
-
go: (position) => showAt(position >= 1 && position <= plans.length ? position - 1 : at),
|
|
38
|
-
back: () => showAt(Math.max(0, at - 1)),
|
|
39
|
-
forward: () => showAt(Math.min(plans.length - 1, at + 1)),
|
|
40
|
-
keep: (canvases) => {
|
|
41
|
-
const plan = plans[at];
|
|
42
|
-
if (plan !== undefined) plans[at] = { ...plan, canvases };
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
};
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { LayoutNodeSchema } from '@niscorp/nova';
|
|
3
|
-
import type { LayoutNode } from '@niscorp/nova';
|
|
4
|
-
import type { Arrangement } from '../../app/arrangements';
|
|
5
|
-
import { AREAS } from '../../app/shell/areas';
|
|
6
|
-
import type { AreaId } from '../../app/shell/areas';
|
|
7
|
-
import { isRecord, recordOf } from './record';
|
|
8
|
-
|
|
9
|
-
export const AreaIdSchema = z.enum(AREAS);
|
|
10
|
-
|
|
11
|
-
export type WrittenLayout = { layout: LayoutNode; areas: AreaId[] };
|
|
12
|
-
|
|
13
|
-
type Grid = { columns: number; areas: readonly { name: AreaId; span?: number | null | undefined }[] };
|
|
14
|
-
|
|
15
|
-
export const areasInLayout = (layout: unknown): AreaId[] => {
|
|
16
|
-
if (Array.isArray(layout)) return layout.flatMap(areasInLayout);
|
|
17
|
-
if (!isRecord(layout)) return [];
|
|
18
|
-
const name = AreaIdSchema.safeParse(recordOf(layout['props'])['name']);
|
|
19
|
-
return [...(layout['component'] === 'Area' && name.success ? [name.data] : []), ...areasInLayout(layout['children'])];
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
// "a (the whole width); b c; d e"
|
|
23
|
-
export const areaRows = (layout: unknown): string => {
|
|
24
|
-
const node = recordOf(layout);
|
|
25
|
-
const columnsProp = recordOf(node['props'])['columns'];
|
|
26
|
-
const columns = typeof columnsProp === 'number' ? columnsProp : 1;
|
|
27
|
-
const rows: string[][] = [[]];
|
|
28
|
-
let used = 0;
|
|
29
|
-
for (const area of Array.isArray(node['children']) ? node['children'] : []) {
|
|
30
|
-
const props = recordOf(recordOf(area)['props']);
|
|
31
|
-
const span = Math.min(typeof props['span'] === 'number' ? props['span'] : 1, columns);
|
|
32
|
-
if (used + span > columns) {
|
|
33
|
-
rows.push([]);
|
|
34
|
-
used = 0;
|
|
35
|
-
}
|
|
36
|
-
rows[rows.length - 1]?.push(`${String(props['name'])}${span === columns && columns > 1 ? ' (the whole width)' : span > 1 ? ` (${span} columns)` : ''}`);
|
|
37
|
-
used += span;
|
|
38
|
-
}
|
|
39
|
-
return rows.map((row) => row.join(' ')).join('; ');
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export const areasOfArrangement = (arrangement: string | WrittenLayout, arrangements: readonly Arrangement[]): AreaId[] =>
|
|
43
|
-
typeof arrangement === 'object' ? arrangement.areas : AREAS.slice(0, arrangements.find((candidate) => candidate.id === arrangement)?.areaCount ?? 0);
|
|
44
|
-
|
|
45
|
-
export const arrangementFor = (asked: string | WrittenLayout, arrangements: readonly Arrangement[]): { arrangement: string; layout: LayoutNode } => {
|
|
46
|
-
if (typeof asked === 'object') return { arrangement: 'written', layout: asked.layout };
|
|
47
|
-
const named = arrangements.find((arrangement) => arrangement.id === asked) ?? arrangements[0];
|
|
48
|
-
return { arrangement: named?.id ?? '', layout: named?.layout ?? { component: 'Grid' } };
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export const gridLayout = (grid: Grid): WrittenLayout => ({
|
|
52
|
-
layout: {
|
|
53
|
-
component: 'Grid',
|
|
54
|
-
props: { columns: grid.columns },
|
|
55
|
-
children: grid.areas.map(({ name, span }) => ({ component: 'Area', props: { name, span: span ?? 1 }, children: [{ component: 'CanvasSlot', props: { canvasId: name } }] })),
|
|
56
|
-
},
|
|
57
|
-
areas: grid.areas.map(({ name }) => name),
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
// The areas an answer changed are marked for the kit to outline; parsed back, so what is swapped in is a layout.
|
|
61
|
-
export const markChanged = (layout: LayoutNode, changed: readonly AreaId[]): LayoutNode => {
|
|
62
|
-
const mark = (node: unknown): unknown => {
|
|
63
|
-
if (Array.isArray(node)) return node.map(mark);
|
|
64
|
-
if (!isRecord(node)) return node;
|
|
65
|
-
const props = recordOf(node['props']);
|
|
66
|
-
const marked = node['component'] === 'Area' && changed.some((area) => area === props['name']) ? { ...node, props: { ...props, changed: true } } : node;
|
|
67
|
-
return 'children' in marked ? { ...marked, children: mark(marked['children']) } : marked;
|
|
68
|
-
};
|
|
69
|
-
return LayoutNodeSchema.parse(mark(layout));
|
|
70
|
-
};
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import type { Usage } from '@niscorp/cortex';
|
|
2
|
-
import type { Screen } from '../../app/screens';
|
|
3
|
-
import type { Catalog } from './catalog';
|
|
4
|
-
import type { OpenCanvas } from './screen';
|
|
5
|
-
|
|
6
|
-
type Question = { type: 'noul'; instructions: string };
|
|
7
|
-
type Picked = { yes: boolean; probability?: number };
|
|
8
|
-
|
|
9
|
-
export type Decide = (state: Record<string, unknown>, questions: Record<string, Question>) => Promise<{ picked: Record<string, Picked>; usage: Usage }>;
|
|
10
|
-
|
|
11
|
-
const KINDS = ['actions', 'reads', 'writes', 'screens'] as const;
|
|
12
|
-
type Kind = (typeof KINDS)[number];
|
|
13
|
-
type Candidate = { name: string; says: string };
|
|
14
|
-
|
|
15
|
-
export type Picks = Record<Kind, { name: string; probability: number }[]>;
|
|
16
|
-
|
|
17
|
-
const ASK: Record<Kind, (name: string) => string> = {
|
|
18
|
-
actions: (name) => `Does the answer to the message need actions["${name}"]?`,
|
|
19
|
-
reads: (name) => `Does the message ask for what reads["${name}"] answers?`,
|
|
20
|
-
writes: (name) => `Does the message ask to do what writes["${name}"] does?`,
|
|
21
|
-
screens: (name) => `Does the message ask to see screens["${name}"] as it is?`,
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const probabilityOf = (picked: Picked | undefined): number => picked?.probability ?? (picked?.yes === true ? 1 : 0);
|
|
25
|
-
|
|
26
|
-
// The state carries what each candidate is, in words; each question names its candidate by key.
|
|
27
|
-
export const pick = async (message: string, open: readonly OpenCanvas[], catalog: Catalog, screens: readonly Screen[], decide: Decide): Promise<{ picks: Picks; usage: Usage }> => {
|
|
28
|
-
const candidates: Record<Kind, Candidate[]> = {
|
|
29
|
-
actions: catalog.actions.map((action) => ({ name: action.id, says: action.description })),
|
|
30
|
-
reads: catalog.reads.map((read) => ({ name: read.fingerprint, says: read.intent })),
|
|
31
|
-
writes: catalog.writes.map((write) => ({ name: write.fingerprint, says: write.intent })),
|
|
32
|
-
screens: screens.map((screen) => ({ name: screen.id, says: screen.description })),
|
|
33
|
-
};
|
|
34
|
-
const state = {
|
|
35
|
-
message,
|
|
36
|
-
open_screen: Object.fromEntries(open.map((canvas) => [canvas.area, canvas.intent])),
|
|
37
|
-
...Object.fromEntries(KINDS.map((kind) => [kind, Object.fromEntries(candidates[kind].map(({ name, says }) => [name, says]))])),
|
|
38
|
-
};
|
|
39
|
-
const questions = Object.fromEntries(KINDS.flatMap((kind) => candidates[kind].map(({ name }, at): [string, Question] => [`${kind}${at}`, { type: 'noul', instructions: ASK[kind](name) }])));
|
|
40
|
-
|
|
41
|
-
const { picked, usage } = await decide(state, questions);
|
|
42
|
-
const scored = (kind: Kind): Picks[Kind] => candidates[kind].map(({ name }, at) => ({ name, probability: probabilityOf(picked[`${kind}${at}`]) }));
|
|
43
|
-
return { picks: { actions: scored('actions'), reads: scored('reads'), writes: scored('writes'), screens: scored('screens') }, usage };
|
|
44
|
-
};
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import type { LayoutNode } from '@niscorp/nova';
|
|
2
|
-
import type { Arrangement } from '../../app/arrangements';
|
|
3
|
-
import type { Canvas } from '../../app/screens';
|
|
4
|
-
import { AREAS } from '../../app/shell/areas';
|
|
5
|
-
import type { AreaId } from '../../app/shell/areas';
|
|
6
|
-
import { areasInLayout, arrangementFor } from './layout';
|
|
7
|
-
import type { WrittenLayout } from './layout';
|
|
8
|
-
|
|
9
|
-
export type AreaCanvases = Partial<Record<AreaId, Canvas>>;
|
|
10
|
-
|
|
11
|
-
export type Said = { title: string; text: string };
|
|
12
|
-
|
|
13
|
-
// Each area's canvas sits in an array, as nova's `reconcile` takes it.
|
|
14
|
-
export type Plan = {
|
|
15
|
-
question: string;
|
|
16
|
-
response?: string;
|
|
17
|
-
label: string;
|
|
18
|
-
screen?: string;
|
|
19
|
-
canvases: Partial<Record<AreaId, Canvas[]>>;
|
|
20
|
-
arrangement: string;
|
|
21
|
-
layout: LayoutNode;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export type ShownPlan = Plan & { position: number; total: number; stack: { position: number; label: string }[] };
|
|
25
|
-
|
|
26
|
-
export const wordsCanvas = (said: Said): Canvas => ({ action: 'answer.text', input: { title: said.title, text: said.text }, intent: `the words "${said.title}"` });
|
|
27
|
-
|
|
28
|
-
export const canvasesInAreas = (canvases: readonly Canvas[]): AreaCanvases => Object.fromEntries(canvases.slice(0, AREAS.length).map((canvas, at) => [AREAS[at], canvas]));
|
|
29
|
-
|
|
30
|
-
export const canvasesOfPlan = (plan: Plan): AreaCanvases => Object.fromEntries(AREAS.flatMap((area) => plan.canvases[area]?.slice(0, 1).map((canvas) => [area, canvas]) ?? []));
|
|
31
|
-
|
|
32
|
-
export const changedAreas = (now: Plan, before: Plan): AreaId[] =>
|
|
33
|
-
AREAS.filter((area) => now.canvases[area] !== undefined && JSON.stringify(now.canvases[area]) !== JSON.stringify(before.canvases[area]));
|
|
34
|
-
|
|
35
|
-
export const arrangementOfPlan = (plan: Plan): string | WrittenLayout => (plan.arrangement === 'written' ? { layout: plan.layout, areas: areasInLayout(plan.layout) } : plan.arrangement);
|
|
36
|
-
|
|
37
|
-
type PlanOptions = { arrangements: readonly Arrangement[]; arrangement: string | WrittenLayout; label?: string; screen?: { id: string; label: string } };
|
|
38
|
-
|
|
39
|
-
export const planOf = (question: string, canvases: AreaCanvases, options: PlanOptions): Plan => ({
|
|
40
|
-
question,
|
|
41
|
-
label: options.screen?.label ?? options.label ?? question,
|
|
42
|
-
...(options.screen !== undefined ? { screen: options.screen.id } : {}),
|
|
43
|
-
canvases: Object.fromEntries(AREAS.flatMap((area) => (canvases[area] !== undefined ? [[area, [canvases[area]]]] : []))),
|
|
44
|
-
...arrangementFor(options.arrangement, options.arrangements),
|
|
45
|
-
});
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { AREAS } from '../../app/shell/areas';
|
|
3
|
-
import type { AreaId } from '../../app/shell/areas';
|
|
4
|
-
import { AreaIdSchema } from './layout';
|
|
5
|
-
import type { Said } from './plan';
|
|
6
|
-
|
|
7
|
-
const GridSchema = z
|
|
8
|
-
.object({
|
|
9
|
-
columns: z.number().int().min(1).max(3).describe('How many columns the areas are laid out in.'),
|
|
10
|
-
areas: z
|
|
11
|
-
.array(
|
|
12
|
-
z
|
|
13
|
-
.object({
|
|
14
|
-
name: AreaIdSchema.describe('An area the answer uses.'),
|
|
15
|
-
span: z.number().int().min(1).max(3).nullish().describe('How many of the columns it takes; one when left out.'),
|
|
16
|
-
})
|
|
17
|
-
.strict(),
|
|
18
|
-
)
|
|
19
|
-
.min(1)
|
|
20
|
-
.max(AREAS.length)
|
|
21
|
-
.describe('The areas the answer uses, in the order they are drawn, row by row.'),
|
|
22
|
-
})
|
|
23
|
-
.strict()
|
|
24
|
-
.describe('A layout of your own, only when no arrangement suits, or the message says where things go and no arrangement puts them there.');
|
|
25
|
-
|
|
26
|
-
export const EntrySchema = z
|
|
27
|
-
.object({
|
|
28
|
-
area: AreaIdSchema.describe('The area this entry is for.'),
|
|
29
|
-
keep: AreaIdSchema.nullish().describe('Moves the canvas now in that area here, unchanged. That area is then empty, unless another entry gives it something.'),
|
|
30
|
-
action: z.string().nullish().describe('Puts a canvas here: the id of one of the ACTIONS.'),
|
|
31
|
-
input: z
|
|
32
|
-
.record(z.string(), z.unknown())
|
|
33
|
-
.nullish()
|
|
34
|
-
.describe("What the canvas is opened with, as its action's input schema says. A read's params carry every value it is asked with; a form's fields carry what the message gives for them."),
|
|
35
|
-
intent: z.string().nullish().describe('Why the canvas is there, in a few words.'),
|
|
36
|
-
say: z
|
|
37
|
-
.object({ title: z.string().max(60).describe('A few words.'), text: z.string().max(240).describe('At most two short sentences.') })
|
|
38
|
-
.strict()
|
|
39
|
-
.nullish()
|
|
40
|
-
.describe('Puts words here: only when the message asks about this app itself, or for something no action shows.'),
|
|
41
|
-
drop: z.boolean().nullish().describe('Empties this area.'),
|
|
42
|
-
})
|
|
43
|
-
.strict()
|
|
44
|
-
.describe('What one area holds after the answer: exactly one of keep, action with input and intent, say, or drop.');
|
|
45
|
-
|
|
46
|
-
export const ReplySchema = z
|
|
47
|
-
.object({
|
|
48
|
-
title: z.string().max(40).describe('What the answer is, in at most four words ("The charter", "How reads work"), never the message repeated. It names the answer among the others.'),
|
|
49
|
-
areas: z.array(EntrySchema).max(AREAS.length).nullish().describe('An entry for each area that changes. An area left out stays as it is, unless the arrangement has no such area.'),
|
|
50
|
-
arrangement: z.string().nullish().describe('The id of one of the ARRANGEMENTS, only when the answer needs other room than the one on screen; every area used must be in it.'),
|
|
51
|
-
grid: GridSchema.nullish(),
|
|
52
|
-
screen: z.string().nullish().describe('The id of one of the SCREENS, only when the message asks for that screen as it is; then no areas.'),
|
|
53
|
-
})
|
|
54
|
-
.strict();
|
|
55
|
-
|
|
56
|
-
export type Reply = z.infer<typeof ReplySchema>;
|
|
57
|
-
|
|
58
|
-
export type Entry = { area: AreaId; keep?: AreaId; action?: string; input?: Record<string, unknown>; intent?: string; say?: Said; drop: boolean };
|
|
59
|
-
|
|
60
|
-
export const entryOf = ({ area, keep, action, input, intent, say, drop }: z.infer<typeof EntrySchema>): Entry => ({
|
|
61
|
-
area,
|
|
62
|
-
...(keep ? { keep } : {}),
|
|
63
|
-
...(action ? { action } : {}),
|
|
64
|
-
...(input ? { input } : {}),
|
|
65
|
-
...(intent ? { intent } : {}),
|
|
66
|
-
...(say ? { say: { title: say.title.trim(), text: say.text.trim() } } : {}),
|
|
67
|
-
drop: drop === true,
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
export const entriesOf = (reply: Reply): Entry[] => (reply.areas ?? []).map(entryOf);
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { Shell } from '@niscorp/nova';
|
|
2
|
-
import { AREAS } from '../../app/shell/areas';
|
|
3
|
-
import type { AreaId } from '../../app/shell/areas';
|
|
4
|
-
import { inputKeysOf } from './catalog';
|
|
5
|
-
import type { Plan } from './plan';
|
|
6
|
-
import { isRecord } from './record';
|
|
7
|
-
|
|
8
|
-
// The screen as it stands. A canvas's input is what it was opened with, and what the person pressed, ticked or typed
|
|
9
|
-
// into what its input carries; `shows` is the rest of its live data beyond what it was opened with — rows that loaded,
|
|
10
|
-
// a line being typed. A list there is its count and its first rows, ids left out: they are for the database, not for
|
|
11
|
-
// a person.
|
|
12
|
-
export type OpenCanvas = { area: AreaId; action: string; intent: string; input: Record<string, unknown>; shows: Record<string, unknown> };
|
|
13
|
-
|
|
14
|
-
const ROWS = 3;
|
|
15
|
-
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
16
|
-
|
|
17
|
-
const withoutIds = (row: unknown): unknown => (isRecord(row) ? Object.fromEntries(Object.entries(row).filter(([, value]) => !(typeof value === 'string' && UUID.test(value)))) : row);
|
|
18
|
-
|
|
19
|
-
const summarised = (value: unknown): unknown => (Array.isArray(value) ? { count: value.length, first: value.slice(0, ROWS).map(withoutIds) } : value);
|
|
20
|
-
|
|
21
|
-
const isEmpty = (value: unknown): boolean => value === null || value === undefined || value === '' || value === false;
|
|
22
|
-
|
|
23
|
-
export const readOpenScreen = (shell: Shell, plan: Plan): OpenCanvas[] =>
|
|
24
|
-
AREAS.flatMap((area) => {
|
|
25
|
-
const placed = plan.canvases[area]?.[0];
|
|
26
|
-
if (placed === undefined) return [];
|
|
27
|
-
const active = shell.getState().canvases[area]?.active;
|
|
28
|
-
const runtime = active === undefined ? undefined : shell.getRuntime(active.id);
|
|
29
|
-
// While an answer streams in, an area can hold a card the plan does not list yet; the plan's card is read as placed.
|
|
30
|
-
if (runtime === undefined || runtime.definition.id !== placed.action) return [{ area, ...placed, shows: {} }];
|
|
31
|
-
const data = runtime.getData();
|
|
32
|
-
const opened = { ...runtime.definition.data, ...placed.input };
|
|
33
|
-
const carried = inputKeysOf(runtime.definition.input);
|
|
34
|
-
const moved = Object.keys(data).filter((key) => JSON.stringify(data[key]) !== JSON.stringify(opened[key]));
|
|
35
|
-
const input = { ...placed.input, ...Object.fromEntries(moved.filter((key) => carried.includes(key)).map((key) => [key, data[key]])) };
|
|
36
|
-
const shows = Object.fromEntries(moved.filter((key) => !carried.includes(key) && !isEmpty(data[key])).map((key) => [key, summarised(data[key])]));
|
|
37
|
-
return [{ area, action: placed.action, intent: placed.intent, input, shows }];
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
export const canvasesStanding = (open: readonly OpenCanvas[]): Plan['canvases'] => Object.fromEntries(open.map(({ area, action, intent, input }) => [area, [{ action, input, intent }]]));
|