@osolmaz/pi-workflows 0.13.2 → 0.13.4

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 (134) hide show
  1. package/README.md +5 -3
  2. package/dist/builtins/autodoc.workflow.d.ts +5 -5
  3. package/dist/builtins/autoimplement.workflow.d.ts +77 -73
  4. package/dist/builtins/autoimplement.workflow.js +47 -8
  5. package/dist/builtins/autoimplement.workflow.js.map +1 -1
  6. package/dist/builtins/autoplan.workflow.d.ts +8 -8
  7. package/dist/builtins/autoplan.workflow.js +44 -35
  8. package/dist/builtins/autoplan.workflow.js.map +1 -1
  9. package/dist/builtins/catalog.js +2 -2
  10. package/dist/builtins/change-verification.workflow.d.ts +1 -1
  11. package/dist/builtins/plain-summary.workflow.d.ts +1 -1
  12. package/dist/builtins/plan-approval.workflow.d.ts +1 -1
  13. package/dist/builtins/plan-change.workflow.d.ts +29 -29
  14. package/dist/builtins/sanity-check.workflow.d.ts +3 -3
  15. package/dist/builtins/workspace-preparation.workflow.d.ts +1 -1
  16. package/dist/controllers/index.d.ts +2 -2
  17. package/dist/controllers/index.js.map +1 -1
  18. package/dist/controllers/sqlite.js +85 -23
  19. package/dist/controllers/sqlite.js.map +1 -1
  20. package/dist/controllers/types.d.ts +34 -0
  21. package/dist/controllers/workflow-engine-scheduler.d.ts +5 -1
  22. package/dist/controllers/workflow-engine-scheduler.js +83 -1
  23. package/dist/controllers/workflow-engine-scheduler.js.map +1 -1
  24. package/dist/controllers/workflows.d.ts +8 -1
  25. package/dist/controllers/workflows.js +36 -0
  26. package/dist/controllers/workflows.js.map +1 -1
  27. package/dist/extension/controller-host.d.ts +2 -1
  28. package/dist/extension/controller-host.js +1 -1
  29. package/dist/extension/controller-host.js.map +1 -1
  30. package/dist/extension/follow-up-coordinator.d.ts +27 -0
  31. package/dist/extension/follow-up-coordinator.js +131 -0
  32. package/dist/extension/follow-up-coordinator.js.map +1 -0
  33. package/dist/extension/index.d.ts +9 -0
  34. package/dist/extension/index.js +460 -75
  35. package/dist/extension/index.js.map +1 -1
  36. package/dist/extension/session-events.d.ts +2 -3
  37. package/dist/extension/session-events.js +11 -10
  38. package/dist/extension/session-events.js.map +1 -1
  39. package/dist/extension/widget.js +9 -0
  40. package/dist/extension/widget.js.map +1 -1
  41. package/dist/state/database.d.ts +2 -1
  42. package/dist/state/database.js +11 -13
  43. package/dist/state/database.js.map +1 -1
  44. package/dist/state/json.js +6 -1
  45. package/dist/state/json.js.map +1 -1
  46. package/dist/state/mutation.d.ts +1 -1
  47. package/dist/state/mutation.js.map +1 -1
  48. package/dist/state/prune.d.ts +18 -0
  49. package/dist/state/prune.js +373 -0
  50. package/dist/state/prune.js.map +1 -0
  51. package/dist/state/schema.d.ts +1 -1
  52. package/dist/state/schema.js +116 -18
  53. package/dist/state/schema.js.map +1 -1
  54. package/dist/viewer/cli.d.ts +3 -1
  55. package/dist/viewer/cli.js +55 -4
  56. package/dist/viewer/cli.js.map +1 -1
  57. package/dist/viewer/render.js +19 -1
  58. package/dist/viewer/render.js.map +1 -1
  59. package/dist/workflows/composition.d.ts +2 -0
  60. package/dist/workflows/composition.js +15 -0
  61. package/dist/workflows/composition.js.map +1 -1
  62. package/dist/workflows/definition.d.ts +6 -3
  63. package/dist/workflows/definition.js +3 -0
  64. package/dist/workflows/definition.js.map +1 -1
  65. package/dist/workflows/engine.d.ts +6 -1
  66. package/dist/workflows/engine.js +301 -35
  67. package/dist/workflows/engine.js.map +1 -1
  68. package/dist/workflows/human-decision.d.ts +1 -0
  69. package/dist/workflows/human-decision.js +25 -9
  70. package/dist/workflows/human-decision.js.map +1 -1
  71. package/dist/workflows/index.d.ts +3 -1
  72. package/dist/workflows/index.js +2 -0
  73. package/dist/workflows/index.js.map +1 -1
  74. package/dist/workflows/json-patch.d.ts +47 -0
  75. package/dist/workflows/json-patch.js +298 -0
  76. package/dist/workflows/json-patch.js.map +1 -0
  77. package/dist/workflows/schema.js +29 -1
  78. package/dist/workflows/schema.js.map +1 -1
  79. package/dist/workflows/settings.d.ts +128 -0
  80. package/dist/workflows/settings.js +199 -0
  81. package/dist/workflows/settings.js.map +1 -0
  82. package/dist/workflows/store.d.ts +75 -2
  83. package/dist/workflows/store.js +1349 -89
  84. package/dist/workflows/store.js.map +1 -1
  85. package/dist/workflows/tool-input.d.ts +22 -0
  86. package/dist/workflows/tool-input.js +43 -0
  87. package/dist/workflows/tool-input.js.map +1 -1
  88. package/dist/workflows/types.d.ts +40 -5
  89. package/docs/2026-08-25-workflow-follow-ups.md +132 -0
  90. package/docs/2026-08-25-workflow-settings.md +169 -0
  91. package/docs/DEFERRED_TURNS.md +6 -0
  92. package/docs/DESIGN_PHILOSOPHY.md +2 -0
  93. package/docs/SQLITE_STATE.md +31 -18
  94. package/docs/WORKFLOW_COMPOSITION.md +16 -1
  95. package/docs/WORKFLOW_STEP_MESSAGES.md +4 -2
  96. package/docs/plans/2026-08-25-live-workflow-settings-plan.md +532 -0
  97. package/docs/plans/2026-08-25-workflow-run-storage-plan.md +67 -0
  98. package/docs/tui-viewer.md +7 -6
  99. package/docs/workflows.md +22 -1
  100. package/examples/workflows/live-settings.workflow.ts +93 -0
  101. package/herdr-plugin.toml +1 -1
  102. package/package.json +1 -1
  103. package/skills/autoplan/SKILL.md +6 -6
  104. package/src/builtins/autoimplement.workflow.ts +56 -8
  105. package/src/builtins/autoplan.workflow.ts +45 -37
  106. package/src/builtins/catalog.ts +2 -2
  107. package/src/controllers/index.ts +6 -0
  108. package/src/controllers/sqlite.ts +129 -35
  109. package/src/controllers/types.ts +40 -0
  110. package/src/controllers/workflow-engine-scheduler.ts +103 -1
  111. package/src/controllers/workflows.ts +68 -0
  112. package/src/extension/controller-host.ts +6 -1
  113. package/src/extension/follow-up-coordinator.ts +151 -0
  114. package/src/extension/index.ts +538 -88
  115. package/src/extension/session-events.ts +15 -13
  116. package/src/extension/widget.ts +8 -0
  117. package/src/state/database.ts +12 -12
  118. package/src/state/json.ts +6 -1
  119. package/src/state/mutation.ts +4 -1
  120. package/src/state/prune.ts +502 -0
  121. package/src/state/schema.ts +116 -18
  122. package/src/viewer/cli.ts +52 -5
  123. package/src/viewer/render.ts +43 -1
  124. package/src/workflows/composition.ts +19 -0
  125. package/src/workflows/definition.ts +19 -5
  126. package/src/workflows/engine.ts +363 -35
  127. package/src/workflows/human-decision.ts +41 -11
  128. package/src/workflows/index.ts +43 -0
  129. package/src/workflows/json-patch.ts +375 -0
  130. package/src/workflows/schema.ts +31 -1
  131. package/src/workflows/settings.ts +430 -0
  132. package/src/workflows/store.ts +1977 -184
  133. package/src/workflows/tool-input.ts +58 -0
  134. package/src/workflows/types.ts +43 -4
