101 lines
3.8 KiB
TypeScript
101 lines
3.8 KiB
TypeScript
// The handlers of the knowledge module's procedures: each resolves its ports from the request's
|
|
// container and calls one use case. No rule lives here.
|
|
|
|
import type { Container } from "@conusai-kit/core"
|
|
import { implement } from "@conusai-kit/core"
|
|
import { DATA_SOURCE } from "@conusai-kit/data"
|
|
import { filesDeps } from "@conusai-kit/files"
|
|
import {
|
|
collectionCreate,
|
|
collectionList,
|
|
documentIndex,
|
|
documentList,
|
|
documentRemove,
|
|
knowledgeSearch,
|
|
knowledgeStatus,
|
|
settingsDisableDocumentAi,
|
|
settingsEnableDocumentAi,
|
|
settingsGet,
|
|
} from "../contract/index.js"
|
|
import { createCollection, listCollections } from "../core/collections.js"
|
|
import { listDocuments, requestIndex, requestRemove } from "../core/documents.js"
|
|
import { flagsOf, KNOWLEDGE_ENABLEMENT, KNOWLEDGE_SEARCH, type KnowledgeDeps } from "../core/ports.js"
|
|
import { searchKnowledge } from "../core/search.js"
|
|
import { disableDocumentAi, enableDocumentAi, getSettings, type SettingsDeps } from "../core/settings.js"
|
|
import { knowledgeStatusOf } from "../core/status.js"
|
|
|
|
/** The ports a write or read handler needs. Never the search service: that port is an effect, and
|
|
* resolving it from a write is a kind violation. */
|
|
export function knowledgeDeps(container: Container): KnowledgeDeps {
|
|
return {
|
|
data: container.resolve(DATA_SOURCE),
|
|
files: filesDeps(container),
|
|
flags: flagsOf(container),
|
|
}
|
|
}
|
|
|
|
/** The effect handler's deps: the search service, bound in the processes that may leave the process. */
|
|
export function knowledgeSearchDeps(container: Container): KnowledgeDeps {
|
|
return { ...knowledgeDeps(container), search: container.resolve(KNOWLEDGE_SEARCH) }
|
|
}
|
|
|
|
function settingsDeps(container: Container): SettingsDeps {
|
|
return { data: container.resolve(DATA_SOURCE), platform: container.resolve(KNOWLEDGE_ENABLEMENT) }
|
|
}
|
|
|
|
export const collectionListImpl = implement(collectionList, ({ ctx }) =>
|
|
listCollections(knowledgeDeps(ctx.container), ctx.scope),
|
|
)
|
|
|
|
export const collectionCreateImpl = implement(collectionCreate, ({ input, ctx }) =>
|
|
createCollection(knowledgeDeps(ctx.container), ctx.scope, input.name),
|
|
)
|
|
|
|
export const documentIndexImpl = implement(documentIndex, ({ input, ctx }) =>
|
|
requestIndex(knowledgeDeps(ctx.container), ctx.scope, input),
|
|
)
|
|
|
|
export const documentListImpl = implement(documentList, ({ input, ctx }) =>
|
|
listDocuments(knowledgeDeps(ctx.container), ctx.scope, input.collectionName),
|
|
)
|
|
|
|
export const documentRemoveImpl = implement(documentRemove, ({ input, ctx }) =>
|
|
requestRemove(knowledgeDeps(ctx.container), ctx.scope, input),
|
|
)
|
|
|
|
export const knowledgeSearchImpl = implement(knowledgeSearch, ({ input, ctx }) =>
|
|
searchKnowledge(knowledgeSearchDeps(ctx.container), ctx.scope, {
|
|
collectionName: input.collectionName,
|
|
query: input.query,
|
|
...(input.limit === undefined ? {} : { limit: input.limit }),
|
|
}),
|
|
)
|
|
|
|
export const knowledgeStatusImpl = implement(knowledgeStatus, ({ ctx }) =>
|
|
knowledgeStatusOf(knowledgeSearchDeps(ctx.container), ctx.scope),
|
|
)
|
|
|
|
export const settingsGetImpl = implement(settingsGet, ({ ctx }) => getSettings(settingsDeps(ctx.container), ctx.scope))
|
|
|
|
export const settingsEnableDocumentAiImpl = implement(settingsEnableDocumentAi, async ({ ctx }) => {
|
|
const settings = await enableDocumentAi(settingsDeps(ctx.container), ctx.scope)
|
|
return { enabled: settings.enabled, backend: settings.backend ?? "vectors" }
|
|
})
|
|
|
|
export const settingsDisableDocumentAiImpl = implement(settingsDisableDocumentAi, ({ ctx }) =>
|
|
disableDocumentAi(settingsDeps(ctx.container), ctx.scope),
|
|
)
|
|
|
|
export const KNOWLEDGE_PROCEDURE_IMPLS = [
|
|
collectionListImpl,
|
|
collectionCreateImpl,
|
|
documentIndexImpl,
|
|
documentListImpl,
|
|
documentRemoveImpl,
|
|
knowledgeSearchImpl,
|
|
knowledgeStatusImpl,
|
|
settingsGetImpl,
|
|
settingsEnableDocumentAiImpl,
|
|
settingsDisableDocumentAiImpl,
|
|
]
|