Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

userMessage.ts

66 lines2.3 KB

1import {
2 type OrchestrationV2Actor,
3 type OrchestrationV2CreationSource,
4 type OrchestrationV2IdleCompaction,
5 ScheduledTaskId,
6} from "@t3tools/contracts";
7​
8/** The working row while T3 Code's own compaction runs. */
9export const IDLE_COMPACTING_LABELS = {
10 beforeCacheExpires: "Compacting before the cache expires…",
11 onReturn: "Compacting after being idle…",
12} satisfies Record<OrchestrationV2IdleCompaction, string>;
13​
14/** Leads the finished compaction's token counts, in place of "Context compacted". */
15export const IDLE_COMPACTION_TITLES = {
16 beforeCacheExpires: "Context automatically compacted before cache expiry",
17 onReturn: "Context compacted after being idle",
18} satisfies Record<OrchestrationV2IdleCompaction, string>;
19​
20const LEGACY_AUTOMATION_PREFIX = /^\[Triggered by schedule task: [^\r\n]+\]\r?\n\r?\n/;
21const LEGACY_AUTOMATION_MESSAGE_ID = /^scheduled-task-message:(.+):\d+:(?:scheduled|manual)$/;
22​
23export function resolveUserMessagePresentation(message: {
24 readonly id?: string;
25 readonly role: string;
26 readonly text: string;
27 readonly createdBy?: OrchestrationV2Actor;
28 readonly creationSource?: OrchestrationV2CreationSource;
29 readonly scheduledTaskId?: ScheduledTaskId;
30}): {
31 readonly text: string;
32 readonly attribution: "automation" | "agent" | "t3code" | null;
33 readonly scheduledTaskId: ScheduledTaskId | undefined;
34} {
35 if (message.role !== "user") {
36 return { text: message.text, attribution: null, scheduledTaskId: undefined };
37 }
38 if (message.scheduledTaskId !== undefined) {
39 return {
40 text: message.text,
41 attribution: "automation",
42 scheduledTaskId: message.scheduledTaskId,
43 };
44 }
45 const legacyPrefix = LEGACY_AUTOMATION_PREFIX.exec(message.text);
46 const legacyTaskId = legacyPrefix
47 ? LEGACY_AUTOMATION_MESSAGE_ID.exec(message.id ?? "")?.[1]
48 : undefined;
49 if (legacyPrefix !== null && (legacyTaskId !== undefined || message.createdBy === "agent")) {
50 return {
51 text: message.text.slice(legacyPrefix[0].length),
52 attribution: "automation",
53 scheduledTaskId: legacyTaskId === undefined ? undefined : ScheduledTaskId.make(legacyTaskId),
54 };
55 }
56 return {
57 text: message.text,
58 attribution:
59 message.createdBy !== "agent"
60 ? null
61 : message.creationSource === "server"
62 ? "t3code"
63 : "agent",
64 scheduledTaskId: undefined,
65 };