Skip to content

Instantly share code, notes, and snippets.

@wsargent
Last active February 24, 2025 17:25
Show Gist options
  • Select an option

  • Save wsargent/dde9e43c7dc19f3f1ebc35a0f893932c to your computer and use it in GitHub Desktop.

Select an option

Save wsargent/dde9e43c7dc19f3f1ebc35a0f893932c to your computer and use it in GitHub Desktop.
Python method to create due link in Letta
from datetime import datetime
from urllib.parse import urlencode, quote
# If we use Optional[int] here then it'll fail because it can't coerce it
# https://github.com/letta-ai/letta/blob/a678e6d51167bcdad0272ee5a6887118c6e5a2b6/letta/functions/ast_parsers.py#L6
# raise ValueError(f"Failed to coerce argument '{arg_name}' to {annotation_str}: {e}")
def create_add_due_url(
title: str,
duedate: str = None,
secslater: int = None,
minslater: int = None,
hourslater: int = None,
timezone: str = None,
autosnooze: int = None) -> str:
"""
Create a correctly formatted URL for adding a timer in the Due application.
Parameters
----------
title : str
The reminder text. Must be a non-empty string.
duedate : str, optional
The absolute due date and time of the timer in ISO 8601 format (e.g., "2023-10-01T14:30:00").
secslater : int, optional
The relative seconds from now.
minslater : int, optional
The relative minutes from now.
hourslater: int, optional
The relative hours from now.
timezone: str, optional
Valid timezone name (e.g., "GMT", "Asia/Singapore").
autosnooze: int, optional
The snooze minutes between reminders, must be one of {1, 5, 10, 15, 30, 60}
Returns:
str
The timer URL.
Raises:
ValueError: If any parameter is invalid.
Examples:
>>> create_add_due_url(
... title="Meeting Reminder",
... minslater=30,
... timezone="Asia/Singapore",
... autosnooze=10
... )
'due://x-callback-url/add?title=Meeting%20Reminder&minslater=30&timezone=Asia%2FSingapore&autosnooze=10'
>>> create_add_due_url(
... title="Lunch Break",
... duedate="2025-02-24T15:19:14+00:00"
... )
'due://x-callback-url/add?title=Lunch%20Break&duedate=1740439154'
"""
# Validate title
if not isinstance(title, str) or not title.strip():
raise ValueError("Title must be a non-empty string.")
# Validate timezone
if timezone is not None and timezone not in pytz.all_timezones:
raise ValueError(f"Invalid timezone '{timezone}'. Must be a valid timezone name.")
# Validate autosnooze
valid_autosnooze_options = [1, 5, 10, 15, 30, 60]
if autosnooze is not None and autosnooze not in valid_autosnooze_options:
raise ValueError(f"Invalid autosnooze value '{autosnooze}'. Must be one of {valid_autosnooze_options}.")
# Validate timestamps and time-based parameters
if duedate is not None:
if not datetime.fromisoformat(duedate):
raise ValueError("Due date must be a valid ISO 8601 date string.")
if secslater is not None:
if not isinstance(secslater, (int, float)) or secslater < 0:
raise ValueError("Seconds later must be a non-negative number.")
if minslater is not None:
if not isinstance(minslater, (int, float)) or minslater < 0:
raise ValueError("Minutes later must be a non-negative number.")
if hourslater is not None:
if not isinstance(hourslater, (int, float)) or hourslater < 0:
raise ValueError("Hours later must be a non-negative number.")
# If no time-based parameter is provided, raise an error
if duedate is None and secslater is None and minslater is None and hourslater is None:
raise ValueError("At least one of duedate, secslater, minslater, or hourslater must be provided.")
# Prepare parameters for URL
params = {'title': title}
if duedate is not None:
params['duedate'] = datetime.fromisoformat(duedate).timestamp()
if secslater is not None:
params['secslater'] = secslater
if minslater is not None:
params['minslater'] = minslater
if hourslater is not None:
params['hourslater'] = hourslater
if timezone:
params['timezone'] = timezone
if autosnooze is not None:
params['autosnooze'] = autosnooze
# URL encode the parameters without using quote plus
encoded_params = urlencode(params, quote_via=quote)
# Construct the full URL
url = f"due://x-callback-url/add?{encoded_params}"
return url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment