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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
| const CACHE_TTL = 864000;
function createHTML(title, content) { return `<!DOCTYPE html> <html> <head> <title>${title}</title> <meta charset="UTF-8"> <style> body { font-family: -apple-system, system-ui, sans-serif; max-width: 800px; margin: 1rem auto; padding: 0 1rem; line-height: 1.5 } .box { background: #f8f9fa; padding: 1rem; margin: 1rem 0; border-radius: 4px; border: 1px solid #dee2e6 } .cache-link { color: #0d6efd; word-break: break-all } </style> </head> <body> ${content} </body> </html>`; }
function escapeHTML(str) { return str.replace(/[&<>"']/g, m => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[m] ); }
function markdownToHtml(markdown) { const lines = markdown.split('\n'); let html = []; let inList = false; let currentParagraph = [];
const flushParagraph = () => { if (currentParagraph.length > 0) { html.push(`<p>${processInline(currentParagraph.join('<br>'))}</p>`); currentParagraph = []; } };
const flushList = () => { if (inList) { html.push('</ul>'); inList = false; } };
const processInline = (text) => { return text .replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img src="$2" alt="$1">') .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>') .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>') .replace(/\*(.*?)\*/g, '<em>$1</em>'); };
for (const line of lines) { const headingMatch = line.match(/^(#+)\s+(.*)/); if (headingMatch) { flushParagraph(); flushList(); const level = Math.min(headingMatch[1].length, 6); html.push(`<h${level}>${processInline(headingMatch[2])}</h${level}>`); continue; }
const listItemMatch = line.match(/^[*\-]\s+(.*)/); if (listItemMatch) { flushParagraph(); if (!inList) { html.push('<ul>'); inList = true; } html.push(`<li>${processInline(listItemMatch[1])}</li>`); continue; } else if (inList) { flushList(); }
if (line.trim() === '') { flushParagraph(); continue; }
currentParagraph.push(line.trim()); }
flushParagraph(); flushList();
return html.join('\n'); }
async function handleRequest(request,env) { const url = new URL(request.url); const params = url.searchParams; const id = params.get('id'); let cacheId = params.get('cache_id'); if (cacheId) { const cachedData = await env.CACHE.get(cacheId); if (cachedData) { const { question, convertedAnswer } = JSON.parse(cachedData); return new Response(createHTML( `缓存内容 #${cacheId}`, `<h1>缓存内容</h1> <div class="box"> <h3>原始问题:</h3> <div>${question}</div> <h3>AI回复:</h3> <div>${convertedAnswer}</div> </div> <p><a href="${url.origin}">返回新提问</a></p>` ), { headers: { 'Content-Type': 'text/html' } }); } return new Response(createHTML( '缓存不存在', '<h1>缓存不存在或已过期</h1><p><a href="/">返回</a></p>' ), { status: 404, headers: { 'Content-Type': 'text/html' } }); } if (id) { cacheId = id; const cachedData = await env.CACHE.get(cacheId); if (cachedData) { const { question, convertedAnswer } = JSON.parse(cachedData); return new Response(createHTML( `#${cacheId} ${question}`, `<h1>缓存内容</h1> <div class="box"> <h3>原始问题:</h3> <div>${question}</div> <h3>AI回复:</h3> <div>${convertedAnswer}</div> </div> <p><a href="${url.origin}">返回新提问</a></p>` ), { headers: { 'Content-Type': 'text/html' } }); } } const question = params.get('content'); if (!question) { return new Response(createHTML('JavaScript-free DeepSeek','<h2>我是 DeepSeek-R1,不该问的别问</h2><p>我有可能可以帮你写代码、读文件、写作各种创意内容,请把你的任务交给我吧~</p><p>提示:上为content,下为id,注意区分</p><form method="GET" action="/"><input type="text" name="content" placeholder="向 DeepSeek-R1 询问任何问题"><input type="text" name="id" placeholder="请输入ID"><button type="submit">发送</button></form><p>Created by Keee</p>'), { status: 400, headers: { 'Content-Type': 'text/html' } }); } try { const response = await fetch('https://integrate.api.nvidia.com/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ***` }, body: JSON.stringify({ model: "deepseek-ai/deepseek-r1", messages: [{ role: "user", content: question }], temperature:0.6, top_p:0.7, stream:false }) });
const data = await response.json(); const answer = data.choices[0].message.content; const convertedAnswer = markdownToHtml(answer); let cacheId = Date.now().toString(36) + Math.random().toString(36).substr(2, 5) if(id) cacheId = id await env.CACHE.put(cacheId, JSON.stringify({ question, convertedAnswer }), { expirationTtl: CACHE_TTL });
return new Response(createHTML( question, `<h1>Deepseek-R1</h1> <div class="box"> <h3>问题:</h3> <div>${escapeHTML(question)}</div> <h3>AI回复:</h3> <div>${convertedAnswer}</div> </div> <p>缓存链接:<br> <a class="cache-link" href="?cache_id=${cacheId}">${url.origin}/?cache_id=${cacheId}</a> </p>` ), { headers: { 'Content-Type': 'text/html' } });
} catch (error) { return new Response(createHTML( '发生错误', `<h1>请求处理失败</h1> <div class="box">${escapeHTML(error.message)}</div>` ), { status: 500, headers: { 'Content-Type': 'text/html' } }); } }
export default { async fetch(request, env) { try { return await handleRequest(request, env); } catch (err) { return new Response(createHTML( '服务器错误', `<h1>服务器内部错误</h1><div class="box">${escapeHTML(err.message)}</div>` ), { status: 500, headers: { 'Content-Type': 'text/html' } }); } } };
|