1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-12-02 20:22:52 +00:00

add reasoning type

This commit is contained in:
Laura
2025-11-11 14:37:09 +01:00
parent b6c1a694e4
commit 61b53ad1c1
3 changed files with 88 additions and 54 deletions

15
chat.go
View File

@@ -434,10 +434,11 @@ func RunCompletion(ctx context.Context, response *Stream, request *openrouter.Ch
defer stream.Close() defer stream.Close()
var ( var (
id string id string
open int open int
close int close int
tool *ToolCall reasoning bool
tool *ToolCall
) )
buf := GetFreeBuffer() buf := GetFreeBuffer()
@@ -506,6 +507,12 @@ func RunCompletion(ctx context.Context, response *Stream, request *openrouter.Ch
response.WriteChunk(NewChunk(ChunkText, delta.Content)) response.WriteChunk(NewChunk(ChunkText, delta.Content))
} else if delta.Reasoning != nil { } else if delta.Reasoning != nil {
if !reasoning && len(delta.ReasoningDetails) != 0 {
reasoning = true
response.WriteChunk(NewChunk(ChunkReasoningType, delta.ReasoningDetails[0].Type))
}
response.WriteChunk(NewChunk(ChunkReasoning, *delta.Reasoning)) response.WriteChunk(NewChunk(ChunkReasoning, *delta.Reasoning))
} else if len(delta.Images) > 0 { } else if len(delta.Images) > 0 {
for _, image := range delta.Images { for _, image := range delta.Images {

View File

@@ -3,12 +3,13 @@
0: "start", 0: "start",
1: "id", 1: "id",
2: "reason", 2: "reason",
3: "text", 3: "reason_type",
4: "image", 4: "text",
5: "tool", 5: "image",
6: "error", 6: "tool",
7: "end", 7: "error",
8: "alive", 8: "end",
9: "alive",
}; };
const $version = document.getElementById("version"), const $version = document.getElementById("version"),
@@ -166,6 +167,7 @@
#id; #id;
#role; #role;
#reasoning; #reasoning;
#reasoningType;
#text; #text;
#images = []; #images = [];
#files = []; #files = [];
@@ -196,33 +198,48 @@
#_tool; #_tool;
#_statistics; #_statistics;
constructor(role, reasoning, text, tool = false, files = [], images = [], tags = [], time = 0, collapsed = false) { constructor(data) {
this.#id = uid(); this.#id = uid();
this.#role = role; this.#role = data.role;
this.#reasoning = reasoning || ""; this.#reasoning = data.reasoning || "";
this.#text = text || ""; this.#reasoningType = data.reasoningType || "";
this.#text = data.text || "";
this.#time = time; this.#time = data.time;
this.#_diff = document.createElement("div"); this.#_diff = document.createElement("div");
this.#build(collapsed); this.#build(data.collapsed);
this.#render(); this.#render();
if (tool?.name) { if (data.tool?.name) {
this.setTool(tool); this.setTool(data.tool);
} }
for (const file of files) { if (data.files) {
this.addFile(file); for (const file of data.files) {
this.addFile(file);
}
} }
for (const image of images) { if (data.images) {
this.addImage(image); for (const image of data.images) {
this.addImage(image);
}
} }
for (const tag of tags) { if (data.tags) {
this.addTag(tag); for (const tag of data.tags) {
this.addTag(tag);
}
}
if (data.statistics) {
this.setStatistics(data.statistics);
}
if (data.error) {
this.setError(data.error);
} }
messages.push(this); messages.push(this);
@@ -755,6 +772,12 @@
} }
if (!only || only === "reasoning") { if (!only || only === "reasoning") {
let reasoning = this.#reasoning || "";
if (this.#reasoningType === "reasoning.summary") {
reasoning = reasoning.replace(/(?<!^)\*\*(?!$)/gm, "\n\n**");
}
this.#patch("reasoning", this.#_reasoning, this.#reasoning, () => { this.#patch("reasoning", this.#_reasoning, this.#reasoning, () => {
this.#updateReasoningHeight(); this.#updateReasoningHeight();
@@ -825,6 +848,10 @@
if (this.#reasoning && full) { if (this.#reasoning && full) {
data.reasoning = this.#reasoning; data.reasoning = this.#reasoning;
if (this.#reasoningType) {
data.reasoningType = this.#reasoningType;
}
} }
if (this.#images.length && full) { if (this.#images.length && full) {
@@ -1003,6 +1030,13 @@
this.#save(); this.#save();
} }
setReasoningType(type) {
this.#reasoningType = type;
this.#render("reasoning");
this.#save();
}
addText(text) { addText(text) {
this.#text += text; this.#text += text;
@@ -1333,7 +1367,9 @@
function start() { function start() {
started = Date.now(); started = Date.now();
message = new Message("assistant", "", ""); message = new Message({
role: "assistant",
});
message.setState("waiting"); message.setState("waiting");
@@ -1436,6 +1472,10 @@
message.setState("reasoning"); message.setState("reasoning");
message.addReasoning(chunk.data); message.addReasoning(chunk.data);
break;
case "reason_type":
message.setReasoningType(chunk.data);
break; break;
case "text": case "text":
message.setState("receiving"); message.setState("receiving");
@@ -1722,25 +1762,7 @@
} }
loadValue("messages", []).forEach(message => { loadValue("messages", []).forEach(message => {
const obj = new Message( new Message(message);
message.role,
message.reasoning,
message.text,
message.tool,
message.files || [],
message.images || [],
message.tags || [],
message.time || 0,
message.collapsed
);
if (message.statistics) {
obj.setStatistics(message.statistics);
}
if (message.error) {
obj.setError(message.error);
}
}); });
chatTitle = loadValue("title"); chatTitle = loadValue("title");
@@ -1879,7 +1901,11 @@
$message.value = ""; $message.value = "";
storeValue("message", ""); storeValue("message", "");
const message = new Message($role.value, "", text, false, attachments); const message = new Message({
role: $role.value,
text: text,
files: attachments,
});
clearAttachments(); clearAttachments();
updateTitle(); updateTitle();

View File

@@ -13,15 +13,16 @@ import (
) )
const ( const (
ChunkStart ChunkType = 0 ChunkStart ChunkType = 0
ChunkID ChunkType = 1 ChunkID ChunkType = 1
ChunkReasoning ChunkType = 2 ChunkReasoning ChunkType = 2
ChunkText ChunkType = 3 ChunkReasoningType ChunkType = 3
ChunkImage ChunkType = 4 ChunkText ChunkType = 4
ChunkTool ChunkType = 5 ChunkImage ChunkType = 5
ChunkError ChunkType = 6 ChunkTool ChunkType = 6
ChunkEnd ChunkType = 7 ChunkError ChunkType = 7
ChunkAlive ChunkType = 8 ChunkEnd ChunkType = 8
ChunkAlive ChunkType = 9
) )
type ChunkType uint8 type ChunkType uint8