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
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
2
|
-
import { mkdtempSync, rmSync } from 'fs';
|
|
3
|
-
import { join } from 'path';
|
|
4
|
-
import { tmpdir } from 'os';
|
|
5
|
-
import { getBuiltinTools, getBuiltinToolsByName } from '../src/tools/builtin';
|
|
6
|
-
import { fileTool, shellTool, datetimeTool } from '../src/tools/builtin';
|
|
7
|
-
|
|
8
|
-
describe('getBuiltinTools', () => {
|
|
9
|
-
it('returns
|
|
10
|
-
const tools = getBuiltinTools();
|
|
11
|
-
expect(tools).toHaveLength(
|
|
12
|
-
const names = tools.map(t => t.name);
|
|
13
|
-
expect(names).toContain('file_operations');
|
|
14
|
-
expect(names).toContain('web_fetch');
|
|
15
|
-
expect(names).toContain('shell_exec');
|
|
16
|
-
expect(names).toContain('datetime');
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('getBuiltinToolsByName filters correctly', () => {
|
|
20
|
-
const tools = getBuiltinToolsByName(['datetime', 'file_operations']);
|
|
21
|
-
expect(tools).toHaveLength(2);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('getBuiltinToolsByName with no args returns all', () => {
|
|
25
|
-
expect(getBuiltinToolsByName()).toHaveLength(
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
describe('file_operations tool', () => {
|
|
30
|
-
// file tool resolves paths relative to cwd, so use relative paths from a temp dir
|
|
31
|
-
// Actually, it uses process.cwd() as workspace. Let's just test with paths relative to cwd.
|
|
32
|
-
const testFile = `tmp-test-${Date.now()}.txt`;
|
|
33
|
-
|
|
34
|
-
afterAll(() => {
|
|
35
|
-
try { require('fs').unlinkSync(testFile); } catch {}
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('write and read a file', async () => {
|
|
39
|
-
const writeRes = await fileTool.execute({ action: 'write', path: testFile, content: 'hello' });
|
|
40
|
-
expect(writeRes.isError).toBe(false);
|
|
41
|
-
|
|
42
|
-
const readRes = await fileTool.execute({ action: 'read', path: testFile });
|
|
43
|
-
expect(readRes.isError).toBe(false);
|
|
44
|
-
expect(readRes.content).toBe('hello');
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('list files', async () => {
|
|
48
|
-
const res = await fileTool.execute({ action: 'list', path: '.' });
|
|
49
|
-
expect(res.isError).toBe(false);
|
|
50
|
-
expect(res.content).toContain('package.json');
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('exists check', async () => {
|
|
54
|
-
const res = await fileTool.execute({ action: 'exists', path: testFile });
|
|
55
|
-
expect(res.content).toBe('true');
|
|
56
|
-
|
|
57
|
-
const res2 = await fileTool.execute({ action: 'exists', path: 'nope-does-not-exist.txt' });
|
|
58
|
-
expect(res2.content).toBe('false');
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('rejects path outside workspace', async () => {
|
|
62
|
-
const res = await fileTool.execute({ action: 'read', path: '../../etc/passwd' });
|
|
63
|
-
expect(res.isError).toBe(true);
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
describe('datetime tool', () => {
|
|
68
|
-
it('returns valid JSON with iso field', async () => {
|
|
69
|
-
const res = await datetimeTool.execute({});
|
|
70
|
-
expect(res.isError).toBe(false);
|
|
71
|
-
const parsed = JSON.parse(res.content);
|
|
72
|
-
expect(parsed.iso).toBeDefined();
|
|
73
|
-
expect(new Date(parsed.iso).toISOString()).toBe(parsed.iso);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
describe('shell_exec tool', () => {
|
|
78
|
-
it('runs a command', async () => {
|
|
79
|
-
const res = await shellTool.execute({ command: 'echo hello' });
|
|
80
|
-
expect(res.isError).toBe(false);
|
|
81
|
-
expect(res.content).toContain('hello');
|
|
82
|
-
});
|
|
83
|
-
});
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
2
|
+
import { mkdtempSync, rmSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { tmpdir } from 'os';
|
|
5
|
+
import { getBuiltinTools, getBuiltinToolsByName } from '../src/tools/builtin';
|
|
6
|
+
import { fileTool, shellTool, datetimeTool } from '../src/tools/builtin';
|
|
7
|
+
|
|
8
|
+
describe('getBuiltinTools', () => {
|
|
9
|
+
it('returns 17 tools', () => {
|
|
10
|
+
const tools = getBuiltinTools();
|
|
11
|
+
expect(tools).toHaveLength(31);
|
|
12
|
+
const names = tools.map(t => t.name);
|
|
13
|
+
expect(names).toContain('file_operations');
|
|
14
|
+
expect(names).toContain('web_fetch');
|
|
15
|
+
expect(names).toContain('shell_exec');
|
|
16
|
+
expect(names).toContain('datetime');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('getBuiltinToolsByName filters correctly', () => {
|
|
20
|
+
const tools = getBuiltinToolsByName(['datetime', 'file_operations']);
|
|
21
|
+
expect(tools).toHaveLength(2);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('getBuiltinToolsByName with no args returns all', () => {
|
|
25
|
+
expect(getBuiltinToolsByName()).toHaveLength(31);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('file_operations tool', () => {
|
|
30
|
+
// file tool resolves paths relative to cwd, so use relative paths from a temp dir
|
|
31
|
+
// Actually, it uses process.cwd() as workspace. Let's just test with paths relative to cwd.
|
|
32
|
+
const testFile = `tmp-test-${Date.now()}.txt`;
|
|
33
|
+
|
|
34
|
+
afterAll(() => {
|
|
35
|
+
try { require('fs').unlinkSync(testFile); } catch {}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('write and read a file', async () => {
|
|
39
|
+
const writeRes = await fileTool.execute({ action: 'write', path: testFile, content: 'hello' });
|
|
40
|
+
expect(writeRes.isError).toBe(false);
|
|
41
|
+
|
|
42
|
+
const readRes = await fileTool.execute({ action: 'read', path: testFile });
|
|
43
|
+
expect(readRes.isError).toBe(false);
|
|
44
|
+
expect(readRes.content).toBe('hello');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('list files', async () => {
|
|
48
|
+
const res = await fileTool.execute({ action: 'list', path: '.' });
|
|
49
|
+
expect(res.isError).toBe(false);
|
|
50
|
+
expect(res.content).toContain('package.json');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('exists check', async () => {
|
|
54
|
+
const res = await fileTool.execute({ action: 'exists', path: testFile });
|
|
55
|
+
expect(res.content).toBe('true');
|
|
56
|
+
|
|
57
|
+
const res2 = await fileTool.execute({ action: 'exists', path: 'nope-does-not-exist.txt' });
|
|
58
|
+
expect(res2.content).toBe('false');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('rejects path outside workspace', async () => {
|
|
62
|
+
const res = await fileTool.execute({ action: 'read', path: '../../etc/passwd' });
|
|
63
|
+
expect(res.isError).toBe(true);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe('datetime tool', () => {
|
|
68
|
+
it('returns valid JSON with iso field', async () => {
|
|
69
|
+
const res = await datetimeTool.execute({});
|
|
70
|
+
expect(res.isError).toBe(false);
|
|
71
|
+
const parsed = JSON.parse(res.content);
|
|
72
|
+
expect(parsed.iso).toBeDefined();
|
|
73
|
+
expect(new Date(parsed.iso).toISOString()).toBe(parsed.iso);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('shell_exec tool', () => {
|
|
78
|
+
it('runs a command', async () => {
|
|
79
|
+
const res = await shellTool.execute({ command: 'echo hello' });
|
|
80
|
+
expect(res.isError).toBe(false);
|
|
81
|
+
expect(res.content).toContain('hello');
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { WhatsAppChannel } from '../src/channels/whatsapp';
|
|
3
|
+
import { SignalChannel } from '../src/channels/signal';
|
|
4
|
+
import { MatrixChannel } from '../src/channels/matrix';
|
|
5
|
+
import { IMessageChannel } from '../src/channels/imessage';
|
|
6
|
+
import { LINEChannel } from '../src/channels/line';
|
|
7
|
+
import { MSTeamsChannel } from '../src/channels/msteams';
|
|
8
|
+
import { QQChannel } from '../src/channels/qq';
|
|
9
|
+
import { NostrChannel } from '../src/channels/nostr';
|
|
10
|
+
import { SMSChannel } from '../src/channels/sms';
|
|
11
|
+
|
|
12
|
+
describe('Additional Channels', () => {
|
|
13
|
+
it('should create WhatsApp channel', () => {
|
|
14
|
+
const ch = new WhatsAppChannel({ phoneNumber: '+1234567890' });
|
|
15
|
+
expect(ch.type).toBe('whatsapp');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should throw on start without deps', async () => {
|
|
19
|
+
const ch = new SignalChannel();
|
|
20
|
+
await expect(ch.start()).rejects.toThrow(/Install/);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should create Matrix channel', () => {
|
|
24
|
+
const ch = new MatrixChannel({ homeserverUrl: 'https://matrix.org' });
|
|
25
|
+
expect(ch.type).toBe('matrix');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should create all channel types', () => {
|
|
29
|
+
const channels = [
|
|
30
|
+
new IMessageChannel(),
|
|
31
|
+
new LINEChannel(),
|
|
32
|
+
new MSTeamsChannel(),
|
|
33
|
+
new QQChannel(),
|
|
34
|
+
new NostrChannel(),
|
|
35
|
+
new SMSChannel(),
|
|
36
|
+
];
|
|
37
|
+
const types = channels.map(c => c.type);
|
|
38
|
+
expect(types).toEqual(['imessage', 'line', 'msteams', 'qq', 'nostr', 'sms']);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should throw on send before start', async () => {
|
|
42
|
+
const ch = new WhatsAppChannel();
|
|
43
|
+
await expect(ch.send('123', 'hello')).rejects.toThrow(/not yet connected/);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { ContextCompressor } from '../src/memory/context-compressor';
|
|
3
|
+
import type { Message } from '../src/core/types';
|
|
4
|
+
|
|
5
|
+
function makeMsg(role: 'user' | 'assistant' | 'system', content: string, id?: string): Message {
|
|
6
|
+
return { id: id ?? `msg-${Math.random()}`, role, content, timestamp: Date.now() };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function makeMessages(count: number, contentLen = 200): Message[] {
|
|
10
|
+
return Array.from({ length: count }, (_, i) =>
|
|
11
|
+
makeMsg(i % 2 === 0 ? 'user' : 'assistant', 'A'.repeat(contentLen), `msg-${i}`)
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('ContextCompressor', () => {
|
|
16
|
+
describe('estimateTokens', () => {
|
|
17
|
+
it('should estimate English text tokens (~1 per 4 chars)', () => {
|
|
18
|
+
const c = new ContextCompressor();
|
|
19
|
+
const tokens = c.estimateTokens('Hello World'); // 11 chars => ~3
|
|
20
|
+
expect(tokens).toBeGreaterThanOrEqual(2);
|
|
21
|
+
expect(tokens).toBeLessThanOrEqual(4);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should estimate Chinese text tokens (~1 per 2 chars)', () => {
|
|
25
|
+
const c = new ContextCompressor();
|
|
26
|
+
const tokens = c.estimateTokens('你好世界测试'); // 6 CJK chars => ~3
|
|
27
|
+
expect(tokens).toBeGreaterThanOrEqual(2);
|
|
28
|
+
expect(tokens).toBeLessThanOrEqual(4);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should handle mixed language text', () => {
|
|
32
|
+
const c = new ContextCompressor();
|
|
33
|
+
const tokens = c.estimateTokens('Hello 你好');
|
|
34
|
+
expect(tokens).toBeGreaterThan(0);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should return 0 for empty string', () => {
|
|
38
|
+
const c = new ContextCompressor();
|
|
39
|
+
expect(c.estimateTokens('')).toBe(0);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should handle long text proportionally', () => {
|
|
43
|
+
const c = new ContextCompressor();
|
|
44
|
+
const short = c.estimateTokens('test');
|
|
45
|
+
const long = c.estimateTokens('test'.repeat(100));
|
|
46
|
+
expect(long).toBeGreaterThan(short * 50);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('compress - no brain', () => {
|
|
51
|
+
it('should not compress when under threshold', async () => {
|
|
52
|
+
const c = new ContextCompressor({ maxTokens: 8000, compressThreshold: 0.8 });
|
|
53
|
+
const msgs = makeMessages(3, 50); // small
|
|
54
|
+
const result = await c.compress(msgs);
|
|
55
|
+
expect(result.messages).toHaveLength(3);
|
|
56
|
+
expect(result.savedTokens).toBe(0);
|
|
57
|
+
expect(result.learnedCount).toBe(0);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should compress when over threshold', async () => {
|
|
61
|
+
const c = new ContextCompressor({ maxTokens: 100, compressThreshold: 0.5, preserveRecent: 2 });
|
|
62
|
+
const msgs = makeMessages(10, 200);
|
|
63
|
+
const result = await c.compress(msgs);
|
|
64
|
+
expect(result.messages.length).toBeLessThan(10);
|
|
65
|
+
expect(result.savedTokens).toBeGreaterThan(0);
|
|
66
|
+
expect(result.summary).toBeTruthy();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should preserve recent messages', async () => {
|
|
70
|
+
const c = new ContextCompressor({ maxTokens: 100, compressThreshold: 0.5, preserveRecent: 3 });
|
|
71
|
+
const msgs = makeMessages(10, 200);
|
|
72
|
+
const result = await c.compress(msgs);
|
|
73
|
+
// Last 3 + 1 compression message = 4
|
|
74
|
+
expect(result.messages).toHaveLength(4);
|
|
75
|
+
expect(result.messages[1].id).toBe('msg-7');
|
|
76
|
+
expect(result.messages[2].id).toBe('msg-8');
|
|
77
|
+
expect(result.messages[3].id).toBe('msg-9');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should add system compression message', async () => {
|
|
81
|
+
const c = new ContextCompressor({ maxTokens: 100, compressThreshold: 0.5, preserveRecent: 2 });
|
|
82
|
+
const msgs = makeMessages(10, 200);
|
|
83
|
+
const result = await c.compress(msgs);
|
|
84
|
+
expect(result.messages[0].role).toBe('system');
|
|
85
|
+
expect(result.messages[0].content).toContain('Context compressed');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should handle preserveRecent >= message count', async () => {
|
|
89
|
+
const c = new ContextCompressor({ maxTokens: 10, compressThreshold: 0.5, preserveRecent: 20 });
|
|
90
|
+
const msgs = makeMessages(5, 200);
|
|
91
|
+
const result = await c.compress(msgs);
|
|
92
|
+
expect(result.messages).toHaveLength(5); // can't compress, all are "recent"
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe('compress - with brain', () => {
|
|
97
|
+
it('should learn insights to brain', async () => {
|
|
98
|
+
const brain = { learn: vi.fn().mockResolvedValue(undefined) };
|
|
99
|
+
const c = new ContextCompressor({ maxTokens: 100, compressThreshold: 0.5, preserveRecent: 2 });
|
|
100
|
+
const msgs = [
|
|
101
|
+
makeMsg('user', 'I decided to use TypeScript for the project'),
|
|
102
|
+
makeMsg('assistant', 'Great decision! TypeScript provides type safety.'.repeat(5)),
|
|
103
|
+
makeMsg('user', 'I prefer functional programming'),
|
|
104
|
+
...makeMessages(4, 200),
|
|
105
|
+
];
|
|
106
|
+
const result = await c.compress(msgs, { brain });
|
|
107
|
+
expect(result.learnedCount).toBeGreaterThan(0);
|
|
108
|
+
expect(brain.learn).toHaveBeenCalled();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('should include brain reference in compression message', async () => {
|
|
112
|
+
const brain = { learn: vi.fn().mockResolvedValue(undefined) };
|
|
113
|
+
const c = new ContextCompressor({ maxTokens: 100, compressThreshold: 0.5, preserveRecent: 1 });
|
|
114
|
+
const msgs = makeMessages(10, 200);
|
|
115
|
+
const result = await c.compress(msgs, { brain });
|
|
116
|
+
expect(result.messages[0].content).toContain('Brain');
|
|
117
|
+
expect(result.messages[0].content).toContain('recall()');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should handle brain.learn failure gracefully', async () => {
|
|
121
|
+
const brain = { learn: vi.fn().mockRejectedValue(new Error('fail')) };
|
|
122
|
+
const c = new ContextCompressor({ maxTokens: 100, compressThreshold: 0.5, preserveRecent: 2 });
|
|
123
|
+
const msgs = [
|
|
124
|
+
makeMsg('user', 'We decided to use Rust'),
|
|
125
|
+
...makeMessages(8, 200),
|
|
126
|
+
];
|
|
127
|
+
const result = await c.compress(msgs, { brain });
|
|
128
|
+
expect(result.learnedCount).toBe(0); // all failed
|
|
129
|
+
expect(result.messages.length).toBeLessThan(10);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('restore', () => {
|
|
134
|
+
it('should call brain.recall and return strings', async () => {
|
|
135
|
+
const brain = { recall: vi.fn().mockResolvedValue(['fact1', 'fact2']) };
|
|
136
|
+
const c = new ContextCompressor();
|
|
137
|
+
const results = await c.restore('test query', brain);
|
|
138
|
+
expect(results).toEqual(['fact1', 'fact2']);
|
|
139
|
+
expect(brain.recall).toHaveBeenCalledWith('test query');
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should handle object results from recall', async () => {
|
|
143
|
+
const brain = { recall: vi.fn().mockResolvedValue([{ content: 'data' }]) };
|
|
144
|
+
const c = new ContextCompressor();
|
|
145
|
+
const results = await c.restore('query', brain);
|
|
146
|
+
expect(results).toEqual(['data']);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('should return empty array when no brain', async () => {
|
|
150
|
+
const c = new ContextCompressor();
|
|
151
|
+
const results = await c.restore('query', null);
|
|
152
|
+
expect(results).toEqual([]);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should handle recall failure', async () => {
|
|
156
|
+
const brain = { recall: vi.fn().mockRejectedValue(new Error('fail')) };
|
|
157
|
+
const c = new ContextCompressor();
|
|
158
|
+
const results = await c.restore('query', brain);
|
|
159
|
+
expect(results).toEqual([]);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe('config override', () => {
|
|
164
|
+
it('should allow per-call config override', async () => {
|
|
165
|
+
const c = new ContextCompressor({ maxTokens: 100000 }); // high default
|
|
166
|
+
const msgs = makeMessages(10, 200);
|
|
167
|
+
// Override with low threshold
|
|
168
|
+
const result = await c.compress(msgs, { maxTokens: 100, compressThreshold: 0.5, preserveRecent: 2 });
|
|
169
|
+
expect(result.messages.length).toBeLessThan(10);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import { ContextRefResolver } from '../src/core/context-refs';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import * as os from 'os';
|
|
6
|
+
|
|
7
|
+
describe('ContextRefResolver.parseRefs', () => {
|
|
8
|
+
let resolver: ContextRefResolver;
|
|
9
|
+
beforeEach(() => { resolver = new ContextRefResolver(); });
|
|
10
|
+
|
|
11
|
+
it('parses @file references', () => {
|
|
12
|
+
const refs = resolver.parseRefs('Check @file:src/index.ts please');
|
|
13
|
+
expect(refs).toEqual([{ type: 'file', path: 'src/index.ts' }]);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('parses @folder references', () => {
|
|
17
|
+
const refs = resolver.parseRefs('Look at @folder:src/');
|
|
18
|
+
expect(refs).toEqual([{ type: 'folder', path: 'src/' }]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('parses @url references', () => {
|
|
22
|
+
const refs = resolver.parseRefs('See @url:https://example.com/api');
|
|
23
|
+
expect(refs).toEqual([{ type: 'url', path: 'https://example.com/api' }]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('parses @git-diff', () => {
|
|
27
|
+
const refs = resolver.parseRefs('Show me @git-diff');
|
|
28
|
+
expect(refs).toEqual([{ type: 'git-diff', path: 'git-diff' }]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('parses @git-log with count', () => {
|
|
32
|
+
const refs = resolver.parseRefs('Show @git-log:5');
|
|
33
|
+
expect(refs).toEqual([{ type: 'git-log', path: '5' }]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('parses @git-log without count defaults to 10', () => {
|
|
37
|
+
const refs = resolver.parseRefs('Show @git-log');
|
|
38
|
+
expect(refs).toEqual([{ type: 'git-log', path: '10' }]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('parses multiple refs', () => {
|
|
42
|
+
const refs = resolver.parseRefs('Check @file:a.ts and @file:b.ts and @git-diff');
|
|
43
|
+
expect(refs).toHaveLength(3);
|
|
44
|
+
expect(refs[0].type).toBe('file');
|
|
45
|
+
expect(refs[1].type).toBe('file');
|
|
46
|
+
expect(refs[2].type).toBe('git-diff');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('returns empty for no refs', () => {
|
|
50
|
+
expect(resolver.parseRefs('no refs here')).toEqual([]);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('ContextRefResolver.resolveRefs', () => {
|
|
55
|
+
let resolver: ContextRefResolver;
|
|
56
|
+
let tmpDir: string;
|
|
57
|
+
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
resolver = new ContextRefResolver();
|
|
60
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ctx-ref-'));
|
|
61
|
+
fs.writeFileSync(path.join(tmpDir, 'test.txt'), 'hello world');
|
|
62
|
+
fs.mkdirSync(path.join(tmpDir, 'sub'));
|
|
63
|
+
fs.writeFileSync(path.join(tmpDir, 'sub', 'nested.txt'), 'nested');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('resolves file ref', async () => {
|
|
67
|
+
const refs = await resolver.resolveRefs([{ type: 'file', path: path.join(tmpDir, 'test.txt') }]);
|
|
68
|
+
expect(refs[0].content).toBe('hello world');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('resolves folder ref', async () => {
|
|
72
|
+
const refs = await resolver.resolveRefs([{ type: 'folder', path: tmpDir }]);
|
|
73
|
+
expect(refs[0].content).toContain('test.txt');
|
|
74
|
+
expect(refs[0].content).toContain('sub');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('handles file not found gracefully', async () => {
|
|
78
|
+
const refs = await resolver.resolveRefs([{ type: 'file', path: '/nonexistent/file.txt' }]);
|
|
79
|
+
expect(refs[0].content).toContain('Error');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('truncates long content', async () => {
|
|
83
|
+
const longContent = 'x'.repeat(6000);
|
|
84
|
+
fs.writeFileSync(path.join(tmpDir, 'long.txt'), longContent);
|
|
85
|
+
const refs = await resolver.resolveRefs([{ type: 'file', path: path.join(tmpDir, 'long.txt') }]);
|
|
86
|
+
expect(refs[0].content!.length).toBeLessThan(6000);
|
|
87
|
+
expect(refs[0].content).toContain('truncated');
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('ContextRefResolver.injectRefs', () => {
|
|
92
|
+
let resolver: ContextRefResolver;
|
|
93
|
+
beforeEach(() => { resolver = new ContextRefResolver(); });
|
|
94
|
+
|
|
95
|
+
it('injects context before last user message', () => {
|
|
96
|
+
const messages = [
|
|
97
|
+
{ role: 'user', content: 'first' },
|
|
98
|
+
{ role: 'assistant', content: 'ok' },
|
|
99
|
+
{ role: 'user', content: 'second' },
|
|
100
|
+
];
|
|
101
|
+
const refs = [{ type: 'file' as const, path: 'a.ts', content: 'code here' }];
|
|
102
|
+
const result = resolver.injectRefs(messages, refs);
|
|
103
|
+
expect(result).toHaveLength(4);
|
|
104
|
+
expect(result[2].role).toBe('system');
|
|
105
|
+
expect(result[2].content).toContain('@file:a.ts');
|
|
106
|
+
expect(result[3].content).toBe('second');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('returns original messages if no refs', () => {
|
|
110
|
+
const messages = [{ role: 'user', content: 'hi' }];
|
|
111
|
+
const result = resolver.injectRefs(messages, []);
|
|
112
|
+
expect(result).toEqual(messages);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('skips refs without content', () => {
|
|
116
|
+
const messages = [{ role: 'user', content: 'hi' }];
|
|
117
|
+
const refs = [{ type: 'file' as const, path: 'a.ts' }];
|
|
118
|
+
const result = resolver.injectRefs(messages, refs);
|
|
119
|
+
expect(result).toEqual(messages);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { ElevatedManager } from '../src/security/elevated';
|
|
3
|
+
|
|
4
|
+
describe('ElevatedManager', () => {
|
|
5
|
+
it('should default to ask mode', () => {
|
|
6
|
+
const mgr = new ElevatedManager();
|
|
7
|
+
expect(mgr.getMode()).toBe('ask');
|
|
8
|
+
expect(mgr.isElevated()).toBe(false);
|
|
9
|
+
mgr.destroy();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('should elevate and revoke', () => {
|
|
13
|
+
const mgr = new ElevatedManager();
|
|
14
|
+
expect(mgr.elevate('test')).toBe(true);
|
|
15
|
+
expect(mgr.isElevated()).toBe(true);
|
|
16
|
+
mgr.revoke();
|
|
17
|
+
expect(mgr.isElevated()).toBe(false);
|
|
18
|
+
mgr.destroy();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should not elevate in off mode', () => {
|
|
22
|
+
const mgr = new ElevatedManager({ mode: 'off' });
|
|
23
|
+
expect(mgr.elevate()).toBe(false);
|
|
24
|
+
expect(mgr.isElevated()).toBe(false);
|
|
25
|
+
mgr.destroy();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should allow commands in allowedCommands list', () => {
|
|
29
|
+
const mgr = new ElevatedManager({ allowedCommands: [/^git\s/] });
|
|
30
|
+
expect(mgr.isCommandAllowed('git pull')).toBe(true);
|
|
31
|
+
expect(mgr.isCommandAllowed('rm -rf /')).toBe(false);
|
|
32
|
+
mgr.destroy();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should auto-execute in on mode', () => {
|
|
36
|
+
const mgr = new ElevatedManager({ mode: 'on' });
|
|
37
|
+
const result = mgr.canExecute('sudo reboot');
|
|
38
|
+
expect(result.allowed).toBe(true);
|
|
39
|
+
expect(mgr.isElevated()).toBe(true);
|
|
40
|
+
mgr.destroy();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should require elevation in ask mode when not elevated', () => {
|
|
44
|
+
const mgr = new ElevatedManager({ mode: 'ask' });
|
|
45
|
+
const result = mgr.canExecute('sudo reboot');
|
|
46
|
+
expect(result.allowed).toBe(false);
|
|
47
|
+
expect(result.needsElevation).toBe(true);
|
|
48
|
+
mgr.destroy();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should allow execution in ask mode when elevated', () => {
|
|
52
|
+
const mgr = new ElevatedManager({ mode: 'ask' });
|
|
53
|
+
mgr.elevate();
|
|
54
|
+
const result = mgr.canExecute('sudo reboot');
|
|
55
|
+
expect(result.allowed).toBe(true);
|
|
56
|
+
mgr.destroy();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should keep audit log', () => {
|
|
60
|
+
const mgr = new ElevatedManager();
|
|
61
|
+
mgr.elevate('testing');
|
|
62
|
+
mgr.revoke('done');
|
|
63
|
+
const log = mgr.getAuditLog();
|
|
64
|
+
expect(log).toHaveLength(2);
|
|
65
|
+
expect(log[0].action).toBe('elevate');
|
|
66
|
+
expect(log[1].action).toBe('revoke');
|
|
67
|
+
mgr.destroy();
|
|
68
|
+
});
|
|
69
|
+
});
|