@ai-devkit/agent-manager 0.15.0 → 0.16.0

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.
Files changed (47) hide show
  1. package/dist/AgentManager.d.ts +4 -0
  2. package/dist/AgentManager.d.ts.map +1 -1
  3. package/dist/AgentManager.js +35 -0
  4. package/dist/AgentManager.js.map +1 -1
  5. package/dist/adapters/CodexAdapter.d.ts +4 -1
  6. package/dist/adapters/CodexAdapter.d.ts.map +1 -1
  7. package/dist/adapters/CodexAdapter.js +35 -6
  8. package/dist/adapters/CodexAdapter.js.map +1 -1
  9. package/dist/adapters/GeminiCliAdapter.d.ts +4 -1
  10. package/dist/adapters/GeminiCliAdapter.d.ts.map +1 -1
  11. package/dist/adapters/GeminiCliAdapter.js +35 -6
  12. package/dist/adapters/GeminiCliAdapter.js.map +1 -1
  13. package/dist/index.d.ts +5 -0
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +3 -0
  16. package/dist/index.js.map +1 -1
  17. package/dist/terminal/TmuxManager.d.ts +28 -0
  18. package/dist/terminal/TmuxManager.d.ts.map +1 -0
  19. package/dist/terminal/TmuxManager.js +112 -0
  20. package/dist/terminal/TmuxManager.js.map +1 -0
  21. package/dist/utils/AgentRegistry.d.ts +35 -0
  22. package/dist/utils/AgentRegistry.d.ts.map +1 -0
  23. package/dist/utils/AgentRegistry.js +114 -0
  24. package/dist/utils/AgentRegistry.js.map +1 -0
  25. package/dist/utils/ClaudeSessionParser.d.ts.map +1 -1
  26. package/dist/utils/ClaudeSessionParser.js +21 -11
  27. package/dist/utils/ClaudeSessionParser.js.map +1 -1
  28. package/dist/utils/agents.d.ts +15 -0
  29. package/dist/utils/agents.d.ts.map +1 -0
  30. package/dist/utils/agents.js +30 -0
  31. package/dist/utils/agents.js.map +1 -0
  32. package/package.json +1 -1
  33. package/src/AgentManager.ts +40 -0
  34. package/src/__tests__/AgentManager.test.ts +135 -0
  35. package/src/__tests__/adapters/ClaudeCodeAdapter.test.ts +1 -2
  36. package/src/__tests__/adapters/CodexAdapter.test.ts +105 -0
  37. package/src/__tests__/adapters/GeminiCliAdapter.test.ts +111 -0
  38. package/src/__tests__/terminal/TmuxManager.test.ts +175 -0
  39. package/src/__tests__/utils/AgentRegistry.test.ts +223 -0
  40. package/src/__tests__/utils/ClaudeSessionParser.test.ts +71 -0
  41. package/src/adapters/CodexAdapter.ts +45 -6
  42. package/src/adapters/GeminiCliAdapter.ts +45 -6
  43. package/src/index.ts +6 -0
  44. package/src/terminal/TmuxManager.ts +118 -0
  45. package/src/utils/AgentRegistry.ts +139 -0
  46. package/src/utils/ClaudeSessionParser.ts +21 -10
  47. package/src/utils/agents.ts +41 -0
