@oyasmi/pipiclaw 0.6.10 → 0.7.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/README.md +1 -1
- package/dist/agent/channel-runner.d.ts +12 -1
- package/dist/agent/channel-runner.js +58 -3
- package/dist/agent/commands.d.ts +1 -1
- package/dist/agent/commands.js +10 -0
- package/dist/agent/prompt-builder.d.ts +17 -0
- package/dist/agent/prompt-builder.js +42 -23
- package/dist/agent/session-events.d.ts +2 -0
- package/dist/agent/session-events.js +31 -12
- package/dist/agent/types.d.ts +9 -0
- package/dist/agent/types.js +8 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/log.d.ts +24 -0
- package/dist/log.js +96 -1
- package/dist/memory/bootstrap.js +35 -1
- package/dist/memory/candidates.js +21 -3
- package/dist/memory/consolidation.d.ts +7 -1
- package/dist/memory/consolidation.js +107 -36
- package/dist/memory/files.d.ts +40 -0
- package/dist/memory/files.js +0 -0
- package/dist/memory/lifecycle.js +2 -0
- package/dist/memory/maintenance-jobs.js +10 -4
- package/dist/memory/post-turn-review.js +4 -2
- package/dist/memory/promotion-signals.js +0 -1
- package/dist/memory/recall.d.ts +1 -0
- package/dist/memory/recall.js +11 -2
- package/dist/memory/session-search.d.ts +1 -0
- package/dist/memory/session-search.js +1 -0
- package/dist/memory/session.d.ts +1 -0
- package/dist/memory/session.js +44 -13
- package/dist/memory/sidecar-worker.d.ts +4 -0
- package/dist/memory/sidecar-worker.js +29 -0
- package/dist/memory/transcript.d.ts +9 -0
- package/dist/memory/transcript.js +30 -0
- package/dist/paths.d.ts +3 -0
- package/dist/paths.js +3 -0
- package/dist/runtime/bootstrap.js +151 -15
- package/dist/runtime/delivery.js +17 -0
- package/dist/runtime/dingtalk.d.ts +2 -0
- package/dist/runtime/dingtalk.js +19 -4
- package/dist/sandbox.d.ts +8 -0
- package/dist/sandbox.js +21 -17
- package/dist/settings.d.ts +16 -0
- package/dist/settings.js +22 -0
- package/dist/shared/jsonl-appender.d.ts +25 -0
- package/dist/shared/jsonl-appender.js +85 -0
- package/dist/subagents/tool.d.ts +2 -2
- package/dist/subagents/tool.js +25 -56
- package/dist/tools/bash.d.ts +8 -1
- package/dist/tools/bash.js +46 -21
- package/dist/tools/config.d.ts +3 -0
- package/dist/tools/config.js +9 -0
- package/dist/tools/edit.d.ts +1 -0
- package/dist/tools/edit.js +18 -11
- package/dist/tools/index.js +18 -53
- package/dist/tools/memory-save.d.ts +17 -0
- package/dist/tools/memory-save.js +43 -0
- package/dist/tools/read.d.ts +2 -2
- package/dist/tools/read.js +2 -2
- package/dist/tools/registry.d.ts +63 -0
- package/dist/tools/registry.js +154 -0
- package/dist/tools/session-search.d.ts +3 -2
- package/dist/tools/session-search.js +10 -5
- package/dist/tools/skill-list.js +1 -1
- package/dist/tools/skill-manage.d.ts +1 -1
- package/dist/tools/skill-manage.js +3 -1
- package/dist/tools/skill-view.js +12 -11
- package/dist/tools/web-search.d.ts +1 -1
- package/dist/tools/web-search.js +1 -1
- package/dist/usage/ledger.d.ts +47 -0
- package/dist/usage/ledger.js +95 -0
- package/dist/usage/render.d.ts +11 -0
- package/dist/usage/render.js +67 -0
- package/package.json +5 -5
- package/dist/tools/attach.d.ts +0 -20
- package/dist/tools/attach.js +0 -69
package/dist/tools/config.d.ts
CHANGED
package/dist/tools/config.js
CHANGED
|
@@ -29,6 +29,9 @@ export const DEFAULT_TOOLS_CONFIG = {
|
|
|
29
29
|
sessionSearch: {
|
|
30
30
|
enabled: true,
|
|
31
31
|
},
|
|
32
|
+
save: {
|
|
33
|
+
enabled: true,
|
|
34
|
+
},
|
|
32
35
|
},
|
|
33
36
|
skills: {
|
|
34
37
|
manage: {
|
|
@@ -80,6 +83,7 @@ function mergeToolsConfig(source, configPath, diagnostics) {
|
|
|
80
83
|
const web = isRecord(tools.web) ? tools.web : {};
|
|
81
84
|
const memory = isRecord(tools.memory) ? tools.memory : {};
|
|
82
85
|
const sessionSearch = isRecord(memory.sessionSearch) ? memory.sessionSearch : {};
|
|
86
|
+
const memorySave = isRecord(memory.save) ? memory.save : {};
|
|
83
87
|
const skills = isRecord(tools.skills) ? tools.skills : {};
|
|
84
88
|
const manage = isRecord(skills.manage) ? skills.manage : {};
|
|
85
89
|
const search = isRecord(web.search) ? web.search : {};
|
|
@@ -152,6 +156,11 @@ function mergeToolsConfig(source, configPath, diagnostics) {
|
|
|
152
156
|
? sessionSearch.enabled
|
|
153
157
|
: DEFAULT_TOOLS_CONFIG.tools.memory.sessionSearch.enabled,
|
|
154
158
|
},
|
|
159
|
+
save: {
|
|
160
|
+
enabled: typeof memorySave.enabled === "boolean"
|
|
161
|
+
? memorySave.enabled
|
|
162
|
+
: DEFAULT_TOOLS_CONFIG.tools.memory.save.enabled,
|
|
163
|
+
},
|
|
155
164
|
},
|
|
156
165
|
skills: {
|
|
157
166
|
manage: {
|
package/dist/tools/edit.d.ts
CHANGED
package/dist/tools/edit.js
CHANGED
|
@@ -82,6 +82,9 @@ const editSchema = Type.Object({
|
|
|
82
82
|
path: Type.String({ description: "Path to the file to edit (relative or absolute)" }),
|
|
83
83
|
oldText: Type.String({ description: "Exact text to find and replace (must match exactly)" }),
|
|
84
84
|
newText: Type.String({ description: "New text to replace the old text with" }),
|
|
85
|
+
replaceAll: Type.Optional(Type.Boolean({
|
|
86
|
+
description: "Replace every occurrence instead of requiring a unique match. Defaults to false (the match must be unique).",
|
|
87
|
+
})),
|
|
85
88
|
});
|
|
86
89
|
function formatPathBlockMessage(resolvedPath, category, reason) {
|
|
87
90
|
const lines = [`Path blocked${category ? ` [${category}]` : ""}`];
|
|
@@ -105,7 +108,7 @@ export function createEditTool(executor, options = {}) {
|
|
|
105
108
|
label: "edit",
|
|
106
109
|
description: "Edit a file by replacing exact text. The oldText must match exactly (including whitespace). Use this for precise, surgical edits.",
|
|
107
110
|
parameters: editSchema,
|
|
108
|
-
execute: async (_toolCallId, { path, oldText, newText }, signal) => {
|
|
111
|
+
execute: async (_toolCallId, { path, oldText, newText, replaceAll, }, signal) => {
|
|
109
112
|
if (securityConfig.enabled && securityConfig.pathGuard.enabled) {
|
|
110
113
|
const readGuard = guardPath(path, "read", { ...securityContext, config: securityConfig.pathGuard });
|
|
111
114
|
if (!readGuard.allowed) {
|
|
@@ -134,12 +137,18 @@ export function createEditTool(executor, options = {}) {
|
|
|
134
137
|
}
|
|
135
138
|
// Count occurrences
|
|
136
139
|
const occurrences = content.split(oldText).length - 1;
|
|
137
|
-
if (occurrences > 1) {
|
|
138
|
-
throw new Error(`Found ${occurrences} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`);
|
|
140
|
+
if (occurrences > 1 && !replaceAll) {
|
|
141
|
+
throw new Error(`Found ${occurrences} occurrences of the text in ${path}. The text must be unique, or pass replaceAll: true to replace all of them. Please provide more context to make it unique.`);
|
|
139
142
|
}
|
|
140
143
|
// Perform replacement
|
|
141
|
-
|
|
142
|
-
|
|
144
|
+
let newContent;
|
|
145
|
+
if (replaceAll) {
|
|
146
|
+
newContent = content.split(oldText).join(newText);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
const index = content.indexOf(oldText);
|
|
150
|
+
newContent = content.substring(0, index) + newText + content.substring(index + oldText.length);
|
|
151
|
+
}
|
|
143
152
|
if (content === newContent) {
|
|
144
153
|
throw new Error(`No changes made to ${path}. The replacement produced identical content. This might indicate an issue with special characters or the text not existing as expected.`);
|
|
145
154
|
}
|
|
@@ -150,13 +159,11 @@ export function createEditTool(executor, options = {}) {
|
|
|
150
159
|
channelId: options.channelId,
|
|
151
160
|
toolName: "edit",
|
|
152
161
|
});
|
|
162
|
+
const replacementSummary = replaceAll
|
|
163
|
+
? `Replaced ${occurrences} occurrence${occurrences === 1 ? "" : "s"} in ${path}.`
|
|
164
|
+
: `Successfully replaced text in ${path}. Changed ${oldText.length} characters to ${newText.length} characters.`;
|
|
153
165
|
return {
|
|
154
|
-
content: [
|
|
155
|
-
{
|
|
156
|
-
type: "text",
|
|
157
|
-
text: `Successfully replaced text in ${path}. Changed ${oldText.length} characters to ${newText.length} characters.`,
|
|
158
|
-
},
|
|
159
|
-
],
|
|
166
|
+
content: [{ type: "text", text: replacementSummary }],
|
|
160
167
|
details: {
|
|
161
168
|
diff: generateDiffString(content, newContent),
|
|
162
169
|
patch: Diff.createPatch(path, content, newContent),
|
package/dist/tools/index.js
CHANGED
|
@@ -5,12 +5,7 @@ import { createBashTool } from "./bash.js";
|
|
|
5
5
|
import { loadToolsConfig } from "./config.js";
|
|
6
6
|
import { createEditTool } from "./edit.js";
|
|
7
7
|
import { createReadTool } from "./read.js";
|
|
8
|
-
import {
|
|
9
|
-
import { createSkillListTool } from "./skill-list.js";
|
|
10
|
-
import { createSkillManageTool } from "./skill-manage.js";
|
|
11
|
-
import { createSkillViewTool } from "./skill-view.js";
|
|
12
|
-
import { createWebFetchTool } from "./web-fetch.js";
|
|
13
|
-
import { createWebSearchTool } from "./web-search.js";
|
|
8
|
+
import { buildToolSet } from "./registry.js";
|
|
14
9
|
import { createWriteTool } from "./write.js";
|
|
15
10
|
export function createPipiclawBaseTools(executor, options = {}) {
|
|
16
11
|
const hasSecurityOptions = options.securityConfig || options.securityContext || options.channelId;
|
|
@@ -36,58 +31,28 @@ export function createPipiclawTools(options) {
|
|
|
36
31
|
workspacePath: options.workspacePath,
|
|
37
32
|
cwd: process.cwd(),
|
|
38
33
|
};
|
|
39
|
-
|
|
34
|
+
// The leaf tools (files, web, memory, skills) come from the single declarative
|
|
35
|
+
// registry so this set, the sub-agent set, and the prompt hints share one source.
|
|
36
|
+
// The `subagent` tool is appended separately: it is never available to sub-agents
|
|
37
|
+
// and keeping it out of the registry avoids a registry ↔ subagents/tool import cycle.
|
|
38
|
+
const leafTools = buildToolSet({
|
|
39
|
+
executor: options.executor,
|
|
40
40
|
securityConfig,
|
|
41
41
|
securityContext,
|
|
42
42
|
channelId: options.channelId,
|
|
43
|
+
channelDir: options.channelDir,
|
|
44
|
+
workspaceDir: options.workspaceDir,
|
|
45
|
+
workspacePath: options.workspacePath,
|
|
46
|
+
webConfig: toolsConfig.tools.web,
|
|
47
|
+
toolsConfig,
|
|
48
|
+
getCurrentModel: options.getCurrentModel,
|
|
49
|
+
getAvailableModels: options.getAvailableModels,
|
|
50
|
+
resolveApiKey: options.resolveApiKey,
|
|
51
|
+
getSessionSearchSettings: options.getSessionSearchSettings,
|
|
52
|
+
memoryCandidateStore: options.memoryCandidateStore,
|
|
43
53
|
});
|
|
44
|
-
const webTools = toolsConfig.tools.web.enable === false
|
|
45
|
-
? []
|
|
46
|
-
: [
|
|
47
|
-
createWebSearchTool({
|
|
48
|
-
webConfig: toolsConfig.tools.web,
|
|
49
|
-
securityConfig,
|
|
50
|
-
workspaceDir: options.workspaceDir,
|
|
51
|
-
channelId: options.channelId,
|
|
52
|
-
}),
|
|
53
|
-
createWebFetchTool({
|
|
54
|
-
webConfig: toolsConfig.tools.web,
|
|
55
|
-
securityConfig,
|
|
56
|
-
workspaceDir: options.workspaceDir,
|
|
57
|
-
channelId: options.channelId,
|
|
58
|
-
}),
|
|
59
|
-
];
|
|
60
|
-
const memoryTools = toolsConfig.tools.memory.sessionSearch.enabled === false
|
|
61
|
-
? []
|
|
62
|
-
: [
|
|
63
|
-
createSessionSearchTool({
|
|
64
|
-
channelDir: options.channelDir,
|
|
65
|
-
getCurrentModel: options.getCurrentModel,
|
|
66
|
-
resolveApiKey: options.resolveApiKey,
|
|
67
|
-
getSessionSearchSettings: options.getSessionSearchSettings,
|
|
68
|
-
}),
|
|
69
|
-
];
|
|
70
|
-
const skillTools = toolsConfig.tools.skills.manage.enabled === false
|
|
71
|
-
? []
|
|
72
|
-
: [
|
|
73
|
-
createSkillListTool({
|
|
74
|
-
workspaceDir: options.workspaceDir,
|
|
75
|
-
workspacePath: options.workspacePath,
|
|
76
|
-
}),
|
|
77
|
-
createSkillViewTool({
|
|
78
|
-
workspaceDir: options.workspaceDir,
|
|
79
|
-
workspacePath: options.workspacePath,
|
|
80
|
-
}),
|
|
81
|
-
createSkillManageTool({
|
|
82
|
-
workspaceDir: options.workspaceDir,
|
|
83
|
-
workspacePath: options.workspacePath,
|
|
84
|
-
}),
|
|
85
|
-
];
|
|
86
54
|
return [
|
|
87
|
-
...
|
|
88
|
-
...webTools,
|
|
89
|
-
...memoryTools,
|
|
90
|
-
...skillTools,
|
|
55
|
+
...leafTools,
|
|
91
56
|
createSubAgentTool({
|
|
92
57
|
executor: options.executor,
|
|
93
58
|
getCurrentModel: options.getCurrentModel,
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import type { MemoryCandidateStore } from "../memory/candidates.js";
|
|
4
|
+
import { type ChannelMemoryQueue } from "../memory/channel-maintenance-queue.js";
|
|
5
|
+
declare const memorySaveSchema: Type.TObject<{
|
|
6
|
+
label: Type.TString;
|
|
7
|
+
content: Type.TString;
|
|
8
|
+
kind: Type.TOptional<Type.TUnion<[Type.TLiteral<"preference">, Type.TLiteral<"fact">, Type.TLiteral<"decision">, Type.TLiteral<"constraint">, Type.TLiteral<"open-loop">]>>;
|
|
9
|
+
}>;
|
|
10
|
+
export interface MemorySaveToolOptions {
|
|
11
|
+
channelId: string;
|
|
12
|
+
channelDir: string;
|
|
13
|
+
memoryCandidateStore: MemoryCandidateStore;
|
|
14
|
+
channelMemoryQueue?: ChannelMemoryQueue;
|
|
15
|
+
}
|
|
16
|
+
export declare function createMemorySaveTool(options: MemorySaveToolOptions): AgentTool<typeof memorySaveSchema>;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
import { getDefaultChannelMemoryQueue } from "../memory/channel-maintenance-queue.js";
|
|
3
|
+
import { applyChannelMemoryOps, getChannelMemoryPath } from "../memory/files.js";
|
|
4
|
+
const memorySaveSchema = Type.Object({
|
|
5
|
+
label: Type.String({ description: "Brief description of what you're saving and why (shown to user)" }),
|
|
6
|
+
content: Type.String({
|
|
7
|
+
description: "The durable fact to remember, as a single self-contained, keyword-rich sentence. Write it so future keyword search can find it.",
|
|
8
|
+
}),
|
|
9
|
+
kind: Type.Optional(Type.Union([
|
|
10
|
+
Type.Literal("preference"),
|
|
11
|
+
Type.Literal("fact"),
|
|
12
|
+
Type.Literal("decision"),
|
|
13
|
+
Type.Literal("constraint"),
|
|
14
|
+
Type.Literal("open-loop"),
|
|
15
|
+
], { description: "What kind of durable memory this is." })),
|
|
16
|
+
});
|
|
17
|
+
export function createMemorySaveTool(options) {
|
|
18
|
+
const queue = options.channelMemoryQueue ?? getDefaultChannelMemoryQueue();
|
|
19
|
+
return {
|
|
20
|
+
name: "memory_save",
|
|
21
|
+
label: "memory_save",
|
|
22
|
+
description: "Save a durable fact to this channel's long-term MEMORY.md immediately. Use when the user explicitly asks you to remember something (a preference, decision, constraint, or long-running commitment), so it survives restarts and future sessions. Do not use for transient task state — that is captured automatically.",
|
|
23
|
+
parameters: memorySaveSchema,
|
|
24
|
+
execute: async (_toolCallId, { content, kind }) => {
|
|
25
|
+
const trimmed = content.trim();
|
|
26
|
+
if (!trimmed) {
|
|
27
|
+
return {
|
|
28
|
+
content: [{ type: "text", text: "No content provided; nothing was saved." }],
|
|
29
|
+
details: { kind: "memory_save", saved: false },
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
// Serialize through the shared channel memory queue so this never races with
|
|
33
|
+
// background consolidation/maintenance on the same channel's files.
|
|
34
|
+
const result = await queue.run(options.channelId, () => applyChannelMemoryOps(options.channelDir, [{ op: "add", content: trimmed }]));
|
|
35
|
+
// Make the new memory recallable within this same turn.
|
|
36
|
+
options.memoryCandidateStore.invalidate(getChannelMemoryPath(options.channelDir));
|
|
37
|
+
return {
|
|
38
|
+
content: [{ type: "text", text: `Saved to channel memory${kind ? ` (${kind})` : ""}.` }],
|
|
39
|
+
details: { kind: "memory_save", saved: result.added > 0 },
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
package/dist/tools/read.d.ts
CHANGED
|
@@ -5,8 +5,8 @@ import type { SecurityConfig, SecurityRuntimeContext } from "../security/types.j
|
|
|
5
5
|
declare const readSchema: Type.TObject<{
|
|
6
6
|
label: Type.TString;
|
|
7
7
|
path: Type.TString;
|
|
8
|
-
offset: Type.TOptional<Type.
|
|
9
|
-
limit: Type.TOptional<Type.
|
|
8
|
+
offset: Type.TOptional<Type.TInteger>;
|
|
9
|
+
limit: Type.TOptional<Type.TInteger>;
|
|
10
10
|
}>;
|
|
11
11
|
export interface ReadToolOptions {
|
|
12
12
|
securityConfig?: SecurityConfig;
|
package/dist/tools/read.js
CHANGED
|
@@ -25,8 +25,8 @@ function isImageFile(filePath) {
|
|
|
25
25
|
const readSchema = Type.Object({
|
|
26
26
|
label: Type.String({ description: "Brief description of what you're reading and why (shown to user)" }),
|
|
27
27
|
path: Type.String({ description: "Path to the file to read (relative or absolute)" }),
|
|
28
|
-
offset: Type.Optional(Type.
|
|
29
|
-
limit: Type.Optional(Type.
|
|
28
|
+
offset: Type.Optional(Type.Integer({ minimum: 1, description: "Line number to start reading from (1-indexed)" })),
|
|
29
|
+
limit: Type.Optional(Type.Integer({ minimum: 1, description: "Maximum number of lines to read" })),
|
|
30
30
|
});
|
|
31
31
|
function countTextLines(content) {
|
|
32
32
|
if (content.length === 0) {
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { MemoryCandidateStore } from "../memory/candidates.js";
|
|
4
|
+
import type { Executor } from "../sandbox.js";
|
|
5
|
+
import type { SecurityConfig, SecurityRuntimeContext } from "../security/types.js";
|
|
6
|
+
import type { PipiclawSessionSearchSettings } from "../settings.js";
|
|
7
|
+
import type { PipiclawToolsConfig, PipiclawWebToolsConfig } from "./config.js";
|
|
8
|
+
/**
|
|
9
|
+
* Unified build context passed to every tool's `create` closure. It carries the union
|
|
10
|
+
* of everything any registered tool might need; each closure reads only its own fields.
|
|
11
|
+
*
|
|
12
|
+
* Fields used exclusively by main-agent-only tools (models, session-search settings,
|
|
13
|
+
* memory store) are optional: the sub-agent tool set never builds those tools, so it
|
|
14
|
+
* does not supply them. Closures assert presence via `req()` to turn a missing
|
|
15
|
+
* dependency into a loud error instead of a silent undefined.
|
|
16
|
+
*/
|
|
17
|
+
export interface ToolBuildContext {
|
|
18
|
+
executor: Executor;
|
|
19
|
+
securityConfig: SecurityConfig;
|
|
20
|
+
securityContext: SecurityRuntimeContext;
|
|
21
|
+
channelId: string;
|
|
22
|
+
channelDir: string;
|
|
23
|
+
workspaceDir: string;
|
|
24
|
+
workspacePath: string;
|
|
25
|
+
/** Main set: `toolsConfig.tools.web`; sub-agent set: the sub-agent's own webConfig. */
|
|
26
|
+
webConfig?: PipiclawWebToolsConfig;
|
|
27
|
+
/** Present only on the main path; gates session_search / memory_save / skills. */
|
|
28
|
+
toolsConfig?: PipiclawToolsConfig;
|
|
29
|
+
/** Sub-agent set passes its per-invocation bash timeout; the main set relies on the built-in default. */
|
|
30
|
+
bashDefaultTimeoutSeconds?: number;
|
|
31
|
+
getCurrentModel?: () => Model<Api>;
|
|
32
|
+
getAvailableModels?: () => Model<Api>[];
|
|
33
|
+
resolveApiKey?: (model: Model<Api>) => Promise<string>;
|
|
34
|
+
getSessionSearchSettings?: () => PipiclawSessionSearchSettings;
|
|
35
|
+
memoryCandidateStore?: MemoryCandidateStore;
|
|
36
|
+
}
|
|
37
|
+
export interface ToolRegistration {
|
|
38
|
+
name: string;
|
|
39
|
+
/** One-line hint rendered in the system prompt's `## Tools` section. */
|
|
40
|
+
promptHint: string;
|
|
41
|
+
/** Whether this tool is included in a sub-agent's tool set. */
|
|
42
|
+
availableToSubagents: boolean;
|
|
43
|
+
/** Config gate; when omitted the tool is always enabled. */
|
|
44
|
+
enabledBy?: (ctx: ToolBuildContext) => boolean;
|
|
45
|
+
create: (ctx: ToolBuildContext) => AgentTool<any>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Single source of truth for the agent's leaf tools. The `subagent` tool is intentionally
|
|
49
|
+
* NOT here: it is never available to sub-agents, needs runtime context the registry does
|
|
50
|
+
* not model, and keeping it out avoids a `registry` ↔ `subagents/tool` import cycle (the
|
|
51
|
+
* sub-agent tool set is built by importing `buildToolSet` from this module).
|
|
52
|
+
*/
|
|
53
|
+
export declare const TOOL_REGISTRY: ToolRegistration[];
|
|
54
|
+
/** Prompt hint for the `subagent` tool, which is registered outside TOOL_REGISTRY. */
|
|
55
|
+
export declare const SUBAGENT_TOOL_HINT = "Delegate a focused task to a sub-agent with its own isolated context";
|
|
56
|
+
/** Single source of truth for prompt hints, keyed by tool name (leaf tools + subagent). */
|
|
57
|
+
export declare const TOOL_PROMPT_HINTS: Record<string, string>;
|
|
58
|
+
export interface BuildToolSetOptions {
|
|
59
|
+
/** When true, include only tools flagged `availableToSubagents`. */
|
|
60
|
+
forSubagent?: boolean;
|
|
61
|
+
}
|
|
62
|
+
/** Build a tool set from the registry, honoring sub-agent availability and config gates. */
|
|
63
|
+
export declare function buildToolSet(ctx: ToolBuildContext, options?: BuildToolSetOptions): AgentTool<any>[];
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { createBashTool } from "./bash.js";
|
|
2
|
+
import { createEditTool } from "./edit.js";
|
|
3
|
+
import { createMemorySaveTool } from "./memory-save.js";
|
|
4
|
+
import { createReadTool } from "./read.js";
|
|
5
|
+
import { createSessionSearchTool } from "./session-search.js";
|
|
6
|
+
import { createSkillListTool } from "./skill-list.js";
|
|
7
|
+
import { createSkillManageTool } from "./skill-manage.js";
|
|
8
|
+
import { createSkillViewTool } from "./skill-view.js";
|
|
9
|
+
import { createWebFetchTool } from "./web-fetch.js";
|
|
10
|
+
import { createWebSearchTool } from "./web-search.js";
|
|
11
|
+
import { createWriteTool } from "./write.js";
|
|
12
|
+
function req(value, name) {
|
|
13
|
+
if (value === undefined) {
|
|
14
|
+
throw new Error(`Tool build context is missing required dependency "${name}"`);
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
function fileToolOptions(ctx) {
|
|
19
|
+
return {
|
|
20
|
+
securityConfig: ctx.securityConfig,
|
|
21
|
+
securityContext: ctx.securityContext,
|
|
22
|
+
channelId: ctx.channelId,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function webEnabled(ctx) {
|
|
26
|
+
return ctx.webConfig != null && ctx.webConfig.enable !== false;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Single source of truth for the agent's leaf tools. The `subagent` tool is intentionally
|
|
30
|
+
* NOT here: it is never available to sub-agents, needs runtime context the registry does
|
|
31
|
+
* not model, and keeping it out avoids a `registry` ↔ `subagents/tool` import cycle (the
|
|
32
|
+
* sub-agent tool set is built by importing `buildToolSet` from this module).
|
|
33
|
+
*/
|
|
34
|
+
export const TOOL_REGISTRY = [
|
|
35
|
+
{
|
|
36
|
+
name: "read",
|
|
37
|
+
promptHint: "Read files",
|
|
38
|
+
availableToSubagents: true,
|
|
39
|
+
create: (ctx) => createReadTool(ctx.executor, fileToolOptions(ctx)),
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: "bash",
|
|
43
|
+
promptHint: "Run shell commands and external programs",
|
|
44
|
+
availableToSubagents: true,
|
|
45
|
+
create: (ctx) => createBashTool(ctx.executor, {
|
|
46
|
+
...fileToolOptions(ctx),
|
|
47
|
+
...(ctx.bashDefaultTimeoutSeconds !== undefined
|
|
48
|
+
? { defaultTimeoutSeconds: ctx.bashDefaultTimeoutSeconds }
|
|
49
|
+
: {}),
|
|
50
|
+
}),
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: "edit",
|
|
54
|
+
promptHint: "Surgical file edits",
|
|
55
|
+
availableToSubagents: true,
|
|
56
|
+
create: (ctx) => createEditTool(ctx.executor, fileToolOptions(ctx)),
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "write",
|
|
60
|
+
promptHint: "Create or overwrite files when needed",
|
|
61
|
+
availableToSubagents: true,
|
|
62
|
+
create: (ctx) => createWriteTool(ctx.executor, fileToolOptions(ctx)),
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "web_search",
|
|
66
|
+
promptHint: "Search the public web and return titles, URLs, and snippets",
|
|
67
|
+
availableToSubagents: true,
|
|
68
|
+
enabledBy: webEnabled,
|
|
69
|
+
create: (ctx) => createWebSearchTool({
|
|
70
|
+
webConfig: req(ctx.webConfig, "webConfig"),
|
|
71
|
+
securityConfig: ctx.securityConfig,
|
|
72
|
+
workspaceDir: ctx.workspaceDir,
|
|
73
|
+
channelId: ctx.channelId,
|
|
74
|
+
}),
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: "web_fetch",
|
|
78
|
+
promptHint: "Fetch a public URL and extract readable content",
|
|
79
|
+
availableToSubagents: true,
|
|
80
|
+
enabledBy: webEnabled,
|
|
81
|
+
create: (ctx) => createWebFetchTool({
|
|
82
|
+
webConfig: req(ctx.webConfig, "webConfig"),
|
|
83
|
+
securityConfig: ctx.securityConfig,
|
|
84
|
+
workspaceDir: ctx.workspaceDir,
|
|
85
|
+
channelId: ctx.channelId,
|
|
86
|
+
}),
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: "session_search",
|
|
90
|
+
promptHint: "Search current-channel cold transcript storage for older conversation details",
|
|
91
|
+
availableToSubagents: false,
|
|
92
|
+
enabledBy: (ctx) => ctx.toolsConfig?.tools.memory.sessionSearch.enabled !== false,
|
|
93
|
+
create: (ctx) => createSessionSearchTool({
|
|
94
|
+
channelId: ctx.channelId,
|
|
95
|
+
channelDir: ctx.channelDir,
|
|
96
|
+
getCurrentModel: req(ctx.getCurrentModel, "getCurrentModel"),
|
|
97
|
+
resolveApiKey: req(ctx.resolveApiKey, "resolveApiKey"),
|
|
98
|
+
getSessionSearchSettings: req(ctx.getSessionSearchSettings, "getSessionSearchSettings"),
|
|
99
|
+
}),
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
name: "memory_save",
|
|
103
|
+
promptHint: "Save a durable fact to this channel's long-term memory immediately when the user asks",
|
|
104
|
+
availableToSubagents: false,
|
|
105
|
+
enabledBy: (ctx) => ctx.toolsConfig?.tools.memory.save.enabled !== false,
|
|
106
|
+
create: (ctx) => createMemorySaveTool({
|
|
107
|
+
channelId: ctx.channelId,
|
|
108
|
+
channelDir: ctx.channelDir,
|
|
109
|
+
memoryCandidateStore: req(ctx.memoryCandidateStore, "memoryCandidateStore"),
|
|
110
|
+
}),
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
name: "skill_list",
|
|
114
|
+
promptHint: "List workspace-level procedural memory in skills/",
|
|
115
|
+
availableToSubagents: false,
|
|
116
|
+
enabledBy: (ctx) => ctx.toolsConfig?.tools.skills.manage.enabled !== false,
|
|
117
|
+
create: (ctx) => createSkillListTool({ workspaceDir: ctx.workspaceDir, workspacePath: ctx.workspacePath }),
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
name: "skill_view",
|
|
121
|
+
promptHint: "Inspect a workspace-level skill's contents",
|
|
122
|
+
availableToSubagents: false,
|
|
123
|
+
enabledBy: (ctx) => ctx.toolsConfig?.tools.skills.manage.enabled !== false,
|
|
124
|
+
create: (ctx) => createSkillViewTool({ workspaceDir: ctx.workspaceDir, workspacePath: ctx.workspacePath }),
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: "skill_manage",
|
|
128
|
+
promptHint: "Create or maintain workspace-level procedural memory in skills/",
|
|
129
|
+
availableToSubagents: false,
|
|
130
|
+
enabledBy: (ctx) => ctx.toolsConfig?.tools.skills.manage.enabled !== false,
|
|
131
|
+
create: (ctx) => createSkillManageTool({ workspaceDir: ctx.workspaceDir, workspacePath: ctx.workspacePath }),
|
|
132
|
+
},
|
|
133
|
+
];
|
|
134
|
+
/** Prompt hint for the `subagent` tool, which is registered outside TOOL_REGISTRY. */
|
|
135
|
+
export const SUBAGENT_TOOL_HINT = "Delegate a focused task to a sub-agent with its own isolated context";
|
|
136
|
+
/** Single source of truth for prompt hints, keyed by tool name (leaf tools + subagent). */
|
|
137
|
+
export const TOOL_PROMPT_HINTS = {
|
|
138
|
+
...Object.fromEntries(TOOL_REGISTRY.map((registration) => [registration.name, registration.promptHint])),
|
|
139
|
+
subagent: SUBAGENT_TOOL_HINT,
|
|
140
|
+
};
|
|
141
|
+
/** Build a tool set from the registry, honoring sub-agent availability and config gates. */
|
|
142
|
+
export function buildToolSet(ctx, options = {}) {
|
|
143
|
+
const result = [];
|
|
144
|
+
for (const registration of TOOL_REGISTRY) {
|
|
145
|
+
if (options.forSubagent && !registration.availableToSubagents) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (registration.enabledBy && !registration.enabledBy(ctx)) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
result.push(registration.create(ctx));
|
|
152
|
+
}
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
@@ -5,10 +5,11 @@ import type { PipiclawSessionSearchSettings } from "../settings.js";
|
|
|
5
5
|
declare const sessionSearchSchema: Type.TObject<{
|
|
6
6
|
label: Type.TString;
|
|
7
7
|
query: Type.TOptional<Type.TString>;
|
|
8
|
-
limit: Type.TOptional<Type.
|
|
9
|
-
roleFilter: Type.TOptional<Type.TArray<Type.
|
|
8
|
+
limit: Type.TOptional<Type.TInteger>;
|
|
9
|
+
roleFilter: Type.TOptional<Type.TArray<Type.TUnion<[Type.TLiteral<"user">, Type.TLiteral<"assistant">, Type.TLiteral<"tool">, Type.TLiteral<"system">, Type.TLiteral<"unknown">]>>>;
|
|
10
10
|
}>;
|
|
11
11
|
export interface SessionSearchToolOptions {
|
|
12
|
+
channelId?: string;
|
|
12
13
|
channelDir: string;
|
|
13
14
|
getCurrentModel: () => Model<Api>;
|
|
14
15
|
resolveApiKey: (model: Model<Api>) => Promise<string>;
|
|
@@ -5,10 +5,14 @@ const sessionSearchSchema = Type.Object({
|
|
|
5
5
|
query: Type.Optional(Type.String({
|
|
6
6
|
description: "Search query for current-channel transcript cold storage. Empty query returns recent entries.",
|
|
7
7
|
})),
|
|
8
|
-
limit: Type.Optional(Type.
|
|
9
|
-
roleFilter: Type.Optional(Type.Array(Type.
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 5, description: "Maximum results to return (1-5)" })),
|
|
9
|
+
roleFilter: Type.Optional(Type.Array(Type.Union([
|
|
10
|
+
Type.Literal("user"),
|
|
11
|
+
Type.Literal("assistant"),
|
|
12
|
+
Type.Literal("tool"),
|
|
13
|
+
Type.Literal("system"),
|
|
14
|
+
Type.Literal("unknown"),
|
|
15
|
+
]), { description: "Optional roles to include." })),
|
|
12
16
|
});
|
|
13
17
|
function clampLimit(limit) {
|
|
14
18
|
if (typeof limit !== "number" || !Number.isFinite(limit)) {
|
|
@@ -26,6 +30,7 @@ export function createSessionSearchTool(options) {
|
|
|
26
30
|
const settings = options.getSessionSearchSettings();
|
|
27
31
|
const model = options.getCurrentModel();
|
|
28
32
|
const response = await searchChannelSessions({
|
|
33
|
+
channelId: options.channelId,
|
|
29
34
|
channelDir: options.channelDir,
|
|
30
35
|
query: query ?? "",
|
|
31
36
|
roleFilter,
|
|
@@ -42,7 +47,7 @@ export function createSessionSearchTool(options) {
|
|
|
42
47
|
content: [
|
|
43
48
|
{
|
|
44
49
|
type: "text",
|
|
45
|
-
text: JSON.stringify(response
|
|
50
|
+
text: JSON.stringify(response),
|
|
46
51
|
},
|
|
47
52
|
],
|
|
48
53
|
details: {
|
package/dist/tools/skill-list.js
CHANGED
|
@@ -78,7 +78,7 @@ export function createSkillListTool(options) {
|
|
|
78
78
|
execute: async () => {
|
|
79
79
|
const skills = await listWorkspaceSkills(options);
|
|
80
80
|
return {
|
|
81
|
-
content: [{ type: "text", text: JSON.stringify({ skills }
|
|
81
|
+
content: [{ type: "text", text: JSON.stringify({ skills }) }],
|
|
82
82
|
details: { kind: "skill_list", count: skills.length },
|
|
83
83
|
};
|
|
84
84
|
},
|
|
@@ -2,7 +2,7 @@ import type { AgentTool } from "@earendil-works/pi-agent-core";
|
|
|
2
2
|
import { Type } from "typebox";
|
|
3
3
|
declare const skillManageSchema: Type.TObject<{
|
|
4
4
|
label: Type.TString;
|
|
5
|
-
action: Type.
|
|
5
|
+
action: Type.TUnion<[Type.TLiteral<"create">, Type.TLiteral<"patch">, Type.TLiteral<"write_file">]>;
|
|
6
6
|
name: Type.TString;
|
|
7
7
|
content: Type.TOptional<Type.TString>;
|
|
8
8
|
filePath: Type.TOptional<Type.TString>;
|
|
@@ -6,7 +6,9 @@ import { createAtomicTempPath, writeFileAtomically } from "../shared/atomic-file
|
|
|
6
6
|
import { resolveSkillPath, resolveSkillSupportingFile, scanSkillContent, validateSkillMarkdown, } from "./skill-security.js";
|
|
7
7
|
const skillManageSchema = Type.Object({
|
|
8
8
|
label: Type.String({ description: "Brief description of the skill management change (shown to user)" }),
|
|
9
|
-
action: Type.
|
|
9
|
+
action: Type.Union([Type.Literal("create"), Type.Literal("patch"), Type.Literal("write_file")], {
|
|
10
|
+
description: 'The skill management action to perform: "create", "patch", or "write_file".',
|
|
11
|
+
}),
|
|
10
12
|
name: Type.String({ description: "Workspace skill name" }),
|
|
11
13
|
content: Type.Optional(Type.String({ description: "Full content for create/write_file." })),
|
|
12
14
|
filePath: Type.Optional(Type.String({
|
package/dist/tools/skill-view.js
CHANGED
|
@@ -2,6 +2,7 @@ import { readFile } from "node:fs/promises";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { Type } from "typebox";
|
|
4
4
|
import { resolveSkillPath, resolveSkillSupportingFile } from "./skill-security.js";
|
|
5
|
+
import { DEFAULT_MAX_BYTES, formatSize, truncateHead } from "./truncate.js";
|
|
5
6
|
const skillViewSchema = Type.Object({
|
|
6
7
|
label: Type.String({ description: "Brief description of why you're viewing this skill (shown to user)" }),
|
|
7
8
|
name: Type.String({ description: "Workspace skill name" }),
|
|
@@ -24,19 +25,19 @@ export function createSkillViewTool(options) {
|
|
|
24
25
|
execute: async (_toolCallId, { name, filePath }) => {
|
|
25
26
|
const skillDir = resolveSkillPath(options.workspaceDir, name);
|
|
26
27
|
const targetPath = filePath ? resolveSkillSupportingFile(skillDir, filePath) : join(skillDir, "SKILL.md");
|
|
28
|
+
const workspacePath = toWorkspacePath(options, targetPath);
|
|
27
29
|
const content = await readFile(targetPath, "utf-8");
|
|
30
|
+
// Return the raw file content (not JSON-escaped) with a small header, and cap it
|
|
31
|
+
// with the shared truncation limits so a large supporting file cannot flood context.
|
|
32
|
+
const truncation = truncateHead(content);
|
|
33
|
+
let body = truncation.content;
|
|
34
|
+
if (truncation.truncated) {
|
|
35
|
+
body += `\n\n[Truncated at ${formatSize(DEFAULT_MAX_BYTES)}. Use the read tool on ${workspacePath} to page through the rest.]`;
|
|
36
|
+
}
|
|
37
|
+
const text = `Skill: ${name}\nPath: ${workspacePath}\n\n${body}`;
|
|
28
38
|
return {
|
|
29
|
-
content: [
|
|
30
|
-
|
|
31
|
-
type: "text",
|
|
32
|
-
text: JSON.stringify({
|
|
33
|
-
name,
|
|
34
|
-
path: toWorkspacePath(options, targetPath),
|
|
35
|
-
content,
|
|
36
|
-
}, null, 2),
|
|
37
|
-
},
|
|
38
|
-
],
|
|
39
|
-
details: { kind: "skill_view", name, path: toWorkspacePath(options, targetPath) },
|
|
39
|
+
content: [{ type: "text", text }],
|
|
40
|
+
details: { kind: "skill_view", name, path: workspacePath, truncated: truncation.truncated },
|
|
40
41
|
};
|
|
41
42
|
},
|
|
42
43
|
};
|
|
@@ -5,7 +5,7 @@ import type { PipiclawWebToolsConfig } from "./config.js";
|
|
|
5
5
|
declare const webSearchSchema: Type.TObject<{
|
|
6
6
|
label: Type.TString;
|
|
7
7
|
query: Type.TString;
|
|
8
|
-
count: Type.TOptional<Type.
|
|
8
|
+
count: Type.TOptional<Type.TInteger>;
|
|
9
9
|
}>;
|
|
10
10
|
export interface WebSearchToolOptions {
|
|
11
11
|
webConfig: PipiclawWebToolsConfig;
|
package/dist/tools/web-search.js
CHANGED
|
@@ -4,7 +4,7 @@ import { runWebSearch } from "../web/search.js";
|
|
|
4
4
|
const webSearchSchema = Type.Object({
|
|
5
5
|
label: Type.String({ description: "Brief description of what you're searching for and why (shown to user)" }),
|
|
6
6
|
query: Type.String({ description: "Search query" }),
|
|
7
|
-
count: Type.Optional(Type.
|
|
7
|
+
count: Type.Optional(Type.Integer({ minimum: 1, maximum: 10, description: "Maximum number of results to return (1-10)" })),
|
|
8
8
|
});
|
|
9
9
|
export function createWebSearchTool(options) {
|
|
10
10
|
return {
|