refactor: memoize useWebSocket return value and improve formatting consistency
This commit is contained in:
@@ -1,21 +1,43 @@
|
|||||||
import type { SessionNotification, RequestPermissionRequest, ClientSideConnection } from "@agentclientprotocol/sdk";
|
import type {
|
||||||
|
SessionNotification,
|
||||||
|
RequestPermissionRequest,
|
||||||
|
ClientSideConnection,
|
||||||
|
} from "@agentclientprotocol/sdk";
|
||||||
import type { ChildProcess } from "child_process";
|
import type { ChildProcess } from "child_process";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extended update types with messageId for proper deduplication
|
* Extended update types with messageId for proper deduplication
|
||||||
* messageId is optional since some sources may not provide it
|
* messageId is optional since some sources may not provide it
|
||||||
*/
|
*/
|
||||||
type UserMessageChunkWithId = Extract<SessionNotification['update'], { sessionUpdate: 'user_message_chunk' }> & { messageId?: string };
|
type UserMessageChunkWithId = Extract<
|
||||||
type AgentMessageChunkWithId = Extract<SessionNotification['update'], { sessionUpdate: 'agent_message_chunk' }> & { messageId?: string };
|
SessionNotification["update"],
|
||||||
type AgentThoughtChunkWithId = Extract<SessionNotification['update'], { sessionUpdate: 'agent_thought_chunk' }> & { messageId?: string };
|
{ sessionUpdate: "user_message_chunk" }
|
||||||
|
> & { messageId?: string };
|
||||||
|
type AgentMessageChunkWithId = Extract<
|
||||||
|
SessionNotification["update"],
|
||||||
|
{ sessionUpdate: "agent_message_chunk" }
|
||||||
|
> & { messageId?: string };
|
||||||
|
type AgentThoughtChunkWithId = Extract<
|
||||||
|
SessionNotification["update"],
|
||||||
|
{ sessionUpdate: "agent_thought_chunk" }
|
||||||
|
> & { messageId?: string };
|
||||||
|
|
||||||
export type EnrichedSessionUpdate =
|
export type EnrichedSessionUpdate =
|
||||||
| UserMessageChunkWithId
|
| UserMessageChunkWithId
|
||||||
| AgentMessageChunkWithId
|
| AgentMessageChunkWithId
|
||||||
| AgentThoughtChunkWithId
|
| AgentThoughtChunkWithId
|
||||||
| Exclude<SessionNotification['update'], { sessionUpdate: 'user_message_chunk' | 'agent_message_chunk' | 'agent_thought_chunk' }>;
|
| Exclude<
|
||||||
|
SessionNotification["update"],
|
||||||
|
{
|
||||||
|
sessionUpdate:
|
||||||
|
| "user_message_chunk"
|
||||||
|
| "agent_message_chunk"
|
||||||
|
| "agent_thought_chunk";
|
||||||
|
}
|
||||||
|
>;
|
||||||
|
|
||||||
export interface EnrichedSessionNotification extends Omit<SessionNotification, 'update'> {
|
export interface EnrichedSessionNotification
|
||||||
|
extends Omit<SessionNotification, "update"> {
|
||||||
update: EnrichedSessionUpdate;
|
update: EnrichedSessionUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,10 +45,15 @@ export interface EnrichedSessionNotification extends Omit<SessionNotification, '
|
|||||||
* Discriminated union for all notification types in the agent update stream
|
* Discriminated union for all notification types in the agent update stream
|
||||||
*/
|
*/
|
||||||
export type AgentNotification =
|
export type AgentNotification =
|
||||||
| { type: 'session'; notification: EnrichedSessionNotification }
|
| { type: "session"; notification: EnrichedSessionNotification }
|
||||||
| { type: 'permission'; requestId: string; request: RequestPermissionRequest }
|
| { type: "permission"; requestId: string; request: RequestPermissionRequest }
|
||||||
| { type: 'permission_resolved'; requestId: string; agentId: string; optionId: string }
|
| {
|
||||||
| { type: 'status'; status: AgentStatus; error?: string };
|
type: "permission_resolved";
|
||||||
|
requestId: string;
|
||||||
|
agentId: string;
|
||||||
|
optionId: string;
|
||||||
|
}
|
||||||
|
| { type: "status"; status: AgentStatus; error?: string };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Status of an agent
|
* Status of an agent
|
||||||
@@ -64,7 +91,11 @@ export interface AgentRuntime {
|
|||||||
* Discriminated union for agent state
|
* Discriminated union for agent state
|
||||||
*/
|
*/
|
||||||
export type ManagedAgentState =
|
export type ManagedAgentState =
|
||||||
| { type: "uninitialized"; persistedSessionId: string | null; lastError?: string }
|
| {
|
||||||
|
type: "uninitialized";
|
||||||
|
persistedSessionId: string | null;
|
||||||
|
lastError?: string;
|
||||||
|
}
|
||||||
| {
|
| {
|
||||||
type: "initializing";
|
type: "initializing";
|
||||||
persistedSessionId: string | null;
|
persistedSessionId: string | null;
|
||||||
|
|||||||
@@ -284,7 +284,8 @@ export class Session {
|
|||||||
const hasUserMessage = updates.some(
|
const hasUserMessage = updates.some(
|
||||||
(update) =>
|
(update) =>
|
||||||
update.notification.type === "session" &&
|
update.notification.type === "session" &&
|
||||||
update.notification.notification.update.sessionUpdate === "user_message_chunk"
|
update.notification.notification.update.sessionUpdate ===
|
||||||
|
"user_message_chunk"
|
||||||
);
|
);
|
||||||
|
|
||||||
// Need at least one user message and some additional updates (3-5) for context
|
// Need at least one user message and some additional updates (3-5) for context
|
||||||
@@ -467,14 +468,15 @@ export class Session {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "create_agent_request":
|
case "create_agent_request":
|
||||||
await this.handleCreateAgentRequest(msg.cwd, msg.initialMode, msg.requestId);
|
await this.handleCreateAgentRequest(
|
||||||
|
msg.cwd,
|
||||||
|
msg.initialMode,
|
||||||
|
msg.requestId
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "initialize_agent_request":
|
case "initialize_agent_request":
|
||||||
await this.handleInitializeAgentRequest(
|
await this.handleInitializeAgentRequest(msg.agentId, msg.requestId);
|
||||||
msg.agentId,
|
|
||||||
msg.requestId
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "set_agent_mode":
|
case "set_agent_mode":
|
||||||
@@ -790,8 +792,12 @@ export class Session {
|
|||||||
this.subscribeToAgent(agentId);
|
this.subscribeToAgent(agentId);
|
||||||
|
|
||||||
// Auto-initialize agent immediately on explicit creation so it's ready to use
|
// Auto-initialize agent immediately on explicit creation so it's ready to use
|
||||||
console.log(`[Session ${this.clientId}] Auto-initializing agent ${agentId}`);
|
console.log(
|
||||||
const { info } = await this.agentManager.initializeAgentAndGetHistory(agentId);
|
`[Session ${this.clientId}] Auto-initializing agent ${agentId}`
|
||||||
|
);
|
||||||
|
const { info } = await this.agentManager.initializeAgentAndGetHistory(
|
||||||
|
agentId
|
||||||
|
);
|
||||||
|
|
||||||
// Emit agent_created message with initialized info
|
// Emit agent_created message with initialized info
|
||||||
this.emit({
|
this.emit({
|
||||||
|
|||||||
Reference in New Issue
Block a user