initial contract generation

pull/4801/head
Stéphane Tetsing 6 months ago
parent 0f1f9f1413
commit 7c4ac8ccf7
  1. 37
      apps/remix-ide/src/app/plugins/solcoderAI.tsx
  2. 36
      libs/remix-ui/editor/src/lib/providers/inlineCompletionProvider.ts

@ -219,6 +219,43 @@ export class SolCoder extends Plugin {
} }
} }
async contract_genertion(prompt): Promise<any> {
this.emit("aiInfering")
let result
try {
result = await(
await fetch(this.completion_url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ "data":[
prompt, // string in 'context_code' Textbox component
"contract_generation",
false, // boolean in 'stream_result' Checkbox component
2000, // number (numeric value between 0 and 2000) in 'max_new_tokens' Slider component
0.9, // number (numeric value between 0.01 and 1) in 'temperature' Slider component
0.90, // number (numeric value between 0 and 1) in 'top_p' Slider component
50, // number (numeric value between 1 and 200) in 'top_k' Slider component
]}),
})
).json()
if ("error" in result){
return result
}
return result.data
} catch (e) {
this.call('terminal', 'log', { type: 'aitypewriterwarning', value: `Unable to get a response ${e.message}` })
return
} finally {
this.emit("aiInferingDone")
}
}
_build_solgpt_promt(user_promt:string){ _build_solgpt_promt(user_promt:string){
if (this.solgpt_chat_history.length === 0){ if (this.solgpt_chat_history.length === 0){
return user_promt return user_promt

@ -14,10 +14,12 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
props: EditorUIProps props: EditorUIProps
monaco: any monaco: any
completionEnabled: boolean completionEnabled: boolean
isGeneratingContract: boolean
constructor(props: any, monaco: any) { constructor(props: any, monaco: any) {
this.props = props this.props = props
this.monaco = monaco this.monaco = monaco
this.completionEnabled = true this.completionEnabled = true
this.isGeneratingContract = false
} }
async provideInlineCompletions(model: monacoTypes.editor.ITextModel, position: monacoTypes.Position, context: monacoTypes.languages.InlineCompletionContext, token: monacoTypes.CancellationToken): Promise<monacoTypes.languages.InlineCompletions<monacoTypes.languages.InlineCompletion>> { async provideInlineCompletions(model: monacoTypes.editor.ITextModel, position: monacoTypes.Position, context: monacoTypes.languages.InlineCompletionContext, token: monacoTypes.CancellationToken): Promise<monacoTypes.languages.InlineCompletions<monacoTypes.languages.InlineCompletion>> {
@ -60,6 +62,7 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
try { try {
const split = word.split('\n') const split = word.split('\n')
if (split.length < 2) return if (split.length < 2) return
const ask = split[split.length - 2].trimStart() const ask = split[split.length - 2].trimStart()
if (split[split.length - 1].trim() === '' && ask.startsWith('///')) { if (split[split.length - 1].trim() === '' && ask.startsWith('///')) {
// use the code generation model, only take max 1000 word as context // use the code generation model, only take max 1000 word as context
@ -82,6 +85,25 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
return return
} }
console.log("is sol comment", this.isSolidityComment(word))
if (this.isSolidityComment(word) && word.includes("sol-gen") && this.isGeneratingContract===false){
this.isGeneratingContract = true
console.log("new contract generation")
const output = await this.props.plugin.call('solcoder', 'contract_generation', word)
_paq.push(['trackEvent', 'ai', 'solcoder', 'contract_generation'])
const handleCompletionTimer = new CompletionTimer(5000, () => { this.isGeneratingContract = false });
handleCompletionTimer.start()
const item: monacoTypes.languages.InlineCompletion = {
insertText: output[0]
};
return {
items: [item],
enableForwardStability: true
}
}
if (word.split('\n').at(-1).trimStart().startsWith('//') || if (word.split('\n').at(-1).trimStart().startsWith('//') ||
word.split('\n').at(-1).trimStart().startsWith('/*') || word.split('\n').at(-1).trimStart().startsWith('/*') ||
word.split('\n').at(-1).trimStart().startsWith('*') || word.split('\n').at(-1).trimStart().startsWith('*') ||
@ -158,6 +180,20 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
} }
} }
isSolidityComment(text: string): boolean {
const joinedText = text.trim().replace(/\n\s*/g, ' ');
const singleLineComment = /^\/\/.*$/;
const multiLineComment = /^\/\*[\s\S]*\*\/$/;
const singleLineNatSpec = /^\/\/\/.*$/;
const multiLineNatSpec = /^\/\*\*[\s\S]*\*\/$/;
return singleLineComment.test(joinedText) ||
multiLineComment.test(joinedText) ||
singleLineNatSpec.test(joinedText) ||
multiLineNatSpec.test(joinedText);
}
process_completion(data: any) { process_completion(data: any) {
let clean = data.split('\n')[0].startsWith('\n') ? [data.split('\n')[0], data.split('\n')[1]].join('\n'): data.split('\n')[0] let clean = data.split('\n')[0].startsWith('\n') ? [data.split('\n')[0], data.split('\n')[1]].join('\n'): data.split('\n')[0]

Loading…
Cancel
Save