@agent-media/providers 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to the Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,8 @@
1
+ import type { MediaProvider } from '@agent-media/core';
2
+ /**
3
+ * Fal.ai provider for image generation and processing
4
+ * Requires FAL_API_KEY environment variable
5
+ */
6
+ export declare const falProvider: MediaProvider;
7
+ export default falProvider;
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fal/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,aAAa,EAMd,MAAM,mBAAmB,CAAC;AAe3B;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAE,aAuCzB,CAAC;AA+HF,eAAe,WAAW,CAAC"}
@@ -0,0 +1,133 @@
1
+ import { createFal } from '@ai-sdk/fal';
2
+ import { generateImage } from 'ai';
3
+ import { writeFile, readFile, stat } from 'node:fs/promises';
4
+ import { createSuccess, createError, ensureOutputDir, generateOutputFilename, getOutputPath, ErrorCodes, } from '@agent-media/core';
5
+ /**
6
+ * Actions supported by the fal provider
7
+ */
8
+ const SUPPORTED_ACTIONS = ['generate', 'remove-background'];
9
+ /**
10
+ * Fal.ai provider for image generation and processing
11
+ * Requires FAL_API_KEY environment variable
12
+ */
13
+ export const falProvider = {
14
+ name: 'fal',
15
+ supports(action) {
16
+ return SUPPORTED_ACTIONS.includes(action);
17
+ },
18
+ async execute(actionConfig, context) {
19
+ const apiKey = process.env['FAL_API_KEY'];
20
+ if (!apiKey) {
21
+ return createError(ErrorCodes.API_ERROR, 'FAL_API_KEY environment variable is not set');
22
+ }
23
+ try {
24
+ await ensureOutputDir(context.outputDir);
25
+ switch (actionConfig.action) {
26
+ case 'generate':
27
+ return await executeGenerate(actionConfig.options, context, apiKey);
28
+ case 'remove-background':
29
+ return await executeRemoveBackground(actionConfig.options, context, apiKey);
30
+ default:
31
+ return createError(ErrorCodes.INVALID_INPUT, `Action '${actionConfig.action}' not supported by fal provider`);
32
+ }
33
+ }
34
+ catch (error) {
35
+ const message = error instanceof Error ? error.message : String(error);
36
+ return createError(ErrorCodes.PROVIDER_ERROR, message);
37
+ }
38
+ },
39
+ };
40
+ /**
41
+ * Execute generate action using fal.ai flux model via AI SDK
42
+ */
43
+ async function executeGenerate(options, context, apiKey) {
44
+ const { prompt, width = 1024, height = 1024 } = options;
45
+ if (!prompt) {
46
+ return createError(ErrorCodes.INVALID_INPUT, 'Prompt is required for image generation');
47
+ }
48
+ const fal = createFal({ apiKey });
49
+ const { image } = await generateImage({
50
+ model: fal.image('fal-ai/flux/schnell'),
51
+ prompt,
52
+ size: `${width}x${height}`,
53
+ });
54
+ const outputFilename = generateOutputFilename('png', 'generated');
55
+ const outputPath = getOutputPath(context.outputDir, outputFilename);
56
+ await writeFile(outputPath, image.uint8Array);
57
+ const stats = await stat(outputPath);
58
+ return createSuccess({
59
+ mediaType: 'image',
60
+ action: 'generate',
61
+ provider: 'fal',
62
+ outputPath: outputPath,
63
+ mime: 'image/png',
64
+ bytes: stats.size,
65
+ });
66
+ }
67
+ /**
68
+ * Execute remove-background action using fal.ai birefnet/v2 model
69
+ * Uses direct API call since birefnet is an image processing model, not generative
70
+ */
71
+ async function executeRemoveBackground(options, context, apiKey) {
72
+ const { input } = options;
73
+ if (!input?.source) {
74
+ return createError(ErrorCodes.INVALID_INPUT, 'Input source is required for background removal');
75
+ }
76
+ // Prepare the image URL - birefnet requires a URL
77
+ let imageUrl;
78
+ if (input.isUrl) {
79
+ imageUrl = input.source;
80
+ }
81
+ else {
82
+ // Convert local file to data URI
83
+ const buffer = await readFile(input.source);
84
+ const base64 = buffer.toString('base64');
85
+ // Detect mime type from extension
86
+ const ext = input.source.toLowerCase().split('.').pop();
87
+ const mimeType = ext === 'png' ? 'image/png' : ext === 'webp' ? 'image/webp' : 'image/jpeg';
88
+ imageUrl = `data:${mimeType};base64,${base64}`;
89
+ }
90
+ // Call birefnet/v2 directly via fal.run API
91
+ const response = await fetch('https://fal.run/fal-ai/birefnet/v2', {
92
+ method: 'POST',
93
+ headers: {
94
+ 'Authorization': `Key ${apiKey}`,
95
+ 'Content-Type': 'application/json',
96
+ },
97
+ body: JSON.stringify({
98
+ image_url: imageUrl,
99
+ model: 'General Use (Light)',
100
+ output_format: 'png',
101
+ refine_foreground: true,
102
+ }),
103
+ });
104
+ if (!response.ok) {
105
+ const errorText = await response.text();
106
+ return createError(ErrorCodes.API_ERROR, `Fal API error: ${response.status} - ${errorText}`);
107
+ }
108
+ const result = await response.json();
109
+ if (!result.image?.url) {
110
+ return createError(ErrorCodes.PROVIDER_ERROR, 'No image returned from background removal');
111
+ }
112
+ // Download the processed image
113
+ const imageResponse = await fetch(result.image.url);
114
+ if (!imageResponse.ok) {
115
+ return createError(ErrorCodes.NETWORK_ERROR, `Failed to download processed image: ${imageResponse.statusText}`);
116
+ }
117
+ const arrayBuffer = await imageResponse.arrayBuffer();
118
+ const buffer = Buffer.from(arrayBuffer);
119
+ const outputFilename = generateOutputFilename('png', 'nobg');
120
+ const outputPath = getOutputPath(context.outputDir, outputFilename);
121
+ await writeFile(outputPath, buffer);
122
+ const stats = await stat(outputPath);
123
+ return createSuccess({
124
+ mediaType: 'image',
125
+ action: 'remove-background',
126
+ provider: 'fal',
127
+ outputPath: outputPath,
128
+ mime: 'image/png',
129
+ bytes: stats.size,
130
+ });
131
+ }
132
+ export default falProvider;
133
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/fal/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AACnC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAS7D,OAAO,EACL,aAAa,EACb,WAAW,EACX,eAAe,EACf,sBAAsB,EACtB,aAAa,EACb,UAAU,GACX,MAAM,mBAAmB,CAAC;AAE3B;;GAEG;AACH,MAAM,iBAAiB,GAAG,CAAC,UAAU,EAAE,mBAAmB,CAAC,CAAC;AAE5D;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAkB;IACxC,IAAI,EAAE,KAAK;IAEX,QAAQ,CAAC,MAAc;QACrB,OAAO,iBAAiB,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;IAED,KAAK,CAAC,OAAO,CACX,YAA2B,EAC3B,OAAsB;QAEtB,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAE1C,IAAI,CAAC,MAAM,EAAE,CAAC;YAC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CAAC,aAAa,EAAE,uCAAuC,aAAa,CAAC,UAAU,EAAE,CAAC,CAAC;IAClH,CAAC;IAED,MAAM,WAAW,GAAG,MAAM,aAAa,CAAC,WAAW,EAAE,CAAC;IACtD,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAExC,MAAM,cAAc,GAAG,sBAAsB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAC7D,MAAM,UAAU,GAAG,aAAa,CAAC,OAAO,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;IAEpE,MAAM,SAAS,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAEpC,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,CAAC;IAErC,OAAO,aAAa,CAAC;QACnB,SAAS,EAAE,OAAO;QAClB,MAAM,EAAE,mBAAmB;QAC3B,QAAQ,EAAE,KAAK;QACf,UAAU,EAAE,UAAU;QACtB,IAAI,EAAE,WAAW;QACjB,KAAK,EAAE,KAAK,CAAC,IAAI;KAClB,CAAC,CAAC;AACL,CAAC;AAED,eAAe,WAAW,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { MediaProvider } from '@agent-media/core';
2
+ /**
3
+ * File provider (fal.ai) for image generation and processing
4
+ * Requires FILE_API_KEY environment variable
5
+ */
6
+ export declare const fileProvider: MediaProvider;
7
+ export default fileProvider;
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EAId,MAAM,mBAAmB,CAAC;AAQ3B;;;GAGG;AACH,eAAO,MAAM,YAAY,EAAE,aA0B1B,CAAC;AAEF,eAAe,YAAY,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { createError, ErrorCodes } from '@agent-media/core';
2
+ /**
3
+ * Actions supported by the file provider
4
+ */
5
+ const SUPPORTED_ACTIONS = ['generate', 'remove-background'];
6
+ /**
7
+ * File provider (fal.ai) for image generation and processing
8
+ * Requires FILE_API_KEY environment variable
9
+ */
10
+ export const fileProvider = {
11
+ name: 'file',
12
+ supports(action) {
13
+ return SUPPORTED_ACTIONS.includes(action);
14
+ },
15
+ async execute(actionConfig, _context) {
16
+ const apiKey = process.env['FILE_API_KEY'];
17
+ if (!apiKey) {
18
+ return createError(ErrorCodes.API_ERROR, 'FILE_API_KEY environment variable is not set');
19
+ }
20
+ // TODO: Implement file provider API calls
21
+ return createError(ErrorCodes.PROVIDER_ERROR, `File provider not yet implemented for action: ${actionConfig.action}`);
22
+ },
23
+ };
24
+ export default fileProvider;
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/file/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE5D;;GAEG;AACH,MAAM,iBAAiB,GAAG,CAAC,UAAU,EAAE,mBAAmB,CAAC,CAAC;AAE5D;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAkB;IACzC,IAAI,EAAE,MAAM;IAEZ,QAAQ,CAAC,MAAc;QACrB,OAAO,iBAAiB,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;IAED,KAAK,CAAC,OAAO,CACX,YAA2B,EAC3B,QAAuB;QAEvB,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;QAE3C,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,WAAW,CAChB,UAAU,CAAC,SAAS,EACpB,8CAA8C,CAC/C,CAAC;QACJ,CAAC;QAED,0CAA0C;QAC1C,OAAO,WAAW,CAChB,UAAU,CAAC,cAAc,EACzB,iDAAiD,YAAY,CAAC,MAAM,EAAE,CACvE,CAAC;IACJ,CAAC;CACF,CAAC;AAEF,eAAe,YAAY,CAAC"}
@@ -0,0 +1,22 @@
1
+ export { localProvider } from './local/index.js';
2
+ export { falProvider } from './fal/index.js';
3
+ export { replicateProvider } from './replicate/index.js';
4
+ export { runpodProvider } from './runpod/index.js';
5
+ /**
6
+ * All available providers
7
+ */
8
+ export declare const providers: {
9
+ local: import("@agent-media/core").MediaProvider;
10
+ fal: import("@agent-media/core").MediaProvider;
11
+ replicate: import("@agent-media/core").MediaProvider;
12
+ runpod: import("@agent-media/core").MediaProvider;
13
+ };
14
+ /**
15
+ * Register all providers with the global registry
16
+ */
17
+ export declare function registerAllProviders(): void;
18
+ /**
19
+ * Register only the local provider (for minimal setup)
20
+ */
21
+ export declare function registerLocalProvider(): void;
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD;;GAEG;AACH,eAAO,MAAM,SAAS;;;;;CAKrB,CAAC;AAEF;;GAEG;AACH,wBAAgB,oBAAoB,IAAI,IAAI,CAK3C;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,IAAI,CAE5C"}
package/dist/index.js ADDED
@@ -0,0 +1,35 @@
1
+ import { globalRegistry } from '@agent-media/core';
2
+ import { localProvider } from './local/index.js';
3
+ import { falProvider } from './fal/index.js';
4
+ import { replicateProvider } from './replicate/index.js';
5
+ import { runpodProvider } from './runpod/index.js';
6
+ // Export individual providers
7
+ export { localProvider } from './local/index.js';
8
+ export { falProvider } from './fal/index.js';
9
+ export { replicateProvider } from './replicate/index.js';
10
+ export { runpodProvider } from './runpod/index.js';
11
+ /**
12
+ * All available providers
13
+ */
14
+ export const providers = {
15
+ local: localProvider,
16
+ fal: falProvider,
17
+ replicate: replicateProvider,
18
+ runpod: runpodProvider,
19
+ };
20
+ /**
21
+ * Register all providers with the global registry
22
+ */
23
+ export function registerAllProviders() {
24
+ globalRegistry.register(localProvider);
25
+ globalRegistry.register(falProvider);
26
+ globalRegistry.register(replicateProvider);
27
+ globalRegistry.register(runpodProvider);
28
+ }
29
+ /**
30
+ * Register only the local provider (for minimal setup)
31
+ */
32
+ export function registerLocalProvider() {
33
+ globalRegistry.register(localProvider);
34
+ }
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD,8BAA8B;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD;;GAEG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,KAAK,EAAE,aAAa;IACpB,GAAG,EAAE,WAAW;IAChB,SAAS,EAAE,iBAAiB;IAC5B,MAAM,EAAE,cAAc;CACvB,CAAC;AAEF;;GAEG;AACH,MAAM,UAAU,oBAAoB;IAClC,cAAc,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;IACvC,cAAc,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IACrC,cAAc,CAAC,QAAQ,CAAC,iBAAiB,CAAC,CAAC;IAC3C,cAAc,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;AAC1C,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB;IACnC,cAAc,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;AACzC,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { MediaProvider } from '@agent-media/core';
2
+ /**
3
+ * Local provider using sharp for image manipulation
4
+ * Supports: resize, convert
5
+ * Zero external dependencies beyond sharp
6
+ */
7
+ export declare const localProvider: MediaProvider;
8
+ export default localProvider;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/local/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,aAAa,EAQd,MAAM,mBAAmB,CAAC;AA0D3B;;;;GAIG;AACH,eAAO,MAAM,aAAa,EAAE,aAgC3B,CAAC;AAsOF,eAAe,aAAa,CAAC"}
@@ -0,0 +1,248 @@
1
+ import sharp from 'sharp';
2
+ import { stat } from 'node:fs/promises';
3
+ import { createReadStream } from 'node:fs';
4
+ import { createSuccess, createError, ensureOutputDir, generateOutputFilename, getOutputPath, ErrorCodes, } from '@agent-media/core';
5
+ /**
6
+ * Actions supported by the local provider
7
+ */
8
+ const SUPPORTED_ACTIONS = ['resize', 'convert', 'extend'];
9
+ /**
10
+ * MIME types for image formats
11
+ */
12
+ const MIME_TYPES = {
13
+ png: 'image/png',
14
+ jpg: 'image/jpeg',
15
+ jpeg: 'image/jpeg',
16
+ webp: 'image/webp',
17
+ };
18
+ /**
19
+ * Get the output format extension
20
+ */
21
+ function getFormatExtension(format) {
22
+ return format === 'jpeg' ? 'jpg' : format;
23
+ }
24
+ /**
25
+ * Get input stream from source (file path or URL)
26
+ */
27
+ async function getInputBuffer(source, isUrl) {
28
+ if (isUrl) {
29
+ const response = await fetch(source);
30
+ if (!response.ok) {
31
+ throw new Error(`Failed to fetch URL: ${response.statusText}`);
32
+ }
33
+ const arrayBuffer = await response.arrayBuffer();
34
+ return Buffer.from(arrayBuffer);
35
+ }
36
+ // Read from file
37
+ return new Promise((resolve, reject) => {
38
+ const chunks = [];
39
+ const stream = createReadStream(source);
40
+ stream.on('data', (chunk) => chunks.push(chunk));
41
+ stream.on('end', () => resolve(Buffer.concat(chunks)));
42
+ stream.on('error', reject);
43
+ });
44
+ }
45
+ /**
46
+ * Local provider using sharp for image manipulation
47
+ * Supports: resize, convert
48
+ * Zero external dependencies beyond sharp
49
+ */
50
+ export const localProvider = {
51
+ name: 'local',
52
+ supports(action) {
53
+ return SUPPORTED_ACTIONS.includes(action);
54
+ },
55
+ async execute(actionConfig, context) {
56
+ try {
57
+ await ensureOutputDir(context.outputDir);
58
+ switch (actionConfig.action) {
59
+ case 'resize':
60
+ return await executeResize(actionConfig.options, context);
61
+ case 'convert':
62
+ return await executeConvert(actionConfig.options, context);
63
+ case 'extend':
64
+ return await executeExtend(actionConfig.options, context);
65
+ default:
66
+ return createError(ErrorCodes.INVALID_INPUT, `Action '${actionConfig.action}' not supported by local provider`);
67
+ }
68
+ }
69
+ catch (error) {
70
+ const message = error instanceof Error ? error.message : String(error);
71
+ return createError(ErrorCodes.PROVIDER_ERROR, message);
72
+ }
73
+ },
74
+ };
75
+ /**
76
+ * Execute resize action
77
+ */
78
+ async function executeResize(options, context) {
79
+ const { input, width, height, maintainAspectRatio = true } = options;
80
+ if (!width && !height) {
81
+ return createError(ErrorCodes.INVALID_INPUT, 'At least one of width or height must be specified');
82
+ }
83
+ const inputBuffer = await getInputBuffer(input.source, input.isUrl);
84
+ let pipeline = sharp(inputBuffer);
85
+ // Get original metadata to determine output format
86
+ const metadata = await pipeline.metadata();
87
+ const outputFormat = metadata.format || 'png';
88
+ pipeline = pipeline.resize({
89
+ width: width,
90
+ height: height,
91
+ fit: maintainAspectRatio ? 'inside' : 'fill',
92
+ withoutEnlargement: true,
93
+ });
94
+ const outputFilename = generateOutputFilename(getFormatExtension(outputFormat), 'resized');
95
+ const outputPath = getOutputPath(context.outputDir, outputFilename);
96
+ await pipeline.toFile(outputPath);
97
+ const stats = await stat(outputPath);
98
+ return createSuccess({
99
+ mediaType: 'image',
100
+ action: 'resize',
101
+ provider: 'local',
102
+ outputPath: outputPath,
103
+ mime: MIME_TYPES[outputFormat] ?? 'application/octet-stream',
104
+ bytes: stats.size,
105
+ });
106
+ }
107
+ /**
108
+ * Check if source is an SVG file
109
+ */
110
+ function isSvgSource(source) {
111
+ return source.toLowerCase().endsWith('.svg');
112
+ }
113
+ /**
114
+ * Execute convert action
115
+ */
116
+ async function executeConvert(options, context) {
117
+ const { input, format, quality = 80, dpi = 72, width, height } = options;
118
+ const isSvg = isSvgSource(input.source);
119
+ const inputBuffer = await getInputBuffer(input.source, input.isUrl);
120
+ // For SVG files, we need to specify density and optionally resize
121
+ // Sharp uses density (DPI) when rasterizing SVGs
122
+ let pipeline;
123
+ if (isSvg) {
124
+ // When converting SVG, use the specified DPI for rasterization
125
+ // If width/height provided, Sharp will resize accordingly
126
+ const sharpOptions = {
127
+ density: dpi,
128
+ };
129
+ pipeline = sharp(inputBuffer, sharpOptions);
130
+ // If width or height specified, resize (maintaining aspect ratio)
131
+ if (width || height) {
132
+ pipeline = pipeline.resize({
133
+ width: width,
134
+ height: height,
135
+ fit: 'inside',
136
+ withoutEnlargement: false, // Allow enlargement for high-DPI output
137
+ });
138
+ }
139
+ }
140
+ else {
141
+ pipeline = sharp(inputBuffer);
142
+ }
143
+ // Apply format conversion with DPI metadata
144
+ switch (format) {
145
+ case 'png':
146
+ pipeline = pipeline.png();
147
+ break;
148
+ case 'jpg':
149
+ case 'jpeg':
150
+ pipeline = pipeline.jpeg({ quality });
151
+ break;
152
+ case 'webp':
153
+ pipeline = pipeline.webp({ quality });
154
+ break;
155
+ default:
156
+ return createError(ErrorCodes.INVALID_FORMAT, `Unsupported output format: ${format}`);
157
+ }
158
+ // Set output DPI metadata using withMetadata
159
+ pipeline = pipeline.withMetadata({
160
+ density: dpi,
161
+ });
162
+ const outputFilename = generateOutputFilename(getFormatExtension(format), 'converted');
163
+ const outputPath = getOutputPath(context.outputDir, outputFilename);
164
+ await pipeline.toFile(outputPath);
165
+ const stats = await stat(outputPath);
166
+ return createSuccess({
167
+ mediaType: 'image',
168
+ action: 'convert',
169
+ provider: 'local',
170
+ outputPath: outputPath,
171
+ mime: MIME_TYPES[format] ?? 'application/octet-stream',
172
+ bytes: stats.size,
173
+ });
174
+ }
175
+ /**
176
+ * Parse hex color to RGB object
177
+ */
178
+ function parseHexColor(hex) {
179
+ const cleanHex = hex.replace('#', '');
180
+ return {
181
+ r: parseInt(cleanHex.substring(0, 2), 16),
182
+ g: parseInt(cleanHex.substring(2, 4), 16),
183
+ b: parseInt(cleanHex.substring(4, 6), 16),
184
+ };
185
+ }
186
+ /**
187
+ * Execute extend action
188
+ * Extends the image canvas by adding padding on all sides with a solid background color.
189
+ * Also flattens any transparency in the original image to the specified color.
190
+ */
191
+ async function executeExtend(options, context) {
192
+ const { input, padding, color, dpi = 300 } = options;
193
+ const inputBuffer = await getInputBuffer(input.source, input.isUrl);
194
+ // Parse the background color
195
+ const bgColor = parseHexColor(color);
196
+ // Get input dimensions
197
+ const metadata = await sharp(inputBuffer).metadata();
198
+ const inputWidth = metadata.width || 0;
199
+ const inputHeight = metadata.height || 0;
200
+ // Calculate final dimensions with padding
201
+ const finalWidth = inputWidth + padding * 2;
202
+ const finalHeight = inputHeight + padding * 2;
203
+ // Create a solid background canvas with the specified color
204
+ // This ensures NO transparency anywhere in the final image
205
+ const background = await sharp({
206
+ create: {
207
+ width: finalWidth,
208
+ height: finalHeight,
209
+ channels: 3,
210
+ background: { r: bgColor.r, g: bgColor.g, b: bgColor.b },
211
+ },
212
+ })
213
+ .png()
214
+ .toBuffer();
215
+ // Flatten the input image onto the background color (removes transparency)
216
+ const flattenedInput = await sharp(inputBuffer)
217
+ .flatten({ background: { r: bgColor.r, g: bgColor.g, b: bgColor.b } })
218
+ .png()
219
+ .toBuffer();
220
+ // Composite the flattened image onto the background with padding offset
221
+ let pipeline = sharp(background).composite([
222
+ {
223
+ input: flattenedInput,
224
+ top: padding,
225
+ left: padding,
226
+ },
227
+ ]);
228
+ // Remove alpha channel and apply PNG format
229
+ pipeline = pipeline.removeAlpha().png();
230
+ // Set DPI metadata
231
+ pipeline = pipeline.withMetadata({
232
+ density: dpi,
233
+ });
234
+ const outputFilename = generateOutputFilename('png', 'extended');
235
+ const outputPath = getOutputPath(context.outputDir, outputFilename);
236
+ await pipeline.toFile(outputPath);
237
+ const stats = await stat(outputPath);
238
+ return createSuccess({
239
+ mediaType: 'image',
240
+ action: 'extend',
241
+ provider: 'local',
242
+ outputPath: outputPath,
243
+ mime: 'image/png',
244
+ bytes: stats.size,
245
+ });
246
+ }
247
+ export default localProvider;
248
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/local/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAW3C,OAAO,EACL,aAAa,EACb,WAAW,EACX,eAAe,EACf,sBAAsB,EACtB,aAAa,EACb,UAAU,GACX,MAAM,mBAAmB,CAAC;AAE3B;;GAEG;AACH,MAAM,iBAAiB,GAAG,CAAC,QAAQ,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;AAE1D;;GAEG;AACH,MAAM,UAAU,GAAgC;IAC9C,GAAG,EAAE,WAAW;IAChB,GAAG,EAAE,YAAY;IACjB,IAAI,EAAE,YAAY;IAClB,IAAI,EAAE,YAAY;CACnB,CAAC;AAEF;;GAEG;AACH,SAAS,kBAAkB,CAAC,MAAmB;IAC7C,OAAO,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CA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,QAAQ,CAAC,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC;IAExC,mBAAmB;IACnB,QAAQ,GAAG,QAAQ,CAAC,YAAY,CAAC;QAC/B,OAAO,EAAE,GAAG;KACb,CAAC,CAAC;IAEH,MAAM,cAAc,GAAG,sBAAsB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;IACjE,MAAM,UAAU,GAAG,aAAa,CAAC,OAAO,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;IAEpE,MAAM,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAElC,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,CAAC;IAErC,OAAO,aAAa,CAAC;QACnB,SAAS,EAAE,OAAO;QAClB,MAAM,EAAE,QAAQ;QAChB,QAAQ,EAAE,OAAO;QACjB,UAAU,EAAE,UAAU;QACtB,IAAI,EAAE,WAAW;QACjB,KAAK,EAAE,KAAK,CAAC,IAAI;KAClB,CAAC,CAAC;AACL,CAAC;AAED,eAAe,aAAa,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { MediaProvider } from '@agent-media/core';
2
+ /**
3
+ * Replicate provider for image generation and processing
4
+ * Requires REPLICATE_API_TOKEN environment variable
5
+ */
6
+ export declare const replicateProvider: MediaProvider;
7
+ export default replicateProvider;
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/replicate/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,aAAa,EAMd,MAAM,mBAAmB,CAAC;AAe3B;;;GAGG;AACH,eAAO,MAAM,iBAAiB,EAAE,aAuC/B,CAAC;AAgGF,eAAe,iBAAiB,CAAC"}
@@ -0,0 +1,111 @@
1
+ import { createReplicate } from '@ai-sdk/replicate';
2
+ import { generateImage } from 'ai';
3
+ import { writeFile, readFile, stat } from 'node:fs/promises';
4
+ import { createSuccess, createError, ensureOutputDir, generateOutputFilename, getOutputPath, ErrorCodes, } from '@agent-media/core';
5
+ /**
6
+ * Actions supported by the replicate provider
7
+ */
8
+ const SUPPORTED_ACTIONS = ['generate', 'remove-background'];
9
+ /**
10
+ * Replicate provider for image generation and processing
11
+ * Requires REPLICATE_API_TOKEN environment variable
12
+ */
13
+ export const replicateProvider = {
14
+ name: 'replicate',
15
+ supports(action) {
16
+ return SUPPORTED_ACTIONS.includes(action);
17
+ },
18
+ async execute(actionConfig, context) {
19
+ const apiToken = process.env['REPLICATE_API_TOKEN'];
20
+ if (!apiToken) {
21
+ return createError(ErrorCodes.API_ERROR, 'REPLICATE_API_TOKEN environment variable is not set');
22
+ }
23
+ try {
24
+ await ensureOutputDir(context.outputDir);
25
+ switch (actionConfig.action) {
26
+ case 'generate':
27
+ return await executeGenerate(actionConfig.options, context, apiToken);
28
+ case 'remove-background':
29
+ return await executeRemoveBackground(actionConfig.options, context, apiToken);
30
+ default:
31
+ return createError(ErrorCodes.INVALID_INPUT, `Action '${actionConfig.action}' not supported by replicate provider`);
32
+ }
33
+ }
34
+ catch (error) {
35
+ const message = error instanceof Error ? error.message : String(error);
36
+ return createError(ErrorCodes.PROVIDER_ERROR, message);
37
+ }
38
+ },
39
+ };
40
+ /**
41
+ * Execute generate action using Replicate flux-schnell model via AI SDK
42
+ */
43
+ async function executeGenerate(options, context, apiToken) {
44
+ const { prompt, width = 1024, height = 1024 } = options;
45
+ if (!prompt) {
46
+ return createError(ErrorCodes.INVALID_INPUT, 'Prompt is required for image generation');
47
+ }
48
+ const replicate = createReplicate({ apiToken });
49
+ const { image } = await generateImage({
50
+ model: replicate.image('black-forest-labs/flux-schnell'),
51
+ prompt,
52
+ size: `${width}x${height}`,
53
+ });
54
+ const outputFilename = generateOutputFilename('webp', 'generated');
55
+ const outputPath = getOutputPath(context.outputDir, outputFilename);
56
+ await writeFile(outputPath, image.uint8Array);
57
+ const stats = await stat(outputPath);
58
+ return createSuccess({
59
+ mediaType: 'image',
60
+ action: 'generate',
61
+ provider: 'replicate',
62
+ outputPath: outputPath,
63
+ mime: 'image/webp',
64
+ bytes: stats.size,
65
+ });
66
+ }
67
+ /**
68
+ * Execute remove-background action using Replicate BiRefNet model via AI SDK
69
+ */
70
+ async function executeRemoveBackground(options, context, apiToken) {
71
+ const { input } = options;
72
+ if (!input?.source) {
73
+ return createError(ErrorCodes.INVALID_INPUT, 'Input source is required for background removal');
74
+ }
75
+ const replicate = createReplicate({ apiToken });
76
+ // Prepare the image input
77
+ let imageBuffer;
78
+ if (input.isUrl) {
79
+ const response = await fetch(input.source);
80
+ if (!response.ok) {
81
+ return createError(ErrorCodes.NETWORK_ERROR, `Failed to fetch image: ${response.statusText}`);
82
+ }
83
+ const arrayBuffer = await response.arrayBuffer();
84
+ imageBuffer = Buffer.from(arrayBuffer);
85
+ }
86
+ else {
87
+ imageBuffer = await readFile(input.source);
88
+ }
89
+ // Use BiRefNet for background removal (same model as fal provider)
90
+ const { image } = await generateImage({
91
+ model: replicate.image('men1scus/birefnet:f74986db0355b58403ed20963af156525e2891ea3c2d499bfbfb2a28cd87c5d7'),
92
+ prompt: {
93
+ text: '',
94
+ images: [imageBuffer],
95
+ },
96
+ });
97
+ const outputFilename = generateOutputFilename('png', 'nobg');
98
+ const outputPath = getOutputPath(context.outputDir, outputFilename);
99
+ await writeFile(outputPath, image.uint8Array);
100
+ const stats = await stat(outputPath);
101
+ return createSuccess({
102
+ mediaType: 'image',
103
+ action: 'remove-background',
104
+ provider: 'replicate',
105
+ outputPath: outputPath,
106
+ mime: 'image/png',
107
+ bytes: stats.size,
108
+ });
109
+ }
110
+ export default replicateProvider;
111
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/replicate/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AACnC,OAAO,EAAE,SAAS,EAAE,Q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AM,KAAK,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,CAAC;IAErC,OAAO,aAAa,CAAC;QACnB,SAAS,EAAE,OAAO;QAClB,MAAM,EAAE,mBAAmB;QAC3B,QAAQ,EAAE,WAAW;QACrB,UAAU,EAAE,UAAU;QACtB,IAAI,EAAE,WAAW;QACjB,KAAK,EAAE,KAAK,CAAC,IAAI;KAClB,CAAC,CAAC;AACL,CAAC;AAED,eAAe,iBAAiB,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { MediaProvider } from '@agent-media/core';
2
+ /**
3
+ * Runpod provider for image generation and editing
4
+ * Requires RUNPOD_API_KEY environment variable
5
+ *
6
+ * Supported actions:
7
+ * - generate: Text-to-image using alibaba/wan-2.6
8
+ * - edit: Image-to-image editing using google/nano-banana-pro-edit
9
+ */
10
+ export declare const runpodProvider: MediaProvider;
11
+ export default runpodProvider;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runpod/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,aAAa,EAMd,MAAM,mBAAmB,CAAC;AAe3B;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,EAAE,aAuC5B,CAAC;AAmGF,eAAe,cAAc,CAAC"}
@@ -0,0 +1,117 @@
1
+ import { createRunpod } from '@runpod/ai-sdk-provider';
2
+ import { generateImage } from 'ai';
3
+ import { writeFile, readFile, stat } from 'node:fs/promises';
4
+ import { createSuccess, createError, ensureOutputDir, generateOutputFilename, getOutputPath, ErrorCodes, } from '@agent-media/core';
5
+ /**
6
+ * Actions supported by the runpod provider
7
+ */
8
+ const SUPPORTED_ACTIONS = ['generate', 'edit'];
9
+ /**
10
+ * Runpod provider for image generation and editing
11
+ * Requires RUNPOD_API_KEY environment variable
12
+ *
13
+ * Supported actions:
14
+ * - generate: Text-to-image using alibaba/wan-2.6
15
+ * - edit: Image-to-image editing using google/nano-banana-pro-edit
16
+ */
17
+ export const runpodProvider = {
18
+ name: 'runpod',
19
+ supports(action) {
20
+ return SUPPORTED_ACTIONS.includes(action);
21
+ },
22
+ async execute(actionConfig, context) {
23
+ const apiKey = process.env['RUNPOD_API_KEY'];
24
+ if (!apiKey) {
25
+ return createError(ErrorCodes.API_ERROR, 'RUNPOD_API_KEY environment variable is not set');
26
+ }
27
+ try {
28
+ await ensureOutputDir(context.outputDir);
29
+ switch (actionConfig.action) {
30
+ case 'generate':
31
+ return await executeGenerate(actionConfig.options, context, apiKey);
32
+ case 'edit':
33
+ return await executeEdit(actionConfig.options, context, apiKey);
34
+ default:
35
+ return createError(ErrorCodes.INVALID_INPUT, `Action '${actionConfig.action}' not supported by runpod provider`);
36
+ }
37
+ }
38
+ catch (error) {
39
+ const message = error instanceof Error ? error.message : String(error);
40
+ return createError(ErrorCodes.PROVIDER_ERROR, message);
41
+ }
42
+ },
43
+ };
44
+ /**
45
+ * Execute generate action using Runpod alibaba/wan-2.6 model via AI SDK
46
+ */
47
+ async function executeGenerate(options, context, apiKey) {
48
+ const { prompt, width = 1024, height = 1024 } = options;
49
+ if (!prompt) {
50
+ return createError(ErrorCodes.INVALID_INPUT, 'Prompt is required for image generation');
51
+ }
52
+ const runpod = createRunpod({ apiKey });
53
+ const { image } = await generateImage({
54
+ model: runpod.image('alibaba/wan-2.6'),
55
+ prompt,
56
+ size: `${width}x${height}`,
57
+ });
58
+ const outputFilename = generateOutputFilename('png', 'generated');
59
+ const outputPath = getOutputPath(context.outputDir, outputFilename);
60
+ await writeFile(outputPath, image.uint8Array);
61
+ const stats = await stat(outputPath);
62
+ return createSuccess({
63
+ mediaType: 'image',
64
+ action: 'generate',
65
+ provider: 'runpod',
66
+ outputPath: outputPath,
67
+ mime: 'image/png',
68
+ bytes: stats.size,
69
+ });
70
+ }
71
+ /**
72
+ * Execute edit action using Runpod google/nano-banana-pro-edit model via AI SDK
73
+ */
74
+ async function executeEdit(options, context, apiKey) {
75
+ const { input, prompt } = options;
76
+ if (!input?.source) {
77
+ return createError(ErrorCodes.INVALID_INPUT, 'Input source is required for image editing');
78
+ }
79
+ if (!prompt) {
80
+ return createError(ErrorCodes.INVALID_INPUT, 'Prompt is required for image editing');
81
+ }
82
+ const runpod = createRunpod({ apiKey });
83
+ // Prepare the image input
84
+ let imageBuffer;
85
+ if (input.isUrl) {
86
+ const response = await fetch(input.source);
87
+ if (!response.ok) {
88
+ return createError(ErrorCodes.NETWORK_ERROR, `Failed to fetch image: ${response.statusText}`);
89
+ }
90
+ const arrayBuffer = await response.arrayBuffer();
91
+ imageBuffer = Buffer.from(arrayBuffer);
92
+ }
93
+ else {
94
+ imageBuffer = await readFile(input.source);
95
+ }
96
+ const { image } = await generateImage({
97
+ model: runpod.image('google/nano-banana-pro-edit'),
98
+ prompt: {
99
+ text: prompt,
100
+ images: [imageBuffer],
101
+ },
102
+ });
103
+ const outputFilename = generateOutputFilename('png', 'edited');
104
+ const outputPath = getOutputPath(context.outputDir, outputFilename);
105
+ await writeFile(outputPath, image.uint8Array);
106
+ const stats = await stat(outputPath);
107
+ return createSuccess({
108
+ mediaType: 'image',
109
+ action: 'edit',
110
+ provider: 'runpod',
111
+ outputPath: outputPath,
112
+ mime: 'image/png',
113
+ bytes: stats.size,
114
+ });
115
+ }
116
+ export default runpodProvider;
117
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/runpod/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AACnC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAS7D,OAAO,EACL,aAAa,EACb,WAAW,EACX,eAAe,EACf,sBAAsB,EACtB,aAAa,EACb,UAAU,GACX,MAAM,mBAAmB,CAAC;AAE3B;;GAEG;AACH,MAAM,iBAAiB,GA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OAAO;QAClB,MAAM,EAAE,MAAM;QACd,QAAQ,EAAE,QAAQ;QAClB,UAAU,EAAE,UAAU;QACtB,IAAI,EAAE,WAAW;QACjB,KAAK,EAAE,KAAK,CAAC,IAAI;KAClB,CAAC,CAAC;AACL,CAAC;AAED,eAAe,cAAc,CAAC"}
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@agent-media/providers",
3
+ "version": "0.1.0",
4
+ "description": "Media providers for agent-media toolkit",
5
+ "license": "Apache-2.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/TimPietrusky/agent-media.git",
9
+ "directory": "packages/providers"
10
+ },
11
+ "type": "module",
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js"
18
+ },
19
+ "./local": {
20
+ "types": "./dist/local/index.d.ts",
21
+ "import": "./dist/local/index.js"
22
+ },
23
+ "./fal": {
24
+ "types": "./dist/fal/index.d.ts",
25
+ "import": "./dist/fal/index.js"
26
+ },
27
+ "./replicate": {
28
+ "types": "./dist/replicate/index.d.ts",
29
+ "import": "./dist/replicate/index.js"
30
+ },
31
+ "./runpod": {
32
+ "types": "./dist/runpod/index.d.ts",
33
+ "import": "./dist/runpod/index.js"
34
+ }
35
+ },
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "dependencies": {
40
+ "@ai-sdk/fal": "^2.0.6",
41
+ "@ai-sdk/replicate": "^2.0.6",
42
+ "@runpod/ai-sdk-provider": "^1.1.0",
43
+ "ai": "^6.0.33",
44
+ "sharp": "^0.33.0",
45
+ "@agent-media/core": "0.1.0"
46
+ },
47
+ "devDependencies": {
48
+ "@types/node": "^22.0.0",
49
+ "typescript": "^5.7.0"
50
+ },
51
+ "scripts": {
52
+ "build": "tsc",
53
+ "clean": "rm -rf dist",
54
+ "typecheck": "tsc --noEmit"
55
+ }
56
+ }