Skip to content

Commit 2e42059

Browse files
committed
feat: initial genkit feature draft
1 parent 7d650cd commit 2e42059

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2016-present Invertase Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import sharp from 'sharp';
18+
import superstruct from 'superstruct';
19+
import { OperationBuilder, ValidatedOperation } from '../types';
20+
21+
const name = 'genkit';
22+
23+
const struct = superstruct.object({
24+
operation: superstruct.literal(name),
25+
prompt: superstruct.string(), // require a prompt string
26+
});
27+
28+
export const operationGenkit: OperationBuilder = {
29+
name,
30+
struct,
31+
async build(
32+
operation: ValidatedOperation,
33+
fileMetadata: sharp.Metadata | null,
34+
) {
35+
const options = operation.options as { prompt: string };
36+
// Return a custom action with the prompt parameter.
37+
return [
38+
{
39+
method: 'genkitCall',
40+
arguments: [options.prompt],
41+
},
42+
];
43+
},
44+
};
45+
46+
// TODO: Genkit implementation
47+
export const callGenkit: (any) => Promise<any> = async (x: any) => x;

extensions/image-processing-api/functions/src/operations/index.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { AssertionError } from 'assert';
18-
import sharp, { SharpOptions } from 'sharp';
18+
import sharp from 'sharp';
1919
import superstruct from 'superstruct';
2020

2121
import { omitKey, omitUndefinedValues } from '../utils';
@@ -56,6 +56,7 @@ import { operationTint } from './tint';
5656
import { operationGrayScale } from './grayscale';
5757
import { operationModulate } from './modulate';
5858
import { operationLinear } from './linear';
59+
import { callGenkit, operationGenkit } from './genkit';
5960

6061
export * from './input';
6162
export * from './output';
@@ -115,6 +116,7 @@ const builders: { [key: string]: OperationBuilder } = {
115116
grayscale: operationGrayScale,
116117
modulate: operationModulate,
117118
linear: operationLinear,
119+
genkit: operationGenkit,
118120
};
119121

120122
export function builderForOperation(operation: Operation): OperationBuilder {
@@ -259,8 +261,27 @@ export async function applyValidatedOperation(
259261
);
260262
for (let i = 0; i < builtOperation.actions.length; i++) {
261263
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+
262281
if (action.method == 'constructor') {
263-
currentInstance = sharp(...(action.arguments as SharpOptions[]));
282+
currentInstance = sharp(
283+
...(action.arguments as Parameters<typeof sharp>),
284+
);
264285
} else if (currentInstance != null) {
265286
currentInstance = (
266287
currentInstance[action.method] as (...args: unknown[]) => sharp.Sharp

0 commit comments

Comments
 (0)