Skip to content

Commit c4d9b96

Browse files
committed
更新代码格式
1 parent 9fc3104 commit c4d9b96

29 files changed

Lines changed: 734 additions & 660 deletions

DirectorPrompt.Agents/Orchestrator.cs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private ResolvedEmbeddingConfig ResolveEmbeddingConfig()
177177

178178
private static async Task RunStageAsync
179179
(
180-
PipelineContext context,
180+
PipelineContext context,
181181
PipelineStageKind kind,
182182
Func<Task> action,
183183
Func<string?>? detailFactory = null
@@ -208,13 +208,21 @@ private async Task<NarrationResult> RunPipelineAsync
208208
CancellationToken cancellationToken
209209
)
210210
{
211-
await RunStageAsync(context, PipelineStageKind.Retrieval,
211+
await RunStageAsync
212+
(
213+
context,
214+
PipelineStageKind.Retrieval,
212215
() => retrievalStage.ExecuteAsync(context, cancellationToken),
213-
() => $"知识长度={context.KnowledgeContext?.Length ?? 0}, 记忆长度={context.MemoryContext?.Length ?? 0}");
216+
() => $"知识长度={context.KnowledgeContext?.Length ?? 0}, 记忆长度={context.MemoryContext?.Length ?? 0}"
217+
);
214218

215-
await RunStageAsync(context, PipelineStageKind.Generation,
219+
await RunStageAsync
220+
(
221+
context,
222+
PipelineStageKind.Generation,
216223
() => generationStage.ExecuteAsync(context, cancellationToken),
217-
() => $"叙事长度={context.NarrativeOutput?.Length ?? 0}");
224+
() => $"叙事长度={context.NarrativeOutput?.Length ?? 0}"
225+
);
218226

219227
var now = DateTime.UtcNow;
220228

@@ -227,18 +235,18 @@ await RunStageAsync(context, PipelineStageKind.Generation,
227235
RoundID = context.RoundID,
228236
SceneID = context.CurrentSceneID,
229237
Type = EventType.DirectorInput,
230-
Data = JsonSerializer.Serialize
231-
(
232-
context.DirectiveBatch.Directives.Select
233-
(d => new
234-
{
235-
type = d.Type.ToString(),
236-
content = d.Content,
237-
order = d.Order,
238-
isSystem = d.IsSystem
239-
}
240-
)
241-
),
238+
Data = JsonSerializer.Serialize
239+
(
240+
context.DirectiveBatch.Directives.Select
241+
(d => new
242+
{
243+
type = d.Type.ToString(),
244+
content = d.Content,
245+
order = d.Order,
246+
isSystem = d.IsSystem
247+
}
248+
)
249+
),
242250
CreatedAt = now
243251
},
244252
new()
@@ -272,10 +280,17 @@ await RunStageAsync(context, PipelineStageKind.Generation,
272280

273281
await eventRepository.AppendBatchAsync(events, cancellationToken);
274282

275-
await RunStageAsync(context, PipelineStageKind.PostProcessing,
276-
() => postProcessingStage.ExecuteAsync(context, cancellationToken));
283+
await RunStageAsync
284+
(
285+
context,
286+
PipelineStageKind.PostProcessing,
287+
() => postProcessingStage.ExecuteAsync(context, cancellationToken)
288+
);
277289

278-
await RunStageAsync(context, PipelineStageKind.SystemState,
290+
await RunStageAsync
291+
(
292+
context,
293+
PipelineStageKind.SystemState,
279294
() => systemStateTransformer.ExecuteAsync
280295
(
281296
context.DirectiveBatch.ProjectID,
@@ -284,7 +299,8 @@ await RunStageAsync(context, PipelineStageKind.SystemState,
284299
context.RoundID,
285300
SystemTrigger.RoundEnd,
286301
cancellationToken
287-
));
302+
)
303+
);
288304

289305
await directiveRepository.DecrementTTLAsync(context.SessionID, cancellationToken);
290306

@@ -421,5 +437,4 @@ private static DirectiveBatch InjectSystemDirectives
421437

422438
return batch with { Directives = allDirectives };
423439
}
424-
425440
}

