Skip to content

Commit

Permalink
feat: thought message => tool message
Browse files Browse the repository at this point in the history
  • Loading branch information
Col0ring committed Mar 5, 2025
1 parent db97c35 commit 4f54f5e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 32 deletions.
6 changes: 3 additions & 3 deletions backend/modelscope_studio/components/pro/chatbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class ChatbotDataTextContentOptions(ChatbotMarkdownConfig):
pass


class ChatbotDataThoughtContentOptions(ChatbotDataTextContentOptions):
class ChatbotDataToolContentOptions(ChatbotDataTextContentOptions):
title: Optional[str] = None
status: Optional[Literal['pending', 'done']] = None

Expand Down Expand Up @@ -161,14 +161,14 @@ class ChatbotDataMeta(GradioModel):


class ChatbotDataMessageContent(GradioModel):
type: Optional[Literal['text', 'thought', 'file', 'suggestion']] = 'text'
type: Optional[Literal['text', 'tool', 'file', 'suggestion']] = 'text'
copyable: Optional[bool] = True
content: Optional[Union[str, List[Union[FileData,
ChatbotDataSuggestionContentItem,
dict, str]]]] = None
options: Optional[Union[dict, ChatbotDataTextContentOptions,
ChatbotDataFileContentOptions,
ChatbotDataThoughtContentOptions,
ChatbotDataToolContentOptions,
ChatbotDataSuggestionContentOptions]] = None


Expand Down
20 changes: 9 additions & 11 deletions frontend/pro/chatbot/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Flex, Input } from 'antd';
import { FileMessage } from './messages/file';
import { SuggestionMessage } from './messages/suggestion';
import { TextMessage } from './messages/text';
import { ThoughtMessage } from './messages/thought';
import { ToolMessage } from './messages/tool';
import type {
ChatbotFileContent,
ChatbotFileContentConfig,
Expand All @@ -17,8 +17,8 @@ import type {
ChatbotSuggestionContentConfig,
ChatbotTextContent,
ChatbotTextContentConfig,
ChatbotThoughtContent,
ChatbotThoughtContentConfig,
ChatbotToolContent,
ChatbotToolContentConfig,
SuggestionData,
} from './type';
import { normalizeMessageContent, walkSuggestionContent } from './utils';
Expand Down Expand Up @@ -62,10 +62,8 @@ export const Message: React.FC<MessageProps> = ({
const content = normalizeMessageContent(message.content);
return content.map((item, i) => {
const render = () => {
if (isEditing && ['text', 'thought'].includes(item.type)) {
const text = item.content as
| ChatbotTextContent
| ChatbotThoughtContent;
if (isEditing && ['text', 'tool'].includes(item.type)) {
const text = item.content as ChatbotTextContent | ChatbotToolContent;
const containerWidth =
containerRef.current?.getBoundingClientRect().width;
return (
Expand Down Expand Up @@ -102,16 +100,16 @@ export const Message: React.FC<MessageProps> = ({
)}
/>
);
case 'thought':
case 'tool':
return (
<ThoughtMessage
value={item.content as ChatbotThoughtContent}
<ToolMessage
value={item.content as ChatbotToolContent}
options={omitUndefinedProps(
{
...markdownConfig,
...(convertObjectKeyToCamelCase(
item.options
) as ChatbotThoughtContentConfig),
) as ChatbotToolContentConfig),
},
{ omitNull: true }
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ import { useEffect, useState } from 'react';
import { Markdown } from '@globals/components';
import { Collapse } from 'antd';

import type {
ChatbotThoughtContent,
ChatbotThoughtContentConfig,
} from '../type';
import type { ChatbotToolContent, ChatbotToolContentConfig } from '../type';

export interface ThoughtMessageProps {
options: ChatbotThoughtContentConfig;
value?: ChatbotThoughtContent;
export interface ToolMessageProps {
options: ChatbotToolContentConfig;
value?: ChatbotToolContent;
}

export const ThoughtMessage: React.FC<ThoughtMessageProps> = ({
value,
options,
}) => {
export const ToolMessage: React.FC<ToolMessageProps> = ({ value, options }) => {
const { renderMarkdown, status, title, ...markdownProps } = options;
const [collapsed, setCollapsed] = useState(() => status !== 'done');
useEffect(() => {
Expand All @@ -25,13 +19,13 @@ export const ThoughtMessage: React.FC<ThoughtMessageProps> = ({
return (
<>
<Collapse
activeKey={collapsed ? ['thought'] : []}
activeKey={collapsed ? ['tool'] : []}
onChange={() => {
setCollapsed(!collapsed);
}}
items={[
{
key: 'thought',
key: 'tool',
label: title,
children: renderMarkdown ? (
<Markdown {...markdownProps} value={value} />
Expand Down
10 changes: 5 additions & 5 deletions frontend/pro/chatbot/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export interface ChatbotWelcomeConfig extends Omit<WelcomeProps, 'icon'> {

export type ChatbotFileContent = (string | (Attachment & FileData))[];
export type ChatbotSuggestionContent = PromptProps[];
export type ChatbotThoughtContent = string;
export type ChatbotToolContent = string;
export type ChatbotTextContent = string;

export interface ChatbotMarkdownConfig extends Omit<MarkdownProps, 'value'> {
renderMarkdown?: boolean;
}
export type ChatbotTextContentConfig = ChatbotMarkdownConfig;
export type ChatbotThoughtContentConfig = ChatbotMarkdownConfig & {
export type ChatbotToolContentConfig = ChatbotMarkdownConfig & {
status?: 'pending' | 'done';
title?: string;
};
Expand Down Expand Up @@ -94,16 +94,16 @@ export interface ChatbotBotConfig
}

export interface ChatbotMessageContentObject {
type: 'text' | 'thought' | 'file' | 'suggestion';
type: 'text' | 'tool' | 'file' | 'suggestion';
copyable?: boolean;
content:
| ChatbotFileContent
| ChatbotSuggestionContent
| ChatbotTextContent
| ChatbotThoughtContent;
| ChatbotToolContent;
options?:
| ChatbotTextContentConfig
| ChatbotThoughtContentConfig
| ChatbotToolContentConfig
| ChatbotFileContentConfig
| ChatbotSuggestionContentConfig;
}
Expand Down

0 comments on commit 4f54f5e

Please sign in to comment.