sinapsis_cli 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 (46) hide show
  1. sinapsis_cli/__init__.py +13 -0
  2. sinapsis_cli/commands/__init__.py +0 -0
  3. sinapsis_cli/commands/cloud/__init__.py +0 -0
  4. sinapsis_cli/commands/cloud/_agent_selection.py +112 -0
  5. sinapsis_cli/commands/cloud/_secrets_render.py +15 -0
  6. sinapsis_cli/commands/cloud/_webhook_render.py +19 -0
  7. sinapsis_cli/commands/cloud/agents.py +137 -0
  8. sinapsis_cli/commands/cloud/api_keys.py +155 -0
  9. sinapsis_cli/commands/cloud/files.py +135 -0
  10. sinapsis_cli/commands/cloud/jobs.py +532 -0
  11. sinapsis_cli/commands/cloud/packages.py +164 -0
  12. sinapsis_cli/commands/cloud/schedules.py +392 -0
  13. sinapsis_cli/commands/cloud/secrets.py +96 -0
  14. sinapsis_cli/commands/cloud/templates.py +95 -0
  15. sinapsis_cli/commands/cloud/webhooks.py +156 -0
  16. sinapsis_cli/commands/common/__init__.py +0 -0
  17. sinapsis_cli/commands/common/auth.py +219 -0
  18. sinapsis_cli/commands/common/profiles.py +156 -0
  19. sinapsis_cli/commands/common/telemetry.py +150 -0
  20. sinapsis_cli/commands/local_api/__init__.py +0 -0
  21. sinapsis_cli/commands/local_api/jobs.py +284 -0
  22. sinapsis_cli/commands/local_venv/__init__.py +0 -0
  23. sinapsis_cli/commands/local_venv/_templates_render.py +56 -0
  24. sinapsis_cli/commands/local_venv/jobs.py +76 -0
  25. sinapsis_cli/commands/local_venv/templates.py +47 -0
  26. sinapsis_cli/composition.py +215 -0
  27. sinapsis_cli/config.py +446 -0
  28. sinapsis_cli/execution.py +77 -0
  29. sinapsis_cli/main.py +185 -0
  30. sinapsis_cli/main_multi.py +152 -0
  31. sinapsis_cli/shared/__init__.py +3 -0
  32. sinapsis_cli/shared/jobs.py +134 -0
  33. sinapsis_cli/shared/terminal.py +8 -0
  34. sinapsis_cli/shared/writer.py +427 -0
  35. sinapsis_cli/state.py +310 -0
  36. sinapsis_cli/telemetry/__init__.py +7 -0
  37. sinapsis_cli/telemetry/collector.py +84 -0
  38. sinapsis_cli/telemetry/core.py +331 -0
  39. sinapsis_cli/telemetry/scheduler.py +234 -0
  40. sinapsis_cli/telemetry/transport.py +150 -0
  41. sinapsis_cli-1.0.0.dist-info/METADATA +386 -0
  42. sinapsis_cli-1.0.0.dist-info/RECORD +46 -0
  43. sinapsis_cli-1.0.0.dist-info/WHEEL +4 -0
  44. sinapsis_cli-1.0.0.dist-info/entry_points.txt +5 -0
  45. sinapsis_cli-1.0.0.dist-info/licenses/LICENSE-APACHE +202 -0
  46. sinapsis_cli-1.0.0.dist-info/licenses/LICENSE-MIT +21 -0
