Skip to content

Commit

Permalink
Merge pull request #49 from nowscott/development
Browse files Browse the repository at this point in the history
style:修改缩进为2个空格
  • Loading branch information
nowscott authored Jul 22, 2024
2 parents e0a5bc8 + c640264 commit 9c38876
Show file tree
Hide file tree
Showing 14 changed files with 443 additions and 443 deletions.
14 changes: 7 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { ThemeProvider } from 'contexts/ThemeContext';
import { SettingsProvider } from 'contexts/SettingsContext';

const App = () => {
return (
<ThemeProvider>
<SettingsProvider>
<ChatPage />
</SettingsProvider>
</ThemeProvider>
);
return (
<ThemeProvider>
<SettingsProvider>
<ChatPage />
</SettingsProvider>
</ThemeProvider>
);
};

export default App;
130 changes: 65 additions & 65 deletions src/components/ChatAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,77 @@ let systemPrompt0 = '';
// `;

const ChatAPI = ({
source,
prompt,
onContentUpdate,
onTokenUpdate,
onCompletion,
model,
maxTokens,
temperature,
topP,
topK,
frequencyPenalty,
systemPrompt, // 可以为空
source,
prompt,
onContentUpdate,
onTokenUpdate,
onCompletion,
model,
maxTokens,
temperature,
topP,
topK,
frequencyPenalty,
systemPrompt, // 可以为空
}) => {
// 使用自定义系统提示,如果提供了的话
const fullSystemPrompt = systemPrompt ? `${systemPrompt0}\n${systemPrompt}` : '';
// 使用自定义系统提示,如果提供了的话
const fullSystemPrompt = systemPrompt ? `${systemPrompt0}\n${systemPrompt}` : '';

useEffect(() => {
if (!prompt) {
console.error('Prompt 缺失');
return;
}
const onMessageCompletion = (result) => {
if (result && result.error) {
console.error(result.error);
}
onCompletion && onCompletion(result);
};
useEffect(() => {
if (!prompt) {
console.error('Prompt 缺失');
return;
}
const onMessageCompletion = (result) => {
if (result && result.error) {
console.error(result.error);
}
onCompletion && onCompletion(result);
};

const options = {
prompt,
model,
maxTokens,
temperature,
topP,
topK,
frequencyPenalty,
systemPrompt: fullSystemPrompt,
onContentUpdate,
onTokenUpdate,
onCompletion: onMessageCompletion,
};
const options = {
prompt,
model,
maxTokens,
temperature,
topP,
topK,
frequencyPenalty,
systemPrompt: fullSystemPrompt,
onContentUpdate,
onTokenUpdate,
onCompletion: onMessageCompletion,
};

let abortFunction;
if (source === 'silicon') {
abortFunction = sendSiliconMessage(options);
} else if (source === 'groq') {
abortFunction = sendGroqMessage(options);
} else {
console.error('未知的 source 参数');
return;
}
let abortFunction;
if (source === 'silicon') {
abortFunction = sendSiliconMessage(options);
} else if (source === 'groq') {
abortFunction = sendGroqMessage(options);
} else {
console.error('未知的 source 参数');
return;
}

return () => {
if (abortFunction) abortFunction();
};
}, [
source,
prompt,
model,
maxTokens,
temperature,
topP,
topK,
frequencyPenalty,
fullSystemPrompt,
onContentUpdate,
onTokenUpdate,
onCompletion,
]);
return () => {
if (abortFunction) abortFunction();
};
}, [
source,
prompt,
model,
maxTokens,
temperature,
topP,
topK,
frequencyPenalty,
fullSystemPrompt,
onContentUpdate,
onTokenUpdate,
onCompletion,
]);

return null;
return null;
};

export default ChatAPI;
52 changes: 26 additions & 26 deletions src/components/CodeBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@ import copy from 'copy-to-clipboard';
import { useState, useCallback } from 'react';

