The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { |
| 2 | type ChatAttachment, |
| 3 | type ChatFileAttachment, |
| 4 | type GuestMessageAttribution, |
| 5 | type OrchestrationMessageContext, |
| 6 | type OrchestrationV2Actor, |
| 7 | type OrchestrationV2CreationSource, |
| 8 | type OrchestrationV2IdleCompaction, |
| 9 | type OrchestrationV2UserMessageInputIntent, |
| 10 | ScheduledTaskId, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import { |
| 13 | isAttachmentOnlyBootstrapPrompt, |
| 14 | replaceComposerContextReferences, |
| 15 | } from "@t3tools/shared/composerContextReferences"; |
| 16 | import { filePreviewKind } from "@t3tools/shared/filePreview"; |
| 17 | import { imageMimeType } from "@t3tools/shared/image"; |
| 18 | import { videoMimeType } from "@t3tools/shared/video"; |
| 19 | import { msg } from "gt-i18n"; |
| 20 | |
| 21 | /** The working row while T3 Code's own compaction runs. */ |
| 22 | export const IDLE_COMPACTING_LABELS = { |
| 23 | beforeCacheExpires: msg("Compacting before the cache expires…"), |
| 24 | onReturn: msg("Compacting…"), |
| 25 | } satisfies Record<OrchestrationV2IdleCompaction, string>; |
| 26 | |
| 27 | /** Leads the finished compaction's token counts, in place of "Context compacted". */ |
| 28 | export const IDLE_COMPACTION_TITLES = { |
| 29 | beforeCacheExpires: msg("Auto-compacted before cache expiry"), |
| 30 | onReturn: msg("Context compacted before sending"), |
| 31 | } satisfies Record<OrchestrationV2IdleCompaction, string>; |
| 32 | |
| 33 | const LEGACY_AUTOMATION_PREFIX = /^\[Triggered by schedule task: [^\r\n]+\]\r?\n\r?\n/; |
| 34 | const LEGACY_AUTOMATION_MESSAGE_ID = /^scheduled-task-message:(.+):\d+:(?:scheduled|manual)$/; |
| 35 | |
| 36 | export function resolveUserMessageFileCards(message: { |
| 37 | readonly text: string; |
| 38 | readonly attachments: ReadonlyArray<ChatAttachment>; |
| 39 | readonly context?: OrchestrationMessageContext | undefined; |
| 40 | }): ReadonlyArray<ChatFileAttachment> | null { |
| 41 | const files = message.attachments.filter( |
| 42 | (attachment): attachment is ChatFileAttachment => |
| 43 | attachment.type === "file" && |
| 44 | imageMimeType(attachment) === null && |
| 45 | videoMimeType(attachment) === null && |
| 46 | filePreviewKind(attachment) !== "audio", |
| 47 | ); |
| 48 | if (files.length === 0 || files.length !== message.attachments.length) return null; |
| 49 | const fileIds = new Set(files.map((file) => file.id)); |
| 50 | const remaining = replaceComposerContextReferences(message.text, (reference) => { |
| 51 | const record = message.context?.records.find((item) => item.contextId === reference.contextId); |
| 52 | return reference.kind === "file" && |
| 53 | record?.kind === "file" && |
| 54 | "attachmentId" in record && |
| 55 | fileIds.has(record.attachmentId) |
| 56 | ? "" |
| 57 | : reference.label || reference.kind; |
| 58 | }); |
| 59 | return remaining.trim() === "" ? files : null; |
| 60 | } |
| 61 | |
| 62 | export function resolveUserMessagePresentation(message: { |
| 63 | readonly id?: string; |
| 64 | readonly role: string; |
| 65 | readonly text: string; |
| 66 | readonly createdBy?: OrchestrationV2Actor; |
| 67 | readonly creationSource?: OrchestrationV2CreationSource; |
| 68 | readonly scheduledTaskId?: ScheduledTaskId | undefined; |
| 69 | readonly context?: { readonly guest?: GuestMessageAttribution | undefined } | undefined; |
| 70 | }): { |
| 71 | readonly text: string; |
| 72 | readonly attribution: "automation" | "agent" | "t3code" | "guest" | null; |
| 73 | readonly scheduledTaskId: ScheduledTaskId | undefined; |
| 74 | /** Set when `attribution` is "guest". */ |
| 75 | readonly guestName?: string; |
| 76 | } { |
| 77 | if (message.role !== "user") { |
| 78 | return { text: message.text, attribution: null, scheduledTaskId: undefined }; |
| 79 | } |
| 80 | // The stored text names the guest for the agent; people see the guest's own words. |
| 81 | const guest = message.context?.guest; |
| 82 | if (guest !== undefined) { |
| 83 | return { |
| 84 | text: guest.text, |
| 85 | attribution: "guest", |
| 86 | scheduledTaskId: undefined, |
| 87 | guestName: guest.name, |
| 88 | }; |
| 89 | } |
| 90 | if (message.scheduledTaskId !== undefined) { |
| 91 | return { |
| 92 | text: message.text, |
| 93 | attribution: "automation", |
| 94 | scheduledTaskId: message.scheduledTaskId, |
| 95 | }; |
| 96 | } |
| 97 | const legacyPrefix = LEGACY_AUTOMATION_PREFIX.exec(message.text); |
| 98 | const legacyTaskId = legacyPrefix |
| 99 | ? LEGACY_AUTOMATION_MESSAGE_ID.exec(message.id ?? "")?.[1] |
| 100 | : undefined; |
| 101 | if (legacyPrefix !== null && (legacyTaskId !== undefined || message.createdBy === "agent")) { |
| 102 | return { |
| 103 | text: message.text.slice(legacyPrefix[0].length), |
| 104 | attribution: "automation", |
| 105 | scheduledTaskId: legacyTaskId === undefined ? undefined : ScheduledTaskId.make(legacyTaskId), |
| 106 | }; |
| 107 | } |
| 108 | return { |
| 109 | // The agent needs words to start a turn; people sent only the attachments. |
| 110 | text: isAttachmentOnlyBootstrapPrompt(message.text) ? "" : message.text, |
| 111 | attribution: |
| 112 | message.createdBy !== "agent" |
| 113 | ? null |
| 114 | : message.creationSource === "server" |
| 115 | ? "t3code" |
| 116 | : "agent", |
| 117 | scheduledTaskId: undefined, |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | type GroupableUserMessage = Parameters<typeof resolveUserMessagePresentation>[0] & { |
| 122 | readonly inputIntent?: OrchestrationV2UserMessageInputIntent; |
| 123 | }; |
| 124 | |
| 125 | export function resolveUserMessageGrouping( |
| 126 | message: GroupableUserMessage | undefined, |
| 127 | previous: GroupableUserMessage | undefined, |
| 128 | next: GroupableUserMessage | undefined, |
| 129 | ) { |
| 130 | const isOwnMessage = (value: GroupableUserMessage | undefined) => |
| 131 | value?.role === "user" && resolveUserMessagePresentation(value).attribution === null; |
| 132 | const isSteer = (value: GroupableUserMessage | undefined) => |
| 133 | value?.inputIntent === "steer" || value?.inputIntent === "promoted_queued_to_steer"; |
| 134 | const groupedWithPrevious = isOwnMessage(message) && isOwnMessage(previous); |
| 135 | return { |
| 136 | groupedWithPrevious, |
| 137 | groupedWithNext: isOwnMessage(message) && isOwnMessage(next), |
| 138 | continuesSteer: groupedWithPrevious && isSteer(message) && isSteer(previous), |
| 139 | }; |
| 140 | } |