@downcity/agent 1.1.22 → 1.1.25

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 (112) hide show
  1. package/README.md +6 -5
  2. package/bin/core/AgentContextTypes.d.ts +21 -2
  3. package/bin/core/AgentContextTypes.d.ts.map +1 -1
  4. package/bin/index.d.ts +6 -3
  5. package/bin/index.d.ts.map +1 -1
  6. package/bin/index.js.map +1 -1
  7. package/bin/runtime/server/http/Server.d.ts +1 -1
  8. package/bin/runtime/server/http/Server.d.ts.map +1 -1
  9. package/bin/runtime/server/http/Server.js +4 -0
  10. package/bin/runtime/server/http/Server.js.map +1 -1
  11. package/bin/runtime/server/http/control/ExecuteBySession.js +1 -1
  12. package/bin/runtime/server/http/control/ExecuteBySession.js.map +1 -1
  13. package/bin/runtime/server/http/control/SessionRoutes.d.ts.map +1 -1
  14. package/bin/runtime/server/http/control/SessionRoutes.js +0 -27
  15. package/bin/runtime/server/http/control/SessionRoutes.js.map +1 -1
  16. package/bin/runtime/server/http/execute/execute.js +1 -1
  17. package/bin/runtime/server/http/execute/execute.js.map +1 -1
  18. package/bin/runtime/server/http/sdk/Router.d.ts +14 -0
  19. package/bin/runtime/server/http/sdk/Router.d.ts.map +1 -0
  20. package/bin/runtime/server/http/sdk/Router.js +18 -0
  21. package/bin/runtime/server/http/sdk/Router.js.map +1 -0
  22. package/bin/runtime/server/http/sdk/SessionRoutes.d.ts +15 -0
  23. package/bin/runtime/server/http/sdk/SessionRoutes.d.ts.map +1 -0
  24. package/bin/runtime/server/http/sdk/SessionRoutes.js +190 -0
  25. package/bin/runtime/server/http/sdk/SessionRoutes.js.map +1 -0
  26. package/bin/sdk/AgentSdkTypes.d.ts +1 -124
  27. package/bin/sdk/AgentSdkTypes.d.ts.map +1 -1
  28. package/bin/sdk/RemoteAgent.d.ts +27 -8
  29. package/bin/sdk/RemoteAgent.d.ts.map +1 -1
  30. package/bin/sdk/RemoteAgent.js +266 -38
  31. package/bin/sdk/RemoteAgent.js.map +1 -1
  32. package/bin/sdk/Session.d.ts +31 -10
  33. package/bin/sdk/Session.d.ts.map +1 -1
  34. package/bin/sdk/Session.js +181 -73
  35. package/bin/sdk/Session.js.map +1 -1
  36. package/bin/sdk/SessionEventMapper.d.ts +32 -0
  37. package/bin/sdk/SessionEventMapper.d.ts.map +1 -0
  38. package/bin/sdk/{StreamEvents.js → SessionEventMapper.js} +43 -28
  39. package/bin/sdk/SessionEventMapper.js.map +1 -0
  40. package/bin/sdk/session/ServicePort.d.ts +51 -4
  41. package/bin/sdk/session/ServicePort.d.ts.map +1 -1
  42. package/bin/sdk/session/ServicePort.js +17 -10
  43. package/bin/sdk/session/ServicePort.js.map +1 -1
  44. package/bin/sdk/session/runtime/SessionEventHub.d.ts +24 -0
  45. package/bin/sdk/session/runtime/SessionEventHub.d.ts.map +1 -0
  46. package/bin/sdk/session/runtime/SessionEventHub.js +39 -0
  47. package/bin/sdk/session/runtime/SessionEventHub.js.map +1 -0
  48. package/bin/sdk/session/runtime/SessionPromptRuntime.d.ts +68 -0
  49. package/bin/sdk/session/runtime/SessionPromptRuntime.d.ts.map +1 -0
  50. package/bin/sdk/session/runtime/SessionPromptRuntime.js +170 -0
  51. package/bin/sdk/session/runtime/SessionPromptRuntime.js.map +1 -0
  52. package/bin/service/builtins/chat/runtime/ChatQueueWorker.js +1 -1
  53. package/bin/service/builtins/chat/runtime/ChatQueueWorker.js.map +1 -1
  54. package/bin/service/builtins/chat/runtime/ChatSession.d.ts +1 -1
  55. package/bin/service/builtins/chat/runtime/ChatSession.js +1 -1
  56. package/bin/service/builtins/contact/runtime/ChatRuntime.js +1 -1
  57. package/bin/service/builtins/contact/runtime/ChatRuntime.js.map +1 -1
  58. package/bin/types/sdk/AgentSessionEvent.d.ts +160 -0
  59. package/bin/types/sdk/AgentSessionEvent.d.ts.map +1 -0
  60. package/bin/types/sdk/AgentSessionEvent.js +9 -0
  61. package/bin/types/sdk/AgentSessionEvent.js.map +1 -0
  62. package/bin/types/sdk/AgentSessionPrompt.d.ts +21 -0
  63. package/bin/types/sdk/AgentSessionPrompt.d.ts.map +1 -0
  64. package/bin/types/sdk/AgentSessionPrompt.js +9 -0
  65. package/bin/types/sdk/AgentSessionPrompt.js.map +1 -0
  66. package/bin/types/sdk/AgentSessionTurn.d.ts +59 -0
  67. package/bin/types/sdk/AgentSessionTurn.d.ts.map +1 -0
  68. package/bin/types/sdk/AgentSessionTurn.js +10 -0
  69. package/bin/types/sdk/AgentSessionTurn.js.map +1 -0
  70. package/bin/types/sdk/InternalUiChunkEvent.d.ts +100 -0
  71. package/bin/types/sdk/InternalUiChunkEvent.d.ts.map +1 -0
  72. package/bin/types/sdk/InternalUiChunkEvent.js +9 -0
  73. package/bin/types/sdk/InternalUiChunkEvent.js.map +1 -0
  74. package/package.json +2 -1
  75. package/scripts/session-prompt-runtime.test.mjs +167 -0
  76. package/src/core/AgentContextTypes.ts +24 -2
  77. package/src/index.ts +11 -8
  78. package/src/runtime/server/http/Server.ts +5 -1
  79. package/src/runtime/server/http/control/ExecuteBySession.ts +1 -1
  80. package/src/runtime/server/http/control/SessionRoutes.ts +0 -27
  81. package/src/runtime/server/http/execute/execute.ts +1 -1
  82. package/src/runtime/server/http/sdk/Router.ts +22 -0
  83. package/src/runtime/server/http/sdk/SessionRoutes.ts +232 -0
  84. package/src/sdk/AgentSdkTypes.ts +1 -137
  85. package/src/sdk/RemoteAgent.ts +368 -49
  86. package/src/sdk/Session.ts +235 -86
  87. package/src/sdk/{StreamEvents.ts → SessionEventMapper.ts} +50 -40
  88. package/src/sdk/session/ServicePort.ts +74 -12
  89. package/src/sdk/session/runtime/SessionEventHub.ts +47 -0
  90. package/src/sdk/session/runtime/SessionPromptRuntime.ts +269 -0
  91. package/src/service/builtins/chat/runtime/ChatQueueWorker.ts +1 -1
  92. package/src/service/builtins/chat/runtime/ChatSession.ts +1 -1
  93. package/src/service/builtins/contact/runtime/ChatRuntime.ts +1 -1
  94. package/src/session/README.md +8 -7
  95. package/src/types/sdk/AgentSessionEvent.ts +195 -0
  96. package/src/types/sdk/AgentSessionPrompt.ts +21 -0
  97. package/src/types/sdk/AgentSessionTurn.ts +65 -0
  98. package/src/types/sdk/InternalUiChunkEvent.ts +108 -0
  99. package/tsconfig.tsbuildinfo +1 -1
  100. package/bin/runtime/server/http/control/StreamBySession.d.ts +0 -22
  101. package/bin/runtime/server/http/control/StreamBySession.d.ts.map +0 -1
  102. package/bin/runtime/server/http/control/StreamBySession.js +0 -135
  103. package/bin/runtime/server/http/control/StreamBySession.js.map +0 -1
  104. package/bin/sdk/AsyncQueue.d.ts +0 -26
  105. package/bin/sdk/AsyncQueue.d.ts.map +0 -1
  106. package/bin/sdk/AsyncQueue.js +0 -75
  107. package/bin/sdk/AsyncQueue.js.map +0 -1
  108. package/bin/sdk/StreamEvents.d.ts +0 -36
  109. package/bin/sdk/StreamEvents.d.ts.map +0 -1
  110. package/bin/sdk/StreamEvents.js.map +0 -1
  111. package/src/runtime/server/http/control/StreamBySession.ts +0 -198
  112. package/src/sdk/AsyncQueue.ts +0 -92
