feat(agent-runner): expose effective prompt and transcript history

This commit is contained in:
huanghuoguoguo
2026-06-04 00:21:30 +08:00
parent 08c51118c5
commit 7053acfb1b
14 changed files with 363 additions and 85 deletions

View File

@@ -408,8 +408,8 @@ class TestChatHandlerAsyncBehavior:
assert query.resp_messages[1].content == 'Response 2'
@pytest.mark.asyncio
async def test_history_update_recreates_conversation_if_tool_resets_it(self):
"""History update should tolerate CREATE_NEW_CONVERSATION during runner execution."""
async def test_agent_turn_recreates_conversation_if_tool_resets_it(self):
"""Agent turn bookkeeping should tolerate CREATE_NEW_CONVERSATION during runner execution."""
from langbot.pkg.pipeline.process.handlers.chat import ChatMessageHandler
from langbot.pkg.pipeline import entities
@@ -449,7 +449,7 @@ class TestChatHandlerAsyncBehavior:
assert results[0].result_type == entities.ResultType.CONTINUE
mock_ap.sess_mgr.get_conversation.assert_awaited_once()
assert query.session.using_conversation is new_conversation
assert new_conversation.messages == [query.user_message, response]
assert new_conversation.messages == []
@pytest.mark.asyncio
async def test_runner_not_found_error(self):

View File

@@ -159,4 +159,4 @@ class TestBuildAdapterContext:
context = QueryEntryAdapter.build_adapter_context(query, binding=None)
assert context == {'params': {}, 'query_id': 123}
assert context == {'params': {}, 'query_id': 123, 'prompt_get': False}

View File

