@downcity/agent 1.1.43 → 1.1.51

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 (171) hide show
  1. package/README.md +6 -8
  2. package/bin/agent/Agent.d.ts +31 -16
  3. package/bin/agent/Agent.d.ts.map +1 -1
  4. package/bin/agent/Agent.js +47 -63
  5. package/bin/agent/Agent.js.map +1 -1
  6. package/bin/agent/RemoteAgent.d.ts +6 -6
  7. package/bin/agent/RemoteAgent.d.ts.map +1 -1
  8. package/bin/agent/RemoteAgent.js +377 -267
  9. package/bin/agent/RemoteAgent.js.map +1 -1
  10. package/bin/config/AgentInitializer.d.ts +4 -4
  11. package/bin/config/AgentInitializer.d.ts.map +1 -1
  12. package/bin/config/AgentInitializer.js +14 -12
  13. package/bin/config/AgentInitializer.js.map +1 -1
  14. package/bin/config/Config.d.ts +9 -30
  15. package/bin/config/Config.d.ts.map +1 -1
  16. package/bin/config/Config.js +23 -70
  17. package/bin/config/Config.js.map +1 -1
  18. package/bin/config/Defaults.d.ts.map +1 -1
  19. package/bin/config/Defaults.js +1 -9
  20. package/bin/config/Defaults.js.map +1 -1
  21. package/bin/config/DowncitySchema.d.ts.map +1 -1
  22. package/bin/config/DowncitySchema.js +3 -111
  23. package/bin/config/DowncitySchema.js.map +1 -1
  24. package/bin/config/ExecutionBinding.d.ts +0 -15
  25. package/bin/config/ExecutionBinding.d.ts.map +1 -1
  26. package/bin/config/ExecutionBinding.js +1 -21
  27. package/bin/config/ExecutionBinding.js.map +1 -1
  28. package/bin/config/Paths.d.ts +4 -43
  29. package/bin/config/Paths.d.ts.map +1 -1
  30. package/bin/config/Paths.js +10 -30
  31. package/bin/config/Paths.js.map +1 -1
  32. package/bin/config/PlatformPaths.d.ts +0 -4
  33. package/bin/config/PlatformPaths.d.ts.map +1 -1
  34. package/bin/config/PlatformPaths.js +5 -1
  35. package/bin/config/PlatformPaths.js.map +1 -1
  36. package/bin/executor/composer/system/default/assets/init/PROFILE.md.d.ts +1 -1
  37. package/bin/executor/composer/system/default/assets/init/PROFILE.md.d.ts.map +1 -1
  38. package/bin/executor/composer/system/default/assets/init/PROFILE.md.js +1 -1
  39. package/bin/executor/composer/system/default/assets/init/PROFILE.md.js.map +1 -1
  40. package/bin/executor/tools/shell/ShellToolFormatting.js +2 -2
  41. package/bin/executor/tools/shell/ShellToolFormatting.js.map +1 -1
  42. package/bin/executor/types/SessionRun.d.ts +1 -1
  43. package/bin/index.d.ts +4 -6
  44. package/bin/index.d.ts.map +1 -1
  45. package/bin/index.js +5 -5
  46. package/bin/index.js.map +1 -1
  47. package/bin/plugin/core/PluginCommandRequest.d.ts +1 -1
  48. package/bin/plugin/core/PluginCommandRequest.js +1 -1
  49. package/bin/plugin/core/PluginLocalExecution.d.ts.map +1 -1
  50. package/bin/plugin/core/PluginLocalExecution.js +4 -8
  51. package/bin/plugin/core/PluginLocalExecution.js.map +1 -1
  52. package/bin/plugin/types/PluginApi.d.ts +1 -1
  53. package/bin/rpc/Client.d.ts +104 -0
  54. package/bin/rpc/Client.d.ts.map +1 -0
  55. package/bin/rpc/Client.js +300 -0
  56. package/bin/rpc/Client.js.map +1 -0
  57. package/bin/rpc/Server.d.ts +41 -0
  58. package/bin/rpc/Server.d.ts.map +1 -0
  59. package/bin/rpc/Server.js +164 -0
  60. package/bin/rpc/Server.js.map +1 -0
  61. package/bin/runtime/host/daemon/Api.d.ts +1 -1
  62. package/bin/runtime/host/daemon/Client.d.ts +1 -1
  63. package/bin/runtime/host/daemon/Client.d.ts.map +1 -1
  64. package/bin/runtime/host/daemon/Client.js +30 -20
  65. package/bin/runtime/host/daemon/Client.js.map +1 -1
  66. package/bin/runtime/server/http/control/OverviewRoutes.js +1 -1
  67. package/bin/runtime/server/http/control/OverviewRoutes.js.map +1 -1
  68. package/bin/session/index.d.ts +1 -1
  69. package/bin/session/index.d.ts.map +1 -1
  70. package/bin/session/index.js +1 -1
  71. package/bin/session/index.js.map +1 -1
  72. package/bin/session/storage/Paths.d.ts +0 -4
  73. package/bin/session/storage/Paths.d.ts.map +1 -1
  74. package/bin/session/storage/Paths.js +0 -6
  75. package/bin/session/storage/Paths.js.map +1 -1
  76. package/bin/types/agent/AgentTypes.d.ts +23 -52
  77. package/bin/types/agent/AgentTypes.d.ts.map +1 -1
  78. package/bin/types/config/AgentProject.d.ts +6 -5
  79. package/bin/types/config/AgentProject.d.ts.map +1 -1
  80. package/bin/types/config/DowncityConfig.d.ts +8 -66
  81. package/bin/types/config/DowncityConfig.d.ts.map +1 -1
  82. package/bin/types/config/LlmConfig.d.ts +1 -1
  83. package/bin/types/config/Start.d.ts +3 -0
  84. package/bin/types/config/Start.d.ts.map +1 -1
  85. package/bin/types/config/Start.js +2 -0
  86. package/bin/types/config/Start.js.map +1 -1
  87. package/bin/types/runtime/host/Store.d.ts +7 -50
  88. package/bin/types/runtime/host/Store.d.ts.map +1 -1
  89. package/bin/types/runtime/platform/Platform.d.ts +6 -6
  90. package/bin/types/runtime/platform/Platform.d.ts.map +1 -1
  91. package/bin/types/runtime/platform/PlatformGateway.d.ts +2 -2
  92. package/bin/types/runtime/platform/PlatformGateway.d.ts.map +1 -1
  93. package/bin/utils/Time.d.ts +0 -1
  94. package/bin/utils/Time.d.ts.map +1 -1
  95. package/bin/utils/Time.js +0 -7
  96. package/bin/utils/Time.js.map +1 -1
  97. package/bin/utils/storage/index.d.ts +0 -1
  98. package/bin/utils/storage/index.d.ts.map +1 -1
  99. package/bin/utils/storage/index.js +0 -6
  100. package/bin/utils/storage/index.js.map +1 -1
  101. package/package.json +4 -6
  102. package/src/agent/Agent.ts +54 -71
  103. package/src/agent/RemoteAgent.ts +515 -345
  104. package/src/config/AgentInitializer.ts +14 -12
  105. package/src/config/Config.ts +28 -85
  106. package/src/config/Defaults.ts +1 -9
  107. package/src/config/DowncitySchema.ts +3 -113
  108. package/src/config/ExecutionBinding.ts +1 -23
  109. package/src/config/Paths.ts +10 -43
  110. package/src/config/PlatformPaths.ts +5 -1
  111. package/src/executor/composer/system/default/assets/init/PROFILE.md.ts +1 -1
  112. package/src/executor/composer/system/default/assets/init/PROFILE.md.ts.txt +1 -2
  113. package/src/executor/tools/shell/ShellToolFormatting.ts +2 -2
  114. package/src/executor/types/SessionRun.ts +1 -1
  115. package/src/index.ts +7 -14
  116. package/src/plugin/core/PluginCommandRequest.ts +1 -1
  117. package/src/plugin/core/PluginLocalExecution.ts +4 -8
  118. package/src/plugin/types/PluginApi.ts +1 -1
  119. package/src/rpc/Client.ts +467 -0
  120. package/src/rpc/Server.ts +302 -0
  121. package/src/runtime/host/daemon/Api.ts +1 -1
  122. package/src/runtime/host/daemon/Client.ts +44 -22
  123. package/src/runtime/server/http/control/OverviewRoutes.ts +1 -1
  124. package/src/session/index.ts +0 -1
  125. package/src/session/storage/Paths.ts +0 -10
  126. package/src/types/agent/AgentTypes.ts +23 -57
  127. package/src/types/config/AgentProject.ts +6 -5
  128. package/src/types/config/DowncityConfig.ts +8 -67
  129. package/src/types/config/LlmConfig.ts +1 -1
  130. package/src/types/config/Start.ts +3 -0
  131. package/src/types/runtime/host/Store.ts +7 -53
  132. package/src/types/runtime/platform/Platform.ts +6 -6
  133. package/src/types/runtime/platform/PlatformGateway.ts +2 -2
  134. package/src/utils/Time.ts +0 -6
  135. package/src/utils/storage/index.ts +0 -7
  136. package/tsconfig.tsbuildinfo +1 -1
  137. package/bin/config/ConfigEnvResolver.d.ts +0 -22
  138. package/bin/config/ConfigEnvResolver.d.ts.map +0 -1
  139. package/bin/config/ConfigEnvResolver.js +0 -41
  140. package/bin/config/ConfigEnvResolver.js.map +0 -1
  141. package/bin/runtime/server/rpc/Server.d.ts +0 -18
  142. package/bin/runtime/server/rpc/Server.d.ts.map +0 -1
  143. package/bin/runtime/server/rpc/Server.js +0 -315
  144. package/bin/runtime/server/rpc/Server.js.map +0 -1
  145. package/bin/runtime/transport/rpc/Client.d.ts +0 -13
  146. package/bin/runtime/transport/rpc/Client.d.ts.map +0 -1
  147. package/bin/runtime/transport/rpc/Client.js +0 -98
  148. package/bin/runtime/transport/rpc/Client.js.map +0 -1
  149. package/bin/runtime/transport/rpc/Paths.d.ts +0 -14
  150. package/bin/runtime/transport/rpc/Paths.d.ts.map +0 -1
  151. package/bin/runtime/transport/rpc/Paths.js +0 -42
  152. package/bin/runtime/transport/rpc/Paths.js.map +0 -1
  153. package/bin/runtime/transport/rpc/Transport.d.ts +0 -21
  154. package/bin/runtime/transport/rpc/Transport.d.ts.map +0 -1
  155. package/bin/runtime/transport/rpc/Transport.js +0 -30
  156. package/bin/runtime/transport/rpc/Transport.js.map +0 -1
  157. package/bin/types/common/ResolvedConfigValue.d.ts +0 -12
  158. package/bin/types/common/ResolvedConfigValue.d.ts.map +0 -1
  159. package/bin/types/common/ResolvedConfigValue.js +0 -2
  160. package/bin/types/common/ResolvedConfigValue.js.map +0 -1
  161. package/bin/types/runtime/rpc/LocalRpc.d.ts +0 -69
  162. package/bin/types/runtime/rpc/LocalRpc.d.ts.map +0 -1
  163. package/bin/types/runtime/rpc/LocalRpc.js +0 -9
  164. package/bin/types/runtime/rpc/LocalRpc.js.map +0 -1
  165. package/src/config/ConfigEnvResolver.ts +0 -52
  166. package/src/runtime/server/rpc/Server.ts +0 -408
  167. package/src/runtime/transport/rpc/Client.ts +0 -113
  168. package/src/runtime/transport/rpc/Paths.ts +0 -50
  169. package/src/runtime/transport/rpc/Transport.ts +0 -43
  170. package/src/types/common/ResolvedConfigValue.ts +0 -16
  171. package/src/types/runtime/rpc/LocalRpc.ts +0 -72
