Files
privateGPT/private_gpt/components/context/models/context_layer.py
Javier Martinez 21d42fd97a feat: code execution v4 (#2295)
* feat: add bundle to remove

* fix: spaces

* feat: add xml render as skill spec

* feat: add skill volume root to cache

* fix: deduplicate values

* fix: change the mount path to skill_id

* fix: use different paths

* feat: add principal

(cherry picked from commit 5db64fe721d5706440ce9f342b1388ffe742bc16)

# Conflicts:
#	private_gpt/components/code_execution/base.py
#	private_gpt/components/code_execution/code_execution_component.py
#	private_gpt/components/code_execution/local.py
#	private_gpt/components/environment/manager.py
#	private_gpt/components/tools/builders/bash_tool_builder.py
#	private_gpt/components/tools/builders/text_editor_tool_builder.py
#	private_gpt/components/tools/processors/bash_processor.py

* fix: mypy

...

* fix: config

* fix: sandbox config

* feat: add forward cookies

* fix: loop

* feat: add present server

* feat: add feature flag for tools

* refactor: move principal to another better place

* feat: add api key principal

* docs: fix docs

* docs: add present server

* fix: principal

* fix: mypy

* fix: avoid to block the loop

* fix: blocks in expansion

* fix: remove maximum concurrent users

...

* fix: multiplexer

* fix: readers

* fix: more fixes

...

* fix: impl

* feat: tool scheduler

* feat: add adaptative

* feat: add chat worker

* fix: config

* fix: max

* feat: add chat/tools workers

* fix: mypy

* feat: add generic scheduler

* fix: get result

* feat: do serializable the tool executor

* fix: tools

* fix: config

* fix: config

* fix: args

* fix: config

* fix: serializer

* Revert "fix: blocks in expansion"

This reverts commit a2110f94a8.

* fix: unify all logic

* feat: add ingestion scheduler

* fix: settings

* fix: config

* feat: add arq worker to chat

* fix: arq worker

* fix: add nest

* fix: mypy

* fix: await

* fix: script stress

* fix: tokenizer

* fix: chat scheduler

* fix: mypy

* fix: add async tokenizer

* fix: improve condense

* fix: tool scheduler

* feat: add initial real async chat worker

* fix: mypy

* fix: do resumable local executor

...

...

...

fix: revert usleess changes

fix: remove parent chat job

fix: refactor

fix: loop

ref: rename models

fix: chat engine

fix: mypy

...

...

...

fix: fix deps

* fix: tests

* fix: tests

* ...

* fix: stream

* fix: config

* fix: scheduler

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Handle PGPT_WORKER_MODE=celery in health check worker status

* fix: cancel

* fix: arch

* fix: test ingestion

* fix: deserialization of chat messages

* fix: broken results

* fix: mypy

* fix: test

* fix: config

* fix: remove arq tool worker

* fix: output cls

* fix: preserve early resumable tool callbacks

* fix: preserve async tool result order

* refactor: address worker PR review comments

* fix: mypy

* test: colocate ARQ chat enqueue coverage

* fix: remove redis from tests

* fix: mypy

* fix: tests

* test: isolate chat mocks and cancellation timing

* fix: tests

* fix: tests

* fix: test

(cherry picked from commit f8ee460af2)

* fix: worker config

* test: remove flaky chat cancellation assertion

(cherry picked from commit 1115ff2349)

# Conflicts:
#	tests/server/chat/test_chat_routes.py

* fix: emit chat pings from stream listeners

* fix: don't duplciate the output

* fix: rss memory

...

* fix: pass the args

* fix: threads

* fix: websearch

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-07-15 13:05:23 +02:00

251 lines
8.5 KiB
Python

from typing import Annotated, Literal
from llama_index.core.base.llms.types import TextBlock
from pydantic import BaseModel, ConfigDict, Field
from private_gpt.components.chat.models.chat_config_models import ToolSpec
from private_gpt.components.context.models.layer_type import LayerType
from private_gpt.components.engines.citations.types import Document
from private_gpt.components.sandbox.content_bundle import ContentBundle
class BaseContextLayer(BaseModel):
"""Common fields shared by every context layer."""
source: str = Field(
default="request",
description="Origin of the layer, e.g. 'platform', 'skill:git', 'mcp'.",
)
priority: int = Field(
default=1000,
description=(
"Render priority for prompt layers. Lower values are rendered first. "
"State-only layers ignore this field."
),
)
model_config = ConfigDict(frozen=True, arbitrary_types_allowed=True)
def render(self) -> str:
"""Return text to include in the system prompt (empty for state layers)."""
return ""
class UserInstructionsLayer(BaseContextLayer):
"""User/system provided baseline instructions."""
type: Literal[LayerType.USER_INSTRUCTIONS] = Field(
default=LayerType.USER_INSTRUCTIONS, frozen=True
)
priority: int = Field(default=100, frozen=True)
text: str | list[TextBlock] | None = Field(description="Raw instruction text.")
def render(self) -> str:
if self.text is None:
return ""
if isinstance(self.text, str):
return self.text.strip()
texts = [block.text.strip() for block in self.text if block.text.strip()]
return "\n\n".join(texts)
class RuntimeInstructionsLayer(BaseContextLayer):
"""Transient runtime instructions (e.g. condensation hints)."""
type: Literal[LayerType.RUNTIME_INSTRUCTIONS] = Field(
default=LayerType.RUNTIME_INSTRUCTIONS, frozen=True
)
priority: int = Field(default=200, frozen=True)
text: str = Field(description="Additional instruction text.")
def render(self) -> str:
return self.text
class SkillCatalogEntry(BaseModel):
id: str = Field(description="Skill identifier.")
name: str = Field(description="Skill frontmatter name.")
description: str = Field(description="Skill frontmatter description.")
loading: Literal["eager", "lazy"] = Field(description="Skill loading mode.")
location: str = Field(
default="",
description="Path to the skill's SKILL.md inside the execution "
"environment, e.g. /mnt/skills/pdf/SKILL.md.",
)
resources: list[str] = Field(
default_factory=list,
description="Bundled file paths relative to the skill directory.",
)
class SkillCatalogLayer(BaseContextLayer):
"""Catalog of available-but-not-yet-loaded skills shown to the LLM."""
type: Literal[LayerType.SKILL_CATALOG] = Field(
default=LayerType.SKILL_CATALOG, frozen=True
)
priority: int = Field(default=300, frozen=True)
entries: list[SkillCatalogEntry] = Field(
default_factory=list,
description="List of available skill entries.",
)
def render(self) -> str:
if not self.entries:
return ""
lines = ["<available_skills>"]
for entry in self.entries:
lines.append(" <skill>")
lines.append(f" <name>{entry.name}</name>")
lines.append(f" <description>{entry.description}</description>")
if entry.location:
lines.append(f" <location>{entry.location}</location>")
if entry.resources:
lines.append(" <resources>")
lines.extend(
f" <resource>{resource}</resource>"
for resource in entry.resources
)
lines.append(" </resources>")
lines.append(" </skill>")
lines.append("</available_skills>")
return "\n".join(lines)
class SkillBodyLayer(BaseContextLayer):
"""Full instructions for one activated skill."""
type: Literal[LayerType.SKILL_BODY] = Field(
default=LayerType.SKILL_BODY, frozen=True
)
priority: int = Field(default=400, frozen=True)
skill_id: str = Field(description="Skill identifier.")
name: str = Field(description="Skill frontmatter name.")
version: str = Field(description="Skill version token.")
instructions: str = Field(description="Skill instruction body content.")
location: str = Field(
default="",
description="Skill directory inside the execution environment, "
"e.g. /mnt/skills/pdf/.",
)
resources: list[str] = Field(
default_factory=list,
description="Bundled file paths relative to the skill directory.",
)
render_as_xml: bool = Field(
default=True,
description="When True, wrap output in <skill_content> XML tags. "
"Eager skills render as plain text (False).",
)
def render(self) -> str:
body = self.instructions.strip()
parts: list[str] = []
if self.location:
parts.append(f"Skill directory: {self.location}")
parts.append(
"Relative paths in this skill are relative to the skill directory."
)
if self.resources:
resource_lines = ["<skill_resources>"]
resource_lines.extend(f" <file>{r}</file>" for r in self.resources)
resource_lines.append("</skill_resources>")
parts.append("\n".join(resource_lines))
footer = "\n\n".join(parts)
inner = f"{body}\n\n{footer}" if body and footer else (body or footer)
if self.render_as_xml:
return f'<skill_content name="{self.name}">\n{inner}\n</skill_content>'
return inner
class ToolInstructionsLayer(BaseContextLayer):
"""Per-tool instructions injected when a tool is available."""
type: Literal[LayerType.TOOL_INSTRUCTIONS] = Field(
default=LayerType.TOOL_INSTRUCTIONS, frozen=True
)
priority: int = Field(default=450, frozen=True)
tool_name: str = Field(
description="Canonical tool name these instructions apply to."
)
instructions: str = Field(description="Instruction text for this tool.")
def render(self) -> str:
return self.instructions
class DocumentLayer(BaseContextLayer):
"""One document injected as context — wraps the real Document entity."""
type: Literal[LayerType.DOCUMENT] = Field(default=LayerType.DOCUMENT, frozen=True)
priority: int = Field(default=2000, frozen=True)
document: Document = Field(description="The Document domain object.")
def render(self) -> str:
return "" # state layer — never in system prompt
class ToolDefinitionsLayer(BaseContextLayer):
"""Tool specs available to the LLM — consumed programmatically, not rendered."""
type: Literal[LayerType.TOOL_DEFINITIONS] = Field(
default=LayerType.TOOL_DEFINITIONS, frozen=True
)
priority: int = Field(default=2000, frozen=True)
tools: list[ToolSpec] = Field(
default_factory=list,
description="List of ToolSpec instances.",
)
def render(self) -> str:
return "" # state layer — never in system prompt
class ContextPromptLayer(BaseContextLayer):
"""Rendered context section to include in the system prompt."""
type: Literal[LayerType.CONTEXT] = Field(default=LayerType.CONTEXT, frozen=True)
priority: int = Field(default=600, frozen=True)
text: str = Field(description="Rendered context prompt text.")
def render(self) -> str:
return self.text
class ContentBundlesLayer(BaseContextLayer):
"""Skill content bundles — consumed by tool builders, not rendered."""
type: Literal[LayerType.CONTENT_BUNDLES] = Field(
default=LayerType.CONTENT_BUNDLES, frozen=True
)
priority: int = Field(default=2000, frozen=True)
bundles: list[ContentBundle] = Field(default_factory=list)
to_remove: list[str] = Field(
default_factory=list,
description="Canonical paths of skill bundles to remove from the sandbox.",
)
model_config = ConfigDict(frozen=True, arbitrary_types_allowed=True)
def render(self) -> str:
return "" # state layer — never in system prompt
AnyContextLayer = Annotated[
UserInstructionsLayer
| RuntimeInstructionsLayer
| ContextPromptLayer
| SkillCatalogLayer
| SkillBodyLayer
| ToolInstructionsLayer
| DocumentLayer
| ToolDefinitionsLayer
| ContentBundlesLayer,
Field(discriminator="type"),
]