Datasets:

Modalities:
Text
Formats:
text
Size:
< 1K
ArXiv:
Libraries:
Datasets
File size: 8,146 Bytes
2517be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { AgenticSectionType } from '$lib/enums';
import { AGENTIC_TAGS, AGENTIC_REGEX, REASONING_TAGS, TRIM_NEWLINES_REGEX } from '$lib/constants';

/**
 * Represents a parsed section of agentic content
 */
export interface AgenticSection {
	type: AgenticSectionType;
	content: string;
	toolName?: string;
	toolArgs?: string;
	toolResult?: string;
}

/**
 * Represents a segment of content that may contain reasoning blocks
 */
type ReasoningSegment = {
	type:
		| AgenticSectionType.TEXT
		| AgenticSectionType.REASONING
		| AgenticSectionType.REASONING_PENDING;
	content: string;
};

/**
 * Parses agentic content into structured sections
 *
 * Main parsing function that processes content containing:
 * - Tool calls (completed, pending, or streaming)
 * - Reasoning blocks (completed or streaming)
 * - Regular text content
 *
 * The parser handles chronological display of agentic flow output, maintaining
 * the order of operations and properly identifying different states of tool calls
 * and reasoning blocks during streaming.
 *
 * @param rawContent - The raw content string to parse
 * @returns Array of structured agentic sections ready for display
 *
 * @example
 * ```typescript
 * const content = "Some text <<<AGENTIC_TOOL_CALL>>>tool_name...";
 * const sections = parseAgenticContent(content);
 * // Returns: [{ type: 'text', content: 'Some text' }, { type: 'tool_call_streaming', ... }]
 * ```
 */
export function parseAgenticContent(rawContent: string): AgenticSection[] {
	if (!rawContent) return [];

	const segments = splitReasoningSegments(rawContent);
	const sections: AgenticSection[] = [];

	for (const segment of segments) {
		if (segment.type === AgenticSectionType.TEXT) {
			sections.push(...parseToolCallContent(segment.content));
			continue;
		}

		if (segment.type === AgenticSectionType.REASONING) {
			if (segment.content.trim()) {
				sections.push({ type: AgenticSectionType.REASONING, content: segment.content });
			}
			continue;
		}

		sections.push({
			type: AgenticSectionType.REASONING_PENDING,
			content: segment.content
		});
	}

	return sections;
}

/**
 * Parses content containing tool call markers
 *
 * Identifies and extracts tool calls from content, handling:
 * - Completed tool calls with name, arguments, and results
 * - Pending tool calls (execution in progress)
 * - Streaming tool calls (arguments being received)
 * - Early-stage tool calls (just started)
 *
 * @param rawContent - The raw content string to parse
 * @returns Array of agentic sections representing tool calls and text
 */
