opc-agent 3.0.1 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/README.md +404 -74
  2. package/README.zh-CN.md +82 -0
  3. package/dist/channels/dingtalk.d.ts +17 -0
  4. package/dist/channels/dingtalk.js +38 -0
  5. package/dist/channels/googlechat.d.ts +14 -0
  6. package/dist/channels/googlechat.js +37 -0
  7. package/dist/channels/imessage.d.ts +13 -0
  8. package/dist/channels/imessage.js +28 -0
  9. package/dist/channels/irc.d.ts +20 -0
  10. package/dist/channels/irc.js +71 -0
  11. package/dist/channels/line.d.ts +14 -0
  12. package/dist/channels/line.js +28 -0
  13. package/dist/channels/matrix.d.ts +15 -0
  14. package/dist/channels/matrix.js +28 -0
  15. package/dist/channels/mattermost.d.ts +18 -0
  16. package/dist/channels/mattermost.js +49 -0
  17. package/dist/channels/msteams.d.ts +14 -0
  18. package/dist/channels/msteams.js +28 -0
  19. package/dist/channels/nostr.d.ts +14 -0
  20. package/dist/channels/nostr.js +28 -0
  21. package/dist/channels/qq.d.ts +15 -0
  22. package/dist/channels/qq.js +28 -0
  23. package/dist/channels/signal.d.ts +14 -0
  24. package/dist/channels/signal.js +28 -0
  25. package/dist/channels/sms.d.ts +15 -0
  26. package/dist/channels/sms.js +28 -0
  27. package/dist/channels/twitch.d.ts +17 -0
  28. package/dist/channels/twitch.js +59 -0
  29. package/dist/channels/voice-call.d.ts +27 -0
  30. package/dist/channels/voice-call.js +82 -0
  31. package/dist/channels/whatsapp.d.ts +14 -0
  32. package/dist/channels/whatsapp.js +28 -0
  33. package/dist/cli/chat.d.ts +2 -0
  34. package/dist/cli/chat.js +134 -0
  35. package/dist/cli/setup.d.ts +4 -0
  36. package/dist/cli/setup.js +303 -0
  37. package/dist/cli.js +142 -6
  38. package/dist/core/api-server.d.ts +25 -0
  39. package/dist/core/api-server.js +286 -0
  40. package/dist/core/audio.d.ts +50 -0
  41. package/dist/core/audio.js +68 -0
  42. package/dist/core/context-discovery.d.ts +16 -0
  43. package/dist/core/context-discovery.js +107 -0
  44. package/dist/core/context-refs.d.ts +29 -0
  45. package/dist/core/context-refs.js +162 -0
  46. package/dist/core/gateway.d.ts +53 -0
  47. package/dist/core/gateway.js +80 -0
  48. package/dist/core/heartbeat.d.ts +19 -0
  49. package/dist/core/heartbeat.js +50 -0
  50. package/dist/core/hooks.d.ts +28 -0
  51. package/dist/core/hooks.js +82 -0
  52. package/dist/core/ide-bridge.d.ts +53 -0
  53. package/dist/core/ide-bridge.js +97 -0
  54. package/dist/core/node-network.d.ts +23 -0
  55. package/dist/core/node-network.js +77 -0
  56. package/dist/core/profiles.d.ts +27 -0
  57. package/dist/core/profiles.js +131 -0
  58. package/dist/core/sandbox.d.ts +25 -0
  59. package/dist/core/sandbox.js +84 -1
  60. package/dist/core/session-manager.d.ts +33 -0
  61. package/dist/core/session-manager.js +157 -0
  62. package/dist/core/vision.d.ts +45 -0
  63. package/dist/core/vision.js +177 -0
  64. package/dist/hub/brain-seed.d.ts +14 -0
  65. package/dist/hub/brain-seed.js +77 -0
  66. package/dist/hub/client.d.ts +25 -0
  67. package/dist/hub/client.js +44 -0
  68. package/dist/index.d.ts +66 -1
  69. package/dist/index.js +95 -3
  70. package/dist/memory/context-compressor.d.ts +43 -0
  71. package/dist/memory/context-compressor.js +167 -0
  72. package/dist/memory/index.d.ts +4 -0
  73. package/dist/memory/index.js +5 -1
  74. package/dist/memory/user-profiler.d.ts +50 -0
  75. package/dist/memory/user-profiler.js +201 -0
  76. package/dist/providers/index.d.ts +1 -1
  77. package/dist/providers/index.js +54 -1
  78. package/dist/scheduler/cron-engine.d.ts +41 -0
  79. package/dist/scheduler/cron-engine.js +200 -0
  80. package/dist/scheduler/index.d.ts +3 -0
  81. package/dist/scheduler/index.js +7 -0
  82. package/dist/schema/oad.d.ts +12 -12
  83. package/dist/security/approvals.d.ts +53 -0
  84. package/dist/security/approvals.js +115 -0
  85. package/dist/security/elevated.d.ts +41 -0
  86. package/dist/security/elevated.js +89 -0
  87. package/dist/security/index.d.ts +6 -0
  88. package/dist/security/index.js +7 -1
  89. package/dist/security/secrets.d.ts +34 -0
  90. package/dist/security/secrets.js +115 -0
  91. package/dist/skills/builtin/index.d.ts +6 -0
  92. package/dist/skills/builtin/index.js +402 -0
  93. package/dist/skills/marketplace.d.ts +30 -0
  94. package/dist/skills/marketplace.js +142 -0
  95. package/dist/skills/types.d.ts +34 -0
  96. package/dist/skills/types.js +16 -0
  97. package/dist/studio/server.d.ts +25 -0
  98. package/dist/studio/server.js +780 -0
  99. package/dist/studio/templates-data.d.ts +21 -0
  100. package/dist/studio/templates-data.js +148 -0
  101. package/dist/studio-ui/index.html +2502 -1073
  102. package/dist/tools/builtin/browser.d.ts +47 -0
  103. package/dist/tools/builtin/browser.js +284 -0
  104. package/dist/tools/builtin/home-assistant.d.ts +12 -0
  105. package/dist/tools/builtin/home-assistant.js +126 -0
  106. package/dist/tools/builtin/index.d.ts +7 -1
  107. package/dist/tools/builtin/index.js +23 -2
  108. package/dist/tools/builtin/rl-tools.d.ts +13 -0
  109. package/dist/tools/builtin/rl-tools.js +228 -0
  110. package/dist/tools/builtin/vision.d.ts +6 -0
  111. package/dist/tools/builtin/vision.js +61 -0
  112. package/dist/tools/builtin/web-search.d.ts +9 -0
  113. package/dist/tools/builtin/web-search.js +150 -0
  114. package/dist/tools/document-processor.d.ts +39 -0
  115. package/dist/tools/document-processor.js +188 -0
  116. package/dist/tools/image-generator.d.ts +42 -0
  117. package/dist/tools/image-generator.js +136 -0
  118. package/dist/tools/web-scraper.d.ts +20 -0
  119. package/dist/tools/web-scraper.js +148 -0
  120. package/dist/tools/web-search.d.ts +51 -0
  121. package/dist/tools/web-search.js +152 -0
  122. package/install.ps1 +154 -0
  123. package/install.sh +164 -0
  124. package/package.json +63 -52
  125. package/src/channels/dingtalk.ts +46 -0
  126. package/src/channels/googlechat.ts +42 -0
  127. package/src/channels/imessage.ts +32 -0
  128. package/src/channels/irc.ts +82 -0
  129. package/src/channels/line.ts +33 -0
  130. package/src/channels/matrix.ts +34 -0
  131. package/src/channels/mattermost.ts +57 -0
  132. package/src/channels/msteams.ts +33 -0
  133. package/src/channels/nostr.ts +33 -0
  134. package/src/channels/qq.ts +34 -0
  135. package/src/channels/signal.ts +33 -0
  136. package/src/channels/sms.ts +34 -0
  137. package/src/channels/twitch.ts +65 -0
  138. package/src/channels/voice-call.ts +100 -0
  139. package/src/channels/whatsapp.ts +33 -0
  140. package/src/cli/chat.ts +99 -0
  141. package/src/cli/setup.ts +314 -0
  142. package/src/cli.ts +148 -6
  143. package/src/core/api-server.ts +277 -0
  144. package/src/core/audio.ts +98 -0
  145. package/src/core/context-discovery.ts +85 -0
  146. package/src/core/context-refs.ts +140 -0
  147. package/src/core/gateway.ts +106 -0
  148. package/src/core/heartbeat.ts +51 -0
  149. package/src/core/hooks.ts +105 -0
  150. package/src/core/ide-bridge.ts +133 -0
  151. package/src/core/node-network.ts +86 -0
  152. package/src/core/profiles.ts +122 -0
  153. package/src/core/sandbox.ts +100 -0
  154. package/src/core/session-manager.ts +137 -0
  155. package/src/core/vision.ts +180 -0
  156. package/src/hub/brain-seed.ts +54 -0
  157. package/src/hub/client.ts +60 -0
  158. package/src/index.ts +86 -1
  159. package/src/memory/context-compressor.ts +189 -0
  160. package/src/memory/index.ts +4 -0
  161. package/src/memory/user-profiler.ts +215 -0
  162. package/src/providers/index.ts +64 -1
  163. package/src/scheduler/cron-engine.ts +191 -0
  164. package/src/scheduler/index.ts +2 -0
  165. package/src/security/approvals.ts +143 -0
  166. package/src/security/elevated.ts +105 -0
  167. package/src/security/index.ts +6 -0
  168. package/src/security/secrets.ts +129 -0
  169. package/src/skills/builtin/index.ts +408 -0
  170. package/src/skills/marketplace.ts +113 -0
  171. package/src/skills/types.ts +42 -0
  172. package/src/studio/server.ts +1591 -791
  173. package/src/studio/templates-data.ts +178 -0
  174. package/src/studio-ui/index.html +2502 -1073
  175. package/src/tools/builtin/browser.ts +299 -0
  176. package/src/tools/builtin/home-assistant.ts +116 -0
  177. package/src/tools/builtin/index.ts +37 -28
  178. package/src/tools/builtin/rl-tools.ts +243 -0
  179. package/src/tools/builtin/vision.ts +64 -0
  180. package/src/tools/builtin/web-search.ts +126 -0
  181. package/src/tools/document-processor.ts +213 -0
  182. package/src/tools/image-generator.ts +150 -0
  183. package/src/tools/web-scraper.ts +179 -0
  184. package/src/tools/web-search.ts +180 -0
  185. package/tests/api-server.test.ts +148 -0
  186. package/tests/approvals.test.ts +89 -0
  187. package/tests/audio.test.ts +40 -0
  188. package/tests/browser.test.ts +179 -0
  189. package/tests/builtin-tools.test.ts +83 -83
  190. package/tests/channels-extra.test.ts +45 -0
  191. package/tests/context-compressor.test.ts +172 -0
  192. package/tests/context-refs.test.ts +121 -0
  193. package/tests/cron-engine.test.ts +101 -0
  194. package/tests/document-processor.test.ts +69 -0
  195. package/tests/e2e-nocode.test.ts +442 -0
  196. package/tests/elevated.test.ts +69 -0
  197. package/tests/gateway.test.ts +63 -71
  198. package/tests/home-assistant.test.ts +40 -0
  199. package/tests/hooks.test.ts +79 -0
  200. package/tests/ide-bridge.test.ts +38 -0
  201. package/tests/image-generator.test.ts +84 -0
  202. package/tests/node-network.test.ts +74 -0
  203. package/tests/profiles.test.ts +61 -0
  204. package/tests/rl-tools.test.ts +93 -0
  205. package/tests/sandbox-manager.test.ts +46 -0
  206. package/tests/secrets.test.ts +107 -0
  207. package/tests/settings-api.test.ts +148 -0
  208. package/tests/setup.test.ts +73 -0
  209. package/tests/studio.test.ts +402 -229
  210. package/tests/tools/builtin-extended.test.ts +138 -138
  211. package/tests/user-profiler.test.ts +169 -0
  212. package/tests/v090-features.test.ts +254 -0
  213. package/tests/vision.test.ts +61 -0
  214. package/tests/voice-call.test.ts +47 -0
  215. package/tests/voice-interaction.test.ts +38 -0
  216. package/tests/web-search.test.ts +155 -0
