Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions cli/src/agents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,11 @@ export class AgentsManager {
const sessionId = result.session.id ?? result.response.sessionId;
this.sessionRuntimes.set(this.sessionKey(agentId, sessionId), agent);
this.sessionAgents.set(sessionId, agentId);
this.rememberSessionClient(agentId, sessionId, clientId);
const availableCommands = latestAvailableCommands(result.updates);
if (availableCommands) {
this.updateSnapshotState(agentId, sessionId, { availableCommands });
}
return result;
}

Expand Down Expand Up @@ -952,6 +957,7 @@ export class AgentsManager {
this.sessionRuntimes.clear();
this.sessionRuntimeCleanupTimers.clear();
this.sessionAgents.clear();
this.sessionStates.clear();
}

getAvailableAgents(): AiBackend[] {
Expand All @@ -971,6 +977,7 @@ export class AgentsManager {
private attachedSessionClients = new Map<string, Set<string>>();
private sessionRevisions = new Map<string, number>();
private sessionRuntimeStates = new Map<string, AiSessionRuntimeState>();
private sessionStates = new Map<string, Partial<AiSessionState>>();
private permissionListenerCleanups = new Map<string, () => void>();

subscribe(
Expand Down Expand Up @@ -1767,6 +1774,16 @@ export class AgentsManager {
this.upsertSnapshotMessage(backend, sessionId, message as AcpMessage);
}
}
if (
typeof sessionId === "string" &&
event.type === "session.status" &&
Array.isArray(event.properties.availableCommands)
) {
this.updateSnapshotState(backend, sessionId, {
availableCommands: event.properties
.availableCommands as AiSessionState["availableCommands"],
});
}
const targets =
typeof sessionId === "string"
? this.getEventTargetClientIds(backend, sessionId, clientId)
Expand Down Expand Up @@ -1794,8 +1811,17 @@ export class AgentsManager {
sessionId: string,
snapshot: AttachedSessionSnapshot,
) {
this.sessionSnapshots.set(this.sessionKey(agentId, sessionId), snapshot);
return snapshot;
const key = this.sessionKey(agentId, sessionId);
const state = this.sessionStates.get(key);
const next = {
...snapshot,
state: {
...(snapshot.state ?? {}),
...(state ?? {}),
},
};
this.sessionSnapshots.set(key, next);
return next;
}

private upsertSnapshotMessage(
Expand All @@ -1822,6 +1848,28 @@ export class AgentsManager {
});
}

private updateSnapshotState(
agentId: AiBackend,
sessionId: string,
state: Partial<AiSessionState>,
) {
const key = this.sessionKey(agentId, sessionId);
this.sessionStates.set(key, {
...(this.sessionStates.get(key) ?? {}),
...state,
});
const snapshot = this.sessionSnapshots.get(key);
if (!snapshot) return;
this.sessionSnapshots.set(key, {
...snapshot,
state: {
...snapshot.state,
...state,
},
revision: this.getSessionRevision(agentId, sessionId),
});
}

private refreshSessionSnapshot(
clientId: string,
agentId: AiBackend,
Expand Down