Commit Graph

1439 Commits

Author SHA1 Message Date
Claude ae6fde2310 feat(chunker): foundation for adaptive 3-tier chunking
Adds the building blocks for tiered chunking strategies — multilingual
patterns, token approximation, document profiling, validation, and a
strategy resolver that today still falls through to the legacy
SplitText. Default Strategy is empty/legacy so existing knowledge bases
continue to chunk identically.

- tokens.go: char/token approximation per language (en/de/zh/mixed)
  and a coarse DetectLanguage heuristic
- patterns.go: source-of-truth regex bibliothek for headings, numbered
  sections, multilingual chapter markers, visual separators, etc.
- profiler.go: single-pass document scan + SelectStrategy chain
- validator.go: rejects obviously broken chunk-sets to drive fallbacks
- strategy.go: public Split / SplitParentChild entry points; resolves
  StrategyAuto via the profiler, anything else maps to a fixed tier
- splitter.go: SplitterConfig gains Strategy / TokenLimit / Languages
  fields and DefaultChunkSize / DefaultChunkOverlap constants (overlap
  raised to 80 ≈ 15%)
- knowledge.go: switches both call sites to chunker.Split /
  SplitParentChild — behavior unchanged for empty Strategy

https://claude.ai/code/session_01XADhx6mtu2ZYW3DE9Lun6k
2026-05-06 17:17:07 +08:00
voidborne-d 46cab52fb0 fix(storage): merge tos/s3/oss/ks3 tenant configs in buildStorageConfig (#1117)
knowledgeService.buildStorageConfig had hardcoded switches in two places that
only handled local/minio/cos. Knowledge bases configured for tos, s3, oss, or
ks3 reached the docreader with a DocParserStorageConfig containing only the
Provider tag — bucket, endpoint, and credentials were silently dropped, and
extraction stalled or fetched from the wrong location.

- Tenant-merge switch: add tos/s3/oss/ks3 cases mapping
  StorageEngineConfig.{TOS,S3,OSS,KS3} fields onto DocParserStorageConfig.
  Same shape as the existing minio/cos branches; AccessKey -> AccessKeyID,
  SecretKey -> SecretAccessKey, no AppID.
- Legacy compat switch: list tos/s3/oss/ks3 alongside local with hasKBFull=false
  so fall-through to the tenant-merge path is intentional rather than an
  unrecognised provider sliding past the switch. The legacy `cos_config` column
  predates these providers and they should never resolve via that path.
- Doc drift: types/custom_agent.go ImageStorageProvider comment and
  handler/system.go StorageEngineStatusItem.Name comment now list all 7
  providers (matched against types.StorageEngineConfig and ParseProviderScheme).

Tests (knowledge_storage_config_test.go, +268 LOC, 17 cases):
- TestBuildStorageConfig_TenantMergeAllProviders pins per-provider field
  population for all 7 providers; the tos/s3/oss/ks3 sub-tests are the
  regression guard for #1117.
- TestBuildStorageConfig_LegacyPathOnlyForCOSAndMinIO pins that legacy
  StorageConfig data is only consumed for cos/minio and skipped for the
  others (intentional fall-through).
- TestBuildStorageConfig_NoTenantFallsThroughToEmpty pins the no-tenant case.

Out of scope (called out in the linked issue):
- The systemic refactor (single source-of-truth provider registry) — left as
  a follow-up; this PR keeps changes local to the buggy code path.
- initialization.go validateStorageEngineConfig multimodal validation gap —
  the InitializationRequest struct does not carry tos/s3/oss/ks3 fields, so
  fixing it requires API surface additions; can ship separately.

Closes #1117 (the buildStorageConfig switches; remaining items deferred).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 17:09:42 +08:00
lyingbug 0d8540c93c Revert "refactor(knowledge): split 9.8k-line service file by responsibility"
This reverts commit f67d0b53ce.
2026-05-06 17:09:24 +08:00
wizardchen f67d0b53ce refactor(knowledge): split 9.8k-line service file by responsibility
internal/application/service/knowledge.go had grown to 9883 lines /
149 functions spanning CRUD, document processing, summary/question
generation, clone/move, FAQ (CRUD+import+index+export), wiki cleanup,
multimodal image, and file utilities. Navigating it, or diffing
changes in it, was increasingly painful.

Purely mechanical split into 8 files in the same package. No
signatures, behaviors, or types were modified. knowledgeService
struct, NewKnowledgeService, and package-level errors remain in
knowledge.go unchanged. Per-file imports were trimmed by goimports.

  knowledge.go              497  struct + constructor + errs + CRUD reads + Search
  knowledge_create.go      1144  CreateFrom{File,URL,Passage,Manual} + helpers
  knowledge_delete.go       686  Delete + wiki cleanup + ProcessKnowledgeListDelete
  knowledge_process.go     2407  ProcessDocument/Summary/Question + Reparse + UpdateImageInfo
  knowledge_clone_move.go  1049  Clone* + Move* + progress persistence
  knowledge_faq.go         1932  FAQ CRUD + Search + Export
  knowledge_faq_import.go  1939  FAQ import/validate/index + runningFAQImportInfo
  knowledge_util.go         352  file-type/url/hash + VLM+Storage config + resolveFileService

Verification: go build, go vet, gofmt on the 8 files, and
golangci-lint all clean (22 issues post-split matches 22 pre-split
in knowledge.go — no new issues introduced). The pre-existing
unused warnings on getVLMConfig / buildStorageConfig were left as-is
rather than removing dead code beyond the scope of this task.
2026-05-06 17:06:05 +08:00
sqkstwj 0e19c978ae fix(files): fall back to global file service when tenant storage config is unavailable 2026-05-06 17:04:38 +08:00
wizardchen d46a51cf6e fix(security): forbid <style> in sanitized document HTML
DOMPurify was configured with USE_PROFILES.svg, which implicitly allows
<style> tags so that inline SVG styling works. User-authored markdown
rendered via v-html therefore let a pasted nginx error page's
<style>body { width: 35em; margin: 0 auto }</style> through the
sanitizer and apply as global CSS, squashing the entire app layout.

Add 'style' to FORBID_TAGS so top-level style elements from document
content are dropped regardless of the SVG profile.
2026-05-06 16:41:20 +08:00
wizardchen 6ac2f964ca feat(kb-list): replace type column with source and add description subtitle
Document list view relied on a redundant Type column (file extension is
already shown in the filename and icon) while hiding where a document
came from — external syncs from Feishu/Notion/Yuque, web URLs, manual
entries, and uploaded files were indistinguishable.

Swap Type for a Source column that resolves from `channel` first and
falls back to `type`, and surface `description` as a 12px subtitle under
the filename so the list view no longer requires hovering to read the
summary.
2026-05-06 16:41:20 +08:00
wizardchen 2d3f32de5c feat(frontend): replace standalone search page with global command palette
Reworks search from a siloed /platform/knowledge-search page into a
universal ⌘K palette plus natural, in-context entry points.

Why the old page felt out of place:
- "Search" was an action pinned next to entity routes (knowledge base,
  agent, organization), mixing noun- and verb-level navigation items.
- Its two tabs (knowledge vs. chat messages) forced users to pick a
  mental model before they knew what they wanted.
- It was also the only way to search at all — no global shortcut, no
  context-aware entry.

What this replaces it with:
- A new ⌘K/Ctrl+K command palette (GlobalCommandPalette.vue) mounted
  once in views/platform/index.vue. Results are grouped by type
  (files, messages, knowledge bases, agents, chat titles, commands)
  so users never need to pre-filter. Debounced, identity-keyed
  selection survives async arrivals (slow KB results no longer yank
  the highlight off the user's current row). ⌘1-9 jump shortcuts are
  rendered inline on each row for discoverability.
- Sidebar gets a visible "Search" entry at the top — clicking it
  opens the palette (no route change), with a platform-aware kbd hint
  (⌘K on Mac, Ctrl K elsewhere).
- KB detail page: opening ⌘K there auto-scopes the search to the
  current KB via a removable chip (Backspace in empty input also
  clears it); no separate embedded search bar needed.
- The legacy route /platform/knowledge-search now redirects to the KB
  list with ?cmdk= set, so existing bookmarks still work.
- KnowledgeBase.vue now handles both route-query changes and a
  window event (weknora:open-knowledge) for intra-KB navigation,
  fixing the silent-no-op when the palette jumps to a chunk in the
  already-open KB.

Dropped:
- frontend/src/views/knowledge/KnowledgeSearch.vue (1163 lines).
- The exploratory SessionSearchBar mode-switching widget in the chat
  submenu; ⌘K covers its use cases without a two-mode UI.

i18n: zh-CN / en-US / ko-KR / ru-RU updated together — new
commandPalette.*, kb.*, and menu.search keys; obsolete session.search.*
and menu.knowledgeSearch removed.
2026-05-06 16:26:22 +08:00
wizardchen 0be4572bd5 feat(kb-list): group pinned knowledge bases into a labeled section
On the KB list page, pinned items already sort to the top but look
identical to the rest, so at a glance you can't tell where the pinned
group ends. Add lightweight section headers ("置顶" / "其他") that
span the grid when the list transitions from pinned to unpinned, in
both the "全部" and "我的" tabs. Shared KBs never enter the pinned
group (backend doesn't pin them).

No divider line, no backend change — purely a visual grouping on the
existing sort order.
2026-05-06 12:33:04 +08:00
Claude c3e53a3fc4 test(ingest): cover insufficient-content gate; allow threshold override
Addresses two pre-merge review notes:

1. Extract the sentinel-returning threshold check from getSummary into
   a small package-private helper (checkSufficientSummaryContent) and
   add a unit test for it. The new test pins the behavior the caller
   in ProcessSummaryGeneration relies on: the sentinel is returned for
   empty / image-only / sub-threshold / scanned-PDF-without-OCR inputs,
   and nil is returned for legitimate short notes and scanned PDFs
   where VLM OCR succeeded.

   The downstream "errors.Is(err, errInsufficientSummaryContent) →
   SummaryStatusFailed + skip summary chunk" branch is mechanical (no
   branching beyond the errors.Is check); covering it end-to-end would
   require mocking KnowledgeRepository / ChunkRepository / chat.Chat,
   for which there is no precedent in this package.

2. Switch minTextContentRunes from const to var so tests can override
   the threshold and so future config plumbing can adjust it at
   runtime without a rebuild. The new test exercises the override.
2026-05-06 11:23:32 +08:00
Claude 7ca4b2f53c chore(comment): update slug source to knowledge ID
Drive-by comment fix: extractEntitiesAndConceptsNoUpsert still claimed
summary slugs are code-generated from the document title. Since the
previous commit they are derived from the knowledge ID instead.
Pure documentation change, no behavior impact.
2026-05-06 11:23:32 +08:00
Claude 6744e1cdd5 fix(ingest): QA follow-up — preserve OCR text and close remaining filename leaks
Self-review of the prior commit surfaced one critical regression and
several smaller issues. This change addresses them.

Critical: the old stripImageMarkup stripped the WHOLE <image>...</image>
block, but searchutil.EnrichContentWithImageInfo wraps successful VLM OCR
output in exactly that shape — so a scanned PDF where OCR worked was
being rejected by hasSufficientTextContent and routed to the same
"failed" path as the OCR-empty case. The fix splits stripping into:
  * <image_original>...</image_original> blocks removed entirely
    (they only carry the redundant Markdown ref already stripped above)
  * <image>, <images>, <image_caption>, <image_ocr> tags removed at
    the tag level, INNER TEXT PRESERVED, so OCR / caption text reaches
    the threshold check and the LLM call.

A regression test pins this down with realistic enriched-image XML that
the previous code failed silently on.

Other fixes:
  * Threshold lowered from 50 to 10 runes — short legitimate documents
    (brief memos, single-line notes) must still pass.
  * Summary chunk content no longer prefixes "# Document\n<filename>";
    only the LLM-generated summary is indexed, so filenames cannot
    leak back into RAG retrieval context.
  * Wiki summary slug is now derived from the knowledge ID rather than
    the upload filename, eliminating filename-based slugs like
    "summary/mx5280-pdf" that downstream wiki-edit prompts can read.
  * Citation sourceRef likewise drops the "|<docTitle>" suffix.
  * Dead docTitle parameter removed from extractCandidateSlugs,
    extractEntitiesAndConceptsNoUpsert, classifyChunkCitations.
  * The redundant stripImageMarkup pass in the warning log is gone;
    realTextRuneCount caches the count once and uses
    utf8.RuneCountInString to avoid the rune-slice allocation.

Wiki-page display titles still flow through DocTitle (filename) into
later wiki-edit / index-rebuild prompts. That is a larger refactor of
the wiki pipeline and is intentionally left for a separate PR.
2026-05-06 11:23:32 +08:00
Claude 15550d14d4 fix(ingest): stop hallucinating summaries from filenames on empty PDFs
Scanned PDFs without a successful OCR pass (e.g. when no VLM is configured
on the KB, or when the VLM returned a "no text content" reply that the
sanitizer discarded) reach the summary and wiki-ingest pipelines with
nothing but markdown image references for content. The downstream LLM
prompts used to receive the filename, file type, and title alongside that
empty content, and the generate_summary template explicitly forbade the
model from refusing — so the model would invent a topic from the filename
alone, producing e.g. a "Canon scanner manual" summary for an MX5280.pdf
that is actually a scanned legal letter.

Two changes shut this down:

1. Drop filename / title / file-type from every LLM prompt input. The
   wiki summary, knowledge extract, candidate slug, and chunk citation
   prompts now see only the document content. The knowledge summary
   path no longer prepends the "Document Type: X / File Name: Y" intro.

2. Add a content-sufficiency guard in front of the LLM call. A new
   helper strips markdown image references and HTML image tags and
   refuses to invoke the model when fewer than 50 real-text runes
   remain. getSummary returns a sentinel error in that case and the
   caller marks the knowledge as SummaryStatusFailed instead of
   surfacing the first chunk's bare image reference. Wiki ingest skips
   LLM extraction with a clear log message.

The generate_summary.yaml prompt is also updated: the "NEVER output
refusal phrases" clause is removed and replaced with an explicit
empty-content rule that returns a deterministic placeholder line.

Real OCR (Tesseract / PaddleOCR) and filename-pattern sanitisation are
intentionally left for separate PRs.
2026-05-06 11:23:32 +08:00
guangyang1206 d3d433574f fix(data-analysis): capture LOCAL_STORAGE_BASE_DIR at init time (#1040)
resolveFileServiceForKnowledge was reading os.Getenv("LOCAL_STORAGE_BASE_DIR")
on every request.  This causes a mismatch when the variable is not in the
process environment at request time (e.g. it was set programmatically before
app startup but not exported, or the DI-constructed fileService uses a
different configured path).

Fix: store the value in a new localBaseDir field at NewDataAnalysisTool
construction time — the same moment the DI container assembles the local
FileService — and use t.localBaseDir in resolveFileServiceForKnowledge
instead.  This guarantees the data-analysis tool always resolves files
under the same base directory that the local FileService was configured
with, eliminating the "path not found" errors when LOCAL storage is used
for a knowledge base while the global storage backend is minio/oss/cos.

Fixes #1040
2026-05-06 11:19:56 +08:00
c e938f1c6ae test(agent): expand graph query regression fixture 2026-05-06 11:04:55 +08:00
c b2ff29cc0f fix(agent): correct graph config reporting 2026-05-06 11:04:55 +08:00
c f08192ba26 fix: include shared knowledge in knowledge search 2026-05-05 01:04:49 +08:00
toy0116 05d35c904b feat(frontend): respect BASE_URL so the SPA works behind a path-prefix reverse proxy
When WeKnora is mounted under a sub-path (Kubernetes Ingress with a
path-prefix rule, an Nginx `location /weknora/` block, a Cloudflare
Tunnel route, or any other reverse-proxy gateway), the frontend's
`getApiBaseUrl()` currently returns `''` and axios issues `/api/v1/...`
requests at the origin root — bypassing the sub-path entirely and
404'ing every backend call.

This change makes `getApiBaseUrl()` honor Vite's
`import.meta.env.BASE_URL` so that, when the bundle was built with e.g.
`vite build --base /weknora/`, axios produces `/weknora/api/v1/...`.
Trailing-slash stripped to avoid double slashes (`/weknora//api/v1/...`).

For deployments at the origin root (the common case), `BASE_URL`
defaults to `'/'` which strips to `''` — behavior unchanged from
upstream. No new build flag required; no behavior change for existing
users.

Reproduce the original problem:

  1. cd frontend && vite build --base /weknora/
  2. Serve dist/ behind Nginx at `location /weknora/`, proxy
     /weknora/api/* → backend.
  3. Open https://example.com/weknora/ → 404 on every API request
     because axios calls /api/v1/auth/test instead of
     /weknora/api/v1/auth/test.

After the fix the same setup just works.
2026-05-05 00:58:19 +08:00
wizardchen e4fe9d7d9b fix(i18n): remove duplicate liveIndicator key in ko-KR locale
Vite raised `Duplicate key "liveIndicator" in object literal` at
ko-KR.ts:4107. Both entries mapped to the same Korean translation so
the second is simply redundant. Removing it silences the warning
without changing any user-visible string.

Verified no other duplicate keys remain in en-US / zh-CN / ko-KR /
ru-RU with a brace-scope-aware scan.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 00:54:43 +08:00
Claude 35a5e29a23 fix(frontend): drop infectious preference fallback, add migration latch
QA review of the previous hardening commit identified two cross-user
infection paths created by the smart-fallback read chain (CRIT-1/2),
plus a UI-desync hazard from validation guards in setters (HIGH-1).

- Extracted localStorage helpers and migration logic into
  composables/preferenceStorage.ts as a single source of truth.
- Reads now hit only the active user's namespace - no anon / legacy
  fallback in the hot path. Pre-existing values are migrated **once**
  per active user via migratePreferencesIntoUser, which adopts anon
  and legacy keys into the user namespace and then deletes the source
  keys, so a second user logging into the same browser cannot inherit
  the previous user's preferences.
- Migration latch tracks which user has already been migrated this
  session; auth store's reloadUserPreferences resets the latch on
  setUser/logout so the next active user gets a fresh migration.
- setSansFont / setMonoFont / setFontSize / setTheme now return a
  boolean; GeneralSettings.vue handlers roll the form back to the
  canonical state when validation rejects a value, preventing the
  UI from drifting out of sync with the source of truth.
- safeSetItem now console.warn on persistence failures so quota /
  private-mode issues are at least diagnosable.
- applyFont is diff-aware: only the CSS variables that actually
  changed are written to the root style, avoiding unnecessary style
  recalculation when only one knob is flipped.
- applyTheme caches the last effective mode to skip redundant
  attribute writes and Wails IPC calls.

https://claude.ai/code/session_01S1FVPf4jRfWXkTSnpMmMp7
2026-05-05 00:48:40 +08:00
Claude 2e0b423057 fix(frontend): harden font/theme preferences for production edge cases
Edge-case review identified several issues that would degrade UX or
silently break under unusual conditions:

- Anon settings chosen on the login screen were lost on first login
  because the storage namespace switched from "anon" to the user id;
  resolveStorageValue now falls through user -> anon -> legacy, so
  pre-login choices carry over and v1 unscoped keys are still picked up.
- localStorage.setItem can throw under quota / private mode; setters now
  swallow the error so the UI still updates for the session.
- applyFont reduced to String(undefined) when currentSize held an
  invalid value; runtime fallbacks to the default scale guard against
  manipulated storage.
- Added var(--app-font-scale, 1) defaults to all 32 calc() expressions
  in theme.css and the body font-size, so a missing variable cannot
  void the calc() value.
- Validation guards in setSansFont / setMonoFont / setFontSize / setTheme
  reject invalid keys instead of writing them to storage.
- GeneralSettings.vue now watches the composable refs so the form stays
  in sync if preferences change externally (e.g. user switch while on
  the page).
- Dropped the unverified t-radio-group variant prop in favour of the
  default style.

https://claude.ai/code/session_01S1FVPf4jRfWXkTSnpMmMp7
2026-05-05 00:48:40 +08:00
Claude bafcbd5e5f feat(frontend): add font size, namespace UI prefs per user
Build on the font picker with three additions per user feedback:

- Font size selector (Small / Normal / Large) wired through a new
  --app-font-scale CSS variable; the body font-size and every
  --td-font-size-* token are wrapped in calc(... * var(--app-font-scale))
  so TDesign components scale together with the body
- localStorage keys for theme, font family, mono font and font size are
  now namespaced as WeKnora_${userId}_${key}; falls back to "anon" before
  login. Each user keeps their own preferences in the same browser
- auth store calls reloadFontFromStorage / reloadThemeFromStorage on
  setUser (when the active user changes) and in logout, so a user switch
  swaps the applied preferences without a reload

https://claude.ai/code/session_01S1FVPf4jRfWXkTSnpMmMp7
2026-05-05 00:48:40 +08:00
Claude f140c12518 feat(frontend): unify font rendering and add font picker in settings
The UI used hardcoded "PingFang SC" in 50+ components, while the global
font stack in App.vue used a different fallback chain. Outside of mainland
China this caused visibly different fonts in the sidebar, breadcrumbs,
search and agent views compared to settings and document panes.

- Introduce --app-font-family / --app-font-family-mono CSS variables in
  theme.css and route TDesign's --td-font-family through them
- Add a useFont composable that persists the user's choice in localStorage
  and applies it via document.documentElement.style before mount
- Replace hardcoded font-family declarations across the app with the new
  variables (TencentSans logo and the SVG attribute list are preserved)
- Add UI font and monospace font selectors in General Settings with
  i18n strings for en-US, zh-CN, ko-KR and ru-RU

https://claude.ai/code/session_01S1FVPf4jRfWXkTSnpMmMp7
2026-05-05 00:48:40 +08:00
wizardchen 6c1442aaf9 fix(im): clamp reconnect backoff to avoid int64 overflow → busy loop
After ~34 failed reconnects, math.Pow(2, attempt-1) * baseDelay overflowed
int64 and produced a negative time.Duration, which bypassed the max-delay
cap. time.After(-1s) fires immediately, so a single bad credential (e.g.
WeCom bot_id/secret error 853000) turned into thousands of reconnects
per minute and massive log spam ("reconnecting in -1s (attempt 4601)").

Replace math.Pow with a bit-shift and cap the exponent at 30, which is
well above the shift needed to reach the 30s max delay. Same fix applied
to the WeChat long-poll client. Added a regression test that exercises
attempt=4601.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 00:36:18 +08:00
ChenRussell 42348eb40d feat(ks3): add KS3 object storage support
Add Kingsoft Cloud KS3 as a supported storage provider, including
  backend service implementation, storage config UI, and provider
  scheme recognition throughout the codebase.
2026-05-05 00:22:27 +08:00
Windfarer 6f3679716f fix oidc auth login 2026-05-05 00:21:41 +08:00
bingxiang.cheng db8abd3f9b feat(openmaic-classroom): 增加基于知识图谱概念的微课堂生成支持,更新需求构建模板 2026-05-04 23:54:15 +08:00
IFeynman 19cd17c0a6 fix(wiki): apply reviewer's detailed resolution for contradiction handling 2026-05-04 23:52:38 +08:00
IFeynman cf8e62fa9e fix(wiki): address PR review feedbacks regarding citation format, rule conflicts, and search loops 2026-05-04 23:52:38 +08:00
IFeynman b313da20ae feat(wiki): enforce strict citation tracing and ontology reuse in Wiki prompts 2026-05-04 23:52:38 +08:00
Andy Yang 3d94a476bf fix(memory): dedupe episode write on duplicate streaming Done
AgentFinalAnswer may carry Done:true more than once (e.g. finish chunk
plus EOF). Guard AddEpisode with sync.Once so memory persists once per
turn, consistent with qa.go completion handling.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-04 23:49:29 +08:00
c 48aeafcc3f fix(chat): include history in model fallback 2026-05-04 23:24:21 +08:00
toy0116 8955bf1aa8 fix(embedding): OpenAIEmbedder.doRequestWithRetry shadows err · returns (nil, nil) on connection failure · caller nil-derefs and SIGSEGVs
Reproduce:

  1. Run WeKnora pointing OPENAI_BASE_URL at any OpenAI-compatible
     embedding endpoint.
  2. Stop that endpoint.
  3. Issue any RAG-mode chat / knowledge_search query — the pipeline
     reaches chunk_search_parallel → knowledgeBaseService.GetQueryEmbedding
     → OpenAIEmbedder.Embed → OpenAIEmbedder.BatchEmbed.
  4. Observe the entire WeKnora process SIGSEGV instead of returning a
     graceful 5xx.

Stack (real, observed in production):

  panic: runtime error: invalid memory address or nil pointer dereference
  [signal SIGSEGV: segmentation violation code=0x2 addr=0x40]

  internal/models/embedding/openai.go:195 +0x458   BatchEmbed
  internal/models/embedding/openai.go:93  +0xb0    Embed
  internal/application/service/knowledgebase_search.go:35
                                                   GetQueryEmbedding
  internal/application/service/chat_pipeline/search.go:371
                                                   searchByTargets.func1

Root cause — variable shadowing in doRequestWithRetry:

  func (e *OpenAIEmbedder) doRequestWithRetry(...) (*http.Response, error) {
      var resp *http.Response
      var err error                                        // outer err
      ...
      for i := 0; i <= e.maxRetries; i++ {
          ...
          req, err := http.NewRequestWithContext(...)      // ← `:=` shadows
                                                           //   outer `err`
                                                           //   because `req`
                                                           //   is new in
                                                           //   the loop scope
          if err != nil { ... continue }
          ...
          resp, err = e.httpClient.Do(req)                 // writes to the
                                                           // shadowed
                                                           // loop-local err
          if err == nil { return resp, nil }
          logger.Errorf("attempt %d failed: %v", i+1, err) // logs the inner err
      }
      return nil, err                                      // returns OUTER
                                                           // err — never set
  }

When httpClient.Do returns connection errors on every attempt, the loop
logs them faithfully but the outer err stays nil. The function returns
(nil, nil). Caller in BatchEmbed reads:

  resp, err := e.doRequestWithRetry(ctx, jsonData)
  if err != nil { return nil, ... }       // skipped (err is nil)
  if resp.Body != nil {                   // ← line 195 · resp itself is nil
      defer resp.Body.Close()             //   nil pointer dereference
  }                                       //   → SIGSEGV → process dies

Fix — declare `req` separately so the shared `err` variable is not shadowed:

  var req *http.Request
  req, err = http.NewRequestWithContext(...)               // outer err

After the fix, when all retries fail, doRequestWithRetry returns the real
error (e.g. `dial tcp 127.0.0.1:11434: connect: connection refused`),
the caller propagates it as a regular error, the chat pipeline returns a
5xx to the client, and the process keeps running.

Verified by stopping the embedding upstream while WeKnora was serving
queries · process stayed up · returned graceful errors · resumed normal
operation when the upstream came back. Without the patch the same
sequence killed the process within one query.
2026-05-04 23:18:42 +08:00
wizardchen 58a283effe docs(readme): add Chrome Extension and ClawHub Skill badges 2026-04-30 17:47:39 +08:00
wizardchen 0c9d7040f4 chore: release v0.5.1
Bump version to 0.5.1 across VERSION, frontend package files, and
Helm chart, and refresh CHANGELOG plus the four-language README
"Latest Updates" section to merge v0.5.1 highlights with the v0.5.0
core feature set.
2026-04-30 17:44:26 +08:00
wizardchen fe0d24ae87 fix(sessions): review fixes for keyword search / pinning / IM titles
- Apply the session-list additions to the SQLite init migration (Lite build)
  and make QueryPaged dialect-aware: LOWER(..) LIKE on SQLite, ILIKE on
  Postgres, drop NULLS LAST on SQLite. Escape LIKE wildcards in the keyword
  via the existing escapeLikeKeyword helper.
- SetPinned now returns rowsAffected so the handler can respond 404 on
  unknown / unauthorized session IDs instead of a misleading 200.
- GET /sessions always returns the enriched shape (pin state + IM origin
  fields) so the frontend never needs a second roundtrip; the dual-path
  legacy branch in the handler is gone.
- Register pin routes with the wildcard name that matches each verb's
  existing radix tree (POST :session_id, DELETE :id) and accept either
  param name in the handler; avoids gin's "wildcard conflicts" panic.
- Drop the redundant [platform] prefix from IM session titles now that
  the list renders a platform icon alongside the title; add unit tests
  for shortID / buildUserSessionTitle / buildThreadSessionTitle.
- Frontend: remove the submenu search input and its i18n/keyword wiring
  (search lives elsewhere in the app); pin icon uses the TDesign `pin`
  glyph and inherits color so the active session turns green; optimistic
  pin moves the item to the top of the list so it shows up at the top
  of the Pinned group; IM text badge replaced by the platform SVG icons
  from assets/img/im, desaturated by default and full-color on hover
  or when the session is active.
2026-04-30 16:23:00 +08:00
wizardchen dbd804d6e3 feat(sessions): add keyword search, user-scoped pinning, and IM source visibility
Sessions today are a flat list ordered by updated_at. Two gaps showed up in
practice:
- Users cannot find specific chats as the list grows beyond a screen.
- IM-created sessions (WeCom/Feishu/Slack/...) are indistinguishable:
  every title was "IM-<platform>" or "IM-<platform>-<username>" and the
  list API hid the underlying im_channel_sessions mapping, so admins had
  no way to tell which Feishu group a session came from.

Backend
- Migration 000039 adds sessions.user_id (owner), is_pinned, pinned_at
  plus a composite index for the list query. Existing rows keep user_id
  NULL and stay visible at the tenant level for backward compatibility.
- CreateSession now writes the caller's user_id from auth context.
- GET /sessions accepts keyword / source / agent_id. When any filter is
  set, the response switches to enriched items that LEFT JOIN
  im_channel_sessions and expose im_platform / im_chat_id / im_thread_id
  / im_user_id / im_agent_id / im_channel_id. No filters => legacy shape,
  existing clients unaffected.
- Ordering: is_pinned DESC, pinned_at DESC NULLS LAST, updated_at DESC.
- POST/DELETE /sessions/:id/pin for user-scoped pin/unpin.
- IM session titles: "[platform] <user|chat|thread>" with short ID
  suffixes so group/DM/thread sessions are visually distinct without
  needing a round-trip to fetch a display name from the IM adapter.

Frontend
- Search input debounced at 300ms drives the keyword filter.
- Pinned chats render in a dedicated group above the time-based groups,
  with a pin icon and a pin/unpin entry in the per-chat dropdown.
- IM chats get a short [platform] badge in the list.
- Pin toggle is optimistic and guards against double-clicks.
- zh/en/ko/ru i18n keys added for the new strings.
2026-04-30 16:23:00 +08:00
wizardchen e029d31f86 feat(im): tenant-wide IM channels overview under the user menu
Until now the IM channels a tenant has connected were only visible from
inside each agent's editor (AgentEditorModal → IMChannelPanel). Finding
out which bots are live across all agents meant clicking through every
one of them. Add a tenant-scoped overview so the set of connected IMs is
always one hop away from the avatar.

Backend

- im.Service.ListChannelsByTenant(tenantID) returns all non-deleted
  channels in the tenant, LEFT JOIN'd with custom_agents.name so
  built-in agents (which don't have rows in custom_agents) still show
  up with an empty agent_name; the frontend substitutes a localized
  "built-in agent" label.
- Credentials are intentionally stripped from this response — the list
  view is read-only and the editor route (GET /agents/:id/im-channels)
  remains the only source for secrets.
- New ChannelWithAgent DTO + IMHandler.ListAllIMChannels wired at
  GET /api/v1/im-channels under the existing RegisterIMChannelRoutes
  group, so auth middleware is inherited.

Frontend

- IMChannelsOverviewPanel.vue: floating submenu pane, stacked rows
  (agent on top, channel below) with identical 20px avatars and 12px
  labels so neither identity dominates. Each row is the whole click
  target (jumps to the agent editor); the switch uses @click.stop to
  toggle in place without triggering navigation.
- UserMenu.vue: new hover-driven submenu entry ("已接入的 IM" + link
  icon). The pane is teleported to <body> because the sidebar container
  has overflow:hidden that would otherwise clip right-flying content.
  Position is computed from the menu item's rect and clamps/flips
  against the viewport edges.
- "Live" indicator: a pulsing green dot on the menu item when at least
  one channel is enabled. UserMenu prefetches the list once on mount;
  the panel re-emits channels-changed after every load/toggle so the
  dot stays in sync. Respects prefers-reduced-motion.
- AgentList.vue: supplement the existing onMounted-only
  checkAndOpenEditModal with a watch(route.query.edit) so
  router.push({ path: '/platform/agents', query: { edit, section } })
  from an already-mounted AgentList (the common case when navigating
  from the overview) opens the editor immediately instead of only
  after a hard refresh.

Assets

- Seven platform SVGs under assets/img/im/, pulled from iconify
  (simple-icons / logos / tdesign / icon-park / remix icon) and baked
  with brand colors so monochrome sources (wecom/wechat/dingtalk/
  feishu) don't render black.

i18n

- New imOverview namespace in zh-CN / en-US / ko-KR / ru-RU covering
  the menu label, panel title/subtitle, column headers, builtin-agent
  fallback, and the live-indicator tooltip.

Tested

- go build ./...
- go test ./internal/im/...
- vue-tsc --noEmit
- npm run build
2026-04-30 15:29:14 +08:00
sqkstwj a5f80b747c fix(web-search): normalize default tenant web search config at runtime 2026-04-30 15:28:35 +08:00
wizardchen 3b23713c54 fix(crypto): fail loudly when encrypted DB fields cannot be decrypted
GORM Scan / AfterFind hooks for every AES-encrypted column followed
the same lenient pattern:

    if decrypted, err := utils.DecryptAESGCM(c.APIKey, key); err == nil {
        c.APIKey = decrypted
    }

When SYSTEM_AES_KEY was missing, rotated, or the wrong length the
decryption error was swallowed and the in-memory struct kept the raw
"enc:v1:..." ciphertext. The application then happily forwarded the
ciphertext upstream as the actual API key / password, surfacing as
401/403/SignatureDoesNotMatch from third-party vendors. Worse, a
ciphertext snippet of a customer credential was leaking into the
external provider's request logs.

Introduce utils.DecryptStoredSecret that:
  - returns "" / legacy plaintext untouched (no false positives for
    pre-encryption rows);
  - returns ErrEncryptedDataMissingKey when the value carries the
    enc:v1: prefix but no AES key is configured;
  - propagates any GCM auth-tag failure from a rotated key.

Wire it into the five Scan / AfterFind sites that currently swallow
the error:
  - Tenant.AfterFind (tenants.api_key)
  - CredentialsConfig.Scan (tenants.we_knora_cloud.app_secret)
  - ModelParameters.Scan (models.parameters.api_key, .app_secret)
  - ConnectionConfig.Scan (vector_store_connections.password, .api_key)
  - WebSearchProviderParameters.Scan (web_search_providers.api_key)

The error is wrapped with the originating column so the failure is
diagnosable in the logs. Operators must restore the previous
SYSTEM_AES_KEY (or rotate the affected secrets) instead of receiving
silent vendor 401s.

Add table-driven tests covering empty input, legacy plaintext,
round-trip, missing key, wrong-length key, and rotated key. The
strict path explicitly asserts that ciphertext does NOT leak in the
returned plaintext on any error path.
2026-04-30 15:17:56 +08:00
wizardchen 097c9d0ad5 fix(im): revert tenant-from-context for presigned URL
Reverts the tenant-ID-from-context change in da5fcd3. The storage path
encodes the resource owner's tenant, and the presigned-URL verifier
uses that ID to look up the owning tenant's StorageEngineConfig. Using
the caller's tenant ID from context would break cross-tenant shared
resources — e.g. when tenant Y reads an image from a KB shared by
tenant X, signing with Y would cause the verifier to open Y's storage
backend and 404 on X's file.

Keep:
- presignDefaultTTL shortened from 24h to 2h (independent improvement).
- godoc note on ParseTenantIDFromStoragePath flagging the ambiguity
  for cloud paths with numeric bucket/region names.
- Unit tests for path-based tenant extraction and the no-external-URL
  backward-compat path.
2026-04-30 11:40:30 +08:00
wizardchen f27a1e083c fix(im): prefer tenant ID from context, shorten presigned URL TTL
Addresses review feedback on the IM storage URL rewrite:

- localFileService.GetFileURL now reads tenant ID from request context
  first, falling back to ParseTenantIDFromStoragePath only when context
  is absent. Fixes ambiguity for cloud providers whose paths embed
  numeric bucket/region names before the tenant segment, which could
  mint presigned URLs bound to the wrong tenant ID.
- Shorten presigned URL default TTL from 24h to 2h. A leaked HMAC key
  authorizes cross-tenant file reads, so URLs should expire quickly;
  IM clients fetch referenced images within seconds anyway.
- Document ParseTenantIDFromStoragePath as a best-effort fallback.
- Add unit tests covering context-first, path-fallback, and the
  no-external-URL backward-compat path.
2026-04-30 11:40:30 +08:00
wizardchen 7fd566bc15 fix(im): rewrite private storage URLs to HTTP in IM channel replies
IM platforms (Feishu, Slack, Telegram, DingTalk, Mattermost, WeCom) cannot
render provider:// URLs (local://, minio://, s3://, etc.) that appear in
LLM answers containing knowledge base images. The web frontend handles
these via the authenticated /files endpoint, but IM clients need publicly
resolvable HTTP URLs.

Changes:
- Add HMAC-SHA256 presigned URL utility (internal/utils/presign.go) for
  generating time-limited, signature-verified file access URLs
- Add GET /api/v1/files/presigned endpoint that serves files without
  session auth, verified by HMAC signature and expiry
- Update localFileService.GetFileURL() to return presigned HTTP URLs
  when APP_EXTERNAL_URL is configured (cloud backends already return
  presigned URLs via their SDKs)
- Add IM content rewriting pipeline: strip <image> XML tags, strip
  citation tags, rewrite storage URLs to HTTP — applied at all IM
  output points (streaming flush, non-streaming reply, fallback)
- Add holdback buffer in streaming flush to prevent URL/tag splitting
  across chunk boundaries

Closes #1058
2026-04-30 11:40:30 +08:00
c 8c62a066af fix(frontend): route chat drag-and-drop uploads correctly 2026-04-30 10:43:03 +08:00
wizardchen 5ff11dce38 fix(frontend): hide card checkbox when not selecting
Card grid showed a faded checkbox at all times after multi-select was
added, which felt visually noisy and misaligned the title column. Hide
the checkbox column by default and let it expand on hover / when any
card is selected / when this card itself is selected. Drop
:focus-within so a cleared selection doesn't get stuck visible due to
the retained input focus.
2026-04-29 22:35:23 +08:00
wizardchen 679e40de82 refactor(frontend): unify Model/WebSearch/MCP settings pages with shared card + drawer
Aligns the three list-style settings pages (ModelSettings, WebSearchSettings,
McpSettings) under a consistent visual language, and replaces the modal editors
with right-side drawers for a calmer editing flow.

- Extract shared building blocks under frontend/src/components/settings/:
  SettingCard (list-item card), SettingDrawer (500px right drawer with a
  pinned footer), and useConfirmDelete (a single DialogPlugin.confirm path
  that replaces the mix of window.confirm / t-popconfirm / ad-hoc dialogs).
- Swap the old bespoke overlay / t-dialog shells in ModelEditorDialog and
  McpServiceDialog for SettingDrawer. Form logic is untouched.
- Model settings now use a single t-tabs filter (All/Chat/Embedding/ReRank/
  Vision/Speech with per-type counts) and decouple "Add model" from the tab
  state via a dropdown in the header. Type tags get a 5-color palette so
  categories read at a glance. The list is a two-column grid.
- Web search and MCP lists are likewise rendered as two-column grids, with
  richer cards: transport type / provider, on-off and default state, plus a
  compact meta row showing base URL, proxy or service URL.
- Introduce modelSettings.typeShort.{chat,embedding,rerank,vllm,asr} in all
  four locales for the shortened category labels.
2026-04-29 22:34:39 +08:00
Mao Meng b48adde3e6 fix:attachment_document_failed 2026-04-29 22:34:13 +08:00
wizardchen b2975f7d1d refactor(im): move adapter factories into per-platform subpackages
registerIMAdapterFactories in internal/container/container.go had grown
to ~290 lines of platform-specific config parsing and runtime start/stop
logic across 7 IM platforms, plus three private credential helpers. The
container package is meant to wire dependencies, not own each platform's
credentials schema and websocket lifecycle.

Extract each platform's factory into its own subpackage file and expose
the credential helpers on the im package:

- New im.ParseCredentials / im.GetString / im.GetBool (formerly the
  unexported parseCredentials / getString / credentialBool in container).
- New im.ResolveMode helper collapses the six identical "default to
  websocket when Mode is empty" blocks.
- New {wecom,feishu,slack,telegram,dingtalk,mattermost,wechat}.NewFactory
  returns an im.AdapterFactory; container.go now calls each one in a
  single line.

Behavior is preserved: adapter construction, goroutine start/stop,
log messages, default modes (mattermost still defaults to "webhook";
others to "websocket"), and error strings are unchanged. The WeCom
corp_agent_id float64/int switch and the WeChat long-poll-only path
are preserved as-is.

Net: container.go drops ~320 lines and no longer needs to be touched
when adding or tuning an IM platform.
2026-04-29 20:08:22 +08:00
wizardchen 9af5fb30ac fix(frontend): polish knowledge list sticky header and floating batch bar
- Detect sticky state via IntersectionObserver and flatten the table
  header's top corners when stuck so the rounded cut-outs no longer
  reveal the list container's background while scrolling.
- Lift the batch-action toolbar out of flex flow and float it 12px above
  the scroll container bottom so selecting rows no longer shrinks the
  visible list; strengthen its shadow to match the floating treatment.
2026-04-29 20:07:55 +08:00
wizardchen 7fcd92994b feat(tenant): expose API Key reset from the API Info page
Add a danger-themed reset button next to the existing copy/eye buttons
on the API Info settings page. Confirmation dialog warns that the old
key is revoked immediately; on success the new plaintext key replaces
the displayed value and is auto-revealed so the user can copy it.

Backend: new POST /api/v1/tenants/:id/api-key handler that wraps the
existing TenantService.UpdateAPIKey; access is gated by the same
authorizeTenantAccess check as other tenant endpoints. The handler
returns the freshly generated plaintext key, while the encrypted form
is persisted to the database.

Frontend: new resetTenantApiKey API client, reset button + confirm
dialog wiring in ApiInfo.vue, plus matching i18n entries for zh-CN,
en-US, ko-KR, and ru-RU.
2026-04-29 20:07:38 +08:00