@wagemule/daemon 0.1.7 → 0.1.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/main.cjs CHANGED
@@ -1837,7 +1837,11 @@ var KimiAdapter = class {
1837
1837
  });
1838
1838
  this.lines = import_node_readline4.default.createInterface({ input: this.child.stdout });
1839
1839
  this.lines.on("line", (line) => this.handleLine(line));
1840
- this.writeWire("initialize", { clientInfo: { name: "wm-daemon", version: "0.0.1" } });
1840
+ this.writeWire("initialize", {
1841
+ protocol_version: "1.10",
1842
+ client: { name: "wm-daemon", version: "0.0.1" },
1843
+ capabilities: { supports_question: false, supports_plan_mode: false }
1844
+ });
1841
1845
  }
1842
1846
  async prompt(input) {
1843
1847
  const startInput = this.startInput;
@@ -1878,8 +1882,17 @@ var KimiAdapter = class {
1878
1882
  if (input.abortSignal?.aborted) throw new Error("Runtime aborted");
1879
1883
  input.abortSignal?.addEventListener("abort", abortHandler, { once: true });
1880
1884
  events.push({ type: "task_delivered", content: input.prompt });
1881
- this.writeWire("prompt", { prompt: input.prompt, sessionId: this.sessionId });
1885
+ this.active.promptRequestId = this.writeWire("prompt", { user_input: input.prompt });
1882
1886
  await withTimeout(turnFinished, input.timeoutMs ?? 18e4, "Kimi wire turn timeout");
1887
+ if (this.active.errorMessage) {
1888
+ return {
1889
+ status: this.active.status ?? "runtime_error",
1890
+ finalMessage,
1891
+ runtimeSessionId: this.sessionId,
1892
+ events,
1893
+ errorMessage: this.active.errorMessage
1894
+ };
1895
+ }
1883
1896
  return { status: "ok", finalMessage, runtimeSessionId: this.sessionId, events };
1884
1897
  } catch (error) {
1885
1898
  return {
@@ -1908,20 +1921,29 @@ var KimiAdapter = class {
1908
1921
  this.child = null;
1909
1922
  }
1910
1923
  writeWire(method, params) {
1911
- this.child?.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id: this.nextId++, method, params })}
1924
+ const id = String(this.nextId++);
1925
+ this.child?.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}
1912
1926
  `);
1927
+ return id;
1913
1928
  }
1914
1929
  handleLine(line) {
1915
- const parsed = parseStreamJsonLine("kimi", line);
1930
+ const parsed = parseKimiWireLine(line);
1916
1931
  if (!parsed) return;
1917
- if (parsed.sessionId) this.sessionId = parsed.sessionId;
1918
1932
  const active = this.active;
1919
1933
  const runInput = active?.runInput ?? (this.startInput ? { ...this.startInput, prompt: "" } : void 0);
1920
1934
  if (!runInput) return;
1935
+ if (parsed.errorMessage && active && parsed.id === active.promptRequestId) {
1936
+ active.errorMessage = parsed.errorMessage;
1937
+ active.status = "runtime_error";
1938
+ active.events.push({ type: "runtime_error", content: parsed.errorMessage, data: parsed.data });
1939
+ runInput.onEvent?.({ type: "runtime_error", content: parsed.errorMessage, data: parsed.data });
1940
+ active.finish();
1941
+ return;
1942
+ }
1921
1943
  if (active) {
1922
1944
  if (parsed.appendFinalMessage) active.append(parsed.appendFinalMessage);
1923
1945
  if (parsed.finalMessage) active.set(parsed.finalMessage);
1924
- if (parsed.events.some((event) => event.type === "turn_finished")) active.finish();
1946
+ if (parsed.finished || parsed.events.some((event) => event.type === "turn_finished")) active.finish();
1925
1947
  for (const event of parsed.events) {
1926
1948
  active.events.push(event);
1927
1949
  runInput.onEvent?.(event);
@@ -1929,6 +1951,67 @@ var KimiAdapter = class {
1929
1951
  }
1930
1952
  }
1931
1953
  };
1954
+ function parseKimiWireLine(line) {
1955
+ if (!line.trim()) return void 0;
1956
+ let value;
1957
+ try {
1958
+ value = JSON.parse(line);
1959
+ } catch {
1960
+ return {
1961
+ appendFinalMessage: `${line}
1962
+ `,
1963
+ events: [{ type: "runtime_stdout", content: line }]
1964
+ };
1965
+ }
1966
+ const msg = value;
1967
+ const id = typeof msg.id === "string" ? msg.id : void 0;
1968
+ if (msg.error && typeof msg.error === "object") {
1969
+ const error = msg.error;
1970
+ return {
1971
+ id,
1972
+ errorMessage: typeof error.message === "string" ? error.message : "Kimi wire request failed",
1973
+ data: msg,
1974
+ events: []
1975
+ };
1976
+ }
1977
+ if (msg.result && typeof msg.result === "object") {
1978
+ const result = msg.result;
1979
+ const status = typeof result.status === "string" ? result.status : void 0;
1980
+ return {
1981
+ id,
1982
+ finished: status === "finished" || status === "cancelled" || status === "max_steps_reached",
1983
+ data: msg,
1984
+ events: status ? [{ type: "turn_finished", data: { runtime: "kimi", status, result } }] : [{ type: "runtime_started", data: { runtime: "kimi", result } }]
1985
+ };
1986
+ }
1987
+ if (msg.method !== "event" || !msg.params || typeof msg.params !== "object") {
1988
+ return { id, events: [{ type: "runtime_event", data: msg }] };
1989
+ }
1990
+ const params = msg.params;
1991
+ const eventType = typeof params.type === "string" ? params.type : "runtime_event";
1992
+ const payload = params.payload && typeof params.payload === "object" ? params.payload : {};
1993
+ const text = stringValue5(payload.text) ?? stringValue5(payload.content) ?? stringValue5(payload.message) ?? stringValue5(payload.delta?.text);
1994
+ if ((eventType === "TextPart" || eventType === "ContentPart") && text) {
1995
+ return {
1996
+ appendFinalMessage: text,
1997
+ events: [{ type: "assistant_delta", content: text, data: msg }]
1998
+ };
1999
+ }
2000
+ if (eventType === "TurnEnd") {
2001
+ return { finished: true, events: [{ type: "turn_finished", data: msg }] };
2002
+ }
2003
+ return {
2004
+ events: [{ type: kimiEventName(eventType), content: text, data: msg }]
2005
+ };
2006
+ }
2007
+ function kimiEventName(eventType) {
2008
+ if (/tool/i.test(eventType)) return "tool_progress";
2009
+ if (/begin|status|loading|retry|compaction|hook|notification/i.test(eventType)) return "runtime_event";
2010
+ return eventType;
2011
+ }
2012
+ function stringValue5(value) {
2013
+ return typeof value === "string" ? value : void 0;
2014
+ }
1932
2015
  function kimiAgentYaml() {
1933
2016
  return [
1934
2017
  "version: 1",
@@ -3853,21 +3936,21 @@ function renderRuntimeLine(context, line) {
3853
3936
  function extractTextFromRuntimeJsonLine(line) {
3854
3937
  try {
3855
3938
  const record = JSON.parse(line);
3856
- const direct = stringValue5(record.result ?? record.text ?? record.content);
3939
+ const direct = stringValue6(record.result ?? record.text ?? record.content);
3857
3940
  if (direct) return direct;
3858
3941
  const message = record.message;
3859
- const messageText = message ? stringValue5(message.text ?? message.content) : void 0;
3942
+ const messageText = message ? stringValue6(message.text ?? message.content) : void 0;
3860
3943
  if (messageText) return messageText;
3861
3944
  const content = Array.isArray(message?.content) ? message?.content : [];
3862
3945
  const parts = content.map(
3863
- (part) => typeof part === "object" && part ? stringValue5(part.text) : void 0
3946
+ (part) => typeof part === "object" && part ? stringValue6(part.text) : void 0
3864
3947
  ).filter(Boolean);
3865
3948
  return parts.length > 0 ? parts.join("") : void 0;
3866
3949
  } catch {
3867
3950
  return line;
3868
3951
  }
3869
3952
  }
3870
- function stringValue5(value) {
3953
+ function stringValue6(value) {
3871
3954
  return typeof value === "string" && value.length > 0 ? value : void 0;
3872
3955
  }
3873
3956
  function writeLine(context, line) {
@@ -3891,7 +3974,7 @@ var import_ws = __toESM(require("ws"));
3891
3974
  // package.json
3892
3975
  var package_default = {
3893
3976
  name: "@wagemule/daemon",
3894
- version: "0.1.7",
3977
+ version: "0.1.9",
3895
3978
  private: false,
3896
3979
  description: "Wage Mule local daemon for connecting local agent runtimes to Workspace Server.",
3897
3980
  main: "./dist/main.cjs",
@@ -3987,30 +4070,7 @@ var ServerConnection = class {
3987
4070
  ws.on("open", () => {
3988
4071
  this.reconnectAttempt = 0;
3989
4072
  this.log(`socket open url=${redactUrl(url)}`);
3990
- this.send({
3991
- type: "ready",
3992
- capabilities: [
3993
- "agent:start",
3994
- "agent:stop",
3995
- "agent:deliver",
3996
- "agent:reset-workspace",
3997
- "agent:diagnostic",
3998
- "workspace:list",
3999
- "workspace:read",
4000
- "agent:skills:list",
4001
- "machine:runtime_models:detect",
4002
- "machine:workspace:scan",
4003
- "machine:workspace:delete",
4004
- "reminder-cache"
4005
- ],
4006
- runtimes: supportedRuntimeIds,
4007
- runningAgents: this.options.runningAgentsProvider?.() ?? [],
4008
- wakeableAgents: this.options.wakeableAgentsProvider?.() ?? [],
4009
- hostname: import_node_os6.default.hostname() || "local",
4010
- name: this.options.name,
4011
- os: `${process.platform}/${process.arch}`,
4012
- daemonVersion: this.options.daemonVersion ?? package_default.version
4013
- });
4073
+ void this.sendReady();
4014
4074
  });
4015
4075
  ws.on("message", (raw) => {
4016
4076
  const msg = JSON.parse(String(raw));
@@ -4036,6 +4096,42 @@ var ServerConnection = class {
4036
4096
  this.scheduleReconnect();
4037
4097
  });
4038
4098
  }
4099
+ async sendReady() {
4100
+ this.send({
4101
+ type: "ready",
4102
+ capabilities: [
4103
+ "agent:start",
4104
+ "agent:stop",
4105
+ "agent:deliver",
4106
+ "agent:reset-workspace",
4107
+ "agent:diagnostic",
4108
+ "workspace:list",
4109
+ "workspace:read",
4110
+ "agent:skills:list",
4111
+ "machine:runtime_models:detect",
4112
+ "machine:workspace:scan",
4113
+ "machine:workspace:delete",
4114
+ "reminder-cache"
4115
+ ],
4116
+ runtimes: await this.detectAvailableRuntimes(),
4117
+ runningAgents: this.options.runningAgentsProvider?.() ?? [],
4118
+ wakeableAgents: this.options.wakeableAgentsProvider?.() ?? [],
4119
+ hostname: import_node_os6.default.hostname() || "local",
4120
+ name: this.options.name,
4121
+ os: `${process.platform}/${process.arch}`,
4122
+ daemonVersion: this.options.daemonVersion ?? package_default.version
4123
+ });
4124
+ }
4125
+ async detectAvailableRuntimes() {
4126
+ if (this.options.runtimesProvider) return this.options.runtimesProvider();
4127
+ try {
4128
+ const results = await doctorRuntimes(supportedRuntimeIds);
4129
+ return results.filter((result) => result.detected).map((result) => result.runtime);
4130
+ } catch (error) {
4131
+ this.log(`runtime detection failed ${error instanceof Error ? error.message : String(error)}`);
4132
+ return [];
4133
+ }
4134
+ }
4039
4135
  scheduleReconnect() {
4040
4136
  if (this.stopped || this.reconnectTimer) return;
4041
4137
  const delay = this.nextReconnectDelayMs();
@@ -4298,20 +4394,24 @@ ${stderr}`);
4298
4394
  };
