Skip to content

Commit

Permalink
Use prettier to indent the Typescript code
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Jan 23, 2024
1 parent b55d894 commit 419295c
Show file tree
Hide file tree
Showing 17 changed files with 2,169 additions and 2,027 deletions.
27 changes: 27 additions & 0 deletions typescript/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"env": {
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 8,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"no-empty": ["error", { "allowEmptyCatch": true }],
"@typescript-eslint/no-this-alias": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unused-vars": "off"
}
}


7 changes: 7 additions & 0 deletions typescript/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"tabWidth": 4,
"trailingComma": "none",
"singleQuote": false,
"printWidth": 80
}
2 changes: 1 addition & 1 deletion typescript/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ all: node_modules
$(MAKE) build

vs:
open -a "Visual Studio Code" . || vscode .
open -a "Visual Studio Code" async || vscode .


SYNCREGEX=
Expand Down
202 changes: 103 additions & 99 deletions typescript/async/ai.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import { R, Module, Process, Thread } from "./r2papi.js"
import { R, Module, Process, Thread } from "./r2papi.js";
import { r2, R2PipeAsync } from "./r2pipe.js";

/**
* Class that interacts with the `r2ai` plugin (requires `rlang-python` and `r2i` r2pm packages to be installed).
* Provides a way to script the interactions with different language models using javascript from inside radare2.
*
*
* @typedef R2AI
*/
export class R2AI {
/**
* Instance variable that informs if the `r2ai` plugin is loaded, must be true in order to use the rest of the methods of this class.
*
* @type {boolean}
*/
available: boolean = false;
/**
* Name of the model instantiated to be used for the subsequent calls.
*
* @type {string}
*/
model: string = "";
r2: R2PipeAsync;
/**
* Instance variable that informs if the `r2ai` plugin is loaded, must be true in order to use the rest of the methods of this class.
*
* @type {boolean}
*/
available: boolean = false;
/**
* Name of the model instantiated to be used for the subsequent calls.
*
* @type {string}
*/
model: string = "";
r2: R2PipeAsync;

constructor(r2: R2PipeAsync, num?: number, model?: string) {
this.r2 = r2;
this.available = false;
}
constructor(r2: R2PipeAsync, num?: number, model?: string) {
this.r2 = r2;
this.available = false;
}

async checkAvailability() : Promise<boolean> {
if (this.available) {
return true;
}
this.available = r2.cmd('r2ai -h').trim() !== "";
/*
async checkAvailability(): Promise<boolean> {
if (this.available) {
return true;
}
this.available = r2.cmd("r2ai -h").trim() !== "";
/*
if (this.available) {
if (num) {
r2.call(`r2ai -n ${num}`)
Expand All @@ -43,78 +43,82 @@ export class R2AI {
}
}
*/
return this.available;
}
/**
* Reset conversation messages
*/
async reset() {
await this.checkAvailability();
if (this.available) {
await r2.call('r2ai -R')
}
}
/**
* Set the role (system prompt) message for the language model to obey.
*
* @param {string} text containing the system prompt
* @returns {boolean} true if successful
*/
async setRole(msg: string): Promise<boolean> {
if (this.available) {
await r2.call(`r2ai -r ${msg}`);
return true;
}
return false;
}
/**
* Set the Model name or path to the GGUF file to use.
*
* @param {string} model name or path to GGUF file
* @returns {boolean} true if successful
*/
async setModel(modelName: string): Promise<boolean> {
if (this.available) {
await r2.call(`r2ai -m ${this.model}`)
return true;
}
return false;
}
/**
* Get the current selected model name.
*
* @returns {boolean} model name
*/
async getModel(): Promise<string> {
if (this.available) {
this.model = await r2.call("r2ai -m").trim();
}
return this.model;
}
/**
* Get a list of suggestions for model names to use.
*
* @returns {string[]} array of strings containing the model names known to work
*/
async listModels(): Promise<string[]> {
if (this.available) {
const models = await r2.call("r2ai -M");
return models.replace(/-m /, "").trim().split(/\n/g).filter((x: string) => x.indexOf(":") !== -1);
}
return [];
}
/**
* Send message to the language model to be appended to the current conversation (see `.reset()`)
*
* @param {string} text sent from the user to the language model
* @returns {string} response from the language model
*/
async query(msg: string): Promise<string> {
if (!this.available || msg == '') {
return '';
}
const fmsg = msg.trim().replace(/\n/g, '.');
const response = r2.call(`r2ai ${fmsg}`);
return response.trim();
}
return this.available;
}
/**
* Reset conversation messages
*/
async reset() {
await this.checkAvailability();
if (this.available) {
await r2.call("r2ai -R");
}
}
/**
* Set the role (system prompt) message for the language model to obey.
*
* @param {string} text containing the system prompt
* @returns {boolean} true if successful
*/
async setRole(msg: string): Promise<boolean> {
if (this.available) {
await r2.call(`r2ai -r ${msg}`);
return true;
}
return false;
}
/**
* Set the Model name or path to the GGUF file to use.
*
* @param {string} model name or path to GGUF file
* @returns {boolean} true if successful
*/
async setModel(modelName: string): Promise<boolean> {
if (this.available) {
await r2.call(`r2ai -m ${this.model}`);
return true;
}
return false;
}
/**
* Get the current selected model name.
*
* @returns {boolean} model name
*/
async getModel(): Promise<string> {
if (this.available) {
this.model = await r2.call("r2ai -m").trim();
}
return this.model;
}
/**
* Get a list of suggestions for model names to use.
*
* @returns {string[]} array of strings containing the model names known to work
*/
async listModels(): Promise<string[]> {
if (this.available) {
const models = await r2.call("r2ai -M");
return models
.replace(/-m /, "")
.trim()
.split(/\n/g)
.filter((x: string) => x.indexOf(":") !== -1);
}
return [];
}
/**
* Send message to the language model to be appended to the current conversation (see `.reset()`)
*
* @param {string} text sent from the user to the language model
* @returns {string} response from the language model
*/
async query(msg: string): Promise<string> {
if (!this.available || msg == "") {
return "";
}
const fmsg = msg.trim().replace(/\n/g, ".");
const response = r2.call(`r2ai ${fmsg}`);
return response.trim();
}
}
38 changes: 19 additions & 19 deletions typescript/async/base64.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
export class Base64 {
/**
* Encode the given input string using base64
*
* @param {string} input string to encode
* @returns {string} base64 encoded string
*/
static encode(input: string): string {
return b64(input);
}
/**
* Decode the given base64 string into plain text
*
* @param {string} input string encoded in base64 format
* @returns {string} base64 decoded string
*/
static decode(input: string): string {
return b64(input, true);
}
/**
* Encode the given input string using base64
*
* @param {string} input string to encode
* @returns {string} base64 encoded string
*/
static encode(input: string): string {
return b64(input);
}
/**
* Decode the given base64 string into plain text
*
* @param {string} input string encoded in base64 format
* @returns {string} base64 decoded string
*/
static decode(input: string): string {
return b64(input, true);
}
}

