@grec0/mcp-mantis 0.3.2

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/mantis.js ADDED
@@ -0,0 +1,296 @@
1
+ import { CANONICAL_ENUM_NAMES, DEFAULT_RESOLVED_STATUS_THRESHOLD, ENUM_CONFIG_OPTIONS, ENUM_GROUPS, } from './constants.js';
2
+ /** Error carrying the HTTP status and MantisBT's structured error payload. */
3
+ export class MantisApiError extends Error {
4
+ status;
5
+ code;
6
+ localized;
7
+ constructor(status, message, code, localized) {
8
+ super(message);
9
+ this.status = status;
10
+ this.code = code;
11
+ this.localized = localized;
12
+ this.name = 'MantisApiError';
13
+ }
14
+ }
15
+ /**
16
+ * Typed wrapper over the MantisBT REST API (v2.27.x). Auth is an API token in the
17
+ * `Authorization` header. The base URL includes `index.php` (URL rewriting is off
18
+ * on the QA host). SOAP is intentionally unused (disabled server-side).
19
+ */
20
+ export class MantisRestClient {
21
+ cfg;
22
+ enumsCache;
23
+ thresholdCache;
24
+ webBase;
25
+ constructor(cfg) {
26
+ this.cfg = cfg;
27
+ this.webBase = deriveWebBaseUrl(cfg.baseUrl);
28
+ }
29
+ getStatus() {
30
+ return {
31
+ baseUrl: this.cfg.baseUrl,
32
+ webBaseUrl: this.webBase,
33
+ tokenConfigured: this.cfg.token.length > 0,
34
+ readonly: this.cfg.readonly,
35
+ };
36
+ }
37
+ get isReadonly() {
38
+ return this.cfg.readonly;
39
+ }
40
+ get webBaseUrl() {
41
+ return this.webBase;
42
+ }
43
+ get defaultPageSize() {
44
+ return this.cfg.defaultPageSize;
45
+ }
46
+ get maxPageSize() {
47
+ return this.cfg.maxPageSize;
48
+ }
49
+ /** Clamps a requested page size to [1, maxPageSize], defaulting when omitted. */
50
+ clampPageSize(requested) {
51
+ return Math.min(requested ?? this.cfg.defaultPageSize, this.cfg.maxPageSize);
52
+ }
53
+ buildIssueViewUrl(issueId) {
54
+ return `${this.webBase}/view.php?id=${issueId}`;
55
+ }
56
+ buildNoteViewUrl(issueId, noteId) {
57
+ return `${this.webBase}/view.php?id=${issueId}#bugnote${noteId}`;
58
+ }
59
+ // --- Transport verbs -----------------------------------------------------
60
+ get(path, query) {
61
+ return this.request('GET', path, toParams(query));
62
+ }
63
+ post(path, body) {
64
+ this.assertWritable();
65
+ return this.request('POST', path, undefined, body);
66
+ }
67
+ patch(path, body) {
68
+ this.assertWritable();
69
+ return this.request('PATCH', path, undefined, body);
70
+ }
71
+ delete(path) {
72
+ this.assertWritable();
73
+ return this.request('DELETE', path);
74
+ }
75
+ // --- Metadata (cached) ---------------------------------------------------
76
+ getConfig(options) {
77
+ const params = new URLSearchParams();
78
+ for (const option of options)
79
+ params.append('option[]', option);
80
+ return this.request('GET', '/config', params);
81
+ }
82
+ /** Live enums keyed by group, each as [{id,name,label}]. Cached for metadataTtlMs. */
83
+ async getEnums(force = false) {
84
+ if (!force && this.enumsCache && Date.now() - this.enumsCache.at < this.cfg.metadataTtlMs) {
85
+ return this.enumsCache.value;
86
+ }
87
+ const raw = await this.getConfig(ENUM_CONFIG_OPTIONS);
88
+ const configs = Array.isArray(raw.configs) ? raw.configs : [];
89
+ const map = {
90
+ status: [],
91
+ priority: [],
92
+ severity: [],
93
+ resolution: [],
94
+ reproducibility: [],
95
+ };
96
+ for (const entry of configs) {
97
+ const option = String(entry.option ?? '');
98
+ const group = ENUM_GROUPS.find((g) => option === `${g}_enum_string`);
99
+ if (group && Array.isArray(entry.value)) {
100
+ map[group] = entry.value.map((v) => ({
101
+ id: Number(v.id),
102
+ name: String(v.name ?? ''),
103
+ label: v.label === undefined ? undefined : String(v.label),
104
+ }));
105
+ }
106
+ }
107
+ this.enumsCache = { at: Date.now(), value: map };
108
+ return map;
109
+ }
110
+ /** Instance-configured status id at/above which an issue counts as resolved. */
111
+ async getResolvedStatusThreshold() {
112
+ if (this.thresholdCache && Date.now() - this.thresholdCache.at < this.cfg.metadataTtlMs) {
113
+ return this.thresholdCache.value;
114
+ }
115
+ let threshold = DEFAULT_RESOLVED_STATUS_THRESHOLD;
116
+ try {
117
+ const raw = await this.getConfig(['bug_resolved_status_threshold']);
118
+ const configs = Array.isArray(raw.configs) ? raw.configs : [];
119
+ const value = configs[0]?.value;
120
+ const parsed = Number(value);
121
+ if (Number.isInteger(parsed) && parsed > 0)
122
+ threshold = parsed;
123
+ }
124
+ catch {
125
+ // fall back to the default threshold
126
+ }
127
+ this.thresholdCache = { at: Date.now(), value: threshold };
128
+ return threshold;
129
+ }
130
+ /**
131
+ * Resolves an enum value (numeric id, localized name, or canonical label) to a
132
+ * numeric id. Throws MantisApiError with the valid options when it cannot match.
133
+ */
134
+ async resolveEnumId(group, value) {
135
+ if (typeof value === 'number' && Number.isInteger(value))
136
+ return value;
137
+ const asString = String(value).trim();
138
+ if (/^\d+$/.test(asString))
139
+ return Number(asString);
140
+ const lower = asString.toLowerCase();
141
+ const enums = await this.getEnums();
142
+ const entries = enums[group] ?? [];
143
+ const match = entries.find((e) => e.name.toLowerCase() === lower || (e.label ?? '').toLowerCase() === lower);
144
+ if (match)
145
+ return match.id;
146
+ // Fallback: canonical English name, but only if that id actually exists here.
147
+ const canonical = CANONICAL_ENUM_NAMES[group];
148
+ const canonicalId = Object.entries(canonical).find(([, n]) => n.toLowerCase() === lower)?.[0];
149
+ if (canonicalId !== undefined && entries.some((e) => e.id === Number(canonicalId))) {
150
+ return Number(canonicalId);
151
+ }
152
+ const valid = entries
153
+ .map((e) => (e.label && e.label !== e.name ? `${e.name}/${e.label}` : e.name))
154
+ .join(', ');
155
+ throw new MantisApiError(400, `Invalid ${group} "${asString}". Valid values: ${valid || '(none — call mantis_get_enums)'}.`);
156
+ }
157
+ // --- Domain reads --------------------------------------------------------
158
+ getMe() {
159
+ return this.get('/users/me');
160
+ }
161
+ getProjects() {
162
+ return this.get('/projects');
163
+ }
164
+ getProject(projectId) {
165
+ return this.get(`/projects/${encodeId(projectId)}`);
166
+ }
167
+ listFilters(projectId) {
168
+ return this.get('/filters', projectId === undefined ? undefined : { project_id: projectId });
169
+ }
170
+ async getProjectUsers(projectId) {
171
+ const raw = await this.get(`/projects/${encodeId(projectId)}/users`, {
172
+ page_size: 200,
173
+ });
174
+ return Array.isArray(raw.users) ? raw.users : [];
175
+ }
176
+ /** Resolves a handler login name (or numeric id) to a user id within a project. */
177
+ async resolveHandlerId(projectId, nameOrId) {
178
+ if (typeof nameOrId === 'number' && Number.isInteger(nameOrId))
179
+ return nameOrId;
180
+ const asString = String(nameOrId).trim();
181
+ if (/^\d+$/.test(asString))
182
+ return Number(asString);
183
+ const users = await this.getProjectUsers(projectId);
184
+ const lower = asString.toLowerCase();
185
+ const user = users.find((u) => u.name.toLowerCase() === lower || (u.real_name ?? '').toLowerCase() === lower);
186
+ if (!user) {
187
+ const names = users.map((u) => u.name).join(', ');
188
+ throw new MantisApiError(404, `User "${asString}" not found in project ${projectId}. Available: ${names || 'none'}.`);
189
+ }
190
+ return user.id;
191
+ }
192
+ /** Downloads an issue attachment; content arrives base64-encoded in the JSON body. */
193
+ async getAttachment(issueId, fileId) {
194
+ const body = await this.get(`/issues/${encodeId(issueId)}/files/${encodeId(fileId)}`);
195
+ const files = Array.isArray(body.files) ? body.files : [];
196
+ const file = files[0];
197
+ if (file === undefined) {
198
+ throw new MantisApiError(404, `Attachment ${fileId} not found on issue ${issueId}.`);
199
+ }
200
+ const contentType = String(file.content_type ?? 'application/octet-stream');
201
+ const base64 = typeof file.content === 'string' ? file.content : '';
202
+ if (base64.length === 0) {
203
+ throw new MantisApiError(502, `Attachment ${fileId} returned no content.`);
204
+ }
205
+ return {
206
+ id: Number(file.id ?? fileId),
207
+ filename: String(file.filename ?? `attachment-${fileId}`),
208
+ size: Number(file.size ?? 0),
209
+ contentType,
210
+ mediaType: stripContentTypeParams(contentType),
211
+ base64,
212
+ };
213
+ }
214
+ // --- Internals -----------------------------------------------------------
215
+ assertWritable() {
216
+ if (this.cfg.readonly) {
217
+ throw new MantisApiError(403, 'Server is in read-only mode (MANTIS_READONLY=true); write operations are disabled.');
218
+ }
219
+ }
220
+ async request(method, path, query, body) {
221
+ if (this.cfg.token.length === 0) {
222
+ throw new MantisApiError(401, 'MANTIS_TOKEN is not configured; cannot call the Mantis API.');
223
+ }
224
+ const qs = query && [...query.keys()].length > 0 ? `?${query.toString()}` : '';
225
+ const url = `${this.cfg.baseUrl}${path}${qs}`;
226
+ const headers = {
227
+ Authorization: this.cfg.token,
228
+ Accept: 'application/json',
229
+ };
230
+ if (body !== undefined)
231
+ headers['Content-Type'] = 'application/json';
232
+ let response;
233
+ try {
234
+ response = await fetch(url, {
235
+ method,
236
+ headers,
237
+ body: body === undefined ? undefined : JSON.stringify(body),
238
+ signal: AbortSignal.timeout(this.cfg.timeoutMs),
239
+ });
240
+ }
241
+ catch (error) {
242
+ const reason = error instanceof Error ? error.message : String(error);
243
+ const detail = error instanceof Error && error.name === 'TimeoutError'
244
+ ? `timed out after ${this.cfg.timeoutMs}ms`
245
+ : reason;
246
+ throw new MantisApiError(0, `Network error calling ${method} ${path}: ${detail}`);
247
+ }
248
+ const text = await response.text();
249
+ if (!response.ok) {
250
+ throw toApiError(response.status, text);
251
+ }
252
+ if (text.length === 0)
253
+ return {};
254
+ try {
255
+ return JSON.parse(text);
256
+ }
257
+ catch {
258
+ throw new MantisApiError(502, `Expected JSON from ${method} ${path} but received: ${text.slice(0, 200)}`);
259
+ }
260
+ }
261
+ }
262
+ function toParams(query) {
263
+ if (!query)
264
+ return undefined;
265
+ const params = new URLSearchParams();
266
+ for (const [key, value] of Object.entries(query)) {
267
+ if (value !== undefined)
268
+ params.set(key, String(value));
269
+ }
270
+ return params;
271
+ }
272
+ function deriveWebBaseUrl(baseUrl) {
273
+ // http://host/mantis/api/rest/index.php -> http://host/mantis
274
+ return baseUrl.replace(/\/api\/rest(\/index\.php)?\/?$/i, '');
275
+ }
276
+ function encodeId(id) {
277
+ if (!Number.isInteger(id) || id < 0) {
278
+ throw new MantisApiError(400, `Invalid id: ${id}`);
279
+ }
280
+ return String(id);
281
+ }
282
+ function stripContentTypeParams(contentType) {
283
+ const semicolon = contentType.indexOf(';');
284
+ return (semicolon === -1 ? contentType : contentType.slice(0, semicolon)).trim();
285
+ }
286
+ function toApiError(status, rawBody) {
287
+ try {
288
+ const parsed = JSON.parse(rawBody);
289
+ const message = parsed.message ?? parsed.localized ?? `HTTP ${status}`;
290
+ return new MantisApiError(status, message, parsed.code, parsed.localized);
291
+ }
292
+ catch {
293
+ const snippet = rawBody.trim().slice(0, 200);
294
+ return new MantisApiError(status, snippet.length > 0 ? snippet : `HTTP ${status}`);
295
+ }
296
+ }
package/dist/mcp.js ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env node
2
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
3
+ import { config } from './config.js';
4
+ import { MantisRestClient } from './mantis.js';
5
+ import { createMcpServer } from './server.js';
6
+ // stdio transport owns stdout for the MCP protocol; only ever log to stderr.
7
+ const client = new MantisRestClient(config.mantis);
8
+ const server = createMcpServer(client, config.search);
9
+ const transport = new StdioServerTransport();
10
+ await server.connect(transport);
11
+ const status = client.getStatus();
12
+ console.error(`[mantis-mcp] connected | baseUrl=${status.baseUrl} | token=${status.tokenConfigured ? 'set' : 'MISSING'} | mode=${status.readonly ? 'read-only' : 'read-write'} | search=${config.search.enabled ? 'on' : 'off'}`);
@@ -0,0 +1,58 @@
1
+ // Wraps @huggingface/transformers for local ONNX embeddings. Lazy-loaded on
2
+ // first use — no model download at server startup. The package is an OPTIONAL
3
+ // dependency; the import is guarded so the server still starts without it.
4
+ export class Embedder {
5
+ modelName;
6
+ numThreads;
7
+ pipe = null;
8
+ constructor(modelName, numThreads = 1) {
9
+ this.modelName = modelName;
10
+ this.numThreads = numThreads;
11
+ }
12
+ async load() {
13
+ if (this.pipe)
14
+ return this.pipe;
15
+ process.stderr.write(`[mantis-search] Loading embedding model ${this.modelName}...\n`);
16
+ let transformers;
17
+ try {
18
+ transformers = (await import('@huggingface/transformers'));
19
+ }
20
+ catch (err) {
21
+ const msg = err instanceof Error ? err.message : String(err);
22
+ throw new Error(`Semantic search needs @huggingface/transformers, which failed to load: ${msg}. ` +
23
+ `Run "npm install @huggingface/transformers" or set MANTIS_SEARCH_ENABLED=false.`);
24
+ }
25
+ this.pipe = await transformers.pipeline('feature-extraction', this.modelName, {
26
+ session_options: { intra_op_num_threads: this.numThreads, inter_op_num_threads: 1 },
27
+ });
28
+ return this.pipe;
29
+ }
30
+ async embed(text) {
31
+ const extractor = await this.load();
32
+ const output = await extractor(text, { pooling: 'mean', normalize: true });
33
+ return extractVector(output);
34
+ }
35
+ async embedBatch(texts) {
36
+ if (texts.length === 0)
37
+ return [];
38
+ const extractor = await this.load();
39
+ const output = await extractor(texts, { pooling: 'mean', normalize: true });
40
+ if (Array.isArray(output))
41
+ return output.map(extractVector);
42
+ return extractBatchVectors(output, texts.length);
43
+ }
44
+ }
45
+ function extractVector(output) {
46
+ return Array.from(output.data);
47
+ }
48
+ function extractBatchVectors(output, batchSize) {
49
+ const data = Array.from(output.data);
50
+ const vecSize = data.length / batchSize;
51
+ if (!Number.isInteger(vecSize)) {
52
+ throw new Error(`Unexpected batch output shape: ${data.length} elements for batch ${batchSize}`);
53
+ }
54
+ const result = [];
55
+ for (let i = 0; i < batchSize; i++)
56
+ result.push(data.slice(i * vecSize, (i + 1) * vecSize));
57
+ return result;
58
+ }
@@ -0,0 +1,48 @@
1
+ // Keyword (lexical) highlighting for search results — not semantic. A result may
2
+ // have no highlighted terms even when it is semantically relevant.
3
+ function escapeRegex(term) {
4
+ return term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
5
+ }
6
+ function combinedPattern(terms, flags) {
7
+ return new RegExp(`\\b(${terms.map(escapeRegex).join('|')})\\b`, flags);
8
+ }
9
+ /** Meaningful query terms, longest-first so short terms don't match inside bolded ones. */
10
+ export function extractTerms(query) {
11
+ const seen = new Set();
12
+ const terms = [];
13
+ for (const raw of query.trim().split(/\s+/)) {
14
+ if (raw.length < 3)
15
+ continue;
16
+ const key = raw.toLowerCase();
17
+ if (!seen.has(key)) {
18
+ seen.add(key);
19
+ terms.push(raw);
20
+ }
21
+ }
22
+ terms.sort((a, b) => b.length - a.length);
23
+ return terms;
24
+ }
25
+ export function highlightText(text, terms) {
26
+ if (!terms.length)
27
+ return text;
28
+ return text.replace(combinedPattern(terms, 'gi'), '**$1**');
29
+ }
30
+ export function hasTermMatch(text, terms) {
31
+ if (!terms.length)
32
+ return false;
33
+ return combinedPattern(terms, 'i').test(text);
34
+ }
35
+ export function extractSnippet(text, terms, contextChars = 300) {
36
+ if (text.length <= contextChars)
37
+ return highlightText(text, terms);
38
+ const match = terms.length > 0 ? text.match(combinedPattern(terms, 'i')) : null;
39
+ const matchIndex = match?.index ?? -1;
40
+ if (matchIndex === -1)
41
+ return text.slice(0, contextChars) + '…';
42
+ const half = Math.floor(contextChars / 2);
43
+ const start = Math.max(0, matchIndex - half);
44
+ const end = Math.min(text.length, start + contextChars);
45
+ const prefix = start > 0 ? '…' : '';
46
+ const suffix = end < text.length ? '…' : '';
47
+ return prefix + highlightText(text.slice(start, end), terms) + suffix;
48
+ }
@@ -0,0 +1,117 @@
1
+ import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ /**
4
+ * Simple file-backed vector store: an in-memory Map persisted to JSON, with
5
+ * brute-force cosine search. Fine for the scale of a single Mantis instance.
6
+ */
7
+ export class VectorStore {
8
+ dir;
9
+ indexFile;
10
+ lastSyncFile;
11
+ lastTotalFile;
12
+ items = new Map();
13
+ loaded = false;
14
+ constructor(dir) {
15
+ this.dir = dir;
16
+ this.indexFile = join(dir, 'index.json');
17
+ this.lastSyncFile = join(dir, 'last_sync.txt');
18
+ this.lastTotalFile = join(dir, 'last_total.txt');
19
+ }
20
+ async ensureLoaded() {
21
+ if (this.loaded)
22
+ return;
23
+ await mkdir(this.dir, { recursive: true });
24
+ try {
25
+ const parsed = JSON.parse(await readFile(this.indexFile, 'utf-8'));
26
+ this.items = new Map(parsed.map((item) => [item.id, item]));
27
+ }
28
+ catch {
29
+ this.items = new Map();
30
+ }
31
+ this.loaded = true;
32
+ }
33
+ async persist() {
34
+ const tmp = this.indexFile + '.tmp';
35
+ await writeFile(tmp, JSON.stringify([...this.items.values()]), 'utf-8');
36
+ await rename(tmp, this.indexFile);
37
+ }
38
+ async addBatch(items) {
39
+ await this.ensureLoaded();
40
+ for (const item of items)
41
+ this.items.set(item.id, item);
42
+ }
43
+ async flush() {
44
+ await this.ensureLoaded();
45
+ await this.persist();
46
+ }
47
+ async search(vector, topN) {
48
+ await this.ensureLoaded();
49
+ const results = [...this.items.values()].map((item) => ({
50
+ id: item.id,
51
+ score: cosineSimilarity(vector, item.vector),
52
+ }));
53
+ results.sort((a, b) => b.score - a.score);
54
+ return results.slice(0, topN);
55
+ }
56
+ async getItem(id) {
57
+ await this.ensureLoaded();
58
+ return this.items.get(id) ?? null;
59
+ }
60
+ async count() {
61
+ await this.ensureLoaded();
62
+ return this.items.size;
63
+ }
64
+ async clear() {
65
+ await this.ensureLoaded();
66
+ this.items.clear();
67
+ await this.persist();
68
+ }
69
+ async getLastSyncedAt() {
70
+ try {
71
+ return (await readFile(this.lastSyncFile, 'utf-8')).trim() || null;
72
+ }
73
+ catch {
74
+ return null;
75
+ }
76
+ }
77
+ async setLastSyncedAt(ts) {
78
+ await mkdir(this.dir, { recursive: true });
79
+ await writeFile(this.lastSyncFile, ts, 'utf-8');
80
+ }
81
+ async resetLastSyncedAt() {
82
+ try {
83
+ await unlink(this.lastSyncFile);
84
+ }
85
+ catch (err) {
86
+ if (err.code !== 'ENOENT')
87
+ throw err;
88
+ }
89
+ }
90
+ async getLastKnownTotal() {
91
+ try {
92
+ const parsed = parseInt((await readFile(this.lastTotalFile, 'utf-8')).trim(), 10);
93
+ return Number.isNaN(parsed) ? null : parsed;
94
+ }
95
+ catch {
96
+ return null;
97
+ }
98
+ }
99
+ async setLastKnownTotal(total) {
100
+ await mkdir(this.dir, { recursive: true });
101
+ await writeFile(this.lastTotalFile, String(total), 'utf-8');
102
+ }
103
+ }
104
+ function cosineSimilarity(a, b) {
105
+ if (a.length !== b.length || a.length === 0)
106
+ return 0;
107
+ let dot = 0;
108
+ let normA = 0;
109
+ let normB = 0;
110
+ for (let i = 0; i < a.length; i++) {
111
+ dot += a[i] * b[i];
112
+ normA += a[i] * a[i];
113
+ normB += b[i] * b[i];
114
+ }
115
+ const denom = Math.sqrt(normA) * Math.sqrt(normB);
116
+ return denom === 0 ? 0 : dot / denom;
117
+ }
@@ -0,0 +1,92 @@
1
+ const PAGE_SIZE = 50;
2
+ const EMBED_BATCH_SIZE = 10;
3
+ const CHECKPOINT_INTERVAL = 100;
4
+ /** Indexes Mantis issues into the vector store (summary + description embeddings). */
5
+ export class SearchSyncService {
6
+ client;
7
+ store;
8
+ embedder;
9
+ constructor(client, store, embedder) {
10
+ this.client = client;
11
+ this.store = store;
12
+ this.embedder = embedder;
13
+ }
14
+ async sync(projectId) {
15
+ // Stamp the sync start BEFORE fetching, so issues edited during the run are
16
+ // re-indexed by the next incremental sync instead of being skipped.
17
+ const startedAt = new Date().toISOString();
18
+ const lastSyncedAt = await this.store.getLastSyncedAt();
19
+ const lastMs = lastSyncedAt ? Date.parse(lastSyncedAt) : null;
20
+ const { issues, totalFromApi } = await this.fetchAllIssues(projectId);
21
+ // The REST API has no server-side date filter, so incremental syncs skip
22
+ // unchanged issues client-side — only re-embedding what changed.
23
+ let skipped = 0;
24
+ const toEmbed = [];
25
+ for (const issue of issues) {
26
+ if (!issue.summary) {
27
+ skipped++;
28
+ continue;
29
+ }
30
+ if (lastMs !== null && issue.updated_at && Date.parse(issue.updated_at) <= lastMs) {
31
+ skipped++; // unchanged since the last sync
32
+ continue;
33
+ }
34
+ toEmbed.push({ issue, text: `${issue.summary}\n${issue.description ?? ''}`.trim() });
35
+ }
36
+ let indexed = 0;
37
+ let sinceCheckpoint = 0;
38
+ for (let i = 0; i < toEmbed.length; i += EMBED_BATCH_SIZE) {
39
+ const batch = toEmbed.slice(i, i + EMBED_BATCH_SIZE);
40
+ const vectors = await this.embedder.embedBatch(batch.map((e) => e.text));
41
+ const items = batch.map((e, j) => ({
42
+ id: e.issue.id,
43
+ vector: vectors[j],
44
+ metadata: {
45
+ summary: e.issue.summary,
46
+ description: e.issue.description,
47
+ updated_at: e.issue.updated_at,
48
+ created_at: e.issue.created_at,
49
+ },
50
+ }));
51
+ await this.store.addBatch(items);
52
+ indexed += items.length;
53
+ sinceCheckpoint += items.length;
54
+ if (sinceCheckpoint >= CHECKPOINT_INTERVAL) {
55
+ await this.store.flush();
56
+ sinceCheckpoint = 0;
57
+ }
58
+ }
59
+ await this.store.flush();
60
+ await this.store.setLastSyncedAt(startedAt);
61
+ const storeCount = await this.store.count();
62
+ const total = totalFromApi ?? (lastSyncedAt === null ? indexed + skipped : storeCount);
63
+ await this.store.setLastKnownTotal(total);
64
+ return { indexed, skipped, total };
65
+ }
66
+ async fetchAllIssues(projectId) {
67
+ const all = [];
68
+ let totalFromApi = null;
69
+ let page = 1;
70
+ for (;;) {
71
+ const params = {
72
+ page_size: PAGE_SIZE,
73
+ page,
74
+ select: 'id,summary,description,updated_at,created_at',
75
+ project_id: projectId,
76
+ };
77
+ const response = await this.client.get('/issues', params);
78
+ const pageIssues = response.issues ?? [];
79
+ all.push(...pageIssues);
80
+ if (totalFromApi === null && response.total_count != null)
81
+ totalFromApi = response.total_count;
82
+ // Hard stop on an empty/short page regardless of any (possibly over-reported)
83
+ // total_count, so the loop can never spin forever hammering the API.
84
+ if (pageIssues.length < PAGE_SIZE)
85
+ break;
86
+ if (totalFromApi !== null && all.length >= totalFromApi)
87
+ break;
88
+ page++;
89
+ }
90
+ return { issues: all, totalFromApi };
91
+ }
92
+ }