4299
4395
  function parseKimiModelConfig(configText) {
4300
4396
  const ids = [];
4397
+ const labels = /* @__PURE__ */ new Map();
4301
4398
  let defaultModel;
4302
4399
  let section;
4400
+ let currentModelKey;
4303
4401
  for (const rawLine of configText.split(/\r?\n/)) {
4304
4402
  const line = stripYamlComment(rawLine).trim();
4305
4403
  if (!line) continue;
4306
4404
  const table = line.match(/^\[models\.([^\]]+)]$/);
4307
4405
  if (table) {
4308
- section = `models.${unquote(table[1].trim())}`;
4309
- pushUnique(ids, section.slice("models.".length));
4406
+ currentModelKey = unquote(table[1].trim());
4407
+ section = `models.${currentModelKey}`;
4408
+ pushUnique(ids, currentModelKey);
4310
4409
  continue;
4311
4410
  }
4312
4411
  const anyTable = line.match(/^\[([^\]]+)]$/);
4313
4412
  if (anyTable) {
4314
4413
  section = anyTable[1]?.trim();
4414
+ currentModelKey = void 0;
4315
4415
  continue;
4316
4416
  }
4317
4417
  const defaultModelLine = line.match(/^default_model\s*=\s*["']?([^"']+)["']?\s*$/);
@@ -4320,7 +4420,11 @@ function parseKimiModelConfig(configText) {
4320
4420
  pushUnique(ids, defaultModel);
4321
4421
  continue;
4322
4422
  }
4323
- if (section?.startsWith("models.")) continue;
4423
+ if (section?.startsWith("models.")) {
4424
+ const model2 = line.match(/^model\s*=\s*["']?([^"']+)["']?\s*$/);
4425
+ if (model2 && currentModelKey) labels.set(currentModelKey, model2[1].trim());
4426
+ continue;
4427
+ }
4324
4428
  const model = line.match(/^model\s*=\s*["']?([^"']+)["']?\s*$/);
4325
4429
  if (model) {
4326
4430
  const value = model[1]?.trim();
@@ -4335,6 +4439,7 @@ function parseKimiModelConfig(configText) {
4335
4439
  }
4336
4440
  const models = ids.map((id) => ({
4337
4441
  ...defaultReasoningModel(id, id === defaultModel),
4442
+ label: labels.get(id) ?? id,
4338
4443
  default: id === defaultModel ? true : void 0
4339
4444
  }));
4340
4445
  return { models: normalizeDefault(models, defaultModel), defaultModel };