File size: 11,484 Bytes
d197cf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { createHash } from "node:crypto";
import { stableStringify } from "@openclaw/normalization-core";
import { inspectModelReference } from "../commands/models/model-reference-validation.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import type {
  ClawAddCapabilityChange,
  ClawAddPlanAction,
  ClawDiagnostic,
  ClawExtensionPlan,
  ClawLocalPrerequisite,
  ClawOpenClawExtension,
  ClawOpenClawProfile,
  ClawPackage,
  ClawPackagePreflight,
  ClawPackagePreflightResult,
} from "./types.js";

export function clawAddCapabilityChange(
  change: Omit<ClawAddCapabilityChange, "classification" | "requiresDistinctConsent" | "digest">,
): ClawAddCapabilityChange {
  return {
    ...change,
    classification: "escalation",
    requiresDistinctConsent: true,
    digest: `sha256:${createHash("sha256").update(stableStringify(change.effect)).digest("hex")}`,
  };
}

export function clawAgentCapabilityChange(
  agentId: string,
  settings: ClawOpenClawProfile["agent"],
): ClawAddCapabilityChange | undefined {
  const effect = {
    ...(settings.model ? { model: settings.model } : {}),
    ...(settings.subagents ? { subagents: settings.subagents } : {}),
    ...(settings.sandbox ? { sandbox: settings.sandbox } : {}),
    ...(settings.tools ? { tools: settings.tools } : {}),
    ...(settings.memory ? { memory: settings.memory } : {}),
    ...(settings.heartbeat ? { heartbeat: settings.heartbeat } : {}),
  };
  if (Object.keys(effect).length === 0) {
    return undefined;
  }
  return clawAddCapabilityChange({
    kind: "agent",
    id: agentId,
    path: "agent",
    action: "create",
    reason:
      settings.model || settings.subagents
        ? "The new agent declares model, delegation, sandbox, tool, memory-search, or recurring heartbeat configuration."
        : "The new agent declares sandbox, tool, memory-search, or recurring heartbeat capabilities.",
    effect,
  });
}

export function clawAgentConfigurationNotices(
  agent: ClawOpenClawProfile["agent"],
  config: OpenClawConfig,
  agentIds: ReadonlySet<string>,
): ClawDiagnostic[] {
  const notices: ClawDiagnostic[] = [];
  const notice = (code: string, path: string, message: string) => {
    notices.push({
      level: "warning",
      phase: "plan",
      code,
      path: `$.profiles.openclaw.agent.${path}`,
      message,
    });
  };
  for (const [index, target] of (agent.subagents?.allowAgents ?? []).entries()) {
    if (!agentIds.has(target)) {
      notice(
        "delegation_target_unresolved",
        `subagents.allowAgents[${index}]`,
        `Delegation target ${JSON.stringify(target)} is not in the local agent roster; it will be applied as declared. Install that agent before delegating to it.`,
      );
    }
  }
  const refs = agent.model ? [agent.model.primary, ...(agent.model.fallbacks ?? [])] : [];
  for (const [index, ref] of refs.entries()) {
    const slash = ref.indexOf("/");
    const inspection = inspectModelReference({
      cfg: config,
      ref: { provider: ref.slice(0, slash), model: ref.slice(slash + 1) },
    });
    if (inspection.status !== "known") {
      notice(
        "model_not_in_catalog",
        index === 0 ? "model.primary" : `model.fallbacks[${index - 1}]`,
        `Model ${JSON.stringify(ref)} is not in the local model catalog; it will be applied as declared. Configure it before running the agent.`,
      );
    }
  }
  return notices;
}

export function clawProfileExtensionPackages(
  profile: ClawOpenClawProfile | undefined,
): ClawPackage[] {
  return (profile?.extensions ?? []).map((extension) => ({
    kind: "plugin",
    source: extension.source,
    ref: extension.ref,
    version: extension.version,
  }));
}

function blocker(code: string, path: string, message: string): ClawDiagnostic {
  return { level: "error", code, phase: "plan", path, message };
}

export function findClawExtensionPackageCollisions(params: {
  packages: ClawPackage[];
  extensions: ClawOpenClawExtension[];
}): Array<{ index: number; diagnostic: ClawDiagnostic }> {
  const declaredPackageIds = new Set(params.packages.map((pkg) => `${pkg.kind}:${pkg.ref}`));
  const collisions: Array<{ index: number; diagnostic: ClawDiagnostic }> = [];

  for (const [index, extension] of params.extensions.entries()) {
    const packageId = `plugin:${extension.ref}`;
    if (declaredPackageIds.has(packageId)) {
      collisions.push({
        index,
        diagnostic: blocker(
          "extension_package_collision",
          `$.profiles.openclaw.extensions[${index}]`,
          `Extension package ${JSON.stringify(packageId)} is already declared by the portable manifest or another profile extension.`,
        ),
      });
      continue;
    }
    declaredPackageIds.add(packageId);
  }

  return collisions;
}

function extensionCapabilityChange(params: {
  extension: ClawOpenClawExtension;
  preflight: ClawPackagePreflightResult;
}): ClawAddCapabilityChange {
  const effect = {
    id: params.extension.id,
    source: params.extension.source,
    ref: params.extension.ref,
    version: params.extension.version,
    expectedFormat: params.extension.format,
    detectedFormat: params.preflight.detectedFormat ?? "unresolved",
    integrity: params.preflight.integrity ?? "unresolved",
    mapped: params.preflight.mapped ?? [],
    unavailable: params.preflight.unavailable ?? [],
    adapterIdentity: params.preflight.adapterIdentity ?? "unresolved",
    ...(params.preflight.installId ? { installId: params.preflight.installId } : {}),
    ...(params.preflight.warning ? { riskWarning: params.preflight.warning } : {}),
  };
  const change = {
    kind: "package" as const,
    id: `extension:${params.extension.id}`,
    path: `openclaw.extensions.${params.extension.id}`,
    action: params.preflight.action === "reuse" ? ("reuse" as const) : ("install" as const),
    reason:
      params.preflight.action === "reuse"
        ? "The OpenClaw profile requires access to an existing native extension."
        : "The OpenClaw profile requires installation of native extension content or executable code.",
    effect,
  };
  return clawAddCapabilityChange(change);
}

