@kiki_agent/daemon 0.1.3 → 0.1.4

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/cli.cjs +157 -122
  2. package/package.json +1 -1
package/dist/cli.cjs CHANGED
@@ -71770,149 +71770,178 @@ async function executeRemoteJob(input) {
71770
71770
  signal: abortController.signal
71771
71771
  });
71772
71772
  }
71773
- async function runRemoteDaemonLoop(input) {
71774
- const wsUrl = toWsUrl(input.serverUrl, input.apiKey);
71775
- appendRuntimeDaemonLog(`\u8FDC\u7A0B daemon \u8FDE\u63A5 ${wsUrl}`);
71776
- const connect = () => new Promise((resolve, reject) => {
71777
- const socket2 = new wrapper_default(wsUrl, { perMessageDeflate: false });
71778
- socket2.once("open", () => resolve(socket2));
71779
- socket2.once("error", reject);
71780
- });
71781
- const socket = await connect();
71782
- let boundUserId = null;
71783
- socket.send(
71784
- JSON.stringify({
71785
- type: "register",
71786
- machineId: "pending",
71787
- os: osFingerprint(),
71788
- daemonVersion: "0.1.3",
71789
- fingerprint: osFingerprint()
71790
- })
71791
- );
71792
- const heartbeat = setInterval(() => {
71793
- if (socket.readyState !== wrapper_default.OPEN) return;
71794
- socket.send(JSON.stringify({ type: "heartbeat", ts: (/* @__PURE__ */ new Date()).toISOString() }));
71795
- }, 15e3);
71796
- socket.on("message", (raw) => {
71797
- let message = null;
71798
- try {
71799
- message = JSON.parse(String(raw));
71800
- } catch {
71801
- return;
71802
- }
71803
- if (message.type === "registered") {
71804
- boundUserId = message.userId;
71805
- provisionUserWorkspace(message.userId);
71806
- enterUserContext(message.userId);
71807
- appendRuntimeDaemonLog(`machine \u5DF2\u6CE8\u518C\uFF1A${message.machineId}\uFF08\u7528\u6237 ${message.userId}\uFF09`);
71808
- return;
71809
- }
71810
- if (message.type === "discover_runtimes") {
71773
+ var DAEMON_VERSION = "0.1.4";
71774
+ var RECONNECT_DELAY_MS = 5e3;
71775
+ function sleep(ms) {
71776
+ return new Promise((resolve) => setTimeout(resolve, ms));
71777
+ }
71778
+ function runOneConnection(wsUrl) {
71779
+ return new Promise((resolve) => {
71780
+ let settled = false;
71781
+ const done = () => {
71782
+ if (settled) return;
71783
+ settled = true;
71784
+ resolve();
71785
+ };
71786
+ const socket = new wrapper_default(wsUrl, { perMessageDeflate: false });
71787
+ let boundUserId = null;
71788
+ let heartbeat = null;
71789
+ socket.on("open", () => {
71790
+ socket.send(
71791
+ JSON.stringify({
71792
+ type: "register",
71793
+ machineId: "pending",
71794
+ os: osFingerprint(),
71795
+ daemonVersion: DAEMON_VERSION,
71796
+ fingerprint: osFingerprint()
71797
+ })
71798
+ );
71799
+ heartbeat = setInterval(() => {
71800
+ if (socket.readyState !== wrapper_default.OPEN) return;
71801
+ socket.send(JSON.stringify({ type: "heartbeat", ts: (/* @__PURE__ */ new Date()).toISOString() }));
71802
+ }, 15e3);
71803
+ });
71804
+ socket.on("message", (raw) => {
71805
+ let message = null;
71806
+ try {
71807
+ message = JSON.parse(String(raw));
71808
+ } catch {
71809
+ return;
71810
+ }
71811
+ if (message.type === "registered") {
71812
+ boundUserId = message.userId;
71813
+ provisionUserWorkspace(message.userId);
71814
+ enterUserContext(message.userId);
71815
+ appendRuntimeDaemonLog(`machine \u5DF2\u6CE8\u518C\uFF1A${message.machineId}\uFF08\u7528\u6237 ${message.userId}\uFF09`);
71816
+ return;
71817
+ }
71818
+ if (message.type === "discover_runtimes") {
71819
+ void (async () => {
71820
+ try {
71821
+ const result = await discoverLocalRuntimes();
71822
+ socket.send(
71823
+ JSON.stringify({
71824
+ type: "discover_runtimes_result",
71825
+ requestId: message.requestId,
71826
+ ok: true,
71827
+ items: result.items,
71828
+ workingDirectory: import_os2.default.homedir()
71829
+ })
71830
+ );
71831
+ } catch (error) {
71832
+ socket.send(
71833
+ JSON.stringify({
71834
+ type: "discover_runtimes_result",
71835
+ requestId: message.requestId,
71836
+ ok: false,
71837
+ error: error instanceof Error ? error.message : "\u626B\u63CF\u5931\u8D25"
71838
+ })
71839
+ );
71840
+ }
71841
+ })();
71842
+ return;
71843
+ }
71844
+ if (message.type === "check_runtime") {
71845
+ void (async () => {
71846
+ try {
71847
+ const result = await validateRuntimeEnvironment(message.payload);
71848
+ socket.send(
71849
+ JSON.stringify({
71850
+ type: "check_runtime_result",
71851
+ requestId: message.requestId,
71852
+ ok: result.ok,
71853
+ result
71854
+ })
71855
+ );
71856
+ } catch (error) {
71857
+ socket.send(
71858
+ JSON.stringify({
71859
+ type: "check_runtime_result",
71860
+ requestId: message.requestId,
71861
+ ok: false,
71862
+ error: error instanceof Error ? error.message : "\u68C0\u6D4B\u5931\u8D25"
71863
+ })
71864
+ );
71865
+ }
71866
+ })();
71867
+ return;
71868
+ }
71869
+ if (message.type !== "execute") return;
71811
71870
  void (async () => {
71812
71871
  try {
71813
- const result = await discoverLocalRuntimes();
71814
- socket.send(
71815
- JSON.stringify({
71816
- type: "discover_runtimes_result",
71872
+ const raw2 = message.payload;
71873
+ if (!raw2.goal || !raw2.subGoal || !raw2.task || !raw2.instance || !raw2.runtimeEnv) {
71874
+ throw new Error("execute payload \u4E0D\u5B8C\u6574");
71875
+ }
71876
+ const payload = {
71877
+ goal: raw2.goal,
71878
+ subGoal: raw2.subGoal,
71879
+ task: raw2.task,
71880
+ instance: raw2.instance,
71881
+ runtimeEnv: raw2.runtimeEnv,
71882
+ resumeContext: raw2.resumeContext
71883
+ };
71884
+ const initialTrajectory = Array.isArray(raw2.trajectory) ? raw2.trajectory : [];
71885
+ if (!boundUserId) {
71886
+ throw new Error("machine \u5C1A\u672A\u5B8C\u6210\u6CE8\u518C");
71887
+ }
71888
+ await runWithUserContext(
71889
+ boundUserId,
71890
+ () => executeRemoteJob({
71891
+ jobId: message.jobId,
71817
71892
  requestId: message.requestId,
71818
- ok: true,
71819
- items: result.items,
71820
- workingDirectory: import_os2.default.homedir()
71893
+ payload,
71894
+ initialTrajectory
71821
71895
  })
71822
71896
  );
71823
- } catch (error) {
71824
71897
  socket.send(
71825
71898
  JSON.stringify({
71826
- type: "discover_runtimes_result",
71827
- requestId: message.requestId,
71828
- ok: false,
71829
- error: error instanceof Error ? error.message : "\u626B\u63CF\u5931\u8D25"
71830
- })
71831
- );
71832
- }
71833
- })();
71834
- return;
71835
- }
71836
- if (message.type === "check_runtime") {
71837
- void (async () => {
71838
- try {
71839
- const result = await validateRuntimeEnvironment(message.payload);
71840
- socket.send(
71841
- JSON.stringify({
71842
- type: "check_runtime_result",
71843
- requestId: message.requestId,
71844
- ok: result.ok,
71845
- result
71899
+ type: "execute_result",
71900
+ jobId: message.jobId,
71901
+ ok: true
71846
71902
  })
71847
71903
  );
71848
71904
  } catch (error) {
71849
71905
  socket.send(
71850
71906
  JSON.stringify({
71851
- type: "check_runtime_result",
71852
- requestId: message.requestId,
71907
+ type: "execute_result",
71908
+ jobId: message.jobId,
71853
71909
  ok: false,
71854
- error: error instanceof Error ? error.message : "\u68C0\u6D4B\u5931\u8D25"
71910
+ error: error instanceof Error ? error.message : "\u6267\u884C\u5931\u8D25"
71855
71911
  })
71856
71912
  );
71857
71913
  }
71858
71914
  })();
71859
- return;
71860
- }
71861
- if (message.type !== "execute") return;
71862
- void (async () => {
71915
+ });
71916
+ socket.on("unexpected-response", (_req, res) => {
71917
+ appendRuntimeDaemonLog(`Tunnel \u63E1\u624B\u88AB\u62D2\u7EDD\uFF08HTTP ${res.statusCode}\uFF09\uFF0C${RECONNECT_DELAY_MS / 1e3}s \u540E\u91CD\u8FDE\u2026`);
71863
71918
  try {
71864
- const raw2 = message.payload;
71865
- if (!raw2.goal || !raw2.subGoal || !raw2.task || !raw2.instance || !raw2.runtimeEnv) {
71866
- throw new Error("execute payload \u4E0D\u5B8C\u6574");
71867
- }
71868
- const payload = {
71869
- goal: raw2.goal,
71870
- subGoal: raw2.subGoal,
71871
- task: raw2.task,
71872
- instance: raw2.instance,
71873
- runtimeEnv: raw2.runtimeEnv,
71874
- resumeContext: raw2.resumeContext
71875
- };
71876
- const initialTrajectory = Array.isArray(raw2.trajectory) ? raw2.trajectory : [];
71877
- if (!boundUserId) {
71878
- throw new Error("machine \u5C1A\u672A\u5B8C\u6210\u6CE8\u518C");
71879
- }
71880
- await runWithUserContext(
71881
- boundUserId,
71882
- () => executeRemoteJob({
71883
- jobId: message.jobId,
71884
- requestId: message.requestId,
71885
- payload,
71886
- initialTrajectory
71887
- })
71888
- );
71889
- socket.send(
71890
- JSON.stringify({
71891
- type: "execute_result",
71892
- jobId: message.jobId,
71893
- ok: true
71894
- })
71895
- );
71896
- } catch (error) {
71897
- socket.send(
71898
- JSON.stringify({
71899
- type: "execute_result",
71900
- jobId: message.jobId,
71901
- ok: false,
71902
- error: error instanceof Error ? error.message : "\u6267\u884C\u5931\u8D25"
71903
- })
71904
- );
71919
+ socket.terminate();
71920
+ } catch {
71905
71921
  }
71906
- })();
71907
- });
71908
- socket.on("close", () => {
71909
- clearInterval(heartbeat);
71910
- appendRuntimeDaemonLog("Tunnel \u8FDE\u63A5\u65AD\u5F00\uFF0C5s \u540E\u91CD\u8FDE\u2026");
71911
- setTimeout(() => {
71912
- void runRemoteDaemonLoop(input);
71913
- }, 5e3);
71922
+ done();
71923
+ });
71924
+ socket.on("error", (error) => {
71925
+ appendRuntimeDaemonLog(
71926
+ `Tunnel \u8FDE\u63A5\u9519\u8BEF\uFF1A${error instanceof Error ? error.message : String(error)}\uFF0C${RECONNECT_DELAY_MS / 1e3}s \u540E\u91CD\u8FDE\u2026`
71927
+ );
71928
+ done();
71929
+ });
71930
+ socket.on("close", () => {
71931
+ if (heartbeat) clearInterval(heartbeat);
71932
+ appendRuntimeDaemonLog(`Tunnel \u8FDE\u63A5\u65AD\u5F00\uFF0C${RECONNECT_DELAY_MS / 1e3}s \u540E\u91CD\u8FDE\u2026`);
71933
+ done();
71934
+ });
71914
71935
  });
71915
71936
  }
71937
+ async function runRemoteDaemonLoop(input) {
71938
+ const wsUrl = toWsUrl(input.serverUrl, input.apiKey);
71939
+ appendRuntimeDaemonLog(`\u8FDC\u7A0B daemon \u8FDE\u63A5 ${wsUrl}`);
71940
+ for (; ; ) {
71941
+ await runOneConnection(wsUrl);
71942
+ await sleep(RECONNECT_DELAY_MS);
71943
+ }
71944
+ }
71916
71945
 
71917
71946
  // src/pathEnv.ts
71918
71947
  var import_fs13 = __toESM(require("fs"));
@@ -72340,6 +72369,12 @@ async function main() {
72340
72369
  }
72341
72370
  const { serverUrl, apiKey } = requireConnectionArgs();
72342
72371
  console.log(`[kiki-daemon] \u524D\u53F0\u8FD0\u884C\uFF0C\u8FDE\u63A5 ${serverUrl}`);
72372
+ process.on("unhandledRejection", (reason) => {
72373
+ console.error("[kiki-daemon] unhandledRejection:", reason instanceof Error ? reason.message : reason);
72374
+ });
72375
+ process.on("uncaughtException", (error) => {
72376
+ console.error("[kiki-daemon] uncaughtException:", error instanceof Error ? error.message : error);
72377
+ });
72343
72378
  await runRemoteDaemonLoop({ serverUrl, apiKey });
72344
72379
  }
72345
72380
  void main().catch((error) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kiki_agent/daemon",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Kiki 本地执行节点 daemon:连接云端编排器并在本机运行任务。",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",