腾讯张军:WorkBuddy 鸿蒙版专门上线了一个其他主流系统目前无法实现的神仙功能

发布时间: 2026-07-17 · 来源: 新能源产业网 · 阅读量: 2963
business image 1 digital image 2 code image 3

2509 → 11.2510 ↓ └→ 12.2510 → 12.2511 → 12.

on('audio_chunk') def on_audio_chunk(data): sid = request.

import OpenAI from 'openai'import type { LlmConfig, ChatMessage } from '../types'import { LLM_CONFIG } from '../constants'class LlmService { private openai: OpenAI | null = null private currentApiKey: string = '' private initClient(config: LlmConfig): void { if (this.currentApiKey === config.apiKey && this.openai) { return // 避免重复初始化 } const baseURL = config.baseURL || LLM_CONFIG.BASE_URL this.openai = new OpenAI({ apiKey: config.apiKey, dangerouslyAllowBrowser: true, // 允许浏览器端调用 baseURL: baseURL, // 确保使用 fetch API 支持流式 fetch: (url, init) => { console.log('LLM请求URL:', url) console.log('LLM请求配置:', { method: init?.method, headers: init?.headers, body: init?.body }) return fetch(url, init) } }) this.currentApiKey = config.apiKey } // 普通对话模式 async sendMessage(config: LlmConfig, userMessage: string): Promise { this.initClient(config) if (!this.openai) { throw new Error('LLM客户端未初始化') } const messages: ChatMessage[] = [ { role: 'system', content: LLM_CONFIG.SYSTEM_PROMPT }, { role: 'user', content: userMessage } ] try { const completion = await this.openai.chat.completions.create({ messages, model: config.model }) const response = completion.choices[0]?.message?.content return response || null } catch (error) { console.error('LLM请求失败:', error) throw error } } // 流式对话模式(核心) async sendMessageWithStream(config: LlmConfig, userMessage: string): Promise { this.initClient(config) if (!this.openai) { throw new Error('LLM客户端未初始化') } const messages: ChatMessage[] = [ { role: 'system', content: LLM_CONFIG.SYSTEM_PROMPT }, { role: 'user', content: userMessage } ] try { const stream = await this.openai.chat.completions.create({ messages, model: config.model, stream: true // 开启流式输出 }) // 返回异步迭代器,逐字输出 return (async function* () { let chunkCount = 0 for await (const part of stream) { chunkCount++ const content = part.choices[0]?.delta?.content if (content) { yield content } } })() } catch (error) { console.error('流式请求失败:', error) throw error } }}export const llmService = new LlmService()设计要点:dangerouslyAllowBrowser: true:允许在浏览器端直接调用 LLM API,适用于 Demo 场景自定义 fetch:拦截请求用于日志追踪,便于调试流式数据sendMessageWithStream 返回 AsyncIterable:调用方可通过 for await 逐字消费,实现流式播报API Key 缓存:currentApiKey 避免重复初始化 OpenAI 客户端4.5 action-manager.ts - 流式播报动作队列流式播报是灵引的核心交互机制。当 LLM 流式返回文本时,需要将文本分段发送给数字人 SDK 进行语音合成和动作驱动。ActionManager 通过队列模式管理这些播报段落,确保按顺序播放。

平均每天不到一篇。关键不是速度,是「读完才写」——这一点我自己花了两三个月才真正养成习惯。

配图