@contentgrowth/llm-service 0.8.3 → 0.8.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/dist/index.cjs +1527 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +529 -0
- package/dist/index.d.ts +529 -0
- package/dist/index.js +1478 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +3 -0
- package/dist/ui/react/components/index.cjs +1474 -0
- package/dist/ui/react/components/index.cjs.map +1 -0
- package/dist/ui/react/components/index.d.cts +292 -0
- package/dist/ui/react/components/index.d.ts +292 -0
- package/dist/ui/react/components/index.js +1432 -0
- package/dist/ui/react/components/index.js.map +1 -0
- package/package.json +46 -10
- package/src/index.js +0 -9
- package/src/llm/config-manager.js +0 -45
- package/src/llm/config-provider.js +0 -140
- package/src/llm/json-utils.js +0 -147
- package/src/llm/providers/base-provider.js +0 -134
- package/src/llm/providers/gemini-provider.js +0 -609
- package/src/llm/providers/openai-provider.js +0 -203
- package/src/llm-service.js +0 -281
- package/src/utils/error-handler.js +0 -117
package/src/llm/json-utils.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Extracts and parses JSON from a text response (e.g., from an LLM).
|
|
3
|
-
* Handles JSON in markdown code blocks or plain JSON objects.
|
|
4
|
-
*
|
|
5
|
-
* TODO: improveme for better performance
|
|
6
|
-
*
|
|
7
|
-
* @param {string} text - The text containing JSON
|
|
8
|
-
* @returns {object|null} - The parsed JSON object, or null if no valid JSON found
|
|
9
|
-
*/
|
|
10
|
-
export function extractJsonFromResponse(text) {
|
|
11
|
-
if (!text || typeof text !== 'string') {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
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
|
-
|
|
28
|
-
// Helper function to attempt JSON parsing with escape sequence normalization
|
|
29
|
-
function tryParseJson(jsonStr) {
|
|
30
|
-
// First, try to parse as-is
|
|
31
|
-
try {
|
|
32
|
-
return JSON.parse(jsonStr);
|
|
33
|
-
} catch (e) {
|
|
34
|
-
// If that fails, check if the LLM over-escaped the content
|
|
35
|
-
// This is a common issue where LLMs return \\\\n instead of \\n
|
|
36
|
-
|
|
37
|
-
// Only attempt normalization if we detect the problematic pattern
|
|
38
|
-
if (jsonStr.includes('\\\\\\\\')) {
|
|
39
|
-
// Log the first parse attempt failure for debugging
|
|
40
|
-
console.warn('Initial JSON parse failed, attempting normalization:', e.message);
|
|
41
|
-
|
|
42
|
-
try {
|
|
43
|
-
// Strategy: The LLM sometimes escapes strings that are already escaped
|
|
44
|
-
// For example: "content": "text\\\\nmore" should be "content": "text\\nmore"
|
|
45
|
-
// Replace quadruple backslashes with double (handles over-escaping)
|
|
46
|
-
let normalized = jsonStr.replace(/\\\\\\\\/g, '\\\\');
|
|
47
|
-
|
|
48
|
-
return JSON.parse(normalized);
|
|
49
|
-
} catch (e2) {
|
|
50
|
-
// Log this failure too
|
|
51
|
-
console.warn('Normalized JSON parse also failed:', e2.message);
|
|
52
|
-
// Fall through to sanitization
|
|
53
|
-
}
|
|
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
|
|
62
|
-
throw e;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Regular expression to find a JSON object within markdown code fences.
|
|
68
|
-
// It's flexible with or without the 'json' language specifier.
|
|
69
|
-
const jsonRegex = /```(?:json)?\s*({[\s\S]*?})\s*```/;
|
|
70
|
-
const match = text.match(jsonRegex);
|
|
71
|
-
|
|
72
|
-
// If a fenced JSON block is found, try to parse it.
|
|
73
|
-
if (match && match[1]) {
|
|
74
|
-
try {
|
|
75
|
-
return tryParseJson(match[1]);
|
|
76
|
-
} catch (e) {
|
|
77
|
-
// If parsing fails, log the error and fall through to the next method.
|
|
78
|
-
console.warn('Could not parse the content of a matched JSON block.', e.message);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Fallback for cases where the AI might not use markdown fences correctly.
|
|
83
|
-
// Find the first opening brace and the last closing brace.
|
|
84
|
-
const firstBrace = text.indexOf('{');
|
|
85
|
-
const lastBrace = text.lastIndexOf('}');
|
|
86
|
-
|
|
87
|
-
if (firstBrace !== -1 && lastBrace > firstBrace) {
|
|
88
|
-
const potentialJson = text.substring(firstBrace, lastBrace + 1);
|
|
89
|
-
try {
|
|
90
|
-
return tryParseJson(potentialJson);
|
|
91
|
-
} catch (e) {
|
|
92
|
-
// This substring is not valid JSON.
|
|
93
|
-
console.error('Error parsing JSON extracted in { and }', e);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// If no valid JSON could be extracted by any method, return null.
|
|
98
|
-
return null;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Generic helper to separate conversational text from a structured JSON payload.
|
|
103
|
-
* Supports both "Text + JSON" and "JSON + Text" patterns.
|
|
104
|
-
*
|
|
105
|
-
* @param {string} input - The full response string
|
|
106
|
-
* @returns {{text: string, json: object|null}}
|
|
107
|
-
*/
|
|
108
|
-
export function extractTextAndJson(input) {
|
|
109
|
-
if (!input) return { text: '', json: null };
|
|
110
|
-
|
|
111
|
-
// 1. Try to extract JSON using the existing robust extractor
|
|
112
|
-
const json = extractJsonFromResponse(input);
|
|
113
|
-
|
|
114
|
-
// 2. If no JSON found, return full input as text
|
|
115
|
-
if (!json) {
|
|
116
|
-
return { text: input, json: null };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// 3. If JSON found, we need to remove the JSON block to get the clean text
|
|
120
|
-
let text = input;
|
|
121
|
-
|
|
122
|
-
// Try fenced block first (most reliable) - same regex as extractJsonFromResponse
|
|
123
|
-
const fencedRegex = /```(?:json)?\s*({[\s\S]*?})\s*```/;
|
|
124
|
-
const fencedMatch = input.match(fencedRegex);
|
|
125
|
-
|
|
126
|
-
if (fencedMatch) {
|
|
127
|
-
// Replace the entire fenced block with empty string to leave just the text
|
|
128
|
-
// This handles both "Text + JSON" and "JSON + Text" patterns
|
|
129
|
-
text = input.replace(fencedMatch[0], '').trim();
|
|
130
|
-
return { text, json };
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Try brace extraction as fallback - same logic as extractJsonFromResponse
|
|
134
|
-
const firstBrace = input.indexOf('{');
|
|
135
|
-
const lastBrace = input.lastIndexOf('}');
|
|
136
|
-
|
|
137
|
-
if (firstBrace !== -1 && lastBrace > firstBrace) {
|
|
138
|
-
// Remove the brace block, keeping text before and after
|
|
139
|
-
const pre = input.substring(0, firstBrace);
|
|
140
|
-
const post = input.substring(lastBrace + 1);
|
|
141
|
-
text = (pre + post).trim();
|
|
142
|
-
return { text, json };
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// Fallback: Return original text if we couldn't cleanly separate it
|
|
146
|
-
return { text: input, json };
|
|
147
|
-
}
|
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Standardized finish reasons across all LLM providers.
|
|
3
|
-
* Providers map their native values to these standard constants.
|
|
4
|
-
*/
|
|
5
|
-
export const FINISH_REASONS = {
|
|
6
|
-
COMPLETED: 'completed', // Normal completion (OpenAI: stop, Gemini: STOP, Anthropic: end_turn)
|
|
7
|
-
TRUNCATED: 'truncated', // Hit max tokens (OpenAI: length, Gemini: MAX_TOKENS, Anthropic: max_tokens)
|
|
8
|
-
CONTENT_FILTER: 'content_filter', // Content was filtered
|
|
9
|
-
TOOL_CALL: 'tool_call', // Stopped for tool call
|
|
10
|
-
UNKNOWN: 'unknown', // Unknown/unmapped reason
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Abstract base class for LLM Providers.
|
|
15
|
-
* Defines the standard interface that all providers must implement.
|
|
16
|
-
*/
|
|
17
|
-
export class BaseLLMProvider {
|
|
18
|
-
constructor(config) {
|
|
19
|
-
this.config = config;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Normalize provider-specific finish reason to standard value.
|
|
24
|
-
* Override in subclass if provider uses different values.
|
|
25
|
-
* @param {string} providerReason - The provider's native finish reason
|
|
26
|
-
* @returns {string} Standardized finish reason from FINISH_REASONS
|
|
27
|
-
*/
|
|
28
|
-
normalizeFinishReason(providerReason) {
|
|
29
|
-
// Default mappings - providers can override
|
|
30
|
-
const upperReason = (providerReason || '').toUpperCase();
|
|
31
|
-
|
|
32
|
-
// Completed mappings
|
|
33
|
-
if (['STOP', 'END_TURN'].includes(upperReason)) {
|
|
34
|
-
return FINISH_REASONS.COMPLETED;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Truncated mappings
|
|
38
|
-
if (['LENGTH', 'MAX_TOKENS'].includes(upperReason)) {
|
|
39
|
-
return FINISH_REASONS.TRUNCATED;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Content filter mappings
|
|
43
|
-
if (['CONTENT_FILTER', 'SAFETY'].includes(upperReason)) {
|
|
44
|
-
return FINISH_REASONS.CONTENT_FILTER;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Tool call mappings
|
|
48
|
-
if (['TOOL_CALLS', 'TOOL_USE', 'FUNCTION_CALL'].includes(upperReason)) {
|
|
49
|
-
return FINISH_REASONS.TOOL_CALL;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return FINISH_REASONS.UNKNOWN;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Simple chat interface for single-turn conversations
|
|
57
|
-
* @param {string} userMessage
|
|
58
|
-
* @param {string} systemPrompt
|
|
59
|
-
* @param {Object} options
|
|
60
|
-
*/
|
|
61
|
-
async chat(userMessage, systemPrompt, options) {
|
|
62
|
-
throw new Error('Method not implemented');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Advanced chat completion with tool support
|
|
67
|
-
* @param {Array} messages
|
|
68
|
-
* @param {string} systemPrompt
|
|
69
|
-
* @param {Array} tools
|
|
70
|
-
* @param {Object} options - Generation options (responseFormat, temperature, etc.)
|
|
71
|
-
*/
|
|
72
|
-
async chatCompletion(messages, systemPrompt, tools, options = {}) {
|
|
73
|
-
throw new Error('Method not implemented');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Execute tools requested by the LLM
|
|
78
|
-
* @param {Array} toolCalls
|
|
79
|
-
* @param {Array} messages
|
|
80
|
-
* @param {string} tenantId
|
|
81
|
-
* @param {Object} toolImplementations
|
|
82
|
-
* @param {Object} env
|
|
83
|
-
*/
|
|
84
|
-
async executeTools(toolCalls, messages, tenantId, toolImplementations, env) {
|
|
85
|
-
throw new Error('Method not implemented');
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Generate image
|
|
90
|
-
* Subclasses should override this method.
|
|
91
|
-
* Model can be overridden via options.model, otherwise uses config.models.image
|
|
92
|
-
* @param {string} prompt - Text description of the image
|
|
93
|
-
* @param {string} systemPrompt - System instructions for generation
|
|
94
|
-
* @param {Object} options - Generation options (aspectRatio, images, model, etc.)
|
|
95
|
-
* @returns {Promise<{imageData: string, mimeType: string}>}
|
|
96
|
-
*/
|
|
97
|
-
async imageGeneration(prompt, systemPrompt, options = {}) {
|
|
98
|
-
throw new Error('Image generation not supported by this provider');
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Start video generation (returns operation name for polling)
|
|
104
|
-
* @param {string} prompt
|
|
105
|
-
* @param {Array} images
|
|
106
|
-
* @param {string} modelName
|
|
107
|
-
* @param {string} systemPrompt
|
|
108
|
-
* @param {Object} options
|
|
109
|
-
* @returns {Promise<{operationName: string}>}
|
|
110
|
-
*/
|
|
111
|
-
async startVideoGeneration(prompt, images, modelName, systemPrompt, options) {
|
|
112
|
-
throw new Error('Video generation not supported by this provider');
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Get video generation status (poll operation)
|
|
117
|
-
* @param {string} operationName
|
|
118
|
-
* @returns {Promise<{done: boolean, progress: number, state: string, videoUri?: string, error?: object}>}
|
|
119
|
-
*/
|
|
120
|
-
async getVideoGenerationStatus(operationName) {
|
|
121
|
-
throw new Error('Video generation not supported by this provider');
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Helper to get the last 6 digits of the API key for logging.
|
|
126
|
-
* @returns {string} "..." + last 6 chars, or "not_set"
|
|
127
|
-
*/
|
|
128
|
-
_getMaskedApiKey() {
|
|
129
|
-
const key = this.config.apiKey;
|
|
130
|
-
if (!key) return 'not_set';
|
|
131
|
-
if (key.length <= 6) return '...';
|
|
132
|
-
return '...' + key.slice(-6);
|
|
133
|
-
}
|
|
134
|
-
}
|