|
| 1 | +import type { ProgressAttributeDescriptor, ProgressRenderConfig } from "./tracing"; |
| 2 | + |
| 3 | +const hiddenAttributePrefixes = ["http.request.header.", "http.response.header."]; |
| 4 | + |
| 5 | +const hiddenInteractiveAttributeKeys = new Set([ |
| 6 | + "concurrency", |
| 7 | + "gen_ai.openai.request.response_format", |
| 8 | + "gen_ai.openai.response.service_tier", |
| 9 | + "gen_ai.operation.name", |
| 10 | + "gen_ai.response.id", |
| 11 | + "gen_ai.system", |
| 12 | + "server.port", |
| 13 | + "url.full", |
| 14 | +]); |
| 15 | + |
| 16 | +const orderedInteractiveAttributeKeys = [ |
| 17 | + "vcs", |
| 18 | + "requested_vcs", |
| 19 | + "dry_run", |
| 20 | + "no_stage", |
| 21 | + "amend", |
| 22 | + "step", |
| 23 | + "reason", |
| 24 | + "full_wizard", |
| 25 | + "hook_count", |
| 26 | + "max_commits", |
| 27 | + "staged_files", |
| 28 | + "unstaged_files", |
| 29 | + "file_count", |
| 30 | + "http.request.method", |
| 31 | + "url.path", |
| 32 | + "http.response.status_code", |
| 33 | + "server.address", |
| 34 | + "gen_ai.request.model", |
| 35 | + "gen_ai.response.model", |
| 36 | +] as const; |
| 37 | + |
| 38 | +const interactiveLabelByKey: Record<string, string> = { |
| 39 | + file_count: "files", |
| 40 | + hook_count: "hooks", |
| 41 | + max_commits: "commits", |
| 42 | + staged_files: "staged", |
| 43 | + unstaged_files: "unstaged", |
| 44 | + "gen_ai.request.model": "model", |
| 45 | + "gen_ai.response.model": "model", |
| 46 | + "http.request.method": "method", |
| 47 | + "http.response.status_code": "status", |
| 48 | + "server.address": "server", |
| 49 | + "url.path": "path", |
| 50 | +}; |
| 51 | + |
| 52 | +const toInteractiveDescriptors = ( |
| 53 | + attributes: ReadonlyMap<string, unknown>, |
| 54 | +): Array<ProgressAttributeDescriptor> => { |
| 55 | + const descriptors: Array<ProgressAttributeDescriptor> = []; |
| 56 | + const used = new Set<string>(); |
| 57 | + |
| 58 | + const push = ( |
| 59 | + key: string, |
| 60 | + value: unknown, |
| 61 | + label = interactiveLabelByKey[key] ?? key, |
| 62 | + dedupeKey?: string, |
| 63 | + ) => { |
| 64 | + if (value === undefined || value === null) { |
| 65 | + return; |
| 66 | + } |
| 67 | + used.add(key); |
| 68 | + descriptors.push( |
| 69 | + dedupeKey == null |
| 70 | + ? { |
| 71 | + key, |
| 72 | + label, |
| 73 | + value, |
| 74 | + } |
| 75 | + : { |
| 76 | + key, |
| 77 | + label, |
| 78 | + value, |
| 79 | + dedupeKey, |
| 80 | + }, |
| 81 | + ); |
| 82 | + }; |
| 83 | + |
| 84 | + const groupIndex = attributes.get("group_index"); |
| 85 | + const groupTotal = attributes.get("group_total"); |
| 86 | + if (groupIndex !== undefined && groupTotal !== undefined) { |
| 87 | + used.add("group_index"); |
| 88 | + used.add("group_total"); |
| 89 | + descriptors.push({ |
| 90 | + key: "group_index", |
| 91 | + label: "group", |
| 92 | + value: `${groupIndex}/${groupTotal}`, |
| 93 | + dedupeKey: `group=${groupIndex}/${groupTotal}`, |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + const inputTokens = attributes.get("gen_ai.usage.input_tokens"); |
| 98 | + const outputTokens = attributes.get("gen_ai.usage.output_tokens"); |
| 99 | + if (inputTokens !== undefined || outputTokens !== undefined) { |
| 100 | + used.add("gen_ai.usage.input_tokens"); |
| 101 | + used.add("gen_ai.usage.output_tokens"); |
| 102 | + descriptors.push({ |
| 103 | + key: "gen_ai.usage.input_tokens", |
| 104 | + label: "tokens", |
| 105 | + value: `${inputTokens ?? 0}/${outputTokens ?? 0}`, |
| 106 | + dedupeKey: `tokens=${inputTokens ?? 0}/${outputTokens ?? 0}`, |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + for (const key of orderedInteractiveAttributeKeys) { |
| 111 | + if (used.has(key) || !attributes.has(key)) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + push(key, attributes.get(key)); |
| 115 | + } |
| 116 | + |
| 117 | + const remaining = [...attributes.entries()] |
| 118 | + .filter(([key, value]) => { |
| 119 | + if (used.has(key) || value === undefined || value === null) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + if (hiddenInteractiveAttributeKeys.has(key)) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + return hiddenAttributePrefixes.every((prefix) => !key.startsWith(prefix)); |
| 126 | + }) |
| 127 | + .sort(([left], [right]) => left.localeCompare(right)); |
| 128 | + |
| 129 | + for (const [key, value] of remaining) { |
| 130 | + push(key, value); |
| 131 | + } |
| 132 | + |
| 133 | + return descriptors; |
| 134 | +}; |
| 135 | + |
| 136 | +export const gitAgentProgressRenderConfig: ProgressRenderConfig = { |
| 137 | + headerLabel: "Git agent", |
| 138 | + formatAttributes({ attributes }, { mode }) { |
| 139 | + if (mode === "raw") { |
| 140 | + return undefined; |
| 141 | + } |
| 142 | + return toInteractiveDescriptors(attributes); |
| 143 | + }, |
| 144 | +}; |
0 commit comments