groundswell 0.0.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 (120) hide show
  1. package/.claude/settings.local.json +9 -0
  2. package/.claude/system_prompts/task-breakdown.md +100 -0
  3. package/PRPs/001-hierarchical-workflow-engine.md +2438 -0
  4. package/PRPs/PRDs/001-hierarchical-workflow-engine.md +543 -0
  5. package/PRPs/PRDs/002-agent-prompt.md +390 -0
  6. package/PRPs/PRDs/003-agent-prompt.md +943 -0
  7. package/PRPs/PRDs/004-agent-prompt.md +1136 -0
  8. package/PRPs/PRDs/tasks-001.json +492 -0
  9. package/PRPs/README.md +83 -0
  10. package/PRPs/templates/prp_base.md +222 -0
  11. package/README.md +218 -0
  12. package/docs/agent.md +422 -0
  13. package/docs/prompt.md +419 -0
  14. package/docs/workflow.md +600 -0
  15. package/examples/README.md +244 -0
  16. package/examples/examples/01-basic-workflow.ts +100 -0
  17. package/examples/examples/02-decorator-options.ts +217 -0
  18. package/examples/examples/03-parent-child.ts +241 -0
  19. package/examples/examples/04-observers-debugger.ts +340 -0
  20. package/examples/examples/05-error-handling.ts +387 -0
  21. package/examples/examples/06-concurrent-tasks.ts +352 -0
  22. package/examples/examples/07-agent-loops.ts +432 -0
  23. package/examples/examples/08-sdk-features.ts +667 -0
  24. package/examples/examples/09-reflection.ts +573 -0
  25. package/examples/examples/10-introspection.ts +550 -0
  26. package/examples/index.ts +143 -0
  27. package/examples/utils/helpers.ts +57 -0
  28. package/llms_full.txt +5890 -0
  29. package/package.json +63 -0
  30. package/plan/P1P2/PRP.md +527 -0
  31. package/plan/P1P2/research/LRU_CACHE_BEST_PRACTICES.md +1929 -0
  32. package/plan/P1P2/research/LRU_CACHE_CODE_PATTERNS.md +857 -0
  33. package/plan/P1P2/research/LRU_CACHE_INTEGRATION_GUIDE.md +738 -0
  34. package/plan/P1P2/research/LRU_CACHE_RESEARCH_INDEX.md +424 -0
  35. package/plan/P1P2/research/REFLECTION_INDEX.md +291 -0
  36. package/plan/P1P2/research/REFLECTION_RESEARCH_REPORT.md +1342 -0
  37. package/plan/P1P2/research/RESEARCH_SUMMARY.md +342 -0
  38. package/plan/P1P2/research/anthropic-sdk.md +174 -0
  39. package/plan/P1P2/research/async-local-storage.md +200 -0
  40. package/plan/P1P2/research/reflection-code-patterns.md +1205 -0
  41. package/plan/P1P2/research/reflection-decision-matrix.md +421 -0
  42. package/plan/P1P2/research/reflection-implementation-guide.md +1341 -0
  43. package/plan/P1P2/research/reflection-integration-guide.md +834 -0
  44. package/plan/P1P2/research/reflection-patterns.md +1468 -0
  45. package/plan/P1P2/research/reflection-quick-reference.md +558 -0
  46. package/plan/P1P2/research/zod-schema.md +152 -0
  47. package/plan/P3P4/PRP.md +1388 -0
  48. package/plan/P3P4/research/caching-lru.md +116 -0
  49. package/plan/P3P4/research/introspection-tools.md +177 -0
  50. package/plan/P3P4/research/reflection-patterns.md +117 -0
  51. package/plan/P4P5/PRP.md +1136 -0
  52. package/plan/P4P5/research/RESEARCH_SUMMARY.md +151 -0
  53. package/plan/architecture/external_deps.md +358 -0
  54. package/plan/architecture/system_context.md +242 -0
  55. package/plan/backlog.json +867 -0
  56. package/plan/research/INTROSPECTION_RESEARCH_SUMMARY.md +378 -0
  57. package/plan/research/README-INTROSPECTION.md +352 -0
  58. package/plan/research/agent-introspection-patterns.md +1085 -0
  59. package/plan/research/introspection-security-guide.md +928 -0
  60. package/plan/research/introspection-tool-examples.md +875 -0
  61. package/scripts/generate-llms-full.ts +206 -0
  62. package/src/__tests__/integration/agent-workflow.test.ts +256 -0
  63. package/src/__tests__/integration/tree-mirroring.test.ts +114 -0
  64. package/src/__tests__/unit/agent.test.ts +169 -0
  65. package/src/__tests__/unit/cache-key.test.ts +182 -0
  66. package/src/__tests__/unit/cache.test.ts +172 -0
  67. package/src/__tests__/unit/context.test.ts +138 -0
  68. package/src/__tests__/unit/decorators.test.ts +100 -0
  69. package/src/__tests__/unit/introspection-tools.test.ts +277 -0
  70. package/src/__tests__/unit/prompt.test.ts +135 -0
  71. package/src/__tests__/unit/reflection.test.ts +210 -0
  72. package/src/__tests__/unit/tree-debugger.test.ts +85 -0
  73. package/src/__tests__/unit/workflow.test.ts +81 -0
  74. package/src/cache/cache-key.ts +244 -0
  75. package/src/cache/cache.ts +236 -0
  76. package/src/cache/index.ts +8 -0
  77. package/src/core/agent.ts +573 -0
  78. package/src/core/context.ts +119 -0
  79. package/src/core/event-tree.ts +260 -0
  80. package/src/core/factory.ts +123 -0
  81. package/src/core/index.ts +17 -0
  82. package/src/core/logger.ts +87 -0
  83. package/src/core/mcp-handler.ts +184 -0
  84. package/src/core/prompt.ts +150 -0
  85. package/src/core/workflow-context.ts +349 -0
  86. package/src/core/workflow.ts +302 -0
  87. package/src/debugger/index.ts +1 -0
  88. package/src/debugger/tree-debugger.ts +210 -0
  89. package/src/decorators/index.ts +3 -0
  90. package/src/decorators/observed-state.ts +95 -0
  91. package/src/decorators/step.ts +139 -0
  92. package/src/decorators/task.ts +96 -0
  93. package/src/examples/index.ts +2 -0
  94. package/src/examples/tdd-orchestrator.ts +65 -0
  95. package/src/examples/test-cycle-workflow.ts +64 -0
  96. package/src/index.ts +140 -0
  97. package/src/reflection/index.ts +5 -0
  98. package/src/reflection/reflection.ts +407 -0
  99. package/src/tools/index.ts +36 -0
  100. package/src/tools/introspection.ts +464 -0
  101. package/src/types/agent.ts +90 -0
  102. package/src/types/decorators.ts +25 -0
  103. package/src/types/error-strategy.ts +13 -0
  104. package/src/types/error.ts +20 -0
  105. package/src/types/events.ts +74 -0
  106. package/src/types/index.ts +55 -0
  107. package/src/types/logging.ts +24 -0
  108. package/src/types/observer.ts +18 -0
  109. package/src/types/prompt.ts +40 -0
  110. package/src/types/reflection.ts +117 -0
  111. package/src/types/sdk-primitives.ts +128 -0
  112. package/src/types/snapshot.ts +14 -0
  113. package/src/types/workflow-context.ts +163 -0
  114. package/src/types/workflow.ts +37 -0
  115. package/src/utils/id.ts +11 -0
  116. package/src/utils/index.ts +3 -0
  117. package/src/utils/observable.ts +77 -0
  118. package/tasks.json +0 -0
  119. package/tsconfig.json +22 -0
  120. package/vitest.config.ts +16 -0
