refactor(hub): add conversation-first rpc aliases

This commit is contained in:
Jiayuan Zhang 2026-02-17 02:34:58 +08:00
parent 708116afb1
commit 3c8569151a
6 changed files with 104 additions and 0 deletions

View file

@ -103,6 +103,11 @@ export interface ListAgentsResult {
agents: { id: string; closed: boolean }[];
}
/** listConversations - no params needed (conversation-first alias of listAgents) */
export interface ListConversationsResult {
conversations: { id: string; closed: boolean }[];
}
/** createAgent - request params */
export interface CreateAgentParams {
id?: string;
@ -113,6 +118,16 @@ export interface CreateAgentResult {
id: string;
}
/** createConversation - request params (conversation-first alias of createAgent) */
export interface CreateConversationParams {
id?: string;
}
/** createConversation - response payload */
export interface CreateConversationResult {
id: string;
}
/** deleteAgent - request params */
export interface DeleteAgentParams {
id: string;
@ -123,6 +138,16 @@ export interface DeleteAgentResult {
ok: boolean;
}
/** deleteConversation - request params (conversation-first alias of deleteAgent) */
export interface DeleteConversationParams {
id: string;
}
/** deleteConversation - response payload */
export interface DeleteConversationResult {
ok: boolean;
}
/** updateGateway - request params */
export interface UpdateGatewayParams {
url: string;

View file

@ -24,6 +24,9 @@ import { createGetHubInfoHandler } from "./rpc/handlers/get-hub-info.js";
import { createListAgentsHandler } from "./rpc/handlers/list-agents.js";
import { createCreateAgentHandler } from "./rpc/handlers/create-agent.js";
import { createDeleteAgentHandler } from "./rpc/handlers/delete-agent.js";
import { createListConversationsHandler } from "./rpc/handlers/list-conversations.js";
import { createCreateConversationHandler } from "./rpc/handlers/create-conversation.js";
import { createDeleteConversationHandler } from "./rpc/handlers/delete-conversation.js";
import { createUpdateGatewayHandler } from "./rpc/handlers/update-gateway.js";
import { createGetLastHeartbeatHandler } from "./rpc/handlers/get-last-heartbeat.js";
import { createSetHeartbeatsHandler } from "./rpc/handlers/set-heartbeats.js";
@ -123,6 +126,9 @@ export class Hub {
this.rpc.register("listAgents", createListAgentsHandler(this));
this.rpc.register("createAgent", createCreateAgentHandler(this));
this.rpc.register("deleteAgent", createDeleteAgentHandler(this));
this.rpc.register("listConversations", createListConversationsHandler(this));
this.rpc.register("createConversation", createCreateConversationHandler(this));
this.rpc.register("deleteConversation", createDeleteConversationHandler(this));
this.rpc.register("updateGateway", createUpdateGatewayHandler(this));
this.rpc.register("last-heartbeat", createGetLastHeartbeatHandler(this));
this.rpc.register("set-heartbeats", createSetHeartbeatsHandler(this));

View file

@ -0,0 +1,13 @@
import type { RpcHandler } from "../dispatcher.js";
interface HubLike {
createConversation(id?: string): { sessionId: string };
}
export function createCreateConversationHandler(hub: HubLike): RpcHandler {
return (params: unknown) => {
const { id } = (params ?? {}) as { id?: string };
const conversation = hub.createConversation(id);
return { id: conversation.sessionId };
};
}

View file

@ -0,0 +1,19 @@
import { RpcError, type RpcHandler } from "../dispatcher.js";
interface HubLike {
closeConversation(id: string): boolean;
}
export function createDeleteConversationHandler(hub: HubLike): RpcHandler {
return (params: unknown) => {
if (!params || typeof params !== "object") {
throw new RpcError("INVALID_PARAMS", "params must be an object");
}
const { id } = params as { id?: string };
if (!id) {
throw new RpcError("INVALID_PARAMS", "Missing required param: id");
}
const ok = hub.closeConversation(id);
return { ok };
};
}

View file

@ -0,0 +1,16 @@
import type { RpcHandler } from "../dispatcher.js";
interface HubLike {
listConversations(): string[];
getConversation(id: string): { closed: boolean } | undefined;
}
export function createListConversationsHandler(hub: HubLike): RpcHandler {
return () => {
const conversations = hub.listConversations().map((id) => {
const conversation = hub.getConversation(id);
return { id, closed: conversation?.closed ?? true };
});
return { conversations };
};
}

View file

@ -112,6 +112,11 @@ export interface ListAgentsResult {
agents: { id: string; closed: boolean }[];
}
/** listConversations - no params needed (conversation-first alias of listAgents) */
export interface ListConversationsResult {
conversations: { id: string; closed: boolean }[];
}
/** createAgent - request params */
export interface CreateAgentParams {
id?: string;
@ -122,6 +127,16 @@ export interface CreateAgentResult {
id: string;
}
/** createConversation - request params (conversation-first alias of createAgent) */
export interface CreateConversationParams {
id?: string;
}
/** createConversation - response payload */
export interface CreateConversationResult {
id: string;
}
/** deleteAgent - request params */
export interface DeleteAgentParams {
id: string;
@ -132,6 +147,16 @@ export interface DeleteAgentResult {
ok: boolean;
}
/** deleteConversation - request params (conversation-first alias of deleteAgent) */
export interface DeleteConversationParams {
id: string;
}
/** deleteConversation - response payload */
export interface DeleteConversationResult {
ok: boolean;
}
/** updateGateway - request params */
export interface UpdateGatewayParams {
url: string;