wangzhibo
10 天以前 220e4fab27b7f419b3e89e2f2d28ae789aac09d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<route lang="jsonc" type="page">
{
  "layout": "default",
  "style": {
    "navigationBarTitleText": "记录详情",
    "navigationStyle": "custom"
  }
}
</route>
 
<script lang="ts" setup>
import type { ParentHistoryChat, ParentHistoryDetail } from '@/api/parent'
import { onLoad, onUnload } from '@dcloudio/uni-app'
import { computed, ref } from 'vue'
import { getAudioId } from '@/api/chat-history/chat-history'
import { getParentHistoryDetail } from '@/api/parent'
import { t } from '@/i18n'
import { debounce, getEnvBaseUrl } from '@/utils'
import { toast } from '@/utils/toast'
import { durationMsOf, formatDuration, formatStartedAt, sourceLabel, statusLabel } from './historyText'
 
defineOptions({
  name: 'ParentHistoryDetail',
})
 
const instanceId = ref('')
const agentId = ref('')
const macAddress = ref('')
const loading = ref(false)
const detail = ref<ParentHistoryDetail | null>(null)
const audioContext = ref<UniApp.InnerAudioContext | null>(null)
const playingAudioId = ref<string | null>(null)
 
const headerMeta = computed(() => {
  if (!detail.value)
    return ''
  return [
    formatStartedAt(detail.value.startedAt),
    formatDuration(durationMsOf(detail.value), t),
    sourceLabel(detail.value.source, t),
  ].filter(Boolean).join(' · ')
})
 
function goBack() {
  uni.navigateBack()
}
 
function parseUserMessage(content: string) {
  try {
    return JSON.parse(content)
  }
  catch {
    return null
  }
}
 
function chatText(message: ParentHistoryChat): string {
  if (message.chatType === 1) {
    const parsed = parseUserMessage(message.content)
    return parsed?.content || message.content
  }
  return message.content
}
 
async function loadDetail() {
  if (!instanceId.value || !agentId.value || !macAddress.value)
    return
  loading.value = true
  try {
    detail.value = await getParentHistoryDetail(instanceId.value, macAddress.value, agentId.value)
  }
  catch (error) {
    console.error('load parent history detail failed', error)
    toast.error(t('parentHistory.loadFailed'))
    detail.value = null
  }
  finally {
    loading.value = false
  }
}
 
function destroyAudio() {
  const ctx = audioContext.value
  audioContext.value = null
  playingAudioId.value = null
  if (!ctx)
    return
  ctx.onEnded(() => {})
  ctx.onError(() => {})
  try {
    ctx.stop()
  }
  catch {
    // already stopped
  }
  ctx.destroy()
}
 
const playAudio = debounce(async (audioId: string) => {
  if (!audioId) {
    toast.error(t('parentHistory.noAudio'))
    return
  }
  try {
    if (playingAudioId.value === audioId) {
      destroyAudio()
      return
    }
    destroyAudio()
    const downloadId = await getAudioId(audioId)
    const audioUrl = `${getEnvBaseUrl()}/agent/play/${downloadId}`
    audioContext.value = uni.createInnerAudioContext()
    audioContext.value.src = audioUrl
    playingAudioId.value = audioId
    audioContext.value.onEnded(() => {
      destroyAudio()
    })
    audioContext.value.onError(() => {
      toast.error(t('chatHistory.audioPlayFailed'))
      destroyAudio()
    })
    audioContext.value.play()
  }
  catch (error) {
    console.error('play history audio failed', error)
    toast.error(t('chatHistory.playAudioFailed'))
    destroyAudio()
  }
}, 400)
 
onLoad((options) => {
  if (options?.id)
    instanceId.value = String(options.id)
  if (options?.agentId)
    agentId.value = String(options.agentId)
  if (options?.macAddress)
    macAddress.value = decodeURIComponent(String(options.macAddress))
  loadDetail()
})
 
onUnload(() => {
  destroyAudio()
})
</script>
 
