@ynhcj/xiaoyi-channel 0.0.75-next → 0.0.76-next
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.
|
@@ -35,11 +35,9 @@ export declare function getLatestSessionContext(): SessionContext | null;
|
|
|
35
35
|
*/
|
|
36
36
|
export declare function runWithSessionContext<T>(context: SessionContext, callback: () => Promise<T>): Promise<T>;
|
|
37
37
|
/**
|
|
38
|
-
* Get the current session context
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* Enhanced version: Automatically fetches the latest taskId from task-manager
|
|
43
|
-
* to support interruption scenarios where a new message updates the taskId.
|
|
38
|
+
* Get the current session context.
|
|
39
|
+
* Prefers AsyncLocalStorage (correct for concurrent sessions).
|
|
40
|
+
* Falls back to the global activeSessions Map when AsyncLocalStorage
|
|
41
|
+
* context is lost (e.g., pi-agent framework tool execution boundary).
|
|
44
42
|
*/
|
|
45
43
|
export declare function getCurrentSessionContext(): SessionContext | null;
|
|
@@ -82,31 +82,53 @@ export function runWithSessionContext(context, callback) {
|
|
|
82
82
|
return asyncLocalStorage.run(context, callback);
|
|
83
83
|
}
|
|
84
84
|
/**
|
|
85
|
-
* Get the current session context
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* Enhanced version: Automatically fetches the latest taskId from task-manager
|
|
90
|
-
* to support interruption scenarios where a new message updates the taskId.
|
|
85
|
+
* Get the current session context.
|
|
86
|
+
* Prefers AsyncLocalStorage (correct for concurrent sessions).
|
|
87
|
+
* Falls back to the global activeSessions Map when AsyncLocalStorage
|
|
88
|
+
* context is lost (e.g., pi-agent framework tool execution boundary).
|
|
91
89
|
*/
|
|
92
90
|
export function getCurrentSessionContext() {
|
|
93
|
-
// 1.
|
|
94
|
-
const
|
|
95
|
-
if (
|
|
91
|
+
// 1. Try AsyncLocalStorage first (correct for concurrent sessions)
|
|
92
|
+
const alsContext = asyncLocalStorage.getStore() ?? null;
|
|
93
|
+
if (alsContext) {
|
|
94
|
+
return enrichWithLatestTaskInfo(alsContext);
|
|
95
|
+
}
|
|
96
|
+
// 2. Fallback: look up from global activeSessions Map
|
|
97
|
+
if (activeSessions.size === 0) {
|
|
96
98
|
return null;
|
|
97
99
|
}
|
|
98
|
-
//
|
|
100
|
+
// 2a. Single active session — return it directly
|
|
101
|
+
if (activeSessions.size === 1) {
|
|
102
|
+
const entry = activeSessions.values().next().value;
|
|
103
|
+
if (entry) {
|
|
104
|
+
const { refCount, ...context } = entry;
|
|
105
|
+
return enrichWithLatestTaskInfo(context);
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
// 2b. Multiple sessions — match by taskId currently being processed
|
|
110
|
+
for (const entry of activeSessions.values()) {
|
|
111
|
+
const latestTaskId = getCurrentTaskId(entry.sessionId);
|
|
112
|
+
if (latestTaskId) {
|
|
113
|
+
const { refCount, ...context } = entry;
|
|
114
|
+
return enrichWithLatestTaskInfo(context);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Enrich a base session context with the latest taskId/messageId
|
|
121
|
+
* from task-manager (supports interruption scenarios).
|
|
122
|
+
*/
|
|
123
|
+
function enrichWithLatestTaskInfo(context) {
|
|
99
124
|
const latestTaskId = getCurrentTaskId(context.sessionId);
|
|
100
125
|
const latestMessageId = getCurrentMessageId(context.sessionId);
|
|
101
|
-
// 3. If task-manager has a newer taskId, use the latest value
|
|
102
126
|
if (latestTaskId && latestTaskId !== context.taskId) {
|
|
103
|
-
// Return updated context (create new object, don't modify original)
|
|
104
127
|
return {
|
|
105
128
|
...context,
|
|
106
129
|
taskId: latestTaskId,
|
|
107
130
|
messageId: latestMessageId ?? context.messageId,
|
|
108
131
|
};
|
|
109
132
|
}
|
|
110
|
-
// 4. No update needed, return original context
|
|
111
133
|
return context;
|
|
112
134
|
}
|