@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
package/src/session/Session.ts
CHANGED
|
@@ -15,8 +15,8 @@ import type {
|
|
|
15
15
|
AgentSession,
|
|
16
16
|
AgentSessionConfigSnapshot,
|
|
17
17
|
AgentSessionForkInput,
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
AgentSessionRecordsInput,
|
|
19
|
+
AgentSessionRecordsPage,
|
|
20
20
|
AgentSessionInfo,
|
|
21
21
|
AgentSessionSetInput,
|
|
22
22
|
AgentSessionSystemBlock,
|
|
@@ -207,7 +207,7 @@ export class Session implements AgentSession {
|
|
|
207
207
|
/**
|
|
208
208
|
* 追加一条 user 文本消息。
|
|
209
209
|
*/
|
|
210
|
-
async
|
|
210
|
+
async append_user_message(input: {
|
|
211
211
|
text: string;
|
|
212
212
|
}): Promise<void> {
|
|
213
213
|
await this.stateService.append_user_message({
|
|
@@ -218,7 +218,7 @@ export class Session implements AgentSession {
|
|
|
218
218
|
/**
|
|
219
219
|
* 追加一条 assistant 文本消息。
|
|
220
220
|
*/
|
|
221
|
-
async
|
|
221
|
+
async append_assistant_message(input: {
|
|
222
222
|
text: string;
|
|
223
223
|
}): Promise<void> {
|
|
224
224
|
await this.stateService.append_assistant_message({
|
|
@@ -229,15 +229,15 @@ export class Session implements AgentSession {
|
|
|
229
229
|
/**
|
|
230
230
|
* 读取当前 session 详情。
|
|
231
231
|
*/
|
|
232
|
-
async
|
|
232
|
+
async get_info(): Promise<AgentSessionInfo> {
|
|
233
233
|
return await this.viewService.get_info();
|
|
234
234
|
}
|
|
235
235
|
|
|
236
236
|
/**
|
|
237
|
-
* 读取当前 session
|
|
237
|
+
* 读取当前 session records 分页。
|
|
238
238
|
*/
|
|
239
|
-
async
|
|
240
|
-
return await this.viewService.
|
|
239
|
+
async records(input?: AgentSessionRecordsInput): Promise<AgentSessionRecordsPage> {
|
|
240
|
+
return await this.viewService.records(input);
|
|
241
241
|
}
|
|
242
242
|
|
|
243
243
|
/**
|
|
@@ -288,10 +288,10 @@ export class Session implements AgentSession {
|
|
|
288
288
|
afterSessionUpdatedAsync: async () => {
|
|
289
289
|
await this.executor.afterSessionUpdatedAsync();
|
|
290
290
|
},
|
|
291
|
-
|
|
291
|
+
append_user_message: async (message_params) => {
|
|
292
292
|
await this.stateService.append_user_message(message_params);
|
|
293
293
|
},
|
|
294
|
-
|
|
294
|
+
append_assistant_message: async (message_params) => {
|
|
295
295
|
await this.stateService.append_assistant_message(message_params);
|
|
296
296
|
},
|
|
297
297
|
isExecuting: () => this.isExecuting(),
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
|
|
10
10
|
import { generateText, type LanguageModel } from "ai";
|
|
11
11
|
import type { SessionHistoryMetaV1 } from "@/executor/types/SessionHistoryMeta.js";
|
|
12
|
-
import type {
|
|
13
|
-
import {
|
|
12
|
+
import type { SessionRecordV1 } from "@/executor/types/SessionRecords.js";
|
|
13
|
+
import { is_session_message_record } from "@/executor/types/SessionRecords.js";
|
|
14
14
|
import {
|
|
15
15
|
normalizeSessionTitle,
|
|
16
16
|
readSessionMetadata,
|
|
@@ -41,7 +41,7 @@ export interface EnsureSessionTitleParams {
|
|
|
41
41
|
/**
|
|
42
42
|
* 当前 session 已落盘消息。
|
|
43
43
|
*/
|
|
44
|
-
messages:
|
|
44
|
+
messages: SessionRecordV1[];
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
47
|
* 可选模型实例;传入时会尝试生成更短标题。
|
|
@@ -61,8 +61,8 @@ function truncateTitle(input: string, maxChars: number): string {
|
|
|
61
61
|
return title.slice(0, maxChars).trimEnd();
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
function extractTextFromMessage(message:
|
|
65
|
-
if (!
|
|
64
|
+
function extractTextFromMessage(message: SessionRecordV1): string {
|
|
65
|
+
if (!is_session_message_record(message)) return "";
|
|
66
66
|
if (!Array.isArray(message.parts)) return "";
|
|
67
67
|
const texts: string[] = [];
|
|
68
68
|
for (const part of message.parts) {
|
|
@@ -76,9 +76,9 @@ function extractTextFromMessage(message: SessionMessageV1): string {
|
|
|
76
76
|
return texts.join("\n").trim();
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
function resolveFirstUserText(messages:
|
|
79
|
+
function resolveFirstUserText(messages: SessionRecordV1[]): string {
|
|
80
80
|
for (const message of messages) {
|
|
81
|
-
if (!
|
|
81
|
+
if (!is_session_message_record(message)) continue;
|
|
82
82
|
if (message.role !== "user") continue;
|
|
83
83
|
const text = extractTextFromMessage(message);
|
|
84
84
|
if (text) return text;
|
|
@@ -17,24 +17,24 @@ import {
|
|
|
17
17
|
} from "ai";
|
|
18
18
|
import type {
|
|
19
19
|
AgentListSessionsInput,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
AgentSessionRecordsInput,
|
|
21
|
+
AgentSessionRecordsPage,
|
|
22
|
+
AgentSessionRecordsView,
|
|
23
23
|
AgentSessionInfo,
|
|
24
24
|
AgentSessionSummary,
|
|
25
25
|
AgentSessionSummaryPage,
|
|
26
26
|
AgentSessionTimelineEvent,
|
|
27
27
|
} from "@/types/agent/AgentTypes.js";
|
|
28
28
|
import type {
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
SessionActionRecordV1,
|
|
30
|
+
SessionRecordV1,
|
|
31
31
|
SessionMetadataV1,
|
|
32
|
-
|
|
33
|
-
} from "@/executor/types/
|
|
32
|
+
SessionMessageRecordV1,
|
|
33
|
+
} from "@/executor/types/SessionRecords.js";
|
|
34
34
|
import {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
} from "@/executor/types/
|
|
35
|
+
is_session_action_record,
|
|
36
|
+
is_session_message_record,
|
|
37
|
+
} from "@/executor/types/SessionRecords.js";
|
|
38
38
|
import type { SessionHistoryMetaV1 } from "@/executor/types/SessionHistoryMeta.js";
|
|
39
39
|
import { pickLastSuccessfulChatSendText } from "@/executor/messages/UserVisibleText.js";
|
|
40
40
|
import { getSdkAgentSessionMessagesPath } from "@/session/storage/Paths.js";
|
|
@@ -81,7 +81,7 @@ type SessionBrowseBaseInput = {
|
|
|
81
81
|
/**
|
|
82
82
|
* 当前 session 已读取到的完整消息。
|
|
83
83
|
*/
|
|
84
|
-
messages:
|
|
84
|
+
messages: SessionRecordV1[];
|
|
85
85
|
|
|
86
86
|
/**
|
|
87
87
|
* 当前 session 是否正在执行。
|
|
@@ -177,7 +177,7 @@ function extractMessageText(parts: unknown): string {
|
|
|
177
177
|
return texts.join("\n").trim();
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
function extractAssistantToolSummary(message:
|
|
180
|
+
function extractAssistantToolSummary(message: SessionMessageRecordV1): string {
|
|
181
181
|
if (!Array.isArray(message.parts)) return "";
|
|
182
182
|
const toolNames = new Set<string>();
|
|
183
183
|
for (const part of message.parts as AnyUiPart[]) {
|
|
@@ -193,13 +193,13 @@ function extractAssistantToolSummary(message: SessionModelMessageV1): string {
|
|
|
193
193
|
/**
|
|
194
194
|
* 解析单条 session 消息的用户可见预览文本。
|
|
195
195
|
*/
|
|
196
|
-
export function resolveSessionMessagePreview(message:
|
|
197
|
-
if (
|
|
196
|
+
export function resolveSessionMessagePreview(message: SessionRecordV1): string {
|
|
197
|
+
if (is_session_action_record(message)) {
|
|
198
198
|
return message.description
|
|
199
199
|
? `${message.title}\n${message.description}`
|
|
200
200
|
: message.title;
|
|
201
201
|
}
|
|
202
|
-
if (!
|
|
202
|
+
if (!is_session_message_record(message)) return "";
|
|
203
203
|
const plainText = extractMessageText(message.parts);
|
|
204
204
|
if (plainText) return plainText;
|
|
205
205
|
if (message.role !== "assistant") return "";
|
|
@@ -237,7 +237,7 @@ function extractToolResultOutput(part: ToolPartCompatShape): unknown {
|
|
|
237
237
|
}
|
|
238
238
|
|
|
239
239
|
function toTimelineEvent(params: {
|
|
240
|
-
message:
|
|
240
|
+
message: SessionMessageRecordV1;
|
|
241
241
|
role: AgentSessionTimelineEvent["role"];
|
|
242
242
|
text: string;
|
|
243
243
|
sequence: number;
|
|
@@ -256,7 +256,7 @@ function toTimelineEvent(params: {
|
|
|
256
256
|
}
|
|
257
257
|
|
|
258
258
|
function toActionTimelineEvent(
|
|
259
|
-
message:
|
|
259
|
+
message: SessionActionRecordV1,
|
|
260
260
|
): AgentSessionTimelineEvent {
|
|
261
261
|
const metadata = message.metadata || null;
|
|
262
262
|
return {
|
|
@@ -274,13 +274,13 @@ function toActionTimelineEvent(
|
|
|
274
274
|
* 把单条 session message 展平成时间线事件。
|
|
275
275
|
*/
|
|
276
276
|
export function toSessionTimelineEvents(
|
|
277
|
-
message:
|
|
277
|
+
message: SessionRecordV1,
|
|
278
278
|
): AgentSessionTimelineEvent[] {
|
|
279
|
-
if (
|
|
279
|
+
if (is_session_action_record(message)) {
|
|
280
280
|
return [toActionTimelineEvent(message)];
|
|
281
281
|
}
|
|
282
282
|
|
|
283
|
-
if (!
|
|
283
|
+
if (!is_session_message_record(message)) return [];
|
|
284
284
|
if (message.role !== "assistant") {
|
|
285
285
|
return [
|
|
286
286
|
toTimelineEvent({
|
|
@@ -366,14 +366,14 @@ export function toSessionTimelineEvents(
|
|
|
366
366
|
*/
|
|
367
367
|
export async function loadSessionMessagesFromPath(
|
|
368
368
|
filePath: string,
|
|
369
|
-
): Promise<
|
|
370
|
-
const messages:
|
|
369
|
+
): Promise<SessionRecordV1[]> {
|
|
370
|
+
const messages: SessionRecordV1[] = [];
|
|
371
371
|
if (await fs.pathExists(filePath)) {
|
|
372
372
|
const raw = await fs.readFile(filePath, "utf-8");
|
|
373
373
|
const lines = raw.split("\n").filter(Boolean);
|
|
374
374
|
for (const line of lines) {
|
|
375
375
|
try {
|
|
376
|
-
const parsed = JSON.parse(line) as
|
|
376
|
+
const parsed = JSON.parse(line) as SessionRecordV1;
|
|
377
377
|
if (!parsed || typeof parsed !== "object") continue;
|
|
378
378
|
const candidate = parsed as { type?: unknown; role?: unknown };
|
|
379
379
|
if (
|
|
@@ -393,9 +393,9 @@ export async function loadSessionMessagesFromPath(
|
|
|
393
393
|
const inflight_path = filePath.replace(/messages\.jsonl$/, "inflight.json");
|
|
394
394
|
if (await fs.pathExists(inflight_path)) {
|
|
395
395
|
try {
|
|
396
|
-
const parsed = (await fs.readJson(inflight_path)) as
|
|
396
|
+
const parsed = (await fs.readJson(inflight_path)) as SessionRecordV1;
|
|
397
397
|
if (
|
|
398
|
-
|
|
398
|
+
is_session_message_record(parsed) &&
|
|
399
399
|
parsed.role === "assistant" &&
|
|
400
400
|
Array.isArray(parsed.parts)
|
|
401
401
|
) {
|
|
@@ -418,11 +418,11 @@ export async function loadSessionMessagesFromPath(
|
|
|
418
418
|
*/
|
|
419
419
|
export async function loadSessionArchiveMessagesFromPath(
|
|
420
420
|
filePath: string,
|
|
421
|
-
): Promise<
|
|
421
|
+
): Promise<SessionRecordV1[]> {
|
|
422
422
|
try {
|
|
423
423
|
const raw = (await fs.readJson(filePath)) as SessionArchiveFileV1 | null;
|
|
424
424
|
const items = Array.isArray(raw?.messages) ? raw.messages : [];
|
|
425
|
-
return items.filter((item): item is
|
|
425
|
+
return items.filter((item): item is SessionRecordV1 => {
|
|
426
426
|
if (!item || typeof item !== "object") return false;
|
|
427
427
|
const candidate = item as { type?: unknown; role?: unknown; parts?: unknown };
|
|
428
428
|
return (
|
|
@@ -439,13 +439,13 @@ export async function loadSessionArchiveMessagesFromPath(
|
|
|
439
439
|
}
|
|
440
440
|
}
|
|
441
441
|
|
|
442
|
-
function isCompactSummaryMessage(message:
|
|
443
|
-
if (!
|
|
442
|
+
function isCompactSummaryMessage(message: SessionRecordV1): boolean {
|
|
443
|
+
if (!is_session_message_record(message)) return false;
|
|
444
444
|
const metadata = (message.metadata || null) as SessionMetadataV1 | null;
|
|
445
445
|
return metadata?.source === "compact" || metadata?.kind === "summary";
|
|
446
446
|
}
|
|
447
447
|
|
|
448
|
-
function resolvePreviousArchiveId(messages:
|
|
448
|
+
function resolvePreviousArchiveId(messages: SessionRecordV1[]): string | undefined {
|
|
449
449
|
for (const message of messages) {
|
|
450
450
|
if (!isCompactSummaryMessage(message)) continue;
|
|
451
451
|
const metadata = (message.metadata || null) as SessionMetadataV1 | null;
|
|
@@ -456,8 +456,8 @@ function resolvePreviousArchiveId(messages: SessionMessageV1[]): string | undefi
|
|
|
456
456
|
}
|
|
457
457
|
|
|
458
458
|
function filterUserVisibleHistoryMessages(
|
|
459
|
-
messages:
|
|
460
|
-
):
|
|
459
|
+
messages: SessionRecordV1[],
|
|
460
|
+
): SessionRecordV1[] {
|
|
461
461
|
return messages.filter((message) => !isCompactSummaryMessage(message));
|
|
462
462
|
}
|
|
463
463
|
|
|
@@ -500,14 +500,14 @@ export function buildSessionInfo(
|
|
|
500
500
|
}
|
|
501
501
|
|
|
502
502
|
/**
|
|
503
|
-
* 基于完整消息列表构建 session
|
|
503
|
+
* 基于完整消息列表构建 session records 分页结果。
|
|
504
504
|
*/
|
|
505
|
-
export function
|
|
505
|
+
export function buildSessionRecordsPage(params: {
|
|
506
506
|
session: AgentSessionInfo;
|
|
507
|
-
messages:
|
|
508
|
-
input?:
|
|
509
|
-
}):
|
|
510
|
-
const view:
|
|
507
|
+
messages: SessionRecordV1[];
|
|
508
|
+
input?: AgentSessionRecordsInput;
|
|
509
|
+
}): AgentSessionRecordsPage {
|
|
510
|
+
const view: AgentSessionRecordsView = params.input?.view || "message";
|
|
511
511
|
const order = params.input?.order || "asc";
|
|
512
512
|
const limit = normalizeLimit(params.input?.limit, 50, 500);
|
|
513
513
|
const cursor = normalizeCursor(params.input?.cursor);
|
package/src/session/index.ts
CHANGED
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { nanoid } from "nanoid";
|
|
11
|
-
import type { SessionUserMessageV1 } from "@/executor/types/
|
|
11
|
+
import type { SessionUserMessageV1 } from "@/executor/types/SessionRecords.js";
|
|
12
12
|
import type {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from "@/executor/types/
|
|
13
|
+
SessionRecordV1,
|
|
14
|
+
SessionMessageRecordV1,
|
|
15
|
+
} from "@/executor/types/SessionRecords.js";
|
|
16
16
|
import type { AgentSessionEvent } from "@/types/sdk/AgentSessionEvent.js";
|
|
17
17
|
import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js";
|
|
18
18
|
import type { AgentSessionStopResult } from "@/types/sdk/AgentSessionStop.js";
|
|
@@ -109,7 +109,7 @@ export interface SessionPromptRuntimeOptions {
|
|
|
109
109
|
}) => Promise<{
|
|
110
110
|
text: string;
|
|
111
111
|
success: boolean;
|
|
112
|
-
assistantMessage?:
|
|
112
|
+
assistantMessage?: SessionMessageRecordV1 | null;
|
|
113
113
|
error?: string;
|
|
114
114
|
}>;
|
|
115
115
|
|
|
@@ -349,7 +349,7 @@ export class SessionPromptRuntime {
|
|
|
349
349
|
function buildPromptRuntimeErrorAssistantMessage(input: {
|
|
350
350
|
sessionId: string;
|
|
351
351
|
message: string;
|
|
352
|
-
}):
|
|
352
|
+
}): SessionMessageRecordV1 {
|
|
353
353
|
return {
|
|
354
354
|
id: `a:${input.sessionId}:${Date.now()}:${nanoid(6)}`,
|
|
355
355
|
role: "assistant",
|
|
@@ -30,16 +30,15 @@ import type {
|
|
|
30
30
|
import type {
|
|
31
31
|
AgentSessionEvent,
|
|
32
32
|
} from "@/types/sdk/AgentSessionEvent.js";
|
|
33
|
-
import type {
|
|
34
|
-
AgentSessionActionEvent,
|
|
35
|
-
AgentSessionActionRecord,
|
|
36
|
-
} from "@/types/sdk/AgentSessionAction.js";
|
|
37
33
|
import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js";
|
|
38
34
|
import type {
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
SessionActionRecordInputV1,
|
|
36
|
+
SessionActionRecordV1,
|
|
37
|
+
SessionRecordV1,
|
|
38
|
+
SessionMessageRecordV1,
|
|
41
39
|
SessionUserMessageV1,
|
|
42
|
-
} from "@/executor/types/
|
|
40
|
+
} from "@/executor/types/SessionRecords.js";
|
|
41
|
+
import { to_session_action_record } from "@/executor/types/SessionRecords.js";
|
|
43
42
|
import type { SessionLocalState } from "@/types/session/SessionLocalState.js";
|
|
44
43
|
import { generateId } from "@/utils/Id.js";
|
|
45
44
|
|
|
@@ -96,8 +95,7 @@ type SessionSetOptions = {
|
|
|
96
95
|
emit_action?: boolean;
|
|
97
96
|
};
|
|
98
97
|
|
|
99
|
-
type EmitActionInput =
|
|
100
|
-
Partial<Pick<AgentSessionActionRecord, "id">>;
|
|
98
|
+
type EmitActionInput = SessionActionRecordInputV1 | SessionActionRecordV1;
|
|
101
99
|
|
|
102
100
|
/**
|
|
103
101
|
* 本地 Session 状态与持久化服务。
|
|
@@ -228,9 +226,17 @@ export class SessionStateService {
|
|
|
228
226
|
const next_model_label = input.model
|
|
229
227
|
? inferAgentModelLabel(input.model)
|
|
230
228
|
: undefined;
|
|
229
|
+
const should_emit_model_switch_action = Boolean(
|
|
230
|
+
input.model &&
|
|
231
|
+
should_emit_action &&
|
|
232
|
+
this.state.sessionConfig.model &&
|
|
233
|
+
previous_model_label &&
|
|
234
|
+
next_model_label &&
|
|
235
|
+
previous_model_label !== next_model_label,
|
|
236
|
+
);
|
|
231
237
|
const action_id = `model-switching:${this.session_id}:${Date.now()}:${generateId()}`;
|
|
232
238
|
|
|
233
|
-
if (
|
|
239
|
+
if (should_emit_model_switch_action) {
|
|
234
240
|
await this.emit_action_event({
|
|
235
241
|
id: action_id,
|
|
236
242
|
title: "Switching session model",
|
|
@@ -254,7 +260,7 @@ export class SessionStateService {
|
|
|
254
260
|
model: this.state.sessionConfig.model,
|
|
255
261
|
});
|
|
256
262
|
} catch (error) {
|
|
257
|
-
if (
|
|
263
|
+
if (should_emit_model_switch_action) {
|
|
258
264
|
await this.emit_action_event({
|
|
259
265
|
id: action_id,
|
|
260
266
|
title: "Session model switch failed",
|
|
@@ -265,7 +271,7 @@ export class SessionStateService {
|
|
|
265
271
|
throw error;
|
|
266
272
|
}
|
|
267
273
|
|
|
268
|
-
if (
|
|
274
|
+
if (should_emit_model_switch_action) {
|
|
269
275
|
await this.emit_action_event({
|
|
270
276
|
id: action_id,
|
|
271
277
|
title: "Session model switched",
|
|
@@ -281,10 +287,10 @@ export class SessionStateService {
|
|
|
281
287
|
* 追加一条 user 消息并刷新标题与 metadata。
|
|
282
288
|
*/
|
|
283
289
|
async append_user_message(params: {
|
|
284
|
-
message?:
|
|
290
|
+
message?: SessionRecordV1 | null;
|
|
285
291
|
text?: string;
|
|
286
292
|
}): Promise<void> {
|
|
287
|
-
await this.executor.
|
|
293
|
+
await this.executor.append_user_message(params);
|
|
288
294
|
await this.ensure_title_from_history({ generate: true });
|
|
289
295
|
await this.touch_metadata();
|
|
290
296
|
}
|
|
@@ -293,10 +299,10 @@ export class SessionStateService {
|
|
|
293
299
|
* 追加一条 assistant 消息并刷新 metadata。
|
|
294
300
|
*/
|
|
295
301
|
async append_assistant_message(params: {
|
|
296
|
-
message?:
|
|
302
|
+
message?: SessionRecordV1 | null;
|
|
297
303
|
fallbackText?: string;
|
|
298
304
|
}): Promise<void> {
|
|
299
|
-
await this.executor.
|
|
305
|
+
await this.executor.append_assistant_message(params);
|
|
300
306
|
await this.touch_metadata();
|
|
301
307
|
}
|
|
302
308
|
|
|
@@ -321,7 +327,7 @@ export class SessionStateService {
|
|
|
321
327
|
*/
|
|
322
328
|
generate?: boolean;
|
|
323
329
|
}): Promise<void> {
|
|
324
|
-
const messages = await this.history_store.
|
|
330
|
+
const messages = await this.history_store.list_records();
|
|
325
331
|
const before_metadata = await readSessionMetadata({
|
|
326
332
|
projectRoot: this.project_root,
|
|
327
333
|
agentId: this.agent_id,
|
|
@@ -349,7 +355,7 @@ export class SessionStateService {
|
|
|
349
355
|
* 持久化最终 assistant 结果。
|
|
350
356
|
*/
|
|
351
357
|
async persist_assistant_result(
|
|
352
|
-
assistant_message?:
|
|
358
|
+
assistant_message?: SessionMessageRecordV1 | null,
|
|
353
359
|
): Promise<void> {
|
|
354
360
|
await persistSdkAssistantResult({
|
|
355
361
|
projectRoot: this.project_root,
|
|
@@ -362,24 +368,13 @@ export class SessionStateService {
|
|
|
362
368
|
}
|
|
363
369
|
|
|
364
370
|
/**
|
|
365
|
-
* 持久化一条 action
|
|
371
|
+
* 持久化一条 action record。
|
|
366
372
|
*/
|
|
367
373
|
async persist_action_event(
|
|
368
|
-
event:
|
|
374
|
+
event: SessionActionRecordV1,
|
|
369
375
|
): Promise<void> {
|
|
370
|
-
const message = this.
|
|
371
|
-
|
|
372
|
-
id: event.id,
|
|
373
|
-
title: event.title,
|
|
374
|
-
state: event.state,
|
|
375
|
-
...(event.description ? { description: event.description } : {}),
|
|
376
|
-
...(event.turnId ? { turnId: event.turnId } : {}),
|
|
377
|
-
},
|
|
378
|
-
metadata: {
|
|
379
|
-
sessionId: this.session_id,
|
|
380
|
-
},
|
|
381
|
-
});
|
|
382
|
-
await this.history_store.append(message);
|
|
376
|
+
const message = to_session_action_record(event, this.session_id);
|
|
377
|
+
await this.history_store.write_record(message);
|
|
383
378
|
await this.touch_metadata();
|
|
384
379
|
}
|
|
385
380
|
|
|
@@ -387,17 +382,15 @@ export class SessionStateService {
|
|
|
387
382
|
* 写入并发布一条 action。
|
|
388
383
|
*/
|
|
389
384
|
async emit_action_event(input: EmitActionInput): Promise<void> {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
id: action_id,
|
|
400
|
-
};
|
|
385
|
+
const event = to_session_action_record(
|
|
386
|
+
{
|
|
387
|
+
...input,
|
|
388
|
+
id:
|
|
389
|
+
String(input.id || "").trim() ||
|
|
390
|
+
`action:${this.session_id}:${Date.now()}:${generateId()}`,
|
|
391
|
+
},
|
|
392
|
+
this.session_id,
|
|
393
|
+
);
|
|
401
394
|
try {
|
|
402
395
|
await this.persist_action_event(event);
|
|
403
396
|
} catch {
|
|
@@ -420,7 +413,7 @@ export class SessionStateService {
|
|
|
420
413
|
sessionId: this.session_id,
|
|
421
414
|
},
|
|
422
415
|
}) as SessionUserMessageV1;
|
|
423
|
-
await this.executor.
|
|
416
|
+
await this.executor.append_user_message({
|
|
424
417
|
message,
|
|
425
418
|
});
|
|
426
419
|
await this.ensure_title_from_history({ generate: true });
|
|
@@ -445,7 +438,7 @@ export class SessionStateService {
|
|
|
445
438
|
kind: "normal",
|
|
446
439
|
},
|
|
447
440
|
};
|
|
448
|
-
await this.executor.
|
|
441
|
+
await this.executor.append_user_message({
|
|
449
442
|
message,
|
|
450
443
|
});
|
|
451
444
|
await this.ensure_title_from_history({ generate: true });
|
|
@@ -464,7 +457,7 @@ export class SessionStateService {
|
|
|
464
457
|
: [];
|
|
465
458
|
if (normalized_messages.length <= 0) return;
|
|
466
459
|
for (const message of normalized_messages) {
|
|
467
|
-
await this.executor.
|
|
460
|
+
await this.executor.append_user_message({
|
|
468
461
|
message,
|
|
469
462
|
});
|
|
470
463
|
}
|
|
@@ -19,10 +19,10 @@ import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js"
|
|
|
19
19
|
import type { AgentSessionStopResult } from "@/types/sdk/AgentSessionStop.js";
|
|
20
20
|
import type { AgentSessionTurnHandle } from "@/types/sdk/AgentSessionTurn.js";
|
|
21
21
|
import type {
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
SessionRecordV1,
|
|
23
|
+
SessionMessageRecordV1,
|
|
24
24
|
SessionUserMessageV1,
|
|
25
|
-
} from "@/executor/types/
|
|
25
|
+
} from "@/executor/types/SessionRecords.js";
|
|
26
26
|
import { mapAgentEventToSessionEvent, mapUiMessageChunkToAgentEvent } from "@/session/SessionEventMapper.js";
|
|
27
27
|
import { SessionEventHub } from "@/session/runtime/SessionEventHub.js";
|
|
28
28
|
import { SessionPromptRuntime } from "@/session/runtime/SessionPromptRuntime.js";
|
|
@@ -145,7 +145,7 @@ export class SessionTurnService {
|
|
|
145
145
|
}): Promise<{
|
|
146
146
|
text: string;
|
|
147
147
|
success: boolean;
|
|
148
|
-
assistantMessage?:
|
|
148
|
+
assistantMessage?: SessionMessageRecordV1 | null;
|
|
149
149
|
error?: string;
|
|
150
150
|
}> {
|
|
151
151
|
const tool_name_by_call_id = new Map<string, string>();
|