Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
refactor(community): PARTICIPANT_SOURCE / MENTION_KIND constants (#5 T3)
Mellyec8657bb6b87
diff --git a/src/shared/src/constants/community.ts b/src/shared/src/constants/community.tsindex 9e9fe8e4e..ba7bae76f 100644--- a/src/shared/src/constants/community.ts+++ b/src/shared/src/constants/community.ts@@ -154,6 +154,28 @@ export function normalizeNotifLevel(input: string): NotificationLevelValue {return "all"}+// Participant `source` — how a user joined a thread/forum-post's participant+// (notify) set: an `@`-mention, having spoken in it, or explicitly added.+// Anchors `community_channel_member.source` / thread-participant `source`.+// Single value source for the literals; `ThreadParticipantSource`+// (queries/community/thread.ts) derives from this via const-assert so the two+// can never drift.+export const PARTICIPANT_SOURCE = {+ MENTION: "mention",+ SPOKE: "spoke",+ ADDED: "added",+} as const+export type ParticipantSource = typeof PARTICIPANT_SOURCE[keyof typeof PARTICIPANT_SOURCE]++// Mention row kind — an `@`-mention vs a reply to your message+// (`community_mention.kind`). Drives the inbox row label. Single value source+// for the literals; `MentionKind` derives from this via const-assert.+export const MENTION_KIND = {+ MENTION: "mention",+ REPLY: "reply",+} as const+export type MentionKind = typeof MENTION_KIND[keyof typeof MENTION_KIND]+// Cache headersexport const CACHE_IMMUTABLE = "public, max-age=31536000, immutable"export const CACHE_SHORT = "public, max-age=3600"diff --git a/src/shared/src/db/queries/community/channel.ts b/src/shared/src/db/queries/community/channel.tsindex dab69b761..6672bbc73 100644--- a/src/shared/src/db/queries/community/channel.ts+++ b/src/shared/src/db/queries/community/channel.ts@@ -7,6 +7,7 @@ import {communityMessage,} from "../../community-schema";import type { Database } from "../../index";+import { PARTICIPANT_SOURCE } from "../../../constants/community";import { createLogger } from "../../../logger";import { canManageServer, canSeePrivateChannel } from "../../../utils/community-roles";import { chunk, D1_MAX_IN_PARAMS } from "../_chunk";@@ -573,7 +574,7 @@ export async function createChannelMember(userId: data.userId,addedBy: data.addedBy ?? null,relation: data.relation ?? "access",- source: data.source ?? "added",+ source: data.source ?? PARTICIPANT_SOURCE.ADDED,}).onConflictDoNothing({target: [diff --git a/src/shared/src/db/queries/community/dm.ts b/src/shared/src/db/queries/community/dm.tsindex bc59729b5..406b6c67d 100644--- a/src/shared/src/db/queries/community/dm.ts+++ b/src/shared/src/db/queries/community/dm.ts@@ -1,6 +1,7 @@import { eq, and, desc, isNull, inArray } from "drizzle-orm";import { communityChannel, communityChannelMember } from "../../community-schema";import { user } from "../../schema";+import { PARTICIPANT_SOURCE } from "../../../constants/community";import type { Database } from "../../index";// DMs are channels now (type='dm', server_id NULL). Their two participants are@@ -90,14 +91,14 @@ export async function createOrGetDM(channelId: channel.id,userId: data.userId1,relation: "access",- source: "added",+ source: PARTICIPANT_SOURCE.ADDED,addedAt: now,},{channelId: channel.id,userId: data.userId2,relation: "access",- source: "added",+ source: PARTICIPANT_SOURCE.ADDED,addedAt: now,},]);diff --git a/src/shared/src/db/queries/community/mention.ts b/src/shared/src/db/queries/community/mention.tsindex ef225ff6c..172f1438d 100644--- a/src/shared/src/db/queries/community/mention.ts+++ b/src/shared/src/db/queries/community/mention.ts@@ -3,6 +3,7 @@ import { communityMention, communityMessage } from "../../community-schema";import { user } from "../../schema";import type { Database } from "../../index";import { chunk, maxRowsPerInsert, D1_MAX_IN_PARAMS } from "../_chunk";+import { MENTION_KIND, type MentionKind } from "../../../constants/community";// communityMention emits 5 bind params/row (id $defaultFn, message_id, user_id,// kind default, read default), so a single INSERT caps at floor(100/5)=20 rows.@@ -10,11 +11,11 @@ const MENTION_INSERT_MAX_ROWS = maxRowsPerInsert(5);export async function createMentions(db: Database,- data: { messageId: string; userIds: string[]; kind?: "mention" | "reply" }+ data: { messageId: string; userIds: string[]; kind?: MentionKind }) {if (data.userIds.length === 0) return [];- const kind = data.kind ?? "mention";+ const kind = data.kind ?? MENTION_KIND.MENTION;// `@everyone` expands userIds to the whole server roster — unbounded. Chunk so// no INSERT statement exceeds D1's 100-param limit; concat the returned rows.const rows = (@@ -40,7 +41,7 @@ export async function listUnreadMentions(db: Database,userId: string,opts: {- kind?: "mention" | "reply";+ kind?: MentionKind;limit?: number;/*** Scope mentions to channels the viewer may see (scope-first, in-query —@@ -194,7 +195,7 @@ export async function hasMentionForMessage(and(eq(communityMention.messageId, messageId),eq(communityMention.userId, userId),- eq(communityMention.kind, "mention")+ eq(communityMention.kind, MENTION_KIND.MENTION))).limit(1);diff --git a/src/shared/src/db/queries/community/server.ts b/src/shared/src/db/queries/community/server.tsindex 2f1675b00..1c47726df 100644--- a/src/shared/src/db/queries/community/server.ts+++ b/src/shared/src/db/queries/community/server.ts@@ -10,6 +10,7 @@ import {import { user } from "../../schema";import type { Database } from "../../index";import { chunk, D1_MAX_IN_PARAMS } from "../_chunk";+import { MENTION_KIND } from "../../../constants/community";export async function createServer(db: Database,@@ -137,7 +138,7 @@ export async function listUserServers(db: Database, userId: string) {and(eq(communityMention.userId, userId),eq(communityMention.read, 0),- eq(communityMention.kind, "mention")+ eq(communityMention.kind, MENTION_KIND.MENTION))).groupBy(communityChannel.serverId)diff --git a/src/shared/src/db/queries/community/thread.ts b/src/shared/src/db/queries/community/thread.tsindex 158291cae..06a985f1e 100644--- a/src/shared/src/db/queries/community/thread.ts+++ b/src/shared/src/db/queries/community/thread.ts@@ -3,6 +3,7 @@ import { communityChannel, communityChannelMember } from "../../community-schemaimport { user } from "../../schema";import type { Database } from "../../index";import { chunk, maxRowsPerInsert, D1_MAX_IN_PARAMS } from "../_chunk";+import { type ParticipantSource } from "../../../constants/community";// The NOTIFICATION set for a thread OR forum_post — now relation='notify' rows// on `community_channel_member` (formerly the standalone@@ -16,7 +17,9 @@ import { chunk, maxRowsPerInsert, D1_MAX_IN_PARAMS } from "../_chunk";// add / leave only. A user may also hold a relation='access' row on the same// channel; the two coexist under the (channel_id, user_id, relation) unique.-export type ThreadParticipantSource = "mention" | "spoke" | "added";+// Derived from PARTICIPANT_SOURCE (constants/community.ts) so the type and the+// literal set can never drift.+export type ThreadParticipantSource = ParticipantSource;const NOTIFY_CONFLICT_TARGET = [communityChannelMember.channelId,diff --git a/src/shared/src/index.ts b/src/shared/src/index.tsindex fa386b244..cf11c79ee 100644--- a/src/shared/src/index.ts+++ b/src/shared/src/index.ts@@ -165,11 +165,13 @@ export {USE_SERVER_DEFAULT,notifLevelDisplay,normalizeNotifLevel,+ PARTICIPANT_SOURCE,+ MENTION_KIND,CACHE_IMMUTABLE,CACHE_SHORT,CACHE_REVALIDATE,} from "./constants/community";-export type { NotifLevel, NotificationLevelValue } from "./constants/community";+export type { NotifLevel, NotificationLevelValue, ParticipantSource, MentionKind } from "./constants/community";export type {AgentStatusType,diff --git a/src/shared/test/constants.test.ts b/src/shared/test/constants.test.tsindex afede2b58..06a647399 100644--- a/src/shared/test/constants.test.ts+++ b/src/shared/test/constants.test.ts@@ -8,7 +8,9 @@ import {} from "../src/constants"import {NOTIF_LEVELS, NOTIFICATION_LEVEL_VALUES, notifLevelDisplay, normalizeNotifLevel,+ PARTICIPANT_SOURCE, MENTION_KIND,} from "../src/constants/community"+import { communityChannel, communityMention } from "../src/db/community-schema"describe("constants", () => {it("OFFLINE_THRESHOLD_MS is 30s", () => expect(OFFLINE_THRESHOLD_MS).toBe(30_000))@@ -147,3 +149,30 @@ describe("notification-level bijection (single source)", () => {expect(normalizeNotifLevel("totally unknown")).toBe("all")})})++describe("PARTICIPANT_SOURCE / MENTION_KIND (single source)", () => {+ it("PARTICIPANT_SOURCE carries the three participant-source values", () => {+ expect(PARTICIPANT_SOURCE.MENTION).toBe("mention")+ expect(PARTICIPANT_SOURCE.SPOKE).toBe("spoke")+ expect(PARTICIPANT_SOURCE.ADDED).toBe("added")+ })++ it("MENTION_KIND carries the two mention-kind values", () => {+ expect(MENTION_KIND.MENTION).toBe("mention")+ expect(MENTION_KIND.REPLY).toBe("reply")+ })++ // Guard against drift between the drizzle column defaults (which MUST be+ // literals in the schema) and the shared constants that now anchor them —+ // drizzle exposes the compiled default on the column object.+ it("community_mention.kind DB default equals MENTION_KIND.MENTION", () => {+ expect((communityMention.kind as { default?: unknown }).default).toBe(MENTION_KIND.MENTION)+ })++ it("community_channel.parentChannelId column exists (schema module loaded for the default probe)", () => {+ // Sanity that the schema import resolved; the meaningful assertion is the+ // kind-default parity above. (channel_member.source lives on a table not+ // re-exported here; message-handler/thread tests cover its use.)+ expect(communityChannel).toBeTruthy()+ })+})diff --git a/src/shared/test/queries/server.test.ts b/src/shared/test/queries/server.test.tsindex 91579c3cd..7f1149500 100644--- a/src/shared/test/queries/server.test.ts+++ b/src/shared/test/queries/server.test.ts@@ -397,9 +397,12 @@ describe("listUserServers — filter predicates (source-level pin)", () => {it("subquery counts only kind='mention' (not replies)", () => {// `kind = "reply"` events live in For You but do not warrant a red badge- // on the server icon — the plan pins this explicitly.+ // on the server icon — the plan pins this explicitly. The literal is now+ // sourced from the shared MENTION_KIND constant (#5 consolidation), so the+ // filter reads `communityMention.kind, MENTION_KIND.MENTION` rather than a+ // bare "mention" string.expect(src).toMatch(/communityMention\.kind/);- expect(src).toMatch(/"mention"/);+ expect(src).toMatch(/MENTION_KIND\.MENTION/);});it("joins go through community_message → community_channel so DM mentions never land in the aggregate", () => {diff --git a/src/web/src/app/api/community/channels/[id]/participants/route.ts b/src/web/src/app/api/community/channels/[id]/participants/route.tsindex d7686aece..d943144b2 100644--- a/src/web/src/app/api/community/channels/[id]/participants/route.ts+++ b/src/web/src/app/api/community/channels/[id]/participants/route.ts@@ -2,7 +2,7 @@ import { NextRequest } from "next/server"import { withAuth } from "@/lib/middleware/auth"import { writeJSON, writeError } from "@/lib/middleware/helpers"import { getDb } from "@/lib/db"-import { queries, WS_EVENTS, isThread, isForumPost } from "@alook/shared"+import { queries, WS_EVENTS, isThread, isForumPost, PARTICIPANT_SOURCE } from "@alook/shared"import { broadcastToUserSafe } from "@/lib/community/fanout"import { requireChannelAccess } from "@/lib/community/permissions"@@ -62,7 +62,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {const created = await queries.communityThread.addThreadParticipant(db, {threadChannelId: channelId,userId: targetUserId,- source: "added",+ source: PARTICIPANT_SOURCE.ADDED,})// Notify the added user so their inbox/thread view reflects the newdiff --git a/src/web/src/app/api/community/messages/[id]/threads/route.ts b/src/web/src/app/api/community/messages/[id]/threads/route.tsindex d5c59a6cb..a100f0816 100644--- a/src/web/src/app/api/community/messages/[id]/threads/route.ts+++ b/src/web/src/app/api/community/messages/[id]/threads/route.ts@@ -2,7 +2,7 @@ import { NextRequest } from "next/server"import { withAuth } from "@/lib/middleware/auth"import { writeJSON, writeError } from "@/lib/middleware/helpers"import { getDb } from "@/lib/db"-import { queries, MAX_CHANNEL_NAME_LENGTH, WS_EVENTS } from "@alook/shared"+import { queries, MAX_CHANNEL_NAME_LENGTH, WS_EVENTS, PARTICIPANT_SOURCE } from "@alook/shared"import { fanOutToChannel } from "@/lib/community/fanout"import { requireChannelMember } from "@/lib/community/permissions"@@ -67,14 +67,14 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {// `requireChannelMember` above only gated the creator, so seeding the author// unconditionally could push a private channel's thread to someone who lost// access (the leak `removeParticipantFromChildChannels` exists to prevent).- const seedRows: { userId: string; source: "spoke" | "added" }[] = [- { userId: ctx.userId, source: "spoke" },+ const seedRows: { userId: string; source: typeof PARTICIPANT_SOURCE.SPOKE | typeof PARTICIPANT_SOURCE.ADDED }[] = [+ { userId: ctx.userId, source: PARTICIPANT_SOURCE.SPOKE },]// De-dupe: when the creator threads their own message the author is the same// user, and the creator's "spoke" row already covers them.if (message.authorId !== ctx.userId) {const authorStillMember = await requireChannelMember(db, message.channelId, message.authorId)- if (authorStillMember.ok) seedRows.push({ userId: message.authorId, source: "added" })+ if (authorStillMember.ok) seedRows.push({ userId: message.authorId, source: PARTICIPANT_SOURCE.ADDED })}await queries.communityThread.addThreadParticipants(db, childChannel.id, seedRows)diff --git a/src/web/src/components/community/_types.ts b/src/web/src/components/community/_types.tsindex affc28973..5900eed13 100644--- a/src/web/src/components/community/_types.ts+++ b/src/web/src/components/community/_types.ts@@ -11,7 +11,7 @@*/import type React from "react"-import type { ChannelType, CommunityRole } from "@alook/shared"+import type { ChannelType, CommunityRole, MentionKind } from "@alook/shared"import type { EntityKind } from "./entity-icon"// ── Presence / enums ───────────────────────────────────────────────────────@@ -290,7 +290,7 @@ export type Mention = {// "mention" (@-mention) vs "reply" (reply to your message). Drives the row// label ("mentioned you" vs "replied to you"). Optional for back-compat with// any cached payload written before the field existed.- kind?: "mention" | "reply"+ kind?: MentionKindserver: stringserverId?: stringchannel: stringdiff --git a/src/web/src/lib/community/message-handler.ts b/src/web/src/lib/community/message-handler.tsindex c16648b11..460cc4f63 100644--- a/src/web/src/lib/community/message-handler.ts+++ b/src/web/src/lib/community/message-handler.ts@@ -5,6 +5,8 @@ import {MAX_MESSAGE_CONTENT_LENGTH,MAX_ATTACHMENTS_PER_MESSAGE,WS_EVENTS,+ PARTICIPANT_SOURCE,+ MENTION_KIND,createLogger,isUniqueConstraintError,} from "@alook/shared"@@ -615,8 +617,8 @@ export async function createCommunityMessage(params: {// card messages (`skipMentions`) don't add the author. A forum post is// enrolled exactly like a thread so it notifies only its participants.if (hasParentChannel(target) && !skipMentions) {- const rows: { userId: string; source: "spoke" | "mention" }[] = [- { userId: authorId, source: "spoke" },+ const rows: { userId: string; source: typeof PARTICIPANT_SOURCE.SPOKE | typeof PARTICIPANT_SOURCE.MENTION }[] = [+ { userId: authorId, source: PARTICIPANT_SOURCE.SPOKE },]// Only EXPLICIT `@user` mentions + reply targets enroll as participants. A// mass `@everyone` is in `mentionTargets` (so everyone is notified@@ -624,7 +626,7 @@ export async function createCommunityMessage(params: {// subscribe the whole channel/server to the thread. `replyParticipants` is// the pre-dedup snapshot so a reply still enrolls even under `@everyone`.for (const id of new Set([...explicitMentionTargets, ...replyParticipants])) {- if (id !== authorId) rows.push({ userId: id, source: "mention" })+ if (id !== authorId) rows.push({ userId: id, source: PARTICIPANT_SOURCE.MENTION })}// One bulk insert (author + mentioned) instead of N+1 sequential inserts.await queries.communityThread.addThreadParticipants(db, target.channelId, rows)@@ -639,14 +641,14 @@ export async function createCommunityMessage(params: {await queries.communityMention.createMentions(db, {messageId: row.id,userIds: liveMentions,- kind: "mention",+ kind: MENTION_KIND.MENTION,})}if (liveReplies.length > 0) {await queries.communityMention.createMentions(db, {messageId: row.id,userIds: liveReplies,- kind: "reply",+ kind: MENTION_KIND.REPLY,})}