0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
173 lines
5.1 KiB
Python
173 lines
5.1 KiB
Python
"""Pure mixed-content parsing helpers for the content router."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from dataclasses import dataclass
|
|
|
|
from .content_detector import ContentType
|
|
|
|
|
|
@dataclass
|
|
class ContentSection:
|
|
"""A typed section of content."""
|
|
|
|
content: str
|
|
content_type: ContentType
|
|
language: str | None = None
|
|
start_line: int = 0
|
|
end_line: int = 0
|
|
is_code_fence: bool = False
|
|
|
|
|
|
_CODE_FENCE_PATTERN = re.compile(r"^```(\w*)\s*$", re.MULTILINE)
|
|
_JSON_BLOCK_START = re.compile(r"^\s*[\[{]", re.MULTILINE)
|
|
_SEARCH_RESULT_PATTERN = re.compile(r"^\S+:\d+:", re.MULTILINE)
|
|
_PROSE_PATTERN = re.compile(r"[A-Z][a-z]+\s+\w+\s+\w+")
|
|
|
|
|
|
def is_mixed_content(content: str) -> bool:
|
|
"""Detect if content contains multiple distinct content types."""
|
|
return sum(mixed_content_indicators(content).values()) >= 2
|
|
|
|
|
|
def mixed_content_indicators(content: str) -> dict[str, bool]:
|
|
"""Return the individual signals used to classify mixed content."""
|
|
return {
|
|
"has_code_fences": bool(_CODE_FENCE_PATTERN.search(content)),
|
|
"has_json_blocks": bool(_JSON_BLOCK_START.search(content)),
|
|
"has_prose": len(_PROSE_PATTERN.findall(content)) > 5,
|
|
"has_search_results": bool(_SEARCH_RESULT_PATTERN.search(content)),
|
|
}
|
|
|
|
|
|
def split_into_sections(content: str) -> list[ContentSection]:
|
|
"""Parse mixed content into typed sections."""
|
|
sections: list[ContentSection] = []
|
|
lines = content.split("\n")
|
|
|
|
i = 0
|
|
while i < len(lines):
|
|
line = lines[i]
|
|
|
|
if match := _CODE_FENCE_PATTERN.match(line):
|
|
language = match.group(1) or "unknown"
|
|
code_lines = []
|
|
start_line = i
|
|
i += 1
|
|
|
|
while i < len(lines) and not lines[i].startswith("```"):
|
|
code_lines.append(lines[i])
|
|
i += 1
|
|
|
|
sections.append(
|
|
ContentSection(
|
|
content="\n".join(code_lines),
|
|
content_type=ContentType.SOURCE_CODE,
|
|
language=language,
|
|
start_line=start_line,
|
|
end_line=i,
|
|
is_code_fence=True,
|
|
)
|
|
)
|
|
i += 1
|
|
continue
|
|
|
|
if line.strip().startswith(("[", "{")):
|
|
json_content, end_i = _extract_json_block(lines, i)
|
|
if json_content:
|
|
sections.append(
|
|
ContentSection(
|
|
content=json_content,
|
|
content_type=ContentType.JSON_ARRAY,
|
|
start_line=i,
|
|
end_line=end_i,
|
|
)
|
|
)
|
|
i = end_i + 1
|
|
continue
|
|
|
|
if _SEARCH_RESULT_PATTERN.match(line):
|
|
search_lines = []
|
|
start_line = i
|
|
while i < len(lines) and _SEARCH_RESULT_PATTERN.match(lines[i]):
|
|
search_lines.append(lines[i])
|
|
i += 1
|
|
sections.append(
|
|
ContentSection(
|
|
content="\n".join(search_lines),
|
|
content_type=ContentType.SEARCH_RESULTS,
|
|
start_line=start_line,
|
|
end_line=i - 1,
|
|
)
|
|
)
|
|
continue
|
|
|
|
text_lines = [line]
|
|
start_line = i
|
|
i += 1
|
|
|
|
while i < len(lines):
|
|
next_line = lines[i]
|
|
if (
|
|
_CODE_FENCE_PATTERN.match(next_line)
|
|
or next_line.strip().startswith(("[", "{"))
|
|
or _SEARCH_RESULT_PATTERN.match(next_line)
|
|
):
|
|
break
|
|
text_lines.append(next_line)
|
|
i += 1
|
|
|
|
text_content = "\n".join(text_lines)
|
|
if text_content.strip():
|
|
sections.append(
|
|
ContentSection(
|
|
content=text_content,
|
|
content_type=ContentType.PLAIN_TEXT,
|
|
start_line=start_line,
|
|
end_line=i - 1,
|
|
)
|
|
)
|
|
|
|
return sections
|
|
|
|
|
|
def _extract_json_block(lines: list[str], start: int) -> tuple[str | None, int]:
|
|
"""Extract a complete JSON object or array block from line-oriented content."""
|
|
bracket_count = 0
|
|
brace_count = 0
|
|
json_lines = []
|
|
in_string = False
|
|
escaped = False
|
|
|
|
for i in range(start, len(lines)):
|
|
line = lines[i]
|
|
json_lines.append(line)
|
|
|
|
for ch in line:
|
|
if escaped:
|
|
escaped = False
|
|
continue
|
|
if ch == "\\":
|
|
if in_string:
|
|
escaped = True
|
|
continue
|
|
if ch == '"':
|
|
in_string = not in_string
|
|
continue
|
|
if in_string:
|
|
continue
|
|
if ch == "[":
|
|
bracket_count += 1
|
|
elif ch == "]":
|
|
bracket_count -= 1
|
|
elif ch == "{":
|
|
brace_count += 1
|
|
elif ch == "}":
|
|
brace_count -= 1
|
|
|
|
if bracket_count <= 0 and brace_count <= 0 and json_lines:
|
|
return "\n".join(json_lines), i
|
|
|
|
return None, start
|