(
messages: list[dict[str, Any]],
artifact_store: ArtifactStore,
semantic_compressor: SemanticCompressor,
policy: CompositionPolicy,
*,
request: Any,
)
| 406 | |
| 407 | |
| 408 | async def _rehydrate_artifacts( |
| 409 | messages: list[dict[str, Any]], |
| 410 | artifact_store: ArtifactStore, |
| 411 | semantic_compressor: SemanticCompressor, |
| 412 | policy: CompositionPolicy, |
| 413 | *, |
| 414 | request: Any, |
| 415 | ) -> tuple[list[dict[str, Any]], int, int, int, int, float, float]: |
| 416 | latest_idx = _latest_user_index(messages) |
| 417 | if latest_idx is None: |
| 418 | return messages, 0, 0, 0, 0, 0.0, 0.0 |
| 419 | msg = dict(messages[latest_idx]) |
| 420 | content = msg.get("content") |
| 421 | if not isinstance(content, str): |
| 422 | return messages, 0, 0, 0, 0, 0.0, 0.0 |
| 423 | |
| 424 | refs = _ARTIFACT_REF_RE.findall(content) |
| 425 | if not refs: |
| 426 | return messages, 0, 0, 0, 0, 0.0, 0.0 |
| 427 | |
| 428 | appended: list[str] = [] |
| 429 | used = 0 |
| 430 | calls = 0 |
| 431 | failures = 0 |
| 432 | quality_fallbacks = 0 |
| 433 | est = 0.0 |
| 434 | act = 0.0 |
| 435 | for artifact_id in refs[:policy.rehydrate_max_artifacts]: |
| 436 | artifact = artifact_store.get(artifact_id) |
| 437 | if artifact is None: |
| 438 | continue |
| 439 | query = content |
| 440 | summary = str(artifact.get("summary", "")) |
| 441 | excerpt = summary |
| 442 | if not excerpt: |
| 443 | call = await semantic_compressor.rehydrate_artifact( |
| 444 | query, |
| 445 | artifact_id=artifact_id, |
| 446 | content=artifact.get("content", ""), |
| 447 | summary=summary, |
| 448 | request=request, |
| 449 | ) |
| 450 | calls += 1 |
| 451 | if call is None: |
| 452 | failures += 1 |
| 453 | excerpt = _truncate_excerpt(artifact.get("content", ""), policy.rehydrate_append_chars) |
| 454 | else: |
| 455 | excerpt = call.text.strip() |
| 456 | est += call.estimated_cost |
| 457 | act += call.actual_cost or 0.0 |
| 458 | quality_fallbacks += call.quality_fallbacks |
| 459 | else: |
| 460 | excerpt = excerpt[:policy.rehydrate_append_chars] |
| 461 | if excerpt: |
| 462 | appended.append(f"[Rehydrated artifact://{artifact_id}]\n{excerpt}") |
| 463 | used += 1 |
| 464 | |
| 465 | if appended: |
no test coverage detected