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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import time
import uuid
from typing import Optional

from sqlalchemy.orm import Session
from open_webui.internal.db import Base, JSONField, get_db, get_db_context
from open_webui.models.groups import Groups
from open_webui.models.users import Users, UserResponse
from open_webui.models.prompt_history import PromptHistories
from open_webui.models.access_grants import AccessGrantModel, AccessGrants


from pydantic import BaseModel, ConfigDict, Field
from sqlalchemy import BigInteger, Boolean, Column, String, Text, JSON, or_, func, cast

####################
# Prompts DB Schema
####################


class Prompt(Base):
    __tablename__ = "prompt"

    id = Column(Text, primary_key=True)
    command = Column(String, unique=True, index=True)
    user_id = Column(String)
    name = Column(Text)
    content = Column(Text)
    data = Column(JSON, nullable=True)
    meta = Column(JSON, nullable=True)
    tags = Column(JSON, nullable=True)
    is_active = Column(Boolean, default=True)
    version_id = Column(Text, nullable=True)  # Points to active history entry
    created_at = Column(BigInteger, nullable=True)
    updated_at = Column(BigInteger, nullable=True)


class PromptModel(BaseModel):
    id: Optional[str] = None
    command: str
    user_id: str
    name: str
    content: str
    data: Optional[dict] = None
    meta: Optional[dict] = None
    tags: Optional[list[str]] = None
    is_active: Optional[bool] = True
    version_id: Optional[str] = None
    created_at: Optional[int] = None
    updated_at: Optional[int] = None
    access_grants: list[AccessGrantModel] = Field(default_factory=list)

    model_config = ConfigDict(from_attributes=True)


####################
# Forms
####################


class PromptUserResponse(PromptModel):
    user: Optional[UserResponse] = None


class PromptAccessResponse(PromptUserResponse):
    write_access: Optional[bool] = False


class PromptListResponse(BaseModel):
    items: list[PromptUserResponse]
    total: int


class PromptAccessListResponse(BaseModel):
    items: list[PromptAccessResponse]
    total: int


class PromptForm(BaseModel):

    command: str
    name: str  # Changed from title
    content: str
    data: Optional[dict] = None
    meta: Optional[dict] = None
    tags: Optional[list[str]] = None
    access_grants: Optional[list[dict]] = None
    version_id: Optional[str] = None  # Active version
    commit_message: Optional[str] = None  # For history tracking
    is_production: Optional[bool] = True  # Whether to set new version as production


