dsh-plugin-mobile-link 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,186 @@
1
+ /**
2
+ * 作答记录(`+link/question`):把 `ask_user_question` 的问题与答案单独下发。
3
+ *
4
+ * 为什么需要:折叠模式把 `tool/call` / `tool/result` 整段收进摘要,正文与参数都不
5
+ * 下发;但用户刚做出的选择属于**用户自己的输入**,不能因为它来自工具结果就跟着
6
+ * 过程一起消失(SR-02.16)。桌面把这种记录渲染成一条用户消息气泡,手机侧沿用同一
7
+ * 语义:它是会话内容,不是过程。
8
+ *
9
+ * 形状:
10
+ *
11
+ * +link/question {
12
+ * callId,
13
+ * questions: [{ id, question, header? }],
14
+ * answers: [{ id, selected: string[], custom?: string }]
15
+ * }
16
+ *
17
+ * - 只发**已结算**的答案批次;`{pending:true}` 的计时提问、失败或取消的调用不发。
18
+ * - 问题文字从 `tool/call` 参数里取;参数缺失/形状不认识时给空数组,客户端至少
19
+ * 还能显示答案(AR-02.16.01 的降级口径)。
20
+ * - 文字按码元截断且不切开代理对(与预览、搜索片段同一套 `truncateSafe`)。
21
+ */
22
+
23
+ import { truncateSafe } from '../unicode.js'
24
+
25
+ /** 记录类型。以 `+` 开头,不会与宿主事件类型相撞。 */
26
+ export const QUESTION_RECORD_TYPE = '+link/question'
27
+
28
+ /** 会带来问题清单的工具名。 */
29
+ const QUESTION_TOOLS = new Set(['ask_user_question', 'request_user_input'])
30
+
31
+ /** 单个字段(问题、标题、自定义答案)下发的上限;超出截断并加省略号。 */
32
+ export const QUESTION_TEXT_LIMIT = 1000
33
+
34
+ export function isQuestionTool(name) {
35
+ return typeof name === 'string' && QUESTION_TOOLS.has(name)
36
+ }
37
+
38
+ /**
39
+ * 工具结果的正文。与摘要的字节统计、搜索的 `docText` 同一组键。
40
+ * 只取文本块与文本字段,图片/文件不在其中。
41
+ */
42
+ export function resultTextOf(data) {
43
+ const parts = []
44
+ const blocks = [data?.message?.content, data?.content].find((value) => Array.isArray(value)) ?? []
45
+ for (const block of blocks) {
46
+ if (typeof block?.text === 'string' && block.text.length > 0) parts.push(block.text)
47
+ }
48
+ for (const key of ['output', 'result', 'stdout', 'stderr', 'text']) {
49
+ if (typeof data?.[key] === 'string' && data[key].length > 0) parts.push(data[key])
50
+ }
51
+ return parts.join('\n')
52
+ }
53
+
54
+ function parseJson(value) {
55
+ if (typeof value !== 'string') return undefined
56
+ try {
57
+ return JSON.parse(value)
58
+ } catch {
59
+ return undefined
60
+ }
61
+ }
62
+
63
+ /** 截到上限并补省略号;截断点不落在 emoji 中间。 */
64
+ function cap(text) {
65
+ if (typeof text !== 'string') return ''
66
+ if (text.length <= QUESTION_TEXT_LIMIT) return text
67
+ return `${truncateSafe(text, QUESTION_TEXT_LIMIT - 1)}…`
68
+ }
69
+
70
+ /**
71
+ * `tool/call` 参数里的问题清单。
72
+ *
73
+ * 严格配对:任何一项缺 `id` / `question` 就整份不要——半份问题清单会把答案配到
74
+ * 错的问题上,比没有文字更糟。`multi_select` / `multiSelect` 两种拼写都收
75
+ * (宿主 schema 用蛇形,旧夹具可能用驼峰),展示只需要文字。
76
+ */
77
+ export function questionsOf(argumentsValue) {
78
+ const parsed = typeof argumentsValue === 'object' && argumentsValue !== null
79
+ ? argumentsValue
80
+ : parseJson(argumentsValue)
81
+ if (parsed === null || typeof parsed !== 'object') return []
82
+ if (!Array.isArray(parsed.questions) || parsed.questions.length === 0) return []
83
+ const out = []
84
+ for (const question of parsed.questions) {
85
+ if (question === null || typeof question !== 'object') return []
86
+ if (typeof question.id !== 'string' || question.id === '') return []
87
+ if (typeof question.question !== 'string') return []
88
+ out.push({
89
+ id: question.id,
90
+ question: cap(question.question),
91
+ ...(typeof question.header === 'string' && question.header !== ''
92
+ ? { header: cap(question.header) }
93
+ : {}),
94
+ })
95
+ }
96
+ return out
97
+ }
98
+
99
+ /**
100
+ * 工具结果里的答案批次;没结算、形状不认识时返回 null。
101
+ *
102
+ * `answers: []` 保留为空数组(调用方据此不发记录):它表示"没有可显示的答案",
103
+ * 与"结果还没回来"不是同一件事。
104
+ */
105
+ export function answersOf(resultText) {
106
+ const parsed = parseJson(resultText)
107
+ if (parsed === null || typeof parsed !== 'object') return null
108
+ if (parsed.pending === true) return null
109
+ if (!Array.isArray(parsed.answers)) return null
110
+ const out = []
111
+ for (const answer of parsed.answers) {
112
+ if (answer === null || typeof answer !== 'object') return null
113
+ if (typeof answer.id !== 'string' || answer.id === '') return null
114
+ if (!Array.isArray(answer.selected) || !answer.selected.every((item) => typeof item === 'string')) {
115
+ return null
116
+ }
117
+ if (answer.custom !== undefined && typeof answer.custom !== 'string') return null
118
+ out.push({
119
+ id: answer.id,
120
+ selected: answer.selected,
121
+ ...(typeof answer.custom === 'string' && answer.custom.trim() !== ''
122
+ ? { custom: cap(answer.custom) }
123
+ : {}),
124
+ })
125
+ }
126
+ return out
127
+ }
128
+
129
+ /**
130
+ * 一次提问调用 + 它的结果 → 一条待下发的记录。
131
+ *
132
+ * @param {object} input
133
+ * @param {{callId?: string, name?: string, arguments?: unknown}} input.call
134
+ * @param {string} input.resultText 工具结果正文
135
+ * @param {number} input.seq 结果事件的 seq(记录的排序键)
136
+ * @param {number} [input.time] 结果事件时间
137
+ * @returns {object|null} 不是提问、没结算、或 callId 缺失时返回 null
138
+ */
139
+ export function questionRecordFor({ call, resultText, seq, time }) {
140
+ if (!isQuestionTool(call?.name)) return null
141
+ if (typeof call?.callId !== 'string' || call.callId === '') return null
142
+ if (!Number.isInteger(seq)) return null
143
+ const answers = answersOf(resultText)
144
+ if (answers === null || answers.length === 0) return null
145
+ return {
146
+ type: QUESTION_RECORD_TYPE,
147
+ seq,
148
+ ...(Number.isFinite(time) ? { time } : {}),
149
+ data: {
150
+ callId: call.callId,
151
+ questions: questionsOf(call.arguments),
152
+ answers,
153
+ },
154
+ }
155
+ }
156
+
157
+ /**
158
+ * 从原始记录里取出提问结果,供 v1/v2 两条折叠路径共用。
159
+ * @param {Array<{type:string,event:object}>} records
160
+ * @returns {Array<{seq:number,record:object}>} 按 seq 升序
161
+ */
162
+ export function questionRecordsOfEvents(events) {
163
+ const calls = new Map()
164
+ const out = []
165
+ for (const event of events) {
166
+ if (event?.type === 'tool/call' && isQuestionTool(event?.data?.name)) {
167
+ const callId = event?.data?.callId
168
+ if (typeof callId === 'string' && callId !== '') {
169
+ calls.set(callId, { callId, name: event.data.name, arguments: event.data.arguments })
170
+ }
171
+ continue
172
+ }
173
+ if (event?.type !== 'tool/result') continue
174
+ const callId = event?.data?.message?.toolCallId ?? event?.data?.toolCallId ?? event?.data?.callId
175
+ const call = calls.get(callId)
176
+ if (call === undefined) continue
177
+ const record = questionRecordFor({
178
+ call,
179
+ resultText: resultTextOf(event.data),
180
+ seq: event.seq,
181
+ time: event.time,
182
+ })
183
+ if (record !== null) out.push({ seq: event.seq, record })
184
+ }
185
+ return out
186
+ }
package/lib/i18n.js ADDED
@@ -0,0 +1,177 @@
1
+ // CLI and status hints are user-facing too. Keep their catalogs separate from the
2
+ // panel bundle (which is hand-inlined) so the headless path can be localized
3
+ // without pulling the browser client into the CLI.
4
+ const CATALOGS = {
5
+ en: {
6
+ 'cli.registered': 'This computer is registered. Two steps remain:',
7
+ 'cli.step1': 'Restart DSH so the connector uses this identity;',
8
+ 'cli.step2': 'Scan the QR code below with the phone app (same as the Mobile link panel).',
9
+ 'cli.code': 'Pairing code ${code} (valid about ${minutes} minutes, one-time)',
10
+ 'cli.hint': 'You can also open the Mobile link panel for a new code; the connector writes the tokenized endpoint to ~/.dsh/mobile-link/endpoint.json.',
11
+ 'cli.prefetchFailed': 'Could not prefetch a pairing code: ${error}',
12
+ 'cli.prefetchHint': 'After restarting DSH, open the Mobile link panel and scan its QR code with the phone app.',
13
+ 'status.config': 'The local address you configured is wrong or stale.',
14
+ 'status.hostService': 'This host did not expose its local address to the connector; the plugin is probably not loaded into the running host.',
15
+ 'status.dshWebUrl': 'The local address in the environment variable is stale.',
16
+ 'status.unreachable': 'The local DSH is unreachable; check that the host is running.',
17
+ 'status.desktop': 'You are using the official desktop app (the connector is installed in its profile).',
18
+ 'status.web': 'You are using the command-line web host.',
19
+ 'status.unknown': 'The host could not be identified; if it cannot connect, follow the troubleshooting section.',
20
+ 'status.unregistered': 'This computer is not registered with the relay yet. Check the relay address in the Mobile link panel, then click Register.',
21
+ },
22
+ 'zh-Hans': {
23
+ 'cli.registered': '这台电脑已登记。接下来还有两步:',
24
+ 'cli.step1': '重启 DSH,让连接器用上这个身份;',
25
+ 'cli.step2': '手机 App 的「扫码配对」扫下面的二维码(与 DSH 界面左下角「移动端连接」里那张等价)。',
26
+ 'cli.code': '配对码 ${code}(约 ${minutes} 分钟有效,一次性)',
27
+ 'cli.hint': '也可以在这台电脑上点 DSH 界面左下角的「移动端连接」取新码;连接器还会把带令牌地址写进 ~/.dsh/mobile-link/endpoint.json。',
28
+ 'cli.prefetchFailed': '(这次没能预先申请配对码:${error})',
29
+ 'cli.prefetchHint': '重启 DSH 后,在这台电脑的 DSH 界面左下角点「移动端连接」,用手机 App 的「扫码配对」扫里面的二维码。',
30
+ 'status.config': '你手工配置的本机地址不对或已失效',
31
+ 'status.hostService': '当前宿主没有把本机访问方式交给插件(多为插件没装进正在运行的宿主)',
32
+ 'status.dshWebUrl': '环境变量里的本机地址已过期',
33
+ 'status.unreachable': '本机 DSH 不可达;请确认宿主正在运行',
34
+ 'status.desktop': '你正在用官方桌面版(连接器已装进它的档案)',
35
+ 'status.web': '你正在用命令行 web 版',
36
+ 'status.unknown': '无法识别当前宿主;若连不上,请按"排查"一节处理',
37
+ 'status.unregistered': '这台电脑还没有登记到中转。在「移动端连接」面板里确认中转地址,然后点「登记」。',
38
+ },
39
+ 'zh-Hant': {
40
+ 'cli.registered': '這台電腦已登記。接下來還有兩步:',
41
+ 'cli.step1': '重新啟動 DSH,讓連接器使用這個身分;',
42
+ 'cli.step2': '用手機 App 的「掃描 QR code」掃描下方 QR code(與 DSH 介面左下角「手機連結」裡的那張相同)。',
43
+ 'cli.code': '配對碼 ${code}(約 ${minutes} 分鐘內有效,一次性)',
44
+ 'cli.hint': '也可以在這台電腦上點 DSH 介面左下角的「手機連結」取得新碼;連接器也會把帶權杖的位址寫入 ~/.dsh/mobile-link/endpoint.json。',
45
+ 'cli.prefetchFailed': '(這次無法預先取得配對碼:${error})',
46
+ 'cli.prefetchHint': '重新啟動 DSH 後,在這台電腦的 DSH 介面左下角點「手機連結」,用手機 App 的「掃描 QR code」掃描裡面的 QR code。',
47
+ 'status.config': '你手動設定的本機位址不正確或已失效',
48
+ 'status.hostService': '目前宿主沒有把本機存取方式交給外掛(多為外掛沒有載入執行中的宿主)',
49
+ 'status.dshWebUrl': '環境變數中的本機位址已過期',
50
+ 'status.unreachable': '本機 DSH 無法連線;請確認宿主正在執行',
51
+ 'status.desktop': '你正在使用官方桌面版(連接器已安裝在它的設定檔中)',
52
+ 'status.web': '你正在使用命令列 web 版',
53
+ 'status.unknown': '無法辨識目前宿主;若無法連線,請依「疑難排解」一節處理',
54
+ 'status.unregistered': '這台電腦尚未登記到中繼。在「手機連結」面板確認中繼位址,然後點「登記」。',
55
+ },
56
+ ja: {
57
+ 'cli.registered': 'このコンピューターは登録されました。残り 2 ステップです:',
58
+ 'cli.step1': 'DSH を再起動して、コネクタがこの ID を使うようにします。',
59
+ 'cli.step2': 'スマートフォンアプリで下の QR コードをスキャンします(「モバイルリンク」パネルのものと同じです)。',
60
+ 'cli.code': 'ペアリングコード ${code}(約 ${minutes} 分間有効、1 回限り)',
61
+ 'cli.hint': 'DSH 左下の「モバイルリンク」から新しいコードを取得することもできます。コネクタはトークン付きのエンドポイントを ~/.dsh/mobile-link/endpoint.json に書き込みます。',
62
+ 'cli.prefetchFailed': '(ペアリングコードを事前に取得できませんでした:${error})',
63
+ 'cli.prefetchHint': 'DSH を再起動した後、DSH 左下の「モバイルリンク」を開き、スマートフォンアプリで QR コードをスキャンしてください。',
64
+ 'status.config': '手動で設定したローカルアドレスが正しくないか、期限切れです。',
65
+ 'status.hostService': 'このホストはローカルアドレスをコネクタに渡していません。プラグインが実行中のホストに読み込まれていない可能性があります。',
66
+ 'status.dshWebUrl': '環境変数のローカルアドレスが期限切れです。',
67
+ 'status.unreachable': 'ローカルの DSH に到達できません。ホストが実行中か確認してください。',
68
+ 'status.desktop': '公式デスクトップアプリを使用しています(コネクタはそのプロファイルにインストールされています)。',
69
+ 'status.web': 'コマンドライン web ホストを使用しています。',
70
+ 'status.unknown': 'ホストを特定できません。接続できない場合はトラブルシューティングの節を参照してください。',
71
+ 'status.unregistered': 'このコンピューターはまだリレーに登録されていません。「モバイルリンク」パネルでリレーアドレスを確認し、「登録」をクリックしてください。',
72
+ },
73
+ ko: {
74
+ 'cli.registered': '이 컴퓨터가 등록되었습니다. 두 단계가 남았습니다:',
75
+ 'cli.step1': 'DSH를 다시 시작하여 커넥터가 이 ID를 사용하도록 합니다.',
76
+ 'cli.step2': '휴대폰 앱으로 아래 QR 코드를 스캔합니다(모바일 링크 패널과 동일).',
77
+ 'cli.code': '페어링 코드 ${code}(약 ${minutes}분 유효, 일회용)',
78
+ 'cli.hint': 'DSH 왼쪽 아래의 모바일 링크 패널에서 새 코드를 받을 수도 있습니다. 커넥터는 토큰이 포함된 주소를 ~/.dsh/mobile-link/endpoint.json에 기록합니다.',
79
+ 'cli.prefetchFailed': '(페어링 코드를 미리 가져오지 못했습니다: ${error})',
80
+ 'cli.prefetchHint': 'DSH를 다시 시작한 뒤 DSH 왼쪽 아래의 모바일 링크 패널을 열고 휴대폰 앱으로 QR 코드를 스캔하세요.',
81
+ 'status.config': '직접 설정한 로컬 주소가 잘못되었거나 만료되었습니다.',
82
+ 'status.hostService': '이 호스트가 로컬 주소를 커넥터에 전달하지 않았습니다. 플러그인이 실행 중인 호스트에 로드되지 않았을 수 있습니다.',
83
+ 'status.dshWebUrl': '환경 변수의 로컬 주소가 만료되었습니다.',
84
+ 'status.unreachable': '로컬 DSH에 연결할 수 없습니다. 호스트가 실행 중인지 확인하세요.',
85
+ 'status.desktop': '공식 데스크톱 앱을 사용 중입니다(커넥터가 해당 프로필에 설치되어 있습니다).',
86
+ 'status.web': '명령줄 web 호스트를 사용 중입니다.',
87
+ 'status.unknown': '호스트를 식별할 수 없습니다. 연결할 수 없으면 문제 해결 절을 따르세요.',
88
+ 'status.unregistered': '이 컴퓨터는 아직 릴레이에 등록되지 않았습니다. 모바일 링크 패널에서 릴레이 주소를 확인한 뒤 등록을 클릭하세요.',
89
+ },
90
+ es: {
91
+ 'cli.registered': 'Este equipo está registrado. Quedan dos pasos:',
92
+ 'cli.step1': 'Reinicia DSH para que el conector use esta identidad;',
93
+ 'cli.step2': 'Escanea el código QR de abajo con la app del teléfono (el mismo del panel Mobile link).',
94
+ 'cli.code': 'Código de emparejamiento ${code} (válido unos ${minutes} minutos, de un solo uso)',
95
+ 'cli.hint': 'También puedes abrir el panel Mobile link para obtener un código nuevo; el conector escribe el endpoint con token en ~/.dsh/mobile-link/endpoint.json.',
96
+ 'cli.prefetchFailed': 'No se pudo obtener un código de emparejamiento por adelantado: ${error}',
97
+ 'cli.prefetchHint': 'Después de reiniciar DSH, abre el panel Mobile link y escanea su código QR con la app del teléfono.',
98
+ 'status.config': 'La dirección local que configuraste es incorrecta o ha caducado.',
99
+ 'status.hostService': 'Este host no expuso su dirección local al conector; probablemente el plugin no está cargado en el host en ejecución.',
100
+ 'status.dshWebUrl': 'La dirección local de la variable de entorno ha caducado.',
101
+ 'status.unreachable': 'No se puede acceder al DSH local; comprueba que el host esté en ejecución.',
102
+ 'status.desktop': 'Estás usando la app de escritorio oficial (el conector está instalado en su perfil).',
103
+ 'status.web': 'Estás usando el host web de línea de comandos.',
104
+ 'status.unknown': 'No se pudo identificar el host; si no conecta, sigue la sección de solución de problemas.',
105
+ 'status.unregistered': 'Este equipo aún no está registrado en el relay. Comprueba la dirección del relay en el panel Mobile link y pulsa Registrar.',
106
+ },
107
+ 'pt-BR': {
108
+ 'cli.registered': 'Este computador está registrado. Faltam dois passos:',
109
+ 'cli.step1': 'Reinicie o DSH para que o conector use esta identidade;',
110
+ 'cli.step2': 'Escaneie o QR code abaixo com o app do celular (o mesmo do painel Mobile link).',
111
+ 'cli.code': 'Código de pareamento ${code} (válido por cerca de ${minutes} minutos, uso único)',
112
+ 'cli.hint': 'Você também pode abrir o painel Mobile link para obter um novo código; o conector grava o endpoint com token em ~/.dsh/mobile-link/endpoint.json.',
113
+ 'cli.prefetchFailed': 'Não foi possível obter um código de pareamento antecipadamente: ${error}',
114
+ 'cli.prefetchHint': 'Depois de reiniciar o DSH, abra o painel Mobile link e escaneie o QR code com o app do celular.',
115
+ 'status.config': 'O endereço local configurado está incorreto ou expirou.',
116
+ 'status.hostService': 'Este host não expôs o endereço local ao conector; provavelmente o plugin não está carregado no host em execução.',
117
+ 'status.dshWebUrl': 'O endereço local da variável de ambiente expirou.',
118
+ 'status.unreachable': 'O DSH local não está acessível; verifique se o host está em execução.',
119
+ 'status.desktop': 'Você está usando o app de desktop oficial (o conector está instalado no perfil dele).',
120
+ 'status.web': 'Você está usando o host web de linha de comando.',
121
+ 'status.unknown': 'Não foi possível identificar o host; se não conectar, siga a seção de solução de problemas.',
122
+ 'status.unregistered': 'Este computador ainda não está registrado no relay. Verifique o endereço do relay no painel Mobile link e clique em Registrar.',
123
+ },
124
+ fr: {
125
+ 'cli.registered': 'Cet ordinateur est enregistré. Il reste deux étapes :',
126
+ 'cli.step1': "Redémarrez DSH pour que le connecteur utilise cette identité ;",
127
+ 'cli.step2': "Scannez le QR code ci-dessous avec l'application mobile (le même que dans le panneau Mobile link).",
128
+ 'cli.code': 'Code d’appairage ${code} (valable environ ${minutes} minutes, à usage unique)',
129
+ 'cli.hint': "Vous pouvez aussi ouvrir le panneau Mobile link pour obtenir un nouveau code ; le connecteur écrit l'endpoint avec jeton dans ~/.dsh/mobile-link/endpoint.json.",
130
+ 'cli.prefetchFailed': "Impossible de précharger un code d'appairage : ${error}",
131
+ 'cli.prefetchHint': "Après avoir redémarré DSH, ouvrez le panneau Mobile link et scannez son QR code avec l'application mobile.",
132
+ 'status.config': "L'adresse locale configurée est incorrecte ou obsolète.",
133
+ 'status.hostService': "Cet hôte n'a pas exposé son adresse locale au connecteur ; le plugin n'est probablement pas chargé dans l'hôte en cours.",
134
+ 'status.dshWebUrl': "L'adresse locale de la variable d'environnement est obsolète.",
135
+ 'status.unreachable': 'Le DSH local est inaccessible ; vérifiez que l’hôte est en cours d’exécution.',
136
+ 'status.desktop': "Vous utilisez l'application de bureau officielle (le connecteur est installé dans son profil).",
137
+ 'status.web': 'Vous utilisez l’hôte web en ligne de commande.',
138
+ 'status.unknown': "L'hôte n'a pas pu être identifié ; si la connexion échoue, suivez la section de dépannage.",
139
+ 'status.unregistered': "Cet ordinateur n'est pas encore enregistré auprès du relais. Vérifiez l'adresse du relais dans le panneau Mobile link, puis cliquez sur Enregistrer.",
140
+ },
141
+ de: {
142
+ 'cli.registered': 'Dieser Computer ist registriert. Es fehlen noch zwei Schritte:',
143
+ 'cli.step1': 'DSH neu starten, damit der Connector diese Identität verwendet;',
144
+ 'cli.step2': 'Den QR-Code unten mit der Telefon-App scannen (derselbe wie im Mobile-link-Panel).',
145
+ 'cli.code': 'Kopplungscode ${code} (etwa ${minutes} Minuten gültig, einmalig)',
146
+ 'cli.hint': 'Du kannst auch im Mobile-link-Panel einen neuen Code holen; der Connector schreibt den Endpunkt mit Token nach ~/.dsh/mobile-link/endpoint.json.',
147
+ 'cli.prefetchFailed': 'Kopplungscode konnte nicht vorab geholt werden: ${error}',
148
+ 'cli.prefetchHint': 'Nach dem Neustart von DSH das Mobile-link-Panel öffnen und den QR-Code mit der Telefon-App scannen.',
149
+ 'status.config': 'Die manuell konfigurierte lokale Adresse ist falsch oder veraltet.',
150
+ 'status.hostService': 'Dieser Host hat dem Connector seine lokale Adresse nicht übergeben; das Plugin ist vermutlich nicht im laufenden Host geladen.',
151
+ 'status.dshWebUrl': 'Die lokale Adresse in der Umgebungsvariable ist veraltet.',
152
+ 'status.unreachable': 'Das lokale DSH ist nicht erreichbar; prüfe, ob der Host läuft.',
153
+ 'status.desktop': 'Du verwendest die offizielle Desktop-App (der Connector ist in ihrem Profil installiert).',
154
+ 'status.web': 'Du verwendest den Web-Host der Kommandozeile.',
155
+ 'status.unknown': 'Der Host konnte nicht erkannt werden; wenn keine Verbindung möglich ist, folge dem Abschnitt zur Fehlerbehebung.',
156
+ 'status.unregistered': 'Dieser Computer ist noch nicht beim Relay registriert. Prüfe die Relay-Adresse im Mobile-link-Panel und klicke auf Registrieren.',
157
+ },
158
+ }
159
+ const ALIASES = { zh: 'zh-Hans', 'zh-Hans': 'zh-Hans', 'zh-CN': 'zh-Hans', 'zh-Hant': 'zh-Hant', 'zh-TW': 'zh-Hant', 'zh-HK': 'zh-Hant', ja: 'ja', ko: 'ko', es: 'es', pt: 'pt-BR', 'pt-BR': 'pt-BR', fr: 'fr', de: 'de', en: 'en' }
160
+ let language = 'zh-Hans'
161
+ export function resolveLanguage(tag) {
162
+ if (!tag) return null
163
+ const value = String(tag).trim().replace('_', '-')
164
+ if (ALIASES[value]) return ALIASES[value]
165
+ const base = value.toLowerCase().split('-')[0]
166
+ return ALIASES[base] ?? null
167
+ }
168
+ export function setLanguage(tag) {
169
+ const next = resolveLanguage(tag)
170
+ if (next) language = next
171
+ return language
172
+ }
173
+ export function currentLanguage() { return language }
174
+ export function T(key, vars = {}) {
175
+ const text = CATALOGS[language]?.[key] ?? CATALOGS['zh-Hans'][key] ?? key
176
+ return text.replace(/\$\{(\w+)\}/g, (m, name) => (name in vars ? String(vars[name]) : m))
177
+ }
@@ -0,0 +1,121 @@
1
+ /**
2
+ * 预览 ↔ 原图 的关联索引(Host 侧持久状态)。
3
+ *
4
+ * 为什么需要它:Host 的 `session/attachment` 只认**图像**引用,没有按文件引用
5
+ * 读回字节的 RPC;而一张图的原图是逐字节保存的 file 附件(`FileAttachmentRef`),
6
+ * 与 Host 归一化后的预览图是两个内容地址。手机要"按需取原图",就必须能从预览的
7
+ * `attachmentId` 找到原图引用。
8
+ *
9
+ * 关联放在**电脑上**而不是 App 里:需求要求重装、重新配对、换设备后仍能取回原图,
10
+ * 而客户端的任何缓存都会在重装时消失。键是预览的 `attachmentId`(内容哈希,全局唯一),
11
+ * 所以索引不需要会话信息。写这条索引的有两处:连接器(手机发送的图)与
12
+ * `send_image` 插件(工具结果图),格式见 `docs/IMAGE-VARIANTS.md` 与 /tmp 契约 §1.4。
13
+ *
14
+ * 追加写而不是重写:两个写者(连接器与图片插件)在同一进程里,但各自是独立的模块实例,
15
+ * 读-改-写会互相覆盖。`O_APPEND` 的单次整行写入在 POSIX 上不会交错;读取端按行解析、
16
+ * 同一预览取最后一条。索引坏掉最坏的后果是"原图不可用",绝不会变成错误或数据丢失。
17
+ */
18
+
19
+ import { closeSync, mkdirSync, openSync, readFileSync, statSync, writeSync } from 'node:fs'
20
+ import { dirname, join } from 'node:path'
21
+
22
+ import { dshHome } from './state.js'
23
+
24
+ /** 索引版本;字段变化时递增,读取端忽略不认识的版本。 */
25
+ export const INDEX_VERSION = 1
26
+
27
+ /** 索引文件最大可读字节数:超过说明文件被写坏了,宁可不认也不要整读进内存。 */
28
+ export const MAX_INDEX_BYTES = 8 * 1024 * 1024
29
+
30
+ /** 索引文件路径(`$DSH_HOME/mobile-link/image-originals.jsonl`)。 */
31
+ export function indexPath(home = dshHome()) {
32
+ return join(home, 'mobile-link', 'image-originals.jsonl')
33
+ }
34
+
35
+ /** 一条索引记录。字段与图片插件的写入端必须一致。 */
36
+ export function indexEntry({ previewId, original, source = 'composer', savedAt = new Date().toISOString() }) {
37
+ return {
38
+ v: INDEX_VERSION,
39
+ previewId,
40
+ original: {
41
+ attachmentId: original.attachmentId,
42
+ name: original.name,
43
+ bytes: original.bytes,
44
+ ...(original.mediaType === undefined ? {} : { mediaType: original.mediaType }),
45
+ },
46
+ savedAt,
47
+ source,
48
+ }
49
+ }
50
+
51
+ /**
52
+ * 追加一条记录。
53
+ *
54
+ * 失败只上报给日志,不抛给调用方:原图已经存进 Host 的对象库了,索引写不进去
55
+ * 只意味着"这张图暂时取不到原图",不该让整次发送失败。
56
+ * @returns {boolean} 是否写入成功(测试与诊断使用)。
57
+ */
58
+ export function appendIndex(entry, { home = dshHome(), logger = console } = {}) {
59
+ const path = indexPath(home)
60
+ try {
61
+ mkdirSync(dirname(path), { recursive: true, mode: 0o700 })
62
+ const line = `${JSON.stringify(entry)}\n`
63
+ const fd = openSync(path, 'a', 0o600)
64
+ try {
65
+ writeSync(fd, line)
66
+ } finally {
67
+ closeSync(fd)
68
+ }
69
+ return true
70
+ } catch (error) {
71
+ logger.warn?.(`mobile-link: image-originals append failed: ${error?.message ?? error}`)
72
+ return false
73
+ }
74
+ }
75
+
76
+ /**
77
+ * 把整个索引读成 `previewId → 原图元数据`。
78
+ *
79
+ * 坏行(半行、JSON 不完整、字段类型不对)逐行跳过;同键后写覆盖先写。
80
+ * 文件不存在返回空表——"没有原图"是正常状态,不是错误。
81
+ */
82
+ export function readIndex({ home = dshHome() } = {}) {
83
+ const path = indexPath(home)
84
+ let text
85
+ try {
86
+ if (statSync(path).size > MAX_INDEX_BYTES) return new Map()
87
+ text = readFileSync(path, 'utf8')
88
+ } catch {
89
+ return new Map()
90
+ }
91
+ const map = new Map()
92
+ for (const line of text.split('\n')) {
93
+ if (!line.trim()) continue
94
+ let parsed
95
+ try {
96
+ parsed = JSON.parse(line)
97
+ } catch {
98
+ continue
99
+ }
100
+ if (parsed?.v !== INDEX_VERSION) continue
101
+ const previewId = parsed.previewId
102
+ const original = parsed.original
103
+ if (typeof previewId !== 'string' || previewId.length === 0) continue
104
+ if (typeof original?.attachmentId !== 'string' || !original.attachmentId) continue
105
+ if (typeof original.name !== 'string' || !original.name) continue
106
+ if (!Number.isSafeInteger(original.bytes) || original.bytes < 0) continue
107
+ map.set(previewId, {
108
+ attachmentId: original.attachmentId,
109
+ name: original.name,
110
+ bytes: original.bytes,
111
+ ...(typeof original.mediaType === 'string' ? { mediaType: original.mediaType } : {}),
112
+ })
113
+ }
114
+ return map
115
+ }
116
+
117
+ /** 查一条原图元数据;没有就是 `undefined`(旧数据只有一份附件时正是这种)。 */
118
+ export function lookupOriginal(previewId, options = {}) {
119
+ if (typeof previewId !== 'string' || !previewId) return undefined
120
+ return readIndex(options).get(previewId)
121
+ }