aiwrapper
Version:
A Universal AI Wrapper for JavaScript & TypeScript
105 lines (104 loc) • 3.68 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import {
LanguageProvider
} from "../language-provider.js";
import { models } from "aimodels";
import { LangMessages, LangMessage as ConversationMessage } from "../messages.js";
class GoogleLang extends LanguageProvider {
constructor(options) {
const modelName = options.model || "gemini-2.0-flash";
super(modelName);
__publicField(this, "_apiKey");
__publicField(this, "_model");
__publicField(this, "_systemPrompt");
__publicField(this, "_maxTokens");
__publicField(this, "modelInfo");
const modelInfo = models.id(modelName);
if (!modelInfo) {
console.error(`Invalid Google model: ${modelName}. Model not found in aimodels database.`);
}
this.modelInfo = modelInfo;
this._apiKey = options.apiKey;
this._model = modelName;
this._systemPrompt = options.systemPrompt || "";
this._maxTokens = options.maxTokens;
}
async ask(prompt, options) {
const messages = new LangMessages();
if (this._systemPrompt) {
messages.push(new ConversationMessage("user", this._systemPrompt));
}
messages.push(new ConversationMessage("user", prompt));
return await this.chat(messages, options);
}
async chat(messages, options) {
throw new Error("Not implemented");
}
transformMessagesForProvider(messages) {
return messages.map((msg) => {
if (msg.role === "tool-results" && Array.isArray(msg.content)) {
return {
role: "user",
parts: msg.content.map((tr) => ({
functionResponse: {
name: tr.name,
response: typeof tr.result === "object" && tr.result !== null ? tr.result : { result: tr.result }
}
}))
};
}
const contentAny = msg.content;
if (Array.isArray(contentAny)) {
return {
role: msg.role === "assistant" ? "model" : "user",
parts: this.mapPartsToGemini(contentAny)
};
}
return {
role: msg.role === "assistant" ? "model" : "user",
parts: [{ text: msg.content }]
};
});
}
mapPartsToGemini(parts) {
const out = [];
for (const p of parts) {
if (p.type === "text") {
out.push({ text: p.text });
} else if (p.type === "image") {
const inlineData = this.imageInputToGeminiInlineData(p.image);
out.push({ inlineData });
}
}
return out;
}
imageInputToGeminiInlineData(image) {
const kind = image.kind;
if (kind === "base64") {
const base64 = image.base64;
const mimeType = image.mimeType || "image/png";
return { mimeType, data: base64 };
}
if (kind === "url") {
const url = image.url;
if (url.startsWith("data:")) {
const match = url.match(/^data:([^;]+);base64,(.*)$/);
if (!match) throw new Error("Invalid data URL for Gemini image");
const mimeType = match[1];
const data = match[2];
return { mimeType, data };
}
throw new Error("Gemini inline image requires base64 or data URL. Provide base64+mimeType or a data: URL.");
}
if (kind === "bytes" || kind === "blob") {
throw new Error("Gemini image input requires base64. Convert bytes/blob to base64 first.");
}
throw new Error("Unknown image input kind for Gemini");
}
}
export {
GoogleLang
};
//# sourceMappingURL=google-lang.js.map