Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions hlx_statics/blocks/ai-assistant/ai-assistant_api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,25 @@ export class AiApiClient {
* @param {'THUMBS_UP_DOWN'} [options.type]
* @param {number} options.score
* @param {string} options.requestId
* @param {string} [options.query] - The user query that triggered the rated response
* @param {string|null} [options.collectionId] - The collection the query was run against
* @param {string} [options.comment] - Optional free-text feedback comment
*/
async submitFeedback({ type = "THUMBS_UP_DOWN", score, requestId }) {
async submitFeedback({
type = "THUMBS_UP_DOWN",
score,
requestId,
query,
collectionId,
comment,
}) {
try {
/** @type {Record<string, unknown>} */
const payload = { scoreType: type, score };
if (query !== undefined) payload.query = query;
if (collectionId !== undefined) payload.collectionId = collectionId;
if (comment !== undefined) payload.comment = comment;

const response = await fetch(
`${this.baseUrl}${AiApiClient.FEEDBACK_ENDPOINT}/${requestId}`,
{
Expand All @@ -212,7 +228,7 @@ export class AiApiClient {
"Content-Type": "application/json",
"X-Api-Key": this.apiKey,
},
body: JSON.stringify({ scoreType: type, score }),
body: JSON.stringify(payload),
},
);

Expand Down
4 changes: 4 additions & 0 deletions hlx_statics/blocks/ai-assistant/ai-assistant_chat-bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,13 @@ export class ChatBubble {
// don't unselect on click
if (button.dataset.selected === "true") return;

const message = chatHistory.findById(requestId);

const success = await aiApiClient.submitFeedback({
score,
requestId,
query: message?.context?.query,
collectionId: message?.context?.collectionId,
});

if (success) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,13 @@ export const handleUserQuery = async (
callbacks: {
onMetadata: (data) => {
if (data.requestId) {
chatHistory.updateLast({ id: data.requestId });
chatHistory.updateLast({
id: data.requestId,
context: {
query: messageContent,
collectionId: data.collectionId ?? null,
},
});
targetBubble.setMessageId(data.requestId);
}
},
Expand Down
12 changes: 11 additions & 1 deletion hlx_statics/blocks/ai-assistant/ai-assistant_chat-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
* @property {string} title
*/

/**
* The request context that produced an AI response.
* Used to enrich feedback submissions.
* @typedef {Object} ChatMessageContext
* @property {string} query - The user query that triggered this response
* @property {string|null} collectionId - The collection the query ran agains
*/

/**
* @typedef {Object} ChatMessage
* @property {string} [id]
Expand All @@ -14,6 +22,7 @@
* @property {ChatReference[]} [references]
* @property {string|null|number} [timestamp]
* @property {{type: 'THUMBS_UP_DOWN'; score: 0|1}} [feedback]
* @property {ChatMessageContext} [context] - For AI messages: how the response was generated
*/

/**
Expand Down Expand Up @@ -222,13 +231,14 @@ export class ChatHistory {
*/
_sanitizeMessages(messages) {
return messages.map(
({ id, content, source, references, timestamp, feedback }) => ({
({ id, content, source, references, timestamp, feedback, context }) => ({
...(id && { id }),
content,
source,
...(references?.length && { references }),
...(timestamp && { timestamp }),
...(feedback && { feedback }),
...(context && { context }),
}),
);
}
Expand Down
Loading