opencode-translate 0.0.16 → 0.0.17

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/auth.ts +59 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-translate",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "OpenCode plugin that lets the user chat in a configured source language while the main chat loop only sees English.",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/auth.ts CHANGED
@@ -198,19 +198,76 @@ function rewriteOpenAICodexBody(body: BodyInit | null | undefined): CodexBodyRew
198
198
  }
199
199
  }
200
200
 
201
+ function normalizeCodexOutputItem(item: unknown, index: number): unknown | undefined {
202
+ if (!isRecord(item)) return undefined
203
+ if (item.type !== "message" || item.role !== "assistant") return item
204
+ if (!Array.isArray(item.content)) return undefined
205
+
206
+ const content: Record<string, unknown>[] = []
207
+ for (const part of item.content) {
208
+ if (!isRecord(part) || part.type !== "output_text" || typeof part.text !== "string") continue
209
+ content.push({ ...part, annotations: Array.isArray(part.annotations) ? part.annotations : [] })
210
+ }
211
+
212
+ if (content.length === 0) return undefined
213
+ return {
214
+ ...item,
215
+ id: typeof item.id === "string" ? item.id : `msg_opencode_translate_${index}`,
216
+ role: "assistant",
217
+ content,
218
+ }
219
+ }
220
+
221
+ function buildCodexTextOutput(text: string): Record<string, unknown> {
222
+ return {
223
+ type: "message",
224
+ id: "msg_opencode_translate_0",
225
+ role: "assistant",
226
+ content: [{ type: "output_text", text, annotations: [] }],
227
+ }
228
+ }
229
+
201
230
  function parseCodexSSEResponse(text: string): unknown | undefined {
231
+ let finalResponse: unknown
232
+ let deltaText = ""
233
+ const outputItems: unknown[] = []
234
+
202
235
  for (const line of text.split(/\r?\n/)) {
203
236
  if (!line.startsWith("data: ")) continue
204
237
  const payload = line.slice(6).trim()
205
238
  if (!payload || payload === "[DONE]") continue
206
239
  try {
207
240
  const parsed = JSON.parse(payload) as Record<string, unknown>
241
+ if (parsed.type === "response.output_text.delta" && typeof parsed.delta === "string") {
242
+ deltaText += parsed.delta
243
+ continue
244
+ }
245
+ if (
246
+ (parsed.type === "response.output_item.done" || parsed.type === "response.output_item.added") &&
247
+ parsed.item
248
+ ) {
249
+ outputItems.push(parsed.item)
250
+ continue
251
+ }
208
252
  if ((parsed.type === "response.done" || parsed.type === "response.completed") && parsed.response) {
209
- return parsed.response
253
+ finalResponse = parsed.response
210
254
  }
211
255
  } catch {}
212
256
  }
213
- return undefined
257
+
258
+ if (!finalResponse && !deltaText && outputItems.length === 0) return undefined
259
+
260
+ const response: Record<string, unknown> = isRecord(finalResponse)
261
+ ? { ...finalResponse }
262
+ : { id: "resp_opencode_translate" }
263
+ const existingOutput: unknown[] = Array.isArray(response.output) ? response.output : []
264
+ const sourceOutput = existingOutput.length > 0 ? existingOutput : outputItems
265
+ const normalizedOutput = sourceOutput
266
+ .map((item, index) => normalizeCodexOutputItem(item, index))
267
+ .filter((item): item is unknown => item !== undefined)
268
+
269
+ response.output = normalizedOutput.length > 0 ? normalizedOutput : deltaText ? [buildCodexTextOutput(deltaText)] : []
270
+ return response
214
271
  }
215
272
 
216
273
  async function convertCodexSSEToJSON(response: Response): Promise<Response> {