@wu529778790/open-im 1.11.1-beta.4 → 1.11.1-beta.5

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.
@@ -178,9 +178,11 @@ export class CommandHandler {
178
178
  }
179
179
  try {
180
180
  this.deps.requestQueue.cancelUser(userId);
181
- const resolved = await this.deps.sessionManager.setWorkDir(userId, dir);
182
- await this.replySender().sendTextReply(chatId, `📁 工作目录已切换到: ${escapePathForMarkdown(resolved)}\n\n` +
183
- `🔄 AI 会话已重置,下一条消息将使用全新上下文。`);
181
+ const result = await this.deps.sessionManager.setWorkDir(userId, dir);
182
+ const msg = result.resumed
183
+ ? `📁 工作目录已切换到: ${escapePathForMarkdown(result.path)}\n\n🔄 已恢复该目录的最近会话,继续之前的上下文。`
184
+ : `📁 工作目录已切换到: ${escapePathForMarkdown(result.path)}\n\n🆕 该目录暂无历史会话,已创建全新上下文。`;
185
+ await this.replySender().sendTextReply(chatId, msg);
184
186
  }
185
187
  catch (err) {
186
188
  await this.replySender().sendTextReply(chatId, err instanceof Error ? err.message : String(err));
@@ -3,6 +3,7 @@ interface ConvHistoryEntry {
3
3
  convId: string;
4
4
  totalTurns: number;
5
5
  createdAt: number;
6
+ workDir?: string;
6
7
  }
7
8
  export declare function resolveWorkDirInput(baseDir: string, targetDir: string): string;
8
9
  export declare class SessionManager {
@@ -24,7 +25,10 @@ export declare class SessionManager {
24
25
  getWorkDir(userId: string): string;
25
26
  hasUserSession(userId: string): boolean;
26
27
  getConvId(userId: string): string;
27
- setWorkDir(userId: string, workDir: string): Promise<string>;
28
+ setWorkDir(userId: string, workDir: string): Promise<{
29
+ path: string;
30
+ resumed: boolean;
31
+ }>;
28
32
  newSession(userId: string): boolean;
29
33
  clearActiveToolSession(userId: string, toolId: ToolId): boolean;
30
34
  listConvHistory(userId: string): ConvHistoryEntry[];
@@ -108,9 +108,15 @@ export class SessionManager {
108
108
  const realPath = await this.resolveAndValidate(currentDir, workDir);
109
109
  const s = this.sessions.get(userId);
110
110
  let oldConvId;
111
+ let resumed = false;
111
112
  if (s) {
113
+ // Same directory — no-op
114
+ if (realPath === currentDir) {
115
+ return { path: realPath, resumed: false };
116
+ }
112
117
  oldConvId = s.activeConvId;
113
118
  this.persistActiveConvSessions(userId, s);
119
+ // Archive current conversation with its workDir
114
120
  if (oldConvId) {
115
121
  if (!s.convHistory)
116
122
  s.convHistory = [];
@@ -118,13 +124,36 @@ export class SessionManager {
118
124
  convId: oldConvId,
119
125
  totalTurns: s.totalTurns ?? 0,
120
126
  createdAt: Date.now(),
127
+ workDir: currentDir,
121
128
  });
122
129
  if (s.convHistory.length > 10)
123
130
  s.convHistory = s.convHistory.slice(-10);
124
131
  }
132
+ // Look for a previous conversation in the target directory
133
+ if (!s.convHistory)
134
+ s.convHistory = [];
135
+ const matchIdx = s.convHistory.findIndex((e) => e.workDir === realPath);
136
+ if (matchIdx !== -1) {
137
+ const [entry] = s.convHistory.splice(matchIdx, 1);
138
+ s.activeConvId = entry.convId;
139
+ s.totalTurns = entry.totalTurns;
140
+ s.sessionIds = {};
141
+ for (const toolId of ['claude', 'codex', 'codebuddy', 'opencode']) {
142
+ const sessionId = this.convSessionMap.get(this.getConvSessionKey(userId, entry.convId, toolId));
143
+ if (sessionId) {
144
+ if (!s.sessionIds)
145
+ s.sessionIds = {};
146
+ s.sessionIds[toolId] = sessionId;
147
+ }
148
+ }
149
+ resumed = true;
150
+ log.info(`Resumed session for user ${userId}: convId=${entry.convId}, workDir=${realPath}, turns=${entry.totalTurns}`);
151
+ }
152
+ else {
153
+ s.sessionIds = {};
154
+ s.activeConvId = randomBytes(4).toString('hex');
155
+ }
125
156
  s.workDir = realPath;
126
- s.sessionIds = {};
127
- s.activeConvId = randomBytes(4).toString('hex');
128
157
  }
129
158
  else {
130
159
  this.sessions.set(userId, {
@@ -133,8 +162,8 @@ export class SessionManager {
133
162
  });
134
163
  }
135
164
  this.flushSync();
136
- log.info(`WorkDir changed for user ${userId}: ${realPath}, oldConvId=${oldConvId}`);
137
- return realPath;
165
+ log.info(`WorkDir changed for user ${userId}: ${realPath}, oldConvId=${oldConvId}, resumed=${resumed}`);
166
+ return { path: realPath, resumed };
138
167
  }
139
168
  newSession(userId) {
140
169
  const s = this.sessions.get(userId);
@@ -150,6 +179,7 @@ export class SessionManager {
150
179
  convId: oldConvId,
151
180
  totalTurns: s.totalTurns ?? 0,
152
181
  createdAt: Date.now(),
182
+ workDir: s.workDir,
153
183
  });
154
184
  // Keep last 10 entries
155
185
  if (s.convHistory.length > 10)
@@ -207,6 +237,7 @@ export class SessionManager {
207
237
  convId: s.activeConvId,
208
238
  totalTurns: s.totalTurns ?? 0,
209
239
  createdAt: Date.now(),
240
+ workDir: s.workDir,
210
241
  });
211
242
  }
212
243
  // Remove target from history
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wu529778790/open-im",
3
- "version": "1.11.1-beta.4",
3
+ "version": "1.11.1-beta.5",
4
4
  "description": "Multi-platform IM bridge for AI CLI tools (Claude, Codex, CodeBuddy)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",