Give every 1:1 its own direct conversation instead of piling into #general

Agents could only post to named public channels - the messaging tool even
excluded direct spaces - so assignments, reports, and questions all crowded
into general. Now ChatService.ensureDirectSpace finds or creates the private
two-member conversation for any agent/employee pair; send_message takes
to="Name" for 1:1 traffic (exactly one of to/channel), read_messages reads a
conversation by teammate name, and task assignments land in the manager's DM
with the assignee. In a direct conversation the counterpart agent wakes on
every message without needing an @mention - same social contract as any
messenger - still bounded by the autonomous-turn budget, and wake task
briefings tell agents to reply with to= in kind. Covered by six new
direct-message contract tests; the assignment-thread test now encodes the
DM routing contract.
This commit is contained in:
ZacharyZhang-NY committed 2026-07-06 22:49:31 -04:00
1 parent f13ef48a3e
commit cab7b01831
6 files changed
+375 -51

No files matched your search

@@ -3,30 +3,39 @@ import type { JsonRecord } from "./provider-http";
export function readMessagesTool(deps: AgentToolDeps, ctx: AgentToolContext, args: JsonRecord): string {
const channel = optionalString(args.channel);
const withTeammate = optionalString(args.with);
const limit = Math.min(Math.max(Number(args.limit) || 10, 1), 30);
if (channel && withTeammate) throw new Error('Pass either "channel" or "with", not both.');
if (withTeammate) {
const counterpart = deps.db.prepare(`
select id, name, 'agent' as kind from agents where org_id = ? and lower(name) = lower(?) and lifecycle = 'active'
union all
select id, display_name as name, 'employee' as kind from employees where org_id = ? and lower(display_name) = lower(?)
limit 1
`).get(ctx.orgId, withTeammate, ctx.orgId, withTeammate) as { id: string; name: string; kind: "agent" | "employee" } | undefined;
if (!counterpart) throw new Error(`No teammate named "${withTeammate}".`);
const spaceId = deps.chat.ensureDirectSpace(
ctx.orgId,
{ type: "agent", id: ctx.agent.id },
{ type: counterpart.kind, id: counterpart.id },
);
const rows = recentMessagesIn(deps, ctx.orgId, spaceId, limit);
if (rows.length === 0) return `No messages yet in your direct conversation with ${counterpart.name}.`;
return rows.map((m) => `[${m.sender_name}] (thread=${m.thread_id}): ${m.body_md}`).join("\n");
}
if (channel) {
const space = deps.db.prepare(
"select id from chat_spaces where org_id = ? and lower(name) = lower(?) and space_type <> 'direct' limit 1",
).get(ctx.orgId, channel) as { id: string } | undefined;
if (!space) throw new Error(`No channel named "${channel}".`);
const rows = deps.db.prepare(`
select m.body_md,
coalesce(a.name, e.display_name, m.sender_type) as sender_name,
m.thread_id
from chat_messages m
left join agents a on a.id = m.sender_id and m.sender_type = 'agent'
left join employees e on e.id = m.sender_id and m.sender_type = 'employee'
where m.space_id = ? and m.org_id = ? and m.deleted_at is null
order by m.created_at desc
limit ?
`).all(space.id, ctx.orgId, limit) as Array<{ body_md: string; sender_name: string; thread_id: string }>;
const rows = recentMessagesIn(deps, ctx.orgId, space.id, limit);
if (rows.length === 0) return `No recent messages in #${channel}.`;
return rows.reverse().map((m) => `[${m.sender_name}] (thread=${m.thread_id}): ${m.body_md}`).join("\n");
return rows.map((m) => `[${m.sender_name}] (thread=${m.thread_id}): ${m.body_md}`).join("\n");
}
const rows = deps.db.prepare(`
select m.body_md,
coalesce(a.name, e.display_name, m.sender_type) as sender_name,
m.thread_id, s.name as channel_name
m.thread_id, s.name as channel_name, s.space_type
from chat_mentions cm
join chat_messages m on m.id = cm.message_id
join chat_spaces s on s.id = m.space_id
@@ -35,9 +44,32 @@ export function readMessagesTool(deps: AgentToolDeps, ctx: AgentToolContext, arg
where cm.target_type = 'agent' and cm.target_id = ? and m.org_id = ? and m.deleted_at is null
order by m.created_at desc
limit ?
`).all(ctx.agent.id, ctx.orgId, limit) as Array<{ body_md: string; sender_name: string; thread_id: string; channel_name: string }>;
`).all(ctx.agent.id, ctx.orgId, limit) as Array<{ body_md: string; sender_name: string; thread_id: string; channel_name: string; space_type: string }>;
if (rows.length === 0) return "No recent mentions.";
return rows.reverse().map((m) => `[${m.sender_name} in #${m.channel_name}] (thread=${m.thread_id}): ${m.body_md}`).join("\n");
return rows.reverse().map((m) => {
const where = m.space_type === "direct" ? "in your direct conversation" : `in #${m.channel_name}`;
return `[${m.sender_name} ${where}] (thread=${m.thread_id}): ${m.body_md}`;
}).join("\n");
}
function recentMessagesIn(
deps: AgentToolDeps,
orgId: string,
spaceId: string,
limit: number,
): Array<{ body_md: string; sender_name: string; thread_id: string }> {
const rows = deps.db.prepare(`
select m.body_md,
coalesce(a.name, e.display_name, m.sender_type) as sender_name,
m.thread_id
from chat_messages m
left join agents a on a.id = m.sender_id and m.sender_type = 'agent'
left join employees e on e.id = m.sender_id and m.sender_type = 'employee'
where m.space_id = ? and m.org_id = ? and m.deleted_at is null
order by m.created_at desc
limit ?
`).all(spaceId, orgId, limit) as Array<{ body_md: string; sender_name: string; thread_id: string }>;
return rows.reverse();
}
function optionalString(value: unknown): string | null {