@sapiom/agent 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +205 -0
  2. package/LICENSE +21 -0
  3. package/README.md +130 -0
  4. package/dist/cjs/agent.d.ts +10 -0
  5. package/dist/cjs/agent.js +38 -0
  6. package/dist/cjs/build-manifest.d.ts +15 -0
  7. package/dist/cjs/build-manifest.js +170 -0
  8. package/dist/cjs/context.d.ts +47 -0
  9. package/dist/cjs/context.js +21 -0
  10. package/dist/cjs/directives.d.ts +95 -0
  11. package/dist/cjs/directives.js +74 -0
  12. package/dist/cjs/errors.d.ts +19 -0
  13. package/dist/cjs/errors.js +49 -0
  14. package/dist/cjs/index.d.ts +17 -0
  15. package/dist/cjs/index.js +41 -0
  16. package/dist/cjs/introspection.d.ts +11 -0
  17. package/dist/cjs/introspection.js +62 -0
  18. package/dist/cjs/manifest.d.ts +56 -0
  19. package/dist/cjs/manifest.js +27 -0
  20. package/dist/cjs/step.d.ts +42 -0
  21. package/dist/cjs/step.js +15 -0
  22. package/dist/esm/agent.d.ts +10 -0
  23. package/dist/esm/agent.js +33 -0
  24. package/dist/esm/build-manifest.d.ts +15 -0
  25. package/dist/esm/build-manifest.js +165 -0
  26. package/dist/esm/context.d.ts +47 -0
  27. package/dist/esm/context.js +17 -0
  28. package/dist/esm/directives.d.ts +95 -0
  29. package/dist/esm/directives.js +61 -0
  30. package/dist/esm/errors.d.ts +19 -0
  31. package/dist/esm/errors.js +42 -0
  32. package/dist/esm/index.d.ts +17 -0
  33. package/dist/esm/index.js +9 -0
  34. package/dist/esm/introspection.d.ts +11 -0
  35. package/dist/esm/introspection.js +56 -0
  36. package/dist/esm/manifest.d.ts +56 -0
  37. package/dist/esm/manifest.js +24 -0
  38. package/dist/esm/package.json +1 -0
  39. package/dist/esm/step.d.ts +42 -0
  40. package/dist/esm/step.js +12 -0
  41. package/dist/tsconfig.cjs.tsbuildinfo +1 -0
  42. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  43. package/package.json +74 -0
