@slopmachine/core 0.1.24 → 0.1.26

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 CHANGED
@@ -1,4 +1,6 @@
1
- type ImageAspectRatio = "1:1" | "2:3" | "3:2" | "3:4" | "4:3" | "4:5" | "5:4" | "9:16" | "16:9" | "21:9";
1
+ import { AspectRatio, VideoAspectRatio } from '@pixerate/schemas';
2
+ export { AspectRatio as ImageAspectRatio, VideoAspectRatio } from '@pixerate/schemas';
3
+
2
4
  interface SlopImageOptions {
3
5
  /**
4
6
  * The unique identifier of your Slop Machine bucket.
@@ -18,7 +20,7 @@ interface SlopImageOptions {
18
20
  * The aspect ratio of the generated image. Defaults to "1:1".
19
21
  * Common values: "1:1", "16:9", "9:16", "4:3", "3:4".
20
22
  */
21
- aspectRatio?: ImageAspectRatio;
23
+ aspectRatio?: AspectRatio;
22
24
  /**
23
25
  * Dynamic variables to interpolate into the prompt.
24
26
  * E.g., if prompt is "A photo of a {color} dog", pass { color: "brown" }.
@@ -40,6 +42,10 @@ interface SlopImageOptions {
40
42
  * Defaults to false.
41
43
  */
42
44
  original?: boolean;
45
+ /**
46
+ * Array of attachment URLs to include with the request.
47
+ */
48
+ attachments?: string[];
43
49
  }
44
50
  declare function interpolatePrompt(prompt?: string, variables?: Record<string, string | number | undefined | null>): string;
45
51
  /**
@@ -57,7 +63,7 @@ declare function buildImageUrl(options: SlopImageOptions): string;
57
63
  * @returns A promise that resolves when the image has been loaded.
58
64
  */
59
65
  declare function preloadImage(options: SlopImageOptions): Promise<void>;
60
- type VideoAspectRatio = "9:16" | "16:9";
66
+
61
67
  interface SlopVideoOptions {
62
68
  /**
63
69
  * The unique identifier of your Slop Machine bucket.
@@ -104,6 +110,10 @@ interface SlopVideoOptions {
104
110
  * Defaults to false.
105
111
  */
106
112
  original?: boolean;
113
+ /**
114
+ * Array of attachment URLs to include with the request.
115
+ */
116
+ attachments?: string[];
107
117
  }
108
118
  /**
109
119
  * Builds a URL to render or retrieve a video from Slop Machine.
@@ -145,6 +155,10 @@ interface SlopTextOptions {
145
155
  * Defaults to the production URL. Useful for testing against local deployments.
146
156
  */
147
157
  baseUrl?: string;
158
+ /**
159
+ * Array of attachment URLs to include with the request.
160
+ */
161
+ attachments?: string[];
148
162
  }
149
163
  /**
150
164
  * Builds a URL to render or retrieve text from Slop Machine.
@@ -161,5 +175,15 @@ declare function buildTextUrl(options: SlopTextOptions): string;
161
175
  * @returns A promise that resolves when the text preload request has been initiated.
162
176
  */
163
177
  declare function preloadText(options: SlopTextOptions): Promise<void>;
178
+ /**
179
+ * Uploads a base64 encoded file as a temporary attachment to be used in generation requests.
180
+ *
181
+ * @param base64 - The base64 encoded file data (without the data:mime/type;base64, prefix).
182
+ * @param mimeType - The MIME type of the file.
183
+ * @returns A promise that resolves to an object containing the URL of the uploaded attachment.
184
+ */
185
+ declare function uploadTempAttachment(base64: string, mimeType: string): Promise<{
186
+ url: string;
187
+ }>;
164
188
 
