feat(client): split WithToken, add Raw + auth helpers (PR-2)

Five additive SDK changes that PR-3 (CLI Factory adapter) and PR-4
(auth login / status) consume.

WithToken split (Breaking + alias):
- WithAPIKey(key) sets X-API-Key header (long-lived API keys)
- WithBearerToken(token) sets Authorization: Bearer header (JWTs)
- WithToken kept as deprecated alias for WithAPIKey for two minor versions
- doRequest now sets X-API-Key and Authorization headers independently;
  callers can carry both kinds of credentials simultaneously

applyAuthHeaders extraction:
- Header injection (X-API-Key, Authorization, X-Request-ID, X-Tenant-ID)
  is now a Client method shared between doRequest and the multipart path
  in CreateKnowledgeFromFile. Eliminates ~30 lines of drift risk and
  fixes the latent missing X-Tenant-ID on file uploads.
- Drops the dead second tenant-header switch in doRequest.

Raw escape hatch:
- Client.Raw(ctx, method, path, body) is the public passthrough used by
  the future \`weknora api\` command. godoc marks it experimental.

Auth methods (auth.go, new):
- Login(ctx, LoginRequest) -> POST /auth/login (existing server endpoint)
- GetCurrentUser(ctx) -> GET /auth/me, returns *CurrentUserResponse
- RefreshToken(ctx, refreshToken) -> POST /auth/refresh
- Types: LoginRequest, LoginResponse, AuthUser, AuthTenant,
  CurrentUserResponse, RefreshTokenResponse

No server changes. The auth methods consume endpoints that already exist;
PR-3/PR-4 wire them through the CLI.
This commit is contained in:
nullkey
2026-05-06 14:22:53 +08:00
committed by lyingbug
parent a39805aa89
commit 37d4fbde5d
4 changed files with 225 additions and 51 deletions
+34 -3
View File
@@ -138,7 +138,37 @@ type KnowledgeBaseListResponse struct {
Data []KnowledgeBase `json:"data"`
}
// SearchResult represents search result
// MatchType records which retrieval channel produced a SearchResult.
// Numeric values are the wire contract; they mirror the iota order in
// server-side internal/types/embedding.go — do not reorder without
// coordinating a server bump. Server-side names are preserved as
// trailing comments so cross-repo grep works in both directions.
//
// Channel grouping: 0-1 primary text channels (vector + keyword);
// 2-5 enrichment chunks (added in addition to primary matches, score=0);
// 6-9 alternate sources (graph DB, web search, raw load, data analysis).
type MatchType int
const (
MatchTypeVector MatchType = 0 // server: MatchTypeEmbedding
MatchTypeKeyword MatchType = 1 // server: MatchTypeKeywords
MatchTypeNearby MatchType = 2 // server: MatchTypeNearByChunk
MatchTypeHistory MatchType = 3 // server: MatchTypeHistory
MatchTypeParent MatchType = 4 // server: MatchTypeParentChunk
MatchTypeRelation MatchType = 5 // server: MatchTypeRelationChunk
MatchTypeGraph MatchType = 6 // server: MatchTypeGraph
MatchTypeWeb MatchType = 7 // server: MatchTypeWebSearch
MatchTypeDirect MatchType = 8 // server: MatchTypeDirectLoad — chunk loaded by ID without scoring
MatchTypeData MatchType = 9 // server: MatchTypeDataAnalysis — produced by analytical pipeline, not retrieval
)
// SearchResult represents search result.
//
// Score is the RRF (reciprocal-rank-fusion) score combining vector and
// keyword channels — typically in the [0, ~0.03] range when both channels
// hit, NOT the raw vector similarity. Use MatchType to tell which channel
// produced each result. Per-channel thresholds (vector_threshold,
// keyword_threshold) filter pre-fusion at retrieval time, before RRF runs.
type SearchResult struct {
ID string `json:"id"`
Content string `json:"content"`
@@ -149,12 +179,13 @@ type SearchResult struct {
EndAt int `json:"end_at"`
Seq int `json:"seq"`
Score float64 `json:"score"`
MatchType MatchType `json:"match_type"`
ChunkType string `json:"chunk_type"`
ImageInfo string `json:"image_info"`
Metadata map[string]string `json:"metadata"`
KnowledgeFilename string `json:"knowledge_filename"`
KnowledgeSource string `json:"knowledge_source"`
KnowledgeChannel string `json:"knowledge_channel"`
KnowledgeSource string `json:"knowledge_source"`
KnowledgeChannel string `json:"knowledge_channel"`
// MatchedContent is the actual content that was matched in vector search
// For FAQ: this is the matched question text (standard or similar question)
MatchedContent string `json:"matched_content,omitempty"`