( toAddress: string, channelType: string, )
| 27 | * Searches all agent session directories for a matching deliveryContext. |
| 28 | */ |
| 29 | export async function resolveAccountIdFromSessionHistory( |
| 30 | toAddress: string, |
| 31 | channelType: string, |
| 32 | ): Promise<string | null> { |
| 33 | const agentsDir = join(getOpenClawConfigDir(), 'agents'); |
| 34 | |
| 35 | let agentDirs: Array<{ name: string; isDirectory: () => boolean }>; |
| 36 | try { |
| 37 | agentDirs = await readdir(agentsDir, { withFileTypes: true }); |
| 38 | } catch { |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | for (const entry of agentDirs) { |
| 43 | if (!entry.isDirectory()) continue; |
| 44 | |
| 45 | const sessionsPath = join(agentsDir, entry.name, 'sessions', 'sessions.json'); |
| 46 | let raw: string; |
| 47 | try { |
| 48 | raw = await readFile(sessionsPath, 'utf8'); |
| 49 | } catch { |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | if (!raw.trim()) continue; |
| 54 | |
| 55 | let parsed: Record<string, unknown>; |
| 56 | try { |
| 57 | parsed = JSON.parse(raw); |
| 58 | } catch { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | for (const session of extractSessionRecords(parsed as JsonRecord)) { |
| 63 | const deliveryContext = session.deliveryContext as Record<string, unknown> | undefined; |
| 64 | if ( |
| 65 | deliveryContext && |
| 66 | typeof deliveryContext.to === 'string' && |
| 67 | deliveryContext.to === toAddress && |
| 68 | typeof deliveryContext.channel === 'string' && |
| 69 | deliveryContext.channel === channelType |
| 70 | ) { |
| 71 | if (typeof deliveryContext.accountId === 'string') { |
| 72 | return deliveryContext.accountId; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return null; |
| 79 | } |
no test coverage detected