|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | import { AssertionError } from 'assert';
|
18 |
| -import sharp, { SharpOptions } from 'sharp'; |
| 18 | +import sharp from 'sharp'; |
19 | 19 | import superstruct from 'superstruct';
|
20 | 20 |
|
21 | 21 | import { omitKey, omitUndefinedValues } from '../utils';
|
@@ -56,6 +56,7 @@ import { operationTint } from './tint';
|
56 | 56 | import { operationGrayScale } from './grayscale';
|
57 | 57 | import { operationModulate } from './modulate';
|
58 | 58 | import { operationLinear } from './linear';
|
| 59 | +import { callGenkit, operationGenkit } from './genkit'; |
59 | 60 |
|
60 | 61 | export * from './input';
|
61 | 62 | export * from './output';
|
@@ -115,6 +116,7 @@ const builders: { [key: string]: OperationBuilder } = {
|
115 | 116 | grayscale: operationGrayScale,
|
116 | 117 | modulate: operationModulate,
|
117 | 118 | linear: operationLinear,
|
| 119 | + genkit: operationGenkit, |
118 | 120 | };
|
119 | 121 |
|
120 | 122 | export function builderForOperation(operation: Operation): OperationBuilder {
|
@@ -259,8 +261,27 @@ export async function applyValidatedOperation(
|
259 | 261 | );
|
260 | 262 | for (let i = 0; i < builtOperation.actions.length; i++) {
|
261 | 263 | const action = builtOperation.actions[i];
|
| 264 | + |
| 265 | + // Handle our custom external API action. |
| 266 | + if (action.method === 'genkitCall' && currentInstance) { |
| 267 | + // Retrieve the prompt from the action arguments. |
| 268 | + const promptParam = action.arguments[0]; |
| 269 | + const buffer = await currentInstance.toBuffer(); |
| 270 | + const base64Image = buffer.toString('base64'); |
| 271 | + // Call the external API with the image and prompt. |
| 272 | + const newBase64 = await callGenkit({ |
| 273 | + image: base64Image, |
| 274 | + prompt: promptParam, |
| 275 | + }); |
| 276 | + const newBuffer = Buffer.from(newBase64, 'base64'); |
| 277 | + currentInstance = sharp(newBuffer); |
| 278 | + continue; |
| 279 | + } |
| 280 | + |
262 | 281 | if (action.method == 'constructor') {
|
263 |
| - currentInstance = sharp(...(action.arguments as SharpOptions[])); |
| 282 | + currentInstance = sharp( |
| 283 | + ...(action.arguments as Parameters<typeof sharp>), |
| 284 | + ); |
264 | 285 | } else if (currentInstance != null) {
|
265 | 286 | currentInstance = (
|
266 | 287 | currentInstance[action.method] as (...args: unknown[]) => sharp.Sharp
|
|
0 commit comments