Mitya
Mitya
CDCloudflare Developers
Created by Mitya on 7/25/2024 in #workers-help
Bug in Wrangler - it randomly adds [object Object] to console.log()'d string output
I think I've raised this before but didn't get anywhere. There's a really weird bug in Wrangler (presumably) whereby, when you console.log() something, if it's over a certain lenght (unclear), Wrangler malforms it and swaps parts of it out for [object Object]. So you'll get something like:
one two three four five [object Object] eleven twelve thirteen
one two three four five [object Object] eleven twelve thirteen
Is a fix planned for this? It makes debugging quite difficult. The only way I've found to get round it is to do console.log(encodeURIComponent()), which seems to avoid the problem.
4 replies
CDCloudflare Developers
Created by Mitya on 7/21/2024 in #workers-help
With function calls, are system messages or description properties preferable to instruct AI?
I realise that title is not super clear. Basically I've found that both of the following function calls produce the same result. One users a type=system message to instruct AI, while the other omits this and instead provides description properties. If I do neither approach, I get erroneous/extra results from just the two bits asked for (fruit + vegetable). Which is preferable, according to how this functionality is designed? Version 1 - with system message
const response = await env.AI.run('@hf/nousresearch/hermes-2-pro-mistral-7b', {
messages: [{
role: "system",
content: "Extract the user's favourite fruit and vegetable"
}, {
role: "user",
content: 'I like pineapple, cars, carrots and Belgium.',
}],
tools: [{
name: "say",
parameters: {
type: "object",
properties: {
fruit: {type: "string"},
vegetable: {type: "string"},
},
required: ["fruit", "vegetable"],
},
}],
});
const response = await env.AI.run('@hf/nousresearch/hermes-2-pro-mistral-7b', {
messages: [{
role: "system",
content: "Extract the user's favourite fruit and vegetable"
}, {
role: "user",
content: 'I like pineapple, cars, carrots and Belgium.',
}],
tools: [{
name: "say",
parameters: {
type: "object",
properties: {
fruit: {type: "string"},
vegetable: {type: "string"},
},
required: ["fruit", "vegetable"],
},
}],
});
Version 2: with description properties
const response = await env.AI.run('@hf/nousresearch/hermes-2-pro-mistral-7b', {
messages: [{
role: "user",
content: 'I like pineapple, cars, carrots and Belgium.',
}],
tools: [{
name: "say",
description: "Extract the user's favourite fruit and vegetable",
parameters: {
type: "object",
properties: {
fruit: {type: "string", description: "The user's favourite fruit"},
vegetable: {type: "string", description: "The user's favourite vegetable"},
},
required: ["fruit", "vegetable"],
},
}],
});
const response = await env.AI.run('@hf/nousresearch/hermes-2-pro-mistral-7b', {
messages: [{
role: "user",
content: 'I like pineapple, cars, carrots and Belgium.',
}],
tools: [{
name: "say",
description: "Extract the user's favourite fruit and vegetable",
parameters: {
type: "object",
properties: {
fruit: {type: "string", description: "The user's favourite fruit"},
vegetable: {type: "string", description: "The user's favourite vegetable"},
},
required: ["fruit", "vegetable"],
},
}],
});
Thank you in advance.
4 replies
CDCloudflare Developers
Created by Mitya on 7/20/2024 in #workers-help
AI embedded functions calls - how can my function influence the AI response?
I'm playing around with CF AI Functions and came up with this. The user message passed to AI hints at a fruit, AI infers the fruit as an orange, and sends "orange" to my function. So far, so good.
import { runWithTools } from '@cloudflare/ai-utils';
router.get('/ai', async (req, env) => {
const fruit = ({fruit}) => {
console.log(fruit);
switch(fruit) {
case 'orange': return '*******';
}
};
return await runWithTools(
env.AI,
'@hf/nousresearch/hermes-2-pro-mistral-7b',
{
messages: [
{
role: 'user',
content: 'Discern the following fruit: shares its name with a colour, is juicy, and is citrus.',
},
],
tools: [
{
name: 'fruit',
description: `Output the user's favourite fruit`,
parameters: {
type: 'object',
properties: {
fruit: { type: 'fruit', description: `the user's favourite fruit` },
},
required: ['fruit'],
},
function: fruit,
},
],
}
);
});
import { runWithTools } from '@cloudflare/ai-utils';
router.get('/ai', async (req, env) => {
const fruit = ({fruit}) => {
console.log(fruit);
switch(fruit) {
case 'orange': return '*******';
}
};
return await runWithTools(
env.AI,
'@hf/nousresearch/hermes-2-pro-mistral-7b',
{
messages: [
{
role: 'user',
content: 'Discern the following fruit: shares its name with a colour, is juicy, and is citrus.',
},
],
tools: [
{
name: 'fruit',
description: `Output the user's favourite fruit`,
parameters: {
type: 'object',
properties: {
fruit: { type: 'fruit', description: `the user's favourite fruit` },
},
required: ['fruit'],
},
function: fruit,
},
],
}
);
});
What I don't know is how to get my function to influence the response. As you can see, my function returns "**", which I thought might implicitly form the AI's response. It doesn't; AI responds in its own wording. Is what I'm trying to do possible?
1 replies
CDCloudflare Developers
Created by Mitya on 6/21/2024 in #workers-help
Can I write a data URI to an R2 bucket?
Is it possible to write to an R2 bucket with a base64 data URI i.e.
await env.r2_pub.put('some/file', 'data:image/png;base64,i5Gkw...');
await env.r2_pub.put('some/file', 'data:image/png;base64,i5Gkw...');
? That fails (it creates a text file with literally that content), but perhaps there's a way to encode the file contents first such that R2 accepts it?
7 replies
CDCloudflare Developers
Created by Mitya on 6/21/2024 in #workers-help
How to validate a file upload in Workers before saving it to R2?
I've spent ages trying to resolve this. I know how to upload a file and save it to R2. What I can't figure out is best practice to validate the file first. Lots of resources out there say use something like Formiddable, but I can't believe I need a third-party library just to enforce things like "must be a PDF" or "must be under 100kb". I know the Workers R2 API allows the stipulation of conditions on PUT operations, but these conditions don't seem to relate to the sort of conditions I mention above. So what's best practice here?
11 replies
CDCloudflare Developers
Created by Mitya on 6/17/2024 in #workers-help
All of a sudden Wrangler keeps opening a browser tab and asking for account access?
No description
20 replies
CDCloudflare Developers
Created by Mitya on 5/24/2024 in #workers-help
Deployment via GH Action sometimes gives error about NPX failing with "exit code 1"
No description
3 replies
CDCloudflare Developers
Created by Mitya on 5/8/2024 in #workers-help
Looking for a really simple explanation of durable objects vs. workers
One problem I repeatedly find with CF docs is that they're not very friendly and seem to assume a lot of knowledge already. I would love to see each product overview have an intro video or something that really explains what the thing is. The docs seem to jump straight into highly technical concepts. With this in mind, what is the relationship between durable objects and workers? Right now I have a worker which powers a chatbot. Users talk to our system via a web interface and our system (i.e. the worker) responds. I'm keen to learn more of the CF ecosystem and would like to see which parts of it might fit into our stack and improve it. Are durable objects their own thing, or are they used in conjunction with workers? I know this is a very basic question, but I appreciate any help!
31 replies
CDCloudflare Developers
Created by Mitya on 4/19/2024 in #workers-help
Getting `TypeError: fetch failed` errors
Another error I'm getting today. Again, I'm developing locally but with --remote. It happens randomly. Any thoughts?
✘ [ERROR] Error on remote worker: TypeError: fetch failed

