Skip to content

Commit

Permalink
Merge pull request #14 from opper-ai/use-uuid-not-id
Browse files Browse the repository at this point in the history
feat: use uuid instead of id when referencing functions and indexes
  • Loading branch information
mattias-lundell authored Jun 27, 2024
2 parents a6e4f7a + 2a91242 commit 108ed6f
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 39 deletions.
5 changes: 4 additions & 1 deletion examples/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 0 additions & 3 deletions src/__tests__/__snapshots__/spans.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand All @@ -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",
}
Expand All @@ -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",
}
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/opperai-indexes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down
2 changes: 0 additions & 2 deletions src/__tests__/spans.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
22 changes: 11 additions & 11 deletions src/api-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -359,23 +359,23 @@ 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`;
};
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 = () => {
Expand Down
10 changes: 5 additions & 5 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class Functions extends APIResource {
* @throws {OpperError} If the function id is not provided.
*/
public async update(f: AIFunction): Promise<AIFunction> {
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(
Expand All @@ -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) {
Expand All @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
await this.doDelete(this.calcURLIndex(id));
public async delete(uuid: string): Promise<void> {
await this.doDelete(this.calcURLIndex(uuid));
}

/**
Expand All @@ -74,7 +74,7 @@ class Indexes extends APIResource {
* ```
*/
public async add(index: Index, document: Document): Promise<void> {
await this.doPost(this.calcURLAddIndex(index.id), JSON.stringify(document));
await this.doPost(this.calcURLAddIndex(index.uuid), JSON.stringify(document));
}

/**
Expand All @@ -93,7 +93,7 @@ class Indexes extends APIResource {
filters: Filter[] | null
): Promise<Document[]> {
const response = await this.doPost(
this.calcURLQueryIndex(index.id),
this.calcURLQueryIndex(index.uuid),
JSON.stringify({ q: query, k: k, filters: filters })
);

Expand Down
2 changes: 0 additions & 2 deletions src/spans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Span, "uuid"> & { uuid?: string }) {
spanContextStorage.enterWith({ spanId: uuid });
Expand All @@ -26,7 +25,6 @@ class Spans extends APIResource {
name,
input,
start_time,
project,
uuid,
});
}
Expand Down
9 changes: 4 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface OpperAIChatResponse {
}

export type IndexFileData = {
id: number;
uuid: string;
original_filename: string;
size: number;
index_status:
Expand All @@ -72,7 +72,7 @@ export type IndexFileData = {
};

export type Index = {
id: number;
uuid: string;
name: string;
created_at: Date;
files: IndexFileData[];
Expand All @@ -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<string, unknown>;
};
export type AIFunction = {
id?: number;
uuid?: string;
path: string;
description: string;
instructions: string;
Expand All @@ -114,7 +114,6 @@ export interface CacheConfig {

export type Span = {
uuid: string;
project?: string;
name?: string;
input?: string;
output?: string;
Expand Down

0 comments on commit 108ed6f

Please sign in to comment.