@aintela/chat 0.2.38 → 0.2.39

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.
@@ -85,22 +85,25 @@ export type UiToolActionFn<TArgs, In = unknown> = (args: TArgs, ctx: UiToolActio
85
85
  * server stamps via `[KernelUIComponent]` (`functionDetails.askUserComponent`), not by the wire tool
86
86
  * name. Same `(args, ctx)` shape as a render tool.
87
87
  */
88
- export type UiToolAskUserQuestionFn<In = unknown, Ans = unknown> = (args: UiToolArgs, ctx: UiToolContext<In, Ans>) => React.ReactNode;
88
+ export type UiToolAskUserQuestionFn<In = unknown, Ans = unknown, TArgs = UiToolArgs> = (args: TArgs, ctx: UiToolContext<In, Ans>) => React.ReactNode;
89
89
  /**
90
90
  * The ANSWERED face of an ask-user component — the locked summary drawn inline in the transcript once
91
91
  * the tool has responded. Receives the tool's response (also available as `ctx.response`), so a host
92
92
  * can render "you answered X" without re-deriving it from args.
93
93
  */
94
- export type UiToolAskUserResponseFn<In = unknown, Ans = unknown, Res = unknown> = (args: UiToolArgs, ctx: UiToolContext<In, Ans>, response: Res) => React.ReactNode;
94
+ export type UiToolAskUserResponseFn<In = unknown, Ans = unknown, Res = unknown, TArgs = UiToolArgs> = (args: TArgs, ctx: UiToolContext<In, Ans>, response: Res) => React.ReactNode;
95
95
  /**
96
96
  * A host ask-user registration: the interactive `question` (required, drawn while parked) plus an
97
97
  * optional locked `response` summary (drawn inline once answered). Omit `response` to reuse `question`
98
98
  * in both states — the question component then owns its own answered/locked view (as the built-in
99
99
  * `AskUserPart`/`AskAuthPart` do).
100
+ *
101
+ * `TArgs` (defaulted, last so older parametrizations keep compiling) is the tool-CALL args shape —
102
+ * the codegen'd typed methods bind it to the generated `…Args` interface.
100
103
  */
101
- export interface UiToolAskUserRenderers<In = unknown, Ans = unknown, Res = unknown> {
102
- question: UiToolAskUserQuestionFn<In, Ans>;
103
- response?: UiToolAskUserResponseFn<In, Ans, Res>;
104
+ export interface UiToolAskUserRenderers<In = unknown, Ans = unknown, Res = unknown, TArgs = UiToolArgs> {
105
+ question: UiToolAskUserQuestionFn<In, Ans, TArgs>;
106
+ response?: UiToolAskUserResponseFn<In, Ans, Res, TArgs>;
104
107
  }
105
108
  export interface UiToolRenderOptions<TArgs> {
106
109
  /** Runtime arg guard/coercion. Return `null` to render nothing (treated as invalid args). */
@@ -166,7 +169,7 @@ export interface UiToolRegistryBuilder {
166
169
  * `askUpdateProfile`) are this method with those generics pre-bound — declaration-merged onto this
167
170
  * interface so the fluent chain stays fully typed.
168
171
  */
169
- askUser<In = unknown, Ans = unknown, Res = unknown>(componentName: string, renderers: UiToolAskUserRenderers<In, Ans, Res>, opts?: UiToolRenderOptions<UiToolArgs>): UiToolRegistryBuilder;
172
+ askUser<In = unknown, Ans = unknown, Res = unknown, TArgs = UiToolArgs>(componentName: string, renderers: UiToolAskUserRenderers<In, Ans, Res, TArgs>, opts?: UiToolRenderOptions<TArgs>): UiToolRegistryBuilder;
170
173
  build(): UiToolRegistry;
171
174
  }
172
175
  /**
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@aintela/chat",
3
- "version": "0.2.38",
3
+ "version": "0.2.39",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
+ "bin": {
9
+ "aia-gen-uitools": "./scripts/gen-uitools.mjs"
10
+ },
8
11
  "main": "./dist/index.js",
9
12
  "types": "./dist/index.d.ts",
10
13
  "sideEffects": [
@@ -66,8 +69,12 @@
66
69
  },
67
70
  "files": [
68
71
  "dist",
72
+ "scripts/gen-uitools.mjs",
69
73
  "README.md"
70
74
  ],
75
+ "dependencies": {
76
+ "json-schema-to-typescript": "^15.0.0"
77
+ },
71
78
  "scripts": {
72
79
  "build": "vite build",
73
80
  "dev": "vite build --watch",
@@ -0,0 +1,155 @@
1
+ #!/usr/bin/env node
2
+ // aia-gen-uitools — turn the UI-tools manifest (per-tool args/input/answer/result JSON Schema,
3
+ // emitted by the server's `UiToolsManifest.PublishManifest(...)` executable) into TypeScript:
4
+ // • uiContracts.generated.ts — the args/input/answer/result interfaces (named after the C# contracts)
5
+ // • aiaUiTools.generated.ts — a typed registry builder adding one method per tool
6
+ // (askUpdateProfile, presentDataTable, …), each typed with its
7
+ // generated `…Args` interface + payload shapes.
8
+ //
9
+ // Shipped as the `aia-gen-uitools` bin of @aintela/chat — the library owns the generator; consumer
10
+ // apps only run it. Usage: aia-gen-uitools <manifest.json> <out-dir>
11
+ import { compile } from 'json-schema-to-typescript';
12
+ import { readFileSync, writeFileSync, mkdirSync } from 'node:fs';
13
+ import { join } from 'node:path';
14
+
15
+ const [, , manifestPath, outDir] = process.argv;
16
+ if (!manifestPath || !outDir) {
17
+ console.error('usage: aia-gen-uitools <manifest.json> <out-dir>');
18
+ process.exit(1);
19
+ }
20
+ const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
21
+ mkdirSync(outDir, { recursive: true });
22
+
23
+ const PRIMITIVE = { string: 'string', integer: 'number', number: 'number', boolean: 'boolean' };
24
+
25
+ // A schema that's a bare scalar (e.g. the askauth token → `string`) becomes an inline TS primitive.
26
+ function primitiveOf(schema) {
27
+ if (!schema || schema.properties) return null;
28
+ const t = Array.isArray(schema.type) ? schema.type.find((x) => x !== 'null') : schema.type;
29
+ return PRIMITIVE[t] ?? null;
30
+ }
31
+
32
+ const opts = { bannerComment: '', additionalProperties: false, format: false };
33
+ const interfaces = new Map(); // declaration name -> its TS block (deduped ACROSS compiles)
34
+
35
+ // One compile() output holds the requested type PLUS every nested sub-schema hoisted into its own
36
+ // named declaration. Split it so identical hoisted names from different tools (deep contracts share
37
+ // sub-types) dedupe instead of colliding as duplicate identifiers in the generated file.
38
+ function splitDeclarations(ts) {
39
+ const starts = [...ts.matchAll(/^export (?:interface|type) (\w+)/gm)]
40
+ .map((m) => ({ name: m[1], start: m.index }));
41
+ return starts.map(({ name, start }, i) => ({
42
+ name,
43
+ text: ts.slice(start, i + 1 < starts.length ? starts[i + 1].start : ts.length).trim(),
44
+ }));
45
+ }
46
+
47
+ // Registers the schema's declarations (deduped by name) and returns the TS type to reference at
48
+ // the use site; `fallback` is used when the tool has no schema for that leg.
49
+ async function register(typeName, schema, fallback = 'void') {
50
+ if (!typeName || !schema) return fallback;
51
+ const prim = primitiveOf(schema);
52
+ if (prim) return prim;
53
+ if (!interfaces.has(typeName)) {
54
+ const ts = await compile(schema, typeName, opts);
55
+ for (const block of splitDeclarations(ts)) {
56
+ const existing = interfaces.get(block.name);
57
+ if (existing === undefined) interfaces.set(block.name, block.text);
58
+ else if (existing !== block.text)
59
+ console.warn(`aia-gen-uitools: type name '${block.name}' appears with differing shapes; keeping the first.`);
60
+ }
61
+ }
62
+ return typeName;
63
+ }
64
+
65
+ const rows = [];
66
+ for (const c of manifest.components) {
67
+ rows.push({
68
+ ...c,
69
+ argsTs: await register(c.argsTypeName, c.args, 'UiToolArgs'),
70
+ inputTs: await register(c.inputTypeName, c.input),
71
+ answerTs: await register(c.answerTypeName, c.answer),
72
+ resultTs: await register(c.resultTypeName, c.result),
73
+ });
74
+ }
75
+
76
+ // ---- uiContracts.generated.ts -------------------------------------------------
77
+ writeFileSync(
78
+ join(outDir, 'uiContracts.generated.ts'),
79
+ [
80
+ '// AUTO-GENERATED by aia-gen-uitools — do not edit. Source: the server UiToolsManifest.',
81
+ '/* eslint-disable */',
82
+ '',
83
+ ...[...interfaces.values()],
84
+ '',
85
+ ].join('\n'),
86
+ );
87
+
88
+ // ---- aiaUiTools.generated.ts --------------------------------------------------
89
+ // Import only the types the builder methods reference (sub-declarations stay in uiContracts).
90
+ const used = new Set();
91
+ for (const r of rows)
92
+ for (const t of [r.argsTs, r.inputTs, r.answerTs, r.resultTs]) if (t && interfaces.has(t)) used.add(t);
93
+ const named = [...used].sort();
94
+ const askRows = rows.filter((r) => r.kind === 'askUser' && r.clientMethod);
95
+ const renderRows = rows.filter((r) => r.kind === 'render' && r.clientMethod);
96
+
97
+ const declAsk = askRows.map(
98
+ (r) => ` ${r.clientMethod}(renderers: UiToolAskUserRenderers<${r.inputTs}, ${r.answerTs}, ${r.resultTs}, ${r.argsTs}>): AiaUiToolRegistryBuilder;`,
99
+ );
100
+ const declRender = renderRows.map(
101
+ (r) => ` ${r.clientMethod}(render: UiToolRenderFn<${r.argsTs}, ${r.inputTs}>): AiaUiToolRegistryBuilder;`,
102
+ );
103
+ const implAsk = askRows.map(
104
+ (r) => ` ${r.clientMethod}: (renderers: UiToolAskUserRenderers<${r.inputTs}, ${r.answerTs}, ${r.resultTs}, ${r.argsTs}>) => b.askUser('${r.component}', renderers),`,
105
+ );
106
+ const implRender = renderRows.map(
107
+ (r) => ` ${r.clientMethod}: (render: UiToolRenderFn<${r.argsTs}, ${r.inputTs}>) => b.render('${r.toolName}', render),`,
108
+ );
109
+
110
+ const importTypes = named.length ? `import type { ${named.join(', ')} } from './uiContracts.generated';\n` : '';
111
+
112
+ writeFileSync(
113
+ join(outDir, 'aiaUiTools.generated.ts'),
114
+ `// AUTO-GENERATED by aia-gen-uitools — do not edit. Source: the server UiToolsManifest.
115
+ /* eslint-disable */
116
+ import {
117
+ createUiToolRegistry,
118
+ type UiToolRegistry,
119
+ type UiToolRegistryBuilder,
120
+ type UiToolArgs,
121
+ type UiToolRenderFn,
122
+ type UiToolActionFn,
123
+ type UiToolRenderOptions,
124
+ type UiToolActionOptions,
125
+ type UiToolAskUserRenderers,
126
+ } from '@aintela/chat';
127
+ ${importTypes}
128
+ /**
129
+ * A builder with the typed per-tool methods (askUpdateProfile, …). It EXTENDS @aintela/chat's
130
+ * UiToolRegistryBuilder rather than declaration-merging onto it — merging would force the library's own
131
+ * builder implementation (compiled from source in dev via the alias) to declare these app-specific methods.
132
+ * The inherited builder methods are re-declared to return THIS builder, so the fluent chain keeps the typed
133
+ * methods no matter the call order. (Signatures mirror @aintela/chat's UiToolRegistryBuilder; regenerate if
134
+ * that surface changes.)
135
+ */
136
+ export interface AiaUiToolRegistryBuilder extends UiToolRegistryBuilder {
137
+ render<TArgs = UiToolArgs, In = unknown>(name: string, render: UiToolRenderFn<TArgs, In>, opts?: UiToolRenderOptions<TArgs>): AiaUiToolRegistryBuilder;
138
+ action<TArgs = UiToolArgs, In = unknown>(name: string, run: UiToolActionFn<TArgs, In>, opts?: UiToolActionOptions<TArgs>): AiaUiToolRegistryBuilder;
139
+ askUser<In = unknown, Ans = unknown, Res = unknown, TArgs = UiToolArgs>(componentName: string, renderers: UiToolAskUserRenderers<In, Ans, Res, TArgs>, opts?: UiToolRenderOptions<TArgs>): AiaUiToolRegistryBuilder;
140
+ build(): UiToolRegistry;
141
+ ${[...declAsk, ...declRender].join('\n')}
142
+ }
143
+
144
+ // Each method delegates to the public askUser/render, pre-binding the component/tool name. Object.assign
145
+ // onto the one builder instance the factory returns, so chaining is preserved at runtime.
146
+ export function createAiaUiToolRegistry(): AiaUiToolRegistryBuilder {
147
+ const b = createUiToolRegistry() as AiaUiToolRegistryBuilder;
148
+ return Object.assign(b, {
149
+ ${[...implAsk, ...implRender].join('\n')}
150
+ });
151
+ }
152
+ `,
153
+ );
154
+
155
+ console.log(`aia-gen-uitools — ${interfaces.size} interfaces + ${askRows.length + renderRows.length} builder methods → ${outDir}`);