@@ -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,101 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import { CronEngine, frequencyToCron } from '../src/scheduler/cron-engine';
3
+ import type { ScheduleTask } from '../src/scheduler/cron-engine';
4
+ import { existsSync, unlinkSync } from 'fs';
5
+ import { join } from 'path';
6
+ import * as os from 'os';
7
+
8
+ describe('CronEngine', () => {
9
+ let engine: CronEngine;
10
+ const schedulesPath = join(os.homedir(), '.opc', 'schedules.json');
11
+
12
+ beforeEach(() => {
13
+ engine = new CronEngine(async () => {});
14
+ });
15
+
16
+ afterEach(() => {
17
+ engine.stop();
18
+ });
19
+
20
+ describe('frequencyToCron', () => {
21
+ it('converts daily + time to cron', () => {
22
+ expect(frequencyToCron('daily', '08:30')).toBe('30 8 * * *');
23
+ });
24
+
25
+ it('converts weekly to Monday cron', () => {
26
+ expect(frequencyToCron('weekly', '09:00')).toBe('0 9 * * 1');
27
+ });
28
+
29
+ it('converts monthly to 1st of month', () => {
30
+ expect(frequencyToCron('monthly', '10:15')).toBe('15 10 1 * *');
31
+ });
32
+
33
+ it('defaults to 9:00 when no time given', () => {
34
+ expect(frequencyToCron('daily')).toBe('0 9 * * *');
35
+ });
36
+ });
37
+
38
+ describe('CRUD operations', () => {
39
+ it('creates a task', () => {
40
+ const task = engine.createTask({
41
+ name: 'Test Task',
42
+ schedule: '0 9 * * *',
43
+ description: 'Test description',
44
+ frequency: 'daily',
45
+ time: '09:00',
46
+ outputChannel: 'web',
47
+ enabled: true,
48
+ });
49
+ expect(task.id).toBeTruthy();
50
+ expect(task.name).toBe('Test Task');
51
+ expect(task.createdAt).toBeTruthy();
52
+ });
53
+
54
+ it('lists tasks', () => {
55
+ engine.createTask({ name: 'A', schedule: '0 9 * * *', description: '', frequency: 'daily', outputChannel: 'web', enabled: true });
56
+ engine.createTask({ name: 'B', schedule: '0 10 * * *', description: '', frequency: 'daily', outputChannel: 'telegram', enabled: false });
57
+ const tasks = engine.listTasks();
58
+ expect(tasks.length).toBeGreaterThanOrEqual(2);
59
+ });
60
+
61
+ it('updates a task', () => {
62
+ const task = engine.createTask({ name: 'Original', schedule: '0 9 * * *', description: '', frequency: 'daily', outputChannel: 'web', enabled: true });
63
+ const updated = engine.updateTask(task.id, { name: 'Updated', enabled: false });
64
+ expect(updated).not.toBeNull();
65
+ expect(updated!.name).toBe('Updated');
66
+ expect(updated!.enabled).toBe(false);
67
+ });
68
+
69
+ it('deletes a task', () => {
70
+ const task = engine.createTask({ name: 'ToDelete', schedule: '0 9 * * *', description: '', frequency: 'daily', outputChannel: 'web', enabled: true });
71
+ expect(engine.deleteTask(task.id)).toBe(true);
72
+ expect(engine.getTask(task.id)).toBeUndefined();
73
+ });
74
+
75
+ it('returns false for deleting non-existent task', () => {
76
+ expect(engine.deleteTask('nonexistent')).toBe(false);
77
+ });
78
+
79
+ it('returns null for updating non-existent task', () => {
80
+ expect(engine.updateTask('nonexistent', { name: 'x' })).toBeNull();
81
+ });
82
+ });
83
+
84
+ describe('run task', () => {
85
+ it('runs a task immediately', async () => {
86
+ const handler = vi.fn();
87
+ const eng = new CronEngine(handler);
88
+ const task = eng.createTask({ name: 'RunMe', schedule: '0 9 * * *', description: 'test', frequency: 'daily', outputChannel: 'web', enabled: true });
89
+ eng.start();
90
+ const result = await eng.runTask(task.id);
91
+ expect(result).toBe(true);
92
+ expect(handler).toHaveBeenCalled();
93
+ eng.stop();
94
+ });
95
+
96
+ it('returns false for running non-existent task', async () => {
97
+ const result = await engine.runTask('nonexistent');
98
+ expect(result).toBe(false);
99
+ });
100
+ });
101
+ });
@@ -0,0 +1,69 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { DocumentProcessor } from '../src/tools/document-processor';
3
+
4
+ describe('DocumentProcessor', () => {
5
+ const processor = new DocumentProcessor();
6
+
7
+ it('should process plain text', async () => {
8
+ const text = 'Hello world. This is a test document.\n\nSecond paragraph here.';
9
+ const buffer = Buffer.from(text, 'utf-8');
10
+ const doc = await processor.process(buffer, 'test.txt');
11
+
12
+ expect(doc.filename).toBe('test.txt');
13
+ expect(doc.format).toBe('txt');
14
+ expect(doc.chunks.length).toBeGreaterThan(0);
15
+ expect(doc.chunks[0].content).toContain('Hello world');
16
+ expect(doc.chunks[0].metadata.source).toBe('test.txt');
17
+ });
18
+
19
+ it('should process markdown with headings', async () => {
20
+ const md = `# Title\n\nFirst section content.\n\n## Section Two\n\nSecond section content.\n\n## Section Three\n\nThird section.`;
21
+ const buffer = Buffer.from(md, 'utf-8');
22
+ const doc = await processor.process(buffer, 'test.md');
23
+
24
+ expect(doc.format).toBe('md');
25
+ expect(doc.chunks.length).toBeGreaterThan(0);
26
+ });
27
+
28
+ it('should process CSV', async () => {
29
+ const csv = 'Name,Age,City\nAlice,30,Beijing\nBob,25,Shanghai';
30
+ const buffer = Buffer.from(csv, 'utf-8');
31
+ const doc = await processor.process(buffer, 'data.csv');
32
+
33
+ expect(doc.format).toBe('csv');
34
+ expect(doc.chunks.length).toBeGreaterThan(0);
35
+ expect(doc.chunks[0].content).toContain('Alice');
36
+ });
37
+
38
+ it('should process JSON array', async () => {
39
+ const json = JSON.stringify([{ name: 'Alice', role: 'engineer' }, { name: 'Bob', role: 'designer' }]);
40
+ const buffer = Buffer.from(json, 'utf-8');
41
+ const doc = await processor.process(buffer, 'data.json');
42
+
43
+ expect(doc.format).toBe('json');
44
+ expect(doc.chunks[0].content).toContain('Alice');
45
+ });
46
+
47
+ it('should chunk large text properly', async () => {
48
+ const bigText = Array(200).fill('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.').join('\n\n');
49
+ const buffer = Buffer.from(bigText, 'utf-8');
50
+ const doc = await processor.process(buffer, 'big.txt');
51
+
52
+ expect(doc.chunks.length).toBeGreaterThan(1);
53
+ for (const chunk of doc.chunks) {
54
+ expect(chunk.content.length).toBeLessThanOrEqual(5000); // some tolerance
55
+ expect(chunk.metadata.totalChunks).toBe(doc.chunks.length);
56
+ }
57
+ });
58
+
59
+ it('should reject files over 50MB', async () => {
60
+ const bigBuffer = Buffer.alloc(51 * 1024 * 1024);
61
+ await expect(processor.process(bigBuffer, 'huge.txt')).rejects.toThrow('too large');
62
+ });
63
+
64
+ it('should handle empty content', async () => {
65
+ const buffer = Buffer.from('', 'utf-8');
66
+ const doc = await processor.process(buffer, 'empty.txt');
67
+ expect(doc.chunks.length).toBe(0);
68
+ });
69
+ });