Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions autogpt_platform/backend/backend/data/auth/api_key.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import uuid
from datetime import datetime, timezone
from typing import Literal, Optional

Expand All @@ -11,6 +10,7 @@

from backend.data.includes import MAX_USER_API_KEYS_FETCH
from backend.util.exceptions import NotAuthorizedError, NotFoundError
from backend.util.ids import new_uuid

from .base import APIAuthorizationInfo

Expand Down Expand Up @@ -83,7 +83,7 @@ async def create_api_key(

saved_key_obj = await PrismaAPIKey.prisma().create(
data={
"id": str(uuid.uuid4()),
"id": new_uuid(),
"name": name,
"head": generated_key.head,
"tail": generated_key.tail,
Expand Down
9 changes: 5 additions & 4 deletions autogpt_platform/backend/backend/data/auth/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import hashlib
import logging
import secrets
import uuid
from datetime import datetime, timedelta, timezone
from typing import Literal, Optional

Expand All @@ -25,6 +24,8 @@
from prisma.types import OAuthApplicationUpdateInput
from pydantic import BaseModel, Field, SecretStr

from backend.util.ids import new_uuid

from .base import APIAuthorizationInfo

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -360,7 +361,7 @@ async def create_authorization_code(

saved_code = await PrismaOAuthAuthorizationCode.prisma().create(
data={
"id": str(uuid.uuid4()),
"id": new_uuid(),
"code": code,
"expiresAt": expires_at,
"applicationId": application_id,
Expand Down Expand Up @@ -491,7 +492,7 @@ async def create_access_token(

saved_token = await PrismaOAuthAccessToken.prisma().create(
data={
"id": str(uuid.uuid4()),
"id": new_uuid(),
"token": token_hash, # SHA256 hash for direct lookup
"expiresAt": expires_at,
"applicationId": application_id,
Expand Down Expand Up @@ -608,7 +609,7 @@ async def create_refresh_token(

saved_token = await PrismaOAuthRefreshToken.prisma().create(
data={
"id": str(uuid.uuid4()),
"id": new_uuid(),
"token": token_hash, # SHA256 hash for direct lookup
"expiresAt": expires_at,
"applicationId": application_id,
Expand Down
10 changes: 5 additions & 5 deletions autogpt_platform/backend/backend/data/graph.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio
import logging
import uuid
from collections import defaultdict
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Annotated, Any, Literal, Optional, Self, cast
Expand Down Expand Up @@ -31,6 +30,7 @@
from backend.integrations.providers import ProviderName
from backend.util import type as type_utils
from backend.util.exceptions import GraphNotAccessibleError, GraphNotInLibraryError
from backend.util.ids import new_uuid
from backend.util.json import SafeJson
from backend.util.models import Pagination
from backend.util.request import parse_url
Expand Down Expand Up @@ -646,8 +646,8 @@ def reassign_ids(self, user_id: str, reassign_graph_id: bool = False):
"""
if reassign_graph_id:
graph_id_map = {
self.id: str(uuid.uuid4()),
**{sub_graph.id: str(uuid.uuid4()) for sub_graph in self.sub_graphs},
self.id: new_uuid(),
**{sub_graph.id: new_uuid() for sub_graph in self.sub_graphs},
}
else:
graph_id_map = {}
Expand All @@ -667,7 +667,7 @@ def _reassign_ids(
graph.id = graph_id_map[graph.id]

# Reassign Node IDs
id_map = {node.id: str(uuid.uuid4()) for node in graph.nodes}
id_map = {node.id: new_uuid() for node in graph.nodes}
for node in graph.nodes:
node.id = id_map[node.id]

Expand Down Expand Up @@ -1708,7 +1708,7 @@ async def __create_graph(tx, graph: Graph, user_id: str):
await AgentNodeLink.prisma(tx).create_many(
data=[
AgentNodeLinkCreateInput(
id=str(uuid.uuid4()),
id=new_uuid(),
sourceName=link.source_name,
sinkName=link.sink_name,
agentNodeSourceId=link.source_id,
Expand Down
14 changes: 14 additions & 0 deletions autogpt_platform/backend/backend/util/ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Sortable identifier helpers."""

from uuid_utils import uuid7


def new_uuid() -> str:
"""Return a fresh sortable UUIDv7 as a lower-case canonical string.

Use this for any application-level ID generation that lands in a Prisma
column. Schema-level ``@default(dbgenerated("uuid_generate_v7()"))``
covers the path where Prisma populates the id; this helper is the
Python-side counterpart for code that needs the id before insert.
"""
return str(uuid7())
Comment thread
majdyz marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- Sortable UUIDv7 generator (RFC 9562). Built on gen_random_uuid()
-- (pgcrypto, already in use). The first 48 bits encode the unix
-- timestamp in milliseconds, so values are k-sortable on insert order.
CREATE OR REPLACE FUNCTION uuid_generate_v7()
RETURNS uuid
AS $$
BEGIN
RETURN encode(
set_bit(
set_bit(
overlay(
uuid_send(gen_random_uuid())
PLACING substring(int8send(floor(extract(epoch FROM clock_timestamp()) * 1000)::bigint) FROM 3)
FROM 1 FOR 6
),
52, 1
),
53, 1
),
'hex'
)::uuid;
END
$$
LANGUAGE plpgsql
Comment thread
majdyz marked this conversation as resolved.
VOLATILE;

-- Repoint existing id defaults from Prisma-client uuid()/gen_random_uuid() to uuid_generate_v7().
ALTER TABLE "UserOnboarding" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "CoPilotUnderstanding" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "UserWorkspace" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "UserWorkspaceFile" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "SharedExecutionFile" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "BuilderSearchHistory" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "ChatSession" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "ChatMessage" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "AgentGraph" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "AgentPreset" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "NotificationEvent" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "UserNotificationBatch" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "PushSubscription" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "LibraryAgent" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "LibraryFolder" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "AgentNode" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "AgentNodeLink" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "AgentBlock" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "AgentGraphExecution" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "AgentNodeExecution" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "AgentNodeExecutionInputOutput" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "IntegrationWebhook" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "AnalyticsDetails" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "AnalyticsMetrics" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "CreditTransaction" ALTER COLUMN "transactionKey" SET DEFAULT uuid_generate_v7();
ALTER TABLE "CreditRefundRequest" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "PlatformCostLog" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "Profile" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "StoreListing" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "StoreListingVersion" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "UnifiedContentEmbedding" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "StoreListingReview" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "APIKey" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "OAuthApplication" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "OAuthAuthorizationCode" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "OAuthAccessToken" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "OAuthRefreshToken" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "PlatformLink" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "PlatformUserLink" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
ALTER TABLE "PlatformLinkToken" ALTER COLUMN "id" SET DEFAULT uuid_generate_v7();
2 changes: 1 addition & 1 deletion autogpt_platform/backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions autogpt_platform/backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ supabase = "2.28.0"
tenacity = "^9.1.4"
todoist-api-python = "^2.1.7"
tweepy = "^4.16.0"
uuid-utils = "^0.14.1"
Comment thread
majdyz marked this conversation as resolved.
Outdated
uvicorn = { extras = ["standard"], version = "^0.40.0" }
websockets = "^15.0"
youtube-transcript-api = "^1.2.1"
Expand Down
Loading
Loading