@objectstack/service-ai 4.0.0 → 4.0.2

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.
Files changed (40) hide show
  1. package/.turbo/turbo-build.log +11 -11
  2. package/CHANGELOG.md +20 -0
  3. package/dist/index.cjs +1245 -54
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.cts +344 -77
  6. package/dist/index.d.ts +344 -77
  7. package/dist/index.js +1230 -51
  8. package/dist/index.js.map +1 -1
  9. package/package.json +26 -4
  10. package/src/__tests__/ai-service.test.ts +248 -27
  11. package/src/__tests__/auth-and-toolcalling.test.ts +627 -0
  12. package/src/__tests__/chatbot-features.test.ts +229 -82
  13. package/src/__tests__/metadata-tools.test.ts +964 -0
  14. package/src/__tests__/objectql-conversation-service.test.ts +34 -16
  15. package/src/__tests__/vercel-stream-encoder.test.ts +263 -0
  16. package/src/adapters/index.ts +2 -0
  17. package/src/adapters/memory-adapter.ts +17 -9
  18. package/src/adapters/vercel-adapter.ts +148 -0
  19. package/src/agent-runtime.ts +27 -3
  20. package/src/agents/index.ts +1 -0
  21. package/src/agents/metadata-assistant-agent.ts +87 -0
  22. package/src/ai-service.ts +174 -22
  23. package/src/conversation/in-memory-conversation-service.ts +2 -2
  24. package/src/conversation/objectql-conversation-service.ts +67 -18
  25. package/src/index.ts +22 -3
  26. package/src/plugin.ts +166 -9
  27. package/src/routes/agent-routes.ts +28 -3
  28. package/src/routes/ai-routes.ts +231 -14
  29. package/src/routes/index.ts +1 -1
  30. package/src/stream/index.ts +3 -0
  31. package/src/stream/vercel-stream-encoder.ts +129 -0
  32. package/src/tools/add-field.tool.ts +70 -0
  33. package/src/tools/create-object.tool.ts +66 -0
  34. package/src/tools/delete-field.tool.ts +38 -0
  35. package/src/tools/describe-metadata-object.tool.ts +32 -0
  36. package/src/tools/index.ts +12 -1
  37. package/src/tools/list-metadata-objects.tool.ts +34 -0
  38. package/src/tools/metadata-tools.ts +430 -0
  39. package/src/tools/modify-field.tool.ts +44 -0
  40. package/src/tools/tool-registry.ts +32 -9
