Skip to content

Commit e29d636

Browse files
committed
Use native AI bindings
1 parent 1a95d6f commit e29d636

File tree

5 files changed

+225
-241
lines changed

5 files changed

+225
-241
lines changed

package-lock.json

Lines changed: 13 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/main/src/handler.ts

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,80 @@ import { sha256, log } from "./libs";
33
import { Config, Update, localhost } from "./types";
44

55
export default class Handler {
6-
configs: Partial<Config>[];
6+
configs: Partial<Config>[];
77

8-
constructor(configs: Partial<Config>[]) {
9-
this.configs = configs;
10-
}
8+
constructor(configs: Partial<Config>[]) {
9+
this.configs = configs;
10+
}
1111

12-
getResponse = async (
13-
_request?: Request,
14-
_bot?: BotApi
15-
): Promise<Response> => {
16-
this.getAccessKeys(this.configs).then((access_keys) =>
17-
Object.keys(access_keys).forEach((key) =>
18-
log(
19-
`${access_keys[key].bot_name} ${((request_url) =>
20-
`${request_url.origin}${request_url.pathname}`)(
21-
new URL(_request?.url ?? localhost)
22-
)}${key}`
23-
)
24-
)
25-
);
26-
if (_bot?.webhook.token) {
27-
return _bot.webhook.process(new URL(_request?.url ?? localhost));
28-
}
29-
return this.responses.default();
30-
};
12+
getResponse = async (
13+
_request?: Request,
14+
_bot?: BotApi
15+
): Promise<Response> => {
16+
this.getAccessKeys(this.configs).then((access_keys) =>
17+
Object.keys(access_keys).forEach((key) =>
18+
log(
19+
`${access_keys[key].bot_name} ${((request_url) =>
20+
`${request_url.origin}${request_url.pathname}`)(
21+
new URL(_request?.url ?? localhost)
22+
)}${key}`
23+
)
24+
)
25+
);
26+
if (_bot?.webhook.token) {
27+
return _bot.webhook.process(new URL(_request?.url ?? localhost));
28+
}
29+
return this.responses.default();
30+
};
3131

32-
postResponse = async (
33-
_request?: Request,
34-
_bot?: BotApi
35-
): Promise<Response> =>
36-
_bot?.webhook.token === ""
37-
? this.responses.default()
38-
: _request
39-
? _request
40-
.json()
41-
.then((update) => (_bot as BotApi).update(update as Update))
42-
: this.responses.default();
32+
postResponse = async (
33+
_request?: Request,
34+
_bot?: BotApi
35+
): Promise<Response> =>
36+
_bot?.webhook.token === ""
37+
? this.responses.default()
38+
: _request
39+
? _request
40+
.json()
41+
.then((update) => (_bot as BotApi).update(update as Update))
42+
: this.responses.default();
4343

44-
responses: Record<
45-
string,
46-
(_request?: Request, _bot?: BotApi) => Promise<Response>
47-
> = {
48-
GET: this.getResponse,
49-
POST: this.postResponse,
50-
default: () => new Promise(() => new Response()),
51-
};
44+
responses: Record<
45+
string,
46+
(_request?: Request, _bot?: BotApi) => Promise<Response>
47+
> = {
48+
GET: this.getResponse,
49+
POST: this.postResponse,
50+
default: () => new Promise(() => new Response()),
51+
};
5252

53-
getAccessKeys = async (
54-
configs: Partial<Config>[]
55-
): Promise<Record<string, Config> | Record<string, never>> =>
56-
Promise.all(
57-
configs.map((bot_config: Partial<Config>) =>
58-
sha256(bot_config.webhook?.token ?? "").then((hash) => {
59-
console.log(hash);
60-
return [hash, bot_config];
61-
})
62-
)
63-
).then((result) => Object.fromEntries(result));
53+
getAccessKeys = async (
54+
configs: Partial<Config>[]
55+
): Promise<Record<string, Config> | Record<string, never>> =>
56+
Promise.all(
57+
configs.map((bot_config: Partial<Config>) =>
58+
sha256(bot_config.webhook?.token ?? "").then((hash) => {
59+
console.log(hash);
60+
return [hash, bot_config];
61+
})
62+
)
63+
).then((result) => Object.fromEntries(result));
6464

65-
// handles the request
66-
handle = async (request: Request): Promise<Response> =>
67-
this.getAccessKeys(this.configs).then((access_keys) =>
68-
Object.keys(this.responses).includes(request.method)
69-
? this.responses[request.method](
70-
request,
71-
((key) => {
72-
return new access_keys[key].api({
73-
...new Config(),
74-
...access_keys[key],
75-
url: new URL(new URL(request.url).origin), // worker url
76-
handler: this,
77-
});
78-
})(new URL(request.url).pathname.substring(1))
79-
)
80-
: this.responses.default()
81-
);
65+
// handles the request
66+
handle = async (request: Request): Promise<Response> =>
67+
this.getAccessKeys(this.configs).then((access_keys) =>
68+
Object.keys(this.responses).includes(request.method)
69+
? this.responses[request.method](
70+
request,
71+
((key) => {
72+
return new access_keys[key].api({
73+
...new Config(),
74+
...access_keys[key],
75+
url: new URL(new URL(request.url).origin), // worker url
76+
handler: this,
77+
});
78+
})(new URL(request.url).pathname.substring(1))
79+
)
80+
: this.responses.default()
81+
);
8282
}

