execweave 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 (234) hide show
  1. execweave/__init__.py +3 -0
  2. execweave/__main__.py +5 -0
  3. execweave/_conversation_records_core.py +902 -0
  4. execweave/_dashboard_shell_base.py +148 -0
  5. execweave/_http_proxy_base.py +565 -0
  6. execweave/_http_proxy_bounded.py +560 -0
  7. execweave/_http_proxy_stage.py +509 -0
  8. execweave/acceptance_output.py +19 -0
  9. execweave/agent_bootstrap.py +395 -0
  10. execweave/agent_topology.py +313 -0
  11. execweave/agent_trace.py +1162 -0
  12. execweave/agy_preview_sanitize.py +102 -0
  13. execweave/analysis.py +427 -0
  14. execweave/anthropic.py +228 -0
  15. execweave/anthropic_cli.py +171 -0
  16. execweave/anthropic_full_fidelity.py +385 -0
  17. execweave/antigravity_adapter.py +112 -0
  18. execweave/antigravity_adapter_base.py +479 -0
  19. execweave/antigravity_full_fidelity.py +656 -0
  20. execweave/antigravity_full_fidelity_base.py +172 -0
  21. execweave/antigravity_full_fidelity_collaboration_base.py +451 -0
  22. execweave/antigravity_hook_cli.py +209 -0
  23. execweave/antigravity_record.py +96 -0
  24. execweave/antigravity_subagent_linkage.py +449 -0
  25. execweave/antigravity_trace_capability.py +39 -0
  26. execweave/auto_specialized.py +547 -0
  27. execweave/backends.py +102 -0
  28. execweave/benchmark.py +80 -0
  29. execweave/claude_adapter.py +517 -0
  30. execweave/claude_child_transcript.py +314 -0
  31. execweave/claude_delegation.py +136 -0
  32. execweave/claude_full_fidelity.py +396 -0
  33. execweave/claude_hook_cli.py +209 -0
  34. execweave/claude_hook_contract.py +943 -0
  35. execweave/claude_model_observer.py +353 -0
  36. execweave/claude_record.py +106 -0
  37. execweave/cli.py +629 -0
  38. execweave/codex_adapter.py +455 -0
  39. execweave/codex_conversation.py +621 -0
  40. execweave/codex_full_fidelity.py +287 -0
  41. execweave/codex_hook_cli.py +301 -0
  42. execweave/codex_hook_entry.py +207 -0
  43. execweave/codex_hook_lifecycle.py +363 -0
  44. execweave/codex_message_diagnostics.py +322 -0
  45. execweave/codex_message_transport_diagnostics.py +399 -0
  46. execweave/codex_record.py +199 -0
  47. execweave/codex_rollout_structures.py +402 -0
  48. execweave/codex_rollout_trace.py +56 -0
  49. execweave/codex_rollout_trace_base.py +1509 -0
  50. execweave/collector.py +768 -0
  51. execweave/command.py +157 -0
  52. execweave/command_privacy.py +20 -0
  53. execweave/content_evidence.py +101 -0
  54. execweave/content_integrity.py +289 -0
  55. execweave/content_store.py +273 -0
  56. execweave/controlled_checks.py +561 -0
  57. execweave/conversation_archive.py +338 -0
  58. execweave/conversation_message_identity.py +264 -0
  59. execweave/conversation_preview.py +66 -0
  60. execweave/conversation_preview_antigravity.py +32 -0
  61. execweave/conversation_preview_claude.py +31 -0
  62. execweave/conversation_preview_codex.py +107 -0
  63. execweave/conversation_preview_common.py +180 -0
  64. execweave/conversation_preview_generic.py +188 -0
  65. execweave/conversation_preview_lines.py +133 -0
  66. execweave/conversation_preview_transcript.py +121 -0
  67. execweave/conversation_records.py +349 -0
  68. execweave/conversation_records_antigravity.py +370 -0
  69. execweave/conversation_records_codex.py +83 -0
  70. execweave/conversation_records_common.py +17 -0
  71. execweave/conversation_records_ollama.py +39 -0
  72. execweave/correlation.py +818 -0
  73. execweave/cursor_adapter.py +349 -0
  74. execweave/cursor_delegation.py +87 -0
  75. execweave/cursor_delegation_base.py +220 -0
  76. execweave/cursor_full_fidelity.py +489 -0
  77. execweave/cursor_hook_cli.py +195 -0
  78. execweave/cursor_hook_contract.py +439 -0
  79. execweave/cursor_lifecycle.py +159 -0
  80. execweave/cursor_record.py +96 -0
  81. execweave/dashboard_shell.py +283 -0
  82. execweave/delivery_status.py +94 -0
  83. execweave/entry.py +98 -0
  84. execweave/evidence_availability.py +113 -0
  85. execweave/evidence_grade.py +166 -0
  86. execweave/fidelity.py +304 -0
  87. execweave/filesystem.py +327 -0
  88. execweave/finalization.py +92 -0
  89. execweave/focus.py +118 -0
  90. execweave/framework_adapters/__init__.py +34 -0
  91. execweave/framework_adapters/autogen.py +398 -0
  92. execweave/framework_adapters/base.py +580 -0
  93. execweave/framework_adapters/camel.py +270 -0
  94. execweave/framework_adapters/camel_channel.py +71 -0
  95. execweave/framework_adapters/metagpt.py +315 -0
  96. execweave/framework_adapters/metagpt_ollama_compat.py +54 -0
  97. execweave/framework_adapters/metagpt_recording.py +342 -0
  98. execweave/framework_adapters/registry.py +90 -0
  99. execweave/graph.py +590 -0
  100. execweave/graph_ops.py +472 -0
  101. execweave/hook_command.py +32 -0
  102. execweave/hook_delivery.py +32 -0
  103. execweave/hook_input.py +16 -0
  104. execweave/http_proxy.py +187 -0
  105. execweave/http_proxy_cli.py +84 -0
  106. execweave/inference_gateway.py +476 -0
  107. execweave/inference_gateway_cli.py +211 -0
  108. execweave/inference_gateway_full_fidelity.py +613 -0
  109. execweave/inference_identity.py +76 -0
  110. execweave/inference_identity_cli.py +60 -0
  111. execweave/integrity.py +235 -0
  112. execweave/integrity_cli.py +55 -0
  113. execweave/investigation_index.py +445 -0
  114. execweave/litellm_callback.py +211 -0
  115. execweave/litellm_callback_cli.py +42 -0
  116. execweave/live.py +445 -0
  117. execweave/live_core.py +972 -0
  118. execweave/live_view.py +109 -0
  119. execweave/live_view_extra_style.py +11 -0
  120. execweave/live_view_markup.py +29 -0
  121. execweave/live_view_process_layout.py +650 -0
  122. execweave/live_view_readability.py +502 -0
  123. execweave/live_view_script_a.py +59 -0
  124. execweave/live_view_script_b.py +97 -0
  125. execweave/live_view_script_c.py +59 -0
  126. execweave/live_view_script_d.py +40 -0
  127. execweave/live_view_style.py +34 -0
  128. execweave/model_runtime.py +560 -0
  129. execweave/model_runtime_cli.py +265 -0
  130. execweave/model_runtime_full_fidelity.py +519 -0
  131. execweave/observation.py +53 -0
  132. execweave/observation_demo.py +69 -0
  133. execweave/openai_agents.py +267 -0
  134. execweave/openai_agents_sdk.py +453 -0
  135. execweave/openai_compatible.py +239 -0
  136. execweave/openai_compatible_cli.py +189 -0
  137. execweave/openai_compatible_full_fidelity.py +438 -0
  138. execweave/opencode_adapter.py +325 -0
  139. execweave/opencode_event_contract.py +734 -0
  140. execweave/opencode_full_fidelity.py +198 -0
  141. execweave/opencode_hook_cli.py +119 -0
  142. execweave/opencode_plugin_cli.py +150 -0
  143. execweave/opencode_record.py +96 -0
  144. execweave/opencode_task_linkage.py +225 -0
  145. execweave/overhead_benchmark.py +440 -0
  146. execweave/privacy.py +84 -0
  147. execweave/private_io.py +465 -0
  148. execweave/provider_capability.py +578 -0
  149. execweave/provider_lifecycle.py +282 -0
  150. execweave/provider_record.py +290 -0
  151. execweave/reader_identity.py +13 -0
  152. execweave/redacted_archive.py +906 -0
  153. execweave/risk.py +106 -0
  154. execweave/rule_pack.py +359 -0
  155. execweave/rule_pack_cli.py +57 -0
  156. execweave/run_assessment.py +200 -0
  157. execweave/runtime_endpoint_availability.py +44 -0
  158. execweave/scalability_benchmark.py +195 -0
  159. execweave/schema.py +62 -0
  160. execweave/scope.py +85 -0
  161. execweave/semantic.py +453 -0
  162. execweave/session_summary.py +159 -0
  163. execweave/sink.py +71 -0
  164. execweave/strace_backend.py +927 -0
  165. execweave/stream_assembly.py +363 -0
  166. execweave/task_artifacts.py +192 -0
  167. execweave/task_validation.py +317 -0
  168. execweave/theme.py +40 -0
  169. execweave/top.py +599 -0
  170. execweave/top_cli.py +150 -0
  171. execweave/validate.py +196 -0
  172. execweave/vendor/dagre.min.js +815 -0
  173. execweave/view_cli.py +49 -0
  174. execweave/viewer.py +474 -0
  175. execweave/viewer_agent_panel.py +630 -0
  176. execweave/viewer_agent_panel_antigravity.py +31 -0
  177. execweave/viewer_agent_panel_claude.py +9 -0
  178. execweave/viewer_agent_panel_codex.py +50 -0
  179. execweave/viewer_agent_panel_cursor.py +9 -0
  180. execweave/viewer_agent_panel_default.py +79 -0
  181. execweave/viewer_agent_panel_ollama.py +20 -0
  182. execweave/viewer_agent_panel_opencode.py +9 -0
  183. execweave/viewer_agent_panel_registry.py +20 -0
  184. execweave/viewer_archive_verifier.py +178 -0
  185. execweave/viewer_background_files.py +86 -0
  186. execweave/viewer_content_browser.py +307 -0
  187. execweave/viewer_content_health.py +208 -0
  188. execweave/viewer_content_inspector.py +309 -0
  189. execweave/viewer_controlled_checks.py +258 -0
  190. execweave/viewer_conversation_sync.py +115 -0
  191. execweave/viewer_dashboard_clean.py +405 -0
  192. execweave/viewer_dashboard_focus.py +127 -0
  193. execweave/viewer_dashboard_hardening.py +264 -0
  194. execweave/viewer_dashboard_hardening_v2.py +62 -0
  195. execweave/viewer_dashboard_projection.py +261 -0
  196. execweave/viewer_delivery_status.py +155 -0
  197. execweave/viewer_edge_decross.py +117 -0
  198. execweave/viewer_execution_flow.py +611 -0
  199. execweave/viewer_execution_flow_provider_normalization.py +107 -0
  200. execweave/viewer_execution_flow_safety.py +349 -0
  201. execweave/viewer_external_endpoints.py +307 -0
  202. execweave/viewer_flow_canvas.py +1140 -0
  203. execweave/viewer_flow_layout.py +922 -0
  204. execweave/viewer_fold_state.py +31 -0
  205. execweave/viewer_framework_flow.py +68 -0
  206. execweave/viewer_framework_model_output.py +133 -0
  207. execweave/viewer_gif.py +278 -0
  208. execweave/viewer_history_browser.py +225 -0
  209. execweave/viewer_investigation.py +268 -0
  210. execweave/viewer_layout_geometry.py +192 -0
  211. execweave/viewer_layout_v2.py +429 -0
  212. execweave/viewer_layout_v2_detached.py +25 -0
  213. execweave/viewer_limits.py +93 -0
  214. execweave/viewer_live_layout.py +61 -0
  215. execweave/viewer_message_handoffs.py +114 -0
  216. execweave/viewer_projection.py +488 -0
  217. execweave/viewer_projection_base.py +522 -0
  218. execweave/viewer_projection_bridges.py +214 -0
  219. execweave/viewer_recorded_sources.py +146 -0
  220. execweave/viewer_run_assessment.py +274 -0
  221. execweave/viewer_run_guide.py +175 -0
  222. execweave/viewer_runtime_evidence.py +258 -0
  223. execweave/viewer_semantic_projection.py +641 -0
  224. execweave/viewer_session_flow.py +491 -0
  225. execweave/viewer_structure_share.py +132 -0
  226. execweave/viewer_task_validation.py +241 -0
  227. execweave/viewer_tool_mirror_identity.py +75 -0
  228. execweave/viewer_workflow_mode.py +127 -0
  229. execweave/workflow.py +183 -0
  230. execweave-1.0.0.dist-info/METADATA +462 -0
  231. execweave-1.0.0.dist-info/RECORD +234 -0
  232. execweave-1.0.0.dist-info/WHEEL +4 -0
  233. execweave-1.0.0.dist-info/entry_points.txt +30 -0
  234. execweave-1.0.0.dist-info/licenses/LICENSE +16 -0
