diff options
| author | polwex <polwex@sortug.com> | 2025-07-23 06:44:51 +0700 |
|---|---|---|
| committer | polwex <polwex@sortug.com> | 2025-07-23 06:44:51 +0700 |
| commit | e1e01fe1c702e33509a276eb1da6efc720c21164 (patch) | |
| tree | 1a3e0c8cf180a2e82ae8c4ecac62298ae865207b /src/gemini.ts | |
| parent | 9766782648617e232fbc4e40ea96a0e567c7cc73 (diff) | |
m
Diffstat (limited to 'src/gemini.ts')
| -rw-r--r-- | src/gemini.ts | 37 |
1 files changed, 33 insertions, 4 deletions
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<Part> { + 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<Content> { try { const input = createUserContent( @@ -100,11 +112,21 @@ export default class GeminiAPI implements AIModelAPI { } } - async send(input: string | Content, systemPrompt?: string): AsyncRes<string> { + async send( + input: string | InputToken[], + systemPrompt?: string, + ): AsyncRes<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; + } 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 } } |
