-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
459 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,5 +72,3 @@ export async function computeAgent( | |
|
||
return { response: code, model: modelVersion }; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,4 +94,3 @@ export async function computeViewAgent( | |
|
||
return { response: code, model: modelVersion }; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,153 +1,144 @@ | ||
import {parser} from '@lezer/python'; | ||
import { TreeCursor } from '@lezer/common'; | ||
import { readFileSync } from 'fs'; | ||
import {PythonShell} from 'python-shell' | ||
|
||
import { parser } from "@lezer/python"; | ||
import { TreeCursor } from "@lezer/common"; | ||
import { readFileSync } from "fs"; | ||
import { PythonShell } from "python-shell"; | ||
|
||
async function validateSource(source) { | ||
await PythonShell.checkSyntax(source) | ||
await PythonShell.checkSyntax(source); | ||
} | ||
|
||
export async function compileComputationFunction(source) { | ||
const result = await extractIO(source) | ||
const docstring = getDocstring(source) | ||
if(docstring) { | ||
result['description'] = docstring | ||
} | ||
return result | ||
const result = await extractIO(source); | ||
const docstring = getDocstring(source); | ||
if (docstring) { | ||
result["description"] = docstring; | ||
} | ||
return result; | ||
} | ||
|
||
|
||
|
||
async function extractIO(source) { | ||
await validateSource(source) | ||
const tree = parser.parse(source); | ||
const cursor = tree.cursor() | ||
await validateSource(source); | ||
const tree = parser.parse(source); | ||
const cursor = tree.cursor(); | ||
|
||
const functionInfo = {}; | ||
while (cursor.next()) { | ||
if (cursor.node.name === "FunctionDefinition") { | ||
cursor.firstChild(); | ||
cursor.nextSibling(); | ||
const functionName = source.slice(cursor.node.from, cursor.node.to); | ||
if (functionName === "compute") { | ||
cursor.nextSibling(); | ||
const inputs = {}; | ||
cursor.firstChild(); | ||
cursor.nextSibling(); | ||
while (cursor.node.name !== ")") { | ||
const param = source.slice(cursor.node.from, cursor.node.to); | ||
inputs[param] = { | ||
type: "Any", // Type inference is complex in Python | ||
connections: [], | ||
relays: [], | ||
}; | ||
cursor.nextSibling(); | ||
if (cursor.node.name === ",") cursor.nextSibling(); | ||
} | ||
|
||
const functionInfo = {} | ||
while(cursor.next()) { | ||
if(cursor.node.name === 'FunctionDefinition') { | ||
cursor.firstChild() | ||
cursor.nextSibling() | ||
const functionName = source.slice(cursor.node.from, cursor.node.to) | ||
if(functionName === 'compute') { | ||
functionInfo["inputs"] = inputs; | ||
cursor.parent(); | ||
|
||
cursor.nextSibling(); | ||
const inputs = {}; | ||
const outputs = {}; | ||
while (cursor.nextSibling()) { | ||
if (cursor.node.name === "Body") { | ||
cursor.firstChild(); | ||
while (cursor.nextSibling()) { | ||
if (cursor.node.name === "ReturnStatement") { | ||
cursor.firstChild(); | ||
cursor.nextSibling() | ||
while(cursor.node.name !== ')') { | ||
const param = source.slice(cursor.node.from, cursor.node.to) | ||
inputs[param] = { | ||
'type': 'Any', // Type inference is complex in Python | ||
'connections': [], | ||
'relays': [] | ||
}; | ||
cursor.nextSibling() | ||
if(cursor.node.name === ',') cursor.nextSibling() | ||
} | ||
|
||
|
||
functionInfo['inputs'] = inputs | ||
cursor.parent() | ||
|
||
const outputs = {} | ||
while(cursor.nextSibling()){ | ||
if(cursor.node.name === 'Body'){ | ||
cursor.firstChild() | ||
while(cursor.nextSibling()){ | ||
|
||
|
||
if(cursor.node.name === 'ReturnStatement'){ | ||
cursor.firstChild() | ||
cursor.nextSibling() | ||
if(cursor.node.name === 'DictionaryExpression') { | ||
cursor.firstChild() | ||
cursor.nextSibling() | ||
while(cursor.node.name !== '}'){ | ||
cursor.nextSibling() | ||
if(cursor.node.name === 'VariableName'){ | ||
const outputName = source.slice(cursor.node.from, cursor.node.to) | ||
outputs[outputName] = { | ||
'type': 'Any', | ||
'connections': [], | ||
'relays': [] | ||
} | ||
} | ||
} | ||
|
||
|
||
} | ||
} | ||
|
||
} | ||
|
||
cursor.nextSibling(); | ||
if (cursor.node.name === "DictionaryExpression") { | ||
cursor.firstChild(); | ||
cursor.nextSibling(); | ||
while (cursor.node.name !== "}") { | ||
cursor.nextSibling(); | ||
if (cursor.node.name === "VariableName") { | ||
const outputName = source.slice( | ||
cursor.node.from, | ||
cursor.node.to, | ||
); | ||
outputs[outputName] = { | ||
type: "Any", | ||
connections: [], | ||
relays: [], | ||
}; | ||
} | ||
} | ||
} | ||
functionInfo['outputs'] = outputs | ||
break | ||
|
||
} | ||
} | ||
} | ||
} | ||
functionInfo["outputs"] = outputs; | ||
break; | ||
} | ||
} | ||
return functionInfo | ||
} | ||
return functionInfo; | ||
} | ||
|
||
|
||
function getDocstring(source) { | ||
const tree = parser.parse(source); | ||
const cursor = tree.cursor(); | ||
|
||
const docstrings = {}; | ||
const tree = parser.parse(source); | ||
const cursor = tree.cursor(); | ||
|
||
while (cursor.next()) { | ||
if (cursor.node.name === 'FunctionDefinition') { | ||
cursor.firstChild(); // Move to the 'def' keyword | ||
cursor.nextSibling(); // Move to the function name | ||
const docstrings = {}; | ||
|
||
const functionName = source.slice(cursor.node.from, cursor.node.to); | ||
|
||
if(functionName === 'compute') { | ||
cursor.firstChild() | ||
while(true) { | ||
if(cursor.node.name === 'Body') { | ||
break | ||
} | ||
cursor.nextSibling() | ||
} | ||
cursor.firstChild() | ||
while(cursor.nextSibling()) { | ||
if(cursor.node.name === 'ExpressionStatement') { | ||
|
||
const docstring = source.slice(cursor.node.from+3, cursor.node.to-3) | ||
return docstring | ||
} | ||
} | ||
} | ||
while (cursor.next()) { | ||
if (cursor.node.name === "FunctionDefinition") { | ||
cursor.firstChild(); // Move to the 'def' keyword | ||
cursor.nextSibling(); // Move to the function name | ||
|
||
cursor.nextSibling(); // Move to the parameters | ||
cursor.nextSibling(); // Move to the colon or the body | ||
const functionName = source.slice(cursor.node.from, cursor.node.to); | ||
|
||
while (cursor.nextSibling()) { | ||
if (cursor.node.name === 'Body') { | ||
cursor.firstChild(); // Enter the body | ||
console.log("HERE") | ||
// Check if the first statement is a docstring | ||
if (cursor.node.name === 'ExpressionStatement') { | ||
cursor.firstChild(); // Move to the string | ||
if (cursor.node.name === 'String') { | ||
let docstring = source.slice(cursor.node.from, cursor.node.to); | ||
docstring = docstring.slice(1, -1); // Remove the quotes from the string | ||
docstrings[functionName] = docstring; | ||
} | ||
cursor.parent(); // Move back to the ExpressionStatement | ||
} | ||
break; | ||
} | ||
if (functionName === "compute") { | ||
cursor.firstChild(); | ||
while (true) { | ||
if (cursor.node.name === "Body") { | ||
break; | ||
} | ||
cursor.nextSibling(); | ||
} | ||
cursor.firstChild(); | ||
while (cursor.nextSibling()) { | ||
if (cursor.node.name === "ExpressionStatement") { | ||
const docstring = source.slice( | ||
cursor.node.from + 3, | ||
cursor.node.to - 3, | ||
); | ||
return docstring; | ||
} | ||
} | ||
} | ||
|
||
cursor.nextSibling(); // Move to the parameters | ||
cursor.nextSibling(); // Move to the colon or the body | ||
|
||
while (cursor.nextSibling()) { | ||
if (cursor.node.name === "Body") { | ||
cursor.firstChild(); // Enter the body | ||
console.log("HERE"); | ||
// Check if the first statement is a docstring | ||
if (cursor.node.name === "ExpressionStatement") { | ||
cursor.firstChild(); // Move to the string | ||
if (cursor.node.name === "String") { | ||
let docstring = source.slice(cursor.node.from, cursor.node.to); | ||
docstring = docstring.slice(1, -1); // Remove the quotes from the string | ||
docstrings[functionName] = docstring; | ||
} | ||
cursor.parent(); // Move back to the ExpressionStatement | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return docstrings; | ||
return docstrings; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.