|
| 1 | +package asker |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + htmltomarkdown "github.com/JohannesKaufmann/html-to-markdown/v2" |
| 9 | + "github.com/Southclaws/fault" |
| 10 | + "github.com/Southclaws/fault/fctx" |
| 11 | + "go.uber.org/zap" |
| 12 | + |
| 13 | + "github.com/Southclaws/storyden/app/resources/account" |
| 14 | + "github.com/Southclaws/storyden/app/resources/datagraph" |
| 15 | + "github.com/Southclaws/storyden/app/resources/question" |
| 16 | + "github.com/Southclaws/storyden/app/services/authentication/session" |
| 17 | + "github.com/Southclaws/storyden/app/services/semdex" |
| 18 | +) |
| 19 | + |
| 20 | +type cachedAsker struct { |
| 21 | + logger *zap.Logger |
| 22 | + asker semdex.Asker |
| 23 | + questions *question.Repository |
| 24 | +} |
| 25 | + |
| 26 | +func newCachedAsker( |
| 27 | + logger *zap.Logger, |
| 28 | + asker semdex.Asker, |
| 29 | + questions *question.Repository, |
| 30 | +) (semdex.Asker, error) { |
| 31 | + return &cachedAsker{ |
| 32 | + logger: logger, |
| 33 | + asker: asker, |
| 34 | + questions: questions, |
| 35 | + }, nil |
| 36 | +} |
| 37 | + |
| 38 | +func (a *cachedAsker) Ask(ctx context.Context, q string) (func(yield func(string, error) bool), error) { |
| 39 | + cached, err := a.questions.GetByQuerySlug(ctx, q) |
| 40 | + if err == nil { |
| 41 | + return a.cachedResult(ctx, cached) |
| 42 | + } |
| 43 | + |
| 44 | + return a.livePrompt(ctx, q) |
| 45 | +} |
| 46 | + |
| 47 | +func (a *cachedAsker) cachedResult(ctx context.Context, q *question.Question) (func(yield func(string, error) bool), error) { |
| 48 | + md, err := htmltomarkdown.ConvertNode(q.Result.HTMLTree()) |
| 49 | + if err != nil { |
| 50 | + return nil, fault.Wrap(err, fctx.With(ctx)) |
| 51 | + } |
| 52 | + |
| 53 | + chunks := strings.SplitAfter(string(md), " ") |
| 54 | + |
| 55 | + return func(yield func(string, error) bool) { |
| 56 | + for _, ch := range chunks { |
| 57 | + select { |
| 58 | + case <-ctx.Done(): |
| 59 | + return |
| 60 | + |
| 61 | + default: |
| 62 | + if !yield(ch, nil) { |
| 63 | + return |
| 64 | + } |
| 65 | + } |
| 66 | + time.Sleep(time.Millisecond * 10) |
| 67 | + } |
| 68 | + }, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (a *cachedAsker) livePrompt(ctx context.Context, q string) (func(yield func(string, error) bool), error) { |
| 72 | + accountID, err := session.GetAccountID(ctx) |
| 73 | + if err != nil { |
| 74 | + return nil, fault.Wrap(err, fctx.With(ctx)) |
| 75 | + } |
| 76 | + |
| 77 | + iter, err := a.asker.Ask(ctx, q) |
| 78 | + if err != nil { |
| 79 | + return nil, fault.Wrap(err, fctx.With(ctx)) |
| 80 | + } |
| 81 | + |
| 82 | + return func(yield func(string, error) bool) { |
| 83 | + acc := []string{} |
| 84 | + |
| 85 | + defer func() { |
| 86 | + err := a.cacheResult(ctx, accountID, q, acc) |
| 87 | + if err != nil { |
| 88 | + a.logger.Error("failed to cache result", zap.Error(err)) |
| 89 | + } |
| 90 | + }() |
| 91 | + |
| 92 | + for chunk, err := range iter { |
| 93 | + if err != nil { |
| 94 | + yield("", err) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + acc = append(acc, chunk) |
| 99 | + if !yield(chunk, nil) { |
| 100 | + return |
| 101 | + } |
| 102 | + } |
| 103 | + }, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (a *cachedAsker) cacheResult(ctx context.Context, accountID account.AccountID, q string, chunks []string) error { |
| 107 | + result := strings.Join(chunks, "") |
| 108 | + |
| 109 | + acc, err := datagraph.NewRichTextFromMarkdown(result) |
| 110 | + if err != nil { |
| 111 | + return fault.Wrap(err, fctx.With(ctx)) |
| 112 | + } |
| 113 | + |
| 114 | + _, err = a.questions.Store(ctx, accountID, q, acc) |
| 115 | + if err != nil { |
| 116 | + return fault.Wrap(err, fctx.With(ctx)) |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
0 commit comments