export async function planClawExtensions(params: {
  extensions: ClawOpenClawExtension[];
  workspace: string;
  packagePreflight?: ClawPackagePreflight;
}): Promise<{
  extensions: ClawExtensionPlan[];
  actions: ClawAddPlanAction[];
  capabilityChanges: ClawAddCapabilityChange[];
  requirements: ClawLocalPrerequisite[];
  blockers: ClawDiagnostic[];
}> {
  const extensions: ClawExtensionPlan[] = [];
  const actions: ClawAddPlanAction[] = [];
  const capabilityChanges: ClawAddCapabilityChange[] = [];
  const requirements: ClawLocalPrerequisite[] = [];
  const blockers: ClawDiagnostic[] = [];

  for (const [index, extension] of params.extensions.entries()) {
    const preflight: ClawPackagePreflightResult = params.packagePreflight
      ? await params.packagePreflight(
          {
            kind: "plugin",
            source: extension.source,
            ref: extension.ref,
            version: extension.version,
          },
          params.workspace,
        )
      : {
          ok: false as const,
          code: "package_install_unavailable",
          message: "Extension preflight is unavailable.",
        };
    const completeProvenance =
      preflight.ok &&
      Boolean(
        preflight.integrity &&
        preflight.installId &&
        preflight.action &&
        preflight.detectedFormat &&
        preflight.adapterIdentity,
      );
    const incompleteProvenance =
      preflight.ok && !completeProvenance
        ? blocker(
            "extension_provenance_incomplete",
            `$.profiles.openclaw.extensions[${index}]`,
            `Extension ${JSON.stringify(extension.id)} did not resolve complete canonical identity and adapter provenance.`,
          )
        : undefined;
    const formatMismatch =
      preflight.ok && completeProvenance && preflight.detectedFormat !== extension.format
        ? blocker(
            "extension_format_mismatch",
            `$.profiles.openclaw.extensions[${index}].format`,
            `Extension ${JSON.stringify(extension.id)} declares format ${JSON.stringify(extension.format)}, but the canonical plugin detector found ${JSON.stringify(preflight.detectedFormat ?? "unknown")}.`,
          )
        : undefined;
    const diagnostic = !preflight.ok
      ? blocker(
          preflight.code ?? "extension_preflight_failed",
          `$.profiles.openclaw.extensions[${index}]`,
          preflight.message ?? "Extension preflight failed.",
        )
      : (incompleteProvenance ?? formatMismatch);
    if (diagnostic) {
      blockers.push(diagnostic);
    }
    if (preflight.ok && preflight.requirements) {
      requirements.push(...preflight.requirements);
    }
    const requirementState: ClawExtensionPlan["requirementState"] = diagnostic
      ? "conflicting"
      : preflight.action === "install"
        ? "missing-installable"
        : preflight.requirements && preflight.requirements.length > 0
          ? "setup-required"
          : "satisfied";
    const extensionPlan: ClawExtensionPlan = {
      ...extension,
      ...(preflight.detectedFormat ? { detectedFormat: preflight.detectedFormat } : {}),
      ...(preflight.integrity ? { integrity: preflight.integrity } : {}),
      ...(preflight.installId ? { installId: preflight.installId } : {}),
      ...(preflight.action ? { ownerAction: preflight.action } : {}),
      requirementState,
      mapped: preflight.mapped ?? [],
      unavailable: preflight.unavailable ?? [],
      ...(preflight.adapterIdentity ? { adapterIdentity: preflight.adapterIdentity } : {}),
      blocked: Boolean(diagnostic),
    };
    extensions.push(extensionPlan);
    actions.push({
      kind: "package",
      id: `plugin:${extension.ref}`,
      action: preflight.ok && preflight.action === "reuse" ? "reuse" : "install",
      target: `${extension.source}:${extension.ref}@${extension.version}`,
      ...(preflight.integrity ? { digest: preflight.integrity } : {}),
      details: {
        kind: "plugin",
        source: extension.source,
        ref: extension.ref,
        version: extension.version,
        ...(preflight.integrity ? { integrity: preflight.integrity } : {}),
        ...(preflight.installId ? { installId: preflight.installId } : {}),
        ...(preflight.action ? { ownerAction: preflight.action } : {}),
        requirementState,
        ...(preflight.requirements ? { prerequisites: preflight.requirements } : {}),
        ...(completeProvenance
          ? {
              extension: {
                id: extension.id,
                format: extension.format,
                detectedFormat: preflight.detectedFormat!,
                mapped: preflight.mapped ?? [],
                unavailable: preflight.unavailable ?? [],
                adapterIdentity: preflight.adapterIdentity!,
              },
            }
          : {}),
        expectedState: !preflight.ok
          ? "unresolved"
          : preflight.action === "reuse"
            ? "present-exact"
            : "absent",
        ...(preflight.warning ? { riskWarning: preflight.warning } : {}),
      },
      blocked: extensionPlan.blocked,
      ...(diagnostic ? { reason: diagnostic.message } : {}),
    });
    capabilityChanges.push(extensionCapabilityChange({ extension, preflight }));
  }

  return { extensions, actions, capabilityChanges, requirements, blockers };
}