@@ -0,0 +1,189 @@
1
+ from __future__ import annotations
2
+
3
+ import argparse
4
+ import json
5
+ import os
6
+ import sys
7
+ from datetime import datetime, timezone
8
+ from pathlib import Path
9
+
10
+ from .content_store import FullFidelityContentStore
11
+ from .privacy import full_content_capture_enabled
12
+ from .openai_compatible import (
13
+ append_openai_compatible_records,
14
+ response_to_events,
15
+ sanitize_openai_compatible_endpoint,
16
+ )
17
+ from .openai_compatible_full_fidelity import (
18
+ exchange_to_content_events,
19
+ response_to_content_events,
20
+ )
21
+
22
+
23
+ def _now() -> str:
24
+ return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
25
+
26
+
27
+ def _sidecar(value: Path | None) -> Path:
28
+ if value is not None:
29
+ return value
30
+ configured = os.environ.get("EXECWEAVE_SEMANTIC_SIDECAR")
31
+ if configured:
32
+ return Path(configured)
33
+ raise ValueError("--sidecar or EXECWEAVE_SEMANTIC_SIDECAR is required")
34
+
35
+
36
+ def _read_json_stdin() -> dict:
37
+ raw = sys.stdin.read()
38
+ if not raw.strip():
39
+ raise ValueError("stdin is empty")
40
+ try:
41
+ payload = json.loads(raw)
42
+ except json.JSONDecodeError as exc:
43
+ raise ValueError(f"stdin is invalid JSON: {exc.msg}") from exc
44
+ if not isinstance(payload, dict):
45
+ raise ValueError("stdin must contain one JSON object")
46
+ return payload
47
+
48
+
49
+ def build_parser() -> argparse.ArgumentParser:
50
+ parser = argparse.ArgumentParser(
51
+ prog="execweave-openai-compatible",
52
+ description=(
53
+ "Capture explicit OpenAI-compatible direct API evidence plus complete content "
54
+ "exposed by the selected integration point."
55
+ ),
56
+ )
57
+ sub = parser.add_subparsers(dest="command", required=True)
58
+
59
+ event = sub.add_parser(
60
+ "event",
61
+ help="Capture one supplied final API response; this is response-only evidence.",
62
+ )
63
+ event.add_argument("--endpoint", required=True)
64
+ event.add_argument("--provider-name", default="openai-compatible")
65
+ event.add_argument("--request-id", default=None)
66
+ event.add_argument("--sidecar", type=Path, default=None)
67
+
68
+ exchange = sub.add_parser(
69
+ "exchange",
70
+ help=(
71
+ "Capture caller-supplied request+response JSON; this does not assert transparent "
72
+ "interception."
73
+ ),
74
+ )
75
+ exchange.add_argument("--endpoint", required=True)
76
+ exchange.add_argument("--provider-name", default="openai-compatible")
77
+ exchange.add_argument("--request-id", default=None)
78
+ exchange.add_argument("--sidecar", type=Path, default=None)
79
+ return parser
80
+
81
+
82
+ def _append_full_fidelity_fail_open(sidecar: Path, records_factory) -> int:
83
+ if not full_content_capture_enabled():
84
+ return 0
85
+ try:
86
+ records = records_factory(FullFidelityContentStore(sidecar.parent))
87
+ append_openai_compatible_records(sidecar, records)
88
+ return len(records)
89
+ except (OSError, RuntimeError, TimeoutError, TypeError, ValueError) as exc:
90
+ print(f"ExecWeave OpenAI-compatible full-fidelity warning: {exc}", file=sys.stderr)
91
+ return 0
92
+
93
+
94
+ def _event_command(args: argparse.Namespace) -> int:
95
+ payload = _read_json_stdin()
96
+ endpoint = sanitize_openai_compatible_endpoint(args.endpoint)
97
+ sidecar = _sidecar(args.sidecar).expanduser().resolve()
98
+ observed_at = _now()
99
+ records = response_to_events(
100
+ payload,
101
+ endpoint=endpoint,
102
+ provider_name=args.provider_name,
103
+ request_id=args.request_id,
104
+ timestamp=observed_at,
105
+ )
106
+ output = append_openai_compatible_records(sidecar, records)
107
+ full_records = _append_full_fidelity_fail_open(
108
+ sidecar,
109
+ lambda store: response_to_content_events(
110
+ payload,
111
+ store=store,
112
+ endpoint=endpoint,
113
+ provider_name=args.provider_name,
114
+ request_id=args.request_id,
115
+ timestamp=observed_at,
116
+ ),
117
+ )
118
+ print(
119
+ json.dumps(
120
+ {
121
+ "full_fidelity_records": full_records,
122
+ "records": len(records),
123
+ "sidecar": str(output),
124
+ },
125
+ sort_keys=True,
126
+ )
127
+ )
128
+ return 0
129
+
130
+
131
+ def _exchange_command(args: argparse.Namespace) -> int:
132
+ exchange = _read_json_stdin()
133
+ request_payload = exchange.get("request")
134
+ response_payload = exchange.get("response")
135
+ if not isinstance(request_payload, dict) or not isinstance(response_payload, dict):
136
+ raise ValueError("exchange stdin requires JSON-object request and response fields")
137
+ endpoint = sanitize_openai_compatible_endpoint(args.endpoint)
138
+ sidecar = _sidecar(args.sidecar).expanduser().resolve()
139
+ observed_at = _now()
140
+ requested_model = request_payload.get("model")
141
+ if not isinstance(requested_model, str) or not requested_model:
142
+ requested_model = None
143
+ records = response_to_events(
144
+ response_payload,
145
+ endpoint=endpoint,
146
+ provider_name=args.provider_name,
147
+ requested_model=requested_model,
148
+ request_id=args.request_id,
149
+ timestamp=observed_at,
150
+ )
151
+ output = append_openai_compatible_records(sidecar, records)
152
+ full_records = _append_full_fidelity_fail_open(
153
+ sidecar,
154
+ lambda store: exchange_to_content_events(
155
+ exchange,
156
+ store=store,
157
+ endpoint=endpoint,
158
+ provider_name=args.provider_name,
159
+ request_id=args.request_id,
160
+ timestamp=observed_at,
161
+ ),
162
+ )
163
+ print(
164
+ json.dumps(
165
+ {
166
+ "full_fidelity_records": full_records,
167
+ "records": len(records),
168
+ "sidecar": str(output),
169
+ },
170
+ sort_keys=True,
171
+ )
172
+ )
173
+ return 0
174
+
175
+
176
+ def main(argv: list[str] | None = None) -> int:
177
+ parser = build_parser()
178
+ args = parser.parse_args(argv)
179
+ try:
180
+ if args.command == "event":
181
+ return _event_command(args)
182
+ return _exchange_command(args)
183
+ except (OSError, TimeoutError, ValueError) as exc:
184
+ parser.error(str(exc))
185
+ return 2
186
+
187
+
188
+ if __name__ == "__main__":
189
+ raise SystemExit(main())
@@ -0,0 +1,438 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import replace
4
+ from datetime import datetime, timezone
5
+ from typing import Any
6
+
7
+ from .content_evidence import content_observation_event
8
+ from .content_store import ContentReference, FullFidelityContentStore
9
+ from .openai_compatible import (
10
+ _endpoint_digest,
11
+ _request_identity,
12
+ sanitize_openai_compatible_endpoint,
13
+ )
14
+
15
+ _REQUEST_CONTENT_KEYS = frozenset(
16
+ {"messages", "prompt", "input", "system", "instructions", "tools", "functions"}
17
+ )
18
+
19
+
20
+ def _now() -> str:
21
+ return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
22
+
23
+
24
+ def _request_entity(
25
+ response: dict[str, Any],
26
+ *,
27
+ endpoint: str,
28
+ provider_name: str,
29
+ request_id: str | None,
30
+ observed_at: str,
31
+ ) -> dict[str, Any]:
32
+ safe_endpoint = sanitize_openai_compatible_endpoint(endpoint)
33
+ endpoint_scope = _endpoint_digest(safe_endpoint)
34
+ native_id, request_id_source = _request_identity(
35
+ response,
36
+ request_id,
37
+ endpoint_scope=endpoint_scope,
38
+ observed_at=observed_at,
39
+ )
40
+ return {
41
+ "type": "inference_request",
42
+ "id": f"inference-request:openai-compatible:{endpoint_scope}:{native_id}",
43
+ "name": native_id,
44
+ "attributes": {
45
+ "protocol": "openai_compatible",
46
+ "provider_name": provider_name,
47
+ "endpoint": safe_endpoint,
48
+ "endpoint_scope": endpoint_scope,
49
+ "request_id_source": request_id_source,
50
+ },
51
+ }
52
+
53
+
54
+ def _store(
55
+ store: FullFidelityContentStore,
56
+ value: Any,
57
+ *,
58
+ content_kind: str,
59
+ ) -> ContentReference:
60
+ if isinstance(value, str):
61
+ return store.put_text(value, content_kind=content_kind)
62
+ return store.put_json(value, content_kind=content_kind)
63
+
64
+
65
+ def _content_event(
66
+ *,
67
+ store: FullFidelityContentStore,
68
+ value: Any,
69
+ content_kind: str,
70
+ timestamp: str,
71
+ provider_name: str,
72
+ source: dict[str, Any],
73
+ relation: str,
74
+ observed_field: str,
75
+ evidence_source: str,
76
+ attribution: str,
77
+ attributes: dict[str, Any],
78
+ ) -> dict[str, Any]:
79
+ return content_observation_event(
80
+ timestamp=timestamp,
81
+ provider=provider_name,
82
+ source=source,
83
+ reference=_store(store, value, content_kind=content_kind),
84
+ relation=relation,
85
+ observed_field=observed_field,
86
+ evidence_source=evidence_source,
87
+ attribution=attribution,
88
+ event_type="openai_compatible.content.observed",
89
+ attributes=attributes,
90
+ )
91
+
92
+
93
+ def _metadata_event(
94
+ *,
95
+ store: FullFidelityContentStore,
96
+ metadata: dict[str, Any],
97
+ timestamp: str,
98
+ provider_name: str,
99
+ source: dict[str, Any],
100
+ evidence_source: str,
101
+ attribution: str,
102
+ attributes: dict[str, Any],
103
+ ) -> dict[str, Any]:
104
+ reference = store.put_json(metadata, content_kind="openai_compatible.provider_metadata")
105
+ reference = replace(reference, complete_from_source=False)
106
+ return content_observation_event(
107
+ timestamp=timestamp,
108
+ provider=provider_name,
109
+ source=source,
110
+ reference=reference,
111
+ relation="OBSERVED_PROVIDER_METADATA",
112
+ observed_field="integration_metadata_projection",
113
+ evidence_source=evidence_source,
114
+ attribution=attribution,
115
+ event_type="openai_compatible.metadata.observed",
116
+ attributes={
117
+ **attributes,
118
+ "metadata_projection": True,
119
+ "metadata_complete_from_source": False,
120
+ },
121
+ )
122
+
123
+
124
+ def _assistant_messages(response: object) -> list[Any]:
125
+ if not isinstance(response, dict):
126
+ return []
127
+ messages: list[Any] = []
128
+ native = response.get("message")
129
+ if isinstance(native, dict):
130
+ messages.append(native)
131
+ choices = response.get("choices")
132
+ if isinstance(choices, list):
133
+ for choice in choices:
134
+ if isinstance(choice, dict) and isinstance(choice.get("message"), dict):
135
+ messages.append(choice["message"])
136
+ output = response.get("output")
137
+ if isinstance(output, list):
138
+ for item in output:
139
+ if isinstance(item, dict) and item.get("type") in {"message", "assistant_message"}:
140
+ messages.append(item)
141
+ return messages
142
+
143
+
144
+ def _response_tool_calls(response: object) -> list[Any]:
145
+ if not isinstance(response, dict):
146
+ return []
147
+ calls: list[Any] = []
148
+ direct = response.get("tool_calls")
149
+ if isinstance(direct, list):
150
+ calls.extend(direct)
151
+ for message in _assistant_messages(response):
152
+ if isinstance(message, dict):
153
+ nested = message.get("tool_calls")
154
+ if isinstance(nested, list):
155
+ calls.extend(nested)
156
+ output = response.get("output")
157
+ if isinstance(output, list):
158
+ for item in output:
159
+ if not isinstance(item, dict):
160
+ continue
161
+ if item.get("type") in {"function_call", "tool_call", "tool_use"}:
162
+ calls.append(item)
163
+ nested = item.get("tool_calls")
164
+ if isinstance(nested, list):
165
+ calls.extend(nested)
166
+ return calls
167
+
168
+
169
+ def _tool_results(request: dict[str, Any]) -> list[Any]:
170
+ results: list[Any] = []
171
+ messages = request.get("messages")
172
+ if isinstance(messages, list):
173
+ results.extend(
174
+ item
175
+ for item in messages
176
+ if isinstance(item, dict) and item.get("role") in {"tool", "function"}
177
+ )
178
+ input_value = request.get("input")
179
+ if isinstance(input_value, list):
180
+ results.extend(
181
+ item
182
+ for item in input_value
183
+ if isinstance(item, dict)
184
+ and item.get("type") in {"function_call_output", "tool_result", "tool_output"}
185
+ )
186
+ return results
187
+
188
+
189
+ def response_to_content_events(
190
+ payload: dict[str, Any],
191
+ *,
192
+ store: FullFidelityContentStore,
193
+ endpoint: str,
194
+ provider_name: str = "openai-compatible",
195
+ request_id: str | None = None,
196
+ timestamp: str | None = None,
197
+ ) -> list[dict[str, Any]]:
198
+ """Store the complete supplied final response without claiming request visibility."""
199
+ observed_at = timestamp or _now()
200
+ safe_endpoint = sanitize_openai_compatible_endpoint(endpoint)
201
+ request = _request_entity(
202
+ payload,
203
+ endpoint=safe_endpoint,
204
+ provider_name=provider_name,
205
+ request_id=request_id,
206
+ observed_at=observed_at,
207
+ )
208
+ attrs = {
209
+ "observation_scope": "response_only",
210
+ "request_observed": False,
211
+ "caller_supplied_exchange": False,
212
+ "wire_interception_asserted": False,
213
+ "streaming_chunks_observed": False,
214
+ }
215
+ common = {
216
+ "store": store,
217
+ "timestamp": observed_at,
218
+ "provider_name": provider_name,
219
+ "source": request,
220
+ "evidence_source": "openai_compatible_response",
221
+ "attribution": "direct_api_response",
222
+ "attributes": attrs,
223
+ }
224
+ events = [
225
+ _content_event(
226
+ **common,
227
+ value=payload,
228
+ content_kind="openai_compatible.response",
229
+ relation="OBSERVED_INFERENCE_RESPONSE",
230
+ observed_field="response",
231
+ )
232
+ ]
233
+ assistant_messages = _assistant_messages(payload)
234
+ if assistant_messages:
235
+ events.append(
236
+ _content_event(
237
+ **common,
238
+ value=assistant_messages,
239
+ content_kind="openai_compatible.assistant_messages",
240
+ relation="OBSERVED_ASSISTANT_MESSAGES",
241
+ observed_field="response.assistant_messages",
242
+ )
243
+ )
244
+ tool_calls = _response_tool_calls(payload)
245
+ if tool_calls:
246
+ events.append(
247
+ _content_event(
248
+ **common,
249
+ value=tool_calls,
250
+ content_kind="openai_compatible.assistant_tool_calls",
251
+ relation="OBSERVED_ASSISTANT_TOOL_CALLS",
252
+ observed_field="response.tool_calls",
253
+ )
254
+ )
255
+ metadata = {
256
+ "protocol": "openai_compatible",
257
+ "provider_name": provider_name,
258
+ "endpoint": safe_endpoint,
259
+ "request_id": request["name"],
260
+ "resolved_model": payload.get("model"),
261
+ }
262
+ events.append(
263
+ _metadata_event(
264
+ store=store,
265
+ metadata=metadata,
266
+ timestamp=observed_at,
267
+ provider_name=provider_name,
268
+ source=request,
269
+ evidence_source="openai_compatible_response",
270
+ attribution="direct_api_response",
271
+ attributes=attrs,
272
+ )
273
+ )
274
+ return events
275
+
276
+
277
+ def exchange_to_content_events(
278
+ exchange: dict[str, Any],
279
+ *,
280
+ store: FullFidelityContentStore,
281
+ endpoint: str,
282
+ provider_name: str = "openai-compatible",
283
+ request_id: str | None = None,
284
+ timestamp: str | None = None,
285
+ ) -> list[dict[str, Any]]:
286
+ """Store caller-supplied request+response evidence; this is not wire interception."""
287
+ request_payload = exchange.get("request")
288
+ response_payload = exchange.get("response")
289
+ if not isinstance(request_payload, dict) or not isinstance(response_payload, dict):
290
+ raise ValueError(
291
+ "OpenAI-compatible exchange requires JSON-object request and response fields"
292
+ )
293
+ stream_chunks = exchange.get("stream_chunks")
294
+ if stream_chunks is not None and not isinstance(stream_chunks, list):
295
+ raise ValueError("OpenAI-compatible stream_chunks must be a JSON array when supplied")
296
+
297
+ observed_at = timestamp or _now()
298
+ safe_endpoint = sanitize_openai_compatible_endpoint(endpoint)
299
+ request = _request_entity(
300
+ response_payload,
301
+ endpoint=safe_endpoint,
302
+ provider_name=provider_name,
303
+ request_id=request_id,
304
+ observed_at=observed_at,
305
+ )
306
+ attrs = {
307
+ "observation_scope": "caller_supplied_exchange",
308
+ "request_observed": True,
309
+ "caller_supplied_exchange": True,
310
+ "wire_interception_asserted": False,
311
+ "streaming_chunks_observed": stream_chunks is not None,
312
+ }
313
+ common = {
314
+ "store": store,
315
+ "timestamp": observed_at,
316
+ "provider_name": provider_name,
317
+ "source": request,
318
+ "evidence_source": "caller_supplied_exchange",
319
+ "attribution": "execweave_openai_compatible_cli",
320
+ "attributes": attrs,
321
+ }
322
+ events = [
323
+ _content_event(
324
+ **common,
325
+ value=request_payload,
326
+ content_kind="openai_compatible.request",
327
+ relation="OBSERVED_INFERENCE_REQUEST",
328
+ observed_field="request",
329
+ )
330
+ ]
331
+ for key, relation in (
332
+ ("messages", "OBSERVED_INFERENCE_REQUEST_MESSAGES"),
333
+ ("prompt", "OBSERVED_INFERENCE_REQUEST_PROMPT"),
334
+ ("input", "OBSERVED_INFERENCE_REQUEST_INPUT"),
335
+ ):
336
+ if key in request_payload:
337
+ events.append(
338
+ _content_event(
339
+ **common,
340
+ value=request_payload[key],
341
+ content_kind=f"openai_compatible.request_{key}",
342
+ relation=relation,
343
+ observed_field=f"request.{key}",
344
+ )
345
+ )
346
+ for key in ("system", "instructions"):
347
+ if key in request_payload:
348
+ events.append(
349
+ _content_event(
350
+ **common,
351
+ value=request_payload[key],
352
+ content_kind=f"openai_compatible.{key}_context",
353
+ relation="OBSERVED_SYSTEM_CONTEXT",
354
+ observed_field=f"request.{key}",
355
+ )
356
+ )
357
+ for key in ("tools", "functions"):
358
+ if key in request_payload:
359
+ events.append(
360
+ _content_event(
361
+ **common,
362
+ value=request_payload[key],
363
+ content_kind=f"openai_compatible.{key}",
364
+ relation="OBSERVED_TOOL_DEFINITIONS",
365
+ observed_field=f"request.{key}",
366
+ )
367
+ )
368
+ tool_results = _tool_results(request_payload)
369
+ if tool_results:
370
+ events.append(
371
+ _content_event(
372
+ **common,
373
+ value=tool_results,
374
+ content_kind="openai_compatible.tool_results",
375
+ relation="OBSERVED_TOOL_RESULT_MESSAGES",
376
+ observed_field="request.tool_results",
377
+ )
378
+ )
379
+ config = {
380
+ key: value for key, value in request_payload.items() if key not in _REQUEST_CONTENT_KEYS
381
+ }
382
+ if config:
383
+ events.append(
384
+ _content_event(
385
+ **common,
386
+ value=config,
387
+ content_kind="openai_compatible.request_config",
388
+ relation="OBSERVED_PROVIDER_REQUEST_CONFIG",
389
+ observed_field="request.provider_facing_config",
390
+ )
391
+ )
392
+ if stream_chunks is not None:
393
+ events.append(
394
+ _content_event(
395
+ **common,
396
+ value=stream_chunks,
397
+ content_kind="openai_compatible.stream_chunks",
398
+ relation="OBSERVED_INFERENCE_STREAM_CHUNKS",
399
+ observed_field="stream_chunks",
400
+ )
401
+ )
402
+
403
+ for event in response_to_content_events(
404
+ response_payload,
405
+ store=store,
406
+ endpoint=safe_endpoint,
407
+ provider_name=provider_name,
408
+ request_id=request_id,
409
+ timestamp=observed_at,
410
+ ):
411
+ if event.get("relation") == "OBSERVED_PROVIDER_METADATA":
412
+ continue
413
+ event["attributes"].update(attrs)
414
+ event["attributes"]["evidence_source"] = "caller_supplied_exchange"
415
+ event["attributes"]["attribution"] = "execweave_openai_compatible_cli"
416
+ events.append(event)
417
+
418
+ metadata = {
419
+ "protocol": "openai_compatible",
420
+ "provider_name": provider_name,
421
+ "endpoint": safe_endpoint,
422
+ "request_id": request["name"],
423
+ "requested_model": request_payload.get("model"),
424
+ "resolved_model": response_payload.get("model"),
425
+ }
426
+ events.append(
427
+ _metadata_event(
428
+ store=store,
429
+ metadata=metadata,
430
+ timestamp=observed_at,
431
+ provider_name=provider_name,
432
+ source=request,
433
+ evidence_source="caller_supplied_exchange",
434
+ attribution="execweave_openai_compatible_cli",
435
+ attributes=attrs,
436
+ )
437
+ )
438
+ return events