Skip to content

Commit

Permalink
chore: playground hotkey
Browse files Browse the repository at this point in the history
  • Loading branch information
hibig committed Jun 25, 2024
1 parent a7ea167 commit ad9775d
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 37 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"lodash": "^4.17.21",
"numeral": "^2.0.6",
"query-string": "^9.0.0",
"react-hotkeys-hook": "^4.5.0",
"umi-presets-pro": "^2.0.3"
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/assets/styles/common.less
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@
display: flex;
align-items: center;
}

.opct-7 {
opacity: 0.7;
}

.opct-8 {
opacity: 0.8;
}
23 changes: 12 additions & 11 deletions src/config/hotkeys.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
export default {
Create: ['ctrl+n', 'cmd+n'],
Save: ['ctrl+s', 'cmd+s'],
SaveAs: ['ctrl+shift+s', 'cmd+shift+s'],
Open: ['ctrl+o', 'cmd+o'],
Cancel: ['ctrl+w', 'cmd+w'],
Delete: ['delete'],
Copy: ['ctrl+c', 'cmd+c'],
Refresh: ['ctrl+r', 'cmd+r'],
Edit: ['ctrl+e', 'cmd+e'],
Search: ['ctrl+f', 'cmd+f'],
Reset: ['ctrl+shift+r', 'cmd+shift+r']
CREATE: ['ctrl+n', 'meta+n'],
SAVE: ['ctrl+s', 'meta+s'],
SUBMIT: ['ctrl+enter', 'meta+enter'],
SAVEAS: ['ctrl+shift+s', 'meta+shift+s'],
OPEN: ['ctrl+o', 'meta+o'],
CANCEL: ['ctrl+w', 'meta+w'],
DELETE: ['delete'],
COPY: ['ctrl+c', 'meta+c'],
REFRESH: ['ctrl+r', 'meta+r'],
EDIT: ['ctrl+e', 'meta+e'],
SEARCH: ['ctrl+f', 'meta+f'],
RESET: ['ctrl+shift+r', 'meta+shift+r']
};
4 changes: 2 additions & 2 deletions src/pages/llmodels/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export interface ModelInstanceListItem {
huggingface_repo_id: string;
huggingface_filename: string;
s3_address: string;
node_id: number;
node_ip: string;
worker_id: number;
worker_ip: string;
pid: number;
port: number;
state: string;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/llmodels/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ const Models: React.FC = () => {
<RowChildren>
<Row style={{ width: '100%' }} align="middle">
<Col span={4}>
{item.node_ip}:{item.port}
{item.worker_ip}:{item.port}
</Col>
<Col span={5}>
<span>{item.huggingface_filename}</span>
Expand Down
25 changes: 17 additions & 8 deletions src/pages/playground/components/chat-footer.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import HotKeys from '@/config/hotkeys';
import {
CodeOutlined,
DeleteOutlined,
PlusOutlined,
SaveOutlined
EnterOutlined,
MacCommandOutlined,
PlusOutlined
} from '@ant-design/icons';
import { useIntl } from '@umijs/max';
import { Button, Col, Row, Space } from 'antd';
import { useHotkeys } from 'react-hotkeys-hook';
import '../style/chat-footer.less';

interface ChatFooterProps {
Expand All @@ -20,6 +23,14 @@ interface ChatFooterProps {
const ChatFooter: React.FC<ChatFooterProps> = (props) => {
const intl = useIntl();
const { onSubmit, onClear, onNewMessage, onView, feedback, disabled } = props;
useHotkeys(
HotKeys.SUBMIT.join(','),
() => {
onSubmit();
},
{ enabled: !disabled }
);

return (
<div className="chat-footer">
<Row style={{ width: '100%' }}>
Expand Down Expand Up @@ -52,13 +63,11 @@ const ChatFooter: React.FC<ChatFooterProps> = (props) => {
>
{intl.formatMessage({ id: 'playground.viewcode' })}
</Button>
<Button
disabled={disabled}
type="primary"
icon={<SaveOutlined></SaveOutlined>}
onClick={onSubmit}
>
<Button disabled={disabled} type="primary" onClick={onSubmit}>
{intl.formatMessage({ id: 'common.button.submit' })}
<span className="m-l-5 opct-7">
<MacCommandOutlined /> + <EnterOutlined />
</span>
</Button>
</Space>
</Col>
Expand Down
28 changes: 28 additions & 0 deletions src/pages/playground/components/ground-left.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import TransitionWrapper from '@/components/transition';
import HotKeys from '@/config/hotkeys';
import { EyeInvisibleOutlined } from '@ant-design/icons';
import { PageContainer } from '@ant-design/pro-components';
import { useIntl } from '@umijs/max';
import { Button, Input, Spin } from 'antd';
import _ from 'lodash';
import { useRef, useState } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
import { fetchChatStream, receiveChatStream } from '../apis';
import { Roles } from '../config';
import '../style/ground-left.less';
Expand All @@ -13,6 +15,7 @@ import ChatFooter from './chat-footer';
import MessageItem from './message-item';
import ReferenceParams from './reference-params';
import ViewCodeModal from './view-code-modal';

interface MessageProps {
parameters: any;
}
Expand Down Expand Up @@ -40,6 +43,7 @@ const MessageList: React.FC<MessageProps> = (props) => {
const [loading, setLoading] = useState(false);
const [activeIndex, setActiveIndex] = useState(-1);
const [tokenResult, setTokenResult] = useState<any>(null);
const [currentIsFocus, setCurrentIsFocus] = useState(false);
const systemRef = useRef<any>(null);
const contentRef = useRef<any>('');

Expand Down Expand Up @@ -153,6 +157,27 @@ const MessageList: React.FC<MessageProps> = (props) => {
</div>
);
};

const handleFocus = () => {
setCurrentIsFocus(true);
};

const handleBlur = () => {
setCurrentIsFocus(false);
};

useHotkeys(
HotKeys.SUBMIT,
() => {
handleSubmit();
},
{
enabled: currentIsFocus && !loading,
enableOnFormTags: currentIsFocus && !loading,
preventDefault: true
}
);

return (
<div className="ground-left">
<PageContainer title={false} className="message-list-wrap">
Expand All @@ -166,6 +191,8 @@ const MessageList: React.FC<MessageProps> = (props) => {
value={systemMessage}
variant="filled"
autoSize={true}
onFocus={handleFocus}
onBlur={handleBlur}
placeholder={intl.formatMessage({ id: 'playground.system.tips' })}
onChange={handleSystemMessageChange}
></Input.TextArea>
Expand All @@ -184,6 +211,7 @@ const MessageList: React.FC<MessageProps> = (props) => {
updateMessage={(message: MessageItemProps) =>
handleUpdateMessage(index, message)
}
onSubmit={handleSubmit}
message={item}
/>
);
Expand Down
25 changes: 24 additions & 1 deletion src/pages/playground/components/message-item.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import HotKeys from '@/config/hotkeys';
import { MinusCircleOutlined } from '@ant-design/icons';
import { useIntl } from '@umijs/max';
import { Button, Input } from 'antd';
import { memo, useEffect, useRef, useState } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
import { Roles } from '../config';
import '../style/message-item.less';
interface MessageItemProps {
Expand All @@ -14,18 +16,32 @@ const MessageItem: React.FC<{
message: MessageItemProps;
loading?: boolean;
islast?: boolean;
onSubmit: () => void;
updateMessage: (message: MessageItemProps) => void;
isFocus: boolean;
onDelete: () => void;
}> = ({ message, isFocus, onDelete, updateMessage }) => {
}> = ({ message, isFocus, onDelete, updateMessage, onSubmit, loading }) => {
const intl = useIntl();
const [roleType, setRoleType] = useState(message.role);
const [isTyping, setIsTyping] = useState(false);
const [messageContent, setMessageContent] = useState(message.content);
const isInitialRender = useRef(true);
const [isAnimating, setIsAnimating] = useState(false);
const [currentIsFocus, setCurrentIsFocus] = useState(isFocus);
const inputRef = useRef<any>(null);

useHotkeys(
HotKeys.SUBMIT,
() => {
onSubmit();
},
{
enabled: currentIsFocus && !loading,
enableOnFormTags: currentIsFocus && !loading,
preventDefault: true
}
);

useEffect(() => {
if (inputRef.current && isFocus) {
inputRef.current.focus();
Expand Down Expand Up @@ -75,6 +91,11 @@ const MessageItem: React.FC<{

const handleBlur = () => {
setIsTyping(true);
setCurrentIsFocus(false);
};

const handleFocus = () => {
setCurrentIsFocus(true);
};

const handleRoleChange = () => {
Expand Down Expand Up @@ -103,13 +124,15 @@ const MessageItem: React.FC<{
autoSize={true}
variant="filled"
onChange={handleMessageChange}
onFocus={handleFocus}
onBlur={handleBlur}
></Input.TextArea>
</div>
<div className="delete-btn">
<Button
type="text"
shape="circle"
size="small"
style={{ color: 'var(--ant-color-primary)' }}
onClick={handleDelete}
icon={<MinusCircleOutlined />}
Expand Down
6 changes: 3 additions & 3 deletions src/pages/resources/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { request } from '@umijs/max';
import { ListItem } from '../config/types';

export const NODES_API = '/nodes';
export const WORKERS_API = '/workers';

export async function queryNodesList(
export async function queryWorkersList(
params: Global.Pagination & { query?: string }
) {
return request<Global.PageResponse<ListItem>>(`${NODES_API}`, {
return request<Global.PageResponse<ListItem>>(`${WORKERS_API}`, {
methos: 'GET',
params
});
Expand Down
15 changes: 8 additions & 7 deletions src/pages/resources/components/nodes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useIntl } from '@umijs/max';
import { Button, Input, Space, Table } from 'antd';
import _ from 'lodash';
import { useEffect, useState } from 'react';
import { queryNodesList } from '../apis';
import { queryWorkersList } from '../apis';
import { ListItem } from '../config/types';
const { Column } = Table;

Expand All @@ -33,8 +33,8 @@ const Models: React.FC = () => {
const params = {
..._.pickBy(queryParams, (val: any) => !!val)
};
const res = await queryNodesList(params);
console.log('res=======', res);
const res = await queryWorkersList(params);

setDataSource(res.items);
setTotal(res.pagination.total);
} catch (error) {
Expand Down Expand Up @@ -131,15 +131,16 @@ const Models: React.FC = () => {
return (
<StatusTag
statusValue={{
status:
record.status?.state === 'active' ? 'success' : 'error',
text: record.status?.state
status: ['Inactive', 'unknown'].includes(record.state)
? 'inactive'
: 'success',
text: record.state
}}
></StatusTag>
);
}}
/>
<Column title="IP" dataIndex="address" key="address" />
<Column title="IP" dataIndex="ip" key="address" />
<Column
title="CPU"
dataIndex="CPU"
Expand Down
3 changes: 2 additions & 1 deletion src/pages/resources/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export interface ListItem {
hostname: string;
address: string;
labels: object;
state: string;
ip: string;
status: {
cpu: {
total: number;
Expand All @@ -60,7 +62,6 @@ export interface ListItem {
uptime: number;
boot_time: string;
};
state: string;
};
id: number;
created_at: string;
Expand Down
6 changes: 3 additions & 3 deletions src/pages/resources/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import Nodes from './components/nodes';

const items: TabsProps['items'] = [
{
key: 'nodes',
label: 'Nodes',
key: 'workers',
label: 'Workers',
children: <Nodes />
},
{
Expand Down Expand Up @@ -38,7 +38,7 @@ const Resources = () => {
<div style={{ marginTop: 70 }}>
<Tabs
type="card"
defaultActiveKey="nodes"
defaultActiveKey="workers"
items={items}
accessKey={activeKey}
onChange={handleChangeTab}
Expand Down

0 comments on commit ad9775d

Please sign in to comment.