Skip to content

feat(chatapps): implement generic platform adapter template (RFC #218)#304

Open
hrygo wants to merge 7 commits intomainfrom
refactor/218-chatapps-template
Open

feat(chatapps): implement generic platform adapter template (RFC #218)#304
hrygo wants to merge 7 commits intomainfrom
refactor/218-chatapps-template

Conversation

@hrygo
Copy link
Copy Markdown
Owner

@hrygo hrygo commented Mar 17, 2026

Summary

Implement RFC #218: ChatApps Platform Adapter Template - reducing 80% boilerplate code for new platform integrations.

Changes

Phase 1: Core Interface Abstraction

  • chatapps/base/template.go: Generic interfaces
    • EventParser[T]: Platform event parsing
    • PlatformFormatter: Message formatting
    • PlatformSender: Message sending

Phase 2: Generic Adapter Template

  • chatapps/base/platform_adapter.go: PlatformAdapter[T] struct
    • HandleWebhook: Common workflow (signature → parse → convert → handler)
    • SendMessage, DeleteMessage, UpdateMessage implementations

Phase 3: Slack Migration

  • chatapps/slack/adapter_template.go: Template-based Slack adapter (~335 lines vs ~800 lines)

Phase 4: New Platform Example

  • chatapps/discord/adapter.go: Complete Discord adapter (~200 lines)

Code Reduction

Adapter Original Template Reduction
Full-featured ~800 LOC - -
Slack Template - ~335 LOC 58%
Discord Template - ~200 LOC 75%

Test plan

  • go build ./chatapps/... passes
  • go vet ./chatapps/... passes
  • go test ./chatapps/... passes
  • Pre-commit checks pass

🤖 Generated with Claude Code

Resolves #218

Add a reference implementation template in chatapps/template/ that provides
a starting point for creating new ChatApps platform adapters. The template
includes:

- config.go: Configuration struct with validation
- adapter.go: Main adapter with ChatAdapter interface implementation
- errors.go: Platform-specific error definitions
- README.md: Documentation and usage guide

Developers can copy this template and implement the TODO sections to quickly
add support for new messaging platforms like Discord, Telegram, etc.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 17, 2026

Codecov Report

❌ Patch coverage is 25.15091% with 372 lines in your changes missing coverage. Please review.
✅ Project coverage is 42.20%. Comparing base (fc88969) to head (349218d).

Files with missing lines Patch % Lines
chatapps/slack/adapter_template.go 0.00% 131 Missing ⚠️
chatapps/discord/adapter.go 0.00% 99 Missing ⚠️
chatapps/template/adapter.go 0.00% 61 Missing ⚠️
chatapps/base/platform_adapter.go 70.33% 35 Missing ⚠️
chatapps/template/errors.go 0.00% 17 Missing ⚠️
chatapps/template/config.go 0.00% 14 Missing ⚠️
chatapps/base/template.go 76.36% 13 Missing ⚠️
chatapps/base/signature.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #304      +/-   ##
==========================================
+ Coverage   41.92%   42.20%   +0.27%     
==========================================
  Files         165      172       +7     
  Lines       19132    19629     +497     
==========================================
+ Hits         8022     8285     +263     
- Misses      11110    11344     +234     
Flag Coverage Δ
unittests 42.20% <25.15%> (+0.27%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

黄飞虹 and others added 3 commits March 18, 2026 09:59
Add Phase 1 & 2 of the platform adapter template:

- chatapps/base/template.go: Core interfaces
  - EventParser[T]: Generic event parsing
  - PlatformFormatter: Message formatting
  - PlatformSender: Message sending
  - SignatureValidator, error types

- chatapps/base/platform_adapter.go: Generic template
  - PlatformAdapter[T]: Orchestrates common workflow
  - HandleWebhook: Signature → Parse → Convert → Handler
  - SendMessage, DeleteMessage, UpdateMessage implementations
  - Implements ChatAdapter and MessageOperations interfaces

- chatapps/base/signature.go: Add SkipVerify to NoOpVerifier

Verification:
- go build ./chatapps/... ✓
- go vet ./chatapps/... ✓
- go test ./chatapps/base/... ✓

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Demonstrate using the new generic PlatformAdapter[T] template with a
simplified Slack adapter implementation:

- SlackSignatureVerifier: Platform-specific signature verification
- SlackEventParser: Event parsing with base.EventParser[Event]
- SlackFormatter: Message formatting with base.PlatformFormatter
- SlackSender: Message sending with base.PlatformSender
- TemplateAdapter: Simplified adapter using the template

This shows the boilerplate reduction achievable with the template:
- ~250 lines vs ~800+ lines in full adapter.go
- Clear separation of concerns
- Reusable components

Verification:
- go build ./chatapps/slack/... ✓
- go vet ./chatapps/slack/... ✓
- go test ./chatapps/slack/... ✓

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Demonstrate new platform integration using the generic template:

- chatapps/discord/adapter.go: Complete adapter using PlatformAdapter[T]
  - DiscordSignatureVerifier: Ed25519 signature verification stub
  - DiscordEventParser: Interaction parsing
  - DiscordFormatter: Message formatting
  - DiscordSender: REST API integration
  - Adapter: Template-based adapter struct

This shows how quickly a new platform can be added:
- ~200 lines vs 800+ lines for full-featured adapters
- Clear separation of platform-specific components
- Reusable generic template infrastructure

The template enables 60%+ code reduction for new platform integrations.

Verification:
- go build ./chatapps/... ✓
- go vet ./chatapps/discord/... ✓

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@hrygo hrygo changed the title feat(chatapps): add adapter template for new platform integrations feat(chatapps): implement generic platform adapter template (RFC #218) Mar 18, 2026
黄飞虹 and others added 3 commits March 18, 2026 10:53
Add comprehensive unit tests for the new template components:

- template_test.go:
  - BaseSignatureValidator tests
  - BaseFormatter.Chunk tests
  - ComputeHMAC256 tests
  - SecureCompare tests
  - Error type tests

- platform_adapter_test.go:
  - NewPlatformAdapter tests
  - HandleWebhook tests (success, method not allowed, verification failed)
  - SendMessage tests
  - DeleteMessage tests
  - UpdateMessage tests
  - Start/Stop tests
  - SetHandler tests

Verification:
- go build ./chatapps/base/... ✓
- go test ./chatapps/base/... ✓ (29% coverage)
- go test ./chatapps/slack/... ✓

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add comprehensive unit tests:

- webhook_helpers_test.go:
  - ReadBodyWithError, ReadBodyWithLog, ReadBodyWithLogAndClose
  - CheckMethod, CheckMethodPOST, CheckMethodGET
  - WebhookRunner tests (New, Run, Wait, Stop)
  - WebhookHandler tests (New, WithVerifier)
  - Response helper tests
  - MessageTypeToStatusType tests

- adapter_options_test.go:
  - AdapterOption tests
  - Adapter basic method tests
  - SendMessage tests
  - Session management tests

Coverage: 29% -> 38.8%

Verification:
- go build ./chatapps/... ✓
- go test ./chatapps/... ✓

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add more tests to improve coverage of new template code:
- TestPlatformAdapter_HandleMessage
- TestPlatformAdapter_SetAssistantStatus
- TestPlatformAdapter_SendThreadReply
- TestPlatformAdapter_StartStream
- TestPlatformAdapter_AppendStream
- TestPlatformAdapter_StopStream
- TestPlatformAdapter_Path
- TestPlatformAdapter_extractChannelID

Coverage: 38.8% -> 39.7%

Verification:
- go build ./chatapps/base/... ✓
- go test ./chatapps/base/... ✓ (39.7% coverage)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] ChatApps 平台适配器模板化:减少 80% 样板代码

1 participant