use list-dir instead of list-files. move function related functions to the center
This commit is contained in:
1 parent
62f5fa87a9
commit
1e35028148
4 files changed
+49
-21
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
export const spec = {
|
||||
name: 'create-function',
|
||||
description: 'Create a new function on this remote by writing a function module.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Function identifier.' },
|
||||
description: { type: 'string', description: 'Description shown to the AI.' },
|
||||
parameters: { type: 'object', description: 'OpenAI tool parameter schema for the function arguments.' },
|
||||
code: { type: 'string', description: 'JavaScript statements forming the function body; `args` and `ctx` are in scope.' },
|
||||
},
|
||||
required: ['name', 'description', 'parameters', 'code'],
|
||||
},
|
||||
};
|
||||
|
||||
export default async function (args, ctx) {
|
||||
const name = args.name;
|
||||
if (typeof name !== 'string' || !/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/.test(name)) {
|
||||
throw new Error(`Invalid function name: ${JSON.stringify(name)}`);
|
||||
}
|
||||
const functionsDir = path.join(ctx.dir, '.remote', 'functions');
|
||||
fs.mkdirSync(functionsDir, { recursive: true });
|
||||
const moduleSource = `export const spec = ${JSON.stringify({
|
||||
name,
|
||||
description: args.description,
|
||||
parameters: args.parameters,
|
||||
}, null, 2)};
|
||||
|
||||
export default async function (args, ctx) {
|
||||
${args.code}
|
||||
}
|
||||
`;
|
||||
fs.writeFileSync(path.join(functionsDir, `${name}.mjs`), moduleSource);
|
||||
return { created: name };
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
export const spec = {
|
||||
name: 'delete-function',
|
||||
description: 'Delete a function module from this remote.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Name of the function to delete.' },
|
||||
},
|
||||
required: ['name'],
|
||||
},
|
||||
};
|
||||
|
||||
export default async function (args, ctx) {
|
||||
const functionsDir = path.join(ctx.dir, '.remote', 'functions');
|
||||
for (const ext of ['.mjs', '.js']) {
|
||||
const file = path.join(functionsDir, `${args.name}${ext}`);
|
||||
if (fs.existsSync(file)) {
|
||||
fs.unlinkSync(file);
|
||||
return { deleted: args.name };
|
||||
}
|
||||
}
|
||||
throw new Error(`No such function: ${args.name}`);
|
||||
}
|
||||
Reference in new issue
Block a user