@leitwerk-dev/showcase-processes 0.1.9 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +14 -0
  2. package/dist/file-external.d.ts +14 -1
  3. package/dist/file-external.d.ts.map +1 -1
  4. package/dist/filesystem-watcher.d.ts +14 -4
  5. package/dist/filesystem-watcher.d.ts.map +1 -1
  6. package/dist/index.d.ts +2 -18
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +1225 -58
  9. package/dist/index.js.map +1 -1
  10. package/dist/poem-leaf-outcome.d.ts +31 -0
  11. package/dist/poem-leaf-outcome.d.ts.map +1 -1
  12. package/dist/process-definition.d.ts +21 -8
  13. package/dist/process-definition.d.ts.map +1 -1
  14. package/dist/turns/external-complete.d.ts +5 -0
  15. package/dist/turns/external-complete.d.ts.map +1 -1
  16. package/dist/turns/poem-creator.d.ts +12 -0
  17. package/dist/turns/poem-creator.d.ts.map +1 -1
  18. package/dist/turns/run-single-prompt.d.ts +6 -0
  19. package/dist/turns/run-single-prompt.d.ts.map +1 -1
  20. package/dist/ui/assets/leaf-outcome-element.js +12 -0
  21. package/dist/ui/assets/leaf-outcome-element.js.map +1 -1
  22. package/dist/ui/assets/poem-leaf-outcome-element.js +12 -0
  23. package/dist/ui/assets/poem-leaf-outcome-element.js.map +1 -1
  24. package/package.json +5 -5
  25. package/src/file-external.ts +13 -2
  26. package/src/filesystem-watcher.ts +33 -55
  27. package/src/index.ts +17 -20
  28. package/src/poem-leaf-outcome.ts +34 -21
  29. package/src/process-definition.ts +83 -101
  30. package/src/turns/external-complete.ts +5 -0
  31. package/src/turns/poem-creator.ts +12 -0
  32. package/src/turns/run-single-prompt.ts +6 -0
  33. package/dist/file-external.js +0 -198
  34. package/dist/file-external.js.map +0 -1
  35. package/dist/filesystem-watcher.js +0 -109
  36. package/dist/filesystem-watcher.js.map +0 -1
  37. package/dist/poem-leaf-outcome.js +0 -110
  38. package/dist/poem-leaf-outcome.js.map +0 -1
  39. package/dist/process-definition.js +0 -813
  40. package/dist/process-definition.js.map +0 -1
  41. package/dist/turns/external-complete.js +0 -6
  42. package/dist/turns/external-complete.js.map +0 -1
  43. package/dist/turns/poem-creator.js +0 -72
  44. package/dist/turns/poem-creator.js.map +0 -1
  45. package/dist/turns/run-single-prompt.js +0 -57
  46. package/dist/turns/run-single-prompt.js.map +0 -1
  47. package/dist/ui/leaf-outcome-element.js +0 -79
  48. package/dist/ui/leaf-outcome-element.js.map +0 -1
  49. package/dist/ui/poem-leaf-outcome-element.js +0 -171
  50. package/dist/ui/poem-leaf-outcome-element.js.map +0 -1
@@ -4,11 +4,13 @@ import {
4
4
  type AutomaticOutcomeBuilder,
5
5
  type Codec,
6
6
  createEmptyStructuralProcessState,
7
+ type ExtensionProcessDefinition,
7
8
  type FormDefinition,
8
9
  flow,
9
10
  type HumanFlowBuilder,
10
11
  parseStructuralProcessState,
11
12
  type StructuralProcessState,
13
+ structuralStateCodec,
12
14
  } from "@leitwerk-dev/process-sdk";
13
15
  import { fileExternal } from "./file-external.js";
14
16
  import { filesystemWatcherSource } from "./filesystem-watcher.js";
@@ -33,13 +35,19 @@ import {
33
35
  buildSinglePromptWithToolInstruction,
34
36
  } from "./turns/run-single-prompt.js";
35
37
 
38
+ /** @internal */
36
39
  interface PromptProcessParams {
40
+ /** @internal */
37
41
  prompt: string;
38
42
  }
