Skip to content

Commit f40ebff

Browse files
committed
update references to @cloudflare/workers-types to use wrangler types instead
1 parent 762fd33 commit f40ebff

File tree

19 files changed

+268
-268
lines changed

19 files changed

+268
-268
lines changed

src/content/changelog/browser-rendering/2025-04-04-playwright-beta.mdx

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,40 @@ We're excited to share that you can now use Playwright's browser automation [cap
1313
Below is an example of how to use Playwright with Browser Rendering to test a TODO application using assertions:
1414

1515
```ts title="Assertion example"
16-
import { launch, type BrowserWorker } from '@cloudflare/playwright';
17-
import { expect } from '@cloudflare/playwright/test';
16+
import { launch, type BrowserWorker } from "@cloudflare/playwright";
17+
import { expect } from "@cloudflare/playwright/test";
1818

1919
interface Env {
20-
MYBROWSER: BrowserWorker;
20+
MYBROWSER: BrowserWorker;
2121
}
2222

2323
export default {
24-
async fetch(request: Request, env: Env) {
25-
26-
const browser = await launch(env.MYBROWSER);
27-
const page = await browser.newPage();
28-
29-
await page.goto('https://demo.playwright.dev/todomvc');
30-
31-
const TODO_ITEMS = [
32-
'buy some cheese',
33-
'feed the cat',
34-
'book a doctors appointment'
35-
];
36-
37-
const newTodo = page.getByPlaceholder('What needs to be done?');
38-
for (const item of TODO_ITEMS) {
39-
await newTodo.fill(item);
40-
await newTodo.press('Enter');
41-
}
42-
43-
await expect(page.getByTestId('todo-title')).toHaveCount(TODO_ITEMS.length);
44-
45-
await Promise.all(TODO_ITEMS.map(
46-
(value, index) => expect(page.getByTestId('todo-title').nth(index)).toHaveText(value)
47-
));
48-
},
24+
async fetch(request: Request, env: Env) {
25+
const browser = await launch(env.MYBROWSER);
26+
const page = await browser.newPage();
27+
28+
await page.goto("https://demo.playwright.dev/todomvc");
29+
30+
const TODO_ITEMS = [
31+
"buy some cheese",
32+
"feed the cat",
33+
"book a doctors appointment",
34+
];
35+
36+
const newTodo = page.getByPlaceholder("What needs to be done?");
37+
for (const item of TODO_ITEMS) {
38+
await newTodo.fill(item);
39+
await newTodo.press("Enter");
40+
}
41+
42+
await expect(page.getByTestId("todo-title")).toHaveCount(TODO_ITEMS.length);
43+
44+
await Promise.all(
45+
TODO_ITEMS.map((value, index) =>
46+
expect(page.getByTestId("todo-title").nth(index)).toHaveText(value),
47+
),
48+
);
49+
},
4950
};
5051
```
5152

src/content/docs/agents/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ And then define your first Agent by creating a class that extends the `Agent` cl
5050
<TypeScriptExample>
5151

5252
```ts
53-
import { Agent, AgentNamespace } from 'agents';
53+
import { Agent, AgentNamespace } from "agents";
5454

5555
export class MyAgent extends Agent {
5656
// Define methods on the Agent:

src/content/docs/ai-gateway/integrations/worker-binding-methods.mdx

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import { Render, PackageManagers } from "~/components";
88

99
This guide provides an overview of how to use the latest Cloudflare Workers AI Gateway binding methods. You will learn how to set up an AI Gateway binding, access new methods, and integrate them into your Workers.
1010

11-
## Prerequisites
12-
13-
- Install and use the `@cloudflare/workers-types` library, version `4.20250124.3` or above.
14-
1511
## 1. Add an AI Binding to your Worker
1612

1713
To connect your Worker to Workers AI, add the following to your [Wrangler configuration file](/workers/wrangler/configuration/):
@@ -29,18 +25,24 @@ binding = "AI"
2925

3026
This configuration sets up the AI binding accessible in your Worker code as `env.AI`.
3127

28+
<Render file="wrangler-typegen" product="workers" />
29+
3230
## 2. Basic Usage with Workers AI + Gateway
3331

3432
To perform an inference task using Workers AI and an AI Gateway, you can use the following code:
3533

3634
```typescript title="src/index.ts"
37-
const resp = await env.AI.run("@cf/meta/llama-3.1-8b-instruct", {
38-
prompt: "tell me a joke"
39-
}, {
40-
gateway: {
41-
id: "my-gateway"
42-
}
43-
});
35+
const resp = await env.AI.run(
36+
"@cf/meta/llama-3.1-8b-instruct",
37+
{
38+
prompt: "tell me a joke",
39+
},
40+
{
41+
gateway: {
42+
id: "my-gateway",
43+
},
44+
},
45+
);
4446
```
4547

4648
Additionally, you can access the latest request log ID with:
@@ -64,12 +66,12 @@ Once you have the gateway instance, you can use the following methods:
6466
The `patchLog` method allows you to send feedback, score, and metadata for a specific log ID. All object properties are optional, so you can include any combination of the parameters:
6567

6668
```typescript
67-
gateway.patchLog('my-log-id', {
68-
feedback: 1,
69-
score: 100,
70-
metadata: {
71-
user: "123"
72-
}
69+
gateway.patchLog("my-log-id", {
70+
feedback: 1,
71+
score: 100,
72+
metadata: {
73+
user: "123",
74+
},
7375
});
7476
```
7577

@@ -78,7 +80,7 @@ gateway.patchLog('my-log-id', {
7880

7981
### 3.2. `getLog`: Read Log Details
8082

81-
The `getLog` method retrieves details of a specific log ID. It returns an object of type `Promise<AiGatewayLog>`. You can import the `AiGatewayLog` type from the `@cloudflare/workers-types` library.
83+
The `getLog` method retrieves details of a specific log ID. It returns an object of type `Promise<AiGatewayLog>`. If this type is missing, ensure you have run [`wrangler types`](/workers/languages/typescript/#generate-types).
8284

8385
```typescript
8486
const log = await gateway.getLog("my-log-id");
@@ -97,7 +99,7 @@ const baseUrl = await gateway.getUrl();
9799
// Output: https://gateway.ai.cloudflare.com/v1/my-account-id/my-gateway/
98100

99101
// Get a provider-specific URL
100-
const openaiUrl = await gateway.getUrl('openai');
102+
const openaiUrl = await gateway.getUrl("openai");
101103
// Output: https://gateway.ai.cloudflare.com/v1/my-account-id/my-gateway/openai
102104
```
103105

@@ -110,30 +112,33 @@ const openaiUrl = await gateway.getUrl('openai');
110112
The `getUrl` method is particularly useful for integrating with popular AI SDKs:
111113

112114
**OpenAI SDK:**
115+
113116
```typescript
114117
import OpenAI from "openai";
115118

116119
const openai = new OpenAI({
117-
apiKey: "my api key", // defaults to process.env["OPENAI_API_KEY"]
118-
baseURL: await env.AI.gateway('my-gateway').getUrl('openai'),
120+
apiKey: "my api key", // defaults to process.env["OPENAI_API_KEY"]
121+
baseURL: await env.AI.gateway("my-gateway").getUrl("openai"),
119122
});
120123
```
121124

122125
**Vercel AI SDK with OpenAI:**
126+
123127
```typescript
124128
import { createOpenAI } from "@ai-sdk/openai";
125129

126130
const openai = createOpenAI({
127-
baseURL: await env.AI.gateway('my-gateway').getUrl('openai'),
131+
baseURL: await env.AI.gateway("my-gateway").getUrl("openai"),
128132
});
129133
```
130134

131135
**Vercel AI SDK with Anthropic:**
136+
132137
```typescript
133138
import { createAnthropic } from "@ai-sdk/anthropic";
134139

135140
const anthropic = createAnthropic({
136-
baseURL: await env.AI.gateway('my-gateway').getUrl('anthropic'),
141+
baseURL: await env.AI.gateway("my-gateway").getUrl("anthropic"),
137142
});
138143
```
139144

@@ -145,14 +150,14 @@ Refer to the [Universal endpoint documentation](/ai-gateway/providers/universal/
145150

146151
```typescript
147152
const resp = await gateway.run({
148-
provider: "workers-ai",
149-
endpoint: "@cf/meta/llama-3.1-8b-instruct",
150-
headers: {
151-
authorization: "Bearer my-api-token"
152-
},
153-
query: {
154-
prompt: "tell me a joke"
155-
}
153+
provider: "workers-ai",
154+
endpoint: "@cf/meta/llama-3.1-8b-instruct",
155+
headers: {
156+
authorization: "Bearer my-api-token",
157+
},
158+
query: {
159+
prompt: "tell me a joke",
160+
},
156161
});
157162
```
158163

@@ -168,4 +173,4 @@ With these AI Gateway binding methods, you can now:
168173
- Get gateway URLs for direct API access with `getUrl`, making it easy to integrate with popular AI SDKs.
169174
- Execute universal requests to any AI Gateway provider with `run`.
170175

171-
These methods offer greater flexibility and control over your AI integrations, empowering you to build more sophisticated applications on the Cloudflare Workers platform.
176+
These methods offer greater flexibility and control over your AI integrations, empowering you to build more sophisticated applications on the Cloudflare Workers platform.

0 commit comments

Comments
 (0)