@operad-io/engine 0.1.1 → 0.2.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.
Files changed (80) hide show
  1. package/config.schema.json +2 -1
  2. package/package.json +1 -1
  3. package/src/config/config.ts +74 -45
  4. package/src/flow/types.ts +4 -0
  5. package/src/flow/validate.ts +5 -0
  6. package/src/http/index.ts +2 -0
  7. package/src/http/plugins.ts +66 -0
  8. package/src/http/server.ts +342 -48
  9. package/src/kernel/decide.ts +115 -30
  10. package/src/kernel/state.ts +9 -0
  11. package/src/kernel/types.ts +25 -40
  12. package/src/log/open-log.ts +2 -5
  13. package/src/log/sqlite.ts +127 -85
  14. package/src/log/types.ts +35 -9
  15. package/src/loop/code-watch.ts +108 -2
  16. package/src/loop/older-code.ts +3 -4
  17. package/src/loop/serve.ts +122 -95
  18. package/src/notify/notify.ts +2 -6
  19. package/src/platform/atomic-write.ts +163 -0
  20. package/src/platform/file-lock.ts +43 -21
  21. package/src/platform/index.ts +2 -0
  22. package/src/platform/kept-workers.ts +290 -0
  23. package/src/platform/process-group.ts +11 -3
  24. package/src/platform/sqlite-open.ts +20 -6
  25. package/src/platform/sqlite-schema.ts +30 -0
  26. package/src/platform/step-reap.ts +33 -18
  27. package/src/platform/watch.ts +1 -11
  28. package/src/plugin/action-schema.ts +59 -1
  29. package/src/plugin/choice-info.ts +133 -0
  30. package/src/plugin/context.ts +279 -0
  31. package/src/plugin/declared-flows.ts +3 -10
  32. package/src/plugin/errors.ts +9 -0
  33. package/src/plugin/freeze.ts +12 -0
  34. package/src/plugin/harness.ts +28 -0
  35. package/src/plugin/index.ts +8 -7
  36. package/src/plugin/install.ts +265 -17
  37. package/src/plugin/keys.ts +2 -12
  38. package/src/plugin/load.ts +47 -23
  39. package/src/plugin/logger.ts +14 -0
  40. package/src/plugin/open-store.ts +0 -6
  41. package/src/plugin/question-form.ts +323 -0
  42. package/src/plugin/registry.ts +512 -76
  43. package/src/plugin/secrets.ts +14 -24
  44. package/src/plugin/semver.ts +52 -0
  45. package/src/plugin/settings.ts +1 -9
  46. package/src/plugin/sqlite-store.ts +94 -64
  47. package/src/plugin/store.ts +5 -0
  48. package/src/plugin/types.ts +566 -21
  49. package/src/plugin/validate.ts +396 -26
  50. package/src/runner/answer.ts +26 -68
  51. package/src/runner/ask.ts +227 -28
  52. package/src/runner/cancel.ts +10 -7
  53. package/src/runner/grants.ts +2 -2
  54. package/src/runner/index.ts +1 -3
  55. package/src/runner/limits.ts +2 -2
  56. package/src/runner/runner.ts +15 -14
  57. package/src/runner/service.ts +52 -19
  58. package/src/runner/step-worker.ts +9 -0
  59. package/src/testing/answer.ts +69 -0
  60. package/src/testing/clock-manual.ts +152 -0
  61. package/src/testing/config.ts +16 -0
  62. package/src/testing/fs.ts +172 -0
  63. package/src/testing/home.ts +43 -0
  64. package/src/testing/index.ts +19 -0
  65. package/src/testing/types.ts +2 -0
  66. package/src/testing/world.ts +9 -2
  67. package/src/trigger/kernel.ts +13 -46
  68. package/src/trigger/rules.ts +22 -12
  69. package/src/trigger/types.ts +2 -6
  70. package/src/util.ts +99 -0
  71. package/src/views/event-line.ts +22 -0
  72. package/src/views/history.ts +36 -19
  73. package/src/views/live-logs.ts +313 -0
  74. package/src/views/pending.ts +23 -5
  75. package/src/views/presentation.ts +629 -81
  76. package/src/views/running.ts +5 -5
  77. package/src/views/settings.ts +205 -66
  78. package/src/views/types.ts +3 -2
  79. package/src/views/uploads.ts +15 -7
  80. package/triggers.schema.json +4 -0
@@ -6,6 +6,7 @@ import { FlowError, validateFlow } from "../flow/validate.ts";
6
6
  import type { JsonValue } from "../log/types.ts";
7
7
  import { validateActionArgs } from "./action-schema.ts";
8
8
  import { readDeclaredFlows, writeDeclaredFlow } from "./declared-flows.ts";
9
+ import { readFile } from "node:fs/promises";
9
10
  import { AccessError, AskTimeoutError, messageOf, PluginError, StepNotLoadedError } from "./errors.ts";
10
11
  import { defaultOperadHome } from "./home.ts";
11
12
  import { createPluginLogger } from "./logger.ts";