165
- export { type ImageAspectRatio, type SlopImageOptions, type SlopTextOptions, type SlopVideoOptions, type VideoAspectRatio, buildImageUrl, buildTextUrl, buildVideoUrl, interpolatePrompt, preloadImage, preloadText, preloadVideo };
189
+ export { type SlopImageOptions, type SlopTextOptions, type SlopVideoOptions, buildImageUrl, buildTextUrl, buildVideoUrl, interpolatePrompt, preloadImage, preloadText, preloadVideo, uploadTempAttachment };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- type ImageAspectRatio = "1:1" | "2:3" | "3:2" | "3:4" | "4:3" | "4:5" | "5:4" | "9:16" | "16:9" | "21:9";
1
+ import { AspectRatio, VideoAspectRatio } from '@pixerate/schemas';
2
+ export { AspectRatio as ImageAspectRatio, VideoAspectRatio } from '@pixerate/schemas';
3
+
2
4
  interface SlopImageOptions {
3
5
  /**
4
6
  * The unique identifier of your Slop Machine bucket.
@@ -18,7 +20,7 @@ interface SlopImageOptions {
18
20
  * The aspect ratio of the generated image. Defaults to "1:1".
19
21
  * Common values: "1:1", "16:9", "9:16", "4:3", "3:4".
20
22
  */
21
- aspectRatio?: ImageAspectRatio;
23
+ aspectRatio?: AspectRatio;
22
24
  /**
23
25
  * Dynamic variables to interpolate into the prompt.
24
26
  * E.g., if prompt is "A photo of a {color} dog", pass { color: "brown" }.
@@ -40,6 +42,10 @@ interface SlopImageOptions {
40
42
  * Defaults to false.
41
43
  */
42
44
  original?: boolean;
45
+ /**
46
+ * Array of attachment URLs to include with the request.
47
+ */
48
+ attachments?: string[];
43
49
  }
44
50
  declare function interpolatePrompt(prompt?: string, variables?: Record<string, string | number | undefined | null>): string;
45
51
  /**
@@ -57,7 +63,7 @@ declare function buildImageUrl(options: SlopImageOptions): string;
57
63
  * @returns A promise that resolves when the image has been loaded.
58
64
  */
59
65
  declare function preloadImage(options: SlopImageOptions): Promise<void>;
60
- type VideoAspectRatio = "9:16" | "16:9";
66
+
61
67
  interface SlopVideoOptions {
62
68
  /**
63
69
  * The unique identifier of your Slop Machine bucket.
@@ -104,6 +110,10 @@ interface SlopVideoOptions {
104
110
  * Defaults to false.
105
111
  */
106
112
  original?: boolean;
113
+ /**
114
+ * Array of attachment URLs to include with the request.
115
+ */
116
+ attachments?: string[];
107
117
  }
108
118
  /**
109
119
  * Builds a URL to render or retrieve a video from Slop Machine.
@@ -145,6 +155,10 @@ interface SlopTextOptions {
145
155
  * Defaults to the production URL. Useful for testing against local deployments.
146
156
  */
147
157
  baseUrl?: string;
158
+ /**
159
+ * Array of attachment URLs to include with the request.
160
+ */
161
+ attachments?: string[];
148
162
  }
149
163
  /**
150
164
  * Builds a URL to render or retrieve text from Slop Machine.
@@ -161,5 +175,15 @@ declare function buildTextUrl(options: SlopTextOptions): string;
161
175
  * @returns A promise that resolves when the text preload request has been initiated.
162
176
  */
163
177
  declare function preloadText(options: SlopTextOptions): Promise<void>;
178
+ /**
179
+ * Uploads a base64 encoded file as a temporary attachment to be used in generation requests.
180
+ *
181
+ * @param base64 - The base64 encoded file data (without the data:mime/type;base64, prefix).
182
+ * @param mimeType - The MIME type of the file.
183
+ * @returns A promise that resolves to an object containing the URL of the uploaded attachment.
184
+ */
185
+ declare function uploadTempAttachment(base64: string, mimeType: string): Promise<{
186
+ url: string;
187
+ }>;
164
188
 
