@terreno/ai 0.18.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,12 @@
1
1
  import {logger} from "@terreno/api";
2
- import type {DataContent, LanguageModel, ModelMessage} from "ai";
3
- import {generateText as aiGenerateText, stepCountIs, streamText} from "ai";
2
+ import type {DataContent, JSONValue, LanguageModel, ModelMessage} from "ai";
3
+ import {
4
+ generateText as aiGenerateText,
5
+ NoObjectGeneratedError,
6
+ Output,
7
+ stepCountIs,
8
+ streamText,
9
+ } from "ai";
4
10
  import type mongoose from "mongoose";
5
11
 
6
12
  import {AIRequest} from "../models/aiRequest";
@@ -8,6 +14,9 @@ import type {
8
14
  AIRequestType,
9
15
  AIServiceOptions,
10
16
  GenerateChatStreamOptions,
17
+ GenerateJsonArrayOptions,
18
+ GenerateJsonObjectOptions,
19
+ GenerateJsonValueOptions,
11
20
  GenerateStreamOptions,
12
21
  GenerateTextOptions,
13
22
  GptHistoryPrompt,
@@ -15,9 +24,11 @@ import type {
15
24
  SummaryOptions,
16
25
  TranslateOptions,
17
26
  } from "../types";
27
+ import {normalizeLlmJsonTextForStructuredOutput} from "./parseAiJson";
18
28
  import {
19
29
  CONTENT_SUMMARY_PROMPT,
20
30
  DEFAULT_GPT_MEMORY,
31
+ JSON_VALUE_SYSTEM_PROMPT,
21
32
  REMIX_PROMPT,
22
33
  TRANSLATION_PROMPT,
23
34
  } from "./prompts";
@@ -31,6 +42,56 @@ export const TemperaturePresets = {
31
42
  MAXIMUM: 2.0,
32
43
  } as const;
33
44
 
45
+ /**
46
+ * Wraps a language model so non-streaming `doGenerate` text parts are normalized via
47
+ * {@link normalizeLlmJsonTextForStructuredOutput} (fences, preamble, balanced slice, light repairs)
48
+ * before Vercel `Output.*` parsing.
49
+ */
50
+ const withStrippedJsonFencesModel = (model: LanguageModel): LanguageModel => {
51
+ if (typeof model === "string") {
52
+ return model;
53
+ }
54
+
55
+ return new Proxy(model, {
56
+ get(target, prop, receiver) {
57
+ if (prop === "doGenerate") {
58
+ const original = Reflect.get(target, prop, receiver);
59
+ if (typeof original !== "function") {
60
+ return original;
61
+ }
62
+
63
+ const boundGenerate = original as (options: unknown) => PromiseLike<{
64
+ content: Array<{text?: string; type: string; [key: string]: unknown}>;
65
+ [key: string]: unknown;
66
+ }>;
67
+
68
+ return async (options: unknown) => {
69
+ const result = await Promise.resolve(boundGenerate.call(target, options));
70
+ if (!result?.content || !Array.isArray(result.content)) {
71
+ return result;
72
+ }
73
+
74
+ return {
75
+ ...result,
76
+ content: result.content.map((part) => {
77
+ if (part.type !== "text" || typeof part.text !== "string") {
78
+ return part;
79
+ }
80
+
81
+ return {
82
+ ...part,
83
+ text: normalizeLlmJsonTextForStructuredOutput(part.text),
84
+ };
85
+ }),
86
+ };
87
+ };
88
+ }
89
+
90
+ return Reflect.get(target, prop, receiver);
91
+ },
92
+ }) as LanguageModel;
93
+ };
94
+
34
95
  const getModelId = (model: LanguageModel): string => {
35
96
  if (typeof model === "string") {
36
97
  return model;
@@ -41,6 +102,7 @@ const getModelId = (model: LanguageModel): string => {
41
102
  export class AIService {
42
103
  readonly model: LanguageModel;
43
104
  readonly defaultTemperature: number;
105
+ private structuredJsonModel?: LanguageModel;
44
106
 
45
107
  constructor({model, defaultTemperature = TemperaturePresets.DEFAULT}: AIServiceOptions) {
46
108
  this.model = model;
@@ -51,9 +113,97 @@ export class AIService {
51
113
  return getModelId(this.model);
52
114
  }
53
115
 
116
+ private getModelForStructuredJson(): LanguageModel {
117
+ if (!this.structuredJsonModel) {
118
+ this.structuredJsonModel = withStrippedJsonFencesModel(this.model);
119
+ }
120
+ return this.structuredJsonModel;
121
+ }
122
+
123
+ private describeStructuredGenerationError(error: unknown): string {
124
+ if (error instanceof Error) {
125
+ const base = `${error.name}: ${error.message}`;
126
+ if (error.cause instanceof Error) {
127
+ return `${base} | cause: ${error.cause.name}: ${error.cause.message}`;
128
+ }
129
+ return base;
130
+ }
131
+ return String(error);
132
+ }
133
+
134
+ private extractRawModelTextFromStructuredError(error: unknown): string | undefined {
135
+ if (NoObjectGeneratedError.isInstance(error) && typeof error.text === "string") {
136
+ return error.text;
137
+ }
138
+ if (error instanceof Error && "text" in error) {
139
+ const t = (error as {text?: unknown}).text;
140
+ if (typeof t === "string") {
141
+ return t;
142
+ }
143
+ }
144
+ return undefined;
145
+ }
146
+
147
+ private extractFinishReasonFromStructuredError(error: unknown): string | undefined {
148
+ if (NoObjectGeneratedError.isInstance(error) && error.finishReason) {
149
+ return error.finishReason;
150
+ }
151
+ return undefined;
152
+ }
153
+
154
+ private async logStructuredJsonFailure(params: {
155
+ error: unknown;
156
+ prompt: string;
157
+ requestType: AIRequestType;
158
+ responseTime: number;
159
+ system: string;
160
+ userId?: mongoose.Types.ObjectId;
161
+ }): Promise<void> {
162
+ const rawText = this.extractRawModelTextFromStructuredError(params.error);
163
+ const responseForLog =
164
+ rawText !== undefined && rawText.length > 0
165
+ ? rawText
166
+ : "(no raw model text captured on this error)";
167
+ const errorDescription = this.describeStructuredGenerationError(params.error);
168
+ const finishReason = this.extractFinishReasonFromStructuredError(params.error);
169
+ const errorStack =
170
+ params.error instanceof Error && typeof params.error.stack === "string"
171
+ ? params.error.stack.length > 8000
172
+ ? `${params.error.stack.slice(0, 8000)}…`
173
+ : params.error.stack
174
+ : undefined;
175
+
176
+ logger.error("AIService structured JSON generation failed", {
177
+ aiModel: getModelId(this.model),
178
+ error: errorDescription,
179
+ finishReason,
180
+ prompt: params.prompt,
181
+ requestType: params.requestType,
182
+ response: rawText ?? "",
183
+ system: params.system,
184
+ });
185
+
186
+ await this.logRequest({
187
+ aiModel: getModelId(this.model),
188
+ error: errorDescription,
189
+ metadata: {
190
+ errorStack,
191
+ finishReason,
192
+ rawModelTextCaptured: Boolean(rawText && rawText.length > 0),
193
+ system: params.system,
194
+ },
195
+ prompt: params.prompt,
196
+ requestType: params.requestType,
197
+ response: responseForLog,
198
+ responseTime: params.responseTime,
199
+ userId: params.userId,
200
+ });
201
+ }
202
+
54
203
  private async logRequest(params: {
55
204
  aiModel: string;
56
205
  error?: string;
206
+ metadata?: Record<string, unknown>;
57
207
  prompt: string;
58
208
  requestType: AIRequestType;
59
209
  response?: string;
@@ -108,6 +258,174 @@ export class AIService {
108
258
  }
109
259
  }
110
260
 
261
+ /** Any JSON value (object, array, primitive, or null) via the AI SDK `Output.json()` parser. */
262
+ async generateJsonValue(options: GenerateJsonValueOptions): Promise<JSONValue> {
263
+ const {
264
+ maxOutputTokens,
265
+ outputDescription,
266
+ outputName,
267
+ prompt,
268
+ systemPrompt,
269
+ temperature,
270
+ userId,
271
+ } = options;
272
+ const startTime = Date.now();
273
+ const system = systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT;
274
+
275
+ try {
276
+ const result = await aiGenerateText({
277
+ experimental_telemetry: {functionId: "generate-json-value", isEnabled: true},
278
+ maxOutputTokens,
279
+ model: this.getModelForStructuredJson(),
280
+ output: Output.json({description: outputDescription, name: outputName}),
281
+ prompt,
282
+ system,
283
+ temperature: temperature ?? TemperaturePresets.DETERMINISTIC,
284
+ });
285
+
286
+ const responseTime = Date.now() - startTime;
287
+ await this.logRequest({
288
+ aiModel: getModelId(this.model),
289
+ prompt,
290
+ requestType: "json_value",
291
+ response: JSON.stringify(result.output),
292
+ responseTime,
293
+ tokensUsed: result.usage?.totalTokens,
294
+ userId,
295
+ });
296
+
297
+ return result.output;
298
+ } catch (error) {
299
+ const responseTime = Date.now() - startTime;
300
+ await this.logStructuredJsonFailure({
301
+ error,
302
+ prompt,
303
+ requestType: "json_value",
304
+ responseTime,
305
+ system,
306
+ userId,
307
+ });
308
+ throw error;
309
+ }
310
+ }
311
+
312
+ /** Typed object from a Zod schema, `jsonSchema(...)`, or other `FlexibleSchema` (`Output.object()`). */
313
+ async generateJsonObject<OBJECT>(options: GenerateJsonObjectOptions<OBJECT>): Promise<OBJECT> {
314
+ const {
315
+ maxOutputTokens,
316
+ prompt,
317
+ schema,
318
+ schemaDescription,
319
+ schemaName,
320
+ systemPrompt,
321
+ temperature,
322
+ userId,
323
+ } = options;
324
+ const startTime = Date.now();
325
+ const system = systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT;
326
+
327
+ try {
328
+ const result = await aiGenerateText({
329
+ experimental_telemetry: {functionId: "generate-json-object", isEnabled: true},
330
+ maxOutputTokens,
331
+ model: this.getModelForStructuredJson(),
332
+ output: Output.object({
333
+ description: schemaDescription,
334
+ name: schemaName,
335
+ schema,
336
+ }),
337
+ prompt,
338
+ system,
339
+ temperature: temperature ?? TemperaturePresets.DETERMINISTIC,
340
+ });
341
+
342
+ const responseTime = Date.now() - startTime;
343
+ await this.logRequest({
344
+ aiModel: getModelId(this.model),
345
+ prompt,
346
+ requestType: "json_object",
347
+ response: JSON.stringify(result.output),
348
+ responseTime,
349
+ tokensUsed: result.usage?.totalTokens,
350
+ userId,
351
+ });
352
+
353
+ return result.output;
354
+ } catch (error) {
355
+ const responseTime = Date.now() - startTime;
356
+ await this.logStructuredJsonFailure({
357
+ error,
358
+ prompt,
359
+ requestType: "json_object",
360
+ responseTime,
361
+ system,
362
+ userId,
363
+ });
364
+ throw error;
365
+ }
366
+ }
367
+
368
+ /**
369
+ * Typed array: the model is steered to emit `{"elements":[...]}`; the SDK validates each entry
370
+ * and this method returns the plain array (`Output.array()`).
371
+ */
372
+ async generateJsonArray<ELEMENT>(
373
+ options: GenerateJsonArrayOptions<ELEMENT>
374
+ ): Promise<Array<ELEMENT>> {
375
+ const {
376
+ element,
377
+ maxOutputTokens,
378
+ outputDescription,
379
+ outputName,
380
+ prompt,
381
+ systemPrompt,
382
+ temperature,
383
+ userId,
384
+ } = options;
385
+ const startTime = Date.now();
386
+ const system = systemPrompt ?? JSON_VALUE_SYSTEM_PROMPT;
387
+
388
+ try {
389
+ const result = await aiGenerateText({
390
+ experimental_telemetry: {functionId: "generate-json-array", isEnabled: true},
391
+ maxOutputTokens,
392
+ model: this.getModelForStructuredJson(),
393
+ output: Output.array({
394
+ description: outputDescription,
395
+ element,
396
+ name: outputName,
397
+ }),
398
+ prompt,
399
+ system,
400
+ temperature: temperature ?? TemperaturePresets.DETERMINISTIC,
401
+ });
402
+
403
+ const responseTime = Date.now() - startTime;
404
+ await this.logRequest({
405
+ aiModel: getModelId(this.model),
406
+ prompt,
407
+ requestType: "json_array",
408
+ response: JSON.stringify(result.output),
409
+ responseTime,
410
+ tokensUsed: result.usage?.totalTokens,
411
+ userId,
412
+ });
413
+
414
+ return result.output;
415
+ } catch (error) {
416
+ const responseTime = Date.now() - startTime;
417
+ await this.logStructuredJsonFailure({
418
+ error,
419
+ prompt,
420
+ requestType: "json_array",
421
+ responseTime,
422
+ system,
423
+ userId,
424
+ });
425
+ throw error;
426
+ }
427
+ }
428
+
111
429
  async *generateTextStream(options: GenerateStreamOptions): AsyncGenerator<string> {
112
430
  const {prompt, systemPrompt, temperature, maxOutputTokens, userId} = options;
113
431
  const startTime = Date.now();
@@ -1,11 +1,16 @@
1
+ export type {FlexibleSchema, JSONValue} from "ai";
2
+ export {jsonSchema, Output} from "ai";
1
3
  export {AIService, TemperaturePresets} from "./aiService";
2
4
  export {FileStorageService} from "./fileStorage";
3
5
  export type {ListGeminiApiModelsOptions} from "./gemini";
4
6
  export {GEMINI_API_BASE_URL, listGeminiApiModels, normalizeGeminiModelId} from "./gemini";
5
7
  export {MCPService} from "./mcpService";
8
+ export type {ParseFailure, ParseResult, ParseSuccess} from "./parseAiJson";
9
+ export {normalizeLlmJsonTextForStructuredOutput, parseAiJson} from "./parseAiJson";
6
10
  export {
7
11
  CONTENT_SUMMARY_PROMPT,
8
12
  DEFAULT_GPT_MEMORY,
13
+ JSON_VALUE_SYSTEM_PROMPT,
9
14
  REMIX_PROMPT,
10
15
  TITLE_GENERATION_PROMPT,
11
16
  TRANSLATION_PROMPT,
@@ -0,0 +1,76 @@
1
+ import {describe, expect, it} from "bun:test";
2
+
3
+ import {normalizeLlmJsonTextForStructuredOutput, parseAiJson} from "./parseAiJson";
4
+
5
+ describe("parseAiJson", () => {
6
+ it("parses clean JSON without repair", () => {
7
+ const r = parseAiJson<{a: number}>(`{"a":1}`);
8
+ expect(r.success).toBe(true);
9
+ if (r.success) {
10
+ expect(r.repaired).toBe(false);
11
+ expect(r.data).toEqual({a: 1});
12
+ }
13
+ });
14
+
15
+ it("strips a fully wrapped ```json fence", () => {
16
+ const r = parseAiJson(`\`\`\`json\n{"b":2}\n\`\`\``);
17
+ expect(r.success).toBe(true);
18
+ if (r.success) {
19
+ expect(r.data).toEqual({b: 2});
20
+ }
21
+ });
22
+
23
+ it("extracts JSON from preamble and trailing prose", () => {
24
+ const r = parseAiJson(`Sure! Here is the result: {"ok":true} — hope this helps.`);
25
+ expect(r.success).toBe(true);
26
+ if (r.success) {
27
+ expect(r.repaired).toBe(true);
28
+ expect(r.data).toEqual({ok: true});
29
+ }
30
+ });
31
+
32
+ it("handles nested objects and arrays in balanced extraction", () => {
33
+ const r = parseAiJson(`noise {"x":[1,{"y":2}]} tail`);
34
+ expect(r.success).toBe(true);
35
+ if (r.success) {
36
+ expect(r.data).toEqual({x: [1, {y: 2}]});
37
+ }
38
+ });
39
+
40
+ it("repairs trailing commas outside strings", () => {
41
+ const r = parseAiJson(`{"a":1,}`);
42
+ expect(r.success).toBe(true);
43
+ if (r.success) {
44
+ expect(r.repaired).toBe(true);
45
+ expect(r.data).toEqual({a: 1});
46
+ }
47
+ });
48
+
49
+ it("repairs smart quotes on property names via aggressive normalization", () => {
50
+ const r = parseAiJson(`{“smart”: true}`);
51
+ expect(r.success).toBe(true);
52
+ if (r.success) {
53
+ expect(r.repaired).toBe(true);
54
+ expect(r.data).toEqual({smart: true});
55
+ }
56
+ });
57
+
58
+ it("returns failure for non-JSON", () => {
59
+ const r = parseAiJson("not json at all");
60
+ expect(r.success).toBe(false);
61
+ if (!r.success) {
62
+ expect(r.error.length).toBeGreaterThan(0);
63
+ expect(r.raw).toBe("not json at all");
64
+ }
65
+ });
66
+ });
67
+
68
+ describe("normalizeLlmJsonTextForStructuredOutput", () => {
69
+ it("returns JSON.stringify output on success", () => {
70
+ expect(normalizeLlmJsonTextForStructuredOutput(`Yep: [1,2]`)).toBe("[1,2]");
71
+ });
72
+
73
+ it("returns fence-stripped text when parse fails", () => {
74
+ expect(normalizeLlmJsonTextForStructuredOutput("```json\nbroken\n```")).toBe("broken");
75
+ });
76
+ });
@@ -0,0 +1,214 @@
1
+ /**
2
+ * Bulletproof-ish extraction of JSON from LLM output.
3
+ *
4
+ * Handles common "return only JSON" violations:
5
+ * - markdown code fences (full ```json … ``` wrappers or partial ``` lines)
6
+ * - preamble / trailing prose around a JSON value
7
+ * - JSON buried mid-string (first balanced `{`/`[` … `}`/`]` slice)
8
+ * - trailing commas before `}` or `]`
9
+ * - smart quotes outside string literals (conservative repair)
10
+ *
11
+ * Strategy: try the cheapest parse first, escalate only when it fails.
12
+ */
13
+
14
+ export interface ParseSuccess<T> {
15
+ data: T;
16
+ repaired: boolean;
17
+ success: true;
18
+ }
19
+
20
+ export interface ParseFailure {
21
+ error: string;
22
+ raw: string;
23
+ success: false;
24
+ }
25
+
26
+ export type ParseResult<T> = ParseFailure | ParseSuccess<T>;
27
+
28
+ /**
29
+ * Strip leading/trailing markdown code fences when the whole payload is fenced,
30
+ * otherwise strip a common opening ```lang line and trailing ``` block.
31
+ */
32
+ const stripFences = (input: string): string => {
33
+ const trimmed = input.trim();
34
+ const fullyFenced = /^```(?:json|javascript|js|typescript|ts)?\s*\n?([\s\S]*?)\n?```$/i.exec(
35
+ trimmed
36
+ );
37
+ if (fullyFenced) {
38
+ return fullyFenced[1].trim();
39
+ }
40
+
41
+ let t = trimmed;
42
+ t = t.replace(/^```[a-zA-Z0-9_-]*\s*:?\s*\n?/, "");
43
+ t = t.replace(/\n?```\s*$/m, "").trim();
44
+ return t;
45
+ };
46
+
47
+ /**
48
+ * Returns the first balanced JSON object or array substring, respecting string literals
49
+ * and escapes. Uses a bracket stack so nested `{}` / `[]` mix correctly.
50
+ */
51
+ const extractBalancedJson = (input: string): string | null => {
52
+ const start = input.search(/[{[]/);
53
+ if (start === -1) {
54
+ return null;
55
+ }
56
+
57
+ const stack: string[] = [];
58
+ let inString = false;
59
+ let escaped = false;
60
+
61
+ for (let i = start; i < input.length; i++) {
62
+ const char = input[i];
63
+
64
+ if (escaped) {
65
+ escaped = false;
66
+ continue;
67
+ }
68
+ if (char === "\\") {
69
+ escaped = true;
70
+ continue;
71
+ }
72
+ if (char === '"') {
73
+ inString = !inString;
74
+ continue;
75
+ }
76
+ if (inString) {
77
+ continue;
78
+ }
79
+
80
+ if (char === "{") {
81
+ stack.push("}");
82
+ continue;
83
+ }
84
+ if (char === "[") {
85
+ stack.push("]");
86
+ continue;
87
+ }
88
+ if (char === "}" || char === "]") {
89
+ const expected = stack.pop();
90
+ if (expected !== char) {
91
+ return null;
92
+ }
93
+ if (stack.length === 0) {
94
+ return input.slice(start, i + 1);
95
+ }
96
+ }
97
+ }
98
+
99
+ return null;
100
+ };
101
+
102
+ /**
103
+ * Light-touch repairs for common malformations. Only changes characters outside
104
+ * string literals so legitimate string content is preserved.
105
+ */
106
+ const repairJsonishOutsideStrings = (input: string): string => {
107
+ let result = "";
108
+ let inString = false;
109
+ let escaped = false;
110
+
111
+ for (let i = 0; i < input.length; i++) {
112
+ const char = input[i];
113
+
114
+ if (escaped) {
115
+ result += char;
116
+ escaped = false;
117
+ continue;
118
+ }
119
+ if (char === "\\") {
120
+ result += char;
121
+ escaped = true;
122
+ continue;
123
+ }
124
+ if (char === '"') {
125
+ inString = !inString;
126
+ result += char;
127
+ continue;
128
+ }
129
+
130
+ if (!inString) {
131
+ if (char === "\u201C" || char === "\u201D") {
132
+ result += '"';
133
+ continue;
134
+ }
135
+ if (char === ",") {
136
+ const rest = input.slice(i + 1);
137
+ const next = rest.match(/^\s*([}\]])/);
138
+ if (next) {
139
+ continue;
140
+ }
141
+ }
142
+ }
143
+
144
+ result += char;
145
+ }
146
+
147
+ return result;
148
+ };
149
+
150
+ const tryParse = <T>(candidate: string): T | undefined => {
151
+ try {
152
+ return JSON.parse(candidate) as T;
153
+ } catch {
154
+ return undefined;
155
+ }
156
+ };
157
+
158
+ /**
159
+ * Extract and parse JSON from arbitrary LLM output.
160
+ */
161
+ export const parseAiJson = <T = unknown>(input: string): ParseResult<T> => {
162
+ if (typeof input !== "string" || input.trim().length === 0) {
163
+ return {error: "Empty or non-string input", raw: input, success: false};
164
+ }
165
+
166
+ const cleaned = stripFences(input);
167
+
168
+ const direct = tryParse<T>(cleaned);
169
+ if (direct !== undefined) {
170
+ return {data: direct, repaired: false, success: true};
171
+ }
172
+
173
+ const balanced = extractBalancedJson(cleaned);
174
+ if (balanced !== null) {
175
+ const extracted = tryParse<T>(balanced);
176
+ if (extracted !== undefined) {
177
+ return {data: extracted, repaired: true, success: true};
178
+ }
179
+
180
+ const repairedSlice = tryParse<T>(repairJsonishOutsideStrings(balanced));
181
+ if (repairedSlice !== undefined) {
182
+ return {data: repairedSlice, repaired: true, success: true};
183
+ }
184
+ }
185
+
186
+ const repairedWhole = tryParse<T>(repairJsonishOutsideStrings(cleaned));
187
+ if (repairedWhole !== undefined) {
188
+ return {data: repairedWhole, repaired: true, success: true};
189
+ }
190
+
191
+ const aggressiveQuotes = tryParse<T>(cleaned.replace(/[\u201C\u201D]/g, '"'));
192
+ if (aggressiveQuotes !== undefined) {
193
+ return {data: aggressiveQuotes, repaired: true, success: true};
194
+ }
195
+
196
+ return {
197
+ error: "No valid JSON could be extracted",
198
+ raw: input,
199
+ success: false,
200
+ };
201
+ };
202
+
203
+ /**
204
+ * Normalizes raw LLM text into a single JSON text payload for Vercel `Output.*` parsers.
205
+ * On success returns `JSON.stringify` of the parsed value; on failure returns `cleaned`
206
+ * (fence-stripped) text so the SDK can still attempt its own error path.
207
+ */
208
+ export const normalizeLlmJsonTextForStructuredOutput = (raw: string): string => {
209
+ const parsed = parseAiJson<unknown>(raw);
210
+ if (parsed.success) {
211
+ return JSON.stringify(parsed.data);
212
+ }
213
+ return stripFences(raw);
214
+ };
@@ -20,3 +20,8 @@ export const TRANSLATION_PROMPT =
20
20
  export const TITLE_GENERATION_PROMPT =
21
21
  "Generate a short title (3-6 words) that summarizes this conversation for a sidebar label. " +
22
22
  "Return only the title text with no quotes, punctuation, or additional commentary.";
23
+
24
+ /** When callers omit systemPrompt for JSON helpers, steer the model away from prose and markdown fences. */
25
+ export const JSON_VALUE_SYSTEM_PROMPT =
26
+ "You respond with a single JSON value (object, array, string, number, boolean, or null) only. " +
27
+ "No markdown code fences, no commentary before or after the JSON.";