Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

agent-protocol

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

agent-protocol - npm Package Compare versions

Comparing version 0.1.2 to 1.0.1

125

dist/index.d.ts

@@ -0,12 +1,60 @@

export { v4 } from 'uuid';
/**
* Input parameters for the task. Any value is allowed.
*/
type TaskInput = any;
type Artifact = any;
/**
* Artifact that the task has produced. Any value is allowed.
*/
type Artifact = {
artifact_id: string;
agent_created: boolean;
file_name: string;
relative_path: string | null;
created_at: string;
};
/**
* Input parameters for the task step. Any value is allowed.
*/
type StepInput = any;
/**
* Output that the task step has produced. Any value is allowed.
*/
type StepOutput = any;
declare enum StepStatus {
CREATED = "created",
RUNNING = "running",
COMPLETED = "completed"
}
interface Step {
/**
* The name of the task step
*/
name?: string;
/**
* Output of the task step
*/
output?: StepOutput;
/**
* A list of artifacts that the step has produced.
*/
artifacts?: Artifact[];
/**
* Whether this is the last step in the task.
*/
is_last?: boolean;
input?: StepInput;
/**
* The ID of the task this step belongs to.
*/
task_id: string;
/**
* The ID of the task step.
*/
step_id: string;
/**
* Current status of step
*/
status: StepStatus;
}

@@ -17,4 +65,17 @@ interface StepRequestBody {

interface StepResult {
/**
* The name of the step
*/
name?: string;
/**
* Output of the step
*/
output?: StepOutput;
/**
* A list of artifacts that the step has produced.
*/
artifacts?: Artifact[];
/**
* Whether this is the last step in the task.
*/
is_last?: boolean;

@@ -24,3 +85,9 @@ }

input?: TaskInput;
/**
* The ID of the task.
*/
task_id: string;
/**
* A list of artifacts that the task has produced.
*/
artifacts?: Artifact[];

@@ -32,4 +99,16 @@ }

/**
* A function that handles a step in a task.
* Returns a step result.
*/
type StepHandler = (input: StepInput | null) => Promise<StepResult>;
type TaskHandler = (input: TaskInput | null) => Promise<StepHandler>;
/**
* A function that handles a task.
* Returns a step handler.
*/
type TaskHandler = (taskId: String, input: TaskInput | null) => Promise<StepHandler>;
/**
* A step result with default values.
* @returns StepResult
*/
declare class StepResultWithDefaults implements StepResult {

@@ -40,13 +119,51 @@ output?: StepOutput;

}
/**
* Creates a task for the agent.
* @param body TaskRequestBody | null
* @returns Promise<Task>
*/
declare const createAgentTask: (body: TaskRequestBody | null) => Promise<Task>;
/**
* Lists all tasks that have been created for the agent.
* @returns Promise<string[]>
*/
declare const listAgentTaskIDs: () => Promise<string[]>;
/**
* Get details about a specified agent task.
* @param taskId string
* @returns
*/
declare const getAgentTask: (taskId: string) => Promise<Task>;
/**
* Lists all steps for the specified task.
* @param taskId string
* @returns Promise<string[]>
*/
declare const listAgentTaskSteps: (taskId: string) => Promise<string[]>;
/**
* Execute a step in the specified agent task.
* @param taskId string
* @param body StepRequestBody | null
* @returns Promise<Step>
*/
declare const executeAgentTaskStep: (taskId: string, body: StepRequestBody | null) => Promise<Step>;
/**
* Get details about a specified task step.
* @param taskId string
* @param stepId string
* @returns Promise<Step>
*/
declare const getAgentTaskStep: (taskId: string, stepId: string) => Promise<Step>;
interface AgentConfig {
port: number;
workspace: string;
}
declare class Agent {
static handleTask(handler: TaskHandler): typeof Agent;
static start(port?: number): void;
taskHandler: TaskHandler;
config: AgentConfig;
constructor(taskHandler: TaskHandler, config: AgentConfig);
static handleTask(_taskHandler: TaskHandler, config: Partial<AgentConfig>): Agent;
start(port?: number): void;
}
export { Artifact, Step, StepHandler, StepInput, StepOutput, StepRequestBody, StepResultWithDefaults as StepResult, Task, TaskHandler, TaskInput, TaskRequestBody, createAgentTask, Agent as default, executeAgentTaskStep, getAgentTask, getAgentTaskStep, listAgentTaskIDs, listAgentTaskSteps };

