@morlay/dsh-agent-loop 0.0.0-stage → 0.2.0-alpha.0

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.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/dist/contract.d.mts +16 -0
  3. package/dist/contract.mjs +6 -0
  4. package/dist/decorate-g56z_1VW.mjs +8 -0
  5. package/dist/index.d.mts +141 -0
  6. package/dist/index.mjs +3 -0
  7. package/dist/server.d.mts +39 -0
  8. package/dist/server.mjs +42 -0
  9. package/dist/src-CD0xJjMS.mjs +201 -0
  10. package/dist/testing.d.mts +45 -0
  11. package/dist/testing.mjs +51 -0
  12. package/dist/tool-executor.service-BlDMj9JS.mjs +114 -0
  13. package/dist/tool-executor.service-Bmyp71vf.d.mts +48 -0
  14. package/package.json +64 -3
  15. package/src/agent-loop.provide.ts +31 -0
  16. package/src/agent-loop.service.ts +74 -0
  17. package/src/agent-session-events.service.ts +85 -0
  18. package/src/checkpoint-policy.service.ts +27 -0
  19. package/src/contract/agent-loop.contract.ts +24 -0
  20. package/src/contract/index.ts +2 -0
  21. package/src/fork-in-process.service.ts +14 -0
  22. package/src/index.ts +18 -0
  23. package/src/repeat-tool-reminder.service.ts +11 -0
  24. package/src/server/agent-loop.provide.ts +26 -0
  25. package/src/server/checkpoint-policy.provide.ts +4 -0
  26. package/src/server/index.ts +5 -0
  27. package/src/server/subagent.provide.ts +6 -0
  28. package/src/server/subagents-endpoint.ts +68 -0
  29. package/src/server/tool-output.provide.ts +8 -0
  30. package/src/server/tool-policy.provide.ts +17 -0
  31. package/src/spawn-in-process.service.ts +14 -0
  32. package/src/spill-local.service.ts +11 -0
  33. package/src/spill-policy.service.ts +14 -0
  34. package/src/subagent-control.tool.ts +23 -0
  35. package/src/subagent-fork.tool.ts +30 -0
  36. package/src/subagent.provide.ts +21 -0
  37. package/src/subagent.tool.ts +33 -0
  38. package/src/testing/agent-loop-harness.ts +61 -0
  39. package/src/testing/index.ts +11 -0
  40. package/src/testing/preset-plane.ts +6 -0
  41. package/src/testing/providers.ts +27 -0
  42. package/src/timeout-policy.service.ts +14 -0
  43. package/src/tool-executor.service.ts +28 -0
  44. package/README.md +0 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 morlay
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,16 @@
1
+ import AgentLoopService from "@deepseek-ai/dsh-agent-loop";
2
+ import { AssertCoveredStrict, AssertTrue } from "@morlay/zephyr-app/client";
3
+ export declare abstract class AgentLoop {
4
+ abstract readonly config: AgentLoopService["config"];
5
+ abstract readonly create: AgentLoopService["create"];
6
+ abstract readonly createAgent: AgentLoopService["createAgent"];
7
+ abstract readonly resume: AgentLoopService["resume"];
8
+ }
9
+ /**
10
+ * 覆盖守卫(严格):上游 `AgentLoopService` 的每个成员都要有同名、同签名的对应项。
11
+ *
12
+ * 为什么不是 `implements`:上游那份是 cordis `Service` **类**(带私有成员),`implements` 会报
13
+ * TS2720("Did you mean to extend … and inherit its members as a subclass?")。签名守卫在这里等价:
14
+ * 漏成员或签名对不上,编译期都会报出成员名(诊断的第二行)。
15
+ */
16
+ export type AgentLoopCovered = AssertTrue<AssertCoveredStrict<AgentLoopService, AgentLoop>>;
@@ -0,0 +1,6 @@
1
+ import { Injectable } from "@morlay/zephyr";
2
+ import { t as __decorate } from "./decorate-g56z_1VW.mjs";
3
+ // src/contract/agent-loop.contract.ts
4
+ let AgentLoop = class AgentLoop {};
5
+ AgentLoop = __decorate([Injectable({ name: "agentLoop" })], AgentLoop);
6
+ export { AgentLoop };
@@ -0,0 +1,8 @@
1
+ // \0@oxc-project+runtime@0.160.0/helpers/esm/decorate.js
2
+ function __decorate(decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ }
8
+ export { __decorate as t };
@@ -0,0 +1,141 @@
1
+ import { Llm, SubagentRegistryDefault, SystemPrompt, ToolRegistry } from "@morlay/dsh-agent";
2
+ import { SessionProjectionStore, SessionStore } from "@morlay/dsh-session";
3
+ import { SessionPersistence } from "@morlay/dsh-session-persistence";
4
+ import * as SessionCheckpointPolicyPlugin from "@deepseek-ai/dsh-session-checkpoint-policy";
5
+ import { Context, OnInit } from "@morlay/zephyr";
6
+ import LocalSpillStore from "@deepseek-ai/dsh-spill-local";
7
+ import * as SpillPolicyPlugin from "@deepseek-ai/dsh-spill-policy";
8
+ import * as RepeatToolReminderPlugin from "@deepseek-ai/dsh-repeat-tool-reminder";
9
+ import * as TimeoutPolicyPlugin from "@deepseek-ai/dsh-tool-call-timeout-policy";
10
+ import { a as SubagentForkInProcess, i as ToolSubagentControl, n as ToolSubagentFork, o as SubagentSpawnInProcess, r as ToolSubagent, t as ToolExecutorNative } from "./tool-executor.service-Bmyp71vf.mjs";
11
+ import AgentLoopService from "@deepseek-ai/dsh-agent-loop";
12
+ import { AgentDirectory as AgentDirectory$1, Llm as Llm$1 } from "@morlay/dsh-agent/contract";
13
+ import { AgentLoop } from "./contract.mjs";
14
+ declare const SessionCheckpointPolicy_base: import("@morlay/zephyr/compat").AdaptedClass<import("@morlay/zephyr").TokenClass<unknown>, typeof SessionCheckpointPolicyPlugin>;
15
+ export declare class SessionCheckpointPolicy extends SessionCheckpointPolicy_base {
16
+ llm: Llm & import("@morlay/zephyr").Required<Llm>;
17
+ sessionPersistence: SessionPersistence & import("@morlay/zephyr").Required<SessionPersistence>;
18
+ sessions: SessionStore & import("@morlay/zephyr").Required<SessionStore>;
19
+ tools: ToolRegistry & import("@morlay/zephyr").Required<ToolRegistry>;
20
+ onInit(ctx: Context): Promise<void>;
21
+ }
22
+ declare const SpillLocal_base: import("@morlay/zephyr/compat").AdaptedClass<import("@morlay/zephyr").TokenClass<unknown>, typeof LocalSpillStore>;
23
+ export declare class SpillLocal extends SpillLocal_base {
24
+ onInit(ctx: Context): Promise<void>;
25
+ }
26
+ declare const SpillPolicy_base: import("@morlay/zephyr/compat").AdaptedClass<import("@morlay/zephyr").TokenClass<unknown>, typeof SpillPolicyPlugin>;
27
+ export declare class SpillPolicy extends SpillPolicy_base {
28
+ tools: ToolRegistry & import("@morlay/zephyr").Required<ToolRegistry>;
29
+ onInit(ctx: Context): Promise<void>;
30
+ }
31
+ declare const RepeatToolReminder_base: import("@morlay/zephyr/compat").AdaptedClass<import("@morlay/zephyr").TokenClass<unknown>, typeof RepeatToolReminderPlugin>;
32
+ export declare class RepeatToolReminder extends RepeatToolReminder_base {
33
+ onInit(ctx: Context): Promise<void>;
34
+ }
35
+ declare const TimeoutPolicy_base: import("@morlay/zephyr/compat").AdaptedClass<import("@morlay/zephyr").TokenClass<unknown>, typeof TimeoutPolicyPlugin>;
36
+ export declare class TimeoutPolicy extends TimeoutPolicy_base {
37
+ tools: ToolRegistry & import("@morlay/zephyr").Required<ToolRegistry>;
38
+ onInit(ctx: Context): Promise<void>;
39
+ }
40
+ declare const AgentLoopDefault_base: import("@morlay/zephyr/compat").AdaptedClass<typeof AgentLoop, typeof AgentLoopService>;
41
+ export declare class AgentLoopDefault extends AgentLoopDefault_base {
42
+ agents: AgentDirectory$1 & import("@morlay/zephyr").Required<AgentDirectory$1>;
43
+ sessions: SessionStore & import("@morlay/zephyr").Required<SessionStore>;
44
+ llm: Llm$1 & import("@morlay/zephyr").Required<Llm$1>;
45
+ tools: ToolRegistry & import("@morlay/zephyr").Required<ToolRegistry>;
46
+ systemPrompt: SystemPrompt & import("@morlay/zephyr").Required<SystemPrompt>;
47
+ sessionProjections: SessionProjectionStore & import("@morlay/zephyr").Required<SessionProjectionStore>;
48
+ /** per-agent 装配的装行能力:声明了 scope 的行(`@AgentScoped()`)由它在每个 agent 的 scope 里装。 */
49
+ private readonly scopedRows;
50
+ onInit(ctx: Context): Promise<void>;
51
+ /**
52
+ * 把 per-agent 装配链到上游的 agent 创建上。
53
+ *
54
+ * 为什么包 `kFace` 上的方法:上游 `agents.setFactory` 是独占的(重复注册直接抛
55
+ * `an agent factory is already registered`),`CreateAgentOptions.setup` 又由上游
56
+ * `api/session-controller` 传进来——我们唯一的缝就是这个 factory 实例本身。手法与
57
+ * `ApiSessionControllerHost.attachWorkspaceOnCreate` 相同:就地包装,不改 vendor。
58
+ *
59
+ * 位置:调用方的 `setup` 先跑(它组合的是这个 agent 的权威世界:模型选择、preset 行),我们的行
60
+ * 随后装进**同一个 `agentCtx`**——两边的注册都在 `agent/created` 与第一次装配之前就位,且都落在
61
+ * 这个 agent 的 scope 里。
62
+ */
63
+ private installAgentRowsOnCreate;
64
+ /**
65
+ * 把「装行」接到调用方的 `setup` 之后。
66
+ * @param setup - 调用方的 setup(可能没有)。
67
+ * @returns 组合后的 setup(commit 原样交回上游)。
68
+ */
69
+ private withAgentRows;
70
+ }
71
+ export declare class AgentSessionEvents implements OnInit {
72
+ private readonly logger;
73
+ /** 会话 → 它的 agent 的派发目标(会话对象在 agent 卸载后可能还活着,所以按会话登记)。 */
74
+ private readonly targets;
75
+ onInit(ctx: Context): void;
76
+ }
77
+ /**
78
+ * agent 的**循环期装配面**:一轮里才发生的事——循环本体、执行器、检查点、输出与调用策略、子代理派生。
79
+ *
80
+ * 与 `provideAgent()` 分开(最小装配原则)。执行器只有一个(上游 `setExecutor` 在根作用域互斥):
81
+ * 本部署选 PTC 时用 `provideAgentLoop(without(ToolExecutorNative))` 剔掉这一条。
82
+ * @returns `(...features) => 条目清单`。
83
+ */
84
+ export declare const provideAgentLoop: (...features: import("@morlay/zephyr").ProviderFeature[]) => readonly [typeof AgentLoopDefault, typeof AgentSessionEvents, typeof ToolExecutorNative, typeof SessionCheckpointPolicy, typeof SpillLocal, typeof SpillPolicy, {
85
+ readonly provide: typeof RepeatToolReminder;
86
+ readonly config: {
87
+ readonly thresholds: readonly [3, 5, 8];
88
+ readonly argumentsPreviewChars: 500;
89
+ };
90
+ }, typeof TimeoutPolicy, typeof import("@morlay/dsh-agent").McpResources, typeof import("@morlay/dsh-agent").SubagentRegistryDefault, {
91
+ readonly provide: typeof SubagentSpawnInProcess;
92
+ readonly config: {
93
+ readonly providerName: "spawn";
94
+ };
95
+ }, {
96
+ readonly provide: typeof SubagentForkInProcess;
97
+ readonly config: {
98
+ readonly providerName: "fork";
99
+ };
100
+ }, typeof ToolSubagentControl, {
101
+ readonly provide: typeof ToolSubagent;
102
+ readonly config: {
103
+ readonly provider: "spawn";
104
+ readonly toolName: "subagent";
105
+ };
106
+ }, {
107
+ readonly provide: typeof ToolSubagentFork;
108
+ readonly config: {
109
+ readonly provider: "fork";
110
+ readonly toolName: "subagent_fork";
111
+ };
112
+ }];
113
+ /**
114
+ * **headless 子代理**:注册表 + 两种运行方式 + 三条工具行。
115
+ *
116
+ * 不含 `remote.subagents` 的 wire 端点——那是 `provideSubagentEndpoints()`。
117
+ */
118
+ export declare const provideSubagent: (...features: import("@morlay/zephyr").ProviderFeature[]) => readonly [typeof SubagentRegistryDefault, {
119
+ readonly provide: typeof SubagentSpawnInProcess;
120
+ readonly config: {
121
+ readonly providerName: "spawn";
122
+ };
123
+ }, {
124
+ readonly provide: typeof SubagentForkInProcess;
125
+ readonly config: {
126
+ readonly providerName: "fork";
127
+ };
128
+ }, typeof ToolSubagentControl, {
129
+ readonly provide: typeof ToolSubagent;
130
+ readonly config: {
131
+ readonly provider: "spawn";
132
+ readonly toolName: "subagent";
133
+ };
134
+ }, {
135
+ readonly provide: typeof ToolSubagentFork;
136
+ readonly config: {
137
+ readonly provider: "fork";
138
+ readonly toolName: "subagent_fork";
139
+ };
140
+ }];
141
+ export { SubagentForkInProcess, SubagentSpawnInProcess, ToolExecutorNative, ToolSubagent, ToolSubagentControl, ToolSubagentFork };
package/dist/index.mjs ADDED
@@ -0,0 +1,3 @@
1
+ import { a as SpillLocal, c as AgentLoopDefault, i as SpillPolicy, n as TimeoutPolicy, o as SessionCheckpointPolicy, r as RepeatToolReminder, s as AgentSessionEvents, t as provideAgentLoop } from "./src-CD0xJjMS.mjs";
2
+ import { a as ToolSubagentControl, i as ToolSubagent, n as provideSubagent, o as SubagentForkInProcess, r as ToolSubagentFork, s as SubagentSpawnInProcess, t as ToolExecutorNative } from "./tool-executor.service-BlDMj9JS.mjs";
3
+ export { AgentLoopDefault, AgentSessionEvents, RepeatToolReminder, SessionCheckpointPolicy, SpillLocal, SpillPolicy, SubagentForkInProcess, SubagentSpawnInProcess, TimeoutPolicy, ToolExecutorNative, ToolSubagent, ToolSubagentControl, ToolSubagentFork, provideAgentLoop, provideSubagent };
@@ -0,0 +1,39 @@
1
+ import { a as SubagentForkInProcess, i as ToolSubagentControl, n as ToolSubagentFork, o as SubagentSpawnInProcess, r as ToolSubagent, t as ToolExecutorNative } from "./tool-executor.service-Bmyp71vf.mjs";
2
+ import { OnInit } from "@morlay/zephyr";
3
+ import { SubagentRegistry } from "@morlay/dsh-agent";
4
+ import { SubagentInterruptByUserValue, SubagentPromptRequest, SubagentPromptValue, SubagentsEndpoint } from "@morlay/dsh-agent/endpoint";
5
+ /** `interruptByUser` 的 wire 形参类型(上游 `interruptByUser` 的第一个形参)。 */
6
+ type SubagentInterruptByUserRequestId = Parameters<SubagentRegistry["interruptByUser"]>[0];
7
+ /** `subagents` 组的宿主实现。 */
8
+ declare class SubagentsEndpointDefault extends SubagentsEndpoint implements OnInit {
9
+ private readonly handlers;
10
+ private readonly subagents;
11
+ onInit(): void;
12
+ prompt(request: SubagentPromptRequest): Promise<SubagentPromptValue>;
13
+ interruptByUser(childSessionId: SubagentInterruptByUserRequestId): Promise<SubagentInterruptByUserValue>;
14
+ }
15
+ /** **wire 子代理**:headless 那几行 + `remote.subagents` 的两条端点。 */
16
+ export declare const provideSubagentEndpoints: (...features: import("@morlay/zephyr").ProviderFeature[]) => readonly [typeof import("@morlay/dsh-agent").SubagentRegistryDefault, {
17
+ readonly provide: typeof SubagentSpawnInProcess;
18
+ readonly config: {
19
+ readonly providerName: "spawn";
20
+ };
21
+ }, {
22
+ readonly provide: typeof SubagentForkInProcess;
23
+ readonly config: {
24
+ readonly providerName: "fork";
25
+ };
26
+ }, typeof ToolSubagentControl, {
27
+ readonly provide: typeof ToolSubagent;
28
+ readonly config: {
29
+ readonly provider: "spawn";
30
+ readonly toolName: "subagent";
31
+ };
32
+ }, {
33
+ readonly provide: typeof ToolSubagentFork;
34
+ readonly config: {
35
+ readonly provider: "fork";
36
+ readonly toolName: "subagent_fork";
37
+ };
38
+ }, typeof SubagentsEndpointDefault];
39
+ export { ToolExecutorNative };
@@ -0,0 +1,42 @@
1
+ import { n as provideSubagent, t as ToolExecutorNative } from "./tool-executor.service-BlDMj9JS.mjs";
2
+ import { Injectable, inject, provide } from "@morlay/zephyr";
3
+ import { SessionId } from "@morlay/dsh-session/endpoint";
4
+ import { RemoteHandlers } from "@morlay/zephyr-app";
5
+ import { RemoteGroup, registerRemoteGroup } from "@morlay/zephyr-app/remote";
6
+ import { SubagentRegistry } from "@morlay/dsh-agent";
7
+ import { SubagentsEndpoint } from "@morlay/dsh-agent/endpoint";
8
+ import { t as __decorate } from "./decorate-g56z_1VW.mjs";
9
+ // src/server/subagents-endpoint.ts
10
+ /**
11
+ * wire 上的裸字符串 → 上游的请求身份。
12
+ * @param value - wire 上的 `requestId`(调用方铸好的消息身份)。
13
+ * @returns 带 `session-request-id` 品牌的同一字符串。
14
+ */
15
+ function asPromptRequestId(value) {
16
+ return value;
17
+ }
18
+ let SubagentsEndpointDefault = class SubagentsEndpointDefault extends SubagentsEndpoint {
19
+ handlers = inject(RemoteHandlers);
20
+ subagents = inject(SubagentRegistry);
21
+ onInit() {
22
+ registerRemoteGroup(this.handlers, this);
23
+ }
24
+ async prompt(request) {
25
+ const upstream = {
26
+ requestId: asPromptRequestId(request.requestId),
27
+ childSessionId: SessionId(request.childSessionId),
28
+ delivery: request.delivery,
29
+ content: request.content,
30
+ ...request.clientTimeZone === void 0 ? {} : { clientTimeZone: request.clientTimeZone }
31
+ };
32
+ return await this.subagents.prompt(upstream, new AbortController().signal);
33
+ }
34
+ async interruptByUser(childSessionId) {
35
+ return this.subagents.interruptByUser(SessionId(childSessionId));
36
+ }
37
+ };
38
+ SubagentsEndpointDefault = __decorate([RemoteGroup(), Injectable()], SubagentsEndpointDefault);
39
+ // src/server/subagent.provide.ts
40
+ /** **wire 子代理**:headless 那几行 + `remote.subagents` 的两条端点。 */
41
+ const provideSubagentEndpoints = provide([...provideSubagent(), SubagentsEndpointDefault]);
42
+ export { ToolExecutorNative, provideSubagentEndpoints };
@@ -0,0 +1,201 @@
1
+ import { Injectable, Logger, configure, inject, provide } from "@morlay/zephyr";
2
+ import AgentLoopService from "@deepseek-ai/dsh-agent-loop";
3
+ import { AgentDirectory, AgentScopedRows, Llm, scopeOf, scopeTarget } from "@morlay/dsh-agent/contract";
4
+ import { fromCordis } from "@morlay/zephyr/compat";
5
+ import { SessionProjectionStore, SessionStore } from "@morlay/dsh-session";
6
+ import { Llm as Llm$1, McpResources, SystemPrompt, ToolRegistry } from "@morlay/dsh-agent";
7
+ import { AgentLoop } from "./contract.mjs";
8
+ import { t as __decorate } from "./decorate-g56z_1VW.mjs";
9
+ import { SessionPersistence } from "@morlay/dsh-session-persistence";
10
+ import * as SessionCheckpointPolicyPlugin from "@deepseek-ai/dsh-session-checkpoint-policy";
11
+ import LocalSpillStore from "@deepseek-ai/dsh-spill-local";
12
+ import * as SpillPolicyPlugin from "@deepseek-ai/dsh-spill-policy";
13
+ import * as RepeatToolReminderPlugin from "@deepseek-ai/dsh-repeat-tool-reminder";
14
+ import * as TimeoutPolicyPlugin from "@deepseek-ai/dsh-tool-call-timeout-policy";
15
+ import { n as provideSubagent, t as ToolExecutorNative } from "./tool-executor.service-BlDMj9JS.mjs";
16
+ // src/agent-loop.service.ts
17
+ let AgentLoopDefault = class AgentLoopDefault extends fromCordis(AgentLoop, AgentLoopService) {
18
+ agents = inject(AgentDirectory);
19
+ sessions = inject(SessionStore);
20
+ llm = inject(Llm);
21
+ tools = inject(ToolRegistry);
22
+ systemPrompt = inject(SystemPrompt);
23
+ sessionProjections = inject(SessionProjectionStore);
24
+ /** per-agent 装配的装行能力:声明了 scope 的行(`@AgentScoped()`)由它在每个 agent 的 scope 里装。 */
25
+ scopedRows = inject(AgentScopedRows);
26
+ async onInit(ctx) {
27
+ const mounted = await this.mount(ctx, void 0, {
28
+ agents: this.agents,
29
+ sessions: this.sessions,
30
+ llm: this.llm,
31
+ sessionProjections: this.sessionProjections
32
+ });
33
+ this.installAgentRowsOnCreate(mounted);
34
+ }
35
+ /**
36
+ * 把 per-agent 装配链到上游的 agent 创建上。
37
+ *
38
+ * 为什么包 `kFace` 上的方法:上游 `agents.setFactory` 是独占的(重复注册直接抛
39
+ * `an agent factory is already registered`),`CreateAgentOptions.setup` 又由上游
40
+ * `api/session-controller` 传进来——我们唯一的缝就是这个 factory 实例本身。手法与
41
+ * `ApiSessionControllerHost.attachWorkspaceOnCreate` 相同:就地包装,不改 vendor。
42
+ *
43
+ * 位置:调用方的 `setup` 先跑(它组合的是这个 agent 的权威世界:模型选择、preset 行),我们的行
44
+ * 随后装进**同一个 `agentCtx`**——两边的注册都在 `agent/created` 与第一次装配之前就位,且都落在
45
+ * 这个 agent 的 scope 里。
46
+ */
47
+ installAgentRowsOnCreate(mounted) {
48
+ const upstream = mounted;
49
+ if (upstream === void 0) return;
50
+ const createAgent = upstream.createAgent.bind(upstream);
51
+ const resume = upstream.resume.bind(upstream);
52
+ upstream.createAgent = (ownerCtx, options) => createAgent(ownerCtx, {
53
+ ...options,
54
+ setup: this.withAgentRows(options.setup)
55
+ });
56
+ upstream.resume = (ownerCtx, options) => resume(ownerCtx, {
57
+ ...options,
58
+ setup: this.withAgentRows(options.setup)
59
+ });
60
+ }
61
+ /**
62
+ * 把「装行」接到调用方的 `setup` 之后。
63
+ * @param setup - 调用方的 setup(可能没有)。
64
+ * @returns 组合后的 setup(commit 原样交回上游)。
65
+ */
66
+ withAgentRows(setup) {
67
+ return async (agentCtx, agent) => {
68
+ const commit = await setup?.(agentCtx, agent);
69
+ await this.scopedRows.install(agentCtx);
70
+ return commit;
71
+ };
72
+ }
73
+ };
74
+ AgentLoopDefault = __decorate([Injectable()], AgentLoopDefault);
75
+ // src/agent-session-events.service.ts
76
+ /**
77
+ * cordis 的监听器过滤符号(`Symbol.for('cordis.filter')`)。
78
+ *
79
+ * 本仓没有对 cordis 的运行期依赖,所以**不 import 它**:从上游自己的 carrier 上取一次
80
+ * (`scopeTarget` 的产物恰好带这个符号)。取不到就整条不装——宁可保留上游行为,也不双触发。
81
+ */
82
+ const FILTER = Object.getOwnPropertySymbols(scopeTarget({}, void 0))[0];
83
+ let AgentSessionEvents = class AgentSessionEvents {
84
+ logger = inject.optional(Logger);
85
+ /** 会话 → 它的 agent 的派发目标(会话对象在 agent 卸载后可能还活着,所以按会话登记)。 */
86
+ targets = /* @__PURE__ */ new WeakMap();
87
+ onInit(ctx) {
88
+ if (FILTER === void 0) {
89
+ this.logger?.warn("agent-loop: 取不到 cordis 的监听器过滤符号,会话事件不补发到 agent scope(注入会每步重发)");
90
+ return;
91
+ }
92
+ ctx.on("agent/created", (payload) => {
93
+ const agent = payload.agent;
94
+ const scopeCtx = agent.ctx;
95
+ this.targets.set(agent.session, {
96
+ events: scopeCtx.events,
97
+ key: scopeOf(agent.ctx)
98
+ });
99
+ scopeCtx.effect(() => () => {
100
+ this.targets.delete(agent.session);
101
+ });
102
+ });
103
+ ctx.on("session/event", (session, event) => {
104
+ if (typeof session !== "object" || session === null) return;
105
+ const target = this.targets.get(session);
106
+ if (target === void 0) return;
107
+ const carrier = { [FILTER]: (hookCtx) => scopeOf(hookCtx) === target.key };
108
+ for (const listener of target.events.dispatch("emit", [
109
+ carrier,
110
+ "session/event",
111
+ session,
112
+ event
113
+ ])) listener(session, event);
114
+ });
115
+ }
116
+ };
117
+ AgentSessionEvents = __decorate([Injectable()], AgentSessionEvents);
118
+ // src/checkpoint-policy.service.ts
119
+ let SessionCheckpointPolicy = class SessionCheckpointPolicy extends fromCordis(SessionCheckpointPolicyPlugin) {
120
+ llm = inject(Llm$1);
121
+ sessionPersistence = inject(SessionPersistence);
122
+ sessions = inject(SessionStore);
123
+ tools = inject(ToolRegistry);
124
+ async onInit(ctx) {
125
+ await this.mount(ctx, void 0, {
126
+ llm: this.llm,
127
+ sessionPersistence: this.sessionPersistence,
128
+ sessions: this.sessions,
129
+ tools: this.tools
130
+ });
131
+ }
132
+ };
133
+ SessionCheckpointPolicy = __decorate([Injectable()], SessionCheckpointPolicy);
134
+ // src/server/checkpoint-policy.provide.ts
135
+ const provideSessionCheckpointPolicy = provide([SessionCheckpointPolicy]);
136
+ // src/spill-local.service.ts
137
+ let SpillLocal = class SpillLocal extends fromCordis(LocalSpillStore) {
138
+ async onInit(ctx) {
139
+ await this.mount(ctx);
140
+ }
141
+ };
142
+ SpillLocal = __decorate([Injectable()], SpillLocal);
143
+ // src/spill-policy.service.ts
144
+ let SpillPolicy = class SpillPolicy extends fromCordis(SpillPolicyPlugin) {
145
+ tools = inject(ToolRegistry);
146
+ async onInit(ctx) {
147
+ await this.mount(ctx, void 0, { tools: this.tools });
148
+ }
149
+ };
150
+ SpillPolicy = __decorate([Injectable()], SpillPolicy);
151
+ // src/server/tool-output.provide.ts
152
+ const provideToolOutput = provide([SpillLocal, SpillPolicy]);
153
+ // src/repeat-tool-reminder.service.ts
154
+ let RepeatToolReminder = class RepeatToolReminder extends fromCordis(RepeatToolReminderPlugin) {
155
+ async onInit(ctx) {
156
+ await this.mount(ctx);
157
+ }
158
+ };
159
+ RepeatToolReminder = __decorate([Injectable()], RepeatToolReminder);
160
+ // src/timeout-policy.service.ts
161
+ let TimeoutPolicy = class TimeoutPolicy extends fromCordis(TimeoutPolicyPlugin) {
162
+ tools = inject(ToolRegistry);
163
+ async onInit(ctx) {
164
+ await this.mount(ctx, void 0, { tools: this.tools });
165
+ }
166
+ };
167
+ TimeoutPolicy = __decorate([Injectable()], TimeoutPolicy);
168
+ // src/server/tool-policy.provide.ts
169
+ const provideToolPolicy = provide([
170
+ {
171
+ provide: RepeatToolReminder,
172
+ config: {
173
+ thresholds: [
174
+ 3,
175
+ 5,
176
+ 8
177
+ ],
178
+ argumentsPreviewChars: 500
179
+ }
180
+ },
181
+ TimeoutPolicy,
182
+ McpResources
183
+ ]);
184
+ // src/agent-loop.provide.ts
185
+ /**
186
+ * agent 的**循环期装配面**:一轮里才发生的事——循环本体、执行器、检查点、输出与调用策略、子代理派生。
187
+ *
188
+ * 与 `provideAgent()` 分开(最小装配原则)。执行器只有一个(上游 `setExecutor` 在根作用域互斥):
189
+ * 本部署选 PTC 时用 `provideAgentLoop(without(ToolExecutorNative))` 剔掉这一条。
190
+ * @returns `(...features) => 条目清单`。
191
+ */
192
+ const provideAgentLoop = provide([
193
+ AgentLoopDefault,
194
+ AgentSessionEvents,
195
+ ToolExecutorNative,
196
+ ...provideSessionCheckpointPolicy(),
197
+ ...provideToolOutput(),
198
+ ...provideToolPolicy(),
199
+ ...provideSubagent()
200
+ ], [configure(AgentLoopDefault, { agents: [] })]);
201
+ export { SpillLocal as a, AgentLoopDefault as c, SpillPolicy as i, TimeoutPolicy as n, SessionCheckpointPolicy as o, RepeatToolReminder as r, AgentSessionEvents as s, provideAgentLoop as t };
@@ -0,0 +1,45 @@
1
+ import { ProviderList } from "@morlay/zephyr";
2
+ import PresetGroup from "@deepseek-ai/cordis-plugin-group";
3
+ import PresetLoader from "@deepseek-ai/cordis-plugin-loader";
4
+ /**
5
+ * 循环期 headless 层的最小清单:循环本体、执行器、检查点、输出与调用策略、子代理派生,
6
+ * 以及它们要的下游(shell → sandbox → 工作目录)。
7
+ *
8
+ * 不含 `AppEnv`(部署事实由调用方给)、不含 wire。要"跑一轮对话"的测试装这份清单就够。
9
+ * @param options.workspaceRoot - 沙箱的可写根(测试一般给临时目录)。
10
+ * @returns 条目清单。
11
+ */
12
+ export declare function headlessLoopProviders(options: {
13
+ readonly workspaceRoot: string;
14
+ }): ProviderList;
15
+ /** 上游 `Agent` 的类型面(vendor 的模块增强,本仓 program 看不到整份形状)。 */
16
+ type Agent = import("@morlay/dsh-agent/contract").Agent;
17
+ /** 装配选项(与上游 testkit 的三个选项同义)。 */
18
+ interface LoopTestOptions {
19
+ /** 装上工作目录(要读目录的用例才需要)。 */
20
+ readonly workingDirectory?: boolean;
21
+ /** 提示词配置(透传给 `SystemPromptDefault`,形状由上游 `Config` 决定)。 */
22
+ readonly systemPrompt?: Record<string, unknown>;
23
+ /** 工具面:`native` 或 `ptc`(默认 native)。 */
24
+ readonly tools?: {
25
+ readonly mode?: "native" | "ptc";
26
+ };
27
+ }
28
+ /**
29
+ * 跑一轮对话所需的装配清单(本仓的装配方式)。
30
+ *
31
+ * 装「骨架(含提示词与默认模型)+ 会话 + 投影 + 执行器」;`headless` 时到此为止,不装循环本体。
32
+ * @param options - 见 `LoopTestOptions` 与 `headless`。
33
+ * @returns 条目清单。
34
+ */
35
+ export declare function loopTestProviders(options?: LoopTestOptions & {
36
+ readonly headless?: boolean;
37
+ }): ProviderList;
38
+ /** 测试驱动面:建真 agent 并驱动它的收件箱(与上游 harness 同形)。 */
39
+ interface LoopHarness {
40
+ /** 建一个真 agent(与它的会话)。 */
41
+ create(id: unknown, options?: unknown, meta?: unknown): Promise<Agent>;
42
+ /** 这一轮的收件箱:投递一条用户消息并取回要处理的那几条。 */
43
+ claim(agent: Agent, target: unknown, turn: number): readonly unknown[];
44
+ }
45
+ export { type Agent, type LoopHarness, type LoopTestOptions, PresetGroup, PresetLoader };
@@ -0,0 +1,51 @@
1
+ import { configure, provide } from "@morlay/zephyr";
2
+ import { SandboxPolicyDefault, provideSandbox } from "@morlay/dsh-sandbox";
3
+ import { provideShell } from "@morlay/dsh-shell/server";
4
+ import { SystemPromptDefault, provideAgent, provideWorkingDirectory } from "@morlay/dsh-agent";
5
+ import { c as AgentLoopDefault, s as AgentSessionEvents, t as provideAgentLoop } from "./src-CD0xJjMS.mjs";
6
+ import { t as ToolExecutorNative } from "./tool-executor.service-BlDMj9JS.mjs";
7
+ import { provideSession, provideSessionProjection } from "@morlay/dsh-session/server";
8
+ import PresetGroup from "@deepseek-ai/cordis-plugin-group";
9
+ import PresetLoader from "@deepseek-ai/cordis-plugin-loader";
10
+ // src/testing/providers.ts
11
+ /**
12
+ * 循环期 headless 层的最小清单:循环本体、执行器、检查点、输出与调用策略、子代理派生,
13
+ * 以及它们要的下游(shell → sandbox → 工作目录)。
14
+ *
15
+ * 不含 `AppEnv`(部署事实由调用方给)、不含 wire。要"跑一轮对话"的测试装这份清单就够。
16
+ * @param options.workspaceRoot - 沙箱的可写根(测试一般给临时目录)。
17
+ * @returns 条目清单。
18
+ */
19
+ function headlessLoopProviders(options) {
20
+ return [
21
+ ...provideShell(),
22
+ ...provideSandbox(configure(SandboxPolicyDefault, {
23
+ mode: "workspace-write",
24
+ workspaceRoot: options.workspaceRoot
25
+ })),
26
+ ...provideWorkingDirectory(),
27
+ ...provideAgentLoop()
28
+ ];
29
+ }
30
+ // src/testing/agent-loop-harness.ts
31
+ /** 循环本体那一份(默认 `agents: []`)。 */
32
+ const loopRow = provide([AgentLoopDefault, AgentSessionEvents], [configure(AgentLoopDefault, { agents: [] })]);
33
+ /**
34
+ * 跑一轮对话所需的装配清单(本仓的装配方式)。
35
+ *
36
+ * 装「骨架(含提示词与默认模型)+ 会话 + 投影 + 执行器」;`headless` 时到此为止,不装循环本体。
37
+ * @param options - 见 `LoopTestOptions` 与 `headless`。
38
+ * @returns 条目清单。
39
+ */
40
+ function loopTestProviders(options = {}) {
41
+ const systemPrompt = options.systemPrompt;
42
+ const personaLed = systemPrompt === void 0 ? [...provideAgent()] : [...provideAgent(configure(SystemPromptDefault, systemPrompt))];
43
+ return [
44
+ ...provideSession(),
45
+ ...provideSessionProjection(),
46
+ ...personaLed,
47
+ ToolExecutorNative,
48
+ ...options.headless === true ? [] : loopRow()
49
+ ];
50
+ }
51
+ export { PresetGroup, PresetLoader, headlessLoopProviders, loopTestProviders };