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
106 lines
3.3 KiB
Python
106 lines
3.3 KiB
Python
"""Pure policy helpers for building memory retrieval queries."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
# Section delimiters surfaced in the embedding input so the embedder
|
|
# sees structured context rather than a wall of run-on text. Kept short
|
|
# so they don't dominate the embedding signal.
|
|
USER_DELIM = "### USER ###\n"
|
|
ASSISTANT_DELIM = "\n### PRIOR_ASSISTANT ###\n"
|
|
TOOL_DELIM = "\n### TOOL_OUTPUT ###\n"
|
|
|
|
|
|
def render_embedding_input(
|
|
*,
|
|
user_text: str,
|
|
recent_tool_outputs: tuple[str, ...],
|
|
recent_assistant_turns: tuple[str, ...],
|
|
) -> str:
|
|
"""Concatenate memory query sources into a delimited embedding input."""
|
|
parts: list[str] = []
|
|
for asst in recent_assistant_turns:
|
|
if asst:
|
|
parts.append(ASSISTANT_DELIM + asst)
|
|
for tool_out in recent_tool_outputs:
|
|
if tool_out:
|
|
parts.append(TOOL_DELIM + tool_out)
|
|
if user_text:
|
|
parts.append(USER_DELIM + user_text)
|
|
return "".join(parts)
|
|
|
|
|
|
def extract_memory_query_sources(
|
|
messages: list[dict[str, Any]] | None,
|
|
*,
|
|
lookback_assistant: int = 2,
|
|
lookback_tools: int = 3,
|
|
) -> tuple[str, tuple[str, ...], tuple[str, ...]]:
|
|
"""Extract user, tool, and assistant sources from chat-style messages.
|
|
|
|
Returns ``(user_text, recent_tool_outputs, recent_assistant_turns)``.
|
|
"""
|
|
if not messages:
|
|
return "", (), ()
|
|
|
|
latest_user = ""
|
|
assistant_turns: list[str] = []
|
|
tool_outputs: list[str] = []
|
|
|
|
for msg in reversed(messages):
|
|
role = msg.get("role")
|
|
content = msg.get("content", "")
|
|
|
|
if role == "user":
|
|
if isinstance(content, list):
|
|
_append_anthropic_tool_results(
|
|
content,
|
|
tool_outputs=tool_outputs,
|
|
lookback_tools=lookback_tools,
|
|
)
|
|
elif isinstance(content, str) and not latest_user:
|
|
latest_user = content
|
|
|
|
elif role == "assistant":
|
|
assistant_text = _assistant_text(content)
|
|
if assistant_text and len(assistant_turns) < lookback_assistant:
|
|
assistant_turns.append(assistant_text)
|
|
|
|
elif role == "tool":
|
|
if isinstance(content, str) and content and len(tool_outputs) < lookback_tools:
|
|
tool_outputs.append(content)
|
|
|
|
return (
|
|
latest_user,
|
|
tuple(reversed(tool_outputs)),
|
|
tuple(reversed(assistant_turns)),
|
|
)
|
|
|
|
|
|
def _append_anthropic_tool_results(
|
|
content: list[Any],
|
|
*,
|
|
tool_outputs: list[str],
|
|
lookback_tools: int,
|
|
) -> None:
|
|
for block in content:
|
|
if not isinstance(block, dict) or block.get("type") != "tool_result":
|
|
continue
|
|
tool_text = block.get("content", "")
|
|
if isinstance(tool_text, list):
|
|
tool_text = "\n".join(b.get("text", "") for b in tool_text if isinstance(b, dict))
|
|
if tool_text and len(tool_outputs) < lookback_tools:
|
|
tool_outputs.append(str(tool_text))
|
|
|
|
|
|
def _assistant_text(content: Any) -> str:
|
|
if isinstance(content, str):
|
|
return content
|
|
if isinstance(content, list):
|
|
text_parts = [
|
|
b.get("text", "") for b in content if isinstance(b, dict) and b.get("type") == "text"
|
|
]
|
|
return "\n".join(p for p in text_parts if p)
|
|
return ""
|