@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
@@ -0,0 +1,8 @@
1
+ import { provide } from "@morlay/zephyr";
2
+ import { SpillLocal } from "../spill-local.service.ts";
3
+ import { SpillPolicy } from "../spill-policy.service.ts";
4
+
5
+ // 工具输出这一组:落盘(spill)、落盘策略、结果裁剪。三行都挂在工具调用生命周期上,
6
+ // 不是工具本身——契约面是 `ToolRegistry` 与上游的 `spillStore` / `tokenMeter`。
7
+ // 工具结果裁剪属 `dsh-compaction`:这里只剩「溢出落盘」两条。
8
+ export const provideToolOutput = provide([SpillLocal, SpillPolicy]);
@@ -0,0 +1,17 @@
1
+ import { provide } from "@morlay/zephyr";
2
+ import { McpResources } from "@morlay/dsh-agent";
3
+ import { RepeatToolReminder } from "../repeat-tool-reminder.service.ts";
4
+ import { TimeoutPolicy } from "../timeout-policy.service.ts";
5
+
6
+ // 工具**调用生命周期**上的三条行:重复提醒、调用超时、MCP 资源。
7
+ //
8
+ // 它们都不是工具本身(工具定义在上游插件里),所以没有自己的注册表,也没有自己的契约。
9
+ //
10
+ // 装配位置**这一票不动**(都还在应用根):它们内部用 `agent.ctx` / `scopeOf(ctx)` 把状态按 agent
11
+ // 分开。按 scope 装配(`@AgentScoped()`)要连着它们的消费方一起量,见
12
+ // `packages/agent/.agents/designs/20261118-per-agent-插件的装配位置.md` 的「迁移面」。
13
+ export const provideToolPolicy = provide([
14
+ { provide: RepeatToolReminder, config: { thresholds: [3, 5, 8], argumentsPreviewChars: 500 } },
15
+ TimeoutPolicy,
16
+ McpResources,
17
+ ]);
@@ -0,0 +1,14 @@
1
+ // 同进程 spawn provider(纯实现行,不声明契约)。
2
+ import * as SubagentSpawnInProcessPlugin from "@deepseek-ai/dsh-subagent-spawn-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 SubagentSpawnInProcess extends fromCordis(SubagentSpawnInProcessPlugin) {
9
+ subagents = inject(SubagentRegistry);
10
+
11
+ override async onInit(ctx: Context): Promise<void> {
12
+ await this.mount(ctx, undefined, { subagents: this.subagents });
13
+ }
14
+ }
@@ -0,0 +1,11 @@
1
+ // SpillLocal(工具/策略实现行)。
2
+ import { Injectable, type Context } from "@morlay/zephyr";
3
+ import { fromCordis } from "@morlay/zephyr/compat";
4
+ import LocalSpillStore from "@deepseek-ai/dsh-spill-local";
5
+
6
+ @Injectable()
7
+ export class SpillLocal extends fromCordis(LocalSpillStore) {
8
+ override async onInit(ctx: Context): Promise<void> {
9
+ await this.mount(ctx);
10
+ }
11
+ }
@@ -0,0 +1,14 @@
1
+ // SpillPolicy(工具/策略实现行)。
2
+ import { Injectable, inject, type Context } from "@morlay/zephyr";
3
+ import { fromCordis } from "@morlay/zephyr/compat";
4
+ import * as SpillPolicyPlugin from "@deepseek-ai/dsh-spill-policy";
5
+ import { ToolRegistry } from "@morlay/dsh-agent";
6
+
7
+ @Injectable()
8
+ export class SpillPolicy extends fromCordis(SpillPolicyPlugin) {
9
+ tools = inject(ToolRegistry);
10
+
11
+ override async onInit(ctx: Context): Promise<void> {
12
+ await this.mount(ctx, undefined, { tools: this.tools });
13
+ }
14
+ }
@@ -0,0 +1,23 @@
1
+ // subagent 控制工具(纯实现行,不声明契约)。
2
+ import * as ToolSubagentControlPlugin from "@deepseek-ai/dsh-tool-subagent-control";
3
+ import { Injectable, inject, type Context } from "@morlay/zephyr";
4
+ import { fromCordis } from "@morlay/zephyr/compat";
5
+ import { AgentDirectory, ToolRegistry } from "@morlay/dsh-agent";
6
+ import { SubagentRegistry } from "@morlay/dsh-agent/contract";
7
+
8
+ @Injectable()
9
+ export class ToolSubagentControl extends fromCordis(ToolSubagentControlPlugin) {
10
+ tools = inject(ToolRegistry);
11
+
12
+ subagents = inject(SubagentRegistry);
13
+
14
+ agents = inject(AgentDirectory);
15
+
16
+ override async onInit(ctx: Context): Promise<void> {
17
+ await this.mount(ctx, undefined, {
18
+ tools: this.tools,
19
+ subagents: this.subagents,
20
+ agents: this.agents,
21
+ });
22
+ }
23
+ }
@@ -0,0 +1,30 @@
1
+ // 子代理 `fork` 工具行:与 `subagent.tool.ts` 同一个上游插件、同一组注入。
2
+ //
3
+ // **不共用基类**:装配合并按 `providerBase` 去重(`fromCordis` 的子类指回被配置的类),
4
+ // 共用基类会让两条行活下只剩一条。两条行各自是一个 `@Injectable()` 类,判别靠类身份。
5
+ import * as ToolSubagentPlugin from "@deepseek-ai/dsh-tool-subagent";
6
+ import { Injectable, inject, type Context } from "@morlay/zephyr";
7
+ import { fromCordis } from "@morlay/zephyr/compat";
8
+ import { SystemPrompt, ToolRegistry } from "@morlay/dsh-agent";
9
+ import { SessionProjectionStore } from "@morlay/dsh-session";
10
+ import { SubagentRegistry } from "@morlay/dsh-agent/contract";
11
+
12
+ @Injectable()
13
+ export class ToolSubagentFork extends fromCordis(ToolSubagentPlugin) {
14
+ tools = inject(ToolRegistry);
15
+
16
+ subagents = inject(SubagentRegistry);
17
+
18
+ systemPrompt = inject(SystemPrompt);
19
+
20
+ sessionProjections = inject(SessionProjectionStore);
21
+
22
+ override async onInit(ctx: Context): Promise<void> {
23
+ await this.mount(ctx, undefined, {
24
+ tools: this.tools,
25
+ subagents: this.subagents,
26
+ systemPrompt: this.systemPrompt,
27
+ sessionProjections: this.sessionProjections,
28
+ });
29
+ }
30
+ }
@@ -0,0 +1,21 @@
1
+ import { provide } from "@morlay/zephyr";
2
+ import { SubagentRegistryDefault } from "@morlay/dsh-agent";
3
+ import { SubagentSpawnInProcess } from "./spawn-in-process.service.ts";
4
+ import { SubagentForkInProcess } from "./fork-in-process.service.ts";
5
+ import { ToolSubagentControl } from "./subagent-control.tool.ts";
6
+ import { ToolSubagent } from "./subagent.tool.ts";
7
+ import { ToolSubagentFork } from "./subagent-fork.tool.ts";
8
+
9
+ /**
10
+ * **headless 子代理**:注册表 + 两种运行方式 + 三条工具行。
11
+ *
12
+ * 不含 `remote.subagents` 的 wire 端点——那是 `provideSubagentEndpoints()`。
13
+ */
14
+ export const provideSubagent = provide([
15
+ SubagentRegistryDefault,
16
+ { provide: SubagentSpawnInProcess, config: { providerName: "spawn" } },
17
+ { provide: SubagentForkInProcess, config: { providerName: "fork" } },
18
+ ToolSubagentControl,
19
+ { provide: ToolSubagent, config: { provider: "spawn", toolName: "subagent" } },
20
+ { provide: ToolSubagentFork, config: { provider: "fork", toolName: "subagent_fork" } },
21
+ ]);
@@ -0,0 +1,33 @@
1
+ // 子代理工具行(`subagent`):把上游 `tool-subagent` 要的依赖交给它。
2
+ //
3
+ // 和 `subagent-fork.tool.ts` 共用同一个上游插件、同一组注入、同一次 `mount`——但**不能**合成一个类
4
+ // 再 `withConfig` 两次:装配合并按 `providerBase` 去重,`withConfig` 生成的子类指回被配置的类,
5
+ // 同一个类配两次只会活下最后一条。所以两条行各有一个自己的类身份(去重键就是它)。
6
+ //
7
+ // 判别两条行的是**类身份**、不是名字:两者的服务名都取上游插件名(`tool-subagent`,适配类的既有口径)。
8
+ import * as ToolSubagentPlugin from "@deepseek-ai/dsh-tool-subagent";
9
+ import { Injectable, inject, type Context } from "@morlay/zephyr";
10
+ import { fromCordis } from "@morlay/zephyr/compat";
11
+ import { SystemPrompt, ToolRegistry } from "@morlay/dsh-agent";
12
+ import { SessionProjectionStore } from "@morlay/dsh-session";
13
+ import { SubagentRegistry } from "@morlay/dsh-agent/contract";
14
+
15
+ @Injectable()
16
+ export class ToolSubagent extends fromCordis(ToolSubagentPlugin) {
17
+ tools = inject(ToolRegistry);
18
+
19
+ subagents = inject(SubagentRegistry);
20
+
21
+ systemPrompt = inject(SystemPrompt);
22
+
23
+ sessionProjections = inject(SessionProjectionStore);
24
+
25
+ override async onInit(ctx: Context): Promise<void> {
26
+ await this.mount(ctx, undefined, {
27
+ tools: this.tools,
28
+ subagents: this.subagents,
29
+ systemPrompt: this.systemPrompt,
30
+ sessionProjections: this.sessionProjections,
31
+ });
32
+ }
33
+ }
@@ -0,0 +1,61 @@
1
+ // 「跑一轮」的测试装配:**本仓实现**(不借上游 testkit)。
2
+ //
3
+ // 只导出 `loopTestProviders()`(纯清单函数):装配统一走 `bootstrapApplication`——本仓的正规测试入口。
4
+ // 原来还导出 `mountLoopTestDependencies()` / `mountLoopTestHarness()`(「挂到调用方 ctx 上」的第二套内核),
5
+ // 全仓零调用且与 `bootstrapApplication` 重复,已随 `mount.ts` 一起删。
6
+ import { configure, provide, type ProviderList } from "@morlay/zephyr";
7
+ import { SystemPromptDefault, provideAgent } from "@morlay/dsh-agent";
8
+ import { provideSession, provideSessionProjection } from "@morlay/dsh-session/server";
9
+ import { AgentLoopDefault, AgentSessionEvents, ToolExecutorNative } from "../index.ts";
10
+
11
+ /** 上游 `Agent` 的类型面(vendor 的模块增强,本仓 program 看不到整份形状)。 */
12
+ export type Agent = import("@morlay/dsh-agent/contract").Agent;
13
+
14
+ /** 循环本体那一份(默认 `agents: []`)。 */
15
+ // 与真装配同一份「补发会话事件」行:少了它,测试里的注入会每步重发(上游的判据靠它)。
16
+ const loopRow = provide(
17
+ [AgentLoopDefault, AgentSessionEvents],
18
+ [configure(AgentLoopDefault, { agents: [] })],
19
+ );
20
+
21
+ /** 装配选项(与上游 testkit 的三个选项同义)。 */
22
+ export interface LoopTestOptions {
23
+ /** 装上工作目录(要读目录的用例才需要)。 */
24
+ readonly workingDirectory?: boolean;
25
+ /** 提示词配置(透传给 `SystemPromptDefault`,形状由上游 `Config` 决定)。 */
26
+ readonly systemPrompt?: Record<string, unknown>;
27
+ /** 工具面:`native` 或 `ptc`(默认 native)。 */
28
+ readonly tools?: { readonly mode?: "native" | "ptc" };
29
+ }
30
+
31
+ /**
32
+ * 跑一轮对话所需的装配清单(本仓的装配方式)。
33
+ *
34
+ * 装「骨架(含提示词与默认模型)+ 会话 + 投影 + 执行器」;`headless` 时到此为止,不装循环本体。
35
+ * @param options - 见 `LoopTestOptions` 与 `headless`。
36
+ * @returns 条目清单。
37
+ */
38
+ export function loopTestProviders(
39
+ options: LoopTestOptions & { readonly headless?: boolean } = {},
40
+ ): ProviderList {
41
+ const systemPrompt = options.systemPrompt;
42
+ const personaLed =
43
+ systemPrompt === undefined
44
+ ? [...provideAgent()]
45
+ : [...provideAgent(configure(SystemPromptDefault, systemPrompt as never))];
46
+ return [
47
+ ...provideSession(),
48
+ ...provideSessionProjection(),
49
+ ...personaLed,
50
+ ToolExecutorNative,
51
+ ...(options.headless === true ? [] : loopRow()),
52
+ ];
53
+ }
54
+
55
+ /** 测试驱动面:建真 agent 并驱动它的收件箱(与上游 harness 同形)。 */
56
+ export interface LoopHarness {
57
+ /** 建一个真 agent(与它的会话)。 */
58
+ create(id: unknown, options?: unknown, meta?: unknown): Promise<Agent>;
59
+ /** 这一轮的收件箱:投递一条用户消息并取回要处理的那几条。 */
60
+ claim(agent: Agent, target: unknown, turn: number): readonly unknown[];
61
+ }
@@ -0,0 +1,11 @@
1
+ // 循环期的**测试支撑**(`@morlay/dsh-agent-loop/testing`):本仓的装配清单、挂载入口与装配原语。
2
+ //
3
+ // **不再转出上游 testkit**:它按 cordis 插件逐条 `plugin()`,与本仓的 `ProviderList` 不是一回事。
4
+ export { headlessLoopProviders } from "./providers.ts";
5
+ export {
6
+ loopTestProviders,
7
+ type Agent,
8
+ type LoopHarness,
9
+ type LoopTestOptions,
10
+ } from "./agent-loop-harness.ts";
11
+ export { PresetGroup, PresetLoader } from "./preset-plane.ts";
@@ -0,0 +1,6 @@
1
+ // 「preset 平面」的装配原语:上游 cordis 的 `Loader` 与 `Group` 插件。
2
+ //
3
+ // 与本目录其它转出(`newTestContext`)同一条理由:扩展包的测试要装真 Loader 来验证「行清单归哪份
4
+ // preset」,但不该为此直接依赖 `@deepseek-ai/cordis-plugin-*`。
5
+ export { default as PresetGroup } from "@deepseek-ai/cordis-plugin-group";
6
+ export { default as PresetLoader } from "@deepseek-ai/cordis-plugin-loader";
@@ -0,0 +1,27 @@
1
+ import { configure, type ProviderList } from "@morlay/zephyr";
2
+ import { SandboxPolicyDefault, provideSandbox } from "@morlay/dsh-sandbox";
3
+ import { provideShell } from "@morlay/dsh-shell/server";
4
+ import { provideWorkingDirectory } from "@morlay/dsh-agent";
5
+ import { provideAgentLoop } from "../agent-loop.provide.ts";
6
+
7
+ /**
8
+ * 循环期 headless 层的最小清单:循环本体、执行器、检查点、输出与调用策略、子代理派生,
9
+ * 以及它们要的下游(shell → sandbox → 工作目录)。
10
+ *
11
+ * 不含 `AppEnv`(部署事实由调用方给)、不含 wire。要"跑一轮对话"的测试装这份清单就够。
12
+ * @param options.workspaceRoot - 沙箱的可写根(测试一般给临时目录)。
13
+ * @returns 条目清单。
14
+ */
15
+ export function headlessLoopProviders(options: { readonly workspaceRoot: string }): ProviderList {
16
+ return [
17
+ ...provideShell(),
18
+ ...provideSandbox(
19
+ configure(SandboxPolicyDefault, {
20
+ mode: "workspace-write",
21
+ workspaceRoot: options.workspaceRoot,
22
+ }),
23
+ ),
24
+ ...provideWorkingDirectory(),
25
+ ...provideAgentLoop(),
26
+ ];
27
+ }
@@ -0,0 +1,14 @@
1
+ // TimeoutPolicy(工具/策略实现行)。
2
+ import { Injectable, inject, type Context } from "@morlay/zephyr";
3
+ import { fromCordis } from "@morlay/zephyr/compat";
4
+ import * as TimeoutPolicyPlugin from "@deepseek-ai/dsh-tool-call-timeout-policy";
5
+ import { ToolRegistry } from "@morlay/dsh-agent";
6
+
7
+ @Injectable()
8
+ export class TimeoutPolicy extends fromCordis(TimeoutPolicyPlugin) {
9
+ tools = inject(ToolRegistry);
10
+
11
+ override async onInit(ctx: Context): Promise<void> {
12
+ await this.mount(ctx, undefined, { tools: this.tools });
13
+ }
14
+ }
@@ -0,0 +1,28 @@
1
+ // 原生工具注册表(适配行,不声明对外服务):上游把「谁提供模型面的工具入口」拆成了独立插件。
2
+ //
3
+ // 上游导出的是「一个类 + 一个 `apply`」(alpha.3 起从「执行器」改成「根注册表」):
4
+ //
5
+ // export class NativeToolRegistry implements ToolRegistry { register/include/get/getAll }
6
+ // export function apply(ctx) { ctx.tools.setRoot(new NativeToolRegistry()) }
7
+ //
8
+ // **为什么仍然需要 `fromCordis`**:`apply` 按 **cordis ctx 语义**写(`ctx.tools.setRoot(...)` 读的是服务实例
9
+ // 自己的 ctx,作用域也从那里取)。而我们 Provider 的 `onInit(ctx)` 拿到的是 cordis 行 ctx,`tools` 是我们的
10
+ // 令牌、不在 cordis 名字表上,直接跑 `apply` 会抛 `cannot get property "tools" without inject`。
11
+ // `fromCordis` 的 `mount(ctx, undefined, { tools })` 正是把 deps 放进这一行的作用域,让上游按它期望的方式
12
+ // 读到 `tools`——这一层不是多余的包装,是**语义适配**。
13
+ //
14
+ // 为什么这一行必须存在:agent loop 在 turn 开始时要求作用域里已经选定根注册表,否则整轮直接以
15
+ // `TOOL_REGISTRY_UNAVAILABLE`("The selected tool registry is unavailable…")结束(假 LLM 一次都不会被调用)。
16
+ import * as ToolExecutorNativePlugin from "@deepseek-ai/dsh-tool-registry-native";
17
+ import { inject, Injectable, type Context } from "@morlay/zephyr";
18
+ import { fromCordis } from "@morlay/zephyr/compat";
19
+ import { ToolRegistry } from "@morlay/dsh-agent";
20
+
21
+ @Injectable()
22
+ export class ToolExecutorNative extends fromCordis(ToolExecutorNativePlugin) {
23
+ tools = inject(ToolRegistry);
24
+
25
+ override async onInit(ctx: Context): Promise<void> {
26
+ await this.mount(ctx, undefined, { tools: this.tools });
27
+ }
28
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.