@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.
- package/README.md +6 -8
- package/bin/agent/Agent.d.ts +31 -16
- package/bin/agent/Agent.d.ts.map +1 -1
- package/bin/agent/Agent.js +47 -63
- package/bin/agent/Agent.js.map +1 -1
- package/bin/agent/RemoteAgent.d.ts +6 -6
- package/bin/agent/RemoteAgent.d.ts.map +1 -1
- package/bin/agent/RemoteAgent.js +377 -267
- package/bin/agent/RemoteAgent.js.map +1 -1
- package/bin/config/AgentInitializer.d.ts +4 -4
- package/bin/config/AgentInitializer.d.ts.map +1 -1
- package/bin/config/AgentInitializer.js +14 -12
- package/bin/config/AgentInitializer.js.map +1 -1
- package/bin/config/Config.d.ts +9 -30
- package/bin/config/Config.d.ts.map +1 -1
- package/bin/config/Config.js +23 -70
- package/bin/config/Config.js.map +1 -1
- package/bin/config/Defaults.d.ts.map +1 -1
- package/bin/config/Defaults.js +1 -9
- package/bin/config/Defaults.js.map +1 -1
- package/bin/config/DowncitySchema.d.ts.map +1 -1
- package/bin/config/DowncitySchema.js +3 -111
- package/bin/config/DowncitySchema.js.map +1 -1
- package/bin/config/ExecutionBinding.d.ts +0 -15
- package/bin/config/ExecutionBinding.d.ts.map +1 -1
- package/bin/config/ExecutionBinding.js +1 -21
- package/bin/config/ExecutionBinding.js.map +1 -1
- package/bin/config/Paths.d.ts +4 -43
- package/bin/config/Paths.d.ts.map +1 -1
- package/bin/config/Paths.js +10 -30
- package/bin/config/Paths.js.map +1 -1
- package/bin/config/PlatformPaths.d.ts +0 -4
- package/bin/config/PlatformPaths.d.ts.map +1 -1
- package/bin/config/PlatformPaths.js +5 -1
- package/bin/config/PlatformPaths.js.map +1 -1
- package/bin/executor/composer/system/default/assets/init/PROFILE.md.d.ts +1 -1
- package/bin/executor/composer/system/default/assets/init/PROFILE.md.d.ts.map +1 -1
- package/bin/executor/composer/system/default/assets/init/PROFILE.md.js +1 -1
- package/bin/executor/composer/system/default/assets/init/PROFILE.md.js.map +1 -1
- package/bin/executor/tools/shell/ShellToolFormatting.js +2 -2
- package/bin/executor/tools/shell/ShellToolFormatting.js.map +1 -1
- package/bin/executor/types/SessionRun.d.ts +1 -1
- package/bin/index.d.ts +4 -6
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +5 -5
- package/bin/index.js.map +1 -1
- package/bin/plugin/core/PluginCommandRequest.d.ts +1 -1
- package/bin/plugin/core/PluginCommandRequest.js +1 -1
- package/bin/plugin/core/PluginLocalExecution.d.ts.map +1 -1
- package/bin/plugin/core/PluginLocalExecution.js +4 -8
- package/bin/plugin/core/PluginLocalExecution.js.map +1 -1
- package/bin/plugin/types/PluginApi.d.ts +1 -1
- package/bin/rpc/Client.d.ts +104 -0
- package/bin/rpc/Client.d.ts.map +1 -0
- package/bin/rpc/Client.js +300 -0
- package/bin/rpc/Client.js.map +1 -0
- package/bin/rpc/Server.d.ts +41 -0
- package/bin/rpc/Server.d.ts.map +1 -0
- package/bin/rpc/Server.js +164 -0
- package/bin/rpc/Server.js.map +1 -0
- package/bin/runtime/host/daemon/Api.d.ts +1 -1
- package/bin/runtime/host/daemon/Client.d.ts +1 -1
- package/bin/runtime/host/daemon/Client.d.ts.map +1 -1
- package/bin/runtime/host/daemon/Client.js +30 -20
- package/bin/runtime/host/daemon/Client.js.map +1 -1
- package/bin/runtime/server/http/control/OverviewRoutes.js +1 -1
- package/bin/runtime/server/http/control/OverviewRoutes.js.map +1 -1
- package/bin/session/index.d.ts +1 -1
- package/bin/session/index.d.ts.map +1 -1
- package/bin/session/index.js +1 -1
- package/bin/session/index.js.map +1 -1
- package/bin/session/storage/Paths.d.ts +0 -4
- package/bin/session/storage/Paths.d.ts.map +1 -1
- package/bin/session/storage/Paths.js +0 -6
- package/bin/session/storage/Paths.js.map +1 -1
- package/bin/types/agent/AgentTypes.d.ts +23 -52
- package/bin/types/agent/AgentTypes.d.ts.map +1 -1
- package/bin/types/config/AgentProject.d.ts +6 -5
- package/bin/types/config/AgentProject.d.ts.map +1 -1
- package/bin/types/config/DowncityConfig.d.ts +8 -66
- package/bin/types/config/DowncityConfig.d.ts.map +1 -1
- package/bin/types/config/LlmConfig.d.ts +1 -1
- package/bin/types/config/Start.d.ts +3 -0
- package/bin/types/config/Start.d.ts.map +1 -1
- package/bin/types/config/Start.js +2 -0
- package/bin/types/config/Start.js.map +1 -1
- package/bin/types/runtime/host/Store.d.ts +7 -50
- package/bin/types/runtime/host/Store.d.ts.map +1 -1
- package/bin/types/runtime/platform/Platform.d.ts +6 -6
- package/bin/types/runtime/platform/Platform.d.ts.map +1 -1
- package/bin/types/runtime/platform/PlatformGateway.d.ts +2 -2
- package/bin/types/runtime/platform/PlatformGateway.d.ts.map +1 -1
- package/bin/utils/Time.d.ts +0 -1
- package/bin/utils/Time.d.ts.map +1 -1
- package/bin/utils/Time.js +0 -7
- package/bin/utils/Time.js.map +1 -1
- package/bin/utils/storage/index.d.ts +0 -1
- package/bin/utils/storage/index.d.ts.map +1 -1
- package/bin/utils/storage/index.js +0 -6
- package/bin/utils/storage/index.js.map +1 -1
- package/package.json +4 -6
- package/src/agent/Agent.ts +54 -71
- package/src/agent/RemoteAgent.ts +515 -345
- package/src/config/AgentInitializer.ts +14 -12
- package/src/config/Config.ts +28 -85
- package/src/config/Defaults.ts +1 -9
- package/src/config/DowncitySchema.ts +3 -113
- package/src/config/ExecutionBinding.ts +1 -23
- package/src/config/Paths.ts +10 -43
- package/src/config/PlatformPaths.ts +5 -1
- package/src/executor/composer/system/default/assets/init/PROFILE.md.ts +1 -1
- package/src/executor/composer/system/default/assets/init/PROFILE.md.ts.txt +1 -2
- package/src/executor/tools/shell/ShellToolFormatting.ts +2 -2
- package/src/executor/types/SessionRun.ts +1 -1
- package/src/index.ts +7 -14
- package/src/plugin/core/PluginCommandRequest.ts +1 -1
- package/src/plugin/core/PluginLocalExecution.ts +4 -8
- package/src/plugin/types/PluginApi.ts +1 -1
- package/src/rpc/Client.ts +467 -0
- package/src/rpc/Server.ts +302 -0
- package/src/runtime/host/daemon/Api.ts +1 -1
- package/src/runtime/host/daemon/Client.ts +44 -22
- package/src/runtime/server/http/control/OverviewRoutes.ts +1 -1
- package/src/session/index.ts +0 -1
- package/src/session/storage/Paths.ts +0 -10
- package/src/types/agent/AgentTypes.ts +23 -57
- package/src/types/config/AgentProject.ts +6 -5
- package/src/types/config/DowncityConfig.ts +8 -67
- package/src/types/config/LlmConfig.ts +1 -1
- package/src/types/config/Start.ts +3 -0
- package/src/types/runtime/host/Store.ts +7 -53
- package/src/types/runtime/platform/Platform.ts +6 -6
- package/src/types/runtime/platform/PlatformGateway.ts +2 -2
- package/src/utils/Time.ts +0 -6
- package/src/utils/storage/index.ts +0 -7
- package/tsconfig.tsbuildinfo +1 -1
- package/bin/config/ConfigEnvResolver.d.ts +0 -22
- package/bin/config/ConfigEnvResolver.d.ts.map +0 -1
- package/bin/config/ConfigEnvResolver.js +0 -41
- package/bin/config/ConfigEnvResolver.js.map +0 -1
- package/bin/runtime/server/rpc/Server.d.ts +0 -18
- package/bin/runtime/server/rpc/Server.d.ts.map +0 -1
- package/bin/runtime/server/rpc/Server.js +0 -315
- package/bin/runtime/server/rpc/Server.js.map +0 -1
- package/bin/runtime/transport/rpc/Client.d.ts +0 -13
- package/bin/runtime/transport/rpc/Client.d.ts.map +0 -1
- package/bin/runtime/transport/rpc/Client.js +0 -98
- package/bin/runtime/transport/rpc/Client.js.map +0 -1
- package/bin/runtime/transport/rpc/Paths.d.ts +0 -14
- package/bin/runtime/transport/rpc/Paths.d.ts.map +0 -1
- package/bin/runtime/transport/rpc/Paths.js +0 -42
- package/bin/runtime/transport/rpc/Paths.js.map +0 -1
- package/bin/runtime/transport/rpc/Transport.d.ts +0 -21
- package/bin/runtime/transport/rpc/Transport.d.ts.map +0 -1
- package/bin/runtime/transport/rpc/Transport.js +0 -30
- package/bin/runtime/transport/rpc/Transport.js.map +0 -1
- package/bin/types/common/ResolvedConfigValue.d.ts +0 -12
- package/bin/types/common/ResolvedConfigValue.d.ts.map +0 -1
- package/bin/types/common/ResolvedConfigValue.js +0 -2
- package/bin/types/common/ResolvedConfigValue.js.map +0 -1
- package/bin/types/runtime/rpc/LocalRpc.d.ts +0 -69
- package/bin/types/runtime/rpc/LocalRpc.d.ts.map +0 -1
- package/bin/types/runtime/rpc/LocalRpc.js +0 -9
- package/bin/types/runtime/rpc/LocalRpc.js.map +0 -1
- package/src/config/ConfigEnvResolver.ts +0 -52
- package/src/runtime/server/rpc/Server.ts +0 -408
- package/src/runtime/transport/rpc/Client.ts +0 -113
- package/src/runtime/transport/rpc/Paths.ts +0 -50
- package/src/runtime/transport/rpc/Transport.ts +0 -43
- package/src/types/common/ResolvedConfigValue.ts +0 -16
- 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
|
+
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 关键点(中文)
|
|
5
5
|
* - 业务模块统一通过 daemon API 与运行时通信。
|
|
6
|
-
* -
|
|
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.
|
|
80
|
-
const explicitPort = parsePortLike(params.
|
|
81
|
+
const explicitHost = normalizeHost(params.explicit_host);
|
|
82
|
+
const explicitPort = parsePortLike(params.explicit_port);
|
|
81
83
|
|
|
82
|
-
const envHost =
|
|
83
|
-
|
|
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,
|
|
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 ||
|
|
106
|
-
const port = explicitPort || envPort || daemonArgPort ||
|
|
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
|
*
|
package/src/session/index.ts
CHANGED
|
@@ -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 {
|
|
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
|
-
* -
|
|
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
|
-
*
|
|
95
|
+
* 是否启动本机 RPC 服务。
|
|
97
96
|
*
|
|
98
97
|
* 关键点(中文)
|
|
99
98
|
* - `false` 表示不启动。
|
|
100
99
|
* - 传对象时会按给定 host/port 启动。
|
|
101
100
|
* - 省略时默认不启动,避免 SDK 本地嵌入场景误开端口。
|
|
102
101
|
*/
|
|
103
|
-
|
|
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
|
-
*
|
|
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
|
|
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
|
|
145
|
+
* Agent RPC 启动参数。
|
|
166
146
|
*/
|
|
167
|
-
export interface
|
|
147
|
+
export interface AgentRpcStartOptions {
|
|
168
148
|
/**
|
|
169
|
-
*
|
|
149
|
+
* RPC 监听主机。
|
|
170
150
|
*/
|
|
171
151
|
host?: string;
|
|
172
152
|
|
|
173
153
|
/**
|
|
174
|
-
*
|
|
154
|
+
* RPC 监听端口。
|
|
175
155
|
*/
|
|
176
156
|
port?: number;
|
|
177
157
|
}
|
|
178
158
|
|
|
179
159
|
/**
|
|
180
|
-
* Agent
|
|
160
|
+
* Agent RPC 绑定信息。
|
|
181
161
|
*/
|
|
182
|
-
export interface
|
|
162
|
+
export interface AgentRpcBinding {
|
|
183
163
|
/**
|
|
184
|
-
*
|
|
164
|
+
* 远程访问地址。
|
|
185
165
|
*/
|
|
186
|
-
|
|
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
|
-
*
|
|
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:
|
|
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
|
-
* 远程
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
77
|
+
id: string;
|
|
77
78
|
|
|
78
79
|
/**
|
|
79
80
|
* 最终启用的 chat channels。
|