@ynhcj/xiaoyi 2.5.6 → 2.5.7
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/dist/auth.d.ts +1 -1
- package/dist/channel.d.ts +59 -14
- package/dist/channel.js +142 -814
- package/dist/file-download.d.ts +17 -0
- package/dist/file-download.js +69 -0
- package/dist/heartbeat.d.ts +39 -0
- package/dist/heartbeat.js +102 -0
- package/dist/index.js +5 -5
- package/dist/push.d.ts +1 -1
- package/dist/runtime.d.ts +2 -2
- package/dist/runtime.js +2 -2
- package/dist/types.d.ts +74 -1
- package/dist/websocket.d.ts +17 -1
- package/dist/websocket.js +163 -13
- package/dist/xy-bot.d.ts +19 -0
- package/dist/xy-bot.js +277 -0
- package/dist/xy-client.d.ts +26 -0
- package/dist/xy-client.js +78 -0
- package/dist/xy-config.d.ts +18 -0
- package/dist/xy-config.js +37 -0
- package/dist/xy-formatter.d.ts +94 -0
- package/dist/xy-formatter.js +303 -0
- package/dist/xy-monitor.d.ts +17 -0
- package/dist/xy-monitor.js +187 -0
- package/dist/xy-parser.d.ts +49 -0
- package/dist/xy-parser.js +109 -0
- package/dist/xy-reply-dispatcher.d.ts +17 -0
- package/dist/xy-reply-dispatcher.js +308 -0
- package/dist/xy-tools/session-manager.d.ts +29 -0
- package/dist/xy-tools/session-manager.js +80 -0
- package/dist/xy-utils/config-manager.d.ts +26 -0
- package/dist/xy-utils/config-manager.js +61 -0
- package/dist/xy-utils/crypto.d.ts +8 -0
- package/dist/xy-utils/crypto.js +21 -0
- package/dist/xy-utils/logger.d.ts +6 -0
- package/dist/xy-utils/logger.js +37 -0
- package/dist/xy-utils/session.d.ts +34 -0
- package/dist/xy-utils/session.js +55 -0
- package/package.json +32 -17
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { A2AJsonRpcRequest, A2AMessagePart, A2ADataEvent } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parsed message information extracted from A2A request.
|
|
4
|
+
* Note: agentId is not extracted from message - it should come from config.
|
|
5
|
+
*/
|
|
6
|
+
export interface ParsedA2AMessage {
|
|
7
|
+
sessionId: string;
|
|
8
|
+
taskId: string;
|
|
9
|
+
messageId: string;
|
|
10
|
+
parts: A2AMessagePart[];
|
|
11
|
+
method: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Parse an A2A JSON-RPC request into structured message data.
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseA2AMessage(request: A2AJsonRpcRequest): ParsedA2AMessage;
|
|
17
|
+
/**
|
|
18
|
+
* Extract text content from message parts.
|
|
19
|
+
*/
|
|
20
|
+
export declare function extractTextFromParts(parts: A2AMessagePart[]): string;
|
|
21
|
+
/**
|
|
22
|
+
* Extract file parts from message parts.
|
|
23
|
+
*/
|
|
24
|
+
export declare function extractFileParts(parts: A2AMessagePart[]): Array<{
|
|
25
|
+
name: string;
|
|
26
|
+
mimeType: string;
|
|
27
|
+
uri: string;
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* Extract data events from message parts (for tool responses).
|
|
31
|
+
*/
|
|
32
|
+
export declare function extractDataEvents(parts: A2AMessagePart[]): A2ADataEvent[];
|
|
33
|
+
/**
|
|
34
|
+
* Check if message is a clearContext request.
|
|
35
|
+
*/
|
|
36
|
+
export declare function isClearContextMessage(method: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Check if message is a tasks/cancel request.
|
|
39
|
+
*/
|
|
40
|
+
export declare function isTasksCancelMessage(method: string): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Extract push_id from message parts.
|
|
43
|
+
* Looks for push_id in data parts under variables.systemVariables.push_id
|
|
44
|
+
*/
|
|
45
|
+
export declare function extractPushId(parts: A2AMessagePart[]): string | null;
|
|
46
|
+
/**
|
|
47
|
+
* Validate A2A request structure.
|
|
48
|
+
*/
|
|
49
|
+
export declare function validateA2ARequest(request: any): request is A2AJsonRpcRequest;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseA2AMessage = parseA2AMessage;
|
|
4
|
+
exports.extractTextFromParts = extractTextFromParts;
|
|
5
|
+
exports.extractFileParts = extractFileParts;
|
|
6
|
+
exports.extractDataEvents = extractDataEvents;
|
|
7
|
+
exports.isClearContextMessage = isClearContextMessage;
|
|
8
|
+
exports.isTasksCancelMessage = isTasksCancelMessage;
|
|
9
|
+
exports.extractPushId = extractPushId;
|
|
10
|
+
exports.validateA2ARequest = validateA2ARequest;
|
|
11
|
+
const logger_js_1 = require("./xy-utils/logger.js");
|
|
12
|
+
/**
|
|
13
|
+
* Parse an A2A JSON-RPC request into structured message data.
|
|
14
|
+
*/
|
|
15
|
+
function parseA2AMessage(request) {
|
|
16
|
+
const { method, params, id } = request;
|
|
17
|
+
if (!params) {
|
|
18
|
+
throw new Error("A2A request missing params");
|
|
19
|
+
}
|
|
20
|
+
const { sessionId, message, id: paramsId } = params;
|
|
21
|
+
if (!sessionId || !message) {
|
|
22
|
+
throw new Error("A2A request params missing required fields");
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
sessionId,
|
|
26
|
+
taskId: paramsId, // Task ID from params (对话唯一标识)
|
|
27
|
+
messageId: id, // Global unique message sequence ID from top-level request
|
|
28
|
+
parts: message.parts || [],
|
|
29
|
+
method,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Extract text content from message parts.
|
|
34
|
+
*/
|
|
35
|
+
function extractTextFromParts(parts) {
|
|
36
|
+
const textParts = parts
|
|
37
|
+
.filter((part) => part.kind === "text")
|
|
38
|
+
.map((part) => part.text);
|
|
39
|
+
return textParts.join("\n").trim();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Extract file parts from message parts.
|
|
43
|
+
*/
|
|
44
|
+
function extractFileParts(parts) {
|
|
45
|
+
return parts
|
|
46
|
+
.filter((part) => part.kind === "file")
|
|
47
|
+
.map((part) => part.file);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Extract data events from message parts (for tool responses).
|
|
51
|
+
*/
|
|
52
|
+
function extractDataEvents(parts) {
|
|
53
|
+
return parts
|
|
54
|
+
.filter((part) => part.kind === "data")
|
|
55
|
+
.map((part) => part.data.event)
|
|
56
|
+
.filter((event) => event !== undefined);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Check if message is a clearContext request.
|
|
60
|
+
*/
|
|
61
|
+
function isClearContextMessage(method) {
|
|
62
|
+
return method === "clearContext" || method === "clear_context";
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Check if message is a tasks/cancel request.
|
|
66
|
+
*/
|
|
67
|
+
function isTasksCancelMessage(method) {
|
|
68
|
+
return method === "tasks/cancel" || method === "tasks_cancel";
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Extract push_id from message parts.
|
|
72
|
+
* Looks for push_id in data parts under variables.systemVariables.push_id
|
|
73
|
+
*/
|
|
74
|
+
function extractPushId(parts) {
|
|
75
|
+
for (const part of parts) {
|
|
76
|
+
if (part.kind === "data" && part.data) {
|
|
77
|
+
const pushId = part.data.variables?.systemVariables?.push_id;
|
|
78
|
+
if (pushId && typeof pushId === "string") {
|
|
79
|
+
return pushId;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Validate A2A request structure.
|
|
87
|
+
*/
|
|
88
|
+
function validateA2ARequest(request) {
|
|
89
|
+
if (!request || typeof request !== "object") {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if (request.jsonrpc !== "2.0") {
|
|
93
|
+
logger_js_1.logger.warn("Invalid JSON-RPC version:", request.jsonrpc);
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
if (!request.method || typeof request.method !== "string") {
|
|
97
|
+
logger_js_1.logger.warn("Missing or invalid method");
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (!request.id) {
|
|
101
|
+
logger_js_1.logger.warn("Missing request id");
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if (!request.params || typeof request.params !== "object") {
|
|
105
|
+
logger_js_1.logger.warn("Missing or invalid params");
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { OpenClawConfig, RuntimeEnv } from "openclaw/dist/plugin-sdk/index.js";
|
|
2
|
+
type ClawdbotConfig = OpenClawConfig;
|
|
3
|
+
export interface CreateXYReplyDispatcherParams {
|
|
4
|
+
cfg: ClawdbotConfig;
|
|
5
|
+
runtime: RuntimeEnv;
|
|
6
|
+
sessionId: string;
|
|
7
|
+
taskId: string;
|
|
8
|
+
messageId: string;
|
|
9
|
+
accountId: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Create a reply dispatcher for XY channel messages.
|
|
13
|
+
* Follows feishu pattern with status updates and streaming support.
|
|
14
|
+
* Runtime is expected to be validated before calling this function.
|
|
15
|
+
*/
|
|
16
|
+
export declare function createXYReplyDispatcher(params: CreateXYReplyDispatcherParams): any;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createXYReplyDispatcher = createXYReplyDispatcher;
|
|
4
|
+
const runtime_js_1 = require("./runtime.js");
|
|
5
|
+
const xy_formatter_js_1 = require("./xy-formatter.js");
|
|
6
|
+
const xy_config_js_1 = require("./xy-config.js");
|
|
7
|
+
/**
|
|
8
|
+
* Create a reply dispatcher for XY channel messages.
|
|
9
|
+
* Follows feishu pattern with status updates and streaming support.
|
|
10
|
+
* Runtime is expected to be validated before calling this function.
|
|
11
|
+
*/
|
|
12
|
+
function createXYReplyDispatcher(params) {
|
|
13
|
+
const { cfg, runtime, sessionId, taskId, messageId, accountId } = params;
|
|
14
|
+
const log = runtime?.log ?? console.log;
|
|
15
|
+
const error = runtime?.error ?? console.error;
|
|
16
|
+
log(`[DISPATCHER-CREATE] ******* Creating dispatcher for session=${sessionId}, taskId=${taskId}, messageId=${messageId} *******`);
|
|
17
|
+
log(`[DISPATCHER-CREATE] Stack trace:`, new Error().stack?.split('\n').slice(1, 4).join('\n'));
|
|
18
|
+
log(`[DISPATCHER-CREATE] ======== Creating reply dispatcher ========`);
|
|
19
|
+
log(`[DISPATCHER-CREATE] sessionId: ${sessionId}, taskId: ${taskId}, messageId: ${messageId}`);
|
|
20
|
+
log(`[DISPATCHER-CREATE] Stack trace:`, new Error().stack?.split('\n').slice(1, 4).join('\n'));
|
|
21
|
+
// Get OpenClaw PluginRuntime (not XiaoYiRuntime)
|
|
22
|
+
const xiaoYiRuntime = (0, runtime_js_1.getXiaoYiRuntime)();
|
|
23
|
+
const core = xiaoYiRuntime.getPluginRuntime();
|
|
24
|
+
// Resolve configuration
|
|
25
|
+
const config = (0, xy_config_js_1.resolveXYConfig)(cfg);
|
|
26
|
+
// Reply prefix context: not imported at runtime to avoid openclaw/plugin-sdk
|
|
27
|
+
// module resolution issues in the CJS require chain. For a bot-to-bot A2A
|
|
28
|
+
// channel the response prefix (model name badge) is not needed.
|
|
29
|
+
const prefixContext = { responsePrefix: undefined, responsePrefixContextProvider: undefined, onModelSelected: undefined };
|
|
30
|
+
// Status update interval (every 60 seconds)
|
|
31
|
+
let statusUpdateInterval = null;
|
|
32
|
+
// Track if we've sent any response
|
|
33
|
+
let hasSentResponse = false;
|
|
34
|
+
// Track if we've sent the final empty message
|
|
35
|
+
let finalSent = false;
|
|
36
|
+
// Accumulate all text from deliver calls
|
|
37
|
+
let accumulatedText = "";
|
|
38
|
+
/**
|
|
39
|
+
* Start the status update interval
|
|
40
|
+
* Call this immediately after creating the dispatcher
|
|
41
|
+
*/
|
|
42
|
+
const startStatusInterval = () => {
|
|
43
|
+
log(`[STATUS INTERVAL] Starting interval for session ${sessionId}, taskId=${taskId}`);
|
|
44
|
+
statusUpdateInterval = setInterval(() => {
|
|
45
|
+
log(`[STATUS INTERVAL] Triggering status update for session ${sessionId}, taskId=${taskId}`);
|
|
46
|
+
void (0, xy_formatter_js_1.sendStatusUpdate)({
|
|
47
|
+
config,
|
|
48
|
+
sessionId,
|
|
49
|
+
taskId,
|
|
50
|
+
messageId,
|
|
51
|
+
text: "任务正在处理中,请稍后~",
|
|
52
|
+
state: "working",
|
|
53
|
+
}).catch((err) => {
|
|
54
|
+
error(`Failed to send status update:`, err);
|
|
55
|
+
});
|
|
56
|
+
}, 30000); // 30 seconds
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Stop the status update interval
|
|
60
|
+
*/
|
|
61
|
+
const stopStatusInterval = () => {
|
|
62
|
+
if (statusUpdateInterval) {
|
|
63
|
+
log(`[STATUS INTERVAL] Stopping interval for session ${sessionId}, taskId=${taskId}`);
|
|
64
|
+
clearInterval(statusUpdateInterval);
|
|
65
|
+
statusUpdateInterval = null;
|
|
66
|
+
log(`[STATUS INTERVAL] Stopped interval for session ${sessionId}, taskId=${taskId}`);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const { dispatcher, replyOptions, markDispatchIdle } = core.channel.reply.createReplyDispatcherWithTyping({
|
|
70
|
+
responsePrefix: prefixContext.responsePrefix,
|
|
71
|
+
responsePrefixContextProvider: prefixContext.responsePrefixContextProvider,
|
|
72
|
+
humanDelay: core.channel.reply.resolveHumanDelayConfig(cfg, accountId),
|
|
73
|
+
onReplyStart: () => {
|
|
74
|
+
log(`[REPLY START] Reply started for session ${sessionId}, taskId=${taskId}`);
|
|
75
|
+
// Status update interval is now managed externally
|
|
76
|
+
},
|
|
77
|
+
deliver: async (payload, info) => {
|
|
78
|
+
const text = payload.text ?? "";
|
|
79
|
+
// 🔍 Debug logging
|
|
80
|
+
log(`[DELIVER] sessionId=${sessionId}, info.kind=${info?.kind}, text.length=${text.length}, text="${text.slice(0, 200)}"`);
|
|
81
|
+
log(`[DELIVER] payload keys: ${Object.keys(payload).join(", ")}`);
|
|
82
|
+
if (payload.mediaUrls) {
|
|
83
|
+
log(`[DELIVER] mediaUrls: ${payload.mediaUrls.length} files`);
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
// Skip empty messages
|
|
87
|
+
if (!text.trim()) {
|
|
88
|
+
log(`[DELIVER SKIP] Empty text, skipping`);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
// Accumulate text instead of sending immediately
|
|
92
|
+
accumulatedText += text;
|
|
93
|
+
hasSentResponse = true;
|
|
94
|
+
log(`[DELIVER ACCUMULATE] Accumulated text, current length=${accumulatedText.length}`);
|
|
95
|
+
// Also stream text as reasoningText for real-time display
|
|
96
|
+
await (0, xy_formatter_js_1.sendReasoningTextUpdate)({
|
|
97
|
+
config,
|
|
98
|
+
sessionId,
|
|
99
|
+
taskId,
|
|
100
|
+
messageId,
|
|
101
|
+
text,
|
|
102
|
+
});
|
|
103
|
+
log(`[DELIVER] ✅ Sent deliver text as reasoningText update`);
|
|
104
|
+
}
|
|
105
|
+
catch (deliverError) {
|
|
106
|
+
error(`Failed to deliver message:`, deliverError);
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
onError: async (err, info) => {
|
|
110
|
+
runtime.error?.(`xy: ${info.kind} reply failed: ${String(err)}`);
|
|
111
|
+
// Stop status updates
|
|
112
|
+
stopStatusInterval();
|
|
113
|
+
// Send error status if we haven't sent any response yet
|
|
114
|
+
if (!hasSentResponse) {
|
|
115
|
+
try {
|
|
116
|
+
await (0, xy_formatter_js_1.sendStatusUpdate)({
|
|
117
|
+
config,
|
|
118
|
+
sessionId,
|
|
119
|
+
taskId,
|
|
120
|
+
messageId,
|
|
121
|
+
text: "处理失败,请稍后重试",
|
|
122
|
+
state: "failed",
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch (statusError) {
|
|
126
|
+
error(`Failed to send error status:`, statusError);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
onIdle: async () => {
|
|
131
|
+
log(`[ON_IDLE] Reply idle for session ${sessionId}, hasSentResponse=${hasSentResponse}, finalSent=${finalSent}`);
|
|
132
|
+
// Send accumulated text with append=false and final=true
|
|
133
|
+
if (hasSentResponse && !finalSent) {
|
|
134
|
+
log(`[ON_IDLE] Sending accumulated text, length=${accumulatedText.length}`);
|
|
135
|
+
try {
|
|
136
|
+
// Send status update before final message
|
|
137
|
+
await (0, xy_formatter_js_1.sendStatusUpdate)({
|
|
138
|
+
config,
|
|
139
|
+
sessionId,
|
|
140
|
+
taskId,
|
|
141
|
+
messageId,
|
|
142
|
+
text: "任务处理已完成~",
|
|
143
|
+
state: "completed",
|
|
144
|
+
});
|
|
145
|
+
log(`[ON_IDLE] ✅ Sent completion status update`);
|
|
146
|
+
await (0, xy_formatter_js_1.sendA2AResponse)({
|
|
147
|
+
config,
|
|
148
|
+
sessionId,
|
|
149
|
+
taskId,
|
|
150
|
+
messageId,
|
|
151
|
+
text: accumulatedText,
|
|
152
|
+
append: false,
|
|
153
|
+
final: true,
|
|
154
|
+
});
|
|
155
|
+
finalSent = true;
|
|
156
|
+
log(`[ON_IDLE] Sent accumulated text`);
|
|
157
|
+
}
|
|
158
|
+
catch (err) {
|
|
159
|
+
error(`[ON_IDLE] Failed to send accumulated text:`, err);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
log(`[ON_IDLE] Skipping final message: hasSentResponse=${hasSentResponse}, finalSent=${finalSent}`);
|
|
164
|
+
// Task was interrupted - send failure status and error response
|
|
165
|
+
try {
|
|
166
|
+
await (0, xy_formatter_js_1.sendStatusUpdate)({
|
|
167
|
+
config,
|
|
168
|
+
sessionId,
|
|
169
|
+
taskId,
|
|
170
|
+
messageId,
|
|
171
|
+
text: "任务处理中断了~",
|
|
172
|
+
state: "failed",
|
|
173
|
+
});
|
|
174
|
+
log(`[ON_IDLE] ✅ Sent failure status update`);
|
|
175
|
+
await (0, xy_formatter_js_1.sendA2AResponse)({
|
|
176
|
+
config,
|
|
177
|
+
sessionId,
|
|
178
|
+
taskId,
|
|
179
|
+
messageId,
|
|
180
|
+
text: "任务执行异常,请重试~",
|
|
181
|
+
append: false,
|
|
182
|
+
final: true,
|
|
183
|
+
});
|
|
184
|
+
finalSent = true;
|
|
185
|
+
log(`[ON_IDLE] ✅ Sent error response`);
|
|
186
|
+
}
|
|
187
|
+
catch (err) {
|
|
188
|
+
error(`[ON_IDLE] Failed to send failure status and error response:`, err);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// Stop status updates
|
|
192
|
+
stopStatusInterval();
|
|
193
|
+
},
|
|
194
|
+
onCleanup: () => {
|
|
195
|
+
log(`[ON_CLEANUP] Reply cleanup for session ${sessionId}, hasSentResponse=${hasSentResponse}, finalSent=${finalSent}`);
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
return {
|
|
199
|
+
dispatcher,
|
|
200
|
+
replyOptions: {
|
|
201
|
+
...replyOptions,
|
|
202
|
+
onModelSelected: prefixContext.onModelSelected,
|
|
203
|
+
// 🔧 Tool execution start callback
|
|
204
|
+
onToolStart: async ({ name, phase }) => {
|
|
205
|
+
log(`[TOOL START] 🔧 Tool execution started/updated: name=${name}, phase=${phase}, session=${sessionId}, taskId=${taskId}`);
|
|
206
|
+
if (phase === "start") {
|
|
207
|
+
const toolName = name || "unknown";
|
|
208
|
+
try {
|
|
209
|
+
await (0, xy_formatter_js_1.sendStatusUpdate)({
|
|
210
|
+
config,
|
|
211
|
+
sessionId,
|
|
212
|
+
taskId,
|
|
213
|
+
messageId,
|
|
214
|
+
text: `正在使用工具: ${toolName}...`,
|
|
215
|
+
state: "working",
|
|
216
|
+
});
|
|
217
|
+
log(`[TOOL START] ✅ Sent status update for tool start: ${toolName}`);
|
|
218
|
+
}
|
|
219
|
+
catch (err) {
|
|
220
|
+
error(`[TOOL START] ❌ Failed to send tool start status:`, err);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
// 🔧 Tool execution result callback
|
|
225
|
+
onToolResult: async (payload) => {
|
|
226
|
+
const text = payload.text ?? "";
|
|
227
|
+
const hasMedia = Boolean(payload.mediaUrl || (payload.mediaUrls?.length ?? 0) > 0);
|
|
228
|
+
log(`[TOOL RESULT] 🔧 Tool execution result received: session=${sessionId}, taskId=${taskId}`);
|
|
229
|
+
log(`[TOOL RESULT] - text.length=${text.length}`);
|
|
230
|
+
log(`[TOOL RESULT] - hasMedia=${hasMedia}`);
|
|
231
|
+
log(`[TOOL RESULT] - isError=${payload.isError}`);
|
|
232
|
+
if (text.length > 0) {
|
|
233
|
+
log(`[TOOL RESULT] - text preview: "${text.slice(0, 200)}"`);
|
|
234
|
+
}
|
|
235
|
+
try {
|
|
236
|
+
if (text.length > 0 || hasMedia) {
|
|
237
|
+
const resultText = text.length > 0 ? text : "工具执行完成";
|
|
238
|
+
await (0, xy_formatter_js_1.sendStatusUpdate)({
|
|
239
|
+
config,
|
|
240
|
+
sessionId,
|
|
241
|
+
taskId,
|
|
242
|
+
messageId,
|
|
243
|
+
text: resultText,
|
|
244
|
+
state: "working",
|
|
245
|
+
});
|
|
246
|
+
log(`[TOOL RESULT] ✅ Sent tool result as status update`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
catch (err) {
|
|
250
|
+
error(`[TOOL RESULT] ❌ Failed to send tool result status:`, err);
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
// 🧠 Reasoning/thinking process streaming callback
|
|
254
|
+
onReasoningStream: async (payload) => {
|
|
255
|
+
const text = payload.text ?? "";
|
|
256
|
+
log(`[REASONING STREAM] 🧠 Reasoning/thinking chunk received: session=${sessionId}, taskId=${taskId}`);
|
|
257
|
+
log(`[REASONING STREAM] - text.length=${text.length}`);
|
|
258
|
+
if (text.length > 0) {
|
|
259
|
+
log(`[REASONING STREAM] - text preview: "${text.slice(0, 200)}"`);
|
|
260
|
+
}
|
|
261
|
+
// try {
|
|
262
|
+
// if (text.length > 0) {
|
|
263
|
+
// await sendReasoningTextUpdate({
|
|
264
|
+
// config,
|
|
265
|
+
// sessionId,
|
|
266
|
+
// taskId,
|
|
267
|
+
// messageId,
|
|
268
|
+
// text,
|
|
269
|
+
// });
|
|
270
|
+
// log(`[REASONING STREAM] ✅ Sent reasoning chunk as reasoningText update`);
|
|
271
|
+
// }
|
|
272
|
+
// } catch (err) {
|
|
273
|
+
// error(`[REASONING STREAM] ❌ Failed to send reasoning chunk reasoningText:`, err);
|
|
274
|
+
// }
|
|
275
|
+
},
|
|
276
|
+
// 📝 Partial reply streaming callback (real-time preview)
|
|
277
|
+
onPartialReply: async (payload) => {
|
|
278
|
+
const text = payload.text ?? "";
|
|
279
|
+
const hasMedia = Boolean(payload.mediaUrl || (payload.mediaUrls?.length ?? 0) > 0);
|
|
280
|
+
log(`[PARTIAL REPLY] 📝 Partial reply chunk received: session=${sessionId}, taskId=${taskId}`);
|
|
281
|
+
log(`[PARTIAL REPLY] - text.length=${text.length}`);
|
|
282
|
+
log(`[PARTIAL REPLY] - hasMedia=${hasMedia}`);
|
|
283
|
+
if (text.length > 0) {
|
|
284
|
+
log(`[PARTIAL REPLY] - text preview: "${text.slice(0, 200)}"`);
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
if (text.length > 0) {
|
|
288
|
+
await (0, xy_formatter_js_1.sendReasoningTextUpdate)({
|
|
289
|
+
config,
|
|
290
|
+
sessionId,
|
|
291
|
+
taskId,
|
|
292
|
+
messageId,
|
|
293
|
+
text,
|
|
294
|
+
append: false,
|
|
295
|
+
});
|
|
296
|
+
log(`[PARTIAL REPLY] ✅ Sent partial reply as reasoningText update (append=false)`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
catch (err) {
|
|
300
|
+
error(`[PARTIAL REPLY] ❌ Failed to send partial reply reasoningText:`, err);
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
markDispatchIdle,
|
|
305
|
+
startStatusInterval, // Expose this to be called immediately
|
|
306
|
+
stopStatusInterval, // Expose this for manual control if needed
|
|
307
|
+
};
|
|
308
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { XiaoYiChannelConfig } from "../types.js";
|
|
2
|
+
export interface SessionContext {
|
|
3
|
+
config: XiaoYiChannelConfig;
|
|
4
|
+
sessionId: string;
|
|
5
|
+
taskId: string;
|
|
6
|
+
messageId: string;
|
|
7
|
+
agentId: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Register a session context for tool access.
|
|
11
|
+
* Should be called when starting to process a message.
|
|
12
|
+
*/
|
|
13
|
+
export declare function registerSession(sessionKey: string, context: SessionContext): void;
|
|
14
|
+
/**
|
|
15
|
+
* Unregister a session context.
|
|
16
|
+
* Should be called when message processing is complete.
|
|
17
|
+
*/
|
|
18
|
+
export declare function unregisterSession(sessionKey: string): void;
|
|
19
|
+
/**
|
|
20
|
+
* Get session context by sessionKey.
|
|
21
|
+
* Returns null if session not found.
|
|
22
|
+
*/
|
|
23
|
+
export declare function getSessionContext(sessionKey: string): SessionContext | null;
|
|
24
|
+
/**
|
|
25
|
+
* Get the most recent session context.
|
|
26
|
+
* This is a fallback for tools that don't have access to sessionKey.
|
|
27
|
+
* Returns null if no sessions are active.
|
|
28
|
+
*/
|
|
29
|
+
export declare function getLatestSessionContext(): SessionContext | null;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerSession = registerSession;
|
|
4
|
+
exports.unregisterSession = unregisterSession;
|
|
5
|
+
exports.getSessionContext = getSessionContext;
|
|
6
|
+
exports.getLatestSessionContext = getLatestSessionContext;
|
|
7
|
+
const logger_js_1 = require("../xy-utils/logger.js");
|
|
8
|
+
const config_manager_js_1 = require("../xy-utils/config-manager.js");
|
|
9
|
+
// Map of sessionKey -> SessionContext
|
|
10
|
+
const activeSessions = new Map();
|
|
11
|
+
/**
|
|
12
|
+
* Register a session context for tool access.
|
|
13
|
+
* Should be called when starting to process a message.
|
|
14
|
+
*/
|
|
15
|
+
function registerSession(sessionKey, context) {
|
|
16
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] 📝 Registering session: ${sessionKey}`);
|
|
17
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - sessionId: ${context.sessionId}`);
|
|
18
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - taskId: ${context.taskId}`);
|
|
19
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - messageId: ${context.messageId}`);
|
|
20
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - agentId: ${context.agentId}`);
|
|
21
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - Active sessions before: ${activeSessions.size}`);
|
|
22
|
+
activeSessions.set(sessionKey, context);
|
|
23
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - Active sessions after: ${activeSessions.size}`);
|
|
24
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - All session keys: [${Array.from(activeSessions.keys()).join(", ")}]`);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Unregister a session context.
|
|
28
|
+
* Should be called when message processing is complete.
|
|
29
|
+
*/
|
|
30
|
+
function unregisterSession(sessionKey) {
|
|
31
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] 🗑️ Unregistering session: ${sessionKey}`);
|
|
32
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - Active sessions before: ${activeSessions.size}`);
|
|
33
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - Session existed: ${activeSessions.has(sessionKey)}`);
|
|
34
|
+
// Get session context before deleting to clear associated pushId
|
|
35
|
+
const context = activeSessions.get(sessionKey);
|
|
36
|
+
const existed = activeSessions.delete(sessionKey);
|
|
37
|
+
// Clear cached pushId for this session
|
|
38
|
+
if (context) {
|
|
39
|
+
config_manager_js_1.configManager.clearSession(context.sessionId);
|
|
40
|
+
}
|
|
41
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - Deleted: ${existed}`);
|
|
42
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - Active sessions after: ${activeSessions.size}`);
|
|
43
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - Remaining session keys: [${Array.from(activeSessions.keys()).join(", ")}]`);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Get session context by sessionKey.
|
|
47
|
+
* Returns null if session not found.
|
|
48
|
+
*/
|
|
49
|
+
function getSessionContext(sessionKey) {
|
|
50
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] 🔍 Getting session by key: ${sessionKey}`);
|
|
51
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - Active sessions: ${activeSessions.size}`);
|
|
52
|
+
const context = activeSessions.get(sessionKey) ?? null;
|
|
53
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - Found: ${context !== null}`);
|
|
54
|
+
if (context) {
|
|
55
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - sessionId: ${context.sessionId}`);
|
|
56
|
+
}
|
|
57
|
+
return context;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Get the most recent session context.
|
|
61
|
+
* This is a fallback for tools that don't have access to sessionKey.
|
|
62
|
+
* Returns null if no sessions are active.
|
|
63
|
+
*/
|
|
64
|
+
function getLatestSessionContext() {
|
|
65
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] 🔍 Getting latest session context`);
|
|
66
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - Active sessions count: ${activeSessions.size}`);
|
|
67
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - Active session keys: [${Array.from(activeSessions.keys()).join(", ")}]`);
|
|
68
|
+
if (activeSessions.size === 0) {
|
|
69
|
+
logger_js_1.logger.error(`[SESSION_MANAGER] - ❌ No active sessions found!`);
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
// Return the last added session
|
|
73
|
+
const sessions = Array.from(activeSessions.values());
|
|
74
|
+
const latestSession = sessions[sessions.length - 1];
|
|
75
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - ✅ Found latest session:`);
|
|
76
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - sessionId: ${latestSession.sessionId}`);
|
|
77
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - taskId: ${latestSession.taskId}`);
|
|
78
|
+
logger_js_1.logger.log(`[SESSION_MANAGER] - messageId: ${latestSession.messageId}`);
|
|
79
|
+
return latestSession;
|
|
80
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manages dynamic configuration updates that can change at runtime.
|
|
3
|
+
* Specifically handles pushId which can be updated per-session.
|
|
4
|
+
*/
|
|
5
|
+
declare class ConfigManager {
|
|
6
|
+
private sessionPushIds;
|
|
7
|
+
private globalPushId;
|
|
8
|
+
/**
|
|
9
|
+
* Update push ID for a specific session.
|
|
10
|
+
*/
|
|
11
|
+
updatePushId(sessionId: string, pushId: string): void;
|
|
12
|
+
/**
|
|
13
|
+
* Get push ID for a session (falls back to global if not found).
|
|
14
|
+
*/
|
|
15
|
+
getPushId(sessionId?: string): string | null;
|
|
16
|
+
/**
|
|
17
|
+
* Clear push ID for a session.
|
|
18
|
+
*/
|
|
19
|
+
clearSession(sessionId: string): void;
|
|
20
|
+
/**
|
|
21
|
+
* Clear all cached push IDs.
|
|
22
|
+
*/
|
|
23
|
+
clear(): void;
|
|
24
|
+
}
|
|
25
|
+
export declare const configManager: ConfigManager;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.configManager = void 0;
|
|
4
|
+
// Dynamic configuration manager for runtime updates
|
|
5
|
+
const logger_js_1 = require("./logger.js");
|
|
6
|
+
/**
|
|
7
|
+
* Manages dynamic configuration updates that can change at runtime.
|
|
8
|
+
* Specifically handles pushId which can be updated per-session.
|
|
9
|
+
*/
|
|
10
|
+
class ConfigManager {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.sessionPushIds = new Map();
|
|
13
|
+
this.globalPushId = null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Update push ID for a specific session.
|
|
17
|
+
*/
|
|
18
|
+
updatePushId(sessionId, pushId) {
|
|
19
|
+
if (!pushId) {
|
|
20
|
+
logger_js_1.logger.warn(`[ConfigManager] Attempted to set empty pushId for session ${sessionId}`);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const previous = this.sessionPushIds.get(sessionId);
|
|
24
|
+
if (previous !== pushId) {
|
|
25
|
+
logger_js_1.logger.log(`[ConfigManager] ✨ Updated pushId for session ${sessionId}`);
|
|
26
|
+
logger_js_1.logger.log(`[ConfigManager] - Previous: ${previous ? previous.substring(0, 20) + '...' : 'none'}`);
|
|
27
|
+
logger_js_1.logger.log(`[ConfigManager] - New: ${pushId.substring(0, 20)}...`);
|
|
28
|
+
logger_js_1.logger.log(`[ConfigManager] - Full new pushId: ${pushId}`);
|
|
29
|
+
this.sessionPushIds.set(sessionId, pushId);
|
|
30
|
+
this.globalPushId = pushId; // Also update global for backward compatibility
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Get push ID for a session (falls back to global if not found).
|
|
35
|
+
*/
|
|
36
|
+
getPushId(sessionId) {
|
|
37
|
+
if (sessionId) {
|
|
38
|
+
const sessionPushId = this.sessionPushIds.get(sessionId);
|
|
39
|
+
if (sessionPushId) {
|
|
40
|
+
return sessionPushId;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return this.globalPushId;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Clear push ID for a session.
|
|
47
|
+
*/
|
|
48
|
+
clearSession(sessionId) {
|
|
49
|
+
this.sessionPushIds.delete(sessionId);
|
|
50
|
+
logger_js_1.logger.debug(`[ConfigManager] Cleared pushId for session ${sessionId}`);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Clear all cached push IDs.
|
|
54
|
+
*/
|
|
55
|
+
clear() {
|
|
56
|
+
this.sessionPushIds.clear();
|
|
57
|
+
this.globalPushId = null;
|
|
58
|
+
logger_js_1.logger.debug(`[ConfigManager] Cleared all cached pushIds`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.configManager = new ConfigManager();
|