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

textGenerationUtils.ts

120 lines3.4 KB

1import { TextGenerationError } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3import * as Schema from "effect/Schema";
4​
5const isTextGenerationError = Schema.is(TextGenerationError);
6const decodeJsonThreadTitle = Schema.decodeOption(
7 Schema.fromJsonString(Schema.Struct({ title: Schema.String })),
8);
9​
10export function toJsonSchemaObject(schema: Schema.Top): unknown {
11 const document = Schema.toJsonSchemaDocument(Schema.toType(schema), {
12 onExcessProperty: "error",
13 });
14 if (document.definitions && Object.keys(document.definitions).length > 0) {
15 return { ...document.schema, $defs: document.definitions };
16 }
17 return document.schema;
19​
20export function limitSection(value: string, maxChars: number): string {
21 if (value.length <= maxChars) return value;
22 const truncated = value.slice(0, maxChars);
23 return `${truncated}\n\n[truncated]`;
25​
26export function sanitizeCommitSubject(raw: string): string {
27 const singleLine = raw.trim().split(/\r?\n/g)[0]?.trim() ?? "";
28 const withoutTrailingPeriod = singleLine.replace(/[.]+$/g, "").trim();
29 if (withoutTrailingPeriod.length === 0) {
30 return "Update project files";
31 }
32​
33 if (withoutTrailingPeriod.length <= 72) {
34 return withoutTrailingPeriod;
35 }
36 const segments = new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(
37 withoutTrailingPeriod,
38 );
39 let subject = "";
40 for (const { segment } of segments) {
41 if (subject.length + segment.length > 72) break;
42 subject += segment;
43 }
44 return subject.trimEnd() || "Update project files";
46​
47export function sanitizePrTitle(raw: string): string {
48 const singleLine = raw.trim().split(/\r?\n/g)[0]?.trim() ?? "";
49 if (singleLine.length > 0) {
50 return singleLine;
51 }
52 return "Update project changes";
54​
55const MAX_THREAD_TITLE_CHARS = 120;
56​
57export function sanitizeThreadTitle(raw: string): string {
58 // Unwrap a JSON-formatted title before truncation can cut off the closing brace.
59 const decoded = decodeJsonThreadTitle(raw);
60 const title = Option.isSome(decoded) ? decoded.value.title : raw;
61 const normalized = title
62 .trim()
63 .split(/\r?\n/g)[0]
64 ?.trim()
65 .replace(/^['"`]+|['"`]+$/g, "")
66 .trim()
67 .replace(/\s+/g, " ");
68​
69 if (!normalized || normalized.trim().length === 0) {
70 return "New thread";
71 }
72​
73 if (normalized.length <= MAX_THREAD_TITLE_CHARS) {
74 return normalized;
75 }
76​
77 return `${normalized.slice(0, MAX_THREAD_TITLE_CHARS - 3).trimEnd()}...`;
79​
80function cliLabel(cliName: string): string {
81 const capitalized = cliName.charAt(0).toUpperCase() + cliName.slice(1);
82 return `${capitalized} CLI (\`${cliName}\`)`;
84​
85export function normalizeCliError(
86 cliName: string,
87 operation: string,
88 error: unknown,
89 fallback: string,
90): TextGenerationError {
91 if (isTextGenerationError(error)) {
92 return error;
93 }
94​
95 if (error instanceof Error) {
96 const lower = error.message.toLowerCase();
97 if (
98 error.message.includes(`Command not found: ${cliName}`) ||
99 lower.includes(`spawn ${cliName}`) ||
100 lower.includes("enoent")
101 ) {
102 return new TextGenerationError({
103 operation,
104 detail: `${cliLabel(cliName)} is required but not available on PATH.`,
105 cause: error,
106 });
107 }
108 return new TextGenerationError({
109 operation,
110 detail: fallback,
111 cause: error,
112 });
113 }
114​
115 return new TextGenerationError({
116 operation,
117 detail: fallback,
118 cause: error,
119 });