39
43
 
44
+ /** @internal */
40
45
  interface PoemCreatorState extends StructuralProcessState {
46
+ /** @internal */
41
47
  latestReviewMarkdown: string | null;
48
+ /** @internal */
42
49
  latestReviewSummary: string | null;
50
+ /** @internal */
43
51
  latestReviewOutcome: "no_issues" | "leave_feedback" | null;
44
52
  }
45
53
 
@@ -58,15 +66,6 @@ const promptProcessParamsCodec: Codec<PromptProcessParams> = {
58
66
  },
59
67
  };
60
68
 
61
- const structuralStateCodec: Codec<StructuralProcessState> = {
62
- parse(value) {
63
- return parseStructuralProcessState(value);
64
- },
65
- serialize(value) {
66
- return value;
67
- },
68
- };
69
-
70
69
  const poemCreatorStateCodec: Codec<PoemCreatorState> = {
71
70
  parse(value) {
72
71
  const structural = parseStructuralProcessState(value);
@@ -114,6 +113,30 @@ function buildPromptTitleSourceFields(prompt: string, label = "Prompt") {
114
113
  return [{ label, value: prompt }] as const;
115
114
  }
116
115
 
116
+ function promptLaunchResolution(options: {
117
+ processId: string;
118
+ startTurnId: string;
119
+ defaultPrompt?: () => string;
120
+ titleLabel?: string;
121
+ }) {
122
+ return {
123
+ resolveDefaults: () => ({ prompt: options.defaultPrompt?.() ?? "" }),
124
+ resolveLaunchConfig(input: Record<string, unknown>) {
125
+ const validated = validateLaunchInput(input, options.defaultPrompt?.());
126
+ if (!validated.ok) return validated;
127
+ return {
128
+ ok: true as const,
129
+ launchConfig: {
130
+ processId: options.processId,
131
+ params: { prompt: validated.prompt },
132
+ titleSourceFields: buildPromptTitleSourceFields(validated.prompt, options.titleLabel),
133
+ startTurnId: options.startTurnId,
134
+ },
135
+ };
136
+ },
137
+ };
138
+ }
139
+
117
140
  function clearReviewRefs(state: PoemCreatorState["semanticEntryRefs"]) {
118
141
  return {
119
142
  ...state,
@@ -281,7 +304,11 @@ const poemReviewChangesForm: FormDefinition = {
281
304
  submitLabel: "Request review changes",
282
305
  };
283
306
 
284
- export const singlePromptProcess = flow
307
+ /** @internal */
308
+ export const singlePromptProcess: ExtensionProcessDefinition<
309
+ PromptProcessParams,
310
+ StructuralProcessState
311
+ > = flow
285
312
  .process<PromptProcessParams, StructuralProcessState>("single_prompt_process")
286
313
  .displayName("Single Prompt")
287
314
  .entry("run_single_prompt")
@@ -293,7 +320,7 @@ export const singlePromptProcess = flow
293
320
  .turn(
294
321
  flow
295
322
  .llm<PromptProcessParams, StructuralProcessState>("run_single_prompt")
296
- .description("Run a single operator-provided prompt and finish on turn end")
323
+ .description("Run Prompt")
297
324
  .tools("read", "bash", "edit", "write")
298
325
  .prompt(async (ctx) => buildSinglePromptInstruction(ctx.params.prompt))
299
326
  .end("completed")
@@ -333,31 +360,19 @@ export const singlePromptProcess = flow
333
360
  ],
334
361
  submitLabel: "Run Prompt",
335
362
  },
336
- resolveDefaults() {
337
- return {
338
- prompt: "",
339
- };
340
- },
341
- resolveLaunchConfig(input) {
342
- const validated = validateLaunchInput(input);
343
- if (!validated.ok) {
344
- return validated;
345
- }
346
- return {
347
- ok: true,
348
- launchConfig: {
349
- processId: "single_prompt_process",
350
- params: { prompt: validated.prompt },
351
- titleSourceFields: buildPromptTitleSourceFields(validated.prompt),
352
- startTurnId: "run_single_prompt",
353
- },
354
- };
355
- },
363
+ ...promptLaunchResolution({
364
+ processId: "single_prompt_process",
365
+ startTurnId: "run_single_prompt",
366
+ }),
356
367
  },
357
368
  })