@@ -2,8 +2,6 @@
from __future__ import annotations
import pytest
from unittest.mock import Mock, MagicMock, patch
import datetime
from langbot.pkg.agent.runner.host_models import (
AgentEventEnvelope,
@@ -17,7 +15,6 @@ from langbot.pkg.agent.runner.event_log_store import EventLogStore
from langbot.pkg.agent.runner.transcript_store import TranscriptStore
from langbot.pkg.agent.runner.session_registry import get_session_registry
from langbot_plugin.api.entities.builtin.agent_runner.event import (
AgentEventContext,
ActorContext,
)
from langbot_plugin.api.entities.builtin.agent_runner.input import AgentInput
@@ -386,9 +383,7 @@ class TestEventLogStoreRealSQLite:
async def db_engine(self):
"""Create an in-memory SQLite database for testing."""
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
from langbot.pkg.entity.persistence.base import Base
from langbot.pkg.entity.persistence.event_log import EventLog
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
@@ -483,9 +478,7 @@ class TestTranscriptStoreRealSQLite:
async def db_engine(self):
"""Create an in-memory SQLite database for testing."""
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
from langbot.pkg.entity.persistence.base import Base
from langbot.pkg.entity.persistence.transcript import Transcript
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
@@ -521,6 +514,44 @@ class TestTranscriptStoreRealSQLite:
assert len(items) == 3
assert items[0]["conversation_id"] == "conv_001"
@pytest.mark.asyncio
async def test_get_legacy_provider_messages_projects_transcript_history(self, db_engine):
"""Transcript is the canonical source; legacy Pipeline readers get a Message view."""
store = TranscriptStore(db_engine)
await store.append_transcript(
transcript_id="trans_view_001",
event_id="evt_view_001",
conversation_id="conv_view",
role="user",
content="User text",
content_json={
"role": "user",
"content": [{"type": "text", "text": "User structured text"}],
},
)
await store.append_transcript(
transcript_id="trans_view_002",
event_id="evt_view_002",
conversation_id="conv_view",
role="tool",
item_type="tool_result",
content="ignored tool result",
)
await store.append_transcript(
transcript_id="trans_view_003",
event_id="evt_view_003",
conversation_id="conv_view",
role="assistant",
content="Assistant text",
)
messages = await store.get_legacy_provider_messages("conv_view")
assert [message.role for message in messages] == ["user", "assistant"]
assert messages[0].content[0].text == "User structured text"
assert messages[1].content == "Assistant text"
@pytest.mark.asyncio
async def test_search_transcript_real_db(self, db_engine):
"""Test search_transcript with real DB."""
@@ -586,7 +617,7 @@ def mock_db_engine():
@pytest.fixture
def mock_handler():
"""Create a mock handler for testing actions."""
from langbot_plugin.runtime.io.handler import Handler, ActionResponse
from langbot_plugin.runtime.io.handler import Handler
class MockHandler(Handler):
def __init__(self):

View File

@@ -593,6 +593,7 @@ class TestQueryEntryAdapterParams:
context = plugin_connector.contexts[0]
assert "prompt" not in context
assert "prompt" not in context["adapter"]["extra"]
assert context["context"]["available_apis"]["prompt_get"] is True
@pytest.mark.asyncio
async def test_params_filtering_keeps_public_param(self, clean_agent_state):

View File

@@ -50,6 +50,8 @@ def make_host_model_runner_descriptor(
multimodal_input: bool = True,
tool_calling: bool = True,
knowledge_retrieval: bool = True,
skill_authoring: bool = False,
skill_injection: bool = False,
):
from langbot.pkg.agent.runner.descriptor import AgentRunnerDescriptor
@@ -69,6 +71,8 @@ def make_host_model_runner_descriptor(
'tool_calling': tool_calling,
'knowledge_retrieval': knowledge_retrieval,
'multimodal_input': multimodal_input,
'skill_authoring': skill_authoring,
'skill_injection': skill_injection,
},
permissions={
'models': ['list', 'invoke', 'stream'],

View File

@@ -17,6 +17,32 @@ from langbot_plugin.api.entities.builtin.provider.prompt import Prompt
from langbot_plugin.api.entities.builtin.provider.session import Conversation, LauncherTypes, Session
class _FakeRunnerDescriptor:
config_schema = [
{'name': 'model', 'type': 'model-fallback-selector'},
{'name': 'prompt', 'type': 'prompt-editor', 'default': []},
{'name': 'knowledge-bases', 'type': 'knowledge-base-multi-selector', 'default': []},
]
permissions = {
'models': ['list', 'invoke', 'stream'],
'tools': ['list', 'detail', 'call'],
'knowledge_bases': ['list', 'retrieve'],
}
capabilities = {
'tool_calling': True,
'knowledge_retrieval': True,
'multimodal_input': True,
'skill_authoring': True,
'skill_injection': True,
}
def supports_tool_calling(self):
return self.capabilities.get('tool_calling', False)
def supports_knowledge_retrieval(self):
return self.capabilities.get('knowledge_retrieval', False)
def _make_query() -> Query:
message_chain = MessageChain([Plain(text='create a skill')])
return Query(
@@ -34,11 +60,13 @@ def _make_query() -> Query:
pipeline_uuid='pipe-1',
pipeline_config={
'ai': {
'runner': {'runner': 'local-agent'},
'local-agent': {
'model': {'primary': 'model-1', 'fallbacks': []},
'prompt': 'default',
'knowledge-bases': [],
'runner': {'id': 'plugin:langbot/local-agent/default'},
'runner_config': {
'plugin:langbot/local-agent/default': {
'model': {'primary': 'model-1', 'fallbacks': []},
'prompt': [],
'knowledge-bases': [],
},
},
},
'trigger': {'misc': {}},
@@ -57,6 +85,15 @@ def _make_conversation() -> Conversation:
)
async def _passthrough_preproc_event(event, bound_plugins):
return SimpleNamespace(
event=SimpleNamespace(
default_prompt=event.default_prompt,
prompt=event.prompt,
)
)
def _make_app(*, skill_service) -> SimpleNamespace:
session = Session(launcher_type=LauncherTypes.PERSON, launcher_id='launcher-1', sender_id='sender-1')
conversation = _make_conversation()
@@ -83,6 +120,7 @@ def _make_app(*, skill_service) -> SimpleNamespace:
pipeline_service=SimpleNamespace(
get_pipeline=AsyncMock(return_value={'extensions_preferences': {'enable_all_skills': True}})
),
agent_runner_registry=SimpleNamespace(get=AsyncMock(return_value=_FakeRunnerDescriptor())),
skill_mgr=SimpleNamespace(
build_skill_aware_prompt_addition=Mock(return_value=''),
skills={},
@@ -197,6 +235,49 @@ async def test_preproc_skips_injection_when_addendum_is_empty():
assert 'Available Skills' not in (query.prompt.messages[0].content or '')
@pytest.mark.asyncio
async def test_preproc_uses_transcript_history_view_when_available():
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
conversation = app.sess_mgr.get_conversation.return_value
conversation.messages = [Message(role='user', content='legacy history')]
app.plugin_connector.emit_event = AsyncMock(side_effect=_passthrough_preproc_event)
transcript_messages = [
Message(role='user', content='from transcript user'),
Message(role='assistant', content='from transcript assistant'),
]
stage = preproc_module.PreProcessor(app)
stage._load_agent_runner_history_messages = AsyncMock(return_value=transcript_messages)
query = _make_query()
result = await stage.process(query, 'PreProcessor')
assert result.result_type == entities_module.ResultType.CONTINUE
assert query.messages == transcript_messages
@pytest.mark.asyncio
async def test_preproc_falls_back_to_conversation_messages_when_transcript_empty():
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
legacy_messages = [Message(role='user', content='legacy history')]
app.sess_mgr.get_conversation.return_value.messages = legacy_messages
app.plugin_connector.emit_event = AsyncMock(side_effect=_passthrough_preproc_event)
stage = preproc_module.PreProcessor(app)
stage._load_agent_runner_history_messages = AsyncMock(return_value=None)
query = _make_query()
result = await stage.process(query, 'PreProcessor')
assert result.result_type == entities_module.ResultType.CONTINUE
assert query.messages == legacy_messages
async def stage_process_capture(preproc_module, app, query):
"""Run PreProcessor.process and return the result while keeping ``query``
accessible to the assertions (process mutates query in place)."""