@downcity/agent 1.1.12 → 1.1.13
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/bin/index.d.ts +2 -2
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +2 -2
- package/bin/index.js.map +1 -1
- package/bin/runtime/AgentRuntime.d.ts +2 -2
- package/bin/runtime/AgentRuntime.d.ts.map +1 -1
- package/bin/runtime/AgentRuntime.js +7 -12
- package/bin/runtime/AgentRuntime.js.map +1 -1
- package/bin/runtime/AgentRuntimeTypes.d.ts +3 -3
- package/bin/runtime/AgentRuntimeTypes.d.ts.map +1 -1
- package/bin/sdk/Agent.d.ts +2 -2
- package/bin/sdk/Agent.d.ts.map +1 -1
- package/bin/sdk/Agent.js +2 -2
- package/bin/sdk/Agent.js.map +1 -1
- package/bin/sdk/Session.d.ts +6 -6
- package/bin/sdk/Session.d.ts.map +1 -1
- package/bin/sdk/Session.js +41 -53
- package/bin/sdk/Session.js.map +1 -1
- package/bin/sdk/SessionMetadata.d.ts +3 -3
- package/bin/sdk/SessionMetadata.d.ts.map +1 -1
- package/bin/sdk/SessionMetadata.js +5 -5
- package/bin/sdk/SessionMetadata.js.map +1 -1
- package/bin/sdk/SessionPersistence.d.ts +6 -6
- package/bin/sdk/SessionPersistence.d.ts.map +1 -1
- package/bin/sdk/SessionPersistence.js +7 -7
- package/bin/sdk/SessionPersistence.js.map +1 -1
- package/bin/sdk/SessionServicePort.d.ts +5 -5
- package/bin/sdk/SessionServicePort.d.ts.map +1 -1
- package/bin/sdk/SessionServicePort.js +9 -9
- package/bin/sdk/SessionServicePort.js.map +1 -1
- package/bin/sdk/{SdkSessionSystemComposer.d.ts → SessionSystemBuilder.d.ts} +7 -7
- package/bin/sdk/SessionSystemBuilder.d.ts.map +1 -0
- package/bin/sdk/{SdkSessionSystemComposer.js → SessionSystemBuilder.js} +14 -14
- package/bin/sdk/SessionSystemBuilder.js.map +1 -0
- package/bin/service/builtins/chat/runtime/ChatSession.d.ts +27 -8
- package/bin/service/builtins/chat/runtime/ChatSession.d.ts.map +1 -1
- package/bin/service/builtins/chat/runtime/ChatSession.js +9 -4
- package/bin/service/builtins/chat/runtime/ChatSession.js.map +1 -1
- package/bin/service/builtins/task/runtime/Runner.d.ts +1 -1
- package/bin/service/builtins/task/runtime/Runner.js +1 -1
- package/bin/service/builtins/task/runtime/TaskRunnerSession.d.ts +2 -2
- package/bin/service/builtins/task/runtime/TaskRunnerSession.js +4 -4
- package/bin/service/builtins/task/runtime/TaskRunnerSession.js.map +1 -1
- package/bin/service/builtins/task/runtime/TaskRunnerTypes.d.ts +1 -1
- package/bin/session/{Session.d.ts → Executor.d.ts} +54 -17
- package/bin/session/Executor.d.ts.map +1 -0
- package/bin/session/{Session.js → Executor.js} +60 -23
- package/bin/session/Executor.js.map +1 -0
- package/bin/session/composer/SessionComposer.d.ts +1 -1
- package/bin/session/composer/SessionComposer.js +1 -1
- package/bin/session/composer/compaction/SessionCompactionComposer.d.ts +2 -2
- package/bin/session/composer/execution/LocalSessionExecutionComposer.d.ts +1 -1
- package/bin/session/composer/execution/LocalSessionExecutionComposer.js +1 -1
- package/bin/session/composer/execution/SessionExecutionComposer.d.ts +1 -1
- package/bin/session/composer/history/SessionHistoryComposer.d.ts +1 -1
- package/bin/session/composer/system/SessionSystemComposer.d.ts +1 -1
- package/bin/session/composer/system/SessionSystemComposer.js +1 -1
- package/bin/session/executors/local/{LocalSessionCore.d.ts → Runner.d.ts} +10 -10
- package/bin/session/executors/local/Runner.d.ts.map +1 -0
- package/bin/session/executors/local/{LocalSessionCore.js → Runner.js} +11 -11
- package/bin/session/executors/local/Runner.js.map +1 -0
- package/bin/session/executors/local/SessionExecutionError.d.ts +1 -1
- package/bin/session/executors/local/SessionExecutionError.js +1 -1
- package/bin/session/executors/local/SessionLoopDecision.d.ts +3 -3
- package/bin/session/executors/local/SessionLoopDecision.js +3 -3
- package/bin/session/executors/local/SessionModelMessageState.d.ts +2 -2
- package/bin/session/executors/local/SessionModelMessageState.js +2 -2
- package/bin/session/executors/local/SessionSignals.d.ts +3 -3
- package/bin/session/executors/local/SessionSignals.js +3 -3
- package/bin/session/executors/local/SessionToolLoopRunner.d.ts +1 -1
- package/bin/session/executors/local/SessionToolLoopRunner.js +2 -2
- package/bin/session/executors/local/SessionToolLoopRunner.js.map +1 -1
- package/bin/session/types/SessionLoop.d.ts +1 -1
- package/bin/session/types/SessionLoop.js +1 -1
- package/bin/session/types/SessionRun.d.ts +2 -2
- package/bin/session/types/SessionRun.js +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -2
- package/src/runtime/AgentRuntime.ts +7 -17
- package/src/runtime/AgentRuntimeTypes.ts +3 -3
- package/src/sdk/Agent.ts +5 -5
- package/src/sdk/Session.ts +52 -66
- package/src/sdk/SessionMetadata.ts +5 -5
- package/src/sdk/SessionPersistence.ts +13 -13
- package/src/sdk/SessionServicePort.ts +13 -13
- package/src/sdk/{SdkSessionSystemComposer.ts → SessionSystemBuilder.ts} +24 -24
- package/src/service/builtins/chat/runtime/ChatSession.ts +37 -13
- package/src/service/builtins/task/runtime/Runner.ts +1 -1
- package/src/service/builtins/task/runtime/TaskRunnerSession.ts +4 -4
- package/src/service/builtins/task/runtime/TaskRunnerTypes.ts +1 -1
- package/src/session/{Session.ts → Executor.ts} +96 -26
- package/src/session/composer/SessionComposer.ts +1 -1
- package/src/session/composer/compaction/SessionCompactionComposer.ts +2 -2
- package/src/session/composer/execution/LocalSessionExecutionComposer.ts +1 -1
- package/src/session/composer/execution/SessionExecutionComposer.ts +1 -1
- package/src/session/composer/history/SessionHistoryComposer.ts +1 -1
- package/src/session/composer/system/SessionSystemComposer.ts +1 -1
- package/src/session/executors/local/{LocalSessionCore.ts → Runner.ts} +13 -13
- package/src/session/executors/local/SessionExecutionError.ts +1 -1
- package/src/session/executors/local/SessionLoopDecision.ts +3 -3
- package/src/session/executors/local/SessionModelMessageState.ts +2 -2
- package/src/session/executors/local/SessionSignals.ts +3 -3
- package/src/session/executors/local/SessionToolLoopRunner.ts +2 -2
- package/src/session/types/SessionLoop.ts +1 -1
- package/src/session/types/SessionRun.ts +2 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/bin/sdk/SdkSessionSystemComposer.d.ts.map +0 -1
- package/bin/sdk/SdkSessionSystemComposer.js.map +0 -1
- package/bin/session/Session.d.ts.map +0 -1
- package/bin/session/Session.js.map +0 -1
- package/bin/session/executors/local/LocalSessionCore.d.ts.map +0 -1
- package/bin/session/executors/local/LocalSessionCore.js.map +0 -1
- package/bin/session/executors/local/LocalSessionExecutor.d.ts +0 -61
- package/bin/session/executors/local/LocalSessionExecutor.d.ts.map +0 -1
- package/bin/session/executors/local/LocalSessionExecutor.js +0 -36
- package/bin/session/executors/local/LocalSessionExecutor.js.map +0 -1
- package/src/session/executors/local/LocalSessionExecutor.ts +0 -89
package/src/sdk/Session.ts
CHANGED
|
@@ -4,15 +4,13 @@
|
|
|
4
4
|
* 关键点(中文)
|
|
5
5
|
* - 面向 `new Agent(...)` 的本地会话使用场景。
|
|
6
6
|
* - 统一收口消息落盘、默认模型配置、run/stream/fork 等高层 API。
|
|
7
|
-
* -
|
|
7
|
+
* - 内部继续复用 `Executor` / `JsonlSessionHistoryComposer` / `Runner`。
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { nanoid } from "nanoid";
|
|
11
11
|
import type { LanguageModel, Tool } from "ai";
|
|
12
|
-
import {
|
|
12
|
+
import { Executor } from "@session/Executor.js";
|
|
13
13
|
import { JsonlSessionHistoryComposer } from "@session/composer/history/jsonl/JsonlSessionHistoryComposer.js";
|
|
14
|
-
import { JsonlSessionCompactionComposer } from "@session/composer/compaction/jsonl/JsonlSessionCompactionComposer.js";
|
|
15
|
-
import { LocalSessionExecutor } from "@session/executors/local/LocalSessionExecutor.js";
|
|
16
14
|
import { extractTextFromUiMessage } from "@/service/builtins/chat/runtime/UIMessageTransformer.js";
|
|
17
15
|
import type {
|
|
18
16
|
AgentSessionConfigSnapshot,
|
|
@@ -27,15 +25,15 @@ import type {
|
|
|
27
25
|
} from "@/sdk/AgentSdkTypes.js";
|
|
28
26
|
import type { SessionMessageV1 } from "@/session/types/SessionMessages.js";
|
|
29
27
|
import {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
} from "@/sdk/
|
|
28
|
+
buildSessionSystemBlocks,
|
|
29
|
+
SessionSystemBuilder,
|
|
30
|
+
} from "@/sdk/SessionSystemBuilder.js";
|
|
33
31
|
import {
|
|
34
32
|
inferModelLabel,
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
patchSessionModelLabel,
|
|
34
|
+
readSessionMetadata,
|
|
37
35
|
resolveSystemTimezone,
|
|
38
|
-
|
|
36
|
+
writeSessionMetadata,
|
|
39
37
|
} from "@/sdk/SessionMetadata.js";
|
|
40
38
|
import {
|
|
41
39
|
getSdkAgentSessionArchiveDirPath,
|
|
@@ -46,11 +44,11 @@ import type { SessionPort } from "@/runtime/AgentContextTypes.js";
|
|
|
46
44
|
import { pushUiMessageChunkAsSdkEvent } from "@/sdk/StreamEvents.js";
|
|
47
45
|
import {
|
|
48
46
|
persistSdkAssistantResult,
|
|
49
|
-
|
|
47
|
+
touchSessionMetadata,
|
|
50
48
|
} from "@/sdk/SessionPersistence.js";
|
|
51
|
-
import {
|
|
49
|
+
import { createSessionServicePort } from "@/sdk/SessionServicePort.js";
|
|
52
50
|
|
|
53
|
-
type
|
|
51
|
+
type SessionOptions = {
|
|
54
52
|
/**
|
|
55
53
|
* 当前 agent 稳定标识。
|
|
56
54
|
*/
|
|
@@ -98,25 +96,25 @@ type SdkSessionOptions = {
|
|
|
98
96
|
/**
|
|
99
97
|
* SDK 本地 Session。
|
|
100
98
|
*/
|
|
101
|
-
export class
|
|
99
|
+
export class Session {
|
|
102
100
|
readonly id: string;
|
|
103
101
|
readonly agentId: string;
|
|
104
102
|
|
|
105
103
|
private readonly projectRoot: string;
|
|
106
104
|
private readonly tools: Record<string, Tool>;
|
|
107
|
-
private readonly logger:
|
|
108
|
-
private readonly getInstructionSystemBlocks:
|
|
109
|
-
private readonly getServiceSystemBlocks:
|
|
110
|
-
private readonly getPluginSystemBlocks:
|
|
105
|
+
private readonly logger: SessionOptions["logger"];
|
|
106
|
+
private readonly getInstructionSystemBlocks: SessionOptions["getInstructionSystemBlocks"];
|
|
107
|
+
private readonly getServiceSystemBlocks: SessionOptions["getServiceSystemBlocks"];
|
|
108
|
+
private readonly getPluginSystemBlocks: SessionOptions["getPluginSystemBlocks"];
|
|
111
109
|
private readonly historyComposer: JsonlSessionHistoryComposer;
|
|
112
|
-
private readonly
|
|
110
|
+
private readonly executor: Executor;
|
|
113
111
|
private sessionConfig: AgentSessionConfigSnapshot = {};
|
|
114
112
|
private createdAt = Date.now();
|
|
115
113
|
private timezone = resolveSystemTimezone();
|
|
116
114
|
private initializePromise: Promise<this> | null = null;
|
|
117
115
|
private servicePort: SessionPort | null = null;
|
|
118
116
|
|
|
119
|
-
constructor(options:
|
|
117
|
+
constructor(options: SessionOptions) {
|
|
120
118
|
this.id = String(options.sessionId || "").trim();
|
|
121
119
|
this.agentId = String(options.agentId || "").trim();
|
|
122
120
|
this.projectRoot = String(options.projectRoot || "").trim();
|
|
@@ -126,13 +124,13 @@ export class SdkSession {
|
|
|
126
124
|
this.getServiceSystemBlocks = options.getServiceSystemBlocks;
|
|
127
125
|
this.getPluginSystemBlocks = options.getPluginSystemBlocks;
|
|
128
126
|
if (!this.id) {
|
|
129
|
-
throw new Error("
|
|
127
|
+
throw new Error("Session requires a non-empty sessionId");
|
|
130
128
|
}
|
|
131
129
|
if (!this.agentId) {
|
|
132
|
-
throw new Error("
|
|
130
|
+
throw new Error("Session requires a non-empty agentId");
|
|
133
131
|
}
|
|
134
132
|
if (!this.projectRoot) {
|
|
135
|
-
throw new Error("
|
|
133
|
+
throw new Error("Session requires a non-empty projectRoot");
|
|
136
134
|
}
|
|
137
135
|
|
|
138
136
|
const sessionDirPath = getSdkAgentSessionDirPath(
|
|
@@ -157,33 +155,21 @@ export class SdkSession {
|
|
|
157
155
|
},
|
|
158
156
|
});
|
|
159
157
|
|
|
160
|
-
this.
|
|
158
|
+
this.executor = new Executor({
|
|
161
159
|
sessionId: this.id,
|
|
162
160
|
historyComposer: this.historyComposer,
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
systemComposer: new SdkSessionSystemComposer({
|
|
176
|
-
agentId: this.agentId,
|
|
177
|
-
projectRoot: this.projectRoot,
|
|
178
|
-
getSessionCreatedAt: () => this.createdAt,
|
|
179
|
-
getSessionTimezone: () => this.timezone,
|
|
180
|
-
getInstructionSystemBlocks: this.getInstructionSystemBlocks,
|
|
181
|
-
getServiceSystemBlocks: this.getServiceSystemBlocks,
|
|
182
|
-
getPluginSystemBlocks: this.getPluginSystemBlocks,
|
|
183
|
-
}),
|
|
184
|
-
getTools: () => this.tools,
|
|
185
|
-
});
|
|
186
|
-
},
|
|
161
|
+
getModel: () => this.sessionConfig.model,
|
|
162
|
+
logger: this.logger as never,
|
|
163
|
+
systemComposer: new SessionSystemBuilder({
|
|
164
|
+
agentId: this.agentId,
|
|
165
|
+
projectRoot: this.projectRoot,
|
|
166
|
+
getSessionCreatedAt: () => this.createdAt,
|
|
167
|
+
getSessionTimezone: () => this.timezone,
|
|
168
|
+
getInstructionSystemBlocks: this.getInstructionSystemBlocks,
|
|
169
|
+
getServiceSystemBlocks: this.getServiceSystemBlocks,
|
|
170
|
+
getPluginSystemBlocks: this.getPluginSystemBlocks,
|
|
171
|
+
}),
|
|
172
|
+
getTools: () => this.tools,
|
|
187
173
|
});
|
|
188
174
|
}
|
|
189
175
|
|
|
@@ -195,7 +181,7 @@ export class SdkSession {
|
|
|
195
181
|
return await this.initializePromise;
|
|
196
182
|
}
|
|
197
183
|
this.initializePromise = (async () => {
|
|
198
|
-
const metadata = await
|
|
184
|
+
const metadata = await readSessionMetadata({
|
|
199
185
|
projectRoot: this.projectRoot,
|
|
200
186
|
agentId: this.agentId,
|
|
201
187
|
sessionId: this.id,
|
|
@@ -206,7 +192,7 @@ export class SdkSession {
|
|
|
206
192
|
typeof metadata.timezone === "string" && metadata.timezone.trim()
|
|
207
193
|
? metadata.timezone.trim()
|
|
208
194
|
: resolveSystemTimezone();
|
|
209
|
-
await
|
|
195
|
+
await writeSessionMetadata({
|
|
210
196
|
projectRoot: this.projectRoot,
|
|
211
197
|
agentId: this.agentId,
|
|
212
198
|
sessionId: this.id,
|
|
@@ -245,9 +231,9 @@ export class SdkSession {
|
|
|
245
231
|
if (input.model) {
|
|
246
232
|
this.sessionConfig.model = input.model;
|
|
247
233
|
this.sessionConfig.modelLabel = inferModelLabel(input.model);
|
|
248
|
-
this.
|
|
234
|
+
this.executor.clearExecutor();
|
|
249
235
|
}
|
|
250
|
-
await
|
|
236
|
+
await patchSessionModelLabel({
|
|
251
237
|
projectRoot: this.projectRoot,
|
|
252
238
|
agentId: this.agentId,
|
|
253
239
|
sessionId: this.id,
|
|
@@ -264,7 +250,7 @@ export class SdkSession {
|
|
|
264
250
|
*/
|
|
265
251
|
text: string;
|
|
266
252
|
}): Promise<void> {
|
|
267
|
-
await this.
|
|
253
|
+
await this.executor.appendUserMessage({
|
|
268
254
|
text: String(input.text || "").trim(),
|
|
269
255
|
});
|
|
270
256
|
await this.touchMetadata();
|
|
@@ -279,7 +265,7 @@ export class SdkSession {
|
|
|
279
265
|
*/
|
|
280
266
|
text: string;
|
|
281
267
|
}): Promise<void> {
|
|
282
|
-
await this.
|
|
268
|
+
await this.executor.appendAssistantMessage({
|
|
283
269
|
fallbackText: String(input.text || "").trim(),
|
|
284
270
|
});
|
|
285
271
|
await this.touchMetadata();
|
|
@@ -301,7 +287,7 @@ export class SdkSession {
|
|
|
301
287
|
* - 返回结构化快照,不把 system prompt 写入会话历史。
|
|
302
288
|
*/
|
|
303
289
|
async system(): Promise<AgentSessionSystemSnapshot> {
|
|
304
|
-
const blocks = await
|
|
290
|
+
const blocks = await buildSessionSystemBlocks({
|
|
305
291
|
agentId: this.agentId,
|
|
306
292
|
projectRoot: this.projectRoot,
|
|
307
293
|
sessionId: this.id,
|
|
@@ -328,14 +314,14 @@ export class SdkSession {
|
|
|
328
314
|
* 返回当前 session 是否正在执行。
|
|
329
315
|
*/
|
|
330
316
|
isExecuting(): boolean {
|
|
331
|
-
return this.
|
|
317
|
+
return this.executor.isExecuting();
|
|
332
318
|
}
|
|
333
319
|
|
|
334
320
|
/**
|
|
335
321
|
* 清理当前 session 的执行器缓存。
|
|
336
322
|
*/
|
|
337
323
|
clearExecutor(): void {
|
|
338
|
-
this.
|
|
324
|
+
this.executor.clearExecutor();
|
|
339
325
|
}
|
|
340
326
|
|
|
341
327
|
/**
|
|
@@ -347,7 +333,7 @@ export class SdkSession {
|
|
|
347
333
|
throw new Error("session.run requires a non-empty query");
|
|
348
334
|
}
|
|
349
335
|
await this.appendUserMessage({ text: query });
|
|
350
|
-
const result = await this.
|
|
336
|
+
const result = await this.executor.run({
|
|
351
337
|
query,
|
|
352
338
|
});
|
|
353
339
|
await this.persistAssistantResult(result.assistantMessage);
|
|
@@ -375,7 +361,7 @@ export class SdkSession {
|
|
|
375
361
|
|
|
376
362
|
const runPromise = (async () => {
|
|
377
363
|
try {
|
|
378
|
-
const result = await this.
|
|
364
|
+
const result = await this.executor.run({
|
|
379
365
|
query,
|
|
380
366
|
onUiMessageChunkCallback: async (chunk) => {
|
|
381
367
|
pushUiMessageChunkAsSdkEvent({
|
|
@@ -406,7 +392,7 @@ export class SdkSession {
|
|
|
406
392
|
/**
|
|
407
393
|
* 从当前 session 创建一个分叉会话。
|
|
408
394
|
*/
|
|
409
|
-
async fork(input?: AgentSessionForkInput["messageId"]): Promise<
|
|
395
|
+
async fork(input?: AgentSessionForkInput["messageId"]): Promise<Session> {
|
|
410
396
|
const messageId = String(input || "").trim() || undefined;
|
|
411
397
|
const messages = await this.historyComposer.list();
|
|
412
398
|
const forkMessages =
|
|
@@ -424,7 +410,7 @@ export class SdkSession {
|
|
|
424
410
|
return messages.slice(0, targetIndex + 1);
|
|
425
411
|
})();
|
|
426
412
|
|
|
427
|
-
const forked = new
|
|
413
|
+
const forked = new Session({
|
|
428
414
|
agentId: this.agentId,
|
|
429
415
|
projectRoot: this.projectRoot,
|
|
430
416
|
sessionId: `fork-${Date.now()}-${nanoid(8)}`,
|
|
@@ -451,7 +437,7 @@ export class SdkSession {
|
|
|
451
437
|
* 生成当前 session 的元数据快照。
|
|
452
438
|
*/
|
|
453
439
|
async toMetadata(): Promise<AgentSessionMetadata> {
|
|
454
|
-
const meta = await
|
|
440
|
+
const meta = await readSessionMetadata({
|
|
455
441
|
projectRoot: this.projectRoot,
|
|
456
442
|
agentId: this.agentId,
|
|
457
443
|
sessionId: this.id,
|
|
@@ -472,9 +458,9 @@ export class SdkSession {
|
|
|
472
458
|
*/
|
|
473
459
|
getServicePort(): SessionPort {
|
|
474
460
|
if (this.servicePort) return this.servicePort;
|
|
475
|
-
this.servicePort =
|
|
461
|
+
this.servicePort = createSessionServicePort({
|
|
476
462
|
sessionId: this.id,
|
|
477
|
-
|
|
463
|
+
executor: this.executor,
|
|
478
464
|
historyComposer: this.historyComposer,
|
|
479
465
|
touchMetadata: async () => {
|
|
480
466
|
await this.touchMetadata();
|
|
@@ -484,7 +470,7 @@ export class SdkSession {
|
|
|
484
470
|
}
|
|
485
471
|
|
|
486
472
|
private async touchMetadata(): Promise<void> {
|
|
487
|
-
await
|
|
473
|
+
await touchSessionMetadata({
|
|
488
474
|
projectRoot: this.projectRoot,
|
|
489
475
|
agentId: this.agentId,
|
|
490
476
|
sessionId: this.id,
|
|
@@ -500,7 +486,7 @@ export class SdkSession {
|
|
|
500
486
|
agentId: this.agentId,
|
|
501
487
|
sessionId: this.id,
|
|
502
488
|
sessionConfig: this.sessionConfig,
|
|
503
|
-
|
|
489
|
+
executor: this.executor,
|
|
504
490
|
assistantMessage,
|
|
505
491
|
});
|
|
506
492
|
}
|
|
@@ -80,7 +80,7 @@ export function inferModelLabel(
|
|
|
80
80
|
/**
|
|
81
81
|
* 读取当前 session 的 meta.json。
|
|
82
82
|
*/
|
|
83
|
-
export async function
|
|
83
|
+
export async function readSessionMetadata(
|
|
84
84
|
input: ReadSessionMetadataInput,
|
|
85
85
|
): Promise<SessionHistoryMetaV1> {
|
|
86
86
|
const filePath = getSdkAgentSessionMetaPath(
|
|
@@ -142,7 +142,7 @@ export async function readSdkSessionMetadata(
|
|
|
142
142
|
/**
|
|
143
143
|
* 写回当前 session 的 meta.json。
|
|
144
144
|
*/
|
|
145
|
-
export async function
|
|
145
|
+
export async function writeSessionMetadata(
|
|
146
146
|
input: ReadSessionMetadataInput & {
|
|
147
147
|
/**
|
|
148
148
|
* 下一份 meta 数据。
|
|
@@ -162,7 +162,7 @@ export async function writeSdkSessionMetadata(
|
|
|
162
162
|
/**
|
|
163
163
|
* 更新当前 session 的 SDK 配置摘要。
|
|
164
164
|
*/
|
|
165
|
-
export async function
|
|
165
|
+
export async function patchSessionModelLabel(
|
|
166
166
|
input: ReadSessionMetadataInput & {
|
|
167
167
|
/**
|
|
168
168
|
* 当前模型实例。
|
|
@@ -170,7 +170,7 @@ export async function patchSdkSessionModelLabel(
|
|
|
170
170
|
model?: LanguageModel;
|
|
171
171
|
},
|
|
172
172
|
): Promise<SessionHistoryMetaV1> {
|
|
173
|
-
const current = await
|
|
173
|
+
const current = await readSessionMetadata(input);
|
|
174
174
|
const modelLabel = inferModelLabel(input.model);
|
|
175
175
|
const next: SessionHistoryMetaV1 = {
|
|
176
176
|
...current,
|
|
@@ -188,7 +188,7 @@ export async function patchSdkSessionModelLabel(
|
|
|
188
188
|
}
|
|
189
189
|
: {}),
|
|
190
190
|
};
|
|
191
|
-
await
|
|
191
|
+
await writeSessionMetadata({
|
|
192
192
|
...input,
|
|
193
193
|
meta: next,
|
|
194
194
|
});
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 关键点(中文)
|
|
5
5
|
* - 集中处理 session meta 更新时间、模型展示标签与 assistant 消息落盘。
|
|
6
|
-
* - `
|
|
6
|
+
* - `Session` 只负责调用这些能力,不直接拼装 meta 结构。
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { resolveAssistantMessageForPersistence } from "@/service/builtins/chat/runtime/UserVisibleText.js";
|
|
@@ -12,14 +12,14 @@ import type { SessionHistoryMetaV1 } from "@/session/types/SessionHistoryMeta.js
|
|
|
12
12
|
import type { SessionMessageV1 } from "@/session/types/SessionMessages.js";
|
|
13
13
|
import type { AgentSessionConfigSnapshot } from "@/sdk/AgentSdkTypes.js";
|
|
14
14
|
import {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
readSessionMetadata,
|
|
16
|
+
writeSessionMetadata,
|
|
17
17
|
} from "@/sdk/SessionMetadata.js";
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* SDK Session 元数据写入参数。
|
|
21
21
|
*/
|
|
22
|
-
export interface
|
|
22
|
+
export interface TouchSessionMetadataParams {
|
|
23
23
|
/**
|
|
24
24
|
* 当前项目根目录。
|
|
25
25
|
*/
|
|
@@ -42,11 +42,11 @@ export interface TouchSdkSessionMetadataParams {
|
|
|
42
42
|
* Assistant 结果落盘参数。
|
|
43
43
|
*/
|
|
44
44
|
export interface PersistSdkAssistantResultParams
|
|
45
|
-
extends
|
|
45
|
+
extends TouchSessionMetadataParams {
|
|
46
46
|
/**
|
|
47
|
-
* 追加 assistant
|
|
47
|
+
* 追加 assistant 消息的底层执行编排器。
|
|
48
48
|
*/
|
|
49
|
-
|
|
49
|
+
executor: {
|
|
50
50
|
appendAssistantMessage(params: {
|
|
51
51
|
/**
|
|
52
52
|
* 已构造好的完整消息。
|
|
@@ -67,10 +67,10 @@ export interface PersistSdkAssistantResultParams
|
|
|
67
67
|
/**
|
|
68
68
|
* 刷新 SDK session 元数据。
|
|
69
69
|
*/
|
|
70
|
-
export async function
|
|
71
|
-
params:
|
|
70
|
+
export async function touchSessionMetadata(
|
|
71
|
+
params: TouchSessionMetadataParams,
|
|
72
72
|
): Promise<void> {
|
|
73
|
-
const current = await
|
|
73
|
+
const current = await readSessionMetadata({
|
|
74
74
|
projectRoot: params.projectRoot,
|
|
75
75
|
agentId: params.agentId,
|
|
76
76
|
sessionId: params.sessionId,
|
|
@@ -90,7 +90,7 @@ export async function touchSdkSessionMetadata(
|
|
|
90
90
|
}
|
|
91
91
|
: {}),
|
|
92
92
|
};
|
|
93
|
-
await
|
|
93
|
+
await writeSessionMetadata({
|
|
94
94
|
projectRoot: params.projectRoot,
|
|
95
95
|
agentId: params.agentId,
|
|
96
96
|
sessionId: params.sessionId,
|
|
@@ -105,9 +105,9 @@ export async function persistSdkAssistantResult(
|
|
|
105
105
|
params: PersistSdkAssistantResultParams,
|
|
106
106
|
): Promise<void> {
|
|
107
107
|
const persisted = resolveAssistantMessageForPersistence(params.assistantMessage);
|
|
108
|
-
await params.
|
|
108
|
+
await params.executor.appendAssistantMessage({
|
|
109
109
|
...(persisted ? { message: persisted } : {}),
|
|
110
110
|
fallbackText: extractTextFromUiMessage(params.assistantMessage),
|
|
111
111
|
});
|
|
112
|
-
await
|
|
112
|
+
await touchSessionMetadata(params);
|
|
113
113
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 关键点(中文)
|
|
5
5
|
* - 把 SDK 本地 session 适配成 chat service 依赖的 `SessionPort`。
|
|
6
|
-
* - service 侧直接复用底层 `
|
|
6
|
+
* - service 侧直接复用底层 `Executor` 协议,避免 SDK `run()` 包装层重复补写消息。
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type { SessionPort } from "@/runtime/AgentContextTypes.js";
|
|
@@ -11,15 +11,15 @@ import type { SessionPort } from "@/runtime/AgentContextTypes.js";
|
|
|
11
11
|
/**
|
|
12
12
|
* 构造 SDK SessionPort 的参数。
|
|
13
13
|
*/
|
|
14
|
-
export interface
|
|
14
|
+
export interface CreateSessionServicePortParams {
|
|
15
15
|
/**
|
|
16
16
|
* 当前 sessionId。
|
|
17
17
|
*/
|
|
18
18
|
sessionId: string;
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
20
|
+
* 底层执行编排器。
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
executor: Omit<SessionPort, "sessionId" | "getHistoryComposer">;
|
|
23
23
|
/**
|
|
24
24
|
* 当前 session 历史持久化端口。
|
|
25
25
|
*/
|
|
@@ -33,30 +33,30 @@ export interface CreateSdkSessionServicePortParams {
|
|
|
33
33
|
/**
|
|
34
34
|
* 创建供 service 使用的 session 端口。
|
|
35
35
|
*/
|
|
36
|
-
export function
|
|
37
|
-
params:
|
|
36
|
+
export function createSessionServicePort(
|
|
37
|
+
params: CreateSessionServicePortParams,
|
|
38
38
|
): SessionPort {
|
|
39
39
|
return {
|
|
40
40
|
sessionId: params.sessionId,
|
|
41
|
-
getExecutor: () => params.
|
|
41
|
+
getExecutor: () => params.executor.getExecutor(),
|
|
42
42
|
getHistoryComposer: () => params.historyComposer,
|
|
43
43
|
run: async (runParams) => {
|
|
44
|
-
return await params.
|
|
44
|
+
return await params.executor.run(runParams);
|
|
45
45
|
},
|
|
46
46
|
clearExecutor: () => {
|
|
47
|
-
params.
|
|
47
|
+
params.executor.clearExecutor();
|
|
48
48
|
},
|
|
49
49
|
afterSessionUpdatedAsync: async () => {
|
|
50
|
-
await params.
|
|
50
|
+
await params.executor.afterSessionUpdatedAsync();
|
|
51
51
|
},
|
|
52
52
|
appendUserMessage: async (messageParams) => {
|
|
53
|
-
await params.
|
|
53
|
+
await params.executor.appendUserMessage(messageParams);
|
|
54
54
|
await params.touchMetadata();
|
|
55
55
|
},
|
|
56
56
|
appendAssistantMessage: async (messageParams) => {
|
|
57
|
-
await params.
|
|
57
|
+
await params.executor.appendAssistantMessage(messageParams);
|
|
58
58
|
await params.touchMetadata();
|
|
59
59
|
},
|
|
60
|
-
isExecuting: () => params.
|
|
60
|
+
isExecuting: () => params.executor.isExecuting(),
|
|
61
61
|
};
|
|
62
62
|
}
|
|
@@ -18,7 +18,7 @@ import type {
|
|
|
18
18
|
/**
|
|
19
19
|
* 解析 SDK session system blocks 的输入。
|
|
20
20
|
*/
|
|
21
|
-
export interface
|
|
21
|
+
export interface BuildSessionSystemBlocksParams {
|
|
22
22
|
/**
|
|
23
23
|
* 当前 agent 的稳定标识。
|
|
24
24
|
*/
|
|
@@ -60,7 +60,7 @@ export interface ResolveSdkSessionSystemBlocksParams {
|
|
|
60
60
|
getPluginSystemBlocks: () => Promise<AgentSessionSystemBlock[]>;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
type
|
|
63
|
+
type SessionSystemBuilderOptions = {
|
|
64
64
|
/**
|
|
65
65
|
* 当前 agent 的稳定标识。
|
|
66
66
|
*/
|
|
@@ -126,7 +126,7 @@ function normalizeSystemBlocks(
|
|
|
126
126
|
|
|
127
127
|
function createSessionInfo(
|
|
128
128
|
params: Pick<
|
|
129
|
-
|
|
129
|
+
BuildSessionSystemBlocksParams,
|
|
130
130
|
"agentId" | "sessionId" | "projectRoot" | "createdAt" | "timezone"
|
|
131
131
|
>,
|
|
132
132
|
): AgentSessionSystemSessionInfo {
|
|
@@ -161,8 +161,8 @@ function createSessionSystemBlock(
|
|
|
161
161
|
/**
|
|
162
162
|
* 解析 SDK session 当前生效的 system blocks。
|
|
163
163
|
*/
|
|
164
|
-
export async function
|
|
165
|
-
params:
|
|
164
|
+
export async function buildSessionSystemBlocks(
|
|
165
|
+
params: BuildSessionSystemBlocksParams,
|
|
166
166
|
): Promise<AgentSessionSystemBlock[]> {
|
|
167
167
|
const agentId = String(params.agentId || "").trim();
|
|
168
168
|
const projectRoot = String(params.projectRoot || "").trim();
|
|
@@ -170,19 +170,19 @@ export async function resolveSdkSessionSystemBlocks(
|
|
|
170
170
|
const createdAt = Number(params.createdAt || 0);
|
|
171
171
|
const timezone = String(params.timezone || "").trim();
|
|
172
172
|
if (!agentId) {
|
|
173
|
-
throw new Error("
|
|
173
|
+
throw new Error("buildSessionSystemBlocks requires a non-empty agentId");
|
|
174
174
|
}
|
|
175
175
|
if (!projectRoot) {
|
|
176
|
-
throw new Error("
|
|
176
|
+
throw new Error("buildSessionSystemBlocks requires a non-empty projectRoot");
|
|
177
177
|
}
|
|
178
178
|
if (!sessionId) {
|
|
179
|
-
throw new Error("
|
|
179
|
+
throw new Error("buildSessionSystemBlocks requires a non-empty sessionId");
|
|
180
180
|
}
|
|
181
181
|
if (!Number.isFinite(createdAt) || createdAt <= 0) {
|
|
182
|
-
throw new Error("
|
|
182
|
+
throw new Error("buildSessionSystemBlocks requires a valid createdAt");
|
|
183
183
|
}
|
|
184
184
|
if (!timezone) {
|
|
185
|
-
throw new Error("
|
|
185
|
+
throw new Error("buildSessionSystemBlocks requires a non-empty timezone");
|
|
186
186
|
}
|
|
187
187
|
return [
|
|
188
188
|
...normalizeSystemBlocks(params.getInstructionSystemBlocks()),
|
|
@@ -198,10 +198,10 @@ export async function resolveSdkSessionSystemBlocks(
|
|
|
198
198
|
/**
|
|
199
199
|
* 解析 SDK session 当前生效的 system messages。
|
|
200
200
|
*/
|
|
201
|
-
export async function
|
|
202
|
-
params:
|
|
201
|
+
export async function buildSessionSystemMessages(
|
|
202
|
+
params: BuildSessionSystemBlocksParams,
|
|
203
203
|
): Promise<SessionSystemMessage[]> {
|
|
204
|
-
const blocks = await
|
|
204
|
+
const blocks = await buildSessionSystemBlocks(params);
|
|
205
205
|
return blocks.map((block) => ({
|
|
206
206
|
role: "system" as const,
|
|
207
207
|
content: block.content,
|
|
@@ -211,18 +211,18 @@ export async function resolveSdkSessionSystemMessages(
|
|
|
211
211
|
/**
|
|
212
212
|
* SDK Session system composer 实现。
|
|
213
213
|
*/
|
|
214
|
-
export class
|
|
214
|
+
export class SessionSystemBuilder extends SessionSystemComposer {
|
|
215
215
|
readonly name = "sdk_prompt_system";
|
|
216
216
|
|
|
217
217
|
private readonly agentId: string;
|
|
218
218
|
private readonly projectRoot: string;
|
|
219
|
-
private readonly getSessionCreatedAt:
|
|
220
|
-
private readonly getSessionTimezone:
|
|
221
|
-
private readonly getInstructionSystemBlocks:
|
|
222
|
-
private readonly getServiceSystemBlocks:
|
|
223
|
-
private readonly getPluginSystemBlocks:
|
|
219
|
+
private readonly getSessionCreatedAt: SessionSystemBuilderOptions["getSessionCreatedAt"];
|
|
220
|
+
private readonly getSessionTimezone: SessionSystemBuilderOptions["getSessionTimezone"];
|
|
221
|
+
private readonly getInstructionSystemBlocks: SessionSystemBuilderOptions["getInstructionSystemBlocks"];
|
|
222
|
+
private readonly getServiceSystemBlocks: SessionSystemBuilderOptions["getServiceSystemBlocks"];
|
|
223
|
+
private readonly getPluginSystemBlocks: SessionSystemBuilderOptions["getPluginSystemBlocks"];
|
|
224
224
|
|
|
225
|
-
constructor(options:
|
|
225
|
+
constructor(options: SessionSystemBuilderOptions) {
|
|
226
226
|
super();
|
|
227
227
|
this.agentId = String(options.agentId || "").trim();
|
|
228
228
|
this.projectRoot = String(options.projectRoot || "").trim();
|
|
@@ -232,10 +232,10 @@ export class SdkSessionSystemComposer extends SessionSystemComposer {
|
|
|
232
232
|
this.getServiceSystemBlocks = options.getServiceSystemBlocks;
|
|
233
233
|
this.getPluginSystemBlocks = options.getPluginSystemBlocks;
|
|
234
234
|
if (!this.agentId) {
|
|
235
|
-
throw new Error("
|
|
235
|
+
throw new Error("SessionSystemBuilder requires a non-empty agentId");
|
|
236
236
|
}
|
|
237
237
|
if (!this.projectRoot) {
|
|
238
|
-
throw new Error("
|
|
238
|
+
throw new Error("SessionSystemBuilder requires a non-empty projectRoot");
|
|
239
239
|
}
|
|
240
240
|
}
|
|
241
241
|
|
|
@@ -245,9 +245,9 @@ export class SdkSessionSystemComposer extends SessionSystemComposer {
|
|
|
245
245
|
async resolve() {
|
|
246
246
|
const sessionId = String(getSessionRunScope()?.sessionId || "").trim();
|
|
247
247
|
if (!sessionId) {
|
|
248
|
-
throw new Error("
|
|
248
|
+
throw new Error("SessionSystemBuilder.resolve requires a non-empty sessionId");
|
|
249
249
|
}
|
|
250
|
-
return await
|
|
250
|
+
return await buildSessionSystemMessages({
|
|
251
251
|
agentId: this.agentId,
|
|
252
252
|
projectRoot: this.projectRoot,
|
|
253
253
|
sessionId,
|