feat: implement session tracking via Claude hooks

Adds a dedicated hook server infrastructure to receive Claude SessionStart
notifications. This allows the CLI to track session ID changes (new sessions,
resume, compact, fork, etc.) without relying on file watchers.

Changes:
- Add SessionFound callback system to AgentSessionBase for session tracking
- Create hook server that listens for POST requests from Claude's hooks
- Generate temporary settings files with hook command configuration
- Implement session hook forwarder CLI command to relay hook data
- Integrate hook server into runClaude with proper cleanup
- Pass hookSettingsPath through local and remote execution paths
- Update SDK query builder to support --settings flag
This commit is contained in:
weishu
2025-12-23 16:02:04 +08:00
parent e637f2bb27
commit 61d3a1bed1
15 changed files with 480 additions and 8 deletions
+16
View File
@@ -29,6 +29,7 @@ export class AgentSessionBase<Mode> {
mode: 'local' | 'remote' = 'local';
thinking: boolean = false;
private sessionFoundCallbacks: ((sessionId: string) => void)[] = [];
private readonly applySessionIdToMetadata: (metadata: Metadata, sessionId: string) => Metadata;
private readonly sessionLabel: string;
private readonly sessionIdLabel: string;
@@ -68,6 +69,21 @@ export class AgentSessionBase<Mode> {
this.sessionId = sessionId;
this.client.updateMetadata((metadata) => this.applySessionIdToMetadata(metadata, sessionId));
logger.debug(`[${this.sessionLabel}] ${this.sessionIdLabel} session ID ${sessionId} added to metadata`);
for (const callback of this.sessionFoundCallbacks) {
callback(sessionId);
}
};
addSessionFoundCallback = (callback: (sessionId: string) => void): void => {
this.sessionFoundCallbacks.push(callback);
};
removeSessionFoundCallback = (callback: (sessionId: string) => void): void => {
const index = this.sessionFoundCallbacks.indexOf(callback);
if (index !== -1) {
this.sessionFoundCallbacks.splice(index, 1);
}
};
stopKeepAlive = (): void => {