miladyai 2.0.0-alpha.27

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 (241) hide show
  1. package/dist/_virtual/_rolldown/runtime.js +7 -0
  2. package/dist/actions/emote.js +64 -0
  3. package/dist/actions/restart.js +81 -0
  4. package/dist/actions/send-message.js +152 -0
  5. package/dist/agent-admin-routes.js +82 -0
  6. package/dist/agent-lifecycle-routes.js +79 -0
  7. package/dist/agent-transfer-routes.js +102 -0
  8. package/dist/api/agent-admin-routes.js +82 -0
  9. package/dist/api/agent-lifecycle-routes.js +79 -0
  10. package/dist/api/agent-transfer-routes.js +102 -0
  11. package/dist/api/apps-hyperscape-routes.js +58 -0
  12. package/dist/api/apps-routes.js +114 -0
  13. package/dist/api/auth-routes.js +56 -0
  14. package/dist/api/autonomy-routes.js +44 -0
  15. package/dist/api/bug-report-routes.js +111 -0
  16. package/dist/api/character-routes.js +195 -0
  17. package/dist/api/cloud-routes.js +330 -0
  18. package/dist/api/cloud-status-routes.js +155 -0
  19. package/dist/api/compat-utils.js +111 -0
  20. package/dist/api/database.js +735 -0
  21. package/dist/api/diagnostics-routes.js +205 -0
  22. package/dist/api/drop-service.js +134 -0
  23. package/dist/api/early-logs.js +86 -0
  24. package/dist/api/http-helpers.js +131 -0
  25. package/dist/api/knowledge-routes.js +534 -0
  26. package/dist/api/memory-bounds.js +71 -0
  27. package/dist/api/models-routes.js +28 -0
  28. package/dist/api/og-tracker.js +36 -0
  29. package/dist/api/permissions-routes.js +109 -0
  30. package/dist/api/plugin-validation.js +198 -0
  31. package/dist/api/provider-switch-config.js +41 -0
  32. package/dist/api/registry-routes.js +86 -0
  33. package/dist/api/registry-service.js +164 -0
  34. package/dist/api/sandbox-routes.js +1112 -0
  35. package/dist/api/server.js +7949 -0
  36. package/dist/api/subscription-routes.js +172 -0
  37. package/dist/api/terminal-run-limits.js +24 -0
  38. package/dist/api/training-routes.js +158 -0
  39. package/dist/api/trajectory-routes.js +300 -0
  40. package/dist/api/trigger-routes.js +246 -0
  41. package/dist/api/twitter-verify.js +134 -0
  42. package/dist/api/tx-service.js +108 -0
  43. package/dist/api/wallet-routes.js +266 -0
  44. package/dist/api/wallet.js +568 -0
  45. package/dist/api/whatsapp-routes.js +182 -0
  46. package/dist/api/zip-utils.js +109 -0
  47. package/dist/apps-hyperscape-routes.js +58 -0
  48. package/dist/apps-routes.js +114 -0
  49. package/dist/ascii.js +20 -0
  50. package/dist/auth/anthropic.js +44 -0
  51. package/dist/auth/apply-stealth.js +41 -0
  52. package/dist/auth/claude-code-stealth.js +78 -0
  53. package/dist/auth/credentials.js +156 -0
  54. package/dist/auth/index.js +5 -0
  55. package/dist/auth/openai-codex.js +66 -0
  56. package/dist/auth/types.js +9 -0
  57. package/dist/auth-routes.js +56 -0
  58. package/dist/autonomy-routes.js +44 -0
  59. package/dist/bug-report-routes.js +111 -0
  60. package/dist/build-info.json +6 -0
  61. package/dist/character-routes.js +195 -0
  62. package/dist/cli/argv.js +63 -0
  63. package/dist/cli/banner.js +34 -0
  64. package/dist/cli/cli-name.js +21 -0
  65. package/dist/cli/cli-utils.js +16 -0
  66. package/dist/cli/git-commit.js +78 -0
  67. package/dist/cli/parse-duration.js +15 -0
  68. package/dist/cli/plugins-cli.js +590 -0
  69. package/dist/cli/profile-utils.js +9 -0
  70. package/dist/cli/profile.js +95 -0
  71. package/dist/cli/program/build-program.js +17 -0
  72. package/dist/cli/program/command-registry.js +23 -0
  73. package/dist/cli/program/help.js +47 -0
  74. package/dist/cli/program/preaction.js +33 -0
  75. package/dist/cli/program/register.config.js +106 -0
  76. package/dist/cli/program/register.configure.js +20 -0
  77. package/dist/cli/program/register.dashboard.js +124 -0
  78. package/dist/cli/program/register.models.js +23 -0
  79. package/dist/cli/program/register.setup.js +36 -0
  80. package/dist/cli/program/register.start.js +22 -0
  81. package/dist/cli/program/register.subclis.js +70 -0
  82. package/dist/cli/program/register.tui.js +163 -0
  83. package/dist/cli/program/register.update.js +154 -0
  84. package/dist/cli/program.js +3 -0
  85. package/dist/cli/run-main.js +37 -0
  86. package/dist/cli/version.js +7 -0
  87. package/dist/cloud/validate-url.js +93 -0
  88. package/dist/cloud-routes.js +330 -0
  89. package/dist/cloud-status-routes.js +155 -0
  90. package/dist/compat-utils.js +111 -0
  91. package/dist/config/config.js +69 -0
  92. package/dist/config/env-vars.js +19 -0
  93. package/dist/config/includes.js +121 -0
  94. package/dist/config/object-utils.js +7 -0
  95. package/dist/config/paths.js +38 -0
  96. package/dist/config/plugin-auto-enable.js +231 -0
  97. package/dist/config/schema.js +864 -0
  98. package/dist/config/telegram-custom-commands.js +76 -0
  99. package/dist/config/zod-schema.agent-runtime.js +519 -0
  100. package/dist/config/zod-schema.core.js +538 -0
  101. package/dist/config/zod-schema.hooks.js +103 -0
  102. package/dist/config/zod-schema.js +488 -0
  103. package/dist/config/zod-schema.providers-core.js +785 -0
  104. package/dist/config/zod-schema.session.js +73 -0
  105. package/dist/core-plugins.js +37 -0
  106. package/dist/custom-actions.js +250 -0
  107. package/dist/database.js +735 -0
  108. package/dist/diagnostics/integration-observability.js +57 -0
  109. package/dist/diagnostics-routes.js +205 -0
  110. package/dist/drop-service.js +134 -0
  111. package/dist/early-logs.js +24 -0
  112. package/dist/eliza.js +2061 -0
  113. package/dist/emotes/catalog.js +271 -0
  114. package/dist/entry.js +40 -0
  115. package/dist/hooks/discovery.js +167 -0
  116. package/dist/hooks/eligibility.js +64 -0
  117. package/dist/hooks/index.js +4 -0
  118. package/dist/hooks/loader.js +147 -0
  119. package/dist/hooks/registry.js +55 -0
  120. package/dist/http-helpers.js +131 -0
  121. package/dist/index.js +49 -0
  122. package/dist/knowledge-routes.js +534 -0
  123. package/dist/memory-bounds.js +71 -0
  124. package/dist/milady-plugin.js +90 -0
  125. package/dist/models-routes.js +28 -0
  126. package/dist/onboarding-names.js +78 -0
  127. package/dist/onboarding-presets.js +922 -0
  128. package/dist/package.json +1 -0
  129. package/dist/permissions-routes.js +109 -0
  130. package/dist/plugin-validation.js +107 -0
  131. package/dist/plugins/whatsapp/actions.js +91 -0
  132. package/dist/plugins/whatsapp/index.js +16 -0
  133. package/dist/plugins/whatsapp/service.js +270 -0
  134. package/dist/provider-switch-config.js +41 -0
  135. package/dist/providers/admin-trust.js +46 -0
  136. package/dist/providers/autonomous-state.js +101 -0
  137. package/dist/providers/session-bridge.js +86 -0
  138. package/dist/providers/session-utils.js +36 -0
  139. package/dist/providers/simple-mode.js +50 -0
  140. package/dist/providers/ui-catalog.js +15 -0
  141. package/dist/providers/workspace-provider.js +93 -0
  142. package/dist/providers/workspace.js +348 -0
  143. package/dist/registry-routes.js +86 -0
  144. package/dist/registry-service.js +164 -0
  145. package/dist/restart.js +40 -0
  146. package/dist/runtime/core-plugins.js +37 -0
  147. package/dist/runtime/custom-actions.js +250 -0
  148. package/dist/runtime/eliza.js +2061 -0
  149. package/dist/runtime/embedding-manager-support.js +185 -0
  150. package/dist/runtime/embedding-manager.js +193 -0
  151. package/dist/runtime/embedding-presets.js +54 -0
  152. package/dist/runtime/embedding-state.js +8 -0
  153. package/dist/runtime/milady-plugin.js +90 -0
  154. package/dist/runtime/onboarding-names.js +78 -0
  155. package/dist/runtime/restart.js +40 -0
  156. package/dist/runtime/version.js +7 -0
  157. package/dist/sandbox-routes.js +1112 -0
  158. package/dist/security/audit-log.js +149 -0
  159. package/dist/security/network-policy.js +70 -0
  160. package/dist/server.js +7949 -0
  161. package/dist/services/agent-export.js +559 -0
  162. package/dist/services/app-manager.js +389 -0
  163. package/dist/services/browser-capture.js +86 -0
  164. package/dist/services/fallback-training-service.js +128 -0
  165. package/dist/services/mcp-marketplace.js +134 -0
  166. package/dist/services/plugin-installer.js +396 -0
  167. package/dist/services/plugin-manager-types.js +15 -0
  168. package/dist/services/registry-client-app-meta.js +144 -0
  169. package/dist/services/registry-client-endpoints.js +166 -0
  170. package/dist/services/registry-client-local.js +271 -0
  171. package/dist/services/registry-client-network.js +93 -0
  172. package/dist/services/registry-client-queries.js +70 -0
  173. package/dist/services/registry-client.js +157 -0
  174. package/dist/services/sandbox-engine.js +511 -0
  175. package/dist/services/sandbox-manager.js +297 -0
  176. package/dist/services/self-updater.js +175 -0
  177. package/dist/services/skill-catalog-client.js +119 -0
  178. package/dist/services/skill-marketplace.js +521 -0
  179. package/dist/services/stream-manager.js +236 -0
  180. package/dist/services/update-checker.js +121 -0
  181. package/dist/services/update-notifier.js +29 -0
  182. package/dist/services/version-compat.js +78 -0
  183. package/dist/services/whatsapp-pairing.js +196 -0
  184. package/dist/shared/ui-catalog-prompt.js +728 -0
  185. package/dist/subscription-routes.js +172 -0
  186. package/dist/terminal/links.js +19 -0
  187. package/dist/terminal/palette.js +14 -0
  188. package/dist/terminal/theme.js +25 -0
  189. package/dist/terminal-run-limits.js +24 -0
  190. package/dist/training-routes.js +158 -0
  191. package/dist/trajectory-routes.js +300 -0
  192. package/dist/trigger-routes.js +246 -0
  193. package/dist/triggers/action.js +218 -0
  194. package/dist/triggers/runtime.js +281 -0
  195. package/dist/triggers/scheduling.js +295 -0
  196. package/dist/triggers/types.js +5 -0
  197. package/dist/tui/components/assistant-message.js +76 -0
  198. package/dist/tui/components/chat-editor.js +34 -0
  199. package/dist/tui/components/embeddings-overlay.js +46 -0
  200. package/dist/tui/components/footer.js +60 -0
  201. package/dist/tui/components/index.js +15 -0
  202. package/dist/tui/components/modal-frame.js +45 -0
  203. package/dist/tui/components/modal-style.js +15 -0
  204. package/dist/tui/components/model-selector.js +70 -0
  205. package/dist/tui/components/pinned-chat-layout.js +46 -0
  206. package/dist/tui/components/plugins-endpoints-tab.js +196 -0
  207. package/dist/tui/components/plugins-installed-tab-view.js +69 -0
  208. package/dist/tui/components/plugins-installed-tab.js +319 -0
  209. package/dist/tui/components/plugins-overlay-catalog.js +81 -0
  210. package/dist/tui/components/plugins-overlay-data-api.js +21 -0
  211. package/dist/tui/components/plugins-overlay-data-shared.js +20 -0
  212. package/dist/tui/components/plugins-overlay-data.js +323 -0
  213. package/dist/tui/components/plugins-overlay.js +117 -0
  214. package/dist/tui/components/plugins-store-tab.js +148 -0
  215. package/dist/tui/components/settings-overlay.js +61 -0
  216. package/dist/tui/components/status-bar.js +64 -0
  217. package/dist/tui/components/tool-execution.js +68 -0
  218. package/dist/tui/components/user-message.js +22 -0
  219. package/dist/tui/eliza-tui-bridge.js +606 -0
  220. package/dist/tui/index.js +370 -0
  221. package/dist/tui/modal-presets.js +33 -0
  222. package/dist/tui/model-spec.js +46 -0
  223. package/dist/tui/sse-parser.js +78 -0
  224. package/dist/tui/theme.js +110 -0
  225. package/dist/tui/titlebar-spinner.js +62 -0
  226. package/dist/tui/tui-app.js +311 -0
  227. package/dist/tui/ws-client.js +215 -0
  228. package/dist/twitter-verify.js +134 -0
  229. package/dist/tx-service.js +108 -0
  230. package/dist/utils/exec-safety.js +17 -0
  231. package/dist/utils/globals.js +20 -0
  232. package/dist/utils/milady-root.js +61 -0
  233. package/dist/utils/number-parsing.js +37 -0
  234. package/dist/version-resolver.js +37 -0
  235. package/dist/version.js +7 -0
  236. package/dist/wallet-routes.js +266 -0
  237. package/dist/wallet.js +568 -0
  238. package/dist/whatsapp-routes.js +182 -0
  239. package/dist/zip-utils.js +109 -0
  240. package/milady.mjs +14 -0
  241. package/package.json +111 -0
