feat: enhance chat CLI and TUI with initial opener handling and improved prompt logic

This commit is contained in:
Xin Wang
2026-03-10 23:55:53 +08:00
parent ef2614a70a
commit a55ca37c39
5 changed files with 278 additions and 26 deletions

View File

@@ -32,8 +32,10 @@ if str(EXAMPLES_DIR) not in sys.path:
sys.path.insert(0, str(EXAMPLES_DIR))
from chat_cli import (
APP_ID,
API_KEY,
BASE_URL,
_extract_chat_init_opener,
_extract_text_from_event,
_interactive_prompt_text,
_normalize_option,
@@ -451,10 +453,11 @@ class FastGPTWorkbench(App[None]):
raise RuntimeError("Set API_KEY and BASE_URL in examples/.env before starting chat_tui.py")
self._refresh_sidebar()
self._set_status("Ready", "Fresh session")
initial_message = self._initial_session_message()
self._append_message(
role="system",
title="Session",
content="Start typing below. FastGPT workflow events will appear in the left rail.",
content=initial_message,
)
self.query_one("#composer", TextArea).focus()
@@ -464,8 +467,9 @@ class FastGPTWorkbench(App[None]):
def _refresh_sidebar(self) -> None:
session_panel = self.query_one("#session_panel", Static)
base_url = BASE_URL or ""
app_id = APP_ID or "(not set)"
session_panel.update(
f"Session\n\nchatId: {self.chat_id}\nbaseUrl: {base_url}"
f"Session\n\nchatId: {self.chat_id}\nappId: {app_id}\nbaseUrl: {base_url}"
)
def _set_status(self, heading: str, detail: str) -> None:
@@ -481,6 +485,24 @@ class FastGPTWorkbench(App[None]):
except Exception:
return content
def _default_session_message(self) -> str:
return "Start typing below. FastGPT workflow events will appear in the left rail."
def _initial_session_message(self) -> str:
if not APP_ID:
return self._default_session_message()
try:
with ChatClient(api_key=API_KEY, base_url=BASE_URL) as client:
response = client.get_chat_init(appId=APP_ID, chatId=self.chat_id)
response.raise_for_status()
opener = _extract_chat_init_opener(response.json())
except Exception as exc:
self._log_event(f"[init] Failed to load app opener: {exc}")
return self._default_session_message()
return opener or self._default_session_message()
def _append_message(self, role: str, title: str, content: str) -> str:
self._message_counter += 1
widget_id = f"message-{self._message_counter}"
@@ -546,7 +568,8 @@ class FastGPTWorkbench(App[None]):
self._start_turn(result, title="Workflow Input", role="workflow")
def _present_interactive(self, event: FastGPTInteractiveEvent) -> None:
self._log_event(f"[interactive] {event.interaction_type}")
prompt_summary = _interactive_prompt_text(event.data, event.interaction_type).replace("\n", " / ")
self._log_event(f"[interactive] {event.interaction_type}: {prompt_summary}")
if event.interaction_type == "userInput":
self.push_screen(InteractiveInputScreen(event), self._handle_interactive_result)
return
@@ -569,10 +592,11 @@ class FastGPTWorkbench(App[None]):
self.query_one("#messages", VerticalScroll).remove_children()
self._refresh_sidebar()
self._set_status("Ready", "Started a new random session")
initial_message = self._initial_session_message()
self._append_message(
role="system",
title="Session",
content="New chat created. Start typing below.",
content=initial_message,
)
self._log_event(f"[local] Started new chatId {self.chat_id}")