-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnowledgeSyncService.cs
More file actions
344 lines (307 loc) · 13.2 KB
/
KnowledgeSyncService.cs
File metadata and controls
344 lines (307 loc) · 13.2 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text.Json;
namespace TimeTask
{
public sealed class KnowledgeSyncOptions
{
public bool Enabled { get; set; } = false;
public int SyncIntervalMinutes { get; set; } = 60;
public string ObsidianVaultPath { get; set; } = string.Empty;
public bool ObsidianVaultAutoDiscovered { get; set; } = false;
public bool ObsidianIncludeSubfolders { get; set; } = true;
public int ObsidianMaxFilesPerSync { get; set; } = 200;
public bool RealtimeWatchEnabled { get; set; } = true;
public int SyncDebounceSeconds { get; set; } = 8;
public bool AutoImportEnabled { get; set; } = true;
public double AutoImportMinConfidence { get; set; } = 0.9;
public int AutoImportMaxPerRun { get; set; } = 6;
public bool SmartNotifyEnabled { get; set; } = true;
public string RulesFilePath { get; set; } = string.Empty;
public string PromptTemplatePath { get; set; } = string.Empty;
public string StateFilePath { get; set; } = string.Empty;
}
public sealed class KnowledgeSyncService
{
private readonly KnowledgeSyncOptions _options;
private readonly ObsidianKnowledgeConnector _connector;
private readonly KnowledgeTaskExtractor _extractor;
private readonly string _promptTemplate;
private KnowledgeSyncService(
KnowledgeSyncOptions options,
ObsidianKnowledgeConnector connector,
KnowledgeTaskExtractor extractor,
string promptTemplate)
{
_options = options;
_connector = connector;
_extractor = extractor;
_promptTemplate = promptTemplate ?? string.Empty;
}
public KnowledgeSyncOptions Options => _options;
public bool IsEnabled => _options != null && _options.Enabled;
public bool IsConnectorReady => _connector != null && _connector.IsConfigured();
public static KnowledgeSyncService CreateFromAppSettings(string appRootPath)
{
string appDataPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"TimeTask");
Directory.CreateDirectory(appDataPath);
string ReadRaw(string key, string defaultValue = "")
{
try
{
string value = ConfigurationManager.AppSettings[key];
return string.IsNullOrWhiteSpace(value) ? defaultValue : value.Trim();
}
catch
{
return defaultValue;
}
}
bool ReadBool(string key, bool defaultValue)
{
string raw = ReadRaw(key, defaultValue.ToString());
return bool.TryParse(raw, out bool value) ? value : defaultValue;
}
int ReadInt(string key, int defaultValue, int min, int max)
{
string raw = ReadRaw(key, defaultValue.ToString());
if (!int.TryParse(raw, out int value))
{
return defaultValue;
}
return Math.Max(min, Math.Min(max, value));
}
double ReadDouble(string key, double defaultValue, double min, double max)
{
string raw = ReadRaw(key, defaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture));
if (!double.TryParse(raw, out double value) &&
!double.TryParse(raw, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out value))
{
return defaultValue;
}
return Math.Max(min, Math.Min(max, value));
}
string Resolve(string configuredPath, string fallbackRelativePath)
{
string path = configuredPath;
if (string.IsNullOrWhiteSpace(path))
{
path = fallbackRelativePath;
}
if (Path.IsPathRooted(path))
{
return path;
}
return Path.Combine(appRootPath, path);
}
var options = new KnowledgeSyncOptions
{
Enabled = ReadBool("KnowledgeSyncEnabled", false),
SyncIntervalMinutes = ReadInt("KnowledgeSyncIntervalMinutes", 60, 5, 720),
ObsidianVaultPath = ReadRaw("ObsidianVaultPath", string.Empty),
ObsidianIncludeSubfolders = ReadBool("ObsidianIncludeSubfolders", true),
ObsidianMaxFilesPerSync = ReadInt("ObsidianMaxFilesPerSync", 200, 10, 5000),
RealtimeWatchEnabled = ReadBool("KnowledgeRealtimeWatchEnabled", true),
SyncDebounceSeconds = ReadInt("KnowledgeSyncDebounceSeconds", 8, 2, 120),
AutoImportEnabled = ReadBool("KnowledgeAutoImportEnabled", true),
AutoImportMinConfidence = ReadDouble("KnowledgeAutoImportMinConfidence", 0.9, 0.5, 1.0),
AutoImportMaxPerRun = ReadInt("KnowledgeAutoImportMaxPerRun", 6, 1, 50),
SmartNotifyEnabled = ReadBool("KnowledgeSmartNotifyEnabled", true),
RulesFilePath = Resolve(ReadRaw("KnowledgeRulesPath"), @"configs\knowledge_rules.yaml"),
PromptTemplatePath = Resolve(ReadRaw("KnowledgePromptTemplatePath"), @"configs\task_extract_prompt.txt"),
StateFilePath = Resolve(ReadRaw("KnowledgeSyncStatePath"), Path.Combine(appDataPath, "knowledge_sync_state.json"))
};
if (string.IsNullOrWhiteSpace(options.ObsidianVaultPath))
{
options.ObsidianVaultPath = TryDiscoverVaultPath();
options.ObsidianVaultAutoDiscovered = !string.IsNullOrWhiteSpace(options.ObsidianVaultPath);
}
var rules = KnowledgeRuleSet.Load(options.RulesFilePath);
var extractor = new KnowledgeTaskExtractor(rules);
var connector = new ObsidianKnowledgeConnector(
options.ObsidianVaultPath,
options.ObsidianIncludeSubfolders,
options.ObsidianMaxFilesPerSync);
string promptTemplate = string.Empty;
if (File.Exists(options.PromptTemplatePath))
{
promptTemplate = File.ReadAllText(options.PromptTemplatePath);
}
return new KnowledgeSyncService(options, connector, extractor, promptTemplate);
}
public KnowledgeSyncResult RunOnce(TaskDraftManager draftManager)
{
var result = new KnowledgeSyncResult();
if (!IsEnabled)
{
result.Errors.Add("Knowledge sync is disabled.");
return result;
}
if (draftManager == null)
{
result.Errors.Add("TaskDraftManager is not available.");
return result;
}
if (!IsConnectorReady)
{
result.Errors.Add("Obsidian vault path is not configured or does not exist.");
return result;
}
var state = LoadState();
var changedNotes = _connector.GetChangedNotes(state.NoteSignatures, out Dictionary<string, string> latestSignatures);
result.NotesScanned = changedNotes.Count;
var importedKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var note in changedNotes)
{
try
{
var candidates = _extractor.Extract(note);
result.TaskCandidates += candidates.Count;
foreach (var candidate in candidates)
{
string dedupeKey = $"{candidate.SourcePath}|{candidate.Title}";
if (!importedKeys.Add(dedupeKey))
{
result.DuplicatesSkipped++;
continue;
}
var draft = BuildDraft(candidate);
draftManager.AddDraft(draft);
result.NewDrafts.Add(draft);
result.DraftsAdded++;
}
}
catch (Exception ex)
{
result.FailedNotes++;
result.Errors.Add($"{note.RelativePath}: {ex.Message}");
}
}
state.LastRunUtc = DateTime.UtcNow;
state.NoteSignatures = latestSignatures ?? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
SaveState(state);
return result;
}
private static TaskDraft BuildDraft(ExtractedTaskCandidate candidate)
{
string reminderHint = candidate.DueAt.HasValue ? $"截止 {candidate.DueAt.Value:yyyy-MM-dd}" : null;
return new TaskDraft
{
RawText = $"{candidate.Title} [{candidate.SourcePath}]",
CleanedText = candidate.Title,
SourceNotePath = candidate.SourcePath,
Confidence = candidate.Confidence,
ReminderTime = candidate.DueAt,
ReminderHintText = reminderHint,
EstimatedQuadrant = MapQuadrant(candidate.Priority),
Importance = candidate.Priority == "low" ? "Low" : "High",
Urgency = candidate.Priority == "high" ? "High" : "Low",
Source = "obsidian",
IsProcessed = false,
LastDetected = DateTime.Now,
CreatedAt = DateTime.Now,
DetectionCount = 1
};
}
private static string TryDiscoverVaultPath()
{
try
{
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string configPath = Path.Combine(appData, "obsidian", "obsidian.json");
if (!File.Exists(configPath))
{
return string.Empty;
}
string json = File.ReadAllText(configPath);
var openMatch = Regex.Match(
json,
"\"path\"\\s*:\\s*\"(?<path>[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"\\s*,\\s*\"ts\"\\s*:\\s*\\d+\\s*,\\s*\"open\"\\s*:\\s*true",
RegexOptions.IgnoreCase);
if (openMatch.Success)
{
string decoded = DecodeJsonString(openMatch.Groups["path"].Value);
if (Directory.Exists(decoded))
{
return decoded;
}
}
var firstPathMatch = Regex.Match(json, "\"path\"\\s*:\\s*\"(?<path>[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"", RegexOptions.IgnoreCase);
if (firstPathMatch.Success)
{
string decoded = DecodeJsonString(firstPathMatch.Groups["path"].Value);
if (Directory.Exists(decoded))
{
return decoded;
}
}
}
catch
{
}
return string.Empty;
}
private static string DecodeJsonString(string value)
{
if (string.IsNullOrEmpty(value))
{
return value;
}
return value
.Replace("\\\\", "\\")
.Replace("\\/", "/")
.Replace("\\\"", "\"");
}
private static string MapQuadrant(string priority)
{
if (string.Equals(priority, "high", StringComparison.OrdinalIgnoreCase))
{
return "important_urgent";
}
if (string.Equals(priority, "low", StringComparison.OrdinalIgnoreCase))
{
return "not_important_not_urgent";
}
return "important_not_urgent";
}
private KnowledgeSyncState LoadState()
{
try
{
if (!File.Exists(_options.StateFilePath))
{
return new KnowledgeSyncState();
}
string json = File.ReadAllText(_options.StateFilePath);
var state = JsonSerializer.Deserialize<KnowledgeSyncState>(json);
return state ?? new KnowledgeSyncState();
}
catch
{
return new KnowledgeSyncState();
}
}
private void SaveState(KnowledgeSyncState state)
{
try
{
string dir = Path.GetDirectoryName(_options.StateFilePath);
if (!string.IsNullOrWhiteSpace(dir))
{
Directory.CreateDirectory(dir);
}
string json = JsonSerializer.Serialize(state, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(_options.StateFilePath, json);
}
catch
{
}
}
}
}