Skip to content

Commit

Permalink
feat: adding step api
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Oct 17, 2024
1 parent 39b5560 commit a29bfa6
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 22 deletions.
12 changes: 11 additions & 1 deletion src/api/query-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
SubscriptionOptions,
} from './nvm-backend'

export const SEARCH_TASKS_ENDPOINT = '/api/v1/agents/search'
export const SEARCH_TASKS_ENDPOINT = '/api/v1/agents/search/tasks'
export const SEARCH_STEPS_ENDPOINT = '/api/v1/agents/search/steps'
export const CREATE_STEPS_ENDPOINT = '/api/v1/agents/{did}/tasks/{taskId}/steps'
export const UPDATE_STEP_ENDPOINT = '/api/v1/agents/{did}/tasks/{taskId}/step/{stepId}'
export const GET_AGENTS_ENDPOINT = '/api/v1/agents'
Expand Down Expand Up @@ -117,6 +118,15 @@ export class AIQueryApi extends NVMBackendApi {
return this.post(SEARCH_TASKS_ENDPOINT, searchParams, { sendThroughProxy: false })
}

/**
* It search steps based on the search parameters
* @param searchParams - The search parameters
* @returns The result of the search query
*/
async searchSteps(searchParams: any) {
return this.post(SEARCH_STEPS_ENDPOINT, searchParams, { sendThroughProxy: false })
}

