pikiloop 0.4.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/LICENSE +21 -0
- package/README.md +353 -0
- package/README.v2.md +287 -0
- package/README.zh-CN.md +352 -0
- package/dashboard/dist/assets/AgentTab-UZPIhlkr.js +1 -0
- package/dashboard/dist/assets/DirBrowser-Ckcmi-Pi.js +1 -0
- package/dashboard/dist/assets/ExtensionsTab-KZhEDrdu.js +1 -0
- package/dashboard/dist/assets/IMAccessTab-Bd_IY1GQ.js +1 -0
- package/dashboard/dist/assets/Modal-CTeL0y7P.js +1 -0
- package/dashboard/dist/assets/Modals-axftHasy.js +1 -0
- package/dashboard/dist/assets/Select-C8tOdPhe.js +1 -0
- package/dashboard/dist/assets/SessionPanel-C1geSRxw.js +1 -0
- package/dashboard/dist/assets/SystemTab-DBDkaPiO.js +1 -0
- package/dashboard/dist/assets/anthropic-BAdojD7P.ico +0 -0
- package/dashboard/dist/assets/codex-DYadqqp0.png +0 -0
- package/dashboard/dist/assets/deepseek-BeYNZEk0.ico +0 -0
- package/dashboard/dist/assets/doubao-DloFDuFR.png +0 -0
- package/dashboard/dist/assets/feishu-C4OMrjCW.ico +0 -0
- package/dashboard/dist/assets/gemini-BYkEpiWr.svg +1 -0
- package/dashboard/dist/assets/hermes-BAarh-tH.png +0 -0
- package/dashboard/dist/assets/index-CpM4CqZJ.js +23 -0
- package/dashboard/dist/assets/index-DXSohzrE.js +3 -0
- package/dashboard/dist/assets/index-reSbuley.css +1 -0
- package/dashboard/dist/assets/markdown-DxQYQFeH.js +29 -0
- package/dashboard/dist/assets/minimax-PuEGTfrF.ico +0 -0
- package/dashboard/dist/assets/mlx-DhWwjtMw.png +0 -0
- package/dashboard/dist/assets/ollama-Bt9O-2K_.png +0 -0
- package/dashboard/dist/assets/openrouter-CsJ_bD5Q.ico +0 -0
- package/dashboard/dist/assets/playwright-BldPFZgC.ico +0 -0
- package/dashboard/dist/assets/qwen-xykkX0_y.png +0 -0
- package/dashboard/dist/assets/react-vendor-C7Sl8SE7.js +9 -0
- package/dashboard/dist/assets/router-DHISdpPk.js +3 -0
- package/dashboard/dist/assets/shared-BIP_4k4I.js +1 -0
- package/dashboard/dist/favicon.svg +28 -0
- package/dashboard/dist/index.html +17 -0
- package/dist/agent/acp-client.js +261 -0
- package/dist/agent/auto-update.js +432 -0
- package/dist/agent/await-resume.js +50 -0
- package/dist/agent/cli/auth.js +325 -0
- package/dist/agent/cli/catalog.js +40 -0
- package/dist/agent/cli/detector.js +136 -0
- package/dist/agent/cli/index.js +7 -0
- package/dist/agent/cli/registry.js +33 -0
- package/dist/agent/driver.js +39 -0
- package/dist/agent/drivers/claude-tui.js +2297 -0
- package/dist/agent/drivers/claude.js +2689 -0
- package/dist/agent/drivers/codex.js +2210 -0
- package/dist/agent/drivers/gemini.js +1059 -0
- package/dist/agent/drivers/hermes.js +795 -0
- package/dist/agent/goal.js +274 -0
- package/dist/agent/handover.js +130 -0
- package/dist/agent/images.js +355 -0
- package/dist/agent/index.js +50 -0
- package/dist/agent/mcp/bridge.js +791 -0
- package/dist/agent/mcp/extensions.js +637 -0
- package/dist/agent/mcp/oauth.js +353 -0
- package/dist/agent/mcp/registry.js +119 -0
- package/dist/agent/mcp/session-server.js +229 -0
- package/dist/agent/mcp/tools/ask-user.js +113 -0
- package/dist/agent/mcp/tools/await-resume.js +77 -0
- package/dist/agent/mcp/tools/goal.js +144 -0
- package/dist/agent/mcp/tools/types.js +12 -0
- package/dist/agent/mcp/tools/workspace.js +212 -0
- package/dist/agent/npm.js +31 -0
- package/dist/agent/session.js +1206 -0
- package/dist/agent/skill-installer.js +160 -0
- package/dist/agent/skills.js +257 -0
- package/dist/agent/stream.js +743 -0
- package/dist/agent/types.js +13 -0
- package/dist/agent/utils.js +687 -0
- package/dist/bot/bot.js +2499 -0
- package/dist/bot/command-ui.js +633 -0
- package/dist/bot/commands.js +513 -0
- package/dist/bot/headless-bot.js +36 -0
- package/dist/bot/host.js +192 -0
- package/dist/bot/human-loop.js +168 -0
- package/dist/bot/menu.js +48 -0
- package/dist/bot/orchestration.js +79 -0
- package/dist/bot/render-shared.js +309 -0
- package/dist/bot/session-hub.js +361 -0
- package/dist/bot/session-status.js +55 -0
- package/dist/bot/streaming.js +309 -0
- package/dist/browser-profile.js +579 -0
- package/dist/browser-supervisor.js +249 -0
- package/dist/catalog/cli-tools.js +421 -0
- package/dist/catalog/index.js +21 -0
- package/dist/catalog/local-models.js +94 -0
- package/dist/catalog/mcp-servers.js +315 -0
- package/dist/catalog/skill-repos.js +173 -0
- package/dist/channels/base.js +55 -0
- package/dist/channels/dingtalk/bot.js +549 -0
- package/dist/channels/dingtalk/channel.js +268 -0
- package/dist/channels/discord/bot.js +552 -0
- package/dist/channels/discord/channel.js +245 -0
- package/dist/channels/feishu/bot.js +1275 -0
- package/dist/channels/feishu/channel.js +911 -0
- package/dist/channels/feishu/markdown.js +91 -0
- package/dist/channels/feishu/render.js +619 -0
- package/dist/channels/health.js +109 -0
- package/dist/channels/slack/bot.js +554 -0
- package/dist/channels/slack/channel.js +283 -0
- package/dist/channels/states.js +6 -0
- package/dist/channels/telegram/bot.js +1310 -0
- package/dist/channels/telegram/channel.js +820 -0
- package/dist/channels/telegram/directory.js +111 -0
- package/dist/channels/telegram/live-preview.js +220 -0
- package/dist/channels/telegram/render.js +384 -0
- package/dist/channels/wecom/bot.js +558 -0
- package/dist/channels/wecom/channel.js +479 -0
- package/dist/channels/weixin/api.js +520 -0
- package/dist/channels/weixin/bot.js +1000 -0
- package/dist/channels/weixin/channel.js +222 -0
- package/dist/cli/autostart.js +262 -0
- package/dist/cli/channel-supervisor.js +313 -0
- package/dist/cli/channels.js +54 -0
- package/dist/cli/main.js +726 -0
- package/dist/cli/onboarding.js +227 -0
- package/dist/cli/run.js +308 -0
- package/dist/cli/setup-wizard.js +235 -0
- package/dist/core/config/runtime-config.js +201 -0
- package/dist/core/config/user-config.js +510 -0
- package/dist/core/config/validation.js +521 -0
- package/dist/core/constants.js +400 -0
- package/dist/core/git.js +145 -0
- package/dist/core/legacy-compat.js +60 -0
- package/dist/core/logging.js +101 -0
- package/dist/core/platform.js +59 -0
- package/dist/core/process-control.js +315 -0
- package/dist/core/secrets/index.js +42 -0
- package/dist/core/secrets/inline-seal.js +60 -0
- package/dist/core/secrets/ref.js +33 -0
- package/dist/core/secrets/resolver.js +65 -0
- package/dist/core/secrets/store.js +63 -0
- package/dist/core/utils.js +233 -0
- package/dist/core/version.js +15 -0
- package/dist/dashboard/platform.js +219 -0
- package/dist/dashboard/routes/agents.js +450 -0
- package/dist/dashboard/routes/cli.js +174 -0
- package/dist/dashboard/routes/config.js +523 -0
- package/dist/dashboard/routes/extensions.js +745 -0
- package/dist/dashboard/routes/local-models.js +290 -0
- package/dist/dashboard/routes/models.js +324 -0
- package/dist/dashboard/routes/sessions.js +838 -0
- package/dist/dashboard/runtime.js +410 -0
- package/dist/dashboard/server.js +237 -0
- package/dist/dashboard/session-control.js +347 -0
- package/dist/model/catalog.js +104 -0
- package/dist/model/index.js +20 -0
- package/dist/model/injector.js +272 -0
- package/dist/model/provider-models.js +112 -0
- package/dist/model/store.js +212 -0
- package/dist/model/types.js +13 -0
- package/dist/model/validation.js +203 -0
- package/package.json +82 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DingTalk channel transport — Stream Mode (no public IP needed).
|
|
3
|
+
*
|
|
4
|
+
* Uses dingtalk-stream@2.x SDK. Auth: clientId (AppKey) + clientSecret
|
|
5
|
+
* (AppSecret) of a self-built robot/app subscribed to the bot message topic.
|
|
6
|
+
*
|
|
7
|
+
* Receive: DWClient subscribes to TOPIC_ROBOT and yields RobotTextMessage
|
|
8
|
+
* payloads via registerCallbackListener.
|
|
9
|
+
*
|
|
10
|
+
* Send: each inbound message carries a `sessionWebhook` URL valid for ~2 hours,
|
|
11
|
+
* which we POST to for replies. We cache the latest sessionWebhook per chat
|
|
12
|
+
* so a long-running task that produces a result message can still reach the
|
|
13
|
+
* user even after the original webhook would have expired (mid-conversation
|
|
14
|
+
* activity refreshes it). DingTalk plain-text messages cannot be edited.
|
|
15
|
+
*/
|
|
16
|
+
import { DWClient, TOPIC_ROBOT, EventAck } from 'dingtalk-stream';
|
|
17
|
+
import { Channel, DEFAULT_CHANNEL_CAPABILITIES, splitText, sleep, } from '../base.js';
|
|
18
|
+
import { DINGTALK_LIMITS } from '../../core/constants.js';
|
|
19
|
+
import { ChannelHealth } from '../health.js';
|
|
20
|
+
import { writeScopedLog } from '../../core/logging.js';
|
|
21
|
+
const DT_MAX = DINGTALK_LIMITS.maxMessageLength;
|
|
22
|
+
function describeError(err) {
|
|
23
|
+
return err instanceof Error ? err.message : String(err ?? 'unknown error');
|
|
24
|
+
}
|
|
25
|
+
function previewText(value, max = 200) {
|
|
26
|
+
const t = value.replace(/\s+/g, ' ').trim();
|
|
27
|
+
return t.length > max ? `${t.slice(0, max)}...` : t || '(empty)';
|
|
28
|
+
}
|
|
29
|
+
export class DingtalkChannel extends Channel {
|
|
30
|
+
capabilities = {
|
|
31
|
+
...DEFAULT_CHANNEL_CAPABILITIES,
|
|
32
|
+
// DingTalk Stream Mode plain-text messages cannot be edited; AI Cards
|
|
33
|
+
// can be updated but are out of scope for this minimal channel.
|
|
34
|
+
};
|
|
35
|
+
knownChats = new Set();
|
|
36
|
+
clientId;
|
|
37
|
+
clientSecret;
|
|
38
|
+
allowedChatIds;
|
|
39
|
+
dwClient = null;
|
|
40
|
+
running = false;
|
|
41
|
+
listenResolve = null;
|
|
42
|
+
chatMeta = new Map();
|
|
43
|
+
messageHandlers = new Set();
|
|
44
|
+
errorHandlers = new Set();
|
|
45
|
+
/** Dedup: stream may redeliver after timeouts. */
|
|
46
|
+
seenMsgIds = new Set();
|
|
47
|
+
seenMsgIdQueue = [];
|
|
48
|
+
static SEEN_CAP = 256;
|
|
49
|
+
constructor(opts) {
|
|
50
|
+
super();
|
|
51
|
+
this.clientId = opts.clientId;
|
|
52
|
+
this.clientSecret = opts.clientSecret;
|
|
53
|
+
this.allowedChatIds = opts.allowedChatIds;
|
|
54
|
+
}
|
|
55
|
+
onMessage(handler) { this.messageHandlers.add(handler); return this; }
|
|
56
|
+
onError(handler) { this.errorHandlers.add(handler); return this; }
|
|
57
|
+
// ========================================================================
|
|
58
|
+
// Lifecycle
|
|
59
|
+
// ========================================================================
|
|
60
|
+
async connect() {
|
|
61
|
+
this.dwClient = new DWClient({
|
|
62
|
+
clientId: this.clientId,
|
|
63
|
+
clientSecret: this.clientSecret,
|
|
64
|
+
keepAlive: true,
|
|
65
|
+
});
|
|
66
|
+
let lastErr = null;
|
|
67
|
+
for (let attempt = 1; attempt <= 5; attempt++) {
|
|
68
|
+
try {
|
|
69
|
+
await this.dwClient.getAccessToken();
|
|
70
|
+
const shortId = this.clientId.length > 12
|
|
71
|
+
? `${this.clientId.slice(0, 6)}...${this.clientId.slice(-4)}`
|
|
72
|
+
: this.clientId;
|
|
73
|
+
this.bot = {
|
|
74
|
+
id: this.clientId,
|
|
75
|
+
username: `dingtalk_${shortId}`,
|
|
76
|
+
displayName: `DingTalk ${shortId}`,
|
|
77
|
+
};
|
|
78
|
+
return this.bot;
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
lastErr = err;
|
|
82
|
+
if (attempt >= 5)
|
|
83
|
+
break;
|
|
84
|
+
await sleep(Math.min(1000 * attempt, 5_000));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
throw new Error(`DingTalk connect failed: ${describeError(lastErr)}`);
|
|
88
|
+
}
|
|
89
|
+
async listen() {
|
|
90
|
+
if (!this.dwClient)
|
|
91
|
+
throw new Error('DingTalk channel not connected');
|
|
92
|
+
this.running = true;
|
|
93
|
+
this.dwClient.registerCallbackListener(TOPIC_ROBOT, (msg) => {
|
|
94
|
+
void this.dispatchRobotMessage(msg).catch(error => {
|
|
95
|
+
this.emitError(error instanceof Error ? error : new Error(describeError(error)));
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
const health = new ChannelHealth({
|
|
99
|
+
label: 'DingTalk',
|
|
100
|
+
opAction: 'WS connect',
|
|
101
|
+
initialDelayMs: DINGTALK_LIMITS.initialRetryDelay,
|
|
102
|
+
maxDelayMs: DINGTALK_LIMITS.maxRetryDelay,
|
|
103
|
+
sustainedFailureHint: 'verify dingtalkClientId / dingtalkClientSecret in setting.json',
|
|
104
|
+
log: (msg, level) => this.log(msg, level),
|
|
105
|
+
});
|
|
106
|
+
while (this.running) {
|
|
107
|
+
try {
|
|
108
|
+
await this.dwClient.connect();
|
|
109
|
+
health.recordSuccess();
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
if (!this.running)
|
|
114
|
+
return;
|
|
115
|
+
await sleep(health.recordFailure(err));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (!this.running)
|
|
119
|
+
return;
|
|
120
|
+
await new Promise(resolve => {
|
|
121
|
+
this.listenResolve = resolve;
|
|
122
|
+
if (!this.running)
|
|
123
|
+
resolve();
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
disconnect() {
|
|
127
|
+
this.running = false;
|
|
128
|
+
try {
|
|
129
|
+
this.dwClient?.disconnect();
|
|
130
|
+
}
|
|
131
|
+
catch { }
|
|
132
|
+
this.dwClient = null;
|
|
133
|
+
this.listenResolve?.();
|
|
134
|
+
this.listenResolve = null;
|
|
135
|
+
}
|
|
136
|
+
// ========================================================================
|
|
137
|
+
// Outgoing primitives
|
|
138
|
+
// ========================================================================
|
|
139
|
+
async send(chatId, text, _opts = {}) {
|
|
140
|
+
const chat = String(chatId);
|
|
141
|
+
const meta = this.chatMeta.get(chat);
|
|
142
|
+
if (!meta?.sessionWebhook) {
|
|
143
|
+
throw new Error(`DingTalk has no active sessionWebhook for chat ${chat}; user must send a message first.`);
|
|
144
|
+
}
|
|
145
|
+
if (meta.sessionWebhookExpiredAt && Date.now() > meta.sessionWebhookExpiredAt) {
|
|
146
|
+
this.debug(`[send] sessionWebhook for ${chat} likely expired — attempting anyway`);
|
|
147
|
+
}
|
|
148
|
+
const trimmed = (text || '').trim() || '(empty)';
|
|
149
|
+
const chunks = splitText(trimmed, DT_MAX);
|
|
150
|
+
let lastId = null;
|
|
151
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
152
|
+
const chunk = chunks[i];
|
|
153
|
+
this.debug(`[send] chat=${chat} chunk=${i + 1}/${chunks.length} chars=${chunk.length} preview=${previewText(chunk)}`);
|
|
154
|
+
const resp = await fetch(meta.sessionWebhook, {
|
|
155
|
+
method: 'POST',
|
|
156
|
+
headers: { 'Content-Type': 'application/json' },
|
|
157
|
+
body: JSON.stringify({ msgtype: 'text', text: { content: chunk } }),
|
|
158
|
+
});
|
|
159
|
+
const body = await resp.text();
|
|
160
|
+
if (!resp.ok) {
|
|
161
|
+
throw new Error(`DingTalk reply failed: HTTP ${resp.status} ${resp.statusText || ''}; body=${previewText(body)}`);
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const data = JSON.parse(body);
|
|
165
|
+
if (typeof data?.errcode === 'number' && data.errcode !== 0) {
|
|
166
|
+
throw new Error(`DingTalk reply errcode=${data.errcode} errmsg=${data.errmsg || 'unknown'}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
if (err instanceof Error && err.message.startsWith('DingTalk reply errcode='))
|
|
171
|
+
throw err;
|
|
172
|
+
// Non-JSON body or no errcode is treated as success.
|
|
173
|
+
}
|
|
174
|
+
lastId = `dt:${Date.now().toString(36)}:${i}`;
|
|
175
|
+
}
|
|
176
|
+
return lastId;
|
|
177
|
+
}
|
|
178
|
+
async editMessage(_chatId, _msgId, _text, _opts) {
|
|
179
|
+
// Plain-text messages on DingTalk are not editable. AI Cards have an
|
|
180
|
+
// update API but require pre-registering a card template — out of scope
|
|
181
|
+
// for this transport. Callers that fall back here just no-op.
|
|
182
|
+
}
|
|
183
|
+
async deleteMessage(_chatId, _msgId) {
|
|
184
|
+
// No public delete API for bot-sent messages.
|
|
185
|
+
}
|
|
186
|
+
async sendTyping(_chatId, _opts) {
|
|
187
|
+
// No typing indicator API.
|
|
188
|
+
}
|
|
189
|
+
// ========================================================================
|
|
190
|
+
// Internal dispatch
|
|
191
|
+
// ========================================================================
|
|
192
|
+
async dispatchRobotMessage(msg) {
|
|
193
|
+
try {
|
|
194
|
+
this.dwClient?.socketCallBackResponse(msg.headers.messageId, { status: EventAck.SUCCESS });
|
|
195
|
+
}
|
|
196
|
+
catch { }
|
|
197
|
+
let parsed;
|
|
198
|
+
try {
|
|
199
|
+
parsed = JSON.parse(msg.data);
|
|
200
|
+
}
|
|
201
|
+
catch (err) {
|
|
202
|
+
this.emitError(new Error(`DingTalk message parse failed: ${describeError(err)}`));
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (!parsed)
|
|
206
|
+
return;
|
|
207
|
+
const chatId = String(parsed.conversationId || '').trim();
|
|
208
|
+
const messageId = String(parsed.msgId || '').trim();
|
|
209
|
+
const senderId = String(parsed.senderStaffId || parsed.senderId || '').trim();
|
|
210
|
+
if (!chatId || !messageId || !senderId)
|
|
211
|
+
return;
|
|
212
|
+
if (this.seenMsgIds.has(messageId))
|
|
213
|
+
return;
|
|
214
|
+
this.seenMsgIds.add(messageId);
|
|
215
|
+
this.seenMsgIdQueue.push(messageId);
|
|
216
|
+
while (this.seenMsgIdQueue.length > DingtalkChannel.SEEN_CAP) {
|
|
217
|
+
this.seenMsgIds.delete(this.seenMsgIdQueue.shift());
|
|
218
|
+
}
|
|
219
|
+
if (!this.isAllowed(chatId))
|
|
220
|
+
return;
|
|
221
|
+
this.knownChats.add(chatId);
|
|
222
|
+
if (parsed.sessionWebhook) {
|
|
223
|
+
this.chatMeta.set(chatId, {
|
|
224
|
+
sessionWebhook: parsed.sessionWebhook,
|
|
225
|
+
sessionWebhookExpiredAt: Number(parsed.sessionWebhookExpiredTime) || Date.now() + 60 * 60_000,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
let text = '';
|
|
229
|
+
if (parsed.msgtype === 'text' && parsed.text?.content)
|
|
230
|
+
text = String(parsed.text.content || '').trim();
|
|
231
|
+
else
|
|
232
|
+
this.debug(`[recv] non-text msgtype=${parsed.msgtype} chat=${chatId} msg=${messageId}`);
|
|
233
|
+
const ctx = {
|
|
234
|
+
chatId,
|
|
235
|
+
messageId,
|
|
236
|
+
conversationType: String(parsed.conversationType || ''),
|
|
237
|
+
from: { userId: senderId, displayName: parsed.senderNick },
|
|
238
|
+
reply: (replyText, opts) => this.send(chatId, replyText, opts),
|
|
239
|
+
editReply: () => Promise.resolve(),
|
|
240
|
+
channel: this,
|
|
241
|
+
raw: parsed,
|
|
242
|
+
};
|
|
243
|
+
const payload = { text, files: [] };
|
|
244
|
+
for (const handler of this.messageHandlers) {
|
|
245
|
+
try {
|
|
246
|
+
await handler(payload, ctx);
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
this.emitError(error instanceof Error ? error : new Error(describeError(error)));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
isAllowed(chatId) {
|
|
254
|
+
if (!this.allowedChatIds?.size)
|
|
255
|
+
return true;
|
|
256
|
+
return this.allowedChatIds.has(chatId);
|
|
257
|
+
}
|
|
258
|
+
emitError(error) {
|
|
259
|
+
for (const handler of this.errorHandlers) {
|
|
260
|
+
try {
|
|
261
|
+
handler(error);
|
|
262
|
+
}
|
|
263
|
+
catch { }
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
debug(msg) { this.log(msg, 'debug'); }
|
|
267
|
+
log(msg, level = 'info') { writeScopedLog('dingtalk', msg, { level }); }
|
|
268
|
+
}
|