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(channel): show every member where an Agent runs in the Channel (#23279)

Arvin Xu555da1460628
diff --git a/packages/database/src/repositories/channel/__tests__/viewMemberEnvironment.test.ts b/packages/database/src/repositories/channel/__tests__/viewMemberEnvironment.test.ts
new file mode 100644
index 00000000000..d6ae1da53d8
--- /dev/null
+++ b/packages/database/src/repositories/channel/__tests__/viewMemberEnvironment.test.ts
@@ -0,0 +1,73 @@
+// @vitest-environment node
+import { eq } from 'drizzle-orm';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { getTestDB } from '../../../core/getTestDB';
+import { channelMembers, devices } from '../../../schemas';
+import { createChannelFixture } from './testUtils';
+
+const db = await getTestDB();
+const fixture = createChannelFixture(db, 'channel-view-environment');
+beforeEach(fixture.setup);
+afterEach(fixture.cleanup);
+
+describe('Channel member environment', () => {
+ it('shows every member where an Agent runs, named by its device', async () => {
+ const { asMember, channel } = await fixture.createTeamChannel();
+ const agent = await fixture.addAgent(channel.id, 'Coder');
+ await db
+ .update(channelMembers)
+ .set({ config: { deviceId: 'device-1', workingDirectory: '/code/lobot' } })
+ .where(eq(channelMembers.id, agent.id));
+ await db
+ .update(devices)
+ .set({ friendlyName: 'Studio Mac', hostname: 'studio.local' })
+ .where(eq(devices.deviceId, 'device-1'));
+
+ const { members } = await asMember.detail(channel.id);
+ expect(members.find((m) => m.id === agent.id)?.environment).toEqual({
+ deviceId: 'device-1',
+ deviceName: 'Studio Mac',
+ workingDirectory: '/code/lobot',
+ });
+ expect(members.find((m) => !m.agentId)?.environment).toBeNull();
+ });
+
+ it("never names another principal's device that shares the id", async () => {
+ const { asMember, channel } = await fixture.createTeamChannel();
+ const agent = await fixture.addAgent(channel.id, 'Coder');
+ await db.insert(devices).values([
+ {
+ deviceId: 'device-shared-id',
+ friendlyName: 'Someone else',
+ identitySource: 'fallback',
+ userId: fixture.memberId,
+ },
+ ]);
+ await db
+ .update(channelMembers)
+ .set({ config: { deviceId: 'device-shared-id', deviceOwnerId: fixture.ownerId } })
+ .where(eq(channelMembers.id, agent.id));
+
+ const hidden = (await asMember.detail(channel.id)).members.find((m) => m.id === agent.id);
+ expect(hidden?.environment?.deviceName).toBeNull();
+
+ await db.insert(devices).values({
+ deviceId: 'device-shared-id',
+ friendlyName: 'Owner laptop',
+ identitySource: 'fallback',
+ userId: fixture.ownerId,
+ });
+ const own = (await asMember.detail(channel.id)).members.find((m) => m.id === agent.id);
+ expect(own?.environment?.deviceName).toBe('Owner laptop');
+ });
+
+ it('has no environment when the Channel sets none', async () => {
+ const { asMember, channel } = await fixture.createTeamChannel();
+ const agent = await fixture.addAgent(channel.id, 'Coder');
+ await db.update(channelMembers).set({ config: {} }).where(eq(channelMembers.id, agent.id));
+
+ const { members } = await asMember.detail(channel.id);
+ expect(members.find((m) => m.id === agent.id)?.environment).toBeNull();
+ });
+});
diff --git a/packages/database/src/repositories/channel/view.ts b/packages/database/src/repositories/channel/view.ts
index 113e737e3aa..8fbd86bab8f 100644
--- a/packages/database/src/repositories/channel/view.ts
+++ b/packages/database/src/repositories/channel/view.ts
@@ -21,7 +21,9 @@ import {
channelJobs,
channelMembers,
channelMessages,
+ channels,
channelThreads,
+ devices,
files as fileTable,
topics,
users,
@@ -261,6 +263,73 @@ const readJobs = (tx: Transaction, where: SQL | undefined, limit: number) =>
.orderBy(desc(channelJobs.createdAt))
.limit(limit);
+interface MemberDevice {
+ agentId: string | null;
+ deviceId: string | null;
+ deviceOwnerId: string | null;
+}
+
+/**
+ * Display names for the devices Agent members run on, keyed by member id. A device id is unique
+ * only within its principal, so each one resolves the way execution does: the Channel's workspace
+ * device first, else the personal device of the member who owns it.
+ */
+const memberDeviceNames = async (tx: Transaction, channelId: string, members: MemberDevice[]) => {
+ const configured = members.filter(
+ (member): member is MemberDevice & { deviceId: string } =>
+ !!member.agentId && !!member.deviceId,
+ );
+ const names = new Map<MemberDevice, string>();
+ if (!configured.length) return names;
+
+ const [channel] = await tx
+ .select({ workspaceId: channels.workspaceId })
+ .from(channels)
+ .where(eq(channels.id, channelId));
+ const owners = [
+ ...new Set(
+ configured.flatMap((member) => (member.deviceOwnerId ? [member.deviceOwnerId] : [])),
+ ),
+ ];
+ const scopes = [
+ channel?.workspaceId ? eq(devices.workspaceId, channel.workspaceId) : undefined,
+ owners.length ? and(isNull(devices.workspaceId), inArray(devices.userId, owners)) : undefined,
+ ].filter((scope): scope is SQL => !!scope);
+ if (!scopes.length) return names;
+
+ const rows = await tx
+ .select({
+ deviceId: devices.deviceId,
+ friendlyName: devices.friendlyName,
+ hostname: devices.hostname,
+ userId: devices.userId,
+ workspaceId: devices.workspaceId,
+ })
+ .from(devices)
+ .where(
+ and(
+ inArray(
+ devices.deviceId,
+ configured.map((member) => member.deviceId),
+ ),
+ or(...scopes),
+ ),
+ );
+ for (const member of configured) {
+ const row =
+ rows.find((entry) => entry.deviceId === member.deviceId && entry.workspaceId) ??
+ rows.find(
+ (entry) =>
+ entry.deviceId === member.deviceId &&
+ !entry.workspaceId &&
+ entry.userId === member.deviceOwnerId,
+ );
+ const name = row?.friendlyName || row?.hostname;
+ if (name) names.set(member, name);
+ }
+ return names;
+};
+
export const readChannelView = async (
tx: Transaction,
channelId: string,
@@ -289,6 +358,9 @@ export const readChannelView = async (
userAvatar: users.avatar,
lastReadSequence: channelMembers.lastReadSequence,
joinedAt: channelMembers.createdAt,
+ deviceId: sql<string | null>`${channelMembers.config}->>'deviceId'`,
+ deviceOwnerId: sql<string | null>`${channelMembers.config}->>'deviceOwnerId'`,
+ workingDirectory: sql<string | null>`${channelMembers.config}->>'workingDirectory'`,
})
.from(channelMembers)
.leftJoin(users, eq(users.id, channelMembers.userId))
@@ -344,6 +416,7 @@ export const readChannelView = async (
THREAD_ACTIVE_JOB_LIMIT,
),
]);
+ const deviceNames = await memberDeviceNames(tx, channelId, members);
const jobs = [...ownJobs, ...threadJobs].sort(
(a, b) => b.createdAt.getTime() - a.createdAt.getTime(),
);
@@ -420,7 +493,16 @@ export const readChannelView = async (
attachments,
hasMore: reversed.length > limit,
jobs,
- members,
+ members: members.map((row) => {
+ const { deviceId, deviceOwnerId: _deviceOwnerId, workingDirectory, ...member } = row;
+ return {
+ ...member,
+ environment:
+ member.agentId && (deviceId || workingDirectory)
+ ? { deviceId, deviceName: deviceNames.get(row) ?? null, workingDirectory }
+ : null,
+ };
+ }),
threads: threads.map((entry) => {
const title = entry.metadata?.title;
const named = typeof title === 'string' && title ? { ...entry, title } : entry;
diff --git a/packages/types/src/channel.ts b/packages/types/src/channel.ts
index 5581829f73f..0d7b4333dd4 100644
--- a/packages/types/src/channel.ts
+++ b/packages/types/src/channel.ts
@@ -627,11 +627,21 @@ export interface ChannelListItem {
workspaceId: string | null;
}
+/** Where an Agent member runs in this Channel, readable by every member. */
+export interface ChannelMemberEnvironmentPreview {
+ deviceId: string | null;
+ /** The device's friendly name, else its hostname; null when the device row is gone. */
+ deviceName: string | null;
+ workingDirectory: string | null;
+}
+
export interface ChannelPublicMember {
actorType: ChannelMemberActorType;
agentId: string | null;
avatar: string | null;
description: string | null;
+ /** Agent members only; null when the Channel sets no device or directory for it. */
+ environment?: ChannelMemberEnvironmentPreview | null;
id: string;
joinedAt: Date;
/** An Agent that answers only when @mentioned; see `ChannelMemberConfig.muted`. */