Skip to content

Commit 2d61834

Browse files
authored
fix: html_render supports chatUserProfile function (#4934)
1 parent d186bac commit 2d61834

2 files changed

Lines changed: 58 additions & 13 deletions

File tree

ui/src/components/ai-chat/index.vue

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ provide('getSelectModelList', (params: any) => {
208208
return loadSharedApi({ type: 'model', systemType: 'workspace' }).getSelectModelList(params)
209209
}
210210
})
211+
provide('chatUserProfile', () => {
212+
if (props.type === 'ai-chat') {
213+
if (chatUser.chat_profile?.authentication_type === 'login') {
214+
return chatUser.getChatUserProfile()
215+
}
216+
}
217+
return Promise.resolve(null)
218+
})
211219
212220
const transcribing = ref<boolean>(false)
213221
defineOptions({ name: 'AiChat' })
@@ -798,16 +806,6 @@ const handleScroll = () => {
798806
}
799807
}
800808
}
801-
onBeforeMount(() => {
802-
window.chatUserProfile = () => {
803-
if (props.type === 'ai-chat') {
804-
if (chatUser.chat_profile?.authentication_type === 'login') {
805-
return chatUser.getChatUserProfile()
806-
}
807-
}
808-
return Promise.resolve(null)
809-
}
810-
})
811809
812810
function parseTransform(transformStr: string) {
813811
const result = { scale: 1, translateX: 0, translateY: 0, translateZ: 0 }

ui/src/components/markdown/HtmlRander.vue

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
</template>
1010

1111
<script setup lang="ts">
12-
import { computed, ref, onMounted, onBeforeUnmount } from 'vue'
13-
12+
import { computed, ref, onMounted, onBeforeUnmount, inject } from 'vue'
13+
const chatUserProfile = inject('chatUserProfile') as any
1414
const htmlRef = ref<HTMLIFrameElement>()
1515
const props = withDefaults(
1616
defineProps<{
@@ -37,6 +37,40 @@ function createIframeHtml(sourceHtml: string) {
3737
* { margin: 0; padding: 0; box-sizing: border-box; }
3838
html, body { margin: 0 !important; padding: 0 !important; overflow: hidden; }
3939
</style>
40+
<script>
41+
const _INSTANCE_ID = '${instanceId}';
42+
window.chatUserProfile=function chatUserProfile() {
43+
return new Promise((resolve, reject) => {
44+
const requestId = Date.now() + '_' + Math.random()
45+
46+
function handler(e) {
47+
const data = e.data
48+
49+
if (
50+
data?.type === 'chatUserProfile:response' &&
51+
data.requestId === requestId
52+
) {
53+
window.removeEventListener('message', handler)
54+
resolve(data.data)
55+
}
56+
}
57+
window.addEventListener('message', handler)
58+
parent.postMessage(
59+
{
60+
type: 'chatUserProfile',
61+
requestId,
62+
instanceId: _INSTANCE_ID
63+
},
64+
'*'
65+
)
66+
67+
setTimeout(() => {
68+
window.removeEventListener('message', handler)
69+
reject(new Error('timeout'))
70+
}, 10000)
71+
})
72+
}
73+
<\/script>
4074
</head>
4175
<body>
4276
${sourceHtml}
@@ -46,7 +80,6 @@ const INSTANCE_ID = '${instanceId}';
4680
function sendMessage(message) {
4781
parent.postMessage({ type: 'chatMessage', instanceId: INSTANCE_ID, message }, '*');
4882
}
49-
5083
let lastSentHeight = 0;
5184
let timer = null;
5285
@@ -88,6 +121,20 @@ function onMessage(e: MessageEvent) {
88121
if (e.data.type === 'chatMessage') {
89122
props.sendMessage?.(e.data.message, 'new')
90123
}
124+
if (e.data?.type === 'chatUserProfile') {
125+
const iframe = htmlRef.value
126+
if (!iframe) return
127+
chatUserProfile().then((ok: any) => {
128+
iframe.contentWindow?.postMessage(
129+
{
130+
type: 'chatUserProfile:response',
131+
requestId: e.data.requestId,
132+
data: ok,
133+
},
134+
'*',
135+
)
136+
})
137+
}
91138
}
92139
93140
onMounted(() => {

0 commit comments

Comments
 (0)