@leitwerk-dev/showcase-processes 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.
- package/README.md +66 -0
- package/dist/LICENSE +13 -0
- package/dist/file-action-triggers.d.ts +9 -0
- package/dist/file-action-triggers.d.ts.map +1 -0
- package/dist/file-action-triggers.js +87 -0
- package/dist/file-action-triggers.js.map +1 -0
- package/dist/file-external.d.ts +16 -0
- package/dist/file-external.d.ts.map +1 -0
- package/dist/file-external.js +198 -0
- package/dist/file-external.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/dist/poem-leaf-outcome.d.ts +41 -0
- package/dist/poem-leaf-outcome.d.ts.map +1 -0
- package/dist/poem-leaf-outcome.js +110 -0
- package/dist/poem-leaf-outcome.js.map +1 -0
- package/dist/process-definition.d.ts +18 -0
- package/dist/process-definition.d.ts.map +1 -0
- package/dist/process-definition.js +832 -0
- package/dist/process-definition.js.map +1 -0
- package/dist/turns/external-complete.d.ts +6 -0
- package/dist/turns/external-complete.d.ts.map +1 -0
- package/dist/turns/external-complete.js +6 -0
- package/dist/turns/external-complete.js.map +1 -0
- package/dist/turns/poem-creator.d.ts +15 -0
- package/dist/turns/poem-creator.d.ts.map +1 -0
- package/dist/turns/poem-creator.js +72 -0
- package/dist/turns/poem-creator.js.map +1 -0
- package/dist/turns/run-single-prompt.d.ts +8 -0
- package/dist/turns/run-single-prompt.d.ts.map +1 -0
- package/dist/turns/run-single-prompt.js +57 -0
- package/dist/turns/run-single-prompt.js.map +1 -0
- package/dist/ui/assets/leaf-outcome-element.js +233 -0
- package/dist/ui/assets/leaf-outcome-element.js.map +1 -0
- package/dist/ui/assets/poem-leaf-outcome-element.js +328 -0
- package/dist/ui/assets/poem-leaf-outcome-element.js.map +1 -0
- package/dist/ui/leaf-outcome-element.d.ts +2 -0
- package/dist/ui/leaf-outcome-element.d.ts.map +1 -0
- package/dist/ui/leaf-outcome-element.js +79 -0
- package/dist/ui/leaf-outcome-element.js.map +1 -0
- package/dist/ui/manifest.json +24 -0
- package/dist/ui/poem-leaf-outcome-element.d.ts +2 -0
- package/dist/ui/poem-leaf-outcome-element.d.ts.map +1 -0
- package/dist/ui/poem-leaf-outcome-element.js +171 -0
- package/dist/ui/poem-leaf-outcome-element.js.map +1 -0
- package/package.json +54 -0
- package/src/file-action-triggers.ts +120 -0
- package/src/file-external.ts +258 -0
- package/src/index.ts +76 -0
- package/src/poem-leaf-outcome.ts +176 -0
- package/src/process-definition.ts +1029 -0
- package/src/turns/external-complete.ts +7 -0
- package/src/turns/poem-creator.ts +79 -0
- package/src/turns/run-single-prompt.ts +70 -0
- package/src/ui/leaf-outcome-element.ts +95 -0
- package/src/ui/manifest.json +24 -0
- package/src/ui/poem-leaf-outcome-element.ts +204 -0
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leitwerk-dev/showcase-processes",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/leitwerk-dev/leitwerk.git",
|
|
7
|
+
"directory": "extensions/showcase-processes"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"leitwerk": {
|
|
11
|
+
"extension": {
|
|
12
|
+
"source": "./src/index.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"discover": false,
|
|
16
|
+
"ui": {
|
|
17
|
+
"source": "./src/ui/manifest.json",
|
|
18
|
+
"import": "./dist/ui/manifest.json"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"source": "./src/index.ts",
|
|
24
|
+
"import": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup --silent --config tsup.runtime.config.ts",
|
|
30
|
+
"build:ext-ui": "rm -rf dist/ui && tsup --silent --config tsup.ui.config.ts && node ./scripts/write-ui-manifest.mjs",
|
|
31
|
+
"dev:ext-ui": "node ./scripts/write-ui-manifest.mjs && tsup --silent --watch --config tsup.ui.config.ts"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@leitwerk-dev/domain": "0.1.0",
|
|
35
|
+
"@leitwerk-dev/process-sdk": "0.1.0",
|
|
36
|
+
"@leitwerk-dev/watcher-utils": "0.1.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"tsup": "^8.5.1"
|
|
40
|
+
},
|
|
41
|
+
"license": "Apache-2.0",
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=22"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist/**",
|
|
50
|
+
"src/**",
|
|
51
|
+
"!dist/**/*.test.*",
|
|
52
|
+
"!src/**/*.test.ts"
|
|
53
|
+
]
|
|
54
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { CoreServerSetupDeps } from "@leitwerk-dev/process-sdk";
|
|
2
|
+
import {
|
|
3
|
+
consumeTriggerFile,
|
|
4
|
+
createPollLoop,
|
|
5
|
+
emptyPollResult,
|
|
6
|
+
type PollResult,
|
|
7
|
+
readTriggerFile,
|
|
8
|
+
} from "@leitwerk-dev/watcher-utils";
|
|
9
|
+
import { poemCreatorActionIds } from "./turns/poem-creator.js";
|
|
10
|
+
|
|
11
|
+
export interface SinglePromptFileTriggerConfig {
|
|
12
|
+
poll_interval: string;
|
|
13
|
+
poem_review_path: string;
|
|
14
|
+
complete_prompt_path: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function findEligiblePoemReviewInstanceIds(deps: CoreServerSetupDeps): string[] {
|
|
18
|
+
return deps.processes
|
|
19
|
+
.listAll()
|
|
20
|
+
.filter(
|
|
21
|
+
(process) =>
|
|
22
|
+
process.processId === "poem_creator_process" &&
|
|
23
|
+
process.selectedTurnId === "poem_review" &&
|
|
24
|
+
process.lifecycleStatus === "waiting",
|
|
25
|
+
)
|
|
26
|
+
.map((process) => process.id);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function processPoemReviewTrigger(
|
|
30
|
+
deps: CoreServerSetupDeps,
|
|
31
|
+
config: SinglePromptFileTriggerConfig,
|
|
32
|
+
eligibleInstanceIds: readonly string[],
|
|
33
|
+
result: PollResult,
|
|
34
|
+
): Promise<void> {
|
|
35
|
+
const file = await readTriggerFile(config.poem_review_path);
|
|
36
|
+
if (!file.exists) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const message = file.content.trim();
|
|
40
|
+
if (message.length === 0) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (eligibleInstanceIds.length !== 1) {
|
|
44
|
+
result.skipped.push(`poem_review:${eligibleInstanceIds.length}`);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const instanceId = eligibleInstanceIds[0];
|
|
48
|
+
if (!instanceId) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const action = await deps.processActions.executeAction(
|
|
52
|
+
instanceId,
|
|
53
|
+
poemCreatorActionIds.requestRevision,
|
|
54
|
+
{ message },
|
|
55
|
+
{ source: "external" },
|
|
56
|
+
);
|
|
57
|
+
if (!action.ok) {
|
|
58
|
+
result.errors.push(
|
|
59
|
+
`poem_review:${instanceId}:${action.code ?? action.error ?? "external_action_failed"}`,
|
|
60
|
+
);
|
|
61
|
+
deps.events.create({
|
|
62
|
+
instanceId,
|
|
63
|
+
eventType: "external_source_failed",
|
|
64
|
+
data: {
|
|
65
|
+
kind: "@leitwerk-dev/showcase-processes.file.instruction",
|
|
66
|
+
path: config.poem_review_path,
|
|
67
|
+
message: action.code ?? action.error,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
deps.events.create({
|
|
73
|
+
instanceId,
|
|
74
|
+
eventType: "external_source_consumed",
|
|
75
|
+
data: {
|
|
76
|
+
kind: "@leitwerk-dev/showcase-processes.file.instruction",
|
|
77
|
+
path: config.poem_review_path,
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
await consumeTriggerFile(config.poem_review_path);
|
|
81
|
+
result.created.push(`poem_review:${instanceId}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function createSinglePromptFileTriggerLoop(
|
|
85
|
+
deps: CoreServerSetupDeps,
|
|
86
|
+
config: SinglePromptFileTriggerConfig,
|
|
87
|
+
) {
|
|
88
|
+
const armed = new Set<string>();
|
|
89
|
+
return createPollLoop({
|
|
90
|
+
pollInterval: () => config.poll_interval,
|
|
91
|
+
isEnabled: () => true,
|
|
92
|
+
defaultIntervalMs: 1_000,
|
|
93
|
+
async pollOnce() {
|
|
94
|
+
const result = emptyPollResult();
|
|
95
|
+
const eligible = findEligiblePoemReviewInstanceIds(deps);
|
|
96
|
+
for (const instanceId of eligible) {
|
|
97
|
+
if (!armed.has(instanceId)) {
|
|
98
|
+
deps.events.create({
|
|
99
|
+
instanceId,
|
|
100
|
+
eventType: "external_trigger_listener_armed",
|
|
101
|
+
data: {
|
|
102
|
+
trigger: "poem_review_file",
|
|
103
|
+
actionId: poemCreatorActionIds.requestRevision,
|
|
104
|
+
path: config.poem_review_path,
|
|
105
|
+
pollInterval: config.poll_interval,
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
armed.add(instanceId);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
for (const instanceId of [...armed]) {
|
|
112
|
+
if (!eligible.includes(instanceId)) {
|
|
113
|
+
armed.delete(instanceId);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
await processPoemReviewTrigger(deps, config, eligible, result);
|
|
117
|
+
return result;
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CoreServerSetupDeps,
|
|
3
|
+
ExternalActionSource,
|
|
4
|
+
ExternalSourceArmingLike,
|
|
5
|
+
} from "@leitwerk-dev/process-sdk";
|
|
6
|
+
import {
|
|
7
|
+
consumeTriggerFile,
|
|
8
|
+
createPollLoop,
|
|
9
|
+
emptyPollResult,
|
|
10
|
+
type PollResult,
|
|
11
|
+
parseDurationMs,
|
|
12
|
+
readTriggerFile,
|
|
13
|
+
} from "@leitwerk-dev/watcher-utils";
|
|
14
|
+
|
|
15
|
+
export type FileExternalConsumeMode = "delete" | "keep";
|
|
16
|
+
|
|
17
|
+
export interface FileExternalInput {
|
|
18
|
+
path: string;
|
|
19
|
+
pollInterval: string;
|
|
20
|
+
consume: FileExternalConsumeMode;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const FILE_EXTERNAL_PRESENCE_KIND = "@leitwerk-dev/showcase-processes.file.presence";
|
|
24
|
+
export const FILE_EXTERNAL_INSTRUCTION_KIND = "@leitwerk-dev/showcase-processes.file.instruction";
|
|
25
|
+
|
|
26
|
+
function normalizeConfig(input: FileExternalInput): FileExternalInput {
|
|
27
|
+
return {
|
|
28
|
+
path: input.path,
|
|
29
|
+
pollInterval: input.pollInterval,
|
|
30
|
+
consume: input.consume,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function interpolateTemplate(template: string, values: Record<string, string>): string {
|
|
35
|
+
return template.replace(/\{([a-zA-Z0-9_]+)\}/g, (match, key: string) => values[key] ?? match);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function resolveFileConfig(
|
|
39
|
+
input: FileExternalInput,
|
|
40
|
+
ctx: { process: { id: string } },
|
|
41
|
+
): FileExternalInput {
|
|
42
|
+
return {
|
|
43
|
+
...input,
|
|
44
|
+
path: interpolateTemplate(input.path, { instanceId: ctx.process.id }),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function fileLabel(kind: "presence" | "instruction", path: string): string {
|
|
49
|
+
return kind === "presence" ? `File present: ${path}` : `File instruction: ${path}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const fileExternal = {
|
|
53
|
+
presence(input: FileExternalInput): ExternalActionSource {
|
|
54
|
+
const config = normalizeConfig(input);
|
|
55
|
+
return {
|
|
56
|
+
kind: FILE_EXTERNAL_PRESENCE_KIND,
|
|
57
|
+
label: fileLabel("presence", config.path),
|
|
58
|
+
description: `Fires when ${config.path} exists`,
|
|
59
|
+
config,
|
|
60
|
+
inputMode: "none",
|
|
61
|
+
resolve(ctx) {
|
|
62
|
+
return resolveFileConfig(config, ctx);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
instruction(input: FileExternalInput): ExternalActionSource {
|
|
67
|
+
const config = normalizeConfig(input);
|
|
68
|
+
return {
|
|
69
|
+
kind: FILE_EXTERNAL_INSTRUCTION_KIND,
|
|
70
|
+
label: fileLabel("instruction", config.path),
|
|
71
|
+
description: `Reads ${config.path} as instruction text`,
|
|
72
|
+
config,
|
|
73
|
+
inputMode: "instruction",
|
|
74
|
+
resolve(ctx) {
|
|
75
|
+
return resolveFileConfig(config, ctx);
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
function parseFileConfig(value: unknown): FileExternalInput | null {
|
|
82
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
const record = value as Record<string, unknown>;
|
|
86
|
+
if (typeof record.path !== "string" || record.path.trim() === "") {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
path: record.path,
|
|
91
|
+
pollInterval: typeof record.pollInterval === "string" ? record.pollInterval : "1s",
|
|
92
|
+
consume: record.consume === "keep" ? "keep" : "delete",
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function maybeConsume(config: FileExternalInput): Promise<void> {
|
|
97
|
+
if (config.consume === "delete") {
|
|
98
|
+
await consumeTriggerFile(config.path);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function createDueTracker() {
|
|
103
|
+
const nextDueAtByKey = new Map<string, number>();
|
|
104
|
+
return {
|
|
105
|
+
isDue(key: string, pollInterval: string, nowMs = Date.now()): boolean {
|
|
106
|
+
const nextDueAt = nextDueAtByKey.get(key) ?? 0;
|
|
107
|
+
if (nextDueAt > nowMs) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
const intervalMs = parseDurationMs(pollInterval, 1_000);
|
|
111
|
+
nextDueAtByKey.set(key, nowMs + intervalMs);
|
|
112
|
+
return true;
|
|
113
|
+
},
|
|
114
|
+
prune(activeKeys: ReadonlySet<string>): void {
|
|
115
|
+
for (const key of [...nextDueAtByKey.keys()]) {
|
|
116
|
+
if (!activeKeys.has(key)) {
|
|
117
|
+
nextDueAtByKey.delete(key);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function resolveArmedFileConfig(
|
|
125
|
+
armed: ReturnType<CoreServerSetupDeps["externalSources"]["listArmed"]>[number],
|
|
126
|
+
resolveAlias: (config: FileExternalInput) => FileExternalInput,
|
|
127
|
+
): FileExternalInput | null {
|
|
128
|
+
const parsed = parseFileConfig(armed.resolved) ?? parseFileConfig(armed.source.config);
|
|
129
|
+
return parsed ? resolveAlias(parsed) : null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function pollArmedFileSource(
|
|
133
|
+
armed: ExternalSourceArmingLike,
|
|
134
|
+
resolveConfig: (config: FileExternalInput) => FileExternalInput,
|
|
135
|
+
isDue: (key: string, pollInterval: string) => boolean,
|
|
136
|
+
activeKeys: Set<string>,
|
|
137
|
+
result: PollResult,
|
|
138
|
+
) {
|
|
139
|
+
const config = resolveArmedFileConfig(armed, resolveConfig);
|
|
140
|
+
if (!config) {
|
|
141
|
+
result.errors.push(`${armed.id}:invalid_config`);
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const key = `${armed.instanceId}:${armed.id}`;
|
|
145
|
+
activeKeys.add(key);
|
|
146
|
+
if (!isDue(key, config.pollInterval)) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
const file = await readTriggerFile(config.path);
|
|
150
|
+
if (!file.exists) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
return { config, file };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function firePresenceSources(
|
|
157
|
+
deps: CoreServerSetupDeps,
|
|
158
|
+
result: PollResult,
|
|
159
|
+
resolveConfig: (config: FileExternalInput) => FileExternalInput,
|
|
160
|
+
isDue: (key: string, pollInterval: string) => boolean,
|
|
161
|
+
activeKeys: Set<string>,
|
|
162
|
+
): Promise<void> {
|
|
163
|
+
for (const armed of deps.externalSources.listArmed(FILE_EXTERNAL_PRESENCE_KIND)) {
|
|
164
|
+
const target = await pollArmedFileSource(armed, resolveConfig, isDue, activeKeys, result);
|
|
165
|
+
if (!target) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const { config } = target;
|
|
169
|
+
const fired = await deps.externalSources.fire({
|
|
170
|
+
instanceId: armed.instanceId,
|
|
171
|
+
armingId: armed.id,
|
|
172
|
+
event: { path: config.path, pollInterval: config.pollInterval },
|
|
173
|
+
mergeKey: config.path,
|
|
174
|
+
});
|
|
175
|
+
if (!fired.ok) {
|
|
176
|
+
result.errors.push(`${armed.id}:${fired.code ?? fired.error ?? "fire_failed"}`);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
await maybeConsume(config);
|
|
180
|
+
result.created.push(armed.id);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function fireInstructionSources(
|
|
185
|
+
deps: CoreServerSetupDeps,
|
|
186
|
+
result: PollResult,
|
|
187
|
+
resolveConfig: (config: FileExternalInput) => FileExternalInput,
|
|
188
|
+
isDue: (key: string, pollInterval: string) => boolean,
|
|
189
|
+
activeKeys: Set<string>,
|
|
190
|
+
): Promise<void> {
|
|
191
|
+
for (const armed of deps.externalSources.listArmed(FILE_EXTERNAL_INSTRUCTION_KIND)) {
|
|
192
|
+
const target = await pollArmedFileSource(armed, resolveConfig, isDue, activeKeys, result);
|
|
193
|
+
if (!target) {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const { config, file } = target;
|
|
197
|
+
const instruction = file.content.trim();
|
|
198
|
+
if (!instruction) {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
const fired = await deps.externalSources.fire({
|
|
202
|
+
instanceId: armed.instanceId,
|
|
203
|
+
armingId: armed.id,
|
|
204
|
+
input: { instruction },
|
|
205
|
+
event: { path: config.path, pollInterval: config.pollInterval },
|
|
206
|
+
mergeKey: config.path,
|
|
207
|
+
});
|
|
208
|
+
if (!fired.ok) {
|
|
209
|
+
result.errors.push(`${armed.id}:${fired.code ?? fired.error ?? "fire_failed"}`);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
await maybeConsume(config);
|
|
213
|
+
result.created.push(armed.id);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function createFileExternalSourceProvider(
|
|
218
|
+
deps: CoreServerSetupDeps,
|
|
219
|
+
aliases: Partial<Record<string, string>> = {},
|
|
220
|
+
) {
|
|
221
|
+
function applyAlias(config: FileExternalInput): FileExternalInput {
|
|
222
|
+
const exact = aliases[config.path];
|
|
223
|
+
if (exact) {
|
|
224
|
+
return { ...config, path: exact };
|
|
225
|
+
}
|
|
226
|
+
for (const [template, replacement] of Object.entries(aliases)) {
|
|
227
|
+
if (!replacement || !template.includes("{instanceId}")) {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
const [prefix, suffix = ""] = template.split("{instanceId}");
|
|
231
|
+
if (!prefix || !config.path.startsWith(prefix) || !config.path.endsWith(suffix)) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
const instanceId = config.path.slice(prefix.length, config.path.length - suffix.length);
|
|
235
|
+
return {
|
|
236
|
+
...config,
|
|
237
|
+
path: replacement.includes("{instanceId}")
|
|
238
|
+
? interpolateTemplate(replacement, { instanceId })
|
|
239
|
+
: replacement,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
return config;
|
|
243
|
+
}
|
|
244
|
+
const due = createDueTracker();
|
|
245
|
+
return createPollLoop({
|
|
246
|
+
pollInterval: () => "50ms",
|
|
247
|
+
isEnabled: () => true,
|
|
248
|
+
defaultIntervalMs: 50,
|
|
249
|
+
async pollOnce() {
|
|
250
|
+
const result = emptyPollResult();
|
|
251
|
+
const activeKeys = new Set<string>();
|
|
252
|
+
await firePresenceSources(deps, result, applyAlias, due.isDue, activeKeys);
|
|
253
|
+
await fireInstructionSources(deps, result, applyAlias, due.isDue, activeKeys);
|
|
254
|
+
due.prune(activeKeys);
|
|
255
|
+
return result;
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { LeitwerkExtensionModule } from "@leitwerk-dev/process-sdk";
|
|
2
|
+
import { coreHostCapabilities } from "@leitwerk-dev/process-sdk";
|
|
3
|
+
import { createFileExternalSourceProvider } from "./file-external.js";
|
|
4
|
+
import {
|
|
5
|
+
k8sSmokeLongProcess,
|
|
6
|
+
k8sSmokeProcess,
|
|
7
|
+
k8sSmokeSpecializedProcess,
|
|
8
|
+
poemCreatorProcess,
|
|
9
|
+
singlePromptExternalCompleteProcess,
|
|
10
|
+
singlePromptProcess,
|
|
11
|
+
singlePromptWithToolProcess,
|
|
12
|
+
} from "./process-definition.js";
|
|
13
|
+
import { leaveFeedbackToolRenderer } from "./turns/poem-creator.js";
|
|
14
|
+
|
|
15
|
+
export interface SinglePromptExtensionConfig {
|
|
16
|
+
file_triggers?: {
|
|
17
|
+
poll_interval?: string;
|
|
18
|
+
poem_review_path?: string;
|
|
19
|
+
complete_prompt_path?: string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const manifest = {
|
|
24
|
+
id: "showcase-processes",
|
|
25
|
+
version: "0.1.0",
|
|
26
|
+
} as const;
|
|
27
|
+
|
|
28
|
+
const fileTriggerDefaults = {
|
|
29
|
+
poll_interval: "1s",
|
|
30
|
+
poem_review_path: "/tmp/poem-review-{instanceId}",
|
|
31
|
+
complete_prompt_path: "/tmp/complete-prompt",
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const singlePromptExtension: LeitwerkExtensionModule = {
|
|
35
|
+
manifest,
|
|
36
|
+
setupCatalog(api) {
|
|
37
|
+
api.registerToolRenderer(leaveFeedbackToolRenderer);
|
|
38
|
+
api.registerProcess(singlePromptProcess);
|
|
39
|
+
api.registerProcess(singlePromptWithToolProcess);
|
|
40
|
+
api.registerProcess(singlePromptExternalCompleteProcess);
|
|
41
|
+
api.registerProcess(k8sSmokeProcess);
|
|
42
|
+
api.registerProcess(k8sSmokeSpecializedProcess);
|
|
43
|
+
api.registerProcess(k8sSmokeLongProcess);
|
|
44
|
+
api.registerProcess(poemCreatorProcess);
|
|
45
|
+
},
|
|
46
|
+
setupServer(api, rawConfig) {
|
|
47
|
+
const deps = api.get(coreHostCapabilities.serverSetup);
|
|
48
|
+
if (!deps || Array.isArray(deps)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const config = (rawConfig ?? {}) as SinglePromptExtensionConfig;
|
|
52
|
+
const resolvedFileTriggerConfig = {
|
|
53
|
+
...fileTriggerDefaults,
|
|
54
|
+
...config.file_triggers,
|
|
55
|
+
};
|
|
56
|
+
const fileSourceProvider = createFileExternalSourceProvider(deps, {
|
|
57
|
+
"/tmp/complete-prompt": resolvedFileTriggerConfig.complete_prompt_path,
|
|
58
|
+
"/tmp/poem-review-{instanceId}": resolvedFileTriggerConfig.poem_review_path,
|
|
59
|
+
});
|
|
60
|
+
api.onStart(() => {
|
|
61
|
+
fileSourceProvider.start();
|
|
62
|
+
});
|
|
63
|
+
api.onStop(() => {
|
|
64
|
+
fileSourceProvider.stop();
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export default singlePromptExtension;
|
|
70
|
+
|
|
71
|
+
export * from "./file-external.js";
|
|
72
|
+
export * from "./poem-leaf-outcome.js";
|
|
73
|
+
export * from "./process-definition.js";
|
|
74
|
+
export * from "./turns/external-complete.js";
|
|
75
|
+
export * from "./turns/poem-creator.js";
|
|
76
|
+
export * from "./turns/run-single-prompt.js";
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { normalizeMarkdownText } from "@leitwerk-dev/domain";
|
|
2
|
+
import type { PiTreeEntry } from "@leitwerk-dev/process-sdk";
|
|
3
|
+
|
|
4
|
+
export type PoemLeafOutcomeReviewOutcome = "no_issues" | "leave_feedback";
|
|
5
|
+
|
|
6
|
+
export interface PoemLeafOutcomeReview extends Record<string, unknown> {
|
|
7
|
+
outcome: PoemLeafOutcomeReviewOutcome;
|
|
8
|
+
summary: string | null;
|
|
9
|
+
feedback: string | null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface PoemLeafOutcomePayload extends Record<string, unknown> {
|
|
13
|
+
prompt: string;
|
|
14
|
+
markdown: string;
|
|
15
|
+
title: string | null;
|
|
16
|
+
stanzas: string[][];
|
|
17
|
+
review?: PoemLeafOutcomeReview | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function normalizePoemBody(body: string): string {
|
|
21
|
+
return body.replace(/<br\s*\/?>/gi, "\n");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function normalizeOptionalText(value: string | null | undefined): string | null {
|
|
25
|
+
if (typeof value !== "string") {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const trimmed = value.trim();
|
|
29
|
+
return trimmed === "" ? null : trimmed;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function normalizeReview(
|
|
33
|
+
review:
|
|
34
|
+
| {
|
|
35
|
+
outcome: PoemLeafOutcomeReviewOutcome;
|
|
36
|
+
summary?: string | null;
|
|
37
|
+
feedback?: string | null;
|
|
38
|
+
}
|
|
39
|
+
| null
|
|
40
|
+
| undefined,
|
|
41
|
+
): PoemLeafOutcomeReview | null {
|
|
42
|
+
if (!review) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
outcome: review.outcome,
|
|
47
|
+
summary: normalizeOptionalText(review.summary),
|
|
48
|
+
feedback: normalizeOptionalText(review.feedback),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function uniqueNonEmptyStrings(values: Array<string | null>): string[] {
|
|
53
|
+
const seen = new Set<string>();
|
|
54
|
+
const result: string[] = [];
|
|
55
|
+
for (const value of values) {
|
|
56
|
+
if (!value || seen.has(value)) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
seen.add(value);
|
|
60
|
+
result.push(value);
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function extractLeafEntryMarkdown(
|
|
66
|
+
entry: Pick<PiTreeEntry, "message"> | null,
|
|
67
|
+
): string | null {
|
|
68
|
+
const content = entry?.message?.content;
|
|
69
|
+
return typeof content === "string" && content.trim() !== "" ? content.trim() : null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function resolvePoemLeafMarkdown(input: {
|
|
73
|
+
turnResultMarkdown: string | null | undefined;
|
|
74
|
+
leafEntry: Pick<PiTreeEntry, "message"> | null;
|
|
75
|
+
}): string {
|
|
76
|
+
const turnResultMarkdown = input.turnResultMarkdown?.trim() ?? "";
|
|
77
|
+
if (turnResultMarkdown !== "") {
|
|
78
|
+
return turnResultMarkdown;
|
|
79
|
+
}
|
|
80
|
+
return extractLeafEntryMarkdown(input.leafEntry) ?? "";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function parsePoemLeafMarkdown(markdown: string): {
|
|
84
|
+
title: string | null;
|
|
85
|
+
stanzas: string[][];
|
|
86
|
+
} {
|
|
87
|
+
const normalized = normalizeMarkdownText(markdown);
|
|
88
|
+
if (normalized === "") {
|
|
89
|
+
return { title: null, stanzas: [] };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const lines = normalized.split("\n");
|
|
93
|
+
let title: string | null = null;
|
|
94
|
+
let bodyStartIndex = 0;
|
|
95
|
+
|
|
96
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
97
|
+
const line = lines[index]?.trim() ?? "";
|
|
98
|
+
if (line === "") {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (line.startsWith("#")) {
|
|
102
|
+
title = line.replace(/^#{1,6}\s*/, "").trim() || null;
|
|
103
|
+
bodyStartIndex = index + 1;
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const body = normalizePoemBody(lines.slice(bodyStartIndex).join("\n")).trim();
|
|
109
|
+
if (body === "") {
|
|
110
|
+
return { title, stanzas: [] };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const stanzas = body
|
|
114
|
+
.split(/\n\s*\n+/)
|
|
115
|
+
.map((stanza) =>
|
|
116
|
+
stanza
|
|
117
|
+
.split("\n")
|
|
118
|
+
.map((line) => line.trim())
|
|
119
|
+
.filter((line) => line !== ""),
|
|
120
|
+
)
|
|
121
|
+
.filter((stanza) => stanza.length > 0);
|
|
122
|
+
|
|
123
|
+
return { title, stanzas };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function buildPoemLeafOutcomePayload(input: {
|
|
127
|
+
prompt: string;
|
|
128
|
+
markdown: string;
|
|
129
|
+
review?: {
|
|
130
|
+
outcome: PoemLeafOutcomeReviewOutcome;
|
|
131
|
+
summary?: string | null;
|
|
132
|
+
feedback?: string | null;
|
|
133
|
+
} | null;
|
|
134
|
+
}): PoemLeafOutcomePayload {
|
|
135
|
+
const normalizedMarkdown = normalizeMarkdownText(input.markdown);
|
|
136
|
+
const parsed = parsePoemLeafMarkdown(normalizedMarkdown);
|
|
137
|
+
const review = normalizeReview(input.review);
|
|
138
|
+
return {
|
|
139
|
+
prompt: input.prompt,
|
|
140
|
+
markdown: normalizedMarkdown,
|
|
141
|
+
title: parsed.title,
|
|
142
|
+
stanzas: parsed.stanzas,
|
|
143
|
+
...(review ? { review } : {}),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function buildPoemLeafOutcomeFallbackMarkdown(input: {
|
|
148
|
+
markdown: string;
|
|
149
|
+
review?: {
|
|
150
|
+
outcome: PoemLeafOutcomeReviewOutcome;
|
|
151
|
+
summary?: string | null;
|
|
152
|
+
feedback?: string | null;
|
|
153
|
+
} | null;
|
|
154
|
+
}): string {
|
|
155
|
+
const normalizedMarkdown = normalizeMarkdownText(input.markdown);
|
|
156
|
+
const review = normalizeReview(input.review);
|
|
157
|
+
if (!review) {
|
|
158
|
+
return normalizedMarkdown;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const reviewSectionHeading = review.outcome === "no_issues" ? "## LLM Opinion" : "## Review";
|
|
162
|
+
const reviewParagraphs =
|
|
163
|
+
review.outcome === "no_issues"
|
|
164
|
+
? uniqueNonEmptyStrings([review.summary ?? "The automated review found no issues."])
|
|
165
|
+
: uniqueNonEmptyStrings([
|
|
166
|
+
review.summary,
|
|
167
|
+
review.feedback,
|
|
168
|
+
review.summary === null && review.feedback === null
|
|
169
|
+
? "The automated review reported issues but returned no details."
|
|
170
|
+
: null,
|
|
171
|
+
]);
|
|
172
|
+
const reviewSection = [reviewSectionHeading, ...reviewParagraphs].join("\n\n");
|
|
173
|
+
return [normalizedMarkdown, reviewSection]
|
|
174
|
+
.filter((section) => section.trim() !== "")
|
|
175
|
+
.join("\n\n");
|
|
176
|
+
}
|