mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-15 00:46:07 +00:00
feat(lark): add functions for current input definitions and visible form content handling
feat(qqofficial): update fallback text handling for non-streaming scenarios feat(difysvapi): enhance form content processing for interactive fields and actions test: add unit tests for Lark and QQ Official adapter functionalities
This commit is contained in:
@@ -1,12 +1,97 @@
|
||||
"""Tests for Lark adapter helper behavior."""
|
||||
|
||||
from langbot.pkg.platform.sources.lark import (
|
||||
LarkAdapter,
|
||||
_lark_clean_form_content,
|
||||
_lark_completed_input_lines,
|
||||
_lark_current_input_defs,
|
||||
_lark_extract_action_form_inputs,
|
||||
_lark_should_update_stream_element,
|
||||
_lark_visible_form_content,
|
||||
)
|
||||
|
||||
|
||||
def test_lark_current_input_defs_only_returns_active_stage():
|
||||
input_defs = [
|
||||
{'output_variable_name': 'us_input', 'type': 'paragraph'},
|
||||
{'output_variable_name': 'xiala', 'type': 'select'},
|
||||
]
|
||||
|
||||
assert _lark_current_input_defs(
|
||||
{
|
||||
'_current_input_field': 'xiala',
|
||||
'input_defs': input_defs,
|
||||
}
|
||||
) == [input_defs[1]]
|
||||
assert (
|
||||
_lark_current_input_defs(
|
||||
{
|
||||
'_action_select_only': True,
|
||||
'input_defs': input_defs,
|
||||
}
|
||||
)
|
||||
== []
|
||||
)
|
||||
|
||||
|
||||
def test_lark_form_field_elements_only_render_active_stage():
|
||||
adapter = LarkAdapter.model_construct()
|
||||
form_data = {
|
||||
'_current_input_field': 'xiala',
|
||||
'input_defs': [
|
||||
{'output_variable_name': 'us_input', 'type': 'paragraph'},
|
||||
{
|
||||
'output_variable_name': 'xiala',
|
||||
'type': 'select',
|
||||
'option_source': {'type': 'constant', 'value': ['1', '2']},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
elements, input_name_map, file_help_lines = adapter._build_lark_form_field_elements(form_data)
|
||||
|
||||
assert len(elements) == 1
|
||||
assert elements[0]['tag'] == 'select_static'
|
||||
assert elements[0]['label']['content'] == 'xiala'
|
||||
assert list(input_name_map.values()) == ['xiala']
|
||||
assert file_help_lines == []
|
||||
|
||||
|
||||
def test_lark_form_stage_skips_closed_streaming_element_update():
|
||||
assert not _lark_should_update_stream_element(
|
||||
resume_from=False,
|
||||
form_data={'_current_input_field': 'xiala'},
|
||||
msg_seq=1,
|
||||
is_final=True,
|
||||
)
|
||||
assert _lark_should_update_stream_element(
|
||||
resume_from=False,
|
||||
form_data=None,
|
||||
msg_seq=1,
|
||||
is_final=True,
|
||||
)
|
||||
|
||||
|
||||
def test_lark_final_action_stage_interleaves_prompts_and_completed_values():
|
||||
form_content = _lark_visible_form_content(
|
||||
{
|
||||
'_action_select_only': True,
|
||||
'raw_form_content': ('11\nQuestion\n{{#$output.us_input#}}\nChoose an answer\n{{#$output.xiala#}}\n'),
|
||||
'all_input_defs': [
|
||||
{'output_variable_name': 'us_input', 'type': 'paragraph'},
|
||||
{'output_variable_name': 'xiala', 'type': 'select'},
|
||||
],
|
||||
'inputs': {'us_input': 'hello', 'xiala': '2'},
|
||||
}
|
||||
)
|
||||
|
||||
assert '{{#$output.' not in form_content
|
||||
assert form_content.startswith('11\nQuestion')
|
||||
assert form_content.index('Question') < form_content.index('us_input')
|
||||
assert form_content.index('us_input') < form_content.index('Choose an answer')
|
||||
assert form_content.index('Choose an answer') < form_content.index('xiala')
|
||||
|
||||
|
||||
def test_lark_completed_input_lines_include_text_select_and_files():
|
||||
lines = _lark_completed_input_lines(
|
||||
{
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
||||
|
||||
from langbot.libs.qq_official_api.api import (
|
||||
QQ_SELECT_ACTION_PREFIX,
|
||||
build_keyboard_from_select_field,
|
||||
@@ -66,6 +68,80 @@ def test_qq_non_select_field_does_not_build_keyboard():
|
||||
assert build_keyboard_from_select_field(form_data)['content']['rows'] == []
|
||||
|
||||
|
||||
def _stream_test_adapter():
|
||||
from langbot.pkg.platform.sources.qqofficial import QQOfficialAdapter
|
||||
|
||||
adapter = QQOfficialAdapter.model_construct()
|
||||
adapter.logger = AsyncMock()
|
||||
adapter.bot = MagicMock()
|
||||
adapter.bot.send_stream_msg = AsyncMock(return_value={'id': 'stream-1'})
|
||||
adapter.ap = None
|
||||
adapter._stream_ctx = {}
|
||||
adapter._stream_ctx_ts = {}
|
||||
adapter._fallback_text = {}
|
||||
adapter._fallback_text_ts = {}
|
||||
return adapter
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_qq_stream_uses_cumulative_chunks_as_snapshots():
|
||||
adapter = _stream_test_adapter()
|
||||
adapter._stream_ctx['message-1'] = {
|
||||
'user_openid': 'user-1',
|
||||
'msg_id': 'source-1',
|
||||
'stream_msg_id': None,
|
||||
'msg_seq': 1,
|
||||
'index': 0,
|
||||
'last_update_ts': 0,
|
||||
'accumulated_text': '',
|
||||
'sent_length': 0,
|
||||
'session_started': False,
|
||||
}
|
||||
adapter._stream_ctx_ts['message-1'] = time.time()
|
||||
source = MagicMock()
|
||||
|
||||
await adapter.reply_message_chunk(
|
||||
source,
|
||||
{'resp_message_id': 'message-1'},
|
||||
platform_message.MessageChain([platform_message.Plain(text='<think>one')]),
|
||||
)
|
||||
await adapter.reply_message_chunk(
|
||||
source,
|
||||
{'resp_message_id': 'message-1'},
|
||||
platform_message.MessageChain([platform_message.Plain(text='<think>one two')]),
|
||||
is_final=True,
|
||||
)
|
||||
|
||||
assert [call.kwargs['content'] for call in adapter.bot.send_stream_msg.await_args_list] == [
|
||||
'<think>one',
|
||||
' two',
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_qq_non_streaming_fallback_keeps_latest_snapshot_only():
|
||||
from langbot.pkg.platform.sources.qqofficial import QQOfficialAdapter
|
||||
|
||||
adapter = _stream_test_adapter()
|
||||
source = MagicMock()
|
||||
|
||||
with patch.object(QQOfficialAdapter, 'reply_message', new=AsyncMock()) as reply_message:
|
||||
await adapter.reply_message_chunk(
|
||||
source,
|
||||
{'resp_message_id': 'message-1'},
|
||||
platform_message.MessageChain([platform_message.Plain(text='Hel')]),
|
||||
)
|
||||
await adapter.reply_message_chunk(
|
||||
source,
|
||||
{'resp_message_id': 'message-1'},
|
||||
platform_message.MessageChain([platform_message.Plain(text='Hello')]),
|
||||
is_final=True,
|
||||
)
|
||||
|
||||
sent_chain = reply_message.await_args.args[1]
|
||||
assert str(sent_chain) == 'Hello'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_qq_select_click_enqueues_input_progress_query():
|
||||
import langbot.pkg.core.app # noqa: F401
|
||||
|
||||
@@ -246,6 +246,46 @@ class TestDifyHumanInputForms:
|
||||
assert 'comment (paragraph)' in display_form_content
|
||||
assert snapshot['form_content'] == display_form_content
|
||||
|
||||
def test_interactive_form_content_is_split_by_field_placeholders(self):
|
||||
from langbot.pkg.provider.runners.difysvapi import (
|
||||
_action_select_form_data,
|
||||
_field_input_form_data,
|
||||
)
|
||||
|
||||
input_defs = [
|
||||
{'output_variable_name': 'us_input', 'type': 'paragraph'},
|
||||
{
|
||||
'output_variable_name': 'xiala',
|
||||
'type': 'select',
|
||||
'option_source': {'type': 'constant', 'value': ['1', '2']},
|
||||
},
|
||||
]
|
||||
pending_form = {
|
||||
'raw_form_content': (
|
||||
'1\n请输入你的问题\n{{#$output.us_input#}}\n请选择你的答案\n{{#$output.xiala#}}\n提交前请确认'
|
||||
),
|
||||
'input_defs': input_defs,
|
||||
'actions': [{'id': 'yes', 'title': 'yes'}],
|
||||
'inputs': {},
|
||||
}
|
||||
|
||||
first_step = _field_input_form_data(pending_form, input_defs[0])
|
||||
second_step = _field_input_form_data(pending_form, input_defs[1])
|
||||
action_step = _action_select_form_data(pending_form)
|
||||
|
||||
assert first_step['form_content'] == '1\n请输入你的问题'
|
||||
assert second_step['form_content'] == '请选择你的答案'
|
||||
assert action_step['form_content'] == '提交前请确认'
|
||||
|
||||
def test_interactive_form_content_without_placeholder_uses_compatibility_fallback(self):
|
||||
from langbot.pkg.provider.runners.difysvapi import _field_input_form_data
|
||||
|
||||
field = {'output_variable_name': 'comment', 'type': 'paragraph'}
|
||||
|
||||
form_data = _field_input_form_data({'raw_form_content': 'Please review', 'input_defs': [field]}, field)
|
||||
|
||||
assert form_data['form_content'] == 'comment (paragraph): reply "comment: <value>"'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_match_pending_form_collects_select_and_text_inputs(self):
|
||||
from langbot.pkg.provider.runners import difysvapi
|
||||
|
||||
Reference in New Issue
Block a user