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): expose a running reply's topic to the member who runs it (#23112)

Arvin Xuc8b3c10b6100
diff --git a/packages/database/src/repositories/channel/__tests__/jobTopic.test.ts b/packages/database/src/repositories/channel/__tests__/jobTopic.test.ts
new file mode 100644
index 00000000000..2c678186bdc
--- /dev/null
+++ b/packages/database/src/repositories/channel/__tests__/jobTopic.test.ts
@@ -0,0 +1,56 @@
+// @vitest-environment node
+import { eq } from 'drizzle-orm';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { getTestDB } from '../../../core/getTestDB';
+import { topics } from '../../../schemas';
+import { ChannelExecutionRepository } from '..';
+import { createChannelFixture, jobsOf, lease } from './testUtils';
+
+const db = await getTestDB();
+const fixture = createChannelFixture(db, 'channel-job-topic');
+const execution = new ChannelExecutionRepository(db);
+beforeEach(fixture.setup);
+afterEach(fixture.cleanup);
+
+/** A member asks the Agent, and the reply is still running in the member's own topic. */
+const runningReply = async () => {
+ const team = await fixture.createTeamChannel();
+ const agent = await fixture.addAgent(team.channel.id, 'Agent');
+ const request = await team.asMember.send(team.channel.id, {
+ content: 'Question',
+ mentions: [agent.id],
+ requestKey: 'question',
+ routing: 'fallback',
+ });
+ const [job] = await jobsOf(db, request.id);
+ const claimed = (await execution.claim(team.channel.id, job.id, lease))!;
+ await execution.accepted(team.channel.id, job.id);
+ const jobTopic = async (repo: typeof team.asOwner) =>
+ (await repo.detail(team.channel.id)).jobs.find((entry) => entry.id === job.id)!.topicId;
+ return { ...team, jobTopic, topicId: claimed.topic.id };
+};
+
+describe('Channel job topic', () => {
+ it('lets the member running a reply follow it into its topic', async () => {
+ const state = await runningReply();
+
+ expect(await state.jobTopic(state.asMember)).toBe(state.topicId);
+ });
+
+ it('hides the topic from other members, admins included', async () => {
+ const state = await runningReply();
+
+ expect(await state.jobTopic(state.asOwner)).toBeUndefined();
+ });
+
+ it('drops the link once the topic is trashed', async () => {
+ const state = await runningReply();
+ await db
+ .update(topics)
+ .set({ deletedAt: new Date(), isDeleted: true })
+ .where(eq(topics.id, state.topicId));
+
+ expect(await state.jobTopic(state.asMember)).toBeUndefined();
+ });
+});
diff --git a/packages/database/src/repositories/channel/index.ts b/packages/database/src/repositories/channel/index.ts
index b58043f2879..134f68e05ed 100644
--- a/packages/database/src/repositories/channel/index.ts
+++ b/packages/database/src/repositories/channel/index.ts
@@ -399,10 +399,13 @@ export class ChannelRepository {
return {
channel: { ...access.channel, canDelete: admin },
...view,
- // Mirrors `stop`: the requester or an admin.
- jobs: view.jobs.map(({ requesterMemberId, ...job }) => ({
+ jobs: view.jobs.map(({ requesterMemberId, topicId, topicOwnerId, ...job }) => ({
...job,
+ // Mirrors `stop`: the requester or an admin.
canStop: admin || requesterMemberId === access.member.id,
+ // Only the viewer who runs the reply can follow it into its topic; for anyone else
+ // the job carries no topic at all.
+ ...(topicId && topicOwnerId === this.userId ? { topicId } : {}),
})),
};
},
diff --git a/packages/database/src/repositories/channel/view.ts b/packages/database/src/repositories/channel/view.ts
index 5b06ddd828c..bba75c906ff 100644
--- a/packages/database/src/repositories/channel/view.ts
+++ b/packages/database/src/repositories/channel/view.ts
@@ -186,7 +186,11 @@ const threadSummaries = async (tx: Transaction, channelId: string, threadIds: st
);
};
-/** A job as the public view shows it; `requesterMemberId` is dropped by the repository. */
+/**
+ * A job as the public view shows it. The repository drops `requesterMemberId`, and keeps
+ * `topicId` only for the viewer who owns that topic (`topicOwnerId`), as replies keep
+ * `sourceTopicId`: the topic belongs to the requester, so other members never learn its id.
+ */
const publicJobColumns = {
id: channelJobs.id,
memberId: channelJobs.memberId,
@@ -209,6 +213,10 @@ const publicJobColumns = {
${channelJobs.executionConfig}->'agencyConfig'->>'boundDeviceId'
)`,
writerHeld: channelJobs.writerHeld,
+ topicId: channelJobs.topicId,
+ // A trashed topic has no owner here, so its link goes with it.
+ topicOwnerId: sql<string | null>`CASE WHEN ${notTrashed(topics.isDeleted)}
+ THEN ${topics.userId} ELSE NULL END`,
worktreeBranch: sql<string | null>`${channelJobs.executionConfig}->>'worktreeBranch'`,
operationStatus: agentOperations.status,
createdAt: channelJobs.createdAt,
@@ -223,6 +231,7 @@ const readJobs = (tx: Transaction, where: SQL | undefined, limit: number) =>
.select(publicJobColumns)
.from(channelJobs)
.leftJoin(agentOperations, eq(agentOperations.id, channelJobs.operationId))
+ .leftJoin(topics, eq(topics.id, channelJobs.topicId))
.where(where)
.orderBy(desc(channelJobs.createdAt))
.limit(limit);
diff --git a/packages/types/src/channel.ts b/packages/types/src/channel.ts
index 3fcf7f852b6..fafc7778b03 100644
--- a/packages/types/src/channel.ts
+++ b/packages/types/src/channel.ts
@@ -576,6 +576,11 @@ export interface ChannelPublicJob {
publicationRevoked: boolean;
status: ChannelJobStatus;
threadId: string | null;
+ /**
+ * The Agent topic the reply runs in, present only for the viewer who owns it (as
+ * `sourceTopicId` on replies), so they can follow a reply still at work.
+ */
+ topicId?: string;
/** Git branch of the worktree the run works in, instead of the member's own directory. */
worktreeBranch?: string | null;
writerHeld: boolean;