-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
520 lines (447 loc) · 17.1 KB
/
main.ts
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import { App, Plugin, PluginSettingTab, Setting, TFile, Notice, Modal, MarkdownView, DropdownComponent } from 'obsidian';
import { AIService, AIServiceConfig } from './src/services/AIService';
interface ProviderSettings {
apiKey: string;
apiUrl: string;
model: string;
}
interface AutoTaggerSettings {
provider: string;
providerSettings: Record<string, ProviderSettings>;
customPrompt: string;
}
const DEFAULT_SETTINGS: AutoTaggerSettings = {
provider: 'openai',
customPrompt: '你是一个文档标签生成器,请根据文档内容生成最多 3 个相关的标签。只需返回标签,用逗号分隔,不要包含其他解释或说明。',
providerSettings: {
openai: {
apiKey: '',
apiUrl: 'https://api.openai.com/v1/chat/completions',
model: 'gpt-4o-mini'
},
gemini: {
apiKey: '',
apiUrl: 'https://generativelanguage.googleapis.com/v1beta/models/',
model: 'gemini-1.5-flash'
},
claude: {
apiKey: '',
apiUrl: 'https://api.anthropic.com/v1/messages',
model: 'claude-3-5-sonnet'
},
deepseek: {
apiKey: '',
apiUrl: 'https://api.deepseek.com/v1/chat/completions',
model: 'deepseek-chat'
},
volcano: {
apiKey: '',
apiUrl: 'https://ark.cn-beijing.volces.com/api/v3/chat/completions',
model: ''
}
}
}
const PROVIDER_CONFIGS = {
openai: {
defaultUrl: 'https://api.openai.com/v1/chat/completions',
defaultModel: 'gpt-4o-mini',
models: ['gpt-4o-mini', 'gpt-4o', 'gpt-3.5-turbo']
},
gemini: {
defaultUrl: 'https://generativelanguage.googleapis.com',
defaultModel: 'gemini-1.5-flash',
models: ['gemini-1.5-flash', 'gemini-2.0-flash']
},
claude: {
defaultUrl: 'https://api.anthropic.com/v1/messages',
defaultModel: 'claude-3-5-sonnet',
models: ['claude-3-5-sonnet', 'claude-3-7-sonnet', 'claude-3-opus', 'claude-3-haiku']
},
deepseek: {
defaultUrl: 'https://api.deepseek.com/v1/chat/completions',
defaultModel: 'deepseek-chat',
models: ['deepseek-chat', 'deepseek-reasoner']
},
volcano: {
defaultUrl: 'https://ark.cn-beijing.volces.com/api/v3/chat/completions',
defaultModel: '',
models: ['']
}
};
export default class AutoTaggerPlugin extends Plugin {
settings: AutoTaggerSettings;
async onload() {
await this.loadSettings();
this.addRibbonIcon('tag', '生成标签', async () => {
await this.generateTags();
});
this.addSettingTab(new AutoTaggerSettingTab(this.app, this));
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async generateTags() {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeView) {
new Notice('请打开一个文档以生成标签');
return;
}
const file = activeView.file;
const content = await this.app.vault.read(file);
const loadingNotice = new Notice('正在生成标签...', 0);
try {
const tags = await this.analyzeTags(content);
loadingNotice.hide();
new TagSelectionModal(this.app, tags, async (selectedTags) => {
await this.updateFileFrontMatter(file, selectedTags);
}).open();
} catch (error) {
loadingNotice.hide();
new Notice(`生成标签失败:${error.message}`);
}
}
async analyzeTags(content: string): Promise<string[]> {
const currentProvider = this.settings.provider;
const providerConfig = this.settings.providerSettings[currentProvider];
if (!providerConfig || !providerConfig.apiKey) {
throw new Error('请先在设置中配置 API 密钥');
}
const config: AIServiceConfig = {
apiKey: providerConfig.apiKey,
apiUrl: providerConfig.apiUrl,
model: providerConfig.model,
customPrompt: this.settings.customPrompt
};
const aiService = new AIService(config);
try {
return await aiService.generateTags(content);
} catch (error) {
throw new Error(`生成标签失败:${error.message}`);
}
}
async updateFileFrontMatter(file: TFile, newTags: string[]) {
const content = await this.app.vault.read(file);
const yamlRegex = /^---\n([\s\S]*?)\n---/;
const hasYaml = yamlRegex.test(content);
let newContent;
if (hasYaml) {
const yamlMatch = content.match(yamlRegex);
const yaml = yamlMatch[1];
const tagsRegex = /^tags:\s*\[(.*)\]/m;
const tagsMatch = yaml.match(tagsRegex);
if (tagsMatch) {
const existingTagsStr = tagsMatch[1];
const existingTags = existingTagsStr
.split(',')
.map(tag => tag.trim())
.filter(tag => tag !== '');
const allTags = [...new Set([...existingTags, ...newTags])];
newContent = content.replace(tagsRegex, `tags: [${allTags.join(', ')}]`);
} else {
newContent = content.replace(yamlRegex, `---\n${yaml}\ntags: [${newTags.join(', ')}]\n---`);
}
} else {
newContent = `---\ntags: [${newTags.join(', ')}]\n---\n\n${content}`;
}
await this.app.vault.modify(file, newContent);
new Notice(`已成功添加标签:${newTags.join(', ')}`);
}
}
class TagSelectionModal extends Modal {
tags: string[];
onSubmit: (tags: string[]) => void;
selectedTags: string[];
constructor(app: App, tags: string[], onSubmit: (tags: string[]) => void) {
super(app);
this.tags = tags;
this.onSubmit = onSubmit;
this.selectedTags = [...tags];
}
onOpen() {
const { contentEl } = this;
contentEl.createEl('h3', { text: '标签生成' });
const tagContainer = contentEl.createDiv({ cls: 'tag-container' });
this.tags.forEach((tag, index) => {
const tagRow = tagContainer.createDiv({ cls: 'tag-row' });
const checkbox = tagRow.createEl('input', {
type: 'checkbox',
attr: { checked: true }
});
const tagInput = tagRow.createEl('input', {
type: 'text',
value: tag
});
checkbox.addEventListener('change', () => {
if (checkbox.checked) {
this.selectedTags[index] = tagInput.value;
} else {
this.selectedTags[index] = null;
}
});
tagInput.addEventListener('input', () => {
if (checkbox.checked) {
this.selectedTags[index] = tagInput.value;
}
});
});
const buttonContainer = contentEl.createDiv({ cls: 'button-container' });
buttonContainer.createEl('button', { text: '取消' }).addEventListener('click', () => {
this.close();
});
buttonContainer.createEl('button', { text: '确定' }).addEventListener('click', () => {
const finalTags = this.selectedTags.filter(tag => tag !== null);
this.onSubmit(finalTags);
this.close();
});
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
class AutoTaggerSettingTab extends PluginSettingTab {
plugin: AutoTaggerPlugin;
constructor(app: App, plugin: AutoTaggerPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h3', { text: '自动标签生成设置' });
// 添加 GitHub 链接
const githubLink = containerEl.createEl('a', {
text: '在 GitHub 上提交反馈',
href: 'https://github.com/dlzmoe/obsidian-ai-tags'
});
githubLink.addClass('setting-item-description');
githubLink.addClass('github-feedback-link');
// 添加提供商说明
const providerDescriptions = {
openai: [
'OpenAI',
'• API 地址:https://api.openai.com/v1/chat/completions',
'• 支持模型:gpt-4o-mini, gpt-4o, gpt-3.5-turbo'
],
gemini: [
'Gemini',
'• API 地址:https://generativelanguage.googleapis.com(暂不支持代理地址)',
'• 支持模型:gemini-1.5-flash, gemini-2.0-flash'
],
claude: [
'Claude',
'• API 地址:https://api.anthropic.com/v1/messages',
'• 支持模型:claude-3-5-sonnet, claude-3-7-sonnet, claude-3-opus, claude-3-haiku'
],
deepseek: [
'DeepSeek - 深度求索',
'• API 地址:https://api.deepseek.com/v1/chat/completions',
'• 支持模型:deepseek-chat, deepseek-reasoner'
],
volcano: [
'DeepSeek - 火山引擎',
'• API 地址:https://ark.cn-beijing.volces.com/api/v3/chat/completions',
'• 注意:需要在火山方舟设置推理模型,然后添加模型名称,如:ep-20250221084433'
]
};
const showProviderInfo = (provider: string) => {
const providerSection = containerEl.querySelector('.provider-section');
if (providerSection) {
providerSection.remove();
}
const section = containerEl.createDiv({ cls: 'provider-section' });
const providerInfo = section.createDiv({ cls: 'provider-info' });
providerDescriptions[provider].forEach(text => {
providerInfo.createEl('p', { text });
});
};
// 显示当前选择的提供商信息
showProviderInfo(this.plugin.settings.provider);
new Setting(containerEl)
.setName('AI 提供商')
.setDesc('选择 AI 服务提供商')
.addDropdown(dropdown => {
Object.keys(PROVIDER_CONFIGS).forEach(provider => {
dropdown.addOption(provider, provider === 'openai' ? 'OpenAI' :
provider === 'gemini' ? 'Gemini' :
provider === 'claude' ? 'Claude(测试中)' :
provider === 'deepseek' ? 'DeepSeek - 深度求索' :
'DeepSeek - 火山引擎');
});
dropdown.setValue(this.plugin.settings.provider);
dropdown.onChange(async (value) => {
// 保存当前提供商的设置
const currentProvider = this.plugin.settings.provider;
const currentSettings = this.plugin.settings.providerSettings[currentProvider];
// 更新提供商
this.plugin.settings.provider = value;
// 初始化新提供商的设置(仅在首次设置时)
if (!this.plugin.settings.providerSettings[value]) {
this.plugin.settings.providerSettings[value] = {
apiKey: '',
apiUrl: PROVIDER_CONFIGS[value].defaultUrl,
model: PROVIDER_CONFIGS[value].defaultModel
};
}
await this.plugin.saveSettings();
showProviderInfo(value);
this.display();
});
});
new Setting(containerEl)
.setName('API 密钥')
.setDesc('输入你的 API 密钥')
.addText(text => text
.setPlaceholder('请输入 API 密钥')
.setValue(this.plugin.settings.providerSettings[this.plugin.settings.provider].apiKey)
.onChange(async (value) => {
this.plugin.settings.providerSettings[this.plugin.settings.provider].apiKey = value;
await this.plugin.saveSettings();
}))
.addButton(button => {
button
.setIcon('check')
.setTooltip('测试连通性')
.onClick(async (evt) => {
const buttonEl = evt.currentTarget as HTMLElement;
if (buttonEl.hasClass('loading')) return;
const provider = this.plugin.settings.provider;
const config = this.plugin.settings.providerSettings[provider];
if (!config.apiKey) {
new Notice('请先输入 API 密钥');
return;
}
buttonEl.addClass('loading');
const originalIcon = buttonEl.querySelector('.svg-icon')?.innerHTML;
buttonEl.querySelector('.svg-icon').innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-loader"><line x1="12" y1="2" x2="12" y2="6"></line><line x1="12" y1="18" x2="12" y2="22"></line><line x1="4.93" y1="4.93" x2="7.76" y2="7.76"></line><line x1="16.24" y1="16.24" x2="19.07" y2="19.07"></line><line x1="2" y1="12" x2="6" y2="12"></line><line x1="18" y1="12" x2="22" y2="12"></line><line x1="4.93" y1="19.07" x2="7.76" y2="16.24"></line><line x1="16.24" y1="7.76" x2="19.07" y2="4.93"></line></svg>';
const aiService = new AIService({
apiKey: config.apiKey,
apiUrl: config.apiUrl,
model: config.model
});
try {
await aiService.testConnection();
new Notice('API 连接测试成功');
} catch (error) {
new Notice(`API 连接测试失败: ${error.message}`);
} finally {
buttonEl.removeClass('loading');
if (originalIcon) {
buttonEl.querySelector('.svg-icon').innerHTML = originalIcon;
}
}
});
});
new Setting(containerEl)
.setName('API 地址')
.setDesc('输入 API 地址')
.addText(text => text
.setPlaceholder(PROVIDER_CONFIGS[this.plugin.settings.provider].defaultUrl)
.setValue(this.plugin.settings.providerSettings[this.plugin.settings.provider].apiUrl)
.onChange(async (value) => {
this.plugin.settings.providerSettings[this.plugin.settings.provider].apiUrl = value;
await this.plugin.saveSettings();
}))
.addButton(button => {
button
.setIcon('reset')
.setTooltip('恢复默认 API 地址')
.onClick(async (evt) => {
const buttonEl = evt.currentTarget as HTMLElement;
if (buttonEl.hasClass('loading')) return;
buttonEl.addClass('loading');
const originalIcon = buttonEl.querySelector('.svg-icon')?.innerHTML;
buttonEl.querySelector('.svg-icon').innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-loader"><line x1="12" y1="2" x2="12" y2="6"></line><line x1="12" y1="18" x2="12" y2="22"></line><line x1="4.93" y1="4.93" x2="7.76" y2="7.76"></line><line x1="16.24" y1="16.24" x2="19.07" y2="19.07"></line><line x1="2" y1="12" x2="6" y2="12"></line><line x1="18" y1="12" x2="22" y2="12"></line><line x1="4.93" y1="19.07" x2="7.76" y2="16.24"></line><line x1="16.24" y1="7.76" x2="19.07" y2="4.93"></line></svg>';
try {
this.plugin.settings.providerSettings[this.plugin.settings.provider].apiUrl =
PROVIDER_CONFIGS[this.plugin.settings.provider].defaultUrl;
await this.plugin.saveSettings();
this.display();
} finally {
buttonEl.removeClass('loading');
if (originalIcon) {
buttonEl.querySelector('.svg-icon').innerHTML = originalIcon;
}
}
});
});
// 为特定提供商添加提示
if (this.plugin.settings.provider === 'gemini') {
containerEl.createEl('div', {
text: '注意:Gemini API URL 暂不支持,请使用 https://generativelanguage.googleapis.com 作为默认地址。',
cls: 'setting-item-description'
});
} else if (this.plugin.settings.provider === 'volcano') {
containerEl.createEl('div', {
text: '注意:火山引擎 API URL 应以 https://ark.cn-beijing.volces.com/api/v3/chat/completions 开头,模型服务名称将自动添加。',
cls: 'setting-item-description'
});
}
// 添加预设模型下拉框
new Setting(containerEl)
.setName('预设模型')
.setDesc('选择预设的 AI 模型')
.addDropdown(dropdown => {
const models = PROVIDER_CONFIGS[this.plugin.settings.provider].models;
models.forEach(model => {
dropdown.addOption(model, model);
});
// 获取当前提供商的模型设置
const currentModel = this.plugin.settings.providerSettings[this.plugin.settings.provider].model;
// 设置当前值为预设模型中的一个,如果不在预设中则选择第一个
const defaultModel = models.includes(currentModel) ? currentModel : models[0];
dropdown.setValue(defaultModel);
dropdown.onChange(async (value) => {
// 只有当用户没有输入自定义模型时,才使用预设模型
const customModelInput = this.plugin.settings.providerSettings[this.plugin.settings.provider].customModel || '';
if (!customModelInput.trim()) {
this.plugin.settings.providerSettings[this.plugin.settings.provider].model = value;
await this.plugin.saveSettings();
}
});
})
.settingEl.dty=nodr':lay =
// 为每个提供商添加自定义模型输入
new === 'volcano' ? 'none' : 'flex'Setting(containerEl)
.setName(this.plugin.settings.provider === 'volcano' ? '模型' : '自定义模型')
.setDesc(this.plugin.settings.provider === 'volcano' ? '输入模型名称' : '输入自定义模型名称 (优先使用)')
.addText(text => text
.setPlaceholder('自定义模型名称')
.setValue(this.plugin.settings.providerSettings[this.plugin.settings.provider].model)
.onChange(async (value) => {
// 更新模型设置为自定义输入的值
this.plugin.settings.providerSettings[this.plugin.settings.provider].model = value.trim();
await this.plugin.saveSettings();
}));
// 添加自定义提示词设置
const defaultPrompt = '你是一个文档标签生成器,请根据文档内容生成最多 3 个相关的标签。只需返回标签,用逗号分隔,不要包含其他解释或说明。';
const customPromptSetting = new Setting(containerEl)
.setClass('custom-prompt-setting')
.setName('自定义提示词')
.setDesc('自定义 AI 生成标签的提示词,留空则使用默认提示词')
.addTextArea(text => {
text.setValue(this.plugin.settings.customPrompt || defaultPrompt)
.onChange(async (value) => {
this.plugin.settings.customPrompt = value;
await this.plugin.saveSettings();
});
text.inputEl.rows = 6;
text.inputEl.cols = 50;
text.inputEl.style.minHeight = '150px';
text.inputEl.style.resize = 'vertical';
})
.addButton(button => {
button
.setIcon('reset')
.setTooltip('重置为默认提示词')
.onClick(async () => {
this.plugin.settings.customPrompt = defaultPrompt;
await this.plugin.saveSettings();
this.display();
});
});
}
}