@artyfacts/claude 1.3.13 → 1.3.14

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,1005 @@
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
+ ## Available Tools
223
+
224
+ You have access to Artyfacts MCP tools. USE THEM to complete your task:
225
+
226
+ - **create_artifact** - Create new artifacts (documents, specs, reports)
227
+ - **create_section** - Add sections to artifacts (content, tasks, decisions)
228
+ - **update_section** - Update existing sections
229
+ - **create_agent** - Create new AI agents with specific roles
230
+ - **list_artifacts** - Query existing artifacts
231
+ - **list_sections** - Query sections within an artifact
232
+ - **complete_task** - Mark a task as complete
233
+ - **block_task** - Block a task with a reason
234
+ - **create_blocker** - Create a decision blocker
235
+
236
+ IMPORTANT: When asked to create agents or update artifacts, USE THE TOOLS. Don't just describe what you would do - actually do it with the tools.
237
+
238
+ ## Guidelines
239
+
240
+ - Be thorough but concise
241
+ - USE THE TOOLS to take action, don't just analyze
242
+ - If the task requires creating something, use create_artifact or create_section
243
+ - If the task requires creating agents, use create_agent
244
+ - If you cannot complete the task, explain why
245
+
246
+ Format your response as follows:
247
+ 1. First, use the tools to complete the task
248
+ 2. Then summarize what you did
249
+ 3. End with a brief summary line starting with "SUMMARY:"`);
250
+ parts.push("");
251
+ parts.push("---");
252
+ parts.push("");
253
+ parts.push("## Organization Context");
254
+ parts.push(`**${context.organization.name}**`);
255
+ if (context.organization.context) {
256
+ parts.push("");
257
+ parts.push(formatOrgContext(context.organization.context));
258
+ }
259
+ parts.push("");
260
+ if (context.project) {
261
+ parts.push(`## Project: ${context.project.name}`);
262
+ if (context.project.description) {
263
+ parts.push(context.project.description);
264
+ }
265
+ parts.push("");
266
+ }
267
+ parts.push(`## Artifact: ${context.artifact.title}`);
268
+ if (context.artifact.summary) {
269
+ parts.push(context.artifact.summary);
270
+ }
271
+ if (context.artifact.description) {
272
+ parts.push("");
273
+ parts.push(context.artifact.description);
274
+ }
275
+ parts.push("");
276
+ const relatedSections = context.artifact.sections.filter(
277
+ (s) => s.id !== context.task.id
278
+ );
279
+ if (relatedSections.length > 0) {
280
+ parts.push("### Related Sections:");
281
+ for (const section of relatedSections) {
282
+ const preview = section.content ? section.content.substring(0, 200) + (section.content.length > 200 ? "..." : "") : "No content";
283
+ const statusBadge = section.task_status ? ` [${section.task_status}]` : "";
284
+ parts.push(`- **${section.heading}**${statusBadge}: ${preview}`);
285
+ }
286
+ parts.push("");
287
+ }
288
+ parts.push("---");
289
+ parts.push("");
290
+ parts.push(`## Your Task: ${context.task.heading}`);
291
+ if (context.task.priority) {
292
+ const priorityLabels = ["\u{1F534} High", "\u{1F7E1} Medium", "\u{1F7E2} Low"];
293
+ parts.push(`**Priority:** ${priorityLabels[context.task.priority - 1] || "Medium"}`);
294
+ }
295
+ parts.push("");
296
+ parts.push("### Description");
297
+ parts.push(context.task.content || "No additional description provided.");
298
+ parts.push("");
299
+ if (context.task.expected_output) {
300
+ parts.push("### Expected Output");
301
+ if (context.task.expected_output.format) {
302
+ parts.push(`**Format:** ${context.task.expected_output.format}`);
303
+ }
304
+ if (context.task.expected_output.requirements && context.task.expected_output.requirements.length > 0) {
305
+ parts.push("**Requirements:**");
306
+ for (const req of context.task.expected_output.requirements) {
307
+ parts.push(`- ${req}`);
308
+ }
309
+ }
310
+ parts.push("");
311
+ }
312
+ parts.push("---");
313
+ parts.push("");
314
+ parts.push("Complete this task and provide your output below.");
315
+ return parts.join("\n");
316
+ }
317
+ function formatOrgContext(context) {
318
+ const trimmed = context.trim();
319
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
320
+ try {
321
+ const parsed = JSON.parse(trimmed);
322
+ return formatContextObject(parsed);
323
+ } catch {
324
+ return context;
325
+ }
326
+ }
327
+ return context;
328
+ }
329
+ function formatContextObject(obj, indent = "") {
330
+ if (typeof obj !== "object" || obj === null) {
331
+ return String(obj);
332
+ }
333
+ if (Array.isArray(obj)) {
334
+ return obj.map((item) => `${indent}- ${formatContextObject(item, indent + " ")}`).join("\n");
335
+ }
336
+ const lines = [];
337
+ for (const [key, value] of Object.entries(obj)) {
338
+ const label = key.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
339
+ if (typeof value === "object" && value !== null) {
340
+ lines.push(`${indent}**${label}:**`);
341
+ lines.push(formatContextObject(value, indent + " "));
342
+ } else {
343
+ lines.push(`${indent}- **${label}:** ${value}`);
344
+ }
345
+ }
346
+ return lines.join("\n");
347
+ }
348
+ function createContextFetcher(config) {
349
+ return new ContextFetcher(config);
350
+ }
351
+
352
+ // src/executor.ts
353
+ import { spawn } from "child_process";
354
+ var DEFAULT_TIMEOUT = 5 * 60 * 1e3;
355
+ var DEFAULT_SYSTEM_PROMPT = `You are an AI agent working within the Artyfacts task management system.
356
+
357
+ Your job is to complete tasks assigned to you using the available tools.
358
+
359
+ ## Available Tools
360
+
361
+ You have access to Artyfacts MCP tools. USE THEM to complete your task:
362
+
363
+ - **create_artifact** - Create new artifacts (documents, specs, reports)
364
+ - **create_section** - Add sections to artifacts (content, tasks, decisions)
365
+ - **update_section** - Update existing sections
366
+ - **create_agent** - Create new AI agents with specific roles
367
+ - **list_artifacts** - Query existing artifacts
368
+ - **list_sections** - Query sections within an artifact
369
+ - **complete_task** - Mark a task as complete
370
+ - **block_task** - Block a task with a reason
371
+ - **create_blocker** - Create a decision blocker
372
+
373
+ IMPORTANT: When asked to create agents or update artifacts, USE THE TOOLS. Don't just describe what you would do - actually do it.
374
+
375
+ ## Guidelines
376
+
377
+ - USE THE TOOLS to take action
378
+ - If creating something, use create_artifact or create_section
379
+ - If creating agents, use create_agent
380
+ - If you cannot complete the task, explain why
381
+
382
+ Format your response as follows:
383
+ 1. First, use the tools to complete the task
384
+ 2. Summarize what you accomplished
385
+ 3. End with a brief summary line starting with "SUMMARY:"`;
386
+ var ClaudeExecutor = class {
387
+ config;
388
+ contextFetcher = null;
389
+ constructor(config = {}) {
390
+ this.config = {
391
+ ...config,
392
+ timeout: config.timeout || DEFAULT_TIMEOUT,
393
+ claudePath: config.claudePath || "claude"
394
+ };
395
+ if (config.baseUrl && config.apiKey) {
396
+ this.contextFetcher = createContextFetcher({
397
+ baseUrl: config.baseUrl,
398
+ apiKey: config.apiKey
399
+ });
400
+ }
401
+ }
402
+ /**
403
+ * Execute a task using Claude Code CLI
404
+ *
405
+ * If full context is available (baseUrl + apiKey configured), fetches
406
+ * organization, project, artifact, and related sections for a rich prompt.
407
+ */
408
+ async execute(task) {
409
+ try {
410
+ let prompt;
411
+ let fullContext = null;
412
+ const useFullContext = this.config.useFullContext !== false && this.contextFetcher;
413
+ if (useFullContext) {
414
+ try {
415
+ fullContext = await this.contextFetcher.fetchTaskContext(task.taskId);
416
+ prompt = buildPromptWithContext(fullContext);
417
+ console.log(" \u{1F4DA} Using full context (org, project, artifact, related sections)");
418
+ } catch (contextError) {
419
+ console.warn(" \u26A0\uFE0F Could not fetch full context, using minimal prompt");
420
+ console.warn(` ${contextError instanceof Error ? contextError.message : contextError}`);
421
+ prompt = this.buildTaskPrompt(task);
422
+ }
423
+ } else {
424
+ prompt = this.buildTaskPrompt(task);
425
+ }
426
+ const output = await this.runClaude(prompt);
427
+ const { content, summary } = this.parseResponse(output, task.heading);
428
+ return {
429
+ success: true,
430
+ output: content,
431
+ summary,
432
+ promptUsed: prompt
433
+ };
434
+ } catch (error) {
435
+ const errorMessage = error instanceof Error ? error.message : String(error);
436
+ return {
437
+ success: false,
438
+ output: "",
439
+ summary: `Failed: ${errorMessage}`,
440
+ error: errorMessage
441
+ };
442
+ }
443
+ }
444
+ /**
445
+ * Run Claude Code CLI with the given prompt
446
+ */
447
+ runClaude(prompt) {
448
+ return new Promise((resolve, reject) => {
449
+ const claudePath = this.config.claudePath || "claude";
450
+ const mcpConfig = {
451
+ mcpServers: {
452
+ artyfacts: {
453
+ command: "npx",
454
+ args: ["-y", "@artyfacts/mcp-server"],
455
+ env: {
456
+ ARTYFACTS_API_KEY: this.config.apiKey || process.env.ARTYFACTS_API_KEY || "",
457
+ ARTYFACTS_BASE_URL: this.config.baseUrl || "https://artyfacts.dev/api/v1"
458
+ }
459
+ }
460
+ }
461
+ };
462
+ const args = [
463
+ "--print",
464
+ "--mcp-config",
465
+ JSON.stringify(mcpConfig),
466
+ "--permission-mode",
467
+ "bypassPermissions"
468
+ ];
469
+ const proc = spawn(claudePath, args, {
470
+ stdio: ["pipe", "pipe", "pipe"],
471
+ timeout: this.config.timeout
472
+ });
473
+ let stdout = "";
474
+ let stderr = "";
475
+ proc.stdout.on("data", (data) => {
476
+ stdout += data.toString();
477
+ });
478
+ proc.stderr.on("data", (data) => {
479
+ stderr += data.toString();
480
+ });
481
+ proc.on("close", (code) => {
482
+ if (code === 0) {
483
+ resolve(stdout.trim());
484
+ } else {
485
+ reject(new Error(stderr || `Claude exited with code ${code}`));
486
+ }
487
+ });
488
+ proc.on("error", (err) => {
489
+ if (err.code === "ENOENT") {
490
+ reject(new Error(
491
+ "Claude Code CLI not found. Please install it:\n npm install -g @anthropic-ai/claude-code"
492
+ ));
493
+ } else {
494
+ reject(err);
495
+ }
496
+ });
497
+ proc.stdin.write(prompt);
498
+ proc.stdin.end();
499
+ });
500
+ }
501
+ /**
502
+ * Build the task prompt
503
+ */
504
+ buildTaskPrompt(task) {
505
+ const parts = [];
506
+ const systemPrompt = this.config.systemPromptPrefix ? `${this.config.systemPromptPrefix}
507
+
508
+ ${DEFAULT_SYSTEM_PROMPT}` : DEFAULT_SYSTEM_PROMPT;
509
+ parts.push(systemPrompt);
510
+ parts.push("");
511
+ parts.push("---");
512
+ parts.push("");
513
+ parts.push(`# Task: ${task.heading}`);
514
+ parts.push("");
515
+ if (task.artifactTitle) {
516
+ parts.push(`**Artifact:** ${task.artifactTitle}`);
517
+ }
518
+ if (task.priority) {
519
+ const priorityLabels = ["High", "Medium", "Low"];
520
+ parts.push(`**Priority:** ${priorityLabels[task.priority - 1] || "Medium"}`);
521
+ }
522
+ parts.push("");
523
+ parts.push("## Description");
524
+ parts.push(task.content || "No additional description provided.");
525
+ parts.push("");
526
+ if (task.context && Object.keys(task.context).length > 0) {
527
+ parts.push("## Additional Context");
528
+ parts.push("```json");
529
+ parts.push(JSON.stringify(task.context, null, 2));
530
+ parts.push("```");
531
+ parts.push("");
532
+ }
533
+ parts.push("## Instructions");
534
+ parts.push("Complete this task and provide your output below.");
535
+ return parts.join("\n");
536
+ }
537
+ /**
538
+ * Parse the response to extract output and summary
539
+ */
540
+ parseResponse(fullOutput, taskHeading) {
541
+ const summaryMatch = fullOutput.match(/SUMMARY:\s*(.+?)(?:\n|$)/i);
542
+ if (summaryMatch) {
543
+ const summary2 = summaryMatch[1].trim();
544
+ const content = fullOutput.replace(/SUMMARY:\s*.+?(?:\n|$)/i, "").trim();
545
+ return { content, summary: summary2 };
546
+ }
547
+ const lines = fullOutput.split("\n").filter((l) => l.trim());
548
+ const firstLine = lines[0] || "";
549
+ const summary = firstLine.length > 100 ? `${firstLine.substring(0, 97)}...` : firstLine || `Completed: ${taskHeading}`;
550
+ return { content: fullOutput, summary };
551
+ }
552
+ /**
553
+ * Test that Claude Code CLI is available and working
554
+ */
555
+ async testConnection() {
556
+ try {
557
+ const output = await this.runClaude('Say "connected" and nothing else.');
558
+ return output.toLowerCase().includes("connected");
559
+ } catch {
560
+ return false;
561
+ }
562
+ }
563
+ /**
564
+ * Check if Claude Code CLI is installed
565
+ */
566
+ async isInstalled() {
567
+ return new Promise((resolve) => {
568
+ const proc = spawn(this.config.claudePath || "claude", ["--version"], {
569
+ stdio: ["ignore", "pipe", "pipe"]
570
+ });
571
+ proc.on("close", (code) => {
572
+ resolve(code === 0);
573
+ });
574
+ proc.on("error", () => {
575
+ resolve(false);
576
+ });
577
+ });
578
+ }
579
+ };
580
+ function createExecutor(config) {
581
+ return new ClaudeExecutor(config);
582
+ }
583
+
584
+ // src/listener.ts
585
+ import EventSource from "eventsource";
586
+ var DEFAULT_BASE_URL2 = "https://artyfacts.dev/api/v1";
587
+ var EVENT_TYPES = [
588
+ "connected",
589
+ "heartbeat",
590
+ "task_assigned",
591
+ "task_unblocked",
592
+ "blocker_resolved",
593
+ "notification",
594
+ "mcp_connect_request",
595
+ "connection_status"
596
+ ];
597
+ var ArtyfactsListener = class {
598
+ config;
599
+ eventSource = null;
600
+ callbacks = /* @__PURE__ */ new Map();
601
+ allCallbacks = /* @__PURE__ */ new Set();
602
+ state = "disconnected";
603
+ reconnectAttempts = 0;
604
+ maxReconnectAttempts = 10;
605
+ reconnectDelay = 1e3;
606
+ constructor(config) {
607
+ if (!config.apiKey) {
608
+ throw new Error("API key is required");
609
+ }
610
+ if (!config.agentId) {
611
+ throw new Error("Agent ID is required");
612
+ }
613
+ this.config = {
614
+ ...config,
615
+ baseUrl: config.baseUrl || DEFAULT_BASE_URL2
616
+ };
617
+ }
618
+ /**
619
+ * Get current connection state
620
+ */
621
+ get connectionState() {
622
+ return this.state;
623
+ }
624
+ /**
625
+ * Check if connected
626
+ */
627
+ get isConnected() {
628
+ return this.state === "connected";
629
+ }
630
+ /**
631
+ * Subscribe to all events
632
+ */
633
+ subscribe(callback) {
634
+ this.allCallbacks.add(callback);
635
+ return () => {
636
+ this.allCallbacks.delete(callback);
637
+ };
638
+ }
639
+ /**
640
+ * Subscribe to a specific event type
641
+ */
642
+ on(type, callback) {
643
+ if (!this.callbacks.has(type)) {
644
+ this.callbacks.set(type, /* @__PURE__ */ new Set());
645
+ }
646
+ this.callbacks.get(type).add(callback);
647
+ return () => {
648
+ const typeCallbacks = this.callbacks.get(type);
649
+ if (typeCallbacks) {
650
+ typeCallbacks.delete(callback);
651
+ if (typeCallbacks.size === 0) {
652
+ this.callbacks.delete(type);
653
+ }
654
+ }
655
+ };
656
+ }
657
+ /**
658
+ * Connect to the SSE stream
659
+ */
660
+ connect() {
661
+ if (this.eventSource) {
662
+ return;
663
+ }
664
+ this.setState("connecting");
665
+ const url = new URL(`${this.config.baseUrl}/events/stream`);
666
+ url.searchParams.set("apiKey", this.config.apiKey);
667
+ url.searchParams.set("agentId", this.config.agentId);
668
+ this.eventSource = new EventSource(url.toString(), {
669
+ headers: {
670
+ "Authorization": `Bearer ${this.config.apiKey}`
671
+ }
672
+ });
673
+ this.eventSource.onopen = () => {
674
+ this.reconnectAttempts = 0;
675
+ this.reconnectDelay = 1e3;
676
+ this.setState("connected");
677
+ };
678
+ this.eventSource.onmessage = (event) => {
679
+ this.handleMessage(event);
680
+ };
681
+ this.eventSource.onerror = (event) => {
682
+ this.handleError(event);
683
+ };
684
+ for (const eventType of EVENT_TYPES) {
685
+ this.eventSource.addEventListener(eventType, (event) => {
686
+ this.handleMessage(event, eventType);
687
+ });
688
+ }
689
+ }
690
+ /**
691
+ * Disconnect from the SSE stream
692
+ */
693
+ disconnect() {
694
+ if (this.eventSource) {
695
+ this.eventSource.close();
696
+ this.eventSource = null;
697
+ }
698
+ this.setState("disconnected");
699
+ }
700
+ /**
701
+ * Reconnect to the SSE stream
702
+ */
703
+ reconnect() {
704
+ this.disconnect();
705
+ this.connect();
706
+ }
707
+ /**
708
+ * Handle incoming SSE message
709
+ */
710
+ handleMessage(event, eventType) {
711
+ try {
712
+ const data = JSON.parse(event.data);
713
+ const rawData = data.data || data;
714
+ const normalizedData = rawData.task_id ? {
715
+ taskId: rawData.task_id,
716
+ sectionId: rawData.section_id,
717
+ artifactId: rawData.artifact_id,
718
+ artifactTitle: rawData.artifact_title,
719
+ heading: rawData.heading,
720
+ content: rawData.content,
721
+ assignedTo: rawData.assigned_to,
722
+ assignedAt: rawData.assigned_at,
723
+ priority: rawData.priority,
724
+ ...rawData
725
+ // Keep original fields too
726
+ } : rawData;
727
+ const artyfactsEvent = {
728
+ type: eventType || data.type || "unknown",
729
+ timestamp: data.timestamp || (/* @__PURE__ */ new Date()).toISOString(),
730
+ data: normalizedData
731
+ };
732
+ const typeCallbacks = this.callbacks.get(artyfactsEvent.type);
733
+ if (typeCallbacks) {
734
+ for (const callback of typeCallbacks) {
735
+ this.safeCallCallback(callback, artyfactsEvent);
736
+ }
737
+ }
738
+ for (const callback of this.allCallbacks) {
739
+ this.safeCallCallback(callback, artyfactsEvent);
740
+ }
741
+ } catch (err) {
742
+ console.error("[Listener] Failed to parse SSE message:", event.data, err);
743
+ }
744
+ }
745
+ /**
746
+ * Safely call a callback, handling async and errors
747
+ */
748
+ async safeCallCallback(callback, event) {
749
+ try {
750
+ await callback(event);
751
+ } catch (err) {
752
+ console.error(`[Listener] Error in event callback for '${event.type}':`, err);
753
+ }
754
+ }
755
+ /**
756
+ * Handle SSE error
757
+ */
758
+ handleError(event) {
759
+ if (this.eventSource?.readyState === EventSource.CONNECTING) {
760
+ this.setState("reconnecting");
761
+ } else if (this.eventSource?.readyState === EventSource.CLOSED) {
762
+ this.setState("disconnected");
763
+ if (this.reconnectAttempts < this.maxReconnectAttempts) {
764
+ this.reconnectAttempts++;
765
+ this.reconnectDelay = Math.min(this.reconnectDelay * 2, 3e4);
766
+ console.log(
767
+ `[Listener] Connection lost, reconnecting in ${this.reconnectDelay / 1e3}s (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`
768
+ );
769
+ setTimeout(() => {
770
+ if (this.state === "disconnected") {
771
+ this.connect();
772
+ }
773
+ }, this.reconnectDelay);
774
+ } else {
775
+ const error = new Error("Max reconnection attempts reached");
776
+ this.config.onError?.(error);
777
+ }
778
+ }
779
+ }
780
+ /**
781
+ * Update connection state
782
+ */
783
+ setState(state) {
784
+ if (this.state !== state) {
785
+ this.state = state;
786
+ this.config.onStateChange?.(state);
787
+ }
788
+ }
789
+ };
790
+ function createListener(config) {
791
+ return new ArtyfactsListener(config);
792
+ }
793
+
794
+ // src/mcp.ts
795
+ import { spawnSync } from "child_process";
796
+ var DEFAULT_BASE_URL3 = "https://artyfacts.dev/api/v1";
797
+ var OAUTH_MCP_SERVERS = {
798
+ supabase: {
799
+ url: "https://mcp.supabase.com/mcp",
800
+ name: "supabase"
801
+ },
802
+ figma: {
803
+ url: "https://mcp.figma.com/mcp",
804
+ name: "figma"
805
+ }
806
+ };
807
+ var CREDENTIAL_MCP_CONFIGS = {
808
+ postgres: (config) => ({
809
+ command: "npx",
810
+ args: ["-y", "@modelcontextprotocol/server-postgres", config.connection_string]
811
+ }),
812
+ github: (config) => ({
813
+ command: "npx",
814
+ args: ["-y", "@modelcontextprotocol/server-github"],
815
+ env: {
816
+ GITHUB_PERSONAL_ACCESS_TOKEN: config.api_key
817
+ }
818
+ }),
819
+ filesystem: (config) => ({
820
+ command: "npx",
821
+ args: ["-y", "@modelcontextprotocol/server-filesystem", ...config.paths || []]
822
+ })
823
+ };
824
+ function addMcpServer(options) {
825
+ const { name, transport, url, command, args = [], env = {}, scope = "user" } = options;
826
+ const cliArgs = ["mcp", "add", "-s", scope, "-t", transport];
827
+ for (const [key, value] of Object.entries(env)) {
828
+ cliArgs.push("-e", `${key}=${value}`);
829
+ }
830
+ cliArgs.push(name);
831
+ if (transport === "http" && url) {
832
+ cliArgs.push(url);
833
+ } else if (transport === "stdio" && command) {
834
+ cliArgs.push("--", command, ...args);
835
+ } else {
836
+ return { success: false, error: "Invalid configuration: missing url or command" };
837
+ }
838
+ console.log(`[MCP] Running: claude ${cliArgs.join(" ")}`);
839
+ const result = spawnSync("claude", cliArgs, {
840
+ encoding: "utf-8",
841
+ stdio: ["pipe", "pipe", "pipe"]
842
+ });
843
+ if (result.status === 0) {
844
+ return { success: true };
845
+ } else {
846
+ const error = result.stderr || result.stdout || `Exit code ${result.status}`;
847
+ if (error.includes("already exists")) {
848
+ console.log(`[MCP] Server ${name} already configured - treating as success`);
849
+ return { success: true, alreadyExists: true };
850
+ }
851
+ return { success: false, error };
852
+ }
853
+ }
854
+ var McpHandler = class {
855
+ config;
856
+ constructor(config) {
857
+ this.config = {
858
+ ...config,
859
+ baseUrl: config.baseUrl || DEFAULT_BASE_URL3
860
+ };
861
+ }
862
+ /**
863
+ * Handle an MCP connect request event
864
+ * Uses `claude mcp add` to configure the server dynamically
865
+ */
866
+ async handleConnectRequest(event) {
867
+ const { connection_id, platform, config: mcpConfig } = event.data;
868
+ try {
869
+ const serverName = mcpConfig?.server_name || platform;
870
+ const oauthServer = OAUTH_MCP_SERVERS[platform];
871
+ if (oauthServer) {
872
+ const result = addMcpServer({
873
+ name: serverName,
874
+ transport: "http",
875
+ url: oauthServer.url
876
+ });
877
+ if (!result.success) {
878
+ throw new Error(`Failed to add ${platform} MCP: ${result.error}`);
879
+ }
880
+ console.log(`[MCP] Added ${serverName} \u2192 ${oauthServer.url}`);
881
+ console.log(`[MCP] OAuth will prompt on first use (one-time login)`);
882
+ } else {
883
+ const configBuilder = CREDENTIAL_MCP_CONFIGS[platform];
884
+ if (!configBuilder) {
885
+ throw new Error(`Unsupported MCP platform: ${platform}. Supported: ${[...Object.keys(OAUTH_MCP_SERVERS), ...Object.keys(CREDENTIAL_MCP_CONFIGS)].join(", ")}`);
886
+ }
887
+ if (!mcpConfig) {
888
+ throw new Error(`Platform ${platform} requires configuration (connection_string or api_key)`);
889
+ }
890
+ const serverConfig = configBuilder(mcpConfig);
891
+ const result = addMcpServer({
892
+ name: serverName,
893
+ transport: "stdio",
894
+ command: serverConfig.command,
895
+ args: serverConfig.args,
896
+ env: serverConfig.env
897
+ });
898
+ if (!result.success) {
899
+ throw new Error(`Failed to add ${platform} MCP: ${result.error}`);
900
+ }
901
+ console.log(`[MCP] Added ${serverName} for ${platform}`);
902
+ }
903
+ await this.updateConnectionStatus(connection_id, {
904
+ status: "active",
905
+ mcp_configured: true
906
+ });
907
+ this.config.onConfigured?.(connection_id, platform);
908
+ } catch (err) {
909
+ console.error(`[MCP] Failed to configure ${platform}:`, err);
910
+ await this.updateConnectionStatus(connection_id, {
911
+ status: "error",
912
+ mcp_configured: false,
913
+ error_message: err.message
914
+ });
915
+ this.config.onError?.(err, connection_id);
916
+ }
917
+ }
918
+ /**
919
+ * Check if a platform supports OAuth (no credentials needed)
920
+ */
921
+ static supportsOAuth(platform) {
922
+ return platform in OAUTH_MCP_SERVERS;
923
+ }
924
+ /**
925
+ * Get list of platforms with OAuth support
926
+ */
927
+ static getOAuthPlatforms() {
928
+ return Object.keys(OAUTH_MCP_SERVERS);
929
+ }
930
+ /**
931
+ * Update connection status in Artyfacts
932
+ */
933
+ async updateConnectionStatus(connectionId, update) {
934
+ const url = `${this.config.baseUrl}/connections/${connectionId}`;
935
+ console.log(`[MCP] Updating connection ${connectionId} to status: ${update.status}`);
936
+ try {
937
+ const response = await fetch(url, {
938
+ method: "PATCH",
939
+ headers: {
940
+ "Authorization": `Bearer ${this.config.apiKey}`,
941
+ "Content-Type": "application/json"
942
+ },
943
+ body: JSON.stringify(update)
944
+ });
945
+ if (!response.ok) {
946
+ const body = await response.text();
947
+ console.error(`[MCP] Failed to update connection status: ${response.status} - ${body}`);
948
+ } else {
949
+ console.log(`[MCP] Connection ${connectionId} updated to ${update.status}`);
950
+ }
951
+ } catch (err) {
952
+ console.error("[MCP] Failed to update connection status:", err);
953
+ }
954
+ }
955
+ /**
956
+ * List configured MCP servers using `claude mcp list`
957
+ */
958
+ listServers() {
959
+ const result = spawnSync("claude", ["mcp", "list"], {
960
+ encoding: "utf-8",
961
+ stdio: ["pipe", "pipe", "pipe"]
962
+ });
963
+ if (result.status === 0 && result.stdout) {
964
+ return result.stdout.trim().split("\n").filter(Boolean);
965
+ }
966
+ return [];
967
+ }
968
+ /**
969
+ * Remove an MCP server using `claude mcp remove`
970
+ */
971
+ removeServer(serverName) {
972
+ const result = spawnSync("claude", ["mcp", "remove", serverName], {
973
+ encoding: "utf-8",
974
+ stdio: ["pipe", "pipe", "pipe"]
975
+ });
976
+ if (result.status === 0) {
977
+ console.log(`[MCP] Removed server: ${serverName}`);
978
+ return true;
979
+ } else {
980
+ console.error(`[MCP] Failed to remove ${serverName}: ${result.stderr || result.stdout}`);
981
+ return false;
982
+ }
983
+ }
984
+ };
985
+ function createMcpHandler(config) {
986
+ return new McpHandler(config);
987
+ }
988
+
989
+ export {
990
+ loadCredentials,
991
+ saveCredentials,
992
+ clearCredentials,
993
+ runDeviceAuth,
994
+ promptForApiKey,
995
+ getCredentials,
996
+ ContextFetcher,
997
+ buildPromptWithContext,
998
+ createContextFetcher,
999
+ ClaudeExecutor,
1000
+ createExecutor,
1001
+ ArtyfactsListener,
1002
+ createListener,
1003
+ McpHandler,
1004
+ createMcpHandler
1005
+ };
package/dist/cli.js CHANGED
@@ -909,7 +909,7 @@ var McpHandler = class {
909
909
  throw new Error(`Failed to add ${platform} MCP: ${result.error}`);
910
910
  }
