Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(agent): add SyncAgent and AsyncAgent wrapper classes

yushen6e82219630c0
diff --git a/src/agent/async-agent.ts b/src/agent/async-agent.ts
new file mode 100644
index 0000000000..f979fb8da4
--- /dev/null
+++ b/src/agent/async-agent.ts
@@ -0,0 +1,81 @@
+import { v7 as uuidv7 } from "uuid";
+import { Agent } from "./runner.js";
+import { Channel } from "./channel.js";
+import type { AgentOptions, Message } from "./types.js";
+
+export class AsyncAgent {
+ private readonly agent: Agent;
+ private readonly channel = new Channel<Message>();
+ private _closed = false;
+ private queue: Promise<void> = Promise.resolve();
+ readonly sessionId: string;
+
+ constructor(options?: AgentOptions) {
+ this.agent = new Agent({
+ ...options,
+ logger: {
+ stdout: this.createChannelStream("[assistant] "),
+ stderr: this.createChannelStream("[tool] "),
+ },
+ });
+ this.sessionId = this.agent.sessionId;
+ }
+
+ get closed(): boolean {
+ return this._closed;
+ }
+
+ /** Write message to agent (non-blocking, serialized queue) */
+ write(content: string): void {
+ if (this._closed) throw new Error("Agent is closed");
+
+ this.queue = this.queue
+ .then(async () => {
+ const result = await this.agent.run(content);
+ if (result.error) {
+ this.channel.send({
+ id: uuidv7(),
+ content: `[error] ${result.error}`,
+ });
+ }
+ })
+ .catch((err) => {
+ const message = err instanceof Error ? err.message : String(err);
+ this.channel.send({ id: uuidv7(), content: `[error] ${message}` });
+ });
+ }
+
+ /** Continuously read message stream */
+ read(): AsyncIterable<Message> {
+ return this.channel;
+ }
+
+ /** Close agent, stop all reads */
+ close(): void {
+ if (this._closed) return;
+ this._closed = true;
+ this.channel.close();
+ }
+
+ private createChannelStream(prefix: string): NodeJS.WritableStream {
+ let buffer = "";
+ return {
+ write: (chunk: any) => {
+ if (this._closed) return false;
+ const text =
+ typeof chunk === "string"
+ ? chunk
+ : chunk?.toString?.() ?? String(chunk);
+ if (!text) return true;
+ buffer += text;
+ const parts = buffer.split("\n");
+ buffer = parts.pop() ?? "";
+ for (const part of parts) {
+ if (part.length === 0) continue;
+ this.channel.send({ id: uuidv7(), content: `${prefix}${part}` });
+ }
+ return true;
+ },
+ } as NodeJS.WritableStream;
+ }
+}
diff --git a/src/agent/channel.ts b/src/agent/channel.ts
new file mode 100644
index 0000000000..5073c3cdcb
--- /dev/null
+++ b/src/agent/channel.ts
@@ -0,0 +1,64 @@
+/**
+ * Go channel style async iterable queue.
+ * Supports multiple writers, single reader, iteration ends after close.
+ */
+export class Channel<T> implements AsyncIterable<T> {
+ private buffer: T[] = [];
+ private closed = false;
+
+ private readers: Array<{
+ resolve: (result: IteratorResult<T>) => void;
+ }> = [];
+
+ get isClosed(): boolean {
+ return this.closed;
+ }
+
+ get size(): number {
+ return this.buffer.length;
+ }
+
+ /** Send value to channel. Returns false when channel is closed. */
+ send(value: T): boolean {
+ if (this.closed) return false;
+
+ const reader = this.readers.shift();
+ if (reader) {
+ reader.resolve({ value, done: false });
+ return true;
+ }
+
+ this.buffer.push(value);
+ return true;
+ }
+
+ /** Close channel, wake up all waiting readers. */
+ close(): void {
+ if (this.closed) return;
+ this.closed = true;
+
+ for (const reader of this.readers) {
+ reader.resolve({ value: undefined as T, done: true });
+ }
+ this.readers = [];
+ }
+
+ [Symbol.asyncIterator](): AsyncIterator<T> {
+ return {
+ next: (): Promise<IteratorResult<T>> => {
+ if (this.buffer.length > 0) {
+ const value = this.buffer.shift()!;
+ return Promise.resolve({ value, done: false });
+ }
+
+ if (this.closed) {
+ return Promise.resolve({ value: undefined as T, done: true });
+ }
+
+ return new Promise<IteratorResult<T>>((resolve) => {
+ this.readers.push({ resolve });
+ });
+ },
+ };
+ }
+}
diff --git a/src/agent/index.ts b/src/agent/index.ts
index c338416099..16dac2f969 100644
--- a/src/agent/index.ts
+++ b/src/agent/index.ts
@@ -3,3 +3,6 @@ export * from "./types.js";
export * from "./profile/index.js";
export * from "./context-window/index.js";
export * from "./skills/index.js";
+export * from "./channel.js";
+export * from "./sync-agent.js";
+export * from "./async-agent.js";
diff --git a/src/agent/sync-agent.ts b/src/agent/sync-agent.ts
new file mode 100644
index 0000000000..d2f77ae283
--- /dev/null
+++ b/src/agent/sync-agent.ts
@@ -0,0 +1,16 @@
+import { Agent } from "./runner.js";
+import type { AgentOptions, AgentRunResult } from "./types.js";
+
+export class SyncAgent {
+ private readonly agent: Agent;
+ readonly sessionId: string;
+
+ constructor(options?: AgentOptions) {
+ this.agent = new Agent(options);
+ this.sessionId = this.agent.sessionId;
+ }
+
+ async run(prompt: string): Promise<AgentRunResult> {
+ return this.agent.run(prompt);
+ }
+}
diff --git a/src/agent/types.ts b/src/agent/types.ts
index 60494a0714..94ab9c451c 100644
--- a/src/agent/types.ts
+++ b/src/agent/types.ts
@@ -55,3 +55,8 @@ export type AgentOptions = {
/** Additional directories to search for skills */
extraSkillDirs?: string[] | undefined;
};
+
+export interface Message {
+ readonly id: string;
+ readonly content: string;
+}