@clwnd/opencode 0.1.0 → 0.3.0

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/dist/index.js +72 -24
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -282,6 +282,53 @@ var generateId = createIdGenerator();
282
282
  var { btoa, atob } = globalThis;
283
283
 
284
284
  // provider.ts
285
+ var MCP_PREFIX = "mcp__clwnd__";
286
+ function mapToolName(name14) {
287
+ if (name14.startsWith(MCP_PREFIX)) return name14.slice(MCP_PREFIX.length);
288
+ return name14;
289
+ }
290
+ var INPUT_FIELD_MAP = {
291
+ read: { file_path: "filePath" },
292
+ edit: { file_path: "filePath", old_string: "oldString", new_string: "newString", replace_all: "replaceAll" },
293
+ write: { file_path: "filePath" },
294
+ bash: {},
295
+ // command, description, timeout are already correct
296
+ glob: {},
297
+ // pattern, path are already correct
298
+ grep: {}
299
+ // pattern, path, include are already correct
300
+ };
301
+ function mapToolInput(toolName, input) {
302
+ const ocName = mapToolName(toolName);
303
+ const fieldMap = INPUT_FIELD_MAP[ocName];
304
+ if (!fieldMap || Object.keys(fieldMap).length === 0) return input;
305
+ try {
306
+ const parsed = JSON.parse(input);
307
+ const mapped = {};
308
+ for (const [k, v] of Object.entries(parsed)) {
309
+ mapped[fieldMap[k] ?? k] = v;
310
+ }
311
+ return JSON.stringify(mapped);
312
+ } catch {
313
+ return input;
314
+ }
315
+ }
316
+ function parseToolResult(resultText) {
317
+ const metaMatch = resultText.match(/<!--clwnd-meta:(.*?)-->/s);
318
+ let title = "";
319
+ let metadata = {};
320
+ let output = resultText;
321
+ if (metaMatch) {
322
+ output = resultText.replace(/\n?<!--clwnd-meta:.*?-->/s, "").trim();
323
+ try {
324
+ const parsed = JSON.parse(metaMatch[1]);
325
+ title = parsed.title ?? "";
326
+ metadata = parsed.metadata ?? {};
327
+ } catch {
328
+ }
329
+ }
330
+ return { output, title, metadata };
331
+ }
285
332
  function defaultSocketPath() {
286
333
  const runtime = process.env.XDG_RUNTIME_DIR;
287
334
  if (runtime) return `${runtime}/clwnd/clwnd.sock`;
@@ -296,15 +343,6 @@ function streamCall(msg) {
296
343
  unix: SOCK_PATH
297
344
  });
298
345
  }
