Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions host-integrations/claude-code/stop-capture.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/usr/bin/env node

import { readFileSync } from "node:fs";

import { captureMessages } from "../../host-runtime.mjs";

function readStdin() {
return new Promise((resolve, reject) => {
const chunks = [];
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => chunks.push(chunk));
process.stdin.on("end", () => resolve(chunks.join("")));
process.stdin.on("error", reject);
});
}

function parseJson(text) {
if (!text || !text.trim()) return {};
try {
return JSON.parse(text);
} catch {
return {};
}
}

function extractTextBlocks(value) {
if (typeof value === "string") return [value];
if (!Array.isArray(value)) return [];
return value
.map((item) => {
if (typeof item === "string") return item;
if (!item || typeof item !== "object") return "";
if (typeof item.text === "string") return item.text;
if (typeof item.content === "string") return item.content;
return "";
})
.filter(Boolean);
}

function readRecentClaudeTurn(transcriptPath) {
const result = { user: "", assistant: "" };
if (typeof transcriptPath !== "string" || !transcriptPath.trim()) return result;

try {
const lines = readFileSync(transcriptPath, "utf8")
.split("\n")
.filter(Boolean);

for (let i = lines.length - 1; i >= 0; i -= 1) {
let entry;
try {
entry = JSON.parse(lines[i]);
} catch {
continue;
}

if (!result.assistant && entry?.type === "assistant") {
const message = entry.message;
if (typeof message?.content === "string" && message.content.trim()) {
result.assistant = message.content.trim();
continue;
}
const blocks = extractTextBlocks(message?.content);
if (blocks.length > 0) {
result.assistant = blocks.join("\n").trim();
continue;
}
}

if (!result.user && entry?.type === "user") {
const message = entry.message;
if (typeof message?.content === "string" && message.content.trim()) {
result.user = message.content.trim();
continue;
}
const blocks = extractTextBlocks(message?.content);
if (blocks.length > 0) {
result.user = blocks.join("\n").trim();
}
}

if (result.user && result.assistant) break;
}
} catch {}

return result;
}

async function main() {
try {
const payload = parseJson(await readStdin());
const lastAssistantMessage =
typeof payload.last_assistant_message === "string" && payload.last_assistant_message.trim()
? payload.last_assistant_message.trim()
: "";
const recentTurn = readRecentClaudeTurn(payload.transcript_path);
const texts = [
recentTurn.user,
lastAssistantMessage || recentTurn.assistant,
].filter(Boolean);

if (texts.length === 0) return;

await captureMessages({
texts,
sessionKey: `claude-code:${payload.session_id || payload.sessionId || "unknown"}`,
agentId: process.env.MEMORY_AGENT_ID || "main",
});
} catch (error) {
console.error(
`memory-lancedb-pro claude-code capture hook failed: ${error instanceof Error ? error.message : String(error)}`,
);
}
}

await main();
117 changes: 117 additions & 0 deletions host-integrations/claude-code/user-prompt-submit.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env node

import { readFileSync } from "node:fs";

import { recallMemories } from "../../host-runtime.mjs";

function readStdin() {
return new Promise((resolve, reject) => {
const chunks = [];
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => chunks.push(chunk));
process.stdin.on("end", () => resolve(chunks.join("")));
process.stdin.on("error", reject);
});
}

function parseJson(text) {
if (!text || !text.trim()) return {};
try {
return JSON.parse(text);
} catch {
return {};
}
}

function extractTextBlocks(value) {
if (typeof value === "string") return [value];
if (!Array.isArray(value)) return [];
return value
.map((item) => {
if (typeof item === "string") return item;
if (!item || typeof item !== "object") return "";
if (typeof item.text === "string") return item.text;
if (typeof item.content === "string") return item.content;
return "";
})
.filter(Boolean);
}

function readLastClaudeUserMessage(transcriptPath) {
if (typeof transcriptPath !== "string" || !transcriptPath.trim()) return "";
try {
const lines = readFileSync(transcriptPath, "utf8")
.split("\n")
.filter(Boolean);
for (let i = lines.length - 1; i >= 0; i -= 1) {
let entry;
try {
entry = JSON.parse(lines[i]);
} catch {
continue;
}
if (entry?.type !== "user") continue;
const message = entry.message;
if (typeof message?.content === "string") return message.content.trim();
const blocks = extractTextBlocks(message?.content);
if (blocks.length > 0) return blocks.join("\n").trim();
}
} catch {}
return "";
}

function resolvePrompt(payload) {
const directCandidates = [
payload.prompt,
payload.text,
payload.input,
payload.user_prompt,
payload.userPrompt,
payload.message,
payload.content,
];
for (const candidate of directCandidates) {
if (typeof candidate === "string" && candidate.trim()) {
return candidate.trim();
}
}

const nestedCandidates = [
payload.message?.content,
payload.user_message?.content,
payload.input_message?.content,
];
for (const candidate of nestedCandidates) {
if (typeof candidate === "string" && candidate.trim()) {
return candidate.trim();
}
const blocks = extractTextBlocks(candidate);
if (blocks.length > 0) return blocks.join("\n").trim();
}

return readLastClaudeUserMessage(payload.transcript_path);
}

async function main() {
try {
const payload = parseJson(await readStdin());
const prompt = resolvePrompt(payload);
if (!prompt) return;

const recall = await recallMemories({
query: prompt,
agentId: process.env.MEMORY_AGENT_ID || "main",
limit: 3,
});

if (recall?.text) {
process.stdout.write(`${recall.text}\n`);
}
} catch (error) {
console.error(
`memory-lancedb-pro claude-code recall hook failed: ${error instanceof Error ? error.message : String(error)}`,
);
}
}

await main();
58 changes: 58 additions & 0 deletions host-integrations/claude-desktop/controller.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env node

import net from "node:net";
const host = "127.0.0.1";
const port = 43129;
const [, , action, ...rest] = process.argv;

if (!["send", "new-chat", "read-last-assistant", "inspect-messages"].includes(action)) {
console.error('Usage: controller.mjs send "message text"');
console.error(" or: controller.mjs new-chat");
console.error(" or: controller.mjs read-last-assistant");
console.error(" or: controller.mjs inspect-messages");
process.exit(1);
}

const text = rest.join(" ").trim();
if (action === "send" && !text) {
console.error("Message text is required.");
process.exit(1);
}

const payload = JSON.stringify(
action === "send"
? { action: "send", text }
: action === "new-chat"
? { action: "new-chat" }
: action === "read-last-assistant"
? { action: "read-last-assistant" }
: { action: "inspect-messages" },
);

const client = net.createConnection({ host, port });
let response = "";

client.setEncoding("utf8");
client.on("connect", () => {
client.write(`${payload}\n`);
});
client.on("data", (chunk) => {
response += chunk;
});
client.on("end", () => {
if (!response.trim()) {
console.error("No response from Claude Desktop control server.");
process.exit(1);
}
console.log(response.trim());
});
client.on("close", () => {
if (!response.trim()) {
console.error("No response from Claude Desktop control server.");
process.exit(1);
}
});
client.on("error", (error) => {
console.error(`Claude Desktop control failed: ${error.message}`);
process.exit(1);
});
Loading