opc-agent 3.0.0 → 4.0.0
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/README.md +30 -24
- package/dist/channels/dingtalk.d.ts +17 -0
- package/dist/channels/dingtalk.js +38 -0
- package/dist/channels/googlechat.d.ts +14 -0
- package/dist/channels/googlechat.js +37 -0
- package/dist/channels/imessage.d.ts +13 -0
- package/dist/channels/imessage.js +28 -0
- package/dist/channels/irc.d.ts +20 -0
- package/dist/channels/irc.js +71 -0
- package/dist/channels/line.d.ts +14 -0
- package/dist/channels/line.js +28 -0
- package/dist/channels/matrix.d.ts +15 -0
- package/dist/channels/matrix.js +28 -0
- package/dist/channels/mattermost.d.ts +18 -0
- package/dist/channels/mattermost.js +49 -0
- package/dist/channels/msteams.d.ts +14 -0
- package/dist/channels/msteams.js +28 -0
- package/dist/channels/nostr.d.ts +14 -0
- package/dist/channels/nostr.js +28 -0
- package/dist/channels/qq.d.ts +15 -0
- package/dist/channels/qq.js +28 -0
- package/dist/channels/signal.d.ts +14 -0
- package/dist/channels/signal.js +28 -0
- package/dist/channels/sms.d.ts +15 -0
- package/dist/channels/sms.js +28 -0
- package/dist/channels/twitch.d.ts +17 -0
- package/dist/channels/twitch.js +59 -0
- package/dist/channels/voice-call.d.ts +27 -0
- package/dist/channels/voice-call.js +82 -0
- package/dist/channels/whatsapp.d.ts +14 -0
- package/dist/channels/whatsapp.js +28 -0
- package/dist/cli.js +36 -0
- package/dist/core/api-server.d.ts +25 -0
- package/dist/core/api-server.js +286 -0
- package/dist/core/audio.d.ts +50 -0
- package/dist/core/audio.js +68 -0
- package/dist/core/context-discovery.d.ts +16 -0
- package/dist/core/context-discovery.js +107 -0
- package/dist/core/context-refs.d.ts +29 -0
- package/dist/core/context-refs.js +162 -0
- package/dist/core/gateway.d.ts +53 -0
- package/dist/core/gateway.js +80 -0
- package/dist/core/heartbeat.d.ts +19 -0
- package/dist/core/heartbeat.js +50 -0
- package/dist/core/hooks.d.ts +28 -0
- package/dist/core/hooks.js +82 -0
- package/dist/core/ide-bridge.d.ts +53 -0
- package/dist/core/ide-bridge.js +97 -0
- package/dist/core/node-network.d.ts +23 -0
- package/dist/core/node-network.js +77 -0
- package/dist/core/profiles.d.ts +27 -0
- package/dist/core/profiles.js +131 -0
- package/dist/core/sandbox.d.ts +25 -0
- package/dist/core/sandbox.js +84 -1
- package/dist/core/session-manager.d.ts +33 -0
- package/dist/core/session-manager.js +157 -0
- package/dist/core/vision.d.ts +45 -0
- package/dist/core/vision.js +177 -0
- package/dist/index.d.ts +64 -1
- package/dist/index.js +86 -3
- package/dist/memory/context-compressor.d.ts +43 -0
- package/dist/memory/context-compressor.js +167 -0
- package/dist/memory/index.d.ts +4 -0
- package/dist/memory/index.js +5 -1
- package/dist/memory/user-profiler.d.ts +50 -0
- package/dist/memory/user-profiler.js +201 -0
- package/dist/schema/oad.d.ts +12 -12
- package/dist/security/approvals.d.ts +53 -0
- package/dist/security/approvals.js +115 -0
- package/dist/security/elevated.d.ts +41 -0
- package/dist/security/elevated.js +89 -0
- package/dist/security/index.d.ts +6 -0
- package/dist/security/index.js +7 -1
- package/dist/security/secrets.d.ts +34 -0
- package/dist/security/secrets.js +115 -0
- package/dist/tools/builtin/browser.d.ts +47 -0
- package/dist/tools/builtin/browser.js +284 -0
- package/dist/tools/builtin/home-assistant.d.ts +12 -0
- package/dist/tools/builtin/home-assistant.js +126 -0
- package/dist/tools/builtin/index.d.ts +6 -1
- package/dist/tools/builtin/index.js +18 -2
- package/dist/tools/builtin/rl-tools.d.ts +13 -0
- package/dist/tools/builtin/rl-tools.js +228 -0
- package/dist/tools/builtin/vision.d.ts +6 -0
- package/dist/tools/builtin/vision.js +61 -0
- package/package.json +3 -3
- package/src/channels/dingtalk.ts +46 -0
- package/src/channels/googlechat.ts +42 -0
- package/src/channels/imessage.ts +32 -0
- package/src/channels/irc.ts +82 -0
- package/src/channels/line.ts +33 -0
- package/src/channels/matrix.ts +34 -0
- package/src/channels/mattermost.ts +57 -0
- package/src/channels/msteams.ts +33 -0
- package/src/channels/nostr.ts +33 -0
- package/src/channels/qq.ts +34 -0
- package/src/channels/signal.ts +33 -0
- package/src/channels/sms.ts +34 -0
- package/src/channels/twitch.ts +65 -0
- package/src/channels/voice-call.ts +100 -0
- package/src/channels/whatsapp.ts +33 -0
- package/src/cli.ts +40 -0
- package/src/core/api-server.ts +277 -0
- package/src/core/audio.ts +98 -0
- package/src/core/context-discovery.ts +85 -0
- package/src/core/context-refs.ts +140 -0
- package/src/core/gateway.ts +106 -0
- package/src/core/heartbeat.ts +51 -0
- package/src/core/hooks.ts +105 -0
- package/src/core/ide-bridge.ts +133 -0
- package/src/core/node-network.ts +86 -0
- package/src/core/profiles.ts +122 -0
- package/src/core/sandbox.ts +100 -0
- package/src/core/session-manager.ts +137 -0
- package/src/core/vision.ts +180 -0
- package/src/index.ts +84 -1
- package/src/memory/context-compressor.ts +189 -0
- package/src/memory/index.ts +4 -0
- package/src/memory/user-profiler.ts +215 -0
- package/src/security/approvals.ts +143 -0
- package/src/security/elevated.ts +105 -0
- package/src/security/index.ts +6 -0
- package/src/security/secrets.ts +129 -0
- package/src/tools/builtin/browser.ts +299 -0
- package/src/tools/builtin/home-assistant.ts +116 -0
- package/src/tools/builtin/index.ts +9 -2
- package/src/tools/builtin/rl-tools.ts +243 -0
- package/src/tools/builtin/vision.ts +64 -0
- package/tests/api-server.test.ts +148 -0
- package/tests/approvals.test.ts +89 -0
- package/tests/audio.test.ts +40 -0
- package/tests/browser.test.ts +179 -0
- package/tests/builtin-tools.test.ts +83 -83
- package/tests/channels-extra.test.ts +45 -0
- package/tests/context-compressor.test.ts +172 -0
- package/tests/context-refs.test.ts +121 -0
- package/tests/elevated.test.ts +69 -0
- package/tests/gateway.test.ts +63 -71
- package/tests/home-assistant.test.ts +40 -0
- package/tests/hooks.test.ts +79 -0
- package/tests/ide-bridge.test.ts +38 -0
- package/tests/node-network.test.ts +74 -0
- package/tests/profiles.test.ts +61 -0
- package/tests/rl-tools.test.ts +93 -0
- package/tests/sandbox-manager.test.ts +46 -0
- package/tests/secrets.test.ts +107 -0
- package/tests/tools/builtin-extended.test.ts +138 -138
- package/tests/user-profiler.test.ts +169 -0
- package/tests/v090-features.test.ts +254 -0
- package/tests/vision.test.ts +61 -0
- package/tests/voice-call.test.ts +47 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach } from 'vitest';
|
|
2
|
+
import { SecretsManager } from '../src/security/secrets';
|
|
3
|
+
import { existsSync, unlinkSync, mkdirSync } from 'fs';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
import { tmpdir } from 'os';
|
|
6
|
+
|
|
7
|
+
const testDir = join(tmpdir(), 'opc-secrets-test-' + Date.now());
|
|
8
|
+
let counter = 0;
|
|
9
|
+
function testPath() { return join(testDir, `secrets-${++counter}.enc`); }
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
// cleanup
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe('SecretsManager', () => {
|
|
16
|
+
it('should create new store and set/get secrets', () => {
|
|
17
|
+
const fp = testPath();
|
|
18
|
+
const mgr = new SecretsManager({ password: 'test123', filePath: fp });
|
|
19
|
+
mgr.set('API_KEY', 'sk-abc123');
|
|
20
|
+
expect(mgr.get('API_KEY')).toBe('sk-abc123');
|
|
21
|
+
expect(existsSync(fp)).toBe(true);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should persist and reload secrets', () => {
|
|
25
|
+
const fp = testPath();
|
|
26
|
+
const mgr1 = new SecretsManager({ password: 'pw', filePath: fp });
|
|
27
|
+
mgr1.set('TOKEN', 'xyz');
|
|
28
|
+
const mgr2 = new SecretsManager({ password: 'pw', filePath: fp });
|
|
29
|
+
expect(mgr2.get('TOKEN')).toBe('xyz');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should fail with wrong password', () => {
|
|
33
|
+
const fp = testPath();
|
|
34
|
+
const mgr = new SecretsManager({ password: 'right', filePath: fp });
|
|
35
|
+
mgr.set('KEY', 'val');
|
|
36
|
+
expect(() => new SecretsManager({ password: 'wrong', filePath: fp })).toThrow();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should delete secrets', () => {
|
|
40
|
+
const fp = testPath();
|
|
41
|
+
const mgr = new SecretsManager({ password: 'pw', filePath: fp });
|
|
42
|
+
mgr.set('A', '1');
|
|
43
|
+
expect(mgr.delete('A')).toBe(true);
|
|
44
|
+
expect(mgr.get('A')).toBeUndefined();
|
|
45
|
+
expect(mgr.delete('nonexistent')).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should list secret keys', () => {
|
|
49
|
+
const fp = testPath();
|
|
50
|
+
const mgr = new SecretsManager({ password: 'pw', filePath: fp });
|
|
51
|
+
mgr.set('A', '1');
|
|
52
|
+
mgr.set('B', '2');
|
|
53
|
+
expect(mgr.list().sort()).toEqual(['A', 'B']);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should check has', () => {
|
|
57
|
+
const fp = testPath();
|
|
58
|
+
const mgr = new SecretsManager({ password: 'pw', filePath: fp });
|
|
59
|
+
mgr.set('X', 'y');
|
|
60
|
+
expect(mgr.has('X')).toBe(true);
|
|
61
|
+
expect(mgr.has('Z')).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should inject secrets into env object', () => {
|
|
65
|
+
const fp = testPath();
|
|
66
|
+
const mgr = new SecretsManager({ password: 'pw', filePath: fp });
|
|
67
|
+
mgr.set('DB_HOST', 'localhost');
|
|
68
|
+
mgr.set('DB_PASS', 'secret');
|
|
69
|
+
const env: Record<string, string | undefined> = { PATH: '/usr/bin' };
|
|
70
|
+
mgr.inject(env, ['DB_HOST']);
|
|
71
|
+
expect(env.DB_HOST).toBe('localhost');
|
|
72
|
+
expect(env.DB_PASS).toBeUndefined();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should inject all secrets when no keys specified', () => {
|
|
76
|
+
const fp = testPath();
|
|
77
|
+
const mgr = new SecretsManager({ password: 'pw', filePath: fp });
|
|
78
|
+
mgr.set('A', '1');
|
|
79
|
+
mgr.set('B', '2');
|
|
80
|
+
const env: Record<string, string | undefined> = {};
|
|
81
|
+
mgr.inject(env);
|
|
82
|
+
expect(env.A).toBe('1');
|
|
83
|
+
expect(env.B).toBe('2');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should rotate password', () => {
|
|
87
|
+
const fp = testPath();
|
|
88
|
+
const mgr = new SecretsManager({ password: 'old', filePath: fp });
|
|
89
|
+
mgr.set('KEY', 'value');
|
|
90
|
+
mgr.rotate('new');
|
|
91
|
+
// Old password should fail
|
|
92
|
+
expect(() => new SecretsManager({ password: 'old', filePath: fp })).toThrow();
|
|
93
|
+
// New password should work
|
|
94
|
+
const mgr2 = new SecretsManager({ password: 'new', filePath: fp });
|
|
95
|
+
expect(mgr2.get('KEY')).toBe('value');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should export and import', () => {
|
|
99
|
+
const fp1 = testPath();
|
|
100
|
+
const mgr = new SecretsManager({ password: 'pw', filePath: fp1 });
|
|
101
|
+
mgr.set('SECRET', 'data');
|
|
102
|
+
const exported = mgr.exportEncrypted();
|
|
103
|
+
const fp2 = testPath();
|
|
104
|
+
const mgr2 = SecretsManager.importEncrypted(exported, 'pw', fp2);
|
|
105
|
+
expect(mgr2.get('SECRET')).toBe('data');
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -1,138 +1,138 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import * as path from 'path';
|
|
4
|
-
import * as os from 'os';
|
|
5
|
-
import { getBuiltinTools, getBuiltinToolsByName, fileTool, datetimeTool, shellTool, webTool } from '../../src/tools/builtin';
|
|
6
|
-
|
|
7
|
-
describe('getBuiltinTools', () => {
|
|
8
|
-
it('returns 4 tools', () => {
|
|
9
|
-
const tools = getBuiltinTools();
|
|
10
|
-
expect(tools).toHaveLength(
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('returns copies (not same array)', () => {
|
|
14
|
-
const a = getBuiltinTools();
|
|
15
|
-
const b = getBuiltinTools();
|
|
16
|
-
expect(a).not.toBe(b);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('contains file_operations tool', () => {
|
|
20
|
-
const tools = getBuiltinTools();
|
|
21
|
-
expect(tools.some(t => t.name === 'file_operations')).toBe(true);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('contains datetime tool', () => {
|
|
25
|
-
const tools = getBuiltinTools();
|
|
26
|
-
expect(tools.some(t => t.name === 'datetime')).toBe(true);
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe('getBuiltinToolsByName', () => {
|
|
31
|
-
it('returns all when no names given', () => {
|
|
32
|
-
expect(getBuiltinToolsByName()).toHaveLength(
|
|
33
|
-
expect(getBuiltinToolsByName([])).toHaveLength(
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('filters by name correctly', () => {
|
|
37
|
-
const tools = getBuiltinToolsByName(['datetime']);
|
|
38
|
-
expect(tools).toHaveLength(1);
|
|
39
|
-
expect(tools[0].name).toBe('datetime');
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('returns empty for unknown names', () => {
|
|
43
|
-
const tools = getBuiltinToolsByName(['nonexistent']);
|
|
44
|
-
expect(tools).toHaveLength(0);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('returns multiple matching tools', () => {
|
|
48
|
-
const tools = getBuiltinToolsByName(['datetime', 'file_operations']);
|
|
49
|
-
expect(tools).toHaveLength(2);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
describe('fileTool', () => {
|
|
54
|
-
let tmpDir: string;
|
|
55
|
-
let origCwd: string;
|
|
56
|
-
|
|
57
|
-
beforeEach(() => {
|
|
58
|
-
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'builtin-test-'));
|
|
59
|
-
origCwd = process.cwd();
|
|
60
|
-
process.chdir(tmpDir);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
afterEach(() => {
|
|
64
|
-
process.chdir(origCwd);
|
|
65
|
-
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('has name file_operations', () => {
|
|
69
|
-
expect(fileTool.name).toBe('file_operations');
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('read existing file', async () => {
|
|
73
|
-
fs.writeFileSync(path.join(tmpDir, 'test.txt'), 'hello world');
|
|
74
|
-
const result = await fileTool.execute({ action: 'read', path: 'test.txt' });
|
|
75
|
-
expect(result.content).toContain('hello world');
|
|
76
|
-
expect(result.isError).toBeFalsy();
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('read non-existent returns error', async () => {
|
|
80
|
-
const result = await fileTool.execute({ action: 'read', path: 'nonexistent.txt' });
|
|
81
|
-
expect(result.isError).toBe(true);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('write creates file', async () => {
|
|
85
|
-
const result = await fileTool.execute({ action: 'write', path: 'out.txt', content: 'data' });
|
|
86
|
-
expect(result.isError).toBeFalsy();
|
|
87
|
-
expect(fs.readFileSync(path.join(tmpDir, 'out.txt'), 'utf-8')).toBe('data');
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('list directory', async () => {
|
|
91
|
-
fs.writeFileSync(path.join(tmpDir, 'a.txt'), 'a');
|
|
92
|
-
fs.writeFileSync(path.join(tmpDir, 'b.txt'), 'b');
|
|
93
|
-
const result = await fileTool.execute({ action: 'list', path: '.' });
|
|
94
|
-
expect(result.content).toContain('a.txt');
|
|
95
|
-
expect(result.content).toContain('b.txt');
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('exists returns true for existing file', async () => {
|
|
99
|
-
fs.writeFileSync(path.join(tmpDir, 'exists.txt'), 'yes');
|
|
100
|
-
const result = await fileTool.execute({ action: 'exists', path: 'exists.txt' });
|
|
101
|
-
expect(result.content).toContain('true');
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it('exists returns false for missing file', async () => {
|
|
105
|
-
const result = await fileTool.execute({ action: 'exists', path: 'nope.txt' });
|
|
106
|
-
expect(result.content).toContain('false');
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('path escape blocked (../)', async () => {
|
|
110
|
-
const result = await fileTool.execute({ action: 'read', path: '../../../etc/passwd' });
|
|
111
|
-
expect(result.isError).toBe(true);
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
describe('datetimeTool', () => {
|
|
116
|
-
it('has name datetime', () => {
|
|
117
|
-
expect(datetimeTool.name).toBe('datetime');
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('returns valid ISO string', async () => {
|
|
121
|
-
const result = await datetimeTool.execute({});
|
|
122
|
-
expect(result.isError).toBeFalsy();
|
|
123
|
-
// Should contain a date-like string
|
|
124
|
-
expect(result.content).toMatch(/\d{4}/);
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
describe('shellTool', () => {
|
|
129
|
-
it('has name shell_exec', () => {
|
|
130
|
-
expect(shellTool.name).toBe('shell_exec');
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
describe('webTool', () => {
|
|
135
|
-
it('has name web_fetch', () => {
|
|
136
|
-
expect(webTool.name).toBe('web_fetch');
|
|
137
|
-
});
|
|
138
|
-
});
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import * as os from 'os';
|
|
5
|
+
import { getBuiltinTools, getBuiltinToolsByName, fileTool, datetimeTool, shellTool, webTool } from '../../src/tools/builtin';
|
|
6
|
+
|
|
7
|
+
describe('getBuiltinTools', () => {
|
|
8
|
+
it('returns 4 tools', () => {
|
|
9
|
+
const tools = getBuiltinTools();
|
|
10
|
+
expect(tools).toHaveLength(31);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('returns copies (not same array)', () => {
|
|
14
|
+
const a = getBuiltinTools();
|
|
15
|
+
const b = getBuiltinTools();
|
|
16
|
+
expect(a).not.toBe(b);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('contains file_operations tool', () => {
|
|
20
|
+
const tools = getBuiltinTools();
|
|
21
|
+
expect(tools.some(t => t.name === 'file_operations')).toBe(true);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('contains datetime tool', () => {
|
|
25
|
+
const tools = getBuiltinTools();
|
|
26
|
+
expect(tools.some(t => t.name === 'datetime')).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('getBuiltinToolsByName', () => {
|
|
31
|
+
it('returns all when no names given', () => {
|
|
32
|
+
expect(getBuiltinToolsByName()).toHaveLength(31);
|
|
33
|
+
expect(getBuiltinToolsByName([])).toHaveLength(31);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('filters by name correctly', () => {
|
|
37
|
+
const tools = getBuiltinToolsByName(['datetime']);
|
|
38
|
+
expect(tools).toHaveLength(1);
|
|
39
|
+
expect(tools[0].name).toBe('datetime');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('returns empty for unknown names', () => {
|
|
43
|
+
const tools = getBuiltinToolsByName(['nonexistent']);
|
|
44
|
+
expect(tools).toHaveLength(0);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('returns multiple matching tools', () => {
|
|
48
|
+
const tools = getBuiltinToolsByName(['datetime', 'file_operations']);
|
|
49
|
+
expect(tools).toHaveLength(2);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('fileTool', () => {
|
|
54
|
+
let tmpDir: string;
|
|
55
|
+
let origCwd: string;
|
|
56
|
+
|
|
57
|
+
beforeEach(() => {
|
|
58
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'builtin-test-'));
|
|
59
|
+
origCwd = process.cwd();
|
|
60
|
+
process.chdir(tmpDir);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
afterEach(() => {
|
|
64
|
+
process.chdir(origCwd);
|
|
65
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('has name file_operations', () => {
|
|
69
|
+
expect(fileTool.name).toBe('file_operations');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('read existing file', async () => {
|
|
73
|
+
fs.writeFileSync(path.join(tmpDir, 'test.txt'), 'hello world');
|
|
74
|
+
const result = await fileTool.execute({ action: 'read', path: 'test.txt' });
|
|
75
|
+
expect(result.content).toContain('hello world');
|
|
76
|
+
expect(result.isError).toBeFalsy();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('read non-existent returns error', async () => {
|
|
80
|
+
const result = await fileTool.execute({ action: 'read', path: 'nonexistent.txt' });
|
|
81
|
+
expect(result.isError).toBe(true);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('write creates file', async () => {
|
|
85
|
+
const result = await fileTool.execute({ action: 'write', path: 'out.txt', content: 'data' });
|
|
86
|
+
expect(result.isError).toBeFalsy();
|
|
87
|
+
expect(fs.readFileSync(path.join(tmpDir, 'out.txt'), 'utf-8')).toBe('data');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('list directory', async () => {
|
|
91
|
+
fs.writeFileSync(path.join(tmpDir, 'a.txt'), 'a');
|
|
92
|
+
fs.writeFileSync(path.join(tmpDir, 'b.txt'), 'b');
|
|
93
|
+
const result = await fileTool.execute({ action: 'list', path: '.' });
|
|
94
|
+
expect(result.content).toContain('a.txt');
|
|
95
|
+
expect(result.content).toContain('b.txt');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('exists returns true for existing file', async () => {
|
|
99
|
+
fs.writeFileSync(path.join(tmpDir, 'exists.txt'), 'yes');
|
|
100
|
+
const result = await fileTool.execute({ action: 'exists', path: 'exists.txt' });
|
|
101
|
+
expect(result.content).toContain('true');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('exists returns false for missing file', async () => {
|
|
105
|
+
const result = await fileTool.execute({ action: 'exists', path: 'nope.txt' });
|
|
106
|
+
expect(result.content).toContain('false');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('path escape blocked (../)', async () => {
|
|
110
|
+
const result = await fileTool.execute({ action: 'read', path: '../../../etc/passwd' });
|
|
111
|
+
expect(result.isError).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('datetimeTool', () => {
|
|
116
|
+
it('has name datetime', () => {
|
|
117
|
+
expect(datetimeTool.name).toBe('datetime');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('returns valid ISO string', async () => {
|
|
121
|
+
const result = await datetimeTool.execute({});
|
|
122
|
+
expect(result.isError).toBeFalsy();
|
|
123
|
+
// Should contain a date-like string
|
|
124
|
+
expect(result.content).toMatch(/\d{4}/);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('shellTool', () => {
|
|
129
|
+
it('has name shell_exec', () => {
|
|
130
|
+
expect(shellTool.name).toBe('shell_exec');
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe('webTool', () => {
|
|
135
|
+
it('has name web_fetch', () => {
|
|
136
|
+
expect(webTool.name).toBe('web_fetch');
|
|
137
|
+
});
|
|
138
|
+
});
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { UserProfiler } from '../src/memory/user-profiler';
|
|
3
|
+
import type { Message } from '../src/core/types';
|
|
4
|
+
|
|
5
|
+
function makeMsg(role: 'user' | 'assistant', content: string): Message {
|
|
6
|
+
return { id: `msg-${Math.random()}`, role, content, timestamp: Date.now() };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe('UserProfiler', () => {
|
|
10
|
+
describe('observe', () => {
|
|
11
|
+
it('should only process user messages', async () => {
|
|
12
|
+
const p = new UserProfiler();
|
|
13
|
+
await p.observe(makeMsg('assistant', 'hello'));
|
|
14
|
+
const profile = await p.getProfile();
|
|
15
|
+
expect(profile.communication_style).toBe('unknown'); // no observations
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should detect Chinese language preference', async () => {
|
|
19
|
+
const p = new UserProfiler();
|
|
20
|
+
await p.observe(makeMsg('user', '你好,请帮我分析一下这个架构'));
|
|
21
|
+
const profile = await p.getProfile();
|
|
22
|
+
expect(profile.preferences.language).toBe('chinese');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should detect English language preference', async () => {
|
|
26
|
+
const p = new UserProfiler();
|
|
27
|
+
await p.observe(makeMsg('user', 'Please help me review this code'));
|
|
28
|
+
const profile = await p.getProfile();
|
|
29
|
+
expect(profile.preferences.language).toBe('english');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should detect mixed language', async () => {
|
|
33
|
+
const p = new UserProfiler();
|
|
34
|
+
await p.observe(makeMsg('user', '帮我看看这个 TypeScript 的 architecture 设计'));
|
|
35
|
+
const profile = await p.getProfile();
|
|
36
|
+
expect(profile.preferences.language).toBe('mixed');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should detect brief communication style', async () => {
|
|
40
|
+
const p = new UserProfiler();
|
|
41
|
+
await p.observe(makeMsg('user', 'fix the bug'));
|
|
42
|
+
await p.observe(makeMsg('user', 'deploy it'));
|
|
43
|
+
await p.observe(makeMsg('user', 'check logs'));
|
|
44
|
+
const profile = await p.getProfile();
|
|
45
|
+
expect(profile.communication_style).toBe('brief');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should detect casual style', async () => {
|
|
49
|
+
const p = new UserProfiler();
|
|
50
|
+
// Use long-enough text so "brief" doesn't dominate, and add multiple casual signals
|
|
51
|
+
await p.observe(makeMsg('user', 'lol this is so broken haha 😂 let me check what is going on here because this is really funny!! 666 amazing stuff right there'));
|
|
52
|
+
await p.observe(makeMsg('user', 'haha omg 😂😂 this is hilarious, the whole thing just crashed lol 666 no way this works'));
|
|
53
|
+
const profile = await p.getProfile();
|
|
54
|
+
expect(profile.communication_style).toBe('casual');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should extract tech keywords', async () => {
|
|
58
|
+
const p = new UserProfiler();
|
|
59
|
+
await p.observe(makeMsg('user', 'I need to deploy the Docker container with Kubernetes'));
|
|
60
|
+
const profile = await p.getProfile();
|
|
61
|
+
expect(profile.expertise_areas).toContain('Docker');
|
|
62
|
+
expect(profile.expertise_areas).toContain('Kubernetes');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should classify how-to requests', async () => {
|
|
66
|
+
const p = new UserProfiler();
|
|
67
|
+
await p.observe(makeMsg('user', 'how to deploy a React app?'));
|
|
68
|
+
const profile = await p.getProfile();
|
|
69
|
+
expect(profile.common_requests).toContain('how-to');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should classify debugging requests', async () => {
|
|
73
|
+
const p = new UserProfiler();
|
|
74
|
+
await p.observe(makeMsg('user', 'I got an error when building the project'));
|
|
75
|
+
const profile = await p.getProfile();
|
|
76
|
+
expect(profile.common_requests).toContain('debugging');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should learn to brain every 20 observations', async () => {
|
|
80
|
+
const brain = { learn: vi.fn().mockResolvedValue(undefined) };
|
|
81
|
+
const p = new UserProfiler();
|
|
82
|
+
for (let i = 0; i < 20; i++) {
|
|
83
|
+
await p.observe(makeMsg('user', `message ${i}`), brain);
|
|
84
|
+
}
|
|
85
|
+
expect(brain.learn).toHaveBeenCalledTimes(1);
|
|
86
|
+
expect(brain.learn).toHaveBeenCalledWith(
|
|
87
|
+
expect.any(String),
|
|
88
|
+
{ insight_type: 'user_profile' }
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should handle brain.learn failure gracefully', async () => {
|
|
93
|
+
const brain = { learn: vi.fn().mockRejectedValue(new Error('fail')) };
|
|
94
|
+
const p = new UserProfiler();
|
|
95
|
+
for (let i = 0; i < 20; i++) {
|
|
96
|
+
await p.observe(makeMsg('user', `message ${i}`), brain);
|
|
97
|
+
}
|
|
98
|
+
// Should not throw
|
|
99
|
+
const profile = await p.getProfile();
|
|
100
|
+
expect(profile).toBeDefined();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('getProfile', () => {
|
|
105
|
+
it('should return empty profile with no observations', async () => {
|
|
106
|
+
const p = new UserProfiler();
|
|
107
|
+
const profile = await p.getProfile();
|
|
108
|
+
expect(profile.communication_style).toBe('unknown');
|
|
109
|
+
expect(profile.expertise_areas).toEqual([]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should fall back to brain recall when no local data', async () => {
|
|
113
|
+
const p = new UserProfiler();
|
|
114
|
+
const stored = JSON.stringify({ preferences: { language: 'chinese' }, communication_style: 'brief', expertise_areas: ['React'], common_requests: [], last_updated: 1 });
|
|
115
|
+
const brain = { recall: vi.fn().mockResolvedValue([stored]) };
|
|
116
|
+
const profile = await p.getProfile(brain);
|
|
117
|
+
expect(profile.preferences.language).toBe('chinese');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should handle brain recall failure', async () => {
|
|
121
|
+
const p = new UserProfiler();
|
|
122
|
+
const brain = { recall: vi.fn().mockRejectedValue(new Error('fail')) };
|
|
123
|
+
const profile = await p.getProfile(brain);
|
|
124
|
+
expect(profile.communication_style).toBe('unknown');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('enhance', () => {
|
|
129
|
+
it('should append language hint', () => {
|
|
130
|
+
const p = new UserProfiler();
|
|
131
|
+
const profile = { preferences: { language: 'chinese' }, communication_style: 'unknown', expertise_areas: [], common_requests: [], last_updated: 0 };
|
|
132
|
+
const result = p.enhance('You are a helpful assistant.', profile);
|
|
133
|
+
expect(result).toContain('Chinese');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('should append style hint', () => {
|
|
137
|
+
const p = new UserProfiler();
|
|
138
|
+
const profile = { preferences: {}, communication_style: 'brief', expertise_areas: [], common_requests: [], last_updated: 0 };
|
|
139
|
+
const result = p.enhance('Base prompt', profile);
|
|
140
|
+
expect(result).toContain('brief');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should append expertise areas', () => {
|
|
144
|
+
const p = new UserProfiler();
|
|
145
|
+
const profile = { preferences: {}, communication_style: 'unknown', expertise_areas: ['React', 'TypeScript'], common_requests: [], last_updated: 0 };
|
|
146
|
+
const result = p.enhance('Base prompt', profile);
|
|
147
|
+
expect(result).toContain('React');
|
|
148
|
+
expect(result).toContain('TypeScript');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('should not modify prompt for empty profile', () => {
|
|
152
|
+
const p = new UserProfiler();
|
|
153
|
+
const profile = { preferences: {}, communication_style: 'unknown', expertise_areas: [], common_requests: [], last_updated: 0 };
|
|
154
|
+
const result = p.enhance('Base prompt', profile);
|
|
155
|
+
expect(result).toBe('Base prompt');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should include all hints when profile is rich', () => {
|
|
159
|
+
const p = new UserProfiler();
|
|
160
|
+
const profile = { preferences: { language: 'english' }, communication_style: 'detailed', expertise_areas: ['Docker'], common_requests: ['how-to'], last_updated: Date.now() };
|
|
161
|
+
const result = p.enhance('Base', profile);
|
|
162
|
+
expect(result).toContain('[User Profile]');
|
|
163
|
+
expect(result).toContain('English');
|
|
164
|
+
expect(result).toContain('detailed');
|
|
165
|
+
expect(result).toContain('Docker');
|
|
166
|
+
expect(result).toContain('how-to');
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
});
|