@ynhcj/xiaoyi-channel 0.0.29-beta → 0.0.30-beta
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.
|
@@ -37,5 +37,8 @@ export declare function runWithSessionContext<T>(context: SessionContext, callba
|
|
|
37
37
|
* Get the current session context from AsyncLocalStorage.
|
|
38
38
|
* This is the recommended way to access session context in tools.
|
|
39
39
|
* Returns null if not running within a session context.
|
|
40
|
+
*
|
|
41
|
+
* Enhanced version: Automatically fetches the latest taskId from task-manager
|
|
42
|
+
* to support interruption scenarios where a new message updates the taskId.
|
|
40
43
|
*/
|
|
41
44
|
export declare function getCurrentSessionContext(): SessionContext | null;
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { AsyncLocalStorage } from "async_hooks";
|
|
4
4
|
import { logger } from "../utils/logger.js";
|
|
5
5
|
import { configManager } from "../utils/config-manager.js";
|
|
6
|
+
import { getCurrentTaskId, getCurrentMessageId } from "../task-manager.js";
|
|
6
7
|
// Map of sessionKey -> SessionContextWithRef
|
|
7
8
|
const activeSessions = new Map();
|
|
8
9
|
// AsyncLocalStorage for thread-safe session context isolation
|
|
@@ -116,16 +117,39 @@ export function runWithSessionContext(context, callback) {
|
|
|
116
117
|
* Get the current session context from AsyncLocalStorage.
|
|
117
118
|
* This is the recommended way to access session context in tools.
|
|
118
119
|
* Returns null if not running within a session context.
|
|
120
|
+
*
|
|
121
|
+
* Enhanced version: Automatically fetches the latest taskId from task-manager
|
|
122
|
+
* to support interruption scenarios where a new message updates the taskId.
|
|
119
123
|
*/
|
|
120
124
|
export function getCurrentSessionContext() {
|
|
125
|
+
// 1. Get base context from AsyncLocalStorage
|
|
121
126
|
const context = asyncLocalStorage.getStore() ?? null;
|
|
122
|
-
if (context) {
|
|
123
|
-
logger.log(`[SESSION_MANAGER] ✅ Got current session context from AsyncLocalStorage`);
|
|
124
|
-
logger.log(`[SESSION_MANAGER] - sessionId: ${context.sessionId}`);
|
|
125
|
-
logger.log(`[SESSION_MANAGER] - taskId: ${context.taskId}`);
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
127
|
+
if (!context) {
|
|
128
128
|
logger.warn(`[SESSION_MANAGER] ⚠️ No session context in AsyncLocalStorage`);
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
// 2. Get latest taskId and messageId from task-manager
|
|
132
|
+
const latestTaskId = getCurrentTaskId(context.sessionId);
|
|
133
|
+
const latestMessageId = getCurrentMessageId(context.sessionId);
|
|
134
|
+
// 3. If task-manager has a newer taskId, use the latest value
|
|
135
|
+
if (latestTaskId && latestTaskId !== context.taskId) {
|
|
136
|
+
logger.log(`[SESSION_MANAGER] 🔄 TaskId updated (interruption detected)`);
|
|
137
|
+
logger.log(`[SESSION_MANAGER] - sessionId: ${context.sessionId}`);
|
|
138
|
+
logger.log(`[SESSION_MANAGER] - Old taskId: ${context.taskId}`);
|
|
139
|
+
logger.log(`[SESSION_MANAGER] - New taskId: ${latestTaskId}`);
|
|
140
|
+
logger.log(`[SESSION_MANAGER] - Old messageId: ${context.messageId}`);
|
|
141
|
+
logger.log(`[SESSION_MANAGER] - New messageId: ${latestMessageId ?? context.messageId}`);
|
|
142
|
+
// Return updated context (create new object, don't modify original)
|
|
143
|
+
return {
|
|
144
|
+
...context,
|
|
145
|
+
taskId: latestTaskId,
|
|
146
|
+
messageId: latestMessageId ?? context.messageId,
|
|
147
|
+
};
|
|
129
148
|
}
|
|
149
|
+
// 4. No update needed, return original context
|
|
150
|
+
logger.log(`[SESSION_MANAGER] ✅ Got current session context from AsyncLocalStorage`);
|
|
151
|
+
logger.log(`[SESSION_MANAGER] - sessionId: ${context.sessionId}`);
|
|
152
|
+
logger.log(`[SESSION_MANAGER] - taskId: ${context.taskId}`);
|
|
153
|
+
logger.log(`[SESSION_MANAGER] - messageId: ${context.messageId}`);
|
|
130
154
|
return context;
|
|
131
155
|
}
|