@@ -1,13 +0,14 @@

var q=Object.create;var c=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var $=Object.getPrototypeOf,E=Object.prototype.hasOwnProperty;var B=(e,s)=>{for(var t in s)c(e,t,{get:s[t],enumerable:!0})},I=(e,s,t,r)=>{if(s&&typeof s=="object"||typeof s=="function")for(let n of x(s))!E.call(e,n)&&n!==t&&c(e,n,{get:()=>s[n],enumerable:!(r=v(s,n))||r.enumerable});return e};var l=(e,s,t)=>(t=e!=null?q($(e)):{},I(s||!e||!e.__esModule?c(t,"default",{value:e,enumerable:!0}):t,e)),D=e=>I(c({},"__esModule",{value:!0}),e);var C={};B(C,{StepResult:()=>u,createAgentTask:()=>y,default:()=>P,executeAgentTaskStep:()=>_,getAgentTask:()=>S,getAgentTaskStep:()=>A,listAgentTaskIDs:()=>g,listAgentTaskSteps:()=>T});module.exports=D(C);var w=l(require("express-openapi-validator")),p=l(require("express")),m=require("uuid"),R=l(require("js-yaml"));var k=`openapi: 3.0.1
var B=Object.create;var g=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var H=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var U=(e,t)=>{for(var a in t)g(e,a,{get:t[a],enumerable:!0})},j=(e,t,a,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of O(t))!L.call(e,r)&&r!==a&&g(e,r,{get:()=>t[r],enumerable:!(s=N(t,r))||s.enumerable});return e};var l=(e,t,a)=>(a=e!=null?B(H(e)):{},j(t||!e||!e.__esModule?g(a,"default",{value:e,enumerable:!0}):a,e)),W=e=>j(g({},"__esModule",{value:!0}),e);var rt={};U(rt,{StepResult:()=>h,createAgentTask:()=>S,default:()=>st,executeAgentTaskStep:()=>I,getAgentTask:()=>m,getAgentTaskStep:()=>q,listAgentTaskIDs:()=>v,listAgentTaskSteps:()=>w,v4:()=>C.v4});module.exports=W(rt);var y=require("uuid"),_=l(require("fs")),c=l(require("path"));var $=l(require("express-openapi-validator")),E=l(require("js-yaml")),o=l(require("express"));var A=`openapi: 3.0.1
info:
title: Agent Communication Protocol
title: Agent Protocol
description: Specification of the API protocol for communication with an agent.
version: 'v1'
version: v1
servers:
- url: 'http://0.0.0.0:8000'
description: Agent Protocol API
paths:
/agent/tasks:
/ap/v1/agent/tasks:
post:
operationId: createAgentTask
summary: Creates a task for the agent.
tags: [agent]
operationId: createAgentTask
requestBody:

@@ -19,4 +20,4 @@ content:

responses:
200:
description: A new agent task was successfuly created.
'200':
description: A new agent task was successfully created.
content:

@@ -26,27 +27,51 @@ application/json:

$ref: '#/components/schemas/Task'
x-postman-variables:
- type: save
name: task_id
path: .task_id
'422':
$ref: '#/components/responses/UnprocessableEntity'
default:
description: Internal Server Error
tags:
- agent
get:
operationId: listAgentTasks
summary: List all tasks that have been created for the agent.
tags: [agent]
operationId: listAgentTasksIDs
parameters:
- name: current_page
in: query
description: Page number
required: false
schema:
type: integer
format: int32
default: 1
minimum: 1
example: 2
- name: page_size
in: query
description: Number of items per page
required: false
schema:
type: integer
format: int32
default: 10
minimum: 1
example: 25
responses:
200:
description: Returned list of agent's task IDs.
'200':
description: Returned list of agent's tasks.
content:
application/json:
schema:
type: array
default: []
description: A list of task IDs
items:
type: string
$ref: '#/components/schemas/TaskListResponse'
default:
description: Internal Server Error
/agent/tasks/{task_id}:
tags:
- agent
'/ap/v1/agent/tasks/{task_id}':
get:
operationId: getAgentTask
summary: Get details about a specified agent task.
tags: [agent]
operationId: getAgentTask
parameters:

