flatmachines 1.0.0__py3-none-any.whl

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 (41) hide show
  1. flatmachines/__init__.py +136 -0
  2. flatmachines/actions.py +408 -0
  3. flatmachines/adapters/__init__.py +38 -0
  4. flatmachines/adapters/flatagent.py +86 -0
  5. flatmachines/adapters/pi_agent_bridge.py +127 -0
  6. flatmachines/adapters/pi_agent_runner.mjs +99 -0
  7. flatmachines/adapters/smolagents.py +125 -0
  8. flatmachines/agents.py +144 -0
  9. flatmachines/assets/MACHINES.md +141 -0
  10. flatmachines/assets/README.md +11 -0
  11. flatmachines/assets/__init__.py +0 -0
  12. flatmachines/assets/flatagent.d.ts +219 -0
  13. flatmachines/assets/flatagent.schema.json +271 -0
  14. flatmachines/assets/flatagent.slim.d.ts +58 -0
  15. flatmachines/assets/flatagents-runtime.d.ts +523 -0
  16. flatmachines/assets/flatagents-runtime.schema.json +281 -0
  17. flatmachines/assets/flatagents-runtime.slim.d.ts +187 -0
  18. flatmachines/assets/flatmachine.d.ts +403 -0
  19. flatmachines/assets/flatmachine.schema.json +620 -0
  20. flatmachines/assets/flatmachine.slim.d.ts +106 -0
  21. flatmachines/assets/profiles.d.ts +140 -0
  22. flatmachines/assets/profiles.schema.json +93 -0
  23. flatmachines/assets/profiles.slim.d.ts +26 -0
  24. flatmachines/backends.py +222 -0
  25. flatmachines/distributed.py +835 -0
  26. flatmachines/distributed_hooks.py +351 -0
  27. flatmachines/execution.py +638 -0
  28. flatmachines/expressions/__init__.py +60 -0
  29. flatmachines/expressions/cel.py +101 -0
  30. flatmachines/expressions/simple.py +166 -0
  31. flatmachines/flatmachine.py +1263 -0
  32. flatmachines/hooks.py +381 -0
  33. flatmachines/locking.py +69 -0
  34. flatmachines/monitoring.py +505 -0
  35. flatmachines/persistence.py +213 -0
  36. flatmachines/run.py +117 -0
  37. flatmachines/utils.py +166 -0
  38. flatmachines/validation.py +79 -0
  39. flatmachines-1.0.0.dist-info/METADATA +390 -0
  40. flatmachines-1.0.0.dist-info/RECORD +41 -0
  41. flatmachines-1.0.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,281 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$ref": "#/definitions/SDKRuntimeWrapper",