@@ -0,0 +1,232 @@
1
+ /**
2
+ * SDK HTTP session 路由。
3
+ *
4
+ * 关键点(中文)
5
+ * - 这组路由面向 `RemoteAgent`,只暴露最小 Session actor 使用面。
6
+ * - 当前公开输入收口到 `prompt()`,公开输出收口到 `events` 长连接。
7
+ * - 不复用 control API 的控制台语义,避免 transport 面混入非 SDK 约束。
8
+ */
9
+
10
+ import { Hono } from "hono";
11
+ import type { AgentCore } from "@/core/AgentCore.js";
12
+ import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js";
13
+ import type { AgentSessionEvent } from "@/types/sdk/AgentSessionEvent.js";
14
+
15
+ const NDJSON_CONTENT_TYPE = "application/x-ndjson; charset=utf-8";
16
+ const SDK_EVENTS_READY_FRAME = {
17
+ type: "sdk-events-ready",
18
+ } as const;
19
+
20
+ /**
21
+ * 注册 SDK session 路由。
22
+ */
23
+ export function registerSdkSessionRoutes(
24
+ app: Hono,
25
+ core: Pick<AgentCore, "session" | "sessions">,
26
+ ): void {
27
+ app.get("/api/sdk/sessions", async (c) => {
28
+ try {
29
+ const sessions = await core.sessions();
30
+ return c.json({
31
+ success: true,
32
+ sessions,
33
+ });
34
+ } catch (error) {
35
+ return c.json(
36
+ {
37
+ success: false,
38
+ error: error instanceof Error ? error.message : String(error),
39
+ },
40
+ 500,
41
+ );
42
+ }
43
+ });
44
+
45
+ app.post("/api/sdk/sessions", async (c) => {
46
+ try {
47
+ const body = (await c.req.json().catch(() => ({}))) as {
48
+ sessionId?: unknown;
49
+ };
50
+ const rawSessionId = String(body.sessionId || "").trim();
51
+ const session = await core.session(rawSessionId || undefined);
52
+ return c.json({
53
+ success: true,
54
+ session: await session.toMetadata(),
55
+ });
56
+ } catch (error) {
57
+ return c.json(
58
+ {
59
+ success: false,
60
+ error: error instanceof Error ? error.message : String(error),
61
+ },
62
+ 500,
63
+ );
64
+ }
65
+ });
66
+
67
+ app.post("/api/sdk/sessions/:sessionId/prompt", async (c) => {
68
+ try {
69
+ const sessionId = String(c.req.param("sessionId") || "").trim();
70
+ if (!sessionId) {
71
+ return c.json({ success: false, error: "Missing sessionId" }, 400);
72
+ }
73
+ const body = (await c.req.json()) as AgentSessionPromptInput;
74
+ const session = await core.session(sessionId);
75
+ const turn = await session.prompt(body);
76
+ return c.json({
77
+ success: true,
78
+ turn: {
79
+ id: turn.id,
80
+ },
81
+ });
82
+ } catch (error) {
83
+ return c.json(
84
+ {
85
+ success: false,
86
+ error: error instanceof Error ? error.message : String(error),
87
+ },
88
+ 500,
89
+ );
90
+ }
91
+ });
92
+
93
+ app.get("/api/sdk/sessions/:sessionId/events", async (c) => {
94
+ const sessionId = String(c.req.param("sessionId") || "").trim();
95
+ if (!sessionId) {
96
+ return c.json({ success: false, error: "Missing sessionId" }, 400);
97
+ }
98
+
99
+ try {
100
+ const session = await core.session(sessionId);
101
+ const encoder = new TextEncoder();
102
+ const requestSignal = c.req.raw.signal;
103
+
104
+ let cleanupEventsConnection = (): void => {};
105
+ const stream = new ReadableStream<Uint8Array>({
106
+ cancel() {
107
+ cleanupEventsConnection();
108
+ },
109
+ start(controller) {
110
+ const writeLine = (value: unknown): void => {
111
+ controller.enqueue(encoder.encode(`${JSON.stringify(value)}\n`));
112
+ };
113
+
114
+ const unsubscribe = session.subscribe((event) => {
115
+ writeLine(event);
116
+ });
117
+
118
+ const closeStream = (): void => {
119
+ cleanupEventsConnection();
120
+ try {
121
+ controller.close();
122
+ } catch {
123
+ // ignore duplicate close attempts
124
+ }
125
+ };
126
+
127
+ cleanupEventsConnection = (): void => {
128
+ unsubscribe();
129
+ requestSignal.removeEventListener("abort", closeStream);
130
+ };
131
+
132
+ if (requestSignal.aborted) {
133
+ closeStream();
134
+ return;
135
+ }
136
+
137
+ requestSignal.addEventListener("abort", closeStream, { once: true });
138
+ writeLine(SDK_EVENTS_READY_FRAME);
139
+ },
140
+ });
141
+
142
+ return new Response(stream, {
143
+ status: 200,
144
+ headers: {
145
+ "Content-Type": NDJSON_CONTENT_TYPE,
146
+ "Cache-Control": "no-cache, no-transform",
147
+ "X-Accel-Buffering": "no",
148
+ },
149
+ });
150
+ } catch (error) {
151
+ return c.json(
152
+ {
153
+ success: false,
154
+ error: error instanceof Error ? error.message : String(error),
155
+ },
156
+ 500,
157
+ );
158
+ }
159
+ });
160
+
161
+ app.get("/api/sdk/sessions/:sessionId/messages", async (c) => {
162
+ try {
163
+ const sessionId = String(c.req.param("sessionId") || "").trim();
164
+ if (!sessionId) {
165
+ return c.json({ success: false, error: "Missing sessionId" }, 400);
166
+ }
167
+ const session = await core.session(sessionId);
168
+ return c.json({
169
+ success: true,
170
+ messages: await session.history(),
171
+ });
172
+ } catch (error) {
173
+ return c.json(
174
+ {
175
+ success: false,
176
+ error: error instanceof Error ? error.message : String(error),
177
+ },
178
+ 500,
179
+ );
180
+ }
181
+ });
182
+
183
+ app.get("/api/sdk/sessions/:sessionId/system", async (c) => {
184
+ try {
185
+ const sessionId = String(c.req.param("sessionId") || "").trim();
186
+ if (!sessionId) {
187
+ return c.json({ success: false, error: "Missing sessionId" }, 400);
188
+ }
189
+ const session = await core.session(sessionId);
190
+ return c.json({
191
+ success: true,
192
+ system: await session.system(),
193
+ });
194
+ } catch (error) {
195
+ return c.json(
196
+ {
197
+ success: false,
198
+ error: error instanceof Error ? error.message : String(error),
199
+ },
200
+ 500,
201
+ );
202
+ }
203
+ });
204
+
205
+ app.post("/api/sdk/sessions/:sessionId/fork", async (c) => {
206
+ try {
207
+ const sessionId = String(c.req.param("sessionId") || "").trim();
208
+ if (!sessionId) {
209
+ return c.json({ success: false, error: "Missing sessionId" }, 400);
210
+ }
211
+ const body = (await c.req.json().catch(() => ({}))) as {
212
+ messageId?: unknown;
213
+ };
214
+ const session = await core.session(sessionId);
215
+ const forked = await session.fork(
216
+ String(body.messageId || "").trim() || undefined,
217
+ );
218
+ return c.json({
219
+ success: true,
220
+ session: await forked.toMetadata(),
221
+ });
222
+ } catch (error) {
223
+ return c.json(
224
+ {
225
+ success: false,
226
+ error: error instanceof Error ? error.message : String(error),
227
+ },
228
+ 500,
229
+ );
230
+ }
231
+ });
232
+ }
@@ -9,9 +9,7 @@
9
9
 
