Skip to content

🀯 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.

✨ feat(device): auto-activate a device for bot triggers on a local target (#16032)

Arvin Xu9e9ab1f05d47
diff --git a/apps/server/src/modules/Mecha/AgentToolsEngine/index.ts b/apps/server/src/modules/Mecha/AgentToolsEngine/index.ts
index 75a5cd51892..3269d5870d5 100644
--- a/apps/server/src/modules/Mecha/AgentToolsEngine/index.ts
+++ b/apps/server/src/modules/Mecha/AgentToolsEngine/index.ts
@@ -161,7 +161,7 @@ export const createServerAgentToolsEngine = (
const executionTarget =
executionPlan?.target ??
resolveExecutionTarget(agentConfig.agencyConfig, {
- isDesktop: platform === 'desktop',
+ clientExecutionAvailable: platform === 'desktop',
});
const runtimeMode: RuntimeEnvMode = executionTargetToRuntimeMode(executionTarget);
// Device tools (local-system, remote-device proxy) only exist for
diff --git a/apps/server/src/services/aiAgent/index.ts b/apps/server/src/services/aiAgent/index.ts
index faf8402d4f9..5d1f82110ee 100644
--- a/apps/server/src/services/aiAgent/index.ts
+++ b/apps/server/src/services/aiAgent/index.ts
@@ -1470,9 +1470,10 @@ export class AiAgentService {
const heteroPlan = resolveExecutionPlan({
agencyConfig: agentConfig.agencyConfig,
canUseDevice,
- isDesktop: false,
isHetero: true,
+ clientExecutionAvailable: false,
requestedDeviceId,
+ trigger: requestTriggerMetadata?.trigger,
});
if (heteroPlan.kind !== 'sandbox') {
@@ -1933,9 +1934,9 @@ export class AiAgentService {
// engine's enabledToolIds exclusion β€” resolving the plan here closes
// that bypass at the source.
//
- // `isDesktop` uses `gatewayConfigured` as a proxy: a device-gateway
- // deployment serves desktop-class users, so the unset-target default
- // resolves to `local` there and `none` otherwise.
+ // `clientExecutionAvailable` is `gatewayConfigured` here: a server with a
+ // device gateway can tunnel a `local` target to the user's device, so the
+ // unset-target default resolves to `local` there and `none` otherwise.
//
// Chat mode is orthogonal to `executionTarget` (the UI toggle only writes
// `enableAgentMode`), so a default/stored `local` target would otherwise
@@ -1947,9 +1948,10 @@ export class AiAgentService {
agencyConfig: agentConfig.agencyConfig,
canUseDevice,
chatConfig: agentConfig.chatConfig ?? undefined,
- isDesktop: gatewayConfigured,
+ clientExecutionAvailable: gatewayConfigured,
onlineDeviceIds: onlineDevices.map((device) => device.deviceId),
requestedDeviceId,
+ trigger: requestTriggerMetadata?.trigger,
});
// Device tools (local-system / remote-device proxy) only exist in a
// device-capable session β€” `none` and `sandbox` sessions must never see
diff --git a/src/features/AgentDocumentPage/RightPanel/index.tsx b/src/features/AgentDocumentPage/RightPanel/index.tsx
index b41f2a2df82..2bef03fb24f 100644
--- a/src/features/AgentDocumentPage/RightPanel/index.tsx
+++ b/src/features/AgentDocumentPage/RightPanel/index.tsx
@@ -82,8 +82,8 @@ const AgentDocumentRightPanel = memo(() => {
activeAgentId ? agentByIdSelectors.getAgencyConfigById(activeAgentId)(s) : undefined,
);
const effectiveTarget = resolveExecutionTarget(agencyConfig, {
- isDesktop,
isHetero,
+ clientExecutionAvailable: isDesktop,
});
const remoteDeviceId =
effectiveTarget === 'device' && agencyConfig?.boundDeviceId
diff --git a/src/features/ChatInput/ControlBar/HeteroDeviceSwitcher.tsx b/src/features/ChatInput/ControlBar/HeteroDeviceSwitcher.tsx
index 416f7d672a9..f0cdaf18c52 100644
--- a/src/features/ChatInput/ControlBar/HeteroDeviceSwitcher.tsx
+++ b/src/features/ChatInput/ControlBar/HeteroDeviceSwitcher.tsx
@@ -322,7 +322,10 @@ const HeteroDeviceSwitcher = memo<HeteroDeviceSwitcherProps>(({ agentId }) => {
// Effective target: shared with server dispatch. In particular, a hetero
// desktop "local" selection that carries this desktop's boundDeviceId becomes
// a device target when the same agent is opened from web.
- const executionTarget = resolveExecutionTarget(agencyConfig, { isDesktop, isHetero });
+ const executionTarget = resolveExecutionTarget(agencyConfig, {
+ isHetero,
+ clientExecutionAvailable: isDesktop,
+ });
const handleSelect = useCallback(
async (target: DeviceExecutionTarget, deviceId?: string) => {
diff --git a/src/features/ChatInput/ControlBar/WorkspaceControls.tsx b/src/features/ChatInput/ControlBar/WorkspaceControls.tsx
index 0d1218b9bee..2c9f514d8b5 100644
--- a/src/features/ChatInput/ControlBar/WorkspaceControls.tsx
+++ b/src/features/ChatInput/ControlBar/WorkspaceControls.tsx
@@ -37,8 +37,8 @@ const WorkspaceControls = memo<WorkspaceControlsProps>(
const isHeterogeneous = useAgentStore(agentByIdSelectors.isAgentHeterogeneousById(agentId));
const agencyConfig = useAgentStore(agentByIdSelectors.getAgencyConfigById(agentId));
const effectiveTarget = resolveExecutionTarget(agencyConfig, {
- isDesktop,
isHetero: isHeterogeneous,
+ clientExecutionAvailable: isDesktop,
});
const isDeviceMode = effectiveTarget === 'device' && !!agencyConfig?.boundDeviceId;
diff --git a/src/features/ChatInput/InputEditor/ActionTag/useSlashActionItems.ts b/src/features/ChatInput/InputEditor/ActionTag/useSlashActionItems.ts
index 596f0cd1d5e..af412627605 100644
--- a/src/features/ChatInput/InputEditor/ActionTag/useSlashActionItems.ts
+++ b/src/features/ChatInput/InputEditor/ActionTag/useSlashActionItems.ts
@@ -69,7 +69,10 @@ export const useSlashActionItems = (): SlashOptions['items'] => {
const isHetero = useAgentStore((s) =>
agentId ? agentByIdSelectors.isAgentHeterogeneousById(agentId)(s) : false,
);
- const effectiveTarget = resolveExecutionTarget(agencyConfig, { isDesktop, isHetero });
+ const effectiveTarget = resolveExecutionTarget(agencyConfig, {
+ isHetero,
+ clientExecutionAvailable: isDesktop,
+ });
const isDeviceMode = effectiveTarget === 'device' && !!agencyConfig?.boundDeviceId;
const remoteDeviceId = isDeviceMode ? agencyConfig.boundDeviceId : undefined;
diff --git a/src/helpers/executionTarget.test.ts b/src/helpers/executionTarget.test.ts
index 1542e29df7a..3f4e5f1abde 100644
--- a/src/helpers/executionTarget.test.ts
+++ b/src/helpers/executionTarget.test.ts
@@ -1,4 +1,5 @@
import type { LobeAgentAgencyConfig } from '@lobechat/types';
+import { RequestTrigger } from '@lobechat/types';
import { describe, expect, it } from 'vitest';
import {
@@ -13,75 +14,152 @@ const cfg = (over: Partial<LobeAgentAgencyConfig> = {}): LobeAgentAgencyConfig =
describe('resolveExecutionTarget', () => {
it('returns the stored target verbatim when set', () => {
- expect(resolveExecutionTarget(cfg({ executionTarget: 'device' }), { isDesktop: true })).toBe(
- 'device',
- );
- expect(resolveExecutionTarget(cfg({ executionTarget: 'sandbox' }), { isDesktop: true })).toBe(
- 'sandbox',
- );
+ expect(
+ resolveExecutionTarget(cfg({ executionTarget: 'device' }), {
+ clientExecutionAvailable: true,
+ }),
+ ).toBe('device');
+ expect(
+ resolveExecutionTarget(cfg({ executionTarget: 'sandbox' }), {
+ clientExecutionAvailable: true,
+ }),
+ ).toBe('sandbox');
});
it('defaults to local on desktop, none on web when unset', () => {
- expect(resolveExecutionTarget(undefined, { isDesktop: true })).toBe('local');
- expect(resolveExecutionTarget(undefined, { isDesktop: false })).toBe('none');
- expect(resolveExecutionTarget(cfg(), { isDesktop: true })).toBe('local');
- expect(resolveExecutionTarget(cfg(), { isDesktop: false })).toBe('none');
+ expect(resolveExecutionTarget(undefined, { clientExecutionAvailable: true })).toBe('local');
+ expect(resolveExecutionTarget(undefined, { clientExecutionAvailable: false })).toBe('none');
+ expect(resolveExecutionTarget(cfg(), { clientExecutionAvailable: true })).toBe('local');
+ expect(resolveExecutionTarget(cfg(), { clientExecutionAvailable: false })).toBe('none');
});
it('coerces a stored `local` to `sandbox` on web (no local filesystem)', () => {
- expect(resolveExecutionTarget(cfg({ executionTarget: 'local' }), { isDesktop: false })).toBe(
- 'sandbox',
- );
+ expect(
+ resolveExecutionTarget(cfg({ executionTarget: 'local' }), {
+ clientExecutionAvailable: false,
+ }),
+ ).toBe('sandbox');
// …but keeps it on desktop
- expect(resolveExecutionTarget(cfg({ executionTarget: 'local' }), { isDesktop: true })).toBe(
- 'local',
- );
+ expect(
+ resolveExecutionTarget(cfg({ executionTarget: 'local' }), { clientExecutionAvailable: true }),
+ ).toBe('local');
});
it('routes hetero desktop-local bindings to the bound device on web', () => {
expect(
resolveExecutionTarget(cfg({ boundDeviceId: 'device-a', executionTarget: 'local' }), {
- isDesktop: false,
+ clientExecutionAvailable: false,
isHetero: true,
}),
).toBe('device');
expect(
resolveExecutionTarget(cfg({ boundDeviceId: 'device-a', executionTarget: 'local' }), {
- isDesktop: false,
+ clientExecutionAvailable: false,
}),
).toBe('sandbox');
});
it('keeps `device` on web (a bound device is reachable from anywhere)', () => {
- expect(resolveExecutionTarget(cfg({ executionTarget: 'device' }), { isDesktop: false })).toBe(
- 'device',
- );
+ expect(
+ resolveExecutionTarget(cfg({ executionTarget: 'device' }), {
+ clientExecutionAvailable: false,
+ }),
+ ).toBe('device');
});
it('keeps an explicit `none` on both platforms', () => {
- expect(resolveExecutionTarget(cfg({ executionTarget: 'none' }), { isDesktop: true })).toBe(
- 'none',
- );
- expect(resolveExecutionTarget(cfg({ executionTarget: 'none' }), { isDesktop: false })).toBe(
- 'none',
- );
+ expect(
+ resolveExecutionTarget(cfg({ executionTarget: 'none' }), { clientExecutionAvailable: true }),
+ ).toBe('none');
+ expect(
+ resolveExecutionTarget(cfg({ executionTarget: 'none' }), { clientExecutionAvailable: false }),
+ ).toBe('none');
});
it('coerces `none` for hetero agents β€” they must execute somewhere', () => {
// stored none β†’ desktop local, web sandbox
expect(
- resolveExecutionTarget(cfg({ executionTarget: 'none' }), { isDesktop: true, isHetero: true }),
+ resolveExecutionTarget(cfg({ executionTarget: 'none' }), {
+ clientExecutionAvailable: true,
+ isHetero: true,
+ }),
).toBe('local');
expect(
resolveExecutionTarget(cfg({ executionTarget: 'none' }), {
- isDesktop: false,
+ clientExecutionAvailable: false,
isHetero: true,
}),
).toBe('sandbox');
// unset β†’ platform default, then the same coercion on web
- expect(resolveExecutionTarget(undefined, { isDesktop: true, isHetero: true })).toBe('local');
- expect(resolveExecutionTarget(undefined, { isDesktop: false, isHetero: true })).toBe('sandbox');
+ expect(
+ resolveExecutionTarget(undefined, { clientExecutionAvailable: true, isHetero: true }),
+ ).toBe('local');
+ expect(
+ resolveExecutionTarget(undefined, { clientExecutionAvailable: false, isHetero: true }),
+ ).toBe('sandbox');
+ });
+
+ describe('trigger=bot β€” upgrades a local target (bound β†’ device, unbound β†’ auto)', () => {
+ it('coerces an UNBOUND desktop `local` (and the unset desktop default) to auto', () => {
+ expect(
+ resolveExecutionTarget(cfg({ executionTarget: 'local' }), {
+ clientExecutionAvailable: true,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toBe('auto');
+ // unset desktop default is `local`, so it coerces too
+ expect(
+ resolveExecutionTarget(undefined, {
+ clientExecutionAvailable: true,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toBe('auto');
+ });
+
+ it('routes a BOUND `local` to `device` β€” honours the pinned machine, not auto', () => {
+ // the switcher persists the desktop's own deviceId as boundDeviceId for a
+ // `local` pick; a bot run must reach THAT machine, not auto-grab another.
+ expect(
+ resolveExecutionTarget(cfg({ boundDeviceId: 'device-a', executionTarget: 'local' }), {
+ clientExecutionAvailable: true,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toBe('device');
+ });
+
+ it('leaves `none` / `sandbox` / `device` untouched β€” explicit intent', () => {
+ for (const executionTarget of ['none', 'sandbox', 'device'] as const) {
+ expect(
+ resolveExecutionTarget(cfg({ executionTarget }), {
+ clientExecutionAvailable: true,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toBe(executionTarget);
+ }
+ });
+
+ it('does not resurrect a device on web β€” `local` still coerces to sandbox first', () => {
+ // the web→sandbox coercion runs before the bot rule, so a web `local`
+ // never becomes auto (there is no in-process local on web anyway).
+ expect(
+ resolveExecutionTarget(cfg({ executionTarget: 'local' }), {
+ clientExecutionAvailable: false,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toBe('sandbox');
+ });
+
+ it('only fires for the bot trigger β€” other triggers keep `local`', () => {
+ for (const trigger of [RequestTrigger.Chat, RequestTrigger.Cron, undefined]) {
+ expect(
+ resolveExecutionTarget(cfg({ executionTarget: 'local' }), {
+ clientExecutionAvailable: true,
+ trigger,
+ }),
+ ).toBe('local');
+ }
+ });
});
});
@@ -126,14 +204,14 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: cfg({ executionTarget: 'none' }),
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_A,
}),
).toEqual({ kind: 'none', target: 'none' });
expect(
resolveExecutionPlan({
agencyConfig: cfg({ boundDeviceId: 'device-a', executionTarget: 'none' }),
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_A,
}),
).toEqual({ kind: 'none', target: 'none' });
@@ -145,7 +223,7 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: cfg({ boundDeviceId: 'device-a', executionTarget: 'sandbox' }),
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_A,
}),
).toEqual({ kind: 'sandbox', target: 'sandbox' });
@@ -156,7 +234,7 @@ describe('resolveExecutionPlan', () => {
resolveExecutionPlan({
agencyConfig: cfg({ executionTarget: 'sandbox' }),
canUseDevice: false,
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_A,
}),
).toEqual({ kind: 'sandbox', target: 'sandbox' });
@@ -168,7 +246,7 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: cfg({ boundDeviceId: 'device-a', executionTarget: 'device' }),
- isDesktop: false,
+ clientExecutionAvailable: false,
onlineDeviceIds: ONLINE_AB,
}),
).toEqual({ deviceId: 'device-a', kind: 'device', target: 'device' });
@@ -178,7 +256,7 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: cfg({ boundDeviceId: 'device-x', executionTarget: 'device' }),
- isDesktop: false,
+ clientExecutionAvailable: false,
onlineDeviceIds: ONLINE_AB,
}),
).toEqual({ kind: 'device-unrouted', reason: 'bound-device-offline', target: 'device' });
@@ -189,10 +267,18 @@ describe('resolveExecutionPlan', () => {
// a single online device used to be auto-activated for `local`; now only
// `auto` does that β€” `local` stays unrouted until a device is bound.
expect(
- resolveExecutionPlan({ agencyConfig: local, isDesktop: true, onlineDeviceIds: ONLINE_A }),
+ resolveExecutionPlan({
+ agencyConfig: local,
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_A,
+ }),
).toEqual({ kind: 'device-unrouted', reason: 'no-bound-device', target: 'local' });
expect(
- resolveExecutionPlan({ agencyConfig: local, isDesktop: true, onlineDeviceIds: ONLINE_AB }),
+ resolveExecutionPlan({
+ agencyConfig: local,
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_AB,
+ }),
).toEqual({ kind: 'device-unrouted', reason: 'no-bound-device', target: 'local' });
});
@@ -201,7 +287,7 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: undefined,
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_A,
}),
).toEqual({ kind: 'device-unrouted', reason: 'no-bound-device', target: 'local' });
@@ -211,7 +297,7 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: undefined,
- isDesktop: false,
+ clientExecutionAvailable: false,
onlineDeviceIds: ONLINE_A,
}),
).toEqual({ kind: 'none', target: 'none' });
@@ -223,25 +309,41 @@ describe('resolveExecutionPlan', () => {
it('activates the single online device', () => {
expect(
- resolveExecutionPlan({ agencyConfig: auto, isDesktop: true, onlineDeviceIds: ONLINE_A }),
+ resolveExecutionPlan({
+ agencyConfig: auto,
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_A,
+ }),
).toEqual({ deviceId: 'device-a', kind: 'device', target: 'auto' });
});
it('stays unrouted (model picks) when several devices are online', () => {
expect(
- resolveExecutionPlan({ agencyConfig: auto, isDesktop: true, onlineDeviceIds: ONLINE_AB }),
+ resolveExecutionPlan({
+ agencyConfig: auto,
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_AB,
+ }),
).toEqual({ kind: 'device-unrouted', reason: 'ambiguous-online-devices', target: 'auto' });
});
it('stays unrouted when no device is online', () => {
expect(
- resolveExecutionPlan({ agencyConfig: auto, isDesktop: true, onlineDeviceIds: [] }),
+ resolveExecutionPlan({
+ agencyConfig: auto,
+ clientExecutionAvailable: true,
+ onlineDeviceIds: [],
+ }),
).toEqual({ kind: 'device-unrouted', reason: 'no-online-device', target: 'auto' });
});
it('works on web too (auto can pick a remote device)', () => {
expect(
- resolveExecutionPlan({ agencyConfig: auto, isDesktop: false, onlineDeviceIds: ONLINE_A }),
+ resolveExecutionPlan({
+ agencyConfig: auto,
+ clientExecutionAvailable: false,
+ onlineDeviceIds: ONLINE_A,
+ }),
).toEqual({ deviceId: 'device-a', kind: 'device', target: 'auto' });
});
@@ -252,7 +354,7 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: staleBound,
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_AB,
}),
).toEqual({ kind: 'device-unrouted', reason: 'ambiguous-online-devices', target: 'auto' });
@@ -262,7 +364,7 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: auto,
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_AB,
requestedDeviceId: 'device-b',
}),
@@ -275,7 +377,7 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: cfg({ executionTarget: 'sandbox' }),
- isDesktop: false,
+ clientExecutionAvailable: false,
onlineDeviceIds: ONLINE_AB,
requestedDeviceId: 'device-b',
}),
@@ -286,7 +388,7 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: cfg({ boundDeviceId: 'device-a', executionTarget: 'device' }),
- isDesktop: false,
+ clientExecutionAvailable: false,
onlineDeviceIds: ONLINE_AB,
requestedDeviceId: 'device-b',
}),
@@ -294,6 +396,140 @@ describe('resolveExecutionPlan', () => {
});
});
+ describe('trigger=bot β€” upgrades a local target to auto', () => {
+ it('upgrades a stored `local` target to auto and activates the single online device', () => {
+ // a bot conversation can't pick a device, and `local` in-process IPC is
+ // unreachable from the cloud bot server β€” so `local` auto-activates.
+ expect(
+ resolveExecutionPlan({
+ agencyConfig: cfg({ executionTarget: 'local' }),
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_A,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toEqual({ deviceId: 'device-a', kind: 'device', target: 'auto' });
+ });
+
+ it('upgrades the unset desktop default (local) to auto', () => {
+ expect(
+ resolveExecutionPlan({
+ agencyConfig: undefined,
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_A,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toEqual({ deviceId: 'device-a', kind: 'device', target: 'auto' });
+ });
+
+ it('stays unrouted (model picks) when several devices are online', () => {
+ expect(
+ resolveExecutionPlan({
+ agencyConfig: cfg({ executionTarget: 'local' }),
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_AB,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toEqual({ kind: 'device-unrouted', reason: 'ambiguous-online-devices', target: 'auto' });
+ });
+
+ it('routes a BOUND `local` to its pinned device, even with several online (no auto-grab)', () => {
+ // regression: the bot upgrade used to relabel bound `local` β†’ auto, which
+ // ignores boundDeviceId and would auto-pick / go ambiguous. A pinned
+ // machine must win β€” here device-b is bound and stays selected.
+ expect(
+ resolveExecutionPlan({
+ agencyConfig: cfg({ boundDeviceId: 'device-b', executionTarget: 'local' }),
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_AB,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toEqual({ deviceId: 'device-b', kind: 'device', target: 'device' });
+ });
+
+ it('keeps a BOUND `local` on its pinned device when offline (no silent fallback)', () => {
+ expect(
+ resolveExecutionPlan({
+ agencyConfig: cfg({ boundDeviceId: 'device-x', executionTarget: 'local' }),
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_AB,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toEqual({ kind: 'device-unrouted', reason: 'bound-device-offline', target: 'device' });
+ });
+
+ it('does NOT upgrade `none` β€” an explicit opt-out stays plain chat', () => {
+ expect(
+ resolveExecutionPlan({
+ agencyConfig: cfg({ executionTarget: 'none' }),
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_A,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toEqual({ kind: 'none', target: 'none' });
+ });
+
+ it('does NOT upgrade `sandbox` β€” an explicit cloud choice stays', () => {
+ expect(
+ resolveExecutionPlan({
+ agencyConfig: cfg({ executionTarget: 'sandbox' }),
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_A,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toEqual({ kind: 'sandbox', target: 'sandbox' });
+ });
+
+ it('does NOT touch an explicitly bound `device` target (binding wins over auto)', () => {
+ expect(
+ resolveExecutionPlan({
+ agencyConfig: cfg({ boundDeviceId: 'device-b', executionTarget: 'device' }),
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_AB,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toEqual({ deviceId: 'device-b', kind: 'device', target: 'device' });
+ });
+
+ it('still routes a requestedDeviceId-pinned local run to that device (now under auto)', () => {
+ // the local→auto coercion lives in resolveExecutionTarget, so it applies
+ // before requestedDeviceId is considered. The explicit device still wins
+ // the routing; only the target label is `auto` (gateway routing) rather
+ // than `local` (in-process) β€” correct for a server-side bot run.
+ expect(
+ resolveExecutionPlan({
+ agencyConfig: cfg({ executionTarget: 'local' }),
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_AB,
+ requestedDeviceId: 'device-b',
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toEqual({ deviceId: 'device-b', kind: 'device', target: 'auto' });
+ });
+
+ it('still honours chat mode β€” a bot on a chat-mode agent stays plain chat', () => {
+ expect(
+ resolveExecutionPlan({
+ agencyConfig: cfg({ executionTarget: 'local' }),
+ chatConfig: { enableAgentMode: false },
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_A,
+ trigger: RequestTrigger.Bot,
+ }),
+ ).toEqual({ kind: 'none', target: 'none' });
+ });
+
+ it('only fires for bot triggers β€” a chat trigger leaves `local` unrouted', () => {
+ expect(
+ resolveExecutionPlan({
+ agencyConfig: cfg({ executionTarget: 'local' }),
+ clientExecutionAvailable: true,
+ onlineDeviceIds: ONLINE_A,
+ trigger: RequestTrigger.Chat,
+ }),
+ ).toEqual({ kind: 'device-unrouted', reason: 'no-bound-device', target: 'local' });
+ });
+ });
+
describe('canUseDevice=false β€” external bot senders', () => {
it('degrades every device-capable target to none', () => {
for (const executionTarget of ['local', 'device', 'none'] as const) {
@@ -301,7 +537,7 @@ describe('resolveExecutionPlan', () => {
resolveExecutionPlan({
agencyConfig: cfg({ boundDeviceId: 'device-a', executionTarget }),
canUseDevice: false,
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_A,
requestedDeviceId: 'device-a',
}),
@@ -323,7 +559,7 @@ describe('resolveExecutionPlan', () => {
resolveExecutionPlan({
agencyConfig: cfg({ boundDeviceId: 'device-a', executionTarget }),
chatConfig,
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_A,
}),
).toEqual({ kind: 'none', target: 'none' });
@@ -336,7 +572,7 @@ describe('resolveExecutionPlan', () => {
resolveExecutionPlan({
agencyConfig: undefined,
chatConfig: { enableAgentMode: false },
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_A,
}),
).toEqual({ kind: 'none', target: 'none' });
@@ -344,7 +580,7 @@ describe('resolveExecutionPlan', () => {
resolveExecutionPlan({
agencyConfig: cfg({ executionTarget: 'sandbox' }),
chatConfig: { enableAgentMode: false },
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_A,
}),
).toEqual({ kind: 'none', target: 'none' });
@@ -359,7 +595,7 @@ describe('resolveExecutionPlan', () => {
resolveExecutionPlan({
agencyConfig: cfg({ executionTarget: 'auto' }),
chatConfig: { enableAgentMode: false, toolMode: 'agent' },
- isDesktop: true,
+ clientExecutionAvailable: true,
onlineDeviceIds: ONLINE_A,
}),
).toEqual({ deviceId: 'device-a', kind: 'device', target: 'auto' });
@@ -370,7 +606,7 @@ describe('resolveExecutionPlan', () => {
resolveExecutionPlan({
agencyConfig: cfg({ boundDeviceId: 'device-a', executionTarget: 'device' }),
chatConfig: { enableAgentMode: false },
- isDesktop: false,
+ clientExecutionAvailable: false,
isHetero: true,
onlineDeviceIds: ONLINE_A,
}),
@@ -388,7 +624,7 @@ describe('resolveExecutionPlan', () => {
resolveExecutionPlan({
agencyConfig: cfg({ boundDeviceId: 'device-a', executionTarget }),
canUseDevice: false,
- isDesktop: false,
+ clientExecutionAvailable: false,
isHetero: true,
}),
).toEqual({ kind: 'sandbox', target: 'sandbox' });
@@ -398,7 +634,7 @@ describe('resolveExecutionPlan', () => {
resolveExecutionPlan({
agencyConfig: cfg({ executionTarget: 'sandbox' }),
canUseDevice: false,
- isDesktop: false,
+ clientExecutionAvailable: false,
isHetero: true,
requestedDeviceId: 'device-a',
}),
@@ -411,14 +647,14 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: cfg({ boundDeviceId: 'device-a', executionTarget: 'device' }),
- isDesktop: false,
+ clientExecutionAvailable: false,
isHetero: true,
}),
).toEqual({ deviceId: 'device-a', kind: 'device', target: 'device' });
expect(
resolveExecutionPlan({
agencyConfig: cfg({ executionTarget: 'device' }),
- isDesktop: false,
+ clientExecutionAvailable: false,
isHetero: true,
}),
).toEqual({ kind: 'device-unrouted', reason: 'no-bound-device', target: 'device' });
@@ -428,7 +664,7 @@ describe('resolveExecutionPlan', () => {
expect(
resolveExecutionPlan({
agencyConfig: cfg({ boundDeviceId: 'device-a', executionTarget: 'local' }),
- isDesktop: false,
+ clientExecutionAvailable: false,
isHetero: true,
}),
).toEqual({ deviceId: 'device-a', kind: 'device', target: 'device' });
@@ -440,7 +676,7 @@ describe('resolveExecutionPlan', () => {
for (const executionTarget of ['local', 'none', 'sandbox', undefined] as const) {
const plan: ExecutionPlan = resolveExecutionPlan({
agencyConfig: executionTarget ? cfg({ executionTarget }) : undefined,
- isDesktop: false,
+ clientExecutionAvailable: false,
isHetero: true,
});
expect(plan).toEqual({ kind: 'sandbox', target: 'sandbox' });
diff --git a/src/helpers/executionTarget.ts b/src/helpers/executionTarget.ts
index 865ad17d59c..61b07306325 100644
--- a/src/helpers/executionTarget.ts
+++ b/src/helpers/executionTarget.ts
@@ -4,6 +4,7 @@ import type {
LobeAgentChatConfig,
RuntimeEnvMode,
} from '@lobechat/types';
+import { RequestTrigger } from '@lobechat/types';
/**
* The agent's tool mode β€” explicit `chatConfig.toolMode` wins; otherwise derive
@@ -20,17 +21,40 @@ export const resolveToolMode = (
export interface ResolveExecutionTargetOptions {
/**
- * Platform of the resolving side. On the server there is no real "desktop"
- * flag β€” callers pass `gatewayConfigured` as a proxy (a device-gateway
- * deployment serves desktop-class users). See `resolveExecutionPlan`.
+ * Whether tools can run on the user's own client/device in this environment β€”
+ * i.e. the `local` target (`runtimeMode: 'local'`, the `'client'` executor)
+ * has somewhere to run. This is the boolean form of `RuntimePlatform`
+ * (`'desktop'` vs `'web'`); it is NOT "the build is desktop" and NOT "the
+ * message came from a desktop client". It is true for:
+ * - the Electron desktop build (the client runs tools in-process), and
+ * - a server with a device gateway (`!!DEVICE_GATEWAY_URL`), which tunnels
+ * the run to a registered device β€” the client lives at the other end.
+ * When false (plain web / a server with no gateway) there is no client to run
+ * on, so a `local` target coerces to `sandbox` (cloud) or the default is
+ * `none` (plain chat). Each layer passes the value that means this for it:
+ * `isDesktop` (build const) in the UI, `gatewayConfigured` on the server,
+ * `hasDeviceProxy` in the tools engine.
+ *
+ * Note this gates only `local` β€” `device` (an explicit `boundDeviceId`) is a
+ * concrete remote target reachable from anywhere, so it is honoured even when
+ * this is false.
*/
- isDesktop: boolean;
+ clientExecutionAvailable: boolean;
/**
* Heterogeneous agents (Claude Code / Codex) bring their own toolchain and
* must execute somewhere, so `'none'` is not a valid target for them: it
* coerces to `'local'` on desktop and `'sandbox'` on web.
*/
isHetero?: boolean;
+ /**
+ * What initiated the run. A `bot` trigger has no UI to pick a device, and
+ * `local` (in-process IPC) is unreachable from the cloud bot server β€” so a
+ * stored `local` target is upgraded: to `device` when it carries a
+ * `boundDeviceId` (route to the pinned machine), otherwise to `auto`
+ * (auto-activate an online device). `none` / `sandbox` are explicit opt-outs
+ * and are left untouched.
+ */
+ trigger?: RequestTrigger;
}
/**
@@ -56,18 +80,35 @@ export interface ResolveExecutionTargetOptions {
* resolves to `sandbox`. For heterogeneous CLI agents, a desktop `local`
* selection that has already been bound to that desktop's `deviceId` resolves
* to `device` on web, so the same machine can execute through `lh connect`.
+ *
+ * Bot triggers (`trigger === bot`) upgrade a `local` target (a bot has no UI
+ * to pick a device and `local` in-process IPC is unreachable from the cloud
+ * bot server): to `device` when a `boundDeviceId` pins a specific machine,
+ * otherwise to `auto` to auto-activate an online device. `none` / `sandbox`
+ * are explicit opt-outs and stay.
*/
export const resolveExecutionTarget = (
agencyConfig: LobeAgentAgencyConfig | undefined,
- { isDesktop, isHetero }: ResolveExecutionTargetOptions,
+ { clientExecutionAvailable, isHetero, trigger }: ResolveExecutionTargetOptions,
): DeviceExecutionTarget => {
const stored = agencyConfig?.executionTarget;
- let effective = stored ?? (isDesktop ? 'local' : 'none');
- if (isHetero && !isDesktop && stored === 'local' && agencyConfig?.boundDeviceId) {
+ let effective = stored ?? (clientExecutionAvailable ? 'local' : 'none');
+ if (isHetero && !clientExecutionAvailable && stored === 'local' && agencyConfig?.boundDeviceId) {
return 'device';
}
- if (isHetero && effective === 'none') effective = isDesktop ? 'local' : 'sandbox';
- if (!isDesktop && effective === 'local') return 'sandbox';
+ if (isHetero && effective === 'none') effective = clientExecutionAvailable ? 'local' : 'sandbox';
+ if (!clientExecutionAvailable && effective === 'local') return 'sandbox';
+ // Bot trigger: a `local` target can't run in-process from the cloud bot
+ // server, so it has to reach a real device. If the user pinned a specific
+ // machine (the switcher persists that desktop's own `deviceId` as
+ // `boundDeviceId` for a `local` pick), honour it as `device` β€” `auto` would
+ // ignore the binding and could grab a different online device, or go
+ // ambiguous with several. Only an UNBOUND `local` auto-activates. Sits after
+ // the web→sandbox coercion, so `effective` is only still `local` when a
+ // client/device can actually run it here.
+ if (trigger === RequestTrigger.Bot && effective === 'local') {
+ return agencyConfig?.boundDeviceId ? 'device' : 'auto';
+ }
return effective;
};
@@ -98,9 +139,9 @@ export const executionTargetToRuntimeMode = (target: DeviceExecutionTarget): Run
*/
export const resolveRuntimeMode = (
agencyConfig: LobeAgentAgencyConfig | undefined,
- isDesktop: boolean,
+ clientExecutionAvailable: boolean,
): RuntimeEnvMode =>
- executionTargetToRuntimeMode(resolveExecutionTarget(agencyConfig, { isDesktop }));
+ executionTargetToRuntimeMode(resolveExecutionTarget(agencyConfig, { clientExecutionAvailable }));
export type ExecutionPlanUnroutedReason =
/** `auto` mode with more than one device online β€” the model must pick one */
@@ -162,7 +203,8 @@ export interface ResolveExecutionPlanParams {
* always need a runtime.
*/
chatConfig?: LobeAgentChatConfig;
- isDesktop: boolean;
+ /** See {@link ResolveExecutionTargetOptions.clientExecutionAvailable}. */
+ clientExecutionAvailable: boolean;
isHetero?: boolean;
/**
* Online device ids from the device gateway. Pass `undefined` to skip
@@ -177,6 +219,14 @@ export interface ResolveExecutionPlanParams {
* of the stored target.
*/
requestedDeviceId?: string;
+ /**
+ * What initiated this run. Bot triggers have no UI to pick a device, so a
+ * stored `local` target (in-process IPC, unreachable from the cloud bot
+ * server) is upgraded to `auto` and auto-activates an online device. `none`
+ * and `sandbox` are deliberate opt-outs and are left untouched. See
+ * `resolveExecutionPlan`.
+ */
+ trigger?: RequestTrigger;
}
/**
@@ -195,16 +245,21 @@ export interface ResolveExecutionPlanParams {
* run auto-activates ONLY in the opt-in `auto` mode (single device β†’ use it;
* several β†’ stay unrouted so the model picks one). `local` / `device` never
* silently grab a device β€” they stay unrouted until one is bound/requested.
+ * Bot triggers coerce a `local` target to `auto` upstream in
+ * `resolveExecutionTarget` (not here) β€” by the time the plan resolves, a bot
+ * run already carries `target: 'auto'` and follows the auto-activation rules
+ * above. `none` / `sandbox` stay as the owner's explicit opt-out.
*/
export const resolveExecutionPlan = (params: ResolveExecutionPlanParams): ExecutionPlan => {
const {
agencyConfig,
canUseDevice = true,
chatConfig,
- isDesktop,
+ clientExecutionAvailable,
isHetero,
onlineDeviceIds,
requestedDeviceId,
+ trigger,
} = params;
// Chat mode = no execution environment (plain chat). It's orthogonal to the
@@ -214,7 +269,11 @@ export const resolveExecutionPlan = (params: ResolveExecutionPlanParams): Execut
// agents always need a runtime, so they never take this path.
if (resolveToolMode(chatConfig) === 'chat' && !isHetero) return { kind: 'none', target: 'none' };
- const target = resolveExecutionTarget(agencyConfig, { isDesktop, isHetero });
+ const target = resolveExecutionTarget(agencyConfig, {
+ isHetero,
+ clientExecutionAvailable,
+ trigger,
+ });
const wantsDevice =
!!requestedDeviceId || target === 'device' || target === 'local' || target === 'auto';
diff --git a/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/ByProjectMode/GroupItem.tsx b/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/ByProjectMode/GroupItem.tsx
index 82e218f587d..16cd6cb47e2 100644
--- a/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/ByProjectMode/GroupItem.tsx
+++ b/src/routes/(main)/agent/_layout/Sidebar/Topic/TopicListContent/ByProjectMode/GroupItem.tsx
@@ -230,8 +230,8 @@ const GroupItem = memo<GroupItemComponentProps>(
// Web can add a topic in a directory too when the agent targets a bound
// device β€” the write goes to `workingDirByDevice`, no Electron dependency.
const effectiveTarget = resolveExecutionTarget(agencyConfig, {
- isDesktop,
isHetero: isHeterogeneous,
+ clientExecutionAvailable: isDesktop,
});
const isDeviceMode = effectiveTarget === 'device' && !!agencyConfig?.boundDeviceId;
const canAddTopic = (isDesktop || isDeviceMode) && !!workingDirectory;
diff --git a/src/routes/(main)/agent/features/Conversation/HeterogeneousChatInput/index.tsx b/src/routes/(main)/agent/features/Conversation/HeterogeneousChatInput/index.tsx
index 6f5f632c5b6..1c790f87593 100644
--- a/src/routes/(main)/agent/features/Conversation/HeterogeneousChatInput/index.tsx
+++ b/src/routes/(main)/agent/features/Conversation/HeterogeneousChatInput/index.tsx
@@ -56,8 +56,8 @@ const HeterogeneousChatInput = memo(() => {
);
const providerType = agencyConfig?.heterogeneousProvider?.type;
const executionTarget = resolveExecutionTarget(agencyConfig, {
- isDesktop,
isHetero: !!providerType,
+ clientExecutionAvailable: isDesktop,
});
const isRemoteAgent = !!providerType && isRemoteHeterogeneousType(providerType);
diff --git a/src/routes/(main)/agent/features/Conversation/WorkingSidebar/index.tsx b/src/routes/(main)/agent/features/Conversation/WorkingSidebar/index.tsx
index 33194bc34e5..5b321dac0ee 100644
--- a/src/routes/(main)/agent/features/Conversation/WorkingSidebar/index.tsx
+++ b/src/routes/(main)/agent/features/Conversation/WorkingSidebar/index.tsx
@@ -102,8 +102,8 @@ const AgentWorkingSidebar = memo(() => {
const targetDeviceId = resolveTargetDeviceId(agencyConfig, currentDeviceId);
const repoType = useRepoType(workingDirectory, targetDeviceId);
const effectiveTarget = resolveExecutionTarget(agencyConfig, {
- isDesktop,
isHetero,
+ clientExecutionAvailable: isDesktop,
});
// Running against a bound device (remote, or this machine as a device): file
diff --git a/src/store/chat/slices/aiChat/actions/agentDispatcher.ts b/src/store/chat/slices/aiChat/actions/agentDispatcher.ts
index ebbdf69b7b2..78e20853e8f 100644
--- a/src/store/chat/slices/aiChat/actions/agentDispatcher.ts
+++ b/src/store/chat/slices/aiChat/actions/agentDispatcher.ts
@@ -110,7 +110,8 @@ export const selectRuntimeType = (
if (ctx.heterogeneousProvider) {
const target = resolveExecutionTarget(
{ boundDeviceId: ctx.boundDeviceId, executionTarget: ctx.executionTarget },
- { isDesktop, isHetero: true },
+ // on the client the desktop build IS where local execution is available
+ { isHetero: true, clientExecutionAvailable: isDesktop },
);
return target === 'local' ? 'hetero' : 'gateway';
}