Files
hapi/ios/Hapi/Features/Chat/ConciseView.swift
T
99f59fc49c feat(ios): add a concise conversation view
Port the H5 concise lens: keep user prompts, each round's final answer,
and anything needing the user (pending approvals/questions, plan
proposals, user-initiated shell commands, terminal errors); drop tool
groups, reasoning, progress narration, and transient events.

The choice persists across chats (UserDefaults), appears as a composer
status chip, and toggles from the chat menu. The transcript renders the
filtered rows while the canonical block list stays intact for outline,
history points, pending-request row lookup, and jumps; a content-search
jump temporarily returns to the full view so the matched narration is
visible.

Adds pure projection tests for answer selection, attention tools,
subagent permissions, and event filtering.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-12-01 07:29:05 +08:00

126 lines
4.4 KiB
Swift

import HapiProtocol
/// Web `filterConciseChatBlocks` port: keep what the user said and what the
/// agent finally answered; drop the tool/reasoning/progress work in between.
/// Anything that needs the user — pending approvals, questions, plan
/// proposals, terminal errors — stays visible.
enum ConciseChatView {
private static let planProposalTools: Set<String> = [
"exitplanmode", "cursorcreateplan",
]
static func filter(_ blocks: [VisibleChatBlock]) -> [VisibleChatBlock] {
var output: [VisibleChatBlock] = []
var round: [VisibleChatBlock] = []
for visible in blocks {
if isUserBoundary(visible) {
filterRound(round, into: &output)
round = []
output.append(visible)
} else {
round.append(visible)
}
}
filterRound(round, into: &output)
return output
}
private static func isUserBoundary(_ visible: VisibleChatBlock) -> Bool {
guard case .block(let block) = visible else { return false }
switch block {
case .userText:
return true
case .cliOutput(let output):
return output.source == .user
default:
return false
}
}
/// Agent text after the round's last tool/reasoning is the reply; text
/// before it narrates work in progress and is dropped.
private static func filterRound(
_ round: [VisibleChatBlock],
into output: inout [VisibleChatBlock]
) {
var lastWorkIndex = -1
for index in round.indices.reversed() {
switch round[index] {
case .toolGroup:
lastWorkIndex = index
case .block(.toolCall), .block(.agentReasoning):
lastWorkIndex = index
default:
continue
}
break
}
for (index, visible) in round.enumerated() {
switch visible {
case .toolGroup:
continue
case .block(.agentText):
if index > lastWorkIndex { output.append(visible) }
case .block(.agentReasoning):
continue
case .block(.toolCall(let tool)):
if isVisibleTool(tool) { output.append(visible) }
case .block(.agentEvent(let event)):
if isVisibleEvent(event.event) { output.append(visible) }
default:
output.append(visible)
}
}
}
private static func isVisibleTool(_ block: ToolCallBlock) -> Bool {
if isAskUserQuestionToolName(block.tool.name)
|| isRequestUserInputToolName(block.tool.name)
|| block.tool.name == "CodexPermission"
|| hasPendingPermission(block) {
return true
}
if planProposalTools.contains(normalizeToolName(block.tool.name)) {
return true
}
// User-initiated Codex shell commands are user input, not agent work.
let input = block.tool.input?.chatObject
let source = input?["command_source"]?.chatString
?? input?["commandSource"]?.chatString
return block.tool.name == "CodexBash"
&& source?.lowercased() == "usershell"
}
private static func hasPendingPermission(_ block: ToolCallBlock) -> Bool {
if block.tool.permission?.status == .pending { return true }
return block.children.contains { child in
guard let tool = child.asToolCall else { return false }
return hasPendingPermission(tool)
}
}
private static func isVisibleEvent(_ event: AgentEvent) -> Bool {
switch event.kind {
case .error, .message, .limitReached, .recap,
.threadGoalUpdated, .threadGoalCleared:
return true
case .apiError:
// Only the terminal failure matters; retries are transient noise.
guard let attempt = event["retryAttempt"]?.chatNumber,
let maxRetries = event["maxRetries"]?.chatNumber,
maxRetries > 0 else { return false }
return attempt >= maxRetries
default:
return false
}
}
private static func normalizeToolName(_ value: String) -> String {
value
.trimmingCharacters(in: .whitespacesAndNewlines)
.lowercased()
.filter { $0.isLetter || $0.isNumber }
}
}