Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

fix: group tool activity per message, refresh titles after a run, keep user line breaks

Alan Buscaglia4ca84d3fbfb9
diff --git a/packages/daemon/src/bridge.d.ts b/packages/daemon/src/bridge.d.ts
index f6a8ef1..7e80514 100644
--- a/packages/daemon/src/bridge.d.ts
+++ b/packages/daemon/src/bridge.d.ts
@@ -1,11 +1,11 @@
import type { AgentState, ClientMessage, ServerPayload } from "@gentle-dot/protocol";
import type { AgentSupervisor } from "./supervisor.ts";
export interface BridgeClient {
- send(payload: ServerPayload): void;
+ send(payload: ServerPayload): void;
}
export interface BridgeOptions {
- dataDir: string;
- log?: (line: string) => void;
+ dataDir: string;
+ log?: (line: string) => void;
}
/**
* Translates between protocol v1 clients and the supervised agent: it keeps
@@ -13,39 +13,39 @@ export interface BridgeOptions {
* and broadcasts every agent event to all connected clients.
*/
export declare class DotBridge {
- private readonly clients;
- private readonly asks;
- private readonly runningTools;
- private readonly seenRequestIds;
- private readonly sessionDir;
- private readonly supervisor;
- private readonly options;
- private state;
- private interrupted;
- private nextMessage;
- private assistantId;
- constructor(supervisor: AgentSupervisor, options: BridgeOptions);
- get agentState(): AgentState;
- attach(client: BridgeClient): () => void;
- handle(client: BridgeClient, message: ClientMessage): Promise<void>;
- private dispatch;
- private afterConversationChange;
- private loadHistory;
- private conversationsPayload;
- private conversationRef;
- private activeRef;
- private onAgentEvent;
- private onMessageStart;
- private onMessageUpdate;
- private onMessageEnd;
- private onTool;
- private onUiRequest;
- private resolveAsk;
- private clearRun;
- private deriveState;
- private refreshState;
- private alreadySeen;
- private broadcast;
- private deliver;
- private log;
+ private readonly clients;
+ private readonly asks;
+ private readonly runningTools;
+ private readonly seenRequestIds;
+ private readonly sessionDir;
+ private readonly supervisor;
+ private readonly options;
+ private state;
+ private interrupted;
+ private nextMessage;
+ private assistantId;
+ constructor(supervisor: AgentSupervisor, options: BridgeOptions);
+ get agentState(): AgentState;
+ attach(client: BridgeClient): () => void;
+ handle(client: BridgeClient, message: ClientMessage): Promise<void>;
+ private dispatch;
+ private afterConversationChange;
+ private loadHistory;
+ private conversationsPayload;
+ private conversationRef;
+ private activeRef;
+ private onAgentEvent;
+ private onMessageStart;
+ private onMessageUpdate;
+ private onMessageEnd;
+ private onTool;
+ private onUiRequest;
+ private resolveAsk;
+ private clearRun;
+ private deriveState;
+ private refreshState;
+ private alreadySeen;
+ private broadcast;
+ private deliver;
+ private log;
}
diff --git a/packages/daemon/src/bridge.ts b/packages/daemon/src/bridge.ts
index 390b4b5..4c76c71 100644
--- a/packages/daemon/src/bridge.ts
+++ b/packages/daemon/src/bridge.ts
@@ -218,6 +218,8 @@ export class DotBridge {
case "agent_settled":
this.runningTools.clear();
this.assistantId = undefined;
+ // The title of a new conversation comes from its first message.
+ this.broadcast(this.conversationsPayload());
break;
case "extension_ui_request":
this.onUiRequest(event);
@@ -260,9 +262,11 @@ export class DotBridge {
if (started) this.runningTools.add(id);
else this.runningTools.delete(id);
const status = started ? "running" : event.isError ? "failed" : "done";
+ // Start and end of one tool call must land on the same message.
+ this.assistantId ??= `m${++this.nextMessage}`;
this.broadcast({
type: "activity",
- messageId: this.assistantId ?? `m${++this.nextMessage}`,
+ messageId: this.assistantId,
activity: { id, ...describeTool(String(event.toolName ?? ""), event.args), status },
});
}
diff --git a/packages/daemon/src/config.d.ts b/packages/daemon/src/config.d.ts
index daeb60d..ca6786e 100644
--- a/packages/daemon/src/config.d.ts
+++ b/packages/daemon/src/config.d.ts
@@ -1,12 +1,12 @@
export interface DotConfig {
- port: number;
- host: string;
- dataDir: string;
- workspace: string;
- uiDir: string;
- agentCommand: string;
- agentArgs: string[];
- allowedOrigins: string[];
+ port: number;
+ host: string;
+ dataDir: string;
+ workspace: string;
+ uiDir: string;
+ agentCommand: string;
+ agentArgs: string[];
+ allowedOrigins: string[];
}
/** Resolves configuration from environment variables, then `~/.gentle-dot/config.json`, then defaults. */
export declare function loadConfig(env?: NodeJS.ProcessEnv): DotConfig;
diff --git a/packages/daemon/src/daemon.d.ts b/packages/daemon/src/daemon.d.ts
index d0984db..3debd73 100644
--- a/packages/daemon/src/daemon.d.ts
+++ b/packages/daemon/src/daemon.d.ts
@@ -1,28 +1,28 @@
import { DotBridge } from "./bridge.ts";
import { AgentSupervisor } from "./supervisor.ts";
export interface DaemonOptions {
- port: number;
- host: string;
- dataDir: string;
- workspace: string;
- /** Directory with the built web UI (`index.html`). */
- uiDir: string;
- agentCommand: string;
- agentArgs?: string[];
- agentExtraArgs?: string[];
- agentEnv?: NodeJS.ProcessEnv;
- /** Origins allowed to open the WebSocket, besides the daemon's own and the desktop app's. */
- allowedOrigins?: string[];
- backoffMs?: number[];
- log?: (line: string) => void;
+ port: number;
+ host: string;
+ dataDir: string;
+ workspace: string;
+ /** Directory with the built web UI (`index.html`). */
+ uiDir: string;
+ agentCommand: string;
+ agentArgs?: string[];
+ agentExtraArgs?: string[];
+ agentEnv?: NodeJS.ProcessEnv;
+ /** Origins allowed to open the WebSocket, besides the daemon's own and the desktop app's. */
+ allowedOrigins?: string[];
+ backoffMs?: number[];
+ log?: (line: string) => void;
}
export interface DotDaemon {
- port: number;
- token: string;
- url: string;
- bridge: DotBridge;
- supervisor: AgentSupervisor;
- close(): Promise<void>;
+ port: number;
+ token: string;
+ url: string;
+ bridge: DotBridge;
+ supervisor: AgentSupervisor;
+ close(): Promise<void>;
}
/** Reads the access token from `<dataDir>/token`, creating a random one (mode 0600) when absent. */
export declare function ensureToken(dataDir: string): string;
diff --git a/packages/daemon/src/jsonl.d.ts b/packages/daemon/src/jsonl.d.ts
index c4d5085..36c2d7c 100644
--- a/packages/daemon/src/jsonl.d.ts
+++ b/packages/daemon/src/jsonl.d.ts
@@ -4,11 +4,11 @@
* U+2028 and U+2029, which are valid inside JSON strings.
*/
export declare class JsonlDecoder {
- private pending;
- private readonly onRecord;
- private readonly onMalformed;
- constructor(onRecord: (record: unknown) => void, onMalformed?: (line: string) => void);
- push(chunk: Buffer): void;
- private emit;
+ private pending;
+ private readonly onRecord;
+ private readonly onMalformed;
+ constructor(onRecord: (record: unknown) => void, onMalformed?: (line: string) => void);
+ push(chunk: Buffer): void;
+ private emit;
}
export declare function encodeRecord(record: unknown): string;
diff --git a/packages/daemon/src/presentation.d.ts b/packages/daemon/src/presentation.d.ts
index f1bb989..b62c910 100644
--- a/packages/daemon/src/presentation.d.ts
+++ b/packages/daemon/src/presentation.d.ts
@@ -3,9 +3,12 @@ import type { ActivityKind } from "@gentle-dot/protocol";
* Turns an internal tool call into a neutral activity. Tool names never reach
* the user; only the kind and a short human title do.
*/
-export declare function describeTool(toolName: string, args: unknown): {
- kind: ActivityKind;
- title: string;
+export declare function describeTool(
+ toolName: string,
+ args: unknown,
+): {
+ kind: ActivityKind;
+ title: string;
};
/** Plain text of a user or assistant message whose content is a string or content blocks. */
export declare function textOf(message: unknown): string;
diff --git a/packages/daemon/src/supervisor.d.ts b/packages/daemon/src/supervisor.d.ts
index 397eda8..fedda1c 100644
--- a/packages/daemon/src/supervisor.d.ts
+++ b/packages/daemon/src/supervisor.d.ts
@@ -1,45 +1,48 @@
/** A record the agent wrote to stdout that is not a command response. */
export type AgentRecord = {
- type: string;
- [key: string]: unknown;
+ type: string;
+ [key: string]: unknown;
};
export type RpcResponse = {
- type: "response";
- id?: string;
- command: string;
- success: boolean;
- data?: unknown;
- error?: string;
+ type: "response";
+ id?: string;
+ command: string;
+ success: boolean;
+ data?: unknown;
+ error?: string;
};
export type SupervisorState = "stopped" | "starting" | "ready" | "restarting";
-export type SupervisorEvent = AgentRecord | {
- type: "supervisor_state";
- state: SupervisorState;
-} | {
- type: "interrupted";
-};
+export type SupervisorEvent =
+ | AgentRecord
+ | {
+ type: "supervisor_state";
+ state: SupervisorState;
+ }
+ | {
+ type: "interrupted";
+ };
export interface SupervisorOptions {
- /** Executable to run, for example `gentle-shell`. */
- command: string;
- /** Arguments placed before the RPC arguments (used to run a script with node). */
- args?: string[];
- /** Arguments placed after the RPC arguments. */
- extraArgs?: string[];
- cwd: string;
- /** Directory for `sessions/` and `state.json`. */
- dataDir: string;
- env?: NodeJS.ProcessEnv;
- /** Respawn delays; the last value repeats. Default 1s, 2s, 4s, 8s, 16s, 30s. */
- backoffMs?: number[];
- /** Uptime after which the backoff starts over. Default 30s. */
- stableMs?: number;
- /** Deadline for one command response. Default 5 minutes. */
- requestTimeoutMs?: number;
- /** Deadline for the agent to answer its first `get_state`. Default 60s. */
- startTimeoutMs?: number;
- /** Grace period after closing stdin before SIGTERM. Default 5s. */
- stopTimeoutMs?: number;
- log?: (line: string) => void;
+ /** Executable to run, for example `gentle-shell`. */
+ command: string;
+ /** Arguments placed before the RPC arguments (used to run a script with node). */
+ args?: string[];
+ /** Arguments placed after the RPC arguments. */
+ extraArgs?: string[];
+ cwd: string;
+ /** Directory for `sessions/` and `state.json`. */
+ dataDir: string;
+ env?: NodeJS.ProcessEnv;
+ /** Respawn delays; the last value repeats. Default 1s, 2s, 4s, 8s, 16s, 30s. */
+ backoffMs?: number[];
+ /** Uptime after which the backoff starts over. Default 30s. */
+ stableMs?: number;
+ /** Deadline for one command response. Default 5 minutes. */
+ requestTimeoutMs?: number;
+ /** Deadline for the agent to answer its first `get_state`. Default 60s. */
+ startTimeoutMs?: number;
+ /** Grace period after closing stdin before SIGTERM. Default 5s. */
+ stopTimeoutMs?: number;
+ log?: (line: string) => void;
}
/**
* Runs the agent (`gentle-shell --mode rpc`) as a child process, correlates
@@ -47,45 +50,45 @@ export interface SupervisorOptions {
* after an unexpected exit, reopening the same conversation.
*/
export declare class AgentSupervisor {
- state: SupervisorState;
- busy: boolean;
- sessionFile: string | undefined;
- sessionId: string | undefined;
- model: string | undefined;
- private child;
- private readonly pending;
- private readonly listeners;
- private nextId;
- private attempts;
- private spawnedAt;
- private stopping;
- private interrupted;
- private restartTimer;
- private closeWaiters;
- private readonly sessionDir;
- private readonly stateFile;
- private readonly options;
- constructor(options: SupervisorOptions);
- get pid(): number | undefined;
- onEvent(listener: (event: SupervisorEvent) => void): () => void;
- /** Spawns the agent and resolves once it answered `get_state`. */
- start(): Promise<void>;
- /** Sends a command and resolves with its successful response. */
- request(command: AgentRecord): Promise<RpcResponse>;
- /** Writes a record that has no response, such as `extension_ui_response`. */
- send(record: AgentRecord): void;
- stop(): Promise<void>;
- private spawnAndInitialize;
- private spawnChild;
- private rawRequest;
- private handleRecord;
- private handleClose;
- private scheduleRestart;
- private refreshSession;
- private applyState;
- private readPersistedSession;
- private persistSession;
- private setState;
- private emit;
- private log;
+ state: SupervisorState;
+ busy: boolean;
+ sessionFile: string | undefined;
+ sessionId: string | undefined;
+ model: string | undefined;
+ private child;
+ private readonly pending;
+ private readonly listeners;
+ private nextId;
+ private attempts;
+ private spawnedAt;
+ private stopping;
+ private interrupted;
+ private restartTimer;
+ private closeWaiters;
+ private readonly sessionDir;
+ private readonly stateFile;
+ private readonly options;
+ constructor(options: SupervisorOptions);
+ get pid(): number | undefined;
+ onEvent(listener: (event: SupervisorEvent) => void): () => void;
+ /** Spawns the agent and resolves once it answered `get_state`. */
+ start(): Promise<void>;
+ /** Sends a command and resolves with its successful response. */
+ request(command: AgentRecord): Promise<RpcResponse>;
+ /** Writes a record that has no response, such as `extension_ui_response`. */
+ send(record: AgentRecord): void;
+ stop(): Promise<void>;
+ private spawnAndInitialize;
+ private spawnChild;
+ private rawRequest;
+ private handleRecord;
+ private handleClose;
+ private scheduleRestart;
+ private refreshSession;
+ private applyState;
+ private readPersistedSession;
+ private persistSession;
+ private setState;
+ private emit;
+ private log;
}
diff --git a/packages/daemon/test/gateway.test.ts b/packages/daemon/test/gateway.test.ts
index 1e59a1e..c31abf9 100644
--- a/packages/daemon/test/gateway.test.ts
+++ b/packages/daemon/test/gateway.test.ts
@@ -158,6 +158,8 @@ describe("gateway", () => {
send(client, { type: "send", text: "tool:read" });
const done = await find(client, "activity", (m) => m.activity.status === "done");
expect(done.activity.kind).toBe("read");
+ const started = await find(client, "activity", (m) => m.activity.status === "running");
+ expect(started.messageId).toBe(done.messageId);
await find(client, "agent_state", (m) => m.state === "working");
});
@@ -207,6 +209,16 @@ describe("gateway", () => {
]);
});
+ it("refreshes the conversation list when a run settles so the title follows the first message", async () => {
+ const d = await daemon();
+ const client = await authed(d);
+ send(client, { type: "send", text: "name this conversation" });
+ const listed = await find(client, "conversations", (m) =>
+ m.conversations.some((c) => c.title === "name this conversation"),
+ );
+ expect(listed.activeId).toBe((await find(client, "ready")).conversationId);
+ });
+
it("refuses a conversation id outside the session directory", async () => {
const d = await daemon();
const client = await authed(d);
diff --git a/packages/ui/src/components/MessageList.tsx b/packages/ui/src/components/MessageList.tsx
index 68b4cd8..d9df985 100644
--- a/packages/ui/src/components/MessageList.tsx
+++ b/packages/ui/src/components/MessageList.tsx
@@ -73,7 +73,10 @@ export function MessageList({ messages }: { messages: ChatMessage[] }) {
{messages.map((message) => (
<article key={message.id} className={`message message-${message.role}`}>
{message.activities.length > 0 ? <Activities activities={message.activities} /> : null}
- {message.text ? (
+ {message.text && message.role === "user" ? (
+ <div className="message-body message-plain">{message.text}</div>
+ ) : null}
+ {message.text && message.role === "assistant" ? (
<div className="message-body">
<Markdown remarkPlugins={[remarkGfm]}>{message.text}</Markdown>
</div>
diff --git a/packages/ui/src/styles.css b/packages/ui/src/styles.css
index b20c03d..586cbed 100644
--- a/packages/ui/src/styles.css
+++ b/packages/ui/src/styles.css
@@ -348,6 +348,11 @@ button.icon {
color: #fff;
}
+.message-plain {
+ white-space: pre-wrap;
+ overflow-wrap: anywhere;
+}
+
.message-body > :first-child {
margin-top: 0;
}
diff --git a/packages/ui/test/components.test.tsx b/packages/ui/test/components.test.tsx
index 6e75815..b97b4c0 100644
--- a/packages/ui/test/components.test.tsx
+++ b/packages/ui/test/components.test.tsx
@@ -110,6 +110,20 @@ describe("MessageList", () => {
});
});
+describe("MessageList user text", () => {
+ it("keeps the user's line breaks as typed", () => {
+ const { container } = render(
+ <MessageList
+ messages={[{ id: "u1", role: "user", text: "first line\nsecond", streaming: false, activities: [] }]}
+ />,
+ );
+ const bubble = container.querySelector(".message-user .message-body");
+ expect(bubble?.textContent).toBe("first line\nsecond");
+ expect(bubble?.querySelector("p")).toBeNull();
+ expect(bubble).toHaveClass("message-plain");
+ });
+});
+
describe("ChatSurface", () => {
it("shows the interruption prompt and continues", async () => {
const send = vi.fn();