Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/Vorssaint/Core/NotchClaudeSessionStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct NotchClaudeSessionStrings {
let renamePlaceholder: String
let conversation: String
let noMessages: String
let answer: String
let showAllFormat: String
let showLess: String
let shrink: String
Expand Down Expand Up @@ -136,6 +137,7 @@ extension NotchClaudeSessionStrings {
renamePlaceholder: "Name this session",
conversation: "Conversation",
noMessages: "Nothing said yet.",
answer: "Summary",
showAllFormat: "Show all %d",
showLess: "Show less",
shrink: "Shrink",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ struct ClaudeReader: AgentHarnessReader {
// tool output can be large: no need to decode it to know that.
if AgentLogParser.contains(line, #""isSidechain":true"#) { return [] }
// Claude Code compacts an idle session on its own, writing the summary
// as a user line that no reply follows; it begins no work.
// as a user line that no reply follows; it begins no work. Nor does a
// slash command, like /recap, whose output now goes to a system line:
// one that asks the model, like a skill, begins with the reply.
guard let json = AgentLogParser.object(line), json["type"] as? String == "user",
json["isMeta"] as? Bool != true, json["isSidechain"] as? Bool != true,
json["isCompactSummary"] as? Bool != true else { return [] }
json["isCompactSummary"] as? Bool != true, !isCommand(json) else { return [] }
adopt(json, into: &state)
state.turnOpen = true
state.runningCommands = []
Expand Down Expand Up @@ -115,6 +117,14 @@ struct ClaudeReader: AgentHarnessReader {
return entries
}

/// A slash command the person typed, which is not a prompt by itself.
private func isCommand(_ json: [String: Any]) -> Bool {
let content = (json["message"] as? [String: Any])?["content"]
let text = (content as? String)
?? (content as? [[String: Any]])?.first { $0["type"] as? String == "text" }?["text"] as? String ?? ""
return text.trimmingCharacters(in: .whitespacesAndNewlines).hasPrefix("<command-name>")
}

/// The interruption or local command output the person's message holds,
/// never text inside a tool result.
private func endsTurn(_ json: [String: Any]) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ final class ClaudeSessionEngine {
struct Activity {
var turnOpen = false
var turnStarted: Date?
/// The open turn is known only from the registry saying busy, with
/// nothing typed, said or run since; and when the turn before began.
var registryOnly = false
var startedBefore: Date?
var turnEnded: Date?
var completed = true
/// The newest moment a hook or transcript line told of.
Expand Down Expand Up @@ -96,14 +100,26 @@ final class ClaudeSessionEngine {
// ended even when its last lines were not read yet.
if var entry = activity[id], entry.turnOpen, record.status == .idle || record.status == .shell,
let changed = record.statusUpdated, changed > entry.lastFact ?? .distantPast {
end(at: changed, completed: true, in: &entry)
if entry.registryOnly {
// Busy a moment with nothing typed, said or run, as while
// Claude Code writes a recap of the session: no turn ran,
// and nothing finished.
entry.turnOpen = false
entry.turnStarted = entry.startedBefore
entry.registryOnly = false
} else {
end(at: changed, completed: true, in: &entry)
}
activity[id] = entry
}
// And it went to work before its prompt was read.
if record.status == .busy, let changed = record.statusUpdated {
var entry = activity[id] ?? Activity(firstSeen: now)
if !entry.turnOpen, changed > entry.lastFact ?? .distantPast {
let before = entry.turnStarted
begin(at: changed, in: &entry)
entry.registryOnly = true
entry.startedBefore = before
activity[id] = entry
}
}
Expand Down Expand Up @@ -188,7 +204,10 @@ final class ClaudeSessionEngine {
events.append(.needsPermission(id: id, tool: entry.permissions.first?.tool ?? entry.openTool?.name ?? ""))
case .waitingInput:
events.append(.needsInput(id: id))
case .finished where previous.phase.isActive || previous.phase.isWaiting:
// Only a turn that ended after the work was seen begin is news;
// going back to a result already shown, as after a recap, is not.
case .finished where (previous.phase.isActive || previous.phase.isWaiting)
&& (entry.turnEnded ?? .distantPast) >= previous.since:
let duration = entry.turnStarted.flatMap { start in entry.turnEnded.map { $0.timeIntervalSince(start) } }
events.append(.finished(id: id, duration: duration))
default: break
Expand Down Expand Up @@ -279,9 +298,9 @@ final class ClaudeSessionEngine {
case .turnEnded(let date, let completed): end(at: date, completed: completed, in: &entry)
case .toolStarted(let tool): start(tool, in: &entry)
case .toolFinished(let id, let date, let failed): finish(id, at: date, failed: failed, in: &entry)
case .text(let text, let date):
case .text(let text, let date, let endsTurn):
if policy.previews { entry.lastText = text }
say(text, as: .assistant, at: date, in: &entry)
say(text, as: .assistant, at: date, endsTurn: endsTurn, in: &entry)
touch(date, in: &entry)
case .prompt(let text, let date):
say(text, as: .user, at: date, in: &entry)
Expand Down Expand Up @@ -345,11 +364,15 @@ final class ClaudeSessionEngine {
/// The conversation keeps its latest messages.
static let chatLength = 150

private func say(_ text: String, as role: ClaudeChatMessage.Role, at date: Date, in entry: inout Activity) {
private func say(_ text: String, as role: ClaudeChatMessage.Role, at date: Date, endsTurn: Bool = false,
in entry: inout Activity) {
guard policy.previews else { return }
if let last = entry.chat.last, last.role == role, last.text == text { return }
if let last = entry.chat.last, last.role == role, last.text == text {
if endsTurn { entry.chat[entry.chat.count - 1].endsTurn = true }
return
}
entry.said += 1
entry.chat.append(ClaudeChatMessage(id: entry.said, role: role, text: text, at: date))
entry.chat.append(ClaudeChatMessage(id: entry.said, role: role, text: text, at: date, endsTurn: endsTurn))
if entry.chat.count > Self.chatLength { entry.chat.removeFirst(entry.chat.count - Self.chatLength) }
}

Expand Down Expand Up @@ -491,5 +514,7 @@ final class ClaudeSessionEngine {

private func touch(_ date: Date, in entry: inout Activity) {
entry.lastFact = max(entry.lastFact ?? date, date)
// Something was typed, said or run: the turn is real.
entry.registryOnly = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ struct ClaudeChatMessage: Equatable, Identifiable {
let role: Role
let text: String
let at: Date
/// The reply the turn ended on, which answers what was asked; the
/// replies before it tell of the work on the way.
var endsTurn = false
}

struct ClaudeSessionCounts: Equatable {
Expand Down Expand Up @@ -259,7 +262,8 @@ enum ClaudeTranscriptFact: Equatable {
case turnEnded(Date, completed: Bool)
case toolStarted(ClaudeToolUse)
case toolFinished(id: String, at: Date, failed: Bool)
case text(String, at: Date)
/// What the model wrote; the text a turn ends on ends that turn.
case text(String, at: Date, endsTurn: Bool = false)
/// A prompt the person typed, read only when previews are allowed.
case prompt(String, at: Date)
case title(String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ struct ClaudeTranscriptReader {
switch message["stop_reason"] as? String {
case "end_turn", "stop_sequence", "max_tokens", "refusal":
let failed = json["isApiErrorMessage"] as? Bool == true || model.hasPrefix("<")
// The last text of a turn that ended as it should sums it up;
// a refusal or a cut-off reply stops short of that.
if !failed, ["end_turn", "stop_sequence"].contains(message["stop_reason"] as? String),
let index = facts.lastIndex(where: { if case .text = $0 { true } else { false } }),
case .text(let text, let at, _) = facts[index] {
facts[index] = .text(text, at: at, endsTurn: true)
}
facts.append(.turnEnded(date, completed: !failed))
default: break
}
Expand All @@ -113,6 +120,10 @@ struct ClaudeTranscriptReader {
?? blocks.compactMap { $0["type"] as? String == "text" ? $0["text"] as? String : nil }
if texts.contains(where: { $0.hasPrefix("[Request interrupted by user") || $0.hasPrefix("<local-command-std") }) {
facts.append(.turnEnded(date, completed: false))
} else if texts.contains(where: { $0.hasPrefix("<command-name>") }) {
// A slash command typed in the terminal, like /recap or /cost,
// asks the model nothing by itself; one that does, like a
// skill, begins its turn with the reply or the registry.
} else if !texts.isEmpty {
facts.append(.turnBegan(date))
// Task notifications and the summary a compaction leaves
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ enum NotchClaudeSessionsSupport {
return thinkingGlyphs[place < thinkingGlyphs.count ? place : cycle - place]
}

/// What the conversation sets apart: each reply a turn ended on, which
/// sums the turn up, and the replies written on the way to one. A turn
/// needs no prompt of its own, as one a slash command, a finished task or
/// a timer began; replies of a turn still running are neither.
static func answers(in chat: [ClaudeChatMessage]) -> (answers: Set<Int>, onTheWay: Set<Int>) {
var answers = Set<Int>(), onTheWay = Set<Int>()
var answered = false
for message in chat.reversed() {
if message.role == .user {
answered = false
} else if message.endsTurn {
answers.insert(message.id)
answered = true
} else if answered {
onTheWay.insert(message.id)
}
}
return (answers, onTheWay)
}

/// Text sizes a session's enlarged page offers.
static let largeTextSizes: ClosedRange<Double> = 12...24
static func largeTextSize(_ size: Double) -> Double {
Expand Down
36 changes: 33 additions & 3 deletions Sources/Vorssaint/UI/Notch/NotchClaudeSessionLargeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,14 @@ struct NotchClaudeSessionLargeView: View {
} else if session.chat.isEmpty && !session.phase.isWorking {
Text(text.noMessages).font(.system(size: size - 1)).foregroundStyle(.tertiary)
} else {
let marks = NotchClaudeSessionsSupport.answers(in: session.chat)
ScrollViewReader { proxy in
ScrollView {
LazyVStack(alignment: .leading, spacing: size) {
ForEach(session.chat) { message in
bubble(message).id(message.id)
bubble(message, answer: marks.answers.contains(message.id),
onTheWay: marks.onTheWay.contains(message.id))
.id(message.id)
}
if session.phase.isWorking {
NotchClaudeThinkingRow(session: session, size: size)
Expand Down Expand Up @@ -243,7 +246,7 @@ struct NotchClaudeSessionLargeView: View {
}
}

@ViewBuilder private func bubble(_ message: ClaudeChatMessage) -> some View {
@ViewBuilder private func bubble(_ message: ClaudeChatMessage, answer: Bool, onTheWay: Bool) -> some View {
switch message.role {
case .user:
VStack(alignment: .trailing, spacing: 4) {
Expand All @@ -259,12 +262,39 @@ struct NotchClaudeSessionLargeView: View {
}
.padding(.leading, 60)
.frame(maxWidth: .infinity, alignment: .trailing)
case .assistant where answer:
// The reply a turn ended on stands apart from the ones written
// on the way, under a rule of its own.
VStack(alignment: .leading, spacing: size * 0.6) {
answerRule(message)
NotchClaudeRichText(text: NotchClaudeSessionFormat.markdown(message.text), size: size,
color: .white.withAlphaComponent(0.96), lineSpacing: size * 0.25)
}
.padding(.top, size * 0.5)
.frame(maxWidth: .infinity, alignment: .leading)
case .assistant:
NotchClaudeRichText(text: NotchClaudeSessionFormat.markdown(message.text), size: size, lineSpacing: size * 0.25)
NotchClaudeRichText(text: NotchClaudeSessionFormat.markdown(message.text), size: size,
color: .white.withAlphaComponent(onTheWay ? 0.6 : 0.88), lineSpacing: size * 0.25)
.frame(maxWidth: .infinity, alignment: .leading)
}
}

private func answerRule(_ message: ClaudeChatMessage) -> some View {
HStack(spacing: 8) {
Text(text.answer.uppercased())
.font(.system(size: max(10, size - 4), weight: .semibold))
.tracking(0.8)
.foregroundStyle(AgentProvider.claude.tint)
Text(message.at, style: .time)
.font(.system(size: max(10, size - 4)))
.foregroundStyle(.tertiary)
Rectangle()
.fill(AgentProvider.claude.tint.opacity(0.35))
.frame(height: 1)
}
.accessibilityElement(children: .combine)
}

// MARK: Sidebar

private func sidebar(now: Date) -> some View {
Expand Down
Loading
Loading