@@ -0,0 +1,430 @@
1
+ // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
2
+
3
+ import type { IMetadataService } from '@objectstack/spec/contracts';
4
+ import type { Tool } from '@objectstack/spec/ai';
5
+ import type { ToolHandler } from './tool-registry.js';
6
+ import type { ToolRegistry } from './tool-registry.js';
7
+
8
+ // ---------------------------------------------------------------------------
9
+ // Tool Metadata — individual .tool.ts files (single source of truth)
10
+ // ---------------------------------------------------------------------------
11
+
12
+ export { createObjectTool } from './create-object.tool.js';
13
+ export { addFieldTool } from './add-field.tool.js';
14
+ export { modifyFieldTool } from './modify-field.tool.js';
15
+ export { deleteFieldTool } from './delete-field.tool.js';
16
+ export { listMetadataObjectsTool } from './list-metadata-objects.tool.js';
17
+ export { describeMetadataObjectTool } from './describe-metadata-object.tool.js';
18
+
19
+ import { createObjectTool } from './create-object.tool.js';
20
+ import { addFieldTool } from './add-field.tool.js';
21
+ import { modifyFieldTool } from './modify-field.tool.js';
22
+ import { deleteFieldTool } from './delete-field.tool.js';
23
+ import { listMetadataObjectsTool } from './list-metadata-objects.tool.js';
24
+ import { describeMetadataObjectTool } from './describe-metadata-object.tool.js';
25
+
26
+ /** All built-in metadata management tool definitions (Tool metadata). */
27
+ export const METADATA_TOOL_DEFINITIONS: Tool[] = [
28
+ createObjectTool,
29
+ addFieldTool,
30
+ modifyFieldTool,
31
+ deleteFieldTool,
32
+ listMetadataObjectsTool,
33
+ describeMetadataObjectTool,
34
+ ];
35
+
36
+ // ---------------------------------------------------------------------------
37
+ // Internal type aliases for metadata payloads (returned as `unknown` from
38
+ // IMetadataService — we cast to these lightweight shapes for field access).
39
+ // ---------------------------------------------------------------------------
40
+
41
+ /** Minimal shape of an object definition as returned by IMetadataService. */
42
+ interface ObjectDef {
43
+ name: string;
44
+ label?: string;
45
+ fields?: Record<string, FieldDef>;
46
+ enable?: Record<string, boolean>;
47
+ }
48
+
49
+ /** Minimal shape of a field definition inside an object. */
50
+ interface FieldDef {
51
+ name?: string;
52
+ type?: string;
53
+ label?: string;
54
+ required?: boolean;
55
+ reference?: string;
56
+ options?: unknown;
57
+ defaultValue?: unknown;
58
+ }
59
+
60
+ // ---------------------------------------------------------------------------
61
+ // Shared validation helpers
62
+ // ---------------------------------------------------------------------------
63
+
64
+ /** snake_case identifier pattern (e.g. `project_task`, `due_date`). */
65
+ const SNAKE_CASE_RE = /^[a-z_][a-z0-9_]*$/;
66
+
67
+ /** Validate that a value matches snake_case. */
68
+ function isSnakeCase(value: string): boolean {
69
+ return SNAKE_CASE_RE.test(value);
70
+ }
71
+
72
+ // ---------------------------------------------------------------------------
73
+ // Context — injected once at registration time
74
+ // ---------------------------------------------------------------------------
75
+
76
+ /**
77
+ * Services required by the metadata management tools.
78
+ *
79
+ * Provided by the kernel at `ai:ready` time and closed over
80
+ * by the handler functions so they stay framework-agnostic.
81
+ */
82
+ export interface MetadataToolContext {
83
+ /** Metadata service for schema CRUD operations. */
84
+ metadataService: IMetadataService;
85
+ }
86
+
87
+ // ---------------------------------------------------------------------------
88
+ // Handler Factories
89
+ // ---------------------------------------------------------------------------
90
+
91
+ function createCreateObjectHandler(ctx: MetadataToolContext): ToolHandler {
92
+ return async (args) => {
93
+ const { name, label, fields, enableFeatures } = args as {
94
+ name: string;
95
+ label: string;
96
+ fields?: Array<{ name: string; label?: string; type: string; required?: boolean }>;
97
+ enableFeatures?: Record<string, boolean>;
98
+ };
99
+
100
+ if (!name || !label) {
101
+ return JSON.stringify({ error: 'Both "name" and "label" are required' });
102
+ }
103
+
104
+ // Validate snake_case name
105
+ if (!isSnakeCase(name)) {
106
+ return JSON.stringify({ error: `Invalid object name "${name}". Must be snake_case.` });
107
+ }
108
+
109
+ // Check if the object already exists
110
+ const existing = await ctx.metadataService.getObject(name);
111
+ if (existing) {
112
+ return JSON.stringify({ error: `Object "${name}" already exists` });
113
+ }
114
+
115
+ // Build field map from array input with per-field validation
116
+ const fieldMap: Record<string, Record<string, unknown>> = {};
117
+ if (fields && Array.isArray(fields)) {
118
+ const seenNames = new Set<string>();
119
+ for (const f of fields) {
120
+ if (!f.name) {
121
+ return JSON.stringify({ error: 'Each field must have a "name" property' });
122
+ }
123
+ if (!isSnakeCase(f.name)) {
124
+ return JSON.stringify({ error: `Invalid field name "${f.name}". Must be snake_case.` });
125
+ }
126
+ if (seenNames.has(f.name)) {
127
+ return JSON.stringify({ error: `Duplicate field name "${f.name}" in initial fields` });
128
+ }
129
+ seenNames.add(f.name);
130
+ fieldMap[f.name] = {
131
+ type: f.type,
132
+ ...(f.label ? { label: f.label } : {}),
133
+ ...(f.required !== undefined ? { required: f.required } : {}),
134
+ };
135
+ }
136
+ }
137
+
138
+ const objectDef: Record<string, unknown> = {
139
+ name,
140
+ label,
141
+ ...(Object.keys(fieldMap).length > 0 ? { fields: fieldMap } : {}),
142
+ ...(enableFeatures ? { enable: enableFeatures } : {}),
143
+ };
144
+
145
+ await ctx.metadataService.register('object', name, objectDef);
146
+
147
+ return JSON.stringify({
148
+ name,
149
+ label,
150
+ fieldCount: Object.keys(fieldMap).length,
151
+ });
152
+ };
153
+ }
154
+
155
+ function createAddFieldHandler(ctx: MetadataToolContext): ToolHandler {
156
+ return async (args) => {
157
+ const { objectName, name, label, type, required, defaultValue, options, reference } = args as {
158
+ objectName: string;
159
+ name: string;
160
+ label?: string;
161
+ type: string;
162
+ required?: boolean;
163
+ defaultValue?: unknown;
164
+ options?: Array<{ label: string; value: string }>;
165
+ reference?: string;
166
+ };
167
+
168
+ if (!objectName || !name || !type) {
169
+ return JSON.stringify({ error: '"objectName", "name", and "type" are required' });
170
+ }
171
+
172
+ // Validate snake_case names
173
+ if (!isSnakeCase(objectName)) {
174
+ return JSON.stringify({ error: `Invalid object name "${objectName}". Must be snake_case.` });
175
+ }
176
+ if (!isSnakeCase(name)) {
177
+ return JSON.stringify({ error: `Invalid field name "${name}". Must be snake_case.` });
178
+ }
179
+
180
+ // Validate reference as snake_case if provided
181
+ if (reference && !isSnakeCase(reference)) {
182
+ return JSON.stringify({ error: `Invalid reference "${reference}". Must be a snake_case object name.` });
183
+ }
184
+
185
+ // Validate select option values as snake_case if provided
186
+ if (options && Array.isArray(options)) {
187
+ for (const opt of options) {
188
+ if (opt.value && !isSnakeCase(opt.value)) {
189
+ return JSON.stringify({ error: `Invalid option value "${opt.value}". Must be lowercase snake_case.` });
190
+ }
191
+ }
192
+ }
193
+
194
+ // Verify the target object exists
195
+ const objectDef = await ctx.metadataService.getObject(objectName);
196
+ if (!objectDef) {
197
+ return JSON.stringify({ error: `Object "${objectName}" not found` });
198
+ }
199
+
200
+ // Check if field already exists
201
+ const def = objectDef as ObjectDef;
202
+ if (def.fields && def.fields[name]) {
203
+ return JSON.stringify({ error: `Field "${name}" already exists on object "${objectName}"` });
204
+ }
205
+
206
+ // Build new field definition
207
+ const fieldDef: Record<string, unknown> = {
208
+ type,
209
+ ...(label ? { label } : {}),
210
+ ...(required !== undefined ? { required } : {}),
211
+ ...(defaultValue !== undefined ? { defaultValue } : {}),
212
+ ...(options ? { options } : {}),
213
+ ...(reference ? { reference } : {}),
214
+ };
215
+
216
+ // Merge the new field into the existing object definition and re-register
217
+ const updatedFields = { ...(def.fields ?? {}), [name]: fieldDef };
218
+ await ctx.metadataService.register('object', objectName, {
219
+ ...def,
220
+ fields: updatedFields,
221
+ });
222
+
223
+ return JSON.stringify({
224
+ objectName,
225
+ fieldName: name,
226
+ fieldType: type,
227
+ });
228
+ };
229
+ }
230
+
231
+ function createModifyFieldHandler(ctx: MetadataToolContext): ToolHandler {
232
+ return async (args) => {
233
+ const { objectName, fieldName, changes } = args as {
234
+ objectName: string;
235
+ fieldName: string;
236
+ changes: Record<string, unknown>;
237
+ };
238
+
239
+ if (!objectName || !fieldName || !changes) {
240
+ return JSON.stringify({ error: '"objectName", "fieldName", and "changes" are required' });
241
+ }
242
+
243
+ // Validate snake_case names
244
+ if (!isSnakeCase(objectName)) {
245
+ return JSON.stringify({ error: `Invalid object name "${objectName}". Must be snake_case.` });
246
+ }
247
+ if (!isSnakeCase(fieldName)) {
248
+ return JSON.stringify({ error: `Invalid field name "${fieldName}". Must be snake_case.` });
249
+ }
250
+
251
+ // Verify the target object exists
252
+ const objectDef = await ctx.metadataService.getObject(objectName);
253
+ if (!objectDef) {
254
+ return JSON.stringify({ error: `Object "${objectName}" not found` });
255
+ }
256
+
257
+ const def = objectDef as ObjectDef;
258
+ if (!def.fields || !def.fields[fieldName]) {
259
+ return JSON.stringify({ error: `Field "${fieldName}" not found on object "${objectName}"` });
260
+ }
261
+
262
+ // Apply changes to the field definition
263
+ const existingField = def.fields[fieldName];
264
+ const updatedField = { ...existingField, ...changes };
265
+ const updatedFields = { ...def.fields, [fieldName]: updatedField };
266
+
267
+ await ctx.metadataService.register('object', objectName, {
268
+ ...def,
269
+ fields: updatedFields,
270
+ });
271
+
272
+ return JSON.stringify({
273
+ objectName,
274
+ fieldName,
275
+ updatedProperties: Object.keys(changes),
276
+ });
277
+ };
278
+ }
279
+
280
+ function createDeleteFieldHandler(ctx: MetadataToolContext): ToolHandler {
281
+ return async (args) => {
282
+ const { objectName, fieldName } = args as {
283
+ objectName: string;
284
+ fieldName: string;
285
+ };
286
+
287
+ if (!objectName || !fieldName) {
288
+ return JSON.stringify({ error: '"objectName" and "fieldName" are required' });
289
+ }
290
+
291
+ // Validate snake_case names
292
+ if (!isSnakeCase(objectName)) {
293
+ return JSON.stringify({ error: `Invalid object name "${objectName}". Must be snake_case.` });
294
+ }
295
+ if (!isSnakeCase(fieldName)) {
296
+ return JSON.stringify({ error: `Invalid field name "${fieldName}". Must be snake_case.` });
297
+ }
298
+
299
+ // Verify the target object exists
300
+ const objectDef = await ctx.metadataService.getObject(objectName);
301
+ if (!objectDef) {
302
+ return JSON.stringify({ error: `Object "${objectName}" not found` });
303
+ }
304
+
305
+ const def = objectDef as ObjectDef;
306
+ if (!def.fields || !def.fields[fieldName]) {
307
+ return JSON.stringify({ error: `Field "${fieldName}" not found on object "${objectName}"` });
308
+ }
309
+
310
+ // Remove the field and re-register
311
+ const { [fieldName]: _removed, ...remainingFields } = def.fields;
312
+ await ctx.metadataService.register('object', objectName, {
313
+ ...def,
314
+ fields: remainingFields,
315
+ });
316
+
317
+ return JSON.stringify({
318
+ objectName,
319
+ fieldName,
320
+ success: true,
321
+ });
322
+ };
323
+ }
324
+
325
+ function createListObjectsHandler(ctx: MetadataToolContext): ToolHandler {
326
+ return async (args) => {
327
+ const { filter, includeFields } = (args ?? {}) as {
328
+ filter?: string;
329
+ includeFields?: boolean;
330
+ };
331
+
332
+ const objects = await ctx.metadataService.listObjects();
333
+ let result = (objects as ObjectDef[]).map(o => {
334
+ const base: Record<string, unknown> = {
335
+ name: o.name,
336
+ label: o.label ?? o.name,
337
+ fieldCount: o.fields ? Object.keys(o.fields).length : 0,
338
+ };
339
+ if (includeFields && o.fields) {
340
+ base.fields = Object.entries(o.fields).map(([key, f]) => ({
341
+ name: key,
342
+ type: f.type,
343
+ label: f.label ?? key,
344
+ }));
345
+ }
346
+ return base;
347
+ });
348
+
349
+ // Apply optional name/label substring filter
350
+ if (filter) {
351
+ const lower = filter.toLowerCase();
352
+ result = result.filter(o =>
353
+ (o.name as string).toLowerCase().includes(lower) ||
354
+ (o.label as string).toLowerCase().includes(lower),
355
+ );
356
+ }
357
+
358
+ return JSON.stringify({
359
+ objects: result,
360
+ totalCount: result.length,
361
+ });
362
+ };
363
+ }
364
+
365
+ function createDescribeObjectHandler(ctx: MetadataToolContext): ToolHandler {
366
+ return async (args) => {
367
+ const { objectName } = args as { objectName: string };
368
+
369
+ if (!objectName) {
370
+ return JSON.stringify({ error: '"objectName" is required' });
371
+ }
372
+
373
+ // Validate snake_case name
374
+ if (!isSnakeCase(objectName)) {
375
+ return JSON.stringify({ error: `Invalid object name "${objectName}". Must be snake_case.` });
376
+ }
377
+
378
+ const objectDef = await ctx.metadataService.getObject(objectName);
379
+ if (!objectDef) {
380
+ return JSON.stringify({ error: `Object "${objectName}" not found` });
381
+ }
382
+
383
+ const def = objectDef as ObjectDef;
384
+ const fields = def.fields ?? {};
385
+ const fieldSummary = Object.entries(fields).map(([key, f]) => ({
386
+ name: key,
387
+ type: f.type,
388
+ label: f.label ?? key,
389
+ required: f.required ?? false,
390
+ ...(f.reference ? { reference: f.reference } : {}),
391
+ ...(f.options ? { options: f.options } : {}),
392
+ }));
393
+
394
+ return JSON.stringify({
395
+ name: def.name,
396
+ label: def.label ?? def.name,
397
+ fields: fieldSummary,
398
+ enableFeatures: def.enable ?? {},
399
+ });
400
+ };
401
+ }
402
+
403
+ // ---------------------------------------------------------------------------
404
+ // Public Registration Helper
405
+ // ---------------------------------------------------------------------------
406
+
407
+ /**
408
+ * Register all built-in metadata management tools on the given {@link ToolRegistry}.
409
+ *
410
+ * Typically called from the `ai:ready` hook after the metadata service is available.
411
+ *
412
+ * @example
413
+ * ```ts
414
+ * ctx.hook('ai:ready', async (aiService) => {
415
+ * const metadataService = ctx.getService<IMetadataService>('metadata');
416
+ * registerMetadataTools(aiService.toolRegistry, { metadataService });
417
+ * });
418
+ * ```
419
+ */
420
+ export function registerMetadataTools(
421
+ registry: ToolRegistry,
422
+ context: MetadataToolContext,
423
+ ): void {
424
+ registry.register(createObjectTool, createCreateObjectHandler(context));
425
+ registry.register(addFieldTool, createAddFieldHandler(context));
426
+ registry.register(modifyFieldTool, createModifyFieldHandler(context));
427
+ registry.register(deleteFieldTool, createDeleteFieldHandler(context));
428
+ registry.register(listMetadataObjectsTool, createListObjectsHandler(context));
429
+ registry.register(describeMetadataObjectTool, createDescribeObjectHandler(context));
430
+ }
@@ -0,0 +1,44 @@
1
+ // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
2
+
3
+ import { defineTool } from '@objectstack/spec/ai';
4
+
5
+ /**
6
+ * modify_field — AI Tool Metadata
7
+ *
8
+ * Modifies an existing field definition (label, type, required, default value, etc.)
9
+ * on a data object. Does not support renaming the field.
10
+ */
11
+ export const modifyFieldTool = defineTool({
12
+ name: 'modify_field',
13
+ label: 'Modify Field',
14
+ description:
15
+ 'Modifies an existing field definition (label, type, required, default value, etc.) on a data object. ' +
16
+ 'Use this when the user wants to change or reconfigure an existing column or attribute (not rename it).',
17
+ category: 'data',
18
+ builtIn: true,
19
+ parameters: {
20
+ type: 'object',
21
+ properties: {
22
+ objectName: {
23
+ type: 'string',
24
+ description: 'Target object machine name (snake_case)',
25
+ },
26
+ fieldName: {
27
+ type: 'string',
28
+ description: 'Existing field machine name to modify (snake_case)',
29
+ },
30
+ changes: {
31
+ type: 'object',
32
+ description: 'Field properties to update (partial patch)',
33
+ properties: {
34
+ label: { type: 'string', description: 'New display label' },
35
+ type: { type: 'string', description: 'New field type' },
36
+ required: { type: 'boolean', description: 'Update required constraint' },
37
+ defaultValue: { description: 'New default value' },
38
+ },
39
+ },
40
+ },
41
+ required: ['objectName', 'fieldName', 'changes'],
42
+ additionalProperties: false,
43
+ },
44
+ });
@@ -1,6 +1,6 @@
1
1
  // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
