@oyasmi/pipiclaw 0.7.3 → 0.7.4

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.
@@ -49,6 +49,7 @@ export interface SubAgentToolOptions {
49
49
  memoryCandidateStore?: MemoryCandidateStore;
50
50
  securityConfig?: SecurityConfig;
51
51
  webConfig?: PipiclawWebToolsConfig;
52
+ rtkEnabled?: boolean;
52
53
  runtimeContext: {
53
54
  workspacePath: string;
54
55
  channelId: string;
@@ -103,6 +103,7 @@ function buildSubagentTools(executor, bashTimeoutSec, options) {
103
103
  workspaceDir: options.workspaceDir,
104
104
  workspacePath: options.runtimeContext.workspacePath,
105
105
  webConfig: options.webConfig,
106
+ rtkEnabled: options.rtkEnabled,
106
107
  bashDefaultTimeoutSeconds: bashTimeoutSec,
107
108
  }, { forSubagent: true });
108
109
  }
@@ -19,6 +19,12 @@ export interface BashToolOptions {
19
19
  securityConfig?: SecurityConfig;
20
20
  securityContext?: SecurityRuntimeContext;
21
21
  channelId?: string;
22
+ /**
23
+ * When true, route each command through the `rtk` command optimizer before executing
24
+ * (best-effort; falls back to the raw command when rtk is unavailable or declines).
25
+ * Gated by `tools.rtk.enabled` in tools.json.
26
+ */
27
+ rtkEnabled?: boolean;
22
28
  }
23
29
  export declare function createBashTool(executor: Executor, options?: BashToolOptions): AgentTool<typeof bashSchema>;
24
30
  export {};
@@ -4,6 +4,7 @@ import { guardCommand } from "../security/command-guard.js";
4
4
  import { DEFAULT_SECURITY_CONFIG } from "../security/config.js";
5
5
  import { logSecurityEvent } from "../security/logger.js";
6
6
  import { shellEscape } from "../shared/shell-escape.js";
7
+ import { maybeOptimizeCommand } from "./command-optimizer.js";
7
8
  import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, truncateTail } from "./truncate.js";