@@ -0,0 +1,139 @@
1
+ import fs from 'fs';
2
+ import os from 'os';
3
+ import path from 'path';
4
+ import type { AgentType } from '../adapters/AgentAdapter.js';
5
+
6
+ export class RenameNotFoundError extends Error {
7
+ constructor(public agentName: string) {
8
+ super(`Agent "${agentName}" not found in registry.`);
9
+ this.name = 'RenameNotFoundError';
10
+ }
11
+ }
12
+
13
+ export class RenameConflictError extends Error {
14
+ constructor(public agentName: string) {
15
+ super(`Agent "${agentName}" is already in use.`);
16
+ this.name = 'RenameConflictError';
17
+ }
18
+ }
19
+
20
+ export interface RegistryEntry {
21
+ name: string;
22
+ type: AgentType;
23
+ pid: number;
24
+ tmuxSession: string;
25
+ cwd: string;
26
+ startedAt: string; // ISO 8601
27
+ sessionId: string;
28
+ sessionFilePath: string;
29
+ }
30
+
31
+ interface RegistryFile {
32
+ entries: RegistryEntry[];
33
+ }
34
+
35
+ const DEFAULT_REGISTRY_PATH = path.join(os.homedir(), '.ai-devkit', 'agents.json');
36
+
37
+ let defaultInstance: AgentRegistry | null = null;
38
+
39
+ export class AgentRegistry {
40
+ private filePath: string;
41
+
42
+ constructor(filePath: string = DEFAULT_REGISTRY_PATH) {
43
+ this.filePath = filePath;
44
+ }
45
+
46
+ static default(): AgentRegistry {
47
+ if (!defaultInstance) {
48
+ defaultInstance = new AgentRegistry();
49
+ }
50
+ return defaultInstance;
51
+ }
52
+
53
+ private readFile(): RegistryFile {
54
+ try {
55
+ const raw = fs.readFileSync(this.filePath, 'utf8');
56
+ const parsed = JSON.parse(raw) as RegistryFile;
57
+ return { entries: Array.isArray(parsed.entries) ? parsed.entries : [] };
58
+ } catch {
59
+ return { entries: [] };
60
+ }
61
+ }
62
+
63
+ private writeFile(data: RegistryFile): void {
64
+ const dir = path.dirname(this.filePath);
65
+ fs.mkdirSync(dir, { recursive: true });
66
+ const tmp = `${this.filePath}.tmp`;
67
+ fs.writeFileSync(tmp, JSON.stringify(data, null, 2), 'utf8');
68
+ fs.renameSync(tmp, this.filePath);
69
+ }
70
+
71
+ private mergeEntry(incoming: RegistryEntry, existing: RegistryEntry | undefined): RegistryEntry {
72
+ if (!existing) return incoming;
73
+ return {
74
+ ...incoming,
75
+ tmuxSession: incoming.tmuxSession || existing.tmuxSession,
76
+ };
77
+ }
78
+
79
+ isAlive(entry: RegistryEntry): boolean {
80
+ try {
81
+ process.kill(entry.pid, 0);
82
+ return true;
83
+ } catch {
84
+ return false;
85
+ }
86
+ }
87
+
88
+ prune(): void {
89
+ const data = this.readFile();
90
+ const live = data.entries.filter((e) => this.isAlive(e));
91
+ if (live.length !== data.entries.length) {
92
+ this.writeFile({ entries: live });
93
+ }
94
+ }
95
+
96
+ register(entry: RegistryEntry): void {
97
+ this.registerBatch([entry]);
98
+ }
99
+
100
+ registerBatch(entries: RegistryEntry[]): void {
101
+ if (entries.length === 0) return;
102
+ const data = this.readFile();
103
+ for (const incoming of entries) {
104
+ const idx = data.entries.findIndex((e) => e.name === incoming.name);
105
+ if (idx >= 0) {
106
+ data.entries[idx] = this.mergeEntry(incoming, data.entries[idx]);
107
+ } else {
108
+ data.entries.push(incoming);
109
+ }
110
+ }
111
+ this.writeFile(data);
112
+ }
113
+
114
+ rename(currentName: string, newName: string): void {
115
+ const data = this.readFile();
116
+ const idx = data.entries.findIndex((e) => e.name === currentName);
117
+ if (idx < 0) {
118
+ throw new RenameNotFoundError(currentName);
119
+ }
120
+ const liveEntries = data.entries.filter((e) => this.isAlive(e));
121
+ const conflict = liveEntries.find((e) => e.name === newName);
122
+ if (conflict) {
123
+ throw new RenameConflictError(newName);
124
+ }
125
+ const pruned = liveEntries.map((e) =>
126
+ e.name === currentName ? { ...e, name: newName } : e,
127
+ );
128
+ this.writeFile({ entries: pruned });
129
+ }
130
+
131
+ lookup(name: string): RegistryEntry | null {
132
+ const data = this.readFile();
133
+ return data.entries.find((e) => e.name === name) ?? null;
134
+ }
135
+
136
+ list(): RegistryEntry[] {
137
+ return this.readFile().entries;
138
+ }
139
+ }
@@ -51,8 +51,23 @@ export interface ClaudeSession {
51
51
  firstUserMessage?: string;
52
52
  }
53
53
 
