@harmonyos-arkts/opencode-acp 0.0.8 → 0.0.9

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/dist/index.cjs CHANGED
@@ -20422,9 +20422,15 @@ function sanitize(obj, depth = 0) {
20422
20422
  } else if (val && typeof val === "object" && !Array.isArray(val)) {
20423
20423
  result[key] = sanitize(val, depth + 1);
20424
20424
  } else if (val && Array.isArray(val)) {
20425
- result[key] = val.map(
20426
- (item) => typeof item === "string" && item.length > 2e3 ? item.slice(0, 2e3) + `... [${item.length} chars total]` : item && typeof item === "object" ? sanitize(item, depth + 1) : item
20427
- );
20425
+ result[key] = val.map((item) => {
20426
+ if (typeof item === "string" && item.length > 2e3) {
20427
+ return item.slice(0, 2e3) + `... [${item.length} chars total]`;
20428
+ }
20429
+ if (item && typeof item === "object") {
20430
+ return sanitize(item, depth + 1);
20431
+ }
20432
+ return item;
20433
+ });
20428
20434
  } else {
20429
20435
  result[key] = val;
20430
20436
  }
@@ -20462,12 +20468,12 @@ function toLocations(toolName, input) {
20462
20468
  case "read":
20463
20469
  case "edit":
20464
20470
  case "write":
20465
- return input["filePath"] ? [{ path: input["filePath"] }] : [];
20471
+ return input.filePath ? [{ path: input.filePath }] : [];
20466
20472
  case "glob":
20467
20473
  case "grep":
20468
- return input["path"] ? [{ path: input["path"] }] : [];
20474
+ return input.path ? [{ path: input.path }] : [];
20469
20475
  case "list":
20470
- return input["path"] ? [{ path: input["path"] }] : [];
20476
+ return input.path ? [{ path: input.path }] : [];
20471
20477
  default:
20472
20478
  return [];
20473
20479
  }
@@ -20492,18 +20498,30 @@ function parseUri(uri) {
20492
20498
  return { type: "text", text: uri };
20493
20499
  }
20494
20500
  }
20501
+ function buildUpdateLogPayload(updateType, update) {
20502
+ if (updateType === "agent_message_chunk" || updateType === "agent_thought_chunk") {
20503
+ return { messageId: update.messageId?.slice(0, 12), delta: update.content?.text?.slice(0, 200) };
20504
+ }
20505
+ if (updateType === "tool_call" || updateType === "tool_call_update") {
20506
+ return { toolCallId: update.toolCallId, tool: update.title, kind: update.kind, status: update.status };
20507
+ }
20508
+ if (updateType === "session_info_update") {
20509
+ return { title: update.title, _meta: update._meta };
20510
+ }
20511
+ if (updateType === "usage_update") {
20512
+ return { used: update.used, size: update.size, cost: update.cost, _meta: update._meta, error: update._meta?.error?.message };
20513
+ }
20514
+ if (update._meta?.error) {
20515
+ return { error: update._meta.error?.message ?? update._meta.error };
20516
+ }
20517
+ return {};
20518
+ }
20495
20519
  async function sendToClient(connection, params) {
20496
20520
  const updateType = params.update.sessionUpdate;
20497
20521
  const update = params.update;
20498
20522
  acpOut(`sessionUpdate.${updateType}`, {
20499
20523
  sessionId: params.sessionId.slice(0, 12),
20500
- ...updateType === "agent_message_chunk" || updateType === "agent_thought_chunk" ? { messageId: update.messageId?.slice(0, 12), delta: update.content?.text?.slice(0, 200) } : updateType === "tool_call" || updateType === "tool_call_update" ? { toolCallId: update.toolCallId, tool: update.title, kind: update.kind, status: update.status } : updateType === "session_info_update" ? { title: update.title, _meta: update._meta } : updateType === "usage_update" ? {
20501
- used: update.used,
20502
- size: update.size,
20503
- cost: update.cost,
20504
- _meta: update._meta,
20505
- error: update._meta?.error?.message
20506
- } : update._meta?.error ? { error: update._meta.error?.message ?? update._meta.error } : {}
20524
+ ...buildUpdateLogPayload(updateType, update)
20507
20525
  });
20508
20526
  return connection.sessionUpdate(params);
20509
20527
  }
