From 9cf32f84934cf13dba77e20fdddf8703e11b0c62 Mon Sep 17 00:00:00 2001 From: liudong <liudong> Date: 星期二, 13 八月 2024 20:04:20 +0800 Subject: [PATCH] agent会话开发 --- src/api/agentSession.ts | 34 ++++++-- src/views/sessionManager/components/agentSession.vue | 159 +++++++++++++++++++++++++++++++++++---- src/views/sessionManager/index.vue | 6 + 3 files changed, 171 insertions(+), 28 deletions(-) diff --git a/src/api/agentSession.ts b/src/api/agentSession.ts index 7101664..7f5767d 100644 --- a/src/api/agentSession.ts +++ b/src/api/agentSession.ts @@ -25,14 +25,18 @@ // { params } // ); // } +// 鑾峰彇鏅鸿兘鍔╂墜鍒楄〃 +// export function getDialogListApi() { +// return axios.get<ISessionListResult>('/api/dialog/list'); +// } // 鑱婂ぉ -// export function chatApi(data: { conversation_id: string; messages: string }) { -// return axios.post<ISessionListResult>( -// '/api/tech/cloudminds/query?modeltype=localragflow', -// data -// ); -// } +export function chatAgentApi(data) { + return axios.post( + '/api/v1/canvas/completion', + data + ); +} // 鑾峰彇浼氳瘽璇︽儏 export function getAgentSessionDetailsApi(id: string) { @@ -41,7 +45,17 @@ {} ); } -// 鑾峰彇鏅鸿兘鍔╂墜鍒楄〃 -// export function getDialogListApi() { -// return axios.get<ISessionListResult>('/api/dialog/list'); -// } +// 璁剧疆 +export function agentSetApi(data) { + return axios.post( + '/api/v1/canvas/set', + data + ); +} +// 閲嶇疆 +export function agentResetApi(data) { + return axios.post( + '/api/v1/canvas/reset', + data + ); +} \ No newline at end of file diff --git a/src/views/sessionManager/components/agentSession.vue b/src/views/sessionManager/components/agentSession.vue index e73415b..53aaf45 100644 --- a/src/views/sessionManager/components/agentSession.vue +++ b/src/views/sessionManager/components/agentSession.vue @@ -99,21 +99,21 @@ </template> <script setup lang="ts"> -import { defineProps, ref, watch, defineEmits, onMounted, reactive, computed } from "vue"; +import { defineProps, ref, watch, defineEmits, onMounted, reactive, computed, nextTick } from "vue"; import { Message } from '@arco-design/web-vue'; import { useAppStore } from "@/store"; import { getAuthorization } from "@/utils/auth"; import { EventSourceParserStream } from 'eventsource-parser/stream'; -// const props = defineProps({ -// modalObj: Object, -// dialogId: String, -// }); +import { agentResetApi, agentSetApi, getAgentSessionDetailsApi } from "@/api/agentSession"; +const props = defineProps({ + modalObj: Object, +}); // const emit = defineEmits(['addSession']); const sessionDetailList = ref([]); //鏍规嵁浼氳瘽id鍑烘潵鐨勪細璇濊鎯� const sessionList = ref([]); //浼氳瘽鍒楄〃 -const modalObj = reactive({ add: false }); +const modalObj = reactive({ }); const dialogId = ref(''); const chatDis = ref(false); const loading = ref(false); @@ -126,8 +126,7 @@ const inputMsg = ref(''); const activeSessionId = ref(''); const fieldNames = { value: 'id', label: 'name' }; -const dialogs = ref([]); -const dialogObj = reactive({}); +const agentObj = reactive({}); const agentList = ref([]); const selectValue = ref(''); const sectionList = ref({}); @@ -136,6 +135,98 @@ const theme = computed(() => { return appStore.theme; }); + + + +// 鍒濆鍖栭〉闈� +const initPage = async () => { + agentSet(); + agentReset(); + agentCompletion(); + queryAgentSessionDetail(agentObj.id); +}; + +// 璋冪敤set鏂规硶 +const agentSet = async () => { + const res = await agentSetApi({ + id: agentObj.id, + title: agentObj.title, + dsl: agentObj.dsl, + }); + if (res.code === 200) { + // Message.success('淇敼鎴愬姛'); + } +} +// 璋冪敤reset鏂规硶 +const agentReset = async () => { + const res = await agentResetApi({ + id: agentObj.id, + }); + if (res.code === 200) { + // Message.success('淇敼鎴愬姛'); + } +} +// 璋冪敤completion鏂规硶 +const agentCompletion = async () => { + const response = await fetch( + '/api/v1/canvas/completion', + { + method: 'POST', + headers: { + 'Authorization': getAuthorization(), + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + id: agentObj.id + }), + } + ); + + const reader = response?.body + ?.pipeThrough(new TextDecoderStream()) + .pipeThrough(new EventSourceParserStream()) + .getReader(); + currIndex.value = 0; + while (true) { + const x = await reader?.read(); + if (x) { + const { done, value } = x; + console.log(x, 999); + try { + const val = JSON.parse(value?.data || ''); + const d = val?.data; + if (typeof d !== 'boolean') { + console.info('data:', d); + streamStr.value = d.content; + startDisplayStr(); + } + } catch (e) { + console.warn(e); + } + if (done) { + console.info('done'); + displayedText.value = ''; + queryAgentSessionDetail(agentObj.id); + break; + } + } + } + chatDis.value = false; + loading.value = false; + inputMsg.value = ''; +} +// 璋冪敤get鏂规硶 + +const queryAgentSessionDetail = async (id) => { + const { code, data } = await getAgentSessionDetailsApi(id); + if (code == 0) { + console.log(data,'浼氳瘽璇︽儏'); + sessionDetailList.value = data.dsl.messages; + // refreshScroll(); //鍒锋柊婊氬姩鏉′綅缃� + } +}; + + @@ -171,7 +262,7 @@ sessionDetailList.value.push({ role: 'last' }); refreshScroll(); const response = await fetch( - '/api/tech/cloudminds/query?modeltype=localragflow', + '/api/v1/canvas/completion', { method: 'POST', headers: { @@ -209,7 +300,7 @@ if (done) { console.info('done'); displayedText.value = ''; - querySessionDetail(sectionList.value); + queryAgentSessionDetail(agentObj.id); break; } } @@ -232,16 +323,52 @@ }; + +//鏂囧瓧鍔ㄦ�佽緭鍑� +const startDisplayStr = () => { + if (timer) { + clearTimeout(timer!); + } + const res = streamStr.value; + // 灏嗘暟缁勪腑鐨勫瓧绗︿覆鎷兼帴璧锋潵 + if (currIndex.value < res.length) { + displayedText.value += res[currIndex.value]; + currIndex.value++; + setTimeout(startDisplayStr, 100); + refreshScroll(); + } else { + clearTimeout(timer!); + timer = null; + } +}; + +const scrollbar = ref(null); +const refreshScroll = () => { + nextTick(() => { + const container = document.getElementById('home'); + scrollbar.value.scrollTop(container.scrollHeight); + }); +}; + + onMounted(() => { }); -// watch( -// () => props.dialogId, -// (newVal, oldVal) => { -// -// } -// ); +watch( + () => props.modalObj, + (newVal, oldVal) => { + console.log(newVal,'鐩戝惉鍙樺寲'); + Object.assign(agentObj, newVal); + //璋冪敤agent鍒濆鍖栨柟娉� + if(JSON.stringify(newVal) != '{}'){ + initPage(); + } + },{ + immediate: true, + deep: true + } +); </script> <style scoped lang="scss"> diff --git a/src/views/sessionManager/index.vue b/src/views/sessionManager/index.vue index 8f450aa..abb44a3 100644 --- a/src/views/sessionManager/index.vue +++ b/src/views/sessionManager/index.vue @@ -252,7 +252,7 @@ </a-col> <a-col :span="18" v-show="agentType == '2'"> <a-card class="center"> - <agentSession ></agentSession> + <agentSession :modalObj="agentObj"></agentSession> </a-card> </a-col> @@ -363,6 +363,7 @@ const fieldNames = { value: 'id', label: 'name' }; const dialogs = ref([]); const dialogObj = reactive({}); +const agentObj = reactive({}); const agentList = ref([]); const selectValue = ref(''); const sectionList = ref({}); @@ -534,7 +535,8 @@ }; const queryAgentSessionDetail = async (id) => { const { code, data } = await getAgentSessionDetailsApi(id); - if (code === 200) { + if (code == 0) { + Object.assign(agentObj, data) // sessionDetailList.value = data.message; // refreshScroll(); //鍒锋柊婊氬姩鏉′綅缃� } -- Gitblit v1.8.0