54
- /** Entry types that are metadata, not conversation state. */
55
- const METADATA_ENTRY_TYPES = new Set(['last-prompt', 'file-history-snapshot']);
54
+ /**
55
+ * Top-level JSONL entry types that represent conversation/agent state.
56
+ *
57
+ * Only these types update `lastEntryType` for status determination. All
58
+ * other types Claude Code emits (`attachment`, `permission-mode`,
59
+ * `ai-title`, `queued_command`, `tools_changed`, `model_changed`,
60
+ * `hook_progress`, …) are UI-state events that must not overwrite the
61
+ * last conversation turn — otherwise polling between writes lands on a
62
+ * UI-state entry and `determineStatus` falls through to UNKNOWN.
63
+ */
64
+ const CONVERSATION_ENTRY_TYPES = new Set([
65
+ 'user',
66
+ 'assistant',
67
+ 'system',
68
+ 'progress',
69
+ 'thinking',
70
+ ]);
56
71
 
57
72
  /**
58
73
  * Parses Claude Code session JSONL files into structured data.
@@ -110,7 +125,7 @@ export class ClaudeSessionParser {
110
125
  lastCwd = entry.cwd;
111
126
  }
112
127
 
113
- if (entry.type && !METADATA_ENTRY_TYPES.has(entry.type)) {
128
+ if (entry.type && CONVERSATION_ENTRY_TYPES.has(entry.type)) {
114
129
  lastEntryType = entry.type;
115
130
 
116
131
  if (entry.type === 'user') {
@@ -220,16 +235,12 @@ export class ClaudeSessionParser {
220
235
  continue;
221
236
  }
222
237
 
223
- const entryType = entry.type;
224
- if (!entryType || METADATA_ENTRY_TYPES.has(entryType)) continue;
225
- if (entryType === 'progress' || entryType === 'thinking') continue;
226
-
227
238
  let role: ConversationMessage['role'];
228
- if (entryType === 'user') {
239
+ if (entry.type === 'user') {
229
240
  role = 'user';
230
- } else if (entryType === 'assistant') {
241
+ } else if (entry.type === 'assistant') {
231
242
  role = 'assistant';
232
- } else if (entryType === 'system') {
243
+ } else if (entry.type === 'system') {
233
244
  role = 'system';
234
245
  } else {
235
246
  continue;
@@ -0,0 +1,41 @@
1
+ import path from 'path';
2
+ import type { AgentType } from '../adapters/AgentAdapter.js';
3
+
4
+ export type StartableAgentType = Exclude<AgentType, 'other'>;
5
+
6
+ export interface AgentConfig {
7
+ /** Shell command to launch the agent (sent to tmux via `send-keys`). */
8
+ command: string;
9
+ /** Returns true if the given `ps` command line belongs to this agent. */
10
+ matches: (psCommand: string) => boolean;
11
+ }
12
+
13
+ /**
14
+ * Per-agent configuration: launch command plus a matcher that recognizes the
15
+ * agent's process in `ps` output. Each matcher knows that agent's distribution
16
+ * quirks (e.g. gemini ships as a Node script so its real binary is in argv[1..]).
17
+ */
18
+ export const AGENTS: Record<StartableAgentType, AgentConfig> = {
19
+ claude: { command: 'claude', matches: matchArgv0('claude') },
20
+ codex: { command: 'codex', matches: matchArgv0('codex') },
21
+ opencode: { command: 'opencode', matches: matchArgv0('opencode') },
22
+ gemini_cli: { command: 'gemini', matches: matchAnyToken('gemini') },
23
+ };
24
+
25
+ function matchArgv0(name: string): (psCommand: string) => boolean {
26
+ const lower = name.toLowerCase();
27
+ return (psCommand) => {
28
+ const token = psCommand.trim().split(/\s+/)[0];
29
+ return token ? path.basename(token).toLowerCase() === lower : false;
30
+ };
31
+ }
32
+
33
+ function matchAnyToken(name: string): (psCommand: string) => boolean {
34
+ const lower = name.toLowerCase();
35
+ return (psCommand) => {
36
+ for (const token of psCommand.trim().split(/\s+/)) {
37
+ if (path.basename(token).toLowerCase() === lower) return true;
38
+ }
39
+ return false;
40
+ };
41
+ }