@@ -0,0 +1,427 @@
1
+ """Persist decoded Sinapsis job results to disk."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import base64
6
+ import dataclasses
7
+ import json
8
+ import re
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+ try:
13
+ from pydantic import BaseModel
14
+ except ImportError: # pragma: no cover
15
+ BaseModel = None # type: ignore[assignment,misc]
16
+
17
+
18
+ _SAFE_NAME_RE = re.compile(r"[^A-Za-z0-9._-]+")
19
+
20
+
21
+ class ResultWriter:
22
+ """Write decoded job results and their artifacts to disk.
23
+
24
+ Results are grouped by agent and template:
25
+
26
+ results/
27
+ <agent_id>/
28
+ <template_name>/
29
+ result_001.json
30
+ result_001_image_001.png
31
+ result_001_file_001.pdf
32
+ ...
33
+
34
+ A separate counter is maintained for every agent/template pair.
35
+ """
36
+
37
+ def __init__(self, save_dir: str | Path = "results") -> None:
38
+ self.save_dir = Path(save_dir)
39
+
40
+ def write(self, result: Any) -> list[Path]:
41
+ """Write all data entries contained in a job result.
42
+
43
+ Returns:
44
+ Paths written to disk.
45
+ """
46
+ written: list[Path] = []
47
+
48
+ agent_id = _safe_name(result.agent_id, "unknown-agent")
49
+
50
+ for idx, entry in enumerate(result.template_data or []):
51
+ if not entry.output:
52
+ continue
53
+
54
+ result_dir = self.save_dir / agent_id / entry.template_name
55
+ result_dir.mkdir(parents=True, exist_ok=True)
56
+
57
+ result_number = self._next_result_number(result_dir)
58
+ prefix = f"result_{result_number:03d}"
59
+
60
+ # exclude the actual data containers
61
+ metadata = result.model_dump(exclude={"template_data": {idx: {"output": True}}})
62
+
63
+ artifacts = self._write_container(entry.output, result_dir=result_dir, prefix=prefix)
64
+
65
+ metadata["artifacts"] = [path.name for path in artifacts if path.name != f"{prefix}.json"]
66
+
67
+ metadata_path = result_dir / f"{prefix}.json"
68
+ metadata_path.write_text(
69
+ json.dumps(metadata, indent=2, ensure_ascii=False, default=_json_value) + "\n",
70
+ encoding="utf-8",
71
+ )
72
+
73
+ written.append(metadata_path)
74
+ written.extend(artifacts)
75
+
76
+ return written
77
+
78
+ def write_container(self, container: Any, *, agent_id: str | None, stream_name: str | None = None) -> list[Path]:
79
+ """Persist a native DataContainer without a cloud result wrapper.
80
+
81
+ Args:
82
+ container: DataContainer-like object to persist.
83
+ agent_id: Logical agent identifier used for the output directory.
84
+ stream_name: Optional job or stream identifier used as the second
85
+ directory component.
86
+
87
+ Returns:
88
+ Paths written to disk.
89
+ """
90
+ result_dir = self.save_dir / _safe_name(agent_id, "unknown-agent") / _safe_name(stream_name, "events")
91
+ result_dir.mkdir(parents=True, exist_ok=True)
92
+
93
+ result_number = self._next_result_number(result_dir)
94
+ prefix = f"result_{result_number:03d}"
95
+
96
+ artifacts = self._write_container(container, result_dir=result_dir, prefix=prefix)
97
+
98
+ metadata = {
99
+ "container_id": getattr(container, "container_id", None),
100
+ "artifacts": [path.name for path in artifacts],
101
+ }
102
+
103
+ metadata_path = result_dir / f"{prefix}.json"
104
+ metadata_path.write_text(
105
+ json.dumps(metadata, indent=2, ensure_ascii=False, default=_json_value) + "\n",
106
+ encoding="utf-8",
107
+ )
108
+
109
+ return [metadata_path, *artifacts]
110
+
111
+ def _write_container(self, container: Any, *, result_dir: Path, prefix: str) -> list[Path]:
112
+ """Write the contents of a DataContainer."""
113
+ written: list[Path] = []
114
+
115
+ packet_groups = (
116
+ "images",
117
+ "audios",
118
+ "texts",
119
+ "llm_conversations",
120
+ "files",
121
+ "time_series",
122
+ "data_frames",
123
+ "binary_data",
124
+ )
125
+
126
+ for group_name in packet_groups:
127
+ packets = getattr(container, group_name)
128
+
129
+ if not packets:
130
+ continue
131
+
132
+ for index, packet in enumerate(packets, start=1):
133
+ paths = self._write_packet(
134
+ packet, group_name=group_name, index=index, result_dir=result_dir, prefix=prefix
135
+ )
136
+ written.extend(paths)
137
+
138
+ generic_data = getattr(container, "generic_data", None)
139
+
140
+ if generic_data:
141
+ path = result_dir / f"{prefix}_generic.json"
142
+ path.write_text(
143
+ json.dumps(generic_data, indent=2, ensure_ascii=False, default=_json_value) + "\n",
144
+ encoding="utf-8",
145
+ )
146
+ written.append(path)
147
+
148
+ return written
149
+
150
+ def _write_packet(self, packet: Any, *, group_name: str, index: int, result_dir: Path, prefix: str) -> list[Path]:
151
+ """Persist one packet."""
152
+ payload = _find_payload(packet)
153
+
154
+ if payload is not None:
155
+ path = self._write_payload(
156
+ payload,
157
+ packet=packet,
158
+ group_name=group_name,
159
+ index=index,
160
+ result_dir=result_dir,
161
+ prefix=prefix,
162
+ )
163
+
164
+ if path is not None:
165
+ return [path]
166
+
167
+ # No directly persistable payload was found. Preserve the packet
168
+ # itself as JSON so no information is silently discarded.
169
+ path = result_dir / f"{prefix}_{group_name}_{index:03d}.json"
170
+
171
+ path.write_text(
172
+ json.dumps(_json_value(packet), indent=2, ensure_ascii=False, default=_json_value) + "\n",
173
+ encoding="utf-8",
174
+ )
175
+
176
+ return [path]
177
+
178
+ def _write_payload(
179
+ self,
180
+ payload: Any,
181
+ *,
182
+ packet: Any,
183
+ group_name: str,
184
+ index: int,
185
+ result_dir: Path,
186
+ prefix: str,
187
+ ) -> Path | None:
188
+ """Write a packet payload using the most appropriate representation."""
189
+ suffix = _extension_for(packet, payload, group_name)
190
+
191
+ path = result_dir / (f"{prefix}_{_safe_name(group_name, 'data')}_{index:03d}{suffix}")
192
+
193
+ if isinstance(payload, bytes):
194
+ path.write_bytes(payload)
195
+ return path
196
+
197
+ if isinstance(payload, bytearray):
198
+ path.write_bytes(bytes(payload))
199
+ return path
200
+
201
+ if isinstance(payload, memoryview):
202
+ path.write_bytes(payload.tobytes())
203
+ return path
204
+
205
+ if isinstance(payload, Path):
206
+ if payload.is_file():
207
+ path.write_bytes(payload.read_bytes())
208
+ return path
209
+
210
+ return None
211
+
212
+ if isinstance(payload, str):
213
+ path.write_text(payload, encoding="utf-8")
214
+ return path
215
+
216
+ if hasattr(payload, "save"):
217
+ try:
218
+ payload.save(path)
219
+ return path
220
+ except (OSError, TypeError, ValueError):
221
+ pass
222
+
223
+ if hasattr(payload, "tofile"):
224
+ try:
225
+ payload.tofile(path)
226
+ return path
227
+ except (OSError, TypeError, ValueError):
228
+ pass
229
+
230
+ return None
231
+
232
+ @staticmethod
233
+ def _next_result_number(result_dir: Path) -> int:
234
+ """Return the next result number for an agent/template directory."""
235
+ numbers: list[int] = []
236
+
237
+ for path in result_dir.glob("result_*.json"):
238
+ match = re.match(r"result_(\d+)\.json$", path.name)
239
+
240
+ if match:
241
+ numbers.append(int(match.group(1)))
242
+
243
+ return max(numbers, default=0) + 1
244
+
245
+
246
+ def _find_payload(packet: Any) -> Any:
247
+ """Find the most likely binary/text payload in a packet.
248
+
249
+ Packet implementations can differ, so inspect their dataclass/model
250
+ fields rather than requiring the CLI to know every packet schema.
251
+ """
252
+ preferred_names = (
253
+ "data",
254
+ "payload",
255
+ "content",
256
+ "value",
257
+ "bytes",
258
+ "file",
259
+ "path",
260
+ "image",
261
+ "audio",
262
+ "text",
263
+ )
264
+
265
+ values = _object_values(packet)
266
+
267
+ for name in preferred_names:
268
+ if name not in values:
269
+ continue
270
+
271
+ value = values[name]
272
+
273
+ if _is_payload(value):
274
+ return value
275
+
276
+ # Fall back to any directly writable value.
277
+ for value in values.values():
278
+ if _is_payload(value):
279
+ return value
280
+
281
+ return None
282
+
283
+
284
+ def _object_values(value: Any) -> dict[str, Any]:
285
+ """Return fields from common Python model types."""
286
+ if value is None:
287
+ return {}
288
+
289
+ if BaseModel is not None and isinstance(value, BaseModel):
290
+ return dict(value.model_dump())
291
+
292
+ if dataclasses.is_dataclass(value):
293
+ return {field.name: getattr(value, field.name) for field in dataclasses.fields(value)}
294
+
295
+ if hasattr(value, "__dict__"):
296
+ return {key: item for key, item in vars(value).items() if not key.startswith("_")}
297
+
298
+ return {}
299
+
300
+
301
+ def _is_payload(value: Any) -> bool:
302
+ """Return whether a value can be directly persisted."""
303
+ return (
304
+ isinstance(value, (bytes, bytearray, memoryview, str, Path))
305
+ or hasattr(value, "save")
306
+ or hasattr(value, "tofile")
307
+ )
308
+
309
+
310
+ def _extension_for(packet: Any, payload: Any, group_name: str) -> str:
311
+ """Determine a useful extension for a payload."""
312
+ if isinstance(payload, Path):
313
+ suffix = payload.suffix.lower()
314
+ if suffix:
315
+ return suffix
316
+
317
+ values = _object_values(packet)
318
+
319
+ for key in ("extension", "file_extension", "mime_type", "media_type"):
320
+ value = values.get(key)
321
+
322
+ if not isinstance(value, str):
323
+ continue
324
+
325
+ extension = _extension_from_value(value)
326
+
327
+ if extension:
328
+ return extension
329
+
330
+ defaults = {
331
+ "images": ".bin",
332
+ "audios": ".bin",
333
+ "texts": ".txt",
334
+ "llm_conversations": ".json",
335
+ "files": ".bin",
336
+ "time_series": ".json",
337
+ "data_frames": ".json",
338
+ "binary_data": ".bin",
339
+ }
340
+
341
+ return defaults.get(group_name, ".json")
342
+
343
+
344
+ def _extension_from_value(value: str) -> str | None:
345
+ """Convert a MIME type or extension into a file extension."""
346
+ value = value.lower().strip()
347
+
348
+ if value.startswith("."):
349
+ return value
350
+
351
+ mime_extensions = {
352
+ "image/png": ".png",
353
+ "image/jpeg": ".jpg",
354
+ "image/jpg": ".jpg",
355
+ "image/webp": ".webp",
356
+ "image/gif": ".gif",
357
+ "audio/wav": ".wav",
358
+ "audio/x-wav": ".wav",
359
+ "audio/mpeg": ".mp3",
360
+ "audio/ogg": ".ogg",
361
+ "application/pdf": ".pdf",
362
+ "application/json": ".json",
363
+ "text/plain": ".txt",
364
+ "text/csv": ".csv",
365
+ "application/zip": ".zip",
366
+ }
367
+
368
+ return mime_extensions.get(value)
369
+
370
+
371
+ def _json_value(value: Any) -> Any:
372
+ """Convert common Python objects into JSON-compatible values."""
373
+ if value is None or isinstance(value, (str, int, float, bool)):
374
+ return value
375
+
376
+ if isinstance(value, Path):
377
+ return str(value)
378
+
379
+ if isinstance(value, bytes):
380
+ return {"encoding": "base64", "data": base64.b64encode(value).decode("ascii")}
381
+
382
+ if isinstance(value, bytearray):
383
+ return {"encoding": "base64", "data": base64.b64encode(bytes(value)).decode("ascii")}
384
+
385
+ if isinstance(value, memoryview):
386
+ return {"encoding": "base64", "data": base64.b64encode(value.tobytes()).decode("ascii")}
387
+
388
+ if BaseModel is not None and isinstance(value, BaseModel):
389
+ return value.model_dump(mode="json")
390
+
391
+ if dataclasses.is_dataclass(value):
392
+ return dataclasses.asdict(value)
393
+
394
+ if isinstance(value, dict):
395
+ return {str(key): _json_value(item) for key, item in value.items()}
396
+
397
+ if isinstance(value, (list, tuple, set)):
398
+ return [_json_value(item) for item in value]
399
+
400
+ if hasattr(value, "isoformat"):
401
+ try:
402
+ return value.isoformat()
403
+ except (AttributeError, TypeError, ValueError):
404
+ pass
405
+
406
+ values = _object_values(value)
407
+
408
+ if values:
409
+ return {str(key): _json_value(item) for key, item in values.items()}
410
+
411
+ return str(value)
412
+
413
+
414
+ def _safe_name(value: Any, fallback: str) -> str:
415
+ """Make an agent/template name safe for use as a directory name."""
416
+ if value is None:
417
+ return fallback
418
+
419
+ value = str(value).strip()
420
+
421
+ if not value:
422
+ return fallback
423
+
424
+ value = _SAFE_NAME_RE.sub("_", value)
425
+ value = value.strip("._")
426
+
427
+ return value or fallback