@leitwerk-dev/process-sdk 0.1.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 (199) hide show
  1. package/dist/LICENSE +13 -0
  2. package/dist/browser-ui-extension.d.ts +27 -0
  3. package/dist/browser-ui-extension.d.ts.map +1 -0
  4. package/dist/browser-ui-extension.js +2 -0
  5. package/dist/browser-ui-extension.js.map +1 -0
  6. package/dist/capabilities.d.ts +9 -0
  7. package/dist/capabilities.d.ts.map +1 -0
  8. package/dist/capabilities.js +7 -0
  9. package/dist/capabilities.js.map +1 -0
  10. package/dist/chunk-5RJZ2E7D.js +58 -0
  11. package/dist/chunk-5RJZ2E7D.js.map +1 -0
  12. package/dist/chunk-ITPH65G6.js +197 -0
  13. package/dist/chunk-ITPH65G6.js.map +1 -0
  14. package/dist/chunk-LIIP7FMU.js +48 -0
  15. package/dist/chunk-LIIP7FMU.js.map +1 -0
  16. package/dist/codecs.d.ts +3 -0
  17. package/dist/codecs.d.ts.map +1 -0
  18. package/dist/codecs.js +9 -0
  19. package/dist/codecs.js.map +1 -0
  20. package/dist/commit-message.d.ts +7 -0
  21. package/dist/commit-message.d.ts.map +1 -0
  22. package/dist/commit-message.js +3 -0
  23. package/dist/commit-message.js.map +1 -0
  24. package/dist/core-capabilities.d.ts +393 -0
  25. package/dist/core-capabilities.d.ts.map +1 -0
  26. package/dist/core-capabilities.js +6 -0
  27. package/dist/core-capabilities.js.map +1 -0
  28. package/dist/define-process.d.ts +269 -0
  29. package/dist/define-process.d.ts.map +1 -0
  30. package/dist/define-process.js +1040 -0
  31. package/dist/define-process.js.map +1 -0
  32. package/dist/event-bus.d.ts +3 -0
  33. package/dist/event-bus.d.ts.map +1 -0
  34. package/dist/event-bus.js +31 -0
  35. package/dist/event-bus.js.map +1 -0
  36. package/dist/extension-api-CnlL69Tu.d.ts +1281 -0
  37. package/dist/extension-api.d.ts +425 -0
  38. package/dist/extension-api.d.ts.map +1 -0
  39. package/dist/extension-api.js +5 -0
  40. package/dist/extension-api.js.map +1 -0
  41. package/dist/flow.d.ts +456 -0
  42. package/dist/flow.d.ts.map +1 -0
  43. package/dist/flow.js +1557 -0
  44. package/dist/flow.js.map +1 -0
  45. package/dist/form-contract.d.ts +2 -0
  46. package/dist/form-contract.d.ts.map +1 -0
  47. package/dist/form-contract.js +2 -0
  48. package/dist/form-contract.js.map +1 -0
  49. package/dist/fs-utils.d.ts +3 -0
  50. package/dist/fs-utils.d.ts.map +1 -0
  51. package/dist/fs-utils.js +23 -0
  52. package/dist/fs-utils.js.map +1 -0
  53. package/dist/git-binary.d.ts +11 -0
  54. package/dist/git-binary.d.ts.map +1 -0
  55. package/dist/git-binary.js +38 -0
  56. package/dist/git-binary.js.map +1 -0
  57. package/dist/graph-fragments.d.ts +23 -0
  58. package/dist/graph-fragments.d.ts.map +1 -0
  59. package/dist/graph-fragments.js +57 -0
  60. package/dist/graph-fragments.js.map +1 -0
  61. package/dist/host-capabilities.d.ts +12 -0
  62. package/dist/host-capabilities.d.ts.map +1 -0
  63. package/dist/host-capabilities.js +42 -0
  64. package/dist/host-capabilities.js.map +1 -0
  65. package/dist/index.d.ts +35 -0
  66. package/dist/index.d.ts.map +1 -0
  67. package/dist/index.js +29 -0
  68. package/dist/index.js.map +1 -0
  69. package/dist/launcher-contract.d.ts +2 -0
  70. package/dist/launcher-contract.d.ts.map +1 -0
  71. package/dist/launcher-contract.js +2 -0
  72. package/dist/launcher-contract.js.map +1 -0
  73. package/dist/leaf-outcome-renderer.d.ts +48 -0
  74. package/dist/leaf-outcome-renderer.d.ts.map +1 -0
  75. package/dist/leaf-outcome-renderer.js +69 -0
  76. package/dist/leaf-outcome-renderer.js.map +1 -0
  77. package/dist/leaf-outcomes.d.ts +5 -0
  78. package/dist/leaf-outcomes.d.ts.map +1 -0
  79. package/dist/leaf-outcomes.js +52 -0
  80. package/dist/leaf-outcomes.js.map +1 -0
  81. package/dist/model-provider.d.ts +210 -0
  82. package/dist/model-provider.d.ts.map +1 -0
  83. package/dist/model-provider.js +171 -0
  84. package/dist/model-provider.js.map +1 -0
  85. package/dist/pi-config.d.ts +59 -0
  86. package/dist/pi-config.d.ts.map +1 -0
  87. package/dist/pi-config.js +182 -0
  88. package/dist/pi-config.js.map +1 -0
  89. package/dist/process-definition-internals.d.ts +7 -0
  90. package/dist/process-definition-internals.d.ts.map +1 -0
  91. package/dist/process-definition-internals.js +25 -0
  92. package/dist/process-definition-internals.js.map +1 -0
  93. package/dist/process-flow-view.d.ts +20 -0
  94. package/dist/process-flow-view.d.ts.map +1 -0
  95. package/dist/process-flow-view.js +418 -0
  96. package/dist/process-flow-view.js.map +1 -0
  97. package/dist/process-graph.d.ts +45 -0
  98. package/dist/process-graph.d.ts.map +1 -0
  99. package/dist/process-graph.js +213 -0
  100. package/dist/process-graph.js.map +1 -0
  101. package/dist/process-launcher-builder.d.ts +9 -0
  102. package/dist/process-launcher-builder.d.ts.map +1 -0
  103. package/dist/process-launcher-builder.js +43 -0
  104. package/dist/process-launcher-builder.js.map +1 -0
  105. package/dist/process-watcher-builder.d.ts +10 -0
  106. package/dist/process-watcher-builder.d.ts.map +1 -0
  107. package/dist/process-watcher-builder.js +40 -0
  108. package/dist/process-watcher-builder.js.map +1 -0
  109. package/dist/registry-utils.d.ts +8 -0
  110. package/dist/registry-utils.d.ts.map +1 -0
  111. package/dist/registry-utils.js +18 -0
  112. package/dist/registry-utils.js.map +1 -0
  113. package/dist/repository-git-env.d.ts +8 -0
  114. package/dist/repository-git-env.d.ts.map +1 -0
  115. package/dist/repository-git-env.js +26 -0
  116. package/dist/repository-git-env.js.map +1 -0
  117. package/dist/review-flow.d.ts +10 -0
  118. package/dist/review-flow.d.ts.map +1 -0
  119. package/dist/review-flow.js +44 -0
  120. package/dist/review-flow.js.map +1 -0
  121. package/dist/runtime-internals.d.ts +3 -0
  122. package/dist/runtime-internals.d.ts.map +1 -0
  123. package/dist/runtime-internals.js +3 -0
  124. package/dist/runtime-internals.js.map +1 -0
  125. package/dist/server-events.d.ts +84 -0
  126. package/dist/server-events.d.ts.map +1 -0
  127. package/dist/server-events.js +2 -0
  128. package/dist/server-events.js.map +1 -0
  129. package/dist/server-process-builder.d.ts +11 -0
  130. package/dist/server-process-builder.d.ts.map +1 -0
  131. package/dist/server-process-builder.js +52 -0
  132. package/dist/server-process-builder.js.map +1 -0
  133. package/dist/state-helpers.d.ts +9 -0
  134. package/dist/state-helpers.d.ts.map +1 -0
  135. package/dist/state-helpers.js +20 -0
  136. package/dist/state-helpers.js.map +1 -0
  137. package/dist/subprocess-env.d.ts +12 -0
  138. package/dist/subprocess-env.d.ts.map +1 -0
  139. package/dist/subprocess-env.js +34 -0
  140. package/dist/subprocess-env.js.map +1 -0
  141. package/dist/tool-renderers.d.ts +10 -0
  142. package/dist/tool-renderers.d.ts.map +1 -0
  143. package/dist/tool-renderers.js +63 -0
  144. package/dist/tool-renderers.js.map +1 -0
  145. package/dist/turn-semantics.d.ts +22 -0
  146. package/dist/turn-semantics.d.ts.map +1 -0
  147. package/dist/turn-semantics.js +339 -0
  148. package/dist/turn-semantics.js.map +1 -0
  149. package/dist/types.d.ts +295 -0
  150. package/dist/types.d.ts.map +1 -0
  151. package/dist/types.js +12 -0
  152. package/dist/types.js.map +1 -0
  153. package/dist/ui-process-builder.d.ts +9 -0
  154. package/dist/ui-process-builder.d.ts.map +1 -0
  155. package/dist/ui-process-builder.js +30 -0
  156. package/dist/ui-process-builder.js.map +1 -0
  157. package/dist/worker-process-builder.d.ts +10 -0
  158. package/dist/worker-process-builder.d.ts.map +1 -0
  159. package/dist/worker-process-builder.js +22 -0
  160. package/dist/worker-process-builder.js.map +1 -0
  161. package/package.json +72 -0
  162. package/src/browser-ui-extension.ts +32 -0
  163. package/src/capabilities.ts +16 -0
  164. package/src/codecs.ts +10 -0
  165. package/src/commit-message.ts +9 -0
  166. package/src/core-capabilities.ts +526 -0
  167. package/src/define-process.ts +1879 -0
  168. package/src/event-bus.ts +35 -0
  169. package/src/extension-api.ts +600 -0
  170. package/src/flow.ts +2268 -0
  171. package/src/form-contract.ts +11 -0
  172. package/src/fs-utils.ts +28 -0
  173. package/src/git-binary.ts +40 -0
  174. package/src/graph-fragments.ts +110 -0
  175. package/src/host-capabilities.ts +60 -0
  176. package/src/index.ts +468 -0
  177. package/src/launcher-contract.ts +8 -0
  178. package/src/leaf-outcome-renderer.ts +112 -0
  179. package/src/leaf-outcomes.ts +74 -0
  180. package/src/model-provider.ts +461 -0
  181. package/src/pi-config.ts +268 -0
  182. package/src/process-definition-internals.ts +46 -0
  183. package/src/process-flow-view.ts +479 -0
  184. package/src/process-graph.ts +309 -0
  185. package/src/process-launcher-builder.ts +59 -0
  186. package/src/process-watcher-builder.ts +72 -0
  187. package/src/registry-utils.ts +31 -0
  188. package/src/repository-git-env.ts +35 -0
  189. package/src/review-flow.ts +66 -0
  190. package/src/runtime-internals.ts +7 -0
  191. package/src/server-events.ts +105 -0
  192. package/src/server-process-builder.ts +90 -0
  193. package/src/state-helpers.ts +36 -0
  194. package/src/subprocess-env.ts +48 -0
  195. package/src/tool-renderers.ts +92 -0
  196. package/src/turn-semantics.ts +554 -0
  197. package/src/types.ts +391 -0
  198. package/src/ui-process-builder.ts +46 -0
  199. package/src/worker-process-builder.ts +33 -0
