c695125dce
Large Feishu wikis stopped syncing after the first batch (Tencent/WeKnora#2136: 5000+ docs, only ~150 ingested; #1262: a child-list failure aborted the run). Three compounding causes, fixed in layers: 1. Rate-limit / transient failures had no retry. Feishu's wiki/drive/export APIs are aggressively rate limited; a thousand-doc sync issues tens of thousands of calls, and a single 429/5xx burst failed whole swathes of docs. Add exponential backoff honouring Retry-After to doRequest (JSON) and downloadRawBytes (files): 429 up to 3x, 5xx once, transport errors back off. 2. The batch path fetched the entire tree into memory before ingesting, so a sync that hit the 2h task timeout lost all progress and restarted from scratch, and a transient per-document export failure advanced the cursor and skipped that doc permanently. Add an optional StreamingConnector interface, implemented for Feishu: FetchStream interleaves fetch -> ingest -> checkpoint, bounding memory to one item and persisting a per-node edit-time cursor at page boundaries. The service dispatches to it via type assertion; connectors without it fall back to FetchAll / FetchIncremental unchanged (non-breaking). A resumed/incremental run skips nodes already recorded at their current edit time (converges), a failed fetch does not advance the cursor (retried next run instead of dropped), and partial listing failures emit error items and carry prior edit times forward instead of aborting the sync. 3. Failures and skips were invisible. Unsupported node types (mindnote/slides/…) were skipped with no item, error or log ("13 docs synced only 3"). FetchAll now tallies discovered/fetched/failed/skipped per resource with a per-type skip breakdown and a progress line every 100 nodes. The backend already persisted per-item errors in SyncLog.result.errors but the frontend type omitted the field; declare `result` on SyncLog and render up to 50 failed documents in the sync-log drawer (#1262). i18n for zh-CN/en-US/ko-KR/ru-RU. result.errors is a jsonb column shipped in every list response, so it is capped at 100 samples server-side (recordSyncError); the exact failure total comes from the bounded items_failed counter, which the drawer uses for its count and "N more" line so a sync that fails thousands of docs stays bounded. Each sample is a structured, localisable SyncItemError instead of a raw "title: ...api error: status=500 body={json}" dump that repeated the title and leaked status/body/log_id. The connector (feishuFailure) classifies the error into a stable i18n code (+ the feishu code as a param); the service builds {title, code, params, message}; the drawer localises via vue-i18n to the viewer's language (zh/en/ko/ru), falling back to message. A custom UnmarshalJSON keeps old string-form logs readable. The banner is localised from items_failed too. Raw errors stay in the server logs. Follows the two-tier norm of Airbyte/Fivetran/Onyx: human, actionable, localised summaries in the UI; raw detail in logs. Transient errors say "will retry" (the cursor is retained); auth/permission errors point at the fix instead. Tests: client_retry_test (backoff), tally_test (accounting), connector_stream / datasource_stream (streaming dispatch + checkpoint), connector_convergence (timeout + transient-failure resume converges to full coverage; proven to catch #2136 via bug injection), datasource_result_cap (error-sample cap + structured localisable SyncItemError, no raw body), connector_error_reason (error→i18n-code classification), sync_item_error (legacy-string + structured back-compat) — all in the normal suite. connector_realapi / connector_seed are an opt-in real-Feishu harness gated by the feishu_integration build tag AND env vars, never run in CI.
WeKnora Data Source Sync Framework
Overview
The data source sync framework enables WeKnora to automatically import and synchronize content from external platforms (Feishu, Notion, Confluence, etc.) into knowledge bases. This is the foundational layer upon which all specific connectors are built.
Architecture
Core Components
┌─────────────────────────────────────────────────────────────┐
│ External Data Sources │
│ (Feishu, Notion, Confluence, GitHub, Google Drive...) │
└─────────────┬───────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Connector Registry & Adapters │
│ Each platform (feishu/, notion/, confluence/, etc) │
│ implements the Connector interface │
└─────────────┬───────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ DataSourceService (Business Logic) │
│ ├─ CreateDataSource / UpdateDataSource │
│ ├─ ManualSync / ListAvailableResources │
│ ├─ ValidateConnection / PauseDataSource │
│ └─ ProcessSync (asynq task handler) │
└─────────────┬───────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ HTTP Handler & API Routes (/api/v1/datasource) │
│ REST endpoints for UI/programmatic access │
└─────────────┬───────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Database (MySQL / PostgreSQL) │
│ ├─ data_sources (configuration & state) │
│ └─ sync_logs (history & metadata) │
└─────────────────────────────────────────────────────────────┘
Task Flow
Manual Trigger or Scheduled Job (cron)
↓
Scheduler enqueues asynq Task
↓
ProcessSync (task handler) executes
↓
Connector.Fetch* methods pull data
↓
Diff engine identifies changes (new/update/delete)
↓
KnowledgeService creates/updates Knowledge entries
↓
Documents parsed → chunks → vectors → indexed
File Structure
internal/datasource/
├── connector.go # Connector interface & registry
├── errors.go # Error definitions
└── README.md # This file
internal/types/
├── datasource.go # Data models (DataSource, SyncLog, etc)
└── interfaces/datasource.go # Service interfaces
internal/application/
├── repository/datasource_repo.go # Database access layer
└── service/datasource_service.go # Business logic
internal/handler/
└── datasource.go # HTTP request handlers
internal/router/
├── router.go # Route registration
├── task.go # Asynq task registration
└── sync_task.go # Lite mode task registration
migrations/versioned/
└── 000028_datasource_tables.up/down.sql # Database schema
Data Models
DataSource
Represents a configured external data source:
- ID: Unique identifier
- Type: Connector type (feishu, notion, confluence, etc)
- Config: Encrypted credentials and configuration (JSON)
- SyncSchedule: Cron expression (e.g., "0 */6 * * *")
- SyncMode: "incremental" or "full"
- Status: active | paused | error
- ConflictStrategy: overwrite | skip
- SyncDeletions: Whether to sync deletions from source
- LastSyncAt: Timestamp of last successful sync
- LastSyncCursor: State for incremental sync
- LastSyncResult: Summary of last sync
SyncLog
Tracks execution of each sync operation:
- Status: running | success | partial | failed | canceled
- StartedAt/FinishedAt: Execution timestamps
- Items*: Counters (total, created, updated, deleted, skipped, failed)
- ErrorMessage: Error details if failed
- Result: Detailed sync result (JSON)
Connector Interface
All external data sources implement this interface:
type Connector interface {
Type() string
Validate(ctx, config) error
ListResources(ctx, config) ([]Resource, error)
FetchAll(ctx, config, resourceIDs) ([]FetchedItem, error)
FetchIncremental(ctx, config, cursor) ([]FetchedItem, *SyncCursor, error)
}
API Endpoints
Data Source Management
POST /api/v1/datasource # Create
GET /api/v1/datasource # List (by kb_id)
GET /api/v1/datasource/:id # Get
PUT /api/v1/datasource/:id # Update
DELETE /api/v1/datasource/:id # Delete
Operations
POST /api/v1/datasource/:id/validate # Test connection
GET /api/v1/datasource/:id/resources # List available resources
POST /api/v1/datasource/:id/sync # Trigger manual sync
POST /api/v1/datasource/:id/pause # Pause
POST /api/v1/datasource/:id/resume # Resume
Logs
GET /api/v1/datasource/:id/logs # List sync logs
GET /api/v1/datasource/logs/:log_id # Get specific log
Metadata
GET /api/v1/datasource/types # Available connectors
Implementation Guide
Adding a New Connector
-
Create the connector package in
internal/datasource/connector/<type>/// connector/<type>/client.go - API client // connector/<type>/connector.go - Implements Connector interface // connector/<type>/types.go - Type-specific models -
Implement the Connector interface
type YourConnector struct { // fields } func (c *YourConnector) Type() string { return types.ConnectorTypeYourType } func (c *YourConnector) Validate(ctx, config) error { // Test connectivity and credentials } func (c *YourConnector) ListResources(ctx, config) ([]types.Resource, error) { // List available resources (documents, spaces, etc) } func (c *YourConnector) FetchAll(ctx, config, resourceIDs) ([]types.FetchedItem, error) { // Full sync - fetch all items from specified resources } func (c *YourConnector) FetchIncremental(ctx, config, cursor) ([]types.FetchedItem, *types.SyncCursor, error) { // Incremental sync - fetch only changed items since cursor } -
Register the connector in the container (in container.go)
container.Provide(func() datasource.Connector { return yourconnector.NewConnector() }) -
Add metadata in
connector.gotypes.ConnectorTypeYourType: { Type: types.ConnectorTypeYourType, Name: "Your Platform", AuthType: "oauth2", // ... }
Database Schema
data_sources table
- Stores data source configurations
- Indexes: tenant_id, knowledge_base_id, type, status
- Encrypted config field (AES-256-GCM)
sync_logs table
- Stores sync operation history
- Indexes: data_source_id, tenant_id, status, started_at
- Foreign key to data_sources with cascade delete
Key Design Decisions
- Adapter Pattern: Each connector is a separate implementation, easy to add new ones
- Async Task Queue: Syncs are non-blocking, using asynq (or sync mode in Lite)
- Incremental Sync: Supports cursor-based incremental syncs to minimize API calls
- Encryption: API keys/tokens stored encrypted with AES-256-GCM
- Multi-tenant: All operations are tenant-isolated
- Channel Tracking: Knowledge.Channel field tracks data source type for origin tracking
Future Enhancements
- Webhook Support: Real-time push from platforms that support webhooks
- Conflict Resolution: Advanced merge/conflict strategies for overlapping content
- Rate Limiting: Per-connector rate limiting and backoff strategies
- Scheduling: Full cron scheduler with time zone support
- Monitoring: Metrics, alerting, and sync health dashboards
- Filtering: User-defined filters for selective syncing (by title, date, tags, etc)
- Transformation: Content transformation pipelines (e.g., extract tables, summarize)
- Deduplication: Smart deduplication across multiple sources
Testing
The framework is designed to be testable:
- Mock connectors can be created for testing
- No external dependencies required for core logic
- Database operations isolated and mockable
Error Handling
All operations return detailed error information:
- Connection failures are captured and status updated
- Partial failures are logged (some items succeed, some fail)
- Error messages stored in DataSource for user troubleshooting