|
| 1 | +package aitools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "slices" |
| 6 | + |
| 7 | + "github.com/databricks/cli/cmd/root" |
| 8 | + "github.com/databricks/cli/libs/aitools/agents" |
| 9 | + "github.com/databricks/cli/libs/aitools/installer" |
| 10 | + "github.com/databricks/cli/libs/telemetry" |
| 11 | + "github.com/databricks/cli/libs/telemetry/protos" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +// tryConfigureAuth resolves auth config onto the context so telemetry can |
| 16 | +// upload at command exit. It does not fail if auth is not configured. |
| 17 | +func tryConfigureAuth(cmd *cobra.Command, args []string) error { |
| 18 | + ctx := root.SkipPrompt(cmd.Context()) |
| 19 | + ctx = root.SkipLoadBundle(ctx) |
| 20 | + cmd.SetContext(ctx) |
| 21 | + _, _ = root.MustAnyClient(cmd, args) |
| 22 | + return nil |
| 23 | +} |
| 24 | + |
| 25 | +// installOpts is the subset of install options telemetry records, kept narrow |
| 26 | +// so every field that leaves the machine is visible here. |
| 27 | +type installOpts struct { |
| 28 | + Scope string |
| 29 | + Experimental bool |
| 30 | +} |
| 31 | + |
| 32 | +// logInstallEvent buffers an install event; cmd/root uploads it at exit. |
| 33 | +func logInstallEvent(ctx context.Context, plan []agentPlanItem, opts installOpts) { |
| 34 | + telemetry.Log(ctx, protos.DatabricksCliLog{ |
| 35 | + AitoolsInstallEvent: &protos.AitoolsInstallEvent{ |
| 36 | + Agents: agentsField(plan), |
| 37 | + Scope: scopeType(opts.Scope), |
| 38 | + Experimental: opts.Experimental, |
| 39 | + }, |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +// agentsField returns the deduped agent enums from the plan, sorted so the |
| 44 | +// same set of agents always produces the same array on the analytics side. |
| 45 | +func agentsField(plan []agentPlanItem) []protos.AitoolsAgentType { |
| 46 | + if len(plan) == 0 { |
| 47 | + return nil |
| 48 | + } |
| 49 | + out := make([]protos.AitoolsAgentType, 0, len(plan)) |
| 50 | + seen := make(map[protos.AitoolsAgentType]struct{}, len(plan)) |
| 51 | + for _, it := range plan { |
| 52 | + if it.agent == nil { |
| 53 | + continue |
| 54 | + } |
| 55 | + t := agentType(it.agent.Name) |
| 56 | + if _, ok := seen[t]; ok { |
| 57 | + continue |
| 58 | + } |
| 59 | + seen[t] = struct{}{} |
| 60 | + out = append(out, t) |
| 61 | + } |
| 62 | + slices.Sort(out) |
| 63 | + return out |
| 64 | +} |
| 65 | + |
| 66 | +// agentType maps a CLI agent registry name to its telemetry enum. Every agent |
| 67 | +// in agents.Registry must have a case here; TestAgentTypeCoversRegistry fails |
| 68 | +// if one is missing. The default maps genuinely unknown names to Unspecified |
| 69 | +// so an older proto never drops an install. |
| 70 | +func agentType(name string) protos.AitoolsAgentType { |
| 71 | + switch name { |
| 72 | + case agents.NameClaudeCode: |
| 73 | + return protos.AitoolsAgentTypeClaudeCode |
| 74 | + case agents.NameCursor: |
| 75 | + return protos.AitoolsAgentTypeCursor |
| 76 | + case agents.NameCodex: |
| 77 | + return protos.AitoolsAgentTypeCodex |
| 78 | + case agents.NameOpenCode: |
| 79 | + return protos.AitoolsAgentTypeOpenCode |
| 80 | + case agents.NameCopilot: |
| 81 | + return protos.AitoolsAgentTypeCopilot |
| 82 | + case agents.NameAntigravity: |
| 83 | + return protos.AitoolsAgentTypeAntigravity |
| 84 | + default: |
| 85 | + return protos.AitoolsAgentTypeUnspecified |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// scopeType maps a resolved install scope to its telemetry enum. |
| 90 | +func scopeType(scope string) protos.AitoolsInstallScope { |
| 91 | + switch scope { |
| 92 | + case installer.ScopeGlobal: |
| 93 | + return protos.AitoolsInstallScopeGlobal |
| 94 | + case installer.ScopeProject: |
| 95 | + return protos.AitoolsInstallScopeProject |
| 96 | + default: |
| 97 | + return protos.AitoolsInstallScopeUnspecified |
| 98 | + } |
| 99 | +} |
0 commit comments