@riotprompt/execution-gemini 0.0.3 → 0.0.4
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 +6 -5
- package/dist/index.cjs +0 -101
- package/dist/index.cjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@riotprompt/execution-gemini",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Google Gemini provider for execution interface",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./dist/index.
|
|
7
|
-
"module": "./dist/index.js",
|
|
6
|
+
"main": "./dist/index.js",
|
|
8
7
|
"types": "./dist/index.d.ts",
|
|
9
8
|
"exports": {
|
|
10
9
|
".": {
|
|
11
10
|
"types": "./dist/index.d.ts",
|
|
12
|
-
"import": "./dist/index.js"
|
|
13
|
-
"require": "./dist/index.cjs"
|
|
11
|
+
"import": "./dist/index.js"
|
|
14
12
|
}
|
|
15
13
|
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=24.0.0"
|
|
16
|
+
},
|
|
16
17
|
"scripts": {
|
|
17
18
|
"clean": "rm -rf dist",
|
|
18
19
|
"build": "vite build",
|
package/dist/index.cjs
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
-
const generativeAi = require("@google/generative-ai");
|
|
4
|
-
class GeminiProvider {
|
|
5
|
-
name = "gemini";
|
|
6
|
-
/**
|
|
7
|
-
* Check if this provider supports a given model
|
|
8
|
-
*/
|
|
9
|
-
supportsModel(model) {
|
|
10
|
-
if (!model) return false;
|
|
11
|
-
return model.startsWith("gemini");
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Execute a request against Gemini
|
|
15
|
-
*/
|
|
16
|
-
async execute(request, options = {}) {
|
|
17
|
-
const apiKey = options.apiKey || process.env.GEMINI_API_KEY;
|
|
18
|
-
if (!apiKey) throw new Error("Gemini API key is required");
|
|
19
|
-
const genAI = new generativeAi.GoogleGenerativeAI(apiKey);
|
|
20
|
-
const modelName = options.model || request.model || "gemini-1.5-pro";
|
|
21
|
-
const generationConfig = {};
|
|
22
|
-
if (request.responseFormat?.type === "json_schema") {
|
|
23
|
-
generationConfig.responseMimeType = "application/json";
|
|
24
|
-
const openAISchema = request.responseFormat.json_schema.schema;
|
|
25
|
-
const mapSchema = (s) => {
|
|
26
|
-
if (!s) return void 0;
|
|
27
|
-
const newSchema = { ...s };
|
|
28
|
-
if (newSchema.type) {
|
|
29
|
-
newSchema.type = typeof newSchema.type === "string" ? newSchema.type.toUpperCase() : newSchema.type;
|
|
30
|
-
}
|
|
31
|
-
if (newSchema.properties) {
|
|
32
|
-
const newProps = {};
|
|
33
|
-
for (const [key, val] of Object.entries(newSchema.properties)) {
|
|
34
|
-
newProps[key] = mapSchema(val);
|
|
35
|
-
}
|
|
36
|
-
newSchema.properties = newProps;
|
|
37
|
-
}
|
|
38
|
-
if (newSchema.items) {
|
|
39
|
-
newSchema.items = mapSchema(newSchema.items);
|
|
40
|
-
}
|
|
41
|
-
delete newSchema.additionalProperties;
|
|
42
|
-
delete newSchema["$schema"];
|
|
43
|
-
return newSchema;
|
|
44
|
-
};
|
|
45
|
-
generationConfig.responseSchema = mapSchema(openAISchema);
|
|
46
|
-
}
|
|
47
|
-
let systemInstruction = "";
|
|
48
|
-
for (const msg of request.messages) {
|
|
49
|
-
if (msg.role === "system" || msg.role === "developer") {
|
|
50
|
-
systemInstruction += (typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)) + "\n\n";
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
const configuredModel = genAI.getGenerativeModel({
|
|
54
|
-
model: modelName,
|
|
55
|
-
systemInstruction: systemInstruction ? systemInstruction.trim() : void 0,
|
|
56
|
-
generationConfig
|
|
57
|
-
});
|
|
58
|
-
const chatHistory = [];
|
|
59
|
-
let lastUserMessage = "";
|
|
60
|
-
for (const msg of request.messages) {
|
|
61
|
-
if (msg.role === "system" || msg.role === "developer") continue;
|
|
62
|
-
const content = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
|
|
63
|
-
if (msg.role === "user") {
|
|
64
|
-
lastUserMessage = content;
|
|
65
|
-
}
|
|
66
|
-
chatHistory.push({
|
|
67
|
-
role: msg.role === "assistant" ? "model" : "user",
|
|
68
|
-
parts: [{ text: content }]
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
let result;
|
|
72
|
-
if (chatHistory.length > 1) {
|
|
73
|
-
const lastMsg = chatHistory.pop();
|
|
74
|
-
const chat = configuredModel.startChat({
|
|
75
|
-
history: chatHistory
|
|
76
|
-
});
|
|
77
|
-
result = await chat.sendMessage(lastMsg?.parts[0].text || "");
|
|
78
|
-
} else {
|
|
79
|
-
result = await configuredModel.generateContent(lastUserMessage || " ");
|
|
80
|
-
}
|
|
81
|
-
const response = await result.response;
|
|
82
|
-
const text = response.text();
|
|
83
|
-
return {
|
|
84
|
-
content: text,
|
|
85
|
-
model: modelName,
|
|
86
|
-
usage: response.usageMetadata ? {
|
|
87
|
-
inputTokens: response.usageMetadata.promptTokenCount,
|
|
88
|
-
outputTokens: response.usageMetadata.candidatesTokenCount
|
|
89
|
-
} : void 0
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
function createGeminiProvider() {
|
|
94
|
-
return new GeminiProvider();
|
|
95
|
-
}
|
|
96
|
-
const VERSION = "0.0.1";
|
|
97
|
-
exports.GeminiProvider = GeminiProvider;
|
|
98
|
-
exports.VERSION = VERSION;
|
|
99
|
-
exports.createGeminiProvider = createGeminiProvider;
|
|
100
|
-
exports.default = GeminiProvider;
|
|
101
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["/**\n * Execution Gemini Package\n *\n * Google Gemini provider implementation for LLM execution.\n *\n * @packageDocumentation\n */\n\nimport { GoogleGenerativeAI } from '@google/generative-ai';\n\n// ===== INLINE TYPES (from 'execution' package) =====\n\nexport type Model = string;\n\nexport interface Message {\n role: 'user' | 'assistant' | 'system' | 'developer' | 'tool';\n content: string | string[] | null;\n name?: string;\n}\n\nexport interface Request {\n messages: Message[];\n model: Model;\n responseFormat?: any;\n validator?: any;\n addMessage(message: Message): void;\n}\n\nexport interface ProviderResponse {\n content: string;\n model: string;\n usage?: {\n inputTokens: number;\n outputTokens: number;\n };\n toolCalls?: Array<{\n id: string;\n type: 'function';\n function: {\n name: string;\n arguments: string;\n };\n }>;\n}\n\nexport interface ExecutionOptions {\n apiKey?: string;\n model?: string;\n temperature?: number;\n maxTokens?: number;\n timeout?: number;\n retries?: number;\n}\n\nexport interface Provider {\n readonly name: string;\n execute(request: Request, options?: ExecutionOptions): Promise<ProviderResponse>;\n supportsModel?(model: Model): boolean;\n}\n\n/**\n * Gemini Provider implementation\n */\nexport class GeminiProvider implements Provider {\n readonly name = 'gemini';\n\n /**\n * Check if this provider supports a given model\n */\n supportsModel(model: Model): boolean {\n if (!model) return false;\n return model.startsWith('gemini');\n }\n\n /**\n * Execute a request against Gemini\n */\n async execute(\n request: Request,\n options: ExecutionOptions = {}\n ): Promise<ProviderResponse> {\n const apiKey = options.apiKey || process.env.GEMINI_API_KEY;\n if (!apiKey) throw new Error('Gemini API key is required');\n\n const genAI = new GoogleGenerativeAI(apiKey);\n\n const modelName = options.model || request.model || 'gemini-1.5-pro';\n\n // Handle generation config for structured output\n const generationConfig: any = {};\n\n if (request.responseFormat?.type === 'json_schema') {\n generationConfig.responseMimeType = 'application/json';\n\n const openAISchema = request.responseFormat.json_schema.schema;\n\n // Map schema types to uppercase for Gemini\n const mapSchema = (s: any): any => {\n if (!s) return undefined;\n\n const newSchema: any = { ...s };\n\n if (newSchema.type) {\n newSchema.type =\n typeof newSchema.type === 'string'\n ? (newSchema.type as string).toUpperCase()\n : newSchema.type;\n }\n\n if (newSchema.properties) {\n const newProps: any = {};\n for (const [key, val] of Object.entries(newSchema.properties)) {\n newProps[key] = mapSchema(val);\n }\n newSchema.properties = newProps;\n }\n\n if (newSchema.items) {\n newSchema.items = mapSchema(newSchema.items);\n }\n\n delete newSchema.additionalProperties;\n delete newSchema['$schema'];\n\n return newSchema;\n };\n\n generationConfig.responseSchema = mapSchema(openAISchema);\n }\n\n // Extract system instruction\n let systemInstruction = '';\n\n for (const msg of request.messages) {\n if (msg.role === 'system' || msg.role === 'developer') {\n systemInstruction +=\n (typeof msg.content === 'string'\n ? msg.content\n : JSON.stringify(msg.content)) + '\\n\\n';\n }\n }\n\n const configuredModel = genAI.getGenerativeModel({\n model: modelName,\n systemInstruction: systemInstruction\n ? systemInstruction.trim()\n : undefined,\n generationConfig,\n });\n\n // Build history/messages\n const chatHistory = [];\n let lastUserMessage = '';\n\n for (const msg of request.messages) {\n if (msg.role === 'system' || msg.role === 'developer') continue;\n\n const content =\n typeof msg.content === 'string'\n ? msg.content\n : JSON.stringify(msg.content);\n\n if (msg.role === 'user') {\n lastUserMessage = content;\n }\n\n chatHistory.push({\n role: msg.role === 'assistant' ? 'model' : 'user',\n parts: [{ text: content }],\n });\n }\n\n let result;\n\n if (chatHistory.length > 1) {\n const lastMsg = chatHistory.pop();\n const chat = configuredModel.startChat({\n history: chatHistory,\n });\n result = await chat.sendMessage(lastMsg?.parts[0].text || '');\n } else {\n result = await configuredModel.generateContent(lastUserMessage || ' ');\n }\n\n const response = await result.response;\n const text = response.text();\n\n return {\n content: text,\n model: modelName,\n usage: response.usageMetadata\n ? {\n inputTokens: response.usageMetadata.promptTokenCount,\n outputTokens: response.usageMetadata.candidatesTokenCount,\n }\n : undefined,\n };\n }\n}\n\n/**\n * Create a new Gemini provider instance\n */\nexport function createGeminiProvider(): GeminiProvider {\n return new GeminiProvider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