Skip to content

Commit ccad00c

Browse files
committed
support weird json
Signed-off-by: Merwane Hamadi <[email protected]>
1 parent 3c8fab4 commit ccad00c

18 files changed

+384
-227
lines changed

src/buildOrUpdateStack/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default async function buildOrUpdateStack(
3131
inputString,
3232
briefSkeleton,
3333
functionAndOutputSkeleton,
34+
inputValues
3435
} = await buildStack({
3536
inputJSON: inputJSON,
3637
outputJSON: outputJSON,
@@ -109,9 +110,9 @@ export default async function buildOrUpdateStack(
109110
} else {
110111
console.log(`function already exists`);
111112
}
113+
console.log(`inputString in buildOrUpdateStack:`, inputString);
112114

113-
const inputStringToArgument = extractInputString(stackSnippet);
114-
const injectedFunction = `await ${methodName}(${inputStringToArgument})`;
115+
const injectedFunction = `await ${methodName}(${inputValues})`;
115116

116117
console.log(`stackSnippet in buildOrUpdateStack:`, stackSnippet);
117118
// console.log(`document in buildOrUpdateStack:`, document);

src/stack/buildStack/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface OutputType {
1919
inputString: string;
2020
briefSkeleton: string;
2121
functionAndOutputSkeleton: string;
22+
inputValues: string;
2223
}
2324

2425
interface InputType {
@@ -36,11 +37,11 @@ export default async function buildStack({
3637
console.log('brief after extraction');
3738
console.log(brief);
3839

39-
const { input: inputString, outExample } = jsonify(stackSnippet);
40+
const { input: inputString, inputValues} = jsonify(stackSnippet);
4041
console.log('input after JSONIFY');
4142
console.log(inputString);
42-
console.log('output after JSONIFY');
43-
console.log(outExample);
43+
// console.log('output after JSONIFY');
44+
// console.log(outExample);
4445
// Create a signature from the extracted parameters
4546
const signature = generateSignature(
4647
brief,
@@ -114,5 +115,6 @@ export default async function buildStack({
114115
inputString,
115116
briefSkeleton,
116117
functionAndOutputSkeleton,
118+
inputValues
117119
};
118120
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import extractInputFromObject from './extractInputFromObject';
2+
import jsonifyString from './jsonify';
3+
4+
test('identifies and extracts placeholders from JSON-like string', () => {
5+
// Sample JSON-like string with placeholders
6+
7+
const inputString = `{
8+
input: { x, y },
9+
outExample: { check: 0 }
10+
}`;
11+
12+
const result = extractInputFromObject(inputString);
13+
14+
const expectedOutput = `{ x, y }`;
15+
console.log(result);
16+
17+
// Check if the result matches the expected output
18+
expect(result).toEqual(expectedOutput);
19+
});
20+
21+
test('identifies and extracts placeholders from JSON-like string', () => {
22+
// Sample JSON-like string with placeholders
23+
24+
const inputString = `{
25+
input: { x, y, "z": z },
26+
outExample: { check: 0 }
27+
}`;
28+
29+
const result = extractInputFromObject(inputString);
30+
31+
const expectedOutput = `{ x, y, "z": z }`;
32+
console.log(result);
33+
34+
// Check if the result matches the expected output
35+
expect(result).toEqual(expectedOutput);
36+
});
37+
38+
test('identifies and extracts placeholders from JSON-like string', () => {
39+
// Sample JSON-like string with placeholders
40+
41+
const inputString = `{"input": { x, "y": y},
42+
"outExample": {"check": 0}
43+
}`;
44+
45+
const result = extractInputFromObject(inputString);
46+
47+
const expectedOutput = `{ x, "y": y}`;
48+
console.log(result);
49+
50+
// Check if the result matches the expected output
51+
expect(result).toEqual(expectedOutput);
52+
});
53+
54+
test('identifies and extracts placeholders from JSON-like string', () => {
55+
// Sample JSON-like string with placeholders
56+
57+
const inputString = `{
58+
"input": {
59+
"dictionary": {
60+
"Use the chatCompletion endpoint from openai to return a response": "callOpenAI",
61+
"Find a good name for this method.": "pickMethodName"
62+
},
63+
"textCode": isolatedFunction
64+
},
65+
"outExample": {
66+
"methodName": "callOpenAI"
67+
}
68+
}`;
69+
70+
const result = extractInputFromObject(inputString);
71+
72+
const expectedOutput =
73+
`{
74+
"dictionary": {
75+
"Use the chatCompletion endpoint from openai to return a response": "callOpenAI",
76+
"Find a good name for this method.": "pickMethodName"
77+
},
78+
"textCode": isolatedFunction
79+
}`;
80+
console.log(result);
81+
82+
// Check if the result matches the expected output
83+
expect(result).toEqual(expectedOutput);
84+
});
85+
86+
test('identifies and extracts placeholders from JSON-like string', () => {
87+
// Sample JSON-like string with placeholders
88+
89+
const inputString = `{
90+
input: "this is a brief",
91+
outExample: {"ok": true,"test": "other"
92+
}
93+
}`;
94+
95+
const result = extractInputFromObject(inputString);
96+
97+
const expectedOutput = `"this is a brief"`;
98+
console.log(result);
99+
100+
// Check if the result matches the expected output
101+
expect(result).toEqual(expectedOutput);
102+
});
103+
104+
test('identifies and extracts placeholders from JSON-like string', () => {
105+
// Sample JSON-like string with placeholders
106+
107+
const inputString = `{"input": ['this is an example', 'this is an example']
108+
}
109+
110+
`;
111+
112+
const result = extractInputFromObject(inputString);
113+
114+
const expectedOutput = `['this is an example', 'this is an example']`;
115+
console.log(result);
116+
117+
// Check if the result matches the expected output
118+
expect(result).toEqual(expectedOutput);
119+
});
120+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export default function extractInputFromObject(inputString: string): string {
2+
let start_index = inputString.indexOf('"input":') + 8;
3+
if (start_index === 7) {
4+
start_index = inputString.indexOf('input:') + 6;
5+
}
6+
7+
while (inputString[start_index] === ' ') {
8+
start_index++;
9+
}
10+
11+
if (inputString[start_index] !== '{' && inputString[start_index] !== '[') {
12+
let end_index = start_index;
13+
let inQuotes = inputString[start_index] === '"';
14+
15+
for (let i = start_index + 1; i < inputString.length; i++) {
16+
if (inQuotes) {
17+
if (inputString[i] === '"' && inputString[i - 1] !== '\\') {
18+
end_index = i + 1;
19+
break;
20+
}
21+
} else {
22+
if (inputString[i] === ',' || inputString[i] === ' ' || inputString[i] === '\n') {
23+
end_index = i;
24+
break;
25+
}
26+
}
27+
}
28+
return inputString.substring(start_index, end_index).trim();
29+
}
30+
31+
let brace_count = 0;
32+
let bracket_count = 0;
33+
let end_index = start_index;
34+
35+
for (let i = start_index; i < inputString.length; i++) {
36+
const char = inputString[i];
37+
if (char === '{' || char === '[') {
38+
if (char === '{') {
39+
brace_count++;
40+
} else {
41+
bracket_count++;
42+
}
43+
} else if (char === '}' || char === ']') {
44+
if (char === '}') {
45+
brace_count--;
46+
} else {
47+
bracket_count--;
48+
}
49+
50+
if (brace_count === 0 && bracket_count === 0) {
51+
end_index = i + 1;
52+
break;
53+
}
54+
}
55+
}
56+
57+
return inputString.substring(start_index, end_index).trim();
58+
}

src/stack/jsonify/extractInputString.test.ts

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)