165
- export { type ImageAspectRatio, type SlopImageOptions, type SlopTextOptions, type SlopVideoOptions, type VideoAspectRatio, buildImageUrl, buildTextUrl, buildVideoUrl, interpolatePrompt, preloadImage, preloadText, preloadVideo };
189
+ export { type SlopImageOptions, type SlopTextOptions, type SlopVideoOptions, 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) {
@@ -50,7 +51,8 @@ function buildImageUrl(options) {
50
51
  quality = "fast",
51
52
  variables = {},
52
53
  baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage",
53
- original
54
+ original,
55
+ attachments
54
56
  } = options;
55
57
  const params = new URLSearchParams();
56
58
  params.set("bucketId", bucketId);
@@ -72,6 +74,9 @@ function buildImageUrl(options) {
72
74
  if (Object.keys(variables).length > 0) {
73
75
  params.set("variables", JSON.stringify(variables));
74
76
  }
77
+ if (attachments && attachments.length > 0) {
78
+ params.set("attachments", JSON.stringify(attachments));
79
+ }
75
80
  } else {
76
81
  params.set("resultId", resultId);
77
82
  }
@@ -99,7 +104,8 @@ function buildVideoUrl(options) {
99
104
  variables = {},
100
105
  duration = 4,
101
106
  baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo",
102
- original
107
+ original,
108
+ attachments
103
109
  } = options;
104
110
  const params = new URLSearchParams();
105
111
  params.set("bucketId", bucketId);
@@ -122,6 +128,9 @@ function buildVideoUrl(options) {
122
128
  if (Object.keys(variables).length > 0) {
123
129
  params.set("variables", JSON.stringify(variables));
124
130
  }
131
+ if (attachments && attachments.length > 0) {
132
+ params.set("attachments", JSON.stringify(attachments));
133
+ }
125
134
  } else {
126
135
  params.set("resultId", resultId);
127
136
  }
@@ -142,7 +151,8 @@ function buildTextUrl(options) {
142
151
  version,
143
152
  resultId,
144
153
  variables = {},
145
- baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText"
154
+ baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText",
155
+ attachments
146
156
  } = options;
147
157
  const params = new URLSearchParams();
148
158
  params.set("bucketId", bucketId);
@@ -156,6 +166,9 @@ function buildTextUrl(options) {
156
166
  if (Object.keys(variables).length > 0) {
157
167
  params.set("variables", JSON.stringify(variables));
158
168
  }
169
+ if (attachments && attachments.length > 0) {
170
+ params.set("attachments", JSON.stringify(attachments));
171
+ }
159
172
  } else {
160
173
  params.set("resultId", resultId);
161
174
  }
@@ -170,6 +183,28 @@ function preloadText(options) {
170
183
  fetch(buildTextUrl(options), { mode: "no-cors" }).then(() => resolve()).catch((err) => reject(err));
171
184
  });
172
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
+ }
173
208
  // Annotate the CommonJS export names for ESM import in node:
174
209
  0 && (module.exports = {
175
210
  buildImageUrl,
@@ -178,6 +213,7 @@ function preloadText(options) {
178
213
  interpolatePrompt,
179
214
  preloadImage,
180
215
  preloadText,
181
- preloadVideo
216
+ preloadVideo,
217
+ uploadTempAttachment
182
218
  });
183
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 * 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\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 } = 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 } 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\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 } = 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 } 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;AAuDO,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,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,WAAW,YAAY,QAAQ;AACjC,aAAO,IAAI,WAAW,OAAO;AAAA,IAC/B,WAAW,YAAY,QAAQ;AAC7B,aAAO,IAAI,WAAW,MAAM;AAAA,IAC9B;AAEA,QAAI,UAAU;AACZ,aAAO,IAAI,YAAY,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EAC/B,CAAC;AACH;","names":[]}
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,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;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AAK;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
@@ -20,7 +20,8 @@ function buildImageUrl(options) {
20
20
  quality = "fast",
21
21
  variables = {},
22
22
  baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage",
23
- original
23
+ original,
24
+ attachments
24
25
  } = options;
25
26
  const params = new URLSearchParams();
26
27
  params.set("bucketId", bucketId);
@@ -42,6 +43,9 @@ function buildImageUrl(options) {
42
43
  if (Object.keys(variables).length > 0) {
43
44
  params.set("variables", JSON.stringify(variables));
44
45
  }
46
+ if (attachments && attachments.length > 0) {
47
+ params.set("attachments", JSON.stringify(attachments));
48
+ }
45
49
  } else {
46
50
  params.set("resultId", resultId);
47
51
  }
@@ -69,7 +73,8 @@ function buildVideoUrl(options) {
69
73
  variables = {},
70
74
  duration = 4,
71
75
  baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo",
72
- original
76
+ original,
77
+ attachments
73
78
  } = options;
74
79
  const params = new URLSearchParams();
75
80
  params.set("bucketId", bucketId);
@@ -92,6 +97,9 @@ function buildVideoUrl(options) {
92
97
  if (Object.keys(variables).length > 0) {
93
98
  params.set("variables", JSON.stringify(variables));
94
99
  }
100
+ if (attachments && attachments.length > 0) {
101
+ params.set("attachments", JSON.stringify(attachments));
102
+ }
95
103
  } else {
96
104
  params.set("resultId", resultId);
97
105
  }
