@slopmachine/core 0.1.20 → 0.1.25
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/dist/index.d.mts +33 -1
- package/dist/index.d.ts +33 -1
- package/dist/index.js +49 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +47 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +80 -0
package/dist/index.d.mts
CHANGED
|
@@ -35,6 +35,15 @@ interface SlopImageOptions {
|
|
|
35
35
|
* Defaults to the production URL. Useful for testing against local deployments.
|
|
36
36
|
*/
|
|
37
37
|
baseUrl?: string;
|
|
38
|
+
/**
|
|
39
|
+
* If `true` (or `?raw=true`), bypasses the WebP optimized media and returns the original generated file (e.g., PNG/JPEG).
|
|
40
|
+
* Defaults to false.
|
|
41
|
+
*/
|
|
42
|
+
original?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Array of attachment URLs to include with the request.
|
|
45
|
+
*/
|
|
46
|
+
attachments?: string[];
|
|
38
47
|
}
|
|
39
48
|
declare function interpolatePrompt(prompt?: string, variables?: Record<string, string | number | undefined | null>): string;
|
|
40
49
|
/**
|
|
@@ -94,6 +103,15 @@ interface SlopVideoOptions {
|
|
|
94
103
|
* Defaults to the production URL. Useful for testing against local deployments.
|
|
95
104
|
*/
|
|
96
105
|
baseUrl?: string;
|
|
106
|
+
/**
|
|
107
|
+
* If `true` (or `?raw=true`), bypasses the WebP optimized media and returns the original generated file.
|
|
108
|
+
* Defaults to false.
|
|
109
|
+
*/
|
|
110
|
+
original?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* Array of attachment URLs to include with the request.
|
|
113
|
+
*/
|
|
114
|
+
attachments?: string[];
|
|
97
115
|
}
|
|
98
116
|
/**
|
|
99
117
|
* Builds a URL to render or retrieve a video from Slop Machine.
|
|
@@ -135,6 +153,10 @@ interface SlopTextOptions {
|
|
|
135
153
|
* Defaults to the production URL. Useful for testing against local deployments.
|
|
136
154
|
*/
|
|
137
155
|
baseUrl?: string;
|
|
156
|
+
/**
|
|
157
|
+
* Array of attachment URLs to include with the request.
|
|
158
|
+
*/
|
|
159
|
+
attachments?: string[];
|
|
138
160
|
}
|
|
139
161
|
/**
|
|
140
162
|
* Builds a URL to render or retrieve text from Slop Machine.
|
|
@@ -151,5 +173,15 @@ declare function buildTextUrl(options: SlopTextOptions): string;
|
|
|
151
173
|
* @returns A promise that resolves when the text preload request has been initiated.
|
|
152
174
|
*/
|
|
153
175
|
declare function preloadText(options: SlopTextOptions): Promise<void>;
|
|
176
|
+
/**
|
|
177
|
+
* Uploads a base64 encoded file as a temporary attachment to be used in generation requests.
|
|
178
|
+
*
|
|
179
|
+
* @param base64 - The base64 encoded file data (without the data:mime/type;base64, prefix).
|
|
180
|
+
* @param mimeType - The MIME type of the file.
|
|
181
|
+
* @returns A promise that resolves to an object containing the URL of the uploaded attachment.
|
|
182
|
+
*/
|
|
183
|
+
declare function uploadTempAttachment(base64: string, mimeType: string): Promise<{
|
|
184
|
+
url: string;
|
|
185
|
+
}>;
|
|
154
186
|
|
|
155
|
-
export { type ImageAspectRatio, type SlopImageOptions, type SlopTextOptions, type SlopVideoOptions, type VideoAspectRatio, buildImageUrl, buildTextUrl, buildVideoUrl, interpolatePrompt, preloadImage, preloadText, preloadVideo };
|
|
187
|
+
export { type ImageAspectRatio, type SlopImageOptions, type SlopTextOptions, type SlopVideoOptions, type VideoAspectRatio, buildImageUrl, buildTextUrl, buildVideoUrl, interpolatePrompt, preloadImage, preloadText, preloadVideo, uploadTempAttachment };
|
package/dist/index.d.ts
CHANGED
|
@@ -35,6 +35,15 @@ interface SlopImageOptions {
|
|
|
35
35
|
* Defaults to the production URL. Useful for testing against local deployments.
|
|
36
36
|
*/
|
|
37
37
|
baseUrl?: string;
|
|
38
|
+
/**
|
|
39
|
+
* If `true` (or `?raw=true`), bypasses the WebP optimized media and returns the original generated file (e.g., PNG/JPEG).
|
|
40
|
+
* Defaults to false.
|
|
41
|
+
*/
|
|
42
|
+
original?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Array of attachment URLs to include with the request.
|
|
45
|
+
*/
|
|
46
|
+
attachments?: string[];
|
|
38
47
|
}
|
|
39
48
|
declare function interpolatePrompt(prompt?: string, variables?: Record<string, string | number | undefined | null>): string;
|
|
40
49
|
/**
|
|
@@ -94,6 +103,15 @@ interface SlopVideoOptions {
|
|
|
94
103
|
* Defaults to the production URL. Useful for testing against local deployments.
|
|
95
104
|
*/
|
|
96
105
|
baseUrl?: string;
|
|
106
|
+
/**
|
|
107
|
+
* If `true` (or `?raw=true`), bypasses the WebP optimized media and returns the original generated file.
|
|
108
|
+
* Defaults to false.
|
|
109
|
+
*/
|
|
110
|
+
original?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* Array of attachment URLs to include with the request.
|
|
113
|
+
*/
|
|
114
|
+
attachments?: string[];
|
|
97
115
|
}
|
|
98
116
|
/**
|
|
99
117
|
* Builds a URL to render or retrieve a video from Slop Machine.
|
|
@@ -135,6 +153,10 @@ interface SlopTextOptions {
|
|
|
135
153
|
* Defaults to the production URL. Useful for testing against local deployments.
|
|
136
154
|
*/
|
|
137
155
|
baseUrl?: string;
|
|
156
|
+
/**
|
|
157
|
+
* Array of attachment URLs to include with the request.
|
|
158
|
+
*/
|
|
159
|
+
attachments?: string[];
|
|
138
160
|
}
|
|
139
161
|
/**
|
|
140
162
|
* Builds a URL to render or retrieve text from Slop Machine.
|
|
@@ -151,5 +173,15 @@ declare function buildTextUrl(options: SlopTextOptions): string;
|
|
|
151
173
|
* @returns A promise that resolves when the text preload request has been initiated.
|
|
152
174
|
*/
|
|
153
175
|
declare function preloadText(options: SlopTextOptions): Promise<void>;
|
|
176
|
+
/**
|
|
177
|
+
* Uploads a base64 encoded file as a temporary attachment to be used in generation requests.
|
|
178
|
+
*
|
|
179
|
+
* @param base64 - The base64 encoded file data (without the data:mime/type;base64, prefix).
|
|
180
|
+
* @param mimeType - The MIME type of the file.
|
|
181
|
+
* @returns A promise that resolves to an object containing the URL of the uploaded attachment.
|
|
182
|
+
*/
|
|
183
|
+
declare function uploadTempAttachment(base64: string, mimeType: string): Promise<{
|
|
184
|
+
url: string;
|
|
185
|
+
}>;
|
|
154
186
|
|
|
155
|
-
export { type ImageAspectRatio, type SlopImageOptions, type SlopTextOptions, type SlopVideoOptions, type VideoAspectRatio, buildImageUrl, buildTextUrl, buildVideoUrl, interpolatePrompt, preloadImage, preloadText, preloadVideo };
|
|
187
|
+
export { type ImageAspectRatio, type SlopImageOptions, type SlopTextOptions, type SlopVideoOptions, type VideoAspectRatio, buildImageUrl, buildTextUrl, buildVideoUrl, interpolatePrompt, preloadImage, preloadText, preloadVideo, uploadTempAttachment };
|
package/dist/index.js
CHANGED
|
@@ -26,7 +26,8 @@ __export(index_exports, {
|
|
|
26
26
|
interpolatePrompt: () => interpolatePrompt,
|
|
27
27
|
preloadImage: () => preloadImage,
|
|
28
28
|
preloadText: () => preloadText,
|
|
29
|
-
preloadVideo: () => preloadVideo
|
|
29
|
+
preloadVideo: () => preloadVideo,
|
|
30
|
+
uploadTempAttachment: () => uploadTempAttachment
|
|
30
31
|
});
|
|
31
32
|
module.exports = __toCommonJS(index_exports);
|
|
32
33
|
function interpolatePrompt(prompt, variables) {
|
|
@@ -49,7 +50,9 @@ function buildImageUrl(options) {
|
|
|
49
50
|
aspectRatio = "1:1",
|
|
50
51
|
quality = "fast",
|
|
51
52
|
variables = {},
|
|
52
|
-
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage"
|
|
53
|
+
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage",
|
|
54
|
+
original,
|
|
55
|
+
attachments
|
|
53
56
|
} = options;
|
|
54
57
|
const params = new URLSearchParams();
|
|
55
58
|
params.set("bucketId", bucketId);
|
|
@@ -65,9 +68,15 @@ function buildImageUrl(options) {
|
|
|
65
68
|
} else if (quality === "fast") {
|
|
66
69
|
params.set("quality", "fast");
|
|
67
70
|
}
|
|
71
|
+
if (original) {
|
|
72
|
+
params.set("original", "true");
|
|
73
|
+
}
|
|
68
74
|
if (Object.keys(variables).length > 0) {
|
|
69
75
|
params.set("variables", JSON.stringify(variables));
|
|
70
76
|
}
|
|
77
|
+
if (attachments && attachments.length > 0) {
|
|
78
|
+
params.set("attachments", JSON.stringify(attachments));
|
|
79
|
+
}
|
|
71
80
|
} else {
|
|
72
81
|
params.set("resultId", resultId);
|
|
73
82
|
}
|
|
@@ -94,7 +103,9 @@ function buildVideoUrl(options) {
|
|
|
94
103
|
quality = "fast",
|
|
95
104
|
variables = {},
|
|
96
105
|
duration = 4,
|
|
97
|
-
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo"
|
|
106
|
+
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo",
|
|
107
|
+
original,
|
|
108
|
+
attachments
|
|
98
109
|
} = options;
|
|
99
110
|
const params = new URLSearchParams();
|
|
100
111
|
params.set("bucketId", bucketId);
|
|
@@ -111,9 +122,15 @@ function buildVideoUrl(options) {
|
|
|
111
122
|
if (quality) {
|
|
112
123
|
params.set("quality", quality);
|
|
113
124
|
}
|
|
125
|
+
if (original) {
|
|
126
|
+
params.set("original", "true");
|
|
127
|
+
}
|
|
114
128
|
if (Object.keys(variables).length > 0) {
|
|
115
129
|
params.set("variables", JSON.stringify(variables));
|
|
116
130
|
}
|
|
131
|
+
if (attachments && attachments.length > 0) {
|
|
132
|
+
params.set("attachments", JSON.stringify(attachments));
|
|
133
|
+
}
|
|
117
134
|
} else {
|
|
118
135
|
params.set("resultId", resultId);
|
|
119
136
|
}
|
|
@@ -134,7 +151,8 @@ function buildTextUrl(options) {
|
|
|
134
151
|
version,
|
|
135
152
|
resultId,
|
|
136
153
|
variables = {},
|
|
137
|
-
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText"
|
|
154
|
+
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText",
|
|
155
|
+
attachments
|
|
138
156
|
} = options;
|
|
139
157
|
const params = new URLSearchParams();
|
|
140
158
|
params.set("bucketId", bucketId);
|
|
@@ -148,6 +166,9 @@ function buildTextUrl(options) {
|
|
|
148
166
|
if (Object.keys(variables).length > 0) {
|
|
149
167
|
params.set("variables", JSON.stringify(variables));
|
|
150
168
|
}
|
|
169
|
+
if (attachments && attachments.length > 0) {
|
|
170
|
+
params.set("attachments", JSON.stringify(attachments));
|
|
171
|
+
}
|
|
151
172
|
} else {
|
|
152
173
|
params.set("resultId", resultId);
|
|
153
174
|
}
|
|
@@ -162,6 +183,28 @@ function preloadText(options) {
|
|
|
162
183
|
fetch(buildTextUrl(options), { mode: "no-cors" }).then(() => resolve()).catch((err) => reject(err));
|
|
163
184
|
});
|
|
164
185
|
}
|
|
186
|
+
async function uploadTempAttachment(base64, mimeType) {
|
|
187
|
+
const response = await fetch(
|
|
188
|
+
"https://us-central1-slopmachine-12bfb.cloudfunctions.net/uploadTempAttachment",
|
|
189
|
+
{
|
|
190
|
+
method: "POST",
|
|
191
|
+
headers: {
|
|
192
|
+
"Content-Type": "application/json"
|
|
193
|
+
},
|
|
194
|
+
body: JSON.stringify({ data: { base64, mimeType } })
|
|
195
|
+
}
|
|
196
|
+
);
|
|
197
|
+
if (!response.ok) {
|
|
198
|
+
throw new Error(`Failed to upload attachment: ${response.statusText}`);
|
|
199
|
+
}
|
|
200
|
+
const json = await response.json();
|
|
201
|
+
if (json.error) {
|
|
202
|
+
throw new Error(
|
|
203
|
+
`Failed to upload attachment: ${json.error.message || JSON.stringify(json.error)}`
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return { url: json.result.url };
|
|
207
|
+
}
|
|
165
208
|
// Annotate the CommonJS export names for ESM import in node:
|
|
166
209
|
0 && (module.exports = {
|
|
167
210
|
buildImageUrl,
|
|
@@ -170,6 +213,7 @@ function preloadText(options) {
|
|
|
170
213
|
interpolatePrompt,
|
|
171
214
|
preloadImage,
|
|
172
215
|
preloadText,
|
|
173
|
-
preloadVideo
|
|
216
|
+
preloadVideo,
|
|
217
|
+
uploadTempAttachment
|
|
174
218
|
});
|
|
175
219
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type ImageAspectRatio =\n | \"1:1\"\n | \"2:3\"\n | \"3:2\"\n | \"3:4\"\n | \"4:3\"\n | \"4:5\"\n | \"5:4\"\n | \"9:16\"\n | \"16:9\"\n | \"21:9\";\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\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 } = 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 (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\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 = \"9:16\" | \"16:9\";\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\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 } = 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 (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\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\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 } = 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 } 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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkDO,SAAS,kBACd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,IACA;AAAA,IACA,YAAY,CAAC;AAAA,IACb,UAAU;AAAA,EACZ,IAAI;AAEJ,QAAM,SAAS,IAAI,gBAAgB;AACnC,SAAO,IAAI,YAAY,QAAQ;AAE/B,MAAI,CAAC,UAAU;AACb,QAAI,SAAS;AACX,aAAO,IAAI,WAAW,OAAO,OAAO,CAAC;AAAA,IACvC;AACA,QAAI,UAAU;AACZ,aAAO,IAAI,YAAY,QAAQ;AAAA,IACjC;AAEA,QAAI,OAAO,KAAK,SAAS,EAAE,SAAS,GAAG;AACrC,aAAO,IAAI,aAAa,KAAK,UAAU,SAAS,CAAC;AAAA,IACnD;AAAA,EACF,OAAO;AACL,WAAO,IAAI,YAAY,QAAQ;AAAA,EACjC;AAEA,SAAO,GAAG,OAAO,IAAI,OAAO,SAAS,CAAC;AACxC;AASO,SAAS,YAAY,SAAyC;AACnE,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,QAAI,OAAO,WAAW,aAAa;AACjC,cAAQ;AACR;AAAA,IACF;AAIA,UAAM,aAAa,OAAO,GAAG,EAAE,MAAM,UAAU,CAAC,EAC7C,KAAK,MAAM,QAAQ,CAAC,EACpB,MAAM,CAAC,QAAQ,OAAO,GAAG,CAAC;AAAA,EAC/B,CAAC;AACH;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type ImageAspectRatio =\n | \"1:1\"\n | \"2:3\"\n | \"3:2\"\n | \"3:4\"\n | \"4:3\"\n | \"4:5\"\n | \"5:4\"\n | \"9:16\"\n | \"16:9\"\n | \"21:9\";\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 = \"9:16\" | \"16:9\";\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;AA2DO,SAAS,kBACd,QACA,WACQ;AACR,MAAI,CAAC,OAAQ,QAAO;AACpB,MAAI,OAAO;AACX,MAAI,CAAC,UAAW,QAAO;AAEvB,SAAO,KAAK,SAAS,EAAE,QAAQ,CAAC,QAAQ;AACtC,UAAM,QAAQ,UAAU,GAAG;AAC3B,QAAI,UAAU,UAAa,UAAU,MAAM;AACzC,aAAO,KAAK,QAAQ,IAAI,OAAO,MAAM,GAAG,OAAO,GAAG,GAAG,OAAO,KAAK,CAAC;AAAA,IACpE;AAAA,EACF,CAAC;AACD,SAAO;AACT;AAQO,SAAS,cAAc,SAAmC;AAC/D,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA,cAAc;AAAA,IACd,UAAU;AAAA,IACV,YAAY,CAAC;AAAA,IACb,UAAU;AAAA,IACV;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,SAAS,IAAI,gBAAgB;AACnC,SAAO,IAAI,YAAY,QAAQ;AAE/B,MAAI,CAAC,UAAU;AACb,QAAI,aAAa;AACf,aAAO,IAAI,eAAe,WAAW;AAAA,IACvC;AACA,QAAI,SAAS;AACX,aAAO,IAAI,WAAW,OAAO,OAAO,CAAC;AAAA,IACvC;AACA,QAAI,WA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nD;AAEA,QAAI,eAAe,YAAY,SAAS,GAAG;AACzC,aAAO,IAAI,eAAe,KAAK,UAAU,WAAW,CAAC;AAAA,IACvD;AAAA,EACF,OAAO;AACL,WAAO,IAAI,YAAY,QAAQ;AAAA,EACjC;AAEA,SAAO,GAAG,OAAO,IAAI,OAAO,SAAS,CAAC;AACxC;AASO,SAAS,YAAY,SAAyC;AACnE,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,QAAI,OAAO,WAAW,aAAa;AACjC,cAAQ;AACR;AAAA,IACF;AAIA,UAAM,aAAa,OAAO,GAAG,EAAE,MAAM,UAAU,CAAC,EAC7C,KAAK,MAAM,QAAQ,CAAC,EACpB,MAAM,CAAC,QAAQ,OAAO,GAAG,CAAC;AAAA,EAC/B,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":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -19,7 +19,9 @@ function buildImageUrl(options) {
|
|
|
19
19
|
aspectRatio = "1:1",
|
|
20
20
|
quality = "fast",
|
|
21
21
|
variables = {},
|
|
22
|
-
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage"
|
|
22
|
+
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage",
|
|
23
|
+
original,
|
|
24
|
+
attachments
|
|
23
25
|
} = options;
|
|
24
26
|
const params = new URLSearchParams();
|
|
25
27
|
params.set("bucketId", bucketId);
|
|
@@ -35,9 +37,15 @@ function buildImageUrl(options) {
|
|
|
35
37
|
} else if (quality === "fast") {
|
|
36
38
|
params.set("quality", "fast");
|
|
37
39
|
}
|
|
40
|
+
if (original) {
|
|
41
|
+
params.set("original", "true");
|
|
42
|
+
}
|
|
38
43
|
if (Object.keys(variables).length > 0) {
|
|
39
44
|
params.set("variables", JSON.stringify(variables));
|
|
40
45
|
}
|
|
46
|
+
if (attachments && attachments.length > 0) {
|
|
47
|
+
params.set("attachments", JSON.stringify(attachments));
|
|
48
|
+
}
|
|
41
49
|
} else {
|
|
42
50
|
params.set("resultId", resultId);
|
|
43
51
|
}
|
|
@@ -64,7 +72,9 @@ function buildVideoUrl(options) {
|
|
|
64
72
|
quality = "fast",
|
|
65
73
|
variables = {},
|
|
66
74
|
duration = 4,
|
|
67
|
-
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo"
|
|
75
|
+
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo",
|
|
76
|
+
original,
|
|
77
|
+
attachments
|
|
68
78
|
} = options;
|
|
69
79
|
const params = new URLSearchParams();
|
|
70
80
|
params.set("bucketId", bucketId);
|
|
@@ -81,9 +91,15 @@ function buildVideoUrl(options) {
|
|
|
81
91
|
if (quality) {
|
|
82
92
|
params.set("quality", quality);
|
|
83
93
|
}
|
|
94
|
+
if (original) {
|
|
95
|
+
params.set("original", "true");
|
|
96
|
+
}
|
|
84
97
|
if (Object.keys(variables).length > 0) {
|
|
85
98
|
params.set("variables", JSON.stringify(variables));
|
|
86
99
|
}
|
|
100
|
+
if (attachments && attachments.length > 0) {
|
|
101
|
+
params.set("attachments", JSON.stringify(attachments));
|
|
102
|
+
}
|
|
87
103
|
} else {
|
|
88
104
|
params.set("resultId", resultId);
|
|
89
105
|
}
|
|
@@ -104,7 +120,8 @@ function buildTextUrl(options) {
|
|
|
104
120
|
version,
|
|
105
121
|
resultId,
|
|
106
122
|
variables = {},
|
|
107
|
-
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText"
|
|
123
|
+
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText",
|
|
124
|
+
attachments
|
|
108
125
|
} = options;
|
|
109
126
|
const params = new URLSearchParams();
|
|
110
127
|
params.set("bucketId", bucketId);
|
|
@@ -118,6 +135,9 @@ function buildTextUrl(options) {
|
|
|
118
135
|
if (Object.keys(variables).length > 0) {
|
|
119
136
|
params.set("variables", JSON.stringify(variables));
|
|
120
137
|
}
|
|
138
|
+
if (attachments && attachments.length > 0) {
|
|
139
|
+
params.set("attachments", JSON.stringify(attachments));
|
|
140
|
+
}
|
|
121
141
|
} else {
|
|
122
142
|
params.set("resultId", resultId);
|
|
123
143
|
}
|
|
@@ -132,6 +152,28 @@ function preloadText(options) {
|
|
|
132
152
|
fetch(buildTextUrl(options), { mode: "no-cors" }).then(() => resolve()).catch((err) => reject(err));
|
|
133
153
|
});
|
|
134
154
|
}
|
|
155
|
+
async function uploadTempAttachment(base64, mimeType) {
|
|
156
|
+
const response = await fetch(
|
|
157
|
+
"https://us-central1-slopmachine-12bfb.cloudfunctions.net/uploadTempAttachment",
|
|
158
|
+
{
|
|
159
|
+
method: "POST",
|
|
160
|
+
headers: {
|
|
161
|
+
"Content-Type": "application/json"
|
|
162
|
+
},
|
|
163
|
+
body: JSON.stringify({ data: { base64, mimeType } })
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
if (!response.ok) {
|
|
167
|
+
throw new Error(`Failed to upload attachment: ${response.statusText}`);
|
|
168
|
+
}
|
|
169
|
+
const json = await response.json();
|
|
170
|
+
if (json.error) {
|
|
171
|
+
throw new Error(
|
|
172
|
+
`Failed to upload attachment: ${json.error.message || JSON.stringify(json.error)}`
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
return { url: json.result.url };
|
|
176
|
+
}
|
|
135
177
|
export {
|
|
136
178
|
buildImageUrl,
|
|
137
179
|
buildTextUrl,
|
|
@@ -139,6 +181,7 @@ export {
|
|
|
139
181
|
interpolatePrompt,
|
|
140
182
|
preloadImage,
|
|
141
183
|
preloadText,
|
|
142
|
-
preloadVideo
|
|
184
|
+
preloadVideo,
|
|
185
|
+
uploadTempAttachment
|
|
143
186
|
};
|
|
144
187
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type ImageAspectRatio =\n | \"1:1\"\n | \"2:3\"\n | \"3:2\"\n | \"3:4\"\n | \"4:3\"\n | \"4:5\"\n | \"5:4\"\n | \"9:16\"\n | \"16:9\"\n | \"21:9\";\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\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 } = 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 (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\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 = \"9:16\" | \"16:9\";\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\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 } = 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 (Object.keys(variables).length > 0) {\n params.set(\"variables\", JSON.stringify(variables));\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\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 } = 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 } 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"],"mappings":";AAkDO,SAAS,kBACd,QACA,WACQ;AACR,MAAI,CAAC,OAAQ,QAAO;AACpB,MAAI,OAAO;AACX,MAAI,CAAC,UAAW,QAAO;AAEvB,SAAO,KAAK,SAAS,EAAE,QAAQ,CAAC,QAAQ;AACtC,UAAM,QAAQ,UAAU,GAAG;AAC3B,QAAI,UAAU,UAAa,UAAU,MAAM;AACzC,aAAO,KAAK,QAAQ,IAAI,OAAO,MAAM,GAAG,OAAO,GAAG,GAAG,OAAO,KAAK,CAAC;AAAA,IACpE;AAAA,EACF,CAAC;AACD,SAAO;AACT;AAQO,SAAS,cAAc,SAAmC;AAC/D,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA,cAAc;AAAA,IACd,UAAU;AAAA,IACV,YAAY,CAAC;AAAA,IACb,UAAU;AAAA,EACZ,IAAI;AAEJ,QAAM,SAAS,IAAI,gBAAgB;AACnC,SAAO,IAAI,YAAY,QAAQ;AAE/B,MAAI,CAAC,UAAU;AACb,QAAI,aAAa;AACf,aAAO,IAAI,eAAe,WAAW;AAAA,IACvC;AACA,QAAI,SAAS;AACX,aAAO,IAAI,WAAW,OAAO,OAAO,CAAC;AAAA,IACvC;AACA,QAAI,WAAW,YAAY,QAAQ;AACjC,aAAO,IAAI,WAAW,OAAO;AAAA,IAC/B,WAAW,YAAY,QAAQ;AAC7B,aAAO,IAAI,WAAW,MAAM;AAAA,IAC9B;AAEA,QAAI,OAAO,KAAK,SAAS,EAAE,SAAS,GAAG;AACrC,aAAO,IAAI,aAAa,KAAK,UAAU,SAAS,CAAC;AAAA,IACnD;AAAA,EACF,OAAO;AACL,WAAO,IAAI,YAAY,QAAQ;AAAA,EACjC;AAEA,SAAO,GAAG,OAAO,IAAI,OAAO,SAAS,CAAC;AACx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,MAAM,QAAQ,CAAC,EACpB,MAAM,CAAC,QAAQ,OAAO,GAAG,CAAC;AAAA,EAC/B,CAAC;AACH;AAmCO,SAAS,aAAa,SAAkC;AAC7D,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA,YAAY,CAAC;AAAA,IACb,UAAU;AAAA,EACZ,IAAI;AAEJ,QAAM,SAAS,IAAI,gBAAgB;AACnC,SAAO,IAAI,YAAY,QAAQ;AAE/B,MAAI,CAAC,UAAU;AACb,QAAI,SAAS;AACX,aAAO,IAAI,WAAW,OAAO,OAAO,CAAC;AAAA,IACvC;AACA,QAAI,UAAU;AACZ,aAAO,IAAI,YAAY,QAAQ;AAAA,IACjC;AAEA,QAAI,OAAO,KAAK,SAAS,EAAE,SAAS,GAAG;AACrC,aAAO,IAAI,aAAa,KAAK,UAAU,SAAS,CAAC;AAAA,IACnD;AAAA,EACF,OAAO;AACL,WAAO,IAAI,YAAY,QAAQ;AAAA,EACjC;AAEA,SAAO,GAAG,OAAO,IAAI,OAAO,SAAS,CAAC;AACxC;AASO,SAAS,YAAY,SAAyC;AACnE,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,QAAI,OAAO,WAAW,aAAa;AACjC,cAAQ;AACR;AAAA,IACF;AAIA,UAAM,aAAa,OAAO,GAAG,EAAE,MAAM,UAAU,CAAC,EAC7C,KAAK,MAAM,QAAQ,CAAC,EACpB,MAAM,CAAC,QAAQ,OAAO,GAAG,CAAC;AAAA,EAC/B,CAAC;AACH;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type ImageAspectRatio =\n | \"1:1\"\n | \"2:3\"\n | \"3:2\"\n | \"3:4\"\n | \"4:3\"\n | \"4:5\"\n | \"5:4\"\n | \"9:16\"\n | \"16:9\"\n | \"21:9\";\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 = \"9:16\" | \"16:9\";\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":";AA2DO,SAAS,kBACd,QACA,WACQ;AACR,MAAI,CAAC,OAAQ,QAAO;AACpB,MAAI,OAAO;AACX,MAAI,CAAC,UAAW,QAAO;AAEvB,SAAO,KAAK,SAAS,EAAE,QAAQ,CAAC,QAAQ;AACtC,UAAM,QAAQ,UAAU,GAAG;AAC3B,QAAI,UAAU,UAAa,UAAU,MAAM;AACzC,aAAO,KAAK,QAAQ,IAAI,OAAO,MAAM,GAAG,OAAO,GAAG,GAAG,OAAO,KAAK,CAAC;AAAA,IACpE;AAAA,EACF,CAAC;AACD,SAAO;AACT;AAQO,SAAS,cAAc,SAAmC;AAC/D,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA,cAAc;AAAA,IACd,UAAU;AAAA,IACV,YAAY,CAAC;AAAA,IACb,UAAU;AAAA,IACV;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,SAAS,IAAI,gBAAgB;AACnC,SAAO,IAAI,YAAY,QAAQ;AAE/B,MAAI,CAAC,UAAU;AACb,QAAI,aAAa;AACf,aAAO,IAAI,eAAe,WAAW;AAAA,IACvC;AACA,QAAI,SAAS;AACX,a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.1.25",
|
|
4
4
|
"description": "The official Core / VanillaJS SDK for Slop Machine",
|
|
5
|
-
"llms": "https://slopmachine
|
|
6
|
-
"llmsFull": "https://slopmachine
|
|
5
|
+
"llms": "https://docs.slopmachine.dev/llms.txt",
|
|
6
|
+
"llmsFull": "https://docs.slopmachine.dev/llms-full.txt",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"module": "dist/index.mjs",
|
|
9
9
|
"types": "dist/index.d.ts",
|
package/src/index.ts
CHANGED
|
@@ -46,6 +46,15 @@ export interface SlopImageOptions {
|
|
|
46
46
|
* Defaults to the production URL. Useful for testing against local deployments.
|
|
47
47
|
*/
|
|
48
48
|
baseUrl?: string;
|
|
49
|
+
/**
|
|
50
|
+
* If `true` (or `?raw=true`), bypasses the WebP optimized media and returns the original generated file (e.g., PNG/JPEG).
|
|
51
|
+
* Defaults to false.
|
|
52
|
+
*/
|
|
53
|
+
original?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Array of attachment URLs to include with the request.
|
|
56
|
+
*/
|
|
57
|
+
attachments?: string[];
|
|
49
58
|
}
|
|
50
59
|
|
|
51
60
|
export function interpolatePrompt(
|
|
@@ -80,6 +89,8 @@ export function buildImageUrl(options: SlopImageOptions): string {
|
|
|
80
89
|
quality = "fast",
|
|
81
90
|
variables = {},
|
|
82
91
|
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage",
|
|
92
|
+
original,
|
|
93
|
+
attachments,
|
|
83
94
|
} = options;
|
|
84
95
|
|
|
85
96
|
const params = new URLSearchParams();
|
|
@@ -98,9 +109,17 @@ export function buildImageUrl(options: SlopImageOptions): string {
|
|
|
98
109
|
params.set("quality", "fast");
|
|
99
110
|
}
|
|
100
111
|
|
|
112
|
+
if (original) {
|
|
113
|
+
params.set("original", "true");
|
|
114
|
+
}
|
|
115
|
+
|
|
101
116
|
if (Object.keys(variables).length > 0) {
|
|
102
117
|
params.set("variables", JSON.stringify(variables));
|
|
103
118
|
}
|
|
119
|
+
|
|
120
|
+
if (attachments && attachments.length > 0) {
|
|
121
|
+
params.set("attachments", JSON.stringify(attachments));
|
|
122
|
+
}
|
|
104
123
|
} else {
|
|
105
124
|
params.set("resultId", resultId);
|
|
106
125
|
}
|
|
@@ -172,6 +191,15 @@ export interface SlopVideoOptions {
|
|
|
172
191
|
* Defaults to the production URL. Useful for testing against local deployments.
|
|
173
192
|
*/
|
|
174
193
|
baseUrl?: string;
|
|
194
|
+
/**
|
|
195
|
+
* If `true` (or `?raw=true`), bypasses the WebP optimized media and returns the original generated file.
|
|
196
|
+
* Defaults to false.
|
|
197
|
+
*/
|
|
198
|
+
original?: boolean;
|
|
199
|
+
/**
|
|
200
|
+
* Array of attachment URLs to include with the request.
|
|
201
|
+
*/
|
|
202
|
+
attachments?: string[];
|
|
175
203
|
}
|
|
176
204
|
|
|
177
205
|
/**
|
|
@@ -190,6 +218,8 @@ export function buildVideoUrl(options: SlopVideoOptions): string {
|
|
|
190
218
|
variables = {},
|
|
191
219
|
duration = 4,
|
|
192
220
|
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo",
|
|
221
|
+
original,
|
|
222
|
+
attachments,
|
|
193
223
|
} = options;
|
|
194
224
|
|
|
195
225
|
const params = new URLSearchParams();
|
|
@@ -209,9 +239,17 @@ export function buildVideoUrl(options: SlopVideoOptions): string {
|
|
|
209
239
|
params.set("quality", quality);
|
|
210
240
|
}
|
|
211
241
|
|
|
242
|
+
if (original) {
|
|
243
|
+
params.set("original", "true");
|
|
244
|
+
}
|
|
245
|
+
|
|
212
246
|
if (Object.keys(variables).length > 0) {
|
|
213
247
|
params.set("variables", JSON.stringify(variables));
|
|
214
248
|
}
|
|
249
|
+
|
|
250
|
+
if (attachments && attachments.length > 0) {
|
|
251
|
+
params.set("attachments", JSON.stringify(attachments));
|
|
252
|
+
}
|
|
215
253
|
} else {
|
|
216
254
|
params.set("resultId", resultId);
|
|
217
255
|
}
|
|
@@ -266,6 +304,10 @@ export interface SlopTextOptions {
|
|
|
266
304
|
* Defaults to the production URL. Useful for testing against local deployments.
|
|
267
305
|
*/
|
|
268
306
|
baseUrl?: string;
|
|
307
|
+
/**
|
|
308
|
+
* Array of attachment URLs to include with the request.
|
|
309
|
+
*/
|
|
310
|
+
attachments?: string[];
|
|
269
311
|
}
|
|
270
312
|
|
|
271
313
|
/**
|
|
@@ -281,6 +323,7 @@ export function buildTextUrl(options: SlopTextOptions): string {
|
|
|
281
323
|
resultId,
|
|
282
324
|
variables = {},
|
|
283
325
|
baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText",
|
|
326
|
+
attachments,
|
|
284
327
|
} = options;
|
|
285
328
|
|
|
286
329
|
const params = new URLSearchParams();
|
|
@@ -297,6 +340,10 @@ export function buildTextUrl(options: SlopTextOptions): string {
|
|
|
297
340
|
if (Object.keys(variables).length > 0) {
|
|
298
341
|
params.set("variables", JSON.stringify(variables));
|
|
299
342
|
}
|
|
343
|
+
|
|
344
|
+
if (attachments && attachments.length > 0) {
|
|
345
|
+
params.set("attachments", JSON.stringify(attachments));
|
|
346
|
+
}
|
|
300
347
|
} else {
|
|
301
348
|
params.set("resultId", resultId);
|
|
302
349
|
}
|
|
@@ -325,3 +372,36 @@ export function preloadText(options: SlopTextOptions): Promise<void> {
|
|
|
325
372
|
.catch((err) => reject(err));
|
|
326
373
|
});
|
|
327
374
|
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Uploads a base64 encoded file as a temporary attachment to be used in generation requests.
|
|
378
|
+
*
|
|
379
|
+
* @param base64 - The base64 encoded file data (without the data:mime/type;base64, prefix).
|
|
380
|
+
* @param mimeType - The MIME type of the file.
|
|
381
|
+
* @returns A promise that resolves to an object containing the URL of the uploaded attachment.
|
|
382
|
+
*/
|
|
383
|
+
export async function uploadTempAttachment(
|
|
384
|
+
base64: string,
|
|
385
|
+
mimeType: string,
|
|
386
|
+
): Promise<{ url: string }> {
|
|
387
|
+
const response = await fetch(
|
|
388
|
+
"https://us-central1-slopmachine-12bfb.cloudfunctions.net/uploadTempAttachment",
|
|
389
|
+
{
|
|
390
|
+
method: "POST",
|
|
391
|
+
headers: {
|
|
392
|
+
"Content-Type": "application/json",
|
|
393
|
+
},
|
|
394
|
+
body: JSON.stringify({ data: { base64, mimeType } }),
|
|
395
|
+
},
|
|
396
|
+
);
|
|
397
|
+
if (!response.ok) {
|
|
398
|
+
throw new Error(`Failed to upload attachment: ${response.statusText}`);
|
|
399
|
+
}
|
|
400
|
+
const json = await response.json();
|
|
401
|
+
if (json.error) {
|
|
402
|
+
throw new Error(
|
|
403
|
+
`Failed to upload attachment: ${json.error.message || JSON.stringify(json.error)}`,
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
return { url: json.result.url };
|
|
407
|
+
}
|