Skip to content

Commit

Permalink
Merge pull request #264 from polywrap/developer-agent
Browse files Browse the repository at this point in the history
feat: dev-agent
  • Loading branch information
dOrgJelli authored Oct 3, 2023
2 parents 8cd2afe + f790a28 commit 4b60f38
Show file tree
Hide file tree
Showing 49 changed files with 604 additions and 84 deletions.
4 changes: 3 additions & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
"test:script": "ts-node ./src/__tests__/runners/script-js.ts"
},
"dependencies": {
"@evo-ninja/evo-agent": "0.1.0",
"@evo-ninja/evo-agent": "~0.1.0",
"@evo-ninja/agent-debug": "~0.1.0",
"@evo-ninja/agent-utils": "~0.1.0",
"@evo-ninja/agent-utils-fs": "~0.1.0",
"forked-agent-protocol": "0.0.5",
"chalk": "^4.1.2",
"commander": "11.0.0",
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions apps/cli/src/agent-protocol/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./AgentProtocolWorkspace";
2 changes: 1 addition & 1 deletion apps/cli/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AgentProtocolWorkspace } from "./sys/AgentProtocolWorkspace";
import { AgentProtocolWorkspace } from "./agent-protocol";
import { createApp } from "./app";

import Agent, {
Expand Down
5 changes: 2 additions & 3 deletions apps/cli/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { FileSystemWorkspace, FileLogger } from "./sys";
import { DebugLog, DebugLlmApi } from "./diagnostic";

import { Evo } from "@evo-ninja/evo-agent";
import {
Env,
Expand All @@ -14,6 +11,8 @@ import {
LlmApi,
ContextWindow,
} from "@evo-ninja/agent-utils";
import { DebugLog, DebugLlmApi } from "@evo-ninja/agent-debug";
import { FileSystemWorkspace, FileLogger } from "@evo-ninja/agent-utils-fs";
import dotenv from "dotenv";
import readline from "readline";
import path from "path";
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
"start": "yarn workspace evo-ninja run start",
"start:browser": "yarn workspace @evo-ninja/ui run start",
"start:api": "yarn workspace evo-ninja run start:api",
"build": "yarn build:agent-utils && yarn build:agents && yarn build:cli && yarn build:browser",
"build:agent-utils": "yarn workspace @evo-ninja/agent-utils run build",
"build:agents": "yarn build:script-writer && yarn build:evo",
"build": "yarn build:agent-utils && yarn build:agent-debug && yarn build:agents && yarn build:cli && yarn build:browser",
"build:agent-utils": "yarn workspace @evo-ninja/agent-utils run build && yarn build:agent-utils-fs",
"build:agent-utils-fs": "yarn workspace @evo-ninja/agent-utils-fs run build",
"build:agent-debug": "yarn workspace @evo-ninja/agent-debug run build",
"build:agents": "yarn build:script-writer && yarn build:dev && yarn build:evo",
"build:evo": "yarn workspace @evo-ninja/evo-agent run build",
"build:dev": "yarn workspace @evo-ninja/dev-agent run build",
"build:script-writer": "yarn workspace @evo-ninja/js-script-writer-agent run build",
"build:cli": "yarn workspace evo-ninja run build",
"build:browser": "yarn workspace @evo-ninja/ui run build"
},
"devDependencies": {
"rimraf": "5.0.1"
"rimraf": "~5.0.1"
}
}
17 changes: 17 additions & 0 deletions packages/agent-debug/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@evo-ninja/agent-debug",
"version": "0.1.0",
"license": "MIT",
"main": "./build/index.js",
"scripts": {
"build": "rimraf build && tsc"
},
"dependencies": {
"@evo-ninja/agent-utils": "~0.1.0"
},
"devDependencies": {
"rimraf": "~5.0.1",
"ts-node": "10.9.1",
"typescript": "4.9.5"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export class DebugLlmApi implements LlmApi {
functionDefinitions: any[],
options?: LlmOptions | undefined
): Promise<ChatMessage | undefined> {
console.log(this.getModel());

const time = new Timer();
time.start();

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions packages/agent-debug/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"outDir": "build"
},
"include": [
"./src/**/*.ts"
],
"exclude": []
}
17 changes: 17 additions & 0 deletions packages/agent-utils-fs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@evo-ninja/agent-utils-fs",
"version": "0.1.0",
"license": "MIT",
"main": "./build/index.js",
"scripts": {
"build": "rimraf build && tsc"
},
"dependencies": {
"@evo-ninja/agent-utils": "~0.1.0"
},
"devDependencies": {
"rimraf": "~5.0.1",
"ts-node": "10.9.1",
"typescript": "4.9.5"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions packages/agent-utils-fs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"outDir": "build"
},
"include": [
"./src/**/*.ts"
],
"exclude": []
}
2 changes: 2 additions & 0 deletions packages/agent-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"codegen:js-engine": "polywrap codegen -m ./src/scripts/js-engine/polywrap.yaml --codegen-dir ./src/scripts/js-engine/types"
},
"dependencies": {
"@polywrap/client-js": "~0.12.0",
"@polywrap/plugin-js": "~0.12.0",
"@polywrap/result": "~0.12.0",
"axios": "^0.26.0",
"figlet": "~1.6.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/agent-utils/src/agent/basicFunctionCallLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ export async function* basicFunctionCallLoop<TContext extends { llm: LlmApi, cha

result.value.messages.forEach(x => chat.temporary(x));

const terminate = functionCalled && shouldTerminate(functionCalled, result);

for (let i = 0; i < result.value.outputs.length; i++) {
const output = result.value.outputs[i];

if (i === result.value.outputs.length - 1 &&
functionCalled && shouldTerminate(functionCalled, result)
) {
if (i === result.value.outputs.length - 1 && terminate) {
return ResultOk(output);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
Module,
manifest
} from "./types";
import { AGENT_PLUGIN_SPEAK_RESPONSE } from "../prompts";
import { Logger } from "../../";

import { Logger } from "@evo-ninja/agent-utils";
import { PluginFactory, PluginPackage } from "@polywrap/plugin-js";

export const AGENT_PLUGIN_SPEAK_RESPONSE =
"User has been informed! If you think you've achieved the goal, execute onGoalAchieved.\nIf you think you've failed, execute onGoalFailed.";

export interface AgentPluginConfig {
logger: Logger;
}
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/agent-utils/src/scripts/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from "./agent-plugin";
export * from "./js-engine";
export * from "./wrap-client";
export * from "./Scripts";
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Env, Logger, Workspace } from "../../";

import {
PolywrapClient,
PolywrapClientConfigBuilder,
Expand All @@ -6,13 +8,11 @@ import {
IWrapPackage,
Result
} from "@polywrap/client-js";
import { PluginPackage } from "@polywrap/plugin-js";
import axios from "axios";

import { InvokerOptions } from "@polywrap/client-js/build/types";
import { PluginPackage } from "@polywrap/plugin-js";
import { ResultErr, ResultOk } from "@polywrap/result";
import { Env, Logger, Workspace } from "@evo-ninja/agent-utils";
import * as path from "path-browserify"
import axios from "axios";

export class WrapClient extends PolywrapClient {

Expand All @@ -21,8 +21,8 @@ export class WrapClient extends PolywrapClient {
constructor(
workspace: Workspace,
logger: Logger,
agentPlugin: IWrapPackage | undefined = undefined,
env: Env
agentPlugin?: IWrapPackage,
env?: Env
) {
const builder = new PolywrapClientConfigBuilder()
.addBundle("web3")
Expand Down Expand Up @@ -185,7 +185,7 @@ export class WrapClient extends PolywrapClient {
"search": async (args: { query: string }) => {
const axiosClient = axios.create({ baseURL: 'https://api.search.brave.com/res/v1/web' });

const apiKey = env.BRAVE_API_KEY
const apiKey = env?.BRAVE_API_KEY

if (!apiKey) {
throw new Error('BRAVE_API_KEY environment variable is required to use the websearch plugin. See env.template for help')
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions packages/dev/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/__tests__/test-cases/
25 changes: 25 additions & 0 deletions packages/dev/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@evo-ninja/dev-agent",
"version": "0.1.0",
"license": "MIT",
"main": "./build/index.js",
"scripts": {
"build": "rimraf build && tsc",
"test": "jest --runInBand --verbose --rootDir . --config ../../jest.config.js"
},
"dependencies": {
"@polywrap/result": "~0.12.0",
"@evo-ninja/agent-utils": "~0.1.0"
},
"devDependencies": {
"@types/jest": "29.5.0",
"@evo-ninja/agent-debug": "~0.1.0",
"@evo-ninja/agent-utils-fs": "~0.1.0",
"gpt-tokenizer": "~2.1.1",
"jest": "29.5.0",
"rimraf": "~5.0.1",
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"typescript": "4.9.5"
}
}
15 changes: 15 additions & 0 deletions packages/dev/src/AgentContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
Chat,
LlmApi,
Scripts,
WrapClient,
Workspace
} from "@evo-ninja/agent-utils";

export interface AgentContext {
llm: LlmApi;
chat: Chat;
workspace: Workspace;
scripts: Scripts;
client: WrapClient;
}
73 changes: 73 additions & 0 deletions packages/dev/src/DevAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { AgentContext } from "./AgentContext";
import { agentFunctions } from "./agent-functions";
import { INITIAL_PROMP, GOAL_PROMPT, LOOP_PREVENTION_PROMPT } from "./prompts";

import {
Agent,
AgentOutput,
Chat,
RunResult,
LlmApi,
Scripts,
Logger,
Workspace,
WrapClient,
agentPlugin,
basicFunctionCallLoop,
ExecuteAgentFunctionCalled
} from "@evo-ninja/agent-utils";
import { ResultErr } from "@polywrap/result";

export class DevAgent implements Agent {
private readonly context: AgentContext;

constructor(
llm: LlmApi,
chat: Chat,
workspace: Workspace,
scripts: Scripts,
private readonly logger: Logger
) {
this.context = {
llm,
chat,
scripts,
workspace,
client: new WrapClient(
workspace,
this.logger,
agentPlugin({ logger: this.logger })
),
};
}

public get workspace(): Workspace {
return this.context.workspace;
}

public async* run(
goal: string
): AsyncGenerator<AgentOutput, RunResult, string | undefined> {
const { chat } = this.context;
try {
chat.persistent("system", INITIAL_PROMP());
chat.persistent("user", GOAL_PROMPT(goal));

return yield* basicFunctionCallLoop(
this.context,
agentFunctions,
(functionCalled: ExecuteAgentFunctionCalled) => {
const terminationFunctions = [
"agent_onGoalAchieved",
"agent_onGoalFailed"
];
return terminationFunctions.includes(functionCalled.name);
},
LOOP_PREVENTION_PROMPT
);
} catch (err) {
this.logger.error(err);
return ResultErr("Unrecoverable error encountered.");
}
}
}
Loading

0 comments on commit 4b60f38

Please sign in to comment.