Skip to content

Commit

Permalink
chore: add breaking change dialog, other small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
louis030195 committed Jan 25, 2025
1 parent 14cda42 commit 3880dee
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 224 deletions.
2 changes: 1 addition & 1 deletion pipes/identify-speakers/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "search",
"name": "identify-speakers",
"version": "0.1.0",
"private": true,
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion pipes/linkedin-ai-assistant/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "linkedin_ai_assistant",
"name": "linkedin-ai-assistant",
"version": "0.1.0",
"private": true,
"type": "commonjs",
Expand Down
2 changes: 1 addition & 1 deletion pipes/meeting/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "search",
"name": "meeting",
"version": "0.1.0",
"private": true,
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion pipes/obsidian/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian",
"version": "0.1.2",
"version": "0.1.6",
"private": true,
"scripts": {
"dev": "next dev --turbopack",
Expand Down
1 change: 0 additions & 1 deletion pipes/obsidian/src/components/obsidian-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export function ObsidianSettings() {
const [intelligenceDeepLink, setIntelligenceDeepLink] = useState<
string | null
>(null);
console.log("settings", settings);
const [customPrompt, setCustomPrompt] = useState<string | null>(null);
const [testLogLoading, setTestLogLoading] = useState(false);
const [pathValidation, setPathValidation] = useState<{
Expand Down
226 changes: 124 additions & 102 deletions pipes/reddit-auto-posts/app/api/pipeline/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use server";
import fs from "node:fs";
import path from "node:path";
import { DailyLog } from "@/lib/types";
Expand All @@ -9,47 +8,59 @@ import generateDailyLog from "@/lib/actions/generate-log";
import generateRedditQuestions from "@/lib/actions/generate-reddit-question";

async function saveDailyLog(logEntry: DailyLog) {
if (!logEntry){
throw new Error("no log entry to save")
if (!logEntry) {
throw new Error("no log entry to save");
}
console.log("saving log entry:", logEntry);

const screenpipeDir = process.env.SCREENPIPE_DIR || process.cwd();
const logsDir = path.join(screenpipeDir, "pipes", "reddit-auto-posts", "logs");
const logsDir = path.join(
screenpipeDir,
"pipes",
"reddit-auto-posts",
"logs"
);
const timestamp = new Date()
.toISOString()
.replace(/:/g, "-")
.replace(/\..+/, "");
const filename = `${timestamp}-${logEntry.category?.replace(/[\/\\?%*:|"<>']/g, "-")}.json`;
const logFile = path.join(logsDir, filename)
const filename = `${timestamp}-${logEntry.category?.replace(
/[\/\\?%*:|"<>']/g,
"-"
)}.json`;
const logFile = path.join(logsDir, filename);
try {
fs.writeFileSync(logFile, JSON.stringify(logEntry, null, 2));
} catch (error) {
console.log(`Failed to write log file: ${error}`)
throw new Error(`failed to write log file: ${error}`)
console.log(`Failed to write log file: ${error}`);
throw new Error(`failed to write log file: ${error}`);
}
}

async function retry(fn: any, retries = 3, delay = 5000) {
for (let i = 0; i < retries; i++) {
try {
const result = await fn();
if (result){
if (result) {
return result;
}
} catch (error) {
console.log(`Screenpipe query failed, retry, attempt: ${i + 1}`)
console.log(`Screenpipe query failed, retry, attempt: ${i + 1}`);
if (i === retries - 1) throw error;
await new Promise(res => setTimeout(res, delay));
await new Promise((res) => setTimeout(res, delay));
}
}
}

export const dynamic = "force-dynamic";

export async function GET(request: NextRequest) {
try {
console.log("starting daily log pipeline");
const settingsManager = pipe.settings;
const redditSettings = await pipe.settings.getNamespaceSettings("reddit-auto-posts");
const redditSettings = await pipe.settings.getNamespaceSettings(
"reddit-auto-posts"
);

if (!settingsManager) {
return NextResponse.json(
Expand Down Expand Up @@ -77,16 +88,29 @@ export async function GET(request: NextRequest) {
const contentType = redditSettings?.contentType || "ocr";
const emailEnabled = !!(emailAddress && emailPassword);
const screenpipeDir = process.env.SCREENPIPE_DIR || process.cwd();
const logsDir = path.join(screenpipeDir, "pipes", "reddit-auto-posts", "logs");
const pipeConfigPath = path.join(screenpipeDir, "pipes", "reddit-auto-posts", "pipe.json");
const logsDir = path.join(
screenpipeDir,
"pipes",
"reddit-auto-posts",
"logs"
);
const pipeConfigPath = path.join(
screenpipeDir,
"pipes",
"reddit-auto-posts",
"pipe.json"
);

try {
fs.mkdirSync(logsDir);
} catch (_error) {
console.warn("creating logs directory, probably already exists:", logsDir);
console.warn(
"creating logs directory, probably already exists:",
logsDir
);
}

const fileContent = fs.readFileSync(pipeConfigPath, 'utf-8');
const fileContent = fs.readFileSync(pipeConfigPath, "utf-8");
const configData = JSON.parse(fileContent);
const url = new URL(request.url);
const fromButton = url.searchParams.get("fromButton");
Expand All @@ -110,123 +134,121 @@ export async function GET(request: NextRequest) {
"daily reddit questions",
welcomeEmail
);
configData.welcomeEmailSent = true;
fs.writeFileSync(pipeConfigPath, JSON.stringify(configData, null, 2));
configData.welcomeEmailSent = true;
fs.writeFileSync(pipeConfigPath, JSON.stringify(configData, null, 2));
} catch (error) {
configData.welcomeEmailSent = false;
fs.writeFileSync(pipeConfigPath, JSON.stringify(configData, null, 2));
configData.welcomeEmailSent = false;
fs.writeFileSync(pipeConfigPath, JSON.stringify(configData, null, 2));
return NextResponse.json(
{ error: `Error in sending welcome email: ${error}` },
{ status: 500 }
);
}
};
}

const now = new Date();
const startTime = new Date(now.getTime() - interval);
const screenData = await retry(() => pipe.queryScreenpipe({
const screenData = await retry(() =>
pipe.queryScreenpipe({
startTime: startTime.toISOString(),
endTime: now.toISOString(),
windowName: windowName,
limit: pageSize,
contentType: contentType,
}));
})
);

if (screenData && screenData.data && screenData.data.length > 0) {
if (aiProvider === "screenpipe-cloud" && !userToken) {
return NextResponse.json(
{ error: `seems like you don't have screenpipe-cloud access :(` },
{ status: 500 }
);
}

let logEntry: DailyLog | undefined;
logEntry = await generateDailyLog(
screenData.data,
dailylogPrompt,
aiProvider,
aiModel,
aiUrl,
openaiApiKey,
userToken as string
);
await saveDailyLog(logEntry);

const redditQuestions = await generateRedditQuestions(
screenData.data,
customPrompt,
aiProvider,
aiModel,
aiUrl,
openaiApiKey,
userToken as string
);
console.log("reddit questions:", redditQuestions);

if (screenData && screenData.data && screenData.data.length > 0) {
if (aiProvider === "screenpipe-cloud" && !userToken) {
// only send mail in those request that are made from cron jobs,
// cz at that time user, is not seeing the frontend of this pipe
if (emailEnabled && redditQuestions && !fromButton) {
try {
await sendEmail(
emailAddress!,
emailPassword!,
"reddit questions",
redditQuestions
);
} catch (error) {
return NextResponse.json(
{ error: `seems like you don't have screenpipe-cloud access :(` },
{ error: `error in sending mail ${error}` },
{ status: 500 }
);
}
} else {
console.log("Failed to get reddit questions!!");
}

let logEntry: DailyLog | undefined;
logEntry = await generateDailyLog(
screenData.data,
dailylogPrompt,
aiProvider,
aiModel,
aiUrl,
openaiApiKey,
userToken as string,
);
await saveDailyLog(logEntry);

const redditQuestions = await generateRedditQuestions(
screenData.data,
customPrompt,
aiProvider,
aiModel,
aiUrl,
openaiApiKey,
userToken as string,
);
console.log("reddit questions:", redditQuestions);

// only send mail in those request that are made from cron jobs,
// cz at that time user, is not seeing the frontend of this pipe
if (emailEnabled && redditQuestions && !fromButton) {
try {
await sendEmail(
emailAddress!,
emailPassword!,
"reddit questions",
redditQuestions
);
} catch(error) {
return NextResponse.json(
{ error: `error in sending mail ${error}` },
{ status: 500 }
);
}
} else {
console.log("Failed to get reddit questions!!")
}

if (redditQuestions) {
try {
console.log("Sending screenpipe inbox notification");
await pipe.inbox.send({
title: "reddit questions",
body: redditQuestions,
});
} catch(error) {
return NextResponse.json(
{ error: `error in sending inbox notification ${error}` },
{ status: 500 }
);
}
} else {
console.log("Failed to get reddit questions!!")
}

if (redditQuestions) {
try {
console.log("Sending desktop notification");
await pipe.sendDesktopNotification({
badge: "reddit questions",
body: "just sent you some reddit questions",
console.log("Sending screenpipe inbox notification");
await pipe.inbox.send({
title: "reddit questions",
body: redditQuestions,
});
} catch (error) {
return NextResponse.json(
{ error: `error in sending desktop notification ${error}` },
{ error: `error in sending inbox notification ${error}` },
{ status: 500 }
);
}
return NextResponse.json(
{ message: "pipe executed successfully", suggestedQuestions: redditQuestions },
{ status: 200 }
);
} else {
console.log("Failed to get reddit questions!!");
}

try {
console.log("Sending desktop notification");
} catch (error) {
return NextResponse.json(
{ message: "query is empty please wait & and try again!" },
{ status: 200 }
{ error: `error in sending desktop notification ${error}` },
{ status: 500 }
);
};
}
return NextResponse.json(
{
message: "pipe executed successfully",
suggestedQuestions: redditQuestions,
},
{ status: 200 }
);
} else {
return NextResponse.json(
{ message: "query is empty please wait & and try again!" },
{ status: 200 }
);
}
} catch (error) {
console.error("error in GET handler:", error);
return NextResponse.json(
{ error: `${error}` },
{ status: 400 }
);
return NextResponse.json({ error: `${error}` }, { status: 400 });
}
}
Loading

0 comments on commit 3880dee

Please sign in to comment.