π€― 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
import type { HeterogeneousAgentType } from '@lobechat/heterogeneous-agents';
import { getHeterogeneousAgentConfig } from '@lobechat/heterogeneous-agents';
import { ChatErrorType, type ErrorType } from '@lobechat/types';
/**
* Turn a raw device-gateway dispatch error code into a human-readable headline.
* The gateway returns terse machine codes (e.g. `GATEWAY_NOT_CONFIGURED`) which,
* surfaced verbatim, render as a cryptic error card. We rewrite the headline the
* caller keeps for non-web surfaces (IM bots) while retaining the raw code in
* `detail` for diagnostics. Web clients localize via the mapped error type below.
*/
/**
* Exported so a consumer that reads a persisted `task.error` can recognise the same
* failure it would have recognised from the raw code: dispatch failures reach storage
* as a code on one path and as this headline on another.
*/
export const HETERO_DISPATCH_ERROR_HEADLINES: Record<string, string> = {
DEVICE_CHANNEL_UNAVAILABLE:
"The device this agent runs on isn't reachable right now β it went offline, went to sleep, or is reconnecting. Check that the LobeHub desktop app (or the `lh` CLI) is running and connected, then try again.",
DEVICE_GATEWAY_ERROR:
'The device connection service hit an error while starting this run. Nothing started on the device. This is usually temporary β try again in a moment.',
DEVICE_GATEWAY_RATE_LIMITED:
'Too many device requests at once, so this run was turned away. Wait a few seconds and try again.',
DEVICE_GATEWAY_UNAUTHORIZED:
"The device connection service rejected this run's credentials. This is a server configuration problem β retrying won't help.",
DEVICE_GATEWAY_UNREACHABLE:
"Couldn't reach the device connection service, so this run never started. Check the network, then try again.",
DEVICE_NOT_FOUND:
'The device this agent is bound to is no longer registered with the connection service. Reconnect the device, or bind this agent to another online device.',
DEVICE_OFFLINE:
"The device this agent runs on is offline, so the run couldn't start. Check that the LobeHub desktop app (or the `lh` CLI) is running and connected, then try again.",
DEVICE_RESPONSE_TIMEOUT:
"The device didn't answer in time. It may be offline or asleep, or it may still be starting this run. Check that the LobeHub desktop app (or the `lh` CLI) is connected and whether this run is already in progress before starting it again.",
GATEWAY_NOT_CONFIGURED:
"The run device gateway isn't configured on the server, so this agent can't reach a device to run on. Configure the device gateway, or switch this agent to a connected local device.",
};
/**
* The gateway's own codes for a failure the transport already names. A run
* dispatch answers 504 `{"error":"TIMEOUT"}` when the device sent no
* `agent_run_ack` in time β the same 504 the transport calls
* `DEVICE_RESPONSE_TIMEOUT`. Most often the socket was already dead (the
* gateway only drops it after a missed heartbeat) and nothing ran, but a slow
* spawn or a lost ack means the run may have started; the copy covers both.
* Do not reclassify it from a later look at the device: a device that is
* offline now may still have received the run before dropping (or run it when
* it wakes), so only a gateway refusal *before* sending proves nothing ran.
* Keeping the body verbatim (so `detail` stays diagnostic) meant the code
* missed every lookup and the raw JSON became the headline.
*/
const GATEWAY_CODE_ALIASES: Record<string, string> = {
TIMEOUT: 'DEVICE_RESPONSE_TIMEOUT',
};
/**
* The gateway may answer with a bare code (`DEVICE_OFFLINE`) or with a code the
* device-gateway client annotated with the status it came from
* (`DEVICE_CHANNEL_UNAVAILABLE (HTTP 503)`). Look the headline up by the code
* itself so the annotation doesn't cost the user a readable message. Non-2xx
* responses can also carry a JSON envelope whose `error` field holds the code.
* `detail` keeps the full raw string for diagnostics in either format.
*/
const toDispatchErrorCode = (raw?: string): string | undefined => {
let text = raw?.trim();
if (text?.startsWith('{')) {
try {
const body: unknown = JSON.parse(text);
if (body && typeof body === 'object' && 'error' in body && typeof body.error === 'string') {
text = body.error.trim();
}
} catch {
// Preserve malformed gateway responses as diagnostic text.
}
}
const code = text?.match(/^([A-Z][\dA-Z_]*)/)?.[1];
return (code && GATEWAY_CODE_ALIASES[code]) || code;
};
/**
* The device a failed dispatch was aimed at, recorded on the operation's error so
* a later reader can ask whether *that* device is back β the routing that picked
* it (topic override, member binding, personal vs workspace pool) is not
* something a consumer should re-derive.
*/
export interface DeviceDispatchRoute {
deviceId: string;
/** The principal whose device pool the gateway was asked to route through. */
userId: string;
/** Set when the device belongs to a workspace pool rather than the personal one. */
workspaceId?: string;
}
export const readDeviceDispatchRoute = (error: unknown): DeviceDispatchRoute | undefined => {
const route = (error as { deviceRoute?: Partial<DeviceDispatchRoute> } | null | undefined)
?.deviceRoute;
if (typeof route?.deviceId !== 'string' || typeof route.userId !== 'string') return undefined;
return {
deviceId: route.deviceId,
userId: route.userId,
...(typeof route.workspaceId === 'string' ? { workspaceId: route.workspaceId } : {}),
};
};
/**
* Whether a dispatch failed only because the device's ack never arrived. Unlike
* every other dispatch failure this does not prove nothing ran β see
* {@link GATEWAY_CODE_ALIASES}.
*/
export const isDeviceResponseTimeout = (raw?: string): boolean =>
toDispatchErrorCode(raw) === 'DEVICE_RESPONSE_TIMEOUT';
export const humanizeHeteroDispatchError = (raw?: string): string => {
const code = toDispatchErrorCode(raw);
return (code && HETERO_DISPATCH_ERROR_HEADLINES[code]) || raw || 'Device dispatch failed';
};
/**
* Map a raw dispatch code to a dedicated `ChatErrorType` so the web client renders
* its own localized headline (the generic `ServerAgentRuntimeError` copy would
* otherwise mask the specific message). Unknown codes keep the generic type.
*/
const HETERO_DISPATCH_ERROR_TYPES: Record<string, ErrorType> = {
// Every "we could not reach a device" flavour shares the localized
// `DeviceGatewayNotConfigured` copy β the user-facing action ("connect a
// device") is the same whether the gateway is unconfigured, the device is
// offline, or its registration is gone.
DEVICE_CHANNEL_UNAVAILABLE: ChatErrorType.DeviceGatewayNotConfigured,
DEVICE_NOT_FOUND: ChatErrorType.DeviceGatewayNotConfigured,
DEVICE_OFFLINE: ChatErrorType.DeviceGatewayNotConfigured,
// Not an unreachable device: the run was delivered and may be running, so
// "connect a device, then try again" would invite a duplicate run.
DEVICE_RESPONSE_TIMEOUT: ChatErrorType.DeviceResponseTimeout,
GATEWAY_NOT_CONFIGURED: ChatErrorType.DeviceGatewayNotConfigured,
};
export const resolveHeteroDispatchErrorType = (raw?: string): ErrorType => {
const code = toDispatchErrorCode(raw);
return (code && HETERO_DISPATCH_ERROR_TYPES[code]) || ChatErrorType.ServerAgentRuntimeError;
};
export const supportsCloudHeterogeneousSandbox = (type: HeterogeneousAgentType): boolean =>
type === 'claude-code' || type === 'codex';
export const getHeterogeneousAgentTitle = (type: HeterogeneousAgentType): string =>
getHeterogeneousAgentConfig(type)?.title ?? type;