-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Workers AI in local mode (#4522)
* Add support for Workers AI in local mode * trigger build * Add support for AI binding in pages * Lint code * Fix pages binding
- Loading branch information
Showing
16 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
Add support for Workers AI in local mode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "ai-app", | ||
"version": "1.0.1", | ||
"private": true, | ||
"description": "", | ||
"license": "ISC", | ||
"author": "", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"check:type": "tsc", | ||
"test": "vitest run", | ||
"test:watch": "vitest", | ||
"type:tests": "tsc -p ./tests/tsconfig.json" | ||
}, | ||
"devDependencies": { | ||
"undici": "^5.23.0", | ||
"wrangler": "workspace:*", | ||
"@cloudflare/workers-tsconfig": "workspace:^", | ||
"@cloudflare/ai": "^1.0.35" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
console.log("startup log"); | ||
|
||
export default { | ||
async fetch(request, env) { | ||
console.log("request log"); | ||
|
||
return Response.json({ | ||
binding: env.AI, | ||
fetcher: env.AI.fetch.toString(), | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { resolve } from "path"; | ||
import { fetch } from "undici"; | ||
import { describe, it, beforeAll, afterAll } from "vitest"; | ||
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived"; | ||
|
||
describe("'wrangler dev' correctly renders pages", () => { | ||
let ip: string, | ||
port: number, | ||
stop: (() => Promise<unknown>) | undefined, | ||
getOutput: () => string; | ||
|
||
beforeAll(async () => { | ||
({ ip, port, stop, getOutput } = await runWranglerDev( | ||
resolve(__dirname, ".."), | ||
["--local", "--port=0", "--inspector-port=0"] | ||
)); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stop?.(); | ||
}); | ||
|
||
it("ai binding is defined ", async ({ expect }) => { | ||
const response = await fetch(`http://${ip}:${port}/`); | ||
const content = await response.json(); | ||
expect(content).toEqual({ | ||
binding: {}, | ||
fetcher: "function fetch() { [native code] }", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "@cloudflare/workers-tsconfig/tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node"] | ||
}, | ||
"include": ["**/*.ts", "../../../node-types.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"esModuleInterop": true, | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["node"], | ||
"skipLibCheck": true, | ||
"moduleResolution": "node", | ||
"noEmit": true | ||
}, | ||
"include": ["tests", "../../node-types.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
testTimeout: 10_000, | ||
hookTimeout: 10_000, | ||
teardownTimeout: 10_000, | ||
useAtomics: true, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name = "ai-app" | ||
compatibility_date = "2023-11-21" | ||
|
||
main = "src/index.js" | ||
|
||
[ai] | ||
binding = "AI" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Response } from "miniflare"; | ||
import { performApiFetch } from "../cfetch/internal"; | ||
import { getAccountId } from "../user"; | ||
import type { Request } from "miniflare"; | ||
|
||
export async function AIFetcher(request: Request) { | ||
const accountId = await getAccountId(); | ||
|
||
request.headers.delete("Host"); | ||
request.headers.delete("Content-Length"); | ||
|
||
const res = await performApiFetch(`/accounts/${accountId}/ai/run/proxy`, { | ||
method: "POST", | ||
headers: Object.fromEntries(request.headers.entries()), | ||
body: request.body, | ||
duplex: "half", | ||
}); | ||
|
||
return new Response(res.body, { status: res.status }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.