libs directory, default main function
This commit is contained in:
1 parent
a08c41a426
commit
80454f9a2a
3 files changed
+47
-9
No files matched your search
@@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
Clone the repository, then install with `npm install -g .`.
|
Clone the repository, then install with `npm install -g .`.
|
||||||
|
|
||||||
Usage: `gral <function> -p <prompt>`, or pass a file as a prompt instead with the `-f <prompt-file>` option.
|
Usage: `gral <function> -p <prompt>`, or pass a file as a prompt instead with the `-f <prompt-file>` option. 'main' is the default function if it exists, otherwise a function name is required.
|
||||||
|
|
||||||
Gral uses the `.gral` directory from the working path for its configuration. The horde connection uses the 'host' and 'token' values stored in `.gral/config.json`, or loads them from the environment if not present.
|
Gral uses the `.gral` directory from the working path for its configuration. The horde connection uses the 'host' and 'token' values stored in `.gral/config.json`, or loads them from the environment if not present.
|
||||||
|
|
||||||
Every javascript module in `.gral/functions` exports a default function, it receives a single prompt string argument. These are the functions invoked by the gral command, you can list them with `gral -l`.
|
Every javascript module in `.gral/functions` exports a default function, it receives a single prompt string argument. These are the functions invoked by the gral command, you can list them with `gral -l`.
|
||||||
|
|
||||||
|
Shared libraries live in `.gral/lib`. Any module there can be imported from a function module using the `@lib/` prefix.
|
||||||
|
|
||||||
The `Gral` global is available in the function module scopes, and exposes:
|
The `Gral` global is available in the function module scopes, and exposes:
|
||||||
- `path`: the working directory
|
- `path`: the working directory
|
||||||
- `run`: the run command for sending prompts to agents
|
- `run`: the run command for sending prompts to agents
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ESM loader hook registered from 'main.js' via `module.register()`.
|
||||||
|
* It resolves '@lib/<name>' specifiers against the project's '.gral/lib'
|
||||||
|
* directory, supplied through the `data` option of `register()`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as path from 'path';
|
||||||
|
import { pathToFileURL } from 'url';
|
||||||
|
|
||||||
|
const LIB_PREFIX = '@lib/';
|
||||||
|
|
||||||
|
let libDir = null;
|
||||||
|
|
||||||
|
export function initialize(data) {
|
||||||
|
libDir = data && data.libDir ? data.libDir : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function resolve(specifier, context, nextResolve) {
|
||||||
|
if (libDir && specifier.startsWith(LIB_PREFIX)) {
|
||||||
|
const target = path.join(libDir, specifier.slice(LIB_PREFIX.length));
|
||||||
|
return { url: pathToFileURL(target).href, shortCircuit: true };
|
||||||
|
}
|
||||||
|
return nextResolve(specifier, context);
|
||||||
|
}
|
||||||
Regular → Executable
+18
-8
@@ -19,12 +19,14 @@
|
|||||||
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
import { register } from 'node:module';
|
||||||
import Horde from './horde.js';
|
import Horde from './horde.js';
|
||||||
import run from './run.js';
|
import run from './run.js';
|
||||||
|
|
||||||
const CONFIG_DIR = '.gral';
|
const CONFIG_DIR = '.gral';
|
||||||
const CONFIG_FILE = 'config.json';
|
const CONFIG_FILE = 'config.json';
|
||||||
const FUNCTIONS_DIR = 'functions';
|
const FUNCTIONS_DIR = 'functions';
|
||||||
|
const LIB_DIR = 'lib';
|
||||||
|
|
||||||
const DEFAULT_CONFIG = {
|
const DEFAULT_CONFIG = {
|
||||||
host: 'wss://hub.hordelab.com',
|
host: 'wss://hub.hordelab.com',
|
||||||
@@ -211,12 +213,12 @@ async function main(argv) {
|
|||||||
let config = loadConfig(root);
|
let config = loadConfig(root);
|
||||||
Gral.path = root;
|
Gral.path = root;
|
||||||
|
|
||||||
|
// Make '.gral/lib/*' importable from function modules via '@lib/<name>'.
|
||||||
|
const libDir = path.join(root, CONFIG_DIR, LIB_DIR);
|
||||||
|
await register('./loader.js', import.meta.url, { data: { libDir } });
|
||||||
|
|
||||||
const functions = await loadFunctions(root);
|
const functions = await loadFunctions(root);
|
||||||
|
Gral.horde = new Horde({host: config.host, token: config.token});
|
||||||
let horde = new Horde({host: config.host, token: config.token});
|
|
||||||
await horde.connect();
|
|
||||||
|
|
||||||
Gral.horde = horde;
|
|
||||||
|
|
||||||
if (command.help) {
|
if (command.help) {
|
||||||
console.log(HELP);
|
console.log(HELP);
|
||||||
@@ -234,15 +236,20 @@ async function main(argv) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!command.functionName) {
|
let fname = command.functionName;
|
||||||
|
if (!fname) {
|
||||||
|
if (functions.has('main')) {
|
||||||
|
fname = 'main';
|
||||||
|
} else {
|
||||||
console.error('Error: a function name is required.');
|
console.error('Error: a function name is required.');
|
||||||
console.error(HELP);
|
console.error(HELP);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const fn = functions.get(command.functionName);
|
const fn = functions.get(fname);
|
||||||
if (!fn) {
|
if (!fn) {
|
||||||
console.error(`Error: unknown function '${command.functionName}'.`);
|
console.error(`Error: unknown function '${fname}'.`);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,6 +260,9 @@ async function main(argv) {
|
|||||||
|
|
||||||
const input = { prompt: command.prompt };
|
const input = { prompt: command.prompt };
|
||||||
|
|
||||||
|
// Connect before executing
|
||||||
|
await Gral.horde.connect();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fn(input);
|
await fn(input);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Reference in new issue
Block a user