From 90bbfc8ebc45c9e22aa2687b558cc2d5d41e86b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugues=20de=20Saxc=C3=A9?= Date: Mon, 13 May 2024 13:22:22 +0200 Subject: [PATCH 1/3] feat: add getSteps API --- src/api.ts | 78 ++++++++++++++++++++++++++++++++--- src/filter.ts | 16 +++++++ tests/integration/api.test.ts | 25 +++++++++++ 3 files changed, 114 insertions(+), 5 deletions(-) diff --git a/src/api.ts b/src/api.ts index a97842f..881264c 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 (conversations) 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..216ee7b 100644 --- a/tests/integration/api.test.ts +++ b/tests/integration/api.test.ts @@ -227,6 +227,31 @@ 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(); + + const steps = await client.api.getSteps({ + filters: [ + { + 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 From 9e26bc10e0008e999a04d4b81373122f13f3007c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugues=20de=20Saxc=C3=A9?= Date: Mon, 13 May 2024 13:23:34 +0200 Subject: [PATCH 2/3] feat: update comment --- src/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api.ts b/src/api.ts index 881264c..28f256a 100644 --- a/src/api.ts +++ b/src/api.ts @@ -441,7 +441,7 @@ export class API { } /** - * Retrieves a paginated list of steps (conversations) based on the provided criteria. + * 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) From 3d7b831d594151d4bf8742a5af765b61f27ec4aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugues=20de=20Saxc=C3=A9?= Date: Mon, 13 May 2024 15:08:42 +0200 Subject: [PATCH 3/3] feat: make test more siloed wrt other tests --- tests/integration/api.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/integration/api.test.ts b/tests/integration/api.test.ts index 216ee7b..442bfb3 100644 --- a/tests/integration/api.test.ts +++ b/tests/integration/api.test.ts @@ -237,8 +237,17 @@ describe('End to end tests for the SDK', function () { }) .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',