aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/FireflyManager.ts
blob: 55fa1e4612a3a459958d8898a2c4d0eddd3b8fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { DashUploadUtils } from '../DashUploadUtils';
import { _invalid, _success, Method } from '../RouteManager';
import ApiManager, { Registration } from './ApiManager';

export default class FireflyManager extends ApiManager {
    getBearerToken = () =>
        fetch('https://ims-na1.adobelogin.com/ims/token/v3', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: `grant_type=client_credentials&client_id=${process.env._CLIENT_FIREFLY_CLIENT_ID}&client_secret=${process.env._CLIENT_FIREFLY_SECRET}&scope=openid,AdobeID,session,additional_info,read_organizations,firefly_api,ff_apis`,
        }).catch(error => {
            console.error('Error:', error);
            return '';
        });
    // askFirefly = (prompt: string = 'a realistic illustration of a cat coding') => {
    //     const fetched = this.getBearerToken().then(response =>
    //         response.json().then((data: { access_token: string }) =>
    //             fetch('https://firefly-api.adobe.io/v3/images/generate', {
    //                 method: 'POST',
    //                 headers: [
    //                     ['Content-Type', 'application/json'],
    //                     ['Accept', 'application/json'],
    //                     ['x-api-key', process.env._CLIENT_FIREFLY_CLIENT_ID ?? ''],
    //                     ['Authorization', `Bearer ${data.access_token}`],
    //                 ],
    //                 body: `{  "prompt": "${prompt}" }`,
    //             })
    //                 .then(response => response.json().then(json => JSON.stringify((json.outputs?.[0] as { image: { url: string } })?.image)))
    //                 .catch(error => {
    //                     console.error('Error:', error);
    //                     return '';
    //                 })
    //         )
    //     );
    //     return fetched;
    // };

    askFirefly = (prompt: string = 'a realistic illustration of a cat coding', uploadId: string, strength: number) => {
        const fetched = this.getBearerToken().then(response =>
            response.json().then((data: { access_token: string }) => {
                const body: any = {
                    prompt: prompt,
                    structure: {
                        strength: strength,
                        imageReference: {
                            source: {
                                uploadId: uploadId,
                            },
                        },
                    },
                };
                return fetch('https://firefly-api.adobe.io/v3/images/generate', {
                    method: 'POST',
                    headers: [
                        ['Content-Type', 'application/json'],
                        ['Accept', 'application/json'],
                        ['x-api-key', process.env._CLIENT_FIREFLY_CLIENT_ID ?? ''],
                        ['Authorization', `Bearer ${data.access_token}`],
                    ],
                    body: JSON.stringify(body),
                })
                    .then(response => response.json().then(json => JSON.stringify((json.outputs?.[0] as { image: { url: string } })?.image)))
                    .catch(error => {
                        console.error('Error:', error);
                        return '';
                    });
            })
        );
        return fetched;
    };

    uploadImageToFirefly = (image: Blob) => {
        const fetched = this.getBearerToken().then(response =>
            response.json().then((data: { access_token: string }) =>
                fetch('https://firefly-api.adobe.io/v3/uploads', {
                    method: 'POST',
                    headers: [
                        ['Content-Type', image.type],
                        ['x-api-key', process.env._CLIENT_FIREFLY_CLIENT_ID ?? ''],
                        ['Authorization', `Bearer ${data.access_token}`],
                    ],
                    body: image,
                })
                    .then(response => response.json())
                    .then(data => data.uploadId) // Extract the uploadId from the response
                    .catch(error => {
                        console.error('Error:', error);
                        return '';
                    })
            )
        );
        return fetched;
    };

    protected initialize(register: Registration): void {
        register({
            method: Method.POST,
            subscription: '/queryFireflyImage',
            secureHandler: async ({ req, res }) => {
                const { prompt, imageBlob, strength = 0.5 } = req.body;
                const uploadId = imageBlob ? await this.uploadImageToFirefly(imageBlob) : null;
                this.askFirefly(prompt, uploadId, strength).then(fire =>
                    DashUploadUtils.UploadImage(JSON.parse(fire).url).then(info => {
                        if (info instanceof Error) _invalid(res, info.message);
                        else _success(res, info.accessPaths.agnostic.client);
                    })
                );
            },
        });
    }
}