|
|
|
@@ -23,7 +23,6 @@ import android.os.Looper;
|
|
|
|
|
import android.provider.Settings;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.text.InputType;
|
|
|
|
|
import android.text.method.LinkMovementMethod;
|
|
|
|
|
import android.view.Gravity;
|
|
|
|
|
import android.view.HapticFeedbackConstants;
|
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
@@ -72,6 +71,8 @@ public final class MainActivity extends Activity {
|
|
|
|
|
private static final int STATUS_BUSY = 1;
|
|
|
|
|
private static final int STATUS_SUCCESS = 2;
|
|
|
|
|
private static final int STATUS_ERROR = 3;
|
|
|
|
|
private static final int TERMINAL_VIEW_CHAT = 0;
|
|
|
|
|
private static final int TERMINAL_VIEW_FULL = 1;
|
|
|
|
|
private static final long TERMINAL_RENDER_INTERVAL_MS = 80L;
|
|
|
|
|
private static final long[] SOCKET_RECONNECT_DELAYS_MS = {1000L, 2000L, 4000L, 8000L, 15000L};
|
|
|
|
|
private static final int COLOR_APP_BG = Color.rgb(18, 20, 24);
|
|
|
|
@@ -127,11 +128,16 @@ public final class MainActivity extends Activity {
|
|
|
|
|
private TextView terminalMetaText;
|
|
|
|
|
private TextView terminalConnectionText;
|
|
|
|
|
private Button terminalReadingButton;
|
|
|
|
|
private Button terminalFullButton;
|
|
|
|
|
private ScrollView terminalScroll;
|
|
|
|
|
private LinearLayout terminalChatList;
|
|
|
|
|
private TextView terminalLiveStatusText;
|
|
|
|
|
private TextView terminalLiveOutputText;
|
|
|
|
|
private EditText inputField;
|
|
|
|
|
private final Button[] terminalAccessoryTabButtons = new Button[4];
|
|
|
|
|
private View terminalAccessoryBar;
|
|
|
|
|
private View terminalComposerBar;
|
|
|
|
|
private View terminalComposerTabs;
|
|
|
|
|
private LinearLayout terminalImagePreviewBar;
|
|
|
|
|
private ImageView terminalImagePreview;
|
|
|
|
|
private TextView terminalImagePreviewPath;
|
|
|
|
@@ -147,13 +153,14 @@ public final class MainActivity extends Activity {
|
|
|
|
|
private int lastActiveSessionCount = -1;
|
|
|
|
|
private TerminalScreenBuffer terminalScreen = new TerminalScreenBuffer(DEFAULT_TERMINAL_COLS, DEFAULT_TERMINAL_ROWS);
|
|
|
|
|
private final StringBuilder queuedTerminalInput = new StringBuilder();
|
|
|
|
|
private final Set<String> terminalExpandedBlocks = new HashSet<>();
|
|
|
|
|
private final Set<String> terminalExpandedMessages = new HashSet<>();
|
|
|
|
|
private final List<ConversationMessage> terminalConversationMessages = new ArrayList<>();
|
|
|
|
|
private boolean terminalConnected;
|
|
|
|
|
private boolean terminalConnecting;
|
|
|
|
|
private boolean terminalRenderPending;
|
|
|
|
|
private boolean terminalSelectionEnabled;
|
|
|
|
|
private boolean terminalFollowOutput = true;
|
|
|
|
|
private boolean terminalReadingMode = true;
|
|
|
|
|
private int terminalViewMode = TERMINAL_VIEW_CHAT;
|
|
|
|
|
private int terminalKeyPage;
|
|
|
|
|
private int terminalReconnectAttempt;
|
|
|
|
|
private int terminalConnectionGeneration;
|
|
|
|
@@ -1605,7 +1612,10 @@ public final class MainActivity extends Activity {
|
|
|
|
|
terminalSelectionEnabled = false;
|
|
|
|
|
terminalFollowOutput = true;
|
|
|
|
|
terminalKeyPage = 0;
|
|
|
|
|
terminalExpandedBlocks.clear();
|
|
|
|
|
terminalExpandedMessages.clear();
|
|
|
|
|
terminalConversationMessages.clear();
|
|
|
|
|
terminalLiveStatusText = null;
|
|
|
|
|
terminalLiveOutputText = null;
|
|
|
|
|
terminalImagePath = "";
|
|
|
|
|
terminalImagePreviewBar = null;
|
|
|
|
|
terminalImagePreview = null;
|
|
|
|
@@ -1631,16 +1641,17 @@ public final class MainActivity extends Activity {
|
|
|
|
|
terminalText.setTextSize(11);
|
|
|
|
|
terminalText.setTypeface(Typeface.MONOSPACE);
|
|
|
|
|
terminalText.setIncludeFontPadding(false);
|
|
|
|
|
terminalText.setHorizontallyScrolling(!terminalReadingMode);
|
|
|
|
|
terminalText.setHorizontallyScrolling(true);
|
|
|
|
|
terminalText.setLineSpacing(0, 1.05f);
|
|
|
|
|
terminalText.setGravity((terminalReadingMode ? Gravity.TOP : Gravity.BOTTOM) | Gravity.START);
|
|
|
|
|
terminalText.setGravity(Gravity.BOTTOM | Gravity.START);
|
|
|
|
|
terminalText.setTextIsSelectable(terminalSelectionEnabled);
|
|
|
|
|
terminalText.setPadding(dp(2), dp(8), dp(2), dp(8));
|
|
|
|
|
terminalText.setBackgroundColor(COLOR_TERMINAL_BG);
|
|
|
|
|
terminalScroll.addView(terminalText, new ScrollView.LayoutParams(
|
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT
|
|
|
|
|
));
|
|
|
|
|
terminalChatList = new LinearLayout(this);
|
|
|
|
|
terminalChatList.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
|
terminalChatList.setPadding(dp(10), dp(12), dp(10), dp(18));
|
|
|
|
|
terminalChatList.addView(projectStateText("Loading conversation..."), matchWrap());
|
|
|
|
|
showTerminalView(terminalViewMode, false);
|
|
|
|
|
terminalScroll.addOnLayoutChangeListener((view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) ->
|
|
|
|
|
resizeTerminalToViewport(false));
|
|
|
|
|
terminalScroll.setOnScrollChangeListener((view, scrollX, scrollY, oldScrollX, oldScrollY) ->
|
|
|
|
@@ -1651,6 +1662,7 @@ public final class MainActivity extends Activity {
|
|
|
|
|
1
|
|
|
|
|
));
|
|
|
|
|
terminalAccessoryBar = createAccessoryBar();
|
|
|
|
|
terminalAccessoryBar.setVisibility(terminalViewMode == TERMINAL_VIEW_CHAT ? View.GONE : View.VISIBLE);
|
|
|
|
|
root.addView(terminalAccessoryBar, new LinearLayout.LayoutParams(
|
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
|
|
dp(TERMINAL_KEYS_HEIGHT_DP)
|
|
|
|
@@ -1662,6 +1674,7 @@ public final class MainActivity extends Activity {
|
|
|
|
|
connectTerminal(sessionName);
|
|
|
|
|
refreshTerminalGroupSessions(sessionName);
|
|
|
|
|
refreshTerminalSessionMeta(sessionName);
|
|
|
|
|
refreshTerminalConversation(sessionName);
|
|
|
|
|
});
|
|
|
|
|
inputField.post(() -> {
|
|
|
|
|
inputField.requestFocus();
|
|
|
|
@@ -1752,36 +1765,67 @@ public final class MainActivity extends Activity {
|
|
|
|
|
dp(13)
|
|
|
|
|
));
|
|
|
|
|
bar.addView(titleBlock, new LinearLayout.LayoutParams(0, dp(32), 1));
|
|
|
|
|
terminalReadingButton = terminalToolButton("读", view ->
|
|
|
|
|
setTerminalReadingMode(!terminalReadingMode));
|
|
|
|
|
terminalReadingButton.setContentDescription("Toggle Chinese reading mode");
|
|
|
|
|
terminalReadingButton = terminalToolButton("聊", view -> showTerminalView(TERMINAL_VIEW_CHAT, true));
|
|
|
|
|
terminalReadingButton.setContentDescription("Chat view");
|
|
|
|
|
terminalFullButton = terminalToolButton("终", view -> showTerminalView(TERMINAL_VIEW_FULL, true));
|
|
|
|
|
terminalFullButton.setContentDescription("Full terminal view");
|
|
|
|
|
styleTerminalReadingButton();
|
|
|
|
|
bar.addView(terminalReadingButton);
|
|
|
|
|
bar.addView(terminalFullButton);
|
|
|
|
|
bar.addView(terminalToolButton("☰", view -> showTerminalActions(sessionName)));
|
|
|
|
|
return bar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setTerminalReadingMode(boolean reading) {
|
|
|
|
|
terminalReadingMode = reading;
|
|
|
|
|
if (terminalText != null) {
|
|
|
|
|
terminalText.setHorizontallyScrolling(!reading);
|
|
|
|
|
terminalText.setGravity((reading ? Gravity.TOP : Gravity.BOTTOM) | Gravity.START);
|
|
|
|
|
terminalText.setMovementMethod(reading ? LinkMovementMethod.getInstance() : null);
|
|
|
|
|
private void showTerminalView(int mode, boolean announce) {
|
|
|
|
|
terminalViewMode = mode;
|
|
|
|
|
if (terminalScroll == null || terminalText == null || terminalChatList == null) {
|
|
|
|
|
styleTerminalReadingButton();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
terminalScroll.removeAllViews();
|
|
|
|
|
if (mode == TERMINAL_VIEW_CHAT) {
|
|
|
|
|
terminalScroll.addView(terminalChatList, new ScrollView.LayoutParams(
|
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT
|
|
|
|
|
));
|
|
|
|
|
renderTerminalConversation();
|
|
|
|
|
} else {
|
|
|
|
|
terminalText.setHorizontallyScrolling(true);
|
|
|
|
|
terminalText.setGravity(Gravity.BOTTOM | Gravity.START);
|
|
|
|
|
terminalText.setMovementMethod(null);
|
|
|
|
|
terminalScroll.addView(terminalText, new ScrollView.LayoutParams(
|
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT
|
|
|
|
|
));
|
|
|
|
|
renderTerminalNow();
|
|
|
|
|
}
|
|
|
|
|
styleTerminalReadingButton();
|
|
|
|
|
renderTerminalNow();
|
|
|
|
|
setStatus(reading ? "Chinese reading mode" : "Full terminal mode");
|
|
|
|
|
boolean chat = mode == TERMINAL_VIEW_CHAT;
|
|
|
|
|
if (terminalAccessoryBar != null) {
|
|
|
|
|
terminalAccessoryBar.setVisibility(chat ? View.GONE : View.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
if (terminalComposerTabs != null) {
|
|
|
|
|
terminalComposerTabs.setVisibility(chat ? View.GONE : View.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
if (inputField != null) {
|
|
|
|
|
inputField.setHint(chat ? "Message" : "type command or text");
|
|
|
|
|
}
|
|
|
|
|
if (announce) {
|
|
|
|
|
setStatus(mode == TERMINAL_VIEW_CHAT ? "Chat view" : "Full terminal mode");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void styleTerminalReadingButton() {
|
|
|
|
|
if (terminalReadingButton == null) {
|
|
|
|
|
if (terminalReadingButton == null || terminalFullButton == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
terminalReadingButton.setText(terminalReadingMode ? "读" : "终");
|
|
|
|
|
terminalReadingButton.setTextColor(terminalReadingMode ? Color.rgb(14, 38, 24) : COLOR_TEXT_MUTED);
|
|
|
|
|
terminalReadingButton.setBackground(terminalReadingMode
|
|
|
|
|
? rounded(COLOR_ACCENT, 7, COLOR_ACCENT, 1)
|
|
|
|
|
: buttonBackground());
|
|
|
|
|
styleTerminalViewButton(terminalReadingButton, terminalViewMode == TERMINAL_VIEW_CHAT);
|
|
|
|
|
styleTerminalViewButton(terminalFullButton, terminalViewMode == TERMINAL_VIEW_FULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void styleTerminalViewButton(Button button, boolean selected) {
|
|
|
|
|
button.setTextColor(selected ? Color.rgb(14, 38, 24) : COLOR_TEXT_MUTED);
|
|
|
|
|
button.setBackground(selected ? rounded(COLOR_ACCENT, 7, COLOR_ACCENT, 1) : buttonBackground());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HorizontalScrollView createTerminalGroupBar() {
|
|
|
|
@@ -1848,6 +1892,242 @@ public final class MainActivity extends Activity {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void refreshTerminalConversation(String sessionName) {
|
|
|
|
|
executor.execute(() -> {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject rootObject = new JSONObject(api.timeline(200));
|
|
|
|
|
JSONArray events = rootObject.optJSONArray("events");
|
|
|
|
|
List<ConversationMessage> loaded = new ArrayList<>();
|
|
|
|
|
for (int index = 0; events != null && index < events.length(); index++) {
|
|
|
|
|
JSONObject event = events.optJSONObject(index);
|
|
|
|
|
if (event != null
|
|
|
|
|
&& "conversation-message".equals(event.optString("type"))
|
|
|
|
|
&& sessionName.equals(event.optString("sessionName"))) {
|
|
|
|
|
loaded.add(ConversationMessage.fromJson(event));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Collections.sort(loaded, (left, right) -> left.createdAt.compareTo(right.createdAt));
|
|
|
|
|
runOnUiThread(() -> {
|
|
|
|
|
if (!sessionName.equals(activeSessionName)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
terminalConversationMessages.clear();
|
|
|
|
|
terminalConversationMessages.addAll(loaded);
|
|
|
|
|
renderTerminalConversation();
|
|
|
|
|
});
|
|
|
|
|
} catch (Exception error) {
|
|
|
|
|
runOnUiThread(() -> {
|
|
|
|
|
if (sessionName.equals(activeSessionName) && terminalChatList != null) {
|
|
|
|
|
terminalChatList.removeAllViews();
|
|
|
|
|
terminalChatList.addView(projectStateText(
|
|
|
|
|
"Conversation unavailable\n" + error.getMessage()), matchWrap());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void upsertConversationMessage(ConversationMessage message) {
|
|
|
|
|
if (message.sessionName.isEmpty() || !message.sessionName.equals(activeSessionName)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!message.local && "user".equals(message.role)) {
|
|
|
|
|
for (int index = terminalConversationMessages.size() - 1; index >= 0; index--) {
|
|
|
|
|
ConversationMessage existing = terminalConversationMessages.get(index);
|
|
|
|
|
if (existing.local && existing.content.equals(message.content)) {
|
|
|
|
|
terminalConversationMessages.remove(index);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (int index = 0; index < terminalConversationMessages.size(); index++) {
|
|
|
|
|
if (terminalConversationMessages.get(index).messageId.equals(message.messageId)) {
|
|
|
|
|
terminalConversationMessages.set(index, message);
|
|
|
|
|
renderTerminalConversation();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
terminalConversationMessages.add(message);
|
|
|
|
|
Collections.sort(terminalConversationMessages,
|
|
|
|
|
(left, right) -> left.createdAt.compareTo(right.createdAt));
|
|
|
|
|
renderTerminalConversation();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void renderTerminalConversation() {
|
|
|
|
|
if (terminalChatList == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
terminalChatList.removeAllViews();
|
|
|
|
|
if (terminalConversationMessages.isEmpty()) {
|
|
|
|
|
TextView empty = bodyText("No structured messages yet");
|
|
|
|
|
empty.setGravity(Gravity.CENTER);
|
|
|
|
|
empty.setPadding(dp(12), dp(24), dp(12), dp(24));
|
|
|
|
|
terminalChatList.addView(empty, matchWrap());
|
|
|
|
|
} else {
|
|
|
|
|
for (ConversationMessage message : terminalConversationMessages) {
|
|
|
|
|
terminalChatList.addView(conversationMessageRow(message));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
terminalChatList.addView(conversationLiveTerminalPanel(), matchWrap());
|
|
|
|
|
updateTerminalLivePanel();
|
|
|
|
|
if (terminalViewMode == TERMINAL_VIEW_CHAT && terminalFollowOutput && terminalScroll != null) {
|
|
|
|
|
terminalScroll.post(() -> terminalScroll.fullScroll(View.FOCUS_DOWN));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private View conversationMessageRow(ConversationMessage message) {
|
|
|
|
|
boolean user = "user".equals(message.role);
|
|
|
|
|
boolean tool = message.isTool();
|
|
|
|
|
LinearLayout row = new LinearLayout(this);
|
|
|
|
|
row.setOrientation(LinearLayout.HORIZONTAL);
|
|
|
|
|
row.setGravity(user ? Gravity.END : Gravity.START);
|
|
|
|
|
|
|
|
|
|
LinearLayout bubble = new LinearLayout(this);
|
|
|
|
|
bubble.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
|
bubble.setPadding(user || tool ? dp(12) : dp(2), dp(9), user || tool ? dp(12) : dp(4), dp(10));
|
|
|
|
|
bubble.setBackground(tool
|
|
|
|
|
? rounded(COLOR_PANEL, 8, COLOR_BORDER, 1)
|
|
|
|
|
: user
|
|
|
|
|
? rounded(COLOR_ACCENT_DARK, 8, COLOR_ACCENT, 1)
|
|
|
|
|
: rounded(Color.TRANSPARENT, 0, Color.TRANSPARENT, 0));
|
|
|
|
|
|
|
|
|
|
TextView meta = new TextView(this);
|
|
|
|
|
meta.setTextColor(user ? COLOR_ACCENT : tool ? COLOR_ACCENT_WARM : COLOR_TEXT_MUTED);
|
|
|
|
|
meta.setTextSize(9);
|
|
|
|
|
meta.setTypeface(Typeface.MONOSPACE, Typeface.BOLD);
|
|
|
|
|
meta.setSingleLine(true);
|
|
|
|
|
meta.setEllipsize(TextUtils.TruncateAt.END);
|
|
|
|
|
meta.setText(tool ? conversationToolTitle(message) : conversationStatusPart(message).replaceFirst("^ · ", ""));
|
|
|
|
|
if (tool || (!"complete".equals(message.status) && !message.status.isEmpty())) {
|
|
|
|
|
bubble.addView(meta, matchWrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean expanded = terminalExpandedMessages.contains(message.messageId);
|
|
|
|
|
if (!tool || expanded) {
|
|
|
|
|
bubble.addView(conversationContent(message, tool), matchWrap());
|
|
|
|
|
} else {
|
|
|
|
|
TextView collapsed = bodyText("Tap to show " + message.content.length() + " characters");
|
|
|
|
|
collapsed.setTextSize(10);
|
|
|
|
|
collapsed.setPadding(0, dp(5), 0, 0);
|
|
|
|
|
bubble.addView(collapsed, matchWrap());
|
|
|
|
|
}
|
|
|
|
|
if (tool) {
|
|
|
|
|
bubble.setOnClickListener(view -> {
|
|
|
|
|
if (!terminalExpandedMessages.add(message.messageId)) {
|
|
|
|
|
terminalExpandedMessages.remove(message.messageId);
|
|
|
|
|
}
|
|
|
|
|
renderTerminalConversation();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LinearLayout.LayoutParams bubbleParams = new LinearLayout.LayoutParams(
|
|
|
|
|
0,
|
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
|
|
|
tool ? 1f : user ? 0.82f : 0.94f
|
|
|
|
|
);
|
|
|
|
|
if (!tool) {
|
|
|
|
|
View spacer = new View(this);
|
|
|
|
|
LinearLayout.LayoutParams spacerParams = new LinearLayout.LayoutParams(
|
|
|
|
|
0,
|
|
|
|
|
1,
|
|
|
|
|
user ? 0.18f : 0.06f
|
|
|
|
|
);
|
|
|
|
|
if (user) {
|
|
|
|
|
row.addView(spacer, spacerParams);
|
|
|
|
|
row.addView(bubble, bubbleParams);
|
|
|
|
|
} else {
|
|
|
|
|
row.addView(bubble, bubbleParams);
|
|
|
|
|
row.addView(spacer, spacerParams);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
row.addView(bubble, bubbleParams);
|
|
|
|
|
}
|
|
|
|
|
LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams(
|
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT
|
|
|
|
|
);
|
|
|
|
|
rowParams.bottomMargin = dp(9);
|
|
|
|
|
row.setLayoutParams(rowParams);
|
|
|
|
|
return row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TextView conversationContent(ConversationMessage message, boolean tool) {
|
|
|
|
|
TextView content = new TextView(this);
|
|
|
|
|
content.setText(message.content.isEmpty() ? "(empty)" : message.content);
|
|
|
|
|
content.setTextColor(COLOR_TEXT);
|
|
|
|
|
content.setTextSize(tool ? 11 : 14);
|
|
|
|
|
content.setTypeface(tool ? Typeface.MONOSPACE : Typeface.DEFAULT);
|
|
|
|
|
content.setTextIsSelectable(true);
|
|
|
|
|
content.setLineSpacing(dp(2), 1f);
|
|
|
|
|
content.setPadding(0, dp(6), 0, 0);
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String conversationToolTitle(ConversationMessage message) {
|
|
|
|
|
String name = defaultValue(message.toolName, message.contentType);
|
|
|
|
|
return defaultValue(name, "Tool output") + conversationStatusPart(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String conversationStatusPart(ConversationMessage message) {
|
|
|
|
|
String status = message.status.trim();
|
|
|
|
|
return status.isEmpty() ? "" : " · " + status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private View conversationLiveTerminalPanel() {
|
|
|
|
|
LinearLayout panel = new LinearLayout(this);
|
|
|
|
|
panel.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
|
panel.setPadding(dp(12), dp(9), dp(12), dp(10));
|
|
|
|
|
panel.setBackground(rounded(COLOR_FIELD, 8, COLOR_BORDER, 1));
|
|
|
|
|
panel.setOnClickListener(view -> showTerminalView(TERMINAL_VIEW_FULL, true));
|
|
|
|
|
|
|
|
|
|
LinearLayout heading = new LinearLayout(this);
|
|
|
|
|
heading.setOrientation(LinearLayout.HORIZONTAL);
|
|
|
|
|
heading.setGravity(Gravity.CENTER_VERTICAL);
|
|
|
|
|
TextView title = new TextView(this);
|
|
|
|
|
title.setText("Live terminal");
|
|
|
|
|
title.setTextColor(COLOR_TEXT);
|
|
|
|
|
title.setTextSize(11);
|
|
|
|
|
title.setTypeface(Typeface.DEFAULT_BOLD);
|
|
|
|
|
heading.addView(title, new LinearLayout.LayoutParams(0, dp(22), 1));
|
|
|
|
|
terminalLiveStatusText = new TextView(this);
|
|
|
|
|
terminalLiveStatusText.setTextSize(9);
|
|
|
|
|
terminalLiveStatusText.setTypeface(Typeface.MONOSPACE);
|
|
|
|
|
terminalLiveStatusText.setSingleLine(true);
|
|
|
|
|
heading.addView(terminalLiveStatusText, new LinearLayout.LayoutParams(
|
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
|
|
|
dp(22)
|
|
|
|
|
));
|
|
|
|
|
panel.addView(heading, matchWrap());
|
|
|
|
|
|
|
|
|
|
terminalLiveOutputText = new TextView(this);
|
|
|
|
|
terminalLiveOutputText.setTextColor(COLOR_TEXT_MUTED);
|
|
|
|
|
terminalLiveOutputText.setTextSize(10);
|
|
|
|
|
terminalLiveOutputText.setTypeface(Typeface.MONOSPACE);
|
|
|
|
|
terminalLiveOutputText.setMaxLines(6);
|
|
|
|
|
terminalLiveOutputText.setEllipsize(TextUtils.TruncateAt.END);
|
|
|
|
|
terminalLiveOutputText.setPadding(0, dp(5), 0, 0);
|
|
|
|
|
panel.addView(terminalLiveOutputText, matchWrap());
|
|
|
|
|
|
|
|
|
|
TextView hint = bodyText("Tap for full terminal");
|
|
|
|
|
hint.setTextSize(9);
|
|
|
|
|
hint.setTextColor(COLOR_TEXT_DIM);
|
|
|
|
|
hint.setPadding(0, dp(6), 0, 0);
|
|
|
|
|
panel.addView(hint, matchWrap());
|
|
|
|
|
return panel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateTerminalLivePanel() {
|
|
|
|
|
if (terminalLiveStatusText == null || terminalLiveOutputText == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String socket = terminalSocketStatus.toLowerCase(java.util.Locale.ROOT);
|
|
|
|
|
boolean connected = socket.contains("connected") && !socket.contains("disconnected");
|
|
|
|
|
terminalLiveStatusText.setText(connected ? "● connected" : "● " + defaultValue(socket, "connecting"));
|
|
|
|
|
terminalLiveStatusText.setTextColor(connected ? COLOR_SUCCESS
|
|
|
|
|
: socket.contains("error") || socket.contains("disconnected") ? COLOR_DANGER : COLOR_ACCENT_WARM);
|
|
|
|
|
terminalLiveOutputText.setText(terminalScreen.renderTail(6));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateTerminalMeta() {
|
|
|
|
|
if (terminalMetaText == null || terminalConnectionText == null) {
|
|
|
|
|
return;
|
|
|
|
@@ -1864,6 +2144,7 @@ public final class MainActivity extends Activity {
|
|
|
|
|
terminalConnectionText.setText("● connecting");
|
|
|
|
|
terminalConnectionText.setTextColor(COLOR_ACCENT_WARM);
|
|
|
|
|
}
|
|
|
|
|
updateTerminalLivePanel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String compactTerminalPath(String path) {
|
|
|
|
@@ -2173,6 +2454,8 @@ public final class MainActivity extends Activity {
|
|
|
|
|
|
|
|
|
|
LinearLayout tabGrid = new LinearLayout(this);
|
|
|
|
|
tabGrid.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
|
terminalComposerTabs = tabGrid;
|
|
|
|
|
tabGrid.setVisibility(terminalViewMode == TERMINAL_VIEW_CHAT ? View.GONE : View.VISIBLE);
|
|
|
|
|
LinearLayout firstTabRow = terminalKeyRow();
|
|
|
|
|
LinearLayout secondTabRow = terminalKeyRow();
|
|
|
|
|
addAccessoryTab(firstTabRow, "Edit", 0);
|
|
|
|
@@ -2199,7 +2482,7 @@ public final class MainActivity extends Activity {
|
|
|
|
|
inputField = new EditText(this);
|
|
|
|
|
inputField.setTextColor(COLOR_TEXT);
|
|
|
|
|
inputField.setHintTextColor(COLOR_TEXT_DIM);
|
|
|
|
|
inputField.setHint("type command or text");
|
|
|
|
|
inputField.setHint(terminalViewMode == TERMINAL_VIEW_CHAT ? "Message" : "type command or text");
|
|
|
|
|
inputField.setSingleLine(false);
|
|
|
|
|
inputField.setMinLines(3);
|
|
|
|
|
inputField.setMaxLines(5);
|
|
|
|
@@ -3143,6 +3426,9 @@ public final class MainActivity extends Activity {
|
|
|
|
|
normalized = normalized + TERMINAL_ENTER;
|
|
|
|
|
}
|
|
|
|
|
terminalFollowOutput = true;
|
|
|
|
|
if (activeSessionName != null) {
|
|
|
|
|
upsertConversationMessage(ConversationMessage.localUser(activeSessionName, text));
|
|
|
|
|
}
|
|
|
|
|
sendTerminalInput(normalized);
|
|
|
|
|
inputField.setText("");
|
|
|
|
|
setStatus("Sent " + text.length() + " chars");
|
|
|
|
@@ -3310,7 +3596,7 @@ public final class MainActivity extends Activity {
|
|
|
|
|
terminalRenderPending = true;
|
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
|
long delay = Math.max(0L, TERMINAL_RENDER_INTERVAL_MS - (now - lastTerminalRenderMs));
|
|
|
|
|
terminalText.postDelayed(() -> {
|
|
|
|
|
mainHandler.postDelayed(() -> {
|
|
|
|
|
terminalRenderPending = false;
|
|
|
|
|
renderTerminalNow();
|
|
|
|
|
}, delay);
|
|
|
|
@@ -3321,20 +3607,10 @@ public final class MainActivity extends Activity {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
lastTerminalRenderMs = System.currentTimeMillis();
|
|
|
|
|
if (terminalReadingMode) {
|
|
|
|
|
terminalText.setMovementMethod(LinkMovementMethod.getInstance());
|
|
|
|
|
terminalText.setHighlightColor(Color.TRANSPARENT);
|
|
|
|
|
terminalText.setText(terminalScreen.renderFocused(terminalExpandedBlocks, key -> {
|
|
|
|
|
if (!terminalExpandedBlocks.add(key)) {
|
|
|
|
|
terminalExpandedBlocks.remove(key);
|
|
|
|
|
}
|
|
|
|
|
renderTerminalNow();
|
|
|
|
|
}));
|
|
|
|
|
} else {
|
|
|
|
|
terminalText.setMovementMethod(null);
|
|
|
|
|
terminalText.setText(terminalScreen.render());
|
|
|
|
|
}
|
|
|
|
|
if (terminalFollowOutput) {
|
|
|
|
|
terminalText.setMovementMethod(null);
|
|
|
|
|
terminalText.setText(terminalScreen.render());
|
|
|
|
|
updateTerminalLivePanel();
|
|
|
|
|
if (terminalViewMode != TERMINAL_VIEW_CHAT && terminalFollowOutput) {
|
|
|
|
|
terminalScroll.post(() -> terminalScroll.fullScroll(View.FOCUS_DOWN));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -3870,6 +4146,12 @@ public final class MainActivity extends Activity {
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ("conversation-message".equals(type)) {
|
|
|
|
|
upsertConversationMessage(ConversationMessage.fromJson(event));
|
|
|
|
|
terminalEventStatus = "message received";
|
|
|
|
|
updateTerminalMeta();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ("hook-event".equals(type)) {
|
|
|
|
|
terminalEventStatus = "hook event";
|
|
|
|
|
updateTerminalMeta();
|
|
|
|
|