@deepseek-ai/dsh-experimental-client-ui-cot-translation 0.2.2-alpha.3 → 0.2.2-rc.2
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/lib/client.js +1 -1272
- package/lib/client.js.map +1 -0
- package/lib/typert.remote-client.d.ts.map +1 -0
- package/lib/types/client/TranslationBody.d.ts.map +1 -0
- package/lib/types/client/TranslationBody.js.map +1 -0
- package/lib/types/client/TranslationSettings.d.ts.map +1 -0
- package/lib/types/client/TranslationSettings.js.map +1 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/index.js.map +1 -0
- package/lib/types/client/locales.d.ts.map +1 -0
- package/lib/types/client/locales.js.map +1 -0
- package/lib/types/client/mount.d.ts.map +1 -0
- package/lib/types/client/mount.js.map +1 -0
- package/lib/types/client/preferences-form.d.ts.map +1 -0
- package/lib/types/client/preferences-form.js.map +1 -0
- package/lib/types/client/translation.d.ts.map +1 -0
- package/lib/types/client/translation.js.map +1 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/preferences.d.ts.map +1 -0
- package/lib/types/preferences.js.map +1 -0
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js.map +1 -0
- package/package.json +31 -27
- package/src/client/Translation.module.css +20 -0
- package/src/client/TranslationBody.tsx +61 -0
- package/src/client/TranslationSettings.tsx +67 -0
- package/src/client/index.ts +21 -0
- package/src/client/locales.ts +179 -0
- package/src/client/mount.ts +129 -0
- package/src/client/preferences-form.ts +48 -0
- package/src/client/translation.ts +114 -0
- package/src/css-modules.d.ts +5 -0
- package/src/index.ts +102 -0
- package/src/preferences.ts +12 -0
- package/src/types.ts +28 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/** Host access to machine translation of displayed reasoning. */
|
|
2
|
+
import { addAbortListener } from 'node:events'
|
|
3
|
+
import { Context, type Volatile } from '@deepseek-ai/cordis'
|
|
4
|
+
import z from '@deepseek-ai/schemastery'
|
|
5
|
+
import { Remote, RemoteError, TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol'
|
|
6
|
+
import type {} from '@deepseek-ai/dsh-settings'
|
|
7
|
+
import type {} from '@deepseek-ai/dsh-api-session-controller'
|
|
8
|
+
import { TranslationError } from '@deepseek-ai/dsh-experimental-translator'
|
|
9
|
+
import type { TranslationRequest, TranslationSpec } from '@deepseek-ai/dsh-experimental-translator/types'
|
|
10
|
+
import { LANGUAGE_PREFERENCE_PATTERN, PROVIDER_PREFERENCES } from './preferences.ts'
|
|
11
|
+
import type { CotTranslationPreferences, CotTranslationSnapshot } from './types.ts'
|
|
12
|
+
|
|
13
|
+
/** Live preferences presented on the bundle's Plugins page. */
|
|
14
|
+
export interface Config {
|
|
15
|
+
/** Explicit translation provider, defaulting to Bing. */
|
|
16
|
+
provider: Volatile<CotTranslationPreferences['provider']>
|
|
17
|
+
/** Target language code; auto follows the browser UI locale. */
|
|
18
|
+
targetLanguage: Volatile<string>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare module '@deepseek-ai/cordis' {
|
|
22
|
+
interface Context {
|
|
23
|
+
/** Remote consumer of the experimental translation service. */
|
|
24
|
+
cotTranslation: CotTranslationController
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Optional reasoning translation delegates Session-bound storage to the translator. */
|
|
29
|
+
export default class CotTranslationController extends TypertRemoteService {
|
|
30
|
+
static inject = ['translator', 'typert', 'sessionController']
|
|
31
|
+
static Config = z.object({
|
|
32
|
+
provider: z.union(PROVIDER_PREFERENCES).default('bing').volatile(),
|
|
33
|
+
targetLanguage: z.string().pattern(LANGUAGE_PREFERENCE_PATTERN).default('auto').volatile(),
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
constructor(ctx: Context, private readonly config: Config) {
|
|
37
|
+
super(ctx, 'cotTranslation', { namespace: 'cotTranslation' })
|
|
38
|
+
ctx.inject(['settings'], (child) => { child.effect(() => child.settings.configure({ auto: false }, ctx.fiber)) })
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Read accepted translation preferences and the current request limit without sending text.
|
|
43
|
+
* @param signal - browser query cancellation or Remote contribution withdrawal.
|
|
44
|
+
* @returns authoritative preferences, eligible routes, and maximum UTF-16 text length per request.
|
|
45
|
+
*/
|
|
46
|
+
@Remote
|
|
47
|
+
async limits(signal: AbortSignal): Promise<CotTranslationSnapshot> {
|
|
48
|
+
signal.throwIfAborted()
|
|
49
|
+
const availableProviders = await this.ctx.translator.availableProviders(signal)
|
|
50
|
+
signal.throwIfAborted()
|
|
51
|
+
return { maxTextChars: this.ctx.translator.maxTextChars,
|
|
52
|
+
preferences: { provider: this.config.provider.get(), targetLanguage: this.config.targetLanguage.get() }, availableProviders }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Translate one displayed fragment through the reader's selected provider.
|
|
57
|
+
* @param request - original text and Session identity supplied by the Client; provider and explicit
|
|
58
|
+
* target language match accepted preferences, while auto uses the browser locale.
|
|
59
|
+
* @param signal - browser cancellation or Remote contribution withdrawal.
|
|
60
|
+
* @returns translated text; an uncached inactive Session joins ordinary GUI activation before retry.
|
|
61
|
+
* Failures omit the source and provider response. Cancellation stops this caller's wait, not shared activation.
|
|
62
|
+
*/
|
|
63
|
+
@Remote
|
|
64
|
+
async translate(request: TranslationRequest, signal: AbortSignal): Promise<string> {
|
|
65
|
+
signal.throwIfAborted()
|
|
66
|
+
try {
|
|
67
|
+
const spec = this.resolveSelected(request)
|
|
68
|
+
try {
|
|
69
|
+
return await this.ctx.translator.translate(spec, signal)
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (!(error instanceof TranslationError) || error.code !== 'TRANSLATION_SESSION_INACTIVE' || spec.sessionId === undefined) throw error
|
|
72
|
+
}
|
|
73
|
+
signal.throwIfAborted()
|
|
74
|
+
const activation = this.ctx.sessionController.resolveAgent(spec.sessionId).then((result) => {
|
|
75
|
+
if ('error' in result) throw result.error
|
|
76
|
+
}).catch((error: unknown) => {
|
|
77
|
+
if (signal.aborted) this.ctx.logger.warn('Reasoning translation Session activation failed after cancellation')
|
|
78
|
+
throw error
|
|
79
|
+
})
|
|
80
|
+
const cancelled = Promise.withResolvers<never>()
|
|
81
|
+
using _listener = addAbortListener(signal, () => {
|
|
82
|
+
// oxlint-disable-next-line typescript/prefer-promise-reject-errors -- Preserve the caller's arbitrary cancellation reason.
|
|
83
|
+
cancelled.reject(signal.reason)
|
|
84
|
+
})
|
|
85
|
+
await Promise.race([activation, cancelled.promise])
|
|
86
|
+
signal.throwIfAborted()
|
|
87
|
+
return await this.ctx.translator.translate(this.resolveSelected(spec), signal)
|
|
88
|
+
} catch (_error) {
|
|
89
|
+
signal.throwIfAborted()
|
|
90
|
+
throw new RemoteError('cotTranslation/failed', 'Translation failed', {})
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private resolveSelected(request: TranslationRequest): TranslationSpec {
|
|
95
|
+
const accepted = this.config.provider.get()
|
|
96
|
+
const provider = request.provider ?? accepted
|
|
97
|
+
if (provider !== accepted) throw new Error('Translation provider is not selected')
|
|
98
|
+
const targetLanguage = this.config.targetLanguage.get()
|
|
99
|
+
if (targetLanguage !== 'auto' && request.targetLanguage !== targetLanguage) throw new Error('Translation language is not selected')
|
|
100
|
+
return this.ctx.translator.resolve({ ...request, provider })
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Shared validation for Host configuration and browser preference drafts. */
|
|
2
|
+
import type { TranslationProvider } from '@deepseek-ai/dsh-experimental-translator/types'
|
|
3
|
+
|
|
4
|
+
export type { CotTranslationPreferences, CotTranslationSnapshot } from './types.ts'
|
|
5
|
+
|
|
6
|
+
/** Language preference accepted by both Host configuration and browser drafts. */
|
|
7
|
+
export const LANGUAGE_PREFERENCE_PATTERN = /^(?:auto|[a-zA-Z]{2,8}(?:-[a-zA-Z0-9]{1,8})*)$/
|
|
8
|
+
|
|
9
|
+
/** Explicit provider choices accepted by Host configuration and browser drafts. */
|
|
10
|
+
export const PROVIDER_PREFERENCES = [
|
|
11
|
+
'bing', 'google', 'deepseek-account', 'deepseek-official',
|
|
12
|
+
] as const satisfies readonly TranslationProvider[]
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Browser-safe translation preferences and Remote metadata. */
|
|
2
|
+
import type {} from '@deepseek-ai/dsh-typert-protocol'
|
|
3
|
+
import type { TranslationProvider } from '@deepseek-ai/dsh-experimental-translator/types'
|
|
4
|
+
|
|
5
|
+
/** Translation choices; auto targets the active browser UI language. */
|
|
6
|
+
export interface CotTranslationPreferences {
|
|
7
|
+
/** Explicit provider selection; requests never fall back to another provider. */
|
|
8
|
+
provider: TranslationProvider
|
|
9
|
+
/** BCP 47 language code, or auto to follow the UI language. */
|
|
10
|
+
targetLanguage: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Authoritative Host metadata read before any reasoning is submitted. */
|
|
14
|
+
export interface CotTranslationSnapshot {
|
|
15
|
+
/** Maximum UTF-16 text length accepted by one translation request. */
|
|
16
|
+
maxTextChars: number
|
|
17
|
+
/** Current accepted provider and target-language preferences. */
|
|
18
|
+
preferences: CotTranslationPreferences
|
|
19
|
+
/** Anonymous endpoints and native paid routes currently eligible for translation. */
|
|
20
|
+
availableProviders: readonly TranslationProvider[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare module '@deepseek-ai/dsh-typert-protocol' {
|
|
24
|
+
interface RemoteErrorDetailsMap {
|
|
25
|
+
/** Provider or input failure, without original text or response details. */
|
|
26
|
+
'cotTranslation/failed': Record<string, never>
|
|
27
|
+
}
|
|
28
|
+
}
|