-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chat): Support extracting code blocks (#27)
- Loading branch information
Showing
17 changed files
with
571 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Ciandt.FlowTools.FlowPair.Chats.Models; | ||
|
||
public sealed record CodeSnippet( | ||
string Content, | ||
string? Language = null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Immutable; | ||
using System.Text.RegularExpressions; | ||
using Ciandt.FlowTools.FlowPair.Chats.Models; | ||
using Ciandt.FlowTools.FlowPair.Common; | ||
using FxKit.Parsers; | ||
|
||
namespace Ciandt.FlowTools.FlowPair.Chats.Services; | ||
|
||
public static partial class MarkdownCodeExtractor | ||
{ | ||
public static Result<ImmutableList<CodeSnippet>, string> TryExtract(string content) | ||
{ | ||
return | ||
from value in StringParser.NonNullOrWhiteSpace(content) | ||
.OkOr("Code not found on empty content") | ||
select CodeBlockRegex().Matches(content) | ||
.Select(m => new CodeSnippet(m.Groups[2].Value, m.Groups[1].Value)) | ||
.ToImmutableList(); | ||
} | ||
|
||
public static Result<CodeSnippet, string> TryExtractSingle(string content) | ||
{ | ||
return | ||
from value in StringParser.NonNullOrWhiteSpace(content) | ||
.OkOr("Code not found on empty content") | ||
from code in CodeBlockRegex().Matches(content) | ||
.Select(m => new CodeSnippet(m.Groups[2].Value, m.Groups[1].Value)) | ||
.TrySingle() | ||
.MapErr( | ||
p => p.Match( | ||
Empty: "No code block found", | ||
MoreThanOneElement: "More than one code block found")) | ||
select code; | ||
} | ||
|
||
[GeneratedRegex(@"```(\w*)\s*\n([\s\S]*?)\r?\n\s*```", RegexOptions.Singleline)] | ||
private static partial Regex CodeBlockRegex(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using FxKit.CompilerServices; | ||
|
||
namespace Ciandt.FlowTools.FlowPair.Common; | ||
|
||
[EnumMatch] | ||
public enum SingleElementProblem | ||
{ | ||
Empty, | ||
MoreThanOneElement, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.