@@ -20,7 +21,9 @@ import {
20
21
  type ProjectSettingsDeclaration,
21
22
  type ActionContext,
22
23
  type ActionEnv,
24
+ type HarnessCapabilities,
23
25
  type HarnessEfforts,
26
+ type HarnessModelContext,
24
27
  type HarnessModels,
25
28
  type ActionEvent,
26
29
  type ActionRun,
@@ -40,17 +43,17 @@ import {
40
43
  type ObjectRef,
41
44
  type StoredObject,
42
45
  type ObjectTypeDefinition,
46
+ type RunObject,
43
47
  type RunLookup,
44
48
  type ObjectViews,
45
49
  type KeyBindings,
46
50
  type PendingActionView,
47
51
  type PendingEntry,
48
- type StandingContext,
49
- type StandingItem,
50
52
  type PluginBaseContext,
53
+ type MissingItem,
51
54
  type PluginCheck,
52
- type PluginKind,
53
- type PluginRequires,
55
+ type SetupGuide,
56
+ type SetupReader,
54
57
  type PluginStepResult,
55
58
  type PluginStore,
56
59
  type ResolvedCommand,
@@ -75,10 +78,40 @@ import type {
75
78
  TriggerRule,
76
79
  } from "../trigger/types.ts";
77
80
  import { askGraceOf, collectStepTypes, grantRequestProblem, narrowerOf, noteInfo, normalizePlugin, parseObjectKeyMaybe, splitActionName, validateFlows, validatePluginSet, validateRuleFlows, type LoadedPlugin } from "./validate.ts";
81
+ import { choiceInfoJson, choiceInfoProblem, normalizeChoiceInfo } from "./choice-info.ts";
82
+ import { normalizeQuestions, questionsJson, questionsProblem } from "./question-form.ts";
78
83
  import { isRecord } from "../util.ts";
79
84
 
80
85
  export { splitActionName };
81
86
 
87
+ /** The items of a check's result; a malformed item makes the check throw. */
88
+ function missingItemsOf(result: { readonly missing: readonly MissingItem[] }): readonly MissingItem[] {
89
+ if (!Array.isArray(result.missing)) throw new PluginError("check: missing must be a list");
90
+ const seen = new Set<string>();
91
+ const items: MissingItem[] = [];
92
+ for (const item of result.missing as readonly unknown[]) {
93
+ if (
94
+ !isRecord(item) ||
95
+ typeof item.name !== "string" ||
96
+ item.name.trim().length === 0 ||
97
+ typeof item.description !== "string" ||
98
+ item.description.trim().length === 0
99
+ ) {
100
+ throw new PluginError("check: each missing item needs a name and a description");
101
+ }
102
+ if (seen.has(item.name)) throw new PluginError(`check: the missing item "${item.name}" is listed twice`);
103
+ seen.add(item.name);
104
+ items.push({ name: item.name, description: item.description });
105
+ }
106
+ return items;
107
+ }
108
+
109
+ /** A `SKILL.md`'s text without its front matter. */
110
+ export function guideBody(text: string): string {
111
+ const match = /^---\r?\n[\s\S]*?\r?\n---\r?\n?/.exec(text);
112
+ return (match === null ? text : text.slice(match[0].length)).trim();
113
+ }
114
+
82
115
  /** An action's rank for an object's data; a failing `rank` counts as 0. */
83
116
  export function rankOf(action: ActionTypeDefinition, data: ObjectData): number {
84
117
  if (action.rank === undefined) return 0;
@@ -126,29 +159,34 @@ export interface PluginRegistryOptions {
126
159
  | undefined;
127
160
  /** Cancels one run when a plugin action asks; the kernel writes the input. */
128
161
  readonly onCancelRun?:
129
- | ((runId: string, flow?: FlowDefinition) => MaybePromise<void>)
162
+ | ((runId: string, flow?: FlowDefinition) => MaybePromise<boolean | void>)
130
163
  | undefined;
164
+ /** Tells whether a run has not ended, for a plugin that reports on it. */
165
+ readonly onRunActive?: ((runId: string) => MaybePromise<boolean | void>) | undefined;
131
166
  /** The specifier each definition was loaded from, aligned with it. */
132
167
  readonly sources?: readonly (string | undefined)[] | undefined;
133
168
  /** The raw config section of each plugin, keyed by plugin name. */
134
169
  readonly settings?: Readonly<Record<string, unknown>> | undefined;
135
170
  /** Plugins the config switched off; a dependent is refused with the reason. */
136
171
  readonly disabled?: readonly string[] | undefined;
172
+ /** The plugins that are off, switched off or not in the file yet; defaults to `disabled`. */
173
+ readonly off?: readonly string[] | undefined;
174
+ /**
175
+ * The path of a switched-off plugin's guide, read from its module; undefined
176
+ * when it has none. Absent: an off plugin's guide is not readable.
177
+ */
178
+ readonly disabledGuide?: ((plugin: string) => Promise<string | undefined>) | undefined;
137
179
  /** Whether to resolve `requires` against the loaded plugins. */
138
180
  readonly requireDependencies?: boolean | undefined;
139
181
  /** Whether to validate each plugin's settings section. */
140
182
  readonly validateSettings?: boolean | undefined;
141
- /** The routing rules, given to source plugins. */
142
- readonly rules?: readonly TriggerRule[] | undefined;
183
+ /** The kernel's runs, so a standing or action hook can tell a live one. */
184
+ readonly runLookup?: RunLookup | undefined;
143
185
  /**
144
186
  * Called when an action sends an event with `ctx.emit`: the kernel routes it
145
187
  * through the rules. Without it an action has no `emit`.
146
188
  */
147
189
  readonly onEvent?: ((event: TriggerEvent) => MaybePromise<void>) | undefined;
148
- /** Called when a source plugin sends an event. */
149
- readonly onSourceEvent?:
150
- | ((event: TriggerEvent, plugin: string) => MaybePromise<void>)
151
- | undefined;
152
190
  }
153
191
 
154
192
  /** A plugin the registry left out, and the line that says why. */
@@ -161,8 +199,8 @@ export interface PluginRefusal {
161
199
  export interface ActionInfo {
162
200
  readonly plugin: string;
163
201
  readonly action: string;
164
- /** The object type the action works on. */
165
- readonly on: string;
202
+ /** The object type the action works on; absent when it targets no object. */
203
+ readonly on?: string | undefined;
166
204
  /** Addressed as `<plugin>:<action>`. */
167
205
  readonly name: string;
168
206
  /** The person's input, as a JSON Schema object. */
@@ -171,10 +209,18 @@ export interface ActionInfo {
171
209
  readonly note?: string | undefined;
172
210
  readonly noteRequired: boolean;
173
211
  readonly confirm: boolean;
212
+ /** The one-line description when a chat may run this action as a command. */
213
+ readonly command?: string | undefined;
174
214
  /** The action ends the object, so a main key or button never runs it. */
175
215
  readonly ends?: boolean | undefined;
176
216
  /** The object type the action creates; a client lists it where that type is created. */
177
217
  readonly creates?: string | undefined;
218
+ /**
219
+ * Whether the action's `when` holds for this object now. Set on the actions
220
+ * an action receives; absent on the list a client reads, which already holds
221
+ * only the available actions.
222
+ */
223
+ readonly available?: boolean | undefined;
178
224
  }
179
225
 
180
226
  /** Where a key binding came from. */
@@ -228,13 +274,14 @@ export class PluginRegistry {
228
274
  ) => MaybePromise<void>)
229
275
  | undefined;
230
276
  private readonly onCancelRun:
231
- | ((runId: string, flow?: FlowDefinition) => MaybePromise<void>)
277
+ | ((runId: string, flow?: FlowDefinition) => MaybePromise<boolean | void>)
232
278
  | undefined;
233
- private readonly rules: readonly TriggerRule[];
234
- private readonly onEvent: ((event: TriggerEvent) => MaybePromise<void>) | undefined;
235
- private readonly onSourceEvent:
236
- | ((event: TriggerEvent, plugin: string) => MaybePromise<void>)
279
+ private readonly onRunActive:
280
+ | ((runId: string) => MaybePromise<boolean | void>)
237
281
  | undefined;
282
+ private readonly onEvent: ((event: TriggerEvent) => MaybePromise<void>) | undefined;
283
+ /** The kernel's runs when the host supplies them; a hook can tell a live one. */
284
+ private runLookup: RunLookup | undefined;
238
285
  private readonly plugins: readonly LoadedPlugin[];
239
286
  private readonly byName: ReadonlyMap<string, LoadedPlugin>;
240
287
  private readonly viewByType: ReadonlyMap<string, { readonly plugin: LoadedPlugin; readonly views: ObjectViews }>;
@@ -257,6 +304,8 @@ export class PluginRegistry {
257
304
  Readonly<Record<string, JsonValue>>
258
305
  >;
259
306
  private readonly disabledNames: ReadonlySet<string>;
307
+ private readonly offNames: ReadonlySet<string>;
308
+ private readonly disabledGuide: ((plugin: string) => Promise<string | undefined>) | undefined;
260
309
 
261
310
  constructor(
262
311
  definitions: readonly unknown[],
@@ -269,9 +318,9 @@ export class PluginRegistry {
269
318
  this.onStartRun = options.onStartRun;
270
319
  this.onAnswer = options.onAnswer;
271
320
  this.onCancelRun = options.onCancelRun;
272
- this.rules = options.rules ?? [];
273
- this.onSourceEvent = options.onSourceEvent;
321
+ this.onRunActive = options.onRunActive;
274
322
  this.onEvent = options.onEvent;
323
+ this.runLookup = options.runLookup;
275
324
 
276
325
  const clock = options.clock ?? (() => Date.now());
277
326
  this.store =
@@ -291,6 +340,8 @@ export class PluginRegistry {
291
340
  }
292
341
  this.sourceByPlugin = sourceMap;
293
342
  this.disabledNames = new Set(options.disabled ?? []);
343
+ this.disabledGuide = options.disabledGuide;
344
+ this.offNames = new Set(options.off ?? options.disabled ?? []);
294
345
 
295
346
  const settingsByPlugin = new Map<
296
347
  string,
@@ -414,15 +465,22 @@ export class PluginRegistry {
414
465
  }
415
466
  for (const action of plugin.actions) {
416
467
  const note = noteInfo(action.args);
468
+ const command =
469
+ typeof action.command === "string"
470
+ ? action.command
471
+ : action.command === true
472
+ ? action.args?.description?.trim() || action.name
473
+ : undefined;
417
474
  actionInfos.push({
418
475
  plugin: plugin.name,
419
476
  action: action.name,
420
- on: action.on,
477
+ ...(action.on === undefined ? {} : { on: action.on }),
421
478
  name: `${plugin.name}:${action.name}`,
422
479
  ...(action.args === undefined ? {} : { args: action.args }),
423
480
  ...(note.name === undefined ? {} : { note: note.name }),
424
481
  noteRequired: note.required,
425
482
  confirm: action.confirm === true,
483
+ ...(command === undefined ? {} : { command }),
426
484
  ...(action.ends === true ? { ends: true } : {}),
427
485
  ...(action.creates === undefined ? {} : { creates: action.creates }),
428
486
  });
@@ -467,6 +525,17 @@ export class PluginRegistry {
467
525
  return this.actionInfos;
468
526
  }
469
527
 
528
+ /** Every command a loaded plugin declares, as `<plugin> <verb>` or its short form. */
529
+ commandNames(): readonly string[] {
530
+ const names = new Set<string>(this.shortCommands.keys());
531
+ for (const plugin of this.plugins) {
532
+ for (const command of plugin.commands) {
533
+ names.add(`${plugin.name} ${command.name}`);
534
+ }
535
+ }
536
+ return [...names];
537
+ }
538
+
470
539
  /** The default and user key bindings in force, by key. */
471
540
  keyBindings(): readonly KeyBinding[] {
472
541
  return [...this.bindingByKey.values()];
@@ -497,6 +566,16 @@ export class PluginRegistry {
497
566
  );
498
567
  }
499
568
 
569
+ /** The definition of one action that targets no object, or undefined. */
570
+ targetlessAction(
571
+ plugin: string,
572
+ action: string,
573
+ ): ActionTypeDefinition | undefined {
574
+ return this.byName
575
+ .get(plugin)
576
+ ?.actions.find((item) => item.name === action && item.on === undefined);
577
+ }
578
+
500
579
  /** The plugin names in load order. */
501
580
  names(): readonly string[] {
502
581
  return this.plugins.map((plugin) => plugin.name);
@@ -907,6 +986,24 @@ export class PluginRegistry {
907
986
  `step "${use}": ask: choices: must be a list of strings`,
908
987
  );
909
988
  }
989
+ const choices = request.choices ?? [];
990
+ const questionsProblemText = questionsProblem(request.questions);
991
+ if (questionsProblemText !== undefined) {
992
+ throw new PluginError(`step "${use}": ask: questions: ${questionsProblemText}`);
993
+ }
994
+ if (request.questions !== undefined && choices.length > 0) {
995
+ throw new PluginError(
996
+ `step "${use}": ask: a form takes no choices of its own; each question carries its own`,
997
+ );
998
+ }
999
+ const questions = normalizeQuestions(request.questions);
1000
+ const infoProblem = choiceInfoProblem(request.choiceInfo, choices);
1001
+ if (infoProblem !== undefined) {
1002
+ throw new PluginError(
1003
+ `step "${use}": ask: choiceInfo: ${infoProblem}`,
1004
+ );
1005
+ }
1006
+ const choiceInfo = normalizeChoiceInfo(request.choiceInfo, choices);
910
1007
  if (
911
1008
  request.onTimeout !== undefined &&
912
1009
  request.onTimeout !== "waiting" &&
@@ -936,6 +1033,14 @@ export class PluginRegistry {
936
1033
  // gets one answer or a refusal, whatever the call said.
937
1034
  const onTimeout =
938
1035
  found.step.resumable === false ? "reject_once" : request.onTimeout;
1036
+ if (
1037
+ questions?.some((question) => question.secret === true) === true &&
1038
+ onTimeout !== "reject_once"
1039
+ ) {
1040
+ throw new PluginError(
1041
+ `step "${use}": ask: a secret question cannot wait, so its onTimeout must be "reject_once"`,
1042
+ );
1043
+ }
939
1044
  if (
940
1045
  onTimeout !== "reject_once" &&
941
1046
  !found.step.results.includes("waiting")
@@ -950,7 +1055,12 @@ export class PluginRegistry {
950
1055
  }
951
1056
  const graceSecs = narrowerOf(request.graceSecs, askGraceOf(params));
952
1057
  const outcome = await options.asker.ask(
953
- { ...request, ...(graceSecs === undefined ? {} : { graceSecs }) },
1058
+ {
1059
+ ...request,
1060
+ ...(questions === undefined ? {} : { questions }),
1061
+ ...(choiceInfo === undefined ? {} : { choiceInfo }),
1062
+ ...(graceSecs === undefined ? {} : { graceSecs }),
1063
+ },
954
1064
  options.signal,
955
1065
  );
956
1066
  if (outcome.kind === "answered") return outcome.answer;
@@ -961,7 +1071,13 @@ export class PluginRegistry {
961
1071
  waitId: outcome.waitId,
962
1072
  prompt: outcome.prompt,
963
1073
  choices: outcome.choices,
964
- resume: request.resume ?? null,
1074
+ ...(outcome.questions === undefined && questions === undefined
1075
+ ? {}
1076
+ : { questions: outcome.questions ?? questions }),
1077
+ ...(outcome.choiceInfo === undefined && choiceInfo === undefined
1078
+ ? {}
1079
+ : { choiceInfo: outcome.choiceInfo ?? choiceInfo }),
1080
+ resume: outcome.resume ?? request.resume ?? null,
965
1081
  });
966
1082
  };
967
1083
  const useGrant = async (waitId: string): Promise<boolean> => {
@@ -987,8 +1103,10 @@ export class PluginRegistry {
987
1103
  settings: this.settingsFor(found.plugin.name),
988
1104
  secrets: this.secretsFor(found.plugin.name),
989
1105
  runId: options.runId,
1106
+ harnesses: this.plugins.filter((plugin) => plugin.kind === "harness").map((plugin) => plugin.name),
990
1107
  object: options.object,
991
1108
  params: options.params ?? null,
1109
+ flow: options.flow,
992
1110
  access: options.access ?? null,
993
1111
  trigger: options.trigger,
994
1112
  emit,
@@ -999,8 +1117,10 @@ export class PluginRegistry {
999
1117
  : (key, ahead) =>
1000
1118
  limiter.report({ key, ...(ahead === undefined ? {} : { ahead }) }),
1001
1119
  stepLoaded: (name) => this.stepIndex.has(name),
1002
- harnessModels: (name) => this.loadedModels()[name],
1120
+ harnessModels: (name) => this.loadedModelFor(name),
1121
+ harnessModelsReady: (name) => this.loadedModelsReadyFor(name),
1003
1122
  harnessEfforts: (name) => this.loadedEfforts()[name],
1123
+ harnessCapabilities: (name) => this.loadedCapabilities()[name],
1004
1124
  runStep: (nestedUse, nestedParams, nestedOptions) => {
1005
1125
  // Declarations must not drift: tests fail on a call the step did not
1006
1126
  // declare; production still runs it.
@@ -1020,6 +1140,7 @@ export class PluginRegistry {
1020
1140
  }
1021
1141
  return this.runStepInner(nestedUse, nestedParams, {
1022
1142
  ...nestedOptions,
1143
+ flow: nestedOptions.flow ?? options.flow,
1023
1144
  limits: nestedOptions.limits ?? options.limits,
1024
1145
  asker: nestedOptions.asker ?? options.asker,
1025
1146
  // A nested step reports its events to the same destination.
@@ -1042,6 +1163,12 @@ export class PluginRegistry {
1042
1163
  waitId: error.waitId,
1043
1164
  prompt: error.prompt,
1044
1165
  choices: [...error.choices],
1166
+ ...(error.choiceInfo === undefined
1167
+ ? {}
1168
+ : { choiceInfo: choiceInfoJson(error.choiceInfo) }),
1169
+ ...(error.questions === undefined
1170
+ ? {}
1171
+ : { questions: questionsJson(error.questions) }),
1045
1172
  resume: error.resume ?? null,
1046
1173
  },
1047
1174
  };
@@ -1069,51 +1196,215 @@ export class PluginRegistry {
1069
1196
  return { result: outcome.result, data };
1070
1197
  }
1071
1198
 
1199
+ /** True when a loaded plugin declares the step `<plugin>/<step>`. */
1200
+ stepLoaded(use: string): boolean {
1201
+ return this.stepIndex.has(use);
1202
+ }
1203
+
1072
1204
  /** The names of the harness plugins loaded now. */
1073
1205
  loadedHarnesses(): readonly string[] {
1074
1206
  return this.plugins.filter((item) => item.kind === "harness").map((item) => item.name);
1075
1207
  }
1076
1208
 
1209
+ private modelContext(): HarnessModelContext {
1210
+ return { home: this.home, log: this.onLog };
1211
+ }
1212
+
1077
1213
  /** The models the loaded harness plugins declare, by plugin name. */
1078
1214
  loadedModels(): Readonly<Record<string, HarnessModels>> {
1079
1215
  const found: Record<string, HarnessModels> = {};
1216
+ const context = this.modelContext();
1080
1217
  for (const item of this.plugins) {
1081
1218
  if (item.kind !== "harness" || item.models === undefined) continue;
1082
1219
  found[item.name] =
1083
- typeof item.models === "function" ? item.models(this.settingsFor(item.name)) : item.models;
1220
+ typeof item.models === "function"
1221
+ ? item.models(this.settingsFor(item.name), context)
1222
+ : item.models;
1084
1223
  }
1085
1224
  return found;
1086
1225
  }
1087
1226
 
1227
+ /** The models one loaded harness plugin declares, without reading another's. */
1228
+ loadedModelFor(name: string): HarnessModels | undefined {
1229
+ const item = this.byName.get(name);
1230
+ if (item === undefined || item.kind !== "harness" || item.models === undefined) {
1231
+ return undefined;
1232
+ }
1233
+ return typeof item.models === "function"
1234
+ ? item.models(this.settingsFor(name), this.modelContext())
1235
+ : item.models;
1236
+ }
1237
+
1238
+ /**
1239
+ * The models the loaded harness plugins declare, waiting for a harness that
1240
+ * has no kept list yet to read one. A plugin without `modelsReady` answers
1241
+ * at once with its ordinary list.
1242
+ */
1243
+ async loadedModelsReady(): Promise<Readonly<Record<string, HarnessModels>>> {
1244
+ const found: Record<string, HarnessModels> = {};
1245
+ const context = this.modelContext();
1246
+ for (const item of this.plugins) {
1247
+ if (item.kind !== "harness") continue;
1248
+ if (item.models === undefined && item.modelsReady === undefined) continue;
1249
+ found[item.name] =
1250
+ item.modelsReady !== undefined
1251
+ ? await item.modelsReady(this.settingsFor(item.name), context)
1252
+ : item.models !== undefined && typeof item.models === "function"
1253
+ ? item.models(this.settingsFor(item.name), context)
1254
+ : item.models ?? { list: [], default: null };
1255
+ }
1256
+ return found;
1257
+ }
1258
+
1259
+ /** One harness's models, waiting for a first refresh when it needs one. */
1260
+ async loadedModelsReadyFor(name: string): Promise<HarnessModels | undefined> {
1261
+ const item = this.byName.get(name);
1262
+ if (item === undefined || item.kind !== "harness") return undefined;
1263
+ if (item.models === undefined && item.modelsReady === undefined) return undefined;
1264
+ const context = this.modelContext();
1265
+ if (item.modelsReady !== undefined) {
1266
+ return await item.modelsReady(this.settingsFor(name), context);
1267
+ }
1268
+ return item.models !== undefined && typeof item.models === "function"
1269
+ ? item.models(this.settingsFor(name), context)
1270
+ : item.models ?? { list: [], default: null };
1271
+ }
1272
+
1088
1273
  /** The efforts the loaded harness plugins declare, by plugin name. */
1089
1274
  loadedEfforts(): Readonly<Record<string, HarnessEfforts>> {
1090
1275
  const found: Record<string, HarnessEfforts> = {};
1091
1276
  for (const item of this.plugins) {
1092
- if (item.kind === "harness" && item.efforts !== undefined) found[item.name] = item.efforts;
1277
+ if (item.kind !== "harness" || item.efforts === undefined) continue;
1278
+ found[item.name] =
1279
+ typeof item.efforts === "function"
1280
+ ? item.efforts(this.settingsFor(item.name))
1281
+ : item.efforts;
1093
1282
  }
1094
1283
  return found;
1095
1284
  }
1096
1285
 
1097
- /** What an action's checks may read. */
1098
- actionEnv(): ActionEnv {
1286
+ /** What the loaded harness plugins declare they can do, by plugin name. */
1287
+ loadedCapabilities(): Readonly<Record<string, HarnessCapabilities>> {
1288
+ const found: Record<string, HarnessCapabilities> = {};
1289
+ for (const item of this.plugins) {
1290
+ if (item.kind === "harness") found[item.name] = item.capabilities ?? {};
1291
+ }
1292
+ return found;
1293
+ }
1294
+
1295
+ /**
1296
+ * Whether a loaded harness plugin's agent is signed in now: true or false
1297
+ * when it can tell, undefined when it is not a harness, declares no
1298
+ * `signedIn`, or the check fails. This is the one answer a client receives;
1299
+ * a credential is never read out.
1300
+ */
1301
+ async signedInFor(plugin: string): Promise<boolean | undefined> {
1302
+ const found = this.byName.get(plugin);
1303
+ if (found === undefined || found.kind !== "harness" || found.signedIn === undefined) {
1304
+ return undefined;
1305
+ }
1306
+ try {
1307
+ return await found.signedIn(this.settingsFor(plugin));
1308
+ } catch {
1309
+ return undefined;
1310
+ }
1311
+ }
1312
+
1313
+ /** What an action's checks may read; `object` names the object it works on. */
1314
+ actionEnv(object?: ObjectRef | undefined): ActionEnv {
1099
1315
  return {
1100
1316
  store: this.store,
1317
+ ...(object === undefined ? {} : { object }),
1318
+ home: this.home,
1101
1319
  harnesses: this.loadedHarnesses(),
1102
1320
  models: this.loadedModels(),
1103
1321
  efforts: this.loadedEfforts(),
1322
+ stepLoaded: (use) => this.stepIndex.has(use),
1323
+ ...(this.onCancelRun === undefined ? {} : { runs: this.runControl() }),
1104
1324
  };
1105
1325
  }
1106
1326
 
1107
- /** Runs an action on an object, by `plugin/action` or a unique plain name. */
1327
+ /**
1328
+ * Runs another action on an object with the checks a person's action gets
1329
+ * (availability, arguments, confirmation), but without a note or files. An
1330
+ * action that forwards a person's text to another uses it; `yes` answers a
1331
+ * confirmation. The action's own check runs before anything is recorded.
1332
+ */
1333
+ async runActionChecked(
1334
+ name: string,
1335
+ args: unknown,
1336
+ options: {
1337
+ readonly object: RunObject;
1338
+ readonly actor: Actor;
1339
+ readonly yes?: boolean | undefined;
1340
+ readonly cwd?: string | undefined;
1341
+ },
1342
+ ): Promise<ActionRun> {
1343
+ const object = options.object;
1344
+ const found = this.findAction(name, object.type);
1345
+ const ref: ObjectRef = { type: object.type, id: object.id };
1346
+ const env = this.actionEnv(ref);
1347
+ let holds: unknown;
1348
+ try {
1349
+ holds = await found.action.when(object.data, env);
1350
+ } catch (error) {
1351
+ throw new PluginError(
1352
+ `action "${found.plugin.name}:${found.action.name}" cannot run now: ${messageOf(error)}`,
1353
+ );
1354
+ }
1355
+ if (holds !== true) throw new PluginError(await this.refusalLine(found, ref, env));
1356
+ const schema =
1357
+ found.action.argsFor?.(object, env) ?? found.action.args;
1358
+ const personArgs = validateActionArgs(
1359
+ schema,
1360
+ args,
1361
+ `action "${found.plugin.name}:${found.action.name}"`,
1362
+ );
1363
+ if (found.action.confirm === true && options.yes !== true) {
1364
+ throw new PluginError(
1365
+ `action "${found.plugin.name}:${found.action.name}" needs confirmation`,
1366
+ );
1367
+ }
1368
+ return this.runAction(`${found.plugin.name}/${found.action.name}`, personArgs, {
1369
+ object,
1370
+ actor: options.actor,
1371
+ ...(options.cwd === undefined ? {} : { cwd: options.cwd }),
1372
+ });
1373
+ }
1374
+
1375
+ /** The line a refusal prints: the plugin's `why`, else the plain one. */
1376
+ private async refusalLine(
1377
+ found: { readonly plugin: LoadedPlugin; readonly action: ActionTypeDefinition },
1378
+ object: ObjectRef,
1379
+ env: ActionEnv,
1380
+ ): Promise<string> {
1381
+ const plain = `action "${found.plugin.name}:${found.action.name}" cannot run now`;
1382
+ if (found.action.why === undefined) return plain;
1383
+ const stored = await this.store.get(object.type, object.id);
1384
+ if (stored === undefined) return plain;
1385
+ try {
1386
+ const why = await found.action.why(stored, env);
1387
+ return why !== undefined && why.length > 0 ? why : plain;
1388
+ } catch (error) {
1389
+ this.log(
1390
+ `plugin "${found.plugin.name}": action "${found.action.name}": why failed: ${messageOf(error)}`,
1391
+ );
1392
+ return plain;
1393
+ }
1394
+ }
1395
+
1396
+ /** Runs an action, by `plugin/action` or a unique plain name. */
1108
1397
  async runAction(
1109
1398
  name: string,
1110
1399
  args: unknown,
1111
1400
  options: ActionRunOptions,
1112
1401
  ): Promise<ActionRun> {
1113
- const found = this.findAction(name, options.object.type);
1402
+ const object = options.object;
1403
+ const found = this.findAction(name, object?.type);
1114
1404
  const label = `action "${found.plugin.name}:${found.action.name}"`;
1405
+ const env = this.actionEnv(object === undefined ? undefined : { type: object.type, id: object.id });
1115
1406
  const personArgs = validateActionArgs(
1116
- found.action.argsFor?.(options.object, this.actionEnv()) ?? found.action.args,
1407
+ (object === undefined ? undefined : found.action.argsFor?.(object, env)) ?? found.action.args,
1117
1408
  args,
1118
1409
  label,
1119
1410
  );
@@ -1130,7 +1421,9 @@ export class PluginRegistry {
1130
1421
  // it never changes the object's data.
1131
1422
  await this.store.append({
1132
1423
  type: "action_invoked",
1133
- object: { type: options.object.type, id: options.object.id },
1424
+ ...(object === undefined
1425
+ ? {}
1426
+ : { object: { type: object.type, id: object.id } }),
1134
1427
  cause,
1135
1428
  data: {
1136
1429
  action: `${found.plugin.name}/${found.action.name}`,
@@ -1153,13 +1446,29 @@ export class PluginRegistry {
1153
1446
  signal: options.signal ?? new AbortController().signal,
1154
1447
  settings: this.settingsFor(found.plugin.name),
1155
1448
  secrets: this.secretsFor(found.plugin.name),
1449
+ setup: this.setupReader(),
1156
1450
  runId,
1157
- object: options.object,
1451
+ // A targetless action has no object; it runs from its arguments alone.
1452
+ object: object ?? { type: "", id: "", data: {} },
1158
1453
  actor,
1159
1454
  answer,
1160
1455
  harnesses: this.loadedHarnesses(),
1456
+ ...(this.runLookup === undefined ? {} : { runLookup: this.runLookup }),
1457
+ stepLoaded: (use) => this.stepIndex.has(use),
1161
1458
  models: this.loadedModels(),
1162
1459
  efforts: this.loadedEfforts(),
1460
+ actions: object === undefined ? [] : await this.actionInfosFor(object),
1461
+ ...(object === undefined
1462
+ ? {}
1463
+ : {
1464
+ act: (name: string, args: unknown, actOptions?: { readonly yes?: boolean | undefined }) =>
1465
+ this.runActionChecked(name, args, {
1466
+ object,
1467
+ actor,
1468
+ ...(actOptions?.yes === undefined ? {} : { yes: actOptions.yes }),
1469
+ ...(options.cwd === undefined ? {} : { cwd: options.cwd }),
1470
+ }),
1471
+ }),
1163
1472
  ...(this.onEvent === undefined
1164
1473
  ? {}
1165
1474
  : { emit: this.actionEmitter(found.plugin.name) }),
@@ -1207,40 +1516,104 @@ export class PluginRegistry {
1207
1516
  };
1208
1517
  }
1209
1518
 
1210
- /** Runs each plugin's optional `check`; a throw is a failed check, not an error. */
1519
+ /** Runs one loaded plugin's `check`; a throw is a failed check, not an error. */
1520
+ private async runCheck(plugin: LoadedPlugin): Promise<PluginCheck | undefined> {
1521
+ if (plugin.check === undefined) return undefined;
1522
+ const ctx: PluginBaseContext = {
1523
+ plugin: plugin.name,
1524
+ home: this.home,
1525
+ cwd: this.cwd,
1526
+ store: this.store,
1527
+ facts: this.factsFor(`check:${plugin.name}`),
1528
+ logger: createPluginLogger(this.store.root, plugin.name, "check"),
1529
+ signal: new AbortController().signal,
1530
+ settings: this.settingsFor(plugin.name),
1531
+ secrets: this.secretsFor(plugin.name),
1532
+ };
1533
+ try {
1534
+ const result = await plugin.check(ctx);
1535
+ if (typeof result === "string") {
1536
+ return { plugin: plugin.name, ok: true, message: result };
1537
+ }
1538
+ if (result === undefined) {
1539
+ return { plugin: plugin.name, ok: true, message: "ok" };
1540
+ }
1541
+ const missing = missingItemsOf(result);
1542
+ const note = typeof result.note === "string" ? result.note : undefined;
1543
+ return {
1544
+ plugin: plugin.name,
1545
+ ok: true,
1546
+ message: note ?? (missing.length === 0 ? "ok" : missing.map((item) => item.description).join("; ")),
1547
+ missing,
1548
+ };
1549
+ } catch (error) {
1550
+ return { plugin: plugin.name, ok: false, message: messageOf(error) };
1551
+ }
1552
+ }
1553
+
1554
+ /** Runs each plugin's optional `check`. */
1211
1555
  async checks(): Promise<readonly PluginCheck[]> {
1212
1556
  const checks: PluginCheck[] = [];
1213
1557
  for (const plugin of this.plugins) {
1214
- if (plugin.check === undefined) continue;
1215
- const ctx: PluginBaseContext = {
1216
- plugin: plugin.name,
1217
- home: this.home,
1218
- cwd: this.cwd,
1219
- store: this.store,
1220
- facts: this.factsFor(`check:${plugin.name}`),
1221
- logger: createPluginLogger(this.store.root, plugin.name, "check"),
1222
- signal: new AbortController().signal,
1223
- settings: this.settingsFor(plugin.name),
1224
- secrets: this.secretsFor(plugin.name),
1225
- };
1226
- try {
1227
- const note = await plugin.check(ctx);
1228
- checks.push({
1229
- plugin: plugin.name,
1230
- ok: true,
1231
- message: typeof note === "string" ? note : "ok",
1232
- });
1233
- } catch (error) {
1234
- checks.push({
1235
- plugin: plugin.name,
1236
- ok: false,
1237
- message: messageOf(error),
1238
- });
1239
- }
1558
+ const check = await this.runCheck(plugin);
1559
+ if (check !== undefined) checks.push(check);
1240
1560
  }
1241
1561
  return checks;
1242
1562
  }
1243
1563
 
1564
+ /**
1565
+ * The setup guides (ADR 0010 D7): a loaded plugin's guide with what its check
1566
+ * reports missing, an off plugin's guide with the one item `enable`.
1567
+ */
1568
+ setupReader(): SetupReader {
1569
+ const read = async (path: string): Promise<string | undefined> => {
1570
+ try {
1571
+ return guideBody(await readFile(path, "utf8"));
1572
+ } catch {
1573
+ return undefined;
1574
+ }
1575
+ };
1576
+ return {
1577
+ list: async () => {
1578
+ const found: { plugin: string; enabled: boolean }[] = [];
1579
+ for (const plugin of this.plugins) {
1580
+ if (plugin.guide !== undefined) found.push({ plugin: plugin.name, enabled: true });
1581
+ }
1582
+ for (const name of this.offNames) {
1583
+ if ((await this.disabledGuide?.(name)) !== undefined) found.push({ plugin: name, enabled: false });
1584
+ }
1585
+ return found;
1586
+ },
1587
+ get: async (name): Promise<SetupGuide | undefined> => {
1588
+ const plugin = this.plugins.find((item) => item.name === name);
1589
+ if (plugin !== undefined) {
1590
+ if (plugin.guide === undefined) return undefined;
1591
+ const text = await read(plugin.guide);
1592
+ if (text === undefined) return undefined;
1593
+ const check = await this.runCheck(plugin);
1594
+ const missing: readonly MissingItem[] =
1595
+ check === undefined
1596
+ ? []
1597
+ : check.ok
1598
+ ? (check.missing ?? [])
1599
+ : [{ name: "check", description: `the check failed: ${check.message}` }];
1600
+ return { plugin: name, enabled: true, text, missing };
1601
+ }
1602
+ if (!this.offNames.has(name)) return undefined;
1603
+ const path = await this.disabledGuide?.(name);
1604
+ if (path === undefined) return undefined;
1605
+ const text = await read(path);
1606
+ if (text === undefined) return undefined;
1607
+ return {
1608
+ plugin: name,
1609
+ enabled: false,
1610
+ text,
1611
+ missing: [{ name: "enable", description: `the plugin "${name}" is off` }],
1612
+ };
1613
+ },
1614
+ };
1615
+ }
1616
+
1244
1617
  /**
1245
1618
  * The short form `<command>`: offered only when exactly one plugin declares
1246
1619
  * the name and it is not a kernel verb. A command left without a short form
@@ -1344,6 +1717,8 @@ export class PluginRegistry {
1344
1717
  signal: options.signal ?? new AbortController().signal,
1345
1718
  settings: this.settingsFor(plugin.name),
1346
1719
  secrets: this.secretsFor(plugin.name),
1720
+ setup: this.setupReader(),
1721
+ flows: this.flows().map((entry) => entry.flow.name),
1347
1722
  ...(options.input === undefined ? {} : { input: options.input }),
1348
1723
  ...(this.onCancelRun === undefined ? {} : { runs: this.runControl() }),
1349
1724
  };
@@ -1355,11 +1730,12 @@ export class PluginRegistry {
1355
1730
  * The items plugins ask a person to know about. A hook that throws is
1356
1731
  * logged and shows nothing, so one plugin cannot hide the others.
1357
1732
  */
1358
- async standing(): Promise<readonly StandingEntry[]> {
1733
+ async standing(runs?: RunLookup): Promise<readonly StandingEntry[]> {
1359
1734
  const items: StandingEntry[] = [];
1360
1735
  for (const plugin of this.plugins) {
1361
1736
  if (plugin.standing === undefined) continue;
1362
1737
  try {
1738
+ const lookup = runs ?? this.runLookup;
1363
1739
  const found = await plugin.standing({
1364
1740
  plugin: plugin.name,
1365
1741
  home: this.home,
@@ -1371,6 +1747,10 @@ export class PluginRegistry {
1371
1747
  settings: this.settingsFor(plugin.name),
1372
1748
  secrets: this.secretsFor(plugin.name),
1373
1749
  loaded: this.plugins.map((item) => item.name),
1750
+ harnesses: this.plugins.filter((item) => item.kind === "harness").map((item) => item.name),
1751
+ ...(lookup === undefined ? {} : { runLookup: lookup }),
1752
+ signedIn: (harness) => this.signedInFor(harness),
1753
+ stepLoaded: (use) => this.stepIndex.has(use),
1374
1754
  });
1375
1755
  for (const item of found) {
1376
1756
  const object =
@@ -1401,10 +1781,11 @@ export class PluginRegistry {
1401
1781
  /** The actions whose `when` holds for the object; a throwing `when` is logged and hidden. */
1402
1782
  private async actionsHolding(object: StoredObject): Promise<readonly PendingActionView[]> {
1403
1783
  const held: { plugin: string; action: string; rank: number }[] = [];
1784
+ const env = this.actionEnv({ type: object.type, id: object.id });
1404
1785
  for (const { plugin, action } of this.actionsFor(object.type)) {
1405
1786
  let holds: unknown;
1406
1787
  try {
1407
- holds = await action.when(object.data, this.actionEnv());
1788
+ holds = await action.when(object.data, env);
1408
1789
  } catch (error) {
1409
1790
  this.log(
1410
1791
  `plugin "${plugin.name}": action "${action.name}": when failed: ${messageOf(error)}`,
@@ -1438,6 +1819,7 @@ export class PluginRegistry {
1438
1819
  runId: run.runId,
1439
1820
  prompt: run.prompt,
1440
1821
  choices: run.choices,
1822
+ ...(run.choiceInfo === undefined ? {} : { choiceInfo: run.choiceInfo }),
1441
1823
  object: {
1442
1824
  type: run.object.type,
1443
1825
  id: run.object.id,
@@ -1456,6 +1838,7 @@ export class PluginRegistry {
1456
1838
  runId: run.runId,
1457
1839
  prompt: run.prompt,
1458
1840
  choices: run.choices,
1841
+ ...(run.choiceInfo === undefined ? {} : { choiceInfo: run.choiceInfo }),
1459
1842
  object,
1460
1843
  actions,
1461
1844
  });
@@ -1463,6 +1846,14 @@ export class PluginRegistry {
1463
1846
  return entries;
1464
1847
  }
1465
1848
 
1849
+ /**
1850
+ * Gives the standing and action hooks the kernel's runs, so a plugin does not
1851
+ * offer a second run while one is live. `serve` calls it once its log is open.
1852
+ */
1853
+ useRunLookup(runs: RunLookup | undefined): void {
1854
+ this.runLookup = runs;
1855
+ }
1856
+
1466
1857
  /** Runs each plugin's `onTick` once; a failure is logged, not raised. */
1467
1858
  async tick(runs?: RunLookup): Promise<void> {
1468
1859
  for (const plugin of this.plugins) {
@@ -1553,15 +1944,13 @@ export class PluginRegistry {
1553
1944
  return [...this.starts];
1554
1945
  }
1555
1946
 
1556
- /** The loaded source plugins, with the function that starts each one. */
1947
+ /** The loaded plugins that start a source, with the function that starts each one. */
1557
1948
  sourcePlugins(): readonly {
1558
1949
  readonly plugin: string;
1559
1950
  readonly start: (ctx: SourceContext) => MaybePromise<SourceHandle> | SourceHandle;
1560
1951
  }[] {
1561
1952
  return this.plugins.flatMap((plugin) =>
1562
- plugin.kind !== "source" || plugin.start === undefined
1563
- ? []
1564
- : [{ plugin: plugin.name, start: plugin.start }],
1953
+ plugin.start === undefined ? [] : [{ plugin: plugin.name, start: plugin.start }],
1565
1954
  );
1566
1955
  }
1567
1956
 
@@ -1578,7 +1967,7 @@ export class PluginRegistry {
1578
1967
  }): Promise<readonly { readonly plugin: string; readonly handle: SourceHandle }[]> {
1579
1968
  const running: { plugin: string; handle: SourceHandle }[] = [];
1580
1969
  for (const plugin of this.plugins) {
1581
- if (plugin.kind !== "source" || plugin.start === undefined) continue;
1970
+ if (plugin.start === undefined) continue;
1582
1971
  const emit = async (event: SourceEvent): Promise<void> => {
1583
1972
  const trigger: TriggerEvent = {
1584
1973
  source: event.source ?? plugin.name,
@@ -1608,10 +1997,13 @@ export class PluginRegistry {
1608
1997
  signal: options.signal ?? new AbortController().signal,
1609
1998
  rules: options.rules,
1610
1999
  clock: options.clock,
2000
+ harnesses: this.plugins.filter((item) => item.kind === "harness").map((item) => item.name),
2001
+ harnessCapabilities: (name) => this.loadedCapabilities()[name],
1611
2002
  settings: this.settingsFor(plugin.name),
1612
2003
  secrets: this.secretsFor(plugin.name),
1613
2004
  emit,
1614
2005
  client: options.client,
2006
+ ...(this.onCancelRun === undefined ? {} : { runs: this.runControl() }),
1615
2007
  };
1616
2008
  try {
1617
2009
  const handle = await plugin.start(ctx);
@@ -1623,9 +2015,33 @@ export class PluginRegistry {
1623
2015
  return running;
1624
2016
  }
1625
2017
 
1626
- /** Removes and returns the start requests collected so far. */
1627
- takeStartedRuns(): StartRunRequest[] {
1628
- return this.starts.splice(0);
2018
+ /**
2019
+ * Removes and returns the start requests collected so far, or the oldest
2020
+ * `count` of them, so a loop drains only what it has tried.
2021
+ */
2022
+ takeStartedRuns(count?: number): StartRunRequest[] {
2023
+ return count === undefined ? this.starts.splice(0) : this.starts.splice(0, count);
2024
+ }
2025
+
2026
+ /** The actions declared on an object's type, each with whether it is available now. */
2027
+ private async actionInfosFor(object: RunObject): Promise<readonly ActionInfo[]> {
2028
+ const env = this.actionEnv({ type: object.type, id: object.id });
2029
+ const holding = new Set<string>();
2030
+ for (const { plugin, action } of this.actionsFor(object.type)) {
2031
+ let holds: unknown;
2032
+ try {
2033
+ holds = await action.when(object.data, env);
2034
+ } catch (error) {
2035
+ this.log(
2036
+ `plugin "${plugin.name}": action "${action.name}": when failed: ${messageOf(error)}`,
2037
+ );
2038
+ continue;
2039
+ }
2040
+ if (holds === true) holding.add(`${plugin.name}:${action.name}`);
2041
+ }
2042
+ return this.actionInfos
2043
+ .filter((info) => info.on === object.type)
2044
+ .map((info) => ({ ...info, available: holding.has(info.name) }));
1629
2045
  }
1630
2046
 
1631
2047
  private actionsFor(
@@ -1648,7 +2064,7 @@ export class PluginRegistry {
1648
2064
 
1649
2065
  private findAction(
1650
2066
  name: string,
1651
- objectType: string,
2067
+ objectType?: string | undefined,
1652
2068
  ): { readonly plugin: LoadedPlugin; readonly action: ActionTypeDefinition } {
1653
2069
  const named = splitActionName(name);
1654
2070
  if (named !== undefined) {
@@ -1657,7 +2073,11 @@ export class PluginRegistry {
1657
2073
  const plugin = this.byName.get(pluginName);
1658
2074
  if (plugin !== undefined) {
1659
2075
  const action = plugin.actions.find(
1660
- (item) => item.name === actionName && item.on === objectType,
2076
+ (item) =>
2077
+ item.name === actionName &&
2078
+ (objectType === undefined
2079
+ ? item.on === undefined
2080
+ : item.on === objectType),
1661
2081
  );
1662
2082
  if (action !== undefined) return { plugin, action };
1663
2083
  const other = plugin.actions.find((item) => item.name === actionName);
@@ -1687,7 +2107,9 @@ export class PluginRegistry {
1687
2107
  throw new PluginError(`action "${name}": no loaded plugin declares it`);
1688
2108
  }
1689
2109
  const action =
1690
- found.actions.find((item) => item.on === objectType) ?? found.actions[0];
2110
+ found.actions.find((item) =>
2111
+ objectType === undefined ? item.on === undefined : item.on === objectType,
2112
+ ) ?? found.actions[0];
1691
2113
  if (action === undefined) {
1692
2114
  throw new PluginError(`action "${name}": no loaded plugin declares it`);
1693
2115
  }
@@ -1698,11 +2120,19 @@ export class PluginRegistry {
1698
2120
  private requireActionObject(
1699
2121
  plugin: LoadedPlugin,
1700
2122
  action: ActionTypeDefinition,
1701
- objectType: string,
2123
+ objectType?: string | undefined,
1702
2124
  ): void {
2125
+ if (objectType === undefined) {
2126
+ if (action.on !== undefined) {
2127
+ throw new PluginError(
2128
+ `plugin "${plugin.name}": action "${action.name}": on: expected no object, got object type "${action.on}"`,
2129
+ );
2130
+ }
2131
+ return;
2132
+ }
1703
2133
  if (action.on !== objectType) {
1704
2134
  throw new PluginError(
1705
- `plugin "${plugin.name}": action "${action.name}": on: expected object type "${action.on}", got "${objectType}"`,
2135
+ `plugin "${plugin.name}": action "${action.name}": on: expected object type "${action.on ?? "none"}", got "${objectType}"`,
1706
2136
  );
1707
2137
  }
1708
2138
  }
@@ -1773,9 +2203,15 @@ export class PluginRegistry {
1773
2203
 
1774
2204
  private runControl(): RunControl {
1775
2205
  const onCancelRun = this.onCancelRun;
2206
+ const onRunActive = this.onRunActive;
1776
2207
  return {
1777
2208
  cancel: async (runId, flow) => {
1778
- if (onCancelRun !== undefined) await onCancelRun(runId, flow);
2209
+ if (onCancelRun !== undefined) return await onCancelRun(runId, flow);
2210
+ return false;
2211
+ },
2212
+ active: async (runId) => {
2213
+ if (onRunActive !== undefined) return await onRunActive(runId);
2214
+ return undefined;
1779
2215
  },
1780
2216
  };
1781
2217
  }