Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
refactor(core-hub): remove legacy agent RPC and fallback routing
Jiayuan Zhang5af7aa784019
diff --git a/packages/core/src/channels/manager.test.ts b/packages/core/src/channels/manager.test.tsindex 10b258e6d9..e8e31cc8f4 100644--- a/packages/core/src/channels/manager.test.ts+++ b/packages/core/src/channels/manager.test.ts@@ -52,15 +52,8 @@ function createHarness() {return harness.agent;});- const mainConversation = createAgentHarness("main-conv");- const createAgent = vi.fn(() => {- conversations.set(mainConversation.agent.sessionId, mainConversation);- return mainConversation.agent;- });-const hub = {- listAgents: vi.fn(() => ["agent-1"]),- createAgent,+ listConversations: vi.fn(() => ["existing-conv"]),createConversation,getConversation: vi.fn((conversationId: string) => conversations.get(conversationId)?.agent),getConversationAgentId: vi.fn(() => "agent-1"),diff --git a/packages/core/src/channels/manager.ts b/packages/core/src/channels/manager.tsindex dbb328576c..d6bae2aa7a 100644--- a/packages/core/src/channels/manager.ts+++ b/packages/core/src/channels/manager.ts@@ -309,12 +309,13 @@ export class ChannelManager {}private resolveDefaultAgentAndConversation(): { agentId: string; conversation?: AsyncAgent } {- const existingAgentId = this.hub.listAgents()[0];- if (existingAgentId) {+ const existingConversationId = this.hub.listConversations()[0];+ if (existingConversationId) {+ const existingAgentId = this.hub.getConversationAgentId(existingConversationId) ?? existingConversationId;return { agentId: existingAgentId };}- const mainConversation = this.hub.createAgent();+ const mainConversation = this.hub.createConversation();const agentId = this.hub.getConversationAgentId(mainConversation.sessionId) ?? mainConversation.sessionId;return { agentId, conversation: mainConversation };}diff --git a/packages/core/src/hub/hub.ts b/packages/core/src/hub/hub.tsindex b735f20cb7..fdeb5356d5 100644--- a/packages/core/src/hub/hub.ts+++ b/packages/core/src/hub/hub.ts@@ -27,9 +27,6 @@ import {import { RpcDispatcher, RpcError } from "./rpc/dispatcher.js";import { createGetAgentMessagesHandler } from "./rpc/handlers/get-agent-messages.js";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";@@ -81,8 +78,6 @@ export interface InboundMessageEvent {}export class Hub {- private readonly allowLegacyAgentConversationFallback = process.env.MULTICA_ALLOW_LEGACY_AGENT_FALLBACK === "1";- private readonly warnedConversationFallbackAgents = new Set<string>();// Runtime conversation map (conversationId -> AsyncAgent).private readonly agents = new Map<string, AsyncAgent>();// Conversation ownership map (conversationId -> logical agentId).@@ -140,7 +135,7 @@ export class Hub {}));this.rpc.register("generateChannelWelcome", createGenerateChannelWelcomeHandler(this));this.rpc.register("getAgentMessages", createGetAgentMessagesHandler((agentId, conversationId) => {- const resolvedConversationId = this.resolveConversationId(agentId, conversationId);+ const resolvedConversationId = this.normalizeId(conversationId);if (!resolvedConversationId) return null;return {conversationId: resolvedConversationId,@@ -148,9 +143,6 @@ export class Hub {};}));this.rpc.register("getHubInfo", createGetHubInfoHandler(this));- 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));@@ -425,41 +417,35 @@ export class Hub {const payload = msg.payload as {agentId?: string;conversationId?: string;- sessionId?: string;content?: string;} | undefined;if (!this.deviceStore.isAllowed(msg.from)) {console.warn(`[Hub] Rejected message from unverified device: ${msg.from}`);- const inboundSessionId = payload?.sessionId ?? payload?.conversationId;this.client.send(msg.from, "error", {code: "UNAUTHORIZED",message: "Device not verified. Please complete verification first.",messageId: msg.id,- ...(inboundSessionId ? { conversationId: inboundSessionId, sessionId: inboundSessionId } : {}),+ ...(payload?.conversationId ? { conversationId: payload.conversationId } : {}),+ });+ return;+ }+ const inboundConversationId = this.normalizeId(payload?.conversationId);+ if (!inboundConversationId) {+ this.client.send(msg.from, "error", {+ code: "INVALID_PARAMS",+ message: "Missing required conversationId.",+ messageId: msg.id,});return;}const incomingAgentId = payload?.agentId;- const conversationId = this.resolveConversationId(- incomingAgentId,- payload?.sessionId ?? payload?.conversationId,- );+ const conversationId = inboundConversationId;const agentId = this.resolveAgentId(incomingAgentId, conversationId);const content = payload?.content;if (!content) {console.warn("[Hub] Invalid payload, missing content");return;}- if (!conversationId) {- const inboundSessionId = payload?.sessionId ?? payload?.conversationId;- this.client.send(msg.from, "error", {- code: "INVALID_PARAMS",- message: "Unable to resolve sessionId (conversationId). Please provide a valid sessionId.",- messageId: msg.id,- ...(inboundSessionId ? { conversationId: inboundSessionId, sessionId: inboundSessionId } : {}),- });- return;- }const allowedScope = this.deviceStore.isAllowed(msg.from, conversationId);if (!allowedScope) {@@ -469,7 +455,6 @@ export class Hub {message: "Device is not authorized for this conversation.",messageId: msg.id,conversationId,- sessionId: conversationId,});return;}@@ -483,7 +468,6 @@ export class Hub {message: "Device is not authorized for this agent.",messageId: msg.id,conversationId,- sessionId: conversationId,});return;}@@ -552,16 +536,15 @@ export class Hub {const normalizedConversationId = this.normalizeId(conversationId);if (!normalizedAgentId || !normalizedConversationId) return;- const resolvedConversationId = this.resolveConversationId(normalizedAgentId, normalizedConversationId);- const ownerAgentId = this.conversationAgents.get(resolvedConversationId);+ const ownerAgentId = this.conversationAgents.get(normalizedConversationId);if (ownerAgentId && ownerAgentId !== normalizedAgentId) {console.warn(- `[Hub] registerToken rejected due to agent/conversation mismatch: agent=${normalizedAgentId}, conversation=${resolvedConversationId}, owner=${ownerAgentId}`,+ `[Hub] registerToken rejected due to agent/conversation mismatch: agent=${normalizedAgentId}, conversation=${normalizedConversationId}, owner=${ownerAgentId}`,);return;}const resolvedAgentId = ownerAgentId ?? normalizedAgentId;- this.deviceStore.registerToken(token, resolvedAgentId, resolvedConversationId, expiresAt);+ this.deviceStore.registerToken(token, resolvedAgentId, normalizedConversationId, expiresAt);}/** 重连到新的 Gateway 地址 */@@ -689,36 +672,6 @@ export class Hub {return typeof id === "string" && id.length > 0 ? id : undefined;}- private resolveConversationId(agentId: string | undefined, conversationId?: string): string {- const normalizedConversationId = this.normalizeId(conversationId);- if (normalizedConversationId) return normalizedConversationId;-- const normalizedAgentId = this.normalizeId(agentId);- if (!normalizedAgentId) return "";-- const mainConversationId = this.resolveAgentMainConversationId(normalizedAgentId);- if (mainConversationId) return mainConversationId;-- if (this.allowLegacyAgentConversationFallback) {- if (!this.warnedConversationFallbackAgents.has(normalizedAgentId)) {- this.warnedConversationFallbackAgents.add(normalizedAgentId);- console.warn(- `[Hub] Legacy fallback enabled: using agentId as conversationId for ${normalizedAgentId}. ` +- "Set explicit conversationId in clients to avoid this deprecated path.",- );- }- return normalizedAgentId;- }-- if (!this.warnedConversationFallbackAgents.has(normalizedAgentId)) {- this.warnedConversationFallbackAgents.add(normalizedAgentId);- console.warn(- `[Hub] Conversation resolution failed for agent ${normalizedAgentId}: no main conversation found and legacy fallback is disabled.`,- );- }- return "";- }-private beginStream(agentId: string, event: unknown): string {const explicitId = this.getMessageIdFromEvent(event);if (explicitId) {@@ -762,7 +715,6 @@ export class Hub {this.client.send(targetDeviceId, "message", {agentId,conversationId,- sessionId: conversationId,content: item.content,});} else {@@ -788,7 +740,6 @@ export class Hub {streamId: `system:${conversationId}`,agentId,conversationId,- sessionId: conversationId,event: item,});continue;@@ -832,7 +783,6 @@ export class Hub {streamId,agentId: pendingStart.agentId,conversationId: pendingStart.conversationId,- sessionId: pendingStart.conversationId,event: pendingStart.event,});this.pendingAssistantStarts.delete(streamId);@@ -842,7 +792,6 @@ export class Hub {streamId,agentId,conversationId,- sessionId: conversationId,event: item,});if (item.type === "message_end") {@@ -856,7 +805,6 @@ export class Hub {streamId,agentId,conversationId,- sessionId: conversationId,event: item,});}@@ -1024,7 +972,6 @@ export class Hub {caption,filename: basename(filePath),conversationId: sessionId,- sessionId,});console.log(`[Hub] Sent file via gateway: ${basename(filePath)} → ${deviceId}`);return true;@@ -1144,8 +1091,10 @@ export class Hub {/** Enqueue a system event for a specific agent or the default agent. */enqueueSystemEvent(text: string, opts?: { agentId?: string }): void {- const agentId = opts?.agentId ?? this.listAgents()[0];- const conversationId = this.resolveConversationId(agentId, undefined);+ const requestedAgentId = this.normalizeId(opts?.agentId);+ const conversationId = requestedAgentId+ ? this.resolveAgentMainConversationId(requestedAgentId)+ : this.listConversations()[0];if (!conversationId) return;enqueueSystemEvent(text, { sessionKey: conversationId });}diff --git a/packages/core/src/hub/rpc/handlers/create-agent.ts b/packages/core/src/hub/rpc/handlers/create-agent.tsdeleted file mode 100644index c0ab118687..0000000000--- a/packages/core/src/hub/rpc/handlers/create-agent.ts+++ /dev/null@@ -1,13 +0,0 @@-import type { RpcHandler } from "../dispatcher.js";--interface HubLike {- createAgent(id?: string): { sessionId: string };-}--export function createCreateAgentHandler(hub: HubLike): RpcHandler {- return (params: unknown) => {- const { id } = (params ?? {}) as { id?: string };- const agent = hub.createAgent(id);- return { id: agent.sessionId };- };-}diff --git a/packages/core/src/hub/rpc/handlers/delete-agent.ts b/packages/core/src/hub/rpc/handlers/delete-agent.tsdeleted file mode 100644index 72207f24c8..0000000000--- a/packages/core/src/hub/rpc/handlers/delete-agent.ts+++ /dev/null@@ -1,19 +0,0 @@-import { RpcError, type RpcHandler } from "../dispatcher.js";--interface HubLike {- closeAgent(id: string): boolean;-}--export function createDeleteAgentHandler(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.closeAgent(id);- return { ok };- };-}diff --git a/packages/core/src/hub/rpc/handlers/get-agent-messages.ts b/packages/core/src/hub/rpc/handlers/get-agent-messages.tsindex eb05381571..d5255bfc2d 100644--- a/packages/core/src/hub/rpc/handlers/get-agent-messages.ts+++ b/packages/core/src/hub/rpc/handlers/get-agent-messages.ts@@ -8,7 +8,7 @@ const DEFAULT_LIMIT = 200;interface GetAgentMessagesParams {agentId: string;- conversationId?: string;+ conversationId: string;offset?: number;limit?: number;}@@ -18,7 +18,7 @@ interface ResolvedConversation {storageAgentId?: string;}-type ConversationResolver = (agentId: string, conversationId?: string) => ResolvedConversation | null;+type ConversationResolver = (agentId: string, conversationId: string) => ResolvedConversation | null;export function createGetAgentMessagesHandler(resolveConversationId?: ConversationResolver): RpcHandler {return (params: unknown) => {@@ -30,10 +30,13 @@ export function createGetAgentMessagesHandler(resolveConversationId?: Conversatiif (!agentId) {throw new RpcError("INVALID_PARAMS", "Missing required param: agentId");}- const fallbackConversationId = (conversationId ?? "").trim() || agentId;+ const normalizedConversationId = (conversationId ?? "").trim();+ if (!normalizedConversationId) {+ throw new RpcError("INVALID_PARAMS", "Missing required param: conversationId");+ }const resolved = resolveConversationId? resolveConversationId(agentId, conversationId)- : { conversationId: fallbackConversationId };+ : { conversationId: normalizedConversationId };const resolvedConversationId = resolved?.conversationId?.trim() ?? "";if (!resolvedConversationId) {diff --git a/packages/core/src/hub/rpc/handlers/get-hub-info.ts b/packages/core/src/hub/rpc/handlers/get-hub-info.tsindex 46da8784af..846e74ff5d 100644--- a/packages/core/src/hub/rpc/handlers/get-hub-info.ts+++ b/packages/core/src/hub/rpc/handlers/get-hub-info.ts@@ -4,7 +4,7 @@ interface HubLike {hubId: string;url: string;connectionState: string;- listAgents(): string[];+ listConversations(): string[];}export function createGetHubInfoHandler(hub: HubLike): RpcHandler {@@ -12,6 +12,6 @@ export function createGetHubInfoHandler(hub: HubLike): RpcHandler {hubId: hub.hubId,url: hub.url,connectionState: hub.connectionState,- agentCount: hub.listAgents().length,+ agentCount: hub.listConversations().length,});}diff --git a/packages/core/src/hub/rpc/handlers/list-agents.ts b/packages/core/src/hub/rpc/handlers/list-agents.tsdeleted file mode 100644index ccd5c3e28d..0000000000--- a/packages/core/src/hub/rpc/handlers/list-agents.ts+++ /dev/null@@ -1,16 +0,0 @@-import type { RpcHandler } from "../dispatcher.js";--interface HubLike {- listAgents(): string[];- getAgent(id: string): { closed: boolean } | undefined;-}--export function createListAgentsHandler(hub: HubLike): RpcHandler {- return () => {- const agents = hub.listAgents().map((id) => {- const agent = hub.getAgent(id);- return { id, closed: agent?.closed ?? true };- });- return { agents };- };-}diff --git a/packages/core/src/hub/rpc/handlers/verify.test.ts b/packages/core/src/hub/rpc/handlers/verify.test.tsindex e29fcd6f7b..9ed058376e 100644--- a/packages/core/src/hub/rpc/handlers/verify.test.ts+++ b/packages/core/src/hub/rpc/handlers/verify.test.ts@@ -37,8 +37,6 @@ describe("createVerifyHandler", () => {hubId: "hub-1",agentId: "agent-1",conversationId: "conv-1",- sessionId: "conv-1",- mainConversationId: "conv-1",isNewDevice: false,});expect(storeApi.consumeToken).not.toHaveBeenCalled();@@ -71,8 +69,6 @@ describe("createVerifyHandler", () => {hubId: "hub-2",agentId: "agent-2",conversationId: "conv-2",- sessionId: "conv-2",- mainConversationId: "conv-2",isNewDevice: true,});expect(onConfirmDevice).toHaveBeenCalledWith("dev-2", "agent-2", "conv-2", undefined);diff --git a/packages/core/src/hub/rpc/handlers/verify.ts b/packages/core/src/hub/rpc/handlers/verify.tsindex 40601b88ea..e6bb09e51a 100644--- a/packages/core/src/hub/rpc/handlers/verify.ts+++ b/packages/core/src/hub/rpc/handlers/verify.ts@@ -38,8 +38,6 @@ export function createVerifyHandler(ctx: VerifyContext): RpcHandler {hubId: ctx.hubId,agentId: allowed.agentId,conversationId,- sessionId: conversationId,- mainConversationId: conversationId,isNewDevice: false,};}@@ -67,8 +65,6 @@ export function createVerifyHandler(ctx: VerifyContext): RpcHandler {hubId: ctx.hubId,agentId: result.agentId,conversationId: mainConversationId,- sessionId: mainConversationId,- mainConversationId,isNewDevice: true,};};diff --git a/packages/core/src/hub/rpc/index.ts b/packages/core/src/hub/rpc/index.tsindex fecb5e202a..4be90f71fe 100644--- a/packages/core/src/hub/rpc/index.ts+++ b/packages/core/src/hub/rpc/index.ts@@ -1,9 +1,9 @@export { RpcDispatcher, RpcError, type RpcHandler } from "./dispatcher.js";export { createGetAgentMessagesHandler } from "./handlers/get-agent-messages.js";export { createGetHubInfoHandler } from "./handlers/get-hub-info.js";-export { createListAgentsHandler } from "./handlers/list-agents.js";-export { createCreateAgentHandler } from "./handlers/create-agent.js";-export { createDeleteAgentHandler } from "./handlers/delete-agent.js";+export { createListConversationsHandler } from "./handlers/list-conversations.js";+export { createCreateConversationHandler } from "./handlers/create-conversation.js";+export { createDeleteConversationHandler } from "./handlers/delete-conversation.js";export { createUpdateGatewayHandler } from "./handlers/update-gateway.js";export { createGetLastHeartbeatHandler } from "./handlers/get-last-heartbeat.js";export { createSetHeartbeatsHandler } from "./handlers/set-heartbeats.js";