diff --git a/edge-stream/api/vary.mjs b/edge-stream/api/vary.mjs deleted file mode 100644 index 3716e9fbb4..0000000000 --- a/edge-stream/api/vary.mjs +++ /dev/null @@ -1,47 +0,0 @@ -// TODO: Make this a netlify function. - -import { Configuration, OpenAIApi } from "openai"; -import { corsHeaders } from "../help.mjs"; - -export default async function handler(req) { - const headers = corsHeaders(req); - - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - - const openai = new OpenAIApi(configuration); - const submission = await req.json(); - - // Decode the base64-encoded image data - const imageData = base64ToUint8Array(submission.image.split(",")[1]); - - console.log(imageData); - - try { - console.log('hi'); - - const response = await openai.createImageVariation(submission.image, 2, "256x256"); - // return new Response(JSON.stringify({ url: response.data.data[0].url }), { - // status: 200, - // headers: { "Content-Type": "application/json", ...headers }, - // }); - } catch (error) { - return new Response(JSON.stringify({ error: "Error varying painting" }), { - status: 500, - headers: { "Content-Type": "application/json", ...headers }, - }); - } -} - -function base64ToUint8Array(base64) { - const binary_string = atob(base64); - const len = binary_string.length; - const bytes = new Uint8Array(len); - for (let i = 0; i < len; i++) { - bytes[i] = binary_string.charCodeAt(i); - } - return bytes; -} - -export const config = { runtime: "edge" }; diff --git a/edge-stream/help.mjs b/edge-stream/help.mjs index 1a0d31abba..161292d12a 100644 --- a/edge-stream/help.mjs +++ b/edge-stream/help.mjs @@ -1,5 +1,10 @@ export function corsHeaders(request) { - const origin = request.headers.get("Origin"); + let origin; + if (typeof request.headers.get === "function") { + origin = request.headers.get("Origin"); // For edge functions.. + } else { + origin = request.headers.origin; // For serverless functions. + } const production = origin === "https://aesthetic.computer"; const allowedOrigin = production ? "https://aesthetic.computer" : "*"; diff --git a/edge-stream/package-lock.json b/edge-stream/package-lock.json index 2d356adbf9..09c0ababe5 100644 --- a/edge-stream/package-lock.json +++ b/edge-stream/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "busboy": "^1.6.0", "eventsource-parser": "^1.0.0", + "multiparty": "^4.2.3", "openai": "^3.2.1", "openai-streams": "^5.3.0" } @@ -57,6 +58,14 @@ "node": ">=0.4.0" } }, + "node_modules/depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/dotenv": { "version": "16.0.3", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", @@ -105,6 +114,26 @@ "node": ">= 6" } }, + "node_modules/http-errors": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.8.1.tgz", + "integrity": "sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==", + "dependencies": { + "depd": "~1.1.2", + "inherits": "2.0.4", + "setprototypeof": "1.2.0", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.1" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", @@ -124,6 +153,19 @@ "node": ">= 0.6" } }, + "node_modules/multiparty": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/multiparty/-/multiparty-4.2.3.tgz", + "integrity": "sha512-Ak6EUJZuhGS8hJ3c2fY6UW5MbkGUPMBEGd13djUzoY/BHqV/gTuFWtC6IuVA7A2+v3yjBS6c4or50xhzTQZImQ==", + "dependencies": { + "http-errors": "~1.8.1", + "safe-buffer": "5.2.1", + "uid-safe": "2.1.5" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/openai": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/openai/-/openai-3.2.1.tgz", @@ -146,6 +188,38 @@ "node": ">=14" } }, + "node_modules/random-bytes": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz", + "integrity": "sha512-iv7LhNVO047HzYR3InF6pUcUsPQiHTM1Qal51DcGSuZFBil1aBBWG5eHPNek7bvILMaYJ/8RU1e8w1AMdHmLQQ==", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" + }, "node_modules/shim-streams": { "version": "0.0.2", "resolved": "https://registry.npmjs.org/shim-streams/-/shim-streams-0.0.2.tgz", @@ -154,6 +228,14 @@ "node": ">=14" } }, + "node_modules/statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/streamsearch": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", @@ -162,6 +244,25 @@ "node": ">=10.0.0" } }, + "node_modules/toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/uid-safe": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz", + "integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==", + "dependencies": { + "random-bytes": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/web-streams-polyfill": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz", @@ -218,6 +319,11 @@ "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==" }, + "depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==" + }, "dotenv": { "version": "16.0.3", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", @@ -243,6 +349,23 @@ "mime-types": "^2.1.12" } }, + "http-errors": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.8.1.tgz", + "integrity": "sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==", + "requires": { + "depd": "~1.1.2", + "inherits": "2.0.4", + "setprototypeof": "1.2.0", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.1" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, "mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", @@ -256,6 +379,16 @@ "mime-db": "1.52.0" } }, + "multiparty": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/multiparty/-/multiparty-4.2.3.tgz", + "integrity": "sha512-Ak6EUJZuhGS8hJ3c2fY6UW5MbkGUPMBEGd13djUzoY/BHqV/gTuFWtC6IuVA7A2+v3yjBS6c4or50xhzTQZImQ==", + "requires": { + "http-errors": "~1.8.1", + "safe-buffer": "5.2.1", + "uid-safe": "2.1.5" + } + }, "openai": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/openai/-/openai-3.2.1.tgz", @@ -275,16 +408,49 @@ "yield-stream": "^3.0.0" } }, + "random-bytes": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz", + "integrity": "sha512-iv7LhNVO047HzYR3InF6pUcUsPQiHTM1Qal51DcGSuZFBil1aBBWG5eHPNek7bvILMaYJ/8RU1e8w1AMdHmLQQ==" + }, + "safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" + }, + "setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" + }, "shim-streams": { "version": "0.0.2", "resolved": "https://registry.npmjs.org/shim-streams/-/shim-streams-0.0.2.tgz", "integrity": "sha512-9Otb+FCl13XxRp1nVddtsCbwvB7AEMTjzc3/fixowyzvSVoCzu/VEstblB2SdIDbd61u5D/zpS5u9fGzDdOoZg==" }, + "statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==" + }, "streamsearch": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==" }, + "toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==" + }, + "uid-safe": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz", + "integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==", + "requires": { + "random-bytes": "~1.0.0" + } + }, "web-streams-polyfill": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz", diff --git a/edge-stream/package.json b/edge-stream/package.json index 78a38b80bd..3c78ead160 100644 --- a/edge-stream/package.json +++ b/edge-stream/package.json @@ -7,6 +7,7 @@ "dependencies": { "busboy": "^1.6.0", "eventsource-parser": "^1.0.0", + "multiparty": "^4.2.3", "openai": "^3.2.1", "openai-streams": "^5.3.0" } diff --git a/system/netlify.toml b/system/netlify.toml index edad2f58b3..4515e069eb 100644 --- a/system/netlify.toml +++ b/system/netlify.toml @@ -29,6 +29,8 @@ external_node_modules = ["got", "redis"] [functions.sotce-blog] external_node_modules = ["got", "crypto", "patreon"] +[functions.vary] + external_node_modules = ["multiparty", "openai"] [dev] NODE_ENV = "development" publish = "public/" @@ -122,6 +124,10 @@ from = "/auth" to = "/.netlify/functions/auth" status = 200 +[[redirects]] + from = "/api/vary" + to = "/.netlify/functions/vary" + status = 200 [[redirects]] from = "/handle" to = "/.netlify/functions/handle" diff --git a/system/netlify/functions/media-collection.js b/system/netlify/functions/media-collection.js index 4e3b7d9376..021e37d67e 100644 --- a/system/netlify/functions/media-collection.js +++ b/system/netlify/functions/media-collection.js @@ -18,7 +18,7 @@ const s3User = new S3Client({ export async function handler(event, context) { // Make sure this is a GET request if (event.httpMethod !== "GET") - return respond(405, { error: "Wrong request type 😩" }); + return respond(405, { error: "Wrong request type!" }); const client = { s3: s3User, bucket: process.env.USER_SPACE_NAME }; const path = decodeURIComponent(event.queryStringParameters.for); diff --git a/system/netlify/functions/presigned-url.js b/system/netlify/functions/presigned-url.js index d728999cd9..be0a496dd9 100644 --- a/system/netlify/functions/presigned-url.js +++ b/system/netlify/functions/presigned-url.js @@ -62,7 +62,7 @@ const s3User = new S3Client({ export async function handler(event, context) { // Only allow GET requests. if (event.httpMethod !== "GET") - return respond(405, { error: "Wrong request type 😩" }); + return respond(405, { error: "Wrong request type!" }); if (event.path === "/presigned-download-url") { const queryParams = new URLSearchParams(event.queryStringParameters); diff --git a/system/netlify/functions/reload.js b/system/netlify/functions/reload.js index d2c7cb6688..476d3e532b 100644 --- a/system/netlify/functions/reload.js +++ b/system/netlify/functions/reload.js @@ -17,7 +17,7 @@ async function fun(event, context) { if (event.httpMethod !== "POST") { status = 405; - out = { status: "Wrong request type 😩" }; + out = { status: "Wrong request type!" }; } else { status = 200; diff --git a/system/netlify/functions/run.js b/system/netlify/functions/run.js index 3d6285a388..a6b2fe680f 100644 --- a/system/netlify/functions/run.js +++ b/system/netlify/functions/run.js @@ -18,7 +18,7 @@ async function fun(event) { if (event.httpMethod !== "POST") { status = 405; - out = { status: "Wrong request type 😩" }; + out = { status: "Wrong request type!" }; } else if (event.httpMethod === "POST" && event.path === "/run") { try { const body = JSON.parse(event.body); diff --git a/system/netlify/functions/user.js b/system/netlify/functions/user.js index 0d4e20c7e7..4dfd014168 100644 --- a/system/netlify/functions/user.js +++ b/system/netlify/functions/user.js @@ -11,10 +11,7 @@ import { respond } from "../../backend/http.mjs"; export async function handler(event, context) { // Make sure this is a GET request if (event.httpMethod !== "GET") { - return { - statusCode: 405, - body: JSON.stringify({ error: "Wrong request type 😩" }), - }; + return respond(405, { error: "Wrong request type." }); } const handleOrEmail = event.queryStringParameters.from; diff --git a/system/netlify/functions/vary.js b/system/netlify/functions/vary.js new file mode 100644 index 0000000000..e9587af009 --- /dev/null +++ b/system/netlify/functions/vary.js @@ -0,0 +1,73 @@ +import { Configuration, OpenAIApi } from "openai"; +import Busboy from "busboy"; +import { respond } from "../../backend/http.mjs"; + +export async function handler(event, context) { + if (event.httpMethod !== "POST") { + return respond(405, { error: "Wrong request type." }); + } + + const data = await parseFormData(event); + + data.image.data.name = "painting.png"; + console.log(data.image.data); + + try { + const configuration = new Configuration({ + apiKey: process.env.OPENAI_API_KEY, + }); + const openai = new OpenAIApi(configuration); + + const response = await openai.createImageVariation( + data.image.data, + 1, + "256x256" + ); + console.log(response); + return respond(200, { url: response.data.data[0].url }); + } catch (error) { + console.log(error); + return respond(500, { error: `Error varying painting: ${error}` }); + } +} + +function parseFormData(event) { + return new Promise((resolve, reject) => { + const busboy = Busboy({ + headers: { "content-type": event.headers["content-type"] }, + }); + let formData = {}; + + busboy.on("field", (fieldname, val) => { + formData[fieldname] = val; + }); + + busboy.on("file", (fieldname, file, filename, encoding, mimetype) => { + let buffers = []; + file.on("data", (data) => buffers.push(data)); + file.on("end", () => { + formData[fieldname] = { + data: Buffer.concat(buffers), + filename, + encoding, + mimetype, + }; + }); + }); + + busboy.on("finish", () => { + resolve(formData); + }); + + busboy.on("error", (error) => reject(error)); + + busboy.write( + event.body, + event.isBase64Encoded ? "base64" : "binary", + (err) => { + if (err) reject(err); + else busboy.end(); + } + ); + }); +} diff --git a/system/package-lock.json b/system/package-lock.json index 164716d17c..d353687fc6 100644 --- a/system/package-lock.json +++ b/system/package-lock.json @@ -14,6 +14,7 @@ "@geckos.io/client": "^2.2.3", "@mediapipe/hands": "^0.4.1675469240", "@mediapipe/tasks-vision": "^0.10.0", + "busboy": "^1.6.0", "chrome-aws-lambda": "^10.1.0", "crypto": "^1.0.1", "express-oauth2-jwt-bearer": "^1.3.0", @@ -24,6 +25,7 @@ "jsonwebtoken": "^9.0.0", "mongodb": "^5.3.0", "nanoid": "^4.0.0", + "openai": "^3.2.1", "patreon": "^0.4.1", "redis": "^4.5.1", "three": "^0.146.0", @@ -4796,6 +4798,14 @@ "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.11.0.tgz", "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==" }, + "node_modules/axios": { + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", + "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", + "dependencies": { + "follow-redirects": "^1.14.8" + } + }, "node_modules/babel-jest": { "version": "29.3.1", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.3.1.tgz", @@ -5261,6 +5271,17 @@ "node": ">=6.14.2" } }, + "node_modules/busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "dependencies": { + "streamsearch": "^1.1.0" + }, + "engines": { + "node": ">=10.16.0" + } + }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -6815,7 +6836,6 @@ "version": "1.15.2", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", - "dev": true, "funding": [ { "type": "individual", @@ -35994,6 +36014,28 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/openai": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/openai/-/openai-3.2.1.tgz", + "integrity": "sha512-762C9BNlJPbjjlWZi4WYK9iM2tAVAv0uUp1UmI34vb0CN5T2mjB/qM6RYBmNKMh/dN9fC+bxqPwWJZUTWW052A==", + "dependencies": { + "axios": "^0.26.0", + "form-data": "^4.0.0" + } + }, + "node_modules/openai/node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/opener": { "version": "1.5.2", "resolved": "https://registry.npmjs.org/opener/-/opener-1.5.2.tgz", @@ -37384,6 +37426,14 @@ "node": ">= 0.8" } }, + "node_modules/streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/strict-uri-encode": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz", diff --git a/system/package.json b/system/package.json index b84db908dc..2636900e02 100644 --- a/system/package.json +++ b/system/package.json @@ -41,6 +41,7 @@ "@geckos.io/client": "^2.2.3", "@mediapipe/hands": "^0.4.1675469240", "@mediapipe/tasks-vision": "^0.10.0", + "busboy": "^1.6.0", "chrome-aws-lambda": "^10.1.0", "crypto": "^1.0.1", "express-oauth2-jwt-bearer": "^1.3.0", @@ -51,6 +52,7 @@ "jsonwebtoken": "^9.0.0", "mongodb": "^5.3.0", "nanoid": "^4.0.0", + "openai": "^3.2.1", "patreon": "^0.4.1", "redis": "^4.5.1", "three": "^0.146.0", diff --git a/system/public/aesthetic.computer/disks/vary.mjs b/system/public/aesthetic.computer/disks/vary.mjs index f82e2b4ad5..11762182f7 100644 --- a/system/public/aesthetic.computer/disks/vary.mjs +++ b/system/public/aesthetic.computer/disks/vary.mjs @@ -7,39 +7,28 @@ // 🥾 Boot (Runs once before first paint and sim) async function boot({ system }) { + // TODO: Retrieve the painting as a PNG... + + // Convert the pixels data to a Blob object const blob = new Blob([system.painting.pixels], { type: "application/octet-stream", }); - // Convert the Blob to a base64 string - const imageBase64 = await blobToBase64(blob); - - // Create an object to hold the image data and other data - const data = { - image: imageBase64, - width: system.painting.width, - height: system.painting.height - }; + const file = new File([blob], "painting.png"); // Make a `File` out of the image, to be consumed by the server. // Create a FormData object to hold the image blob and other data - // const formData = new FormData(); - // formData.append("image", blob); - // formData.append("width", system.painting.width); - // formData.append("height", system.painting.height); - - const host = DEBUG - ? "http://localhost:3000" - : "https://ai.aesthetic.computer"; + const formData = new FormData(); + formData.append("image", file); + formData.append("width", system.painting.width); + formData.append("height", system.painting.height); // Make a POST request to the API - fetch(`${host}/api/vary`, { - method: "POST", - body: JSON.stringify(data), - }) - .then((response) => { + fetch(`/api/vary`, { method: "POST", body: formData }) + .then(async (response) => { if (!response.ok) { - throw new Error("Network response was not ok"); + console.log(await response.json()); + throw new Error("Network response was not ok."); } return response.json(); }) @@ -59,11 +48,3 @@ export const system = "nopaint"; export { boot, paint }; // 📚 Library (Useful functions used throughout the piece) -function blobToBase64(blob) { - return new Promise((resolve, reject) => { - const reader = new FileReader(); - reader.onloadend = () => resolve(reader.result); - reader.onerror = reject; - reader.readAsDataURL(blob); - }); -} \ No newline at end of file