Skip to content

Commit

Permalink
Message feedback WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
NolanTrem committed Oct 31, 2024
1 parent 946743e commit 30b945a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
17 changes: 17 additions & 0 deletions js/sdk/src/r2rClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,23 @@ export class r2rClient {
});
}

/**
* Update the metadata of a message in an existing conversation.
* @param message_id The ID of the message to update.
* @param metadata The updated metadata.
* @returns A promise that resolves to the response from the server.
*/
@feature("updateMessageMetadata")
async updateMessageMetadata(
message_id: string,
metadata: Record<string, any>,
): Promise<Record<string, any>> {
this._ensureAuthenticated();
return this._makeRequest("PATCH", `messages/${message_id}/metadata`, {
data: metadata,
});
}

/**
* Get an overview of branches in a conversation.
* @param conversationId The ID of the conversation to get branches for.
Expand Down
15 changes: 15 additions & 0 deletions py/core/main/api/management_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,21 @@ async def edit_message(
"new_branch_id": new_branch_id,
}

@self.router.patch("/messages/{message_id}/metadata")
@self.base_endpoint
async def update_message_metadata(
message_id: str = Path(..., description="Message ID"),
metadata: dict = Body(..., description="Metadata to update"),
auth_user=Depends(self.service.providers.auth.auth_wrapper),
):
"""Update metadata for a specific message.
The provided metadata will be merged with existing metadata.
New keys will be added, existing keys will be updated.
"""
await self.service.update_message_metadata(message_id, metadata)
return "ok"

@self.router.get("/branches_overview/{conversation_id}")
@self.base_endpoint
async def branches_overview(
Expand Down
8 changes: 8 additions & 0 deletions py/core/main/services/management_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,14 @@ async def edit_message(
message_id, new_content
)

@telemetry_event("addMessageMetadata")
async def update_message_metadata(
self, message_id: str, metadata: dict, auth_user=None
):
await self.logging_connection.update_message_metadata(
message_id, metadata
)

@telemetry_event("BranchesOverview")
async def branches_overview(
self, conversation_id: str, auth_user=None
Expand Down
41 changes: 41 additions & 0 deletions py/core/providers/logger/r2r_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,47 @@ async def edit_message(
await self.conn.commit()
return new_message_id, new_branch_id

async def update_message_metadata(
self, message_id: str, metadata: dict
) -> None:
"""Update metadata for a specific message."""

if not self.conn:
raise ValueError(
"Initialize the connection pool before attempting to log."
)

try:
await self.conn.execute("BEGIN TRANSACTION")

cursor = await self.conn.execute(
"SELECT metadata FROM messages WHERE id = ?",
(message_id,),
)
row = await cursor.fetchone()
if not row:
raise ValueError(f"Message {message_id} not found")
current_metadata_json = row[0]
current_metadata = (
json.loads(current_metadata_json)
if current_metadata_json
else {}
)

updated_metadata = {**current_metadata, **metadata}
updated_metadata_json = json.dumps(updated_metadata)

await self.conn.execute(
"UPDATE messages SET metadata = ? WHERE id = ?",
(updated_metadata_json, message_id),
)

await self.conn.commit()

except Exception as e:
await self.conn.rollback()
raise e

async def get_conversation(
self, conversation_id: str, branch_id: Optional[str] = None
) -> Tuple[str, list[Message]]:
Expand Down

0 comments on commit 30b945a

Please sign in to comment.