@memgrafter/flatagents 0.9.0 → 2.0.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/MACHINES.md CHANGED
@@ -22,7 +22,7 @@
22
22
  ```yaml
23
23
  # profiles.yml — agents reference by name
24
24
  spec: flatprofiles
25
- spec_version: "0.9.0"
25
+ spec_version: "2.0.0"
26
26
  data:
27
27
  model_profiles:
28
28
  fast: { provider: cerebras, name: zai-glm-4.6, temperature: 0.6 }
@@ -34,6 +34,13 @@ data:
34
34
  Agent model field: `"fast"` | `{ profile: "fast", temperature: 0.9 }` | `{ provider: x, name: y }`
35
35
  Resolution: default → profile → overrides → override
36
36
 
37
+ ## Agent References
38
+
39
+ `data.agents` values may be:
40
+ - String path to a flatagent config
41
+ - Inline flatagent config (`spec: flatagent`)
42
+ - Typed adapter ref: `{ type: "flatagent" | "smolagents" | "pi-agent", ref?: "...", config?: {...} }`
43
+
37
44
  ## State Fields
38
45
 
39
46
  | Field | Purpose |
@@ -49,6 +56,7 @@ Resolution: default → profile → overrides → override
49
56
  | `on_error` | State name or `{ default: x, ErrorType: y }` |
50
57
  | `transitions` | `[{ condition: "expr", to: state }, { to: default }]` |
51
58
  | `mode` | `settled` (all) / `any` (first) for parallel |
59
+ | `wait_for` | Channel to wait for external signal (Jinja2 template) |
52
60
  | `timeout` | Seconds (0=forever) |
53
61
 
54
62
  ## Patterns
@@ -80,6 +88,66 @@ launch: background_task
80
88
  launch_input: { data: "{{ context.data }}" }
81
89
  ```
82
90
 
91
+ **Wait for signal** (checkpoint, exit, resume on signal):
92
+ ```yaml
93
+ wait_for_approval:
94
+ wait_for: "approval/{{ context.task_id }}"
95
+ timeout: 86400
96
+ output_to_context:
97
+ approved: "{{ output.approved }}"
98
+ transitions:
99
+ - condition: "context.approved"
100
+ to: continue
101
+ - to: rejected
102
+ ```
103
+
104
+ ## Distributed Worker Pattern
105
+
106
+ Use hook actions (e.g., `DistributedWorkerHooks`) with a `RegistrationBackend` + `WorkBackend` to build worker pools.
107
+
108
+ **Core machines**
109
+ - **Checker**: `get_pool_state` → `calculate_spawn` → `spawn_workers`
110
+ - **Worker**: `register_worker` → `claim_job` → process → `complete_job`/`fail_job` → `deregister_worker`
111
+ - **Reaper**: `list_stale_workers` → `reap_stale_workers`
112
+
113
+ `spawn_workers` expects `worker_config_path` in context (or override hooks to resolve it). Custom queues can compose the base hooks and add actions.
114
+
115
+ ```yaml
116
+ context:
117
+ worker_config_path: "./job_worker.yml"
118
+ states:
119
+ check_state: { action: get_pool_state }
120
+ calculate_spawn: { action: calculate_spawn }
121
+ spawn_workers: { action: spawn_workers }
122
+ ```
123
+
124
+ See `sdk/examples/distributed_worker/` for a full example.
125
+
126
+ ## Signals & Triggers
127
+
128
+ Machine pauses at `wait_for` state → checkpoints with `waiting_channel` → process exits. Nothing running.
129
+
130
+ **Signal delivery**: External process writes signal → trigger fires → dispatcher resumes matching machines.
131
+
132
+ ```
133
+ send("approval/task-001", {approved: true})
134
+ → SQLite INSERT + touch trigger file
135
+ → launchd/systemd starts dispatcher
136
+ → dispatcher queries checkpoints WHERE waiting_channel = "approval/task-001"
137
+ → resumes machine from checkpoint, signal data as output.*
138
+ ```
139
+
140
+ **Channel semantics**:
141
+ - Addressed: `"approval/{{ context.task_id }}"` → one waiter
142
+ - Broadcast: `"quota/openai"` → N waiters (dispatcher controls limit)
143
+
144
+ **10,000 waiting machines** = 10,000 rows in SQLite. Zero processes, zero memory.
145
+
146
+ **Trigger backends**: `none` (polling), `file` (launchd/systemd — nothing running), `socket` (UDS, in-process).
147
+ DynamoDB Streams is implicit (no trigger code needed).
148
+
149
+ **Signal backends**: `memory` (testing), `sqlite` (local durable), `dynamodb` (AWS).
150
+
83
151
  ## Context Variables
