diff --git a/examples/indexes.ts b/examples/indexes.ts index ad03a03..0a7e97b 100644 --- a/examples/indexes.ts +++ b/examples/indexes.ts @@ -5,7 +5,10 @@ import Client from "../src"; const client = new Client(); (async () => { - const index = await client.indexes.create("support-tickets"); + let index = await client.indexes.get("support-tickets"); + if (!index) { + index = await client.indexes.create("support-tickets"); + } const tickets = [ { diff --git a/package.json b/package.json index 4fb144c..d7797be 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opperai", - "version": "0.5.2", + "version": "0.6.0", "description": "Typescript SDK for the Opper API", "main": "dist/index.js", "types": "dist/index.js", diff --git a/src/__tests__/__snapshots__/spans.test.ts.snap b/src/__tests__/__snapshots__/spans.test.ts.snap index 3976d4d..b87e72f 100644 --- a/src/__tests__/__snapshots__/spans.test.ts.snap +++ b/src/__tests__/__snapshots__/spans.test.ts.snap @@ -17,7 +17,6 @@ exports[`Spans span with child span Should create a parent and child span 1`] = "name": "child-span", "output": "child output", "parent_uuid": "parent-uuid", - "project": "missing_project", "start_time": 2020-04-01T00:00:00.000Z, "uuid": "return-a-uuid", } @@ -30,7 +29,6 @@ exports[`Spans span with child span Should create a parent and child span 2`] = "name": "parent-span", "output": "parent output", "parent_uuid": undefined, - "project": "missing_project", "start_time": 2020-04-01T00:00:00.000Z, "uuid": "return-a-uuid", } @@ -40,7 +38,6 @@ exports[`Spans startSpan should call fetch with correct parameters and resolve w { "input": "", "name": "test-span", - "project": "missing_project", "start_time": 2020-04-01T00:00:00.000Z, "uuid": "span-uuid", } diff --git a/src/__tests__/client.test.ts b/src/__tests__/client.test.ts index de2a4bc..6b6f776 100644 --- a/src/__tests__/client.test.ts +++ b/src/__tests__/client.test.ts @@ -96,13 +96,13 @@ describe("OpperAIClient", () => { it("should be able to list indexes", async () => { const mockIndexes: Index[] = [ { - id: 1, + uuid: "1", name: "Test Index 1", created_at: new Date(), files: [], }, { - id: 2, + uuid: "2", name: "Test Index 2", created_at: new Date(), files: [], diff --git a/src/__tests__/opperai-indexes.test.ts b/src/__tests__/opperai-indexes.test.ts index 7f02ed0..4499fb5 100644 --- a/src/__tests__/opperai-indexes.test.ts +++ b/src/__tests__/opperai-indexes.test.ts @@ -17,13 +17,13 @@ describe("OpperAIIndexes", () => { const mockIndexes: Index[] = [ { - id: 1, + uuid: "1", name: "Test Index 1", created_at: new Date(), files: [], }, { - id: 2, + uuid: "2", name: "Test Index 2", created_at: new Date(), files: [], diff --git a/src/__tests__/spans.test.ts b/src/__tests__/spans.test.ts index 5516154..97fa077 100644 --- a/src/__tests__/spans.test.ts +++ b/src/__tests__/spans.test.ts @@ -157,13 +157,11 @@ describe("Spans", () => { expect(childEndSpan.name).toBe("child-span"); expect(childEndSpan.output).toBe("child output"); expect(childEndSpan.parent_uuid).toBe("parent-uuid"); - expect(childEndSpan.project).toBe("missing_project"); expect(childEndSpan).toMatchSnapshot(); expect(parentEndSpan.name).toBe("parent-span"); expect(parentEndSpan.output).toBe("parent output"); expect(parentEndSpan.parent_uuid).toBe(undefined); - expect(parentEndSpan.project).toBe("missing_project"); expect(parentEndSpan).toMatchSnapshot(); }); }); diff --git a/src/api-resource.ts b/src/api-resource.ts index 9044305..1947f0d 100644 --- a/src/api-resource.ts +++ b/src/api-resource.ts @@ -254,11 +254,11 @@ class APIResource { }); if (response.status === 200) { - const { id } = await response.json(); + const { uuid } = await response.json(); const update = await this.doPost( - `${paths.create}/${id}`, - JSON.stringify({ ...data, id }) + `${paths.create}/${uuid}`, + JSON.stringify({ ...data, uuid }) ); return update.json(); @@ -359,14 +359,14 @@ class APIResource { protected calcURLIndexes = () => { return `${this._client.baseURL}/v1/indexes`; }; - protected calcURLIndex = (id: number) => { - return `${this._client.baseURL}/v1/indexes/${id}`; + protected calcURLIndex = (uuid: string) => { + return `${this._client.baseURL}/v1/indexes/${uuid}`; }; - protected calcURLAddIndex = (id: number) => { - return `${this._client.baseURL}/v1/indexes/${id}/index`; + protected calcURLAddIndex = (uuid: string) => { + return `${this._client.baseURL}/v1/indexes/${uuid}/index`; }; - protected calcURLQueryIndex = (id: number) => { - return `${this._client.baseURL}/v1/indexes/${id}/query`; + protected calcURLQueryIndex = (uuid: string) => { + return `${this._client.baseURL}/v1/indexes/${uuid}/query`; }; protected calcURLCreateFunction = () => { return `${this._client.baseURL}/api/v1/functions`; @@ -374,8 +374,8 @@ class APIResource { protected calcURLGetFunctionByPath = (path: string) => { return `${this._client.baseURL}/api/v1/functions/by_path/${path}`; }; - protected calcURLUpdateFunction = (id: number) => { - return `${this._client.baseURL}/api/v1/functions/${id}`; + protected calcURLUpdateFunction = (uuid: string) => { + return `${this._client.baseURL}/api/v1/functions/${uuid}`; }; protected calcURLSpans = () => { diff --git a/src/functions.ts b/src/functions.ts index 278570d..9dcc401 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -30,10 +30,10 @@ class Functions extends APIResource { * @throws {OpperError} If the function id is not provided. */ public async update(f: AIFunction): Promise { - if (!f.id) { - throw new OpperError("Function id is required"); + if (!f.uuid) { + throw new OpperError("Function uuid is required"); } - const response = await this.doPost(this.calcURLUpdateFunction(f.id), JSON.stringify(f)); + const response = await this.doPost(this.calcURLUpdateFunction(f.uuid), JSON.stringify(f)); if (response.status !== 200) { const responseData = await response.json(); throw new OpperError( @@ -60,7 +60,7 @@ class Functions extends APIResource { if (!update) { throw new OpperError(`Function with path ${f.path} already exists`); } - f.id = responseData.id; + f.uuid = responseData.uuid; return await this.update(f); } } catch (error) { @@ -75,7 +75,7 @@ class Functions extends APIResource { if (response.status === 200) { const data = await response.json(); - f.id = data.id; + f.uuid = data.uuid; return f; } diff --git a/src/indexes.ts b/src/indexes.ts index 5f2a4f7..01fcfc6 100644 --- a/src/indexes.ts +++ b/src/indexes.ts @@ -52,12 +52,12 @@ class Indexes extends APIResource { /** * Deletes an index - * @param id The id of the index to delete. + * @param uuid The uuid of the index to delete. * @returns A promise that resolves to void. * @throws {APIError} If the response status is not 200. */ - public async delete(id: number): Promise { - await this.doDelete(this.calcURLIndex(id)); + public async delete(uuid: string): Promise { + await this.doDelete(this.calcURLIndex(uuid)); } /** @@ -74,7 +74,7 @@ class Indexes extends APIResource { * ``` */ public async add(index: Index, document: Document): Promise { - await this.doPost(this.calcURLAddIndex(index.id), JSON.stringify(document)); + await this.doPost(this.calcURLAddIndex(index.uuid), JSON.stringify(document)); } /** @@ -93,7 +93,7 @@ class Indexes extends APIResource { filters: Filter[] | null ): Promise { const response = await this.doPost( - this.calcURLQueryIndex(index.id), + this.calcURLQueryIndex(index.uuid), JSON.stringify({ q: query, k: k, filters: filters }) ); diff --git a/src/spans.ts b/src/spans.ts index 8289554..a655cb4 100644 --- a/src/spans.ts +++ b/src/spans.ts @@ -16,7 +16,6 @@ class Spans extends APIResource { input = "", start_time = new Date(), uuid = this.nanoId(), - project = process.env.OPPER_PROJECT || "missing_project", ...rest }: Omit & { uuid?: string }) { spanContextStorage.enterWith({ spanId: uuid }); @@ -26,7 +25,6 @@ class Spans extends APIResource { name, input, start_time, - project, uuid, }); } diff --git a/src/types.ts b/src/types.ts index 2ecb7db..11416f7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -55,7 +55,7 @@ export interface OpperAIChatResponse { } export type IndexFileData = { - id: number; + uuid: string; original_filename: string; size: number; index_status: @@ -72,7 +72,7 @@ export type IndexFileData = { }; export type Index = { - id: number; + uuid: string; name: string; created_at: Date; files: IndexFileData[]; @@ -87,13 +87,13 @@ export interface Filter { } export type Document = { - id?: string; + uuid?: string; key?: string; // a unique key that one can use if you want to update the document content: string; metadata: Record; }; export type AIFunction = { - id?: number; + uuid?: string; path: string; description: string; instructions: string; @@ -114,7 +114,6 @@ export interface CacheConfig { export type Span = { uuid: string; - project?: string; name?: string; input?: string; output?: string;