Skip to content

Commit 855076f

Browse files
committed
feat: attachment things
1 parent 4bed7c4 commit 855076f

File tree

4 files changed

+657
-22
lines changed

4 files changed

+657
-22
lines changed

src/lib/acp/session.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export class ACPSession {
3434
updatedAt: string | null = null;
3535

3636
private agentTextBuffer = "";
37+
private userTextBuffer = "";
3738
private currentAgentMessageId: string | null = null;
39+
private currentUserMessageId: string | null = null;
3840
private currentPlanEntryId: string | null = null;
3941
private listeners = new Map<SessionEventType, Set<SessionEventHandler>>();
4042
private pendingPermissions: PermissionRequest[] = [];
@@ -69,6 +71,7 @@ export class ACPSession {
6971

7072
addUserMessage(content: ContentBlock[]): ChatMessage {
7173
this.closeCurrentAgentMessage();
74+
this.closeCurrentUserMessage();
7275

7376
const message: ChatMessage = {
7477
id: this.nextMessageId(),
@@ -121,6 +124,7 @@ export class ACPSession {
121124

122125
finishAgentTurn(): void {
123126
this.closeCurrentAgentMessage();
127+
this.closeCurrentUserMessage();
124128
this.currentPlanEntryId = null;
125129
this.emit("session_end", null);
126130
}
@@ -133,10 +137,14 @@ export class ACPSession {
133137
role: ChatMessage["role"],
134138
content: ContentBlock,
135139
): void {
136-
const message =
137-
role === "agent"
138-
? this.getOrCreateCurrentAgentMessage()
139-
: this.createMessage(role, [], false);
140+
let message: ChatMessage;
141+
if (role === "agent") {
142+
this.closeCurrentUserMessage();
143+
message = this.getOrCreateCurrentAgentMessage();
144+
} else {
145+
this.closeCurrentAgentMessage();
146+
message = this.getOrCreateCurrentUserMessage();
147+
}
140148
if (!message) return;
141149

142150
if (role === "agent") {
@@ -146,17 +154,17 @@ export class ACPSession {
146154
if (content.type === "text") {
147155
if (role === "agent") {
148156
this.agentTextBuffer += content.text;
157+
} else {
158+
this.userTextBuffer += content.text;
149159
}
150160
const existingText = message.content.find((c) => c.type === "text");
151161
if (existingText && existingText.type === "text") {
152162
existingText.text =
153-
role === "agent"
154-
? this.agentTextBuffer
155-
: existingText.text + content.text;
163+
role === "agent" ? this.agentTextBuffer : this.userTextBuffer;
156164
} else {
157165
message.content.push({
158166
type: "text",
159-
text: role === "agent" ? this.agentTextBuffer : content.text,
167+
text: role === "agent" ? this.agentTextBuffer : this.userTextBuffer,
160168
});
161169
}
162170
} else {
@@ -170,6 +178,7 @@ export class ACPSession {
170178
update: SessionUpdate & { sessionUpdate: "tool_call" },
171179
): void {
172180
this.closeCurrentAgentMessage();
181+
this.closeCurrentUserMessage();
173182

174183
const toolCall: ToolCall = {
175184
toolCallId: update.toolCallId,
@@ -300,6 +309,10 @@ export class ACPSession {
300309
this.currentAgentMessageId = message.id;
301310
this.agentTextBuffer = "";
302311
}
312+
if (role === "user") {
313+
this.currentUserMessageId = message.id;
314+
this.userTextBuffer = "";
315+
}
303316
return message;
304317
}
305318

@@ -311,6 +324,14 @@ export class ACPSession {
311324
return this.createMessage("agent", [], true);
312325
}
313326

327+
private getOrCreateCurrentUserMessage(): ChatMessage {
328+
const existing = this.messages.find(
329+
(message) => message.id === this.currentUserMessageId,
330+
);
331+
if (existing) return existing;
332+
return this.createMessage("user", [], false);
333+
}
334+
314335
private closeCurrentAgentMessage(): void {
315336
if (!this.currentAgentMessageId) return;
316337

@@ -326,8 +347,18 @@ export class ACPSession {
326347
this.agentTextBuffer = "";
327348
}
328349

350+
private closeCurrentUserMessage(): void {
351+
if (!this.currentUserMessageId) return;
352+
this.currentUserMessageId = null;
353+
this.userTextBuffer = "";
354+
}
355+
329356
dispose(): void {
330357
this.listeners.clear();
331358
this.pendingPermissions.length = 0;
359+
this.currentAgentMessageId = null;
360+
this.currentUserMessageId = null;
361+
this.agentTextBuffer = "";
362+
this.userTextBuffer = "";
332363
}
333364
}

0 commit comments

Comments
 (0)