@@ -59,4 +84,8 @@ - name: task_id

type: string
example: 1d5a533e-3904-4401-8a07-c49adf88b981
x-postman-variables:
- type: load
name: task_id
responses:
200:
'200':
description: Returned details about an agent task.

@@ -67,34 +96,59 @@ content:

$ref: '#/components/schemas/Task'
'404':
$ref: '#/components/responses/NotFound'
default:
description: Internal Server Error
/agent/tasks/{task_id}/steps:
tags:
- agent
'/ap/v1/agent/tasks/{task_id}/steps':
get:
operationId: listAgentTaskSteps
summary: List all steps for the specified task.
tags: [agent]
operationId: listAgentTaskSteps
parameters:
- name: task_id
in: path
description: ID of the task
description: ID of the task.
required: true
schema:
type: string
example: 50da533e-3904-4401-8a07-c49adf88b5eb
x-postman-variables:
- type: load
name: task_id
- name: current_page
in: query
description: Page number
required: false
schema:
type: integer
format: int32
default: 1
minimum: 1
example: 2
- name: page_size
in: query
description: Number of items per page
required: false
schema:
type: integer
format: int32
default: 10
minimum: 1
example: 25
responses:
200:
description: Returned list of agent's step IDs for the specified task.
'200':
description: Returned list of agent's steps for the specified task.
content:
application/json:
schema:
type: array
default: []
description: A list of step IDs for the task
items:
type: string
$ref: '#/components/schemas/TaskStepsListResponse'
'404':
$ref: '#/components/responses/NotFound'
default:
description: Internal Server Error
tags:
- agent
post:
operationId: executeAgentTaskStep
summary: Execute a step in the specified agent task.
tags: [agent]
operationId: executeAgentTaskStep
parameters:

@@ -107,2 +161,6 @@ - name: task_id

type: string
example: 50da533e-3904-4401-8a07-c49adf88b5eb
x-postman-variables:
- type: load
name: task_id
requestBody:

@@ -114,3 +172,3 @@ content:

responses:
200:
'200':
description: Executed step for the agent task.

@@ -121,10 +179,18 @@ content:

$ref: '#/components/schemas/Step'
x-postman-variables:
- type: save
name: step_id
path: .step_id
'404':
$ref: '#/components/responses/NotFound'
'422':
$ref: '#/components/responses/UnprocessableEntity'
default:
description: Internal Server Error
/agent/tasks/{task_id}/steps/{step_id}:
tags:
- agent
'/ap/v1/agent/tasks/{task_id}/steps/{step_id}':
get:
operationId: getAgentTaskStep
summary: Get details about a specified task step.
tags: [agent]
operationId: getAgentTaskStep
parameters:

@@ -137,2 +203,8 @@ - name: task_id

type: string
example: 50da533e-3904-4401-8a07-c49adf88b5eb
x-postman-variables:
- type: load
name: task_id
- type: load
name: step_id
- name: step_id

@@ -144,4 +216,8 @@ in: path

type: string
example: 28ca533e-3904-4401-8a07-c49adf8891c2
x-postman-variables:
- type: load
name: step_id
responses:
200:
'200':
description: Returned details about an agent task step.

@@ -152,26 +228,261 @@ content:

