diff --git a/aider/analytics.py b/aider/analytics.py index d5c37c10a..59913ccbf 100644 --- a/aider/analytics.py +++ b/aider/analytics.py @@ -70,9 +70,17 @@ class Analytics: # ephemeral logfile = None - def __init__(self, logfile=None, permanently_disable=False): + def __init__( + self, + logfile=None, + permanently_disable=False, + posthog_host=None, + posthog_project_api_key=None + ): self.logfile = logfile self.get_or_create_uuid() + self.custom_posthog_host = posthog_host + self.custom_posthog_project_api_key = posthog_project_api_key if self.permanently_disable or permanently_disable or not self.asked_opt_in: self.disable(permanently_disable) @@ -92,8 +100,8 @@ class Analytics: # self.mp = Mixpanel(mixpanel_project_token) self.ph = Posthog( - project_api_key=posthog_project_api_key, - host=posthog_host, + project_api_key=self.custom_posthog_project_api_key or posthog_project_api_key, + host=self.custom_posthog_host or posthog_host, on_error=self.posthog_error, enable_exception_autocapture=True, super_properties=self.get_system_info(), # Add system info to all events diff --git a/aider/args.py b/aider/args.py index 0e1b13c42..0728d5938 100644 --- a/aider/args.py +++ b/aider/args.py @@ -582,6 +582,16 @@ def get_parser(default_config_files, git_root): help="Permanently disable analytics", default=False, ) + group.add_argument( + "--analytics-posthog-host", + metavar="ANALYTICS_POSTHOG_HOST", + help="Send analytics to custom PostHog instance", + ) + group.add_argument( + "--analytics-posthog-project-api-key", + metavar="ANALYTICS_POSTHOG_PROJECT_API_KEY", + help="Send analytics to custom PostHog project" + ) ######### group = parser.add_argument_group("Upgrading") diff --git a/aider/history.py b/aider/history.py index a4727b941..9836be0ca 100644 --- a/aider/history.py +++ b/aider/history.py @@ -63,37 +63,38 @@ class ChatSummary: if split_index <= min_split: return self.summarize_all(messages) + # Split head and tail head = messages[:split_index] tail = messages[split_index:] - sized = sized[:split_index] - head.reverse() - sized.reverse() + # Only size the head once + sized_head = sized[:split_index] + + # Precompute token limit (fallback to 4096 if undefined) + model_max_input_tokens = self.models[0].info.get("max_input_tokens") or 4096 + model_max_input_tokens -= 512 # reserve buffer for safety + keep = [] total = 0 - # These sometimes come set with value = None - model_max_input_tokens = self.models[0].info.get("max_input_tokens") or 4096 - model_max_input_tokens -= 512 - - for i in range(split_index): - total += sized[i][0] + # Iterate in original order, summing tokens until limit + for tokens, msg in sized_head: + total += tokens if total > model_max_input_tokens: break - keep.append(head[i]) - - keep.reverse() + keep.append(msg) + # No need to reverse lists back and forth summary = self.summarize_all(keep) - tail_tokens = sum(tokens for tokens, msg in sized[split_index:]) + # If the combined summary and tail still fits, return directly summary_tokens = self.token_count(summary) - - result = summary + tail + tail_tokens = sum(tokens for tokens, _ in sized[split_index:]) if summary_tokens + tail_tokens < self.max_tokens: - return result + return summary + tail - return self.summarize_real(result, depth + 1) + # Otherwise recurse with increased depth + return self.summarize_real(summary + tail, depth + 1) def summarize_all(self, messages): content = "" diff --git a/aider/main.py b/aider/main.py index 83855bad7..00427cc39 100644 --- a/aider/main.py +++ b/aider/main.py @@ -633,7 +633,12 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F ) os.environ["OPENAI_ORGANIZATION"] = args.openai_organization_id - analytics = Analytics(logfile=args.analytics_log, permanently_disable=args.analytics_disable) + analytics = Analytics( + logfile=args.analytics_log, + permanently_disable=args.analytics_disable, + posthog_host=args.analytics_posthog_host, + posthog_project_api_key=args.analytics_posthog_project_api_key + ) if args.analytics is not False: if analytics.need_to_ask(args.analytics): io.tool_output( diff --git a/aider/website/assets/sample.aider.conf.yml b/aider/website/assets/sample.aider.conf.yml index 3e2082cf5..171d4a021 100644 --- a/aider/website/assets/sample.aider.conf.yml +++ b/aider/website/assets/sample.aider.conf.yml @@ -298,6 +298,12 @@ ## Permanently disable analytics #analytics-disable: false +## PostHog host (default: https://us.i.posthog.com) +#analytics-posthog-host: xxx + +## PostHog project api key (default: phc_99T7muzafUMMZX15H8XePbMSreEUzahHbtWjy3l5Qbv) +#analytics-posthog-project-api-key: xxx + ############ # Upgrading: diff --git a/aider/website/docs/more/analytics.md b/aider/website/docs/more/analytics.md index adb0ddebd..74de05213 100644 --- a/aider/website/docs/more/analytics.md +++ b/aider/website/docs/more/analytics.md @@ -105,6 +105,12 @@ If you want to just log analytics without reporting them, you can do: aider --analytics-log filename.jsonl --no-analytics ``` +### Sending analytics to custom PostHog project or installation + +Aider uses PostHog for analytics collection. You can configure aider to send analytics to your own PostHog project or a custom PostHog installation using these parameters: + +- `--analytics-posthog-project-api-key KEY` - Set a custom PostHog project API key +- `--analytics-posthog-host HOST` - Set a custom PostHog host (default is app.posthog.com) ## Reporting issues