feat: enhanced ai command to perfom other commands (#1156)

* Enhanced ai command to perfom other commands

* Enhance AI Command in Puter's shell
This commit is contained in:
Ntwari Bruce
2025-03-08 16:47:53 -06:00
committed by GitHub
parent 8c497935cf
commit 7ed779bbf3
3 changed files with 57 additions and 1 deletions

1
package-lock.json generated
View File

@@ -14,6 +14,7 @@
"experiments/js-parse-and-output"
],
"dependencies": {
"@heyputer/putility": "^1.0.2",
"dedent": "^1.5.3",
"javascript-time-ago": "^2.5.11",
"json-colorizer": "^3.0.1",

View File

@@ -47,6 +47,7 @@
]
},
"dependencies": {
"@heyputer/putility": "^1.0.2",
"dedent": "^1.5.3",
"javascript-time-ago": "^2.5.11",
"json-colorizer": "^3.0.1",

View File

@@ -26,6 +26,9 @@ export default {
$: 'simple-parser',
allowPositionals: true,
},
input: {
synchLines: true,
},
execute: async ctx => {
const { positionals } = ctx.locals;
const [ prompt ] = positionals;
@@ -49,6 +52,18 @@ export default {
a_args = {
messages: [
...chatHistory.get_messages(),
{
role: 'system',
content: `You are a helpful AI assistant that helps users with shell commands.
When a user asks to perform an action:
1. If the action requires a command, wrap ONLY the command between %%% markers
2. Keep the command simple and on a single line
3. Do not ask for confirmation
Example:
User: "create a directory named test"
You: "Creating directory 'test'
%%%mkdir test%%%"`
},
{
role: 'user',
content: prompt,
@@ -80,8 +95,47 @@ export default {
return;
}
chatHistory.add_message(resobj?.result?.message);
await ctx.externs.out.write(message + '\n');
const commandMatch = message.match(/%%%(.*?)%%%/);
if (commandMatch) {
const commandToExecute = commandMatch[1].trim();
const cleanMessage = message.replace(/%%%(.*?)%%%/, '');
await ctx.externs.out.write(cleanMessage + '\n');
await ctx.externs.out.write(`Execute command: '${commandToExecute}' (y/n): `);
try {
let line, done;
const next_line = async () => {
({ value: line, done } = await ctx.externs.in_.read());
}
await next_line();
const inputString = new TextDecoder().decode(line);
const response = (inputString ?? '').trim().toLowerCase();
console.log('processed response', {response});
if (!response.startsWith('y')) {
await ctx.externs.out.write('\nCommand execution cancelled\n');
return;
}
await ctx.externs.out.write('\n');
await ctx.shell.runPipeline(commandToExecute);
await ctx.externs.out.write(`Command executed: ${commandToExecute}\n`);
} catch (error) {
await ctx.externs.err.write(`Error executing command: ${error.message}\n`);
return;
}
} else {
await ctx.externs.out.write(message + '\n');
}
}
}