dsh-think-translate 1.2.11 → 1.2.13
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.
- package/README.de.md +6 -1
- package/README.es.md +6 -1
- package/README.fr.md +6 -1
- package/README.ja.md +6 -1
- package/README.ko.md +6 -1
- package/README.md +127 -122
- package/README.ru.md +6 -1
- package/README.zh-CN.md +125 -120
- package/lib/client.js +2914 -2655
- package/lib/index.js +6 -9
- package/lib/text.js +77 -0
- package/package.json +2 -1
package/lib/index.js
CHANGED
|
@@ -13,6 +13,7 @@ import { createLocalPreparation, ollamaOrigin, localFirst, validOllamaSignature
|
|
|
13
13
|
import { fileURLToPath } from 'node:url'
|
|
14
14
|
import { requestTextInfo, isLoopback, DEFAULT_HEADERS } from './transport.js'
|
|
15
15
|
import { scanDshProviders, extractDshProviders, extractCredRefs, extractHarnessDefaultRoute } from './discovery.js'
|
|
16
|
+
import { protectedTextParts, translateProtectedText } from './text.js'
|
|
16
17
|
|
|
17
18
|
export const name = 'dsh-think-translate'
|
|
18
19
|
|
|
@@ -970,9 +971,7 @@ function isCodeOnlyLine(line) {
|
|
|
970
971
|
return /^(?:[$>#]\s*)?(?:(?:npm|pnpm|yarn|bun|npx|node|git|cd|curl|pwsh|reg)\b|(?:const|let|var|import|export|function|class|return|if|for|while|async|await)\b|[\[{(]|['"]?[\w.-]+['"]?\s*(?:[:=]|=>)|[\w$.]+\s*\(|(?:[~./][\w@~./-]*|[\w.-]+\/[\w@~./-]+)(?::\d+)?$)/.test(line)
|
|
971
972
|
}
|
|
972
973
|
function hasProse(text) {
|
|
973
|
-
const outside = text
|
|
974
|
-
.replace(/```[\s\S]*?```/g, ' ')
|
|
975
|
-
.replace(/`[^`\n]*`/g, ' ')
|
|
974
|
+
const outside = protectedTextParts(text).filter(part => !part.code).map(part => part.text).join(' ')
|
|
976
975
|
.replace(/https?:\/\/\S+/g, ' ')
|
|
977
976
|
const lines = outside.split('\n').map(function (l) { return l.trim() }).filter(Boolean)
|
|
978
977
|
if (lines.length === 0) return false
|
|
@@ -982,7 +981,7 @@ function hasProse(text) {
|
|
|
982
981
|
}
|
|
983
982
|
// Decide from prose, not paths, identifiers, or structured payloads (#10).
|
|
984
983
|
function proseForDetection(text) {
|
|
985
|
-
let prose = text.
|
|
984
|
+
let prose = protectedTextParts(text).filter(part => !part.code).map(part => part.text).join(' ')
|
|
986
985
|
.replace(/https?:\/\/[^\s<>]+/g, ' ')
|
|
987
986
|
.replace(/\b[A-Za-z]:[\\/][^\s"'`,;,。;!?、)}\]]*/g, ' ')
|
|
988
987
|
.replace(/(^|\s)\/(?:[^\s"'`,;,。;!?、)}\]]+)/g, '$1 ')
|
|
@@ -1057,15 +1056,13 @@ async function runChainFor(ids, text, target, cfg) {
|
|
|
1057
1056
|
if (!adapter) continue
|
|
1058
1057
|
try {
|
|
1059
1058
|
if (ollamaOrigin(id, p)) await localPreparation.ensure(p)
|
|
1060
|
-
const
|
|
1061
|
-
const outs = []
|
|
1062
|
-
for (const c of chunks) outs.push(await adapter(c, target, p))
|
|
1059
|
+
const translated = await translateProtectedText(text, chunk => adapter(chunk, target, p), chunkText)
|
|
1063
1060
|
state.ok++
|
|
1064
1061
|
state.lastProvider = id
|
|
1065
1062
|
state.lastError = null
|
|
1066
1063
|
// Carry the concrete model name so the UI can show e.g. "openai: qwen2.5:14b".
|
|
1067
1064
|
attempts.push({ provider: id, signature: providerFingerprint(p), ok: true, model: p.model || null })
|
|
1068
|
-
return { ok: true, text:
|
|
1065
|
+
return { ok: true, text: translated, provider: id, providerSignature: providerFingerprint(p), attempts, ...(p.model ? { model: p.model } : {}) }
|
|
1069
1066
|
} catch (e) {
|
|
1070
1067
|
lastErr = e
|
|
1071
1068
|
attempts.push({ provider: id, signature: providerFingerprint(p), ok: false, model: p.model || null })
|
|
@@ -1117,7 +1114,7 @@ async function handleTranslate(body) {
|
|
|
1117
1114
|
if (!adapter) return { ok: false, text, error: 'unsupported type: ' + p.type }
|
|
1118
1115
|
try {
|
|
1119
1116
|
if (ollamaOrigin(forced, p)) await localPreparation.ensure(p)
|
|
1120
|
-
const out = await
|
|
1117
|
+
const out = await translateProtectedText(text, chunk => adapter(chunk, target, p), chunkText)
|
|
1121
1118
|
return { ok: true, text: out, provider: forced, providerSignature: providerFingerprint(p), ...(p.model ? { model: p.model } : {}) }
|
|
1122
1119
|
} catch (e) {
|
|
1123
1120
|
return { ok: false, text, error: e instanceof Error ? e.message : String(e) }
|
package/lib/text.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Code spans keep their exact source offsets so neither translation nor batching
|
|
2
|
+
// needs placeholders that a provider could translate, remove, or reorder.
|
|
3
|
+
export function protectedTextParts(text) {
|
|
4
|
+
const parts = []
|
|
5
|
+
let proseStart = 0
|
|
6
|
+
let cursor = 0
|
|
7
|
+
function protect(start, end, complete) {
|
|
8
|
+
if (start > proseStart) parts.push({ text: text.slice(proseStart, start), start: proseStart, end: start, code: false, complete: true })
|
|
9
|
+
parts.push({ text: text.slice(start, end), start, end, code: true, complete })
|
|
10
|
+
proseStart = end
|
|
11
|
+
cursor = end
|
|
12
|
+
}
|
|
13
|
+
function lineEnd(start) {
|
|
14
|
+
const newline = text.indexOf('\n', start)
|
|
15
|
+
return newline < 0 ? text.length : newline + 1
|
|
16
|
+
}
|
|
17
|
+
while (cursor < text.length) {
|
|
18
|
+
if (cursor === 0 || text[cursor - 1] === '\n') {
|
|
19
|
+
const end = lineEnd(cursor)
|
|
20
|
+
const line = text.slice(cursor, end)
|
|
21
|
+
// Treat indented/quoted fences conservatively as code as well.
|
|
22
|
+
const fence = line.match(/^[\t ]*(?:>[\t ]*)*(?:(?:[-+*]|[0-9]+[.)])[\t ]+)?(`{3,}|~{3,})[^\r\n]*(?:\r?\n)?$/)
|
|
23
|
+
if (fence) {
|
|
24
|
+
const close = new RegExp('^[\\t ]*(?:>[\\t ]*)*' + fence[1][0] + '{' + fence[1].length + ',}[\\t ]*(?:\\r?\\n)?$')
|
|
25
|
+
let stop = end
|
|
26
|
+
let complete = false
|
|
27
|
+
while (stop < text.length) {
|
|
28
|
+
const next = lineEnd(stop)
|
|
29
|
+
if (close.test(text.slice(stop, next))) { stop = next; complete = true; break }
|
|
30
|
+
stop = next
|
|
31
|
+
}
|
|
32
|
+
protect(cursor, stop, complete)
|
|
33
|
+
continue
|
|
34
|
+
}
|
|
35
|
+
if (/^(?: {4}|\t)/.test(line)) {
|
|
36
|
+
protect(cursor, end, end < text.length || line.endsWith('\n'))
|
|
37
|
+
continue
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (text[cursor] === '`') {
|
|
41
|
+
let slashes = 0
|
|
42
|
+
for (let i = cursor - 1; i >= 0 && text[i] === '\\'; i--) slashes++
|
|
43
|
+
if (slashes % 2) { cursor++; continue }
|
|
44
|
+
let after = cursor + 1
|
|
45
|
+
while (text[after] === '`') after++
|
|
46
|
+
const size = after - cursor
|
|
47
|
+
let next = after
|
|
48
|
+
let stop = text.length
|
|
49
|
+
let complete = false
|
|
50
|
+
while ((next = text.indexOf('`', next)) >= 0) {
|
|
51
|
+
let end = next + 1
|
|
52
|
+
while (text[end] === '`') end++
|
|
53
|
+
if (end - next === size) { stop = end; complete = true; break }
|
|
54
|
+
next = end
|
|
55
|
+
}
|
|
56
|
+
// An unfinished streaming code span must not expose its body as prose.
|
|
57
|
+
protect(cursor, stop, complete)
|
|
58
|
+
continue
|
|
59
|
+
}
|
|
60
|
+
cursor++
|
|
61
|
+
}
|
|
62
|
+
if (proseStart < text.length) parts.push({ text: text.slice(proseStart), start: proseStart, end: text.length, code: false, complete: true })
|
|
63
|
+
return parts
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Only prose reaches a provider; separators adjoining code stay byte-for-byte.
|
|
67
|
+
export async function translateProtectedText(text, translate, split = value => [value]) {
|
|
68
|
+
const output = []
|
|
69
|
+
for (const part of protectedTextParts(text)) {
|
|
70
|
+
const match = !part.code && part.text.match(/^(\s*)([\s\S]*?)(\s*)$/)
|
|
71
|
+
if (!match || !/\p{L}/u.test(match[2])) { output.push(part.text); continue }
|
|
72
|
+
const chunks = []
|
|
73
|
+
for (const chunk of split(match[2])) chunks.push((await translate(chunk)).trim())
|
|
74
|
+
output.push(match[1] + chunks.join('\n\n') + match[3])
|
|
75
|
+
}
|
|
76
|
+
return output.join('')
|
|
77
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-think-translate",
|
|
3
3
|
"description": "Display-layer translation for the DSH web app: thinking chain, task cards, and optional answer text, in 8 target languages (local Ollama / any OpenAI-compatible or Anthropic endpoint / Google / Bing, fail-open)",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.13",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"lib/ollama.js",
|
|
16
16
|
"lib/transport.js",
|
|
17
17
|
"lib/discovery.js",
|
|
18
|
+
"lib/text.js",
|
|
18
19
|
"cordis.patch.yml",
|
|
19
20
|
"demo/demo.gif",
|
|
20
21
|
"demo/demo2.gif"
|