911
911
  console.log(`[MCP] Added ${serverName} \u2192 ${oauthServer.url}`);
912
- console.log(`[MCP] OAuth will prompt when you use ${platform} tools`);
912
+ console.log(`[MCP] OAuth will prompt on first use (one-time login)`);
913
913
  } else {
914
914
  const configBuilder = CREDENTIAL_MCP_CONFIGS[platform];
915
915
  if (!configBuilder) {
@@ -1293,7 +1293,7 @@ async function runAgent(options) {
1293
1293
  onConfigured: (connectionId, platform) => {
1294
1294
  console.log(`
1295
1295
  \u{1F527} [MCP] Configured ${platform} connection: ${connectionId}`);
1296
- console.log(" \u{1F4A1} Try using the tools now - OAuth will prompt when needed");
1296
+ console.log(" \u{1F4A1} Try using the tools now - OAuth prompts once on first use");
1297
1297
  },
1298
1298
  onError: (error, connectionId) => {
1299
1299
  console.error(`
package/dist/cli.mjs CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  getCredentials,
8
8
  loadCredentials,
9
9
  promptForApiKey
10
- } from "./chunk-4L6FPX5K.mjs";
10
+ } from "./chunk-CSODKZR3.mjs";
11
11
 
12
12
  // src/cli.ts
13
13
  import { Command } from "commander";
@@ -287,7 +287,7 @@ async function runAgent(options) {
287
287
  onConfigured: (connectionId, platform) => {
288
288
  console.log(`
289
289
  \u{1F527} [MCP] Configured ${platform} connection: ${connectionId}`);
290
- console.log(" \u{1F4A1} Try using the tools now - OAuth will prompt when needed");
290
+ console.log(" \u{1F4A1} Try using the tools now - OAuth prompts once on first use");
291
291
  },
292
292
  onError: (error, connectionId) => {
293
293
  console.error(`
package/dist/index.js CHANGED
@@ -6102,7 +6102,7 @@ var McpHandler = class {
6102
6102
  throw new Error(`Failed to add ${platform} MCP: ${result.error}`);
6103
6103
  }
6104
6104
  console.log(`[MCP] Added ${serverName} \u2192 ${oauthServer.url}`);
6105
- console.log(`[MCP] OAuth will prompt when you use ${platform} tools`);
6105
+ console.log(`[MCP] OAuth will prompt on first use (one-time login)`);
6106
6106
  } else {
6107
6107
  const configBuilder = CREDENTIAL_MCP_CONFIGS[platform];
6108
6108
  if (!configBuilder) {
package/dist/index.mjs CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  promptForApiKey,
15
15
  runDeviceAuth,
16
16
  saveCredentials
17
- } from "./chunk-4L6FPX5K.mjs";
17
+ } from "./chunk-CSODKZR3.mjs";
18
18
 
19
19
  // node_modules/@anthropic-ai/sdk/internal/tslib.mjs
20
20
  function __classPrivateFieldSet(receiver, state, value, kind, f) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@artyfacts/claude",
3
- "version": "1.3.13",
3
+ "version": "1.3.14",
4
4
  "description": "Claude adapter for Artyfacts - Execute tasks using Claude Code CLI",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
package/src/cli.ts CHANGED
@@ -438,7 +438,7 @@ async function runAgent(options: {
438
438
  baseUrl: options.baseUrl,
439
439
  onConfigured: (connectionId, platform) => {
440
440
  console.log(`\nšŸ”§ [MCP] Configured ${platform} connection: ${connectionId}`);
441
- console.log(' šŸ’” Try using the tools now - OAuth will prompt when needed');
441
+ console.log(' šŸ’” Try using the tools now - OAuth prompts once on first use');
442
442
  },
443
443
  onError: (error, connectionId) => {
444
444
  console.error(`\nāŒ [MCP] Failed to configure connection ${connectionId}:`, error.message);
package/src/mcp.ts CHANGED
@@ -186,7 +186,7 @@ export class McpHandler {
186
186
  }
187
187
 
188
188
  console.log(`[MCP] Added ${serverName} → ${oauthServer.url}`);
189
- console.log(`[MCP] OAuth will prompt when you use ${platform} tools`);
189
+ console.log(`[MCP] OAuth will prompt on first use (one-time login)`);
190
190
  } else {
191
191
  // Command-based MCP server - needs credentials
192
192
  const configBuilder = CREDENTIAL_MCP_CONFIGS[platform];