fix: detect and remove markdown block syntax that llms sometimes hallucinate for file actions (#886)
Browse files
app/lib/runtime/message-parser.ts
CHANGED
@@ -52,6 +52,17 @@ interface MessageState {
|
|
52 |
actionId: number;
|
53 |
}
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
export class StreamingMessageParser {
|
56 |
#messages = new Map<string, MessageState>();
|
57 |
|
@@ -95,6 +106,12 @@ export class StreamingMessageParser {
|
|
95 |
let content = currentAction.content.trim();
|
96 |
|
97 |
if ('type' in currentAction && currentAction.type === 'file') {
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
content += '\n';
|
99 |
}
|
100 |
|
@@ -120,7 +137,11 @@ export class StreamingMessageParser {
|
|
120 |
i = closeIndex + ARTIFACT_ACTION_TAG_CLOSE.length;
|
121 |
} else {
|
122 |
if ('type' in currentAction && currentAction.type === 'file') {
|
123 |
-
|
|
|
|
|
|
|
|
|
124 |
|
125 |
this._options.callbacks?.onActionStream?.({
|
126 |
artifactId: currentArtifact.id,
|
|
|
52 |
actionId: number;
|
53 |
}
|
54 |
|
55 |
+
function cleanoutMarkdownSyntax(content: string) {
|
56 |
+
const codeBlockRegex = /^\s*```\w*\n([\s\S]*?)\n\s*```\s*$/;
|
57 |
+
const match = content.match(codeBlockRegex);
|
58 |
+
console.log('matching', !!match, content);
|
59 |
+
|
60 |
+
if (match) {
|
61 |
+
return match[1]; // Remove common leading 4-space indent
|
62 |
+
} else {
|
63 |
+
return content;
|
64 |
+
}
|
65 |
+
}
|
66 |
export class StreamingMessageParser {
|
67 |
#messages = new Map<string, MessageState>();
|
68 |
|
|
|
106 |
let content = currentAction.content.trim();
|
107 |
|
108 |
if ('type' in currentAction && currentAction.type === 'file') {
|
109 |
+
// Remove markdown code block syntax if present and file is not markdown
|
110 |
+
if (!currentAction.filePath.endsWith('.md')) {
|
111 |
+
content = cleanoutMarkdownSyntax(content);
|
112 |
+
console.log('content after cleanup', content);
|
113 |
+
}
|
114 |
+
|
115 |
content += '\n';
|
116 |
}
|
117 |
|
|
|
137 |
i = closeIndex + ARTIFACT_ACTION_TAG_CLOSE.length;
|
138 |
} else {
|
139 |
if ('type' in currentAction && currentAction.type === 'file') {
|
140 |
+
let content = input.slice(i);
|
141 |
+
|
142 |
+
if (!currentAction.filePath.endsWith('.md')) {
|
143 |
+
content = cleanoutMarkdownSyntax(content);
|
144 |
+
}
|
145 |
|
146 |
this._options.callbacks?.onActionStream?.({
|
147 |
artifactId: currentArtifact.id,
|