opencode-translate 1.0.5 → 1.0.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-translate",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "OpenCode plugin that lets the user chat in a configured language while the main chat loop only sees English.",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -33,6 +33,24 @@ interface PartProcessingResult {
33
33
  firstUserTextPart?: TextPartLike & { text: string }
34
34
  }
35
35
 
36
+ interface ExistingTranslation {
37
+ source: string
38
+ english: string
39
+ }
40
+
41
+ const INLINE_ENGLISH_MARKER = "\n\n→ EN: "
42
+
43
+ function extractExistingTranslation(ctx: HookContext, text: string): ExistingTranslation | undefined {
44
+ const bannerSuffix = `\n\n${createActivationBannerText(ctx.options)}`
45
+ const content = text.endsWith(bannerSuffix) ? text.slice(0, -bannerSuffix.length) : text
46
+ const markerIndex = content.lastIndexOf(INLINE_ENGLISH_MARKER)
47
+ if (markerIndex < 0) return
48
+
49
+ const english = content.slice(markerIndex + INLINE_ENGLISH_MARKER.length)
50
+ if (english.trim().length === 0) return
51
+ return { source: content.slice(0, markerIndex), english }
52
+ }
53
+
36
54
  async function activateFromTrigger(
37
55
  ctx: HookContext,
38
56
  input: ChatMessageInput,
@@ -68,15 +86,22 @@ async function translateUserPart(
68
86
  errors: { part: TextPartLike; error: unknown }[],
69
87
  ) {
70
88
  try {
71
- const english = await ctx.translator.translateText({
72
- text: part.text,
73
- sourceLanguage: state.translate_user_lang,
74
- targetLanguage: LLM_LANGUAGE,
75
- direction: "inbound",
76
- })
77
- const sourceHash = hashText(part.text)
78
- part.metadata = { ...(part.metadata ?? {}), ...mergeTranslatedMetadata(state, part, english) }
79
- part.text = `${part.text}\n\n→ EN: ${english}`
89
+ const existing = extractExistingTranslation(ctx, part.text)
90
+ const source = existing?.source ?? part.text
91
+ const english =
92
+ existing?.english ??
93
+ (await ctx.translator.translateText({
94
+ text: source,
95
+ sourceLanguage: state.translate_user_lang,
96
+ targetLanguage: LLM_LANGUAGE,
97
+ direction: "inbound",
98
+ }))
99
+ const sourceHash = hashText(source)
100
+ part.metadata = {
101
+ ...(part.metadata ?? {}),
102
+ ...mergeTranslatedMetadata(state, { ...part, text: source }, english),
103
+ }
104
+ if (!existing) part.text = `${source}${INLINE_ENGLISH_MARKER}${english}`
80
105
  nextParts.push(
81
106
  createLlmOnlyTextPart(part.sessionID, part.messageID, english, {
82
107
  translate_role: "llm_only_translation",