parent
214768a3e6
commit
fab2076eb3
@ -1,23 +1,78 @@ |
|||||||
import * as fs from 'fs'; |
import FlexSearch from 'flexsearch'; |
||||||
|
import lunr from 'lunr'; |
||||||
|
|
||||||
class CodeCompletionAgent { |
interface Document { |
||||||
private codebase: string[]; |
id: number; |
||||||
|
filename: string; |
||||||
|
content: string; |
||||||
|
identifier: number; |
||||||
|
} |
||||||
|
|
||||||
constructor(codebasePath: string) { |
enum SupportedFileExtensions { |
||||||
// git or fs
|
solidity = '.sol', |
||||||
this.codebase = this.loadCodebase(codebasePath); |
vyper = '.vy', |
||||||
|
circom = '.circom', |
||||||
} |
} |
||||||
|
|
||||||
private loadCodebase(path: string): string[] { |
export class CodeCompletionAgent { |
||||||
const files = fs.readdirSync(path); |
private workspacesIndexes = new Map<string, any>(); |
||||||
return files |
props: any; |
||||||
.filter(file => file.endsWith('.ts')) |
indexer: any; |
||||||
.flatMap(file => fs.readFileSync(`${path}/${file}`, 'utf-8').split('\n')); |
Documents: Document[] = []; |
||||||
|
|
||||||
|
constructor(props) { |
||||||
|
this.props = props; |
||||||
|
this.props.on('fileManager', 'fileAdded', (path) => { }); |
||||||
|
this.props.on('filePanel', 'workspaceCreated', async () => { }); |
||||||
} |
} |
||||||
|
|
||||||
public getSuggestions(currentLine: string, numSuggestions: number = 3): string[] { |
async getDcocuments() { |
||||||
const suggestions: string[] = []; |
const documents: Document[] = []; |
||||||
// get `numSuggestions` from the llm
|
const dirs = await this.props.call('fileManager', 'dirList', '/'); |
||||||
return suggestions; |
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; |
||||||
|
} |
||||||
|
|
||||||
|
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