const CodeBlock = ({ language, value, messageId }) => {
const [copied, setCopied] = useState(null);
const [copied, setCopied] = useState(null);

const handleCopy = useCallback(() => {
copy(value);
setCopied(messageId);
setTimeout(() => {
setCopied(null);
}, 5000);
}, [value, messageId]);
const handleCopy = useCallback(() => {
copy(value);
setCopied(messageId);
setTimeout(() => {
setCopied(null);
}, 5000);
}, [value, messageId]);

return (
<div className="relative">
<SyntaxHighlighter
style={atomOneDark}
language={language}
PreTag="div"
className="text-sm rounded-md"
>
{value}
</SyntaxHighlighter>
<button
className="absolute top-2 right-2 text-xs text-sky-500"
onClick={handleCopy}
>
{copied === messageId ? <HiCheckCircle className="h-4 w-4 text-lime-500" /> : <HiOutlineDuplicate className="h-4 w-4" />}
</button>
</div>
);
return (
<div className="relative">
<SyntaxHighlighter
style={atomOneDark}
language={language}
PreTag="div"
className="text-sm rounded-md"
>
{value}
</SyntaxHighlighter>
<button
className="absolute top-2 right-2 text-xs text-sky-500"
onClick={handleCopy}
>
{copied === messageId ? <HiCheckCircle className="h-4 w-4 text-lime-500" /> : <HiOutlineDuplicate className="h-4 w-4" />}
</button>
</div>
);
};

export default CodeBlock;
46 changes: 23 additions & 23 deletions src/components/MessageExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@ import { HiOutlineDownload } from "react-icons/hi";
import moment from 'moment';

const convertMessagesToMarkdown = (messages, title) => {
const header = `# ${title}\n\n`;
const body = messages.map(message => {
const role = message.role === 'user' ? '用户' : 'AI';
const date = moment(message.timestamp).format('YYYY-MM-DD_HH-mm-ss');
return `### ${role}\n\n${message.content}\n\n*时间: ${date}*\n`;
}).join('\n');
return header + body;
const header = `# ${title}\n\n`;
const body = messages.map(message => {
const role = message.role === 'user' ? '用户' : 'AI';
const date = moment(message.timestamp).format('YYYY-MM-DD_HH-mm-ss');
return `### ${role}\n\n${message.content}\n\n*时间: ${date}*\n`;
}).join('\n');
return header + body;
};

const exportMarkdown = (markdown, filename) => {
const blob = new Blob([markdown], { type: 'text/markdown;charset=utf-8' });
saveAs(blob, filename);
const blob = new Blob([markdown], { type: 'text/markdown;charset=utf-8' });
saveAs(blob, filename);
};

const MessageExporter = () => {
const handleExport = () => {
const savedMessages = localStorage.getItem('messages');
const messages = savedMessages ? JSON.parse(savedMessages) : [];
const date = moment().format('YYYY-MM-DD_HH-mm-ss');
const title = `chat-history-${date}`;
const md = convertMessagesToMarkdown(messages, title);
const filename = `${title}.md`;
exportMarkdown(md, filename);
};
const handleExport = () => {
const savedMessages = localStorage.getItem('messages');
const messages = savedMessages ? JSON.parse(savedMessages) : [];
const date = moment().format('YYYY-MM-DD_HH-mm-ss');
const title = `chat-history-${date}`;
const md = convertMessagesToMarkdown(messages, title);
const filename = `${title}.md`;
exportMarkdown(md, filename);
};

return (
<button onClick={handleExport} style={{ background: 'none', border: 'none', cursor: 'pointer' }}>
<HiOutlineDownload size={24} />
</button>
);
return (
<button onClick={handleExport} style={{ background: 'none', border: 'none', cursor: 'pointer' }}>
<HiOutlineDownload size={24} />
</button>
);
};

export default MessageExporter;
Loading

0 comments on commit 9c38876

Please sign in to comment.