-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathchatbot-features.test.ts
More file actions
1121 lines (972 loc) · 43.3 KB
/
Copy pathchatbot-features.test.ts
File metadata and controls
1121 lines (972 loc) · 43.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, vi, beforeEach } from 'vitest';
import type {
ModelMessage,
AIResult,
AIRequestOptions,
ToolCallPart,
AIToolDefinition,
IDataEngine,
IMetadataService,
LLMAdapter,
} from '@objectstack/spec/contracts';
import { AIService } from '../ai-service.js';
import { ToolRegistry } from '../tools/tool-registry.js';
import { registerDataTools, DATA_TOOL_DEFINITIONS } from '../tools/data-tools.js';
import type { DataToolContext } from '../tools/data-tools.js';
import { AgentRuntime } from '../agent-runtime.js';
import { SkillRegistry } from '../skill-registry.js';
import type { AgentChatContext } from '../agent-runtime.js';
import { buildAgentRoutes } from '../routes/agent-routes.js';
import { registerAgentAlias } from '../agents/agent-aliases.js';
import type { Agent } from '@objectstack/spec/ai';
// BOTH platform PERSONAS moved to the cloud-only @objectstack/service-ai-studio
// package (the `ask` data product + the `build` authoring agent are commercial
// features). The open-source AI runtime is headless. These local stubs stand in
// as the two PLATFORM agents for the runtime/route agent-listing tests below;
// they satisfy AgentSchema and carry just enough persona text for the generic
// `buildSystemMessages` assertions. The persona-as-subject specs (skill bundles,
// instruction wording, surface affinity) live with the agents in the cloud
// package now (see metadata-assistant-agent.test.ts / ask-agent.test.ts there).
//
// The `ask` stub's instructions intentionally include the "assistant for this
// business application platform" / "do NOT build" / "Builder" phrases the
// runtime tests assert on, so those tests still exercise buildSystemMessages
// without depending on the moved persona.
const ASK_AGENT: Agent = {
name: 'ask',
label: 'Assistant',
role: 'Business Application Assistant',
surface: 'ask',
instructions:
'You are the assistant for this business application platform. You help the ' +
'user explore their data. You do NOT build or change the application itself; ' +
'app-building lives in the Builder (the separate "build" experience).',
model: { provider: 'openai', model: 'gpt-4', temperature: 0.2, maxTokens: 4096 },
skills: ['schema_reader', 'data_explorer', 'actions_executor'],
active: true,
visibility: 'global',
guardrails: { maxTokensPerInvocation: 8192, maxExecutionTimeSec: 30, blockedTopics: ['raw_sql'] },
planning: { strategy: 'react', maxIterations: 10, allowReplan: true },
} as any;
// Registering the `metadata_assistant`→`build` alias (as the cloud plugin does
// at init) makes `build` a recognised platform agent so the runtime catalog
// surfaces it (ADR-0063 §2).
registerAgentAlias('metadata_assistant', 'build');
const BUILD_AGENT = {
...ASK_AGENT,
name: 'build',
label: 'Builder',
role: 'Schema Architect',
surface: 'build',
skills: [],
active: true,
visibility: 'global',
} as any;
// ── Helpers ────────────────────────────────────────────────────────
const silentLogger = {
info: vi.fn(),
debug: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
child: vi.fn().mockReturnThis(),
} as any;
/** Build a mock LLM adapter that returns sequential responses. */
function createMockAdapter(responses: AIResult[]): LLMAdapter {
let callIndex = 0;
return {
name: 'mock',
chat: vi.fn(async () => responses[callIndex++] ?? { content: 'done' }),
complete: vi.fn(async () => ({ content: '' })),
};
}
/** Build a mock IDataEngine. */
function createMockDataEngine(overrides: Partial<IDataEngine> = {}): IDataEngine {
return {
find: vi.fn(async () => []),
findOne: vi.fn(async () => null),
insert: vi.fn(async () => ({})),
update: vi.fn(async () => ({})),
delete: vi.fn(async () => ({})),
count: vi.fn(async () => 0),
aggregate: vi.fn(async () => []),
...overrides,
};
}
/** Build a mock IMetadataService. */
function createMockMetadataService(overrides: Partial<IMetadataService> = {}): IMetadataService {
return {
register: vi.fn(async () => {}),
get: vi.fn(async () => undefined),
list: vi.fn(async () => []),
unregister: vi.fn(async () => {}),
exists: vi.fn(async () => false),
listNames: vi.fn(async () => []),
getObject: vi.fn(async () => undefined),
listObjects: vi.fn(async () => []),
...overrides,
};
}
// ═══════════════════════════════════════════════════════════════════
// chatWithTools — Tool Call Loop
// ═══════════════════════════════════════════════════════════════════
describe('AIService.chatWithTools', () => {
let registry: ToolRegistry;
beforeEach(() => {
registry = new ToolRegistry();
registry.register(
{ name: 'get_weather', description: 'Get weather', parameters: {} },
async (args) => JSON.stringify({ temp: 22, city: args.city }),
);
});
it('should return immediately if no tool calls in response', async () => {
const adapter = createMockAdapter([{ content: 'Hello there!' }]);
const service = new AIService({ adapter, logger: silentLogger, toolRegistry: registry });
const result = await service.chatWithTools([{ role: 'user', content: 'Hi' }]);
expect(result.content).toBe('Hello there!');
expect(adapter.chat).toHaveBeenCalledTimes(1);
});
it('should auto-inject registered tools into options', async () => {
const adapter = createMockAdapter([{ content: 'No tools needed' }]);
const service = new AIService({ adapter, logger: silentLogger, toolRegistry: registry });
await service.chatWithTools([{ role: 'user', content: 'Hi' }]);
const callArgs = (adapter.chat as any).mock.calls[0];
const options = callArgs[1] as AIRequestOptions;
expect(options.tools).toHaveLength(1);
expect(options.tools![0].name).toBe('get_weather');
expect(options.toolChoice).toBe('auto');
});
it('should execute tool calls and loop until final text response', async () => {
const toolCall: ToolCallPart = {
type: 'tool-call' as const,
toolCallId: 'call_1',
toolName: 'get_weather',
input: { city: 'Tokyo' },
};
const adapter = createMockAdapter([
// First response: model requests a tool call
{ content: '', toolCalls: [toolCall] },
// Second response: final text after receiving tool result
{ content: 'The weather in Tokyo is 22°C.' },
]);
const service = new AIService({ adapter, logger: silentLogger, toolRegistry: registry });
const result = await service.chatWithTools([
{ role: 'user', content: "What's the weather in Tokyo?" },
]);
expect(result.content).toBe('The weather in Tokyo is 22°C.');
expect(adapter.chat).toHaveBeenCalledTimes(2);
// Verify the second call includes the tool result message
const secondCallMessages = (adapter.chat as any).mock.calls[1][0] as ModelMessage[];
expect(secondCallMessages).toHaveLength(3); // user + assistant(tool_call) + tool(result)
expect(secondCallMessages[1].role).toBe('assistant');
const assistantContent = secondCallMessages[1].content as any[];
const toolCallParts = assistantContent.filter((p: any) => p.type === 'tool-call');
expect(toolCallParts).toEqual([toolCall]);
expect(secondCallMessages[2].role).toBe('tool');
const toolResultContent = secondCallMessages[2].content as any[];
expect(toolResultContent[0].toolCallId).toBe('call_1');
expect(toolResultContent[0].output.value).toContain('"temp":22');
});
it('should handle multiple sequential tool calls', async () => {
registry.register(
{ name: 'get_time', description: 'Get time', parameters: {} },
async () => JSON.stringify({ time: '14:30' }),
);
const adapter = createMockAdapter([
// Round 1: call get_weather
{ content: '', toolCalls: [{ type: 'tool-call' as const, toolCallId: 'c1', toolName: 'get_weather', input: { city: 'NYC' } }] },
// Round 2: call get_time
{ content: '', toolCalls: [{ type: 'tool-call' as const, toolCallId: 'c2', toolName: 'get_time', input: {} }] },
// Round 3: final response
{ content: 'NYC: 22°C at 14:30' },
]);
const service = new AIService({ adapter, logger: silentLogger, toolRegistry: registry });
const result = await service.chatWithTools([{ role: 'user', content: 'Weather and time?' }]);
expect(result.content).toBe('NYC: 22°C at 14:30');
expect(adapter.chat).toHaveBeenCalledTimes(3);
});
it('should handle parallel tool calls in a single response', async () => {
registry.register(
{ name: 'get_population', description: 'Population', parameters: {} },
async (args) => JSON.stringify({ pop: 1000000, city: args.city }),
);
const adapter = createMockAdapter([
// Model calls two tools at once
{
content: '',
toolCalls: [
{ type: 'tool-call' as const, toolCallId: 'c1', toolName: 'get_weather', input: { city: 'London' } },
{ type: 'tool-call' as const, toolCallId: 'c2', toolName: 'get_population', input: { city: 'London' } },
],
},
// Final response with both results
{ content: 'London: 22°C, pop 1M' },
]);
const service = new AIService({ adapter, logger: silentLogger, toolRegistry: registry });
const result = await service.chatWithTools([{ role: 'user', content: 'London stats?' }]);
expect(result.content).toBe('London: 22°C, pop 1M');
// Both tool results should be in the conversation
const secondCallMessages = (adapter.chat as any).mock.calls[1][0] as ModelMessage[];
const toolMessages = secondCallMessages.filter(m => m.role === 'tool');
expect(toolMessages).toHaveLength(2);
});
it('should respect maxIterations and force final response', async () => {
// Adapter always returns tool calls — would loop forever
const infiniteToolCall: AIResult = {
content: '',
toolCalls: [{ type: 'tool-call' as const, toolCallId: 'c', toolName: 'get_weather', input: { city: 'X' } }],
};
const adapter = createMockAdapter(
Array(5).fill(infiniteToolCall).concat([{ content: 'Forced stop' }]),
);
const service = new AIService({ adapter, logger: silentLogger, toolRegistry: registry });
const result = await service.chatWithTools(
[{ role: 'user', content: 'Loop me' }],
{ maxIterations: 3 },
);
// 3 iterations + 1 final forced call = 4 total
expect(adapter.chat).toHaveBeenCalledTimes(4);
// The forced final call should NOT have tools in options
const lastCallOptions = (adapter.chat as any).mock.calls[3][1] as AIRequestOptions;
expect(lastCallOptions.tools).toBeUndefined();
});
it('should merge explicit tools with registered tools', async () => {
const explicitTool: AIToolDefinition = {
name: 'custom_tool',
description: 'Custom',
parameters: {},
};
const adapter = createMockAdapter([{ content: 'ok' }]);
const service = new AIService({ adapter, logger: silentLogger, toolRegistry: registry });
await service.chatWithTools(
[{ role: 'user', content: 'test' }],
{ tools: [explicitTool] },
);
const options = (adapter.chat as any).mock.calls[0][1] as AIRequestOptions;
expect(options.tools).toHaveLength(2); // get_weather + custom_tool
expect(options.tools!.map(t => t.name)).toContain('get_weather');
expect(options.tools!.map(t => t.name)).toContain('custom_tool');
});
it('should handle tool execution errors gracefully', async () => {
registry.register(
{ name: 'bad_tool', description: 'Breaks', parameters: {} },
async () => { throw new Error('Tool crashed'); },
);
const adapter = createMockAdapter([
{ content: '', toolCalls: [{ type: 'tool-call' as const, toolCallId: 'c1', toolName: 'bad_tool', input: {} }] },
{ content: 'I see the tool failed' },
]);
const service = new AIService({ adapter, logger: silentLogger, toolRegistry: registry });
const result = await service.chatWithTools([{ role: 'user', content: 'Use bad tool' }]);
expect(result.content).toBe('I see the tool failed');
// The error message should be in the tool result
const secondCallMessages = (adapter.chat as any).mock.calls[1][0] as ModelMessage[];
const toolMsg = secondCallMessages.find(m => m.role === 'tool');
let toolContent: string | undefined;
if (toolMsg?.role === 'tool' && Array.isArray(toolMsg.content)) {
const firstResult = toolMsg.content[0];
if ('output' in firstResult && firstResult.output && typeof firstResult.output === 'object' && 'value' in firstResult.output) {
toolContent = String(firstResult.output.value);
}
}
expect(toolContent).toContain('Tool crashed');
});
it('should work with no registered tools', async () => {
const emptyRegistry = new ToolRegistry();
const adapter = createMockAdapter([{ content: 'No tools available' }]);
const service = new AIService({ adapter, logger: silentLogger, toolRegistry: emptyRegistry });
const result = await service.chatWithTools([{ role: 'user', content: 'Hi' }]);
expect(result.content).toBe('No tools available');
const options = (adapter.chat as any).mock.calls[0][1] as AIRequestOptions;
expect(options.tools).toBeUndefined();
});
it('should not pass maxIterations to adapter options', async () => {
const adapter = createMockAdapter([{ content: 'ok' }]);
const service = new AIService({ adapter, logger: silentLogger, toolRegistry: registry });
await service.chatWithTools(
[{ role: 'user', content: 'test' }],
{ maxIterations: 5, model: 'gpt-4' },
);
const callArgs = (adapter.chat as any).mock.calls[0];
const options = callArgs[1];
expect(options).not.toHaveProperty('maxIterations');
expect(options.model).toBe('gpt-4');
});
});
// ═══════════════════════════════════════════════════════════════════
// Data Tools
// ═══════════════════════════════════════════════════════════════════
describe('Data Tools', () => {
describe('DATA_TOOL_DEFINITIONS', () => {
it('should define exactly 3 tools', () => {
expect(DATA_TOOL_DEFINITIONS).toHaveLength(3);
});
it('should include all expected tool names', () => {
const names = DATA_TOOL_DEFINITIONS.map(t => t.name);
expect(names).toEqual([
'query_records',
'get_record',
'aggregate_data',
]);
});
it('should have descriptions and parameters for each tool', () => {
for (const def of DATA_TOOL_DEFINITIONS) {
expect(def.description).toBeTruthy();
expect(def.parameters).toBeDefined();
}
});
});
describe('registerDataTools', () => {
let registry: ToolRegistry;
let dataEngine: IDataEngine;
let metadataService: IMetadataService;
beforeEach(() => {
registry = new ToolRegistry();
dataEngine = createMockDataEngine();
metadataService = createMockMetadataService();
registerDataTools(registry, { dataEngine });
});
it('should register all 3 tools', () => {
expect(registry.size).toBe(3);
expect(registry.has('query_records')).toBe(true);
expect(registry.has('get_record')).toBe(true);
expect(registry.has('aggregate_data')).toBe(true);
});
// NOTE: list_objects / describe_object are part of the metadata authoring
// tools, which moved to the cloud-only @objectstack/service-ai-studio
// package. Their tests live there now (see metadata-tools.test.ts).
it('query_records should call dataEngine.find with correct params', async () => {
const records = [{ id: '1', name: 'Acme' }, { id: '2', name: 'Beta' }];
(dataEngine.find as any).mockResolvedValue(records);
const result = await registry.execute({
type: 'tool-call' as const,
toolCallId: 'c1',
toolName: 'query_records',
input: {
objectName: 'account',
where: { status: 'active' },
fields: ['name', 'status'],
limit: 10,
},
});
expect(dataEngine.find).toHaveBeenCalledWith('account', {
where: { status: 'active' },
fields: ['name', 'status'],
orderBy: undefined,
limit: 10,
offset: undefined,
context: { roles: [], permissions: [], isSystem: true },
});
const parsed = JSON.parse((result.output as any).value);
expect(parsed.count).toBe(2);
expect(parsed.records).toEqual(records);
});
it('query_records should cap limit at 200', async () => {
(dataEngine.find as any).mockResolvedValue([]);
await registry.execute({
type: 'tool-call' as const,
toolCallId: 'c1',
toolName: 'query_records',
input: { objectName: 'account', limit: 999 },
});
expect(dataEngine.find).toHaveBeenCalledWith('account', expect.objectContaining({
limit: 200,
}));
});
it('query_records should use default limit of 20', async () => {
(dataEngine.find as any).mockResolvedValue([]);
await registry.execute({
type: 'tool-call' as const,
toolCallId: 'c1',
toolName: 'query_records',
input: { objectName: 'account' },
});
expect(dataEngine.find).toHaveBeenCalledWith('account', expect.objectContaining({
limit: 20,
}));
});
it('get_record should call findOne with where id filter', async () => {
const record = { id: 'rec_123', name: 'Acme Corp' };
(dataEngine.findOne as any).mockResolvedValue(record);
const result = await registry.execute({
type: 'tool-call' as const,
toolCallId: 'c1',
toolName: 'get_record',
input: { objectName: 'account', recordId: 'rec_123' },
});
expect(dataEngine.findOne).toHaveBeenCalledWith('account', {
where: { id: 'rec_123' },
fields: undefined,
context: { roles: [], permissions: [], isSystem: true },
});
const parsed = JSON.parse((result.output as any).value);
expect(parsed.name).toBe('Acme Corp');
});
it('get_record should return error for missing record', async () => {
const result = await registry.execute({
type: 'tool-call' as const,
toolCallId: 'c1',
toolName: 'get_record',
input: { objectName: 'account', recordId: 'not_found' },
});
const parsed = JSON.parse((result.output as any).value);
expect(parsed.error).toContain('not found');
});
it('aggregate_data should call dataEngine.aggregate', async () => {
const aggResult = [{ total_revenue: 1000000 }];
(dataEngine.aggregate as any).mockResolvedValue(aggResult);
const result = await registry.execute({
type: 'tool-call' as const,
toolCallId: 'c1',
toolName: 'aggregate_data',
input: {
objectName: 'account',
aggregations: [{ function: 'sum', field: 'revenue', alias: 'total_revenue' }],
where: { status: 'active' },
},
});
expect(dataEngine.aggregate).toHaveBeenCalledWith('account', {
where: { status: 'active' },
groupBy: undefined,
aggregations: [{ function: 'sum', field: 'revenue', alias: 'total_revenue' }],
context: { roles: [], permissions: [], isSystem: true },
});
const parsed = JSON.parse((result.output as any).value);
expect(parsed).toEqual(aggResult);
});
it('aggregate_data should reject invalid aggregation functions', async () => {
const result = await registry.execute({
type: 'tool-call' as const,
toolCallId: 'c1',
toolName: 'aggregate_data',
input: {
objectName: 'account',
aggregations: [{ function: 'drop_table', field: 'id', alias: 'x' }],
},
});
const parsed = JSON.parse((result.output as any).value);
expect(parsed.error).toContain('Invalid aggregation function');
expect(parsed.error).toContain('drop_table');
expect(dataEngine.aggregate).not.toHaveBeenCalled();
});
it('query_records should clamp negative limit to default', async () => {
(dataEngine.find as any).mockResolvedValue([]);
await registry.execute({
type: 'tool-call' as const,
toolCallId: 'c1',
toolName: 'query_records',
input: { objectName: 'account', limit: -5 },
});
expect(dataEngine.find).toHaveBeenCalledWith('account', expect.objectContaining({
limit: 20, // DEFAULT_QUERY_LIMIT
}));
});
it('query_records should clamp NaN limit to default', async () => {
(dataEngine.find as any).mockResolvedValue([]);
await registry.execute({
type: 'tool-call' as const,
toolCallId: 'c1',
toolName: 'query_records',
input: { objectName: 'account', limit: 'not_a_number' },
});
expect(dataEngine.find).toHaveBeenCalledWith('account', expect.objectContaining({
limit: 20,
}));
});
it('query_records should ignore negative offset', async () => {
(dataEngine.find as any).mockResolvedValue([]);
await registry.execute({
type: 'tool-call' as const,
toolCallId: 'c1',
toolName: 'query_records',
input: { objectName: 'account', offset: -10 },
});
expect(dataEngine.find).toHaveBeenCalledWith('account', expect.objectContaining({
offset: undefined,
}));
});
});
});
// ═══════════════════════════════════════════════════════════════════
// Agent Runtime
// ═══════════════════════════════════════════════════════════════════
describe('AgentRuntime', () => {
let metadataService: IMetadataService;
let runtime: AgentRuntime;
beforeEach(() => {
metadataService = createMockMetadataService();
const skillRegistry = new SkillRegistry(metadataService);
runtime = new AgentRuntime(metadataService, skillRegistry);
});
describe('loadAgent', () => {
it('should return agent definition from metadata service (legacy name resolves via alias)', async () => {
(metadataService.get as any).mockResolvedValue(ASK_AGENT);
const agent = await runtime.loadAgent('data_chat');
// Path A: `data_chat` is an alias for the renamed `ask` agent.
expect(metadataService.get).toHaveBeenCalledWith('agent', 'ask');
expect(agent?.name).toBe('ask');
expect(agent?.role).toBe('Business Application Assistant');
});
it('should return undefined for unknown agent', async () => {
const agent = await runtime.loadAgent('nonexistent');
expect(agent).toBeUndefined();
});
it('should return undefined for malformed agent metadata', async () => {
// Missing required fields: role, instructions
(metadataService.get as any).mockResolvedValue({ name: 'bad_agent', label: 'Bad' });
const agent = await runtime.loadAgent('bad_agent');
expect(agent).toBeUndefined();
});
});
describe('buildSystemMessages', () => {
it('should create system message from agent instructions', () => {
const messages = runtime.buildSystemMessages(ASK_AGENT);
expect(messages).toHaveLength(1);
expect(messages[0].role).toBe('system');
expect(messages[0].content).toContain('assistant for this business application platform');
});
it('should include context when provided', () => {
const context: AgentChatContext = {
objectName: 'account',
recordId: 'rec_123',
viewName: 'all_accounts',
};
const messages = runtime.buildSystemMessages(ASK_AGENT, context);
expect(messages[0].content).toContain('Current object: account');
expect(messages[0].content).toContain('Selected record ID: rec_123');
expect(messages[0].content).toContain('Current view: all_accounts');
});
it('should not include context section when no context fields set', () => {
const messages = runtime.buildSystemMessages(ASK_AGENT, {});
expect(messages[0].content).not.toContain('Current Context');
});
it('tells the agent builds are live when the environment auto-publishes', () => {
const messages = runtime.buildSystemMessages(ASK_AGENT, { autoPublishAiBuilds: true });
expect(messages[0].content).toContain('publish AUTOMATICALLY');
expect(messages[0].content).toContain('is live');
});
it('stays silent on publishing when auto-publish is off/absent', () => {
for (const ctx of [{}, { autoPublishAiBuilds: false }]) {
const messages = runtime.buildSystemMessages(ASK_AGENT, ctx);
expect(messages[0].content).not.toContain('publish AUTOMATICALLY');
}
});
it('injects no runtime capability-gating block (ADR-0063: surfaces are separated)', () => {
// The per-deployment `buildRegisterActive` shim was removed: `ask` never
// advertises authoring, so there is nothing to walk back at runtime. The
// decline-to-build guidance lives in the agent's own persona, not in an
// injected capability block keyed off skill presence.
const messages = runtime.buildSystemMessages(ASK_AGENT, undefined, [
{ name: 'data_explorer', label: 'Data Explorer', tools: [] } as never,
]);
expect(messages[0].content).not.toContain('BUILDING / AUTHORING is NOT available');
expect(messages[0].content).not.toContain('Capabilities in this deployment');
});
});
describe('buildContextSchemaMessages', () => {
it('injects the current object schema so "this object" resolves without a tool', async () => {
(metadataService.getObject as any).mockResolvedValue({
name: 'showcase_task',
label: 'Task',
fields: { id: { type: 'text' }, subject: { type: 'text', label: 'Subject' } },
});
const messages = await runtime.buildContextSchemaMessages({ objectName: 'showcase_task' });
expect(metadataService.getObject).toHaveBeenCalledWith('showcase_task');
expect(messages).toHaveLength(1);
expect(messages[0].role).toBe('system');
expect(messages[0].content).toContain('currently viewing the "showcase_task" object');
expect(messages[0].content).toContain('### showcase_task');
expect(messages[0].content).toContain('subject: text');
});
it('returns nothing when no object is in context', async () => {
expect(await runtime.buildContextSchemaMessages(undefined)).toEqual([]);
expect(await runtime.buildContextSchemaMessages({})).toEqual([]);
});
it('returns nothing when the object cannot be resolved', async () => {
(metadataService.getObject as any).mockResolvedValue(undefined);
expect(await runtime.buildContextSchemaMessages({ objectName: 'nope' })).toEqual([]);
});
it('degrades gracefully when metadata lookup throws', async () => {
(metadataService.getObject as any).mockRejectedValue(new Error('boom'));
expect(await runtime.buildContextSchemaMessages({ objectName: 'showcase_task' })).toEqual([]);
});
});
describe('buildRequestOptions', () => {
it('should derive model config from agent', () => {
const options = runtime.buildRequestOptions(ASK_AGENT, []);
expect(options.model).toBe('gpt-4');
expect(options.temperature).toBe(0.2);
expect(options.maxTokens).toBe(4096);
});
it('should resolve skill tool references against available tools', async () => {
const availableTools: AIToolDefinition[] = [
{ name: 'list_objects', description: 'List objects', parameters: {} },
{ name: 'query_records', description: 'Query records', parameters: {} },
{ name: 'unrelated_tool', description: 'Not in any skill', parameters: {} },
];
// Mechanism test: the resolved tool set is the union of the active skills'
// claimed tools. `schema_reader` stays open in this package; `data_explorer`
// moved to the cloud studio package, so we stub it inline here — the runtime
// resolver doesn't care WHERE a skill is defined, only what tools it claims.
const { SCHEMA_READER_SKILL } = await import('../skills/schema-reader-skill.js');
const dataExplorerStub = {
name: 'data_explorer',
label: 'Data Explorer',
surface: 'ask',
tools: ['query_records', 'get_record', 'aggregate_data', 'visualize_data'],
active: true,
} as never;
const options = runtime.buildRequestOptions(ASK_AGENT, availableTools, [SCHEMA_READER_SKILL, dataExplorerStub]);
const resolvedNames = options.tools?.map(t => t.name) ?? [];
expect(resolvedNames).toContain('list_objects');
expect(resolvedNames).toContain('query_records');
expect(resolvedNames).not.toContain('unrelated_tool');
});
it('should handle agent with no tools', () => {
const agent = { ...ASK_AGENT, tools: undefined };
const options = runtime.buildRequestOptions(agent, []);
expect(options.tools).toBeUndefined();
});
it('should handle agent with no model config', () => {
const agent = { ...ASK_AGENT, model: undefined };
const options = runtime.buildRequestOptions(agent, []);
expect(options.model).toBeUndefined();
});
});
describe('listAgents', () => {
it('should return summaries of all active agents', async () => {
(metadataService.list as any).mockResolvedValue([
ASK_AGENT,
BUILD_AGENT,
]);
const agents = await runtime.listAgents();
expect(agents).toHaveLength(2);
expect(agents[0]).toEqual({ name: 'ask', label: 'Assistant', role: 'Business Application Assistant' });
expect(agents[1]).toEqual({ name: 'build', label: 'Builder', role: 'Schema Architect' });
});
it('should filter out inactive agents', async () => {
(metadataService.list as any).mockResolvedValue([
ASK_AGENT,
{ ...BUILD_AGENT, active: false },
]);
const agents = await runtime.listAgents();
expect(agents).toHaveLength(1);
expect(agents[0].name).toBe('ask');
});
it('should return empty array when no agents registered', async () => {
(metadataService.list as any).mockResolvedValue([]);
const agents = await runtime.listAgents();
expect(agents).toEqual([]);
});
it('should skip malformed agent metadata', async () => {
(metadataService.list as any).mockResolvedValue([
ASK_AGENT,
{ name: 'bad', label: 'Bad' }, // missing required fields
]);
const agents = await runtime.listAgents();
expect(agents).toHaveLength(1);
expect(agents[0].name).toBe('ask');
});
// ── Catalog membership is decoupled from the in-memory alias table ──────
// A platform agent must surface from an INTRINSIC, persisted signal so a
// missed `registerAgentAlias` call (bundle load ordering) never hides it.
it('surfaces a platform agent carrying the `_provenance:"package"` envelope even when its name is NOT alias-registered', async () => {
const unaliased = { ...BUILD_AGENT, name: 'buildx', _provenance: 'package' };
(metadataService.list as any).mockResolvedValue([unaliased]);
const agents = await runtime.listAgents();
expect(agents.map((a) => a.name)).toContain('buildx');
});
it('surfaces an agent carrying a `_lock` envelope when not alias-registered', async () => {
const locked = { ...BUILD_AGENT, name: 'buildz', _lock: 'full' };
(metadataService.list as any).mockResolvedValue([locked]);
const agents = await runtime.listAgents();
expect(agents.map((a) => a.name)).toContain('buildz');
});
it('surfaces an agent carrying the pre-translation `protection` block when not alias-registered', async () => {
const withBlock = { ...BUILD_AGENT, name: 'buildy', protection: { lock: 'full', reason: 'x' } };
(metadataService.list as any).mockResolvedValue([withBlock]);
const agents = await runtime.listAgents();
expect(agents.map((a) => a.name)).toContain('buildy');
});
it('hides a stray tenant custom agent (no platform envelope, not alias-registered)', async () => {
const { protection: _omit, ...buildNoProtection } = BUILD_AGENT;
const tenant = { ...buildNoProtection, name: 'tenant_custom_agent' };
(metadataService.list as any).mockResolvedValue([ASK_AGENT, tenant]);
const agents = await runtime.listAgents();
expect(agents.map((a) => a.name)).toEqual(['ask']);
});
});
});
// ═══════════════════════════════════════════════════════════════════
// Agent Routes
// ═══════════════════════════════════════════════════════════════════
describe('Agent Routes', () => {
let aiService: AIService;
let metadataService: IMetadataService;
let runtime: AgentRuntime;
let routes: ReturnType<typeof buildAgentRoutes>;
beforeEach(() => {
const registry = new ToolRegistry();
const adapter = createMockAdapter([{ content: 'Agent response' }]);
aiService = new AIService({ adapter, logger: silentLogger, toolRegistry: registry });
metadataService = createMockMetadataService({
get: vi.fn(async (_type, name) => {
// Canonical name after Path A rename; `data_chat` resolves here via alias.
if (name === 'ask') return ASK_AGENT;
if (name === 'inactive_agent') return { ...ASK_AGENT, name: 'inactive_agent', active: false };
return undefined;
}),
list: vi.fn(async () => [ASK_AGENT, BUILD_AGENT]),
});
runtime = new AgentRuntime(metadataService);
routes = buildAgentRoutes(aiService, runtime, silentLogger);
});
it('should define a GET list route and a POST chat route', () => {
expect(routes).toHaveLength(2);
expect(routes[0].method).toBe('GET');
expect(routes[0].path).toBe('/api/v1/ai/agents');
expect(routes[1].method).toBe('POST');
expect(routes[1].path).toBe('/api/v1/ai/agents/:agentName/chat');
});
// ── GET /api/v1/ai/agents ──
it('should return list of active agents', async () => {
const listRoute = routes.find(r => r.method === 'GET')!;
const resp = await listRoute.handler({});
expect(resp.status).toBe(200);
const body = resp.body as { agents: Array<{ name: string; label: string; role: string }> };
expect(body.agents).toHaveLength(2);
expect(body.agents[0].name).toBe('ask');
expect(body.agents[1].name).toBe('build');
});
// ── POST /api/v1/ai/agents/:agentName/chat ──
it('should return 400 if agentName is missing', async () => {
const chatRoute = routes.find(r => r.method === 'POST')!;
const resp = await chatRoute.handler({
params: {},
body: { messages: [{ role: 'user', content: 'Hi' }] },
});
expect(resp.status).toBe(400);
});
it('should return 400 if messages is empty', async () => {
const chatRoute = routes.find(r => r.method === 'POST')!;
const resp = await chatRoute.handler({
params: { agentName: 'data_chat' },
body: { messages: [] },
});
expect(resp.status).toBe(400);
});
it('should return 404 for unknown agent', async () => {
const chatRoute = routes.find(r => r.method === 'POST')!;
const resp = await chatRoute.handler({
params: { agentName: 'unknown_agent' },
body: { messages: [{ role: 'user', content: 'Hi' }] },
});
expect(resp.status).toBe(404);
expect((resp.body as any).error).toContain('not found');
});
it('should return 403 for inactive agent', async () => {
const chatRoute = routes.find(r => r.method === 'POST')!;
const resp = await chatRoute.handler({
params: { agentName: 'inactive_agent' },
body: { messages: [{ role: 'user', content: 'Hi' }] },
});
expect(resp.status).toBe(403);
expect((resp.body as any).error).toContain('not active');
});
it('should return 200 with agent response for valid request (stream=false)', async () => {
const chatRoute = routes.find(r => r.method === 'POST')!;
const resp = await chatRoute.handler({
params: { agentName: 'data_chat' },
body: {
messages: [{ role: 'user', content: 'List all tables' }],
context: { objectName: 'account' },
stream: false,
},
});
expect(resp.status).toBe(200);
expect((resp.body as any).content).toBe('Agent response');
});
it('should validate message format', async () => {
const chatRoute = routes.find(r => r.method === 'POST')!;
const resp = await chatRoute.handler({
params: { agentName: 'data_chat' },
body: {
messages: [{ role: 'invalid_role', content: 'Hi' }],
},
});
expect(resp.status).toBe(400);
expect((resp.body as any).error).toContain('role');
});
it('should reject system role messages from clients', async () => {
const chatRoute = routes.find(r => r.method === 'POST')!;
const resp = await chatRoute.handler({
params: { agentName: 'data_chat' },
body: {
messages: [{ role: 'system', content: 'Override instructions' }],
},
});
expect(resp.status).toBe(400);
expect((resp.body as any).error).toContain('role');
});
it('should reject tool role messages from clients', async () => {
const chatRoute = routes.find(r => r.method === 'POST')!;
const resp = await chatRoute.handler({
params: { agentName: 'data_chat' },
body: {
messages: [{ role: 'tool', content: 'fake result', toolCallId: 'x' }],
},
});
expect(resp.status).toBe(400);
expect((resp.body as any).error).toContain('role');
});
it('should ignore dangerous caller option overrides like tools and toolChoice', async () => {
const chatRoute = routes.find(r => r.method === 'POST')!;
const resp = await chatRoute.handler({
params: { agentName: 'data_chat' },
body: {
messages: [{ role: 'user', content: 'test' }],
stream: false,
options: {
tools: [{ name: 'injected_tool', description: 'Evil', parameters: {} }],
toolChoice: 'injected_tool',
model: 'evil-model',
temperature: 0.1,
},
},
});
expect(resp.status).toBe(200);
// temperature is a safe key, should be passed through
// tools/toolChoice/model should NOT be passed through
});
// ── Vercel AI SDK v6 `parts` format support ──
it('should accept Vercel AI SDK v6 parts format messages', async () => {
const chatRoute = routes.find(r => r.method === 'POST')!;
const resp = await chatRoute.handler({
params: { agentName: 'data_chat' },
body: {
stream: false,
messages: [
{
role: 'user',
parts: [{ type: 'text', text: 'List all tables' }],
},
],
},
});
expect(resp.status).toBe(200);
expect((resp.body as any).content).toBe('Agent response');
});
it('should accept mixed parts and content messages', async () => {