OpenCode commited on
Commit
4becab4
·
1 Parent(s): d6d30df

debug: test CF token verify endpoint too

Browse files
Files changed (1) hide show
  1. api/debug.js +30 -16
api/debug.js CHANGED
@@ -8,9 +8,23 @@ function mask(s) {
8
  return s.slice(0, 6) + "..." + s.slice(-4);
9
  }
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  async function testCredential(credential) {
12
  const model = mapModelToCf("kimi-k2.7");
13
- const url = `https://api.cloudflare.com/client/v4/accounts/${credential.accountId}/ai/v1/chat/completions`;
 
14
  const body = {
15
  model,
16
  messages: [{ role: "user", content: "hi" }],
@@ -18,21 +32,20 @@ async function testCredential(credential) {
18
  stream: false,
19
  };
20
 
21
- try {
22
- const res = await fetch(url, {
23
- method: "POST",
24
- headers: {
25
- "Content-Type": "application/json",
26
- Authorization: `Bearer ${credential.token}`,
27
- },
28
- body: JSON.stringify(body),
29
- signal: AbortSignal.timeout(30000),
30
- });
31
- const text = await res.text();
32
- return { status: res.status, bodyPrefix: text.slice(0, 200) };
33
- } catch (err) {
34
- return { status: 0, error: err.message };
35
- }
36
  }
37
 
38
  module.exports = async function handler(req, res) {
@@ -46,5 +59,6 @@ module.exports = async function handler(req, res) {
46
  last: credentials[credentials.length - 1] ? { accountId: mask(credentials[credentials.length - 1].accountId), tokenPrefix: mask(credentials[credentials.length - 1].token) } : null,
47
  rawLength: (process.env.CLOUDFLARE_CREDENTIALS || "").length,
48
  firstTest,
 
49
  });
50
  };
 
8
  return s.slice(0, 6) + "..." + s.slice(-4);
9
  }
10
 
11
+ async function testUrl(url, options = {}) {
12
+ try {
13
+ const res = await fetch(url, {
14
+ ...options,
15
+ signal: AbortSignal.timeout(30000),
16
+ });
17
+ const text = await res.text();
18
+ return { status: res.status, bodyPrefix: text.slice(0, 200) };
19
+ } catch (err) {
20
+ return { status: 0, error: err.message, cause: err.cause ? String(err.cause) : null };
21
+ }
22
+ }
23
+
24
  async function testCredential(credential) {
25
  const model = mapModelToCf("kimi-k2.7");
26
+ const chatUrl = `https://api.cloudflare.com/client/v4/accounts/${credential.accountId}/ai/v1/chat/completions`;
27
+ const verifyUrl = "https://api.cloudflare.com/client/v4/user/tokens/verify";
28
  const body = {
29
  model,
30
  messages: [{ role: "user", content: "hi" }],
 
32
  stream: false,
33
  };
34
 
35
+ const verifyResult = await testUrl(verifyUrl, {
36
+ headers: { Authorization: `Bearer ${credential.token}` },
37
+ });
38
+
39
+ const chatResult = await testUrl(chatUrl, {
40
+ method: "POST",
41
+ headers: {
42
+ "Content-Type": "application/json",
43
+ Authorization: `Bearer ${credential.token}`,
44
+ },
45
+ body: JSON.stringify(body),
46
+ });
47
+
48
+ return { verifyResult, chatResult };
 
49
  }
50
 
51
  module.exports = async function handler(req, res) {
 
59
  last: credentials[credentials.length - 1] ? { accountId: mask(credentials[credentials.length - 1].accountId), tokenPrefix: mask(credentials[credentials.length - 1].token) } : null,
60
  rawLength: (process.env.CLOUDFLARE_CREDENTIALS || "").length,
61
  firstTest,
62
+ nodeVersion: process.version,
63
  });
64
  };