@artyfacts/claude 1.1.2 → 1.2.1

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,741 @@
1
+ // src/auth.ts
2
+ import * as fs from "fs";
3
+ import * as path from "path";
4
+ import * as os from "os";
5
+ import * as readline from "readline";
6
+ var CREDENTIALS_DIR = path.join(os.homedir(), ".artyfacts");
7
+ var CREDENTIALS_FILE = path.join(CREDENTIALS_DIR, "credentials.json");
8
+ var DEFAULT_BASE_URL = "https://artyfacts.dev/api/v1";
9
+ function loadCredentials() {
10
+ try {
11
+ if (!fs.existsSync(CREDENTIALS_FILE)) {
12
+ return null;
13
+ }
14
+ const data = fs.readFileSync(CREDENTIALS_FILE, "utf-8");
15
+ const credentials = JSON.parse(data);
16
+ if (credentials.expiresAt) {
17
+ const expiresAt = new Date(credentials.expiresAt);
18
+ if (expiresAt < /* @__PURE__ */ new Date()) {
19
+ console.log("\u26A0\uFE0F Credentials have expired");
20
+ return null;
21
+ }
22
+ }
23
+ return credentials;
24
+ } catch (error) {
25
+ console.error("Failed to load credentials:", error);
26
+ return null;
27
+ }
28
+ }
29
+ function saveCredentials(credentials) {
30
+ try {
31
+ if (!fs.existsSync(CREDENTIALS_DIR)) {
32
+ fs.mkdirSync(CREDENTIALS_DIR, { mode: 448, recursive: true });
33
+ }
34
+ fs.writeFileSync(
35
+ CREDENTIALS_FILE,
36
+ JSON.stringify(credentials, null, 2),
37
+ { mode: 384 }
38
+ );
39
+ } catch (error) {
40
+ throw new Error(`Failed to save credentials: ${error}`);
41
+ }
42
+ }
43
+ function clearCredentials() {
44
+ try {
45
+ if (fs.existsSync(CREDENTIALS_FILE)) {
46
+ fs.unlinkSync(CREDENTIALS_FILE);
47
+ }
48
+ } catch (error) {
49
+ console.error("Failed to clear credentials:", error);
50
+ }
51
+ }
52
+ async function runDeviceAuth(baseUrl = DEFAULT_BASE_URL) {
53
+ console.log("\u{1F510} Starting device authentication...\n");
54
+ const deviceAuth = await requestDeviceCode(baseUrl);
55
+ console.log("\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501");
56
+ console.log("\u{1F4CB} To authenticate, visit:");
57
+ console.log(` ${deviceAuth.verificationUri}`);
58
+ console.log("");
59
+ console.log("\u{1F511} Enter this code:");
60
+ console.log(` ${deviceAuth.userCode}`);
61
+ console.log("\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\n");
62
+ console.log("\u23F3 Waiting for authentication...\n");
63
+ const credentials = await pollForToken(
64
+ baseUrl,
65
+ deviceAuth.deviceCode,
66
+ deviceAuth.interval,
67
+ deviceAuth.expiresIn
68
+ );
69
+ saveCredentials(credentials);
70
+ console.log("\u2705 Authentication successful!");
71
+ console.log(` Agent ID: ${credentials.agentId}`);
72
+ if (credentials.agentName) {
73
+ console.log(` Agent Name: ${credentials.agentName}`);
74
+ }
75
+ console.log("");
76
+ return credentials;
77
+ }
78
+ async function requestDeviceCode(baseUrl) {
79
+ const response = await fetch(`${baseUrl}/auth/device`, {
80
+ method: "POST",
81
+ headers: {
82
+ "Content-Type": "application/json"
83
+ },
84
+ body: JSON.stringify({
85
+ client_id: "artyfacts-claude",
86
+ scope: "agent:execute"
87
+ })
88
+ });
89
+ if (!response.ok) {
90
+ const error = await response.text();
91
+ throw new Error(`Failed to start device auth: ${error}`);
92
+ }
93
+ const data = await response.json();
94
+ return {
95
+ deviceCode: data.deviceCode || data.device_code || "",
96
+ userCode: data.userCode || data.user_code || "",
97
+ verificationUri: data.verificationUri || data.verification_uri || `https://artyfacts.dev/auth/device`,
98
+ expiresIn: data.expiresIn || data.expires_in || 600,
99
+ interval: data.interval || 5
100
+ };
101
+ }
102
+ async function pollForToken(baseUrl, deviceCode, interval, expiresIn) {
103
+ const startTime = Date.now();
104
+ const timeoutMs = expiresIn * 1e3;
105
+ while (true) {
106
+ if (Date.now() - startTime > timeoutMs) {
107
+ throw new Error("Device authentication timed out");
108
+ }
109
+ await sleep(interval * 1e3);
110
+ const response = await fetch(`${baseUrl}/auth/device/token`, {
111
+ method: "POST",
112
+ headers: {
113
+ "Content-Type": "application/json"
114
+ },
115
+ body: JSON.stringify({
116
+ device_code: deviceCode,
117
+ client_id: "artyfacts-claude"
118
+ })
119
+ });
120
+ if (response.ok) {
121
+ const data = await response.json();
122
+ return {
123
+ apiKey: data.apiKey,
124
+ agentId: data.agentId,
125
+ agentName: data.agentName,
126
+ expiresAt: data.expiresAt
127
+ };
128
+ }
129
+ const errorData = await response.json().catch(() => ({}));
130
+ const errorCode = errorData.error || errorData.code;
131
+ if (errorCode === "authorization_pending") {
132
+ process.stdout.write(".");
133
+ continue;
134
+ }
135
+ if (errorCode === "slow_down") {
136
+ interval = Math.min(interval * 2, 30);
137
+ continue;
138
+ }
139
+ if (errorCode === "expired_token") {
140
+ throw new Error("Device code expired. Please try again.");
141
+ }
142
+ if (errorCode === "access_denied") {
143
+ throw new Error("Authorization was denied.");
144
+ }
145
+ throw new Error(`Authentication failed: ${errorData.message || errorCode || response.statusText}`);
146
+ }
147
+ }
148
+ async function promptForApiKey() {
149
+ const rl = readline.createInterface({
150
+ input: process.stdin,
151
+ output: process.stdout
152
+ });
153
+ const question = (prompt) => {
154
+ return new Promise((resolve) => {
155
+ rl.question(prompt, resolve);
156
+ });
157
+ };
158
+ console.log("\u{1F511} Manual Configuration\n");
159
+ console.log("Enter your Artyfacts credentials:\n");
160
+ const apiKey = await question("API Key: ");
161
+ const agentId = await question("Agent ID: ");
162
+ const agentName = await question("Agent Name (optional): ");
163
+ rl.close();
164
+ if (!apiKey || !agentId) {
165
+ throw new Error("API Key and Agent ID are required");
166
+ }
167
+ const credentials = {
168
+ apiKey: apiKey.trim(),
169
+ agentId: agentId.trim(),
170
+ agentName: agentName.trim() || void 0
171
+ };
172
+ saveCredentials(credentials);
173
+ console.log("\n\u2705 Credentials saved!");
174
+ return credentials;
175
+ }
176
+ function sleep(ms) {
177
+ return new Promise((resolve) => setTimeout(resolve, ms));
178
+ }
179
+ async function getCredentials(options) {
180
+ if (!options?.forceAuth) {
181
+ const existing = loadCredentials();
182
+ if (existing) {
183
+ return existing;
184
+ }
185
+ }
186
+ return runDeviceAuth(options?.baseUrl);
187
+ }
188
+
189
+ // src/context.ts
190
+ var ContextFetcher = class {
191
+ config;
192
+ constructor(config) {
193
+ this.config = config;
194
+ }
195
+ /**
196
+ * Fetch full context for a task
197
+ */
198
+ async fetchTaskContext(taskId) {
199
+ const response = await fetch(
200
+ `${this.config.baseUrl}/tasks/${taskId}/context`,
201
+ {
202
+ headers: {
203
+ "Authorization": `Bearer ${this.config.apiKey}`,
204
+ "Accept": "application/json"
205
+ }
206
+ }
207
+ );
208
+ if (!response.ok) {
209
+ const errorText = await response.text().catch(() => "Unknown error");
210
+ throw new Error(`Failed to fetch task context: ${response.status} - ${errorText}`);
211
+ }
212
+ const data = await response.json();
213
+ return data;
214
+ }
215
+ };
216
+ function buildPromptWithContext(context) {
217
+ const parts = [];
218
+ parts.push(`You are an AI agent working within the Artyfacts task management system.
219
+
220
+ Your job is to complete the assigned task. You have full context about the organization, project, and related work.
221
+
222
+ Guidelines:
223
+ - Be thorough but concise
224
+ - If the task requires code, provide working code
225
+ - If the task requires analysis, provide structured findings
226
+ - If the task requires a decision, explain your reasoning
227
+ - If you cannot complete the task, explain why
228
+
229
+ Format your response as follows:
230
+ 1. First, provide your main output (the task deliverable)
231
+ 2. End with a brief summary line starting with "SUMMARY:"`);
232
+ parts.push("");
233
+ parts.push("---");
234
+ parts.push("");
235
+ parts.push("## Organization Context");
236
+ parts.push(`**${context.organization.name}**`);
237
+ if (context.organization.context) {
238
+ parts.push("");
239
+ parts.push(formatOrgContext(context.organization.context));
240
+ }
241
+ parts.push("");
242
+ if (context.project) {
243
+ parts.push(`## Project: ${context.project.name}`);
244
+ if (context.project.description) {
245
+ parts.push(context.project.description);
246
+ }
247
+ parts.push("");
248
+ }
249
+ parts.push(`## Artifact: ${context.artifact.title}`);
250
+ if (context.artifact.summary) {
251
+ parts.push(context.artifact.summary);
252
+ }
253
+ if (context.artifact.description) {
254
+ parts.push("");
255
+ parts.push(context.artifact.description);
256
+ }
257
+ parts.push("");
258
+ const relatedSections = context.artifact.sections.filter(
259
+ (s) => s.id !== context.task.id
260
+ );
261
+ if (relatedSections.length > 0) {
262
+ parts.push("### Related Sections:");
263
+ for (const section of relatedSections) {
264
+ const preview = section.content ? section.content.substring(0, 200) + (section.content.length > 200 ? "..." : "") : "No content";
265
+ const statusBadge = section.task_status ? ` [${section.task_status}]` : "";
266
+ parts.push(`- **${section.heading}**${statusBadge}: ${preview}`);
267
+ }
268
+ parts.push("");
269
+ }
270
+ parts.push("---");
271
+ parts.push("");
272
+ parts.push(`## Your Task: ${context.task.heading}`);
273
+ if (context.task.priority) {
274
+ const priorityLabels = ["\u{1F534} High", "\u{1F7E1} Medium", "\u{1F7E2} Low"];
275
+ parts.push(`**Priority:** ${priorityLabels[context.task.priority - 1] || "Medium"}`);
276
+ }
277
+ parts.push("");
278
+ parts.push("### Description");
279
+ parts.push(context.task.content || "No additional description provided.");
280
+ parts.push("");
281
+ if (context.task.expected_output) {
282
+ parts.push("### Expected Output");
283
+ if (context.task.expected_output.format) {
284
+ parts.push(`**Format:** ${context.task.expected_output.format}`);
285
+ }
286
+ if (context.task.expected_output.requirements && context.task.expected_output.requirements.length > 0) {
287
+ parts.push("**Requirements:**");
288
+ for (const req of context.task.expected_output.requirements) {
289
+ parts.push(`- ${req}`);
290
+ }
291
+ }
292
+ parts.push("");
293
+ }
294
+ parts.push("---");
295
+ parts.push("");
296
+ parts.push("Complete this task and provide your output below.");
297
+ return parts.join("\n");
298
+ }
299
+ function formatOrgContext(context) {
300
+ const trimmed = context.trim();
301
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
302
+ try {
303
+ const parsed = JSON.parse(trimmed);
304
+ return formatContextObject(parsed);
305
+ } catch {
306
+ return context;
307
+ }
308
+ }
309
+ return context;
310
+ }
311
+ function formatContextObject(obj, indent = "") {
312
+ if (typeof obj !== "object" || obj === null) {
313
+ return String(obj);
314
+ }
315
+ if (Array.isArray(obj)) {
316
+ return obj.map((item) => `${indent}- ${formatContextObject(item, indent + " ")}`).join("\n");
317
+ }
318
+ const lines = [];
319
+ for (const [key, value] of Object.entries(obj)) {
320
+ const label = key.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
321
+ if (typeof value === "object" && value !== null) {
322
+ lines.push(`${indent}**${label}:**`);
323
+ lines.push(formatContextObject(value, indent + " "));
324
+ } else {
325
+ lines.push(`${indent}- **${label}:** ${value}`);
326
+ }
327
+ }
328
+ return lines.join("\n");
329
+ }
330
+ function createContextFetcher(config) {
331
+ return new ContextFetcher(config);
332
+ }
333
+
334
+ // src/executor.ts
335
+ import { spawn } from "child_process";
336
+ var DEFAULT_TIMEOUT = 5 * 60 * 1e3;
337
+ var DEFAULT_SYSTEM_PROMPT = `You are an AI agent working within the Artyfacts task management system.
338
+
339
+ Your job is to complete tasks assigned to you. For each task:
340
+ 1. Understand the requirements from the task heading and content
341
+ 2. Complete the task to the best of your ability
342
+ 3. Provide a clear, actionable output
343
+
344
+ Guidelines:
345
+ - Be thorough but concise
346
+ - If the task requires code, provide working code
347
+ - If the task requires analysis, provide structured findings
348
+ - If the task requires a decision, explain your reasoning
349
+ - If you cannot complete the task, explain why
350
+
351
+ Format your response as follows:
352
+ 1. First, provide your main output (the task deliverable)
353
+ 2. End with a brief summary line starting with "SUMMARY:"`;
354
+ var ClaudeExecutor = class {
355
+ config;
356
+ contextFetcher = null;
357
+ constructor(config = {}) {
358
+ this.config = {
359
+ ...config,
360
+ timeout: config.timeout || DEFAULT_TIMEOUT,
361
+ claudePath: config.claudePath || "claude"
362
+ };
363
+ if (config.baseUrl && config.apiKey) {
364
+ this.contextFetcher = createContextFetcher({
365
+ baseUrl: config.baseUrl,
366
+ apiKey: config.apiKey
367
+ });
368
+ }
369
+ }
370
+ /**
371
+ * Execute a task using Claude Code CLI
372
+ *
373
+ * If full context is available (baseUrl + apiKey configured), fetches
374
+ * organization, project, artifact, and related sections for a rich prompt.
375
+ */
376
+ async execute(task) {
377
+ try {
378
+ let prompt;
379
+ let fullContext = null;
380
+ const useFullContext = this.config.useFullContext !== false && this.contextFetcher;
381
+ if (useFullContext) {
382
+ try {
383
+ fullContext = await this.contextFetcher.fetchTaskContext(task.taskId);
384
+ prompt = buildPromptWithContext(fullContext);
385
+ console.log(" \u{1F4DA} Using full context (org, project, artifact, related sections)");
386
+ } catch (contextError) {
387
+ console.warn(" \u26A0\uFE0F Could not fetch full context, using minimal prompt");
388
+ console.warn(` ${contextError instanceof Error ? contextError.message : contextError}`);
389
+ prompt = this.buildTaskPrompt(task);
390
+ }
391
+ } else {
392
+ prompt = this.buildTaskPrompt(task);
393
+ }
394
+ const output = await this.runClaude(prompt);
395
+ const { content, summary } = this.parseResponse(output, task.heading);
396
+ return {
397
+ success: true,
398
+ output: content,
399
+ summary,
400
+ promptUsed: prompt
401
+ };
402
+ } catch (error) {
403
+ const errorMessage = error instanceof Error ? error.message : String(error);
404
+ return {
405
+ success: false,
406
+ output: "",
407
+ summary: `Failed: ${errorMessage}`,
408
+ error: errorMessage
409
+ };
410
+ }
411
+ }
412
+ /**
413
+ * Run Claude Code CLI with the given prompt
414
+ */
415
+ runClaude(prompt) {
416
+ return new Promise((resolve, reject) => {
417
+ const claudePath = this.config.claudePath || "claude";
418
+ const proc = spawn(claudePath, ["--print"], {
419
+ stdio: ["pipe", "pipe", "pipe"],
420
+ timeout: this.config.timeout
421
+ });
422
+ let stdout = "";
423
+ let stderr = "";
424
+ proc.stdout.on("data", (data) => {
425
+ stdout += data.toString();
426
+ });
427
+ proc.stderr.on("data", (data) => {
428
+ stderr += data.toString();
429
+ });
430
+ proc.on("close", (code) => {
431
+ if (code === 0) {
432
+ resolve(stdout.trim());
433
+ } else {
434
+ reject(new Error(stderr || `Claude exited with code ${code}`));
435
+ }
436
+ });
437
+ proc.on("error", (err) => {
438
+ if (err.code === "ENOENT") {
439
+ reject(new Error(
440
+ "Claude Code CLI not found. Please install it:\n npm install -g @anthropic-ai/claude-code"
441
+ ));
442
+ } else {
443
+ reject(err);
444
+ }
445
+ });
446
+ proc.stdin.write(prompt);
447
+ proc.stdin.end();
448
+ });
449
+ }
450
+ /**
451
+ * Build the task prompt
452
+ */
453
+ buildTaskPrompt(task) {
454
+ const parts = [];
455
+ const systemPrompt = this.config.systemPromptPrefix ? `${this.config.systemPromptPrefix}
456
+
457
+ ${DEFAULT_SYSTEM_PROMPT}` : DEFAULT_SYSTEM_PROMPT;
458
+ parts.push(systemPrompt);
459
+ parts.push("");
460
+ parts.push("---");
461
+ parts.push("");
462
+ parts.push(`# Task: ${task.heading}`);
463
+ parts.push("");
464
+ if (task.artifactTitle) {
465
+ parts.push(`**Artifact:** ${task.artifactTitle}`);
466
+ }
467
+ if (task.priority) {
468
+ const priorityLabels = ["High", "Medium", "Low"];
469
+ parts.push(`**Priority:** ${priorityLabels[task.priority - 1] || "Medium"}`);
470
+ }
471
+ parts.push("");
472
+ parts.push("## Description");
473
+ parts.push(task.content || "No additional description provided.");
474
+ parts.push("");
475
+ if (task.context && Object.keys(task.context).length > 0) {
476
+ parts.push("## Additional Context");
477
+ parts.push("```json");
478
+ parts.push(JSON.stringify(task.context, null, 2));
479
+ parts.push("```");
480
+ parts.push("");
481
+ }
482
+ parts.push("## Instructions");
483
+ parts.push("Complete this task and provide your output below.");
484
+ return parts.join("\n");
485
+ }
486
+ /**
487
+ * Parse the response to extract output and summary
488
+ */
489
+ parseResponse(fullOutput, taskHeading) {
490
+ const summaryMatch = fullOutput.match(/SUMMARY:\s*(.+?)(?:\n|$)/i);
491
+ if (summaryMatch) {
492
+ const summary2 = summaryMatch[1].trim();
493
+ const content = fullOutput.replace(/SUMMARY:\s*.+?(?:\n|$)/i, "").trim();
494
+ return { content, summary: summary2 };
495
+ }
496
+ const lines = fullOutput.split("\n").filter((l) => l.trim());
497
+ const firstLine = lines[0] || "";
498
+ const summary = firstLine.length > 100 ? `${firstLine.substring(0, 97)}...` : firstLine || `Completed: ${taskHeading}`;
499
+ return { content: fullOutput, summary };
500
+ }
501
+ /**
502
+ * Test that Claude Code CLI is available and working
503
+ */
504
+ async testConnection() {
505
+ try {
506
+ const output = await this.runClaude('Say "connected" and nothing else.');
507
+ return output.toLowerCase().includes("connected");
508
+ } catch {
509
+ return false;
510
+ }
511
+ }
512
+ /**
513
+ * Check if Claude Code CLI is installed
514
+ */
515
+ async isInstalled() {
516
+ return new Promise((resolve) => {
517
+ const proc = spawn(this.config.claudePath || "claude", ["--version"], {
518
+ stdio: ["ignore", "pipe", "pipe"]
519
+ });
520
+ proc.on("close", (code) => {
521
+ resolve(code === 0);
522
+ });
523
+ proc.on("error", () => {
524
+ resolve(false);
525
+ });
526
+ });
527
+ }
528
+ };
529
+ function createExecutor(config) {
530
+ return new ClaudeExecutor(config);
531
+ }
532
+
533
+ // src/listener.ts
534
+ import EventSource from "eventsource";
535
+ var DEFAULT_BASE_URL2 = "https://artyfacts.dev/api/v1";
536
+ var EVENT_TYPES = [
537
+ "connected",
538
+ "heartbeat",
539
+ "task_assigned",
540
+ "task_unblocked",
541
+ "blocker_resolved",
542
+ "notification"
543
+ ];
544
+ var ArtyfactsListener = class {
545
+ config;
546
+ eventSource = null;
547
+ callbacks = /* @__PURE__ */ new Map();
548
+ allCallbacks = /* @__PURE__ */ new Set();
549
+ state = "disconnected";
550
+ reconnectAttempts = 0;
551
+ maxReconnectAttempts = 10;
552
+ reconnectDelay = 1e3;
553
+ constructor(config) {
554
+ if (!config.apiKey) {
555
+ throw new Error("API key is required");
556
+ }
557
+ if (!config.agentId) {
558
+ throw new Error("Agent ID is required");
559
+ }
560
+ this.config = {
561
+ ...config,
562
+ baseUrl: config.baseUrl || DEFAULT_BASE_URL2
563
+ };
564
+ }
565
+ /**
566
+ * Get current connection state
567
+ */
568
+ get connectionState() {
569
+ return this.state;
570
+ }
571
+ /**
572
+ * Check if connected
573
+ */
574
+ get isConnected() {
575
+ return this.state === "connected";
576
+ }
577
+ /**
578
+ * Subscribe to all events
579
+ */
580
+ subscribe(callback) {
581
+ this.allCallbacks.add(callback);
582
+ return () => {
583
+ this.allCallbacks.delete(callback);
584
+ };
585
+ }
586
+ /**
587
+ * Subscribe to a specific event type
588
+ */
589
+ on(type, callback) {
590
+ if (!this.callbacks.has(type)) {
591
+ this.callbacks.set(type, /* @__PURE__ */ new Set());
592
+ }
593
+ this.callbacks.get(type).add(callback);
594
+ return () => {
595
+ const typeCallbacks = this.callbacks.get(type);
596
+ if (typeCallbacks) {
597
+ typeCallbacks.delete(callback);
598
+ if (typeCallbacks.size === 0) {
599
+ this.callbacks.delete(type);
600
+ }
601
+ }
602
+ };
603
+ }
604
+ /**
605
+ * Connect to the SSE stream
606
+ */
607
+ connect() {
608
+ if (this.eventSource) {
609
+ return;
610
+ }
611
+ this.setState("connecting");
612
+ const url = new URL(`${this.config.baseUrl}/events/stream`);
613
+ url.searchParams.set("apiKey", this.config.apiKey);
614
+ url.searchParams.set("agentId", this.config.agentId);
615
+ this.eventSource = new EventSource(url.toString(), {
616
+ headers: {
617
+ "Authorization": `Bearer ${this.config.apiKey}`
618
+ }
619
+ });
620
+ this.eventSource.onopen = () => {
621
+ this.reconnectAttempts = 0;
622
+ this.reconnectDelay = 1e3;
623
+ this.setState("connected");
624
+ };
625
+ this.eventSource.onmessage = (event) => {
626
+ this.handleMessage(event);
627
+ };
628
+ this.eventSource.onerror = (event) => {
629
+ this.handleError(event);
630
+ };
631
+ for (const eventType of EVENT_TYPES) {
632
+ this.eventSource.addEventListener(eventType, (event) => {
633
+ this.handleMessage(event, eventType);
634
+ });
635
+ }
636
+ }
637
+ /**
638
+ * Disconnect from the SSE stream
639
+ */
640
+ disconnect() {
641
+ if (this.eventSource) {
642
+ this.eventSource.close();
643
+ this.eventSource = null;
644
+ }
645
+ this.setState("disconnected");
646
+ }
647
+ /**
648
+ * Reconnect to the SSE stream
649
+ */
650
+ reconnect() {
651
+ this.disconnect();
652
+ this.connect();
653
+ }
654
+ /**
655
+ * Handle incoming SSE message
656
+ */
657
+ handleMessage(event, eventType) {
658
+ try {
659
+ const data = JSON.parse(event.data);
660
+ const artyfactsEvent = {
661
+ type: eventType || data.type || "unknown",
662
+ timestamp: data.timestamp || (/* @__PURE__ */ new Date()).toISOString(),
663
+ data: data.data || data
664
+ };
665
+ const typeCallbacks = this.callbacks.get(artyfactsEvent.type);
666
+ if (typeCallbacks) {
667
+ for (const callback of typeCallbacks) {
668
+ this.safeCallCallback(callback, artyfactsEvent);
669
+ }
670
+ }
671
+ for (const callback of this.allCallbacks) {
672
+ this.safeCallCallback(callback, artyfactsEvent);
673
+ }
674
+ } catch (err) {
675
+ console.error("[Listener] Failed to parse SSE message:", event.data, err);
676
+ }
677
+ }
678
+ /**
679
+ * Safely call a callback, handling async and errors
680
+ */
681
+ async safeCallCallback(callback, event) {
682
+ try {
683
+ await callback(event);
684
+ } catch (err) {
685
+ console.error(`[Listener] Error in event callback for '${event.type}':`, err);
686
+ }
687
+ }
688
+ /**
689
+ * Handle SSE error
690
+ */
691
+ handleError(event) {
692
+ if (this.eventSource?.readyState === EventSource.CONNECTING) {
693
+ this.setState("reconnecting");
694
+ } else if (this.eventSource?.readyState === EventSource.CLOSED) {
695
+ this.setState("disconnected");
696
+ if (this.reconnectAttempts < this.maxReconnectAttempts) {
697
+ this.reconnectAttempts++;
698
+ this.reconnectDelay = Math.min(this.reconnectDelay * 2, 3e4);
699
+ console.log(
700
+ `[Listener] Connection lost, reconnecting in ${this.reconnectDelay / 1e3}s (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`
701
+ );
702
+ setTimeout(() => {
703
+ if (this.state === "disconnected") {
704
+ this.connect();
705
+ }
706
+ }, this.reconnectDelay);
707
+ } else {
708
+ const error = new Error("Max reconnection attempts reached");
709
+ this.config.onError?.(error);
710
+ }
711
+ }
712
+ }
713
+ /**
714
+ * Update connection state
715
+ */
716
+ setState(state) {
717
+ if (this.state !== state) {
718
+ this.state = state;
719
+ this.config.onStateChange?.(state);
720
+ }
721
+ }
722
+ };
723
+ function createListener(config) {
724
+ return new ArtyfactsListener(config);
725
+ }
726
+
727
+ export {
728
+ loadCredentials,
729
+ saveCredentials,
730
+ clearCredentials,
731
+ runDeviceAuth,
732
+ promptForApiKey,
733
+ getCredentials,
734
+ ContextFetcher,
735
+ buildPromptWithContext,
736
+ createContextFetcher,
737
+ ClaudeExecutor,
738
+ createExecutor,
739
+ ArtyfactsListener,
740
+ createListener
741
+ };