299
- async function ipcCall(msg) {
300
- const r = await fetch("http://localhost/", {
301
- method: "POST",
302
- headers: { "Content-Type": "application/json" },
303
- body: JSON.stringify(msg),
304
- unix: SOCK_PATH
305
- });
306
- if (!r.ok) throw new Error(`clwnd IPC ${r.status}`);
307
- }
308
346
  function extractText(prompt) {
309
347
  for (let i = prompt.length - 1; i >= 0; i--) {
310
348
  const m = prompt[i];
@@ -356,8 +394,6 @@ var ClwndModel = class {
356
394
  const abort = () => {
357
395
  if (resolved) return;
358
396
  resolved = true;
359
- ipcCall({ action: "destroy", opencodeSessionId: sid }).catch(() => {
360
- });
361
397
  reject(new Error("aborted"));
362
398
  };
363
399
  opts.abortSignal?.addEventListener("abort", abort);
@@ -394,16 +430,17 @@ var ClwndModel = class {
394
430
  }
395
431
  if (ct === "tool_call" && msg.toolCallId && msg.toolName) {
396
432
  const accumulated = toolInputAccum.get(msg.toolCallId) ?? "{}";
433
+ const mapped = mapToolInput(msg.toolName, accumulated);
397
434
  let input = {};
398
435
  try {
399
- input = JSON.parse(accumulated);
436
+ input = JSON.parse(mapped);
400
437
  } catch {
401
438
  input = {};
402
439
  }
403
440
  toolCalls.push({
404
441
  type: "tool-call",
405
442
  toolCallId: msg.toolCallId,
406
- toolName: msg.toolName,
443
+ toolName: mapToolName(msg.toolName),
407
444
  input
408
445
  });
409
446
  }
@@ -496,8 +533,6 @@ var ClwndModel = class {
496
533
  }
497
534
  }
498
535
  opts.abortSignal?.addEventListener("abort", () => {
499
- ipcCall({ action: "destroy", opencodeSessionId: sid }).catch(() => {
500
- });
501
536
  close();
502
537
  });
503
538
  let resp;
@@ -559,7 +594,7 @@ var ClwndModel = class {
559
594
  }
560
595
  if (ct === "tool_input_start" && msg.toolCallId && msg.toolName) {
561
596
  toolInputAccum.set(msg.toolCallId, "");
562
- emit({ type: "tool-input-start", id: msg.toolCallId, toolName: msg.toolName });
597
+ emit({ type: "tool-input-start", id: msg.toolCallId, toolName: mapToolName(msg.toolName) });
563
598
  }
564
599
  if (ct === "tool_input_delta" && msg.toolCallId && msg.partialJson) {
565
600
  const prev = toolInputAccum.get(msg.toolCallId) ?? "";
@@ -567,20 +602,35 @@ var ClwndModel = class {
567
602
  emit({ type: "tool-input-delta", id: msg.toolCallId, delta: msg.partialJson });
568
603
  }
569
604
  if (ct === "tool_call" && msg.toolCallId && msg.toolName) {
570
- const accumulated = toolInputAccum.get(msg.toolCallId) ?? "{}";
605
+ const ocToolName = mapToolName(msg.toolName);
606
+ if (!toolInputAccum.has(msg.toolCallId)) {
607
+ emit({ type: "tool-input-start", id: msg.toolCallId, toolName: ocToolName });
608
+ }
609
+ const accumulated = toolInputAccum.get(msg.toolCallId);
610
+ let rawInput;
611
+ if (accumulated) {
612
+ rawInput = mapToolInput(msg.toolName, accumulated);
613
+ } else if (msg.input && typeof msg.input === "object") {
614
+ rawInput = mapToolInput(msg.toolName, JSON.stringify(msg.input));
615
+ } else {
616
+ rawInput = "{}";
617
+ }
571
618
  emit({
572
619
  type: "tool-call",
573
620
  toolCallId: msg.toolCallId,
574
- toolName: msg.toolName,
575
- input: accumulated,
621
+ toolName: ocToolName,
622
+ input: rawInput,
576
623
  providerExecuted: true
577
624
  });
578
625
  }
579
- if (ct === "tool_result" && msg.toolCallId) {
626
+ if (ct === "tool_result" && (msg.toolCallId || msg.toolUseId)) {
627
+ const callId = msg.toolCallId ?? msg.toolUseId;
628
+ const raw = msg.result ?? "";
629
+ const { output, title, metadata } = parseToolResult(typeof raw === "string" ? raw : JSON.stringify(raw));
580
630
  emit({
581
631
  type: "tool-result",
582
- toolCallId: msg.toolCallId,
583
- result: { output: msg.result ?? "", title: "", metadata: {} },
632
+ toolCallId: callId,
633
+ result: { output, title, metadata },
584
634
  providerExecuted: true
585
635
  });
586
636
  }
@@ -619,8 +669,6 @@ var ClwndModel = class {
619
669
  }
620
670
  },
621
671
  cancel() {
622
- ipcCall({ action: "destroy", opencodeSessionId: sid }).catch(() => {
623
- });
624
672
  }
625
673
  });
626
674
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clwnd/opencode",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "clwnd for opencode",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",