@morlay/session-mode 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 (85) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +85 -2
  3. package/dist/endpoint.d.mts +2 -0
  4. package/dist/endpoint.mjs +3 -0
  5. package/dist/index.d.mts +691 -0
  6. package/dist/index.mjs +2350 -0
  7. package/dist/mode-config-BIYT66jI.mjs +22 -0
  8. package/dist/mode-config-rules-z4gdHLR1.mjs +52 -0
  9. package/dist/rolldown-runtime-3qf6kE1h.d.mts +1 -0
  10. package/dist/server.d.mts +2 -0
  11. package/dist/server.mjs +1 -0
  12. package/dist/session-mode-endpoint-CLpvSnzM.d.mts +120 -0
  13. package/dist/session-mode-endpoint-Dq-E_kHt.mjs +53 -0
  14. package/dist/session-mode-endpoint.service-Bn4EH6aB.d.mts +21 -0
  15. package/dist/session-mode-endpoint.service-MrqQipN6.mjs +79 -0
  16. package/dist/testing.d.mts +12 -0
  17. package/dist/testing.mjs +23 -0
  18. package/dist/ui.d.mts +125 -0
  19. package/dist/ui.mjs +1165 -0
  20. package/package.json +63 -3
  21. package/src/context-assembler.provide.ts +139 -0
  22. package/src/context-assembler.service.ts +67 -0
  23. package/src/contract/context-assembler.contract.ts +30 -0
  24. package/src/contract/index.ts +3 -0
  25. package/src/contract/session-mode.contract.ts +104 -0
  26. package/src/contract/session-scope.contract.ts +53 -0
  27. package/src/defaults.ts +39 -0
  28. package/src/endpoint/index.ts +6 -0
  29. package/src/endpoint/mode-config-rules.ts +59 -0
  30. package/src/endpoint/mode-config.ts +58 -0
  31. package/src/endpoint/session-mode-endpoint.ts +121 -0
  32. package/src/endpoint/session-mode-shapes.ts +71 -0
  33. package/src/endpoint/tool-group-ref.ts +11 -0
  34. package/src/events.ts +102 -0
  35. package/src/guidance/groups.ts +101 -0
  36. package/src/guidance/index.ts +4 -0
  37. package/src/guidance/packs/ask.ts +17 -0
  38. package/src/guidance/packs/code.ts +25 -0
  39. package/src/guidance/packs/fs.ts +60 -0
  40. package/src/guidance/packs/index.ts +52 -0
  41. package/src/guidance/packs/mcp.ts +25 -0
  42. package/src/guidance/packs/plan.ts +42 -0
  43. package/src/guidance/packs/shell.ts +45 -0
  44. package/src/guidance/packs/skill.ts +12 -0
  45. package/src/guidance/packs/swarm.ts +60 -0
  46. package/src/guidance/packs/web-access.ts +24 -0
  47. package/src/guidance/tool-groups.ts +72 -0
  48. package/src/guidance/types.ts +29 -0
  49. package/src/index.ts +63 -0
  50. package/src/mode-sources.ts +83 -0
  51. package/src/mode-store.ts +45 -0
  52. package/src/modes.ts +126 -0
  53. package/src/per-agent-rows.ts +62 -0
  54. package/src/per-agent.ts +36 -0
  55. package/src/persona.ts +30 -0
  56. package/src/ptc-describe.ts +136 -0
  57. package/src/ptc-sdk.ts +101 -0
  58. package/src/reminder.ts +89 -0
  59. package/src/rules.ts +8 -0
  60. package/src/server/index.ts +7 -0
  61. package/src/server/session-mode-endpoint.service.ts +117 -0
  62. package/src/session-mode-store.ts +218 -0
  63. package/src/session-mode.projection.ts +78 -0
  64. package/src/session-mode.provide.ts +38 -0
  65. package/src/session-mode.service.ts +670 -0
  66. package/src/session-scope.service.ts +515 -0
  67. package/src/subagent/delegation-context.ts +30 -0
  68. package/src/subagent/return-guidance.ts +56 -0
  69. package/src/subagent/subagent-guidance.service.ts +76 -0
  70. package/src/subagent.provide.ts +5 -0
  71. package/src/system-remainder.ts +43 -0
  72. package/src/testing/index.ts +34 -0
  73. package/src/tool-guidance.service.ts +265 -0
  74. package/src/ui/index.ts +12 -0
  75. package/src/ui/locales.ts +134 -0
  76. package/src/ui/session-mode-endpoint.client.ts +19 -0
  77. package/src/ui/session-mode-locales.contribution.ts +51 -0
  78. package/src/ui/session-mode-panel-tab.ts +39 -0
  79. package/src/ui/session-mode-panel.view.tsx +931 -0
  80. package/src/ui/session-mode-seat.view.tsx +174 -0
  81. package/src/ui/session-mode-slots.ts +13 -0
  82. package/src/ui/session-mode.provide.ts +24 -0
  83. package/src/ui/session-mode.store.ts +90 -0
  84. package/src/ui/use-roster.ts +24 -0
  85. package/src/ui/view-props.ts +19 -0
