@ynhcj/xiaoyi-channel 0.0.122-beta → 0.0.123-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.
package/dist/src/client.js
CHANGED
|
@@ -12,8 +12,14 @@ export function setClientRuntime(rt) {
|
|
|
12
12
|
/**
|
|
13
13
|
* Global cache for WebSocket managers.
|
|
14
14
|
* Key format: `${apiKey}-${agentId}`
|
|
15
|
+
* Uses globalThis to ensure a single cache across all module copies
|
|
16
|
+
* (same fix as session-manager.ts for openclaw multi-instance loading).
|
|
15
17
|
*/
|
|
16
|
-
const
|
|
18
|
+
const _g = globalThis;
|
|
19
|
+
if (!_g.__xyWsManagerCache) {
|
|
20
|
+
_g.__xyWsManagerCache = new Map();
|
|
21
|
+
}
|
|
22
|
+
const wsManagerCache = _g.__xyWsManagerCache;
|
|
17
23
|
/**
|
|
18
24
|
* Get or create a WebSocket manager for the given configuration.
|
|
19
25
|
* Reuses existing managers if config matches.
|
package/dist/src/task-manager.js
CHANGED
|
@@ -5,8 +5,13 @@ import { logger } from "./utils/logger.js";
|
|
|
5
5
|
* Session到活跃TaskId的映射
|
|
6
6
|
* Key: sessionId (注意:这里用sessionId,不是sessionKey)
|
|
7
7
|
* Value: TaskIdBinding
|
|
8
|
+
* Uses globalThis to ensure a single Map across all module copies.
|
|
8
9
|
*/
|
|
9
|
-
const
|
|
10
|
+
const _g = globalThis;
|
|
11
|
+
if (!_g.__xyActiveTaskIds) {
|
|
12
|
+
_g.__xyActiveTaskIds = new Map();
|
|
13
|
+
}
|
|
14
|
+
const activeTaskIds = _g.__xyActiveTaskIds;
|
|
10
15
|
/**
|
|
11
16
|
* 注册或更新session的活跃taskId
|
|
12
17
|
* 返回是否是更新(用于判断是否是第二条消息)
|
|
@@ -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;
|
|
@@ -4,8 +4,15 @@ import { AsyncLocalStorage } from "async_hooks";
|
|
|
4
4
|
import { configManager } from "../utils/config-manager.js";
|
|
5
5
|
import { toolCallNudgeManager } from "../utils/tool-call-nudge-manager.js";
|
|
6
6
|
import { getCurrentTaskId, getCurrentMessageId } from "../task-manager.js";
|
|
7
|
-
// Map
|
|
8
|
-
|
|
7
|
+
// Use globalThis to ensure a single Map instance across all module copies.
|
|
8
|
+
// The xy_channel plugin may be loaded by openclaw from different module resolution
|
|
9
|
+
// paths (plugin entry vs tool registration), causing session-manager.ts to be
|
|
10
|
+
// instantiated multiple times. globalThis guarantees all code shares the same Map.
|
|
11
|
+
const _g = globalThis;
|
|
12
|
+
if (!_g.__xyActiveSessions) {
|
|
13
|
+
_g.__xyActiveSessions = new Map();
|
|
14
|
+
}
|
|
15
|
+
const activeSessions = _g.__xyActiveSessions;
|
|
9
16
|
// AsyncLocalStorage for thread-safe session context isolation
|
|
10
17
|
const asyncLocalStorage = new AsyncLocalStorage();
|
|
11
18
|
/**
|
|
@@ -82,31 +89,53 @@ export function runWithSessionContext(context, callback) {
|
|
|
82
89
|
return asyncLocalStorage.run(context, callback);
|
|
83
90
|
}
|
|
84
91
|
/**
|
|
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.
|
|
92
|
+
* Get the current session context.
|
|
93
|
+
* Prefers AsyncLocalStorage (correct for concurrent sessions).
|
|
94
|
+
* Falls back to the global activeSessions Map when AsyncLocalStorage
|
|
95
|
+
* context is lost (e.g., pi-agent framework tool execution boundary).
|
|
91
96
|
*/
|
|
92
97
|
export function getCurrentSessionContext() {
|
|
93
|
-
// 1.
|
|
94
|
-
const
|
|
95
|
-
if (
|
|
98
|
+
// 1. Try AsyncLocalStorage first (correct for concurrent sessions)
|
|
99
|
+
const alsContext = asyncLocalStorage.getStore() ?? null;
|
|
100
|
+
if (alsContext) {
|
|
101
|
+
return enrichWithLatestTaskInfo(alsContext);
|
|
102
|
+
}
|
|
103
|
+
// 2. Fallback: look up from global activeSessions Map
|
|
104
|
+
if (activeSessions.size === 0) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
// 2a. Single active session — return it directly
|
|
108
|
+
if (activeSessions.size === 1) {
|
|
109
|
+
const entry = activeSessions.values().next().value;
|
|
110
|
+
if (entry) {
|
|
111
|
+
const { refCount, ...context } = entry;
|
|
112
|
+
return enrichWithLatestTaskInfo(context);
|
|
113
|
+
}
|
|
96
114
|
return null;
|
|
97
115
|
}
|
|
98
|
-
//
|
|
116
|
+
// 2b. Multiple sessions — match by taskId currently being processed
|
|
117
|
+
for (const entry of activeSessions.values()) {
|
|
118
|
+
const latestTaskId = getCurrentTaskId(entry.sessionId);
|
|
119
|
+
if (latestTaskId) {
|
|
120
|
+
const { refCount, ...context } = entry;
|
|
121
|
+
return enrichWithLatestTaskInfo(context);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Enrich a base session context with the latest taskId/messageId
|
|
128
|
+
* from task-manager (supports interruption scenarios).
|
|
129
|
+
*/
|
|
130
|
+
function enrichWithLatestTaskInfo(context) {
|
|
99
131
|
const latestTaskId = getCurrentTaskId(context.sessionId);
|
|
100
132
|
const latestMessageId = getCurrentMessageId(context.sessionId);
|
|
101
|
-
// 3. If task-manager has a newer taskId, use the latest value
|
|
102
133
|
if (latestTaskId && latestTaskId !== context.taskId) {
|
|
103
|
-
// Return updated context (create new object, don't modify original)
|
|
104
134
|
return {
|
|
105
135
|
...context,
|
|
106
136
|
taskId: latestTaskId,
|
|
107
137
|
messageId: latestMessageId ?? context.messageId,
|
|
108
138
|
};
|
|
109
139
|
}
|
|
110
|
-
// 4. No update needed, return original context
|
|
111
140
|
return context;
|
|
112
141
|
}
|