Repository navigation
Expand file tree
/
Copy pathBlogSession.cs
More file actions
84 lines (74 loc) · 2.3 KB
/
Copy pathBlogSession.cs
File metadata and controls
84 lines (74 loc) · 2.3 KB
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
using Newtonsoft.Json;
using System.Text;
namespace BlogWriter;
/// <summary>Persisted state for one user's blog-writing conversation.</summary>
public sealed class BlogSession
{
[JsonProperty("id")]
public required string Id { get; init; }
public string OwnerId { get; init; } = "";
[JsonIgnore]
public string? ETag { get; set; }
public DateTimeOffset CreatedAt { get; init; }
public DateTimeOffset UpdatedAt { get; set; }
public required ResearchState State { get; set; }
}
public sealed record BlogSessionSummary(
string Id,
string MainTask,
DateTimeOffset CreatedAt,
DateTimeOffset UpdatedAt)
{
public int MinWords { get; init; } = ResearchState.DefaultMinWords;
public int MaxWords { get; init; } = ResearchState.DefaultMaxWords;
public string DraftPreview { get; init; } = "";
public bool IsDraftTruncated { get; init; }
public static BlogSessionSummary Create(
string id,
string mainTask,
DateTimeOffset createdAt,
DateTimeOffset updatedAt,
int minWords,
int maxWords,
string? draft)
{
WordRange range = minWords > 0 && maxWords >= minWords
? new WordRange(minWords, maxWords)
: WordRange.Default;
var preview = new StringBuilder();
int position = 0;
int wordCount = 0;
bool truncated = false;
while (draft is not null && position < draft.Length)
{
if (char.IsWhiteSpace(draft[position]))
{
position++;
continue;
}
if (wordCount == 50)
{
truncated = true;
break;
}
int start = position;
while (position < draft.Length && !char.IsWhiteSpace(draft[position]))
{
position++;
}
if (wordCount > 0)
{
preview.Append(' ');
}
preview.Append(draft.AsSpan(start, position - start));
wordCount++;
}
return new BlogSessionSummary(id, mainTask, createdAt, updatedAt)
{
MinWords = range.Min,
MaxWords = range.Max,
DraftPreview = preview.ToString(),
IsDraftTruncated = truncated,
};
}
}