8
8
9
9
ToolCallParser::ToolCallParser ()
10
10
{
11
- m_possibleStartTags << ToolCallConstants::CodeInterpreterTag
12
- << ToolCallConstants::ThinkTag;
13
- m_possibleEndTags << ToolCallConstants::CodeInterpreterEndTag
14
- << ToolCallConstants::ThinkEndTag;
11
+ m_possibleStartTags << ToolCallConstants::CodeInterpreterTag. toUtf8 ()
12
+ << ToolCallConstants::ThinkTag. toUtf8 () ;
13
+ m_possibleEndTags << ToolCallConstants::CodeInterpreterEndTag. toUtf8 ()
14
+ << ToolCallConstants::ThinkEndTag. toUtf8 () ;
15
15
reset ();
16
16
}
17
17
@@ -22,7 +22,7 @@ void ToolCallParser::reset()
22
22
23
23
// These are global states maintained between update calls
24
24
m_buffers.clear ();
25
- m_buffers. append ( QString () );
25
+ m_buffers << QByteArray ( );
26
26
}
27
27
28
28
void ToolCallParser::resetSearchState ()
@@ -40,48 +40,48 @@ void ToolCallParser::resetSearchState()
40
40
m_endIndex = -1 ;
41
41
}
42
42
43
- bool ToolCallParser::isExpected (QChar c) const
43
+ bool ToolCallParser::isExpected (char c) const
44
44
{
45
45
return m_expected.isEmpty () || m_expected.contains (c);
46
46
}
47
47
48
- void ToolCallParser::setExpected (const QStringList &tags)
48
+ void ToolCallParser::setExpected (const QList<QByteArray> &tags)
49
49
{
50
50
m_expected.clear ();
51
- for (const QString &tag : tags) {
51
+ for (const auto &tag : tags) {
52
52
Q_ASSERT (tag.size () > m_expectedIndex);
53
53
m_expected << tag.at (m_expectedIndex);
54
54
}
55
55
}
56
56
57
- QString ToolCallParser::startTag () const
57
+ QByteArray ToolCallParser::startTag () const
58
58
{
59
59
if (m_currentTagIndex < 0 )
60
- return QString () ;
60
+ return {} ;
61
61
return m_possibleStartTags.at (m_currentTagIndex);
62
62
}
63
63
64
- QString ToolCallParser::endTag () const
64
+ QByteArray ToolCallParser::endTag () const
65
65
{
66
66
if (m_currentTagIndex < 0 )
67
- return QString () ;
67
+ return {} ;
68
68
return m_possibleEndTags.at (m_currentTagIndex);
69
69
}
70
70
71
- QString &ToolCallParser::currentBuffer ()
71
+ QByteArray &ToolCallParser::currentBuffer ()
72
72
{
73
73
return m_buffers.last ();
74
74
}
75
75
76
76
// This method is called with an arbitrary string and a current state. This method should take the
77
77
// current state into account and then parse through the update character by character to arrive at
78
78
// the new state.
79
- void ToolCallParser::update (const QString &update)
79
+ void ToolCallParser::update (const QByteArray &update)
80
80
{
81
81
currentBuffer ().append (update);
82
82
83
83
for (size_t i = currentBuffer ().size () - update.size (); i < currentBuffer ().size (); ++i) {
84
- const QChar c = currentBuffer ()[i];
84
+ const char c = currentBuffer ()[i];
85
85
const bool foundMatch = isExpected (c);
86
86
if (!foundMatch) {
87
87
resetSearchState ();
@@ -100,7 +100,7 @@ void ToolCallParser::update(const QString &update)
100
100
case ToolEnums::ParseState::InTagChoice:
101
101
{
102
102
for (int i = 0 ; i < m_possibleStartTags.size (); ++i) {
103
- const QString tag = m_possibleStartTags.at (i);
103
+ const auto & tag = m_possibleStartTags.at (i);
104
104
if (c == tag.at (1 )) m_currentTagIndex = i;
105
105
}
106
106
if (m_currentTagIndex >= 0 ) {
@@ -115,7 +115,7 @@ void ToolCallParser::update(const QString &update)
115
115
{
116
116
m_startTagBuffer.append (c);
117
117
118
- const QString startTag = this ->startTag ();
118
+ const auto startTag = this ->startTag ();
119
119
Q_ASSERT (!startTag.isEmpty ());
120
120
if (m_expectedIndex == startTag.size () - 1 ) {
121
121
m_expectedIndex = 0 ;
@@ -131,7 +131,7 @@ void ToolCallParser::update(const QString &update)
131
131
case ToolEnums::ParseState::Partial:
132
132
{
133
133
Q_ASSERT (m_currentTagIndex >= 0 );
134
- const QString endTag = this ->endTag ();
134
+ const auto endTag = this ->endTag ();
135
135
Q_ASSERT (!endTag.isEmpty ());
136
136
m_toolCall.append (c);
137
137
m_endTagBuffer.append (c);
@@ -159,26 +159,30 @@ bool ToolCallParser::splitIfPossible()
159
159
// The first split happens when we're in a partial state
160
160
if (m_buffers.size () < 2 && m_state == ToolEnums::ParseState::Partial) {
161
161
Q_ASSERT (m_startIndex >= 0 );
162
- const QString beforeToolCall = currentBuffer ().left (m_startIndex);
163
- const QString toolCall = currentBuffer ().mid (m_startIndex);
162
+ const auto beforeToolCall = currentBuffer ().left (m_startIndex);
163
+ const auto toolCall = currentBuffer ().mid (m_startIndex);
164
164
m_buffers = { beforeToolCall, toolCall };
165
165
return true ;
166
166
}
167
167
168
168
// The second split happens when we're in the complete state
169
169
if (m_buffers.size () < 3 && m_state == ToolEnums::ParseState::Complete) {
170
170
Q_ASSERT (m_endIndex >= 0 );
171
- const QString beforeToolCall = m_buffers.first ();
172
- const QString toolCall = currentBuffer ().left (m_endIndex);
173
- const QString afterToolCall = currentBuffer ().mid (m_endIndex);
171
+ const auto & beforeToolCall = m_buffers.first ();
172
+ const auto toolCall = currentBuffer ().left (m_endIndex);
173
+ const auto afterToolCall = currentBuffer ().mid (m_endIndex);
174
174
m_buffers = { beforeToolCall, toolCall, afterToolCall };
175
175
return true ;
176
176
}
177
177
178
178
return false ;
179
179
}
180
180
181
- const QVector<QString> & ToolCallParser::buffers () const
181
+ QStringList ToolCallParser::buffers () const
182
182
{
183
- return m_buffers;
183
+ QStringList result;
184
+ result.reserve (m_buffers.size ());
185
+ for (const auto &buffer : m_buffers)
186
+ result << QString::fromUtf8 (buffer);
187
+ return result;
184
188
}
0 commit comments