2
2
 
3
- import type { AIToolDefinition, AIToolCall, AIToolResult } from '@objectstack/spec/contracts';
3
+ import type { AIToolDefinition, ToolCallPart, ToolResultPart } from '@objectstack/spec/contracts';
4
4
 
5
5
  /**
6
6
  * Handler function for a registered tool.
@@ -9,6 +9,14 @@ import type { AIToolDefinition, AIToolCall, AIToolResult } from '@objectstack/sp
9
9
  */
10
10
  export type ToolHandler = (args: Record<string, unknown>) => Promise<string> | string;
11
11
 
12
+ /**
13
+ * Extended ToolResultPart that carries an `isError` flag for internal
14
+ * error-tracking in the tool-call loop.
15
+ */
16
+ export interface ToolExecutionResult extends ToolResultPart {
17
+ isError?: boolean;
18
+ }
19
+
12
20
  /**
13
21
  * ToolRegistry — Central registry for AI-callable tools.
14
22
  *
@@ -72,30 +80,45 @@ export class ToolRegistry {
72
80
  /**
73
81
  * Execute a tool call and return the result.
74
82
  */
75
- async execute(toolCall: AIToolCall): Promise<AIToolResult> {
76
- const handler = this.handlers.get(toolCall.name);
83
+ async execute(toolCall: ToolCallPart): Promise<ToolExecutionResult> {
84
+ const handler = this.handlers.get(toolCall.toolName);
77
85
  if (!handler) {
78
86
  return {
79
- toolCallId: toolCall.id,
80
- content: `Tool "${toolCall.name}" is not registered`,
87
+ type: 'tool-result',
88
+ toolCallId: toolCall.toolCallId,
89
+ toolName: toolCall.toolName,
90
+ output: { type: 'text', value: `Tool "${toolCall.toolName}" is not registered` },
81
91
  isError: true,
82
92
  };
83
93
  }
84
94
 
85
95
  try {
86
- const args: Record<string, unknown> = JSON.parse(toolCall.arguments);
96
+ const args = typeof toolCall.input === 'string'
97
+ ? JSON.parse(toolCall.input)
98
+ : (toolCall.input as Record<string, unknown>) ?? {};
87
99
  const content = await handler(args);
88
- return { toolCallId: toolCall.id, content };
100
+ return {
101
+ type: 'tool-result',
102
+ toolCallId: toolCall.toolCallId,
103
+ toolName: toolCall.toolName,
104
+ output: { type: 'text', value: content },
105
+ };
89
106
  } catch (err) {
90
107
  const message = err instanceof Error ? err.message : String(err);
91
- return { toolCallId: toolCall.id, content: message, isError: true };
108
+ return {
109
+ type: 'tool-result',
110
+ toolCallId: toolCall.toolCallId,
111
+ toolName: toolCall.toolName,
112
+ output: { type: 'text', value: message },
113
+ isError: true,
114
+ };
92
115
  }
93
116
  }
94
117
 
95
118
  /**
96
119
  * Execute multiple tool calls in parallel.
97
120
  */
98
- async executeAll(toolCalls: AIToolCall[]): Promise<AIToolResult[]> {
121
+ async executeAll(toolCalls: ToolCallPart[]): Promise<ToolExecutionResult[]> {
99
122
  return Promise.all(toolCalls.map(tc => this.execute(tc)));
100
123
  }
101
124