@operad-io/engine 0.0.0-stage → 0.0.1

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 (102) hide show
  1. package/LICENSE +21 -0
  2. package/config.schema.json +188 -0
  3. package/keys.schema.json +12 -0
  4. package/package.json +22 -4
  5. package/src/config/config.ts +627 -0
  6. package/src/config/index.ts +1 -0
  7. package/src/flow/define-flow.ts +57 -0
  8. package/src/flow/index.ts +5 -0
  9. package/src/flow/load.ts +41 -0
  10. package/src/flow/types.ts +107 -0
  11. package/src/flow/validate.ts +296 -0
  12. package/src/http/address.ts +142 -0
  13. package/src/http/index.ts +25 -0
  14. package/src/http/link.ts +20 -0
  15. package/src/http/server.ts +920 -0
  16. package/src/http/token.ts +85 -0
  17. package/src/index.ts +16 -0
  18. package/src/kernel/decide.ts +689 -0
  19. package/src/kernel/index.ts +3 -0
  20. package/src/kernel/state.ts +275 -0
  21. package/src/kernel/types.ts +366 -0
  22. package/src/log/errors.ts +77 -0
  23. package/src/log/facts.ts +50 -0
  24. package/src/log/index.ts +6 -0
  25. package/src/log/json-value.ts +67 -0
  26. package/src/log/open-log.ts +17 -0
  27. package/src/log/schema.ts +5 -0
  28. package/src/log/secret.ts +30 -0
  29. package/src/log/sqlite.ts +1011 -0
  30. package/src/log/types.ts +230 -0
  31. package/src/loop/code-watch.ts +486 -0
  32. package/src/loop/index.ts +3 -0
  33. package/src/loop/older-code.ts +58 -0
  34. package/src/loop/serve.ts +483 -0
  35. package/src/markdown.ts +451 -0
  36. package/src/notify/index.ts +10 -0
  37. package/src/notify/notify.ts +256 -0
  38. package/src/platform/file-lock.ts +385 -0
  39. package/src/platform/index.ts +13 -0
  40. package/src/platform/load.ts +18 -0
  41. package/src/platform/process-group.ts +635 -0
  42. package/src/platform/process-watch.ts +152 -0
  43. package/src/platform/sqlite-open.ts +104 -0
  44. package/src/platform/step-reap.ts +668 -0
  45. package/src/platform/watch.ts +158 -0
  46. package/src/platform/worker-reports.ts +34 -0
  47. package/src/plugin/action-schema.ts +550 -0
  48. package/src/plugin/declared-flows.ts +74 -0
  49. package/src/plugin/define-plugin.ts +12 -0
  50. package/src/plugin/errors.ts +73 -0
  51. package/src/plugin/filters.ts +46 -0
  52. package/src/plugin/harness.ts +50 -0
  53. package/src/plugin/home.ts +13 -0
  54. package/src/plugin/index.ts +65 -0
  55. package/src/plugin/install.ts +268 -0
  56. package/src/plugin/keys.ts +63 -0
  57. package/src/plugin/load.ts +256 -0
  58. package/src/plugin/logger.ts +30 -0
  59. package/src/plugin/object-facts.ts +132 -0
  60. package/src/plugin/open-store.ts +32 -0
  61. package/src/plugin/paging.ts +133 -0
  62. package/src/plugin/registry.ts +1783 -0
  63. package/src/plugin/secrets.ts +141 -0
  64. package/src/plugin/semver.ts +110 -0
  65. package/src/plugin/settings.ts +64 -0
  66. package/src/plugin/sqlite-store.ts +756 -0
  67. package/src/plugin/store.ts +318 -0
  68. package/src/plugin/types.ts +1236 -0
  69. package/src/plugin/usage.ts +157 -0
  70. package/src/plugin/validate.ts +1028 -0
  71. package/src/runner/answer.ts +156 -0
  72. package/src/runner/ask.ts +483 -0
  73. package/src/runner/cancel.ts +51 -0
  74. package/src/runner/errors.ts +20 -0
  75. package/src/runner/grants.ts +92 -0
  76. package/src/runner/index.ts +33 -0
  77. package/src/runner/limits.ts +403 -0
  78. package/src/runner/lock.ts +140 -0
  79. package/src/runner/runner.ts +1175 -0
  80. package/src/runner/service.ts +184 -0
  81. package/src/runner/step-worker.ts +303 -0
  82. package/src/testing/clock.ts +67 -0
  83. package/src/testing/index.ts +90 -0
  84. package/src/testing/types.ts +116 -0
  85. package/src/testing/world.ts +369 -0
  86. package/src/trigger/cron.ts +193 -0
  87. package/src/trigger/index.ts +32 -0
  88. package/src/trigger/kernel.ts +1212 -0
  89. package/src/trigger/rules.ts +300 -0
  90. package/src/trigger/store.ts +233 -0
  91. package/src/trigger/types.ts +156 -0
  92. package/src/util.ts +50 -0
  93. package/src/views/history.ts +634 -0
  94. package/src/views/index.ts +7 -0
  95. package/src/views/pending.ts +209 -0
  96. package/src/views/presentation.ts +2551 -0
  97. package/src/views/running.ts +255 -0
  98. package/src/views/settings.ts +452 -0
  99. package/src/views/types.ts +72 -0
  100. package/src/views/uploads.ts +228 -0
  101. package/triggers.schema.json +48 -0
  102. package/README.md +0 -3
