n8n-nodes-vercel-ai-sdk-universal-temp 0.1.36 → 0.1.38
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/nodes/UniversalAI/UniversalAI.node.js +169 -167
- package/dist/nodes/UniversalAI/UniversalAI.node.js.map +1 -1
- package/dist/nodes/shared/descriptions.js +13 -10
- package/dist/nodes/shared/descriptions.js.map +1 -1
- package/dist/package.json +3 -2
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -2
|
@@ -119,52 +119,69 @@ let googleCacheManager = null;
|
|
|
119
119
|
const createdCaches = new Map();
|
|
120
120
|
async function createGoogleCache(exec, index, apiKey, systemPrompt, tools) {
|
|
121
121
|
try {
|
|
122
|
-
if (!googleCacheManager) {
|
|
123
|
-
const { GoogleAICacheManager } = require('@google/generative-ai/server');
|
|
124
|
-
googleCacheManager = new GoogleAICacheManager(apiKey);
|
|
125
|
-
}
|
|
126
122
|
const useGoogleCache = exec.getNodeParameter('useGoogleCache', index, false);
|
|
127
123
|
if (!useGoogleCache) {
|
|
128
124
|
return null;
|
|
129
125
|
}
|
|
126
|
+
if (!googleCacheManager) {
|
|
127
|
+
const { GoogleGenAI } = await Promise.resolve().then(() => __importStar(require('@google/genai')));
|
|
128
|
+
googleCacheManager = new GoogleGenAI({ apiKey });
|
|
129
|
+
}
|
|
130
130
|
const hasContent = (systemPrompt && systemPrompt.trim()) || (tools && Object.keys(tools).length > 0);
|
|
131
131
|
if (!hasContent) {
|
|
132
|
-
console.log('UniversalAI:
|
|
132
|
+
console.log('UniversalAI: No content to cache, skipping cache creation');
|
|
133
|
+
return null;
|
|
133
134
|
}
|
|
134
135
|
const cacheKeyData = {
|
|
135
136
|
system: systemPrompt || '',
|
|
136
137
|
tools: tools ? Object.keys(tools).sort() : [],
|
|
137
|
-
|
|
138
|
+
model: 'gemini-2.0-flash-001',
|
|
138
139
|
};
|
|
139
140
|
const cacheKeyString = JSON.stringify(cacheKeyData);
|
|
140
141
|
const existingCache = createdCaches.get(cacheKeyString);
|
|
141
|
-
if (existingCache && (Date.now() - existingCache.createdAt) < (existingCache.ttlSeconds * 1000)) {
|
|
142
|
+
if (existingCache && (Date.now() - existingCache.createdAt) < (existingCache.ttlSeconds * 1000 * 0.83)) {
|
|
142
143
|
console.log('UniversalAI: Reusing existing cache:', existingCache.name);
|
|
143
144
|
return existingCache.name;
|
|
144
145
|
}
|
|
145
146
|
const ttlSeconds = 3600;
|
|
146
|
-
const
|
|
147
|
-
console.log('UniversalAI: Creating Google cache:', {
|
|
148
|
-
|
|
147
|
+
const displayName = `universal_ai_cache_${Date.now()}`;
|
|
148
|
+
console.log('UniversalAI: Creating Google cache with config:', {
|
|
149
|
+
displayName,
|
|
149
150
|
hasSystem: !!systemPrompt,
|
|
150
151
|
hasTools: !!tools && Object.keys(tools).length > 0,
|
|
151
152
|
ttlSeconds
|
|
152
153
|
});
|
|
153
154
|
const cacheConfig = {
|
|
154
155
|
model: 'gemini-2.0-flash-001',
|
|
155
|
-
|
|
156
|
-
|
|
156
|
+
config: {
|
|
157
|
+
displayName,
|
|
158
|
+
ttl: `${ttlSeconds}s`,
|
|
159
|
+
},
|
|
157
160
|
};
|
|
158
161
|
if (systemPrompt && systemPrompt.trim()) {
|
|
159
|
-
cacheConfig.systemInstruction = {
|
|
162
|
+
cacheConfig.config.systemInstruction = {
|
|
160
163
|
parts: [{ text: systemPrompt }],
|
|
161
164
|
};
|
|
165
|
+
console.log('UniversalAI: Added system instruction to cache');
|
|
162
166
|
}
|
|
163
167
|
if (tools && Object.keys(tools).length > 0) {
|
|
164
|
-
cacheConfig.tools = Object.values(tools);
|
|
168
|
+
cacheConfig.config.tools = Object.values(tools);
|
|
169
|
+
console.log('UniversalAI: Added tools to cache:', Object.keys(tools));
|
|
170
|
+
}
|
|
171
|
+
cacheConfig.config.contents = [
|
|
172
|
+
{
|
|
173
|
+
parts: [{ text: "This is cached content for context." }],
|
|
174
|
+
role: "user"
|
|
175
|
+
}
|
|
176
|
+
];
|
|
177
|
+
console.log('UniversalAI: Final cache config:', JSON.stringify(cacheConfig, null, 2));
|
|
178
|
+
console.log('UniversalAI: About to call googleCacheManager.caches.create()...');
|
|
179
|
+
const result = await googleCacheManager.caches.create(cacheConfig);
|
|
180
|
+
console.log('UniversalAI: Cache creation API call completed, result:', result);
|
|
181
|
+
const cachedContentName = result.name;
|
|
182
|
+
if (!cachedContentName) {
|
|
183
|
+
throw new Error('Failed to get cached content name from creation response');
|
|
165
184
|
}
|
|
166
|
-
console.log('UniversalAI: Cache config before creation:', cacheConfig);
|
|
167
|
-
const { name: cachedContentName } = await googleCacheManager.create(cacheConfig);
|
|
168
185
|
createdCaches.set(cacheKeyString, {
|
|
169
186
|
name: cachedContentName,
|
|
170
187
|
createdAt: Date.now(),
|
|
@@ -175,9 +192,13 @@ async function createGoogleCache(exec, index, apiKey, systemPrompt, tools) {
|
|
|
175
192
|
}
|
|
176
193
|
catch (error) {
|
|
177
194
|
console.error('UniversalAI: Failed to create Google cache:', error);
|
|
195
|
+
console.log('UniversalAI: Falling back to non-cached execution');
|
|
178
196
|
return null;
|
|
179
197
|
}
|
|
180
198
|
}
|
|
199
|
+
function canUseCache(systemPrompt, tools) {
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
181
202
|
const messageSchema = zod_1.z.object({
|
|
182
203
|
role: zod_1.z.enum(['system', 'user', 'assistant']),
|
|
183
204
|
content: zod_1.z.any(),
|
|
@@ -330,32 +351,6 @@ async function getBinaryData(fileContentInput, itemBinary, exec, itemIndex) {
|
|
|
330
351
|
}
|
|
331
352
|
return null;
|
|
332
353
|
}
|
|
333
|
-
function formatTextWithGroundingLinks(text, groundingMetadata) {
|
|
334
|
-
if (!(groundingMetadata === null || groundingMetadata === void 0 ? void 0 : groundingMetadata.groundingSupports) || !(groundingMetadata === null || groundingMetadata === void 0 ? void 0 : groundingMetadata.groundingChunks)) {
|
|
335
|
-
return text;
|
|
336
|
-
}
|
|
337
|
-
let formattedText = text;
|
|
338
|
-
const supports = groundingMetadata.groundingSupports;
|
|
339
|
-
const chunks = groundingMetadata.groundingChunks;
|
|
340
|
-
const sortedSupports = supports
|
|
341
|
-
.filter((support) => { var _a; return ((_a = support.groundingChunkIndices) === null || _a === void 0 ? void 0 : _a.length) > 0; })
|
|
342
|
-
.sort((a, b) => b.segment.startIndex - a.segment.startIndex);
|
|
343
|
-
for (const support of sortedSupports) {
|
|
344
|
-
const segment = support.segment;
|
|
345
|
-
const chunkIndex = support.groundingChunkIndices[0];
|
|
346
|
-
const chunk = chunks[chunkIndex];
|
|
347
|
-
if (chunk === null || chunk === void 0 ? void 0 : chunk.web) {
|
|
348
|
-
const startIndex = segment.startIndex;
|
|
349
|
-
const endIndex = segment.endIndex;
|
|
350
|
-
const segmentText = text.substring(startIndex, endIndex);
|
|
351
|
-
const linkUrl = chunk.web.uri;
|
|
352
|
-
const beforeSegment = formattedText.substring(0, startIndex);
|
|
353
|
-
const afterSegment = formattedText.substring(endIndex);
|
|
354
|
-
formattedText = `${beforeSegment}[${segmentText}](${linkUrl})${afterSegment}`;
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
return formattedText;
|
|
358
|
-
}
|
|
359
354
|
function formatTextResult(result, includeRequestBody, provider) {
|
|
360
355
|
var _a, _b, _c, _d;
|
|
361
356
|
let { text, reasoning } = result;
|
|
@@ -386,9 +381,6 @@ function formatTextResult(result, includeRequestBody, provider) {
|
|
|
386
381
|
baseResult.groundingMetadata = (_a = providerMetadata === null || providerMetadata === void 0 ? void 0 : providerMetadata.google) === null || _a === void 0 ? void 0 : _a.groundingMetadata;
|
|
387
382
|
baseResult.safetyRatings = (_b = providerMetadata === null || providerMetadata === void 0 ? void 0 : providerMetadata.google) === null || _b === void 0 ? void 0 : _b.safetyRatings;
|
|
388
383
|
baseResult.urlContextMetadata = (_c = providerMetadata === null || providerMetadata === void 0 ? void 0 : providerMetadata.google) === null || _c === void 0 ? void 0 : _c.urlContextMetadata;
|
|
389
|
-
if (baseResult.groundingMetadata) {
|
|
390
|
-
baseResult.textWithLinks = formatTextWithGroundingLinks(text, baseResult.groundingMetadata);
|
|
391
|
-
}
|
|
392
384
|
}
|
|
393
385
|
if (includeRequestBody) {
|
|
394
386
|
const requestBody = (_d = result.request) === null || _d === void 0 ? void 0 : _d.body;
|
|
@@ -427,9 +419,15 @@ function formatUsage(result, provider) {
|
|
|
427
419
|
return usage;
|
|
428
420
|
}
|
|
429
421
|
function getCacheMetrics(result, provider) {
|
|
430
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
422
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
|
|
431
423
|
const metadata = result.experimental_providerMetadata;
|
|
432
424
|
const metrics = {};
|
|
425
|
+
console.log('UniversalAI: Checking cache metrics from metadata:', {
|
|
426
|
+
provider,
|
|
427
|
+
hasMetadata: !!metadata,
|
|
428
|
+
metadataKeys: metadata ? Object.keys(metadata) : [],
|
|
429
|
+
googleMetadata: metadata === null || metadata === void 0 ? void 0 : metadata.google,
|
|
430
|
+
});
|
|
433
431
|
switch (provider) {
|
|
434
432
|
case 'deepseek':
|
|
435
433
|
if (((_a = metadata === null || metadata === void 0 ? void 0 : metadata.deepseek) === null || _a === void 0 ? void 0 : _a.promptCacheHitTokens) !== undefined) {
|
|
@@ -448,24 +446,51 @@ function getCacheMetrics(result, provider) {
|
|
|
448
446
|
}
|
|
449
447
|
break;
|
|
450
448
|
case 'google':
|
|
451
|
-
|
|
449
|
+
console.log('UniversalAI: Google metadata structure:', {
|
|
450
|
+
google: metadata === null || metadata === void 0 ? void 0 : metadata.google,
|
|
451
|
+
usageMetadata: (_e = metadata === null || metadata === void 0 ? void 0 : metadata.google) === null || _e === void 0 ? void 0 : _e.usageMetadata,
|
|
452
|
+
candidates: (_f = metadata === null || metadata === void 0 ? void 0 : metadata.google) === null || _f === void 0 ? void 0 : _f.candidates,
|
|
453
|
+
});
|
|
454
|
+
if (((_h = (_g = metadata === null || metadata === void 0 ? void 0 : metadata.google) === null || _g === void 0 ? void 0 : _g.usageMetadata) === null || _h === void 0 ? void 0 : _h.cachedContentTokenCount) !== undefined) {
|
|
452
455
|
metrics.cachedContentTokenCount = metadata.google.usageMetadata.cachedContentTokenCount;
|
|
456
|
+
console.log('UniversalAI: Found cachedContentTokenCount in usageMetadata:', metrics.cachedContentTokenCount);
|
|
457
|
+
}
|
|
458
|
+
if (((_k = (_j = result.response) === null || _j === void 0 ? void 0 : _j.usageMetadata) === null || _k === void 0 ? void 0 : _k.cachedContentTokenCount) !== undefined) {
|
|
459
|
+
metrics.cachedContentTokenCount = result.response.usageMetadata.cachedContentTokenCount;
|
|
460
|
+
console.log('UniversalAI: Found cachedContentTokenCount in response metadata:', metrics.cachedContentTokenCount);
|
|
453
461
|
}
|
|
454
|
-
if (((
|
|
462
|
+
if (((_p = (_o = (_m = (_l = metadata === null || metadata === void 0 ? void 0 : metadata.google) === null || _l === void 0 ? void 0 : _l.candidates) === null || _m === void 0 ? void 0 : _m[0]) === null || _o === void 0 ? void 0 : _o.usageMetadata) === null || _p === void 0 ? void 0 : _p.cachedContentTokenCount) !== undefined) {
|
|
463
|
+
metrics.cachedContentTokenCount = metadata.google.candidates[0].usageMetadata.cachedContentTokenCount;
|
|
464
|
+
console.log('UniversalAI: Found cachedContentTokenCount in candidates metadata:', metrics.cachedContentTokenCount);
|
|
465
|
+
}
|
|
466
|
+
if (((_r = (_q = metadata === null || metadata === void 0 ? void 0 : metadata.google) === null || _q === void 0 ? void 0 : _q.usageMetadata) === null || _r === void 0 ? void 0 : _r.thoughtsTokenCount) !== undefined) {
|
|
455
467
|
metrics.thoughtsTokenCount = metadata.google.usageMetadata.thoughtsTokenCount;
|
|
468
|
+
console.log('UniversalAI: Found thoughtsTokenCount:', metrics.thoughtsTokenCount);
|
|
469
|
+
}
|
|
470
|
+
if (((_t = (_s = metadata === null || metadata === void 0 ? void 0 : metadata.google) === null || _s === void 0 ? void 0 : _s.usageMetadata) === null || _t === void 0 ? void 0 : _t.cacheTokensUsed) !== undefined) {
|
|
471
|
+
metrics.cacheTokensUsed = metadata.google.usageMetadata.cacheTokensUsed;
|
|
472
|
+
console.log('UniversalAI: Found cacheTokensUsed:', metrics.cacheTokensUsed);
|
|
456
473
|
}
|
|
474
|
+
console.log('UniversalAI: Final Google cache metrics:', metrics);
|
|
457
475
|
break;
|
|
458
476
|
}
|
|
477
|
+
console.log('UniversalAI: Extracted cache metrics:', metrics);
|
|
459
478
|
return metrics;
|
|
460
479
|
}
|
|
461
480
|
function formatResponse(result) {
|
|
462
|
-
var _a, _b, _c, _d;
|
|
463
|
-
|
|
481
|
+
var _a, _b, _c, _d, _e;
|
|
482
|
+
const response = {
|
|
464
483
|
id: (_a = result.response) === null || _a === void 0 ? void 0 : _a.id,
|
|
465
484
|
modelId: (_b = result.response) === null || _b === void 0 ? void 0 : _b.modelId,
|
|
466
485
|
timestamp: (_c = result.response) === null || _c === void 0 ? void 0 : _c.timestamp,
|
|
467
486
|
headers: (_d = result.response) === null || _d === void 0 ? void 0 : _d.headers,
|
|
468
487
|
};
|
|
488
|
+
console.log('UniversalAI: Response metadata:', {
|
|
489
|
+
hasResponse: !!result.response,
|
|
490
|
+
responseKeys: result.response ? Object.keys(result.response) : [],
|
|
491
|
+
usageMetadata: (_e = result.response) === null || _e === void 0 ? void 0 : _e.usageMetadata,
|
|
492
|
+
});
|
|
493
|
+
return response;
|
|
469
494
|
}
|
|
470
495
|
async function getProvider(provider, apiKey, baseURL) {
|
|
471
496
|
const cacheKey = `${provider}:${apiKey}:${baseURL || ''}`;
|
|
@@ -559,6 +584,7 @@ class UniversalAI {
|
|
|
559
584
|
async execute() {
|
|
560
585
|
const items = this.getInputData();
|
|
561
586
|
const returnData = [];
|
|
587
|
+
const errorData = [];
|
|
562
588
|
const provider = this.getNodeParameter('provider', 0);
|
|
563
589
|
const credentialType = {
|
|
564
590
|
google: 'googleGenerativeAIApi',
|
|
@@ -576,19 +602,24 @@ class UniversalAI {
|
|
|
576
602
|
const aiProvider = await getProvider(provider, credentials.apiKey, credentials.baseUrl);
|
|
577
603
|
for (let i = 0; i < items.length; i++) {
|
|
578
604
|
try {
|
|
579
|
-
const result = await processItem(this, i, provider, aiProvider);
|
|
605
|
+
const result = await processItem(this, i, provider, aiProvider, credentials.apiKey);
|
|
580
606
|
returnData.push(...result);
|
|
581
607
|
}
|
|
582
608
|
catch (error) {
|
|
583
609
|
if (this.continueOnFail()) {
|
|
584
|
-
const
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
610
|
+
const errorOutput = {
|
|
611
|
+
success: false,
|
|
612
|
+
error: error instanceof Error ? error.message : String(error),
|
|
613
|
+
itemIndex: i,
|
|
614
|
+
timestamp: new Date().toISOString(),
|
|
615
|
+
nodeName: this.getNode().name,
|
|
616
|
+
provider,
|
|
617
|
+
};
|
|
618
|
+
if (error instanceof Error && error.stack) {
|
|
619
|
+
errorOutput.stack = error.stack;
|
|
620
|
+
}
|
|
621
|
+
errorData.push({
|
|
622
|
+
json: errorOutput,
|
|
592
623
|
pairedItem: { item: i },
|
|
593
624
|
});
|
|
594
625
|
}
|
|
@@ -598,23 +629,24 @@ class UniversalAI {
|
|
|
598
629
|
}
|
|
599
630
|
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, {
|
|
600
631
|
itemIndex: i,
|
|
632
|
+
description: `Failed to process item ${i + 1}`,
|
|
601
633
|
});
|
|
602
634
|
}
|
|
603
635
|
}
|
|
604
636
|
}
|
|
605
|
-
return [returnData];
|
|
637
|
+
return [returnData, errorData];
|
|
606
638
|
}
|
|
607
639
|
}
|
|
608
640
|
exports.UniversalAI = UniversalAI;
|
|
609
|
-
async function processItem(exec, index, provider, aiProvider) {
|
|
641
|
+
async function processItem(exec, index, provider, aiProvider, apiKey) {
|
|
610
642
|
const operation = exec.getNodeParameter('operation', index);
|
|
611
643
|
const model = exec.getNodeParameter('model', index);
|
|
612
644
|
const options = exec.getNodeParameter('options', index, {});
|
|
613
645
|
const input = await buildInput(exec, index);
|
|
614
646
|
const modelSettings = getModelSettings(exec, index, provider, operation, options);
|
|
615
647
|
return operation === 'generateText'
|
|
616
|
-
? await generateTextOperation(exec, index, provider, aiProvider, model, modelSettings, input, options)
|
|
617
|
-
: await generateObjectOperation(exec, index, provider, aiProvider, model, modelSettings, input, options);
|
|
648
|
+
? await generateTextOperation(exec, index, provider, aiProvider, model, modelSettings, input, options, apiKey)
|
|
649
|
+
: await generateObjectOperation(exec, index, provider, aiProvider, model, modelSettings, input, options, apiKey);
|
|
618
650
|
}
|
|
619
651
|
function getModelSettings(exec, index, provider, operation, options) {
|
|
620
652
|
const settings = {};
|
|
@@ -638,86 +670,72 @@ function getModelSettings(exec, index, provider, operation, options) {
|
|
|
638
670
|
}
|
|
639
671
|
return settings;
|
|
640
672
|
}
|
|
641
|
-
function buildGoogleProviderOptions(exec, index) {
|
|
673
|
+
function buildGoogleProviderOptions(exec, index, cachedContentName) {
|
|
642
674
|
const thinkingBudgetValue = Number(exec.getNodeParameter('thinkingBudget', index, 0));
|
|
643
675
|
const includeThoughts = exec.getNodeParameter('includeThoughts', index, false);
|
|
644
676
|
const options = {};
|
|
645
|
-
if (!Number.isNaN(thinkingBudgetValue)) {
|
|
677
|
+
if (!Number.isNaN(thinkingBudgetValue) && thinkingBudgetValue > 0) {
|
|
646
678
|
options.thinkingConfig = {
|
|
647
679
|
thinkingBudget: thinkingBudgetValue,
|
|
648
680
|
includeThoughts,
|
|
649
681
|
};
|
|
650
682
|
}
|
|
683
|
+
if (cachedContentName) {
|
|
684
|
+
options.cachedContent = cachedContentName;
|
|
685
|
+
}
|
|
651
686
|
return Object.keys(options).length > 0 ? options : undefined;
|
|
652
687
|
}
|
|
653
|
-
async function generateTextOperation(exec, index, provider, aiProvider, model, modelSettings, input, options) {
|
|
654
|
-
var _a, _b;
|
|
688
|
+
async function generateTextOperation(exec, index, provider, aiProvider, model, modelSettings, input, options, apiKey) {
|
|
655
689
|
const enableStreaming = exec.getNodeParameter('enableStreaming', index, false);
|
|
656
690
|
const includeRequestBody = options.includeRequestBody;
|
|
657
691
|
const tools = provider === 'google' ? await buildGoogleTools(exec, index) : undefined;
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
...input,
|
|
661
|
-
...(tools && { tools }),
|
|
662
|
-
};
|
|
692
|
+
let cachedContentName = null;
|
|
693
|
+
let googleProviderOptions;
|
|
663
694
|
if (provider === 'google') {
|
|
664
695
|
const useGoogleCache = exec.getNodeParameter('useGoogleCache', index, false);
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
console.log('UniversalAI: Cache enabled, attempting to create cache...');
|
|
696
|
+
if (useGoogleCache && canUseCache(input.system, tools)) {
|
|
697
|
+
console.log('UniversalAI: Creating Google cache...');
|
|
668
698
|
try {
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
console.log('UniversalAI: Got API key, calling createGoogleCache...');
|
|
673
|
-
const cachedContentName = await createGoogleCache(exec, index, apiKey, input.system, tools);
|
|
674
|
-
console.log('UniversalAI: Cache creation result:', cachedContentName);
|
|
675
|
-
if (cachedContentName) {
|
|
676
|
-
const googleProviderOptions = buildGoogleProviderOptions(exec, index);
|
|
677
|
-
params.providerOptions = {
|
|
678
|
-
google: {
|
|
679
|
-
...googleProviderOptions,
|
|
680
|
-
cachedContent: cachedContentName,
|
|
681
|
-
},
|
|
682
|
-
};
|
|
683
|
-
if (params.system)
|
|
684
|
-
delete params.system;
|
|
685
|
-
if (params.tools)
|
|
686
|
-
delete params.tools;
|
|
687
|
-
console.log('UniversalAI: Using Google cache (system/tools moved to cache):', cachedContentName);
|
|
688
|
-
console.log('UniversalAI: Request params after cache processing:', {
|
|
689
|
-
hasSystem: !!params.system,
|
|
690
|
-
hasTools: !!params.tools,
|
|
691
|
-
hasCachedContent: !!((_b = (_a = params.providerOptions) === null || _a === void 0 ? void 0 : _a.google) === null || _b === void 0 ? void 0 : _b.cachedContent),
|
|
692
|
-
});
|
|
693
|
-
}
|
|
694
|
-
else {
|
|
695
|
-
console.log('UniversalAI: No cache created, continuing without cache');
|
|
696
|
-
}
|
|
699
|
+
cachedContentName = await createGoogleCache(exec, index, apiKey, input.system, tools);
|
|
700
|
+
if (cachedContentName) {
|
|
701
|
+
console.log('UniversalAI: Successfully created/retrieved cache:', cachedContentName);
|
|
697
702
|
}
|
|
698
703
|
else {
|
|
699
|
-
console.log('UniversalAI:
|
|
704
|
+
console.log('UniversalAI: Cache creation returned null, continuing without cache');
|
|
700
705
|
}
|
|
701
706
|
}
|
|
702
707
|
catch (error) {
|
|
703
708
|
console.warn('UniversalAI: Cache creation failed, continuing without cache:', error);
|
|
704
709
|
}
|
|
705
710
|
}
|
|
706
|
-
|
|
707
|
-
|
|
711
|
+
googleProviderOptions = buildGoogleProviderOptions(exec, index, cachedContentName || undefined);
|
|
712
|
+
}
|
|
713
|
+
const params = {
|
|
714
|
+
model: aiProvider(model, modelSettings),
|
|
715
|
+
...input,
|
|
716
|
+
};
|
|
717
|
+
if (cachedContentName) {
|
|
718
|
+
console.log('UniversalAI: Using cache, removing system and tools from request params');
|
|
719
|
+
if (params.system) {
|
|
720
|
+
console.log('UniversalAI: Removed system instruction from request (moved to cache)');
|
|
721
|
+
delete params.system;
|
|
722
|
+
}
|
|
723
|
+
if (params.tools) {
|
|
724
|
+
console.log('UniversalAI: Removed tools from request (moved to cache)');
|
|
725
|
+
delete params.tools;
|
|
708
726
|
}
|
|
709
727
|
}
|
|
710
728
|
else {
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
if (provider === 'google' && !params.providerOptions) {
|
|
714
|
-
const googleProviderOptions = buildGoogleProviderOptions(exec, index);
|
|
715
|
-
if (googleProviderOptions) {
|
|
716
|
-
params.providerOptions = {
|
|
717
|
-
google: googleProviderOptions,
|
|
718
|
-
};
|
|
729
|
+
if (tools) {
|
|
730
|
+
params.tools = tools;
|
|
719
731
|
}
|
|
720
732
|
}
|
|
733
|
+
if (provider === 'google' && googleProviderOptions) {
|
|
734
|
+
params.providerOptions = {
|
|
735
|
+
google: googleProviderOptions,
|
|
736
|
+
};
|
|
737
|
+
console.log('UniversalAI: Added Google provider options:', googleProviderOptions);
|
|
738
|
+
}
|
|
721
739
|
const numericOptionKeys = [
|
|
722
740
|
'maxTokens',
|
|
723
741
|
'temperature',
|
|
@@ -733,6 +751,12 @@ async function generateTextOperation(exec, index, provider, aiProvider, model, m
|
|
|
733
751
|
params[key] = value;
|
|
734
752
|
}
|
|
735
753
|
}
|
|
754
|
+
console.log('UniversalAI: Final request params:', {
|
|
755
|
+
hasSystem: !!params.system,
|
|
756
|
+
hasTools: !!params.tools,
|
|
757
|
+
hasCachedContent: !!cachedContentName,
|
|
758
|
+
providerOptions: params.providerOptions,
|
|
759
|
+
});
|
|
736
760
|
if (enableStreaming) {
|
|
737
761
|
return await handleStreaming(params, provider, includeRequestBody);
|
|
738
762
|
}
|
|
@@ -742,6 +766,7 @@ async function generateTextOperation(exec, index, provider, aiProvider, model, m
|
|
|
742
766
|
hasProviderMetadata: !!result.experimental_providerMetadata,
|
|
743
767
|
providerMetadata: result.experimental_providerMetadata,
|
|
744
768
|
usage: result.usage,
|
|
769
|
+
response: result.response,
|
|
745
770
|
});
|
|
746
771
|
const formattedResult = formatTextResult(result, includeRequestBody, provider);
|
|
747
772
|
return [{ json: formattedResult }];
|
|
@@ -766,9 +791,6 @@ async function buildGoogleTools(exec, index) {
|
|
|
766
791
|
return tools;
|
|
767
792
|
}
|
|
768
793
|
async function handleStreaming(params, provider, includeRequestBody) {
|
|
769
|
-
if (provider === 'google' && params.providerOptions) {
|
|
770
|
-
params.providerOptions.google.streaming = true;
|
|
771
|
-
}
|
|
772
794
|
const stream = await (0, ai_1.streamText)(params);
|
|
773
795
|
const chunks = [];
|
|
774
796
|
let fullText = '';
|
|
@@ -795,73 +817,48 @@ async function handleStreaming(params, provider, includeRequestBody) {
|
|
|
795
817
|
chunks.push({ json: finalJson });
|
|
796
818
|
return chunks;
|
|
797
819
|
}
|
|
798
|
-
async function generateObjectOperation(exec, index, provider, aiProvider, model, modelSettings, input, options) {
|
|
799
|
-
var _a, _b;
|
|
820
|
+
async function generateObjectOperation(exec, index, provider, aiProvider, model, modelSettings, input, options, apiKey) {
|
|
800
821
|
const schemaName = exec.getNodeParameter('schemaName', index, '');
|
|
801
822
|
const schemaDescription = exec.getNodeParameter('schemaDescription', index, '');
|
|
802
823
|
const rawSchema = exec.getNodeParameter('schema', index);
|
|
803
824
|
const parsedSchema = parseAndValidateSchema(rawSchema, exec);
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
schema: (0, ai_1.jsonSchema)(parsedSchema),
|
|
807
|
-
schemaName,
|
|
808
|
-
schemaDescription,
|
|
809
|
-
...input,
|
|
810
|
-
};
|
|
825
|
+
let cachedContentName = null;
|
|
826
|
+
let googleProviderOptions;
|
|
811
827
|
if (provider === 'google') {
|
|
812
828
|
const useGoogleCache = exec.getNodeParameter('useGoogleCache', index, false);
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
console.log('UniversalAI: Cache enabled for object generation, attempting to create cache...');
|
|
829
|
+
if (useGoogleCache && canUseCache(input.system, undefined)) {
|
|
830
|
+
console.log('UniversalAI: Creating Google cache for object generation...');
|
|
816
831
|
try {
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
console.log('UniversalAI: Got API key for object generation, calling createGoogleCache...');
|
|
821
|
-
const cachedContentName = await createGoogleCache(exec, index, apiKey, input.system, undefined);
|
|
822
|
-
console.log('UniversalAI: Object generation cache creation result:', cachedContentName);
|
|
823
|
-
if (cachedContentName) {
|
|
824
|
-
const googleProviderOptions = buildGoogleProviderOptions(exec, index);
|
|
825
|
-
params.providerOptions = {
|
|
826
|
-
google: {
|
|
827
|
-
...googleProviderOptions,
|
|
828
|
-
cachedContent: cachedContentName,
|
|
829
|
-
},
|
|
830
|
-
};
|
|
831
|
-
if (params.system)
|
|
832
|
-
delete params.system;
|
|
833
|
-
console.log('UniversalAI: Using Google cache for object generation (system moved to cache):', cachedContentName);
|
|
834
|
-
console.log('UniversalAI: Object generation request params after cache processing:', {
|
|
835
|
-
hasSystem: !!params.system,
|
|
836
|
-
hasCachedContent: !!((_b = (_a = params.providerOptions) === null || _a === void 0 ? void 0 : _a.google) === null || _b === void 0 ? void 0 : _b.cachedContent),
|
|
837
|
-
});
|
|
838
|
-
}
|
|
839
|
-
else {
|
|
840
|
-
console.log('UniversalAI: No cache created for object generation, continuing without cache');
|
|
841
|
-
}
|
|
832
|
+
cachedContentName = await createGoogleCache(exec, index, apiKey, input.system, undefined);
|
|
833
|
+
if (cachedContentName) {
|
|
834
|
+
console.log('UniversalAI: Successfully created/retrieved cache for object generation:', cachedContentName);
|
|
842
835
|
}
|
|
843
836
|
else {
|
|
844
|
-
console.log('UniversalAI:
|
|
837
|
+
console.log('UniversalAI: Cache creation for object generation returned null, continuing without cache');
|
|
845
838
|
}
|
|
846
839
|
}
|
|
847
840
|
catch (error) {
|
|
848
841
|
console.warn('UniversalAI: Cache creation failed for object generation, continuing without cache:', error);
|
|
849
842
|
}
|
|
850
843
|
}
|
|
851
|
-
|
|
852
|
-
console.log('UniversalAI: Cache not enabled for object generation');
|
|
853
|
-
}
|
|
844
|
+
googleProviderOptions = buildGoogleProviderOptions(exec, index, cachedContentName || undefined);
|
|
854
845
|
}
|
|
855
|
-
|
|
856
|
-
|
|
846
|
+
const params = {
|
|
847
|
+
model: aiProvider(model, modelSettings),
|
|
848
|
+
schema: (0, ai_1.jsonSchema)(parsedSchema),
|
|
849
|
+
schemaName,
|
|
850
|
+
schemaDescription,
|
|
851
|
+
...input,
|
|
852
|
+
};
|
|
853
|
+
if (cachedContentName && params.system) {
|
|
854
|
+
console.log('UniversalAI: Using cache for object generation, removing system from request params');
|
|
855
|
+
delete params.system;
|
|
857
856
|
}
|
|
858
|
-
if (provider === 'google' &&
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
};
|
|
864
|
-
}
|
|
857
|
+
if (provider === 'google' && googleProviderOptions) {
|
|
858
|
+
params.providerOptions = {
|
|
859
|
+
google: googleProviderOptions,
|
|
860
|
+
};
|
|
861
|
+
console.log('UniversalAI: Added Google provider options for object generation:', googleProviderOptions);
|
|
865
862
|
}
|
|
866
863
|
const numericOptionKeys = [
|
|
867
864
|
'temperature',
|
|
@@ -877,6 +874,11 @@ async function generateObjectOperation(exec, index, provider, aiProvider, model,
|
|
|
877
874
|
params[key] = value;
|
|
878
875
|
}
|
|
879
876
|
}
|
|
877
|
+
console.log('UniversalAI: Final object generation request params:', {
|
|
878
|
+
hasSystem: !!params.system,
|
|
879
|
+
hasCachedContent: !!cachedContentName,
|
|
880
|
+
providerOptions: params.providerOptions,
|
|
881
|
+
});
|
|
880
882
|
const result = await (0, ai_1.generateObject)(params);
|
|
881
883
|
console.log('UniversalAI: Debug - generateObject result:', {
|
|
882
884
|
provider,
|