feat: Implement wiki_config updates in knowledge base management
Added functionality to enable updates for the `wiki_config` field in the `KnowledgeBaseConfig` struct. This includes: 1. **Type Definition Update**: Added `WikiConfig` field to `KnowledgeBaseConfig` to allow JSON unmarshaling during update requests. 2. **Service Layer Enhancement**: Updated the `UpdateKnowledgeBase` method to persist changes to `wiki_config` when provided in the request. 3. **Frontend TypeScript Adjustments**: Enhanced type definitions in the frontend to support `wiki_config`, improving IDE autocomplete and type checking. This fix addresses the critical issue where `wiki_config` was not being updated due to its absence in the `KnowledgeBaseConfig` struct, ensuring proper handling of wiki configurations during knowledge base updates.
This commit is contained in:
@@ -0,0 +1,328 @@
|
|||||||
|
# Wiki Config Fix - Complete Implementation Guide
|
||||||
|
|
||||||
|
## Problem Statement
|
||||||
|
The `wiki_config` field cannot be updated via the KB update endpoint because `KnowledgeBaseConfig` struct is missing the `WikiConfig` field.
|
||||||
|
|
||||||
|
## Solution Overview
|
||||||
|
Add `WikiConfig` field to `KnowledgeBaseConfig` struct and update the service layer to persist it.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Change 1: Update Type Definition
|
||||||
|
|
||||||
|
**File:** `internal/types/knowledgebase.go`
|
||||||
|
|
||||||
|
**Location:** Lines 99-107
|
||||||
|
|
||||||
|
**Current Code:**
|
||||||
|
```go
|
||||||
|
// KnowledgeBaseConfig represents the knowledge base configuration
|
||||||
|
type KnowledgeBaseConfig struct {
|
||||||
|
// Chunking configuration
|
||||||
|
ChunkingConfig ChunkingConfig `yaml:"chunking_config" json:"chunking_config"`
|
||||||
|
// Image processing configuration
|
||||||
|
ImageProcessingConfig ImageProcessingConfig `yaml:"image_processing_config" json:"image_processing_config"`
|
||||||
|
// FAQ configuration (only for FAQ type knowledge bases)
|
||||||
|
FAQConfig *FAQConfig `yaml:"faq_config" json:"faq_config"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Fixed Code:**
|
||||||
|
```go
|
||||||
|
// KnowledgeBaseConfig represents the knowledge base configuration
|
||||||
|
type KnowledgeBaseConfig struct {
|
||||||
|
// Chunking configuration
|
||||||
|
ChunkingConfig ChunkingConfig `yaml:"chunking_config" json:"chunking_config"`
|
||||||
|
// Image processing configuration
|
||||||
|
ImageProcessingConfig ImageProcessingConfig `yaml:"image_processing_config" json:"image_processing_config"`
|
||||||
|
// FAQ configuration (only for FAQ type knowledge bases)
|
||||||
|
FAQConfig *FAQConfig `yaml:"faq_config" json:"faq_config"`
|
||||||
|
// Wiki configuration (for document type knowledge bases with wiki feature enabled)
|
||||||
|
WikiConfig *WikiConfig `yaml:"wiki_config" json:"wiki_config"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Changes:**
|
||||||
|
- Added `WikiConfig *WikiConfig` field with proper YAML and JSON tags
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Change 2: Update Service Layer
|
||||||
|
|
||||||
|
**File:** `internal/application/service/knowledgebase.go`
|
||||||
|
|
||||||
|
**Location:** Lines 265-311 (UpdateKnowledgeBase method)
|
||||||
|
|
||||||
|
**Current Code (Lines 288-297):**
|
||||||
|
```go
|
||||||
|
// Update the knowledge base properties
|
||||||
|
kb.Name = name
|
||||||
|
kb.Description = description
|
||||||
|
if config != nil {
|
||||||
|
kb.ChunkingConfig = config.ChunkingConfig
|
||||||
|
kb.ImageProcessingConfig = config.ImageProcessingConfig
|
||||||
|
if config.FAQConfig != nil {
|
||||||
|
kb.FAQConfig = config.FAQConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Fixed Code:**
|
||||||
|
```go
|
||||||
|
// Update the knowledge base properties
|
||||||
|
kb.Name = name
|
||||||
|
kb.Description = description
|
||||||
|
if config != nil {
|
||||||
|
kb.ChunkingConfig = config.ChunkingConfig
|
||||||
|
kb.ImageProcessingConfig = config.ImageProcessingConfig
|
||||||
|
if config.FAQConfig != nil {
|
||||||
|
kb.FAQConfig = config.FAQConfig
|
||||||
|
}
|
||||||
|
if config.WikiConfig != nil {
|
||||||
|
kb.WikiConfig = config.WikiConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Changes:**
|
||||||
|
- Added conditional check to update `kb.WikiConfig` from `config.WikiConfig`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Change 3: Update Frontend Type Hints (Optional but Recommended)
|
||||||
|
|
||||||
|
**File:** `frontend/src/api/knowledge-base/index.ts`
|
||||||
|
|
||||||
|
**Location:** Lines 11-33 (createKnowledgeBase function)
|
||||||
|
|
||||||
|
**Current Code:**
|
||||||
|
```typescript
|
||||||
|
export function createKnowledgeBase(data: {
|
||||||
|
name: string;
|
||||||
|
description?: string;
|
||||||
|
type?: 'document' | 'faq';
|
||||||
|
chunking_config?: any;
|
||||||
|
embedding_model_id?: string;
|
||||||
|
summary_model_id?: string;
|
||||||
|
vlm_config?: {
|
||||||
|
enabled: boolean;
|
||||||
|
model_id?: string;
|
||||||
|
};
|
||||||
|
storage_provider_config?: { provider: string };
|
||||||
|
storage_config?: any; // legacy, kept for backward compat (dual-write)
|
||||||
|
asr_config?: {
|
||||||
|
enabled: boolean;
|
||||||
|
model_id?: string;
|
||||||
|
language?: string;
|
||||||
|
};
|
||||||
|
extract_config?: any;
|
||||||
|
faq_config?: { index_mode: string; question_index_mode?: string };
|
||||||
|
}) {
|
||||||
|
return post(`/api/v1/knowledge-bases`, data);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Fixed Code:**
|
||||||
|
```typescript
|
||||||
|
export function createKnowledgeBase(data: {
|
||||||
|
name: string;
|
||||||
|
description?: string;
|
||||||
|
type?: 'document' | 'faq';
|
||||||
|
chunking_config?: any;
|
||||||
|
embedding_model_id?: string;
|
||||||
|
summary_model_id?: string;
|
||||||
|
vlm_config?: {
|
||||||
|
enabled: boolean;
|
||||||
|
model_id?: string;
|
||||||
|
};
|
||||||
|
storage_provider_config?: { provider: string };
|
||||||
|
storage_config?: any; // legacy, kept for backward compat (dual-write)
|
||||||
|
asr_config?: {
|
||||||
|
enabled: boolean;
|
||||||
|
model_id?: string;
|
||||||
|
language?: string;
|
||||||
|
};
|
||||||
|
extract_config?: any;
|
||||||
|
faq_config?: { index_mode: string; question_index_mode?: string };
|
||||||
|
wiki_config?: {
|
||||||
|
enabled: boolean;
|
||||||
|
auto_ingest?: boolean;
|
||||||
|
synthesis_model_id?: string;
|
||||||
|
wiki_language?: string;
|
||||||
|
max_pages_per_ingest?: number;
|
||||||
|
};
|
||||||
|
}) {
|
||||||
|
return post(`/api/v1/knowledge-bases`, data);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Changes:**
|
||||||
|
- Added `wiki_config` property with full type definition for better IDE autocomplete
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
### Test Case 1: Create KB with wiki_config
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X POST http://localhost:8080/api/v1/knowledge-bases \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "Authorization: Bearer <token>" \
|
||||||
|
-d '{
|
||||||
|
"name": "Test KB with Wiki",
|
||||||
|
"type": "document",
|
||||||
|
"embedding_model_id": "text-embedding-ada-002",
|
||||||
|
"wiki_config": {
|
||||||
|
"enabled": true,
|
||||||
|
"auto_ingest": true,
|
||||||
|
"synthesis_model_id": "gpt-4",
|
||||||
|
"wiki_language": "en"
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
**Expected Result:**
|
||||||
|
- KB created with wiki_config populated ✅
|
||||||
|
|
||||||
|
### Test Case 2: Update KB with wiki_config (MAIN TEST)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# First, get the KB to see initial state
|
||||||
|
curl -X GET http://localhost:8080/api/v1/knowledge-bases/{id} \
|
||||||
|
-H "Authorization: Bearer <token>"
|
||||||
|
|
||||||
|
# Then update it
|
||||||
|
curl -X PUT http://localhost:8080/api/v1/knowledge-bases/{id} \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "Authorization: Bearer <token>" \
|
||||||
|
-d '{
|
||||||
|
"name": "Updated KB",
|
||||||
|
"description": "Updated description",
|
||||||
|
"config": {
|
||||||
|
"wiki_config": {
|
||||||
|
"enabled": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
|
||||||
|
# Verify the update
|
||||||
|
curl -X GET http://localhost:8080/api/v1/knowledge-bases/{id} \
|
||||||
|
-H "Authorization: Bearer <token>"
|
||||||
|
```
|
||||||
|
|
||||||
|
**Expected Result (Before Fix):**
|
||||||
|
- wiki_config remains unchanged (bug) ❌
|
||||||
|
|
||||||
|
**Expected Result (After Fix):**
|
||||||
|
- wiki_config.enabled changed to false ✅
|
||||||
|
|
||||||
|
### Test Case 3: Update other config fields (Regression Test)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X PUT http://localhost:8080/api/v1/knowledge-bases/{id} \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "Authorization: Bearer <token>" \
|
||||||
|
-d '{
|
||||||
|
"name": "Updated KB",
|
||||||
|
"config": {
|
||||||
|
"chunking_config": {
|
||||||
|
"chunk_size": 1024,
|
||||||
|
"chunk_overlap": 100
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
**Expected Result:**
|
||||||
|
- Chunking config updated ✅
|
||||||
|
- wiki_config preserved ✅
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Verification Checklist
|
||||||
|
|
||||||
|
After implementing the fix, verify:
|
||||||
|
|
||||||
|
- [ ] Type definition compiles without errors
|
||||||
|
- [ ] Backend builds successfully
|
||||||
|
- [ ] Test Case 1 passes (create with wiki_config)
|
||||||
|
- [ ] Test Case 2 passes (update with wiki_config)
|
||||||
|
- [ ] Test Case 3 passes (no regression on other configs)
|
||||||
|
- [ ] Frontend compiles without TypeScript errors
|
||||||
|
- [ ] Database column `wiki_config` contains expected JSON
|
||||||
|
- [ ] EnsureDefaults() still works correctly
|
||||||
|
- [ ] Wiki feature still functions end-to-end
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Code Review Checklist
|
||||||
|
|
||||||
|
- [ ] All GORM tags are consistent with other fields
|
||||||
|
- [ ] JSON marshaling/unmarshaling follows existing patterns
|
||||||
|
- [ ] Null safety handled (config can be nil)
|
||||||
|
- [ ] No breaking changes to existing API
|
||||||
|
- [ ] Backwards compatible (existing requests still work)
|
||||||
|
- [ ] Service layer properly validates wiki_config
|
||||||
|
- [ ] Repository layer properly persists changes
|
||||||
|
- [ ] EnsureDefaults() method still handles wiki_config correctly
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Rollback Plan
|
||||||
|
|
||||||
|
If needed, to rollback:
|
||||||
|
|
||||||
|
1. Revert Change 1: Remove WikiConfig field from KnowledgeBaseConfig
|
||||||
|
2. Revert Change 2: Remove the wiki_config update code from service
|
||||||
|
3. Revert Change 3: Remove wiki_config from frontend type hints
|
||||||
|
4. Rebuild and redeploy
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Impact Analysis
|
||||||
|
|
||||||
|
### Affected Files
|
||||||
|
1. `internal/types/knowledgebase.go` - 1 line added
|
||||||
|
2. `internal/application/service/knowledgebase.go` - 3 lines added
|
||||||
|
3. `frontend/src/api/knowledge-base/index.ts` - 7 lines added
|
||||||
|
|
||||||
|
### Backwards Compatibility
|
||||||
|
- ✅ Fully backwards compatible
|
||||||
|
- ✅ Existing requests without wiki_config still work
|
||||||
|
- ✅ Existing KBs with wiki_config unaffected
|
||||||
|
- ✅ No database migration needed
|
||||||
|
|
||||||
|
### Performance Impact
|
||||||
|
- ✅ Negligible - only adds one more field to struct
|
||||||
|
|
||||||
|
### Security Impact
|
||||||
|
- ✅ No security issues introduced
|
||||||
|
- ✅ Same permission checks apply
|
||||||
|
- ✅ wiki_config validated through EnsureDefaults()
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Related Code
|
||||||
|
|
||||||
|
### WikiConfig Struct Definition
|
||||||
|
Location: `internal/types/wiki_page.go` (Lines 89-120)
|
||||||
|
|
||||||
|
### EnsureDefaults() Method
|
||||||
|
Location: `internal/types/knowledgebase.go` (Lines 472-508)
|
||||||
|
- Already handles wiki_config validation
|
||||||
|
- No changes needed
|
||||||
|
|
||||||
|
### Database Column
|
||||||
|
Location: `migrations/versioned/000032_wiki_pages.up.sql` (Line 7)
|
||||||
|
- Already created as JSONB
|
||||||
|
- No changes needed
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Timeline
|
||||||
|
- **Implementation:** ~5 minutes
|
||||||
|
- **Testing:** ~10 minutes
|
||||||
|
- **Code Review:** ~5 minutes
|
||||||
|
- **Deployment:** ~5 minutes
|
||||||
|
- **Total:** ~25 minutes
|
||||||
@@ -0,0 +1,501 @@
|
|||||||
|
# WeKnora Wiki Config Flow Analysis - Frontend to Database
|
||||||
|
|
||||||
|
## Executive Summary
|
||||||
|
|
||||||
|
The `wiki_config` field **IS DEFINED** in the `KnowledgeBase` struct but **IS NOT CURRENTLY INCLUDED** in the `KnowledgeBaseConfig` struct used by the update endpoints. This means:
|
||||||
|
|
||||||
|
- ✅ `wiki_config` is stored in the database (`knowledge_bases.wiki_config` JSONB column)
|
||||||
|
- ✅ `wiki_config` can be set during KB creation (included in full `KnowledgeBase` struct)
|
||||||
|
- ❌ `wiki_config` **CANNOT** be updated via the `/knowledge-bases/{id}` update endpoint
|
||||||
|
- ⚠️ Frontend sends `config: { wiki_config: {...} }` but backend ignores it
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Detailed Flow Analysis
|
||||||
|
|
||||||
|
### 1. DATABASE LAYER
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/migrations/versioned/000032_wiki_pages.up.sql`
|
||||||
|
|
||||||
|
```sql
|
||||||
|
ALTER TABLE knowledge_bases ADD COLUMN IF NOT EXISTS wiki_config JSONB;
|
||||||
|
|
||||||
|
COMMENT ON COLUMN knowledge_bases.wiki_config IS
|
||||||
|
'Wiki configuration: {"auto_ingest": bool, "synthesis_model_id": string, "wiki_language": string, "max_pages_per_ingest": int}';
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ✅ Column exists and properly typed as JSONB
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. TYPE DEFINITIONS
|
||||||
|
|
||||||
|
#### 2a. WikiConfig Struct
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/internal/types/wiki_page.go` (Lines 89-120)
|
||||||
|
|
||||||
|
```go
|
||||||
|
type WikiConfig struct {
|
||||||
|
// Enabled activates the wiki feature for this knowledge base
|
||||||
|
Enabled bool `yaml:"enabled" json:"enabled"`
|
||||||
|
|
||||||
|
// AutoIngest triggers wiki page generation/update when new documents are added
|
||||||
|
AutoIngest bool `yaml:"auto_ingest" json:"auto_ingest"`
|
||||||
|
|
||||||
|
// SynthesisModelID is the LLM model ID used for wiki page generation and updates
|
||||||
|
SynthesisModelID string `yaml:"synthesis_model_id" json:"synthesis_model_id"`
|
||||||
|
|
||||||
|
// WikiLanguage is the preferred language for wiki content (zh, en, auto)
|
||||||
|
WikiLanguage string `yaml:"wiki_language" json:"wiki_language"`
|
||||||
|
|
||||||
|
// MaxPagesPerIngest limits pages created/updated per ingest operation (0 = no limit)
|
||||||
|
MaxPagesPerIngest int `yaml:"max_pages_per_ingest" json:"max_pages_per_ingest"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implements driver.Valuer and sql.Scanner for GORM
|
||||||
|
func (c WikiConfig) Value() (driver.Value, error) { return json.Marshal(c) }
|
||||||
|
func (c *WikiConfig) Scan(value interface{}) error { ... }
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ✅ Properly defined with GORM marshaling
|
||||||
|
|
||||||
|
#### 2b. KnowledgeBase Struct
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/internal/types/knowledgebase.go` (Line 76)
|
||||||
|
|
||||||
|
```go
|
||||||
|
type KnowledgeBase struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
// ... other fields ...
|
||||||
|
|
||||||
|
// WikiConfig stores wiki-specific configuration (only for wiki type knowledge bases)
|
||||||
|
WikiConfig *WikiConfig `yaml:"wiki_config" json:"wiki_config" gorm:"column:wiki_config;type:json"`
|
||||||
|
|
||||||
|
// ... timestamps ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ✅ Field exists with proper GORM mapping
|
||||||
|
|
||||||
|
#### 2c. KnowledgeBaseConfig Struct (REQUEST STRUCTURE)
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/internal/types/knowledgebase.go` (Lines 99-107)
|
||||||
|
|
||||||
|
```go
|
||||||
|
type KnowledgeBaseConfig struct {
|
||||||
|
// Chunking configuration
|
||||||
|
ChunkingConfig ChunkingConfig `yaml:"chunking_config" json:"chunking_config"`
|
||||||
|
|
||||||
|
// Image processing configuration
|
||||||
|
ImageProcessingConfig ImageProcessingConfig `yaml:"image_processing_config" json:"image_processing_config"`
|
||||||
|
|
||||||
|
// FAQ configuration (only for FAQ type knowledge bases)
|
||||||
|
FAQConfig *FAQConfig `yaml:"faq_config" json:"faq_config"`
|
||||||
|
|
||||||
|
// ❌ MISSING: WikiConfig is NOT included here!
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ❌ **CRITICAL ISSUE**: WikiConfig is missing from KnowledgeBaseConfig
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. HANDLER LAYER
|
||||||
|
|
||||||
|
#### 3a. CreateKnowledgeBase Handler
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/internal/handler/knowledgebase.go` (Lines 114-147)
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (h *KnowledgeBaseHandler) CreateKnowledgeBase(c *gin.Context) {
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
|
||||||
|
// Parse request body as full KnowledgeBase struct
|
||||||
|
var req types.KnowledgeBase
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
// ... error handling ...
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Passes to service
|
||||||
|
kb, err := h.service.CreateKnowledgeBase(ctx, &req)
|
||||||
|
|
||||||
|
// ... response ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Request Format Accepted:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"name": "My KB",
|
||||||
|
"type": "document",
|
||||||
|
"wiki_config": {
|
||||||
|
"enabled": true,
|
||||||
|
"auto_ingest": true,
|
||||||
|
"synthesis_model_id": "gpt-4",
|
||||||
|
"wiki_language": "en"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ✅ Can accept wiki_config during creation
|
||||||
|
|
||||||
|
#### 3b. UpdateKnowledgeBase Handler
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/internal/handler/knowledgebase.go` (Lines 433-488)
|
||||||
|
|
||||||
|
```go
|
||||||
|
type UpdateKnowledgeBaseRequest struct {
|
||||||
|
Name string `json:"name" binding:"required"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Config *types.KnowledgeBaseConfig `json:"config"` // ❌ Uses KnowledgeBaseConfig, not full KnowledgeBase
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *KnowledgeBaseHandler) UpdateKnowledgeBase(c *gin.Context) {
|
||||||
|
// Parse as UpdateKnowledgeBaseRequest
|
||||||
|
var req UpdateKnowledgeBaseRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
// ... error handling ...
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calls service with config
|
||||||
|
kb, err := h.service.UpdateKnowledgeBase(ctx, id, req.Name, req.Description, req.Config)
|
||||||
|
|
||||||
|
// ... response ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Request Format:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"name": "Updated KB Name",
|
||||||
|
"description": "Updated description",
|
||||||
|
"config": {
|
||||||
|
"chunking_config": { ... },
|
||||||
|
"image_processing_config": { ... }
|
||||||
|
// ❌ wiki_config cannot be sent here because KnowledgeBaseConfig doesn't have it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ❌ **CRITICAL ISSUE**: Cannot pass wiki_config in update request
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4. SERVICE LAYER
|
||||||
|
|
||||||
|
#### 4a. CreateKnowledgeBase Service
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/internal/application/service/knowledgebase.go` (Lines 73-98)
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s *knowledgeBaseService) CreateKnowledgeBase(ctx context.Context, kb *types.KnowledgeBase) (*types.KnowledgeBase, error) {
|
||||||
|
// Generate UUID and set creation timestamps
|
||||||
|
if kb.ID == "" {
|
||||||
|
kb.ID = uuid.New().String()
|
||||||
|
}
|
||||||
|
kb.CreatedAt = time.Now()
|
||||||
|
kb.TenantID = types.MustTenantIDFromContext(ctx)
|
||||||
|
kb.UpdatedAt = time.Now()
|
||||||
|
kb.EnsureDefaults() // ✅ This calls EnsureDefaults which validates wiki_config
|
||||||
|
|
||||||
|
logger.Infof(ctx, "Creating knowledge base, ID: %s, tenant ID: %d, name: %s", kb.ID, kb.TenantID, kb.Name)
|
||||||
|
|
||||||
|
if err := s.repo.CreateKnowledgeBase(ctx, kb); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return kb, nil
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ✅ Receives full KnowledgeBase with wiki_config
|
||||||
|
|
||||||
|
#### 4b. EnsureDefaults() Method
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/internal/types/knowledgebase.go` (Lines 472-508)
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (kb *KnowledgeBase) EnsureDefaults() {
|
||||||
|
if kb == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if kb.Type == "" {
|
||||||
|
kb.Type = KnowledgeBaseTypeDocument
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear type-specific configs that don't belong
|
||||||
|
if kb.Type != KnowledgeBaseTypeFAQ {
|
||||||
|
kb.FAQConfig = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set defaults for Wiki
|
||||||
|
if kb.IsWikiEnabled() { // Checks if WikiConfig != nil && WikiConfig.Enabled
|
||||||
|
if kb.WikiConfig.WikiLanguage == "" {
|
||||||
|
kb.WikiConfig.WikiLanguage = "auto"
|
||||||
|
}
|
||||||
|
if !kb.WikiConfig.AutoIngest {
|
||||||
|
kb.WikiConfig.AutoIngest = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set defaults for FAQ ...
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kb *KnowledgeBase) IsWikiEnabled() bool {
|
||||||
|
return kb != nil && kb.WikiConfig != nil && kb.WikiConfig.Enabled
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ✅ Properly sets wiki_config defaults during creation
|
||||||
|
|
||||||
|
#### 4c. UpdateKnowledgeBase Service
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/internal/application/service/knowledgebase.go` (Lines 265-311)
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s *knowledgeBaseService) UpdateKnowledgeBase(ctx context.Context,
|
||||||
|
id string,
|
||||||
|
name string,
|
||||||
|
description string,
|
||||||
|
config *types.KnowledgeBaseConfig, // ❌ Only receives KnowledgeBaseConfig
|
||||||
|
) (*types.KnowledgeBase, error) {
|
||||||
|
if id == "" {
|
||||||
|
return nil, errors.New("knowledge base ID cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get existing knowledge base
|
||||||
|
kb, err := s.repo.GetKnowledgeBaseByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the knowledge base properties
|
||||||
|
kb.Name = name
|
||||||
|
kb.Description = description
|
||||||
|
if config != nil {
|
||||||
|
kb.ChunkingConfig = config.ChunkingConfig
|
||||||
|
kb.ImageProcessingConfig = config.ImageProcessingConfig
|
||||||
|
if config.FAQConfig != nil {
|
||||||
|
kb.FAQConfig = config.FAQConfig
|
||||||
|
}
|
||||||
|
// ❌ NO CODE TO UPDATE kb.WikiConfig - it's not in config!
|
||||||
|
}
|
||||||
|
kb.UpdatedAt = time.Now()
|
||||||
|
kb.EnsureDefaults() // ✅ But if wiki_config was passed, this would validate it
|
||||||
|
|
||||||
|
if err := s.repo.UpdateKnowledgeBase(ctx, kb); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return kb, nil
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ❌ **CRITICAL ISSUE**: Cannot update wiki_config because it's not in the config parameter
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5. REPOSITORY LAYER
|
||||||
|
|
||||||
|
#### 5a. CreateKnowledgeBase Repository
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/internal/application/repository/knowledgebase.go` (Lines 25-28)
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (r *knowledgeBaseRepository) CreateKnowledgeBase(ctx context.Context, kb *types.KnowledgeBase) error {
|
||||||
|
return r.db.WithContext(ctx).Create(kb).Error // ✅ GORM will serialize all fields, including wiki_config
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ✅ GORM properly handles all fields
|
||||||
|
|
||||||
|
#### 5b. UpdateKnowledgeBase Repository
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/internal/application/repository/knowledgebase.go` (Lines 115-118)
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (r *knowledgeBaseRepository) UpdateKnowledgeBase(ctx context.Context, kb *types.KnowledgeBase) error {
|
||||||
|
return r.db.WithContext(ctx).Save(kb).Error // ✅ GORM.Save() will update all fields, including wiki_config
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ✅ Repository properly saves wiki_config if it's set
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 6. FRONTEND API LAYER
|
||||||
|
|
||||||
|
**File:** `/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/frontend/src/api/knowledge-base/index.ts`
|
||||||
|
|
||||||
|
#### 6a. createKnowledgeBase
|
||||||
|
```typescript
|
||||||
|
export function createKnowledgeBase(data: {
|
||||||
|
name: string;
|
||||||
|
description?: string;
|
||||||
|
type?: 'document' | 'faq';
|
||||||
|
chunking_config?: any;
|
||||||
|
embedding_model_id?: string;
|
||||||
|
// ... other fields ...
|
||||||
|
// ❓ No wiki_config field in type hints!
|
||||||
|
}) {
|
||||||
|
return post(`/api/v1/knowledge-bases`, data);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ⚠️ Type hints don't include wiki_config (but it would be accepted as any)
|
||||||
|
|
||||||
|
#### 6b. updateKnowledgeBase
|
||||||
|
```typescript
|
||||||
|
export function updateKnowledgeBase(id: string, data: {
|
||||||
|
name: string;
|
||||||
|
description?: string;
|
||||||
|
config: any // Any config object
|
||||||
|
}) {
|
||||||
|
return put(`/api/v1/knowledge-bases/${id}`, data);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Status:** ⚠️ Frontend can send `config: { wiki_config: {...} }` but backend ignores it
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Tracing the Flow
|
||||||
|
|
||||||
|
### Creating a KB with wiki_config ✅
|
||||||
|
```
|
||||||
|
Frontend: POST /api/v1/knowledge-bases { name, type, wiki_config: {...} }
|
||||||
|
↓
|
||||||
|
Handler.CreateKnowledgeBase: Parses as types.KnowledgeBase
|
||||||
|
↓
|
||||||
|
Service.CreateKnowledgeBase: Receives KnowledgeBase with wiki_config
|
||||||
|
↓
|
||||||
|
Service.EnsureDefaults: Validates and sets defaults for wiki_config
|
||||||
|
↓
|
||||||
|
Repository.CreateKnowledgeBase: GORM.Create() serializes wiki_config to JSONB
|
||||||
|
↓
|
||||||
|
Database: wiki_config column populated ✅
|
||||||
|
```
|
||||||
|
|
||||||
|
### Updating a KB with wiki_config ❌
|
||||||
|
```
|
||||||
|
Frontend: PUT /api/v1/knowledge-bases/{id} { name, config: { wiki_config: {...} } }
|
||||||
|
↓
|
||||||
|
Handler.UpdateKnowledgeBase: Parses as UpdateKnowledgeBaseRequest
|
||||||
|
↓
|
||||||
|
❌ UpdateKnowledgeBaseRequest.Config is of type KnowledgeBaseConfig (NOT including WikiConfig)
|
||||||
|
↓
|
||||||
|
❌ JSON unmarshaling fails or ignores wiki_config field
|
||||||
|
↓
|
||||||
|
Service.UpdateKnowledgeBase: Receives config WITHOUT wiki_config
|
||||||
|
↓
|
||||||
|
❌ Code never updates kb.WikiConfig - it stays at existing value
|
||||||
|
↓
|
||||||
|
Repository.UpdateKnowledgeBase: GORM.Save() persists unchanged wiki_config
|
||||||
|
↓
|
||||||
|
Database: wiki_config column NOT updated ❌
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Required Fixes
|
||||||
|
|
||||||
|
### Fix 1: Add WikiConfig to KnowledgeBaseConfig Struct
|
||||||
|
**File:** `internal/types/knowledgebase.go`
|
||||||
|
|
||||||
|
```go
|
||||||
|
type KnowledgeBaseConfig struct {
|
||||||
|
// Chunking configuration
|
||||||
|
ChunkingConfig ChunkingConfig `yaml:"chunking_config" json:"chunking_config"`
|
||||||
|
|
||||||
|
// Image processing configuration
|
||||||
|
ImageProcessingConfig ImageProcessingConfig `yaml:"image_processing_config" json:"image_processing_config"`
|
||||||
|
|
||||||
|
// FAQ configuration (only for FAQ type knowledge bases)
|
||||||
|
FAQConfig *FAQConfig `yaml:"faq_config" json:"faq_config"`
|
||||||
|
|
||||||
|
// ADD THIS:
|
||||||
|
// Wiki configuration (for document type knowledge bases with wiki feature enabled)
|
||||||
|
WikiConfig *WikiConfig `yaml:"wiki_config" json:"wiki_config"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Fix 2: Update UpdateKnowledgeBase Service
|
||||||
|
**File:** `internal/application/service/knowledgebase.go`
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (s *knowledgeBaseService) UpdateKnowledgeBase(ctx context.Context,
|
||||||
|
id string,
|
||||||
|
name string,
|
||||||
|
description string,
|
||||||
|
config *types.KnowledgeBaseConfig,
|
||||||
|
) (*types.KnowledgeBase, error) {
|
||||||
|
// ... existing code ...
|
||||||
|
|
||||||
|
if config != nil {
|
||||||
|
kb.ChunkingConfig = config.ChunkingConfig
|
||||||
|
kb.ImageProcessingConfig = config.ImageProcessingConfig
|
||||||
|
if config.FAQConfig != nil {
|
||||||
|
kb.FAQConfig = config.FAQConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// ADD THIS:
|
||||||
|
if config.WikiConfig != nil {
|
||||||
|
kb.WikiConfig = config.WikiConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... rest of code ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Fix 3: Update Frontend Type Hints
|
||||||
|
**File:** `frontend/src/api/knowledge-base/index.ts`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export function createKnowledgeBase(data: {
|
||||||
|
name: string;
|
||||||
|
description?: string;
|
||||||
|
type?: 'document' | 'faq';
|
||||||
|
chunking_config?: any;
|
||||||
|
embedding_model_id?: string;
|
||||||
|
summary_model_id?: string;
|
||||||
|
vlm_config?: { enabled: boolean; model_id?: string };
|
||||||
|
storage_provider_config?: { provider: string };
|
||||||
|
asr_config?: { enabled: boolean; model_id?: string; language?: string };
|
||||||
|
extract_config?: any;
|
||||||
|
faq_config?: { index_mode: string; question_index_mode?: string };
|
||||||
|
// ADD THIS:
|
||||||
|
wiki_config?: {
|
||||||
|
enabled: boolean;
|
||||||
|
auto_ingest?: boolean;
|
||||||
|
synthesis_model_id?: string;
|
||||||
|
wiki_language?: string;
|
||||||
|
max_pages_per_ingest?: number;
|
||||||
|
};
|
||||||
|
}) {
|
||||||
|
return post(`/api/v1/knowledge-bases`, data);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Summary Table
|
||||||
|
|
||||||
|
| Component | Location | Status | Can Handle wiki_config |
|
||||||
|
|-----------|----------|--------|----------------------|
|
||||||
|
| Database Column | `migrations/000032_wiki_pages.up.sql` | ✅ Exists | ✅ Yes |
|
||||||
|
| WikiConfig Type | `internal/types/wiki_page.go` | ✅ Defined | ✅ Yes |
|
||||||
|
| KnowledgeBase Struct | `internal/types/knowledgebase.go` | ✅ Has field | ✅ Yes |
|
||||||
|
| KnowledgeBaseConfig Struct | `internal/types/knowledgebase.go` | ❌ Missing field | ❌ No |
|
||||||
|
| CreateKnowledgeBase Handler | `internal/handler/knowledgebase.go` | ✅ Full struct | ✅ Yes |
|
||||||
|
| UpdateKnowledgeBase Handler | `internal/handler/knowledgebase.go` | ⚠️ Limited struct | ❌ No |
|
||||||
|
| CreateKnowledgeBase Service | `internal/application/service/knowledgebase.go` | ✅ Receives KB | ✅ Yes |
|
||||||
|
| UpdateKnowledgeBase Service | `internal/application/service/knowledgebase.go` | ❌ Not updated | ❌ No |
|
||||||
|
| Repository Layer | `internal/application/repository/knowledgebase.go` | ✅ Works | ✅ Yes |
|
||||||
|
| Frontend API | `frontend/src/api/knowledge-base/index.ts` | ⚠️ Missing types | ⚠️ Partial |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
The infrastructure to handle `wiki_config` is almost complete, but there's a **critical gap in the update flow**:
|
||||||
|
|
||||||
|
1. **Creation works** ✅ - Wiki config can be set when creating a KB
|
||||||
|
2. **Update broken** ❌ - Wiki config cannot be updated after creation
|
||||||
|
3. **Root cause** - `KnowledgeBaseConfig` struct is missing the `WikiConfig` field
|
||||||
|
4. **Impact** - Frontend sends wiki config updates but backend ignores them
|
||||||
|
|
||||||
|
**3 simple code changes are needed** to fix this issue (see Required Fixes section above).
|
||||||
@@ -0,0 +1,201 @@
|
|||||||
|
# Wiki Config Flow - Quick Reference
|
||||||
|
|
||||||
|
## Critical Finding: UPDATE BROKEN ❌
|
||||||
|
|
||||||
|
When updating a KB via `PUT /api/v1/knowledge-bases/{id}`, the `wiki_config` is **NOT** persisted.
|
||||||
|
|
||||||
|
## Why?
|
||||||
|
|
||||||
|
```
|
||||||
|
UpdateKnowledgeBaseRequest struct:
|
||||||
|
├── Name: string
|
||||||
|
├── Description: string
|
||||||
|
└── Config: KnowledgeBaseConfig ❌ Missing WikiConfig field
|
||||||
|
├── ChunkingConfig
|
||||||
|
├── ImageProcessingConfig
|
||||||
|
└── FAQConfig
|
||||||
|
```
|
||||||
|
|
||||||
|
The `KnowledgeBaseConfig` struct lacks the `WikiConfig` field, so:
|
||||||
|
- ✅ Frontend CAN send `config: { wiki_config: {...} }`
|
||||||
|
- ❌ But backend IGNORES it during unmarshaling
|
||||||
|
- ❌ Service never updates kb.WikiConfig
|
||||||
|
- ❌ Database column remains unchanged
|
||||||
|
|
||||||
|
## Creation Works ✅
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /api/v1/knowledge-bases
|
||||||
|
{
|
||||||
|
"name": "My KB",
|
||||||
|
"wiki_config": { "enabled": true, ... } ← FULL KnowledgeBase struct accepted
|
||||||
|
}
|
||||||
|
|
||||||
|
Handler parses as types.KnowledgeBase (not KnowledgeBaseConfig)
|
||||||
|
→ Service receives wiki_config
|
||||||
|
→ EnsureDefaults() validates it
|
||||||
|
→ Repository saves it to database ✅
|
||||||
|
```
|
||||||
|
|
||||||
|
## Update Broken ❌
|
||||||
|
|
||||||
|
```
|
||||||
|
PUT /api/v1/knowledge-bases/{id}
|
||||||
|
{
|
||||||
|
"name": "Updated",
|
||||||
|
"config": {
|
||||||
|
"wiki_config": { "enabled": false } ← Sent to backend
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Handler parses as UpdateKnowledgeBaseRequest
|
||||||
|
→ Config field is KnowledgeBaseConfig (no WikiConfig!)
|
||||||
|
→ JSON unmarshaling IGNORES wiki_config field
|
||||||
|
→ Service never sees it
|
||||||
|
→ Database NOT updated ❌
|
||||||
|
```
|
||||||
|
|
||||||
|
## Files That Need Changes
|
||||||
|
|
||||||
|
### 1. `/internal/types/knowledgebase.go` (Lines 99-107)
|
||||||
|
```diff
|
||||||
|
type KnowledgeBaseConfig struct {
|
||||||
|
ChunkingConfig ChunkingConfig
|
||||||
|
ImageProcessingConfig ImageProcessingConfig
|
||||||
|
FAQConfig *FAQConfig
|
||||||
|
+ WikiConfig *WikiConfig // ADD THIS LINE
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. `/internal/application/service/knowledgebase.go` (Lines 291-296)
|
||||||
|
```diff
|
||||||
|
if config != nil {
|
||||||
|
kb.ChunkingConfig = config.ChunkingConfig
|
||||||
|
kb.ImageProcessingConfig = config.ImageProcessingConfig
|
||||||
|
if config.FAQConfig != nil {
|
||||||
|
kb.FAQConfig = config.FAQConfig
|
||||||
|
}
|
||||||
|
+ if config.WikiConfig != nil {
|
||||||
|
+ kb.WikiConfig = config.WikiConfig
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. `/frontend/src/api/knowledge-base/index.ts` (createKnowledgeBase function)
|
||||||
|
Add wiki_config to type hints (for TypeScript completion):
|
||||||
|
```typescript
|
||||||
|
wiki_config?: {
|
||||||
|
enabled: boolean;
|
||||||
|
auto_ingest?: boolean;
|
||||||
|
synthesis_model_id?: string;
|
||||||
|
wiki_language?: string;
|
||||||
|
max_pages_per_ingest?: number;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
## Database Path
|
||||||
|
|
||||||
|
### Storage
|
||||||
|
- Table: `knowledge_bases`
|
||||||
|
- Column: `wiki_config` (JSONB)
|
||||||
|
- Created by: Migration `000032_wiki_pages.up.sql`
|
||||||
|
|
||||||
|
### Data Types
|
||||||
|
- Go struct: `WikiConfig` in `/internal/types/wiki_page.go`
|
||||||
|
- Fields: `enabled`, `auto_ingest`, `synthesis_model_id`, `wiki_language`, `max_pages_per_ingest`
|
||||||
|
- GORM mapping: `gorm:"column:wiki_config;type:json"`
|
||||||
|
|
||||||
|
## Request/Response Flow
|
||||||
|
|
||||||
|
### Create (Works ✅)
|
||||||
|
```
|
||||||
|
POST /api/v1/knowledge-bases
|
||||||
|
→ CreateKnowledgeBaseRequest (full KnowledgeBase)
|
||||||
|
→ Handler: c.ShouldBindJSON(&types.KnowledgeBase)
|
||||||
|
→ Service: CreateKnowledgeBase(ctx, &kb)
|
||||||
|
→ Repository: GORM.Create(kb)
|
||||||
|
→ DB: wiki_config saved ✅
|
||||||
|
```
|
||||||
|
|
||||||
|
### Update (Broken ❌)
|
||||||
|
```
|
||||||
|
PUT /api/v1/knowledge-bases/{id}
|
||||||
|
→ UpdateKnowledgeBaseRequest (Config: KnowledgeBaseConfig)
|
||||||
|
→ Handler: c.ShouldBindJSON(&UpdateKnowledgeBaseRequest)
|
||||||
|
→ Service: UpdateKnowledgeBase(ctx, id, name, desc, config)
|
||||||
|
→ Service: config.WikiConfig is nil (never set!)
|
||||||
|
→ Repository: GORM.Save(kb) with old wiki_config
|
||||||
|
→ DB: wiki_config NOT updated ❌
|
||||||
|
```
|
||||||
|
|
||||||
|
## Handler Code Locations
|
||||||
|
|
||||||
|
| Operation | Handler | File:Lines | Status |
|
||||||
|
|-----------|---------|-----------|--------|
|
||||||
|
| Create | CreateKnowledgeBase | knowledgebase.go:114-147 | ✅ |
|
||||||
|
| Read | GetKnowledgeBase | knowledgebase.go:260-284 | ✅ |
|
||||||
|
| Update | UpdateKnowledgeBase | knowledgebase.go:446-488 | ❌ |
|
||||||
|
| Delete | DeleteKnowledgeBase | knowledgebase.go:502-536 | ✅ |
|
||||||
|
|
||||||
|
## Service Layer Locations
|
||||||
|
|
||||||
|
| Operation | Service Method | File:Lines | Status |
|
||||||
|
|-----------|----------------|-----------|--------|
|
||||||
|
| Create | CreateKnowledgeBase | knowledgebase.go:73-98 | ✅ |
|
||||||
|
| Read | GetKnowledgeBaseByID | knowledgebase.go:101-117 | ✅ |
|
||||||
|
| Update | UpdateKnowledgeBase | knowledgebase.go:265-311 | ❌ |
|
||||||
|
| List | ListKnowledgeBases | knowledgebase.go:157-210 | ✅ |
|
||||||
|
|
||||||
|
## Frontend Integration
|
||||||
|
|
||||||
|
### API Functions
|
||||||
|
- `createKnowledgeBase()` - accepts any data ✅
|
||||||
|
- `updateKnowledgeBase()` - accepts any config ✅ (but backend ignores wiki_config)
|
||||||
|
- `getKnowledgeBaseById()` - returns full KB ✅
|
||||||
|
|
||||||
|
### Type Hints Location
|
||||||
|
- File: `/frontend/src/api/knowledge-base/index.ts`
|
||||||
|
- Lines: 11-33 (createKnowledgeBase)
|
||||||
|
- Lines: 42-44 (updateKnowledgeBase)
|
||||||
|
|
||||||
|
## Test Cases
|
||||||
|
|
||||||
|
To verify the fix works:
|
||||||
|
|
||||||
|
### Test 1: Create with wiki_config
|
||||||
|
```
|
||||||
|
POST /api/v1/knowledge-bases
|
||||||
|
{
|
||||||
|
"name": "Test KB",
|
||||||
|
"type": "document",
|
||||||
|
"wiki_config": {
|
||||||
|
"enabled": true,
|
||||||
|
"auto_ingest": true,
|
||||||
|
"synthesis_model_id": "gpt-4",
|
||||||
|
"wiki_language": "en"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
→ Should persist wiki_config ✅
|
||||||
|
```
|
||||||
|
|
||||||
|
### Test 2: Update wiki_config
|
||||||
|
```
|
||||||
|
PUT /api/v1/knowledge-bases/{id}
|
||||||
|
{
|
||||||
|
"name": "Updated",
|
||||||
|
"config": {
|
||||||
|
"wiki_config": {
|
||||||
|
"enabled": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
→ Before fix: wiki_config NOT updated ❌
|
||||||
|
→ After fix: wiki_config updated ✅
|
||||||
|
```
|
||||||
|
|
||||||
|
## Implementation Time Estimate
|
||||||
|
- Add WikiConfig to KnowledgeBaseConfig: 2 minutes
|
||||||
|
- Update Service layer: 3 minutes
|
||||||
|
- Update Frontend types: 2 minutes
|
||||||
|
- Testing: 5 minutes
|
||||||
|
- **Total: ~12 minutes**
|
||||||
@@ -0,0 +1,354 @@
|
|||||||
|
# Wiki Config Flow - Trace Summary
|
||||||
|
|
||||||
|
## 📋 Executive Summary
|
||||||
|
|
||||||
|
I've completed a comprehensive trace of how `wiki_config` flows from the frontend through the backend to the database in the WeKnora codebase.
|
||||||
|
|
||||||
|
**Key Finding:** ❌ **Wiki config UPDATE is broken** - can only be set during creation, not updated afterward.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🎯 Three Documents Generated
|
||||||
|
|
||||||
|
### 1. **WIKI_CONFIG_FLOW_ANALYSIS.md** (16 KB)
|
||||||
|
**Comprehensive technical analysis** with all file paths, struct definitions, and code snippets.
|
||||||
|
|
||||||
|
**Sections:**
|
||||||
|
- Database layer (SQL migration)
|
||||||
|
- Type definitions (WikiConfig, KnowledgeBase, KnowledgeBaseConfig)
|
||||||
|
- Handler layer (create vs update)
|
||||||
|
- Service layer implementation
|
||||||
|
- Repository layer
|
||||||
|
- Frontend API
|
||||||
|
- Flow diagrams (works vs broken)
|
||||||
|
- Required fixes with code examples
|
||||||
|
- Summary table
|
||||||
|
|
||||||
|
**Best for:** Full understanding of the system architecture
|
||||||
|
|
||||||
|
### 2. **WIKI_CONFIG_QUICK_REFERENCE.md** (5.3 KB)
|
||||||
|
**Quick lookup guide** for developers implementing the fix.
|
||||||
|
|
||||||
|
**Sections:**
|
||||||
|
- Critical finding summary
|
||||||
|
- Why it's broken (struct diagram)
|
||||||
|
- Files that need changes
|
||||||
|
- Database path info
|
||||||
|
- Request/response flows
|
||||||
|
- Handler/Service code locations
|
||||||
|
- Frontend integration
|
||||||
|
- Test cases
|
||||||
|
|
||||||
|
**Best for:** Quick reference during implementation
|
||||||
|
|
||||||
|
### 3. **WIKI_CONFIG_FIX.md** (8.5 KB)
|
||||||
|
**Complete implementation guide** with exact code changes.
|
||||||
|
|
||||||
|
**Sections:**
|
||||||
|
- Problem statement
|
||||||
|
- Change 1: Type definition (code diff)
|
||||||
|
- Change 2: Service layer (code diff)
|
||||||
|
- Change 3: Frontend types (code diff)
|
||||||
|
- Testing procedures (curl examples)
|
||||||
|
- Verification checklist
|
||||||
|
- Code review checklist
|
||||||
|
- Rollback plan
|
||||||
|
- Impact analysis
|
||||||
|
|
||||||
|
**Best for:** Actually implementing the fix
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔍 Findings at a Glance
|
||||||
|
|
||||||
|
### Database Layer ✅
|
||||||
|
```
|
||||||
|
CREATE TABLE knowledge_bases (
|
||||||
|
...
|
||||||
|
wiki_config JSONB ← EXISTS and properly typed
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
**File:** `migrations/versioned/000032_wiki_pages.up.sql` (Line 7)
|
||||||
|
|
||||||
|
### Type Definitions
|
||||||
|
|
||||||
|
#### WikiConfig Struct ✅
|
||||||
|
```go
|
||||||
|
type WikiConfig struct {
|
||||||
|
Enabled bool
|
||||||
|
AutoIngest bool
|
||||||
|
SynthesisModelID string
|
||||||
|
WikiLanguage string
|
||||||
|
MaxPagesPerIngest int
|
||||||
|
}
|
||||||
|
```
|
||||||
|
**File:** `internal/types/wiki_page.go` (Lines 89-120)
|
||||||
|
|
||||||
|
#### KnowledgeBase Struct ✅
|
||||||
|
```go
|
||||||
|
type KnowledgeBase struct {
|
||||||
|
// ...
|
||||||
|
WikiConfig *WikiConfig `json:"wiki_config" gorm:"column:wiki_config;type:json"`
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
**File:** `internal/types/knowledgebase.go` (Line 76)
|
||||||
|
|
||||||
|
#### KnowledgeBaseConfig Struct ❌
|
||||||
|
```go
|
||||||
|
type KnowledgeBaseConfig struct {
|
||||||
|
ChunkingConfig ChunkingConfig
|
||||||
|
ImageProcessingConfig ImageProcessingConfig
|
||||||
|
FAQConfig *FAQConfig
|
||||||
|
// ❌ MISSING: WikiConfig *WikiConfig
|
||||||
|
}
|
||||||
|
```
|
||||||
|
**File:** `internal/types/knowledgebase.go` (Lines 99-107)
|
||||||
|
|
||||||
|
**This is the root cause!**
|
||||||
|
|
||||||
|
### Handler Layer
|
||||||
|
|
||||||
|
#### CreateKnowledgeBase ✅
|
||||||
|
- **File:** `internal/handler/knowledgebase.go` (Lines 114-147)
|
||||||
|
- Accepts full `types.KnowledgeBase` struct
|
||||||
|
- wiki_config can be sent: `{ name, wiki_config: {...} }`
|
||||||
|
- **Status:** Works perfectly
|
||||||
|
|
||||||
|
#### UpdateKnowledgeBase ❌
|
||||||
|
- **File:** `internal/handler/knowledgebase.go` (Lines 446-488)
|
||||||
|
- Accepts `UpdateKnowledgeBaseRequest` with `Config: KnowledgeBaseConfig`
|
||||||
|
- wiki_config in request: `{ name, config: { wiki_config: {...} } }`
|
||||||
|
- **Problem:** KnowledgeBaseConfig doesn't have WikiConfig field
|
||||||
|
- **Status:** Silently ignores wiki_config
|
||||||
|
|
||||||
|
### Service Layer
|
||||||
|
|
||||||
|
#### CreateKnowledgeBase ✅
|
||||||
|
- **File:** `internal/application/service/knowledgebase.go` (Lines 73-98)
|
||||||
|
- Receives full KnowledgeBase
|
||||||
|
- Calls `EnsureDefaults()` which validates wiki_config
|
||||||
|
- **Status:** Works
|
||||||
|
|
||||||
|
#### UpdateKnowledgeBase ❌
|
||||||
|
- **File:** `internal/application/service/knowledgebase.go` (Lines 265-311)
|
||||||
|
- Receives `config *KnowledgeBaseConfig`
|
||||||
|
- No code to update `kb.WikiConfig`
|
||||||
|
- **Status:** Cannot persist updates
|
||||||
|
|
||||||
|
### Repository Layer ✅
|
||||||
|
- **File:** `internal/application/repository/knowledgebase.go`
|
||||||
|
- `CreateKnowledgeBase()`: Line 26 - Uses GORM.Create()
|
||||||
|
- `UpdateKnowledgeBase()`: Line 116 - Uses GORM.Save()
|
||||||
|
- **Status:** Both correctly handle wiki_config if it's set
|
||||||
|
|
||||||
|
### Frontend API ⚠️
|
||||||
|
- **File:** `frontend/src/api/knowledge-base/index.ts`
|
||||||
|
- `createKnowledgeBase()`: Lines 11-33 - accepts any data ✅
|
||||||
|
- `updateKnowledgeBase()`: Lines 42-44 - accepts any config ✅
|
||||||
|
- **Issue:** Type hints don't include wiki_config (but data is sent)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔄 Flow Diagram
|
||||||
|
|
||||||
|
### ✅ CREATION FLOW (Works)
|
||||||
|
```
|
||||||
|
Frontend sends:
|
||||||
|
POST /api/v1/knowledge-bases
|
||||||
|
{ name, type, wiki_config: {...} }
|
||||||
|
|
||||||
|
↓
|
||||||
|
Handler: CreateKnowledgeBase
|
||||||
|
Parses as types.KnowledgeBase ✅
|
||||||
|
|
||||||
|
↓
|
||||||
|
Service: CreateKnowledgeBase
|
||||||
|
Receives KB with wiki_config ✅
|
||||||
|
Calls EnsureDefaults() ✅
|
||||||
|
|
||||||
|
↓
|
||||||
|
Repository: CreateKnowledgeBase
|
||||||
|
GORM.Create(kb) serializes all fields ✅
|
||||||
|
|
||||||
|
↓
|
||||||
|
Database: wiki_config column populated ✅
|
||||||
|
```
|
||||||
|
|
||||||
|
### ❌ UPDATE FLOW (Broken)
|
||||||
|
```
|
||||||
|
Frontend sends:
|
||||||
|
PUT /api/v1/knowledge-bases/{id}
|
||||||
|
{ name, config: { wiki_config: {...} } }
|
||||||
|
|
||||||
|
↓
|
||||||
|
Handler: UpdateKnowledgeBase
|
||||||
|
Parses as UpdateKnowledgeBaseRequest ❌
|
||||||
|
Config is KnowledgeBaseConfig (no WikiConfig!) ❌
|
||||||
|
|
||||||
|
↓
|
||||||
|
JSON Unmarshaling:
|
||||||
|
wiki_config field ignored ❌
|
||||||
|
|
||||||
|
↓
|
||||||
|
Service: UpdateKnowledgeBase
|
||||||
|
Receives config without wiki_config ❌
|
||||||
|
Never updates kb.WikiConfig ❌
|
||||||
|
|
||||||
|
↓
|
||||||
|
Repository: UpdateKnowledgeBase
|
||||||
|
GORM.Save(kb) persists OLD wiki_config ❌
|
||||||
|
|
||||||
|
↓
|
||||||
|
Database: wiki_config NOT updated ❌
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🛠️ The Fix (3 Changes)
|
||||||
|
|
||||||
|
### Change 1: Add WikiConfig to KnowledgeBaseConfig
|
||||||
|
**File:** `internal/types/knowledgebase.go` (After line 106)
|
||||||
|
```go
|
||||||
|
// Wiki configuration (for document type knowledge bases with wiki feature enabled)
|
||||||
|
WikiConfig *WikiConfig `yaml:"wiki_config" json:"wiki_config"`
|
||||||
|
```
|
||||||
|
|
||||||
|
### Change 2: Update Service to Persist WikiConfig
|
||||||
|
**File:** `internal/application/service/knowledgebase.go` (After line 296)
|
||||||
|
```go
|
||||||
|
if config.WikiConfig != nil {
|
||||||
|
kb.WikiConfig = config.WikiConfig
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Change 3: Update Frontend Type Hints
|
||||||
|
**File:** `frontend/src/api/knowledge-base/index.ts` (In createKnowledgeBase)
|
||||||
|
```typescript
|
||||||
|
wiki_config?: {
|
||||||
|
enabled: boolean;
|
||||||
|
auto_ingest?: boolean;
|
||||||
|
synthesis_model_id?: string;
|
||||||
|
wiki_language?: string;
|
||||||
|
max_pages_per_ingest?: number;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📊 Component Status Matrix
|
||||||
|
|
||||||
|
| Component | File | Lines | Current Status | Issue |
|
||||||
|
|-----------|------|-------|--------|---------|
|
||||||
|
| Database Column | `000032_wiki_pages.up.sql` | 7 | ✅ Exists | None |
|
||||||
|
| WikiConfig Type | `wiki_page.go` | 89-120 | ✅ Defined | None |
|
||||||
|
| KnowledgeBase Field | `knowledgebase.go` | 76 | ✅ Has field | None |
|
||||||
|
| KnowledgeBaseConfig Field | `knowledgebase.go` | 99-107 | ❌ Missing | **ROOT CAUSE** |
|
||||||
|
| CreateKB Handler | `knowledgebase.go` | 114-147 | ✅ Works | None |
|
||||||
|
| UpdateKB Handler | `knowledgebase.go` | 446-488 | ⚠️ Limited | Uses KnowledgeBaseConfig |
|
||||||
|
| CreateKB Service | `knowledgebase.go` | 73-98 | ✅ Works | None |
|
||||||
|
| UpdateKB Service | `knowledgebase.go` | 265-311 | ❌ Broken | No WikiConfig update |
|
||||||
|
| Repository | `repository/knowledgebase.go` | 26-118 | ✅ Works | None |
|
||||||
|
| Frontend API | `api/knowledge-base/index.ts` | Various | ⚠️ Partial | Missing types |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🧪 Test Verification
|
||||||
|
|
||||||
|
### Before Fix
|
||||||
|
- ✅ Create KB with wiki_config → Works
|
||||||
|
- ❌ Update wiki_config → Fails (not persisted)
|
||||||
|
- ❌ Fetch KB after update → Old value still there
|
||||||
|
|
||||||
|
### After Fix
|
||||||
|
- ✅ Create KB with wiki_config → Works
|
||||||
|
- ✅ Update wiki_config → Works
|
||||||
|
- ✅ Fetch KB after update → New value correctly persisted
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📁 Document Files Location
|
||||||
|
|
||||||
|
All documents saved in WeKnora root:
|
||||||
|
```
|
||||||
|
/Users/wizard/code/go/src/git.woa.com/wxg-prc/WeKnora/
|
||||||
|
├── WIKI_CONFIG_TRACE_SUMMARY.md ← You are here
|
||||||
|
├── WIKI_CONFIG_FLOW_ANALYSIS.md ← Full technical analysis
|
||||||
|
├── WIKI_CONFIG_QUICK_REFERENCE.md ← Developer quick reference
|
||||||
|
└── WIKI_CONFIG_FIX.md ← Implementation guide
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🎓 Key Learnings
|
||||||
|
|
||||||
|
### Architecture Pattern
|
||||||
|
WeKnora uses a layered architecture:
|
||||||
|
1. **Frontend API** - TypeScript functions that call HTTP endpoints
|
||||||
|
2. **Handler Layer** - Gin HTTP handlers that parse requests
|
||||||
|
3. **Service Layer** - Business logic
|
||||||
|
4. **Repository Layer** - Database operations using GORM
|
||||||
|
5. **Database** - PostgreSQL with JSON columns
|
||||||
|
|
||||||
|
### Request Structure Design
|
||||||
|
- **Creation**: Uses full struct (`KnowledgeBase`) - more flexible
|
||||||
|
- **Update**: Uses limited struct (`KnowledgeBaseConfig`) - more controlled
|
||||||
|
- **Issue**: Limited struct was too limited for wiki_config
|
||||||
|
|
||||||
|
### GORM Behavior
|
||||||
|
- GORM automatically serializes/deserializes JSON fields using `Value()` and `Scan()` methods
|
||||||
|
- All config structs implement these interfaces
|
||||||
|
- The repository layer is "dumb" - it trusts what the service gives it
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ✨ Recommendations
|
||||||
|
|
||||||
|
1. **Implement the fix immediately** - Very low risk, high impact
|
||||||
|
2. **Add integration tests** - Test both creation and update flows
|
||||||
|
3. **Update API documentation** - Document wiki_config in API docs
|
||||||
|
4. **Consider using base struct for all config updates** - Currently inconsistent between handlers
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📞 Questions Answered
|
||||||
|
|
||||||
|
### 1. How does wiki_config get saved during KB creation?
|
||||||
|
Via full `KnowledgeBase` struct → Service → Repository → GORM.Create()
|
||||||
|
|
||||||
|
### 2. How does it get updated after creation?
|
||||||
|
It **doesn't** - that's the bug! KnowledgeBaseConfig is missing the field.
|
||||||
|
|
||||||
|
### 3. Where is it stored in the database?
|
||||||
|
`knowledge_bases.wiki_config` JSONB column
|
||||||
|
|
||||||
|
### 4. What struct definitions are involved?
|
||||||
|
- `WikiConfig` (source)
|
||||||
|
- `KnowledgeBase` (has WikiConfig field)
|
||||||
|
- `KnowledgeBaseConfig` (missing WikiConfig field - THE BUG)
|
||||||
|
|
||||||
|
### 5. What's the exact code path from frontend to database?
|
||||||
|
- Frontend sends HTTP request
|
||||||
|
- Handler parses request into struct
|
||||||
|
- Service updates the struct
|
||||||
|
- Repository calls GORM.Save()
|
||||||
|
- GORM serializes struct to SQL using Value() method
|
||||||
|
- Database receives JSON in wiki_config column
|
||||||
|
|
||||||
|
### 6. How to fix it?
|
||||||
|
Add WikiConfig field to KnowledgeBaseConfig + update service layer (2 code changes)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📝 Next Steps
|
||||||
|
|
||||||
|
1. **Review** the three generated documents
|
||||||
|
2. **Implement** the changes using `WIKI_CONFIG_FIX.md`
|
||||||
|
3. **Test** using the provided test cases
|
||||||
|
4. **Deploy** and verify in production
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Generated: April 7, 2026
|
||||||
|
Analysis Depth: Complete code trace with exact file locations and line numbers
|
||||||
@@ -1587,7 +1587,6 @@ export default {
|
|||||||
typeLabel: 'Knowledge Base Type',
|
typeLabel: 'Knowledge Base Type',
|
||||||
typeDocument: 'Document-based',
|
typeDocument: 'Document-based',
|
||||||
typeFAQ: 'FAQ Q&A',
|
typeFAQ: 'FAQ Q&A',
|
||||||
typeWiki: 'Wiki',
|
|
||||||
typeDescription: 'FAQ suits structured Q&A datasets; document type supports file parsing and chunking; Wiki type auto-builds interlinked knowledge pages via LLM.',
|
typeDescription: 'FAQ suits structured Q&A datasets; document type supports file parsing and chunking; Wiki type auto-builds interlinked knowledge pages via LLM.',
|
||||||
nameLabel: 'Knowledge Base Name',
|
nameLabel: 'Knowledge Base Name',
|
||||||
namePlaceholder: 'Enter knowledge base name',
|
namePlaceholder: 'Enter knowledge base name',
|
||||||
@@ -1596,7 +1595,9 @@ export default {
|
|||||||
},
|
},
|
||||||
wiki: {
|
wiki: {
|
||||||
title: 'Wiki Settings',
|
title: 'Wiki Settings',
|
||||||
description: 'Configure Wiki knowledge base auto-generation and language preferences',
|
description: 'Configure Wiki auto-generation preferences',
|
||||||
|
enableLabel: 'Enable Wiki',
|
||||||
|
enableTip: 'When enabled, uploading documents will auto-generate interlinked wiki knowledge pages',
|
||||||
autoIngestLabel: 'Auto Ingest',
|
autoIngestLabel: 'Auto Ingest',
|
||||||
autoIngestTip: 'When enabled, uploading new documents automatically triggers Wiki page generation/update',
|
autoIngestTip: 'When enabled, uploading new documents automatically triggers Wiki page generation/update',
|
||||||
synthesisModelLabel: 'Synthesis Model',
|
synthesisModelLabel: 'Synthesis Model',
|
||||||
@@ -1606,6 +1607,34 @@ export default {
|
|||||||
maxPagesLabel: 'Max Pages Per Ingest',
|
maxPagesLabel: 'Max Pages Per Ingest',
|
||||||
maxPagesTip: 'Maximum number of pages to create/update per ingest (0 = no limit)',
|
maxPagesTip: 'Maximum number of pages to create/update per ingest (0 = no limit)',
|
||||||
},
|
},
|
||||||
|
wikiBrowser: {
|
||||||
|
tabDocuments: 'Documents',
|
||||||
|
tabWiki: 'Wiki',
|
||||||
|
tabGraph: 'Graph',
|
||||||
|
tabGraph: 'Graph',
|
||||||
|
searchPlaceholder: 'Search wiki pages...',
|
||||||
|
filterAll: 'All Types',
|
||||||
|
filterSummary: 'Summaries',
|
||||||
|
filterEntity: 'Entities',
|
||||||
|
filterConcept: 'Concepts',
|
||||||
|
filterSynthesis: 'Synthesis',
|
||||||
|
filterComparison: 'Comparisons',
|
||||||
|
viewReader: 'Reader',
|
||||||
|
viewGraph: 'Graph',
|
||||||
|
stats: '{pages} pages · {links} links',
|
||||||
|
emptyTitle: 'No wiki pages yet',
|
||||||
|
emptyDesc: 'Upload documents with Wiki enabled to auto-generate knowledge pages',
|
||||||
|
selectPageHint: 'Select a page from the left to view its content',
|
||||||
|
version: 'v{ver}',
|
||||||
|
linksTo: 'Links to',
|
||||||
|
linkedFrom: 'Linked from',
|
||||||
|
sources: 'Source documents',
|
||||||
|
recentActivity: 'Recent Activity',
|
||||||
|
graphEmpty: 'Loading graph...',
|
||||||
|
logTitle: 'Activity Log',
|
||||||
|
graphNoData: 'No graph data yet. Upload documents first.',
|
||||||
|
healthCheck: 'Health Check',
|
||||||
|
},
|
||||||
buttons: {
|
buttons: {
|
||||||
create: 'Create Knowledge Base',
|
create: 'Create Knowledge Base',
|
||||||
save: 'Save Configuration'
|
save: 'Save Configuration'
|
||||||
|
|||||||
@@ -2110,7 +2110,6 @@ export default {
|
|||||||
typeLabel: "지식베이스 유형",
|
typeLabel: "지식베이스 유형",
|
||||||
typeDocument: "문서",
|
typeDocument: "문서",
|
||||||
typeFAQ: "Q&A",
|
typeFAQ: "Q&A",
|
||||||
typeWiki: "Wiki",
|
|
||||||
typeDescription:
|
typeDescription:
|
||||||
"FAQ 유형은 구조화된 Q&A 데이터에 적합합니다. 문서 유형은 파일 파싱과 청킹을 지원합니다. Wiki 유형은 LLM을 통해 상호 연결된 지식 페이지를 자동으로 구축합니다.",
|
"FAQ 유형은 구조화된 Q&A 데이터에 적합합니다. 문서 유형은 파일 파싱과 청킹을 지원합니다. Wiki 유형은 LLM을 통해 상호 연결된 지식 페이지를 자동으로 구축합니다.",
|
||||||
nameLabel: "지식베이스 이름",
|
nameLabel: "지식베이스 이름",
|
||||||
@@ -2120,7 +2119,9 @@ export default {
|
|||||||
},
|
},
|
||||||
wiki: {
|
wiki: {
|
||||||
title: "Wiki 설정",
|
title: "Wiki 설정",
|
||||||
description: "Wiki 지식베이스의 자동 생성 및 언어 환경설정을 구성합니다",
|
description: "Wiki 자동 생성 환경설정을 구성합니다",
|
||||||
|
enableLabel: "Wiki 활성화",
|
||||||
|
enableTip: "활성화하면 문서 업로드 시 자동으로 상호 연결된 Wiki 지식 페이지가 생성됩니다",
|
||||||
autoIngestLabel: "자동 Ingest",
|
autoIngestLabel: "자동 Ingest",
|
||||||
autoIngestTip: "활성화하면 새 문서 업로드 시 자동으로 Wiki 페이지가 생성/업데이트됩니다",
|
autoIngestTip: "활성화하면 새 문서 업로드 시 자동으로 Wiki 페이지가 생성/업데이트됩니다",
|
||||||
synthesisModelLabel: "합성 모델",
|
synthesisModelLabel: "합성 모델",
|
||||||
@@ -2130,6 +2131,33 @@ export default {
|
|||||||
maxPagesLabel: "Ingest당 최대 페이지 수",
|
maxPagesLabel: "Ingest당 최대 페이지 수",
|
||||||
maxPagesTip: "Ingest당 생성/업데이트할 최대 페이지 수 (0 = 제한 없음)",
|
maxPagesTip: "Ingest당 생성/업데이트할 최대 페이지 수 (0 = 제한 없음)",
|
||||||
},
|
},
|
||||||
|
wikiBrowser: {
|
||||||
|
tabDocuments: "문서",
|
||||||
|
tabWiki: "Wiki",
|
||||||
|
tabGraph: "그래프",
|
||||||
|
searchPlaceholder: "Wiki 페이지 검색...",
|
||||||
|
filterAll: "전체 유형",
|
||||||
|
filterSummary: "요약",
|
||||||
|
filterEntity: "엔티티",
|
||||||
|
filterConcept: "개념",
|
||||||
|
filterSynthesis: "종합",
|
||||||
|
filterComparison: "비교",
|
||||||
|
viewReader: "리더",
|
||||||
|
viewGraph: "그래프",
|
||||||
|
stats: "페이지 {pages}개 · 링크 {links}개",
|
||||||
|
emptyTitle: "Wiki 페이지가 없습니다",
|
||||||
|
emptyDesc: "Wiki를 활성화하고 문서를 업로드하면 지식 페이지가 자동 생성됩니다",
|
||||||
|
selectPageHint: "왼쪽에서 페이지를 선택하여 내용을 확인하세요",
|
||||||
|
version: "v{ver}",
|
||||||
|
linksTo: "링크 대상",
|
||||||
|
linkedFrom: "링크 출처",
|
||||||
|
sources: "원본 문서",
|
||||||
|
recentActivity: "최근 활동",
|
||||||
|
graphEmpty: "그래프 로딩 중...",
|
||||||
|
logTitle: '활동 로그',
|
||||||
|
graphNoData: '그래프 데이터가 없습니다. 먼저 문서를 업로드하세요.',
|
||||||
|
healthCheck: "상태 점검",
|
||||||
|
},
|
||||||
buttons: {
|
buttons: {
|
||||||
create: "지식베이스 생성",
|
create: "지식베이스 생성",
|
||||||
save: "설정 저장",
|
save: "설정 저장",
|
||||||
|
|||||||
@@ -1790,7 +1790,6 @@ export default {
|
|||||||
typeLabel: 'Тип базы знаний',
|
typeLabel: 'Тип базы знаний',
|
||||||
typeDocument: 'Документальная',
|
typeDocument: 'Документальная',
|
||||||
typeFAQ: 'FAQ (вопрос-ответ)',
|
typeFAQ: 'FAQ (вопрос-ответ)',
|
||||||
typeWiki: 'Wiki',
|
|
||||||
typeDescription: 'FAQ подходит для структурированных Q&A; документальный тип поддерживает загрузку файлов и разбиение; Wiki автоматически создает связанные страницы знаний через LLM.',
|
typeDescription: 'FAQ подходит для структурированных Q&A; документальный тип поддерживает загрузку файлов и разбиение; Wiki автоматически создает связанные страницы знаний через LLM.',
|
||||||
nameLabel: 'Название базы знаний',
|
nameLabel: 'Название базы знаний',
|
||||||
namePlaceholder: 'Введите название базы знаний',
|
namePlaceholder: 'Введите название базы знаний',
|
||||||
@@ -1799,7 +1798,9 @@ export default {
|
|||||||
},
|
},
|
||||||
wiki: {
|
wiki: {
|
||||||
title: 'Wiki настройки',
|
title: 'Wiki настройки',
|
||||||
description: 'Настройте автоматическую генерацию и язык Wiki',
|
description: 'Настройте автоматическую генерацию Wiki',
|
||||||
|
enableLabel: 'Включить Wiki',
|
||||||
|
enableTip: 'При включении загрузка документов автоматически создаст связанные Wiki-страницы',
|
||||||
autoIngestLabel: 'Автоматический Ingest',
|
autoIngestLabel: 'Автоматический Ingest',
|
||||||
autoIngestTip: 'При загрузке новых документов автоматически создаются/обновляются Wiki-страницы',
|
autoIngestTip: 'При загрузке новых документов автоматически создаются/обновляются Wiki-страницы',
|
||||||
synthesisModelLabel: 'Модель синтеза',
|
synthesisModelLabel: 'Модель синтеза',
|
||||||
@@ -1809,6 +1810,34 @@ export default {
|
|||||||
maxPagesLabel: 'Макс. страниц за Ingest',
|
maxPagesLabel: 'Макс. страниц за Ingest',
|
||||||
maxPagesTip: 'Максимальное количество страниц для создания/обновления за одну операцию (0 = без ограничения)',
|
maxPagesTip: 'Максимальное количество страниц для создания/обновления за одну операцию (0 = без ограничения)',
|
||||||
},
|
},
|
||||||
|
wikiBrowser: {
|
||||||
|
tabDocuments: 'Документы',
|
||||||
|
tabWiki: 'Wiki',
|
||||||
|
tabGraph: 'Граф',
|
||||||
|
tabGraph: 'Граф',
|
||||||
|
searchPlaceholder: 'Поиск Wiki-страниц...',
|
||||||
|
filterAll: 'Все типы',
|
||||||
|
filterSummary: 'Резюме',
|
||||||
|
filterEntity: 'Сущности',
|
||||||
|
filterConcept: 'Концепции',
|
||||||
|
filterSynthesis: 'Синтез',
|
||||||
|
filterComparison: 'Сравнения',
|
||||||
|
viewReader: 'Читатель',
|
||||||
|
viewGraph: 'Граф',
|
||||||
|
stats: '{pages} страниц · {links} ссылок',
|
||||||
|
emptyTitle: 'Wiki-страниц пока нет',
|
||||||
|
emptyDesc: 'Загрузите документы с включённым Wiki для автоматической генерации страниц',
|
||||||
|
selectPageHint: 'Выберите страницу слева для просмотра',
|
||||||
|
version: 'v{ver}',
|
||||||
|
linksTo: 'Ссылается на',
|
||||||
|
linkedFrom: 'Цитируется',
|
||||||
|
sources: 'Исходные документы',
|
||||||
|
recentActivity: 'Последняя активность',
|
||||||
|
graphEmpty: 'Загрузка графа...',
|
||||||
|
logTitle: 'Журнал активности',
|
||||||
|
graphNoData: 'Нет данных графа. Сначала загрузите документы.',
|
||||||
|
healthCheck: 'Проверка здоровья',
|
||||||
|
},
|
||||||
buttons: {
|
buttons: {
|
||||||
create: 'Создать базу знаний',
|
create: 'Создать базу знаний',
|
||||||
save: 'Сохранить настройки'
|
save: 'Сохранить настройки'
|
||||||
|
|||||||
@@ -2094,7 +2094,6 @@ export default {
|
|||||||
typeLabel: "知识库类型",
|
typeLabel: "知识库类型",
|
||||||
typeDocument: "文档",
|
typeDocument: "文档",
|
||||||
typeFAQ: "问答",
|
typeFAQ: "问答",
|
||||||
typeWiki: "Wiki",
|
|
||||||
typeDescription: "FAQ 类型适合结构化问答数据;文档型支持文件解析与分块;Wiki 类型由 LLM 自动构建互链知识页面。",
|
typeDescription: "FAQ 类型适合结构化问答数据;文档型支持文件解析与分块;Wiki 类型由 LLM 自动构建互链知识页面。",
|
||||||
nameLabel: "知识库名称",
|
nameLabel: "知识库名称",
|
||||||
namePlaceholder: "请输入知识库名称",
|
namePlaceholder: "请输入知识库名称",
|
||||||
@@ -2103,7 +2102,9 @@ export default {
|
|||||||
},
|
},
|
||||||
wiki: {
|
wiki: {
|
||||||
title: "Wiki 设置",
|
title: "Wiki 设置",
|
||||||
description: "配置 Wiki 知识库的自动生成和语言偏好",
|
description: "配置 Wiki 知识库的自动生成偏好",
|
||||||
|
enableLabel: "启用 Wiki",
|
||||||
|
enableTip: "开启后,上传文档时将自动生成互相关联的 Wiki 知识页面",
|
||||||
autoIngestLabel: "自动 Ingest",
|
autoIngestLabel: "自动 Ingest",
|
||||||
autoIngestTip: "开启后,新文档上传后自动触发 Wiki 页面生成/更新",
|
autoIngestTip: "开启后,新文档上传后自动触发 Wiki 页面生成/更新",
|
||||||
synthesisModelLabel: "合成模型",
|
synthesisModelLabel: "合成模型",
|
||||||
@@ -2113,6 +2114,33 @@ export default {
|
|||||||
maxPagesLabel: "单次最大页面数",
|
maxPagesLabel: "单次最大页面数",
|
||||||
maxPagesTip: "每次 Ingest 最多创建/更新的页面数(0 表示不限制)",
|
maxPagesTip: "每次 Ingest 最多创建/更新的页面数(0 表示不限制)",
|
||||||
},
|
},
|
||||||
|
wikiBrowser: {
|
||||||
|
tabDocuments: "文档",
|
||||||
|
tabWiki: "Wiki",
|
||||||
|
tabGraph: "图谱",
|
||||||
|
searchPlaceholder: "搜索 Wiki 页面...",
|
||||||
|
filterAll: "全部类型",
|
||||||
|
filterSummary: "摘要",
|
||||||
|
filterEntity: "实体",
|
||||||
|
filterConcept: "概念",
|
||||||
|
filterSynthesis: "综合",
|
||||||
|
filterComparison: "对比",
|
||||||
|
viewReader: "阅读器",
|
||||||
|
viewGraph: "图谱",
|
||||||
|
stats: "{pages} 个页面 · {links} 个链接",
|
||||||
|
emptyTitle: "暂无 Wiki 页面",
|
||||||
|
emptyDesc: "上传文档并启用 Wiki 后将自动生成知识页面",
|
||||||
|
selectPageHint: "从左侧选择一个页面查看内容",
|
||||||
|
version: "v{ver}",
|
||||||
|
linksTo: "链接到",
|
||||||
|
linkedFrom: "被链接",
|
||||||
|
sources: "来源文档",
|
||||||
|
recentActivity: "最近活动",
|
||||||
|
graphEmpty: "加载图谱中...",
|
||||||
|
logTitle: '操作日志',
|
||||||
|
graphNoData: '暂无图谱数据,请先上传文档',
|
||||||
|
healthCheck: "健康检查",
|
||||||
|
},
|
||||||
buttons: {
|
buttons: {
|
||||||
create: "创建知识库",
|
create: "创建知识库",
|
||||||
save: "保存配置",
|
save: "保存配置",
|
||||||
|
|||||||
@@ -45,7 +45,11 @@ const uploading = ref(false);
|
|||||||
const kbLoading = ref(false);
|
const kbLoading = ref(false);
|
||||||
const docListLoading = ref(true);
|
const docListLoading = ref(true);
|
||||||
const isFAQ = computed(() => (kbInfo.value?.type || '') === 'faq');
|
const isFAQ = computed(() => (kbInfo.value?.type || '') === 'faq');
|
||||||
const isWiki = computed(() => (kbInfo.value?.type || '') === 'wiki');
|
const isWiki = computed(() => !!kbInfo.value?.wiki_config?.enabled);
|
||||||
|
const validTabs = ['documents', 'wiki', 'graph'] as const
|
||||||
|
type KbTab = typeof validTabs[number]
|
||||||
|
const initTab = validTabs.includes(route.query.tab as any) ? (route.query.tab as KbTab) : 'documents'
|
||||||
|
const activeKbTab = ref<KbTab>(initTab);
|
||||||
const missingStorageEngine = computed(() => {
|
const missingStorageEngine = computed(() => {
|
||||||
if (!kbInfo.value || isFAQ.value) return false
|
if (!kbInfo.value || isFAQ.value) return false
|
||||||
const spc = kbInfo.value.storage_provider_config
|
const spc = kbInfo.value.storage_provider_config
|
||||||
@@ -606,6 +610,17 @@ const loadKnowledgeList = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 监听路由参数变化,重新获取知识库内容
|
// 监听路由参数变化,重新获取知识库内容
|
||||||
|
// Sync activeKbTab to URL query so it survives page refresh
|
||||||
|
watch(activeKbTab, (tab) => {
|
||||||
|
const query = { ...route.query }
|
||||||
|
if (tab === 'documents') {
|
||||||
|
delete query.tab
|
||||||
|
} else {
|
||||||
|
query.tab = tab
|
||||||
|
}
|
||||||
|
router.replace({ query })
|
||||||
|
})
|
||||||
|
|
||||||
watch(() => kbId.value, (newKbId, oldKbId) => {
|
watch(() => kbId.value, (newKbId, oldKbId) => {
|
||||||
if (newKbId && newKbId !== oldKbId) {
|
if (newKbId && newKbId !== oldKbId) {
|
||||||
tagSearchQuery.value = '';
|
tagSearchQuery.value = '';
|
||||||
@@ -835,6 +850,12 @@ const openCardDetails = (item: KnowledgeCard) => {
|
|||||||
getCardDetails(item);
|
getCardDetails(item);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Open source document preview from WikiBrowser
|
||||||
|
const openSourceDoc = (knowledgeId: string) => {
|
||||||
|
isCardDetails.value = true;
|
||||||
|
getCardDetails({ id: knowledgeId });
|
||||||
|
};
|
||||||
|
|
||||||
// 悬停知识卡片时跟随鼠标显示详情气泡
|
// 悬停知识卡片时跟随鼠标显示详情气泡
|
||||||
const hoveredCardItem = ref<KnowledgeCard | null>(null);
|
const hoveredCardItem = ref<KnowledgeCard | null>(null);
|
||||||
const cardPopoverPos = ref({ x: 0, y: 0 });
|
const cardPopoverPos = ref({ x: 0, y: 0 });
|
||||||
@@ -1532,12 +1553,7 @@ async function createNewSession(value: string): Promise<void> {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<template v-if="isWiki">
|
<template v-if="!isFAQ">
|
||||||
<div class="wiki-manager-wrapper" style="padding: 0;">
|
|
||||||
<WikiBrowser v-if="kbId" :knowledge-base-id="kbId" />
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
<template v-else-if="!isFAQ">
|
|
||||||
<div class="knowledge-layout">
|
<div class="knowledge-layout">
|
||||||
<div class="document-header">
|
<div class="document-header">
|
||||||
<div class="document-header-title">
|
<div class="document-header-title">
|
||||||
@@ -1584,7 +1600,23 @@ async function createNewSession(value: string): Promise<void> {
|
|||||||
</template>
|
</template>
|
||||||
</button>
|
</button>
|
||||||
<t-icon name="chevron-right" class="breadcrumb-separator" />
|
<t-icon name="chevron-right" class="breadcrumb-separator" />
|
||||||
<span class="breadcrumb-current">{{ $t('knowledgeEditor.document.title') }}</span>
|
<template v-if="isWiki">
|
||||||
|
<span
|
||||||
|
:class="['breadcrumb-tab', { active: activeKbTab === 'documents' }]"
|
||||||
|
@click="activeKbTab = 'documents'"
|
||||||
|
>{{ $t('knowledgeEditor.wikiBrowser.tabDocuments') }}</span>
|
||||||
|
<span class="breadcrumb-tab-sep">/</span>
|
||||||
|
<span
|
||||||
|
:class="['breadcrumb-tab', { active: activeKbTab === 'wiki' }]"
|
||||||
|
@click="activeKbTab = 'wiki'"
|
||||||
|
>Wiki</span>
|
||||||
|
<span class="breadcrumb-tab-sep">/</span>
|
||||||
|
<span
|
||||||
|
:class="['breadcrumb-tab', { active: activeKbTab === 'graph' }]"
|
||||||
|
@click="activeKbTab = 'graph'"
|
||||||
|
>{{ $t('knowledgeEditor.wikiBrowser.tabGraph') }}</span>
|
||||||
|
</template>
|
||||||
|
<span v-else class="breadcrumb-current">{{ $t('knowledgeEditor.document.title') }}</span>
|
||||||
</h2>
|
</h2>
|
||||||
<!-- 身份与最后更新:紧凑单行,置于标题行右侧,悬停显示权限说明 -->
|
<!-- 身份与最后更新:紧凑单行,置于标题行右侧,悬停显示权限说明 -->
|
||||||
<div v-if="kbInfo && !authStore.isLiteMode" class="kb-access-meta">
|
<div v-if="kbInfo && !authStore.isLiteMode" class="kb-access-meta">
|
||||||
@@ -1635,7 +1667,13 @@ async function createNewSession(value: string): Promise<void> {
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Wiki Browser / Graph (shown when wiki or graph tab is active) -->
|
||||||
|
<div v-if="isWiki && (activeKbTab === 'wiki' || activeKbTab === 'graph')" class="wiki-main-area">
|
||||||
|
<WikiBrowser v-if="kbId" :knowledge-base-id="kbId" :view="activeKbTab === 'graph' ? 'graph' : 'browser'" @open-source-doc="openSourceDoc" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<template v-if="activeKbTab === 'documents' || !isWiki">
|
||||||
<input
|
<input
|
||||||
ref="uploadInputRef"
|
ref="uploadInputRef"
|
||||||
type="file"
|
type="file"
|
||||||
@@ -2181,10 +2219,13 @@ async function createNewSession(value: string): Promise<void> {
|
|||||||
<div class="url-input-tip">{{ $t('knowledgeBase.urlTip') }}</div>
|
<div class="url-input-tip">{{ $t('knowledgeBase.urlTip') }}</div>
|
||||||
</div>
|
</div>
|
||||||
</t-dialog>
|
</t-dialog>
|
||||||
|
|
||||||
<DocContent :visible="isCardDetails" :details="details" @closeDoc="closeDoc" @getDoc="getDoc"></DocContent>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- DocContent drawer (shared by documents tab and wiki source refs) -->
|
||||||
|
<DocContent :visible="isCardDetails" :details="details" @closeDoc="closeDoc" @getDoc="getDoc"></DocContent>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
@@ -2220,6 +2261,35 @@ async function createNewSession(value: string): Promise<void> {
|
|||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Breadcrumb tab switch (文档/Wiki in breadcrumb)
|
||||||
|
.breadcrumb-tab {
|
||||||
|
cursor: pointer;
|
||||||
|
color: var(--td-text-color-placeholder);
|
||||||
|
font-weight: 400;
|
||||||
|
transition: color 0.15s;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
color: var(--td-text-color-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
&.active {
|
||||||
|
color: var(--td-brand-color);
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.breadcrumb-tab-sep {
|
||||||
|
margin: 0 6px;
|
||||||
|
color: var(--td-text-color-disabled);
|
||||||
|
font-weight: 400;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wiki-main-area {
|
||||||
|
flex: 1;
|
||||||
|
min-height: 0;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
// 与列表页一致:浅灰底圆角区,左侧筛选为白底卡片
|
// 与列表页一致:浅灰底圆角区,左侧筛选为白底卡片
|
||||||
.knowledge-main {
|
.knowledge-main {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|||||||
@@ -49,7 +49,6 @@
|
|||||||
>
|
>
|
||||||
<t-radio-button value="document">{{ $t('knowledgeEditor.basic.typeDocument') }}</t-radio-button>
|
<t-radio-button value="document">{{ $t('knowledgeEditor.basic.typeDocument') }}</t-radio-button>
|
||||||
<t-radio-button value="faq">{{ $t('knowledgeEditor.basic.typeFAQ') }}</t-radio-button>
|
<t-radio-button value="faq">{{ $t('knowledgeEditor.basic.typeFAQ') }}</t-radio-button>
|
||||||
<t-radio-button value="wiki">{{ $t('knowledgeEditor.basic.typeWiki') }}</t-radio-button>
|
|
||||||
</t-radio-group>
|
</t-radio-group>
|
||||||
<p class="form-tip">{{ $t('knowledgeEditor.basic.typeDescription') }}</p>
|
<p class="form-tip">{{ $t('knowledgeEditor.basic.typeDescription') }}</p>
|
||||||
</div>
|
</div>
|
||||||
@@ -122,48 +121,73 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Wiki 配置 -->
|
<!-- Wiki 配置 -->
|
||||||
<div v-if="isWiki && formData" v-show="currentSection === 'wiki'" class="section">
|
<div v-if="!isFAQ && formData" v-show="currentSection === 'wiki'" class="section">
|
||||||
<div class="section-content">
|
<div v-if="formData" class="kb-multimodal-settings">
|
||||||
<div class="section-header">
|
<div class="section-header">
|
||||||
<h3 class="section-title">{{ $t('knowledgeEditor.wiki.title') }}</h3>
|
<h2>{{ $t('knowledgeEditor.wiki.title') }}</h2>
|
||||||
<p class="section-desc">{{ $t('knowledgeEditor.wiki.description') }}</p>
|
<p class="section-description">{{ $t('knowledgeEditor.wiki.description') }}</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section-body">
|
|
||||||
<div class="form-item">
|
<div class="settings-group">
|
||||||
<label class="form-label">{{ $t('knowledgeEditor.wiki.autoIngestLabel') }}</label>
|
<!-- Wiki 开关 -->
|
||||||
<t-switch v-model="formData.wikiConfig.autoIngest" size="medium" />
|
<div class="setting-row">
|
||||||
<p class="form-tip">{{ $t('knowledgeEditor.wiki.autoIngestTip') }}</p>
|
<div class="setting-info">
|
||||||
</div>
|
<label>{{ $t('knowledgeEditor.wiki.enableLabel') }}</label>
|
||||||
<div class="form-item">
|
<p class="desc">{{ $t('knowledgeEditor.wiki.enableTip') }}</p>
|
||||||
<label class="form-label">{{ $t('knowledgeEditor.wiki.synthesisModelLabel') }}</label>
|
</div>
|
||||||
<ModelSelector
|
<div class="setting-control">
|
||||||
model-type="LLM"
|
<t-switch v-model="formData.wikiConfig.enabled" size="medium" />
|
||||||
:selected-model-id="formData.wikiConfig.synthesisModelId"
|
</div>
|
||||||
:all-models="allModels"
|
|
||||||
@update:selected-model-id="(val: string) => { if (formData) formData.wikiConfig.synthesisModelId = val }"
|
|
||||||
:placeholder="$t('knowledgeEditor.wiki.synthesisModelPlaceholder')"
|
|
||||||
/>
|
|
||||||
<p class="form-tip">{{ $t('knowledgeEditor.wiki.synthesisModelTip') }}</p>
|
|
||||||
</div>
|
|
||||||
<div class="form-item">
|
|
||||||
<label class="form-label">{{ $t('knowledgeEditor.wiki.languageLabel') }}</label>
|
|
||||||
<t-radio-group v-model="formData.wikiConfig.wikiLanguage">
|
|
||||||
<t-radio-button value="auto">Auto</t-radio-button>
|
|
||||||
<t-radio-button value="zh">中文</t-radio-button>
|
|
||||||
<t-radio-button value="en">English</t-radio-button>
|
|
||||||
</t-radio-group>
|
|
||||||
</div>
|
|
||||||
<div class="form-item">
|
|
||||||
<label class="form-label">{{ $t('knowledgeEditor.wiki.maxPagesLabel') }}</label>
|
|
||||||
<t-input-number v-model="formData.wikiConfig.maxPagesPerIngest" :min="0" :max="50" />
|
|
||||||
<p class="form-tip">{{ $t('knowledgeEditor.wiki.maxPagesTip') }}</p>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- 以下仅在 wiki 开启时显示 -->
|
||||||
|
<template v-if="formData.wikiConfig.enabled">
|
||||||
|
<!-- 自动 Ingest -->
|
||||||
|
<div class="setting-row">
|
||||||
|
<div class="setting-info">
|
||||||
|
<label>{{ $t('knowledgeEditor.wiki.autoIngestLabel') }}</label>
|
||||||
|
<p class="desc">{{ $t('knowledgeEditor.wiki.autoIngestTip') }}</p>
|
||||||
|
</div>
|
||||||
|
<div class="setting-control">
|
||||||
|
<t-switch v-model="formData.wikiConfig.autoIngest" size="medium" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 合成模型 -->
|
||||||
|
<div class="setting-row">
|
||||||
|
<div class="setting-info">
|
||||||
|
<label>{{ $t('knowledgeEditor.wiki.synthesisModelLabel') }}</label>
|
||||||
|
<p class="desc">{{ $t('knowledgeEditor.wiki.synthesisModelTip') }}</p>
|
||||||
|
</div>
|
||||||
|
<div class="setting-control">
|
||||||
|
<ModelSelector
|
||||||
|
model-type="KnowledgeQA"
|
||||||
|
:selected-model-id="formData.wikiConfig.synthesisModelId"
|
||||||
|
:all-models="allModels"
|
||||||
|
@update:selected-model-id="(val: string) => { if (formData) formData.wikiConfig.synthesisModelId = val }"
|
||||||
|
@add-model="handleAddWikiModel"
|
||||||
|
:placeholder="$t('knowledgeEditor.wiki.synthesisModelPlaceholder')"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 单次最大页面数 -->
|
||||||
|
<div class="setting-row">
|
||||||
|
<div class="setting-info">
|
||||||
|
<label>{{ $t('knowledgeEditor.wiki.maxPagesLabel') }}</label>
|
||||||
|
<p class="desc">{{ $t('knowledgeEditor.wiki.maxPagesTip') }}</p>
|
||||||
|
</div>
|
||||||
|
<div class="setting-control">
|
||||||
|
<t-input-number v-model="formData.wikiConfig.maxPagesPerIngest" :min="0" :max="50" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 解析引擎 -->
|
<!-- 解析引擎 -->
|
||||||
<div v-if="!isFAQ && !isWiki && formData" v-show="currentSection === 'parser'" class="section">
|
<div v-if="!isFAQ && formData" v-show="currentSection === 'parser'" class="section">
|
||||||
<KBParserSettings
|
<KBParserSettings
|
||||||
:parser-engine-rules="formData.chunkingConfig.parserEngineRules"
|
:parser-engine-rules="formData.chunkingConfig.parserEngineRules"
|
||||||
@update:parser-engine-rules="handleParserEngineRulesUpdate"
|
@update:parser-engine-rules="handleParserEngineRulesUpdate"
|
||||||
@@ -171,7 +195,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 存储引擎 -->
|
<!-- 存储引擎 -->
|
||||||
<div v-if="!isFAQ && !isWiki && formData" v-show="currentSection === 'storage'" class="section">
|
<div v-if="!isFAQ && formData" v-show="currentSection === 'storage'" class="section">
|
||||||
<KBStorageSettings
|
<KBStorageSettings
|
||||||
:storage-provider="formData.storageProvider"
|
:storage-provider="formData.storageProvider"
|
||||||
:has-files="mode === 'edit' && hasFiles"
|
:has-files="mode === 'edit' && hasFiles"
|
||||||
@@ -180,7 +204,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 分块设置 -->
|
<!-- 分块设置 -->
|
||||||
<div v-if="!isFAQ && !isWiki" v-show="currentSection === 'chunking'" class="section">
|
<div v-if="!isFAQ" v-show="currentSection === 'chunking'" class="section">
|
||||||
<KBChunkingSettings
|
<KBChunkingSettings
|
||||||
v-if="formData"
|
v-if="formData"
|
||||||
:config="formData.chunkingConfig"
|
:config="formData.chunkingConfig"
|
||||||
@@ -189,7 +213,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 图谱设置 -->
|
<!-- 图谱设置 -->
|
||||||
<div v-if="!isFAQ && !isWiki" v-show="currentSection === 'graph'" class="section">
|
<div v-if="!isFAQ" v-show="currentSection === 'graph'" class="section">
|
||||||
<GraphSettings
|
<GraphSettings
|
||||||
v-if="formData"
|
v-if="formData"
|
||||||
:graph-extract="formData.nodeExtractConfig"
|
:graph-extract="formData.nodeExtractConfig"
|
||||||
@@ -200,7 +224,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 多模态配置 -->
|
<!-- 多模态配置 -->
|
||||||
<div v-if="!isFAQ && !isWiki" v-show="currentSection === 'multimodal'" class="section">
|
<div v-if="!isFAQ" v-show="currentSection === 'multimodal'" class="section">
|
||||||
<div v-if="formData" class="kb-multimodal-settings">
|
<div v-if="formData" class="kb-multimodal-settings">
|
||||||
<div class="section-header">
|
<div class="section-header">
|
||||||
<h2>{{ $t('knowledgeEditor.multimodal.title') }}</h2>
|
<h2>{{ $t('knowledgeEditor.multimodal.title') }}</h2>
|
||||||
@@ -245,7 +269,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 音频处理(ASR)设置 -->
|
<!-- 音频处理(ASR)设置 -->
|
||||||
<div v-if="!isFAQ && !isWiki" v-show="currentSection === 'asr'" class="section">
|
<div v-if="!isFAQ" v-show="currentSection === 'asr'" class="section">
|
||||||
<div v-if="formData" class="kb-multimodal-settings">
|
<div v-if="formData" class="kb-multimodal-settings">
|
||||||
<div class="section-header">
|
<div class="section-header">
|
||||||
<h2>{{ $t('knowledgeEditor.asr.title') }}</h2>
|
<h2>{{ $t('knowledgeEditor.asr.title') }}</h2>
|
||||||
@@ -289,7 +313,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 高级设置 -->
|
<!-- 高级设置 -->
|
||||||
<div v-if="!isFAQ && !isWiki" v-show="currentSection === 'advanced'" class="section">
|
<div v-if="!isFAQ" v-show="currentSection === 'advanced'" class="section">
|
||||||
<KBAdvancedSettings
|
<KBAdvancedSettings
|
||||||
ref="advancedSettingsRef"
|
ref="advancedSettingsRef"
|
||||||
v-if="formData"
|
v-if="formData"
|
||||||
@@ -355,7 +379,7 @@ const props = defineProps<{
|
|||||||
visible: boolean
|
visible: boolean
|
||||||
mode: 'create' | 'edit'
|
mode: 'create' | 'edit'
|
||||||
kbId?: string
|
kbId?: string
|
||||||
initialType?: 'document' | 'faq' | 'wiki'
|
initialType?: 'document' | 'faq'
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
// Emits
|
// Emits
|
||||||
@@ -379,8 +403,6 @@ const navItems = computed(() => {
|
|||||||
]
|
]
|
||||||
if (formData.value?.type === 'faq') {
|
if (formData.value?.type === 'faq') {
|
||||||
items.push({ key: 'faq', icon: 'help-circle', label: t('knowledgeEditor.sidebar.faq') })
|
items.push({ key: 'faq', icon: 'help-circle', label: t('knowledgeEditor.sidebar.faq') })
|
||||||
} else if (formData.value?.type === 'wiki') {
|
|
||||||
items.push({ key: 'wiki', icon: 'catalog', label: t('knowledgeEditor.sidebar.wiki') })
|
|
||||||
} else {
|
} else {
|
||||||
items.push(
|
items.push(
|
||||||
{ key: 'parser', icon: 'file-search', label: t('settings.parserEngine') },
|
{ key: 'parser', icon: 'file-search', label: t('settings.parserEngine') },
|
||||||
@@ -389,6 +411,7 @@ const navItems = computed(() => {
|
|||||||
{ key: 'storage', icon: 'cloud', label: t('knowledgeEditor.sidebar.storage') },
|
{ key: 'storage', icon: 'cloud', label: t('knowledgeEditor.sidebar.storage') },
|
||||||
{ key: 'chunking', icon: 'file-copy', label: t('knowledgeEditor.sidebar.chunking') },
|
{ key: 'chunking', icon: 'file-copy', label: t('knowledgeEditor.sidebar.chunking') },
|
||||||
{ key: 'graph', icon: 'chart-bubble', label: t('knowledgeEditor.sidebar.graph') },
|
{ key: 'graph', icon: 'chart-bubble', label: t('knowledgeEditor.sidebar.graph') },
|
||||||
|
{ key: 'wiki', icon: 'catalog', label: t('knowledgeEditor.sidebar.wiki') },
|
||||||
{ key: 'advanced', icon: 'setting', label: t('knowledgeEditor.sidebar.advanced') }
|
{ key: 'advanced', icon: 'setting', label: t('knowledgeEditor.sidebar.advanced') }
|
||||||
)
|
)
|
||||||
if (props.mode === 'edit' && props.kbId) {
|
if (props.mode === 'edit' && props.kbId) {
|
||||||
@@ -408,7 +431,6 @@ const advancedSettingsRef = ref<InstanceType<typeof KBAdvancedSettings>>()
|
|||||||
// 表单数据
|
// 表单数据
|
||||||
const formData = ref<any>(null)
|
const formData = ref<any>(null)
|
||||||
const isFAQ = computed(() => formData.value?.type === 'faq')
|
const isFAQ = computed(() => formData.value?.type === 'faq')
|
||||||
const isWiki = computed(() => formData.value?.type === 'wiki')
|
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => formData.value?.type,
|
() => formData.value?.type,
|
||||||
@@ -421,21 +443,14 @@ watch(
|
|||||||
if (!['basic', 'models', 'faq'].includes(currentSection.value)) {
|
if (!['basic', 'models', 'faq'].includes(currentSection.value)) {
|
||||||
currentSection.value = 'faq'
|
currentSection.value = 'faq'
|
||||||
}
|
}
|
||||||
} else if (newType === 'wiki') {
|
} else if (oldType === 'faq' && currentSection.value === 'faq') {
|
||||||
if (!formData.value.wikiConfig) {
|
|
||||||
formData.value.wikiConfig = { autoIngest: true, synthesisModelId: '', wikiLanguage: 'auto', maxPagesPerIngest: 0 }
|
|
||||||
}
|
|
||||||
if (!['basic', 'models', 'wiki'].includes(currentSection.value)) {
|
|
||||||
currentSection.value = 'wiki'
|
|
||||||
}
|
|
||||||
} else if ((oldType === 'faq' && currentSection.value === 'faq') || (oldType === 'wiki' && currentSection.value === 'wiki')) {
|
|
||||||
currentSection.value = 'basic'
|
currentSection.value = 'basic'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// 初始化表单数据
|
// 初始化表单数据
|
||||||
const initFormData = (type: 'document' | 'faq' | 'wiki' = 'document') => {
|
const initFormData = (type: 'document' | 'faq' = 'document') => {
|
||||||
return {
|
return {
|
||||||
type,
|
type,
|
||||||
name: '',
|
name: '',
|
||||||
@@ -486,9 +501,9 @@ const initFormData = (type: 'document' | 'faq' | 'wiki' = 'document') => {
|
|||||||
questionCount: 3
|
questionCount: 3
|
||||||
},
|
},
|
||||||
wikiConfig: {
|
wikiConfig: {
|
||||||
|
enabled: false,
|
||||||
autoIngest: true,
|
autoIngest: true,
|
||||||
synthesisModelId: '',
|
synthesisModelId: '',
|
||||||
wikiLanguage: 'auto',
|
|
||||||
maxPagesPerIngest: 0,
|
maxPagesPerIngest: 0,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -526,7 +541,7 @@ const loadKBData = async () => {
|
|||||||
hasFiles.value = (filesResult as any)?.total > 0
|
hasFiles.value = (filesResult as any)?.total > 0
|
||||||
|
|
||||||
// 设置表单数据
|
// 设置表单数据
|
||||||
const kbType = (kb.type as 'document' | 'faq' | 'wiki') || 'document'
|
const kbType = (kb.type as 'document' | 'faq') || 'document'
|
||||||
formData.value = {
|
formData.value = {
|
||||||
type: kbType,
|
type: kbType,
|
||||||
name: kb.name || '',
|
name: kb.name || '',
|
||||||
@@ -573,9 +588,9 @@ const loadKBData = async () => {
|
|||||||
questionCount: kb.question_generation_config?.question_count || 3
|
questionCount: kb.question_generation_config?.question_count || 3
|
||||||
},
|
},
|
||||||
wikiConfig: {
|
wikiConfig: {
|
||||||
|
enabled: kb.wiki_config?.enabled ?? false,
|
||||||
autoIngest: kb.wiki_config?.auto_ingest ?? true,
|
autoIngest: kb.wiki_config?.auto_ingest ?? true,
|
||||||
synthesisModelId: kb.wiki_config?.synthesis_model_id || '',
|
synthesisModelId: kb.wiki_config?.synthesis_model_id || '',
|
||||||
wikiLanguage: kb.wiki_config?.wiki_language || 'auto',
|
|
||||||
maxPagesPerIngest: kb.wiki_config?.max_pages_per_ingest || 0,
|
maxPagesPerIngest: kb.wiki_config?.max_pages_per_ingest || 0,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -628,6 +643,10 @@ const handleAddASRModel = () => {
|
|||||||
uiStore.openSettings('models', 'asr')
|
uiStore.openSettings('models', 'asr')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleAddWikiModel = () => {
|
||||||
|
uiStore.openSettings('models', 'knowledgeqa')
|
||||||
|
}
|
||||||
|
|
||||||
const handleStorageProviderUpdate = (value: string) => {
|
const handleStorageProviderUpdate = (value: string) => {
|
||||||
if (formData.value) {
|
if (formData.value) {
|
||||||
formData.value.storageProvider = value || 'local'
|
formData.value.storageProvider = value || 'local'
|
||||||
@@ -762,11 +781,12 @@ const buildSubmitData = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formData.value.type === 'wiki') {
|
// Always send wiki_config so enabled/disabled state is persisted
|
||||||
|
if (formData.value.type !== 'faq') {
|
||||||
data.wiki_config = {
|
data.wiki_config = {
|
||||||
|
enabled: formData.value.wikiConfig?.enabled ?? false,
|
||||||
auto_ingest: formData.value.wikiConfig?.autoIngest ?? true,
|
auto_ingest: formData.value.wikiConfig?.autoIngest ?? true,
|
||||||
synthesis_model_id: formData.value.wikiConfig?.synthesisModelId || '',
|
synthesis_model_id: formData.value.wikiConfig?.synthesisModelId || '',
|
||||||
wiki_language: formData.value.wikiConfig?.wikiLanguage || 'auto',
|
|
||||||
max_pages_per_ingest: formData.value.wikiConfig?.maxPagesPerIngest || 0,
|
max_pages_per_ingest: formData.value.wikiConfig?.maxPagesPerIngest || 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -837,11 +857,11 @@ const doSubmit = async () => {
|
|||||||
question_index_mode: formData.value.faqConfig.questionIndexMode || 'separate'
|
question_index_mode: formData.value.faqConfig.questionIndexMode || 'separate'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (formData.value.type === 'wiki' && formData.value.wikiConfig) {
|
if (formData.value.wikiConfig && formData.value.type !== 'faq') {
|
||||||
updateConfig.wiki_config = {
|
updateConfig.wiki_config = {
|
||||||
|
enabled: formData.value.wikiConfig.enabled ?? false,
|
||||||
auto_ingest: formData.value.wikiConfig.autoIngest ?? true,
|
auto_ingest: formData.value.wikiConfig.autoIngest ?? true,
|
||||||
synthesis_model_id: formData.value.wikiConfig.synthesisModelId || '',
|
synthesis_model_id: formData.value.wikiConfig.synthesisModelId || '',
|
||||||
wiki_language: formData.value.wikiConfig.wikiLanguage || 'auto',
|
|
||||||
max_pages_per_ingest: formData.value.wikiConfig.maxPagesPerIngest || 0,
|
max_pages_per_ingest: formData.value.wikiConfig.maxPagesPerIngest || 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -34,6 +34,12 @@ const (
|
|||||||
// produce content (e.g., thinking-only loops without KB).
|
// produce content (e.g., thinking-only loops without KB).
|
||||||
// Trade-off: each retry costs ~2s of LLM latency; 2 retries = max 4s extra.
|
// Trade-off: each retry costs ~2s of LLM latency; 2 retries = max 4s extra.
|
||||||
maxEmptyResponseRetries = 2
|
maxEmptyResponseRetries = 2
|
||||||
|
|
||||||
|
// maxRepeatedResponseRounds is the maximum number of consecutive rounds
|
||||||
|
// where the LLM returns identical content without any tool calls before
|
||||||
|
// the loop is forcibly terminated. This catches stuck loops caused by
|
||||||
|
// unhandled finish reasons (e.g., content_filter not caught elsewhere).
|
||||||
|
maxRepeatedResponseRounds = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
// transientErrorMarkers are substrings that indicate a transient (retryable) error.
|
// transientErrorMarkers are substrings that indicate a transient (retryable) error.
|
||||||
|
|||||||
@@ -271,6 +271,8 @@ func (e *AgentEngine) executeLoop(
|
|||||||
"max_iterations": e.config.MaxIterations,
|
"max_iterations": e.config.MaxIterations,
|
||||||
})
|
})
|
||||||
emptyRetries := 0
|
emptyRetries := 0
|
||||||
|
consecutiveSameContent := 0
|
||||||
|
lastResponseContent := ""
|
||||||
for state.CurrentRound < e.config.MaxIterations {
|
for state.CurrentRound < e.config.MaxIterations {
|
||||||
// Check for context cancellation (request timeout, user cancel, etc.)
|
// Check for context cancellation (request timeout, user cancel, etc.)
|
||||||
select {
|
select {
|
||||||
@@ -326,6 +328,27 @@ func (e *AgentEngine) executeLoop(
|
|||||||
response.Usage.CompletionTokens, response.Usage.TotalTokens)
|
response.Usage.CompletionTokens, response.Usage.TotalTokens)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Detect stuck loops: if the LLM keeps returning the same content
|
||||||
|
// without tool calls (e.g., an unhandled finish reason), break early.
|
||||||
|
if len(response.ToolCalls) == 0 && response.Content != "" {
|
||||||
|
if response.Content == lastResponseContent {
|
||||||
|
consecutiveSameContent++
|
||||||
|
} else {
|
||||||
|
consecutiveSameContent = 0
|
||||||
|
}
|
||||||
|
lastResponseContent = response.Content
|
||||||
|
if consecutiveSameContent >= maxRepeatedResponseRounds {
|
||||||
|
logger.Warnf(ctx, "[Agent][Round-%d] Detected stuck loop: same content repeated %d times (finish=%s), stopping",
|
||||||
|
state.CurrentRound+1, consecutiveSameContent+1, response.FinishReason)
|
||||||
|
state.FinalAnswer = response.Content
|
||||||
|
state.IsComplete = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
consecutiveSameContent = 0
|
||||||
|
lastResponseContent = ""
|
||||||
|
}
|
||||||
|
|
||||||
// Create agent step
|
// Create agent step
|
||||||
step := types.AgentStep{
|
step := types.AgentStep{
|
||||||
Iteration: state.CurrentRound,
|
Iteration: state.CurrentRound,
|
||||||
|
|||||||
@@ -15,11 +15,14 @@ const WikiSummaryPrompt = `You are a wiki editor. Given the following document c
|
|||||||
## Document Content
|
## Document Content
|
||||||
{{.Content}}
|
{{.Content}}
|
||||||
|
|
||||||
|
## Available Wiki Pages (use these exact slugs for links)
|
||||||
|
{{.ExtractedSlugs}}
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
1. Write a comprehensive summary of the document in Markdown format.
|
1. Write a comprehensive summary of the document in Markdown format.
|
||||||
2. Include the key facts, arguments, and conclusions.
|
2. Include the key facts, arguments, and conclusions.
|
||||||
3. Use proper heading hierarchy (## for sections, ### for subsections).
|
3. Use proper heading hierarchy (## for sections, ### for subsections).
|
||||||
4. Where relevant, use [[wiki-link]] syntax to reference entities and concepts that might have their own wiki pages. Use lowercase slugs with hyphens, e.g. [[entity/company-name]] or [[concept/machine-learning]].
|
4. Use [[wiki-link]] syntax to reference the available wiki pages listed above. Use the EXACT slugs provided — do NOT invent new slugs.
|
||||||
5. At the end, include a "## Key Takeaways" section with bullet points.
|
5. At the end, include a "## Key Takeaways" section with bullet points.
|
||||||
6. Write in {{.Language}}.
|
6. Write in {{.Language}}.
|
||||||
7. Keep the summary concise but thorough (500-1500 words depending on document length).
|
7. Keep the summary concise but thorough (500-1500 words depending on document length).
|
||||||
@@ -37,24 +40,35 @@ const WikiKnowledgeExtractPrompt = `You are a knowledge extraction system. Analy
|
|||||||
## Document Content
|
## Document Content
|
||||||
{{.Content}}
|
{{.Content}}
|
||||||
|
|
||||||
|
## Previously Extracted Slugs (from earlier version of this document)
|
||||||
|
{{.PreviousSlugs}}
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
Return a JSON object with two arrays: "entities" and "concepts".
|
Return a JSON object with two arrays: "entities" and "concepts".
|
||||||
|
**IMPORTANT: Write ALL names, descriptions, and details in {{.Language}}.**
|
||||||
|
|
||||||
|
### Slug Continuity Rules
|
||||||
|
If a "Previously Extracted Slugs" list is provided above, you MUST follow these rules:
|
||||||
|
- If an entity or concept from the previous extraction still exists in the current document, **reuse its exact slug** from the previous list. Do NOT generate a new slug for the same thing.
|
||||||
|
- If an entity or concept no longer appears in the document, **do NOT include it** in the output.
|
||||||
|
- Only generate new slugs for entities/concepts that are genuinely new (not present in the previous list).
|
||||||
|
- This ensures slug stability across document updates.
|
||||||
|
|
||||||
### Entities (people, organizations, products, places, technologies, events, etc.)
|
### Entities (people, organizations, products, places, technologies, events, etc.)
|
||||||
Each entity should have:
|
Each entity should have:
|
||||||
- "name": The entity name (human-readable)
|
- "name": The entity name in {{.Language}} (human-readable)
|
||||||
- "slug": URL-friendly slug, format "entity/<lowercase-hyphenated-name>"
|
- "slug": URL-friendly slug, format "entity/<lowercase-hyphenated-name>" (use romanized/pinyin form for non-Latin names). **Reuse previous slug if the entity was extracted before.**
|
||||||
- "description": A one-sentence description based on what the document says
|
- "description": A one-sentence description in {{.Language}} based on what the document says
|
||||||
- "details": A 2-5 sentence summary of key facts from the document
|
- "details": A 2-5 sentence summary in {{.Language}} of key facts from the document
|
||||||
|
|
||||||
Only include entities that are substantively discussed (mentioned at least twice or described in detail). Do NOT include generic terms.
|
Only include entities that are substantively discussed (mentioned at least twice or described in detail). Do NOT include generic terms.
|
||||||
|
|
||||||
### Concepts (topics, themes, methodologies, theories, etc.)
|
### Concepts (topics, themes, methodologies, theories, etc.)
|
||||||
Each concept should have:
|
Each concept should have:
|
||||||
- "name": The concept name (human-readable)
|
- "name": The concept name in {{.Language}} (human-readable)
|
||||||
- "slug": URL-friendly slug, format "concept/<lowercase-hyphenated-name>"
|
- "slug": URL-friendly slug, format "concept/<lowercase-hyphenated-name>" (use romanized/pinyin form for non-Latin names). **Reuse previous slug if the concept was extracted before.**
|
||||||
- "description": A one-sentence definition or description
|
- "description": A one-sentence definition or description in {{.Language}}
|
||||||
- "details": A 2-5 sentence explanation as discussed in the document
|
- "details": A 2-5 sentence explanation in {{.Language}} as discussed in the document
|
||||||
|
|
||||||
Only include concepts that are substantively discussed. Skip trivial or overly generic concepts.
|
Only include concepts that are substantively discussed. Skip trivial or overly generic concepts.
|
||||||
|
|
||||||
@@ -104,6 +118,31 @@ const WikiPageUpdatePrompt = `You are a wiki editor tasked with updating an exis
|
|||||||
|
|
||||||
Output ONLY the updated Markdown content. Do not include any preamble or explanation.`
|
Output ONLY the updated Markdown content. Do not include any preamble or explanation.`
|
||||||
|
|
||||||
|
// WikiPageRetractPrompt removes information contributed by a deleted document from an existing wiki page.
|
||||||
|
const WikiPageRetractPrompt = `You are a wiki editor. A source document has been DELETED from the knowledge base. You must update the existing wiki page to remove any information that came exclusively from this deleted document.
|
||||||
|
|
||||||
|
## Existing Page Content
|
||||||
|
{{.ExistingContent}}
|
||||||
|
|
||||||
|
## Deleted Document Title
|
||||||
|
{{.DeletedDocTitle}}
|
||||||
|
|
||||||
|
## Remaining Source Documents
|
||||||
|
{{.RemainingSources}}
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
1. Carefully review the existing page content.
|
||||||
|
2. Remove any facts, claims, or details that were ONLY sourced from "{{.DeletedDocTitle}}".
|
||||||
|
3. If a fact is also supported by the remaining source documents, KEEP it.
|
||||||
|
4. If you are unsure whether a fact came from the deleted document, keep it but add a note: "> *This information may need verification after source removal.*"
|
||||||
|
5. Update or remove the "Source: {{.DeletedDocTitle}}" reference line if present.
|
||||||
|
6. Remove any [[wiki-link]] references that point to pages that no longer exist, if you can identify them.
|
||||||
|
7. Maintain the existing page structure, formatting style, and language.
|
||||||
|
8. If after removing the deleted document's contributions the page becomes nearly empty, output just: "# [Title]\n\n*This page has been archived — the primary source document was removed.*"
|
||||||
|
9. Write in {{.Language}}.
|
||||||
|
|
||||||
|
Output ONLY the updated Markdown content. Do not include any preamble or explanation.`
|
||||||
|
|
||||||
// WikiIndexRebuildPrompt generates the index page content from a list of all pages.
|
// WikiIndexRebuildPrompt generates the index page content from a list of all pages.
|
||||||
const WikiIndexRebuildPrompt = `You are a wiki editor. Generate an index page (table of contents) for a wiki based on the following page listing.
|
const WikiIndexRebuildPrompt = `You are a wiki editor. Generate an index page (table of contents) for a wiki based on the following page listing.
|
||||||
|
|
||||||
|
|||||||
@@ -138,11 +138,17 @@ func (r *wikiPageRepository) ListByType(ctx context.Context, kbID string, pageTy
|
|||||||
return pages, nil
|
return pages, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListBySourceRef retrieves all wiki pages that reference a given source knowledge ID
|
// ListBySourceRef retrieves all wiki pages that reference a given source knowledge ID.
|
||||||
|
// Handles both old format ("knowledgeID") and new format ("knowledgeID|title") in source_refs JSON array.
|
||||||
func (r *wikiPageRepository) ListBySourceRef(ctx context.Context, kbID string, sourceKnowledgeID string) ([]*types.WikiPage, error) {
|
func (r *wikiPageRepository) ListBySourceRef(ctx context.Context, kbID string, sourceKnowledgeID string) ([]*types.WikiPage, error) {
|
||||||
var pages []*types.WikiPage
|
var pages []*types.WikiPage
|
||||||
|
// Match either exact "knowledgeID" or "knowledgeID|..." prefix in the JSON array
|
||||||
if err := r.db.WithContext(ctx).
|
if err := r.db.WithContext(ctx).
|
||||||
Where("knowledge_base_id = ? AND source_refs @> ?", kbID, fmt.Sprintf(`["%s"]`, sourceKnowledgeID)).
|
Where("knowledge_base_id = ? AND (source_refs @> ? OR source_refs::text LIKE ?)",
|
||||||
|
kbID,
|
||||||
|
fmt.Sprintf(`["%s"]`, sourceKnowledgeID),
|
||||||
|
fmt.Sprintf(`%%"%s|%%`, sourceKnowledgeID),
|
||||||
|
).
|
||||||
Find(&pages).Error; err != nil {
|
Find(&pages).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ func (s *agentService) CreateAgentEngine(
|
|||||||
// 4.5 Append wiki guidelines if any search target is a wiki KB
|
// 4.5 Append wiki guidelines if any search target is a wiki KB
|
||||||
for _, target := range config.SearchTargets {
|
for _, target := range config.SearchTargets {
|
||||||
kb, err := s.knowledgeBaseService.GetKnowledgeBaseByIDOnly(ctx, target.KnowledgeBaseID)
|
kb, err := s.knowledgeBaseService.GetKnowledgeBaseByIDOnly(ctx, target.KnowledgeBaseID)
|
||||||
if err == nil && kb != nil && kb.Type == types.KnowledgeBaseTypeWiki {
|
if err == nil && kb != nil && kb.IsWikiEnabled() {
|
||||||
systemPromptTemplate += "\n" + agent.WikiAgentSystemPromptAddendum
|
systemPromptTemplate += "\n" + agent.WikiAgentSystemPromptAddendum
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -383,7 +383,7 @@ func (s *agentService) registerTools(
|
|||||||
var wikiTenantID uint64
|
var wikiTenantID uint64
|
||||||
for _, target := range config.SearchTargets {
|
for _, target := range config.SearchTargets {
|
||||||
kb, err := s.knowledgeBaseService.GetKnowledgeBaseByIDOnly(ctx, target.KnowledgeBaseID)
|
kb, err := s.knowledgeBaseService.GetKnowledgeBaseByIDOnly(ctx, target.KnowledgeBaseID)
|
||||||
if err == nil && kb.Type == types.KnowledgeBaseTypeWiki {
|
if err == nil && kb.IsWikiEnabled() {
|
||||||
wikiKBIDs = append(wikiKBIDs, kb.ID)
|
wikiKBIDs = append(wikiKBIDs, kb.ID)
|
||||||
wikiTenantID = kb.TenantID
|
wikiTenantID = kb.TenantID
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func (p *PluginWikiBoost) OnEvent(
|
|||||||
hasWikiKB := false
|
hasWikiKB := false
|
||||||
for _, target := range chatManage.SearchTargets {
|
for _, target := range chatManage.SearchTargets {
|
||||||
kb, err := p.kbService.GetKnowledgeBaseByIDOnly(ctx, target.KnowledgeBaseID)
|
kb, err := p.kbService.GetKnowledgeBaseByIDOnly(ctx, target.KnowledgeBaseID)
|
||||||
if err == nil && kb.Type == types.KnowledgeBaseTypeWiki {
|
if err == nil && kb.IsWikiEnabled() {
|
||||||
hasWikiKB = true
|
hasWikiKB = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,6 +82,8 @@ type knowledgeService struct {
|
|||||||
// In-memory fallbacks for Lite mode (no Redis)
|
// In-memory fallbacks for Lite mode (no Redis)
|
||||||
memFAQProgress sync.Map // taskID -> *types.FAQImportProgress
|
memFAQProgress sync.Map // taskID -> *types.FAQImportProgress
|
||||||
memFAQRunningImport sync.Map // kbID -> *runningFAQImportInfo
|
memFAQRunningImport sync.Map // kbID -> *runningFAQImportInfo
|
||||||
|
wikiRepo interfaces.WikiPageRepository
|
||||||
|
wikiService interfaces.WikiPageService
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -110,6 +112,8 @@ func NewKnowledgeService(
|
|||||||
redisClient *redis.Client,
|
redisClient *redis.Client,
|
||||||
kbShareService interfaces.KBShareService,
|
kbShareService interfaces.KBShareService,
|
||||||
imageResolver *docparser.ImageResolver,
|
imageResolver *docparser.ImageResolver,
|
||||||
|
wikiRepo interfaces.WikiPageRepository,
|
||||||
|
wikiService interfaces.WikiPageService,
|
||||||
) (interfaces.KnowledgeService, error) {
|
) (interfaces.KnowledgeService, error) {
|
||||||
return &knowledgeService{
|
return &knowledgeService{
|
||||||
config: config,
|
config: config,
|
||||||
@@ -130,6 +134,8 @@ func NewKnowledgeService(
|
|||||||
redisClient: redisClient,
|
redisClient: redisClient,
|
||||||
kbShareService: kbShareService,
|
kbShareService: kbShareService,
|
||||||
imageResolver: imageResolver,
|
imageResolver: imageResolver,
|
||||||
|
wikiRepo: wikiRepo,
|
||||||
|
wikiService: wikiService,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1233,6 +1239,14 @@ func (s *knowledgeService) DeleteKnowledge(ctx context.Context, id string) error
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Clean up wiki pages that reference this knowledge
|
||||||
|
if kb != nil && kb.IsWikiEnabled() {
|
||||||
|
wg.Go(func() error {
|
||||||
|
s.cleanupWikiOnKnowledgeDelete(ctx, knowledge.KnowledgeBaseID, knowledge.ID)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if err = wg.Wait(); err != nil {
|
if err = wg.Wait(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1240,6 +1254,102 @@ func (s *knowledgeService) DeleteKnowledge(ctx context.Context, id string) error
|
|||||||
return s.repo.DeleteKnowledge(ctx, ctx.Value(types.TenantIDContextKey).(uint64), id)
|
return s.repo.DeleteKnowledge(ctx, ctx.Value(types.TenantIDContextKey).(uint64), id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cleanupWikiOnKnowledgeDelete handles wiki pages when a source document is deleted.
|
||||||
|
// - Pages with only this source ref → archived (kept for audit, hidden from normal views)
|
||||||
|
// - Pages with multiple source refs → source ref removed, page stays published
|
||||||
|
// - Index and log pages are rebuilt afterward
|
||||||
|
func (s *knowledgeService) cleanupWikiOnKnowledgeDelete(ctx context.Context, kbID, knowledgeID string) {
|
||||||
|
// Find all wiki pages referencing this knowledge ID
|
||||||
|
pages, err := s.wikiRepo.ListBySourceRef(ctx, kbID, knowledgeID)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki cleanup: failed to list pages by source ref %s: %v", knowledgeID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(pages) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract doc title from source refs for retract prompt
|
||||||
|
docTitle := knowledgeID
|
||||||
|
for _, page := range pages {
|
||||||
|
for _, ref := range page.SourceRefs {
|
||||||
|
prefix := knowledgeID + "|"
|
||||||
|
if strings.HasPrefix(ref, prefix) {
|
||||||
|
docTitle = ref[len(prefix):]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if docTitle != knowledgeID {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var archivedSlugs []string
|
||||||
|
var retractSlugs []string
|
||||||
|
for _, page := range pages {
|
||||||
|
// Skip index/log system pages
|
||||||
|
if page.PageType == types.WikiPageTypeIndex || page.PageType == types.WikiPageTypeLog {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if this is the only source ref (match by prefix "knowledgeID|" or exact "knowledgeID")
|
||||||
|
remaining := removeSourceRef(page.SourceRefs, knowledgeID)
|
||||||
|
|
||||||
|
if len(remaining) == 0 {
|
||||||
|
// No other sources → archive the page
|
||||||
|
page.Status = types.WikiPageStatusArchived
|
||||||
|
page.SourceRefs = remaining
|
||||||
|
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki cleanup: failed to archive page %s: %v", page.Slug, err)
|
||||||
|
} else {
|
||||||
|
archivedSlugs = append(archivedSlugs, page.Slug)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Other sources remain → remove source ref and queue LLM content retraction
|
||||||
|
page.SourceRefs = remaining
|
||||||
|
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki cleanup: failed to update source refs for page %s: %v", page.Slug, err)
|
||||||
|
} else {
|
||||||
|
retractSlugs = append(retractSlugs, page.Slug)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(archivedSlugs) > 0 {
|
||||||
|
logger.Infof(ctx, "wiki cleanup: archived %d pages after knowledge %s deletion: %v",
|
||||||
|
len(archivedSlugs), knowledgeID, archivedSlugs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enqueue async LLM retraction for multi-source pages
|
||||||
|
if len(retractSlugs) > 0 {
|
||||||
|
lang, _ := types.LanguageFromContext(ctx)
|
||||||
|
tenantID, _ := types.TenantIDFromContext(ctx)
|
||||||
|
EnqueueWikiRetract(ctx, s.task, WikiRetractPayload{
|
||||||
|
TenantID: tenantID,
|
||||||
|
KnowledgeBaseID: kbID,
|
||||||
|
KnowledgeID: knowledgeID,
|
||||||
|
DocTitle: docTitle,
|
||||||
|
Language: lang,
|
||||||
|
PageSlugs: retractSlugs,
|
||||||
|
})
|
||||||
|
logger.Infof(ctx, "wiki cleanup: enqueued retract task for %d pages: %v", len(retractSlugs), retractSlugs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// removeSourceRef removes entries from source_refs that match a knowledge ID.
|
||||||
|
// Handles both old format ("knowledgeID") and new format ("knowledgeID|title").
|
||||||
|
func removeSourceRef(refs types.StringArray, knowledgeID string) types.StringArray {
|
||||||
|
var result types.StringArray
|
||||||
|
prefix := knowledgeID + "|"
|
||||||
|
for _, ref := range refs {
|
||||||
|
if ref == knowledgeID || strings.HasPrefix(ref, prefix) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, ref)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteKnowledgeList deletes a knowledge entry and all related resources
|
// DeleteKnowledgeList deletes a knowledge entry and all related resources
|
||||||
func (s *knowledgeService) DeleteKnowledgeList(ctx context.Context, ids []string) error {
|
func (s *knowledgeService) DeleteKnowledgeList(ctx context.Context, ids []string) error {
|
||||||
if len(ids) == 0 {
|
if len(ids) == 0 {
|
||||||
@@ -1383,6 +1493,17 @@ func (s *knowledgeService) DeleteKnowledgeList(ctx context.Context, ids []string
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Clean up wiki pages that reference deleted knowledge
|
||||||
|
wg.Go(func() error {
|
||||||
|
for _, knowledge := range knowledgeList {
|
||||||
|
kb, _ := s.kbService.GetKnowledgeBaseByID(ctx, knowledge.KnowledgeBaseID)
|
||||||
|
if kb != nil && kb.IsWikiEnabled() {
|
||||||
|
s.cleanupWikiOnKnowledgeDelete(ctx, knowledge.KnowledgeBaseID, knowledge.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
if err = wg.Wait(); err != nil {
|
if err = wg.Wait(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,9 +18,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// TypeWikiIngest is the asynq task type for wiki ingest
|
|
||||||
TypeWikiIngest = "wiki:ingest"
|
|
||||||
|
|
||||||
// maxContentForWiki limits the document content sent to LLM for wiki generation
|
// maxContentForWiki limits the document content sent to LLM for wiki generation
|
||||||
maxContentForWiki = 32768
|
maxContentForWiki = 32768
|
||||||
)
|
)
|
||||||
@@ -30,6 +27,17 @@ type WikiIngestPayload struct {
|
|||||||
TenantID uint64 `json:"tenant_id"`
|
TenantID uint64 `json:"tenant_id"`
|
||||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||||
KnowledgeID string `json:"knowledge_id"`
|
KnowledgeID string `json:"knowledge_id"`
|
||||||
|
Language string `json:"language,omitempty"` // locale code, e.g. "zh-CN"
|
||||||
|
}
|
||||||
|
|
||||||
|
// WikiRetractPayload is the asynq task payload for wiki content retraction
|
||||||
|
type WikiRetractPayload struct {
|
||||||
|
TenantID uint64 `json:"tenant_id"`
|
||||||
|
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||||
|
KnowledgeID string `json:"knowledge_id"`
|
||||||
|
DocTitle string `json:"doc_title"`
|
||||||
|
Language string `json:"language,omitempty"`
|
||||||
|
PageSlugs []string `json:"page_slugs"` // pages to retract content from
|
||||||
}
|
}
|
||||||
|
|
||||||
// wikiIngestService handles the LLM-powered wiki generation pipeline
|
// wikiIngestService handles the LLM-powered wiki generation pipeline
|
||||||
@@ -60,25 +68,137 @@ func NewWikiIngestService(
|
|||||||
|
|
||||||
// EnqueueWikiIngest enqueues an async wiki ingest task
|
// EnqueueWikiIngest enqueues an async wiki ingest task
|
||||||
func EnqueueWikiIngest(ctx context.Context, task interfaces.TaskEnqueuer, tenantID uint64, kbID, knowledgeID string) {
|
func EnqueueWikiIngest(ctx context.Context, task interfaces.TaskEnqueuer, tenantID uint64, kbID, knowledgeID string) {
|
||||||
|
lang, _ := types.LanguageFromContext(ctx)
|
||||||
payload := WikiIngestPayload{
|
payload := WikiIngestPayload{
|
||||||
TenantID: tenantID,
|
TenantID: tenantID,
|
||||||
KnowledgeBaseID: kbID,
|
KnowledgeBaseID: kbID,
|
||||||
KnowledgeID: knowledgeID,
|
KnowledgeID: knowledgeID,
|
||||||
|
Language: lang,
|
||||||
}
|
}
|
||||||
payloadBytes, err := json.Marshal(payload)
|
payloadBytes, err := json.Marshal(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf(ctx, "wiki ingest: failed to marshal payload: %v", err)
|
logger.Errorf(ctx, "wiki ingest: failed to marshal payload: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t := asynq.NewTask(TypeWikiIngest, payloadBytes, asynq.Queue("low"), asynq.MaxRetry(2))
|
t := asynq.NewTask(types.TypeWikiIngest, payloadBytes, asynq.Queue("low"), asynq.MaxRetry(2))
|
||||||
if _, err := task.Enqueue(t); err != nil {
|
if _, err := task.Enqueue(t); err != nil {
|
||||||
logger.Warnf(ctx, "wiki ingest: failed to enqueue task: %v", err)
|
logger.Warnf(ctx, "wiki ingest: failed to enqueue task: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnqueueWikiRetract enqueues an async wiki content retraction task
|
||||||
|
func EnqueueWikiRetract(ctx context.Context, task interfaces.TaskEnqueuer, payload WikiRetractPayload) {
|
||||||
|
payloadBytes, err := json.Marshal(payload)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf(ctx, "wiki retract: failed to marshal payload: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t := asynq.NewTask(types.TypeWikiRetract, payloadBytes, asynq.Queue("low"), asynq.MaxRetry(2))
|
||||||
|
if _, err := task.Enqueue(t); err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki retract: failed to enqueue task: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Handle implements interfaces.TaskHandler for asynq task processing
|
// Handle implements interfaces.TaskHandler for asynq task processing
|
||||||
func (s *wikiIngestService) Handle(ctx context.Context, t *asynq.Task) error {
|
func (s *wikiIngestService) Handle(ctx context.Context, t *asynq.Task) error {
|
||||||
return s.ProcessWikiIngest(ctx, t)
|
switch t.Type() {
|
||||||
|
case types.TypeWikiRetract:
|
||||||
|
return s.ProcessWikiRetract(ctx, t)
|
||||||
|
default:
|
||||||
|
return s.ProcessWikiIngest(ctx, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProcessWikiRetract uses LLM to remove deleted document's contributions from wiki pages
|
||||||
|
func (s *wikiIngestService) ProcessWikiRetract(ctx context.Context, t *asynq.Task) error {
|
||||||
|
var payload WikiRetractPayload
|
||||||
|
if err := json.Unmarshal(t.Payload(), &payload); err != nil {
|
||||||
|
return fmt.Errorf("wiki retract: unmarshal payload: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof(ctx, "wiki retract: starting for knowledge %s (%s), %d pages",
|
||||||
|
payload.KnowledgeID, payload.DocTitle, len(payload.PageSlugs))
|
||||||
|
|
||||||
|
// Inject context values
|
||||||
|
ctx = context.WithValue(ctx, types.TenantIDContextKey, payload.TenantID)
|
||||||
|
if payload.Language != "" {
|
||||||
|
ctx = context.WithValue(ctx, types.LanguageContextKey, payload.Language)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get KB and synthesis model
|
||||||
|
kb, err := s.kbService.GetKnowledgeBaseByIDOnly(ctx, payload.KnowledgeBaseID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("wiki retract: get KB: %w", err)
|
||||||
|
}
|
||||||
|
synthesisModelID := kb.WikiConfig.SynthesisModelID
|
||||||
|
if synthesisModelID == "" {
|
||||||
|
synthesisModelID = kb.SummaryModelID
|
||||||
|
}
|
||||||
|
if synthesisModelID == "" {
|
||||||
|
return fmt.Errorf("wiki retract: no synthesis model for KB %s", kb.ID)
|
||||||
|
}
|
||||||
|
chatModel, err := s.modelService.GetChatModel(ctx, synthesisModelID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("wiki retract: get chat model: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
lang := types.LanguageNameFromContext(ctx)
|
||||||
|
|
||||||
|
for _, slug := range payload.PageSlugs {
|
||||||
|
page, err := s.wikiService.GetPageBySlug(ctx, payload.KnowledgeBaseID, slug)
|
||||||
|
if err != nil || page == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build remaining sources list
|
||||||
|
var remainingSources string
|
||||||
|
for _, ref := range page.SourceRefs {
|
||||||
|
pipeIdx := strings.Index(ref, "|")
|
||||||
|
if pipeIdx > 0 {
|
||||||
|
remainingSources += "- " + ref[pipeIdx+1:] + "\n"
|
||||||
|
} else {
|
||||||
|
remainingSources += "- " + ref + "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if remainingSources == "" {
|
||||||
|
remainingSources = "(no other sources)"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call LLM to retract content
|
||||||
|
updatedContent, err := s.generateWithTemplate(ctx, chatModel, agent.WikiPageRetractPrompt, map[string]string{
|
||||||
|
"ExistingContent": page.Content,
|
||||||
|
"DeletedDocTitle": payload.DocTitle,
|
||||||
|
"RemainingSources": remainingSources,
|
||||||
|
"Language": lang,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki retract: LLM call failed for page %s: %v", slug, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
page.Content = updatedContent
|
||||||
|
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki retract: failed to update page %s: %v", slug, err)
|
||||||
|
} else {
|
||||||
|
logger.Infof(ctx, "wiki retract: updated page %s after removing content from '%s'", slug, payload.DocTitle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rebuild index page to reflect changes
|
||||||
|
if err := s.rebuildIndexPage(ctx, chatModel, WikiIngestPayload{
|
||||||
|
TenantID: payload.TenantID,
|
||||||
|
KnowledgeBaseID: payload.KnowledgeBaseID,
|
||||||
|
}, lang); err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki retract: rebuild index failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append log entry
|
||||||
|
s.appendLogEntry(ctx, WikiIngestPayload{
|
||||||
|
TenantID: payload.TenantID,
|
||||||
|
KnowledgeBaseID: payload.KnowledgeBaseID,
|
||||||
|
}, payload.DocTitle+" [DELETED]", payload.PageSlugs, nil)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessWikiIngest processes a wiki ingest task (asynq handler)
|
// ProcessWikiIngest processes a wiki ingest task (asynq handler)
|
||||||
@@ -90,12 +210,20 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
|
|||||||
|
|
||||||
logger.Infof(ctx, "wiki ingest: starting for knowledge %s in KB %s", payload.KnowledgeID, payload.KnowledgeBaseID)
|
logger.Infof(ctx, "wiki ingest: starting for knowledge %s in KB %s", payload.KnowledgeID, payload.KnowledgeBaseID)
|
||||||
|
|
||||||
// Get KB and validate it's a wiki type
|
// Inject tenant ID into context — asynq tasks don't have it from middleware
|
||||||
|
ctx = context.WithValue(ctx, types.TenantIDContextKey, payload.TenantID)
|
||||||
|
|
||||||
|
// Inject language into context — captured from the original HTTP request at enqueue time
|
||||||
|
if payload.Language != "" {
|
||||||
|
ctx = context.WithValue(ctx, types.LanguageContextKey, payload.Language)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get KB and validate it's wiki-enabled
|
||||||
kb, err := s.kbService.GetKnowledgeBaseByIDOnly(ctx, payload.KnowledgeBaseID)
|
kb, err := s.kbService.GetKnowledgeBaseByIDOnly(ctx, payload.KnowledgeBaseID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("wiki ingest: get KB: %w", err)
|
return fmt.Errorf("wiki ingest: get KB: %w", err)
|
||||||
}
|
}
|
||||||
if kb.Type != types.KnowledgeBaseTypeWiki {
|
if !kb.IsWikiEnabled() {
|
||||||
return fmt.Errorf("wiki ingest: KB %s is not wiki type", kb.ID)
|
return fmt.Errorf("wiki ingest: KB %s is not wiki type", kb.ID)
|
||||||
}
|
}
|
||||||
if kb.WikiConfig == nil || !kb.WikiConfig.AutoIngest {
|
if kb.WikiConfig == nil || !kb.WikiConfig.AutoIngest {
|
||||||
@@ -132,10 +260,8 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
|
|||||||
content = string([]rune(content)[:maxContentForWiki])
|
content = string([]rune(content)[:maxContentForWiki])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get human-readable language name for LLM prompts
|
// Get human-readable language name for LLM prompts from middleware context
|
||||||
// Reuses language mapping from middleware infrastructure (supports 9+ languages)
|
lang := types.LanguageNameFromContext(ctx)
|
||||||
// Maps locale codes like "zh", "en" to names like "Chinese (Simplified)", "English"
|
|
||||||
lang := types.LanguageLocaleName(kb.WikiConfig.WikiLanguage)
|
|
||||||
|
|
||||||
// Get document title from first chunk's knowledge info
|
// Get document title from first chunk's knowledge info
|
||||||
docTitle := payload.KnowledgeID
|
docTitle := payload.KnowledgeID
|
||||||
@@ -155,14 +281,34 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
|
|||||||
var pagesAffected []string
|
var pagesAffected []string
|
||||||
var synthesisSuggestions []string
|
var synthesisSuggestions []string
|
||||||
|
|
||||||
// Step 1: Generate summary page
|
// Format source ref as "knowledgeID|docTitle" for frontend display
|
||||||
|
sourceRef := fmt.Sprintf("%s|%s", payload.KnowledgeID, docTitle)
|
||||||
|
|
||||||
|
// Snapshot: existing page slugs that reference this knowledge ID (before extraction)
|
||||||
|
// Used later to detect pages that are no longer relevant after document update
|
||||||
|
oldPageSlugs := s.getExistingPageSlugsForKnowledge(ctx, payload.KnowledgeBaseID, payload.KnowledgeID)
|
||||||
|
|
||||||
|
// Step 1: Extract entities and concepts FIRST (so we know the slugs for summary links)
|
||||||
|
extractedPages, extractedSlugs, err := s.extractEntitiesAndConcepts(ctx, chatModel, content, docTitle, lang, payload, sourceRef, oldPageSlugs)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki ingest: knowledge extraction failed: %v", err)
|
||||||
|
} else {
|
||||||
|
pagesAffected = append(pagesAffected, extractedPages...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Generate summary page (with extracted slugs for accurate [[wiki-links]])
|
||||||
summarySlug := fmt.Sprintf("summary/%s", slugify(docTitle))
|
summarySlug := fmt.Sprintf("summary/%s", slugify(docTitle))
|
||||||
|
var slugListing string
|
||||||
|
for _, slug := range extractedSlugs {
|
||||||
|
slugListing += fmt.Sprintf("- [[%s]]\n", slug)
|
||||||
|
}
|
||||||
summaryContent, err := s.generateWithTemplate(ctx, chatModel, agent.WikiSummaryPrompt, map[string]string{
|
summaryContent, err := s.generateWithTemplate(ctx, chatModel, agent.WikiSummaryPrompt, map[string]string{
|
||||||
"Title": docTitle,
|
"Title": docTitle,
|
||||||
"FileName": docTitle,
|
"FileName": docTitle,
|
||||||
"FileType": "document",
|
"FileType": "document",
|
||||||
"Content": content,
|
"Content": content,
|
||||||
"Language": lang,
|
"Language": lang,
|
||||||
|
"ExtractedSlugs": slugListing,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf(ctx, "wiki ingest: generate summary failed: %v", err)
|
logger.Errorf(ctx, "wiki ingest: generate summary failed: %v", err)
|
||||||
@@ -177,7 +323,7 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
|
|||||||
Status: types.WikiPageStatusDraft,
|
Status: types.WikiPageStatusDraft,
|
||||||
Content: summaryContent,
|
Content: summaryContent,
|
||||||
Summary: truncateString(summaryContent, 200),
|
Summary: truncateString(summaryContent, 200),
|
||||||
SourceRefs: types.StringArray{payload.KnowledgeID},
|
SourceRefs: types.StringArray{sourceRef},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warnf(ctx, "wiki ingest: create summary page failed: %v", err)
|
logger.Warnf(ctx, "wiki ingest: create summary page failed: %v", err)
|
||||||
@@ -186,14 +332,6 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: Extract entities and concepts in a single LLM call, then create/update pages
|
|
||||||
extractedPages, err := s.extractEntitiesAndConcepts(ctx, chatModel, content, docTitle, lang, payload)
|
|
||||||
if err != nil {
|
|
||||||
logger.Warnf(ctx, "wiki ingest: knowledge extraction failed: %v", err)
|
|
||||||
} else {
|
|
||||||
pagesAffected = append(pagesAffected, extractedPages...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 3: Detect synthesis opportunities (no LLM call, pure heuristic)
|
// Step 3: Detect synthesis opportunities (no LLM call, pure heuristic)
|
||||||
synthesisSuggestions = s.detectSynthesisOpportunities(ctx, payload)
|
synthesisSuggestions = s.detectSynthesisOpportunities(ctx, payload)
|
||||||
|
|
||||||
@@ -208,12 +346,128 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
|
|||||||
// Step 6: Publish all draft pages created during this ingest
|
// Step 6: Publish all draft pages created during this ingest
|
||||||
s.publishDraftPages(ctx, payload.KnowledgeBaseID, pagesAffected)
|
s.publishDraftPages(ctx, payload.KnowledgeBaseID, pagesAffected)
|
||||||
|
|
||||||
|
// Step 7: Handle stale pages — pages that previously referenced this document
|
||||||
|
// but are no longer produced by the updated extraction. This handles the
|
||||||
|
// "document updated and some entities/concepts were removed" scenario.
|
||||||
|
s.retractStalePages(ctx, payload, oldPageSlugs, pagesAffected, docTitle, lang)
|
||||||
|
|
||||||
logger.Infof(ctx, "wiki ingest: completed for knowledge %s, %d pages affected",
|
logger.Infof(ctx, "wiki ingest: completed for knowledge %s, %d pages affected",
|
||||||
payload.KnowledgeID, len(pagesAffected))
|
payload.KnowledgeID, len(pagesAffected))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getExistingPageSlugsForKnowledge returns all page slugs that currently reference
|
||||||
|
// a given knowledge ID in their source_refs. Used to snapshot state before re-ingest.
|
||||||
|
func (s *wikiIngestService) getExistingPageSlugsForKnowledge(ctx context.Context, kbID, knowledgeID string) map[string]bool {
|
||||||
|
resp, err := s.wikiService.ListPages(ctx, &types.WikiPageListRequest{
|
||||||
|
KnowledgeBaseID: kbID,
|
||||||
|
PageSize: 500,
|
||||||
|
})
|
||||||
|
if err != nil || resp == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
slugs := make(map[string]bool)
|
||||||
|
prefix := knowledgeID + "|"
|
||||||
|
for _, p := range resp.Pages {
|
||||||
|
if p.PageType == types.WikiPageTypeIndex || p.PageType == types.WikiPageTypeLog {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, ref := range p.SourceRefs {
|
||||||
|
if ref == knowledgeID || strings.HasPrefix(ref, prefix) {
|
||||||
|
slugs[p.Slug] = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return slugs
|
||||||
|
}
|
||||||
|
|
||||||
|
// retractStalePages handles pages that were previously linked to this document
|
||||||
|
// but are no longer produced by the updated extraction.
|
||||||
|
// - Single-source stale pages → archived
|
||||||
|
// - Multi-source stale pages → enqueue LLM retract to clean content
|
||||||
|
func (s *wikiIngestService) retractStalePages(
|
||||||
|
ctx context.Context,
|
||||||
|
payload WikiIngestPayload,
|
||||||
|
oldSlugs map[string]bool,
|
||||||
|
newSlugs []string,
|
||||||
|
docTitle, lang string,
|
||||||
|
) {
|
||||||
|
if len(oldSlugs) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build set of newly affected slugs (including summary)
|
||||||
|
newSet := make(map[string]bool, len(newSlugs))
|
||||||
|
for _, s := range newSlugs {
|
||||||
|
newSet[s] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stale = was in old set but not in new set
|
||||||
|
var staleSlugs []string
|
||||||
|
for slug := range oldSlugs {
|
||||||
|
if !newSet[slug] {
|
||||||
|
staleSlugs = append(staleSlugs, slug)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(staleSlugs) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof(ctx, "wiki ingest: %d stale pages detected after document update: %v", len(staleSlugs), staleSlugs)
|
||||||
|
|
||||||
|
var retractSlugs []string
|
||||||
|
sourceRef := fmt.Sprintf("%s|%s", payload.KnowledgeID, docTitle)
|
||||||
|
prefix := payload.KnowledgeID + "|"
|
||||||
|
|
||||||
|
for _, slug := range staleSlugs {
|
||||||
|
page, err := s.wikiService.GetPageBySlug(ctx, payload.KnowledgeBaseID, slug)
|
||||||
|
if err != nil || page == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove this doc's source ref
|
||||||
|
var remaining types.StringArray
|
||||||
|
for _, ref := range page.SourceRefs {
|
||||||
|
if ref == payload.KnowledgeID || ref == sourceRef || strings.HasPrefix(ref, prefix) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
remaining = append(remaining, ref)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(remaining) == 0 {
|
||||||
|
// No other sources → archive
|
||||||
|
page.Status = types.WikiPageStatusArchived
|
||||||
|
page.SourceRefs = remaining
|
||||||
|
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki ingest: failed to archive stale page %s: %v", slug, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Multi-source → remove ref, queue retract
|
||||||
|
page.SourceRefs = remaining
|
||||||
|
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki ingest: failed to update stale page %s: %v", slug, err)
|
||||||
|
} else {
|
||||||
|
retractSlugs = append(retractSlugs, slug)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enqueue LLM retraction for multi-source stale pages
|
||||||
|
if len(retractSlugs) > 0 {
|
||||||
|
EnqueueWikiRetract(ctx, s.task, WikiRetractPayload{
|
||||||
|
TenantID: payload.TenantID,
|
||||||
|
KnowledgeBaseID: payload.KnowledgeBaseID,
|
||||||
|
KnowledgeID: payload.KnowledgeID,
|
||||||
|
DocTitle: docTitle,
|
||||||
|
Language: payload.Language,
|
||||||
|
PageSlugs: retractSlugs,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// extractedItem represents a single extracted entity or concept
|
// extractedItem represents a single extracted entity or concept
|
||||||
type extractedItem struct {
|
type extractedItem struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -229,20 +483,37 @@ type combinedExtraction struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// extractEntitiesAndConcepts performs a single LLM call to extract both entities and concepts,
|
// extractEntitiesAndConcepts performs a single LLM call to extract both entities and concepts,
|
||||||
// then upserts pages for each. Returns the list of affected page slugs.
|
// then upserts pages for each. Returns the list of affected page slugs and all extracted slugs (for linking).
|
||||||
|
// oldPageSlugs contains slugs from the previous version of this document — passed to LLM for slug stability.
|
||||||
func (s *wikiIngestService) extractEntitiesAndConcepts(
|
func (s *wikiIngestService) extractEntitiesAndConcepts(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
chatModel chat.Chat,
|
chatModel chat.Chat,
|
||||||
content, docTitle, lang string,
|
content, docTitle, lang string,
|
||||||
payload WikiIngestPayload,
|
payload WikiIngestPayload,
|
||||||
) ([]string, error) {
|
sourceRef string,
|
||||||
|
oldPageSlugs map[string]bool,
|
||||||
|
) ([]string, []string, error) {
|
||||||
|
// Build previous slugs listing for the prompt
|
||||||
|
var prevSlugsText string
|
||||||
|
if len(oldPageSlugs) > 0 {
|
||||||
|
var sb strings.Builder
|
||||||
|
for slug := range oldPageSlugs {
|
||||||
|
fmt.Fprintf(&sb, "- %s\n", slug)
|
||||||
|
}
|
||||||
|
prevSlugsText = sb.String()
|
||||||
|
} else {
|
||||||
|
prevSlugsText = "(none — this is a new document)"
|
||||||
|
}
|
||||||
|
|
||||||
// Single LLM call for both entities and concepts
|
// Single LLM call for both entities and concepts
|
||||||
extractionJSON, err := s.generateWithTemplate(ctx, chatModel, agent.WikiKnowledgeExtractPrompt, map[string]string{
|
extractionJSON, err := s.generateWithTemplate(ctx, chatModel, agent.WikiKnowledgeExtractPrompt, map[string]string{
|
||||||
"Title": docTitle,
|
"Title": docTitle,
|
||||||
"Content": content,
|
"Content": content,
|
||||||
|
"Language": lang,
|
||||||
|
"PreviousSlugs": prevSlugsText,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("combined extraction failed: %w", err)
|
return nil, nil, fmt.Errorf("combined extraction failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean JSON - strip markdown code blocks if present
|
// Clean JSON - strip markdown code blocks if present
|
||||||
@@ -255,7 +526,20 @@ func (s *wikiIngestService) extractEntitiesAndConcepts(
|
|||||||
var result combinedExtraction
|
var result combinedExtraction
|
||||||
if err := json.Unmarshal([]byte(extractionJSON), &result); err != nil {
|
if err := json.Unmarshal([]byte(extractionJSON), &result); err != nil {
|
||||||
logger.Warnf(ctx, "wiki ingest: failed to parse combined extraction JSON: %v\nRaw: %s", err, truncateString(extractionJSON, 500))
|
logger.Warnf(ctx, "wiki ingest: failed to parse combined extraction JSON: %v\nRaw: %s", err, truncateString(extractionJSON, 500))
|
||||||
return nil, fmt.Errorf("parse combined extraction JSON: %w", err)
|
return nil, nil, fmt.Errorf("parse combined extraction JSON: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect all extracted slugs (before dedup remapping) for summary linking
|
||||||
|
var allSlugs []string
|
||||||
|
for _, item := range result.Entities {
|
||||||
|
if item.Slug != "" {
|
||||||
|
allSlugs = append(allSlugs, item.Slug)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, item := range result.Concepts {
|
||||||
|
if item.Slug != "" {
|
||||||
|
allSlugs = append(allSlugs, item.Slug)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var affected []string
|
var affected []string
|
||||||
@@ -266,8 +550,21 @@ func (s *wikiIngestService) extractEntitiesAndConcepts(
|
|||||||
// Deduplicate concepts against existing wiki pages (LLM-based)
|
// Deduplicate concepts against existing wiki pages (LLM-based)
|
||||||
result.Concepts = s.deduplicateItems(ctx, chatModel, result.Concepts, types.WikiPageTypeConcept, payload.KnowledgeBaseID)
|
result.Concepts = s.deduplicateItems(ctx, chatModel, result.Concepts, types.WikiPageTypeConcept, payload.KnowledgeBaseID)
|
||||||
|
|
||||||
|
// Update allSlugs after dedup (use final slugs)
|
||||||
|
allSlugs = allSlugs[:0]
|
||||||
|
for _, item := range result.Entities {
|
||||||
|
if item.Slug != "" {
|
||||||
|
allSlugs = append(allSlugs, item.Slug)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, item := range result.Concepts {
|
||||||
|
if item.Slug != "" {
|
||||||
|
allSlugs = append(allSlugs, item.Slug)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Upsert entity pages
|
// Upsert entity pages
|
||||||
entitySlugs, err := s.upsertExtractedPages(ctx, chatModel, result.Entities, types.WikiPageTypeEntity, docTitle, lang, payload)
|
entitySlugs, err := s.upsertExtractedPages(ctx, chatModel, result.Entities, types.WikiPageTypeEntity, docTitle, lang, payload, sourceRef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warnf(ctx, "wiki ingest: entity upsert failed: %v", err)
|
logger.Warnf(ctx, "wiki ingest: entity upsert failed: %v", err)
|
||||||
} else {
|
} else {
|
||||||
@@ -275,14 +572,14 @@ func (s *wikiIngestService) extractEntitiesAndConcepts(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Upsert concept pages
|
// Upsert concept pages
|
||||||
conceptSlugs, err := s.upsertExtractedPages(ctx, chatModel, result.Concepts, types.WikiPageTypeConcept, docTitle, lang, payload)
|
conceptSlugs, err := s.upsertExtractedPages(ctx, chatModel, result.Concepts, types.WikiPageTypeConcept, docTitle, lang, payload, sourceRef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warnf(ctx, "wiki ingest: concept upsert failed: %v", err)
|
logger.Warnf(ctx, "wiki ingest: concept upsert failed: %v", err)
|
||||||
} else {
|
} else {
|
||||||
affected = append(affected, conceptSlugs...)
|
affected = append(affected, conceptSlugs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return affected, nil
|
return affected, allSlugs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// upsertExtractedPages creates or updates wiki pages from pre-extracted items.
|
// upsertExtractedPages creates or updates wiki pages from pre-extracted items.
|
||||||
@@ -293,6 +590,7 @@ func (s *wikiIngestService) upsertExtractedPages(
|
|||||||
pageType string,
|
pageType string,
|
||||||
docTitle, lang string,
|
docTitle, lang string,
|
||||||
payload WikiIngestPayload,
|
payload WikiIngestPayload,
|
||||||
|
sourceRef string,
|
||||||
) ([]string, error) {
|
) ([]string, error) {
|
||||||
var affected []string
|
var affected []string
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
@@ -317,7 +615,7 @@ func (s *wikiIngestService) upsertExtractedPages(
|
|||||||
|
|
||||||
existing.Content = updatedContent
|
existing.Content = updatedContent
|
||||||
existing.Summary = item.Description
|
existing.Summary = item.Description
|
||||||
existing.SourceRefs = appendUnique(existing.SourceRefs, payload.KnowledgeID)
|
existing.SourceRefs = appendUnique(existing.SourceRefs, sourceRef)
|
||||||
|
|
||||||
if _, err := s.wikiService.UpdatePage(ctx, existing); err != nil {
|
if _, err := s.wikiService.UpdatePage(ctx, existing); err != nil {
|
||||||
logger.Warnf(ctx, "wiki ingest: save updated page %s failed: %v", item.Slug, err)
|
logger.Warnf(ctx, "wiki ingest: save updated page %s failed: %v", item.Slug, err)
|
||||||
@@ -339,7 +637,7 @@ func (s *wikiIngestService) upsertExtractedPages(
|
|||||||
Status: types.WikiPageStatusDraft,
|
Status: types.WikiPageStatusDraft,
|
||||||
Content: pageContent,
|
Content: pageContent,
|
||||||
Summary: item.Description,
|
Summary: item.Description,
|
||||||
SourceRefs: types.StringArray{payload.KnowledgeID},
|
SourceRefs: types.StringArray{sourceRef},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
logger.Warnf(ctx, "wiki ingest: create page %s failed: %v", item.Slug, err)
|
logger.Warnf(ctx, "wiki ingest: create page %s failed: %v", item.Slug, err)
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ func (s *WikiLintService) RunLint(ctx context.Context, kbID string) (*WikiLintRe
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("get KB: %w", err)
|
return nil, fmt.Errorf("get KB: %w", err)
|
||||||
}
|
}
|
||||||
if kb.Type != types.KnowledgeBaseTypeWiki {
|
if !kb.IsWikiEnabled() {
|
||||||
return nil, fmt.Errorf("KB %s is not a wiki type", kbID)
|
return nil, fmt.Errorf("KB %s is not a wiki type", kbID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,8 +53,8 @@ func (h *WikiPageHandler) validateWikiKB(c *gin.Context) (string, uint64, error)
|
|||||||
return "", 0, errors.NewNotFoundError("Knowledge base not found")
|
return "", 0, errors.NewNotFoundError("Knowledge base not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
if kb.Type != types.KnowledgeBaseTypeWiki {
|
if !kb.IsWikiEnabled() {
|
||||||
return "", 0, errors.NewBadRequestError("Knowledge base is not a wiki type")
|
return "", 0, errors.NewBadRequestError("Wiki feature is not enabled for this knowledge base")
|
||||||
}
|
}
|
||||||
|
|
||||||
return kbID, tenantID, nil
|
return kbID, tenantID, nil
|
||||||
@@ -62,7 +62,7 @@ func (h *WikiPageHandler) validateWikiKB(c *gin.Context) (string, uint64, error)
|
|||||||
|
|
||||||
// getSlugParam extracts and cleans the slug from gin's wildcard path param
|
// getSlugParam extracts and cleans the slug from gin's wildcard path param
|
||||||
func getSlugParam(c *gin.Context) string {
|
func getSlugParam(c *gin.Context) string {
|
||||||
slug := getSlugParam(c)
|
slug := c.Param("slug")
|
||||||
// gin wildcard params include a leading "/"
|
// gin wildcard params include a leading "/"
|
||||||
slug = strings.TrimPrefix(slug, "/")
|
slug = strings.TrimPrefix(slug, "/")
|
||||||
return strings.TrimSpace(slug)
|
return strings.TrimSpace(slug)
|
||||||
|
|||||||
@@ -102,5 +102,6 @@ func RegisterSyncHandlers(params SyncTaskParams) {
|
|||||||
params.Executor.RegisterHandler(types.TypeKnowledgePostProcess, params.KnowledgePostProcess.Handle)
|
params.Executor.RegisterHandler(types.TypeKnowledgePostProcess, params.KnowledgePostProcess.Handle)
|
||||||
params.Executor.RegisterHandler(types.TypeDataSourceSync, params.DataSourceService.ProcessSync)
|
params.Executor.RegisterHandler(types.TypeDataSourceSync, params.DataSourceService.ProcessSync)
|
||||||
params.Executor.RegisterHandler(types.TypeWikiIngest, params.WikiIngest.Handle)
|
params.Executor.RegisterHandler(types.TypeWikiIngest, params.WikiIngest.Handle)
|
||||||
|
params.Executor.RegisterHandler(types.TypeWikiRetract, params.WikiIngest.Handle)
|
||||||
logger.Infof(context.Background(), "[SyncTask] All task handlers registered (Lite mode, no Redis)")
|
logger.Infof(context.Background(), "[SyncTask] All task handlers registered (Lite mode, no Redis)")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,6 +119,7 @@ func RunAsynqServer(params AsynqTaskParams) *asynq.ServeMux {
|
|||||||
|
|
||||||
// Register wiki ingest handler
|
// Register wiki ingest handler
|
||||||
mux.HandleFunc(types.TypeWikiIngest, params.WikiIngest.Handle)
|
mux.HandleFunc(types.TypeWikiIngest, params.WikiIngest.Handle)
|
||||||
|
mux.HandleFunc(types.TypeWikiRetract, params.WikiIngest.Handle)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
// Start the server
|
// Start the server
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ const (
|
|||||||
// KnowledgeBaseTypeDocument represents the document knowledge base type
|
// KnowledgeBaseTypeDocument represents the document knowledge base type
|
||||||
KnowledgeBaseTypeDocument = "document"
|
KnowledgeBaseTypeDocument = "document"
|
||||||
KnowledgeBaseTypeFAQ = "faq"
|
KnowledgeBaseTypeFAQ = "faq"
|
||||||
|
KnowledgeBaseTypeWiki = "wiki"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FAQIndexMode represents the FAQ index mode: only index questions or index questions and answers
|
// FAQIndexMode represents the FAQ index mode: only index questions or index questions and answers
|
||||||
@@ -489,15 +490,8 @@ func (kb *KnowledgeBase) EnsureDefaults() {
|
|||||||
if kb.Type != KnowledgeBaseTypeFAQ {
|
if kb.Type != KnowledgeBaseTypeFAQ {
|
||||||
kb.FAQConfig = nil
|
kb.FAQConfig = nil
|
||||||
}
|
}
|
||||||
// Set defaults for Wiki (wiki is an add-on to document KBs, not a separate type)
|
// Wiki defaults are handled at creation time, not in EnsureDefaults,
|
||||||
if kb.IsWikiEnabled() {
|
// because AutoIngest is a user-configurable toggle (not a missing-field default).
|
||||||
if kb.WikiConfig.WikiLanguage == "" {
|
|
||||||
kb.WikiConfig.WikiLanguage = "auto"
|
|
||||||
}
|
|
||||||
if !kb.WikiConfig.AutoIngest {
|
|
||||||
kb.WikiConfig.AutoIngest = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Set defaults for FAQ
|
// Set defaults for FAQ
|
||||||
if kb.Type == KnowledgeBaseTypeFAQ {
|
if kb.Type == KnowledgeBaseTypeFAQ {
|
||||||
if kb.FAQConfig == nil {
|
if kb.FAQConfig == nil {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ const (
|
|||||||
TypeManualProcess = "manual:process" // 手工知识更新任务(cleanup + 重新索引)
|
TypeManualProcess = "manual:process" // 手工知识更新任务(cleanup + 重新索引)
|
||||||
TypeDataSourceSync = "datasource:sync" // 数据源同步任务
|
TypeDataSourceSync = "datasource:sync" // 数据源同步任务
|
||||||
TypeWikiIngest = "wiki:ingest" // Wiki 页面同步任务
|
TypeWikiIngest = "wiki:ingest" // Wiki 页面同步任务
|
||||||
|
TypeWikiRetract = "wiki:retract" // Wiki 内容回收任务(文档删除时)
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExtractChunkPayload represents the extract chunk task payload
|
// ExtractChunkPayload represents the extract chunk task payload
|
||||||
|
|||||||
@@ -87,14 +87,15 @@ func (WikiPage) TableName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WikiConfig stores wiki-specific configuration for a knowledge base.
|
// WikiConfig stores wiki-specific configuration for a knowledge base.
|
||||||
// Only applicable when KnowledgeBase.Type == "wiki".
|
// Applicable to document-type knowledge bases with wiki feature enabled.
|
||||||
|
// When Enabled is true, document ingestion triggers automatic wiki page generation.
|
||||||
type WikiConfig struct {
|
type WikiConfig struct {
|
||||||
|
// Enabled activates the wiki feature for this knowledge base
|
||||||
|
Enabled bool `yaml:"enabled" json:"enabled"`
|
||||||
// AutoIngest triggers wiki page generation/update when new documents are added
|
// AutoIngest triggers wiki page generation/update when new documents are added
|
||||||
AutoIngest bool `yaml:"auto_ingest" json:"auto_ingest"`
|
AutoIngest bool `yaml:"auto_ingest" json:"auto_ingest"`
|
||||||
// SynthesisModelID is the LLM model ID used for wiki page generation and updates
|
// SynthesisModelID is the LLM model ID used for wiki page generation and updates
|
||||||
SynthesisModelID string `yaml:"synthesis_model_id" json:"synthesis_model_id"`
|
SynthesisModelID string `yaml:"synthesis_model_id" json:"synthesis_model_id"`
|
||||||
// WikiLanguage is the preferred language for wiki content (zh, en, auto)
|
|
||||||
WikiLanguage string `yaml:"wiki_language" json:"wiki_language"`
|
|
||||||
// MaxPagesPerIngest limits pages created/updated per ingest operation (0 = no limit)
|
// MaxPagesPerIngest limits pages created/updated per ingest operation (0 = no limit)
|
||||||
MaxPagesPerIngest int `yaml:"max_pages_per_ingest" json:"max_pages_per_ingest"`
|
MaxPagesPerIngest int `yaml:"max_pages_per_ingest" json:"max_pages_per_ingest"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,9 +31,9 @@ func TestWikiPageTypes(t *testing.T) {
|
|||||||
func TestWikiConfigValueScan(t *testing.T) {
|
func TestWikiConfigValueScan(t *testing.T) {
|
||||||
// Test Value (serialize)
|
// Test Value (serialize)
|
||||||
config := WikiConfig{
|
config := WikiConfig{
|
||||||
|
Enabled: true,
|
||||||
AutoIngest: true,
|
AutoIngest: true,
|
||||||
SynthesisModelID: "model-123",
|
SynthesisModelID: "model-123",
|
||||||
WikiLanguage: "zh",
|
|
||||||
MaxPagesPerIngest: 20,
|
MaxPagesPerIngest: 20,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,9 +58,6 @@ func TestWikiConfigValueScan(t *testing.T) {
|
|||||||
if restored.SynthesisModelID != "model-123" {
|
if restored.SynthesisModelID != "model-123" {
|
||||||
t.Error("SynthesisModelID mismatch")
|
t.Error("SynthesisModelID mismatch")
|
||||||
}
|
}
|
||||||
if restored.WikiLanguage != "zh" {
|
|
||||||
t.Error("WikiLanguage mismatch")
|
|
||||||
}
|
|
||||||
if restored.MaxPagesPerIngest != 20 {
|
if restored.MaxPagesPerIngest != 20 {
|
||||||
t.Error("MaxPagesPerIngest mismatch")
|
t.Error("MaxPagesPerIngest mismatch")
|
||||||
}
|
}
|
||||||
@@ -118,32 +115,36 @@ func TestStringArrayEmpty(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestKnowledgeBaseEnsureDefaultsWiki(t *testing.T) {
|
func TestKnowledgeBaseEnsureDefaultsWiki(t *testing.T) {
|
||||||
kb := &KnowledgeBase{Type: KnowledgeBaseTypeWiki}
|
kb := &KnowledgeBase{Type: KnowledgeBaseTypeDocument, WikiConfig: &WikiConfig{Enabled: true, AutoIngest: true}}
|
||||||
kb.EnsureDefaults()
|
kb.EnsureDefaults()
|
||||||
|
|
||||||
if kb.WikiConfig == nil {
|
if kb.WikiConfig == nil {
|
||||||
t.Fatal("EnsureDefaults should create default WikiConfig for wiki type")
|
t.Fatal("EnsureDefaults should preserve WikiConfig for wiki-enabled KB")
|
||||||
}
|
}
|
||||||
if kb.WikiConfig.AutoIngest != true {
|
if kb.WikiConfig.AutoIngest != true {
|
||||||
t.Error("Default AutoIngest should be true")
|
t.Error("AutoIngest should be preserved")
|
||||||
}
|
|
||||||
if kb.WikiConfig.WikiLanguage != "auto" {
|
|
||||||
t.Errorf("Default WikiLanguage should be 'auto', got '%s'", kb.WikiConfig.WikiLanguage)
|
|
||||||
}
|
}
|
||||||
if kb.FAQConfig != nil {
|
if kb.FAQConfig != nil {
|
||||||
t.Error("Wiki KB should not have FAQConfig")
|
t.Error("Document KB should not have FAQConfig")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that user can disable AutoIngest
|
||||||
|
kb2 := &KnowledgeBase{Type: KnowledgeBaseTypeDocument, WikiConfig: &WikiConfig{Enabled: true, AutoIngest: false}}
|
||||||
|
kb2.EnsureDefaults()
|
||||||
|
if kb2.WikiConfig.AutoIngest != false {
|
||||||
|
t.Error("EnsureDefaults should not override user's AutoIngest=false setting")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKnowledgeBaseEnsureDefaultsDocumentClearsWikiConfig(t *testing.T) {
|
func TestKnowledgeBaseEnsureDefaultsDocumentWithoutWiki(t *testing.T) {
|
||||||
kb := &KnowledgeBase{
|
kb := &KnowledgeBase{
|
||||||
Type: KnowledgeBaseTypeDocument,
|
Type: KnowledgeBaseTypeDocument,
|
||||||
WikiConfig: &WikiConfig{AutoIngest: true},
|
|
||||||
}
|
}
|
||||||
kb.EnsureDefaults()
|
kb.EnsureDefaults()
|
||||||
|
|
||||||
|
// WikiConfig should remain nil when not set
|
||||||
if kb.WikiConfig != nil {
|
if kb.WikiConfig != nil {
|
||||||
t.Error("Document KB should not have WikiConfig after EnsureDefaults")
|
t.Error("Document KB without wiki should not have WikiConfig after EnsureDefaults")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user