feat: add external knowledge retrieval and cross-project activity
Connect Dify, FastGPT and RAGFlow through read-only bindings, encrypted local credentials, shared retrieval and persistent citations. Configuration and removal affect GoodBuddy only. Show active conversations across projects, retain older live work, and improve project dialog scrolling. Queue concurrent questions and route managed SSH OpenCode questions through the Agent. Preserve recoverable read/search errors and partial search results. Document the Electron UI automation standard, real model and external knowledge validation, and remaining acceptance limits. Validation: earlier full regression passed 4081 tests with 66 skipped; subsequent focused remote-question checks and real Linux evidence are recorded in feature progress. Precommit typecheck and lint passed; latest project dialog and shared UI suites passed 58 tests. The full suite was not rerun for the final dialog changes. Release note: Use external knowledge in conversations, find active work across projects, and answer concurrent local or managed SSH OpenCode questions. Managed SSH questions require the updated Agent adapter.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { randomBytes } from 'node:crypto'
|
||||
import { knowledgeReferenceKey, toKnowledgeReference } from '../../shared/knowledge-reference'
|
||||
import {
|
||||
createServer,
|
||||
type IncomingMessage,
|
||||
@@ -24,7 +25,6 @@ import type {
|
||||
BrowserTabSummary,
|
||||
KnowledgeSearchReference
|
||||
} from '../../shared/contracts'
|
||||
import { stripKnowledgeHighlightTags } from '../../shared/knowledge-text'
|
||||
import {
|
||||
knowledgeToolNames,
|
||||
knowledgeScopedDataToolCatalog,
|
||||
@@ -273,13 +273,7 @@ function textContent(value: string): MagicNoteRichContent {
|
||||
}
|
||||
|
||||
function referenceKey(reference: KnowledgeSearchReference): string {
|
||||
return [
|
||||
reference.libraryId,
|
||||
reference.documentId,
|
||||
reference.chunkId ?? '',
|
||||
reference.locator ?? '',
|
||||
reference.snippet
|
||||
].join('\0')
|
||||
return knowledgeReferenceKey(reference)
|
||||
}
|
||||
|
||||
function ensureBoundedCustomMcpResult(result: unknown): CallToolResult {
|
||||
@@ -681,20 +675,22 @@ export class KnowledgeMcpGateway {
|
||||
const { query, limit } = knowledgeSearchTool.inputSchema.parse(
|
||||
input
|
||||
)
|
||||
const effectiveSignal = signal
|
||||
? AbortSignal.any([signal, capability.signal])
|
||||
: capability.signal
|
||||
const effectiveSignal = AbortSignal.any([capability.signal, capability.brokerController.signal, ...(signal ? [signal] : [])])
|
||||
effectiveSignal.throwIfAborted()
|
||||
const libraries = this.knowledgeService.database.listKnowledgeBases(500)
|
||||
const libraryNames = new Map(
|
||||
libraries.map((library) => [library.id, library.name])
|
||||
)
|
||||
const results = await this.knowledgeService.searchHybridMany(
|
||||
const outcomes = await this.knowledgeService.retrieveMany(
|
||||
capability.libraryIds,
|
||||
query,
|
||||
limit,
|
||||
effectiveSignal
|
||||
)
|
||||
effectiveSignal.throwIfAborted()
|
||||
const warnings = outcomes.flatMap(item => item.response.diagnostics.failure ? [item.response.diagnostics.failure] : [])
|
||||
if (outcomes.length && warnings.length === outcomes.length) throw new Error(warnings.join('; '))
|
||||
const results = outcomes.flatMap(item => item.response.results.map(result => ({ knowledgeBaseId: item.knowledgeBaseId, result })))
|
||||
if (!results.length && warnings.length) throw new Error(warnings.join('; '))
|
||||
const references: KnowledgeSearchReference[] = []
|
||||
const seen = new Set<string>()
|
||||
for (const { knowledgeBaseId, result } of results.sort(
|
||||
@@ -704,22 +700,8 @@ export class KnowledgeMcpGateway {
|
||||
break
|
||||
}
|
||||
const reference: KnowledgeSearchReference = {
|
||||
libraryId: knowledgeBaseId,
|
||||
libraryName: libraryNames.get(knowledgeBaseId) ?? '知识库',
|
||||
documentId: result.document.id,
|
||||
chunkId: result.chunk.id,
|
||||
documentName: result.document.title.slice(0, 500),
|
||||
sourceName: result.source.displayName.slice(0, 500),
|
||||
locator: result.chunk.location?.slice(0, 1_000),
|
||||
snippet: stripKnowledgeHighlightTags(result.snippet).slice(0, 12_000),
|
||||
rank: result.rank,
|
||||
score: result.retrieval.score,
|
||||
lexicalRank: result.retrieval.lexicalRank,
|
||||
vectorRank: result.retrieval.vectorRank,
|
||||
graphRank: result.retrieval.graphRank,
|
||||
similarity: result.retrieval.similarity,
|
||||
retrievalChannels: result.retrieval.channels,
|
||||
evidenceIds: result.retrieval.evidenceIds?.slice(0, 100)
|
||||
...toKnowledgeReference(result, libraryNames.get(knowledgeBaseId) ?? '知识库'),
|
||||
...(warnings.length ? { warnings } : {})
|
||||
}
|
||||
const key = referenceKey(reference)
|
||||
if (seen.has(key)) {
|
||||
@@ -1473,13 +1455,14 @@ export class KnowledgeMcpGateway {
|
||||
private async callScopedTool(
|
||||
token: string,
|
||||
name: ScopedDataToolName,
|
||||
input: unknown
|
||||
input: unknown,
|
||||
signal?: AbortSignal
|
||||
): Promise<Record<string, unknown>> {
|
||||
switch (name) {
|
||||
case 'knowledge_list':
|
||||
return { libraries: this.listLibraries(token, input) }
|
||||
case 'knowledge_search':
|
||||
return { references: await this.search(token, input) }
|
||||
return { references: await this.search(token, input, signal) }
|
||||
case 'note_list':
|
||||
return { notes: this.listMagicNotes(token, input) }
|
||||
case 'note_get':
|
||||
@@ -1641,7 +1624,8 @@ export class KnowledgeMcpGateway {
|
||||
await this.callScopedTool(
|
||||
token,
|
||||
name as ScopedDataToolName,
|
||||
parsedInput
|
||||
parsedInput,
|
||||
extra.signal
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user