jsfe 0.2.0 → 0.4.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 +203 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -58,8 +58,8 @@ interface ContextEntry {
|
|
|
58
58
|
role: 'user' | 'assistant' | 'system' | 'tool'; // Message role type
|
|
59
59
|
content: string | Record<string, unknown>; // Message content (text, object, etc.)
|
|
60
60
|
timestamp: number; // Unix timestamp in milliseconds
|
|
61
|
-
stepId?: string; // Optional:
|
|
62
|
-
toolName?: string; // Optional: Tool
|
|
61
|
+
stepId?: string; // Optional: Used by the system when a Step calls a Tool
|
|
62
|
+
toolName?: string; // Optional: Used by the system to record Tool result into the the chat context
|
|
63
63
|
metadata?: Record<string, unknown>; // Optional: Additional context data
|
|
64
64
|
}
|
|
65
65
|
```
|
|
@@ -71,7 +71,6 @@ interface ContextEntry {
|
|
|
71
71
|
const userEntry = {
|
|
72
72
|
role: 'user',
|
|
73
73
|
content: 'I need help with my account',
|
|
74
|
-
timestamp: Date.now()
|
|
75
74
|
};
|
|
76
75
|
// Process the message
|
|
77
76
|
await engine.updateActivity(userEntry, sessionContext);
|
|
@@ -80,8 +79,6 @@ await engine.updateActivity(userEntry, sessionContext);
|
|
|
80
79
|
const assistantEntry = {
|
|
81
80
|
role: 'assistant',
|
|
82
81
|
content: 'I can help you with your account. What specific issue are you experiencing?',
|
|
83
|
-
timestamp: Date.now(),
|
|
84
|
-
stepId: 'greeting-step'
|
|
85
82
|
};
|
|
86
83
|
// Process the message
|
|
87
84
|
await engine.updateActivity(assistantEntry, sessionContext);
|
|
@@ -100,25 +97,38 @@ The Flow Engine implements a sophisticated "stack-of-stacks" architecture that a
|
|
|
100
97
|
- Automatic stack switching for flow interruption/resumption
|
|
101
98
|
- Proper isolation between different workflow contexts
|
|
102
99
|
|
|
103
|
-
2. **Flow Frame Structure**
|
|
100
|
+
2. **Flow Frame Structure (Runtime Execution Context)**
|
|
104
101
|
Each flow execution maintains a complete context frame:
|
|
105
102
|
```typescript
|
|
106
|
-
{
|
|
107
|
-
flowName: string
|
|
108
|
-
flowId: string
|
|
109
|
-
flowVersion: string
|
|
110
|
-
flowStepsStack: [
|
|
111
|
-
contextStack: [
|
|
112
|
-
inputStack: [
|
|
113
|
-
variables:
|
|
114
|
-
transaction: TransactionObj
|
|
115
|
-
userId: string
|
|
116
|
-
startTime: number
|
|
117
|
-
pendingVariable
|
|
118
|
-
lastSayMessage
|
|
119
|
-
pendingInterruption
|
|
103
|
+
interface FlowFrame {
|
|
104
|
+
flowName: string; // Human-readable flow name (from FlowDefinition.name)
|
|
105
|
+
flowId: string; // Unique flow identifier (from FlowDefinition.id)
|
|
106
|
+
flowVersion: string; // Flow version for compatibility (from FlowDefinition.version)
|
|
107
|
+
flowStepsStack: FlowStep[]; // Remaining steps (reversed for efficient pop)
|
|
108
|
+
contextStack: ContextEntry[]; // Complete interaction history with role info
|
|
109
|
+
inputStack: unknown[]; // Current input context for step execution
|
|
110
|
+
variables: Record<string, unknown>; // Unified variable storage (shared across sub-flows)
|
|
111
|
+
transaction: TransactionObj; // Comprehensive transaction and audit tracking
|
|
112
|
+
userId: string; // User identifier for this flow session
|
|
113
|
+
startTime: number; // Flow start timestamp for timing analysis
|
|
114
|
+
pendingVariable?: string; // Variable name awaiting user input (SAY-GET steps)
|
|
115
|
+
lastSayMessage?: string; // Last SAY step output for context
|
|
116
|
+
pendingInterruption?: Record<string, unknown>; // Interruption state management
|
|
117
|
+
accumulatedMessages?: string[]; // Accumulated SAY messages for batching
|
|
118
|
+
parentTransaction?: string; // Parent transaction ID for sub-flow tracking
|
|
119
|
+
justResumed?: boolean; // Flag indicating flow was just resumed
|
|
120
120
|
}
|
|
121
121
|
```
|
|
122
|
+
|
|
123
|
+
**Technical Implementation Details:**
|
|
124
|
+
- **Flow Identity**: `flowName`, `flowId`, `flowVersion` are copied from the FlowDefinition
|
|
125
|
+
- **Dynamic Properties**: Flow `prompt`, `description`, and localized prompts are accessed dynamically from FlowDefinition
|
|
126
|
+
- **flowStepsStack**: Steps stored in reverse order for efficient pop operations
|
|
127
|
+
- **contextStack**: Enhanced with role information for complete conversation context
|
|
128
|
+
- **variables**: Flat storage shared across sub-flows for seamless data passing
|
|
129
|
+
- **accumulatedMessages**: SAY steps are batched for efficient output
|
|
130
|
+
- **justResumed**: Helps engine provide appropriate resumption messages
|
|
131
|
+
```
|
|
122
132
|
|
|
123
133
|
3. **Helper Function Architecture**
|
|
124
134
|
All stack operations go through centralized helper functions:
|
|
@@ -127,6 +137,179 @@ The Flow Engine implements a sophisticated "stack-of-stacks" architecture that a
|
|
|
127
137
|
- `pushToCurrentStack(engine, frame)`: Adds flow to active stack
|
|
128
138
|
- `popFromCurrentStack(engine)`: Removes flow from active stack
|
|
129
139
|
|
|
140
|
+
4. **Tool Definition Structure (Runtime Integration Context)**
|
|
141
|
+
Tools provide external capabilities with comprehensive security and validation:
|
|
142
|
+
```typescript
|
|
143
|
+
interface ToolDefinition {
|
|
144
|
+
id: string; // Unique tool identifier
|
|
145
|
+
name: string; // Human-readable tool name
|
|
146
|
+
description: string; // Tool functionality description
|
|
147
|
+
|
|
148
|
+
parameters?: { // OpenAI Function Calling Standard schema
|
|
149
|
+
type: string; // Parameter type structure
|
|
150
|
+
properties?: Record<string, PropertySchema>; // Parameter validation rules
|
|
151
|
+
required?: string[]; // Required parameter names
|
|
152
|
+
additionalProperties?: boolean; // Additional parameter handling
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
implementation?: { // Execution configuration
|
|
156
|
+
type: 'local' | 'http'; // Implementation type
|
|
157
|
+
function?: string; // Local function name (APPROVED_FUNCTIONS)
|
|
158
|
+
url?: string; // HTTP endpoint with {param} placeholders
|
|
159
|
+
method?: HttpMethod; // HTTP method
|
|
160
|
+
pathParams?: string[]; // URL parameter substitution
|
|
161
|
+
queryParams?: string[]; // Query string parameters
|
|
162
|
+
responseMapping?: MappingConfig; // Response transformation config
|
|
163
|
+
timeout?: number; // Request timeout
|
|
164
|
+
retries?: number; // Retry attempts
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
security?: { // Security controls
|
|
168
|
+
rateLimit?: { // Rate limiting configuration
|
|
169
|
+
requests: number; // Max requests per window
|
|
170
|
+
window: number; // Time window in milliseconds
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
apiKey?: string; // Authentication token
|
|
174
|
+
riskLevel?: 'low' | 'medium' | 'high'; // Security classification
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
**Technical Implementation Details:**
|
|
179
|
+
- **Parameter Validation**: JSON Schema validation with ajv for type safety
|
|
180
|
+
- **Local Function Execution**: Secure execution through APPROVED_FUNCTIONS registry
|
|
181
|
+
- **HTTP Integration**: Full REST API support with authentication and retries
|
|
182
|
+
- **Response Mapping**: Declarative transformation without code injection
|
|
183
|
+
- **Security Controls**: Rate limiting, risk classification, audit logging
|
|
184
|
+
- **Error Handling**: Automatic retry logic and graceful degradation
|
|
185
|
+
|
|
186
|
+
5. **Response Mapping Configuration (MappingConfig Interface)**
|
|
187
|
+
The comprehensive response mapping system supports multiple transformation types:
|
|
188
|
+
```typescript
|
|
189
|
+
export type MappingConfig =
|
|
190
|
+
| JsonPathMappingConfig
|
|
191
|
+
| ObjectMappingConfig
|
|
192
|
+
| ArrayMappingConfig
|
|
193
|
+
| TemplateMappingConfig
|
|
194
|
+
| ConditionalMappingConfig
|
|
195
|
+
| PathConfig
|
|
196
|
+
| string
|
|
197
|
+
| Record<string, unknown>;
|
|
198
|
+
|
|
199
|
+
// JSONPath-based field extraction and transformation
|
|
200
|
+
export type JsonPathMappingConfig = {
|
|
201
|
+
type: 'jsonPath';
|
|
202
|
+
mappings: Record<string, {
|
|
203
|
+
path: string; // JSONPath expression for data extraction
|
|
204
|
+
transform?: ValueTransformConfig; // Optional value transformation
|
|
205
|
+
fallback?: unknown; // Fallback value if path not found
|
|
206
|
+
}>;
|
|
207
|
+
strict?: boolean; // Strict mode validation
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
// Object structure mapping with nested support
|
|
211
|
+
export type ObjectMappingConfig = {
|
|
212
|
+
type: 'object';
|
|
213
|
+
mappings: Record<string, string | PathConfig | MappingConfig | object>;
|
|
214
|
+
strict?: boolean; // Strict mode validation
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
// Array processing with filtering, sorting, and pagination
|
|
218
|
+
export type ArrayMappingConfig = {
|
|
219
|
+
type: 'array';
|
|
220
|
+
source?: string; // Source array path
|
|
221
|
+
filter?: ConditionConfig; // Filtering conditions
|
|
222
|
+
itemMapping?: MappingConfig; // Per-item transformation
|
|
223
|
+
sort?: { field: string; order?: 'asc' | 'desc' }; // Sorting configuration
|
|
224
|
+
offset?: number; // Pagination offset
|
|
225
|
+
limit?: number; // Pagination limit
|
|
226
|
+
fallback?: unknown[]; // Fallback array if source not found
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
// Template-based string generation with variable substitution
|
|
230
|
+
export type TemplateMappingConfig = {
|
|
231
|
+
type: 'template';
|
|
232
|
+
template: string; // Template string with {{variable}} placeholders
|
|
233
|
+
dataPath?: string; // Optional path to resolve template data from
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
// Conditional logic-based mapping selection
|
|
237
|
+
export type ConditionalMappingConfig = {
|
|
238
|
+
type: 'conditional';
|
|
239
|
+
conditions: Array<{
|
|
240
|
+
if: ConditionConfig; // Condition evaluation
|
|
241
|
+
then: MappingConfig; // Mapping to apply if condition true
|
|
242
|
+
}>;
|
|
243
|
+
else?: MappingConfig; // Default mapping if no conditions match
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// Path-based value extraction with transformation
|
|
247
|
+
export type PathConfig = {
|
|
248
|
+
path: string; // Data path for extraction
|
|
249
|
+
transform?: ValueTransformConfig; // Optional value transformation
|
|
250
|
+
fallback?: unknown; // Fallback value if path not found
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
// Comprehensive value transformation system with 25+ transform types
|
|
254
|
+
export interface ValueTransformConfig {
|
|
255
|
+
type: 'parseInt' | 'parseFloat' | 'toLowerCase' | 'toUpperCase' | 'trim' |
|
|
256
|
+
'replace' | 'concat' | 'regex' | 'date' | 'default' | 'conditional' |
|
|
257
|
+
'substring' | 'split' | 'join' | 'abs' | 'round' | 'floor' | 'ceil' |
|
|
258
|
+
'template' | 'sum' | 'average' | 'count' | 'min' | 'max' | 'multiply' |
|
|
259
|
+
'divide' | 'percentage' | 'add' | 'subtract' | 'currentYear' |
|
|
260
|
+
'yearDifference' | 'handlebars' | 'custom';
|
|
261
|
+
|
|
262
|
+
// Common transformation parameters
|
|
263
|
+
fallback?: unknown; // Default value for failed transforms
|
|
264
|
+
prefix?: string; // String prefix for concat operations
|
|
265
|
+
suffix?: string; // String suffix for concat operations
|
|
266
|
+
pattern?: string; // Regex pattern for replace/match operations
|
|
267
|
+
replacement?: string; // Replacement string for regex operations
|
|
268
|
+
template?: string; // Template string for template transforms
|
|
269
|
+
value?: unknown; // Static value for default transforms
|
|
270
|
+
|
|
271
|
+
// Mathematical operation parameters
|
|
272
|
+
precision?: number; // Decimal precision for rounding operations
|
|
273
|
+
divisor?: number; // Divisor for division/percentage operations
|
|
274
|
+
multiplier?: number; // Multiplier for multiplication operations
|
|
275
|
+
addend?: number; // Value to add for addition operations
|
|
276
|
+
subtrahend?: number; // Value to subtract for subtraction operations
|
|
277
|
+
|
|
278
|
+
// Array and aggregation parameters
|
|
279
|
+
field?: string; // Field name for array aggregations
|
|
280
|
+
delimiter?: string; // Delimiter for join/split operations
|
|
281
|
+
index?: number; // Array index for element selection
|
|
282
|
+
|
|
283
|
+
// Conditional and date parameters
|
|
284
|
+
condition?: ConditionConfig; // Condition for conditional transforms
|
|
285
|
+
fromYear?: number; // Start year for year difference calculations
|
|
286
|
+
dataPath?: string; // Path for accessing context data
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Flexible condition evaluation system
|
|
290
|
+
export interface ConditionConfig {
|
|
291
|
+
field: string; // Field path for evaluation
|
|
292
|
+
operator: 'equals' | 'eq' | 'notEquals' | 'ne' | 'contains' | 'exists' |
|
|
293
|
+
'notExists' | 'greaterThan' | 'gt' | 'lessThan' | 'lt' |
|
|
294
|
+
'greaterThanOrEqual' | 'gte' | 'lessThanOrEqual' | 'lte' |
|
|
295
|
+
'startsWith' | 'endsWith' | 'matches' | 'in' | 'hasLength' |
|
|
296
|
+
'isArray' | 'isObject' | 'isString' | 'isNumber';
|
|
297
|
+
value?: unknown; // Comparison value for operators
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
**Response Mapping Technical Features:**
|
|
302
|
+
- **Type Safety**: Full TypeScript interface definitions with validation
|
|
303
|
+
- **Declarative Configuration**: No code injection - pure JSON configuration
|
|
304
|
+
- **Comprehensive Transforms**: 25+ built-in transformation types
|
|
305
|
+
- **Mathematical Operations**: Arithmetic, statistical, and precision control
|
|
306
|
+
- **Date Processing**: Dynamic date calculations and formatting
|
|
307
|
+
- **Template System**: Handlebars-style variable substitution with array iteration
|
|
308
|
+
- **Conditional Logic**: Complex branching and filtering capabilities
|
|
309
|
+
- **Array Processing**: Filtering, sorting, pagination, and aggregation
|
|
310
|
+
- **Path Resolution**: JSONPath support with fallback handling
|
|
311
|
+
- **Security**: Complete input sanitization and validation
|
|
312
|
+
|
|
130
313
|
## Features & Capabilities
|
|
131
314
|
|
|
132
315
|
### Flow Execution Modes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsfe",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "TypeScript workflow engine for conversational/automation apps: multi-flow orchestration, interrupt/resume, stack-based control, robust errors, and flexible tool/REST integration.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Ron Pinkas",
|