feat(resource): add stable resource registry with LLM-context alias compaction

Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.

Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
This commit is contained in:
wizardchen
2026-07-15 16:31:21 +08:00
committed by lyingbug
parent 9733bd28bf
commit 6d14cc3536
32 changed files with 1790 additions and 59 deletions
+16
View File
@@ -9,6 +9,7 @@ import (
agenttools "github.com/Tencent/WeKnora/internal/agent/tools"
"github.com/Tencent/WeKnora/internal/common"
"github.com/Tencent/WeKnora/internal/event"
"github.com/Tencent/WeKnora/internal/llmresource"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/types"
@@ -37,6 +38,7 @@ func (e *AgentEngine) streamLLMToEventBus(
llmCtx, llmCancel := context.WithTimeout(ctx, e.getLLMCallTimeout())
defer llmCancel()
messages = e.resourceRefs.EncodeMessages(messages)
stream, err := e.chatModel.ChatStream(llmCtx, messages, opts)
if err != nil {
logger.Errorf(ctx, "[Agent][Stream] Failed to start LLM stream: %v", err)
@@ -47,6 +49,8 @@ func (e *AgentEngine) streamLLMToEventBus(
chunkCount := 0
responseTypeCounts := make(map[string]int)
firstChunkTime := time.Time{}
answerDecoder := llmresource.NewStreamDecoder(e.resourceRefs)
thinkingDecoder := llmresource.NewStreamDecoder(e.resourceRefs)
for chunk := range stream {
chunkCount++
@@ -62,6 +66,12 @@ func (e *AgentEngine) streamLLMToEventBus(
result.StreamError = chunk.Content
continue
}
if chunk.ResponseType == types.ResponseTypeThinking {
chunk.Content = thinkingDecoder.Feed(chunk.Content)
} else {
chunk.Content = answerDecoder.Feed(chunk.Content)
}
e.resourceRefs.DecodeToolCalls(chunk.ToolCalls)
if chunk.Content != "" {
isExtracted := chunk.Data != nil && chunk.Data["source"] != nil
@@ -90,6 +100,12 @@ func (e *AgentEngine) streamLLMToEventBus(
emitFunc(&chunk, result.Content)
}
}
result.Content += answerDecoder.Flush()
result.ReasoningContent += thinkingDecoder.Flush()
// Some providers stream tool-call argument fragments but expose the
// accumulated call in the final chunk. Decode once more after assembly so
// aliases split across provider chunks cannot leak into tool execution.
e.resourceRefs.DecodeToolCalls(result.ToolCalls)
// Stream diagnostic summary: helps identify non-streaming patterns
streamDuration := time.Duration(0)