diff --git a/src/api.ts b/src/api.ts index a97842f..28f256a 100644 --- a/src/api.ts +++ b/src/api.ts @@ -9,6 +9,8 @@ import { ParticipantsFilter, ScoresFilter, ScoresOrderBy, + StepsFilter, + StepsOrderBy, ThreadsFilter, ThreadsOrderBy } from './filter'; @@ -30,6 +32,7 @@ import { Prompt, Score, Step, + StepConstructor, StepType, User, Utils @@ -437,6 +440,71 @@ export class API { return this.makeGqlCall(query, variables); } + /** + * Retrieves a paginated list of steps (runs) based on the provided criteria. + * + * @param variables - The parameters to filter and paginate the steps. + * @param variables.first - The number of steps to retrieve after the cursor. (Optional) + * @param variables.after - The cursor to start retrieving steps after. (Optional) + * @param variables.before - The cursor to start retrieving steps before. (Optional) + * @param variables.filters - The filters to apply on the steps retrieval. (Optional) + * @param variables.orderBy - The order in which to retrieve the steps. (Optional) + * @returns A promise that resolves to a paginated response of steps. + */ + async getSteps(variables: { + first?: Maybe; + after?: Maybe; + before?: Maybe; + filters?: StepsFilter[]; + orderBy?: StepsOrderBy; + }): Promise>> { + const query = ` + query GetSteps( + $after: ID, + $before: ID, + $cursorAnchor: DateTime, + $filters: [stepsInputType!], + $orderBy: StepsOrderByInput, + $first: Int, + $last: Int, + $projectId: String, + ) { + steps( + after: $after, + before: $before, + cursorAnchor: $cursorAnchor, + filters: $filters, + orderBy: $orderBy, + first: $first, + last: $last, + projectId: $projectId, + ) { + pageInfo { + startCursor + endCursor + hasNextPage + hasPreviousPage + } + totalCount + edges { + cursor + node { + ${stepFields} + } + } + } + }`; + + const result = await this.makeGqlCall(query, variables); + + const response = result.data.steps; + + response.data = response.edges.map((x: any) => x.node); + delete response.edges; + + return response; + } + /** * Retrieves a step by its ID. * @@ -448,12 +516,12 @@ export class API { */ async getStep(id: string): Promise> { const query = ` - query GetStep($id: String!) { - step(id: $id) { - ${stepFields} + query GetStep($id: String!) { + step(id: $id) { + ${stepFields} + } } - } - `; + `; const variables = { id }; diff --git a/src/filter.ts b/src/filter.ts index 893ba25..43ec0ed 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -48,6 +48,22 @@ type OrderBy = { direction: 'ASC' | 'DESC'; }; +export type StepsFilter = + | Filter<'id', 'string'> + | Filter<'name', 'string', true> + | Filter<'input', 'json', true> + | Filter<'output', 'json', true> + | Filter<'participantIdentifiers', 'stringList', true> + | Filter<'startTime', 'datetime', true> + | Filter<'endTime', 'datetime', true> + | Filter<'metadata', 'json', true> + | Filter<'parentId', 'string', true> + | Filter<'threadId', 'string'> + | Filter<'error', 'string', true> + | Filter<'tags', 'stringList', true>; + +export type StepsOrderBy = OrderBy<'createdAt'>; + export type ThreadsFilter = | Filter<'id', 'string'> | Filter<'createdAt', 'datetime'> diff --git a/tests/integration/api.test.ts b/tests/integration/api.test.ts index 9d933f8..442bfb3 100644 --- a/tests/integration/api.test.ts +++ b/tests/integration/api.test.ts @@ -227,6 +227,40 @@ describe('End to end tests for the SDK', function () { expect(deletedStep).toBeNull(); }); + it('should test steps', async function () { + const thread = await client.thread({ id: uuidv4() }); + const step = await thread + .step({ + name: 'test', + type: 'run', + tags: ['to_score'] + }) + .send(); + + if (!step.id) { + throw new Error('Step id is null'); + } + + const steps = await client.api.getSteps({ + filters: [ + { + field: 'id', + operator: 'eq', + value: step.id + }, + { + field: 'tags', + operator: 'in', + value: ['to_score'] + } + ] + }); + expect(steps.data.length).toBe(1); + expect(steps.data[0].id).toBe(step.id); + + await client.api.deleteThread(thread.id); + }); + it('should test score', async function () { const thread = await client.thread({ id: uuidv4() }); const step = await thread