From db6fb7aed6a945dfc05a19fea848f745fd0493c8 Mon Sep 17 00:00:00 2001 From: Nadeesha Cabral Date: Mon, 9 Dec 2024 14:59:09 +1100 Subject: [PATCH] update --- bootstrap-node/README.md | 61 ++++++++++++++++++++++--------------- bootstrap-node/src/index.ts | 1 + 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/bootstrap-node/README.md b/bootstrap-node/README.md index f429b9c1..d136c696 100644 --- a/bootstrap-node/README.md +++ b/bootstrap-node/README.md @@ -8,23 +8,27 @@ This is a Node.js bootstrap application that demonstrates how to integrate and u ## The Application -The application is a simple Node.js application that extracts the top posts from Hacker News and summarizes the comments for each post. It demonstrates how to: +The application demonstrates an agent that can inspect and analyze source code by iteratively executing system commands. It shows how to: - Register Typescript functions with Inferable -- Trigger a Run programmatically to orchestrate the functions -- Control the control flow of the Run using native Node.js control flow primitives +- Trigger a Run programmatically to provide a goal +- Restrict the agent's access to the filesystem using source code ```mermaid sequenceDiagram - participant extract - participant summarizePost - participant generatePage - - extract->>extract: Get top 3 HN posts - extract->>summarizePost: Posts data - summarizePost->>summarizePost: Get & analyze comments - summarizePost->>generatePage: Summaries data - generatePage->>generatePage: Generate HTML + participant Agent + participant exec + participant FileSystem + + Agent->>exec: Request file listing (ls) + exec->>FileSystem: Execute ls command + FileSystem->>exec: Return file list + exec->>Agent: File list results + Agent->>exec: Request file contents (cat) + exec->>FileSystem: Execute cat command + FileSystem->>exec: Return file contents + exec->>Agent: File contents + Agent->>Agent: Analyze code and generate report ``` ## How to Run @@ -43,20 +47,29 @@ npm run run ## How it works -1. The worker machine uses the Inferable Node.js SDK to register the functions with Inferable. These functions are: +1. The worker machine uses the Inferable Node.js SDK to register the `exec` function with Inferable. This function: -- `getUrlContent`: Get the html content of any url -- `scoreHNPost`: Score a post based on the number of comments and upvotes -- `generatePage`: Generate an HTML page with the summaries and save it to a tmp file in your OS's temp directory + - Accepts `ls` or `cat` commands with path arguments + - Only allows accessing paths that start with "./" + - Returns the stdout and stderr from the command execution -2. The `run.ts` script defines "Runs" with the Inferable Node.js SDK. These are: +2. The `run.ts` script creates a Re-Act agent that: -- `extract`: Extracts the top 3 HN posts -- `summarizePost`: Summarizes the comments for a given post -- `generatePage`: Generates an HTML page from the summaries + - Receives an initial prompt to inspect source code in the current directory + - Can iteratively call the `exec` function to list and read files + - Produces a final report containing: + - The name of the program + - A list of its capabilities -3. Given the run configuration (prompts, result schema, etc), the worker machine will orchestrate the functions to generate the page. +3. The agent will: -- `extract` will get the top 3 HN posts using the `getUrlContent` function, and score them using the `scoreHNPost` function -- `summarizePost` will summarize the comments for each post using the `getUrlContent` function -- `generatePage` will generate the HTML page using the `generatePage` function + - Use `ls` to discover files in the directory + - Use `cat` to read the contents of relevant files + - Analyze the code to understand its functionality + - Generate a structured report based on its findings + +## Security + +- The `exec` function is restricted to only allow access to files starting with "./" +- The agent is designed to be safe and only perform actions that are relevant to the task +- The constraints are enforced by source code, and cannot be bypassed by the agent diff --git a/bootstrap-node/src/index.ts b/bootstrap-node/src/index.ts index 93c8646d..7f81492c 100644 --- a/bootstrap-node/src/index.ts +++ b/bootstrap-node/src/index.ts @@ -16,6 +16,7 @@ client.default.register({ func: async ({ command, arg }: { command: string; arg: string }) => { assert(arg.startsWith("./"), "can only access paths starting with ./"); const { stdout, stderr } = await execFilePromise(command, [arg]); + return { stdout: stdout.trim(), stderr: stderr.trim(),