358
369
  .define();
359
370
 
360
- export const singlePromptWithToolProcess = flow
371
+ /** @internal */
372
+ export const singlePromptWithToolProcess: ExtensionProcessDefinition<
373
+ PromptProcessParams,
374
+ StructuralProcessState
375
+ > = flow
361
376
  .process<PromptProcessParams, StructuralProcessState>("single_prompt_with_tool_process")
362
377
  .displayName("Single Prompt + Done Tool")
363
378
  .entry("run_single_prompt_with_tool")
@@ -369,7 +384,7 @@ export const singlePromptWithToolProcess = flow
369
384
  .turn(
370
385
  flow
371
386
  .llm<PromptProcessParams, StructuralProcessState>("run_single_prompt_with_tool")
372
- .description("Run a single operator-provided prompt and require the done tool")
387
+ .description("Run Prompt")
373
388
  .tools("read", "bash", "edit", "write")
374
389
  .prompt(async (ctx) => buildSinglePromptWithToolInstruction(ctx.params.prompt))
375
390
  .outcomeTool("done", (tool) =>
@@ -413,26 +428,10 @@ export const singlePromptWithToolProcess = flow
413
428
  ],
414
429
  submitLabel: "Run Prompt + Tool",
415
430
  },
416
- resolveDefaults() {
417
- return {
418
- prompt: "",
419
- };
420
- },
421
- resolveLaunchConfig(input) {
422
- const validated = validateLaunchInput(input);
423
- if (!validated.ok) {
424
- return validated;
425
- }
426
- return {
427
- ok: true,
428
- launchConfig: {
429
- processId: "single_prompt_with_tool_process",
430
- params: { prompt: validated.prompt },
431
- titleSourceFields: buildPromptTitleSourceFields(validated.prompt),
432
- startTurnId: "run_single_prompt_with_tool",
433
- },
434
- };
435
- },
431
+ ...promptLaunchResolution({
432
+ processId: "single_prompt_with_tool_process",
433
+ startTurnId: "run_single_prompt_with_tool",
434
+ }),
436
435
  },
437
436
  })
438
437
  .define();
