Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoltan Erdos committed Sep 29, 2024
1 parent 3f15d06 commit 6822e1d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 64 deletions.
78 changes: 18 additions & 60 deletions packages/code/src/@/lib/chat-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,21 @@ ${code}
`;

export const extractCodeModification = (response: string): string[] => {
// console.log("No code modifications found in response");

const match = response.match(CODE_MODIFICATION_REGEX) || [];

console.log("No code modifications found in response");
// Filter out code blocks that don't contain any of the SEARCH_REPLACE

// they should also contain one of the SEARCH_REPLACE_MARKERS to have a match
const codeBlockMatches = response.match(/```[\s\S]*?```/g);
if (!codeBlockMatches) return match;

const myBlocks = codeBlockMatches.filter((block) => SEARCH_REPLACE_MARKERS.some((marker) => block.includes(marker)))
.map((myBlock) => {
const block = myBlock.trim().split("<<<<<<< SEARCH").join("=======").split(">>>>>>> REPLACE").join("=======")
.split("\n");
/// remove first and last line
block.shift();
block.pop();

const parts = block.map(x => x.trim()).filter(x => x).join("\n").split("=======").filter(part =>
part.trim().length > 0
);
// return parts.join("ccccccccc");

if (parts.length < 2) return ``;
if (parts.length === 2) {
Expand All @@ -62,10 +54,8 @@ export const loadMessages = (codeSpace: string): Message[] => {
const key = `chatMessages-${codeSpace}`;
const rawMessages = JSON.parse(localStorage.getItem(key) || "[]") as Message[];

// Filter out messages without a role
const validMessages = rawMessages.filter(m => !!m.role);

// Remove consecutive messages with the same role
const uniqueRoleMessages = validMessages.reduce((acc, current, index) => {
if (index === 0 || current.role !== validMessages[index - 1].role) {
acc.push(current);
Expand All @@ -76,57 +66,25 @@ export const loadMessages = (codeSpace: string): Message[] => {
return uniqueRoleMessages;
};

export const updateSearchReplace = (
{ oldCode, codeNow }: { oldCode: string; codeNow: string },
): string => {
let replacedCode = codeNow;

extractCodeModification(oldCode).map((mod: string) => mod.replace(/<<<<<<< SEARCH|>>>>>>> REPLACE/g, "")).map(
(mod: string) => mod.split("======="),
).map((mod: string[]) => {
const [search, replace] = mod;

const starterCode = replacedCode;

replacedCode = replacePreservingWhitespace(
replacedCode,
search.trim(),
replace.split("\n").slice(1).map((x) => x.trim()).filter((x) => x).join(
"\n",
),
);

const replacedFirst = replacedCode;
replacedCode = starterCode;

replacedCode = replacePreservingWhitespace(
replacedCode,
search.trim(),
replace.split("\n").slice(1).map((x) => x.trim()).filter((x) => x).join(
"\n",
) + "foo\n",
);
const replacedSecond = replacedCode;
replacedCode = replacedFirst;

if (replacedFirst === replacedSecond) {
return replacedFirst;
}
return starterCode;
});

// let tsxCodeBocks = codeNow.match(/```tsx([\s\S]*?)```/g);

// let newCode = "";
export const updateSearchReplace = (instructions: string, codeNow: string): string => {
try {
let replacedCode = codeNow;

// if (tsxCodeBocks) {
// [...tsxCodeBocks].map((block) => {
// const code = block.replace(/```tsx|```/g, "").trim();
// const codeSpace = code.split("\n")[0].trim().replace(/\.tsx$/, "").replace("//", "");
extractCodeModification(instructions).forEach((mod: string) => {
const [search, replace] = mod.replace(/<<<<<<< SEARCH|>>>>>>> REPLACE/g, "").split("=======");

// newCode += formatCodeAsSection(codeSpace, code) + "\n";
// });
// }
if (search && replace) {
replacedCode = replacePreservingWhitespace(
replacedCode,
search.trim(),
replace.trim(),
);
}
});

return replacedCode;
return replacedCode;
} catch (error) {
console.error("Error in updateSearchReplace:", error);
return codeNow;
}
};
13 changes: 11 additions & 2 deletions packages/code/src/@/lib/lazy-load-scripts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const loadedScripts: Set<string> = globalThis.loadedScripts || new Set<string>();
globalThis.loadedScripts = loadedScripts;
interface GlobalWithLoadedScripts {
loadedScripts?: Set<string>;
}

const globalWithLoadedScripts = globalThis as GlobalWithLoadedScripts;

const loadedScripts: Set<string> = globalWithLoadedScripts.loadedScripts || new Set<string>();
globalWithLoadedScripts.loadedScripts = loadedScripts;

export const lazyLoadScript = (scriptName: string): void => {
if (!loadedScripts.has(scriptName)) {
Expand All @@ -12,3 +18,6 @@ export const lazyLoadScript = (scriptName: string): void => {
}
}
};

// If you need to add other global properties, you can do it like this:
// (globalThis as any).someOtherGlobalProperty = 'value';
4 changes: 2 additions & 2 deletions packages/code/src/@/workers/chat-utils.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const updateSearchReplace = async (
console.log("instructions", instructions);
console.log("code", code);

const result = up({ codeNow: code, oldCode: instructions });
const resultBad = up({ codeNow: code, oldCode: instructions + "\nfooo\n" });
const result = up(instructions, code);
const resultBad = up(instructions + "\nfooo\n", code);
if (result !== resultBad) {
return code;
}
Expand Down

0 comments on commit 6822e1d

Please sign in to comment.