Skip to content

Commit

Permalink
Added Agent.build() and return app for use in serverless environmen…
Browse files Browse the repository at this point in the history
…ts (#99)
  • Loading branch information
nalbion committed Apr 2, 2024
1 parent c8209a1 commit 4310e7f
Show file tree
Hide file tree
Showing 8 changed files with 1,905 additions and 73 deletions.
1,869 changes: 1,803 additions & 66 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
"eslint-config-standard-with-typescript": "^36.1.0",
"husky": "^7.0.0",
"prettier": "^3.0.0",
"prettier-plugin-tailwindcss": "^0.5.11",
"typescript": "^5.1.6"
},
"dependencies": {
"express": "^4.18.2",
"express-openapi-validator": "^5.1.5",
"express-serve-static-core": "^0.1.1"
}
}
2 changes: 2 additions & 0 deletions packages/sdk/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ async function taskHandler(taskInput: TaskInput | null): Promise<StepHandler> {
Agent.handleTask(taskHandler).start()
```

See the [https://github.com/AI-Engineer-Foundation/agent-protocol/tree/main/packages/sdk/js/examples](examples folder) for running in serverless environments.

## Docs

You can find more info and examples in the [docs](https://agentprotocol.ai/sdks/js).
Expand Down
27 changes: 27 additions & 0 deletions packages/sdk/js/examples/aws-lambda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import awsServerlessExpress from 'aws-serverless-express'
import Agent, {
type StepResult,
type StepHandler,
type TaskInput,
type StepInput,
} from 'agent-protocol'

async function taskHandler(taskInput: TaskInput | null): Promise<StepHandler> {
console.log(`task: ${taskInput}`)

async function stepHandler(stepInput: StepInput | null): Promise<StepResult> {
console.log(`step: ${stepInput}`)
return {
output: stepInput,
}
}

return stepHandler
}

const app = Agent.handleTask(taskHandler, {}).build()
const server = awsServerlessExpress.createServer(app)

exports.handler = (event, context) => {
awsServerlessExpress.proxy(server, event, context)
}
24 changes: 24 additions & 0 deletions packages/sdk/js/examples/firebase-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { onRequest } from 'firebase-functions/v2/https'
import Agent, {
type StepResult,
type StepHandler,
type TaskInput,
type StepInput,
} from 'agent-protocol'

async function taskHandler(taskInput: TaskInput | null): Promise<StepHandler> {
console.log(`task: ${taskInput}`)

async function stepHandler(stepInput: StepInput | null): Promise<StepResult> {
console.log(`step: ${stepInput}`)
return {
output: stepInput,
}
}

return stepHandler
}

const app = Agent.handleTask(taskHandler, {}).build()

export const agentprotocol = onRequest(app)
23 changes: 23 additions & 0 deletions packages/sdk/js/examples/google-cloud-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import functions from '@google-cloud/functions-framework'
import Agent, {
type StepResult,
type StepHandler,
type TaskInput,
type StepInput,
} from 'agent-protocol'

async function taskHandler(taskInput: TaskInput | null): Promise<StepHandler> {
console.log(`task: ${taskInput}`)

async function stepHandler(stepInput: StepInput | null): Promise<StepResult> {
console.log(`step: ${stepInput}`)
return {
output: stepInput,
}
}

return stepHandler
}

const app = Agent.handleTask(taskHandler, {}).build()
functions.http('agentprotocol', app)
16 changes: 12 additions & 4 deletions packages/sdk/js/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
type RouteRegisterFn,
type RouteContext,
} from './api'
import { type Router } from 'express'
import { type Router, type Express } from 'express'

/**
* A function that handles a step in a task.
Expand Down Expand Up @@ -499,8 +499,18 @@ export class Agent {
})
}

build(port?: number): Express {
const config = this.buildApiConfig(port)
return createApi(config, false)
}

start(port?: number): void {
const config: ApiConfig = {
const config = this.buildApiConfig(port)
createApi(config)
}

private buildApiConfig(port?: number): ApiConfig {
return {
port: port ?? this.config.port ?? defaultAgentConfig.port,
routes: [
registerCreateAgentTask,
Expand All @@ -520,7 +530,5 @@ export class Agent {
workspace: this.config.workspace,
},
}

createApi(config)
}
}
11 changes: 8 additions & 3 deletions packages/sdk/js/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as OpenApiValidator from 'express-openapi-validator'
import yaml from 'js-yaml'
import express, { Router } from 'express' // <-- Import Router
import express, { type Express, Router } from 'express' // <-- Import Router
import type * as core from 'express-serve-static-core'

import spec from '../../../../schemas/openapi.yml'
Expand All @@ -20,7 +20,7 @@ export interface ApiConfig {
routes: RouteRegisterFn[]
}

export const createApi = (config: ApiConfig): void => {
export const createApi = (config: ApiConfig, start = true): Express => {
const app = express()

app.use(express.json())
Expand Down Expand Up @@ -48,5 +48,10 @@ export const createApi = (config: ApiConfig): void => {
})

app.use('/ap/v1', router)
app.listen(config.port, config.callback)

if (start) {
app.listen(config.port, config.callback)
}

return app
}

0 comments on commit 4310e7f

Please sign in to comment.