@downcity/agent 1.1.11 → 1.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.d.ts +3 -3
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +2 -2
- package/bin/index.js.map +1 -1
- package/bin/runtime/AgentRuntime.d.ts +2 -2
- package/bin/runtime/AgentRuntime.d.ts.map +1 -1
- package/bin/runtime/AgentRuntime.js +7 -12
- package/bin/runtime/AgentRuntime.js.map +1 -1
- package/bin/runtime/AgentRuntimeTypes.d.ts +3 -3
- package/bin/runtime/AgentRuntimeTypes.d.ts.map +1 -1
- package/bin/sdk/Agent.d.ts +13 -8
- package/bin/sdk/Agent.d.ts.map +1 -1
- package/bin/sdk/Agent.js +62 -18
- package/bin/sdk/Agent.js.map +1 -1
- package/bin/sdk/AgentSdkTypes.d.ts +95 -0
- package/bin/sdk/AgentSdkTypes.d.ts.map +1 -1
- package/bin/sdk/RemoteAgent.d.ts +3 -3
- package/bin/sdk/RemoteAgent.d.ts.map +1 -1
- package/bin/sdk/RemoteAgent.js +5 -2
- package/bin/sdk/RemoteAgent.js.map +1 -1
- package/bin/sdk/Session.d.ts +21 -19
- package/bin/sdk/Session.d.ts.map +1 -1
- package/bin/sdk/Session.js +79 -69
- package/bin/sdk/Session.js.map +1 -1
- package/bin/sdk/SessionMetadata.d.ts +7 -3
- package/bin/sdk/SessionMetadata.d.ts.map +1 -1
- package/bin/sdk/SessionMetadata.js +21 -5
- package/bin/sdk/SessionMetadata.js.map +1 -1
- package/bin/sdk/SessionPersistence.d.ts +6 -6
- package/bin/sdk/SessionPersistence.d.ts.map +1 -1
- package/bin/sdk/SessionPersistence.js +7 -7
- package/bin/sdk/SessionPersistence.js.map +1 -1
- package/bin/sdk/SessionServicePort.d.ts +5 -5
- package/bin/sdk/SessionServicePort.d.ts.map +1 -1
- package/bin/sdk/SessionServicePort.js +9 -9
- package/bin/sdk/SessionServicePort.js.map +1 -1
- package/bin/sdk/SessionSystemBuilder.d.ts +106 -0
- package/bin/sdk/SessionSystemBuilder.d.ts.map +1 -0
- package/bin/sdk/SessionSystemBuilder.js +150 -0
- package/bin/sdk/SessionSystemBuilder.js.map +1 -0
- package/bin/service/builtins/chat/runtime/ChatSession.d.ts +27 -8
- package/bin/service/builtins/chat/runtime/ChatSession.d.ts.map +1 -1
- package/bin/service/builtins/chat/runtime/ChatSession.js +9 -4
- package/bin/service/builtins/chat/runtime/ChatSession.js.map +1 -1
- package/bin/service/builtins/task/runtime/Runner.d.ts +1 -1
- package/bin/service/builtins/task/runtime/Runner.js +1 -1
- package/bin/service/builtins/task/runtime/TaskRunnerSession.d.ts +2 -2
- package/bin/service/builtins/task/runtime/TaskRunnerSession.js +4 -4
- package/bin/service/builtins/task/runtime/TaskRunnerSession.js.map +1 -1
- package/bin/service/builtins/task/runtime/TaskRunnerTypes.d.ts +1 -1
- package/bin/session/{Session.d.ts → Executor.d.ts} +54 -17
- package/bin/session/Executor.d.ts.map +1 -0
- package/bin/session/{Session.js → Executor.js} +60 -23
- package/bin/session/Executor.js.map +1 -0
- package/bin/session/composer/SessionComposer.d.ts +1 -1
- package/bin/session/composer/SessionComposer.js +1 -1
- package/bin/session/composer/compaction/SessionCompactionComposer.d.ts +2 -2
- package/bin/session/composer/execution/LocalSessionExecutionComposer.d.ts +1 -1
- package/bin/session/composer/execution/LocalSessionExecutionComposer.js +1 -1
- package/bin/session/composer/execution/SessionExecutionComposer.d.ts +1 -1
- package/bin/session/composer/history/SessionHistoryComposer.d.ts +1 -1
- package/bin/session/composer/system/SessionSystemComposer.d.ts +1 -1
- package/bin/session/composer/system/SessionSystemComposer.js +1 -1
- package/bin/session/executors/local/{LocalSessionCore.d.ts → Runner.d.ts} +10 -10
- package/bin/session/executors/local/Runner.d.ts.map +1 -0
- package/bin/session/executors/local/{LocalSessionCore.js → Runner.js} +11 -11
- package/bin/session/executors/local/Runner.js.map +1 -0
- package/bin/session/executors/local/SessionExecutionError.d.ts +1 -1
- package/bin/session/executors/local/SessionExecutionError.js +1 -1
- package/bin/session/executors/local/SessionLoopDecision.d.ts +3 -3
- package/bin/session/executors/local/SessionLoopDecision.js +3 -3
- package/bin/session/executors/local/SessionModelMessageState.d.ts +2 -2
- package/bin/session/executors/local/SessionModelMessageState.js +2 -2
- package/bin/session/executors/local/SessionSignals.d.ts +3 -3
- package/bin/session/executors/local/SessionSignals.js +3 -3
- package/bin/session/executors/local/SessionToolLoopRunner.d.ts +1 -1
- package/bin/session/executors/local/SessionToolLoopRunner.js +2 -2
- package/bin/session/executors/local/SessionToolLoopRunner.js.map +1 -1
- package/bin/session/types/SessionHistoryMeta.d.ts +2 -0
- package/bin/session/types/SessionHistoryMeta.d.ts.map +1 -1
- package/bin/session/types/SessionLoop.d.ts +1 -1
- package/bin/session/types/SessionLoop.js +1 -1
- package/bin/session/types/SessionRun.d.ts +2 -2
- package/bin/session/types/SessionRun.js +1 -1
- package/package.json +1 -1
- package/src/index.ts +6 -2
- package/src/runtime/AgentRuntime.ts +7 -17
- package/src/runtime/AgentRuntimeTypes.ts +3 -3
- package/src/sdk/Agent.ts +75 -23
- package/src/sdk/AgentSdkTypes.ts +113 -0
- package/src/sdk/RemoteAgent.ts +10 -4
- package/src/sdk/Session.ts +100 -85
- package/src/sdk/SessionMetadata.ts +23 -5
- package/src/sdk/SessionPersistence.ts +13 -13
- package/src/sdk/SessionServicePort.ts +13 -13
- package/src/sdk/SessionSystemBuilder.ts +261 -0
- package/src/service/builtins/chat/runtime/ChatSession.ts +37 -13
- package/src/service/builtins/task/runtime/Runner.ts +1 -1
- package/src/service/builtins/task/runtime/TaskRunnerSession.ts +4 -4
- package/src/service/builtins/task/runtime/TaskRunnerTypes.ts +1 -1
- package/src/session/{Session.ts → Executor.ts} +96 -26
- package/src/session/composer/SessionComposer.ts +1 -1
- package/src/session/composer/compaction/SessionCompactionComposer.ts +2 -2
- package/src/session/composer/execution/LocalSessionExecutionComposer.ts +1 -1
- package/src/session/composer/execution/SessionExecutionComposer.ts +1 -1
- package/src/session/composer/history/SessionHistoryComposer.ts +1 -1
- package/src/session/composer/system/SessionSystemComposer.ts +1 -1
- package/src/session/executors/local/{LocalSessionCore.ts → Runner.ts} +13 -13
- package/src/session/executors/local/SessionExecutionError.ts +1 -1
- package/src/session/executors/local/SessionLoopDecision.ts +3 -3
- package/src/session/executors/local/SessionModelMessageState.ts +2 -2
- package/src/session/executors/local/SessionSignals.ts +3 -3
- package/src/session/executors/local/SessionToolLoopRunner.ts +2 -2
- package/src/session/types/SessionHistoryMeta.ts +2 -0
- package/src/session/types/SessionLoop.ts +1 -1
- package/src/session/types/SessionRun.ts +2 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/bin/sdk/SdkSessionSystemComposer.d.ts +0 -73
- package/bin/sdk/SdkSessionSystemComposer.d.ts.map +0 -1
- package/bin/sdk/SdkSessionSystemComposer.js +0 -96
- package/bin/sdk/SdkSessionSystemComposer.js.map +0 -1
- package/bin/session/Session.d.ts.map +0 -1
- package/bin/session/Session.js.map +0 -1
- package/bin/session/executors/local/LocalSessionCore.d.ts.map +0 -1
- package/bin/session/executors/local/LocalSessionCore.js.map +0 -1
- package/bin/session/executors/local/LocalSessionExecutor.d.ts +0 -61
- package/bin/session/executors/local/LocalSessionExecutor.d.ts.map +0 -1
- package/bin/session/executors/local/LocalSessionExecutor.js +0 -36
- package/bin/session/executors/local/LocalSessionExecutor.js.map +0 -1
- package/src/sdk/SdkSessionSystemComposer.ts +0 -173
- package/src/session/executors/local/LocalSessionExecutor.ts +0 -89
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 关键点(中文)
|
|
5
5
|
* - `SessionRunInput` 表示上层会话入口输入(例如 context query)。
|
|
6
|
-
* - `SessionExecuteInput` 表示
|
|
6
|
+
* - `SessionExecuteInput` 表示 Runner 通过 Composer 装配后的中间运行态。
|
|
7
7
|
* - 输出仅暴露 assistantMessage(UIMessage)。
|
|
8
8
|
*/
|
|
9
9
|
import type { Tool, UIMessageChunk } from "ai";
|
|
@@ -89,7 +89,7 @@ export interface SessionRunInput {
|
|
|
89
89
|
query: string;
|
|
90
90
|
}
|
|
91
91
|
/**
|
|
92
|
-
*
|
|
92
|
+
* Runner 通过 Composer 装配后的中间运行态。
|
|
93
93
|
*/
|
|
94
94
|
export interface SessionExecuteInput {
|
|
95
95
|
/**
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
// SDK 入口
|
|
11
11
|
export { Agent } from "./sdk/Agent.js";
|
|
12
|
+
export { Session } from "./sdk/Session.js";
|
|
12
13
|
export { RemoteAgent } from "./sdk/RemoteAgent.js";
|
|
13
14
|
export type {
|
|
14
15
|
AgentOptions,
|
|
@@ -20,6 +21,10 @@ export type {
|
|
|
20
21
|
AgentSessionRunResult,
|
|
21
22
|
AgentSessionSetInput,
|
|
22
23
|
AgentSessionStreamEvent,
|
|
24
|
+
AgentSessionSystemBlock,
|
|
25
|
+
AgentSessionSystemBlockSource,
|
|
26
|
+
AgentSessionSystemSessionInfo,
|
|
27
|
+
AgentSessionSystemSnapshot,
|
|
23
28
|
} from "./sdk/AgentSdkTypes.js";
|
|
24
29
|
|
|
25
30
|
// 服务与插件作者 API
|
|
@@ -57,7 +62,7 @@ export type { AgentRuntime, AgentRuntimeBase } from "./runtime/AgentRuntimeState
|
|
|
57
62
|
export type { AgentContext } from "./runtime/AgentContextTypes.js";
|
|
58
63
|
|
|
59
64
|
// Session 与即时执行集成
|
|
60
|
-
export {
|
|
65
|
+
export { Executor } from "./session/Executor.js";
|
|
61
66
|
export {
|
|
62
67
|
drainDeferredPersistedUserMessages,
|
|
63
68
|
getSessionRunScope,
|
|
@@ -67,7 +72,6 @@ export { JsonlSessionCompactionComposer } from "./session/composer/compaction/js
|
|
|
67
72
|
export { SessionSystemComposer } from "./session/composer/system/SessionSystemComposer.js";
|
|
68
73
|
export { transformPromptsIntoSystemMessages } from "./session/composer/system/default/PromptRenderer.js";
|
|
69
74
|
export { loadStaticSystemPrompts } from "./session/composer/system/default/StaticPromptCatalog.js";
|
|
70
|
-
export { LocalSessionExecutor } from "./session/executors/local/LocalSessionExecutor.js";
|
|
71
75
|
|
|
72
76
|
// Agent server 与 transport 集成
|
|
73
77
|
export { startServer } from "./server/http/Server.js";
|
|
@@ -5,13 +5,12 @@
|
|
|
5
5
|
* - 这里是单 agent 宿主的主装配入口,负责把 config、model、session、service、plugin 串起来。
|
|
6
6
|
* - 完整运行态类型仍由 `AgentRuntimeState.ts` 维护;本模块只负责装配与对外导出。
|
|
7
7
|
* - 该模块同时负责热重载提示词系统与导出统一的 `getAgentContext()` 入口。
|
|
8
|
-
* - 当前只有 `api` 执行模式:从 console 模型池取模型,走
|
|
8
|
+
* - 当前只有 `api` 执行模式:从 console 模型池取模型,走 Runner。
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import path from "path";
|
|
12
12
|
import fs from "fs";
|
|
13
13
|
import { logger as defaultLogger } from "@/utils/logger/Logger.js";
|
|
14
|
-
import { LocalSessionExecutor } from "@session/executors/local/LocalSessionExecutor.js";
|
|
15
14
|
import { ensureRuntimeProjectReady } from "@/host/daemon/ProjectSetup.js";
|
|
16
15
|
import { createModel } from "@/model/CreateModel.js";
|
|
17
16
|
import {
|
|
@@ -29,7 +28,6 @@ import { getSessionRunScope } from "@session/SessionRunScope.js";
|
|
|
29
28
|
import { JsonlSessionHistoryComposer } from "@session/composer/history/jsonl/JsonlSessionHistoryComposer.js";
|
|
30
29
|
import { JsonlSessionCompactionComposer } from "@session/composer/compaction/jsonl/JsonlSessionCompactionComposer.js";
|
|
31
30
|
import { DefaultSessionSystemComposer } from "@session/composer/system/default/DefaultSessionSystemComposer.js";
|
|
32
|
-
import type { SessionHistoryComposer } from "@session/composer/history/SessionHistoryComposer.js";
|
|
33
31
|
import { ChatSession } from "@/service/builtins/chat/runtime/ChatSession.js";
|
|
34
32
|
import { ChatSessionExecutionComposer } from "@/service/builtins/chat/runtime/ChatSessionExecutionComposer.js";
|
|
35
33
|
import {
|
|
@@ -224,7 +222,7 @@ function createSessionHistoryComposer(
|
|
|
224
222
|
* 初始化入口。
|
|
225
223
|
*
|
|
226
224
|
* 关键点(中文)
|
|
227
|
-
* - 只有 api 执行模式:从 console 模型池创建模型 →
|
|
225
|
+
* - 只有 api 执行模式:从 console 模型池创建模型 → Executor → Runner。
|
|
228
226
|
*/
|
|
229
227
|
export async function initAgentRuntime(
|
|
230
228
|
cwd: string,
|
|
@@ -326,19 +324,11 @@ export async function initAgentRuntime(
|
|
|
326
324
|
sessionId: key,
|
|
327
325
|
historyComposer,
|
|
328
326
|
executionComposer,
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
model,
|
|
335
|
-
logger: defaultLogger,
|
|
336
|
-
historyComposer: sessionHistoryComposer,
|
|
337
|
-
compactionComposer,
|
|
338
|
-
systemComposer,
|
|
339
|
-
getTools: () => shellTools,
|
|
340
|
-
executionComposer: chatExecutionComposer,
|
|
341
|
-
}),
|
|
327
|
+
getModel: () => model,
|
|
328
|
+
logger: defaultLogger,
|
|
329
|
+
compactionComposer,
|
|
330
|
+
systemComposer,
|
|
331
|
+
getTools: () => shellTools,
|
|
342
332
|
});
|
|
343
333
|
sessionsById.set(key, created);
|
|
344
334
|
return created;
|
|
@@ -16,7 +16,7 @@ import type {
|
|
|
16
16
|
AgentPluginConfigRuntime,
|
|
17
17
|
} from "@/types/host/AgentHost.js";
|
|
18
18
|
import type { BaseService } from "@/service/builtins/BaseService.js";
|
|
19
|
-
import type {
|
|
19
|
+
import type { Executor } from "@session/Executor.js";
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* AgentRuntime 启动早期的基础状态。
|
|
@@ -73,9 +73,9 @@ export interface AgentRuntime extends AgentRuntimeBase {
|
|
|
73
73
|
*/
|
|
74
74
|
model?: LanguageModel;
|
|
75
75
|
/**
|
|
76
|
-
* 读取指定 sessionId
|
|
76
|
+
* 读取指定 sessionId 对应的内部 Executor 实例。
|
|
77
77
|
*/
|
|
78
|
-
getSession(sessionId: string):
|
|
78
|
+
getSession(sessionId: string): Executor;
|
|
79
79
|
/**
|
|
80
80
|
* 返回当前执行中的 sessionId 列表。
|
|
81
81
|
*/
|
package/src/sdk/Agent.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Agent SDK 本地入口。
|
|
3
3
|
*
|
|
4
4
|
* 关键点(中文)
|
|
5
|
-
* - `new Agent({ id, path, tools, services, plugins })` 只做同步装配,不在构造阶段启动任何 I/O。
|
|
5
|
+
* - `new Agent({ id, path, instruction, tools, services, plugins })` 只做同步装配,不在构造阶段启动任何 I/O。
|
|
6
6
|
* - session、HTTP、RPC、service lifecycle 都按需异步初始化。
|
|
7
7
|
* - SDK Agent 通过最小 `AgentContext` 适配层复用现有 service/plugin/chat 运行协议,而不是重复实现一套执行链。
|
|
8
8
|
*/
|
|
@@ -26,9 +26,10 @@ import type {
|
|
|
26
26
|
import type {
|
|
27
27
|
AgentOptions,
|
|
28
28
|
AgentSessionMetadata,
|
|
29
|
+
AgentSessionSystemBlock,
|
|
29
30
|
} from "@/sdk/AgentSdkTypes.js";
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
31
|
+
import { Session } from "@/sdk/Session.js";
|
|
32
|
+
import { DEFAULT_SHIP_PROMPTS } from "@session/composer/system/default/SystemDomain.js";
|
|
32
33
|
import { getSdkAgentSessionsRootDirPath } from "@/sdk/Paths.js";
|
|
33
34
|
import { SdkAgentHttpServer } from "@/sdk/HttpServer.js";
|
|
34
35
|
import { SdkAgentRpcServer } from "@/sdk/RpcServer.js";
|
|
@@ -70,6 +71,41 @@ function createFallbackSdkConfig(agentId: string): DowncityConfig {
|
|
|
70
71
|
} as DowncityConfig;
|
|
71
72
|
}
|
|
72
73
|
|
|
74
|
+
function normalizeInstructionInput(input: string | string[] | undefined): string[] {
|
|
75
|
+
const items = Array.isArray(input) ? input : typeof input === "string" ? [input] : [];
|
|
76
|
+
return items
|
|
77
|
+
.map((item) => String(item || "").trim())
|
|
78
|
+
.filter((item) => item.length > 0);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function createCoreInstructionContent(projectRoot: string): string {
|
|
82
|
+
const currentYear = String(new Date().getFullYear());
|
|
83
|
+
return DEFAULT_SHIP_PROMPTS
|
|
84
|
+
.replaceAll("{{project_path}}", projectRoot)
|
|
85
|
+
.replaceAll("{{project_root}}", projectRoot)
|
|
86
|
+
.replaceAll("{{current_year}}", currentYear);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function createInstructionSystemBlocks(
|
|
90
|
+
instruction: string[],
|
|
91
|
+
projectRoot: string,
|
|
92
|
+
): AgentSessionSystemBlock[] {
|
|
93
|
+
if (instruction.length === 0) {
|
|
94
|
+
return [
|
|
95
|
+
{
|
|
96
|
+
source: "core",
|
|
97
|
+
name: "default",
|
|
98
|
+
content: createCoreInstructionContent(projectRoot),
|
|
99
|
+
},
|
|
100
|
+
];
|
|
101
|
+
}
|
|
102
|
+
return instruction.map((content, index) => ({
|
|
103
|
+
source: "instruction" as const,
|
|
104
|
+
name: instruction.length === 1 ? "agent" : `agent:${index + 1}`,
|
|
105
|
+
content,
|
|
106
|
+
}));
|
|
107
|
+
}
|
|
108
|
+
|
|
73
109
|
/**
|
|
74
110
|
* SDK 本地 Agent。
|
|
75
111
|
*/
|
|
@@ -83,14 +119,14 @@ export class Agent {
|
|
|
83
119
|
readonly plugins: PluginPort;
|
|
84
120
|
|
|
85
121
|
private readonly logger: Logger;
|
|
86
|
-
private readonly sessionsById = new Map<string,
|
|
122
|
+
private readonly sessionsById = new Map<string, Session>();
|
|
87
123
|
private readonly runtime: AgentRuntime;
|
|
88
124
|
private readonly serviceContext: AgentContext;
|
|
89
125
|
private readonly pluginRegistry: PluginRegistry;
|
|
90
126
|
private readonly pluginSystemProviders: Plugin[];
|
|
91
127
|
private readonly config: DowncityConfig;
|
|
92
128
|
private readonly platform: AgentPlatformRuntime;
|
|
93
|
-
private
|
|
129
|
+
private instruction: string[];
|
|
94
130
|
private servicesStartPromise: Promise<void> | null = null;
|
|
95
131
|
|
|
96
132
|
constructor(options: AgentOptions) {
|
|
@@ -109,7 +145,7 @@ export class Agent {
|
|
|
109
145
|
this.logger = new Logger();
|
|
110
146
|
this.logger.bindProjectRoot(this.path);
|
|
111
147
|
this.platform = options.platform || EMPTY_SDK_PLATFORM;
|
|
112
|
-
this.
|
|
148
|
+
this.instruction = normalizeInstructionInput(options.instruction);
|
|
113
149
|
this.config = this.loadConfig();
|
|
114
150
|
this.services = this.createServiceMap(options.services || []);
|
|
115
151
|
this.runtime = this.createRuntime();
|
|
@@ -129,7 +165,7 @@ export class Agent {
|
|
|
129
165
|
/**
|
|
130
166
|
* 获取或创建一个 session。
|
|
131
167
|
*/
|
|
132
|
-
async session(sessionId?: string): Promise<
|
|
168
|
+
async session(sessionId?: string): Promise<Session> {
|
|
133
169
|
const session = this.getOrCreateSession(sessionId);
|
|
134
170
|
await session.initialize();
|
|
135
171
|
return session;
|
|
@@ -176,10 +212,14 @@ export class Agent {
|
|
|
176
212
|
}
|
|
177
213
|
|
|
178
214
|
/**
|
|
179
|
-
*
|
|
215
|
+
* 更新当前 SDK Agent 的静态基础指令。
|
|
216
|
+
*
|
|
217
|
+
* 关键点(中文)
|
|
218
|
+
* - instruction 不做动态变量替换。
|
|
219
|
+
* - 已创建的 session 后续 run 会读取最新 instruction blocks。
|
|
180
220
|
*/
|
|
181
|
-
|
|
182
|
-
this.
|
|
221
|
+
setInstruction(input: string | string[]): void {
|
|
222
|
+
this.instruction = normalizeInstructionInput(input);
|
|
183
223
|
}
|
|
184
224
|
|
|
185
225
|
private loadConfig(): DowncityConfig {
|
|
@@ -252,8 +292,12 @@ export class Agent {
|
|
|
252
292
|
};
|
|
253
293
|
}
|
|
254
294
|
|
|
255
|
-
private
|
|
256
|
-
|
|
295
|
+
private loadInstructionSystemBlocks(): AgentSessionSystemBlock[] {
|
|
296
|
+
return createInstructionSystemBlocks(this.instruction, this.path);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private async loadPluginSystemBlocks(): Promise<AgentSessionSystemBlock[]> {
|
|
300
|
+
const out: AgentSessionSystemBlock[] = [];
|
|
257
301
|
for (const plugin of this.pluginSystemProviders) {
|
|
258
302
|
if (typeof plugin.system !== "function") continue;
|
|
259
303
|
try {
|
|
@@ -264,7 +308,11 @@ export class Agent {
|
|
|
264
308
|
}
|
|
265
309
|
const text = String(await plugin.system(this.serviceContext)).trim();
|
|
266
310
|
if (!text) continue;
|
|
267
|
-
out.push(
|
|
311
|
+
out.push({
|
|
312
|
+
source: "plugin",
|
|
313
|
+
name: plugin.name,
|
|
314
|
+
content: text,
|
|
315
|
+
});
|
|
268
316
|
} catch {
|
|
269
317
|
// 单个 plugin system 失败不应阻断 SDK session 主链路。
|
|
270
318
|
}
|
|
@@ -272,14 +320,18 @@ export class Agent {
|
|
|
272
320
|
return out;
|
|
273
321
|
}
|
|
274
322
|
|
|
275
|
-
private async
|
|
276
|
-
const out:
|
|
323
|
+
private async loadServiceSystemBlocks(): Promise<AgentSessionSystemBlock[]> {
|
|
324
|
+
const out: AgentSessionSystemBlock[] = [];
|
|
277
325
|
for (const service of this.services.values()) {
|
|
278
326
|
if (typeof service.system !== "function") continue;
|
|
279
327
|
try {
|
|
280
328
|
const text = String(await service.system(this.serviceContext)).trim();
|
|
281
329
|
if (!text) continue;
|
|
282
|
-
out.push(
|
|
330
|
+
out.push({
|
|
331
|
+
source: "service",
|
|
332
|
+
name: service.name,
|
|
333
|
+
content: text,
|
|
334
|
+
});
|
|
283
335
|
} catch {
|
|
284
336
|
// 单个 service system 失败不应阻断 SDK session 主链路。
|
|
285
337
|
}
|
|
@@ -295,7 +347,7 @@ export class Agent {
|
|
|
295
347
|
config: this.config,
|
|
296
348
|
env: {},
|
|
297
349
|
globalEnv: {},
|
|
298
|
-
systems: this.
|
|
350
|
+
systems: this.instruction,
|
|
299
351
|
paths: createAgentPathRuntime(this.path),
|
|
300
352
|
pluginConfig: createAgentPluginConfigRuntime(this.path),
|
|
301
353
|
platform: this.platform,
|
|
@@ -326,7 +378,7 @@ export class Agent {
|
|
|
326
378
|
config: this.config,
|
|
327
379
|
env: {},
|
|
328
380
|
globalEnv: {},
|
|
329
|
-
systems: this.
|
|
381
|
+
systems: this.instruction,
|
|
330
382
|
paths: this.runtime.paths,
|
|
331
383
|
pluginConfig: this.runtime.pluginConfig,
|
|
332
384
|
platform: this.platform,
|
|
@@ -397,21 +449,21 @@ export class Agent {
|
|
|
397
449
|
return context;
|
|
398
450
|
}
|
|
399
451
|
|
|
400
|
-
private getOrCreateSession(sessionId?: string):
|
|
452
|
+
private getOrCreateSession(sessionId?: string): Session {
|
|
401
453
|
const resolvedSessionId =
|
|
402
454
|
String(sessionId || "").trim() || `session-${Date.now()}-${nanoid(8)}`;
|
|
403
455
|
const cached = this.sessionsById.get(resolvedSessionId);
|
|
404
456
|
if (cached) return cached;
|
|
405
457
|
|
|
406
|
-
const created = new
|
|
458
|
+
const created = new Session({
|
|
407
459
|
agentId: this.id,
|
|
408
460
|
projectRoot: this.path,
|
|
409
461
|
sessionId: resolvedSessionId,
|
|
410
462
|
tools: this.tools,
|
|
411
463
|
logger: this.logger,
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
464
|
+
getInstructionSystemBlocks: () => this.loadInstructionSystemBlocks(),
|
|
465
|
+
getServiceSystemBlocks: () => this.loadServiceSystemBlocks(),
|
|
466
|
+
getPluginSystemBlocks: () => this.loadPluginSystemBlocks(),
|
|
415
467
|
});
|
|
416
468
|
this.sessionsById.set(resolvedSessionId, created);
|
|
417
469
|
return created;
|
package/src/sdk/AgentSdkTypes.ts
CHANGED
|
@@ -41,6 +41,16 @@ export interface AgentOptions {
|
|
|
41
41
|
*/
|
|
42
42
|
tools?: Record<string, Tool>;
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* 调用方显式传入的静态基础指令。
|
|
46
|
+
*
|
|
47
|
+
* 关键点(中文)
|
|
48
|
+
* - `instruction` 是稳定、缓存友好的 system 前缀,不做动态变量替换。
|
|
49
|
+
* - SDK 不主动读取 `PROFILE.md` / `SOUL.md`;这类项目文件应由 city 或调用方读取后传入。
|
|
50
|
+
* - 未传入时,SDK 会使用包内最小 core instruction 作为 fallback。
|
|
51
|
+
*/
|
|
52
|
+
instruction?: string | string[];
|
|
53
|
+
|
|
44
54
|
/**
|
|
45
55
|
* 当前 agent 显式持有的 service 实例集合。
|
|
46
56
|
*
|
|
@@ -246,6 +256,109 @@ export type AgentSessionStreamEvent =
|
|
|
246
256
|
error: string;
|
|
247
257
|
};
|
|
248
258
|
|
|
259
|
+
/**
|
|
260
|
+
* Session system block 来源类型。
|
|
261
|
+
*/
|
|
262
|
+
export type AgentSessionSystemBlockSource =
|
|
263
|
+
| "core"
|
|
264
|
+
| "instruction"
|
|
265
|
+
| "service"
|
|
266
|
+
| "plugin"
|
|
267
|
+
| "session";
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Session system prompt 的单个组成块。
|
|
271
|
+
*/
|
|
272
|
+
export interface AgentSessionSystemBlock {
|
|
273
|
+
/**
|
|
274
|
+
* 当前 block 的来源层级。
|
|
275
|
+
*/
|
|
276
|
+
source: AgentSessionSystemBlockSource;
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 当前 block 在来源层级内的名称。
|
|
280
|
+
*
|
|
281
|
+
* 说明(中文)
|
|
282
|
+
* - `instruction` 通常使用 `agent`。
|
|
283
|
+
* - `service` / `plugin` 使用对应 service/plugin 名称。
|
|
284
|
+
* - `core` 使用 `default`。
|
|
285
|
+
* - `session` 使用当前 session 上下文名称。
|
|
286
|
+
*/
|
|
287
|
+
name: string;
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* 已归一化后的 system 文本内容。
|
|
291
|
+
*
|
|
292
|
+
* 关键点(中文)
|
|
293
|
+
* - SDK 不对 instruction 做动态变量替换。
|
|
294
|
+
* - 动态上下文应由调用方放入 user message。
|
|
295
|
+
*/
|
|
296
|
+
content: string;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* 当前 session 的稳定上下文信息。
|
|
301
|
+
*/
|
|
302
|
+
export interface AgentSessionSystemSessionInfo {
|
|
303
|
+
/**
|
|
304
|
+
* 当前 session 所属 agentId。
|
|
305
|
+
*/
|
|
306
|
+
agentId: string;
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* 当前 session 唯一标识。
|
|
310
|
+
*/
|
|
311
|
+
sessionId: string;
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* 当前 agent 绑定的项目根目录。
|
|
315
|
+
*/
|
|
316
|
+
projectRoot: string;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* 当前 session 首次创建时间。
|
|
320
|
+
*
|
|
321
|
+
* 关键点(中文)
|
|
322
|
+
* - 这是 session 初始化时落盘的稳定参考时间,按 Date/ISO 字符串对外展示。
|
|
323
|
+
* - 它不是每轮运行的当前时间,不会随着 `run()` / `stream()` 改变。
|
|
324
|
+
*/
|
|
325
|
+
createdAt: string;
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 当前 session 初始化时解析到的系统时区。
|
|
329
|
+
*
|
|
330
|
+
* 关键点(中文)
|
|
331
|
+
* - 这是 session 级参考时区,随创建信息一起固定。
|
|
332
|
+
* - 它不是每轮运行重新解析的动态时区。
|
|
333
|
+
*/
|
|
334
|
+
timezone: string;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 当前 session 生效的完整 system prompt 快照。
|
|
339
|
+
*/
|
|
340
|
+
export interface AgentSessionSystemSnapshot {
|
|
341
|
+
/**
|
|
342
|
+
* 当前 sessionId。
|
|
343
|
+
*/
|
|
344
|
+
sessionId: string;
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* 当前 session 的稳定上下文信息。
|
|
348
|
+
*
|
|
349
|
+
* 关键点(中文)
|
|
350
|
+
* - 这里包含 session 创建时间这类稳定参考信息。
|
|
351
|
+
* - 这里不包含当前时间、轮次、用户输入等每轮变化的数据。
|
|
352
|
+
* - 每轮动态信息应由调用方放入 user message,避免破坏 instruction 缓存命中。
|
|
353
|
+
*/
|
|
354
|
+
session: AgentSessionSystemSessionInfo;
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* 当前生效的 system blocks,按进入模型的顺序排列。
|
|
358
|
+
*/
|
|
359
|
+
blocks: AgentSessionSystemBlock[];
|
|
360
|
+
}
|
|
361
|
+
|
|
249
362
|
/**
|
|
250
363
|
* Session 元数据列表项。
|
|
251
364
|
*/
|
package/src/sdk/RemoteAgent.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
AgentSessionRunResult,
|
|
13
13
|
AgentSessionSetInput,
|
|
14
14
|
AgentSessionStreamEvent,
|
|
15
|
+
AgentSessionSystemSnapshot,
|
|
15
16
|
RemoteAgentOptions,
|
|
16
17
|
} from "@/sdk/AgentSdkTypes.js";
|
|
17
18
|
import type { SessionMessageV1 } from "@/session/types/SessionMessages.js";
|
|
@@ -78,18 +79,23 @@ class RemoteSession {
|
|
|
78
79
|
}
|
|
79
80
|
|
|
80
81
|
/**
|
|
81
|
-
* 读取远程 session 当前生效的 system prompt
|
|
82
|
+
* 读取远程 session 当前生效的 system prompt 快照。
|
|
82
83
|
*/
|
|
83
|
-
async system(): Promise<
|
|
84
|
+
async system(): Promise<AgentSessionSystemSnapshot> {
|
|
84
85
|
const response = await fetch(
|
|
85
86
|
`${this.baseUrl}/api/sdk/sessions/${encodeURIComponent(this.id)}/system`,
|
|
86
87
|
);
|
|
87
88
|
const payload = (await response.json()) as {
|
|
88
89
|
success?: boolean;
|
|
89
90
|
error?: string;
|
|
90
|
-
system?:
|
|
91
|
+
system?: AgentSessionSystemSnapshot;
|
|
91
92
|
};
|
|
92
|
-
if (
|
|
93
|
+
if (
|
|
94
|
+
!response.ok ||
|
|
95
|
+
!payload.success ||
|
|
96
|
+
!payload.system ||
|
|
97
|
+
!Array.isArray(payload.system.blocks)
|
|
98
|
+
) {
|
|
93
99
|
throw new Error(String(payload.error || "Remote session system failed"));
|
|
94
100
|
}
|
|
95
101
|
return payload.system;
|