From e1e01fe1c702e33509a276eb1da6efc720c21164 Mon Sep 17 00:00:00 2001 From: polwex Date: Wed, 23 Jul 2025 06:44:51 +0700 Subject: m --- src/gemini.ts | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'src/gemini.ts') diff --git a/src/gemini.ts b/src/gemini.ts index 5c7267b..aac5338 100644 --- a/src/gemini.ts +++ b/src/gemini.ts @@ -6,6 +6,7 @@ import { createUserContent, GoogleGenAI, type Content, + type ContentListUnion, type GeneratedImage, type GeneratedVideo, type Part, @@ -81,6 +82,17 @@ export default class GeminiAPI implements AIModelAPI { return { ok: part }; } else return { ok: createPartFromBase64(imageString, mimeType) }; } + async inlineImage(imageURI: URL): AsyncRes { + try { + const imgdata = await fetch(imageURI); + const imageArrayBuffer = await imgdata.arrayBuffer(); + const base64ImageData = Buffer.from(imageArrayBuffer).toString("base64"); + const mimeType = imgdata.headers.get("content-type") || "image/jpeg"; + return { ok: { inlineData: { mimeType, data: base64ImageData } } }; + } catch (e) { + return { error: `${e}` }; + } + } public buildInput(tokens: InputToken[]): Result { try { const input = createUserContent( @@ -100,11 +112,21 @@ export default class GeminiAPI implements AIModelAPI { } } - async send(input: string | Content, systemPrompt?: string): AsyncRes { + async send( + input: string | InputToken[], + systemPrompt?: string, + ): AsyncRes { + let contents: ContentListUnion; + if (typeof input === "string") contents = input; + else { + const built = this.buildInput(input); + if ("error" in built) return built; + else contents = built.ok; + } try { const opts = { model: this.model, - contents: input, + contents, }; const fopts = systemPrompt ? { ...opts, config: { systemInstruction: systemPrompt } } @@ -117,13 +139,20 @@ export default class GeminiAPI implements AIModelAPI { } } async stream( - input: string | Content, + input: string | InputToken[], handler: (s: string) => void, systemPrompt?: string, ) { + let contents: ContentListUnion; + if (typeof input === "string") contents = input; + else { + const built = this.buildInput(input); + if ("error" in built) return built; + else contents = built.ok; + } const opts = { model: this.model, - contents: input, + contents, }; const fopts = systemPrompt ? { ...opts, config: { systemInstruction: systemPrompt } } -- cgit v1.2.3