Skip to content

Commit

Permalink
Fix network -> agent tate in run (#27)
Browse files Browse the repository at this point in the history
* Fix network -> agent tate in run

For some reason, we were not using the actual run state when calling
agent.run in networks.  This is via a large refactor.

* Changeset
  • Loading branch information
tonyhb authored Dec 19, 2024
1 parent 6c7d3b6 commit 9f302e4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-jeans-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inngest/agent-kit": patch
---

Network state flow to agents
7 changes: 6 additions & 1 deletion examples/swebench/inngest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { execSync } from "child_process";
import fs from "fs";
import { EventSchemas, Inngest } from "inngest";
import { z } from "zod";
import { State } from "@inngest/agent-kit";
import { codeWritingNetwork } from "./networks/codeWritingNetwork";

export const inngest = new Inngest({
Expand Down Expand Up @@ -49,8 +50,12 @@ export const fn = inngest.createFunction(
execSync(`cd ${dir} && git reset --hard FETCH_HEAD`);
});

// Create new state and store the repo in KV for access via tools.
const state = new State();
state.kv.set("repo", event.data.repo);

await codeWritingNetwork.run(event.data.problem_statement, {
state: { repo: event.data.repo },
state,
});
}
);
7 changes: 3 additions & 4 deletions src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type AiAdapter } from "inngest";
import { createAgenticModelFromAiAdapter, type AgenticModel } from "./model";
import { type Network } from "./network";
import { NetworkRun } from "./networkRun";
import {
InferenceResult,
Expand Down Expand Up @@ -119,8 +118,8 @@ export class Agent {
const p = createAgenticModelFromAiAdapter(rawModel);

// input state always overrides the network state.
const s = state || network?.defaultState?.clone();
const run = network && new NetworkRun(network, s || new State());
const s = state || network?.state?.clone() || new State();
const run = network && new NetworkRun(network, s);

let history = s ? s.format() : [];
let prompt = await this.agentPrompt(input, run);
Expand Down Expand Up @@ -361,7 +360,7 @@ export namespace Agent {

export interface RunOptions {
model?: AiAdapter.Any;
network?: Network;
network?: NetworkRun;
/**
* State allows you to pass custom state into a single agent run call. This should only
* be provided if you are running agents outside of a network. Networks automatically
Expand Down
4 changes: 4 additions & 0 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class State {
get: <T = any>(key: string) => T | undefined;
delete: (key: string) => boolean;
has: (key: string) => boolean;
all: () => Record<string, unknown>;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -96,6 +97,9 @@ export class State {
has: (key: string) => {
return this._kv.has(key);
},
all: () => {
return Object.fromEntries(this._kv);
},
};
}

Expand Down

0 comments on commit 9f302e4

Please sign in to comment.