opencode-translate 1.0.3 → 1.0.4
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.md +1 -3
- package/package.json +1 -1
- package/src/activation/index.ts +1 -6
- package/src/activation/text-complete.ts +0 -2
- package/src/activation/types.ts +0 -1
- package/src/constants/options.ts +1 -14
- package/src/constants/types.ts +0 -17
- package/src/activation/event.ts +0 -132
package/README.md
CHANGED
|
@@ -36,8 +36,7 @@ Add to `~/.config/opencode/opencode.jsonc`:
|
|
|
36
36
|
["opencode-translate", {
|
|
37
37
|
"model": "openai/gpt-5.4-mini", // model to use for translation
|
|
38
38
|
"variant": "minimal", // optional model variant / thinking effort
|
|
39
|
-
"lang": "Korean"
|
|
40
|
-
"assistantTranslation": "final-message" // or "each-part"
|
|
39
|
+
"lang": "Korean" // language you speak
|
|
41
40
|
}]
|
|
42
41
|
]
|
|
43
42
|
}
|
|
@@ -61,5 +60,4 @@ All subsequent messages in the same session are translated automatically — no
|
|
|
61
60
|
| `variant` | string | optional | Translator model variant / thinking effort (for example, `"minimal"`, `"high"`, or `"max"`) |
|
|
62
61
|
| `lang` | string | required | Language you speak (e.g. `"Korean"`, `"Japanese"`) |
|
|
63
62
|
| `trigger` | string[] | `["$en"]` | Keywords that activate translation |
|
|
64
|
-
| `assistantTranslation` | `"each-part" \| "final-message"` | `"final-message"` | Translate only the final assistant text part after the loop becomes idle, or opt into translating each text part as it completes |
|
|
65
63
|
| `verbose` | boolean | `false` | Print translation logs |
|
package/package.json
CHANGED
package/src/activation/index.ts
CHANGED
|
@@ -2,7 +2,6 @@ import type { Hooks, PluginInput, PluginOptions } from "@opencode-ai/plugin"
|
|
|
2
2
|
import { type PluginClientLike, resolveOptions } from "../constants"
|
|
3
3
|
import { createTranslator } from "../translator"
|
|
4
4
|
import { createChatMessageHook } from "./chat-message"
|
|
5
|
-
import { createEventHook } from "./event"
|
|
6
5
|
import { createMessagesTransformHook } from "./messages-transform"
|
|
7
6
|
import { createToolExecuteAfterHook, createToolExecuteBeforeHook, resetQuestionSnapshots } from "./question-hooks"
|
|
8
7
|
import { resetSessionStateCache } from "./state"
|
|
@@ -25,19 +24,15 @@ export function createHooks(ctx: PluginInput, rawOptions: PluginOptions = {}, de
|
|
|
25
24
|
const hookContext: HookContext = {
|
|
26
25
|
client,
|
|
27
26
|
directory: ctx.directory,
|
|
28
|
-
serverUrl: ctx.serverUrl,
|
|
29
27
|
options,
|
|
30
28
|
translator: deps.translator ?? createTranslator(client, options),
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
|
|
31
|
+
return {
|
|
34
32
|
"chat.message": createChatMessageHook(hookContext),
|
|
35
33
|
"experimental.chat.messages.transform": createMessagesTransformHook(hookContext),
|
|
36
34
|
"experimental.text.complete": createTextCompleteHook(hookContext),
|
|
37
35
|
"tool.execute.before": createToolExecuteBeforeHook(hookContext),
|
|
38
36
|
"tool.execute.after": createToolExecuteAfterHook(hookContext),
|
|
39
37
|
}
|
|
40
|
-
|
|
41
|
-
if (options.assistantTranslation === "final-message") hooks.event = createEventHook(hookContext)
|
|
42
|
-
return hooks
|
|
43
38
|
}
|
|
@@ -11,8 +11,6 @@ type TextCompleteHook = NonNullable<Hooks["experimental.text.complete"]>
|
|
|
11
11
|
export function createTextCompleteHook(ctx: HookContext): TextCompleteHook {
|
|
12
12
|
return async (input, output) => {
|
|
13
13
|
try {
|
|
14
|
-
if (ctx.options.assistantTranslation === "final-message") return
|
|
15
|
-
|
|
16
14
|
const resolved = await resolveSessionState(ctx.client, ctx.directory, input.sessionID)
|
|
17
15
|
const activeState = resolved.state
|
|
18
16
|
if (!activeState) return
|
package/src/activation/types.ts
CHANGED
package/src/constants/options.ts
CHANGED
|
@@ -1,17 +1,5 @@
|
|
|
1
1
|
import { AUTH_ENV_FALLBACK, DEFAULT_TRIGGER, PLUGIN_NAME } from "./plugin"
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
const ASSISTANT_TRANSLATION_MODES = new Set<AssistantTranslationMode>(["each-part", "final-message"])
|
|
5
|
-
|
|
6
|
-
function resolveAssistantTranslationMode(value: unknown): AssistantTranslationMode {
|
|
7
|
-
if (value === undefined) return "final-message"
|
|
8
|
-
if (typeof value === "string" && ASSISTANT_TRANSLATION_MODES.has(value as AssistantTranslationMode)) {
|
|
9
|
-
return value as AssistantTranslationMode
|
|
10
|
-
}
|
|
11
|
-
throw new Error(
|
|
12
|
-
`[${PLUGIN_NAME}:INVALID_OPTIONS] options.assistantTranslation must be "each-part" or "final-message".`,
|
|
13
|
-
)
|
|
14
|
-
}
|
|
2
|
+
import type { ProviderInfo, ResolvedTranslateOptions } from "./types"
|
|
15
3
|
|
|
16
4
|
export function resolveOptions(options: Record<string, unknown>): ResolvedTranslateOptions {
|
|
17
5
|
const model = typeof options.model === "string" ? options.model.trim() : ""
|
|
@@ -48,7 +36,6 @@ export function resolveOptions(options: Record<string, unknown>): ResolvedTransl
|
|
|
48
36
|
trigger: trigger.length > 0 ? trigger : [...DEFAULT_TRIGGER],
|
|
49
37
|
lang,
|
|
50
38
|
verbose: options.verbose === true,
|
|
51
|
-
assistantTranslation: resolveAssistantTranslationMode(options.assistantTranslation),
|
|
52
39
|
}
|
|
53
40
|
}
|
|
54
41
|
|
package/src/constants/types.ts
CHANGED
|
@@ -10,11 +10,8 @@ export interface ResolvedTranslateOptions {
|
|
|
10
10
|
trigger: string[]
|
|
11
11
|
lang: string
|
|
12
12
|
verbose: boolean
|
|
13
|
-
assistantTranslation: AssistantTranslationMode
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
export type AssistantTranslationMode = "each-part" | "final-message"
|
|
17
|
-
|
|
18
15
|
export interface TranslateState {
|
|
19
16
|
translate_enabled: true
|
|
20
17
|
translate_user_lang: string
|
|
@@ -44,7 +41,6 @@ interface MessageLike {
|
|
|
44
41
|
id: string
|
|
45
42
|
sessionID: string
|
|
46
43
|
role: string
|
|
47
|
-
summary?: boolean
|
|
48
44
|
}
|
|
49
45
|
|
|
50
46
|
export interface TextPartLike {
|
|
@@ -160,17 +156,4 @@ export interface PluginClientLike {
|
|
|
160
156
|
}
|
|
161
157
|
}): Promise<unknown>
|
|
162
158
|
}
|
|
163
|
-
part?: {
|
|
164
|
-
update(
|
|
165
|
-
input: {
|
|
166
|
-
sessionID: string
|
|
167
|
-
messageID: string
|
|
168
|
-
partID: string
|
|
169
|
-
directory?: string
|
|
170
|
-
workspace?: string
|
|
171
|
-
part?: TextPartLike
|
|
172
|
-
},
|
|
173
|
-
options?: { throwOnError?: boolean },
|
|
174
|
-
): Promise<TextPartLike | SDKResponseLike<TextPartLike>>
|
|
175
|
-
}
|
|
176
159
|
}
|
package/src/activation/event.ts
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import type { Hooks } from "@opencode-ai/plugin"
|
|
2
|
-
import { isTextPart, LLM_LANGUAGE, type MessageWithPartsLike, type TextPartLike, unwrapData } from "../constants"
|
|
3
|
-
import { composeTranslatedAssistantText, composeTranslationFailureText, extractEnglishHistoryText } from "../formatting"
|
|
4
|
-
import { getDisplayLanguageLabel } from "../labels"
|
|
5
|
-
import { logError } from "./logging"
|
|
6
|
-
import { resolveSessionState } from "./state"
|
|
7
|
-
import type { HookContext } from "./types"
|
|
8
|
-
|
|
9
|
-
type EventHook = NonNullable<Hooks["event"]>
|
|
10
|
-
|
|
11
|
-
const inFlightFinalTranslations = new Set<string>()
|
|
12
|
-
|
|
13
|
-
function latestAssistantMessage(messages: MessageWithPartsLike[]): MessageWithPartsLike | undefined {
|
|
14
|
-
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
15
|
-
const message = messages[index]
|
|
16
|
-
if (message.info.role === "assistant" && message.info.summary !== true) return message
|
|
17
|
-
}
|
|
18
|
-
return undefined
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function lastNonEmptyTextPart(message: MessageWithPartsLike): (TextPartLike & { text: string }) | undefined {
|
|
22
|
-
for (let index = message.parts.length - 1; index >= 0; index -= 1) {
|
|
23
|
-
const part = message.parts[index]
|
|
24
|
-
if (isTextPart(part) && part.text.trim().length > 0) return part
|
|
25
|
-
}
|
|
26
|
-
return undefined
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async function composeFinalAssistantText(
|
|
30
|
-
ctx: HookContext,
|
|
31
|
-
text: string,
|
|
32
|
-
targetLanguage: string,
|
|
33
|
-
label: string,
|
|
34
|
-
): Promise<string> {
|
|
35
|
-
try {
|
|
36
|
-
const translated = await ctx.translator.translateText({
|
|
37
|
-
text,
|
|
38
|
-
sourceLanguage: LLM_LANGUAGE,
|
|
39
|
-
targetLanguage,
|
|
40
|
-
direction: "outbound",
|
|
41
|
-
})
|
|
42
|
-
return composeTranslatedAssistantText(text, label, translated)
|
|
43
|
-
} catch (error) {
|
|
44
|
-
await logError(ctx.client, error)
|
|
45
|
-
return composeTranslationFailureText(text)
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async function patchPartViaServer(ctx: HookContext, sessionID: string, part: TextPartLike) {
|
|
50
|
-
if (!ctx.serverUrl) throw new Error("client.part.update is required for final-message assistant translation")
|
|
51
|
-
|
|
52
|
-
const url = new URL(
|
|
53
|
-
`/session/${encodeURIComponent(sessionID)}/message/${encodeURIComponent(part.messageID)}/part/${encodeURIComponent(part.id)}`,
|
|
54
|
-
ctx.serverUrl,
|
|
55
|
-
)
|
|
56
|
-
if (ctx.directory) url.searchParams.set("directory", ctx.directory)
|
|
57
|
-
|
|
58
|
-
const response = await fetch(url, {
|
|
59
|
-
method: "PATCH",
|
|
60
|
-
headers: { "content-type": "application/json" },
|
|
61
|
-
body: JSON.stringify(part),
|
|
62
|
-
})
|
|
63
|
-
if (!response.ok) {
|
|
64
|
-
throw new Error(`Failed to update final assistant translation part: HTTP ${response.status}`)
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async function updatePart(ctx: HookContext, sessionID: string, part: TextPartLike) {
|
|
69
|
-
const partUpdater = ctx.client.part?.update
|
|
70
|
-
if (partUpdater) {
|
|
71
|
-
const input = {
|
|
72
|
-
sessionID,
|
|
73
|
-
messageID: part.messageID,
|
|
74
|
-
partID: part.id,
|
|
75
|
-
...(ctx.directory ? { directory: ctx.directory } : {}),
|
|
76
|
-
part,
|
|
77
|
-
}
|
|
78
|
-
await partUpdater.call(ctx.client.part, input, { throwOnError: true })
|
|
79
|
-
return
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
await patchPartViaServer(ctx, sessionID, part)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async function translateFinalAssistantMessage(ctx: HookContext, sessionID: string) {
|
|
86
|
-
if (inFlightFinalTranslations.has(sessionID)) return
|
|
87
|
-
inFlightFinalTranslations.add(sessionID)
|
|
88
|
-
try {
|
|
89
|
-
const resolved = await resolveSessionState(ctx.client, ctx.directory, sessionID)
|
|
90
|
-
const activeState = resolved.state
|
|
91
|
-
if (!activeState) return
|
|
92
|
-
if (activeState.translate_user_lang === LLM_LANGUAGE) return
|
|
93
|
-
|
|
94
|
-
const messages = unwrapData(
|
|
95
|
-
await ctx.client.session.messages({
|
|
96
|
-
path: { id: sessionID },
|
|
97
|
-
query: { ...(ctx.directory ? { directory: ctx.directory } : {}), limit: 20 },
|
|
98
|
-
throwOnError: true,
|
|
99
|
-
}),
|
|
100
|
-
)
|
|
101
|
-
const message = latestAssistantMessage(messages)
|
|
102
|
-
if (!message) return
|
|
103
|
-
|
|
104
|
-
const part = lastNonEmptyTextPart(message)
|
|
105
|
-
if (!part) return
|
|
106
|
-
|
|
107
|
-
const label = getDisplayLanguageLabel(activeState.translate_user_lang)
|
|
108
|
-
if (extractEnglishHistoryText(part.text, { nonce: activeState.translate_nonce, label }) !== part.text) return
|
|
109
|
-
|
|
110
|
-
const updatedPart = {
|
|
111
|
-
...part,
|
|
112
|
-
text: await composeFinalAssistantText(ctx, part.text, activeState.translate_user_lang, label),
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
await updatePart(ctx, sessionID, updatedPart)
|
|
116
|
-
} finally {
|
|
117
|
-
inFlightFinalTranslations.delete(sessionID)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function createEventHook(ctx: HookContext): EventHook {
|
|
122
|
-
return async (input) => {
|
|
123
|
-
if (ctx.options.assistantTranslation !== "final-message") return
|
|
124
|
-
if (input.event.type !== "session.idle") return
|
|
125
|
-
|
|
126
|
-
try {
|
|
127
|
-
await translateFinalAssistantMessage(ctx, input.event.properties.sessionID)
|
|
128
|
-
} catch (error) {
|
|
129
|
-
await logError(ctx.client, error)
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|