DirectorPrompt.Agents/Pipeline/PostProcessingStage.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ private async Task<string> BuildAgentContextAsync(ToolExecutionContext context,
109109

110110
foreach (var attr in attributes)
111111
{
112-
var value = valueMap.TryGetValue(attr.ID, out var sv) ? sv : null;
112+
var value = valueMap.TryGetValue(attr.ID, out var sv) ?
113+
sv :
114+
null;
113115
var type = FormatType(attr);
114116
var constraint = FormatConstraint(attr);
115117
var rules = FormatRules(attr);
@@ -178,10 +180,10 @@ private async Task<string> BuildAgentContextAsync(ToolExecutionContext context,
178180
var attrLookup = categoryAttrs.ToDictionary(a => a.ID);
179181
var characterIDs = characters.Select(c => c.ID).ToList();
180182
var allStateValues = await characterRepository.GetCharacterStateValuesBatchAsync(characterIDs, cancellationToken);
181-
var valuesByChar = allStateValues
182-
.Where(v => categoryAttrIDs.Contains(v.AttributeID))
183-
.GroupBy(v => v.CharacterID)
184-
.ToDictionary(g => g.Key);
183+
var valuesByChar = allStateValues
184+
.Where(v => categoryAttrIDs.Contains(v.AttributeID))
185+
.GroupBy(v => v.CharacterID)
186+
.ToDictionary(g => g.Key);
185187

186188
sb.AppendLine("## 人物当前状态值");
187189

DirectorPrompt.Agents/Pipeline/RetrievalStage.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public async Task ExecuteAsync(PipelineContext context, CancellationToken cancel
2222
{
2323
Log.Information("RetrievalStage 开始: 对话={SessionID}, 轮次={RoundID}", context.SessionID, context.RoundID);
2424

25-
var toolContext = context.ToolContext;
26-
var indexingTask = embeddingIndexService.SynchronizeProjectAsync
27-
(
28-
toolContext.ProjectID,
29-
toolContext.EmbeddingConfig,
30-
cancellationToken
31-
);
25+
var toolContext = context.ToolContext;
26+
var indexingTask = embeddingIndexService.SynchronizeProjectAsync
27+
(
28+
toolContext.ProjectID,
29+
toolContext.EmbeddingConfig,
30+
cancellationToken
31+
);
3232
var queryTask = BuildRetrievalQueryAsync(context, cancellationToken);
3333
var injectionTask = BuildSystemInjectionAsync(toolContext, cancellationToken);
3434

@@ -180,7 +180,9 @@ private async Task<string> BuildSystemInjectionAsync(ToolExecutionContext contex
180180

181181
foreach (var attr in attributes)
182182
{
183-
var value = valueMap.TryGetValue(attr.ID, out var sv) ? sv : null;
183+
var value = valueMap.TryGetValue(attr.ID, out var sv) ?
184+
sv :
185+
null;
184186
sb.AppendLine($"- {attr.DisplayName} ({attr.Name}): {value?.Value ?? "未设置"}");
185187
}
186188

@@ -238,13 +240,13 @@ CancellationToken cancellationToken
238240
if (attributes.Count == 0)
239241
return;
240242

241-
var attrLookup = attributes.ToDictionary(a => a.ID);
242-
var characterIDs = characters.Select(c => c.ID).ToList();
243-
var allStateValues = await characterRepository.GetCharacterStateValuesBatchAsync(characterIDs, cancellationToken);
244-
var valuesByChar = allStateValues
245-
.Where(v => attrLookup.ContainsKey(v.AttributeID))
246-
.GroupBy(v => v.CharacterID)
247-
.ToDictionary(g => g.Key);
243+
var attrLookup = attributes.ToDictionary(a => a.ID);
244+
var characterIDs = characters.Select(c => c.ID).ToList();
245+
var allStateValues = await characterRepository.GetCharacterStateValuesBatchAsync(characterIDs, cancellationToken);
246+
var valuesByChar = allStateValues
247+
.Where(v => attrLookup.ContainsKey(v.AttributeID))
248+
.GroupBy(v => v.CharacterID)
249+
.ToDictionary(g => g.Key);
248250

249251
sb.AppendLine("## 在场人物状态");
250252

@@ -308,5 +310,4 @@ CancellationToken cancellationToken
308310

309311
sb.AppendLine();
310312
}
311-
312313
}

DirectorPrompt.Agents/Retrieval/EmbeddingIndexService.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,13 @@ public async Task IndexKnowledgeAsync
6262
var vectors = new List<(string source, byte[] embedding)>();
6363

6464
for (var i = 0; i < texts.Count; i++)
65-
{
6665
vectors.Add
6766
(
6867
(
6968
texts[i].source,
7069
EmbeddingConversions.FloatsToBytes(embeddings[offset + i])
7170
)
7271
);
73-
}
7472

7573
var hash = ComputeHash(texts, embeddingConfig.Fingerprint, KNOWLEDGE_INDEX_VERSION);
7674
await knowledgeRepository.SaveEmbeddingsAsync(entry.ProjectID, entry.ID, vectors, hash, cancellationToken);
@@ -105,15 +103,13 @@ public async Task IndexMemoriesAsync
105103
var vectors = new List<(string source, byte[] embedding)>();
106104

107105
for (var i = 0; i < texts.Count; i++)
108-
{
109106
vectors.Add
110107
(
111108
(
112109
texts[i].source,
113110
EmbeddingConversions.FloatsToBytes(embeddings[offset + i])
114111
)
115112
);
116-
}
117113

118114
var hash = ComputeHash(texts, embeddingConfig.Fingerprint, MEMORY_INDEX_VERSION);
119115
await memoryRepository.SaveEmbeddingsAsync(entry.ProjectID, entry.ID, vectors, hash, cancellationToken);
@@ -160,8 +156,8 @@ public async Task IndexMemoriesAsync
160156
private static string ComputeHash
161157
(
162158
IReadOnlyList<(string source, string text)> texts,
163-
string fingerprint,
164-
string indexVersion
159+
string fingerprint,
160+
string indexVersion
165161
)
166162
{
167163
var combined = string.Join('\0', texts.Select(t => t.text));

DirectorPrompt.Agents/Retrieval/KnowledgeRetrievalService.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public async Task<IReadOnlyList<KnowledgeRetrievalResult>> SearchAsync
3232
var queryEmbedding = await embeddingService.GenerateEmbeddingAsync(query, cancellationToken);
3333
var queryBytes = EmbeddingConversions.FloatsToBytes(queryEmbedding);
3434
var candidateIDs = entries.Select(e => e.ID).ToList();
35-
var searchResults = await knowledgeRepository.SearchByVectorAsync
36-
(
37-
context.ProjectID,
38-
queryBytes,
39-
config.SemanticTopK,
40-
candidateIDs,
41-
cancellationToken
42-
);
35+
var searchResults = await knowledgeRepository.SearchByVectorAsync
36+
(
37+
context.ProjectID,
38+
queryBytes,
39+
config.SemanticTopK,
40+
candidateIDs,
41+
cancellationToken
42+
);
4343
var entryMap = entries.ToDictionary(e => e.ID);
4444
var usedTokens = 0;
4545

DirectorPrompt.Agents/Retrieval/MemoryRetrievalService.cs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,42 +36,44 @@ public async Task<IReadOnlyList<MemoryRetrievalResult>> SearchAsync
3636
var queryEmbedding = await embeddingService.GenerateEmbeddingAsync(query, cancellationToken);
3737
var queryBytes = EmbeddingConversions.FloatsToBytes(queryEmbedding);
3838
var candidateIDs = memories.Select(m => m.ID).ToList();
39-
var searchResults = await memoryRepository.SearchByVectorAsync
40-
(
41-
context.ProjectID,
42-
queryBytes,
43-
memories.Count,
44-
candidateIDs,
45-
cancellationToken
46-
);
39+
var searchResults = await memoryRepository.SearchByVectorAsync
40+
(
41+
context.ProjectID,
42+
queryBytes,
43+
memories.Count,
44+
candidateIDs,
45+
cancellationToken
46+
);
4747
var memoryMap = memories.ToDictionary(m => m.ID);
4848
var lambda = config.TimeDecayLambda;
49-
var ranked = searchResults
50-
.Where(r => memoryMap.ContainsKey(r.EntryID))
51-
.Select
52-
(r =>
53-
{
54-
var memory = memoryMap[r.EntryID];
55-
var semanticSimilarity = 1f - r.Distance;
56-
var sceneDistance = (context.TimelinePosition - memory.TimelinePos) / (double)TimelineCalculator.GAP;
57-
var recencyWeight = lambda > 0 ? Math.Exp(-lambda * sceneDistance) : 1d;
58-
var finalScore = semanticSimilarity * recencyWeight;
49+
var ranked = searchResults
50+
.Where(r => memoryMap.ContainsKey(r.EntryID))
51+
.Select
52+
(r =>
53+
{
54+
var memory = memoryMap[r.EntryID];
55+
var semanticSimilarity = 1f - r.Distance;
56+
var sceneDistance = (context.TimelinePosition - memory.TimelinePos) / (double)TimelineCalculator.GAP;
57+
var recencyWeight = lambda > 0 ?
58+
Math.Exp(-lambda * sceneDistance) :
59+
1d;
60+
var finalScore = semanticSimilarity * recencyWeight;
5961

60-
return new MemoryRetrievalResult
61-
(
62-
memory.ID,
63-
memory.Content,
64-
memory.Tags,
65-
memory.SceneID,
66-
r.Source,
67-
semanticSimilarity,
68-
recencyWeight,
69-
finalScore
70-
);
71-
}
72-
)
73-
.Where(r => config.MinRelevance <= 0 || r.FinalScore >= config.MinRelevance)
74-
.OrderByDescending(r => r.FinalScore);
62+
return new MemoryRetrievalResult
63+
(
64+
memory.ID,
65+
memory.Content,
66+
memory.Tags,
67+
memory.SceneID,
68+
r.Source,
69+
semanticSimilarity,
70+
recencyWeight,
71+
finalScore
72+
);
73+
}
74+
)
75+
.Where(r => config.MinRelevance <= 0 || r.FinalScore >= config.MinRelevance)
76+
.OrderByDescending(r => r.FinalScore);
7577
var usedTokens = 0;
7678
var result = ranked
7779
.Where

DirectorPrompt.Agents/SystemStateTransformer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ CancellationToken cancellationToken
122122

123123
var characterIDs = characters.Select(c => c.ID).ToList();
124124
var allStateValues = await characterRepository.GetCharacterStateValuesBatchAsync(characterIDs, cancellationToken);
125-
var valuesByChar = allStateValues.GroupBy(v => v.CharacterID)
126-
.ToDictionary(g => g.Key);
125+
var valuesByChar = allStateValues.GroupBy(v => v.CharacterID)
126+
.ToDictionary(g => g.Key);
127127

128128
foreach (var character in characters)
129129
{
@@ -365,8 +365,8 @@ CancellationToken cancellationToken
365365
foreach (var attr in globalAttrs)
366366
{
367367
result[attr.Name] = valueMap.TryGetValue(attr.ID, out var sv) ?
368-
sv.Value :
369-
string.Empty;
368+
sv.Value :
369+
string.Empty;
370370
}
371371

372372
return result;

DirectorPrompt.Agents/ToolExecutionContext.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ namespace DirectorPrompt.Agents;
44

55
public record ToolExecutionContext
66
(
7-
long ProjectID,
8-
long SessionID,
9-
long? SceneID,
10-
long TimelinePosition,
11-
long RoundID,
12-
ResolvedEmbeddingConfig EmbeddingConfig,
7+
long ProjectID,
8+
long SessionID,
9+
long? SceneID,
10+
long TimelinePosition,
11+
long RoundID,
12+
ResolvedEmbeddingConfig EmbeddingConfig,
1313
KnowledgeRetrievalConfig KnowledgeConfig,
1414
MemoryConfig MemoryConfig,
15-
IReadOnlyList<long>? PhaseActivatedEntryIDs = null
15+
IReadOnlyList<long>? PhaseActivatedEntryIDs = null
1616
);

0 commit comments

Comments
 (0)