-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_worker.py
More file actions
157 lines (139 loc) · 4.6 KB
/
Copy pathfix_worker.py
File metadata and controls
157 lines (139 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import re
with open('cloudflare-worker.ts', 'r', encoding='utf-8') as f:
content = f.read()
old_env = """export interface Env {
OPENROUTER_API_KEY?: string;
GROQ_API_KEY?: string;
}"""
new_env = """export interface Env {
OPENROUTER_API_KEY?: string;
GROQ_API_KEY?: string;
GEMINI_API_KEY?: string;
NVIDIA_API_KEY?: string;
}"""
if old_env in content:
content = content.replace(old_env, new_env)
print("Env interface updated")
else:
print("WARNING: Env interface pattern not found")
pattern = re.compile(r"async function handleChat\(.*?\n\}\n", re.DOTALL)
new_handle_chat = '''async function handleChat(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const body = await request.json().catch(() => ({}));
const { messages } = body as any;
type Provider = { name: string; apiKey: string; upstream: string; model: string; extraHeaders?: Record<string, string> };
const providers: Provider[] = [];
if (env.GROQ_API_KEY) {
providers.push({
name: 'groq',
apiKey: env.GROQ_API_KEY,
upstream: 'https://api.groq.com/openai/v1/chat/completions',
model: 'llama-3.3-70b-versatile',
});
}
if (env.OPENROUTER_API_KEY) {
providers.push({
name: 'openrouter',
apiKey: env.OPENROUTER_API_KEY,
upstream: 'https://openrouter.ai/api/v1/chat/completions',
model: 'meta-llama/llama-3.3-70b-instruct:free',
extraHeaders: { 'HTTP-Referer': 'https://aiscern.com', 'X-Title': 'Aiscern' },
});
}
if (env.GEMINI_API_KEY) {
providers.push({
name: 'gemini',
apiKey: env.GEMINI_API_KEY,
upstream: 'https://generativelanguage.googleapis.com/v1beta/openai/chat/completions',
model: 'gemini-2.0-flash',
});
}
if (env.NVIDIA_API_KEY) {
providers.push({
name: 'nvidia',
apiKey: env.NVIDIA_API_KEY,
upstream: 'https://integrate.api.nvidia.com/v1/chat/completions',
model: 'meta/llama-3.3-70b-instruct',
});
}
if (!providers.length) {
return json({
text: "I'm ARIA, Aiscern's detection assistant. I can analyze text, images, audio, and video for AI-generated content. Upload a file or ask me anything about deepfake detection.",
source: 'knowledge_base_fallback',
});
}
let upstreamRes: Response | null = null;
let lastErr = '';
for (const provider of providers) {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${provider.apiKey}`,
...(provider.extraHeaders || {}),
};
const res = await fetch(provider.upstream, {
method: 'POST',
headers,
body: JSON.stringify({
model: provider.model,
messages: messages?.map((m: any) => ({ role: m.role, content: m.content })) || [],
stream: true,
temperature: 0.7,
max_tokens: 2048,
}),
});
if (res.ok) {
upstreamRes = res;
break;
}
lastErr = `[${provider.name}] ${await res.text()}`;
}
if (!upstreamRes) {
return json({ error: `All providers failed. Last error: ${lastErr}` }, 502);
}
const { readable, writable } = new TransformStream();
const writer = writable.getWriter();
const encoder = new TextEncoder();
ctx.waitFor((async () => {
const reader = upstreamRes.body!.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = new TextDecoder().decode(value);
for (const line of chunk.split('\\n')) {
if (!line.startsWith('data: ')) continue;
const data = line.slice(6).trim();
if (data === '[DONE]') {
await writer.write(encoder.encode('data: {"type":"done"}\\n\\n'));
continue;
}
try {
const parsed = JSON.parse(data);
const delta = parsed.choices?.[0]?.delta?.content || '';
if (delta) {
await writer.write(encoder.encode(`data: ${JSON.stringify({ choices: [{ delta: { content: delta } }] })}\\n\\n`));
}
} catch { /* skip malformed */ }
}
}
} finally {
await writer.close();
}
})());
return new Response(readable, {
headers: {
...CORS_HEADERS,
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
},
});
}
'''
new_content, n = pattern.subn(new_handle_chat, content, count=1)
if n == 1:
print("handleChat function replaced")
else:
print("ERROR: handleChat pattern not matched - NOTHING changed")
with open('cloudflare-worker.ts', 'w', encoding='utf-8') as f:
f.write(new_content)
print("Done.")