@genesislcap/ai-assistant 15.69.0 → 15.71.0-FUI-2650.1
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/dist/ai-assistant.api.json +97 -0
- package/dist/ai-assistant.d.ts +65 -0
- package/dist/chat-driver.cjs +18031 -0
- package/dist/chat-driver.cjs.map +4 -4
- package/dist/chat-driver.mjs +18024 -0
- package/dist/chat-driver.mjs.map +4 -4
- package/dist/custom-elements.json +5524 -2684
- package/dist/dts/chat-driver-node.d.ts +2 -0
- package/dist/dts/chat-driver-node.d.ts.map +1 -1
- package/dist/dts/code-review/code-review.types.d.ts +76 -0
- package/dist/dts/code-review/code-review.types.d.ts.map +1 -0
- package/dist/dts/code-review/code-reviewer.agent.d.ts +77 -0
- package/dist/dts/code-review/code-reviewer.agent.d.ts.map +1 -0
- package/dist/dts/code-review/code-reviewer.prompt.d.ts +23 -0
- package/dist/dts/code-review/code-reviewer.prompt.d.ts.map +1 -0
- package/dist/dts/code-review/gate.d.ts +44 -0
- package/dist/dts/code-review/gate.d.ts.map +1 -0
- package/dist/dts/code-review/index.d.ts +27 -0
- package/dist/dts/code-review/index.d.ts.map +1 -0
- package/dist/dts/code-review/review-changes.d.ts +70 -0
- package/dist/dts/code-review/review-changes.d.ts.map +1 -0
- package/dist/dts/code-review/rules/context-trigger-map.d.ts +6 -0
- package/dist/dts/code-review/rules/context-trigger-map.d.ts.map +1 -0
- package/dist/dts/code-review/rules/context.registry.d.ts +123 -0
- package/dist/dts/code-review/rules/context.registry.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-ai-extensions.d.ts +13 -0
- package/dist/dts/code-review/rules/fui-ai-extensions.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-blotter.d.ts +16 -0
- package/dist/dts/code-review/rules/fui-blotter.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-chart.d.ts +20 -0
- package/dist/dts/code-review/rules/fui-chart.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-column-defs.d.ts +21 -0
- package/dist/dts/code-review/rules/fui-column-defs.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-components.d.ts +9 -0
- package/dist/dts/code-review/rules/fui-components.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-criteria.d.ts +32 -0
- package/dist/dts/code-review/rules/fui-criteria.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-custom-renderer.d.ts +15 -0
- package/dist/dts/code-review/rules/fui-custom-renderer.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-dashboard.d.ts +19 -0
- package/dist/dts/code-review/rules/fui-dashboard.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-dialogs.d.ts +19 -0
- package/dist/dts/code-review/rules/fui-dialogs.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-entity-management.d.ts +19 -0
- package/dist/dts/code-review/rules/fui-entity-management.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-error-boundary.d.ts +9 -0
- package/dist/dts/code-review/rules/fui-error-boundary.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-eventing.d.ts +8 -0
- package/dist/dts/code-review/rules/fui-eventing.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-flexlayout.d.ts +17 -0
- package/dist/dts/code-review/rules/fui-flexlayout.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-flyout-panel.d.ts +10 -0
- package/dist/dts/code-review/rules/fui-flyout-panel.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-form-component.d.ts +9 -0
- package/dist/dts/code-review/rules/fui-form-component.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-formatters.d.ts +12 -0
- package/dist/dts/code-review/rules/fui-formatters.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-foundation-comms.d.ts +10 -0
- package/dist/dts/code-review/rules/fui-foundation-comms.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-global-filters.d.ts +33 -0
- package/dist/dts/code-review/rules/fui-global-filters.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-import-surface.d.ts +37 -0
- package/dist/dts/code-review/rules/fui-import-surface.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-inline-editing.d.ts +10 -0
- package/dist/dts/code-review/rules/fui-inline-editing.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-layout.d.ts +7 -0
- package/dist/dts/code-review/rules/fui-layout.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-notifications.d.ts +12 -0
- package/dist/dts/code-review/rules/fui-notifications.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-primitives.d.ts +26 -0
- package/dist/dts/code-review/rules/fui-primitives.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-rapid-grid-pro.d.ts +18 -0
- package/dist/dts/code-review/rules/fui-rapid-grid-pro.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-redux-store.d.ts +8 -0
- package/dist/dts/code-review/rules/fui-redux-store.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-reporting.d.ts +19 -0
- package/dist/dts/code-review/rules/fui-reporting.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-styling.d.ts +15 -0
- package/dist/dts/code-review/rules/fui-styling.d.ts.map +1 -0
- package/dist/dts/code-review/rules/fui-trade-form.d.ts +13 -0
- package/dist/dts/code-review/rules/fui-trade-form.d.ts.map +1 -0
- package/dist/dts/code-review/submit-code-review.tool.d.ts +12 -0
- package/dist/dts/code-review/submit-code-review.tool.d.ts.map +1 -0
- package/dist/dts/code-review/unified-diff.d.ts +7 -0
- package/dist/dts/code-review/unified-diff.d.ts.map +1 -0
- package/dist/dts/code-review/verdict.d.ts +23 -0
- package/dist/dts/code-review/verdict.d.ts.map +1 -0
- package/dist/dts/components/code-review-result/code-review-result.d.ts +26 -0
- package/dist/dts/components/code-review-result/code-review-result.d.ts.map +1 -0
- package/dist/dts/index.d.ts +1 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/react.d.ts +20 -10
- package/dist/esm/chat-driver-node.js +6 -0
- package/dist/esm/code-review/code-review.types.js +6 -0
- package/dist/esm/code-review/code-reviewer.agent.js +167 -0
- package/dist/esm/code-review/code-reviewer.prompt.js +103 -0
- package/dist/esm/code-review/gate.js +93 -0
- package/dist/esm/code-review/index.js +28 -0
- package/dist/esm/code-review/review-changes.js +123 -0
- package/dist/esm/code-review/rules/context-trigger-map.js +20 -0
- package/dist/esm/code-review/rules/context.registry.js +772 -0
- package/dist/esm/code-review/rules/fui-ai-extensions.js +306 -0
- package/dist/esm/code-review/rules/fui-blotter.js +596 -0
- package/dist/esm/code-review/rules/fui-chart.js +669 -0
- package/dist/esm/code-review/rules/fui-column-defs.js +862 -0
- package/dist/esm/code-review/rules/fui-components.js +245 -0
- package/dist/esm/code-review/rules/fui-criteria.js +538 -0
- package/dist/esm/code-review/rules/fui-custom-renderer.js +612 -0
- package/dist/esm/code-review/rules/fui-dashboard.js +604 -0
- package/dist/esm/code-review/rules/fui-dialogs.js +588 -0
- package/dist/esm/code-review/rules/fui-entity-management.js +1014 -0
- package/dist/esm/code-review/rules/fui-error-boundary.js +244 -0
- package/dist/esm/code-review/rules/fui-eventing.js +628 -0
- package/dist/esm/code-review/rules/fui-flexlayout.js +860 -0
- package/dist/esm/code-review/rules/fui-flyout-panel.js +432 -0
- package/dist/esm/code-review/rules/fui-form-component.js +898 -0
- package/dist/esm/code-review/rules/fui-formatters.js +415 -0
- package/dist/esm/code-review/rules/fui-foundation-comms.js +518 -0
- package/dist/esm/code-review/rules/fui-global-filters.js +778 -0
- package/dist/esm/code-review/rules/fui-import-surface.js +452 -0
- package/dist/esm/code-review/rules/fui-inline-editing.js +445 -0
- package/dist/esm/code-review/rules/fui-layout.js +431 -0
- package/dist/esm/code-review/rules/fui-notifications.js +439 -0
- package/dist/esm/code-review/rules/fui-primitives.js +1216 -0
- package/dist/esm/code-review/rules/fui-rapid-grid-pro.js +650 -0
- package/dist/esm/code-review/rules/fui-redux-store.js +522 -0
- package/dist/esm/code-review/rules/fui-reporting.js +506 -0
- package/dist/esm/code-review/rules/fui-styling.js +1229 -0
- package/dist/esm/code-review/rules/fui-trade-form.js +618 -0
- package/dist/esm/code-review/submit-code-review.tool.js +43 -0
- package/dist/esm/code-review/unified-diff.js +96 -0
- package/dist/esm/code-review/verdict.js +24 -0
- package/dist/esm/components/code-review-result/code-review-result.js +89 -0
- package/dist/esm/index.js +3 -0
- package/dist/react.cjs +25 -18
- package/dist/react.mjs +23 -17
- package/docs/code-review.md +137 -0
- package/package.json +24 -17
- package/scripts/smoke-chat-driver-node.mjs +15 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context for the generated app's own AI chat additions under ai/extensions/: tools, the widgets
|
|
3
|
+
* they open with requestInteraction, and writes. The widget contract, the write traps and the
|
|
4
|
+
* shape checks are read from @genesislcap/ai-assistant 15.54 (chat-interaction-wrapper,
|
|
5
|
+
* interaction-controller, genesis/write-commit, register-genesis-assistant), the version the seed's
|
|
6
|
+
* assistant runs on.
|
|
7
|
+
*
|
|
8
|
+
* Tags for selective context injection — inject when conversation mentions these keywords.
|
|
9
|
+
*/
|
|
10
|
+
export const FUI_AI_EXTENSIONS_TAGS = [
|
|
11
|
+
'ai/extensions',
|
|
12
|
+
'ai extensions',
|
|
13
|
+
'ai chat',
|
|
14
|
+
'chat tool',
|
|
15
|
+
'assistant tool',
|
|
16
|
+
'toolDefinitions',
|
|
17
|
+
'toolHandlers',
|
|
18
|
+
'systemPrompt',
|
|
19
|
+
'requestInteraction',
|
|
20
|
+
'interaction-completed',
|
|
21
|
+
'review widget',
|
|
22
|
+
'chat widget',
|
|
23
|
+
'write tool',
|
|
24
|
+
'GenesisAssistantExtensions',
|
|
25
|
+
];
|
|
26
|
+
export const FUI_AI_EXTENSIONS_DEPS = ['fui-foundation-comms'];
|
|
27
|
+
export const FUI_AI_EXTENSIONS_CONTEXT = `
|
|
28
|
+
## AI CHAT EXTENSIONS — the app's own chat tools, widgets and writes (\`ai/extensions/\`)
|
|
29
|
+
|
|
30
|
+
The generated app's AI chat already has tools made from the project: \`req_*\` reads for its request servers, and \`event_*\` actions for the events the project offers the chat. \`ai/extensions/\` is where the app adds its own: a tool those do not cover, a widget the chat shows, a rule for the chat's system prompt. This is guidance for doing that well, not a gate: build what the user asks for.
|
|
31
|
+
|
|
32
|
+
**A widget reaches the chat only through a tool in \`ai/extensions/index.ts\`**, whose handler opens it with \`context.requestInteraction\`. **A widget file on its own does nothing**: no tool opens it, so the chat never shows it. **Never tell the user a tool or widget is ready** until \`index.ts\` exports a definition and a handler that open it.
|
|
33
|
+
|
|
34
|
+
### Where the files go
|
|
35
|
+
- Path keys under \`ai/extensions/\` map to \`client/src/ai/extensions/\`. The entry is \`ai/extensions/index.ts\`: the seed writes it as \`export const extensions = {};\` and the assistant reads it. **\`vfs_read\` it first** and keep every tool already in it.
|
|
36
|
+
- Put each widget in its own file beside it (e.g. \`ai/extensions/cancel-order-review.ts\`) and import that file from \`index.ts\`, so the element is registered before any tool asks for it. **Write the tool that opens it in \`index.ts\` in the same change**: the widget file alone does nothing.
|
|
37
|
+
- Only an app whose project selected AI Chat has this folder (the app then depends on \`@genesislcap/ai-assistant\`). In any other app an \`ai/\` write is skipped, and the write's \`warnings\` say so: AI Chat has to be on in the project first.
|
|
38
|
+
- Never write \`ai/generated/\`: it is rewritten from the project's AI config on every generate.
|
|
39
|
+
- **Import nothing from \`@genesislcap/ai-assistant\` or \`@genesislcap/foundation-ai\`** in these files. The app does not depend on foundation-ai, and the assistant checks the shape of \`extensions\` where it reads it, so type what you need structurally (section 1). Import only packages every generated app has: \`@genesislcap/foundation-comms\`, \`@genesislcap/web-core\`, \`@genesislcap/foundation-events\`, \`@genesislcap/rapid-grid-pro\`.
|
|
40
|
+
- **No secrets and no AI vendor calls.** Never put an API key, token or password in these files, and never call an AI vendor (OpenAI, Anthropic, Gemini, …) from them: the app's chat proxy holds the key and meters the spend. A tool talks to the app's own server through Connect, and to nothing else.
|
|
41
|
+
- Extensions add tools, widgets and prompt text only. They cannot declare agents or sub-agents.
|
|
42
|
+
|
|
43
|
+
### 1. The tool shape
|
|
44
|
+
One definition the model sees, and one handler that runs it, under the same name:
|
|
45
|
+
|
|
46
|
+
\`\`\`ts
|
|
47
|
+
// ai/extensions/index.ts
|
|
48
|
+
import { getConnect } from '@genesislcap/foundation-comms';
|
|
49
|
+
import './cancel-order-review'; // registers <cancel-order-review> before a tool opens it
|
|
50
|
+
|
|
51
|
+
// Typed here, not imported: the assistant checks \`extensions\` against its own types where it reads it.
|
|
52
|
+
type InteractionResult<T = unknown> = {
|
|
53
|
+
status: 'approved' | 'rejected' | 'timeout';
|
|
54
|
+
payload?: T;
|
|
55
|
+
message?: string;
|
|
56
|
+
};
|
|
57
|
+
type ToolContext = {
|
|
58
|
+
requestInteraction: <T = unknown>(
|
|
59
|
+
componentName: string,
|
|
60
|
+
data: unknown,
|
|
61
|
+
options?: { timeoutMs?: number; signal?: AbortSignal },
|
|
62
|
+
) => Promise<T>;
|
|
63
|
+
turnSignal?: AbortSignal;
|
|
64
|
+
};
|
|
65
|
+
type ToolHandler = (args: Record<string, unknown>, context: ToolContext) => Promise<unknown>;
|
|
66
|
+
|
|
67
|
+
/** A criteria string literal: backslashes, then quotes, escaped, so a value can never end the expression. */
|
|
68
|
+
const quoted = (value: string) => \`'\${value.replace(/\\\\/g, '\\\\\\\\').replace(/'/g, "\\\\'")}'\`;
|
|
69
|
+
|
|
70
|
+
const openOrders: ToolHandler = async (args) => {
|
|
71
|
+
const counterpartyId = String(args.counterpartyId ?? '').trim();
|
|
72
|
+
if (!counterpartyId) return 'Say which counterparty (its COUNTERPARTY_ID).';
|
|
73
|
+
const reply = await getConnect().request('REQ_ORDER', {
|
|
74
|
+
REQUEST: {},
|
|
75
|
+
DETAILS: { CRITERIA_MATCH: \`COUNTERPARTY_ID == \${quoted(counterpartyId)} && STATUS == 'OPEN'\` },
|
|
76
|
+
});
|
|
77
|
+
return serialisable((reply.REPLY ?? []).slice(0, 20));
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const extensions = {
|
|
81
|
+
toolDefinitions: [
|
|
82
|
+
{
|
|
83
|
+
name: 'open_orders_for_counterparty',
|
|
84
|
+
description:
|
|
85
|
+
'Lists the OPEN orders of one counterparty, at most 20. Use it when the user asks what a ' +
|
|
86
|
+
'counterparty still has open. Returns rows of ORDER_ID, INSTRUMENT_ID, QUANTITY, PRICE.',
|
|
87
|
+
parameters: {
|
|
88
|
+
type: 'object',
|
|
89
|
+
properties: {
|
|
90
|
+
counterpartyId: { type: 'string', description: 'The COUNTERPARTY_ID, exactly as stored.' },
|
|
91
|
+
},
|
|
92
|
+
required: ['counterpartyId'],
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
toolHandlers: { open_orders_for_counterparty: openOrders },
|
|
97
|
+
};
|
|
98
|
+
\`\`\`
|
|
99
|
+
- **Name**: snake_case and unique. Never \`req_*\` or \`event_*\`: those are the generated tools' names, and an extension tool with one of them replaces the generated tool, so the chat silently loses a tool that worked.
|
|
100
|
+
- **A wrong shape blocks the whole chat.** The assistant checks \`extensions\` when it starts, and turns itself off with a fault message if:
|
|
101
|
+
- a definition has no name;
|
|
102
|
+
- a name is defined twice;
|
|
103
|
+
- a definition has no handler of the same name, or a handler has no definition;
|
|
104
|
+
- \`systemPrompt\` is not a string.
|
|
105
|
+
|
|
106
|
+
Keep \`toolDefinitions\` and \`toolHandlers\` in step.
|
|
107
|
+
- **Description**: written for the model. What it does, when to use it, what it returns, and for a write whether it asks the user first.
|
|
108
|
+
- **Parameters**: a JSON schema. Mark the required ones, and describe each value's form (an id as text, a date as \`YYYY-MM-DD\`).
|
|
109
|
+
- **What the handler returns** is what the model reads: a short string, or a small object that \`JSON.stringify\` can take. Return a refusal or a failure as text the model can relay (\`'Not written: …'\`) rather than throwing.
|
|
110
|
+
- The app's tsconfig is \`strict\` with \`noUnusedParameters\`, so an unused parameter is named \`_args\` or \`_context\`.
|
|
111
|
+
- **A handler returns a Promise** (write it \`async\`), as the assistant's \`ChatToolHandlers\` requires. The assistant checks \`extensions\` against that type in \`ai/generated/assistant.ts\`, and the type check of the files you wrote does not report a mismatch there, so a handler typed \`=> unknown\` passes your check and still breaks the app's build.
|
|
112
|
+
- Confirm every resource and event name with \`query_project_schema\` before using it. The names here (\`REQ_ORDER\`, \`EVENT_ORDER_CANCEL\`) are examples.
|
|
113
|
+
|
|
114
|
+
### 2. Reads
|
|
115
|
+
- Read with Connect: \`getConnect().request('REQ_<TABLE>', …)\` for a request server, or \`snapshot(resource, { CRITERIA_MATCH, MAX_ROWS })\` for a data server. See \`fui-foundation-comms\` for the shapes.
|
|
116
|
+
- **Filter a request server with \`DETAILS.CRITERIA_MATCH\`.** The Genesis server also applies fields put in \`REQUEST\`, but Create's instant preview does not. That is the mock backend the app runs on until the user switches to the live server, and there every row comes back. So \`REQUEST: { ORDER_ID }\` followed by \`REPLY[0]\` reads somebody else's order in the preview. Both apply \`CRITERIA_MATCH\` (string values single-quoted, with backslashes and quotes escaped, as \`quoted\` does above).
|
|
117
|
+
- **Use the row whose key is the one you asked for**, never \`REPLY[0]\` blindly.
|
|
118
|
+
- **Cap the rows in code** (\`.slice(0, N)\`): the instant preview does not apply \`MAX_ROWS\`. Return only the fields the model needs, since everything a tool returns goes into the conversation.
|
|
119
|
+
- **A LONG beyond 2^53 arrives as a \`BigInt\`.** That covers ids, \`RECORD_ID\`, sequences and nano timestamps. \`JSON.stringify\` throws on one ("Do not know how to serialize a BigInt"), and the chat stringifies every tool result and every widget's \`data\`. So pass rows through a helper first:
|
|
120
|
+
\`\`\`ts
|
|
121
|
+
/** A safe integer becomes a number; anything larger becomes its exact digits, never a rounded number. */
|
|
122
|
+
function serialisable(value: unknown): unknown {
|
|
123
|
+
if (typeof value === 'bigint') {
|
|
124
|
+
const asNumber = Number(value);
|
|
125
|
+
return Number.isSafeInteger(asNumber) ? asNumber : value.toString();
|
|
126
|
+
}
|
|
127
|
+
if (Array.isArray(value)) return value.map(serialisable);
|
|
128
|
+
if (value && typeof value === 'object') {
|
|
129
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, serialisable(v)]));
|
|
130
|
+
}
|
|
131
|
+
return value;
|
|
132
|
+
}
|
|
133
|
+
\`\`\`
|
|
134
|
+
- **DATE and DATETIME travel as epoch millis** (UTC), both ways. Show them formatted, and send millis.
|
|
135
|
+
- Reads run as the signed-in user, so the server's rights apply to them.
|
|
136
|
+
|
|
137
|
+
### 3. Widgets: \`context.requestInteraction\`
|
|
138
|
+
A handler can put a widget in the chat and wait for the user: \`await context.requestInteraction(tag, data, options)\`. This is the contract (\`@genesislcap/ai-assistant\` 15.54):
|
|
139
|
+
- The widget is a **custom element**, created by its tag. It does **not** get \`data\` as a property. It gets \`interaction\`:
|
|
140
|
+
- \`{ status: 'pending', data, … }\` while it waits;
|
|
141
|
+
- \`{ status: 'resolved', data, result }\` once answered, set again on the live element when the answer lands.
|
|
142
|
+
- It answers by dispatching **\`interaction-completed\`** on itself, with the result as the event's detail: \`{ status: 'approved', payload? }\` or \`{ status: 'rejected', message? }\`. Only the first answer counts.
|
|
143
|
+
- \`requestInteraction\` **resolves** with that result. It never rejects for a refusal. With \`timeoutMs\`, or when \`signal\` aborts, it resolves \`{ status: 'timeout', message }\` and the widget turns read-only. **Pass \`signal: context.turnSignal\`**, so the user's Stop closes a widget left open.
|
|
144
|
+
- **Only one interaction may be open at a time**: a second \`requestInteraction\` while one waits throws at once. Open widgets one after another, never from parallel calls.
|
|
145
|
+
- A tag that is not registered shows "Component … is not available" and the call never settles. That is why \`index.ts\` imports the widget file.
|
|
146
|
+
- The tag must contain a hyphen and be unique in the app. Prefix it with something of the app's (e.g. \`orders-cancel-review\`) rather than a generic name.
|
|
147
|
+
- The app registers the rapid design system and grid-pro app-wide, so a widget's template can use \`<rapid-button>\`, \`<rapid-text-field>\` and \`<rapid-grid-pro>\` directly.
|
|
148
|
+
- **Render the widget through its own template** (the \`template\` given to \`@customElement\`, as below), even though the app's pages are React. A \`GenesisElement\` has its own shadow root with no slot, so anything rendered into the element itself never shows, and the widget opens blank. That covers \`createRoot(this)\`, \`this.innerHTML\` and React in the light DOM. Keep React out of chat widgets.
|
|
149
|
+
|
|
150
|
+
A review widget, in the app's own \`GenesisElement\` (decorators are on in the app's tsconfig):
|
|
151
|
+
\`\`\`ts
|
|
152
|
+
// ai/extensions/cancel-order-review.ts
|
|
153
|
+
import { css, customElement, GenesisElement, html, observable, when } from '@genesislcap/web-core';
|
|
154
|
+
|
|
155
|
+
type Review = { orderId: string; rows: Record<string, unknown>[] };
|
|
156
|
+
type Interaction =
|
|
157
|
+
| { status: 'pending'; data: Review }
|
|
158
|
+
| { status: 'resolved'; data: Review; result?: { status: string; message?: string } };
|
|
159
|
+
|
|
160
|
+
const template = html<CancelOrderReview>\`
|
|
161
|
+
<rapid-grid-pro
|
|
162
|
+
:rowData="\${(x) => x.review?.rows ?? []}"
|
|
163
|
+
:gridOptions="\${(x) => x.gridOptions}"
|
|
164
|
+
domLayout="autoHeight"
|
|
165
|
+
></rapid-grid-pro>
|
|
166
|
+
\${when(
|
|
167
|
+
(x) => x.interaction?.status === 'pending',
|
|
168
|
+
html<CancelOrderReview>\`
|
|
169
|
+
<rapid-button @click="\${(x) => x.answer('approved')}">Cancel the order</rapid-button>
|
|
170
|
+
<rapid-button @click="\${(x) => x.answer('rejected')}">Keep it</rapid-button>
|
|
171
|
+
\`,
|
|
172
|
+
)}
|
|
173
|
+
\${when(
|
|
174
|
+
(x) => x.interaction?.status === 'resolved',
|
|
175
|
+
html<CancelOrderReview>\`<span>\${(x) => x.outcome}</span>\`,
|
|
176
|
+
)}
|
|
177
|
+
\`;
|
|
178
|
+
|
|
179
|
+
@customElement({
|
|
180
|
+
name: 'orders-cancel-review',
|
|
181
|
+
template,
|
|
182
|
+
styles: css\`:host { display: block; }\`,
|
|
183
|
+
})
|
|
184
|
+
export class CancelOrderReview extends GenesisElement {
|
|
185
|
+
@observable interaction?: Interaction;
|
|
186
|
+
|
|
187
|
+
gridOptions = {
|
|
188
|
+
columnDefs: [
|
|
189
|
+
{ field: 'ORDER_ID', headerName: 'Order' },
|
|
190
|
+
{ field: 'STATUS', headerName: 'Status' },
|
|
191
|
+
{ field: 'QUANTITY', headerName: 'Quantity' },
|
|
192
|
+
],
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
get review(): Review | undefined {
|
|
196
|
+
return this.interaction?.data;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
get outcome(): string {
|
|
200
|
+
const result = this.interaction?.status === 'resolved' ? this.interaction.result : undefined;
|
|
201
|
+
return result?.status === 'approved' ? 'Approved' : result?.message ?? 'Not approved';
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
answer(status: 'approved' | 'rejected') {
|
|
205
|
+
this.$emit(
|
|
206
|
+
'interaction-completed',
|
|
207
|
+
status === 'approved' ? { status } : { status, message: 'The user kept the order.' },
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
\`\`\`
|
|
212
|
+
- A widget can show anything the user needs to decide: a grid of the rows (as above), a form to edit values before approving (return them as \`payload\`), or a choice between options.
|
|
213
|
+
- **An inline \`@keydown\` or \`@input\` handler on an input must \`return true\`.** A FAST event handler that returns anything but \`true\` makes FAST call \`preventDefault()\`, so a handler that returns nothing stops the user typing in the field. A \`@click\` on a button gets away with it, because a button has no default to lose.
|
|
214
|
+
- Show the values exactly as they will be sent, and tell the model what the user decided through the result.
|
|
215
|
+
|
|
216
|
+
### 4. Writes: show a review first, by default
|
|
217
|
+
Unless the user explicitly asks for a tool that writes without asking, every write tool follows this order:
|
|
218
|
+
1. **Read the row by its key, from its table's own request server** (\`REQ_<TABLE>\` for the table the event writes; \`CRITERIA_MATCH\`, then the row with that key): the pre-image. Not a view or a query over the table: it can hold other rows or other fields, and in the instant preview a view's request server returns no rows, so the tool reports "not found" for a row that exists.
|
|
219
|
+
2. **Show the review**: \`requestInteraction\` with the row and the exact values that will be sent.
|
|
220
|
+
3. **Not approved: stop.** \`rejected\` gives \`'Not written: <message>'\`, and \`timeout\` gives \`'Not written: no answer.'\`
|
|
221
|
+
4. **Read the row again by its key, just before committing.** If any value the review showed has changed, refuse: \`'Not written: the order changed since the review (QUANTITY).'\` Leave out \`RECORD_ID\` and \`TIMESTAMP\`, which move on every write, and compare as text, so that a \`BigInt\` and its digits match.
|
|
222
|
+
5. **Commit exactly what was approved**, then report what the server said.
|
|
223
|
+
|
|
224
|
+
\`\`\`ts
|
|
225
|
+
const SHOWN = ['ORDER_ID', 'STATUS', 'QUANTITY'];
|
|
226
|
+
// By CRITERIA_MATCH, and only the row with the asked key: the instant preview ignores REQUEST fields (see Reads).
|
|
227
|
+
const rowOf = async (orderId: string) => {
|
|
228
|
+
const reply = await getConnect().request('REQ_ORDER', {
|
|
229
|
+
REQUEST: {},
|
|
230
|
+
DETAILS: { CRITERIA_MATCH: \`ORDER_ID == \${quoted(orderId)}\` },
|
|
231
|
+
});
|
|
232
|
+
return (reply.REPLY ?? []).find((r: Record<string, unknown>) => String(r.ORDER_ID) === orderId) as
|
|
233
|
+
| Record<string, unknown>
|
|
234
|
+
| undefined;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const cancelOrder: ToolHandler = async (args, context) => {
|
|
238
|
+
const orderId = String(args.orderId ?? '').trim();
|
|
239
|
+
const before = orderId ? await rowOf(orderId) : undefined;
|
|
240
|
+
if (!before) return \`Not written: there is no order \${orderId}.\`;
|
|
241
|
+
|
|
242
|
+
const answer = await context.requestInteraction<InteractionResult>(
|
|
243
|
+
'orders-cancel-review',
|
|
244
|
+
{ orderId, rows: [serialisable(before)] },
|
|
245
|
+
{ signal: context.turnSignal },
|
|
246
|
+
);
|
|
247
|
+
if (answer.status !== 'approved') {
|
|
248
|
+
return \`Not written: \${answer.status === 'timeout' ? 'no answer' : answer.message ?? 'the user declined'}.\`;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const now = await rowOf(orderId);
|
|
252
|
+
const changed = SHOWN.filter((f) => String(now?.[f] ?? '') !== String(before[f] ?? ''));
|
|
253
|
+
if (!now || changed.length) {
|
|
254
|
+
return \`Not written: the order changed since the review (\${changed.join(', ') || 'it is gone'}).\`;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
const reply = await getConnect().commitEvent('EVENT_ORDER_CANCEL', { DETAILS: { ORDER_ID: orderId } });
|
|
259
|
+
if (reply.MESSAGE_TYPE === 'EVENT_ACK') return \`Order \${orderId} cancelled.\`;
|
|
260
|
+
return \`Not written: \${(reply.ERROR ?? []).map((e: { TEXT?: string }) => e.TEXT).join('; ') || 'the server refused it'}.\`;
|
|
261
|
+
} catch (error) {
|
|
262
|
+
const received = (error as { receivedMessage?: { MESSAGE_TYPE?: string; ERROR?: { TEXT?: string }[] } })
|
|
263
|
+
?.receivedMessage;
|
|
264
|
+
if (received?.MESSAGE_TYPE === 'EVENT_NACK' || received?.MESSAGE_TYPE === 'MSG_NACK') {
|
|
265
|
+
return \`Not written: \${(received.ERROR ?? []).map((e) => e.TEXT).join('; ') || 'the server refused it'}.\`;
|
|
266
|
+
}
|
|
267
|
+
return 'The app did not answer, so whether the order was cancelled is unknown.';
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
\`\`\`
|
|
271
|
+
- **A write that skips the review** is written only when the user asks for one. Its description then says so plainly, e.g. \`'Cancels the order immediately, without asking the user to review it.'\`
|
|
272
|
+
|
|
273
|
+
### 5. Write traps
|
|
274
|
+
- **\`commitEvent\` RESOLVES on a NACK.** Check \`MESSAGE_TYPE === 'EVENT_ACK'\`, and never report "written" without one. A NACK carries the server's reasons in \`ERROR[].TEXT\`: relay those.
|
|
275
|
+
- **The instant preview ignores \`REQUEST\` fields.** Read by \`DETAILS.CRITERIA_MATCH\`, which the preview and the server both apply, and act only on the row whose key you asked for. Otherwise, in the preview, a tool cancels or refuses the wrong order.
|
|
276
|
+
- **A throw is not a refusal.** A rejection carrying \`receivedMessage\` with \`EVENT_NACK\` or \`MSG_NACK\` is the server's refusal. Anything else means the connection gave up, and the write may or may not have landed, so say that it is unknown.
|
|
277
|
+
- **Never send \`VALIDATE: true\` as a dry run.** A handler without an \`onValidate\` block (\`hasValidate: false\` in \`query_project_schema\`) COMMITS the row anyway, and even with one it is not a preview of the write. Show the review widget instead.
|
|
278
|
+
- **Whole numbers beyond 2^53 cannot be sent as numbers.** A key read as a \`BigInt\` or as digits must not go through \`Number()\`, because a rounded id writes against the wrong record. If the event takes it as a number and it is not a safe integer, refuse rather than round.
|
|
279
|
+
- **DATE and DATETIME are epoch millis.** Convert a date the user typed before sending, and show the date back in the review.
|
|
280
|
+
- **Field sizes**: a plain \`STRING\` field holds 64 characters on the app's H2 database, and \`INT\` is 32-bit. Check a value fits before sending it, or the server refuses it, sometimes with an SQL error.
|
|
281
|
+
- **It runs as the signed-in user.** The event's permission codes, and the handler's own validation, still apply. A refusal is an answer to relay, not a bug to work around.
|
|
282
|
+
- **Do not hand raw \`Error.message\` to the model.** A transport error can carry internal host names: return your own plain text.
|
|
283
|
+
- Send \`IGNORE_WARNINGS\` only if the user asked to.
|
|
284
|
+
|
|
285
|
+
### 6. Rules for the system prompt
|
|
286
|
+
\`systemPrompt\` is appended to the chat's configured prompt. Use it to tell the model when each of your tools applies, and what the server will refuse:
|
|
287
|
+
\`\`\`ts
|
|
288
|
+
export const extensions = {
|
|
289
|
+
toolDefinitions: [/* … */],
|
|
290
|
+
toolHandlers: { /* … */ },
|
|
291
|
+
systemPrompt: [
|
|
292
|
+
'Use cancel_order only for an order the user names; it shows the order for review before cancelling.',
|
|
293
|
+
'Cancelling needs the ORDER_CANCEL right; without it the server refuses and you say so.',
|
|
294
|
+
'Only an OPEN order can be cancelled: the server refuses any other with "Order is already <status>".',
|
|
295
|
+
'If the server refuses, relay its reason word for word.',
|
|
296
|
+
].join('\\n'),
|
|
297
|
+
};
|
|
298
|
+
\`\`\`
|
|
299
|
+
- Derive the rules from \`query_project_schema('events', NAME)\`:
|
|
300
|
+
- **\`refusals\`**: each message the handler's own code refuses with, as \`{ block, line, text }\`. A \`validate\` one is checked before anything is written, and a \`commit\` one while writing. \`\${…}\` is a value the server fills in. Turn each into a rule the model can act on, e.g. \`Order is already \${…}\` becomes "only an OPEN order can be cancelled".
|
|
301
|
+
- \`permissionCodes\`: the rights it needs.
|
|
302
|
+
- \`writes\` and \`enumWrites\`: what it changes.
|
|
303
|
+
- \`entity\` and \`inputType\`.
|
|
304
|
+
- \`refusals\` is a floor, not a complete list: a message built in code, or a check in a function it calls, is not in it. For a Spring handler, or a spec from an older preview, it is unknown; the answer's \`refusalsNote\` says which. **Anything not listed there, ask the user** rather than invent it, or write the general form ("the server may refuse it; relay its reason").
|
|
305
|
+
- Keep it short and specific to the app's own tools. The chat's base prompt already covers the generated tools.
|
|
306
|
+
`;
|