$ref: '#/components/schemas/Step'
'404':
$ref: '#/components/responses/NotFound'
default:
description: Internal Server Error
tags:
- agent
'/ap/v1/agent/tasks/{task_id}/artifacts':
get:
operationId: listAgentTaskArtifacts
summary: List all artifacts that have been created for the given task.
parameters:
- name: task_id
in: path
description: ID of the task
required: true
schema:
type: string
example: 50da533e-3904-4401-8a07-c49adf88b5eb
x-postman-variables:
- type: load
name: task_id
- name: current_page
in: query
description: Page number
required: false
schema:
type: integer
format: int32
default: 1
minimum: 1
example: 2
- name: page_size
in: query
description: Number of items per page
required: false
schema:
type: integer
format: int32
default: 10
minimum: 1
example: 25
responses:
'200':
description: Returned the list of artifacts for the task.
content:
application/json:
schema:
$ref: '#/components/schemas/TaskArtifactsListResponse'
'404':
$ref: '#/components/responses/NotFound'
default:
description: Internal Server Error
tags:
- agent
post:
operationId: uploadAgentTaskArtifacts
summary: Upload an artifact for the specified task.
parameters:
- name: task_id
in: path
description: ID of the task
required: true
schema:
type: string
example: 50da533e-3904-4401-8a07-c49adf88b5eb
x-postman-variables:
- type: load
name: task_id
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/ArtifactUpload'
responses:
'200':
description: Returned the content of the artifact.
content:
application/json:
schema:
$ref: '#/components/schemas/Artifact'
'404':
$ref: '#/components/responses/NotFound'
default:
description: Internal Server Error
tags:
- agent
'/ap/v1/agent/tasks/{task_id}/artifacts/{artifact_id}':
get:
operationId: downloadAgentTaskArtifact
summary: Download a specified artifact.
parameters:
- name: task_id
in: path
description: ID of the task
required: true
schema:
type: string
example: 50da533e-3904-4401-8a07-c49adf88b5eb
x-postman-variables:
- type: load
name: task_id
- name: artifact_id
in: path
description: ID of the artifact
required: true
schema:
type: string
example: 1e41533e-3904-4401-8a07-c49adf8893de
responses:
'200':
description: Returned the content of the artifact.
content:
application/octet-stream:
schema:
type: string
format: binary
'404':
$ref: '#/components/responses/NotFound'
default:
description: Internal Server Error
tags:
- agent
components:
schemas:
Pagination:
type: object
properties:
total_items:
description: Total number of items.
type: integer
example: 42
total_pages:
description: Total number of pages.
type: integer
example: 97
current_page:
description: Current_page page number.
type: integer
example: 1
page_size:
description: Number of items per page.
type: integer
example: 25
required:
- total_items
- total_pages
- current_page
- page_size
TaskListResponse:
type: object
properties:
tasks:
type: array
items:
$ref: '#/components/schemas/Task'
pagination:
$ref: '#/components/schemas/Pagination'
required:
- tasks
- pagination
TaskStepsListResponse:
type: object
properties:
steps:
type: array
items:
$ref: '#/components/schemas/Step'
pagination:
$ref: '#/components/schemas/Pagination'
required:
- steps
- pagination
TaskArtifactsListResponse:
type: object
properties:
artifacts:
type: array
items:
$ref: '#/components/schemas/Artifact'
pagination:
$ref: '#/components/schemas/Pagination'
required:
- artifacts
- pagination
TaskInput:
description: Input parameters for the task. Any value is allowed.
type: object
example: |-
{
"debug": false,
"mode": "benchmarks"
}
Artifact:
description: Artifact that the task has produced. Any value is allowed.
description: An Artifact either created by or submitted to the agent.
type: object
properties:
artifact_id:
description: ID of the artifact.
type: string
example: b225e278-8b4c-4f99-a696-8facf19f0e56
agent_created:
description: Whether the artifact has been created by the agent.
type: boolean
example: false
file_name:
description: Filename of the artifact.
type: string
example: main.py
relative_path:
description: Relative path of the artifact in the agent's workspace.
type: string
example: python/code/
nullable: true
required:
- artifact_id
- agent_created
- file_name
ArtifactUpload:
description: Artifact to upload to the agent.
type: object
properties:
file:
description: File to upload.
type: string
format: binary
example: binary representation of file
relative_path:
description: Relative path of the artifact in the agent's workspace.
type: string
example: python/code
required:
- file
StepInput:
description: Input parameters for the task step. Any value is allowed.
type: object
example: |-
{
"file_to_refactor": "models.py"
}
StepOutput:
description: Output that the task step has produced. Any value is allowed.
type: object
example: |-
{
"tokens": 7894,
"estimated_cost": "0,24$"
}
nullable: true
TaskRequestBody:
description: Body of the task request.
type: object
description: Body of the task request.
properties:
input:
description: Input prompt for the task.
type: string
example: Write 'Washington' to the file 'output.txt'.
nullable: true
additional_input:
$ref: '#/components/schemas/TaskInput'
Task:

