@modular-prompt/process 0.1.11
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/LICENSE +21 -0
- package/README.md +166 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/modules/dialogue.d.ts +39 -0
- package/dist/modules/dialogue.d.ts.map +1 -0
- package/dist/modules/dialogue.js +106 -0
- package/dist/modules/dialogue.js.map +1 -0
- package/dist/modules/index.d.ts +9 -0
- package/dist/modules/index.d.ts.map +1 -0
- package/dist/modules/index.js +5 -0
- package/dist/modules/index.js.map +1 -0
- package/dist/modules/material.d.ts +21 -0
- package/dist/modules/material.d.ts.map +1 -0
- package/dist/modules/material.js +41 -0
- package/dist/modules/material.js.map +1 -0
- package/dist/modules/stream-processing.d.ts +26 -0
- package/dist/modules/stream-processing.d.ts.map +1 -0
- package/dist/modules/stream-processing.js +128 -0
- package/dist/modules/stream-processing.js.map +1 -0
- package/dist/modules/summarize.d.ts +32 -0
- package/dist/modules/summarize.d.ts.map +1 -0
- package/dist/modules/summarize.js +97 -0
- package/dist/modules/summarize.js.map +1 -0
- package/dist/workflows/agent-workflow.d.ts +55 -0
- package/dist/workflows/agent-workflow.d.ts.map +1 -0
- package/dist/workflows/agent-workflow.js +337 -0
- package/dist/workflows/agent-workflow.js.map +1 -0
- package/dist/workflows/agentic-workflow/agentic-workflow.d.ts +13 -0
- package/dist/workflows/agentic-workflow/agentic-workflow.d.ts.map +1 -0
- package/dist/workflows/agentic-workflow/agentic-workflow.js +235 -0
- package/dist/workflows/agentic-workflow/agentic-workflow.js.map +1 -0
- package/dist/workflows/agentic-workflow/index.d.ts +4 -0
- package/dist/workflows/agentic-workflow/index.d.ts.map +1 -0
- package/dist/workflows/agentic-workflow/index.js +5 -0
- package/dist/workflows/agentic-workflow/index.js.map +1 -0
- package/dist/workflows/agentic-workflow/modules/agentic.d.ts +12 -0
- package/dist/workflows/agentic-workflow/modules/agentic.d.ts.map +1 -0
- package/dist/workflows/agentic-workflow/modules/agentic.js +22 -0
- package/dist/workflows/agentic-workflow/modules/agentic.js.map +1 -0
- package/dist/workflows/agentic-workflow/modules/execution-freeform.d.ts +15 -0
- package/dist/workflows/agentic-workflow/modules/execution-freeform.d.ts.map +1 -0
- package/dist/workflows/agentic-workflow/modules/execution-freeform.js +140 -0
- package/dist/workflows/agentic-workflow/modules/execution-freeform.js.map +1 -0
- package/dist/workflows/agentic-workflow/modules/execution.d.ts +11 -0
- package/dist/workflows/agentic-workflow/modules/execution.d.ts.map +1 -0
- package/dist/workflows/agentic-workflow/modules/execution.js +180 -0
- package/dist/workflows/agentic-workflow/modules/execution.js.map +1 -0
- package/dist/workflows/agentic-workflow/modules/index.d.ts +6 -0
- package/dist/workflows/agentic-workflow/modules/index.d.ts.map +1 -0
- package/dist/workflows/agentic-workflow/modules/index.js +7 -0
- package/dist/workflows/agentic-workflow/modules/index.js.map +1 -0
- package/dist/workflows/agentic-workflow/modules/integration.d.ts +11 -0
- package/dist/workflows/agentic-workflow/modules/integration.d.ts.map +1 -0
- package/dist/workflows/agentic-workflow/modules/integration.js +85 -0
- package/dist/workflows/agentic-workflow/modules/integration.js.map +1 -0
- package/dist/workflows/agentic-workflow/modules/planning.d.ts +11 -0
- package/dist/workflows/agentic-workflow/modules/planning.d.ts.map +1 -0
- package/dist/workflows/agentic-workflow/modules/planning.js +103 -0
- package/dist/workflows/agentic-workflow/modules/planning.js.map +1 -0
- package/dist/workflows/agentic-workflow/types.d.ts +60 -0
- package/dist/workflows/agentic-workflow/types.d.ts.map +1 -0
- package/dist/workflows/agentic-workflow/types.js +2 -0
- package/dist/workflows/agentic-workflow/types.js.map +1 -0
- package/dist/workflows/concat-process-workflow.d.ts +29 -0
- package/dist/workflows/concat-process-workflow.d.ts.map +1 -0
- package/dist/workflows/concat-process-workflow.js +121 -0
- package/dist/workflows/concat-process-workflow.js.map +1 -0
- package/dist/workflows/dialogue-workflow.d.ts +25 -0
- package/dist/workflows/dialogue-workflow.d.ts.map +1 -0
- package/dist/workflows/dialogue-workflow.js +139 -0
- package/dist/workflows/dialogue-workflow.js.map +1 -0
- package/dist/workflows/index.d.ts +14 -0
- package/dist/workflows/index.d.ts.map +1 -0
- package/dist/workflows/index.js +13 -0
- package/dist/workflows/index.js.map +1 -0
- package/dist/workflows/self-prompting-workflow/index.d.ts +12 -0
- package/dist/workflows/self-prompting-workflow/index.d.ts.map +1 -0
- package/dist/workflows/self-prompting-workflow/index.js +11 -0
- package/dist/workflows/self-prompting-workflow/index.js.map +1 -0
- package/dist/workflows/self-prompting-workflow/modules/integration.d.ts +11 -0
- package/dist/workflows/self-prompting-workflow/modules/integration.d.ts.map +1 -0
- package/dist/workflows/self-prompting-workflow/modules/integration.js +82 -0
- package/dist/workflows/self-prompting-workflow/modules/integration.js.map +1 -0
- package/dist/workflows/self-prompting-workflow/modules/planning.d.ts +11 -0
- package/dist/workflows/self-prompting-workflow/modules/planning.d.ts.map +1 -0
- package/dist/workflows/self-prompting-workflow/modules/planning.js +108 -0
- package/dist/workflows/self-prompting-workflow/modules/planning.js.map +1 -0
- package/dist/workflows/self-prompting-workflow/self-prompting-workflow.d.ts +14 -0
- package/dist/workflows/self-prompting-workflow/self-prompting-workflow.d.ts.map +1 -0
- package/dist/workflows/self-prompting-workflow/self-prompting-workflow.js +250 -0
- package/dist/workflows/self-prompting-workflow/self-prompting-workflow.js.map +1 -0
- package/dist/workflows/self-prompting-workflow/types.d.ts +56 -0
- package/dist/workflows/self-prompting-workflow/types.d.ts.map +1 -0
- package/dist/workflows/self-prompting-workflow/types.js +2 -0
- package/dist/workflows/self-prompting-workflow/types.js.map +1 -0
- package/dist/workflows/stream-workflow.d.ts +17 -0
- package/dist/workflows/stream-workflow.d.ts.map +1 -0
- package/dist/workflows/stream-workflow.js +119 -0
- package/dist/workflows/stream-workflow.js.map +1 -0
- package/dist/workflows/summarize-workflow.d.ts +39 -0
- package/dist/workflows/summarize-workflow.d.ts.map +1 -0
- package/dist/workflows/summarize-workflow.js +152 -0
- package/dist/workflows/summarize-workflow.js.map +1 -0
- package/dist/workflows/types.d.ts +37 -0
- package/dist/workflows/types.d.ts.map +1 -0
- package/dist/workflows/types.js +25 -0
- package/dist/workflows/types.js.map +1 -0
- package/package.json +72 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { compile, merge } from '@modular-prompt/core';
|
|
2
|
+
import { WorkflowExecutionError } from '../types.js';
|
|
3
|
+
import { planning } from './modules/planning.js';
|
|
4
|
+
import { integration } from './modules/integration.js';
|
|
5
|
+
/**
|
|
6
|
+
* Base module for self-prompting workflow
|
|
7
|
+
*/
|
|
8
|
+
const baseSelfPromptingModule = {
|
|
9
|
+
methodology: [
|
|
10
|
+
'- Self-prompting workflow executes work through Planning → Execution → Integration.',
|
|
11
|
+
'- Planning generates complete prompts (instructions + data) for each step.',
|
|
12
|
+
'- Execution uses generated prompts directly without further compilation.'
|
|
13
|
+
],
|
|
14
|
+
state: [
|
|
15
|
+
(ctx) => ctx.phase ? `Current phase: ${ctx.phase}` : null
|
|
16
|
+
]
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Execute planning phase to generate execution plan with complete prompts
|
|
20
|
+
*/
|
|
21
|
+
async function executePlanningPhase(driver, module, context, maxSteps, logger) {
|
|
22
|
+
const planningPrompt = compile(merge(baseSelfPromptingModule, planning, module), context);
|
|
23
|
+
try {
|
|
24
|
+
const planResult = await driver.query(planningPrompt);
|
|
25
|
+
if (planResult.finishReason && planResult.finishReason !== 'stop') {
|
|
26
|
+
throw new WorkflowExecutionError(`Planning failed with reason: ${planResult.finishReason}`, context, {
|
|
27
|
+
phase: 'planning',
|
|
28
|
+
finishReason: planResult.finishReason
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (!planResult.structuredOutput) {
|
|
32
|
+
throw new WorkflowExecutionError('Planning did not return structured output', context, {
|
|
33
|
+
phase: 'planning',
|
|
34
|
+
partialResult: planResult.content
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
let plan;
|
|
38
|
+
const rawOutput = planResult.structuredOutput;
|
|
39
|
+
// Debug: Log raw model output
|
|
40
|
+
if (logger) {
|
|
41
|
+
logger.info(`Raw planning output: ${JSON.stringify(rawOutput, null, 2)}`);
|
|
42
|
+
}
|
|
43
|
+
// Handle case where model returns a single step object instead of {steps: [...]}
|
|
44
|
+
if ('id' in rawOutput && 'prompt' in rawOutput && !('steps' in rawOutput)) {
|
|
45
|
+
if (logger) {
|
|
46
|
+
logger.info('Model returned single step object, wrapping in array');
|
|
47
|
+
}
|
|
48
|
+
plan = { steps: [rawOutput] };
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
plan = rawOutput;
|
|
52
|
+
}
|
|
53
|
+
if (!plan.steps || !Array.isArray(plan.steps)) {
|
|
54
|
+
throw new WorkflowExecutionError('Plan must contain steps array', context, {
|
|
55
|
+
phase: 'planning',
|
|
56
|
+
partialResult: JSON.stringify(plan, null, 2)
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (plan.steps.length > maxSteps) {
|
|
60
|
+
plan.steps = plan.steps.slice(0, maxSteps);
|
|
61
|
+
}
|
|
62
|
+
return plan;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
if (error instanceof WorkflowExecutionError) {
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
throw new WorkflowExecutionError(error, context, {
|
|
69
|
+
phase: 'planning'
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Execute a single step using generated prompt
|
|
75
|
+
*/
|
|
76
|
+
async function executeStep(driver, module, context, step, actions) {
|
|
77
|
+
let actionResult;
|
|
78
|
+
// TODO: Reconsider action handling in self-prompting workflow
|
|
79
|
+
// In the current implementation, actions are executed before the prompt,
|
|
80
|
+
// but this may not align with the self-prompting philosophy where the AI
|
|
81
|
+
// generates complete prompts that should be self-contained.
|
|
82
|
+
// Possible alternatives:
|
|
83
|
+
// 1. Actions should be part of the generated prompt instructions
|
|
84
|
+
// 2. Actions should be executed based on the step result (not before)
|
|
85
|
+
// 3. Remove action support entirely for this workflow variant
|
|
86
|
+
// Execute actions if specified
|
|
87
|
+
if (step.actions) {
|
|
88
|
+
for (const action of step.actions) {
|
|
89
|
+
const handler = actions[action.tool];
|
|
90
|
+
if (!handler) {
|
|
91
|
+
throw new WorkflowExecutionError(`Action "${action.tool}" is not available`, context, { phase: 'execution' });
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
actionResult = await handler(action.params ?? {}, context);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
throw new WorkflowExecutionError(error, context, {
|
|
98
|
+
phase: 'execution',
|
|
99
|
+
partialResult: `Action ${action.tool} failed`
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Use the generated prompt string directly
|
|
105
|
+
// Create a minimal CompiledPrompt structure with just the prompt text
|
|
106
|
+
// Add previous step results as materials for context
|
|
107
|
+
const previousResults = context.executionLog || [];
|
|
108
|
+
const materials = previousResults.map((log) => ({
|
|
109
|
+
type: 'material',
|
|
110
|
+
id: `previous-step-${log.stepId}`,
|
|
111
|
+
title: `Result from ${log.stepId}`,
|
|
112
|
+
content: log.result
|
|
113
|
+
}));
|
|
114
|
+
const executionPrompt = {
|
|
115
|
+
instructions: [{
|
|
116
|
+
type: 'text',
|
|
117
|
+
content: step.prompt
|
|
118
|
+
}],
|
|
119
|
+
data: materials,
|
|
120
|
+
output: []
|
|
121
|
+
};
|
|
122
|
+
try {
|
|
123
|
+
const result = await driver.query(executionPrompt);
|
|
124
|
+
if (result.finishReason && result.finishReason !== 'stop') {
|
|
125
|
+
throw new WorkflowExecutionError(`Execution failed with reason: ${result.finishReason}`, context, {
|
|
126
|
+
phase: 'execution',
|
|
127
|
+
partialResult: result.content,
|
|
128
|
+
finishReason: result.finishReason
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
// Freeform output - use content directly
|
|
132
|
+
return {
|
|
133
|
+
stepId: step.id,
|
|
134
|
+
result: result.content,
|
|
135
|
+
actionResult,
|
|
136
|
+
metadata: {
|
|
137
|
+
usage: result.usage
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
if (error instanceof WorkflowExecutionError) {
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
throw new WorkflowExecutionError(error, context, {
|
|
146
|
+
phase: 'execution'
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Execute all steps in the execution plan
|
|
152
|
+
*/
|
|
153
|
+
async function executeExecutionPhase(driver, module, context, plan, actions) {
|
|
154
|
+
const executionLog = context.executionLog ? [...context.executionLog] : [];
|
|
155
|
+
const startIndex = executionLog.length;
|
|
156
|
+
for (let i = startIndex; i < plan.steps.length; i++) {
|
|
157
|
+
const currentStep = plan.steps[i];
|
|
158
|
+
const logEntry = await executeStep(driver, module, context, currentStep, actions);
|
|
159
|
+
executionLog.push(logEntry);
|
|
160
|
+
context.executionLog = executionLog;
|
|
161
|
+
}
|
|
162
|
+
return executionLog;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Execute integration phase to combine all results
|
|
166
|
+
*/
|
|
167
|
+
async function executeIntegrationPhase(driver, module, context) {
|
|
168
|
+
const integrationPrompt = compile(merge(baseSelfPromptingModule, integration, module), context);
|
|
169
|
+
try {
|
|
170
|
+
const result = await driver.query(integrationPrompt);
|
|
171
|
+
if (result.finishReason && result.finishReason !== 'stop') {
|
|
172
|
+
throw new WorkflowExecutionError(`Integration failed with reason: ${result.finishReason}`, context, {
|
|
173
|
+
phase: 'integration',
|
|
174
|
+
partialResult: result.content,
|
|
175
|
+
finishReason: result.finishReason
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return result.content;
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
if (error instanceof WorkflowExecutionError) {
|
|
182
|
+
throw error;
|
|
183
|
+
}
|
|
184
|
+
throw new WorkflowExecutionError(error, context, {
|
|
185
|
+
phase: 'integration'
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Self-prompting workflow - AI generates complete prompts for each step
|
|
191
|
+
*
|
|
192
|
+
* @param driver - AI driver for executing prompts
|
|
193
|
+
* @param module - User's prompt module (merged with phase modules internally)
|
|
194
|
+
* @param context - Workflow context
|
|
195
|
+
* @param options - Workflow options
|
|
196
|
+
* @returns Workflow result with final output and updated context
|
|
197
|
+
*/
|
|
198
|
+
export async function selfPromptingProcess(driver, module, context, options = {}) {
|
|
199
|
+
const { maxSteps = 5, actions = {}, enablePlanning = true, logger } = options;
|
|
200
|
+
let currentContext = { ...context };
|
|
201
|
+
let plan = currentContext.plan;
|
|
202
|
+
// Planning phase
|
|
203
|
+
if (enablePlanning || !plan) {
|
|
204
|
+
if (logger) {
|
|
205
|
+
logger.info('Starting planning phase');
|
|
206
|
+
}
|
|
207
|
+
currentContext.phase = 'planning';
|
|
208
|
+
plan = await executePlanningPhase(driver, module, currentContext, maxSteps, logger);
|
|
209
|
+
currentContext.plan = plan;
|
|
210
|
+
if (logger) {
|
|
211
|
+
logger.info(`Planning completed: ${plan.steps.length} steps`);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (!plan) {
|
|
215
|
+
throw new WorkflowExecutionError('Self-prompting workflow requires a plan to execute', currentContext, { phase: 'planning' });
|
|
216
|
+
}
|
|
217
|
+
// Execution phase
|
|
218
|
+
if (logger) {
|
|
219
|
+
logger.info('Starting execution phase');
|
|
220
|
+
}
|
|
221
|
+
currentContext.phase = 'execution';
|
|
222
|
+
const executionLog = await executeExecutionPhase(driver, module, currentContext, plan, actions);
|
|
223
|
+
currentContext.executionLog = executionLog;
|
|
224
|
+
if (logger) {
|
|
225
|
+
logger.info(`Execution completed: ${executionLog.length} steps`);
|
|
226
|
+
}
|
|
227
|
+
// Integration phase
|
|
228
|
+
if (logger) {
|
|
229
|
+
logger.info('Starting integration phase');
|
|
230
|
+
}
|
|
231
|
+
currentContext.phase = 'integration';
|
|
232
|
+
const finalOutput = await executeIntegrationPhase(driver, module, currentContext);
|
|
233
|
+
if (logger) {
|
|
234
|
+
logger.info('Integration completed');
|
|
235
|
+
}
|
|
236
|
+
const finalContext = {
|
|
237
|
+
...currentContext,
|
|
238
|
+
phase: 'complete'
|
|
239
|
+
};
|
|
240
|
+
return {
|
|
241
|
+
output: finalOutput,
|
|
242
|
+
context: finalContext,
|
|
243
|
+
metadata: {
|
|
244
|
+
planSteps: plan.steps.length,
|
|
245
|
+
executedSteps: executionLog.length,
|
|
246
|
+
actionsUsed: executionLog.filter(log => log.actionResult !== undefined).length
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=self-prompting-workflow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"self-prompting-workflow.js","sourceRoot":"","sources":["../../../src/workflows/self-prompting-workflow/self-prompting-workflow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAEtD,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAUrD,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAEvD;;GAEG;AACH,MAAM,uBAAuB,GAA+C;IAC1E,WAAW,EAAE;QACX,qFAAqF;QACrF,4EAA4E;QAC5E,0EAA0E;KAC3E;IACD,KAAK,EAAE;QACL,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,kBAAkB,GAAG,CAA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ACnE,CAAC;IAED,oBAAoB;IACpB,IAAI,MAAM,EAAE,CAAC;QACX,MAAM,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAC;IAC5C,CAAC;IACD,cAAc,CAAC,KAAK,GAAG,aAAa,CAAC;IACrC,MAAM,WAAW,GAAG,MAAM,uBAAuB,CAAC,MAAM,EAAE,MAAM,EAAE,cAAc,CAAC,CAAC;IAClF,IAAI,MAAM,EAAE,CAAC;QACX,MAAM,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC;IACvC,CAAC;IAED,MAAM,YAAY,GAAiC;QACjD,GAAG,cAAc;QACjB,KAAK,EAAE,UAAU;KAClB,CAAC;IAEF,OAAO;QACL,MAAM,EAAE,WAAW;QACnB,OAAO,EAAE,YAAY;QACrB,QAAQ,EAAE;YACR,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM;YAC5B,aAAa,EAAE,YAAY,CAAC,MAAM;YAClC,WAAW,EAAE,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,MAAM;SAC/E;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Action handler type for self-prompting workflow
|
|
3
|
+
*/
|
|
4
|
+
export type ActionHandler<TContext = any> = (params: any, context: TContext) => Promise<any>;
|
|
5
|
+
/**
|
|
6
|
+
* Self-prompting workflow step definition
|
|
7
|
+
* Each step contains a complete prompt string that will be executed directly
|
|
8
|
+
*/
|
|
9
|
+
export interface SelfPromptingStep {
|
|
10
|
+
id: string;
|
|
11
|
+
prompt: string;
|
|
12
|
+
actions?: Array<{
|
|
13
|
+
tool: string;
|
|
14
|
+
params?: any;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Self-prompting workflow execution log entry
|
|
19
|
+
*/
|
|
20
|
+
export interface SelfPromptingExecutionLog {
|
|
21
|
+
stepId: string;
|
|
22
|
+
result: string;
|
|
23
|
+
actionResult?: any;
|
|
24
|
+
metadata?: any;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Self-prompting workflow plan (structured output from planning phase)
|
|
28
|
+
*/
|
|
29
|
+
export interface SelfPromptingPlan {
|
|
30
|
+
steps: SelfPromptingStep[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Context for self-prompting workflow
|
|
34
|
+
*/
|
|
35
|
+
export interface SelfPromptingWorkflowContext {
|
|
36
|
+
inputs?: any;
|
|
37
|
+
state?: {
|
|
38
|
+
content: string;
|
|
39
|
+
usage?: number;
|
|
40
|
+
};
|
|
41
|
+
plan?: SelfPromptingPlan;
|
|
42
|
+
executionLog?: SelfPromptingExecutionLog[];
|
|
43
|
+
currentStep?: SelfPromptingStep;
|
|
44
|
+
actionResult?: any;
|
|
45
|
+
phase?: 'planning' | 'execution' | 'integration' | 'complete';
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Options for self-prompting workflow
|
|
49
|
+
*/
|
|
50
|
+
export interface SelfPromptingWorkflowOptions {
|
|
51
|
+
maxSteps?: number;
|
|
52
|
+
actions?: Record<string, ActionHandler>;
|
|
53
|
+
enablePlanning?: boolean;
|
|
54
|
+
logger?: any;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/workflows/self-prompting-workflow/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,aAAa,CAAC,QAAQ,GAAG,GAAG,IAAI,CAC1C,MAAM,EAAE,GAAG,EACX,OAAO,EAAE,QAAQ,KACd,OAAO,CAAC,GAAG,CAAC,CAAC;AAElB;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,KAAK,CAAC;QACd,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,CAAC,EAAE,GAAG,CAAC;KACd,CAAC,CAAC;CACJ;AAED;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,QAAQ,CAAC,EAAE,GAAG,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,iBAAiB,EAAE,CAAC;CAC5B;AAED;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC3C,MAAM,CAAC,EAAE,GAAG,CAAC;IACb,KAAK,CAAC,EAAE;QACN,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,CAAC,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,IAAI,CAAC,EAAE,iBAAiB,CAAC;IACzB,YAAY,CAAC,EAAE,yBAAyB,EAAE,CAAC;IAC3C,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAChC,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,KAAK,CAAC,EAAE,UAAU,GAAG,WAAW,GAAG,aAAa,GAAG,UAAU,CAAC;CAC/D;AAED;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC3C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IACxC,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,GAAG,CAAC;CACd"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/workflows/self-prompting-workflow/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { PromptModule } from '@modular-prompt/core';
|
|
2
|
+
import { type AIDriver, type WorkflowResult } from './types.js';
|
|
3
|
+
import type { StreamProcessingContext } from '../modules/stream-processing.js';
|
|
4
|
+
/**
|
|
5
|
+
* Options for stream workflow
|
|
6
|
+
*/
|
|
7
|
+
export interface StreamWorkflowOptions {
|
|
8
|
+
tokenLimit?: number;
|
|
9
|
+
maxChunk?: number;
|
|
10
|
+
targetTokens?: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Stream processing workflow - processes chunks sequentially with state accumulation
|
|
14
|
+
* Each iteration processes a range of chunks and updates the state
|
|
15
|
+
*/
|
|
16
|
+
export declare function streamProcess(driver: AIDriver, module: PromptModule<StreamProcessingContext>, context: StreamProcessingContext, options?: StreamWorkflowOptions): Promise<WorkflowResult<StreamProcessingContext>>;
|
|
17
|
+
//# sourceMappingURL=stream-workflow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-workflow.d.ts","sourceRoot":"","sources":["../../src/workflows/stream-workflow.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAA0B,KAAK,QAAQ,EAAE,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AACxF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAS/E;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AA+CD;;;GAGG;AACH,wBAAsB,aAAa,CACjC,MAAM,EAAE,QAAQ,EAChB,MAAM,EAAE,YAAY,CAAC,uBAAuB,CAAC,EAC7C,OAAO,EAAE,uBAAuB,EAChC,OAAO,GAAE,qBAA0B,GAClC,OAAO,CAAC,cAAc,CAAC,uBAAuB,CAAC,CAAC,CA8FlD"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { compile } from '@modular-prompt/core';
|
|
2
|
+
import { WorkflowExecutionError } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Simple token estimation (roughly 4 characters per token)
|
|
5
|
+
*/
|
|
6
|
+
function estimateTokens(text) {
|
|
7
|
+
return Math.ceil(text.length / 4);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Get next range of chunks to process
|
|
11
|
+
*/
|
|
12
|
+
function getNextRange(chunks, currentRange, options) {
|
|
13
|
+
const { tokenLimit = 128000, maxChunk = 100, } = options || {};
|
|
14
|
+
if (!chunks || chunks.length === 0) {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
const range = { ...currentRange };
|
|
18
|
+
if (range.end === undefined) {
|
|
19
|
+
range.end = 0;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
range.start = range.end;
|
|
23
|
+
}
|
|
24
|
+
if (range.start === undefined) {
|
|
25
|
+
range.start = range.end;
|
|
26
|
+
}
|
|
27
|
+
let tokens = 0;
|
|
28
|
+
for (let i = range.start; i < chunks.length; i++) {
|
|
29
|
+
tokens += chunks[i].usage || 0;
|
|
30
|
+
range.end = i + 1;
|
|
31
|
+
if (tokens > tokenLimit || range.end - range.start > maxChunk) {
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (range.start >= range.end) {
|
|
36
|
+
return undefined; // done
|
|
37
|
+
}
|
|
38
|
+
return range;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Stream processing workflow - processes chunks sequentially with state accumulation
|
|
42
|
+
* Each iteration processes a range of chunks and updates the state
|
|
43
|
+
*/
|
|
44
|
+
export async function streamProcess(driver, module, context, options = {}) {
|
|
45
|
+
const { tokenLimit, maxChunk, targetTokens } = options;
|
|
46
|
+
if (!context.chunks || context.chunks.length === 0) {
|
|
47
|
+
throw new Error('No chunks provided for stream processing');
|
|
48
|
+
}
|
|
49
|
+
// Initialize or use existing state
|
|
50
|
+
let state = context.state || {
|
|
51
|
+
content: '',
|
|
52
|
+
usage: 0
|
|
53
|
+
};
|
|
54
|
+
// Calculate initial range
|
|
55
|
+
let range = getNextRange(context.chunks, context.range, { tokenLimit, maxChunk });
|
|
56
|
+
while (range) {
|
|
57
|
+
const iterationContext = {
|
|
58
|
+
...context,
|
|
59
|
+
state,
|
|
60
|
+
range,
|
|
61
|
+
targetTokens
|
|
62
|
+
};
|
|
63
|
+
const prompt = compile(module, iterationContext);
|
|
64
|
+
let nextStateContent;
|
|
65
|
+
let nextStateUsage;
|
|
66
|
+
try {
|
|
67
|
+
const result = await driver.query(prompt);
|
|
68
|
+
// Check finish reason for dynamic failures
|
|
69
|
+
if (result.finishReason && result.finishReason !== 'stop') {
|
|
70
|
+
throw new WorkflowExecutionError(`Query failed with reason: ${result.finishReason}`, {
|
|
71
|
+
...context,
|
|
72
|
+
state,
|
|
73
|
+
range
|
|
74
|
+
}, {
|
|
75
|
+
phase: 'stream-iteration',
|
|
76
|
+
partialResult: state.content,
|
|
77
|
+
finishReason: result.finishReason
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
nextStateContent = result.content;
|
|
81
|
+
nextStateUsage = result.usage?.completionTokens || estimateTokens(result.content);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
// If it's already a WorkflowExecutionError, re-throw
|
|
85
|
+
if (error instanceof WorkflowExecutionError) {
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
// Return error with context that can be used to resume
|
|
89
|
+
throw new WorkflowExecutionError(error, {
|
|
90
|
+
...context,
|
|
91
|
+
state,
|
|
92
|
+
range
|
|
93
|
+
}, {
|
|
94
|
+
phase: 'stream-iteration',
|
|
95
|
+
partialResult: state.content
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
// Update state with the result
|
|
99
|
+
state = {
|
|
100
|
+
content: nextStateContent,
|
|
101
|
+
usage: nextStateUsage
|
|
102
|
+
};
|
|
103
|
+
range = getNextRange(context.chunks, range, { tokenLimit, maxChunk });
|
|
104
|
+
}
|
|
105
|
+
const finalContext = {
|
|
106
|
+
...context,
|
|
107
|
+
state,
|
|
108
|
+
range: undefined // Processing complete
|
|
109
|
+
};
|
|
110
|
+
return {
|
|
111
|
+
output: state.content,
|
|
112
|
+
context: finalContext,
|
|
113
|
+
metadata: {
|
|
114
|
+
finalTokens: state.usage,
|
|
115
|
+
targetTokens
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=stream-workflow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-workflow.js","sourceRoot":"","sources":["../../src/workflows/stream-workflow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,OAAO,EAAE,sBAAsB,EAAsC,MAAM,YAAY,CAAC;AAGxF;;GAEG;AACH,SAAS,cAAc,CAAC,IAAY;IAClC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACpC,CAAC;AAWD;;GAEG;AACH,SAAS,YAAY,CACnB,MAAyC,EACzC,YAA+C,EAC/C,OAAoD;IAEpD,MAAM,EACJ,UAAU,GAAG,MAAM,EACnB,QAAQ,GAAG,GAAG,GACf,GAAG,OAAO,IAAI,EAAE,CAAC;IAElB,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACnC,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,KAAK,GAAG,EAAE,GAAG,YAAY,EAAE,CAAC;IAElC,IAAI,KAAK,CAAC,GAAG,KAAK,SAAS,EAAE,CAAC;QAC5B,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC;IAChB,CAAC;SAAM,CAAC;QACN,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC;IAC1B,CAAC;IACD,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;Q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gB;YACzB,KAAK,EAAE,cAAc;SACtB,CAAC;QAEF,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,CAAC;IACxE,CAAC;IAED,MAAM,YAAY,GAA4B;QAC5C,GAAG,OAAO;QACV,KAAK;QACL,KAAK,EAAE,SAAS,CAAC,sBAAsB;KACxC,CAAC;IAEF,OAAO;QACL,MAAM,EAAE,KAAK,CAAC,OAAO;QACrB,OAAO,EAAE,YAAY;QACrB,QAAQ,EAAE;YACR,WAAW,EAAE,KAAK,CAAC,KAAK;YACxB,YAAY;SACb;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { PromptModule } from '@modular-prompt/core';
|
|
2
|
+
import { type AIDriver, type WorkflowResult } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Context for summarization workflow
|
|
5
|
+
*/
|
|
6
|
+
export interface SummarizeWorkflowContext {
|
|
7
|
+
state?: {
|
|
8
|
+
content: string;
|
|
9
|
+
usage?: number;
|
|
10
|
+
};
|
|
11
|
+
chunks?: Array<{
|
|
12
|
+
content: string;
|
|
13
|
+
partOf?: string;
|
|
14
|
+
usage?: number;
|
|
15
|
+
attachments?: any[];
|
|
16
|
+
}>;
|
|
17
|
+
preparationNote?: {
|
|
18
|
+
content: string;
|
|
19
|
+
};
|
|
20
|
+
targetTokens?: number;
|
|
21
|
+
range?: {
|
|
22
|
+
start: number;
|
|
23
|
+
end: number;
|
|
24
|
+
};
|
|
25
|
+
analysisReport?: string;
|
|
26
|
+
phase?: 'analysis' | 'summarization' | 'complete';
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Options for summarization workflow
|
|
30
|
+
*/
|
|
31
|
+
export interface SummarizeWorkflowOptions {
|
|
32
|
+
targetTokens: number;
|
|
33
|
+
enableAnalysis?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Summarization workflow - performs analysis and summarization with stream processing
|
|
37
|
+
*/
|
|
38
|
+
export declare function summarizeProcess(driver: AIDriver, module: PromptModule<SummarizeWorkflowContext>, context: SummarizeWorkflowContext, options: SummarizeWorkflowOptions): Promise<WorkflowResult<SummarizeWorkflowContext>>;
|
|
39
|
+
//# sourceMappingURL=summarize-workflow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"summarize-workflow.d.ts","sourceRoot":"","sources":["../../src/workflows/summarize-workflow.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAGzD,OAAO,EAA0B,KAAK,QAAQ,EAAE,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AASxF;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,KAAK,CAAC,EAAE;QACN,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,CAAC,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,MAAM,CAAC,EAAE,KAAK,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;QAChB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,WAAW,CAAC,EAAE,GAAG,EAAE,CAAC;KACrB,CAAC,CAAC;IACH,eAAe,CAAC,EAAE;QAChB,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE;QACN,KAAK,EAAE,MAAM,CAAC;QACd,GAAG,EAAE,MAAM,CAAC;KACb,CAAC;IACF,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,KAAK,CAAC,EAAE,UAAU,GAAG,eAAe,GAAG,UAAU,CAAC;CACnD;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,QAAQ,EAChB,MAAM,EAAE,YAAY,CAAC,wBAAwB,CAAC,EAC9C,OAAO,EAAE,wBAAwB,EACjC,OAAO,EAAE,wBAAwB,GAChC,OAAO,CAAC,cAAc,CAAC,wBAAwB,CAAC,CAAC,CA2KnD"}
|