🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 修复 Web 回复人工交互后服务端任务未恢复 (#22060)
Mocha11953d1ab17a
diff --git a/apps/server/src/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts b/apps/server/src/modules/AgentRuntime/__tests__/RuntimeExecutors.test.tsindex 180785dcf3c..89959d10692 100644--- a/apps/server/src/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts+++ b/apps/server/src/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts@@ -5215,6 +5215,7 @@ describe('RuntimeExecutors', { timeout: 60_000 }, () => {parentId: 'assistant-msg-123',pluginError: 'blocked_by_security_privacy',pluginIntervention: {+ runtimeType: 'server',rejectedReason: 'blocked_by_security_privacy',status: 'rejected',},@@ -5284,14 +5285,13 @@ describe('RuntimeExecutors', { timeout: 60_000 }, () => {// Should create two aborted tool messagesexpect(mockMessageModel.create).toHaveBeenCalledTimes(2);-// First tool messageexpect(mockMessageModel.create).toHaveBeenCalledWith(expect.objectContaining({agentId: 'agent-123',content: 'Tool execution was aborted by user.',parentId: 'assistant-msg-123',- pluginIntervention: { status: 'aborted' },+ pluginIntervention: { runtimeType: 'server', status: 'aborted' },role: 'tool',threadId: 'thread-123',tool_call_id: 'tool-call-1',@@ -5305,7 +5305,7 @@ describe('RuntimeExecutors', { timeout: 60_000 }, () => {agentId: 'agent-123',content: 'Tool execution was aborted by user.',parentId: 'assistant-msg-123',- pluginIntervention: { status: 'aborted' },+ pluginIntervention: { runtimeType: 'server', status: 'aborted' },role: 'tool',threadId: 'thread-123',tool_call_id: 'tool-call-2',diff --git a/apps/server/src/modules/AgentRuntime/adapters/ServerMessageTransport.test.ts b/apps/server/src/modules/AgentRuntime/adapters/ServerMessageTransport.test.tsindex 8a82a17f48e..2368900021a 100644--- a/apps/server/src/modules/AgentRuntime/adapters/ServerMessageTransport.test.ts+++ b/apps/server/src/modules/AgentRuntime/adapters/ServerMessageTransport.test.ts@@ -25,6 +25,30 @@ const createUniqueViolation = (constraint: string) => {};describe('ServerMessageTransport', () => {+ it('persists the server origin alongside the shared intervention identity', async () => {+ const create = vi.fn().mockResolvedValue({ id: 'tool-1' });+ const transport = new ServerMessageTransport({ create } as unknown as MessageModel);+ await transport.createToolMessage({+ ...params,+ role: 'tool',+ pluginIntervention: {+ batchId: 'op:1:assistant',+ operationId: 'op',+ status: 'pending',+ },+ });+ expect(create).toHaveBeenCalledWith(+ expect.objectContaining({+ pluginIntervention: {+ batchId: 'op:1:assistant',+ operationId: 'op',+ runtimeType: 'server',+ status: 'pending',+ },+ }),+ );+ });+it('keeps the complete Work intent readable while excluding it from runtime message queries', async () => {const intent = {type: 'skill' as const,diff --git a/apps/server/src/modules/AgentRuntime/adapters/ServerMessageTransport.ts b/apps/server/src/modules/AgentRuntime/adapters/ServerMessageTransport.tsindex ef573c2a1b5..e5bf94e851c 100644--- a/apps/server/src/modules/AgentRuntime/adapters/ServerMessageTransport.ts+++ b/apps/server/src/modules/AgentRuntime/adapters/ServerMessageTransport.ts@@ -74,7 +74,12 @@ export class ServerMessageTransport implements MessageTransport {async createToolMessage(params: CreateMessageParams): Promise<RuntimeMessageRef> {try {- return await this.messageModel.create(params);+ return await this.messageModel.create({+ ...params,+ ...(params.pluginIntervention && {+ pluginIntervention: { ...params.pluginIntervention, runtimeType: 'server' },+ }),+ });} catch (error) {if (typeof params.parentId === 'string' && isMidOperationReferenceMissingError(error)) {throw createConversationParentMissingError(params.parentId, error);diff --git a/packages/types/src/message/common/tools.ts b/packages/types/src/message/common/tools.tsindex 241b9894eb7..c24d24d3d56 100644--- a/packages/types/src/message/common/tools.ts+++ b/packages/types/src/message/common/tools.ts@@ -16,6 +16,8 @@ export interface ToolIntervention {resolutionRequestId?: string;/** User decision published, awaiting the blocked producer's ACK. */resolving?: boolean;+ /** Runtime that owns this intervention; tool dispatch location is independent. */+ runtimeType?: 'client' | 'server';/*** The user skipped the interaction (e.g. AskUserQuestion) rather than* rejecting the tool call — still `status: 'rejected'` for the runtime, but@@ -30,6 +32,7 @@ export const ToolInterventionSchema = z.object({batchId: z.string().optional(),itemIndex: z.number().int().nonnegative().optional(),operationId: z.string().optional(),+ runtimeType: z.enum(['client', 'server']).optional(),rejectedReason: z.string().optional(),resolving: z.boolean().optional(),resolutionRequestId: z.string().optional(),diff --git a/src/store/chat/agents/__tests__/clientRuntimeExecutors/resolve-aborted-tools.test.ts b/src/store/chat/agents/__tests__/clientRuntimeExecutors/resolve-aborted-tools.test.tsindex 145846b2bfa..ca4ce91ee8c 100644--- a/src/store/chat/agents/__tests__/clientRuntimeExecutors/resolve-aborted-tools.test.ts+++ b/src/store/chat/agents/__tests__/clientRuntimeExecutors/resolve-aborted-tools.test.ts@@ -46,7 +46,7 @@ describe('resolve_aborted_tools executor', () => {role: 'tool',content: 'Tool execution was aborted by user.',plugin: toolCalls[0],- pluginIntervention: { status: 'aborted' },+ pluginIntervention: { runtimeType: 'client', status: 'aborted' },tool_call_id: 'tool_1',agentId: 'test-session',topicId: 'test-topic',@@ -109,7 +109,7 @@ describe('resolve_aborted_tools executor', () => {role: 'tool',content: 'Tool execution was aborted by user.',plugin: toolCall,- pluginIntervention: { status: 'aborted' },+ pluginIntervention: { runtimeType: 'client', status: 'aborted' },tool_call_id: toolCall.id,}),expect.objectContaining({@@ -199,7 +199,7 @@ describe('resolve_aborted_tools executor', () => {role: 'tool',content: 'Tool execution was aborted by user.',plugin: toolCall,- pluginIntervention: { status: 'aborted' },+ pluginIntervention: { runtimeType: 'client', status: 'aborted' },tool_call_id: 'tool_abc',parentId: 'msg_parent',agentId: 'sess_123',diff --git a/src/store/chat/agents/transports/ClientMessageTransport.test.ts b/src/store/chat/agents/transports/ClientMessageTransport.test.tsindex a91c1324313..09b3f8f94d2 100644--- a/src/store/chat/agents/transports/ClientMessageTransport.test.ts+++ b/src/store/chat/agents/transports/ClientMessageTransport.test.ts@@ -39,6 +39,36 @@ describe('ClientMessageTransport', () => {vi.restoreAllMocks();});+ it('persists the client origin without dropping shared intervention IDs', async () => {+ const store = createStore();+ const transport = new ClientMessageTransport(() => store, 'message-key', 'operation-1');+ await transport.createToolMessage({+ agentId: 'agent-1',+ content: '',+ role: 'tool',+ pluginIntervention: {+ batchId: 'operation-1:1:assistant',+ operationId: 'operation-1',+ status: 'pending',+ },+ });+ expect(messageService.batchMutate).toHaveBeenCalledWith(+ expect.arrayContaining([+ expect.objectContaining({+ type: 'createMessage',+ message: expect.objectContaining({+ pluginIntervention: {+ batchId: 'operation-1:1:assistant',+ operationId: 'operation-1',+ runtimeType: 'client',+ status: 'pending',+ },+ }),+ }),+ ]),+ );+ });+it('keeps raw Work intents available to replay but outside runtime conversation messages', async () => {const store = createStore();const intent = {diff --git a/src/store/chat/agents/transports/ClientMessageTransport.ts b/src/store/chat/agents/transports/ClientMessageTransport.tsindex 1fe72237908..686c543de6c 100644--- a/src/store/chat/agents/transports/ClientMessageTransport.ts+++ b/src/store/chat/agents/transports/ClientMessageTransport.ts@@ -59,14 +59,22 @@ export class ClientMessageTransport implements MessageTransport {}createToolMessage(params: CreateMessageParams): Promise<RuntimeMessageRef> {- return this.createMessage(params);+ return this.createToolMessageForOperation(params, this.operationId);}createToolMessageForOperation(params: CreateMessageParams,operationId: string,): Promise<RuntimeMessageRef> {- return this.createMessage(params, operationId);+ return this.createMessage(+ {+ ...params,+ ...(params.pluginIntervention && {+ pluginIntervention: { ...params.pluginIntervention, runtimeType: 'client' },+ }),+ },+ operationId,+ );}async deleteMessage(id: string): Promise<void> {diff --git a/src/store/chat/slices/agentRun/actions/entries/conversationControl.serverSource.test.ts b/src/store/chat/slices/agentRun/actions/entries/conversationControl.serverSource.test.tsnew file mode 100644index 00000000000..e146b54b153--- /dev/null+++ b/src/store/chat/slices/agentRun/actions/entries/conversationControl.serverSource.test.ts@@ -0,0 +1,295 @@+import type { ConversationContext } from '@lobechat/types';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { lambdaClient } from '@/libs/trpc/client';+import { agentByIdSelectors, agentSelectors } from '@/store/agent/selectors';+import { operationSelectors } from '@/store/chat/slices/operation/selectors';+import { useChatStore } from '@/store/chat/store';+import { messageMapKey } from '@/store/chat/utils/messageMapKey';++import { createMockMessage, createMockResolvedAgentConfig } from '../__tests__/fixtures';++vi.mock('@/libs/trpc/client', () => ({+ lambdaClient: {+ aiAgent: {+ resolveAgentInterventionBySource: { mutate: vi.fn() },+ },+ },+}));++vi.mock('@/utils/localStorage', () => ({+ AsyncLocalStorage: class {+ getFromLocalStorageSync() {+ return {};+ }+ async getFromLocalStorage() {+ return {};+ }+ async saveToLocalStorage() {}+ },+}));++const context: ConversationContext = {+ agentId: 'member-agent',+ groupId: 'group',+ scope: 'thread',+ threadId: 'member-thread',+ topicId: 'topic',+};+const toolMessageId = 'pending-tool';+const sourceMutation = () =>+ vi.mocked(lambdaClient.aiAgent.resolveAgentInterventionBySource.mutate);++const seed = (serverSource = true) => {+ const message = createMockMessage({+ id: toolMessageId,+ plugin: { apiName: 'askUser', arguments: '{}', identifier: 'lobe-agent', type: 'builtin' },+ pluginIntervention: {+ batchId: serverSource ? 'server-batch' : 'client-op:1:assistant',+ operationId: serverSource ? 'original-server-op' : 'client-op',+ runtimeType: serverSource ? 'server' : 'client',+ status: 'pending',+ },+ role: 'tool',+ tool_call_id: 'call-question',+ });+ const key = messageMapKey(context);+ useChatStore.setState({+ activeAgentId: 'different-active-agent',+ dbMessagesMap: { [key]: [message] },+ gatewayConnections: {},+ messagesMap: { [key]: [message] },+ operations: {},+ questionSubmissions: {},+ });+ const store = useChatStore.getState();+ vi.spyOn(agentSelectors, 'getAgentConfigById').mockReturnValue(() =>+ createMockResolvedAgentConfig(),+ );+ vi.spyOn(agentByIdSelectors, 'getAgentById').mockReturnValue(() => undefined);+ vi.spyOn(store, 'isGatewayModeEnabled').mockReturnValue(false);+ vi.spyOn(store, 'executeGatewayAgent').mockResolvedValue({} as never);+ vi.spyOn(store, 'executeClientAgent').mockResolvedValue(undefined);+ vi.spyOn(store, 'optimisticCreateMessage').mockResolvedValue({+ id: 'synthetic-user',+ messages: [],+ });+ vi.spyOn(store, 'optimisticUpdateMessagePlugin').mockResolvedValue(undefined);+ vi.spyOn(store, 'optimisticUpdateMessageContent').mockResolvedValue(undefined);+ vi.spyOn(store, 'internal_createAgentState').mockReturnValue({+ agentConfig: createMockResolvedAgentConfig(),+ context: { phase: 'init' },+ state: {},+ } as never);+ vi.spyOn(store, 'updateTopicStatus').mockResolvedValue(undefined);+ vi.spyOn(store, 'refreshMessages').mockResolvedValue(undefined);+ return store;+};++beforeEach(() => {+ sourceMutation()+ .mockReset()+ .mockResolvedValue({+ contractVersion: 2,+ state: 'claimed',+ status: 'resolved',+ success: true,+ } as never);+});+afterEach(() => vi.restoreAllMocks());++const actions = {+ approve: () => useChatStore.getState().approveToolCalling(toolMessageId, '', context),+ approveAll: () => useChatStore.getState().approveAllToolCalls([toolMessageId], context),+ answer: () =>+ useChatStore.getState().submitToolInteraction(toolMessageId, { Color: 'Blue' }, context),+ reject: () => useChatStore.getState().rejectToolCalling(toolMessageId, 'Denied', context),+ rejectContinue: () =>+ useChatStore.getState().rejectAndContinueToolCalling(toolMessageId, 'Denied', context),+ skip: () => useChatStore.getState().skipToolInteraction(toolMessageId, undefined, context),+ cancel: () => useChatStore.getState().cancelToolInteraction(toolMessageId, context),+};++describe('server-origin intervention with client-configured agent after reload', () => {+ it.each(Object.entries(actions))(+ 'routes %s through the original source without local execution',+ async (_name, action) => {+ const store = seed();+ await action();+ expect(sourceMutation()).toHaveBeenCalledWith(+ expect.objectContaining({+ batchId: 'server-batch',+ operationId: 'original-server-op',+ targets: [{ toolCallId: 'call-question', toolMessageId }],+ }),+ );+ expect(store.executeClientAgent).not.toHaveBeenCalled();+ expect(store.optimisticCreateMessage).not.toHaveBeenCalled();+ expect(store.optimisticUpdateMessagePlugin).not.toHaveBeenCalled();+ expect(store.optimisticUpdateMessageContent).not.toHaveBeenCalled();+ },+ );++ it.each(Object.keys(actions) as (keyof typeof actions)[])(+ 'falls back to the configured client path for %s when the source is unavailable',+ async (action) => {+ const store = seed();+ sourceMutation().mockResolvedValue({+ contractVersion: 2,+ status: 'unavailable',+ success: false,+ } as never);+ await actions[action]();+ expect(sourceMutation()).toHaveBeenCalled();+ if (action === 'reject' || action === 'cancel') {+ expect(store.executeClientAgent).not.toHaveBeenCalled();+ } else {+ expect(store.executeClientAgent).toHaveBeenCalledOnce();+ }+ expect(store.executeGatewayAgent).not.toHaveBeenCalled();+ expect(store.optimisticUpdateMessagePlugin).toHaveBeenCalled();+ expect(operationSelectors.isMessageProcessing(toolMessageId)(useChatStore.getState())).toBe(+ false,+ );+ },+ );++ it('clears processing when the client rejection continuation fails', async () => {+ const store = seed();+ sourceMutation().mockResolvedValue({ success: false } as never);+ vi.mocked(store.executeClientAgent).mockRejectedValueOnce(new Error('client failed'));+ await actions.rejectContinue();+ expect(operationSelectors.isMessageProcessing(toolMessageId)(useChatStore.getState())).toBe(+ false,+ );+ expect(Object.values(useChatStore.getState().operations)).toContainEqual(+ expect.objectContaining({ status: 'failed' }),+ );+ });++ it.each(['approve', 'answer'] as const)(+ 'keeps the configured Gateway legacy path for %s when the source is unavailable',+ async (action) => {+ const store = seed();+ vi.mocked(store.isGatewayModeEnabled).mockReturnValue(true);+ sourceMutation().mockResolvedValue({+ contractVersion: 2,+ status: 'unavailable',+ success: false,+ } as never);+ await actions[action]();+ expect(store.executeGatewayAgent).toHaveBeenCalledOnce();+ expect(store.executeClientAgent).not.toHaveBeenCalled();+ expect(store.optimisticUpdateMessagePlugin).not.toHaveBeenCalled();+ },+ );++ it('applies staged approval edits before the client fallback executes', async () => {+ const store = seed();+ sourceMutation().mockResolvedValue({ success: false } as never);+ const prepare = vi.fn(async () => {+ const key = messageMapKey(context);+ const messages = useChatStore.getState().dbMessagesMap[key].map((message) => ({+ ...message,+ plugin: { ...message.plugin!, arguments: '{"path":"edited"}' },+ }));+ useChatStore.setState({ dbMessagesMap: { [key]: messages } });+ });+ await store.approveToolCalling(toolMessageId, '', context, {+ editedArguments: { path: 'edited' },+ onLegacyEditFallback: prepare,+ });+ expect(prepare).toHaveBeenCalledOnce();+ expect(store.executeClientAgent).toHaveBeenCalledWith(+ expect.objectContaining({+ initialContext: expect.objectContaining({+ payload: expect.objectContaining({+ approvedToolCall: expect.objectContaining({ arguments: '{"path":"edited"}' }),+ }),+ }),+ }),+ );+ });++ it.each(['skip', 'cancel'] as const)(+ 'preserves the %s legacy side effect on client fallback',+ async (action) => {+ const store = seed();+ sourceMutation().mockResolvedValue({ success: false } as never);+ const prepare = vi.fn(async () => {});+ if (action === 'skip')+ await store.skipToolInteraction(toolMessageId, undefined, context, {+ onLegacyFallback: prepare,+ });+ else await store.cancelToolInteraction(toolMessageId, context, { onLegacyFallback: prepare });+ expect(prepare).toHaveBeenCalledOnce();+ expect(store.executeGatewayAgent).not.toHaveBeenCalled();+ },+ );++ it('subscribes to the server-created continuation in the original group thread', async () => {+ const store = seed();+ const execution = { operationId: 'server-continuation' };+ sourceMutation().mockResolvedValueOnce({+ contractVersion: 2,+ execution,+ state: 'claimed',+ status: 'resolved',+ success: true,+ } as never);+ await actions.answer();+ expect(store.executeGatewayAgent).toHaveBeenCalledWith(+ expect.objectContaining({+ context,+ precreatedResult: execution,+ }),+ );+ expect(store.executeClientAgent).not.toHaveBeenCalled();+ });++ it('keeps the pending card and surfaces a failed server answer for retry', async () => {+ const store = seed();+ const error = new Error('source unavailable');+ sourceMutation().mockRejectedValueOnce(error);+ await expect(actions.answer()).rejects.toThrow(error);+ expect(store.executeClientAgent).not.toHaveBeenCalled();+ expect(store.optimisticUpdateMessagePlugin).not.toHaveBeenCalled();+ expect(store.optimisticUpdateMessageContent).not.toHaveBeenCalled();+ expect(Object.values(useChatStore.getState().operations)).toEqual([+ expect.objectContaining({ status: 'failed' }),+ ]);+ await actions.answer();+ expect(sourceMutation()).toHaveBeenCalledTimes(2);+ expect(sourceMutation().mock.calls[1][0]).toEqual(sourceMutation().mock.calls[0][0]);+ });++ it('leaves a source already answered elsewhere to the server and refreshes', async () => {+ const store = seed();+ sourceMutation().mockResolvedValueOnce({+ contractVersion: 2,+ state: 'already_resolved',+ status: 'resolved',+ success: true,+ } as never);+ await actions.answer();+ expect(store.refreshMessages).toHaveBeenCalledWith(context);+ expect(store.executeClientAgent).not.toHaveBeenCalled();+ expect(store.executeGatewayAgent).not.toHaveBeenCalled();+ });++ it.each([false, true])(+ 'keeps a client question on its originating runtime even when gateway mode is %s',+ async (gatewayEnabled) => {+ const store = seed(false);+ vi.mocked(store.isGatewayModeEnabled).mockReturnValue(gatewayEnabled);+ await useChatStore+ .getState()+ .submitToolInteraction(toolMessageId, { Color: 'Blue' }, context, {+ createUserMessage: false,+ });+ expect(sourceMutation()).not.toHaveBeenCalled();+ expect(store.executeClientAgent).toHaveBeenCalledOnce();+ expect(store.optimisticUpdateMessagePlugin).toHaveBeenCalled();+ },+ );+});diff --git a/src/store/chat/slices/agentRun/actions/entries/conversationControl.ts b/src/store/chat/slices/agentRun/actions/entries/conversationControl.tsindex 84ecded66e1..daf66097eff 100644--- a/src/store/chat/slices/agentRun/actions/entries/conversationControl.ts+++ b/src/store/chat/slices/agentRun/actions/entries/conversationControl.ts@@ -1,26 +1,16 @@// Disable the auto sort key eslint rule to make the code more logic and readableimport { type AgentRuntimeContext } from '@lobechat/agent-runtime';-import { isHeterogeneousAgentModelId, MESSAGE_CANCEL_FLAT } from '@lobechat/const';+import { MESSAGE_CANCEL_FLAT } from '@lobechat/const';import {type ChatTopicStatus,type ConversationContext,type MessageMetadata,- resolveAgentAgencyConfig,type UIChatMessage,} from '@lobechat/types';import { toast } from '@lobehub/ui';import { t } from 'i18next';import { type ChatInputEditor } from '@/features/ChatInput';-import {- ensureAgentManagementAccess,- getRuntimeCanManageAgent,-} from '@/helpers/agentManagementAccess';-import {- applyTopicDeviceBinding,- getTopicBoundDeviceId,- resolveWorkspaceScoped,-} from '@/helpers/executionTarget';import { lambdaClient } from '@/libs/trpc/client';import {type AgentInterventionSourceAction,@@ -28,13 +18,7 @@ import {type ResolveAgentInterventionBySourceResult,} from '@/services/aiAgent';import { shareChatService } from '@/services/shareChat';-import { getAgentStoreState } from '@/store/agent';-import { agentByIdSelectors, agentSelectors } from '@/store/agent/selectors';-import { topicSelectors } from '@/store/chat/selectors';-import {- type AgentRuntimeType,- selectRuntimeType,-} from '@/store/chat/slices/agentRun/actions/dispatch/agentDispatcher';+import { type AgentRuntimeType } from '@/store/chat/slices/agentRun/actions/dispatch/agentDispatcher';import { type OptimisticUpdateContext } from '@/store/chat/slices/message/actions/optimisticUpdate';import { dbMessageSelectors } from '@/store/chat/slices/message/selectors';import { operationSelectors } from '@/store/chat/slices/operation/selectors';@@ -42,14 +26,16 @@ import type { Operation } from '@/store/chat/slices/operation/types';import { AI_RUNTIME_OPERATION_TYPES } from '@/store/chat/slices/operation/types';import { type ChatStore } from '@/store/chat/store';import { messageMapKey } from '@/store/chat/utils/messageMapKey';-import { getElectronStoreState } from '@/store/electron';import { type StoreSetter } from '@/store/types';import { useUserStore } from '@/store/user';-import { userProfileSelectors } from '@/store/user/selectors';import { buildRunLifecycle } from '../lifecycle/buildRunLifecycle';import { type RunScope } from '../lifecycle/types';import { readClientRunMessages } from './ensureFullHistoryForClientRun';+import {+ shouldUseConfiguredGatewayResume as resolveConfiguredGatewayResume,+ shouldUseGatewayResume as resolveGatewayResume,+} from './gatewayResume';import { createInterventionProjection } from './interventionProjection';/**@@ -226,89 +212,6 @@ export class ConversationControlActionImpl {return result;};- /**- * Decide whether approve/reject/reject_continue should go through the- * Gateway resume path (new op carrying `resumeApproval`) instead of the- * local `executeClientAgent` path. Mirrors the "interrupt + new op"- * pattern from .- *- * Routes via `selectRuntimeType` so approve/reject align with how the- * conversation was dispatched at sendMessage time. Hetero resume is not yet- * implemented and falls through to client local resume — see .- *- * We deliberately do **not** look for a living `execServerAgentRuntime`- * op here. The server's `waiting_for_human` → `agent_runtime_end` signal- * marks the paused op `completed` client-side, and `startOperation` runs- * `cleanupCompletedOperations(30_000)` on every new op, which means the- * paused op is typically gone by the time the user clicks approve — so- * scanning for it would flip us back into client-mode against a live- * Gateway backend.- */- #shouldUseGatewayResume = async (context: ConversationContext): Promise<boolean> => {- // A share run only ever executes on the server through the share gateway;- // the visitor has no local runtime nor access to the owner's agent config.- if (context.agentShareId) return true;-- const agentId = context.agentId;- if (!agentId) return false;-- const agentState = getAgentStoreState();- const agentConfig = agentSelectors.getAgentConfigById(agentId)(agentState);- const agent = agentByIdSelectors.getAgentById(agentId)(agentState);- const currentUserId = userProfileSelectors.userId(useUserStore.getState());- await ensureAgentManagementAccess({- agentId,- agentUserId: agent?.userId,- currentUserId,- visibility: agent?.visibility,- workspaceId: agent?.workspaceId,- });- const canManage = getRuntimeCanManageAgent({- agentId,- agentUserId: agent?.userId,- currentUserId,- });- const usesWorkspaceMemberSelection =- !!agent?.workspaceId && agent.visibility !== 'private' && !canManage;- const deviceOverride = agent?.workspaceId- ? useUserStore.getState().workspaceUserPreference.agentDeviceOverrides?.[agentId]- : undefined;- // Same topic machine binding as sendMessage, so the resume takes the path- // the paused run was dispatched on.- const { agencyConfig, workspaceScoped } = applyTopicDeviceBinding(- {- agencyConfig: resolveAgentAgencyConfig(agentConfig?.agencyConfig, deviceOverride, {- canManage,- visibility: agent?.visibility,- workspaceId: agent?.workspaceId,- }),- workspaceScoped: resolveWorkspaceScoped(usesWorkspaceMemberSelection, deviceOverride),- },- getTopicBoundDeviceId(- context.topicId ? topicSelectors.getTopicById(context.topicId)(this.#get()) : undefined,- agentId,- ),- getElectronStoreState().gatewayDeviceInfo?.deviceId,- );- const isGatewayMode = this.#get().isGatewayModeEnabled(agentId);- const heterogeneousProvider =- agencyConfig?.heterogeneousProvider ??- (!isGatewayMode && isHeterogeneousAgentModelId(agentConfig?.model)- ? { type: agentConfig.model }- : undefined);-- return (- selectRuntimeType({- boundDeviceId: agencyConfig?.boundDeviceId,- executionTarget: agencyConfig?.executionTarget,- heterogeneousProvider,- isGatewayMode,- isWorkspaceAgent: !!agent?.workspaceId,- workspaceScoped,- }) === 'gateway'- );- };-/*** Return running (non-aborting) `execServerAgentRuntime` ops in the given* context. Used only to snapshot paused ops before starting a resume op@@ -659,15 +562,7 @@ export class ConversationControlActionImpl {return !!op && op.status !== 'running';};- /**- * Claim a Gateway approval and start the run that resumes it, for both single- * and batch approve. The card already shows the decision and no longer covers- * the composer, so Stop is reachable while the claim is in flight:- * - Before the claim leaves, nothing reached the server: undo locally.- * - After it, `executeGatewayAgent` interrupts the started run (via- * `parentOperationId`). The local decision is undone and the server's state- * reloaded, since the refresh merge would otherwise keep the local approval.- */+ /** Resolve the source first; false falls through to the original client path. */#resumeGatewayApproval = async (approval: {action: AgentInterventionSourceAction;anchorMessageId: string;@@ -676,11 +571,12 @@ export class ConversationControlActionImpl {/** Runs once the resumed run started; `handled` means the source store took the decision. */onClaimed?: (handled: boolean) => Promise<void>;operationId: string;+ prepareLegacy?: () => Promise<void>;/** Resume through the message-row claim when no source store handles the decision. */resumeLegacy: (params: Parameters<ChatStore['executeGatewayAgent']>[0]) => Promise<unknown>;rollbackLocalDecision: () => void;toolMessageIds: string[];- }): Promise<void> => {+ }): Promise<boolean> => {const { anchorMessageId, context, logTag, operationId, toolMessageIds } = approval;const isStopped = () => this.#get().operations[operationId]?.status === 'cancelled';const settleStopped = () => {@@ -689,7 +585,10 @@ export class ConversationControlActionImpl {.refreshMessages(context).catch((error) => console.error(`${logTag} refresh after Stop failed:`, error));};- if (isStopped()) return approval.rollbackLocalDecision();+ if (isStopped()) {+ approval.rollbackLocalDecision();+ return true;+ }// Retired only once the resume starts, so a retry still sees Gateway mode.const pausedOpIds = this.#getRunningServerOps(context).map((op) => op.id);@@ -706,7 +605,16 @@ export class ConversationControlActionImpl {context,toolMessageIds,});- if (this.#discardAlreadyResolvedSource(sourceResolution, operationId)) return;+ if (this.#discardAlreadyResolvedSource(sourceResolution, operationId)) return true;+ if (!sourceResolution.handled) {+ const gateway = await resolveConfiguredGatewayResume(context, this.#get);+ if (!gateway && isStopped()) {+ approval.rollbackLocalDecision();+ return true;+ }+ await approval.prepareLegacy?.();+ if (!gateway) return false;+ }this.#emitRunResumed(context, {operationId,@@ -726,10 +634,17 @@ export class ConversationControlActionImpl {// paused run ended) while the server's still reads `waitingForHuman`.this.#writeTopicStatus(context, 'active', isStopped());this.#completeOpsById(pausedOpIds);- if (isStopped()) return await settleStopped();+ if (isStopped()) {+ await settleStopped();+ return true;+ }this.#get().completeOperation(operationId);+ return true;} catch (error) {- if (isStopped()) return await settleStopped();+ if (isStopped()) {+ await settleStopped();+ return true;+ }const err = error as Error;console.error(`${logTag} Gateway resume failed:`, err);approval.rollbackLocalDecision();@@ -765,6 +680,7 @@ export class ConversationControlActionImpl {// 1. Get tool message and verify it existsconst toolMessage = dbMessageSelectors.getDbMessageById(toolMessageId)(this.#get());if (!toolMessage) return;+ let approvedToolCall = toolMessage.plugin;// Create an operation to carry the context for optimistic updates// This ensures optimistic updates use the correct agentId/topicId@@ -787,37 +703,15 @@ export class ConversationControlActionImpl {{ status: 'approved' },optimisticContext,);- const shouldUseGatewayResume = await this.#shouldUseGatewayResume(effectiveContext);-- if (!shouldUseGatewayResume) {- this.#writeTopicStatus(effectiveContext, 'active');- // Park → resume: a new op continues the run paused on this tool's approval.- this.#emitRunResumed(effectiveContext, {- operationId,- parentMessageId: toolMessageId,- runtimeType: 'client',- });- }+ const shouldUseGatewayResume = await resolveGatewayResume(+ effectiveContext,+ [toolMessageId],+ this.#get,+ );// Local runtime persists the decision itself. A Gateway claim needs the row still// `pending` on the server, so it must not persist an optimistic approval first.- if (!shouldUseGatewayResume) {- await this.#interventions.settleAfter([toolMessageId], () =>- this.#get().optimisticUpdateMessagePlugin(- toolMessageId,- { intervention: { status: 'approved' } },- optimisticContext,- ),- );- }- // NOTE: the client path does NOT bail on Stop: the approval is persisted above, so- // returning would leave the tool approved but never run. Gateway honors Stop below.- // 2.5. Server-mode: start a **new** Gateway op carrying the approval- // decision via `resumeApproval`. The server reads the target tool- // message, persists `intervention=approved`, dispatches the approved- // tool, and streams results back on the new op. No in-place resume of- // the paused op — simpler state + avoids stepIndex races.if (shouldUseGatewayResume) {const toolCallId = toolMessage.tool_call_id;if (!toolCallId) {@@ -833,39 +727,59 @@ export class ConversationControlActionImpl {// visitor's "don't ask again" approves once and lands in their own allow// list instead, which their share runs send as `userInterventionConfig`.const remembersOnServer = !!options?.rememberToolKey && !effectiveContext.agentShareId;- await this.#resumeGatewayApproval({- action: {- ...(options?.editedArguments && {- edits: { [toolMessageId]: options.editedArguments },- }),- scope: remembersOnServer ? 'remember' : 'once',- type: 'approve_tool',- },- anchorMessageId: toolMessageId,- context: effectiveContext,- logTag: '[approveToolCalling][server]',- onClaimed: async (handled) => {- if (options?.rememberToolKey && !(handled && remembersOnServer)) {- await useUserStore.getState().addToolToAllowList(options.rememberToolKey);- }- },- operationId,- resumeLegacy: async (params) => {- // OSS compatibility: no generic durable store. Persist a staged edit- // only now, after the generic capability probe, then use the existing- // message-row claim path.- if (options?.editedArguments) await options.onLegacyEditFallback?.();- await this.#get().executeGatewayAgent({- ...params,- resumeApproval: { decision: 'approved', parentMessageId: toolMessageId, toolCallId },- });- },- rollbackLocalDecision,- toolMessageIds: [toolMessageId],- });- return;+ if (+ await this.#resumeGatewayApproval({+ action: {+ ...(options?.editedArguments && {+ edits: { [toolMessageId]: options.editedArguments },+ }),+ scope: remembersOnServer ? 'remember' : 'once',+ type: 'approve_tool',+ },+ anchorMessageId: toolMessageId,+ context: effectiveContext,+ logTag: '[approveToolCalling][server]',+ onClaimed: async (handled) => {+ if (options?.rememberToolKey && !(handled && remembersOnServer)) {+ await useUserStore.getState().addToolToAllowList(options.rememberToolKey);+ }+ },+ operationId,+ prepareLegacy: async () => {+ if (!options?.editedArguments) return;+ await options.onLegacyEditFallback?.();+ approvedToolCall =+ dbMessageSelectors.getDbMessageById(toolMessageId)(this.#get())?.plugin ??+ approvedToolCall;+ },+ resumeLegacy: async (params) => {+ await this.#get().executeGatewayAgent({+ ...params,+ resumeApproval: { decision: 'approved', parentMessageId: toolMessageId, toolCallId },+ });+ },+ rollbackLocalDecision,+ toolMessageIds: [toolMessageId],+ })+ )+ return;}+ this.#writeTopicStatus(effectiveContext, 'active');+ // Park → resume: a new op continues the run paused on this tool's approval.+ this.#emitRunResumed(effectiveContext, {+ operationId,+ parentMessageId: toolMessageId,+ runtimeType: 'client',+ });+ await this.#interventions.settleAfter([toolMessageId], () =>+ this.#get().optimisticUpdateMessagePlugin(+ toolMessageId,+ { intervention: { status: 'approved' } },+ optimisticContext,+ ),+ );+// 3. Get current messages for state construction using contextconst chatKey = messageMapKey({ agentId, topicId, threadId, scope });const currentMessages = await readClientRunMessages(this.#get, effectiveContext, chatKey);@@ -889,7 +803,7 @@ export class ConversationControlActionImpl {...initialContext,phase: 'human_approved_tool',payload: {- approvedToolCall: toolMessage.plugin,+ approvedToolCall,parentMessageId: toolMessageId,skipCreateToolMessage: true,},@@ -923,36 +837,7 @@ export class ConversationControlActionImpl {}};- /**- * Approve every pending tool of a parallel batch in ONE action.- *- * Server mode resolves the whole batch through a single Gateway op carrying- * `resumeApprovals`, so the run executes all approved tools as one- * `call_tools_batch` and continues the LLM exactly once with the complete- * result set. Looping `approveToolCalling` instead would start one op per- * tool, and each op continues the run while its siblings are still empty- * pending rows — which is what forks the parent chain and shows the model- * blank tool results.- *- * Client mode has no batch resume: the local runtime re-parks on the- * remaining pending tools after each approval, so sequential approvals are- * already correct there. Approvals are issued in order and awaited so the- * runtime never sees two resumes racing on the same assistant turn.- */- /**- * Stop a run parked on tool approval — "from this step on, don't continue".- *- * Distinct from rejecting a tool: a rejection resumes the model so it can- * respond to the refusal, whereas stopping ends the turn outright. Nothing- * in the batch executes.- *- * This cannot go through the ordinary cancel actions: they all filter on- * `status === 'running'`, and a parked run's client operation is `completed`- * (the server's park is stream-terminal) and pruned ~30s later, so the client- * no longer holds its operation object. The pending tool rows retain the- * authoritative operationId + sealed batchId, which are the only identities- * accepted by the server stop path.- */+ /** Stop the authoritative sealed batch; a parked run no longer has an active client operation. */stopPendingApproval = async (toolMessageIds: string[],context?: ConversationContext,@@ -1034,7 +919,7 @@ export class ConversationControlActionImpl {threadId: this.#get().activeThreadId,};- if (!(await this.#shouldUseGatewayResume(effectiveContext))) {+ if (!(await resolveGatewayResume(effectiveContext, toolMessageIds, this.#get))) {for (const toolMessageId of toolMessageIds) {await this.approveToolCalling(toolMessageId, '', effectiveContext);}@@ -1076,7 +961,7 @@ export class ConversationControlActionImpl {{ operationId },);- await this.#resumeGatewayApproval({+ const resumed = await this.#resumeGatewayApproval({action: { scope: 'once', type: 'approve_tool' },anchorMessageId,context: effectiveContext,@@ -1087,6 +972,12 @@ export class ConversationControlActionImpl {rollbackLocalDecision,toolMessageIds: approvedIds,});+ if (!resumed) {+ this.#get().completeOperation(operationId);+ for (const toolMessageId of toolMessageIds) {+ await this.approveToolCalling(toolMessageId, '', effectiveContext);+ }+ }};submitToolInteraction = async (@@ -1137,55 +1028,15 @@ export class ConversationControlActionImpl {const optimisticContext: OptimisticUpdateContext = { operationId };let resolvedOptions = options;let resolvedResponse = response;- const shouldCreateUserMessage = resolvedOptions?.createUserMessage !== false;- const shouldUseGatewayResume = await this.#shouldUseGatewayResume(effectiveContext);-- if (!shouldUseGatewayResume) {- this.#writeTopicStatus(effectiveContext, 'active');- // Park → resume: a new op continues the run paused on this tool interaction.- this.#emitRunResumed(effectiveContext, {- operationId,- parentMessageId: toolMessageId,- runtimeType: 'client',- });- }+ const shouldUseGatewayResume = await resolveGatewayResume(+ effectiveContext,+ [toolMessageId],+ this.#get,+ );let toolContent =resolvedOptions?.toolResultContent ?? `User submitted: ${JSON.stringify(resolvedResponse)}`;- if (!shouldUseGatewayResume) {- // Client runtime owns its local pending row. Gateway must leave the DB- // row pending until resumeToolResult acquires the server-side CAS.- await this.#get().optimisticUpdateMessagePlugin(- toolMessageId,- { intervention: { status: 'approved' } },- optimisticContext,- );- await this.#get().optimisticUpdateMessageContent(- toolMessageId,- toolContent,- undefined,- optimisticContext,- );-- if (resolvedOptions?.pluginState) {- await this.#get().optimisticUpdatePluginState(- toolMessageId,- resolvedOptions.pluginState,- optimisticContext,- );- }- }-- // NOTE: intentionally do NOT bail on Stop here. In client mode the result- // is already persisted; in Gateway mode the authoritative claim+resume- // below is the indivisible action. Honor the next Stop after it settles.- // 1.5. Server-mode: start a **new** Gateway op carrying the human answer as- // the tool result via `resumeToolResult`. The server writes the answer as- // the pending tool message's content, marks intervention approved, and- // resumes from `phase: 'tool_result'` (NO re-execution). The synthetic user- // message (2b) is not needed — the server continues from the answered tool- // call. Mirrors approveToolCalling's gateway branch.if (shouldUseGatewayResume) {const toolCallId = toolMessage.tool_call_id;if (!toolCallId) {@@ -1196,9 +1047,7 @@ export class ConversationControlActionImpl {return;}const requestMetadata = this.#getRequestMetadataFromMessageChain(toolMessageId);- // Snapshot paused op IDs before the resume call; retire them only after- // executeGatewayAgent succeeds so a transient failure leaves the running- // marker intact and `#shouldUseGatewayResume` still flags Gateway mode.+ // Retire paused operations only after a successful server resume.const pausedOpIds = this.#getRunningServerOps(effectiveContext).map((op) => op.id);try {const answerResult = toAgentInterventionAnswerResult(resolvedResponse);@@ -1215,23 +1064,7 @@ export class ConversationControlActionImpl {if (this.#discardAlreadyResolvedSource(sourceResolution, operationId)) return;- this.#emitRunResumed(effectiveContext, {- operationId,- parentMessageId: toolMessageId,- runtimeType: 'gateway',- });-- if (sourceResolution.handled) {- if (sourceResolution.execution) {- await this.#get().executeGatewayAgent({- context: effectiveContext,- message: '',- metadata: requestMetadata,- parentMessageId: toolMessageId,- precreatedResult: sourceResolution.execution,- });- }- } else {+ if (!sourceResolution.handled) {const legacyFallback = await resolvedOptions?.prepareLegacyFallback?.();if (legacyFallback) {resolvedResponse = legacyFallback.response;@@ -1240,23 +1073,50 @@ export class ConversationControlActionImpl {resolvedOptions.toolResultContent ??`User submitted: ${JSON.stringify(resolvedResponse)}`;}- await this.#get().executeGatewayAgent({- context: effectiveContext,- message: '',- metadata: requestMetadata,+ }++ if (+ sourceResolution.handled ||+ (await resolveConfiguredGatewayResume(effectiveContext, this.#get))+ ) {+ this.#emitRunResumed(effectiveContext, {+ operationId,parentMessageId: toolMessageId,- resumeToolResult: {- content: toolContent,- outcome: 'submitted',- parentMessageId: toolMessageId,- toolCallId,- ...(resolvedOptions?.pluginState ? { pluginState: resolvedOptions.pluginState } : {}),- },+ runtimeType: 'gateway',});++ if (sourceResolution.handled) {+ if (sourceResolution.execution) {+ await this.#get().executeGatewayAgent({+ context: effectiveContext,+ message: '',+ metadata: requestMetadata,+ parentMessageId: toolMessageId,+ precreatedResult: sourceResolution.execution,+ });+ }+ } else {+ await this.#get().executeGatewayAgent({+ context: effectiveContext,+ message: '',+ metadata: requestMetadata,+ parentMessageId: toolMessageId,+ resumeToolResult: {+ content: toolContent,+ outcome: 'submitted',+ parentMessageId: toolMessageId,+ toolCallId,+ ...(resolvedOptions?.pluginState+ ? { pluginState: resolvedOptions.pluginState }+ : {}),+ },+ });+ }+ this.#writeTopicStatus(effectiveContext, 'active');+ this.#completeOpsById(pausedOpIds);+ completeOperation(operationId);+ return;}- this.#writeTopicStatus(effectiveContext, 'active');- this.#completeOpsById(pausedOpIds);- completeOperation(operationId);} catch (error) {const err = error as Error;console.error('[submitToolInteraction][server] Gateway resume failed:', err);@@ -1266,15 +1126,41 @@ export class ConversationControlActionImpl {});throw error;}- return;+ }++ const shouldCreateUserMessage = resolvedOptions?.createUserMessage !== false;+ this.#writeTopicStatus(effectiveContext, 'active');+ // Park → resume: a new op continues the run paused on this tool interaction.+ this.#emitRunResumed(effectiveContext, {+ operationId,+ parentMessageId: toolMessageId,+ runtimeType: 'client',+ });+ // Client runtime owns its local pending row. Gateway must leave the DB+ // row pending until resumeToolResult acquires the server-side CAS.+ await this.#get().optimisticUpdateMessagePlugin(+ toolMessageId,+ { intervention: { status: 'approved' } },+ optimisticContext,+ );+ await this.#get().optimisticUpdateMessageContent(+ toolMessageId,+ toolContent,+ undefined,+ optimisticContext,+ );++ if (resolvedOptions?.pluginState) {+ await this.#get().optimisticUpdatePluginState(+ toolMessageId,+ resolvedOptions.pluginState,+ optimisticContext,+ );}const chatKey = messageMapKey({ agentId, topicId, threadId, scope });- // 2a. Tool-result-only path: skip the synthetic user message and resume from the- // tool message. Used by interventions whose UI handles its own side effect (e.g.- // the agent marketplace picker forks agents directly) — the LLM should see the- // tool result, not a fake user turn.+ // A completed interaction result resumes without a synthetic user message or tool re-execution.if (!shouldCreateUserMessage) {const currentMessages = await readClientRunMessages(this.#get, effectiveContext, chatKey);const requestMetadata = this.#getRequestMetadataFromMessageChain(@@ -1291,12 +1177,7 @@ export class ConversationControlActionImpl {operationId,});- // Resume directly from `tool_result` phase rather than `human_approved_tool`.- // The intervention UI already wrote the final tool result content via- // `optimisticUpdateMessageContent`; routing through `human_approved_tool`- // would re-execute the builtin tool on the server and overwrite our- // content with the server-side placeholder (e.g. the marketplace picker- // would clobber the picked-templates result with "picker is now visible").+ // The UI already stored the result; do not execute the intervention tool again.const agentRuntimeContext: AgentRuntimeContext = {...initialContext,phase: 'tool_result',@@ -1425,17 +1306,11 @@ export class ConversationControlActionImpl {});const optimisticContext: OptimisticUpdateContext = { operationId };- const shouldUseGatewayResume = await this.#shouldUseGatewayResume(effectiveContext);-- if (!shouldUseGatewayResume) {- this.#writeTopicStatus(effectiveContext, 'active');- // Park → resume: a new op continues the run paused on this tool interaction.- this.#emitRunResumed(effectiveContext, {- operationId,- parentMessageId: toolMessageId,- runtimeType: 'client',- });- }+ const shouldUseGatewayResume = await resolveGatewayResume(+ effectiveContext,+ [toolMessageId],+ this.#get,+ );const toolContent = reason ? `User skipped: ${reason}` : 'User skipped this question.';@@ -1461,41 +1336,47 @@ export class ConversationControlActionImpl {toolMessageIds: [toolMessageId],});if (this.#discardAlreadyResolvedSource(sourceResolution, operationId)) return;-- this.#emitRunResumed(effectiveContext, {- operationId,- parentMessageId: toolMessageId,- runtimeType: 'gateway',- });- if (sourceResolution.handled) {- if (sourceResolution.execution) {+ if (!sourceResolution.handled) await options?.onLegacyFallback?.();++ if (+ sourceResolution.handled ||+ (await resolveConfiguredGatewayResume(effectiveContext, this.#get))+ ) {+ this.#emitRunResumed(effectiveContext, {+ operationId,+ parentMessageId: toolMessageId,+ runtimeType: 'gateway',+ });+ if (sourceResolution.handled) {+ if (sourceResolution.execution) {+ await this.#get().executeGatewayAgent({+ context: effectiveContext,+ message: '',+ metadata: requestMetadata,+ parentMessageId: toolMessageId,+ precreatedResult: sourceResolution.execution,+ });+ }+ } else {await this.#get().executeGatewayAgent({context: effectiveContext,message: '',metadata: requestMetadata,parentMessageId: toolMessageId,- precreatedResult: sourceResolution.execution,+ resumeToolResult: {+ content: toolContent,+ outcome: 'skipped',+ parentMessageId: toolMessageId,+ rejectionReason: reason,+ toolCallId,+ },});}- } else {- await options?.onLegacyFallback?.();- await this.#get().executeGatewayAgent({- context: effectiveContext,- message: '',- metadata: requestMetadata,- parentMessageId: toolMessageId,- resumeToolResult: {- content: toolContent,- outcome: 'skipped',- parentMessageId: toolMessageId,- rejectionReason: reason,- toolCallId,- },- });+ this.#writeTopicStatus(effectiveContext, 'active');+ this.#completeOpsById(pausedOpIds);+ completeOperation(operationId);+ return;}- this.#writeTopicStatus(effectiveContext, 'active');- this.#completeOpsById(pausedOpIds);- completeOperation(operationId);} catch (error) {const err = error as Error;console.error('[skipToolInteraction][server] Gateway resume failed:', err);@@ -1505,9 +1386,16 @@ export class ConversationControlActionImpl {});throw error;}- return;}+ this.#writeTopicStatus(effectiveContext, 'active');+ // Park → resume: a new op continues the run paused on this tool interaction.+ this.#emitRunResumed(effectiveContext, {+ operationId,+ parentMessageId: toolMessageId,+ runtimeType: 'client',+ });+// 1. Mark intervention as rejected (skipped) with reasonawait this.#get().optimisticUpdateMessagePlugin(toolMessageId,@@ -1611,7 +1499,11 @@ export class ConversationControlActionImpl {const toolMessage = dbMessageSelectors.getDbMessageById(toolMessageId)(this.#get());if (!toolMessage) return;- const shouldUseGatewayStop = await this.#shouldUseGatewayResume(effectiveContext);+ const shouldUseGatewayStop = await resolveGatewayResume(+ effectiveContext,+ [toolMessageId],+ this.#get,+ );const { operationId } = startOperation({type: 'cancelToolInteraction',@@ -1659,15 +1551,21 @@ export class ConversationControlActionImpl {toolMessageIds: [toolMessageId],});if (this.#discardAlreadyResolvedSource(sourceResolution, operationId)) return;-- if (!sourceResolution.handled) {- await options?.onLegacyFallback?.();- await this.stopPendingApproval(- batchToolMessageIds.length > 0 ? batchToolMessageIds : [toolMessageId],- effectiveContext,- );+ if (!sourceResolution.handled) await options?.onLegacyFallback?.();++ if (+ sourceResolution.handled ||+ (await resolveConfiguredGatewayResume(effectiveContext, this.#get))+ ) {+ if (!sourceResolution.handled) {+ await this.stopPendingApproval(+ batchToolMessageIds.length > 0 ? batchToolMessageIds : [toolMessageId],+ effectiveContext,+ );+ }+ completeOperation(operationId);+ return;}- completeOperation(operationId);} catch (error) {const err = error as Error;this.#get().failOperation(operationId, {@@ -1676,7 +1574,6 @@ export class ConversationControlActionImpl {});throw error;}- return;}this.#writeTopicStatus(effectiveContext, 'active');@@ -2017,35 +1914,17 @@ export class ConversationControlActionImpl {{ rejectedReason: reason, status: 'rejected' },optimisticContext,);- const shouldUseGatewayResume = await this.#shouldUseGatewayResume(effectiveContext);-- if (!shouldUseGatewayResume) this.#writeTopicStatus(effectiveContext, 'active');+ const shouldUseGatewayResume = await resolveGatewayResume(+ effectiveContext,+ [messageId],+ this.#get,+ );const toolContent = !!reason? `User reject this tool calling with reason: ${reason}`: 'User reject this tool calling without reason';- if (!shouldUseGatewayResume) {- await this.#interventions.settleAfter([messageId], async () => {- await this.#get().optimisticUpdateMessagePlugin(- toolMessage.id,- { intervention: { rejectedReason: reason, status: 'rejected' } },- optimisticContext,- );- await this.#get().optimisticUpdateMessageContent(- messageId,- toolContent,- undefined,- optimisticContext,- );- });- }const requestMetadata = this.#getRequestMetadataFromMessageChain(messageId);- // Server-mode: start a **new** Gateway op carrying the rejection.- // We use `rejected_continue` uniformly — server-side `rejected` and- // `rejected_continue` share the same code path (both surface the- // rejection to the LLM as user feedback), so a separate `rejected`- // decision adds complexity without behavioural difference.if (shouldUseGatewayResume) {const toolCallId = toolMessage.tool_call_id;if (!toolCallId) {@@ -2065,38 +1944,45 @@ export class ConversationControlActionImpl {});if (this.#discardAlreadyResolvedSource(sourceResolution, operationId)) return;- // Park → resume: the new gateway op continues the run paused on this tool.- this.#emitRunResumed(effectiveContext, {- operationId,- parentMessageId: messageId,- runtimeType: 'gateway',- });- if (sourceResolution.handled) {- if (sourceResolution.execution) {+ if (+ sourceResolution.handled ||+ (await resolveConfiguredGatewayResume(effectiveContext, this.#get))+ ) {+ // Park → resume: the new gateway op continues the run paused on this tool.+ this.#emitRunResumed(effectiveContext, {+ operationId,+ parentMessageId: messageId,+ runtimeType: 'gateway',+ });+ if (sourceResolution.handled) {+ if (sourceResolution.execution) {+ await this.#get().executeGatewayAgent({+ context: effectiveContext,+ message: '',+ metadata: requestMetadata,+ parentMessageId: messageId,+ precreatedResult: sourceResolution.execution,+ });+ }+ } else {await this.#get().executeGatewayAgent({context: effectiveContext,message: '',metadata: requestMetadata,parentMessageId: messageId,- precreatedResult: sourceResolution.execution,+ resumeApproval: {+ decision: 'rejected_continue',+ parentMessageId: messageId,+ rejectionReason: reason,+ toolCallId,+ },});}- } else {- await this.#get().executeGatewayAgent({- context: effectiveContext,- message: '',- metadata: requestMetadata,- parentMessageId: messageId,- resumeApproval: {- decision: 'rejected_continue',- parentMessageId: messageId,- rejectionReason: reason,- toolCallId,- },- });+ this.#writeTopicStatus(effectiveContext, 'active');+ this.#completeOpsById(pausedOpIds);+ completeOperation(operationId);+ return;}- this.#writeTopicStatus(effectiveContext, 'active');- this.#completeOpsById(pausedOpIds);} catch (error) {const err = error as Error;console.error('[rejectToolCalling][server] Gateway resume failed:', err);@@ -2109,6 +1995,21 @@ export class ConversationControlActionImpl {}}+ this.#writeTopicStatus(effectiveContext, 'active');+ await this.#interventions.settleAfter([messageId], async () => {+ await this.#get().optimisticUpdateMessagePlugin(+ toolMessage.id,+ { intervention: { rejectedReason: reason, status: 'rejected' } },+ optimisticContext,+ );+ await this.#get().optimisticUpdateMessageContent(+ messageId,+ toolContent,+ undefined,+ optimisticContext,+ );+ });+completeOperation(operationId);};@@ -2141,7 +2042,7 @@ export class ConversationControlActionImpl {{ rejectedReason: reason, status: 'rejected' },{ context: effectiveContext },);- if (await this.#shouldUseGatewayResume(effectiveContext)) {+ if (await resolveGatewayResume(effectiveContext, [messageId], this.#get)) {const requestMetadata = this.#getRequestMetadataFromMessageChain(messageId);const toolCallId = toolMessage.tool_call_id;if (!toolCallId) {@@ -2153,7 +2054,6 @@ export class ConversationControlActionImpl {}const pausedOpIds = this.#getRunningServerOps(effectiveContext).map((op) => op.id);-const { operationId } = startOperation({type: 'rejectToolCalling',context: {@@ -2173,38 +2073,45 @@ export class ConversationControlActionImpl {});if (this.#discardAlreadyResolvedSource(sourceResolution, operationId)) return;- // Park → resume: the new gateway op continues the run paused on this tool.- this.#emitRunResumed(effectiveContext, {- operationId,- parentMessageId: messageId,- runtimeType: 'gateway',- });- if (sourceResolution.handled) {- if (sourceResolution.execution) {+ if (+ sourceResolution.handled ||+ (await resolveConfiguredGatewayResume(effectiveContext, this.#get))+ ) {+ // Park → resume: the new gateway op continues the run paused on this tool.+ this.#emitRunResumed(effectiveContext, {+ operationId,+ parentMessageId: messageId,+ runtimeType: 'gateway',+ });+ if (sourceResolution.handled) {+ if (sourceResolution.execution) {+ await this.#get().executeGatewayAgent({+ context: effectiveContext,+ message: '',+ metadata: requestMetadata,+ parentMessageId: messageId,+ precreatedResult: sourceResolution.execution,+ });+ }+ } else {await this.#get().executeGatewayAgent({context: effectiveContext,message: '',metadata: requestMetadata,parentMessageId: messageId,- precreatedResult: sourceResolution.execution,+ resumeApproval: {+ decision: 'rejected_continue',+ parentMessageId: messageId,+ rejectionReason: reason,+ toolCallId,+ },});}- } else {- await this.#get().executeGatewayAgent({- context: effectiveContext,- message: '',- metadata: requestMetadata,- parentMessageId: messageId,- resumeApproval: {- decision: 'rejected_continue',- parentMessageId: messageId,- rejectionReason: reason,- toolCallId,- },- });+ this.#writeTopicStatus(effectiveContext, 'active');+ this.#completeOpsById(pausedOpIds);+ completeOperation(operationId);+ return;}- this.#writeTopicStatus(effectiveContext, 'active');- this.#completeOpsById(pausedOpIds);completeOperation(operationId);} catch (error) {const err = error as Error;@@ -2216,7 +2123,6 @@ export class ConversationControlActionImpl {});throw error;}- return;}// Client-mode path: reject first (persists rejection + updates content),diff --git a/src/store/chat/slices/agentRun/actions/entries/gatewayResume.ts b/src/store/chat/slices/agentRun/actions/entries/gatewayResume.tsnew file mode 100644index 00000000000..f02e8ca53e8--- /dev/null+++ b/src/store/chat/slices/agentRun/actions/entries/gatewayResume.ts@@ -0,0 +1,112 @@+import { isHeterogeneousAgentModelId } from '@lobechat/const';+import { type ConversationContext, resolveAgentAgencyConfig } from '@lobechat/types';++import {+ ensureAgentManagementAccess,+ getRuntimeCanManageAgent,+} from '@/helpers/agentManagementAccess';+import {+ applyTopicDeviceBinding,+ getTopicBoundDeviceId,+ resolveWorkspaceScoped,+} from '@/helpers/executionTarget';+import { getAgentStoreState } from '@/store/agent';+import { agentByIdSelectors, agentSelectors } from '@/store/agent/selectors';+import { topicSelectors } from '@/store/chat/selectors';+import { dbMessageSelectors } from '@/store/chat/slices/message/selectors';+import { type ChatStore } from '@/store/chat/store';+import { getElectronStoreState } from '@/store/electron';+import { useUserStore } from '@/store/user';+import { userProfileSelectors } from '@/store/user/selectors';++import { selectRuntimeType } from '../dispatch/agentDispatcher';++/** A persisted server source survives reloads and changes to the agent's execution target. */+export const shouldUseGatewayResume = async (+ context: ConversationContext,+ toolMessageIds: string[],+ get: () => ChatStore,+): Promise<boolean> => {+ const interventions = toolMessageIds.map(+ (id) => dbMessageSelectors.getDbMessageById(id)(get())?.pluginIntervention,+ );+ if (+ interventions.some(+ (source) => source?.runtimeType === 'server' && !!source.operationId && !!source.batchId,+ )+ )+ return true;+ if (interventions.length > 0 && interventions.every((source) => source?.runtimeType === 'client'))+ return false;++ return shouldUseConfiguredGatewayResume(context, get);+};++/** The legacy route applies when this deployment does not handle the persisted source. */+export const shouldUseConfiguredGatewayResume = async (+ context: ConversationContext,+ get: () => ChatStore,+): Promise<boolean> => {+ // A share run only ever executes on the server through the share gateway;+ // the visitor has no local runtime nor access to the owner's agent config.+ if (context.agentShareId) return true;++ const agentId = context.agentId;+ if (!agentId) return false;++ const agentState = getAgentStoreState();+ const agentConfig = agentSelectors.getAgentConfigById(agentId)(agentState);+ const agent = agentByIdSelectors.getAgentById(agentId)(agentState);+ const currentUserId = userProfileSelectors.userId(useUserStore.getState());+ await ensureAgentManagementAccess({+ agentId,+ agentUserId: agent?.userId,+ currentUserId,+ visibility: agent?.visibility,+ workspaceId: agent?.workspaceId,+ });+ const canManage = getRuntimeCanManageAgent({+ agentId,+ agentUserId: agent?.userId,+ currentUserId,+ });+ const usesWorkspaceMemberSelection =+ !!agent?.workspaceId && agent.visibility !== 'private' && !canManage;+ const deviceOverride = agent?.workspaceId+ ? useUserStore.getState().workspaceUserPreference.agentDeviceOverrides?.[agentId]+ : undefined;+ // Same topic machine binding as sendMessage, so the resume takes the path+ // the paused run was dispatched on.+ const { agencyConfig, workspaceScoped } = applyTopicDeviceBinding(+ {+ agencyConfig: resolveAgentAgencyConfig(agentConfig?.agencyConfig, deviceOverride, {+ canManage,+ visibility: agent?.visibility,+ workspaceId: agent?.workspaceId,+ }),+ workspaceScoped: resolveWorkspaceScoped(usesWorkspaceMemberSelection, deviceOverride),+ },+ getTopicBoundDeviceId(+ context.topicId ? topicSelectors.getTopicById(context.topicId)(get()) : undefined,+ agentId,+ ),+ getElectronStoreState().gatewayDeviceInfo?.deviceId,+ );+ const isGatewayMode = get().isGatewayModeEnabled(agentId);+ const heterogeneousProvider =+ agencyConfig?.heterogeneousProvider ??+ (!isGatewayMode && isHeterogeneousAgentModelId(agentConfig?.model)+ ? { type: agentConfig.model }+ : undefined);++ return (+ selectRuntimeType({+ boundDeviceId: agencyConfig?.boundDeviceId,+ executionTarget: agencyConfig?.executionTarget,+ heterogeneousProvider,+ isGatewayMode,+ isWorkspaceAgent: !!agent?.workspaceId,+ workspaceScoped,+ }) === 'gateway'+ );+};