File size: 6,117 Bytes
fcd8223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Bundles plugin command metadata for package output.
import fs from "node:fs";
import path from "node:path";
import {
  normalizeOptionalLowercaseString,
  normalizeOptionalString,
} from "@openclaw/normalization-core/string-coerce";
import {
  parseFrontmatterBlock,
  stripFrontmatterBlock,
} from "../../packages/markdown-core/src/frontmatter.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { formatErrorMessage } from "../infra/errors.js";
import { readRootJsonObjectSync } from "../infra/json-files.js";
import { readRegularFileSync } from "../infra/regular-file.js";
import { createSubsystemLogger } from "../logging/subsystem.js";
import { isPathInsideWithRealpath } from "../security/scan-paths.js";
import { parseFrontmatterBool } from "../shared/frontmatter.js";
import {
  CLAUDE_BUNDLE_MANIFEST_RELATIVE_PATH,
  mergeBundlePathLists,
  normalizeBundlePathList,
} from "./bundle-manifest.js";
import {
  hasExplicitPluginConfig,
  normalizePluginsConfig,
  resolveEffectivePluginActivationState,
} from "./config-state.js";
import { loadPluginManifestRegistryForPluginRegistry } from "./plugin-registry-contributions.js";

type ClaudeBundleCommandSpec = {
  pluginId: string;
  rawName: string;
  description: string;
  promptTemplate: string;
  sourceFilePath: string;
};

const BUNDLE_COMMAND_MAX_BYTES = 1 * 1024 * 1024;
const log = createSubsystemLogger("plugins/bundle-commands");

function readClaudeBundleManifest(rootDir: string): Record<string, unknown> {
  const result = readRootJsonObjectSync({
    rootDir,
    relativePath: CLAUDE_BUNDLE_MANIFEST_RELATIVE_PATH,
    boundaryLabel: "plugin root",
    rejectHardlinks: true,
  });
  return result.ok ? result.value : {};
}

function resolveClaudeCommandRootDirs(rootDir: string): string[] {
  const raw = readClaudeBundleManifest(rootDir);
  const declared = normalizeBundlePathList(raw.commands);
  const defaults = fs.existsSync(path.join(rootDir, "commands")) ? ["commands"] : [];
  return mergeBundlePathLists(defaults, declared);
}

function listMarkdownFilesRecursive(rootDir: string): string[] {
  const pending = [rootDir];
  const files: string[] = [];
  while (pending.length > 0) {
    const current = pending.pop();
    if (!current) {
      continue;
    }
    let entries: fs.Dirent[];
    try {
      entries = fs.readdirSync(current, { withFileTypes: true });
    } catch {
      continue;
    }
    for (const entry of entries) {
      if (entry.name.startsWith(".")) {
        continue;
      }
      const fullPath = path.join(current, entry.name);
      if (entry.isDirectory()) {
        pending.push(fullPath);
        continue;
      }
      if (entry.isFile() && normalizeOptionalLowercaseString(entry.name)?.endsWith(".md")) {
        files.push(fullPath);
      }
    }
  }
  return files.toSorted((a, b) => a.localeCompare(b));
}

function toDefaultCommandName(rootDir: string, filePath: string): string {
  const relativePath = path.relative(rootDir, filePath);
  const withoutExt = relativePath.replace(/\.[^.]+$/u, "");
  return withoutExt.split(path.sep).join(":");
}

function toDefaultDescription(promptTemplate: string): string {
  // stripFrontmatterBlock already normalized line endings and trimmed the nonempty body.
  const lineEnd = promptTemplate.indexOf("\n");
  return (lineEnd < 0 ? promptTemplate : promptTemplate.slice(0, lineEnd)).trimEnd();
}

function loadBundleCommandsFromRoot(params: {
  pluginId: string;
  commandRoot: string;
}): ClaudeBundleCommandSpec[] {
  const entries: ClaudeBundleCommandSpec[] = [];
  for (const filePath of listMarkdownFilesRecursive(params.commandRoot)) {
    let raw: string;
    try {
      raw = readRegularFileSync({ filePath, maxBytes: BUNDLE_COMMAND_MAX_BYTES }).buffer.toString(
        "utf-8",
      );
    } catch (error) {
      log.warn(`skipping unreadable bundle command file ${filePath}: ${formatErrorMessage(error)}`);
      continue;
    }
    const frontmatter = parseFrontmatterBlock(raw);
    if (!parseFrontmatterBool(frontmatter["user-invocable"], true)) {
      continue;
    }
    const promptTemplate = stripFrontmatterBlock(raw);
    if (!promptTemplate) {
      continue;
    }
    const rawName =
      normalizeOptionalString(frontmatter.name) ||
      toDefaultCommandName(params.commandRoot, filePath);
    if (!rawName) {
      continue;
    }
    const description =
      normalizeOptionalString(frontmatter.description) || toDefaultDescription(promptTemplate);
    entries.push({
      pluginId: params.pluginId,
      rawName,
      description,
      promptTemplate,
      sourceFilePath: filePath,
    });
  }
  return entries;
}

export function loadEnabledClaudeBundleCommands(params: {
  workspaceDir: string;
  cfg?: OpenClawConfig;
}): ClaudeBundleCommandSpec[] {
  if (!hasExplicitPluginConfig(params.cfg?.plugins)) {
    return [];
  }
  const registry = loadPluginManifestRegistryForPluginRegistry({
    workspaceDir: params.workspaceDir,
    config: params.cfg,
    includeDisabled: true,
  });
  const normalizedPlugins = normalizePluginsConfig(params.cfg?.plugins);
  const commands: ClaudeBundleCommandSpec[] = [];

  for (const record of registry.plugins) {
    if (
      record.format !== "bundle" ||
      record.bundleFormat !== "claude" ||
      !(record.bundleCapabilities ?? []).includes("commands")
    ) {
      continue;
    }
    const activationState = resolveEffectivePluginActivationState({
      id: record.id,
      origin: record.origin,
      channelIds: record.channels,
      config: normalizedPlugins,
      rootConfig: params.cfg,
    });
    if (!activationState.activated) {
      continue;
    }
    for (const relativeRoot of resolveClaudeCommandRootDirs(record.rootDir)) {
      const commandRoot = path.resolve(record.rootDir, relativeRoot);
      if (!fs.existsSync(commandRoot)) {
        continue;
      }
      if (!isPathInsideWithRealpath(record.rootDir, commandRoot, { requireRealpath: true })) {
        continue;
      }
      commands.push(...loadBundleCommandsFromRoot({ pluginId: record.id, commandRoot }));
    }
  }

  return commands;
}