@downcity/agent 1.1.217 → 1.1.222
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/agent/local/services/AgentSessions.d.ts.map +1 -1
- package/bin/agent/local/services/AgentSessions.js +1 -4
- package/bin/agent/local/services/AgentSessions.js.map +1 -1
- package/bin/agent/remote/RemoteSession.d.ts +9 -3
- package/bin/agent/remote/RemoteSession.d.ts.map +1 -1
- package/bin/agent/remote/RemoteSession.js +19 -4
- package/bin/agent/remote/RemoteSession.js.map +1 -1
- package/bin/agent/remote/RemoteTransport.d.ts +7 -3
- package/bin/agent/remote/RemoteTransport.d.ts.map +1 -1
- package/bin/agent/remote/transports/HttpRemoteAgentTransport.d.ts +5 -2
- package/bin/agent/remote/transports/HttpRemoteAgentTransport.d.ts.map +1 -1
- package/bin/agent/remote/transports/HttpRemoteAgentTransport.js +25 -11
- package/bin/agent/remote/transports/HttpRemoteAgentTransport.js.map +1 -1
- package/bin/agent/remote/transports/RpcRemoteAgentTransport.d.ts +5 -2
- package/bin/agent/remote/transports/RpcRemoteAgentTransport.d.ts.map +1 -1
- package/bin/agent/remote/transports/RpcRemoteAgentTransport.js +5 -2
- package/bin/agent/remote/transports/RpcRemoteAgentTransport.js.map +1 -1
- package/bin/executor/types/SessionRecords.d.ts +2 -0
- package/bin/executor/types/SessionRecords.d.ts.map +1 -1
- package/bin/executor/types/SessionRecords.js.map +1 -1
- package/bin/index.d.ts +5 -2
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +1 -0
- package/bin/index.js.map +1 -1
- package/bin/rpc/Client.d.ts +11 -4
- package/bin/rpc/Client.d.ts.map +1 -1
- package/bin/rpc/Client.js +14 -3
- package/bin/rpc/Client.js.map +1 -1
- package/bin/session/Session.d.ts +13 -3
- package/bin/session/Session.d.ts.map +1 -1
- package/bin/session/Session.js +47 -18
- package/bin/session/Session.js.map +1 -1
- package/bin/session/recorder/JsonlSessionMessageStore.d.ts +45 -0
- package/bin/session/recorder/JsonlSessionMessageStore.d.ts.map +1 -0
- package/bin/session/recorder/JsonlSessionMessageStore.js +127 -0
- package/bin/session/recorder/JsonlSessionMessageStore.js.map +1 -0
- package/bin/session/recorder/SessionMessageCodec.d.ts +20 -0
- package/bin/session/recorder/SessionMessageCodec.d.ts.map +1 -0
- package/bin/session/recorder/SessionMessageCodec.js +252 -0
- package/bin/session/recorder/SessionMessageCodec.js.map +1 -0
- package/bin/session/recorder/SessionMessageReducer.d.ts +12 -0
- package/bin/session/recorder/SessionMessageReducer.d.ts.map +1 -0
- package/bin/session/recorder/SessionMessageReducer.js +109 -0
- package/bin/session/recorder/SessionMessageReducer.js.map +1 -0
- package/bin/session/recorder/SessionRecorder.d.ts +177 -0
- package/bin/session/recorder/SessionRecorder.d.ts.map +1 -0
- package/bin/session/recorder/SessionRecorder.js +610 -0
- package/bin/session/recorder/SessionRecorder.js.map +1 -0
- package/bin/session/recorder/SessionRecorderCompaction.d.ts +18 -0
- package/bin/session/recorder/SessionRecorderCompaction.d.ts.map +1 -0
- package/bin/session/recorder/SessionRecorderCompaction.js +141 -0
- package/bin/session/recorder/SessionRecorderCompaction.js.map +1 -0
- package/bin/session/recorder/SessionRecorderHistoryStore.d.ts +66 -0
- package/bin/session/recorder/SessionRecorderHistoryStore.d.ts.map +1 -0
- package/bin/session/recorder/SessionRecorderHistoryStore.js +195 -0
- package/bin/session/recorder/SessionRecorderHistoryStore.js.map +1 -0
- package/bin/session/runtime/SessionMessageEventHub.d.ts +15 -0
- package/bin/session/runtime/SessionMessageEventHub.d.ts.map +1 -0
- package/bin/session/runtime/SessionMessageEventHub.js +28 -0
- package/bin/session/runtime/SessionMessageEventHub.js.map +1 -0
- package/bin/session/runtime/SessionPromptRuntime.d.ts +7 -13
- package/bin/session/runtime/SessionPromptRuntime.d.ts.map +1 -1
- package/bin/session/runtime/SessionPromptRuntime.js +13 -13
- package/bin/session/runtime/SessionPromptRuntime.js.map +1 -1
- package/bin/session/services/SessionStateService.d.ts +7 -3
- package/bin/session/services/SessionStateService.d.ts.map +1 -1
- package/bin/session/services/SessionStateService.js +132 -72
- package/bin/session/services/SessionStateService.js.map +1 -1
- package/bin/session/services/SessionTurnService.d.ts +4 -0
- package/bin/session/services/SessionTurnService.d.ts.map +1 -1
- package/bin/session/services/SessionTurnService.js +82 -52
- package/bin/session/services/SessionTurnService.js.map +1 -1
- package/bin/session/services/SessionViewService.d.ts +8 -3
- package/bin/session/services/SessionViewService.d.ts.map +1 -1
- package/bin/session/services/SessionViewService.js +10 -5
- package/bin/session/services/SessionViewService.js.map +1 -1
- package/bin/session/storage/Persistence.d.ts +6 -0
- package/bin/session/storage/Persistence.d.ts.map +1 -1
- package/bin/session/storage/Persistence.js +7 -0
- package/bin/session/storage/Persistence.js.map +1 -1
- package/bin/session/storage/RuntimeSessionPort.d.ts +3 -2
- package/bin/session/storage/RuntimeSessionPort.d.ts.map +1 -1
- package/bin/session/storage/RuntimeSessionPort.js.map +1 -1
- package/bin/types/agent/SessionActor.d.ts +10 -4
- package/bin/types/agent/SessionActor.d.ts.map +1 -1
- package/bin/types/rpc/RpcProtocol.d.ts +20 -6
- package/bin/types/rpc/RpcProtocol.d.ts.map +1 -1
- package/bin/types/runtime/agent/AgentContext.d.ts +4 -3
- package/bin/types/runtime/agent/AgentContext.d.ts.map +1 -1
- package/bin/types/runtime/agent/AgentContext.js.map +1 -1
- package/bin/types/sdk/AgentSessionEvent.d.ts +2 -1
- package/bin/types/sdk/AgentSessionEvent.d.ts.map +1 -1
- package/bin/types/session/SessionMessage.d.ts +217 -0
- package/bin/types/session/SessionMessage.d.ts.map +1 -0
- package/bin/types/session/SessionMessage.js +8 -0
- package/bin/types/session/SessionMessage.js.map +1 -0
- package/bin/types/session/SessionMessageMutation.d.ts +92 -0
- package/bin/types/session/SessionMessageMutation.d.ts.map +1 -0
- package/bin/types/session/SessionMessageMutation.js +21 -0
- package/bin/types/session/SessionMessageMutation.js.map +1 -0
- package/package.json +3 -3
- package/scripts/agent-env.test.mjs +4 -4
- package/scripts/session-action-message.test.mjs +98 -27
- package/scripts/session-list-title.test.mjs +6 -19
- package/scripts/session-prompt-runtime.test.mjs +4 -16
- package/scripts/session-recorder.test.mjs +245 -0
- package/scripts/session-title-event.test.mjs +10 -18
- package/src/agent/local/services/AgentSessions.ts +1 -3
- package/src/agent/remote/RemoteSession.ts +35 -7
- package/src/agent/remote/RemoteTransport.ts +17 -6
- package/src/agent/remote/transports/HttpRemoteAgentTransport.ts +45 -13
- package/src/agent/remote/transports/RpcRemoteAgentTransport.ts +19 -6
- package/src/executor/types/SessionRecords.ts +2 -0
- package/src/index.ts +23 -5
- package/src/rpc/Client.ts +29 -8
- package/src/session/Session.ts +65 -31
- package/src/session/recorder/JsonlSessionMessageStore.ts +162 -0
- package/src/session/recorder/SessionMessageCodec.ts +279 -0
- package/src/session/recorder/SessionMessageReducer.ts +155 -0
- package/src/session/recorder/SessionRecorder.ts +812 -0
- package/src/session/recorder/SessionRecorderCompaction.ts +187 -0
- package/src/session/recorder/SessionRecorderHistoryStore.ts +276 -0
- package/src/session/runtime/SessionMessageEventHub.ts +35 -0
- package/src/session/runtime/SessionPromptRuntime.ts +23 -24
- package/src/session/services/SessionStateService.ts +151 -80
- package/src/session/services/SessionTurnService.ts +88 -59
- package/src/session/services/SessionViewService.ts +25 -13
- package/src/session/storage/Persistence.ts +13 -0
- package/src/session/storage/RuntimeSessionPort.ts +6 -4
- package/src/types/agent/SessionActor.ts +26 -6
- package/src/types/rpc/RpcProtocol.ts +20 -6
- package/src/types/runtime/agent/AgentContext.ts +8 -2
- package/src/types/sdk/AgentSessionEvent.ts +2 -0
- package/src/types/session/SessionMessage.ts +247 -0
- package/src/types/session/SessionMessageMutation.ts +121 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/bin/session/SessionEventMapper.d.ts +0 -32
- package/bin/session/SessionEventMapper.d.ts.map +0 -1
- package/bin/session/SessionEventMapper.js +0 -131
- package/bin/session/SessionEventMapper.js.map +0 -1
- package/bin/types/sdk/InternalUiChunkEvent.d.ts +0 -100
- package/bin/types/sdk/InternalUiChunkEvent.d.ts.map +0 -1
- package/bin/types/sdk/InternalUiChunkEvent.js +0 -9
- package/bin/types/sdk/InternalUiChunkEvent.js.map +0 -1
- package/scripts/session-history-archive.test.mjs +0 -188
- package/src/session/SessionEventMapper.ts +0 -148
- package/src/types/sdk/InternalUiChunkEvent.ts +0 -108
|
@@ -17,7 +17,7 @@ import { getSdkAgentSessionArchiveFilePath } from "@/session/storage/Paths.js";
|
|
|
17
17
|
import { ensureSessionTitle } from "@/session/SessionTitle.js";
|
|
18
18
|
import { readSessionMetadata } from "@/session/storage/Metadata.js";
|
|
19
19
|
import { buildSessionSystemBlocks } from "@/session/SessionSystemBuilder.js";
|
|
20
|
-
import type {
|
|
20
|
+
import type { SessionHistoryStore } from "@/executor/store/history/SessionHistoryStore.js";
|
|
21
21
|
import type { SessionSystemComposer } from "@/executor/composer/system/SessionSystemComposer.js";
|
|
22
22
|
import type {
|
|
23
23
|
AgentSessionForkInput,
|
|
@@ -32,6 +32,8 @@ import type { SessionRecordV1 } from "@/executor/types/SessionRecords.js";
|
|
|
32
32
|
import { SessionStateService } from "@/session/services/SessionStateService.js";
|
|
33
33
|
import type { SessionRunContext } from "@/types/executor/SessionRunContext.js";
|
|
34
34
|
import type { Logger } from "@/utils/logger/Logger.js";
|
|
35
|
+
import { SessionRecorder } from "@/session/recorder/SessionRecorder.js";
|
|
36
|
+
import type { SessionMessage } from "@/types/session/SessionMessage.js";
|
|
35
37
|
|
|
36
38
|
type SessionViewServiceOptions<TSession extends Pick<AgentSession, "set">> = {
|
|
37
39
|
/**
|
|
@@ -52,7 +54,10 @@ type SessionViewServiceOptions<TSession extends Pick<AgentSession, "set">> = {
|
|
|
52
54
|
/**
|
|
53
55
|
* 当前 session 历史事实源。
|
|
54
56
|
*/
|
|
55
|
-
history_store:
|
|
57
|
+
history_store: SessionHistoryStore;
|
|
58
|
+
|
|
59
|
+
/** 当前 Session Message Recorder。 */
|
|
60
|
+
recorder: SessionRecorder;
|
|
56
61
|
|
|
57
62
|
/**
|
|
58
63
|
* 当前 session 状态服务。
|
|
@@ -98,7 +103,8 @@ type SessionViewServiceOptions<TSession extends Pick<AgentSession, "set">> = {
|
|
|
98
103
|
*/
|
|
99
104
|
create_fork_session: (session_id: string) => Promise<{
|
|
100
105
|
session: TSession;
|
|
101
|
-
history_store:
|
|
106
|
+
history_store: SessionHistoryStore;
|
|
107
|
+
recorder: SessionRecorder;
|
|
102
108
|
state_service: SessionStateService;
|
|
103
109
|
}>;
|
|
104
110
|
};
|
|
@@ -110,7 +116,8 @@ export class SessionViewService<TSession extends Pick<AgentSession, "set">> {
|
|
|
110
116
|
private readonly agent_id: string;
|
|
111
117
|
private readonly project_root: string;
|
|
112
118
|
private readonly session_id: string;
|
|
113
|
-
private readonly history_store:
|
|
119
|
+
private readonly history_store: SessionHistoryStore;
|
|
120
|
+
private readonly recorder: SessionRecorder;
|
|
114
121
|
private readonly state_service: SessionStateService;
|
|
115
122
|
private readonly logger: Logger;
|
|
116
123
|
private readonly is_executing: SessionViewServiceOptions<TSession>["is_executing"];
|
|
@@ -125,6 +132,7 @@ export class SessionViewService<TSession extends Pick<AgentSession, "set">> {
|
|
|
125
132
|
this.project_root = options.project_root;
|
|
126
133
|
this.session_id = options.session_id;
|
|
127
134
|
this.history_store = options.history_store;
|
|
135
|
+
this.recorder = options.recorder;
|
|
128
136
|
this.state_service = options.state_service;
|
|
129
137
|
this.logger = options.logger;
|
|
130
138
|
this.is_executing = options.is_executing;
|
|
@@ -282,11 +290,14 @@ export class SessionViewService<TSession extends Pick<AgentSession, "set">> {
|
|
|
282
290
|
typeof input === "string"
|
|
283
291
|
? String(input || "").trim() || undefined
|
|
284
292
|
: String(input?.messageId || "").trim() || undefined;
|
|
285
|
-
const
|
|
293
|
+
const message_page = await this.recorder.list_messages({
|
|
294
|
+
limit: 500,
|
|
295
|
+
include_internal: true,
|
|
296
|
+
});
|
|
286
297
|
const fork_messages =
|
|
287
298
|
!message_id
|
|
288
|
-
?
|
|
289
|
-
: this.resolve_fork_messages(
|
|
299
|
+
? message_page.items
|
|
300
|
+
: this.resolve_fork_messages(message_page.items, message_id);
|
|
290
301
|
const action_id = `history-forking:${this.session_id}:${Date.now()}:${nanoid(8)}`;
|
|
291
302
|
|
|
292
303
|
await this.state_service.emit_action_event({
|
|
@@ -333,11 +344,11 @@ export class SessionViewService<TSession extends Pick<AgentSession, "set">> {
|
|
|
333
344
|
}
|
|
334
345
|
|
|
335
346
|
private resolve_fork_messages(
|
|
336
|
-
messages:
|
|
347
|
+
messages: SessionMessage[],
|
|
337
348
|
message_id: string,
|
|
338
|
-
):
|
|
349
|
+
): SessionMessage[] {
|
|
339
350
|
const target_index = messages.findIndex(
|
|
340
|
-
(message) =>
|
|
351
|
+
(message) => message.message_id === message_id,
|
|
341
352
|
);
|
|
342
353
|
if (target_index < 0) {
|
|
343
354
|
throw new Error(
|
|
@@ -349,12 +360,13 @@ export class SessionViewService<TSession extends Pick<AgentSession, "set">> {
|
|
|
349
360
|
|
|
350
361
|
private async append_fork_messages(
|
|
351
362
|
forked_bundle: {
|
|
352
|
-
history_store:
|
|
363
|
+
history_store: SessionHistoryStore;
|
|
364
|
+
recorder: SessionRecorder;
|
|
353
365
|
state_service: SessionStateService;
|
|
354
366
|
},
|
|
355
|
-
messages:
|
|
367
|
+
messages: SessionMessage[],
|
|
356
368
|
): Promise<void> {
|
|
357
|
-
await forked_bundle.
|
|
369
|
+
await forked_bundle.recorder.import_messages(messages);
|
|
358
370
|
}
|
|
359
371
|
|
|
360
372
|
private stringify_system_content(content: unknown): string {
|
|
@@ -39,6 +39,12 @@ export interface TouchSessionMetadataParams {
|
|
|
39
39
|
* 当前 session 内存配置快照。
|
|
40
40
|
*/
|
|
41
41
|
sessionConfig: AgentSessionConfigSnapshot;
|
|
42
|
+
/** 当前 canonical Message 总数。 */
|
|
43
|
+
message_count?: number;
|
|
44
|
+
/** 当前 Message 日志文件字节数。 */
|
|
45
|
+
history_bytes?: number;
|
|
46
|
+
/** 最近一条用户可见 Message 的文本摘要。 */
|
|
47
|
+
preview_text?: string;
|
|
42
48
|
}
|
|
43
49
|
|
|
44
50
|
/**
|
|
@@ -98,6 +104,13 @@ export async function touchSessionMetadata(
|
|
|
98
104
|
modelId: params.sessionConfig.modelId,
|
|
99
105
|
}
|
|
100
106
|
: {}),
|
|
107
|
+
...(typeof params.message_count === "number"
|
|
108
|
+
? { messageCount: params.message_count }
|
|
109
|
+
: {}),
|
|
110
|
+
...(typeof params.history_bytes === "number"
|
|
111
|
+
? { historyBytes: params.history_bytes }
|
|
112
|
+
: {}),
|
|
113
|
+
...(params.preview_text ? { previewText: params.preview_text } : {}),
|
|
101
114
|
};
|
|
102
115
|
await writeSessionMetadata({
|
|
103
116
|
projectRoot: params.projectRoot,
|
|
@@ -12,9 +12,11 @@ import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js"
|
|
|
12
12
|
import type { AgentSessionStopResult } from "@/types/sdk/AgentSessionStop.js";
|
|
13
13
|
import type {
|
|
14
14
|
AgentSessionEvent,
|
|
15
|
-
AgentSessionSubscriber,
|
|
16
|
-
AgentSessionUnsubscribe,
|
|
17
15
|
} from "@/types/sdk/AgentSessionEvent.js";
|
|
16
|
+
import type {
|
|
17
|
+
SessionMessageMutationSubscriber,
|
|
18
|
+
SessionMessageMutationUnsubscribe,
|
|
19
|
+
} from "@/types/session/SessionMessageMutation.js";
|
|
18
20
|
import type { AgentSessionTurnHandle } from "@/types/sdk/AgentSessionTurn.js";
|
|
19
21
|
|
|
20
22
|
/**
|
|
@@ -41,8 +43,8 @@ export interface CreateRuntimeSessionPortParams {
|
|
|
41
43
|
* 订阅当前 session 的 future 事件。
|
|
42
44
|
*/
|
|
43
45
|
subscribe: (
|
|
44
|
-
subscriber:
|
|
45
|
-
) =>
|
|
46
|
+
subscriber: SessionMessageMutationSubscriber,
|
|
47
|
+
) => SessionMessageMutationUnsubscribe;
|
|
46
48
|
/**
|
|
47
49
|
* 发布一条 session runtime 事件。
|
|
48
50
|
*/
|
|
@@ -16,8 +16,6 @@ import type {
|
|
|
16
16
|
AgentListSessionsInput,
|
|
17
17
|
AgentSessionConfigSnapshot,
|
|
18
18
|
AgentSessionForkInput,
|
|
19
|
-
AgentSessionRecordsInput,
|
|
20
|
-
AgentSessionRecordsPage,
|
|
21
19
|
AgentSessionInfo,
|
|
22
20
|
AgentSessionSetInput,
|
|
23
21
|
AgentSessionSummaryPage,
|
|
@@ -27,6 +25,16 @@ import type {
|
|
|
27
25
|
AgentSessionSubscriber,
|
|
28
26
|
AgentSessionUnsubscribe,
|
|
29
27
|
} from "@/types/sdk/AgentSessionEvent.js";
|
|
28
|
+
import type {
|
|
29
|
+
ListSessionMessageChangesInput,
|
|
30
|
+
SessionMessageMutationPage,
|
|
31
|
+
SessionMessageMutationSubscriber,
|
|
32
|
+
SessionMessageMutationUnsubscribe,
|
|
33
|
+
} from "@/types/session/SessionMessageMutation.js";
|
|
34
|
+
import type {
|
|
35
|
+
ListSessionMessagesInput,
|
|
36
|
+
SessionMessagePage,
|
|
37
|
+
} from "@/types/session/SessionMessage.js";
|
|
30
38
|
import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js";
|
|
31
39
|
import type { AgentSessionStopResult } from "@/types/sdk/AgentSessionStop.js";
|
|
32
40
|
import type { AgentSessionTurnHandle } from "@/types/sdk/AgentSessionTurn.js";
|
|
@@ -71,10 +79,22 @@ export interface AgentSessionActor {
|
|
|
71
79
|
stop(): Promise<AgentSessionStopResult>;
|
|
72
80
|
|
|
73
81
|
/** 订阅当前 session 的未来事件。 */
|
|
74
|
-
subscribe(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
subscribe(
|
|
83
|
+
subscriber: SessionMessageMutationSubscriber,
|
|
84
|
+
): SessionMessageMutationUnsubscribe;
|
|
85
|
+
|
|
86
|
+
/** 订阅远程 transport 使用的 Message 与 turn lifecycle 事件。 */
|
|
87
|
+
subscribe_transport(
|
|
88
|
+
subscriber: AgentSessionSubscriber,
|
|
89
|
+
): AgentSessionUnsubscribe;
|
|
90
|
+
|
|
91
|
+
/** 读取当前 session messages 分页。 */
|
|
92
|
+
messages(input?: ListSessionMessagesInput): Promise<SessionMessagePage>;
|
|
93
|
+
|
|
94
|
+
/** 读取指定 cursor 后的 Message Mutation。 */
|
|
95
|
+
message_changes(
|
|
96
|
+
input: ListSessionMessageChangesInput,
|
|
97
|
+
): Promise<SessionMessageMutationPage>;
|
|
78
98
|
|
|
79
99
|
/** 读取当前 session 生效的 system 快照。 */
|
|
80
100
|
system(): Promise<AgentSessionSystemSnapshot>;
|
|
@@ -15,13 +15,14 @@ import type {
|
|
|
15
15
|
AgentArchiveSessionResult,
|
|
16
16
|
AgentArchiveSessionsResult,
|
|
17
17
|
AgentCleanArchiveResult,
|
|
18
|
-
AgentSessionRecordsInput,
|
|
19
18
|
AgentSessionSystemSnapshot,
|
|
20
19
|
AgentSessionSetInput,
|
|
21
20
|
} from "@/types/agent/SessionTypes.js";
|
|
22
21
|
import type { AgentSessionEvent } from "@/types/sdk/AgentSessionEvent.js";
|
|
23
22
|
import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js";
|
|
24
23
|
import type { AgentSessionStopResult } from "@/types/sdk/AgentSessionStop.js";
|
|
24
|
+
import type { ListSessionMessagesInput } from "@/types/session/SessionMessage.js";
|
|
25
|
+
import type { ListSessionMessageChangesInput } from "@/types/session/SessionMessageMutation.js";
|
|
25
26
|
import type { JsonObject, JsonValue } from "@/types/common/Json.js";
|
|
26
27
|
import type { PluginControlAction } from "@/types/plugin/PluginState.js";
|
|
27
28
|
import type { ShellApprovalMode } from "@downcity/shell";
|
|
@@ -126,14 +127,27 @@ export type RpcRequest =
|
|
|
126
127
|
| {
|
|
127
128
|
/** 请求 id,用于匹配响应。 */
|
|
128
129
|
id: string;
|
|
129
|
-
/** 读取 session
|
|
130
|
-
method: "sdk.sessions.
|
|
131
|
-
/**
|
|
130
|
+
/** 读取 session messages。 */
|
|
131
|
+
method: "sdk.sessions.messages";
|
|
132
|
+
/** messages 查询参数。 */
|
|
132
133
|
params: {
|
|
133
134
|
/** 目标 session id。 */
|
|
134
135
|
sessionId: string;
|
|
135
|
-
/**
|
|
136
|
-
input?:
|
|
136
|
+
/** messages 分页参数。 */
|
|
137
|
+
input?: ListSessionMessagesInput;
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
| {
|
|
141
|
+
/** 请求 id,用于匹配响应。 */
|
|
142
|
+
id: string;
|
|
143
|
+
/** 读取 session message mutations。 */
|
|
144
|
+
method: "sdk.sessions.message_changes";
|
|
145
|
+
/** changes 查询参数。 */
|
|
146
|
+
params: {
|
|
147
|
+
/** 目标 session id。 */
|
|
148
|
+
sessionId: string;
|
|
149
|
+
/** Mutation 增量参数。 */
|
|
150
|
+
input: ListSessionMessageChangesInput;
|
|
137
151
|
};
|
|
138
152
|
}
|
|
139
153
|
| {
|
|
@@ -33,6 +33,10 @@ import type {
|
|
|
33
33
|
AgentSessionSubscriber,
|
|
34
34
|
AgentSessionUnsubscribe,
|
|
35
35
|
} from "@/types/sdk/AgentSessionEvent.js";
|
|
36
|
+
import type {
|
|
37
|
+
SessionMessageMutationSubscriber,
|
|
38
|
+
SessionMessageMutationUnsubscribe,
|
|
39
|
+
} from "@/types/session/SessionMessageMutation.js";
|
|
36
40
|
import type { AgentSessionTurnHandle } from "@/types/sdk/AgentSessionTurn.js";
|
|
37
41
|
|
|
38
42
|
/**
|
|
@@ -133,9 +137,11 @@ export interface SessionPort {
|
|
|
133
137
|
*
|
|
134
138
|
* 关键点(中文)
|
|
135
139
|
* - 只广播订阅之后产生的事件。
|
|
136
|
-
* -
|
|
140
|
+
* - 当前 Message snapshot 通过公开 SDK `session.messages()` 读取。
|
|
137
141
|
*/
|
|
138
|
-
subscribe(
|
|
142
|
+
subscribe(
|
|
143
|
+
subscriber: SessionMessageMutationSubscriber,
|
|
144
|
+
): SessionMessageMutationUnsubscribe;
|
|
139
145
|
/**
|
|
140
146
|
* 发布一条 session runtime 事件。
|
|
141
147
|
*
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import type { JsonValue } from "@/types/common/Json.js";
|
|
10
10
|
import type { SessionAssistantStepVisibility } from "@/executor/types/SessionRun.js";
|
|
11
11
|
import type { AgentSessionActionEvent } from "@/types/sdk/AgentSessionAction.js";
|
|
12
|
+
import type { SessionMessageMutation } from "@/types/session/SessionMessageMutation.js";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* 单个 turn 开始事件。
|
|
@@ -346,6 +347,7 @@ export interface AgentSessionErrorEvent {
|
|
|
346
347
|
* Session 订阅可见事件联合类型。
|
|
347
348
|
*/
|
|
348
349
|
export type AgentSessionEvent =
|
|
350
|
+
| SessionMessageMutation
|
|
349
351
|
| AgentSessionTurnStartEvent
|
|
350
352
|
| AgentSessionTextDeltaEvent
|
|
351
353
|
| AgentSessionReasoningDeltaEvent
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session canonical 消息类型。
|
|
3
|
+
*
|
|
4
|
+
* Session 只维护一条由 sequence 排序的消息序列;assistant 的 text、reasoning、tool、file
|
|
5
|
+
* 均为内部 part,不提升为顶层消息。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { JsonObject, JsonValue } from "@/types/common/Json.js";
|
|
9
|
+
|
|
10
|
+
/** Message 默认展示范围。 */
|
|
11
|
+
export type SessionMessageVisibility = "visible" | "internal";
|
|
12
|
+
|
|
13
|
+
/** 从其他 Session 导入时保留的来源身份。 */
|
|
14
|
+
export interface SessionMessageOrigin {
|
|
15
|
+
/** 来源 Session 标识。 */
|
|
16
|
+
session_id: string;
|
|
17
|
+
/** 来源 Message 标识。 */
|
|
18
|
+
message_id: string;
|
|
19
|
+
/** 来源 turn 标识。 */
|
|
20
|
+
turn_id?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Session Message 公共字段。 */
|
|
24
|
+
export interface SessionMessageBase {
|
|
25
|
+
/** 当前 Message 在 Session 内的稳定唯一标识。 */
|
|
26
|
+
message_id: string;
|
|
27
|
+
/** 当前 Message 所属 Session 标识。 */
|
|
28
|
+
session_id: string;
|
|
29
|
+
/** 当前 Message 所属 turn;独立 action 可以省略。 */
|
|
30
|
+
turn_id?: string;
|
|
31
|
+
/** Message 的线性位置;创建后永远不变。 */
|
|
32
|
+
sequence: number;
|
|
33
|
+
/** Message 版本号;每次 Mutation 后递增。 */
|
|
34
|
+
revision: number;
|
|
35
|
+
/** 当前 Message 是否默认对用户可见。 */
|
|
36
|
+
visibility: SessionMessageVisibility;
|
|
37
|
+
/** Message 首次创建时间戳(ms)。 */
|
|
38
|
+
created_at: number;
|
|
39
|
+
/** Message 最近更新时间戳(ms)。 */
|
|
40
|
+
updated_at: number;
|
|
41
|
+
/** 从其他 Session 导入时的来源信息。 */
|
|
42
|
+
origin?: SessionMessageOrigin;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** User 文本 part。 */
|
|
46
|
+
export interface SessionUserTextPart {
|
|
47
|
+
/** User Message 内稳定的 part 标识。 */
|
|
48
|
+
part_id: string;
|
|
49
|
+
/** part 类型固定为 text。 */
|
|
50
|
+
type: "text";
|
|
51
|
+
/** 用户输入文本。 */
|
|
52
|
+
text: string;
|
|
53
|
+
/** User 文本已经完整,不参与流式更新。 */
|
|
54
|
+
state: "done";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** User 文件 part。 */
|
|
58
|
+
export interface SessionUserFilePart {
|
|
59
|
+
/** User Message 内稳定的 part 标识。 */
|
|
60
|
+
part_id: string;
|
|
61
|
+
/** part 类型固定为 file。 */
|
|
62
|
+
type: "file";
|
|
63
|
+
/** 文件可读取地址或 data URL。 */
|
|
64
|
+
url: string;
|
|
65
|
+
/** 文件 MIME 类型。 */
|
|
66
|
+
media_type: string;
|
|
67
|
+
/** 可选原始文件名。 */
|
|
68
|
+
filename?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** User 结构化数据 part。 */
|
|
72
|
+
export interface SessionUserDataPart {
|
|
73
|
+
/** User Message 内稳定的 part 标识。 */
|
|
74
|
+
part_id: string;
|
|
75
|
+
/** part 类型固定为 data。 */
|
|
76
|
+
type: "data";
|
|
77
|
+
/** 对应 AI SDK 的 data-* 类型名称。 */
|
|
78
|
+
data_type: `data-${string}` | string;
|
|
79
|
+
/** 可 JSON 序列化的数据。 */
|
|
80
|
+
data: JsonValue;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** User Message part。 */
|
|
84
|
+
export type SessionUserMessagePart =
|
|
85
|
+
| SessionUserTextPart
|
|
86
|
+
| SessionUserFilePart
|
|
87
|
+
| SessionUserDataPart;
|
|
88
|
+
|
|
89
|
+
/** User 顶层 Message。 */
|
|
90
|
+
export interface SessionUserMessage extends SessionMessageBase {
|
|
91
|
+
/** Message 类型固定为 user。 */
|
|
92
|
+
type: "user";
|
|
93
|
+
/** 普通 prompt 或当前 turn 中的 steering 输入。 */
|
|
94
|
+
input_type: "prompt" | "steer";
|
|
95
|
+
/** 用户消息的结构化 parts。 */
|
|
96
|
+
parts: SessionUserMessagePart[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Assistant 文本或推理 part。 */
|
|
100
|
+
export interface SessionAssistantTextPart {
|
|
101
|
+
/** Assistant Message 内稳定的 part 标识。 */
|
|
102
|
+
part_id: string;
|
|
103
|
+
/** part 是可见文本或推理文本。 */
|
|
104
|
+
type: "text" | "reasoning";
|
|
105
|
+
/** 当前已经累计的完整文本。 */
|
|
106
|
+
text: string;
|
|
107
|
+
/** 文本 part 是否已经结束。 */
|
|
108
|
+
state: "streaming" | "done";
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Assistant 工具 part。 */
|
|
112
|
+
export interface SessionAssistantToolPart {
|
|
113
|
+
/** Assistant Message 内稳定的 part 标识。 */
|
|
114
|
+
part_id: string;
|
|
115
|
+
/** part 类型固定为 tool。 */
|
|
116
|
+
type: "tool";
|
|
117
|
+
/** 模型工具调用稳定标识。 */
|
|
118
|
+
tool_call_id: string;
|
|
119
|
+
/** 工具注册名称。 */
|
|
120
|
+
tool_name: string;
|
|
121
|
+
/** 工具当前生命周期状态。 */
|
|
122
|
+
state: "input-streaming" | "approval-required" | "running" | "completed" | "failed";
|
|
123
|
+
/** 流式接收中的参数原文。 */
|
|
124
|
+
input_text?: string;
|
|
125
|
+
/** 收敛后的结构化输入。 */
|
|
126
|
+
input?: JsonValue;
|
|
127
|
+
/** 工具成功输出。 */
|
|
128
|
+
output?: JsonValue;
|
|
129
|
+
/** 工具失败信息。 */
|
|
130
|
+
error?: string;
|
|
131
|
+
/** 工具审批请求标识。 */
|
|
132
|
+
approval_id?: string;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Assistant 文件 part。 */
|
|
136
|
+
export interface SessionAssistantFilePart {
|
|
137
|
+
/** Assistant Message 内稳定的 part 标识。 */
|
|
138
|
+
part_id: string;
|
|
139
|
+
/** part 类型固定为 file。 */
|
|
140
|
+
type: "file";
|
|
141
|
+
/** 文件 MIME 类型。 */
|
|
142
|
+
media_type: string;
|
|
143
|
+
/** 文件可读取地址或 data URL。 */
|
|
144
|
+
url: string;
|
|
145
|
+
/** 可选原始文件名。 */
|
|
146
|
+
filename?: string;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Assistant 结构化数据 part。 */
|
|
150
|
+
export interface SessionAssistantDataPart {
|
|
151
|
+
/** Assistant Message 内稳定的 part 标识。 */
|
|
152
|
+
part_id: string;
|
|
153
|
+
/** part 类型固定为 data。 */
|
|
154
|
+
type: "data";
|
|
155
|
+
/** 对应 AI SDK 的 data-* 类型名称。 */
|
|
156
|
+
data_type: `data-${string}` | string;
|
|
157
|
+
/** 可 JSON 序列化的数据。 */
|
|
158
|
+
data: JsonValue;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Assistant Message part。 */
|
|
162
|
+
export type SessionAssistantMessagePart =
|
|
163
|
+
| SessionAssistantTextPart
|
|
164
|
+
| SessionAssistantToolPart
|
|
165
|
+
| SessionAssistantFilePart
|
|
166
|
+
| SessionAssistantDataPart;
|
|
167
|
+
|
|
168
|
+
/** Assistant 顶层 Message。 */
|
|
169
|
+
export interface SessionAssistantMessage extends SessionMessageBase {
|
|
170
|
+
/** Message 类型固定为 assistant。 */
|
|
171
|
+
type: "assistant";
|
|
172
|
+
/** 普通 assistant segment 或内部 compact summary。 */
|
|
173
|
+
kind: "normal" | "summary";
|
|
174
|
+
/** Assistant 在所属 turn 内的 segment 序号,从一开始。 */
|
|
175
|
+
segment_index: number;
|
|
176
|
+
/** Assistant 当前执行状态。 */
|
|
177
|
+
status: "streaming" | "completed" | "stopped" | "failed";
|
|
178
|
+
/** Assistant 内按真实生成顺序保存的 parts。 */
|
|
179
|
+
parts: SessionAssistantMessagePart[];
|
|
180
|
+
/** Summary 已覆盖到的来源 Message 标识。 */
|
|
181
|
+
summary_through_message_id?: string;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Action 顶层 Message。 */
|
|
185
|
+
export interface SessionActionMessage extends SessionMessageBase {
|
|
186
|
+
/** Message 类型固定为 action。 */
|
|
187
|
+
type: "action";
|
|
188
|
+
/** Action 业务类型。 */
|
|
189
|
+
action_type: string;
|
|
190
|
+
/** Action 当前状态。 */
|
|
191
|
+
status: "running" | "completed" | "failed";
|
|
192
|
+
/** Action 展示标题。 */
|
|
193
|
+
title: string;
|
|
194
|
+
/** Action 展示描述。 */
|
|
195
|
+
description?: string;
|
|
196
|
+
/** Action 附加结构化信息。 */
|
|
197
|
+
data?: JsonObject;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Error 顶层 Message。 */
|
|
201
|
+
export interface SessionErrorMessage extends SessionMessageBase {
|
|
202
|
+
/** Message 类型固定为 error。 */
|
|
203
|
+
type: "error";
|
|
204
|
+
/** 错误影响范围。 */
|
|
205
|
+
scope: "session" | "turn";
|
|
206
|
+
/** 稳定错误码。 */
|
|
207
|
+
code: string;
|
|
208
|
+
/** 用户可见错误信息。 */
|
|
209
|
+
message: string;
|
|
210
|
+
/** 当前错误是否允许重试恢复。 */
|
|
211
|
+
recoverable: boolean;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Session 唯一顶层 Message 联合类型。 */
|
|
215
|
+
export type SessionMessage =
|
|
216
|
+
| SessionUserMessage
|
|
217
|
+
| SessionAssistantMessage
|
|
218
|
+
| SessionActionMessage
|
|
219
|
+
| SessionErrorMessage;
|
|
220
|
+
|
|
221
|
+
/** 读取 Session Message snapshot 的分页输入。 */
|
|
222
|
+
export interface ListSessionMessagesInput {
|
|
223
|
+
/** 单页数量上限。 */
|
|
224
|
+
limit?: number;
|
|
225
|
+
/** 上一页返回的透明游标。 */
|
|
226
|
+
cursor?: string;
|
|
227
|
+
/** 只返回 sequence 严格小于该值的 Message,供向前翻页。 */
|
|
228
|
+
before_sequence?: number;
|
|
229
|
+
/** 是否包含 internal Message。 */
|
|
230
|
+
include_internal?: boolean;
|
|
231
|
+
/** 只返回不大于该 sequence 的 Message。 */
|
|
232
|
+
through_sequence?: number;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Session Message snapshot 分页结果。 */
|
|
236
|
+
export interface SessionMessagePage {
|
|
237
|
+
/** 当前页按 sequence 升序排列的 Message。 */
|
|
238
|
+
items: SessionMessage[];
|
|
239
|
+
/** 当前过滤条件下的 Message 总数。 */
|
|
240
|
+
total: number;
|
|
241
|
+
/** 下一页透明游标。 */
|
|
242
|
+
next_cursor?: string;
|
|
243
|
+
/** 是否仍有更多 Message。 */
|
|
244
|
+
has_more: boolean;
|
|
245
|
+
/** 当前 snapshot 已包含的最大 commit_sequence。 */
|
|
246
|
+
latest_commit_sequence: number;
|
|
247
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Message Mutation 与增量读取类型。
|
|
3
|
+
*
|
|
4
|
+
* Mutation 只按变化层级分成 message、part、delta 三种 variant;`type` 在各 variant
|
|
5
|
+
* 内直接表达对应的 Message 或 Part 类型。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { SessionAssistantMessagePart, SessionMessage } from "@/types/session/SessionMessage.js";
|
|
9
|
+
|
|
10
|
+
/** Mutation 公共字段。 */
|
|
11
|
+
export interface SessionMessageMutationBase {
|
|
12
|
+
/** Mutation 稳定唯一标识。 */
|
|
13
|
+
mutation_id: string;
|
|
14
|
+
/** Mutation 在 Session 内的单调提交序号。 */
|
|
15
|
+
commit_sequence: number;
|
|
16
|
+
/** Mutation 目标 Message。 */
|
|
17
|
+
message_id: string;
|
|
18
|
+
/** 应用 Mutation 后的 Message revision。 */
|
|
19
|
+
revision: number;
|
|
20
|
+
/** Mutation 所属 Session。 */
|
|
21
|
+
session_id: string;
|
|
22
|
+
/** Mutation 所属 turn。 */
|
|
23
|
+
turn_id?: string;
|
|
24
|
+
/** Mutation 创建时间戳(ms)。 */
|
|
25
|
+
created_at: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 顶层 Message 创建或状态更新 Mutation。 */
|
|
29
|
+
export type SessionMessageSnapshotMutation = {
|
|
30
|
+
[TType in SessionMessage["type"]]: SessionMessageMutationBase & {
|
|
31
|
+
/** Mutation 层级固定为 message。 */
|
|
32
|
+
variant: "message";
|
|
33
|
+
/** 当前完整 Message 类型。 */
|
|
34
|
+
type: TType;
|
|
35
|
+
/** Message 在线性 Session 中的固定位置。 */
|
|
36
|
+
sequence: number;
|
|
37
|
+
/** 创建或更新后的完整 Message 快照。 */
|
|
38
|
+
message: Extract<SessionMessage, { type: TType }>;
|
|
39
|
+
};
|
|
40
|
+
}[SessionMessage["type"]];
|
|
41
|
+
|
|
42
|
+
/** Assistant Part 创建或状态更新 Mutation。 */
|
|
43
|
+
export type SessionPartSnapshotMutation = {
|
|
44
|
+
[TType in SessionAssistantMessagePart["type"]]: SessionMessageMutationBase & {
|
|
45
|
+
/** Mutation 层级固定为 part。 */
|
|
46
|
+
variant: "part";
|
|
47
|
+
/** 当前完整 Part 类型。 */
|
|
48
|
+
type: TType;
|
|
49
|
+
/** 被创建或更新的 Part 标识。 */
|
|
50
|
+
part_id: string;
|
|
51
|
+
/** 创建或更新后的完整 Part 快照。 */
|
|
52
|
+
part: SessionAssistantMessagePart & { type: TType };
|
|
53
|
+
};
|
|
54
|
+
}[SessionAssistantMessagePart["type"]];
|
|
55
|
+
|
|
56
|
+
/** Assistant 文本或推理原始增量 Mutation。 */
|
|
57
|
+
export interface SessionDeltaMutation extends SessionMessageMutationBase {
|
|
58
|
+
/** Mutation 层级固定为 delta。 */
|
|
59
|
+
variant: "delta";
|
|
60
|
+
/** Delta 类型只允许 text 或 reasoning。 */
|
|
61
|
+
type: "text" | "reasoning";
|
|
62
|
+
/** Delta 所属 Part 标识。 */
|
|
63
|
+
part_id: string;
|
|
64
|
+
/** 本次模型新增的原始文本,不是累计全文。 */
|
|
65
|
+
delta: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 全部合法 Session Message Mutation。 */
|
|
69
|
+
export type SessionMessageMutation =
|
|
70
|
+
| SessionMessageSnapshotMutation
|
|
71
|
+
| SessionPartSnapshotMutation
|
|
72
|
+
| SessionDeltaMutation;
|
|
73
|
+
|
|
74
|
+
/** 从 commit cursor 增量读取 Mutation 的输入。 */
|
|
75
|
+
export interface ListSessionMessageChangesInput {
|
|
76
|
+
/** 只返回 commit_sequence 严格大于该值的 Mutation。 */
|
|
77
|
+
after_commit_sequence: number;
|
|
78
|
+
/** 单页数量上限。 */
|
|
79
|
+
limit?: number;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Mutation 增量分页结果。 */
|
|
83
|
+
export interface SessionMessageMutationPage {
|
|
84
|
+
/** 当前页按 commit_sequence 升序排列的 Mutation。 */
|
|
85
|
+
items: SessionMessageMutation[];
|
|
86
|
+
/** 当前页之后是否仍有 Mutation。 */
|
|
87
|
+
has_more: boolean;
|
|
88
|
+
/** 下一次增量读取应使用的 commit_sequence。 */
|
|
89
|
+
next_commit_sequence: number;
|
|
90
|
+
/** 当前 Session 已提交的最大 commit_sequence。 */
|
|
91
|
+
latest_commit_sequence: number;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Session Mutation 订阅回调。 */
|
|
95
|
+
export type SessionMessageMutationSubscriber = (mutation: SessionMessageMutation) => void;
|
|
96
|
+
|
|
97
|
+
/** 取消 Session Mutation 订阅的函数。 */
|
|
98
|
+
export type SessionMessageMutationUnsubscribe = () => void;
|
|
99
|
+
|
|
100
|
+
/** 判断未知事件是否为 Session Message Mutation。 */
|
|
101
|
+
export function is_session_message_mutation(input: unknown): input is SessionMessageMutation {
|
|
102
|
+
if (!input || typeof input !== "object") return false;
|
|
103
|
+
const candidate = input as {
|
|
104
|
+
variant?: unknown;
|
|
105
|
+
type?: unknown;
|
|
106
|
+
mutation_id?: unknown;
|
|
107
|
+
commit_sequence?: unknown;
|
|
108
|
+
};
|
|
109
|
+
if (
|
|
110
|
+
typeof candidate.mutation_id !== "string" ||
|
|
111
|
+
typeof candidate.commit_sequence !== "number" ||
|
|
112
|
+
typeof candidate.type !== "string"
|
|
113
|
+
) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
return (
|
|
117
|
+
candidate.variant === "message" ||
|
|
118
|
+
candidate.variant === "part" ||
|
|
119
|
+
candidate.variant === "delta"
|
|
120
|
+
);
|
|
121
|
+
}
|