merge: resolve conflicts with main branch

- Merge auth-profiles feature from main into runner.ts
- Merge closeCallbacks feature from main into async-agent.ts
- Regenerate pnpm-lock.yaml with new dependencies

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jiang Bohan 2026-02-03 18:57:05 +08:00
commit dafbf856ac
83 changed files with 12239 additions and 258 deletions

View file

@ -1,18 +1,20 @@
import { v7 as uuidv7 } from "uuid";
import type { AgentEvent } from "@mariozechner/pi-agent-core";
import { Agent } from "./runner.js";
import { Channel } from "./channel.js";
import { extractText } from "./extract-text.js";
import type { AgentOptions, Message } from "./types.js";
import type { StreamPayload } from "@multica/sdk";
const devNull = { write: () => true } as NodeJS.WritableStream;
/** Discriminated union of legacy Message (error fallback) and raw AgentEvent */
export type ChannelItem = Message | AgentEvent;
export class AsyncAgent {
private readonly agent: Agent;
private readonly channel = new Channel<Message>();
private readonly channel = new Channel<ChannelItem>();
private _closed = false;
private queue: Promise<void> = Promise.resolve();
private streamCallback?: (payload: StreamPayload) => void;
private closeCallbacks: Array<() => void> = [];
readonly sessionId: string;
constructor(options?: AgentOptions) {
@ -21,18 +23,17 @@ export class AsyncAgent {
logger: { stdout: devNull, stderr: devNull },
});
this.sessionId = this.agent.sessionId;
this.setupStreamEvents();
// Forward raw AgentEvent into the channel
this.agent.subscribe((event: AgentEvent) => {
this.channel.send(event);
});
}
get closed(): boolean {
return this._closed;
}
/** Register callback for streaming events */
onStream(cb: (payload: StreamPayload) => void): void {
this.streamCallback = cb;
}
/** Write message to agent (non-blocking, serialized queue) */
write(content: string): void {
if (this._closed) throw new Error("Agent is closed");
@ -41,15 +42,9 @@ export class AsyncAgent {
.then(async () => {
if (this._closed) return;
const result = await this.agent.run(content);
// Only send final message via channel if no stream callback
// (stream callback already sent the final content)
if (!this.streamCallback) {
if (result.text) {
this.channel.send({ id: uuidv7(), content: result.text });
}
if (result.error) {
this.channel.send({ id: uuidv7(), content: `[error] ${result.error}` });
}
// Normal text is delivered via message_end event; only handle errors here
if (result.error) {
this.channel.send({ id: uuidv7(), content: `[error] ${result.error}` });
}
})
.catch((err) => {
@ -58,16 +53,39 @@ export class AsyncAgent {
});
}
/** Continuously read message stream */
read(): AsyncIterable<Message> {
/** Continuously read channel stream (AgentEvent + error Messages) */
read(): AsyncIterable<ChannelItem> {
return this.channel;
}
/** Close agent, stop all reads */
/** Returns a promise that resolves when the current message queue is drained */
waitForIdle(): Promise<void> {
return this.queue;
}
/** Register a callback to be invoked when the agent is closed */
onClose(callback: () => void): void {
if (this._closed) {
// Already closed, fire immediately
callback();
return;
}
this.closeCallbacks.push(callback);
}
/** Close agent, stop all reads, fire close callbacks */
close(): void {
if (this._closed) return;
this._closed = true;
this.channel.close();
for (const cb of this.closeCallbacks) {
try {
cb();
} catch {
// Don't let callback errors prevent other callbacks
}
}
this.closeCallbacks = [];
}
/** Get current active tool names */
@ -130,50 +148,4 @@ export class AsyncAgent {
getProfileId(): string | undefined {
return this.agent.getProfileId();
}
private setupStreamEvents(): void {
let currentStreamId: string | null = null;
this.agent.subscribe((event) => {
if (!this.streamCallback) return;
switch (event.type) {
case "message_start": {
if (event.message.role === "assistant") {
currentStreamId = uuidv7();
this.streamCallback({
streamId: currentStreamId,
agentId: this.sessionId,
state: "delta",
content: extractText(event.message),
});
}
break;
}
case "message_update": {
if (event.message.role === "assistant" && currentStreamId) {
this.streamCallback({
streamId: currentStreamId,
agentId: this.sessionId,
state: "delta",
content: extractText(event.message),
});
}
break;
}
case "message_end": {
if (event.message.role === "assistant" && currentStreamId) {
this.streamCallback({
streamId: currentStreamId,
agentId: this.sessionId,
state: "final",
content: extractText(event.message),
});
currentStreamId = null;
}
break;
}
}
});
}
}