Skip to content

Commit

Permalink
Merge pull request #41 from 10clouds/code-refactor
Browse files Browse the repository at this point in the history
code refactor
  • Loading branch information
maciejt10c authored Oct 16, 2023
2 parents c9f5bd6 + ebe6872 commit ca43275
Show file tree
Hide file tree
Showing 81 changed files with 1,096 additions and 1,140 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z } from 'zod';

import { createFullPromptFromSections } from '../../src/gpt/createFullPromptFromSections';
import { gptExecute } from '../../src/gpt/gptExecute';
import { GPTMode } from '../../src/gpt/types';
import { createFullPromptFromSections } from '../../src/gpt/utils/createFullPromptFromSections';
import { Fix, SolutionWithMeta } from '../../src/stepEvolve/FitnessFunction';
import { getRandomElement } from '../../src/utils/random/getRandomElement';
import { shuffleArray } from '../../src/utils/random/shuffleArray';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z } from 'zod';

import { createFullPromptFromSections } from '../../../src/gpt/createFullPromptFromSections';
import { gptExecute } from '../../../src/gpt/gptExecute';
import { GPTMode } from '../../../src/gpt/types';
import { createFullPromptFromSections } from '../../../src/gpt/utils/createFullPromptFromSections';
import { SolutionWithMeta } from '../../../src/stepEvolve/FitnessFunction';
import { TaskDefinition } from '../TaskDefinition';

Expand Down
2 changes: 1 addition & 1 deletion examples/runLLMCustomTaskWithStepEvolve/rateSolution.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z } from 'zod';

import { createFullPromptFromSections } from '../../src/gpt/createFullPromptFromSections';
import { gptExecute } from '../../src/gpt/gptExecute';
import { GPTMode } from '../../src/gpt/types';
import { createFullPromptFromSections } from '../../src/gpt/utils/createFullPromptFromSections';
import { SolutionWithMeta } from '../../src/stepEvolve/FitnessFunction';
import { shuffleArray } from '../../src/utils/random/shuffleArray';
import { sum } from '../../src/utils/utils';
Expand Down
138 changes: 1 addition & 137 deletions examples/runMinionTask/runMinionTask.ts
Original file line number Diff line number Diff line change
@@ -1,139 +1,3 @@
/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

/*
Task: Add comments to this code
*/

import path from 'path';

import { initCLISystems } from '../../src/CLI/setupCLISystems';
Expand Down Expand Up @@ -177,6 +41,6 @@ This example creates a minion task and runs it.

console.log('Applying task ...');
await mutatorApplyMinionTask(task);

getEditorManager().closeTextDocument?.(task.documentURI);
console.log('Done');
})();
8 changes: 4 additions & 4 deletions score/generateScoreTests.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { z } from 'zod';