@@ -0,0 +1,11 @@
1
+ export type {
2
+ ActionFormDefinition,
3
+ ActionFormDefinition as FormDefinition,
4
+ ActionFormFieldDefinition,
5
+ ActionFormFieldDefinition as FormFieldDefinition,
6
+ ActionFormFieldKind,
7
+ FormFieldKind,
8
+ FormFieldOptionDefinition,
9
+ FormFieldPublishDefinition,
10
+ FormFieldStateDefinition,
11
+ } from "@leitwerk-dev/protocol/form-contract";
@@ -0,0 +1,28 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { mkdir, rename, unlink, writeFile } from "node:fs/promises";
3
+ import path from "node:path";
4
+
5
+ export async function atomicWriteUtf8(filePath: string, content: string): Promise<void> {
6
+ const dir = path.dirname(filePath);
7
+ await mkdir(dir, { recursive: true });
8
+ const tempPath = path.join(
9
+ dir,
10
+ `.${path.basename(filePath)}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`,
11
+ );
12
+ try {
13
+ await writeFile(tempPath, content, "utf8");
14
+ await rename(tempPath, filePath);
15
+ } catch (error) {
16
+ await unlink(tempPath).catch(() => {});
17
+ throw error;
18
+ }
19
+ }
20
+
21
+ export function isEnoent(error: unknown): boolean {
22
+ return (
23
+ typeof error === "object" &&
24
+ error !== null &&
25
+ "code" in error &&
26
+ (error as { code?: unknown }).code === "ENOENT"
27
+ );
28
+ }
@@ -0,0 +1,40 @@
1
+ import { existsSync } from "node:fs";
2
+ import { delimiter, join } from "node:path";
3
+
4
+ // Resolving a bare "git" through child_process forces Node to stat every PATH
5
+ // entry on each spawn. On developer machines with long PATHs that is ~60ms per
6
+ // call, which dominates git-heavy code paths and tests that fan out into dozens
7
+ // of subprocesses. Resolve the absolute path once and reuse it so each spawn
8
+ // skips the PATH search.
9
+ let cachedGitBinary: string | null = null;
10
+
11
+ /**
12
+ * Resolve the absolute path to the `git` executable once and cache it.
13
+ *
14
+ * POSIX-only by design (matching the leitwerk platform target). Falls back
15
+ * to the bare `"git"` name when no PATH entry matches so spawning still works
16
+ * and surfaces a clear error from the child process itself.
17
+ */
18
+ export function resolveGitBinary(): string {
19
+ if (cachedGitBinary) {
20
+ return cachedGitBinary;
21
+ }
22
+ const pathValue = process.env.PATH ?? "";
23
+ for (const dir of pathValue.split(delimiter)) {
24
+ if (!dir) {
25
+ continue;
26
+ }
27
+ const candidate = join(dir, "git");
28
+ if (existsSync(candidate)) {
29
+ cachedGitBinary = candidate;
30
+ return candidate;
31
+ }
32
+ }
33
+ cachedGitBinary = "git";
34
+ return cachedGitBinary;
35
+ }
36
+
37
+ /** Reset the cached resolution. Intended for tests that mutate PATH. */
38
+ export function resetGitBinaryCache(): void {
39
+ cachedGitBinary = null;
40
+ }
@@ -0,0 +1,110 @@
1
+ import type { InputKind, InputSource, ProcessInputTarget } from "@leitwerk-dev/domain";
2
+ import type {
3
+ ProcessActionEffect,
4
+ ProcessActionExecution,
5
+ ProcessEffectPlan,
6
+ ProcessHumanTurnActionSpec,
7
+ } from "./define-process.js";
8
+
9
+ type MaybePromise<T> = T | Promise<T>;
10
+ type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
11
+
12
+ function appendQueuedInstruction<TState>(input: {
13
+ plan: ProcessEffectPlan<TState> | undefined;
14
+ bodyMarkdown: string | null | undefined;
15
+ queueTarget?: ProcessInputTarget;
16
+ queueSource?: InputSource;
17
+ queueKind?: InputKind;
18
+ }) {
19
+ if (input.bodyMarkdown == null) {
20
+ return input.plan;
21
+ }
22
+ if (input.bodyMarkdown.trim() === "") {
23
+ throw new Error("Queued instruction bodyMarkdown must not be empty");
24
+ }
25
+ return {
26
+ ...(input.plan ?? {}),
27
+ queueInput: [
28
+ ...(input.plan?.queueInput ?? []),
29
+ {
30
+ source: input.queueSource ?? "action_prompt",
31
+ kind: input.queueKind ?? "instruction",
32
+ ...(input.queueTarget ? { target: input.queueTarget } : {}),
33
+ bodyMarkdown: input.bodyMarkdown,
34
+ },
35
+ ],
36
+ };
37
+ }
38
+
39
+ export type QueuedInstructionActionSpec<TParams = unknown, TState = unknown> = DistributiveOmit<
40
+ ProcessHumanTurnActionSpec<TParams, TState>,
41
+ "effect"
42
+ > & {
43
+ queueTarget?: ProcessInputTarget;
44
+ queueSource?: InputSource;
45
+ queueKind?: InputKind;
46
+ resolveBodyMarkdown(
47
+ input: ProcessActionExecution<TParams, TState>,
48
+ ): MaybePromise<string | null | undefined>;
49
+ effect?: ProcessActionEffect<TParams, TState>;
50
+ };
51
+
52
+ export function queuedInstructionAction<TParams = unknown, TState = unknown>(
53
+ spec: QueuedInstructionActionSpec<TParams, TState>,
54
+ ): ProcessHumanTurnActionSpec<TParams, TState> {
55
+ const { queueTarget, queueSource, queueKind, resolveBodyMarkdown, effect, ...rest } = spec;
56
+ return {
57
+ ...rest,
58
+ async effect(input) {
59
+ const bodyMarkdown = await resolveBodyMarkdown(input);
60
+ const plan = effect ? await effect(input) : undefined;
61
+ return appendQueuedInstruction({
62
+ plan,
63
+ bodyMarkdown,
64
+ queueTarget,
65
+ queueSource,
66
+ queueKind,
67
+ });
68
+ },
69
+ } as ProcessHumanTurnActionSpec<TParams, TState>;
70
+ }
71
+
72
+ export type RevisionActionSpec<TParams = unknown, TState = unknown> = DistributiveOmit<
73
+ QueuedInstructionActionSpec<TParams, TState>,
74
+ "resolveBodyMarkdown"
75
+ > & {
76
+ messageFieldId?: string;
77
+ missingMessageError?: string;
78
+ };
79
+
80
+ export function revisionAction<TParams = unknown, TState = unknown>(
81
+ spec: RevisionActionSpec<TParams, TState>,
82
+ ): ProcessHumanTurnActionSpec<TParams, TState> {
83
+ const { messageFieldId = "message", missingMessageError, ...rest } = spec;
84
+ return queuedInstructionAction({
85
+ ...rest,
86
+ resolveBodyMarkdown({ input }) {
87
+ const message = typeof input[messageFieldId] === "string" ? input[messageFieldId].trim() : "";
88
+ if (!message) {
89
+ throw new Error(missingMessageError ?? `${messageFieldId} is required`);
90
+ }
91
+ return message;
92
+ },
93
+ });
94
+ }
95
+
96
+ export type AcceptedReviewHandoffActionSpec<TParams = unknown, TState = unknown> = DistributiveOmit<
97
+ QueuedInstructionActionSpec<TParams, TState>,
98
+ "queueTarget"
99
+ > & {
100
+ queueTarget?: ProcessInputTarget;
101
+ };
102
+
103
+ export function acceptedReviewHandoffAction<TParams = unknown, TState = unknown>(
104
+ spec: AcceptedReviewHandoffActionSpec<TParams, TState>,
105
+ ): ProcessHumanTurnActionSpec<TParams, TState> {
106
+ return queuedInstructionAction({
107
+ ...spec,
108
+ queueTarget: spec.queueTarget ?? { semanticRef: "currentPrimaryPathLeaf" },
109
+ });
110
+ }
@@ -0,0 +1,60 @@
1
+ import type { CapabilityToken } from "./capabilities.js";
2
+
3
+ export interface ProvidedCapability<T = unknown> {
4
+ token: CapabilityToken<T>;
5
+ value: T;
6
+ }
7
+
8
+ export interface CapabilityAccessor {
9
+ provide<T>(token: CapabilityToken<T>, value: T): void;
10
+ get<T>(token: CapabilityToken<T>): T | T[] | undefined;
11
+ require<T>(token: CapabilityToken<T>): T | T[];
12
+ }
13
+
14
+ export function createCapabilityAccessor(
15
+ provided: readonly ProvidedCapability[] = [],
16
+ ): CapabilityAccessor {
17
+ const singleCapabilities = new Map<string, unknown>();
18
+ const multiCapabilities = new Map<string, unknown[]>();
19
+
20
+ for (const entry of provided) {
21
+ if (entry.token.cardinality === "multi") {
22
+ const list = multiCapabilities.get(entry.token.id) ?? [];
23
+ list.push(entry.value);
24
+ multiCapabilities.set(entry.token.id, list);
25
+ continue;
26
+ }
27
+ singleCapabilities.set(entry.token.id, entry.value);
28
+ }
29
+
30
+ return {
31
+ provide(token, value) {
32
+ if (token.cardinality === "multi") {
33
+ const list = multiCapabilities.get(token.id) ?? [];
34
+ list.push(value);
35
+ multiCapabilities.set(token.id, list);
36
+ return;
37
+ }
38
+ if (singleCapabilities.has(token.id)) {
39
+ throw new Error(`Capability '${token.id}' is already provided`);
40
+ }
41
+ singleCapabilities.set(token.id, value);
42
+ },
43
+ get<T>(token: CapabilityToken<T>): T | T[] | undefined {
44
+ if (token.cardinality === "multi") {
45
+ return [...(multiCapabilities.get(token.id) ?? [])] as T[];
46
+ }
47
+ return singleCapabilities.get(token.id) as T | undefined;
48
+ },
49
+ require<T>(token: CapabilityToken<T>): T | T[] {
50
+ const value = this.get(token);
51
+ if (
52
+ value === undefined ||
53
+ (Array.isArray(value) && token.cardinality === "multi" && value.length === 0)
54
+ ) {
55
+ throw new Error(`Required capability '${token.id}' is not available`);
56
+ }
57
+ return value;
58
+ },
59
+ };
60
+ }