@myassis/gateway 1.0.36 → 1.0.38
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.
|
@@ -474,9 +474,23 @@ class Session {
|
|
|
474
474
|
const sendSSE = (res, data) => {
|
|
475
475
|
if (!res)
|
|
476
476
|
return;
|
|
477
|
-
|
|
478
|
-
|
|
477
|
+
try {
|
|
478
|
+
res.write(`data: ${JSON.stringify(data)}\n\n`);
|
|
479
|
+
res.flush?.();
|
|
480
|
+
}
|
|
481
|
+
catch (error) {
|
|
482
|
+
logger.error('SSE write error:', error);
|
|
483
|
+
}
|
|
479
484
|
};
|
|
485
|
+
// 心跳机制:每 15 秒发送一次心跳,防止连接超时
|
|
486
|
+
const heartbeatInterval = setInterval(() => {
|
|
487
|
+
if (res && !res.destroyed) {
|
|
488
|
+
sendSSE(res, { type: 'heartbeat' });
|
|
489
|
+
}
|
|
490
|
+
else {
|
|
491
|
+
clearInterval(heartbeatInterval);
|
|
492
|
+
}
|
|
493
|
+
}, 15000);
|
|
480
494
|
// Send message start event
|
|
481
495
|
sendSSE(res, { type: 'message_start' });
|
|
482
496
|
// Build messages for API call (保留 tool_call_id 等必要字段)
|
|
@@ -918,17 +932,29 @@ class Session {
|
|
|
918
932
|
this.addAssistantMessage('', toolCalls, [...new Set(modelNames)].join(','), this.currentMessageId);
|
|
919
933
|
}
|
|
920
934
|
if (error.message !== 'aborted') {
|
|
921
|
-
|
|
935
|
+
try {
|
|
936
|
+
sendSSE(res, { type: 'error', error: error.message });
|
|
937
|
+
}
|
|
938
|
+
catch (e) {
|
|
939
|
+
logger.error('Failed to send error event:', e);
|
|
940
|
+
}
|
|
922
941
|
}
|
|
923
942
|
}
|
|
924
943
|
return { success: false, error: error.message };
|
|
925
944
|
}
|
|
926
945
|
finally {
|
|
946
|
+
// 清理心跳定时器
|
|
947
|
+
clearInterval(heartbeatInterval);
|
|
927
948
|
if (!childAgent) {
|
|
928
949
|
this.isGenerating = false;
|
|
929
950
|
this.abortController = null;
|
|
930
|
-
if (res) {
|
|
931
|
-
|
|
951
|
+
if (res && !res.destroyed) {
|
|
952
|
+
try {
|
|
953
|
+
res.end();
|
|
954
|
+
}
|
|
955
|
+
catch (e) {
|
|
956
|
+
logger.error('Error closing SSE connection:', e);
|
|
957
|
+
}
|
|
932
958
|
}
|
|
933
959
|
// 非当前查看的 session,助手回复完成后增加未读计数
|
|
934
960
|
(0, SessionManager_js_1.getSessionManager)(this.userId).onMessageComplete(this.id);
|
|
@@ -140,28 +140,39 @@ exports.execTool = {
|
|
|
140
140
|
type: 'number',
|
|
141
141
|
description: '命令超时时间(毫秒),默认 60000',
|
|
142
142
|
default: 60000,
|
|
143
|
+
},
|
|
144
|
+
isUserAsk: {
|
|
145
|
+
type: 'boolean',
|
|
146
|
+
description: '是否为用户主动要求的命令(用于绕过危险命令拦截)。如果用户明确要求执行此命令(如"帮我执行shutdown"),设置为true。',
|
|
147
|
+
default: false,
|
|
143
148
|
}
|
|
144
149
|
},
|
|
145
150
|
required: ['command'],
|
|
146
151
|
},
|
|
147
152
|
handler: async (args, sessionId) => {
|
|
148
153
|
return new Promise(async (resolve) => {
|
|
149
|
-
const { cwd, timeout = 60000, approved } = args;
|
|
154
|
+
const { cwd, timeout = 60000, approved, isUserAsk = false } = args;
|
|
150
155
|
let command = args.command;
|
|
151
|
-
//
|
|
156
|
+
// 检查危险命令(如果已批准或用户主动要求则跳过拦截)
|
|
152
157
|
if (!approved && isDangerousCommand(command)) {
|
|
153
|
-
if (
|
|
154
|
-
|
|
158
|
+
if (isUserAsk) {
|
|
159
|
+
// 用户主动要求,跳过拦截
|
|
160
|
+
logger.info(`用户主动要求执行危险命令,跳过拦截: ${command}`);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
if (!sessionId) {
|
|
164
|
+
resolve({ success: false, errorMessage: '危险命令需要用户确认,但缺少会话ID' });
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const token = addPendingApproval(command, cwd, timeout, sessionId);
|
|
168
|
+
resolve({
|
|
169
|
+
success: false,
|
|
170
|
+
needsApproval: true,
|
|
171
|
+
approvalToken: token,
|
|
172
|
+
errorMessage: `危险命令需要用户确认:${command}`,
|
|
173
|
+
});
|
|
155
174
|
return;
|
|
156
175
|
}
|
|
157
|
-
const token = addPendingApproval(command, cwd, timeout, sessionId);
|
|
158
|
-
resolve({
|
|
159
|
-
success: false,
|
|
160
|
-
needsApproval: true,
|
|
161
|
-
approvalToken: token,
|
|
162
|
-
errorMessage: `危险命令需要用户确认:${command}`,
|
|
163
|
-
});
|
|
164
|
-
return;
|
|
165
176
|
}
|
|
166
177
|
const options = {
|
|
167
178
|
cwd,
|