safechunk 0.1.0 → 0.2.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.
@@ -0,0 +1,66 @@
1
+ import { Format, Target } from './index.cjs';
2
+
3
+ /** Zero-dependency adapters for LangChain.js, LlamaIndex.TS, Telegram, and Discord. */
4
+
5
+ interface DocumentLike {
6
+ pageContent: string;
7
+ metadata: Record<string, unknown>;
8
+ }
9
+ interface NodeLike {
10
+ text: string;
11
+ metadata: Record<string, unknown>;
12
+ }
13
+ interface SplitterAdapterOptions {
14
+ chunkSize?: number;
15
+ chunkOverlap?: number;
16
+ target?: Target;
17
+ format?: Format;
18
+ addChunkMetadata?: boolean;
19
+ }
20
+ /**
21
+ * LangChain.js-compatible text splitter backed by `safechunk`.
22
+ * Structurally compatible with `@langchain/core/documents` (`Document`).
23
+ */
24
+ declare class SafeChunkTextSplitter {
25
+ readonly chunkSize?: number;
26
+ readonly chunkOverlap: number;
27
+ readonly target?: Target;
28
+ readonly format: Format;
29
+ readonly addChunkMetadata: boolean;
30
+ constructor(options?: SplitterAdapterOptions);
31
+ splitText(text: string): string[];
32
+ createDocuments(texts: readonly string[], metadatas?: readonly Record<string, unknown>[]): DocumentLike[];
33
+ splitDocuments(documents: Iterable<DocumentLike>): DocumentLike[];
34
+ }
35
+ /**
36
+ * LlamaIndex.TS-compatible node parser backed by `safechunk`.
37
+ */
38
+ declare class SafeChunkNodeParser {
39
+ readonly chunkSize?: number;
40
+ readonly chunkOverlap: number;
41
+ readonly target?: Target;
42
+ readonly format: Format;
43
+ readonly addChunkMetadata: boolean;
44
+ constructor(options?: SplitterAdapterOptions);
45
+ splitText(text: string): string[];
46
+ getNodesFromDocuments(documents: Iterable<{
47
+ text?: string;
48
+ getText?: () => string;
49
+ metadata?: Record<string, unknown>;
50
+ }>): NodeLike[];
51
+ }
52
+ interface BotChunkOptions {
53
+ format?: Format;
54
+ maxSize?: number;
55
+ overlap?: number;
56
+ }
57
+ /** Split text safely for Telegram's 4096-character limit (HTML or Markdown). */
58
+ declare function chunkTelegram(text: string, options?: BotChunkOptions): string[];
59
+ /** Send chunked Telegram messages via grammY (`ctx.reply`), Telegraf, or node-telegram-bot-api. */
60
+ declare function sendTelegramChunks<T>(sendFn: (part: string) => Promise<T> | T, text: string, options?: BotChunkOptions): Promise<T[]>;
61
+ /** Split text safely for Discord's 2000-character limit preserving fenced code blocks. */
62
+ declare function chunkDiscord(text: string, options?: BotChunkOptions): string[];
63
+ /** Send chunked Discord messages via discord.js (`channel.send`, `message.reply`, `interaction.followUp`). */
64
+ declare function sendDiscordChunks<T>(sendFn: (part: string) => Promise<T> | T, text: string, options?: BotChunkOptions): Promise<T[]>;
65
+
66
+ export { type BotChunkOptions, type DocumentLike, type NodeLike, SafeChunkNodeParser, SafeChunkTextSplitter, type SplitterAdapterOptions, chunkDiscord, chunkTelegram, sendDiscordChunks, sendTelegramChunks };
@@ -0,0 +1,66 @@
1
+ import { Format, Target } from './index.js';
2
+
3
+ /** Zero-dependency adapters for LangChain.js, LlamaIndex.TS, Telegram, and Discord. */
4
+
5
+ interface DocumentLike {
6
+ pageContent: string;
7
+ metadata: Record<string, unknown>;
8
+ }
9
+ interface NodeLike {
10
+ text: string;
11
+ metadata: Record<string, unknown>;
12
+ }
13
+ interface SplitterAdapterOptions {
14
+ chunkSize?: number;
15
+ chunkOverlap?: number;
16
+ target?: Target;
17
+ format?: Format;
18
+ addChunkMetadata?: boolean;
19
+ }
20
+ /**
21
+ * LangChain.js-compatible text splitter backed by `safechunk`.
22
+ * Structurally compatible with `@langchain/core/documents` (`Document`).
23
+ */
24
+ declare class SafeChunkTextSplitter {
25
+ readonly chunkSize?: number;
26
+ readonly chunkOverlap: number;
27
+ readonly target?: Target;
28
+ readonly format: Format;
29
+ readonly addChunkMetadata: boolean;
30
+ constructor(options?: SplitterAdapterOptions);
31
+ splitText(text: string): string[];
32
+ createDocuments(texts: readonly string[], metadatas?: readonly Record<string, unknown>[]): DocumentLike[];
33
+ splitDocuments(documents: Iterable<DocumentLike>): DocumentLike[];
34
+ }
35
+ /**
36
+ * LlamaIndex.TS-compatible node parser backed by `safechunk`.
37
+ */
38
+ declare class SafeChunkNodeParser {
39
+ readonly chunkSize?: number;
40
+ readonly chunkOverlap: number;
41
+ readonly target?: Target;
42
+ readonly format: Format;
43
+ readonly addChunkMetadata: boolean;
44
+ constructor(options?: SplitterAdapterOptions);
45
+ splitText(text: string): string[];
46
+ getNodesFromDocuments(documents: Iterable<{
47
+ text?: string;
48
+ getText?: () => string;
49
+ metadata?: Record<string, unknown>;
50
+ }>): NodeLike[];
51
+ }
52
+ interface BotChunkOptions {
53
+ format?: Format;
54
+ maxSize?: number;
55
+ overlap?: number;
56
+ }
57
+ /** Split text safely for Telegram's 4096-character limit (HTML or Markdown). */
58
+ declare function chunkTelegram(text: string, options?: BotChunkOptions): string[];
59
+ /** Send chunked Telegram messages via grammY (`ctx.reply`), Telegraf, or node-telegram-bot-api. */
60
+ declare function sendTelegramChunks<T>(sendFn: (part: string) => Promise<T> | T, text: string, options?: BotChunkOptions): Promise<T[]>;
61
+ /** Split text safely for Discord's 2000-character limit preserving fenced code blocks. */
62
+ declare function chunkDiscord(text: string, options?: BotChunkOptions): string[];
63
+ /** Send chunked Discord messages via discord.js (`channel.send`, `message.reply`, `interaction.followUp`). */
64
+ declare function sendDiscordChunks<T>(sendFn: (part: string) => Promise<T> | T, text: string, options?: BotChunkOptions): Promise<T[]>;
65
+
66
+ export { type BotChunkOptions, type DocumentLike, type NodeLike, SafeChunkNodeParser, SafeChunkTextSplitter, type SplitterAdapterOptions, chunkDiscord, chunkTelegram, sendDiscordChunks, sendTelegramChunks };
@@ -0,0 +1,121 @@
1
+ import {
2
+ TARGET_LIMITS,
3
+ chunk
4
+ } from "./chunk-FPS7R6OR.js";
5
+
6
+ // src/adapters.ts
7
+ var SafeChunkTextSplitter = class {
8
+ constructor(options = {}) {
9
+ this.chunkSize = options.chunkSize;
10
+ this.chunkOverlap = options.chunkOverlap ?? 0;
11
+ this.target = options.target;
12
+ this.format = options.format ?? "markdown";
13
+ this.addChunkMetadata = options.addChunkMetadata ?? true;
14
+ }
15
+ splitText(text) {
16
+ return chunk(text, {
17
+ maxSize: this.chunkSize,
18
+ target: this.target,
19
+ format: this.format,
20
+ overlap: this.chunkOverlap
21
+ });
22
+ }
23
+ createDocuments(texts, metadatas) {
24
+ if (metadatas !== void 0 && metadatas.length !== texts.length) {
25
+ throw new RangeError("metadatas must have the same length as texts");
26
+ }
27
+ const docs = [];
28
+ for (let i = 0; i < texts.length; i += 1) {
29
+ const baseMeta = metadatas?.[i] ?? {};
30
+ const parts = this.splitText(texts[i]);
31
+ const total = parts.length;
32
+ for (let idx = 0; idx < total; idx += 1) {
33
+ const metadata = this.addChunkMetadata ? { ...baseMeta, chunk_index: idx, chunk_total: total } : { ...baseMeta };
34
+ docs.push({ pageContent: parts[idx], metadata });
35
+ }
36
+ }
37
+ return docs;
38
+ }
39
+ splitDocuments(documents) {
40
+ const out = [];
41
+ for (const doc of documents) {
42
+ const baseMeta = doc.metadata ?? {};
43
+ const parts = this.splitText(doc.pageContent);
44
+ const total = parts.length;
45
+ for (let idx = 0; idx < total; idx += 1) {
46
+ const metadata = this.addChunkMetadata ? { ...baseMeta, chunk_index: idx, chunk_total: total } : { ...baseMeta };
47
+ out.push({ pageContent: parts[idx], metadata });
48
+ }
49
+ }
50
+ return out;
51
+ }
52
+ };
53
+ var SafeChunkNodeParser = class {
54
+ constructor(options = {}) {
55
+ this.chunkSize = options.chunkSize;
56
+ this.chunkOverlap = options.chunkOverlap ?? 0;
57
+ this.target = options.target;
58
+ this.format = options.format ?? "markdown";
59
+ this.addChunkMetadata = options.addChunkMetadata ?? true;
60
+ }
61
+ splitText(text) {
62
+ return chunk(text, {
63
+ maxSize: this.chunkSize,
64
+ target: this.target,
65
+ format: this.format,
66
+ overlap: this.chunkOverlap
67
+ });
68
+ }
69
+ getNodesFromDocuments(documents) {
70
+ const nodes = [];
71
+ for (const doc of documents) {
72
+ const rawText = typeof doc.getText === "function" ? doc.getText() : doc.text ?? "";
73
+ const baseMeta = doc.metadata ?? {};
74
+ const parts = this.splitText(rawText);
75
+ const total = parts.length;
76
+ for (let idx = 0; idx < total; idx += 1) {
77
+ const metadata = this.addChunkMetadata ? { ...baseMeta, chunk_index: idx, chunk_total: total } : { ...baseMeta };
78
+ nodes.push({ text: parts[idx], metadata });
79
+ }
80
+ }
81
+ return nodes;
82
+ }
83
+ };
84
+ function chunkTelegram(text, options = {}) {
85
+ return chunk(text, {
86
+ maxSize: options.maxSize ?? TARGET_LIMITS.telegram,
87
+ target: "telegram",
88
+ format: options.format ?? "html",
89
+ overlap: options.overlap ?? 0
90
+ });
91
+ }
92
+ async function sendTelegramChunks(sendFn, text, options = {}) {
93
+ const results = [];
94
+ for (const part of chunkTelegram(text, options)) {
95
+ results.push(await sendFn(part));
96
+ }
97
+ return results;
98
+ }
99
+ function chunkDiscord(text, options = {}) {
100
+ return chunk(text, {
101
+ maxSize: options.maxSize ?? TARGET_LIMITS.discord,
102
+ target: "discord",
103
+ format: options.format ?? "markdown",
104
+ overlap: options.overlap ?? 0
105
+ });
106
+ }
107
+ async function sendDiscordChunks(sendFn, text, options = {}) {
108
+ const results = [];
109
+ for (const part of chunkDiscord(text, options)) {
110
+ results.push(await sendFn(part));
111
+ }
112
+ return results;
113
+ }
114
+ export {
115
+ SafeChunkNodeParser,
116
+ SafeChunkTextSplitter,
117
+ chunkDiscord,
118
+ chunkTelegram,
119
+ sendDiscordChunks,
120
+ sendTelegramChunks
121
+ };