@@ -112,7 +120,8 @@ function buildTextUrl(options) {
112
120
  version,
113
121
  resultId,
114
122
  variables = {},
115
- baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText"
123
+ baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText",
124
+ attachments
116
125
  } = options;
117
126
  const params = new URLSearchParams();
118
127
  params.set("bucketId", bucketId);
@@ -126,6 +135,9 @@ function buildTextUrl(options) {
126
135
  if (Object.keys(variables).length > 0) {
127
136
  params.set("variables", JSON.stringify(variables));
128
137
  }
138
+ if (attachments && attachments.length > 0) {
139
+ params.set("attachments", JSON.stringify(attachments));
140
+ }
129
141
  } else {
130
142
  params.set("resultId", resultId);
131
143
  }
@@ -140,6 +152,28 @@ function preloadText(options) {
140
152
  fetch(buildTextUrl(options), { mode: "no-cors" }).then(() => resolve()).catch((err) => reject(err));
141
153
  });
142
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
+ }
143
177
  export {
144
178
  buildImageUrl,
145
179
  buildTextUrl,
@@ -147,6 +181,7 @@ export {
147
181
  interpolatePrompt,
148
182
  preloadImage,
149
183
  preloadText,
150
- preloadVideo
184
+ preloadVideo,
185
+ uploadTempAttachment
151
186
  };
152
187
  //# sourceMappingURL=index.mjs.map
@@ -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 * 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\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 } = 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 } 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\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 } = 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 } 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":";AAuDO,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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,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":["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":";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,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,WAAW,YAAY,QAAQ;AACjC,aAAO,IAAI,WAAW,OAAO;AAAA,IAC/B,WAAW,YAAY,QAAQ;AAC7B,aAAO,IAAI,WAAW,MAAM;AAAA,IAC9B;AAEA,QAAI,UAAU;AACZ,aAAO,IAAI,YAAY,MAAM;AAAA,IAC/B;AAEA,QAAI,OAAO,KAAK,SAAS,EAAE,SAAS,GAAG;AACrC,aAAO,IAAI,aAAa,KAAK,UAAU,SAAS,CAAC;AAAA,IACnD;AAEA,QAAI,eAAe,YAAY,SAAS,GAAG;AACzC,aAAO,IAAI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AAM,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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slopmachine/core",
3
- "version": "0.1.24",
3
+ "version": "0.1.26",
4
4
  "description": "The official Core / VanillaJS SDK for Slop Machine",
5
5
  "llms": "https://docs.slopmachine.dev/llms.txt",
6
6
  "llmsFull": "https://docs.slopmachine.dev/llms-full.txt",
@@ -27,5 +27,8 @@
27
27
  "tsup": "^8.0.0",
28
28
  "typescript": "^5.0.0"
29
29
  },
30
+ "dependencies": {
31
+ "@pixerate/schemas": "^1.0.0"
32
+ },
30
33
  "license": "MIT"
31
34
  }
package/src/index.ts CHANGED
@@ -1,14 +1,5 @@
1
- export type ImageAspectRatio =
2
- | "1:1"
3
- | "2:3"
4
- | "3:2"
5
- | "3:4"
6
- | "4:3"
7
- | "4:5"
8
- | "5:4"
9
- | "9:16"
10
- | "16:9"
11
- | "21:9";
1
+ import { AspectRatio as ImageAspectRatio, VideoAspectRatio } from "@pixerate/schemas";
2
+ export type { ImageAspectRatio };
12
3
 
13
4
  export interface SlopImageOptions {
14
5
  /**
@@ -51,6 +42,10 @@ export interface SlopImageOptions {
51
42
  * Defaults to false.
52
43
  */
53
44
  original?: boolean;
45
+ /**
46
+ * Array of attachment URLs to include with the request.
47
+ */
48
+ attachments?: string[];
54
49
  }
55
50
 
56
51
  export function interpolatePrompt(
@@ -86,6 +81,7 @@ export function buildImageUrl(options: SlopImageOptions): string {
86
81
  variables = {},
87
82
  baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderImage",
88
83
  original,
84
+ attachments,
89
85
  } = options;
90
86
 
91
87
  const params = new URLSearchParams();
@@ -111,6 +107,10 @@ export function buildImageUrl(options: SlopImageOptions): string {
111
107
  if (Object.keys(variables).length > 0) {
112
108
  params.set("variables", JSON.stringify(variables));
113
109
  }
110
+
111
+ if (attachments && attachments.length > 0) {
112
+ params.set("attachments", JSON.stringify(attachments));
113
+ }
114
114
  } else {
115
115
  params.set("resultId", resultId);
116
116
  }
@@ -139,7 +139,7 @@ export function preloadImage(options: SlopImageOptions): Promise<void> {
139
139
  });
