feat(ai): ai剧本页添加历史信息获取功能和重新生成功能
This commit is contained in:
parent
9de2ef3734
commit
19f29666a7
|
@ -5,6 +5,8 @@ export default{
|
||||||
productApi:require("./module/productApi.js").default,
|
productApi:require("./module/productApi.js").default,
|
||||||
// 订单,模块
|
// 订单,模块
|
||||||
orderApi:require("./module/orderApi.js").default,
|
orderApi:require("./module/orderApi.js").default,
|
||||||
|
// ai,模块
|
||||||
|
aiApi:require("./module/aiApi.js").default,
|
||||||
/*
|
/*
|
||||||
* 登录相关
|
* 登录相关
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
import {request} from '../request'
|
||||||
|
export default {
|
||||||
|
// 获取商品列表
|
||||||
|
getAiHistory(businessId){
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
request.get(`/ai/chat/history/${businessId}`)
|
||||||
|
.then((res) =>{
|
||||||
|
resolve(res);
|
||||||
|
}).catch(err =>{
|
||||||
|
reject(err);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,16 +22,26 @@
|
||||||
<text class="message-time">{{ message.time }}</text>
|
<text class="message-time">{{ message.time }}</text>
|
||||||
</view>
|
</view>
|
||||||
<!-- 在每条AI消息后显示确认按钮(除了系统消息) -->
|
<!-- 在每条AI消息后显示确认按钮(除了系统消息) -->
|
||||||
<view v-if="message.role === 'assistant' && message.endStatus" class="accept-button-container">
|
<view v-if="message.role === 'assistant' && message.endStatus && index !== 0" class="accept-button-container">
|
||||||
<u-button
|
<u-button
|
||||||
type="success"
|
type="success"
|
||||||
size="mini"
|
size="mini"
|
||||||
@click="acceptScript(message.content)"
|
@click="acceptScript(message.content)"
|
||||||
:custom-style="{marginTop: '10px'}"
|
:custom-style="{marginTop: '10px'}"
|
||||||
>
|
>
|
||||||
<u-icon name="checkmark" size="14" class="button-icon"></u-icon>
|
<u-icon name="checkmark" size="20" class="button-icon"></u-icon>
|
||||||
确认使用此剧本
|
使用此剧本
|
||||||
</u-button>
|
</u-button>
|
||||||
|
<u-button
|
||||||
|
v-if="index === findLastAssistantIndex()"
|
||||||
|
type="primary"
|
||||||
|
size="mini"
|
||||||
|
@click="resetScript(index)"
|
||||||
|
:custom-style="{marginTop: '10px',marginLeft: '10px'}"
|
||||||
|
>
|
||||||
|
<u-icon name="reload" size="20" class="button-icon"></u-icon>
|
||||||
|
重新生成
|
||||||
|
</u-button>
|
||||||
</view>
|
</view>
|
||||||
</block>
|
</block>
|
||||||
|
|
||||||
|
@ -120,7 +130,7 @@ export default {
|
||||||
needScrollToBottom: true,
|
needScrollToBottom: true,
|
||||||
// 滚动计时器
|
// 滚动计时器
|
||||||
scrollTimer: null,
|
scrollTimer: null,
|
||||||
token:''
|
token:'',
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
onLoad(options) {
|
onLoad(options) {
|
||||||
|
@ -137,6 +147,7 @@ export default {
|
||||||
|
|
||||||
// 渲染初始消息的Markdown内容
|
// 渲染初始消息的Markdown内容
|
||||||
this.renderAllMarkdown();
|
this.renderAllMarkdown();
|
||||||
|
this.getAiChatHistory();
|
||||||
},
|
},
|
||||||
onReady() {
|
onReady() {
|
||||||
// 页面加载完成后滚动到底部
|
// 页面加载完成后滚动到底部
|
||||||
|
@ -168,6 +179,55 @@ export default {
|
||||||
title: 'AI 剧本助手'
|
title: 'AI 剧本助手'
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
async getAiChatHistory(){
|
||||||
|
console.log("this.$api",this.$api)
|
||||||
|
let res = await this.$api.aiApi.getAiHistory(this.orderId)
|
||||||
|
console.log('当前获取历史记录', res);
|
||||||
|
if(res.code == 200){
|
||||||
|
let list = res.data || [];
|
||||||
|
this.messages = list.map((item) => {
|
||||||
|
return {
|
||||||
|
role: item.messageType,
|
||||||
|
content: item.message,
|
||||||
|
renderedContent: renderMarkdown(item.message),
|
||||||
|
time: item.createTime,
|
||||||
|
endStatus: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (list.length > 0) {
|
||||||
|
// 滚动到底部
|
||||||
|
this.$nextTick(()=>{
|
||||||
|
setTimeout(()=>this.throttledScrollToBottom(),1000)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
uni.showToast({
|
||||||
|
title: res.msg,
|
||||||
|
icon: 'error'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
findLastAssistantIndex() {
|
||||||
|
// 从后向前遍历数组
|
||||||
|
for (let i = this.messages.length - 1; i >= 0; i--) {
|
||||||
|
if (this.messages[i].role === 'assistant') {
|
||||||
|
return i; // 返回最后一条 assistant 的索引
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1; // 如果没有找到,返回 -1
|
||||||
|
},
|
||||||
|
resetScript(index) {
|
||||||
|
if(this.messages[index-1] && this.messages[index-1].content && this.messages[index-1].role === 'user' ){
|
||||||
|
this.inputMessage = this.messages[index-1].content;
|
||||||
|
this.sendMessage()
|
||||||
|
}else{
|
||||||
|
uni.showToast({
|
||||||
|
title: '重新生成失败',
|
||||||
|
icon: 'error'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// 渲染所有消息的Markdown内容
|
// 渲染所有消息的Markdown内容
|
||||||
renderAllMarkdown() {
|
renderAllMarkdown() {
|
||||||
|
@ -205,7 +265,7 @@ export default {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
|
|
||||||
// 滚动到底部
|
// 滚动到底部
|
||||||
this.throttledScrollToBottom();
|
this.throttledScrollToBottom()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 准备发送给API的消息
|
// 准备发送给API的消息
|
||||||
|
@ -224,7 +284,7 @@ export default {
|
||||||
|
|
||||||
// 在H5环境中使用fetch API进行流式请求
|
// 在H5环境中使用fetch API进行流式请求
|
||||||
if (this.isH5) {
|
if (this.isH5) {
|
||||||
await this.fetchStreamResponse(apiMessages);
|
await this.fetchStreamResponse(userInput);
|
||||||
} else {
|
} else {
|
||||||
// 在非H5环境中使用普通请求
|
// 在非H5环境中使用普通请求
|
||||||
await this.fetchNormalResponse(apiMessages);
|
await this.fetchNormalResponse(apiMessages);
|
||||||
|
@ -255,7 +315,7 @@ export default {
|
||||||
endStatus: false
|
endStatus: false
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await fetch(`${config.baseUrl}/ai/generateStream`, {
|
const response = await fetch(`${config.baseUrl}/ai/chatStream`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${this.token}`,
|
'Authorization': `Bearer ${this.token}`,
|
||||||
|
@ -263,8 +323,9 @@ export default {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
redirect: 'follow',
|
redirect: 'follow',
|
||||||
body: JSON.stringify(apiMessages)
|
body: JSON.stringify({ businessId:this.orderId, message:apiMessages })
|
||||||
});
|
});
|
||||||
|
// console.log("response",response)
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`API请求失败: ${response.status}`);
|
throw new Error(`API请求失败: ${response.status}`);
|
||||||
|
@ -444,19 +505,18 @@ export default {
|
||||||
title: '已确认使用此剧本',
|
title: '已确认使用此剧本',
|
||||||
icon: 'success'
|
icon: 'success'
|
||||||
});
|
});
|
||||||
|
|
||||||
// Return to previous page
|
// 当前页面(假设是 pageB)
|
||||||
uni.navigateBack({
|
uni.navigateBack({
|
||||||
delta: 1, // Number of pages to go back, 1 means return to previous page
|
delta: 1, // 返回的页面层数,1 表示返回上一个页面
|
||||||
success: function() {
|
success: function() {
|
||||||
// Get page stack
|
// 获取页面栈
|
||||||
const pages = getCurrentPages();
|
const pages = getCurrentPages();
|
||||||
// Previous page instance
|
// 上一个页面实例(假设是 pageA)
|
||||||
const prevPage = pages[pages.length - 1];
|
const prevPage = pages[pages.length - 2];
|
||||||
// Call method on previous page
|
console.log("prevPage",prevPage)
|
||||||
if (prevPage && typeof prevPage.getAiDramaTxt === 'function') {
|
// 调用上一个页面的方法
|
||||||
prevPage.getAiDramaTxt(scriptContent);
|
prevPage.getAiDramaTxt(scriptContent);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -138,7 +138,7 @@
|
||||||
},
|
},
|
||||||
openAiDrama(){
|
openAiDrama(){
|
||||||
uni.navigateTo({
|
uni.navigateTo({
|
||||||
url:'./AiDrama'
|
url:'./AiDrama?orderId=' + this.orderItemId
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
getDisabledStatus(){
|
getDisabledStatus(){
|
||||||
|
@ -203,7 +203,6 @@
|
||||||
fileName:'',
|
fileName:'',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
async getMaterialDictList(){
|
async getMaterialDictList(){
|
||||||
let res = await this.$api.orderApi.getProductMaterialDictList({ pageNum: 1, pageSize: 100, productId: this.productId })
|
let res = await this.$api.orderApi.getProductMaterialDictList({ pageNum: 1, pageSize: 100, productId: this.productId })
|
||||||
|
|
Loading…
Reference in New Issue