-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for new messages API endpoints (#42)
Added Message API endpoints * Minimum server version: 0.21.0 * Bump version to 0.10.0 Signed-off-by: Peter Garbers <[email protected]> --------- Signed-off-by: Peter Garbers <[email protected]> Co-authored-by: Peter <[email protected]>
- Loading branch information
1 parent
3dd5ae6
commit f4d2faa
Showing
10 changed files
with
381 additions
and
62 deletions.
There are no files selected for viewing
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
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,107 @@ | ||
import { Message } from "./message_models"; | ||
|
||
import { IZepClient } from "./interfaces"; | ||
import { handleRequest } from "./utils"; | ||
|
||
export default class MessageManager { | ||
client: IZepClient; | ||
|
||
constructor(client: IZepClient) { | ||
this.client = client; | ||
} | ||
|
||
async getSessionMessages( | ||
sessionId: string, | ||
limit?: number, | ||
cursor?: number, | ||
): Promise<Message[]> { | ||
if (!sessionId || sessionId.trim() === "") { | ||
throw new Error("sessionId must be provided"); | ||
} | ||
|
||
let url = this.client.getFullUrl(`/sessions/${sessionId}/messages`); | ||
|
||
const params = new URLSearchParams(); | ||
if (limit) params.append("limit", limit.toString()); | ||
if (cursor) params.append("cursor", cursor.toString()); | ||
|
||
if (params.toString()) url += `?${params.toString()}`; | ||
|
||
const response = await handleRequest( | ||
fetch(url, { | ||
method: "GET", | ||
headers: { | ||
...this.client.headers, | ||
"Content-Type": "application/json", | ||
}, | ||
}), | ||
`No session found for session ${sessionId}`, | ||
); | ||
|
||
const responseData = await response.json(); | ||
return responseData.messages.map((message: any) => new Message(message)); | ||
} | ||
|
||
async getSessionMessage( | ||
sessionId: string, | ||
messageId: string, | ||
): Promise<Message> { | ||
if (!sessionId || sessionId.trim() === "") { | ||
throw new Error("sessionId must be provided"); | ||
} | ||
|
||
if (!messageId || messageId.trim() === "") { | ||
throw new Error("messageId must be provided"); | ||
} | ||
|
||
const response = await handleRequest( | ||
fetch( | ||
this.client.getFullUrl( | ||
`/sessions/${sessionId}/messages/${messageId}`, | ||
), | ||
{ | ||
headers: this.client.headers, | ||
}, | ||
), | ||
`No session found for session ${sessionId}, or message ${messageId}`, | ||
); | ||
|
||
const responseData = await response.json(); | ||
|
||
return new Message(responseData); | ||
} | ||
|
||
async updateSessionMessageMetadata( | ||
sessionId: string, | ||
messageId: string, | ||
metadata: Record<string, any>, | ||
): Promise<Message> { | ||
if (!sessionId || sessionId.trim() === "") { | ||
throw new Error("sessionId must be provided"); | ||
} | ||
|
||
if (!messageId || messageId.trim() === "") { | ||
throw new Error("messageId must be provided"); | ||
} | ||
|
||
const response = await handleRequest( | ||
fetch( | ||
this.client.getFullUrl( | ||
`/sessions/${sessionId}/messages/${messageId}`, | ||
), | ||
{ | ||
method: "PATCH", | ||
headers: this.client.headers, | ||
body: JSON.stringify({ | ||
metadata, | ||
}), | ||
}, | ||
), | ||
`No session found for session ${sessionId}, or message ${messageId}`, | ||
); | ||
|
||
const responseData = await response.json(); | ||
|
||
return new Message(responseData); | ||
} | ||
} |
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,56 @@ | ||
/** | ||
* IMessage interface for providing input to create a Message instance. | ||
*/ | ||
export interface IMessage { | ||
uuid?: string; | ||
created_at?: string; | ||
role: string; | ||
content: string; | ||
token_count?: number; | ||
metadata?: Record<string, any>; | ||
} | ||
|
||
/** | ||
* Represents a message in the memory. | ||
*/ | ||
export class Message { | ||
uuid?: string; | ||
|
||
created_at?: string; | ||
|
||
role: string; | ||
|
||
content: string; | ||
|
||
token_count?: number; | ||
|
||
metadata?: Record<string, any>; | ||
|
||
/** | ||
* Constructs a new Message instance. | ||
* @param {IMessage} data - The data to create a message instance. | ||
*/ | ||
constructor(data: IMessage) { | ||
this.uuid = data.uuid; | ||
this.created_at = data.created_at; | ||
this.role = data.role; | ||
this.content = data.content; | ||
this.token_count = data.token_count; | ||
this.metadata = data.metadata; | ||
} | ||
|
||
/** | ||
* Converts the Message instance to a dictionary. | ||
* @returns {IMessage} A dictionary representation of Message instance. | ||
*/ | ||
toDict(): IMessage { | ||
return { | ||
uuid: this.uuid, | ||
created_at: this.created_at, | ||
role: this.role, | ||
content: this.content, | ||
token_count: this.token_count, | ||
metadata: this.metadata, | ||
}; | ||
} | ||
} |
Oops, something went wrong.