Add OpenAI compatible chat backend

This commit is contained in:
2026-05-22 20:52:35 +03:00
parent 321d185592
commit 46a99605e6
41 changed files with 2244 additions and 151 deletions
+61
View File
@@ -5,6 +5,11 @@ const {
extractOpenAiToolCalls,
extractOpenAiStreamingToolCalls,
extractOpenAiTextDelta,
extractOpenAiChatToolCalls,
extractOpenAiChatStreamingToolCalls,
extractOpenAiChatTextDelta,
mergeToolCallChunks,
normalizeStreamingTextDelta,
extractMistralToolCalls,
extractMistralTextDelta,
extractOllamaToolCalls,
@@ -42,6 +47,62 @@ test("openai contract extracts text delta and function calls", () => {
assert.equal(streamed[0].name, "search_files");
});
test("openai chat contract extracts text delta and tool calls", () => {
assert.equal(extractOpenAiChatTextDelta({choices: [{delta: {content: "hello chat"}}]}), "hello chat");
assert.equal(normalizeStreamingTextDelta("hel", "hello"), "lo");
assert.equal(normalizeStreamingTextDelta("hel", "lo"), "lo");
const calls = extractOpenAiChatToolCalls({
choices: [{
message: {
tool_calls: [{
id: "chat-1",
function: {
name: "read_user_info",
arguments: "{\"userId\":123}",
},
}],
},
}],
});
assert.equal(calls.length, 1);
assert.equal(calls[0].id, "chat-1");
assert.equal(calls[0].name, "read_user_info");
const streamed = extractOpenAiChatStreamingToolCalls({
choices: [{
delta: {
tool_calls: [{
index: 0,
id: "chat-2",
function: {
name: "write_note",
arguments: "{\"text\":",
},
}],
},
}],
});
assert.equal(streamed.length, 1);
assert.equal(streamed[0].id, "chat-2");
assert.equal(streamed[0].name, "write_note");
assert.equal(streamed[0].argumentsText, "{\"text\":");
const merged = mergeToolCallChunks([
{id: "chat-2", name: "", argumentsText: "{\"text\":"},
], [{
id: "chat-2",
name: "write_note",
argumentsText: "\"hello\"}",
}]);
assert.equal(merged.length, 1);
assert.equal(merged[0].name, "write_note");
assert.equal(merged[0].argumentsText, "{\"text\":\"hello\"}");
});
test("mistral contract extracts content and tool calls", () => {
assert.equal(extractMistralTextDelta({
content: [{text: "hello"}, {text: " world"}],