@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
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// `sessionMode` 这一组的跨端契约:组名、方法名与请求 / 响应形状。
|
|
2
|
+
//
|
|
3
|
+
// 两端共用同一份声明:宿主侧实现它(`server/`),浏览器侧按消费它(`ui/` 的 `@RemoteClient()`)。
|
|
4
|
+
// **不继承 `RemoteClientBase`**:那个基类带 `inject(Remote)`,而 `Remote` 只有浏览器侧有。
|
|
5
|
+
import { RemoteGroup, RemoteMethod } from "@morlay/zephyr-app/client/remote";
|
|
6
|
+
import type { ModeConfig } from "./mode-config.ts";
|
|
7
|
+
import type {
|
|
8
|
+
SessionModeActiveState,
|
|
9
|
+
SessionModeCatalogValue,
|
|
10
|
+
SessionModePatch,
|
|
11
|
+
SessionModeRoster,
|
|
12
|
+
} from "./session-mode-shapes.ts";
|
|
13
|
+
|
|
14
|
+
// 某个会话的身份参数。
|
|
15
|
+
export interface SessionModeOfRequest {
|
|
16
|
+
readonly sessionId: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// 把某个空白会话切到某个模式。
|
|
20
|
+
export interface SessionModeSelectRequest {
|
|
21
|
+
readonly sessionId: string;
|
|
22
|
+
readonly mode: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface SessionModeSelectValue {
|
|
26
|
+
readonly mode: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 会话级字段覆盖(`null` = 回到模式默认)。
|
|
30
|
+
export interface SessionModePatchRequest {
|
|
31
|
+
readonly sessionId: string;
|
|
32
|
+
readonly patch: SessionModePatch;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 一次应用整份会话覆盖。
|
|
36
|
+
export interface SessionModeApplyConfigRequest {
|
|
37
|
+
readonly sessionId: string;
|
|
38
|
+
readonly config: ModeConfig;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 把草稿写回模式配置("保存为默认")。
|
|
42
|
+
export interface SessionModeSaveConfigRequest {
|
|
43
|
+
readonly sessionId: string;
|
|
44
|
+
readonly config: ModeConfig;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 以当前生效配置另存为一个新模式。
|
|
48
|
+
export interface SessionModeSaveAsRequest {
|
|
49
|
+
readonly sessionId: string;
|
|
50
|
+
readonly code: string;
|
|
51
|
+
readonly name?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface SessionModeSaveAsValue {
|
|
55
|
+
readonly mode: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// `sessionMode` 组:清单 / 生效态 / 切换 / 面板写面 / 全局候选。
|
|
59
|
+
// 每个方法的第一个形参就是请求对象(wire 上统一叫 `request`)。
|
|
60
|
+
@RemoteGroup({ name: "sessionMode" })
|
|
61
|
+
export class SessionModeEndpoint {
|
|
62
|
+
// 模式清单 + 默认模式。
|
|
63
|
+
@RemoteMethod()
|
|
64
|
+
async roster(): Promise<SessionModeRoster> {
|
|
65
|
+
throw new Error("sessionMode/roster:契约类未实现(宿主侧要在子类里实现它)");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 某个会话当下生效的那一份。
|
|
69
|
+
@RemoteMethod()
|
|
70
|
+
async active(_request: SessionModeOfRequest): Promise<SessionModeActiveState> {
|
|
71
|
+
throw new Error("sessionMode/active:契约类未实现(宿主侧要在子类里实现它)");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 把一个空白会话切到某个模式。
|
|
75
|
+
@RemoteMethod()
|
|
76
|
+
async select(_request: SessionModeSelectRequest): Promise<SessionModeSelectValue> {
|
|
77
|
+
throw new Error("sessionMode/select:契约类未实现(宿主侧要在子类里实现它)");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 改这个会话的字段覆盖。
|
|
81
|
+
@RemoteMethod()
|
|
82
|
+
async patch(_request: SessionModePatchRequest): Promise<SessionModeActiveState> {
|
|
83
|
+
throw new Error("sessionMode/patch:契约类未实现(宿主侧要在子类里实现它)");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 一次性应用整份会话覆盖。
|
|
87
|
+
@RemoteMethod()
|
|
88
|
+
async applyConfig(_request: SessionModeApplyConfigRequest): Promise<SessionModeActiveState> {
|
|
89
|
+
throw new Error("sessionMode/applyConfig:契约类未实现(宿主侧要在子类里实现它)");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 把草稿写回模式配置(对新会话生效)。
|
|
93
|
+
@RemoteMethod()
|
|
94
|
+
async saveConfig(_request: SessionModeSaveConfigRequest): Promise<SessionModeActiveState> {
|
|
95
|
+
throw new Error("sessionMode/saveConfig:契约类未实现(宿主侧要在子类里实现它)");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 把当前会话的覆盖写回这个模式。
|
|
99
|
+
@RemoteMethod()
|
|
100
|
+
async saveAsDefault(_request: SessionModeOfRequest): Promise<SessionModeActiveState> {
|
|
101
|
+
throw new Error("sessionMode/saveAsDefault:契约类未实现(宿主侧要在子类里实现它)");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// 以当前生效配置另存为一个新模式。
|
|
105
|
+
@RemoteMethod()
|
|
106
|
+
async saveAs(_request: SessionModeSaveAsRequest): Promise<SessionModeSaveAsValue> {
|
|
107
|
+
throw new Error("sessionMode/saveAs:契约类未实现(宿主侧要在子类里实现它)");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 删除一个"另存为"出来的模式;返回会话回到的那个模式。
|
|
111
|
+
@RemoteMethod()
|
|
112
|
+
async removeMode(_request: SessionModeOfRequest): Promise<SessionModeSelectValue> {
|
|
113
|
+
throw new Error("sessionMode/removeMode:契约类未实现(宿主侧要在子类里实现它)");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 全局候选:部署里当前可见的工具名与技能名。
|
|
117
|
+
@RemoteMethod()
|
|
118
|
+
async catalog(): Promise<SessionModeCatalogValue> {
|
|
119
|
+
throw new Error("sessionMode/catalog:契约类未实现(宿主侧要在子类里实现它)");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// `sessionMode` 这一组的两端共用形状:清单、生效态、全量目录与写面补丁。
|
|
2
|
+
import type { ModeConfig } from "./mode-config.ts";
|
|
3
|
+
|
|
4
|
+
// 模式可以改写的上游 policy 规则名(上游 waterfall 的事件名,不带前导斜杠)。名单封闭:写错的名字在装配期
|
|
5
|
+
// 就拒绝,而不是静默变成"没配"。
|
|
6
|
+
export const POLICY_NAMES = ["fs/write-intent", "fs/edit-intent", "fs/delete-intent"] as const;
|
|
7
|
+
|
|
8
|
+
export type PolicyName = (typeof POLICY_NAMES)[number];
|
|
9
|
+
|
|
10
|
+
// 三条 policy 的说明(面板与清单同源)。
|
|
11
|
+
export const POLICY_DESCRIPTIONS: Readonly<Record<PolicyName, string>> = {
|
|
12
|
+
"fs/write-intent": "文件写入前的意图检查(CAS 安全网)。",
|
|
13
|
+
"fs/edit-intent": "文件编辑前的意图检查(先读后改那条规则)。",
|
|
14
|
+
"fs/delete-intent": "文件删除前的意图检查。",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// 一个模式对外的那部分:选择器要的名字与说明。
|
|
18
|
+
export interface SessionModeRow {
|
|
19
|
+
readonly id: string;
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly description?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface SessionModeRoster {
|
|
25
|
+
readonly default: string;
|
|
26
|
+
readonly modes: readonly SessionModeRow[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type SessionModeSwitchKey = "instructions" | "skills" | "runtimeContext";
|
|
30
|
+
|
|
31
|
+
export type SessionModeTriState = "allow" | "deny";
|
|
32
|
+
|
|
33
|
+
export interface SessionModeDirectoryEntry {
|
|
34
|
+
readonly id: string;
|
|
35
|
+
readonly description: string;
|
|
36
|
+
readonly group?: string;
|
|
37
|
+
readonly groupLabel?: string;
|
|
38
|
+
// 模式默认下的判定(继承来的那档)。
|
|
39
|
+
readonly fallback?: SessionModeTriState | "default";
|
|
40
|
+
// 这个会话显式覆盖成的判定;`null` 表示跟着默认。
|
|
41
|
+
readonly override?: SessionModeTriState | null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface SessionModeDirectory {
|
|
45
|
+
readonly tools: readonly SessionModeDirectoryEntry[];
|
|
46
|
+
readonly skills: readonly SessionModeDirectoryEntry[];
|
|
47
|
+
readonly policies: readonly SessionModeDirectoryEntry[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 某个会话**当下生效**的一份(工具是运行期实际可见的集合)。
|
|
51
|
+
export interface SessionModeActiveState {
|
|
52
|
+
readonly mode: { readonly id: string; readonly name: string; readonly description: string };
|
|
53
|
+
readonly config: ModeConfig;
|
|
54
|
+
// 这个会话覆盖了哪些字段(**字段级**子集:没有的字段跟模式默认)。
|
|
55
|
+
readonly session: Readonly<Partial<ModeConfig>> | undefined;
|
|
56
|
+
readonly overriddenFields: readonly string[];
|
|
57
|
+
readonly catalog: SessionModeDirectory;
|
|
58
|
+
// 还能不能换模式(空白窗口):面板据此把选择器画成只读。
|
|
59
|
+
readonly editable: boolean;
|
|
60
|
+
// 只有"另存为"出来的模式允许删除。
|
|
61
|
+
readonly deletable: boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 面板的写面:`null` = 清掉这一项的会话覆盖、回到模式默认。
|
|
65
|
+
export type SessionModePatch = Readonly<Record<string, unknown>>;
|
|
66
|
+
|
|
67
|
+
// 全局候选(工具名 / 技能名):配置页与面板的名单字段用。
|
|
68
|
+
export interface SessionModeCatalogValue {
|
|
69
|
+
readonly tools: readonly string[];
|
|
70
|
+
readonly skills: readonly string[];
|
|
71
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// 名单里引用整组时的写法:MCP 组直接用 `mcp:<server>`,其余加 `group:` 前缀。
|
|
2
|
+
//
|
|
3
|
+
// 纯字符串处理、两端共用(宿主半用它收口名单,浏览器半用它渲染面板的组引用),所以放 `endpoint/`:
|
|
4
|
+
// 浏览器半**不引宿主半**,那两个方向只有这条出口。
|
|
5
|
+
export function groupRef(group: string): string {
|
|
6
|
+
return group.startsWith("mcp:") ? group : `group:${group}`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function refGroup(ref: string): string {
|
|
10
|
+
return ref.startsWith("group:") ? ref.slice("group:".length) : ref;
|
|
11
|
+
}
|
package/src/events.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// 本包订阅的上游事件:名字、参数表、返回类型都收在类里(拼错名字或参数表在编译期就报)。
|
|
2
|
+
// 与上游同名即可互通——上游用 `ctx.emit("agent/created", …)` 派发,我们在这里用同一套名字订阅。
|
|
3
|
+
//
|
|
4
|
+
// 声明成 Provider 的**字段**(`readonly assemble$ = new SystemPromptAssemble()`):框架在填充注入后对每个
|
|
5
|
+
// `EventEmitter` 字段调 `onInit(ctx)`,不需要额外的接线。
|
|
6
|
+
import type { Agent } from "@morlay/dsh-agent/contract";
|
|
7
|
+
import type { LlmFailure, MessageSource, ModelSelection } from "@morlay/dsh-agent/contract";
|
|
8
|
+
import type { ResolvedRetryPolicy } from "@morlay/dsh-agent/contract";
|
|
9
|
+
import type { UserMessage } from "@morlay/dsh-agent/contract";
|
|
10
|
+
import type {
|
|
11
|
+
AssembleContext,
|
|
12
|
+
AssembledContext,
|
|
13
|
+
AssembledSection,
|
|
14
|
+
} from "@morlay/dsh-agent/contract";
|
|
15
|
+
import { EventEmitter } from "@morlay/zephyr";
|
|
16
|
+
|
|
17
|
+
// 一次装配的呈现面(`system-prompt/assemble` 的入参 / 出参)。
|
|
18
|
+
export interface PromptPresentation {
|
|
19
|
+
readonly sections: AssembledSection[];
|
|
20
|
+
readonly contexts: AssembledContext[];
|
|
21
|
+
readonly variables: Record<string, string | undefined>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 一步的前置决定:`enter` 进这一步,`reject` 拒绝。
|
|
25
|
+
export type PreStepDecision =
|
|
26
|
+
| { readonly kind: "enter"; readonly messages: UserMessage[] }
|
|
27
|
+
| { readonly kind: "reject"; readonly reason?: string };
|
|
28
|
+
|
|
29
|
+
// 一次失败的模型请求:`retry` 表示接管重试。
|
|
30
|
+
export type RequestErrorAction = { readonly kind: "retry" } | undefined;
|
|
31
|
+
|
|
32
|
+
// 会话建好了(晚于它的 preset 行挂载,早于第一次装配)。
|
|
33
|
+
export class AgentCreated extends EventEmitter<[{ readonly agent: Agent }]> {
|
|
34
|
+
constructor() {
|
|
35
|
+
super("agent/created");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 工具注册表变了(全局或某个会话的作用域):汉化要跟着改新注册的那些定义。
|
|
40
|
+
export class ToolsChanged extends EventEmitter {
|
|
41
|
+
constructor() {
|
|
42
|
+
super("tools/change");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 一步装配系统提示词(瀑布):监听者拿到 `next`。
|
|
47
|
+
export class SystemPromptAssemble extends EventEmitter<
|
|
48
|
+
[PromptPresentation, AssembleContext],
|
|
49
|
+
Promise<PromptPresentation>
|
|
50
|
+
> {
|
|
51
|
+
constructor() {
|
|
52
|
+
super("system-prompt/assemble");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 一步开始前的消息表(瀑布):官方那几行的每步注入都在这里。
|
|
57
|
+
//
|
|
58
|
+
// 六个字段与上游 `Events['agent/pre-step']` 的 payload 一一对应(少一个,消费方就只能在类型外绕道)。
|
|
59
|
+
export interface PreStepPayload {
|
|
60
|
+
readonly agent: Agent;
|
|
61
|
+
readonly messages: UserMessage[];
|
|
62
|
+
readonly turn: number;
|
|
63
|
+
readonly step: number;
|
|
64
|
+
readonly signal: AbortSignal;
|
|
65
|
+
/** 这一步捕获的模型选择:重试沿用同一个值,省略推理强度时走适配器默认。 */
|
|
66
|
+
readonly modelSelection: Readonly<ModelSelection>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export class AgentPreStep extends EventEmitter<[PreStepPayload], Promise<PreStepDecision>> {
|
|
70
|
+
constructor() {
|
|
71
|
+
super("agent/pre-step");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 一次模型请求失败(瀑布):返回 `retry` 表示接管重试。
|
|
76
|
+
//
|
|
77
|
+
// 七个字段与上游 `Events['agent/request-error']` 的 payload 一一对应:失败本身(`provider` / `failure`)、
|
|
78
|
+
// 它发生在哪一步(`turn` / `step`)、以及当时生效的重试策略——判定"这一次该不该接管"要用到它们。
|
|
79
|
+
export interface RequestErrorPayload {
|
|
80
|
+
readonly agent: Agent;
|
|
81
|
+
readonly turn: number;
|
|
82
|
+
readonly step: number;
|
|
83
|
+
readonly provider: string;
|
|
84
|
+
readonly failure: LlmFailure;
|
|
85
|
+
readonly retryPolicy: ResolvedRetryPolicy | undefined;
|
|
86
|
+
readonly signal: AbortSignal;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export class AgentRequestError extends EventEmitter<
|
|
90
|
+
[RequestErrorPayload],
|
|
91
|
+
Promise<RequestErrorAction>
|
|
92
|
+
> {
|
|
93
|
+
constructor() {
|
|
94
|
+
super("agent/request-error");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 一条消息来源的 kind(`skill-catalog` 这些由各自的行声明,按字符串读)。
|
|
99
|
+
export function sourceKindOf(source: MessageSource): string | undefined {
|
|
100
|
+
const kind = (source as { readonly kind?: unknown } | undefined)?.kind;
|
|
101
|
+
return typeof kind === "string" ? kind : undefined;
|
|
102
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// 工具组:数据在 `packs/*.ts`,这份文件只做汇总与渲染。
|
|
2
|
+
//
|
|
3
|
+
// 组只决定"用法说明怎么分批送达":`injection: "init"` 常驻注入,`on-demand` 由 `describe_tool({ group })` 按需返回。
|
|
4
|
+
import { shortDescriptionsOf, TOOL_GROUPS as GROUPS } from "./packs/index.ts";
|
|
5
|
+
import type { ToolGroup } from "./types.ts";
|
|
6
|
+
|
|
7
|
+
export type { GroupLine, ToolGroup, ToolGuidance } from "./types.ts";
|
|
8
|
+
// 组表的重名检查(装配期 fail loud):数据在 `packs/**`,判据由它自己给。
|
|
9
|
+
export { assertGroupsUnique } from "./packs/index.ts";
|
|
10
|
+
|
|
11
|
+
export const TOOL_GROUPS: readonly ToolGroup[] = GROUPS;
|
|
12
|
+
|
|
13
|
+
// 工具名 → 一行中文:由各组的数据汇总(重名在 `assertGroupsUnique` 里抛错)。
|
|
14
|
+
export const SHORT_TOOL_DESCRIPTIONS: Readonly<Record<string, string>> =
|
|
15
|
+
shortDescriptionsOf(TOOL_GROUPS);
|
|
16
|
+
|
|
17
|
+
export function groupByKey(key: string): ToolGroup {
|
|
18
|
+
const group = TOOL_GROUPS.find((entry) => entry.key === key);
|
|
19
|
+
if (group === undefined) throw new Error(`tool-guidance: unknown group "${key}"`);
|
|
20
|
+
return group;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 某个组的正文:markdown 列表,一行一条。`visible` 是"这一行依赖的工具在不在"的判据。
|
|
24
|
+
export function groupSkillBody(
|
|
25
|
+
group: ToolGroup,
|
|
26
|
+
visible: (tool: string) => boolean = () => true,
|
|
27
|
+
): string {
|
|
28
|
+
return group.lines
|
|
29
|
+
.filter((line) => {
|
|
30
|
+
const when = line.when;
|
|
31
|
+
if (when === undefined) return true;
|
|
32
|
+
return typeof when === "string" ? visible(when) : when.some(visible);
|
|
33
|
+
})
|
|
34
|
+
.map((line) => `- ${line.text}`)
|
|
35
|
+
.join("\n");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 工具名 → 组 key(工具组是这份表的唯一 home)。
|
|
39
|
+
export function groupOfTool(name: string): string | undefined {
|
|
40
|
+
return TOOL_GROUPS.find((group) => group.tools.some((entry) => entry.tool === name))?.key;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 组 key → 完整描述(`describe_tool` 按组返回的摘要与正文就是它)。
|
|
44
|
+
//
|
|
45
|
+
// 动态组(`mcp:<server>`)回落到 `mcp` 那一份:每个 server 的工具不同,但「怎么读资源」是同一件事。
|
|
46
|
+
export interface GroupDescriptor {
|
|
47
|
+
readonly title: string;
|
|
48
|
+
// 什么时候该看这一组(`describe_tool` 不带参数时逐行渲染它)。
|
|
49
|
+
readonly guideline: string;
|
|
50
|
+
// 用法正文(带参数时给)。
|
|
51
|
+
readonly body: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function groupDescriptorOf(
|
|
55
|
+
key: string,
|
|
56
|
+
visible: (tool: string) => boolean = () => true,
|
|
57
|
+
): GroupDescriptor | undefined {
|
|
58
|
+
const isMcp = key.startsWith("mcp:");
|
|
59
|
+
const group = TOOL_GROUPS.find((candidate) => candidate.key === (isMcp ? "mcp" : key));
|
|
60
|
+
if (group === undefined) return undefined;
|
|
61
|
+
return {
|
|
62
|
+
// 动态组的标题要带上 server 名,否则清单里一排 "MCP 资源" 分不清是哪个 server。
|
|
63
|
+
title: isMcp ? `MCP · ${key.slice("mcp:".length)}` : group.title,
|
|
64
|
+
guideline: group.guideline,
|
|
65
|
+
// **按当前可见工具过滤**:组里列了一个这个会话根本没有的工具(上游改名或部署没装那一行),
|
|
66
|
+
// 它的用法行就不该注入——否则模型会照着说明去调一个不存在的工具。
|
|
67
|
+
body: groupSkillBody(group, visible),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 组 key → 显示名。
|
|
72
|
+
export function groupTitleOf(key: string): string | undefined {
|
|
73
|
+
return TOOL_GROUPS.find((group) => group.key === key)?.title;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 工具成员在 d.ts 里的最小形状:调用签名 + 可选的一行说明。
|
|
77
|
+
export interface ToolSignature {
|
|
78
|
+
readonly name: string;
|
|
79
|
+
readonly args: string;
|
|
80
|
+
readonly output: string;
|
|
81
|
+
readonly doc?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 成员容器的名字:PTC 骨架与组签名都写它,模型看到的才是同一个 `tools`。
|
|
85
|
+
export const TOOLS_SDK_INTERFACE = "ToolsSdk";
|
|
86
|
+
|
|
87
|
+
// 工具名 → `tools` 上的成员键:合法标识符裸写,其余(MCP 的 `-`)加引号。
|
|
88
|
+
export function renderToolKey(name: string): string {
|
|
89
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/u.test(name) ? name : JSON.stringify(name);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 一组成员的调用签名块:键能裸写就裸写,不能的加引号(`tools["名字"](args)` 同样成立)。
|
|
93
|
+
export function renderToolsDeclaration(signatures: readonly ToolSignature[]): string {
|
|
94
|
+
const members = signatures.flatMap((signature) => [
|
|
95
|
+
...(signature.doc === undefined ? [] : [` /** ${signature.doc} */`]),
|
|
96
|
+
` ${renderToolKey(signature.name)}(args: ${signature.args}): Promise<${signature.output}>`,
|
|
97
|
+
]);
|
|
98
|
+
return ["declare global {", ` interface ${TOOLS_SDK_INTERFACE} {`, ...members, " }", "}"].join(
|
|
99
|
+
"\n",
|
|
100
|
+
);
|
|
101
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// 与用户交互。
|
|
2
|
+
import type { ToolGroup } from "../types.ts";
|
|
3
|
+
|
|
4
|
+
export const ASK_GROUP: ToolGroup = {
|
|
5
|
+
key: "ask",
|
|
6
|
+
title: "问答",
|
|
7
|
+
injection: "init",
|
|
8
|
+
guideline: "要向用户提一个必须由人定夺的问题(而不是自己猜)时用它。",
|
|
9
|
+
lines: [
|
|
10
|
+
{
|
|
11
|
+
when: "ask_user_question",
|
|
12
|
+
text: "ask_user_question:需要用户定夺时必须通过该工具询问,不要自己猜。",
|
|
13
|
+
},
|
|
14
|
+
],
|
|
15
|
+
drops: [],
|
|
16
|
+
tools: [{ tool: "ask_user_question", short: "向用户提问并等待回答" }],
|
|
17
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// 代码执行通道:PTC 模式下用程序调用其它工具。
|
|
2
|
+
import type { ToolGroup } from "../types.ts";
|
|
3
|
+
|
|
4
|
+
export const CODE_GROUP: ToolGroup = {
|
|
5
|
+
key: "code",
|
|
6
|
+
title: "代码执行",
|
|
7
|
+
injection: "init",
|
|
8
|
+
guideline: "PTC:在程序里直接编排工具调用时用它。",
|
|
9
|
+
lines: [
|
|
10
|
+
{
|
|
11
|
+
when: "present",
|
|
12
|
+
text: "present:要单独访问的完整交付物(图片、Office 文档、表格、幻灯片)用它发布;已经内联展示过的结果不必重复。",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
when: "read",
|
|
16
|
+
text: "文件引用:命令、配置表达式与代码块之外,提到已存在的文件就链接到它相对工作目录(或绝对路径)的完整路径;已知行号时加 #L24 或 #L24-L30;链接文本用文件名或清晰别名。",
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
when: "run_code",
|
|
20
|
+
text: "run_code:PTC 模式下唯一能直接调用的工具——别的工具都在程序里 `await tools.name(args)` 调;code 是 async 函数体,只把打印或返回的内容当输出。",
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
drops: ["tools:ptc-only", "ui:deliverable-file-references", "context:file-reference"],
|
|
24
|
+
tools: [{ tool: "run_code", short: "用代码调用其它工具(PTC)" }],
|
|
25
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// 文件与目录:读写搜 + 工作目录。
|
|
2
|
+
import type { ToolGroup } from "../types.ts";
|
|
3
|
+
|
|
4
|
+
export const FS_GROUP: ToolGroup = {
|
|
5
|
+
key: "fs",
|
|
6
|
+
title: "文件",
|
|
7
|
+
injection: "init",
|
|
8
|
+
guideline: "读、写、改文件,或按文件名 / 内容检索时用它。",
|
|
9
|
+
lines: [
|
|
10
|
+
{
|
|
11
|
+
when: "read",
|
|
12
|
+
text: "read:读文本文件(严禁使用 sed/cat 之类的 shell 命令);大文件用 offset/limit 续读。",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
when: "write",
|
|
16
|
+
text: "write:创建文件或整体覆盖;已存在的文件先 read 再改,局部改动优先 edit。",
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
when: "edit",
|
|
20
|
+
text: "edit:按精确匹配替换;old_string 默认必须唯一,出现多次时给更长的上下文或 replace_all。",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
when: "apply_patch",
|
|
24
|
+
text: "apply_patch:跨文件或分开的几处一起改时用它(Codex 风格补丁:`*** Begin Patch` … `*** End Patch`);整份先校验、拒绝则什么都不改,开始应用后按序执行、遇错即停且不回滚——重试前先重读受影响的文件。",
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
when: "str_replace_editor",
|
|
28
|
+
text: "str_replace_editor:path 必须绝对路径、目录只能 view;str_replace 的 old_str 必须逐字且唯一(不唯一就不改),删除匹配要省略 new_str 而不是传 null。",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
when: "glob",
|
|
32
|
+
text: "glob:按路径模式找文件(不要用 shell find);不含斜杠的模式匹配任意深度的文件名,结果只有文件。",
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
when: "grep",
|
|
36
|
+
text: "grep:按内容搜索(不要用 shell grep / rg);需要上下文再 read 命中的文件。",
|
|
37
|
+
},
|
|
38
|
+
{ when: "read_image", text: "read_image:看图。" },
|
|
39
|
+
],
|
|
40
|
+
drops: [
|
|
41
|
+
"tool:read",
|
|
42
|
+
"tool:write",
|
|
43
|
+
"tool:edit",
|
|
44
|
+
"tool:apply_patch",
|
|
45
|
+
"tool:glob",
|
|
46
|
+
"tool:grep",
|
|
47
|
+
"tool:jobs",
|
|
48
|
+
"tool:pty",
|
|
49
|
+
],
|
|
50
|
+
tools: [
|
|
51
|
+
{ tool: "read", short: "读文件内容(可指定行范围)" },
|
|
52
|
+
{ tool: "write", short: "写入文件或整文件覆盖" },
|
|
53
|
+
{ tool: "edit", short: "按精确匹配替换文件片段" },
|
|
54
|
+
{ tool: "apply_patch", short: "用一个补丁批量增删改移多个文件" },
|
|
55
|
+
{ tool: "glob", short: "按模式查找文件路径" },
|
|
56
|
+
{ tool: "grep", short: "按正则搜索文件内容" },
|
|
57
|
+
{ tool: "read_image", short: "读取图片内容" },
|
|
58
|
+
{ tool: "str_replace_editor", short: "查看 / 创建 / 精确替换文件" },
|
|
59
|
+
],
|
|
60
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// 组索引:**加一个工具组 = 加一份文件 + 在这里登一行**。key 与工具名都做重名检查,撞了就 fail loud。
|
|
2
|
+
import type { ToolGroup } from "../types.ts";
|
|
3
|
+
import { ASK_GROUP } from "./ask.ts";
|
|
4
|
+
import { CODE_GROUP } from "./code.ts";
|
|
5
|
+
import { FS_GROUP } from "./fs.ts";
|
|
6
|
+
import { MCP_GROUP } from "./mcp.ts";
|
|
7
|
+
import { PLAN_GROUP } from "./plan.ts";
|
|
8
|
+
import { SHELL_GROUP } from "./shell.ts";
|
|
9
|
+
import { SKILL_GROUP } from "./skill.ts";
|
|
10
|
+
import { SWARM_GROUP } from "./swarm.ts";
|
|
11
|
+
import { WEB_ACCESS_GROUP } from "./web-access.ts";
|
|
12
|
+
|
|
13
|
+
export type { GroupLine, ToolGroup, ToolGuidance } from "../types.ts";
|
|
14
|
+
|
|
15
|
+
// 全部工具组(顺序只影响报错信息里的排列)。
|
|
16
|
+
export const TOOL_GROUPS: readonly ToolGroup[] = [
|
|
17
|
+
FS_GROUP,
|
|
18
|
+
SHELL_GROUP,
|
|
19
|
+
WEB_ACCESS_GROUP,
|
|
20
|
+
CODE_GROUP,
|
|
21
|
+
ASK_GROUP,
|
|
22
|
+
SKILL_GROUP,
|
|
23
|
+
PLAN_GROUP,
|
|
24
|
+
SWARM_GROUP,
|
|
25
|
+
MCP_GROUP,
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
// 重名检查:组 key 唯一、工具名跨组唯一。
|
|
29
|
+
export function assertGroupsUnique(groups: readonly ToolGroup[] = TOOL_GROUPS): void {
|
|
30
|
+
const keys = new Set<string>();
|
|
31
|
+
const tools = new Map<string, string>();
|
|
32
|
+
for (const group of groups) {
|
|
33
|
+
if (keys.has(group.key)) throw new Error(`tool-guidance: duplicate group key "${group.key}"`);
|
|
34
|
+
keys.add(group.key);
|
|
35
|
+
for (const { tool } of group.tools) {
|
|
36
|
+
const owner = tools.get(tool);
|
|
37
|
+
if (owner !== undefined && owner !== group.key) {
|
|
38
|
+
throw new Error(`tool-guidance: tool "${tool}" appears in "${owner}" and "${group.key}"`);
|
|
39
|
+
}
|
|
40
|
+
tools.set(tool, group.key);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 工具名 → 一行中文。
|
|
46
|
+
export function shortDescriptionsOf(
|
|
47
|
+
groups: readonly ToolGroup[] = TOOL_GROUPS,
|
|
48
|
+
): Readonly<Record<string, string>> {
|
|
49
|
+
return Object.fromEntries(
|
|
50
|
+
groups.flatMap((group) => group.tools.map(({ tool, short }) => [tool, short])),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// MCP 资源:只有 server 宣告了 resources 才有这三件。
|
|
2
|
+
import type { ToolGroup } from "../types.ts";
|
|
3
|
+
|
|
4
|
+
export const MCP_GROUP: ToolGroup = {
|
|
5
|
+
key: "mcp",
|
|
6
|
+
title: "MCP 资源",
|
|
7
|
+
injection: "on-demand",
|
|
8
|
+
guideline: "要读某个 MCP server 暴露的只读资源(文档 / 日志 / 数据)时加载它。",
|
|
9
|
+
lines: [
|
|
10
|
+
{
|
|
11
|
+
when: ["list_mcp_resources", "list_mcp_resource_templates"],
|
|
12
|
+
text: "先 list 再 read:`list_mcp_resources` 给静态资源,`list_mcp_resource_templates` 给带参数的模板(如 `logs://{date}`)。",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
when: "read_mcp_resource",
|
|
16
|
+
text: "`read_mcp_resource` 按 URI 取内容;server 没有宣告 resources 时列表为空,不要反复重试。",
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
drops: ["mcp-resource-servers"],
|
|
20
|
+
tools: [
|
|
21
|
+
{ tool: "list_mcp_resources", short: "列出某个 MCP server 暴露的资源" },
|
|
22
|
+
{ tool: "list_mcp_resource_templates", short: "列出某个 MCP server 的资源模板" },
|
|
23
|
+
{ tool: "read_mcp_resource", short: "按 URI 读取 MCP 资源内容" },
|
|
24
|
+
],
|
|
25
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// 流程与交付:todo / 计划 / goal / 交付物。
|
|
2
|
+
import type { ToolGroup } from "../types.ts";
|
|
3
|
+
|
|
4
|
+
export const PLAN_GROUP: ToolGroup = {
|
|
5
|
+
key: "plan",
|
|
6
|
+
title: "流程",
|
|
7
|
+
injection: "on-demand",
|
|
8
|
+
guideline: "要在会话里跟踪待办与目标、安排定时提醒、或声明交付物时加载它。",
|
|
9
|
+
lines: [
|
|
10
|
+
{
|
|
11
|
+
when: "todo_write",
|
|
12
|
+
text: "todo_write:多步任务先建清单,并随着进展更新状态。",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
when: ["create_goal", "get_goal", "update_goal"],
|
|
16
|
+
text: "create_goal:长任务跟踪目标进展(配 get_goal 看、update_goal 改)。",
|
|
17
|
+
},
|
|
18
|
+
{ when: "present", text: "present:要给用户看的产物,用它声明出来。" },
|
|
19
|
+
{
|
|
20
|
+
when: "exit_plan_mode",
|
|
21
|
+
text: "exit_plan_mode:动手前提交计划(计划模式下只读);本部署装了计划模式才有这一行。",
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
when: ["schedule_create", "schedule_list", "schedule_view", "schedule_edit"],
|
|
25
|
+
text: "定时任务:schedule_create 用 rrule(一行 DTSTART + 一行 RRULE)与 target 指定投递去处,prompt 每次触发时作为用户消息送达;schedule_list 列 Host 全部任务(含别的会话建的,分页),schedule_view 看单个,schedule_edit 按 action 改 / 暂停 / 恢复 / 删除(带 expectedUpdatedAt 才防并发覆盖,删除不撤回已入队的消息)。",
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
drops: ["tool:goal", "tool:schedule"],
|
|
29
|
+
tools: [
|
|
30
|
+
{ tool: "todo_write", short: "维护多步任务的待办清单" },
|
|
31
|
+
{ tool: "exit_plan_mode", short: "提交计划并请求批准" },
|
|
32
|
+
{ tool: "get_goal", short: "查看当前会话目标" },
|
|
33
|
+
{ tool: "create_goal", short: "创建会话目标" },
|
|
34
|
+
{ tool: "update_goal", short: "更新会话目标状态" },
|
|
35
|
+
{ tool: "present", short: "声明交付给用户的产物" },
|
|
36
|
+
{ tool: "schedule_create", short: "按 rrule 建定时任务" },
|
|
37
|
+
{ tool: "schedule_list", short: "列出全部定时任务" },
|
|
38
|
+
{ tool: "schedule_view", short: "按 id 查看一个定时任务" },
|
|
39
|
+
{ tool: "schedule_edit", short: "按 id 改 / 暂停 / 删除任务" },
|
|
40
|
+
{ tool: "plugin_manager", short: "管理本 profile 的插件与 bundle" },
|
|
41
|
+
],
|
|
42
|
+
};
|