Skip to content

Commit

Permalink
fix(js-sdk): Got testing suite working with current sdk (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
jzanecook committed Apr 2, 2024
1 parent e2edf1d commit 1f00196
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/js/examples/minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ async function taskHandler(taskInput: TaskInput | null): Promise<StepHandler> {
return stepHandler
}

Agent.handleTask(taskHandler).start()
Agent.handleTask(taskHandler, {}).start()
45 changes: 39 additions & 6 deletions packages/sdk/js/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const createAgentTask = async (
const task: Task = {
task_id: uuid(),
input: body?.input ?? null,
additional_input: body?.additional_input ?? {},
artifacts: [],
}
const stepHandler = await taskHandler(task.task_id, body?.input ?? null)
Expand All @@ -89,15 +90,47 @@ const registerCreateAgentTask: RouteRegisterFn = (router: Router) => {
* Lists all tasks that have been created for the agent.
* @returns Promise<string[]>
*/
export const listAgentTaskIDs = async (): Promise<string[]> => {
return tasks.map(([task, _]) => task.task_id)
export const listAgentTasks = async (): Promise<Task[]> => {
return tasks.map(([task, _]) => task)
}
const registerListAgentTaskIDs: RouteRegisterFn = (router: Router) => {
const registerListAgentTasks: RouteRegisterFn = (router: Router) => {
router.get('/agent/tasks', (req, res) => {
void (async () => {
try {
const ids = await listAgentTaskIDs()
res.status(200).json(ids)
const tasks = await listAgentTasks()

let currentPage = 1
let pageSize = 10
if (
req.query.current_page !== undefined &&
!isNaN(Number(req.query.current_page))
) {
currentPage = Number(req.query.current_page)
}

if (
req.query.page_size !== undefined &&
!isNaN(Number(req.query.page_size))
) {
pageSize = Number(req.query.page_size)
}

const totalItems = tasks.length
const totalPages = Math.ceil(totalItems / pageSize)

const start = (currentPage - 1) * pageSize
const end = start + pageSize
const pagedTasks = tasks.slice(start, end)

res.status(200).json({
tasks: pagedTasks,
pagination: {
total_items: totalItems,
total_pages: totalPages,
current_page: currentPage,
page_size: pageSize,
}
})
} catch (err: Error | any) {
console.error(err)
res.status(500).json({ error: err.message })
Expand Down Expand Up @@ -471,7 +504,7 @@ export class Agent {
port: port ?? this.config.port ?? defaultAgentConfig.port,
routes: [
registerCreateAgentTask,
registerListAgentTaskIDs,
registerListAgentTasks,
registerGetAgentTask,
registerListAgentTaskSteps,
registerExecuteAgentTaskStep,
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
type TaskHandler,
StepResultWithDefaults,
createAgentTask,
listAgentTaskIDs,
listAgentTasks,
getAgentTask,
listAgentTaskSteps,
executeAgentTaskStep,
Expand All @@ -34,7 +34,7 @@ export {
type TaskHandler,
StepResultWithDefaults as StepResult,
createAgentTask,
listAgentTaskIDs,
listAgentTasks,
getAgentTask,
listAgentTaskSteps,
executeAgentTaskStep,
Expand Down
3 changes: 3 additions & 0 deletions packages/sdk/js/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Input parameters for the task. Any value is allowed.
*/
export type TaskInput = any
export type AdditionalInput = any

/**
* Artifact that the task has produced. Any value is allowed.
Expand Down Expand Up @@ -87,6 +88,7 @@ export interface StepResult {

export interface Task {
input?: TaskInput
additional_input?: AdditionalInput
/**
* The ID of the task.
*/
Expand All @@ -99,4 +101,5 @@ export interface Task {

export interface TaskRequestBody {
input?: TaskInput
additional_input?: AdditionalInput
}

0 comments on commit 1f00196

Please sign in to comment.