🤯 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 { ChannelJobWake } from '@lobechat/types';
import { and, eq, inArray, sql } from 'drizzle-orm';
import type {
ChannelMemberItem,
ChannelMessageItem,
ChannelThreadItem,
NewChannelMessage,
} from '../../schemas';
import {
channelJobs,
channelMembers,
channelMessages,
channels,
channelThreads,
} from '../../schemas';
import type { Transaction } from '../../type';
import { notTrashed } from '../../utils/softDelete';
import { ChannelError } from './error';
import { unhideForActivity } from './sections';
/** Why a deferred `followup` has not started; see {@link deliver} and `claimJob`. */
export const AWAITING_PRIMARY_REASON = 'Waiting for the first responder to finish';
/** An Agent member that is not muted; a muted one is reached only by an @mention. */
export const memberNotMuted = () =>
sql`coalesce((${channelMembers.config}->>'muted')::boolean, false) = false`;
/** The Thread a message belongs to, or null for the main channel. */
export const findThread = async (
tx: Transaction,
channelId: string,
threadId?: string | null,
): Promise<ChannelThreadItem | null> => {
if (!threadId) return null;
const [thread] = await tx
.select()
.from(channelThreads)
.where(
and(
eq(channelThreads.channelId, channelId),
eq(channelThreads.id, threadId),
notTrashed(channelThreads.isDeleted),
),
);
if (!thread) throw new ChannelError('NOT_FOUND', 'Thread not found');
return thread;
};
/** Rejects a write into an archived Thread; it stays readable until an admin unarchives it. */
export const assertThreadOpen = (thread: ChannelThreadItem | null) => {
if (thread?.status === 'archived') throw new ChannelError('CONFLICT', 'Thread is archived');
return thread;
};
/** Active members by id. Throws when any of them is missing or has left. */
export const activeMembers = async (
tx: Transaction,
channelId: string,
memberIds: string[],
): Promise<ChannelMemberItem[]> => {
const unique = [...new Set(memberIds)];
if (!unique.length) return [];
const members = await tx
.select()
.from(channelMembers)
.where(
and(
eq(channelMembers.channelId, channelId),
eq(channelMembers.status, 'active'),
inArray(channelMembers.id, unique),
),
);
if (members.length !== unique.length)
throw new ChannelError('BAD_REQUEST', 'A selected member is unavailable');
return members;
};
/**
* Agents that may answer a message without @mentions: every active Agent on the main
* channel, the following Agents inside a Thread.
*/
export const candidateAgents = async (
tx: Transaction,
channelId: string,
thread: ChannelThreadItem | null,
): Promise<ChannelMemberItem[]> => {
const agents = await tx
.select()
.from(channelMembers)
.where(
and(
eq(channelMembers.channelId, channelId),
eq(channelMembers.actorType, 'agent'),
eq(channelMembers.status, 'active'),
memberNotMuted(),
),
);
return thread ? agents.filter((agent) => thread.followerMemberIds.includes(agent.id)) : agents;
};
/**
* The only Agent of a group with exactly one active person and one active Agent. Such a
* group needs no responder decision: every message the person sends goes to this Agent.
*/
export const pairedAgent = async (
tx: Transaction,
channelId: string,
): Promise<ChannelMemberItem | undefined> => {
const members = await tx
.select()
.from(channelMembers)
.where(and(eq(channelMembers.channelId, channelId), eq(channelMembers.status, 'active')));
const agents = members.filter((member) => member.actorType === 'agent');
// A muted one waits for an @mention like any other.
return agents.length === 1 && members.length === 2 && !agents[0].config?.muted
? agents[0]
: undefined;
};
/**
* Queues one job per message and Agent member. Skips Agents whose
* environment changed after the message, so a late route cannot run an old instruction
* in a new directory. Returns the ids of the Agents that received a job.
*
* `wakes` records why each member was woken; a member without one owes an answer (`mention`).
* A wake deferred behind the message's primary responder (`afterMemberId`) is queued as
* `awaiting_primary` from the start, so it never reads as a reply in progress before it can run.
*/
export const deliver = async (
tx: Transaction,
message: ChannelMessageItem,
agents: ChannelMemberItem[],
requesterMemberId: string,
wakes?: Readonly<Record<string, ChannelJobWake>>,
): Promise<string[]> => {
const recipients = agents.filter((agent) => agent.actorType === 'agent');
if (!recipients.length) return [];
await tx
.insert(channelJobs)
.values(
recipients.map((agent) => ({
channelId: message.channelId,
memberId: agent.id,
messageId: message.id,
requesterMemberId,
threadId: message.threadId,
wake: wakes?.[agent.id] ?? null,
blockedCode: wakes?.[agent.id]?.afterMemberId ? ('awaiting_primary' as const) : null,
blockedReason: wakes?.[agent.id]?.afterMemberId ? AWAITING_PRIMARY_REASON : null,
})),
)
.onConflictDoNothing();
return recipients.map((agent) => agent.id);
};
/** Reserves the channel's next message sequence. The caller holds the channel lock. */
export const takeSequence = async (tx: Transaction, channelId: string): Promise<number> => {
const [{ sequence }] = await tx
.update(channels)
.set({ sequence: sql`${channels.sequence} + 1` })
.where(eq(channels.id, channelId))
.returning({ sequence: channels.sequence });
return sequence;
};
/** Appends a message at the next channel sequence. The caller holds the channel lock. */
export const appendMessage = async (
tx: Transaction,
values: Omit<NewChannelMessage, 'sequence' | 'id'> & { id?: string },
): Promise<ChannelMessageItem> => {
// A reply is an Agent's; a request is a person's, or an Agent's in a conversation between Agents.
const isReply = values.routingStatus === 'reply';
if (
isReply
? !values.agentId || values.userId != null
: Boolean(values.userId) === Boolean(values.agentId)
)
throw new ChannelError(
'BAD_REQUEST',
'A message requires exactly one sender of the correct type',
);
const sequence = await takeSequence(tx, values.channelId);
const [message] = await tx
.insert(channelMessages)
.values({ ...values, sequence })
.returning();
await unhideForActivity(tx, values.channelId);
return message;
};