class PromptsTable:
    def _get_access_grants(
        self, prompt_id: str, db: Optional[Session] = None
    ) -> list[AccessGrantModel]:
        return AccessGrants.get_grants_by_resource("prompt", prompt_id, db=db)

    def _to_prompt_model(
        self, prompt: Prompt, db: Optional[Session] = None
    ) -> PromptModel:
        prompt_data = PromptModel.model_validate(prompt).model_dump(
            exclude={"access_grants"}
        )
        prompt_data["access_grants"] = self._get_access_grants(prompt_data["id"], db=db)
        return PromptModel.model_validate(prompt_data)

    def insert_new_prompt(
        self, user_id: str, form_data: PromptForm, db: Optional[Session] = None
    ) -> Optional[PromptModel]:
        now = int(time.time())
        prompt_id = str(uuid.uuid4())

        prompt = PromptModel(
            id=prompt_id,
            user_id=user_id,
            command=form_data.command,
            name=form_data.name,
            content=form_data.content,
            data=form_data.data or {},
            meta=form_data.meta or {},
            tags=form_data.tags or [],
            access_grants=[],
            is_active=True,
            created_at=now,
            updated_at=now,
        )

        try:
            with get_db_context(db) as db:
                result = Prompt(**prompt.model_dump(exclude={"access_grants"}))
                db.add(result)
                db.commit()
                db.refresh(result)
                AccessGrants.set_access_grants(
                    "prompt", prompt_id, form_data.access_grants, db=db
                )

                if result:
                    current_access_grants = self._get_access_grants(prompt_id, db=db)
                    snapshot = {
                        "name": form_data.name,
                        "content": form_data.content,
                        "command": form_data.command,
                        "data": form_data.data or {},
                        "meta": form_data.meta or {},
                        "tags": form_data.tags or [],
                        "access_grants": [
                            grant.model_dump() for grant in current_access_grants
                        ],
                    }

                    history_entry = PromptHistories.create_history_entry(
                        prompt_id=prompt_id,
                        snapshot=snapshot,
                        user_id=user_id,
                        parent_id=None,  # Initial commit has no parent
                        commit_message=form_data.commit_message or "Initial version",
                        db=db,
                    )

                    # Set the initial version as the production version
                    if history_entry:
                        result.version_id = history_entry.id
                        db.commit()
                        db.refresh(result)

                    return self._to_prompt_model(result, db=db)
                else:
                    return None
        except Exception:
            return None

    def get_prompt_by_id(
        self, prompt_id: str, db: Optional[Session] = None
    ) -> Optional[PromptModel]:
        """Get prompt by UUID."""
        try:
            with get_db_context(db) as db:
                prompt = db.query(Prompt).filter_by(id=prompt_id).first()
                if prompt:
                    return self._to_prompt_model(prompt, db=db)
                return None
        except Exception:
            return None

    def get_prompt_by_command(
        self, command: str, db: Optional[Session] = None
    ) -> Optional[PromptModel]:
        try:
            with get_db_context(db) as db:
                prompt = db.query(Prompt).filter_by(command=command).first()
                if prompt:
                    return self._to_prompt_model(prompt, db=db)
                return None
        except Exception:
            return None

    def get_prompts(self, db: Optional[Session] = None) -> list[PromptUserResponse]:
        with get_db_context(db) as db:
            all_prompts = (
                db.query(Prompt)
                .filter(Prompt.is_active == True)
                .order_by(Prompt.updated_at.desc())
                .all()
            )

            user_ids = list(set(prompt.user_id for prompt in all_prompts))

            users = Users.get_users_by_user_ids(user_ids, db=db) if user_ids else []
            users_dict = {user.id: user for user in users}

            prompts = []
            for prompt in all_prompts:
                user = users_dict.get(prompt.user_id)
                prompts.append(
                    PromptUserResponse.model_validate(
                        {
                            **self._to_prompt_model(prompt, db=db).model_dump(),
                            "user": user.model_dump() if user else None,
                        }
                    )
                )

            return prompts

    def get_prompts_by_user_id(
        self, user_id: str, permission: str = "write", db: Optional[Session] = None
    ) -> list[PromptUserResponse]:
        prompts = self.get_prompts(db=db)
        user_group_ids = {
            group.id for group in Groups.get_groups_by_member_id(user_id, db=db)
        }

        return [
            prompt
            for prompt in prompts
            if prompt.user_id == user_id
            or AccessGrants.has_access(
                user_id=user_id,
                resource_type="prompt",
                resource_id=prompt.id,
                permission=permission,
                user_group_ids=user_group_ids,
                db=db,
            )
        ]

    def search_prompts(
        self,
        user_id: str,
        filter: dict = {},
        skip: int = 0,
        limit: int = 30,
        db: Optional[Session] = None,
    ) -> PromptListResponse:
        with get_db_context(db) as db:
            from open_webui.models.users import User, UserModel

            # Join with User table for user filtering and sorting
            query = db.query(Prompt, User).outerjoin(User, User.id == Prompt.user_id)
            query = query.filter(Prompt.is_active == True)

            if filter:
                query_key = filter.get("query")
                if query_key:
                    query = query.filter(
                        or_(
                            Prompt.name.ilike(f"%{query_key}%"),
                            Prompt.command.ilike(f"%{query_key}%"),
                            Prompt.content.ilike(f"%{query_key}%"),
                            User.name.ilike(f"%{query_key}%"),
                            User.email.ilike(f"%{query_key}%"),
                        )
                    )

                view_option = filter.get("view_option")
                if view_option == "created":
                    query = query.filter(Prompt.user_id == user_id)
                elif view_option == "shared":
                    query = query.filter(Prompt.user_id != user_id)

                # Apply access grant filtering
                query = AccessGrants.has_permission_filter(
                    db=db,
                    query=query,
                    DocumentModel=Prompt,
                    filter=filter,
                    resource_type="prompt",
                    permission="read",
                )

                tag = filter.get("tag")
                if tag:
                    # Search for tag in JSON array field
                    like_pattern = f'%"{tag.lower()}"%'
                    tags_text = func.lower(cast(Prompt.tags, String))
                    query = query.filter(tags_text.like(like_pattern))

                order_by = filter.get("order_by")
                direction = filter.get("direction")

                if order_by == "name":
                    if direction == "asc":
                        query = query.order_by(Prompt.name.asc())
                    else:
                        query = query.order_by(Prompt.name.desc())
                elif order_by == "created_at":
                    if direction == "asc":
                        query = query.order_by(Prompt.created_at.asc())
                    else:
                        query = query.order_by(Prompt.created_at.desc())
                elif order_by == "updated_at":
                    if direction == "asc":
                        query = query.order_by(Prompt.updated_at.asc())
                    else:
                        query = query.order_by(Prompt.updated_at.desc())
                else:
                    query = query.order_by(Prompt.updated_at.desc())
            else:
                query = query.order_by(Prompt.updated_at.desc())

            # Count BEFORE pagination
            total = query.count()

            if skip:
                query = query.offset(skip)
            if limit:
                query = query.limit(limit)

            items = query.all()

            prompts = []
            for prompt, user in items:
                prompts.append(
                    PromptUserResponse(
                        **self._to_prompt_model(prompt, db=db).model_dump(),
                        user=(
                            UserResponse(**UserModel.model_validate(user).model_dump())
                            if user
                            else None
                        ),
                    )
                )

            return PromptListResponse(items=prompts, total=total)

    def update_prompt_by_command(
        self,
        command: str,
        form_data: PromptForm,
        user_id: str,
        db: Optional[Session] = None,
    ) -> Optional[PromptModel]:
        try:
            with get_db_context(db) as db:
                prompt = db.query(Prompt).filter_by(command=command).first()
                if not prompt:
                    return None

                latest_history = PromptHistories.get_latest_history_entry(
                    prompt.id, db=db
                )
                parent_id = latest_history.id if latest_history else None
                current_access_grants = self._get_access_grants(prompt.id, db=db)

                # Check if content changed to decide on history creation
                content_changed = (
                    prompt.name != form_data.name
                    or prompt.content != form_data.content
                    or form_data.access_grants is not None
                )

                # Update prompt fields
                prompt.name = form_data.name
                prompt.content = form_data.content
                prompt.data = form_data.data or prompt.data
                prompt.meta = form_data.meta or prompt.meta
                prompt.updated_at = int(time.time())
                if form_data.access_grants is not None:
                    AccessGrants.set_access_grants(
                        "prompt", prompt.id, form_data.access_grants, db=db
                    )
                    current_access_grants = self._get_access_grants(prompt.id, db=db)

                db.commit()

                # Create history entry only if content changed
                if content_changed:
                    snapshot = {
                        "name": form_data.name,
                        "content": form_data.content,
                        "command": command,
                        "data": form_data.data or {},
                        "meta": form_data.meta or {},
                        "access_grants": [
                            grant.model_dump() for grant in current_access_grants
                        ],
                    }

                    history_entry = PromptHistories.create_history_entry(
                        prompt_id=prompt.id,
                        snapshot=snapshot,
                        user_id=user_id,
                        parent_id=parent_id,
                        commit_message=form_data.commit_message,
                        db=db,
                    )

                    # Set as production if flag is True (default)
                    if form_data.is_production and history_entry:
                        prompt.version_id = history_entry.id
                        db.commit()

                return self._to_prompt_model(prompt, db=db)
        except Exception:
            return None

    def update_prompt_by_id(
        self,
        prompt_id: str,
        form_data: PromptForm,
        user_id: str,
        db: Optional[Session] = None,
    ) -> Optional[PromptModel]:
        try:
            with get_db_context(db) as db:
                prompt = db.query(Prompt).filter_by(id=prompt_id).first()
                if not prompt:
                    return None

                latest_history = PromptHistories.get_latest_history_entry(
                    prompt.id, db=db
                )
                parent_id = latest_history.id if latest_history else None
                current_access_grants = self._get_access_grants(prompt.id, db=db)

                # Check if content changed to decide on history creation
                content_changed = (
                    prompt.name != form_data.name
                    or prompt.command != form_data.command
                    or prompt.content != form_data.content
                    or form_data.access_grants is not None
                    or (form_data.tags is not None and prompt.tags != form_data.tags)
                )

                # Update prompt fields
                prompt.name = form_data.name
                prompt.command = form_data.command
                prompt.content = form_data.content
                prompt.data = form_data.data or prompt.data
                prompt.meta = form_data.meta or prompt.meta

                if form_data.tags is not None:
                    prompt.tags = form_data.tags

                if form_data.access_grants is not None:
                    AccessGrants.set_access_grants(
                        "prompt", prompt.id, form_data.access_grants, db=db
                    )
                    current_access_grants = self._get_access_grants(prompt.id, db=db)

                prompt.updated_at = int(time.time())

                db.commit()

                # Create history entry only if content changed
                if content_changed:
                    snapshot = {
                        "name": form_data.name,
                        "content": form_data.content,
                        "command": prompt.command,
                        "data": form_data.data or {},
                        "meta": form_data.meta or {},
                        "tags": prompt.tags or [],
                        "access_grants": [
                            grant.model_dump() for grant in current_access_grants
                        ],
                    }

                    history_entry = PromptHistories.create_history_entry(
                        prompt_id=prompt.id,
                        snapshot=snapshot,
                        user_id=user_id,
                        parent_id=parent_id,
                        commit_message=form_data.commit_message,
                        db=db,
                    )

                    # Set as production if flag is True (default)
                    if form_data.is_production and history_entry:
                        prompt.version_id = history_entry.id
                        db.commit()

                return self._to_prompt_model(prompt, db=db)
        except Exception:
            return None

    def update_prompt_metadata(
        self,
        prompt_id: str,
        name: str,
        command: str,
        tags: Optional[list[str]] = None,
        db: Optional[Session] = None,
    ) -> Optional[PromptModel]:
        """Update only name, command, and tags (no history created)."""
        try:
            with get_db_context(db) as db:
                prompt = db.query(Prompt).filter_by(id=prompt_id).first()
                if not prompt:
                    return None

                prompt.name = name
                prompt.command = command

                if tags is not None:
                    prompt.tags = tags

                prompt.updated_at = int(time.time())
                db.commit()

                return self._to_prompt_model(prompt, db=db)
        except Exception:
            return None

    def update_prompt_version(
        self,
        prompt_id: str,
        version_id: str,
        db: Optional[Session] = None,
    ) -> Optional[PromptModel]:
        """Set the active version of a prompt and restore content from that version's snapshot."""
        try:
            with get_db_context(db) as db:
                prompt = db.query(Prompt).filter_by(id=prompt_id).first()
                if not prompt:
                    return None

                history_entry = PromptHistories.get_history_entry_by_id(
                    version_id, db=db
                )

                if not history_entry:
                    return None

                # Restore prompt content from the snapshot
                snapshot = history_entry.snapshot
                if snapshot:
                    prompt.name = snapshot.get("name", prompt.name)
                    prompt.content = snapshot.get("content", prompt.content)
                    prompt.data = snapshot.get("data", prompt.data)
                    prompt.meta = snapshot.get("meta", prompt.meta)
                    prompt.tags = snapshot.get("tags", prompt.tags)
                    # Note: command and access_grants are not restored from snapshot

                prompt.version_id = version_id
                prompt.updated_at = int(time.time())
                db.commit()

                return self._to_prompt_model(prompt, db=db)
        except Exception:
            return None

    def delete_prompt_by_command(
        self, command: str, db: Optional[Session] = None
    ) -> bool:
        """Soft delete a prompt by setting is_active to False."""
        try:
            with get_db_context(db) as db:
                prompt = db.query(Prompt).filter_by(command=command).first()
                if prompt:
                    PromptHistories.delete_history_by_prompt_id(prompt.id, db=db)
                    AccessGrants.revoke_all_access("prompt", prompt.id, db=db)

                    prompt.is_active = False
                    prompt.updated_at = int(time.time())
                    db.commit()
                    return True
                return False
        except Exception:
            return False

    def delete_prompt_by_id(self, prompt_id: str, db: Optional[Session] = None) -> bool:
        """Soft delete a prompt by setting is_active to False."""
        try:
            with get_db_context(db) as db:
                prompt = db.query(Prompt).filter_by(id=prompt_id).first()
                if prompt:
                    PromptHistories.delete_history_by_prompt_id(prompt.id, db=db)
                    AccessGrants.revoke_all_access("prompt", prompt.id, db=db)

                    prompt.is_active = False
                    prompt.updated_at = int(time.time())
                    db.commit()
                    return True
                return False
        except Exception:
            return False

    def hard_delete_prompt_by_command(
        self, command: str, db: Optional[Session] = None
    ) -> bool:
        """Permanently delete a prompt and its history."""
        try:
            with get_db_context(db) as db:
                prompt = db.query(Prompt).filter_by(command=command).first()
                if prompt:
                    PromptHistories.delete_history_by_prompt_id(prompt.id, db=db)
                    AccessGrants.revoke_all_access("prompt", prompt.id, db=db)

                    # Delete prompt
                    db.query(Prompt).filter_by(command=command).delete()
                    db.commit()
                    return True
                return False
        except Exception:
            return False

    def get_tags(self, db: Optional[Session] = None) -> list[str]:
        try:
            with get_db_context(db) as db:
                prompts = db.query(Prompt).filter_by(is_active=True).all()
                tags = set()
                for prompt in prompts:
                    if prompt.tags:
                        for tag in prompt.tags:
                            if tag:
                                tags.add(tag)
                return sorted(list(tags))
        except Exception:
            return []


Prompts = PromptsTable()