Skip to content

Commit

Permalink
tsdoc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
unixfreaxjp committed Jan 10, 2024
1 parent 6d0fbec commit 2653653
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 258 deletions.
3 changes: 3 additions & 0 deletions typescript/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ all: node_modules
$(TSC) -m node16 --target es2020 opt.ts && cp -f opt.js opt.r2.js
$(TSC) -m node16 --target es2020 r2papi.ts && cp -f r2papi.js index.r2.js

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

pdq: node_modules
# TODO: use pdq.r2.ts
$(TSC) -m node16 --target es2020 pdq.ts
Expand Down
64 changes: 31 additions & 33 deletions typescript/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export class R2AI {
*
* @type {boolean}
*/
available : boolean = false;
available: boolean = false;
/**
* Name of the model instantiated to be used for the subsequent calls.
*
* @type {string}
*/
model : string = "";
constructor (num?: number, model?: string) {
model: string = "";
constructor(num?: number, model?: string) {
this.available = r2.cmd('r2ai -h').trim() !== "";
if (this.available) {
if (num) {
Expand All @@ -31,78 +31,76 @@ export class R2AI {
this.model = model;
}
} else {
throw new Error ("ERROR: r2ai is not installed");
throw new Error("ERROR: r2ai is not installed");
}
}
/**
* Reset conversation messages
*/
* Reset conversation messages
*/
reset() {
if (this.available) {
r2.call('r2ai -R')
}
}
/**
* Set the role (system prompt) message for the language model to obey.
* Set the role (system prompt) message for the language model to obey.
*
* @param {string} text containing the system prompt
* @returns {boolean} true if successful
*/
setRole(msg: string) : boolean {
* @param {string} text containing the system prompt
* @returns {boolean} true if successful
*/
setRole(msg: string): boolean {
if (this.available) {
r2.call(`r2ai -r ${msg}`);
return true;
}
return false;
}
/**
* Set the Model name or path to the GGUF file to use.
* 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
*/
setModel(modelName: string) : boolean {
* @param {string} model name or path to GGUF file
* @returns {boolean} true if successful
*/
setModel(modelName: string): boolean {
if (this.available) {
r2.call(`r2ai -m ${this.model}`)
return true;
}
return false;
}
/**
* Get the current selected model name.
* Get the current selected model name.
*
* @returns {boolean} model name
*/
getModel() : string {
* @returns {boolean} model name
*/
getModel(): string {
if (this.available) {
this.model = r2.call("r2ai -m").trim();
}
return this.model;
}
/**
* Get a list of suggestions for model names to use.
* Get a list of suggestions for model names to use.
*
* @returns {string[]} array of strings containing the model names known to work
*/
listModels() : string[] {
* @returns {string[]} array of strings containing the model names known to work
*/
listModels(): string[] {
if (this.available) {
return r2.call("r2ai -M").replace(/-m /,"").trim().split(/\n/g).filter((x) => x.indexOf(":") !== -1);
return r2.call("r2ai -M").replace(/-m /, "").trim().split(/\n/g).filter((x) => x.indexOf(":") !== -1);
}
return [];
}
/**
* Send message to the language model to be appended to the current conversation (see `.reset()`)
* 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
*/
query(msg: string) : string {
* @param {string} text sent from the user to the language model
* @returns {string} response from the language model
*/
query(msg: string): string {
if (!this.available || msg == '') {
return '';
}
const fmsg = msg.trim().replace(/\n/g, '.');
return r2.call(`r2ai ${fmsg}`).trim();
}
}


}
20 changes: 16 additions & 4 deletions typescript/base64.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
export class Base64 {
static encode(x: string) : string {
return b64(x);
/**
* 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);
}
static decode(x: string) : string {
return b64(x, true);
/**
* 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);
}
}

Expand Down
20 changes: 20 additions & 0 deletions typescript/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,32 @@ class Graph {
constructor () {
r2.cmd("ag-");
}

/**
* Add a node into the graph
*
* @param {string} title label of the node, this label must be unique to the graph
* @param {string} body contents of the node
*/
addNode(title: string, body: string) {
r2.cmd(`agn ${title} ${body}`);
}

/**
* Add an edge linking two nodes referenced by the title
*
* @param {string} a source title node
* @param {string} b destination title node
*/
addEdge(a: string, b: string) {
r2.cmd(`age ${a} ${b}`);
}

/**
* Get an ascii art representation of the graph as a string
*
* @returns {string} the computed graph
*/
toString() {
return r2.cmd("agg");
}
Expand Down
Loading

0 comments on commit 2653653

Please sign in to comment.