@wu529778790/open-im 1.8.1-beta.13 → 1.8.1-beta.14
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,39 +37,51 @@ function isResult(msg) {
|
|
|
37
37
|
* @param permissionMode 权限模式
|
|
38
38
|
* @returns SDKSession 对象和实际的 sessionId
|
|
39
39
|
*/
|
|
40
|
-
async function getOrCreateSession(sessionId,
|
|
41
|
-
model, permissionMode) {
|
|
40
|
+
async function getOrCreateSession(sessionId, workDir, model, permissionMode) {
|
|
42
41
|
const resolvedModel = model?.trim() || 'claude-opus-4-5';
|
|
43
42
|
const sessionOptions = {
|
|
44
43
|
model: resolvedModel,
|
|
45
44
|
permissionMode,
|
|
46
|
-
// 可以添加其他选项,如 hooks, allowedTools 等
|
|
47
45
|
};
|
|
48
46
|
const baseUrl = process.env.ANTHROPIC_BASE_URL ?? '(default)';
|
|
49
|
-
log.info(`[V2] getOrCreateSession model param=${String(model ?? '')} resolved=${resolvedModel} baseUrl=${baseUrl}`);
|
|
47
|
+
log.info(`[V2] getOrCreateSession model param=${String(model ?? '')} resolved=${resolvedModel} baseUrl=${baseUrl} workDir=${workDir}`);
|
|
50
48
|
let session;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return { session, sessionId };
|
|
49
|
+
// SDK V2 的 SDKSessionOptions 没有 cwd 字段,
|
|
50
|
+
// 需要通过 process.chdir() 临时切换工作目录。
|
|
51
|
+
// 因为 createSession/resumeSession 是同步调用,且 JS 单线程,所以是安全的。
|
|
52
|
+
const originalCwd = process.cwd();
|
|
53
|
+
try {
|
|
54
|
+
if (workDir && workDir !== originalCwd) {
|
|
55
|
+
process.chdir(workDir);
|
|
59
56
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
57
|
+
if (sessionId) {
|
|
58
|
+
// 尝试恢复已有会话
|
|
59
|
+
try {
|
|
60
|
+
log.info(`Attempting to resume session: ${sessionId}`);
|
|
61
|
+
session = unstable_v2_resumeSession(sessionId, sessionOptions);
|
|
62
|
+
activeSessions.set(sessionId, session);
|
|
63
|
+
log.info(`Successfully resumed session: ${sessionId}`);
|
|
64
|
+
return { session, sessionId };
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
log.warn(`Failed to resume session ${sessionId}, creating new one: ${err}`);
|
|
68
|
+
// 恢复失败,创建新会话
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// 创建新会话
|
|
72
|
+
session = unstable_v2_createSession(sessionOptions);
|
|
73
|
+
// 新会话的 sessionId 需要从第一个消息中获取
|
|
74
|
+
// 暂时返回 undefined,稍后在 init 消息中获取
|
|
75
|
+
const tempId = `pending-${Date.now()}`;
|
|
76
|
+
activeSessions.set(tempId, session);
|
|
77
|
+
log.info(`Created new session (tempId: ${tempId})`);
|
|
78
|
+
return { session, sessionId: tempId };
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
if (workDir && workDir !== originalCwd) {
|
|
82
|
+
process.chdir(originalCwd);
|
|
63
83
|
}
|
|
64
84
|
}
|
|
65
|
-
// 创建新会话
|
|
66
|
-
session = unstable_v2_createSession(sessionOptions);
|
|
67
|
-
// 新会话的 sessionId 需要从第一个消息中获取
|
|
68
|
-
// 暂时返回 undefined,稍后在 init 消息中获取
|
|
69
|
-
const tempId = `pending-${Date.now()}`;
|
|
70
|
-
activeSessions.set(tempId, session);
|
|
71
|
-
log.info(`Created new session (tempId: ${tempId})`);
|
|
72
|
-
return { session, sessionId: tempId };
|
|
73
85
|
}
|
|
74
86
|
export class ClaudeSDKAdapter {
|
|
75
87
|
toolId = 'claude-sdk';
|