forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentLifecycleTranscriptProjection.swift
More file actions
145 lines (135 loc) · 4.94 KB
/
Copy pathAgentLifecycleTranscriptProjection.swift
File metadata and controls
145 lines (135 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import Foundation
/// Produces a presentation-only transcript for agent lifecycle receipts.
///
/// The kernel's session, run, and pill identifiers remain in structured blocks
/// for lifecycle linking and recovery. They are implementation details, though,
/// so they must not also leak through an assistant's free-form launch prose.
enum AgentLifecycleTranscriptProjection {
static func project(_ message: ChatMessage) -> ChatMessage {
guard message.sender == .ai else { return message }
let identifiers = internalIdentifiers(in: message.contentBlocks)
let hasLifecycleReceipt = message.contentBlocks.contains { block in
switch block {
case .agentSpawn, .agentCompletion:
return true
default:
return false
}
}
guard hasLifecycleReceipt else { return message }
let hasSpawnReceipt = message.contentBlocks.contains { block in
if case .agentSpawn = block { return true }
return false
}
var projected = message
projected.text = visibleText(
message.text,
identifiers: identifiers,
hasSpawnReceipt: hasSpawnReceipt
)
projected.contentBlocks = message.contentBlocks.map { block in
guard case .text(let id, let text) = block else { return block }
return .text(
id: id,
text: visibleText(
text,
identifiers: identifiers,
hasSpawnReceipt: hasSpawnReceipt
)
)
}
return projected
}
/// Removes a duplicate launch paragraph only when the message already has a
/// structured spawn receipt. In all other prose, only exact, known internal
/// identifiers are removed; user text and arbitrary code are not pattern
/// matched or rewritten.
static func visibleText(
_ text: String,
identifiers: Set<String>,
hasSpawnReceipt: Bool
) -> String {
guard !text.isEmpty else { return text }
return
text
.components(separatedBy: "\n\n")
.compactMap { paragraph -> String? in
if hasSpawnReceipt, isReceiptOnlySpawnAnnouncement(paragraph) {
return nil
}
return redactExactIdentifiers(in: paragraph, identifiers: identifiers)
}
.joined(separator: "\n\n")
.trimmingCharacters(in: .whitespacesAndNewlines)
}
private static func internalIdentifiers(in blocks: [ChatContentBlock]) -> Set<String> {
var identifiers = Set<String>()
for block in blocks {
switch block {
case .agentSpawn(_, let pillID, let sessionID, let runID, _, _, _):
insert(runID, into: &identifiers)
insert(sessionID, into: &identifiers)
if let pillID { identifiers.insert(pillID.uuidString) }
case .agentCompletion(_, let pillID, let sessionID, let runID, _, _, _, _):
insert(runID, into: &identifiers)
insert(sessionID, into: &identifiers)
if let pillID { identifiers.insert(pillID.uuidString) }
default:
continue
}
}
return identifiers
}
private static func insert(_ value: String?, into identifiers: inout Set<String>) {
let trimmed = value?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
guard !trimmed.isEmpty else { return }
identifiers.insert(trimmed)
}
/// Keep a complete paragraph only when it is solely the redundant launch
/// receipt. Model output can append a useful result to the same paragraph,
/// and hiding that result would make the projection lossy.
private static func isReceiptOnlySpawnAnnouncement(_ text: String) -> Bool {
let lowercased = text.lowercased()
let explicitLaunchPhrases = [
"subagent spawned",
"sub-agent spawned",
"agent spawned",
"started a subagent",
"started a sub-agent",
"launched a subagent",
"launched a sub-agent",
"spun up a subagent",
"spun up a sub-agent",
]
let isLaunchAnnouncement =
explicitLaunchPhrases.contains(where: { lowercased.contains($0) })
|| (lowercased.contains("floating pill")
&& (lowercased.contains("running") || lowercased.contains("spawn")))
guard isLaunchAnnouncement else { return false }
// This deliberately errs on showing some redundant wording over ever
// dropping a real result. The known lifecycle phrases do not contain
// any of these result signals.
let resultSignals = [
"result:", "answer:", "found ", "returned ", "output:",
"summary:", "reply:", "responded ", "produced ",
]
return !resultSignals.contains(where: { lowercased.contains($0) })
}
private static func redactExactIdentifiers(in text: String, identifiers: Set<String>) -> String {
identifiers
.sorted { $0.count > $1.count }
.reduce(text) { result, identifier in
result
.replacingOccurrences(
of: "(\(identifier))",
with: "",
options: .caseInsensitive
)
.replacingOccurrences(
of: identifier,
with: "",
options: .caseInsensitive
)
}
}
}