add markdown parsing + latex + tree-sitter highlighting for codeblocks in llm responses

This commit is contained in:
2026-08-24 23:55:51 +02:00
parent aae3757daf
commit e18875a752
53 changed files with 5043 additions and 618 deletions
+42 -18
View File
@@ -1,5 +1,8 @@
#pragma once
#include "segment.hpp"
#include <QList>
#include <QObject>
#include <QString>
#include <QTimer>
@@ -7,6 +10,11 @@
namespace ZShell::llm {
// One attempt at answering a message: a chronologically ordered list
// of segments. Content bursts, reasoning bursts and tool calls all
// appear in the order the model produced them; a new content (or
// reasoning) segment starts whenever the model switches between them.
// Together with the attempt's aggregate state.
class ChatGeneration : public QObject {
Q_OBJECT
QML_ELEMENT
@@ -16,55 +24,71 @@ class ChatGeneration : public QObject {
Q_PROPERTY(QString reasoning READ reasoning NOTIFY reasoningChanged)
Q_PROPERTY(qint64 reasoningElapsedMs READ reasoningElapsedMs NOTIFY elapsedMsChanged)
Q_PROPERTY(qint64 contentElapsedMs READ contentElapsedMs NOTIFY elapsedMsChanged)
Q_PROPERTY(qint64 toolsElapsedMs READ toolsElapsedMs NOTIFY elapsedMsChanged)
Q_PROPERTY(bool streaming READ streaming NOTIFY streamingChanged)
Q_PROPERTY(bool reasoningActive READ reasoningActive NOTIFY reasoningActiveChanged)
Q_PROPERTY(bool hasRunningTool READ hasRunningTool NOTIFY toolStateChanged)
Q_PROPERTY(qint64 timestamp READ timestamp CONSTANT)
Q_PROPERTY(
QList<ZShell::llm::LlmSegment*> segments READ segments
NOTIFY segmentsChanged)
Q_PROPERTY(int toolCallCount READ toolCallCount NOTIFY segmentsChanged)
public:
explicit ChatGeneration(qint64 timestamp, QObject* parent = nullptr);
[[nodiscard]] QString content() const { return m_content; }
[[nodiscard]] QString reasoning() const { return m_reasoning; }
[[nodiscard]] bool reasoningActive() const { return m_reasoningActive; }
// All content bursts, joined (the model's full answer).
[[nodiscard]] QString content() const;
// Every reasoning burst, joined.
[[nodiscard]] QString reasoning() const;
// True while the model is thinking: streaming, no content yet, and
// no tool call in flight.
[[nodiscard]] bool reasoningActive() const;
[[nodiscard]] qint64 reasoningElapsedMs() const;
[[nodiscard]] qint64 contentElapsedMs() const;
[[nodiscard]] qint64 toolsElapsedMs() const;
[[nodiscard]] bool streaming() const { return m_streaming; }
[[nodiscard]] qint64 timestamp() const { return m_timestamp; }
[[nodiscard]] QList<LlmSegment*> segments() const { return m_segments; }
[[nodiscard]] int toolCallCount() const;
[[nodiscard]] bool hasRunningTool() const;
void setContent(const QString& value);
void setReasoning(const QString& value);
void appendContent(const QString& piece);
void appendReasoning(const QString& piece);
void setElapsedMs(qint64 reasoningMs, qint64 contentMs);
void setStreaming(bool value);
// The in-flight content segment, or a fresh one. A new content
// segment starts whenever the model resumes writing after reasoning
// or a tool call.
[[nodiscard]] LlmSegment* openContentSegment();
// The in-flight reasoning segment, or a fresh one.
[[nodiscard]] LlmSegment* openReasoningSegment();
// Creates and appends a running tool-call segment.
[[nodiscard]] LlmSegment* beginToolCall(
const QString& name, const QString& toolCallId);
// Appends a segment created by the persistence layer.
void addSegment(LlmSegment* segment);
// Stops the clocks of every in-flight segment.
void closeOpenSegments();
Q_SIGNALS:
void contentChanged();
void reasoningChanged();
void reasoningActiveChanged();
void elapsedMsChanged();
void streamingChanged();
void toolStateChanged();
void segmentsChanged();
private:
void updateReasoningActive();
[[nodiscard]] bool reasoningInFlight() const {
return m_reasoningStartedAt > 0 && m_reasoningEndedAt <= 0;
}
[[nodiscard]] bool contentInFlight() const {
return m_contentStartedAt > 0 && m_contentEndedAt <= 0;
}
QTimer m_timer;
QString m_content;
QString m_reasoning;
QList<LlmSegment*> m_segments;
bool m_reasoningActive = false;
bool m_streaming = false;
qint64 m_timestamp;
qint64 m_reasoningStartedAt = 0;
qint64 m_reasoningEndedAt = 0;
qint64 m_contentStartedAt = 0;
qint64 m_contentEndedAt = 0;
};
} // namespace ZShell::llm