@takisvc/code-canvas 0.1.2 → 0.1.3
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/code-canvas.js +54 -17
- package/package.json +1 -1
package/dist/code-canvas.js
CHANGED
|
@@ -63,9 +63,7 @@ function displayInlineImage(base64) {
|
|
|
63
63
|
const chunk = base64.slice(i, i + chunkSize);
|
|
64
64
|
const more = i + chunkSize < base64.length ? 1 : 0;
|
|
65
65
|
if (i === 0) {
|
|
66
|
-
process.stdout.write(
|
|
67
|
-
`\x1B_Gf=100,a=T,m=${more};${chunk}\x1B\\`
|
|
68
|
-
);
|
|
66
|
+
process.stdout.write(`\x1B_Gf=100,a=T,m=${more};${chunk}\x1B\\`);
|
|
69
67
|
} else {
|
|
70
68
|
process.stdout.write(`\x1B_Gm=${more};${chunk}\x1B\\`);
|
|
71
69
|
}
|
|
@@ -87,7 +85,7 @@ function openFile(filepath) {
|
|
|
87
85
|
} catch {
|
|
88
86
|
}
|
|
89
87
|
}
|
|
90
|
-
function saveImage(base64,
|
|
88
|
+
function saveImage(base64, prompt) {
|
|
91
89
|
const filename = `generated-${Date.now()}.png`;
|
|
92
90
|
const buffer = Buffer.from(base64, "base64");
|
|
93
91
|
writeFileSync(filename, buffer);
|
|
@@ -95,7 +93,7 @@ function saveImage(base64, prompt2) {
|
|
|
95
93
|
const tweetText = encodeURIComponent(
|
|
96
94
|
`My AI agent just roasted my coding session \u{1F525}
|
|
97
95
|
|
|
98
|
-
Prompt: "${
|
|
96
|
+
Prompt: "${prompt}"
|
|
99
97
|
|
|
100
98
|
Powered by @worldcoin AgentKit`
|
|
101
99
|
);
|
|
@@ -125,7 +123,7 @@ function showUsage(usage) {
|
|
|
125
123
|
function extractAgentkitChallenge(body) {
|
|
126
124
|
return body?.extensions?.agentkit ?? body?.accepts?.[0]?.extensions?.agentkit ?? body?.paymentRequirements?.[0]?.extensions?.agentkit ?? body?.challenge;
|
|
127
125
|
}
|
|
128
|
-
async function generateImage(endpoint2,
|
|
126
|
+
async function generateImage(endpoint2, prompt, account2) {
|
|
129
127
|
const spinner = ora(randomSpinnerMessage()).start();
|
|
130
128
|
const interval = setInterval(() => {
|
|
131
129
|
spinner.text = randomSpinnerMessage();
|
|
@@ -134,12 +132,12 @@ async function generateImage(endpoint2, prompt2, account2) {
|
|
|
134
132
|
const firstResponse = await fetch(endpoint2, {
|
|
135
133
|
method: "POST",
|
|
136
134
|
headers: { "Content-Type": "application/json" },
|
|
137
|
-
body: JSON.stringify({ prompt
|
|
135
|
+
body: JSON.stringify({ prompt })
|
|
138
136
|
});
|
|
139
137
|
if (firstResponse.status === 200) {
|
|
140
138
|
const body = await firstResponse.json();
|
|
141
139
|
spinner.succeed(chalk.green("Image generated!"));
|
|
142
|
-
saveImage(body.image,
|
|
140
|
+
saveImage(body.image, prompt);
|
|
143
141
|
showUsage(body.usage);
|
|
144
142
|
return true;
|
|
145
143
|
}
|
|
@@ -195,14 +193,14 @@ async function generateImage(endpoint2, prompt2, account2) {
|
|
|
195
193
|
"Content-Type": "application/json",
|
|
196
194
|
agentkit: encoded
|
|
197
195
|
},
|
|
198
|
-
body: JSON.stringify({ prompt
|
|
196
|
+
body: JSON.stringify({ prompt })
|
|
199
197
|
});
|
|
200
198
|
if (secondResponse.status === 200) {
|
|
201
199
|
const body = await secondResponse.json();
|
|
202
200
|
spinner.succeed(
|
|
203
201
|
chalk.green("Image generated (free \u2014 World ID verified!)")
|
|
204
202
|
);
|
|
205
|
-
saveImage(body.image,
|
|
203
|
+
saveImage(body.image, prompt);
|
|
206
204
|
showUsage(body.usage);
|
|
207
205
|
return true;
|
|
208
206
|
}
|
|
@@ -216,13 +214,19 @@ async function generateImage(endpoint2, prompt2, account2) {
|
|
|
216
214
|
console.log();
|
|
217
215
|
console.log(chalk.yellow("Your agent is not registered with World ID."));
|
|
218
216
|
console.log();
|
|
219
|
-
console.log(
|
|
217
|
+
console.log(
|
|
218
|
+
` ${chalk.bold("Register:")} Run this in your terminal and scan the QR code with World App:`
|
|
219
|
+
);
|
|
220
220
|
console.log();
|
|
221
|
-
console.log(
|
|
221
|
+
console.log(
|
|
222
|
+
` npx @worldcoin/agentkit-cli register ${account2.address} --network world`
|
|
223
|
+
);
|
|
222
224
|
console.log();
|
|
223
225
|
console.log(` Then re-run the image generation command.`);
|
|
224
226
|
console.log();
|
|
225
|
-
console.log(
|
|
227
|
+
console.log(
|
|
228
|
+
` ${chalk.bold("Or pay:")} Fund wallet ${account2.address} with ${chalk.cyan("$1.20 USDC")} on World Chain.`
|
|
229
|
+
);
|
|
226
230
|
if (errorBody?.error) {
|
|
227
231
|
console.log();
|
|
228
232
|
console.log(chalk.dim(`Server: ${errorBody.error}`));
|
|
@@ -232,10 +236,39 @@ async function generateImage(endpoint2, prompt2, account2) {
|
|
|
232
236
|
clearInterval(interval);
|
|
233
237
|
}
|
|
234
238
|
}
|
|
239
|
+
function runSetup(account2) {
|
|
240
|
+
console.log(chalk.bold("Code Canvas Setup"));
|
|
241
|
+
console.log();
|
|
242
|
+
console.log(` Wallet: ${chalk.cyan(account2.address)}`);
|
|
243
|
+
console.log();
|
|
244
|
+
console.log(
|
|
245
|
+
"Registering with World ID \u2014 scan the QR code with your World App:"
|
|
246
|
+
);
|
|
247
|
+
console.log();
|
|
248
|
+
try {
|
|
249
|
+
execSync(
|
|
250
|
+
`npx @worldcoin/agentkit-cli register ${account2.address} --network world`,
|
|
251
|
+
{ stdio: "inherit" }
|
|
252
|
+
);
|
|
253
|
+
console.log();
|
|
254
|
+
console.log(chalk.green("Setup complete! You can now generate images:"));
|
|
255
|
+
return true;
|
|
256
|
+
} catch {
|
|
257
|
+
console.log();
|
|
258
|
+
console.log(chalk.red("Registration failed or was cancelled."));
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
235
262
|
var SERVER_URL = process.env.SERVER_URL ?? "https://takis.dev";
|
|
236
|
-
var
|
|
237
|
-
if (!
|
|
238
|
-
console.log(`Usage
|
|
263
|
+
var command = process.argv[2];
|
|
264
|
+
if (!command) {
|
|
265
|
+
console.log(`Usage:`);
|
|
266
|
+
console.log(
|
|
267
|
+
` ${chalk.bold("code-canvas setup")} Register with World ID`
|
|
268
|
+
);
|
|
269
|
+
console.log(
|
|
270
|
+
` ${chalk.bold('code-canvas "<prompt>"')} Generate an image`
|
|
271
|
+
);
|
|
239
272
|
console.log();
|
|
240
273
|
console.log("Examples:");
|
|
241
274
|
console.log(' code-canvas "a cat riding a skateboard"');
|
|
@@ -243,6 +276,10 @@ if (!prompt) {
|
|
|
243
276
|
process.exit(1);
|
|
244
277
|
}
|
|
245
278
|
var account = ensureWallet();
|
|
279
|
+
if (command === "setup") {
|
|
280
|
+
const ok = runSetup(account);
|
|
281
|
+
process.exit(ok ? 0 : 1);
|
|
282
|
+
}
|
|
246
283
|
var endpoint = `${SERVER_URL}/code-canvas/generate-image`;
|
|
247
|
-
var result = await generateImage(endpoint,
|
|
284
|
+
var result = await generateImage(endpoint, command, account);
|
|
248
285
|
process.exit(result === true ? 0 : result === "needs-registration" ? 0 : 1);
|