Mitya
Mitya
CDCloudflare Developers
Created by Mitya on 2/21/2025 in #workers-help
Several questions about developing locally with queues
1. The Get started guide for queues suggests running wrangler tail to watch published messages arrive. However this gives an error 10007 when working locally. Should this command work locally? 2. How does one configure the batch size/frequency of a queue when working locally, since the queue is automatically-created by Wrangler? That is, you don't run the create command when working locally, nor do you bind the queue explicitly to your config TOML/JSON. 3. In my consumer handler, I have:
async queue(batch, env) {
for (const message of batch.messages)
console.log('RECEIVED!', message.body);
}
async queue(batch, env) {
for (const message of batch.messages)
console.log('RECEIVED!', message.body);
}
Note I'm not running message.ack(). I expected that this would result in the consumer repeatedly re-receiving the same message (because I never acknowledged it). But this doesn't happen; the console message shows up only once. Am I mistaken here? 4. Is there any ETA on queues being supported with the --remote flag? This would make life MUCH easier - right now developing with queues is really quite painful. Thank you in advance.
2 replies
CDCloudflare Developers
Created by Mitya on 2/13/2025 in #workers-help
Getting lots of CF errors while working locally with `--remote`
Way more errors than usual today that don't seem to result from anything I'm doing in code. Here's two of the errors I'm seeing frequently. I have to restart the Worker each time.
✘ [ERROR] Error on remote worker: ParseError: A request to the Cloudflare API (/accounts/************/workers/scripts/hlp-v2-back-dev/edge-preview) failed.
text: 'binding central of type d1 failed to generate. Please try again later or contact
support. [code: 10021]'
✘ [ERROR] Error on remote worker: ParseError: A request to the Cloudflare API (/accounts/************/workers/scripts/hlp-v2-back-dev/edge-preview) failed.
text: 'binding central of type d1 failed to generate. Please try again later or contact
support. [code: 10021]'
and
✘ [ERROR] Error in ProxyController: Error inside ProxyWorker

{
name: 'Error',
message: 'internal error',
stack: 'Error: internal error'
}
✘ [ERROR] Error in ProxyController: Error inside ProxyWorker

