88
99ToolCallParser::ToolCallParser ()
1010{
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 () ;
1515 reset ();
1616}
1717
@@ -22,7 +22,7 @@ void ToolCallParser::reset()
2222
2323 // These are global states maintained between update calls
2424 m_buffers.clear ();
25- m_buffers. append ( QString () );
25+ m_buffers << QByteArray ( );
2626}
2727
2828void ToolCallParser::resetSearchState ()
@@ -40,48 +40,48 @@ void ToolCallParser::resetSearchState()
4040 m_endIndex = -1 ;
4141}
4242
43- bool ToolCallParser::isExpected (QChar c) const
43+ bool ToolCallParser::isExpected (char c) const
4444{
4545 return m_expected.isEmpty () || m_expected.contains (c);
4646}
4747
48- void ToolCallParser::setExpected (const QStringList &tags)
48+ void ToolCallParser::setExpected (const QList<QByteArray> &tags)
4949{
5050 m_expected.clear ();
51- for (const QString &tag : tags) {
51+ for (const auto &tag : tags) {
5252 Q_ASSERT (tag.size () > m_expectedIndex);
5353 m_expected << tag.at (m_expectedIndex);
5454 }
5555}
5656
57- QString ToolCallParser::startTag () const
57+ QByteArray ToolCallParser::startTag () const
5858{
5959 if (m_currentTagIndex < 0 )
60- return QString () ;
60+ return {} ;
6161 return m_possibleStartTags.at (m_currentTagIndex);
6262}
6363
64- QString ToolCallParser::endTag () const
64+ QByteArray ToolCallParser::endTag () const
6565{
6666 if (m_currentTagIndex < 0 )
67- return QString () ;
67+ return {} ;
6868 return m_possibleEndTags.at (m_currentTagIndex);
6969}
7070
71- QString &ToolCallParser::currentBuffer ()
71+ QByteArray &ToolCallParser::currentBuffer ()
7272{
7373 return m_buffers.last ();
7474}
7575
7676// This method is called with an arbitrary string and a current state. This method should take the
7777// current state into account and then parse through the update character by character to arrive at
7878// the new state.
79- void ToolCallParser::update (const QString &update)
79+ void ToolCallParser::update (const QByteArray &update)
8080{
8181 currentBuffer ().append (update);
8282
8383 for (size_t i = currentBuffer ().size () - update.size (); i < currentBuffer ().size (); ++i) {
84- const QChar c = currentBuffer ()[i];
84+ const char c = currentBuffer ()[i];
8585 const bool foundMatch = isExpected (c);
8686 if (!foundMatch) {
8787 resetSearchState ();
@@ -100,7 +100,7 @@ void ToolCallParser::update(const QString &update)
100100 case ToolEnums::ParseState::InTagChoice:
101101 {
102102 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);
104104 if (c == tag.at (1 )) m_currentTagIndex = i;
105105 }
106106 if (m_currentTagIndex >= 0 ) {
@@ -115,7 +115,7 @@ void ToolCallParser::update(const QString &update)
115115 {
116116 m_startTagBuffer.append (c);
117117
118- const QString startTag = this ->startTag ();
118+ const auto startTag = this ->startTag ();
119119 Q_ASSERT (!startTag.isEmpty ());
120120 if (m_expectedIndex == startTag.size () - 1 ) {
121121 m_expectedIndex = 0 ;
@@ -131,7 +131,7 @@ void ToolCallParser::update(const QString &update)
131131 case ToolEnums::ParseState::Partial:
132132 {
133133 Q_ASSERT (m_currentTagIndex >= 0 );
134- const QString endTag = this ->endTag ();
134+ const auto endTag = this ->endTag ();
135135 Q_ASSERT (!endTag.isEmpty ());
136136 m_toolCall.append (c);
137137 m_endTagBuffer.append (c);
@@ -159,26 +159,30 @@ bool ToolCallParser::splitIfPossible()
159159 // The first split happens when we're in a partial state
160160 if (m_buffers.size () < 2 && m_state == ToolEnums::ParseState::Partial) {
161161 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);
164164 m_buffers = { beforeToolCall, toolCall };
165165 return true ;
166166 }
167167
168168 // The second split happens when we're in the complete state
169169 if (m_buffers.size () < 3 && m_state == ToolEnums::ParseState::Complete) {
170170 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);
174174 m_buffers = { beforeToolCall, toolCall, afterToolCall };
175175 return true ;
176176 }
177177
178178 return false ;
179179}
180180
181- const QVector<QString> & ToolCallParser::buffers () const
181+ QStringList ToolCallParser::buffers () const
182182{
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;
184188}
0 commit comments