Is your feature request related to a problem? Please describe.
Would like to extract JS function for the following prompt.
Initial prompt: "Write a JavaScript function to add two numbers"
Categories:
export const JS_CODE_CATEGORIES: Category[] = [
{
name: 'Average function',
fields: [
{
name: 'function',
type: 'string',
description: 'Extract function to add two numbers',
},
],
description: 'Function that returns average rating of books.',
},
];
Expected output:
function addNumbers(num1, num2) {
return num1 + num2;
}
Describe the solution you'd like
Expecting to see JavaScript function output in the field function
Describe alternatives you've considered
Tried creating a JS template:
import { TemplateBase } from './base';
export class JavaScriptTemplate extends TemplateBase {
protected template: string;
constructor(template: string) {
super();
this.template = template;
}
public numChars(): number {
return this.template.length;
}
public numTokens(): number {
return this.template.split(/\s+/).length;
}
public render(replacements: Record<string, string>): string {
let result = '';
const lines = this.template.split('\n');
for (const line of lines) {
if (line.startsWith('```js')) {
result += '```js\n';
let code = '';
while (lines.length > 0 && !lines[0].startsWith('```')) {
code += `${lines.shift() ?? ''}\n`; // Use template literal
}
result += code;
result += '```\n';
} else {
result += this.replacePlaceholders(line, replacements) + '\n';
}
}
return result;
}
}
Using it like this:
export const JS_PROMPT = new JavaScriptTemplate(`
You are a JavaScript parser. You will be given JavaScript code and you must return an abstract syntax tree (AST) representing that code.
Here is an example AST:
{
"type": "Program",
"body": [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "x"
},
"init": {
"type": "Literal",
"value": 10
}
}
],
"kind": "let"
}
]
}
The AST must follow the ESTree spec (https://github.com/estree/estree)
Here is the JavaScript code to parse`);
Feature Request
Is your feature request related to a problem? Please describe.
Would like to extract JS function for the following prompt.
Initial prompt: "Write a JavaScript function to add two numbers"
Categories:
Expected output:
Describe the solution you'd like
function
Describe alternatives you've considered
Tried creating a JS template:
Using it like this: