feat(coding-agent): add get_entries and get_tree RPC commands
Adds two read-only RPC commands exposing existing SessionManager reads: - get_entries: all session entries in append order, with optional since-entry-id cursor (strictly-after semantics, error on unknown id), plus current leafId. - get_tree: getTree() roots plus current leafId. Since sessions are append-only trees with stable entry ids, an entry id is a durable cursor: external orchestrators can use these commands to catch up after a restart without losing pre-compaction history, and can observe branch structure (/tree jumps, abandoned branches) that get_messages hides.
This commit is contained in:
@@ -10,6 +10,7 @@ import type { ImageContent } from "@earendil-works/pi-ai";
|
||||
import type { SessionStats } from "../../core/agent-session.ts";
|
||||
import type { BashResult } from "../../core/bash-executor.ts";
|
||||
import type { CompactionResult } from "../../core/compaction/index.ts";
|
||||
import type { SessionEntry, SessionTreeNode } from "../../core/session-manager.ts";
|
||||
import { attachJsonlLineReader, serializeJsonLine } from "./jsonl.ts";
|
||||
import type { RpcCommand, RpcResponse, RpcSessionState, RpcSlashCommand } from "./rpc-types.ts";
|
||||
|
||||
@@ -388,6 +389,22 @@ export class RpcClient {
|
||||
return this.getData<{ messages: Array<{ entryId: string; text: string }> }>(response).messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get session entries in append order, optionally only those after the `since` entry id.
|
||||
*/
|
||||
async getEntries(since?: string): Promise<{ entries: SessionEntry[]; leafId: string | null }> {
|
||||
const response = await this.send({ type: "get_entries", since });
|
||||
return this.getData<{ entries: SessionEntry[]; leafId: string | null }>(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the session entry tree.
|
||||
*/
|
||||
async getTree(): Promise<{ tree: SessionTreeNode[]; leafId: string | null }> {
|
||||
const response = await this.send({ type: "get_tree" });
|
||||
return this.getData<{ tree: SessionTreeNode[]; leafId: string | null }>(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get text of last assistant message.
|
||||
*/
|
||||
|
||||
@@ -606,6 +606,24 @@ export async function runRpcMode(runtimeHost: AgentSessionRuntime): Promise<neve
|
||||
return success(id, "get_fork_messages", { messages });
|
||||
}
|
||||
|
||||
case "get_entries": {
|
||||
const sessionManager = session.sessionManager;
|
||||
let entries = sessionManager.getEntries();
|
||||
if (command.since !== undefined) {
|
||||
const sinceIndex = entries.findIndex((e) => e.id === command.since);
|
||||
if (sinceIndex === -1) {
|
||||
return error(id, "get_entries", `Entry not found: ${command.since}`);
|
||||
}
|
||||
entries = entries.slice(sinceIndex + 1);
|
||||
}
|
||||
return success(id, "get_entries", { entries, leafId: sessionManager.getLeafId() });
|
||||
}
|
||||
|
||||
case "get_tree": {
|
||||
const sessionManager = session.sessionManager;
|
||||
return success(id, "get_tree", { tree: sessionManager.getTree(), leafId: sessionManager.getLeafId() });
|
||||
}
|
||||
|
||||
case "get_last_assistant_text": {
|
||||
const text = session.getLastAssistantText();
|
||||
return success(id, "get_last_assistant_text", { text });
|
||||
|
||||
@@ -10,6 +10,7 @@ import type { ImageContent, Model } from "@earendil-works/pi-ai";
|
||||
import type { SessionStats } from "../../core/agent-session.ts";
|
||||
import type { BashResult } from "../../core/bash-executor.ts";
|
||||
import type { CompactionResult } from "../../core/compaction/index.ts";
|
||||
import type { SessionEntry, SessionTreeNode } from "../../core/session-manager.ts";
|
||||
import type { SourceInfo } from "../../core/source-info.ts";
|
||||
|
||||
// ============================================================================
|
||||
@@ -59,6 +60,8 @@ export type RpcCommand =
|
||||
| { id?: string; type: "fork"; entryId: string }
|
||||
| { id?: string; type: "clone" }
|
||||
| { id?: string; type: "get_fork_messages" }
|
||||
| { id?: string; type: "get_entries"; since?: string }
|
||||
| { id?: string; type: "get_tree" }
|
||||
| { id?: string; type: "get_last_assistant_text" }
|
||||
| { id?: string; type: "set_session_name"; name: string }
|
||||
|
||||
@@ -181,6 +184,20 @@ export type RpcResponse =
|
||||
success: true;
|
||||
data: { messages: Array<{ entryId: string; text: string }> };
|
||||
}
|
||||
| {
|
||||
id?: string;
|
||||
type: "response";
|
||||
command: "get_entries";
|
||||
success: true;
|
||||
data: { entries: SessionEntry[]; leafId: string | null };
|
||||
}
|
||||
| {
|
||||
id?: string;
|
||||
type: "response";
|
||||
command: "get_tree";
|
||||
success: true;
|
||||
data: { tree: SessionTreeNode[]; leafId: string | null };
|
||||
}
|
||||
| {
|
||||
id?: string;
|
||||
type: "response";
|
||||
|
||||
Reference in New Issue
Block a user