@@ -0,0 +1,55 @@
1
+ // Core types
2
+ export type { WorkflowStatus, WorkflowNode } from './workflow.js';
3
+ export type { LogLevel, LogEntry } from './logging.js';
4
+ export type { SerializedWorkflowState, StateFieldMetadata } from './snapshot.js';
5
+ export type { WorkflowError } from './error.js';
6
+ export type { WorkflowEvent } from './events.js';
7
+ export type { WorkflowObserver } from './observer.js';
8
+ export type { StepOptions, TaskOptions } from './decorators.js';
9
+ export type { ErrorMergeStrategy } from './error-strategy.js';
10
+
11
+ // SDK primitive types
12
+ export type {
13
+ Tool,
14
+ ToolResult,
15
+ MCPServer,
16
+ Skill,
17
+ HookHandler,
18
+ PreToolUseContext,
19
+ PostToolUseContext,
20
+ SessionStartContext,
21
+ SessionEndContext,
22
+ AgentHooks,
23
+ TokenUsage,
24
+ } from './sdk-primitives.js';
25
+
26
+ // Agent types
27
+ export type { AgentConfig, PromptOverrides } from './agent.js';
28
+
29
+ // Prompt types
30
+ export type { PromptConfig } from './prompt.js';
31
+
32
+ // WorkflowContext types
33
+ export type {
34
+ WorkflowContext,
35
+ WorkflowConfig,
36
+ WorkflowResult,
37
+ EventTreeHandle,
38
+ EventNode,
39
+ EventMetrics,
40
+ AgentLike,
41
+ PromptLike,
42
+ } from './workflow-context.js';
43
+
44
+ // Reflection types
45
+ export type {
46
+ ReflectionAPI,
47
+ ReflectionConfig,
48
+ ReflectionContext,
49
+ ReflectionResult,
50
+ ReflectionEntry,
51
+ } from './reflection.js';
52
+ export {
53
+ DEFAULT_REFLECTION_CONFIG,
54
+ createReflectionConfig,
55
+ } from './reflection.js';
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Log severity levels
3
+ */
4
+ export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
5
+
6
+ /**
7
+ * A single log entry in the workflow
8
+ */
9
+ export interface LogEntry {
10
+ /** Unique identifier for this log entry */
11
+ id: string;
12
+ /** ID of the workflow that created this log */
13
+ workflowId: string;
14
+ /** Unix timestamp in milliseconds */
15
+ timestamp: number;
16
+ /** Severity level */
17
+ level: LogLevel;
18
+ /** Log message */
19
+ message: string;
20
+ /** Optional structured data */
21
+ data?: unknown;
22
+ /** ID of parent log entry (for hierarchical logging) */
23
+ parentLogId?: string;
24
+ }
@@ -0,0 +1,18 @@
1
+ import type { LogEntry } from './logging.js';
2
+ import type { WorkflowEvent } from './events.js';
3
+ import type { WorkflowNode } from './workflow.js';
4
+
5
+ /**
6
+ * Observer interface for subscribing to workflow events
7
+ * Observers attach to the root workflow and receive all events
8
+ */
9
+ export interface WorkflowObserver {
10
+ /** Called when a log entry is created */
11
+ onLog(entry: LogEntry): void;
12
+ /** Called when any workflow event occurs */
13
+ onEvent(event: WorkflowEvent): void;
14
+ /** Called when a node's state is updated */
15
+ onStateUpdated(node: WorkflowNode): void;
16
+ /** Called when the tree structure changes */
17
+ onTreeChanged(root: WorkflowNode): void;
18
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Prompt configuration types
3
+ * Prompts are immutable definitions of what to send to an agent
4
+ */
5
+
6
+ import type { z } from 'zod';
7
+ import type { Tool, MCPServer, Skill, AgentHooks } from './sdk-primitives.js';
8
+
9
+ /**
10
+ * Configuration for creating a Prompt instance
11
+ * @template T The expected response type (inferred from responseFormat)
12
+ */
13
+ export interface PromptConfig<T> {
14
+ /** User message content */
15
+ user: string;
16
+
17
+ /** Structured data to inject into the prompt */
18
+ data?: Record<string, unknown>;
19
+
20
+ /** Zod schema defining the expected response format */
21
+ responseFormat: z.ZodType<T>;
22
+
23
+ /** Override system prompt (takes precedence over agent config) */
24
+ system?: string;
25
+
26
+ /** Override tools (takes precedence over agent config) */
27
+ tools?: Tool[];
28
+
29
+ /** Override MCPs (takes precedence over agent config) */
30
+ mcps?: MCPServer[];
31
+
32
+ /** Override skills (takes precedence over agent config) */
33
+ skills?: Skill[];
34
+
35
+ /** Override hooks (takes precedence over agent config) */
36
+ hooks?: AgentHooks;
37
+
38
+ /** Enable reflection specifically for this prompt */
39
+ enableReflection?: boolean;
40
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Reflection types - Interfaces for multi-level reflection and self-correction
3
+ *
4
+ * Reflection allows agents to analyze errors and retry with modified approaches.
5
+ * Supports workflow, agent, and prompt level reflection with configurable limits.
6
+ */
7
+
8
+ import type { WorkflowNode } from './workflow.js';
9
+
10
+ /**
11
+ * Configuration for reflection behavior
12
+ */
13
+ export interface ReflectionConfig {
14
+ /** Whether reflection is enabled */
15
+ enabled: boolean;
16
+ /** Maximum number of reflection attempts (default: 3) */
17
+ maxAttempts: number;
18
+ /** Delay between retry attempts in milliseconds (default: 0) */
19
+ retryDelayMs?: number;
20
+ }
21
+
22
+ /**
23
+ * Context provided to the reflection process
24
+ */
25
+ export interface ReflectionContext {
26
+ /** Level at which reflection is occurring */
27
+ level: 'workflow' | 'agent' | 'prompt';
28
+ /** The workflow node that failed */
29
+ failedNode: WorkflowNode;
30
+ /** The error that triggered reflection */
31
+ error: Error;
32
+ /** Current attempt number (1-indexed) */
33
+ attemptNumber: number;
34
+ /** History of previous reflection attempts */
35
+ previousAttempts: ReflectionEntry[];
36
+ }
37
+
38
+ /**
39
+ * Result from a reflection analysis
40
+ */
41
+ export interface ReflectionResult {
42
+ /** Whether to retry the operation */
43
+ shouldRetry: boolean;
44
+ /** Revised prompt data for retry */
45
+ revisedPromptData?: Record<string, unknown>;
46
+ /** Revised system prompt for retry */
47
+ revisedSystemPrompt?: string;
48
+ /** Explanation of the reflection decision */
49
+ reason: string;
50
+ }
51
+
52
+ /**
53
+ * Record of a single reflection attempt
54
+ */
55
+ export interface ReflectionEntry {
56
+ /** Timestamp when reflection occurred */
57
+ timestamp: number;
58
+ /** Level at which reflection occurred */
59
+ level: 'workflow' | 'agent' | 'prompt';
60
+ /** Reason for the reflection */
61
+ reason: string;
62
+ /** Original error that triggered reflection */
63
+ error: Error;
64
+ /** Resolution action taken */
65
+ resolution: 'retry' | 'skip' | 'abort';
66
+ /** Whether the retry was successful */
67
+ success: boolean;
68
+ }
69
+
70
+ /**
71
+ * Full ReflectionAPI interface for workflow contexts
72
+ */
73
+ export interface ReflectionAPI {
74
+ /**
75
+ * Check if reflection is enabled
76
+ */
77
+ isEnabled(): boolean;
78
+
79
+ /**
80
+ * Trigger a reflection on the current context
81
+ * @param reason Optional reason for the reflection
82
+ */
83
+ triggerReflection(reason?: string): Promise<void>;
84
+
85
+ /**
86
+ * Get the history of reflection attempts
87
+ */
88
+ getReflectionHistory(): ReflectionEntry[];
89
+
90
+ /**
91
+ * Perform reflection analysis on an error
92
+ * @param context The reflection context with error details
93
+ * @returns Result indicating whether to retry
94
+ */
95
+ reflect(context: ReflectionContext): Promise<ReflectionResult>;
96
+ }
97
+
98
+ /**
99
+ * Default reflection configuration
100
+ */
101
+ export const DEFAULT_REFLECTION_CONFIG: ReflectionConfig = {
102
+ enabled: false,
103
+ maxAttempts: 3,
104
+ retryDelayMs: 0,
105
+ };
106
+
107
+ /**
108
+ * Create a reflection config with defaults
109
+ */
110
+ export function createReflectionConfig(
111
+ partial?: Partial<ReflectionConfig>
112
+ ): ReflectionConfig {
113
+ return {
114
+ ...DEFAULT_REFLECTION_CONFIG,
115
+ ...partial,
116
+ };
117
+ }
@@ -0,0 +1,128 @@
1
+ /**
2
+ * SDK primitive types that mirror Anthropic SDK structures
3
+ * These types pass through unchanged to the SDK
4
+ */
5
+
6
+ /**
7
+ * Tool definition for Anthropic SDK
8
+ * Maps directly to Anthropic.Tool
9
+ */
10
+ export interface Tool {
11
+ /** Tool name (must be unique within an agent) */
12
+ name: string;
13
+ /** Human-readable description of what the tool does */
14
+ description: string;
15
+ /** JSON Schema describing the tool's input parameters */
16
+ input_schema: {
17
+ type: 'object';
18
+ properties: Record<string, unknown>;
19
+ required?: string[];
20
+ };
21
+ }
22
+
23
+ /**
24
+ * Tool result returned from tool execution
25
+ */
26
+ export interface ToolResult {
27
+ type: 'tool_result';
28
+ tool_use_id: string;
29
+ content: string | unknown;
30
+ is_error?: boolean;
31
+ }
32
+
33
+ /**
34
+ * MCP Server configuration
35
+ * Supports stdio and inprocess transports
36
+ */
37
+ export interface MCPServer {
38
+ /** Server name for identification */
39
+ name: string;
40
+ /** Server version (optional) */
41
+ version?: string;
42
+ /** Transport type */
43
+ transport: 'stdio' | 'inprocess';
44
+ /** Command to run for stdio transport */
45
+ command?: string;
46
+ /** Arguments for the command */
47
+ args?: string[];
48
+ /** Tools provided by this MCP server */
49
+ tools?: Tool[];
50
+ /** Environment variables for the MCP process */
51
+ env?: Record<string, string>;
52
+ }
53
+
54
+ /**
55
+ * Skill definition
56
+ * Skills are loaded from directories containing SKILL.md
57
+ */
58
+ export interface Skill {
59
+ /** Skill name for identification */
60
+ name: string;
61
+ /** Path to skill directory containing SKILL.md */
62
+ path: string;
63
+ }
64
+
65
+ /**
66
+ * Hook handler function type
67
+ */
68
+ export type HookHandler<T = unknown> = (context: T) => Promise<void> | void;
69
+
70
+ /**
71
+ * Pre-tool use hook context
72
+ */
73
+ export interface PreToolUseContext {
74
+ toolName: string;
75
+ toolInput: unknown;
76
+ agentId: string;
77
+ }
78
+
79
+ /**
80
+ * Post-tool use hook context
81
+ */
82
+ export interface PostToolUseContext {
83
+ toolName: string;
84
+ toolInput: unknown;
85
+ toolOutput: unknown;
86
+ agentId: string;
87
+ duration: number;
88
+ }
89
+
90
+ /**
91
+ * Session start hook context
92
+ */
93
+ export interface SessionStartContext {
94
+ agentId: string;
95
+ agentName?: string;
96
+ }
97
+
98
+ /**
99
+ * Session end hook context
100
+ */
101
+ export interface SessionEndContext {
102
+ agentId: string;
103
+ agentName?: string;
104
+ totalDuration: number;
105
+ }
106
+
107
+ /**
108
+ * Agent lifecycle hooks
109
+ * Maps to Anthropic SDK hook conventions
110
+ */
111
+ export interface AgentHooks {
112
+ /** Called before each tool invocation */
113
+ preToolUse?: HookHandler<PreToolUseContext>[];
114
+ /** Called after each tool invocation */
115
+ postToolUse?: HookHandler<PostToolUseContext>[];
116
+ /** Called when agent session starts */
117
+ sessionStart?: HookHandler<SessionStartContext>[];
118
+ /** Called when agent session ends */
119
+ sessionEnd?: HookHandler<SessionEndContext>[];
120
+ }
121
+
122
+ /**
123
+ * Token usage information from API response
124
+ */
125
+ export interface TokenUsage {
126
+ input_tokens: number;
127
+ output_tokens: number;
128
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Serialized workflow state as key-value pairs
3
+ */
4
+ export type SerializedWorkflowState = Record<string, unknown>;
5
+
6
+ /**
7
+ * Metadata for observed state fields
8
+ */
9
+ export interface StateFieldMetadata {
10
+ /** If true, field is not included in snapshots */
11
+ hidden?: boolean;
12
+ /** If true, value is shown as '***' in snapshots */
13
+ redact?: boolean;
14
+ }
@@ -0,0 +1,163 @@
1
+ /**
2
+ * WorkflowContext - Interface for functional workflow execution
3
+ *
4
+ * Provides step(), spawnWorkflow(), and other methods for
5
+ * composing workflows in arbitrary JavaScript control flow.
6
+ */
7
+
8
+ import type { WorkflowNode } from './workflow.js';
9
+ import type { ReflectionAPI } from './reflection.js';
10
+
11
+ // Re-export ReflectionAPI for backward compatibility
12
+ export type { ReflectionAPI } from './reflection.js';
13
+
14
+ /**
15
+ * Handle for querying the event tree
16
+ */
17
+ export interface EventTreeHandle {
18
+ /** Root node of the event tree */
19
+ readonly root: EventNode;
20
+
21
+ /**
22
+ * Get a node by ID
23
+ * @param id Node ID to find
24
+ */
25
+ getNode(id: string): EventNode | undefined;
26
+
27
+ /**
28
+ * Get all children of a node
29
+ * @param id Parent node ID
30
+ */
31
+ getChildren(id: string): EventNode[];
32
+
33
+ /**
34
+ * Get all ancestors of a node (from node up to root)
35
+ * @param id Node ID
36
+ */
37
+ getAncestors(id: string): EventNode[];
38
+
39
+ /**
40
+ * Export tree as JSON
41
+ */
42
+ toJSON(): EventNode;
43
+ }
44
+
45
+ /**
46
+ * Event node in the queryable tree
47
+ */
48
+ export interface EventNode {
49
+ id: string;
50
+ type: string;
51
+ timestamp: number;
52
+ name?: string;
53
+ payload?: unknown;
54
+ metrics?: EventMetrics;
55
+ parentId?: string;
56
+ children: EventNode[];
57
+ }
58
+
59
+ /**
60
+ * Metrics associated with an event node
61
+ */
62
+ export interface EventMetrics {
63
+ duration?: number;
64
+ tokenUsage?: {
65
+ input: number;
66
+ output: number;
67
+ };
68
+ toolCalls?: number;
69
+ }
70
+
71
+ /**
72
+ * Agent interface for context revision (minimal to avoid circular deps)
73
+ */
74
+ export interface AgentLike {
75
+ prompt<T>(prompt: PromptLike<T>): Promise<T>;
76
+ }
77
+
78
+ /**
79
+ * Prompt interface for context revision (minimal to avoid circular deps)
80
+ */
81
+ export interface PromptLike<T> {
82
+ id: string;
83
+ buildUserMessage(): string;
84
+ validateResponse(data: unknown): T;
85
+ }
86
+
87
+ /**
88
+ * WorkflowContext - Available within functional workflow executor
89
+ */
90
+ export interface WorkflowContext {
91
+ /** Unique ID of this workflow */
92
+ readonly workflowId: string;
93
+
94
+ /** Parent workflow ID if nested */
95
+ readonly parentWorkflowId?: string;
96
+
97
+ /**
98
+ * Execute a named step
99
+ * Can be called anywhere in JavaScript control flow (loops, conditionals, etc.)
100
+ *
101
+ * @param name Step name for logging and debugging
102
+ * @param fn Step function to execute
103
+ * @returns Result of the step function
104
+ */
105
+ step<T>(name: string, fn: () => Promise<T>): Promise<T>;
106
+
107
+ /**
108
+ * Spawn a child workflow
109
+ * The child workflow is automatically attached to this workflow's tree
110
+ *
111
+ * @param workflow Workflow instance to spawn
112
+ * @returns Result of the child workflow
113
+ */
114
+ spawnWorkflow<T>(workflow: { run(): Promise<T> }): Promise<T>;
115
+
116
+ /**
117
+ * Replace the last prompt result with a new one (context revision)
118
+ * The previous prompt node is marked as 'revised' and the new result is attached as sibling
119
+ *
120
+ * @param newPrompt The new prompt to execute
121
+ * @param agent The agent to use for execution
122
+ * @returns Result of the new prompt
123
+ */
124
+ replaceLastPromptResult<T>(
125
+ newPrompt: PromptLike<T>,
126
+ agent: AgentLike
127
+ ): Promise<T>;
128
+
129
+ /**
130
+ * Access to the event tree for this workflow
131
+ */
132
+ readonly eventTree: EventTreeHandle;
133
+
134
+ /**
135
+ * Reflection API for error handling and retry logic
136
+ */
137
+ readonly reflection: ReflectionAPI;
138
+ }
139
+
140
+ /**
141
+ * Configuration for creating a functional workflow
142
+ */
143
+ export interface WorkflowConfig {
144
+ /** Human-readable workflow name */
145
+ name?: string;
146
+
147
+ /** Enable reflection for this workflow */
148
+ enableReflection?: boolean;
149
+ }
150
+
151
+ /**
152
+ * Result from workflow execution
153
+ */
154
+ export interface WorkflowResult<T = unknown> {
155
+ /** The result value */
156
+ data: T;
157
+
158
+ /** The workflow node */
159
+ node: WorkflowNode;
160
+
161
+ /** Total duration in milliseconds */
162
+ duration: number;
163
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Workflow status representing the current execution state
3
+ */
4
+ export type WorkflowStatus =
5
+ | 'idle'
6
+ | 'running'
7
+ | 'completed'
8
+ | 'failed'
9
+ | 'cancelled';
10
+
11
+ // Forward declarations - import from their respective files
12
+ import type { LogEntry } from './logging.js';
13
+ import type { WorkflowEvent } from './events.js';
14
+ import type { SerializedWorkflowState } from './snapshot.js';
15
+
16
+ /**
17
+ * Represents a node in the workflow execution tree
18
+ * This is the data structure, not the Workflow class
19
+ */
20
+ export interface WorkflowNode {
21
+ /** Unique identifier for this workflow instance */
22
+ id: string;
23
+ /** Human-readable name */
24
+ name: string;
25
+ /** Parent node reference (null for root) */
26
+ parent: WorkflowNode | null;
27
+ /** Child workflow nodes */
28
+ children: WorkflowNode[];
29
+ /** Current execution status */
30
+ status: WorkflowStatus;
31
+ /** Log entries for this node */
32
+ logs: LogEntry[];
33
+ /** Events emitted by this node */
34
+ events: WorkflowEvent[];
35
+ /** Optional serialized state snapshot */
36
+ stateSnapshot: SerializedWorkflowState | null;
37
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Generate a unique identifier
3
+ * Uses crypto.randomUUID if available, falls back to timestamp + random
4
+ */
5
+ export function generateId(): string {
6
+ if (typeof crypto !== 'undefined' && crypto.randomUUID) {
7
+ return crypto.randomUUID();
8
+ }
9
+ // Fallback for environments without crypto.randomUUID
10
+ return `${Date.now().toString(36)}-${Math.random().toString(36).substring(2, 11)}`;
11
+ }
@@ -0,0 +1,3 @@
1
+ export { generateId } from './id.js';
2
+ export { Observable } from './observable.js';
3
+ export type { Subscription, Observer } from './observable.js';
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Lightweight Observable implementation for event streaming
3
+ * No external dependencies
4
+ */
5
+ export interface Subscription {
6
+ unsubscribe(): void;
7
+ }
8
+
9
+ export interface Observer<T> {
10
+ next?: (value: T) => void;
11
+ error?: (error: unknown) => void;
12
+ complete?: () => void;
13
+ }
14
+
15
+ export class Observable<T> {
16
+ private subscribers: Set<Observer<T>> = new Set();
17
+
18
+ /**
19
+ * Subscribe to this observable
20
+ * @returns Subscription with unsubscribe method
21
+ */
22
+ subscribe(observer: Observer<T>): Subscription {
23
+ this.subscribers.add(observer);
24
+ return {
25
+ unsubscribe: () => {
26
+ this.subscribers.delete(observer);
27
+ },
28
+ };
29
+ }
30
+
31
+ /**
32
+ * Emit a value to all subscribers
33
+ */
34
+ next(value: T): void {
35
+ for (const subscriber of this.subscribers) {
36
+ try {
37
+ subscriber.next?.(value);
38
+ } catch (err) {
39
+ console.error('Observable subscriber error:', err);
40
+ }
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Signal an error to all subscribers
46
+ */
47
+ error(err: unknown): void {
48
+ for (const subscriber of this.subscribers) {
49
+ try {
50
+ subscriber.error?.(err);
51
+ } catch (e) {
52
+ console.error('Observable error handler failed:', e);
53
+ }
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Signal completion to all subscribers
59
+ */
60
+ complete(): void {
61
+ for (const subscriber of this.subscribers) {
62
+ try {
63
+ subscriber.complete?.();
64
+ } catch (err) {
65
+ console.error('Observable complete handler failed:', err);
66
+ }
67
+ }
68
+ this.subscribers.clear();
69
+ }
70
+
71
+ /**
72
+ * Get current subscriber count
73
+ */
74
+ get subscriberCount(): number {
75
+ return this.subscribers.size;
76
+ }
77
+ }
package/tasks.json ADDED
File without changes