8
9
  /**
9
10
  * Default wall-clock timeout for bash commands when the caller supplies neither a
@@ -72,8 +73,12 @@ export function createBashTool(executor, options = {}) {
72
73
  throw new Error(formatCommandBlockMessage(command, guardResult.category, guardResult.reason, guardResult.matchedText));
73
74
  }
74
75
  }
76
+ // Optimize *after* the security guard: the guard must inspect the operator's real
77
+ // intent (`command`), while rtk only reshapes a semantically-equivalent command for
78
+ // compact output. Optimizing first would hide the true command from the guard.
79
+ const effectiveCommand = options.rtkEnabled ? await maybeOptimizeCommand(command, executor, signal) : command;
75
80
  const effectiveTimeout = timeout ?? options.defaultTimeoutSeconds ?? DEFAULT_BASH_TIMEOUT_SECONDS;
76
- const result = await executor.exec(command, { timeout: effectiveTimeout, signal });
81
+ const result = await executor.exec(effectiveCommand, { timeout: effectiveTimeout, signal });
77
82
  let output = "";
78
83
  if (result.stdout)
79
84
  output += result.stdout;
@@ -0,0 +1,6 @@
1
+ import type { Executor } from "../sandbox.js";
2
+ /**
3
+ * Return the rtk-optimized form of `command`, or `command` unchanged when rtk is
4
+ * unavailable or declines to rewrite it. Never throws.
5
+ */
6
+ export declare function maybeOptimizeCommand(command: string, executor: Executor, signal?: AbortSignal): Promise<string>;
@@ -0,0 +1,73 @@
1
+ import { shellEscape } from "../shared/shell-escape.js";
2
+ /**
3
+ * Optional "outbound command transform" for the bash tool. When enabled, a command is
4
+ * handed to the `rtk` binary (https://github.com/rtk-ai/rtk) which rewrites known
5
+ * read-only commands to their token-compact equivalents (e.g. `git status` →
6
+ * `rtk git status`) before execution, cutting the output the model has to read.
7
+ *
8
+ * pipiclaw owns none of rtk's rules: `rtk rewrite` is rtk's single source of truth for
9
+ * hooks. It prints the rewritten command on stdout when it can optimize, and prints
10
+ * nothing when it cannot. We key off stdout, not the exit code: the exit code is not a
11
+ * reliable success signal across rtk versions — 0.43.0 exits 3 on a successful rewrite
12
+ * and 1 on "no equivalent", while its own `--help` claims 0/1. Presence of stdout is the
13
+ * stable contract, so a non-empty line is the rewrite and empty means "keep original".
14
+ *
15
+ * This is best-effort by contract: any failure (rtk absent, timeout, empty output) falls
16
+ * back to the original command. Enabling rtk must never make a bash command fail.
17
+ */
18
+ /**
19
+ * rtk rewrite is a pure, local string transform (no network), so a tight bound is safe.
20
+ * On timeout we simply run the original command.
21
+ */
22
+ const RTK_TIMEOUT_SECONDS = 2;
23
+ /**
24
+ * rtk must exist in the *execution environment* — the host PATH for a host executor, or
25
+ * inside the container for a docker executor — not merely on the pipiclaw host. We probe
26
+ * through the executor so the check reflects where the command will actually run, and
27
+ * memoize the probe promise per executor instance so concurrent turns share one probe and
28
+ * we do not re-shell on every command. The instance is stable for a channel's lifetime,
29
+ * so this is effectively "probe once".
30
+ */
31
+ const availabilityByExecutor = new WeakMap();
32
+ function probeRtk(executor) {
33
+ return executor
34
+ .exec("command -v rtk", { timeout: RTK_TIMEOUT_SECONDS })
35
+ .then((result) => result.code === 0 && result.stdout.trim().length > 0)
36
+ .catch(() => false);
37
+ }
38
+ function isRtkAvailable(executor) {
39
+ let probe = availabilityByExecutor.get(executor);
40
+ if (!probe) {
41
+ probe = probeRtk(executor);
42
+ availabilityByExecutor.set(executor, probe);
43
+ }
44
+ return probe;
45
+ }
46
+ /**
47
+ * Return the rtk-optimized form of `command`, or `command` unchanged when rtk is
48
+ * unavailable or declines to rewrite it. Never throws.
49
+ */
50
+ export async function maybeOptimizeCommand(command, executor, signal) {
51
+ if (signal?.aborted) {
52
+ return command;
53
+ }
54
+ if (!(await isRtkAvailable(executor))) {
55
+ return command;
56
+ }
57
+ try {
58
+ const result = await executor.exec(`rtk rewrite ${shellEscape(command)}`, {
59
+ timeout: RTK_TIMEOUT_SECONDS,
60
+ signal,
61
+ });
62
+ // Trust stdout over the exit code (see the module comment): a non-empty line is the
63
+ // rewrite; empty output means rtk had no equivalent, so keep the original command.
64
+ const rewritten = result.stdout.trim();
65
+ if (rewritten.length > 0) {
66
+ return rewritten;
67
+ }
68
+ }
69
+ catch {
70
+ // Any failure — spawn error, timeout, abort — falls back to the original command.
71
+ }
72
+ return command;
73
+ }
@@ -38,6 +38,9 @@ export interface PipiclawToolsConfig {
38
38
  enabled: boolean;
39
39
  };
40
40
  };
41
+ rtk: {
42
+ enabled: boolean;
43
+ };
41
44
  };
42
45
  }
43
46
  export interface LoadedToolsConfig {
@@ -38,6 +38,9 @@ export const DEFAULT_TOOLS_CONFIG = {
38
38
  enabled: true,
39
39
  },
40
40
  },