@@ -183,3 +494,4 @@ allOf:

required:
- 'task_id'
- task_id
- artifacts
properties:

@@ -189,43 +501,33 @@ task_id:

type: string
example: 50da533e-3904-4401-8a07-c49adf88b5eb
artifacts:
description: A list of artifacts that the task has produced.
type: array
default: []
description: A list of artifacts that the task has produced.
items:
$ref: '#/components/schemas/Artifact'
example:
- 7a49f31c-f9c6-4346-a22c-e32bc5af4d8e
- ab7b4091-2560-4692-a4fe-d831ea3ca7d6
default: []
StepRequestBody:
description: Body of the task request.
type: object
description: Body of the task request.
properties:
input:
description: Input prompt for the step.
type: string
example: Write the words you receive to the file 'output.txt'.
nullable: true
additional_input:
$ref: '#/components/schemas/StepInput'
StepResult:
description: Result of the task step.
type: object
required:
- 'artifacts'
properties:
output:
$ref: '#/components/schemas/StepOutput'
artifacts:
type: array
default: []
description: A list of artifacts that the step has produced.
items:
$ref: '#/components/schemas/Artifact'
is_last:
description: Whether this is the last step in the task.
type: boolean
default: false
Step:
allOf:
- $ref: '#/components/schemas/StepRequestBody'
- $ref: '#/components/schemas/StepResult'
- type: object
required:
- 'step_id'
- 'task_id'
- step_id
- task_id
- status
- is_last
- artifacts
properties:

@@ -235,6 +537,60 @@ task_id:

