-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add basic server * Add changeset * Move `createServer()` to a separate `@inngest/agent-kit/server` export * Add `name` as required to `Network` * Add `description` to `Network` * Simplify `createServer()` if `Network#name` is present Also use `slugify` from `inngest` * Fix build error; provide all `Network` opts --------- Co-authored-by: Jack Williams <[email protected]>
- Loading branch information
1 parent
d4a0d26
commit c8343c0
Showing
6 changed files
with
110 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inngest/agent-kit": minor | ||
--- | ||
|
||
Add basic AgentKit server to serve agents and networks as Inngest functions for easy testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Inngest, slugify, type InngestFunction } from "inngest"; | ||
import { createServer as createInngestServer } from "inngest/node"; | ||
import { type Agent } from "./agent"; | ||
import { type Network } from "./network"; | ||
|
||
/** | ||
* Create a server to serve Agents and Networks as Inngest functions | ||
* | ||
* @example | ||
* ```ts | ||
* import { createServer, createAgent, createNetwork } from "@inngest/agent-kit"; | ||
* | ||
* const myAgent = createAgent(...); | ||
* const myNetwork = createNetwork(...); | ||
* const server = createServer({ | ||
* agents: [myAgent], | ||
* networks: [myNetworks], | ||
* }); | ||
* server.listen(3000) | ||
* ``` | ||
* | ||
* @public | ||
*/ | ||
export const createServer = ({ | ||
appId = "agent-kit", | ||
networks = [], | ||
agents = [], | ||
}: { | ||
appId?: string; | ||
networks?: Network[]; | ||
agents?: Agent[]; | ||
}) => { | ||
const inngest = new Inngest({ id: appId }); | ||
|
||
const functions: { [keyof: string]: InngestFunction.Any } = {}; | ||
|
||
for (const agent of agents) { | ||
const slug = slugify(agent.name); | ||
const id = `agent-${slug}`; | ||
|
||
functions[id] = inngest.createFunction( | ||
{ id, name: agent.name }, | ||
{ event: `${appId}/${id}` }, | ||
async ({ event }) => { | ||
// eslint-disable-next-line | ||
return agent.run(event.data.input); | ||
} | ||
); | ||
} | ||
|
||
for (const network of networks) { | ||
const slug = slugify(network.name); | ||
const id = `network-${slug}`; | ||
|
||
functions[id] = inngest.createFunction( | ||
{ id, name: network.name }, | ||
{ event: `${appId}/${id}` }, | ||
async ({ event }) => { | ||
// eslint-disable-next-line | ||
return network.run(event.data.input); | ||
} | ||
); | ||
} | ||
|
||
return createInngestServer({ | ||
client: inngest, | ||
functions: Object.values(functions), | ||
}); | ||
}; |