41
+ rtk: {
42
+ enabled: false,
43
+ },
41
44
  },
42
45
  };
43
46
  function clampInteger(value, fallback, minimum, maximum) {
@@ -86,6 +89,7 @@ function mergeToolsConfig(source, configPath, diagnostics) {
86
89
  const memorySave = isRecord(memory.save) ? memory.save : {};
87
90
  const skills = isRecord(tools.skills) ? tools.skills : {};
88
91
  const manage = isRecord(skills.manage) ? skills.manage : {};
92
+ const rtk = isRecord(tools.rtk) ? tools.rtk : {};
89
93
  const search = isRecord(web.search) ? web.search : {};
90
94
  const fetch = isRecord(web.fetch) ? web.fetch : {};
91
95
  const providerValue = asTrimmedString(search.provider, DEFAULT_TOOLS_CONFIG.tools.web.search.provider).toLowerCase();
@@ -169,6 +173,9 @@ function mergeToolsConfig(source, configPath, diagnostics) {
169
173
  : DEFAULT_TOOLS_CONFIG.tools.skills.manage.enabled,
170
174
  },
171
175
  },
176
+ rtk: {
177
+ enabled: typeof rtk.enabled === "boolean" ? rtk.enabled : DEFAULT_TOOLS_CONFIG.tools.rtk.enabled,
178
+ },
172
179
  },
173
180
  };
174
181
  }
@@ -25,6 +25,7 @@ export function createPipiclawTools(options) {
25
25
  workspacePath: options.workspacePath,
26
26
  webConfig: toolsConfig.tools.web,
27
27
  toolsConfig,
28
+ rtkEnabled: toolsConfig.tools.rtk.enabled,
28
29
  getCurrentModel: options.getCurrentModel,
29
30
  getAvailableModels: options.getAvailableModels,
30
31
  resolveApiKey: options.resolveApiKey,
@@ -45,6 +46,7 @@ export function createPipiclawTools(options) {
45
46
  memoryCandidateStore: options.memoryCandidateStore,
46
47
  securityConfig,
47
48
  webConfig: toolsConfig.tools.web,
49
+ rtkEnabled: toolsConfig.tools.rtk.enabled,
48
50
  runtimeContext: {
49
51
  workspacePath: options.workspacePath,
50
52
  channelId: options.channelId,
@@ -28,6 +28,8 @@ export interface ToolBuildContext {
28
28
  toolsConfig?: PipiclawToolsConfig;
29
29
  /** Sub-agent set passes its per-invocation bash timeout; the main set relies on the built-in default. */
30
30
  bashDefaultTimeoutSeconds?: number;
31
+ /** Gates the bash tool's rtk command optimizer (`tools.rtk.enabled`). Threaded to both sets. */
32
+ rtkEnabled?: boolean;
31
33
  getCurrentModel?: () => Model<Api>;
32
34
  getAvailableModels?: () => Model<Api>[];
33
35
  resolveApiKey?: (model: Model<Api>) => Promise<string>;
@@ -44,6 +44,7 @@ export const TOOL_REGISTRY = [
44
44
  availableToSubagents: true,
45
45
  create: (ctx) => createBashTool(ctx.executor, {
46
46
  ...fileToolOptions(ctx),
47
+ rtkEnabled: ctx.rtkEnabled === true,
47
48
  ...(ctx.bashDefaultTimeoutSeconds !== undefined
48
49
  ? { defaultTimeoutSeconds: ctx.bashDefaultTimeoutSeconds }
49
50
  : {}),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oyasmi/pipiclaw",
3
- "version": "0.7.3",
3
+ "version": "0.7.4",
4
4
  "description": "An AI assistant runtime for coding and team workflows, with DingTalk AI Cards, sub-agents, memory, and scheduled events.",
5
5
  "type": "module",
6
6
  "bin": {