generated from theonlyamos/spiral
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
135 lines (121 loc) · 4.71 KB
/
test.js
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
function xmlToJson(xml) {
let obj = {};
// const responseContent = xml.match(/<response>(.*)<\/response>/s)[1];
let tags = xml.match(/<(.*?)>/g);
console.log(tags)
const mainKeys = ['<observation>', '<mindspace>', '<thought>', '<type>', '<result>', '<tool_calls>', '<artifacts>'];
let isSubLevel = false
if (tags) {
tags.forEach(tag => {
let tagName = tag.match(/<(.*)>/)[1];
if (!tagName.startsWith('/')) {
let regex = xml.match(new RegExp(`<${tagName}>(.*?)<\/${tagName}>`, 's'));
console.log(tagName)
let content = regex[1];
// if (mainKeys.includes(tag) && !isSubLevel) {
// isSubLevel = false
// }
// else {
// isSubLevel = true
// }
if (content.includes('<')) {
obj[tagName] = xmlToJson(content);
} else {
obj[tagName] = content.trim();
}
}
});
}
return obj;
}
// Test the function
let xml = `
<observation>The user asked for a Python script to calculate the factorial of a number.</observation>
<mindspace>
Mathematical: Factorial operation, recursive function
Programming: Python syntax, function definition, conditional statements
Educational: Explaining the concept of factorial
Practical: Use cases for factorial calculations
</mindspace>
<thought>Step 1) A factorial calculation can be implemented using a recursive function in Python.
Step 2) We should create a function that handles both the base case and the recursive case.
Step 3) Let's create an artifact with the Python script for calculating factorials.</thought>
<type>result</type>
<result>I've created a Python script that calculates the factorial of a given number using a recursive function. You can find the script in the artifact below.</result>
<artifacts>
<artifact>
<language>python</language>
<identifier>factorial-script</identifier>
<type>application/vnd.ant.code</type>
<title>Python Factorial Calculator</title>
<content>
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Example usage
number = 5
result = factorial(number)
print(f"The factorial of {number} is {result}")
</content>
</artifact>
</artifacts>
`;
console.log(xmlToJson(xml));
// function convertXmlToJson(xmlText) {
// const parser = new DOMParser();
// const xmlDoc = parser.parseFromString(xmlText, "text/xml");
// function parseNode(node) {
// if (node.nodeType === Node.TEXT_NODE) {
// return node.nodeValue.trim();
// }
// const jsonObject = {};
// if (node.attributes && node.attributes.length > 0) {
// for (let i = 0; i < node.attributes.length; i++) {
// jsonObject[node.attributes[i].name] = node.attributes[i].value;
// }
// }
// if (node.childNodes && node.childNodes.length > 0) {
// for (let i = 0; i < node.childNodes.length; i++) {
// const childNode = node.childNodes[i];
// const childNodeName = childNode.nodeName;
// if (jsonObject[childNodeName] === undefined) {
// jsonObject[childNodeName] = parseNode(childNode);
// } else if (Array.isArray(jsonObject[childNodeName])) {
// jsonObject[childNodeName].push(parseNode(childNode));
// } else {
// jsonObject[childNodeName] = [jsonObject[childNodeName], parseNode(childNode)];
// }
// }
// }
// return jsonObject;
// }
// return parseNode(xmlDoc.documentElement);
// }
// // Example usage:
// const xmlResponse = `
// <response>
// <observation>The user asked to search for information about AI on Wikipedia.</observation>
// <mindspace>
// Technological: Machine learning, AI applications
// Scientific: Computer science, algorithms
// Philosophical: Intelligence, human-AI interaction
// Historical: AI development, research milestones
// </mindspace>
// <thought>Step 1) To search Wikipedia, use the 'search_wikipedia' tool.
// Step 2) The relevant argument is the search query: 'artificial intelligence'.</thought>
// <type>tool_calls</type>
// <tool_calls>
// <tool>
// <name>Wikipedia</name>
// <arguments>
// <query>artificial intelligence</query>
// </arguments>
// </tool>
// </tool_calls>
// <artifacts></artifacts>
//
// `;
// const jsonObject = convertXmlToJson(xmlResponse);
// console.log(JSON.stringify(jsonObject, null, 2));