Browse Source

change ai replace

master
godo 6 months ago
parent
commit
af6121d337
  1. 39
      frontend/src/hook/useAi.ts
  2. 20
      packages/word/src/editor/core/command/CommandAdapt.ts

39
frontend/src/hook/useAi.ts

@ -10,19 +10,32 @@ export async function askAi(question: any, action: string) {
if (!model) { if (!model) {
return '请先设置模型' return '请先设置模型'
} }
let prompt = await assistantStore.getPrompt(action) let prompt = ""
if (!prompt) { if (action === 'creation_ask') {
return '请先设置prompt' if (question.title) {
} prompt = question.title
if (question.content) { } else {
prompt = prompt.replace('{content}', question.content) return ""
} }
if (question.title) { if (question.content || question.content != "") {
prompt = prompt.replace('{title}', question.title) prompt = `${prompt} \n ${question.content}`
} }
if (question.category) { } else {
prompt = prompt.replace('{category}', question.category) prompt = await assistantStore.getPrompt(action)
if (!prompt) {
return '请先设置prompt'
}
if (question.content) {
prompt = prompt.replace('{content}', question.content)
}
if (question.title) {
prompt = prompt.replace('{title}', question.title)
}
if (question.category) {
prompt = prompt.replace('{category}', question.category)
}
} }
const apiUrl = config.aiUrl + '/ai/chat' const apiUrl = config.aiUrl + '/ai/chat'
const postMsg: any = { const postMsg: any = {
messages: [ messages: [
@ -43,7 +56,7 @@ export async function askAi(question: any, action: string) {
const data = await complain.json() const data = await complain.json()
return data.choices[0].message.content return data.choices[0].message.content
} catch (error) { } catch (error) {
return '请求失败'+error return '请求失败' + error
} }

20
packages/word/src/editor/core/command/CommandAdapt.ts

@ -1203,18 +1203,24 @@ export class CommandAdapt {
public getSearchNavigateInfo(): null | INavigateInfo { public getSearchNavigateInfo(): null | INavigateInfo {
return this.searchManager.getSearchNavigateInfo() return this.searchManager.getSearchNavigateInfo()
} }
public checkPayload(payload: string): boolean {
if (!payload || payload.includes(ZERO)) {
return true;
}
return false;
}
public replace(payload: string, aiArticle?: boolean) { public replace(payload: string, aiArticle?: boolean) {
// console.log('替换', payload) // console.log('替换', payload)
const isReadonly = this.draw.isReadonly() const isReadonly = this.draw.isReadonly()
if (isReadonly) return if (isReadonly) return
if(aiArticle == true) { if (aiArticle == true) {
if (!payload) return if (!payload) return
} else { } else {
// console.log('zero:', new RegExp(`${ZERO}`, 'g').test(payload)) // console.log('zero:', new RegExp(`${ZERO}`, 'g').test(payload))
if (!payload || new RegExp(`${ZERO}`, 'g').test(payload)) return //if (!payload || new RegExp(`${ZERO}`, 'g').test(payload)) return
if(this.checkPayload(payload))return
} }
// if (!payload || (isZero && aiArticle)) return // if (!payload || (isZero && aiArticle)) return
const matchList = this.draw.getSearch().getSearchMatchList() const matchList = this.draw.getSearch().getSearchMatchList()
@ -1841,8 +1847,8 @@ export class CommandAdapt {
const getElementList = (htmlText?: string) => const getElementList = (htmlText?: string) =>
htmlText !== undefined htmlText !== undefined
? getElementListByHTML(htmlText, { ? getElementListByHTML(htmlText, {
innerWidth innerWidth
}) })
: undefined : undefined
this.setValue({ this.setValue({
header: getElementList(header), header: getElementList(header),
@ -2076,7 +2082,7 @@ export class CommandAdapt {
if ( if (
nextElement.level && nextElement.level &&
titleOrderNumberMapping[nextElement.level] <= titleOrderNumberMapping[nextElement.level] <=
titleOrderNumberMapping[element.level!] titleOrderNumberMapping[element.level!]
) { ) {
break break
} }

Loading…
Cancel
Save