<template>
  <view class="history-detail-page">
    <wd-navbar :title="t('parentHistory.detailTitle')" safe-area-inset-top>
      <template #left>
        <wd-icon name="arrow-left" size="18" @click="goBack" />
      </template>
    </wd-navbar>
 
    <view v-if="loading && !detail" class="flex flex-col items-center py-[80rpx]">
      <wd-loading color="#336cff" />
      <text class="mt-[16rpx] text-[28rpx] text-[#666]">
        {{ t('common.loading') }}
      </text>
    </view>
 
    <view v-else-if="detail" class="px-[20rpx] py-[16rpx] pb-[40rpx]">
      <view class="mb-[16rpx] rounded-[20rpx] bg-white p-[28rpx]">
        <view class="mb-[8rpx] flex items-center justify-between">
          <text class="text-[32rpx] text-[#232338] font-semibold">
            {{ detail.sceneName || detail.scene }}
          </text>
          <text
            class="rounded-[8rpx] px-[12rpx] py-[4rpx] text-[22rpx] text-white"
            :class="detail.status === 'completed' ? 'bg-[#2ba471]' : detail.status === 'running' ? 'bg-[#336cff]' : 'bg-[#9d9ea3]'"
          >
            {{ statusLabel(detail, t) }}
          </text>
        </view>
        <text class="text-[26rpx] text-[#65686f]">
          {{ headerMeta }}
        </text>
      </view>
 
      <view v-if="detail.kind === 'extensive'" class="rounded-[20rpx] bg-white p-[28rpx]">
        <text class="block text-[28rpx] leading-[1.6] text-[#65686f]">
          {{ t('parentHistory.extensiveHint') }}
        </text>
      </view>
 
      <view v-else-if="detail.kind === 'shadowing'">
        <view v-if="detail.turns.length === 0" class="rounded-[20rpx] bg-white p-[48rpx] text-center text-[26rpx] text-[#999]">
          {{ t('parentHistory.emptyTurns') }}
        </view>
        <view
          v-for="turn in detail.turns"
          :key="turn.index"
          class="mb-[16rpx] rounded-[20rpx] border-[1rpx] border-[#eeeeee] bg-white p-[28rpx]"
        >
          <view class="mb-[12rpx] flex items-center justify-between">
            <text class="text-[24rpx] text-[#9d9ea3]">
              {{ t('parentHistory.sentence', { n: (turn.index ?? 0) + 1 }) }}
            </text>
            <view
              v-if="turn.audioId"
              class="text-[#336cff]"
              @click="playAudio(turn.audioId)"
            >
              <wd-icon
                :name="playingAudioId === turn.audioId ? 'pause-circle-filled' : 'play-circle-filled'"
                size="22"
              />
            </view>
          </view>
          <text class="mb-[8rpx] block text-[24rpx] text-[#9d9ea3]">
            {{ t('parentHistory.expected') }}
          </text>
          <text class="mb-[16rpx] block text-[28rpx] text-[#232338]">
            {{ turn.expectedText || t('parentHistory.noExpected') }}
          </text>
          <text class="mb-[8rpx] block text-[24rpx] text-[#9d9ea3]">
            {{ t('parentHistory.asr') }}
          </text>
          <text class="block text-[28rpx] text-[#232338]">
            {{ turn.asrText || t('parentHistory.noAsr') }}
          </text>
        </view>
      </view>
 
      <view v-else>
        <view v-if="!detail.chats || detail.chats.length === 0" class="rounded-[20rpx] bg-white p-[48rpx] text-center text-[26rpx] text-[#999]">
          {{ t('parentHistory.noChat') }}
        </view>
        <view v-else class="flex flex-col gap-[20rpx]">
          <view
            v-for="(message, index) in detail.chats"
            :key="index"
            class="w-full flex"
            :class="message.chatType === 1 ? 'justify-end' : 'justify-start'"
          >
            <view
              class="max-w-[80%] rounded-[20rpx] p-[24rpx]"
              :class="message.chatType === 1 ? 'bg-[#336cff] text-white' : 'border-[1rpx] border-[#eeeeee] bg-white text-[#232338]'"
            >
              <view class="flex items-center gap-[12rpx]">
                <view
                  v-if="message.audioId"
                  class="flex-shrink-0"
                  :class="message.chatType === 1 ? 'text-white' : 'text-[#336cff]'"
                  @click="playAudio(message.audioId)"
                >
                  <wd-icon
                    :name="playingAudioId === message.audioId ? 'pause-circle-filled' : 'play-circle-filled'"
                    size="20"
                  />
                </view>
                <text class="text-[28rpx] leading-[1.4]">
                  {{ chatText(message) }}
                </text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>
 
<style scoped>
.history-detail-page {
  min-height: 100%;
  background: #f5f7fb;
}
</style>