Skip to content

Commit c3a2edc

Browse files
committed
added message details
1 parent c226b2f commit c3a2edc

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

Sources/osaurus_telegram/Models.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ struct TaskFailedEvent: Decodable {
116116
let summary: String?
117117
}
118118

119+
struct TaskOutputEvent: Decodable {
120+
let text: String?
121+
}
122+
119123
// MARK: - Dispatch Response
120124

121125
struct DispatchResponse: Decodable {

Sources/osaurus_telegram/TaskHandler.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ private enum TaskEventType {
1010
static let completed: Int32 = 4
1111
static let failed: Int32 = 5
1212
static let cancelled: Int32 = 6
13+
static let output: Int32 = 7
1314
}
1415

1516
/// Derives a stable non-zero draft ID from a task ID string.
@@ -23,7 +24,7 @@ func draftId(for taskId: String) -> Int {
2324

2425
private let taskEventNames: [Int32: String] = [
2526
0: "STARTED", 1: "ACTIVITY", 2: "PROGRESS", 3: "CLARIFICATION",
26-
4: "COMPLETED", 5: "FAILED", 6: "CANCELLED",
27+
4: "COMPLETED", 5: "FAILED", 6: "CANCELLED", 7: "OUTPUT",
2728
]
2829

2930
func handleTaskEvent(ctx: PluginContext, taskId: String, eventType: Int32, eventJSON: String) {
@@ -71,6 +72,10 @@ func handleTaskEvent(ctx: PluginContext, taskId: String, eventType: Int32, event
7172
case TaskEventType.cancelled:
7273
handleCancelled(token: token, chatId: chatId, task: task, isPrivate: isPrivate)
7374

75+
case TaskEventType.output:
76+
handleOutput(
77+
token: token, chatId: chatId, task: task, isPrivate: isPrivate, eventJSON: eventJSON)
78+
7479
default:
7580
logWarn("Unknown task event type \(eventType) for task \(taskId)")
7681
}
@@ -276,3 +281,26 @@ private func handleCancelled(token: String, chatId: String, task: TaskRow, isPri
276281
DatabaseManager.updateTask(taskId: task.taskId, status: "cancelled")
277282
logInfo("Task \(task.taskId) cancelled for chat \(chatId)")
278283
}
284+
285+
private func handleOutput(
286+
token: String, chatId: String, task: TaskRow, isPrivate: Bool, eventJSON: String
287+
) {
288+
guard let event = parseJSON(eventJSON, as: TaskOutputEvent.self),
289+
let text = event.text, !text.isEmpty
290+
else {
291+
logDebug("handleOutput: task \(task.taskId) failed to parse output event or empty text")
292+
return
293+
}
294+
295+
logDebug("handleOutput: task \(task.taskId) text=\(text.count) chars")
296+
297+
if isPrivate {
298+
_ = telegramSendMessageDraft(
299+
token: token, chatId: chatId,
300+
draftId: draftId(for: task.taskId),
301+
text: String(text.prefix(4096))
302+
)
303+
} else {
304+
telegramSendChatAction(token: token, chatId: chatId)
305+
}
306+
}

Tests/osaurus_telegram_tests/ModelsTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,19 @@ struct TaskEventPayloadTests {
219219
#expect(event.detail == "src/main.swift")
220220
}
221221

222+
@Test("Decodes TaskOutputEvent")
223+
func outputEvent() throws {
224+
let json = "{\"text\":\"Here are the results:\\n\\n1. First item\"}"
225+
let event = try #require(parseJSON(json, as: TaskOutputEvent.self))
226+
#expect(event.text == "Here are the results:\n\n1. First item")
227+
}
228+
229+
@Test("Decodes TaskOutputEvent with null text")
230+
func outputEventNullText() throws {
231+
let event = try #require(parseJSON("{}", as: TaskOutputEvent.self))
232+
#expect(event.text == nil)
233+
}
234+
222235
@Test("Handles missing optional fields gracefully")
223236
func missingOptionals() throws {
224237
let event = try #require(parseJSON("{}", as: TaskCompletedEvent.self))

0 commit comments

Comments
 (0)