UNPKG

@tencentcloud/ai-desk-customer-wechat

Version:

AI Desk customer UIKit for wechat mini program

70 lines (69 loc) 2.89 kB
"use strict"; const common_vendor = require("../../../../../../../common/vendor.js"); const aiDeskCustomerUniapp_adapterVue = require("../../../../../../adapter-vue.js"); const aiDeskCustomerUniapp_components_CustomerServiceChat_messageList_messageElements_messageDesk_messageDeskElements_marked = require("./marked.js"); const aiDeskCustomerUniapp_components_CustomerServiceChat_messageList_messageElements_messageDesk_messageDeskElements_typeWriter = require("./type-writer.js"); const aiDeskCustomerUniapp_utils_index = require("../../../../../../utils/index.js"); const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({ __name: "message-stream", props: { payload: { default: () => "" } }, setup(__props) { const { ref, computed, withDefaults, defineProps, watch } = aiDeskCustomerUniapp_adapterVue.vue; const props = __props; const isStreaming = ref(false); const chunks = ref(""); const isFinished = ref(true); const prevChunksLength = ref(0); const streamContent = ref(""); const displayedContent = computed(() => aiDeskCustomerUniapp_components_CustomerServiceChat_messageList_messageElements_messageDesk_messageDeskElements_marked.parseMarkdown(streamContent.value)); const typeWriter = new aiDeskCustomerUniapp_components_CustomerServiceChat_messageList_messageElements_messageDesk_messageDeskElements_typeWriter.TypeWriter({ onTyping: (item) => { streamContent.value += item; }, onComplete() { isStreaming.value = false; } }); function startStreaming(content) { if (!isStreaming.value) { isStreaming.value = true; typeWriter.add(content); typeWriter.start(); } else { typeWriter.add(content); } } watch( () => props.payload, (newValue, oldValue) => { var _a, _b; if (newValue === oldValue) { return; } const _payloadObject = aiDeskCustomerUniapp_utils_index.JSONToObject(props.payload); chunks.value = Array.isArray(_payloadObject.chunks) ? _payloadObject.chunks.join("") : _payloadObject.chunks; isFinished.value = _payloadObject.isFinished === 1; if (newValue && !oldValue && isFinished.value) { streamContent.value = chunks.value; } else { const _newChunksToAdd = (_a = chunks.value) == null ? void 0 : _a.slice(prevChunksLength.value); startStreaming([_newChunksToAdd]); } prevChunksLength.value = (_b = chunks.value) == null ? void 0 : _b.length; }, { deep: true, immediate: true } ); return (_ctx, _cache) => { return { a: common_vendor.unref(displayedContent) }; }; } }); const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-c3be0523"]]); wx.createComponent(Component);