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

TextGenerationUtils.ts

103 lines3.1 KB

1import { TextGenerationError } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3​
4const isTextGenerationError = Schema.is(TextGenerationError);
5​
6/** Convert an Effect Schema to JSON Schema, including referenced `$defs` when present. */
7export function toJsonSchemaObject(schema: Schema.Top): unknown {
8 const document = Schema.toJsonSchemaDocument(schema);
9 if (document.definitions && Object.keys(document.definitions).length > 0) {
10 return { ...document.schema, $defs: document.definitions };
11 }
12 return document.schema;
14​
15/** Truncate a text section to `maxChars`, appending a `[truncated]` marker when needed. */
16export function limitSection(value: string, maxChars: number): string {
17 if (value.length <= maxChars) return value;
18 const truncated = value.slice(0, maxChars);
19 return `${truncated}\n\n[truncated]`;
21​
22/** Normalise a raw commit subject to ≤72 chars with no trailing period. */
23export function sanitizeCommitSubject(raw: string): string {
24 const singleLine = raw.trim().split(/\r?\n/g)[0]?.trim() ?? "";
25 const withoutTrailingPeriod = singleLine.replace(/[.]+$/g, "").trim();
26 if (withoutTrailingPeriod.length === 0) {
27 return "Update project files";
28 }
29​
30 if (withoutTrailingPeriod.length <= 72) {
31 return withoutTrailingPeriod;
32 }
33 return withoutTrailingPeriod.slice(0, 72).trimEnd();
35​
36/** Normalise a raw PR title to a single line with a sensible fallback. */
37export function sanitizePrTitle(raw: string): string {
38 const singleLine = raw.trim().split(/\r?\n/g)[0]?.trim() ?? "";
39 if (singleLine.length > 0) {
40 return singleLine;
41 }
42 return "Update project changes";
44​
45/** Normalise a raw thread title to a compact single-line sidebar-safe label. */
46export function sanitizeThreadTitle(raw: string): string {
47 const normalized = raw
48 .trim()
49 .split(/\r?\n/g)[0]
50 ?.trim()
51 .replace(/^['"`]+|['"`]+$/g, "")
52 .trim()
53 .replace(/\s+/g, " ");
54​
55 if (!normalized || normalized.trim().length === 0) {
56 return "New thread";
57 }
58​
59 if (normalized.length <= 50) {
60 return normalized;
61 }
62​
63 return `${normalized.slice(0, 47).trimEnd()}...`;
65​
66/** CLI name to human-readable label, e.g. "codex" → "Codex CLI (`codex`)" */
67function cliLabel(cliName: string): string {
68 const capitalized = cliName.charAt(0).toUpperCase() + cliName.slice(1);
69 return `${capitalized} CLI (\`${cliName}\`)`;
71​
72/** Preserve typed errors and hide CLI failure details behind the public fallback. */
73export function normalizeCliError(
74 cliName: string,
75 operation: string,
76 error: unknown,
77 fallback: string,
78): TextGenerationError {
79 if (isTextGenerationError(error)) {
80 return error;
81 }
82​
83 if (error instanceof Error) {
84 const lower = error.message.toLowerCase();
85 if (
86 error.message.includes(`Command not found: ${cliName}`) ||
87 lower.includes(`spawn ${cliName}`) ||
88 lower.includes("enoent")
89 ) {
90 return new TextGenerationError({
91 operation,
92 detail: `${cliLabel(cliName)} is required but not available on PATH.`,
93 cause: error,
94 });
95 }
96 }
97​
98 return new TextGenerationError({
99 operation,
100 detail: fallback,
101 cause: error,
102 });