at fetch
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:16838:17)
at async performApiFetch
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:119342:10)
at async fetchInternal
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:119352:20)
at async fetchResult
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:119504:16)
at async createPreviewToken
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:157913:29)
at async createWorkerPreview
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:157934:17)
at async start
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:158503:34)
{
cause: Error: write ECONNRESET
at WriteWrap.onWriteComplete [as oncomplete] (node:internal/stream_base_commons:94:16) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'write'
}
}
✘ [ERROR] Error on remote worker: TypeError: fetch failed

at fetch
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:16838:17)
at async performApiFetch
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:119342:10)
at async fetchInternal
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:119352:20)
at async fetchResult
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:119504:16)
at async createPreviewToken
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:157913:29)
at async createWorkerPreview
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:157934:17)
at async start
(C:\Users\termi\Sync\dev\***\node_modules\wrangler\wrangler-dist\cli.js:158503:34)
{
cause: Error: write ECONNRESET
at WriteWrap.onWriteComplete [as oncomplete] (node:internal/stream_base_commons:94:16) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'write'
}
}
2 replies
CDCloudflare Developers
Created by Mitya on 4/19/2024 in #workers-help
Workers randomly throwing 502 "bad gateway" responses while developing locally with `--remote`
There are so many seemingly random errors thrown by workers while developing locally that the experience can be a drag (I've raised most of these in previous threads here.) Today, I've started getting 502s every now and then. When this happens, I have no choice but to restart the Worker and then the exact same request succeeds. In case it's relevant, I'm developing with the --remote flag for reasons that are outside this topic.
Error on remote worker: ParseError: Received a malformed response from the API

at fetchInternal
(C:\Users\termi\Sync\dev\projects\***\node_modules\wrangler\wrangler-dist\cli.js:119376:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async fetchResult
(C:\Users\termi\Sync\dev\projects\***\node_modules\wrangler\wrangler-dist\cli.js:119504:16)
at async createPreviewToken
(C:\Users\termi\Sync\dev\projects\***\node_modules\wrangler\wrangler-dist\cli.js:157913:29)
at async createWorkerPreview
(C:\Users\termi\Sync\dev\projects\***\node_modules\wrangler\wrangler-dist\cli.js:157934:17)
at async start
(C:\Users\termi\Sync\dev\projects\***\node_modules\wrangler\wrangler-dist\cli.js:158503:34)
{
text: 'Received a malformed response from the API',
notes: [
{
text: '<html>\r\n' +
'<head><title>502 Bad Gateway</title></head>\r\n' +
'<body>\r\n' +
'<center><h1>502 Bad Gateway</h1></cente... (length = 155)'
},
{
text: 'POST
/accounts/8701fe61c0686bad57c64dd62d3dc16b/workers/scripts/hlp-v2-back-dev/edge-preview -> 502 Bad
Gateway'
}
],
location: undefined,
kind: 'error'
}
Error on remote worker: ParseError: Received a malformed response from the API

at fetchInternal
(C:\Users\termi\Sync\dev\projects\***\node_modules\wrangler\wrangler-dist\cli.js:119376:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async fetchResult
(C:\Users\termi\Sync\dev\projects\***\node_modules\wrangler\wrangler-dist\cli.js:119504:16)
at async createPreviewToken
(C:\Users\termi\Sync\dev\projects\***\node_modules\wrangler\wrangler-dist\cli.js:157913:29)
at async createWorkerPreview
(C:\Users\termi\Sync\dev\projects\***\node_modules\wrangler\wrangler-dist\cli.js:157934:17)
at async start
(C:\Users\termi\Sync\dev\projects\***\node_modules\wrangler\wrangler-dist\cli.js:158503:34)
{
text: 'Received a malformed response from the API',
notes: [
{
text: '<html>\r\n' +
'<head><title>502 Bad Gateway</title></head>\r\n' +
'<body>\r\n' +
'<center><h1>502 Bad Gateway</h1></cente... (length = 155)'
},
{
text: 'POST
/accounts/8701fe61c0686bad57c64dd62d3dc16b/workers/scripts/hlp-v2-back-dev/edge-preview -> 502 Bad
Gateway'
}
],
location: undefined,
kind: 'error'
}
Nothing in my app throws this error explicitly or outputs HTML, so I assume this response is coming directly from CF. Any thoughts here? Thank you.
3 replies
CDCloudflare Developers
Created by Mitya on 4/17/2024 in #workers-help
Strange issue with `console.log()` omitting members of objects
I'm using Itty Router with CF Workers. For some reason, when working locally and I run console.log(req) (in middleware, obviously) the vast majority of the members of that object are not shown. For example, if I run:
console.log(req)
console.log(req)
...all I get logged is:
Request {
query: Object,
params: Object,
route: *,
content: Object
}
Request {
query: Object,
params: Object,
route: *,
content: Object
}
However, if I run:
console.log(req.keepalive)
console.log(req.keepalive)
...I get:
false
false
That's just one example of missing data. There's also a Headers object that I know is there because I can call req.headers.get('my-header') and it works. Any thoughts as to what's going on here?
6 replies
CDCloudflare Developers
Created by Mitya on 3/25/2024 in #workers-help
GH secrets aren't coming through to my worker
Well, some are. But I added some new ones 4 hours ago and when I go to CF > Workers > my worker > settings > variables, the ones I added are not listed. I'm definitely looking at the right branch/corresponding worker environment. In GH > settings > secrets > actions, I can see all my secrets, including those I added today. If I go inspect .github/workflows/wrangler-action.yml I have added my secrets to those that were already there, i.e.
env:
SECRET1: ${{ secrets.SECRET1 }}
SECRET2: ${{ secrets.SECRET2 }}
env:
SECRET1: ${{ secrets.SECRET1 }}
SECRET2: ${{ secrets.SECRET2 }}
As I say, previous secrets have come through. Do I need to do something e.g. forcibly restart my worker somehow?
3 replies
CDCloudflare Developers
Created by Mitya on 3/5/2024 in #workers-help
How do you enable support for async local storage?
If I try to import node:async_hooks I get an error:
Are you trying to bundle for Node? [Obviously] You can use "platform: 'node'
Are you trying to bundle for Node? [Obviously] You can use "platform: 'node'
- My TOML has node_compat = true - The ALS docs don't say anything about prerequisites to support ALS - platform is not a documented config property that I can see - This page says I can enable ALS by adding compatibility_flags = [ "nodejs_als" ] in the TOML but that raises the error _"No such compatibility flag: nodejsals"
35 replies
CDCloudflare Developers
Created by Mitya on 1/24/2024 in #workers-help
Confusing advice about secrets and Wrangler
From what I can tell, it does not make sense to put sensitive data in the TOML file because this would be committed. CF acknowledges this by suggesting we use .dev.vars for local, and secrets for production. However, when adding secrets (via the dashboard), CF then says it "recommend[s] updating your wrangler.toml file to keep your local development environment in sync." So this advice seems to be contradictory. A related issue is that, as far as I know, local DB connection strings for Hyperdrives must be specified in TOML, not .dev.vars (the latter didn't work for me). So this necessitates having two TOML files - one production, one local - the local one being denoted via the --config param, because putting the connection string in the main TOML would mean committing DB credentials. Am I right in all the above, or can anyone clarify anything I'm missing?
8 replies
CDCloudflare Developers
Created by Mitya on 1/22/2024 in #workers-help
"CODE_MOVED for unknown code block" bug?
No description
16 replies
CDCloudflare Developers
Created by Mitya on 1/19/2024 in #pages-help
Pointing subdomain (of non-CF domain) to pages site gives SSL error
No description
11 replies
CDCloudflare Developers
Created by Mitya on 1/17/2024 in #workers-help
Does anyone have a working example of evaluating JavaScript via WASM?
(This is a spin-off from this thread, which ultimately asks a different question and has become quite lengthy.) I need to evaluate some JavaScript, but understand that eval() / new Function() is blocked on workers. I understand my only route to evalulation is via WASM. But I'm struggling to implement a working example of JS evaluation via WASM in Workers. I'm completely new to WASM and am focusing on packages that do the heavy lifting for you. Here's what I've tried, using quickjs-emscripten:
import { getQuickJS, shouldInterruptAfterDeadline } from "quickjs-emscripten"
getQuickJS().then((QuickJS) => {
console.log('start'); //<-- never happens
const result = QuickJS.evalCode("1 + 1", {
shouldInterrupt: shouldInterruptAfterDeadline(Date.now() + 1000),
memoryLimitBytes: 1024 * 1024,
})
console.log(result)
})
import { getQuickJS, shouldInterruptAfterDeadline } from "quickjs-emscripten"
getQuickJS().then((QuickJS) => {
console.log('start'); //<-- never happens
const result = QuickJS.evalCode("1 + 1", {
shouldInterrupt: shouldInterruptAfterDeadline(Date.now() + 1000),
memoryLimitBytes: 1024 * 1024,
})
console.log(result)
})
This throws no error but simply never gets into the callback. I've also tried wasm-jseval, via:
const { duktapeEval, quickjsEval } = require('wasm-jseval')
duktapeEval.getInstance().then(mod => {
console.log(mod.eval('1+1')) // 2
const add = mod.newFunction(['a', 'b'], 'return a+b')
console.log(add(1, 2)) // 3
})
const { duktapeEval, quickjsEval } = require('wasm-jseval')
duktapeEval.getInstance().then(mod => {
console.log(mod.eval('1+1')) // 2
const add = mod.newFunction(['a', 'b'], 'return a+b')
console.log(add(1, 2)) // 3
})
But this errors - it seems the package seems to think I'm working in the browser, as the error pertains to it trying to access location.href. So does anyone have a working cut-and-paste version of evaluating JS via a package like this I could use? I'd be most grateful!
3 replies
CDCloudflare Developers
Created by Mitya on 1/16/2024 in #workers-help
How can I enable support for `eval()` in Workers?
Yes yes I know all the caveats about eval() but it's essential for my use-case. When running in dev mode (but with --remote i.e. running on CF architecture) I get:
EvalError: Code generation from strings disallowed for this context
EvalError: Code generation from strings disallowed for this context
I've read this but didn't fully understand what to action from it. Could someone please advise? Thank you!
50 replies
CDCloudflare Developers
Created by Mitya on 1/12/2024 in #workers-help
Occasional "error on remote worker" errors
No description
1 replies
CDCloudflare Developers
Created by Mitya on 1/8/2024 in #workers-help
DB sometimes returns rows, sometimes returns nothing
I wanted to raise this formally although I've been discussing it in #hyperdrive-beta , as I'm not sure where in the CF stack the issue lies. Basically a single query in my app, among hundreds, sometimes returns rows and sometimes returns this:
{
command: null,
rowCount: null,
oid: null,
rows: Array(0),
fields: Array(0)
...
}
{
command: null,
rowCount: null,
oid: null,
rows: Array(0),
fields: Array(0)
...
}
All other queries work fine, as I say. So I'm sat here refreshing and endpoint and sometimes seeing data and sometimes not (with no code changes). This is happening while running my app locally (with --remote) so HD is being used. So I assume this is a HD issue, though perhaps it's at a Workers/Wrangler level for all I know. I could disable HD caching, but then that presumably defeats the point of HD? Any help is greatly appreciated as this is blocking me at the mo! Thanks in advance.
5 replies