@@ -0,0 +1,95 @@
1
+ import type { DispatchHandle } from '@sapiom/tools';
2
+ export declare const DIRECTIVE_KIND: {
3
+ readonly CONTINUE: "continue";
4
+ readonly RETRY: "retry";
5
+ readonly PAUSE_UNTIL_SIGNAL: "pause_until_signal";
6
+ readonly TERMINATE: "terminate";
7
+ readonly FAIL: "fail";
8
+ };
9
+ export type DirectiveKind = (typeof DIRECTIVE_KIND)[keyof typeof DIRECTIVE_KIND];
10
+ export type NextStepDirective = ContinueDirective | RetryDirective | PauseUntilSignalDirective | TerminateDirective | FailDirective;
11
+ export interface ContinueDirective {
12
+ readonly kind: typeof DIRECTIVE_KIND.CONTINUE;
13
+ readonly stepName: string;
14
+ readonly input?: unknown;
15
+ }
16
+ export interface RetryDirective {
17
+ readonly kind: typeof DIRECTIVE_KIND.RETRY;
18
+ readonly delayMs?: number;
19
+ readonly reason?: string;
20
+ }
21
+ export interface PauseUntilSignalDirective {
22
+ readonly kind: typeof DIRECTIVE_KIND.PAUSE_UNTIL_SIGNAL;
23
+ readonly signal: {
24
+ readonly name: string;
25
+ readonly correlationId?: string;
26
+ };
27
+ readonly timeoutMs?: number;
28
+ readonly resumeStep?: string;
29
+ }
30
+ export interface TerminateDirective {
31
+ readonly kind: typeof DIRECTIVE_KIND.TERMINATE;
32
+ readonly reason?: string;
33
+ }
34
+ export interface FailDirective {
35
+ readonly kind: typeof DIRECTIVE_KIND.FAIL;
36
+ readonly reason?: string;
37
+ }
38
+ export declare function isContinue(d: NextStepDirective): d is ContinueDirective;
39
+ export declare function isRetry(d: NextStepDirective): d is RetryDirective;
40
+ export declare function isPause(d: NextStepDirective): d is PauseUntilSignalDirective;
41
+ export declare function isTerminate(d: NextStepDirective): d is TerminateDirective;
42
+ export declare function isFail(d: NextStepDirective): d is FailDirective;
43
+ export interface Goto<Target extends string> {
44
+ readonly kind: typeof DIRECTIVE_KIND.CONTINUE;
45
+ readonly stepName: Target;
46
+ readonly input?: unknown;
47
+ }
48
+ export interface Terminate {
49
+ readonly kind: typeof DIRECTIVE_KIND.TERMINATE;
50
+ readonly output?: unknown;
51
+ readonly reason?: string;
52
+ }
53
+ export interface Fail {
54
+ readonly kind: typeof DIRECTIVE_KIND.FAIL;
55
+ readonly reason?: string;
56
+ readonly output?: unknown;
57
+ }
58
+ export interface Pause<Resume extends string> {
59
+ readonly kind: typeof DIRECTIVE_KIND.PAUSE_UNTIL_SIGNAL;
60
+ readonly signal: {
61
+ readonly name: string;
62
+ readonly correlationId?: string;
63
+ };
64
+ readonly resumeStep?: Resume;
65
+ readonly timeoutMs?: number;
66
+ readonly output?: unknown;
67
+ }
68
+ export interface Retry {
69
+ readonly kind: typeof DIRECTIVE_KIND.RETRY;
70
+ readonly delayMs?: number;
71
+ readonly reason?: string;
72
+ }
73
+ export declare function goto<const Target extends string>(target: Target, output?: unknown): Goto<Target>;
74
+ export declare function terminate(output?: unknown, opts?: {
75
+ reason?: string;
76
+ }): Terminate;
77
+ export declare function fail(reason?: string, opts?: {
78
+ output?: unknown;
79
+ }): Fail;
80
+ export declare function pauseUntilSignal<const Resume extends string>(args: {
81
+ signal: string;
82
+ resumeStep?: Resume;
83
+ correlationId?: string;
84
+ timeoutMs?: number;
85
+ output?: unknown;
86
+ }): Pause<Resume>;
87
+ export declare function pauseUntilSignal<const Resume extends string>(handle: DispatchHandle | Promise<DispatchHandle>, opts?: {
88
+ resumeStep?: Resume;
89
+ timeoutMs?: number;
90
+ output?: unknown;
91
+ }): Promise<Pause<Resume>>;
92
+ export declare function retry(opts?: {
93
+ delayMs?: number;
94
+ reason?: string;
95
+ }): Retry;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DIRECTIVE_KIND = void 0;
4
+ exports.isContinue = isContinue;
5
+ exports.isRetry = isRetry;
6
+ exports.isPause = isPause;
7
+ exports.isTerminate = isTerminate;
8
+ exports.isFail = isFail;
9
+ exports.goto = goto;
10
+ exports.terminate = terminate;
11
+ exports.fail = fail;
12
+ exports.pauseUntilSignal = pauseUntilSignal;
13
+ exports.retry = retry;
14
+ exports.DIRECTIVE_KIND = {
15
+ CONTINUE: 'continue',
16
+ RETRY: 'retry',
17
+ PAUSE_UNTIL_SIGNAL: 'pause_until_signal',
18
+ TERMINATE: 'terminate',
19
+ FAIL: 'fail',
20
+ };
21
+ function isContinue(d) {
22
+ return d.kind === exports.DIRECTIVE_KIND.CONTINUE;
23
+ }
24
+ function isRetry(d) {
25
+ return d.kind === exports.DIRECTIVE_KIND.RETRY;
26
+ }
27
+ function isPause(d) {
28
+ return d.kind === exports.DIRECTIVE_KIND.PAUSE_UNTIL_SIGNAL;
29
+ }
30
+ function isTerminate(d) {
31
+ return d.kind === exports.DIRECTIVE_KIND.TERMINATE;
32
+ }
33
+ function isFail(d) {
34
+ return d.kind === exports.DIRECTIVE_KIND.FAIL;
35
+ }
36
+ function goto(target, output) {
37
+ return { kind: exports.DIRECTIVE_KIND.CONTINUE, stepName: target, input: output };
38
+ }
39
+ function terminate(output, opts) {
40
+ return { kind: exports.DIRECTIVE_KIND.TERMINATE, output, reason: opts?.reason };
41
+ }
42
+ function fail(reason, opts) {
43
+ return { kind: exports.DIRECTIVE_KIND.FAIL, reason, output: opts?.output };
44
+ }
45
+ function pauseUntilSignal(argOrHandle, opts) {
46
+ if (isThenable(argOrHandle)) {
47
+ return Promise.resolve(argOrHandle).then((handle) => pauseFromHandle(handle, opts));
48
+ }
49
+ if ('dispatch' in argOrHandle) {
50
+ return Promise.resolve(pauseFromHandle(argOrHandle, opts));
51
+ }
52
+ return {
53
+ kind: exports.DIRECTIVE_KIND.PAUSE_UNTIL_SIGNAL,
54
+ signal: { name: argOrHandle.signal, correlationId: argOrHandle.correlationId },
55
+ resumeStep: argOrHandle.resumeStep,
56
+ timeoutMs: argOrHandle.timeoutMs,
57
+ output: argOrHandle.output,
58
+ };
59
+ }
60
+ function isThenable(x) {
61
+ return x != null && typeof x.then === 'function';
62
+ }
63
+ function pauseFromHandle(handle, opts) {
64
+ return {
65
+ kind: exports.DIRECTIVE_KIND.PAUSE_UNTIL_SIGNAL,
66
+ signal: { name: handle.dispatch.resultSignal, correlationId: handle.dispatch.correlationId },
67
+ resumeStep: opts?.resumeStep,
68
+ timeoutMs: opts?.timeoutMs,
69
+ output: opts?.output,
70
+ };
71
+ }
72
+ function retry(opts) {
73
+ return { kind: exports.DIRECTIVE_KIND.RETRY, delayMs: opts?.delayMs, reason: opts?.reason };
74
+ }
@@ -0,0 +1,19 @@
1
+ import type { $ZodIssue } from 'zod/v4/core';
2
+ export declare class AgentError extends Error {
3
+ constructor(message: string);
4
+ }
5
+ export declare class StepInputValidationError extends AgentError {
6
+ readonly stepName: string;
7
+ readonly issues: readonly $ZodIssue[];
8
+ constructor(stepName: string, issues: readonly $ZodIssue[]);
9
+ }
10
+ export declare class UnknownStepError extends AgentError {
11
+ readonly stepName: string;
12
+ constructor(stepName: string);
13
+ }
14
+ export declare class DisallowedTransitionError extends AgentError {
15
+ readonly stepName: string;
16
+ readonly directiveKind: string;
17
+ readonly target?: string;
18
+ constructor(stepName: string, directiveKind: string, target?: string);
19
+ }
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DisallowedTransitionError = exports.UnknownStepError = exports.StepInputValidationError = exports.AgentError = void 0;
4
+ class AgentError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = 'AgentError';
8
+ }
9
+ }
10
+ exports.AgentError = AgentError;
11
+ class StepInputValidationError extends AgentError {
12
+ constructor(stepName, issues) {
13
+ super(`Input for step '${stepName}' failed validation: ${formatIssues(issues)}`);
14
+ this.name = 'StepInputValidationError';
15
+ this.stepName = stepName;
16
+ this.issues = issues;
17
+ }
18
+ }
19
+ exports.StepInputValidationError = StepInputValidationError;
20
+ function formatIssues(issues) {
21
+ if (issues.length === 0)
22
+ return 'unknown validation error';
23
+ return issues
24
+ .map((issue) => {
25
+ const path = issue.path.length > 0 ? issue.path.join('.') : '(root)';
26
+ return `${path}: ${issue.message}`;
27
+ })
28
+ .join('; ');
29
+ }
30
+ class UnknownStepError extends AgentError {
31
+ constructor(stepName) {
32
+ super(`Unknown step: ${stepName}`);
33
+ this.name = 'UnknownStepError';
34
+ this.stepName = stepName;
35
+ }
36
+ }
37
+ exports.UnknownStepError = UnknownStepError;
38
+ class DisallowedTransitionError extends AgentError {
39
+ constructor(stepName, directiveKind, target) {
40
+ super(`Step '${stepName}' returned a '${directiveKind}' directive` +
41
+ (target ? ` to '${target}'` : '') +
42
+ ` that is not in its declared transitions`);
43
+ this.name = 'DisallowedTransitionError';
44
+ this.stepName = stepName;
45
+ this.directiveKind = directiveKind;
46
+ this.target = target;
47
+ }
48
+ }
49
+ exports.DisallowedTransitionError = DisallowedTransitionError;
@@ -0,0 +1,17 @@
1
+ export { DIRECTIVE_KIND, isContinue, isRetry, isPause, isTerminate, isFail } from './directives.js';
2
+ export type { DirectiveKind, NextStepDirective, ContinueDirective, RetryDirective, PauseUntilSignalDirective, TerminateDirective, FailDirective, } from './directives.js';
3
+ export { goto, terminate, fail, pauseUntilSignal, retry } from './directives.js';
4
+ export type { Goto, Terminate, Fail, Pause, Retry } from './directives.js';
5
+ export { defineStep } from './step.js';
6
+ export type { Step, StepResult, StepDefinition, Allowed } from './step.js';
7
+ export type { AgentExecutionContext, TypedContextStore, StepExecutionRecord, StepLogger, FinishedStepStatus, } from './context.js';
8
+ export { InMemoryContextStore } from './context.js';
9
+ export type { AgentDefinition } from './agent.js';
10
+ export { defineAgent, isAgentDefinition, AGENT_DEFINITION_BRAND } from './agent.js';
11
+ export { AgentError, UnknownStepError, StepInputValidationError, DisallowedTransitionError } from './errors.js';
12
+ export { zodToJsonSchema, exampleFromJsonSchema, stepInputContract, workflowInputContract } from './introspection.js';
13
+ export type { StepInputContract, AgentInputContract } from './introspection.js';
14
+ export { MANIFEST_PROTOCOL, agentManifestSchema } from './manifest.js';
15
+ export type { AgentManifest, AgentStepManifest, ManifestTransition } from './manifest.js';
16
+ export { buildManifest, validateGraph, assertValidGraph } from './build-manifest.js';
17
+ export type { GraphValidation } from './build-manifest.js';
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertValidGraph = exports.validateGraph = exports.buildManifest = exports.agentManifestSchema = exports.MANIFEST_PROTOCOL = exports.workflowInputContract = exports.stepInputContract = exports.exampleFromJsonSchema = exports.zodToJsonSchema = exports.DisallowedTransitionError = exports.StepInputValidationError = exports.UnknownStepError = exports.AgentError = exports.AGENT_DEFINITION_BRAND = exports.isAgentDefinition = exports.defineAgent = exports.InMemoryContextStore = exports.defineStep = exports.retry = exports.pauseUntilSignal = exports.terminate = exports.goto = exports.isFail = exports.isTerminate = exports.isPause = exports.isRetry = exports.isContinue = exports.DIRECTIVE_KIND = void 0;
4
+ var directives_js_1 = require("./directives.js");
5
+ Object.defineProperty(exports, "DIRECTIVE_KIND", { enumerable: true, get: function () { return directives_js_1.DIRECTIVE_KIND; } });
6
+ Object.defineProperty(exports, "isContinue", { enumerable: true, get: function () { return directives_js_1.isContinue; } });
7
+ Object.defineProperty(exports, "isRetry", { enumerable: true, get: function () { return directives_js_1.isRetry; } });
8
+ Object.defineProperty(exports, "isPause", { enumerable: true, get: function () { return directives_js_1.isPause; } });
9
+ Object.defineProperty(exports, "isTerminate", { enumerable: true, get: function () { return directives_js_1.isTerminate; } });
10
+ Object.defineProperty(exports, "isFail", { enumerable: true, get: function () { return directives_js_1.isFail; } });
11
+ var directives_js_2 = require("./directives.js");
12
+ Object.defineProperty(exports, "goto", { enumerable: true, get: function () { return directives_js_2.goto; } });
13
+ Object.defineProperty(exports, "terminate", { enumerable: true, get: function () { return directives_js_2.terminate; } });
14
+ Object.defineProperty(exports, "fail", { enumerable: true, get: function () { return directives_js_2.fail; } });
15
+ Object.defineProperty(exports, "pauseUntilSignal", { enumerable: true, get: function () { return directives_js_2.pauseUntilSignal; } });
16
+ Object.defineProperty(exports, "retry", { enumerable: true, get: function () { return directives_js_2.retry; } });
17
+ var step_js_1 = require("./step.js");
18
+ Object.defineProperty(exports, "defineStep", { enumerable: true, get: function () { return step_js_1.defineStep; } });
19
+ var context_js_1 = require("./context.js");
20
+ Object.defineProperty(exports, "InMemoryContextStore", { enumerable: true, get: function () { return context_js_1.InMemoryContextStore; } });
21
+ var agent_js_1 = require("./agent.js");
22
+ Object.defineProperty(exports, "defineAgent", { enumerable: true, get: function () { return agent_js_1.defineAgent; } });
23
+ Object.defineProperty(exports, "isAgentDefinition", { enumerable: true, get: function () { return agent_js_1.isAgentDefinition; } });
24
+ Object.defineProperty(exports, "AGENT_DEFINITION_BRAND", { enumerable: true, get: function () { return agent_js_1.AGENT_DEFINITION_BRAND; } });
25
+ var errors_js_1 = require("./errors.js");
26
+ Object.defineProperty(exports, "AgentError", { enumerable: true, get: function () { return errors_js_1.AgentError; } });
27
+ Object.defineProperty(exports, "UnknownStepError", { enumerable: true, get: function () { return errors_js_1.UnknownStepError; } });
28
+ Object.defineProperty(exports, "StepInputValidationError", { enumerable: true, get: function () { return errors_js_1.StepInputValidationError; } });
29
+ Object.defineProperty(exports, "DisallowedTransitionError", { enumerable: true, get: function () { return errors_js_1.DisallowedTransitionError; } });
30
+ var introspection_js_1 = require("./introspection.js");
31
+ Object.defineProperty(exports, "zodToJsonSchema", { enumerable: true, get: function () { return introspection_js_1.zodToJsonSchema; } });
32
+ Object.defineProperty(exports, "exampleFromJsonSchema", { enumerable: true, get: function () { return introspection_js_1.exampleFromJsonSchema; } });
33
+ Object.defineProperty(exports, "stepInputContract", { enumerable: true, get: function () { return introspection_js_1.stepInputContract; } });
34
+ Object.defineProperty(exports, "workflowInputContract", { enumerable: true, get: function () { return introspection_js_1.workflowInputContract; } });
35
+ var manifest_js_1 = require("./manifest.js");
36
+ Object.defineProperty(exports, "MANIFEST_PROTOCOL", { enumerable: true, get: function () { return manifest_js_1.MANIFEST_PROTOCOL; } });
37
+ Object.defineProperty(exports, "agentManifestSchema", { enumerable: true, get: function () { return manifest_js_1.agentManifestSchema; } });
38
+ var build_manifest_js_1 = require("./build-manifest.js");
39
+ Object.defineProperty(exports, "buildManifest", { enumerable: true, get: function () { return build_manifest_js_1.buildManifest; } });
40
+ Object.defineProperty(exports, "validateGraph", { enumerable: true, get: function () { return build_manifest_js_1.validateGraph; } });
41
+ Object.defineProperty(exports, "assertValidGraph", { enumerable: true, get: function () { return build_manifest_js_1.assertValidGraph; } });
@@ -0,0 +1,11 @@
1
+ import { z } from 'zod/v4';
2
+ import type { AgentDefinition } from './agent.js';
3
+ export interface StepInputContract {
4
+ readonly jsonSchema: Record<string, unknown>;
5
+ readonly example: unknown;
6
+ }
7
+ export type AgentInputContract = StepInputContract;
8
+ export declare function zodToJsonSchema(schema: z.ZodType): Record<string, unknown>;
9
+ export declare function stepInputContract(def: AgentDefinition<unknown, Record<string, unknown>>, stepName: string): StepInputContract | null;
10
+ export declare function workflowInputContract(def: AgentDefinition<unknown, Record<string, unknown>>): StepInputContract | null;
11
+ export declare function exampleFromJsonSchema(jsonSchema: Record<string, unknown>): unknown;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.zodToJsonSchema = zodToJsonSchema;
4
+ exports.stepInputContract = stepInputContract;
5
+ exports.workflowInputContract = workflowInputContract;
6
+ exports.exampleFromJsonSchema = exampleFromJsonSchema;
7
+ const v4_1 = require("zod/v4");
8
+ function zodToJsonSchema(schema) {
9
+ return v4_1.z.toJSONSchema(schema);
10
+ }
11
+ function stepInputContract(def, stepName) {
12
+ const schema = def.steps[stepName]?.inputSchema;
13
+ if (!schema)
14
+ return null;
15
+ const jsonSchema = zodToJsonSchema(schema);
16
+ return { jsonSchema, example: exampleFromJsonSchema(jsonSchema) };
17
+ }
18
+ function workflowInputContract(def) {
19
+ return stepInputContract(def, def.entry);
20
+ }
21
+ function exampleFromJsonSchema(jsonSchema) {
22
+ const examples = jsonSchema.examples;
23
+ if (Array.isArray(examples) && examples.length > 0) {
24
+ return examples[0];
25
+ }
26
+ if ('example' in jsonSchema && jsonSchema.example !== undefined) {
27
+ return jsonSchema.example;
28
+ }
29
+ return skeletonFromJsonSchema(jsonSchema);
30
+ }
31
+ function skeletonFromJsonSchema(schema) {
32
+ if (Array.isArray(schema.enum) && schema.enum.length > 0) {
33
+ return schema.enum[0];
34
+ }
35
+ const branches = (schema.anyOf ?? schema.oneOf);
36
+ if (Array.isArray(branches) && branches.length > 0) {
37
+ return skeletonFromJsonSchema(branches[0]);
38
+ }
39
+ switch (schema.type) {
40
+ case 'object': {
41
+ const props = (schema.properties ?? {});
42
+ const out = {};
43
+ for (const [key, value] of Object.entries(props)) {
44
+ out[key] = skeletonFromJsonSchema(value);
45
+ }
46
+ return out;
47
+ }
48
+ case 'array':
49
+ return [];
50
+ case 'string':
51
+ return '';
52
+ case 'number':
53
+ case 'integer':
54
+ return 0;
55
+ case 'boolean':
56
+ return false;
57
+ case 'null':
58
+ return null;
59
+ default:
60
+ return null;
61
+ }
62
+ }
@@ -0,0 +1,56 @@
1
+ import { z } from 'zod/v4';
2
+ export declare const MANIFEST_PROTOCOL: 1;
3
+ export type ManifestTransition = {
4
+ readonly kind: 'continue';
5
+ readonly target: string;
6
+ } | {
7
+ readonly kind: 'terminate';
8
+ } | {
9
+ readonly kind: 'fail';
10
+ } | {
11
+ readonly kind: 'pause';
12
+ readonly signal: string;
13
+ readonly resumeStep: string;
14
+ };
15
+ export interface AgentStepManifest {
16
+ readonly timeoutMs: number | null;
17
+ readonly inputSchema: Record<string, unknown> | null;
18
+ readonly transitions: readonly ManifestTransition[];
19
+ }
20
+ export interface AgentManifest {
21
+ readonly protocol: typeof MANIFEST_PROTOCOL;
22
+ readonly name: string;
23
+ readonly entry: string;
24
+ readonly sdkVersion: string;
25
+ readonly artifact: {
26
+ readonly sha256: string;
27
+ readonly entryFile: string;
28
+ };
29
+ readonly steps: Readonly<Record<string, AgentStepManifest>>;
30
+ }
31
+ export declare const agentManifestSchema: z.ZodObject<{
32
+ protocol: z.ZodLiteral<1>;
33
+ name: z.ZodString;
34
+ entry: z.ZodString;
35
+ sdkVersion: z.ZodString;
36
+ artifact: z.ZodObject<{
37
+ sha256: z.ZodString;
38
+ entryFile: z.ZodString;
39
+ }, z.core.$strip>;
40
+ steps: z.ZodRecord<z.ZodString, z.ZodObject<{
41
+ timeoutMs: z.ZodUnion<readonly [z.ZodNumber, z.ZodNull]>;
42
+ inputSchema: z.ZodUnion<readonly [z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodNull]>;
43
+ transitions: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
44
+ kind: z.ZodLiteral<"continue">;
45
+ target: z.ZodString;
46
+ }, z.core.$strip>, z.ZodObject<{
47
+ kind: z.ZodLiteral<"terminate">;
48
+ }, z.core.$strip>, z.ZodObject<{
49
+ kind: z.ZodLiteral<"fail">;
50
+ }, z.core.$strip>, z.ZodObject<{
51
+ kind: z.ZodLiteral<"pause">;
52
+ signal: z.ZodString;
53
+ resumeStep: z.ZodString;
54
+ }, z.core.$strip>]>>;
55
+ }, z.core.$strip>>;
56
+ }, z.core.$strip>;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.agentManifestSchema = exports.MANIFEST_PROTOCOL = void 0;
4
+ const v4_1 = require("zod/v4");
5
+ exports.MANIFEST_PROTOCOL = 1;
6
+ const manifestTransitionSchema = v4_1.z.discriminatedUnion('kind', [
7
+ v4_1.z.object({ kind: v4_1.z.literal('continue'), target: v4_1.z.string().min(1) }),
8
+ v4_1.z.object({ kind: v4_1.z.literal('terminate') }),
9
+ v4_1.z.object({ kind: v4_1.z.literal('fail') }),
10
+ v4_1.z.object({ kind: v4_1.z.literal('pause'), signal: v4_1.z.string().min(1), resumeStep: v4_1.z.string().min(1) }),
11
+ ]);
12
+ const workflowStepManifestSchema = v4_1.z.object({
13
+ timeoutMs: v4_1.z.union([v4_1.z.number().int().positive(), v4_1.z.null()]),
14
+ inputSchema: v4_1.z.union([v4_1.z.record(v4_1.z.string(), v4_1.z.unknown()), v4_1.z.null()]),
15
+ transitions: v4_1.z.array(manifestTransitionSchema),
16
+ });
17
+ exports.agentManifestSchema = v4_1.z.object({
18
+ protocol: v4_1.z.literal(exports.MANIFEST_PROTOCOL),
19
+ name: v4_1.z.string().min(1),
20
+ entry: v4_1.z.string().min(1),
21
+ sdkVersion: v4_1.z.string().min(1),
22
+ artifact: v4_1.z.object({
23
+ sha256: v4_1.z.string().min(1),
24
+ entryFile: v4_1.z.string().min(1),
25
+ }),
26
+ steps: v4_1.z.record(v4_1.z.string(), workflowStepManifestSchema),
27
+ });
@@ -0,0 +1,42 @@
1
+ import type { ZodType } from 'zod/v4';
2
+ import type { AgentExecutionContext } from './context.js';
3
+ import type { Fail, Goto, NextStepDirective, Pause, Retry, Terminate } from './directives.js';
4
+ export interface Step<TIn = unknown, TOut = unknown, TShared extends Record<string, unknown> = Record<string, unknown>> {
5
+ readonly name: string;
6
+ readonly inputSchema?: ZodType<TIn>;
7
+ readonly timeoutMs?: number;
8
+ run(input: TIn, ctx: AgentExecutionContext<TShared>): Promise<StepResult<TOut>>;
9
+ }
10
+ export interface StepResult<TOut = unknown> {
11
+ readonly output: TOut;
12
+ readonly next: NextStepDirective;
13
+ }
14
+ export type Allowed<Next extends readonly string[], Term extends boolean, CanFail extends boolean, PauseTo extends string> = Goto<Next[number]> | (Term extends true ? Terminate : never) | (CanFail extends true ? Fail : never) | ([PauseTo] extends [never] ? never : Pause<PauseTo>) | Retry;
15
+ export interface StepDefinition<TShared extends Record<string, unknown> = Record<string, unknown>> {
16
+ readonly name: string;
17
+ readonly next: readonly string[];
18
+ readonly terminal: boolean;
19
+ readonly canFail: boolean;
20
+ readonly pause?: {
21
+ readonly signal: string;
22
+ readonly resumeStep: string;
23
+ };
24
+ readonly inputSchema?: ZodType<unknown>;
25
+ readonly timeoutMs?: number;
26
+ run(input: unknown, ctx: AgentExecutionContext<TShared>): Promise<NextStepDirective>;
27
+ }
28
+ export declare function defineStep<TIn = unknown, TShared extends Record<string, unknown> = Record<string, unknown>, const Next extends readonly string[] = readonly [], const Term extends boolean = false, const CanFail extends boolean = false, const PauseDecl extends {
29
+ signal: string;
30
+ resumeStep: string;
31
+ } | undefined = undefined>(def: {
32
+ name: string;
33
+ next?: Next;
34
+ terminal?: Term;
35
+ canFail?: CanFail;
36
+ pause?: PauseDecl;
37
+ inputSchema?: ZodType<TIn>;
38
+ timeoutMs?: number;
39
+ run: (input: TIn, ctx: AgentExecutionContext<TShared>) => Promise<Allowed<Next, Term, CanFail, PauseDecl extends {
40
+ resumeStep: infer R extends string;
41
+ } ? R : never>>;
42
+ }): StepDefinition<TShared>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defineStep = defineStep;
4
+ function defineStep(def) {
5
+ return {
6
+ name: def.name,
7
+ next: def.next ?? [],
8
+ terminal: def.terminal ?? false,
9
+ canFail: def.canFail ?? false,
10
+ ...(def.pause ? { pause: def.pause } : {}),
11
+ ...(def.inputSchema ? { inputSchema: def.inputSchema } : {}),
12
+ ...(def.timeoutMs !== undefined ? { timeoutMs: def.timeoutMs } : {}),
13
+ run: def.run,
14
+ };
15
+ }
@@ -0,0 +1,10 @@
1
+ import type { StepDefinition } from './step.js';
2
+ export declare const AGENT_DEFINITION_BRAND: unique symbol;
3
+ export interface AgentDefinition<TInput = unknown, TShared extends Record<string, unknown> = Record<string, unknown>> {
4
+ readonly name: string;
5
+ readonly entry: string;
6
+ readonly steps: Readonly<Record<string, StepDefinition<TShared>>>;
7
+ readonly __inputType?: TInput;
8
+ }
9
+ export declare function isAgentDefinition(val: unknown): val is AgentDefinition;
10
+ export declare function defineAgent<TInput = unknown, TShared extends Record<string, unknown> = Record<string, unknown>>(def: AgentDefinition<TInput, TShared>): AgentDefinition<TInput, TShared>;
@@ -0,0 +1,33 @@
1
+ import { UnknownStepError } from './errors.js';
2
+ export const AGENT_DEFINITION_BRAND = Symbol.for('sapiom.models.definition');
3
+ export function isAgentDefinition(val) {
4
+ if (val === null || typeof val !== 'object')
5
+ return false;
6
+ return val[AGENT_DEFINITION_BRAND] === 1;
7
+ }
8
+ export function defineAgent(def) {
9
+ if (!def.name) {
10
+ throw new Error('Agent definition must have a non-empty name');
11
+ }
12
+ if (!def.entry) {
13
+ throw new Error(`Agent '${def.name}' must declare an entry step`);
14
+ }
15
+ if (!def.steps[def.entry]) {
16
+ throw new UnknownStepError(def.entry);
17
+ }
18
+ for (const [key, step] of Object.entries(def.steps)) {
19
+ if (!step) {
20
+ throw new Error(`Agent '${def.name}' has null/undefined step at key '${key}'`);
21
+ }
22
+ if (step.name !== key) {
23
+ throw new Error(`Agent '${def.name}' step name mismatch at key '${key}': step.name='${step.name}'`);
24
+ }
25
+ }
26
+ Object.defineProperty(def, AGENT_DEFINITION_BRAND, {
27
+ value: 1,
28
+ enumerable: false,
29
+ writable: false,
30
+ configurable: false,
31
+ });
32
+ return def;
33
+ }
@@ -0,0 +1,15 @@
1
+ import { type AgentManifest } from "./manifest.js";
2
+ import type { AgentDefinition } from "./agent.js";
3
+ export declare function buildManifest(def: AgentDefinition, opts: {
4
+ sdkVersion: string;
5
+ artifact: {
6
+ sha256: string;
7
+ entryFile: string;
8
+ };
9
+ }): AgentManifest;
10
+ export interface GraphValidation {
11
+ readonly errors: string[];
12
+ readonly warnings: string[];
13
+ }
14
+ export declare function validateGraph(manifest: AgentManifest): GraphValidation;
15
+ export declare function assertValidGraph(manifest: AgentManifest): string[];