import { countTokens } from '../src/gpt/countTokens';
import { ensureIRunThisInRange } from '../src/gpt/ensureIRunThisInRange';
import { gptExecute } from '../src/gpt/gptExecute';
import { GPTMode } from '../src/gpt/types';
import { countTokens } from '../src/gpt/utils/countTokens';
import { ensureIRunThisInRange } from '../src/gpt/utils/ensureIRunThisInRange';
import { MinionTask } from '../src/minionTasks/MinionTask';
import { extractExtensionNameFromPath } from '../src/utils/extractFileNameFromPath';
import { listOfTypes, ScoreTest, TestSchemas } from './types';
Expand Down Expand Up @@ -81,7 +81,7 @@ const prompt = async (minionTask: MinionTask, test?: boolean) => {
Original code contains user's original file content
===== ORIGINAL CODE =====
${minionTask.originalContent}
${minionTask.getOriginalContent}
User query is a prompt for GPT and defines what should happend to code.
===== USER QUERY =====
Expand Down Expand Up @@ -118,7 +118,7 @@ export const generateScoreTests = async (
const maxTokens = ensureIRunThisInRange({
prompt: fullPrompt,
mode,
preferedTokens: fullPromptTokens,
preferredTokens: fullPromptTokens,
minTokens: fullPromptTokens,
});

Expand Down
9 changes: 6 additions & 3 deletions score/initTestMinionTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { readFile } from 'node:fs/promises';
import { existsSync } from 'fs';
import path from 'path';

import { getEditorManager } from '../src/managers/EditorManager';
import {
EditorPosition,
getEditorManager,
} from '../src/managers/EditorManager';
import { MinionTask } from '../src/minionTasks/MinionTask';

export interface Selection {
start: { line: number; character: number };
end: { line: number; character: number };
start: EditorPosition;
end: EditorPosition;
selectedText: string;
}

Expand Down
2 changes: 1 addition & 1 deletion score/prepareScoreTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
initCLISystems,
setupCLISystemsForTest,
} from '../src/CLI/setupCLISystems';
import { countTokens } from '../src/gpt/countTokens';
import { gptExecute } from '../src/gpt/gptExecute';
import { GPTMode } from '../src/gpt/types';
import { countTokens } from '../src/gpt/utils/countTokens';
import {
generateScoreTests,
GenerateScoreTestsResult,
Expand Down
4 changes: 2 additions & 2 deletions score/rateMinionTask.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { z } from 'zod';

import { countTokens } from '../src/gpt/countTokens';
import { createFullPromptFromSections } from '../src/gpt/createFullPromptFromSections';
import { gptExecute } from '../src/gpt/gptExecute';
import { GPTMode } from '../src/gpt/types';
import { countTokens } from '../src/gpt/utils/countTokens';
import { createFullPromptFromSections } from '../src/gpt/utils/createFullPromptFromSections';
import { MinionTask } from '../src/minionTasks/MinionTask';
import { shuffleArray } from '../src/utils/random/shuffleArray';
import { sum } from '../src/utils/utils';
Expand Down
2 changes: 1 addition & 1 deletion score/utils/gptAssert.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z } from 'zod';

import { countTokens } from '../../src/gpt/countTokens';
import { gptExecute } from '../../src/gpt/gptExecute';
import { GPTMode } from '../../src/gpt/types';
import { countTokens } from '../../src/gpt/utils/countTokens';

// TODO: left as reference for now, but should be removed propably
export async function gptAssert({
Expand Down
1 change: 0 additions & 1 deletion src/CLI/CLIEditEntry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EditorTextEdit } from '../managers/EditorManager';

export class CLIEditEntry implements EditorTextEdit {
constructor(
public action: string,
Expand Down
46 changes: 17 additions & 29 deletions src/CLI/CLIEditorDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';

import {
EditorDocument,
EditorPosition,
EditorRange,
EditorUri,
} from '../managers/EditorManager';
Expand All @@ -11,8 +12,7 @@ export class CLIEditorDocument implements EditorDocument {
readonly languageId: string;
readonly lineCount: number;
readonly uri: EditorUri;
private _textLines: string[] = []; // This will store our text lines.
private _numberLines: number[] = []; // This will store our line numbers.
private textLines: string[] = []; // This will store our text lines.

constructor(uri: EditorUri) {
this.uri = uri;
Expand All @@ -21,27 +21,21 @@ export class CLIEditorDocument implements EditorDocument {

// Reading file contents synchronously for simplicity. Consider using async I/O in production code
const fileContent = fs.readFileSync(fileName, 'utf8');
this._textLines = fileContent.split('\n');
this.lineCount = this._textLines.length;
this.textLines = fileContent.split('\n');
this.lineCount = this.textLines.length;

// Derive languageId from file extension. This is simplistic and might not always be correct.
this.languageId = path.extname(fileName).slice(1);

// Populate _numberLines assuming 1-based line numbers
this._numberLines = Array(this.lineCount)
.fill(0)
.map((_, i) => i + 1);
}

getText(range?: EditorRange): string {
// Return joined text from _textLines array within given range or whole text if range is not provided
// Return joined text from textLines array within given range or whole text if range is not provided
return (
range
? this._textLines.slice(range.start.line, range.end.line)
: this._textLines
? this.textLines.slice(range.start.line, range.end.line)
: this.textLines
).join('\n');
}

lineAt(line: number): {
readonly text: string;
readonly lineNumber: number;
Expand All @@ -52,12 +46,12 @@ export class CLIEditorDocument implements EditorDocument {

// Return object with text and line number from the given line
return {
text: this._textLines[line],
lineNumber: this._numberLines[line],
text: this.textLines[line],
lineNumber: line + 1,
};
}

insert(start: { line: number; character: number }, text: string) {
insert(start: EditorPosition, text: string) {
// First, get the existing text for the line
const existingText = this.lineAt(start.line).text;

Expand All @@ -68,16 +62,10 @@ export class CLIEditorDocument implements EditorDocument {
existingText.slice(start.character);

// Update the line with the new text
this._textLines[start.line] = newText;
this.textLines[start.line] = newText;
}

replace(
range: {
start: { line: number; character: number };
end: { line: number; character: number };
},
text: string,
) {
replace(range: EditorRange, text: string) {
// Here we need to replace the text from start to end within the range
const startLineNumber = range.start.line;
const endLineNumber = range.end.line;
Expand All @@ -92,12 +80,12 @@ export class CLIEditorDocument implements EditorDocument {

// If start and end line numbers are same, then it's a replacement within same line
if (startLineNumber === endLineNumber) {
this._textLines[startLineNumber] = newTextStart + newTextEnd;
this.textLines[startLineNumber] = newTextStart + newTextEnd;
} else {
// Update start, end lines and remove lines between them
this._textLines[startLineNumber] = newTextStart;
this._textLines[endLineNumber] = newTextEnd;
this._textLines.splice(
this.textLines[startLineNumber] = newTextStart;
this.textLines[endLineNumber] = newTextEnd;
this.textLines.splice(
startLineNumber + 1,
endLineNumber - startLineNumber,
);
Expand All @@ -106,6 +94,6 @@ export class CLIEditorDocument implements EditorDocument {

save() {
// Write the text back to the file synchronously for simplicity. Consider using async I/O in production code
fs.writeFileSync(this.uri.fsPath, this._textLines.join('\n'), 'utf8');
fs.writeFileSync(this.uri.fsPath, this.textLines.join('\n'), 'utf8');
}
}
Loading

0 comments on commit ca43275

Please sign in to comment.