@downcity/agent 1.1.200 → 1.1.203
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/Agent.d.ts +3 -7
- package/bin/agent/local/Agent.d.ts.map +1 -1
- package/bin/agent/local/Agent.js +14 -19
- package/bin/agent/local/Agent.js.map +1 -1
- package/bin/agent/local/services/{AgentSessionManager.d.ts → AgentSessions.d.ts} +12 -17
- package/bin/agent/local/services/AgentSessions.d.ts.map +1 -0
- package/bin/agent/local/services/{AgentSessionManager.js → AgentSessions.js} +11 -26
- package/bin/agent/local/services/AgentSessions.js.map +1 -0
- package/bin/agent/remote/RemoteAgent.d.ts +2 -9
- package/bin/agent/remote/RemoteAgent.d.ts.map +1 -1
- package/bin/agent/remote/RemoteAgent.js +40 -22
- package/bin/agent/remote/RemoteAgent.js.map +1 -1
- package/bin/agent/remote/RemoteSession.d.ts +5 -5
- package/bin/agent/remote/RemoteSession.d.ts.map +1 -1
- package/bin/agent/remote/RemoteSession.js +5 -3
- package/bin/agent/remote/RemoteSession.js.map +1 -1
- package/bin/agent/remote/RemoteTransport.d.ts +2 -2
- package/bin/agent/remote/transports/HttpRemoteAgentTransport.d.ts +2 -2
- package/bin/agent/remote/transports/HttpRemoteAgentTransport.js +5 -5
- package/bin/agent/remote/transports/RpcRemoteAgentTransport.d.ts +2 -2
- package/bin/agent/remote/transports/RpcRemoteAgentTransport.js +2 -2
- package/bin/executor/Executor.d.ts +5 -5
- package/bin/executor/Executor.d.ts.map +1 -1
- package/bin/executor/Executor.js +22 -13
- package/bin/executor/Executor.js.map +1 -1
- package/bin/executor/SessionRunScope.d.ts +1 -1
- package/bin/executor/SessionRunScope.d.ts.map +1 -1
- package/bin/executor/composer/compaction/SessionCompactionComposer.d.ts +3 -3
- package/bin/executor/composer/compaction/SessionCompactionComposer.d.ts.map +1 -1
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.d.ts +4 -5
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.d.ts.map +1 -1
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.js +56 -16
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.js.map +1 -1
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.d.ts +27 -0
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.d.ts.map +1 -0
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.js +181 -0
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.js.map +1 -0
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.d.ts +45 -0
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.d.ts.map +1 -0
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.js +144 -0
- package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.js.map +1 -0
- package/bin/executor/composer/context/LocalSessionContextComposer.d.ts +3 -3
- package/bin/executor/composer/context/LocalSessionContextComposer.d.ts.map +1 -1
- package/bin/executor/composer/context/SessionContextComposer.d.ts +3 -3
- package/bin/executor/composer/context/SessionContextComposer.d.ts.map +1 -1
- package/bin/executor/composer/history/SessionHistoryComposer.d.ts +2 -2
- package/bin/executor/composer/history/SessionHistoryComposer.d.ts.map +1 -1
- package/bin/executor/composer/history/SessionHistoryWriter.d.ts +5 -5
- package/bin/executor/composer/history/SessionHistoryWriter.d.ts.map +1 -1
- package/bin/executor/composer/history/SessionHistoryWriter.js +6 -6
- package/bin/executor/composer/history/SessionHistoryWriter.js.map +1 -1
- package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.d.ts +2 -2
- package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.d.ts.map +1 -1
- package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.js +5 -5
- package/bin/executor/composer/history/jsonl/JsonlSessionHistoryComposer.js.map +1 -1
- package/bin/executor/core-engine/CoreEngineMessageState.d.ts +5 -5
- package/bin/executor/core-engine/CoreEngineMessageState.d.ts.map +1 -1
- package/bin/executor/core-engine/CoreEngineMessageState.js.map +1 -1
- package/bin/executor/core-engine/CoreEngineRunner.js.map +1 -1
- package/bin/executor/core-engine/CoreEngineSignals.d.ts +4 -4
- package/bin/executor/core-engine/CoreEngineSignals.d.ts.map +1 -1
- package/bin/executor/core-engine/CoreEngineSignals.js.map +1 -1
- package/bin/executor/core-engine/CoreEngineUiStreamCollector.d.ts +3 -3
- package/bin/executor/core-engine/CoreEngineUiStreamCollector.d.ts.map +1 -1
- package/bin/executor/core-engine/CoreEngineUiStreamCollector.js.map +1 -1
- package/bin/executor/messages/AssistantResultPersistence.d.ts +5 -5
- package/bin/executor/messages/AssistantResultPersistence.d.ts.map +1 -1
- package/bin/executor/messages/AssistantResultPersistence.js +1 -1
- package/bin/executor/messages/AssistantResultPersistence.js.map +1 -1
- package/bin/executor/messages/SessionAttachmentMapper.d.ts +3 -3
- package/bin/executor/messages/SessionAttachmentMapper.d.ts.map +1 -1
- package/bin/executor/messages/SessionAttachmentMapper.js.map +1 -1
- package/bin/executor/messages/SessionMessageCodec.d.ts +4 -4
- package/bin/executor/messages/SessionMessageCodec.d.ts.map +1 -1
- package/bin/executor/messages/SessionMessageCodec.js +5 -5
- package/bin/executor/messages/SessionMessageCodec.js.map +1 -1
- package/bin/executor/messages/SessionMessageLog.d.ts +3 -3
- package/bin/executor/messages/SessionMessageLog.d.ts.map +1 -1
- package/bin/executor/messages/SessionMessageLog.js.map +1 -1
- package/bin/executor/messages/SessionStepEventMapper.d.ts +2 -2
- package/bin/executor/messages/SessionStepEventMapper.d.ts.map +1 -1
- package/bin/executor/messages/SessionStepEventMapper.js.map +1 -1
- package/bin/executor/messages/UserVisibleText.d.ts +3 -3
- package/bin/executor/messages/UserVisibleText.d.ts.map +1 -1
- package/bin/executor/messages/UserVisibleText.js +3 -3
- package/bin/executor/messages/UserVisibleText.js.map +1 -1
- package/bin/executor/services/ExecutorInflightService.d.ts +2 -2
- package/bin/executor/services/ExecutorInflightService.d.ts.map +1 -1
- package/bin/executor/services/ExecutorInflightService.js +4 -4
- package/bin/executor/services/ExecutorInflightService.js.map +1 -1
- package/bin/executor/store/history/SessionHistoryStore.d.ts +24 -21
- package/bin/executor/store/history/SessionHistoryStore.d.ts.map +1 -1
- package/bin/executor/store/history/SessionHistoryStore.js +3 -3
- package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.d.ts +18 -19
- package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.d.ts.map +1 -1
- package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.js +33 -36
- package/bin/executor/store/history/jsonl/JsonlSessionHistoryStore.js.map +1 -1
- package/bin/executor/tools/plugin/PluginToolDefinition.d.ts +1 -7
- package/bin/executor/tools/plugin/PluginToolDefinition.d.ts.map +1 -1
- package/bin/executor/tools/plugin/PluginToolSchemas.d.ts +4 -6
- package/bin/executor/tools/plugin/PluginToolSchemas.d.ts.map +1 -1
- package/bin/executor/tools/plugin/PluginToolSchemas.js +24 -10
- package/bin/executor/tools/plugin/PluginToolSchemas.js.map +1 -1
- package/bin/executor/types/{SessionMessages.d.ts → SessionRecords.d.ts} +53 -28
- package/bin/executor/types/SessionRecords.d.ts.map +1 -0
- package/bin/executor/types/SessionRecords.js +52 -0
- package/bin/executor/types/SessionRecords.js.map +1 -0
- package/bin/executor/types/SessionRun.d.ts +3 -3
- package/bin/executor/types/SessionRun.d.ts.map +1 -1
- package/bin/index.d.ts +1 -1
- package/bin/index.d.ts.map +1 -1
- package/bin/rpc/Client.d.ts +5 -5
- package/bin/rpc/Client.js +4 -4
- package/bin/session/Session.d.ts +6 -6
- package/bin/session/Session.d.ts.map +1 -1
- package/bin/session/Session.js +8 -8
- package/bin/session/Session.js.map +1 -1
- package/bin/session/SessionTitle.d.ts +2 -2
- package/bin/session/SessionTitle.d.ts.map +1 -1
- package/bin/session/SessionTitle.js +3 -3
- package/bin/session/SessionTitle.js.map +1 -1
- package/bin/session/browse/Browse.d.ts +12 -12
- package/bin/session/browse/Browse.d.ts.map +1 -1
- package/bin/session/browse/Browse.js +9 -9
- package/bin/session/browse/Browse.js.map +1 -1
- package/bin/session/index.d.ts +1 -1
- package/bin/session/index.js +1 -1
- package/bin/session/runtime/SessionPromptRuntime.d.ts +3 -3
- package/bin/session/runtime/SessionPromptRuntime.d.ts.map +1 -1
- package/bin/session/services/SessionStateService.d.ts +7 -8
- package/bin/session/services/SessionStateService.d.ts.map +1 -1
- package/bin/session/services/SessionStateService.js +24 -34
- package/bin/session/services/SessionStateService.js.map +1 -1
- package/bin/session/services/SessionViewService.d.ts +4 -4
- package/bin/session/services/SessionViewService.js +12 -12
- package/bin/session/services/SessionViewService.js.map +1 -1
- package/bin/session/storage/Persistence.d.ts +4 -4
- package/bin/session/storage/Persistence.d.ts.map +1 -1
- package/bin/session/storage/RuntimeSessionPort.d.ts +2 -2
- package/bin/session/storage/RuntimeSessionPort.d.ts.map +1 -1
- package/bin/session/storage/RuntimeSessionPort.js +4 -4
- package/bin/session/storage/RuntimeSessionPort.js.map +1 -1
- package/bin/types/agent/AgentTypes.d.ts +2 -2
- package/bin/types/agent/AgentTypes.d.ts.map +1 -1
- package/bin/types/agent/SessionActor.d.ts +11 -11
- package/bin/types/agent/SessionActor.d.ts.map +1 -1
- package/bin/types/agent/SessionTypes.d.ts +12 -12
- package/bin/types/agent/SessionTypes.d.ts.map +1 -1
- package/bin/types/executor/SessionRunContext.d.ts +2 -2
- package/bin/types/executor/SessionRunContext.d.ts.map +1 -1
- package/bin/types/rpc/RpcProtocol.d.ts +6 -6
- package/bin/types/runtime/agent/AgentContext.d.ts +7 -7
- package/bin/types/runtime/agent/AgentContext.d.ts.map +1 -1
- package/bin/types/sdk/AgentSessionAction.d.ts +8 -49
- package/bin/types/sdk/AgentSessionAction.d.ts.map +1 -1
- package/bin/types/sdk/AgentSessionAction.js +4 -4
- package/bin/types/sdk/AgentSessionTurn.d.ts +2 -2
- package/bin/types/sdk/AgentSessionTurn.d.ts.map +1 -1
- package/package.json +3 -3
- package/scripts/agent-ready.test.mjs +1 -1
- package/scripts/city-model-tool-loop.test.mjs +36 -4
- package/scripts/plugin-tool-bridge.test.mjs +13 -0
- package/scripts/session-action-message.test.mjs +71 -27
- package/scripts/session-compaction-prompts.test.mjs +97 -0
- package/scripts/session-history-archive.test.mjs +8 -8
- package/scripts/session-history-inflight.test.mjs +10 -10
- package/scripts/session-list-title.test.mjs +9 -9
- package/scripts/session-title-event.test.mjs +12 -10
- package/src/agent/local/Agent.ts +16 -22
- package/src/agent/local/services/{AgentSessionManager.ts → AgentSessions.ts} +16 -32
- package/src/agent/remote/RemoteAgent.ts +58 -23
- package/src/agent/remote/RemoteSession.ts +10 -8
- package/src/agent/remote/RemoteTransport.ts +5 -5
- package/src/agent/remote/transports/HttpRemoteAgentTransport.ts +10 -10
- package/src/agent/remote/transports/RpcRemoteAgentTransport.ts +6 -6
- package/src/executor/Executor.ts +30 -19
- package/src/executor/SessionRunScope.ts +1 -1
- package/src/executor/composer/compaction/SessionCompactionComposer.ts +3 -3
- package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionExecutor.ts +73 -29
- package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.ts +197 -0
- package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.ts +162 -0
- package/src/executor/composer/context/LocalSessionContextComposer.ts +5 -5
- package/src/executor/composer/context/SessionContextComposer.ts +5 -5
- package/src/executor/composer/history/SessionHistoryComposer.ts +2 -2
- package/src/executor/composer/history/SessionHistoryWriter.ts +10 -10
- package/src/executor/composer/history/jsonl/JsonlSessionHistoryComposer.ts +13 -13
- package/src/executor/core-engine/CoreEngineMessageState.ts +8 -8
- package/src/executor/core-engine/CoreEngineRunner.ts +7 -7
- package/src/executor/core-engine/CoreEngineSignals.ts +7 -7
- package/src/executor/core-engine/CoreEngineUiStreamCollector.ts +5 -5
- package/src/executor/messages/AssistantResultPersistence.ts +8 -8
- package/src/executor/messages/SessionAttachmentMapper.ts +11 -11
- package/src/executor/messages/SessionMessageCodec.ts +10 -10
- package/src/executor/messages/SessionMessageLog.ts +3 -3
- package/src/executor/messages/SessionStepEventMapper.ts +5 -5
- package/src/executor/messages/UserVisibleText.ts +9 -9
- package/src/executor/services/ExecutorInflightService.ts +7 -7
- package/src/executor/store/history/SessionHistoryStore.ts +26 -23
- package/src/executor/store/history/jsonl/JsonlSessionHistoryStore.ts +58 -62
- package/src/executor/tools/plugin/PluginToolSchemas.ts +24 -10
- package/src/executor/types/{SessionMessages.ts → SessionRecords.ts} +71 -40
- package/src/executor/types/SessionRun.ts +5 -5
- package/src/index.ts +4 -4
- package/src/rpc/Client.ts +9 -9
- package/src/session/Session.ts +10 -10
- package/src/session/SessionTitle.ts +7 -7
- package/src/session/browse/Browse.ts +38 -38
- package/src/session/index.ts +1 -1
- package/src/session/runtime/SessionPromptRuntime.ts +6 -6
- package/src/session/services/SessionStateService.ts +40 -47
- package/src/session/services/SessionTurnService.ts +4 -4
- package/src/session/services/SessionViewService.ts +21 -21
- package/src/session/storage/Persistence.ts +6 -6
- package/src/session/storage/RuntimeSessionPort.ts +6 -6
- package/src/types/agent/AgentTypes.ts +4 -4
- package/src/types/agent/SessionActor.ts +12 -12
- package/src/types/agent/SessionTypes.ts +12 -12
- package/src/types/executor/SessionRunContext.ts +2 -2
- package/src/types/rpc/RpcProtocol.ts +6 -6
- package/src/types/runtime/agent/AgentContext.ts +8 -8
- package/src/types/sdk/AgentSessionAction.ts +12 -56
- package/src/types/sdk/AgentSessionTurn.ts +2 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/bin/agent/local/services/AgentSessionManager.d.ts.map +0 -1
- package/bin/agent/local/services/AgentSessionManager.js.map +0 -1
- package/bin/executor/types/SessionMessages.d.ts.map +0 -1
- package/bin/executor/types/SessionMessages.js +0 -46
- package/bin/executor/types/SessionMessages.js.map +0 -1
|
@@ -8,7 +8,15 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import type {
|
|
11
|
-
|
|
11
|
+
AgentArchiveSessionInput,
|
|
12
|
+
AgentArchiveSessionsInput,
|
|
13
|
+
AgentArchiveSessionResult,
|
|
14
|
+
AgentArchiveSessionsResult,
|
|
15
|
+
AgentCleanArchiveResult,
|
|
16
|
+
AgentCreateSessionInput,
|
|
17
|
+
AgentListSessionsInput,
|
|
18
|
+
AgentSessionSummaryPage,
|
|
19
|
+
AgentSessions,
|
|
12
20
|
RemoteAgentPluginActionInput,
|
|
13
21
|
RemoteAgentPluginActionResult,
|
|
14
22
|
RemoteAgentOptions,
|
|
@@ -30,6 +38,8 @@ import { create_remote_agent_transport } from "@/agent/remote/TransportFactory.j
|
|
|
30
38
|
* RemoteAgent:远程 Agent 客户端。
|
|
31
39
|
*/
|
|
32
40
|
export class RemoteAgent {
|
|
41
|
+
readonly sessions: AgentSessions<RemoteAgentSession>;
|
|
42
|
+
|
|
33
43
|
private readonly transport: RemoteAgentTransport;
|
|
34
44
|
|
|
35
45
|
constructor(options: RemoteAgentOptions) {
|
|
@@ -38,6 +48,7 @@ export class RemoteAgent {
|
|
|
38
48
|
throw new Error("RemoteAgent requires a non-empty url");
|
|
39
49
|
}
|
|
40
50
|
this.transport = create_remote_agent_transport(url, options.token);
|
|
51
|
+
this.sessions = new RemoteAgentSessions(this.transport);
|
|
41
52
|
}
|
|
42
53
|
|
|
43
54
|
/**
|
|
@@ -121,27 +132,51 @@ export class RemoteAgent {
|
|
|
121
132
|
await this.transport.close?.();
|
|
122
133
|
}
|
|
123
134
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 远程 Agent session 集合入口。
|
|
139
|
+
*
|
|
140
|
+
* 关键点(中文)
|
|
141
|
+
* - `RemoteAgentTransport` 只负责协议传输,不直接暴露为用户 SDK API。
|
|
142
|
+
* - 这里把远程传输包装成和本地 `agent.sessions` 一致的方法命名。
|
|
143
|
+
*/
|
|
144
|
+
class RemoteAgentSessions implements AgentSessions<RemoteAgentSession> {
|
|
145
|
+
private readonly transport: RemoteAgentTransport;
|
|
146
|
+
|
|
147
|
+
constructor(transport: RemoteAgentTransport) {
|
|
148
|
+
this.transport = transport;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 新建一个远程 session。 */
|
|
152
|
+
async create(input?: AgentCreateSessionInput): Promise<RemoteAgentSession> {
|
|
153
|
+
const info = await this.transport.create_session(input);
|
|
154
|
+
return new RemoteSession(this.transport, info);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** 获取一个远程 session。 */
|
|
158
|
+
async get(session_id: string): Promise<RemoteAgentSession> {
|
|
159
|
+
const info = await this.transport.get_info(session_id);
|
|
160
|
+
return new RemoteSession(this.transport, info);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 列出远程 session 摘要页。 */
|
|
164
|
+
async list(input?: AgentListSessionsInput): Promise<AgentSessionSummaryPage> {
|
|
165
|
+
return await this.transport.list_sessions(input);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 归档一个远程 session。 */
|
|
169
|
+
async archive(input: AgentArchiveSessionInput): Promise<AgentArchiveSessionResult> {
|
|
170
|
+
return await this.transport.archive_session(input);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** 列出远程已归档 session。 */
|
|
174
|
+
async archived(input?: AgentArchiveSessionsInput): Promise<AgentArchiveSessionsResult> {
|
|
175
|
+
return await this.transport.archive_sessions(input);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 永久清空远程已归档 session。 */
|
|
179
|
+
async clean_archive(): Promise<AgentCleanArchiveResult> {
|
|
180
|
+
return await this.transport.clean_archive();
|
|
146
181
|
}
|
|
147
182
|
}
|
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
|
-
AgentSession,
|
|
11
10
|
AgentSessionConfigSnapshot,
|
|
12
11
|
AgentSessionForkInput,
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
AgentSessionRecordsInput,
|
|
13
|
+
AgentSessionRecordsPage,
|
|
15
14
|
AgentSessionInfo,
|
|
16
15
|
AgentSessionSetInput,
|
|
17
16
|
AgentSessionSystemSnapshot,
|
|
17
|
+
RemoteAgentSession,
|
|
18
18
|
} from "@/types/agent/AgentTypes.js";
|
|
19
19
|
import { isAgentSessionPromptInputEmpty } from "@/types/sdk/AgentSessionPrompt.js";
|
|
20
20
|
import type { AgentSessionStopResult } from "@/types/sdk/AgentSessionStop.js";
|
|
@@ -53,7 +53,7 @@ type RemoteTurnLifecycle = {
|
|
|
53
53
|
/**
|
|
54
54
|
* 远程 Session 客户端。
|
|
55
55
|
*/
|
|
56
|
-
export class RemoteSession implements
|
|
56
|
+
export class RemoteSession implements RemoteAgentSession {
|
|
57
57
|
readonly id: string;
|
|
58
58
|
readonly agentId: string;
|
|
59
59
|
readonly config: AgentSessionConfigSnapshot;
|
|
@@ -87,7 +87,7 @@ export class RemoteSession implements AgentSession {
|
|
|
87
87
|
/**
|
|
88
88
|
* 读取当前远程 session 详情。
|
|
89
89
|
*/
|
|
90
|
-
async
|
|
90
|
+
async get_info(): Promise<AgentSessionInfo> {
|
|
91
91
|
return await this.transport.get_info(this.id);
|
|
92
92
|
}
|
|
93
93
|
|
|
@@ -135,8 +135,8 @@ export class RemoteSession implements AgentSession {
|
|
|
135
135
|
/**
|
|
136
136
|
* 读取远程消息历史。
|
|
137
137
|
*/
|
|
138
|
-
async
|
|
139
|
-
return await this.transport.
|
|
138
|
+
async records(input?: AgentSessionRecordsInput): Promise<AgentSessionRecordsPage> {
|
|
139
|
+
return await this.transport.records(this.id, input);
|
|
140
140
|
}
|
|
141
141
|
|
|
142
142
|
/**
|
|
@@ -149,7 +149,7 @@ export class RemoteSession implements AgentSession {
|
|
|
149
149
|
/**
|
|
150
150
|
* 分叉远程 session。
|
|
151
151
|
*/
|
|
152
|
-
async fork(input?: AgentSessionForkInput | string): Promise<
|
|
152
|
+
async fork(input?: AgentSessionForkInput | string): Promise<RemoteAgentSession> {
|
|
153
153
|
const info = await this.transport.fork(this.id, input);
|
|
154
154
|
return new RemoteSession(this.transport, info);
|
|
155
155
|
}
|
|
@@ -296,6 +296,8 @@ function extract_turn_id(event: AgentSessionEvent): string | null {
|
|
|
296
296
|
case "assistant-step":
|
|
297
297
|
case "turn-finish":
|
|
298
298
|
return event.turnId;
|
|
299
|
+
case "action":
|
|
300
|
+
return event.metadata.turnId || null;
|
|
299
301
|
default:
|
|
300
302
|
return null;
|
|
301
303
|
}
|
|
@@ -15,8 +15,8 @@ import type {
|
|
|
15
15
|
AgentArchiveSessionsResult,
|
|
16
16
|
AgentCleanArchiveResult,
|
|
17
17
|
AgentSessionForkInput,
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
AgentSessionRecordsInput,
|
|
19
|
+
AgentSessionRecordsPage,
|
|
20
20
|
AgentSessionInfo,
|
|
21
21
|
AgentSessionSummaryPage,
|
|
22
22
|
AgentSessionSystemSnapshot,
|
|
@@ -63,10 +63,10 @@ export type RemoteSessionTransport = {
|
|
|
63
63
|
on_event: (event: AgentSessionEvent) => void;
|
|
64
64
|
}): Promise<TransportSubscription>;
|
|
65
65
|
/** 读取 history。 */
|
|
66
|
-
|
|
66
|
+
records(
|
|
67
67
|
session_id: string,
|
|
68
|
-
input?:
|
|
69
|
-
): Promise<
|
|
68
|
+
input?: AgentSessionRecordsInput,
|
|
69
|
+
): Promise<AgentSessionRecordsPage>;
|
|
70
70
|
/** 读取 system snapshot。 */
|
|
71
71
|
system(session_id: string): Promise<AgentSessionSystemSnapshot>;
|
|
72
72
|
/** 分叉 session。 */
|
|
@@ -15,8 +15,8 @@ import type {
|
|
|
15
15
|
AgentArchiveSessionsResult,
|
|
16
16
|
AgentCleanArchiveResult,
|
|
17
17
|
AgentSessionForkInput,
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
AgentSessionRecordsInput,
|
|
19
|
+
AgentSessionRecordsPage,
|
|
20
20
|
AgentSessionInfo,
|
|
21
21
|
AgentSessionSummaryPage,
|
|
22
22
|
AgentSessionSystemSnapshot,
|
|
@@ -184,10 +184,10 @@ export class HttpRemoteAgentTransport implements RemoteAgentTransport {
|
|
|
184
184
|
};
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
async
|
|
187
|
+
async records(
|
|
188
188
|
session_id: string,
|
|
189
|
-
input?:
|
|
190
|
-
): Promise<
|
|
189
|
+
input?: AgentSessionRecordsInput,
|
|
190
|
+
): Promise<AgentSessionRecordsPage> {
|
|
191
191
|
const query = new URLSearchParams();
|
|
192
192
|
if (input?.limit !== undefined) query.set("limit", String(input.limit));
|
|
193
193
|
if (input?.cursor) query.set("cursor", input.cursor);
|
|
@@ -197,19 +197,19 @@ export class HttpRemoteAgentTransport implements RemoteAgentTransport {
|
|
|
197
197
|
const payload = await read_http_json<{
|
|
198
198
|
success?: boolean;
|
|
199
199
|
error?: string;
|
|
200
|
-
|
|
200
|
+
records?: AgentSessionRecordsPage;
|
|
201
201
|
}>(
|
|
202
|
-
`${this.base_url}/api/sdk/sessions/${encodeURIComponent(session_id)}/
|
|
202
|
+
`${this.base_url}/api/sdk/sessions/${encodeURIComponent(session_id)}/records${
|
|
203
203
|
query.size > 0 ? `?${query.toString()}` : ""
|
|
204
204
|
}`,
|
|
205
205
|
{
|
|
206
206
|
headers: this.headers(),
|
|
207
207
|
},
|
|
208
208
|
);
|
|
209
|
-
if (!payload.success || !payload.
|
|
210
|
-
throw new Error(String(payload.error || "Remote session
|
|
209
|
+
if (!payload.success || !payload.records || !Array.isArray(payload.records.items)) {
|
|
210
|
+
throw new Error(String(payload.error || "Remote session records failed"));
|
|
211
211
|
}
|
|
212
|
-
return payload.
|
|
212
|
+
return payload.records;
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
async system(session_id: string): Promise<AgentSessionSystemSnapshot> {
|
|
@@ -15,8 +15,8 @@ import type {
|
|
|
15
15
|
AgentArchiveSessionsResult,
|
|
16
16
|
AgentCleanArchiveResult,
|
|
17
17
|
AgentSessionForkInput,
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
AgentSessionRecordsInput,
|
|
19
|
+
AgentSessionRecordsPage,
|
|
20
20
|
AgentSessionInfo,
|
|
21
21
|
AgentSessionSummaryPage,
|
|
22
22
|
AgentSessionSystemSnapshot,
|
|
@@ -89,11 +89,11 @@ export class RpcRemoteAgentTransport implements RemoteAgentTransport {
|
|
|
89
89
|
};
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
async
|
|
92
|
+
async records(
|
|
93
93
|
session_id: string,
|
|
94
|
-
input?:
|
|
95
|
-
): Promise<
|
|
96
|
-
return await this.client.
|
|
94
|
+
input?: AgentSessionRecordsInput,
|
|
95
|
+
): Promise<AgentSessionRecordsPage> {
|
|
96
|
+
return await this.client.get_session_records({
|
|
97
97
|
session_id,
|
|
98
98
|
input,
|
|
99
99
|
});
|
package/src/executor/Executor.ts
CHANGED
|
@@ -25,10 +25,13 @@ import { ExecutorRecoveryPolicy } from "@executor/services/ExecutorRecoveryPolic
|
|
|
25
25
|
import { generateId } from "@/utils/Id.js";
|
|
26
26
|
import type { Logger } from "@/utils/logger/Logger.js";
|
|
27
27
|
import type { JsonObject } from "@/types/common/Json.js";
|
|
28
|
-
import type {
|
|
28
|
+
import type {
|
|
29
|
+
SessionActionRecordV1,
|
|
30
|
+
SessionRecordV1,
|
|
31
|
+
} from "@/executor/types/SessionRecords.js";
|
|
32
|
+
import { to_session_action_record } from "@/executor/types/SessionRecords.js";
|
|
29
33
|
import type { SessionExecutor } from "@/executor/types/SessionExecutor.js";
|
|
30
34
|
import type { SessionRunContext } from "@/types/executor/SessionRunContext.js";
|
|
31
|
-
import type { AgentSessionActionRecord } from "@/types/sdk/AgentSessionAction.js";
|
|
32
35
|
import type {
|
|
33
36
|
SessionExecuteInput,
|
|
34
37
|
SessionRunResult,
|
|
@@ -212,23 +215,23 @@ export class Executor implements SessionExecutor {
|
|
|
212
215
|
/**
|
|
213
216
|
* 追加一条 user 消息。
|
|
214
217
|
*/
|
|
215
|
-
async
|
|
216
|
-
message?:
|
|
218
|
+
async append_user_message(params: {
|
|
219
|
+
message?: SessionRecordV1 | null;
|
|
217
220
|
text?: string;
|
|
218
221
|
extra?: JsonObject;
|
|
219
222
|
}): Promise<void> {
|
|
220
|
-
await this.historyWriter.
|
|
223
|
+
await this.historyWriter.append_user_message(params);
|
|
221
224
|
}
|
|
222
225
|
|
|
223
226
|
/**
|
|
224
227
|
* 追加一条 assistant 消息。
|
|
225
228
|
*/
|
|
226
|
-
async
|
|
227
|
-
message?:
|
|
229
|
+
async append_assistant_message(params: {
|
|
230
|
+
message?: SessionRecordV1 | null;
|
|
228
231
|
fallbackText?: string;
|
|
229
232
|
extra?: JsonObject;
|
|
230
233
|
}): Promise<void> {
|
|
231
|
-
await this.historyWriter.
|
|
234
|
+
await this.historyWriter.append_assistant_message(params);
|
|
232
235
|
}
|
|
233
236
|
|
|
234
237
|
/**
|
|
@@ -372,7 +375,7 @@ export class Executor implements SessionExecutor {
|
|
|
372
375
|
}
|
|
373
376
|
|
|
374
377
|
const emit_compaction_action = async (
|
|
375
|
-
action:
|
|
378
|
+
action: SessionActionRecordV1,
|
|
376
379
|
): Promise<void> => {
|
|
377
380
|
if (action.state === "running") {
|
|
378
381
|
compaction_action_id = action.id;
|
|
@@ -389,13 +392,18 @@ export class Executor implements SessionExecutor {
|
|
|
389
392
|
});
|
|
390
393
|
} catch (error) {
|
|
391
394
|
await this.emitAction(run_context, {
|
|
395
|
+
type: "action",
|
|
392
396
|
id:
|
|
393
397
|
compaction_action_id ||
|
|
394
398
|
`compacting:${this.sessionId}:failed:${Date.now()}:${generateId()}`,
|
|
395
|
-
title: "Session
|
|
399
|
+
title: "Session records compact failed",
|
|
396
400
|
description: error instanceof Error ? error.message : String(error),
|
|
397
401
|
state: "failed",
|
|
398
|
-
|
|
402
|
+
metadata: {
|
|
403
|
+
v: 1,
|
|
404
|
+
ts: Date.now(),
|
|
405
|
+
sessionId: this.sessionId,
|
|
406
|
+
},
|
|
399
407
|
});
|
|
400
408
|
// 压缩失败不阻断主流程,继续使用当前历史消息执行。
|
|
401
409
|
}
|
|
@@ -514,21 +522,24 @@ export class Executor implements SessionExecutor {
|
|
|
514
522
|
*/
|
|
515
523
|
private async emitAction(
|
|
516
524
|
run_context: SessionRunContext,
|
|
517
|
-
action:
|
|
525
|
+
action: SessionActionRecordV1,
|
|
518
526
|
): Promise<void> {
|
|
519
527
|
if (typeof run_context.onActionCallback !== "function") return;
|
|
520
528
|
const action_id =
|
|
521
529
|
String(action.id || "").trim() ||
|
|
522
530
|
`action:${this.sessionId}:${Date.now()}:${generateId()}`;
|
|
523
|
-
|
|
524
|
-
type: "action",
|
|
525
|
-
sessionId: run_context.sessionId || this.sessionId,
|
|
531
|
+
const event = to_session_action_record({
|
|
526
532
|
...action,
|
|
527
533
|
id: action_id,
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
:
|
|
531
|
-
|
|
534
|
+
metadata: {
|
|
535
|
+
...action.metadata,
|
|
536
|
+
sessionId: run_context.sessionId || this.sessionId,
|
|
537
|
+
...(run_context.turnId && !action.metadata.turnId
|
|
538
|
+
? { turnId: run_context.turnId }
|
|
539
|
+
: {}),
|
|
540
|
+
},
|
|
541
|
+
}, run_context.sessionId || this.sessionId);
|
|
542
|
+
await run_context.onActionCallback(event);
|
|
532
543
|
}
|
|
533
544
|
|
|
534
545
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
2
|
import type { FileUIPart } from "ai";
|
|
3
|
-
import type { SessionUserMessageV1 } from "@/executor/types/
|
|
3
|
+
import type { SessionUserMessageV1 } from "@/executor/types/SessionRecords.js";
|
|
4
4
|
import type { SessionRunContext } from "@/types/executor/SessionRunContext.js";
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import type { LanguageModel, SystemModelMessage } from "ai";
|
|
10
10
|
import type { SessionHistoryStore } from "@/executor/store/history/SessionHistoryStore.js";
|
|
11
|
-
import type {
|
|
11
|
+
import type { SessionActionRecordV1 } from "@/executor/types/SessionRecords.js";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* compaction Composer 执行输入。
|
|
@@ -39,9 +39,9 @@ export type SessionCompactionInput = {
|
|
|
39
39
|
*
|
|
40
40
|
* 说明(中文)
|
|
41
41
|
* - 默认 compaction Composer 可在真正开始压缩时发布 action。
|
|
42
|
-
* - 回调由上层 Session 负责转成订阅事件与 action
|
|
42
|
+
* - 回调由上层 Session 负责转成订阅事件与 action record。
|
|
43
43
|
*/
|
|
44
|
-
onAction?: (action:
|
|
44
|
+
onAction?: (action: SessionActionRecordV1) => Promise<void>;
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
/**
|
|
@@ -18,16 +18,22 @@ import {
|
|
|
18
18
|
import { generateId } from "@/utils/Id.js";
|
|
19
19
|
import { getLogger } from "@/utils/logger/Logger.js";
|
|
20
20
|
import type {
|
|
21
|
-
|
|
21
|
+
SessionActionRecordV1,
|
|
22
|
+
SessionRecordV1,
|
|
22
23
|
SessionMetadataV1,
|
|
23
|
-
|
|
24
|
-
} from "@/executor/types/
|
|
24
|
+
SessionMessageRecordV1,
|
|
25
|
+
} from "@/executor/types/SessionRecords.js";
|
|
25
26
|
import {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
} from "@/executor/types/SessionMessages.js";
|
|
27
|
+
is_session_message_record,
|
|
28
|
+
} from "@/executor/types/SessionRecords.js";
|
|
29
29
|
import type { SessionHistoryMetaV1 } from "@/executor/types/SessionHistoryMeta.js";
|
|
30
|
-
import
|
|
30
|
+
import {
|
|
31
|
+
append_session_compaction_file_operations,
|
|
32
|
+
build_initial_session_compaction_prompt,
|
|
33
|
+
build_update_session_compaction_prompt,
|
|
34
|
+
SESSION_COMPACTION_SYSTEM_PROMPT,
|
|
35
|
+
} from "@executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.js";
|
|
36
|
+
import { format_session_compaction_file_operations } from "@executor/composer/compaction/jsonl/JsonlSessionCompactionFileOperations.js";
|
|
31
37
|
|
|
32
38
|
export type SessionCompactParams = {
|
|
33
39
|
model: LanguageModel;
|
|
@@ -36,19 +42,19 @@ export type SessionCompactParams = {
|
|
|
36
42
|
maxInputTokensApprox: number;
|
|
37
43
|
archiveOnCompact: boolean;
|
|
38
44
|
compactRatio: number;
|
|
39
|
-
onAction?: (action:
|
|
45
|
+
onAction?: (action: SessionActionRecordV1) => Promise<void>;
|
|
40
46
|
};
|
|
41
47
|
|
|
42
48
|
type SessionCompactDeps = {
|
|
43
49
|
rootPath: string;
|
|
44
50
|
sessionId: string;
|
|
45
51
|
withWriteLock: <T>(fn: () => Promise<T>) => Promise<T>;
|
|
46
|
-
loadAll: () => Promise<
|
|
52
|
+
loadAll: () => Promise<SessionRecordV1[]>;
|
|
47
53
|
createSummaryMessage: (params: {
|
|
48
54
|
text: string;
|
|
49
55
|
archiveId?: string;
|
|
50
56
|
sourceRange?: SessionMetadataV1["sourceRange"];
|
|
51
|
-
}) =>
|
|
57
|
+
}) => SessionRecordV1;
|
|
52
58
|
getArchiveDirPath: () => string;
|
|
53
59
|
getMessagesFilePath: () => string;
|
|
54
60
|
readMetaUnsafe: () => Promise<SessionHistoryMetaV1>;
|
|
@@ -71,12 +77,18 @@ export async function compactSessionMessagesIfNeeded(
|
|
|
71
77
|
let action_started = false;
|
|
72
78
|
|
|
73
79
|
const publish_action = async (
|
|
74
|
-
action: Omit<
|
|
80
|
+
action: Omit<SessionActionRecordV1, "type" | "id" | "metadata">,
|
|
75
81
|
): Promise<void> => {
|
|
76
82
|
if (typeof params.onAction !== "function") return;
|
|
77
83
|
await params.onAction({
|
|
84
|
+
type: "action",
|
|
78
85
|
id: action_id,
|
|
79
86
|
...action,
|
|
87
|
+
metadata: {
|
|
88
|
+
v: 1,
|
|
89
|
+
ts: Date.now(),
|
|
90
|
+
sessionId: deps.sessionId,
|
|
91
|
+
},
|
|
80
92
|
});
|
|
81
93
|
};
|
|
82
94
|
|
|
@@ -84,10 +96,10 @@ export async function compactSessionMessagesIfNeeded(
|
|
|
84
96
|
// phase 1:snapshot(短锁)
|
|
85
97
|
// - 仅负责拿一致性快照,不做耗时的模型调用。
|
|
86
98
|
// - 目的是把锁持有时间降到最低。
|
|
87
|
-
let snapshot:
|
|
99
|
+
let snapshot: SessionMessageRecordV1[] = [];
|
|
88
100
|
let snapshotTailId = "";
|
|
89
101
|
await deps.withWriteLock(async () => {
|
|
90
|
-
snapshot = (await deps.loadAll()).filter(
|
|
102
|
+
snapshot = (await deps.loadAll()).filter(is_session_message_record);
|
|
91
103
|
snapshotTailId =
|
|
92
104
|
snapshot.length > 0
|
|
93
105
|
? String(snapshot[snapshot.length - 1].id || "")
|
|
@@ -121,16 +133,27 @@ export async function compactSessionMessagesIfNeeded(
|
|
|
121
133
|
|
|
122
134
|
action_started = true;
|
|
123
135
|
await publish_action({
|
|
124
|
-
title: "Compacting session
|
|
136
|
+
title: "Compacting session records",
|
|
125
137
|
state: "running",
|
|
126
138
|
});
|
|
127
139
|
|
|
128
|
-
const
|
|
140
|
+
const previous_summary = extract_existing_compaction_summary(older[0]);
|
|
141
|
+
const conversation_messages = previous_summary ? older.slice(1) : older;
|
|
142
|
+
const olderTextAll = extractPlainTextFromMessages(conversation_messages);
|
|
129
143
|
const maxOlderChars = 24_000;
|
|
130
144
|
const olderText =
|
|
131
145
|
olderTextAll.length > maxOlderChars
|
|
132
146
|
? "(注意:更早历史过长,已截断保留末尾)\n" + olderTextAll.slice(-maxOlderChars)
|
|
133
147
|
: olderTextAll;
|
|
148
|
+
const prompt = previous_summary
|
|
149
|
+
? build_update_session_compaction_prompt({
|
|
150
|
+
previous_summary,
|
|
151
|
+
new_conversation_text: olderText || "(none)",
|
|
152
|
+
})
|
|
153
|
+
: build_initial_session_compaction_prompt({
|
|
154
|
+
conversation_text: olderText || "(none)",
|
|
155
|
+
});
|
|
156
|
+
const file_operations_xml = format_session_compaction_file_operations(older);
|
|
134
157
|
|
|
135
158
|
// phase 1.5:生成摘要(不持锁)
|
|
136
159
|
// - 这一步最耗时,必须在锁外执行,避免阻塞 append。
|
|
@@ -141,18 +164,15 @@ export async function compactSessionMessagesIfNeeded(
|
|
|
141
164
|
system: [
|
|
142
165
|
{
|
|
143
166
|
role: "system",
|
|
144
|
-
content:
|
|
145
|
-
"你是对话压缩助手。请把更早的对话历史压缩成“可持续复用”的工作摘要。\n" +
|
|
146
|
-
"要求:\n" +
|
|
147
|
-
"- 输出中文\n" +
|
|
148
|
-
"- 不要复述无关细节,不要输出工具原始日志\n" +
|
|
149
|
-
"- 必须包含:已确认事实/用户偏好约束/已做决策/未完成事项\n" +
|
|
150
|
-
"- 使用 Markdown 列表,控制在 300~800 字",
|
|
167
|
+
content: SESSION_COMPACTION_SYSTEM_PROMPT,
|
|
151
168
|
},
|
|
152
169
|
],
|
|
153
|
-
prompt
|
|
170
|
+
prompt,
|
|
171
|
+
});
|
|
172
|
+
summary = append_session_compaction_file_operations({
|
|
173
|
+
summary: String(r.text || "").trim(),
|
|
174
|
+
file_operations_xml,
|
|
154
175
|
});
|
|
155
|
-
summary = String(r.text || "").trim();
|
|
156
176
|
} catch (e) {
|
|
157
177
|
await logger.log(
|
|
158
178
|
"warn",
|
|
@@ -162,9 +182,12 @@ export async function compactSessionMessagesIfNeeded(
|
|
|
162
182
|
error: String(e),
|
|
163
183
|
},
|
|
164
184
|
);
|
|
165
|
-
summary =
|
|
185
|
+
summary = append_session_compaction_file_operations({
|
|
186
|
+
summary: "(系统自动压缩:摘要生成失败,已丢弃更早历史,仅保留最近对话。)",
|
|
187
|
+
file_operations_xml,
|
|
188
|
+
});
|
|
166
189
|
await publish_action({
|
|
167
|
-
title: "Compacting session
|
|
190
|
+
title: "Compacting session records",
|
|
168
191
|
description: "Summary generation failed; using a fallback summary.",
|
|
169
192
|
state: "running",
|
|
170
193
|
});
|
|
@@ -190,7 +213,7 @@ export async function compactSessionMessagesIfNeeded(
|
|
|
190
213
|
void snapshotTailId;
|
|
191
214
|
|
|
192
215
|
const current_model_messages = current.filter(
|
|
193
|
-
|
|
216
|
+
is_session_message_record,
|
|
194
217
|
);
|
|
195
218
|
const currentCompactCount = resolveCompactCount(
|
|
196
219
|
current_model_messages.length,
|
|
@@ -243,7 +266,7 @@ export async function compactSessionMessagesIfNeeded(
|
|
|
243
266
|
|
|
244
267
|
if (action_started) {
|
|
245
268
|
await publish_action({
|
|
246
|
-
title: "Session
|
|
269
|
+
title: "Session records compacted",
|
|
247
270
|
description: `Compacted ${String(older.length)} earlier messages into ${archiveId}.`,
|
|
248
271
|
state: "completed",
|
|
249
272
|
});
|
|
@@ -297,7 +320,7 @@ function estimateTokensApproxFromText(text: string): number {
|
|
|
297
320
|
* - 统一把 user / assistant 内容线性化,作为 compact 摘要输入。
|
|
298
321
|
* - tool 原始结构不会原样输出,避免把噪声日志喂给摘要模型。
|
|
299
322
|
*/
|
|
300
|
-
function extractPlainTextFromMessages(messages:
|
|
323
|
+
function extractPlainTextFromMessages(messages: SessionMessageRecordV1[]): string {
|
|
301
324
|
const lines: string[] = [];
|
|
302
325
|
for (const m of messages) {
|
|
303
326
|
if (!m || typeof m !== "object") continue;
|
|
@@ -310,3 +333,24 @@ function extractPlainTextFromMessages(messages: SessionModelMessageV1[]): string
|
|
|
310
333
|
}
|
|
311
334
|
return lines.join("\n");
|
|
312
335
|
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 从已有 compact summary 消息中提取旧摘要。
|
|
339
|
+
*
|
|
340
|
+
* 关键点(中文)
|
|
341
|
+
* - 旧摘要通常位于 messages.jsonl 第一条,后续 compact 应基于它迭代更新。
|
|
342
|
+
* - 只识别 compact 生成的 summary,避免误把普通 assistant 文本当作旧摘要。
|
|
343
|
+
*/
|
|
344
|
+
function extract_existing_compaction_summary(
|
|
345
|
+
message: SessionMessageRecordV1 | undefined,
|
|
346
|
+
): string {
|
|
347
|
+
if (!message || typeof message !== "object") return "";
|
|
348
|
+
const metadata = message.metadata;
|
|
349
|
+
if (metadata?.source !== "compact" || metadata.kind !== "summary") return "";
|
|
350
|
+
const parts = Array.isArray(message.parts) ? message.parts : [];
|
|
351
|
+
return parts
|
|
352
|
+
.filter(isTextUIPart)
|
|
353
|
+
.map((part) => String(part.text ?? ""))
|
|
354
|
+
.join("\n")
|
|
355
|
+
.trim();
|
|
356
|
+
}
|