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

TextGenerationUtils.ts

112 lines3.4 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 a flat JSON Schema object, inlining `$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 imperative-mood, ≤72 chars, 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/**
73 * Normalize an unknown error from a CLI text generation process into a
74 * typed `TextGenerationError`. Parameterized by CLI name so both Codex
75 * and Claude (and future providers) can share the same logic.
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}: ${error.message}`,
103 cause: error,
104 });
105 }
106​
107 return new TextGenerationError({
108 operation,
109 detail: fallback,
110 cause: error,
111 });