parent
214768a3e6
commit
fab2076eb3
@ -1,23 +1,78 @@ |
||||
import * as fs from 'fs'; |
||||
import FlexSearch from 'flexsearch'; |
||||
import lunr from 'lunr'; |
||||
|
||||
class CodeCompletionAgent { |
||||
private codebase: string[]; |
||||
interface Document { |
||||
id: number; |
||||
filename: string; |
||||
content: string; |
||||
identifier: number; |
||||
} |
||||
|
||||
enum SupportedFileExtensions { |
||||
solidity = '.sol', |
||||
vyper = '.vy', |
||||
circom = '.circom', |
||||
} |
||||
|
||||
export class CodeCompletionAgent { |
||||
private workspacesIndexes = new Map<string, any>(); |
||||
props: any; |
||||
indexer: any; |
||||
Documents: Document[] = []; |
||||
|
||||
constructor(codebasePath: string) { |
||||
// git or fs
|
||||
this.codebase = this.loadCodebase(codebasePath); |
||||
constructor(props) { |
||||
this.props = props; |
||||
this.props.on('fileManager', 'fileAdded', (path) => { }); |
||||
this.props.on('filePanel', 'workspaceCreated', async () => { }); |
||||
} |
||||
|
||||
private loadCodebase(path: string): string[] { |
||||
const files = fs.readdirSync(path); |
||||
return files |
||||
.filter(file => file.endsWith('.ts')) |
||||
.flatMap(file => fs.readFileSync(`${path}/${file}`, 'utf-8').split('\n')); |
||||
async getDcocuments() { |
||||
const documents: Document[] = []; |
||||
const dirs = await this.props.call('fileManager', 'dirList', '/'); |
||||
let c = 0; |
||||
for (const dir of dirs) { |
||||
const files = await this.props.call('fileManager', 'fileList', dir); |
||||
for (const file of files) { |
||||
const content = await this.props.call('fileManager', 'readFile', file); |
||||
// filter out any SupportedFileExtensions
|
||||
if (! Object.values(SupportedFileExtensions).some(ext => file.endsWith(ext))) continue; |
||||
documents.push({ |
||||
id: ++c, |
||||
filename: file, |
||||
content: content, |
||||
identifier: c*34, |
||||
}); |
||||
} |
||||
} |
||||
console.log('Documents', documents); |
||||
return documents; |
||||
} |
||||
|
||||
public getSuggestions(currentLine: string, numSuggestions: number = 3): string[] { |
||||
const suggestions: string[] = []; |
||||
// get `numSuggestions` from the llm
|
||||
return suggestions; |
||||
indexWorkspace() { |
||||
this.getDcocuments().then((documents) => { |
||||
this.Documents = documents; |
||||
this.indexer =lunr(function () { |
||||
this.ref('id') |
||||
this.field('filename') |
||||
this.field('content') |
||||
this.field('Identifier'); |
||||
|
||||
documents.forEach(doc => { |
||||
this.add(doc); |
||||
}); |
||||
}); |
||||
}); |
||||
} |
||||
|
||||
async searchIndex(query: string) { |
||||
try { |
||||
const searchResult = this.indexer.search(query); |
||||
console.log('Search result', searchResult); |
||||
return searchResult[0]; |
||||
} catch (error) { |
||||
console.log('Error searching index', error); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
@ -0,0 +1,22 @@ |
||||
import React from 'react'; |
||||
|
||||
interface ButtonProps { |
||||
label: string; |
||||
onClick: () => void; |
||||
icon?: string; |
||||
} |
||||
|
||||
const DynamicButtons: React.FC<{ buttons: ButtonProps[] }> = ({ buttons }) => { |
||||
return ( |
||||
<div className="dynamic-buttons"> |
||||
{buttons.map((button, index) => ( |
||||
<button key={index} onClick={button.onClick}> |
||||
{button.icon && <i className={button.icon}></i>} |
||||
{button.label} |
||||
</button> |
||||
))} |
||||
</div> |
||||
); |
||||
}; |
||||
|
||||
export default DynamicButtons; |
Loading…
Reference in new issue