Skip to content

Commit c5f00e4

Browse files
fix: harden JSON parser
1 parent d643392 commit c5f00e4

File tree

1 file changed

+7
-4
lines changed
  • templates/components/engines/typescript/agent/tools

1 file changed

+7
-4
lines changed

templates/components/engines/typescript/agent/tools/artifact.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 FoundryLabs, Inc.
2+
* Copyright 2023 FoundryLabs, Inc., LlamaIndex Inc.
33
* Portions of this file are copied from the e2b project (https://github.com/e2b-dev/ai-artifacts)
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -149,9 +149,12 @@ export class ArtifactTool implements BaseTool<ArtifactParameter> {
149149
];
150150
try {
151151
const response = await Settings.llm.chat({ messages });
152-
const artifact = JSON.parse(
153-
response.message.content.toString(),
154-
) as Artifact;
152+
const content = response.message.content.toString();
153+
const jsonContent = content
154+
.replace(/^```json\s*|\s*```$/g, "")
155+
.replace(/^`+|`+$/g, "")
156+
.trim();
157+
const artifact = JSON.parse(jsonContent) as Artifact;
155158
return artifact;
156159
} catch (error) {
157160
console.log("Failed to generate artifact", error);

0 commit comments

Comments
 (0)