ragpack-js 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/dist/index.js ADDED
@@ -0,0 +1,302 @@
1
+ // src/requester.ts
2
+ function createRequester(config) {
3
+ return async function req(path, init) {
4
+ const isFormData = init?.body instanceof FormData;
5
+ const res = await fetch(`${config.baseUrl}/api/v1${path}`, {
6
+ ...init,
7
+ headers: {
8
+ ...!isFormData && { "Content-Type": "application/json" },
9
+ Authorization: `Bearer ${config.apiKey}`,
10
+ ...init?.headers
11
+ }
12
+ });
13
+ if (res.status === 204) return void 0;
14
+ const body = await res.json();
15
+ if (!res.ok) throw new RagPackError(res.status, body.error ?? res.statusText);
16
+ return body;
17
+ };
18
+ }
19
+ var RagPackError = class extends Error {
20
+ constructor(status, message) {
21
+ super(message);
22
+ this.status = status;
23
+ this.name = "RagPackError";
24
+ }
25
+ };
26
+
27
+ // src/resources/jobs.ts
28
+ var JobsResource = class {
29
+ constructor(req, slug) {
30
+ this.req = req;
31
+ this.slug = slug;
32
+ }
33
+ /** List ingestion jobs for this collection, or all jobs if no collection is scoped. */
34
+ async list() {
35
+ const path = this.slug ? `/collections/${this.slug}/jobs` : "/jobs";
36
+ const r = await this.req(path);
37
+ return r.jobs;
38
+ }
39
+ /**
40
+ * Get a single ingestion job by ID.
41
+ * @param id - The job ID.
42
+ */
43
+ async get(id) {
44
+ const r = await this.req(`/collections/${this.slug}/jobs/${id}`);
45
+ return r.job;
46
+ }
47
+ /**
48
+ * Poll until the job reaches a terminal state (`complete` or `failed`).
49
+ * Throws {@link RagPackError} if the job fails or the timeout is exceeded.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * const job = await collection.ingest(file);
54
+ * await collection.jobs.waitUntilComplete(job.id);
55
+ * console.log("ingestion done, ready to search");
56
+ * ```
57
+ */
58
+ async waitUntilComplete(id, options = {}) {
59
+ const { pollIntervalMs = 1500, timeoutMs = 3e5 } = options;
60
+ const deadline = Date.now() + timeoutMs;
61
+ while (Date.now() < deadline) {
62
+ const job = await this.get(id);
63
+ if (job.status === "complete") return job;
64
+ if (job.status === "failed") {
65
+ throw new RagPackError(422, job.error ?? "ingestion job failed");
66
+ }
67
+ await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
68
+ }
69
+ throw new RagPackError(408, `job ${id} did not complete within ${timeoutMs}ms`);
70
+ }
71
+ };
72
+
73
+ // src/resources/documents.ts
74
+ var DocumentsResource = class {
75
+ constructor(req, slug) {
76
+ this.req = req;
77
+ this.slug = slug;
78
+ }
79
+ /**
80
+ * List documents in this collection.
81
+ * @param options.limit - Max number of documents to return. Defaults to 50.
82
+ * @param options.offset - Pagination offset. Defaults to 0.
83
+ */
84
+ async list(options) {
85
+ const params = new URLSearchParams({
86
+ limit: String(options?.limit ?? 50),
87
+ offset: String(options?.offset ?? 0)
88
+ });
89
+ const r = await this.req(
90
+ `/collections/${this.slug}/documents?${params}`
91
+ );
92
+ return r.documents;
93
+ }
94
+ /**
95
+ * Delete a document and all its chunks from this collection.
96
+ * @param id - The document ID.
97
+ */
98
+ delete(id) {
99
+ return this.req(`/collections/${this.slug}/documents/${id}`, {
100
+ method: "DELETE"
101
+ });
102
+ }
103
+ };
104
+
105
+ // src/resources/query.ts
106
+ var QueryResource = class {
107
+ constructor(req, slug) {
108
+ this.req = req;
109
+ this.slug = slug;
110
+ }
111
+ /**
112
+ * Find semantically similar content within this collection.
113
+ * @returns Ranked results with similarity scores and source chunks.
114
+ */
115
+ async findSimilar(options) {
116
+ const r = await this.req(
117
+ `/collections/${this.slug}/query`,
118
+ {
119
+ method: "POST",
120
+ body: JSON.stringify({ query: options.query, top_k: options.topK ?? 5 })
121
+ }
122
+ );
123
+ return r.results;
124
+ }
125
+ };
126
+
127
+ // src/collection-client.ts
128
+ var CollectionClient = class {
129
+ constructor(slug, req) {
130
+ this.slug = slug;
131
+ this._req = req;
132
+ this.jobs = new JobsResource(req, slug);
133
+ this.documents = new DocumentsResource(req, slug);
134
+ this._query = new QueryResource(req, slug);
135
+ }
136
+ ingest(input, filename) {
137
+ if (input instanceof Blob) {
138
+ const form = new FormData();
139
+ form.append("file", input, filename);
140
+ return this._req(`/collections/${this.slug}/ingest`, {
141
+ method: "POST",
142
+ body: form
143
+ });
144
+ }
145
+ return this._req(`/collections/${this.slug}/ingest`, {
146
+ method: "POST",
147
+ body: JSON.stringify({ file_uri: input.uri, mime_type: input.mimeType })
148
+ });
149
+ }
150
+ /**
151
+ * Find semantically similar content in this collection.
152
+ * @example
153
+ * ```ts
154
+ * const results = await collection.findSimilar({ query: "what is RagPack?", topK: 10 });
155
+ * ```
156
+ */
157
+ findSimilar(options) {
158
+ return this._query.findSimilar(options);
159
+ }
160
+ /**
161
+ * Full RAG pipeline: retrieve relevant chunks, build context, expand the
162
+ * prompt template, and call the configured LLM — all server-side.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * const { answer, chunks } = await collection.rag({
167
+ * prompt: "basic-rag",
168
+ * query: "How do I reset my password?",
169
+ * });
170
+ * ```
171
+ */
172
+ rag(options) {
173
+ return this._req(`/collections/${this.slug}/rag`, {
174
+ method: "POST",
175
+ body: JSON.stringify({
176
+ query: options.query,
177
+ top_k: options.topK ?? 5,
178
+ ...options.promptSlug ? { prompt_slug: options.promptSlug } : {},
179
+ ...options.model ? { model: options.model } : {},
180
+ ...options.minSimilarity != null ? { min_similarity: options.minSimilarity } : {}
181
+ })
182
+ });
183
+ }
184
+ };
185
+
186
+ // src/resources/collections.ts
187
+ var CollectionsResource = class {
188
+ constructor(req) {
189
+ this.req = req;
190
+ }
191
+ /** List all collections. */
192
+ async list() {
193
+ const r = await this.req("/collections");
194
+ return r.collections;
195
+ }
196
+ /**
197
+ * Create a new collection and return a scoped client for it.
198
+ * @param name - Display name for the collection.
199
+ * @param options.embedModel - Embedding model to use. Defaults to the server's configured provider.
200
+ * @param options.chunkConfig - Override chunking behaviour for this collection.
201
+ * Omit to use server defaults.
202
+ *
203
+ * @example
204
+ * ```ts
205
+ * const collection = await client.collections.create("my-docs");
206
+ * await collection.ingest(file);
207
+ * ```
208
+ */
209
+ async create(name, options) {
210
+ const col = await this.req("/collections", {
211
+ method: "POST",
212
+ body: JSON.stringify({
213
+ name,
214
+ embed_model: options?.embedModel,
215
+ chunk_config: options?.chunkConfig
216
+ })
217
+ });
218
+ return new CollectionClient(col.slug, this.req);
219
+ }
220
+ /**
221
+ * Get a collection by its slug.
222
+ * @param slug - The collection's URL-safe identifier.
223
+ */
224
+ get(slug) {
225
+ return this.req(`/collections/${slug}`);
226
+ }
227
+ /**
228
+ * Delete a collection and all its documents.
229
+ * @param slug - The collection's URL-safe identifier.
230
+ */
231
+ delete(slug) {
232
+ return this.req(`/collections/${slug}`, { method: "DELETE" });
233
+ }
234
+ };
235
+
236
+ // src/resources/prompts.ts
237
+ var PromptsResource = class {
238
+ constructor(req) {
239
+ this.req = req;
240
+ }
241
+ /** List all prompt templates (built-in system prompts first, then custom). */
242
+ async list() {
243
+ const r = await this.req("/prompts");
244
+ return [...r.system ?? [], ...r.user ?? []];
245
+ }
246
+ /** Fetch a prompt template by slug. */
247
+ get(slug) {
248
+ return this.req(`/prompts/${slug}`);
249
+ }
250
+ };
251
+
252
+ // src/resources/llms.ts
253
+ var LLMsResource = class {
254
+ constructor(req) {
255
+ this.req = req;
256
+ }
257
+ /** List all configured LLM models and the server default. */
258
+ list() {
259
+ return this.req("/llms");
260
+ }
261
+ };
262
+
263
+ // src/resources/embedders.ts
264
+ var EmbeddersResource = class {
265
+ constructor(req) {
266
+ this.req = req;
267
+ }
268
+ /** List all configured embedding models and the server default. */
269
+ list() {
270
+ return this.req("/embedders");
271
+ }
272
+ };
273
+
274
+ // src/index.ts
275
+ var RagPack = class {
276
+ constructor(config) {
277
+ this._req = createRequester(config);
278
+ this.collections = new CollectionsResource(this._req);
279
+ this.prompts = new PromptsResource(this._req);
280
+ this.llms = new LLMsResource(this._req);
281
+ this.embedders = new EmbeddersResource(this._req);
282
+ }
283
+ /**
284
+ * Scope all operations to a specific collection.
285
+ * @param slug - The collection's URL-safe identifier.
286
+ *
287
+ * @example
288
+ * ```ts
289
+ * const collection = client.collection("my-docs");
290
+ * await collection.ingest(file);
291
+ * await collection.findSimilar({ query: "..." });
292
+ * await collection.jobs.list();
293
+ * ```
294
+ */
295
+ collection(slug) {
296
+ return new CollectionClient(slug, this._req);
297
+ }
298
+ };
299
+ export {
300
+ RagPack,
301
+ RagPackError
302
+ };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "ragpack-js",
3
+ "version": "0.1.0",
4
+ "description": "TypeScript client for the Ragpack self-hosted RAG engine",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
20
+ "dev": "tsup src/index.ts --format esm,cjs --dts --watch",
21
+ "lint": "eslint src",
22
+ "typecheck": "tsc --noEmit",
23
+ "prepublishOnly": "npm run lint && npm run typecheck && npm run build"
24
+ },
25
+ "devDependencies": {
26
+ "eslint": "^9.39.4",
27
+ "tsup": "^8.0.0",
28
+ "typescript": "^5.0.0",
29
+ "typescript-eslint": "^8.62.0"
30
+ },
31
+ "engines": {
32
+ "node": ">=18"
33
+ },
34
+ "type": "module",
35
+ "license": "MIT",
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "https://github.com/eozsahin1993/ragpack.git",
39
+ "directory": "sdks/js"
40
+ }
41
+ }