🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
SSEParser.hpp
浏览该文件的文档.
1
21
22#pragma once
23
24#include "AstGlobal.h"
25#include "AstUtil/NetworkStreamReceiver.hpp"
26#include "AstUtil/JsonValue.hpp"
27#include "AstAI/ChatEventHandler.hpp"
28#include <map>
29#include <string>
30
31AST_NAMESPACE_BEGIN
32
33
37class AST_AI_API SSEParser : public NetworkStreamReceiver
38{
39public:
40 explicit SSEParser(ChatEventHandler& handler);
41
42 // ── NetworkStreamReceiver 接口 ──
43
44 void onHeaders(int statusCode, const std::map<std::string, std::string>& headers) override;
45 errc_t onData(const char* data, size_t size) override;
46 void onComplete() override;
47 void onError(errc_t error) override;
48
49 // ── 状态 ──
50
52 bool hasError() const { return !error_.empty(); }
53
55 const std::string& errorMessage() const { return error_; }
56
57 // ── 累积结果 ──
58
60 JsonValue buildResult() const;
61
62private:
63 struct AccumulatedToolCall
64 {
65 std::string id;
66 std::string type;
67 std::string functionName;
68 std::string functionArguments;
69 };
70
71 void processBuffer();
72 void processEvent(const std::string& event);
73 void processToolCallDeltas(const JsonValue& toolCalls);
74
75 ChatEventHandler& handler_;
76 bool thoughtCompleted_{false}; // 是否已完成推理/思考内容
77 std::string buffer_;
78 std::string error_;
79
80 std::string id_;
81 std::string model_;
82 std::string accumulatedContent_;
83 std::string accumulatedReasoning_;
84 std::string finishReason_;
85 std::map<int, AccumulatedToolCall> toolCallsByIndex_;
86};
87
88
89AST_NAMESPACE_END
流式聊天事件处理器
定义 ChatEventHandler.hpp:42
JSON 值类
定义 JsonValue.hpp:47
流式数据接收器
定义 NetworkStreamReceiver.hpp:41
virtual void onHeaders(int statusCode, const std::map< std::string, std::string > &headers)
收到 HTTP 响应头(在 onData 之前触发一次)
定义 NetworkStreamReceiver.hpp:49
virtual void onComplete()
数据接收完毕(正常完成时触发)
定义 NetworkStreamReceiver.hpp:65
virtual errc_t onData(const char *data, size_t size)=0
收到一块响应体数据(可能被多次调用)
virtual void onError(errc_t error)
发生网络错误
定义 NetworkStreamReceiver.hpp:62
SSE 增量解析器(内部组件)
定义 SSEParser.hpp:38
bool hasError() const
是否发生了错误(HTTP 非 200 或网络错误)
定义 SSEParser.hpp:52
const std::string & errorMessage() const
获取错误描述
定义 SSEParser.hpp:55