@@ -471,7 +470,7 @@ function defineK8sSmokeProcess(args: {
471
470
  defaultPrompt: string;
472
471
  titleLabel: string;
473
472
  run: SmokeRunFn;
474
- }) {
473
+ }): ExtensionProcessDefinition<PromptProcessParams, StructuralProcessState> {
475
474
  const promptField = args.promptField;
476
475
  const resolveLaunchConfig = promptField
477
476
  ? (input: Record<string, unknown>) => {
@@ -560,6 +559,7 @@ function defineK8sSmokeProcess(args: {
560
559
  .define();
561
560
  }
562
561
 
562
+ /** @internal */
563
563
  export const k8sSmokeProcess = defineK8sSmokeProcess({
564
564
  processId: "k8s_smoke_process",
565
565
  displayName: "Kubernetes Smoke",
@@ -586,6 +586,7 @@ export const k8sSmokeProcess = defineK8sSmokeProcess({
586
586
  }),
587
587
  });
588
588
 
589
+ /** @internal */
589
590
  export const k8sSmokeSpecializedProcess = defineK8sSmokeProcess({
590
591
  processId: "k8s_smoke_specialized_process",
591
592
  displayName: "Kubernetes Specialized Smoke",
@@ -610,6 +611,7 @@ export const k8sSmokeSpecializedProcess = defineK8sSmokeProcess({
610
611
  },
611
612
  });
612
613
 
614
+ /** @internal */
613
615
  export const k8sSmokeLongProcess = defineK8sSmokeProcess({
614
616
  processId: "k8s_smoke_long_process",
615
617
  displayName: "Kubernetes Long Smoke",
@@ -632,7 +634,11 @@ export const k8sSmokeLongProcess = defineK8sSmokeProcess({
632
634
  },
633
635
  });
634
636
 
635
- export const singlePromptExternalCompleteProcess = flow
637
+ /** @internal */
638
+ export const singlePromptExternalCompleteProcess: ExtensionProcessDefinition<
639
+ PromptProcessParams,
640
+ StructuralProcessState
641
+ > = flow
636
642
  .process<PromptProcessParams, StructuralProcessState>("single_prompt_external_complete_process")
637
643
  .displayName("Single Prompt + External Complete")
638
644
  .entry("run_single_prompt")
@@ -644,7 +650,7 @@ export const singlePromptExternalCompleteProcess = flow
644
650
  .turn(
645
651
  flow
646
652
  .llm<PromptProcessParams, StructuralProcessState>("run_single_prompt")
647
- .description("Run a single operator-provided prompt and finish on turn end")
653
+ .description("Run Prompt")
648
654
  .tools("read", "bash", "edit", "write")
649
655
  .prompt(async (ctx) => buildSinglePromptInstruction(ctx.params.prompt))
650
656
  .end("completed")
@@ -697,33 +703,17 @@ export const singlePromptExternalCompleteProcess = flow
697
703
  ],
698
704
  submitLabel: "Run Prompt",
699
705
  },
700
- resolveDefaults() {
701
- return {
702
- prompt: "",
703
- };
704
- },
705
- resolveLaunchConfig(input) {
706
- const validated = validateLaunchInput(input);
707
- if (!validated.ok) {
708
- return validated;
709
- }
710
- return {
711
- ok: true,
712
- launchConfig: {
713
- processId: "single_prompt_external_complete_process",
714
- params: { prompt: validated.prompt },
715
- titleSourceFields: buildPromptTitleSourceFields(validated.prompt),
716
- startTurnId: "run_single_prompt",
717
- },
718
- };
719
- },
706
+ ...promptLaunchResolution({
707
+ processId: "single_prompt_external_complete_process",
708
+ startTurnId: "run_single_prompt",
709
+ }),
720
710
  },
721
711
  })
722
712
  .define();
723
713
 
724
714
  const poemDraftReviewSpec = flow
725
715
  .human<PromptProcessParams, PoemCreatorState>(poemTurnIds.poemReview)
726
- .description("Human review of the drafted poem on the primary branch")
716
+ .description("Review")
727
717
  .reviewProduct("poem-draft")
728
718
  .notesFields([
729
719
  {
@@ -786,7 +776,7 @@ const poemDraftReviewSpec = flow
786
776
 
787
777
  const poemReviewFeedbackSpec = flow
788
778
  .human<PromptProcessParams, PoemCreatorState>(poemTurnIds.poemReviewFeedback)
789
- .description("Human review of the LLM review output")
779
+ .description("Review Feedback")
790
780
  .reviewProduct("message")
791
781
  .notesFields([
792
782
  {
@@ -833,7 +823,7 @@ const poemReviewFeedbackSpec = flow
833
823
  })),
834
824
  ).definition;
835
825
 
836
- export const poemCreatorProcess = flow
826
+ const poemCreatorProcessDefinition = flow
837
827
  .process<PromptProcessParams, PoemCreatorState>("poem_creator_process")
838
828
  .displayName("Poem Creator")
839
829
  .entry(poemTurnIds.draftPoem)
@@ -850,7 +840,7 @@ export const poemCreatorProcess = flow
850
840
  .turn(
851
841
  flow
852
842
  .llm<PromptProcessParams, PoemCreatorState>(poemTurnIds.draftPoem)
853
- .description("Draft poem")
843
+ .description("Draft Poem")
854
844
  .fullPrimary()
855
845
  .continueFromPrimaryLeaf()
856
846
  .optionalConsume("message")
@@ -875,7 +865,7 @@ export const poemCreatorProcess = flow
875
865
  .turn(
876
866
  flow
877
867
  .llm<PromptProcessParams, PoemCreatorState>(poemTurnIds.reviewPoemDraft)
878
- .description("Review poem")
868
+ .description("Assess Poem")
879
869
  .rootBranchReview()
880
870
  .continueFromReviewBranch()
881
871
  .consume("poem-draft")
@@ -894,6 +884,7 @@ export const poemCreatorProcess = flow
894
884
  .outcomeTool("no_issues", (tool) =>
895
885
  tool
896
886
  .description("The poem is ready to publish")
887
+ .resultSummary("summary")
897
888
  .requiredString("summary", "Short confirmation summary")
898
889
  .markdown("review", {
899
890
  description: "Concise review opinion confirming the poem is ready",
@@ -912,6 +903,7 @@ export const poemCreatorProcess = flow
912
903
  .outcomeTool("leave_feedback", (tool) =>
913
904
  tool
914
905
  .description("Leave concise feedback for the human reviewer")
906
+ .resultSummary("summary")
915
907
  .requiredString("summary", "Short summary of the feedback")
916
908
  .markdown("message", {
917
909
  description: "Concise plain-text feedback describing what to improve",
@@ -966,26 +958,12 @@ export const poemCreatorProcess = flow
966
958
  ],
967
959
  submitLabel: "Create Poem",
968
960
  },
969
- resolveDefaults() {
970
- return {
971
- prompt: buildDefaultPoemPrompt(),
972
- };
973
- },
974
- resolveLaunchConfig(input) {
975
- const validated = validateLaunchInput(input, buildDefaultPoemPrompt());
976
- if (!validated.ok) {
977
- return validated;
978
- }
979
- return {
980
- ok: true,
981
- launchConfig: {
982
- processId: "poem_creator_process",
983
- params: { prompt: validated.prompt },
984
- titleSourceFields: buildPromptTitleSourceFields(validated.prompt, "Poem Prompt"),
985
- startTurnId: poemTurnIds.draftPoem,
986
- },
987
- };
988
- },
961
+ ...promptLaunchResolution({
962
+ processId: "poem_creator_process",
963
+ startTurnId: poemTurnIds.draftPoem,
964
+ defaultPrompt: buildDefaultPoemPrompt,
965
+ titleLabel: "Poem Prompt",
966
+ }),
989
967
  },
990
968
  })
991
969
  .watcher({
@@ -1005,3 +983,7 @@ export const poemCreatorProcess = flow
1005
983
  },
1006
984
  })
1007
985
  .define();
986
+
987
+ /** @internal */
988
+ export const poemCreatorProcess: ExtensionProcessDefinition<PromptProcessParams, PoemCreatorState> =
989
+ poemCreatorProcessDefinition;
@@ -1,7 +1,12 @@
1
+ /** @internal */
1
2
  export const externalCompleteActionId = "complete_external_prompt";
3
+ /** @internal */
2
4
  export const externalPromptCompletionTurnId = "await_external_prompt_completion";
5
+ /** @internal */
3
6
  export const externalPromptCompletionTurnDescription =
4
7
  "Wait for an external completion trigger after the single prompt finishes";
8
+ /** @internal */
5
9
  export const externalPromptCompletionTriggerLabel = "Configured prompt-complete file";
10
+ /** @internal */
6
11
  export const externalPromptCompletionTriggerDescription =
7
12
  "Write any content to the configured prompt-complete trigger file (default: /tmp/complete-prompt) to complete the process. The file is removed after a successful completion.";
@@ -1,11 +1,18 @@
1
1
  import type { ToolCallRendererDefinition } from "@leitwerk-dev/process-sdk";
2
2
 
3
+ /** @internal */
3
4
  export const poemCreatorActionIds = {
5
+ /** @internal */
4
6
  completePoem: "complete_poem",
7
+ /** @internal */
5
8
  requestRevision: "request_poem_revision",
9
+ /** @internal */
6
10
  runAutoReview: "run_poem_auto_review",
11
+ /** @internal */
7
12
  acceptReview: "accept_poem_review",
13
+ /** @internal */
8
14
  requestReviewChanges: "request_poem_review_changes",
15
+ /** @internal */
9
16
  dismissReview: "dismiss_poem_review",
10
17
  } as const;
11
18
 
@@ -19,11 +26,13 @@ function formatGermanyDay(now: Date): string {
19
26
  }).format(now);
20
27
  }
21
28
 
29
+ /** @internal */
22
30
  export function buildDefaultPoemPrompt(now = new Date()): string {
23
31
  const germanyDay = formatGermanyDay(now);
24
32
  return `Write a short poem in Markdown that is either inspired by ${germanyDay} in Germany or by the craft of building cloud software. Keep it vivid, warm, and concise.`;
25
33
  }
26
34
 
35
+ /** @internal */
27
36
  export function buildDraftPoemInstruction(promptText: string): string {
28
37
  return `Write a short poem in Markdown that clearly responds to the operator's prompt.
29
38
 
@@ -39,6 +48,7 @@ Requirements:
39
48
  - only the primary branch should write or revise the poem directly`;
40
49
  }
41
50
 
51
+ /** @internal */
42
52
  export function buildRevisePoemInstruction(revisionGuidance: string): string {
43
53
  return `Continue from the current poem on this branch and revise it according to this request:
44
54
 
@@ -47,6 +57,7 @@ ${revisionGuidance}
47
57
  Preserve the parts of the current poem that the request does not ask you to change. Return the complete revised poem in the same Markdown format.`;
48
58
  }
49
59
 
60
+ /** @internal */
50
61
  export function buildReviewPoemInstruction(promptText: string, poemDraftMarkdown: string): string {
51
62
  return `Review the latest poem draft against the operator's prompt.
52
63
 
@@ -64,6 +75,7 @@ If the poem is strong, relevant, and well-formed Markdown, call no_issues with a
64
75
  If the poem misses the prompt, needs revision, or is awkwardly structured, call leave_feedback with a brief summary and concise plain-text feedback.`;
65
76
  }
66
77
 
78
+ /** @internal */
67
79
  export const leaveFeedbackToolRenderer: ToolCallRendererDefinition = {
68
80
  toolName: "leave_feedback",
69
81
  title: "Feedback",
@@ -4,12 +4,14 @@ import {
4
4
  REQUIRED_MARKDOWN_RESULT_TURN_RESULT,
5
5
  } from "@leitwerk-dev/process-sdk";
6
6
 
7
+ /** @internal */
7
8
  export function buildSinglePromptInstruction(promptText: string): string {
8
9
  return `Run the following one-shot operator prompt exactly once.
9
10
 
10
11
  ${promptText}`;
11
12
  }
12
13
 
14
+ /** @internal */
13
15
  export function buildSinglePromptWithToolInstruction(promptText: string): string {
14
16
  return `Run the following one-shot operator prompt exactly once.
15
17
 
@@ -18,6 +20,7 @@ ${promptText}
18
20
  When you are done, call the done tool with a short summary of the result.`;
19
21
  }
20
22
 
23
+ /** @internal */
21
24
  export function createRunSinglePromptTurn(promptText: string): LlmTurnDefinition<"completed"> {
22
25
  return llmTurn({
23
26
  description: "Run a single operator-provided prompt and finish on turn end",
@@ -36,6 +39,7 @@ export function createRunSinglePromptTurn(promptText: string): LlmTurnDefinition
36
39
  });
37
40
  }
38
41
 
42
+ /** @internal */
39
43
  export function createRunSinglePromptWithToolTurn(promptText: string): LlmTurnDefinition<"done"> {
40
44
  return llmTurn({
41
45
  description: "Run a single operator-provided prompt and require the done tool",
@@ -61,10 +65,12 @@ export function createRunSinglePromptWithToolTurn(promptText: string): LlmTurnDe
61
65
  });
62
66
  }
63
67
 
68
+ /** @internal */
64
69
  export const runSinglePrompt = createRunSinglePromptTurn(
65
70
  "Run the operator-provided single prompt.",
66
71
  );
67
72
 
73
+ /** @internal */
68
74
  export const runSinglePromptWithTool = createRunSinglePromptWithToolTurn(
69
75
  "Run the operator-provided single prompt.",
70
76
  );
@@ -1,198 +0,0 @@
1
- import { consumeTriggerFile, createPollLoop, emptyPollResult, parseDurationMs, readTriggerFile, } from "@leitwerk-dev/watcher-utils";
2
- export const FILE_EXTERNAL_PRESENCE_KIND = "@leitwerk-dev/showcase-processes.file.presence";
3
- export const FILE_EXTERNAL_INSTRUCTION_KIND = "@leitwerk-dev/showcase-processes.file.instruction";
4
- function normalizeConfig(input) {
5
- return {
6
- path: input.path,
7
- pollInterval: input.pollInterval,
8
- consume: input.consume,
9
- };
10
- }
11
- function interpolateTemplate(template, values) {
12
- return template.replace(/\{([a-zA-Z0-9_]+)\}/g, (match, key) => values[key] ?? match);
13
- }
14
- function resolveFileConfig(input, ctx) {
15
- return {
16
- ...input,
17
- path: interpolateTemplate(input.path, { instanceId: ctx.process.id }),
18
- };
19
- }
20
- function fileLabel(kind, path) {
21
- return kind === "presence" ? `File present: ${path}` : `File instruction: ${path}`;
22
- }
23
- export const fileExternal = {
24
- presence(input) {
25
- const config = normalizeConfig(input);
26
- return {
27
- kind: FILE_EXTERNAL_PRESENCE_KIND,
28
- label: fileLabel("presence", config.path),
29
- description: `Fires when ${config.path} exists`,
30
- config,
31
- inputMode: "none",
32
- resolve(ctx) {
33
- return resolveFileConfig(config, ctx);
34
- },
35
- };
36
- },
37
- instruction(input) {
38
- const config = normalizeConfig(input);
39
- return {
40
- kind: FILE_EXTERNAL_INSTRUCTION_KIND,
41
- label: fileLabel("instruction", config.path),
42
- description: `Reads ${config.path} as instruction text`,
43
- config,
44
- inputMode: "instruction",
45
- resolve(ctx) {
46
- return resolveFileConfig(config, ctx);
47
- },
48
- };
49
- },
50
- };
51
- function parseFileConfig(value) {
52
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
53
- return null;
54
- }
55
- const record = value;
56
- if (typeof record.path !== "string" || record.path.trim() === "") {
57
- return null;
58
- }
59
- return {
60
- path: record.path,
61
- pollInterval: typeof record.pollInterval === "string" ? record.pollInterval : "1s",
62
- consume: record.consume === "keep" ? "keep" : "delete",
63
- };
64
- }
65
- async function maybeConsume(config) {
66
- if (config.consume === "delete") {
67
- await consumeTriggerFile(config.path);
68
- }
69
- }
70
- function createDueTracker() {
71
- const nextDueAtByKey = new Map();
72
- return {
73
- isDue(key, pollInterval, nowMs = Date.now()) {
74
- const nextDueAt = nextDueAtByKey.get(key) ?? 0;
75
- if (nextDueAt > nowMs) {
76
- return false;
77
- }
78
- const intervalMs = parseDurationMs(pollInterval, 1_000);
79
- nextDueAtByKey.set(key, nowMs + intervalMs);
80
- return true;
81
- },
82
- prune(activeKeys) {
83
- for (const key of [...nextDueAtByKey.keys()]) {
84
- if (!activeKeys.has(key)) {
85
- nextDueAtByKey.delete(key);
86
- }
87
- }
88
- },
89
- };
90
- }
91
- function resolveArmedFileConfig(armed, resolveAlias) {
92
- const parsed = parseFileConfig(armed.resolved) ?? parseFileConfig(armed.source.config);
93
- return parsed ? resolveAlias(parsed) : null;
94
- }
95
- async function pollArmedFileSource(armed, resolveConfig, isDue, activeKeys, result) {
96
- const config = resolveArmedFileConfig(armed, resolveConfig);
97
- if (!config) {
98
- result.errors.push(`${armed.id}:invalid_config`);
99
- return null;
100
- }
101
- const key = `${armed.instanceId}:${armed.id}`;
102
- activeKeys.add(key);
103
- if (!isDue(key, config.pollInterval)) {
104
- return null;
105
- }
106
- const file = await readTriggerFile(config.path);
107
- if (!file.exists) {
108
- return null;
109
- }
110
- return { config, file };
111
- }
112
- async function firePresenceSources(deps, result, resolveConfig, isDue, activeKeys) {
113
- for (const armed of deps.externalSources.listArmed(FILE_EXTERNAL_PRESENCE_KIND)) {
114
- const target = await pollArmedFileSource(armed, resolveConfig, isDue, activeKeys, result);
115
- if (!target) {
116
- continue;
117
- }
118
- const { config } = target;
119
- const fired = await deps.externalSources.fire({
120
- instanceId: armed.instanceId,
121
- armingId: armed.id,
122
- event: { path: config.path, pollInterval: config.pollInterval },
123
- mergeKey: config.path,
124
- });
125
- if (!fired.ok) {
126
- result.errors.push(`${armed.id}:${fired.code ?? fired.error ?? "fire_failed"}`);
127
- continue;
128
- }
129
- await maybeConsume(config);
130
- result.created.push(armed.id);
131
- }
132
- }
133
- async function fireInstructionSources(deps, result, resolveConfig, isDue, activeKeys) {
134
- for (const armed of deps.externalSources.listArmed(FILE_EXTERNAL_INSTRUCTION_KIND)) {
135
- const target = await pollArmedFileSource(armed, resolveConfig, isDue, activeKeys, result);
136
- if (!target) {
137
- continue;
138
- }
139
- const { config, file } = target;
140
- const instruction = file.content.trim();
141
- if (!instruction) {
142
- continue;
143
- }
144
- const fired = await deps.externalSources.fire({
145
- instanceId: armed.instanceId,
146
- armingId: armed.id,
147
- input: { instruction },
148
- event: { path: config.path, pollInterval: config.pollInterval },
149
- mergeKey: config.path,
150
- });
151
- if (!fired.ok) {
152
- result.errors.push(`${armed.id}:${fired.code ?? fired.error ?? "fire_failed"}`);
153
- continue;
154
- }
155
- await maybeConsume(config);
156
- result.created.push(armed.id);
157
- }
158
- }
159
- export function createFileExternalSourceProvider(deps, aliases = {}) {
160
- function applyAlias(config) {
161
- const exact = aliases[config.path];
162
- if (exact) {
163
- return { ...config, path: exact };
164
- }
165
- for (const [template, replacement] of Object.entries(aliases)) {
166
- if (!replacement || !template.includes("{instanceId}")) {
167
- continue;
168
- }
169
- const [prefix, suffix = ""] = template.split("{instanceId}");
170
- if (!prefix || !config.path.startsWith(prefix) || !config.path.endsWith(suffix)) {
171
- continue;
172
- }
173
- const instanceId = config.path.slice(prefix.length, config.path.length - suffix.length);
174
- return {
175
- ...config,
176
- path: replacement.includes("{instanceId}")
177
- ? interpolateTemplate(replacement, { instanceId })
178
- : replacement,
179
- };
180
- }
181
- return config;
182
- }
183
- const due = createDueTracker();
184
- return createPollLoop({
185
- pollInterval: () => "50ms",
186
- isEnabled: () => true,
187
- defaultIntervalMs: 50,
188
- async pollOnce() {
189
- const result = emptyPollResult();
190
- const activeKeys = new Set();
191
- await firePresenceSources(deps, result, applyAlias, due.isDue, activeKeys);
192
- await fireInstructionSources(deps, result, applyAlias, due.isDue, activeKeys);
193
- due.prune(activeKeys);
194
- return result;
195
- },
196
- });
197
- }
198
- //# sourceMappingURL=file-external.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"file-external.js","sourceRoot":"","sources":["../src/file-external.ts"],"names":[],"mappings":"AAKA,OAAO,EACN,kBAAkB,EAClB,cAAc,EACd,eAAe,EAEf,eAAe,EACf,eAAe,GACf,MAAM,6BAA6B,CAAC;AAUrC,MAAM,CAAC,MAAM,2BAA2B,GAAG,gDAAgD,CAAC;AAC5F,MAAM,CAAC,MAAM,8BAA8B,GAAG,mDAAmD,CAAC;AAElG,SAAS,eAAe,CAAC,KAAwB;IAChD,OAAO;QACN,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,OAAO,EAAE,KAAK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