Add assistant-ui data agent frontend
This commit is contained in:
+48
-7
@@ -99,6 +99,20 @@ class PromptPreflightResult:
|
||||
reason: str | None = None
|
||||
|
||||
|
||||
def _prepend_runtime_context(prompt: str, runtime_context: str | None) -> str:
|
||||
"""把仅模型可见的运行上下文附加到本轮 prompt 前,不污染用户历史消息。"""
|
||||
if not runtime_context or not runtime_context.strip():
|
||||
return prompt
|
||||
return '\n\n'.join(
|
||||
[
|
||||
'<system-reminder>',
|
||||
runtime_context.strip(),
|
||||
'</system-reminder>',
|
||||
prompt,
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class LocalCodingAgent:
|
||||
model_config: ModelConfig
|
||||
@@ -355,12 +369,18 @@ class LocalCodingAgent:
|
||||
),
|
||||
)
|
||||
|
||||
def run(self, prompt: str) -> AgentRunResult:
|
||||
def run(
|
||||
self,
|
||||
prompt: str,
|
||||
session_id: str | None = None,
|
||||
*,
|
||||
runtime_context: str | None = None,
|
||||
) -> AgentRunResult:
|
||||
self.managed_agent_id = None
|
||||
self.resume_source_session_id = None
|
||||
if self.plugin_runtime is not None:
|
||||
self.plugin_runtime.restore_session_state({})
|
||||
session_id = uuid4().hex
|
||||
session_id = session_id or uuid4().hex
|
||||
scratchpad_directory = self._ensure_scratchpad_directory(session_id)
|
||||
result = self._run_prompt(
|
||||
prompt,
|
||||
@@ -368,12 +388,19 @@ class LocalCodingAgent:
|
||||
session_id=session_id,
|
||||
scratchpad_directory=scratchpad_directory,
|
||||
existing_file_history=(),
|
||||
runtime_context=runtime_context,
|
||||
)
|
||||
self._accumulate_usage(result)
|
||||
self._finalize_managed_agent(result)
|
||||
return result
|
||||
|
||||
def resume(self, prompt: str, stored_session: StoredAgentSession) -> AgentRunResult:
|
||||
def resume(
|
||||
self,
|
||||
prompt: str,
|
||||
stored_session: StoredAgentSession,
|
||||
*,
|
||||
runtime_context: str | None = None,
|
||||
) -> AgentRunResult:
|
||||
self.managed_agent_id = None
|
||||
self.resume_source_session_id = stored_session.session_id
|
||||
session = AgentSessionState.from_persisted(
|
||||
@@ -390,7 +417,9 @@ class LocalCodingAgent:
|
||||
self.active_session_id = stored_session.session_id
|
||||
self.last_session = session
|
||||
self.last_session_path = str(
|
||||
self.runtime_config.session_directory / f'{stored_session.session_id}.json'
|
||||
self.runtime_config.session_directory
|
||||
/ stored_session.session_id
|
||||
/ 'session.json'
|
||||
)
|
||||
if self.plugin_runtime is not None:
|
||||
self.plugin_runtime.restore_session_state(stored_session.plugin_state)
|
||||
@@ -405,6 +434,7 @@ class LocalCodingAgent:
|
||||
session_id=stored_session.session_id,
|
||||
scratchpad_directory=scratchpad_directory,
|
||||
existing_file_history=stored_session.file_history,
|
||||
runtime_context=runtime_context,
|
||||
)
|
||||
self._accumulate_usage(result)
|
||||
self._finalize_managed_agent(result)
|
||||
@@ -418,6 +448,7 @@ class LocalCodingAgent:
|
||||
session_id: str,
|
||||
scratchpad_directory: Path | None,
|
||||
existing_file_history: tuple[dict[str, object], ...],
|
||||
runtime_context: str | None = None,
|
||||
) -> AgentRunResult:
|
||||
slash_result = preprocess_slash_command(self, prompt)
|
||||
if slash_result.handled and not slash_result.should_query:
|
||||
@@ -457,7 +488,11 @@ class LocalCodingAgent:
|
||||
scratchpad_directory=scratchpad_directory,
|
||||
)
|
||||
)
|
||||
session.append_user(effective_prompt)
|
||||
model_prompt = _prepend_runtime_context(
|
||||
effective_prompt,
|
||||
runtime_context,
|
||||
)
|
||||
session.append_user(effective_prompt, model_content=model_prompt)
|
||||
self.last_session = session
|
||||
self.active_session_id = session_id
|
||||
tool_specs = [tool.to_openai_tool() for tool in self.tool_registry.values()]
|
||||
@@ -3042,7 +3077,9 @@ class LocalCodingAgent:
|
||||
return normalized[: limit - 3] + '...'
|
||||
|
||||
def _ensure_scratchpad_directory(self, session_id: str) -> Path:
|
||||
scratchpad_directory = (self.runtime_config.scratchpad_root / session_id).resolve()
|
||||
scratchpad_directory = (
|
||||
self.runtime_config.scratchpad_root / session_id / 'scratchpad'
|
||||
).resolve()
|
||||
scratchpad_directory.mkdir(parents=True, exist_ok=True)
|
||||
return scratchpad_directory
|
||||
|
||||
@@ -3430,7 +3467,11 @@ class LocalCodingAgent:
|
||||
previous_turns = 0
|
||||
previous_tool_calls = 0
|
||||
previous_budget_state: dict[str, object] = {}
|
||||
existing_path = self.runtime_config.session_directory / f'{result.session_id}.json'
|
||||
existing_path = (
|
||||
self.runtime_config.session_directory / result.session_id / 'session.json'
|
||||
)
|
||||
if not existing_path.exists():
|
||||
existing_path = self.runtime_config.session_directory / f'{result.session_id}.json'
|
||||
if existing_path.exists():
|
||||
try:
|
||||
previous = load_agent_session(
|
||||
|
||||
Reference in New Issue
Block a user