ag-common 0.0.853 → 0.0.854
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/api/helpers/google/gemini.d.ts +6 -0
- package/dist/api/helpers/google/gemini.js +22 -1
- package/dist/node/helpers/fetch.d.ts +4 -0
- package/dist/node/helpers/fetch.js +27 -0
- package/dist/node/helpers/headers.d.ts +29 -0
- package/dist/node/helpers/headers.js +73 -0
- package/dist/node/helpers/index.d.ts +1 -0
- package/dist/node/helpers/index.js +1 -0
- package/package.json +1 -1
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
export type ModelPreference = 'quality' | 'fast' | undefined;
|
|
2
|
+
export declare const geminiPromptImage: ({ prompt, urls, ident, prefer, }: {
|
|
3
|
+
prompt: string;
|
|
4
|
+
urls?: string[];
|
|
5
|
+
ident: string | undefined;
|
|
6
|
+
prefer?: ModelPreference;
|
|
7
|
+
}) => Promise<string>;
|
|
2
8
|
export declare const geminiPromptDirect: ({ prompt, images, ident, prefer, groundedSearch, }: {
|
|
3
9
|
prompt: string;
|
|
4
10
|
images?: {
|
|
@@ -9,9 +9,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.resolveGroundedUrl = exports.geminiPromptDirect = void 0;
|
|
12
|
+
exports.resolveGroundedUrl = exports.geminiPromptDirect = exports.geminiPromptImage = void 0;
|
|
13
13
|
const genai_1 = require("@google/genai");
|
|
14
|
+
const array_1 = require("../../../common/helpers/array");
|
|
15
|
+
const async_1 = require("../../../common/helpers/async");
|
|
14
16
|
const log_1 = require("../../../common/helpers/log");
|
|
17
|
+
const fetch_1 = require("../../../node/helpers/fetch");
|
|
15
18
|
const apikey_1 = require("./apikey");
|
|
16
19
|
let genAIs;
|
|
17
20
|
// Available Gemini models
|
|
@@ -72,6 +75,24 @@ const getAvailableGeminiCombinations = (prefer) => {
|
|
|
72
75
|
}
|
|
73
76
|
return combinations;
|
|
74
77
|
};
|
|
78
|
+
const geminiPromptImage = (_a) => __awaiter(void 0, [_a], void 0, function* ({ prompt, urls, ident, prefer, }) {
|
|
79
|
+
let images = [];
|
|
80
|
+
if (urls && urls.length > 0) {
|
|
81
|
+
images = yield (0, async_1.asyncMap)(urls, (i) => (0, fetch_1.fetchToMemory)(i));
|
|
82
|
+
}
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
84
|
+
if (images.find((a) => !a)) {
|
|
85
|
+
throw new Error('image not downloaded correctly');
|
|
86
|
+
}
|
|
87
|
+
const r = yield (0, exports.geminiPromptDirect)({
|
|
88
|
+
prompt,
|
|
89
|
+
images: images.filter(array_1.notEmpty),
|
|
90
|
+
ident,
|
|
91
|
+
prefer,
|
|
92
|
+
});
|
|
93
|
+
return r;
|
|
94
|
+
});
|
|
95
|
+
exports.geminiPromptImage = geminiPromptImage;
|
|
75
96
|
const geminiPromptDirect = (_a) => __awaiter(void 0, [_a], void 0, function* ({ prompt, images = [], ident, prefer, groundedSearch = false, }) {
|
|
76
97
|
var _b;
|
|
77
98
|
const combinations = getAvailableGeminiCombinations(prefer);
|
|
@@ -13,7 +13,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.fetchFile = fetchFile;
|
|
16
|
+
exports.fetchToMemory = fetchToMemory;
|
|
16
17
|
const fs_1 = __importDefault(require("fs"));
|
|
18
|
+
const log_1 = require("../../common/helpers/log");
|
|
19
|
+
const headers_1 = require("./headers");
|
|
17
20
|
/**
|
|
18
21
|
* download(fetch) file
|
|
19
22
|
* @param p
|
|
@@ -45,3 +48,27 @@ function fetchFile(p) {
|
|
|
45
48
|
}
|
|
46
49
|
});
|
|
47
50
|
}
|
|
51
|
+
function fetchToMemory(imageUrl) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
try {
|
|
54
|
+
// Download the image
|
|
55
|
+
const response = yield fetch(imageUrl, { headers: (0, headers_1.getFetchHeaders)() });
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
58
|
+
}
|
|
59
|
+
let type = response.headers.get('content-type') || 'image/jpeg';
|
|
60
|
+
if (type.includes('octet')) {
|
|
61
|
+
(0, log_1.debug)('forcing type to jpeg:' + imageUrl);
|
|
62
|
+
type = 'image/jpeg';
|
|
63
|
+
}
|
|
64
|
+
// Get the image data as an ArrayBuffer
|
|
65
|
+
const arraybuffer = yield response.arrayBuffer();
|
|
66
|
+
return { type, arraybuffer };
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
const em = e;
|
|
70
|
+
(0, log_1.warn)('Error downloading image:', imageUrl, em.toString());
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export declare const getFetchHeaders: (host?: string) => {
|
|
2
|
+
accept: string;
|
|
3
|
+
'accept-language': string;
|
|
4
|
+
connection: string;
|
|
5
|
+
dnt: string;
|
|
6
|
+
host: string;
|
|
7
|
+
'sec-fetch-dest': string;
|
|
8
|
+
'sec-fetch-mode': string;
|
|
9
|
+
'sec-fetch-site': string;
|
|
10
|
+
'user-agent': string;
|
|
11
|
+
'sec-ch-ua': string;
|
|
12
|
+
'sec-ch-ua-mobile': string;
|
|
13
|
+
'sec-ch-ua-platform': string;
|
|
14
|
+
'sec-fetch-user': string;
|
|
15
|
+
'upgrade-insecure-requests': string;
|
|
16
|
+
'viewport-width': string;
|
|
17
|
+
'viewport-height': string;
|
|
18
|
+
'sec-ch-ua-full-version-list': string;
|
|
19
|
+
'sec-ch-ua-arch': string;
|
|
20
|
+
'sec-ch-ua-bitness': string;
|
|
21
|
+
'sec-ch-ua-model': string;
|
|
22
|
+
'sec-ch-ua-platform-version': string;
|
|
23
|
+
'sec-ch-ua-wow64': string;
|
|
24
|
+
'device-memory': string;
|
|
25
|
+
downlink: string;
|
|
26
|
+
ect: string;
|
|
27
|
+
rtt: string;
|
|
28
|
+
'color-depth': string;
|
|
29
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFetchHeaders = void 0;
|
|
4
|
+
const languages = [
|
|
5
|
+
'en-AU,en;q=0.9,en-GB;q=0.8,en-US;q=0.7,cy;q=0.6,af;q=0.5,lb;q=0.4,hu;q=0.3,pt;q=0.2',
|
|
6
|
+
];
|
|
7
|
+
const browsers = [
|
|
8
|
+
{
|
|
9
|
+
name: 'Google Chrome',
|
|
10
|
+
version: '135',
|
|
11
|
+
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
name: 'Firefox',
|
|
15
|
+
version: '124',
|
|
16
|
+
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0',
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
name: 'Edge',
|
|
20
|
+
version: '122',
|
|
21
|
+
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0',
|
|
22
|
+
},
|
|
23
|
+
];
|
|
24
|
+
const platforms = ['Windows', 'Macintosh', 'Linux'];
|
|
25
|
+
const getRandomItem = (array) => {
|
|
26
|
+
return array[Math.floor(Math.random() * array.length)];
|
|
27
|
+
};
|
|
28
|
+
// Add randomization to viewport dimensions
|
|
29
|
+
const viewports = [
|
|
30
|
+
{ width: 1920, height: 1080 },
|
|
31
|
+
{ width: 1366, height: 768 },
|
|
32
|
+
{ width: 1536, height: 864 },
|
|
33
|
+
{ width: 1440, height: 900 },
|
|
34
|
+
{ width: 1280, height: 720 },
|
|
35
|
+
];
|
|
36
|
+
const getFetchHeaders = (host) => {
|
|
37
|
+
const browser = getRandomItem(browsers);
|
|
38
|
+
const browserString = `"${browser.name}";v="${browser.version}", "Not-A.Brand";v="8", "Chromium";v="${browser.version}"`;
|
|
39
|
+
const viewport = getRandomItem(viewports);
|
|
40
|
+
const platform = getRandomItem(platforms);
|
|
41
|
+
const colorDepth = getRandomItem([24, 30, 48]);
|
|
42
|
+
const connection = getRandomItem(['4g', '5g', 'wifi']);
|
|
43
|
+
return {
|
|
44
|
+
accept: '*/*',
|
|
45
|
+
'accept-language': getRandomItem(languages),
|
|
46
|
+
connection: 'keep-alive',
|
|
47
|
+
dnt: '1',
|
|
48
|
+
host: host || '',
|
|
49
|
+
'sec-fetch-dest': 'empty',
|
|
50
|
+
'sec-fetch-mode': 'cors',
|
|
51
|
+
'sec-fetch-site': 'same-origin',
|
|
52
|
+
'user-agent': browser.userAgent,
|
|
53
|
+
'sec-ch-ua': browserString,
|
|
54
|
+
'sec-ch-ua-mobile': '?0',
|
|
55
|
+
'sec-ch-ua-platform': '"Windows"',
|
|
56
|
+
'sec-fetch-user': '?1',
|
|
57
|
+
'upgrade-insecure-requests': '1',
|
|
58
|
+
'viewport-width': viewport.width.toString(),
|
|
59
|
+
'viewport-height': viewport.height.toString(),
|
|
60
|
+
'sec-ch-ua-full-version-list': browserString,
|
|
61
|
+
'sec-ch-ua-arch': `"${platform === 'Windows' ? 'x86' : 'arm'}"`,
|
|
62
|
+
'sec-ch-ua-bitness': '64',
|
|
63
|
+
'sec-ch-ua-model': '',
|
|
64
|
+
'sec-ch-ua-platform-version': platform === 'Windows' ? '"15.0.0"' : '"14.0.0"',
|
|
65
|
+
'sec-ch-ua-wow64': '?0',
|
|
66
|
+
'device-memory': getRandomItem(['4', '8', '16']),
|
|
67
|
+
downlink: getRandomItem(['1.5', '5.0', '10.0']),
|
|
68
|
+
ect: connection,
|
|
69
|
+
rtt: getRandomItem(['50', '100', '150']),
|
|
70
|
+
'color-depth': colorDepth.toString(),
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
exports.getFetchHeaders = getFetchHeaders;
|
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./fetch"), exports);
|
|
18
18
|
__exportStar(require("./fs"), exports);
|
|
19
|
+
__exportStar(require("./headers"), exports);
|
package/package.json
CHANGED