/**
* It retrieves all the steps that the agent needs to execute to complete a specific task.
* @param did - Agent DID
Expand Down
130 changes: 109 additions & 21 deletions tests/e2e/payments.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ describe('Payments API (e2e)', () => {
let accessToken: string
let proxyHost: string
let subscriberQueryOpts = {}
let balanceBefore: bigint
let completedTaskId: string
let completedTaskDID: string
let failedTaskId: string
let failedTaskDID: string
describe('Payments Setup', () => {
it('The Payments client can be initialized correctly', () => {
paymentsSubscriber = Payments.getInstance({
Expand Down Expand Up @@ -189,6 +194,7 @@ describe('Payments API (e2e)', () => {
console.log('Balance Result', balanceResult)
expect(balanceResult.isSubscriptor).toBeTruthy()
expect(BigInt(balanceResult.balance)).toBeGreaterThan(0)
balanceBefore = BigInt(balanceResult.balance)
})

it('I should be able to create a AI Task', async () => {
Expand Down Expand Up @@ -216,8 +222,8 @@ describe('Payments API (e2e)', () => {
})

describe('AI Builder Agent', () => {
let completedTaskId: string
let completedTaskDID: string

let taskCost: bigint
it('Builder should be able to fetch pending tasks', async () => {
const steps = await paymentsBuilder.query.getSteps(AgentExecutionStatus.Pending)
expect(steps).toBeDefined()
Expand All @@ -235,23 +241,42 @@ describe('Payments API (e2e)', () => {
}
await paymentsBuilder.query.subscribe(async (data) => {
console.log('TEST:: Step received', data)
expect(data).toBeDefined()
const step = JSON.parse(data)
console.log(step)
expect(data).toBeDefined()
const eventData = JSON.parse(data)
expect(eventData.step_id).toBeDefined()



stepsReceived++
const result = await paymentsBuilder.query.updateStep(step.did, step.task_id, step.step_id, {
step_id: step.step_id,
task_id: step.task_id,
step_status: AgentExecutionStatus.Completed,
is_last: true,
output: 'LFG!',
cost: 1
})
// expect(result.status).toBe(201)
if (result.status === 201) {
completedTaskId = step.task_id
completedTaskDID = step.did
let result
const searchResult = await paymentsBuilder.query.searchSteps({ step_id: eventData.step_id })
expect(searchResult.data.steps).toBeDefined()
const step = searchResult.data.steps[0]
if (step.input_query.startsWith('http')) {
console.log(`LISTENER :: Received URL ${step.input_query}`)
result = await paymentsBuilder.query.updateStep(step.did, step.task_id, step.step_id, {
step_id: step.step_id,
task_id: step.task_id,
step_status: AgentExecutionStatus.Completed,
is_last: true,
output: 'LFG!',
cost: 1
})
// expect(result.status).toBe(201)
if (result.status === 201) {
completedTaskId = step.task_id
completedTaskDID = step.did
}
} else {
console.log(`LISTENER :: Received Invalid URL ${step.input_query}`)
result = await paymentsBuilder.query.updateStep(step.did, step.task_id, step.step_id, {
step_id: step.step_id,
task_id: step.task_id,
step_status: AgentExecutionStatus.Failed,
is_last: true,
output: 'Invalid URL',
cost: 0
})
}

}, opts)
Expand Down Expand Up @@ -290,9 +315,27 @@ describe('Payments API (e2e)', () => {
subscriberQueryOpts
)
expect(result).toBeDefined()
console.log('Task with Steps', result)
// console.log('Task with Steps', result)
expect(result.status).toBe(200)
console.log('Task with Steps', result.data)
expect(result.data.task.cost).toBeDefined()
taskCost = BigInt(result.data.task.cost)
expect(taskCost).toBeGreaterThan(0)
})

it('I should be able to check that I consumed some credits', async () => {
const balanceResult = await paymentsSubscriber.getSubscriptionBalance(planDID)
expect(balanceResult).toBeDefined()
const balanceAfter = BigInt(balanceResult.balance)


expect(balanceAfter).toBeDefined()

console.log(`Balance Before: ${balanceBefore}`)
console.log(`Balance After: ${balanceAfter}`)
console.log(`Task Cost: ${taskCost}`)

expect(balanceAfter).toBeLessThan(balanceBefore)
})

it.skip('I should be able to end a task with a failure', () => {
Expand All @@ -302,11 +345,56 @@ describe('Payments API (e2e)', () => {



describe.skip('Subscriber validates Results', () => {
it('I should be able to receive the results of a Task created', () => {
describe('Failed tasks are free of charge', () => {
it('I should be able to create a wrong AI Task', async () => {
const aiTask = {
query: "this is not a URL !!!!",
name: "transcribe",
"additional_params": [],
"artifacts": []
}
const accessConfig = await paymentsSubscriber.getServiceAccessConfig(agentDID)
accessToken = accessConfig.accessToken
proxyHost = accessConfig.neverminedProxyUri
subscriberQueryOpts = {
accessToken,
proxyHost
}

const taskResult = await paymentsSubscriber.query.createTask(agentDID, aiTask, subscriberQueryOpts)

expect(taskResult).toBeDefined()
expect(taskResult.status).toBe(201)
console.log('Task Result', taskResult.data)
failedTaskDID = taskResult.data.task.did
failedTaskId = taskResult.data.task.task_id
console.log(`Failed Task DID: ${failedTaskDID}`)
console.log(`Failed TaskId: ${failedTaskId}`)
}, TEST_TIMEOUT)

it('I should be able to validate an AI task Failed', async () => {
const result = await paymentsSubscriber.query.getTaskWithSteps(
failedTaskDID,
failedTaskId,
subscriberQueryOpts
)
expect(result).toBeDefined()
console.log('Task with Steps', result)
expect(result.status).toBe(200)
// console.log('Task with Steps', result.data)
// expect(result.data.task.cost).toBeDefined()
// taskCost = BigInt(result.data.task.cost)
// expect(taskCost).toBeGreaterThan(0)
})

it('I should be charged only by the exact number of credits consumed', () => {
it('I should be charged only by the exact number of credits consumed', async () => {
const balanceResult = await paymentsSubscriber.getSubscriptionBalance(planDID)
expect(balanceResult).toBeDefined()
const balanceAfter = BigInt(balanceResult.balance)
expect(balanceAfter).toBeDefined()

console.log(`Balance Before: ${balanceBefore}`)
console.log(`Balance After: ${balanceBefore}`)
})
})

Expand Down

0 comments on commit a29bfa6

Please sign in to comment.