forked from openbsd/src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar-parser.ts
More file actions
568 lines (500 loc) · 22.1 KB
/
Copy pathcalendar-parser.ts
File metadata and controls
568 lines (500 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import { createServerFn } from "@tanstack/react-start";
import { z } from "zod";
const GATEWAY_AI_URL = "https://ai.gateway.lovable.dev/v1/chat/completions";
const GATEWAY_CAL_URL = "https://connector-gateway.lovable.dev/google_calendar/calendar/v3";
const ParsedEventSchema = z.object({
title: z.string().min(1).max(500),
date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Date must be in YYYY-MM-DD format"),
startTime: z.string().regex(/^\d{2}:\d{2}$/, "Time must be in HH:mm format"),
endTime: z.string().regex(/^\d{2}:\d{2}$/, "Time must be in HH:mm format"),
location: z.string().max(1000).nullable().optional(),
description: z.string().max(5000).nullable().optional(),
allDay: z.boolean().optional(),
reminderMinutes: z.number().int().min(0).max(10080).nullable().optional(),
assumptions: z.array(z.string().max(500)).max(10).optional(),
warnings: z.array(z.string().max(500)).max(10).optional(),
alternativeSlots: z.array(z.object({
startTime: z.string().regex(/^\d{2}:\d{2}$/),
endTime: z.string().regex(/^\d{2}:\d{2}$/),
})).max(3).optional(),
followUpQuestion: z.string().max(1000).nullable().optional(),
}).refine(
(data: any) => {
if (data.allDay) return true; // no time validation for all-day events
const [startH, startM] = data.startTime.split(":").map(Number);
const [endH, endM] = data.endTime.split(":").map(Number);
const startMins = startH * 60 + startM;
const endMins = endH * 60 + endM;
return endMins > startMins;
},
{
message: "endTime must be after startTime",
path: ["endTime"],
}
);
export type ParsedEvent = z.infer<typeof ParsedEventSchema>;
export type DayEvent = {
summary: string;
start: string; // HH:mm or "כל היום"
end: string;
};
type CalendarEvent = {
summary?: string;
start?: { dateTime?: string; date?: string };
end?: { dateTime?: string; date?: string };
};
// מושך את האירועים לשבוע הקרוב כדי לתת ל-AI קונטקסט על חפיפות
async function fetchUpcomingEvents(
nowIso: string,
tz: string,
apiKey: string,
connKey: string,
): Promise<DayEvent[]> {
try {
const timeMin = encodeURIComponent(nowIso);
// מחשב תאריך מקסימלי: 7 ימים מהיום
// Parse ISO string properly using UTC to avoid timezone issues
const now = new Date(nowIso);
if (isNaN(now.getTime())) {
console.error("Invalid ISO date string:", nowIso);
return [];
}
const maxDate = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
const timeMax = encodeURIComponent(maxDate.toISOString());
const url = `${GATEWAY_CAL_URL}/calendars/primary/events?singleEvents=true&orderBy=startTime&timeMin=${timeMin}&timeMax=${timeMax}&timeZone=${encodeURIComponent(tz)}&maxResults=30`;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout
try {
const res = await fetch(url, {
headers: {
Authorization: `Bearer ${apiKey}`,
"X-Connection-Api-Key": connKey,
},
signal: controller.signal,
});
if (!res.ok) {
console.warn(`Calendar fetch returned ${res.status}`);
return [];
}
const json = (await res.json()) as { items?: CalendarEvent[] };
const items = json.items ?? [];
return items.map((it) => {
const startDateStr = it.start?.dateTime
? new Date(it.start.dateTime).toLocaleString("he-IL", { weekday: "short", day: "numeric", month: "numeric", hour: "2-digit", minute: "2-digit", timeZone: tz })
: it.start?.date ? `${it.start.date} (כל היום)` : "לא ידוע";
const endDateStr = it.end?.dateTime
? new Date(it.end.dateTime).toLocaleTimeString("he-IL", { hour: "2-digit", minute: "2-digit", timeZone: tz })
: "";
return {
summary: it.summary ?? "(ללא כותרת)",
start: startDateStr,
end: endDateStr
};
});
} finally {
clearTimeout(timeoutId);
}
} catch (e) {
if (e instanceof Error && e.name === "AbortError") {
console.error("Calendar fetch timeout");
} else {
console.error("fetchUpcomingEvents failed", e);
}
return [];
}
}
/**
* Increments a date string (YYYY-MM-DD) by N days.
* Works with UTC to avoid timezone issues.
*/
function addDaysToDateString(dateStr: string, days: number): string {
// Validate date format
if (!/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
throw new Error(`Invalid date format: ${dateStr}. Expected YYYY-MM-DD`);
}
const parts = dateStr.split("-");
const year = parseInt(parts[0], 10);
const month = parseInt(parts[1], 10) - 1;
const date = parseInt(parts[2], 10);
const d = new Date(Date.UTC(year, month, date));
d.setUTCDate(d.getUTCDate() + days);
const y = d.getUTCFullYear();
const m = String(d.getUTCMonth() + 1).padStart(2, "0");
const day = String(d.getUTCDate()).padStart(2, "0");
return `${y}-${m}-${day}`;
}
export const parseEventFromText = createServerFn({ method: "POST" })
.inputValidator((input: { text: string; nowIso: string; tz: string; profile?: string }) => {
const schema = z
.object({
text: z.string().min(1).max(2000),
nowIso: z.string().regex(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(.\d{3})?Z?$/, "Invalid ISO date format"),
tz: z.string().min(1).max(64),
profile: z.string().max(2000).optional(),
});
return schema.parse(input);
})
.handler(async ({ data }: { data: { text: string; nowIso: string; tz: string; profile?: string } }) => {
const LOVABLE_API_KEY = process.env.LOVABLE_API_KEY?.trim();
const GOOGLE_CALENDAR_API_KEY = process.env.GOOGLE_CALENDAR_API_KEY?.trim();
if (!LOVABLE_API_KEY) {
console.error("LOVABLE_API_KEY environment variable not set");
return { ok: false as const, error: "LOVABLE_API_KEY חסר" };
}
const agendaPromise = GOOGLE_CALENDAR_API_KEY
? fetchUpcomingEvents(data.nowIso, data.tz, LOVABLE_API_KEY, GOOGLE_CALENDAR_API_KEY)
: Promise.resolve<DayEvent[]>([]);
const profileBlock = data.profile?.trim()
? `\n\nפרופיל אישי של המשתמש (קונטקסט חיוני — שמות, מקומות, תפקידים, העדפות, אילוצים):\n"""\n${data.profile.trim()}\n"""\nכשרלוונטי, השלם פרטים חסרים מהפרופיל (מקום קבוע, משך אופייני, כותרת מדויקת) — אבל אל תמציא מידע שלא נתמך.`
: "";
const agenda = await agendaPromise;
const agendaBlock = agenda.length
? `\n\nהאירועים ביומן לשבוע הקרוב:\n${agenda.map((a) => `- ${a.start}${a.end ? ` עד ${a.end}` : ""}: ${a.summary}`).join("\n")}\n* בדוק תמיד אם התאריך/שעה שהמשתמש ביקש מתנגשים עם אחד מהאירועים ברשימה זו. אם כן, הוסף אזהרה (warning) מפורטת והצע זמנים חלופיים (alternativeSlots).`
: "";
const systemPrompt = `אתה Chief of Staff אישי חכם בעברית. אתה לא רק מחלץ פרטים — אתה חושב לעומק על האירוע ועוזר למשתמש להחליט נכון.
הזמן הנוכחי: ${data.nowIso} (אזור זמן: ${data.tz}).
כללי חשיבה:
1. תאריכים יחסיים ("מחר", "ביום שני הבא", "בעוד שבועיים", "בערב", "אחה״צ") — פרש ביחס לזמן הנוכחי בצורה מדויקת.
2. אם אין שעת התחלה כלל → allDay=true ושעות 00:00–23:59.
3. אם יש שעת התחלה ואין סיום → הסק משך הגיוני לפי סוג האירוע (פגישה=שעה, ארוחה=שעה, משמרת=8, סדנה=3 וכו').
4. הסק תזכורת חכמה (reminderMinutes): פגישה רגילה=30, נסיעה רחוקה=60, אירוע משפחתי=120, פגישה רפואית=180. אם המשתמש ציין במפורש — כבד אותו.
5. assumptions: רשימת הנחות שעשית שלא היו במפורש בטקסט (למשל "הנחתי שמשך המשמרת 8 שעות לפי הפרופיל"). תהיה שקוף.
6. warnings: אזהרות מעשיות — חפיפה עם אירוע קיים, סוף שבוע, שעה מאוד מאוחרת/מוקדמת, אילוץ שמופיע בפרופיל ("המשתמש ציין שלא לקבוע בשישי אחה״צ").
7. alternativeSlots: אם יש חפיפה או התנגשות עם אילוצי המשתמש, הצע עד 3 חלונות זמן פנויים באותו יום או יום אחרי.
8. followUpQuestion: רק אם משהו ממש לא ברור ודרוש להבהיר — אחרת null.
9. תאריך בפורמט YYYY-MM-DD, שעות HH:mm (24).${profileBlock}${agendaBlock}`;
const res = await fetch(GATEWAY_AI_URL, {
method: "POST",
headers: {
Authorization: `Bearer ${LOVABLE_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "anthropic/claude-3-5-sonnet",
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: data.text },
],
tools: [
{
type: "function",
function: {
name: "create_event",
description: "פרטי אירוע יומן + חשיבה מעמיקה",
parameters: {
type: "object",
properties: {
title: { type: "string" },
date: { type: "string", description: "YYYY-MM-DD" },
startTime: { type: "string", description: "HH:mm" },
endTime: { type: "string", description: "HH:mm" },
location: { type: "string" },
description: { type: "string" },
allDay: { type: "boolean" },
reminderMinutes: {
type: "number",
description: "כמה דקות לפני האירוע להתריע (0 = ללא תזכורת)",
},
assumptions: {
type: "array",
items: { type: "string" },
description: "הנחות שהמודל עשה כדי להשלים פרטים חסרים",
},
warnings: {
type: "array",
items: { type: "string" },
description: "אזהרות מעשיות למשתמש (חפיפה, אילוץ מהפרופיל וכו')",
},
alternativeSlots: {
type: "array",
items: {
type: "object",
properties: {
startTime: { type: "string", description: "HH:mm" },
endTime: { type: "string", description: "HH:mm" },
},
required: ["startTime", "endTime"],
},
description: "הצעות לזמנים חלופיים במקרה של התנגשות",
},
followUpQuestion: {
type: "string",
description: "שאלת הבהרה — רק אם משהו לא ברור באמת",
},
},
required: ["title", "date", "startTime", "endTime"],
additionalProperties: false,
},
},
},
],
tool_choice: { type: "function", function: { name: "create_event" } },
reasoning: { effort: "medium" },
}),
signal: AbortSignal.timeout(30000), // 30 second timeout
});
if (res.status === 429) {
return { ok: false as const, error: "הגעת למגבלת הקצב, נסה שוב בעוד דקה" };
}
if (res.status === 402) {
return { ok: false as const, error: "נגמרו הקרדיטים ב-Lovable AI" };
}
if (res.status === 401 || res.status === 403) {
console.error("Auth error from AI gateway", res.status);
return { ok: false as const, error: "בעיה בהרשאה ל-AI" };
}
if (!res.ok) {
const t = await res.text();
console.error("AI gateway error", res.status, t);
return { ok: false as const, error: `שגיאת AI (${res.status})` };
}
let json: unknown;
try {
json = await res.json();
} catch (e) {
console.error("Failed to parse AI response", e);
return { ok: false as const, error: "תגובת ה-AI לא תקינה" };
}
const toolCall = (json as {
choices?: Array<{
message?: {
tool_calls?: Array<{
function: {
arguments: string;
};
}>;
};
}>;
}).choices?.[0]?.message?.tool_calls?.[0];
if (!toolCall?.function?.arguments) {
console.error("Missing tool call in AI response", json);
return { ok: false as const, error: "ה-AI לא הצליח לחלץ אירוע" };
}
try {
const args = JSON.parse(toolCall.function.arguments) as unknown;
const parsed = ParsedEventSchema.parse(args);
return { ok: true as const, event: parsed, agenda };
} catch (e) {
console.error("parse error", e);
return { ok: false as const, error: "פלט ה-AI לא תקין" };
}
});
export const insertCalendarEvent = createServerFn({ method: "POST" })
.inputValidator((input: { event: ParsedEvent; tz: string }) => {
return z
.object({
event: ParsedEventSchema,
tz: z.string().min(1).max(64),
})
.parse(input);
})
.handler(async ({ data }: { data: { event: ParsedEvent; tz: string } }) => {
const LOVABLE_API_KEY = process.env.LOVABLE_API_KEY?.trim();
const GOOGLE_CALENDAR_API_KEY = process.env.GOOGLE_CALENDAR_API_KEY?.trim();
if (!LOVABLE_API_KEY) {
console.error("LOVABLE_API_KEY environment variable not set");
return { ok: false as const, error: "LOVABLE_API_KEY חסר" };
}
if (!GOOGLE_CALENDAR_API_KEY) {
console.error("GOOGLE_CALENDAR_API_KEY environment variable not set");
return { ok: false as const, error: "Google Calendar לא מחובר" };
}
const { event, tz } = data;
const reminders =
event.reminderMinutes != null && event.reminderMinutes > 0
? {
useDefault: false,
overrides: [{ method: "popup", minutes: event.reminderMinutes }],
}
: undefined;
// חישוב תאריך סיום תקין לאירוע של יום שלם (בלי בעיות timezone)
const allDayEndDate = event.allDay ? addDaysToDateString(event.date, 1) : event.date;
const body = event.allDay
? {
summary: event.title,
location: event.location ?? undefined,
description: event.description ?? undefined,
start: { date: event.date },
end: { date: allDayEndDate },
reminders,
}
: {
summary: event.title,
location: event.location ?? undefined,
description: event.description ?? undefined,
start: { dateTime: `${event.date}T${event.startTime}:00`, timeZone: tz },
end: { dateTime: `${event.date}T${event.endTime}:00`, timeZone: tz },
reminders,
};
const res = await fetch(`${GATEWAY_CAL_URL}/calendars/primary/events`, {
method: "POST",
headers: {
Authorization: `Bearer ${LOVABLE_API_KEY}`,
"X-Connection-Api-Key": GOOGLE_CALENDAR_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
signal: AbortSignal.timeout(15000), // 15 second timeout
});
if (res.status === 401 || res.status === 403) {
console.error("Auth error when inserting calendar event", res.status);
return { ok: false as const, error: "בעיה בהרשאה ליומן" };
}
if (!res.ok) {
const t = await res.text();
console.error("Calendar insert failed", res.status, t);
return { ok: false as const, error: `שגיאת יומן (${res.status})` };
}
let json: unknown;
try {
json = await res.json();
} catch (e) {
console.error("Failed to parse calendar response", e);
return { ok: false as const, error: "תגובת היומן לא תקינה" };
}
const id = (json as { id?: string }).id;
const link = (json as { htmlLink?: string }).htmlLink;
if (!id) {
console.error("Missing event ID in calendar response", json);
return { ok: false as const, error: "האירוע לא נוצר כראוי" };
}
return {
ok: true as const,
eventId: id,
htmlLink: link,
};
});
export const updateCalendarEvent = createServerFn({ method: "POST" })
.inputValidator((input: { eventId: string; event: ParsedEvent; tz: string }) => {
return z
.object({
eventId: z.string().min(1),
event: ParsedEventSchema,
tz: z.string().min(1).max(64),
})
.parse(input);
})
.handler(async ({ data }: { data: { eventId: string; event: ParsedEvent; tz: string } }) => {
const LOVABLE_API_KEY = process.env.LOVABLE_API_KEY?.trim();
const GOOGLE_CALENDAR_API_KEY = process.env.GOOGLE_CALENDAR_API_KEY?.trim();
if (!LOVABLE_API_KEY || !GOOGLE_CALENDAR_API_KEY) {
return { ok: false as const, error: "חסר מפתח API או חיבור ליומן" };
}
const { eventId, event, tz } = data;
const allDayEndDate = event.allDay ? addDaysToDateString(event.date, 1) : event.date;
const body = event.allDay
? {
summary: event.title,
location: event.location ?? undefined,
description: event.description ?? undefined,
start: { date: event.date },
end: { date: allDayEndDate },
}
: {
summary: event.title,
location: event.location ?? undefined,
description: event.description ?? undefined,
start: { dateTime: `${event.date}T${event.startTime}:00`, timeZone: tz },
end: { dateTime: `${event.date}T${event.endTime}:00`, timeZone: tz },
};
const res = await fetch(`${GATEWAY_CAL_URL}/calendars/primary/events/${eventId}`, {
method: "PUT",
headers: {
Authorization: `Bearer ${LOVABLE_API_KEY}`,
"X-Connection-Api-Key": GOOGLE_CALENDAR_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!res.ok) {
return { ok: false as const, error: `שגיאת עדכון יומן (${res.status})` };
}
return { ok: true as const };
});
export const deleteCalendarEvent = createServerFn({ method: "POST" })
.inputValidator((input: { eventId: string }) => {
return z.object({ eventId: z.string().min(1) }).parse(input);
})
.handler(async ({ data }: { data: { eventId: string } }) => {
const LOVABLE_API_KEY = process.env.LOVABLE_API_KEY?.trim();
const GOOGLE_CALENDAR_API_KEY = process.env.GOOGLE_CALENDAR_API_KEY?.trim();
if (!LOVABLE_API_KEY || !GOOGLE_CALENDAR_API_KEY) {
return { ok: false as const, error: "חסר מפתח API או חיבור ליומן" };
}
const res = await fetch(`${GATEWAY_CAL_URL}/calendars/primary/events/${data.eventId}`, {
method: "DELETE",
headers: {
Authorization: `Bearer ${LOVABLE_API_KEY}`,
"X-Connection-Api-Key": GOOGLE_CALENDAR_API_KEY,
},
});
if (!res.ok && res.status !== 204) {
return { ok: false as const, error: `שגיאת מחיקת יומן (${res.status})` };
}
return { ok: true as const };
});
export const listCalendarEvents = createServerFn({ method: "GET" })
.inputValidator((input: { timeMin: string; timeMax: string; tz: string }) => {
return z.object({
timeMin: z.string(),
timeMax: z.string(),
tz: z.string(),
}).parse(input);
})
.handler(async ({ data }: { data: { timeMin: string; timeMax: string; tz: string } }) => {
const LOVABLE_API_KEY = process.env.LOVABLE_API_KEY?.trim();
const GOOGLE_CALENDAR_API_KEY = process.env.GOOGLE_CALENDAR_API_KEY?.trim();
if (!LOVABLE_API_KEY || !GOOGLE_CALENDAR_API_KEY) {
return { ok: false as const, error: "חסר מפתח API או חיבור ליומן" };
}
const url = `${GATEWAY_CAL_URL}/calendars/primary/events?singleEvents=true&orderBy=startTime&timeMin=${encodeURIComponent(data.timeMin)}&timeMax=${encodeURIComponent(data.timeMax)}&timeZone=${encodeURIComponent(data.tz)}`;
const res = await fetch(url, {
headers: {
Authorization: `Bearer ${LOVABLE_API_KEY}`,
"X-Connection-Api-Key": GOOGLE_CALENDAR_API_KEY,
},
});
if (!res.ok) {
return { ok: false as const, error: `שגיאת משיכת יומן (${res.status})` };
}
const json = await res.json() as { items?: any[] };
return { ok: true as const, events: json.items ?? [] };
});
export const saveOnboardingProfile = createServerFn({ method: "POST" })
.inputValidator((input: {
workHours: string;
preferredMeetingTimes: string;
deepWorkTimes: string;
workoutTimes: string;
otherPreferences: string;
}) => {
return z.object({
workHours: z.string().max(500),
preferredMeetingTimes: z.string().max(500),
deepWorkTimes: z.string().max(500),
workoutTimes: z.string().max(500),
otherPreferences: z.string().max(1000),
}).parse(input);
})
.handler(async ({ data }: { data: {
workHours: string;
preferredMeetingTimes: string;
deepWorkTimes: string;
workoutTimes: string;
otherPreferences: string;
} }) => {
const LOVABLE_API_KEY = process.env.LOVABLE_API_KEY?.trim();
if (!LOVABLE_API_KEY) {
return { ok: false as const, error: "LOVABLE_API_KEY חסר" };
}
// כאן בדרך כלל תהיה קריאה ל-Supabase/DB לשמירת הפרופיל
// כרגע נחזיר את הפרופיל המעובד כטקסט שה-AI יוכל להשתמש בו כקונטקסט
const profileText = `
שעות עבודה: ${data.workHours}
זמני פגישות מועדפים: ${data.preferredMeetingTimes}
זמני עבודה עמוקה: ${data.deepWorkTimes}
זמני אימון: ${data.workoutTimes}
העדפות נוספות: ${data.otherPreferences}
`.trim();
return { ok: true as const, profileText };
});