-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
531 additions
and
166 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
js/sdk/__tests__/ConversationsIntegrationSuperUser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { r2rClient } from "../src/index"; | ||
const fs = require("fs"); | ||
import { describe, test, beforeAll, expect } from "@jest/globals"; | ||
|
||
const baseUrl = "http://localhost:7272"; | ||
|
||
describe("r2rClient V3 Collections Integration Tests", () => { | ||
let client: r2rClient; | ||
let conversationId: string; | ||
let messageId: string; | ||
|
||
beforeAll(async () => { | ||
client = new r2rClient(baseUrl); | ||
await client.users.login({ | ||
email: "admin@example.com", | ||
password: "change_me_immediately", | ||
}); | ||
}); | ||
|
||
test("List all conversations", async () => { | ||
const response = await client.conversations.list(); | ||
expect(response.results).toBeDefined(); | ||
}); | ||
|
||
test("Create a conversation", async () => { | ||
const response = await client.conversations.create(); | ||
conversationId = response.results.id; | ||
expect(response.results).toBeDefined(); | ||
}); | ||
|
||
test("Add a message to a conversation", async () => { | ||
const response = await client.conversations.addMessage({ | ||
id: conversationId, | ||
content: "Hello, world!", | ||
role: "user", | ||
}); | ||
messageId = response.results.id; | ||
expect(response.results).toBeDefined(); | ||
}); | ||
|
||
// TODO: This is throwing a 405? Why? | ||
// test("Update a message in a conversation", async () => { | ||
// const response = await client.conversations.updateMessage({ | ||
// id: conversationId, | ||
// message_id: messageId, | ||
// content: "Hello, world! How are you?", | ||
// }); | ||
// expect(response.results).toBeDefined(); | ||
// }); | ||
|
||
test("List branches in a conversation", async () => { | ||
const response = await client.conversations.listBranches({ | ||
id: conversationId, | ||
}); | ||
console.log("List branches response: ", response); | ||
expect(response.results).toBeDefined(); | ||
}); | ||
|
||
test("Delete a conversation", async () => { | ||
const response = await client.conversations.delete({ id: conversationId }); | ||
expect(response.results).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { r2rClient } from "../../r2rClient"; | ||
|
||
export class ConversationsClient { | ||
constructor(private client: r2rClient) {} | ||
|
||
/** | ||
* Create a new conversation. | ||
* @returns | ||
*/ | ||
async create(): Promise<any> { | ||
return this.client.makeRequest("POST", "conversations"); | ||
} | ||
|
||
/** | ||
* List conversations with pagination and sorting options. | ||
* @param ids List of conversation IDs to retrieve | ||
* @param offset Specifies the number of objects to skip. Defaults to 0. | ||
* @param limit Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100. | ||
* @returns | ||
*/ | ||
async list(options?: { | ||
ids?: string[]; | ||
offset?: number; | ||
limit?: number; | ||
}): Promise<any> { | ||
const params: Record<string, any> = { | ||
offset: options?.offset ?? 0, | ||
limit: options?.limit ?? 100, | ||
}; | ||
|
||
if (options?.ids && options.ids.length > 0) { | ||
params.ids = options.ids; | ||
} | ||
|
||
return this.client.makeRequest("GET", "conversations", { | ||
params, | ||
}); | ||
} | ||
|
||
/** | ||
* Get detailed information about a specific conversation. | ||
* @param id The ID of the conversation to retrieve | ||
* @param branch_id The ID of the branch to retrieve | ||
* @returns | ||
*/ | ||
async retrieve(options: { id: string; branch_id?: string }): Promise<any> { | ||
const params: Record<string, any> = { | ||
branch_id: options.branch_id, | ||
}; | ||
|
||
return this.client.makeRequest("GET", `conversations/${options.id}`, { | ||
params, | ||
}); | ||
} | ||
|
||
/** | ||
* Delete a conversation. | ||
* @param id The ID of the conversation to delete | ||
* @returns | ||
*/ | ||
async delete(options: { id: string }): Promise<any> { | ||
return this.client.makeRequest("DELETE", `conversations/${options.id}`); | ||
} | ||
|
||
/** | ||
* Add a new message to a conversation. | ||
* @param id The ID of the conversation to add the message to | ||
* @param content The content of the message | ||
* @param role The role of the message (e.g., "user" or "assistant") | ||
* @param parent_id The ID of the parent message | ||
* @param metadata Additional metadata to attach to the message | ||
* @returns | ||
*/ | ||
async addMessage(options: { | ||
id: string; | ||
content: string; | ||
role: string; | ||
parent_id?: string; | ||
metadata?: Record<string, any>; | ||
}): Promise<any> { | ||
const data: Record<string, any> = { | ||
content: options.content, | ||
role: options.role, | ||
...(options.parent_id && { parent_id: options.parent_id }), | ||
...(options.metadata && { metadata: options.metadata }), | ||
}; | ||
|
||
return this.client.makeRequest( | ||
"POST", | ||
`conversations/${options.id}/messages`, | ||
{ | ||
data, | ||
}, | ||
); | ||
} | ||
|
||
/** | ||
* Update an existing message in a conversation. | ||
* @param id The ID of the conversation containing the message | ||
* @param message_id The ID of the message to update | ||
* @param content The new content of the message | ||
* @returns | ||
*/ | ||
async updateMessage(options: { | ||
id: string; | ||
message_id: string; | ||
content: string; | ||
}): Promise<any> { | ||
const data: Record<string, any> = { | ||
content: options.content, | ||
}; | ||
|
||
return this.client.makeRequest( | ||
"POST", | ||
`conversations/${options.id}/messages/${options.message_id}`, | ||
{ | ||
data, | ||
}, | ||
); | ||
} | ||
|
||
/** | ||
* List all branches in a conversation. | ||
* @param id The ID of the conversation to list branches for | ||
* @returns | ||
*/ | ||
async listBranches(options: { id: string }): Promise<any> { | ||
return this.client.makeRequest( | ||
"GET", | ||
`conversations/${options.id}/branches`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.