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,34 @@
1
+ import { BaseChannel } from './index';
2
+ import type { Message } from '../core/types';
3
+
4
+ export interface MatrixChannelConfig {
5
+ homeserverUrl?: string;
6
+ accessToken?: string;
7
+ userId?: string;
8
+ }
9
+
10
+ export class MatrixChannel extends BaseChannel {
11
+ readonly type = 'matrix';
12
+ private config: MatrixChannelConfig;
13
+
14
+ constructor(config: MatrixChannelConfig = {}) {
15
+ super();
16
+ this.config = config;
17
+ }
18
+
19
+ async start(): Promise<void> {
20
+ try {
21
+ require('matrix-js-sdk');
22
+ } catch {
23
+ throw new Error('Install matrix-js-sdk to use the MatrixChannel. Run: npm install matrix-js-sdk');
24
+ }
25
+ }
26
+
27
+ async stop(): Promise<void> {
28
+ // cleanup
29
+ }
30
+
31
+ async send(chatId: string, text: string): Promise<void> {
32
+ throw new Error('MatrixChannel: not yet connected. Call start() first.');
33
+ }
34
+ }
@@ -0,0 +1,57 @@
1
+ import { BaseChannel } from './index';
2
+ import type { Message } from '../core/types';
3
+
4
+ export interface MattermostChannelConfig {
5
+ serverUrl: string;
6
+ token: string;
7
+ teamId?: string;
8
+ defaultChannelId?: string;
9
+ }
10
+
11
+ export class MattermostChannel extends BaseChannel {
12
+ readonly type = 'mattermost';
13
+ private config: MattermostChannelConfig;
14
+ private running = false;
15
+ private ws: any = null;
16
+
17
+ constructor(config: MattermostChannelConfig) {
18
+ super();
19
+ if (!config.serverUrl || !config.token) {
20
+ throw new Error('MattermostChannel requires serverUrl and token in config');
21
+ }
22
+ this.config = config;
23
+ }
24
+
25
+ async start(): Promise<void> {
26
+ // Verify connection by hitting the API
27
+ const res = await fetch(`${this.config.serverUrl}/api/v4/users/me`, {
28
+ headers: { Authorization: `Bearer ${this.config.token}` },
29
+ }).catch(() => null);
30
+ this.running = true;
31
+ }
32
+
33
+ async stop(): Promise<void> {
34
+ if (this.ws) {
35
+ this.ws.close();
36
+ this.ws = null;
37
+ }
38
+ this.running = false;
39
+ }
40
+
41
+ async send(channelId: string, text: string): Promise<void> {
42
+ if (!this.running) {
43
+ throw new Error('MattermostChannel: not started. Call start() first.');
44
+ }
45
+ const res = await fetch(`${this.config.serverUrl}/api/v4/posts`, {
46
+ method: 'POST',
47
+ headers: {
48
+ Authorization: `Bearer ${this.config.token}`,
49
+ 'Content-Type': 'application/json',
50
+ },
51
+ body: JSON.stringify({ channel_id: channelId, message: text }),
52
+ });
53
+ if (!res.ok) {
54
+ throw new Error(`Mattermost API failed: ${res.status}`);
55
+ }
56
+ }
57
+ }
@@ -0,0 +1,33 @@
1
+ import { BaseChannel } from './index';
2
+ import type { Message } from '../core/types';
3
+
4
+ export interface MSTeamsChannelConfig {
5
+ appId?: string;
6
+ appPassword?: string;
7
+ }
8
+
9
+ export class MSTeamsChannel extends BaseChannel {
10
+ readonly type = 'msteams';
11
+ private config: MSTeamsChannelConfig;
12
+
13
+ constructor(config: MSTeamsChannelConfig = {}) {
14
+ super();
15
+ this.config = config;
16
+ }
17
+
18
+ async start(): Promise<void> {
19
+ try {
20
+ require('botframework-connector');
21
+ } catch {
22
+ throw new Error('Install botframework-connector to use the MSTeamsChannel. Run: npm install botframework-connector');
23
+ }
24
+ }
25
+
26
+ async stop(): Promise<void> {
27
+ // cleanup
28
+ }
29
+
30
+ async send(chatId: string, text: string): Promise<void> {
31
+ throw new Error('MSTeamsChannel: not yet connected. Call start() first.');
32
+ }
33
+ }
@@ -0,0 +1,33 @@
1
+ import { BaseChannel } from './index';
2
+ import type { Message } from '../core/types';
3
+
4
+ export interface NostrChannelConfig {
5
+ privateKey?: string;
6
+ relays?: string[];
7
+ }
8
+
9
+ export class NostrChannel extends BaseChannel {
10
+ readonly type = 'nostr';
11
+ private config: NostrChannelConfig;
12
+
13
+ constructor(config: NostrChannelConfig = {}) {
14
+ super();
15
+ this.config = config;
16
+ }
17
+
18
+ async start(): Promise<void> {
19
+ try {
20
+ require('nostr-tools');
21
+ } catch {
22
+ throw new Error('Install nostr-tools to use the NostrChannel. Run: npm install nostr-tools');
23
+ }
24
+ }
25
+
26
+ async stop(): Promise<void> {
27
+ // cleanup
28
+ }
29
+
30
+ async send(chatId: string, text: string): Promise<void> {
31
+ throw new Error('NostrChannel: not yet connected. Call start() first.');
32
+ }
33
+ }
@@ -0,0 +1,34 @@
1
+ import { BaseChannel } from './index';
2
+ import type { Message } from '../core/types';
3
+
4
+ export interface QQChannelConfig {
5
+ appId?: string;
6
+ token?: string;
7
+ sandbox?: boolean;
8
+ }
9
+
10
+ export class QQChannel extends BaseChannel {
11
+ readonly type = 'qq';
12
+ private config: QQChannelConfig;
13
+
14
+ constructor(config: QQChannelConfig = {}) {
15
+ super();
16
+ this.config = config;
17
+ }
18
+
19
+ async start(): Promise<void> {
20
+ try {
21
+ require('qq-bot-sdk');
22
+ } catch {
23
+ throw new Error('Install qq-bot-sdk to use the QQChannel. Run: npm install qq-bot-sdk');
24
+ }
25
+ }
26
+
27
+ async stop(): Promise<void> {
28
+ // cleanup
29
+ }
30
+
31
+ async send(chatId: string, text: string): Promise<void> {
32
+ throw new Error('QQChannel: not yet connected. Call start() first.');
33
+ }
34
+ }
@@ -0,0 +1,33 @@
1
+ import { BaseChannel } from './index';
2
+ import type { Message } from '../core/types';
3
+
4
+ export interface SignalChannelConfig {
5
+ signalCliPath?: string;
6
+ phoneNumber?: string;
7
+ }
8
+
9
+ export class SignalChannel extends BaseChannel {
10
+ readonly type = 'signal';
11
+ private config: SignalChannelConfig;
12
+
13
+ constructor(config: SignalChannelConfig = {}) {
14
+ super();
15
+ this.config = config;
16
+ }
17
+
18
+ async start(): Promise<void> {
19
+ try {
20
+ require('signal-cli');
21
+ } catch {
22
+ throw new Error('Install signal-cli to use the SignalChannel. Run: npm install signal-cli');
23
+ }
24
+ }
25
+
26
+ async stop(): Promise<void> {
27
+ // cleanup
28
+ }
29
+
30
+ async send(chatId: string, text: string): Promise<void> {
31
+ throw new Error('SignalChannel: not yet connected. Call start() first.');
32
+ }
33
+ }
@@ -0,0 +1,34 @@
1
+ import { BaseChannel } from './index';
2
+ import type { Message } from '../core/types';
3
+
4
+ export interface SMSChannelConfig {
5
+ accountSid?: string;
6
+ authToken?: string;
7
+ fromNumber?: string;
8
+ }
9
+
10
+ export class SMSChannel extends BaseChannel {
11
+ readonly type = 'sms';
12
+ private config: SMSChannelConfig;
13
+
14
+ constructor(config: SMSChannelConfig = {}) {
15
+ super();
16
+ this.config = config;
17
+ }
18
+
19
+ async start(): Promise<void> {
20
+ try {
21
+ require('twilio');
22
+ } catch {
23
+ throw new Error('Install twilio to use the SMSChannel. Run: npm install twilio');
24
+ }
25
+ }
26
+
27
+ async stop(): Promise<void> {
28
+ // cleanup
29
+ }
30
+
31
+ async send(chatId: string, text: string): Promise<void> {
32
+ throw new Error('SMSChannel: not yet connected. Call start() first.');
33
+ }
34
+ }
@@ -0,0 +1,65 @@
1
+ import { BaseChannel } from './index';
2
+ import type { Message } from '../core/types';
3
+
4
+ export interface TwitchChannelConfig {
5
+ username: string;
6
+ oauthToken: string;
7
+ channels: string[];
8
+ }
9
+
10
+ export class TwitchChannel extends BaseChannel {
11
+ readonly type = 'twitch';
12
+ private config: TwitchChannelConfig;
13
+ private client: any = null;
14
+ private running = false;
15
+
16
+ constructor(config: TwitchChannelConfig) {
17
+ super();
18
+ if (!config.username || !config.oauthToken || !config.channels?.length) {
19
+ throw new Error('TwitchChannel requires username, oauthToken, and channels in config');
20
+ }
21
+ this.config = config;
22
+ }
23
+
24
+ async start(): Promise<void> {
25
+ let tmi: any;
26
+ try {
27
+ tmi = require('tmi.js');
28
+ } catch {
29
+ throw new Error('Install tmi.js to use the TwitchChannel. Run: npm install tmi.js');
30
+ }
31
+ this.client = new tmi.Client({
32
+ identity: { username: this.config.username, password: this.config.oauthToken },
33
+ channels: this.config.channels,
34
+ });
35
+ await this.client.connect();
36
+ this.running = true;
37
+
38
+ this.client.on('message', async (channel: string, tags: any, message: string, self: boolean) => {
39
+ if (self || !this.handler) return;
40
+ const msg: Message = {
41
+ id: tags.id || Date.now().toString(),
42
+ role: 'user',
43
+ content: message,
44
+ timestamp: Date.now(),
45
+ metadata: { channel, username: tags.username, tags },
46
+ };
47
+ await this.handler(msg);
48
+ });
49
+ }
50
+
51
+ async stop(): Promise<void> {
52
+ if (this.client) {
53
+ await this.client.disconnect();
54
+ this.client = null;
55
+ }
56
+ this.running = false;
57
+ }
58
+
59
+ async send(channel: string, text: string): Promise<void> {
60
+ if (!this.running || !this.client) {
61
+ throw new Error('TwitchChannel: not started. Call start() first.');
62
+ }
63
+ await this.client.say(channel, text);
64
+ }
65
+ }
@@ -0,0 +1,100 @@
1
+ import { EventEmitter } from 'events';
2
+ import { randomUUID } from 'crypto';
3
+
4
+ export interface VoiceCallConfig {
5
+ provider: 'twilio' | 'vonage' | 'webrtc' | 'sip';
6
+ credentials?: Record<string, string>;
7
+ sttProvider?: string;
8
+ ttsProvider?: string;
9
+ }
10
+
11
+ interface ActiveCall {
12
+ callId: string;
13
+ from: string;
14
+ to: string;
15
+ status: 'ringing' | 'active' | 'ended';
16
+ startedAt: number;
17
+ }
18
+
19
+ export class VoiceCallManager extends EventEmitter {
20
+ private config: VoiceCallConfig;
21
+ private calls = new Map<string, ActiveCall>();
22
+ private audioListeners = new Map<string, ((audio: Buffer) => void)[]>();
23
+
24
+ constructor(config: VoiceCallConfig) {
25
+ super();
26
+ this.config = config;
27
+ }
28
+
29
+ private ensureCredentials(): void {
30
+ if (!this.config.credentials || Object.keys(this.config.credentials).length === 0) {
31
+ throw new Error(
32
+ `Voice call provider "${this.config.provider}" requires credentials. ` +
33
+ `Please configure credentials for ${this.config.provider} in your VoiceCallConfig.`
34
+ );
35
+ }
36
+ }
37
+
38
+ async startCall(to: string): Promise<string> {
39
+ this.ensureCredentials();
40
+ const callId = randomUUID();
41
+ const call: ActiveCall = {
42
+ callId,
43
+ from: 'self',
44
+ to,
45
+ status: 'ringing',
46
+ startedAt: Date.now(),
47
+ };
48
+ this.calls.set(callId, call);
49
+ // Simulate connection
50
+ setTimeout(() => {
51
+ const c = this.calls.get(callId);
52
+ if (c && c.status === 'ringing') c.status = 'active';
53
+ }, 100);
54
+ return callId;
55
+ }
56
+
57
+ async endCall(callId: string): Promise<void> {
58
+ const call = this.calls.get(callId);
59
+ if (!call) throw new Error(`Call ${callId} not found`);
60
+ call.status = 'ended';
61
+ this.audioListeners.delete(callId);
62
+ }
63
+
64
+ onIncoming(callback: (callId: string, from: string) => void): void {
65
+ this.on('incoming', callback);
66
+ }
67
+
68
+ simulateIncoming(from: string): string {
69
+ const callId = randomUUID();
70
+ const call: ActiveCall = { callId, from, to: 'self', status: 'ringing', startedAt: Date.now() };
71
+ this.calls.set(callId, call);
72
+ this.emit('incoming', callId, from);
73
+ return callId;
74
+ }
75
+
76
+ async sendAudio(callId: string, audio: Buffer): Promise<void> {
77
+ const call = this.calls.get(callId);
78
+ if (!call) throw new Error(`Call ${callId} not found`);
79
+ if (call.status !== 'active') throw new Error(`Call ${callId} is not active`);
80
+ // Stub: would send audio to provider
81
+ }
82
+
83
+ onAudio(callId: string, callback: (audio: Buffer) => void): void {
84
+ const listeners = this.audioListeners.get(callId) || [];
85
+ listeners.push(callback);
86
+ this.audioListeners.set(callId, listeners);
87
+ }
88
+
89
+ getCallStatus(callId: string): 'ringing' | 'active' | 'ended' {
90
+ const call = this.calls.get(callId);
91
+ if (!call) throw new Error(`Call ${callId} not found`);
92
+ return call.status;
93
+ }
94
+
95
+ listActiveCalls(): Array<{ callId: string; from: string; startedAt: number }> {
96
+ return Array.from(this.calls.values())
97
+ .filter(c => c.status !== 'ended')
98
+ .map(({ callId, from, startedAt }) => ({ callId, from, startedAt }));
99
+ }
100
+ }
@@ -0,0 +1,33 @@
1
+ import { BaseChannel } from './index';
2
+ import type { Message } from '../core/types';
3
+
4
+ export interface WhatsAppChannelConfig {
5
+ phoneNumber?: string;
6
+ authDir?: string;
7
+ }
8
+
9
+ export class WhatsAppChannel extends BaseChannel {
10
+ readonly type = 'whatsapp';
11
+ private config: WhatsAppChannelConfig;
12
+
13
+ constructor(config: WhatsAppChannelConfig = {}) {
14
+ super();
15
+ this.config = config;
16
+ }
17
+
18
+ async start(): Promise<void> {
19
+ try {
20
+ require('@whiskeysockets/baileys');
21
+ } catch {
22
+ throw new Error('Install @whiskeysockets/baileys to use the WhatsAppChannel. Run: npm install @whiskeysockets/baileys');
23
+ }
24
+ }
25
+
26
+ async stop(): Promise<void> {
27
+ // cleanup
28
+ }
29
+
30
+ async send(chatId: string, text: string): Promise<void> {
31
+ throw new Error('WhatsAppChannel: not yet connected. Call start() first.');
32
+ }
33
+ }
@@ -0,0 +1,99 @@
1
+ import * as readline from 'readline';
2
+ import * as fs from 'fs';
3
+ import * as path from 'path';
4
+ import * as os from 'os';
5
+ import { createProvider } from '../providers';
6
+
7
+ const OPC_HOME = path.join(os.homedir(), '.opc');
8
+ const CONFIG_PATH = path.join(OPC_HOME, 'config.json');
9
+
10
+ const c = {
11
+ green: (s: string) => `\x1b[32m${s}\x1b[0m`,
12
+ red: (s: string) => `\x1b[31m${s}\x1b[0m`,
13
+ cyan: (s: string) => `\x1b[36m${s}\x1b[0m`,
14
+ bold: (s: string) => `\x1b[1m${s}\x1b[0m`,
15
+ dim: (s: string) => `\x1b[2m${s}\x1b[0m`,
16
+ };
17
+
18
+ function loadConfig(): Record<string, any> {
19
+ if (!fs.existsSync(CONFIG_PATH)) return {};
20
+ try { return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8')); } catch { return {}; }
21
+ }
22
+
23
+ export async function runChat(): Promise<void> {
24
+ const config = loadConfig();
25
+
26
+ if (!config.provider || !config.model) {
27
+ console.log(`${c.red('✘')} 未找到配置。请先运行 ${c.cyan('opc setup')} 完成初始设置。`);
28
+ process.exit(1);
29
+ }
30
+
31
+ const agentId = config.defaultAgent;
32
+ let agentConfig: Record<string, any> = {};
33
+ if (agentId) {
34
+ const agentPath = path.join(OPC_HOME, 'agents', agentId, 'config.json');
35
+ if (fs.existsSync(agentPath)) {
36
+ try { agentConfig = JSON.parse(fs.readFileSync(agentPath, 'utf-8')); } catch { /* ignore */ }
37
+ }
38
+ }
39
+
40
+ const agentName = agentConfig.name || 'AI 助手';
41
+ const systemPrompt = agentConfig.description
42
+ ? `你是「${agentName}」,${agentConfig.description}。请用简洁友好的方式回答。`
43
+ : `你是「${agentName}」,一个智能 AI 助手。请用简洁友好的方式回答。`;
44
+
45
+ console.log('');
46
+ console.log(c.bold(`💬 与「${agentName}」对话`));
47
+ console.log(c.dim(' 输入 /quit 退出'));
48
+ console.log('');
49
+
50
+ let provider: any;
51
+ try {
52
+ provider = createProvider({
53
+ provider: config.provider,
54
+ model: config.model,
55
+ apiKey: config.apiKey,
56
+ baseUrl: config.baseUrl,
57
+ } as any);
58
+ } catch (err: any) {
59
+ console.log(`${c.red('✘')} 无法初始化模型: ${err.message}`);
60
+ console.log(c.dim(` Provider: ${config.provider}, Model: ${config.model}`));
61
+ process.exit(1);
62
+ }
63
+
64
+ const messages: Array<{ role: string; content: string }> = [
65
+ { role: 'system', content: systemPrompt },
66
+ ];
67
+
68
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
69
+
70
+ const askQuestion = (): void => {
71
+ rl.question(c.cyan('你: '), async (input: string) => {
72
+ const trimmed = input.trim();
73
+ if (!trimmed) { askQuestion(); return; }
74
+ if (trimmed === '/quit' || trimmed === '/exit' || trimmed === '/q') {
75
+ console.log(c.dim('\n👋 再见!'));
76
+ rl.close();
77
+ return;
78
+ }
79
+
80
+ messages.push({ role: 'user', content: trimmed });
81
+
82
+ try {
83
+ process.stdout.write(c.green(`${agentName}: `));
84
+ const response = await provider.chat(messages);
85
+ const reply = typeof response === 'string' ? response : response?.content || response?.message?.content || JSON.stringify(response);
86
+ console.log(reply);
87
+ console.log('');
88
+ messages.push({ role: 'assistant', content: reply });
89
+ } catch (err: any) {
90
+ console.log(c.red(`\n 错误: ${err.message}`));
91
+ console.log('');
92
+ }
93
+
94
+ askQuestion();
95
+ });
96
+ };
97
+
98
+ askQuestion();
99
+ }