type: string
example: 50da533e-3904-4401-8a07-c49adf88b5eb
step_id:
description: The ID of the task step.
type: string
`;var a=(0,p.default)();a.use(p.default.json());a.use(p.default.text());a.use(p.default.urlencoded({extended:!1}));a.get("/openapi.yaml",(e,s)=>{s.setHeader("Content-Type","text/yaml").status(200).send(k)});var O=R.default.load(k);a.use(w.middleware({apiSpec:O,validateRequests:!0,validateResponses:!0}));var i=[],h=[],f=null,u=class{constructor(){this.output=null;this.artifacts=[];this.is_last=!1}},y=async e=>{if(f==null)throw new Error("Task handler not defined");let s=await f((e==null?void 0:e.input)??null),t={task_id:(0,m.v4)(),input:(e==null?void 0:e.input)??null,artifacts:[]};return i.push([t,s]),t};a.post("/agent/tasks",(e,s)=>{(async()=>{try{let t=await y(e.body);s.status(200).json(t)}catch(t){console.error(t),s.status(500).json({error:t.message})}})()});var g=async()=>i.map(([e,s])=>e.task_id);a.get("/agent/tasks",(e,s)=>{(async()=>{try{let t=await g();s.status(200).json(t)}catch(t){console.error(t),s.status(500).json({error:t.message})}})()});var S=async e=>{let s=i.find(([t,r])=>t.task_id===e);if(s==null)throw new Error(`Task with id ${e} not found`);return s[0]};a.get("/agent/tasks/:task_id",(e,s)=>{(async()=>{try{let t=await S(e.params.task_id);s.status(200).json(t)}catch(t){console.error(t),s.status(500).json({error:t.message})}})()});var T=async e=>{if(i.find(([t,r])=>t.task_id===e)==null)throw new Error(`Task with id ${e} not found`);return h.filter(t=>t.task_id===e).map(t=>t.step_id)};a.get("/agent/tasks/:task_id/steps",(e,s)=>{(async()=>{try{let t=await T(e.params.task_id);s.status(200).json(t)}catch(t){console.error(t),s.status(500).json({error:t.message})}})()});var _=async(e,s)=>{let t=i.find(([j,G])=>j.task_id===e);if(t==null)throw new Error(`Task with id ${e} not found`);let r=t[1],n=await r((s==null?void 0:s.input)??null),o={task_id:e,step_id:(0,m.v4)(),input:(s==null?void 0:s.input)??null,output:n.output??null,artifacts:n.artifacts??[],is_last:n.is_last??!1};return o.artifacts!=null&&(t[0].artifacts==null||t[0].artifacts.length===0?t[0].artifacts=o.artifacts:t[0].artifacts.push(...o.artifacts)),h.push(o),o};a.post("/agent/tasks/:task_id/steps",(e,s)=>{(async()=>{try{let t=await _(e.params.task_id,e.body);s.status(200).json(t)}catch(t){console.error(t),s.status(500).json({error:t.message})}})()});var A=async(e,s)=>{let t=h.find(r=>r.task_id===e&&r.step_id===s);if(t==null)throw new Error(`Step with task id ${e} and step id ${s} not found`);return t};a.get("/agent/tasks/:task_id/steps/:step_id",(e,s)=>{(async()=>{try{let t=await A(e.params.task_id,e.params.step_id);s.status(200).json(t)}catch(t){console.error(t),s.status(500).json({error:t.message})}})()});var d=class{static handleTask(s){return f=s,this}static start(s=8e3){a.listen(s,()=>{console.log(`Agent listening at http://localhost:${s}`)})}};var P=d;0&&(module.exports={StepResult,createAgentTask,executeAgentTaskStep,getAgentTask,getAgentTaskStep,listAgentTaskIDs,listAgentTaskSteps});
example: 6bb1801a-fd80-45e8-899a-4dd723cc602e
name:
description: The name of the task step.
type: string
example: Write to file
nullable: true
status:
description: The status of the task step.
type: string
example: created
enum:
- created
- running
- completed
output:
description: Output of the task step.
type: string
example: 'I am going to use the write_to_file command and write Washington to a file called output.txt <write_to_file(''output.txt'', ''Washington'')'
nullable: true
additional_output:
$ref: '#/components/schemas/StepOutput'
artifacts:
description: A list of artifacts that the step has produced.
type: array
items:
$ref: '#/components/schemas/Artifact'
default: []
is_last:
description: Whether this is the last step in the task.
type: boolean
example: true
default: false
responses:
UnprocessableEntity:
description: Unable to process request. Likely due to improperly formatted request.
content:
application/json:
schema:
description: A generic JSON object without any specific requirements.
type: object
NotFound:
description: Unable to find entity with a given identifier
content:
application/json:
schema:
type: object
properties:
message:
description: Message stating the entity was not found
type: string
example: Unable to find entity with the provided id
required:
- message
`;var P=e=>{let t=(0,o.default)();t.use(o.default.json()),t.use(o.default.text()),t.use(o.default.urlencoded({extended:!1}));let a=E.default.load(A);t.use($.middleware({apiSpec:a,validateRequests:!0,validateResponses:!0})),t.get("/openapi.yaml",(r,n)=>{n.setHeader("Content-Type","text/yaml").status(200).send(A)});let s=(0,o.Router)();e.routes.map(r=>{r(s,e.context)}),t.use("/ap/v1",s),t.listen(e.port,e.callback)};var d=[],R=[],T=null,h=class{output=null;artifacts=[];is_last=!1},S=async e=>{if(T==null)throw new Error("Task handler not defined");let t={task_id:(0,y.v4)(),input:(e==null?void 0:e.input)??null,artifacts:[]},a=await T(t.task_id,(e==null?void 0:e.input)??null);return d.push([t,a]),t},G=e=>{e.post("/agent/tasks",(t,a)=>{(async()=>{try{let s=await S(t.body);a.status(200).json(s)}catch(s){console.error(s),a.status(500).json({error:s.message})}})()})},v=async()=>d.map(([e,t])=>e.task_id),M=e=>{e.get("/agent/tasks",(t,a)=>{(async()=>{try{let s=await v();a.status(200).json(s)}catch(s){console.error(s),a.status(500).json({error:s.message})}})()})},m=async e=>{let t=d.find(([a,s])=>a.task_id===e);if(t==null)throw new Error(`Task with id ${e} not found`);return t[0]},V=e=>{e.get("/agent/tasks/:task_id",(t,a)=>{(async()=>{try{let s=await m(t.params.task_id);a.status(200).json(s)}catch(s){console.error(s),a.status(500).json({error:s.message})}})()})},w=async e=>{if(d.find(([a,s])=>a.task_id===e)==null)throw new Error(`Task with id ${e} not found`);return R.filter(a=>a.task_id===e).map(a=>a.step_id)},J=e=>{e.get("/agent/tasks/:task_id/steps",(t,a)=>{(async()=>{try{let s=await w(t.params.task_id);a.status(200).json(s)}catch(s){console.error(s),a.status(500).json({error:s.message})}})()})},I=async(e,t)=>{let a=d.find(([i,p])=>i.task_id===e);if(a==null)throw new Error(`Task with id ${e} not found`);let s=a[1],r=await s((t==null?void 0:t.input)??null),n={task_id:e,step_id:(0,y.v4)(),input:(t==null?void 0:t.input)??null,name:r.name,output:r.output??null,artifacts:r.artifacts??[],is_last:r.is_last??!1,status:"completed"};return n.artifacts&&n.artifacts.length>0&&(a[0].artifacts=a[0].artifacts??[],a[0].artifacts.push(...n.artifacts)),R.push(n),n},K=e=>{e.post("/agent/tasks/:task_id/steps",(t,a)=>{(async()=>{try{let s=await I(t.params.task_id,t.body);a.status(200).json(s)}catch(s){console.error(s),a.status(500).json({error:s.message})}})()})},q=async(e,t)=>{let a=R.find(s=>s.task_id===e&&s.step_id===t);if(a==null)throw new Error(`Step with task id ${e} and step id ${t} not found`);return a},Q=e=>{e.get("/agent/tasks/:task_id/steps/:step_id",(t,a)=>{(async()=>{try{let s=await q(t.params.task_id,t.params.step_id);a.status(200).json(s)}catch(s){console.error(s),a.status(500).json({error:s.message})}})()})},X=async e=>(await m(e)).artifacts,Y=e=>{e.get("/agent/tasks/:task_id/artifacts",(t,a)=>{(async()=>{let s=t.params.task_id;try{let r=await X(s),n=Number(t.query.current_page)||1,i=Number(t.query.page_size)||10;r||a.status(200).send({artifacts:[],pagination:{total_items:0,total_pages:0,current_page:n,page_size:i}});let p=r.length,x=Math.ceil(p/i),f=(n-1)*i,u=f+i,D=r.slice(f,u);a.status(200).send({artifacts:D,pagination:{total_items:p,total_pages:x,current_page:n,page_size:i}})}catch(r){console.error(r),a.status(404).json({error:r.message})}})()})},F=(e,t,a)=>{let s=c.isAbsolute(t)?t:c.join(process.cwd(),t);return c.join(s,e,a.relative_path??"",a.file_name)},Z=async(e,t,a,s)=>{let n={artifact_id:(0,y.v4)(),agent_created:!1,file_name:a.originalname,relative_path:s||null,created_at:Date.now().toString()};t.artifacts=t.artifacts||[],t.artifacts.push(n);let i=F(t.task_id,e,n);return _.mkdirSync(c.join(i,".."),{recursive:!0}),_.writeFileSync(i,a.buffer),n},tt=(e,t)=>{e.post("/agent/tasks/:task_id/artifacts",(a,s)=>{(async()=>{try{let r=a.params.task_id,n=a.body.relative_path,i=d.find(([{task_id:u}])=>u==r);i||s.status(404).json({message:"Unable to find task with the provided id"});let x=a.files.find(({fieldname:u})=>u=="file"),f=await Z(t.workspace,i[0],x,n);s.status(200).json(f)}catch(r){console.error(r),s.status(500).json({error:r.message})}})()})},et=async(e,t)=>{var r;let s=(r=(await m(e)).artifacts)==null?void 0:r.find(n=>n.artifact_id===t);if(!s)throw new Error(`Artifact with id ${t} in task with id ${e} was not found`);return s},at=(e,t)=>{e.get("/agent/tasks/:task_id/artifacts/:artifact_id",(a,s)=>{(async()=>{let r=a.params.task_id,n=a.params.artifact_id;try{let i=await et(r,n),p=F(r,t.workspace,i);s.status(200).sendFile(p)}catch(i){console.error(i),s.status(404).json({error:i.message})}})()})},b={port:8e3,workspace:"./workspace"},k=class e{constructor(t,a){this.taskHandler=t;this.config=a}static handleTask(t,a){return T=t,new e(t,{workspace:a.workspace||b.workspace,port:a.port||b.port})}start(t){let a={port:t||this.config.port||b.port,routes:[G,M,V,J,K,Q,Y,tt,at],callback:()=>{console.log(`Agent listening at http://localhost:${this.config.port}`)},context:{workspace:this.config.workspace}};P(a)}};var C=require("uuid");var st=k;0&&(module.exports={StepResult,createAgentTask,executeAgentTaskStep,getAgentTask,getAgentTaskStep,listAgentTaskIDs,listAgentTaskSteps,v4});
//# sourceMappingURL=index.js.map

