From 7de8b1d75e886cddcabbb1e81d6b79edf090834e Mon Sep 17 00:00:00 2001 From: Hannah Date: Wed, 8 Jan 2025 13:31:21 +0100 Subject: [PATCH] get thought content in copied value --- js/chatbot/shared/ButtonPanel.svelte | 33 ++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/js/chatbot/shared/ButtonPanel.svelte b/js/chatbot/shared/ButtonPanel.svelte index a025b50d0e97d..b35088495bc63 100644 --- a/js/chatbot/shared/ButtonPanel.svelte +++ b/js/chatbot/shared/ButtonPanel.svelte @@ -2,7 +2,7 @@ import LikeDislike from "./LikeDislike.svelte"; import Copy from "./Copy.svelte"; import type { FileData } from "@gradio/client"; - import type { NormalisedMessage, TextMessage } from "../types"; + import type { NormalisedMessage, TextMessage, ThoughtNode } from "../types"; import { Retry, Undo, Edit, Check, Clear } from "@gradio/icons"; import { IconButtonWrapper, IconButton } from "@gradio/atoms"; export let likeable: boolean; @@ -32,9 +32,38 @@ ); } + function get_thought_content(msg: NormalisedMessage, depth = 0): string { + let content = ""; + const indent = " ".repeat(depth); + + if (msg.metadata?.title) { + content += `${indent}${depth > 0 ? "- " : ""}${msg.metadata.title}\n`; + } + if (typeof msg.content === "string") { + content += `${indent} ${msg.content}\n`; + } + const thought = msg as ThoughtNode; + if (thought.children?.length > 0) { + content += thought.children + .map((child) => get_thought_content(child, depth + 1)) + .join(""); + } + return content; + } + function all_text(message: TextMessage[] | TextMessage): string { if (Array.isArray(message)) { - return message.map((m) => m.content).join("\n"); + return message + .map((m) => { + if (m.metadata?.title) { + return get_thought_content(m); + } + return m.content; + }) + .join("\n"); + } + if (message.metadata?.title) { + return get_thought_content(message); } return message.content; }