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

textGenerationUtils.ts

112 lines3.1 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 return withoutTrailingPeriod.slice(0, 72).trimEnd();
38​
39export function sanitizePrTitle(raw: string): string {
40 const singleLine = raw.trim().split(/\r?\n/g)[0]?.trim() ?? "";
41 if (singleLine.length > 0) {
42 return singleLine;
43 }
44 return "Update project changes";
46​
47const MAX_THREAD_TITLE_CHARS = 120;
48​
49export function sanitizeThreadTitle(raw: string): string {
50 // Unwrap a JSON-formatted title before truncation can cut off the closing brace.
51 const decoded = decodeJsonThreadTitle(raw);
52 const title = Option.isSome(decoded) ? decoded.value.title : raw;
53 const normalized = title
54 .trim()
55 .split(/\r?\n/g)[0]
56 ?.trim()
57 .replace(/^['"`]+|['"`]+$/g, "")
58 .trim()
59 .replace(/\s+/g, " ");
60​
61 if (!normalized || normalized.trim().length === 0) {
62 return "New thread";
63 }
64​
65 if (normalized.length <= MAX_THREAD_TITLE_CHARS) {
66 return normalized;
67 }
68​
69 return `${normalized.slice(0, MAX_THREAD_TITLE_CHARS - 3).trimEnd()}...`;
71​
72function cliLabel(cliName: string): string {
73 const capitalized = cliName.charAt(0).toUpperCase() + cliName.slice(1);
74 return `${capitalized} CLI (\`${cliName}\`)`;
76​
77export function normalizeCliError(
78 cliName: string,
79 operation: string,
80 error: unknown,
81 fallback: string,
82): TextGenerationError {
83 if (isTextGenerationError(error)) {
84 return error;
85 }
86​
87 if (error instanceof Error) {
88 const lower = error.message.toLowerCase();
89 if (
90 error.message.includes(`Command not found: ${cliName}`) ||
91 lower.includes(`spawn ${cliName}`) ||
92 lower.includes("enoent")
93 ) {
94 return new TextGenerationError({
95 operation,
96 detail: `${cliLabel(cliName)} is required but not available on PATH.`,
97 cause: error,
98 });
99 }
100 return new TextGenerationError({
101 operation,
102 detail: fallback,
103 cause: error,
104 });
105 }
106​
107 return new TextGenerationError({
108 operation,
109 detail: fallback,
110 cause: error,
111 });