packages/main/src/telegram_bot.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
Commands,
1919
Kv,
2020
} from "./types";
21-
import { Ai } from "@cloudflare/ai";
2221

2322
export default class TelegramBot extends TelegramApi {
2423
url: URL;
@@ -55,7 +54,6 @@ export default class TelegramBot extends TelegramApi {
5554
if (this.ai === undefined) {
5655
return new Response("ok");
5756
}
58-
const ai = new Ai(this.ai);
5957
let _prompt: string;
6058
if (args[0][0] === "/") {
6159
_prompt = args.slice(1).join(" ");
@@ -68,7 +66,7 @@ export default class TelegramBot extends TelegramApi {
6866
const langs = ["french", "arabic", "german", "spanish", "russian"];
6967
const inline_articles = await Promise.all(
7068
langs.map(async (lang) => {
71-
const response = await ai.run("@cf/meta/m2m100-1.2b", {
69+
const response = await this.ai.run("@cf/meta/m2m100-1.2b", {
7270
text: _prompt,
7371
source_lang: lang,
7472
target_lang: "english",
@@ -109,7 +107,6 @@ export default class TelegramBot extends TelegramApi {
109107

110108
// bot command: /image
111109
image = async (update: TelegramUpdate, args: string[]): Promise<Response> => {
112-
const ai = new Ai(this.ai);
113110
let _prompt: string;
114111
if (args[0][0] === "/") {
115112
_prompt = args.slice(1).join(" ");
@@ -124,7 +121,7 @@ export default class TelegramBot extends TelegramApi {
124121
update.message?.chat.id ?? 0,
125122
"image is processing. please wait..."
126123
);
127-
const response = await ai.run(
124+
const response = await this.ai.run(
128125
"@cf/stabilityai/stable-diffusion-xl-base-1.0",
129126
inputs
130127
);
@@ -142,7 +139,6 @@ export default class TelegramBot extends TelegramApi {
142139
if (this.ai === undefined) {
143140
return new Response("ok");
144141
}
145-
const ai = new Ai(this.ai);
146142
let _prompt: string;
147143
if (args[0][0] === "/") {
148144
_prompt = args.slice(1).join(" ");
@@ -194,13 +190,12 @@ export default class TelegramBot extends TelegramApi {
194190

195191
const p = system_prompt + "[INST]" + _prompt + "[/INST]";
196192
const prompt = p.slice(p.length - 4096, p.length);
197-
const response = await ai
198-
// @ts-expect-error ModelName doesn't need to be verified at build time
193+
const response = await this.ai
199194
.run(this.chat_model, {
200195
prompt,
201196
max_tokens: 596,
202197
})
203-
.then(({ response }) =>
198+
.then(({ response }: { response: string }) =>
204199
response
205200
.replace(/(\[|)(\/|)INST(S|)(s|)(\]|)/, "")
206201
.replace(/<<(\/|)SYS>>/, "")
@@ -247,7 +242,6 @@ export default class TelegramBot extends TelegramApi {
247242
if (this.ai === undefined) {
248243
return new Response("ok");
249244
}
250-
const ai = new Ai(this.ai);
251245
let _prompt: string;
252246
if (args[0][0] === "/") {
253247
_prompt = args.slice(1).join(" ");
@@ -303,13 +297,12 @@ export default class TelegramBot extends TelegramApi {
303297
const p = system_prompt + "[INST]" + _prompt + "[/INST]";
304298
const prompt = p.slice(p.length - 4096, p.length);
305299

306-
const response = await ai
307-
// @ts-expect-error ModelName doesn't need to be verified at build time
300+
const response = await this.ai
308301
.run(this.chat_model, {
309302
prompt,
310303
max_tokens: 596,
311304
})
312-
.then(({ response }) =>
305+
.then(({ response }: { response: string }) =>
313306
response
314307
.replace(/(\[|)(\/|)INST(S|)(s|)(\]|)/, "")
315308
.replace(/<<(\/|)SYS>>/, "")

0 commit comments

Comments
 (0)