🤯 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
'use client';
import { HETEROGENEOUS_TYPE_LABELS } from '@lobechat/heterogeneous-agents';
import type { DeviceListItem, LobeAgentAgencyConfig } from '@lobechat/types';
import type { SelectOption, SelectOptions } from '@lobehub/ui';
import { Select } from '@lobehub/ui';
import { MonitorSmartphone } from 'lucide-react';
import { memo, useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import AsyncError from '@/components/AsyncError';
import {
ExecutionTargetLabel,
executionTargetSelectClassNames,
executionTargetValue,
groupExecutionTargetDevices,
parseExecutionTargetValue,
resolveExecutionTargetSelection,
} from '@/features/ExecutionTargetPicker';
import { isHeterogeneousSandboxExecutionAvailable } from '@/helpers/executionTarget';
import { useAgentStore } from '@/store/agent';
import { useDeviceList } from '@/store/device';
import { WorkspaceAgentPolicyCard } from './WorkspaceAgentPolicyCard';
interface WorkspaceAgentDevicePolicyProps {
agentId: string;
showDevicePicker?: boolean;
}
const WorkspaceAgentDevicePolicy = memo<WorkspaceAgentDevicePolicyProps>(
({ agentId, showDevicePicker = true }) => {
const { t } = useTranslation(['setting', 'chat']);
const config = useAgentStore((s) => s.agentMap[agentId]);
const updateAgentConfigById = useAgentStore((s) => s.updateAgentConfigById);
const { data: devices, error, isConfirmed, isLoading, mutate } = useDeviceList();
const { privateWorkspace: privateWorkspaceDevices, publicWorkspace: publicWorkspaceDevices } =
useMemo(() => groupExecutionTargetDevices(devices), [devices]);
const selectableDevices = useMemo(
() => [...privateWorkspaceDevices, ...publicWorkspaceDevices],
[privateWorkspaceDevices, publicWorkspaceDevices],
);
const agencyConfig = config?.agencyConfig;
// This card writes the shared config, so a private device (resolvable only
// by the member who enrolled it) may be bound only when that member is the
// sole runner: a private Agent — fixed or not, as the policy is dormant
// until publication, and publishing re-checks a fixed target
// (`assertFixedExecutionTarget`).
const privateDeviceBlockedReason =
config?.visibility !== 'private'
? t('settingAgent.devicePolicy.privateDevicePublicAgent')
: undefined;
const heterogeneousType = agencyConfig?.heterogeneousProvider?.type;
const isHeterogeneous = !!heterogeneousType;
const supportsSandbox = isHeterogeneousSandboxExecutionAvailable(heterogeneousType);
const targetLabels = useMemo(
() => ({
auto: {
description: t('chat:heteroAgent.executionTarget.autoDesc'),
label: t('chat:heteroAgent.executionTarget.auto'),
},
none: {
description: t('chat:heteroAgent.executionTarget.noneDesc'),
label: t('chat:heteroAgent.executionTarget.none'),
},
sandbox: {
description: t(
supportsSandbox
? 'chat:heteroAgent.executionTarget.sandboxDesc'
: 'chat:heteroAgent.executionTarget.sandboxUnsupported',
{
name: heterogeneousType ? HETEROGENEOUS_TYPE_LABELS[heterogeneousType] : undefined,
},
),
label: t('chat:heteroAgent.executionTarget.sandbox'),
},
}),
[heterogeneousType, supportsSandbox, t],
);
const renderTargetLabel = useCallback(
(target: 'auto' | 'none' | 'sandbox', compact = false) => (
<ExecutionTargetLabel
compact={compact}
description={targetLabels[target].description}
label={targetLabels[target].label}
offlineLabel={t('chat:heteroAgent.executionTarget.offline')}
onlineLabel={t('chat:heteroAgent.executionTarget.online')}
target={target}
unknownLabel={t('chat:heteroAgent.executionTarget.presenceChecking')}
/>
),
[t, targetLabels],
);
const renderDeviceLabel = useCallback(
(device: DeviceListItem, compact = false) => (
<ExecutionTargetLabel
compact={compact}
description=""
device={device}
label={device.friendlyName || device.hostname || device.deviceId}
offlineLabel={t('chat:heteroAgent.executionTarget.offline')}
onlineLabel={t('chat:heteroAgent.executionTarget.online')}
target={'device'}
unknownLabel={t('chat:heteroAgent.executionTarget.presenceChecking')}
/>
),
[t],
);
const targetOptions = useMemo<SelectOptions<string>>(() => {
const sharedTargets: SelectOptions<string> = [
...(!isHeterogeneous
? [
{
label: renderTargetLabel('none'),
title: targetLabels.none.label,
value: executionTargetValue('none'),
},
{
label: renderTargetLabel('auto'),
title: targetLabels.auto.label,
value: executionTargetValue('auto'),
},
]
: []),
{
disabled: !supportsSandbox,
label: renderTargetLabel('sandbox'),
title: targetLabels.sandbox.label,
value: executionTargetValue('sandbox'),
},
];
// Until the server confirms the list, the stored copy may hold devices that
// were since deleted or unshared (binding one would point the agent at an
// unresolvable machine) or lack ones that exist: show its rows, but only
// the confirmed list can be picked from or reported empty.
const workspaceOptions =
isLoading || (!isConfirmed && publicWorkspaceDevices.length === 0)
? [
{
disabled: true,
label: t('chat:heteroAgent.executionTarget.loading'),
value: 'status:loading',
},
]
: publicWorkspaceDevices.length > 0
? publicWorkspaceDevices.map((device) => ({
disabled: !isConfirmed,
label: renderDeviceLabel(device),
title: device.friendlyName || device.hostname || device.deviceId,
value: executionTargetValue('device', device.deviceId),
}))
: [
{
disabled: true,
label: t('settingAgent.devicePolicy.noPublicDevice'),
value: 'status:empty',
},
];
// Only the caller's own private devices reach this list, so the group is
// simply absent for anyone without one.
const privateOptions: SelectOption<string>[] = [
...privateWorkspaceDevices.map((device) => ({
disabled: !isConfirmed || !!privateDeviceBlockedReason,
label: renderDeviceLabel(device),
title: device.friendlyName || device.hostname || device.deviceId,
value: executionTargetValue('device', device.deviceId),
})),
...(privateDeviceBlockedReason
? [
{
disabled: true,
label: privateDeviceBlockedReason,
value: 'status:privateUnavailable',
},
]
: []),
];
return [
...sharedTargets,
...(!error && !isLoading && privateWorkspaceDevices.length > 0
? [
{
label: t('chat:heteroAgent.executionTarget.personalGroup'),
options: privateOptions,
},
]
: []),
...(!error
? [
{
label: t('chat:heteroAgent.executionTarget.workspaceGroup'),
options: workspaceOptions,
},
]
: []),
];
}, [
error,
isConfirmed,
isLoading,
isHeterogeneous,
privateDeviceBlockedReason,
privateWorkspaceDevices,
publicWorkspaceDevices,
renderDeviceLabel,
renderTargetLabel,
supportsSandbox,
t,
targetLabels,
]);
const selectedTarget = resolveExecutionTargetSelection({
boundDeviceId: agencyConfig?.boundDeviceId,
configuredTarget: agencyConfig?.executionTarget,
devices: selectableDevices,
isHeterogeneous,
});
const selectedValue = selectedTarget
? executionTargetValue(selectedTarget.target, selectedTarget.deviceId)
: undefined;
const saveAgencyConfig = useCallback(
(patch: Partial<LobeAgentAgencyConfig>) =>
updateAgentConfigById(agentId, { agencyConfig: patch }),
[agentId, updateAgentConfigById],
);
if (!config?.workspaceId) return null;
// Whether members may switch this environment is configured on the Agent's
// Permission page — this card only picks the environment itself.
return (
<WorkspaceAgentPolicyCard
icon={MonitorSmartphone}
title={t('settingAgent.devicePolicy.title')}
>
{showDevicePicker ? (
<Select
classNames={executionTargetSelectClassNames}
optionRender={(option) => option.label}
options={targetOptions}
placeholder={t('settingAgent.devicePolicy.selectTarget')}
popupMatchSelectWidth={true}
value={selectedValue}
labelRender={(option) => {
if (typeof option.value !== 'string') return option.label;
const selection = parseExecutionTargetValue(option.value);
if (!selection) return option.label;
if (selection.target === 'device') {
const device = selectableDevices.find(
(item) => item.deviceId === selection.deviceId,
);
return device ? renderDeviceLabel(device, true) : option.label;
}
if (selection.target === 'local') return option.label;
return renderTargetLabel(selection.target, true);
}}
onChange={(value) => {
if (typeof value !== 'string') return;
const selection = parseExecutionTargetValue(value);
if (!selection) return;
void saveAgencyConfig({
...(selection.deviceId ? { boundDeviceId: selection.deviceId } : {}),
executionTarget: selection.target,
});
}}
/>
) : null}
{error ? (
<AsyncError error={error} variant={'inline'} onRetry={() => void mutate()} />
) : null}
</WorkspaceAgentPolicyCard>
);
},
);
WorkspaceAgentDevicePolicy.displayName = 'WorkspaceAgentDevicePolicy';
export default WorkspaceAgentDevicePolicy;