{"repo_name": "glass", "file_name": "/glass/src/ui/listen/stt/SttView.js", "inference_info": {"prefix_code": "import { html, css, LitElement } from '../../assets/lit-core-2.7.4.min.js';\n\nexport ", "suffix_code": "\n\ncustomElements.define('stt-view', SttView); ", "middle_code": "class SttView extends LitElement {\n static styles = css`\n :host {\n display: block;\n width: 100%;\n }\n .transcription-container {\n overflow-y: auto;\n padding: 12px 12px 16px 12px;\n display: flex;\n flex-direction: column;\n gap: 8px;\n min-height: 150px;\n max-height: 600px;\n position: relative;\n z-index: 1;\n flex: 1;\n }\n .transcription-container::-webkit-scrollbar {\n width: 8px;\n }\n .transcription-container::-webkit-scrollbar-track {\n background: rgba(0, 0, 0, 0.1);\n border-radius: 4px;\n }\n .transcription-container::-webkit-scrollbar-thumb {\n background: rgba(255, 255, 255, 0.3);\n border-radius: 4px;\n }\n .transcription-container::-webkit-scrollbar-thumb:hover {\n background: rgba(255, 255, 255, 0.5);\n }\n .stt-message {\n padding: 8px 12px;\n border-radius: 12px;\n max-width: 80%;\n word-wrap: break-word;\n word-break: break-word;\n line-height: 1.5;\n font-size: 13px;\n margin-bottom: 4px;\n box-sizing: border-box;\n }\n .stt-message.them {\n background: rgba(255, 255, 255, 0.1);\n color: rgba(255, 255, 255, 0.9);\n align-self: flex-start;\n border-bottom-left-radius: 4px;\n margin-right: auto;\n }\n .stt-message.me {\n background: rgba(0, 122, 255, 0.8);\n color: white;\n align-self: flex-end;\n border-bottom-right-radius: 4px;\n margin-left: auto;\n }\n .empty-state {\n display: flex;\n align-items: center;\n justify-content: center;\n height: 100px;\n color: rgba(255, 255, 255, 0.6);\n font-size: 12px;\n font-style: italic;\n }\n `;\n static properties = {\n sttMessages: { type: Array },\n isVisible: { type: Boolean },\n };\n constructor() {\n super();\n this.sttMessages = [];\n this.isVisible = true;\n this.messageIdCounter = 0;\n this._shouldScrollAfterUpdate = false;\n this.handleSttUpdate = this.handleSttUpdate.bind(this);\n }\n connectedCallback() {\n super.connectedCallback();\n if (window.api) {\n window.api.sttView.onSttUpdate(this.handleSttUpdate);\n }\n }\n disconnectedCallback() {\n super.disconnectedCallback();\n if (window.api) {\n window.api.sttView.removeOnSttUpdate(this.handleSttUpdate);\n }\n }\n resetTranscript() {\n this.sttMessages = [];\n this.requestUpdate();\n }\n handleSttUpdate(event, { speaker, text, isFinal, isPartial }) {\n if (text === undefined) return;\n const container = this.shadowRoot.querySelector('.transcription-container');\n this._shouldScrollAfterUpdate = container ? container.scrollTop + container.clientHeight >= container.scrollHeight - 10 : false;\n const findLastPartialIdx = spk => {\n for (let i = this.sttMessages.length - 1; i >= 0; i--) {\n const m = this.sttMessages[i];\n if (m.speaker === spk && m.isPartial) return i;\n }\n return -1;\n };\n const newMessages = [...this.sttMessages];\n const targetIdx = findLastPartialIdx(speaker);\n if (isPartial) {\n if (targetIdx !== -1) {\n newMessages[targetIdx] = {\n ...newMessages[targetIdx],\n text,\n isPartial: true,\n isFinal: false,\n };\n } else {\n newMessages.push({\n id: this.messageIdCounter++,\n speaker,\n text,\n isPartial: true,\n isFinal: false,\n });\n }\n } else if (isFinal) {\n if (targetIdx !== -1) {\n newMessages[targetIdx] = {\n ...newMessages[targetIdx],\n text,\n isPartial: false,\n isFinal: true,\n };\n } else {\n newMessages.push({\n id: this.messageIdCounter++,\n speaker,\n text,\n isPartial: false,\n isFinal: true,\n });\n }\n }\n this.sttMessages = newMessages;\n this.dispatchEvent(new CustomEvent('stt-messages-updated', {\n detail: { messages: this.sttMessages },\n bubbles: true\n }));\n }\n scrollToBottom() {\n setTimeout(() => {\n const container = this.shadowRoot.querySelector('.transcription-container');\n if (container) {\n container.scrollTop = container.scrollHeight;\n }\n }, 0);\n }\n getSpeakerClass(speaker) {\n return speaker.toLowerCase() === 'me' ? 'me' : 'them';\n }\n getTranscriptText() {\n return this.sttMessages.map(msg => `${msg.speaker}: ${msg.text}`).join('\\n');\n }\n updated(changedProperties) {\n super.updated(changedProperties);\n if (changedProperties.has('sttMessages')) {\n if (this._shouldScrollAfterUpdate) {\n this.scrollToBottom();\n this._shouldScrollAfterUpdate = false;\n }\n }\n }\n render() {\n if (!this.isVisible) {\n return html`
`;\n }\n return html`\n')\n .replace(/\\n/g, '
')\n .replace(/\\*\\*(.*?)\\*\\*/g, '$1')\n .replace(/\\*(.*?)\\*/g, '$1')\n .replace(/`([^`]+)`/g, '$1');\n\n responseContainer.innerHTML = `
${basicHtml}
`;\n }\n }\n\n\n requestWindowResize(targetHeight) {\n if (window.api) {\n window.api.askView.adjustWindowHeight(targetHeight);\n }\n }\n\n animateHeaderText(text) {\n this.headerAnimating = true;\n this.requestUpdate();\n\n setTimeout(() => {\n this.headerText = text;\n this.headerAnimating = false;\n this.requestUpdate();\n }, 150);\n }\n\n startHeaderAnimation() {\n this.animateHeaderText('analyzing screen...');\n\n if (this.headerAnimationTimeout) {\n clearTimeout(this.headerAnimationTimeout);\n }\n\n this.headerAnimationTimeout = setTimeout(() => {\n this.animateHeaderText('thinking...');\n }, 1500);\n }\n\n renderMarkdown(content) {\n if (!content) return '';\n\n if (this.isLibrariesLoaded && this.marked) {\n return this.parseMarkdown(content);\n }\n\n return content\n .replace(/\\*\\*(.*?)\\*\\*/g, '$1')\n .replace(/\\*(.*?)\\*/g, '$1')\n .replace(/`(.*?)`/g, '$1');\n }\n\n fixIncompleteMarkdown(text) {\n if (!text) return text;\n\n // 불완전한 볼드체 처리\n const boldCount = (text.match(/\\*\\*/g) || []).length;\n if (boldCount % 2 === 1) {\n text += '**';\n }\n\n // 불완전한 이탤릭체 처리\n const italicCount = (text.match(/(? closeBrackets) {\n text += ']';\n }\n\n const openParens = (text.match(/\\]\\(/g) || []).length;\n const closeParens = (text.match(/\\)\\s*$/g) || []).length;\n if (openParens > closeParens && text.endsWith('(')) {\n text += ')';\n }\n\n return text;\n }\n\n\n async handleCopy() {\n if (this.copyState === 'copied') return;\n\n let responseToCopy = this.currentResponse;\n\n if (this.isDOMPurifyLoaded && this.DOMPurify) {\n const testHtml = this.renderMarkdown(responseToCopy);\n const sanitized = this.DOMPurify.sanitize(testHtml);\n\n if (this.DOMPurify.removed && this.DOMPurify.removed.length > 0) {\n console.warn('Unsafe content detected, copy blocked');\n return;\n }\n }\n\n const textToCopy = `Question: ${this.currentQuestion}\\n\\nAnswer: ${responseToCopy}`;\n\n try {\n await navigator.clipboard.writeText(textToCopy);\n console.log('Content copied to clipboard');\n\n this.copyState = 'copied';\n this.requestUpdate();\n\n if (this.copyTimeout) {\n clearTimeout(this.copyTimeout);\n }\n\n this.copyTimeout = setTimeout(() => {\n this.copyState = 'idle';\n this.requestUpdate();\n }, 1500);\n } catch (err) {\n console.error('Failed to copy:', err);\n }\n }\n\n async handleLineCopy(lineIndex) {\n const originalLines = this.currentResponse.split('\\n');\n const lineToCopy = originalLines[lineIndex];\n\n if (!lineToCopy) return;\n\n try {\n await navigator.clipboard.writeText(lineToCopy);\n console.log('Line copied to clipboard');\n\n // '복사됨' 상태로 UI 즉시 업데이트\n this.lineCopyState = { ...this.lineCopyState, [lineIndex]: true };\n this.requestUpdate(); // LitElement에 UI 업데이트 요청\n\n // 기존 타임아웃이 있다면 초기화\n if (this.lineCopyTimeouts && this.lineCopyTimeouts[lineIndex]) {\n clearTimeout(this.lineCopyTimeouts[lineIndex]);\n }\n\n // ✨ 수정된 타임아웃: 1.5초 후 '복사됨' 상태 해제\n this.lineCopyTimeouts[lineIndex] = setTimeout(() => {\n const updatedState = { ...this.lineCopyState };\n delete updatedState[lineIndex];\n this.lineCopyState = updatedState;\n this.requestUpdate(); // UI 업데이트 요청\n }, 1500);\n } catch (err) {\n console.error('Failed to copy line:', err);\n }\n }\n\n async handleSendText(e, overridingText = '') {\n const textInput = this.shadowRoot?.getElementById('textInput');\n const text = (overridingText || textInput?.value || '').trim();\n // if (!text) return;\n\n textInput.value = '';\n\n if (window.api) {\n window.api.askView.sendMessage(text).catch(error => {\n console.error('Error sending text:', error);\n });\n }\n }\n\n handleTextKeydown(e) {\n // Fix for IME composition issue: Ignore Enter key presses while composing.\n if (e.isComposing) {\n return;\n }\n\n const isPlainEnter = e.key === 'Enter' && !e.shiftKey && !e.metaKey && !e.ctrlKey;\n const isModifierEnter = e.key === 'Enter' && (e.metaKey || e.ctrlKey);\n\n if (isPlainEnter || isModifierEnter) {\n e.preventDefault();\n this.handleSendText();\n }\n }\n\n updated(changedProperties) {\n super.updated(changedProperties);\n \n // ✨ isLoading 또는 currentResponse가 변경될 때마다 뷰를 다시 그립니다.\n if (changedProperties.has('isLoading') || changedProperties.has('currentResponse')) {\n this.renderContent();\n }\n \n if (changedProperties.has('showTextInput') || changedProperties.has('isLoading') || changedProperties.has('currentResponse')) {\n this.adjustWindowHeightThrottled();\n }\n \n if (changedProperties.has('showTextInput') && this.showTextInput) {\n this.focusTextInput();\n }\n }\n\n firstUpdated() {\n setTimeout(() => this.adjustWindowHeight(), 200);\n }\n\n\n getTruncatedQuestion(question, maxLength = 30) {\n if (!question) return '';\n if (question.length <= maxLength) return question;\n return question.substring(0, maxLength) + '...';\n }\n\n\n\n render() {\n const hasResponse = this.isLoading || this.currentResponse || this.isStreaming;\n const headerText = this.isLoading ? 'Thinking...' : 'AI Response';\n\n return html`\n