hob-bot 0.0.0-stage → 1.0.2
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 +111 -2
- package/README.zh-CN.md +112 -0
- package/bin/hob.mjs +7 -0
- package/dist/web/assets/index-D88l35PH.css +1 -0
- package/dist/web/assets/index-DUs8nN3E.js +218 -0
- package/dist/web/favicon.ico +0 -0
- package/dist/web/fonts/inter-latin.woff2 +0 -0
- package/dist/web/fonts/jetbrains-mono-latin.woff2 +0 -0
- package/dist/web/icons/apple-touch-icon-180.png +0 -0
- package/dist/web/icons/favicon-32.png +0 -0
- package/dist/web/icons/icon-192.png +0 -0
- package/dist/web/icons/icon-512.png +0 -0
- package/dist/web/icons/icon-maskable-512.png +0 -0
- package/dist/web/index.html +32 -0
- package/dist/web/manifest.webmanifest +16 -0
- package/dist/web/sw.js +131 -0
- package/package.json +62 -4
- package/server/src/actions.ts +230 -0
- package/server/src/boot.ts +85 -0
- package/server/src/bus.ts +21 -0
- package/server/src/config.ts +123 -0
- package/server/src/cron-text.ts +49 -0
- package/server/src/delegations.ts +113 -0
- package/server/src/files.ts +142 -0
- package/server/src/http.ts +802 -0
- package/server/src/ids.ts +19 -0
- package/server/src/image.ts +218 -0
- package/server/src/imports.ts +302 -0
- package/server/src/jobs.ts +730 -0
- package/server/src/main.ts +69 -0
- package/server/src/memory.ts +272 -0
- package/server/src/models.ts +358 -0
- package/server/src/overview.ts +116 -0
- package/server/src/pages.ts +54 -0
- package/server/src/password.ts +23 -0
- package/server/src/pi.ts +273 -0
- package/server/src/prompt.ts +227 -0
- package/server/src/providers.ts +532 -0
- package/server/src/push.ts +285 -0
- package/server/src/router-tools.ts +549 -0
- package/server/src/router.ts +841 -0
- package/server/src/routines.ts +129 -0
- package/server/src/runtime.ts +104 -0
- package/server/src/search.ts +91 -0
- package/server/src/secrets.ts +46 -0
- package/server/src/settings.ts +452 -0
- package/server/src/skills.ts +150 -0
- package/server/src/store.ts +324 -0
- package/server/src/text.ts +14 -0
- package/server/src/time.ts +42 -0
- package/server/src/transcript.ts +272 -0
- package/server/src/worker-tools.ts +129 -0
- package/shared/command.ts +42 -0
- package/shared/files.ts +141 -0
- package/shared/media.ts +17 -0
- package/shared/model-note.ts +32 -0
- package/shared/page-shell.ts +108 -0
- package/shared/search.ts +19 -0
- package/shared/skills.ts +37 -0
- package/shared/sprites.ts +422 -0
- package/shared/thinking.ts +118 -0
- package/shared/types.ts +631 -0
- package/vendor/lore-pi-extension/README.md +28 -0
- package/vendor/lore-pi-extension/api.ts +140 -0
- package/vendor/lore-pi-extension/formatters.ts +204 -0
- package/vendor/lore-pi-extension/hooks.ts +195 -0
- package/vendor/lore-pi-extension/index.ts +25 -0
- package/vendor/lore-pi-extension/package.json +23 -0
- package/vendor/lore-pi-extension/skills.ts +447 -0
- package/vendor/lore-pi-extension/tools.ts +344 -0
- package/vendor/lore-pi-extension/uri.ts +67 -0
- package/vendor/lore-pi-extension/vendor/skill-workcopy/index.d.mts +266 -0
- package/vendor/lore-pi-extension/vendor/skill-workcopy/index.mjs +1328 -0
- package/vendor/lore-pi-extension/vendor/skill-workcopy/skills-sync.d.mts +33 -0
- package/vendor/lore-pi-extension/vendor/skill-workcopy/skills-sync.mjs +202 -0
package/server/src/pi.ts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createAgentSession,
|
|
3
|
+
createExtensionRuntime,
|
|
4
|
+
DefaultResourceLoader,
|
|
5
|
+
ModelRuntime,
|
|
6
|
+
SessionManager,
|
|
7
|
+
SettingsManager,
|
|
8
|
+
type LoadExtensionsResult,
|
|
9
|
+
type ResourceLoader,
|
|
10
|
+
} from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import type { Model, Provider } from "@earendil-works/pi-ai";
|
|
12
|
+
import type { AgentSession, Skill, ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import { mkdirSync, readFileSync } from "node:fs";
|
|
14
|
+
import { createRequire } from "node:module";
|
|
15
|
+
import { dirname, join } from "node:path";
|
|
16
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
17
|
+
import type { MemoryProvider } from "../../shared/types.ts";
|
|
18
|
+
import type { ThinkingLevel } from "./config.ts";
|
|
19
|
+
import { MEMORY_TOOL_PATTERNS } from "./memory.ts";
|
|
20
|
+
|
|
21
|
+
export interface PiHostOptions {
|
|
22
|
+
/** Hob 自己的引擎目录(凭据、资源、记忆),通常在 HOB_HOME/engine */
|
|
23
|
+
engineDir: string;
|
|
24
|
+
/** 引擎的模型配置(内置服务商的思考档写成 modelOverrides);缺省 HOB_HOME/engine/models.json */
|
|
25
|
+
modelsPath?: string | null;
|
|
26
|
+
/** 测试注入的假 provider */
|
|
27
|
+
nativeProviders?: Provider[];
|
|
28
|
+
/** 服务商套进引擎(设 Key、注册兼容接口),引擎建好、拿到之前调一次 */
|
|
29
|
+
configure?: (runtime: ModelRuntime) => Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface OpenSessionOptions {
|
|
33
|
+
file: string;
|
|
34
|
+
cwd: string;
|
|
35
|
+
model: string;
|
|
36
|
+
thinking: ThinkingLevel;
|
|
37
|
+
systemPrompt: () => string;
|
|
38
|
+
tools: string[];
|
|
39
|
+
customTools: ToolDefinition<any, any>[];
|
|
40
|
+
/** 这一轮用哪个记忆提供者:决定加载哪个捆绑插件(off = 两个都不加载) */
|
|
41
|
+
memory?: MemoryProvider;
|
|
42
|
+
/** 任务使用的技能;对话会话不加载。 */
|
|
43
|
+
skills?: Skill[];
|
|
44
|
+
disabledSkills?: string[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface OpenedSession {
|
|
48
|
+
session: AgentSession;
|
|
49
|
+
manager: SessionManager;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* pi 的 AgentSession 在构建时就把系统提示词算进缓存,之后 ResourceLoader 现读 store 也用不上,
|
|
54
|
+
* 所以要每轮开始前调一次:重设一遍当前工具集会让它按 loader 重算提示词,下一轮生效。
|
|
55
|
+
*/
|
|
56
|
+
export function refreshSystemPrompt(session: AgentSession): void {
|
|
57
|
+
session.setActiveToolsByName(session.getActiveToolNames());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const requireFromHere = createRequire(import.meta.url);
|
|
61
|
+
|
|
62
|
+
/** 捆绑的记忆插件包名(pi-memory 在 package.json 里钉死版本)。 */
|
|
63
|
+
const MEMORY_PACKAGES = { local: "pi-memory", lore: "@local/lore-pi-extension" } as const;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 不在 npm 上的插件:源码(固定版本)直接放在 vendor/ 下随包发布,按路径加载。
|
|
67
|
+
* 不用 bundleDependencies:npm 全局安装带捆绑依赖的包会去编译 macOS 才有的 fsevents,直接装失败。
|
|
68
|
+
*/
|
|
69
|
+
const VENDORED_PLUGINS: Record<string, string> = {
|
|
70
|
+
"@local/lore-pi-extension": fileURLToPath(new URL("../../vendor/lore-pi-extension/package.json", import.meta.url)),
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** 每个插件注册的工具都属于自己那一家:用来做互斥断言。 */
|
|
74
|
+
const PI_MEMORY_TOOLS = /^memory_(?:write|read|forget|restore|search|status)$|^scratchpad$/;
|
|
75
|
+
|
|
76
|
+
/** 捆绑插件的入口文件(包 manifest 里 `pi.extensions` 的第一项)。 */
|
|
77
|
+
export function bundledPluginEntry(packageName: string): string {
|
|
78
|
+
const manifest = VENDORED_PLUGINS[packageName] ?? requireFromHere.resolve(`${packageName}/package.json`);
|
|
79
|
+
const pkg = JSON.parse(readFileSync(manifest, "utf8")) as { pi?: { extensions?: unknown } };
|
|
80
|
+
const declared = Array.isArray(pkg.pi?.extensions) ? pkg.pi?.extensions[0] : undefined;
|
|
81
|
+
const entry = typeof declared === "string" && declared ? declared : "./index.ts";
|
|
82
|
+
return join(dirname(manifest), entry);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function emptyLoader(systemPrompt: () => string): ResourceLoader {
|
|
86
|
+
return {
|
|
87
|
+
getExtensions: () => ({ extensions: [], errors: [], runtime: createExtensionRuntime() }),
|
|
88
|
+
getSkills: () => ({ skills: [], diagnostics: [] }),
|
|
89
|
+
getPrompts: () => ({ prompts: [], diagnostics: [] }),
|
|
90
|
+
getThemes: () => ({ themes: [], diagnostics: [] }),
|
|
91
|
+
getAgentsFiles: () => ({ agentsFiles: [] }),
|
|
92
|
+
// 每轮现读 store:换了规矩/记忆下一轮就生效
|
|
93
|
+
getSystemPrompt: () => systemPrompt(),
|
|
94
|
+
getSystemPromptSource: () => undefined,
|
|
95
|
+
getAppendSystemPrompt: () => [],
|
|
96
|
+
getAppendSystemPromptSources: () => [],
|
|
97
|
+
extendResources: () => {},
|
|
98
|
+
reload: async () => {},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 记忆插件互斥断言 + 本地记忆的工具修剪。
|
|
104
|
+
*
|
|
105
|
+
* 一次只加载一家的插件:万一哪天两条路径同时挂上,这里立刻报错,而不是把用户的记忆
|
|
106
|
+
* 分裂成两份。pi-memory 没带 qmd,`memory_search` 只会报错,所以在注册前就去掉。
|
|
107
|
+
*/
|
|
108
|
+
export function filterMemoryExtensions(
|
|
109
|
+
result: LoadExtensionsResult,
|
|
110
|
+
provider: MemoryProvider,
|
|
111
|
+
): LoadExtensionsResult {
|
|
112
|
+
if (result.extensions.length > 1) throw new Error("记忆插件互斥:一次只能加载一个记忆插件。");
|
|
113
|
+
const families = new Set<MemoryProvider>();
|
|
114
|
+
for (const extension of result.extensions) {
|
|
115
|
+
for (const name of extension.tools?.keys() ?? []) {
|
|
116
|
+
if (PI_MEMORY_TOOLS.test(name)) families.add("local");
|
|
117
|
+
else if (name.startsWith("lore_")) families.add("lore");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (families.size > 1) throw new Error("记忆插件互斥:本地记忆与 Lore 不能同时加载。");
|
|
121
|
+
const loaded = [...families][0];
|
|
122
|
+
if (provider === "off" ? loaded !== undefined : loaded !== provider) {
|
|
123
|
+
throw new Error(`记忆插件与当前记忆来源不一致:期望 ${provider},实际 ${loaded ?? "无"}。`);
|
|
124
|
+
}
|
|
125
|
+
const extensions = result.extensions.map((extension) => {
|
|
126
|
+
if (!extension.tools?.has("memory_search")) return extension;
|
|
127
|
+
const tools = new Map(extension.tools);
|
|
128
|
+
tools.delete("memory_search");
|
|
129
|
+
return { ...extension, tools };
|
|
130
|
+
});
|
|
131
|
+
return { ...result, extensions };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* pi SDK 的宿主:模型运行时 + 会话工厂。不发现用户扩展、技能、AGENTS.md;
|
|
136
|
+
* 扩展只有按记忆提供者挑出来的那一个捆绑插件。
|
|
137
|
+
*/
|
|
138
|
+
export class PiHost {
|
|
139
|
+
/** Hob 自己的引擎目录:凭据和资源都在 HOB_HOME 里,不碰任何别的工具。 */
|
|
140
|
+
readonly engineDir: string;
|
|
141
|
+
/** 引擎的模型配置:只读 Hob 自己写的那份(内置服务商的思考档),不读别的工具的 models.json。 */
|
|
142
|
+
readonly modelsPath: string;
|
|
143
|
+
/** 服务商套进引擎(设 Key、注册兼容接口);引擎建好、返回之前调一次。 */
|
|
144
|
+
configure?: (runtime: ModelRuntime) => Promise<void>;
|
|
145
|
+
private runtimePromise?: Promise<ModelRuntime>;
|
|
146
|
+
/** Lore 插件在 resources_discover 中注册的目录。 */
|
|
147
|
+
loreSkillPaths: string[] = [];
|
|
148
|
+
|
|
149
|
+
constructor(private readonly options: PiHostOptions) {
|
|
150
|
+
this.engineDir = options.engineDir;
|
|
151
|
+
this.modelsPath = options.modelsPath ?? join(this.engineDir, "models.json");
|
|
152
|
+
this.configure = options.configure;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
runtime(): Promise<ModelRuntime> {
|
|
156
|
+
this.runtimePromise ??= (async () => {
|
|
157
|
+
mkdirSync(this.engineDir, { recursive: true });
|
|
158
|
+
const runtime = await ModelRuntime.create({
|
|
159
|
+
authPath: join(this.engineDir, "auth.json"),
|
|
160
|
+
modelsPath: this.modelsPath,
|
|
161
|
+
allowModelNetwork: false,
|
|
162
|
+
});
|
|
163
|
+
for (const provider of this.options.nativeProviders ?? []) runtime.registerNativeProvider(provider);
|
|
164
|
+
await this.configure?.(runtime);
|
|
165
|
+
return runtime;
|
|
166
|
+
})();
|
|
167
|
+
return this.runtimePromise;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 设置页先于任何会话打开时,也通过插件自己的发现流程取得目录。 */
|
|
171
|
+
async discoverLoreSkills(): Promise<void> {
|
|
172
|
+
const pluginDir = dirname(bundledPluginEntry(MEMORY_PACKAGES.lore));
|
|
173
|
+
const api = await import(pathToFileURL(join(pluginDir, "api.ts")).href);
|
|
174
|
+
const skills = await import(pathToFileURL(join(pluginDir, "skills.ts")).href);
|
|
175
|
+
const config = api.pickPluginConfig({});
|
|
176
|
+
if (!config.skillsEnabled) { this.loreSkillPaths = []; return; }
|
|
177
|
+
let discover: (() => Promise<{ skillPaths?: string[] } | undefined>) | undefined;
|
|
178
|
+
skills.registerSkillResourceDiscovery({ on: (event: string, handler: typeof discover) => { if (event === "resources_discover") discover = handler; } }, config);
|
|
179
|
+
const result = await discover?.();
|
|
180
|
+
if (!result?.skillPaths?.length) throw new Error("无法从 Lore 获取技能目录,请检查连接和服务端技能能力。");
|
|
181
|
+
this.loreSkillPaths = result.skillPaths;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async model(ref: string): Promise<Model<any>> {
|
|
185
|
+
const runtime = await this.runtime();
|
|
186
|
+
const slash = ref.indexOf("/");
|
|
187
|
+
const provider = slash === -1 ? "" : ref.slice(0, slash);
|
|
188
|
+
const id = slash === -1 ? ref : ref.slice(slash + 1);
|
|
189
|
+
const model = provider ? runtime.getModel(provider, id) : undefined;
|
|
190
|
+
if (!model) throw new Error(`无法使用模型 ${ref}:该模型的服务商已删除或未配置 Key。`);
|
|
191
|
+
return model;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 会话用的资源:只加载当前记忆提供者的那个捆绑插件。
|
|
196
|
+
* 别的工具装的扩展、技能、提示词、主题一份都不加载(`noExtensions` 只放我们给的路径)。
|
|
197
|
+
*/
|
|
198
|
+
private async resourceLoader(
|
|
199
|
+
options: OpenSessionOptions,
|
|
200
|
+
settingsManager: SettingsManager,
|
|
201
|
+
): Promise<ResourceLoader> {
|
|
202
|
+
const provider = options.memory ?? "off";
|
|
203
|
+
const pluginPath = provider === "off" ? undefined : bundledPluginEntry(MEMORY_PACKAGES[provider]);
|
|
204
|
+
if (provider === "local") {
|
|
205
|
+
// 会话被频繁回收,退出总结只会白花一次模型调用(和参考产品一致)
|
|
206
|
+
process.env.PI_MEMORY_EXIT_SUMMARY = "0";
|
|
207
|
+
}
|
|
208
|
+
const base = new DefaultResourceLoader({
|
|
209
|
+
cwd: options.cwd,
|
|
210
|
+
agentDir: this.engineDir,
|
|
211
|
+
settingsManager,
|
|
212
|
+
noExtensions: true,
|
|
213
|
+
noSkills: true,
|
|
214
|
+
noPromptTemplates: true,
|
|
215
|
+
noThemes: true,
|
|
216
|
+
noContextFiles: true,
|
|
217
|
+
additionalExtensionPaths: pluginPath ? [pluginPath] : [],
|
|
218
|
+
extensionsOverride: (result) => filterMemoryExtensions(result, provider),
|
|
219
|
+
});
|
|
220
|
+
await base.reload();
|
|
221
|
+
// 系统提示词仍由我们每轮现读(DefaultResourceLoader 的提示词只在 reload 时算一次)
|
|
222
|
+
return {
|
|
223
|
+
...emptyLoader(options.systemPrompt),
|
|
224
|
+
getExtensions: () => base.getExtensions(),
|
|
225
|
+
getSkills: () => {
|
|
226
|
+
if (!options.skills) return { skills: [], diagnostics: [] };
|
|
227
|
+
const discovered = base.getSkills();
|
|
228
|
+
const own = options.skills.filter((skill) => skill.sourceInfo.source === "hob");
|
|
229
|
+
const common = options.skills.filter((skill) => skill.sourceInfo.source !== "hob");
|
|
230
|
+
const seen = new Set<string>();
|
|
231
|
+
const skills = [...own, ...discovered.skills, ...common].filter((skill) => {
|
|
232
|
+
if (discovered.skills.includes(skill) && options.disabledSkills?.includes(`lore:${skill.name}`)) return false;
|
|
233
|
+
if (seen.has(skill.name)) return false;
|
|
234
|
+
seen.add(skill.name);
|
|
235
|
+
return true;
|
|
236
|
+
});
|
|
237
|
+
return { skills, diagnostics: discovered.diagnostics };
|
|
238
|
+
},
|
|
239
|
+
extendResources: (paths) => {
|
|
240
|
+
if (provider !== "lore") return;
|
|
241
|
+
this.loreSkillPaths = (paths.skillPaths ?? []).map((entry) => entry.path);
|
|
242
|
+
base.extendResources({ skillPaths: paths.skillPaths });
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async open(options: OpenSessionOptions): Promise<OpenedSession> {
|
|
248
|
+
mkdirSync(dirname(options.file), { recursive: true });
|
|
249
|
+
const model = await this.model(options.model);
|
|
250
|
+
const manager = SessionManager.open(options.file, dirname(options.file), options.cwd);
|
|
251
|
+
const settingsManager = SettingsManager.inMemory({ compaction: { enabled: true }, retry: { enabled: true } });
|
|
252
|
+
const resourceLoader = await this.resourceLoader(options, settingsManager);
|
|
253
|
+
const memoryTools = MEMORY_TOOL_PATTERNS[options.memory ?? "off"];
|
|
254
|
+
const { session } = await createAgentSession({
|
|
255
|
+
cwd: options.cwd,
|
|
256
|
+
agentDir: this.engineDir,
|
|
257
|
+
model,
|
|
258
|
+
thinkingLevel: options.thinking,
|
|
259
|
+
modelRuntime: await this.runtime(),
|
|
260
|
+
resourceLoader,
|
|
261
|
+
sessionManager: manager,
|
|
262
|
+
settingsManager,
|
|
263
|
+
tools: [...options.tools, ...memoryTools],
|
|
264
|
+
customTools: options.customTools,
|
|
265
|
+
});
|
|
266
|
+
// resources_discover 由 bindExtensions 触发;仅注册技能目录,不加载对话层技能。
|
|
267
|
+
if (options.memory === "lore") {
|
|
268
|
+
this.loreSkillPaths = [];
|
|
269
|
+
await session.bindExtensions({ mode: "print" });
|
|
270
|
+
}
|
|
271
|
+
return { session, manager };
|
|
272
|
+
}
|
|
273
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { PROP_IDS, PROPS } from "../../shared/sprites.ts";
|
|
3
|
+
import type { GeneralSettings, MemoryProvider, ThinkingLevel } from "../../shared/types.ts";
|
|
4
|
+
import type { Store } from "./store.ts";
|
|
5
|
+
import { cronToChinese } from "./cron-text.ts";
|
|
6
|
+
import { memoryPromptLines } from "./memory.ts";
|
|
7
|
+
import { nowText } from "./time.ts";
|
|
8
|
+
|
|
9
|
+
/** 提示词里列的模型:助手能派其中一个。 */
|
|
10
|
+
export interface PromptModel {
|
|
11
|
+
name: string;
|
|
12
|
+
ref: string;
|
|
13
|
+
note: string; // 用途说明
|
|
14
|
+
thinking: ThinkingLevel; // 默认思考档
|
|
15
|
+
levels?: ThinkingLevel[]; // 模型可用的思考档
|
|
16
|
+
isDefault: boolean; // 不指定模型时用它
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** 派出去的活能做什么(助手设置 → 能力)。 */
|
|
20
|
+
export interface Abilities {
|
|
21
|
+
exec: boolean;
|
|
22
|
+
write: boolean;
|
|
23
|
+
image: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 提示词里要用的设置:时区、「关于你」、记忆提供者、模型、能力。 */
|
|
27
|
+
export interface PromptSettings {
|
|
28
|
+
timezone: string;
|
|
29
|
+
aboutYou: GeneralSettings["aboutYou"];
|
|
30
|
+
memory: MemoryProvider;
|
|
31
|
+
/** 这个助手能派的模型(Router 用) */
|
|
32
|
+
models?: PromptModel[];
|
|
33
|
+
skills?: { name: string; description: string }[];
|
|
34
|
+
thinkingChoice?: "auto" | "fixed";
|
|
35
|
+
/** 任务能做什么(Worker 用) */
|
|
36
|
+
abilities?: Abilities;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 「关于你」一节:三行里有哪行填了才出哪行,全空就整节不出。 */
|
|
40
|
+
function aboutLines(aboutYou: GeneralSettings["aboutYou"]): string[] {
|
|
41
|
+
const lines: string[] = [];
|
|
42
|
+
if (aboutYou.name) lines.push(`- 称呼:${aboutYou.name}`);
|
|
43
|
+
if (aboutYou.language) lines.push(`- 语言:${aboutYou.language}`);
|
|
44
|
+
if (aboutYou.notes) lines.push(`- 其他:${aboutYou.notes}`);
|
|
45
|
+
return lines;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function aboutSection(aboutYou: GeneralSettings["aboutYou"]): string[] {
|
|
49
|
+
const lines = aboutLines(aboutYou);
|
|
50
|
+
return lines.length ? ["", "## 关于用户", ...lines] : [];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 「可以派的模型」:只有一个时整节只一行。 */
|
|
54
|
+
function modelsSection(models: PromptModel[], fixed: boolean): string[] {
|
|
55
|
+
if (!models.length) return [];
|
|
56
|
+
const lines = ["", "## 可以派的模型", "聪明、成本、速度都是 0–100:越高越聪明、越贵、越快。按任务难度、可接受的花费和要多快来挑;简单的活别派给又贵又慢的模型。"];
|
|
57
|
+
if (fixed) {
|
|
58
|
+
for (const model of models) {
|
|
59
|
+
lines.push(`- ${model.ref}${model.isDefault ? "(默认)" : ""}${model.note ? ` · ${model.note}` : ""} · 思考档 ${model.thinking}`);
|
|
60
|
+
}
|
|
61
|
+
if (models.length > 1) lines.push("按任务难度和类型选择:start_job 的 model 填括号里的 ref;不确定就不填,使用默认值。");
|
|
62
|
+
return lines;
|
|
63
|
+
}
|
|
64
|
+
if (models.length === 1) {
|
|
65
|
+
const only = models[0];
|
|
66
|
+
lines.push(
|
|
67
|
+
`- ${only.ref}${only.isDefault ? "(默认)" : ""}${only.note ? ` · ${only.note}` : ""} · 思考档默认 ${only.thinking},可选 ${(only.levels ?? [only.thinking]).join("、")}:start_job 不用填 model,需要更强推理时填 thinking。`,
|
|
68
|
+
);
|
|
69
|
+
return lines;
|
|
70
|
+
}
|
|
71
|
+
for (const model of models) {
|
|
72
|
+
lines.push(
|
|
73
|
+
`- ${model.ref}${model.isDefault ? "(默认)" : ""}${model.note ? ` · ${model.note}` : ""} · 思考档默认 ${model.thinking},可选 ${(model.levels ?? [model.thinking]).join("、")}`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
lines.push("按任务难度和类型选择:start_job 的 model 填括号里的 ref,thinking 按需要填;不确定就不填,使用默认值。");
|
|
77
|
+
return lines;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 「你能做什么」:把助手开关关掉的能力明确写成不能做。 */
|
|
81
|
+
function abilitiesSection(abilities: Abilities): string[] {
|
|
82
|
+
const can: string[] = ["读文件(read、grep、find、ls)"];
|
|
83
|
+
const cannot: string[] = [];
|
|
84
|
+
(abilities.exec ? can : cannot).push("执行命令(bash)");
|
|
85
|
+
(abilities.write ? can : cannot).push("改文件(edit、write)");
|
|
86
|
+
(abilities.image ? can : cannot).push("生图(generate_image)");
|
|
87
|
+
const lines = ["", "## 你能做什么", `- 能做:${can.join("、")}`];
|
|
88
|
+
if (cannot.length) {
|
|
89
|
+
lines.push(`- 不能做:${cannot.join("、")}。确需该能力时用 report(stuck) 说明缺少什么,不要勉强完成。`);
|
|
90
|
+
}
|
|
91
|
+
return lines;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Router 的系统提示词:每轮现读 store,所以改了规矩/补充说明下一轮就生效。 */
|
|
95
|
+
export function buildSystemPrompt(store: Store, assistantId: string, settings: PromptSettings): string {
|
|
96
|
+
const assistant = store.assistant(assistantId);
|
|
97
|
+
if (!assistant) throw new Error(`助手不存在:${assistantId}`);
|
|
98
|
+
const rules = store.rules(assistantId);
|
|
99
|
+
const routines = store.routines().filter((routine) => routine.assistantId === assistantId);
|
|
100
|
+
const others = store.assistants().filter((item) => item.id !== assistantId && !item.hidden);
|
|
101
|
+
|
|
102
|
+
const lines: string[] = [
|
|
103
|
+
`你是「${assistant.name}」,用户的${assistant.primary ? "主助手" : "助手"}。`,
|
|
104
|
+
`职责:${assistant.duty}`,
|
|
105
|
+
...(assistant.dutyLong?.trim() ? [`详细职责:${assistant.dutyLong.trim()}`] : []),
|
|
106
|
+
`现在时间:${nowText(settings.timezone)}`,
|
|
107
|
+
];
|
|
108
|
+
|
|
109
|
+
lines.push(...aboutSection(settings.aboutYou));
|
|
110
|
+
lines.push(...modelsSection(settings.models ?? [], settings.thinkingChoice === "fixed"));
|
|
111
|
+
if (settings.skills?.length) {
|
|
112
|
+
lines.push("", "## 任务可用的技能", ...settings.skills.map((skill) => `- ${skill.name}:${skill.description}`));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (rules.length) {
|
|
116
|
+
lines.push("", "## 你的规则");
|
|
117
|
+
for (const rule of rules) lines.push(`- ${rule.mode === "ask" ? "执行前确认" : "直接执行"}:${rule.text}`);
|
|
118
|
+
}
|
|
119
|
+
if (assistant.instructions.trim()) {
|
|
120
|
+
lines.push("", "## 补充说明", assistant.instructions.trim());
|
|
121
|
+
}
|
|
122
|
+
const memory = memoryPromptLines(settings.memory);
|
|
123
|
+
if (memory.length) lines.push("", ...memory);
|
|
124
|
+
if (routines.length) {
|
|
125
|
+
lines.push("", "## 你的定时");
|
|
126
|
+
for (const routine of routines) {
|
|
127
|
+
const when = routine.schedules.map(cronToChinese).join("、");
|
|
128
|
+
lines.push(`- ${routine.name} · ${when}${routine.active ? "" : "(暂停)"}:${routine.instruction}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (assistant.primary && others.length) {
|
|
132
|
+
lines.push("", "## 其他助手");
|
|
133
|
+
for (const other of others) {
|
|
134
|
+
const ask = store.rules(other.id).filter((rule) => rule.mode === "ask").map((rule) => rule.text);
|
|
135
|
+
lines.push(`- ${other.name}(${other.id}):${other.duty}${other.dutyLong?.trim() ? `。详细职责:${other.dutyLong.trim()}` : ""}${ask.length ? `。执行前要确认:${ask.join(";")}` : ""}`);
|
|
136
|
+
}
|
|
137
|
+
lines.push("转交前先对照详细职责挑最合适的助手;没有合适的就自己派任务,或者提议创建新助手。");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (!assistant.primary) {
|
|
141
|
+
lines.push("", "## 转来的事", "带「转自 Hob」的消息是主助手替用户转来的,等同于用户本人的要求:照着去做,需要执行就直接 start_job,不要只回复计划;做完不用另外给 Hob 发消息,结果会自动回到 Hob。", "", "## 不归你管的事", "主助手 Hob 负责在各助手之间分派。用户的要求明显超出你的职责时,调用 hand_back_to_hob 交回 Hob,不要硬做;在你职责内的事照常自己做。");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
lines.push(
|
|
145
|
+
"",
|
|
146
|
+
"## 工作方式",
|
|
147
|
+
"- 说要做的事,就在同一条消息里调用对应工具去做;不要只描述计划然后停下等用户催。只有规则要求确认或操作不可逆时才停下来问。",
|
|
148
|
+
"- 用简洁、规范的书面中文回复,直接给出结论,不要复述步骤、不要罗列清单。",
|
|
149
|
+
"- 你不能亲自执行任何操作:没有 bash、read、edit、write 这些工具(联网搜索除外)。需要执行任务时调用 start_job 交给后台任务,任务完成后会有事件回到这里。",
|
|
150
|
+
"- 需要最新信息或核对事实时,直接用 web_search / web_fetch 查,不必为此派任务;要多步调研、写文件或跑命令时再用 start_job。",
|
|
151
|
+
"- 任务本身就是带完整工具的编码 agent:写代码、改项目、查文件都是普通任务,用 start_job 的 cwd 指定任务的工作目录。",
|
|
152
|
+
"- 只有规则标为「执行前确认」,或者操作不可逆(发送、删除、付费)时,才用 ask_user 请用户确认。",
|
|
153
|
+
"- 会产生卡片的工具(start_job、ask_user、propose_assistant):先在同一条消息里写一句话(要做什么、等用户确认什么),再调用工具。这些工具会返回“本轮到此结束”:卡片已经出现在对话里,内容齐全,调用完立即结束这一轮,不要再补任何文字(“草稿卡在上面了”“点创建就成”这类话都是重复的废话)。",
|
|
154
|
+
"- 用 ask_user 时,如果同类操作以后很可能还会遇到(日常的常规操作;发布、删除、付费这类不可逆的事除外),第三个选项写“以后同类操作直接执行,无需确认”(always),并填一句很短的 ruleText,如“清理临时目录”。",
|
|
155
|
+
"- 用户说“以后”“别再”这类要求时,用 set_rule 记入规则,不要只做口头承诺。",
|
|
156
|
+
...(assistant.primary
|
|
157
|
+
? [
|
|
158
|
+
"- 用户要早报、日报,或者问“昨晚/今天都做了什么”时,先调 overview 看各助手这段时间的日志和任务,再照实写一两句汇总,不要凭印象编造。",
|
|
159
|
+
]
|
|
160
|
+
: []),
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
if (assistant.primary) {
|
|
164
|
+
lines.push(
|
|
165
|
+
"- 同类任务在对话中反复出现(同一件事被要求 3 次以上),就用 propose_assistant 提议创建专属助手。",
|
|
166
|
+
"- propose_assistant 的 duty 是左栏头像下面那行小字:最多 8 个字,如「线上和机器」「模型和 agent」;更详细的职责写在 dutyLong(草稿卡里的「职责」行)。",
|
|
167
|
+
`- propose_assistant 的 sprite 按职责选:${PROP_IDS.filter((id) => id !== "hob")
|
|
168
|
+
.map((id) => `${id} ${PROPS[id].label}`)
|
|
169
|
+
.join(",")};hob 那个火苗是主助手专用的,不要用。`,
|
|
170
|
+
"- 库里没有贴合职责的道具时,可以自己画一件:sprite 填 custom,prop_art 给出 art、pal、dx 画一件 16×16 的像素道具;画得不合适会返回原因,照着改好重发就行。",
|
|
171
|
+
"- 有更合适的助手时用 message_assistant 把任务转过去。",
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
lines.push("- 用户问你在做什么、有什么在运行,可依据任务事件回答,不要编造进度。");
|
|
175
|
+
lines.push(
|
|
176
|
+
"- 最后重申:start_job、ask_user、propose_assistant 返回“本轮到此结束”后,你说的下一个字都是多余的,直接结束回合。",
|
|
177
|
+
);
|
|
178
|
+
return lines.join("\n");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Worker 的系统提示词:所属助手、要问的事、补充说明、任务说明、必须 report 收尾。 */
|
|
182
|
+
export function buildWorkerPrompt(store: Store, jobId: string, settings: PromptSettings): string {
|
|
183
|
+
const job = store.job(jobId);
|
|
184
|
+
if (!job) throw new Error(`任务不存在:${jobId}`);
|
|
185
|
+
const assistant = store.assistant(job.assistantId);
|
|
186
|
+
const askFirst = store.rules(job.assistantId).filter((rule) => rule.mode === "ask");
|
|
187
|
+
const lines: string[] = [
|
|
188
|
+
`你在后台执行「${assistant?.name ?? job.assistantId}」的任务。`,
|
|
189
|
+
`职责:${assistant?.duty ?? ""}`,
|
|
190
|
+
`现在时间:${nowText(settings.timezone)}`,
|
|
191
|
+
];
|
|
192
|
+
lines.push(...aboutSection(settings.aboutYou));
|
|
193
|
+
if (settings.abilities) lines.push(...abilitiesSection(settings.abilities));
|
|
194
|
+
if (askFirst.length) {
|
|
195
|
+
lines.push("", "## 这些事执行前必须用 ask_user 询问用户");
|
|
196
|
+
for (const rule of askFirst) lines.push(`- ${rule.text}`);
|
|
197
|
+
}
|
|
198
|
+
if (assistant?.instructions.trim()) {
|
|
199
|
+
lines.push("", "## 补充说明", assistant.instructions.trim());
|
|
200
|
+
}
|
|
201
|
+
const memory = memoryPromptLines(settings.memory);
|
|
202
|
+
if (memory.length) lines.push("", ...memory);
|
|
203
|
+
if (assistant?.primary) {
|
|
204
|
+
lines.push(
|
|
205
|
+
"",
|
|
206
|
+
"## 早报",
|
|
207
|
+
"- 用户要早报、日报或汇总“昨晚/今天都做了什么”时,先调 overview(sinceHours) 拿各助手这段时间的日志和任务,再照实写汇总。",
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
lines.push(
|
|
211
|
+
"",
|
|
212
|
+
`## 任务:${job.title}`,
|
|
213
|
+
job.instruction,
|
|
214
|
+
...(job.inputs?.length ? ["", "## 你收到的文件", ...job.inputs.map((file) => `- ${file}`)] : []),
|
|
215
|
+
"",
|
|
216
|
+
`工作目录:${job.cwd ?? homedir()}`,
|
|
217
|
+
"",
|
|
218
|
+
"## 要求",
|
|
219
|
+
"- 产出文件只写在工作目录里:新建的文件、下载的内容、导出的结果都放在这个目录,不要写到用户主目录或其他位置。",
|
|
220
|
+
"- 读取不受目录限制:可读取任何位置的文件作参考,只是不要写入这些位置。",
|
|
221
|
+
"- 要给用户看的文件放进 report 的 files(绝对路径,文件本身在工作目录里)。",
|
|
222
|
+
"- 完成后必须调用 report 提交结果(outcome:已完成 / 受阻 / 待确认),不调用视为未完成。",
|
|
223
|
+
"- summary 用中文写给用户:一两句结论 + 关键数字,不要罗列过程。",
|
|
224
|
+
"- 受阻时调用 report(stuck) 说明受阻原因;要做不该做的、无法撤回的事就调用 ask_user。",
|
|
225
|
+
);
|
|
226
|
+
return lines.join("\n");
|
|
227
|
+
}
|