140
140
  }
141
141
 
142
- export type VideoAspectRatio = "9:16" | "16:9";
142
+ export type { VideoAspectRatio };
143
143
 
144
144
  export interface SlopVideoOptions {
145
145
  /**
@@ -187,6 +187,10 @@ export interface SlopVideoOptions {
187
187
  * Defaults to false.
188
188
  */
189
189
  original?: boolean;
190
+ /**
191
+ * Array of attachment URLs to include with the request.
192
+ */
193
+ attachments?: string[];
190
194
  }
191
195
 
192
196
  /**
@@ -206,6 +210,7 @@ export function buildVideoUrl(options: SlopVideoOptions): string {
206
210
  duration = 4,
207
211
  baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderVideo",
208
212
  original,
213
+ attachments,
209
214
  } = options;
210
215
 
211
216
  const params = new URLSearchParams();
@@ -232,6 +237,10 @@ export function buildVideoUrl(options: SlopVideoOptions): string {
232
237
  if (Object.keys(variables).length > 0) {
233
238
  params.set("variables", JSON.stringify(variables));
234
239
  }
240
+
241
+ if (attachments && attachments.length > 0) {
242
+ params.set("attachments", JSON.stringify(attachments));
243
+ }
235
244
  } else {
236
245
  params.set("resultId", resultId);
237
246
  }
@@ -286,6 +295,10 @@ export interface SlopTextOptions {
286
295
  * Defaults to the production URL. Useful for testing against local deployments.
287
296
  */
288
297
  baseUrl?: string;
298
+ /**
299
+ * Array of attachment URLs to include with the request.
300
+ */
301
+ attachments?: string[];
289
302
  }
290
303
 
291
304
  /**
@@ -301,6 +314,7 @@ export function buildTextUrl(options: SlopTextOptions): string {
301
314
  resultId,
302
315
  variables = {},
303
316
  baseUrl = "https://us-central1-slopmachine-12bfb.cloudfunctions.net/renderText",
317
+ attachments,
304
318
  } = options;
305
319
 
306
320
  const params = new URLSearchParams();
@@ -317,6 +331,10 @@ export function buildTextUrl(options: SlopTextOptions): string {
317
331
  if (Object.keys(variables).length > 0) {
318
332
  params.set("variables", JSON.stringify(variables));
319
333
  }
334
+
335
+ if (attachments && attachments.length > 0) {
336
+ params.set("attachments", JSON.stringify(attachments));
337
+ }
320
338
  } else {
321
339
  params.set("resultId", resultId);
322
340
  }
@@ -345,3 +363,36 @@ export function preloadText(options: SlopTextOptions): Promise<void> {
345
363
  .catch((err) => reject(err));
346
364
  });
347
365
  }
366
+
367
+ /**
368
+ * Uploads a base64 encoded file as a temporary attachment to be used in generation requests.
369
+ *
370
+ * @param base64 - The base64 encoded file data (without the data:mime/type;base64, prefix).
371
+ * @param mimeType - The MIME type of the file.
372
+ * @returns A promise that resolves to an object containing the URL of the uploaded attachment.
373
+ */
374
+ export async function uploadTempAttachment(
375
+ base64: string,
376
+ mimeType: string,
377
+ ): Promise<{ url: string }> {
378
+ const response = await fetch(
379
+ "https://us-central1-slopmachine-12bfb.cloudfunctions.net/uploadTempAttachment",
380
+ {
381
+ method: "POST",
382
+ headers: {
383
+ "Content-Type": "application/json",
384
+ },
385
+ body: JSON.stringify({ data: { base64, mimeType } }),
386
+ },
387
+ );
388
+ if (!response.ok) {
389
+ throw new Error(`Failed to upload attachment: ${response.statusText}`);
390
+ }
391
+ const json = await response.json();
392
+ if (json.error) {
393
+ throw new Error(
394
+ `Failed to upload attachment: ${json.error.message || JSON.stringify(json.error)}`,
395
+ );
396
+ }
397
+ return { url: json.result.url };
398
+ }