@contentgrowth/llm-service 0.7.6 → 0.7.8
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
package/src/index.js
CHANGED
|
@@ -4,6 +4,6 @@ export { BaseConfigProvider, DefaultConfigProvider } from './llm/config-provider
|
|
|
4
4
|
export { MODEL_CONFIGS } from './llm/config-manager.js';
|
|
5
5
|
export { OpenAIProvider } from './llm/providers/openai-provider.js';
|
|
6
6
|
export { GeminiProvider } from './llm/providers/gemini-provider.js';
|
|
7
|
-
export { extractJsonFromResponse } from './llm/json-utils.js';
|
|
7
|
+
export { extractJsonFromResponse, extractTextAndJson } from './llm/json-utils.js';
|
|
8
8
|
export { FINISH_REASONS } from './llm/providers/base-provider.js';
|
|
9
9
|
|
|
@@ -16,11 +16,11 @@ export const MODEL_CONFIGS = {
|
|
|
16
16
|
free: 'gpt-4o-mini',
|
|
17
17
|
},
|
|
18
18
|
gemini: {
|
|
19
|
-
default: 'gemini-2.5-flash',
|
|
20
|
-
edge: 'gemini-2.5-pro',
|
|
21
|
-
fast: 'gemini-2.5-flash-lite',
|
|
22
|
-
cost: 'gemini-2.5-flash-lite',
|
|
23
|
-
free: 'gemini-2.0-flash-lite',
|
|
19
|
+
default: 'gemini-3-flash-preview', // 'gemini-2.5-flash',
|
|
20
|
+
edge: 'gemini-3-pro-preview', // 'gemini-2.5-pro',
|
|
21
|
+
fast: 'gemini-3-flash-preview', // 'gemini-2.5-flash-lite',
|
|
22
|
+
cost: 'gemini-3-flash-preview', // 'gemini-2.5-flash-lite',
|
|
23
|
+
free: 'gemini-3-flash-preview', // 'gemini-2.0-flash-lite',
|
|
24
24
|
video: 'veo',
|
|
25
25
|
},
|
|
26
26
|
};
|
package/src/llm/json-utils.js
CHANGED
|
@@ -97,3 +97,51 @@ export function extractJsonFromResponse(text) {
|
|
|
97
97
|
// If no valid JSON could be extracted by any method, return null.
|
|
98
98
|
return null;
|
|
99
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
|
+
}
|
|
@@ -113,12 +113,24 @@ export class GeminiProvider extends BaseLLMProvider {
|
|
|
113
113
|
break;
|
|
114
114
|
case 'assistant':
|
|
115
115
|
role = 'model';
|
|
116
|
+
parts = [];
|
|
117
|
+
|
|
118
|
+
// Always include text if present (Thought)
|
|
119
|
+
if (msg.content) {
|
|
120
|
+
parts.push({ text: msg.content });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Append tool calls if present (Call)
|
|
116
124
|
if (msg.tool_calls) {
|
|
117
|
-
|
|
125
|
+
const callParts = msg.tool_calls.map(tc => ({
|
|
118
126
|
functionCall: { name: tc.function.name, args: tc.function.arguments }
|
|
119
127
|
}));
|
|
120
|
-
|
|
121
|
-
|
|
128
|
+
parts.push(...callParts);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Safety fallback: if nothing was added, ensure valid structure
|
|
132
|
+
if (parts.length === 0) {
|
|
133
|
+
parts.push({ text: '' });
|
|
122
134
|
}
|
|
123
135
|
break;
|
|
124
136
|
case 'tool':
|