Spaces:
Running
Running
incognitolm commited on
Commit ·
1baccaa
1
Parent(s): c4a54a5
Versions
Browse files- server/chatStream.js +12 -1
- server/wsHandler.js +10 -0
server/chatStream.js
CHANGED
|
@@ -129,6 +129,17 @@ export async function streamChat(ws, {
|
|
| 129 |
{ role: "user", content: userMessage },
|
| 130 |
];
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
try {
|
| 133 |
const stream = await client.chat.completions.create({
|
| 134 |
model: model || "lightning",
|
|
@@ -190,7 +201,7 @@ function normalizeMessage(msg) {
|
|
| 190 |
.join("\n");
|
| 191 |
return { role: msg.role, content: textOnly || "" };
|
| 192 |
}
|
| 193 |
-
return { role: msg.role, content: msg.content };
|
| 194 |
}
|
| 195 |
|
| 196 |
function buildToolList(tools) {
|
|
|
|
| 129 |
{ role: "user", content: userMessage },
|
| 130 |
];
|
| 131 |
|
| 132 |
+
// Validate messages before API call
|
| 133 |
+
for (let i = 0; i < messages.length; i++) {
|
| 134 |
+
const msg = messages[i];
|
| 135 |
+
if (msg.role !== "assistant" || !msg.tool_calls) {
|
| 136 |
+
if (typeof msg.content !== "string") {
|
| 137 |
+
console.error(`Message ${i} has invalid content:`, msg);
|
| 138 |
+
throw new Error(`Message ${i} missing or invalid content property`);
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
try {
|
| 144 |
const stream = await client.chat.completions.create({
|
| 145 |
model: model || "lightning",
|
|
|
|
| 201 |
.join("\n");
|
| 202 |
return { role: msg.role, content: textOnly || "" };
|
| 203 |
}
|
| 204 |
+
return { role: msg.role, content: msg.content ?? "" };
|
| 205 |
}
|
| 206 |
|
| 207 |
function buildToolList(tools) {
|
server/wsHandler.js
CHANGED
|
@@ -192,6 +192,16 @@ const handlers = {
|
|
| 192 |
const asstEntry = buildEntry('assistant', finalText, mergedCalls);
|
| 193 |
const newHistory = [...(session.history || []), userEntry, asstEntry];
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
// Use session name from XML tag; fall back to existing name or default
|
| 196 |
let newName = session.name;
|
| 197 |
if (sessionNameFromTag) {
|
|
|
|
| 192 |
const asstEntry = buildEntry('assistant', finalText, mergedCalls);
|
| 193 |
const newHistory = [...(session.history || []), userEntry, asstEntry];
|
| 194 |
|
| 195 |
+
// Update tails for all versioned messages to include new messages
|
| 196 |
+
for (let i = 0; i < newHistory.length; i++) {
|
| 197 |
+
const msg = newHistory[i];
|
| 198 |
+
if (msg.versions) {
|
| 199 |
+
for (const version of msg.versions) {
|
| 200 |
+
version.tail = newHistory.slice(i + 1);
|
| 201 |
+
}
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
// Use session name from XML tag; fall back to existing name or default
|
| 206 |
let newName = session.name;
|
| 207 |
if (sessionNameFromTag) {
|