( options?: ChatHistoryItem[] | UpdateAgentMetadataOptions, )
| 102 | * @param options - Options including history and completion state |
| 103 | */ |
| 104 | export async function updateAgentMetadata( |
| 105 | options?: ChatHistoryItem[] | UpdateAgentMetadataOptions, |
| 106 | ): Promise<void> { |
| 107 | // Support both old signature (history array) and new signature (options object) |
| 108 | const { history, isComplete } = Array.isArray(options) |
| 109 | ? { history: options, isComplete: false } |
| 110 | : { history: options?.history, isComplete: options?.isComplete ?? false }; |
| 111 | |
| 112 | try { |
| 113 | const agentId = getAgentIdFromArgs(); |
| 114 | if (!agentId) { |
| 115 | logger.debug("No agent ID found, skipping metadata update"); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | const metadata: Record<string, any> = {}; |
| 120 | const hasChanges = await collectDiffStats(metadata); |
| 121 | |
| 122 | if (isComplete) { |
| 123 | metadata.isComplete = true; |
| 124 | metadata.hasChanges = hasChanges; |
| 125 | } |
| 126 | |
| 127 | collectSummary(metadata, history); |
| 128 | collectSessionUsage(metadata); |
| 129 | |
| 130 | if (Object.keys(metadata).length > 0) { |
| 131 | await postAgentMetadata(agentId, metadata); |
| 132 | } |
| 133 | } catch (err) { |
| 134 | // Non-critical: log but don't fail the process |
| 135 | logger.debug("Error in updateAgentMetadata (ignored)", err as any); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Display session usage breakdown in verbose mode |
no test coverage detected