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

userMessage.ts

80 lines2.8 KB

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