feat: init
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { App, Notice, PluginSettingTab, Setting } from "obsidian";
|
||||
import { listAgents, BeaverApiError } from "./api";
|
||||
import type BeaverPlugin from "./main";
|
||||
|
||||
export interface BeaverSettings {
|
||||
baseUrl: string;
|
||||
token: string;
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: BeaverSettings = {
|
||||
baseUrl: "http://localhost:62993",
|
||||
token: "",
|
||||
};
|
||||
|
||||
export class BeaverSettingsTab extends PluginSettingTab {
|
||||
plugin: BeaverPlugin;
|
||||
|
||||
constructor(app: App, plugin: BeaverPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
display(): void {
|
||||
const { containerEl } = this;
|
||||
containerEl.empty();
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Base URL")
|
||||
.setDesc("Markdown frontend root, e.g. http://localhost:62993")
|
||||
.addText((text) =>
|
||||
text
|
||||
.setPlaceholder("http://localhost:62993")
|
||||
.setValue(this.plugin.settings.baseUrl)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.baseUrl = value.trim().replace(/\/+$/, "");
|
||||
await this.plugin.saveSettings();
|
||||
}),
|
||||
);
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Bearer token")
|
||||
.setDesc("Token with the `messages` scope.")
|
||||
.addText((text) => {
|
||||
text.inputEl.type = "password";
|
||||
text
|
||||
.setPlaceholder("paste token")
|
||||
.setValue(this.plugin.settings.token)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.token = value;
|
||||
await this.plugin.saveSettings();
|
||||
});
|
||||
});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Test connection")
|
||||
.setDesc("Calls GET /agents and reports the count.")
|
||||
.addButton((btn) =>
|
||||
btn.setButtonText("Test").onClick(async () => {
|
||||
try {
|
||||
const agents = await listAgents(this.plugin.settings);
|
||||
this.plugin.cacheAgents(agents);
|
||||
new Notice(`Beaver: found ${agents.length} agents`);
|
||||
} catch (err) {
|
||||
const msg =
|
||||
err instanceof BeaverApiError
|
||||
? `${err.status}: ${err.message}`
|
||||
: err instanceof Error
|
||||
? err.message
|
||||
: String(err);
|
||||
new Notice(`Beaver: ${msg}`, 8000);
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user