xithead 1.2.2 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.actrc +14 -0
- package/.actrc.local.example +15 -0
- package/.changelog.md +2 -0
- package/.claude/agents/academic-anthropologist.md +125 -0
- package/.claude/agents/academic-geographer.md +127 -0
- package/.claude/agents/academic-historian.md +123 -0
- package/.claude/agents/academic-narratologist.md +118 -0
- package/.claude/agents/academic-psychologist.md +118 -0
- package/.claude/agents/academic-statistician.md +144 -0
- package/.claude/agents/accounts-payable-agent.md +205 -0
- package/.claude/agents/agentic-identity-trust.md +394 -0
- package/.claude/agents/agents-orchestrator.md +367 -0
- package/.claude/agents/automation-governance-architect.md +216 -0
- package/.claude/agents/blender-addon-engineer.md +243 -0
- package/.claude/agents/business-strategist.md +488 -0
- package/.claude/agents/change-management-consultant.md +497 -0
- package/.claude/agents/chief-financial-officer.md +388 -0
- package/.claude/agents/corporate-training-designer.md +192 -0
- package/.claude/agents/customer-service.md +398 -0
- package/.claude/agents/customer-success-manager.md +460 -0
- package/.claude/agents/data-consolidation-agent.md +60 -0
- package/.claude/agents/data-privacy-officer.md +412 -0
- package/.claude/agents/design-brand-guardian.md +322 -0
- package/.claude/agents/design-image-prompt-engineer.md +236 -0
- package/.claude/agents/design-inclusive-visuals-specialist.md +73 -0
- package/.claude/agents/design-persona-walkthrough.md +272 -0
- package/.claude/agents/design-ui-designer.md +386 -0
- package/.claude/agents/design-ui-finish-gate-reviewer.md +217 -0
- package/.claude/agents/design-ux-architect.md +482 -0
- package/.claude/agents/design-ux-researcher.md +329 -0
- package/.claude/agents/design-visual-storyteller.md +149 -0
- package/.claude/agents/design-whimsy-injector.md +460 -0
- package/.claude/agents/economy-designer.md +156 -0
- package/.claude/agents/engineering-ai-data-remediation-engineer.md +211 -0
- package/.claude/agents/engineering-ai-engineer.md +146 -0
- package/.claude/agents/engineering-api-platform-engineer.md +181 -0
- package/.claude/agents/engineering-ats-validator-architect.md +383 -0
- package/.claude/agents/engineering-autonomous-optimization-architect.md +121 -0
- package/.claude/agents/engineering-backend-architect.md +255 -0
- package/.claude/agents/engineering-china-network-engineer.md +252 -0
- package/.claude/agents/engineering-cms-developer.md +536 -0
- package/.claude/agents/engineering-code-reviewer.md +76 -0
- package/.claude/agents/engineering-codebase-onboarding-engineer.md +173 -0
- package/.claude/agents/engineering-data-engineer.md +349 -0
- package/.claude/agents/engineering-data-visualization-engineer.md +151 -0
- package/.claude/agents/engineering-database-optimizer.md +188 -0
- package/.claude/agents/engineering-database-reliability-engineer.md +201 -0
- package/.claude/agents/engineering-desktop-app-engineer.md +209 -0
- package/.claude/agents/engineering-developer-tooling-engineer.md +153 -0
- package/.claude/agents/engineering-devops-automator.md +385 -0
- package/.claude/agents/engineering-drupal-performance.md +347 -0
- package/.claude/agents/engineering-drupal-shopping-cart.md +360 -0
- package/.claude/agents/engineering-electrical-engineer.md +575 -0
- package/.claude/agents/engineering-email-intelligence-engineer.md +390 -0
- package/.claude/agents/engineering-embedded-firmware-engineer.md +196 -0
- package/.claude/agents/engineering-feishu-integration-developer.md +606 -0
- package/.claude/agents/engineering-filament-optimization-specialist.md +283 -0
- package/.claude/agents/engineering-finops-engineer.md +164 -0
- package/.claude/agents/engineering-frontend-developer.md +257 -0
- package/.claude/agents/engineering-gaussdb-expert.md +334 -0
- package/.claude/agents/engineering-git-workflow-master.md +98 -0
- package/.claude/agents/engineering-i18n-engineer.md +214 -0
- package/.claude/agents/engineering-identity-access-engineer.md +217 -0
- package/.claude/agents/engineering-incident-response-commander.md +444 -0
- package/.claude/agents/engineering-iot-fleet-engineer.md +148 -0
- package/.claude/agents/engineering-it-service-manager.md +561 -0
- package/.claude/agents/engineering-knowledge-graph-engineer.md +372 -0
- package/.claude/agents/engineering-llm-post-training-engineer.md +166 -0
- package/.claude/agents/engineering-minimal-change-engineer.md +207 -0
- package/.claude/agents/engineering-mobile-app-builder.md +493 -0
- package/.claude/agents/engineering-mobile-release-engineer.md +164 -0
- package/.claude/agents/engineering-multi-agent-systems-architect.md +600 -0
- package/.claude/agents/engineering-network-engineer.md +245 -0
- package/.claude/agents/engineering-orgscript-engineer.md +113 -0
- package/.claude/agents/engineering-payments-billing-engineer.md +228 -0
- package/.claude/agents/engineering-pdf-engine-architect.md +696 -0
- package/.claude/agents/engineering-platform-engineer.md +270 -0
- package/.claude/agents/engineering-privacy-engineer.md +152 -0
- package/.claude/agents/engineering-prompt-engineer.md +209 -0
- package/.claude/agents/engineering-rag-pipeline-engineer.md +450 -0
- package/.claude/agents/engineering-rapid-prototyper.md +466 -0
- package/.claude/agents/engineering-realtime-collaboration-engineer.md +200 -0
- package/.claude/agents/engineering-rust-refactoring-specialist.md +313 -0
- package/.claude/agents/engineering-search-relevance-engineer.md +237 -0
- package/.claude/agents/engineering-section-508-specialist.md +339 -0
- package/.claude/agents/engineering-senior-developer.md +176 -0
- package/.claude/agents/engineering-servicenow-developer-mentor.md +180 -0
- package/.claude/agents/engineering-software-architect.md +112 -0
- package/.claude/agents/engineering-solidity-smart-contract-engineer.md +522 -0
- package/.claude/agents/engineering-sre.md +90 -0
- package/.claude/agents/engineering-technical-writer.md +393 -0
- package/.claude/agents/engineering-universal-document-compiler.md +370 -0
- package/.claude/agents/engineering-uswds-developer.md +340 -0
- package/.claude/agents/engineering-video-streaming-engineer.md +151 -0
- package/.claude/agents/engineering-voice-ai-integration-engineer.md +584 -0
- package/.claude/agents/engineering-webassembly-engineer.md +153 -0
- package/.claude/agents/engineering-wechat-mini-program-developer.md +356 -0
- package/.claude/agents/engineering-wordpress-performance.md +346 -0
- package/.claude/agents/engineering-wordpress-shopping-cart.md +346 -0
- package/.claude/agents/esg-sustainability-officer.md +396 -0
- package/.claude/agents/finance-bookkeeper-controller.md +260 -0
- package/.claude/agents/finance-financial-analyst.md +234 -0
- package/.claude/agents/finance-fpa-analyst.md +263 -0
- package/.claude/agents/finance-investment-researcher.md +272 -0
- package/.claude/agents/finance-tax-strategist.md +239 -0
- package/.claude/agents/game-audio-engineer.md +264 -0
- package/.claude/agents/game-designer.md +167 -0
- package/.claude/agents/gis-3d-scene-developer.md +111 -0
- package/.claude/agents/gis-analyst.md +91 -0
- package/.claude/agents/gis-bim-specialist.md +108 -0
- package/.claude/agents/gis-cartography-designer.md +150 -0
- package/.claude/agents/gis-drone-reality-mapping.md +120 -0
- package/.claude/agents/gis-geoai-ml-engineer.md +105 -0
- package/.claude/agents/gis-geoprocessing-specialist.md +97 -0
- package/.claude/agents/gis-qa-engineer.md +133 -0
- package/.claude/agents/gis-solution-engineer.md +101 -0
- package/.claude/agents/gis-spatial-data-engineer.md +97 -0
- package/.claude/agents/gis-spatial-data-scientist.md +111 -0
- package/.claude/agents/gis-technical-consultant.md +86 -0
- package/.claude/agents/gis-web-gis-developer.md +108 -0
- package/.claude/agents/godot-gameplay-scripter.md +338 -0
- package/.claude/agents/godot-multiplayer-engineer.md +301 -0
- package/.claude/agents/godot-shader-developer.md +267 -0
- package/.claude/agents/government-digital-presales-consultant.md +363 -0
- package/.claude/agents/grant-writer.md +511 -0
- package/.claude/agents/healthcare-aging-parent-care-companion.md +414 -0
- package/.claude/agents/healthcare-clinical-evidence-agent.md +231 -0
- package/.claude/agents/healthcare-customer-service.md +389 -0
- package/.claude/agents/healthcare-innovation-strategist.md +433 -0
- package/.claude/agents/healthcare-marketing-compliance.md +395 -0
- package/.claude/agents/healthcare-sovereign-health-systems-agent.md +312 -0
- package/.claude/agents/hospitality-guest-services.md +603 -0
- package/.claude/agents/hr-onboarding.md +451 -0
- package/.claude/agents/identity-graph-operator.md +303 -0
- package/.claude/agents/language-translator.md +264 -0
- package/.claude/agents/legal-billing-time-tracking.md +569 -0
- package/.claude/agents/legal-client-intake.md +492 -0
- package/.claude/agents/legal-document-review.md +454 -0
- package/.claude/agents/level-designer.md +208 -0
- package/.claude/agents/loan-officer-assistant.md +555 -0
- package/.claude/agents/lsp-index-engineer.md +320 -0
- package/.claude/agents/ma-integration-manager.md +427 -0
- package/.claude/agents/macos-spatial-metal-engineer.md +337 -0
- package/.claude/agents/marketing-aeo-foundations.md +264 -0
- package/.claude/agents/marketing-agentic-search-optimizer.md +279 -0
- package/.claude/agents/marketing-ai-citation-strategist.md +172 -0
- package/.claude/agents/marketing-app-store-optimizer.md +321 -0
- package/.claude/agents/marketing-baidu-seo-specialist.md +226 -0
- package/.claude/agents/marketing-bilibili-content-strategist.md +199 -0
- package/.claude/agents/marketing-book-co-author.md +110 -0
- package/.claude/agents/marketing-carousel-growth-engine.md +204 -0
- package/.claude/agents/marketing-china-ecommerce-operator.md +283 -0
- package/.claude/agents/marketing-china-market-localization-strategist.md +283 -0
- package/.claude/agents/marketing-content-creator.md +54 -0
- package/.claude/agents/marketing-cross-border-ecommerce.md +259 -0
- package/.claude/agents/marketing-developer-community-builder.md +285 -0
- package/.claude/agents/marketing-douyin-strategist.md +149 -0
- package/.claude/agents/marketing-email-strategist.md +249 -0
- package/.claude/agents/marketing-global-podcast-strategist.md +206 -0
- package/.claude/agents/marketing-growth-hacker.md +54 -0
- package/.claude/agents/marketing-instagram-curator.md +113 -0
- package/.claude/agents/marketing-kuaishou-strategist.md +223 -0
- package/.claude/agents/marketing-linkedin-content-creator.md +214 -0
- package/.claude/agents/marketing-livestream-commerce-coach.md +305 -0
- package/.claude/agents/marketing-multi-platform-publisher.md +217 -0
- package/.claude/agents/marketing-podcast-strategist.md +277 -0
- package/.claude/agents/marketing-pr-communications-manager.md +473 -0
- package/.claude/agents/marketing-private-domain-operator.md +308 -0
- package/.claude/agents/marketing-reddit-community-builder.md +123 -0
- package/.claude/agents/marketing-seo-specialist.md +370 -0
- package/.claude/agents/marketing-short-video-editing-coach.md +412 -0
- package/.claude/agents/marketing-social-media-strategist.md +125 -0
- package/.claude/agents/marketing-tiktok-strategist.md +125 -0
- package/.claude/agents/marketing-twitter-engager.md +126 -0
- package/.claude/agents/marketing-video-optimization-specialist.md +119 -0
- package/.claude/agents/marketing-wechat-official-account.md +145 -0
- package/.claude/agents/marketing-weibo-strategist.md +240 -0
- package/.claude/agents/marketing-x-twitter-intelligence-analyst.md +161 -0
- package/.claude/agents/marketing-xiaohongshu-specialist.md +138 -0
- package/.claude/agents/marketing-zhihu-strategist.md +162 -0
- package/.claude/agents/medical-billing-coding-specialist.md +491 -0
- package/.claude/agents/narrative-designer.md +243 -0
- package/.claude/agents/operations-manager.md +399 -0
- package/.claude/agents/organizational-psychologist.md +391 -0
- package/.claude/agents/paid-media-auditor.md +71 -0
- package/.claude/agents/paid-media-creative-strategist.md +71 -0
- package/.claude/agents/paid-media-paid-social-strategist.md +71 -0
- package/.claude/agents/paid-media-ppc-strategist.md +71 -0
- package/.claude/agents/paid-media-programmatic-buyer.md +71 -0
- package/.claude/agents/paid-media-search-query-analyst.md +71 -0
- package/.claude/agents/paid-media-tracking-specialist.md +71 -0
- package/.claude/agents/personal-growth-mentor.md +159 -0
- package/.claude/agents/product-behavioral-nudge-engine.md +83 -0
- package/.claude/agents/product-dx-engineer.md +332 -0
- package/.claude/agents/product-feedback-synthesizer.md +119 -0
- package/.claude/agents/product-manager.md +469 -0
- package/.claude/agents/product-sprint-prioritizer.md +154 -0
- package/.claude/agents/product-trend-researcher.md +159 -0
- package/.claude/agents/project-management-experiment-tracker.md +198 -0
- package/.claude/agents/project-management-jira-workflow-steward.md +233 -0
- package/.claude/agents/project-management-meeting-notes-specialist.md +95 -0
- package/.claude/agents/project-management-project-shepherd.md +194 -0
- package/.claude/agents/project-management-studio-operations.md +200 -0
- package/.claude/agents/project-management-studio-producer.md +203 -0
- package/.claude/agents/project-manager-senior.md +135 -0
- package/.claude/agents/real-estate-buyer-seller.md +596 -0
- package/.claude/agents/recruitment-specialist.md +515 -0
- package/.claude/agents/report-distribution-agent.md +65 -0
- package/.claude/agents/research-synthesist.md +136 -0
- package/.claude/agents/resume-tailor.md +230 -0
- package/.claude/agents/retail-customer-returns.md +566 -0
- package/.claude/agents/roblox-avatar-creator.md +297 -0
- package/.claude/agents/roblox-experience-designer.md +306 -0
- package/.claude/agents/roblox-systems-scripter.md +332 -0
- package/.claude/agents/sales-account-strategist.md +227 -0
- package/.claude/agents/sales-coach.md +271 -0
- package/.claude/agents/sales-data-extraction-agent.md +67 -0
- package/.claude/agents/sales-deal-strategist.md +180 -0
- package/.claude/agents/sales-discovery-coach.md +225 -0
- package/.claude/agents/sales-engineer.md +182 -0
- package/.claude/agents/sales-offer-lead-gen-strategist.md +257 -0
- package/.claude/agents/sales-outbound-strategist.md +201 -0
- package/.claude/agents/sales-outreach.md +425 -0
- package/.claude/agents/sales-pipeline-analyst.md +267 -0
- package/.claude/agents/sales-proposal-strategist.md +217 -0
- package/.claude/agents/security-ai-generated-code-auditor.md +207 -0
- package/.claude/agents/security-appsec-engineer.md +536 -0
- package/.claude/agents/security-architect.md +307 -0
- package/.claude/agents/security-blockchain-security-auditor.md +482 -0
- package/.claude/agents/security-cloud-security-architect.md +576 -0
- package/.claude/agents/security-compliance-auditor.md +158 -0
- package/.claude/agents/security-incident-responder.md +450 -0
- package/.claude/agents/security-penetration-tester.md +399 -0
- package/.claude/agents/security-secrets-credential-engineer.md +189 -0
- package/.claude/agents/security-senior-secops.md +760 -0
- package/.claude/agents/security-threat-detection-engineer.md +553 -0
- package/.claude/agents/security-threat-intelligence-analyst.md +661 -0
- package/.claude/agents/specialized-chief-of-staff.md +279 -0
- package/.claude/agents/specialized-civil-engineer.md +356 -0
- package/.claude/agents/specialized-codebase-archaeologist.md +341 -0
- package/.claude/agents/specialized-cultural-intelligence-strategist.md +88 -0
- package/.claude/agents/specialized-developer-advocate.md +317 -0
- package/.claude/agents/specialized-document-generator.md +55 -0
- package/.claude/agents/specialized-fedramp-rmf-compliance.md +378 -0
- package/.claude/agents/specialized-focus-music-architect.md +175 -0
- package/.claude/agents/specialized-french-consulting-market.md +194 -0
- package/.claude/agents/specialized-korean-business-navigator.md +216 -0
- package/.claude/agents/specialized-master-plan-architect.md +157 -0
- package/.claude/agents/specialized-mcp-builder.md +260 -0
- package/.claude/agents/specialized-model-qa.md +528 -0
- package/.claude/agents/specialized-pricing-analyst.md +243 -0
- package/.claude/agents/specialized-salesforce-architect.md +182 -0
- package/.claude/agents/specialized-strategy-duel-agent.md +130 -0
- package/.claude/agents/specialized-workflow-architect.md +597 -0
- package/.claude/agents/study-abroad-advisor.md +282 -0
- package/.claude/agents/supply-chain-strategist.md +591 -0
- package/.claude/agents/support-analytics-reporter.md +387 -0
- package/.claude/agents/support-executive-summary-generator.md +212 -0
- package/.claude/agents/support-finance-tracker.md +480 -0
- package/.claude/agents/support-infrastructure-maintainer.md +628 -0
- package/.claude/agents/support-legal-compliance-checker.md +591 -0
- package/.claude/agents/support-support-responder.md +607 -0
- package/.claude/agents/technical-artist.md +233 -0
- package/.claude/agents/terminal-integration-specialist.md +70 -0
- package/.claude/agents/testing-accessibility-auditor.md +327 -0
- package/.claude/agents/testing-api-tester.md +334 -0
- package/.claude/agents/testing-evidence-collector.md +210 -0
- package/.claude/agents/testing-performance-benchmarker.md +283 -0
- package/.claude/agents/testing-reality-checker.md +249 -0
- package/.claude/agents/testing-test-automation-engineer.md +196 -0
- package/.claude/agents/testing-test-results-analyzer.md +330 -0
- package/.claude/agents/testing-tool-evaluator.md +416 -0
- package/.claude/agents/testing-workflow-optimizer.md +463 -0
- package/.claude/agents/unity-architect.md +271 -0
- package/.claude/agents/unity-editor-tool-developer.md +310 -0
- package/.claude/agents/unity-multiplayer-engineer.md +323 -0
- package/.claude/agents/unity-shader-graph-artist.md +269 -0
- package/.claude/agents/unreal-multiplayer-architect.md +364 -0
- package/.claude/agents/unreal-systems-engineer.md +322 -0
- package/.claude/agents/unreal-technical-artist.md +256 -0
- package/.claude/agents/unreal-world-builder.md +273 -0
- package/.claude/agents/visionos-spatial-engineer.md +54 -0
- package/.claude/agents/xr-cockpit-interaction-specialist.md +32 -0
- package/.claude/agents/xr-immersive-developer.md +32 -0
- package/.claude/agents/xr-interface-architect.md +32 -0
- package/.claude/agents/zk-steward.md +211 -0
- package/.claude-plugin/marketplace.json +30 -0
- package/.commitlintrc.json +26 -0
- package/.devcontainer/Dockerfile +24 -0
- package/.devcontainer/devcontainer.json +70 -0
- package/.devcontainer/docker-compose.memory.yml +42 -0
- package/.devcontainer/memory-stack/devcontainer.json +84 -0
- package/.devcontainer/post-create.sh +83 -0
- package/.dockerignore +23 -0
- package/.env.act.example +15 -0
- package/.env.example +171 -0
- package/.eslintrc.json +16 -0
- package/.git-blame-ignore-revs +10 -0
- package/.gitattributes +2 -0
- package/.gitguardian.yaml +50 -0
- package/.githooks/pre-commit +6 -0
- package/.pre-commit-config.yaml +30 -0
- package/.release-please-config-xithead.json +22 -0
- package/.release-please-config.json +44 -0
- package/.release-please-manifest-xithead.json +3 -0
- package/.release-please-manifest.json +3 -0
- package/CODE_OF_CONDUCT.md +133 -0
- package/CONTRIBUTING.md +133 -0
- package/Cargo.lock +5557 -0
- package/Cargo.toml +121 -0
- package/Dockerfile +38 -0
- package/Dockerfile.proxy +48 -0
- package/ENTERPRISE.md +5 -0
- package/FEATURES_ADVANCED.md +311 -0
- package/Headroom-2.gif +0 -0
- package/HeadroomDemo-Fast.gif +0 -0
- package/MANIFEST.in +4 -0
- package/MODERNIZATION_GUIDE.md +358 -0
- package/Makefile +140 -0
- package/NOTICE +43 -0
- package/NPM_TOKEN_SETUP.md +81 -0
- package/PR.md +170 -0
- package/PUBLISH.md +77 -0
- package/README.md +58 -137
- package/README_FRONTEND.md +196 -0
- package/REALIGNMENT/00-overview.md +53 -0
- package/REALIGNMENT/01-bug-list.md +421 -0
- package/REALIGNMENT/02-architecture.md +322 -0
- package/REALIGNMENT/03-phase-A-lockdown.md +420 -0
- package/REALIGNMENT/04-phase-B-live-zone.md +470 -0
- package/REALIGNMENT/05-phase-C-rust-proxy.md +329 -0
- package/REALIGNMENT/06-phase-D-bedrock-vertex.md +222 -0
- package/REALIGNMENT/07-phase-E-cache-stabilization.md +338 -0
- package/REALIGNMENT/08-phase-F-auth-mode.md +254 -0
- package/REALIGNMENT/09-phase-G-rtk-observability.md +194 -0
- package/REALIGNMENT/10-phase-H-python-retirement.md +214 -0
- package/REALIGNMENT/11-phase-I-test-infra.md +385 -0
- package/REALIGNMENT/12-decisions-needed.md +196 -0
- package/REALIGNMENT/INDEX.md +82 -0
- package/REFACTORING_CAMPAIGN.md +277 -0
- package/RESTRUCTURE_PLAN.md +100 -0
- package/RUST_DEV.md +361 -0
- package/SECURITY.md +65 -0
- package/SETUP_AUTO_PUBLISH.md +164 -0
- package/TESTING-copilot-subscription.md +153 -0
- package/app/api/config/route.ts +22 -0
- package/app/api/export/route.ts +83 -0
- package/app/api/models/route.ts +12 -0
- package/app/api/stats/route.ts +46 -0
- package/app/dashboard/page.tsx +445 -0
- package/app/globals.css +35 -0
- package/app/installation/page.tsx +293 -0
- package/app/layout.tsx +35 -0
- package/app/master-design-system.css +2076 -0
- package/app/master-design-system.source.css +8 -0
- package/app/page.tsx +187 -0
- package/app/rechtliches/agb/page.tsx +165 -0
- package/app/rechtliches/datenschutz/page.tsx +128 -0
- package/app/rechtliches/impressum/page.tsx +122 -0
- package/app/rechtliches/widerruf/page.tsx +185 -0
- package/bin/xithead.js +264 -0
- package/codecov.yml +34 -0
- package/components/layout/AppShell.tsx +25 -0
- package/components/layout/Footer.tsx +120 -0
- package/components/layout/Header.tsx +145 -0
- package/components/marketing/LegalHighlights.tsx +45 -0
- package/components/marketing/LegalPageShell.tsx +49 -0
- package/components/marketing/LegalPanel.tsx +41 -0
- package/components/marketing/index.ts +3 -0
- package/crates/headroom-core/Cargo.toml +165 -0
- package/crates/headroom-core/benches/auth_mode.rs +74 -0
- package/crates/headroom-core/benches/ccr_store.rs +224 -0
- package/crates/headroom-core/benches/tokenizer.rs +52 -0
- package/crates/headroom-core/config/pipeline.toml +151 -0
- package/crates/headroom-core/proptest-regressions/transforms/tag_protector.txt +9 -0
- package/crates/headroom-core/src/auth_mode.rs +250 -0
- package/crates/headroom-core/src/cache_control.rs +414 -0
- package/crates/headroom-core/src/ccr/backends/in_memory.rs +317 -0
- package/crates/headroom-core/src/ccr/backends/mod.rs +152 -0
- package/crates/headroom-core/src/ccr/backends/redis.rs +146 -0
- package/crates/headroom-core/src/ccr/backends/sqlite.rs +205 -0
- package/crates/headroom-core/src/ccr/mod.rs +119 -0
- package/crates/headroom-core/src/compression_policy.rs +505 -0
- package/crates/headroom-core/src/lib.rs +33 -0
- package/crates/headroom-core/src/relevance/base.rs +139 -0
- package/crates/headroom-core/src/relevance/bm25.rs +385 -0
- package/crates/headroom-core/src/relevance/embedding.rs +354 -0
- package/crates/headroom-core/src/relevance/hybrid.rs +364 -0
- package/crates/headroom-core/src/relevance/mod.rs +64 -0
- package/crates/headroom-core/src/signals/keyword_detector.rs +433 -0
- package/crates/headroom-core/src/signals/line_importance.rs +84 -0
- package/crates/headroom-core/src/signals/mod.rs +61 -0
- package/crates/headroom-core/src/signals/tiered.rs +141 -0
- package/crates/headroom-core/src/tokenizer/estimator.rs +152 -0
- package/crates/headroom-core/src/tokenizer/hf_impl.rs +314 -0
- package/crates/headroom-core/src/tokenizer/mod.rs +56 -0
- package/crates/headroom-core/src/tokenizer/registry.rs +342 -0
- package/crates/headroom-core/src/tokenizer/tiktoken_impl.rs +265 -0
- package/crates/headroom-core/src/transforms/adaptive_sizer.rs +610 -0
- package/crates/headroom-core/src/transforms/anchor_selector.rs +1189 -0
- package/crates/headroom-core/src/transforms/content_detector.rs +769 -0
- package/crates/headroom-core/src/transforms/detection.rs +222 -0
- package/crates/headroom-core/src/transforms/diff_compressor.rs +1685 -0
- package/crates/headroom-core/src/transforms/live_zone.rs +2967 -0
- package/crates/headroom-core/src/transforms/log_compressor.rs +1295 -0
- package/crates/headroom-core/src/transforms/magika_detector.rs +395 -0
- package/crates/headroom-core/src/transforms/mod.rs +64 -0
- package/crates/headroom-core/src/transforms/pipeline/config.rs +331 -0
- package/crates/headroom-core/src/transforms/pipeline/mod.rs +103 -0
- package/crates/headroom-core/src/transforms/pipeline/offloads/diff_noise.rs +524 -0
- package/crates/headroom-core/src/transforms/pipeline/offloads/diff_offload.rs +279 -0
- package/crates/headroom-core/src/transforms/pipeline/offloads/json_offload.rs +400 -0
- package/crates/headroom-core/src/transforms/pipeline/offloads/log_offload.rs +284 -0
- package/crates/headroom-core/src/transforms/pipeline/offloads/mod.rs +34 -0
- package/crates/headroom-core/src/transforms/pipeline/offloads/search_offload.rs +331 -0
- package/crates/headroom-core/src/transforms/pipeline/orchestrator.rs +848 -0
- package/crates/headroom-core/src/transforms/pipeline/reformats/json_minifier.rs +175 -0
- package/crates/headroom-core/src/transforms/pipeline/reformats/log_template.rs +545 -0
- package/crates/headroom-core/src/transforms/pipeline/reformats/mod.rs +12 -0
- package/crates/headroom-core/src/transforms/pipeline/traits.rs +338 -0
- package/crates/headroom-core/src/transforms/recommendations.rs +329 -0
- package/crates/headroom-core/src/transforms/safety.rs +215 -0
- package/crates/headroom-core/src/transforms/search_compressor.rs +902 -0
- package/crates/headroom-core/src/transforms/smart_crusher/analyzer.rs +1262 -0
- package/crates/headroom-core/src/transforms/smart_crusher/anchors.rs +407 -0
- package/crates/headroom-core/src/transforms/smart_crusher/builder.rs +263 -0
- package/crates/headroom-core/src/transforms/smart_crusher/classifier.rs +214 -0
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/classifier.rs +283 -0
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/compactor.rs +733 -0
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/formatter.rs +961 -0
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/ir.rs +252 -0
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/mod.rs +127 -0
- package/crates/headroom-core/src/transforms/smart_crusher/compaction/walker.rs +397 -0
- package/crates/headroom-core/src/transforms/smart_crusher/config.rs +173 -0
- package/crates/headroom-core/src/transforms/smart_crusher/constraints.rs +159 -0
- package/crates/headroom-core/src/transforms/smart_crusher/crusher.rs +1665 -0
- package/crates/headroom-core/src/transforms/smart_crusher/crushers.rs +865 -0
- package/crates/headroom-core/src/transforms/smart_crusher/error_keywords.rs +77 -0
- package/crates/headroom-core/src/transforms/smart_crusher/field_detect.rs +393 -0
- package/crates/headroom-core/src/transforms/smart_crusher/hashing.rs +72 -0
- package/crates/headroom-core/src/transforms/smart_crusher/mod.rs +81 -0
- package/crates/headroom-core/src/transforms/smart_crusher/observer.rs +66 -0
- package/crates/headroom-core/src/transforms/smart_crusher/orchestration.rs +459 -0
- package/crates/headroom-core/src/transforms/smart_crusher/outliers.rs +507 -0
- package/crates/headroom-core/src/transforms/smart_crusher/planning.rs +904 -0
- package/crates/headroom-core/src/transforms/smart_crusher/statistics.rs +510 -0
- package/crates/headroom-core/src/transforms/smart_crusher/stats_math.rs +287 -0
- package/crates/headroom-core/src/transforms/smart_crusher/traits.rs +203 -0
- package/crates/headroom-core/src/transforms/smart_crusher/types.rs +263 -0
- package/crates/headroom-core/src/transforms/tag_protector.rs +1272 -0
- package/crates/headroom-core/src/transforms/unidiff_detector.rs +232 -0
- package/crates/headroom-parity/Cargo.toml +26 -0
- package/crates/headroom-parity/examples/diff_fixture.rs +66 -0
- package/crates/headroom-parity/src/bin/parity_run.rs +78 -0
- package/crates/headroom-parity/src/lib.rs +613 -0
- package/crates/headroom-proxy/Cargo.toml +119 -0
- package/crates/headroom-proxy/data/model_prices_and_context_window.json +39767 -0
- package/crates/headroom-proxy/src/bedrock/auth_mode_layer.rs +192 -0
- package/crates/headroom-proxy/src/bedrock/envelope.rs +239 -0
- package/crates/headroom-proxy/src/bedrock/eventstream.rs +769 -0
- package/crates/headroom-proxy/src/bedrock/eventstream_to_sse.rs +459 -0
- package/crates/headroom-proxy/src/bedrock/invoke.rs +735 -0
- package/crates/headroom-proxy/src/bedrock/invoke_streaming.rs +1098 -0
- package/crates/headroom-proxy/src/bedrock/mod.rs +66 -0
- package/crates/headroom-proxy/src/bedrock/sigv4.rs +324 -0
- package/crates/headroom-proxy/src/bedrock/vendor.rs +82 -0
- package/crates/headroom-proxy/src/cache_stabilization/anthropic_cache_control.rs +698 -0
- package/crates/headroom-proxy/src/cache_stabilization/drift_detector.rs +683 -0
- package/crates/headroom-proxy/src/cache_stabilization/mod.rs +61 -0
- package/crates/headroom-proxy/src/cache_stabilization/openai_cache_key.rs +600 -0
- package/crates/headroom-proxy/src/cache_stabilization/tool_def_normalize.rs +514 -0
- package/crates/headroom-proxy/src/cache_stabilization/volatile_detector.rs +720 -0
- package/crates/headroom-proxy/src/compression/anthropic.rs +92 -0
- package/crates/headroom-proxy/src/compression/live_zone_anthropic.rs +1311 -0
- package/crates/headroom-proxy/src/compression/live_zone_openai.rs +660 -0
- package/crates/headroom-proxy/src/compression/live_zone_responses.rs +678 -0
- package/crates/headroom-proxy/src/compression/mod.rs +127 -0
- package/crates/headroom-proxy/src/compression/model_limits.rs +211 -0
- package/crates/headroom-proxy/src/config.rs +646 -0
- package/crates/headroom-proxy/src/error.rs +69 -0
- package/crates/headroom-proxy/src/handlers/chat_completions.rs +100 -0
- package/crates/headroom-proxy/src/handlers/conversations.rs +241 -0
- package/crates/headroom-proxy/src/handlers/mod.rs +13 -0
- package/crates/headroom-proxy/src/handlers/responses.rs +256 -0
- package/crates/headroom-proxy/src/headers.rs +288 -0
- package/crates/headroom-proxy/src/health.rs +41 -0
- package/crates/headroom-proxy/src/lib.rs +21 -0
- package/crates/headroom-proxy/src/main.rs +141 -0
- package/crates/headroom-proxy/src/observability/cache_hit_rate.rs +340 -0
- package/crates/headroom-proxy/src/observability/compression_ratio.rs +158 -0
- package/crates/headroom-proxy/src/observability/metric_names.rs +174 -0
- package/crates/headroom-proxy/src/observability/mod.rs +68 -0
- package/crates/headroom-proxy/src/observability/prometheus.rs +394 -0
- package/crates/headroom-proxy/src/observability/proxy_metrics.rs +368 -0
- package/crates/headroom-proxy/src/proxy.rs +1619 -0
- package/crates/headroom-proxy/src/responses_items.rs +565 -0
- package/crates/headroom-proxy/src/sse/anthropic.rs +415 -0
- package/crates/headroom-proxy/src/sse/framing.rs +366 -0
- package/crates/headroom-proxy/src/sse/mod.rs +40 -0
- package/crates/headroom-proxy/src/sse/openai_chat.rs +231 -0
- package/crates/headroom-proxy/src/sse/openai_responses.rs +458 -0
- package/crates/headroom-proxy/src/vertex/adc.rs +315 -0
- package/crates/headroom-proxy/src/vertex/envelope.rs +167 -0
- package/crates/headroom-proxy/src/vertex/mod.rs +277 -0
- package/crates/headroom-proxy/src/vertex/raw_predict.rs +490 -0
- package/crates/headroom-proxy/src/vertex/stream_raw_predict.rs +49 -0
- package/crates/headroom-proxy/src/websocket.rs +246 -0
- package/crates/headroom-py/Cargo.toml +37 -0
- package/crates/headroom-py/build.rs +77 -0
- package/crates/headroom-py/glibc_compat.c +170 -0
- package/crates/headroom-py/src/lib.rs +1655 -0
- package/deny.toml +32 -0
- package/design-system/base.css +127 -0
- package/design-system/components.css +1943 -0
- package/design-system/fonts.css +17 -0
- package/design-system/tailwind-preset.js +95 -0
- package/design-system/tokens.css +48 -0
- package/design-system/utilities.css +53 -0
- package/docker/differential-network-capture/Dockerfile.runner +14 -0
- package/docker/differential-network-capture/docker-compose.yml +142 -0
- package/docker/differential-network-capture/run-claude-lane.sh +24 -0
- package/docker/docker-compose.native.yml +44 -0
- package/docker-bake.hcl +88 -0
- package/docker-compose.yml +80 -0
- package/e2e/_lib/make_shim.ps1 +24 -0
- package/e2e/_lib/make_shim.sh +28 -0
- package/e2e/docker-native-install.sh +120 -0
- package/e2e/init/Dockerfile +79 -0
- package/e2e/wrap/Dockerfile +115 -0
- package/examples/07-context-compression.ipynb +279 -0
- package/examples/deployment/macos-launchagent/com.headroom.proxy.plist.template +65 -0
- package/examples/deployment/macos-launchagent/install.sh +227 -0
- package/examples/deployment/macos-launchagent/shell-integration.sh +74 -0
- package/examples/deployment/macos-launchagent/uninstall.sh +145 -0
- package/headroom-savings.png +0 -0
- package/headroom_learn.gif +0 -0
- package/lib/config/index.ts +12 -0
- package/lib/hooks/index.ts +4 -0
- package/lib/hooks/use-mounted.ts +13 -0
- package/lib/hooks/use-session.ts +34 -0
- package/lib/hooks/use-settings.ts +49 -0
- package/lib/utils/index.ts +1 -0
- package/lib/utils/settings.ts +162 -0
- package/lib/utils/utils.ts +6 -0
- package/llms.txt +67 -0
- package/middleware.ts +26 -0
- package/mkdocs.yml +136 -0
- package/next-env.d.ts +7 -0
- package/next.config.ts +12 -0
- package/npm/bin/xithead.js +2 -2
- package/npm/bin/xtream-head.js +366 -0
- package/npm/index.js +18 -91
- package/npm/package.json +55 -0
- package/package.json +47 -42
- package/plugins/headroom-agent-hooks/.claude-plugin/plugin.json +17 -0
- package/plugins/headroom-agent-hooks/hooks/hooks.json +29 -0
- package/plugins/headroom-oauth2/LICENSE +3 -0
- package/plugins/headroom-oauth2/SPEC.md +115 -0
- package/plugins/headroom-oauth2/pyproject.toml +48 -0
- package/plugins/hermes/headroom_retrieve/plugin.yaml +6 -0
- package/plugins/openclaw/hook-shim/HOOK.md +21 -0
- package/plugins/openclaw/hook-shim/handler.js +3 -0
- package/plugins/openclaw/openclaw.plugin.json +98 -0
- package/plugins/openclaw/package-lock.json +2692 -0
- package/plugins/openclaw/package.json +54 -0
- package/plugins/openclaw/prepare-dist.mjs +56 -0
- package/plugins/openclaw/src/convert.ts +418 -0
- package/plugins/openclaw/src/engine.ts +284 -0
- package/plugins/openclaw/src/gateway-config.ts +143 -0
- package/plugins/openclaw/src/index.ts +11 -0
- package/plugins/openclaw/src/plugin/index.ts +98 -0
- package/plugins/openclaw/src/proxy-manager.ts +477 -0
- package/plugins/openclaw/src/tools/headroom-retrieve.ts +76 -0
- package/plugins/openclaw/test/convert.test.ts +96 -0
- package/plugins/openclaw/test/engine-normalization.test.ts +21 -0
- package/plugins/openclaw/test/engine.test.ts +101 -0
- package/plugins/openclaw/test/gateway-config.test.ts +278 -0
- package/plugins/openclaw/test/plugin-runtime-routing.test.ts +141 -0
- package/plugins/openclaw/test/proxy-manager.test.ts +343 -0
- package/plugins/openclaw/tsup.config.ts +9 -0
- package/postcss.config.mjs +7 -0
- package/pyproject.toml +438 -0
- package/rust-toolchain.toml +16 -0
- package/sdk/typescript/examples/basic-compress.ts +64 -0
- package/sdk/typescript/examples/ccr-retrieve.ts +80 -0
- package/sdk/typescript/examples/hooks-custom-compression.ts +117 -0
- package/sdk/typescript/examples/middleware-composition.ts +59 -0
- package/sdk/typescript/examples/multi-provider.ts +53 -0
- package/sdk/typescript/examples/openai-anthropic-adapters.ts +73 -0
- package/sdk/typescript/examples/shared-context-multi-agent.ts +74 -0
- package/sdk/typescript/examples/simulation-dry-run.ts +80 -0
- package/sdk/typescript/examples/streaming-chat.ts +71 -0
- package/sdk/typescript/examples/structured-output.ts +99 -0
- package/sdk/typescript/examples/tool-calling-agent.ts +86 -0
- package/sdk/typescript/examples/with-headroom-vercel.ts +57 -0
- package/sdk/typescript/package-lock.json +3437 -0
- package/sdk/typescript/package.json +108 -0
- package/sdk/typescript/src/adapters/anthropic.ts +218 -0
- package/sdk/typescript/src/adapters/gemini.ts +83 -0
- package/sdk/typescript/src/adapters/openai.ts +77 -0
- package/sdk/typescript/src/adapters/vercel-ai.ts +131 -0
- package/sdk/typescript/src/client.ts +632 -0
- package/sdk/typescript/src/compress.ts +87 -0
- package/sdk/typescript/src/errors.ts +114 -0
- package/sdk/typescript/src/hooks.ts +113 -0
- package/sdk/typescript/src/index.ts +140 -0
- package/sdk/typescript/src/paths.ts +272 -0
- package/sdk/typescript/src/shared-context.ts +188 -0
- package/sdk/typescript/src/simulate.ts +52 -0
- package/sdk/typescript/src/types/config.ts +183 -0
- package/sdk/typescript/src/types/models.ts +314 -0
- package/sdk/typescript/src/types.ts +139 -0
- package/sdk/typescript/src/utils/case.ts +34 -0
- package/sdk/typescript/src/utils/format.ts +480 -0
- package/sdk/typescript/src/utils/stream.ts +50 -0
- package/sdk/typescript/test/adapters/anthropic.test.ts +217 -0
- package/sdk/typescript/test/adapters/gemini.test.ts +131 -0
- package/sdk/typescript/test/adapters/openai.test.ts +174 -0
- package/sdk/typescript/test/adapters/vercel-ai.test.ts +306 -0
- package/sdk/typescript/test/client-expanded.test.ts +333 -0
- package/sdk/typescript/test/client.test.ts +266 -0
- package/sdk/typescript/test/compress.test.ts +118 -0
- package/sdk/typescript/test/config.test.ts +275 -0
- package/sdk/typescript/test/errors.test.ts +169 -0
- package/sdk/typescript/test/format-integration.test.ts +252 -0
- package/sdk/typescript/test/hooks.test.ts +265 -0
- package/sdk/typescript/test/integration.test.ts +249 -0
- package/sdk/typescript/test/mdx-examples.test.ts +253 -0
- package/sdk/typescript/test/models.test.ts +318 -0
- package/sdk/typescript/test/parity.test.ts +337 -0
- package/sdk/typescript/test/paths.test.ts +447 -0
- package/sdk/typescript/test/shared-context.test.ts +245 -0
- package/sdk/typescript/test/simulate.test.ts +84 -0
- package/sdk/typescript/test/types.test.ts +244 -0
- package/sdk/typescript/test/utils/case.test.ts +129 -0
- package/sdk/typescript/test/utils/detect-format.test.ts +133 -0
- package/sdk/typescript/test/utils/format.test.ts +434 -0
- package/sdk/typescript/test/vercel-ai-e2e.test.ts +179 -0
- package/sdk/typescript/tsup.config.ts +17 -0
- package/tailwind.master.config.cjs +47 -0
- package/types/api.ts +12 -0
- package/types/config.ts +9 -0
- package/types/index.ts +2 -0
- package/uv.lock +6951 -0
- package/xithead.egg-info/PKG-INFO +22 -0
- package/xithead.egg-info/SOURCES.txt +16 -0
- package/xithead.egg-info/dependency_links.txt +1 -0
- package/xithead.egg-info/entry_points.txt +2 -0
- package/xithead.egg-info/requires.txt +10 -0
- package/xithead.egg-info/top_level.txt +2 -0
- package/xithead.ps1 +136 -0
- package/xithead.sh +129 -0
- package/xithead_dist/pyproject.toml +39 -0
- package/xithead_dist/xithead.egg-info/PKG-INFO +22 -0
- package/xithead_dist/xithead.egg-info/SOURCES.txt +16 -0
- package/xithead_dist/xithead.egg-info/dependency_links.txt +1 -0
- package/xithead_dist/xithead.egg-info/entry_points.txt +3 -0
- package/xithead_dist/xithead.egg-info/requires.txt +10 -0
- package/xithead_dist/xithead.egg-info/top_level.txt +2 -0
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
//! Byte-level Server-Sent Events (SSE) framing.
|
|
2
|
+
//!
|
|
3
|
+
//! # Why byte-level
|
|
4
|
+
//!
|
|
5
|
+
//! The Python proxy decoded each TCP chunk to UTF-8 with
|
|
6
|
+
//! `errors="ignore"`, which silently lost bytes whenever a multi-byte
|
|
7
|
+
//! codepoint (any emoji, any non-ASCII character) straddled a chunk
|
|
8
|
+
//! boundary. Production telemetry logged 1946 parse failures over 9
|
|
9
|
+
//! days from this single quirk — see
|
|
10
|
+
//! `~/Desktop/HEADROOM_PROXY_LOG_FINDINGS_2026_05_03.md` (P1-15).
|
|
11
|
+
//!
|
|
12
|
+
//! The Rust framer accumulates raw bytes into a `BytesMut` buffer and
|
|
13
|
+
//! finds event terminators (`\n\n`) in **bytes**. UTF-8 is decoded
|
|
14
|
+
//! exactly once, per complete event, so split-codepoint chunks rejoin
|
|
15
|
+
//! correctly. Per project invariants we never call `from_utf8_lossy`
|
|
16
|
+
//! on partial buffers — the buffer holds bytes until a full event is
|
|
17
|
+
//! framed, then a single `String::from_utf8` decodes the whole thing.
|
|
18
|
+
//!
|
|
19
|
+
//! # Wire format
|
|
20
|
+
//!
|
|
21
|
+
//! Per WHATWG SSE spec + provider extensions:
|
|
22
|
+
//! - Lines end with `\n` (CR is tolerated and stripped, but providers
|
|
23
|
+
//! in practice send LF only).
|
|
24
|
+
//! - An event terminates on a blank line (i.e. consecutive `\n\n`).
|
|
25
|
+
//! - Lines starting with `:` are comments. Anthropic and OpenAI use
|
|
26
|
+
//! `: ping` keepalives; we drop them silently (they convey no data).
|
|
27
|
+
//! - The literal `data: [DONE]` is a stream-end sentinel used by
|
|
28
|
+
//! OpenAI Chat/Responses (Anthropic uses `event: message_stop`
|
|
29
|
+
//! instead). Some providers append a trailing `\n\n` after `[DONE]`;
|
|
30
|
+
//! we tolerate any number of trailing bytes.
|
|
31
|
+
//! - Multiple `data:` lines in one event concatenate with `\n` per
|
|
32
|
+
//! the WHATWG spec. (Anthropic does not currently use this; OpenAI
|
|
33
|
+
//! Responses occasionally does.)
|
|
34
|
+
//!
|
|
35
|
+
//! # Zero-copy where possible
|
|
36
|
+
//!
|
|
37
|
+
//! The framer is built on `bytes::Bytes` which is reference-counted;
|
|
38
|
+
//! `extract_event_payload` slices the underlying buffer rather than
|
|
39
|
+
//! copying. UTF-8 decoding is the one unavoidable allocation per
|
|
40
|
+
//! event (we need a `String`/`&str` for downstream parsing).
|
|
41
|
+
|
|
42
|
+
use bytes::{Bytes, BytesMut};
|
|
43
|
+
|
|
44
|
+
/// One framed SSE event ready for state-machine consumption.
|
|
45
|
+
///
|
|
46
|
+
/// `event_name` is the value of the `event:` field (e.g.
|
|
47
|
+
/// `message_start`, `content_block_delta`). It is `None` when the
|
|
48
|
+
/// event has no `event:` line — the OpenAI Chat Completions stream
|
|
49
|
+
/// does this for every chunk (only the `data:` field is present, and
|
|
50
|
+
/// the event type is encoded inside the JSON payload).
|
|
51
|
+
///
|
|
52
|
+
/// `data` is the concatenated value of all `data:` fields in the
|
|
53
|
+
/// event, joined by `\n` per the SSE spec. `data` is **not** UTF-8
|
|
54
|
+
/// validated here — the framer only guarantees that `data` is the
|
|
55
|
+
/// exact byte slice between the field prefix and the event
|
|
56
|
+
/// terminator. State-machine code that needs UTF-8 calls
|
|
57
|
+
/// `event.data_str()` which returns `Result<&str, Utf8Error>`.
|
|
58
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
59
|
+
pub struct SseEvent {
|
|
60
|
+
pub event_name: Option<String>,
|
|
61
|
+
pub data: Bytes,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
impl SseEvent {
|
|
65
|
+
/// UTF-8 decode the data payload. Returns `Err` on invalid UTF-8;
|
|
66
|
+
/// the framer never panics, so malformed bytes surface here as a
|
|
67
|
+
/// recoverable error the state machine can log and skip.
|
|
68
|
+
pub fn data_str(&self) -> Result<&str, std::str::Utf8Error> {
|
|
69
|
+
std::str::from_utf8(&self.data)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// True iff the data field is the literal `[DONE]` sentinel used
|
|
73
|
+
/// by OpenAI Chat Completions and OpenAI Responses to mark end of
|
|
74
|
+
/// stream. Anthropic does not use this — it terminates with an
|
|
75
|
+
/// `event: message_stop`.
|
|
76
|
+
pub fn is_done_sentinel(&self) -> bool {
|
|
77
|
+
// Compare bytes directly so we never touch UTF-8 validation
|
|
78
|
+
// on the hot path. `[DONE]` is pure ASCII, so any byte-equal
|
|
79
|
+
// match is a genuine sentinel.
|
|
80
|
+
self.data.as_ref() == b"[DONE]"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// Stateful byte-level SSE framer.
|
|
85
|
+
///
|
|
86
|
+
/// Feed `Bytes` chunks via [`SseFramer::push`]; pull complete framed
|
|
87
|
+
/// events via [`SseFramer::next_event`]. The framer never blocks and
|
|
88
|
+
/// never allocates on the input chunks (it appends to an internal
|
|
89
|
+
/// `BytesMut` and then slices that). On `take_remaining`, callers can
|
|
90
|
+
/// recover unparsed trailing bytes (used by tests; production code
|
|
91
|
+
/// just drops the framer at end-of-stream).
|
|
92
|
+
#[derive(Debug, Default)]
|
|
93
|
+
pub struct SseFramer {
|
|
94
|
+
/// Accumulator for inbound bytes that have not yet been framed
|
|
95
|
+
/// into a complete event. Always holds at most one in-flight
|
|
96
|
+
/// event's worth of data plus any partial trailing bytes.
|
|
97
|
+
buf: BytesMut,
|
|
98
|
+
/// Once we see `[DONE]` we still tolerate further events (some
|
|
99
|
+
/// providers append a final empty `\n\n`), but the state machine
|
|
100
|
+
/// can use this to gate "no more useful events expected".
|
|
101
|
+
done_seen: bool,
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
impl SseFramer {
|
|
105
|
+
pub fn new() -> Self {
|
|
106
|
+
Self::default()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// True after the framer has yielded an event whose `data` is the
|
|
110
|
+
/// `[DONE]` sentinel. Once this is set, additional bytes pushed
|
|
111
|
+
/// in are still framed (so trailing whitespace doesn't error),
|
|
112
|
+
/// but state-machine code may stop attending to events.
|
|
113
|
+
pub fn done_seen(&self) -> bool {
|
|
114
|
+
self.done_seen
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// Append a chunk of inbound bytes to the framer's buffer.
|
|
118
|
+
///
|
|
119
|
+
/// `chunk` may straddle event boundaries, line boundaries, or
|
|
120
|
+
/// multi-byte UTF-8 codepoints. The framer makes no assumptions
|
|
121
|
+
/// beyond "these bytes appear, in order, after the bytes we've
|
|
122
|
+
/// already seen on this stream". Zero-length chunks are a no-op.
|
|
123
|
+
pub fn push(&mut self, chunk: &[u8]) {
|
|
124
|
+
if chunk.is_empty() {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
self.buf.extend_from_slice(chunk);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/// Drain the next complete event from the buffer, if one is
|
|
131
|
+
/// available. Returns `None` if the buffer doesn't yet contain a
|
|
132
|
+
/// blank-line terminator (`\n\n`). Returns `Some(Ok(event))` for
|
|
133
|
+
/// each complete event. Returns `Some(Err(...))` only on
|
|
134
|
+
/// genuinely malformed event content (e.g. `data:` line with
|
|
135
|
+
/// invalid UTF-8 in `event:` name — `event:` must be ASCII per
|
|
136
|
+
/// spec). Comments (`: ping`) and empty events are silently
|
|
137
|
+
/// skipped: the framer keeps consuming bytes until either a real
|
|
138
|
+
/// event surfaces or the buffer is exhausted.
|
|
139
|
+
pub fn next_event(&mut self) -> Option<Result<SseEvent, FramingError>> {
|
|
140
|
+
loop {
|
|
141
|
+
let term_len: usize;
|
|
142
|
+
let block_end = match find_double_newline(&self.buf) {
|
|
143
|
+
Some((end, len)) => {
|
|
144
|
+
term_len = len;
|
|
145
|
+
end
|
|
146
|
+
}
|
|
147
|
+
None => return None,
|
|
148
|
+
};
|
|
149
|
+
// The block is bytes [0, block_end); skip past the
|
|
150
|
+
// terminator (1 or 2 bytes — see find_double_newline).
|
|
151
|
+
// BytesMut::split_to is O(1): it advances the head
|
|
152
|
+
// pointer without copying, so we keep zero-copy framing.
|
|
153
|
+
let block = self.buf.split_to(block_end).freeze();
|
|
154
|
+
let _term = self.buf.split_to(term_len);
|
|
155
|
+
|
|
156
|
+
match parse_event_block(block) {
|
|
157
|
+
Ok(Some(event)) => {
|
|
158
|
+
if event.is_done_sentinel() {
|
|
159
|
+
self.done_seen = true;
|
|
160
|
+
}
|
|
161
|
+
return Some(Ok(event));
|
|
162
|
+
}
|
|
163
|
+
// No data lines (comment-only / empty event /
|
|
164
|
+
// pure ping). Skip and try the next block.
|
|
165
|
+
Ok(None) => continue,
|
|
166
|
+
Err(e) => return Some(Err(e)),
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/// Number of bytes currently buffered (un-framed). Useful for
|
|
172
|
+
/// tests asserting that no bytes were lost across chunk boundaries.
|
|
173
|
+
pub fn buffered_len(&self) -> usize {
|
|
174
|
+
self.buf.len()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/// Drain any remaining un-framed bytes. Used by tests to assert
|
|
178
|
+
/// "no bytes left over"; production code drops the framer.
|
|
179
|
+
pub fn take_remaining(&mut self) -> Bytes {
|
|
180
|
+
std::mem::take(&mut self.buf).freeze()
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/// Errors the framer surfaces. Per project rules we do **not**
|
|
185
|
+
/// silently swallow these — callers must `tracing::warn!` and either
|
|
186
|
+
/// drop the event or close the stream. The framer itself never
|
|
187
|
+
/// panics; the property test in `tests/sse_framing.rs` enforces this.
|
|
188
|
+
#[derive(Debug, thiserror::Error)]
|
|
189
|
+
pub enum FramingError {
|
|
190
|
+
/// `event:` field present but not valid UTF-8. The SSE spec
|
|
191
|
+
/// requires the field name and value to be ASCII; binary in
|
|
192
|
+
/// `event:` is a wire-format violation, not data we can recover
|
|
193
|
+
/// from. Real providers never emit this.
|
|
194
|
+
#[error("event field is not valid UTF-8: {0}")]
|
|
195
|
+
EventNameNotUtf8(std::str::Utf8Error),
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/// Find the byte offset of the first `\n\n` (or `\r\n\r\n`) terminator
|
|
199
|
+
/// in `buf`. Returns `(offset, terminator_len)` so the caller can
|
|
200
|
+
/// split off the event block and skip the terminator. `\r\n\r\n` is
|
|
201
|
+
/// tolerated for completeness; in practice all production providers
|
|
202
|
+
/// emit pure `\n\n`.
|
|
203
|
+
fn find_double_newline(buf: &[u8]) -> Option<(usize, usize)> {
|
|
204
|
+
// Manual byte-level search. Memchr would be faster on long
|
|
205
|
+
// buffers, but a typical SSE event is < 4KB and we'd be searching
|
|
206
|
+
// a tight window, so a simple loop wins on cache locality.
|
|
207
|
+
let mut i = 0;
|
|
208
|
+
while i + 1 < buf.len() {
|
|
209
|
+
if buf[i] == b'\n' && buf[i + 1] == b'\n' {
|
|
210
|
+
return Some((i, 2));
|
|
211
|
+
}
|
|
212
|
+
// \r\n\r\n
|
|
213
|
+
if i + 3 < buf.len()
|
|
214
|
+
&& buf[i] == b'\r'
|
|
215
|
+
&& buf[i + 1] == b'\n'
|
|
216
|
+
&& buf[i + 2] == b'\r'
|
|
217
|
+
&& buf[i + 3] == b'\n'
|
|
218
|
+
{
|
|
219
|
+
return Some((i, 4));
|
|
220
|
+
}
|
|
221
|
+
i += 1;
|
|
222
|
+
}
|
|
223
|
+
None
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/// Parse a single SSE event block (the bytes between two `\n\n`
|
|
227
|
+
/// terminators). Returns `Ok(None)` when the block contained no
|
|
228
|
+
/// `data:` lines (comment-only, empty, or pure ping).
|
|
229
|
+
fn parse_event_block(block: Bytes) -> Result<Option<SseEvent>, FramingError> {
|
|
230
|
+
let mut event_name: Option<String> = None;
|
|
231
|
+
let mut data_parts: Vec<Bytes> = Vec::new();
|
|
232
|
+
|
|
233
|
+
let mut start = 0usize;
|
|
234
|
+
while start < block.len() {
|
|
235
|
+
// Find end of line.
|
|
236
|
+
let line_end = block[start..]
|
|
237
|
+
.iter()
|
|
238
|
+
.position(|&b| b == b'\n')
|
|
239
|
+
.map(|p| start + p)
|
|
240
|
+
.unwrap_or(block.len());
|
|
241
|
+
|
|
242
|
+
// Strip a single trailing \r if present (CRLF tolerance).
|
|
243
|
+
let mut line_stop = line_end;
|
|
244
|
+
if line_stop > start && block[line_stop.saturating_sub(1)] == b'\r' {
|
|
245
|
+
line_stop -= 1;
|
|
246
|
+
}
|
|
247
|
+
let line = &block[start..line_stop];
|
|
248
|
+
start = line_end.saturating_add(1);
|
|
249
|
+
|
|
250
|
+
if line.is_empty() {
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if line[0] == b':' {
|
|
254
|
+
// Comment line. Includes `: ping` keepalives and any
|
|
255
|
+
// provider-specific debug comments. Drop silently.
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Split field:value on the FIRST colon. Per SSE spec, a
|
|
260
|
+
// single space following the colon is stripped from the value.
|
|
261
|
+
let (field, value_with_space) = match line.iter().position(|&b| b == b':') {
|
|
262
|
+
Some(p) => (&line[..p], &line[p + 1..]),
|
|
263
|
+
// No colon → entire line is the field name with empty value.
|
|
264
|
+
None => (line, &line[line.len()..]),
|
|
265
|
+
};
|
|
266
|
+
let value = if value_with_space.first() == Some(&b' ') {
|
|
267
|
+
&value_with_space[1..]
|
|
268
|
+
} else {
|
|
269
|
+
value_with_space
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
match field {
|
|
273
|
+
b"event" => {
|
|
274
|
+
let name = std::str::from_utf8(value).map_err(FramingError::EventNameNotUtf8)?;
|
|
275
|
+
event_name = Some(name.to_string());
|
|
276
|
+
}
|
|
277
|
+
b"data" => {
|
|
278
|
+
// We slice `block` (a `Bytes`) so the resulting
|
|
279
|
+
// payload shares the underlying allocation. No copy.
|
|
280
|
+
let abs_start = value.as_ptr() as usize - block.as_ptr() as usize;
|
|
281
|
+
let abs_end = abs_start + value.len();
|
|
282
|
+
data_parts.push(block.slice(abs_start..abs_end));
|
|
283
|
+
}
|
|
284
|
+
// `id:` and `retry:` are valid SSE fields but neither
|
|
285
|
+
// Anthropic nor OpenAI uses them on streamed responses.
|
|
286
|
+
// Track-and-ignore: future providers can be added.
|
|
287
|
+
_ => continue,
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if data_parts.is_empty() {
|
|
292
|
+
return Ok(None);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Per WHATWG SSE: multiple data: lines are joined with '\n'.
|
|
296
|
+
// The vast majority of provider events have exactly one data: line.
|
|
297
|
+
let data = if data_parts.len() == 1 {
|
|
298
|
+
data_parts.into_iter().next().unwrap()
|
|
299
|
+
} else {
|
|
300
|
+
let total: usize =
|
|
301
|
+
data_parts.iter().map(|b| b.len()).sum::<usize>() + (data_parts.len() - 1);
|
|
302
|
+
let mut out = BytesMut::with_capacity(total);
|
|
303
|
+
for (i, part) in data_parts.iter().enumerate() {
|
|
304
|
+
if i > 0 {
|
|
305
|
+
out.extend_from_slice(b"\n");
|
|
306
|
+
}
|
|
307
|
+
out.extend_from_slice(part);
|
|
308
|
+
}
|
|
309
|
+
out.freeze()
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
Ok(Some(SseEvent { event_name, data }))
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
#[cfg(test)]
|
|
316
|
+
mod tests {
|
|
317
|
+
use super::*;
|
|
318
|
+
|
|
319
|
+
#[test]
|
|
320
|
+
fn empty_buffer_yields_nothing() {
|
|
321
|
+
let mut f = SseFramer::new();
|
|
322
|
+
assert!(f.next_event().is_none());
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
#[test]
|
|
326
|
+
fn comment_skipped_no_event_yielded() {
|
|
327
|
+
let mut f = SseFramer::new();
|
|
328
|
+
f.push(b": ping\n\n");
|
|
329
|
+
assert!(f.next_event().is_none());
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
#[test]
|
|
333
|
+
fn data_line_only_yields_event_with_no_event_name() {
|
|
334
|
+
let mut f = SseFramer::new();
|
|
335
|
+
f.push(b"data: hello\n\n");
|
|
336
|
+
let ev = f.next_event().unwrap().unwrap();
|
|
337
|
+
assert_eq!(ev.event_name, None);
|
|
338
|
+
assert_eq!(ev.data.as_ref(), b"hello");
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
#[test]
|
|
342
|
+
fn event_name_and_data() {
|
|
343
|
+
let mut f = SseFramer::new();
|
|
344
|
+
f.push(b"event: message_start\ndata: {\"x\":1}\n\n");
|
|
345
|
+
let ev = f.next_event().unwrap().unwrap();
|
|
346
|
+
assert_eq!(ev.event_name.as_deref(), Some("message_start"));
|
|
347
|
+
assert_eq!(ev.data.as_ref(), b"{\"x\":1}");
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
#[test]
|
|
351
|
+
fn multiple_data_lines_joined_with_newline() {
|
|
352
|
+
let mut f = SseFramer::new();
|
|
353
|
+
f.push(b"data: a\ndata: b\n\n");
|
|
354
|
+
let ev = f.next_event().unwrap().unwrap();
|
|
355
|
+
assert_eq!(ev.data.as_ref(), b"a\nb");
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
#[test]
|
|
359
|
+
fn done_sentinel_detected() {
|
|
360
|
+
let mut f = SseFramer::new();
|
|
361
|
+
f.push(b"data: [DONE]\n\n");
|
|
362
|
+
let ev = f.next_event().unwrap().unwrap();
|
|
363
|
+
assert!(ev.is_done_sentinel());
|
|
364
|
+
assert!(f.done_seen());
|
|
365
|
+
}
|
|
366
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//! Server-Sent Events (SSE) parsing for streaming LLM responses.
|
|
2
|
+
//!
|
|
3
|
+
//! This module replaces the Python proxy's bug-prone `errors="ignore"`
|
|
4
|
+
//! UTF-8-decode-per-chunk SSE parsing with a byte-level framer plus
|
|
5
|
+
//! three provider-specific state machines.
|
|
6
|
+
//!
|
|
7
|
+
//! # Layering
|
|
8
|
+
//!
|
|
9
|
+
//! ```text
|
|
10
|
+
//! [TCP bytes] ─▶ SseFramer (framing.rs)
|
|
11
|
+
//! │ yields SseEvent { event_name, data: Bytes }
|
|
12
|
+
//! ▼
|
|
13
|
+
//! ┌─────────┴──────────┐
|
|
14
|
+
//! AnthropicStreamState ChunkState ResponseState
|
|
15
|
+
//! (anthropic.rs) (openai_chat.rs) (openai_responses.rs)
|
|
16
|
+
//! ```
|
|
17
|
+
//!
|
|
18
|
+
//! The framer is provider-agnostic. Each state machine consumes
|
|
19
|
+
//! `SseEvent`s and updates structured per-stream state used by
|
|
20
|
+
//! telemetry. None of these mutate the bytes flowing back to the
|
|
21
|
+
//! client — see `proxy.rs` for the byte-passthrough + parallel
|
|
22
|
+
//! state-machine wiring.
|
|
23
|
+
//!
|
|
24
|
+
//! # Bugs this retires
|
|
25
|
+
//!
|
|
26
|
+
//! - **P1-15** UTF-8 split across TCP reads (framer decodes per
|
|
27
|
+
//! complete event, not per chunk).
|
|
28
|
+
//! - **P1-8** missing `thinking_delta` arm.
|
|
29
|
+
//! - **P1-9** missing `signature_delta` arm.
|
|
30
|
+
//! - **P1-14** missing `citations_delta` arm.
|
|
31
|
+
//! - **P1-17** OpenAI Responses items keyed by position not id.
|
|
32
|
+
//! - **P4-48** OpenAI tool_call `id` overwritten by None on
|
|
33
|
+
//! subsequent chunks.
|
|
34
|
+
|
|
35
|
+
pub mod anthropic;
|
|
36
|
+
pub mod framing;
|
|
37
|
+
pub mod openai_chat;
|
|
38
|
+
pub mod openai_responses;
|
|
39
|
+
|
|
40
|
+
pub use framing::{FramingError, SseEvent, SseFramer};
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
//! OpenAI Chat Completions streaming state machine.
|
|
2
|
+
//!
|
|
3
|
+
//! Per OpenAI's streaming spec (and §5.2 of the Headroom realignment
|
|
4
|
+
//! guide), a Chat Completions stream is a sequence of `data:` lines
|
|
5
|
+
//! with no `event:` field. Each `data:` payload is a JSON `chunk`
|
|
6
|
+
//! object whose shape mirrors the non-streaming response, except
|
|
7
|
+
//! that:
|
|
8
|
+
//!
|
|
9
|
+
//! - Each `choices[*]` carries a `delta` instead of a `message`.
|
|
10
|
+
//! - The first chunk for a choice carries `delta.role = "assistant"`.
|
|
11
|
+
//! - Subsequent chunks carry `delta.content` (string fragments to
|
|
12
|
+
//! concatenate) and/or `delta.tool_calls` (per-tool-call delta
|
|
13
|
+
//! objects keyed by `index`).
|
|
14
|
+
//! - For tool calls: ONLY the first chunk for a given index carries
|
|
15
|
+
//! `id` and `function.name`. Subsequent chunks carry just
|
|
16
|
+
//! `function.arguments` to concatenate. The Python proxy didn't
|
|
17
|
+
//! handle this and silently overwrote `arguments` when a chunk
|
|
18
|
+
//! omitted `id` (P4-48 telemetry).
|
|
19
|
+
//! - The literal `[DONE]` sentinel marks end-of-stream; no event
|
|
20
|
+
//! payload is parsed.
|
|
21
|
+
//! - When `stream_options.include_usage = true`, OpenAI emits a
|
|
22
|
+
//! final chunk with `choices: []` and a populated `usage` object.
|
|
23
|
+
//! Without that flag, no usage is emitted on the stream — the
|
|
24
|
+
//! client must use the non-streaming endpoint for token counts.
|
|
25
|
+
//! - The `refusal` field, used by GPT-4o-class safety responses,
|
|
26
|
+
//! carries fragments to concatenate just like `content`.
|
|
27
|
+
|
|
28
|
+
use serde_json::Value;
|
|
29
|
+
use std::collections::HashMap;
|
|
30
|
+
|
|
31
|
+
use super::framing::SseEvent;
|
|
32
|
+
|
|
33
|
+
/// Stream-level status mirroring the Anthropic shape.
|
|
34
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
|
35
|
+
pub enum StreamStatus {
|
|
36
|
+
#[default]
|
|
37
|
+
Open,
|
|
38
|
+
Done,
|
|
39
|
+
Errored,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
#[derive(Debug, Default, Clone)]
|
|
43
|
+
pub struct ChunkState {
|
|
44
|
+
pub id: Option<String>,
|
|
45
|
+
pub model: Option<String>,
|
|
46
|
+
pub system_fingerprint: Option<String>,
|
|
47
|
+
pub choices: HashMap<usize, ChoiceState>,
|
|
48
|
+
pub usage: Option<Value>,
|
|
49
|
+
pub status: StreamStatus,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#[derive(Debug, Default, Clone)]
|
|
53
|
+
pub struct ChoiceState {
|
|
54
|
+
pub role: Option<String>,
|
|
55
|
+
pub content: String,
|
|
56
|
+
pub refusal: String,
|
|
57
|
+
pub finish_reason: Option<String>,
|
|
58
|
+
/// Tool calls, keyed by `index` from the wire (NOT vec position).
|
|
59
|
+
pub tool_calls: HashMap<usize, ToolCallState>,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
#[derive(Debug, Default, Clone)]
|
|
63
|
+
pub struct ToolCallState {
|
|
64
|
+
pub id: Option<String>,
|
|
65
|
+
pub call_type: Option<String>,
|
|
66
|
+
pub function_name: Option<String>,
|
|
67
|
+
/// Accumulated `function.arguments` string. Stays as a string
|
|
68
|
+
/// (NOT parsed JSON) because OpenAI's tool-call argument shape
|
|
69
|
+
/// is producer-defined and may legitimately not be JSON-parseable
|
|
70
|
+
/// on partial-stream snapshots.
|
|
71
|
+
pub function_arguments: String,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#[derive(Debug, thiserror::Error)]
|
|
75
|
+
pub enum StateError {
|
|
76
|
+
#[error("event payload is not valid UTF-8: {0}")]
|
|
77
|
+
PayloadNotUtf8(#[from] std::str::Utf8Error),
|
|
78
|
+
#[error("event payload is not valid JSON: {0}")]
|
|
79
|
+
PayloadNotJson(#[from] serde_json::Error),
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
impl ChunkState {
|
|
83
|
+
pub fn new() -> Self {
|
|
84
|
+
Self::default()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/// Apply one framed event. Chat Completions has no `event:` line;
|
|
88
|
+
/// callers feed every framed event in here.
|
|
89
|
+
pub fn apply(&mut self, event: SseEvent) -> Result<(), StateError> {
|
|
90
|
+
if event.is_done_sentinel() {
|
|
91
|
+
self.status = StreamStatus::Done;
|
|
92
|
+
return Ok(());
|
|
93
|
+
}
|
|
94
|
+
// We do warn on `event:` being set because Chat Completions
|
|
95
|
+
// doesn't use it — if a producer (or a reverse proxy in
|
|
96
|
+
// front of us) added one, that's wire-format drift we want
|
|
97
|
+
// to surface, not paper over.
|
|
98
|
+
if let Some(name) = event.event_name.as_deref() {
|
|
99
|
+
tracing::warn!(
|
|
100
|
+
event = "sse_unknown_event",
|
|
101
|
+
provider = "openai_chat",
|
|
102
|
+
event_name = name,
|
|
103
|
+
payload_preview = %payload_preview(&event.data),
|
|
104
|
+
"openai chat event has an event: line; spec says it shouldn't"
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let v: Value = parse_json(&event.data)?;
|
|
109
|
+
|
|
110
|
+
// Top-level fields: id, model, system_fingerprint, choices, usage.
|
|
111
|
+
if let Some(id) = v.get("id").and_then(|x| x.as_str()) {
|
|
112
|
+
// Only set on first chunk; later chunks repeat the same id.
|
|
113
|
+
if self.id.is_none() {
|
|
114
|
+
self.id = Some(id.to_string());
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if let Some(m) = v.get("model").and_then(|x| x.as_str()) {
|
|
118
|
+
if self.model.is_none() {
|
|
119
|
+
self.model = Some(m.to_string());
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if let Some(fp) = v.get("system_fingerprint").and_then(|x| x.as_str()) {
|
|
123
|
+
if self.system_fingerprint.is_none() {
|
|
124
|
+
self.system_fingerprint = Some(fp.to_string());
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if let Some(choices) = v.get("choices").and_then(|x| x.as_array()) {
|
|
129
|
+
for choice in choices {
|
|
130
|
+
self.apply_choice(choice);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Usage (final chunk when include_usage is set).
|
|
135
|
+
if let Some(usage) = v.get("usage") {
|
|
136
|
+
if !usage.is_null() {
|
|
137
|
+
self.usage = Some(usage.clone());
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
Ok(())
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
fn apply_choice(&mut self, choice: &Value) {
|
|
144
|
+
let Some(index) = choice.get("index").and_then(|x| x.as_u64()) else {
|
|
145
|
+
// Spec mandates `index`; missing → log and drop the
|
|
146
|
+
// choice (don't fall back to choice[0] silently).
|
|
147
|
+
tracing::warn!(
|
|
148
|
+
event = "sse_unknown_event",
|
|
149
|
+
provider = "openai_chat",
|
|
150
|
+
event_name = "choice_missing_index",
|
|
151
|
+
"choice missing index; dropping"
|
|
152
|
+
);
|
|
153
|
+
return;
|
|
154
|
+
};
|
|
155
|
+
let index = index as usize;
|
|
156
|
+
let cs = self.choices.entry(index).or_default();
|
|
157
|
+
|
|
158
|
+
if let Some(delta) = choice.get("delta") {
|
|
159
|
+
if let Some(role) = delta.get("role").and_then(|x| x.as_str()) {
|
|
160
|
+
// Set on first delta only; subsequent deltas omit it.
|
|
161
|
+
if cs.role.is_none() {
|
|
162
|
+
cs.role = Some(role.to_string());
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if let Some(text) = delta.get("content").and_then(|x| x.as_str()) {
|
|
166
|
+
cs.content.push_str(text);
|
|
167
|
+
}
|
|
168
|
+
if let Some(text) = delta.get("refusal").and_then(|x| x.as_str()) {
|
|
169
|
+
cs.refusal.push_str(text);
|
|
170
|
+
}
|
|
171
|
+
if let Some(tcs) = delta.get("tool_calls").and_then(|x| x.as_array()) {
|
|
172
|
+
for tc in tcs {
|
|
173
|
+
apply_tool_call_delta(cs, tc);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if let Some(fr) = choice.get("finish_reason").and_then(|x| x.as_str()) {
|
|
179
|
+
cs.finish_reason = Some(fr.to_string());
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
fn apply_tool_call_delta(cs: &mut ChoiceState, tc: &Value) {
|
|
185
|
+
let Some(idx) = tc.get("index").and_then(|x| x.as_u64()) else {
|
|
186
|
+
tracing::warn!(
|
|
187
|
+
event = "sse_unknown_event",
|
|
188
|
+
provider = "openai_chat",
|
|
189
|
+
event_name = "tool_call_missing_index",
|
|
190
|
+
"tool_call delta missing index; dropping"
|
|
191
|
+
);
|
|
192
|
+
return;
|
|
193
|
+
};
|
|
194
|
+
let entry = cs.tool_calls.entry(idx as usize).or_default();
|
|
195
|
+
// id and function.name only on first chunk for this tool call.
|
|
196
|
+
if let Some(id) = tc.get("id").and_then(|x| x.as_str()) {
|
|
197
|
+
if entry.id.is_none() {
|
|
198
|
+
entry.id = Some(id.to_string());
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if let Some(t) = tc.get("type").and_then(|x| x.as_str()) {
|
|
202
|
+
if entry.call_type.is_none() {
|
|
203
|
+
entry.call_type = Some(t.to_string());
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if let Some(func) = tc.get("function") {
|
|
207
|
+
if let Some(n) = func.get("name").and_then(|x| x.as_str()) {
|
|
208
|
+
if entry.function_name.is_none() {
|
|
209
|
+
entry.function_name = Some(n.to_string());
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if let Some(args) = func.get("arguments").and_then(|x| x.as_str()) {
|
|
213
|
+
entry.function_arguments.push_str(args);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
fn parse_json(data: &bytes::Bytes) -> Result<Value, StateError> {
|
|
219
|
+
let s = std::str::from_utf8(data)?;
|
|
220
|
+
Ok(serde_json::from_str(s)?)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
fn payload_preview(data: &bytes::Bytes) -> String {
|
|
224
|
+
const LIMIT: usize = 96;
|
|
225
|
+
let slice = if data.len() > LIMIT {
|
|
226
|
+
&data[..LIMIT]
|
|
227
|
+
} else {
|
|
228
|
+
&data[..]
|
|
229
|
+
};
|
|
230
|
+
String::from_utf8_lossy(slice).into_owned()
|
|
231
|
+
}
|