@ai-devkit/agent-manager 0.33.0 → 0.33.1
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/AgentManager.d.ts +11 -2
- package/dist/AgentManager.d.ts.map +1 -1
- package/dist/AgentManager.js +37 -6
- package/dist/AgentManager.js.map +1 -1
- package/dist/__tests__/AgentManager.test.js +212 -5
- package/dist/__tests__/AgentManager.test.js.map +1 -1
- package/dist/__tests__/adapters/OpenCodeAdapter.test.js +1 -0
- package/dist/__tests__/adapters/OpenCodeAdapter.test.js.map +1 -1
- package/dist/__tests__/{adapters → providers/copilot}/CopilotAdapter.test.js +19 -14
- package/dist/__tests__/providers/copilot/CopilotAdapter.test.js.map +1 -0
- package/dist/__tests__/{adapters → providers/gemini}/GeminiCliAdapter.test.js +37 -29
- package/dist/__tests__/providers/gemini/GeminiCliAdapter.test.js.map +1 -0
- package/dist/__tests__/runtime/HerdrAgentDiscovery.test.js +164 -0
- package/dist/__tests__/runtime/HerdrAgentDiscovery.test.js.map +1 -0
- package/dist/__tests__/runtime/HerdrAgentRuntime.test.js +37 -1
- package/dist/__tests__/runtime/HerdrAgentRuntime.test.js.map +1 -1
- package/dist/__tests__/utils/AgentRegistry.test.js +20 -13
- package/dist/__tests__/utils/AgentRegistry.test.js.map +1 -1
- package/dist/adapters/OpenCodeAdapter.d.ts +1 -0
- package/dist/adapters/OpenCodeAdapter.d.ts.map +1 -1
- package/dist/adapters/OpenCodeAdapter.js +7 -4
- package/dist/adapters/OpenCodeAdapter.js.map +1 -1
- package/dist/adapters/index.d.ts +2 -2
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/adapters/index.js +2 -2
- package/dist/adapters/index.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/{adapters → providers/copilot}/CopilotAdapter.d.ts +11 -25
- package/dist/providers/copilot/CopilotAdapter.d.ts.map +1 -0
- package/dist/providers/copilot/CopilotAdapter.js +108 -0
- package/dist/providers/copilot/CopilotAdapter.js.map +1 -0
- package/dist/providers/copilot/CopilotAgentMapper.d.ts +9 -0
- package/dist/providers/copilot/CopilotAgentMapper.d.ts.map +1 -0
- package/dist/providers/copilot/CopilotAgentMapper.js +37 -0
- package/dist/providers/copilot/CopilotAgentMapper.js.map +1 -0
- package/dist/providers/copilot/CopilotSessionLocator.d.ts +19 -0
- package/dist/providers/copilot/CopilotSessionLocator.d.ts.map +1 -0
- package/dist/providers/copilot/CopilotSessionLocator.js +41 -0
- package/dist/providers/copilot/CopilotSessionLocator.js.map +1 -0
- package/dist/providers/copilot/CopilotSessionParser.d.ts +30 -0
- package/dist/providers/copilot/CopilotSessionParser.d.ts.map +1 -0
- package/dist/providers/copilot/CopilotSessionParser.js +181 -0
- package/dist/providers/copilot/CopilotSessionParser.js.map +1 -0
- package/dist/providers/gemini/GeminiAgentMapper.d.ts +9 -0
- package/dist/providers/gemini/GeminiAgentMapper.d.ts.map +1 -0
- package/dist/providers/gemini/GeminiAgentMapper.js +37 -0
- package/dist/providers/gemini/GeminiAgentMapper.js.map +1 -0
- package/dist/providers/gemini/GeminiCliAdapter.d.ts +52 -0
- package/dist/providers/gemini/GeminiCliAdapter.d.ts.map +1 -0
- package/dist/providers/gemini/GeminiCliAdapter.js +188 -0
- package/dist/providers/gemini/GeminiCliAdapter.js.map +1 -0
- package/dist/providers/gemini/GeminiSessionLocator.d.ts +30 -0
- package/dist/providers/gemini/GeminiSessionLocator.d.ts.map +1 -0
- package/dist/providers/gemini/GeminiSessionLocator.js +142 -0
- package/dist/providers/gemini/GeminiSessionLocator.js.map +1 -0
- package/dist/providers/gemini/GeminiSessionParser.d.ts +94 -0
- package/dist/providers/gemini/GeminiSessionParser.d.ts.map +1 -0
- package/dist/providers/gemini/GeminiSessionParser.js +166 -0
- package/dist/providers/gemini/GeminiSessionParser.js.map +1 -0
- package/dist/runtime/herdr/HerdrAgentDiscovery.d.ts +18 -0
- package/dist/runtime/herdr/HerdrAgentDiscovery.d.ts.map +1 -0
- package/dist/runtime/herdr/HerdrAgentDiscovery.js +183 -0
- package/dist/runtime/herdr/HerdrAgentDiscovery.js.map +1 -0
- package/dist/runtime/herdr/HerdrAgentRuntime.d.ts +5 -0
- package/dist/runtime/herdr/HerdrAgentRuntime.d.ts.map +1 -1
- package/dist/runtime/herdr/HerdrAgentRuntime.js +26 -8
- package/dist/runtime/herdr/HerdrAgentRuntime.js.map +1 -1
- package/dist/runtime/herdr/HerdrResponseParsers.d.ts +2 -0
- package/dist/runtime/herdr/HerdrResponseParsers.d.ts.map +1 -1
- package/dist/runtime/herdr/HerdrResponseParsers.js +12 -1
- package/dist/runtime/herdr/HerdrResponseParsers.js.map +1 -1
- package/dist/utils/AgentRegistry.d.ts.map +1 -1
- package/dist/utils/AgentRegistry.js +5 -5
- package/dist/utils/AgentRegistry.js.map +1 -1
- package/package.json +1 -1
- package/src/AgentManager.ts +67 -5
- package/src/__tests__/AgentManager.test.ts +231 -5
- package/src/__tests__/adapters/OpenCodeAdapter.test.ts +1 -0
- package/src/__tests__/{adapters → providers/copilot}/CopilotAdapter.test.ts +13 -16
- package/src/__tests__/{adapters → providers/gemini}/GeminiCliAdapter.test.ts +39 -31
- package/src/__tests__/runtime/HerdrAgentDiscovery.test.ts +131 -0
- package/src/__tests__/runtime/HerdrAgentRuntime.test.ts +20 -1
- package/src/__tests__/utils/AgentRegistry.test.ts +20 -10
- package/src/adapters/OpenCodeAdapter.ts +8 -4
- package/src/adapters/index.ts +2 -2
- package/src/index.ts +10 -3
- package/src/providers/copilot/CopilotAdapter.ts +145 -0
- package/src/providers/copilot/CopilotAgentMapper.ts +36 -0
- package/src/providers/copilot/CopilotSessionLocator.ts +57 -0
- package/src/providers/copilot/CopilotSessionParser.ts +289 -0
- package/src/providers/gemini/GeminiAgentMapper.ts +36 -0
- package/src/providers/gemini/GeminiCliAdapter.ts +266 -0
- package/src/providers/gemini/GeminiSessionLocator.ts +183 -0
- package/src/providers/gemini/GeminiSessionParser.ts +271 -0
- package/src/runtime/herdr/HerdrAgentDiscovery.ts +178 -0
- package/src/runtime/herdr/HerdrAgentRuntime.ts +33 -7
- package/src/runtime/herdr/HerdrResponseParsers.ts +11 -1
- package/src/utils/AgentRegistry.ts +8 -6
- package/dist/__tests__/adapters/CopilotAdapter.test.js.map +0 -1
- package/dist/__tests__/adapters/GeminiCliAdapter.test.js.map +0 -1
- package/dist/adapters/CopilotAdapter.d.ts.map +0 -1
- package/dist/adapters/CopilotAdapter.js +0 -329
- package/dist/adapters/CopilotAdapter.js.map +0 -1
- package/dist/adapters/GeminiCliAdapter.d.ts +0 -98
- package/dist/adapters/GeminiCliAdapter.d.ts.map +0 -1
- package/dist/adapters/GeminiCliAdapter.js +0 -513
- package/dist/adapters/GeminiCliAdapter.js.map +0 -1
- package/src/adapters/CopilotAdapter.ts +0 -483
- package/src/adapters/GeminiCliAdapter.ts +0 -699
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import * as crypto from "crypto";
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import type { ProcessInfo } from "../../adapters/AgentAdapter.js";
|
|
5
|
+
import type { SessionFile } from "../../utils/session.js";
|
|
6
|
+
import { isDirectory, safeReaddir } from "../../utils/session.js";
|
|
7
|
+
|
|
8
|
+
export interface GeminiSessionDiscovery {
|
|
9
|
+
sessions: SessionFile[];
|
|
10
|
+
contentCache: Map<string, string>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface GeminiSessionLocatorOptions {
|
|
14
|
+
geminiTmpDir?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface GeminiSessionMetadata {
|
|
18
|
+
sessionId?: string;
|
|
19
|
+
projectHash?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const SESSION_FILE_PREFIX = "session-";
|
|
23
|
+
const CHATS_DIR_NAME = "chats";
|
|
24
|
+
|
|
25
|
+
export class GeminiSessionLocator {
|
|
26
|
+
private readonly geminiTmpDir: string;
|
|
27
|
+
|
|
28
|
+
constructor(options: GeminiSessionLocatorOptions = {}) {
|
|
29
|
+
const homeDir = process.env.HOME || process.env.USERPROFILE || "";
|
|
30
|
+
this.geminiTmpDir = options.geminiTmpDir ?? path.join(homeDir, ".gemini", "tmp");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Discover session files for the given processes.
|
|
35
|
+
*
|
|
36
|
+
* Gemini CLI writes sessions to ~/.gemini/tmp/<shortId>/chats/session-*.json
|
|
37
|
+
* where <shortId> is opaque (managed by a project registry). We scan every
|
|
38
|
+
* shortId directory and filter by matching session.projectHash against
|
|
39
|
+
* sha256(process.cwd) to bind each session to a candidate process CWD.
|
|
40
|
+
*/
|
|
41
|
+
discoverSessions(processes: ProcessInfo[]): GeminiSessionDiscovery {
|
|
42
|
+
const empty = { sessions: [] as SessionFile[], contentCache: new Map<string, string>() };
|
|
43
|
+
if (!fs.existsSync(this.geminiTmpDir)) return empty;
|
|
44
|
+
|
|
45
|
+
const cwdHashMap = this.buildCwdHashMap(processes);
|
|
46
|
+
if (cwdHashMap.size === 0) return empty;
|
|
47
|
+
|
|
48
|
+
const contentCache = new Map<string, string>();
|
|
49
|
+
const sessions: SessionFile[] = [];
|
|
50
|
+
|
|
51
|
+
let shortIdEntries: string[];
|
|
52
|
+
try {
|
|
53
|
+
shortIdEntries = fs.readdirSync(this.geminiTmpDir);
|
|
54
|
+
} catch {
|
|
55
|
+
return empty;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (const shortId of shortIdEntries) {
|
|
59
|
+
const chatsDir = path.join(this.geminiTmpDir, shortId, CHATS_DIR_NAME);
|
|
60
|
+
if (!isDirectory(chatsDir)) continue;
|
|
61
|
+
|
|
62
|
+
for (const fileName of safeReaddir(chatsDir)) {
|
|
63
|
+
if (!this.isSessionFile(fileName)) continue;
|
|
64
|
+
|
|
65
|
+
const session = this.readCandidateSession(chatsDir, fileName, cwdHashMap);
|
|
66
|
+
if (!session) continue;
|
|
67
|
+
|
|
68
|
+
contentCache.set(session.filePath, session.content);
|
|
69
|
+
sessions.push(session.sessionFile);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return { sessions, contentCache };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
discoverHistoricalSessionFiles(): string[] {
|
|
77
|
+
if (!isDirectory(this.geminiTmpDir)) return [];
|
|
78
|
+
|
|
79
|
+
const files: string[] = [];
|
|
80
|
+
for (const shortId of safeReaddir(this.geminiTmpDir)) {
|
|
81
|
+
const chatsDir = path.join(this.geminiTmpDir, shortId, CHATS_DIR_NAME);
|
|
82
|
+
if (!isDirectory(chatsDir)) continue;
|
|
83
|
+
|
|
84
|
+
for (const fileName of safeReaddir(chatsDir)) {
|
|
85
|
+
if (this.isSessionFile(fileName)) {
|
|
86
|
+
files.push(path.join(chatsDir, fileName));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return files;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private readCandidateSession(
|
|
95
|
+
chatsDir: string,
|
|
96
|
+
fileName: string,
|
|
97
|
+
cwdHashMap: Map<string, string>,
|
|
98
|
+
): { filePath: string; content: string; sessionFile: SessionFile } | null {
|
|
99
|
+
const filePath = path.join(chatsDir, fileName);
|
|
100
|
+
|
|
101
|
+
let content: string;
|
|
102
|
+
try {
|
|
103
|
+
content = fs.readFileSync(filePath, "utf-8");
|
|
104
|
+
} catch {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const parsed = this.parseSessionMetadata(content);
|
|
109
|
+
if (!parsed?.projectHash) return null;
|
|
110
|
+
|
|
111
|
+
const resolvedCwd = cwdHashMap.get(parsed.projectHash);
|
|
112
|
+
if (!resolvedCwd) return null;
|
|
113
|
+
|
|
114
|
+
let birthtimeMs = 0;
|
|
115
|
+
try {
|
|
116
|
+
birthtimeMs = fs.statSync(filePath).birthtimeMs;
|
|
117
|
+
} catch {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
filePath,
|
|
123
|
+
content,
|
|
124
|
+
sessionFile: {
|
|
125
|
+
sessionId: parsed.sessionId || fileName.replace(/\.json$/, ""),
|
|
126
|
+
filePath,
|
|
127
|
+
projectDir: chatsDir,
|
|
128
|
+
birthtimeMs,
|
|
129
|
+
resolvedCwd,
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private buildCwdHashMap(processes: ProcessInfo[]): Map<string, string> {
|
|
135
|
+
const map = new Map<string, string>();
|
|
136
|
+
for (const proc of processes) {
|
|
137
|
+
if (!proc.cwd) continue;
|
|
138
|
+
|
|
139
|
+
// Gemini CLI resolves its project root by walking up from the
|
|
140
|
+
// startup directory looking for a `.git` boundary marker. A
|
|
141
|
+
// session's projectHash therefore tracks that ancestor rather
|
|
142
|
+
// than the process' actual CWD. Enumerate every ancestor as a
|
|
143
|
+
// candidate so subdirectory invocations still line up with the
|
|
144
|
+
// session the Gemini process wrote.
|
|
145
|
+
for (const candidate of this.candidateProjectRoots(proc.cwd)) {
|
|
146
|
+
const hash = this.hashProjectRoot(candidate);
|
|
147
|
+
if (!map.has(hash)) {
|
|
148
|
+
map.set(hash, proc.cwd);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return map;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private candidateProjectRoots(cwd: string): string[] {
|
|
156
|
+
const roots: string[] = [];
|
|
157
|
+
let current = path.resolve(cwd);
|
|
158
|
+
let parent = path.dirname(current);
|
|
159
|
+
while (parent !== current) {
|
|
160
|
+
roots.push(current);
|
|
161
|
+
current = parent;
|
|
162
|
+
parent = path.dirname(current);
|
|
163
|
+
}
|
|
164
|
+
roots.push(current);
|
|
165
|
+
return roots;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private hashProjectRoot(projectRoot: string): string {
|
|
169
|
+
return crypto.createHash("sha256").update(projectRoot).digest("hex");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private isSessionFile(fileName: string): boolean {
|
|
173
|
+
return fileName.startsWith(SESSION_FILE_PREFIX) && fileName.endsWith(".json");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private parseSessionMetadata(content: string): GeminiSessionMetadata | null {
|
|
177
|
+
try {
|
|
178
|
+
return JSON.parse(content) as GeminiSessionMetadata;
|
|
179
|
+
} catch {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import type { ConversationMessage, SessionSummary } from "../../adapters/AgentAdapter.js";
|
|
3
|
+
import { AgentStatus } from "../../adapters/AgentAdapter.js";
|
|
4
|
+
import { safeReadFile, safeStat } from "../../utils/session.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A single Gemini CLI message content part. Mirrors the `{text?: string}`
|
|
8
|
+
* shape that Gemini writes for user message parts (derived from the
|
|
9
|
+
* Gemini API `Content.parts[]` schema). Non-text part variants (data,
|
|
10
|
+
* file, etc.) are preserved via the index signature but ignored by
|
|
11
|
+
* `resolveContent` since the adapter only surfaces human-readable text.
|
|
12
|
+
*/
|
|
13
|
+
interface GeminiContentPart {
|
|
14
|
+
text?: string;
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type GeminiMessageContent = string | GeminiContentPart[];
|
|
19
|
+
|
|
20
|
+
export interface GeminiMessageEntry {
|
|
21
|
+
id?: string;
|
|
22
|
+
timestamp?: string;
|
|
23
|
+
type?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Gemini CLI stores two different content shapes depending on the
|
|
26
|
+
* message origin:
|
|
27
|
+
* - `type: "user"` messages carry the raw Part[] from userContent.parts
|
|
28
|
+
* (e.g. `[{ text: "hello" }]`).
|
|
29
|
+
* - `type: "gemini"` (assistant) messages carry a pre-joined string
|
|
30
|
+
* built from `consolidatedParts.filter(p => p.text).join('').trim()`.
|
|
31
|
+
* Both forms must be normalized via resolveContent before any string
|
|
32
|
+
* operation is applied.
|
|
33
|
+
*/
|
|
34
|
+
content?: GeminiMessageContent;
|
|
35
|
+
displayContent?: GeminiMessageContent;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface GeminiSessionFile {
|
|
39
|
+
sessionId?: string;
|
|
40
|
+
projectHash?: string;
|
|
41
|
+
startTime?: string;
|
|
42
|
+
lastUpdated?: string;
|
|
43
|
+
messages?: GeminiMessageEntry[];
|
|
44
|
+
directories?: string[];
|
|
45
|
+
kind?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface GeminiSession {
|
|
49
|
+
sessionId: string;
|
|
50
|
+
projectPath: string;
|
|
51
|
+
summary: string;
|
|
52
|
+
sessionStart: Date;
|
|
53
|
+
lastActive: Date;
|
|
54
|
+
lastMessageType?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const IDLE_THRESHOLD_MINUTES = 5;
|
|
58
|
+
|
|
59
|
+
export class GeminiSessionParser {
|
|
60
|
+
/**
|
|
61
|
+
* Parse session file content into GeminiSession.
|
|
62
|
+
* Uses cached content if available, otherwise reads from disk.
|
|
63
|
+
*/
|
|
64
|
+
parseSession(cachedContent: string | undefined, filePath: string): GeminiSession | null {
|
|
65
|
+
const content = cachedContent ?? this.readSessionFile(filePath);
|
|
66
|
+
if (content === null) return null;
|
|
67
|
+
|
|
68
|
+
const parsed = this.parseSessionJson(content);
|
|
69
|
+
if (!parsed?.sessionId) return null;
|
|
70
|
+
|
|
71
|
+
const messages = Array.isArray(parsed.messages) ? parsed.messages : [];
|
|
72
|
+
const lastEntry = messages.length > 0 ? messages[messages.length - 1] : undefined;
|
|
73
|
+
const fileStat = safeStat(filePath);
|
|
74
|
+
|
|
75
|
+
const lastActive =
|
|
76
|
+
this.parseTimestamp(parsed.lastUpdated) ||
|
|
77
|
+
this.parseTimestamp(lastEntry?.timestamp) ||
|
|
78
|
+
fileStat?.mtime ||
|
|
79
|
+
new Date();
|
|
80
|
+
|
|
81
|
+
const sessionStart = this.parseTimestamp(parsed.startTime) || lastActive;
|
|
82
|
+
const projectPath =
|
|
83
|
+
Array.isArray(parsed.directories) && parsed.directories.length > 0
|
|
84
|
+
? parsed.directories[0]
|
|
85
|
+
: "";
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
sessionId: parsed.sessionId,
|
|
89
|
+
projectPath,
|
|
90
|
+
summary: this.extractSummary(messages),
|
|
91
|
+
sessionStart,
|
|
92
|
+
lastActive,
|
|
93
|
+
lastMessageType: lastEntry?.type,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
determineStatus(session: GeminiSession): AgentStatus {
|
|
98
|
+
const diffMs = Date.now() - session.lastActive.getTime();
|
|
99
|
+
const diffMinutes = diffMs / 60000;
|
|
100
|
+
|
|
101
|
+
if (diffMinutes > IDLE_THRESHOLD_MINUTES) {
|
|
102
|
+
return AgentStatus.IDLE;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (session.lastMessageType === "gemini" || session.lastMessageType === "assistant") {
|
|
106
|
+
return AgentStatus.WAITING;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return AgentStatus.RUNNING;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Read the full conversation from a Gemini CLI session JSON file.
|
|
114
|
+
*
|
|
115
|
+
* Gemini sessions store messages in an array with `type` field — typically
|
|
116
|
+
* 'user' or 'gemini' for visible turns, with tool and system entries mixed in.
|
|
117
|
+
*/
|
|
118
|
+
getConversation(sessionFilePath: string, options?: { verbose?: boolean }): ConversationMessage[] {
|
|
119
|
+
const verbose = options?.verbose ?? false;
|
|
120
|
+
const content = safeReadFile(sessionFilePath);
|
|
121
|
+
if (content === undefined) return [];
|
|
122
|
+
|
|
123
|
+
const parsed = this.parseSessionJson(content);
|
|
124
|
+
if (!Array.isArray(parsed?.messages)) return [];
|
|
125
|
+
|
|
126
|
+
const messages: ConversationMessage[] = [];
|
|
127
|
+
for (const entry of parsed.messages) {
|
|
128
|
+
const role = this.roleForEntry(entry?.type, verbose);
|
|
129
|
+
if (!role) continue;
|
|
130
|
+
|
|
131
|
+
const text = this.messageText(entry).trim();
|
|
132
|
+
if (!text) continue;
|
|
133
|
+
|
|
134
|
+
messages.push({
|
|
135
|
+
role,
|
|
136
|
+
content: text,
|
|
137
|
+
timestamp: entry.timestamp,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return messages;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Read a Gemini session JSON file and produce a {@link SessionSummary}.
|
|
146
|
+
* Returns null when the file is unreadable, the JSON doesn't parse,
|
|
147
|
+
* or the body lacks a sessionId.
|
|
148
|
+
*/
|
|
149
|
+
fileToSessionSummary(filePath: string): SessionSummary | null {
|
|
150
|
+
const content = safeReadFile(filePath);
|
|
151
|
+
if (content === undefined) return null;
|
|
152
|
+
|
|
153
|
+
const parsed = this.parseSessionJson(content);
|
|
154
|
+
if (!parsed?.sessionId) return null;
|
|
155
|
+
|
|
156
|
+
const messages = Array.isArray(parsed.messages) ? parsed.messages : [];
|
|
157
|
+
const firstUserMessage = this.extractFirstUserMessage(messages);
|
|
158
|
+
const cwd =
|
|
159
|
+
Array.isArray(parsed.directories) && parsed.directories.length > 0
|
|
160
|
+
? parsed.directories[0]
|
|
161
|
+
: "";
|
|
162
|
+
|
|
163
|
+
const stat = safeStat(filePath);
|
|
164
|
+
const lastEntryTimestamp = this.parseTimestamp(
|
|
165
|
+
messages.length > 0 ? messages[messages.length - 1]?.timestamp : undefined,
|
|
166
|
+
);
|
|
167
|
+
const lastActive =
|
|
168
|
+
this.parseTimestamp(parsed.lastUpdated) || lastEntryTimestamp || stat?.mtime || new Date();
|
|
169
|
+
const startedAt =
|
|
170
|
+
this.parseTimestamp(parsed.startTime) || stat?.birthtime || stat?.mtime || lastActive;
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
type: "gemini_cli",
|
|
174
|
+
sessionId: parsed.sessionId,
|
|
175
|
+
cwd,
|
|
176
|
+
firstUserMessage,
|
|
177
|
+
lastActive,
|
|
178
|
+
startedAt,
|
|
179
|
+
sessionFilePath: filePath,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private parseSessionJson(content: string): GeminiSessionFile | null {
|
|
184
|
+
try {
|
|
185
|
+
return JSON.parse(content) as GeminiSessionFile;
|
|
186
|
+
} catch {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private readSessionFile(filePath: string): string | null {
|
|
192
|
+
try {
|
|
193
|
+
return fs.readFileSync(filePath, "utf-8");
|
|
194
|
+
} catch {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private roleForEntry(
|
|
200
|
+
entryType: string | undefined,
|
|
201
|
+
verbose: boolean,
|
|
202
|
+
): ConversationMessage["role"] | null {
|
|
203
|
+
if (entryType === "user") return "user";
|
|
204
|
+
if (entryType === "gemini" || entryType === "assistant") return "assistant";
|
|
205
|
+
if (verbose && entryType) return "system";
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private extractSummary(messages: GeminiMessageEntry[]): string {
|
|
210
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
211
|
+
const entry = messages[i];
|
|
212
|
+
if (entry?.type !== "user") continue;
|
|
213
|
+
const text = this.messageText(entry).trim();
|
|
214
|
+
if (text) return this.truncate(text, 120);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return "Gemini CLI session active";
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Normalize an entry's content/displayContent into a plain string.
|
|
222
|
+
* Prefers displayContent when both are present (matches Gemini CLI's
|
|
223
|
+
* own rendering priority for the /chat UI).
|
|
224
|
+
*/
|
|
225
|
+
private messageText(entry: GeminiMessageEntry): string {
|
|
226
|
+
const displayText = this.resolveContent(entry.displayContent);
|
|
227
|
+
if (displayText) return displayText;
|
|
228
|
+
return this.resolveContent(entry.content);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Collapse a Gemini message content field into plain text.
|
|
233
|
+
* Accepts either a pre-joined string (assistant turns) or a Part[]
|
|
234
|
+
* list (user turns carrying `[{text: "..."}]`). Non-text part
|
|
235
|
+
* variants (data, file) are dropped since this helper is only used
|
|
236
|
+
* for summary/conversation rendering.
|
|
237
|
+
*/
|
|
238
|
+
private resolveContent(content: GeminiMessageContent | undefined): string {
|
|
239
|
+
if (!content) return "";
|
|
240
|
+
if (typeof content === "string") return content;
|
|
241
|
+
if (!Array.isArray(content)) return "";
|
|
242
|
+
|
|
243
|
+
const parts: string[] = [];
|
|
244
|
+
for (const part of content) {
|
|
245
|
+
if (part && typeof part.text === "string" && part.text) {
|
|
246
|
+
parts.push(part.text);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return parts.join("");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private extractFirstUserMessage(messages: GeminiMessageEntry[]): string {
|
|
253
|
+
for (const entry of messages) {
|
|
254
|
+
if (entry?.type !== "user") continue;
|
|
255
|
+
const text = this.messageText(entry).trim();
|
|
256
|
+
if (text) return text;
|
|
257
|
+
}
|
|
258
|
+
return "";
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
private parseTimestamp(value?: string): Date | null {
|
|
262
|
+
if (!value) return null;
|
|
263
|
+
const timestamp = new Date(value);
|
|
264
|
+
return Number.isNaN(timestamp.getTime()) ? null : timestamp;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private truncate(value: string, maxLength: number): string {
|
|
268
|
+
if (value.length <= maxLength) return value;
|
|
269
|
+
return `${value.slice(0, maxLength - 3)}...`;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { AgentInfo, AgentType } from "../../adapters/AgentAdapter.js";
|
|
2
|
+
import { HerdrCliClient, type HerdrCommandRunner } from "./HerdrCliClient.js";
|
|
3
|
+
import type { HerdrRuntimeRef } from "./HerdrRuntimeRef.js";
|
|
4
|
+
|
|
5
|
+
export interface HerdrAgentPane {
|
|
6
|
+
agent: string;
|
|
7
|
+
agentSessionId?: string;
|
|
8
|
+
cwd?: string;
|
|
9
|
+
paneId: string;
|
|
10
|
+
workspaceId?: string;
|
|
11
|
+
tabId?: string;
|
|
12
|
+
foregroundPids?: number[];
|
|
13
|
+
shellPid?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function fetchHerdrAgentPanes(runner?: HerdrCommandRunner): Promise<HerdrAgentPane[]> {
|
|
17
|
+
const client = new HerdrCliClient(runner);
|
|
18
|
+
const response = await client.runJson(["pane", "list"]);
|
|
19
|
+
return enrichHerdrAgentPanes(client, extractHerdrAgentPanes(response));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function extractHerdrAgentPanes(value: unknown): HerdrAgentPane[] {
|
|
23
|
+
const snapshot = getObject(value, ["result", "snapshot"]);
|
|
24
|
+
const rawPanes = getArray(value, ["result", "panes"]) ?? snapshot?.agents;
|
|
25
|
+
if (!Array.isArray(rawPanes)) return [];
|
|
26
|
+
|
|
27
|
+
const panes: HerdrAgentPane[] = [];
|
|
28
|
+
for (const rawPane of rawPanes) {
|
|
29
|
+
if (!rawPane || typeof rawPane !== "object" || Array.isArray(rawPane)) continue;
|
|
30
|
+
const pane = rawPane as Record<string, unknown>;
|
|
31
|
+
const agent = nonEmptyString(pane.agent);
|
|
32
|
+
const paneId = nonEmptyString(pane.pane_id);
|
|
33
|
+
if (!agent || !paneId) continue;
|
|
34
|
+
|
|
35
|
+
const session = getObject(pane, ["agent_session"]);
|
|
36
|
+
const agentSessionId = nonEmptyString(session?.value);
|
|
37
|
+
const cwd = nonEmptyString(pane.cwd);
|
|
38
|
+
const workspaceId = nonEmptyString(pane.workspace_id);
|
|
39
|
+
const tabId = nonEmptyString(pane.tab_id);
|
|
40
|
+
|
|
41
|
+
panes.push({
|
|
42
|
+
agent,
|
|
43
|
+
paneId,
|
|
44
|
+
...(agentSessionId ? { agentSessionId } : {}),
|
|
45
|
+
...(cwd ? { cwd } : {}),
|
|
46
|
+
...(workspaceId ? { workspaceId } : {}),
|
|
47
|
+
...(tabId ? { tabId } : {}),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return panes;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function findMatchingHerdrPane(
|
|
55
|
+
agent: Pick<AgentInfo, "type" | "projectPath" | "sessionId" | "pid">,
|
|
56
|
+
panes: readonly HerdrAgentPane[],
|
|
57
|
+
): HerdrAgentPane | null {
|
|
58
|
+
if (agent.sessionId) {
|
|
59
|
+
const sessionMatches = panes.filter((pane) => pane.agentSessionId === agent.sessionId);
|
|
60
|
+
if (sessionMatches.length === 1) return sessionMatches[0]!;
|
|
61
|
+
if (sessionMatches.length > 1) return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const foregroundPidMatches = panes.filter((pane) => pane.foregroundPids?.includes(agent.pid));
|
|
65
|
+
if (foregroundPidMatches.length === 1) return foregroundPidMatches[0]!;
|
|
66
|
+
if (foregroundPidMatches.length > 1) return null;
|
|
67
|
+
|
|
68
|
+
const shellPidMatches = panes.filter((pane) => pane.shellPid === agent.pid);
|
|
69
|
+
if (shellPidMatches.length === 1) return shellPidMatches[0]!;
|
|
70
|
+
if (shellPidMatches.length > 1) return null;
|
|
71
|
+
|
|
72
|
+
const cwdMatches = panes.filter(
|
|
73
|
+
(pane) => pane.cwd === agent.projectPath && herdrAgentMatchesType(pane.agent, agent.type),
|
|
74
|
+
);
|
|
75
|
+
return cwdMatches.length === 1 ? cwdMatches[0]! : null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function herdrPaneToRuntimeRef(pane: HerdrAgentPane, agentName: string): HerdrRuntimeRef {
|
|
79
|
+
return {
|
|
80
|
+
session: "default",
|
|
81
|
+
paneId: pane.paneId,
|
|
82
|
+
agentName,
|
|
83
|
+
...(pane.workspaceId ? { workspaceId: pane.workspaceId } : {}),
|
|
84
|
+
...(pane.tabId ? { tabId: pane.tabId } : {}),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function herdrAgentMatchesType(herdrAgent: string, agentType: AgentType): boolean {
|
|
89
|
+
const normalized = herdrAgent.toLowerCase();
|
|
90
|
+
const candidates: Record<AgentType, readonly string[]> = {
|
|
91
|
+
claude: ["claude"],
|
|
92
|
+
codex: ["codex"],
|
|
93
|
+
copilot: ["copilot"],
|
|
94
|
+
gemini_cli: ["gemini", "gemini_cli"],
|
|
95
|
+
grok_cli: ["grok", "grok_cli"],
|
|
96
|
+
opencode: ["opencode"],
|
|
97
|
+
pi: ["pi"],
|
|
98
|
+
other: ["other"],
|
|
99
|
+
};
|
|
100
|
+
return candidates[agentType]?.includes(normalized) ?? false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function enrichHerdrAgentPanes(
|
|
104
|
+
client: HerdrCliClient,
|
|
105
|
+
panes: HerdrAgentPane[],
|
|
106
|
+
): Promise<HerdrAgentPane[]> {
|
|
107
|
+
return Promise.all(
|
|
108
|
+
panes.map(async (pane) => {
|
|
109
|
+
try {
|
|
110
|
+
const response = await client.runJson(["pane", "process-info", "--pane", pane.paneId]);
|
|
111
|
+
return {
|
|
112
|
+
...pane,
|
|
113
|
+
...extractPaneProcessIds(response),
|
|
114
|
+
};
|
|
115
|
+
} catch {
|
|
116
|
+
return pane;
|
|
117
|
+
}
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function extractPaneProcessIds(value: unknown): Pick<HerdrAgentPane, "foregroundPids" | "shellPid"> {
|
|
123
|
+
const processInfo = getObject(value, ["result", "process_info"]);
|
|
124
|
+
if (!processInfo) return {};
|
|
125
|
+
|
|
126
|
+
const foregroundProcesses = processInfo.foreground_processes;
|
|
127
|
+
const foregroundPids: number[] = [];
|
|
128
|
+
if (Array.isArray(foregroundProcesses)) {
|
|
129
|
+
for (const foregroundProcess of foregroundProcesses) {
|
|
130
|
+
if (
|
|
131
|
+
!foregroundProcess ||
|
|
132
|
+
typeof foregroundProcess !== "object" ||
|
|
133
|
+
Array.isArray(foregroundProcess)
|
|
134
|
+
)
|
|
135
|
+
continue;
|
|
136
|
+
const pid = positiveIntegerOrNull((foregroundProcess as { pid?: unknown }).pid);
|
|
137
|
+
if (pid !== null) foregroundPids.push(pid);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const shellPid = positiveIntegerOrNull(processInfo.shell_pid);
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
...(foregroundPids.length > 0 ? { foregroundPids } : {}),
|
|
144
|
+
...(shellPid !== null ? { shellPid } : {}),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function getArray(value: unknown, path: string[]): unknown[] | null {
|
|
149
|
+
let current = value;
|
|
150
|
+
for (const part of path) {
|
|
151
|
+
if (!current || typeof current !== "object" || Array.isArray(current) || !(part in current)) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
current = (current as Record<string, unknown>)[part];
|
|
155
|
+
}
|
|
156
|
+
return Array.isArray(current) ? current : null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function getObject(value: unknown, path: string[]): Record<string, unknown> | null {
|
|
160
|
+
let current = value;
|
|
161
|
+
for (const part of path) {
|
|
162
|
+
if (!current || typeof current !== "object" || Array.isArray(current) || !(part in current)) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
current = (current as Record<string, unknown>)[part];
|
|
166
|
+
}
|
|
167
|
+
return current && typeof current === "object" && !Array.isArray(current)
|
|
168
|
+
? (current as Record<string, unknown>)
|
|
169
|
+
: null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function nonEmptyString(value: unknown): string | undefined {
|
|
173
|
+
return typeof value === "string" && value.trim() ? value : undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function positiveIntegerOrNull(value: unknown): number | null {
|
|
177
|
+
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : null;
|
|
178
|
+
}
|