function parseToolCallContent(rawContent: string): AgenticSection[] {
	if (!rawContent) return [];

	const sections: AgenticSection[] = [];

	const completedToolCallRegex = new RegExp(AGENTIC_REGEX.COMPLETED_TOOL_CALL.source, 'g');

	let lastIndex = 0;
	let match;

	while ((match = completedToolCallRegex.exec(rawContent)) !== null) {
		if (match.index > lastIndex) {
			const textBefore = rawContent.slice(lastIndex, match.index).trim();
			if (textBefore) {
				sections.push({ type: AgenticSectionType.TEXT, content: textBefore });
			}
		}

		const toolName = match[1];
		const toolArgs = match[2];
		const toolResult = match[3].replace(TRIM_NEWLINES_REGEX, '');

		sections.push({
			type: AgenticSectionType.TOOL_CALL,
			content: toolResult,
			toolName,
			toolArgs,
			toolResult
		});

		lastIndex = match.index + match[0].length;
	}

	const remainingContent = rawContent.slice(lastIndex);

	const pendingMatch = remainingContent.match(AGENTIC_REGEX.PENDING_TOOL_CALL);
	const partialWithNameMatch = remainingContent.match(AGENTIC_REGEX.PARTIAL_WITH_NAME);
	const earlyMatch = remainingContent.match(AGENTIC_REGEX.EARLY_MATCH);

	if (pendingMatch) {
		const pendingIndex = remainingContent.indexOf(AGENTIC_TAGS.TOOL_CALL_START);

		if (pendingIndex > 0) {
			const textBefore = remainingContent.slice(0, pendingIndex).trim();

			if (textBefore) {
				sections.push({ type: AgenticSectionType.TEXT, content: textBefore });
			}
		}

		const toolName = pendingMatch[1];
		const toolArgs = pendingMatch[2];
		const streamingResult = (pendingMatch[3] || '').replace(TRIM_NEWLINES_REGEX, '');

		sections.push({
			type: AgenticSectionType.TOOL_CALL_PENDING,
			content: streamingResult,
			toolName,
			toolArgs,
			toolResult: streamingResult || undefined
		});
	} else if (partialWithNameMatch) {
		const pendingIndex = remainingContent.indexOf(AGENTIC_TAGS.TOOL_CALL_START);

		if (pendingIndex > 0) {
			const textBefore = remainingContent.slice(0, pendingIndex).trim();
			if (textBefore) {
				sections.push({ type: AgenticSectionType.TEXT, content: textBefore });
			}
		}

		const partialArgs = partialWithNameMatch[2] || '';

		sections.push({
			type: AgenticSectionType.TOOL_CALL_STREAMING,
			content: '',
			toolName: partialWithNameMatch[1],
			toolArgs: partialArgs || undefined,
			toolResult: undefined
		});
	} else if (earlyMatch) {
		const pendingIndex = remainingContent.indexOf(AGENTIC_TAGS.TOOL_CALL_START);

		if (pendingIndex > 0) {
			const textBefore = remainingContent.slice(0, pendingIndex).trim();
			if (textBefore) {
				sections.push({ type: AgenticSectionType.TEXT, content: textBefore });
			}
		}

		const nameMatch = earlyMatch[1]?.match(AGENTIC_REGEX.TOOL_NAME_EXTRACT);

		sections.push({
			type: AgenticSectionType.TOOL_CALL_STREAMING,
			content: '',
			toolName: nameMatch?.[1],
			toolArgs: undefined,
			toolResult: undefined
		});
	} else if (lastIndex < rawContent.length) {
		let remainingText = rawContent.slice(lastIndex).trim();

		const partialMarkerMatch = remainingText.match(AGENTIC_REGEX.PARTIAL_MARKER);

		if (partialMarkerMatch) {
			remainingText = remainingText.slice(0, partialMarkerMatch.index).trim();
		}

		if (remainingText) {
			sections.push({ type: AgenticSectionType.TEXT, content: remainingText });
		}
	}

	if (sections.length === 0 && rawContent.trim()) {
		sections.push({ type: AgenticSectionType.TEXT, content: rawContent });
	}

	return sections;
}

/**
 * Strips partial marker from text content
 *
 * Removes incomplete agentic markers (e.g., "<<<", "<<<AGENTIC") that may appear
 * at the end of streaming content.
 *
 * @param text - The text content to process
 * @returns Text with partial markers removed
 */
function stripPartialMarker(text: string): string {
	const partialMarkerMatch = text.match(AGENTIC_REGEX.PARTIAL_MARKER);

	if (partialMarkerMatch) {
		return text.slice(0, partialMarkerMatch.index).trim();
	}

	return text;
}

/**
 * Splits raw content into segments based on reasoning blocks
 *
 * Identifies and extracts reasoning content wrapped in REASONING_TAGS.START/END markers,
 * separating it from regular text content. Handles both complete and incomplete
 * (streaming) reasoning blocks.
 *
 * @param rawContent - The raw content string to parse
 * @returns Array of reasoning segments with their types and content
 */
function splitReasoningSegments(rawContent: string): ReasoningSegment[] {
	if (!rawContent) return [];

	const segments: ReasoningSegment[] = [];
	let cursor = 0;

	while (cursor < rawContent.length) {
		const startIndex = rawContent.indexOf(REASONING_TAGS.START, cursor);

		if (startIndex === -1) {
			const remainingText = rawContent.slice(cursor);

			if (remainingText) {
				segments.push({ type: AgenticSectionType.TEXT, content: remainingText });
			}

			break;
		}

		if (startIndex > cursor) {
			const textBefore = rawContent.slice(cursor, startIndex);

			if (textBefore) {
				segments.push({ type: AgenticSectionType.TEXT, content: textBefore });
			}
		}

		const contentStart = startIndex + REASONING_TAGS.START.length;
		const endIndex = rawContent.indexOf(REASONING_TAGS.END, contentStart);

		if (endIndex === -1) {
			const pendingContent = rawContent.slice(contentStart);

			segments.push({
				type: AgenticSectionType.REASONING_PENDING,
				content: stripPartialMarker(pendingContent)
			});

			break;
		}

		const reasoningContent = rawContent.slice(contentStart, endIndex);
		segments.push({ type: AgenticSectionType.REASONING, content: reasoningContent });
		cursor = endIndex + REASONING_TAGS.END.length;
	}

	return segments;
}