package/package.json CHANGED
@@ -1,6 +1,66 @@
1
1
  {
2
2
  "name": "@morlay/session-mode",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0-alpha.0",
4
+ "private": false,
5
+ "description": "会话模式、工具说明分组与提示词通道:模式是一份数据、按会话收口;PTC 由运行时事实(工具目录里有 run_code)触发。",
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
+ "./testing": {
18
+ "types": "./dist/testing.d.mts",
19
+ "default": "./dist/testing.mjs"
20
+ },
21
+ "./endpoint": {
22
+ "types": "./dist/endpoint.d.mts",
23
+ "default": "./dist/endpoint.mjs"
24
+ },
25
+ "./server": {
26
+ "types": "./dist/server.d.mts",
27
+ "default": "./dist/server.mjs"
28
+ },
29
+ "./ui": {
30
+ "types": "./dist/ui.d.mts",
31
+ "default": "./dist/ui.mjs"
32
+ },
33
+ "./package.json": "./package.json"
34
+ },
35
+ "dependencies": {
36
+ "@morlay/dsh-agent": "^0.2.0-alpha.0",
37
+ "@morlay/dsh-fs": "^0.2.0-alpha.0",
38
+ "@morlay/dsh-ptc": "^0.2.0-alpha.0",
39
+ "@morlay/dsh-session": "^0.2.0-alpha.0",
40
+ "@morlay/dsh-skill": "^0.2.0-alpha.0",
41
+ "@morlay/dsh-storage": "^0.2.0-alpha.0",
42
+ "@morlay/zephyr": "^0.2.0-alpha.0",
43
+ "@morlay/zephyr-app": "^0.2.0-alpha.0",
44
+ "@morlay/zephyr-ui": "^0.2.0-alpha.0"
45
+ },
46
+ "devDependencies": {
47
+ "@morlay/dsh-agent-loop": "^0.2.0-alpha.0",
48
+ "@morlay/dsh-compaction": "^0.2.0-alpha.0",
49
+ "@morlay/dsh-session-persistence": "^0.2.0-alpha.0",
50
+ "@morlay/session-rdb": "^0.2.0-alpha.0",
51
+ "@testing-library/react": "^16.3.3"
52
+ },
53
+ "peerDependencies": {
54
+ "react": "^19.3.1",
55
+ "zod": "^4.6.6"
56
+ },
57
+ "engines": {
58
+ "dsh": ">= 0.2.3-alpha.3 < 0.2.4"
59
+ },
60
+ "repository": {
61
+ "type": "git",
62
+ "url": "https://github.com/morlay/zephyr.git",
63
+ "directory": "packages/agent-ex/session-mode"
64
+ },
65
+ "scripts": {}
6
66
  }