@@ -20534,9 +20552,19 @@ function normalizeError(err) {
20534
20552
  }
20535
20553
  if (typeof err === "object") {
20536
20554
  const o = err;
20537
- const message = typeof o.message === "string" && o.message.trim() ? o.message.trim() : typeof o.error === "string" && o.error.trim() ? o.error.trim() : void 0;
20555
+ let message;
20556
+ if (typeof o.message === "string" && o.message.trim()) {
20557
+ message = o.message.trim();
20558
+ } else if (typeof o.error === "string" && o.error.trim()) {
20559
+ message = o.error.trim();
20560
+ }
20538
20561
  if (!message) return void 0;
20539
- const cause = o.cause instanceof Error ? o.cause.message : typeof o.cause === "object" && o.cause !== null && typeof o.cause.message === "string" ? o.cause.message : void 0;
20562
+ let cause;
20563
+ if (o.cause instanceof Error) {
20564
+ cause = o.cause.message;
20565
+ } else if (typeof o.cause === "object" && o.cause !== null && typeof o.cause.message === "string") {
20566
+ cause = o.cause.message;
20567
+ }
20540
20568
  return {
20541
20569
  code: inferCode(o),
20542
20570
  message,
@@ -20757,8 +20785,8 @@ var EventHandler = class {
20757
20785
  }
20758
20786
  if (res.outcome.optionId !== "reject" && permission.permission === "edit") {
20759
20787
  const metadata = permission.metadata || {};
20760
- const filepath = typeof metadata["filepath"] === "string" ? metadata["filepath"] : "";
20761
- const diff = typeof metadata["diff"] === "string" ? metadata["diff"] : "";
20788
+ const filepath = typeof metadata.filepath === "string" ? metadata.filepath : "";
20789
+ const diff = typeof metadata.diff === "string" ? metadata.diff : "";
20762
20790
  if (filepath && diff) {
20763
20791
  const fileResult = await this.sdk.file.read({ path: filepath, directory }).then((x) => x.data).catch(() => void 0);
20764
20792
  const original = typeof fileResult?.content === "string" ? fileResult.content : "";
@@ -20927,9 +20955,8 @@ var EventHandler = class {
20927
20955
  this.fileDiffStats.set(resolved.sessionId, { additions, deletions, files: diff.length });
20928
20956
  return;
20929
20957
  }
20930
- default: {
20958
+ default:
20931
20959
  return;
20932
- }
20933
20960
  }
20934
20961
  }
20935
20962
  // ─── Child Session Announcement ──────────────────────────────────
@@ -21028,7 +21055,7 @@ var EventHandler = class {
21028
21055
  }
21029
21056
  this.bashSnapshots.delete(part.callID);
21030
21057
  if (part.tool === "task" && part.state.metadata) {
21031
- const childSessionId = typeof part.state.metadata["sessionId"] === "string" ? part.state.metadata["sessionId"] : void 0;
21058
+ const childSessionId = typeof part.state.metadata.sessionId === "string" ? part.state.metadata.sessionId : void 0;
21032
21059
  if (childSessionId && this.sessionManager.tryGet(childSessionId)) {
21033
21060
  const tracker = this.childToolCounts.get(childSessionId);
21034
21061
  await this.sendChildSessionFinished(
@@ -21058,9 +21085,14 @@ var EventHandler = class {
21058
21085
  ];
21059
21086
  if (kind === "edit") {
21060
21087
  const input = part.state.input;
21061
- const filePath = typeof input["filePath"] === "string" ? input["filePath"] : "";
21062
- const oldText = typeof input["oldString"] === "string" ? input["oldString"] : "";
21063
- const newText = typeof input["newString"] === "string" ? input["newString"] : typeof input["content"] === "string" ? input["content"] : "";
21088
+ const filePath = typeof input.filePath === "string" ? input.filePath : "";
21089
+ const oldText = typeof input.oldString === "string" ? input.oldString : "";
21090
+ let newText = "";
21091
+ if (typeof input.newString === "string") {
21092
+ newText = input.newString;
21093
+ } else if (typeof input.content === "string") {
21094
+ newText = input.content;
21095
+ }
21064
21096
  content.push({ type: "diff", path: filePath, oldText, newText });
21065
21097
  this.accumulateAICodeChangeStats(sessionId, part);
21066
21098
  }
@@ -21109,7 +21141,7 @@ var EventHandler = class {
21109
21141
  }
21110
21142
  this.bashSnapshots.delete(part.callID);
21111
21143
  if (part.tool === "task" && part.state.metadata) {
21112
- const childSessionId = typeof part.state.metadata["sessionId"] === "string" ? part.state.metadata["sessionId"] : void 0;
21144
+ const childSessionId = typeof part.state.metadata.sessionId === "string" ? part.state.metadata.sessionId : void 0;
21113
21145
  if (childSessionId && this.sessionManager.tryGet(childSessionId)) {
21114
21146
  const tracker = this.childToolCounts.get(childSessionId);
21115
21147
  const errText = typeof part.state.error === "string" && part.state.error.trim() ? part.state.error.trim() : "Sub-agent task failed";
@@ -21154,6 +21186,8 @@ var EventHandler = class {
21154
21186
  });
21155
21187
  return;
21156
21188
  }
21189
+ default:
21190
+ return;
21157
21191
  }
21158
21192
  }
21159
21193
  /**
@@ -21186,7 +21220,7 @@ var EventHandler = class {
21186
21220
  bashOutput(part) {
21187
21221
  if (part.tool !== "bash") return;
21188
21222
  if (!("metadata" in part.state) || !part.state.metadata || typeof part.state.metadata !== "object") return;
21189
- const output = part.state.metadata["output"];
21223
+ const output = part.state.metadata.output;
21190
21224
  if (typeof output !== "string") return;
21191
21225
  return output;
21192
21226
  }
@@ -21205,7 +21239,7 @@ var EventHandler = class {
21205
21239
  accumulateAICodeChangeStats(sessionId, part) {
21206
21240
  if (part.state.status !== "completed") return;
21207
21241
  const meta3 = part.state.metadata ?? {};
21208
- const aiCodeChange = meta3["aiCodeChange"];
21242
+ const aiCodeChange = meta3.aiCodeChange;
21209
21243
  if (!aiCodeChange || aiCodeChange.additions === 0 && aiCodeChange.deletions === 0) return;
21210
21244
  const fileMap = this.aiCodeChangeStats.get(sessionId) ?? /* @__PURE__ */ new Map();
21211
21245
  const existing = fileMap.get(aiCodeChange.file) ?? { additions: 0, deletions: 0 };
@@ -21346,14 +21380,19 @@ function sessionIdFromEvent(event) {
21346
21380
  return typeof props.sessionID === "string" ? props.sessionID : void 0;
21347
21381
  case "session.diff":
21348
21382
  case "session.updated":
21349
- case "session.created":
21350
- return typeof props.sessionID === "string" ? props.sessionID : typeof props.info?.id === "string" ? props.info.id : void 0;
21383
+ case "session.created": {
21384
+ if (typeof props.sessionID === "string") return props.sessionID;
21385
+ const info = props.info;
21386
+ if (typeof info?.id === "string") return info.id;
21387
+ return void 0;
21388
+ }
21351
21389
  case "permission.asked": {
21352
21390
  const permission = props;
21353
21391
  return typeof permission.sessionID === "string" ? permission.sessionID : void 0;
21354
21392
  }
21393
+ default:
21394
+ return void 0;
21355
21395
  }
21356
- return void 0;
21357
21396
  }
21358
21397
  var TurnTimeoutTracker = class {
21359
21398
  constructor(sessionManager) {
@@ -21973,7 +22012,7 @@ var Agent = class {
21973
22012
  const session = this.sessionManager.get(params.sessionId);
21974
22013
  const providers = await this.sdk.config.providers({ directory: session.cwd }).then((x) => x.data?.providers ?? []).catch(() => []);
21975
22014
  const sortedProviders = [...providers].sort(
21976
- (a, b) => a.name.toLowerCase() < b.name.toLowerCase() ? -1 : a.name.toLowerCase() > b.name.toLowerCase() ? 1 : 0
22015
+ (a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase())
21977
22016
  );
21978
22017
  if (params.configId === "model") {
21979
22018
  if (typeof params.value !== "string") throw RequestError.invalidParams("model value must be a string");
@@ -22102,7 +22141,7 @@ var Agent = class {
22102
22141
  const model = await this.defaultModel(params.cwd);
22103
22142
  const providers = await this.sdk.config.providers({ directory: params.cwd }).then((x) => x.data?.providers ?? []).catch(() => []);
22104
22143
  const sortedProviders = [...providers].sort(
22105
- (a, b) => a.name.toLowerCase() < b.name.toLowerCase() ? -1 : a.name.toLowerCase() > b.name.toLowerCase() ? 1 : 0
22144
+ (a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase())
22106
22145
  );
22107
22146
  const availableModels = sortedProviders.flatMap(
22108
22147
  (provider) => Object.values(provider.models).flatMap((modelInfo) => {
@@ -22224,7 +22263,12 @@ var Agent = class {
22224
22263
  if (part.type === "tool") {
22225
22264
  await this.replayToolPart(sessionId, part);
22226
22265
  } else if (part.type === "text" && part.text) {
22227
- const audience = part.synthetic ? ["assistant"] : part.ignored ? ["user"] : void 0;
22266
+ let audience;
22267
+ if (part.synthetic) {
22268
+ audience = ["assistant"];
22269
+ } else if (part.ignored) {
22270
+ audience = ["user"];
22271
+ }
22228
22272
  await sendToClient(this.connection, {
22229
22273
  sessionId,
22230
22274
  update: {
@@ -22324,11 +22368,17 @@ var Agent = class {
22324
22368
  const content = [{ type: "content", content: { type: "text", text: part.state.output } }];
22325
22369
  if (kind === "edit") {
22326
22370
  const input = part.state.input;
22371
+ let newText = "";
22372
+ if (typeof input.newString === "string") {
22373
+ newText = input.newString;
22374
+ } else if (typeof input.content === "string") {
22375
+ newText = input.content;
22376
+ }
22327
22377
  content.push({
22328
22378
  type: "diff",
22329
- path: typeof input["filePath"] === "string" ? input["filePath"] : "",
22330
- oldText: typeof input["oldString"] === "string" ? input["oldString"] : "",
22331
- newText: typeof input["newString"] === "string" ? input["newString"] : typeof input["content"] === "string" ? input["content"] : ""
22379
+ path: typeof input.filePath === "string" ? input.filePath : "",
22380
+ oldText: typeof input.oldString === "string" ? input.oldString : "",
22381
+ newText
22332
22382
  });
22333
22383
  this.eventHandler.accumulateAICodeChangeStats(sessionId, part);
22334
22384
  }
@@ -22384,6 +22434,8 @@ var Agent = class {
22384
22434
  }).catch(() => {
22385
22435
  });
22386
22436
  break;
22437
+ default:
22438
+ break;
22387
22439
  }
22388
22440
  }
22389
22441
  async sendUsageUpdate(sessionId, directory, turnError) {
@@ -22473,11 +22525,18 @@ var Agent = class {
22473
22525
  } : { ...cs };
22474
22526
  }
22475
22527
  }
22476
- const totalAICodeChangeStats = parentStats ? childAICodeChangeStats ? {
22477
- additions: parentStats.additions + childAICodeChangeStats.additions,
22478
- deletions: parentStats.deletions + childAICodeChangeStats.deletions,
22479
- files: parentStats.files + childAICodeChangeStats.files
22480
- } : parentStats : childAICodeChangeStats;
22528
+ let totalAICodeChangeStats;
22529
+ if (parentStats && childAICodeChangeStats) {
22530
+ totalAICodeChangeStats = {
22531
+ additions: parentStats.additions + childAICodeChangeStats.additions,
22532
+ deletions: parentStats.deletions + childAICodeChangeStats.deletions,
22533
+ files: parentStats.files + childAICodeChangeStats.files
22534
+ };
22535
+ } else if (parentStats) {
22536
+ totalAICodeChangeStats = parentStats;
22537
+ } else {
22538
+ totalAICodeChangeStats = childAICodeChangeStats;
22539
+ }
22481
22540
  if (totalAICodeChangeStats) {
22482
22541
  _meta.aiCodeChange = totalAICodeChangeStats;
22483
22542
  }
@@ -22533,6 +22592,8 @@ var Agent = class {
22533
22592
  }
22534
22593
  break;
22535
22594
  }
22595
+ default:
22596
+ break;
22536
22597
  }
22537
22598
  }
22538
22599
  return result;