retagUpdate = async (codeWithSnippetDelimited, updatedCodeWithoutDelimiters, delimiter) => {
console.log(codeWithSnippetDelimited, updatedCodeWithoutDelimiters, delimiter)
let gptOut;
try {
const gptOutCompletion = await openai.chat.completions.create({
messages: [
{
role: "system",
content: "You are a helpful assistant designed to output JSON.",
},
{ role: "user", content: prompt_breakdown11({codeWithSnippetDelimited,
updatedCodeWithSnippetDelimited: updatedCodeWithoutDelimiters,
delimiter })}
],
model: "gpt-4-0125-preview",
response_format: { type: "json_object" },
});
console.log(gptOutCompletion)
gptOut = gptOutCompletion.choices[0].message.content
console.log(gptOut)
} catch (e) {
return {error: e, errorType: 'model'}
}
let gptRetaggingJSON;
try {
gptRetaggingJSON = JSON.parse(gptOut)
console.log(gptRetaggingJSON)
} catch (e) {
return {error: e, errorType: 'JSON parse', gptOut}
}
const computeUpdatedCodeWithSnippetRetagged = ({code, snippet, lineStart, lineEnd, nthOccurrence, delimiterStart, delimiterEnd}) => {
let sectionString = code.split('\n').slice(lineStart - 1, lineEnd).join('\n')
let lenUpToSection = code.split('\n').slice(0, lineStart - 1).map(s=>s + '\n').join('').length
let snippetIdxInSection = findStartAndEndNormalized(sectionString, snippet, nthOccurrence)
if (snippetIdxInSection.start === -1) {
lineStart = Math.max(0, lineStart - 1)
lineEnd = Math.min(lineEnd + 1, code.split('\n').length)
sectionString = code.split('\n').slice(lineStart - 1, lineEnd).join('\n')
lenUpToSection = code.split('\n').slice(0, lineStart - 1).map(s=>s + '\n').join('').length
snippetIdxInSection = findStartAndEndNormalized(sectionString, snippet, nthOccurrence)
}
const leftIdx = lenUpToSection + snippetIdxInSection.start
const rightIdx = leftIdx + snippetIdxInSection.end - snippetIdxInSection.start
return code.slice(0, leftIdx) + delimiterStart + code.slice(leftIdx, rightIdx) + delimiterEnd + code.slice(rightIdx, code.length)
}
try {
const out = computeUpdatedCodeWithSnippetRetagged({
code:updatedCodeWithoutDelimiters,
snippet:gptRetaggingJSON[1],
lineStart:gptRetaggingJSON[2],
lineEnd:gptRetaggingJSON[3],
nthOccurrence:gptRetaggingJSON[4],
delimiterStart:delimiter,
delimiterEnd:delimiter})
console.log(out)
return {gptRetaggingJSON, out}
} catch (e) {
return {error: e, errorType: 'snippet matching', gptOut, gptRetaggingJSON}
}
}