@@ -0,0 +1,139 @@
1
+ // 通道那一行:装配结果上的文本转换(`replace` / `suppress`)与降级 section 的按步送达。
2
+ // 工作区指令与技能目录走官方行,不经这里;本行只管"装配结果上换文本 / 丢文本 / 把降级的改到 reminder 里"。
3
+ import type { Agent } from "@morlay/dsh-agent/contract";
4
+ import type { AssembledSection } from "@morlay/dsh-agent/contract";
5
+ import { SystemPrompt } from "@morlay/dsh-agent";
6
+ import { inject, Injectable, provide, type Context, type OnInit } from "@morlay/zephyr";
7
+ import { ContextAssembler } from "./contract/context-assembler.contract.ts";
8
+ import { ContextAssemblerDefault } from "./context-assembler.service.ts";
9
+ import { DEFAULT_KEEP, DEFAULT_REPLACE, DEFAULT_SUPPRESS } from "./defaults.ts";
10
+ import { AgentPreStep, AgentRequestError, SystemPromptAssemble } from "./events.ts";
11
+ import { latestReminderText, reminderMessage, RULES_SECTION } from "./reminder.ts";
12
+ import { RULES_TEXT } from "./rules.ts";
13
+
14
+ // 规则声明的位置:紧跟部署 persona(order 0),在任何降级内容之前。
15
+ const RULES_ORDER = 1;
16
+
17
+ // `keep` / `suppress` / `replace` 是可覆盖的 config:缺省就是本部署要的那一份。
18
+ export interface ContextAssemblerConfig {
19
+ readonly keep?: readonly string[];
20
+ readonly suppress?: readonly string[];
21
+ readonly replace?: Readonly<Record<string, string>>;
22
+ }
23
+
24
+ function configWithDefaults(input: ContextAssemblerConfig): Required<ContextAssemblerConfig> {
25
+ return {
26
+ keep: [...(input.keep ?? DEFAULT_KEEP)],
27
+ suppress: [...(input.suppress ?? DEFAULT_SUPPRESS)],
28
+ replace: { ...(input.replace ?? DEFAULT_REPLACE) },
29
+ };
30
+ }
31
+
32
+ // 降级 section 的条目 id:一条 section 一个 id,于是同一次变化只重发那一条。
33
+ function sectionId(name: string): string {
34
+ return `section:${name}`;
35
+ }
36
+
37
+ // 通道那一行的 Provider:把三份清单装进服务,再把两个送达接缝挂上。
38
+ @Injectable()
39
+ export class ContextChannel implements OnInit {
40
+ static Config = configWithDefaults;
41
+
42
+ readonly assemble$ = new SystemPromptAssemble();
43
+
44
+ readonly preStep$ = new AgentPreStep();
45
+
46
+ readonly requestError$ = new AgentRequestError();
47
+
48
+ private readonly prompt = inject(SystemPrompt);
49
+
50
+ private readonly assembler = inject(ContextAssembler);
51
+
52
+ private readonly config: Required<ContextAssemblerConfig>;
53
+
54
+ constructor(input: ContextAssemblerConfig = {}) {
55
+ this.config = configWithDefaults(input);
56
+ }
57
+
58
+ onInit(_ctx: Context): void {
59
+ const keep = new Set(this.config.keep);
60
+ for (const name of this.config.suppress) this.assembler.suppressSection(name);
61
+ for (const [name, text] of Object.entries(this.config.replace)) {
62
+ this.assembler.replaceSection(name, text);
63
+ }
64
+ // 三段清单都显式留空 = 上游原版:不降级、不抑制、不换文案,规则声明也不注册。
65
+ if (
66
+ keep.size > 0 ||
67
+ this.config.suppress.length > 0 ||
68
+ Object.keys(this.config.replace).length > 0
69
+ ) {
70
+ this.prompt.section({ name: RULES_SECTION, order: RULES_ORDER, text: RULES_TEXT });
71
+ }
72
+
73
+ this.assemble$.onWaterfall(async (_assembly, context, next) => {
74
+ const result = await next();
75
+ const agent = context.agent as Agent | undefined;
76
+ const demoted = new Map<string, string>();
77
+ const sections: AssembledSection[] = [];
78
+ let changed = false;
79
+ for (const section of result.sections) {
80
+ if (this.assembler.isSuppressed(section.name)) {
81
+ changed = true;
82
+ continue;
83
+ }
84
+ const text = this.assembler.replacement(section.name) ?? section.text;
85
+ if (text !== section.text) changed = true;
86
+ // `keep` 显式留空 = 全部保留(只在有白名单时才把其余 section 降级成 reminder)。
87
+ if (keep.size === 0 || keep.has(section.name) || text.length === 0) {
88
+ sections.push(text === section.text ? section : { ...section, text });
89
+ continue;
90
+ }
91
+ changed = true;
92
+ demoted.set(sectionId(section.name), text);
93
+ }
94
+ if (agent !== undefined) this.assembler.sync(agent, { sections: demoted });
95
+ return changed ? { ...result, sections } : result;
96
+ });
97
+
98
+ this.preStep$.onWaterfall(async ({ agent, messages }, next) => {
99
+ const decision = await next();
100
+ if (decision.kind === "reject" || decision.messages.length === 0) return decision;
101
+ const pending = this.pendingEntries(agent);
102
+ if (pending.length === 0) return decision;
103
+ // 落点:本步认领的用户消息**之后**(不是列表末尾)。
104
+ const claimedEnd = decision.messages.findLastIndex((message) => messages.includes(message));
105
+ return {
106
+ ...decision,
107
+ messages: decision.messages.toSpliced(
108
+ claimedEnd + 1,
109
+ 0,
110
+ ...pending.map(([id, text]) => reminderMessage(id, text)),
111
+ ),
112
+ };
113
+ });
114
+
115
+ // 上游只在首次尝试落库 `decision.messages`,重试是同一批消息的再请求:压缩会吃掉早期 reminder,
116
+ // 重试不重跑 `pre-step`,所以这里补写进会话。
117
+ this.requestError$.onWaterfall(
118
+ async ({ agent, signal }, next) => {
119
+ const action = await next();
120
+ if (action?.kind !== "retry" || signal.aborted) return action;
121
+ for (const [id, text] of this.pendingEntries(agent)) {
122
+ agent.session.append("user/message", reminderMessage(id, text), { surfaceOp: "append" });
123
+ }
124
+ return action;
125
+ },
126
+ // 抢在压缩那一侧的重试监听之前:补写要落在同一次重试决策里(源包同一位置)。
127
+ { prepend: true },
128
+ );
129
+ }
130
+
131
+ // 本步要注入的条目(id → 已渲染正文):同 id 文本未变就不注入,按 id 字典序(顺序不随注册顺序抖动)。
132
+ private pendingEntries(agent: Agent): [string, string][] {
133
+ return [...this.assembler.collect(agent)]
134
+ .filter(([id, text]) => latestReminderText(agent, id) !== text)
135
+ .toSorted(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0));
136
+ }
137
+ }
138
+
139
+ export const provideContextAssembler = provide([ContextAssemblerDefault, ContextChannel]);
@@ -0,0 +1,67 @@
1
+ // 提示词通道:装配结果上的文本改写,与降级 section 的按步送达。
2
+ import type { Agent } from "@morlay/dsh-agent/contract";
3
+ import { Injectable } from "@morlay/zephyr";
4
+ import { ContextAssembler, type AssemblerEntries } from "./contract/context-assembler.contract.ts";
5
+ import { renderReminder } from "./reminder.ts";
6
+
7
+ interface AgentState {
8
+ // 本步装配降级出来的 section 条目:id → 正文(未渲染信封)。
9
+ sections: AssemblerEntries;
10
+ }
11
+
12
+ @Injectable()
13
+ export class ContextAssemblerDefault extends ContextAssembler {
14
+ private readonly replacements = new Map<string, string>();
15
+
16
+ private readonly suppressed = new Set<string>();
17
+
18
+ private readonly states = new WeakMap<Agent, AgentState>();
19
+
20
+ // 不要 instruction 类注入的会话(模式说了 `instructions: false`):降级 section 不进提示词。
21
+ private readonly withoutInstructions = new WeakSet<Agent>();
22
+
23
+ replaceSection(name: string, text: string): () => void {
24
+ this.replacements.set(name, text);
25
+ return () => {
26
+ if (this.replacements.get(name) === text) this.replacements.delete(name);
27
+ };
28
+ }
29
+
30
+ suppressSection(name: string): () => void {
31
+ this.suppressed.add(name);
32
+ return () => {
33
+ this.suppressed.delete(name);
34
+ };
35
+ }
36
+
37
+ isSuppressed(name: string): boolean {
38
+ return this.suppressed.has(name);
39
+ }
40
+
41
+ replacement(name: string): string | undefined {
42
+ return this.replacements.get(name);
43
+ }
44
+
45
+ setInstructions(agent: Agent, on: boolean): void {
46
+ if (on) this.withoutInstructions.delete(agent);
47
+ else this.withoutInstructions.add(agent);
48
+ }
49
+
50
+ collect(agent: Agent): AssemblerEntries {
51
+ const entries = new Map<string, string>();
52
+ if (this.withoutInstructions.has(agent)) return entries;
53
+ for (const [id, text] of this.states.get(agent)?.sections ?? new Map<string, string>()) {
54
+ entries.set(id, renderReminder(id, text));
55
+ }
56
+ return entries;
57
+ }
58
+
59
+ sync(agent: Agent, input: { readonly sections: AssemblerEntries }): void {
60
+ const existing = this.states.get(agent);
61
+ if (existing !== undefined) {
62
+ existing.sections = input.sections;
63
+ return;
64
+ }
65
+ this.states.set(agent, { sections: input.sections });
66
+ }
67
+ }
@@ -0,0 +1,30 @@
1
+ // 提示词通道的契约:装配结果上的文本改写(`replaceSection` / `suppressSection`)与降级 section 的按步送达。
2
+ import type { Agent } from "@morlay/dsh-agent/contract";
3
+ import { Injectable } from "@morlay/zephyr";
4
+
5
+ // 一条要按步注入的条目:id 是幂等键(一条 section 一个 id),正文已套好信封。
6
+ export type AssemblerEntries = ReadonlyMap<string, string>;
7
+
8
+ // `name` 是上游服务名别名:源包与上游别的行按 `ctx.contextAssembler` 读这一份。
9
+ @Injectable({ name: "contextAssembler" })
10
+ export abstract class ContextAssembler {
11
+ // 装配结果上把这个 section 的文本换成 `text`;返回撤销函数。
12
+ abstract replaceSection(name: string, text: string): () => void;
13
+
14
+ // 这个 section 不进提示词;返回撤销函数。
15
+ abstract suppressSection(name: string): () => void;
16
+
17
+ abstract isSuppressed(name: string): boolean;
18
+
19
+ // 装配结果上该 section 要换成的文本;`undefined` 表示原样保留。
20
+ abstract replacement(name: string): string | undefined;
21
+
22
+ // 这个会话要不要 instruction 类注入(模式说 `instructions: false` 就是不要)。
23
+ abstract setInstructions(agent: Agent, on: boolean): void;
24
+
25
+ // 本步要注入的条目:降级出来的 section(id → 已渲染正文,含信封)。
26
+ abstract collect(agent: Agent): AssemblerEntries;
27
+
28
+ // 每步装配调一次:记下本步降级出来的 section 文本(id → 正文,未套信封),供下一步注入。
29
+ abstract sync(agent: Agent, input: { readonly sections: AssemblerEntries }): void;
30
+ }
@@ -0,0 +1,3 @@
1
+ // session-mode 的**契约面**:进程内的端口(宿主半按它取服务)。
2
+ export * from "./context-assembler.contract.ts";
3
+ export * from "./session-mode.contract.ts";
@@ -0,0 +1,104 @@
1
+ // 会话模式的契约:模式清单、按会话的选择、把一份模式定义落到这个会话上(收口)。
2
+ // 只列**有调用方**的成员:wire 端点(`./server` 的 `SessionModeEndpointDefault`)、收口与子代理候选、
3
+ // 以及 `tool-guidance` 的注入判据。内部用得到的读面(如默认模型)走实现类,不上契约。
4
+ import type { Agent } from "@morlay/dsh-agent/contract";
5
+ import type { Session } from "@morlay/dsh-session/contract";
6
+ import type { SessionId } from "@morlay/dsh-session/endpoint";
7
+ import { Injectable } from "@morlay/zephyr";
8
+ import type { ModeConfig, SessionModeRole } from "../endpoint/mode-config.ts";
9
+ import type {
10
+ SessionModeActiveState,
11
+ SessionModePatch,
12
+ SessionModeRoster,
13
+ SessionModeRow,
14
+ } from "../endpoint/session-mode-shapes.ts";
15
+
16
+ // 两端共用的角色声明归 `./endpoint`,这里只转出(一处一个 home)。
17
+ export type { SessionModeRole } from "../endpoint/mode-config.ts";
18
+
19
+ // 三个注入开关的补丁:`null` = 清掉这一项的会话覆盖。
20
+ export type SessionModeSwitchPatch = Readonly<
21
+ Partial<Record<"instructions" | "skills" | "runtimeContext", boolean | null>>
22
+ >;
23
+
24
+ // `name` 是上游服务名别名:源包与上游别的行按 `ctx.sessionModes` 读这一份。
25
+ @Injectable({ name: "sessionModes" })
26
+ export abstract class SessionMode {
27
+ // 新会话用它。
28
+ abstract readonly defaultId: string;
29
+
30
+ // 选择器要的清单(只列 `main` 角色)。
31
+ abstract list(): SessionModeRow[];
32
+
33
+ // 声明了某个角色的模式 id(顺序即清单插入序):`main` 给选择器,`subagent` 给子代理候选。
34
+ abstract idsFor(role: SessionModeRole): string[];
35
+
36
+ // 同 `idsFor`,给的是定义——「指定 mode」那条接缝要拿候选集。
37
+ abstract modesFor(role: SessionModeRole): { id: string; mode: ModeConfig }[];
38
+
39
+ // 清单 + 默认模式(wire 的 `sessionMode/roster`)。
40
+ abstract roster(): SessionModeRoster;
41
+
42
+ // 按 id 取定义;未知 id 抛。
43
+ abstract definition(id?: string): ModeConfig;
44
+
45
+ // 会话当前模式的完整定义(`modeOf` 的展开)。
46
+ abstract modeOfSession(session: Session): ModeConfig;
47
+
48
+ // 会话自己选过的模式(没选过是 `null`:回落部署默认不算「选过」)。
49
+ abstract selectedOf(session: Session): string | null;
50
+
51
+ // 会话当前模式(投影上有就用它,否则部署默认)。
52
+ abstract modeOf(session: Session): string;
53
+
54
+ // 这个会话还能不能换模式(空白窗口):服务端拒绝与 client chip 的只读形态都读它。
55
+ abstract editableOf(session: Session): boolean;
56
+
57
+ // 这个会话在模式名单收窄之后"程序内可用"的工具名(`tool-guidance` 的注入判据)。
58
+ abstract knownToolsOf(agent: Agent): readonly string[];
59
+
60
+ // preset → 模式的反查:只在唯一时回答(模式声明了 `presetsOnly` 且本部署那个 preset 在其中时)。
61
+ abstract modeForPreset(preset: string | undefined): string | undefined;
62
+
63
+ // 把某个空白会话切到某个模式(wire 的 `sessionMode/select`)。
64
+ abstract select(sessionId: SessionId, mode: string): Promise<string>;
65
+
66
+ // 把某个活着的 agent 切到某个模式(子代理继承走这里)。
67
+ abstract applyTo(agent: Agent, mode: string, options?: { record?: boolean }): void;
68
+
69
+ // 某个会话**当下生效**的一份(面板读它)。
70
+ abstract activeState(sessionId: SessionId): Promise<SessionModeActiveState | undefined>;
71
+
72
+ // 改这个会话的字段覆盖(`null` 清掉该字段)。
73
+ abstract setModePatch(
74
+ sessionId: SessionId,
75
+ patch: SessionModePatch,
76
+ ): Promise<SessionModeActiveState | undefined>;
77
+
78
+ // 改这个会话的三个注入开关。
79
+ abstract setSwitches(
80
+ sessionId: SessionId,
81
+ patch: SessionModeSwitchPatch,
82
+ ): Promise<SessionModeActiveState | undefined>;
83
+
84
+ // 一次性应用整份会话覆盖(面板的"应用到会话")。
85
+ abstract applyConfig(
86
+ sessionId: SessionId,
87
+ config: ModeConfig,
88
+ ): Promise<SessionModeActiveState | undefined>;
89
+
90
+ // 把草稿写回模式配置(面板的"保存为默认")。
91
+ abstract saveConfig(
92
+ sessionId: SessionId,
93
+ config: ModeConfig,
94
+ ): Promise<SessionModeActiveState | undefined>;
95
+
96
+ // 把当前会话的覆盖写回这个模式。
97
+ abstract saveAsDefault(sessionId: SessionId): Promise<SessionModeActiveState | undefined>;
98
+
99
+ // 以当前会话的生效配置另存为一个新模式(面板的"另存为"弹窗给 code 与显示名);返回新 id。
100
+ abstract saveAsNewMode(sessionId: SessionId, code: string, name?: string): Promise<string>;
101
+
102
+ // 删除一个"另存为"出来的模式;返回会话回到的那个模式 id。
103
+ abstract deleteMode(sessionId: SessionId): Promise<string>;
104
+ }
@@ -0,0 +1,53 @@
1
+ // 按会话收口:一份模式定义落成这个会话的工具面、执行层 guard 与几个抑制面。
2
+ import type { Agent } from "@morlay/dsh-agent/contract";
3
+ import { Injectable } from "@morlay/zephyr";
4
+
5
+ // 一次收口的全部输入:模式定义里本域用到的那几项。
6
+ export interface SessionScopeDefinition {
7
+ // 拒绝对话时用的模式名。
8
+ readonly name: string;
9
+ // 这个会话能用的工具;留空 = 不设白名单(起点是全部工具)。
10
+ readonly allowTools: readonly string[];
11
+ // 这个会话明确排除的工具(deny 优先)。
12
+ readonly denyTools: readonly string[];
13
+ readonly allowSkills: readonly string[];
14
+ readonly denySkills: readonly string[];
15
+ // 要不要 instruction 类注入(关掉官方工作区指令与通道的降级注入)。
16
+ readonly instructions: boolean;
17
+ // 要不要技能目录(关掉官方 `skill-catalog` 的注入)。
18
+ readonly skills: boolean;
19
+ // 要不要动态快照(沙箱 / 审批策略与时钟注入)。
20
+ readonly runtimeContext: boolean;
21
+ // 这个模式下要丢弃的 system prompt section(精确名,或以 `*` 结尾的前缀)。
22
+ readonly dropSections: readonly string[];
23
+ }
24
+
25
+ // 一份 per-agent 的收回账:`add` 记一笔,`release` 同步、幂等地收干净。
26
+ export interface SessionScopeAccount {
27
+ add(stop: () => void): void;
28
+ release(): void;
29
+ }
30
+
31
+ // 一条工具 / 技能在目录里的样子(id + 描述)。
32
+ export interface ScopedToolEntry {
33
+ readonly name: string;
34
+ readonly description: string;
35
+ }
36
+
37
+ @Injectable()
38
+ export abstract class SessionScope {
39
+ // 一份新的 per-agent 收回账(行卸载与 agent 卸载任一处先到都收干净)。
40
+ abstract account(agent: Agent): SessionScopeAccount;
41
+
42
+ // 把某个会话收口到这份定义上(同一个 agent 再调就是换一份)。
43
+ abstract apply(agent: Agent, definition: SessionScopeDefinition): void;
44
+
45
+ // 这个会话在模式名单收窄之后"程序内可用"的工具名。
46
+ abstract knownTools(agent: Agent): readonly string[];
47
+
48
+ // 全量工具目录:收口那一刻抓的那一份(面板的"工具"折叠组读它)。
49
+ abstract catalogOf(agent: Agent): readonly ScopedToolEntry[];
50
+
51
+ // 技能目录:按这个会话的收口与工作目录扫出来的那一份。
52
+ abstract skillCatalog(agent: Agent): Promise<readonly ScopedToolEntry[]>;
53
+ }
@@ -0,0 +1,39 @@
1
+ // 三份可覆盖的缺省清单:留什么、丢什么、换成什么文案。
2
+ import { PERSONA_PREFIX_SECTION, PERSONA_SUFFIX_SECTION } from "@morlay/dsh-agent/contract";
3
+ import { RULES_SECTION } from "./reminder.ts";
4
+
5
+ // 留在系统提示词里的 section:部署级 persona(子 agent 在 agent 作用域注册的同名 persona 也命中)、规则声明,
6
+ // 以及工具声明的两条载体。后者留着的理由:PTC 生效时 `tools:sdk` 是被换成**稳定骨架**的(字节不随工具集变),
7
+ // 降级成 reminder 反而每步都追加一条、破坏前缀缓存——前缀缓存正是 PTC 骨架存在的意义。
8
+ export const DEFAULT_KEEP: readonly string[] = [
9
+ PERSONA_PREFIX_SECTION,
10
+ PERSONA_SUFFIX_SECTION,
11
+ RULES_SECTION,
12
+ "tools:sdk",
13
+ "tools:ptc-only",
14
+ ];
15
+
16
+ // 不进提示词的 section:harness 自带的开场白与平台运维说明、本部署不装配的工具说明、平台外壳说明。
17
+ export const DEFAULT_SUPPRESS: readonly string[] = [
18
+ "harness:identity",
19
+ "harness:source",
20
+ "app:web-surface",
21
+ "tool:lsp",
22
+ "tool:pty",
23
+ "tool:session-query",
24
+ "tool:cordis",
25
+ "tool:ralph",
26
+ ];
27
+
28
+ // 改写成中文的两段上游说明(内容忠实原意)。
29
+ export const DEFAULT_REPLACE: Readonly<Record<string, string>> = {
30
+ "context:file-reference":
31
+ "以 @ 开头的词是用户显式引用的工作区路径,相对于工作区根。结尾带斜杠表示目录:需要时列目录内容。" +
32
+ "其余是文件:需要内容时用 read 工具读取,不要在读取前声称自己已经看过。" +
33
+ '用 @"..." 括住含空格的路径。',
34
+ "ui:deliverable-file-references":
35
+ "成功创建或修改文件后,在最终回复里点明主要产物。除命令、配置表达式与代码块之外," +
36
+ "每处提到已存在的文件都要链到它的完整路径(相对工作目录或绝对路径);已知行号时在目标后加 " +
37
+ "`#L24` 或 `#L24-L30`。链接文本用文件名或清晰别名,只加够区分的父目录,不要把完整路径放进标签;" +
38
+ "默认只用文件名,需要精确位置时在标签后接 `:24` 或 `:24–30`(不带 `#` 或 `L`)。",
39
+ };
@@ -0,0 +1,6 @@
1
+ // session-mode 的 **RPC 面**(`@morlay/session-mode/endpoint`):组契约 + 两端共用的形状。
2
+ //
3
+ // 与包根的分界:包根是 host 契约与默认实现;这里只放两端都要读的声明,浏览器半因此不必引包根。
4
+ export * from "./mode-config.ts";
5
+ export * from "./session-mode-shapes.ts";
6
+ export * from "./session-mode-endpoint.ts";
@@ -0,0 +1,59 @@
1
+ // 两份纯函数:配置 + 会话覆盖 → 生效配置;面板草稿 → 会话覆盖差异。
2
+ // 会话覆盖是**全量**的一份 `ModeConfig`,不是字段级 merge——面板的 ViewModel 就是它。
3
+ import type { ModeConfig } from "./mode-config.ts";
4
+
5
+ export function resolveModeConfig(config: ModeConfig, session: ModeConfig | undefined): ModeConfig {
6
+ return structuredClone(session ?? config);
7
+ }
8
+
9
+ // 全量比较:不同 → 整份 ViewModel 作为覆盖;相同 → 没有覆盖。
10
+ export function diffModeConfig(config: ModeConfig, view: ModeConfig): ModeConfig | undefined {
11
+ return JSON.stringify(config) === JSON.stringify(view) ? undefined : structuredClone(view);
12
+ }
13
+
14
+ // 面板草稿:三态按"组引用 / 条目 id"记录;`unset` = 从两边名单里都移除。
15
+ export interface ModeDraft {
16
+ readonly lists?: Readonly<
17
+ Record<"tools" | "skills" | "policies", Readonly<Record<string, "allow" | "deny" | "unset">>>
18
+ >;
19
+ readonly persona?: { prefix: string; suffix: string } | null;
20
+ readonly switches?: Readonly<Record<string, boolean | null>>;
21
+ }
22
+
23
+ // ViewModel 的修改 → 符合预期的 `ModeConfig`(`defaults` 是"回到默认"时的取值基座)。
24
+ export function applyDraft(
25
+ config: ModeConfig,
26
+ draft: ModeDraft,
27
+ defaults?: ModeConfig,
28
+ ): ModeConfig {
29
+ const view: Record<string, unknown> = { ...structuredClone(config) };
30
+ const lists = [
31
+ ["tools", "allowTools", "denyTools"],
32
+ ["skills", "allowSkills", "denySkills"],
33
+ ["policies", "allowPolicies", "denyPolicies"],
34
+ ] as const;
35
+ for (const [kind, allowKey, denyKey] of lists) {
36
+ const allow = new Set(view[allowKey] as string[]);
37
+ const deny = new Set(view[denyKey] as string[]);
38
+ for (const [id, value] of Object.entries(draft.lists?.[kind] ?? {})) {
39
+ if (value === "deny") deny.add(id);
40
+ else if (value === "allow") {
41
+ deny.delete(id);
42
+ // 空白名单 = 全放行:点"允许"只从 deny 里移除,不写 allow(写了就把配置变成"只允许它")。
43
+ if (allow.size > 0) allow.add(id);
44
+ } else {
45
+ allow.delete(id);
46
+ deny.delete(id);
47
+ }
48
+ }
49
+ view[allowKey] = [...allow];
50
+ view[denyKey] = [...deny];
51
+ }
52
+ if (draft.persona !== undefined && draft.persona !== null) view["persona"] = draft.persona;
53
+ for (const [key, value] of Object.entries(draft.switches ?? {})) {
54
+ if (value !== null) view[key] = value;
55
+ else if (defaults !== undefined)
56
+ view[key] = (defaults as unknown as Record<string, unknown>)[key];
57
+ }
58
+ return view as unknown as ModeConfig;
59
+ }
@@ -0,0 +1,58 @@
1
+ // 模式配置的唯一模型:与 `<AppEnv.dataDir>/modes/<id>.toml` 同构,也是面板与 wire 上走的那一份。
2
+ // 字段全部必填(`defaultModel` 除外):读进来的部分配置先归一到这个形状,下游才不会读到 `undefined`。
3
+
4
+ export type SessionModeRole = "main" | "subagent";
5
+
6
+ // 模式的默认模型;省略的字段跟 provider 默认(与全局 `agent-default-model` 同形状)。
7
+ export interface SessionModeModel {
8
+ readonly provider: string;
9
+ readonly model: string;
10
+ readonly reasoningEffort?: string;
11
+ }
12
+
13
+ export interface SessionModePersona {
14
+ readonly prefix: string;
15
+ readonly suffix: string;
16
+ }
17
+
18
+ export interface ModeConfig {
19
+ // 这个模式只允许挂哪些 agent preset;留空 = 不限制(选这个模式不换 preset)。
20
+ readonly presetsOnly: readonly string[];
21
+ readonly name: string;
22
+ readonly description: string;
23
+ readonly role: readonly SessionModeRole[];
24
+ readonly persona: SessionModePersona;
25
+ // 工具白名单;留空 = 不设收窄。
26
+ readonly allowTools: readonly string[];
27
+ // 工具黑名单:从上面那份里减掉(deny 优先)。
28
+ readonly denyTools: readonly string[];
29
+ readonly allowSkills: readonly string[];
30
+ readonly denySkills: readonly string[];
31
+ // 上游 policy 规则的生效白名单;留空 = 全部生效。
32
+ readonly allowPolicies: readonly string[];
33
+ readonly denyPolicies: readonly string[];
34
+ // 是否要 instruction 类注入(官方工作区指令 + 通道的降级注入)。
35
+ readonly instructions: boolean;
36
+ // 是否要技能目录;不写就按工具名单推导(见 `derivedSkills`)。
37
+ readonly skills: boolean;
38
+ // 这个模式下要丢弃的 system prompt section(精确名,或以 `*` 结尾的前缀)。
39
+ readonly dropSections: readonly string[];
40
+ // 是否要动态快照(沙箱 / 审批策略)与时钟注入。
41
+ readonly runtimeContext: boolean;
42
+ readonly defaultModel?: SessionModeModel;
43
+ }
44
+
45
+ // 一份模式定义 → 完整 `ModeConfig`:`skills` 缺省时由调用方按这份定义自己的工具名单推导(`derivedSkills`)。
46
+ // 面板与 `activeState` 都经它,于是下游读到的永远是解析后的布尔。
47
+ export function asModeConfig(
48
+ mode: Omit<ModeConfig, "skills"> & { readonly skills?: boolean },
49
+ skills: boolean,
50
+ ): ModeConfig {
51
+ return { ...mode, skills } as ModeConfig;
52
+ }
53
+
54
+ // `skills` 不写时的推导:起点是白名单(留空 = 全部工具,含 `skill`),减去黑名单,还留着 `skill` 就要目录。
55
+ export function derivedSkills(mode: Pick<ModeConfig, "allowTools" | "denyTools">): boolean {
56
+ const allowed = mode.allowTools.length === 0 || mode.allowTools.includes("skill");
57
+ return allowed && !mode.denyTools.includes("skill");
58
+ }