export interface Base64Interface {
(message: string, decode?: boolean): string;
(message: string, decode?: boolean): string;
}

export declare const b64: Base64Interface;
45 changes: 22 additions & 23 deletions typescript/async/elang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@ import { R2PipeAsync } from "./index";
//

export class EsilLang {
[vars : string]: any;
constructor() {
this.vars = {};
}
set(obj: any, val: any) {
this.vars[obj] = val;
}
fun(name: string, code: any[]) {
this.vars[name] = code;
}
get(varname: string) : any {
if (varname in Object.keys(this.vars)) {
return this.vars[varname];
}
this.vars[varname] = 0;
return 0;
}
println(...args: any) {
console.log(...args);
}
eval(code: string) {
}
[vars: string]: any;
constructor() {
this.vars = {};
}
set(obj: any, val: any) {
this.vars[obj] = val;
}
fun(name: string, code: any[]) {
this.vars[name] = code;
}
get(varname: string): any {
if (varname in Object.keys(this.vars)) {
return this.vars[varname];
}
this.vars[varname] = 0;
return 0;
}
println(...args: any) {
console.log(...args);
}
eval(code: string) {}
}

// basic elements: array, string,
// basic elements: array, string,
// console.log("Hello Lang");
const el = new EsilLang();
const code = `[
Expand Down
Loading

0 comments on commit 419295c

Please sign in to comment.