@operad-io/engine 0.1.1 → 0.3.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.
- package/config.schema.json +2 -1
- package/package.json +1 -1
- package/src/config/config.ts +74 -45
- package/src/flow/types.ts +4 -0
- package/src/flow/validate.ts +5 -0
- package/src/http/index.ts +2 -0
- package/src/http/plugins.ts +68 -0
- package/src/http/server.ts +342 -48
- package/src/kernel/decide.ts +115 -30
- package/src/kernel/state.ts +9 -0
- package/src/kernel/types.ts +25 -40
- package/src/log/open-log.ts +2 -5
- package/src/log/sqlite.ts +127 -85
- package/src/log/types.ts +35 -9
- package/src/loop/code-watch.ts +108 -2
- package/src/loop/older-code.ts +3 -4
- package/src/loop/serve.ts +122 -95
- package/src/notify/notify.ts +2 -6
- package/src/platform/atomic-write.ts +163 -0
- package/src/platform/file-lock.ts +43 -21
- package/src/platform/git-program.ts +108 -0
- package/src/platform/index.ts +3 -0
- package/src/platform/kept-workers.ts +290 -0
- package/src/platform/process-group.ts +11 -3
- package/src/platform/sqlite-open.ts +20 -6
- package/src/platform/sqlite-schema.ts +30 -0
- package/src/platform/step-reap.ts +33 -18
- package/src/platform/watch.ts +1 -11
- package/src/plugin/action-schema.ts +59 -1
- package/src/plugin/choice-info.ts +133 -0
- package/src/plugin/context.ts +279 -0
- package/src/plugin/declared-flows.ts +3 -10
- package/src/plugin/errors.ts +9 -0
- package/src/plugin/freeze.ts +12 -0
- package/src/plugin/harness.ts +28 -0
- package/src/plugin/index.ts +8 -7
- package/src/plugin/install.ts +265 -17
- package/src/plugin/keys.ts +2 -12
- package/src/plugin/load.ts +47 -23
- package/src/plugin/logger.ts +14 -0
- package/src/plugin/open-store.ts +0 -6
- package/src/plugin/question-form.ts +323 -0
- package/src/plugin/registry.ts +519 -76
- package/src/plugin/secrets.ts +14 -24
- package/src/plugin/semver.ts +52 -0
- package/src/plugin/settings.ts +1 -9
- package/src/plugin/sqlite-store.ts +94 -64
- package/src/plugin/store.ts +5 -0
- package/src/plugin/types.ts +575 -21
- package/src/plugin/validate.ts +402 -26
- package/src/runner/answer.ts +26 -68
- package/src/runner/ask.ts +227 -28
- package/src/runner/cancel.ts +10 -7
- package/src/runner/grants.ts +2 -2
- package/src/runner/index.ts +1 -3
- package/src/runner/limits.ts +2 -2
- package/src/runner/runner.ts +15 -14
- package/src/runner/service.ts +52 -19
- package/src/runner/step-worker.ts +9 -0
- package/src/testing/answer.ts +69 -0
- package/src/testing/clock-manual.ts +152 -0
- package/src/testing/config.ts +16 -0
- package/src/testing/fs.ts +172 -0
- package/src/testing/home.ts +43 -0
- package/src/testing/index.ts +19 -0
- package/src/testing/types.ts +2 -0
- package/src/testing/world.ts +9 -2
- package/src/trigger/kernel.ts +13 -46
- package/src/trigger/rules.ts +22 -12
- package/src/trigger/types.ts +2 -6
- package/src/util.ts +99 -0
- package/src/views/event-line.ts +22 -0
- package/src/views/history.ts +36 -19
- package/src/views/live-logs.ts +313 -0
- package/src/views/pending.ts +23 -5
- package/src/views/presentation.ts +629 -81
- package/src/views/running.ts +5 -5
- package/src/views/settings.ts +205 -66
- package/src/views/types.ts +8 -2
- package/src/views/uploads.ts +15 -7
- package/triggers.schema.json +4 -0
package/src/plugin/validate.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* normalization of the fields a plugin declares, and the rules that span all
|
|
4
4
|
* plugins of a set.
|
|
5
5
|
*/
|
|
6
|
+
import { isAbsolute } from "node:path";
|
|
6
7
|
import { ALL_FILTER } from "./filters.ts";
|
|
7
8
|
import type { FlowDefinition, StepType } from "../flow/types.ts";
|
|
8
9
|
import type { JsonValue } from "../log/types.ts";
|
|
@@ -19,7 +20,12 @@ import {
|
|
|
19
20
|
type ActionTypeDefinition,
|
|
20
21
|
type AskRequest,
|
|
21
22
|
type CommandDefinition,
|
|
23
|
+
type HarnessCapabilities,
|
|
22
24
|
type HarnessEfforts,
|
|
25
|
+
type HarnessEffortsSource,
|
|
26
|
+
type HarnessModelCapability,
|
|
27
|
+
type HarnessModelContext,
|
|
28
|
+
type HarnessModelInfo,
|
|
23
29
|
type HarnessModels,
|
|
24
30
|
type KeyBindings,
|
|
25
31
|
type MaybePromise,
|
|
@@ -27,9 +33,11 @@ import {
|
|
|
27
33
|
type ObjectTypeDefinition,
|
|
28
34
|
type ObjectViews,
|
|
29
35
|
type PluginBaseContext,
|
|
36
|
+
type PluginCheckReturn,
|
|
30
37
|
type PluginKind,
|
|
31
38
|
type PluginRequires,
|
|
32
39
|
type ProjectSettingsDeclaration,
|
|
40
|
+
type ProjectSettingsFile,
|
|
33
41
|
type SourceContext,
|
|
34
42
|
type SourceHandle,
|
|
35
43
|
type StandingContext,
|
|
@@ -38,7 +46,7 @@ import {
|
|
|
38
46
|
type StepTypeDefinition,
|
|
39
47
|
type TickContext,
|
|
40
48
|
} from "./types.ts";
|
|
41
|
-
import { isRecord } from "../util.ts";
|
|
49
|
+
import { isRecord, parseObjectId } from "../util.ts";
|
|
42
50
|
|
|
43
51
|
export interface NormalizedPlugin {
|
|
44
52
|
readonly name: string;
|
|
@@ -60,18 +68,33 @@ export interface NormalizedPlugin {
|
|
|
60
68
|
| ((ctx: SourceContext) => MaybePromise<SourceHandle> | SourceHandle)
|
|
61
69
|
| undefined;
|
|
62
70
|
readonly check:
|
|
63
|
-
| ((ctx: PluginBaseContext) => MaybePromise<
|
|
71
|
+
| ((ctx: PluginBaseContext) => MaybePromise<PluginCheckReturn>)
|
|
64
72
|
| undefined;
|
|
73
|
+
readonly guide: string | undefined;
|
|
65
74
|
readonly settingsSchema: ActionArgsSchema | undefined;
|
|
66
75
|
/** The plugin's objects belong under a client's settings view. */
|
|
67
76
|
readonly objectsUnderSettings: boolean;
|
|
77
|
+
readonly listTitle: string | undefined;
|
|
68
78
|
readonly projectSettings: ProjectSettingsDeclaration | undefined;
|
|
69
79
|
readonly enforces: readonly string[];
|
|
70
80
|
readonly models:
|
|
71
81
|
| HarnessModels
|
|
72
|
-
| ((
|
|
82
|
+
| ((
|
|
83
|
+
settings: Readonly<Record<string, JsonValue>>,
|
|
84
|
+
context: HarnessModelContext,
|
|
85
|
+
) => HarnessModels)
|
|
86
|
+
| undefined;
|
|
87
|
+
readonly modelsReady:
|
|
88
|
+
| ((
|
|
89
|
+
settings: Readonly<Record<string, JsonValue>>,
|
|
90
|
+
context: HarnessModelContext,
|
|
91
|
+
) => MaybePromise<HarnessModels>)
|
|
92
|
+
| undefined;
|
|
93
|
+
readonly efforts: HarnessEffortsSource | undefined;
|
|
94
|
+
readonly capabilities: HarnessCapabilities;
|
|
95
|
+
readonly signedIn:
|
|
96
|
+
| ((settings: Readonly<Record<string, JsonValue>>) => MaybePromise<boolean | undefined>)
|
|
73
97
|
| undefined;
|
|
74
|
-
readonly efforts: HarnessEfforts | undefined;
|
|
75
98
|
readonly requires: PluginRequires | undefined;
|
|
76
99
|
}
|
|
77
100
|
|
|
@@ -122,11 +145,7 @@ export function narrowerOf(...values: readonly unknown[]): number | undefined {
|
|
|
122
145
|
}
|
|
123
146
|
|
|
124
147
|
export function parseObjectKeyMaybe(key: string): { readonly type: string; readonly id: string } {
|
|
125
|
-
|
|
126
|
-
if (colon <= 0 || colon === key.length - 1) {
|
|
127
|
-
return { type: "object", id: key };
|
|
128
|
-
}
|
|
129
|
-
return { type: key.slice(0, colon), id: key.slice(colon + 1) };
|
|
148
|
+
return parseObjectId(key) ?? { type: "object", id: key };
|
|
130
149
|
}
|
|
131
150
|
|
|
132
151
|
function isPluginKind(value: unknown): value is PluginKind {
|
|
@@ -170,6 +189,18 @@ function normalizeViews(
|
|
|
170
189
|
if (raw.showScope !== undefined && typeof raw.showScope !== "function") {
|
|
171
190
|
throw new PluginError(`${at}.${type}: showScope: must be a function`);
|
|
172
191
|
}
|
|
192
|
+
if (raw.context !== undefined && typeof raw.context !== "function") {
|
|
193
|
+
throw new PluginError(`${at}.${type}: context: must be a function`);
|
|
194
|
+
}
|
|
195
|
+
if (raw.skills !== undefined && typeof raw.skills !== "function") {
|
|
196
|
+
throw new PluginError(`${at}.${type}: skills: must be a function`);
|
|
197
|
+
}
|
|
198
|
+
if (raw.file !== undefined && typeof raw.file !== "function") {
|
|
199
|
+
throw new PluginError(`${at}.${type}: file: must be a function`);
|
|
200
|
+
}
|
|
201
|
+
if (raw.tail !== undefined && typeof raw.tail !== "function") {
|
|
202
|
+
throw new PluginError(`${at}.${type}: tail: must be a function`);
|
|
203
|
+
}
|
|
173
204
|
views[type] = {
|
|
174
205
|
row: raw.row as ObjectViews["row"],
|
|
175
206
|
sections: raw.sections as ObjectViews["sections"],
|
|
@@ -177,6 +208,18 @@ function normalizeViews(
|
|
|
177
208
|
...(raw.showScope === undefined
|
|
178
209
|
? {}
|
|
179
210
|
: { showScope: raw.showScope as NonNullable<ObjectViews["showScope"]> }),
|
|
211
|
+
...(raw.context === undefined
|
|
212
|
+
? {}
|
|
213
|
+
: { context: raw.context as NonNullable<ObjectViews["context"]> }),
|
|
214
|
+
...(raw.skills === undefined
|
|
215
|
+
? {}
|
|
216
|
+
: { skills: raw.skills as NonNullable<ObjectViews["skills"]> }),
|
|
217
|
+
...(raw.file === undefined
|
|
218
|
+
? {}
|
|
219
|
+
: { file: raw.file as NonNullable<ObjectViews["file"]> }),
|
|
220
|
+
...(raw.tail === undefined
|
|
221
|
+
? {}
|
|
222
|
+
: { tail: raw.tail as NonNullable<ObjectViews["tail"]> }),
|
|
180
223
|
};
|
|
181
224
|
}
|
|
182
225
|
return views;
|
|
@@ -221,7 +264,22 @@ function normalizeProjectSettings(
|
|
|
221
264
|
throw new PluginError(messageOf(error), { cause: error });
|
|
222
265
|
}
|
|
223
266
|
if (schema === undefined) throw new PluginError(`${where}: schema: is required`);
|
|
224
|
-
|
|
267
|
+
for (const hook of ["check", "derived"]) {
|
|
268
|
+
if (item[hook] !== undefined && typeof item[hook] !== "function") {
|
|
269
|
+
throw new PluginError(`${where}: ${hook}: must be a function`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
name,
|
|
274
|
+
path,
|
|
275
|
+
schema,
|
|
276
|
+
...(item.check === undefined
|
|
277
|
+
? {}
|
|
278
|
+
: { check: item.check as ProjectSettingsFile["check"] }),
|
|
279
|
+
...(item.derived === undefined
|
|
280
|
+
? {}
|
|
281
|
+
: { derived: item.derived as ProjectSettingsFile["derived"] }),
|
|
282
|
+
};
|
|
225
283
|
});
|
|
226
284
|
return {
|
|
227
285
|
repositories: value.repositories as ProjectSettingsDeclaration["repositories"],
|
|
@@ -570,11 +628,16 @@ function normalizeActions(
|
|
|
570
628
|
throw new PluginError(`${at}[${index}]: must be an object`);
|
|
571
629
|
}
|
|
572
630
|
const name = requireNameText(entry.name, `${at}[${index}]: name`);
|
|
573
|
-
const on =
|
|
574
|
-
|
|
631
|
+
const on =
|
|
632
|
+
entry.on === undefined
|
|
633
|
+
? undefined
|
|
634
|
+
: requireNameText(entry.on, `${at}[${index}]: on`);
|
|
635
|
+
const key = `${name}\u0000${on ?? ""}`;
|
|
575
636
|
if (names.has(key)) {
|
|
576
637
|
throw new PluginError(
|
|
577
|
-
|
|
638
|
+
on === undefined
|
|
639
|
+
? `${at}: duplicate action "${name}" that targets no object`
|
|
640
|
+
: `${at}: duplicate action "${name}" on object type "${on}"`,
|
|
578
641
|
);
|
|
579
642
|
}
|
|
580
643
|
names.add(key);
|
|
@@ -604,6 +667,13 @@ function normalizeActions(
|
|
|
604
667
|
if (confirm !== undefined && typeof confirm !== "boolean") {
|
|
605
668
|
throw new PluginError(`${at}[${index}]: confirm: must be a boolean`);
|
|
606
669
|
}
|
|
670
|
+
if (
|
|
671
|
+
entry.command !== undefined &&
|
|
672
|
+
typeof entry.command !== "boolean" &&
|
|
673
|
+
(typeof entry.command !== "string" || entry.command.trim().length === 0)
|
|
674
|
+
) {
|
|
675
|
+
throw new PluginError(`${at}[${index}]: command: must be a boolean or a non-empty string`);
|
|
676
|
+
}
|
|
607
677
|
if (
|
|
608
678
|
entry.creates !== undefined &&
|
|
609
679
|
(typeof entry.creates !== "string" || entry.creates.length === 0)
|
|
@@ -612,10 +682,13 @@ function normalizeActions(
|
|
|
612
682
|
}
|
|
613
683
|
actions.push({
|
|
614
684
|
name,
|
|
615
|
-
on,
|
|
685
|
+
...(on === undefined ? {} : { on }),
|
|
616
686
|
...(typeof entry.creates === "string" ? { creates: entry.creates } : {}),
|
|
617
687
|
...(args === undefined ? {} : { args }),
|
|
618
688
|
...(confirm === undefined ? {} : { confirm }),
|
|
689
|
+
...(typeof entry.command === "string" || typeof entry.command === "boolean"
|
|
690
|
+
? { command: entry.command }
|
|
691
|
+
: {}),
|
|
619
692
|
...(entry.ends === true ? { ends: true } : {}),
|
|
620
693
|
when: entry.when as ActionTypeDefinition["when"],
|
|
621
694
|
...(typeof entry.rank === "function"
|
|
@@ -706,15 +779,22 @@ export function normalizePlugin(raw: unknown, index: number): NormalizedPlugin {
|
|
|
706
779
|
}
|
|
707
780
|
|
|
708
781
|
let check:
|
|
709
|
-
| ((ctx: PluginBaseContext) => MaybePromise<
|
|
782
|
+
| ((ctx: PluginBaseContext) => MaybePromise<PluginCheckReturn>)
|
|
710
783
|
| undefined;
|
|
784
|
+
let guide: string | undefined;
|
|
785
|
+
if (raw.guide !== undefined) {
|
|
786
|
+
if (typeof raw.guide !== "string" || !isAbsolute(raw.guide)) {
|
|
787
|
+
throw new PluginError(`${at}: guide: must be the absolute path of a SKILL.md`);
|
|
788
|
+
}
|
|
789
|
+
guide = raw.guide;
|
|
790
|
+
}
|
|
711
791
|
if (raw.check !== undefined) {
|
|
712
792
|
if (typeof raw.check !== "function") {
|
|
713
793
|
throw new PluginError(`${at}: check: must be a function`);
|
|
714
794
|
}
|
|
715
795
|
check = raw.check as (
|
|
716
796
|
ctx: PluginBaseContext,
|
|
717
|
-
) => MaybePromise<
|
|
797
|
+
) => MaybePromise<PluginCheckReturn>;
|
|
718
798
|
}
|
|
719
799
|
|
|
720
800
|
let start: NormalizedPlugin["start"];
|
|
@@ -741,6 +821,10 @@ export function normalizePlugin(raw: unknown, index: number): NormalizedPlugin {
|
|
|
741
821
|
throw new PluginError(`${at}: objectsUnderSettings: must be a boolean`);
|
|
742
822
|
}
|
|
743
823
|
|
|
824
|
+
if (raw.listTitle !== undefined && (typeof raw.listTitle !== "string" || raw.listTitle === "")) {
|
|
825
|
+
throw new PluginError(`${at}: listTitle: must be a non-empty string`);
|
|
826
|
+
}
|
|
827
|
+
|
|
744
828
|
const projectSettings = normalizeProjectSettings(
|
|
745
829
|
raw.projectSettings,
|
|
746
830
|
`${at}: projectSettings`,
|
|
@@ -766,12 +850,17 @@ export function normalizePlugin(raw: unknown, index: number): NormalizedPlugin {
|
|
|
766
850
|
standing,
|
|
767
851
|
start,
|
|
768
852
|
check,
|
|
853
|
+
guide,
|
|
769
854
|
settingsSchema,
|
|
770
855
|
objectsUnderSettings: raw.objectsUnderSettings === true,
|
|
856
|
+
listTitle: raw.listTitle,
|
|
771
857
|
projectSettings,
|
|
772
858
|
enforces: normalizeEnforces(raw.enforces, `${at}: enforces`) ?? [],
|
|
773
859
|
models: normalizeModels(raw.models, kind, at),
|
|
860
|
+
modelsReady: normalizeModelsReady(raw.modelsReady, kind, at),
|
|
774
861
|
efforts: normalizeEfforts(raw.efforts, kind, at),
|
|
862
|
+
capabilities: normalizeCapabilities(raw.capabilities, kind, at),
|
|
863
|
+
signedIn: normalizeSignedIn(raw.signedIn, kind, at),
|
|
775
864
|
requires: normalizeRequires(raw.requires, at),
|
|
776
865
|
};
|
|
777
866
|
}
|
|
@@ -781,24 +870,224 @@ function normalizeModels(
|
|
|
781
870
|
value: unknown,
|
|
782
871
|
kind: string,
|
|
783
872
|
at: string,
|
|
784
|
-
): HarnessModels | ((settings: Readonly<Record<string, JsonValue
|
|
873
|
+
): HarnessModels | ((settings: Readonly<Record<string, JsonValue>>, context: HarnessModelContext) => HarnessModels) | undefined {
|
|
785
874
|
if (typeof value === "function") {
|
|
786
875
|
if (kind !== "harness") throw new PluginError(`${at}: models: only a harness plugin declares models`);
|
|
787
|
-
const source = value as (
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
876
|
+
const source = value as (
|
|
877
|
+
settings: Readonly<Record<string, JsonValue>>,
|
|
878
|
+
context: HarnessModelContext,
|
|
879
|
+
) => unknown;
|
|
880
|
+
return (settings, context) => {
|
|
881
|
+
const raw = source(settings, context);
|
|
882
|
+
const levels = normalizeModelLevels(raw, kind, at);
|
|
883
|
+
return levels === undefined ? { list: [], default: null } : levels;
|
|
791
884
|
};
|
|
792
885
|
}
|
|
793
|
-
|
|
794
|
-
|
|
886
|
+
return normalizeModelLevels(value, kind, at);
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
/** A harness plugin's `modelsReady` hook: a list, possibly after waiting for one refresh. */
|
|
890
|
+
function normalizeModelsReady(
|
|
891
|
+
value: unknown,
|
|
892
|
+
kind: string,
|
|
893
|
+
at: string,
|
|
894
|
+
): ((settings: Readonly<Record<string, JsonValue>>, context: HarnessModelContext) => MaybePromise<HarnessModels>) | undefined {
|
|
895
|
+
if (value === undefined) return undefined;
|
|
896
|
+
if (kind !== "harness") throw new PluginError(`${at}: modelsReady: only a harness plugin declares modelsReady`);
|
|
897
|
+
if (typeof value !== "function") throw new PluginError(`${at}: modelsReady: must be a function`);
|
|
898
|
+
const source = value as (
|
|
899
|
+
settings: Readonly<Record<string, JsonValue>>,
|
|
900
|
+
context: HarnessModelContext,
|
|
901
|
+
) => unknown;
|
|
902
|
+
return async (settings, context) => {
|
|
903
|
+
const raw = await source(settings, context);
|
|
904
|
+
const levels = normalizeModelLevels(raw, kind, at);
|
|
905
|
+
return levels === undefined ? { list: [], default: null } : levels;
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
/** The capabilities a model declares, as the names a client shows. */
|
|
910
|
+
const MODEL_CAPABILITIES = ["vision", "reasoning", "tools", "web"] as const;
|
|
911
|
+
|
|
912
|
+
function normalizeModelCapabilities(value: unknown, at: string): readonly HarnessModelCapability[] | undefined {
|
|
913
|
+
if (value === undefined) return undefined;
|
|
914
|
+
const found: HarnessModelCapability[] = [];
|
|
915
|
+
if (Array.isArray(value)) {
|
|
916
|
+
for (const item of value) {
|
|
917
|
+
if (typeof item !== "string" || !(MODEL_CAPABILITIES as readonly string[]).includes(item)) {
|
|
918
|
+
throw new PluginError(`${at}: must be one of ${MODEL_CAPABILITIES.join(", ")}`);
|
|
919
|
+
}
|
|
920
|
+
found.push(item as HarnessModelCapability);
|
|
921
|
+
}
|
|
922
|
+
} else if (isRecord(value)) {
|
|
923
|
+
for (const capability of MODEL_CAPABILITIES) {
|
|
924
|
+
const on = value[capability];
|
|
925
|
+
if (on !== undefined && typeof on !== "boolean") {
|
|
926
|
+
throw new PluginError(`${at}.${capability}: must be a boolean`);
|
|
927
|
+
}
|
|
928
|
+
if (on === true) found.push(capability);
|
|
929
|
+
}
|
|
930
|
+
for (const key of Object.keys(value)) {
|
|
931
|
+
if (!(MODEL_CAPABILITIES as readonly string[]).includes(key)) {
|
|
932
|
+
throw new PluginError(`${at}.${key}: not a model capability`);
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
} else {
|
|
936
|
+
throw new PluginError(`${at}: must be an array of capabilities or an object of booleans`);
|
|
937
|
+
}
|
|
938
|
+
return found.length === 0 ? undefined : found;
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
/** One model's description, as the harness declares it. */
|
|
942
|
+
function normalizeModelInfo(value: unknown, at: string, id: string): HarnessModelInfo {
|
|
943
|
+
if (!isRecord(value)) throw new PluginError(`${at}: must be an object`);
|
|
944
|
+
const declared = value.id;
|
|
945
|
+
if (declared !== undefined && (typeof declared !== "string" || declared.length === 0)) {
|
|
946
|
+
throw new PluginError(`${at}.id: must be a non-empty string`);
|
|
947
|
+
}
|
|
948
|
+
if (declared !== undefined && declared !== id) {
|
|
949
|
+
throw new PluginError(`${at}.id: must be "${id}"`);
|
|
950
|
+
}
|
|
951
|
+
const known = new Set(["id", "name", "provider", "window", "capabilities", ...MODEL_CAPABILITIES]);
|
|
952
|
+
for (const key of Object.keys(value)) {
|
|
953
|
+
if (!known.has(key)) throw new PluginError(`${at}.${key}: not a model field`);
|
|
954
|
+
}
|
|
955
|
+
const name = value.name;
|
|
956
|
+
if (name !== undefined && (typeof name !== "string" || name.length === 0)) {
|
|
957
|
+
throw new PluginError(`${at}.name: must be a non-empty string`);
|
|
958
|
+
}
|
|
959
|
+
const provider = value.provider;
|
|
960
|
+
if (provider !== undefined && (typeof provider !== "string" || provider.length === 0)) {
|
|
961
|
+
throw new PluginError(`${at}.provider: must be a non-empty string`);
|
|
962
|
+
}
|
|
963
|
+
const window = value.window;
|
|
964
|
+
if (window !== undefined && (typeof window !== "number" || !Number.isInteger(window) || window <= 0)) {
|
|
965
|
+
throw new PluginError(`${at}.window: must be a positive whole number`);
|
|
966
|
+
}
|
|
967
|
+
const found = new Set<HarnessModelCapability>();
|
|
968
|
+
for (const capability of MODEL_CAPABILITIES) {
|
|
969
|
+
const on = value[capability];
|
|
970
|
+
if (on === true) found.add(capability);
|
|
971
|
+
else if (on !== undefined && typeof on !== "boolean") {
|
|
972
|
+
throw new PluginError(`${at}.${capability}: must be a boolean`);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
for (const capability of normalizeModelCapabilities(value.capabilities, `${at}.capabilities`) ?? []) {
|
|
976
|
+
found.add(capability);
|
|
977
|
+
}
|
|
978
|
+
const capabilities = MODEL_CAPABILITIES.filter((capability) => found.has(capability));
|
|
979
|
+
return {
|
|
980
|
+
id,
|
|
981
|
+
...(typeof name === "string" ? { name } : {}),
|
|
982
|
+
...(typeof provider === "string" ? { provider } : {}),
|
|
983
|
+
...(typeof window === "number" ? { window } : {}),
|
|
984
|
+
...(capabilities.length === 0 ? {} : { capabilities }),
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
/** The descriptions a harness gives beside its list, by model id. */
|
|
989
|
+
function normalizeModelInfoMap(
|
|
990
|
+
value: unknown,
|
|
991
|
+
list: readonly string[],
|
|
992
|
+
at: string,
|
|
993
|
+
): Readonly<Record<string, HarnessModelInfo>> | undefined {
|
|
994
|
+
if (value === undefined) return undefined;
|
|
995
|
+
const info: Record<string, HarnessModelInfo> = {};
|
|
996
|
+
const add = (raw: unknown, id: string, where: string): void => {
|
|
997
|
+
if (!list.includes(id)) {
|
|
998
|
+
throw new PluginError(`${where}: must be one of the models in the list`);
|
|
999
|
+
}
|
|
1000
|
+
info[id] = normalizeModelInfo(raw, where, id);
|
|
1001
|
+
};
|
|
1002
|
+
if (Array.isArray(value)) {
|
|
1003
|
+
value.forEach((item, index) => {
|
|
1004
|
+
if (!isRecord(item) || typeof item.id !== "string" || item.id.length === 0) {
|
|
1005
|
+
throw new PluginError(`${at}: models.info[${index}].id: must be a non-empty string`);
|
|
1006
|
+
}
|
|
1007
|
+
add(item, item.id, `${at}: models.info[${index}]`);
|
|
1008
|
+
});
|
|
1009
|
+
} else if (isRecord(value)) {
|
|
1010
|
+
for (const [id, item] of Object.entries(value)) {
|
|
1011
|
+
add(item, id, `${at}: models.info.${id}`);
|
|
1012
|
+
}
|
|
1013
|
+
} else {
|
|
1014
|
+
throw new PluginError(`${at}: models.info: must be an object or an array`);
|
|
1015
|
+
}
|
|
1016
|
+
return Object.keys(info).length === 0 ? undefined : info;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/** The list, fallback and descriptions of a harness plugin's `models`. */
|
|
1020
|
+
function normalizeModelLevels(
|
|
1021
|
+
value: unknown,
|
|
1022
|
+
kind: string,
|
|
1023
|
+
at: string,
|
|
1024
|
+
): HarnessModels | undefined {
|
|
1025
|
+
if (value === undefined) return undefined;
|
|
1026
|
+
if (kind !== "harness") throw new PluginError(`${at}: models: only a harness plugin declares models`);
|
|
1027
|
+
if (!isRecord(value) || !Array.isArray(value.list)) throw new PluginError(`${at}: models: must be { list, default }`);
|
|
1028
|
+
const list: string[] = [];
|
|
1029
|
+
const described: HarnessModelInfo[] = [];
|
|
1030
|
+
value.list.forEach((item, index) => {
|
|
1031
|
+
if (typeof item === "string" && item.length > 0) {
|
|
1032
|
+
list.push(item);
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
if (isRecord(item)) {
|
|
1036
|
+
const id = item.id;
|
|
1037
|
+
if (typeof id !== "string" || id.length === 0) {
|
|
1038
|
+
throw new PluginError(`${at}: models.list[${index}].id: must be a non-empty string`);
|
|
1039
|
+
}
|
|
1040
|
+
list.push(id);
|
|
1041
|
+
described.push(normalizeModelInfo(item, `${at}: models.list[${index}]`, id));
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
throw new PluginError(`${at}: models.list[${index}]: must be a non-empty string or a model`);
|
|
1045
|
+
});
|
|
1046
|
+
const fallback = value.default === undefined ? null : value.default;
|
|
1047
|
+
if (fallback !== null && (typeof fallback !== "string" || !list.includes(fallback))) {
|
|
1048
|
+
throw new PluginError(`${at}: models.default: must be null or one of the list`);
|
|
1049
|
+
}
|
|
1050
|
+
const fromInfo = normalizeModelInfoMap(value.info, list, at);
|
|
1051
|
+
const info: Record<string, HarnessModelInfo> = { ...(fromInfo ?? {}) };
|
|
1052
|
+
for (const model of described) info[model.id] = model;
|
|
1053
|
+
const declaredWindows = normalizeWindows(value, list, at);
|
|
1054
|
+
const windows: Record<string, number> = { ...(declaredWindows ?? {}) };
|
|
1055
|
+
for (const model of Object.values(info)) {
|
|
1056
|
+
if (model.window !== undefined) windows[model.id] = model.window;
|
|
1057
|
+
}
|
|
1058
|
+
return {
|
|
1059
|
+
list,
|
|
1060
|
+
default: fallback,
|
|
1061
|
+
...(Object.keys(windows).length === 0 ? {} : { windows }),
|
|
1062
|
+
...(Object.keys(info).length === 0 ? {} : { info }),
|
|
1063
|
+
};
|
|
795
1064
|
}
|
|
796
1065
|
|
|
797
1066
|
/**
|
|
798
1067
|
* The efforts a harness plugin declares: a list of levels, the default among
|
|
799
|
-
* them or null,
|
|
1068
|
+
* them or null, the step argument that sets one, or a function of its settings.
|
|
800
1069
|
*/
|
|
801
|
-
function normalizeEfforts(
|
|
1070
|
+
function normalizeEfforts(
|
|
1071
|
+
value: unknown,
|
|
1072
|
+
kind: string,
|
|
1073
|
+
at: string,
|
|
1074
|
+
): HarnessEffortsSource | undefined {
|
|
1075
|
+
if (typeof value === "function") {
|
|
1076
|
+
if (kind !== "harness") {
|
|
1077
|
+
throw new PluginError(`${at}: efforts: only a harness plugin declares efforts`);
|
|
1078
|
+
}
|
|
1079
|
+
const source = value as (settings: Readonly<Record<string, JsonValue>>) => unknown;
|
|
1080
|
+
return (settings) =>
|
|
1081
|
+
normalizeEffortsValue(source(settings), "harness", at) ?? { list: [], default: null };
|
|
1082
|
+
}
|
|
1083
|
+
return normalizeEffortsValue(value, kind, at);
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
function normalizeEffortsValue(
|
|
1087
|
+
value: unknown,
|
|
1088
|
+
kind: string,
|
|
1089
|
+
at: string,
|
|
1090
|
+
): HarnessEfforts | undefined {
|
|
802
1091
|
const levels = normalizeLevels(value, kind, at, "efforts");
|
|
803
1092
|
if (levels === undefined) return undefined;
|
|
804
1093
|
const flag = value === undefined ? undefined : (value as Record<string, unknown>).flag;
|
|
@@ -812,6 +1101,70 @@ function normalizeEfforts(value: unknown, kind: string, at: string): HarnessEffo
|
|
|
812
1101
|
};
|
|
813
1102
|
}
|
|
814
1103
|
|
|
1104
|
+
/** A harness plugin's declared capabilities: what its agent does itself. */
|
|
1105
|
+
function normalizeCapabilities(value: unknown, kind: string, at: string): HarnessCapabilities {
|
|
1106
|
+
if (value === undefined) return {};
|
|
1107
|
+
if (kind !== "harness") {
|
|
1108
|
+
throw new PluginError(`${at}: capabilities: only a harness plugin declares capabilities`);
|
|
1109
|
+
}
|
|
1110
|
+
if (!isRecord(value)) throw new PluginError(`${at}: capabilities: must be an object`);
|
|
1111
|
+
for (const key of Object.keys(value)) {
|
|
1112
|
+
if (
|
|
1113
|
+
key !== "backgroundModel" &&
|
|
1114
|
+
key !== "loadsSkills" &&
|
|
1115
|
+
key !== "takesMessagesMidRun" &&
|
|
1116
|
+
key !== "accessLevels"
|
|
1117
|
+
) {
|
|
1118
|
+
throw new PluginError(`${at}: capabilities.${key}: not a capability`);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
const result: {
|
|
1122
|
+
backgroundModel?: string;
|
|
1123
|
+
loadsSkills?: boolean;
|
|
1124
|
+
takesMessagesMidRun?: boolean;
|
|
1125
|
+
accessLevels?: readonly string[];
|
|
1126
|
+
} = {};
|
|
1127
|
+
const backgroundModel = value["backgroundModel"];
|
|
1128
|
+
if (backgroundModel !== undefined) {
|
|
1129
|
+
if (typeof backgroundModel !== "string" || backgroundModel.length === 0) {
|
|
1130
|
+
throw new PluginError(`${at}: capabilities.backgroundModel: must be a non-empty string`);
|
|
1131
|
+
}
|
|
1132
|
+
result.backgroundModel = backgroundModel;
|
|
1133
|
+
}
|
|
1134
|
+
for (const key of ["loadsSkills", "takesMessagesMidRun"] as const) {
|
|
1135
|
+
const capability = value[key];
|
|
1136
|
+
if (capability !== undefined && typeof capability !== "boolean") {
|
|
1137
|
+
throw new PluginError(`${at}: capabilities.${key}: must be a boolean`);
|
|
1138
|
+
}
|
|
1139
|
+
if (capability === true) result[key] = true;
|
|
1140
|
+
}
|
|
1141
|
+
const levels = value["accessLevels"];
|
|
1142
|
+
if (levels !== undefined) {
|
|
1143
|
+
if (!Array.isArray(levels) || !levels.every((level) => typeof level === "string" && level.length > 0)) {
|
|
1144
|
+
throw new PluginError(`${at}: capabilities.accessLevels: must be an array of level ids`);
|
|
1145
|
+
}
|
|
1146
|
+
if (levels.length > 0) result.accessLevels = [...levels];
|
|
1147
|
+
}
|
|
1148
|
+
return result;
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
/** A harness plugin's `signedIn` hook: true, false, or undefined when it cannot tell. */
|
|
1152
|
+
function normalizeSignedIn(
|
|
1153
|
+
value: unknown,
|
|
1154
|
+
kind: string,
|
|
1155
|
+
at: string,
|
|
1156
|
+
): ((settings: Readonly<Record<string, JsonValue>>) => MaybePromise<boolean | undefined>) | undefined {
|
|
1157
|
+
if (value === undefined) return undefined;
|
|
1158
|
+
if (kind !== "harness") throw new PluginError(`${at}: signedIn: only a harness plugin declares signedIn`);
|
|
1159
|
+
if (typeof value !== "function") throw new PluginError(`${at}: signedIn: must be a function`);
|
|
1160
|
+
const source = value as (settings: Readonly<Record<string, JsonValue>>) => unknown;
|
|
1161
|
+
return async (settings) => {
|
|
1162
|
+
const answer = await source(settings);
|
|
1163
|
+
if (answer === undefined || answer === true || answer === false) return answer;
|
|
1164
|
+
throw new PluginError(`${at}: signedIn: must return true, false or undefined`);
|
|
1165
|
+
};
|
|
1166
|
+
}
|
|
1167
|
+
|
|
815
1168
|
/** The list and fallback of a harness plugin's `models` or `efforts`. */
|
|
816
1169
|
function normalizeLevels(
|
|
817
1170
|
value: unknown,
|
|
@@ -833,6 +1186,29 @@ function normalizeLevels(
|
|
|
833
1186
|
return { list, fallback };
|
|
834
1187
|
}
|
|
835
1188
|
|
|
1189
|
+
/** The context windows a harness's model list declares, when it declares any. */
|
|
1190
|
+
function normalizeWindows(
|
|
1191
|
+
value: unknown,
|
|
1192
|
+
list: readonly string[],
|
|
1193
|
+
at: string,
|
|
1194
|
+
): Readonly<Record<string, number>> | undefined {
|
|
1195
|
+
if (!isRecord(value)) return undefined;
|
|
1196
|
+
const raw = value["windows"];
|
|
1197
|
+
if (raw === undefined) return undefined;
|
|
1198
|
+
if (!isRecord(raw)) throw new PluginError(`${at}: models.windows: must be an object`);
|
|
1199
|
+
const windows: Record<string, number> = {};
|
|
1200
|
+
for (const [model, window] of Object.entries(raw)) {
|
|
1201
|
+
if (!list.includes(model)) {
|
|
1202
|
+
throw new PluginError(`${at}: models.windows.${model}: must be one of the models in the list`);
|
|
1203
|
+
}
|
|
1204
|
+
if (typeof window !== "number" || !Number.isInteger(window) || window <= 0) {
|
|
1205
|
+
throw new PluginError(`${at}: models.windows.${model}: must be a positive whole number`);
|
|
1206
|
+
}
|
|
1207
|
+
windows[model] = window;
|
|
1208
|
+
}
|
|
1209
|
+
return Object.keys(windows).length === 0 ? undefined : windows;
|
|
1210
|
+
}
|
|
1211
|
+
|
|
836
1212
|
/** A harness step type ends only in the standard end results. */
|
|
837
1213
|
function checkHarnessResults(
|
|
838
1214
|
steps: readonly StepTypeDefinition[],
|
|
@@ -971,7 +1347,7 @@ export function validatePluginSet(plugins: readonly NormalizedPlugin[]): void {
|
|
|
971
1347
|
|
|
972
1348
|
for (const plugin of plugins) {
|
|
973
1349
|
for (const action of plugin.actions) {
|
|
974
|
-
if (!objects.has(action.on)) {
|
|
1350
|
+
if (action.on !== undefined && !objects.has(action.on)) {
|
|
975
1351
|
throw new PluginError(
|
|
976
1352
|
`plugin "${plugin.name}": action "${action.name}": on: unknown object type "${action.on}"`,
|
|
977
1353
|
);
|