Q5: give chat wakes timed, attributed history apart from the new ask
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fe76f0bc52
commit
46dd77ddda
5 files changed
+168
-39
No files matched your search
@@ -0,0 +1,54 @@
|
||||
import type { DatabaseSync } from "node:sqlite";
|
||||
import type { ChatContextBlock } from "./task-prompts";
|
||||
|
||||
interface ThreadMessageRow {
|
||||
body_md: string;
|
||||
created_at: string;
|
||||
edited_at: string | null;
|
||||
sender_type: string;
|
||||
sender_id: string | null;
|
||||
sender_name: string;
|
||||
sender_role: string | null;
|
||||
}
|
||||
|
||||
/** The thread a chat-wake run answers, split at the agent's last reply. */
|
||||
export function loadChatWakeContext(db: DatabaseSync, orgId: string, agentId: string, secretScopeJson: string): ChatContextBlock | null {
|
||||
const scope = JSON.parse(secretScopeJson) as { sourceThreadId?: string };
|
||||
if (!scope.sourceThreadId) return null;
|
||||
const thread = db.prepare("select space_id from chat_threads where id = ? and org_id = ?")
|
||||
.get(scope.sourceThreadId, orgId) as { space_id: string } | undefined;
|
||||
if (!thread) throw new Error(`Chat wake thread ${scope.sourceThreadId} no longer exists.`);
|
||||
const messages = db.prepare(`
|
||||
select m.body_md, m.created_at, m.edited_at, m.sender_type, m.sender_id,
|
||||
coalesce(a.name, e.display_name, m.sender_type) as sender_name,
|
||||
coalesce(a.role, e.role) as sender_role
|
||||
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.thread_id = ? and m.org_id = ?
|
||||
order by m.created_at asc
|
||||
limit 20
|
||||
`).all(scope.sourceThreadId, orgId) as unknown as ThreadMessageRow[];
|
||||
if (messages.length === 0) return null;
|
||||
const isOwn = (m: ThreadMessageRow) => m.sender_type === "agent" && m.sender_id === agentId;
|
||||
const ownReplies = messages.filter(isOwn);
|
||||
const lastReplyAt = ownReplies.length > 0 ? ownReplies[ownReplies.length - 1].created_at : "";
|
||||
const isFresh = (m: ThreadMessageRow) =>
|
||||
!isOwn(m) && (m.created_at > lastReplyAt || (m.edited_at !== null && m.edited_at > lastReplyAt));
|
||||
return {
|
||||
threadId: scope.sourceThreadId,
|
||||
spaceId: thread.space_id,
|
||||
history: messages.filter((m) => !isFresh(m)).map(formatMessage).join("\n"),
|
||||
fresh: messages.filter(isFresh).map(formatMessage).join("\n"),
|
||||
};
|
||||
}
|
||||
|
||||
function formatMessage(m: ThreadMessageRow): string {
|
||||
const who = m.sender_role ? `${m.sender_name} (${m.sender_type}, ${m.sender_role})` : `${m.sender_name} (${m.sender_type})`;
|
||||
const edited = m.edited_at ? ` (edited ${utcMinute(m.edited_at)})` : "";
|
||||
return `[${utcMinute(m.created_at)}] ${who}${edited}: ${m.body_md}`;
|
||||
}
|
||||
|
||||
function utcMinute(iso: string): string {
|
||||
return `${iso.slice(0, 10)} ${iso.slice(11, 16)} UTC`;
|
||||
}
|
||||
@@ -14,7 +14,6 @@ import type { AgentToolContext } from "./agent-tools";
|
||||
import {
|
||||
buildAgentInstructions,
|
||||
buildTaskPrompt,
|
||||
type ChatContextBlock,
|
||||
type GoalRow,
|
||||
type ProjectRow,
|
||||
type SkillRuntimeSnapshotRow,
|
||||
@@ -25,6 +24,7 @@ import {
|
||||
type WorkspaceRow,
|
||||
} from "./task-prompts";
|
||||
import { renderTaskHandoff } from "./agent-task-handoff";
|
||||
import { loadChatWakeContext } from "./chat-wake-context";
|
||||
import { loadComposioTools } from "./composio/composio-tool-bridge";
|
||||
import { connectorDisplayName } from "./composio/connector-brands";
|
||||
import { ConnectorGrantsService } from "./composio/connector-grants";
|
||||
@@ -134,7 +134,7 @@ export class TaskExecutionService {
|
||||
let skillRuntimeSnapshot: SkillRuntimeSnapshotRow | null = null;
|
||||
try {
|
||||
skillRuntimeSnapshot = this.resolveSkillRuntimeSnapshot(orgId, agent.id, runId);
|
||||
const chatContext = this.loadChatContext(orgId, isolation);
|
||||
const chatContext = loadChatWakeContext(this.db, orgId, agent.id, isolation.secretScopeJson);
|
||||
const toolContext: AgentToolContext = {
|
||||
orgId,
|
||||
agent: { id: agent.id, modelProviderId: agent.model_provider_id, modelId: agent.model_id },
|
||||
@@ -523,39 +523,6 @@ export class TaskExecutionService {
|
||||
return runId;
|
||||
}
|
||||
|
||||
private loadChatContext(orgId: string, isolation: TaskRunIsolation): ChatContextBlock | null {
|
||||
let scope: { sourceMessageId?: string; sourceThreadId?: string };
|
||||
try {
|
||||
scope = JSON.parse(isolation.secretScopeJson);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (!scope.sourceThreadId) return null;
|
||||
const thread = this.db.prepare("select space_id from chat_threads where id = ? and org_id = ?")
|
||||
.get(scope.sourceThreadId, orgId) as { space_id: string } | undefined;
|
||||
if (!thread) return null;
|
||||
const messages = this.db.prepare(`
|
||||
select m.body_md, m.sender_type, m.sender_id,
|
||||
coalesce(a.name, e.display_name, m.sender_type) as sender_name
|
||||
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.thread_id = ? and m.org_id = ?
|
||||
order by m.created_at asc
|
||||
limit 20
|
||||
`).all(scope.sourceThreadId, orgId) as Array<{ body_md: string; sender_type: string; sender_name: string }>;
|
||||
if (messages.length === 0) return null;
|
||||
// Human principals are marked as the founder: an agent replying to its
|
||||
// boss must know it is not talking to another agent.
|
||||
const label = (m: { sender_type: string; sender_name: string }) =>
|
||||
m.sender_type === "employee" || m.sender_type === "board" ? `${m.sender_name} (founder)` : m.sender_name;
|
||||
return {
|
||||
threadId: scope.sourceThreadId,
|
||||
spaceId: thread.space_id,
|
||||
transcript: messages.map((m) => `[${label(m)}]: ${m.body_md}`).join("\n"),
|
||||
};
|
||||
}
|
||||
|
||||
private completedRunCount(taskId: string, excludingRunId: string): number {
|
||||
const row = this.db.prepare("select count(*) as n from task_runs where task_id = ? and state = 'completed' and id <> ?")
|
||||
.get(taskId, excludingRunId) as { n: number };
|
||||
|
||||
@@ -71,7 +71,10 @@ export interface ChatContextBlock {
|
||||
threadId: string;
|
||||
/** The space the thread lives in — where the run's reply must land. */
|
||||
spaceId: string;
|
||||
transcript: string;
|
||||
/** Messages up to and including the agent's last reply. */
|
||||
history: string;
|
||||
/** Messages after it: what this run answers. */
|
||||
fresh: string;
|
||||
}
|
||||
|
||||
export interface AgentInstructionContext {
|
||||
@@ -211,8 +214,11 @@ export function buildTaskPrompt(input: {
|
||||
if (input.chatContext) {
|
||||
lines.push(
|
||||
"",
|
||||
"The conversation in this thread so far:",
|
||||
input.chatContext.transcript,
|
||||
"Earlier in this thread (context only — newer messages override it):",
|
||||
input.chatContext.history || "(nothing before)",
|
||||
"",
|
||||
"New since your last reply in this thread — this is what you are answering:",
|
||||
input.chatContext.fresh || "(nothing new since your last reply)",
|
||||
"",
|
||||
`You have been brought into this thread. Reply to the people here in your own voice — answer what they asked, give your honest take, push back if you disagree, or hand work over. Stay in the thread by passing thread_id="${input.chatContext.threadId}" to send_message. This is a conversation between colleagues, not a status report; only change a task's status if the work itself actually moved.`,
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user