@@ -0,0 +1,302 @@
1
+ /**
2
+ * Agent 本机 RPC Server。
3
+ *
4
+ * 职责说明(中文)
5
+ * - 为本机 `RemoteAgent(rpc://...)` 提供最小 SDK 会话访问面。
6
+ * - 当前只承载 Session actor 所需方法,不混入控制台 HTTP 语义。
7
+ * - 协议使用逐行 JSON(NDJSON),便于调试与事件流推送。
8
+ */
9
+
10
+ import net from "node:net";
11
+ import type {
12
+ AgentListSessionsInput,
13
+ AgentSessionCollection,
14
+ } from "@/types/agent/AgentTypes.js";
15
+ import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js";
16
+ import type { AgentSessionEvent } from "@/types/sdk/AgentSessionEvent.js";
17
+
18
+ type RpcSessionRequest =
19
+ | {
20
+ id: string;
21
+ method: "sdk.sessions.list";
22
+ params?: AgentListSessionsInput;
23
+ }
24
+ | {
25
+ id: string;
26
+ method: "sdk.sessions.create";
27
+ params?: {
28
+ sessionId?: string;
29
+ };
30
+ }
31
+ | {
32
+ id: string;
33
+ method: "sdk.sessions.get";
34
+ params: {
35
+ sessionId: string;
36
+ };
37
+ }
38
+ | {
39
+ id: string;
40
+ method: "sdk.sessions.prompt";
41
+ params: {
42
+ sessionId: string;
43
+ input: AgentSessionPromptInput;
44
+ };
45
+ }
46
+ | {
47
+ id: string;
48
+ method: "sdk.sessions.history";
49
+ params: {
50
+ sessionId: string;
51
+ input?: {
52
+ limit?: number;
53
+ cursor?: string;
54
+ order?: "asc" | "desc";
55
+ view?: "message" | "timeline";
56
+ };
57
+ };
58
+ }
59
+ | {
60
+ id: string;
61
+ method: "sdk.sessions.system";
62
+ params: {
63
+ sessionId: string;
64
+ };
65
+ }
66
+ | {
67
+ id: string;
68
+ method: "sdk.sessions.fork";
69
+ params: {
70
+ sessionId: string;
71
+ messageId?: string;
72
+ };
73
+ }
74
+ | {
75
+ id: string;
76
+ method: "sdk.sessions.subscribe";
77
+ params: {
78
+ sessionId: string;
79
+ };
80
+ }
81
+ | {
82
+ id: string;
83
+ method: "sdk.sessions.unsubscribe";
84
+ params: {
85
+ subscriptionId: string;
86
+ };
87
+ };
88
+
89
+ type RpcSuccessFrame = {
90
+ id: string;
91
+ success: true;
92
+ data?: unknown;
93
+ };
94
+
95
+ type RpcErrorFrame = {
96
+ id: string;
97
+ success: false;
98
+ error: string;
99
+ };
100
+
101
+ type RpcEventFrame = {
102
+ type: "event";
103
+ subscriptionId: string;
104
+ event: AgentSessionEvent;
105
+ };
106
+
107
+ type SocketSubscription = {
108
+ sessionId: string;
109
+ unsubscribe: () => void;
110
+ };
111
+
112
+ /**
113
+ * RPC Server 启动参数。
114
+ */
115
+ export interface RpcServerStartOptions {
116
+ /** RPC 服务监听端口。 */
117
+ port: number;
118
+ /** RPC 服务监听主机。 */
119
+ host: string;
120
+ /** Session 集合访问口。 */
121
+ sessionCollection: AgentSessionCollection;
122
+ }
123
+
124
+ /**
125
+ * RPC Server 运行实例。
126
+ */
127
+ export interface RpcServerInstance {
128
+ /** 当前监听 host。 */
129
+ host: string;
130
+ /** 当前监听 port。 */
131
+ port: number;
132
+ /** 当前访问 URL。 */
133
+ url: string;
134
+ /** 原生 net server。 */
135
+ server: net.Server;
136
+ /** 停止当前服务。 */
137
+ stop(): Promise<void>;
138
+ }
139
+
140
+ /**
141
+ * 启动 Agent 本机 RPC 服务。
142
+ */
143
+ export async function startRpcServer(
144
+ options: RpcServerStartOptions,
145
+ ): Promise<RpcServerInstance> {
146
+ const server = net.createServer((socket) => {
147
+ const subscriptions = new Map<string, SocketSubscription>();
148
+ let buffered = "";
149
+
150
+ const cleanupSubscriptions = (): void => {
151
+ for (const subscription of subscriptions.values()) {
152
+ subscription.unsubscribe();
153
+ }
154
+ subscriptions.clear();
155
+ };
156
+
157
+ const writeFrame = (frame: RpcSuccessFrame | RpcErrorFrame | RpcEventFrame): void => {
158
+ socket.write(`${JSON.stringify(frame)}\n`);
159
+ };
160
+
161
+ const writeSuccess = (id: string, data?: unknown): void => {
162
+ writeFrame({
163
+ id,
164
+ success: true,
165
+ ...(data === undefined ? {} : { data }),
166
+ });
167
+ };
168
+
169
+ const writeError = (id: string, error: unknown): void => {
170
+ writeFrame({
171
+ id,
172
+ success: false,
173
+ error: error instanceof Error ? error.message : String(error),
174
+ });
175
+ };
176
+
177
+ const handleRequest = async (request: RpcSessionRequest): Promise<void> => {
178
+ try {
179
+ switch (request.method) {
180
+ case "sdk.sessions.list": {
181
+ const page = await options.sessionCollection.listSessions(request.params);
182
+ writeSuccess(request.id, { page });
183
+ return;
184
+ }
185
+ case "sdk.sessions.create": {
186
+ const session = await options.sessionCollection.createSession(request.params);
187
+ writeSuccess(request.id, { session: await session.getInfo() });
188
+ return;
189
+ }
190
+ case "sdk.sessions.get": {
191
+ const session = await options.sessionCollection.getSession(request.params.sessionId);
192
+ writeSuccess(request.id, { session: await session.getInfo() });
193
+ return;
194
+ }
195
+ case "sdk.sessions.prompt": {
196
+ const session = await options.sessionCollection.getSession(request.params.sessionId);
197
+ const turn = await session.prompt(request.params.input);
198
+ writeSuccess(request.id, { turn: { id: turn.id } });
199
+ return;
200
+ }
201
+ case "sdk.sessions.history": {
202
+ const session = await options.sessionCollection.getSession(request.params.sessionId);
203
+ const history = await session.history(request.params.input);
204
+ writeSuccess(request.id, { history });
205
+ return;
206
+ }
207
+ case "sdk.sessions.system": {
208
+ const session = await options.sessionCollection.getSession(request.params.sessionId);
209
+ writeSuccess(request.id, { system: await session.system() });
210
+ return;
211
+ }
212
+ case "sdk.sessions.fork": {
213
+ const session = await options.sessionCollection.getSession(request.params.sessionId);
214
+ const forked = await session.fork(request.params.messageId);
215
+ writeSuccess(request.id, { session: await forked.getInfo() });
216
+ return;
217
+ }
218
+ case "sdk.sessions.subscribe": {
219
+ const session = await options.sessionCollection.getSession(request.params.sessionId);
220
+ const subscriptionId = `${request.params.sessionId}:${Date.now()}:${Math.random().toString(36).slice(2, 10)}`;
221
+ const unsubscribe = session.subscribe((event) => {
222
+ writeFrame({
223
+ type: "event",
224
+ subscriptionId,
225
+ event,
226
+ });
227
+ });
228
+ subscriptions.set(subscriptionId, {
229
+ sessionId: request.params.sessionId,
230
+ unsubscribe,
231
+ });
232
+ writeSuccess(request.id, { subscriptionId });
233
+ return;
234
+ }
235
+ case "sdk.sessions.unsubscribe": {
236
+ const subscription = subscriptions.get(request.params.subscriptionId);
237
+ if (subscription) {
238
+ subscription.unsubscribe();
239
+ subscriptions.delete(request.params.subscriptionId);
240
+ }
241
+ writeSuccess(request.id, { unsubscribed: true });
242
+ return;
243
+ }
244
+ }
245
+ } catch (error) {
246
+ writeError(request.id, error);
247
+ }
248
+ };
249
+
250
+ socket.on("data", (chunk) => {
251
+ buffered += chunk.toString("utf8");
252
+ let newlineIndex = buffered.indexOf("\n");
253
+ while (newlineIndex >= 0) {
254
+ const line = buffered.slice(0, newlineIndex).trim();
255
+ buffered = buffered.slice(newlineIndex + 1);
256
+ if (line) {
257
+ try {
258
+ const parsed = JSON.parse(line) as RpcSessionRequest;
259
+ void handleRequest(parsed);
260
+ } catch (error) {
261
+ writeFrame({
262
+ id: "parse",
263
+ success: false,
264
+ error: error instanceof Error ? error.message : String(error),
265
+ });
266
+ }
267
+ }
268
+ newlineIndex = buffered.indexOf("\n");
269
+ }
270
+ });
271
+
272
+ socket.on("error", () => {
273
+ cleanupSubscriptions();
274
+ });
275
+ socket.on("close", () => {
276
+ cleanupSubscriptions();
277
+ });
278
+ socket.on("end", () => {
279
+ cleanupSubscriptions();
280
+ });
281
+ });
282
+
283
+ await new Promise<void>((resolve, reject) => {
284
+ server.once("error", reject);
285
+ server.listen(options.port, options.host, () => {
286
+ server.off("error", reject);
287
+ resolve();
288
+ });
289
+ });
290
+
291
+ return {
292
+ host: options.host,
293
+ port: options.port,
294
+ url: `rpc://${options.host}:${options.port}`,
295
+ server,
296
+ async stop(): Promise<void> {
297
+ await new Promise<void>((resolve) => {
298
+ server.close(() => resolve());
299
+ });
300
+ },
301
+ };
302
+ }
@@ -39,7 +39,7 @@ export type DaemonJsonApiCallParams = {
39
39
  * 单次调用超时(毫秒)。
40
40
  *
41
41
  * 说明(中文)
42
- * - 仅本地 IPC transport 当前会消费该参数。
42
+ * - HTTP daemon client 可消费该参数。
43
43
  * - 用于 `session execute`、`plugin command` 这类可能持续几十秒的请求,
44
44
  * 避免被默认短超时误判成“Agent server 未启动”。
45
45
  */
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * 关键点(中文)
5
5
  * - 业务模块统一通过 daemon API 与运行时通信。
6
- * - 地址解析优先级:CLI 参数 > 环境变量 > daemon meta > 默认值。
6
+ * - HTTP gateway 与本机 RPC 的地址解析分开,避免端口语义混淆。
7
7
  */
8
8
 
9
9
  import fs from "fs-extra";
@@ -62,29 +62,27 @@ function pickArgValue(args: string[], key: string): string | undefined {
62
62
  return next || undefined;
63
63
  }
64
64
 
65
- /**
66
- * 解析 daemon endpoint。
67
- *
68
- * 优先级(中文)
69
- * 1) 显式入参 `host/port`
70
- * 2) 环境变量 `DC_SERVER_*` / `DC_CTX_SERVER_*`
71
- * 3) daemon meta args(`downcity.daemon.json`)
72
- * 4) 默认 `127.0.0.1:5314`
73
- */
74
- function resolveDaemonEndpoint(params: {
65
+ type ResolveDaemonEndpointParams = {
75
66
  projectRoot: string;
76
67
  host?: string;
77
68
  port?: number;
69
+ };
70
+
71
+ function resolveDaemonEndpointFromSources(params: {
72
+ projectRoot: string;
73
+ explicit_host?: string;
74
+ explicit_port?: number;
75
+ env_host_name: string;
76
+ env_port_name: string;
77
+ arg_port_name: string;
78
+ default_host: string;
79
+ default_port: number;
78
80
  }): DaemonEndpoint {
79
- const explicitHost = normalizeHost(params.host);
80
- const explicitPort = parsePortLike(params.port);
81
+ const explicitHost = normalizeHost(params.explicit_host);
82
+ const explicitPort = parsePortLike(params.explicit_port);
81
83
 
82
- const envHost =
83
- normalizeHost(process.env.DC_SERVER_HOST) ||
84
- normalizeHost(process.env.DC_CTX_SERVER_HOST);
85
- const envPort =
86
- parsePortLike(process.env.DC_SERVER_PORT) ||
87
- parsePortLike(process.env.DC_CTX_SERVER_PORT);
84
+ const envHost = normalizeHost(process.env[params.env_host_name]);
85
+ const envPort = parsePortLike(process.env[params.env_port_name]);
88
86
 
89
87
  let daemonArgHost: string | undefined;
90
88
  let daemonArgPort: number | undefined;
@@ -96,14 +94,14 @@ function resolveDaemonEndpoint(params: {
96
94
  ? raw.args.map((item) => String(item))
97
95
  : [];
98
96
  daemonArgHost = normalizeHost(pickArgValue(args, "--host"));
99
- daemonArgPort = parsePortLike(pickArgValue(args, "--port"));
97
+ daemonArgPort = parsePortLike(pickArgValue(args, params.arg_port_name));
100
98
  }
101
99
  } catch {
102
100
  // ignore daemon meta errors, fallback to other sources
103
101
  }
104
102
 
105
- const host = explicitHost || envHost || daemonArgHost || "127.0.0.1";
106
- const port = explicitPort || envPort || daemonArgPort || 5314;
103
+ const host = explicitHost || envHost || daemonArgHost || params.default_host;
104
+ const port = explicitPort || envPort || daemonArgPort || params.default_port;
107
105
 
108
106
  return {
109
107
  host,
@@ -112,6 +110,30 @@ function resolveDaemonEndpoint(params: {
112
110
  };
113
111
  }
114
112
 
113
+ /**
114
+ * 解析 daemon endpoint。
115
+ *
116
+ * 优先级(中文)
117
+ * 1) 显式入参 `host/port`
118
+ * 2) 环境变量 `DC_CITY_HOST/DC_CITY_PORT`
119
+ * 3) daemon meta args(`downcity.daemon.json`)
120
+ * 4) 默认 `127.0.0.1:5314`
121
+ */
122
+ function resolveDaemonEndpoint(
123
+ params: ResolveDaemonEndpointParams,
124
+ ): DaemonEndpoint {
125
+ return resolveDaemonEndpointFromSources({
126
+ projectRoot: params.projectRoot,
127
+ explicit_host: params.host,
128
+ explicit_port: params.port,
129
+ env_host_name: "DC_CITY_HOST",
130
+ env_port_name: "DC_CITY_PORT",
131
+ arg_port_name: "--port",
132
+ default_host: "127.0.0.1",
133
+ default_port: 5314,
134
+ });
135
+ }
136
+
115
137
  /**
116
138
  * 调用 daemon JSON API。
117
139
  *
@@ -82,7 +82,7 @@ export function registerControlOverviewRoutes(
82
82
  cityVersion: DC_VERSION,
83
83
  now: new Date().toISOString(),
84
84
  agent: {
85
- name: runtime.config.name,
85
+ id: runtime.config.id,
86
86
  status: "running",
87
87
  },
88
88
  sessions: {
@@ -10,7 +10,6 @@ export { Session } from "./Session.js";
10
10
 
11
11
  export {
12
12
  getSdkAgentDirPath,
13
- getSdkAgentRpcEndpointPath,
14
13
  getSdkAgentSessionArchiveDirPath,
15
14
  getSdkAgentSessionDirPath,
16
15
  getSdkAgentSessionMessagesDirPath,
@@ -114,13 +114,3 @@ export function getSdkAgentSessionArchiveDirPath(
114
114
  "archive",
115
115
  );
116
116
  }
117
-
118
- /**
119
- * 单个 agent 的本地 RPC socket 路径。
120
- */
121
- export function getSdkAgentRpcEndpointPath(
122
- projectRoot: string,
123
- agentId: string,
124
- ): string {
125
- return path.join(getSdkAgentDirPath(projectRoot, agentId), "agent.rpc.sock");
126
- }
@@ -9,8 +9,7 @@
9
9
 
10
10
  import type { LanguageModel, Tool } from "ai";
11
11
  import type { BasePlugin } from "@/plugin/core/BasePlugin.js";
12
- import type { LocalRpcServerHandle } from "@/types/runtime/rpc/LocalRpc.js";
13
- import type { ServerInstance } from "@/runtime/server/http/Server.js";
12
+ import type { RpcServerInstance } from "@/rpc/Server.js";
14
13
  import type { SessionMessageV1 } from "@/executor/types/SessionMessages.js";
15
14
  import type {
16
15
  AgentSessionSubscriber,
@@ -77,12 +76,12 @@ export interface AgentOptions {
77
76
  plugins?: BasePlugin[];
78
77
 
79
78
  /**
80
- * 当前 agent 实例可见的环境变量快照。
79
+ * 当前 agent 的显式环境变量覆盖项。
81
80
  *
82
81
  * 关键点(中文)
83
- * - 这里传入的是已经在宿主侧完成合并的最终环境变量集合。
84
- * - SDK 不再区分 global env agent env;如需区分,应由更上层在传入前自行处理。
85
- * - 该快照会参与配置解析与运行时上下文装配,但不会回写到宿主环境。
82
+ * - 这里表示宿主显式注入给 agent 的基础 env。
83
+ * - `Agent` 会在这份基础 env 之上继续叠加项目 `.env`。
84
+ * - 覆盖后的最终 env 会参与配置解析与运行时上下文装配,但不会回写到宿主环境。
86
85
  */
87
86
  env?: Record<string, string>;
88
87
 
@@ -93,23 +92,14 @@ export interface AgentOptions {
93
92
  */
94
93
  export interface AgentStartOptions {
95
94
  /**
96
- * 是否启动 HTTP 服务。
95
+ * 是否启动本机 RPC 服务。
97
96
  *
98
97
  * 关键点(中文)
99
98
  * - `false` 表示不启动。
100
99
  * - 传对象时会按给定 host/port 启动。
101
100
  * - 省略时默认不启动,避免 SDK 本地嵌入场景误开端口。
102
101
  */
103
- http?: false | AgentHttpStartOptions;
104
-
105
- /**
106
- * 是否启动本地 RPC 服务。
107
- *
108
- * 关键点(中文)
109
- * - `true` 时启动主 local RPC server。
110
- * - 省略或 `false` 时不启动。
111
- */
112
- rpc?: boolean;
102
+ rpc?: false | AgentRpcStartOptions;
113
103
 
114
104
  /**
115
105
  * 是否启动当前 agent 的 plugins。
@@ -126,12 +116,7 @@ export interface AgentStartOptions {
126
116
  */
127
117
  export interface AgentStopResult {
128
118
  /**
129
- * 本次是否实际停止了 HTTP 服务。
130
- */
131
- httpStopped: boolean;
132
-
133
- /**
134
- * 本次是否实际停止了本地 RPC 服务。
119
+ * 本次是否实际停止了本机 RPC 服务。
135
120
  */
136
121
  rpcStopped: boolean;
137
122
 
@@ -146,12 +131,7 @@ export interface AgentStopResult {
146
131
  */
147
132
  export interface AgentStartResult {
148
133
  /**
149
- * 当前 agent 是否已启动 HTTP 服务。
150
- */
151
- http?: AgentHttpBinding;
152
-
153
- /**
154
- * 当前 agent 是否已启动本地 RPC 服务。
134
+ * 当前 agent 是否已启动本机 RPC 服务。
155
135
  */
156
136
  rpc?: AgentRpcBinding;
157
137
 
@@ -162,28 +142,28 @@ export interface AgentStartResult {
162
142
  }
163
143
 
164
144
  /**
165
- * Agent HTTP 启动参数。
145
+ * Agent RPC 启动参数。
166
146
  */
167
- export interface AgentHttpStartOptions {
147
+ export interface AgentRpcStartOptions {
168
148
  /**
169
- * HTTP 监听主机。
149
+ * RPC 监听主机。
170
150
  */
171
151
  host?: string;
172
152
 
173
153
  /**
174
- * HTTP 监听端口。
154
+ * RPC 监听端口。
175
155
  */
176
156
  port?: number;
177
157
  }
178
158
 
179
159
  /**
180
- * Agent HTTP 绑定信息。
160
+ * Agent RPC 绑定信息。
181
161
  */
182
- export interface AgentHttpBinding {
162
+ export interface AgentRpcBinding {
183
163
  /**
184
- * 对外访问地址。
164
+ * 远程访问地址。
185
165
  */
186
- baseUrl: string;
166
+ url: string;
187
167
 
188
168
  /**
189
169
  * 当前 host。
@@ -196,24 +176,9 @@ export interface AgentHttpBinding {
196
176
  port: number;
197
177
 
198
178
  /**
199
- * HTTP server 句柄。
200
- */
201
- server: ServerInstance;
202
- }
203
-
204
- /**
205
- * Agent RPC 绑定信息。
206
- */
207
- export interface AgentRpcBinding {
208
- /**
209
- * 当前本地 RPC endpoint。
210
- */
211
- endpoint: string;
212
-
213
- /**
214
- * 本地 RPC server 句柄。
179
+ * RPC server 句柄。
215
180
  */
216
- server: LocalRpcServerHandle;
181
+ server: RpcServerInstance;
217
182
  }
218
183
 
219
184
  /**
@@ -221,11 +186,12 @@ export interface AgentRpcBinding {
221
186
  */
222
187
  export interface RemoteAgentOptions {
223
188
  /**
224
- * 远程 SDK HTTP 基础地址。
189
+ * 远程 agent 访问地址。
225
190
  *
226
- * 例如:`http://127.0.0.1:15314`
191
+ * 例如:`https://city.example.com`、`http://127.0.0.1:15314`
192
+ * 或 `rpc://127.0.0.1:5314`
227
193
  */
228
- baseUrl: string;
194
+ url: string;
229
195
  }
230
196
 
231
197
  /**
@@ -24,12 +24,13 @@ export interface AgentProjectInitializationInput {
24
24
  projectRoot: string;
25
25
 
26
26
  /**
27
- * agent 展示名。
27
+ * agent 唯一标识。
28
28
  *
29
29
  * 关键点(中文)
30
- * - 为空时会自动回退到目录名推导出的默认名称。
30
+ * - 为空时会自动回退到目录名推导出的默认 id。
31
+ * - 该字段会直接写入 `downcity.json.id`。
31
32
  */
32
- agentName?: string;
33
+ id?: string;
33
34
 
34
35
  /**
35
36
  * 项目执行绑定配置。
@@ -71,9 +72,9 @@ export interface AgentProjectInitializationResult {
71
72
  projectRoot: string;
72
73
 
73
74
  /**
74
- * 最终写入的 agent 名称。
75
+ * 最终写入的 agent id。
75
76
  */
76
- agentName: string;
77
+ id: string;
77
78
 
78
79
  /**
79
80
  * 最终启用的 chat channels。