@intlayer/backend 7.2.1-canary.1 → 7.2.3
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/dist/esm/utils/AI/aiSdk.mjs +7 -1
- package/dist/esm/utils/AI/aiSdk.mjs.map +1 -1
- package/dist/types/controllers/dictionary.controller.d.ts.map +1 -1
- package/dist/types/controllers/tag.controller.d.ts.map +1 -1
- package/dist/types/emails/InviteUserEmail.d.ts +4 -4
- package/dist/types/emails/InviteUserEmail.d.ts.map +1 -1
- package/dist/types/emails/MagicLinkEmail.d.ts +4 -4
- package/dist/types/emails/MagicLinkEmail.d.ts.map +1 -1
- package/dist/types/emails/OAuthTokenCreatedEmail.d.ts +4 -4
- package/dist/types/emails/PasswordChangeConfirmation.d.ts +4 -4
- package/dist/types/emails/PasswordChangeConfirmation.d.ts.map +1 -1
- package/dist/types/emails/ResetUserPassword.d.ts +4 -4
- package/dist/types/emails/ResetUserPassword.d.ts.map +1 -1
- package/dist/types/emails/SubscriptionPaymentCancellation.d.ts +4 -4
- package/dist/types/emails/SubscriptionPaymentCancellation.d.ts.map +1 -1
- package/dist/types/emails/SubscriptionPaymentError.d.ts +4 -4
- package/dist/types/emails/SubscriptionPaymentSuccess.d.ts +4 -4
- package/dist/types/emails/ValidateUserEmail.d.ts +4 -4
- package/dist/types/emails/Welcome.d.ts +4 -4
- package/dist/types/emails/Welcome.d.ts.map +1 -1
- package/dist/types/models/dictionary.model.d.ts +4 -4
- package/dist/types/models/discussion.model.d.ts +2 -2
- package/dist/types/models/discussion.model.d.ts.map +1 -1
- package/dist/types/models/oAuth2.model.d.ts +3 -3
- package/dist/types/routes/eventListener.routes.d.ts.map +1 -1
- package/dist/types/schemas/dictionary.schema.d.ts +6 -6
- package/dist/types/schemas/dictionary.schema.d.ts.map +1 -1
- package/dist/types/schemas/discussion.schema.d.ts +6 -6
- package/dist/types/schemas/oAuth2.schema.d.ts +5 -5
- package/dist/types/schemas/oAuth2.schema.d.ts.map +1 -1
- package/dist/types/schemas/organization.schema.d.ts +6 -6
- package/dist/types/schemas/plans.schema.d.ts +6 -6
- package/dist/types/schemas/project.schema.d.ts +6 -6
- package/dist/types/schemas/project.schema.d.ts.map +1 -1
- package/dist/types/schemas/tag.schema.d.ts +6 -6
- package/dist/types/schemas/user.schema.d.ts +6 -6
- package/dist/types/utils/filtersAndPagination/getDiscussionFiltersAndPagination.d.ts +2 -2
- package/dist/types/utils/filtersAndPagination/getOrganizationFiltersAndPagination.d.ts +2 -2
- package/dist/types/utils/filtersAndPagination/getProjectFiltersAndPagination.d.ts +2 -2
- package/dist/types/utils/filtersAndPagination/getTagFiltersAndPagination.d.ts +2 -2
- package/package.json +17 -18
- package/dist/cjs/_virtual/_utils_asset.cjs +0 -98
- package/dist/cjs/_virtual/rolldown_runtime.cjs +0 -25
- package/dist/cjs/controllers/ai.controller.cjs +0 -381
- package/dist/cjs/controllers/ai.controller.cjs.map +0 -1
- package/dist/cjs/controllers/dictionary.controller.cjs +0 -463
- package/dist/cjs/controllers/dictionary.controller.cjs.map +0 -1
- package/dist/cjs/controllers/eventListener.controller.cjs +0 -53
- package/dist/cjs/controllers/eventListener.controller.cjs.map +0 -1
- package/dist/cjs/controllers/newsletter.controller.cjs +0 -164
- package/dist/cjs/controllers/newsletter.controller.cjs.map +0 -1
- package/dist/cjs/controllers/oAuth2.controller.cjs +0 -22
- package/dist/cjs/controllers/oAuth2.controller.cjs.map +0 -1
- package/dist/cjs/controllers/organization.controller.cjs +0 -492
- package/dist/cjs/controllers/organization.controller.cjs.map +0 -1
- package/dist/cjs/controllers/project.controller.cjs +0 -430
- package/dist/cjs/controllers/project.controller.cjs.map +0 -1
- package/dist/cjs/controllers/projectAccessKey.controller.cjs +0 -161
- package/dist/cjs/controllers/projectAccessKey.controller.cjs.map +0 -1
- package/dist/cjs/controllers/search.controller.cjs +0 -14
- package/dist/cjs/controllers/search.controller.cjs.map +0 -1
- package/dist/cjs/controllers/stripe.controller.cjs +0 -149
- package/dist/cjs/controllers/stripe.controller.cjs.map +0 -1
- package/dist/cjs/controllers/tag.controller.cjs +0 -226
- package/dist/cjs/controllers/tag.controller.cjs.map +0 -1
- package/dist/cjs/controllers/user.controller.cjs +0 -269
- package/dist/cjs/controllers/user.controller.cjs.map +0 -1
- package/dist/cjs/emails/InviteUserEmail.cjs +0 -325
- package/dist/cjs/emails/InviteUserEmail.cjs.map +0 -1
- package/dist/cjs/emails/MagicLinkEmail.cjs +0 -242
- package/dist/cjs/emails/MagicLinkEmail.cjs.map +0 -1
- package/dist/cjs/emails/OAuthTokenCreatedEmail.cjs +0 -301
- package/dist/cjs/emails/OAuthTokenCreatedEmail.cjs.map +0 -1
- package/dist/cjs/emails/PasswordChangeConfirmation.cjs +0 -177
- package/dist/cjs/emails/PasswordChangeConfirmation.cjs.map +0 -1
- package/dist/cjs/emails/ResetUserPassword.cjs +0 -240
- package/dist/cjs/emails/ResetUserPassword.cjs.map +0 -1
- package/dist/cjs/emails/SubscriptionPaymentCancellation.cjs +0 -202
- package/dist/cjs/emails/SubscriptionPaymentCancellation.cjs.map +0 -1
- package/dist/cjs/emails/SubscriptionPaymentError.cjs +0 -202
- package/dist/cjs/emails/SubscriptionPaymentError.cjs.map +0 -1
- package/dist/cjs/emails/SubscriptionPaymentSuccess.cjs +0 -208
- package/dist/cjs/emails/SubscriptionPaymentSuccess.cjs.map +0 -1
- package/dist/cjs/emails/ValidateUserEmail.cjs +0 -240
- package/dist/cjs/emails/ValidateUserEmail.cjs.map +0 -1
- package/dist/cjs/emails/Welcome.cjs +0 -216
- package/dist/cjs/emails/Welcome.cjs.map +0 -1
- package/dist/cjs/emails/index.cjs +0 -25
- package/dist/cjs/export.cjs +0 -24
- package/dist/cjs/index.cjs +0 -91
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/logger/index.cjs +0 -13
- package/dist/cjs/logger/index.cjs.map +0 -1
- package/dist/cjs/middlewares/oAuth2.middleware.cjs +0 -42
- package/dist/cjs/middlewares/oAuth2.middleware.cjs.map +0 -1
- package/dist/cjs/middlewares/request.middleware.cjs +0 -17
- package/dist/cjs/middlewares/request.middleware.cjs.map +0 -1
- package/dist/cjs/middlewares/sessionAuth.middleware.cjs +0 -22
- package/dist/cjs/middlewares/sessionAuth.middleware.cjs.map +0 -1
- package/dist/cjs/models/dictionary.model.cjs +0 -10
- package/dist/cjs/models/dictionary.model.cjs.map +0 -1
- package/dist/cjs/models/discussion.model.cjs +0 -10
- package/dist/cjs/models/discussion.model.cjs.map +0 -1
- package/dist/cjs/models/oAuth2.model.cjs +0 -10
- package/dist/cjs/models/oAuth2.model.cjs.map +0 -1
- package/dist/cjs/models/organization.model.cjs +0 -10
- package/dist/cjs/models/organization.model.cjs.map +0 -1
- package/dist/cjs/models/project.model.cjs +0 -10
- package/dist/cjs/models/project.model.cjs.map +0 -1
- package/dist/cjs/models/session.model.cjs +0 -10
- package/dist/cjs/models/session.model.cjs.map +0 -1
- package/dist/cjs/models/tag.model.cjs +0 -10
- package/dist/cjs/models/tag.model.cjs.map +0 -1
- package/dist/cjs/models/user.model.cjs +0 -10
- package/dist/cjs/models/user.model.cjs.map +0 -1
- package/dist/cjs/routes/ai.routes.cjs +0 -75
- package/dist/cjs/routes/ai.routes.cjs.map +0 -1
- package/dist/cjs/routes/dictionary.routes.cjs +0 -64
- package/dist/cjs/routes/dictionary.routes.cjs.map +0 -1
- package/dist/cjs/routes/eventListener.routes.cjs +0 -20
- package/dist/cjs/routes/eventListener.routes.cjs.map +0 -1
- package/dist/cjs/routes/newsletter.routes.cjs +0 -34
- package/dist/cjs/routes/newsletter.routes.cjs.map +0 -1
- package/dist/cjs/routes/organization.routes.cjs +0 -70
- package/dist/cjs/routes/organization.routes.cjs.map +0 -1
- package/dist/cjs/routes/project.routes.cjs +0 -83
- package/dist/cjs/routes/project.routes.cjs.map +0 -1
- package/dist/cjs/routes/search.routes.cjs +0 -20
- package/dist/cjs/routes/search.routes.cjs.map +0 -1
- package/dist/cjs/routes/stripe.routes.cjs +0 -34
- package/dist/cjs/routes/stripe.routes.cjs.map +0 -1
- package/dist/cjs/routes/tags.routes.cjs +0 -40
- package/dist/cjs/routes/tags.routes.cjs.map +0 -1
- package/dist/cjs/routes/user.routes.cjs +0 -58
- package/dist/cjs/routes/user.routes.cjs.map +0 -1
- package/dist/cjs/schemas/dictionary.schema.cjs +0 -73
- package/dist/cjs/schemas/dictionary.schema.cjs.map +0 -1
- package/dist/cjs/schemas/discussion.schema.cjs +0 -81
- package/dist/cjs/schemas/discussion.schema.cjs.map +0 -1
- package/dist/cjs/schemas/oAuth2.schema.cjs +0 -49
- package/dist/cjs/schemas/oAuth2.schema.cjs.map +0 -1
- package/dist/cjs/schemas/organization.schema.cjs +0 -62
- package/dist/cjs/schemas/organization.schema.cjs.map +0 -1
- package/dist/cjs/schemas/plans.schema.cjs +0 -75
- package/dist/cjs/schemas/plans.schema.cjs.map +0 -1
- package/dist/cjs/schemas/project.schema.cjs +0 -112
- package/dist/cjs/schemas/project.schema.cjs.map +0 -1
- package/dist/cjs/schemas/session.schema.cjs +0 -43
- package/dist/cjs/schemas/session.schema.cjs.map +0 -1
- package/dist/cjs/schemas/tag.schema.cjs +0 -62
- package/dist/cjs/schemas/tag.schema.cjs.map +0 -1
- package/dist/cjs/schemas/user.schema.cjs +0 -87
- package/dist/cjs/schemas/user.schema.cjs.map +0 -1
- package/dist/cjs/services/dictionary.service.cjs +0 -176
- package/dist/cjs/services/dictionary.service.cjs.map +0 -1
- package/dist/cjs/services/email.service.cjs +0 -155
- package/dist/cjs/services/email.service.cjs.map +0 -1
- package/dist/cjs/services/oAuth2.service.cjs +0 -211
- package/dist/cjs/services/oAuth2.service.cjs.map +0 -1
- package/dist/cjs/services/organization.service.cjs +0 -106
- package/dist/cjs/services/organization.service.cjs.map +0 -1
- package/dist/cjs/services/project.service.cjs +0 -89
- package/dist/cjs/services/project.service.cjs.map +0 -1
- package/dist/cjs/services/projectAccessKey.service.cjs +0 -111
- package/dist/cjs/services/projectAccessKey.service.cjs.map +0 -1
- package/dist/cjs/services/subscription.service.cjs +0 -184
- package/dist/cjs/services/subscription.service.cjs.map +0 -1
- package/dist/cjs/services/tag.service.cjs +0 -88
- package/dist/cjs/services/tag.service.cjs.map +0 -1
- package/dist/cjs/services/user.service.cjs +0 -120
- package/dist/cjs/services/user.service.cjs.map +0 -1
- package/dist/cjs/types/Routes.cjs +0 -0
- package/dist/cjs/types/dictionary.types.cjs +0 -0
- package/dist/cjs/types/discussion.types.cjs +0 -0
- package/dist/cjs/types/oAuth2.types.cjs +0 -0
- package/dist/cjs/types/organization.types.cjs +0 -0
- package/dist/cjs/types/plan.types.cjs +0 -0
- package/dist/cjs/types/project.types.cjs +0 -0
- package/dist/cjs/types/session.types.cjs +0 -0
- package/dist/cjs/types/tag.types.cjs +0 -0
- package/dist/cjs/types/user.types.cjs +0 -10
- package/dist/cjs/types/user.types.cjs.map +0 -1
- package/dist/cjs/utils/AI/aiSdk.cjs +0 -94
- package/dist/cjs/utils/AI/aiSdk.cjs.map +0 -1
- package/dist/cjs/utils/AI/askDocQuestion/askDocQuestion.cjs +0 -201
- package/dist/cjs/utils/AI/askDocQuestion/askDocQuestion.cjs.map +0 -1
- package/dist/cjs/utils/AI/askDocQuestion/indexMarkdownFiles.cjs +0 -150
- package/dist/cjs/utils/AI/askDocQuestion/indexMarkdownFiles.cjs.map +0 -1
- package/dist/cjs/utils/AI/auditDictionary/index.cjs +0 -60
- package/dist/cjs/utils/AI/auditDictionary/index.cjs.map +0 -1
- package/dist/cjs/utils/AI/auditDictionaryField/index.cjs +0 -63
- package/dist/cjs/utils/AI/auditDictionaryField/index.cjs.map +0 -1
- package/dist/cjs/utils/AI/auditDictionaryMetadata/index.cjs +0 -45
- package/dist/cjs/utils/AI/auditDictionaryMetadata/index.cjs.map +0 -1
- package/dist/cjs/utils/AI/auditTag/index.cjs +0 -40
- package/dist/cjs/utils/AI/auditTag/index.cjs.map +0 -1
- package/dist/cjs/utils/AI/autocomplete/index.cjs +0 -41
- package/dist/cjs/utils/AI/autocomplete/index.cjs.map +0 -1
- package/dist/cjs/utils/AI/customQuery/index.cjs +0 -27
- package/dist/cjs/utils/AI/customQuery/index.cjs.map +0 -1
- package/dist/cjs/utils/AI/translateJSON/index.cjs +0 -71
- package/dist/cjs/utils/AI/translateJSON/index.cjs.map +0 -1
- package/dist/cjs/utils/access.cjs +0 -0
- package/dist/cjs/utils/accessControl.cjs +0 -133
- package/dist/cjs/utils/accessControl.cjs.map +0 -1
- package/dist/cjs/utils/auth/getAuth.cjs +0 -218
- package/dist/cjs/utils/auth/getAuth.cjs.map +0 -1
- package/dist/cjs/utils/cors.cjs +0 -33
- package/dist/cjs/utils/cors.cjs.map +0 -1
- package/dist/cjs/utils/ensureArrayQueryFilter.cjs +0 -13
- package/dist/cjs/utils/ensureArrayQueryFilter.cjs.map +0 -1
- package/dist/cjs/utils/ensureMongoDocumentToObject.cjs +0 -16
- package/dist/cjs/utils/ensureMongoDocumentToObject.cjs.map +0 -1
- package/dist/cjs/utils/errors/ErrorHandler.cjs +0 -76
- package/dist/cjs/utils/errors/ErrorHandler.cjs.map +0 -1
- package/dist/cjs/utils/errors/ErrorsClass.cjs +0 -54
- package/dist/cjs/utils/errors/ErrorsClass.cjs.map +0 -1
- package/dist/cjs/utils/errors/errorCodes.cjs +0 -1387
- package/dist/cjs/utils/errors/errorCodes.cjs.map +0 -1
- package/dist/cjs/utils/errors/index.cjs +0 -8
- package/dist/cjs/utils/extractJSON.cjs +0 -61
- package/dist/cjs/utils/extractJSON.cjs.map +0 -1
- package/dist/cjs/utils/filtersAndPagination/getDictionaryFiltersAndPagination.cjs +0 -94
- package/dist/cjs/utils/filtersAndPagination/getDictionaryFiltersAndPagination.cjs.map +0 -1
- package/dist/cjs/utils/filtersAndPagination/getDiscussionFiltersAndPagination.cjs +0 -65
- package/dist/cjs/utils/filtersAndPagination/getDiscussionFiltersAndPagination.cjs.map +0 -1
- package/dist/cjs/utils/filtersAndPagination/getFiltersAndPaginationFromBody.cjs +0 -36
- package/dist/cjs/utils/filtersAndPagination/getFiltersAndPaginationFromBody.cjs.map +0 -1
- package/dist/cjs/utils/filtersAndPagination/getOrganizationFiltersAndPagination.cjs +0 -49
- package/dist/cjs/utils/filtersAndPagination/getOrganizationFiltersAndPagination.cjs.map +0 -1
- package/dist/cjs/utils/filtersAndPagination/getProjectFiltersAndPagination.cjs +0 -53
- package/dist/cjs/utils/filtersAndPagination/getProjectFiltersAndPagination.cjs.map +0 -1
- package/dist/cjs/utils/filtersAndPagination/getTagFiltersAndPagination.cjs +0 -57
- package/dist/cjs/utils/filtersAndPagination/getTagFiltersAndPagination.cjs.map +0 -1
- package/dist/cjs/utils/filtersAndPagination/getUserFiltersAndPagination.cjs +0 -74
- package/dist/cjs/utils/filtersAndPagination/getUserFiltersAndPagination.cjs.map +0 -1
- package/dist/cjs/utils/httpStatusCodes.cjs +0 -401
- package/dist/cjs/utils/httpStatusCodes.cjs.map +0 -1
- package/dist/cjs/utils/mapper/dictionary.cjs +0 -29
- package/dist/cjs/utils/mapper/dictionary.cjs.map +0 -1
- package/dist/cjs/utils/mapper/organization.cjs +0 -19
- package/dist/cjs/utils/mapper/organization.cjs.map +0 -1
- package/dist/cjs/utils/mapper/project.cjs +0 -26
- package/dist/cjs/utils/mapper/project.cjs.map +0 -1
- package/dist/cjs/utils/mapper/session.cjs +0 -15
- package/dist/cjs/utils/mapper/session.cjs.map +0 -1
- package/dist/cjs/utils/mapper/tag.cjs +0 -25
- package/dist/cjs/utils/mapper/tag.cjs.map +0 -1
- package/dist/cjs/utils/mapper/user.cjs +0 -24
- package/dist/cjs/utils/mapper/user.cjs.map +0 -1
- package/dist/cjs/utils/mergeFunctionTypes.cjs +0 -0
- package/dist/cjs/utils/mongoDB/connectDB.cjs +0 -32
- package/dist/cjs/utils/mongoDB/connectDB.cjs.map +0 -1
- package/dist/cjs/utils/mongoDB/types.cjs +0 -0
- package/dist/cjs/utils/oAuth2.cjs +0 -25
- package/dist/cjs/utils/oAuth2.cjs.map +0 -1
- package/dist/cjs/utils/permissions.cjs +0 -103
- package/dist/cjs/utils/permissions.cjs.map +0 -1
- package/dist/cjs/utils/plan.cjs +0 -63
- package/dist/cjs/utils/plan.cjs.map +0 -1
- package/dist/cjs/utils/rateLimiter.cjs +0 -48
- package/dist/cjs/utils/rateLimiter.cjs.map +0 -1
- package/dist/cjs/utils/removeObjectKeys.cjs +0 -11
- package/dist/cjs/utils/removeObjectKeys.cjs.map +0 -1
- package/dist/cjs/utils/responseData.cjs +0 -33
- package/dist/cjs/utils/responseData.cjs.map +0 -1
- package/dist/cjs/utils/validation/validateArray.cjs +0 -31
- package/dist/cjs/utils/validation/validateArray.cjs.map +0 -1
- package/dist/cjs/utils/validation/validateDictionary.cjs +0 -35
- package/dist/cjs/utils/validation/validateDictionary.cjs.map +0 -1
- package/dist/cjs/utils/validation/validateEmail.cjs +0 -20
- package/dist/cjs/utils/validation/validateEmail.cjs.map +0 -1
- package/dist/cjs/utils/validation/validateOrganization.cjs +0 -43
- package/dist/cjs/utils/validation/validateOrganization.cjs.map +0 -1
- package/dist/cjs/utils/validation/validatePhone.cjs +0 -20
- package/dist/cjs/utils/validation/validatePhone.cjs.map +0 -1
- package/dist/cjs/utils/validation/validateProject.cjs +0 -55
- package/dist/cjs/utils/validation/validateProject.cjs.map +0 -1
- package/dist/cjs/utils/validation/validateString.cjs +0 -21
- package/dist/cjs/utils/validation/validateString.cjs.map +0 -1
- package/dist/cjs/utils/validation/validateTag.cjs +0 -47
- package/dist/cjs/utils/validation/validateTag.cjs.map +0 -1
- package/dist/cjs/utils/validation/validateUser.cjs +0 -47
- package/dist/cjs/utils/validation/validateUser.cjs.map +0 -1
- package/dist/cjs/webhooks/stripe.webhook.cjs +0 -116
- package/dist/cjs/webhooks/stripe.webhook.cjs.map +0 -1
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
2
|
-
const require__utils_asset = require('../../../_virtual/_utils_asset.cjs');
|
|
3
|
-
let __intlayer_core = require("@intlayer/core");
|
|
4
|
-
let __intlayer_docs = require("@intlayer/docs");
|
|
5
|
-
let ai = require("ai");
|
|
6
|
-
let openai = require("openai");
|
|
7
|
-
|
|
8
|
-
//#region src/utils/AI/askDocQuestion/askDocQuestion.ts
|
|
9
|
-
const readEmbeddingsForFile = (fileKey) => {
|
|
10
|
-
try {
|
|
11
|
-
return JSON.parse(require__utils_asset.readAsset(`./embeddings/${fileKey.replace(".md", ".json")}`, "utf-8"));
|
|
12
|
-
} catch {
|
|
13
|
-
return {};
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Simple in-memory vector store to hold document embeddings and their content.
|
|
18
|
-
* Each entry contains:
|
|
19
|
-
* - fileKey: A unique key identifying the file
|
|
20
|
-
* - chunkNumber: The number of the chunk within the document
|
|
21
|
-
* - content: The chunk content
|
|
22
|
-
* - embedding: The numerical embedding vector for the chunk
|
|
23
|
-
*/
|
|
24
|
-
const vectorStore = [];
|
|
25
|
-
const MAX_RELEVANT_CHUNKS_NB = 20;
|
|
26
|
-
const MIN_RELEVANT_CHUNKS_SIMILARITY = .42;
|
|
27
|
-
const EMBEDDING_MODEL = "text-embedding-3-large";
|
|
28
|
-
const OVERLAP_TOKENS = 200;
|
|
29
|
-
const MAX_CHUNK_TOKENS = 800;
|
|
30
|
-
const CHAR_BY_TOKEN = 4.15;
|
|
31
|
-
const MAX_CHARS = MAX_CHUNK_TOKENS * CHAR_BY_TOKEN;
|
|
32
|
-
const OVERLAP_CHARS = OVERLAP_TOKENS * CHAR_BY_TOKEN;
|
|
33
|
-
const skipDocEmbeddingsIndex = process.env.SKIP_DOC_EMBEDDINGS_INDEX === "true";
|
|
34
|
-
/**
|
|
35
|
-
* Splits a given text into chunks ensuring each chunk does not exceed MAX_CHARS.
|
|
36
|
-
* @param text - The input text to split.
|
|
37
|
-
* @returns - Array of text chunks.
|
|
38
|
-
*/
|
|
39
|
-
const chunkText = (text) => {
|
|
40
|
-
const chunks = [];
|
|
41
|
-
let start = 0;
|
|
42
|
-
while (start < text.length) {
|
|
43
|
-
let end = Math.min(start + MAX_CHARS, text.length);
|
|
44
|
-
if (end < text.length) {
|
|
45
|
-
const lastSpace = text.lastIndexOf(" ", end);
|
|
46
|
-
if (lastSpace > start) end = lastSpace;
|
|
47
|
-
}
|
|
48
|
-
chunks.push(text.substring(start, end));
|
|
49
|
-
const nextStart = end - OVERLAP_CHARS;
|
|
50
|
-
if (nextStart <= start) start = end;
|
|
51
|
-
else start = nextStart;
|
|
52
|
-
}
|
|
53
|
-
return chunks;
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* Generates an embedding for a given text using OpenAI's embedding API.
|
|
57
|
-
* Trims the text if it exceeds the maximum allowed characters.
|
|
58
|
-
*
|
|
59
|
-
* @param text - The input text to generate an embedding for
|
|
60
|
-
* @returns The embedding vector as a number array
|
|
61
|
-
*/
|
|
62
|
-
const generateEmbedding = async (text) => {
|
|
63
|
-
try {
|
|
64
|
-
return (await new openai.OpenAI({ apiKey: process.env.OPENAI_API_KEY }).embeddings.create({
|
|
65
|
-
model: EMBEDDING_MODEL,
|
|
66
|
-
input: text
|
|
67
|
-
})).data[0].embedding;
|
|
68
|
-
} catch (error) {
|
|
69
|
-
console.error("Error generating embedding:", error);
|
|
70
|
-
return [];
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
/**
|
|
74
|
-
* Calculates the cosine similarity between two vectors.
|
|
75
|
-
* Cosine similarity measures the cosine of the angle between two vectors in an inner product space.
|
|
76
|
-
* Used to determine the similarity between chunks of text.
|
|
77
|
-
*
|
|
78
|
-
* @param vecA - The first vector
|
|
79
|
-
* @param vecB - The second vector
|
|
80
|
-
* @returns The cosine similarity score
|
|
81
|
-
*/
|
|
82
|
-
const cosineSimilarity = (vecA, vecB) => {
|
|
83
|
-
return vecA.reduce((sum, a, idx) => sum + a * vecB[idx], 0) / (Math.sqrt(vecA.reduce((sum, a) => sum + a * a, 0)) * Math.sqrt(vecB.reduce((sum, b) => sum + b * b, 0)));
|
|
84
|
-
};
|
|
85
|
-
/**
|
|
86
|
-
* Indexes all Markdown documents by generating embeddings for each chunk and storing them in memory.
|
|
87
|
-
* Persists per-document embeddings under `embeddings/<fileKey>.json`.
|
|
88
|
-
* Handles cases where files have been updated and chunk counts have changed.
|
|
89
|
-
*/
|
|
90
|
-
const loadMarkdownFiles = async () => {
|
|
91
|
-
const frequentQuestions = await (0, __intlayer_docs.getFrequentQuestions)();
|
|
92
|
-
const docs = await (0, __intlayer_docs.getDocs)();
|
|
93
|
-
const blogs = await (0, __intlayer_docs.getBlogs)();
|
|
94
|
-
const files = {
|
|
95
|
-
...docs,
|
|
96
|
-
...blogs,
|
|
97
|
-
...frequentQuestions
|
|
98
|
-
};
|
|
99
|
-
for await (const fileKey of Object.keys(files)) {
|
|
100
|
-
const fileMetadata = (0, __intlayer_core.getMarkdownMetadata)(files[fileKey]);
|
|
101
|
-
const fileChunks = chunkText(files[fileKey]);
|
|
102
|
-
const existingEmbeddings = readEmbeddingsForFile(fileKey);
|
|
103
|
-
const existingChunksForFile = Object.keys(existingEmbeddings);
|
|
104
|
-
const currentChunkCount = fileChunks.length;
|
|
105
|
-
const previousChunkCount = existingChunksForFile.length;
|
|
106
|
-
let shouldRegenerateFileEmbeddings = false;
|
|
107
|
-
if (currentChunkCount !== previousChunkCount) {
|
|
108
|
-
console.info(`File "${fileKey}" chunk count changed: ${previousChunkCount} -> ${currentChunkCount}. Regenerating embeddings.`);
|
|
109
|
-
shouldRegenerateFileEmbeddings = !skipDocEmbeddingsIndex;
|
|
110
|
-
}
|
|
111
|
-
let resultForFile = {};
|
|
112
|
-
for await (const chunkIndex of Object.keys(fileChunks)) {
|
|
113
|
-
const chunkNumber = Number(chunkIndex) + 1;
|
|
114
|
-
const chunksNumber = fileChunks.length;
|
|
115
|
-
const fileChunk = fileChunks[chunkIndex];
|
|
116
|
-
const chunkKeyName = `chunk_${chunkNumber}`;
|
|
117
|
-
const embedding = !shouldRegenerateFileEmbeddings ? existingEmbeddings[chunkKeyName] : void 0;
|
|
118
|
-
resultForFile = {
|
|
119
|
-
...resultForFile,
|
|
120
|
-
[chunkKeyName]: embedding
|
|
121
|
-
};
|
|
122
|
-
vectorStore.push({
|
|
123
|
-
fileKey,
|
|
124
|
-
chunkNumber,
|
|
125
|
-
embedding,
|
|
126
|
-
content: fileChunk,
|
|
127
|
-
docUrl: fileMetadata.url,
|
|
128
|
-
docName: fileMetadata.title
|
|
129
|
-
});
|
|
130
|
-
console.info(`- Loaded: ${fileKey}/${chunkKeyName}/${chunksNumber}`);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
loadMarkdownFiles();
|
|
135
|
-
/**
|
|
136
|
-
* Searches the indexed documents for the most relevant chunks based on a query.
|
|
137
|
-
* Utilizes cosine similarity to find the closest matching embeddings.
|
|
138
|
-
*
|
|
139
|
-
* @param query - The search query provided by the user
|
|
140
|
-
* @returns An array of the top matching document chunks' content
|
|
141
|
-
*/
|
|
142
|
-
const searchChunkReference = async (query, maxResults = MAX_RELEVANT_CHUNKS_NB, minSimilarity = MIN_RELEVANT_CHUNKS_SIMILARITY) => {
|
|
143
|
-
const queryEmbedding = await generateEmbedding(query);
|
|
144
|
-
const selection = vectorStore.filter((chunk) => chunk.embedding).map((chunk) => ({
|
|
145
|
-
...chunk,
|
|
146
|
-
similarity: cosineSimilarity(queryEmbedding, chunk.embedding)
|
|
147
|
-
})).filter((chunk) => chunk.similarity > minSimilarity).sort((a, b) => b.similarity - a.similarity).slice(0, maxResults);
|
|
148
|
-
const orderedDocKeys = new Set(selection.map((chunk) => chunk.fileKey));
|
|
149
|
-
return vectorStore.sort((a, _b) => orderedDocKeys.has(a.fileKey) ? -1 : 1).filter((chunk) => selection.some((v) => v.fileKey === chunk.fileKey && v.chunkNumber === chunk.chunkNumber));
|
|
150
|
-
};
|
|
151
|
-
const CHAT_GPT_PROMPT = require__utils_asset.readAsset("./PROMPT.md");
|
|
152
|
-
const initPrompt = {
|
|
153
|
-
role: "system",
|
|
154
|
-
content: CHAT_GPT_PROMPT
|
|
155
|
-
};
|
|
156
|
-
/**
|
|
157
|
-
* Handles the "Ask a question" endpoint in an Express.js route.
|
|
158
|
-
* Processes user messages, retrieves relevant documents, and interacts with AI models to generate responses.
|
|
159
|
-
*
|
|
160
|
-
* @param messages - An array of chat messages from the user and assistant
|
|
161
|
-
* @returns The assistant's response as a string
|
|
162
|
-
*/
|
|
163
|
-
const askDocQuestion = async (messages, aiConfig, options) => {
|
|
164
|
-
const relevantFilesReferences = await searchChunkReference(messages.filter((message) => message.role === "user").map((message) => `- ${message.content}`).join("\n"));
|
|
165
|
-
const aiMessages = [{
|
|
166
|
-
role: "system",
|
|
167
|
-
content: initPrompt.content.replace("{{relevantFilesReferences}}", relevantFilesReferences.length === 0 ? "Not relevant file found related to the question." : relevantFilesReferences.map((doc, idx) => [
|
|
168
|
-
"-----",
|
|
169
|
-
"---",
|
|
170
|
-
`chunkId: ${idx}`,
|
|
171
|
-
`docChunk: "${doc.chunkNumber}/${doc.fileKey.length}"`,
|
|
172
|
-
`docName: "${doc.docName}"`,
|
|
173
|
-
`docUrl: "${doc.docUrl}"`,
|
|
174
|
-
`---`,
|
|
175
|
-
doc.content,
|
|
176
|
-
`-----`
|
|
177
|
-
].join("\n")).join("\n\n"))
|
|
178
|
-
}, ...messages.slice(-8)];
|
|
179
|
-
if (!aiConfig) throw new Error("Failed to initialize AI configuration");
|
|
180
|
-
let fullResponse = "";
|
|
181
|
-
const stream = (0, ai.streamText)({
|
|
182
|
-
...aiConfig,
|
|
183
|
-
messages: aiMessages
|
|
184
|
-
});
|
|
185
|
-
for await (const chunk of stream.textStream) {
|
|
186
|
-
fullResponse += chunk;
|
|
187
|
-
options?.onMessage?.(chunk);
|
|
188
|
-
}
|
|
189
|
-
const relatedFiles = [...new Set(relevantFilesReferences.map((doc) => doc.fileKey))];
|
|
190
|
-
return {
|
|
191
|
-
response: fullResponse ?? "Error: No result found",
|
|
192
|
-
relatedFiles
|
|
193
|
-
};
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
//#endregion
|
|
197
|
-
exports.askDocQuestion = askDocQuestion;
|
|
198
|
-
exports.initPrompt = initPrompt;
|
|
199
|
-
exports.loadMarkdownFiles = loadMarkdownFiles;
|
|
200
|
-
exports.searchChunkReference = searchChunkReference;
|
|
201
|
-
//# sourceMappingURL=askDocQuestion.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"askDocQuestion.cjs","names":["readAsset","vectorStore: VectorStoreEl[]","MAX_RELEVANT_CHUNKS_NB: number","MIN_RELEVANT_CHUNKS_SIMILARITY: number","EMBEDDING_MODEL: OpenAI.EmbeddingModel","OVERLAP_TOKENS: number","MAX_CHUNK_TOKENS: number","CHAR_BY_TOKEN: number","MAX_CHARS: number","OVERLAP_CHARS: number","chunks: string[]","OpenAI","resultForFile: Record<string, number[] | undefined>","initPrompt: ChatCompletionRequestMessage"],"sources":["../../../../../src/utils/AI/askDocQuestion/askDocQuestion.ts"],"sourcesContent":["import { readAsset } from 'utils:asset';\nimport { getMarkdownMetadata } from '@intlayer/core';\nimport { getBlogs, getDocs, getFrequentQuestions } from '@intlayer/docs';\nimport { streamText } from 'ai';\nimport { OpenAI } from 'openai';\nimport type { AIConfig, ChatCompletionRequestMessage } from '../aiSdk';\n\nconst readEmbeddingsForFile = (fileKey: string): Record<string, number[]> => {\n try {\n return JSON.parse(\n readAsset(`./embeddings/${fileKey.replace('.md', '.json')}`, 'utf-8')\n ) as Record<string, number[]>;\n } catch {\n return {};\n }\n};\n\ntype VectorStoreEl = {\n fileKey: string;\n chunkNumber: number;\n content: string;\n embedding?: number[];\n docUrl: string;\n docName: string;\n};\n\n/**\n * Simple in-memory vector store to hold document embeddings and their content.\n * Each entry contains:\n * - fileKey: A unique key identifying the file\n * - chunkNumber: The number of the chunk within the document\n * - content: The chunk content\n * - embedding: The numerical embedding vector for the chunk\n */\nconst vectorStore: VectorStoreEl[] = [];\n\n/*\n * Ask question AI configuration\n */\nconst MAX_RELEVANT_CHUNKS_NB: number = 20; // Maximum number of relevant chunks to attach to chatGPT context\nconst MIN_RELEVANT_CHUNKS_SIMILARITY: number = 0.42; // Minimum similarity required for a chunk to be considered relevant\n\n/*\n * Embedding model configuration\n */\nconst EMBEDDING_MODEL: OpenAI.EmbeddingModel = 'text-embedding-3-large'; // Model to use for embedding generation\nconst OVERLAP_TOKENS: number = 200; // Number of tokens to overlap between chunks\nconst MAX_CHUNK_TOKENS: number = 800; // Maximum number of tokens per chunk\nconst CHAR_BY_TOKEN: number = 4.15; // Approximate pessimistically the number of characters per token // Can use `tiktoken` or other tokenizers to calculate it more precisely\nconst MAX_CHARS: number = MAX_CHUNK_TOKENS * CHAR_BY_TOKEN;\nconst OVERLAP_CHARS: number = OVERLAP_TOKENS * CHAR_BY_TOKEN;\n\nconst skipDocEmbeddingsIndex = process.env.SKIP_DOC_EMBEDDINGS_INDEX === 'true';\n\n/**\n * Splits a given text into chunks ensuring each chunk does not exceed MAX_CHARS.\n * @param text - The input text to split.\n * @returns - Array of text chunks.\n */\nconst chunkText = (text: string): string[] => {\n const chunks: string[] = [];\n let start = 0;\n\n while (start < text.length) {\n let end = Math.min(start + MAX_CHARS, text.length);\n\n // Ensure we don't cut words in the middle (find nearest space)\n if (end < text.length) {\n const lastSpace = text.lastIndexOf(' ', end);\n if (lastSpace > start) {\n end = lastSpace;\n }\n }\n\n chunks.push(text.substring(start, end));\n\n // Move start forward correctly\n const nextStart = end - OVERLAP_CHARS;\n if (nextStart <= start) {\n // Prevent infinite loop if overlap is too large\n start = end;\n } else {\n start = nextStart;\n }\n }\n\n return chunks;\n};\n\n/**\n * Generates an embedding for a given text using OpenAI's embedding API.\n * Trims the text if it exceeds the maximum allowed characters.\n *\n * @param text - The input text to generate an embedding for\n * @returns The embedding vector as a number array\n */\nconst generateEmbedding = async (text: string): Promise<number[]> => {\n try {\n const openaiClient = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n\n const response = await openaiClient.embeddings.create({\n model: EMBEDDING_MODEL,\n input: text,\n });\n\n return response.data[0].embedding;\n } catch (error) {\n console.error('Error generating embedding:', error);\n return [];\n }\n};\n\n/**\n * Calculates the cosine similarity between two vectors.\n * Cosine similarity measures the cosine of the angle between two vectors in an inner product space.\n * Used to determine the similarity between chunks of text.\n *\n * @param vecA - The first vector\n * @param vecB - The second vector\n * @returns The cosine similarity score\n */\nconst cosineSimilarity = (vecA: number[], vecB: number[]): number => {\n // Calculate the dot product of the two vectors\n const dotProduct = vecA.reduce((sum, a, idx) => sum + a * vecB[idx], 0);\n\n // Calculate the magnitude (Euclidean norm) of each vector\n const magnitudeA = Math.sqrt(vecA.reduce((sum, a) => sum + a * a, 0));\n const magnitudeB = Math.sqrt(vecB.reduce((sum, b) => sum + b * b, 0));\n\n // Compute and return the cosine similarity\n return dotProduct / (magnitudeA * magnitudeB);\n};\n\n/**\n * Indexes all Markdown documents by generating embeddings for each chunk and storing them in memory.\n * Persists per-document embeddings under `embeddings/<fileKey>.json`.\n * Handles cases where files have been updated and chunk counts have changed.\n */\nexport const loadMarkdownFiles = async (): Promise<void> => {\n // Retrieve documentation and blog posts in English locale\n const frequentQuestions = await getFrequentQuestions();\n const docs = await getDocs();\n const blogs = await getBlogs();\n\n const files = { ...docs, ...blogs, ...frequentQuestions }; // Combine docs and blogs into a single object\n\n // Iterate over each file key (identifier) in the combined files\n for await (const fileKey of Object.keys(files)) {\n // Get the metadata of the file\n const fileMetadata = getMarkdownMetadata(\n files[fileKey as keyof typeof files] as string\n );\n\n // Split the document into chunks based on headings\n const fileChunks = chunkText(\n files[fileKey as keyof typeof files] as string\n );\n\n // Read existing embeddings for this file\n const existingEmbeddings = readEmbeddingsForFile(fileKey);\n\n // Check if the number of chunks has changed for this file\n const existingChunksForFile = Object.keys(existingEmbeddings);\n const currentChunkCount = fileChunks.length;\n const previousChunkCount = existingChunksForFile.length;\n\n let shouldRegenerateFileEmbeddings = false;\n\n // If chunk count differs, we need to regenerate embeddings for this file\n if (currentChunkCount !== previousChunkCount) {\n console.info(\n `File \"${fileKey}\" chunk count changed: ${previousChunkCount} -> ${currentChunkCount}. Regenerating embeddings.`\n );\n\n shouldRegenerateFileEmbeddings = !skipDocEmbeddingsIndex;\n }\n\n // Iterate over each chunk within the current file\n let resultForFile: Record<string, number[] | undefined> = {};\n for await (const chunkIndex of Object.keys(fileChunks)) {\n const chunkNumber = Number(chunkIndex) + 1; // Chunk number starts at 1\n const chunksNumber = fileChunks.length;\n\n const fileChunk = fileChunks[\n chunkIndex as keyof typeof fileChunks\n ] as string;\n\n const chunkKeyName = `chunk_${chunkNumber}`; // Unique key for the chunk within the file\n\n // Retrieve precomputed embedding if available and file hasn't changed\n const docEmbedding = !shouldRegenerateFileEmbeddings\n ? (existingEmbeddings[\n chunkKeyName as keyof typeof existingEmbeddings\n ] as number[] | undefined)\n : undefined;\n\n const embedding = docEmbedding; // Use existing embedding if available and valid\n\n // Update the file-scoped result object with the embedding\n resultForFile = { ...resultForFile, [chunkKeyName]: embedding };\n\n // Store the embedding and content in the in-memory vector store\n vectorStore.push({\n fileKey,\n chunkNumber,\n embedding,\n content: fileChunk,\n docUrl: fileMetadata.url,\n docName: fileMetadata.title,\n });\n\n console.info(`- Loaded: ${fileKey}/${chunkKeyName}/${chunksNumber}`);\n }\n }\n};\n\n// Automatically index Markdown files\nloadMarkdownFiles();\n\n/**\n * Searches the indexed documents for the most relevant chunks based on a query.\n * Utilizes cosine similarity to find the closest matching embeddings.\n *\n * @param query - The search query provided by the user\n * @returns An array of the top matching document chunks' content\n */\nexport const searchChunkReference = async (\n query: string,\n maxResults: number = MAX_RELEVANT_CHUNKS_NB,\n minSimilarity: number = MIN_RELEVANT_CHUNKS_SIMILARITY\n): Promise<VectorStoreEl[]> => {\n // Generate an embedding for the user's query\n const queryEmbedding = await generateEmbedding(query);\n\n // Calculate similarity scores between the query embedding and each document's embedding\n const selection = vectorStore\n .filter((chunk) => chunk.embedding)\n .map((chunk) => ({\n ...chunk,\n similarity: cosineSimilarity(queryEmbedding, chunk.embedding!), // Add similarity score to each doc\n }))\n .filter((chunk) => chunk.similarity > minSimilarity) // Filter out documents with low similarity scores\n .sort((a, b) => b.similarity - a.similarity) // Sort documents by highest similarity first\n .slice(0, maxResults); // Select the top 6 most similar documents\n\n const orderedDocKeys = new Set(selection.map((chunk) => chunk.fileKey));\n\n const orderedVectorStore = vectorStore.sort((a, _b) =>\n orderedDocKeys.has(a.fileKey) ? -1 : 1\n );\n\n const results = orderedVectorStore.filter((chunk) =>\n selection.some(\n (v) => v.fileKey === chunk.fileKey && v.chunkNumber === chunk.chunkNumber\n )\n );\n\n // Return the content of the top matching documents\n return results;\n};\n\nconst CHAT_GPT_PROMPT = readAsset('./PROMPT.md');\n\n// Initial prompt configuration for the chatbot\nexport const initPrompt: ChatCompletionRequestMessage = {\n role: 'system',\n content: CHAT_GPT_PROMPT,\n};\n\nexport type AskDocQuestionResult = {\n response: string;\n relatedFiles: string[];\n};\n\nexport type AskDocQuestionOptions = {\n onMessage?: (chunk: string) => void;\n};\n\n/**\n * Handles the \"Ask a question\" endpoint in an Express.js route.\n * Processes user messages, retrieves relevant documents, and interacts with AI models to generate responses.\n *\n * @param messages - An array of chat messages from the user and assistant\n * @returns The assistant's response as a string\n */\nexport const askDocQuestion = async (\n messages: ChatCompletionRequestMessage[],\n aiConfig: AIConfig,\n options?: AskDocQuestionOptions\n): Promise<AskDocQuestionResult> => {\n // Format the user's question to keep only the relevant keywords\n const query = messages\n .filter((message) => message.role === 'user')\n .map((message) => `- ${message.content}`)\n .join('\\n');\n\n // 1) Find relevant documents based on the user's question\n const relevantFilesReferences = await searchChunkReference(query);\n\n // 2) Integrate the relevant documents into the initial system prompt\n const systemPrompt = initPrompt.content.replace(\n '{{relevantFilesReferences}}',\n relevantFilesReferences.length === 0\n ? 'Not relevant file found related to the question.'\n : relevantFilesReferences\n .map((doc, idx) =>\n [\n '-----',\n '---',\n `chunkId: ${idx}`,\n `docChunk: \"${doc.chunkNumber}/${doc.fileKey.length}\"`,\n `docName: \"${doc.docName}\"`,\n `docUrl: \"${doc.docUrl}\"`,\n `---`,\n doc.content,\n `-----`,\n ].join('\\n')\n )\n .join('\\n\\n') // Insert relevant docs into the prompt\n );\n\n // Format messages for AI SDK\n const aiMessages = [\n {\n role: 'system' as const,\n content: systemPrompt,\n },\n ...messages.slice(-8),\n ];\n\n if (!aiConfig) {\n throw new Error('Failed to initialize AI configuration');\n }\n\n // 3) Use the AI SDK to stream the response\n let fullResponse = '';\n const stream = streamText({\n ...aiConfig,\n messages: aiMessages,\n });\n\n // Process the stream\n for await (const chunk of stream.textStream) {\n fullResponse += chunk;\n options?.onMessage?.(chunk);\n }\n\n // 4) Extract unique related files\n const relatedFiles = [\n ...new Set(relevantFilesReferences.map((doc) => doc.fileKey)),\n ];\n\n // 5) Return the assistant's response to the user\n return {\n response: fullResponse ?? 'Error: No result found',\n relatedFiles,\n };\n};\n"],"mappings":"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"}
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
2
|
-
let node_fs = require("node:fs");
|
|
3
|
-
let node_path = require("node:path");
|
|
4
|
-
let node_url = require("node:url");
|
|
5
|
-
let __intlayer_core = require("@intlayer/core");
|
|
6
|
-
let __intlayer_docs = require("@intlayer/docs");
|
|
7
|
-
let openai = require("openai");
|
|
8
|
-
let dotenv = require("dotenv");
|
|
9
|
-
dotenv = require_rolldown_runtime.__toESM(dotenv);
|
|
10
|
-
|
|
11
|
-
//#region src/utils/AI/askDocQuestion/indexMarkdownFiles.ts
|
|
12
|
-
const OUTPUT_EMBEDDINGS_DIR = "src/utils/AI/askDocQuestion/embeddings";
|
|
13
|
-
const __dirname$1 = (0, node_path.dirname)((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));
|
|
14
|
-
const readEmbeddingsForFile = (fileKey) => {
|
|
15
|
-
try {
|
|
16
|
-
return JSON.parse((0, node_fs.readFileSync)(`${__dirname$1}/embeddings/${fileKey.replace(".md", ".json")}`, "utf-8"));
|
|
17
|
-
} catch {
|
|
18
|
-
return {};
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
const writeEmbeddingsForFile = (fileKey, data) => {
|
|
22
|
-
const filePath = (0, node_path.join)(OUTPUT_EMBEDDINGS_DIR, `${fileKey.replace(".md", ".json")}`);
|
|
23
|
-
const dir = (0, node_path.dirname)(filePath);
|
|
24
|
-
if (!(0, node_fs.existsSync)(dir)) (0, node_fs.mkdirSync)(dir, { recursive: true });
|
|
25
|
-
(0, node_fs.writeFileSync)(filePath, JSON.stringify(data));
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* Simple in-memory vector store to hold document embeddings and their content.
|
|
29
|
-
* Each entry contains:
|
|
30
|
-
* - fileKey: A unique key identifying the file
|
|
31
|
-
* - chunkNumber: The number of the chunk within the document
|
|
32
|
-
* - content: The chunk content
|
|
33
|
-
* - embedding: The numerical embedding vector for the chunk
|
|
34
|
-
*/
|
|
35
|
-
const vectorStore = [];
|
|
36
|
-
const EMBEDDING_MODEL = "text-embedding-3-large";
|
|
37
|
-
const OVERLAP_TOKENS = 200;
|
|
38
|
-
const MAX_CHUNK_TOKENS = 800;
|
|
39
|
-
const CHAR_BY_TOKEN = 4.15;
|
|
40
|
-
const MAX_CHARS = MAX_CHUNK_TOKENS * CHAR_BY_TOKEN;
|
|
41
|
-
const OVERLAP_CHARS = OVERLAP_TOKENS * CHAR_BY_TOKEN;
|
|
42
|
-
const skipDocEmbeddingsIndex = process.env.SKIP_DOC_EMBEDDINGS_INDEX === "true";
|
|
43
|
-
/**
|
|
44
|
-
* Splits a given text into chunks ensuring each chunk does not exceed MAX_CHARS.
|
|
45
|
-
* @param text - The input text to split.
|
|
46
|
-
* @returns - Array of text chunks.
|
|
47
|
-
*/
|
|
48
|
-
const chunkText = (text) => {
|
|
49
|
-
const chunks = [];
|
|
50
|
-
let start = 0;
|
|
51
|
-
while (start < text.length) {
|
|
52
|
-
let end = Math.min(start + MAX_CHARS, text.length);
|
|
53
|
-
if (end < text.length) {
|
|
54
|
-
const lastSpace = text.lastIndexOf(" ", end);
|
|
55
|
-
if (lastSpace > start) end = lastSpace;
|
|
56
|
-
}
|
|
57
|
-
chunks.push(text.substring(start, end));
|
|
58
|
-
const nextStart = end - OVERLAP_CHARS;
|
|
59
|
-
if (nextStart <= start) start = end;
|
|
60
|
-
else start = nextStart;
|
|
61
|
-
}
|
|
62
|
-
return chunks;
|
|
63
|
-
};
|
|
64
|
-
/**
|
|
65
|
-
* Generates an embedding for a given text using OpenAI's embedding API.
|
|
66
|
-
* Trims the text if it exceeds the maximum allowed characters.
|
|
67
|
-
*
|
|
68
|
-
* @param text - The input text to generate an embedding for
|
|
69
|
-
* @returns The embedding vector as a number array
|
|
70
|
-
*/
|
|
71
|
-
const generateEmbedding = async (text) => {
|
|
72
|
-
try {
|
|
73
|
-
return (await new openai.OpenAI({ apiKey: process.env.OPENAI_API_KEY }).embeddings.create({
|
|
74
|
-
model: EMBEDDING_MODEL,
|
|
75
|
-
input: text
|
|
76
|
-
})).data[0].embedding;
|
|
77
|
-
} catch (error) {
|
|
78
|
-
console.error("Error generating embedding:", error);
|
|
79
|
-
return [];
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
/**
|
|
83
|
-
* Indexes all Markdown documents by generating embeddings for each chunk and storing them in memory.
|
|
84
|
-
* Persists per-document embeddings under `embeddings/<fileKey>.json`.
|
|
85
|
-
* Handles cases where files have been updated and chunk counts have changed.
|
|
86
|
-
*/
|
|
87
|
-
const indexMarkdownFiles = async () => {
|
|
88
|
-
const env = process.env.NODE_ENV;
|
|
89
|
-
dotenv.default.config({ path: [
|
|
90
|
-
`.env.${env}.local`,
|
|
91
|
-
`.env.${env}`,
|
|
92
|
-
".env.local",
|
|
93
|
-
".env"
|
|
94
|
-
] });
|
|
95
|
-
const frequentQuestions = await (0, __intlayer_docs.getFrequentQuestions)();
|
|
96
|
-
const docs = await (0, __intlayer_docs.getDocs)();
|
|
97
|
-
const blogs = await (0, __intlayer_docs.getBlogs)();
|
|
98
|
-
const files = {
|
|
99
|
-
...docs,
|
|
100
|
-
...blogs,
|
|
101
|
-
...frequentQuestions
|
|
102
|
-
};
|
|
103
|
-
for await (const fileKey of Object.keys(files)) {
|
|
104
|
-
const fileMetadata = (0, __intlayer_core.getMarkdownMetadata)(files[fileKey]);
|
|
105
|
-
const fileChunks = chunkText(files[fileKey]);
|
|
106
|
-
const existingEmbeddings = readEmbeddingsForFile(fileKey);
|
|
107
|
-
const existingChunksForFile = Object.keys(existingEmbeddings);
|
|
108
|
-
const currentChunkCount = fileChunks.length;
|
|
109
|
-
const previousChunkCount = existingChunksForFile.length;
|
|
110
|
-
let shouldRegenerateFileEmbeddings = false;
|
|
111
|
-
if (currentChunkCount !== previousChunkCount) {
|
|
112
|
-
console.info(`File "${fileKey}" chunk count changed: ${previousChunkCount} -> ${currentChunkCount}. Regenerating embeddings.`);
|
|
113
|
-
shouldRegenerateFileEmbeddings = !skipDocEmbeddingsIndex;
|
|
114
|
-
}
|
|
115
|
-
let resultForFile = {};
|
|
116
|
-
for await (const chunkIndex of Object.keys(fileChunks)) {
|
|
117
|
-
const chunkNumber = Number(chunkIndex) + 1;
|
|
118
|
-
const chunksNumber = fileChunks.length;
|
|
119
|
-
const fileChunk = fileChunks[chunkIndex];
|
|
120
|
-
const chunkKeyName = `chunk_${chunkNumber}`;
|
|
121
|
-
let embedding = !shouldRegenerateFileEmbeddings ? existingEmbeddings[chunkKeyName] : void 0;
|
|
122
|
-
if (!embedding) {
|
|
123
|
-
embedding = await generateEmbedding(fileChunk);
|
|
124
|
-
console.info(`- Generated new embedding: ${fileKey}/${chunkKeyName}`);
|
|
125
|
-
}
|
|
126
|
-
resultForFile = {
|
|
127
|
-
...resultForFile,
|
|
128
|
-
[chunkKeyName]: embedding
|
|
129
|
-
};
|
|
130
|
-
vectorStore.push({
|
|
131
|
-
fileKey,
|
|
132
|
-
chunkNumber,
|
|
133
|
-
embedding,
|
|
134
|
-
content: fileChunk,
|
|
135
|
-
docUrl: fileMetadata.url,
|
|
136
|
-
docName: fileMetadata.title
|
|
137
|
-
});
|
|
138
|
-
console.info(`- Indexed: ${fileKey}/${chunkKeyName}/${chunksNumber}`);
|
|
139
|
-
}
|
|
140
|
-
try {
|
|
141
|
-
if (JSON.stringify(resultForFile) !== JSON.stringify(existingEmbeddings)) writeEmbeddingsForFile(fileKey, resultForFile);
|
|
142
|
-
} catch (error) {
|
|
143
|
-
console.error(error);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
//#endregion
|
|
149
|
-
exports.indexMarkdownFiles = indexMarkdownFiles;
|
|
150
|
-
//# sourceMappingURL=indexMarkdownFiles.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"indexMarkdownFiles.cjs","names":["__dirname","vectorStore: VectorStoreEl[]","EMBEDDING_MODEL: OpenAI.EmbeddingModel","OVERLAP_TOKENS: number","MAX_CHUNK_TOKENS: number","CHAR_BY_TOKEN: number","MAX_CHARS: number","OVERLAP_CHARS: number","chunks: string[]","OpenAI","resultForFile: Record<string, number[]>"],"sources":["../../../../../src/utils/AI/askDocQuestion/indexMarkdownFiles.ts"],"sourcesContent":["import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';\nimport { dirname, join } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { getMarkdownMetadata } from '@intlayer/core';\nimport { getBlogs, getDocs, getFrequentQuestions } from '@intlayer/docs';\nimport dotenv from 'dotenv';\nimport { OpenAI } from 'openai';\nimport { type AIOptions, AIProvider } from '../aiSdk';\n\nconst OUTPUT_EMBEDDINGS_DIR = 'src/utils/AI/askDocQuestion/embeddings';\nconst __dirname = dirname(fileURLToPath(import.meta.url));\n\nconst readEmbeddingsForFile = (fileKey: string): Record<string, number[]> => {\n try {\n return JSON.parse(\n readFileSync(\n `${__dirname}/embeddings/${fileKey.replace('.md', '.json')}`,\n 'utf-8'\n )\n ) as Record<string, number[]>;\n } catch {\n return {};\n }\n};\n\nconst writeEmbeddingsForFile = (\n fileKey: string,\n data: Record<string, number[]>\n): void => {\n const filePath = join(\n OUTPUT_EMBEDDINGS_DIR,\n `${fileKey.replace('.md', '.json')}`\n );\n const dir = dirname(filePath);\n if (!existsSync(dir)) {\n mkdirSync(dir, { recursive: true });\n }\n writeFileSync(filePath, JSON.stringify(data));\n};\n\ntype VectorStoreEl = {\n fileKey: string;\n chunkNumber: number;\n content: string;\n embedding: number[];\n docUrl: string;\n docName: string;\n};\n\n/**\n * Simple in-memory vector store to hold document embeddings and their content.\n * Each entry contains:\n * - fileKey: A unique key identifying the file\n * - chunkNumber: The number of the chunk within the document\n * - content: The chunk content\n * - embedding: The numerical embedding vector for the chunk\n */\nconst vectorStore: VectorStoreEl[] = [];\n\n/*\n * Embedding model configuration\n */\nconst EMBEDDING_MODEL: OpenAI.EmbeddingModel = 'text-embedding-3-large'; // Model to use for embedding generation\nconst OVERLAP_TOKENS: number = 200; // Number of tokens to overlap between chunks\nconst MAX_CHUNK_TOKENS: number = 800; // Maximum number of tokens per chunk\nconst CHAR_BY_TOKEN: number = 4.15; // Approximate pessimistically the number of characters per token // Can use `tiktoken` or other tokenizers to calculate it more precisely\nconst MAX_CHARS: number = MAX_CHUNK_TOKENS * CHAR_BY_TOKEN;\nconst OVERLAP_CHARS: number = OVERLAP_TOKENS * CHAR_BY_TOKEN;\n\nconst skipDocEmbeddingsIndex = process.env.SKIP_DOC_EMBEDDINGS_INDEX === 'true';\n\n/**\n * Splits a given text into chunks ensuring each chunk does not exceed MAX_CHARS.\n * @param text - The input text to split.\n * @returns - Array of text chunks.\n */\nconst chunkText = (text: string): string[] => {\n const chunks: string[] = [];\n let start = 0;\n\n while (start < text.length) {\n let end = Math.min(start + MAX_CHARS, text.length);\n\n // Ensure we don't cut words in the middle (find nearest space)\n if (end < text.length) {\n const lastSpace = text.lastIndexOf(' ', end);\n if (lastSpace > start) {\n end = lastSpace;\n }\n }\n\n chunks.push(text.substring(start, end));\n\n // Move start forward correctly\n const nextStart = end - OVERLAP_CHARS;\n if (nextStart <= start) {\n // Prevent infinite loop if overlap is too large\n start = end;\n } else {\n start = nextStart;\n }\n }\n\n return chunks;\n};\n\n/**\n * Generates an embedding for a given text using OpenAI's embedding API.\n * Trims the text if it exceeds the maximum allowed characters.\n *\n * @param text - The input text to generate an embedding for\n * @returns The embedding vector as a number array\n */\nconst generateEmbedding = async (text: string): Promise<number[]> => {\n try {\n const openaiClient = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n\n const response = await openaiClient.embeddings.create({\n model: EMBEDDING_MODEL,\n input: text,\n });\n\n return response.data[0].embedding;\n } catch (error) {\n console.error('Error generating embedding:', error);\n return [];\n }\n};\n\n/**\n * Indexes all Markdown documents by generating embeddings for each chunk and storing them in memory.\n * Persists per-document embeddings under `embeddings/<fileKey>.json`.\n * Handles cases where files have been updated and chunk counts have changed.\n */\nexport const indexMarkdownFiles = async (): Promise<void> => {\n const env = process.env.NODE_ENV;\n dotenv.config({\n path: [`.env.${env}.local`, `.env.${env}`, '.env.local', '.env'],\n });\n\n // Retrieve documentation and blog posts in English locale\n const frequentQuestions = await getFrequentQuestions();\n const docs = await getDocs();\n const blogs = await getBlogs();\n\n const files = { ...docs, ...blogs, ...frequentQuestions }; // Combine docs and blogs into a single object\n\n // Iterate over each file key (identifier) in the combined files\n for await (const fileKey of Object.keys(files)) {\n // Get the metadata of the file\n const fileMetadata = getMarkdownMetadata(\n files[fileKey as keyof typeof files] as string\n );\n\n // Split the document into chunks based on headings\n const fileChunks = chunkText(\n files[fileKey as keyof typeof files] as string\n );\n\n // Read existing embeddings for this file\n const existingEmbeddings = readEmbeddingsForFile(fileKey);\n\n // Check if the number of chunks has changed for this file\n const existingChunksForFile = Object.keys(existingEmbeddings);\n const currentChunkCount = fileChunks.length;\n const previousChunkCount = existingChunksForFile.length;\n\n let shouldRegenerateFileEmbeddings = false;\n\n // If chunk count differs, we need to regenerate embeddings for this file\n if (currentChunkCount !== previousChunkCount) {\n console.info(\n `File \"${fileKey}\" chunk count changed: ${previousChunkCount} -> ${currentChunkCount}. Regenerating embeddings.`\n );\n\n shouldRegenerateFileEmbeddings = !skipDocEmbeddingsIndex;\n }\n\n // Iterate over each chunk within the current file\n let resultForFile: Record<string, number[]> = {};\n for await (const chunkIndex of Object.keys(fileChunks)) {\n const chunkNumber = Number(chunkIndex) + 1; // Chunk number starts at 1\n const chunksNumber = fileChunks.length;\n\n const fileChunk = fileChunks[\n chunkIndex as keyof typeof fileChunks\n ] as string;\n\n const chunkKeyName = `chunk_${chunkNumber}`; // Unique key for the chunk within the file\n\n // Retrieve precomputed embedding if available and file hasn't changed\n const docEmbedding = !shouldRegenerateFileEmbeddings\n ? (existingEmbeddings[\n chunkKeyName as keyof typeof existingEmbeddings\n ] as number[] | undefined)\n : undefined;\n\n let embedding = docEmbedding; // Use existing embedding if available and valid\n\n if (!embedding) {\n embedding = await generateEmbedding(fileChunk); // Generate embedding if not present or file changed\n console.info(`- Generated new embedding: ${fileKey}/${chunkKeyName}`);\n }\n\n // Update the file-scoped result object with the embedding\n resultForFile = { ...resultForFile, [chunkKeyName]: embedding };\n\n // Store the embedding and content in the in-memory vector store\n vectorStore.push({\n fileKey,\n chunkNumber,\n embedding,\n content: fileChunk,\n docUrl: fileMetadata.url,\n docName: fileMetadata.title,\n });\n\n console.info(`- Indexed: ${fileKey}/${chunkKeyName}/${chunksNumber}`);\n }\n\n // Persist per-file embeddings if changed\n try {\n if (\n JSON.stringify(resultForFile) !== JSON.stringify(existingEmbeddings)\n ) {\n writeEmbeddingsForFile(fileKey, resultForFile);\n }\n } catch (error) {\n console.error(error);\n }\n }\n};\n"],"mappings":";;;;;;;;;;;AASA,MAAM,wBAAwB;AAC9B,MAAMA,+GAAkD,CAAC;AAEzD,MAAM,yBAAyB,YAA8C;AAC3E,KAAI;AACF,SAAO,KAAK,gCAER,GAAGA,YAAU,cAAc,QAAQ,QAAQ,OAAO,QAAQ,IAC1D,QACD,CACF;SACK;AACN,SAAO,EAAE;;;AAIb,MAAM,0BACJ,SACA,SACS;CACT,MAAM,+BACJ,uBACA,GAAG,QAAQ,QAAQ,OAAO,QAAQ,GACnC;CACD,MAAM,6BAAc,SAAS;AAC7B,KAAI,yBAAY,IAAI,CAClB,wBAAU,KAAK,EAAE,WAAW,MAAM,CAAC;AAErC,4BAAc,UAAU,KAAK,UAAU,KAAK,CAAC;;;;;;;;;;AAoB/C,MAAMC,cAA+B,EAAE;AAKvC,MAAMC,kBAAyC;AAC/C,MAAMC,iBAAyB;AAC/B,MAAMC,mBAA2B;AACjC,MAAMC,gBAAwB;AAC9B,MAAMC,YAAoB,mBAAmB;AAC7C,MAAMC,gBAAwB,iBAAiB;AAE/C,MAAM,yBAAyB,QAAQ,IAAI,8BAA8B;;;;;;AAOzE,MAAM,aAAa,SAA2B;CAC5C,MAAMC,SAAmB,EAAE;CAC3B,IAAI,QAAQ;AAEZ,QAAO,QAAQ,KAAK,QAAQ;EAC1B,IAAI,MAAM,KAAK,IAAI,QAAQ,WAAW,KAAK,OAAO;AAGlD,MAAI,MAAM,KAAK,QAAQ;GACrB,MAAM,YAAY,KAAK,YAAY,KAAK,IAAI;AAC5C,OAAI,YAAY,MACd,OAAM;;AAIV,SAAO,KAAK,KAAK,UAAU,OAAO,IAAI,CAAC;EAGvC,MAAM,YAAY,MAAM;AACxB,MAAI,aAAa,MAEf,SAAQ;MAER,SAAQ;;AAIZ,QAAO;;;;;;;;;AAUT,MAAM,oBAAoB,OAAO,SAAoC;AACnE,KAAI;AAQF,UALiB,MAFI,IAAIC,cAAO,EAAE,QAAQ,QAAQ,IAAI,gBAAgB,CAAC,CAEnC,WAAW,OAAO;GACpD,OAAO;GACP,OAAO;GACR,CAAC,EAEc,KAAK,GAAG;UACjB,OAAO;AACd,UAAQ,MAAM,+BAA+B,MAAM;AACnD,SAAO,EAAE;;;;;;;;AASb,MAAa,qBAAqB,YAA2B;CAC3D,MAAM,MAAM,QAAQ,IAAI;AACxB,gBAAO,OAAO,EACZ,MAAM;EAAC,QAAQ,IAAI;EAAS,QAAQ;EAAO;EAAc;EAAO,EACjE,CAAC;CAGF,MAAM,oBAAoB,iDAA4B;CACtD,MAAM,OAAO,oCAAe;CAC5B,MAAM,QAAQ,qCAAgB;CAE9B,MAAM,QAAQ;EAAE,GAAG;EAAM,GAAG;EAAO,GAAG;EAAmB;AAGzD,YAAW,MAAM,WAAW,OAAO,KAAK,MAAM,EAAE;EAE9C,MAAM,wDACJ,MAAM,SACP;EAGD,MAAM,aAAa,UACjB,MAAM,SACP;EAGD,MAAM,qBAAqB,sBAAsB,QAAQ;EAGzD,MAAM,wBAAwB,OAAO,KAAK,mBAAmB;EAC7D,MAAM,oBAAoB,WAAW;EACrC,MAAM,qBAAqB,sBAAsB;EAEjD,IAAI,iCAAiC;AAGrC,MAAI,sBAAsB,oBAAoB;AAC5C,WAAQ,KACN,SAAS,QAAQ,yBAAyB,mBAAmB,MAAM,kBAAkB,4BACtF;AAED,oCAAiC,CAAC;;EAIpC,IAAIC,gBAA0C,EAAE;AAChD,aAAW,MAAM,cAAc,OAAO,KAAK,WAAW,EAAE;GACtD,MAAM,cAAc,OAAO,WAAW,GAAG;GACzC,MAAM,eAAe,WAAW;GAEhC,MAAM,YAAY,WAChB;GAGF,MAAM,eAAe,SAAS;GAS9B,IAAI,YANiB,CAAC,iCACjB,mBACC,gBAEF;AAIJ,OAAI,CAAC,WAAW;AACd,gBAAY,MAAM,kBAAkB,UAAU;AAC9C,YAAQ,KAAK,8BAA8B,QAAQ,GAAG,eAAe;;AAIvE,mBAAgB;IAAE,GAAG;KAAgB,eAAe;IAAW;AAG/D,eAAY,KAAK;IACf;IACA;IACA;IACA,SAAS;IACT,QAAQ,aAAa;IACrB,SAAS,aAAa;IACvB,CAAC;AAEF,WAAQ,KAAK,cAAc,QAAQ,GAAG,aAAa,GAAG,eAAe;;AAIvE,MAAI;AACF,OACE,KAAK,UAAU,cAAc,KAAK,KAAK,UAAU,mBAAmB,CAEpE,wBAAuB,SAAS,cAAc;WAEzC,OAAO;AACd,WAAQ,MAAM,MAAM"}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
2
|
-
const require_logger_index = require('../../../logger/index.cjs');
|
|
3
|
-
const require__utils_asset = require('../../../_virtual/_utils_asset.cjs');
|
|
4
|
-
const require_utils_extractJSON = require('../../extractJSON.cjs');
|
|
5
|
-
let __intlayer_types = require("@intlayer/types");
|
|
6
|
-
let __intlayer_core = require("@intlayer/core");
|
|
7
|
-
let ai = require("ai");
|
|
8
|
-
|
|
9
|
-
//#region src/utils/AI/auditDictionary/index.ts
|
|
10
|
-
const CHAT_GPT_PROMPT = require__utils_asset.readAsset("./PROMPT.md");
|
|
11
|
-
const aiDefaultOptions = {};
|
|
12
|
-
/**
|
|
13
|
-
* Format a locale with its name.
|
|
14
|
-
*
|
|
15
|
-
* @param locale - The locale to format.
|
|
16
|
-
* @returns A string in the format "locale: name", e.g. "en: English".
|
|
17
|
-
*/
|
|
18
|
-
const formatLocaleWithName = (locale) => {
|
|
19
|
-
return `${locale}: ${(0, __intlayer_core.getLocaleName)(locale, __intlayer_types.Locales.ENGLISH)}`;
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* Formats tag instructions for the AI prompt.
|
|
23
|
-
* Creates a string with all available tags and their descriptions.
|
|
24
|
-
*
|
|
25
|
-
* @param tags - The list of tags to format.
|
|
26
|
-
* @returns A formatted string with tag instructions.
|
|
27
|
-
*/
|
|
28
|
-
const formatTagInstructions = (tags) => {
|
|
29
|
-
if (!tags || tags.length === 0) return "";
|
|
30
|
-
return [`Based on the dictionary content, identify specific tags from the list below that would be relevant:`, tags.map(({ key, description }) => `- ${key}: ${description}`).join("\n\n")].join("\n\n");
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Audits a content declaration file by constructing a prompt for AI models.
|
|
34
|
-
* The prompt includes details about the project's locales, file paths of content declarations,
|
|
35
|
-
* and requests for identifying issues or inconsistencies.
|
|
36
|
-
*/
|
|
37
|
-
const auditDictionary = async ({ fileContent, filePath, locales, defaultLocale, tags, aiConfig, applicationContext }) => {
|
|
38
|
-
const otherLocales = locales.filter((locale) => locale !== defaultLocale);
|
|
39
|
-
const prompt = CHAT_GPT_PROMPT.replace("{{defaultLocale}}", formatLocaleWithName(defaultLocale)).replace("{{otherLocales}}", `{${otherLocales.map(formatLocaleWithName).join(", ")}}`).replace("{{filePath}}", filePath ?? "").replace("{{applicationContext}}", applicationContext ?? "").replace("{{tagsInstructions}}", formatTagInstructions(tags));
|
|
40
|
-
const { text: newContent, usage } = await (0, ai.generateText)({
|
|
41
|
-
...aiConfig,
|
|
42
|
-
messages: [{
|
|
43
|
-
role: "system",
|
|
44
|
-
content: prompt
|
|
45
|
-
}, {
|
|
46
|
-
role: "user",
|
|
47
|
-
content: ["**File to Audit:**", fileContent].join("\n")
|
|
48
|
-
}]
|
|
49
|
-
});
|
|
50
|
-
require_logger_index.logger.info(`${usage?.totalTokens ?? 0} tokens used in the request`);
|
|
51
|
-
return {
|
|
52
|
-
fileContent: require_utils_extractJSON.extractJson(newContent),
|
|
53
|
-
tokenUsed: usage?.totalTokens ?? 0
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
//#endregion
|
|
58
|
-
exports.aiDefaultOptions = aiDefaultOptions;
|
|
59
|
-
exports.auditDictionary = auditDictionary;
|
|
60
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["readAsset","aiDefaultOptions: AIOptions","Locales","extractJson"],"sources":["../../../../../src/utils/AI/auditDictionary/index.ts"],"sourcesContent":["import { readAsset } from 'utils:asset';\nimport { getLocaleName } from '@intlayer/core';\nimport { type Locale, Locales } from '@intlayer/types';\nimport { logger } from '@logger';\nimport { extractJson } from '@utils/extractJSON';\nimport { generateText } from 'ai';\nimport type { Tag } from '@/types/tag.types';\nimport type { AIConfig, AIOptions } from '../aiSdk';\n\nexport type AuditOptions = {\n fileContent: string;\n filePath?: string;\n locales: Locale[];\n defaultLocale: Locale;\n tags: Tag[];\n aiConfig: AIConfig;\n applicationContext?: string;\n};\n\nexport type AuditFileResultData = {\n fileContent: {\n title: string;\n description: string;\n tags: string[];\n };\n tokenUsed: number;\n};\n\n// The prompt template to send to the AI model\nconst CHAT_GPT_PROMPT = readAsset('./PROMPT.md');\n\nexport const aiDefaultOptions: AIOptions = {\n // Keep default options\n};\n\n/**\n * Format a locale with its name.\n *\n * @param locale - The locale to format.\n * @returns A string in the format \"locale: name\", e.g. \"en: English\".\n */\nconst formatLocaleWithName = (locale: Locale): string => {\n return `${locale}: ${getLocaleName(locale, Locales.ENGLISH)}`;\n};\n\n/**\n * Formats tag instructions for the AI prompt.\n * Creates a string with all available tags and their descriptions.\n *\n * @param tags - The list of tags to format.\n * @returns A formatted string with tag instructions.\n */\nconst formatTagInstructions = (tags: Tag[]): string => {\n if (!tags || tags.length === 0) return '';\n\n // Prepare the tag instructions.\n return [\n `Based on the dictionary content, identify specific tags from the list below that would be relevant:`,\n tags.map(({ key, description }) => `- ${key}: ${description}`).join('\\n\\n'),\n ].join('\\n\\n');\n};\n\n/**\n * Audits a content declaration file by constructing a prompt for AI models.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies.\n */\nexport const auditDictionary = async ({\n fileContent,\n filePath,\n locales,\n defaultLocale,\n tags,\n aiConfig,\n applicationContext,\n}: AuditOptions): Promise<AuditFileResultData | undefined> => {\n const otherLocales = locales.filter((locale) => locale !== defaultLocale);\n\n // Prepare the prompt for AI by replacing placeholders with actual values.\n const prompt = CHAT_GPT_PROMPT.replace(\n '{{defaultLocale}}',\n formatLocaleWithName(defaultLocale)\n )\n .replace(\n '{{otherLocales}}',\n `{${otherLocales.map(formatLocaleWithName).join(', ')}}`\n )\n .replace('{{filePath}}', filePath ?? '')\n .replace('{{applicationContext}}', applicationContext ?? '')\n .replace('{{tagsInstructions}}', formatTagInstructions(tags));\n\n // Use the AI SDK to generate the completion\n const { text: newContent, usage } = await generateText({\n ...aiConfig,\n messages: [\n { role: 'system', content: prompt },\n {\n role: 'user',\n content: ['**File to Audit:**', fileContent].join('\\n'),\n },\n ],\n });\n\n logger.info(`${usage?.totalTokens ?? 0} tokens used in the request`);\n\n return {\n fileContent: extractJson(newContent),\n tokenUsed: usage?.totalTokens ?? 0,\n };\n};\n"],"mappings":";;;;;;;;;AA6BA,MAAM,kBAAkBA,+BAAU,cAAc;AAEhD,MAAaC,mBAA8B,EAE1C;;;;;;;AAQD,MAAM,wBAAwB,WAA2B;AACvD,QAAO,GAAG,OAAO,uCAAkB,QAAQC,yBAAQ,QAAQ;;;;;;;;;AAU7D,MAAM,yBAAyB,SAAwB;AACrD,KAAI,CAAC,QAAQ,KAAK,WAAW,EAAG,QAAO;AAGvC,QAAO,CACL,uGACA,KAAK,KAAK,EAAE,KAAK,kBAAkB,KAAK,IAAI,IAAI,cAAc,CAAC,KAAK,OAAO,CAC5E,CAAC,KAAK,OAAO;;;;;;;AAQhB,MAAa,kBAAkB,OAAO,EACpC,aACA,UACA,SACA,eACA,MACA,UACA,yBAC4D;CAC5D,MAAM,eAAe,QAAQ,QAAQ,WAAW,WAAW,cAAc;CAGzE,MAAM,SAAS,gBAAgB,QAC7B,qBACA,qBAAqB,cAAc,CACpC,CACE,QACC,oBACA,IAAI,aAAa,IAAI,qBAAqB,CAAC,KAAK,KAAK,CAAC,GACvD,CACA,QAAQ,gBAAgB,YAAY,GAAG,CACvC,QAAQ,0BAA0B,sBAAsB,GAAG,CAC3D,QAAQ,wBAAwB,sBAAsB,KAAK,CAAC;CAG/D,MAAM,EAAE,MAAM,YAAY,UAAU,2BAAmB;EACrD,GAAG;EACH,UAAU,CACR;GAAE,MAAM;GAAU,SAAS;GAAQ,EACnC;GACE,MAAM;GACN,SAAS,CAAC,sBAAsB,YAAY,CAAC,KAAK,KAAK;GACxD,CACF;EACF,CAAC;AAEF,6BAAO,KAAK,GAAG,OAAO,eAAe,EAAE,6BAA6B;AAEpE,QAAO;EACL,aAAaC,sCAAY,WAAW;EACpC,WAAW,OAAO,eAAe;EAClC"}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
2
|
-
const require_logger_index = require('../../../logger/index.cjs');
|
|
3
|
-
const require__utils_asset = require('../../../_virtual/_utils_asset.cjs');
|
|
4
|
-
let __intlayer_types = require("@intlayer/types");
|
|
5
|
-
let __intlayer_core = require("@intlayer/core");
|
|
6
|
-
let ai = require("ai");
|
|
7
|
-
|
|
8
|
-
//#region src/utils/AI/auditDictionaryField/index.ts
|
|
9
|
-
const CHAT_GPT_PROMPT = require__utils_asset.readAsset("./PROMPT.md");
|
|
10
|
-
const aiDefaultOptions = {};
|
|
11
|
-
/**
|
|
12
|
-
* Format a locale with its name.
|
|
13
|
-
*
|
|
14
|
-
* @param locale - The locale to format.
|
|
15
|
-
* @returns A string in the format "locale: name", e.g. "en: English".
|
|
16
|
-
*/
|
|
17
|
-
const formatLocaleWithName = (locale) => {
|
|
18
|
-
return `${locale}: ${(0, __intlayer_core.getLocaleName)(locale, __intlayer_types.Locales.ENGLISH)}`;
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Formats tag instructions for the AI prompt.
|
|
22
|
-
*
|
|
23
|
-
* @param tags - Array of tags to format
|
|
24
|
-
* @returns A formatted string with tag instructions
|
|
25
|
-
*/
|
|
26
|
-
const formatTagInstructions = (tags) => {
|
|
27
|
-
if (!tags || tags.length === 0) return "";
|
|
28
|
-
return `Based on the dictionary content, identify specific tags from the list below that would be relevant:
|
|
29
|
-
|
|
30
|
-
${tags.map(({ key, description }) => `- ${key}: ${description}`).join("\n\n")}`;
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Audits a content declaration file by constructing a prompt for AI models.
|
|
34
|
-
* The prompt includes details about the project's locales, file paths of content declarations,
|
|
35
|
-
* and requests for identifying issues or inconsistencies.
|
|
36
|
-
*/
|
|
37
|
-
const auditDictionaryField = async ({ fileContent, applicationContext, locales, keyPath, tags, aiConfig }) => {
|
|
38
|
-
const prompt = CHAT_GPT_PROMPT.replace("{{otherLocales}}", `{${locales.map(formatLocaleWithName).join(", ")}}`).replace("{{keyPath}}", JSON.stringify(keyPath)).replace("{{applicationContext}}", applicationContext ?? "").replace("{{tagsInstructions}}", formatTagInstructions(tags));
|
|
39
|
-
if (!aiConfig) {
|
|
40
|
-
require_logger_index.logger.error("Failed to configure AI model");
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
const { text: newContent, usage } = await (0, ai.generateText)({
|
|
44
|
-
...aiConfig,
|
|
45
|
-
messages: [{
|
|
46
|
-
role: "system",
|
|
47
|
-
content: prompt
|
|
48
|
-
}, {
|
|
49
|
-
role: "user",
|
|
50
|
-
content: ["**File to Audit:**", fileContent].join("\n")
|
|
51
|
-
}]
|
|
52
|
-
});
|
|
53
|
-
require_logger_index.logger.info(`${usage?.totalTokens ?? 0} tokens used in the request`);
|
|
54
|
-
return {
|
|
55
|
-
fileContent: newContent,
|
|
56
|
-
tokenUsed: usage?.totalTokens ?? 0
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
//#endregion
|
|
61
|
-
exports.aiDefaultOptions = aiDefaultOptions;
|
|
62
|
-
exports.auditDictionaryField = auditDictionaryField;
|
|
63
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["readAsset","aiDefaultOptions: AIOptions","Locales"],"sources":["../../../../../src/utils/AI/auditDictionaryField/index.ts"],"sourcesContent":["import { readAsset } from 'utils:asset';\nimport { getLocaleName } from '@intlayer/core';\nimport type { KeyPath } from '@intlayer/types';\nimport { type Locale, Locales } from '@intlayer/types';\nimport { logger } from '@logger';\nimport { generateText } from 'ai';\nimport type { Tag } from '@/types/tag.types';\nimport type { AIConfig, AIOptions } from '../aiSdk';\n\nexport type AuditDictionaryFieldOptions = {\n fileContent: string;\n locales: Locale[];\n keyPath: KeyPath[];\n tags: Tag[];\n aiConfig: AIConfig;\n applicationContext?: string;\n};\n\nexport type AuditDictionaryFieldResultData = {\n fileContent: string;\n tokenUsed: number;\n};\n\n// The prompt template to send to the AI model\nconst CHAT_GPT_PROMPT = readAsset('./PROMPT.md');\n\nexport const aiDefaultOptions: AIOptions = {\n // Keep default options\n};\n\n/**\n * Format a locale with its name.\n *\n * @param locale - The locale to format.\n * @returns A string in the format \"locale: name\", e.g. \"en: English\".\n */\nconst formatLocaleWithName = (locale: Locale): string => {\n return `${locale}: ${getLocaleName(locale, Locales.ENGLISH)}`;\n};\n\n/**\n * Formats tag instructions for the AI prompt.\n *\n * @param tags - Array of tags to format\n * @returns A formatted string with tag instructions\n */\nconst formatTagInstructions = (tags: Tag[]): string => {\n if (!tags || tags.length === 0) {\n return '';\n }\n\n return `Based on the dictionary content, identify specific tags from the list below that would be relevant:\n \n${tags.map(({ key, description }) => `- ${key}: ${description}`).join('\\n\\n')}`;\n};\n\n/**\n * Audits a content declaration file by constructing a prompt for AI models.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies.\n */\nexport const auditDictionaryField = async ({\n fileContent,\n applicationContext,\n locales,\n keyPath,\n tags,\n aiConfig,\n}: AuditDictionaryFieldOptions): Promise<\n AuditDictionaryFieldResultData | undefined\n> => {\n // Prepare the prompt for AI by replacing placeholders with actual values.\n const prompt = CHAT_GPT_PROMPT.replace(\n '{{otherLocales}}',\n `{${locales.map(formatLocaleWithName).join(', ')}}`\n )\n .replace('{{keyPath}}', JSON.stringify(keyPath))\n .replace('{{applicationContext}}', applicationContext ?? '')\n .replace('{{tagsInstructions}}', formatTagInstructions(tags));\n\n if (!aiConfig) {\n logger.error('Failed to configure AI model');\n return undefined;\n }\n\n // Use the AI SDK to generate the completion\n const { text: newContent, usage } = await generateText({\n ...aiConfig,\n messages: [\n { role: 'system', content: prompt },\n {\n role: 'user',\n content: ['**File to Audit:**', fileContent].join('\\n'),\n },\n ],\n });\n\n logger.info(`${usage?.totalTokens ?? 0} tokens used in the request`);\n\n return {\n fileContent: newContent,\n tokenUsed: usage?.totalTokens ?? 0,\n };\n};\n"],"mappings":";;;;;;;;AAwBA,MAAM,kBAAkBA,+BAAU,cAAc;AAEhD,MAAaC,mBAA8B,EAE1C;;;;;;;AAQD,MAAM,wBAAwB,WAA2B;AACvD,QAAO,GAAG,OAAO,uCAAkB,QAAQC,yBAAQ,QAAQ;;;;;;;;AAS7D,MAAM,yBAAyB,SAAwB;AACrD,KAAI,CAAC,QAAQ,KAAK,WAAW,EAC3B,QAAO;AAGT,QAAO;;EAEP,KAAK,KAAK,EAAE,KAAK,kBAAkB,KAAK,IAAI,IAAI,cAAc,CAAC,KAAK,OAAO;;;;;;;AAQ7E,MAAa,uBAAuB,OAAO,EACzC,aACA,oBACA,SACA,SACA,MACA,eAGG;CAEH,MAAM,SAAS,gBAAgB,QAC7B,oBACA,IAAI,QAAQ,IAAI,qBAAqB,CAAC,KAAK,KAAK,CAAC,GAClD,CACE,QAAQ,eAAe,KAAK,UAAU,QAAQ,CAAC,CAC/C,QAAQ,0BAA0B,sBAAsB,GAAG,CAC3D,QAAQ,wBAAwB,sBAAsB,KAAK,CAAC;AAE/D,KAAI,CAAC,UAAU;AACb,8BAAO,MAAM,+BAA+B;AAC5C;;CAIF,MAAM,EAAE,MAAM,YAAY,UAAU,2BAAmB;EACrD,GAAG;EACH,UAAU,CACR;GAAE,MAAM;GAAU,SAAS;GAAQ,EACnC;GACE,MAAM;GACN,SAAS,CAAC,sBAAsB,YAAY,CAAC,KAAK,KAAK;GACxD,CACF;EACF,CAAC;AAEF,6BAAO,KAAK,GAAG,OAAO,eAAe,EAAE,6BAA6B;AAEpE,QAAO;EACL,aAAa;EACb,WAAW,OAAO,eAAe;EAClC"}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
2
|
-
const require_logger_index = require('../../../logger/index.cjs');
|
|
3
|
-
const require__utils_asset = require('../../../_virtual/_utils_asset.cjs');
|
|
4
|
-
const require_utils_extractJSON = require('../../extractJSON.cjs');
|
|
5
|
-
let ai = require("ai");
|
|
6
|
-
|
|
7
|
-
//#region src/utils/AI/auditDictionaryMetadata/index.ts
|
|
8
|
-
const CHAT_GPT_PROMPT = require__utils_asset.readAsset("./PROMPT.md");
|
|
9
|
-
const aiDefaultOptions = {};
|
|
10
|
-
/**
|
|
11
|
-
* Audits a content declaration file by constructing a prompt for AI models.
|
|
12
|
-
* The prompt includes details about the project's locales, file paths of content declarations,
|
|
13
|
-
* and requests for identifying issues or inconsistencies.
|
|
14
|
-
*/
|
|
15
|
-
const auditDictionaryMetadata = async ({ tags, fileContent, applicationContext, aiConfig }) => {
|
|
16
|
-
const prompt = CHAT_GPT_PROMPT.replace("{{tags}}", `${JSON.stringify(tags.map(({ key, description }) => `- ${key}: ${description}`).join("\n\n"), null, 2)}`).replace("{{applicationContext}}", applicationContext ?? "");
|
|
17
|
-
if (!aiConfig) {
|
|
18
|
-
require_logger_index.logger.error("Failed to configure AI model");
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
const { text: newContent, usage } = await (0, ai.generateText)({
|
|
22
|
-
...aiConfig,
|
|
23
|
-
messages: [{
|
|
24
|
-
role: "system",
|
|
25
|
-
content: prompt
|
|
26
|
-
}, {
|
|
27
|
-
role: "user",
|
|
28
|
-
content: [
|
|
29
|
-
"**Content declaration to describe:**",
|
|
30
|
-
"This is the content declaration that you should consider to describe:",
|
|
31
|
-
fileContent
|
|
32
|
-
].join("\n")
|
|
33
|
-
}]
|
|
34
|
-
});
|
|
35
|
-
require_logger_index.logger.info(`${usage?.totalTokens ?? 0} tokens used in the request`);
|
|
36
|
-
return {
|
|
37
|
-
fileContent: require_utils_extractJSON.extractJson(newContent),
|
|
38
|
-
tokenUsed: usage?.totalTokens ?? 0
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
//#endregion
|
|
43
|
-
exports.aiDefaultOptions = aiDefaultOptions;
|
|
44
|
-
exports.auditDictionaryMetadata = auditDictionaryMetadata;
|
|
45
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["readAsset","aiDefaultOptions: AIOptions","extractJson"],"sources":["../../../../../src/utils/AI/auditDictionaryMetadata/index.ts"],"sourcesContent":["import { readAsset } from 'utils:asset';\nimport { logger } from '@logger';\nimport { extractJson } from '@utils/extractJSON';\nimport { generateText } from 'ai';\nimport type { Tag } from '@/types/tag.types';\nimport type { AIConfig, AIOptions } from '../aiSdk';\n\nexport type AuditOptions = {\n fileContent: string;\n tags: Tag[];\n aiConfig: AIConfig;\n applicationContext?: string;\n};\n\nexport type AuditFileResultData = {\n fileContent: {\n title: string;\n description: string;\n tags: string[];\n };\n tokenUsed: number;\n};\n\n// The prompt template to send to AI models\nconst CHAT_GPT_PROMPT = readAsset('./PROMPT.md');\n\nexport const aiDefaultOptions: AIOptions = {\n // Keep default options\n};\n\n/**\n * Audits a content declaration file by constructing a prompt for AI models.\n * The prompt includes details about the project's locales, file paths of content declarations,\n * and requests for identifying issues or inconsistencies.\n */\nexport const auditDictionaryMetadata = async ({\n tags,\n fileContent,\n applicationContext,\n aiConfig,\n}: AuditOptions): Promise<AuditFileResultData | undefined> => {\n // Prepare the prompt for AI by replacing placeholders with actual values.\n const prompt = CHAT_GPT_PROMPT.replace(\n '{{tags}}',\n `${JSON.stringify(\n tags\n .map(({ key, description }) => `- ${key}: ${description}`)\n .join('\\n\\n'),\n null,\n 2\n )}`\n ).replace('{{applicationContext}}', applicationContext ?? '');\n\n if (!aiConfig) {\n logger.error('Failed to configure AI model');\n return undefined;\n }\n\n // Use the AI SDK to generate the completion\n const { text: newContent, usage } = await generateText({\n ...aiConfig,\n messages: [\n { role: 'system', content: prompt },\n {\n role: 'user',\n content: [\n '**Content declaration to describe:**',\n 'This is the content declaration that you should consider to describe:',\n fileContent,\n ].join('\\n'),\n },\n ],\n });\n\n logger.info(`${usage?.totalTokens ?? 0} tokens used in the request`);\n\n return {\n fileContent: extractJson(newContent),\n tokenUsed: usage?.totalTokens ?? 0,\n };\n};\n"],"mappings":";;;;;;;AAwBA,MAAM,kBAAkBA,+BAAU,cAAc;AAEhD,MAAaC,mBAA8B,EAE1C;;;;;;AAOD,MAAa,0BAA0B,OAAO,EAC5C,MACA,aACA,oBACA,eAC4D;CAE5D,MAAM,SAAS,gBAAgB,QAC7B,YACA,GAAG,KAAK,UACN,KACG,KAAK,EAAE,KAAK,kBAAkB,KAAK,IAAI,IAAI,cAAc,CACzD,KAAK,OAAO,EACf,MACA,EACD,GACF,CAAC,QAAQ,0BAA0B,sBAAsB,GAAG;AAE7D,KAAI,CAAC,UAAU;AACb,8BAAO,MAAM,+BAA+B;AAC5C;;CAIF,MAAM,EAAE,MAAM,YAAY,UAAU,2BAAmB;EACrD,GAAG;EACH,UAAU,CACR;GAAE,MAAM;GAAU,SAAS;GAAQ,EACnC;GACE,MAAM;GACN,SAAS;IACP;IACA;IACA;IACD,CAAC,KAAK,KAAK;GACb,CACF;EACF,CAAC;AAEF,6BAAO,KAAK,GAAG,OAAO,eAAe,EAAE,6BAA6B;AAEpE,QAAO;EACL,aAAaC,sCAAY,WAAW;EACpC,WAAW,OAAO,eAAe;EAClC"}
|