-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support image generate #116
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,10 @@ | |
import { appWindow } from '@tauri-apps/api/window' | ||
|
||
const recordStore = useSessionStore() | ||
const { isThinking } = storeToRefs(recordStore) | ||
const { isThinking, currentSession } = storeToRefs(recordStore) | ||
|
||
const roleStore = useRoleStore() | ||
const { currentRole, isEdit, textAreaValue, popoverVisible } = | ||
const { currentRole, isEdit, textAreaValue, imageValue, popoverVisible } = | ||
storeToRefs(roleStore) | ||
|
||
const textAreaElement = ref<HTMLTextAreaElement | null>(null) | ||
|
@@ -25,11 +25,10 @@ const onKeydown = (event: KeyboardEvent) => { | |
|
||
if (!value) return | ||
|
||
// if (popoverVisible) { | ||
popoverVisible.value = false | ||
// } | ||
|
||
getAiMessage(value) | ||
if (currentSession.value?.type === 'text') getAiMessage(value) | ||
else getAiIamge(value) | ||
|
||
textAreaElement.value?.blur() | ||
textAreaValue.value = '' | ||
|
@@ -60,21 +59,32 @@ onMounted(() => { | |
<template> | ||
<div class="app-input flex items-center gap-2"> | ||
<RoleList /> | ||
|
||
<a-textarea | ||
ref="textAreaElement" | ||
class="bordered bg-transparent!" | ||
:class="!textAreaValue && 'rounded-10'" | ||
:placeholder="isThinking ? 'AI 正在思考...' : '有什么问题尽管问我'" | ||
v-model="textAreaValue" | ||
:disabled="isThinking || isEdit" | ||
:auto-size="{ | ||
minRows: 1, | ||
maxRows: 5 | ||
}" | ||
clearable | ||
@keydown="onKeydown" | ||
></a-textarea> | ||
<div class="flex w-full flex-col"> | ||
<a-textarea | ||
ref="textAreaElement" | ||
class="bordered bg-transparent!" | ||
:class="!textAreaValue && 'rounded-10'" | ||
:placeholder="isThinking ? 'AI 正在思考...' : '有什么问题尽管问我'" | ||
v-model="textAreaValue" | ||
:disabled="isThinking || isEdit" | ||
:auto-size="{ | ||
minRows: 1, | ||
maxRows: 5 | ||
}" | ||
clearable | ||
@keydown="onKeydown" | ||
></a-textarea> | ||
<div class="flex w-full" v-if="currentSession?.type === 'image'"> | ||
<a-select placeholder="选择生成的尺寸" v-model="imageValue.size"> | ||
<a-option>256x256</a-option> | ||
<a-option>512x512</a-option> | ||
<a-option>1024x1024</a-option> | ||
</a-select> | ||
<a-select placeholder="选择生成的数量" v-model="imageValue.number"> | ||
<a-option v-for="i in 10" :key="i">{{ i + '' }}</a-option> | ||
</a-select> | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段代码主要是关于一个聊天界面的交互部分,其中包含了输入框以及相关的一些处理逻辑。以下是我对这段代码的简要评估:
除此之外,这份代码看起来比较简洁和易懂,没有明显的错误和潜在风险。如果你能提供更多上下文和其他文件的话,我们可以做更深入的评估和优化建议。 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,10 @@ export const useRoleStore = defineStore( | |
const popoverVisible = ref(false) | ||
// 检索角色列表的输入框值 | ||
const textAreaValue = ref('') | ||
const imageValue = reactive({ | ||
number: '1', | ||
size: '256x256' | ||
}) | ||
// 是否有角色正在编辑 | ||
const isEdit = computed(() => roleList.value.some((item) => item.isEdit)) | ||
|
||
|
@@ -118,6 +122,7 @@ export const useRoleStore = defineStore( | |
filterRoleList, | ||
popoverVisible, | ||
textAreaValue, | ||
imageValue, | ||
isEdit, | ||
getRoleList, | ||
getFilterRoleList, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段代码看起来很简洁,没有明显的 bug 风险。这里有两个改进建议:
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,12 @@ export interface MessageData { | |
role: Role | ||
content: string | ||
} | ||
|
||
export interface ImageData { | ||
n: number | ||
prompt: string | ||
size: '256x256' | '512x512' | '1024x1024' | ||
} | ||
export interface RolePayload { | ||
id?: number | ||
name: string | ||
|
@@ -33,6 +39,7 @@ export interface SessionPayload { | |
role_id: number | ||
update_time?: string | ||
name?: string | ||
type: 'text' | 'image' | ||
isEdit?: boolean | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段代码主要是 TypeScript 中定义了一些类型和接口。以下是我的一些建议:
最后,建议添加更多注释以增强代码可读性。 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,3 +122,71 @@ export const getAiMessage = async (value?: string) => { | |
isThinking.value = false | ||
} | ||
} | ||
|
||
/** | ||
* 获取 ai 作图 | ||
* @param value 消息内容 | ||
*/ | ||
export const getAiIamge = async (value?: string) => { | ||
const { isThinking, sessionDataList } = storeToRefs(useSessionStore()) | ||
const { updateSessionData } = useSessionStore() | ||
|
||
try { | ||
// 获取图像的记忆模式 = 按照上一张图去修改生成 | ||
let imageData | ||
|
||
if (!value) { | ||
// 重复上一次提问 | ||
const { sessionDataList } = useSessionStore() | ||
|
||
const lastQuestion = sessionDataList.filter((item) => item.is_ask).at(-1) | ||
if (!lastQuestion) return | ||
|
||
const deleteSql = `DELETE FROM session_data WHERE session_id = '${lastQuestion?.session_id}' AND id >= ${lastQuestion?.id};` | ||
await executeSQL(deleteSql) | ||
|
||
imageData = JSON.parse(lastQuestion?.message.content as any) | ||
} else { | ||
// 添加正常提问 | ||
const { imageValue } = useRoleStore() | ||
|
||
imageData = { | ||
n: parseInt(imageValue.number), | ||
size: imageValue.size, | ||
prompt: value | ||
} | ||
} | ||
|
||
const { addSessionData } = useSessionStore() | ||
|
||
isThinking.value = true | ||
|
||
await addSessionData({ | ||
isAsk: true, | ||
data: { | ||
role: 'user', | ||
content: imageData | ||
} | ||
}) | ||
|
||
await addSessionData({ | ||
isAsk: false, | ||
messageType: 'image', | ||
data: { | ||
role: 'assistant', | ||
content: '' | ||
} | ||
}) | ||
|
||
const res = await getOpenAIImage(imageData) | ||
if (!res) return | ||
|
||
sessionDataList.value.at(-1)!.message.content = res.data as string | ||
} catch ({ message }: any) { | ||
sessionDataList.value.at(-1)!.message.content = message as any | ||
|
||
updateSessionData(sessionDataList.value.at(-1)!) | ||
} finally { | ||
isThinking.value = false | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段代码看起来是一段 TypeScript 代码,主要用于调用 OpenAI 的 API 进行文本或者图片的生成,具体可分为两个部分:getAiMessage 和 getAiIamge。以下是我的一些建议和注意事项:
以上是我的一些建议,希望对你有所帮助。 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ const marked = new MarkdownIt({ | |
}) | ||
|
||
export const checkVersion = async () => { | ||
const updateInfo = await checkUpdate() | ||
const updateInfo = await checkUpdate().catch(() => null) | ||
|
||
if (!updateInfo || !updateInfo.manifest) return | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段代码看起来比较简单,主要是导入了一个MarkdownIt库和一个异步函数checkVersion。在checkVersion函数中,它使用了一个异步函数checkUpdate()来检查更新信息,并且在调用checkUpdate()时通过.catch(() => null)捕获了可能的异常。 从风险角度看,如果checkUpdate()函数出现了错误并返回了无效的数据,那么这个函数就会继续执行而不会抛出任何错误。因此,我们需要确保checkUpdate()在出现错误时能够正确地处理异常情况,并且在必要的情况下抛出适当的错误。 另外,由于没有看到checkUpdate()函数的定义,我无法评估它的质量和安全性。因此,如果有必要的话,我们可能需要对checkUpdate()函数进行进一步的审核和测试。 关于改进建议,我建议可以添加更多的注释,以便其他开发人员更容易理解代码和实现细节。此外,如果代码被用于生产环境,还应该考虑添加日志记录和错误处理机制,以便更好地监控和管理应用程序的行为。 |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段代码补丁主要有两个改动:
代码本身没有明显的 bug 风险,但是可以考虑一些改进: