mirror of
https://github.com/wu736139669/hapi.git
synced 2026-12-04 06:57:55 +00:00
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>
139 lines
5.1 KiB
Swift
139 lines
5.1 KiB
Swift
import XCTest
|
|
@testable import Hapi
|
|
@testable import HapiProtocol
|
|
|
|
final class ConciseViewTests: XCTestCase {
|
|
private func user(_ id: String) -> VisibleChatBlock {
|
|
.block(.userText(UserTextBlock(
|
|
id: id, localId: nil, createdAt: 0, invokedAt: 1, text: id,
|
|
attachments: nil, status: nil, originalText: nil, meta: nil
|
|
)))
|
|
}
|
|
|
|
private func agentText(_ id: String) -> VisibleChatBlock {
|
|
.block(.agentText(AgentTextBlock(
|
|
id: id, localId: nil, createdAt: 0, invokedAt: nil, durationMs: nil,
|
|
usage: nil, model: nil, text: id, meta: nil
|
|
)))
|
|
}
|
|
|
|
private func reasoning(_ id: String) -> VisibleChatBlock {
|
|
.block(.agentReasoning(AgentReasoningBlock(
|
|
id: id, localId: nil, createdAt: 0, invokedAt: nil, durationMs: nil,
|
|
usage: nil, model: nil, text: id, meta: nil
|
|
)))
|
|
}
|
|
|
|
private func tool(
|
|
_ id: String,
|
|
name: String,
|
|
input: JSONValue? = nil,
|
|
permission: ToolPermission? = nil,
|
|
children: [ChatBlock] = []
|
|
) -> VisibleChatBlock {
|
|
.block(.toolCall(ToolCallBlock(
|
|
id: id, localId: nil, createdAt: 0, invokedAt: nil, durationMs: nil,
|
|
usage: nil, model: nil,
|
|
tool: ChatToolCall(
|
|
id: id, name: name, state: .completed, input: input,
|
|
createdAt: 0, permission: permission
|
|
),
|
|
children: children, meta: nil
|
|
)))
|
|
}
|
|
|
|
private func event(_ id: String, type: String, extra: [String: JSONValue] = [:]) -> VisibleChatBlock {
|
|
var object = extra
|
|
object["type"] = .string(type)
|
|
return .block(.agentEvent(AgentEventBlock(
|
|
id: id, createdAt: 0, invokedAt: nil, model: nil,
|
|
event: AgentEvent(object: object), meta: nil
|
|
)))
|
|
}
|
|
|
|
private func group(_ id: String) -> VisibleChatBlock {
|
|
.toolGroup(ToolGroupBlock(
|
|
id: id, createdAt: 0, invokedAt: nil,
|
|
firstToolId: "t1", lastToolId: "t2", tools: [],
|
|
defaultOpen: false, historyState: .complete, needsOlderHistory: false,
|
|
activityTitle: nil, presentationMode: .default,
|
|
summary: ToolGroupSummary(
|
|
totalTools: 2, countsByKind: [:], fileTargets: [], commandTargets: [],
|
|
searchTargets: [], urlTargets: [], otherTargets: [], errorCount: 0,
|
|
runningCount: 0, pendingCount: 0
|
|
)
|
|
))
|
|
}
|
|
|
|
private func ids(_ blocks: [VisibleChatBlock]) -> [String] {
|
|
blocks.map { visible in
|
|
switch visible {
|
|
case .block(let block): return block.id
|
|
case .toolGroup(let group): return group.id
|
|
}
|
|
}
|
|
}
|
|
|
|
func testKeepsPromptsAndFinalAnswersOnly() {
|
|
let blocks = [
|
|
user("u1"),
|
|
agentText("narration"),
|
|
reasoning("think"),
|
|
tool("read", name: "Read"),
|
|
agentText("final-1"),
|
|
user("u2"),
|
|
group("group"),
|
|
agentText("final-2"),
|
|
]
|
|
XCTAssertEqual(ids(ConciseChatView.filter(blocks)), ["u1", "final-1", "u2", "final-2"])
|
|
}
|
|
|
|
func testAttentionPlanAndUserShellToolsStayVisible() {
|
|
let blocks = [
|
|
user("u1"),
|
|
tool("pending", name: "Bash", permission: ToolPermission(id: "p", status: .pending)),
|
|
tool("ask", name: "AskUserQuestion"),
|
|
tool("question", name: "request_user_input"),
|
|
tool("plan", name: "ExitPlanMode"),
|
|
tool("shell", name: "CodexBash", input: .object(["command_source": .string("usershell")])),
|
|
tool("read", name: "Read"),
|
|
agentText("final"),
|
|
]
|
|
XCTAssertEqual(
|
|
ids(ConciseChatView.filter(blocks)),
|
|
["u1", "pending", "ask", "question", "plan", "shell", "final"]
|
|
)
|
|
}
|
|
|
|
func testSubagentPendingPermissionKeepsTheParentTool() {
|
|
let child = ChatBlock.toolCall(ToolCallBlock(
|
|
id: "child", localId: nil, createdAt: 0, invokedAt: nil, durationMs: nil,
|
|
usage: nil, model: nil,
|
|
tool: ChatToolCall(
|
|
id: "child", name: "Bash", state: .running,
|
|
createdAt: 0, permission: ToolPermission(id: "p", status: .pending)
|
|
),
|
|
children: [], meta: nil
|
|
))
|
|
let blocks = [user("u"), tool("task", name: "Task", children: [child]), agentText("final")]
|
|
XCTAssertEqual(ids(ConciseChatView.filter(blocks)), ["u", "task", "final"])
|
|
}
|
|
|
|
func testTerminalEventsSurviveAndProgressEventsDoNot() {
|
|
let blocks = [
|
|
user("u"),
|
|
event("token", type: "token-count"),
|
|
event("error", type: "error"),
|
|
event("recap", type: "recap"),
|
|
event("retry", type: "api-error", extra: [
|
|
"retryAttempt": .number(2), "maxRetries": .number(5),
|
|
]),
|
|
event("terminal", type: "api-error", extra: [
|
|
"retryAttempt": .number(5), "maxRetries": .number(5),
|
|
]),
|
|
agentText("final"),
|
|
]
|
|
XCTAssertEqual(ids(ConciseChatView.filter(blocks)), ["u", "error", "recap", "terminal", "final"])
|
|
}
|
|
}
|