84
152
 
85
153
  `context.*` (all states), `input.*` (initial), `output.*` (in output_to_context), `item`/`as` (foreach)
@@ -101,3 +169,12 @@ class MyHooks(MachineHooks):
101
169
  persistence: { enabled: true, backend: local } # local | memory
102
170
  ```
103
171
  Resume: `machine.execute(resume_from=execution_id)`
172
+
173
+ ## SDKs
174
+
175
+ ### Python SDKs
176
+ - **flatagents** (agents): `pip install flatagents[litellm]`
177
+ - **flatmachines** (orchestration): `pip install flatmachines[flatagents]`
178
+
179
+ ### JavaScript SDK
180
+ A single JS SDK lives under [`sdk/js`](./sdk/js). It follows the same specs but is not yet split into separate FlatAgents/FlatMachines packages.
package/dist/index.d.mts CHANGED
@@ -1,3 +1,39 @@
1
+ declare class WebhookHooks implements MachineHooks {
2
+ private url;
3
+ constructor(url: string);
4
+ private send;
5
+ onMachineStart(context: Record<string, any>): Promise<Record<string, any>>;
6
+ onMachineEnd(context: Record<string, any>, output: any): Promise<any>;
7
+ onStateEnter(state: string, context: Record<string, any>): Promise<Record<string, any>>;
8
+ onStateExit(state: string, context: Record<string, any>, output: any): Promise<any>;
9
+ onAction(action: string, context: Record<string, any>): Promise<Record<string, any>>;
10
+ }
11
+ declare class CompositeHooks implements MachineHooks {
12
+ private hooks;
13
+ constructor(hooks: MachineHooks[]);
14
+ onMachineStart(context: Record<string, any>): Promise<Record<string, any>>;
15
+ onMachineEnd(context: Record<string, any>, output: any): Promise<any>;
16
+ onStateEnter(state: string, context: Record<string, any>): Promise<Record<string, any>>;
17
+ onStateExit(state: string, context: Record<string, any>, output: any): Promise<any>;
18
+ onTransition(from: string, to: string, context: Record<string, any>): Promise<string>;
19
+ onError(state: string, error: Error, context: Record<string, any>): Promise<string | null>;
20
+ onAction(action: string, context: Record<string, any>): Promise<Record<string, any>>;
21
+ }
22
+ /**
23
+ * Name-based registry for resolving hooks from machine config.
24
+ *
25
+ * Machine configs reference hooks by name (e.g., hooks: "my-hooks").
26
+ * The registry maps names to factory classes/functions and resolves
27
+ * them at runtime, keeping configs language-agnostic.
28
+ */
29
+ declare class HooksRegistry {
30
+ private factories;
31
+ register(name: string, factory: HooksFactory): void;
32
+ has(name: string): boolean;
33
+ resolve(ref: HooksRef): MachineHooks;
34
+ private resolveSingle;
35
+ }
36
+
1
37
  /**
2
38
  * Model configuration for an agent.
3
39
  * Can be inline config, profile reference, or profile with overrides.
@@ -200,9 +236,18 @@ interface ResultBackend {
200
236
  exists(uri: string): Promise<boolean>;
201
237
  delete(uri: string): Promise<void>;
202
238
  }
239
+ type HooksRef = string | HooksRefConfig | Array<string | HooksRefConfig>;
240
+ interface HooksRefConfig {
241
+ name: string;
242
+ args?: Record<string, any>;
243
+ }
244
+ type HooksFactory = {
245
+ new (args?: Record<string, any>): MachineHooks;
246
+ } | ((args?: Record<string, any>) => MachineHooks);
203
247
  interface MachineOptions {
204
248
  config: MachineConfig | string;
205
249
  hooks?: MachineHooks;
250
+ hooksRegistry?: HooksRegistry;
206
251
  persistence?: PersistenceBackend;
207
252
  resultBackend?: ResultBackend;
208
253
  executionLock?: ExecutionLock;
@@ -409,6 +454,7 @@ declare class FlatMachine {
409
454
  private context;
410
455
  private input;
411
456
  private hooks?;
457
+ private _hooksRegistry;
412
458
  private checkpointManager?;
413
459
  private resultBackend?;
414
460
  private executionLock;
@@ -420,6 +466,8 @@ declare class FlatMachine {
420
466
  private currentState?;
421
467
  private currentStep;
422
468
  constructor(options: MachineOptions);
469
+ get hooksRegistry(): HooksRegistry;
470
+ private resolveHooks;
423
471
  execute(input?: Record<string, any>, resumeSnapshot?: MachineSnapshot): Promise<any>;
424
472
  private executeInternal;
425
473
  resume(executionId: string): Promise<any>;
@@ -574,28 +622,6 @@ declare class MDAPVotingExecution implements ExecutionType {
574
622
  }
575
623
  declare function getExecutionType(config?: ExecutionConfig): ExecutionType;
576
624
 
577
- declare class WebhookHooks implements MachineHooks {
578
- private url;
579
- constructor(url: string);
580
- private send;
581
- onMachineStart(context: Record<string, any>): Promise<Record<string, any>>;
582
- onMachineEnd(context: Record<string, any>, output: any): Promise<any>;
583
- onStateEnter(state: string, context: Record<string, any>): Promise<Record<string, any>>;
584
- onStateExit(state: string, context: Record<string, any>, output: any): Promise<any>;
585
- onAction(action: string, context: Record<string, any>): Promise<Record<string, any>>;
586
- }
587
- declare class CompositeHooks implements MachineHooks {
588
- private hooks;
589
- constructor(hooks: MachineHooks[]);
590
- onMachineStart(context: Record<string, any>): Promise<Record<string, any>>;
591
- onMachineEnd(context: Record<string, any>, output: any): Promise<any>;
592
- onStateEnter(state: string, context: Record<string, any>): Promise<Record<string, any>>;
593
- onStateExit(state: string, context: Record<string, any>, output: any): Promise<any>;
594
- onTransition(from: string, to: string, context: Record<string, any>): Promise<string>;
595
- onError(state: string, error: Error, context: Record<string, any>): Promise<string | null>;
596
- onAction(action: string, context: Record<string, any>): Promise<Record<string, any>>;
597
- }
598
-
599
625
  declare class MemoryBackend implements PersistenceBackend {
600
626
  private store;
601
627
  save(key: string, snapshot: MachineSnapshot): Promise<void>;
@@ -670,4 +696,4 @@ declare class LocalFileLock implements ExecutionLock {
670
696
  release(key: string): Promise<void>;
671
697
  }
672
698
 
673
- export { type AgentConfig, type AgentOptions, type BackendConfig, CheckpointManager, CompositeHooks, DefaultExecution, type ExecutionConfig, type ExecutionLock, type ExecutionType, FlatAgent, FlatMachine, type LLMBackend, type LLMBackendConfig, type LLMOptions, LocalFileBackend, LocalFileLock, type MCPServer, MCPToolProvider, MDAPVotingExecution, type MachineConfig, type MachineHooks, type MachineOptions, type MachineSnapshot, MemoryBackend, type Message, MockLLMBackend, type MockResponse, type ModelConfig, type ModelProfileConfig, NoOpLock, ParallelExecution, type PersistenceBackend, ProfileManager, type ProfiledModelConfig, type ProfilesConfig, type ResultBackend, RetryExecution, type State, type TemplateAllowlist, type ToolCall, type ToolDefinition, type ToolFilter, VercelAIBackend, WebhookHooks, evaluate, getExecutionType, inMemoryResultBackend, resolveModelConfig, setTemplateAllowlist };
699
+ export { type AgentConfig, type AgentOptions, type BackendConfig, CheckpointManager, CompositeHooks, DefaultExecution, type ExecutionConfig, type ExecutionLock, type ExecutionType, FlatAgent, FlatMachine, type HooksFactory, type HooksRef, type HooksRefConfig, HooksRegistry, type LLMBackend, type LLMBackendConfig, type LLMOptions, LocalFileBackend, LocalFileLock, type MCPServer, MCPToolProvider, MDAPVotingExecution, type MachineConfig, type MachineHooks, type MachineOptions, type MachineSnapshot, MemoryBackend, type Message, MockLLMBackend, type MockResponse, type ModelConfig, type ModelProfileConfig, NoOpLock, ParallelExecution, type PersistenceBackend, ProfileManager, type ProfiledModelConfig, type ProfilesConfig, type ResultBackend, RetryExecution, type State, type TemplateAllowlist, type ToolCall, type ToolDefinition, type ToolFilter, VercelAIBackend, WebhookHooks, evaluate, getExecutionType, inMemoryResultBackend, resolveModelConfig, setTemplateAllowlist };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,39 @@
1
+ declare class WebhookHooks implements MachineHooks {
2
+ private url;
3
+ constructor(url: string);
4
+ private send;
5
+ onMachineStart(context: Record<string, any>): Promise<Record<string, any>>;
6
+ onMachineEnd(context: Record<string, any>, output: any): Promise<any>;
7
+ onStateEnter(state: string, context: Record<string, any>): Promise<Record<string, any>>;
8
+ onStateExit(state: string, context: Record<string, any>, output: any): Promise<any>;
9
+ onAction(action: string, context: Record<string, any>): Promise<Record<string, any>>;
10
+ }
11
+ declare class CompositeHooks implements MachineHooks {
12
+ private hooks;
13
+ constructor(hooks: MachineHooks[]);
14
+ onMachineStart(context: Record<string, any>): Promise<Record<string, any>>;
15
+ onMachineEnd(context: Record<string, any>, output: any): Promise<any>;
16
+ onStateEnter(state: string, context: Record<string, any>): Promise<Record<string, any>>;
17
+ onStateExit(state: string, context: Record<string, any>, output: any): Promise<any>;
18
+ onTransition(from: string, to: string, context: Record<string, any>): Promise<string>;
19
+ onError(state: string, error: Error, context: Record<string, any>): Promise<string | null>;
20
+ onAction(action: string, context: Record<string, any>): Promise<Record<string, any>>;
21
+ }
22
+ /**
23
+ * Name-based registry for resolving hooks from machine config.
24
+ *
25
+ * Machine configs reference hooks by name (e.g., hooks: "my-hooks").
26
+ * The registry maps names to factory classes/functions and resolves
27
+ * them at runtime, keeping configs language-agnostic.
28
+ */
29
+ declare class HooksRegistry {
30
+ private factories;
31
+ register(name: string, factory: HooksFactory): void;
32
+ has(name: string): boolean;
33
+ resolve(ref: HooksRef): MachineHooks;
34
+ private resolveSingle;
35
+ }
36
+
1
37
  /**
2
38
  * Model configuration for an agent.
3
39
  * Can be inline config, profile reference, or profile with overrides.
@@ -200,9 +236,18 @@ interface ResultBackend {
200
236
  exists(uri: string): Promise<boolean>;
201
237
  delete(uri: string): Promise<void>;
202
238
  }
239
+ type HooksRef = string | HooksRefConfig | Array<string | HooksRefConfig>;
240
+ interface HooksRefConfig {
241
+ name: string;
242
+ args?: Record<string, any>;
243
+ }
244
+ type HooksFactory = {
245
+ new (args?: Record<string, any>): MachineHooks;
246
+ } | ((args?: Record<string, any>) => MachineHooks);
203
247
  interface MachineOptions {
204
248
  config: MachineConfig | string;
205
249
  hooks?: MachineHooks;
250
+ hooksRegistry?: HooksRegistry;
206
251
  persistence?: PersistenceBackend;
207
252
  resultBackend?: ResultBackend;
208
253
  executionLock?: ExecutionLock;
@@ -409,6 +454,7 @@ declare class FlatMachine {
409
454
  private context;
410
455
  private input;
411
456
  private hooks?;
457
+ private _hooksRegistry;
412
458
  private checkpointManager?;
413
459
  private resultBackend?;
414
460
  private executionLock;
@@ -420,6 +466,8 @@ declare class FlatMachine {
420
466
  private currentState?;
421
467
  private currentStep;
422
468
  constructor(options: MachineOptions);
469
+ get hooksRegistry(): HooksRegistry;
470
+ private resolveHooks;
423
471
  execute(input?: Record<string, any>, resumeSnapshot?: MachineSnapshot): Promise<any>;
424
472
  private executeInternal;
425
473
  resume(executionId: string): Promise<any>;
@@ -574,28 +622,6 @@ declare class MDAPVotingExecution implements ExecutionType {
574
622
  }
575
623
  declare function getExecutionType(config?: ExecutionConfig): ExecutionType;
576
624
 
577
- declare class WebhookHooks implements MachineHooks {
578
- private url;
579
- constructor(url: string);
580
- private send;
581
- onMachineStart(context: Record<string, any>): Promise<Record<string, any>>;
582
- onMachineEnd(context: Record<string, any>, output: any): Promise<any>;
583
- onStateEnter(state: string, context: Record<string, any>): Promise<Record<string, any>>;
584
- onStateExit(state: string, context: Record<string, any>, output: any): Promise<any>;
585
- onAction(action: string, context: Record<string, any>): Promise<Record<string, any>>;
586
- }
587
- declare class CompositeHooks implements MachineHooks {
588
- private hooks;
589
- constructor(hooks: MachineHooks[]);
590
- onMachineStart(context: Record<string, any>): Promise<Record<string, any>>;
591
- onMachineEnd(context: Record<string, any>, output: any): Promise<any>;
592
- onStateEnter(state: string, context: Record<string, any>): Promise<Record<string, any>>;
593
- onStateExit(state: string, context: Record<string, any>, output: any): Promise<any>;
594
- onTransition(from: string, to: string, context: Record<string, any>): Promise<string>;
595
- onError(state: string, error: Error, context: Record<string, any>): Promise<string | null>;
596
- onAction(action: string, context: Record<string, any>): Promise<Record<string, any>>;
597
- }
598
-
599
625
  declare class MemoryBackend implements PersistenceBackend {
600
626
  private store;
601
627
  save(key: string, snapshot: MachineSnapshot): Promise<void>;
@@ -670,4 +696,4 @@ declare class LocalFileLock implements ExecutionLock {
670
696
  release(key: string): Promise<void>;
671
697
  }
672
698
 
673
- export { type AgentConfig, type AgentOptions, type BackendConfig, CheckpointManager, CompositeHooks, DefaultExecution, type ExecutionConfig, type ExecutionLock, type ExecutionType, FlatAgent, FlatMachine, type LLMBackend, type LLMBackendConfig, type LLMOptions, LocalFileBackend, LocalFileLock, type MCPServer, MCPToolProvider, MDAPVotingExecution, type MachineConfig, type MachineHooks, type MachineOptions, type MachineSnapshot, MemoryBackend, type Message, MockLLMBackend, type MockResponse, type ModelConfig, type ModelProfileConfig, NoOpLock, ParallelExecution, type PersistenceBackend, ProfileManager, type ProfiledModelConfig, type ProfilesConfig, type ResultBackend, RetryExecution, type State, type TemplateAllowlist, type ToolCall, type ToolDefinition, type ToolFilter, VercelAIBackend, WebhookHooks, evaluate, getExecutionType, inMemoryResultBackend, resolveModelConfig, setTemplateAllowlist };
699
+ export { type AgentConfig, type AgentOptions, type BackendConfig, CheckpointManager, CompositeHooks, DefaultExecution, type ExecutionConfig, type ExecutionLock, type ExecutionType, FlatAgent, FlatMachine, type HooksFactory, type HooksRef, type HooksRefConfig, HooksRegistry, type LLMBackend, type LLMBackendConfig, type LLMOptions, LocalFileBackend, LocalFileLock, type MCPServer, MCPToolProvider, MDAPVotingExecution, type MachineConfig, type MachineHooks, type MachineOptions, type MachineSnapshot, MemoryBackend, type Message, MockLLMBackend, type MockResponse, type ModelConfig, type ModelProfileConfig, NoOpLock, ParallelExecution, type PersistenceBackend, ProfileManager, type ProfiledModelConfig, type ProfilesConfig, type ResultBackend, RetryExecution, type State, type TemplateAllowlist, type ToolCall, type ToolDefinition, type ToolFilter, VercelAIBackend, WebhookHooks, evaluate, getExecutionType, inMemoryResultBackend, resolveModelConfig, setTemplateAllowlist };