@xi4z/dsh-utils-approval-qq 0.0.0-stage → 0.9.0

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,106 @@
1
+ /**
2
+ * One-shot English-to-Chinese translation for approval reasons.
3
+ *
4
+ * Auto-review reasons (and some hook reasons) arrive in English. The QQ card is
5
+ * Chinese, so an English reason is translated through the host's own model
6
+ * routing (\`ctx.llm\`) before the card is rendered. This is deliberately a plain
7
+ * one-shot completion: no tools, no session, no approval of its own — so it can
8
+ * never recurse back into the approval waterfall it is serving.
9
+ *
10
+ * Failure is always soft: an unavailable model, a timeout, or an answer without
11
+ * any Chinese falls back to the original text.
12
+ *
13
+ * dsh-llm is imported dynamically: the host provides it, but translation is an
14
+ * enhancement and must never prevent the component (or the approval card) from
15
+ * loading when that package is not resolvable.
16
+ *
17
+ * @module dsh-utils/approval-qq/translate
18
+ */
19
+ import type { FinishReason, GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm'
20
+
21
+ /** Minimal \`ctx.llm\` slice used here. */
22
+ export interface LlmRuntime {
23
+ stream(options: GenerateOptions): AsyncIterable<StreamChunk>
24
+ }
25
+
26
+ /** Resolved provider/model route for the translation call. */
27
+ export interface ModelRoute {
28
+ provider: string
29
+ model: string
30
+ }
31
+
32
+ /** Translation instructions: translate prose, never touch identifiers or code. */
33
+ const SYSTEM_PROMPT = [
34
+ 'You translate approval reasons into Simplified Chinese.',
35
+ 'Keep tool names, commands, paths, identifiers and code verbatim.',
36
+ 'Do not add explanation, quotes, or markdown fences.',
37
+ 'Output only the translation.',
38
+ ].join(' ')
39
+
40
+ let hostModule: Promise<typeof import('@deepseek-ai/dsh-llm') | undefined> | undefined
41
+
42
+ async function loadHostModule(): Promise<typeof import('@deepseek-ai/dsh-llm') | undefined> {
43
+ hostModule ??= import('@deepseek-ai/dsh-llm').catch(() => undefined)
44
+ return hostModule
45
+ }
46
+
47
+ /**
48
+ * Whether a reason should be translated: it has real prose but no Chinese at
49
+ * all. Text that already contains CJK is left untouched (mixed text is assumed
50
+ * to be mostly Chinese), and very short or identifier-only strings are skipped.
51
+ */
52
+ export function needsTranslation(text: string | undefined): boolean {
53
+ if (typeof text !== 'string') return false
54
+ const trimmed = text.trim()
55
+ if (trimmed.length < 8) return false
56
+ if (/[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]/.test(trimmed)) return false
57
+ const letters = (trimmed.match(/[A-Za-z]/g) ?? []).length
58
+ return letters >= 8
59
+ }
60
+
61
+ /** Best-effort translation; returns undefined so callers can fall back. */
62
+ export async function translateToChinese(
63
+ llm: LlmRuntime,
64
+ route: ModelRoute,
65
+ text: string,
66
+ options: { timeoutMs: number; signal?: AbortSignal; maxChars?: number },
67
+ ): Promise<string | undefined> {
68
+ const source = text.trim().slice(0, options.maxChars ?? 1200)
69
+ if (source === '') return undefined
70
+
71
+ const host = await loadHostModule()
72
+ if (host === undefined) return undefined
73
+
74
+ const message = host.createUserMessage({
75
+ content: [{ type: 'text', text: source }],
76
+ source: { kind: 'user' },
77
+ })
78
+ const signals: AbortSignal[] = [AbortSignal.timeout(Math.max(200, options.timeoutMs))]
79
+ if (options.signal) signals.push(options.signal)
80
+
81
+ const assembler = new host.BlockAssembler()
82
+ for await (const chunk of llm.stream({
83
+ provider: route.provider,
84
+ model: route.model,
85
+ messages: [message],
86
+ system: SYSTEM_PROMPT,
87
+ temperature: 0,
88
+ maxTokens: 500,
89
+ signal: AbortSignal.any(signals),
90
+ })) {
91
+ assembler.push(chunk)
92
+ }
93
+
94
+ const finish: FinishReason = assembler.finish
95
+ if (finish.kind === 'error' || finish.kind === 'aborted') return undefined
96
+
97
+ const translated = assembler.blocks()
98
+ .filter((block): block is Extract<typeof block, { type: 'text' }> => block.type === 'text')
99
+ .map((block) => block.text)
100
+ .join(' ')
101
+ .trim()
102
+ if (translated === '') return undefined
103
+ // A translation that produced no Chinese is not a translation; keep the source.
104
+ if (!/[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]/.test(translated)) return undefined
105
+ return translated
106
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.