16

package.json
{
"name": "agent-protocol",
"version": "0.1.2",
"version": "1.0.1",
"description": "API for interacting with Agent",
"homepage": "https://e2b.dev",
"homepage": "https://agentprotocol.ai",
"license": "MIT",
"author": {
"name": "e2b",
"email": "hello@e2b.dev",
"url": "https://e2b.dev"
"name": "aiengfoundation",
"email": "aiengfoundation@gmail.com",
"url": "https://aie.foundation"
},
"bugs": "https://github.com/e2b-dev/agent-protocol/issues",
"bugs": "https://github.com/AI-Engineer-Foundation/agent-protocol-sdk-js/issues",
"repository": {
"type": "git",
"url": "https://github.com/e2b-dev/agent-protocol/tree/main/agent/js"
"url": "https://github.com/AI-Engineer-Foundation/agent-protocol-sdk-js"
},

@@ -29,3 +29,2 @@ "main": "./dist/index.js",

"@types/node": "^20.4.1",
"@types/uuid": "^9.0.2",
"prettier": "^3.0.0",

@@ -54,2 +53,3 @@ "tsup": "^7.1.0",

"dependencies": {
"@types/uuid": "^9.0.2",
"express": "^4.18.2",

@@ -56,0 +56,0 @@ "express-openapi-validator": "^5.0.4",

@@ -41,83 +41,13 @@ # Agent Communication Protocol - JavaScript/TypeScript SDK

### Javascript
## Docs
```javascript
import Agent from 'agent-protocol'
You can find more info and examples in the [docs](https://agentprotocol.ai/sdks/js).
async function taskHandler(taskInput) {
console.log(`task: ${taskInput}`)
async function stepHandler(stepInput) {
console.log(`step: ${stepInput}`)
return {
output: stepInput,
}
}
return stepHandler
}
Agent.handleTask(taskHandler).start()
```
## Usage
To start the server run the file where you added the code above:
### Typescript
## Contributing
```bash
# Typescript
$ ts-node file/where/you/added/code.ts
# Javascript
$ node file/where/you/added/code.ts
git clone https://github.com/AI-Engineers-Foundation/agent-protocol-sdk-js
cd ./agent-protocol-sdk-js
git submodule update --init
npm install
npm run build
```
and then you can call the API using the following terminal commands:
To **create a task** run:
```bash
curl --request POST \
--url http://localhost:8000/agent/tasks \
--header 'Content-Type: application/json' \
--data '{
"input": "task-input-to-your-agent"
}'
```
You will get a response like this:
```json
{
"input": "task-input-to-your-agent",
"task_id": "e6d768bb-4c50-4007-9853-aeffb46c77be",
"artifacts": []
}
```
Then to **execute one step of the task** copy the `task_id` you got from the
previous request and run:
```bash
curl --request POST \
--url http://localhost:8000/agent/tasks/<task-id>/steps
```
To get a response like this:
```json
{
"output": "output-from-the-agent",
"artifacts": [],
"is_last": false,
"input": null,
"task_id": "e6d768bb-4c50-4007-9853-aeffb46c77be",
"step_id": "8ff8ba39-2c3e-4246-8086-fbd2a897240b"
}
```
## Examples
- [Minimal Typescript Example (Same as Above)](./examples/minimal.ts)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc