luzia-core 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +16 -2
- package/dist/chat-model.d.ts +46 -0
- package/dist/chat-model.js +56 -0
- package/dist/chat-model.js.map +1 -0
- package/dist/embedding-model.d.ts +44 -0
- package/dist/embedding-model.js +58 -0
- package/dist/embedding-model.js.map +1 -0
- package/dist/env.d.ts +4 -0
- package/dist/env.js +5 -0
- package/dist/env.js.map +1 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +9 -0
- package/dist/errors.js.map +1 -0
- package/dist/handler.d.ts +45 -0
- package/dist/handler.js +79 -0
- package/dist/handler.js.map +1 -0
- package/dist/http.d.ts +30 -0
- package/dist/http.js +5 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/knowledge-base/build.d.ts +43 -0
- package/dist/knowledge-base/build.js +102 -0
- package/dist/knowledge-base/build.js.map +1 -0
- package/dist/knowledge-base/compiled.d.ts +34 -0
- package/dist/knowledge-base/compiled.js +7 -0
- package/dist/knowledge-base/compiled.js.map +1 -0
- package/dist/knowledge-base/load.d.ts +6 -0
- package/dist/knowledge-base/load.js +21 -0
- package/dist/knowledge-base/load.js.map +1 -0
- package/dist/knowledge-base/markdown.d.ts +9 -0
- package/dist/knowledge-base/markdown.js +44 -0
- package/dist/knowledge-base/markdown.js.map +1 -0
- package/dist/knowledge-base/sources.d.ts +36 -0
- package/dist/knowledge-base/sources.js +46 -0
- package/dist/knowledge-base/sources.js.map +1 -0
- package/dist/knowledge-base/split.d.ts +9 -0
- package/dist/knowledge-base/split.js +44 -0
- package/dist/knowledge-base/split.js.map +1 -0
- package/dist/knowledge-base/write.d.ts +3 -0
- package/dist/knowledge-base/write.js +8 -0
- package/dist/knowledge-base/write.js.map +1 -0
- package/dist/prompt/system-prompt.d.ts +23 -0
- package/dist/prompt/system-prompt.js +127 -0
- package/dist/prompt/system-prompt.js.map +1 -0
- package/dist/protection/cors.d.ts +7 -0
- package/dist/protection/cors.js +26 -0
- package/dist/protection/cors.js.map +1 -0
- package/dist/protection/index.d.ts +36 -0
- package/dist/protection/index.js +38 -0
- package/dist/protection/index.js.map +1 -0
- package/dist/protection/limits.d.ts +50 -0
- package/dist/protection/limits.js +94 -0
- package/dist/protection/limits.js.map +1 -0
- package/dist/protection/origins.d.ts +28 -0
- package/dist/protection/origins.js +57 -0
- package/dist/protection/origins.js.map +1 -0
- package/dist/providers.d.ts +41 -0
- package/dist/providers.js +31 -0
- package/dist/providers.js.map +1 -0
- package/dist/reply-stream.d.ts +10 -0
- package/dist/reply-stream.js +58 -0
- package/dist/reply-stream.js.map +1 -0
- package/dist/retrieval/retriever.d.ts +29 -0
- package/dist/retrieval/retriever.js +47 -0
- package/dist/retrieval/retriever.js.map +1 -0
- package/dist/retrieval/vector-store.d.ts +16 -0
- package/dist/retrieval/vector-store.js +28 -0
- package/dist/retrieval/vector-store.js.map +1 -0
- package/package.json +52 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sergio Deri
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# luzia-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The luzIA server handler and Knowledge Base compiler.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { createLuziaHandler, loadCompiledKnowledgeBase } from 'luzia-core';
|
|
7
|
+
import compiledKnowledgeBase from './.luzia/knowledge-base.json';
|
|
8
|
+
|
|
9
|
+
const handler = createLuziaHandler({ knowledgeBase: loadCompiledKnowledgeBase(compiledKnowledgeBase) });
|
|
10
|
+
|
|
11
|
+
export const POST = handler; // Web Request in, streamed Response out
|
|
12
|
+
export const OPTIONS = handler; // CORS preflight
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The handler is framework-agnostic: mount it as a Next.js route handler or deploy it as a standalone Vercel Function. Chat and embeddings models, allowed origins and limits come from handler options or `LUZIA_*` environment variables. The compiled Knowledge Base is produced by `luzia build` from [`luzia-cli`](../cli), or by `buildKnowledgeBase` from this package.
|
|
16
|
+
|
|
17
|
+
See the [root README](../../README.md) for setup, configuration and every option.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { LanguageModel } from 'ai';
|
|
2
|
+
import type { Env } from './env.ts';
|
|
3
|
+
import { type ProviderModelConfig } from './providers.ts';
|
|
4
|
+
/** A ready-made AI SDK language model (any provider). */
|
|
5
|
+
export type ChatModelInstance = Exclude<LanguageModel, string>;
|
|
6
|
+
/**
|
|
7
|
+
* Built-in chat providers: display name, where the API key is read from, how a
|
|
8
|
+
* model is made, and a few suggested model ids (any other id works too).
|
|
9
|
+
*/
|
|
10
|
+
export declare const CHAT_PROVIDERS: {
|
|
11
|
+
readonly anthropic: {
|
|
12
|
+
readonly label: 'Anthropic (Claude)';
|
|
13
|
+
readonly apiKeyEnv: 'ANTHROPIC_API_KEY';
|
|
14
|
+
readonly suggestedModels: readonly ['claude-sonnet-5-5', 'claude-haiku-5-5', 'claude-opus-5-5'];
|
|
15
|
+
readonly create: (apiKey: string, model: string) => import("@ai-sdk/provider").LanguageModelV4;
|
|
16
|
+
};
|
|
17
|
+
readonly openai: {
|
|
18
|
+
readonly label: 'OpenAI';
|
|
19
|
+
readonly apiKeyEnv: 'OPENAI_API_KEY';
|
|
20
|
+
readonly suggestedModels: readonly ['gpt-5.5', 'gpt-5.4-mini'];
|
|
21
|
+
readonly create: (apiKey: string, model: string) => import("@ai-sdk/provider").LanguageModelV4;
|
|
22
|
+
};
|
|
23
|
+
readonly google: {
|
|
24
|
+
readonly label: 'Google (Gemini)';
|
|
25
|
+
readonly apiKeyEnv: 'GOOGLE_GENERATIVE_AI_API_KEY';
|
|
26
|
+
readonly suggestedModels: readonly ['gemini-3.5-flash', 'gemini-3.5-flash-lite'];
|
|
27
|
+
readonly create: (apiKey: string, model: string) => import("@ai-sdk/provider").LanguageModelV4;
|
|
28
|
+
};
|
|
29
|
+
readonly groq: {
|
|
30
|
+
readonly label: 'Groq';
|
|
31
|
+
readonly apiKeyEnv: 'GROQ_API_KEY';
|
|
32
|
+
readonly suggestedModels: readonly ['openai/gpt-oss-120b', 'llama-3.3-70b-versatile'];
|
|
33
|
+
readonly create: (apiKey: string, model: string) => import("@ai-sdk/provider").LanguageModelV4;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
export type ChatProvider = keyof typeof CHAT_PROVIDERS;
|
|
37
|
+
/**
|
|
38
|
+
* How the Agent's chat model is chosen. Either a built-in provider by name
|
|
39
|
+
* (API key read server-side from the provider's environment variable unless
|
|
40
|
+
* given), or any AI SDK language model instance.
|
|
41
|
+
*/
|
|
42
|
+
export type ChatModelConfig = ProviderModelConfig<ChatProvider, ChatModelInstance>;
|
|
43
|
+
/** Environment variables used when `chat` is omitted from the handler config. */
|
|
44
|
+
export declare const CHAT_PROVIDER_ENV = "LUZIA_CHAT_PROVIDER";
|
|
45
|
+
export declare const CHAT_MODEL_ENV = "LUZIA_CHAT_MODEL";
|
|
46
|
+
export declare function resolveChatModel(config: ChatModelConfig | undefined, env: Env): ChatModelInstance;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createAnthropic } from '@ai-sdk/anthropic';
|
|
2
|
+
import { createGoogle } from '@ai-sdk/google';
|
|
3
|
+
import { createGroq } from '@ai-sdk/groq';
|
|
4
|
+
import { createOpenAI } from '@ai-sdk/openai';
|
|
5
|
+
import { LuziaConfigError } from './errors.js';
|
|
6
|
+
import { providerModelFromEnv, resolveProviderModel } from './providers.js';
|
|
7
|
+
/**
|
|
8
|
+
* Built-in chat providers: display name, where the API key is read from, how a
|
|
9
|
+
* model is made, and a few suggested model ids (any other id works too).
|
|
10
|
+
*/
|
|
11
|
+
export const CHAT_PROVIDERS = {
|
|
12
|
+
anthropic: {
|
|
13
|
+
label: 'Anthropic (Claude)',
|
|
14
|
+
apiKeyEnv: 'ANTHROPIC_API_KEY',
|
|
15
|
+
suggestedModels: ['claude-sonnet-5-5', 'claude-haiku-5-5', 'claude-opus-5-5'],
|
|
16
|
+
create: (apiKey, model) => createAnthropic({ apiKey })(model),
|
|
17
|
+
},
|
|
18
|
+
openai: {
|
|
19
|
+
label: 'OpenAI',
|
|
20
|
+
apiKeyEnv: 'OPENAI_API_KEY',
|
|
21
|
+
suggestedModels: ['gpt-5.5', 'gpt-5.4-mini'],
|
|
22
|
+
create: (apiKey, model) => createOpenAI({ apiKey })(model),
|
|
23
|
+
},
|
|
24
|
+
google: {
|
|
25
|
+
label: 'Google (Gemini)',
|
|
26
|
+
apiKeyEnv: 'GOOGLE_GENERATIVE_AI_API_KEY',
|
|
27
|
+
suggestedModels: ['gemini-3.5-flash', 'gemini-3.5-flash-lite'],
|
|
28
|
+
create: (apiKey, model) => createGoogle({ apiKey })(model),
|
|
29
|
+
},
|
|
30
|
+
groq: {
|
|
31
|
+
label: 'Groq',
|
|
32
|
+
apiKeyEnv: 'GROQ_API_KEY',
|
|
33
|
+
suggestedModels: ['openai/gpt-oss-120b', 'llama-3.3-70b-versatile'],
|
|
34
|
+
create: (apiKey, model) => createGroq({ apiKey })(model),
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
/** Environment variables used when `chat` is omitted from the handler config. */
|
|
38
|
+
export const CHAT_PROVIDER_ENV = 'LUZIA_CHAT_PROVIDER';
|
|
39
|
+
export const CHAT_MODEL_ENV = 'LUZIA_CHAT_MODEL';
|
|
40
|
+
export function resolveChatModel(config, env) {
|
|
41
|
+
return resolveProviderModel({
|
|
42
|
+
configKey: 'chat',
|
|
43
|
+
label: 'chat',
|
|
44
|
+
instanceDescription: 'an AI SDK language model',
|
|
45
|
+
providers: CHAT_PROVIDERS,
|
|
46
|
+
}, config ?? chatModelConfigFromEnv(env), env);
|
|
47
|
+
}
|
|
48
|
+
function chatModelConfigFromEnv(env) {
|
|
49
|
+
const fromEnv = providerModelFromEnv(env, CHAT_PROVIDER_ENV, CHAT_MODEL_ENV);
|
|
50
|
+
if (!fromEnv) {
|
|
51
|
+
throw new LuziaConfigError(`No chat model configured. Pass \`chat\` to createLuziaHandler, or set the ${CHAT_PROVIDER_ENV} ` +
|
|
52
|
+
`(${Object.keys(CHAT_PROVIDERS).join(' | ')}) and ${CHAT_MODEL_ENV} environment variables.`);
|
|
53
|
+
}
|
|
54
|
+
return fromEnv;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=chat-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{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S,EAAE,cAAc;QACzB,eAAe,EAAE,CAAC,qBAAqB,EAAE,yBAAyB,CAAC;QACnE,MAAM,EAAE,CAAC,MAAc,EAAE,KAAa,EAAE,EAAE,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC;KACzE;CACO,CAAC;AAWX,iFAAiF;AACjF,MAAM,CAAC,MAAM,iBAAiB,GAAG,qBAAqB,CAAC;AACvD,MAAM,CAAC,MAAM,cAAc,GAAG,kBAAkB,CAAC;AAEjD,MAAM,UAAU,gBAAgB,CAAC,MAAmC,EAAE,GAAQ;IAC5E,OAAO,oBAAoB,CACzB;QACE,SAAS,EAAE,MAAM;QACjB,KAAK,EAAE,MAAM;QACb,mBAAmB,EAAE,0BAA0B;QAC/C,SAAS,EAAE,cAAc;KAC1B,EACD,MAAM,IAAI,sBAAsB,CAAC,GAAG,CAAC,EACrC,GAAG,CACJ,CAAC;AACJ,CAAC;AAED,SAAS,sBAAsB,CAAC,GAAQ;IACtC,MAAM,OAAO,GAAG,oBAAoB,CAAC,GAAG,EAAE,iBAAiB,EAAE,cAAc,CAAC,CAAC;IAC7E,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,gBAAgB,CACxB,6EAA6E,iBAAiB,GAAG;YAC/F,IAAI,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,cAAc,yBAAyB,CAC9F,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { EmbeddingModel } from 'ai';
|
|
2
|
+
import type { Env } from './env.ts';
|
|
3
|
+
import { type ProviderModelConfig } from './providers.ts';
|
|
4
|
+
/** A ready-made AI SDK embedding model (any provider). */
|
|
5
|
+
export type EmbeddingModelInstance = Exclude<EmbeddingModel, string>;
|
|
6
|
+
/**
|
|
7
|
+
* Built-in embeddings providers. Anthropic has no embeddings API, so an Owner
|
|
8
|
+
* chatting with Claude picks one of these (with its own key) for retrieval.
|
|
9
|
+
* Each has a display name and a few suggested model ids (any other id works too).
|
|
10
|
+
*/
|
|
11
|
+
export declare const EMBEDDING_PROVIDERS: {
|
|
12
|
+
readonly openai: {
|
|
13
|
+
readonly label: 'OpenAI';
|
|
14
|
+
readonly apiKeyEnv: 'OPENAI_API_KEY';
|
|
15
|
+
readonly suggestedModels: readonly ['text-embedding-3-small', 'text-embedding-3-large'];
|
|
16
|
+
readonly create: (apiKey: string, model: string) => import("@ai-sdk/provider").EmbeddingModelV4;
|
|
17
|
+
};
|
|
18
|
+
readonly google: {
|
|
19
|
+
readonly label: 'Google (Gemini)';
|
|
20
|
+
readonly apiKeyEnv: 'GOOGLE_GENERATIVE_AI_API_KEY';
|
|
21
|
+
readonly suggestedModels: readonly ['gemini-embedding-001', 'gemini-embedding-2'];
|
|
22
|
+
readonly create: (apiKey: string, model: string) => import("@ai-sdk/provider").EmbeddingModelV4;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
export type EmbeddingProvider = keyof typeof EMBEDDING_PROVIDERS;
|
|
26
|
+
/**
|
|
27
|
+
* How the embeddings model is chosen, independently of the chat model. Either
|
|
28
|
+
* a built-in provider by name (API key read server-side from the provider's
|
|
29
|
+
* environment variable unless given), or any AI SDK embedding model instance.
|
|
30
|
+
*/
|
|
31
|
+
export type EmbeddingModelConfig = ProviderModelConfig<EmbeddingProvider, EmbeddingModelInstance>;
|
|
32
|
+
/** Environment variables used when `embedding` is not configured explicitly. */
|
|
33
|
+
export declare const EMBEDDING_PROVIDER_ENV = "LUZIA_EMBEDDING_PROVIDER";
|
|
34
|
+
export declare const EMBEDDING_MODEL_ENV = "LUZIA_EMBEDDING_MODEL";
|
|
35
|
+
/** A resolved embeddings model and the id recorded in the compiled Knowledge Base. */
|
|
36
|
+
export interface ResolvedEmbeddingModel {
|
|
37
|
+
model: EmbeddingModelInstance;
|
|
38
|
+
id: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Resolves the embeddings model from explicit config or, failing that, from
|
|
42
|
+
* the environment. Returns `null` when neither configures one.
|
|
43
|
+
*/
|
|
44
|
+
export declare function resolveEmbeddingModel(config: EmbeddingModelConfig | undefined, env: Env): ResolvedEmbeddingModel | null;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createGoogle } from '@ai-sdk/google';
|
|
2
|
+
import { createOpenAI } from '@ai-sdk/openai';
|
|
3
|
+
import { LuziaConfigError } from './errors.js';
|
|
4
|
+
import { providerModelFromEnv, resolveProviderModel } from './providers.js';
|
|
5
|
+
/**
|
|
6
|
+
* Built-in embeddings providers. Anthropic has no embeddings API, so an Owner
|
|
7
|
+
* chatting with Claude picks one of these (with its own key) for retrieval.
|
|
8
|
+
* Each has a display name and a few suggested model ids (any other id works too).
|
|
9
|
+
*/
|
|
10
|
+
export const EMBEDDING_PROVIDERS = {
|
|
11
|
+
openai: {
|
|
12
|
+
label: 'OpenAI',
|
|
13
|
+
apiKeyEnv: 'OPENAI_API_KEY',
|
|
14
|
+
suggestedModels: ['text-embedding-3-small', 'text-embedding-3-large'],
|
|
15
|
+
create: (apiKey, model) => createOpenAI({ apiKey }).embeddingModel(model),
|
|
16
|
+
},
|
|
17
|
+
google: {
|
|
18
|
+
label: 'Google (Gemini)',
|
|
19
|
+
apiKeyEnv: 'GOOGLE_GENERATIVE_AI_API_KEY',
|
|
20
|
+
suggestedModels: ['gemini-embedding-001', 'gemini-embedding-2'],
|
|
21
|
+
create: (apiKey, model) => createGoogle({ apiKey }).embeddingModel(model),
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
/** Environment variables used when `embedding` is not configured explicitly. */
|
|
25
|
+
export const EMBEDDING_PROVIDER_ENV = 'LUZIA_EMBEDDING_PROVIDER';
|
|
26
|
+
export const EMBEDDING_MODEL_ENV = 'LUZIA_EMBEDDING_MODEL';
|
|
27
|
+
/**
|
|
28
|
+
* Resolves the embeddings model from explicit config or, failing that, from
|
|
29
|
+
* the environment. Returns `null` when neither configures one.
|
|
30
|
+
*/
|
|
31
|
+
export function resolveEmbeddingModel(config, env) {
|
|
32
|
+
const embedding = config ?? embeddingModelConfigFromEnv(env);
|
|
33
|
+
if (!embedding)
|
|
34
|
+
return null;
|
|
35
|
+
const model = resolveProviderModel({
|
|
36
|
+
configKey: 'embedding',
|
|
37
|
+
label: 'embeddings',
|
|
38
|
+
instanceDescription: 'an AI SDK embedding model',
|
|
39
|
+
providers: EMBEDDING_PROVIDERS,
|
|
40
|
+
unknownProviderHint: '(Anthropic has no embeddings API)',
|
|
41
|
+
}, embedding, env);
|
|
42
|
+
return { model, id: embeddingModelId(model) };
|
|
43
|
+
}
|
|
44
|
+
/** Identifies an embeddings model as `<provider>:<model id>`, e.g. `openai.embedding:text-embedding-3-small`. */
|
|
45
|
+
function embeddingModelId(model) {
|
|
46
|
+
return `${model.provider}:${model.modelId}`;
|
|
47
|
+
}
|
|
48
|
+
function embeddingModelConfigFromEnv(env) {
|
|
49
|
+
if (!env[EMBEDDING_PROVIDER_ENV] && !env[EMBEDDING_MODEL_ENV])
|
|
50
|
+
return null;
|
|
51
|
+
const fromEnv = providerModelFromEnv(env, EMBEDDING_PROVIDER_ENV, EMBEDDING_MODEL_ENV);
|
|
52
|
+
if (!fromEnv) {
|
|
53
|
+
throw new LuziaConfigError(`Set both ${EMBEDDING_PROVIDER_ENV} (${Object.keys(EMBEDDING_PROVIDERS).join(' | ')}) and ${EMBEDDING_MODEL_ENV} ` +
|
|
54
|
+
'to configure the embeddings model.');
|
|
55
|
+
}
|
|
56
|
+
return fromEnv;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=embedding-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedding-model.js","sourceRoot":"","sources":["../src/embedding-model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,YAA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CACJ,CAAC;IACF,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;AAChD,CAAC;AAED,iHAAiH;AACjH,SAAS,gBAAgB,CAAC,KAA6B;IACrD,OAAO,GAAG,KAAK,CAAC,QAAQ,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;AAC9C,CAAC;AAED,SAAS,2BAA2B,CAAC,GAAQ;IAC3C,IAAI,CAAC,GAAG,CAAC,sBAAsB,CAAC,IAAI,CAAC,GAAG,CAAC,mBAAmB,CAAC;QAAE,OAAO,IAAI,CAAC;IAC3E,MAAM,OAAO,GAAG,oBAAoB,CAAC,GAAG,EAAE,sBAAsB,EAAE,mBAAmB,CAAC,CAAC;IACvF,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,gBAAgB,CACxB,YAAY,sBAAsB,KAAK,MAAM,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,mBAAmB,GAAG;YAChH,oCAAoC,CACvC,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Server-side settings and API keys, as environment variables. */
|
|
2
|
+
export type Env = Record<string, string | undefined>;
|
|
3
|
+
/** The environment to read settings from: `env` when given, else `process.env` where there is one. */
|
|
4
|
+
export declare function serverEnv(env?: Env): Env;
|
package/dist/env.js
ADDED
package/dist/env.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAGA,sGAAsG;AACtG,MAAM,UAAU,SAAS,CAAC,GAAS;IACjC,OAAO,GAAG,IAAI,UAAU,CAAC,OAAO,EAAE,GAAG,IAAI,EAAE,CAAC;AAC9C,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Raised at build time when the Knowledge Base folder cannot be compiled. */
|
|
2
|
+
export declare class KnowledgeBaseError extends Error {
|
|
3
|
+
name: string;
|
|
4
|
+
}
|
|
5
|
+
/** Raised when the handler is created with missing or invalid configuration. */
|
|
6
|
+
export declare class LuziaConfigError extends Error {
|
|
7
|
+
name: string;
|
|
8
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Raised at build time when the Knowledge Base folder cannot be compiled. */
|
|
2
|
+
export class KnowledgeBaseError extends Error {
|
|
3
|
+
name = 'KnowledgeBaseError';
|
|
4
|
+
}
|
|
5
|
+
/** Raised when the handler is created with missing or invalid configuration. */
|
|
6
|
+
export class LuziaConfigError extends Error {
|
|
7
|
+
name = 'LuziaConfigError';
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAClC,IAAI,GAAG,oBAAoB,CAAC;CACtC;AAED,gFAAgF;AAChF,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAChC,IAAI,GAAG,kBAAkB,CAAC;CACpC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { type ChatModelConfig } from './chat-model.ts';
|
|
3
|
+
import type { EmbeddingModelConfig } from './embedding-model.ts';
|
|
4
|
+
import { type Env } from './env.ts';
|
|
5
|
+
import type { CompiledKnowledgeBase } from './knowledge-base/compiled.ts';
|
|
6
|
+
import { type ProtectionConfig } from './protection/index.ts';
|
|
7
|
+
import { type RetrievalOptions } from './retrieval/retriever.ts';
|
|
8
|
+
export interface LuziaHandlerConfig extends ProtectionConfig {
|
|
9
|
+
/** The compiled Knowledge Base produced by `luzia build` (e.g. its imported JSON). */
|
|
10
|
+
knowledgeBase: CompiledKnowledgeBase;
|
|
11
|
+
/**
|
|
12
|
+
* The chat model. When omitted it is read from the LUZIA_CHAT_PROVIDER and
|
|
13
|
+
* LUZIA_CHAT_MODEL environment variables.
|
|
14
|
+
*/
|
|
15
|
+
chat?: ChatModelConfig;
|
|
16
|
+
/**
|
|
17
|
+
* The embeddings model used to search the Knowledge Base. Must match the one
|
|
18
|
+
* `luzia build` used. When omitted it is read from the LUZIA_EMBEDDING_PROVIDER
|
|
19
|
+
* and LUZIA_EMBEDDING_MODEL environment variables. Not needed for a Core-only
|
|
20
|
+
* Knowledge Base.
|
|
21
|
+
*/
|
|
22
|
+
embedding?: EmbeddingModelConfig;
|
|
23
|
+
/** Retrieval settings (top-k, vector store). */
|
|
24
|
+
retrieval?: RetrievalOptions;
|
|
25
|
+
/** Where server-side settings and API keys are read from. Defaults to `process.env`. */
|
|
26
|
+
env?: Env;
|
|
27
|
+
}
|
|
28
|
+
/** A framework-agnostic handler: Web `Request` in, Web `Response` out. */
|
|
29
|
+
export type LuziaHandler = (request: Request) => Promise<Response>;
|
|
30
|
+
/** Shape of the JSON body the chat endpoint accepts. */
|
|
31
|
+
export declare const chatRequestBodySchema: z.ZodObject<{
|
|
32
|
+
messages: z.ZodArray<z.ZodObject<{
|
|
33
|
+
role: z.ZodEnum<{
|
|
34
|
+
assistant: "assistant";
|
|
35
|
+
user: "user";
|
|
36
|
+
}>;
|
|
37
|
+
content: z.ZodString;
|
|
38
|
+
}, z.core.$strip>>;
|
|
39
|
+
}, z.core.$strip>;
|
|
40
|
+
export type ChatRequestBody = z.infer<typeof chatRequestBodySchema>;
|
|
41
|
+
/**
|
|
42
|
+
* Creates the luzIA server handler. Validates configuration eagerly, so a
|
|
43
|
+
* missing API key or invalid Knowledge Base fails at startup, not on first chat.
|
|
44
|
+
*/
|
|
45
|
+
export declare function createLuziaHandler(config: LuziaHandlerConfig): LuziaHandler;
|
package/dist/handler.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { streamText } from 'ai';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { resolveChatModel } from './chat-model.js';
|
|
4
|
+
import { serverEnv } from './env.js';
|
|
5
|
+
import { errorResponse } from './http.js';
|
|
6
|
+
import { loadCompiledKnowledgeBase } from './knowledge-base/load.js';
|
|
7
|
+
import { assembleSystemPrompt } from './prompt/system-prompt.js';
|
|
8
|
+
import { replyResponse } from './reply-stream.js';
|
|
9
|
+
import { createProtection } from './protection/index.js';
|
|
10
|
+
import { createRetriever } from './retrieval/retriever.js';
|
|
11
|
+
/** Shape of the JSON body the chat endpoint accepts. */
|
|
12
|
+
export const chatRequestBodySchema = z.object({
|
|
13
|
+
messages: z
|
|
14
|
+
.array(z.object({
|
|
15
|
+
role: z.enum(['user', 'assistant']),
|
|
16
|
+
content: z.string(),
|
|
17
|
+
}))
|
|
18
|
+
.min(1)
|
|
19
|
+
.refine((messages) => messages.at(-1)?.role === 'user', {
|
|
20
|
+
message: 'The last message must be from the Visitor.',
|
|
21
|
+
}),
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* Creates the luzIA server handler. Validates configuration eagerly, so a
|
|
25
|
+
* missing API key or invalid Knowledge Base fails at startup, not on first chat.
|
|
26
|
+
*/
|
|
27
|
+
export function createLuziaHandler(config) {
|
|
28
|
+
const env = serverEnv(config.env);
|
|
29
|
+
const knowledgeBase = loadCompiledKnowledgeBase(config.knowledgeBase);
|
|
30
|
+
const model = resolveChatModel(config.chat, env);
|
|
31
|
+
// Origins, CORS and limits: see ./protection.
|
|
32
|
+
const protection = createProtection(config, env);
|
|
33
|
+
const retrieve = createRetriever({
|
|
34
|
+
knowledgeBase,
|
|
35
|
+
embedding: config.embedding,
|
|
36
|
+
env,
|
|
37
|
+
...(config.retrieval ? { retrieval: config.retrieval } : {}),
|
|
38
|
+
});
|
|
39
|
+
return protection.wrap(async function luziaHandler(request) {
|
|
40
|
+
if (request.method !== 'POST') {
|
|
41
|
+
return errorResponse(405, 'method_not_allowed', 'Use POST to chat with luzIA.', { allow: 'POST' });
|
|
42
|
+
}
|
|
43
|
+
const text = await protection.readBody(request);
|
|
44
|
+
if (text instanceof Response)
|
|
45
|
+
return text;
|
|
46
|
+
const body = chatRequestBodySchema.safeParse(parseJson(text));
|
|
47
|
+
if (!body.success) {
|
|
48
|
+
return errorResponse(400, 'invalid_request', 'Expected a JSON body like { "messages": [{ "role": "user", "content": "..." }] }.');
|
|
49
|
+
}
|
|
50
|
+
const overLimit = protection.checkLimits(body.data.messages);
|
|
51
|
+
if (overLimit)
|
|
52
|
+
return overLimit;
|
|
53
|
+
const messages = body.data.messages;
|
|
54
|
+
let retrievedChunks;
|
|
55
|
+
try {
|
|
56
|
+
retrievedChunks = await retrieve(body.data.messages);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return errorResponse(502, 'provider_error', 'The embeddings model failed to search the Knowledge Base. Please try again later.');
|
|
60
|
+
}
|
|
61
|
+
const result = streamText({
|
|
62
|
+
model,
|
|
63
|
+
// AI SDK "instructions" is the system prompt; the Owner's Instructions are one part of it.
|
|
64
|
+
instructions: assembleSystemPrompt({ knowledgeBase, retrievedChunks }),
|
|
65
|
+
messages,
|
|
66
|
+
maxOutputTokens: protection.limits.maxOutputTokens,
|
|
67
|
+
});
|
|
68
|
+
return replyResponse(result.fullStream);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function parseJson(text) {
|
|
72
|
+
try {
|
|
73
|
+
return JSON.parse(text);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAqB,MAAM,IAAI,CAAC;AACnD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK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a,CAAC,GAAG,EAAE,gBAAgB,EAAE,mFAAmF,CAAC,CAAC;QACnI,CAAC;QAED,MAAM,MAAM,GAAG,UAAU,CAAC;YACxB,KAAK;YACL,2FAA2F;YAC3F,YAAY,EAAE,oBAAoB,CAAC,EAAE,aAAa,EAAE,eAAe,EAAE,CAAC;YACtE,QAAQ;YACR,eAAe,EAAE,UAAU,CAAC,MAAM,CAAC,eAAe;SACnD,CAAC,CAAC;QAEH,OAAO,aAAa,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,SAAS,CAAC,IAAY;IAC7B,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC"}
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** Machine-readable error codes the handler returns in JSON error bodies. */
|
|
2
|
+
export type LuziaErrorCode = 'method_not_allowed' | 'invalid_request'
|
|
3
|
+
/** The request's `Origin` is not in the allowed origins (403). */
|
|
4
|
+
| 'origin_not_allowed'
|
|
5
|
+
/**
|
|
6
|
+
* The conversation has more Visitor messages than `limits.maxMessages`, or
|
|
7
|
+
* more than `2 × maxMessages − 1` messages in all (429).
|
|
8
|
+
*/
|
|
9
|
+
| 'conversation_limit_reached'
|
|
10
|
+
/**
|
|
11
|
+
* A Visitor message is longer than `limits.maxMessageLength`, or an Agent
|
|
12
|
+
* reply in the conversation is longer than `limits.maxReplyLength` (413).
|
|
13
|
+
*/
|
|
14
|
+
| 'message_too_long'
|
|
15
|
+
/** The request body is larger than `limits.maxRequestBytes` (413); it is not read further. */
|
|
16
|
+
| 'request_too_large'
|
|
17
|
+
/**
|
|
18
|
+
* The chat model failed before replying, or the embeddings model failed
|
|
19
|
+
* while searching the Knowledge Base (502). A failure mid-reply breaks the
|
|
20
|
+
* stream instead.
|
|
21
|
+
*/
|
|
22
|
+
| 'provider_error';
|
|
23
|
+
/** Body of every non-streamed error response: `{ error: { code, message } }`. */
|
|
24
|
+
export interface LuziaErrorBody {
|
|
25
|
+
error: {
|
|
26
|
+
code: LuziaErrorCode;
|
|
27
|
+
message: string;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export declare function errorResponse(status: number, code: LuziaErrorCode, message: string, headers?: HeadersInit): Response;
|
package/dist/http.js
ADDED
package/dist/http.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AA8BA,MAAM,UAAU,aAAa,CAC3B,MAAc,EACd,IAAoB,EACpB,OAAe,EACf,OAAqB;IAErB,MAAM,IAAI,GAAmB,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC;IAC1D,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;AAClD,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export { buildKnowledgeBase, buildKnowledgeBaseWithReport, CORE_FILE_NAME, INSTRUCTIONS_FILE_NAME, } from './knowledge-base/build.ts';
|
|
2
|
+
export type { BuildKnowledgeBaseOptions, KnowledgeBaseBuildReport, KnowledgeBaseBuildResult, } from './knowledge-base/build.ts';
|
|
3
|
+
export { pdfReader } from './knowledge-base/sources.ts';
|
|
4
|
+
export type { SourceReader, SourceReadResult, SourceSection } from './knowledge-base/sources.ts';
|
|
5
|
+
export { writeCompiledKnowledgeBase } from './knowledge-base/write.ts';
|
|
6
|
+
export { loadCompiledKnowledgeBase } from './knowledge-base/load.ts';
|
|
7
|
+
export { COMPILED_KNOWLEDGE_BASE_FORMAT } from './knowledge-base/compiled.ts';
|
|
8
|
+
export type { CompiledKnowledgeBase, KnowledgeBaseChunk, KnowledgeBaseDocument, } from './knowledge-base/compiled.ts';
|
|
9
|
+
export { createLuziaHandler, chatRequestBodySchema } from './handler.ts';
|
|
10
|
+
export type { ChatRequestBody, LuziaHandler, LuziaHandlerConfig } from './handler.ts';
|
|
11
|
+
export { CHAT_MODEL_ENV, CHAT_PROVIDER_ENV, CHAT_PROVIDERS, resolveChatModel } from './chat-model.ts';
|
|
12
|
+
export type { ChatModelConfig, ChatModelInstance, ChatProvider } from './chat-model.ts';
|
|
13
|
+
export type { Env } from './env.ts';
|
|
14
|
+
export { EMBEDDING_MODEL_ENV, EMBEDDING_PROVIDER_ENV, EMBEDDING_PROVIDERS } from './embedding-model.ts';
|
|
15
|
+
export type { EmbeddingModelConfig, EmbeddingModelInstance, EmbeddingProvider } from './embedding-model.ts';
|
|
16
|
+
export { createInMemoryVectorStore } from './retrieval/vector-store.ts';
|
|
17
|
+
export type { ScoredChunk, VectorStore } from './retrieval/vector-store.ts';
|
|
18
|
+
export { DEFAULT_TOP_K } from './retrieval/retriever.ts';
|
|
19
|
+
export type { RetrievalOptions } from './retrieval/retriever.ts';
|
|
20
|
+
export type { LuziaErrorBody, LuziaErrorCode } from './http.ts';
|
|
21
|
+
export { ALLOWED_ORIGINS_ENV, DEFAULT_LIMITS, LIMIT_ENV, REPLY_CHARS_PER_TOKEN } from './protection/index.ts';
|
|
22
|
+
export type { LuziaLimits, ProtectionConfig } from './protection/index.ts';
|
|
23
|
+
export { KnowledgeBaseError, LuziaConfigError } from './errors.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { buildKnowledgeBase, buildKnowledgeBaseWithReport, CORE_FILE_NAME, INSTRUCTIONS_FILE_NAME, } from './knowledge-base/build.js';
|
|
2
|
+
export { pdfReader } from './knowledge-base/sources.js';
|
|
3
|
+
export { writeCompiledKnowledgeBase } from './knowledge-base/write.js';
|
|
4
|
+
export { loadCompiledKnowledgeBase } from './knowledge-base/load.js';
|
|
5
|
+
export { COMPILED_KNOWLEDGE_BASE_FORMAT } from './knowledge-base/compiled.js';
|
|
6
|
+
export { createLuziaHandler, chatRequestBodySchema } from './handler.js';
|
|
7
|
+
export { CHAT_MODEL_ENV, CHAT_PROVIDER_ENV, CHAT_PROVIDERS, resolveChatModel } from './chat-model.js';
|
|
8
|
+
export { EMBEDDING_MODEL_ENV, EMBEDDING_PROVIDER_ENV, EMBEDDING_PROVIDERS } from './embedding-model.js';
|
|
9
|
+
export { createInMemoryVectorStore } from './retrieval/vector-store.js';
|
|
10
|
+
export { DEFAULT_TOP_K } from './retrieval/retriever.js';
|
|
11
|
+
export { ALLOWED_ORIGINS_ENV, DEFAULT_LIMITS, LIMIT_ENV, REPLY_CHARS_PER_TOKEN } from './protection/index.js';
|
|
12
|
+
export { KnowledgeBaseError, LuziaConfigError } from './errors.js';
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,4BAA4B,EAC5B,cAAc,EACd,sBAAsB,GACvB,MAAM,2BAA2B,CAAC;AAMnC,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAExD,OAAO,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AACvE,OAAO,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,EAAE,8BAA8B,EAAE,MAAM,8BAA8B,CAAC;AAM9E,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAEzE,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAGtG,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAExG,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAExE,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAGzD,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,SAAS,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAE9G,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type Env } from '../env.ts';
|
|
2
|
+
import { type EmbeddingModelConfig } from '../embedding-model.ts';
|
|
3
|
+
import { type CompiledKnowledgeBase } from './compiled.ts';
|
|
4
|
+
/** File names (case-insensitive) that identify the Core and the Instructions. */
|
|
5
|
+
export declare const CORE_FILE_NAME = "core.md";
|
|
6
|
+
export declare const INSTRUCTIONS_FILE_NAME = "instructions.md";
|
|
7
|
+
export interface BuildKnowledgeBaseOptions {
|
|
8
|
+
/** The Knowledge Base folder. */
|
|
9
|
+
dir: string;
|
|
10
|
+
/**
|
|
11
|
+
* The embeddings model chunks are embedded with. When omitted it is read from
|
|
12
|
+
* the LUZIA_EMBEDDING_PROVIDER and LUZIA_EMBEDDING_MODEL environment
|
|
13
|
+
* variables. Only required when the folder has files besides Core and Instructions.
|
|
14
|
+
*/
|
|
15
|
+
embedding?: EmbeddingModelConfig;
|
|
16
|
+
/** Where embeddings settings and API keys are read from. Defaults to `process.env`. */
|
|
17
|
+
env?: Env;
|
|
18
|
+
/** Sections longer than this many characters are split into several chunks. */
|
|
19
|
+
maxChunkChars?: number;
|
|
20
|
+
}
|
|
21
|
+
/** A summary of what a build compiled, for the Owner. */
|
|
22
|
+
export interface KnowledgeBaseBuildReport {
|
|
23
|
+
/** Every file compiled (Core and Instructions included), relative to the Knowledge Base folder. */
|
|
24
|
+
files: string[];
|
|
25
|
+
/** Retrievable chunks produced. */
|
|
26
|
+
chunks: number;
|
|
27
|
+
/** Pages whose text was extracted from PDF files. */
|
|
28
|
+
pdfPages: number;
|
|
29
|
+
/** Problems the Owner should fix, e.g. a PDF with no extractable text. They do not fail the build. */
|
|
30
|
+
warnings: string[];
|
|
31
|
+
}
|
|
32
|
+
export interface KnowledgeBaseBuildResult {
|
|
33
|
+
knowledgeBase: CompiledKnowledgeBase;
|
|
34
|
+
report: KnowledgeBaseBuildReport;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Compiles a Knowledge Base folder into a {@link CompiledKnowledgeBase}.
|
|
38
|
+
* Throws a {@link KnowledgeBaseError} when the folder or its Core file is missing.
|
|
39
|
+
* Use {@link buildKnowledgeBaseWithReport} to also get a summary of the build.
|
|
40
|
+
*/
|
|
41
|
+
export declare function buildKnowledgeBase(options: BuildKnowledgeBaseOptions): Promise<CompiledKnowledgeBase>;
|
|
42
|
+
/** Like {@link buildKnowledgeBase}, also reporting files processed, chunks produced, PDF pages and warnings. */
|
|
43
|
+
export declare function buildKnowledgeBaseWithReport(options: BuildKnowledgeBaseOptions): Promise<KnowledgeBaseBuildResult>;
|