Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions apps/sim/executor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class StreamingResponseFormatProcessor {
): ReadableStream {
let buffer = ''
let hasProcessedComplete = false
let endsWithBracket = false

const self = this

Expand All @@ -73,7 +74,12 @@ export class StreamingResponseFormatProcessor {
const chunk = decoder.decode(value, { stream: true })
buffer += chunk

if (!hasProcessedComplete) {
// Only text ending in `}` or `]` can parse to an object, so skip re-parsing
// the whole buffer on every other token: that made each stream quadratic.
const trimmedChunk = chunk.trimEnd()
if (trimmedChunk) endsWithBracket = /[}\]]$/.test(trimmedChunk)

if (!hasProcessedComplete && endsWithBracket) {
const processedChunk = self.processStreamingChunk(buffer, selectedFields)

if (processedChunk) {
Expand Down Expand Up @@ -114,32 +120,6 @@ export class StreamingResponseFormatProcessor {
}
} catch {}

const openBraces = (buffer.match(/\{/g) || []).length
const closeBraces = (buffer.match(/\}/g) || []).length

if (openBraces > 0 && openBraces === closeBraces) {
try {
const parsed = JSON.parse(buffer.trim())
if (typeof parsed === 'object' && parsed !== null) {
const results: string[] = []
for (const field of selectedFields) {
if (field in parsed) {
const value = parsed[field]
const formattedValue = typeof value === 'string' ? value : JSON.stringify(value)
results.push(formattedValue)
}
}

if (results.length > 0) {
const result = results.join('\n')
return result
}

return null
}
} catch {}
}

return null
}

Expand Down
Loading