π€― 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 { WorkingDirEntry } from '@lobechat/types';
import { sql } from 'drizzle-orm';
import { index, jsonb, pgTable, text, uniqueIndex, uuid, varchar } from 'drizzle-orm/pg-core';
import { timestamps, timestamptz } from './_helpers';
import { users } from './user';
import { workspaces } from './workspace';
/**
* Stable device identity anchor β one row per physical machine per user.
*
* `deviceId` is derived from a machine-level identifier
* (`sha256(machineUUID + userId + salt)`), so it survives LobeHub reinstalls
* and desktop upgrades. Online status is NOT stored here β it belongs to the
* device gateway's live connection state; this table only records "ever seen"
* so offline devices stay visible and bindable.
*/
export const devices = pgTable(
'devices',
{
id: uuid('id').defaultRandom().primaryKey().notNull(),
userId: text('user_id')
.references(() => users.id, { onDelete: 'cascade' })
.notNull(),
// `workspace_id` distinguishes the two kinds of device row:
// - NULL β a PERSONAL device, identified by (userId, deviceId).
// - <workspaceId> β a device ENROLLED into that workspace (shared infra),
// identified by (workspaceId, deviceId). `userId` then only records the
// first enroller β it is NOT part of the identity, so two members
// enrolling the same machine resolve to ONE row (see the partial unique
// below) and the original enroller is preserved. The router uses this
// `userId` to gate writes to "self-or-owner" (see
// `canEditWorkspaceDevice`). The same physical machine produces a
// distinct `deviceId` per principal (the hash mixes in userId /
// `workspace:<id>`), so personal and workspace rows never collide.
workspaceId: text('workspace_id').references(() => workspaces.id, { onDelete: 'cascade' }),
// Visibility WITHIN a workspace (same contract as agents/documents/files):
// - 'public' β every workspace member sees and can use the device.
// - 'private' β only the enrolling member (`userId`) sees it; other
// members' device lists / pickers / agent runs never surface it.
// Defaults to 'private' β exposing a machine to the whole workspace is an
// explicit opt-in (rows that predate the column were backfilled 'public'
// to preserve their then-current everyone-visible behaviour). Ignored for
// personal rows (`workspace_id IS NULL` β implicitly private to their
// owner). Filtered via `buildWorkspaceWhere`.
visibility: text('visibility', { enum: ['private', 'public'] })
.default('private')
.notNull(),
// Workspace rows enrolled remotely from the owner's personal device list
// ("share this device to a workspace") record the PERSONAL deviceId they
// were shared from. deviceIds are one-way hashes of (machine, principal),
// so without this link the server cannot correlate a personal row with its
// workspace twins. NULL for personal rows and for devices enrolled directly
// on the machine (`lh connect --workspace`).
sharedFromDeviceId: varchar('shared_from_device_id', { length: 64 }),
/**
* Machine-derived id (sha256 truncated to 32 chars; 64 leaves room for
* fallback randomUUID). For a managed cloud device it is the id the
* managed device provider assigned.
*/
deviceId: varchar('device_id', { length: 64 }).notNull(),
/**
* 'machine-id' | 'fallback' β reported by the connecting client, validated
* by zod at the router boundary β or `MANAGED_CLOUD_DEVICE_IDENTITY_SOURCE`
* from `@lobechat/types`, which the server writes when it creates a managed
* cloud device. The client registration routers do not accept that value.
*/
identitySource: varchar('identity_source', { length: 20 }).notNull(),
hostname: text('hostname'),
/** 'darwin' | 'win32' | 'linux' */
platform: varchar('platform', { length: 20 }),
/**
* CPU architecture as reported by the client (`process.arch`: 'x64' | 'arm64',
* stored raw β no normalization β so future values like linux arm keep working).
* Only a device running a client new enough to report it gets a value;
* historical rows stay NULL (arch is not derivable from stored data).
*/
architecture: varchar('architecture', { length: 20 }),
metadata: jsonb('metadata').$type<Record<string, string>>(),
/** User-editable alias */
friendlyName: text('friendly_name'),
defaultCwd: text('default_cwd'),
/** @deprecated superseded by `workingDirs` (structured). Kept as a legacy column; no longer read/written. */
recentCwds: text('recent_cwds').array().default([]).notNull(),
workingDirs: jsonb('working_dirs').$type<WorkingDirEntry[]>().default([]),
firstSeenAt: timestamptz('first_seen_at').defaultNow().notNull(),
lastSeenAt: timestamptz('last_seen_at').defaultNow().notNull(),
...timestamps,
},
(t) => [
/**
* One row per (user, machine) for PERSONAL devices; register() upserts on
* this target (partial β ON CONFLICT must repeat the
* `WHERE workspace_id IS NULL` predicate). Workspace rows are excluded so
* `user_id` is not part of their identity (see workspace partial below).
*/
uniqueIndex('devices_user_id_device_id_unique')
.on(t.userId, t.deviceId)
.where(sql`${t.workspaceId} IS NULL`),
/**
* One row per (workspace, machine) for enrolled devices, regardless of which
* member ran the enrollment. registerWorkspaceDevice() upserts on this target
* (partial β ON CONFLICT must repeat the `WHERE workspace_id IS NOT NULL`
* predicate).
*/
uniqueIndex('devices_workspace_id_device_id_unique')
.on(t.workspaceId, t.deviceId)
.where(sql`${t.workspaceId} IS NOT NULL`),
index('devices_user_id_idx').on(t.userId),
index('devices_workspace_id_idx').on(t.workspaceId),
index('devices_workspace_visibility_idx').on(t.workspaceId, t.visibility, t.userId),
],
);
export type DeviceItem = typeof devices.$inferSelect;
export type NewDevice = typeof devices.$inferInsert;