Skip to content

Commit 3bf081d

Browse files
committed
refactor: optimize file search and update documentation for tool usage guidelines
1 parent 0a855ea commit 3bf081d

File tree

8 files changed

+590
-831
lines changed

8 files changed

+590
-831
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4-
<VersionSuffix>0</VersionSuffix>
4+
<VersionSuffix>1</VersionSuffix>
55
<TimeStamp>$([System.DateTime]::UtcNow.ToString("yyyyMMdd"))</TimeStamp>
66
<Version>0.9.$(VersionSuffix)</Version>
77
<!-- 项目信息 -->

src/KoalaWiki/Git/GitService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static (string localPath, string organization) GetRepositoryPath(string r
2222

2323

2424
// 对于 GitLab,最后一个段是仓库名,前面的都是组织/子组织
25-
repositoryName = segments[segments.Length - 1].Trim('/').Replace(".git", "");
25+
repositoryName = segments[^1].Trim('/').Replace(".git", "");
2626

2727
// 组织名包含所有中间路径,用下划线连接以避免路径冲突
2828
var orgParts = new List<string>();

src/KoalaWiki/KernelFactory.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,8 @@ public static Kernel GetKernel(string chatEndpoint,
104104
var fileFunction = new FileTool(gitPath, files);
105105
kernelBuilder.Plugins.AddFromObject(fileFunction, "file");
106106

107-
if (DocumentOptions.EnableAgentTool)
108-
{
109-
kernelBuilder.Plugins.AddFromType<AgentTool>();
110-
activity?.SetTag("plugins.agent_tool", "loaded");
111-
}
107+
kernelBuilder.Plugins.AddFromType<AgentTool>();
108+
activity?.SetTag("plugins.agent_tool", "loaded");
112109

113110
activity?.SetTag("plugins.file_function", "loaded");
114111

src/KoalaWiki/KoalaWarehouse/GenerateThinkCatalogue/GenerateThinkCatalogueService.Prompt.cs

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,17 @@ namespace KoalaWiki.KoalaWarehouse.GenerateThinkCatalogue;
55
public partial class GenerateThinkCatalogueService
66
{
77
public static async Task<string> GenerateThinkCataloguePromptAsync(ClassifyType? classifyType,
8-
string catalogue, int attemptNumber)
8+
string catalogue)
99
{
1010
var projectType = GetProjectTypeDescription(classifyType);
1111
var basePrompt = await PromptContext.Warehouse(nameof(PromptConstant.Warehouse.AnalyzeCatalogue),
1212
new KernelArguments()
1313
{
1414
["code_files"] = catalogue,
15-
["projectType"] = projectType,
16-
["language"] = Prompt.Language
15+
["projectType"] = projectType
1716
}, OpenAIOptions.AnalysisModel);
1817

19-
var toolUsage = """
20-
## Catalogue Tool Usage Guidelines
21-
22-
**PARALLEL READ OPERATIONS**
23-
- MANDATORY: Always perform PARALLEL File.Read calls — batch multiple files in a SINGLE message for maximum efficiency
24-
- CRITICAL: Read MULTIPLE files simultaneously in one operation
25-
- PROHIBITED: Sequential one-by-one file reads (inefficient and wastes context capacity)
26-
27-
**EDITING OPERATION LIMITS**
28-
- HARD LIMIT: Maximum of 3 editing operations total (Catalogue.MultiEdit only)
29-
- PRIORITY: Maximize each Catalogue.MultiEdit operation by bundling ALL related changes across multiple files
30-
- STRATEGIC PLANNING: Consolidate all modifications into minimal MultiEdit operations to stay within the limit
31-
- Use Catalogue.Write **only once** for initial creation or full rebuild (counts as initial structure creation, not part of the 3 edits)
32-
- Always verify content before further changes using Catalogue.Read (Reads do NOT count toward limit)
33-
34-
**CRITICAL MULTIEDIT BEST PRACTICES**
35-
- MAXIMIZE EFFICIENCY: Each MultiEdit should target multiple distinct sections across files
36-
- AVOID CONFLICTS: Never edit overlapping or identical content regions within the same MultiEdit operation
37-
- UNIQUE TARGETS: Ensure each edit instruction addresses a completely different section or file
38-
- BATCH STRATEGY: Group all necessary changes by proximity and relevance, but maintain clear separation between edit targets
39-
40-
**RECOMMENDED EDITING SEQUENCE**
41-
1. Initial creation → Catalogue.Write (one-time full structure creation)
42-
2. Bulk refinements → Catalogue.MultiEdit with maximum parallel changes (counts toward 3-operation limit)
43-
3. Validation → Use Catalogue.Read after each MultiEdit to verify success before next operation
44-
4. Final adjustments → Remaining MultiEdit operations for any missed changes
45-
""";
46-
47-
// Attempt-based enhancement focusing on specific quality improvements
48-
var enhancementLevel = Math.Min(attemptNumber, 3);
49-
var enhancement = enhancementLevel switch
50-
{
51-
0 => "\n\nATTEMPT 1 FOCUS: Prioritize thorough code analysis and solid JSON foundation.",
52-
1 => "\n\nATTEMPT 2 FOCUS: Emphasize structural depth and component relationships.",
53-
2 => "\n\nATTEMPT 3 FOCUS: Optimize prompt specificity and actionable guidance.",
54-
_ => "\n\nFINAL ATTEMPT: Address any remaining gaps and ensure completeness."
55-
};
56-
57-
return toolUsage + basePrompt + enhancement;
18+
return basePrompt;
5819
}
5920

6021

@@ -145,4 +106,4 @@ private static string GetProjectTypeDescription(ClassifyType? classifyType)
145106
Provide thorough analysis that serves both newcomers seeking understanding and experienced developers requiring implementation details.
146107
""";
147108
}
148-
}
109+
}

src/KoalaWiki/KoalaWarehouse/GenerateThinkCatalogue/GenerateThinkCatalogueService.cs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private enum ErrorType
4141
try
4242
{
4343
var result =
44-
await ExecuteSingleAttempt(path, catalogue, warehouse, classify, retryCount);
44+
await ExecuteSingleAttempt(path, catalogue, classify, retryCount);
4545

4646
if (result != null)
4747
{
@@ -97,11 +97,10 @@ private enum ErrorType
9797
}
9898

9999
private static async Task<DocumentResultCatalogue?> ExecuteSingleAttempt(
100-
string path, string catalogue,
101-
Warehouse warehouse, ClassifyType? classify, int attemptNumber)
100+
string path, string catalogue, ClassifyType? classify, int attemptNumber)
102101
{
103102
// 根据尝试次数调整提示词策略
104-
var enhancedPrompt = await GenerateThinkCataloguePromptAsync(classify, catalogue, attemptNumber);
103+
var enhancedPrompt = await GenerateThinkCataloguePromptAsync(classify, catalogue);
105104

106105
var history = new ChatHistory();
107106

@@ -113,10 +112,43 @@ private enum ErrorType
113112
new TextContent(
114113
$"""
115114
<system-reminder>
116-
**CRITICAL FIRST STEP: You MUST begin every research task by calling the 'think' tool to analyze the query complexity. This is mandatory and must be the very first action you take, regardless of the query type or complexity.**
115+
<catalog_tool_usage_guidelines>
116+
**PARALLEL READ OPERATIONS**
117+
- MANDATORY: Always perform PARALLEL File.Read calls — batch multiple files in a SINGLE message for maximum efficiency
118+
- CRITICAL: Read MULTIPLE files simultaneously in one operation
119+
- PROHIBITED: Sequential one-by-one file reads (inefficient and wastes context capacity)
120+
121+
**EDITING OPERATION LIMITS**
122+
- HARD LIMIT: Maximum of 3 editing operations total (catalog.MultiEdit only)
123+
- PRIORITY: Maximize each catalog.MultiEdit operation by bundling ALL related changes across multiple files
124+
- STRATEGIC PLANNING: Consolidate all modifications into minimal MultiEdit operations to stay within the limit
125+
- Use catalog.Write **only once** for initial creation or full rebuild (counts as initial structure creation, not part of the 3 edits)
126+
- Always verify content before further changes using catalog.Read (Reads do NOT count toward limit)
127+
128+
**CRITICAL MULTIEDIT BEST PRACTICES**
129+
- MAXIMIZE EFFICIENCY: Each MultiEdit should target multiple distinct sections across files
130+
- AVOID CONFLICTS: Never edit overlapping or identical content regions within the same MultiEdit operation
131+
- UNIQUE TARGETS: Ensure each edit instruction addresses a completely different section or file
132+
- BATCH STRATEGY: Group all necessary changes by proximity and relevance, but maintain clear separation between edit targets
133+
134+
**RECOMMENDED EDITING SEQUENCE**
135+
1. catalog.Write (one-time full structure creation)
136+
2. catalog.MultiEdit with maximum parallel changes (counts toward 3-operation limit)
137+
3. Use catalog.Read after each MultiEdit to verify success before next operation
138+
4. Remaining MultiEdit operations for any missed changes
139+
</catalog_tool_usage_guidelines>
140+
141+
142+
## Execution steps requirements:
143+
1. Before performing any other operations, you must first invoke the 'agent-think' tool to plan the analytical steps. This is a necessary step for completing each research task.
144+
2. Then, the code structure provided in the code_file must be utilized by calling file.Read to read the code for in-depth analysis, and then use catalog.Write to write the results of the analysis into the catalog directory.
145+
3. If necessary, some parts that need to be optimized can be edited through catalog.MultiEdit.
146+
117147
For maximum efficiency, whenever you need to perform multiple independent operations, invoke all relevant tools simultaneously rather than sequentially.
118-
Note: The repository's directory structure has been provided in <code_files>. Please utilize the provided structure directly for file navigation and reading operations, rather than relying on glob patterns or filesystem traversal methods.
148+
The repository's directory structure has been provided in <code_files>. Please utilize the provided structure directly for file navigation and reading operations, rather than relying on glob patterns or filesystem traversal methods.
149+
119150
{Prompt.Language}
151+
120152
</system-reminder>
121153
"""),
122154
new TextContent(Prompt.Language)
@@ -127,7 +159,7 @@ private enum ErrorType
127159
var catalogueTool = new CatalogueFunction();
128160
var analysisModel = KernelFactory.GetKernel(OpenAIOptions.Endpoint,
129161
OpenAIOptions.ChatApiKey, path, OpenAIOptions.AnalysisModel, false, null,
130-
builder => { builder.Plugins.AddFromObject(catalogueTool, "Catalogue"); });
162+
builder => { builder.Plugins.AddFromObject(catalogueTool, "catalog"); });
131163

132164
var chat = analysisModel.Services.GetService<IChatCompletionService>();
133165
if (chat == null)
@@ -146,7 +178,7 @@ private enum ErrorType
146178

147179
var inputTokenCount = 0;
148180
var outputTokenCount = 0;
149-
181+
150182
retry:
151183
// 流式获取响应
152184
await foreach (var item in chat.GetStreamingChatMessageContentsAsync(history, settings, analysisModel))
@@ -196,6 +228,7 @@ private static async Task RefineResponse(ChatHistory history, IChatCompletionSer
196228
• enrich each section's 'prompt' with actionable guidance (scope, code areas, outputs)
197229
- Prefer localized edits; only use Catalogue.Write for a complete rewrite if necessary.
198230
- Never print JSON in chat; use tools exclusively.
231+
- Start by editing some parts that need optimization through catalog.MultiEdit.
199232
""";
200233

201234
history.AddUserMessage(refinementPrompt);

0 commit comments

Comments
 (0)