@@ -0,0 +1,1236 @@
1
+ import type { FlowDefinition } from "../flow/types.ts";
2
+ import type { KernelState } from "../kernel/types.ts";
3
+ import type { Secret } from "../log/secret.ts";
4
+ import type { JsonValue, RunInfo } from "../log/types.ts";
5
+ import type {
6
+ SourceEvent,
7
+ StepEvent,
8
+ TriggerEvent,
9
+ TriggerRule,
10
+ TriggerView,
11
+ } from "../trigger/types.ts";
12
+
13
+ /** The plugin API version a module declares with `operadPlugin`. */
14
+ export const OPERAD_PLUGIN_VERSION = 1 as const;
15
+
16
+ /** The plugin API version as a semver version, for `requires.api`. */
17
+ export const PLUGIN_API_VERSION = `${OPERAD_PLUGIN_VERSION}.0.0` as const;
18
+
19
+ /**
20
+ * The plugin kinds the kernel recognises. These names are internal: user text
21
+ * never names a kind.
22
+ */
23
+ export type PluginKind =
24
+ | "domain"
25
+ | "harness"
26
+ | "stage"
27
+ | "channel"
28
+ | "source"
29
+ | "action";
30
+
31
+ /** A synchronous value, or a promise of one. Plugin hooks may use either. */
32
+ export type MaybePromise<T> = T | Promise<T>;
33
+
34
+ /** The data of an object: JSON-shaped and owned by the plugin that declares it. */
35
+ export type ObjectData = Readonly<Record<string, unknown>>;
36
+
37
+ /** Who a person input came from. `via` is a client such as `cli` or `ios`. */
38
+ export interface Actor {
39
+ readonly via: string;
40
+ readonly id: string;
41
+ }
42
+
43
+ /** The semantic marks a row may carry; clients draw one icon per value. */
44
+ export const ROW_MARKS = [
45
+ "todo",
46
+ "blocked",
47
+ "retry_wait",
48
+ "paused",
49
+ "proposed",
50
+ "starting",
51
+ "running",
52
+ "reconciling",
53
+ "checking",
54
+ "rework_pending",
55
+ "waiting",
56
+ "integrating",
57
+ "ready_to_merge",
58
+ "done",
59
+ "failed",
60
+ "cancelled",
61
+ "broken",
62
+ ] as const;
63
+
64
+ /** One of the values in `ROW_MARKS`. */
65
+ export type RowMark = (typeof ROW_MARKS)[number];
66
+
67
+ /** The longest `badge` a row may carry, in characters. */
68
+ export const ROW_BADGE_MAX = 16;
69
+
70
+ /** One line of a list: what the plugin shows for an object. */
71
+ export interface ObjectRow {
72
+ readonly id: string;
73
+ readonly title: string;
74
+ readonly state: string;
75
+ /** A short tail for the row, such as a cost or a duration. */
76
+ readonly note?: string | undefined;
77
+ /**
78
+ * Why the object waits, as one text, separate from `note` so a client need
79
+ * not split the note. A protocol field; absent when nothing is waited for.
80
+ */
81
+ readonly wait?: string | undefined;
82
+ /**
83
+ * The namespace the id is unique in, such as a repository. A list shows it
84
+ * only when the type's `showScope` says so.
85
+ */
86
+ readonly scope?: string | undefined;
87
+ /**
88
+ * Whether `state` is final for the type: the object will not move on. A
89
+ * client that reports ends reads this and no state name.
90
+ */
91
+ readonly final?: boolean | undefined;
92
+ /** What the object is doing, as one of `ROW_MARKS`; clients choose the icon. */
93
+ readonly mark?: RowMark | undefined;
94
+ /** A short single-line text clients show after the state, such as `#2`. */
95
+ readonly badge?: string | undefined;
96
+ /**
97
+ * The object one level up: a full object id (`type:id`) or a bare id that
98
+ * resolves the way `get` resolves it. The kernel passes it through untouched
99
+ * and never invents one.
100
+ */
101
+ readonly parent?: string | undefined;
102
+ /**
103
+ * The siblings (same `parent`) this object waits for, as row ids. The kernel
104
+ * passes it through untouched; clients draw it.
105
+ */
106
+ readonly after?: readonly string[] | undefined;
107
+ /**
108
+ * The object waits for a person although no run does, as a proposed task
109
+ * does. The row is drawn waiting, like one with a run waiting.
110
+ */
111
+ readonly waiting?: boolean | undefined;
112
+ }
113
+
114
+ /** One section a detail page offers. `live` means its content still grows. */
115
+ export interface ViewSection {
116
+ readonly id: string;
117
+ readonly title: string;
118
+ readonly live?: boolean | undefined;
119
+ }
120
+
121
+ /** Where a section read starts; the plugin owns the meaning of the values. */
122
+ export interface SectionReadOptions {
123
+ readonly cursor?: string | undefined;
124
+ /**
125
+ * Return only the finished content after this mark, a position an earlier
126
+ * read returned. The plugin owns the meaning of the value; a plugin that
127
+ * does not implement it answers with the whole section and leaves `delta` out.
128
+ */
129
+ readonly after?: string | undefined;
130
+ }
131
+
132
+ /** The Markdown of one section, and where to continue when it is paged. */
133
+ export interface SectionContent {
134
+ readonly markdown: string;
135
+ readonly next?: string | undefined;
136
+ /** A table of contents: one entry per page; the first page's cursor is absent. */
137
+ readonly pages?: readonly SectionPage[] | undefined;
138
+ /**
139
+ * An opaque position the finished content goes to; a client passes it back
140
+ * as `after`. The plugin owns its meaning.
141
+ */
142
+ readonly mark?: string | undefined;
143
+ /**
144
+ * The part that is still changing, as Markdown: always its whole current
145
+ * text, not only what changed since the last read.
146
+ */
147
+ readonly tail?: string | undefined;
148
+ /**
149
+ * `markdown` holds only what follows the requested `after`. Omit, or set
150
+ * false, when the answer is a whole page; the kernel then tells the client
151
+ * to replace instead of appending.
152
+ */
153
+ readonly delta?: boolean | undefined;
154
+ }
155
+
156
+ /** One page of a long section, as the section API takes it. */
157
+ export interface SectionPage {
158
+ readonly label: string;
159
+ readonly cursor?: string | undefined;
160
+ }
161
+
162
+ /** The context a `load` call receives. */
163
+ export interface ViewContext extends PluginBaseContext {
164
+ /**
165
+ * A run's own times from its facts: when it started and ended, and its
166
+ * steps and waits for a person in order, each with when it began and ended.
167
+ * Undefined for a run the log does not know. Absent when the kernel has no
168
+ * run log to read.
169
+ */
170
+ readonly runTimes?:
171
+ | ((runId: string) => Promise<
172
+ | {
173
+ readonly startedAt: number;
174
+ readonly endedAt: number | null;
175
+ readonly segments: readonly {
176
+ readonly step: string;
177
+ readonly person: boolean;
178
+ readonly from: number;
179
+ readonly to: number | null;
180
+ }[];
181
+ }
182
+ | undefined
183
+ >)
184
+ | undefined;
185
+ /**
186
+ * A plugin's settings as they are in `config.json` now, with defaults
187
+ * filled; the registry's settings when the file cannot be read. For a view
188
+ * that must follow an edit without a restart.
189
+ */
190
+ readonly settingsInForce?:
191
+ | ((plugin: string) => Readonly<Record<string, JsonValue>>)
192
+ | undefined;
193
+ /**
194
+ * An object after its type's `prepare` has run with the settings in force
195
+ * now, so a view can see a change without a restart. Undefined when the
196
+ * object is gone.
197
+ */
198
+ readonly refreshedObject?:
199
+ | ((type: string, id: string) => Promise<StoredObject | undefined>)
200
+ | undefined;
201
+ }
202
+
203
+ /**
204
+ * An access profile: opaque JSON the kernel hands to steps. Its shape belongs
205
+ * to `operad-sandbox.md`, not to the kernel.
206
+ */
207
+ export type AccessProfile = Readonly<Record<string, JsonValue>>;
208
+
209
+ /** A plugin's secrets, scoped to that plugin and stored outside the log. */
210
+ export interface SecretStore {
211
+ get(name: string): Promise<Secret<string> | undefined>;
212
+ set(name: string, value: string | Secret<string>): Promise<void>;
213
+ delete(name: string): Promise<void>;
214
+ names(): Promise<readonly string[]>;
215
+ }
216
+
217
+ /** How one object type appears to every client. */
218
+ export interface ObjectViews {
219
+ /** Must be pure and fast: it reads the stored object and nothing else. */
220
+ readonly row: (object: StoredObject) => ObjectRow;
221
+ /** The section directory only; it reads no section content. */
222
+ readonly sections: (object: StoredObject) => readonly ViewSection[];
223
+ /**
224
+ * Whether a list shows each row's `scope` next to its id, because more than
225
+ * one namespace exists. Read once per list.
226
+ */
227
+ readonly showScope?: ((ctx: ViewContext) => MaybePromise<boolean>) | undefined;
228
+ /** Called when a person opens a section; it may be slow and paged. */
229
+ readonly load: (
230
+ ctx: ViewContext,
231
+ object: StoredObject,
232
+ sectionId: string,
233
+ options: SectionReadOptions,
234
+ ) => MaybePromise<SectionContent>;
235
+ }
236
+
237
+ /** A key binding a plugin brings as its default. */
238
+ export type KeyBindings = Readonly<Record<string, string>>;
239
+
240
+ /** Names an object without carrying its state. */
241
+ export interface ObjectRef {
242
+ readonly type: string;
243
+ readonly id: string;
244
+ }
245
+
246
+ /** An object as a run sees it. */
247
+ export interface RunObject extends ObjectRef {
248
+ readonly data: ObjectData;
249
+ }
250
+
251
+ /** An object after it was stored, with the fact that produced this version. */
252
+ export interface StoredObject extends RunObject {
253
+ readonly version: number;
254
+ readonly cause: string;
255
+ readonly at: number;
256
+ }
257
+
258
+ /**
259
+ * One fact a plugin sees. Facts hold data and paths, never process output.
260
+ * The name stays apart from the storage backend's `Fact`, which is run-scoped.
261
+ */
262
+ export interface PluginFact {
263
+ readonly sequence: number;
264
+ readonly type: string;
265
+ readonly object: ObjectRef | undefined;
266
+ readonly cause: string;
267
+ readonly at: number;
268
+ readonly data: ObjectData;
269
+ }
270
+
271
+ /** A fact a plugin appends for itself, outside the objects it owns. */
272
+ export interface FactInput {
273
+ readonly type: string;
274
+ readonly data: ObjectData;
275
+ readonly object?: ObjectRef | undefined;
276
+ readonly cause?: string | undefined;
277
+ }
278
+
279
+ /**
280
+ * Options for reading one page of objects. `where` holds equality filters on
281
+ * fields the object type declared in `index`; `cursor` continues a page.
282
+ */
283
+ export interface ListOptions {
284
+ readonly where?: Readonly<Record<string, unknown>> | undefined;
285
+ readonly cursor?: string | undefined;
286
+ readonly limit?: number | undefined;
287
+ /** The fields `where` may name. The store normally learns them from `declare`. */
288
+ readonly index?: readonly string[] | undefined;
289
+ }
290
+
291
+ /** One page of objects, ordered by id, and where to continue. */
292
+ export interface ListPage {
293
+ readonly objects: readonly StoredObject[];
294
+ readonly next?: string | undefined;
295
+ }
296
+
297
+ /** Options for reading one object's facts. */
298
+ export interface FactsOptions {
299
+ readonly cursor?: string | undefined;
300
+ readonly limit?: number | undefined;
301
+ }
302
+
303
+ /** One page of an object's facts in sequence order, and where to continue. */
304
+ export interface FactsPage {
305
+ readonly facts: readonly PluginFact[];
306
+ readonly next?: string | undefined;
307
+ }
308
+
309
+ /**
310
+ * The object side of the store. A plugin reads and writes objects here and
311
+ * never touches the storage backend.
312
+ */
313
+ export interface ObjectStore {
314
+ /** The directory that holds this store's files, such as logs. */
315
+ readonly root: string;
316
+
317
+ /** Tells the store which fields a type allows `where` to filter on. */
318
+ declare?(type: string, index: readonly string[]): void;
319
+
320
+ /**
321
+ * Tells a listener whenever an object gains a fact. Optional: a store that
322
+ * cannot watch simply returns a no-op unsubscribe. With an object and a
323
+ * cursor, the same call subscribes to that object's facts instead.
324
+ */
325
+ subscribe(listener: (object: ObjectRef) => void): () => void;
326
+ subscribe(
327
+ object: ObjectRef,
328
+ options: FactsOptions,
329
+ listener: (fact: PluginFact) => void,
330
+ ): () => void;
331
+
332
+ /** Every fact in append order, when this store keeps them in memory. */
333
+ facts(): readonly PluginFact[];
334
+
335
+ /** One page of one object's facts in sequence order. */
336
+ facts(object: ObjectRef, options?: FactsOptions): FactsPage;
337
+
338
+ /** The distinct object types that have facts under this object id, sorted. */
339
+ objectTypesOf(id: string): readonly string[];
340
+
341
+ /**
342
+ * Calls `listener` for facts of `object` after `options.cursor`; without a
343
+ * cursor, only facts appended after the subscription starts.
344
+ */
345
+ subscribeFacts?(
346
+ object: ObjectRef,
347
+ options: FactsOptions,
348
+ listener: (fact: PluginFact) => void,
349
+ ): () => void;
350
+
351
+ /** Stores a new version of an object and appends the change as a fact. */
352
+ put(
353
+ type: string,
354
+ id: string,
355
+ data: ObjectData,
356
+ cause: string,
357
+ ): MaybePromise<StoredObject>;
358
+
359
+ get(type: string, id: string): MaybePromise<StoredObject | undefined>;
360
+
361
+ /** One page of objects of `type`, ordered by id and filtered by `where`. */
362
+ list(type: string, options?: ListOptions): MaybePromise<ListPage>;
363
+ }
364
+
365
+ /** Appends facts that are not an object change, such as a step's output path. */
366
+ export interface FactWriter {
367
+ append(input: FactInput): MaybePromise<PluginFact>;
368
+ }
369
+
370
+ /** A fact as a store takes it: the cause is always known by then. */
371
+ export interface StoreFactInput extends FactInput {
372
+ readonly cause: string;
373
+ }
374
+
375
+ /** How a worker reopens the store its run uses. */
376
+ export interface PluginStoreLocation {
377
+ readonly backend: "sqlite";
378
+ readonly root: string;
379
+ /** The SQLite file, when the backend keeps one file. */
380
+ readonly file?: string | undefined;
381
+ }
382
+
383
+ /** The store a plugin sees: objects plus its own facts. */
384
+ export interface PluginStore extends ObjectStore {
385
+ /** Appends a fact; the caller names its cause (the kernel fills it for plugins). */
386
+ append(input: StoreFactInput): MaybePromise<PluginFact>;
387
+
388
+ /**
389
+ * `file` stores live under one directory and a worker step sees the same
390
+ * data; `memory` stores exist only in this process.
391
+ */
392
+ readonly kind: "file" | "memory";
393
+
394
+ /** How a worker opens the same store; absent for an in-memory store. */
395
+ readonly location?: PluginStoreLocation | undefined;
396
+ }
397
+
398
+ /** Writes text to a file under the store and returns its path. */
399
+ export interface PluginLogger {
400
+ readonly path: string;
401
+ write(text: string): Promise<string>;
402
+ }
403
+
404
+ /** Fields every plugin hook receives. */
405
+ export interface PluginBaseContext {
406
+ readonly plugin: string;
407
+ readonly home: string;
408
+ readonly cwd: string;
409
+ readonly store: ObjectStore;
410
+ readonly facts: FactWriter;
411
+ readonly logger: PluginLogger;
412
+ readonly signal: AbortSignal;
413
+ /** The plugin's validated `config.json` section, with defaults filled. */
414
+ readonly settings: Readonly<Record<string, JsonValue>>;
415
+ /** The plugin's own secrets: never stored in the log. */
416
+ readonly secrets: SecretStore;
417
+ /** Set when the kernel can cancel a run for this plugin. */
418
+ readonly runs?: RunControl | undefined;
419
+ }
420
+
421
+ /** The answer a plugin action gives to the person wait it is answering. */
422
+ export type Answer = (choice: string, note?: string) => MaybePromise<void>;
423
+
424
+ /** What a plugin may ask the kernel to do with a run it owns. */
425
+ export interface RunControl {
426
+ /** Cancels a run through the kernel; an ended run is left alone. */
427
+ cancel(runId: string, flow?: FlowDefinition): MaybePromise<void>;
428
+ }
429
+
430
+ /** The result of one plugin's optional `check`. */
431
+ export interface PluginCheck {
432
+ readonly plugin: string;
433
+ readonly ok: boolean;
434
+ readonly message: string;
435
+ }
436
+
437
+ /** The longest grace, in seconds, a timer can hold (2^31-1 ms, rounded down). */
438
+ export const MAX_ASK_GRACE_SECS = 2_147_483;
439
+
440
+ /** What a step asks a person with `ctx.ask`. */
441
+ export interface AskRequest {
442
+ readonly prompt: string;
443
+ /** The answers offered; none means a free note only. */
444
+ readonly choices?: readonly string[] | undefined;
445
+ /**
446
+ * How long to wait for the answer, in seconds, while the step goes on
447
+ * waiting. The narrowest of this, the step's `askGraceSecs` param and the
448
+ * `ask.graceSecs` setting counts. `0` does not wait.
449
+ */
450
+ readonly graceSecs?: number | undefined;
451
+ /**
452
+ * What happens when the grace passes without an answer. `"waiting"` (the
453
+ * default) ends the step with the result `waiting`; `"reject_once"` returns
454
+ * to the step with `rejected: true`.
455
+ */
456
+ readonly onTimeout?: "waiting" | "reject_once" | undefined;
457
+ /** What the step needs to carry on later; handed back in the `waiting` data. */
458
+ readonly resume?: JsonValue | undefined;
459
+ }
460
+
461
+ /** The answer `ctx.ask` returns. */
462
+ export interface AskAnswer {
463
+ readonly choice: string | undefined;
464
+ readonly note?: string | undefined;
465
+ /** Who answered; absent when `rejected`. */
466
+ readonly actor?: Actor | undefined;
467
+ /** True when the grace passed and `onTimeout` was `"reject_once"`. */
468
+ readonly rejected?: boolean | undefined;
469
+ }
470
+
471
+ /** The data of a step that ended `waiting` because an ask timed out. */
472
+ export interface AskWaitingData {
473
+ readonly waitId: string;
474
+ readonly prompt: string;
475
+ readonly choices: readonly string[];
476
+ readonly resume: JsonValue;
477
+ }
478
+
479
+ /** What the host reports back for one question a step asked. */
480
+ export type AskOutcome =
481
+ | { readonly kind: "answered"; readonly answer: AskAnswer }
482
+ | {
483
+ readonly kind: "timeout";
484
+ readonly waitId: string;
485
+ readonly prompt: string;
486
+ readonly choices: readonly string[];
487
+ };
488
+
489
+ /**
490
+ * Where a step's questions go. `request.graceSecs` already holds the narrower of
491
+ * the call and the step's `askGraceSecs`; the host narrows it by its own
492
+ * setting.
493
+ */
494
+ export interface StepAsker {
495
+ ask(request: AskRequest, signal?: AbortSignal): Promise<AskOutcome>;
496
+ /**
497
+ * Uses up the one-time permission an answer gave. True when this call used
498
+ * it; false when it was used before or the step has none by that id. The fact
499
+ * that records the use is written before this returns.
500
+ */
501
+ useGrant(waitId: string): Promise<boolean>;
502
+ }
503
+
504
+ /** The context a step run receives. */
505
+ export interface StepContext extends PluginBaseContext {
506
+ readonly runId: string;
507
+ readonly object: RunObject | undefined;
508
+ /** Fixed JSON the flow was started with; null when it was given none. */
509
+ readonly params: JsonValue;
510
+ /** The run's access profile, opaque to the kernel; null when it has none. */
511
+ readonly access: JsonValue;
512
+ /** The event that started the run, with paths to its text. */
513
+ readonly trigger: TriggerView | undefined;
514
+ /** Sends an event; the type must begin with this plugin's name. */
515
+ emit(event: StepEvent): Promise<void>;
516
+ /**
517
+ * Asks a person and waits for the answer while the step lives. Only steps
518
+ * have it; a flow cannot ask. When the grace passes, `onTimeout` decides.
519
+ */
520
+ ask(request: AskRequest): Promise<AskAnswer>;
521
+ /**
522
+ * Uses up the one-time permission an answer gave this step (see
523
+ * `access.grants`). True when this call used it, false when it was already
524
+ * used. Call it before the call the permission covers.
525
+ */
526
+ useGrant(waitId: string): Promise<boolean>;
527
+ /** The caller's receiver of log lines, when it gave one; see `StepRunOptions.receiver`. */
528
+ readonly receiver?: StepLineReceiver | undefined;
529
+ /**
530
+ * Tells the kernel this step waits for a place the plugin manages itself
531
+ * (not a step limit), under `key`; the running view shows it queued. Call
532
+ * the returned function when the wait is over. `ahead` is how many
533
+ * waiters the plugin says are before this one in its own order; the running
534
+ * view shows it instead of counting reports. To change it, end the wait and
535
+ * report again. Undefined when the host has
536
+ * no limiter.
537
+ */
538
+ readonly queued?:
539
+ | ((key: string, ahead?: number) => Promise<() => Promise<void>>)
540
+ | undefined;
541
+ /** Whether a plugin that declares this step is loaded now. */
542
+ readonly stepLoaded?: ((use: string) => boolean) | undefined;
543
+ /** The models a loaded harness plugin declares; undefined when it is not loaded or declares none. */
544
+ readonly harnessModels?: ((harness: string) => HarnessModels | undefined) | undefined;
545
+ /** The efforts a loaded harness plugin declares; undefined when it is not loaded or declares none. */
546
+ readonly harnessEfforts?: ((harness: string) => HarnessEfforts | undefined) | undefined;
547
+ /**
548
+ * Runs another loaded step in this run. The kernel offers it when a plugin
549
+ * step delegates to a harness or another plugin; a direct call to a step's
550
+ * `run` has no host and leaves it undefined.
551
+ */
552
+ readonly runStep?:
553
+ | ((
554
+ use: string,
555
+ params: unknown,
556
+ options: StepRunOptions,
557
+ ) => MaybePromise<PluginStepResult>)
558
+ | undefined;
559
+ }
560
+
561
+ /** What a step returns. `result` must be one of the step type's results. */
562
+ export interface PluginStepResult {
563
+ readonly result: string;
564
+ readonly data?: ObjectData | undefined;
565
+ }
566
+
567
+ /**
568
+ * Where a step type's concurrency is limited. `key` names what is shared (a
569
+ * model, an account); `undefined` means this call is not limited. `concurrent`
570
+ * is how many calls under one key may run at once across the operad home.
571
+ */
572
+ export interface StepLimitDefinition {
573
+ readonly key: (
574
+ params: unknown,
575
+ settings: Readonly<Record<string, JsonValue>>,
576
+ ) => string | undefined;
577
+ readonly concurrent: (
578
+ params: unknown,
579
+ settings: Readonly<Record<string, JsonValue>>,
580
+ ) => number;
581
+ }
582
+
583
+ /** A held place under a limit key; `release` gives it back. */
584
+ export interface StepLimitRequest {
585
+ readonly use: string;
586
+ readonly key: string;
587
+ readonly concurrent: number;
588
+ readonly signal?: AbortSignal | undefined;
589
+ }
590
+
591
+ /** Hands out places under limit keys to steps a step starts with `runStep`. */
592
+ export interface StepLimiter {
593
+ acquire(request: StepLimitRequest): Promise<() => Promise<void>>;
594
+ /**
595
+ * Records that the step waits for a place that it manages itself, so the
596
+ * running view shows the run queued under `key`. The returned function ends
597
+ * the wait. The kernel grants nothing for it.
598
+ */
599
+ report(request: {
600
+ readonly key: string;
601
+ readonly ahead?: number | undefined;
602
+ }): Promise<() => Promise<void>>;
603
+ }
604
+
605
+ /** Takes one log line of a step as it is written. */
606
+ export type StepLineReceiver = (line: string) => void;
607
+
608
+ /** Options for running a step through the registry. */
609
+ export interface StepRunOptions {
610
+ readonly runId: string;
611
+ /** Limits the steps this step starts through `ctx.runStep`. */
612
+ readonly limits?: StepLimiter | undefined;
613
+ /** Takes the questions the step asks with `ctx.ask`. */
614
+ readonly asker?: StepAsker | undefined;
615
+ /**
616
+ * Takes each line a harness step writes to its log, as the text of the line.
617
+ * Opaque to the kernel: only a harness step calls it, and only for the step
618
+ * it is given to (a nested step does not inherit it).
619
+ */
620
+ readonly receiver?: StepLineReceiver | undefined;
621
+ readonly object?: RunObject | undefined;
622
+ readonly cwd?: string | undefined;
623
+ readonly signal?: AbortSignal | undefined;
624
+ readonly params?: JsonValue | undefined;
625
+ /** The run's access profile; the kernel only checks who enforces it. */
626
+ readonly access?: JsonValue | undefined;
627
+ readonly trigger?: TriggerView | undefined;
628
+ /** Sends a step event; the plugin is the one that registered the step. */
629
+ readonly emit?:
630
+ | ((plugin: string, event: StepEvent) => MaybePromise<void>)
631
+ | undefined;
632
+ /** Sees each event this step sends, besides the destination; only the step it is given to. */
633
+ readonly observe?:
634
+ | ((plugin: string, event: StepEvent) => MaybePromise<void>)
635
+ | undefined;
636
+ }
637
+
638
+ /** A step type a plugin declares. The use name is `<plugin>/<name>`. */
639
+ export interface StepTypeDefinition {
640
+ readonly name: string;
641
+ /**
642
+ * True when the step can run again from where a question left it (it takes
643
+ * `resumeSession` and `askAnswer` in its params). A step that says `false`
644
+ * has every question it asks forced to `onTimeout: "reject_once"`.
645
+ */
646
+ readonly resumable?: boolean | undefined;
647
+ /**
648
+ * False when the step cannot let one call through and no other. `ctx.ask`
649
+ * then refuses a question that offers a one-time permission, before anyone
650
+ * is asked.
651
+ */
652
+ readonly grants?: boolean | undefined;
653
+ readonly results: readonly string[];
654
+ /**
655
+ * The steps this one calls through `ctx.runStep`, as `plugin/step`. A run
656
+ * does not start when one of them is not loaded.
657
+ */
658
+ readonly calls?: readonly string[] | undefined;
659
+ /**
660
+ * The step also calls steps that settings or an object name (a chat's
661
+ * `refresh` step), so `calls` cannot list them. A run does not wait on them
662
+ * to start; a call to one that is not loaded fails where it is made.
663
+ */
664
+ readonly callsNamedByData?: boolean | undefined;
665
+ /** Access profile fields this step applies, such as `denyPaths`. */
666
+ readonly enforces?: readonly string[] | undefined;
667
+ /**
668
+ * How long one run of this step may take, in seconds. Default: the runner's
669
+ * step timeout (5 minutes). A step that runs an agent or a test suite sets
670
+ * it above its own limits.
671
+ */
672
+ readonly timeoutSecs?: number | undefined;
673
+ /** Limits how many runs of this step may be going at once. */
674
+ readonly limit?: StepLimitDefinition | undefined;
675
+ readonly run: (
676
+ ctx: StepContext,
677
+ params: unknown,
678
+ ) => MaybePromise<PluginStepResult>;
679
+ }
680
+
681
+ /** What an object type's `resolve` receives. `cwd` is where the person is. */
682
+ export interface ObjectResolveContext {
683
+ readonly cwd: string;
684
+ /** A namespace the person named explicitly (`--repo <name>`), when they did. */
685
+ readonly scope?: string | undefined;
686
+ readonly store: ObjectStore;
687
+ }
688
+
689
+ /**
690
+ * A named subset of a type's rows, by state. A row matches when its `state` is
691
+ * in `states`. The kernel adds an implicit `all` filter; a plugin may not
692
+ * declare that name.
693
+ */
694
+ export interface ObjectFilterDefinition {
695
+ readonly name: string;
696
+ readonly states: readonly string[];
697
+ /** At most one filter is the default; with none marked, the first is. */
698
+ readonly default?: boolean | undefined;
699
+ }
700
+
701
+ /** An object type a plugin owns, such as `task`. */
702
+ export interface ObjectTypeDefinition {
703
+ readonly name: string;
704
+ readonly schema?: unknown;
705
+ /**
706
+ * Turns a bare id into the object's store key, or undefined when this type
707
+ * does not claim it. The kernel asks this before its own suffix lookup, so a
708
+ * plugin can scope an id to a repository or another namespace.
709
+ */
710
+ readonly resolve?: (
711
+ ctx: ObjectResolveContext,
712
+ type: string,
713
+ id: string,
714
+ ) => MaybePromise<string | undefined>;
715
+ /**
716
+ * Brings this type's stored objects up to date (for example after a rename)
717
+ * before a client reads them. Views run it once per instance, before the
718
+ * first list or get; it must be safe to run again.
719
+ */
720
+ readonly prepare?: (ctx: {
721
+ readonly store: ObjectStore;
722
+ readonly home: string;
723
+ /** The owning plugin's validated settings. */
724
+ readonly settings: Readonly<Record<string, JsonValue>>;
725
+ }) => MaybePromise<void>;
726
+ /**
727
+ * Whether the plugin itself will answer this object's wait for a person
728
+ * automatically. It runs each time the pending view is built and returns
729
+ * what holds now, so a wait whose automatic answer is refused, or whose
730
+ * auto level changes, stops being marked.
731
+ */
732
+ readonly answeredAutomatically?:
733
+ | ((
734
+ data: Record<string, unknown>,
735
+ ctx: { readonly store: ObjectStore },
736
+ ) => MaybePromise<boolean>)
737
+ | undefined;
738
+ /** The fields a person may filter on with `list(..., { where })`. */
739
+ readonly index?: readonly string[] | undefined;
740
+ /** Ordered row filters for lists; see {@link ObjectFilterDefinition}. */
741
+ readonly filters?: readonly ObjectFilterDefinition[] | undefined;
742
+ }
743
+
744
+ /** The value types an action argument may have. */
745
+ export type ActionArgType =
746
+ | "string"
747
+ | "number"
748
+ | "integer"
749
+ | "boolean"
750
+ | "array"
751
+ | "object";
752
+
753
+ /**
754
+ * One property of an action's arguments. It is the JSON Schema subset the
755
+ * kernel validates: `type`, `enum`, `minimum`, `maximum`, `minLength`,
756
+ * `maxLength`, `items`, `properties`, `required`, `additionalProperties` (`false` or a schema for the other fields),
757
+ * `description` and `role`.
758
+ */
759
+ export interface ActionArgSchema {
760
+ readonly type?: ActionArgType | undefined;
761
+ readonly enum?: readonly (string | number | boolean)[] | undefined;
762
+ readonly minimum?: number | undefined;
763
+ readonly maximum?: number | undefined;
764
+ readonly minLength?: number | undefined;
765
+ readonly maxLength?: number | undefined;
766
+ readonly items?: ActionArgSchema | undefined;
767
+ readonly minItems?: number | undefined;
768
+ readonly exclusiveMinimum?: number | undefined;
769
+ readonly exclusiveMaximum?: number | undefined;
770
+ readonly minProperties?: number | undefined;
771
+ /** A regular expression the string must match somewhere. */
772
+ readonly pattern?: string | undefined;
773
+ /** The one value allowed. */
774
+ readonly const?: unknown;
775
+ /** In a declaration: a reference `#/$defs/<name>`, replaced at load by what it names. */
776
+ readonly $ref?: string | undefined;
777
+ /** In a declaration's root: the schemas a `$ref` may name. */
778
+ readonly $defs?: Readonly<Record<string, unknown>> | undefined;
779
+ /** Exactly one of these must fit. A `$ref` is already replaced by what it names. */
780
+ readonly oneOf?: readonly ActionArgSchema[] | undefined;
781
+ /**
782
+ * Conditionals a client reads to narrow a field by another (`if`/`then`, as
783
+ * JSON Schema). Only an action's `argsFor` result carries them; the kernel
784
+ * does not evaluate them when it validates the person's values.
785
+ */
786
+ readonly allOf?: readonly ActionArgSchema[] | undefined;
787
+ readonly if?: ActionArgSchema | undefined;
788
+ readonly then?: ActionArgSchema | undefined;
789
+ readonly properties?: Readonly<Record<string, ActionArgSchema>> | undefined;
790
+ readonly required?: readonly string[] | undefined;
791
+ readonly additionalProperties?: false | ActionArgSchema | undefined;
792
+ /** A value the kernel fills when the field is absent. */
793
+ readonly default?: unknown;
794
+ readonly description?: string | undefined;
795
+ /** A note is prompted for in a terminal and accepted as `--note`. */
796
+ readonly role?: "note" | undefined;
797
+ /**
798
+ * `"file"`: the value is a file a client uploaded; the client passes the
799
+ * upload's reference and the action runs with the stored path.
800
+ */
801
+ readonly format?: "file" | undefined;
802
+ /** In a settings schema: a client may change this setting. */
803
+ readonly editable?: boolean | undefined;
804
+ /** In a settings schema: changing it needs the person's confirmation. */
805
+ readonly confirm?: boolean | undefined;
806
+ }
807
+
808
+ /** The arguments of an action: a JSON Schema object. */
809
+ export interface ActionArgsSchema extends ActionArgSchema {
810
+ readonly type: "object";
811
+ }
812
+
813
+ /** What an action's checks may read: the object store, never write. */
814
+ export interface ActionEnv {
815
+ readonly store: ObjectStore;
816
+ /** The harness plugins loaded now; the kernel always sets it. */
817
+ readonly harnesses?: readonly string[] | undefined;
818
+ /** The models each loaded harness plugin declares, by plugin name. */
819
+ readonly models?: Readonly<Record<string, HarnessModels>> | undefined;
820
+ /** The efforts each loaded harness plugin declares, by plugin name. */
821
+ readonly efforts?: Readonly<Record<string, HarnessEfforts>> | undefined;
822
+ }
823
+
824
+ /** The models a harness plugin declares: those a chat may pick, and the one used when none is set. */
825
+ export interface HarnessModels {
826
+ readonly list: readonly string[];
827
+ readonly default: string | null;
828
+ }
829
+
830
+ /**
831
+ * The efforts a harness plugin declares: those a chat may pick, the one used
832
+ * when none is set, and the step argument that sets it (`--effort` when absent).
833
+ */
834
+ export interface HarnessEfforts {
835
+ readonly list: readonly string[];
836
+ readonly default: string | null;
837
+ readonly flag?: string | undefined;
838
+ }
839
+
840
+ /** An action a plugin offers on an object type. */
841
+ export interface ActionTypeDefinition {
842
+ readonly name: string;
843
+ readonly on: string;
844
+ /** The person's input, as a JSON Schema object. */
845
+ readonly args?: ActionArgsSchema | undefined;
846
+ /**
847
+ * The person's input for this object right now, when it depends on what is
848
+ * loaded; it is computed each time the action is offered and checked, and
849
+ * replaces `args` (which stays the static form for help).
850
+ */
851
+ readonly argsFor?: ((object: RunObject, env: ActionEnv) => ActionArgsSchema) | undefined;
852
+ /** The action asks for confirmation before it runs. */
853
+ readonly confirm?: boolean | undefined;
854
+ /**
855
+ * The action ends the object (a cancel). A client's main key or button never
856
+ * picks it, even when it is the only action offered.
857
+ */
858
+ readonly ends?: boolean | undefined;
859
+ /**
860
+ * The object type the action creates (`"chat"`). A client keeps such an action
861
+ * out of the parent's page and offers it where that type is created; it is
862
+ * never an object's main action.
863
+ */
864
+ readonly creates?: string | undefined;
865
+ readonly when: (state: ObjectData, env: ActionEnv) => MaybePromise<boolean>;
866
+ /**
867
+ * Where the action stands among the object's available actions: lower comes
868
+ * first, equal ranks keep the plugin's order, none is 0. The first available
869
+ * action is the main one (a client's main key or button), so a plugin ranks
870
+ * what a person most likely wants next ahead of an action that ends the object.
871
+ */
872
+ readonly rank?: ((state: ObjectData) => number) | undefined;
873
+ /**
874
+ * Why `when` refuses this object, in one line that starts with the object's
875
+ * own name and says when the action becomes possible. A client prints it
876
+ * when the action is not available.
877
+ */
878
+ readonly why?:
879
+ | ((object: StoredObject, env: ActionEnv) => MaybePromise<string | undefined>)
880
+ | undefined;
881
+ /**
882
+ * The line to print when the action's result is already true (nothing to
883
+ * do), or undefined. The action then succeeds with that line and does not
884
+ * run; it is checked before `when`, with the arguments as the person sent them.
885
+ */
886
+ readonly settled?:
887
+ | ((
888
+ object: StoredObject,
889
+ args: unknown,
890
+ env: ActionEnv,
891
+ ) => MaybePromise<string | undefined>)
892
+ | undefined;
893
+ /**
894
+ * Returns the line to show, or an `ActionOutcome` when the action created an
895
+ * object a client should open. Any other value is the result as it is.
896
+ */
897
+ readonly run: (ctx: ActionContext, args: unknown) => MaybePromise<unknown>;
898
+ }
899
+
900
+ /**
901
+ * What an action returns when it created an object: the line of text a person
902
+ * reads, and `opens`, the object as `type:id`. Returning only text still works.
903
+ * The kernel, the client API and HTTP pass `opens` on unchanged.
904
+ */
905
+ export interface ActionOutcome {
906
+ readonly text: string;
907
+ readonly opens: string;
908
+ }
909
+
910
+ /** Whether an action's return value is an `ActionOutcome`. */
911
+ export function isActionOutcome(value: unknown): value is ActionOutcome {
912
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
913
+ const keys = Object.keys(value);
914
+ const record = value as { readonly text?: unknown; readonly opens?: unknown };
915
+ return (
916
+ keys.length === 2 &&
917
+ typeof record.text === "string" &&
918
+ typeof record.opens === "string"
919
+ );
920
+ }
921
+
922
+ /** An event an action sends: the person's client is the source. */
923
+ export interface ActionEvent {
924
+ /** Where it came from, such as `ios`; never `operad`. */
925
+ readonly source: string;
926
+ readonly type: string;
927
+ /** Makes a repeat of the same event a no-op. */
928
+ readonly key: string;
929
+ /** The object it is about, as `<type>:<id>`. */
930
+ readonly object?: string | undefined;
931
+ readonly summary?: string | undefined;
932
+ readonly data?: JsonValue | undefined;
933
+ }
934
+
935
+ /** The context an action receives. `runId` is set when it answers a person wait. */
936
+ export interface ActionContext extends PluginBaseContext {
937
+ /** The names of the loaded plugins of kind `harness`. */
938
+ readonly harnesses: readonly string[];
939
+ /** The models each loaded harness plugin declares, by plugin name. */
940
+ readonly models: Readonly<Record<string, HarnessModels>>;
941
+ /** The efforts each loaded harness plugin declares, by plugin name. */
942
+ readonly efforts: Readonly<Record<string, HarnessEfforts>>;
943
+ /** Sends an event through the kernel's rules; set when the host can route events. */
944
+ readonly emit?: ((event: ActionEvent) => Promise<void>) | undefined;
945
+ readonly runId: string | undefined;
946
+ readonly object: RunObject;
947
+ /** Who asked for the action: a person through a client. */
948
+ readonly actor: Actor;
949
+ /** Answers the run's current person wait; set when the action runs for a run. */
950
+ readonly answer?: Answer | undefined;
951
+ }
952
+
953
+ /** Options for running an action through the registry. */
954
+ export interface ActionRunOptions {
955
+ readonly object: RunObject | StoredObject;
956
+ readonly runId?: string | undefined;
957
+ readonly cwd?: string | undefined;
958
+ readonly signal?: AbortSignal | undefined;
959
+ readonly args?: unknown;
960
+ readonly actor: Actor;
961
+ }
962
+
963
+ /** A CLI verb a plugin declares. */
964
+ export interface CommandDefinition {
965
+ readonly name: string;
966
+ /** Usage text for the verb's arguments, such as `"<id> [--force]"`. */
967
+ readonly args: string;
968
+ readonly run: (
969
+ ctx: CommandContext,
970
+ argv: readonly string[],
971
+ ) => MaybePromise<unknown>;
972
+ }
973
+
974
+ /** The context a command verb receives. */
975
+ export interface CommandContext extends PluginBaseContext {
976
+ /** The text the command reads from stdin, when its caller supplied one. */
977
+ readonly input?: string | undefined;
978
+ }
979
+
980
+ /** Options for running a command through the registry. */
981
+ export interface CommandRunOptions {
982
+ readonly cwd?: string | undefined;
983
+ readonly signal?: AbortSignal | undefined;
984
+ /** The text the command reads from stdin; absent means the real stdin. */
985
+ readonly input?: string | undefined;
986
+ }
987
+
988
+ /** Where a command resolved: always `<plugin> <verb>`, optionally `<verb>`. */
989
+ export interface ResolvedCommand {
990
+ readonly plugin: string;
991
+ readonly command: string;
992
+ readonly argv: readonly string[];
993
+ }
994
+
995
+ /** A command that ran, with the value its plugin returned. */
996
+ export interface CommandRun extends ResolvedCommand {
997
+ readonly value: unknown;
998
+ }
999
+
1000
+ /** An action that ran, with the value its plugin returned. */
1001
+ export interface ActionRun {
1002
+ readonly plugin: string;
1003
+ readonly action: string;
1004
+ readonly value: unknown;
1005
+ /** The object the action created, as `type:id`, when it said so. */
1006
+ readonly opens?: string | undefined;
1007
+ }
1008
+
1009
+ /** Asks the kernel to start a run of one of the plugin's flows. */
1010
+ export interface StartRunRequest {
1011
+ readonly plugin: string;
1012
+ readonly flow: string;
1013
+ readonly object?: ObjectRef | undefined;
1014
+ /** The access profile the run starts with, opaque to the kernel. */
1015
+ readonly access?: JsonValue | undefined;
1016
+ readonly params?: JsonValue | undefined;
1017
+ readonly trigger?: TriggerEvent | undefined;
1018
+ /** Internal: an event already recorded as a global input. */
1019
+ readonly inputId?: string | undefined;
1020
+ }
1021
+
1022
+ /** What `ctx.start` accepts on a tick: a flow name, or a full request. */
1023
+ export interface TickStartRequest {
1024
+ readonly flow: string;
1025
+ readonly params?: JsonValue | undefined;
1026
+ readonly object?: ObjectRef | string | undefined;
1027
+ readonly trigger?: TriggerEvent | undefined;
1028
+ /** The access profile the run starts with, opaque to the kernel. */
1029
+ readonly access?: JsonValue | undefined;
1030
+ }
1031
+
1032
+ /**
1033
+ * What a plugin may ask the kernel about runs while it ticks. `serve` supplies
1034
+ * it; a registry used on its own leaves it undefined.
1035
+ */
1036
+ export interface RunLookup {
1037
+ /** The active runs, optionally only those on one object key. */
1038
+ active(object?: string): Promise<readonly RunInfo[]>;
1039
+ /** The state a run's log derives, or undefined when it cannot be read. */
1040
+ state(runId: string): Promise<KernelState | undefined>;
1041
+ /** The last step a run entered, so a repair can name it. */
1042
+ lastStep(runId: string): Promise<string | null>;
1043
+ }
1044
+
1045
+ /** The context the kernel loop passes to a plugin's `onTick`. */
1046
+ export interface TickContext extends PluginBaseContext {
1047
+ /** Starts a run of a flow this plugin declares. */
1048
+ start(
1049
+ flowOrRequest: string | TickStartRequest,
1050
+ object?: ObjectRef,
1051
+ access?: JsonValue,
1052
+ ): Promise<void>;
1053
+ /**
1054
+ * Adds a flow this plugin generated, so `start` can name it and a later
1055
+ * process that sees the same run gets the same graph. Declaring a name again
1056
+ * with the same definition is a no-op; a different definition under a name
1057
+ * already in use is refused.
1058
+ */
1059
+ declareFlow(flow: FlowDefinition): void;
1060
+ /**
1061
+ * Answers the current person wait of a run this plugin owns. The kernel's
1062
+ * configured answer handler records the answer; a run that no longer waits
1063
+ * rejects with a `StateError`.
1064
+ */
1065
+ answer(runId: string, choice: string, note?: string): Promise<void>;
1066
+ /** What the kernel knows about runs, when the loop supplies it. */
1067
+ readonly runLookup?: RunLookup | undefined;
1068
+ /** Set when the kernel can cancel a run for this plugin. */
1069
+ readonly runs?: RunControl | undefined;
1070
+ /** The names of the plugins this process loaded. */
1071
+ readonly loaded: readonly string[];
1072
+ }
1073
+
1074
+ /** One thing a plugin asks a person to know about; it waits on no run. */
1075
+ export interface StandingItem {
1076
+ /** Names the item within its plugin; the same id is the same item. */
1077
+ readonly id: string;
1078
+ /** What the person reads. */
1079
+ readonly prompt: string;
1080
+ /**
1081
+ * The object the item is about. The item then carries the actions of the
1082
+ * plugins whose `when` holds for it, and the object's row, like a waiting run.
1083
+ */
1084
+ readonly object?: ObjectRef | undefined;
1085
+ /** False keeps a channel from announcing the item; it is still pending. */
1086
+ readonly announce?: boolean | undefined;
1087
+ /** Other objects the item stands for, such as the proposed tasks of a draft plan. */
1088
+ readonly covers?: readonly ObjectRef[] | undefined;
1089
+ }
1090
+
1091
+ /** The context a plugin's `standing` hook receives. */
1092
+ export interface StandingContext extends PluginBaseContext {
1093
+ /** The names of the plugins this process loaded. */
1094
+ readonly loaded: readonly string[];
1095
+ }
1096
+
1097
+ /** A plugin's declared dependencies: plugins and the plugin API version. */
1098
+ export interface PluginRequires {
1099
+ readonly plugins?: Readonly<Record<string, string>> | undefined;
1100
+ readonly api?: string | undefined;
1101
+ }
1102
+
1103
+ /** A running source plugin. */
1104
+ export interface SourceHandle {
1105
+ stop(): MaybePromise<void>;
1106
+ }
1107
+
1108
+ /** The context a source plugin's `start` receives. */
1109
+ export interface SourceContext extends PluginBaseContext {
1110
+ /** The routing rules, already checked. */
1111
+ readonly rules: readonly TriggerRule[];
1112
+ readonly clock: () => number;
1113
+ /** Sends one event through the kernel's rules. */
1114
+ emit(event: SourceEvent): Promise<void>;
1115
+ }
1116
+
1117
+ /** The shape `definePlugin` accepts. The version is added by the helper. */
1118
+ export interface PluginShape {
1119
+ readonly name: string;
1120
+ readonly kind: PluginKind;
1121
+ readonly version: string;
1122
+ readonly objects?: readonly ObjectTypeDefinition[] | undefined;
1123
+ readonly actions?: readonly ActionTypeDefinition[] | undefined;
1124
+ readonly steps?: readonly StepTypeDefinition[] | undefined;
1125
+ readonly flows?: readonly FlowDefinition[] | undefined;
1126
+ readonly commands?: readonly CommandDefinition[] | undefined;
1127
+ /** How each object type appears in a list and a detail page. */
1128
+ readonly views?: Readonly<Record<string, ObjectViews>> | undefined;
1129
+ /** Default key bindings, keyed by key, valued as `<plugin>:<action>`. */
1130
+ readonly keys?: KeyBindings | undefined;
1131
+ /**
1132
+ * Default trigger rules this plugin ships. A user rule with the same source
1133
+ * and type replaces every plugin rule for that pair.
1134
+ */
1135
+ readonly rules?: readonly TriggerRule[] | undefined;
1136
+ readonly onTick?: ((ctx: TickContext) => MaybePromise<void>) | undefined;
1137
+ /**
1138
+ * Items for the pending view that wait on no run: a problem the plugin sees
1139
+ * and a person should act on. The hook runs each time the view is built and
1140
+ * returns the items that hold now, so an item disappears when it is fixed.
1141
+ */
1142
+ readonly standing?:
1143
+ | ((ctx: StandingContext) => MaybePromise<readonly StandingItem[]>)
1144
+ | undefined;
1145
+ /** The JSON Schema of this plugin's `config.json` settings section. */
1146
+ readonly settings?: ActionArgsSchema | undefined;
1147
+ /**
1148
+ * True when the plugin's objects belong under a client's settings view
1149
+ * instead of a tab of their own (a bot: its page is a bot's settings). A
1150
+ * client gives such a plugin no tab; the types listing marks each of its
1151
+ * types `settings: true`.
1152
+ */
1153
+ readonly objectsUnderSettings?: boolean | undefined;
1154
+ /** Settings files this plugin keeps under a registered repository's `.operad/`. */
1155
+ readonly projectSettings?: ProjectSettingsDeclaration | undefined;
1156
+ /**
1157
+ * A harness plugin: the models a chat may pick (empty list: none can be
1158
+ * chosen). A function is called with the plugin's settings each time the
1159
+ * list is read, so a harness can ask the agent itself.
1160
+ */
1161
+ readonly models?:
1162
+ | HarnessModels
1163
+ | ((settings: Readonly<Record<string, JsonValue>>) => HarnessModels)
1164
+ | undefined;
1165
+ /** A harness plugin: the efforts a chat may pick (empty list: none can be chosen). */
1166
+ readonly efforts?: HarnessEfforts | undefined;
1167
+ /** Access profile fields this plugin's steps apply. */
1168
+ readonly enforces?: readonly string[] | undefined;
1169
+ /** What this plugin needs loaded before it can be used. */
1170
+ readonly requires?: PluginRequires | undefined;
1171
+ /** Starts a source; `operad serve` stops it on exit. */
1172
+ readonly start?: ((ctx: SourceContext) => MaybePromise<SourceHandle> | SourceHandle) | undefined;
1173
+ /** Runs when `operad init` checks the plugin; a string is the check's note. */
1174
+ readonly check?: ((ctx: PluginBaseContext) => MaybePromise<string | void>) | undefined;
1175
+ }
1176
+
1177
+ /** One settings file under a repository's `.operad/`, with its schema. */
1178
+ export interface ProjectSettingsFile {
1179
+ /**
1180
+ * The file's name in a setting's qualified name: `<plugin>.<name>.<path>`,
1181
+ * or `<plugin>.<path>` when the name equals the plugin's name.
1182
+ */
1183
+ readonly name: string;
1184
+ /** Where the file is, relative to the repository's `.operad/`. */
1185
+ readonly path: string;
1186
+ readonly schema: ActionArgsSchema;
1187
+ }
1188
+
1189
+ /** A repository a plugin has registered. */
1190
+ export interface SettingsRepository {
1191
+ readonly name: string;
1192
+ readonly path: string;
1193
+ }
1194
+
1195
+ /** The project settings a plugin declares. */
1196
+ export interface ProjectSettingsDeclaration {
1197
+ /** The registered repositories whose `.operad/` holds the files. */
1198
+ readonly repositories: (ctx: {
1199
+ readonly store: ObjectStore;
1200
+ }) => MaybePromise<readonly SettingsRepository[]>;
1201
+ readonly files: readonly ProjectSettingsFile[];
1202
+ }
1203
+
1204
+ /** A plugin definition ready for the registry. */
1205
+ export interface PluginDefinition extends PluginShape {
1206
+ readonly operadPlugin: typeof OPERAD_PLUGIN_VERSION;
1207
+ }
1208
+
1209
+ /** A run waiting for a person. The kernel supplies these to the pending view. */
1210
+ export interface WaitingRun {
1211
+ readonly runId: string;
1212
+ readonly object: ObjectRef;
1213
+ readonly prompt?: string | undefined;
1214
+ readonly choices?: readonly string[] | undefined;
1215
+ /**
1216
+ * When the run last recorded anything. A run whose object is not in the store
1217
+ * takes this as the time of its placeholder object, so an age shown for it
1218
+ * is the run's own.
1219
+ */
1220
+ readonly lastFactAt?: number | undefined;
1221
+ }
1222
+
1223
+ /** One action the pending view offers for a waiting run. */
1224
+ export interface PendingActionView {
1225
+ readonly plugin: string;
1226
+ readonly action: string;
1227
+ }
1228
+
1229
+ /** An entry in the pending view: a waiting run, its object and its actions. */
1230
+ export interface PendingEntry {
1231
+ readonly runId: string;
1232
+ readonly prompt: string | undefined;
1233
+ readonly choices: readonly string[] | undefined;
1234
+ readonly object: StoredObject;
1235
+ readonly actions: readonly PendingActionView[];
1236
+ }