@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.
- package/LICENSE +21 -0
- package/README.md +85 -2
- package/dist/endpoint.d.mts +2 -0
- package/dist/endpoint.mjs +3 -0
- package/dist/index.d.mts +691 -0
- package/dist/index.mjs +2350 -0
- package/dist/mode-config-BIYT66jI.mjs +22 -0
- package/dist/mode-config-rules-z4gdHLR1.mjs +52 -0
- package/dist/rolldown-runtime-3qf6kE1h.d.mts +1 -0
- package/dist/server.d.mts +2 -0
- package/dist/server.mjs +1 -0
- package/dist/session-mode-endpoint-CLpvSnzM.d.mts +120 -0
- package/dist/session-mode-endpoint-Dq-E_kHt.mjs +53 -0
- package/dist/session-mode-endpoint.service-Bn4EH6aB.d.mts +21 -0
- package/dist/session-mode-endpoint.service-MrqQipN6.mjs +79 -0
- package/dist/testing.d.mts +12 -0
- package/dist/testing.mjs +23 -0
- package/dist/ui.d.mts +125 -0
- package/dist/ui.mjs +1165 -0
- package/package.json +63 -3
- package/src/context-assembler.provide.ts +139 -0
- package/src/context-assembler.service.ts +67 -0
- package/src/contract/context-assembler.contract.ts +30 -0
- package/src/contract/index.ts +3 -0
- package/src/contract/session-mode.contract.ts +104 -0
- package/src/contract/session-scope.contract.ts +53 -0
- package/src/defaults.ts +39 -0
- package/src/endpoint/index.ts +6 -0
- package/src/endpoint/mode-config-rules.ts +59 -0
- package/src/endpoint/mode-config.ts +58 -0
- package/src/endpoint/session-mode-endpoint.ts +121 -0
- package/src/endpoint/session-mode-shapes.ts +71 -0
- package/src/endpoint/tool-group-ref.ts +11 -0
- package/src/events.ts +102 -0
- package/src/guidance/groups.ts +101 -0
- package/src/guidance/index.ts +4 -0
- package/src/guidance/packs/ask.ts +17 -0
- package/src/guidance/packs/code.ts +25 -0
- package/src/guidance/packs/fs.ts +60 -0
- package/src/guidance/packs/index.ts +52 -0
- package/src/guidance/packs/mcp.ts +25 -0
- package/src/guidance/packs/plan.ts +42 -0
- package/src/guidance/packs/shell.ts +45 -0
- package/src/guidance/packs/skill.ts +12 -0
- package/src/guidance/packs/swarm.ts +60 -0
- package/src/guidance/packs/web-access.ts +24 -0
- package/src/guidance/tool-groups.ts +72 -0
- package/src/guidance/types.ts +29 -0
- package/src/index.ts +63 -0
- package/src/mode-sources.ts +83 -0
- package/src/mode-store.ts +45 -0
- package/src/modes.ts +126 -0
- package/src/per-agent-rows.ts +62 -0
- package/src/per-agent.ts +36 -0
- package/src/persona.ts +30 -0
- package/src/ptc-describe.ts +136 -0
- package/src/ptc-sdk.ts +101 -0
- package/src/reminder.ts +89 -0
- package/src/rules.ts +8 -0
- package/src/server/index.ts +7 -0
- package/src/server/session-mode-endpoint.service.ts +117 -0
- package/src/session-mode-store.ts +218 -0
- package/src/session-mode.projection.ts +78 -0
- package/src/session-mode.provide.ts +38 -0
- package/src/session-mode.service.ts +670 -0
- package/src/session-scope.service.ts +515 -0
- package/src/subagent/delegation-context.ts +30 -0
- package/src/subagent/return-guidance.ts +56 -0
- package/src/subagent/subagent-guidance.service.ts +76 -0
- package/src/subagent.provide.ts +5 -0
- package/src/system-remainder.ts +43 -0
- package/src/testing/index.ts +34 -0
- package/src/tool-guidance.service.ts +265 -0
- package/src/ui/index.ts +12 -0
- package/src/ui/locales.ts +134 -0
- package/src/ui/session-mode-endpoint.client.ts +19 -0
- package/src/ui/session-mode-locales.contribution.ts +51 -0
- package/src/ui/session-mode-panel-tab.ts +39 -0
- package/src/ui/session-mode-panel.view.tsx +931 -0
- package/src/ui/session-mode-seat.view.tsx +174 -0
- package/src/ui/session-mode-slots.ts +13 -0
- package/src/ui/session-mode.provide.ts +24 -0
- package/src/ui/session-mode.store.ts +90 -0
- package/src/ui/use-roster.ts +24 -0
- package/src/ui/view-props.ts +19 -0
package/src/per-agent.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// 本行替某个 agent 装的那一组注册的收回账。
|
|
2
|
+
//
|
|
3
|
+
// `agent.ctx.tools.register()` / `systemPrompt.section()` / `agent.ctx.on()` 把 effect 挂在 **agent 的 scope
|
|
4
|
+
// fiber** 上,所以这些注册不随本行卸载。账挂两处(行与 agent),任一处先到都收干净——本行被重挂时新实例才不会
|
|
5
|
+
// 撞上 `already registered`。
|
|
6
|
+
import type { Agent } from "@morlay/dsh-agent/contract";
|
|
7
|
+
import type { Context } from "@morlay/zephyr";
|
|
8
|
+
import type { SessionScopeAccount } from "./contract/session-scope.contract.ts";
|
|
9
|
+
|
|
10
|
+
export function perAgentAccount(ctx: Context, agent: Agent): SessionScopeAccount {
|
|
11
|
+
const stoppers: (() => void)[] = [];
|
|
12
|
+
let released = false;
|
|
13
|
+
let rowAccount: () => void = () => {};
|
|
14
|
+
let agentAccount: () => void = () => {};
|
|
15
|
+
|
|
16
|
+
const release = (): void => {
|
|
17
|
+
if (released) return;
|
|
18
|
+
released = true;
|
|
19
|
+
for (const stop of stoppers.splice(0).reverse()) stop();
|
|
20
|
+
agentAccount();
|
|
21
|
+
rowAccount();
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
rowAccount = ctx.effect(() => () => release()) as unknown as () => void;
|
|
25
|
+
agentAccount = (agent.ctx as unknown as Context).effect(
|
|
26
|
+
() => () => release(),
|
|
27
|
+
) as unknown as () => void;
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
add: (stop) => {
|
|
31
|
+
if (released) stop();
|
|
32
|
+
else stoppers.push(stop);
|
|
33
|
+
},
|
|
34
|
+
release,
|
|
35
|
+
};
|
|
36
|
+
}
|
package/src/persona.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// 按会话给提示词:把模式的 persona 注册到该 agent 自己的 scope 上(`deployment:persona-prefix` / `-suffix`),
|
|
2
|
+
// 跨层遮蔽部署级那层。两个 section 都注册(缺省是空串)。
|
|
3
|
+
import type { Agent } from "@morlay/dsh-agent/contract";
|
|
4
|
+
import type { PromptSectionOrderName } from "@morlay/dsh-agent/contract";
|
|
5
|
+
import { PERSONA_PREFIX_SECTION, PERSONA_SUFFIX_SECTION } from "@morlay/dsh-agent/contract";
|
|
6
|
+
import type { SessionModePersona } from "./endpoint/mode-config.ts";
|
|
7
|
+
import type { Context } from "@morlay/zephyr";
|
|
8
|
+
|
|
9
|
+
const PREFIX_ORDER: PromptSectionOrderName = "DEPLOYMENT_PERSONA_PREFIX";
|
|
10
|
+
const SUFFIX_ORDER: PromptSectionOrderName = "DEPLOYMENT_PERSONA_SUFFIX";
|
|
11
|
+
|
|
12
|
+
// 把一段 persona 装到该 agent 的 scope 上;返回注销这两个 section 的 disposer(模式切换时先调它)。
|
|
13
|
+
export function installPersona(agent: Agent, persona: SessionModePersona): () => void {
|
|
14
|
+
const prompt = (agent.ctx as unknown as Context).systemPrompt;
|
|
15
|
+
const disposers = [
|
|
16
|
+
prompt.section({
|
|
17
|
+
name: PERSONA_PREFIX_SECTION,
|
|
18
|
+
order: prompt.getSectionOrder(PREFIX_ORDER),
|
|
19
|
+
text: persona.prefix,
|
|
20
|
+
}),
|
|
21
|
+
prompt.section({
|
|
22
|
+
name: PERSONA_SUFFIX_SECTION,
|
|
23
|
+
order: prompt.getSectionOrder(SUFFIX_ORDER),
|
|
24
|
+
text: persona.suffix,
|
|
25
|
+
}),
|
|
26
|
+
];
|
|
27
|
+
return () => {
|
|
28
|
+
for (const dispose of disposers) dispose();
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// `describe_tool`:PTC 程序里现取某组工具的 d.ts(不把全量签名塞进 system)。
|
|
2
|
+
import { jsonSchemaToTs } from "@morlay/dsh-ptc/contract";
|
|
3
|
+
import { SHORT_TOOL_DESCRIPTIONS, type GroupDescriptor } from "./guidance/groups.ts";
|
|
4
|
+
import { toolGroupOf } from "./guidance/tool-groups.ts";
|
|
5
|
+
import { renderTools, type PtcToolSchema } from "./ptc-sdk.ts";
|
|
6
|
+
|
|
7
|
+
// 一条工具的静态描述(描述文本 + 宽松的 args/output 类型)。
|
|
8
|
+
export interface DescribeEntry {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly description: string;
|
|
11
|
+
readonly parametersSchema?: unknown;
|
|
12
|
+
readonly outputSchema?: unknown;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 按工具组分组;组名与面板上的分组一致(`mcp:agent-browser` 等)。
|
|
16
|
+
export function groupEntries(entries: readonly DescribeEntry[]): Map<string, PtcToolSchema[]> {
|
|
17
|
+
const groups = new Map<string, PtcToolSchema[]>();
|
|
18
|
+
for (const entry of entries) {
|
|
19
|
+
const group = toolGroupOf(entry.name);
|
|
20
|
+
const schema: PtcToolSchema = {
|
|
21
|
+
name: entry.name,
|
|
22
|
+
description: SHORT_TOOL_DESCRIPTIONS[entry.name] ?? entry.description,
|
|
23
|
+
args: schemaText(entry.parametersSchema),
|
|
24
|
+
output: schemaText(entry.outputSchema),
|
|
25
|
+
};
|
|
26
|
+
const list = groups.get(group);
|
|
27
|
+
if (list === undefined) groups.set(group, [schema]);
|
|
28
|
+
else list.push(schema);
|
|
29
|
+
}
|
|
30
|
+
return groups;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 输出 schema → TS;转不了就是 `unknown`(`jsonSchemaToTs` 自己兜底)。
|
|
34
|
+
function schemaText(schema: unknown): string {
|
|
35
|
+
return schema === undefined ? "JsonValue" : jsonSchemaToTs(schema);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const GROUP_SUMMARY: Readonly<Record<string, string>> = {
|
|
39
|
+
fs: "文件读写",
|
|
40
|
+
shell: "执行命令",
|
|
41
|
+
"web-access": "联网检索与抓取",
|
|
42
|
+
ask: "向用户提问",
|
|
43
|
+
plan: "计划与待办",
|
|
44
|
+
swarm: "子代理与消息",
|
|
45
|
+
skill: "加载技能说明",
|
|
46
|
+
code: "代码执行(PTC)",
|
|
47
|
+
mcp: "MCP 资源",
|
|
48
|
+
other: "其它",
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
function summaryOf(group: string): string {
|
|
52
|
+
if (group.startsWith("mcp:")) return `MCP · ${group.slice("mcp:".length)}`;
|
|
53
|
+
return GROUP_SUMMARY[group] ?? "其它";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 工具的执行体:不带 group 给「分组 + 一句话说明」,带 group 给该组的**用法正文** + d.ts 片段。
|
|
57
|
+
//
|
|
58
|
+
// 正文与签名一起给:模型在程序里 `await tools.describe_tool({ group })` 一次,就拿到该组的用法正文与 d.ts。
|
|
59
|
+
export function describeTools(
|
|
60
|
+
entries: readonly DescribeEntry[],
|
|
61
|
+
group?: string,
|
|
62
|
+
groupOf?: (group: string) => GroupDescriptor | undefined,
|
|
63
|
+
): string {
|
|
64
|
+
const groups = groupEntries(entries);
|
|
65
|
+
if (group === undefined) {
|
|
66
|
+
// 每组都给**一行判据**(「什么时候该看它」),不是只有一个名字:模型据此决定下一句查哪一组。
|
|
67
|
+
return [...groups.keys()]
|
|
68
|
+
.sort()
|
|
69
|
+
.map((name) => {
|
|
70
|
+
const described = groupOf?.(name);
|
|
71
|
+
const title = described?.title ?? summaryOf(name);
|
|
72
|
+
const guideline = described?.guideline ?? "工具说明";
|
|
73
|
+
return `${name} — ${title}:${guideline}`;
|
|
74
|
+
})
|
|
75
|
+
.join("\n");
|
|
76
|
+
}
|
|
77
|
+
const declaration = renderTools(groups.get(group) ?? []);
|
|
78
|
+
// 组正文(中文用法,按组表渲染);动态组(`mcp:<server>`)没有静态正文,只有签名。
|
|
79
|
+
const section = groupOf?.(group)?.body ?? "";
|
|
80
|
+
return section.length === 0 ? declaration : `${section}\n\n${declaration}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// `describe_tool` 的工具定义:PTC 程序里 `await tools.describe_tool({ group })` 取某组的 d.ts。
|
|
84
|
+
//
|
|
85
|
+
// **它是常驻工具**:骨架(`renderSkeleton`)在 system 里承诺了它,模型在程序里一定会调;
|
|
86
|
+
// 没注册时上游把调用当未知工具拒掉,模型只能瞎猜参数。所以 PTC 生效时必须在**该 agent 的 scope**
|
|
87
|
+
// 上注册它(与工具面的收窄同一个落点)。
|
|
88
|
+
//
|
|
89
|
+
// 描述留空:PTC 下工具说明不占 system,正文随 describe 结果返回。
|
|
90
|
+
export function describeToolDefinition(input: {
|
|
91
|
+
// 这一次要列的工具(调用时现取:工具集可能随按需组变化)。
|
|
92
|
+
readonly entries: () => readonly DescribeEntry[];
|
|
93
|
+
// 某个工具在 harness 里的说明 section 正文(`tool:<name>`)。
|
|
94
|
+
readonly sectionOf: (name: string) => string | undefined;
|
|
95
|
+
// 某个组的描述(标题 / 判据 / 正文);动态组由实现回落到它那一族。
|
|
96
|
+
readonly groupOf?: (group: string) => GroupDescriptor | undefined;
|
|
97
|
+
// 取到某一组时把它放行(按需组默认不可见,「取签名」与「能调用」必须是同一件事)。
|
|
98
|
+
readonly onDescribe?: (group: string) => void;
|
|
99
|
+
}): {
|
|
100
|
+
readonly name: "describe_tool";
|
|
101
|
+
readonly description: string;
|
|
102
|
+
readonly parameters: Readonly<Record<string, unknown>>;
|
|
103
|
+
readonly output: {
|
|
104
|
+
readonly schema: Readonly<Record<string, unknown>>;
|
|
105
|
+
readonly render: (
|
|
106
|
+
args: unknown,
|
|
107
|
+
value: { readonly text: string },
|
|
108
|
+
) => readonly { readonly type: "text"; readonly text: string }[];
|
|
109
|
+
};
|
|
110
|
+
readonly execute: (args: { readonly group?: unknown }) => Promise<{ readonly text: string }>;
|
|
111
|
+
} {
|
|
112
|
+
return {
|
|
113
|
+
name: "describe_tool",
|
|
114
|
+
description: "",
|
|
115
|
+
parameters: { group: { type: "string" } },
|
|
116
|
+
output: {
|
|
117
|
+
// 形状照源包:`required` 是对象级字段,不能写在属性里(上游会拒绝这种 schema)。
|
|
118
|
+
schema: {
|
|
119
|
+
type: "object",
|
|
120
|
+
additionalProperties: false,
|
|
121
|
+
properties: { text: { type: "string" } },
|
|
122
|
+
required: ["text"],
|
|
123
|
+
},
|
|
124
|
+
render: (_args, value) => [{ type: "text", text: value.text }],
|
|
125
|
+
},
|
|
126
|
+
async execute(args) {
|
|
127
|
+
const group = typeof args.group === "string" ? args.group : undefined;
|
|
128
|
+
const entries = input.entries().map((entry) => ({
|
|
129
|
+
...entry,
|
|
130
|
+
section: input.sectionOf(entry.name),
|
|
131
|
+
}));
|
|
132
|
+
if (group !== undefined) input.onDescribe?.(group);
|
|
133
|
+
return { text: describeTools(entries, group, input.groupOf) };
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
package/src/ptc-sdk.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// PTC 的 SDK 说明:**不**把全部工具铺成一张大表(工具一变、system 前缀就变)。
|
|
2
|
+
//
|
|
3
|
+
// - system 里只放稳定骨架:`interface ToolsSdk`(含 `describe_tool`)+ `declare const tools: ToolsSdk`;
|
|
4
|
+
// - 具体分组通过 `describe_tool({ group })` 现取(同一条 interface 的成员片段,多次 describe 合并)。
|
|
5
|
+
import { TOOLS_SDK_INTERFACE, renderToolKey, renderToolsDeclaration } from "./guidance/groups.ts";
|
|
6
|
+
import { isResidentToolGroup, toolGroupOf } from "./guidance/tool-groups.ts";
|
|
7
|
+
|
|
8
|
+
// 一个工具在 SDK 文本里的最小信息。
|
|
9
|
+
export interface PtcToolSchema {
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly description: string;
|
|
12
|
+
// 参数的 TypeScript 类型文本(已由调用方从 JSON Schema 转换好)。
|
|
13
|
+
readonly args: string;
|
|
14
|
+
// 输出的 TypeScript 类型文本。
|
|
15
|
+
readonly output: string;
|
|
16
|
+
// 该工具在 harness 里的说明 section(`tool:<name>`):随 describe 一起返回,不占 system。
|
|
17
|
+
readonly section?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 稳定的 system 骨架:所有具体工具都通过 describe 取,字节不随工具集变化。
|
|
21
|
+
export function renderSkeleton(groups: readonly string[], members: readonly string[] = []): string {
|
|
22
|
+
const list =
|
|
23
|
+
groups.length === 0
|
|
24
|
+
? "(当前没有可用的组)"
|
|
25
|
+
: groups.map((group) => JSON.stringify(group)).join(" | ");
|
|
26
|
+
return [
|
|
27
|
+
'在 `run_code` 写的程序里调用工具:`await tools.name(args)`;名字带引号的成员用索引访问(`tools["名字"](args)`)。**常驻组**的签名已列在下面;其余组用 `describe_tool({ group })` 现取(不带 `group` 返回分组清单):',
|
|
28
|
+
"",
|
|
29
|
+
"```ts",
|
|
30
|
+
"declare global {",
|
|
31
|
+
` interface ${TOOLS_SDK_INTERFACE} {`,
|
|
32
|
+
...members,
|
|
33
|
+
` describe_tool(args: { group?: ${list} }): Promise<{ text?: string }>`,
|
|
34
|
+
" }",
|
|
35
|
+
` const tools: ${TOOLS_SDK_INTERFACE}`,
|
|
36
|
+
"}",
|
|
37
|
+
"```",
|
|
38
|
+
].join("\n");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const PTC_SDK_SKELETON = renderSkeleton([]);
|
|
42
|
+
|
|
43
|
+
// 一段工具声明:参数与返回都内联成 TS 类型。
|
|
44
|
+
export function renderTools(schemas: readonly PtcToolSchema[]): string {
|
|
45
|
+
if (schemas.length === 0) return "";
|
|
46
|
+
const sorted = [...schemas].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
47
|
+
const declaration = renderToolsDeclaration(
|
|
48
|
+
sorted.map((schema) => ({ name: schema.name, args: schema.args, output: schema.output })),
|
|
49
|
+
);
|
|
50
|
+
const hint = sorted.some((schema) => renderToolKey(schema.name) !== schema.name)
|
|
51
|
+
? '名字带引号的成员用索引访问:`tools["名字"](args)`。\n\n'
|
|
52
|
+
: "";
|
|
53
|
+
const sections = schemas
|
|
54
|
+
.map((schema) => schema.section?.trim() ?? "")
|
|
55
|
+
.filter((text) => text.length > 0)
|
|
56
|
+
.join("\n\n");
|
|
57
|
+
const lead = sections.length === 0 ? "" : `${sections}\n\n`;
|
|
58
|
+
return `${lead}${hint}${["```ts", declaration, "```"].join("\n")}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// `describe_tool()` 的结果:不带参数返回分组清单,带参数返回该组的声明片段。
|
|
62
|
+
export function renderDescribe(
|
|
63
|
+
groups: ReadonlyMap<string, readonly PtcToolSchema[]>,
|
|
64
|
+
group?: string,
|
|
65
|
+
): string {
|
|
66
|
+
if (group === undefined) return [...groups.keys()].sort().join("\n");
|
|
67
|
+
return renderTools(groups.get(group) ?? []);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// PTC 生效时把上游那份全量 `tools:sdk` 换成稳定骨架;别的 section 原样保留。
|
|
71
|
+
export function replaceToolsSdk<T extends { readonly name: string; readonly text: string }>(
|
|
72
|
+
sections: readonly T[],
|
|
73
|
+
skeleton: string,
|
|
74
|
+
): readonly T[] {
|
|
75
|
+
return sections.map((section) =>
|
|
76
|
+
section.name === "tools:sdk" ? { ...section, text: skeleton } : section,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// system 骨架的正文:group 清单由当前可见工具现算(模型才知道有哪些组可取)。
|
|
81
|
+
export function ptcSdkText(visible: readonly string[]): string {
|
|
82
|
+
return renderSkeleton([...new Set(visible.map((name) => toolGroupOf(name)))].sort());
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 一次 PTC 装配对本步 section 的改写:换掉 `tools:sdk`,丢掉按需组的工具说明(它们随 describe 回)。
|
|
86
|
+
export function ptcSections<T extends { readonly name: string; readonly text: string }>(
|
|
87
|
+
sections: readonly T[],
|
|
88
|
+
visible: readonly string[],
|
|
89
|
+
): readonly T[] {
|
|
90
|
+
const keep = (name: string): boolean => {
|
|
91
|
+
if (!name.startsWith("tool:")) return true;
|
|
92
|
+
const tool = name.slice("tool:".length);
|
|
93
|
+
// 常驻组的说明留在 system;按需组与不可见的工具由 describe 现取。
|
|
94
|
+
if (!visible.includes(tool)) return false;
|
|
95
|
+
return isResidentToolGroup(toolGroupOf(tool));
|
|
96
|
+
};
|
|
97
|
+
return replaceToolsSdk(
|
|
98
|
+
sections.filter((section) => keep(section.name)),
|
|
99
|
+
ptcSdkText(visible),
|
|
100
|
+
);
|
|
101
|
+
}
|
package/src/reminder.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// 两种按步送达的条目,身份都写在 `source` 上(不扩上游 `MessageSourceMap`:source 只是随消息落账的普通对象):
|
|
2
|
+
//
|
|
3
|
+
// - **通道**(降级 section):`kind: "context-assembler"`,幂等键是 `id`(一条 section 一个 id),
|
|
4
|
+
// 信封是带 id 的 `<system-reminder id="…">`——与源包 `context-assembler/src/reminder.ts` 同形;
|
|
5
|
+
// - **工具说明的常驻正文**:借上游已有的 `kind: "skill-invocation"` + `name`(与按需加载的技能同形态),
|
|
6
|
+
// 信封是不带 id 的 `<system-reminder>`。
|
|
7
|
+
//
|
|
8
|
+
// 两者都是同一条 user 消息形态,所以「读哪一条」必须按来源分开,否则会互相顶掉。
|
|
9
|
+
import type { Agent } from "@morlay/dsh-agent/contract";
|
|
10
|
+
import { createUserMessage, type MessageSource } from "@morlay/dsh-agent/contract";
|
|
11
|
+
import type { UserMessage } from "@morlay/dsh-agent/contract";
|
|
12
|
+
|
|
13
|
+
const REMINDER_OPEN = "<system-reminder";
|
|
14
|
+
const REMINDER_CLOSE = "</system-reminder>";
|
|
15
|
+
|
|
16
|
+
// 覆盖规则在系统提示词里声明一次,每条 reminder 只带 id 与正文。
|
|
17
|
+
export const RULES_SECTION = "assembler:rules";
|
|
18
|
+
|
|
19
|
+
// 通道自己的条目形状:降级出来的规则块,幂等键是 id。
|
|
20
|
+
export interface PromptReminderSource {
|
|
21
|
+
readonly kind: "context-assembler";
|
|
22
|
+
readonly form: "instructions";
|
|
23
|
+
readonly id?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 这条消息的通道幂等键:判据是 `id`(不是 kind——kind 只描述这条消息对外的身份)。
|
|
27
|
+
export function promptEntryIdOf(source: MessageSource): string | undefined {
|
|
28
|
+
const record = source as { readonly kind?: unknown; readonly id?: unknown };
|
|
29
|
+
if (record.kind !== "context-assembler") return undefined;
|
|
30
|
+
return typeof record.id === "string" ? record.id : undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function escapeFrameBody(body: string): string {
|
|
34
|
+
return body.replaceAll(REMINDER_CLOSE, "<\\/system-reminder>");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function escapeAttr(value: string): string {
|
|
38
|
+
return value.replaceAll("&", "&").replaceAll('"', """).replaceAll("<", "<");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 带 id 的信封(通道用)。
|
|
42
|
+
export function renderReminder(id: string, body: string): string {
|
|
43
|
+
return `${REMINDER_OPEN} id="${escapeAttr(id)}">\n${escapeFrameBody(body)}\n${REMINDER_CLOSE}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 不带 id 的信封(工具说明的常驻正文用):上下各空一行,身份由 `source.name` 给。
|
|
47
|
+
export function renderConstantReminder(body: string): string {
|
|
48
|
+
return `${REMINDER_OPEN}>\n\n${escapeFrameBody(body)}\n\n${REMINDER_CLOSE}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// surface 上最近一条该 id 的 reminder 原文(含信封);没有则 `undefined`。
|
|
52
|
+
// 走 `deriveMessages()`(surface 的派生消息视图,受支持的同步读)而不是逐 seq 读事件。
|
|
53
|
+
export function latestReminderText(agent: Agent, id: string): string | undefined {
|
|
54
|
+
for (const message of agent.session.deriveMessages().toReversed()) {
|
|
55
|
+
if (message.role !== "user") continue;
|
|
56
|
+
if (promptEntryIdOf(message.source) !== id) continue;
|
|
57
|
+
const [block] = message.content;
|
|
58
|
+
return message.content.length === 1 && block?.type === "text" ? block.text : "";
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// 注入一条提醒:`text` 是已渲染好的正文(规则块),`key` 是它的幂等键。
|
|
64
|
+
export function reminderMessage(key: string, text: string): UserMessage {
|
|
65
|
+
// 上游 `MessageSourceMap` 不认这个 kind(通道自己的条目),构造入参在这里断言一次。
|
|
66
|
+
return createUserMessage({
|
|
67
|
+
content: [{ type: "text", text }],
|
|
68
|
+
source: { kind: "context-assembler", form: "instructions", id: key } as never,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 工具说明的常驻正文:身份是上游 `skill-invocation` + `name`(幂等键),信封不带 id。
|
|
73
|
+
export function latestConstantText(agent: Agent, name: string): string | undefined {
|
|
74
|
+
for (const message of agent.session.deriveMessages().toReversed()) {
|
|
75
|
+
if (message.role !== "user") continue;
|
|
76
|
+
const source = message.source as { readonly kind?: unknown; readonly name?: unknown };
|
|
77
|
+
if (source.kind !== "skill-invocation" || source.name !== name) continue;
|
|
78
|
+
const [block] = message.content;
|
|
79
|
+
return message.content.length === 1 && block?.type === "text" ? block.text : undefined;
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function constantMessage(name: string, text: string): UserMessage {
|
|
85
|
+
return createUserMessage({
|
|
86
|
+
content: [{ type: "text", text }],
|
|
87
|
+
source: { kind: "skill-invocation", name, form: "instructions" } as never,
|
|
88
|
+
});
|
|
89
|
+
}
|
package/src/rules.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// session-mode 的**宿主半**(`@morlay/session-mode/server`):**只出服务相关的**——装配函数与 wire 端点实现。
|
|
2
|
+
//
|
|
3
|
+
// 规范:`server/` = `endpoint/` 契约的宿主实现 + 同进程端口实现 + `provideXxx`。默认实现与它们
|
|
4
|
+
// 依赖的纯函数/纯数据在包根(与 `dsh-workspace` / `dsh-skill` 同形);浏览器半在 `./ui`。
|
|
5
|
+
|
|
6
|
+
// `../endpoint/` 契约的宿主实现(注册到 wire)。
|
|
7
|
+
export { SessionModeEndpointDefault } from "./session-mode-endpoint.service.ts";
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// `sessionMode` 组的宿主实现:把契约方法转发到 `SessionMode` 服务,并注册到 wire。
|
|
2
|
+
import type { Agent } from "@morlay/dsh-agent/contract";
|
|
3
|
+
import { AgentDirectory } from "@morlay/dsh-agent";
|
|
4
|
+
import { SessionId } from "@morlay/dsh-session/endpoint";
|
|
5
|
+
import { inject, type OnInit } from "@morlay/zephyr";
|
|
6
|
+
import { RemoteHandlers } from "@morlay/zephyr-app";
|
|
7
|
+
import { RemoteGroup, registerRemoteGroup } from "@morlay/zephyr-app/remote";
|
|
8
|
+
import { SessionMode } from "../contract/session-mode.contract.ts";
|
|
9
|
+
import { SessionScope } from "../contract/session-scope.contract.ts";
|
|
10
|
+
import {
|
|
11
|
+
SessionModeEndpoint,
|
|
12
|
+
type SessionModeApplyConfigRequest,
|
|
13
|
+
type SessionModeOfRequest,
|
|
14
|
+
type SessionModePatchRequest,
|
|
15
|
+
type SessionModeSaveAsRequest,
|
|
16
|
+
type SessionModeSaveAsValue,
|
|
17
|
+
type SessionModeSaveConfigRequest,
|
|
18
|
+
type SessionModeSelectRequest,
|
|
19
|
+
type SessionModeSelectValue,
|
|
20
|
+
} from "../endpoint/session-mode-endpoint.ts";
|
|
21
|
+
import type {
|
|
22
|
+
SessionModeActiveState,
|
|
23
|
+
SessionModeCatalogValue,
|
|
24
|
+
SessionModeRoster,
|
|
25
|
+
} from "../endpoint/session-mode-shapes.ts";
|
|
26
|
+
|
|
27
|
+
// 会话不在场时 wire 回的失败:形状固定,客户端按 message 显示。
|
|
28
|
+
function missing(sessionId: string): Error {
|
|
29
|
+
return new Error(`会话不存在:${sessionId}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@RemoteGroup()
|
|
33
|
+
export class SessionModeEndpointDefault extends SessionModeEndpoint implements OnInit {
|
|
34
|
+
private readonly handlers = inject(RemoteHandlers);
|
|
35
|
+
|
|
36
|
+
private readonly modes = inject(SessionMode);
|
|
37
|
+
|
|
38
|
+
private readonly scope = inject(SessionScope);
|
|
39
|
+
|
|
40
|
+
private readonly directory = inject(AgentDirectory);
|
|
41
|
+
|
|
42
|
+
onInit(): void {
|
|
43
|
+
registerRemoteGroup(this.handlers, this);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
override async roster(): Promise<SessionModeRoster> {
|
|
47
|
+
return this.modes.roster();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
override async active(request: SessionModeOfRequest): Promise<SessionModeActiveState> {
|
|
51
|
+
const state = await this.modes.activeState(SessionId(request.sessionId));
|
|
52
|
+
if (state === undefined) throw missing(request.sessionId);
|
|
53
|
+
return state;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
override async select(request: SessionModeSelectRequest): Promise<SessionModeSelectValue> {
|
|
57
|
+
return { mode: await this.modes.select(SessionId(request.sessionId), request.mode) };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
override async patch(request: SessionModePatchRequest): Promise<SessionModeActiveState> {
|
|
61
|
+
const state = await this.modes.setModePatch(SessionId(request.sessionId), request.patch);
|
|
62
|
+
if (state === undefined) throw missing(request.sessionId);
|
|
63
|
+
return state;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
override async applyConfig(
|
|
67
|
+
request: SessionModeApplyConfigRequest,
|
|
68
|
+
): Promise<SessionModeActiveState> {
|
|
69
|
+
const state = await this.modes.applyConfig(SessionId(request.sessionId), request.config);
|
|
70
|
+
if (state === undefined) throw missing(request.sessionId);
|
|
71
|
+
return state;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
override async saveConfig(
|
|
75
|
+
request: SessionModeSaveConfigRequest,
|
|
76
|
+
): Promise<SessionModeActiveState> {
|
|
77
|
+
const state = await this.modes.saveConfig(SessionId(request.sessionId), request.config);
|
|
78
|
+
if (state === undefined) throw missing(request.sessionId);
|
|
79
|
+
return state;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
override async saveAsDefault(request: SessionModeOfRequest): Promise<SessionModeActiveState> {
|
|
83
|
+
const state = await this.modes.saveAsDefault(SessionId(request.sessionId));
|
|
84
|
+
if (state === undefined) throw missing(request.sessionId);
|
|
85
|
+
return state;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
override async saveAs(request: SessionModeSaveAsRequest): Promise<SessionModeSaveAsValue> {
|
|
89
|
+
return {
|
|
90
|
+
mode: await this.modes.saveAsNewMode(
|
|
91
|
+
SessionId(request.sessionId),
|
|
92
|
+
request.code,
|
|
93
|
+
request.name,
|
|
94
|
+
),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
override async removeMode(request: SessionModeOfRequest): Promise<SessionModeSelectValue> {
|
|
99
|
+
return { mode: await this.modes.deleteMode(SessionId(request.sessionId)) };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
override async catalog(): Promise<SessionModeCatalogValue> {
|
|
103
|
+
const tools = new Set<string>();
|
|
104
|
+
const skills = new Set<string>();
|
|
105
|
+
for (const agent of this.directory.list()) {
|
|
106
|
+
for (const tool of this.scope.catalogOf(this.agentOf(agent))) tools.add(tool.name);
|
|
107
|
+
for (const skill of await this.scope.skillCatalog(this.agentOf(agent)))
|
|
108
|
+
skills.add(skill.name);
|
|
109
|
+
}
|
|
110
|
+
return { tools: [...tools], skills: [...skills] };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 目录里的条目就是 agent 本身(上游 `AgentRegistry.list()` 给实例)。
|
|
114
|
+
private agentOf(agent: Agent): Agent {
|
|
115
|
+
return agent;
|
|
116
|
+
}
|
|
117
|
+
}
|