1+ // Upstream reference: claude_code_formatted.js L1867-L1884
2+ // OpenTelemetry-style structured metrics for observability
3+
14const DEFAULT_NEW_PROMPT_API_URL = "https://deepcode.vegamo.cn/api/plugin/new" ;
25const DEFAULT_REPORT_TIMEOUT_MS = 3000 ;
36
@@ -7,6 +10,26 @@ export type NewPromptReportOptions = {
710 timeoutMs ?: number ;
811} ;
912
13+ export type PermissionDecisionMetrics = {
14+ /** Tool type: "Edit" | "Write" | "Read" | "Bash" | "NotebookEdit" */
15+ toolType : string ;
16+ /** Decision: "accept" | "reject" */
17+ decision : "accept" | "reject" ;
18+ } ;
19+
20+ export type ToolUsageMetrics = {
21+ /** Total cost in USD for the session */
22+ totalCostUSD : number ;
23+ /** Total API duration in ms */
24+ totalAPIDuration : number ;
25+ /** Lines of code added */
26+ totalLinesAdded : number ;
27+ /** Lines of code removed */
28+ totalLinesRemoved : number ;
29+ /** Number of tool calls made */
30+ totalToolCalls : number ;
31+ } ;
32+
1033/**
1134 * Fire-and-forget report of a new prompt session.
1235 * Respects the `enabled` toggle: when disabled, the call is a no-op.
@@ -32,3 +55,67 @@ export function reportNewPrompt(options: NewPromptReportOptions): void {
3255 . catch ( ( ) => { } )
3356 . finally ( ( ) => clearTimeout ( timeout ) ) ;
3457}
58+
59+ // In-memory metrics store (non-persistent, per-session)
60+ let sessionPermissionDecisions : { accept : number ; reject : number } = { accept : 0 , reject : 0 } ;
61+ let sessionToolUsage : ToolUsageMetrics = {
62+ totalCostUSD : 0 ,
63+ totalAPIDuration : 0 ,
64+ totalLinesAdded : 0 ,
65+ totalLinesRemoved : 0 ,
66+ totalToolCalls : 0 ,
67+ } ;
68+
69+ /**
70+ * Record a permission decision (accept/reject for a tool).
71+ * Upstream reference: code_edit_tool.decision counter
72+ */
73+ export function recordPermissionDecision ( decision : PermissionDecisionMetrics ) : void {
74+ if ( decision . decision === "accept" ) {
75+ sessionPermissionDecisions . accept ++ ;
76+ } else {
77+ sessionPermissionDecisions . reject ++ ;
78+ }
79+ }
80+
81+ /**
82+ * Record tool usage metrics.
83+ * Upstream reference: totalToolDuration, totalCostUSD, totalLinesAdded/Removed
84+ */
85+ export function recordToolUsage ( metrics : Partial < ToolUsageMetrics > ) : void {
86+ if ( metrics . totalCostUSD !== undefined ) {
87+ sessionToolUsage . totalCostUSD += metrics . totalCostUSD ;
88+ }
89+ if ( metrics . totalAPIDuration !== undefined ) {
90+ sessionToolUsage . totalAPIDuration += metrics . totalAPIDuration ;
91+ }
92+ if ( metrics . totalLinesAdded !== undefined ) {
93+ sessionToolUsage . totalLinesAdded += metrics . totalLinesAdded ;
94+ }
95+ if ( metrics . totalLinesRemoved !== undefined ) {
96+ sessionToolUsage . totalLinesRemoved += metrics . totalLinesRemoved ;
97+ }
98+ if ( metrics . totalToolCalls !== undefined ) {
99+ sessionToolUsage . totalToolCalls += metrics . totalToolCalls ;
100+ }
101+ }
102+
103+ export function getSessionPermissionDecisions ( ) : { accept : number ; reject : number } {
104+ return { ...sessionPermissionDecisions } ;
105+ }
106+
107+ export function getSessionToolUsage ( ) : ToolUsageMetrics {
108+ return { ...sessionToolUsage } ;
109+ }
110+
111+ /** Reset all metrics (for testing or session restart) */
112+ export function resetSessionMetrics ( ) : void {
113+ sessionPermissionDecisions = { accept : 0 , reject : 0 } ;
114+ sessionToolUsage = {
115+ totalCostUSD : 0 ,
116+ totalAPIDuration : 0 ,
117+ totalLinesAdded : 0 ,
118+ totalLinesRemoved : 0 ,
119+ totalToolCalls : 0 ,
120+ } ;
121+ }
0 commit comments