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) {
return '请先设置模型'
}
let 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)
let prompt = ""
if (action === 'creation_ask') {
if (question.title) {
prompt = question.title
} else {
return ""
}
if (question.content || question.content != "") {
prompt = `${prompt} \n ${question.content}`
}
} else {
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 postMsg: any = {
messages: [
@ -43,7 +56,7 @@ export async function askAi(question: any, action: string) {
const data = await complain.json()
return data.choices[0].message.content
} 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 {
return this.searchManager.getSearchNavigateInfo()
}
public checkPayload(payload: string): boolean {
if (!payload || payload.includes(ZERO)) {
return true;
}
return false;
}
public replace(payload: string, aiArticle?: boolean) {
// console.log('替换', payload)
const isReadonly = this.draw.isReadonly()
if (isReadonly) return
if(aiArticle == true) {
if (aiArticle == true) {
if (!payload) return
} else {
// 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
const matchList = this.draw.getSearch().getSearchMatchList()
@ -1841,8 +1847,8 @@ export class CommandAdapt {
const getElementList = (htmlText?: string) =>
htmlText !== undefined
? getElementListByHTML(htmlText, {
innerWidth
})
innerWidth
})
: undefined
this.setValue({
header: getElementList(header),
@ -2076,7 +2082,7 @@ export class CommandAdapt {
if (
nextElement.level &&
titleOrderNumberMapping[nextElement.level] <=
titleOrderNumberMapping[element.level!]
titleOrderNumberMapping[element.level!]
) {
break
}

Loading…
Cancel
Save