{
name: 'Error',
message: 'internal error',
stack: 'Error: internal error'
}
4 replies
CDCloudflare Developers
Created by Mitya on 1/30/2025 in #workers-help
Do workers invoked by service binding get a fresh 30 seconds?
This has crept up in various discussions but I don't believe there's been a definitive answer from CF staff on this and the docs seem (at least to me) unclear on the point. If worker A (triggered over HTTP) invokes worker B via a service binding and waitUntil(), must worker B fulfill its job within the original 30 seconds afforded to worker A, or does it get its own, fresh 30 seconds? In other words, which of these is correct, if worker A invokes worker B after 20 seconds? 1. Worker B has 10 seconds remaining to do what it needs (the whole operation is subject to worker A's 30 seconds) 2. Worker B has 30 seconds remaining to do what it needs (the whole operation takes 50 seconds)
3 replies
CDCloudflare Developers
Created by Mitya on 1/30/2025 in #workers-help
How do I specify producer and consumer workers in TOML under an environment?
TOML continues to hurt my head and I'm struggling to bind queues within an environment. I've tried the following (both suggested by Cursor but both apparently wrong):
[env.production]
queues = {
producers = [
{ queue = "my-queue", binding = "MY_QUEUE" }
]
}
[env.production]
queues = {
producers = [
{ queue = "my-queue", binding = "MY_QUEUE" }
]
}
and
[env.production]
queues = [
{ queue = 'user-msg-ai-analysis', binding = 'USER_MSG_AI_ANALYSIS', type = "producer" }
]
[env.production]
queues = [
{ queue = 'user-msg-ai-analysis', binding = 'USER_MSG_AI_ANALYSIS', type = "producer" }
]
What's the correct syntax here?
4 replies
CDCloudflare Developers
Created by Mitya on 1/30/2025 in #workers-help
Service bindings - do I need to use waitUntil() in this scenario?
Suppose I have a service binding from worker A to worker B. Worker A is triggered over HTTP, and hands off to worker B to do a task e.g. update a DB record or call a remote API. Worker A does NOT need to know the result of worker B's task - it simply kicks it off. Worker A is then free to terminate. In this scenario, do I need to use waitUntil() in worker A or not? Struggling to understand when waitUntil() is and is not required. Thank you in advance.
8 replies
CDCloudflare Developers
Created by Mitya on 1/29/2025 in #workers-help
Can I run two workers from one monorepo?
I'm sure the answer is yes; I'm just wondering what the set up here would look like. If my repo was:
| worker1/
| - <all worker 1 files here - src/, wrangler.toml, etc.>
| worker2/
| - <ditto for worker 2>
| worker1/
| - <all worker 1 files here - src/, wrangler.toml, etc.>
| worker2/
| - <ditto for worker 2>
...how would I configure the action to read from those two top-level directories and build two distinct workers? Or would I need some sort of top-level Wrangler file? Just unable to visualise this.
9 replies
CDCloudflare Developers
Created by Mitya on 1/23/2025 in #general-help
Zero trust says my cert is expiring, and that's it's in use. But by what?
I generally understand little about certificates. I've never thus far used Zero Trust but today I went to it and got a notification that my cert was expiring, and I should generate a new one. This I've done, but it says the old one is "in use". But it doesn't say by what. Do I need to do anything here, or update my CF domains/workers/pages apps or whatever? I realise this is vague but any help appreciated!
7 replies
CDCloudflare Developers
Created by Mitya on 1/9/2025 in #workers-help
Hyperdrive fails to connect in `scheduled()` (but works over HTTP)
No description
8 replies
CDCloudflare Developers
Created by Mitya on 1/6/2025 in #workers-help
"SyntaxError: Unexpected end of JSON input" in wrangler-dist/cli.js
This has happened a few times recently for me, after making some innocuous code changes. Restarting my PC seems to get rid of it, but it's annoying and I'm not sure what's causing it. Anyone any idea? Thanks in advance.
18 replies
CDCloudflare Developers
Created by Mitya on 1/6/2025 in #workers-help
With the `scheduled` handler, what's the difference between waitUntil and just using async/await?
Is it necessary to use waitUntil() when I could just use good old async/await to delay termination of the worker?
20 replies
CDCloudflare Developers
Created by Mitya on 1/4/2025 in #workers-help
Another worker vs. workflow for background work?
We need to vectorise some user input to our worker, and my idea was to have a separate worker, via a binding, that could do this task without holding up the main worker that receives user input and gives output. I can do this via waitUntil(). However I just discovered Workflows and this sounds like much the same thing, suited to the task. Which would be better in my case and what are the key considerations? Thank you.
2 replies
CDCloudflare Developers
Created by Mitya on 12/12/2024 in #workers-help
Can AI Functions work effectively with multiple inputs that it needs to iterate over?
I have a simple AI Function setup that's working. It extracts a couple of bits of info from the user message and returns them as arguments for my function. So far, so simple. What I'm struggling to do (and this may not be possible) is to get it to perform well with multiple inputs. Suppose my user message was this - a JSON-formatted array of user messages, and I wanted to extract the sentiment and categories for each one:
{
role: "user",
content: JSON.stringify([
"I like cars and parsnips",
"Potholes are a real problem on city roads",
"Christmas comes around too early"
])
}
{
role: "user",
content: JSON.stringify([
"I like cars and parsnips",
"Potholes are a real problem on city roads",
"Christmas comes around too early"
])
}
I'm struggling to sest up the tools > parameters > properties section such that it can report on each message in the input. I've tried something like:
parameters: {
type: 'object',
properties: {
msg1Sentiment: {type: 'string', description: 'The sentiment of message 1 - unhappy, neutral or happy'},
msg1Categories: {type: 'array', description: 'One or more categories relating to message 1'},
msg2Sentiment: {type: 'string', description: 'The sentiment of message 2 - unhappy, neutral or happy'},
msg2Categories: {type: 'array', description: 'One or more categories relating to message 2'},
...
}
}
parameters: {
type: 'object',
properties: {
msg1Sentiment: {type: 'string', description: 'The sentiment of message 1 - unhappy, neutral or happy'},
msg1Categories: {type: 'array', description: 'One or more categories relating to message 1'},
msg2Sentiment: {type: 'string', description: 'The sentiment of message 2 - unhappy, neutral or happy'},
msg2Categories: {type: 'array', description: 'One or more categories relating to message 2'},
...
}
}
Sometimes this sort of works and at other times it just gives nonsensical responses or breaks down. I've also tried asking it to give me an object of params per message, like so:
parameters: {
type: 'object',
properties: {
msg1Data: {type: 'object', description: 'Object about message 1, containing two properties: "sentiment" and "categories"'},
msg2Data: {type: 'object', description: 'Object about message 2, containing two properties: "sentiment" and "categories"'},
...
}
}
parameters: {
type: 'object',
properties: {
msg1Data: {type: 'object', description: 'Object about message 1, containing two properties: "sentiment" and "categories"'},
msg2Data: {type: 'object', description: 'Object about message 2, containing two properties: "sentiment" and "categories"'},
...
}
}
...but this rarely works out well. Is there a better way here?
1 replies
CDCloudflare Developers
Created by Mitya on 12/12/2024 in #general-help
Why isn't my cname showing up in DNS checkers?
No description
5 replies
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