@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.
- package/LICENSE +21 -0
- package/dist/contract.d.mts +16 -0
- package/dist/contract.mjs +6 -0
- package/dist/decorate-g56z_1VW.mjs +8 -0
- package/dist/index.d.mts +141 -0
- package/dist/index.mjs +3 -0
- package/dist/server.d.mts +39 -0
- package/dist/server.mjs +42 -0
- package/dist/src-CD0xJjMS.mjs +201 -0
- package/dist/testing.d.mts +45 -0
- package/dist/testing.mjs +51 -0
- package/dist/tool-executor.service-BlDMj9JS.mjs +114 -0
- package/dist/tool-executor.service-Bmyp71vf.d.mts +48 -0
- package/package.json +64 -3
- package/src/agent-loop.provide.ts +31 -0
- package/src/agent-loop.service.ts +74 -0
- package/src/agent-session-events.service.ts +85 -0
- package/src/checkpoint-policy.service.ts +27 -0
- package/src/contract/agent-loop.contract.ts +24 -0
- package/src/contract/index.ts +2 -0
- package/src/fork-in-process.service.ts +14 -0
- package/src/index.ts +18 -0
- package/src/repeat-tool-reminder.service.ts +11 -0
- package/src/server/agent-loop.provide.ts +26 -0
- package/src/server/checkpoint-policy.provide.ts +4 -0
- package/src/server/index.ts +5 -0
- package/src/server/subagent.provide.ts +6 -0
- package/src/server/subagents-endpoint.ts +68 -0
- package/src/server/tool-output.provide.ts +8 -0
- package/src/server/tool-policy.provide.ts +17 -0
- package/src/spawn-in-process.service.ts +14 -0
- package/src/spill-local.service.ts +11 -0
- package/src/spill-policy.service.ts +14 -0
- package/src/subagent-control.tool.ts +23 -0
- package/src/subagent-fork.tool.ts +30 -0
- package/src/subagent.provide.ts +21 -0
- package/src/subagent.tool.ts +33 -0
- package/src/testing/agent-loop-harness.ts +61 -0
- package/src/testing/index.ts +11 -0
- package/src/testing/preset-plane.ts +6 -0
- package/src/testing/providers.ts +27 -0
- package/src/timeout-policy.service.ts +14 -0
- package/src/tool-executor.service.ts +28 -0
- package/README.md +0 -3
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { t as __decorate } from "./decorate-g56z_1VW.mjs";
|
|
2
|
+
import { Injectable, inject, provide } from "@morlay/zephyr";
|
|
3
|
+
import { SubagentRegistry } from "@morlay/dsh-agent/contract";
|
|
4
|
+
import { fromCordis } from "@morlay/zephyr/compat";
|
|
5
|
+
import { SessionProjectionStore } from "@morlay/dsh-session";
|
|
6
|
+
import { AgentDirectory as AgentDirectory$1, SubagentRegistryDefault, SystemPrompt, ToolRegistry } from "@morlay/dsh-agent";
|
|
7
|
+
import * as SubagentSpawnInProcessPlugin from "@deepseek-ai/dsh-subagent-spawn-in-process";
|
|
8
|
+
import * as SubagentForkInProcessPlugin from "@deepseek-ai/dsh-subagent-fork-in-process";
|
|
9
|
+
import * as ToolSubagentControlPlugin from "@deepseek-ai/dsh-tool-subagent-control";
|
|
10
|
+
import * as ToolSubagentPlugin from "@deepseek-ai/dsh-tool-subagent";
|
|
11
|
+
import * as ToolExecutorNativePlugin from "@deepseek-ai/dsh-tool-registry-native";
|
|
12
|
+
// src/spawn-in-process.service.ts
|
|
13
|
+
let SubagentSpawnInProcess = class SubagentSpawnInProcess extends fromCordis(SubagentSpawnInProcessPlugin) {
|
|
14
|
+
subagents = inject(SubagentRegistry);
|
|
15
|
+
async onInit(ctx) {
|
|
16
|
+
await this.mount(ctx, void 0, { subagents: this.subagents });
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
SubagentSpawnInProcess = __decorate([Injectable()], SubagentSpawnInProcess);
|
|
20
|
+
// src/fork-in-process.service.ts
|
|
21
|
+
let SubagentForkInProcess = class SubagentForkInProcess extends fromCordis(SubagentForkInProcessPlugin) {
|
|
22
|
+
subagents = inject(SubagentRegistry);
|
|
23
|
+
async onInit(ctx) {
|
|
24
|
+
await this.mount(ctx, void 0, { subagents: this.subagents });
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
SubagentForkInProcess = __decorate([Injectable()], SubagentForkInProcess);
|
|
28
|
+
// src/subagent-control.tool.ts
|
|
29
|
+
let ToolSubagentControl = class ToolSubagentControl extends fromCordis(ToolSubagentControlPlugin) {
|
|
30
|
+
tools = inject(ToolRegistry);
|
|
31
|
+
subagents = inject(SubagentRegistry);
|
|
32
|
+
agents = inject(AgentDirectory$1);
|
|
33
|
+
async onInit(ctx) {
|
|
34
|
+
await this.mount(ctx, void 0, {
|
|
35
|
+
tools: this.tools,
|
|
36
|
+
subagents: this.subagents,
|
|
37
|
+
agents: this.agents
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
ToolSubagentControl = __decorate([Injectable()], ToolSubagentControl);
|
|
42
|
+
// src/subagent.tool.ts
|
|
43
|
+
let ToolSubagent = class ToolSubagent extends fromCordis(ToolSubagentPlugin) {
|
|
44
|
+
tools = inject(ToolRegistry);
|
|
45
|
+
subagents = inject(SubagentRegistry);
|
|
46
|
+
systemPrompt = inject(SystemPrompt);
|
|
47
|
+
sessionProjections = inject(SessionProjectionStore);
|
|
48
|
+
async onInit(ctx) {
|
|
49
|
+
await this.mount(ctx, void 0, {
|
|
50
|
+
tools: this.tools,
|
|
51
|
+
subagents: this.subagents,
|
|
52
|
+
systemPrompt: this.systemPrompt,
|
|
53
|
+
sessionProjections: this.sessionProjections
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
ToolSubagent = __decorate([Injectable()], ToolSubagent);
|
|
58
|
+
// src/subagent-fork.tool.ts
|
|
59
|
+
let ToolSubagentFork = class ToolSubagentFork extends fromCordis(ToolSubagentPlugin) {
|
|
60
|
+
tools = inject(ToolRegistry);
|
|
61
|
+
subagents = inject(SubagentRegistry);
|
|
62
|
+
systemPrompt = inject(SystemPrompt);
|
|
63
|
+
sessionProjections = inject(SessionProjectionStore);
|
|
64
|
+
async onInit(ctx) {
|
|
65
|
+
await this.mount(ctx, void 0, {
|
|
66
|
+
tools: this.tools,
|
|
67
|
+
subagents: this.subagents,
|
|
68
|
+
systemPrompt: this.systemPrompt,
|
|
69
|
+
sessionProjections: this.sessionProjections
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
ToolSubagentFork = __decorate([Injectable()], ToolSubagentFork);
|
|
74
|
+
// src/subagent.provide.ts
|
|
75
|
+
/**
|
|
76
|
+
* **headless 子代理**:注册表 + 两种运行方式 + 三条工具行。
|
|
77
|
+
*
|
|
78
|
+
* 不含 `remote.subagents` 的 wire 端点——那是 `provideSubagentEndpoints()`。
|
|
79
|
+
*/
|
|
80
|
+
const provideSubagent = provide([
|
|
81
|
+
SubagentRegistryDefault,
|
|
82
|
+
{
|
|
83
|
+
provide: SubagentSpawnInProcess,
|
|
84
|
+
config: { providerName: "spawn" }
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
provide: SubagentForkInProcess,
|
|
88
|
+
config: { providerName: "fork" }
|
|
89
|
+
},
|
|
90
|
+
ToolSubagentControl,
|
|
91
|
+
{
|
|
92
|
+
provide: ToolSubagent,
|
|
93
|
+
config: {
|
|
94
|
+
provider: "spawn",
|
|
95
|
+
toolName: "subagent"
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
provide: ToolSubagentFork,
|
|
100
|
+
config: {
|
|
101
|
+
provider: "fork",
|
|
102
|
+
toolName: "subagent_fork"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
]);
|
|
106
|
+
// src/tool-executor.service.ts
|
|
107
|
+
let ToolExecutorNative = class ToolExecutorNative extends fromCordis(ToolExecutorNativePlugin) {
|
|
108
|
+
tools = inject(ToolRegistry);
|
|
109
|
+
async onInit(ctx) {
|
|
110
|
+
await this.mount(ctx, void 0, { tools: this.tools });
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
ToolExecutorNative = __decorate([Injectable()], ToolExecutorNative);
|
|
114
|
+
export { ToolSubagentControl as a, ToolSubagent as i, provideSubagent as n, SubagentForkInProcess as o, ToolSubagentFork as r, SubagentSpawnInProcess as s, ToolExecutorNative as t };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { AgentDirectory, SystemPrompt, ToolRegistry } from "@morlay/dsh-agent";
|
|
2
|
+
import { SessionProjectionStore } from "@morlay/dsh-session";
|
|
3
|
+
import { Context } from "@morlay/zephyr";
|
|
4
|
+
import * as SubagentSpawnInProcessPlugin from "@deepseek-ai/dsh-subagent-spawn-in-process";
|
|
5
|
+
import { SubagentRegistry as SubagentRegistry$1 } from "@morlay/dsh-agent/contract";
|
|
6
|
+
import * as SubagentForkInProcessPlugin from "@deepseek-ai/dsh-subagent-fork-in-process";
|
|
7
|
+
import * as ToolSubagentControlPlugin from "@deepseek-ai/dsh-tool-subagent-control";
|
|
8
|
+
import * as ToolSubagentPlugin from "@deepseek-ai/dsh-tool-subagent";
|
|
9
|
+
import * as ToolExecutorNativePlugin from "@deepseek-ai/dsh-tool-registry-native";
|
|
10
|
+
declare const SubagentSpawnInProcess_base: import("@morlay/zephyr/compat").AdaptedClass<import("@morlay/zephyr").TokenClass<unknown>, typeof SubagentSpawnInProcessPlugin>;
|
|
11
|
+
declare class SubagentSpawnInProcess extends SubagentSpawnInProcess_base {
|
|
12
|
+
subagents: SubagentRegistry$1 & import("@morlay/zephyr").Required<SubagentRegistry$1>;
|
|
13
|
+
onInit(ctx: Context): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
declare const SubagentForkInProcess_base: import("@morlay/zephyr/compat").AdaptedClass<import("@morlay/zephyr").TokenClass<unknown>, typeof SubagentForkInProcessPlugin>;
|
|
16
|
+
declare class SubagentForkInProcess extends SubagentForkInProcess_base {
|
|
17
|
+
subagents: SubagentRegistry$1 & import("@morlay/zephyr").Required<SubagentRegistry$1>;
|
|
18
|
+
onInit(ctx: Context): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
declare const ToolSubagentControl_base: import("@morlay/zephyr/compat").AdaptedClass<import("@morlay/zephyr").TokenClass<unknown>, typeof ToolSubagentControlPlugin>;
|
|
21
|
+
declare class ToolSubagentControl extends ToolSubagentControl_base {
|
|
22
|
+
tools: ToolRegistry & import("@morlay/zephyr").Required<ToolRegistry>;
|
|
23
|
+
subagents: SubagentRegistry$1 & import("@morlay/zephyr").Required<SubagentRegistry$1>;
|
|
24
|
+
agents: AgentDirectory & import("@morlay/zephyr").Required<AgentDirectory>;
|
|
25
|
+
onInit(ctx: Context): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
declare const ToolSubagent_base: import("@morlay/zephyr/compat").AdaptedClass<import("@morlay/zephyr").TokenClass<unknown>, typeof ToolSubagentPlugin>;
|
|
28
|
+
declare class ToolSubagent extends ToolSubagent_base {
|
|
29
|
+
tools: ToolRegistry & import("@morlay/zephyr").Required<ToolRegistry>;
|
|
30
|
+
subagents: SubagentRegistry$1 & import("@morlay/zephyr").Required<SubagentRegistry$1>;
|
|
31
|
+
systemPrompt: SystemPrompt & import("@morlay/zephyr").Required<SystemPrompt>;
|
|
32
|
+
sessionProjections: SessionProjectionStore & import("@morlay/zephyr").Required<SessionProjectionStore>;
|
|
33
|
+
onInit(ctx: Context): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
declare const ToolSubagentFork_base: import("@morlay/zephyr/compat").AdaptedClass<import("@morlay/zephyr").TokenClass<unknown>, typeof ToolSubagentPlugin>;
|
|
36
|
+
declare class ToolSubagentFork extends ToolSubagentFork_base {
|
|
37
|
+
tools: ToolRegistry & import("@morlay/zephyr").Required<ToolRegistry>;
|
|
38
|
+
subagents: SubagentRegistry$1 & import("@morlay/zephyr").Required<SubagentRegistry$1>;
|
|
39
|
+
systemPrompt: SystemPrompt & import("@morlay/zephyr").Required<SystemPrompt>;
|
|
40
|
+
sessionProjections: SessionProjectionStore & import("@morlay/zephyr").Required<SessionProjectionStore>;
|
|
41
|
+
onInit(ctx: Context): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
declare const ToolExecutorNative_base: import("@morlay/zephyr/compat").AdaptedClass<import("@morlay/zephyr").TokenClass<unknown>, typeof ToolExecutorNativePlugin>;
|
|
44
|
+
declare class ToolExecutorNative extends ToolExecutorNative_base {
|
|
45
|
+
tools: ToolRegistry & import("@morlay/zephyr").Required<ToolRegistry>;
|
|
46
|
+
onInit(ctx: Context): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
export { SubagentForkInProcess as a, ToolSubagentControl as i, ToolSubagentFork as n, SubagentSpawnInProcess as o, ToolSubagent as r, ToolExecutorNative as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@morlay/dsh-agent-loop",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.2.0-alpha.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "agent 的循环期:一轮里的执行器、检查点、工具输出与调用策略、子代理派生。契约在 dsh-agent,行为在这里。",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"src",
|
|
9
|
+
"!**/__tests__"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"default": "./dist/index.mjs"
|
|
16
|
+
},
|
|
17
|
+
"./contract": {
|
|
18
|
+
"types": "./dist/contract.d.mts",
|
|
19
|
+
"default": "./dist/contract.mjs"
|
|
20
|
+
},
|
|
21
|
+
"./server": {
|
|
22
|
+
"types": "./dist/server.d.mts",
|
|
23
|
+
"default": "./dist/server.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./testing": {
|
|
26
|
+
"types": "./dist/testing.d.mts",
|
|
27
|
+
"default": "./dist/testing.mjs"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@morlay/dsh-agent": "^0.2.0-alpha.0",
|
|
33
|
+
"@morlay/dsh-session": "^0.2.0-alpha.0",
|
|
34
|
+
"@morlay/dsh-session-persistence": "^0.2.0-alpha.0",
|
|
35
|
+
"@morlay/zephyr": "^0.2.0-alpha.0",
|
|
36
|
+
"@morlay/zephyr-app": "^0.2.0-alpha.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@deepseek-ai/cordis-plugin-group": "^1.2.0-alpha.2",
|
|
40
|
+
"@deepseek-ai/cordis-plugin-loader": "^1.2.0-alpha.2",
|
|
41
|
+
"@morlay/dsh-sandbox": "^0.2.0-alpha.0",
|
|
42
|
+
"@morlay/dsh-shell": "^0.2.0-alpha.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@deepseek-ai/dsh-agent": "^0.2.3-alpha.3",
|
|
46
|
+
"@deepseek-ai/dsh-agent-loop": "^0.2.3-alpha.3",
|
|
47
|
+
"@deepseek-ai/dsh-repeat-tool-reminder": "^0.2.3-alpha.3",
|
|
48
|
+
"@deepseek-ai/dsh-session-checkpoint-policy": "^0.2.3-alpha.3",
|
|
49
|
+
"@deepseek-ai/dsh-spill-local": "^0.2.3-alpha.3",
|
|
50
|
+
"@deepseek-ai/dsh-spill-policy": "^0.2.3-alpha.3",
|
|
51
|
+
"@deepseek-ai/dsh-subagent-fork-in-process": "^0.2.3-alpha.3",
|
|
52
|
+
"@deepseek-ai/dsh-subagent-spawn-in-process": "^0.2.3-alpha.3",
|
|
53
|
+
"@deepseek-ai/dsh-tool-call-timeout-policy": "^0.2.3-alpha.3",
|
|
54
|
+
"@deepseek-ai/dsh-tool-registry-native": "^0.2.3-alpha.3",
|
|
55
|
+
"@deepseek-ai/dsh-tool-subagent": "^0.2.3-alpha.3",
|
|
56
|
+
"@deepseek-ai/dsh-tool-subagent-control": "^0.2.3-alpha.3"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"dsh": ">= 0.2.3-alpha.3 < 0.2.4"
|
|
60
|
+
},
|
|
61
|
+
"repository": {
|
|
62
|
+
"type": "git",
|
|
63
|
+
"url": "https://github.com/morlay/zephyr.git",
|
|
64
|
+
"directory": "packages/agent/dsh-agent-loop"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {}
|
|
6
67
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { configure, provide } from "@morlay/zephyr";
|
|
2
|
+
import { AgentLoopDefault } from "./agent-loop.service.ts";
|
|
3
|
+
import { AgentSessionEvents } from "./agent-session-events.service.ts";
|
|
4
|
+
import { provideSessionCheckpointPolicy } from "./server/checkpoint-policy.provide.ts";
|
|
5
|
+
import { provideToolOutput } from "./server/tool-output.provide.ts";
|
|
6
|
+
import { provideToolPolicy } from "./server/tool-policy.provide.ts";
|
|
7
|
+
import { provideSubagent } from "./subagent.provide.ts";
|
|
8
|
+
import { ToolExecutorNative } from "./tool-executor.service.ts";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* agent 的**循环期装配面**:一轮里才发生的事——循环本体、执行器、检查点、输出与调用策略、子代理派生。
|
|
12
|
+
*
|
|
13
|
+
* 与 `provideAgent()` 分开(最小装配原则)。执行器只有一个(上游 `setExecutor` 在根作用域互斥):
|
|
14
|
+
* 本部署选 PTC 时用 `provideAgentLoop(without(ToolExecutorNative))` 剔掉这一条。
|
|
15
|
+
* @returns `(...features) => 条目清单`。
|
|
16
|
+
*/
|
|
17
|
+
export const provideAgentLoop = provide(
|
|
18
|
+
[
|
|
19
|
+
// `AgentLoopDefault` 必须在清单里:`configure` 只替换**既有**条目,不新增。
|
|
20
|
+
AgentLoopDefault,
|
|
21
|
+
// 会话事件补发到 agent scope:上游的「注入不重发」判据靠它(见该文件头)。
|
|
22
|
+
AgentSessionEvents,
|
|
23
|
+
ToolExecutorNative,
|
|
24
|
+
...provideSessionCheckpointPolicy(),
|
|
25
|
+
...provideToolOutput(),
|
|
26
|
+
...provideToolPolicy(),
|
|
27
|
+
...provideSubagent(),
|
|
28
|
+
],
|
|
29
|
+
// 默认配置走 `configure`(保证类型推导),不写成清单里的 `{ provide, config }`。
|
|
30
|
+
[configure(AgentLoopDefault, { agents: [] })],
|
|
31
|
+
);
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// agent loop:上游 `AgentLoopService` 经 `fromCordis` 接管。
|
|
2
|
+
import AgentLoopService from "@deepseek-ai/dsh-agent-loop";
|
|
3
|
+
import { AgentScopedRows } from "@morlay/dsh-agent/contract";
|
|
4
|
+
import { Injectable, inject, type Context } from "@morlay/zephyr";
|
|
5
|
+
import { fromCordis } from "@morlay/zephyr/compat";
|
|
6
|
+
import { Llm } from "@morlay/dsh-agent/contract";
|
|
7
|
+
import { SessionStore } from "@morlay/dsh-session";
|
|
8
|
+
import { SessionProjectionStore } from "@morlay/dsh-session";
|
|
9
|
+
import { ToolRegistry } from "@morlay/dsh-agent";
|
|
10
|
+
import { AgentLoop } from "./contract/index.ts";
|
|
11
|
+
import { AgentDirectory } from "@morlay/dsh-agent/contract";
|
|
12
|
+
import { SystemPrompt } from "@morlay/dsh-agent";
|
|
13
|
+
/** 上游 `AgentSetup`:`(agentCtx, agent) => commit | void`(从工厂签名派生,不另引一份上游类型)。 */
|
|
14
|
+
type AgentSetup = NonNullable<Parameters<AgentLoopService["createAgent"]>[1]["setup"]>;
|
|
15
|
+
@Injectable()
|
|
16
|
+
export class AgentLoopDefault extends fromCordis(AgentLoop, AgentLoopService) {
|
|
17
|
+
agents = inject(AgentDirectory);
|
|
18
|
+
sessions = inject(SessionStore);
|
|
19
|
+
llm = inject(Llm);
|
|
20
|
+
tools = inject(ToolRegistry);
|
|
21
|
+
systemPrompt = inject(SystemPrompt);
|
|
22
|
+
sessionProjections = inject(SessionProjectionStore);
|
|
23
|
+
/** per-agent 装配的装行能力:声明了 scope 的行(`@AgentScoped()`)由它在每个 agent 的 scope 里装。 */
|
|
24
|
+
private readonly scopedRows = inject(AgentScopedRows);
|
|
25
|
+
override async onInit(ctx: Context): Promise<void> {
|
|
26
|
+
// `tools` / `systemPrompt` **不进 deps**:它们是真正的 cordis 服务(在 store 里),
|
|
27
|
+
// 而 `deps` 会一路继承到 `agent.ctx`——上游 `file-reference-local` 用
|
|
28
|
+
// `agent.ctx.inject(['systemPrompt','tools'], …)` 注册 per-agent 段落,一旦这两个名字走了
|
|
29
|
+
// deps 短路,回调里拿到的是**我们的契约实例**,调用方 ctx 变成行 ctx(没有 scope 标记),
|
|
30
|
+
// 段落于是掉进 global 层,第二个 agent 再注册同名段落就撞。
|
|
31
|
+
const mounted = await this.mount(ctx, undefined, {
|
|
32
|
+
agents: this.agents,
|
|
33
|
+
sessions: this.sessions,
|
|
34
|
+
llm: this.llm,
|
|
35
|
+
sessionProjections: this.sessionProjections,
|
|
36
|
+
});
|
|
37
|
+
this.installAgentRowsOnCreate(mounted);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 把 per-agent 装配链到上游的 agent 创建上。
|
|
41
|
+
*
|
|
42
|
+
* 为什么包 `kFace` 上的方法:上游 `agents.setFactory` 是独占的(重复注册直接抛
|
|
43
|
+
* `an agent factory is already registered`),`CreateAgentOptions.setup` 又由上游
|
|
44
|
+
* `api/session-controller` 传进来——我们唯一的缝就是这个 factory 实例本身。手法与
|
|
45
|
+
* `ApiSessionControllerHost.attachWorkspaceOnCreate` 相同:就地包装,不改 vendor。
|
|
46
|
+
*
|
|
47
|
+
* 位置:调用方的 `setup` 先跑(它组合的是这个 agent 的权威世界:模型选择、preset 行),我们的行
|
|
48
|
+
* 随后装进**同一个 `agentCtx`**——两边的注册都在 `agent/created` 与第一次装配之前就位,且都落在
|
|
49
|
+
* 这个 agent 的 scope 里。
|
|
50
|
+
*/
|
|
51
|
+
private installAgentRowsOnCreate(mounted: unknown): void {
|
|
52
|
+
const upstream = mounted as Pick<AgentLoopService, "createAgent" | "resume"> | undefined;
|
|
53
|
+
if (upstream === undefined) return;
|
|
54
|
+
const createAgent = upstream.createAgent.bind(upstream);
|
|
55
|
+
const resume = upstream.resume.bind(upstream);
|
|
56
|
+
upstream.createAgent = (ownerCtx, options) =>
|
|
57
|
+
createAgent(ownerCtx, { ...options, setup: this.withAgentRows(options.setup) });
|
|
58
|
+
upstream.resume = (ownerCtx, options) =>
|
|
59
|
+
resume(ownerCtx, { ...options, setup: this.withAgentRows(options.setup) });
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 把「装行」接到调用方的 `setup` 之后。
|
|
63
|
+
* @param setup - 调用方的 setup(可能没有)。
|
|
64
|
+
* @returns 组合后的 setup(commit 原样交回上游)。
|
|
65
|
+
*/
|
|
66
|
+
private withAgentRows(setup: AgentSetup | undefined): AgentSetup {
|
|
67
|
+
return async (agentCtx, agent) => {
|
|
68
|
+
const commit = await setup?.(agentCtx, agent);
|
|
69
|
+
// 边界收窄一次:上游给的是 cordis ctx,我们的 `Context` 是它的子类型(只补了运行时本来就有的成员)。
|
|
70
|
+
await this.scopedRows.install(agentCtx as Context);
|
|
71
|
+
return commit;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// agent 的**会话事件补发**:把 `session/event` 定向派发到该会话所属 agent 的 scope 上。
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要:上游的「上次送过的注入没变就不重发」判据监听在 **agent scope** 上
|
|
4
|
+
// (`agent-loop/src/runtime-context.ts` 的 `RuntimeContextProjection`),而 `session/event` 由会话存储
|
|
5
|
+
// (本仓的 `provideSession()` 是根上的一行)发出。cordis 的事件**只向上流**:根上 emit 的事件到不了
|
|
6
|
+
// 子 scope 的监听器,那条判据于是永远不成立——每个请求都追加一整条同样的 `<system-reminder>`
|
|
7
|
+
// (`packages/agent/.agents/designs/20261119-会话事件补发到agent-scope.md`)。
|
|
8
|
+
//
|
|
9
|
+
// 为什么必须**定向**:`ctx.emit` 会触发该 ctx 及其祖先上的监听器,直接在 agent scope 上 emit 会让根上的
|
|
10
|
+
// 监听器(持久化、投影缓存)再收一次——那是双写。这里用上游 `scopeTarget` 的同一个过滤符号构造 carrier,
|
|
11
|
+
// 只放行「scope 正是这个 agent」的监听器。
|
|
12
|
+
//
|
|
13
|
+
// 前提(本仓的装配事实):会话存储是根上的一行。若哪天它变成 agent-scoped,这条补发会与上游自己的派发
|
|
14
|
+
// 重复——那时应当删掉本行(判据见上面那份债记录里的两条用例)。
|
|
15
|
+
import { inject, Injectable, Logger, type Context, type OnInit } from "@morlay/zephyr";
|
|
16
|
+
import { scopeOf, scopeTarget, type Agent } from "@morlay/dsh-agent/contract";
|
|
17
|
+
|
|
18
|
+
/** 上游 `events.dispatch` 解析出来的一条监听器(已绑定它的 `this`)。 */
|
|
19
|
+
type SessionListener = (session: object, event: unknown) => unknown;
|
|
20
|
+
|
|
21
|
+
/** agent scope 的 ctx:本仓只用到「按过滤符号解析监听器」与「scope 卸载时收尾」这两面。 */
|
|
22
|
+
interface ScopeCtx {
|
|
23
|
+
readonly events: { dispatch(type: string, args: readonly unknown[]): readonly SessionListener[] };
|
|
24
|
+
effect(callback: () => () => void): unknown;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 一个 agent 的派发目标:它的 scope key 与 `events` 面。 */
|
|
28
|
+
interface Target {
|
|
29
|
+
readonly events: ScopeCtx["events"];
|
|
30
|
+
readonly key: unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* cordis 的监听器过滤符号(`Symbol.for('cordis.filter')`)。
|
|
35
|
+
*
|
|
36
|
+
* 本仓没有对 cordis 的运行期依赖,所以**不 import 它**:从上游自己的 carrier 上取一次
|
|
37
|
+
* (`scopeTarget` 的产物恰好带这个符号)。取不到就整条不装——宁可保留上游行为,也不双触发。
|
|
38
|
+
*/
|
|
39
|
+
const FILTER = Object.getOwnPropertySymbols(scopeTarget({} as object, undefined))[0];
|
|
40
|
+
|
|
41
|
+
@Injectable()
|
|
42
|
+
export class AgentSessionEvents implements OnInit {
|
|
43
|
+
private readonly logger = inject.optional(Logger);
|
|
44
|
+
|
|
45
|
+
/** 会话 → 它的 agent 的派发目标(会话对象在 agent 卸载后可能还活着,所以按会话登记)。 */
|
|
46
|
+
private readonly targets = new WeakMap<object, Target>();
|
|
47
|
+
|
|
48
|
+
onInit(ctx: Context): void {
|
|
49
|
+
if (FILTER === undefined) {
|
|
50
|
+
this.logger?.warn(
|
|
51
|
+
"agent-loop: 取不到 cordis 的监听器过滤符号,会话事件不补发到 agent scope(注入会每步重发)",
|
|
52
|
+
);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
ctx.on("agent/created", (payload: { readonly agent: Agent }) => {
|
|
56
|
+
const agent = payload.agent;
|
|
57
|
+
// 边界桥:另一头是 cordis 的 Context(`events` / `effect` 不在本仓的 `Context` 面上)。
|
|
58
|
+
const scopeCtx = agent.ctx as unknown as ScopeCtx;
|
|
59
|
+
this.targets.set(agent.session, { events: scopeCtx.events, key: scopeOf(agent.ctx) });
|
|
60
|
+
// agent scope 卸载时摘掉这条映射(会话对象可能比它活得久)。
|
|
61
|
+
scopeCtx.effect(() => () => {
|
|
62
|
+
this.targets.delete(agent.session);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
// 事件的参数在本仓的 `Events` 面里没有收窄(上游把 `session` 声明成 `Scoped<Session>`),
|
|
66
|
+
// 这里按实际形状收一次:它是会话对象本身。
|
|
67
|
+
ctx.on("session/event", (session: unknown, event: unknown) => {
|
|
68
|
+
if (typeof session !== "object" || session === null) return;
|
|
69
|
+
const target = this.targets.get(session);
|
|
70
|
+
if (target === undefined) return;
|
|
71
|
+
const carrier = {
|
|
72
|
+
[FILTER]: (hookCtx: unknown) => scopeOf(hookCtx as never) === target.key,
|
|
73
|
+
};
|
|
74
|
+
// `dispatch` 只解析监听器,调用归调用方(与上游 `collectSessionCallbacks` 同一手法)。
|
|
75
|
+
for (const listener of target.events.dispatch("emit", [
|
|
76
|
+
carrier,
|
|
77
|
+
"session/event",
|
|
78
|
+
session,
|
|
79
|
+
event,
|
|
80
|
+
])) {
|
|
81
|
+
listener(session, event);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Llm } from "@morlay/dsh-agent";
|
|
2
|
+
import { SessionStore } from "@morlay/dsh-session";
|
|
3
|
+
import { SessionPersistence } from "@morlay/dsh-session-persistence";
|
|
4
|
+
import { ToolRegistry } from "@morlay/dsh-agent";
|
|
5
|
+
import * as SessionCheckpointPolicyPlugin from "@deepseek-ai/dsh-session-checkpoint-policy";
|
|
6
|
+
import { Injectable, inject, type Context } from "@morlay/zephyr";
|
|
7
|
+
import { fromCordis } from "@morlay/zephyr/compat";
|
|
8
|
+
|
|
9
|
+
@Injectable()
|
|
10
|
+
export class SessionCheckpointPolicy extends fromCordis(SessionCheckpointPolicyPlugin) {
|
|
11
|
+
llm = inject(Llm);
|
|
12
|
+
|
|
13
|
+
sessionPersistence = inject(SessionPersistence);
|
|
14
|
+
|
|
15
|
+
sessions = inject(SessionStore);
|
|
16
|
+
|
|
17
|
+
tools = inject(ToolRegistry);
|
|
18
|
+
|
|
19
|
+
override async onInit(ctx: Context): Promise<void> {
|
|
20
|
+
await this.mount(ctx, undefined, {
|
|
21
|
+
llm: this.llm,
|
|
22
|
+
sessionPersistence: this.sessionPersistence,
|
|
23
|
+
sessions: this.sessions,
|
|
24
|
+
tools: this.tools,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// agent 循环契约:令牌按服务语义命名(上游注册名 `agentLoop`)。
|
|
2
|
+
import type AgentLoopService from "@deepseek-ai/dsh-agent-loop";
|
|
3
|
+
import { Injectable } from "@morlay/zephyr";
|
|
4
|
+
import type { AssertCoveredStrict, AssertTrue } from "@morlay/zephyr-app/client";
|
|
5
|
+
|
|
6
|
+
@Injectable({ name: "agentLoop" })
|
|
7
|
+
export abstract class AgentLoop {
|
|
8
|
+
abstract readonly config: AgentLoopService["config"];
|
|
9
|
+
|
|
10
|
+
abstract readonly create: AgentLoopService["create"];
|
|
11
|
+
|
|
12
|
+
abstract readonly createAgent: AgentLoopService["createAgent"];
|
|
13
|
+
|
|
14
|
+
abstract readonly resume: AgentLoopService["resume"];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 覆盖守卫(严格):上游 `AgentLoopService` 的每个成员都要有同名、同签名的对应项。
|
|
19
|
+
*
|
|
20
|
+
* 为什么不是 `implements`:上游那份是 cordis `Service` **类**(带私有成员),`implements` 会报
|
|
21
|
+
* TS2720("Did you mean to extend … and inherit its members as a subclass?")。签名守卫在这里等价:
|
|
22
|
+
* 漏成员或签名对不上,编译期都会报出成员名(诊断的第二行)。
|
|
23
|
+
*/
|
|
24
|
+
export type AgentLoopCovered = AssertTrue<AssertCoveredStrict<AgentLoopService, AgentLoop>>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// 同进程 fork provider(纯实现行,不声明契约)。
|
|
2
|
+
import * as SubagentForkInProcessPlugin from "@deepseek-ai/dsh-subagent-fork-in-process";
|
|
3
|
+
import { Injectable, inject, type Context } from "@morlay/zephyr";
|
|
4
|
+
import { fromCordis } from "@morlay/zephyr/compat";
|
|
5
|
+
import { SubagentRegistry } from "@morlay/dsh-agent/contract";
|
|
6
|
+
|
|
7
|
+
@Injectable()
|
|
8
|
+
export class SubagentForkInProcess extends fromCordis(SubagentForkInProcessPlugin) {
|
|
9
|
+
subagents = inject(SubagentRegistry);
|
|
10
|
+
|
|
11
|
+
override async onInit(ctx: Context): Promise<void> {
|
|
12
|
+
await this.mount(ctx, undefined, { subagents: this.subagents });
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// agent 循环期的包根:本域的实现类(契约在 `@morlay/dsh-agent`)。
|
|
2
|
+
// **headless 装配面**(root):循环期的一整份清单。带 wire 的装配在 `./server`。
|
|
3
|
+
export { provideAgentLoop } from "./agent-loop.provide.ts";
|
|
4
|
+
export { provideSubagent } from "./subagent.provide.ts";
|
|
5
|
+
|
|
6
|
+
export * from "./agent-loop.service.ts";
|
|
7
|
+
export * from "./agent-session-events.service.ts";
|
|
8
|
+
export * from "./tool-executor.service.ts";
|
|
9
|
+
export * from "./checkpoint-policy.service.ts";
|
|
10
|
+
export * from "./spill-local.service.ts";
|
|
11
|
+
export * from "./spill-policy.service.ts";
|
|
12
|
+
export * from "./repeat-tool-reminder.service.ts";
|
|
13
|
+
export * from "./timeout-policy.service.ts";
|
|
14
|
+
export * from "./subagent.tool.ts";
|
|
15
|
+
export * from "./subagent-fork.tool.ts";
|
|
16
|
+
export * from "./subagent-control.tool.ts";
|
|
17
|
+
export * from "./fork-in-process.service.ts";
|
|
18
|
+
export * from "./spawn-in-process.service.ts";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// RepeatToolReminder(工具/策略实现行)。
|
|
2
|
+
import { Injectable, type Context } from "@morlay/zephyr";
|
|
3
|
+
import { fromCordis } from "@morlay/zephyr/compat";
|
|
4
|
+
import * as RepeatToolReminderPlugin from "@deepseek-ai/dsh-repeat-tool-reminder";
|
|
5
|
+
|
|
6
|
+
@Injectable()
|
|
7
|
+
export class RepeatToolReminder extends fromCordis(RepeatToolReminderPlugin) {
|
|
8
|
+
override async onInit(ctx: Context): Promise<void> {
|
|
9
|
+
await this.mount(ctx);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { configure, provide } from "@morlay/zephyr";
|
|
2
|
+
import { AgentLoopDefault } from "../agent-loop.service.ts";
|
|
3
|
+
import { provideSessionCheckpointPolicy } from "./checkpoint-policy.provide.ts";
|
|
4
|
+
import { provideToolOutput } from "./tool-output.provide.ts";
|
|
5
|
+
import { provideToolPolicy } from "./tool-policy.provide.ts";
|
|
6
|
+
import { provideSubagentEndpoints } from "./subagent.provide.ts";
|
|
7
|
+
import { ToolExecutorNative } from "../tool-executor.service.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* agent 的**循环期装配面**:一轮里才发生的事——循环本体、执行器、检查点、输出与调用策略、子代理派生。
|
|
11
|
+
*
|
|
12
|
+
* 与 `provideAgent()` 分开(最小装配原则)。执行器只有一个(上游 `setExecutor` 在根作用域互斥):
|
|
13
|
+
* 本部署选 PTC 时用 `provideAgentLoop(without(ToolExecutorNative))` 剔掉这一条。
|
|
14
|
+
* @returns `(...features) => 条目清单`。
|
|
15
|
+
*/
|
|
16
|
+
export const provideAgentLoop = provide(
|
|
17
|
+
[
|
|
18
|
+
ToolExecutorNative,
|
|
19
|
+
...provideSessionCheckpointPolicy(),
|
|
20
|
+
...provideToolOutput(),
|
|
21
|
+
...provideToolPolicy(),
|
|
22
|
+
...provideSubagentEndpoints(),
|
|
23
|
+
],
|
|
24
|
+
// 默认配置走 `configure`(保证类型推导),不写成清单里的 `{ provide, config }`。
|
|
25
|
+
[configure(AgentLoopDefault, { agents: [] })],
|
|
26
|
+
);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { provide } from "@morlay/zephyr";
|
|
2
|
+
import { SubagentsEndpointDefault } from "./subagents-endpoint.ts";
|
|
3
|
+
import { provideSubagent } from "../subagent.provide.ts";
|
|
4
|
+
|
|
5
|
+
/** **wire 子代理**:headless 那几行 + `remote.subagents` 的两条端点。 */
|
|
6
|
+
export const provideSubagentEndpoints = provide([...provideSubagent(), SubagentsEndpointDefault]);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// `subagents` 这一组的**宿主实现**:继承 `endpoint/` 的契约类,方法体里转发到上游 `SubagentRuntime`
|
|
2
|
+
// (本包的 `SubagentRegistryDefault` 已经把那个实例挂在 `SubagentRegistry` 上,这里不重复 mount)。
|
|
3
|
+
//
|
|
4
|
+
// 组名从契约类继承(`@RemoteGroup()` 不写名字,`remoteGroupNameOf` 沿原型链取),
|
|
5
|
+
// 端点名只有一个 home:`endpoint/subagents-endpoint.ts`。
|
|
6
|
+
import { SessionId } from "@morlay/dsh-session/endpoint";
|
|
7
|
+
import { inject, Injectable, type OnInit } from "@morlay/zephyr";
|
|
8
|
+
import { RemoteHandlers } from "@morlay/zephyr-app";
|
|
9
|
+
import { RemoteGroup, registerRemoteGroup } from "@morlay/zephyr-app/remote";
|
|
10
|
+
import { SubagentRegistry } from "@morlay/dsh-agent";
|
|
11
|
+
import {
|
|
12
|
+
SubagentsEndpoint,
|
|
13
|
+
type SubagentInterruptByUserValue,
|
|
14
|
+
type SubagentPromptRequest,
|
|
15
|
+
type SubagentPromptValue,
|
|
16
|
+
} from "@morlay/dsh-agent/endpoint";
|
|
17
|
+
import type {
|
|
18
|
+
SubagentPromptRequest as UpstreamPromptRequest,
|
|
19
|
+
SubagentPromptRequestId,
|
|
20
|
+
} from "@morlay/dsh-agent/contract";
|
|
21
|
+
|
|
22
|
+
/** `interruptByUser` 的 wire 形参类型(上游 `interruptByUser` 的第一个形参)。 */
|
|
23
|
+
type SubagentInterruptByUserRequestId = Parameters<SubagentRegistry["interruptByUser"]>[0];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* wire 上的裸字符串 → 上游的请求身份。
|
|
27
|
+
* @param value - wire 上的 `requestId`(调用方铸好的消息身份)。
|
|
28
|
+
* @returns 带 `session-request-id` 品牌的同一字符串。
|
|
29
|
+
*/
|
|
30
|
+
function asPromptRequestId(value: string): SubagentPromptRequestId {
|
|
31
|
+
return value as SubagentPromptRequestId;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `subagents` 组的宿主实现。 */
|
|
35
|
+
@RemoteGroup()
|
|
36
|
+
@Injectable()
|
|
37
|
+
export class SubagentsEndpointDefault extends SubagentsEndpoint implements OnInit {
|
|
38
|
+
private readonly handlers = inject(RemoteHandlers);
|
|
39
|
+
|
|
40
|
+
private readonly subagents = inject(SubagentRegistry);
|
|
41
|
+
|
|
42
|
+
onInit(): void {
|
|
43
|
+
registerRemoteGroup(this.handlers, this);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 回**裸值**:`RemoteResult` 由桥与客户端面各包一层,宿主再包就是双包(消费方读 `value` 会拿到 `{ok, value}`)。
|
|
47
|
+
override async prompt(request: SubagentPromptRequest): Promise<SubagentPromptValue> {
|
|
48
|
+
// wire 上是裸字符串,上游要品牌身份:在边界逐字段铸一次(会话身份与请求身份都在这里转换)。
|
|
49
|
+
// 逐字段展开而不是 `...request`:`exactOptionalPropertyTypes` 下,wire 上"可能有 undefined"
|
|
50
|
+
// 的可选字段不能直接摊进上游的 `?` 字段。
|
|
51
|
+
const upstream: UpstreamPromptRequest = {
|
|
52
|
+
requestId: asPromptRequestId(request.requestId),
|
|
53
|
+
childSessionId: SessionId(request.childSessionId),
|
|
54
|
+
delivery: request.delivery,
|
|
55
|
+
content: request.content,
|
|
56
|
+
...(request.clientTimeZone === undefined ? {} : { clientTimeZone: request.clientTimeZone }),
|
|
57
|
+
};
|
|
58
|
+
// unary 的 wire 不带信号:上游 `prompt(request, signal)` 要一个(它随激活一起取消)。
|
|
59
|
+
return await this.subagents.prompt(upstream, new AbortController().signal);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
override async interruptByUser(
|
|
63
|
+
childSessionId: SubagentInterruptByUserRequestId,
|
|
64
|
+
): Promise<SubagentInterruptByUserValue> {
|
|
65
|
+
// 上游只收一个品牌化的子会话身份;权限判定(user 侧)在它内部做。
|
|
66
|
+
return this.subagents.interruptByUser(SessionId(childSessionId));
|
|
67
|
+
}
|
|
68
|
+
}
|