File size: 3,032 Bytes
4888d8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5f49d0
4888d8d
 
e5f49d0
4888d8d
 
 
 
e5f49d0
 
 
 
 
 
 
 
 
4888d8d
 
 
 
e5f49d0
4888d8d
e5f49d0
4888d8d
 
e5f49d0
4888d8d
 
 
a5a5683
4888d8d
 
 
 
 
e5f49d0
a5a5683
e5f49d0
 
 
a5a5683
 
 
e5f49d0
a5a5683
 
 
 
 
e5f49d0
 
 
 
a5a5683
e5f49d0
 
 
 
f4215ce
e5f49d0
 
 
 
 
f4215ce
a5a5683
e5f49d0
a5a5683
 
f4215ce
a5a5683
 
 
e5f49d0
f4215ce
 
 
e5f49d0
 
 
a5a5683
 
 
 
 
 
 
e5f49d0
f4215ce
a5a5683
4888d8d
 
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
// src/api.js
export const API_BASE = "https://deepthoughtai-prolog-gateway.hf.space";

export async function login(username, password) {
  const res = await fetch(`${API_BASE}/login`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ username, password }),
  });

  let json;
  const text = await res.text().catch(() => "");
  try {
    json = text ? JSON.parse(text) : {};
  } catch {
    json = {};
  }

  if (!res.ok) {
    throw new Error(json?.error || text || `Login failed (${res.status})`);
  }
  if (!json.ok || !json.token) {
    throw new Error(json?.error || "Login failed");
  }
  return json; // { ok, token }
}

export async function streamChat(
  message,
  history,
  token,
  onToken,
  sessionId = null,
  onStatus = null,
  onDebug = null
) {
  const res = await fetch(`${API_BASE}/chat/stream`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify({ message, history, session_id: sessionId }),
  });

  if (!res.body) throw new Error("Streaming not supported");

  const reader = res.body.getReader();
  const decoder = new TextDecoder();
  let buffer = "";

  while (true) {
    const { value, done } = await reader.read();
    if (done) break;

    buffer += decoder.decode(value);

    // Parse status messages: __STATUS__:{"message":"..."}__END__
    const statusRe = /__STATUS__:(.*?)__END__/g;
    const debugRe = /__DEBUG__:(.*?)__END__/g;
    let match;
    let lastIndex = 0;

    while ((match = statusRe.exec(buffer)) !== null) {
      if (match.index > lastIndex) {
        const content = buffer.substring(lastIndex, match.index);
        if (content) onToken(content);
      }
      try {
        const data = JSON.parse(match[1]);
        if (onStatus && data.message) onStatus(data.message);
      } catch {}
      lastIndex = statusRe.lastIndex;
    }

    // Parse debug markers
    debugRe.lastIndex = 0;
    while ((match = debugRe.exec(buffer)) !== null) {
      try {
        const data = JSON.parse(match[1]);
        if (onDebug) onDebug(data);
        buffer = buffer.substring(0, match.index) + buffer.substring(debugRe.lastIndex);
        debugRe.lastIndex = match.index;
      } catch {}
    }

    // Emit remaining content
    if (lastIndex < buffer.length) {
      const content = buffer.substring(lastIndex);
      if (!content.includes("__STATUS__") && !content.includes("__DEBUG__")) {
        onToken(content);
        buffer = "";
      } else {
        const markerStart = Math.max(
          content.lastIndexOf("__STATUS__"),
          content.lastIndexOf("__DEBUG__")
        );
        if (markerStart > 0) {
          onToken(content.substring(0, markerStart));
          buffer = content.substring(markerStart);
        } else {
          buffer = content;
        }
      }
    }
  }

  // Flush remaining
  if (buffer && !buffer.includes("__STATUS__") && !buffer.includes("__DEBUG__")) {
    onToken(buffer);
  }
}