4
+ "definitions": {
5
+ "SDKRuntimeWrapper": {
6
+ "type": "object",
7
+ "properties": {
8
+ "spec": {
9
+ "type": "string",
10
+ "const": "flatagents-runtime"
11
+ },
12
+ "spec_version": {
13
+ "type": "string",
14
+ "const": "1.0.0"
15
+ },
16
+ "execution_lock": {
17
+ "$ref": "#/definitions/ExecutionLock"
18
+ },
19
+ "persistence_backend": {
20
+ "$ref": "#/definitions/PersistenceBackend"
21
+ },
22
+ "result_backend": {
23
+ "$ref": "#/definitions/ResultBackend"
24
+ },
25
+ "execution_config": {
26
+ "$ref": "#/definitions/ExecutionConfig"
27
+ },
28
+ "machine_hooks": {
29
+ "$ref": "#/definitions/MachineHooks"
30
+ },
31
+ "llm_backend": {
32
+ "$ref": "#/definitions/LLMBackend"
33
+ },
34
+ "machine_invoker": {
35
+ "$ref": "#/definitions/MachineInvoker"
36
+ },
37
+ "backend_config": {
38
+ "$ref": "#/definitions/BackendConfig"
39
+ },
40
+ "machine_snapshot": {
41
+ "$ref": "#/definitions/MachineSnapshot"
42
+ },
43
+ "registration_backend": {
44
+ "$ref": "#/definitions/RegistrationBackend"
45
+ },
46
+ "work_backend": {
47
+ "$ref": "#/definitions/WorkBackend"
48
+ }
49
+ },
50
+ "required": [
51
+ "spec",
52
+ "spec_version"
53
+ ],
54
+ "additionalProperties": false,
55
+ "description": "Wrapper interface for JSON schema generation. Groups all runtime interfaces that SDKs must implement."
56
+ },
57
+ "ExecutionLock": {
58
+ "type": "object",
59
+ "additionalProperties": false,
60
+ "description": "FlatAgents Runtime Interface Spec ==================================\n\nThis file defines the runtime interfaces that SDKs MUST implement to be considered compliant. These are NOT configuration schemas (see flatagent.d.ts and flatmachine.d.ts for those).\n\nREQUIRED IMPLEMENTATIONS:\n------------------------- - ExecutionLock: NoOpLock (MUST), LocalFileLock (SHOULD) - PersistenceBackend: MemoryBackend (MUST), LocalFileBackend (SHOULD) - ResultBackend: InMemoryResultBackend (MUST) - ExecutionType: Default, Retry, Parallel, MDAPVoting (MUST) - MachineHooks: Base interface (MUST) - RegistrationBackend: SQLiteRegistrationBackend (MUST), MemoryRegistrationBackend (SHOULD) - WorkBackend: SQLiteWorkBackend (MUST), MemoryWorkBackend (SHOULD)\n\nOPTIONAL IMPLEMENTATIONS:\n------------------------- - Distributed backends (Redis, Postgres, etc.) - LLMBackend (SDK may use native provider SDKs)\n\nEXECUTION LOCKING:\n------------------ Prevents concurrent execution of the same machine instance.\n\nSDKs MUST provide: - NoOpLock: For when locking is handled externally or disabled\n\nSDKs SHOULD provide: - LocalFileLock: For single-node deployments using fcntl/flock\n\nDistributed deployments should implement Redis/Consul/etcd locks.\n\nPERSISTENCE BACKEND:\n-------------------- Storage backend for machine checkpoints.\n\nSDKs MUST provide: - MemoryBackend: For testing and ephemeral runs\n\nSDKs SHOULD provide: - LocalFileBackend: For durable local storage with atomic writes\n\nRESULT BACKEND:\n--------------- Inter-machine communication via URI-addressed results.\n\nURI format: flatagents://{execution_id}/{path} - path is typically \"result\" or \"checkpoint\"\n\nSDKs MUST provide: - InMemoryResultBackend: For single-process execution\n\nEXECUTION TYPES:\n---------------- Execution strategy for agent calls.\n\nSDKs MUST implement all four types: - default: Single call, no retry - retry: Configurable backoffs with jitter - parallel: Run N samples, return all successes - mdap_voting: Multi-sample with consensus voting\n\nMACHINE HOOKS:\n-------------- Extension points for machine execution. All methods are optional and can be sync or async.\n\nSDKs SHOULD provide: - WebhookHooks: Send events to HTTP endpoint - CompositeHooks: Combine multiple hook implementations\n\nLLM BACKEND (OPTIONAL):\n----------------------- Abstraction over LLM providers.\n\nThis interface is OPTIONAL - SDKs may use provider SDKs directly. Useful for: - Unified retry/monitoring across providers - Provider-agnostic code - Testing with mock backends\n\nMACHINE INVOKER:\n---------------- Interface for invoking peer machines. Used internally by FlatMachine for `machine:` and `launch:` states.\n\nBACKEND CONFIGURATION:\n---------------------- Backend configuration for machine settings.\n\nExample in YAML: settings: backends: persistence: local locking: none results: memory"
61
+ },
62
+ "PersistenceBackend": {
63
+ "type": "object",
64
+ "additionalProperties": false
65
+ },
66
+ "ResultBackend": {
67
+ "type": "object",
68
+ "additionalProperties": false
69
+ },
70
+ "ExecutionConfig": {
71
+ "type": "object",
72
+ "properties": {
73
+ "type": {
74
+ "type": "string",
75
+ "enum": [
76
+ "default",
77
+ "retry",
78
+ "parallel",
79
+ "mdap_voting"
80
+ ]
81
+ },
82
+ "backoffs": {
83
+ "type": "array",
84
+ "items": {
85
+ "type": "number"
86
+ }
87
+ },
88
+ "jitter": {
89
+ "type": "number"
90
+ },
91
+ "n_samples": {
92
+ "type": "number"
93
+ },
94
+ "k_margin": {
95
+ "type": "number"
96
+ },
97
+ "max_candidates": {
98
+ "type": "number"
99
+ }
100
+ },
101
+ "required": [
102
+ "type"
103
+ ],
104
+ "additionalProperties": false
105
+ },
106
+ "MachineHooks": {
107
+ "type": "object",
108
+ "additionalProperties": false
109
+ },
110
+ "LLMBackend": {
111
+ "type": "object",
112
+ "properties": {
113
+ "totalCost": {
114
+ "type": "number",
115
+ "description": "Total cost accumulated across all calls."
116
+ },
117
+ "totalApiCalls": {
118
+ "type": "number",
119
+ "description": "Total API calls made."
120
+ }
121
+ },
122
+ "required": [
123
+ "totalCost",
124
+ "totalApiCalls"
125
+ ],
126
+ "additionalProperties": false
127
+ },
128
+ "MachineInvoker": {
129
+ "type": "object",
130
+ "additionalProperties": false
131
+ },
132
+ "BackendConfig": {
133
+ "type": "object",
134
+ "properties": {
135
+ "persistence": {
136
+ "type": "string",
137
+ "enum": [
138
+ "memory",
139
+ "local",
140
+ "redis",
141
+ "postgres",
142
+ "s3"
143
+ ],
144
+ "description": "Checkpoint storage. Default: memory"
145
+ },
146
+ "locking": {
147
+ "type": "string",
148
+ "enum": [
149
+ "none",
150
+ "local",
151
+ "redis",
152
+ "consul"
153
+ ],
154
+ "description": "Execution locking. Default: none"
155
+ },
156
+ "results": {
157
+ "type": "string",
158
+ "enum": [
159
+ "memory",
160
+ "redis"
161
+ ],
162
+ "description": "Inter-machine results. Default: memory"
163
+ },
164
+ "registration": {
165
+ "type": "string",
166
+ "enum": [
167
+ "memory",
168
+ "sqlite",
169
+ "redis"
170
+ ],
171
+ "description": "Worker registration. Default: memory"
172
+ },
173
+ "work": {
174
+ "type": "string",
175
+ "enum": [
176
+ "memory",
177
+ "sqlite",
178
+ "redis"
179
+ ],
180
+ "description": "Work pool. Default: memory"
181
+ },
182
+ "sqlite_path": {
183
+ "type": "string",
184
+ "description": "Path for sqlite backends (registration and work share this)"
185
+ }
186
+ },
187
+ "additionalProperties": false
188
+ },
189
+ "MachineSnapshot": {
190
+ "type": "object",
191
+ "properties": {
192
+ "execution_id": {
193
+ "type": "string"
194
+ },
195
+ "machine_name": {
196
+ "type": "string"
197
+ },
198
+ "spec_version": {
199
+ "type": "string"
200
+ },
201
+ "current_state": {
202
+ "type": "string"
203
+ },
204
+ "context": {
205
+ "type": "object"
206
+ },
207
+ "step": {
208
+ "type": "number"
209
+ },
210
+ "created_at": {
211
+ "type": "string"
212
+ },
213
+ "event": {
214
+ "type": "string"
215
+ },
216
+ "output": {
217
+ "type": "object"
218
+ },
219
+ "total_api_calls": {
220
+ "type": "number"
221
+ },
222
+ "total_cost": {
223
+ "type": "number"
224
+ },
225
+ "parent_execution_id": {
226
+ "type": "string"
227
+ },
228
+ "pending_launches": {
229
+ "type": "array",
230
+ "items": {
231
+ "$ref": "#/definitions/LaunchIntent"
232
+ }
233
+ }
234
+ },
235
+ "required": [
236
+ "execution_id",
237
+ "machine_name",
238
+ "spec_version",
239
+ "current_state",
240
+ "context",
241
+ "step",
242
+ "created_at"
243
+ ],
244
+ "additionalProperties": false
245
+ },
246
+ "LaunchIntent": {
247
+ "type": "object",
248
+ "properties": {
249
+ "execution_id": {
250
+ "type": "string"
251
+ },
252
+ "machine": {
253
+ "type": "string"
254
+ },
255
+ "input": {
256
+ "type": "object"
257
+ },
258
+ "launched": {
259
+ "type": "boolean"
260
+ }
261
+ },
262
+ "required": [
263
+ "execution_id",
264
+ "machine",
265
+ "input",
266
+ "launched"
267
+ ],
268
+ "additionalProperties": false
269
+ },
270
+ "RegistrationBackend": {
271
+ "type": "object",
272
+ "additionalProperties": false,
273
+ "description": "REGISTRATION BACKEND:\n--------------------- Worker lifecycle management for distributed execution.\n\nSDKs MUST provide: - SQLiteRegistrationBackend: For local deployments\n\nSDKs SHOULD provide: - MemoryRegistrationBackend: For testing\n\nImplementation notes: - Time units: Python reference SDK uses seconds for all interval values - Stale threshold: SDKs SHOULD default to 2× heartbeat_interval if not specified"
274
+ },
275
+ "WorkBackend": {
276
+ "type": "object",
277
+ "additionalProperties": false,
278
+ "description": "WORK BACKEND:\n------------- Work distribution via named pools with atomic claim.\n\nSDKs MUST provide: - SQLiteWorkBackend: For local deployments\n\nSDKs SHOULD provide: - MemoryWorkBackend: For testing\n\nImplementation notes: - Atomic claim: SDKs MUST ensure no two workers can claim the same job - Test requirements: Include concurrent claim race condition tests"
279
+ }
280
+ }
281
+ }
@@ -0,0 +1,187 @@
1
+ export interface ExecutionLock {
2
+ acquire(key: string): Promise<boolean>;
3
+ release(key: string): Promise<void>;
4
+ }
5
+ export interface PersistenceBackend {
6
+ save(key: string, snapshot: MachineSnapshot): Promise<void>;
7
+ load(key: string): Promise<MachineSnapshot | null>;
8
+ delete(key: string): Promise<void>;
9
+ list(prefix: string): Promise<string[]>;
10
+ }
11
+ export interface ResultBackend {
12
+ write(uri: string, data: any): Promise<void>;
13
+ read(uri: string, options?: {
14
+ block?: boolean;
15
+ timeout?: number;
16
+ }): Promise<any>;
17
+ exists(uri: string): Promise<boolean>;
18
+ delete(uri: string): Promise<void>;
19
+ }
20
+ export interface AgentResult {
21
+ output?: Record<string, any> | null;
22
+ content?: string | null;
23
+ raw?: any;
24
+ usage?: Record<string, any> | null;
25
+ cost?: number | null;
26
+ metadata?: Record<string, any> | null;
27
+ }
28
+ export interface AgentExecutor {
29
+ execute(input: Record<string, any>, context?: Record<string, any>): Promise<AgentResult>;
30
+ metadata?: Record<string, any>;
31
+ }
32
+ export interface ExecutionType {
33
+ execute(executor: AgentExecutor, input: Record<string, any>, context?: Record<string, any>): Promise<AgentResult>;
34
+ }
35
+ export interface ExecutionConfig {
36
+ type: "default" | "retry" | "parallel" | "mdap_voting";
37
+ backoffs?: number[];
38
+ jitter?: number;
39
+ n_samples?: number;
40
+ k_margin?: number;
41
+ max_candidates?: number;
42
+ }
43
+ export interface MachineHooks {
44
+ onMachineStart?(context: Record<string, any>): Record<string, any> | Promise<Record<string, any>>;
45
+ onMachineEnd?(context: Record<string, any>, output: any): any | Promise<any>;
46
+ onStateEnter?(state: string, context: Record<string, any>): Record<string, any> | Promise<Record<string, any>>;
47
+ onStateExit?(state: string, context: Record<string, any>, output: any): any | Promise<any>;
48
+ onTransition?(from: string, to: string, context: Record<string, any>): string | Promise<string>;
49
+ onError?(state: string, error: Error, context: Record<string, any>): string | null | Promise<string | null>;
50
+ onAction?(action: string, context: Record<string, any>): Record<string, any> | Promise<Record<string, any>>;
51
+ }
52
+ export interface LLMBackend {
53
+ totalCost: number;
54
+ totalApiCalls: number;
55
+ call(messages: Message[], options?: LLMOptions): Promise<string>;
56
+ callRaw(messages: Message[], options?: LLMOptions): Promise<any>;
57
+ }
58
+ export interface Message {
59
+ role: "system" | "user" | "assistant" | "tool";
60
+ content: string;
61
+ tool_call_id?: string;
62
+ tool_calls?: ToolCall[];
63
+ }
64
+ export interface ToolCall {
65
+ id: string;
66
+ type: "function";
67
+ function: {
68
+ name: string;
69
+ arguments: string;
70
+ };
71
+ }
72
+ export interface LLMOptions {
73
+ temperature?: number;
74
+ max_tokens?: number;
75
+ tools?: ToolDefinition[];
76
+ response_format?: {
77
+ type: "json_object";
78
+ } | {
79
+ type: "text";
80
+ };
81
+ }
82
+ export interface ToolDefinition {
83
+ type: "function";
84
+ function: {
85
+ name: string;
86
+ description?: string;
87
+ parameters?: Record<string, any>;
88
+ };
89
+ }
90
+ export interface MachineInvoker {
91
+ invoke(machineName: string, input: Record<string, any>, options?: {
92
+ timeout?: number;
93
+ }): Promise<Record<string, any>>;
94
+ launch(machineName: string, input: Record<string, any>): Promise<string>;
95
+ }
96
+ export interface MachineSnapshot {
97
+ execution_id: string;
98
+ machine_name: string;
99
+ spec_version: string;
100
+ current_state: string;
101
+ context: Record<string, any>;
102
+ step: number;
103
+ created_at: string;
104
+ event?: string;
105
+ output?: Record<string, any>;
106
+ total_api_calls?: number;
107
+ total_cost?: number;
108
+ parent_execution_id?: string;
109
+ pending_launches?: LaunchIntent[];
110
+ }
111
+ export interface LaunchIntent {
112
+ execution_id: string;
113
+ machine: string;
114
+ input: Record<string, any>;
115
+ launched: boolean;
116
+ }
117
+ export interface RegistrationBackend {
118
+ register(worker: WorkerRegistration): Promise<WorkerRecord>;
119
+ heartbeat(worker_id: string, metadata?: Record<string, any>): Promise<void>;
120
+ updateStatus(worker_id: string, status: string): Promise<void>;
121
+ get(worker_id: string): Promise<WorkerRecord | null>;
122
+ list(filter?: WorkerFilter): Promise<WorkerRecord[]>;
123
+ }
124
+ export interface WorkerRegistration {
125
+ worker_id: string;
126
+ host?: string;
127
+ pid?: number;
128
+ capabilities?: string[];
129
+ pool_id?: string;
130
+ started_at: string;
131
+ }
132
+ export interface WorkerRecord extends WorkerRegistration {
133
+ status: string;
134
+ last_heartbeat: string;
135
+ current_task_id?: string;
136
+ }
137
+ export interface WorkerFilter {
138
+ status?: string | string[];
139
+ capability?: string;
140
+ pool_id?: string;
141
+ stale_threshold_seconds?: number;
142
+ }
143
+ export interface WorkBackend {
144
+ pool(name: string): WorkPool;
145
+ }
146
+ export interface WorkPool {
147
+ push(item: any, options?: {
148
+ max_retries?: number;
149
+ }): Promise<string>;
150
+ claim(worker_id: string): Promise<WorkItem | null>;
151
+ complete(item_id: string, result?: any): Promise<void>;
152
+ fail(item_id: string, error?: string): Promise<void>;
153
+ size(): Promise<number>;
154
+ releaseByWorker(worker_id: string): Promise<number>;
155
+ }
156
+ export interface WorkItem {
157
+ id: string;
158
+ data: any;
159
+ claimed_by?: string;
160
+ claimed_at?: string;
161
+ attempts: number;
162
+ max_retries: number;
163
+ }
164
+ export interface BackendConfig {
165
+ persistence?: "memory" | "local" | "redis" | "postgres" | "s3";
166
+ locking?: "none" | "local" | "redis" | "consul";
167
+ results?: "memory" | "redis";
168
+ registration?: "memory" | "sqlite" | "redis";
169
+ work?: "memory" | "sqlite" | "redis";
170
+ sqlite_path?: string;
171
+ }
172
+ export const SPEC_VERSION = "1.0.0";
173
+ export interface SDKRuntimeWrapper {
174
+ spec: "flatagents-runtime";
175
+ spec_version: typeof SPEC_VERSION;
176
+ execution_lock?: ExecutionLock;
177
+ persistence_backend?: PersistenceBackend;
178
+ result_backend?: ResultBackend;
179
+ execution_config?: ExecutionConfig;
180
+ machine_hooks?: MachineHooks;
181
+ llm_backend?: LLMBackend;
182
+ machine_invoker?: MachineInvoker;
183
+ backend_config?: BackendConfig;
184
+ machine_snapshot?: MachineSnapshot;
185
+ registration_backend?: RegistrationBackend;
186
+ work_backend?: WorkBackend;
187
+ }