Skip to content

Commit

Permalink
add workflow logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Fontanier committed Feb 24, 2025
1 parent 7d865bb commit ab0a52e
Show file tree
Hide file tree
Showing 9 changed files with 537 additions and 20 deletions.
2 changes: 1 addition & 1 deletion x/henry/mp-sandbox-agent/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Agent {
console.log("--------------------------------");
console.log(`Creating agent with goal: ${goal}`);
console.log("--------------------------------");
const agent = new Agent(goal);
const agent = new Agent(goal, process.env.OPENAI_API_KEY!);
agent.sandbox = await PythonSandbox.create();
return agent;
}
Expand Down
6 changes: 6 additions & 0 deletions x/henry/mp-sandbox-agent/bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"name": "mp-sandbox",
"dependencies": {
"@micropython/micropython-webassembly-pyscript": "^1.24.1",
"@types/uuid": "^10.0.0",
"dotenv": "^16.4.7",
"google-search-results-nodejs": "^2.1.0",
"openai": "^4.85.1",
"uuid": "^11.1.0",
"zod": "^3.24.2",
},
"devDependencies": {
Expand All @@ -27,6 +29,8 @@

"@types/node-fetch": ["@types/[email protected]", "", { "dependencies": { "@types/node": "*", "form-data": "^4.0.0" } }, "sha512-8nneRWKCg3rMtF69nLQJnOYUcbafYeFSjqkw3jCRLsqkWFlHaoQrr5mXmofFGOx3DKn7UfmBMyov8ySvLRVldA=="],

"@types/uuid": ["@types/[email protected]", "", {}, "sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ=="],

"@types/ws": ["@types/[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-bd/YFLW+URhBzMXurx7lWByOu+xzU9+kb3RboOteXYDfW+tr+JZa99OyNmPINEGB/ahzKrEuc8rcv4gnpJmxTw=="],

"abort-controller": ["[email protected]", "", { "dependencies": { "event-target-shim": "^5.0.0" } }, "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg=="],
Expand Down Expand Up @@ -101,6 +105,8 @@

"undici-types": ["[email protected]", "", {}, "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA=="],

"uuid": ["[email protected]", "", { "bin": { "uuid": "dist/esm/bin/uuid" } }, "sha512-0/A9rDy9P7cJ+8w1c9WD9V//9Wj15Ce2MPz8Ri6032usz+NfePxx5AcN3bN+r6ZL6jEo066/yNYB3tn4pQEx+A=="],

"web-streams-polyfill": ["[email protected]", "", {}, "sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug=="],

"webidl-conversions": ["[email protected]", "", {}, "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="],
Expand Down
37 changes: 20 additions & 17 deletions x/henry/mp-sandbox-agent/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fetchWeather } from "./tools/fetch_weather";
import { Agent } from "./agent";
import { scrapePages } from "./tools/scrape";
import { searchWeb } from "./tools/serp";
import { Workflow } from "./workflow/workflow";

// Load environment variables from .env file
dotenv.config();
Expand All @@ -23,23 +24,25 @@ async function main() {
process.exit(1);
}

// Initialize agent with a goal
const agent = await Agent.create(request, process.env.OPENAI_API_KEY!);

// Define available tools
const tools = {
fetch_weather: fetchWeather,
scrape_pages: scrapePages,
search_web: searchWeb,
};

// Run a step with the user's request
let answer: string | null = null;
while (answer === null) {
answer = await agent.step(tools);
}

console.log(answer);
const workflow = new Workflow<string>("Main Flow").addStep(() => ({
id: "agent",
name: "Agent",
execute: async (input, context) => {
const agent = await Agent.create(request);
const tools = {
fetch_weather: fetchWeather,
scrape_pages: scrapePages,
search_web: searchWeb,
};
let answer: string | null = null;
while (answer === null) {
answer = await agent.step(tools);
}
return answer;
},
}));

const result = await workflow.execute(request);
}

main().catch((error) => {
Expand Down
2 changes: 2 additions & 0 deletions x/henry/mp-sandbox-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
},
"dependencies": {
"@micropython/micropython-webassembly-pyscript": "^1.24.1",
"@types/uuid": "^10.0.0",
"dotenv": "^16.4.7",
"google-search-results-nodejs": "^2.1.0",
"openai": "^4.85.1",
"uuid": "^11.1.0",
"zod": "^3.24.2"
}
}
6 changes: 5 additions & 1 deletion x/henry/mp-sandbox-agent/tools/scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export const scrapePages = defineTool(

log(
input.urls
.map((url, i) => `${url}:\n${JSON.stringify(results[i])}`)
.map((url, i) =>
results[i].success
? `${url}:\n${JSON.stringify(results[i].data.markdown)}`
: `${url}: failed to scrape`
)
.join("\n\n")
);

Expand Down
8 changes: 7 additions & 1 deletion x/henry/mp-sandbox-agent/tools/serp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ export const searchWeb = defineTool(
log(
`Retrieved ${data.organic_results?.length || 0} results for query "${
input.query
}" (page ${input.page}):\n${JSON.stringify(data)}`
}" (page ${input.page}):\n${JSON.stringify(
data.organic_results?.map((r: any) => ({
title: r.title,
link: r.link,
snippet: r.snippet,
}))
)}`
);

return {
Expand Down
26 changes: 26 additions & 0 deletions x/henry/mp-sandbox-agent/workflow/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type WorkflowStepStatus = "pending" | "running" | "completed" | "failed";

export interface WorkflowStepResult<T> {
status: WorkflowStepStatus;
output?: T;
error?: Error;
}

// Type to represent the context available to a step
export type StepContext<T extends Record<string, unknown>> = {
previousOutputs: T;
workflowInput: unknown;
};

export interface WorkflowStep<
Input,
Output,
Context extends Record<string, unknown>
> {
id: string;
name: string;
execute: (input: Input, context: StepContext<Context>) => Promise<Output>;
status?: WorkflowStepStatus;
output?: Output;
error?: Error;
}
Loading

0 comments on commit ab0a52e

Please sign in to comment.