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

userMessage.ts

82 lines3.0 KB

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