10
10
  import type { LanguageModel, Tool } from "ai";
11
11
  import type { BaseService } from "@/service/builtins/BaseService.js";
12
- import type { JsonValue } from "@/types/common/Json.js";
13
12
  import type { Plugin } from "@/plugin/types/Plugin.js";
14
- import type { SessionMessageV1 } from "@/session/types/SessionMessages.js";
15
13
  import type { AgentPlatformRuntime } from "@/types/runtime/host/AgentHost.js";
16
14
  import type { LocalRpcServerHandle } from "@/types/runtime/rpc/LocalRpc.js";
17
15
  import type { ServerInstance } from "@/runtime/server/http/Server.js";
@@ -292,140 +290,6 @@ export interface AgentSessionConfigSnapshot {
292
290
  modelLabel?: string;
293
291
  }
294
292
 
295
- /**
296
- * Session 运行输入。
297
- */
298
- export interface AgentSessionRunInput {
299
- /**
300
- * 当前轮用户查询文本。
301
- */
302
- query: string;
303
- }
304
-
305
- /**
306
- * Session 运行结果。
307
- */
308
- export interface AgentSessionRunResult {
309
- /**
310
- * 本轮执行是否成功。
311
- */
312
- success: boolean;
313
-
314
- /**
315
- * 失败时的错误文本。
316
- */
317
- error?: string;
318
-
319
- /**
320
- * 最终 assistant 文本。
321
- */
322
- text: string;
323
-
324
- /**
325
- * 最终 assistant 原始 UIMessage。
326
- */
327
- assistantMessage: SessionMessageV1;
328
- }
329
-
330
- /**
331
- * SDK 对外的流式事件。
332
- */
333
- export type AgentSessionStreamEvent =
334
- | {
335
- /**
336
- * 文本增量事件。
337
- */
338
- type: "text-delta";
339
- /**
340
- * 当前追加的文本片段。
341
- */
342
- text: string;
343
- }
344
- | {
345
- /**
346
- * reasoning 增量事件。
347
- */
348
- type: "reasoning-delta";
349
- /**
350
- * 当前追加的 reasoning 文本片段。
351
- */
352
- text: string;
353
- }
354
- | {
355
- /**
356
- * 工具调用可用事件。
357
- */
358
- type: "tool-call";
359
- /**
360
- * 当前工具调用唯一标识。
361
- */
362
- toolCallId: string;
363
- /**
364
- * 工具名称。
365
- */
366
- toolName: string;
367
- /**
368
- * 工具输入参数。
369
- */
370
- args: JsonValue;
371
- }
372
- | {
373
- /**
374
- * 工具调用结果事件。
375
- */
376
- type: "tool-result";
377
- /**
378
- * 当前工具调用唯一标识。
379
- */
380
- toolCallId: string;
381
- /**
382
- * 工具名称。
383
- */
384
- toolName: string;
385
- /**
386
- * 工具输出结果。
387
- */
388
- result: JsonValue;
389
- }
390
- | {
391
- /**
392
- * 工具调用失败事件。
393
- */
394
- type: "tool-error";
395
- /**
396
- * 当前工具调用唯一标识。
397
- */
398
- toolCallId: string;
399
- /**
400
- * 工具名称。
401
- */
402
- toolName: string;
403
- /**
404
- * 错误文本。
405
- */
406
- error: string;
407
- }
408
- | {
409
- /**
410
- * 运行结束事件。
411
- */
412
- type: "finish";
413
- /**
414
- * 最终完成原因(若底层可提供)。
415
- */
416
- finishReason?: string;
417
- }
418
- | {
419
- /**
420
- * 运行错误事件。
421
- */
422
- type: "error";
423
- /**
424
- * 错误文本。
425
- */
426
- error: string;
427
- };
428
-
429
293
  /**
430
294
  * Session system block 来源类型。
431
295
  */
@@ -490,7 +354,7 @@ export interface AgentSessionSystemSessionInfo {
490
354
  *
491
355
  * 关键点(中文)
492
356
  * - 这是 session 初始化时落盘的稳定参考时间,按 Date/ISO 字符串对外展示。
493
- * - 它不是每轮运行的当前时间,不会随着 `run()` / `stream()` 改变。
357
+ * - 它不是每轮运行的当前时间,不会随着后续 turn 执行而改变。
494
358
  */
495
359
  createdAt: string;
496
360