extractFirstJsonStringValue static method
Extracts the first JSON string value from a single-key object fragment,
e.g. { "thought": "Hello -> returns Hello (best-effort, supports partial).
This is used for Thinking delta-args where the payload is always a key/value
pair but we don't want to show { "key": " in the UI.
Implementation
static String? extractFirstJsonStringValue(String rawBuffer) {
try {
if (rawBuffer.trim().isEmpty) return rawBuffer;
final int firstKeyQuote = rawBuffer.indexOf('"');
if (firstKeyQuote < 0) return null;
final int colonIndex = rawBuffer.indexOf(':', firstKeyQuote + 1);
if (colonIndex < 0) return null;
final int firstValueQuote = rawBuffer.indexOf('"', colonIndex + 1);
if (firstValueQuote < 0) return null;
final StringBuffer value = StringBuffer();
bool escaped = false;
for (int i = firstValueQuote + 1; i < rawBuffer.length; i++) {
final String ch = rawBuffer[i];
if (escaped) {
switch (ch) {
case 'n':
value.write('\n');
break;
case 'r':
value.write('\r');
break;
case 't':
value.write('\t');
break;
case '"':
value.write('"');
break;
case '\\':
value.write('\\');
break;
default:
value.write(ch);
break;
}
escaped = false;
continue;
}
if (ch == '\\') {
escaped = true;
continue;
}
if (ch == '"') {
break;
}
value.write(ch);
}
final String result = value.toString();
return result.trim().isEmpty ? rawBuffer : result;
} catch (e) {
return rawBuffer;
}
}