Skip to content

Commit bd5d6f5

Browse files
author
linyuan.yang
committed
小爱 渠道
1 parent 1163104 commit bd5d6f5

6 files changed

Lines changed: 78 additions & 81 deletions

File tree

‎packages/admin/src/views/chat/ChannelsView.vue‎

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -459,32 +459,6 @@ const passwordVisible = ref<Record<string, boolean>>({})
459459
460460
const actionState = ref<Record<string, { loading: boolean; qrUrl?: string; qrLink?: string; qrType?: 'image' | 'link'; status?: string; error?: string }>>({})
461461
462-
const pasteJson = ref('')
463-
const pasteError = ref('')
464-
465-
function applyPasteJson() {
466-
pasteError.value = ''
467-
const raw = pasteJson.value.trim()
468-
if (!raw) { pasteError.value = '请粘贴 JSON 后再点击'; return }
469-
let obj: any
470-
try { obj = JSON.parse(raw) } catch (e: any) { pasteError.value = `JSON 解析失败: ${e.message}`; return }
471-
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) { pasteError.value = '需要一个 JSON 对象'; return }
472-
const schema = currentSchema.value
473-
let applied = 0
474-
const skipped: string[] = []
475-
for (const [k, v] of Object.entries(obj)) {
476-
if (k in schema) {
477-
form.value.config[k] = v as any
478-
applied++
479-
} else {
480-
skipped.push(k)
481-
}
482-
}
483-
if (applied === 0) { pasteError.value = `无可用字段(schema 内不存在: ${skipped.join(', ')})`; return }
484-
pasteJson.value = ''
485-
show(`已填入 ${applied} 个字段${skipped.length ? `(忽略 ${skipped.join(', ')})` : ''}`)
486-
}
487-
488462
function clearActionState() {
489463
actionState.value = {}
490464
}
@@ -885,16 +859,6 @@ async function refresh() {
885859
</SFormSection>
886860

887861
<SFormSection v-if="Object.keys(currentSchema).length > 0" :title="t('channels.section_plugin')">
888-
<SFormItem label="粘贴凭据 JSON">
889-
<details class="paste-json-block">
890-
<summary>从辅助工具导入</summary>
891-
<STextarea v-model="pasteJson" :rows="6" placeholder='{"userId":"...","passToken":"...","deviceId":"...","device":"客厅小爱"}' />
892-
<div class="paste-json-actions">
893-
<SButton type="outline" size="sm" @click="applyPasteJson">解析并填入</SButton>
894-
<span v-if="pasteError" class="qr-msg qr-msg-error">{{ pasteError }}</span>
895-
</div>
896-
</details>
897-
</SFormItem>
898862
<template v-for="(field, key) in currentSchema" :key="key">
899863
<SFormItem v-if="field.type === 'qrcode'" :label="field.label">
900864
<div class="qrcode-block">
@@ -1153,10 +1117,6 @@ async function refresh() {
11531117
.qr-msg-success { color: var(--sui-success); }
11541118
.qr-msg-error { color: var(--sui-danger); }
11551119
1156-
.paste-json-block summary { cursor: pointer; font-size: var(--sui-fs-sm); color: var(--sui-fg-muted); margin-bottom: var(--sui-sp-2); }
1157-
.paste-json-block[open] summary { color: var(--sui-fg); }
1158-
.paste-json-actions { display: flex; align-items: center; gap: var(--sui-sp-3); margin-top: var(--sui-sp-2); }
1159-
11601120
.session-avatar {
11611121
width: 28px;
11621122
height: 28px;

‎packages/channel.xiaoai/src/XiaoaiService.ts‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { speak } from './speaker';
99
import type { AuthedAccount } from './mi/types';
1010
import { XiaoaiSessionHandler } from './XiaoaiSessionHandler';
1111

12+
export type XiaoaiAuthMode = 'password' | 'passToken';
13+
1214
export interface XiaoaiMessageArgs extends ChannelMessageArgs {
1315
accountUserId: string;
1416
deviceId: string;
@@ -17,10 +19,10 @@ export interface XiaoaiMessageArgs extends ChannelMessageArgs {
1719

1820
export interface XiaoaiServiceOptions {
1921
userId: string;
20-
password?: string;
21-
passToken?: string;
22-
deviceId?: string;
23-
device: string;
22+
mode: XiaoaiAuthMode;
23+
credential: string;
24+
loginDeviceId?: string;
25+
deviceName: string;
2426
heartbeat: number;
2527
textChunkLimit: number;
2628
volume?: number;
@@ -32,7 +34,7 @@ export interface XiaoaiServiceOptions {
3234
export class XiaoaiService implements IChannelService {
3335
private authed: AuthedAccount | undefined;
3436
private poller: MessagePoller | undefined;
35-
private deviceId: string = '';
37+
private speakerDeviceId: string = '';
3638
private logger?: ILogger;
3739
private options: XiaoaiServiceOptions;
3840

@@ -46,8 +48,8 @@ export class XiaoaiService implements IChannelService {
4648
}
4749

4850
async sendTextToSession(_sessionId: string, text: string): Promise<void> {
49-
if (!this.authed || !this.deviceId) return;
50-
await speak(this.authed, this.deviceId, text, {
51+
if (!this.authed || !this.speakerDeviceId) return;
52+
await speak(this.authed, this.speakerDeviceId, text, {
5153
chunkLimit: this.options.textChunkLimit,
5254
volume: this.options.volume,
5355
});
@@ -66,19 +68,21 @@ export class XiaoaiService implements IChannelService {
6668
}
6769

6870
async start(): Promise<void> {
69-
const { userId, password, passToken, deviceId, device } = this.options;
71+
const { userId, mode, credential, loginDeviceId, deviceName } = this.options;
72+
const password = mode === 'password' ? credential : '';
73+
const passToken = mode === 'passToken' ? credential : undefined;
7074

7175
this.logger?.info(`XiaoAi logging in: ${userId}`);
72-
this.authed = await login({ userId, password: password ?? '', passToken, deviceId });
76+
this.authed = await login({ userId, password, passToken, deviceId: loginDeviceId });
7377
this.logger?.info(`XiaoAi logged in: ${userId}`);
7478

7579
const allDevices = await getDeviceList(this.authed);
7680
this.logger?.info(`XiaoAi found ${allDevices.length} devices`);
7781

78-
const matched = findDeviceByName(allDevices, device);
82+
const matched = findDeviceByName(allDevices, deviceName);
7983
if (!matched) {
8084
const available = allDevices.map((d) => d.name).join(', ');
81-
throw new Error(`Device "${device}" not found. Available: ${available}`);
85+
throw new Error(`Device "${deviceName}" not found. Available: ${available}`);
8286
}
8387

8488
this.poller = new MessagePoller(
@@ -87,8 +91,8 @@ export class XiaoaiService implements IChannelService {
8791
(msg) => this.handleMessage(msg),
8892
this.logger,
8993
);
90-
this.deviceId = matched.deviceID;
91-
this.poller.startDevice(matched.deviceID, device);
94+
this.speakerDeviceId = matched.deviceID;
95+
this.poller.startDevice(matched.deviceID, deviceName);
9296
}
9397

9498
private async handleMessage(msg: PollingMessage): Promise<void> {

‎packages/channel.xiaoai/src/plugin.ts‎

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,26 @@ import {
22
ChannelPlugin, ChannelPluginContext, IChannelService, ConfigFieldType,
33
type MessageContent,
44
} from 'channel.base';
5-
import { XiaoaiService } from './XiaoaiService';
5+
import { XiaoaiService, type XiaoaiAuthMode } from './XiaoaiService';
66
import type { XiaoaiMessageArgs } from './XiaoaiService';
77

8+
function readString(value: unknown): string {
9+
return typeof value === 'string' ? value.trim() : '';
10+
}
11+
12+
function resolveAuthMode(config: Record<string, any>): XiaoaiAuthMode {
13+
if (config.mode === 'password' || config.mode === 'passToken') return config.mode;
14+
if (readString(config.passToken)) return 'passToken';
15+
if (readString(config.password)) return 'password';
16+
return 'passToken';
17+
}
18+
19+
function resolveCredential(config: Record<string, any>, mode: XiaoaiAuthMode): string {
20+
const credential = readString(config.credential);
21+
if (credential) return credential;
22+
return mode === 'passToken' ? readString(config.passToken) : readString(config.password);
23+
}
24+
825
export const xiaoaiPlugin: ChannelPlugin = {
926
type: 'xiaoai',
1027
label: '小爱同学',
@@ -16,26 +33,33 @@ export const xiaoaiPlugin: ChannelPlugin = {
1633
required: true,
1734
description: '小米账号 ID',
1835
},
19-
password: {
20-
label: '密码',
21-
type: ConfigFieldType.Password,
22-
description: '小米账号密码(推荐用辅助工具登录,免填密码)',
36+
mode: {
37+
label: '登录方式',
38+
type: ConfigFieldType.Select,
39+
required: true,
40+
default: 'passToken',
41+
description: '推荐使用辅助工具导出的 passToken',
42+
options: [
43+
{ label: 'passToken(推荐)', value: 'passToken' },
44+
{ label: '密码', value: 'password' },
45+
],
2346
},
24-
passToken: {
25-
label: 'passToken',
47+
credential: {
48+
label: '密码 / passToken',
2649
type: ConfigFieldType.Password,
27-
description: '小米 passToken,由辅助工具登录后导出',
50+
required: true,
51+
description: '按登录方式填写小米账号密码或辅助工具导出的 passToken',
2852
},
29-
deviceId: {
30-
label: 'deviceId',
53+
loginDeviceId: {
54+
label: '登录设备 ID',
3155
type: ConfigFieldType.String,
32-
description: 'PassportSDK 设备 ID,由辅助工具一并导出',
56+
description: 'PassportSDK deviceId,由辅助工具一并导出;留空则随机生成',
3357
},
34-
device: {
35-
label: '设备名称',
58+
deviceName: {
59+
label: '音箱名称',
3660
type: ConfigFieldType.String,
3761
required: true,
38-
description: '音箱设备名称(需与米家中一致)',
62+
description: '米家中的小爱音箱名称',
3963
},
4064
heartbeat: {
4165
label: '轮询间隔 (ms)',
@@ -59,19 +83,19 @@ export const xiaoaiPlugin: ChannelPlugin = {
5983
async init(ctx: ChannelPluginContext): Promise<IChannelService | undefined> {
6084
const { config, logger, filterEvent, initSession, onReceiveMessage } = ctx;
6185

62-
const userId = config.userId?.trim();
63-
const password = config.password?.trim();
64-
const passToken = config.passToken?.trim();
65-
const deviceId = config.deviceId?.trim();
66-
const device = config.device?.trim();
67-
if (!userId || !device || (!password && !passToken)) return undefined;
86+
const userId = readString(config.userId);
87+
const mode = resolveAuthMode(config);
88+
const credential = resolveCredential(config, mode);
89+
const loginDeviceId = readString(config.loginDeviceId) || readString(config.deviceId);
90+
const deviceName = readString(config.deviceName) || readString(config.device);
91+
if (!userId || !deviceName || !credential) return undefined;
6892

6993
const service = new XiaoaiService({
7094
userId,
71-
password,
72-
passToken,
73-
deviceId,
74-
device,
95+
mode,
96+
credential,
97+
loginDeviceId,
98+
deviceName,
7599
heartbeat: Number(config.heartbeat) || 1000,
76100
textChunkLimit: Number(config.textChunkLimit) || 200,
77101
volume: config.volume ? Number(config.volume) : undefined,

‎packages/docs-site/guide/channels.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Select the channel type and fill in the credentials, then assign agent + saver +
1313
| WeCom | Bot ID, Secret |
1414
| WeChat | QR code login (credentials auto-populated) |
1515
| OneBot (QQ) | WS Host, WS Port, optional Access Token |
16-
| XiaoAI | Mi account ID, Password, Device name |
16+
| XiaoAI | Mi account ID, Login credential, Speaker name |
1717

1818
## Lark / Feishu
1919

@@ -106,7 +106,9 @@ Talk to your XiaoAi smart speaker as a sbot channel.
106106
| Field | Description |
107107
|-------|-------------|
108108
| 小米 ID | Your Mi account ID |
109-
| 密码 | Mi account password |
110-
| 设备名称 | Target device name (matches a device in your account) |
109+
| 登录方式 | `passToken` from the helper tool, or Mi account password |
110+
| 密码 / passToken | The credential matching the selected login mode |
111+
| 登录设备 ID | Optional PassportSDK `deviceId` from the helper tool |
112+
| 音箱名称 | Target speaker name (matches a device in your Mi account) |
111113

112114
2. The bot logs in to Mi Cloud, listens for voice messages on the device, and replies via TTS through the speaker

‎packages/helper/src/tools/xiaoai-login/XiaoaiLoginView.vue‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ async function switchAccount() {
121121
{{ copiedKey === 'passToken' ? '已复制 ✓' : '复制' }}
122122
</button>
123123
</div>
124+
<div class="field">
125+
<span class="field-label">deviceId</span>
126+
<code class="field-value">{{ creds.deviceId }}</code>
127+
<button class="copy-btn" @click="copyValue('deviceId', creds.deviceId)">
128+
{{ copiedKey === 'deviceId' ? '已复制 ✓' : '复制' }}
129+
</button>
130+
</div>
124131
</div>
125132

126133
<div class="field-group">

‎packages/helper/src/views/HomeView.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const tools: Tool[] = [
1414
{
1515
key: 'xiaoai-login',
1616
label: '小爱登录',
17-
description: '登录小米账号,导出 sbot channel.xiaoai 所需的 userId / passToken / deviceId / device',
17+
description: '登录小米账号,导出 sbot channel.xiaoai 所需的 userId / passToken / loginDeviceId / deviceName',
1818
route: '/xiaoai-login',
1919
},
2020
]

0 commit comments

Comments
 (0)