@@ -0,0 +1,151 @@
1
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
+ import type { LeaseClaim } from "../state/mutation.js";
3
+ import type { WorkflowFollowUpRecord } from "../workflows/settings.js";
4
+ import { WorkflowRunStore } from "../workflows/store.js";
5
+
6
+ const FOLLOW_UP_ID_PREFIX = "pi-workflows-follow-up:";
7
+
8
+ export type FollowUpDelivery = {
9
+ followUp: WorkflowFollowUpRecord;
10
+ claim: LeaseClaim;
11
+ };
12
+
13
+ export class FollowUpCoordinator {
14
+ private pending: FollowUpDelivery | undefined;
15
+ private synchronizing = false;
16
+
17
+ constructor(
18
+ private readonly store: WorkflowRunStore,
19
+ private readonly ownerId: string,
20
+ private readonly sendUserMessage: (text: string, options?: { deliverAs?: "followUp" }) => void,
21
+ ) {}
22
+
23
+ async synchronize(ctx: ExtensionContext, workflowActive: boolean): Promise<void> {
24
+ if (this.synchronizing) return;
25
+ this.synchronizing = true;
26
+ try {
27
+ const sessionId = ctx.sessionManager.getSessionId();
28
+ if (this.pending !== undefined) {
29
+ const entryId = findFollowUpEntryId(
30
+ ctx.sessionManager.getBranch(),
31
+ this.pending.followUp.followUpId,
32
+ );
33
+ if (entryId === undefined) return;
34
+ this.store.markFollowUpSent(this.pending.followUp.followUpId, this.pending.claim, entryId);
35
+ this.releaseClaim(this.pending.claim);
36
+ this.pending = undefined;
37
+ }
38
+ if (!ctx.isIdle() || workflowActive) return;
39
+ const claimed = this.store.claimNextFollowUp(sessionId, this.ownerId);
40
+ if (claimed === undefined) return;
41
+ const entryId = findFollowUpEntryId(
42
+ ctx.sessionManager.getBranch(),
43
+ claimed.followUp.followUpId,
44
+ );
45
+ if (entryId !== undefined) {
46
+ this.store.markFollowUpSent(claimed.followUp.followUpId, claimed.claim, entryId);
47
+ this.releaseClaim(claimed.claim);
48
+ return;
49
+ }
50
+ this.pending = claimed;
51
+ try {
52
+ this.sendUserMessage(followUpMessageText(claimed.followUp));
53
+ } catch (error) {
54
+ this.pending = undefined;
55
+ this.releaseClaim(claimed.claim);
56
+ throw error;
57
+ }
58
+ } finally {
59
+ this.synchronizing = false;
60
+ }
61
+ }
62
+
63
+ clear(): void {
64
+ this.pending = undefined;
65
+ }
66
+
67
+ private releaseClaim(claim: LeaseClaim): void {
68
+ try {
69
+ this.store.releaseFollowUpClaim(claim);
70
+ } catch {
71
+ // The lease can expire or move after delivery evidence is saved.
72
+ }
73
+ }
74
+ }
75
+
76
+ export function followUpMessageText(
77
+ followUp: Pick<WorkflowFollowUpRecord, "followUpId" | "prompt">,
78
+ ): string {
79
+ return `${followUp.prompt}\n\n<!-- ${FOLLOW_UP_ID_PREFIX}${followUp.followUpId} -->`;
80
+ }
81
+
82
+ export function findFollowUpEntryId(
83
+ entries: readonly unknown[],
84
+ followUpId: string,
85
+ ): string | undefined {
86
+ const idText = `${FOLLOW_UP_ID_PREFIX}${followUpId}`;
87
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
88
+ const entry = entries[index];
89
+ if (!isRecord(entry) || entry.type !== "message" || typeof entry.id !== "string") continue;
90
+ const message = entry.message;
91
+ if (!isRecord(message) || message.role !== "user") continue;
92
+ if (messageText(message.content).includes(idText)) return entry.id;
93
+ }
94
+ return undefined;
95
+ }
96
+
97
+ export function findSettledPresentationEntries(
98
+ entries: readonly unknown[],
99
+ runId: string,
100
+ ): { presentationEntryId: string; assistantEntryId: string } | undefined {
101
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
102
+ const entry = entries[index];
103
+ if (!isPresentationEntry(entry, runId)) continue;
104
+ for (let childIndex = index + 1; childIndex < entries.length; childIndex += 1) {
105
+ const child = entries[childIndex];
106
+ if (!isRecord(child) || child.type !== "message" || typeof child.id !== "string") continue;
107
+ const message = child.message;
108
+ if (!isRecord(message)) continue;
109
+ if (message.role === "user") break;
110
+ if (
111
+ message.role === "assistant" &&
112
+ (message.stopReason === "stop" || message.stopReason === "length")
113
+ ) {
114
+ return { presentationEntryId: entry.id, assistantEntryId: child.id };
115
+ }
116
+ }
117
+ }
118
+ return undefined;
119
+ }
120
+
121
+ function isPresentationEntry(value: unknown, runId: string): value is { id: string } {
122
+ if (!isRecord(value) || typeof value.id !== "string") return false;
123
+ if (value.type === "custom_message") {
124
+ return (
125
+ value.customType === "pi-workflows-presentation" &&
126
+ isRecord(value.details) &&
127
+ value.details.runId === runId
128
+ );
129
+ }
130
+ if (value.type !== "message" || !isRecord(value.message)) return false;
131
+ return (
132
+ value.message.role === "custom" &&
133
+ value.message.customType === "pi-workflows-presentation" &&
134
+ isRecord(value.message.details) &&
135
+ value.message.details.runId === runId
136
+ );
137
+ }
138
+
139
+ function messageText(value: unknown): string {
140
+ if (typeof value === "string") return value;
141
+ if (!Array.isArray(value)) return "";
142
+ return value
143
+ .flatMap((item) =>
144
+ isRecord(item) && item.type === "text" && typeof item.text === "string" ? [item.text] : [],
145
+ )
146
+ .join("\n");
147
+ }
148
+
149
+ function isRecord(value: unknown): value is Record<string, unknown> {
150
+ return typeof value === "object" && value !== null && !Array.isArray(value);
151
+ }