Open History to see the last commit
1import {
2 type OrchestrationV2Actor,
3 type OrchestrationV2CreationSource,
4 ScheduledTaskId,
5} from "@t3tools/contracts";
6
7const LEGACY_AUTOMATION_PREFIX = /^\[Triggered by schedule task: [^\r\n]+\]\r?\n\r?\n/;
8const LEGACY_AUTOMATION_MESSAGE_ID = /^scheduled-task-message:(.+):\d+:(?:scheduled|manual)$/;
9
10export function resolveUserMessagePresentation(message: {
11 readonly id?: string;
12 readonly role: string;
13 readonly text: string;
14 readonly createdBy?: OrchestrationV2Actor;
15 readonly creationSource?: OrchestrationV2CreationSource;
16 readonly scheduledTaskId?: ScheduledTaskId;
17}): {
18 readonly text: string;
19 readonly attribution: "automation" | "agent" | "t3code" | null;
20 readonly scheduledTaskId: ScheduledTaskId | undefined;
21} {
22 if (message.role !== "user") {
23 return { text: message.text, attribution: null, scheduledTaskId: undefined };
24 }
25 if (message.scheduledTaskId !== undefined) {
26 return {
27 text: message.text,
28 attribution: "automation",
29 scheduledTaskId: message.scheduledTaskId,
30 };
31 }
32 const legacyPrefix = LEGACY_AUTOMATION_PREFIX.exec(message.text);
33 const legacyTaskId = legacyPrefix
34 ? LEGACY_AUTOMATION_MESSAGE_ID.exec(message.id ?? "")?.[1]
35 : undefined;
36 if (legacyPrefix !== null && (legacyTaskId !== undefined || message.createdBy === "agent")) {
37 return {
38 text: message.text.slice(legacyPrefix[0].length),
39 attribution: "automation",
40 scheduledTaskId: legacyTaskId === undefined ? undefined : ScheduledTaskId.make(legacyTaskId),
41 };
42 }
43 return {
44 text: message.text,
45 attribution:
46 message.createdBy !== "agent"
47 ? null
48 : message.creationSource === "server"
49 ? "t3code"
50 : "agent",
51 scheduledTaskId: undefined,
52 };
53}