| 354 | } |
| 355 | |
| 356 | func (c *chatConfigCache) UserPrompt(ctx context.Context, userID uuid.UUID) (string, error) { |
| 357 | if prompt, ok := c.cachedUserPrompt(userID); ok { |
| 358 | return prompt, nil |
| 359 | } |
| 360 | |
| 361 | epoch := c.currentUserPromptEpoch() |
| 362 | prompt, err := singleflightDoChan(ctx, &c.userPromptFetches, fmt.Sprintf("%d:%s", epoch, userID), func() (string, error) { |
| 363 | if cached, ok := c.cachedUserPrompt(userID); ok { |
| 364 | return cached, nil |
| 365 | } |
| 366 | |
| 367 | fetched, err := c.db.GetUserChatCustomPrompt(c.ctx, userID) |
| 368 | if err != nil { |
| 369 | if errors.Is(err, sql.ErrNoRows) { |
| 370 | c.storeUserPrompt(epoch, userID, "") |
| 371 | return "", nil |
| 372 | } |
| 373 | return "", err |
| 374 | } |
| 375 | c.storeUserPrompt(epoch, userID, fetched) |
| 376 | return fetched, nil |
| 377 | }) |
| 378 | if err != nil { |
| 379 | return "", err |
| 380 | } |
| 381 | |
| 382 | return prompt, nil |
| 383 | } |
| 384 | |
| 385 | func (c *chatConfigCache) cachedUserPrompt(userID uuid.UUID) (string, bool) { |
| 386 | prompt, _, ok := c.userPrompts.Get(userID) |