@olivervea/pipelines 2.0.0-beta.2 → 2.0.0-beta.20
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/README.md +125 -62
- package/manifest.schema.json +208 -11
- package/package.json +6 -4
- package/src/assets.d.ts +2 -2
- package/src/build/build.ts +121 -51
- package/src/build/cli.ts +29 -22
- package/src/build/elf.ts +8 -7
- package/src/build/hashes.ts +82 -20
- package/src/build/project.ts +95 -28
- package/src/builder/pipeline.ts +344 -88
- package/src/builder/refs.ts +15 -5
- package/src/core/brands.ts +7 -5
- package/src/core/config-refs.ts +103 -0
- package/src/core/config.ts +224 -0
- package/src/core/external.ts +86 -0
- package/src/core/handlers.ts +61 -15
- package/src/core/models.ts +32 -15
- package/src/core/origin.ts +23 -0
- package/src/core/secrets.ts +2 -2
- package/src/core/steps.ts +89 -38
- package/src/core/types.ts +5 -1
- package/src/core/webhooks.ts +21 -7
- package/src/ctx/files.ts +111 -40
- package/src/ctx/index.ts +84 -31
- package/src/ctx/sh.ts +105 -44
- package/src/ctx/types.ts +12 -4
- package/src/describe/describe.ts +701 -133
- package/src/describe/manifest-schema.ts +258 -61
- package/src/describe/mapper-source.ts +249 -104
- package/src/describe/record.ts +67 -37
- package/src/dev/cli.ts +72 -35
- package/src/harness/env.ts +45 -25
- package/src/harness/execute.ts +58 -29
- package/src/harness/index.ts +22 -9
- package/src/harness/main.ts +239 -78
- package/src/harness/result.ts +38 -22
- package/src/harness/validate.ts +77 -15
- package/src/index.ts +124 -11
- package/src/library/checks.ts +28 -0
- package/src/library/compose-deploy.ts +113 -0
- package/src/library/github-source.ts +100 -47
- package/src/library/helm-deploy-ssh.ts +452 -108
- package/src/library/helm-deploy.ts +449 -0
- package/src/library/http-health.ts +7 -5
- package/src/library/image-build.ts +499 -105
- package/src/library/index.ts +48 -12
- package/src/library/models.ts +33 -9
- package/src/library/ssh.ts +67 -0
- package/src/testing/index.ts +74 -36
- package/src/util/canonical.ts +7 -4
- package/src/util/handler-hash.ts +17 -7
- package/src/util/hash.ts +5 -5
- package/src/util/tar.ts +86 -36
- package/src/library/script.ts +0 -85
package/src/core/brands.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
// Runtime markers. `Symbol.for` (not `Symbol()`) so that two copies of the SDK in one process — e.g.
|
|
2
2
|
// a dev runner and the user's installed package — still recognise each other's objects.
|
|
3
|
-
export const STEP = Symbol.for(
|
|
4
|
-
export const IMPLEMENTATION = Symbol.for(
|
|
5
|
-
export const
|
|
6
|
-
export const
|
|
7
|
-
export const
|
|
3
|
+
export const STEP = Symbol.for('olve.pipelines.step');
|
|
4
|
+
export const IMPLEMENTATION = Symbol.for('olve.pipelines.implementation');
|
|
5
|
+
export const ORIGIN = Symbol.for('olve.pipelines.origin');
|
|
6
|
+
export const SKIPPED = Symbol.for('olve.pipelines.skipped');
|
|
7
|
+
export const PIPELINE = Symbol.for('olve.pipelines.pipeline');
|
|
8
|
+
export const REF_PATH = Symbol.for('olve.pipelines.refPath');
|
|
8
9
|
|
|
9
10
|
// Type-only brands (never exist at runtime).
|
|
10
11
|
export declare const ModelBrand: unique symbol;
|
|
11
12
|
export declare const RefBrand: unique symbol;
|
|
12
13
|
export declare const StepBrand: unique symbol;
|
|
13
14
|
export declare const SkippedBrand: unique symbol;
|
|
15
|
+
export declare const ImplementationBrand: unique symbol;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Config references inside library-step definitions: collected (the step receives their entries),
|
|
2
|
+
// recorded in the manifest as `{"$config": "<entry>.<path>"}`, and resolved from ctx.config at run time.
|
|
3
|
+
import {
|
|
4
|
+
configRefPath,
|
|
5
|
+
configRefTarget,
|
|
6
|
+
isConfigRef,
|
|
7
|
+
type AnyConfigEntry,
|
|
8
|
+
type ConfigRef,
|
|
9
|
+
} from './config.ts';
|
|
10
|
+
|
|
11
|
+
/** `T` with every config reference replaced by the type of its value. */
|
|
12
|
+
export type Resolved<T> =
|
|
13
|
+
T extends ConfigRef<infer U>
|
|
14
|
+
? U
|
|
15
|
+
: T extends readonly (infer E)[]
|
|
16
|
+
? Resolved<E>[]
|
|
17
|
+
: T extends (...args: never[]) => unknown
|
|
18
|
+
? T
|
|
19
|
+
: T extends object
|
|
20
|
+
? { [K in keyof T]: Resolved<T[K]> }
|
|
21
|
+
: T;
|
|
22
|
+
|
|
23
|
+
/** Manifest key of a recorded config reference. */
|
|
24
|
+
export const CONFIG_REF_KEY = '$config';
|
|
25
|
+
/** Beside it on a reference narrowed by a `select` case: the case's condition, `<ref>.<discriminator>=<value>`. */
|
|
26
|
+
export const CONFIG_CASE_KEY = '$case';
|
|
27
|
+
|
|
28
|
+
function isPlain(v: unknown): v is Record<string, unknown> {
|
|
29
|
+
if (typeof v !== 'object' || v === null || Array.isArray(v)) return false;
|
|
30
|
+
const proto = Object.getPrototypeOf(v);
|
|
31
|
+
return proto === Object.prototype || proto === null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Every config entry referenced anywhere in `v` (plain objects and arrays only), by name. */
|
|
35
|
+
export function configRefsIn(
|
|
36
|
+
v: unknown,
|
|
37
|
+
into = new Map<string, AnyConfigEntry>(),
|
|
38
|
+
): AnyConfigEntry[] {
|
|
39
|
+
if (isConfigRef(v)) {
|
|
40
|
+
const { entry } = configRefTarget(v);
|
|
41
|
+
if (!into.has(entry.name)) into.set(entry.name, entry);
|
|
42
|
+
} else if (Array.isArray(v)) for (const x of v) configRefsIn(x, into);
|
|
43
|
+
else if (isPlain(v)) for (const x of Object.values(v)) configRefsIn(x, into);
|
|
44
|
+
return [...into.values()];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** `v` as manifest JSON: references become `{"$config": "target.beta.host"}`; literal keys starting with `$` are reserved. */
|
|
48
|
+
export function recordConfigRefs(v: unknown): unknown {
|
|
49
|
+
if (isConfigRef(v)) {
|
|
50
|
+
const { path, case: kase } = configRefTarget(v);
|
|
51
|
+
for (const seg of path)
|
|
52
|
+
if (seg === '' || seg.includes('.'))
|
|
53
|
+
throw new Error(
|
|
54
|
+
`config reference '${configRefPath(v)}': field names must not be empty or contain '.'`,
|
|
55
|
+
);
|
|
56
|
+
return {
|
|
57
|
+
[CONFIG_REF_KEY]: configRefPath(v),
|
|
58
|
+
...(kase ? { [CONFIG_CASE_KEY]: kase } : {}),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(v)) return v.map(recordConfigRefs);
|
|
62
|
+
if (isPlain(v)) {
|
|
63
|
+
const out: Record<string, unknown> = {};
|
|
64
|
+
for (const [k, x] of Object.entries(v)) {
|
|
65
|
+
if (k.startsWith('$'))
|
|
66
|
+
throw new Error(`key '${k}': keys starting with '$' are reserved`);
|
|
67
|
+
out[k] = recordConfigRefs(x);
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
return v;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** `v` with every reference replaced by its value in `config` (the step's parsed config entries). */
|
|
75
|
+
export function resolveConfigRefs<T>(
|
|
76
|
+
v: T,
|
|
77
|
+
config: Readonly<Record<string, unknown>>,
|
|
78
|
+
): Resolved<T> {
|
|
79
|
+
if (isConfigRef(v)) {
|
|
80
|
+
const { entry, path } = configRefTarget(v);
|
|
81
|
+
if (!Object.hasOwn(config, entry.name))
|
|
82
|
+
throw new Error(`config entry '${entry.name}' was not delivered to the step`);
|
|
83
|
+
let cur: unknown = config[entry.name];
|
|
84
|
+
for (const seg of path)
|
|
85
|
+
cur =
|
|
86
|
+
cur !== null && typeof cur === 'object'
|
|
87
|
+
? (cur as Record<string, unknown>)[seg]
|
|
88
|
+
: undefined;
|
|
89
|
+
return cur as Resolved<T>;
|
|
90
|
+
}
|
|
91
|
+
if (Array.isArray(v)) return v.map((x) => resolveConfigRefs(x, config)) as Resolved<T>;
|
|
92
|
+
if (isPlain(v)) {
|
|
93
|
+
const out: Record<string, unknown> = {};
|
|
94
|
+
for (const [k, x] of Object.entries(v)) out[k] = resolveConfigRefs(x, config);
|
|
95
|
+
return out as Resolved<T>;
|
|
96
|
+
}
|
|
97
|
+
return v as Resolved<T>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Literal `v` (no reference inside), so define-time checks can run on it. */
|
|
101
|
+
export function isLiteral(v: unknown): boolean {
|
|
102
|
+
return configRefsIn(v).length === 0;
|
|
103
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { Secret } from './secrets.ts';
|
|
3
|
+
|
|
4
|
+
const CONFIG_REF = Symbol.for('olve.configRef');
|
|
5
|
+
declare const VALUE: unique symbol;
|
|
6
|
+
|
|
7
|
+
interface RefTarget {
|
|
8
|
+
readonly entry: AnyConfigEntry;
|
|
9
|
+
readonly path: readonly string[];
|
|
10
|
+
/** Set on a reference narrowed by a `select` case: `<ref>.<discriminator>=<value>`. */
|
|
11
|
+
readonly case?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A typed reference to a config value (`TARGET.beta.host`), accepted in a step's settings in place of a
|
|
16
|
+
* literal. It is recorded in the manifest as `{"$config": "target.beta.host"}` and resolved from the
|
|
17
|
+
* run's config when the step runs; the step receives the entry automatically.
|
|
18
|
+
*/
|
|
19
|
+
export interface ConfigRef<T = unknown> {
|
|
20
|
+
readonly [CONFIG_REF]: RefTarget;
|
|
21
|
+
readonly [VALUE]?: T;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A literal or a reference to a config value of the same type. */
|
|
25
|
+
export type Configurable<T> = T | ConfigRef<T>;
|
|
26
|
+
|
|
27
|
+
type Fields<T> = T extends readonly unknown[] ? never : T extends object ? T : never;
|
|
28
|
+
type Children<T> = [Fields<NonNullable<T>>] extends [never]
|
|
29
|
+
? {}
|
|
30
|
+
: {
|
|
31
|
+
readonly [K in keyof Fields<NonNullable<T>> & string]-?: ConfigNode<
|
|
32
|
+
Fields<NonNullable<T>>[K] | (undefined extends T ? undefined : never)
|
|
33
|
+
>;
|
|
34
|
+
};
|
|
35
|
+
/** A reference to a config value, and to each of its fields. */
|
|
36
|
+
export type ConfigNode<T> = ConfigRef<T> & Children<T>;
|
|
37
|
+
|
|
38
|
+
/** Fields of an entry that are its own properties; reach config fields with these names through `$`. */
|
|
39
|
+
export type ReservedConfigField = 'kind' | 'name' | 'description' | 'schema' | '$';
|
|
40
|
+
|
|
41
|
+
/** A named, typed value stored on the instance (one YAML document per pipeline), declared by the steps that use it. */
|
|
42
|
+
export interface ConfigEntryBase<
|
|
43
|
+
N extends string = string,
|
|
44
|
+
S extends z.ZodType = z.ZodType,
|
|
45
|
+
> {
|
|
46
|
+
readonly kind: 'config';
|
|
47
|
+
readonly name: N;
|
|
48
|
+
readonly description: string;
|
|
49
|
+
readonly schema: S;
|
|
50
|
+
}
|
|
51
|
+
export type ConfigEntry<
|
|
52
|
+
N extends string = string,
|
|
53
|
+
S extends z.ZodType = z.ZodType,
|
|
54
|
+
> = ConfigEntryBase<N, S> &
|
|
55
|
+
ConfigRef<z.output<S>> &
|
|
56
|
+
Omit<Children<z.output<S>>, ReservedConfigField> & {
|
|
57
|
+
readonly $: ConfigNode<z.output<S>>;
|
|
58
|
+
};
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
+
export type AnyConfigEntry = ConfigEntryBase<string, any>;
|
|
61
|
+
|
|
62
|
+
/** Config names are YAML keys: camelCase identifiers. */
|
|
63
|
+
export const CONFIG_NAME = /^[a-z][a-zA-Z0-9]*$/;
|
|
64
|
+
|
|
65
|
+
const NOT_REFS = new Set<PropertyKey>([
|
|
66
|
+
'then',
|
|
67
|
+
'toJSON',
|
|
68
|
+
'constructor',
|
|
69
|
+
'valueOf',
|
|
70
|
+
'toString',
|
|
71
|
+
'asymmetricMatch',
|
|
72
|
+
'$$typeof',
|
|
73
|
+
'nodeType',
|
|
74
|
+
]);
|
|
75
|
+
|
|
76
|
+
function node(entry: AnyConfigEntry, path: readonly string[], kase?: string): unknown {
|
|
77
|
+
const target = Object.freeze({
|
|
78
|
+
[CONFIG_REF]: Object.freeze({ entry, path, ...(kase ? { case: kase } : {}) }),
|
|
79
|
+
});
|
|
80
|
+
return new Proxy(target, {
|
|
81
|
+
get(t, prop) {
|
|
82
|
+
if (prop === CONFIG_REF) return t[CONFIG_REF];
|
|
83
|
+
if (typeof prop === 'symbol' || NOT_REFS.has(prop)) return undefined;
|
|
84
|
+
return node(entry, [...path, prop], kase);
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** `ref` narrowed to the union member whose `discriminator` is `value` (a `select` case). */
|
|
90
|
+
export function narrowConfigRef(
|
|
91
|
+
ref: ConfigRef,
|
|
92
|
+
discriminator: string,
|
|
93
|
+
value: string,
|
|
94
|
+
): unknown {
|
|
95
|
+
const { entry, path } = ref[CONFIG_REF];
|
|
96
|
+
return node(
|
|
97
|
+
entry,
|
|
98
|
+
path,
|
|
99
|
+
`${[entry.name, ...path].join('.')}.${discriminator}=${value}`,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function config<const N extends string, S extends z.ZodType>(
|
|
104
|
+
name: N,
|
|
105
|
+
description: string,
|
|
106
|
+
schema: S,
|
|
107
|
+
): ConfigEntry<N, S> {
|
|
108
|
+
const ref = {
|
|
109
|
+
entry: undefined as unknown as AnyConfigEntry,
|
|
110
|
+
path: [] as readonly string[],
|
|
111
|
+
};
|
|
112
|
+
const target = Object.freeze({
|
|
113
|
+
kind: 'config' as const,
|
|
114
|
+
name,
|
|
115
|
+
description,
|
|
116
|
+
schema,
|
|
117
|
+
[CONFIG_REF]: ref,
|
|
118
|
+
});
|
|
119
|
+
const entry: unknown = new Proxy(target, {
|
|
120
|
+
get(t, prop) {
|
|
121
|
+
if (prop in t) return t[prop as keyof typeof t];
|
|
122
|
+
if (typeof prop === 'symbol' || NOT_REFS.has(prop)) return undefined;
|
|
123
|
+
if (prop === '$') return node(entry as AnyConfigEntry, []);
|
|
124
|
+
return node(entry as AnyConfigEntry, [prop]);
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
ref.entry = entry as AnyConfigEntry;
|
|
128
|
+
Object.freeze(ref);
|
|
129
|
+
return entry as ConfigEntry<N, S>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function isConfigEntry(v: unknown): v is AnyConfigEntry {
|
|
133
|
+
return (
|
|
134
|
+
typeof v === 'object' &&
|
|
135
|
+
v !== null &&
|
|
136
|
+
(v as { kind?: unknown }).kind === 'config' &&
|
|
137
|
+
(v as { schema?: unknown }).schema instanceof z.ZodType
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function isConfigRef(v: unknown): v is ConfigRef {
|
|
142
|
+
return (
|
|
143
|
+
typeof v === 'object' &&
|
|
144
|
+
v !== null &&
|
|
145
|
+
(v as Record<symbol, unknown>)[CONFIG_REF] !== undefined
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The entry and field path a reference points at, and the `select` case it was narrowed by. */
|
|
150
|
+
export function configRefTarget(ref: ConfigRef): {
|
|
151
|
+
entry: AnyConfigEntry;
|
|
152
|
+
path: readonly string[];
|
|
153
|
+
case?: string;
|
|
154
|
+
} {
|
|
155
|
+
return ref[CONFIG_REF];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** `target.beta.host` */
|
|
159
|
+
export function configRefPath(ref: ConfigRef): string {
|
|
160
|
+
const { entry, path } = configRefTarget(ref);
|
|
161
|
+
return [entry.name, ...path].join('.');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export const SECRET_REF_KIND = 'secretRef';
|
|
165
|
+
|
|
166
|
+
/** A config field that names a secret: `{ secret: NAME }` in YAML. The value never enters the config. */
|
|
167
|
+
export function secretRef<const N extends string>(s: Secret<N>) {
|
|
168
|
+
return z
|
|
169
|
+
.strictObject({ secret: z.literal(s.name) })
|
|
170
|
+
.meta({ 'x-olve-kind': SECRET_REF_KIND });
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** `ctx.config`: exactly the entries a step declares, after parsing (defaults applied). */
|
|
174
|
+
export type ConfigValues<C extends AnyConfigEntry> = {
|
|
175
|
+
readonly [E in C as E['name']]: z.output<E['schema']>;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// ─── select: branching on a config union ────────────────────────────────────────────────────
|
|
179
|
+
|
|
180
|
+
type IsLiteralString<T> = [T] extends [string]
|
|
181
|
+
? string extends T
|
|
182
|
+
? false
|
|
183
|
+
: true
|
|
184
|
+
: false;
|
|
185
|
+
/** The fields of union `U` whose type is a string literal in every member: its discriminator(s). */
|
|
186
|
+
export type DiscriminatorOf<U> = {
|
|
187
|
+
[K in keyof U & string]-?: IsLiteralString<U[K]> extends true ? K : never;
|
|
188
|
+
}[keyof U & string];
|
|
189
|
+
/** The member of `U` whose discriminator `D` is `V`. */
|
|
190
|
+
export type VariantOf<U, D extends string, V> = Extract<U, { readonly [K in D]: V }>;
|
|
191
|
+
|
|
192
|
+
function unwrap(schema: z.ZodType): z.ZodType {
|
|
193
|
+
let s = schema as unknown as { _zod: { def: { innerType?: z.ZodType } } };
|
|
194
|
+
for (let i = 0; i < 16 && s._zod.def.innerType; i++) s = s._zod.def.innerType as never;
|
|
195
|
+
return s as unknown as z.ZodType;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The discriminated union a reference points at: its discriminator and, per value, nothing else.
|
|
200
|
+
* Undefined when the referenced value is not a `z.discriminatedUnion`.
|
|
201
|
+
*/
|
|
202
|
+
export function unionAt(
|
|
203
|
+
ref: ConfigRef,
|
|
204
|
+
): { discriminator: string; values: string[] } | undefined {
|
|
205
|
+
const { entry, path } = configRefTarget(ref);
|
|
206
|
+
let s = unwrap(entry.schema as z.ZodType) as unknown as {
|
|
207
|
+
_zod: { def: Record<string, unknown> };
|
|
208
|
+
};
|
|
209
|
+
for (const seg of path) {
|
|
210
|
+
const shape = s._zod.def.shape as Record<string, z.ZodType> | undefined;
|
|
211
|
+
if (!shape || !(seg in shape)) return undefined;
|
|
212
|
+
s = unwrap(shape[seg]!) as never;
|
|
213
|
+
}
|
|
214
|
+
const def = s._zod.def as {
|
|
215
|
+
discriminator?: string;
|
|
216
|
+
options?: { _zod: { propValues?: Record<string, Set<unknown>> } }[];
|
|
217
|
+
};
|
|
218
|
+
if (typeof def.discriminator !== 'string' || !Array.isArray(def.options))
|
|
219
|
+
return undefined;
|
|
220
|
+
const values = def.options
|
|
221
|
+
.flatMap((o) => [...(o._zod.propValues?.[def.discriminator!] ?? [])])
|
|
222
|
+
.filter((v): v is string => typeof v === 'string');
|
|
223
|
+
return { discriminator: def.discriminator, values };
|
|
224
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { isModel, type AnyModel } from './models.ts';
|
|
2
|
+
|
|
3
|
+
/** Who or what may supply an external input's value. */
|
|
4
|
+
export type ExternalSource =
|
|
5
|
+
| { readonly kind: 'person' }
|
|
6
|
+
| { readonly kind: 'callback' }
|
|
7
|
+
| {
|
|
8
|
+
readonly kind: 'poll';
|
|
9
|
+
readonly url: string;
|
|
10
|
+
readonly valuePath: string;
|
|
11
|
+
readonly intervalSeconds: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** A signed-in user answers, in the UI or with `pl input`. */
|
|
15
|
+
export function person(): ExternalSource {
|
|
16
|
+
return Object.freeze({ kind: 'person' as const });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Another system answers by POSTing to this run's callback URL for the input, which an earlier step hands it
|
|
21
|
+
* with `ctx.inputUrl(step, input)`. The body is the value as JSON, or `{"$decline": "reason"}`.
|
|
22
|
+
*/
|
|
23
|
+
export function callback(): ExternalSource {
|
|
24
|
+
return Object.freeze({ kind: 'callback' as const });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface PollOptions {
|
|
28
|
+
/** GET while the step waits; `{run.id}` and `{run.number}` are filled in. */
|
|
29
|
+
url: string;
|
|
30
|
+
/** Dot path to the value in the JSON response; `.` is the whole body. */
|
|
31
|
+
valuePath: string;
|
|
32
|
+
/** Default 30; at least 10. */
|
|
33
|
+
intervalSeconds?: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The service polls `url` while the step waits and answers with the value at `valuePath` once it validates. */
|
|
37
|
+
export function poll(options: PollOptions): ExternalSource {
|
|
38
|
+
return Object.freeze({
|
|
39
|
+
kind: 'poll' as const,
|
|
40
|
+
url: options.url,
|
|
41
|
+
valuePath: options.valuePath,
|
|
42
|
+
intervalSeconds: options.intervalSeconds ?? 30,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A step input whose value comes from outside the run. The step waits until it is answered (the value is
|
|
48
|
+
* validated against `model`), then runs with the value in its input like any other.
|
|
49
|
+
*/
|
|
50
|
+
export interface External<M extends AnyModel = AnyModel> {
|
|
51
|
+
readonly kind: 'external';
|
|
52
|
+
readonly model: M;
|
|
53
|
+
readonly title: string;
|
|
54
|
+
readonly description: string;
|
|
55
|
+
readonly from: readonly ExternalSource[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface ExternalOptions {
|
|
59
|
+
/** What is asked, shown to whoever answers. */
|
|
60
|
+
title: string;
|
|
61
|
+
description?: string;
|
|
62
|
+
/** Who may answer (default: a person). */
|
|
63
|
+
from?: readonly ExternalSource[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function external<M extends AnyModel>(
|
|
67
|
+
model: M,
|
|
68
|
+
options: ExternalOptions,
|
|
69
|
+
): External<M> {
|
|
70
|
+
return Object.freeze({
|
|
71
|
+
kind: 'external' as const,
|
|
72
|
+
model,
|
|
73
|
+
title: options.title,
|
|
74
|
+
description: options.description ?? '',
|
|
75
|
+
from: Object.freeze([...(options.from ?? [person()])]),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function isExternal(v: unknown): v is External {
|
|
80
|
+
return (
|
|
81
|
+
typeof v === 'object' &&
|
|
82
|
+
v !== null &&
|
|
83
|
+
(v as { kind?: unknown }).kind === 'external' &&
|
|
84
|
+
isModel((v as { model?: unknown }).model)
|
|
85
|
+
);
|
|
86
|
+
}
|
package/src/core/handlers.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import {
|
|
2
|
+
IMPLEMENTATION,
|
|
3
|
+
ORIGIN,
|
|
4
|
+
SKIPPED,
|
|
5
|
+
type ImplementationBrand,
|
|
6
|
+
type SkippedBrand,
|
|
7
|
+
} from './brands.ts';
|
|
8
|
+
import type { Ctx } from '../ctx/types.ts';
|
|
9
|
+
import { callerFile, originsOf } from './origin.ts';
|
|
10
|
+
import type { AnyStep, ConfigOf, OutputValue, SecretsOf, StepInput } from './steps.ts';
|
|
11
|
+
import type { Awaitable } from './types.ts';
|
|
5
12
|
|
|
6
13
|
/** Returned by `ctx.skip(reason)`: the step produced no output on purpose. */
|
|
7
14
|
export interface Skipped {
|
|
@@ -12,33 +19,72 @@ export function skipped(reason: string): Skipped {
|
|
|
12
19
|
return Object.freeze({ [SKIPPED]: true, reason }) as unknown as Skipped;
|
|
13
20
|
}
|
|
14
21
|
export function isSkipped(v: unknown): v is Skipped {
|
|
15
|
-
return
|
|
22
|
+
return (
|
|
23
|
+
typeof v === 'object' &&
|
|
24
|
+
v !== null &&
|
|
25
|
+
(v as Record<symbol, unknown>)[SKIPPED] === true
|
|
26
|
+
);
|
|
16
27
|
}
|
|
17
28
|
|
|
18
29
|
export type HandlerResult<S extends AnyStep> = OutputValue<S> | Skipped;
|
|
19
|
-
export type Handler<S extends AnyStep> = (
|
|
30
|
+
export type Handler<S extends AnyStep> = (
|
|
31
|
+
input: StepInput<S['input']>,
|
|
32
|
+
ctx: Ctx<SecretsOf<S>, ConfigOf<S>>,
|
|
33
|
+
) => Awaitable<HandlerResult<S>>;
|
|
20
34
|
|
|
21
|
-
|
|
35
|
+
/** A step with its handler. It is itself a step: stage it directly, or default-export it from `handlers/*.ts`. */
|
|
36
|
+
export type Implementation<S extends AnyStep = AnyStep> = S & {
|
|
37
|
+
readonly [ImplementationBrand]: true;
|
|
38
|
+
/** The step as defined. */
|
|
22
39
|
readonly step: S;
|
|
23
40
|
readonly handler: Handler<S>;
|
|
24
|
-
}
|
|
41
|
+
};
|
|
25
42
|
|
|
26
43
|
/** Block-bodied/async returns get no excess-property check; this restores it for handler outputs. */
|
|
27
44
|
type NoExtraFields<R, T> = R extends Skipped | void
|
|
28
45
|
? unknown
|
|
29
|
-
: {
|
|
46
|
+
: {
|
|
47
|
+
[
|
|
48
|
+
K in Exclude<keyof R, keyof T>
|
|
49
|
+
]: `❌ '${K & string}' is not a field of the output model`;
|
|
50
|
+
};
|
|
30
51
|
|
|
31
52
|
/**
|
|
32
|
-
* Implement a
|
|
33
|
-
*
|
|
53
|
+
* Implement a step. Input, ctx and return types come from the step definition alone. Default-export
|
|
54
|
+
* the result from a `handlers/*.ts` file, or stage it directly (a reusable step: give it a `kind`).
|
|
34
55
|
*/
|
|
35
56
|
export function implement<S extends AnyStep, R extends HandlerResult<S>>(
|
|
36
|
-
step: S &
|
|
37
|
-
|
|
57
|
+
step: S &
|
|
58
|
+
(S extends { readonly [ImplementationBrand]: true }
|
|
59
|
+
? '❌ this step already carries its implementation'
|
|
60
|
+
: unknown),
|
|
61
|
+
handler: (
|
|
62
|
+
input: StepInput<S['input']>,
|
|
63
|
+
ctx: Ctx<SecretsOf<S>, ConfigOf<S>>,
|
|
64
|
+
) => Awaitable<R & NoExtraFields<R, OutputValue<S>>>,
|
|
38
65
|
): Implementation<S> {
|
|
39
|
-
|
|
66
|
+
if (isImplementation(step))
|
|
67
|
+
throw new TypeError(
|
|
68
|
+
`step '${(step as AnyStep).name}' already carries its implementation`,
|
|
69
|
+
);
|
|
70
|
+
const origins = [
|
|
71
|
+
...new Set(
|
|
72
|
+
[...originsOf(step), callerFile()].filter((f): f is string => f !== undefined),
|
|
73
|
+
),
|
|
74
|
+
];
|
|
75
|
+
return Object.freeze({
|
|
76
|
+
...step,
|
|
77
|
+
[IMPLEMENTATION]: true,
|
|
78
|
+
[ORIGIN]: origins,
|
|
79
|
+
step,
|
|
80
|
+
handler: handler as unknown as Handler<S>,
|
|
81
|
+
}) as unknown as Implementation<S>;
|
|
40
82
|
}
|
|
41
83
|
|
|
42
84
|
export function isImplementation(v: unknown): v is Implementation {
|
|
43
|
-
return
|
|
85
|
+
return (
|
|
86
|
+
typeof v === 'object' &&
|
|
87
|
+
v !== null &&
|
|
88
|
+
(v as Record<symbol, unknown>)[IMPLEMENTATION] === true
|
|
89
|
+
);
|
|
44
90
|
}
|
package/src/core/models.ts
CHANGED
|
@@ -1,30 +1,40 @@
|
|
|
1
|
-
import { z } from
|
|
2
|
-
import type { ModelBrand } from
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { ModelBrand } from './brands.ts';
|
|
3
3
|
|
|
4
4
|
/** A named, nominal zod schema. The name is its identity in the manifest (one schema per name). */
|
|
5
5
|
export interface Model<N extends string = string, S extends z.ZodType = z.ZodType> {
|
|
6
|
-
readonly kind:
|
|
6
|
+
readonly kind: 'model';
|
|
7
7
|
readonly name: N;
|
|
8
8
|
readonly schema: S;
|
|
9
9
|
}
|
|
10
10
|
export type AnyModel = Model<string, z.ZodType>;
|
|
11
11
|
|
|
12
12
|
/** Output view of a model (`z.output`), nominally branded with the model name. The brand is optional, so plain literals still assign. */
|
|
13
|
-
export type Infer<M extends AnyModel> = z.output<M[
|
|
13
|
+
export type Infer<M extends AnyModel> = z.output<M['schema']> & {
|
|
14
|
+
readonly [ModelBrand]?: M['name'];
|
|
15
|
+
};
|
|
14
16
|
/** Input view of a model (`z.input`: fields with defaults are optional). What trigger mappers produce. */
|
|
15
|
-
export type InferInput<M extends AnyModel> = z.input<M[
|
|
17
|
+
export type InferInput<M extends AnyModel> = z.input<M['schema']>;
|
|
16
18
|
/** Output view without the nominal brand. */
|
|
17
|
-
export type Plain<M extends AnyModel> = z.output<M[
|
|
19
|
+
export type Plain<M extends AnyModel> = z.output<M['schema']>;
|
|
18
20
|
|
|
19
21
|
/** Model names: identifier-like, so they are safe as JSON keys, C# names and URL segments. */
|
|
20
22
|
export const MODEL_NAME = /^[A-Za-z][A-Za-z0-9_]*$/;
|
|
21
23
|
|
|
22
|
-
export function model<const N extends string, S extends z.ZodType>(
|
|
23
|
-
|
|
24
|
+
export function model<const N extends string, S extends z.ZodType>(
|
|
25
|
+
name: N,
|
|
26
|
+
schema: S,
|
|
27
|
+
): Model<N, S> {
|
|
28
|
+
return Object.freeze({ kind: 'model' as const, name, schema });
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
export function isModel(v: unknown): v is AnyModel {
|
|
27
|
-
return
|
|
32
|
+
return (
|
|
33
|
+
typeof v === 'object' &&
|
|
34
|
+
v !== null &&
|
|
35
|
+
(v as { kind?: unknown }).kind === 'model' &&
|
|
36
|
+
(v as { schema?: unknown }).schema instanceof z.ZodType
|
|
37
|
+
);
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
// ─── File references ─────────────────────────────────────────────────────────────────────────
|
|
@@ -36,17 +46,19 @@ export function isModel(v: unknown): v is AnyModel {
|
|
|
36
46
|
const SEGMENT = String.raw`(?!\.\.?(?:/|$))[^/\\\x00]+`;
|
|
37
47
|
export const FILE_PATH_PATTERN = `^(?:${SEGMENT}/)*${SEGMENT}/?$`;
|
|
38
48
|
export const FILE_PATH = new RegExp(FILE_PATH_PATTERN);
|
|
49
|
+
/** Step names: lower-case kebab, ≤ 63 chars (usable as K8s label values, URL segments and storage paths). */
|
|
50
|
+
export const STEP_NAME = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
39
51
|
|
|
40
52
|
const FileRefSchema = z
|
|
41
53
|
.object({
|
|
42
54
|
run: z.number().int().nonnegative(),
|
|
43
|
-
step: z.string().
|
|
55
|
+
step: z.string().regex(STEP_NAME),
|
|
44
56
|
attempt: z.number().int().positive(),
|
|
45
57
|
path: z.string().regex(FILE_PATH),
|
|
46
58
|
sha256: z.string().regex(/^[0-9a-f]{64}$/),
|
|
47
59
|
})
|
|
48
|
-
.brand<
|
|
49
|
-
.meta({
|
|
60
|
+
.brand<'FileRef'>()
|
|
61
|
+
.meta({ 'x-olve-kind': 'fileRef' });
|
|
50
62
|
|
|
51
63
|
/** A file or directory in some step attempt's output (`runs/<p>/<run>/<step>/<attempt>/files/<path>`). Branded: only `ctx.output.*` or a mapper ref produce one. */
|
|
52
64
|
export type FileRef = z.output<typeof FileRefSchema>;
|
|
@@ -58,12 +70,17 @@ export const fileRefSchema = FileRefSchema;
|
|
|
58
70
|
|
|
59
71
|
/** A step input that may be missing because its producer was *maybe skipped* (inside a `when`). There must still be a producer. */
|
|
60
72
|
export interface Optional<M extends AnyModel = AnyModel> {
|
|
61
|
-
readonly kind:
|
|
73
|
+
readonly kind: 'optional';
|
|
62
74
|
readonly model: M;
|
|
63
75
|
}
|
|
64
76
|
export function optional<M extends AnyModel>(model: M): Optional<M> {
|
|
65
|
-
return Object.freeze({ kind:
|
|
77
|
+
return Object.freeze({ kind: 'optional' as const, model });
|
|
66
78
|
}
|
|
67
79
|
export function isOptional(v: unknown): v is Optional {
|
|
68
|
-
return
|
|
80
|
+
return (
|
|
81
|
+
typeof v === 'object' &&
|
|
82
|
+
v !== null &&
|
|
83
|
+
(v as { kind?: unknown }).kind === 'optional' &&
|
|
84
|
+
isModel((v as { model?: unknown }).model)
|
|
85
|
+
);
|
|
69
86
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ORIGIN } from './brands.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The file that called the function calling this one, from the stack. A tail call drops its caller's
|
|
5
|
+
* frame, so this can name a module further up; the build only uses it to choose what to hash.
|
|
6
|
+
*/
|
|
7
|
+
export function callerFile(): string | undefined {
|
|
8
|
+
const files = (new Error().stack ?? '')
|
|
9
|
+
.split('\n')
|
|
10
|
+
.slice(1)
|
|
11
|
+
.map((f) => f.match(FRAME)?.[1]);
|
|
12
|
+
const own = files[0];
|
|
13
|
+
const callee = files[1];
|
|
14
|
+
const file = files.slice(2).find((f) => f && f !== own && f !== callee) ?? undefined;
|
|
15
|
+
return file?.startsWith('file://') ? new URL(file).pathname : file;
|
|
16
|
+
}
|
|
17
|
+
const FRAME = /\(?((?:file:\/\/)?\/[^()]*?):\d+(?::\d+)?\)?\s*$/;
|
|
18
|
+
|
|
19
|
+
/** The modules that defined and implemented a step (where `step` and `implement` were called). */
|
|
20
|
+
export function originsOf(v: unknown): string[] {
|
|
21
|
+
const o = (v as Record<symbol, unknown>)[ORIGIN];
|
|
22
|
+
return Array.isArray(o) ? o.filter((x): x is string => typeof x === 'string') : [];
|
|
23
|
+
}
|
package/src/core/secrets.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** A named pipeline secret. Steps (and poll triggers) declare the secrets they use; only those are mounted. */
|
|
2
2
|
export interface Secret<N extends string = string> {
|
|
3
|
-
readonly kind:
|
|
3
|
+
readonly kind: 'secret';
|
|
4
4
|
readonly name: N;
|
|
5
5
|
readonly description: string;
|
|
6
6
|
}
|
|
@@ -8,6 +8,6 @@ export interface Secret<N extends string = string> {
|
|
|
8
8
|
export const SECRET_NAME = /^[A-Z_][A-Z0-9_]*$/;
|
|
9
9
|
|
|
10
10
|
export function secret<const N extends string>(name: N, description: string): Secret<N> {
|
|
11
|
-
return Object.freeze({ kind:
|
|
11
|
+
return Object.freeze({ kind: 'secret' as const, name, description });
|
|
12
12
|
}
|
|
13
13
|
export type DeclaredSecrets<N extends string> = { readonly [K in N]: string };
|