@slopmachine/core 0.1.26 → 0.2.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/CHANGELOG.md +7 -0
- package/README.md +22 -3
- package/dist/index.d.mts +169 -5
- package/dist/index.d.ts +169 -5
- package/dist/index.js +156 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +154 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +361 -24
package/dist/index.js
CHANGED
|
@@ -21,8 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
buildImageUrl: () => buildImageUrl,
|
|
24
|
+
buildPipelineUrl: () => buildPipelineUrl,
|
|
24
25
|
buildTextUrl: () => buildTextUrl,
|
|
25
26
|
buildVideoUrl: () => buildVideoUrl,
|
|
27
|
+
executePipeline: () => executePipeline,
|
|
26
28
|
interpolatePrompt: () => interpolatePrompt,
|
|
27
29
|
preloadImage: () => preloadImage,
|
|
28
30
|
preloadText: () => preloadText,
|
|
@@ -45,17 +47,40 @@ function interpolatePrompt(prompt, variables) {
|
|
|
45
47
|
function buildImageUrl(options) {
|
|
46
48
|
const {
|
|
47
49
|
bucketId,
|
|
50
|
+
pipelineId,
|
|
51
|
+
siloId,
|
|
52
|
+
prompt,
|
|
53
|
+
metadata,
|
|
48
54
|
version,
|
|
49
55
|
resultId,
|
|
50
56
|
aspectRatio = "1:1",
|
|
51
57
|
quality = "fast",
|
|
52
58
|
variables = {},
|
|
53
|
-
baseUrl
|
|
59
|
+
baseUrl,
|
|
54
60
|
original,
|
|
55
61
|
attachments
|
|
56
62
|
} = options;
|
|
63
|
+
if (pipelineId) {
|
|
64
|
+
const endpoint2 = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline";
|
|
65
|
+
const params2 = new URLSearchParams();
|
|
66
|
+
params2.set("pipelineId", pipelineId);
|
|
67
|
+
params2.set("redirect", "true");
|
|
68
|
+
if (siloId) params2.set("siloId", siloId);
|
|
69
|
+
if (prompt) params2.set("prompt", prompt);
|
|
70
|
+
if (resultId) params2.set("resultId", resultId);
|
|
71
|
+
if (Object.keys(variables).length > 0) {
|
|
72
|
+
params2.set("variables", JSON.stringify(variables));
|
|
73
|
+
}
|
|
74
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
75
|
+
params2.set("metadata", JSON.stringify(metadata));
|
|
76
|
+
}
|
|
77
|
+
return `${endpoint2}?${params2.toString()}`;
|
|
78
|
+
}
|
|
79
|
+
const endpoint = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage";
|
|
57
80
|
const params = new URLSearchParams();
|
|
58
|
-
|
|
81
|
+
if (bucketId) {
|
|
82
|
+
params.set("bucketId", bucketId);
|
|
83
|
+
}
|
|
59
84
|
if (!resultId) {
|
|
60
85
|
if (aspectRatio) {
|
|
61
86
|
params.set("aspectRatio", aspectRatio);
|
|
@@ -74,13 +99,16 @@ function buildImageUrl(options) {
|
|
|
74
99
|
if (Object.keys(variables).length > 0) {
|
|
75
100
|
params.set("variables", JSON.stringify(variables));
|
|
76
101
|
}
|
|
102
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
103
|
+
params.set("metadata", JSON.stringify(metadata));
|
|
104
|
+
}
|
|
77
105
|
if (attachments && attachments.length > 0) {
|
|
78
106
|
params.set("attachments", JSON.stringify(attachments));
|
|
79
107
|
}
|
|
80
108
|
} else {
|
|
81
109
|
params.set("resultId", resultId);
|
|
82
110
|
}
|
|
83
|
-
return `${
|
|
111
|
+
return `${endpoint}?${params.toString()}`;
|
|
84
112
|
}
|
|
85
113
|
function preloadImage(options) {
|
|
86
114
|
return new Promise((resolve, reject) => {
|
|
@@ -97,18 +125,41 @@ function preloadImage(options) {
|
|
|
97
125
|
function buildVideoUrl(options) {
|
|
98
126
|
const {
|
|
99
127
|
bucketId,
|
|
128
|
+
pipelineId,
|
|
129
|
+
siloId,
|
|
130
|
+
prompt,
|
|
131
|
+
metadata,
|
|
100
132
|
version,
|
|
101
133
|
resultId,
|
|
102
134
|
aspectRatio = "16:9",
|
|
103
135
|
quality = "fast",
|
|
104
136
|
variables = {},
|
|
105
137
|
duration = 4,
|
|
106
|
-
baseUrl
|
|
138
|
+
baseUrl,
|
|
107
139
|
original,
|
|
108
140
|
attachments
|
|
109
141
|
} = options;
|
|
142
|
+
if (pipelineId) {
|
|
143
|
+
const endpoint2 = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline";
|
|
144
|
+
const params2 = new URLSearchParams();
|
|
145
|
+
params2.set("pipelineId", pipelineId);
|
|
146
|
+
params2.set("redirect", "true");
|
|
147
|
+
if (siloId) params2.set("siloId", siloId);
|
|
148
|
+
if (prompt) params2.set("prompt", prompt);
|
|
149
|
+
if (resultId) params2.set("resultId", resultId);
|
|
150
|
+
if (Object.keys(variables).length > 0) {
|
|
151
|
+
params2.set("variables", JSON.stringify(variables));
|
|
152
|
+
}
|
|
153
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
154
|
+
params2.set("metadata", JSON.stringify(metadata));
|
|
155
|
+
}
|
|
156
|
+
return `${endpoint2}?${params2.toString()}`;
|
|
157
|
+
}
|
|
158
|
+
const endpoint = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo";
|
|
110
159
|
const params = new URLSearchParams();
|
|
111
|
-
|
|
160
|
+
if (bucketId) {
|
|
161
|
+
params.set("bucketId", bucketId);
|
|
162
|
+
}
|
|
112
163
|
if (!resultId) {
|
|
113
164
|
if (aspectRatio) {
|
|
114
165
|
params.set("aspectRatio", aspectRatio);
|
|
@@ -128,13 +179,16 @@ function buildVideoUrl(options) {
|
|
|
128
179
|
if (Object.keys(variables).length > 0) {
|
|
129
180
|
params.set("variables", JSON.stringify(variables));
|
|
130
181
|
}
|
|
182
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
183
|
+
params.set("metadata", JSON.stringify(metadata));
|
|
184
|
+
}
|
|
131
185
|
if (attachments && attachments.length > 0) {
|
|
132
186
|
params.set("attachments", JSON.stringify(attachments));
|
|
133
187
|
}
|
|
134
188
|
} else {
|
|
135
189
|
params.set("resultId", resultId);
|
|
136
190
|
}
|
|
137
|
-
return `${
|
|
191
|
+
return `${endpoint}?${params.toString()}`;
|
|
138
192
|
}
|
|
139
193
|
function preloadVideo(options) {
|
|
140
194
|
return new Promise((resolve, reject) => {
|
|
@@ -148,31 +202,54 @@ function preloadVideo(options) {
|
|
|
148
202
|
function buildTextUrl(options) {
|
|
149
203
|
const {
|
|
150
204
|
bucketId,
|
|
205
|
+
pipelineId,
|
|
206
|
+
siloId,
|
|
207
|
+
prompt,
|
|
208
|
+
metadata,
|
|
151
209
|
version,
|
|
152
210
|
resultId,
|
|
153
211
|
variables = {},
|
|
154
|
-
baseUrl
|
|
212
|
+
baseUrl,
|
|
155
213
|
attachments
|
|
156
214
|
} = options;
|
|
215
|
+
if (pipelineId) {
|
|
216
|
+
const endpoint2 = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline";
|
|
217
|
+
const params2 = new URLSearchParams();
|
|
218
|
+
params2.set("pipelineId", pipelineId);
|
|
219
|
+
params2.set("sync", "true");
|
|
220
|
+
if (siloId) params2.set("siloId", siloId);
|
|
221
|
+
if (prompt) params2.set("prompt", prompt);
|
|
222
|
+
if (resultId) params2.set("resultId", resultId);
|
|
223
|
+
if (Object.keys(variables).length > 0) {
|
|
224
|
+
params2.set("variables", JSON.stringify(variables));
|
|
225
|
+
}
|
|
226
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
227
|
+
params2.set("metadata", JSON.stringify(metadata));
|
|
228
|
+
}
|
|
229
|
+
return `${endpoint2}?${params2.toString()}`;
|
|
230
|
+
}
|
|
231
|
+
const endpoint = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText";
|
|
157
232
|
const params = new URLSearchParams();
|
|
158
|
-
|
|
233
|
+
if (bucketId) {
|
|
234
|
+
params.set("bucketId", bucketId);
|
|
235
|
+
}
|
|
159
236
|
if (!resultId) {
|
|
160
237
|
if (version) {
|
|
161
238
|
params.set("version", String(version));
|
|
162
239
|
}
|
|
163
|
-
if (resultId) {
|
|
164
|
-
params.set("resultId", resultId);
|
|
165
|
-
}
|
|
166
240
|
if (Object.keys(variables).length > 0) {
|
|
167
241
|
params.set("variables", JSON.stringify(variables));
|
|
168
242
|
}
|
|
243
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
244
|
+
params.set("metadata", JSON.stringify(metadata));
|
|
245
|
+
}
|
|
169
246
|
if (attachments && attachments.length > 0) {
|
|
170
247
|
params.set("attachments", JSON.stringify(attachments));
|
|
171
248
|
}
|
|
172
249
|
} else {
|
|
173
250
|
params.set("resultId", resultId);
|
|
174
251
|
}
|
|
175
|
-
return `${
|
|
252
|
+
return `${endpoint}?${params.toString()}`;
|
|
176
253
|
}
|
|
177
254
|
function preloadText(options) {
|
|
178
255
|
return new Promise((resolve, reject) => {
|
|
@@ -183,6 +260,71 @@ function preloadText(options) {
|
|
|
183
260
|
fetch(buildTextUrl(options), { mode: "no-cors" }).then(() => resolve()).catch((err) => reject(err));
|
|
184
261
|
});
|
|
185
262
|
}
|
|
263
|
+
function buildPipelineUrl(options) {
|
|
264
|
+
const {
|
|
265
|
+
pipelineId,
|
|
266
|
+
siloId,
|
|
267
|
+
prompt,
|
|
268
|
+
variables = {},
|
|
269
|
+
metadata = {},
|
|
270
|
+
sync = true,
|
|
271
|
+
redirect = false,
|
|
272
|
+
resultId,
|
|
273
|
+
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline"
|
|
274
|
+
} = options;
|
|
275
|
+
const params = new URLSearchParams();
|
|
276
|
+
params.set("pipelineId", pipelineId);
|
|
277
|
+
if (siloId) params.set("siloId", siloId);
|
|
278
|
+
if (prompt) params.set("prompt", prompt);
|
|
279
|
+
if (resultId) params.set("resultId", resultId);
|
|
280
|
+
if (sync !== void 0) params.set("sync", String(sync));
|
|
281
|
+
if (redirect) params.set("redirect", "true");
|
|
282
|
+
if (Object.keys(variables).length > 0) {
|
|
283
|
+
params.set("variables", JSON.stringify(variables));
|
|
284
|
+
}
|
|
285
|
+
if (Object.keys(metadata).length > 0) {
|
|
286
|
+
params.set("metadata", JSON.stringify(metadata));
|
|
287
|
+
}
|
|
288
|
+
return `${baseUrl}?${params.toString()}`;
|
|
289
|
+
}
|
|
290
|
+
async function executePipeline(options) {
|
|
291
|
+
const {
|
|
292
|
+
pipelineId,
|
|
293
|
+
siloId,
|
|
294
|
+
prompt,
|
|
295
|
+
variables,
|
|
296
|
+
metadata,
|
|
297
|
+
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline"
|
|
298
|
+
} = options;
|
|
299
|
+
const response = await fetch(baseUrl, {
|
|
300
|
+
method: "POST",
|
|
301
|
+
headers: {
|
|
302
|
+
"Content-Type": "application/json"
|
|
303
|
+
},
|
|
304
|
+
body: JSON.stringify({
|
|
305
|
+
pipelineId,
|
|
306
|
+
...siloId ? { siloId } : {},
|
|
307
|
+
...prompt ? { prompt } : {},
|
|
308
|
+
variables,
|
|
309
|
+
metadata,
|
|
310
|
+
sync: true
|
|
311
|
+
})
|
|
312
|
+
});
|
|
313
|
+
if (!response.ok) {
|
|
314
|
+
let errorDetail = response.statusText;
|
|
315
|
+
try {
|
|
316
|
+
const errorJson = await response.json();
|
|
317
|
+
if (errorJson.error) {
|
|
318
|
+
errorDetail = errorJson.error;
|
|
319
|
+
}
|
|
320
|
+
} catch {
|
|
321
|
+
}
|
|
322
|
+
throw new Error(
|
|
323
|
+
`Pipeline execution failed (${response.status}): ${errorDetail}`
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
return await response.json();
|
|
327
|
+
}
|
|
186
328
|
async function uploadTempAttachment(base64, mimeType) {
|
|
187
329
|
const response = await fetch(
|
|
188
330
|
"https://us-central1-slopmachine-12bfb.cloudfunctions.net/uploadTempAttachment",
|
|
@@ -208,8 +350,10 @@ async function uploadTempAttachment(base64, mimeType) {
|
|
|
208
350
|
// Annotate the CommonJS export names for ESM import in node:
|
|
209
351
|
0 && (module.exports = {
|
|
210
352
|
buildImageUrl,
|
|
353
|
+
buildPipelineUrl,
|
|
211
354
|
buildTextUrl,
|
|
212
355
|
buildVideoUrl,
|
|
356
|
+
executePipeline,
|
|
213
357
|
interpolatePrompt,
|
|
214
358
|
preloadImage,
|
|
215
359
|
preloadText,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AspectRatio as ImageAspectRatio, VideoAspectRatio } from \"@pixerate/schemas\";\nexport type { ImageAspectRatio };\n\nexport interface SlopImageOptions {\n /**\n * The unique identifier of your Slop Machine bucket.\n */\n bucketId: string;\n /**\n * The specific version of the prompt/settings to use.\n * If omitted, the latest version will be used.\n */\n version?: number;\n /**\n * Result ID to retrieve a specific previously generated image\n * instead of generating a new one.\n */\n resultId?: string;\n /**\n * The aspect ratio of the generated image. Defaults to \"1:1\".\n * Common values: \"1:1\", \"16:9\", \"9:16\", \"4:3\", \"3:4\".\n */\n aspectRatio?: ImageAspectRatio;\n /**\n * Dynamic variables to interpolate into the prompt.\n * E.g., if prompt is \"A photo of a {color} dog\", pass { color: \"brown\" }.\n */\n variables?: Record<string, string | number | undefined | null>;\n /**\n * The target quality (\"fast\" or \"high\"). Only affects new generations and is ignored for caching.\n * Ignored if `model` is provided.\n * Defaults to \"fast\".\n */\n quality?: \"fast\" | \"high\";\n /**\n * The base URL for the Slop Machine API.\n * Defaults to the production URL. Useful for testing against local deployments.\n */\n baseUrl?: string;\n /**\n * If `true` (or `?raw=true`), bypasses the WebP optimized media and returns the original generated file (e.g., PNG/JPEG).\n * Defaults to false.\n */\n original?: boolean;\n /**\n * Array of attachment URLs to include with the request.\n */\n attachments?: string[];\n}\n\nexport function interpolatePrompt(\n prompt?: string,\n variables?: Record<string, string | number | undefined | null>,\n): string {\n if (!prompt) return \"\";\n let text = prompt;\n if (!variables) return text;\n\n Object.keys(variables).forEach((key) => {\n const value = variables[key];\n if (value !== undefined && value !== null) {\n text = text.replace(new RegExp(`\\\\{${key}\\\\}`, \"g\"), String(value));\n }\n });\n return text;\n}\n\n/**\n * Builds a URL to render or retrieve an image from Slop Machine.\n *\n * @param options - Configuration options for the image generation.\n * @returns A string containing the fully constructed URL.\n */\nexport function buildImageUrl(options: SlopImageOptions): string {\n const {\n bucketId,\n version,\n resultId,\n aspectRatio = \"1:1\",\n quality = \"fast\",\n variables = {},\n baseUrl = \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage\",\n original,\n attachments,\n } = options;\n\n const params = new URLSearchParams();\n params.set(\"bucketId\", bucketId);\n\n if (!resultId) {\n if (aspectRatio) {\n params.set(\"aspectRatio\", aspectRatio);\n }\n if (version) {\n params.set(\"version\", String(version));\n }\n if (quality && quality !== \"fast\") {\n params.set(\"quality\", quality);\n } else if (quality === \"fast\") {\n params.set(\"quality\", \"fast\");\n }\n\n if (original) {\n params.set(\"original\", \"true\");\n }\n\n if (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\n }\n\n if (attachments && attachments.length > 0) {\n params.set(\"attachments\", JSON.stringify(attachments));\n }\n } else {\n params.set(\"resultId\", resultId);\n }\n\n return `${baseUrl}?${params.toString()}`;\n}\n\n/**\n * Preloads an image from Slop Machine into the browser's cache.\n * Useful for ensuring images are ready before displaying them.\n *\n * @param options - Configuration options for the image generation.\n * @returns A promise that resolves when the image has been loaded.\n */\nexport function preloadImage(options: SlopImageOptions): Promise<void> {\n return new Promise((resolve, reject) => {\n if (typeof window === \"undefined\") {\n resolve();\n return;\n }\n\n const img = new Image();\n img.onload = () => resolve();\n img.onerror = (err) => reject(err);\n img.src = buildImageUrl(options);\n });\n}\n\nexport type { VideoAspectRatio };\n\nexport interface SlopVideoOptions {\n /**\n * The unique identifier of your Slop Machine bucket.\n */\n bucketId: string;\n /**\n * The specific version of the prompt/settings to use.\n * If omitted, the latest version will be used.\n */\n version?: number;\n /**\n * Result ID to retrieve a specific previously generated video\n * instead of generating a new one.\n */\n resultId?: string;\n /**\n * The aspect ratio of the generated video. Defaults to \"16:9\".\n * Common values: \"1:1\", \"16:9\", \"9:16\", \"4:3\", \"3:4\".\n */\n aspectRatio?: VideoAspectRatio;\n /**\n * Dynamic variables to interpolate into the prompt.\n * E.g., if prompt is \"A video of a {color} dog\", pass { color: \"brown\" }.\n */\n variables?: Record<string, string | number | undefined | null>;\n /**\n * The duration of the generated video in seconds.\n * Must be between 4 and 8. Defaults to 4.\n */\n duration?: number;\n /**\n * The target quality (\"fast\" or \"high\"). Only affects new generations and is ignored for caching.\n * Ignored if `model` is provided.\n * Defaults to \"fast\".\n */\n quality?: \"fast\" | \"high\";\n /**\n * The base URL for the Slop Machine API.\n * Defaults to the production URL. Useful for testing against local deployments.\n */\n baseUrl?: string;\n /**\n * If `true` (or `?raw=true`), bypasses the WebP optimized media and returns the original generated file.\n * Defaults to false.\n */\n original?: boolean;\n /**\n * Array of attachment URLs to include with the request.\n */\n attachments?: string[];\n}\n\n/**\n * Builds a URL to render or retrieve a video from Slop Machine.\n *\n * @param options - Configuration options for the video generation.\n * @returns A string containing the fully constructed URL.\n */\nexport function buildVideoUrl(options: SlopVideoOptions): string {\n const {\n bucketId,\n version,\n resultId,\n aspectRatio = \"16:9\",\n quality = \"fast\",\n variables = {},\n duration = 4,\n baseUrl = \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo\",\n original,\n attachments,\n } = options;\n\n const params = new URLSearchParams();\n params.set(\"bucketId\", bucketId);\n\n if (!resultId) {\n if (aspectRatio) {\n params.set(\"aspectRatio\", aspectRatio);\n }\n if (version) {\n params.set(\"version\", String(version));\n }\n if (duration) {\n params.set(\"duration\", String(duration));\n }\n if (quality) {\n params.set(\"quality\", quality);\n }\n\n if (original) {\n params.set(\"original\", \"true\");\n }\n\n if (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\n }\n\n if (attachments && attachments.length > 0) {\n params.set(\"attachments\", JSON.stringify(attachments));\n }\n } else {\n params.set(\"resultId\", resultId);\n }\n\n return `${baseUrl}?${params.toString()}`;\n}\n\n/**\n * Preloads a video from Slop Machine into the browser's cache.\n * Useful for ensuring videos are ready before displaying them.\n *\n * @param options - Configuration options for the video generation.\n * @returns A promise that resolves when the video preload request has been initiated.\n */\nexport function preloadVideo(options: SlopVideoOptions): Promise<void> {\n return new Promise((resolve, reject) => {\n if (typeof window === \"undefined\") {\n resolve();\n return;\n }\n\n // We can just fetch the URL to preload it into the browser's cache.\n // We use no-cors to avoid CORS errors for simple preloads\n fetch(buildVideoUrl(options), { mode: \"no-cors\" })\n .then(() => resolve())\n .catch((err) => reject(err));\n });\n}\n\nexport interface SlopTextOptions {\n /**\n * The unique identifier of your Slop Machine bucket.\n */\n bucketId: string;\n /**\n * The specific version of the prompt/settings to use.\n * If omitted, the latest version will be used.\n */\n version?: number;\n /**\n * Result ID to retrieve a specific previously generated text\n * instead of generating a new one.\n */\n resultId?: string;\n /**\n * Dynamic variables to interpolate into the prompt.\n * E.g., if prompt is \"A story about a {color} dog\", pass { color: \"brown\" }.\n */\n variables?: Record<string, string | number | undefined | null>;\n /**\n * The base URL for the Slop Machine API.\n * Defaults to the production URL. Useful for testing against local deployments.\n */\n baseUrl?: string;\n /**\n * Array of attachment URLs to include with the request.\n */\n attachments?: string[];\n}\n\n/**\n * Builds a URL to render or retrieve text from Slop Machine.\n *\n * @param options - Configuration options for the text generation.\n * @returns A string containing the fully constructed URL.\n */\nexport function buildTextUrl(options: SlopTextOptions): string {\n const {\n bucketId,\n version,\n resultId,\n variables = {},\n baseUrl = \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText\",\n attachments,\n } = options;\n\n const params = new URLSearchParams();\n params.set(\"bucketId\", bucketId);\n\n if (!resultId) {\n if (version) {\n params.set(\"version\", String(version));\n }\n if (resultId) {\n params.set(\"resultId\", resultId);\n }\n\n if (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\n }\n\n if (attachments && attachments.length > 0) {\n params.set(\"attachments\", JSON.stringify(attachments));\n }\n } else {\n params.set(\"resultId\", resultId);\n }\n\n return `${baseUrl}?${params.toString()}`;\n}\n\n/**\n * Preloads text from Slop Machine into the browser's cache.\n * Useful for ensuring text is ready before displaying it.\n *\n * @param options - Configuration options for the text generation.\n * @returns A promise that resolves when the text preload request has been initiated.\n */\nexport function preloadText(options: SlopTextOptions): Promise<void> {\n return new Promise((resolve, reject) => {\n if (typeof window === \"undefined\") {\n resolve();\n return;\n }\n\n // Fetch the URL to preload it into the browser's cache.\n // We use no-cors to avoid CORS errors for simple preloads\n fetch(buildTextUrl(options), { mode: \"no-cors\" })\n .then(() => resolve())\n .catch((err) => reject(err));\n });\n}\n\n/**\n * Uploads a base64 encoded file as a temporary attachment to be used in generation requests.\n *\n * @param base64 - The base64 encoded file data (without the data:mime/type;base64, prefix).\n * @param mimeType - The MIME type of the file.\n * @returns A promise that resolves to an object containing the URL of the uploaded attachment.\n */\nexport async function uploadTempAttachment(\n base64: string,\n mimeType: string,\n): Promise<{ url: string }> {\n const response = await fetch(\n \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/uploadTempAttachment\",\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({ data: { base64, mimeType } }),\n },\n );\n if (!response.ok) {\n throw new Error(`Failed to upload attachment: ${response.statusText}`);\n }\n const json = await response.json();\n if (json.error) {\n throw new Error(\n `Failed to upload attachment: ${json.error.message || JSON.stringify(json.error)}`,\n );\n }\n return { url: json.result.url };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkDO,SAAS,kBACd,QACA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CAAC;AACH;AASA,eAAsB,qBACpB,QACA,UAC0B;AAC1B,QAAM,WAAW,MAAM;AAAA,IACrB;AAAA,IACA;AAAA,MACE,QAAQ;AAAA,MACR,SAAS;AAAA,QACP,gBAAgB;AAAA,MAClB;AAAA,MACA,MAAM,KAAK,UAAU,EAAE,MAAM,EAAE,QAAQ,SAAS,EAAE,CAAC;AAAA,IACrD;AAAA,EACF;AACA,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,IAAI,MAAM,gCAAgC,SAAS,UAAU,EAAE;AAAA,EACvE;AACA,QAAM,OAAO,MAAM,SAAS,KAAK;AACjC,MAAI,KAAK,OAAO;AACd,UAAM,IAAI;AAAA,MACR,gCAAgC,KAAK,MAAM,WAAW,KAAK,UAAU,KAAK,KAAK,CAAC;AAAA,IAClF;AAAA,EACF;AACA,SAAO,EAAE,KAAK,KAAK,OAAO,IAAI;AAChC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AspectRatio as ImageAspectRatio, VideoAspectRatio } from \"@pixerate/schemas\";\nexport type { ImageAspectRatio, VideoAspectRatio };\n\nexport interface PipelineStepResult {\n stepId: string;\n stepName?: string;\n type: string;\n outputUrl?: string;\n outputText?: string;\n outputData?: any;\n computeTimeMs?: number;\n fuelCost?: number;\n error?: string;\n}\n\nexport interface PipelineResult {\n id: string;\n pipelineId: string;\n siloId: string;\n url?: string;\n text?: string;\n data?: any;\n resultType: string;\n status: \"completed\" | \"failed\" | \"pending\";\n stepResults: PipelineStepResult[];\n totalComputeTimeMs: number;\n totalFuelCost: number;\n metadata?: Record<string, any>;\n timestamp: any;\n error?: string;\n}\n\nexport interface SlopPipelineOptions {\n /**\n * The unique identifier of the pipeline to execute.\n */\n pipelineId: string;\n /**\n * Optional silo identifier for direct pipeline path resolution.\n */\n siloId?: string;\n /**\n * Dynamic runtime prompt to feed into the pipeline.\n */\n prompt?: string;\n /**\n * Variables to interpolate into prompt or step configs.\n */\n variables?: Record<string, string | number | undefined | null>;\n /**\n * Arbitrary user metadata to attach to the pipeline result document.\n */\n metadata?: Record<string, any>;\n /**\n * Whether to wait for full execution (default true) or return a job ID immediately.\n */\n sync?: boolean;\n /**\n * Whether to redirect to the primary media output URL upon completion.\n */\n redirect?: boolean;\n /**\n * Result ID to retrieve a specific previously generated result.\n */\n resultId?: string;\n /**\n * Base URL for the renderPipeline cloud function endpoint.\n */\n baseUrl?: string;\n}\n\nexport interface ExecutePipelineOptions {\n /**\n * The unique identifier of the pipeline to execute.\n */\n pipelineId: string;\n /**\n * Optional silo identifier for direct pipeline path resolution.\n */\n siloId?: string;\n /**\n * Dynamic runtime prompt to feed into the pipeline.\n */\n prompt?: string;\n /**\n * Variables to interpolate into prompt or step configs.\n */\n variables?: Record<string, string | number | undefined | null>;\n /**\n * Arbitrary user metadata to attach to the pipeline result document.\n */\n metadata?: Record<string, any>;\n /**\n * Base URL for the renderPipeline cloud function endpoint.\n */\n baseUrl?: string;\n}\n\nexport interface SlopImageOptions {\n /**\n * The unique identifier of your Slop Machine bucket.\n * Required when using a bucket unless pipelineId is provided.\n */\n bucketId?: string;\n /**\n * The unique identifier of your Slop Machine pipeline.\n * Required when targeting a pipeline instead of a bucket.\n */\n pipelineId?: string;\n /**\n * Optional silo identifier.\n */\n siloId?: string;\n /**\n * Dynamic runtime prompt (used when targeting a pipeline).\n */\n prompt?: string;\n /**\n * Arbitrary user metadata to attach to the generation request / result document.\n */\n metadata?: Record<string, any>;\n /**\n * The specific version of the prompt/settings to use.\n * If omitted, the latest version will be used.\n */\n version?: number;\n /**\n * Result ID to retrieve a specific previously generated image\n * instead of generating a new one.\n */\n resultId?: string;\n /**\n * The aspect ratio of the generated image. Defaults to \"1:1\".\n * Common values: \"1:1\", \"16:9\", \"9:16\", \"4:3\", \"3:4\".\n */\n aspectRatio?: ImageAspectRatio;\n /**\n * Dynamic variables to interpolate into the prompt.\n * E.g., if prompt is \"A photo of a {color} dog\", pass { color: \"brown\" }.\n */\n variables?: Record<string, string | number | undefined | null>;\n /**\n * The target quality (\"fast\" or \"high\"). Only affects new generations and is ignored for caching.\n * Ignored if `model` is provided.\n * Defaults to \"fast\".\n */\n quality?: \"fast\" | \"high\";\n /**\n * The base URL for the Slop Machine API.\n * Defaults to the production URL. Useful for testing against local deployments.\n */\n baseUrl?: string;\n /**\n * If `true` (or `?raw=true`), bypasses the WebP optimized media and returns the original generated file (e.g., PNG/JPEG).\n * Defaults to false.\n */\n original?: boolean;\n /**\n * Array of attachment URLs to include with the request.\n */\n attachments?: string[];\n}\n\nexport function interpolatePrompt(\n prompt?: string,\n variables?: Record<string, string | number | undefined | null>,\n): string {\n if (!prompt) return \"\";\n let text = prompt;\n if (!variables) return text;\n\n Object.keys(variables).forEach((key) => {\n const value = variables[key];\n if (value !== undefined && value !== null) {\n text = text.replace(new RegExp(`\\\\{${key}\\\\}`, \"g\"), String(value));\n }\n });\n return text;\n}\n\n/**\n * Builds a URL to render or retrieve an image from Slop Machine.\n *\n * Supports both standard Buckets (via `bucketId`) and multi-step Pipelines (via `pipelineId`).\n *\n * @param options - Configuration options for the image generation.\n * @returns A string containing the fully constructed URL.\n */\nexport function buildImageUrl(options: SlopImageOptions): string {\n const {\n bucketId,\n pipelineId,\n siloId,\n prompt,\n metadata,\n version,\n resultId,\n aspectRatio = \"1:1\",\n quality = \"fast\",\n variables = {},\n baseUrl,\n original,\n attachments,\n } = options;\n\n if (pipelineId) {\n const endpoint =\n baseUrl ||\n \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline\";\n const params = new URLSearchParams();\n params.set(\"pipelineId\", pipelineId);\n params.set(\"redirect\", \"true\");\n\n if (siloId) params.set(\"siloId\", siloId);\n if (prompt) params.set(\"prompt\", prompt);\n if (resultId) params.set(\"resultId\", resultId);\n\n if (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\n }\n if (metadata && Object.keys(metadata).length > 0) {\n params.set(\"metadata\", JSON.stringify(metadata));\n }\n\n return `${endpoint}?${params.toString()}`;\n }\n\n const endpoint =\n baseUrl ||\n \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage\";\n const params = new URLSearchParams();\n if (bucketId) {\n params.set(\"bucketId\", bucketId);\n }\n\n if (!resultId) {\n if (aspectRatio) {\n params.set(\"aspectRatio\", aspectRatio);\n }\n if (version) {\n params.set(\"version\", String(version));\n }\n if (quality && quality !== \"fast\") {\n params.set(\"quality\", quality);\n } else if (quality === \"fast\") {\n params.set(\"quality\", \"fast\");\n }\n\n if (original) {\n params.set(\"original\", \"true\");\n }\n\n if (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\n }\n\n if (metadata && Object.keys(metadata).length > 0) {\n params.set(\"metadata\", JSON.stringify(metadata));\n }\n\n if (attachments && attachments.length > 0) {\n params.set(\"attachments\", JSON.stringify(attachments));\n }\n } else {\n params.set(\"resultId\", resultId);\n }\n\n return `${endpoint}?${params.toString()}`;\n}\n\n/**\n * Preloads an image from Slop Machine into the browser's cache.\n * Useful for ensuring images are ready before displaying them.\n *\n * @param options - Configuration options for the image generation.\n * @returns A promise that resolves when the image has been loaded.\n */\nexport function preloadImage(options: SlopImageOptions): Promise<void> {\n return new Promise((resolve, reject) => {\n if (typeof window === \"undefined\") {\n resolve();\n return;\n }\n\n const img = new Image();\n img.onload = () => resolve();\n img.onerror = (err) => reject(err);\n img.src = buildImageUrl(options);\n });\n}\n\nexport interface SlopVideoOptions {\n /**\n * The unique identifier of your Slop Machine bucket.\n * Required when using a bucket unless pipelineId is provided.\n */\n bucketId?: string;\n /**\n * The unique identifier of your Slop Machine pipeline.\n * Required when targeting a pipeline instead of a bucket.\n */\n pipelineId?: string;\n /**\n * Optional silo identifier.\n */\n siloId?: string;\n /**\n * Dynamic runtime prompt (used when targeting a pipeline).\n */\n prompt?: string;\n /**\n * Arbitrary user metadata to attach to the generation request / result document.\n */\n metadata?: Record<string, any>;\n /**\n * The specific version of the prompt/settings to use.\n * If omitted, the latest version will be used.\n */\n version?: number;\n /**\n * Result ID to retrieve a specific previously generated video\n * instead of generating a new one.\n */\n resultId?: string;\n /**\n * The aspect ratio of the generated video. Defaults to \"16:9\".\n * Common values: \"1:1\", \"16:9\", \"9:16\", \"4:3\", \"3:4\".\n */\n aspectRatio?: VideoAspectRatio;\n /**\n * Dynamic variables to interpolate into the prompt.\n * E.g., if prompt is \"A video of a {color} dog\", pass { color: \"brown\" }.\n */\n variables?: Record<string, string | number | undefined | null>;\n /**\n * The duration of the generated video in seconds.\n * Must be between 4 and 8. Defaults to 4.\n */\n duration?: number;\n /**\n * The target quality (\"fast\" or \"high\"). Only affects new generations and is ignored for caching.\n * Ignored if `model` is provided.\n * Defaults to \"fast\".\n */\n quality?: \"fast\" | \"high\";\n /**\n * The base URL for the Slop Machine API.\n * Defaults to the production URL. Useful for testing against local deployments.\n */\n baseUrl?: string;\n /**\n * If `true` (or `?raw=true`), bypasses the WebP optimized media and returns the original generated file.\n * Defaults to false.\n */\n original?: boolean;\n /**\n * Array of attachment URLs to include with the request.\n */\n attachments?: string[];\n}\n\n/**\n * Builds a URL to render or retrieve a video from Slop Machine.\n *\n * Supports both standard Buckets (via `bucketId`) and multi-step Pipelines (via `pipelineId`).\n *\n * @param options - Configuration options for the video generation.\n * @returns A string containing the fully constructed URL.\n */\nexport function buildVideoUrl(options: SlopVideoOptions): string {\n const {\n bucketId,\n pipelineId,\n siloId,\n prompt,\n metadata,\n version,\n resultId,\n aspectRatio = \"16:9\",\n quality = \"fast\",\n variables = {},\n duration = 4,\n baseUrl,\n original,\n attachments,\n } = options;\n\n if (pipelineId) {\n const endpoint =\n baseUrl ||\n \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline\";\n const params = new URLSearchParams();\n params.set(\"pipelineId\", pipelineId);\n params.set(\"redirect\", \"true\");\n\n if (siloId) params.set(\"siloId\", siloId);\n if (prompt) params.set(\"prompt\", prompt);\n if (resultId) params.set(\"resultId\", resultId);\n\n if (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\n }\n if (metadata && Object.keys(metadata).length > 0) {\n params.set(\"metadata\", JSON.stringify(metadata));\n }\n\n return `${endpoint}?${params.toString()}`;\n }\n\n const endpoint =\n baseUrl ||\n \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo\";\n const params = new URLSearchParams();\n if (bucketId) {\n params.set(\"bucketId\", bucketId);\n }\n\n if (!resultId) {\n if (aspectRatio) {\n params.set(\"aspectRatio\", aspectRatio);\n }\n if (version) {\n params.set(\"version\", String(version));\n }\n if (duration) {\n params.set(\"duration\", String(duration));\n }\n if (quality) {\n params.set(\"quality\", quality);\n }\n\n if (original) {\n params.set(\"original\", \"true\");\n }\n\n if (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\n }\n\n if (metadata && Object.keys(metadata).length > 0) {\n params.set(\"metadata\", JSON.stringify(metadata));\n }\n\n if (attachments && attachments.length > 0) {\n params.set(\"attachments\", JSON.stringify(attachments));\n }\n } else {\n params.set(\"resultId\", resultId);\n }\n\n return `${endpoint}?${params.toString()}`;\n}\n\n/**\n * Preloads a video from Slop Machine into the browser's cache.\n * Useful for ensuring videos are ready before displaying them.\n *\n * @param options - Configuration options for the video generation.\n * @returns A promise that resolves when the video preload request has been initiated.\n */\nexport function preloadVideo(options: SlopVideoOptions): Promise<void> {\n return new Promise((resolve, reject) => {\n if (typeof window === \"undefined\") {\n resolve();\n return;\n }\n\n fetch(buildVideoUrl(options), { mode: \"no-cors\" })\n .then(() => resolve())\n .catch((err) => reject(err));\n });\n}\n\nexport interface SlopTextOptions {\n /**\n * The unique identifier of your Slop Machine bucket.\n * Required when using a bucket unless pipelineId is provided.\n */\n bucketId?: string;\n /**\n * The unique identifier of your Slop Machine pipeline.\n * Required when targeting a pipeline instead of a bucket.\n */\n pipelineId?: string;\n /**\n * Optional silo identifier.\n */\n siloId?: string;\n /**\n * Dynamic runtime prompt (used when targeting a pipeline).\n */\n prompt?: string;\n /**\n * Arbitrary user metadata to attach to the generation request / result document.\n */\n metadata?: Record<string, any>;\n /**\n * The specific version of the prompt/settings to use.\n * If omitted, the latest version will be used.\n */\n version?: number;\n /**\n * Result ID to retrieve a specific previously generated text\n * instead of generating a new one.\n */\n resultId?: string;\n /**\n * Dynamic variables to interpolate into the prompt.\n * E.g., if prompt is \"A story about a {color} dog\", pass { color: \"brown\" }.\n */\n variables?: Record<string, string | number | undefined | null>;\n /**\n * The base URL for the Slop Machine API.\n * Defaults to the production URL. Useful for testing against local deployments.\n */\n baseUrl?: string;\n /**\n * Array of attachment URLs to include with the request.\n */\n attachments?: string[];\n}\n\n/**\n * Builds a URL to render or retrieve text from Slop Machine.\n *\n * Supports both standard Buckets (via `bucketId`) and multi-step Pipelines (via `pipelineId`).\n *\n * @param options - Configuration options for the text generation.\n * @returns A string containing the fully constructed URL.\n */\nexport function buildTextUrl(options: SlopTextOptions): string {\n const {\n bucketId,\n pipelineId,\n siloId,\n prompt,\n metadata,\n version,\n resultId,\n variables = {},\n baseUrl,\n attachments,\n } = options;\n\n if (pipelineId) {\n const endpoint =\n baseUrl ||\n \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline\";\n const params = new URLSearchParams();\n params.set(\"pipelineId\", pipelineId);\n params.set(\"sync\", \"true\");\n\n if (siloId) params.set(\"siloId\", siloId);\n if (prompt) params.set(\"prompt\", prompt);\n if (resultId) params.set(\"resultId\", resultId);\n\n if (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\n }\n if (metadata && Object.keys(metadata).length > 0) {\n params.set(\"metadata\", JSON.stringify(metadata));\n }\n\n return `${endpoint}?${params.toString()}`;\n }\n\n const endpoint =\n baseUrl ||\n \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText\";\n const params = new URLSearchParams();\n if (bucketId) {\n params.set(\"bucketId\", bucketId);\n }\n\n if (!resultId) {\n if (version) {\n params.set(\"version\", String(version));\n }\n if (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\n }\n if (metadata && Object.keys(metadata).length > 0) {\n params.set(\"metadata\", JSON.stringify(metadata));\n }\n if (attachments && attachments.length > 0) {\n params.set(\"attachments\", JSON.stringify(attachments));\n }\n } else {\n params.set(\"resultId\", resultId);\n }\n\n return `${endpoint}?${params.toString()}`;\n}\n\n/**\n * Preloads text from Slop Machine into the browser's cache.\n * Useful for ensuring text is ready before displaying it.\n *\n * @param options - Configuration options for the text generation.\n * @returns A promise that resolves when the text preload request has been initiated.\n */\nexport function preloadText(options: SlopTextOptions): Promise<void> {\n return new Promise((resolve, reject) => {\n if (typeof window === \"undefined\") {\n resolve();\n return;\n }\n\n fetch(buildTextUrl(options), { mode: \"no-cors\" })\n .then(() => resolve())\n .catch((err) => reject(err));\n });\n}\n\n/**\n * Builds a URL to execute or inspect a multi-step Pipeline from Slop Machine.\n *\n * @param options - Configuration options for the pipeline execution.\n * @returns A string containing the fully constructed URL.\n */\nexport function buildPipelineUrl(options: SlopPipelineOptions): string {\n const {\n pipelineId,\n siloId,\n prompt,\n variables = {},\n metadata = {},\n sync = true,\n redirect = false,\n resultId,\n baseUrl = \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline\",\n } = options;\n\n const params = new URLSearchParams();\n params.set(\"pipelineId\", pipelineId);\n\n if (siloId) params.set(\"siloId\", siloId);\n if (prompt) params.set(\"prompt\", prompt);\n if (resultId) params.set(\"resultId\", resultId);\n if (sync !== undefined) params.set(\"sync\", String(sync));\n if (redirect) params.set(\"redirect\", \"true\");\n\n if (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\n }\n if (Object.keys(metadata).length > 0) {\n params.set(\"metadata\", JSON.stringify(metadata));\n }\n\n return `${baseUrl}?${params.toString()}`;\n}\n\n/**\n * Executes a Slop Machine multi-step Pipeline programmatically and returns the full typed result payload.\n *\n * @param options - Execution parameters including the pipelineId and runtime prompt/variables/metadata.\n * @returns A promise resolving to the completed PipelineResult document.\n */\nexport async function executePipeline(\n options: ExecutePipelineOptions,\n): Promise<PipelineResult> {\n const {\n pipelineId,\n siloId,\n prompt,\n variables,\n metadata,\n baseUrl = \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline\",\n } = options;\n\n const response = await fetch(baseUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n pipelineId,\n ...(siloId ? { siloId } : {}),\n ...(prompt ? { prompt } : {}),\n variables,\n metadata,\n sync: true,\n }),\n });\n\n if (!response.ok) {\n let errorDetail = response.statusText;\n try {\n const errorJson = await response.json();\n if (errorJson.error) {\n errorDetail = errorJson.error;\n }\n } catch {\n // Use statusText\n }\n throw new Error(\n `Pipeline execution failed (${response.status}): ${errorDetail}`,\n );\n }\n\n return (await response.json()) as PipelineResult;\n}\n\n/**\n * Uploads a base64 encoded file as a temporary attachment to be used in generation requests.\n *\n * @param base64 - The base64 encoded file data (without the data:mime/type;base64, prefix).\n * @param mimeType - The MIME type of the file.\n * @returns A promise that resolves to an object containing the URL of the uploaded attachment.\n */\nexport async function uploadTempAttachment(\n base64: string,\n mimeType: string,\n): Promise<{ url: string }> {\n const response = await fetch(\n \"https://us-central1-slopmachine-12bfb.cloudfunctions.net/uploadTempAttachment\",\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({ data: { base64, mimeType } }),\n },\n );\n if (!response.ok) {\n throw new Error(`Failed to upload attachment: ${response.statusText}`);\n }\n const json = await response.json();\n if (json.error) {\n throw new Error(\n `Failed to upload attachment: ${json.error.message || JSON.stringify(json.error)}`,\n );\n }\n return { url: json.result.url };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmKO,SAAS,kBACd,QACA,WACQ;AACR,MAAI,CAAC,OAAQ,QAAO;AACpB,MAAI,OAAO;AACX,MAAI,CAAC,UAAW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AA,IAER;AACA,UAAM,IAAI;AAAA,MACR,8BAA8B,SAAS,MAAM,MAAM,WAAW;AAAA,IAChE;AAAA,EACF;AAEA,SAAQ,MAAM,SAAS,KAAK;AAC9B;AASA,eAAsB,qBACpB,QACA,UAC0B;AAC1B,QAAM,WAAW,MAAM;AAAA,IACrB;AAAA,IACA;AAAA,MACE,QAAQ;AAAA,MACR,SAAS;AAAA,QACP,gBAAgB;AAAA,MAClB;AAAA,MACA,MAAM,KAAK,UAAU,EAAE,MAAM,EAAE,QAAQ,SAAS,EAAE,CAAC;AAAA,IACrD;AAAA,EACF;AACA,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,IAAI,MAAM,gCAAgC,SAAS,UAAU,EAAE;AAAA,EACvE;AACA,QAAM,OAAO,MAAM,SAAS,KAAK;AACjC,MAAI,KAAK,OAAO;AACd,UAAM,IAAI;AAAA,MACR,gCAAgC,KAAK,MAAM,WAAW,KAAK,UAAU,KAAK,KAAK,CAAC;AAAA,IAClF;AAAA,EACF;AACA,SAAO,EAAE,KAAK,KAAK,OAAO,IAAI;AAChC;","names":["endpoint","params"]}
|
package/dist/index.mjs
CHANGED
|
@@ -14,17 +14,40 @@ function interpolatePrompt(prompt, variables) {
|
|
|
14
14
|
function buildImageUrl(options) {
|
|
15
15
|
const {
|
|
16
16
|
bucketId,
|
|
17
|
+
pipelineId,
|
|
18
|
+
siloId,
|
|
19
|
+
prompt,
|
|
20
|
+
metadata,
|
|
17
21
|
version,
|
|
18
22
|
resultId,
|
|
19
23
|
aspectRatio = "1:1",
|
|
20
24
|
quality = "fast",
|
|
21
25
|
variables = {},
|
|
22
|
-
baseUrl
|
|
26
|
+
baseUrl,
|
|
23
27
|
original,
|
|
24
28
|
attachments
|
|
25
29
|
} = options;
|
|
30
|
+
if (pipelineId) {
|
|
31
|
+
const endpoint2 = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline";
|
|
32
|
+
const params2 = new URLSearchParams();
|
|
33
|
+
params2.set("pipelineId", pipelineId);
|
|
34
|
+
params2.set("redirect", "true");
|
|
35
|
+
if (siloId) params2.set("siloId", siloId);
|
|
36
|
+
if (prompt) params2.set("prompt", prompt);
|
|
37
|
+
if (resultId) params2.set("resultId", resultId);
|
|
38
|
+
if (Object.keys(variables).length > 0) {
|
|
39
|
+
params2.set("variables", JSON.stringify(variables));
|
|
40
|
+
}
|
|
41
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
42
|
+
params2.set("metadata", JSON.stringify(metadata));
|
|
43
|
+
}
|
|
44
|
+
return `${endpoint2}?${params2.toString()}`;
|
|
45
|
+
}
|
|
46
|
+
const endpoint = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage";
|
|
26
47
|
const params = new URLSearchParams();
|
|
27
|
-
|
|
48
|
+
if (bucketId) {
|
|
49
|
+
params.set("bucketId", bucketId);
|
|
50
|
+
}
|
|
28
51
|
if (!resultId) {
|
|
29
52
|
if (aspectRatio) {
|
|
30
53
|
params.set("aspectRatio", aspectRatio);
|
|
@@ -43,13 +66,16 @@ function buildImageUrl(options) {
|
|
|
43
66
|
if (Object.keys(variables).length > 0) {
|
|
44
67
|
params.set("variables", JSON.stringify(variables));
|
|
45
68
|
}
|
|
69
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
70
|
+
params.set("metadata", JSON.stringify(metadata));
|
|
71
|
+
}
|
|
46
72
|
if (attachments && attachments.length > 0) {
|
|
47
73
|
params.set("attachments", JSON.stringify(attachments));
|
|
48
74
|
}
|
|
49
75
|
} else {
|
|
50
76
|
params.set("resultId", resultId);
|
|
51
77
|
}
|
|
52
|
-
return `${
|
|
78
|
+
return `${endpoint}?${params.toString()}`;
|
|
53
79
|
}
|
|
54
80
|
function preloadImage(options) {
|
|
55
81
|
return new Promise((resolve, reject) => {
|
|
@@ -66,18 +92,41 @@ function preloadImage(options) {
|
|
|
66
92
|
function buildVideoUrl(options) {
|
|
67
93
|
const {
|
|
68
94
|
bucketId,
|
|
95
|
+
pipelineId,
|
|
96
|
+
siloId,
|
|
97
|
+
prompt,
|
|
98
|
+
metadata,
|
|
69
99
|
version,
|
|
70
100
|
resultId,
|
|
71
101
|
aspectRatio = "16:9",
|
|
72
102
|
quality = "fast",
|
|
73
103
|
variables = {},
|
|
74
104
|
duration = 4,
|
|
75
|
-
baseUrl
|
|
105
|
+
baseUrl,
|
|
76
106
|
original,
|
|
77
107
|
attachments
|
|
78
108
|
} = options;
|
|
109
|
+
if (pipelineId) {
|
|
110
|
+
const endpoint2 = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline";
|
|
111
|
+
const params2 = new URLSearchParams();
|
|
112
|
+
params2.set("pipelineId", pipelineId);
|
|
113
|
+
params2.set("redirect", "true");
|
|
114
|
+
if (siloId) params2.set("siloId", siloId);
|
|
115
|
+
if (prompt) params2.set("prompt", prompt);
|
|
116
|
+
if (resultId) params2.set("resultId", resultId);
|
|
117
|
+
if (Object.keys(variables).length > 0) {
|
|
118
|
+
params2.set("variables", JSON.stringify(variables));
|
|
119
|
+
}
|
|
120
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
121
|
+
params2.set("metadata", JSON.stringify(metadata));
|
|
122
|
+
}
|
|
123
|
+
return `${endpoint2}?${params2.toString()}`;
|
|
124
|
+
}
|
|
125
|
+
const endpoint = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo";
|
|
79
126
|
const params = new URLSearchParams();
|
|
80
|
-
|
|
127
|
+
if (bucketId) {
|
|
128
|
+
params.set("bucketId", bucketId);
|
|
129
|
+
}
|
|
81
130
|
if (!resultId) {
|
|
82
131
|
if (aspectRatio) {
|
|
83
132
|
params.set("aspectRatio", aspectRatio);
|
|
@@ -97,13 +146,16 @@ function buildVideoUrl(options) {
|
|
|
97
146
|
if (Object.keys(variables).length > 0) {
|
|
98
147
|
params.set("variables", JSON.stringify(variables));
|
|
99
148
|
}
|
|
149
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
150
|
+
params.set("metadata", JSON.stringify(metadata));
|
|
151
|
+
}
|
|
100
152
|
if (attachments && attachments.length > 0) {
|
|
101
153
|
params.set("attachments", JSON.stringify(attachments));
|
|
102
154
|
}
|
|
103
155
|
} else {
|
|
104
156
|
params.set("resultId", resultId);
|
|
105
157
|
}
|
|
106
|
-
return `${
|
|
158
|
+
return `${endpoint}?${params.toString()}`;
|
|
107
159
|
}
|
|
108
160
|
function preloadVideo(options) {
|
|
109
161
|
return new Promise((resolve, reject) => {
|
|
@@ -117,31 +169,54 @@ function preloadVideo(options) {
|
|
|
117
169
|
function buildTextUrl(options) {
|
|
118
170
|
const {
|
|
119
171
|
bucketId,
|
|
172
|
+
pipelineId,
|
|
173
|
+
siloId,
|
|
174
|
+
prompt,
|
|
175
|
+
metadata,
|
|
120
176
|
version,
|
|
121
177
|
resultId,
|
|
122
178
|
variables = {},
|
|
123
|
-
baseUrl
|
|
179
|
+
baseUrl,
|
|
124
180
|
attachments
|
|
125
181
|
} = options;
|
|
182
|
+
if (pipelineId) {
|
|
183
|
+
const endpoint2 = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline";
|
|
184
|
+
const params2 = new URLSearchParams();
|
|
185
|
+
params2.set("pipelineId", pipelineId);
|
|
186
|
+
params2.set("sync", "true");
|
|
187
|
+
if (siloId) params2.set("siloId", siloId);
|
|
188
|
+
if (prompt) params2.set("prompt", prompt);
|
|
189
|
+
if (resultId) params2.set("resultId", resultId);
|
|
190
|
+
if (Object.keys(variables).length > 0) {
|
|
191
|
+
params2.set("variables", JSON.stringify(variables));
|
|
192
|
+
}
|
|
193
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
194
|
+
params2.set("metadata", JSON.stringify(metadata));
|
|
195
|
+
}
|
|
196
|
+
return `${endpoint2}?${params2.toString()}`;
|
|
197
|
+
}
|
|
198
|
+
const endpoint = baseUrl || "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText";
|
|
126
199
|
const params = new URLSearchParams();
|
|
127
|
-
|
|
200
|
+
if (bucketId) {
|
|
201
|
+
params.set("bucketId", bucketId);
|
|
202
|
+
}
|
|
128
203
|
if (!resultId) {
|
|
129
204
|
if (version) {
|
|
130
205
|
params.set("version", String(version));
|
|
131
206
|
}
|
|
132
|
-
if (resultId) {
|
|
133
|
-
params.set("resultId", resultId);
|
|
134
|
-
}
|
|
135
207
|
if (Object.keys(variables).length > 0) {
|
|
136
208
|
params.set("variables", JSON.stringify(variables));
|
|
137
209
|
}
|
|
210
|
+
if (metadata && Object.keys(metadata).length > 0) {
|
|
211
|
+
params.set("metadata", JSON.stringify(metadata));
|
|
212
|
+
}
|
|
138
213
|
if (attachments && attachments.length > 0) {
|
|
139
214
|
params.set("attachments", JSON.stringify(attachments));
|
|
140
215
|
}
|
|
141
216
|
} else {
|
|
142
217
|
params.set("resultId", resultId);
|
|
143
218
|
}
|
|
144
|
-
return `${
|
|
219
|
+
return `${endpoint}?${params.toString()}`;
|
|
145
220
|
}
|
|
146
221
|
function preloadText(options) {
|
|
147
222
|
return new Promise((resolve, reject) => {
|
|
@@ -152,6 +227,71 @@ function preloadText(options) {
|
|
|
152
227
|
fetch(buildTextUrl(options), { mode: "no-cors" }).then(() => resolve()).catch((err) => reject(err));
|
|
153
228
|
});
|
|
154
229
|
}
|
|
230
|
+
function buildPipelineUrl(options) {
|
|
231
|
+
const {
|
|
232
|
+
pipelineId,
|
|
233
|
+
siloId,
|
|
234
|
+
prompt,
|
|
235
|
+
variables = {},
|
|
236
|
+
metadata = {},
|
|
237
|
+
sync = true,
|
|
238
|
+
redirect = false,
|
|
239
|
+
resultId,
|
|
240
|
+
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline"
|
|
241
|
+
} = options;
|
|
242
|
+
const params = new URLSearchParams();
|
|
243
|
+
params.set("pipelineId", pipelineId);
|
|
244
|
+
if (siloId) params.set("siloId", siloId);
|
|
245
|
+
if (prompt) params.set("prompt", prompt);
|
|
246
|
+
if (resultId) params.set("resultId", resultId);
|
|
247
|
+
if (sync !== void 0) params.set("sync", String(sync));
|
|
248
|
+
if (redirect) params.set("redirect", "true");
|
|
249
|
+
if (Object.keys(variables).length > 0) {
|
|
250
|
+
params.set("variables", JSON.stringify(variables));
|
|
251
|
+
}
|
|
252
|
+
if (Object.keys(metadata).length > 0) {
|
|
253
|
+
params.set("metadata", JSON.stringify(metadata));
|
|
254
|
+
}
|
|
255
|
+
return `${baseUrl}?${params.toString()}`;
|
|
256
|
+
}
|
|
257
|
+
async function executePipeline(options) {
|
|
258
|
+
const {
|
|
259
|
+
pipelineId,
|
|
260
|
+
siloId,
|
|
261
|
+
prompt,
|
|
262
|
+
variables,
|
|
263
|
+
metadata,
|
|
264
|
+
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderPipeline"
|
|
265
|
+
} = options;
|
|
266
|
+
const response = await fetch(baseUrl, {
|
|
267
|
+
method: "POST",
|
|
268
|
+
headers: {
|
|
269
|
+
"Content-Type": "application/json"
|
|
270
|
+
},
|
|
271
|
+
body: JSON.stringify({
|
|
272
|
+
pipelineId,
|
|
273
|
+
...siloId ? { siloId } : {},
|
|
274
|
+
...prompt ? { prompt } : {},
|
|
275
|
+
variables,
|
|
276
|
+
metadata,
|
|
277
|
+
sync: true
|
|
278
|
+
})
|
|
279
|
+
});
|
|
280
|
+
if (!response.ok) {
|
|
281
|
+
let errorDetail = response.statusText;
|
|
282
|
+
try {
|
|
283
|
+
const errorJson = await response.json();
|
|
284
|
+
if (errorJson.error) {
|
|
285
|
+
errorDetail = errorJson.error;
|
|
286
|
+
}
|
|
287
|
+
} catch {
|
|
288
|
+
}
|
|
289
|
+
throw new Error(
|
|
290
|
+
`Pipeline execution failed (${response.status}): ${errorDetail}`
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
return await response.json();
|
|
294
|
+
}
|
|
155
295
|
async function uploadTempAttachment(base64, mimeType) {
|
|
156
296
|
const response = await fetch(
|
|
157
297
|
"https://us-central1-slopmachine-12bfb.cloudfunctions.net/uploadTempAttachment",
|
|
@@ -176,8 +316,10 @@ async function uploadTempAttachment(base64, mimeType) {
|
|
|
176
316
|
}
|
|
177
317
|
export {
|
|
178
318
|
buildImageUrl,
|
|
319
|
+
buildPipelineUrl,
|
|
179
320
|
buildTextUrl,
|
|
180
321
|
buildVideoUrl,
|
|
322
|
+
executePipeline,
|
|
181
323
|
interpolatePrompt,
|
|
182
324
|
preloadImage,
|
|
183
325
|
preloadText,
|