Skip to content

Commit c04303c

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

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed
Lines changed: 47 additions & 0 deletions
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+
// Add any parameters for your external API if needed.
26+
});
27+
28+
export const operationGenkit: OperationBuilder = {
29+
name,
30+
struct,
31+
async build(
32+
operation: ValidatedOperation,
33+
fileMetadata: sharp.Metadata | null,
34+
) {
35+
// Instead of returning a standard sharp method action,
36+
// return a custom action that signals the external API call.
37+
return [
38+
{
39+
method: 'genkitCall',
40+
arguments: [],
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

Lines changed: 21 additions & 2 deletions
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,25 @@ 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+
// Convert the current image buffer to a base64 string.
268+
const buffer = await currentInstance.toBuffer();
269+
const base64Image = buffer.toString('base64');
270+
// Call the external API (here, the mock function).
271+
const newBase64 = await callGenkit(base64Image);
272+
// Convert the returned base64 string back to a buffer.
273+
const newBuffer = Buffer.from(newBase64, 'base64');
274+
// Wrap the new buffer in a sharp instance.
275+
currentInstance = sharp(newBuffer);
276+
continue; // Move on to the next action.
277+
}
278+
262279
if (action.method == 'constructor') {
263-
currentInstance = sharp(...(action.arguments as SharpOptions[]));
280+
currentInstance = sharp(
281+
...(action.arguments as Parameters<typeof sharp>),
282+
);
264283
} else if (currentInstance != null) {
265284
currentInstance = (
266285
currentInstance[action.method] as (...args: unknown[]) => sharp.Sharp

0 commit comments

Comments
 (0)