@mohanscodex/spectra-agent 0.4.6 → 0.4.9
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/__tests__/agent-edge-cases.test.d.ts +2 -0
- package/dist/__tests__/agent-edge-cases.test.d.ts.map +1 -0
- package/dist/__tests__/agent-edge-cases.test.js +302 -0
- package/dist/__tests__/agent-edge-cases.test.js.map +1 -0
- package/dist/__tests__/agent-features.test.js +42 -42
- package/dist/__tests__/agent-features.test.js.map +1 -1
- package/dist/__tests__/agent.test.js +43 -38
- package/dist/__tests__/agent.test.js.map +1 -1
- package/dist/__tests__/e2e.test.js +263 -282
- package/dist/__tests__/e2e.test.js.map +1 -1
- package/dist/agent.d.ts +5 -3
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +139 -83
- package/dist/agent.js.map +1 -1
- package/dist/define-tool.d.ts +2 -2
- package/dist/define-tool.d.ts.map +1 -1
- package/dist/define-tool.js +4 -4
- package/dist/define-tool.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/types.d.ts +23 -13
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
|
@@ -1,87 +1,92 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from
|
|
2
|
-
import { Agent } from
|
|
3
|
-
import { defineTool } from
|
|
4
|
-
import { z } from
|
|
5
|
-
const testModel = {
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { Agent } from '../agent.js';
|
|
3
|
+
import { defineTool } from '../define-tool.js';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
const testModel = {
|
|
6
|
+
id: 'claude-sonnet-4-20250514',
|
|
7
|
+
name: 'Claude Sonnet 4',
|
|
8
|
+
provider: 'anthropic',
|
|
9
|
+
api: 'anthropic-messages',
|
|
10
|
+
};
|
|
11
|
+
describe('Agent', () => {
|
|
12
|
+
it('should create agent instance', () => {
|
|
8
13
|
const agent = new Agent({
|
|
9
14
|
model: testModel,
|
|
10
|
-
systemPrompt:
|
|
15
|
+
systemPrompt: 'You are a helpful assistant.',
|
|
11
16
|
});
|
|
12
17
|
expect(agent).toBeDefined();
|
|
13
18
|
});
|
|
14
|
-
it(
|
|
19
|
+
it('should store messages', () => {
|
|
15
20
|
const agent = new Agent({ model: testModel });
|
|
16
21
|
expect(agent.messages).toEqual([]);
|
|
17
22
|
});
|
|
18
|
-
it(
|
|
23
|
+
it('should register tools', () => {
|
|
19
24
|
const agent = new Agent({ model: testModel });
|
|
20
25
|
const tool = defineTool({
|
|
21
|
-
name:
|
|
22
|
-
description:
|
|
26
|
+
name: 'get_weather',
|
|
27
|
+
description: 'Get the current weather',
|
|
23
28
|
parameters: z.object({
|
|
24
|
-
location: z.string().describe(
|
|
29
|
+
location: z.string().describe('The location to get weather for'),
|
|
25
30
|
}),
|
|
26
31
|
execute: async (args, context) => {
|
|
27
32
|
return {
|
|
28
|
-
content: [{ type:
|
|
33
|
+
content: [{ type: 'text', text: `The weather in ${args.location} is sunny.` }],
|
|
29
34
|
};
|
|
30
35
|
},
|
|
31
36
|
});
|
|
32
37
|
agent.registerTool(tool);
|
|
33
38
|
expect(agent.messages).toEqual([]);
|
|
34
39
|
});
|
|
35
|
-
it(
|
|
40
|
+
it('should subscribe and unsubscribe listeners', () => {
|
|
36
41
|
const agent = new Agent({ model: testModel });
|
|
37
42
|
const listener = vi.fn();
|
|
38
43
|
const unsubscribe = agent.subscribe(listener);
|
|
39
44
|
expect(unsubscribe).toBeDefined();
|
|
40
45
|
unsubscribe();
|
|
41
46
|
});
|
|
42
|
-
it(
|
|
47
|
+
it('should abort request', () => {
|
|
43
48
|
const agent = new Agent({ model: testModel });
|
|
44
49
|
expect(() => agent.abort()).not.toThrow();
|
|
45
50
|
});
|
|
46
|
-
it(
|
|
51
|
+
it('should reset state', () => {
|
|
47
52
|
const agent = new Agent({ model: testModel });
|
|
48
53
|
agent.reset();
|
|
49
54
|
expect(agent.messages).toEqual([]);
|
|
50
55
|
expect(agent.isStreaming).toBe(false);
|
|
51
56
|
});
|
|
52
|
-
it(
|
|
57
|
+
it('should return signal', () => {
|
|
53
58
|
const agent = new Agent({ model: testModel });
|
|
54
59
|
const signal = agent.signal;
|
|
55
60
|
expect(signal).toBeUndefined();
|
|
56
61
|
});
|
|
57
62
|
});
|
|
58
|
-
describe(
|
|
59
|
-
it(
|
|
63
|
+
describe('Agent Queues and Hooks', () => {
|
|
64
|
+
it('should add steering messages to queue', () => {
|
|
60
65
|
const agent = new Agent({ model: testModel });
|
|
61
|
-
agent.steer(
|
|
66
|
+
agent.steer('Please be more concise');
|
|
62
67
|
// Steering message should be queued (internal state)
|
|
63
68
|
// We can't directly check the queue, but we can verify it doesn't throw
|
|
64
|
-
expect(() => agent.steer(
|
|
69
|
+
expect(() => agent.steer('Another message')).not.toThrow();
|
|
65
70
|
});
|
|
66
|
-
it(
|
|
71
|
+
it('should add follow-up messages to queue', () => {
|
|
67
72
|
const agent = new Agent({ model: testModel });
|
|
68
|
-
agent.followUp(
|
|
73
|
+
agent.followUp('Can you also check the forecast?');
|
|
69
74
|
// Follow-up message should be queued
|
|
70
|
-
expect(() => agent.followUp(
|
|
75
|
+
expect(() => agent.followUp('And the temperature?')).not.toThrow();
|
|
71
76
|
});
|
|
72
|
-
it(
|
|
77
|
+
it('should accept Message objects in steer and followUp', () => {
|
|
73
78
|
const agent = new Agent({ model: testModel });
|
|
74
79
|
const message = {
|
|
75
|
-
role:
|
|
76
|
-
content:
|
|
80
|
+
role: 'user',
|
|
81
|
+
content: 'Custom message',
|
|
77
82
|
timestamp: Date.now(),
|
|
78
83
|
};
|
|
79
84
|
expect(() => agent.steer(message)).not.toThrow();
|
|
80
85
|
expect(() => agent.followUp(message)).not.toThrow();
|
|
81
86
|
});
|
|
82
87
|
});
|
|
83
|
-
describe(
|
|
84
|
-
it(
|
|
88
|
+
describe('Agent Retry Logic', () => {
|
|
89
|
+
it('should configure max retry delay', () => {
|
|
85
90
|
const agent = new Agent({
|
|
86
91
|
model: testModel,
|
|
87
92
|
maxRetryDelayMs: 5000,
|
|
@@ -89,20 +94,20 @@ describe("Agent Retry Logic", () => {
|
|
|
89
94
|
expect(agent).toBeDefined();
|
|
90
95
|
});
|
|
91
96
|
});
|
|
92
|
-
describe(
|
|
93
|
-
it(
|
|
97
|
+
describe('defineTool', () => {
|
|
98
|
+
it('should create tool with Zod schema', () => {
|
|
94
99
|
const tool = defineTool({
|
|
95
|
-
name:
|
|
96
|
-
description:
|
|
100
|
+
name: 'get_weather',
|
|
101
|
+
description: 'Get the current weather',
|
|
97
102
|
parameters: z.object({
|
|
98
|
-
location: z.string().describe(
|
|
103
|
+
location: z.string().describe('The location'),
|
|
99
104
|
}),
|
|
100
105
|
execute: async (args, context) => ({
|
|
101
|
-
content: [{ type:
|
|
106
|
+
content: [{ type: 'text', text: 'sunny' }],
|
|
102
107
|
}),
|
|
103
108
|
});
|
|
104
|
-
expect(tool.name).toBe(
|
|
105
|
-
expect(tool.description).toBe(
|
|
109
|
+
expect(tool.name).toBe('get_weather');
|
|
110
|
+
expect(tool.description).toBe('Get the current weather');
|
|
106
111
|
expect(tool.parameters).toBeDefined();
|
|
107
112
|
});
|
|
108
113
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.test.js","sourceRoot":"","sources":["../../src/__tests__/agent.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAc,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,MAAM,SAAS,GAAU,EAAE,EAAE,
|
|
1
|
+
{"version":3,"file":"agent.test.js","sourceRoot":"","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