Skip to content

Commit 39fdc1a

Browse files
committed
enhance: trim blocking code markdown identifier in AI response
Signed-off-by: leo <longshuang@msn.cn>
1 parent 8e17ff0 commit 39fdc1a

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/AI/Agent.cs‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,28 @@ public async Task GenerateCommitMessageAsync(string repo, string changeList, Act
4646
onUpdate.Invoke(string.Empty);
4747
onUpdate.Invoke("# Assistant");
4848
if (completion.Content.Count > 0)
49-
onUpdate.Invoke(completion.Content[0].Text);
49+
{
50+
var text = completion.Content[0].Text.ReplaceLineEndings("\n").Trim();
51+
var start = 0;
52+
var len = text.Length;
53+
if (text.StartsWith("```\n", StringComparison.Ordinal))
54+
{
55+
start += 4;
56+
len -= 4;
57+
}
58+
59+
if (text.EndsWith("\n```", StringComparison.Ordinal))
60+
len -= 4;
61+
62+
if (len > 0)
63+
onUpdate.Invoke(text.Substring(start, len));
64+
else
65+
onUpdate.Invoke("[No content was generated.]");
66+
}
5067
else
68+
{
5169
onUpdate.Invoke("[No content was generated.]");
70+
}
5271

5372
onUpdate.Invoke(string.Empty);
5473
onUpdate.Invoke("# Token Usage");

0 commit comments

Comments
 (0)