@@ -0,0 +1,7 @@
1
+ import { createRequire } from "node:module";
2
+
3
+ //#region \0rolldown/runtime.js
4
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
5
+
6
+ //#endregion
7
+ export { __require };
@@ -0,0 +1,64 @@
1
+ import { EMOTE_BY_ID } from "../emotes/catalog.js";
2
+
3
+ //#region src/actions/emote.ts
4
+ /** API port for posting emote requests. */
5
+ const API_PORT = process.env.API_PORT || process.env.SERVER_PORT || "2138";
6
+ const emoteAction = {
7
+ name: "PLAY_EMOTE",
8
+ similes: [
9
+ "EMOTE",
10
+ "ANIMATE",
11
+ "GESTURE",
12
+ "DANCE",
13
+ "WAVE",
14
+ "PLAY_ANIMATION",
15
+ "DO_EMOTE",
16
+ "PERFORM"
17
+ ],
18
+ description: "Play an emote animation on the avatar. Emotes are visual gestures or animations that express emotion or action (e.g., wave, dance, cheer).",
19
+ validate: async (_runtime, _message, _state) => {
20
+ return true;
21
+ },
22
+ handler: async (_runtime, _message, _state, options) => {
23
+ try {
24
+ const params = options?.parameters;
25
+ const emoteId = typeof params?.emote === "string" ? params.emote : void 0;
26
+ if (!emoteId) return {
27
+ text: "",
28
+ success: false
29
+ };
30
+ const emote = EMOTE_BY_ID.get(emoteId);
31
+ if (!emote) return {
32
+ text: "",
33
+ success: false
34
+ };
35
+ if (!(await fetch(`http://localhost:${API_PORT}/api/emote`, {
36
+ method: "POST",
37
+ headers: { "Content-Type": "application/json" },
38
+ body: JSON.stringify({ emoteId: emote.id })
39
+ })).ok) return {
40
+ text: "",
41
+ success: false
42
+ };
43
+ return {
44
+ text: `*${emote.name.toLowerCase()}s*`,
45
+ success: true,
46
+ data: { emoteId: emote.id }
47
+ };
48
+ } catch (_err) {
49
+ return {
50
+ text: "",
51
+ success: false
52
+ };
53
+ }
54
+ },
55
+ parameters: [{
56
+ name: "emote",
57
+ description: "The emote ID to play",
58
+ required: true,
59
+ schema: { type: "string" }
60
+ }]
61
+ };
62
+
63
+ //#endregion
64
+ export { emoteAction };
@@ -0,0 +1,81 @@
1
+ import { requestRestart } from "../runtime/restart.js";
2
+ import { logger } from "@elizaos/core";
3
+ import crypto from "node:crypto";
4
+
5
+ //#region src/actions/restart.ts
6
+ /**
7
+ * RESTART_AGENT action — gracefully restarts the agent.
8
+ *
9
+ * When triggered the action:
10
+ * 1. Persists a "Restarting…" memory so the event is visible in logs
11
+ * 2. Returns a brief restart notice to the caller
12
+ * 3. After a short delay (so the response can flush), invokes
13
+ * {@link requestRestart} which delegates to the registered
14
+ * {@link RestartHandler}.
15
+ *
16
+ * In CLI mode the default handler exits with code 75 so the runner script
17
+ * rebuilds and relaunches. In headless / Electron mode a custom handler
18
+ * performs an in-process restart (stop → re-init → hot-swap references).
19
+ *
20
+ * @module actions/restart
21
+ */
22
+ /** Small delay (ms) before restarting so the response has time to flush. */
23
+ const SHUTDOWN_DELAY_MS = 1500;
24
+ const restartAction = {
25
+ name: "RESTART_AGENT",
26
+ similes: [
27
+ "RESTART",
28
+ "REBOOT",
29
+ "RELOAD",
30
+ "REFRESH",
31
+ "RESPAWN",
32
+ "RESTART_SELF",
33
+ "REBOOT_AGENT",
34
+ "RELOAD_AGENT"
35
+ ],
36
+ description: "Restart the agent process. This stops the runtime, rebuilds if source files changed, and relaunches — picking up new code, config, or plugins.",
37
+ validate: async (_runtime, _message, _state) => {
38
+ return true;
39
+ },
40
+ handler: async (runtime, message, _state, options) => {
41
+ const params = options?.parameters;
42
+ const reason = typeof params?.reason === "string" ? params.reason : void 0;
43
+ const restartText = reason ? `Restarting… (${reason})` : "Restarting…";
44
+ logger.info(`[milady] ${restartText}`);
45
+ try {
46
+ const restartMemory = {
47
+ id: crypto.randomUUID(),
48
+ entityId: runtime.agentId,
49
+ roomId: message.roomId,
50
+ worldId: message.worldId,
51
+ content: {
52
+ text: restartText,
53
+ source: "milady",
54
+ type: "system"
55
+ }
56
+ };
57
+ await runtime.createMemory(restartMemory, "messages");
58
+ } catch (err) {
59
+ const msg = err instanceof Error ? err.message : String(err);
60
+ logger.warn(`[milady] Could not persist restart memory: ${msg}`);
61
+ }
62
+ setTimeout(() => {
63
+ requestRestart(reason);
64
+ }, SHUTDOWN_DELAY_MS);
65
+ return {
66
+ text: restartText,
67
+ success: true,
68
+ values: { restarting: true },
69
+ data: { reason }
70
+ };
71
+ },
72
+ parameters: [{
73
+ name: "reason",
74
+ description: "Optional reason for the restart (logged for diagnostics).",
75
+ required: false,
76
+ schema: { type: "string" }
77
+ }]
78
+ };
79
+
80
+ //#endregion
81
+ export { restartAction };
@@ -0,0 +1,152 @@
1
+ //#region src/actions/send-message.ts
2
+ const sendMessageAction = {
3
+ name: "SEND_MESSAGE",
4
+ similes: [
5
+ "DM",
6
+ "MESSAGE",
7
+ "SEND_DM",
8
+ "POST_MESSAGE"
9
+ ],
10
+ description: "Send a message to a user or room on a specific platform/service using explicit parameters.",
11
+ validate: async () => true,
12
+ handler: async (runtime, _message, _state, options) => {
13
+ const params = options?.parameters;
14
+ const targetType = params?.targetType === "user" || params?.targetType === "room" ? params.targetType : null;
15
+ const source = typeof params?.source === "string" ? params.source.trim() : "";
16
+ const target = typeof params?.target === "string" ? params.target.trim() : "";
17
+ const text = typeof params?.text === "string" ? params.text.trim() : "";
18
+ if (!targetType || !source || !target || !text) return {
19
+ text: "SEND_MESSAGE requires targetType, source, target, and text parameters.",
20
+ success: false,
21
+ values: {
22
+ success: false,
23
+ error: "INVALID_PARAMETERS"
24
+ },
25
+ data: {
26
+ actionName: "SEND_MESSAGE",
27
+ targetType,
28
+ source: source || null,
29
+ target: target || null
30
+ }
31
+ };
32
+ const service = runtime.getService(source);
33
+ if (!service) return {
34
+ text: `Message service '${source}' is not available.`,
35
+ success: false,
36
+ values: {
37
+ success: false,
38
+ error: "SERVICE_NOT_FOUND"
39
+ },
40
+ data: {
41
+ actionName: "SEND_MESSAGE",
42
+ targetType,
43
+ source,
44
+ target
45
+ }
46
+ };
47
+ if (targetType === "user") {
48
+ if (!service.sendDirectMessage) return {
49
+ text: `Direct messaging is not supported by '${source}'.`,
50
+ success: false,
51
+ values: {
52
+ success: false,
53
+ error: "DIRECT_MESSAGE_UNSUPPORTED"
54
+ },
55
+ data: {
56
+ actionName: "SEND_MESSAGE",
57
+ targetType,
58
+ source,
59
+ target
60
+ }
61
+ };
62
+ await service.sendDirectMessage(target, {
63
+ text,
64
+ source
65
+ });
66
+ return {
67
+ text: `Message sent to user ${target} on ${source}.`,
68
+ success: true,
69
+ values: {
70
+ success: true,
71
+ targetType,
72
+ source,
73
+ target
74
+ },
75
+ data: {
76
+ actionName: "SEND_MESSAGE",
77
+ targetType,
78
+ source,
79
+ target,
80
+ text
81
+ }
82
+ };
83
+ }
84
+ if (!service.sendRoomMessage) return {
85
+ text: `Room messaging is not supported by '${source}'.`,
86
+ success: false,
87
+ values: {
88
+ success: false,
89
+ error: "ROOM_MESSAGE_UNSUPPORTED"
90
+ },
91
+ data: {
92
+ actionName: "SEND_MESSAGE",
93
+ targetType,
94
+ source,
95
+ target
96
+ }
97
+ };
98
+ await service.sendRoomMessage(target, {
99
+ text,
100
+ source
101
+ });
102
+ return {
103
+ text: `Message sent to room ${target} on ${source}.`,
104
+ success: true,
105
+ values: {
106
+ success: true,
107
+ targetType,
108
+ source,
109
+ target
110
+ },
111
+ data: {
112
+ actionName: "SEND_MESSAGE",
113
+ targetType,
114
+ source,
115
+ target,
116
+ text
117
+ }
118
+ };
119
+ },
120
+ parameters: [
121
+ {
122
+ name: "targetType",
123
+ description: "Target entity type: user or room.",
124
+ required: true,
125
+ schema: {
126
+ type: "string",
127
+ enum: ["user", "room"]
128
+ }
129
+ },
130
+ {
131
+ name: "source",
132
+ description: "Messaging source/service name (e.g. telegram, discord).",
133
+ required: true,
134
+ schema: { type: "string" }
135
+ },
136
+ {
137
+ name: "target",
138
+ description: "Target identifier. For users: entity ID/username. For rooms: room ID/name.",
139
+ required: true,
140
+ schema: { type: "string" }
141
+ },
142
+ {
143
+ name: "text",
144
+ description: "Message text to send.",
145
+ required: true,
146
+ schema: { type: "string" }
147
+ }
148
+ ]
149
+ };
150
+
151
+ //#endregion
152
+ export { sendMessageAction };
@@ -0,0 +1,82 @@
1
+ //#region src/api/agent-admin-routes.ts
2
+ async function handleAgentAdminRoutes(ctx) {
3
+ const { res, method, pathname, state, onRestart, json, error, resolveStateDir, resolvePath, getHomeDir, isSafeResetStateDir, stateDirExists, removeStateDir, logWarn } = ctx;
4
+ if (method === "POST" && pathname === "/api/agent/restart") {
5
+ if (!onRestart) {
6
+ error(res, "Restart is not supported in this mode (no restart handler registered)", 501);
7
+ return true;
8
+ }
9
+ if (state.agentState === "restarting") {
10
+ error(res, "A restart is already in progress", 409);
11
+ return true;
12
+ }
13
+ const previousState = state.agentState;
14
+ state.agentState = "restarting";
15
+ try {
16
+ const newRuntime = await onRestart();
17
+ if (newRuntime) {
18
+ state.runtime = newRuntime;
19
+ state.chatConnectionReady = null;
20
+ state.chatConnectionPromise = null;
21
+ state.agentState = "running";
22
+ state.agentName = newRuntime.character.name ?? "Milady";
23
+ state.startedAt = Date.now();
24
+ state.pendingRestartReasons = [];
25
+ json(res, {
26
+ ok: true,
27
+ pendingRestart: false,
28
+ status: {
29
+ state: state.agentState,
30
+ agentName: state.agentName,
31
+ startedAt: state.startedAt
32
+ }
33
+ });
34
+ } else {
35
+ state.agentState = previousState;
36
+ error(res, "Restart handler returned null — runtime failed to re-initialize", 500);
37
+ }
38
+ } catch (err) {
39
+ const message = err instanceof Error ? err.message : String(err);
40
+ state.agentState = previousState;
41
+ error(res, `Restart failed: ${message}`, 500);
42
+ }
43
+ return true;
44
+ }
45
+ if (method === "POST" && pathname === "/api/agent/reset") {
46
+ try {
47
+ if (state.runtime) {
48
+ try {
49
+ await state.runtime.stop();
50
+ } catch (stopErr) {
51
+ logWarn(`[milady-api] Error stopping runtime during reset: ${stopErr instanceof Error ? stopErr.message : String(stopErr)}`);
52
+ }
53
+ state.runtime = null;
54
+ }
55
+ const resolvedState = resolvePath(resolveStateDir());
56
+ if (!isSafeResetStateDir(resolvedState, getHomeDir())) {
57
+ logWarn(`[milady-api] Refusing to delete unsafe state dir: "${resolvedState}"`);
58
+ error(res, `Reset aborted: state directory "${resolvedState}" does not appear safe to delete`, 400);
59
+ return true;
60
+ }
61
+ if (stateDirExists(resolvedState)) removeStateDir(resolvedState);
62
+ state.agentState = "stopped";
63
+ state.agentName = "Milady";
64
+ state.model = void 0;
65
+ state.startedAt = void 0;
66
+ state.config = {};
67
+ state.chatRoomId = null;
68
+ state.chatUserId = null;
69
+ state.chatConnectionReady = null;
70
+ state.chatConnectionPromise = null;
71
+ state.pendingRestartReasons = [];
72
+ json(res, { ok: true });
73
+ } catch (err) {
74
+ error(res, `Reset failed: ${err instanceof Error ? err.message : String(err)}`, 500);
75
+ }
76
+ return true;
77
+ }
78
+ return false;
79
+ }
80
+
81
+ //#endregion
82
+ export { handleAgentAdminRoutes };
@@ -0,0 +1,79 @@
1
+ import { getAutonomySvc } from "./autonomy-routes.js";
2
+
3
+ //#region src/api/agent-lifecycle-routes.ts
4
+ function detectModel(runtime) {
5
+ if (!runtime) return "unknown";
6
+ return runtime.plugins.find((plugin) => plugin.name.includes("anthropic") || plugin.name.includes("openai") || plugin.name.includes("groq"))?.name ?? "unknown";
7
+ }
8
+ async function handleAgentLifecycleRoutes(ctx) {
9
+ const { res, method, pathname, state, json } = ctx;
10
+ if (method === "POST" && pathname === "/api/agent/start") {
11
+ state.agentState = "running";
12
+ state.startedAt = Date.now();
13
+ state.model = detectModel(state.runtime);
14
+ const svc = getAutonomySvc(state.runtime);
15
+ if (svc) await svc.enableAutonomy();
16
+ json(res, {
17
+ ok: true,
18
+ status: {
19
+ state: state.agentState,
20
+ agentName: state.agentName,
21
+ model: state.model,
22
+ uptime: 0,
23
+ startedAt: state.startedAt
24
+ }
25
+ });
26
+ return true;
27
+ }
28
+ if (method === "POST" && pathname === "/api/agent/stop") {
29
+ const svc = getAutonomySvc(state.runtime);
30
+ if (svc) await svc.disableAutonomy();
31
+ state.agentState = "stopped";
32
+ state.startedAt = void 0;
33
+ state.model = void 0;
34
+ json(res, {
35
+ ok: true,
36
+ status: {
37
+ state: state.agentState,
38
+ agentName: state.agentName
39
+ }
40
+ });
41
+ return true;
42
+ }
43
+ if (method === "POST" && pathname === "/api/agent/pause") {
44
+ const svc = getAutonomySvc(state.runtime);
45
+ if (svc) await svc.disableAutonomy();
46
+ state.agentState = "paused";
47
+ json(res, {
48
+ ok: true,
49
+ status: {
50
+ state: state.agentState,
51
+ agentName: state.agentName,
52
+ model: state.model,
53
+ uptime: state.startedAt ? Date.now() - state.startedAt : void 0,
54
+ startedAt: state.startedAt
55
+ }
56
+ });
57
+ return true;
58
+ }
59
+ if (method === "POST" && pathname === "/api/agent/resume") {
60
+ const svc = getAutonomySvc(state.runtime);
61
+ if (svc) await svc.enableAutonomy();
62
+ state.agentState = "running";
63
+ json(res, {
64
+ ok: true,
65
+ status: {
66
+ state: state.agentState,
67
+ agentName: state.agentName,
68
+ model: state.model,
69
+ uptime: state.startedAt ? Date.now() - state.startedAt : void 0,
70
+ startedAt: state.startedAt
71
+ }
72
+ });
73
+ return true;
74
+ }
75
+ return false;
76
+ }
77
+
78
+ //#endregion
79
+ export { handleAgentLifecycleRoutes };
@@ -0,0 +1,102 @@
1
+ import { AgentExportError, estimateExportSize, exportAgent, importAgent } from "./services/agent-export.js";
2
+ import { readRequestBodyBuffer } from "./http-helpers.js";
3
+
4
+ //#region src/api/agent-transfer-routes.ts
5
+ const MAX_IMPORT_BYTES = 512 * 1048576;
6
+ const AGENT_TRANSFER_MIN_PASSWORD_LENGTH = 4;
7
+ const AGENT_TRANSFER_MAX_PASSWORD_LENGTH = 1024;
8
+ function readRawBody(req, maxBytes) {
9
+ return readRequestBodyBuffer(req, { maxBytes }).then((body) => {
10
+ if (body === null) throw new Error(`Request body exceeds maximum size (${maxBytes} bytes)`);
11
+ return body;
12
+ });
13
+ }
14
+ async function handleAgentTransferRoutes(ctx) {
15
+ const { req, res, method, pathname, state, readJsonBody, json, error } = ctx;
16
+ if (method === "POST" && pathname === "/api/agent/export") {
17
+ if (!state.runtime) {
18
+ error(res, "Agent is not running — start it before exporting.", 503);
19
+ return true;
20
+ }
21
+ const body = await readJsonBody(req, res);
22
+ if (!body) return true;
23
+ if (!body.password || typeof body.password !== "string") {
24
+ error(res, `A password of at least ${AGENT_TRANSFER_MIN_PASSWORD_LENGTH} characters is required.`, 400);
25
+ return true;
26
+ }
27
+ if (body.password.length < AGENT_TRANSFER_MIN_PASSWORD_LENGTH) {
28
+ error(res, `A password of at least ${AGENT_TRANSFER_MIN_PASSWORD_LENGTH} characters is required.`, 400);
29
+ return true;
30
+ }
31
+ try {
32
+ const fileBuffer = await exportAgent(state.runtime, body.password, { includeLogs: body.includeLogs === true });
33
+ const filename = `${(state.runtime.character.name ?? "agent").replace(/[^a-zA-Z0-9_-]/g, "_").toLowerCase()}-${(/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-").slice(0, 19)}.eliza-agent`;
34
+ res.statusCode = 200;
35
+ res.setHeader("Content-Type", "application/octet-stream");
36
+ res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
37
+ res.setHeader("Content-Length", fileBuffer.length);
38
+ res.end(fileBuffer);
39
+ } catch (err) {
40
+ const message = err instanceof Error ? err.message : String(err);
41
+ if (err instanceof AgentExportError) error(res, message, 400);
42
+ else error(res, `Export failed: ${message}`, 500);
43
+ }
44
+ return true;
45
+ }
46
+ if (method === "GET" && pathname === "/api/agent/export/estimate") {
47
+ if (!state.runtime) {
48
+ error(res, "Agent is not running.", 503);
49
+ return true;
50
+ }
51
+ try {
52
+ json(res, await estimateExportSize(state.runtime));
53
+ } catch (err) {
54
+ error(res, `Estimate failed: ${err instanceof Error ? err.message : String(err)}`, 500);
55
+ }
56
+ return true;
57
+ }
58
+ if (method === "POST" && pathname === "/api/agent/import") {
59
+ if (!state.runtime) {
60
+ error(res, "Agent is not running — start it before importing.", 503);
61
+ return true;
62
+ }
63
+ let rawBody;
64
+ try {
65
+ rawBody = await readRawBody(req, MAX_IMPORT_BYTES);
66
+ } catch (err) {
67
+ error(res, err instanceof Error ? err.message : String(err), 413);
68
+ return true;
69
+ }
70
+ if (rawBody.length < 5) {
71
+ error(res, "Request body is too small — expected password + file data.", 400);
72
+ return true;
73
+ }
74
+ const passwordLength = rawBody.readUInt32BE(0);
75
+ if (passwordLength < AGENT_TRANSFER_MIN_PASSWORD_LENGTH) {
76
+ error(res, `Password must be at least ${AGENT_TRANSFER_MIN_PASSWORD_LENGTH} characters.`, 400);
77
+ return true;
78
+ }
79
+ if (passwordLength > AGENT_TRANSFER_MAX_PASSWORD_LENGTH) {
80
+ error(res, `Password is too long (max ${AGENT_TRANSFER_MAX_PASSWORD_LENGTH} bytes).`, 400);
81
+ return true;
82
+ }
83
+ if (rawBody.length < 4 + passwordLength + 1) {
84
+ error(res, "Request body is incomplete — missing file data after password.", 400);
85
+ return true;
86
+ }
87
+ const password = rawBody.subarray(4, 4 + passwordLength).toString("utf-8");
88
+ const fileBuffer = rawBody.subarray(4 + passwordLength);
89
+ try {
90
+ json(res, await importAgent(state.runtime, fileBuffer, password));
91
+ } catch (err) {
92
+ const message = err instanceof Error ? err.message : String(err);
93
+ if (err instanceof AgentExportError) error(res, message, 400);
94
+ else error(res, `Import failed: ${message}`, 500);
95
+ }
96
+ return true;
97
+ }
98
+ return false;
99
+ }
100
+
101
+ //#endregion
102
+ export { handleAgentTransferRoutes };
@@ -0,0 +1,82 @@
1
+ //#region src/api/agent-admin-routes.ts
2
+ async function handleAgentAdminRoutes(ctx) {
3
+ const { res, method, pathname, state, onRestart, json, error, resolveStateDir, resolvePath, getHomeDir, isSafeResetStateDir, stateDirExists, removeStateDir, logWarn } = ctx;
4
+ if (method === "POST" && pathname === "/api/agent/restart") {
5
+ if (!onRestart) {
6
+ error(res, "Restart is not supported in this mode (no restart handler registered)", 501);
7
+ return true;
8
+ }
9
+ if (state.agentState === "restarting") {
10
+ error(res, "A restart is already in progress", 409);
11
+ return true;
12
+ }
13
+ const previousState = state.agentState;
14
+ state.agentState = "restarting";
15
+ try {
16
+ const newRuntime = await onRestart();
17
+ if (newRuntime) {
18
+ state.runtime = newRuntime;
19
+ state.chatConnectionReady = null;
20
+ state.chatConnectionPromise = null;
21
+ state.agentState = "running";
22
+ state.agentName = newRuntime.character.name ?? "Milady";
23
+ state.startedAt = Date.now();
24
+ state.pendingRestartReasons = [];
25
+ json(res, {
26
+ ok: true,
27
+ pendingRestart: false,
28
+ status: {
29
+ state: state.agentState,
30
+ agentName: state.agentName,
31
+ startedAt: state.startedAt
32
+ }
33
+ });
34
+ } else {
35
+ state.agentState = previousState;
36
+ error(res, "Restart handler returned null — runtime failed to re-initialize", 500);
37
+ }
38
+ } catch (err) {
39
+ const message = err instanceof Error ? err.message : String(err);
40
+ state.agentState = previousState;
41
+ error(res, `Restart failed: ${message}`, 500);
42
+ }
43
+ return true;
44
+ }
45
+ if (method === "POST" && pathname === "/api/agent/reset") {
46
+ try {
47
+ if (state.runtime) {
48
+ try {
49
+ await state.runtime.stop();
50
+ } catch (stopErr) {
51
+ logWarn(`[milady-api] Error stopping runtime during reset: ${stopErr instanceof Error ? stopErr.message : String(stopErr)}`);
52
+ }
53
+ state.runtime = null;
54
+ }
55
+ const resolvedState = resolvePath(resolveStateDir());
56
+ if (!isSafeResetStateDir(resolvedState, getHomeDir())) {
57
+ logWarn(`[milady-api] Refusing to delete unsafe state dir: "${resolvedState}"`);
58
+ error(res, `Reset aborted: state directory "${resolvedState}" does not appear safe to delete`, 400);
59
+ return true;
60
+ }
61
+ if (stateDirExists(resolvedState)) removeStateDir(resolvedState);
62
+ state.agentState = "stopped";
63
+ state.agentName = "Milady";
64
+ state.model = void 0;
65
+ state.startedAt = void 0;
66
+ state.config = {};
67
+ state.chatRoomId = null;
68
+ state.chatUserId = null;
69
+ state.chatConnectionReady = null;
70
+ state.chatConnectionPromise = null;
71
+ state.pendingRestartReasons = [];
72
+ json(res, { ok: true });
73
+ } catch (err) {
74
+ error(res, `Reset failed: ${err instanceof Error ? err.message : String(err)}`, 500);
75
+ }
76
+ return true;
77
+ }
78
+ return false;
79
+ }
80
+
81
+ //#endregion
82
+ export { handleAgentAdminRoutes };