@stackmemoryai/stackmemory 0.5.25 ā 0.5.27
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/cli/claude-sm.js +94 -4
- package/dist/cli/claude-sm.js.map +2 -2
- package/dist/cli/index.js +0 -2
- package/dist/cli/index.js.map +2 -2
- package/dist/hooks/events.js +4 -1
- package/dist/hooks/events.js.map +2 -2
- package/dist/hooks/sms-webhook.js +116 -2
- package/dist/hooks/sms-webhook.js.map +2 -2
- package/dist/skills/api-discovery.js +3 -2
- package/dist/skills/api-discovery.js.map +2 -2
- package/package.json +3 -8
- package/scripts/debug-railway-build.js +0 -87
- package/scripts/deployment/railway.sh +0 -352
- package/scripts/railway-env-setup.sh +0 -39
- package/scripts/setup-railway-deployment.sh +0 -37
- package/scripts/sync-frames-from-railway.js +0 -228
- package/scripts/test-railway-db.js +0 -222
- package/scripts/validate-railway-deployment.js +0 -137
- package/scripts/verify-railway-schema.ts +0 -35
package/dist/hooks/events.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/events.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * StackMemory Hook Events\n * Event types and emitter for the hook system\n */\n\nimport { EventEmitter } from 'events';\n\nexport type HookEventType =\n | 'input_idle'\n | 'file_change'\n | 'context_switch'\n | 'session_start'\n | 'session_end'\n | 'prompt_submit'\n | 'tool_use'\n | 'suggestion_ready'\n | 'error';\n\nexport interface HookEvent {\n type: HookEventType;\n timestamp: number;\n data: Record<string, unknown>;\n}\n\nexport interface FileChangeEvent extends HookEvent {\n type: 'file_change';\n data: {\n path: string;\n changeType: 'create' | 'modify' | 'delete';\n content?: string;\n };\n}\n\nexport interface InputIdleEvent extends HookEvent {\n type: 'input_idle';\n data: {\n idleDuration: number;\n lastInput?: string;\n };\n}\n\nexport interface ContextSwitchEvent extends HookEvent {\n type: 'context_switch';\n data: {\n fromBranch?: string;\n toBranch?: string;\n fromProject?: string;\n toProject?: string;\n };\n}\n\nexport interface SuggestionReadyEvent extends HookEvent {\n type: 'suggestion_ready';\n data: {\n suggestion: string;\n source: string;\n confidence?: number;\n preview?: string;\n };\n}\n\nexport type HookEventData =\n | FileChangeEvent\n | InputIdleEvent\n | ContextSwitchEvent\n | SuggestionReadyEvent\n | HookEvent;\n\nexport type HookHandler = (event: HookEventData) => Promise<void> | void;\n\nexport class HookEventEmitter extends EventEmitter {\n private handlers: Map<HookEventType, Set<HookHandler>> = new Map();\n\n registerHandler(eventType: HookEventType, handler: HookHandler): void {\n if (!this.handlers.has(eventType)) {\n this.handlers.set(eventType, new Set());\n }\n this.handlers.get(eventType)
|
|
5
|
-
"mappings": ";;;;AAKA,SAAS,oBAAoB;AAiEtB,MAAM,yBAAyB,aAAa;AAAA,EACzC,WAAiD,oBAAI,IAAI;AAAA,EAEjE,gBAAgB,WAA0B,SAA4B;AACpE,QAAI,CAAC,KAAK,SAAS,IAAI,SAAS,GAAG;AACjC,WAAK,SAAS,IAAI,WAAW,oBAAI,IAAI,CAAC;AAAA,IACxC;AACA,
|
|
4
|
+
"sourcesContent": ["/**\n * StackMemory Hook Events\n * Event types and emitter for the hook system\n */\n\nimport { EventEmitter } from 'events';\n\nexport type HookEventType =\n | 'input_idle'\n | 'file_change'\n | 'context_switch'\n | 'session_start'\n | 'session_end'\n | 'prompt_submit'\n | 'tool_use'\n | 'suggestion_ready'\n | 'error';\n\nexport interface HookEvent {\n type: HookEventType;\n timestamp: number;\n data: Record<string, unknown>;\n}\n\nexport interface FileChangeEvent extends HookEvent {\n type: 'file_change';\n data: {\n path: string;\n changeType: 'create' | 'modify' | 'delete';\n content?: string;\n };\n}\n\nexport interface InputIdleEvent extends HookEvent {\n type: 'input_idle';\n data: {\n idleDuration: number;\n lastInput?: string;\n };\n}\n\nexport interface ContextSwitchEvent extends HookEvent {\n type: 'context_switch';\n data: {\n fromBranch?: string;\n toBranch?: string;\n fromProject?: string;\n toProject?: string;\n };\n}\n\nexport interface SuggestionReadyEvent extends HookEvent {\n type: 'suggestion_ready';\n data: {\n suggestion: string;\n source: string;\n confidence?: number;\n preview?: string;\n };\n}\n\nexport type HookEventData =\n | FileChangeEvent\n | InputIdleEvent\n | ContextSwitchEvent\n | SuggestionReadyEvent\n | HookEvent;\n\nexport type HookHandler = (event: HookEventData) => Promise<void> | void;\n\nexport class HookEventEmitter extends EventEmitter {\n private handlers: Map<HookEventType, Set<HookHandler>> = new Map();\n\n registerHandler(eventType: HookEventType, handler: HookHandler): void {\n if (!this.handlers.has(eventType)) {\n this.handlers.set(eventType, new Set());\n }\n const handlers = this.handlers.get(eventType);\n if (handlers) {\n handlers.add(handler);\n }\n this.on(eventType, handler);\n }\n\n unregisterHandler(eventType: HookEventType, handler: HookHandler): void {\n const handlers = this.handlers.get(eventType);\n if (handlers) {\n handlers.delete(handler);\n this.off(eventType, handler);\n }\n }\n\n async emitHook(event: HookEventData): Promise<void> {\n const handlers = this.handlers.get(event.type);\n if (!handlers || handlers.size === 0) {\n return;\n }\n\n const promises: Promise<void>[] = [];\n for (const handler of handlers) {\n try {\n const result = handler(event);\n if (result instanceof Promise) {\n promises.push(result);\n }\n } catch (error) {\n this.emit('error', {\n type: 'error',\n timestamp: Date.now(),\n data: { error, originalEvent: event },\n });\n }\n }\n\n await Promise.allSettled(promises);\n }\n\n getRegisteredEvents(): HookEventType[] {\n return Array.from(this.handlers.keys()).filter(\n (type) => (this.handlers.get(type)?.size ?? 0) > 0\n );\n }\n}\n\nexport const hookEmitter = new HookEventEmitter();\n"],
|
|
5
|
+
"mappings": ";;;;AAKA,SAAS,oBAAoB;AAiEtB,MAAM,yBAAyB,aAAa;AAAA,EACzC,WAAiD,oBAAI,IAAI;AAAA,EAEjE,gBAAgB,WAA0B,SAA4B;AACpE,QAAI,CAAC,KAAK,SAAS,IAAI,SAAS,GAAG;AACjC,WAAK,SAAS,IAAI,WAAW,oBAAI,IAAI,CAAC;AAAA,IACxC;AACA,UAAM,WAAW,KAAK,SAAS,IAAI,SAAS;AAC5C,QAAI,UAAU;AACZ,eAAS,IAAI,OAAO;AAAA,IACtB;AACA,SAAK,GAAG,WAAW,OAAO;AAAA,EAC5B;AAAA,EAEA,kBAAkB,WAA0B,SAA4B;AACtE,UAAM,WAAW,KAAK,SAAS,IAAI,SAAS;AAC5C,QAAI,UAAU;AACZ,eAAS,OAAO,OAAO;AACvB,WAAK,IAAI,WAAW,OAAO;AAAA,IAC7B;AAAA,EACF;AAAA,EAEA,MAAM,SAAS,OAAqC;AAClD,UAAM,WAAW,KAAK,SAAS,IAAI,MAAM,IAAI;AAC7C,QAAI,CAAC,YAAY,SAAS,SAAS,GAAG;AACpC;AAAA,IACF;AAEA,UAAM,WAA4B,CAAC;AACnC,eAAW,WAAW,UAAU;AAC9B,UAAI;AACF,cAAM,SAAS,QAAQ,KAAK;AAC5B,YAAI,kBAAkB,SAAS;AAC7B,mBAAS,KAAK,MAAM;AAAA,QACtB;AAAA,MACF,SAAS,OAAO;AACd,aAAK,KAAK,SAAS;AAAA,UACjB,MAAM;AAAA,UACN,WAAW,KAAK,IAAI;AAAA,UACpB,MAAM,EAAE,OAAO,eAAe,MAAM;AAAA,QACtC,CAAC;AAAA,MACH;AAAA,IACF;AAEA,UAAM,QAAQ,WAAW,QAAQ;AAAA,EACnC;AAAA,EAEA,sBAAuC;AACrC,WAAO,MAAM,KAAK,KAAK,SAAS,KAAK,CAAC,EAAE;AAAA,MACtC,CAAC,UAAU,KAAK,SAAS,IAAI,IAAI,GAAG,QAAQ,KAAK;AAAA,IACnD;AAAA,EACF;AACF;AAEO,MAAM,cAAc,IAAI,iBAAiB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -12,7 +12,8 @@ import { execFileSync } from "child_process";
|
|
|
12
12
|
import {
|
|
13
13
|
processIncomingResponse,
|
|
14
14
|
loadSMSConfig,
|
|
15
|
-
cleanupExpiredPrompts
|
|
15
|
+
cleanupExpiredPrompts,
|
|
16
|
+
sendNotification
|
|
16
17
|
} from "./sms-notify.js";
|
|
17
18
|
import {
|
|
18
19
|
queueAction,
|
|
@@ -93,6 +94,58 @@ function storeLatestResponse(promptId, response, action) {
|
|
|
93
94
|
})
|
|
94
95
|
);
|
|
95
96
|
}
|
|
97
|
+
function storeIncomingRequest(from, message) {
|
|
98
|
+
ensureSecureDir(join(homedir(), ".stackmemory"));
|
|
99
|
+
const requestPath = join(
|
|
100
|
+
homedir(),
|
|
101
|
+
".stackmemory",
|
|
102
|
+
"sms-incoming-request.json"
|
|
103
|
+
);
|
|
104
|
+
writeFileSecure(
|
|
105
|
+
requestPath,
|
|
106
|
+
JSON.stringify({
|
|
107
|
+
from,
|
|
108
|
+
message,
|
|
109
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
110
|
+
processed: false
|
|
111
|
+
})
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
function getIncomingRequest() {
|
|
115
|
+
const requestPath = join(
|
|
116
|
+
homedir(),
|
|
117
|
+
".stackmemory",
|
|
118
|
+
"sms-incoming-request.json"
|
|
119
|
+
);
|
|
120
|
+
if (!existsSync(requestPath)) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
const data = JSON.parse(readFileSync(requestPath, "utf-8"));
|
|
125
|
+
if (data.processed) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
return data;
|
|
129
|
+
} catch {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function markRequestProcessed() {
|
|
134
|
+
const requestPath = join(
|
|
135
|
+
homedir(),
|
|
136
|
+
".stackmemory",
|
|
137
|
+
"sms-incoming-request.json"
|
|
138
|
+
);
|
|
139
|
+
if (!existsSync(requestPath)) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
const data = JSON.parse(readFileSync(requestPath, "utf-8"));
|
|
144
|
+
data.processed = true;
|
|
145
|
+
writeFileSecure(requestPath, JSON.stringify(data));
|
|
146
|
+
} catch {
|
|
147
|
+
}
|
|
148
|
+
}
|
|
96
149
|
async function handleSMSWebhook(payload) {
|
|
97
150
|
const { From, Body } = payload;
|
|
98
151
|
if (Body && Body.length > MAX_SMS_BODY_LENGTH) {
|
|
@@ -111,7 +164,11 @@ async function handleSMSWebhook(payload) {
|
|
|
111
164
|
response: `Invalid response. Expected: ${result.prompt.options.map((o) => o.key).join(", ")}`
|
|
112
165
|
};
|
|
113
166
|
}
|
|
114
|
-
|
|
167
|
+
storeIncomingRequest(From, Body);
|
|
168
|
+
console.log(
|
|
169
|
+
`[sms-webhook] Stored new request from ${From}: ${Body.substring(0, 50)}...`
|
|
170
|
+
);
|
|
171
|
+
return { response: "Got it! Your request has been queued." };
|
|
115
172
|
}
|
|
116
173
|
storeLatestResponse(
|
|
117
174
|
result.prompt?.id || "unknown",
|
|
@@ -317,6 +374,61 @@ function startWebhookServer(port = 3456) {
|
|
|
317
374
|
);
|
|
318
375
|
return;
|
|
319
376
|
}
|
|
377
|
+
if (url.pathname === "/request" && req.method === "GET") {
|
|
378
|
+
const request = getIncomingRequest();
|
|
379
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
380
|
+
res.end(JSON.stringify({ request }));
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
if (url.pathname === "/request/ack" && req.method === "POST") {
|
|
384
|
+
markRequestProcessed();
|
|
385
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
386
|
+
res.end(JSON.stringify({ success: true }));
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
if (url.pathname === "/send" && req.method === "POST") {
|
|
390
|
+
let body = "";
|
|
391
|
+
req.on("data", (chunk) => {
|
|
392
|
+
body += chunk;
|
|
393
|
+
if (body.length > MAX_BODY_SIZE) {
|
|
394
|
+
req.destroy();
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
req.on("end", async () => {
|
|
398
|
+
try {
|
|
399
|
+
const payload = JSON.parse(body);
|
|
400
|
+
const message = payload.message || payload.body || "";
|
|
401
|
+
const title = payload.title || "Notification";
|
|
402
|
+
const type = payload.type || "custom";
|
|
403
|
+
if (!message) {
|
|
404
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
405
|
+
res.end(
|
|
406
|
+
JSON.stringify({ success: false, error: "Message required" })
|
|
407
|
+
);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const result = await sendNotification({
|
|
411
|
+
type,
|
|
412
|
+
title,
|
|
413
|
+
message
|
|
414
|
+
});
|
|
415
|
+
res.writeHead(result.success ? 200 : 500, {
|
|
416
|
+
"Content-Type": "application/json"
|
|
417
|
+
});
|
|
418
|
+
res.end(JSON.stringify(result));
|
|
419
|
+
} catch (err) {
|
|
420
|
+
console.error("[sms-webhook] Send error:", err);
|
|
421
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
422
|
+
res.end(
|
|
423
|
+
JSON.stringify({
|
|
424
|
+
success: false,
|
|
425
|
+
error: err instanceof Error ? err.message : "Send failed"
|
|
426
|
+
})
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
320
432
|
res.writeHead(404);
|
|
321
433
|
res.end("Not found");
|
|
322
434
|
}
|
|
@@ -358,7 +470,9 @@ if (process.argv[1]?.endsWith("sms-webhook.js")) {
|
|
|
358
470
|
startWebhookServer(port);
|
|
359
471
|
}
|
|
360
472
|
export {
|
|
473
|
+
getIncomingRequest,
|
|
361
474
|
handleSMSWebhook,
|
|
475
|
+
markRequestProcessed,
|
|
362
476
|
smsWebhookMiddleware,
|
|
363
477
|
startWebhookServer
|
|
364
478
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/sms-webhook.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * SMS Webhook Handler for receiving Twilio responses\n * Can run as standalone server or integrate with existing Express app\n *\n * Security features:\n * - Twilio signature verification\n * - Rate limiting per IP\n * - Body size limits\n * - Content-type validation\n * - Safe action execution (no shell injection)\n */\n\nimport { createServer, IncomingMessage, ServerResponse } from 'http';\nimport { parse as parseUrl } from 'url';\nimport { existsSync, readFileSync } from 'fs';\nimport { join } from 'path';\nimport { homedir } from 'os';\nimport { createHmac } from 'crypto';\nimport { execFileSync } from 'child_process';\nimport {\n processIncomingResponse,\n loadSMSConfig,\n cleanupExpiredPrompts,\n} from './sms-notify.js';\nimport {\n queueAction,\n executeActionSafe,\n cleanupOldActions,\n} from './sms-action-runner.js';\nimport { writeFileSecure, ensureSecureDir } from './secure-fs.js';\nimport {\n logWebhookRequest,\n logRateLimit,\n logSignatureInvalid,\n logBodyTooLarge,\n logContentTypeInvalid,\n logActionAllowed,\n logActionBlocked,\n logCleanup,\n} from './security-logger.js';\n\n// Cleanup interval (5 minutes)\nconst CLEANUP_INTERVAL_MS = 5 * 60 * 1000;\n\n// Input validation constants\nconst MAX_SMS_BODY_LENGTH = 1000;\nconst MAX_PHONE_LENGTH = 20;\n\n// Security constants\nconst MAX_BODY_SIZE = 50 * 1024; // 50KB max body\nconst RATE_LIMIT_WINDOW_MS = 60 * 1000; // 1 minute\nconst RATE_LIMIT_MAX_REQUESTS = 30; // 30 requests per minute per IP\n\n// Rate limiting store (in production, use Redis)\nconst rateLimitStore = new Map<string, { count: number; resetTime: number }>();\n\nfunction checkRateLimit(ip: string): boolean {\n const now = Date.now();\n const record = rateLimitStore.get(ip);\n\n if (!record || now > record.resetTime) {\n rateLimitStore.set(ip, { count: 1, resetTime: now + RATE_LIMIT_WINDOW_MS });\n return true;\n }\n\n if (record.count >= RATE_LIMIT_MAX_REQUESTS) {\n return false;\n }\n\n record.count++;\n return true;\n}\n\n// Twilio signature verification\nfunction verifyTwilioSignature(\n url: string,\n params: Record<string, string>,\n signature: string\n): boolean {\n const authToken = process.env['TWILIO_AUTH_TOKEN'];\n if (!authToken) {\n console.warn(\n '[sms-webhook] TWILIO_AUTH_TOKEN not set, skipping signature verification'\n );\n return true; // Allow in development, but log warning\n }\n\n // Build the data string (URL + sorted params)\n const sortedKeys = Object.keys(params).sort();\n let data = url;\n for (const key of sortedKeys) {\n data += key + params[key];\n }\n\n // Calculate expected signature\n const hmac = createHmac('sha1', authToken);\n hmac.update(data);\n const expectedSignature = hmac.digest('base64');\n\n return signature === expectedSignature;\n}\n\ninterface TwilioWebhookPayload {\n From: string;\n To: string;\n Body: string;\n MessageSid: string;\n}\n\nfunction parseFormData(body: string): Record<string, string> {\n const params = new URLSearchParams(body);\n const result: Record<string, string> = {};\n params.forEach((value, key) => {\n result[key] = value;\n });\n return result;\n}\n\n// Store response for Claude hook to pick up\nfunction storeLatestResponse(\n promptId: string,\n response: string,\n action?: string\n): void {\n ensureSecureDir(join(homedir(), '.stackmemory'));\n const responsePath = join(\n homedir(),\n '.stackmemory',\n 'sms-latest-response.json'\n );\n writeFileSecure(\n responsePath,\n JSON.stringify({\n promptId,\n response,\n action,\n timestamp: new Date().toISOString(),\n })\n );\n}\n\nexport async function handleSMSWebhook(payload: TwilioWebhookPayload): Promise<{\n response: string;\n action?: string;\n queued?: boolean;\n}> {\n const { From, Body } = payload;\n\n // Input length validation\n if (Body && Body.length > MAX_SMS_BODY_LENGTH) {\n console.log(`[sms-webhook] Body too long: ${Body.length} chars`);\n return { response: 'Message too long. Max 1000 characters.' };\n }\n\n if (From && From.length > MAX_PHONE_LENGTH) {\n console.log(`[sms-webhook] Invalid phone number length`);\n return { response: 'Invalid phone number.' };\n }\n\n console.log(`[sms-webhook] Received from ${From}: ${Body}`);\n\n const result = processIncomingResponse(From, Body);\n\n if (!result.matched) {\n if (result.prompt) {\n return {\n response: `Invalid response. Expected: ${result.prompt.options.map((o) => o.key).join(', ')}`,\n };\n }\n return { response: 'No pending prompt found.' };\n }\n\n // Store response for Claude hook\n storeLatestResponse(\n result.prompt?.id || 'unknown',\n result.response || Body,\n result.action\n );\n\n // Trigger notification to alert user/Claude\n triggerResponseNotification(result.response || Body);\n\n // Execute action safely if present (no shell injection)\n if (result.action) {\n console.log(`[sms-webhook] Executing action: ${result.action}`);\n\n const actionResult = await executeActionSafe(\n result.action,\n result.response || Body\n );\n\n if (actionResult.success) {\n logActionAllowed('sms-webhook', result.action);\n console.log(\n `[sms-webhook] Action completed: ${(actionResult.output || '').substring(0, 200)}`\n );\n\n return {\n response: `Done! Action executed successfully.`,\n action: result.action,\n queued: false,\n };\n } else {\n logActionBlocked(\n 'sms-webhook',\n result.action,\n actionResult.error || 'unknown'\n );\n console.log(`[sms-webhook] Action failed: ${actionResult.error}`);\n\n // Queue for retry\n queueAction(\n result.prompt?.id || 'unknown',\n result.response || Body,\n result.action\n );\n\n return {\n response: `Action failed, queued for retry: ${(actionResult.error || '').substring(0, 50)}`,\n action: result.action,\n queued: true,\n };\n }\n }\n\n return {\n response: `Received: ${result.response}. Next action will be triggered.`,\n };\n}\n\n// Escape string for AppleScript (prevent injection)\nfunction escapeAppleScript(str: string): string {\n return str\n .replace(/\\\\/g, '\\\\\\\\')\n .replace(/\"/g, '\\\\\"')\n .replace(/\\n/g, '\\\\n')\n .replace(/\\r/g, '\\\\r')\n .substring(0, 200); // Limit length\n}\n\n// Trigger notification when response received\nfunction triggerResponseNotification(response: string): void {\n const safeMessage = escapeAppleScript(`SMS Response: ${response}`);\n\n // macOS notification using execFile (safer than execSync with shell)\n try {\n execFileSync(\n 'osascript',\n [\n '-e',\n `display notification \"${safeMessage}\" with title \"StackMemory\" sound name \"Glass\"`,\n ],\n { stdio: 'ignore', timeout: 5000 }\n );\n } catch {\n // Ignore if not on macOS\n }\n\n // Write signal file for other processes\n try {\n const signalPath = join(homedir(), '.stackmemory', 'sms-signal.txt');\n writeFileSecure(\n signalPath,\n JSON.stringify({\n type: 'sms_response',\n response,\n timestamp: new Date().toISOString(),\n })\n );\n } catch {\n // Ignore\n }\n\n console.log(`\\n*** SMS RESPONSE RECEIVED: \"${response}\" ***`);\n console.log(`*** Run: stackmemory notify run-actions ***\\n`);\n}\n\n// TwiML response helper\nfunction twimlResponse(message: string): string {\n return `<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response>\n <Message>${escapeXml(message)}</Message>\n</Response>`;\n}\n\nfunction escapeXml(str: string): string {\n return str\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"')\n .replace(/'/g, ''');\n}\n\n// Standalone webhook server\nexport function startWebhookServer(port: number = 3456): void {\n const server = createServer(\n async (req: IncomingMessage, res: ServerResponse) => {\n const url = parseUrl(req.url || '/', true);\n\n // Health check\n if (url.pathname === '/health') {\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ status: 'ok' }));\n return;\n }\n\n // SMS webhook endpoint (incoming messages)\n if (\n (url.pathname === '/sms' ||\n url.pathname === '/sms/incoming' ||\n url.pathname === '/webhook') &&\n req.method === 'POST'\n ) {\n const clientIp = req.socket.remoteAddress || 'unknown';\n\n // Log webhook request\n logWebhookRequest(\n 'sms-webhook',\n req.method || 'POST',\n url.pathname || '/sms',\n clientIp\n );\n\n // Rate limiting\n if (!checkRateLimit(clientIp)) {\n logRateLimit('sms-webhook', clientIp);\n res.writeHead(429, {\n 'Content-Type': 'text/xml',\n 'Retry-After': '60',\n });\n res.end(twimlResponse('Too many requests. Please try again later.'));\n return;\n }\n\n // Content-type validation\n const contentType = req.headers['content-type'] || '';\n if (!contentType.includes('application/x-www-form-urlencoded')) {\n logContentTypeInvalid('sms-webhook', contentType, clientIp);\n res.writeHead(400, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Invalid content type'));\n return;\n }\n\n let body = '';\n let bodyTooLarge = false;\n\n req.on('data', (chunk) => {\n body += chunk;\n // Body size limit\n if (body.length > MAX_BODY_SIZE) {\n bodyTooLarge = true;\n logBodyTooLarge('sms-webhook', body.length, clientIp);\n req.destroy();\n }\n });\n\n req.on('end', async () => {\n if (bodyTooLarge) {\n res.writeHead(413, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Request too large'));\n return;\n }\n\n try {\n const payload = parseFormData(\n body\n ) as unknown as TwilioWebhookPayload;\n\n // Verify Twilio signature\n const twilioSignature = req.headers['x-twilio-signature'] as string;\n const webhookUrl = `${req.headers['x-forwarded-proto'] || 'http'}://${req.headers.host}${req.url}`;\n\n if (\n twilioSignature &&\n !verifyTwilioSignature(\n webhookUrl,\n payload as unknown as Record<string, string>,\n twilioSignature\n )\n ) {\n logSignatureInvalid('sms-webhook', clientIp);\n console.error('[sms-webhook] Invalid Twilio signature');\n res.writeHead(401, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Unauthorized'));\n return;\n }\n\n const result = await handleSMSWebhook(payload);\n\n res.writeHead(200, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse(result.response));\n } catch (err) {\n console.error('[sms-webhook] Error:', err);\n res.writeHead(500, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Error processing message'));\n }\n });\n return;\n }\n\n // Status callback endpoint (delivery status updates)\n if (url.pathname === '/sms/status' && req.method === 'POST') {\n let body = '';\n req.on('data', (chunk) => {\n body += chunk;\n });\n\n req.on('end', () => {\n try {\n const payload = parseFormData(body);\n console.log(\n `[sms-webhook] Status update: ${payload['MessageSid']} -> ${payload['MessageStatus']}`\n );\n\n // Store status for tracking\n const statusPath = join(\n homedir(),\n '.stackmemory',\n 'sms-status.json'\n );\n const statuses: Record<string, string> = existsSync(statusPath)\n ? JSON.parse(readFileSync(statusPath, 'utf8'))\n : {};\n statuses[payload['MessageSid']] = payload['MessageStatus'];\n writeFileSecure(statusPath, JSON.stringify(statuses, null, 2));\n\n res.writeHead(200, { 'Content-Type': 'text/plain' });\n res.end('OK');\n } catch (err) {\n console.error('[sms-webhook] Status error:', err);\n res.writeHead(500);\n res.end('Error');\n }\n });\n return;\n }\n\n // Server status endpoint\n if (url.pathname === '/status') {\n const config = loadSMSConfig();\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n enabled: config.enabled,\n pendingPrompts: config.pendingPrompts.length,\n })\n );\n return;\n }\n\n res.writeHead(404);\n res.end('Not found');\n }\n );\n\n server.listen(port, () => {\n console.log(`[sms-webhook] Server listening on port ${port}`);\n console.log(\n `[sms-webhook] Incoming messages: http://localhost:${port}/sms/incoming`\n );\n console.log(\n `[sms-webhook] Status callback: http://localhost:${port}/sms/status`\n );\n console.log(`[sms-webhook] Configure these URLs in Twilio console`);\n\n // Start timed cleanup of expired prompts and old actions\n setInterval(() => {\n try {\n const expiredPrompts = cleanupExpiredPrompts();\n const oldActions = cleanupOldActions();\n if (expiredPrompts > 0 || oldActions > 0) {\n logCleanup('sms-webhook', expiredPrompts, oldActions);\n console.log(\n `[sms-webhook] Cleanup: ${expiredPrompts} expired prompts, ${oldActions} old actions`\n );\n }\n } catch {\n // Ignore cleanup errors\n }\n }, CLEANUP_INTERVAL_MS);\n console.log(\n `[sms-webhook] Cleanup interval: every ${CLEANUP_INTERVAL_MS / 1000}s`\n );\n });\n}\n\n// Express middleware for integration\nexport async function smsWebhookMiddleware(\n req: { body: TwilioWebhookPayload },\n res: { type: (t: string) => void; send: (s: string) => void }\n): Promise<void> {\n const result = await handleSMSWebhook(req.body);\n res.type('text/xml');\n res.send(twimlResponse(result.response));\n}\n\n// CLI entry\nif (process.argv[1]?.endsWith('sms-webhook.js')) {\n const port = parseInt(process.env['SMS_WEBHOOK_PORT'] || '3456', 10);\n startWebhookServer(port);\n}\n"],
|
|
5
|
-
"mappings": ";;;;AAYA,SAAS,oBAAqD;AAC9D,SAAS,SAAS,gBAAgB;AAClC,SAAS,YAAY,oBAAoB;AACzC,SAAS,YAAY;AACrB,SAAS,eAAe;AACxB,SAAS,kBAAkB;AAC3B,SAAS,oBAAoB;AAC7B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAC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ACT;AAGA,SAAS,oBACP,UACA,UACA,QACM;AACN,kBAAgB,KAAK,QAAQ,GAAG,cAAc,CAAC;AAC/C,QAAM,eAAe;AAAA,IACnB,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,EACF;AACA;AAAA,IACE;AAAA,IACA,KAAK,UAAU;AAAA,MACb;AAAA,MACA;AAAA,MACA;AAAA,MACA,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,IACpC,CAAC;AAAA,EACH;AACF;AAEA,eAAsB,iBAAiB,SAIpC;AACD,QAAM,EAAE,MAAM,KAAK,IAAI;AAGvB,MAAI,QAAQ,KAAK,SAAS,qBAAqB;AAC7C,YAAQ,IAAI,gCAAgC,KAAK,MAAM,QAAQ;AAC/D,WAAO,EAAE,UAAU,yCAAyC;AAAA,EAC9D;AAEA,MAAI,QAAQ,KAAK,SAAS,kBAAkB;AAC1C,YAAQ,IAAI,2CAA2C;AACvD,WAAO,EAAE,UAAU,wBAAwB;AAAA,EAC7C;AAEA,UAAQ,IAAI,+BAA+B,IAAI,KAAK,IAAI,EAAE;AAE1D,QAAM,SAAS,wBAAwB,MAAM,IAAI;AAEjD,MAAI,CAAC,OAAO,SAAS;AACnB,QAAI,OAAO,QAAQ;AACjB,aAAO;AAAA,QACL,UAAU,+BAA+B,OAAO,OAAO,QAAQ,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;AAAA,MAC7F;AAAA,IACF;AACA,WAAO,EAAE,UAAU,
|
|
4
|
+
"sourcesContent": ["/**\n * SMS Webhook Handler for receiving Twilio responses\n * Can run as standalone server or integrate with existing Express app\n *\n * Security features:\n * - Twilio signature verification\n * - Rate limiting per IP\n * - Body size limits\n * - Content-type validation\n * - Safe action execution (no shell injection)\n */\n\nimport { createServer, IncomingMessage, ServerResponse } from 'http';\nimport { parse as parseUrl } from 'url';\nimport { existsSync, readFileSync } from 'fs';\nimport { join } from 'path';\nimport { homedir } from 'os';\nimport { createHmac } from 'crypto';\nimport { execFileSync } from 'child_process';\nimport {\n processIncomingResponse,\n loadSMSConfig,\n cleanupExpiredPrompts,\n sendNotification,\n} from './sms-notify.js';\nimport {\n queueAction,\n executeActionSafe,\n cleanupOldActions,\n} from './sms-action-runner.js';\nimport { writeFileSecure, ensureSecureDir } from './secure-fs.js';\nimport {\n logWebhookRequest,\n logRateLimit,\n logSignatureInvalid,\n logBodyTooLarge,\n logContentTypeInvalid,\n logActionAllowed,\n logActionBlocked,\n logCleanup,\n} from './security-logger.js';\n\n// Cleanup interval (5 minutes)\nconst CLEANUP_INTERVAL_MS = 5 * 60 * 1000;\n\n// Input validation constants\nconst MAX_SMS_BODY_LENGTH = 1000;\nconst MAX_PHONE_LENGTH = 20;\n\n// Security constants\nconst MAX_BODY_SIZE = 50 * 1024; // 50KB max body\nconst RATE_LIMIT_WINDOW_MS = 60 * 1000; // 1 minute\nconst RATE_LIMIT_MAX_REQUESTS = 30; // 30 requests per minute per IP\n\n// Rate limiting store (in production, use Redis)\nconst rateLimitStore = new Map<string, { count: number; resetTime: number }>();\n\nfunction checkRateLimit(ip: string): boolean {\n const now = Date.now();\n const record = rateLimitStore.get(ip);\n\n if (!record || now > record.resetTime) {\n rateLimitStore.set(ip, { count: 1, resetTime: now + RATE_LIMIT_WINDOW_MS });\n return true;\n }\n\n if (record.count >= RATE_LIMIT_MAX_REQUESTS) {\n return false;\n }\n\n record.count++;\n return true;\n}\n\n// Twilio signature verification\nfunction verifyTwilioSignature(\n url: string,\n params: Record<string, string>,\n signature: string\n): boolean {\n const authToken = process.env['TWILIO_AUTH_TOKEN'];\n if (!authToken) {\n console.warn(\n '[sms-webhook] TWILIO_AUTH_TOKEN not set, skipping signature verification'\n );\n return true; // Allow in development, but log warning\n }\n\n // Build the data string (URL + sorted params)\n const sortedKeys = Object.keys(params).sort();\n let data = url;\n for (const key of sortedKeys) {\n data += key + params[key];\n }\n\n // Calculate expected signature\n const hmac = createHmac('sha1', authToken);\n hmac.update(data);\n const expectedSignature = hmac.digest('base64');\n\n return signature === expectedSignature;\n}\n\ninterface TwilioWebhookPayload {\n From: string;\n To: string;\n Body: string;\n MessageSid: string;\n}\n\nfunction parseFormData(body: string): Record<string, string> {\n const params = new URLSearchParams(body);\n const result: Record<string, string> = {};\n params.forEach((value, key) => {\n result[key] = value;\n });\n return result;\n}\n\n// Store response for Claude hook to pick up\nfunction storeLatestResponse(\n promptId: string,\n response: string,\n action?: string\n): void {\n ensureSecureDir(join(homedir(), '.stackmemory'));\n const responsePath = join(\n homedir(),\n '.stackmemory',\n 'sms-latest-response.json'\n );\n writeFileSecure(\n responsePath,\n JSON.stringify({\n promptId,\n response,\n action,\n timestamp: new Date().toISOString(),\n })\n );\n}\n\n/**\n * Store incoming request for Claude to pick up\n * Used when a WhatsApp/SMS message arrives without a pending prompt\n */\nfunction storeIncomingRequest(from: string, message: string): void {\n ensureSecureDir(join(homedir(), '.stackmemory'));\n const requestPath = join(\n homedir(),\n '.stackmemory',\n 'sms-incoming-request.json'\n );\n writeFileSecure(\n requestPath,\n JSON.stringify({\n from,\n message,\n timestamp: new Date().toISOString(),\n processed: false,\n })\n );\n}\n\n/**\n * Get pending incoming request (if any)\n */\nexport function getIncomingRequest(): {\n from: string;\n message: string;\n timestamp: string;\n processed: boolean;\n} | null {\n const requestPath = join(\n homedir(),\n '.stackmemory',\n 'sms-incoming-request.json'\n );\n if (!existsSync(requestPath)) {\n return null;\n }\n try {\n const data = JSON.parse(readFileSync(requestPath, 'utf-8'));\n if (data.processed) {\n return null;\n }\n return data;\n } catch {\n return null;\n }\n}\n\n/**\n * Mark incoming request as processed\n */\nexport function markRequestProcessed(): void {\n const requestPath = join(\n homedir(),\n '.stackmemory',\n 'sms-incoming-request.json'\n );\n if (!existsSync(requestPath)) {\n return;\n }\n try {\n const data = JSON.parse(readFileSync(requestPath, 'utf-8'));\n data.processed = true;\n writeFileSecure(requestPath, JSON.stringify(data));\n } catch {\n // Ignore errors\n }\n}\n\nexport async function handleSMSWebhook(payload: TwilioWebhookPayload): Promise<{\n response: string;\n action?: string;\n queued?: boolean;\n}> {\n const { From, Body } = payload;\n\n // Input length validation\n if (Body && Body.length > MAX_SMS_BODY_LENGTH) {\n console.log(`[sms-webhook] Body too long: ${Body.length} chars`);\n return { response: 'Message too long. Max 1000 characters.' };\n }\n\n if (From && From.length > MAX_PHONE_LENGTH) {\n console.log(`[sms-webhook] Invalid phone number length`);\n return { response: 'Invalid phone number.' };\n }\n\n console.log(`[sms-webhook] Received from ${From}: ${Body}`);\n\n const result = processIncomingResponse(From, Body);\n\n if (!result.matched) {\n if (result.prompt) {\n return {\n response: `Invalid response. Expected: ${result.prompt.options.map((o) => o.key).join(', ')}`,\n };\n }\n // No pending prompt - store as new incoming request for Claude\n storeIncomingRequest(From, Body);\n console.log(\n `[sms-webhook] Stored new request from ${From}: ${Body.substring(0, 50)}...`\n );\n return { response: 'Got it! Your request has been queued.' };\n }\n\n // Store response for Claude hook\n storeLatestResponse(\n result.prompt?.id || 'unknown',\n result.response || Body,\n result.action\n );\n\n // Trigger notification to alert user/Claude\n triggerResponseNotification(result.response || Body);\n\n // Execute action safely if present (no shell injection)\n if (result.action) {\n console.log(`[sms-webhook] Executing action: ${result.action}`);\n\n const actionResult = await executeActionSafe(\n result.action,\n result.response || Body\n );\n\n if (actionResult.success) {\n logActionAllowed('sms-webhook', result.action);\n console.log(\n `[sms-webhook] Action completed: ${(actionResult.output || '').substring(0, 200)}`\n );\n\n return {\n response: `Done! Action executed successfully.`,\n action: result.action,\n queued: false,\n };\n } else {\n logActionBlocked(\n 'sms-webhook',\n result.action,\n actionResult.error || 'unknown'\n );\n console.log(`[sms-webhook] Action failed: ${actionResult.error}`);\n\n // Queue for retry\n queueAction(\n result.prompt?.id || 'unknown',\n result.response || Body,\n result.action\n );\n\n return {\n response: `Action failed, queued for retry: ${(actionResult.error || '').substring(0, 50)}`,\n action: result.action,\n queued: true,\n };\n }\n }\n\n return {\n response: `Received: ${result.response}. Next action will be triggered.`,\n };\n}\n\n// Escape string for AppleScript (prevent injection)\nfunction escapeAppleScript(str: string): string {\n return str\n .replace(/\\\\/g, '\\\\\\\\')\n .replace(/\"/g, '\\\\\"')\n .replace(/\\n/g, '\\\\n')\n .replace(/\\r/g, '\\\\r')\n .substring(0, 200); // Limit length\n}\n\n// Trigger notification when response received\nfunction triggerResponseNotification(response: string): void {\n const safeMessage = escapeAppleScript(`SMS Response: ${response}`);\n\n // macOS notification using execFile (safer than execSync with shell)\n try {\n execFileSync(\n 'osascript',\n [\n '-e',\n `display notification \"${safeMessage}\" with title \"StackMemory\" sound name \"Glass\"`,\n ],\n { stdio: 'ignore', timeout: 5000 }\n );\n } catch {\n // Ignore if not on macOS\n }\n\n // Write signal file for other processes\n try {\n const signalPath = join(homedir(), '.stackmemory', 'sms-signal.txt');\n writeFileSecure(\n signalPath,\n JSON.stringify({\n type: 'sms_response',\n response,\n timestamp: new Date().toISOString(),\n })\n );\n } catch {\n // Ignore\n }\n\n console.log(`\\n*** SMS RESPONSE RECEIVED: \"${response}\" ***`);\n console.log(`*** Run: stackmemory notify run-actions ***\\n`);\n}\n\n// TwiML response helper\nfunction twimlResponse(message: string): string {\n return `<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response>\n <Message>${escapeXml(message)}</Message>\n</Response>`;\n}\n\nfunction escapeXml(str: string): string {\n return str\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"')\n .replace(/'/g, ''');\n}\n\n// Standalone webhook server\nexport function startWebhookServer(port: number = 3456): void {\n const server = createServer(\n async (req: IncomingMessage, res: ServerResponse) => {\n const url = parseUrl(req.url || '/', true);\n\n // Health check\n if (url.pathname === '/health') {\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ status: 'ok' }));\n return;\n }\n\n // SMS webhook endpoint (incoming messages)\n if (\n (url.pathname === '/sms' ||\n url.pathname === '/sms/incoming' ||\n url.pathname === '/webhook') &&\n req.method === 'POST'\n ) {\n const clientIp = req.socket.remoteAddress || 'unknown';\n\n // Log webhook request\n logWebhookRequest(\n 'sms-webhook',\n req.method || 'POST',\n url.pathname || '/sms',\n clientIp\n );\n\n // Rate limiting\n if (!checkRateLimit(clientIp)) {\n logRateLimit('sms-webhook', clientIp);\n res.writeHead(429, {\n 'Content-Type': 'text/xml',\n 'Retry-After': '60',\n });\n res.end(twimlResponse('Too many requests. Please try again later.'));\n return;\n }\n\n // Content-type validation\n const contentType = req.headers['content-type'] || '';\n if (!contentType.includes('application/x-www-form-urlencoded')) {\n logContentTypeInvalid('sms-webhook', contentType, clientIp);\n res.writeHead(400, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Invalid content type'));\n return;\n }\n\n let body = '';\n let bodyTooLarge = false;\n\n req.on('data', (chunk) => {\n body += chunk;\n // Body size limit\n if (body.length > MAX_BODY_SIZE) {\n bodyTooLarge = true;\n logBodyTooLarge('sms-webhook', body.length, clientIp);\n req.destroy();\n }\n });\n\n req.on('end', async () => {\n if (bodyTooLarge) {\n res.writeHead(413, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Request too large'));\n return;\n }\n\n try {\n const payload = parseFormData(\n body\n ) as unknown as TwilioWebhookPayload;\n\n // Verify Twilio signature\n const twilioSignature = req.headers['x-twilio-signature'] as string;\n const webhookUrl = `${req.headers['x-forwarded-proto'] || 'http'}://${req.headers.host}${req.url}`;\n\n if (\n twilioSignature &&\n !verifyTwilioSignature(\n webhookUrl,\n payload as unknown as Record<string, string>,\n twilioSignature\n )\n ) {\n logSignatureInvalid('sms-webhook', clientIp);\n console.error('[sms-webhook] Invalid Twilio signature');\n res.writeHead(401, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Unauthorized'));\n return;\n }\n\n const result = await handleSMSWebhook(payload);\n\n res.writeHead(200, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse(result.response));\n } catch (err) {\n console.error('[sms-webhook] Error:', err);\n res.writeHead(500, { 'Content-Type': 'text/xml' });\n res.end(twimlResponse('Error processing message'));\n }\n });\n return;\n }\n\n // Status callback endpoint (delivery status updates)\n if (url.pathname === '/sms/status' && req.method === 'POST') {\n let body = '';\n req.on('data', (chunk) => {\n body += chunk;\n });\n\n req.on('end', () => {\n try {\n const payload = parseFormData(body);\n console.log(\n `[sms-webhook] Status update: ${payload['MessageSid']} -> ${payload['MessageStatus']}`\n );\n\n // Store status for tracking\n const statusPath = join(\n homedir(),\n '.stackmemory',\n 'sms-status.json'\n );\n const statuses: Record<string, string> = existsSync(statusPath)\n ? JSON.parse(readFileSync(statusPath, 'utf8'))\n : {};\n statuses[payload['MessageSid']] = payload['MessageStatus'];\n writeFileSecure(statusPath, JSON.stringify(statuses, null, 2));\n\n res.writeHead(200, { 'Content-Type': 'text/plain' });\n res.end('OK');\n } catch (err) {\n console.error('[sms-webhook] Status error:', err);\n res.writeHead(500);\n res.end('Error');\n }\n });\n return;\n }\n\n // Server status endpoint\n if (url.pathname === '/status') {\n const config = loadSMSConfig();\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n enabled: config.enabled,\n pendingPrompts: config.pendingPrompts.length,\n })\n );\n return;\n }\n\n // Get pending incoming request endpoint\n if (url.pathname === '/request' && req.method === 'GET') {\n const request = getIncomingRequest();\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ request }));\n return;\n }\n\n // Mark request as processed endpoint\n if (url.pathname === '/request/ack' && req.method === 'POST') {\n markRequestProcessed();\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ success: true }));\n return;\n }\n\n // Send outgoing notification endpoint\n if (url.pathname === '/send' && req.method === 'POST') {\n let body = '';\n req.on('data', (chunk) => {\n body += chunk;\n if (body.length > MAX_BODY_SIZE) {\n req.destroy();\n }\n });\n\n req.on('end', async () => {\n try {\n const payload = JSON.parse(body);\n const message = payload.message || payload.body || '';\n const title = payload.title || 'Notification';\n const type = payload.type || 'custom';\n\n if (!message) {\n res.writeHead(400, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({ success: false, error: 'Message required' })\n );\n return;\n }\n\n const result = await sendNotification({\n type: type as\n | 'task_complete'\n | 'review_ready'\n | 'error'\n | 'custom',\n title,\n message,\n });\n\n res.writeHead(result.success ? 200 : 500, {\n 'Content-Type': 'application/json',\n });\n res.end(JSON.stringify(result));\n } catch (err) {\n console.error('[sms-webhook] Send error:', err);\n res.writeHead(500, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n success: false,\n error: err instanceof Error ? err.message : 'Send failed',\n })\n );\n }\n });\n return;\n }\n\n res.writeHead(404);\n res.end('Not found');\n }\n );\n\n server.listen(port, () => {\n console.log(`[sms-webhook] Server listening on port ${port}`);\n console.log(\n `[sms-webhook] Incoming messages: http://localhost:${port}/sms/incoming`\n );\n console.log(\n `[sms-webhook] Status callback: http://localhost:${port}/sms/status`\n );\n console.log(`[sms-webhook] Configure these URLs in Twilio console`);\n\n // Start timed cleanup of expired prompts and old actions\n setInterval(() => {\n try {\n const expiredPrompts = cleanupExpiredPrompts();\n const oldActions = cleanupOldActions();\n if (expiredPrompts > 0 || oldActions > 0) {\n logCleanup('sms-webhook', expiredPrompts, oldActions);\n console.log(\n `[sms-webhook] Cleanup: ${expiredPrompts} expired prompts, ${oldActions} old actions`\n );\n }\n } catch {\n // Ignore cleanup errors\n }\n }, CLEANUP_INTERVAL_MS);\n console.log(\n `[sms-webhook] Cleanup interval: every ${CLEANUP_INTERVAL_MS / 1000}s`\n );\n });\n}\n\n// Express middleware for integration\nexport async function smsWebhookMiddleware(\n req: { body: TwilioWebhookPayload },\n res: { type: (t: string) => void; send: (s: string) => void }\n): Promise<void> {\n const result = await handleSMSWebhook(req.body);\n res.type('text/xml');\n res.send(twimlResponse(result.response));\n}\n\n// CLI entry\nif (process.argv[1]?.endsWith('sms-webhook.js')) {\n const port = parseInt(process.env['SMS_WEBHOOK_PORT'] || '3456', 10);\n startWebhookServer(port);\n}\n"],
|
|
5
|
+
"mappings": ";;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
210
|
+
const existing = this.discoveredAPIs.get(discovered.name);
|
|
211
|
+
if (existing) {
|
|
212
|
+
return existing;
|
|
212
213
|
}
|
|
213
214
|
if (!discovered.specUrl && discovered.source !== "known") {
|
|
214
215
|
try {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/skills/api-discovery.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * API Auto-Discovery Skill\n *\n * Automatically detects API endpoints and OpenAPI specs when Claude\n * reads documentation or API URLs, then registers them for easy access.\n */\n\nimport { execSync } from 'child_process';\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport * as os from 'os';\nimport { logger } from '../core/monitoring/logger.js';\nimport { getAPISkill } from './api-skill.js';\n\n// Common API documentation patterns\nconst API_PATTERNS = [\n // Direct API URLs\n {\n pattern: /https?:\\/\\/api\\.([a-z0-9-]+)\\.(com|io|dev|app|co)/,\n nameGroup: 1,\n },\n // REST API paths in docs\n { pattern: /https?:\\/\\/([a-z0-9-]+)\\.com\\/api/, nameGroup: 1 },\n // Developer docs\n { pattern: /https?:\\/\\/developer\\.([a-z0-9-]+)\\.com/, nameGroup: 1 },\n // Docs subdomains\n { pattern: /https?:\\/\\/docs\\.([a-z0-9-]+)\\.(com|io|dev)/, nameGroup: 1 },\n];\n\n// Known OpenAPI spec locations for popular services\nconst KNOWN_SPECS: Record<string, string> = {\n github:\n 'https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.json',\n stripe:\n 'https://raw.githubusercontent.com/stripe/openapi/master/openapi/spec3.json',\n twilio:\n 'https://raw.githubusercontent.com/twilio/twilio-oai/main/spec/json/twilio_api_v2010.json',\n slack: 'https://api.slack.com/specs/openapi/v2/slack_web.json',\n discord:\n 'https://raw.githubusercontent.com/discord/discord-api-spec/main/specs/openapi.json',\n openai:\n 'https://raw.githubusercontent.com/openai/openai-openapi/master/openapi.yaml',\n anthropic:\n 'https://raw.githubusercontent.com/anthropics/anthropic-sdk-python/main/openapi.json',\n linear: 'https://api.linear.app/graphql', // GraphQL, not REST\n notion:\n 'https://raw.githubusercontent.com/NotionX/notion-sdk-js/main/openapi.json',\n vercel: 'https://openapi.vercel.sh/',\n cloudflare:\n 'https://raw.githubusercontent.com/cloudflare/api-schemas/main/openapi.json',\n // Google Cloud Platform - uses Google Discovery format\n gcp: 'https://www.googleapis.com/discovery/v1/apis',\n 'gcp-compute': 'https://compute.googleapis.com/$discovery/rest?version=v1',\n 'gcp-storage': 'https://storage.googleapis.com/$discovery/rest?version=v1',\n 'gcp-run': 'https://run.googleapis.com/$discovery/rest?version=v2',\n 'gcp-functions':\n 'https://cloudfunctions.googleapis.com/$discovery/rest?version=v2',\n 'gcp-bigquery': 'https://bigquery.googleapis.com/$discovery/rest?version=v2',\n 'gcp-aiplatform':\n 'https://aiplatform.googleapis.com/$discovery/rest?version=v1',\n // Railway - GraphQL API\n railway: 'https://backboard.railway.com/graphql/v2', // GraphQL endpoint\n};\n\n// Known base URLs for popular services\nconst KNOWN_BASES: Record<string, string> = {\n github: 'https://api.github.com',\n stripe: 'https://api.stripe.com',\n twilio: 'https://api.twilio.com',\n slack: 'https://slack.com/api',\n discord: 'https://discord.com/api',\n openai: 'https://api.openai.com',\n anthropic: 'https://api.anthropic.com',\n linear: 'https://api.linear.app',\n notion: 'https://api.notion.com',\n vercel: 'https://api.vercel.com',\n cloudflare: 'https://api.cloudflare.com',\n // Google Cloud Platform\n gcp: 'https://www.googleapis.com',\n 'gcp-compute': 'https://compute.googleapis.com',\n 'gcp-storage': 'https://storage.googleapis.com',\n 'gcp-run': 'https://run.googleapis.com',\n 'gcp-functions': 'https://cloudfunctions.googleapis.com',\n 'gcp-bigquery': 'https://bigquery.googleapis.com',\n 'gcp-aiplatform': 'https://aiplatform.googleapis.com',\n // Railway (GraphQL)\n railway: 'https://backboard.railway.com/graphql/v2',\n};\n\n// API types for special handling\nconst API_TYPES: Record<string, 'rest' | 'graphql' | 'google-discovery'> = {\n railway: 'graphql',\n linear: 'graphql',\n gcp: 'google-discovery',\n 'gcp-compute': 'google-discovery',\n 'gcp-storage': 'google-discovery',\n 'gcp-run': 'google-discovery',\n 'gcp-functions': 'google-discovery',\n 'gcp-bigquery': 'google-discovery',\n 'gcp-aiplatform': 'google-discovery',\n};\n\nexport interface DiscoveredAPI {\n name: string;\n baseUrl: string;\n specUrl?: string;\n source: 'url' | 'docs' | 'known' | 'inferred';\n confidence: number; // 0-1\n apiType?: 'rest' | 'graphql' | 'google-discovery';\n}\n\nexport interface DiscoveryResult {\n discovered: DiscoveredAPI[];\n registered: string[];\n skipped: string[];\n}\n\nexport class APIDiscoverySkill {\n private discoveryLog: string;\n private discoveredAPIs: Map<string, DiscoveredAPI> = new Map();\n\n constructor() {\n this.discoveryLog = path.join(\n os.homedir(),\n '.stackmemory',\n 'api-discovery.log'\n );\n }\n\n /**\n * Analyze a URL for potential API endpoints\n */\n analyzeUrl(url: string): DiscoveredAPI | null {\n // Check for GCP URLs first (special pattern)\n if (url.includes('googleapis.com')) {\n const gcpMatch = url.match(/https?:\\/\\/([a-z]+)\\.googleapis\\.com/);\n if (gcpMatch) {\n const service = gcpMatch[1];\n const name = `gcp-${service}`;\n return {\n name,\n baseUrl: `https://${service}.googleapis.com`,\n specUrl:\n KNOWN_SPECS[name] ||\n `https://${service}.googleapis.com/$discovery/rest?version=v1`,\n source: 'known',\n confidence: 0.95,\n apiType: 'google-discovery',\n };\n }\n }\n\n // Check for Railway\n if (url.includes('railway.com') || url.includes('railway.app')) {\n return {\n name: 'railway',\n baseUrl: KNOWN_BASES['railway'],\n specUrl: KNOWN_SPECS['railway'],\n source: 'known',\n confidence: 0.95,\n apiType: 'graphql',\n };\n }\n\n // Check if it's a known service\n for (const [name, baseUrl] of Object.entries(KNOWN_BASES)) {\n if (url.includes(name) || url.includes(baseUrl)) {\n return {\n name,\n baseUrl,\n specUrl: KNOWN_SPECS[name],\n source: 'known',\n confidence: 0.95,\n apiType: API_TYPES[name] || 'rest',\n };\n }\n }\n\n // Try to match API patterns\n for (const { pattern, nameGroup } of API_PATTERNS) {\n const match = url.match(pattern);\n if (match) {\n const name = match[nameGroup].toLowerCase();\n const baseUrl = this.inferBaseUrl(url, name);\n\n return {\n name,\n baseUrl,\n source: 'inferred',\n confidence: 0.7,\n apiType: 'rest',\n };\n }\n }\n\n return null;\n }\n\n /**\n * Infer base URL from a discovered URL\n */\n private inferBaseUrl(url: string, name: string): string {\n // Try common patterns\n const patterns = [\n `https://api.${name}.com`,\n `https://api.${name}.io`,\n `https://${name}.com/api`,\n ];\n\n // Extract domain from URL\n try {\n const urlObj = new URL(url);\n if (urlObj.hostname.startsWith('api.')) {\n return `${urlObj.protocol}//${urlObj.hostname}`;\n }\n if (urlObj.pathname.includes('/api')) {\n return `${urlObj.protocol}//${urlObj.hostname}/api`;\n }\n return `${urlObj.protocol}//${urlObj.hostname}`;\n } catch {\n return patterns[0];\n }\n }\n\n /**\n * Try to discover OpenAPI spec for a service\n */\n async discoverSpec(name: string, baseUrl: string): Promise<string | null> {\n // Check known specs first\n if (KNOWN_SPECS[name]) {\n return KNOWN_SPECS[name];\n }\n\n // Try common spec locations\n const specPaths = [\n '/openapi.json',\n '/openapi.yaml',\n '/swagger.json',\n '/swagger.yaml',\n '/api-docs',\n '/v1/openapi.json',\n '/v2/openapi.json',\n '/docs/openapi.json',\n '/.well-known/openapi.json',\n ];\n\n for (const specPath of specPaths) {\n const specUrl = `${baseUrl}${specPath}`;\n try {\n // Quick HEAD request to check if spec exists\n execSync(`curl -sI --max-time 2 \"${specUrl}\" | grep -q \"200 OK\"`, {\n stdio: 'pipe',\n });\n return specUrl;\n } catch {\n // Spec not found at this location\n }\n }\n\n return null;\n }\n\n /**\n * Process a URL and auto-register if it's an API\n */\n async processUrl(\n url: string,\n autoRegister: boolean = true\n ): Promise<DiscoveredAPI | null> {\n const discovered = this.analyzeUrl(url);\n\n if (!discovered) {\n return null;\n }\n\n // Check if already discovered\n if (this.discoveredAPIs.has(discovered.name)) {\n return this.discoveredAPIs.get(discovered.name)!;\n }\n\n // Only probe for spec if it's not a known service (known services already have spec URLs)\n if (!discovered.specUrl && discovered.source !== 'known') {\n // Try to find OpenAPI spec (with timeout protection)\n try {\n discovered.specUrl =\n (await this.discoverSpec(discovered.name, discovered.baseUrl)) ||\n undefined;\n } catch {\n // Spec discovery failed, continue without\n }\n }\n\n this.discoveredAPIs.set(discovered.name, discovered);\n this.logDiscovery(discovered, url);\n\n // Auto-register if enabled and confidence is high enough\n if (autoRegister && discovered.confidence >= 0.7) {\n await this.registerAPI(discovered);\n }\n\n return discovered;\n }\n\n /**\n * Register a discovered API\n */\n async registerAPI(api: DiscoveredAPI): Promise<boolean> {\n const skill = getAPISkill();\n\n try {\n const result = await skill.add(api.name, api.baseUrl, {\n spec: api.specUrl,\n });\n\n if (result.success) {\n logger.info(`Auto-registered API: ${api.name}`);\n return true;\n }\n } catch (error) {\n logger.warn(`Failed to auto-register API ${api.name}:`, error);\n }\n\n return false;\n }\n\n /**\n * Log discovery for debugging\n */\n private logDiscovery(api: DiscoveredAPI, sourceUrl: string): void {\n const entry = {\n timestamp: new Date().toISOString(),\n api,\n sourceUrl,\n };\n\n try {\n const dir = path.dirname(this.discoveryLog);\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n\n fs.appendFileSync(this.discoveryLog, JSON.stringify(entry) + '\\n');\n } catch (error) {\n logger.warn('Failed to log API discovery:', error);\n }\n }\n\n /**\n * Get all discovered APIs\n */\n getDiscoveredAPIs(): DiscoveredAPI[] {\n return Array.from(this.discoveredAPIs.values());\n }\n\n /**\n * Suggest API registration based on recent activity\n */\n async suggestFromContext(recentUrls: string[]): Promise<DiscoveryResult> {\n const result: DiscoveryResult = {\n discovered: [],\n registered: [],\n skipped: [],\n };\n\n for (const url of recentUrls) {\n const discovered = await this.processUrl(url, false);\n\n if (discovered) {\n result.discovered.push(discovered);\n\n // Check if already registered\n const skill = getAPISkill();\n const listResult = await skill.list();\n const existingAPIs = (listResult.data as Array<{ name: string }>) || [];\n\n if (existingAPIs.some((api) => api.name === discovered.name)) {\n result.skipped.push(discovered.name);\n } else if (discovered.confidence >= 0.7) {\n const registered = await this.registerAPI(discovered);\n if (registered) {\n result.registered.push(discovered.name);\n }\n }\n }\n }\n\n return result;\n }\n\n /**\n * Get help text\n */\n getHelp(): string {\n const restAPIs = Object.keys(KNOWN_SPECS).filter(\n (s) => !API_TYPES[s] || API_TYPES[s] === 'rest'\n );\n const graphqlAPIs = Object.keys(KNOWN_SPECS).filter(\n (s) => API_TYPES[s] === 'graphql'\n );\n const gcpAPIs = Object.keys(KNOWN_SPECS).filter(\n (s) => API_TYPES[s] === 'google-discovery'\n );\n\n return `\nAPI Auto-Discovery\n\nAutomatically detects and registers APIs when you browse documentation.\n\nREST APIs (OpenAPI specs):\n${restAPIs.map((s) => ` - ${s}`).join('\\n')}\n\nGraphQL APIs:\n${graphqlAPIs.map((s) => ` - ${s}`).join('\\n')}\n\nGoogle Cloud Platform (Discovery format):\n${gcpAPIs.map((s) => ` - ${s}`).join('\\n')}\n\nHow It Works:\n1. Monitors URLs you access during development\n2. Identifies API documentation and endpoints\n3. Finds OpenAPI specs automatically\n4. Registers APIs for easy access via /api exec\n\nUsage:\n # Check if a URL is a known API\n stackmemory api discover <url>\n\n # List discovered APIs\n stackmemory api discovered\n\n # Register all discovered APIs\n stackmemory api register-discovered\n`;\n }\n}\n\n// Singleton instance\nlet discoveryInstance: APIDiscoverySkill | null = null;\n\nexport function getAPIDiscovery(): APIDiscoverySkill {\n if (!discoveryInstance) {\n discoveryInstance = new APIDiscoverySkill();\n }\n return discoveryInstance;\n}\n"],
|
|
5
|
-
"mappings": ";;;;AAOA,SAAS,gBAAgB;AACzB,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,YAAY,QAAQ;AACpB,SAAS,cAAc;AACvB,SAAS,mBAAmB;AAG5B,MAAM,eAAe;AAAA;AAAA,EAEnB;AAAA,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A,
|
|
4
|
+
"sourcesContent": ["/**\n * API Auto-Discovery Skill\n *\n * Automatically detects API endpoints and OpenAPI specs when Claude\n * reads documentation or API URLs, then registers them for easy access.\n */\n\nimport { execSync } from 'child_process';\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport * as os from 'os';\nimport { logger } from '../core/monitoring/logger.js';\nimport { getAPISkill } from './api-skill.js';\n\n// Common API documentation patterns\nconst API_PATTERNS = [\n // Direct API URLs\n {\n pattern: /https?:\\/\\/api\\.([a-z0-9-]+)\\.(com|io|dev|app|co)/,\n nameGroup: 1,\n },\n // REST API paths in docs\n { pattern: /https?:\\/\\/([a-z0-9-]+)\\.com\\/api/, nameGroup: 1 },\n // Developer docs\n { pattern: /https?:\\/\\/developer\\.([a-z0-9-]+)\\.com/, nameGroup: 1 },\n // Docs subdomains\n { pattern: /https?:\\/\\/docs\\.([a-z0-9-]+)\\.(com|io|dev)/, nameGroup: 1 },\n];\n\n// Known OpenAPI spec locations for popular services\nconst KNOWN_SPECS: Record<string, string> = {\n github:\n 'https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.json',\n stripe:\n 'https://raw.githubusercontent.com/stripe/openapi/master/openapi/spec3.json',\n twilio:\n 'https://raw.githubusercontent.com/twilio/twilio-oai/main/spec/json/twilio_api_v2010.json',\n slack: 'https://api.slack.com/specs/openapi/v2/slack_web.json',\n discord:\n 'https://raw.githubusercontent.com/discord/discord-api-spec/main/specs/openapi.json',\n openai:\n 'https://raw.githubusercontent.com/openai/openai-openapi/master/openapi.yaml',\n anthropic:\n 'https://raw.githubusercontent.com/anthropics/anthropic-sdk-python/main/openapi.json',\n linear: 'https://api.linear.app/graphql', // GraphQL, not REST\n notion:\n 'https://raw.githubusercontent.com/NotionX/notion-sdk-js/main/openapi.json',\n vercel: 'https://openapi.vercel.sh/',\n cloudflare:\n 'https://raw.githubusercontent.com/cloudflare/api-schemas/main/openapi.json',\n // Google Cloud Platform - uses Google Discovery format\n gcp: 'https://www.googleapis.com/discovery/v1/apis',\n 'gcp-compute': 'https://compute.googleapis.com/$discovery/rest?version=v1',\n 'gcp-storage': 'https://storage.googleapis.com/$discovery/rest?version=v1',\n 'gcp-run': 'https://run.googleapis.com/$discovery/rest?version=v2',\n 'gcp-functions':\n 'https://cloudfunctions.googleapis.com/$discovery/rest?version=v2',\n 'gcp-bigquery': 'https://bigquery.googleapis.com/$discovery/rest?version=v2',\n 'gcp-aiplatform':\n 'https://aiplatform.googleapis.com/$discovery/rest?version=v1',\n // Railway - GraphQL API\n railway: 'https://backboard.railway.com/graphql/v2', // GraphQL endpoint\n};\n\n// Known base URLs for popular services\nconst KNOWN_BASES: Record<string, string> = {\n github: 'https://api.github.com',\n stripe: 'https://api.stripe.com',\n twilio: 'https://api.twilio.com',\n slack: 'https://slack.com/api',\n discord: 'https://discord.com/api',\n openai: 'https://api.openai.com',\n anthropic: 'https://api.anthropic.com',\n linear: 'https://api.linear.app',\n notion: 'https://api.notion.com',\n vercel: 'https://api.vercel.com',\n cloudflare: 'https://api.cloudflare.com',\n // Google Cloud Platform\n gcp: 'https://www.googleapis.com',\n 'gcp-compute': 'https://compute.googleapis.com',\n 'gcp-storage': 'https://storage.googleapis.com',\n 'gcp-run': 'https://run.googleapis.com',\n 'gcp-functions': 'https://cloudfunctions.googleapis.com',\n 'gcp-bigquery': 'https://bigquery.googleapis.com',\n 'gcp-aiplatform': 'https://aiplatform.googleapis.com',\n // Railway (GraphQL)\n railway: 'https://backboard.railway.com/graphql/v2',\n};\n\n// API types for special handling\nconst API_TYPES: Record<string, 'rest' | 'graphql' | 'google-discovery'> = {\n railway: 'graphql',\n linear: 'graphql',\n gcp: 'google-discovery',\n 'gcp-compute': 'google-discovery',\n 'gcp-storage': 'google-discovery',\n 'gcp-run': 'google-discovery',\n 'gcp-functions': 'google-discovery',\n 'gcp-bigquery': 'google-discovery',\n 'gcp-aiplatform': 'google-discovery',\n};\n\nexport interface DiscoveredAPI {\n name: string;\n baseUrl: string;\n specUrl?: string;\n source: 'url' | 'docs' | 'known' | 'inferred';\n confidence: number; // 0-1\n apiType?: 'rest' | 'graphql' | 'google-discovery';\n}\n\nexport interface DiscoveryResult {\n discovered: DiscoveredAPI[];\n registered: string[];\n skipped: string[];\n}\n\nexport class APIDiscoverySkill {\n private discoveryLog: string;\n private discoveredAPIs: Map<string, DiscoveredAPI> = new Map();\n\n constructor() {\n this.discoveryLog = path.join(\n os.homedir(),\n '.stackmemory',\n 'api-discovery.log'\n );\n }\n\n /**\n * Analyze a URL for potential API endpoints\n */\n analyzeUrl(url: string): DiscoveredAPI | null {\n // Check for GCP URLs first (special pattern)\n if (url.includes('googleapis.com')) {\n const gcpMatch = url.match(/https?:\\/\\/([a-z]+)\\.googleapis\\.com/);\n if (gcpMatch) {\n const service = gcpMatch[1];\n const name = `gcp-${service}`;\n return {\n name,\n baseUrl: `https://${service}.googleapis.com`,\n specUrl:\n KNOWN_SPECS[name] ||\n `https://${service}.googleapis.com/$discovery/rest?version=v1`,\n source: 'known',\n confidence: 0.95,\n apiType: 'google-discovery',\n };\n }\n }\n\n // Check for Railway\n if (url.includes('railway.com') || url.includes('railway.app')) {\n return {\n name: 'railway',\n baseUrl: KNOWN_BASES['railway'],\n specUrl: KNOWN_SPECS['railway'],\n source: 'known',\n confidence: 0.95,\n apiType: 'graphql',\n };\n }\n\n // Check if it's a known service\n for (const [name, baseUrl] of Object.entries(KNOWN_BASES)) {\n if (url.includes(name) || url.includes(baseUrl)) {\n return {\n name,\n baseUrl,\n specUrl: KNOWN_SPECS[name],\n source: 'known',\n confidence: 0.95,\n apiType: API_TYPES[name] || 'rest',\n };\n }\n }\n\n // Try to match API patterns\n for (const { pattern, nameGroup } of API_PATTERNS) {\n const match = url.match(pattern);\n if (match) {\n const name = match[nameGroup].toLowerCase();\n const baseUrl = this.inferBaseUrl(url, name);\n\n return {\n name,\n baseUrl,\n source: 'inferred',\n confidence: 0.7,\n apiType: 'rest',\n };\n }\n }\n\n return null;\n }\n\n /**\n * Infer base URL from a discovered URL\n */\n private inferBaseUrl(url: string, name: string): string {\n // Try common patterns\n const patterns = [\n `https://api.${name}.com`,\n `https://api.${name}.io`,\n `https://${name}.com/api`,\n ];\n\n // Extract domain from URL\n try {\n const urlObj = new URL(url);\n if (urlObj.hostname.startsWith('api.')) {\n return `${urlObj.protocol}//${urlObj.hostname}`;\n }\n if (urlObj.pathname.includes('/api')) {\n return `${urlObj.protocol}//${urlObj.hostname}/api`;\n }\n return `${urlObj.protocol}//${urlObj.hostname}`;\n } catch {\n return patterns[0];\n }\n }\n\n /**\n * Try to discover OpenAPI spec for a service\n */\n async discoverSpec(name: string, baseUrl: string): Promise<string | null> {\n // Check known specs first\n if (KNOWN_SPECS[name]) {\n return KNOWN_SPECS[name];\n }\n\n // Try common spec locations\n const specPaths = [\n '/openapi.json',\n '/openapi.yaml',\n '/swagger.json',\n '/swagger.yaml',\n '/api-docs',\n '/v1/openapi.json',\n '/v2/openapi.json',\n '/docs/openapi.json',\n '/.well-known/openapi.json',\n ];\n\n for (const specPath of specPaths) {\n const specUrl = `${baseUrl}${specPath}`;\n try {\n // Quick HEAD request to check if spec exists\n execSync(`curl -sI --max-time 2 \"${specUrl}\" | grep -q \"200 OK\"`, {\n stdio: 'pipe',\n });\n return specUrl;\n } catch {\n // Spec not found at this location\n }\n }\n\n return null;\n }\n\n /**\n * Process a URL and auto-register if it's an API\n */\n async processUrl(\n url: string,\n autoRegister: boolean = true\n ): Promise<DiscoveredAPI | null> {\n const discovered = this.analyzeUrl(url);\n\n if (!discovered) {\n return null;\n }\n\n // Check if already discovered\n const existing = this.discoveredAPIs.get(discovered.name);\n if (existing) {\n return existing;\n }\n\n // Only probe for spec if it's not a known service (known services already have spec URLs)\n if (!discovered.specUrl && discovered.source !== 'known') {\n // Try to find OpenAPI spec (with timeout protection)\n try {\n discovered.specUrl =\n (await this.discoverSpec(discovered.name, discovered.baseUrl)) ||\n undefined;\n } catch {\n // Spec discovery failed, continue without\n }\n }\n\n this.discoveredAPIs.set(discovered.name, discovered);\n this.logDiscovery(discovered, url);\n\n // Auto-register if enabled and confidence is high enough\n if (autoRegister && discovered.confidence >= 0.7) {\n await this.registerAPI(discovered);\n }\n\n return discovered;\n }\n\n /**\n * Register a discovered API\n */\n async registerAPI(api: DiscoveredAPI): Promise<boolean> {\n const skill = getAPISkill();\n\n try {\n const result = await skill.add(api.name, api.baseUrl, {\n spec: api.specUrl,\n });\n\n if (result.success) {\n logger.info(`Auto-registered API: ${api.name}`);\n return true;\n }\n } catch (error) {\n logger.warn(`Failed to auto-register API ${api.name}:`, error);\n }\n\n return false;\n }\n\n /**\n * Log discovery for debugging\n */\n private logDiscovery(api: DiscoveredAPI, sourceUrl: string): void {\n const entry = {\n timestamp: new Date().toISOString(),\n api,\n sourceUrl,\n };\n\n try {\n const dir = path.dirname(this.discoveryLog);\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n\n fs.appendFileSync(this.discoveryLog, JSON.stringify(entry) + '\\n');\n } catch (error) {\n logger.warn('Failed to log API discovery:', error);\n }\n }\n\n /**\n * Get all discovered APIs\n */\n getDiscoveredAPIs(): DiscoveredAPI[] {\n return Array.from(this.discoveredAPIs.values());\n }\n\n /**\n * Suggest API registration based on recent activity\n */\n async suggestFromContext(recentUrls: string[]): Promise<DiscoveryResult> {\n const result: DiscoveryResult = {\n discovered: [],\n registered: [],\n skipped: [],\n };\n\n for (const url of recentUrls) {\n const discovered = await this.processUrl(url, false);\n\n if (discovered) {\n result.discovered.push(discovered);\n\n // Check if already registered\n const skill = getAPISkill();\n const listResult = await skill.list();\n const existingAPIs = (listResult.data as Array<{ name: string }>) || [];\n\n if (existingAPIs.some((api) => api.name === discovered.name)) {\n result.skipped.push(discovered.name);\n } else if (discovered.confidence >= 0.7) {\n const registered = await this.registerAPI(discovered);\n if (registered) {\n result.registered.push(discovered.name);\n }\n }\n }\n }\n\n return result;\n }\n\n /**\n * Get help text\n */\n getHelp(): string {\n const restAPIs = Object.keys(KNOWN_SPECS).filter(\n (s) => !API_TYPES[s] || API_TYPES[s] === 'rest'\n );\n const graphqlAPIs = Object.keys(KNOWN_SPECS).filter(\n (s) => API_TYPES[s] === 'graphql'\n );\n const gcpAPIs = Object.keys(KNOWN_SPECS).filter(\n (s) => API_TYPES[s] === 'google-discovery'\n );\n\n return `\nAPI Auto-Discovery\n\nAutomatically detects and registers APIs when you browse documentation.\n\nREST APIs (OpenAPI specs):\n${restAPIs.map((s) => ` - ${s}`).join('\\n')}\n\nGraphQL APIs:\n${graphqlAPIs.map((s) => ` - ${s}`).join('\\n')}\n\nGoogle Cloud Platform (Discovery format):\n${gcpAPIs.map((s) => ` - ${s}`).join('\\n')}\n\nHow It Works:\n1. Monitors URLs you access during development\n2. Identifies API documentation and endpoints\n3. Finds OpenAPI specs automatically\n4. Registers APIs for easy access via /api exec\n\nUsage:\n # Check if a URL is a known API\n stackmemory api discover <url>\n\n # List discovered APIs\n stackmemory api discovered\n\n # Register all discovered APIs\n stackmemory api register-discovered\n`;\n }\n}\n\n// Singleton instance\nlet discoveryInstance: APIDiscoverySkill | null = null;\n\nexport function getAPIDiscovery(): APIDiscoverySkill {\n if (!discoveryInstance) {\n discoveryInstance = new APIDiscoverySkill();\n }\n return discoveryInstance;\n}\n"],
|
|
5
|
+
"mappings": ";;;;AAOA,SAAS,gBAAgB;AACzB,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,YAAY,QAAQ;AACpB,SAAS,cAAc;AACvB,SAAS,mBAAmB;AAG5B,MAAM,eAAe;AAAA;AAAA,EAEnB;AAAA,IACE,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AAAA;AAAA,EAEA,EAAE,SAAS,qCAAqC,WAAW,EAAE;AAAA;AAAA,EAE7D,EAAE,SAAS,2CAA2C,WAAW,EAAE;AAAA;AAAA,EAEnE,EAAE,SAAS,+CAA+C,WAAW,EAAE;AACzE;AAGA,MAAM,cAAsC;AAAA,EAC1C,QACE;AAAA,EACF,QACE;AAAA,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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBzC;AACF;AAGA,IAAI,oBAA8C;AAE3C,SAAS,kBAAqC;AACnD,MAAI,CAAC,mBAAmB;AACtB,wBAAoB,IAAI,kBAAkB;AAAA,EAC5C;AACA,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stackmemoryai/stackmemory",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.27",
|
|
4
4
|
"description": "Lossless memory runtime for AI coding tools - organizes context as a call stack instead of linear chat logs, with team collaboration and infinite retention",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=20.0.0",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"persistence"
|
|
45
45
|
],
|
|
46
46
|
"scripts": {
|
|
47
|
-
"start": "node dist/
|
|
48
|
-
"start:full": "node dist/
|
|
47
|
+
"start": "node dist/integrations/mcp/server.js",
|
|
48
|
+
"start:full": "node dist/integrations/mcp/server.js",
|
|
49
49
|
"setup": "npm install && npm run build && npm run init",
|
|
50
50
|
"postinstall": "node scripts/install-claude-hooks-auto.js || true",
|
|
51
51
|
"init": "node dist/scripts/initialize.js",
|
|
@@ -68,11 +68,6 @@
|
|
|
68
68
|
"status": "node dist/scripts/status.js",
|
|
69
69
|
"linear:sync": "node scripts/sync-linear-graphql.js",
|
|
70
70
|
"linear:mirror": "node scripts/sync-linear-graphql.js --mirror",
|
|
71
|
-
"railway:setup": "./scripts/setup-railway-deployment.sh",
|
|
72
|
-
"railway:deploy": "railway up --detach",
|
|
73
|
-
"railway:migrate": "tsx src/cli/commands/migrate.ts",
|
|
74
|
-
"railway:schema:verify": "tsx scripts/verify-railway-schema.ts",
|
|
75
|
-
"railway:logs": "railway logs",
|
|
76
71
|
"claude:setup": "node scripts/setup-claude-integration.js",
|
|
77
72
|
"daemons:start": "node scripts/claude-sm-autostart.js",
|
|
78
73
|
"daemons:status": "node scripts/claude-sm-autostart.js status",
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Debug Railway build issues
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import fs from 'fs';
|
|
8
|
-
import path from 'path';
|
|
9
|
-
import { fileURLToPath } from 'url';
|
|
10
|
-
|
|
11
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
12
|
-
const __dirname = path.dirname(__filename);
|
|
13
|
-
|
|
14
|
-
console.log('š Railway Build Debugger');
|
|
15
|
-
console.log('========================\n');
|
|
16
|
-
|
|
17
|
-
// Check for server files
|
|
18
|
-
const serverDir = path.join(__dirname, '..', 'dist', 'servers', 'railway');
|
|
19
|
-
const srcDir = path.join(__dirname, '..', 'src', 'servers', 'railway');
|
|
20
|
-
|
|
21
|
-
console.log('š Checking dist/servers/railway:');
|
|
22
|
-
if (fs.existsSync(serverDir)) {
|
|
23
|
-
const files = fs.readdirSync(serverDir);
|
|
24
|
-
files.forEach(file => {
|
|
25
|
-
const stats = fs.statSync(path.join(serverDir, file));
|
|
26
|
-
console.log(` - ${file} (${stats.size} bytes, modified: ${stats.mtime.toISOString()})`);
|
|
27
|
-
|
|
28
|
-
// Check for minimal server references
|
|
29
|
-
if (file === 'index.js') {
|
|
30
|
-
const content = fs.readFileSync(path.join(serverDir, file), 'utf-8');
|
|
31
|
-
if (content.includes('Minimal')) {
|
|
32
|
-
console.log(` ā ļø Contains "Minimal" references`);
|
|
33
|
-
}
|
|
34
|
-
if (content.includes('/auth/signup')) {
|
|
35
|
-
console.log(` ā
Contains auth endpoints`);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
} else {
|
|
40
|
-
console.log(' ā Directory does not exist');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
console.log('\nš Checking src/servers/railway:');
|
|
44
|
-
if (fs.existsSync(srcDir)) {
|
|
45
|
-
const files = fs.readdirSync(srcDir);
|
|
46
|
-
files.forEach(file => {
|
|
47
|
-
const stats = fs.statSync(path.join(srcDir, file));
|
|
48
|
-
console.log(` - ${file} (${stats.size} bytes)`);
|
|
49
|
-
});
|
|
50
|
-
} else {
|
|
51
|
-
console.log(' ā Directory does not exist');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Check package.json scripts
|
|
55
|
-
console.log('\nš¦ Package.json start scripts:');
|
|
56
|
-
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf-8'));
|
|
57
|
-
Object.entries(packageJson.scripts).forEach(([key, value]) => {
|
|
58
|
-
if (key.includes('start')) {
|
|
59
|
-
console.log(` ${key}: ${value}`);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// Check Dockerfile
|
|
64
|
-
console.log('\nš³ Dockerfile CMD:');
|
|
65
|
-
const dockerfile = fs.readFileSync(path.join(__dirname, '..', 'Dockerfile'), 'utf-8');
|
|
66
|
-
const cmdMatch = dockerfile.match(/CMD\s+\[.*\]/g);
|
|
67
|
-
if (cmdMatch) {
|
|
68
|
-
cmdMatch.forEach(cmd => {
|
|
69
|
-
console.log(` ${cmd}`);
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Check Railway config
|
|
74
|
-
console.log('\nš Railway.json:');
|
|
75
|
-
const railwayConfig = path.join(__dirname, '..', 'railway.json');
|
|
76
|
-
if (fs.existsSync(railwayConfig)) {
|
|
77
|
-
const config = JSON.parse(fs.readFileSync(railwayConfig, 'utf-8'));
|
|
78
|
-
console.log(JSON.stringify(config, null, 2));
|
|
79
|
-
} else {
|
|
80
|
-
console.log(' ā railway.json not found');
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
console.log('\nš” Recommendations:');
|
|
84
|
-
console.log('1. Railway may be using a cached build layer');
|
|
85
|
-
console.log('2. Try changing the base image in Dockerfile to force rebuild');
|
|
86
|
-
console.log('3. Check Railway dashboard for any override settings');
|
|
87
|
-
console.log('4. Consider contacting Railway support about cache issues');
|