Files
DeepTutor/deeptutor/book/generation_context.py
Bingxi Zhao (Frank) 4c1e000d85 release: v1.7.3
Prepare v1.7.3 notes for reading and book editing, expanded provider and
agent connections, learning records, model streams, and desktop upgrades.

Sync README prose and 11 translations, archive older release notes, and
update the package version. Resolve release lint/type blockers with explicit
parser and capability contracts, compatible Book call forwarding, accurate
auth task types, and quoted SQLite schema identifiers. Cover positional Book
calls and quoted legacy columns; align KB seed tests with visible retrieval
failure reporting. Keep HTTP fixtures independent of host proxies and translate
new Portuguese UI copy.

Release notes: assets/releases/ver1-7-3.md
2026-11-16 02:17:36 +08:00

124 lines
4.0 KiB
Python

"""Owner-scoped instructions for one book generation operation."""
from contextlib import contextmanager
from contextvars import ContextVar
from typing import Any, AsyncGenerator
from deeptutor.agents.base_agent import BaseAgent
from deeptutor.services.llm import StreamOutcome
_snapshot: ContextVar[tuple[tuple[str, str], str] | None] = ContextVar(
"book_instructions", default=None
)
def _owner() -> tuple[str, str]:
from deeptutor.multi_user.paths import get_account_path_service
from deeptutor.services.workspace.context import current_workspace_id
return str(get_account_path_service().get_user_root()), current_workspace_id()
def _instructions(owner: tuple[str, str]) -> str:
from deeptutor.services.workspace import get_content_workspace_service
return get_content_workspace_service().workspace_instructions(owner[1])
@contextmanager
def book_instruction_scope():
"""Freeze preferences for a stage and its spawned tasks, with owner fencing."""
owner = _owner()
existing = _snapshot.get()
if existing is not None and existing[0] == owner:
yield
return
token = _snapshot.set((owner, _instructions(owner)))
try:
yield
finally:
_snapshot.reset(token)
def with_workspace_instructions(prompt: str) -> str:
owner = _owner()
snapshot = _snapshot.get()
instructions = (
snapshot[1] if snapshot is not None and snapshot[0] == owner else _instructions(owner)
)
if not instructions:
return prompt
return (
prompt.rstrip()
+ "\n\n[Workspace preferences]\n"
+ instructions
+ "\n[End workspace preferences]\nApply these learning and presentation preferences while preserving the required output schema and source-evidence rules."
)
class BookAgent(BaseAgent):
"""The shared generation boundary for ideation, sources and spine agents."""
async def stream_llm(
self,
user_prompt: str,
system_prompt: str,
messages: list[dict[str, Any]] | None = None,
temperature: float | None = None,
max_tokens: int | None = None,
model: str | None = None,
response_format: dict[str, Any] | None = None,
stage: str | None = None,
attachments: list[Any] | None = None,
trace_meta: dict[str, Any] | None = None,
reasoning_effort: str | None = None,
tools: list[dict[str, Any]] | None = None,
outcome: StreamOutcome | None = None,
) -> AsyncGenerator[str, None]:
async for chunk in super().stream_llm(
user_prompt=user_prompt,
system_prompt=with_workspace_instructions(system_prompt),
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
model=model,
response_format=response_format,
stage=stage,
attachments=attachments,
trace_meta=trace_meta,
reasoning_effort=reasoning_effort,
tools=tools,
outcome=outcome,
):
yield chunk
async def call_llm(
self,
user_prompt: str,
system_prompt: str,
messages: list[dict[str, Any]] | None = None,
response_format: dict[str, str] | None = None,
temperature: float | None = None,
max_tokens: int | None = None,
model: str | None = None,
verbose: bool = True,
stage: str | None = None,
attachments: list[Any] | None = None,
trace_meta: dict[str, Any] | None = None,
reasoning_effort: str | None = None,
) -> str:
return await super().call_llm(
user_prompt=user_prompt,
system_prompt=with_workspace_instructions(system_prompt),
messages=messages,
response_format=response_format,
temperature=temperature,
max_tokens=max_tokens,
model=model,
verbose=verbose,
stage=stage,
attachments=attachments,
trace_meta=trace_meta,
reasoning_effort=reasoning_effort,
)