@contentgrowth/llm-service 0.6.3 → 0.6.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentgrowth/llm-service",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "description": "Unified LLM Service for Content Growth",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -12,6 +12,19 @@ export function extractJsonFromResponse(text) {
12
12
  return null;
13
13
  }
14
14
 
15
+ // Helper to sanitize JSON strings with unescaped control characters
16
+ function sanitizeJsonString(str) {
17
+ // Find all JSON strings: "..."
18
+ // Handle escaped quotes \" and backslashes \\
19
+ return str.replace(/"(?:[^"\\]|\\.)*"/g, (match) => {
20
+ // Replace unescaped control characters inside the string
21
+ return match
22
+ .replace(/\n/g, "\\n")
23
+ .replace(/\r/g, "\\r")
24
+ .replace(/\t/g, "\\t");
25
+ });
26
+ }
27
+
15
28
  // Helper function to attempt JSON parsing with escape sequence normalization
16
29
  function tryParseJson(jsonStr) {
17
30
  // First, try to parse as-is
@@ -36,10 +49,16 @@ export function extractJsonFromResponse(text) {
36
49
  } catch (e2) {
37
50
  // Log this failure too
38
51
  console.warn('Normalized JSON parse also failed:', e2.message);
39
- throw e; // Throw original error
52
+ // Fall through to sanitization
40
53
  }
41
- } else {
42
- // No over-escaping pattern detected, throw original error
54
+ }
55
+
56
+ // Try sanitizing unescaped control characters (common LLM error)
57
+ try {
58
+ const sanitized = sanitizeJsonString(jsonStr);
59
+ return JSON.parse(sanitized);
60
+ } catch (e3) {
61
+ // If all attempts fail, throw the original error
43
62
  throw e;
44
63
  }
45
64
  }
@@ -136,8 +136,7 @@ export class LLMService {
136
136
  */
137
137
  async chatCompletionJson(messages, tenantId, systemPrompt, schema = null, tools = null) {
138
138
  const options = {
139
- responseFormat: schema ? 'json_schema' : 'json',
140
- responseSchema: schema,
139
+ responseFormat: schema ? { type: 'json_schema', schema } : 'json',
141
140
  autoParse: true
142
141
  };
143
142