Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
nadeesha committed Dec 9, 2024
1 parent 996462b commit db6fb7a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
61 changes: 37 additions & 24 deletions bootstrap-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions bootstrap-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit db6fb7a

Please sign in to comment.