feat(background-task): show original prompt and last message in running task status
- Add prompt field to BackgroundTask to store original prompt - Add lastMessage/lastMessageAt to TaskProgress for real-time monitoring - Extract last assistant message during polling - Update formatTaskStatus() to display prompt (truncated 300 chars) and last message (truncated 500 chars) with timestamp 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
@@ -59,6 +59,7 @@ export class BackgroundManager {
|
|||||||
parentSessionID: input.parentSessionID,
|
parentSessionID: input.parentSessionID,
|
||||||
parentMessageID: input.parentMessageID,
|
parentMessageID: input.parentMessageID,
|
||||||
description: input.description,
|
description: input.description,
|
||||||
|
prompt: input.prompt,
|
||||||
agent: input.agent,
|
agent: input.agent,
|
||||||
status: "running",
|
status: "running",
|
||||||
startedAt: new Date(),
|
startedAt: new Date(),
|
||||||
@@ -316,7 +317,7 @@ export class BackgroundManager {
|
|||||||
if (!messagesResult.error && messagesResult.data) {
|
if (!messagesResult.error && messagesResult.data) {
|
||||||
const messages = messagesResult.data as Array<{
|
const messages = messagesResult.data as Array<{
|
||||||
info?: { role?: string }
|
info?: { role?: string }
|
||||||
parts?: Array<{ type?: string; tool?: string; name?: string }>
|
parts?: Array<{ type?: string; tool?: string; name?: string; text?: string }>
|
||||||
}>
|
}>
|
||||||
const assistantMsgs = messages.filter(
|
const assistantMsgs = messages.filter(
|
||||||
(m) => m.info?.role === "assistant"
|
(m) => m.info?.role === "assistant"
|
||||||
@@ -324,6 +325,7 @@ export class BackgroundManager {
|
|||||||
|
|
||||||
let toolCalls = 0
|
let toolCalls = 0
|
||||||
let lastTool: string | undefined
|
let lastTool: string | undefined
|
||||||
|
let lastMessage: string | undefined
|
||||||
|
|
||||||
for (const msg of assistantMsgs) {
|
for (const msg of assistantMsgs) {
|
||||||
const parts = msg.parts ?? []
|
const parts = msg.parts ?? []
|
||||||
@@ -332,6 +334,9 @@ export class BackgroundManager {
|
|||||||
toolCalls++
|
toolCalls++
|
||||||
lastTool = part.tool || part.name || "unknown"
|
lastTool = part.tool || part.name || "unknown"
|
||||||
}
|
}
|
||||||
|
if (part.type === "text" && part.text) {
|
||||||
|
lastMessage = part.text
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -341,6 +346,10 @@ export class BackgroundManager {
|
|||||||
task.progress.toolCalls = toolCalls
|
task.progress.toolCalls = toolCalls
|
||||||
task.progress.lastTool = lastTool
|
task.progress.lastTool = lastTool
|
||||||
task.progress.lastUpdate = new Date()
|
task.progress.lastUpdate = new Date()
|
||||||
|
if (lastMessage) {
|
||||||
|
task.progress.lastMessage = lastMessage
|
||||||
|
task.progress.lastMessageAt = new Date()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log("[background-agent] Poll error for task:", { taskId: task.id, error })
|
log("[background-agent] Poll error for task:", { taskId: task.id, error })
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ export interface TaskProgress {
|
|||||||
toolCalls: number
|
toolCalls: number
|
||||||
lastTool?: string
|
lastTool?: string
|
||||||
lastUpdate: Date
|
lastUpdate: Date
|
||||||
|
lastMessage?: string
|
||||||
|
lastMessageAt?: Date
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface BackgroundTask {
|
export interface BackgroundTask {
|
||||||
@@ -16,6 +18,7 @@ export interface BackgroundTask {
|
|||||||
parentSessionID: string
|
parentSessionID: string
|
||||||
parentMessageID: string
|
parentMessageID: string
|
||||||
description: string
|
description: string
|
||||||
|
prompt: string
|
||||||
agent: string
|
agent: string
|
||||||
status: BackgroundTaskStatus
|
status: BackgroundTaskStatus
|
||||||
startedAt: Date
|
startedAt: Date
|
||||||
|
|||||||
@@ -62,21 +62,51 @@ function delay(ms: number): Promise<void> {
|
|||||||
return new Promise(resolve => setTimeout(resolve, ms))
|
return new Promise(resolve => setTimeout(resolve, ms))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function truncateText(text: string, maxLength: number): string {
|
||||||
|
if (text.length <= maxLength) return text
|
||||||
|
return text.slice(0, maxLength) + "..."
|
||||||
|
}
|
||||||
|
|
||||||
function formatTaskStatus(task: BackgroundTask): string {
|
function formatTaskStatus(task: BackgroundTask): string {
|
||||||
const duration = formatDuration(task.startedAt, task.completedAt)
|
const duration = formatDuration(task.startedAt, task.completedAt)
|
||||||
const progress = task.progress
|
const promptPreview = truncateText(task.prompt, 500)
|
||||||
? `\nTool calls: ${task.progress.toolCalls}\nLast tool: ${task.progress.lastTool ?? "N/A"}`
|
|
||||||
: ""
|
|
||||||
|
|
||||||
return `Task Status
|
let progressSection = ""
|
||||||
|
if (task.progress) {
|
||||||
|
progressSection = `\nTool calls: ${task.progress.toolCalls}\nLast tool: ${task.progress.lastTool ?? "N/A"}`
|
||||||
|
}
|
||||||
|
|
||||||
Task ID: ${task.id}
|
let lastMessageSection = ""
|
||||||
Description: ${task.description}
|
if (task.progress?.lastMessage) {
|
||||||
Agent: ${task.agent}
|
const truncated = truncateText(task.progress.lastMessage, 500)
|
||||||
Status: ${task.status}
|
const messageTime = task.progress.lastMessageAt
|
||||||
Duration: ${duration}${progress}
|
? task.progress.lastMessageAt.toISOString()
|
||||||
|
: "N/A"
|
||||||
|
lastMessageSection = `
|
||||||
|
|
||||||
Session ID: ${task.sessionID}`
|
## Last Message (${messageTime})
|
||||||
|
|
||||||
|
\`\`\`
|
||||||
|
${truncated}
|
||||||
|
\`\`\``
|
||||||
|
}
|
||||||
|
|
||||||
|
return `# Task Status
|
||||||
|
|
||||||
|
| Field | Value |
|
||||||
|
|-------|-------|
|
||||||
|
| Task ID | \`${task.id}\` |
|
||||||
|
| Description | ${task.description} |
|
||||||
|
| Agent | ${task.agent} |
|
||||||
|
| Status | **${task.status}** |
|
||||||
|
| Duration | ${duration} |
|
||||||
|
| Session ID | \`${task.sessionID}\` |${progressSection}
|
||||||
|
|
||||||
|
## Original Prompt
|
||||||
|
|
||||||
|
\`\`\`
|
||||||
|
${promptPreview}
|
||||||
|
\`\`\`${lastMessageSection}`
|
||||||
}
|
}
|
||||||
|
|
||||||
async function formatTaskResult(task: BackgroundTask, client: OpencodeClient): Promise<string> {
|
async function formatTaskResult(task: BackgroundTask, client: OpencodeClient): Promise<string> {
|
||||||
|
|||||||
Reference in New Issue
Block a user