Compare commits

..

No commits in common. "master" and "1.22.1" have entirely different histories.

3 changed files with 140 additions and 276 deletions

412
main.ts
View file

@ -113,6 +113,18 @@ export default class LoomPlugin extends Plugin {
return callback(file);
}
saveAndRender() {
this.save();
if (this.rendering) return;
this.rendering = true;
this.renderLoomViews();
this.renderLoomSiblingsViews();
this.rendering = false;
}
thenSaveAndRender(callback: () => void) {
callback();
this.saveAndRender();
@ -124,6 +136,20 @@ export default class LoomPlugin extends Plugin {
});
}
renderLoomViews() {
const views = this.app.workspace
.getLeavesOfType("loom")
.map((leaf) => leaf.view) as LoomView[];
views.forEach((view) => view.render());
}
renderLoomSiblingsViews() {
const views = this.app.workspace
.getLeavesOfType("loom-siblings")
.map((leaf) => leaf.view) as LoomSiblingsView[];
views.forEach((view) => view.render());
}
initializeProviders() {
const preset = getPreset(this.settings);
if (preset === undefined) return;
@ -789,7 +815,6 @@ export default class LoomPlugin extends Plugin {
// return;
// }
// this.editor.setCursor(cursor);
this.saveAndRender(); // Add explicit view refresh
})
)
);
@ -1194,7 +1219,8 @@ export default class LoomPlugin extends Plugin {
};
plugin.update();
this.refreshViews();
this.renderLoomViews();
this.renderLoomSiblingsViews();
};
this.registerEvent(
@ -1211,7 +1237,8 @@ export default class LoomPlugin extends Plugin {
this.registerEvent(
this.app.workspace.on("resize", () => {
this.refreshViews();
this.renderLoomViews();
this.renderLoomSiblingsViews();
})
);
@ -1267,7 +1294,7 @@ export default class LoomPlugin extends Plugin {
this.state[file.path].generating = rootNode;
// show the "Generating..." indicator in the loom view
this.refreshViews();
this.renderLoomViews();
let prompt = `${this.settings.prepend}${this.fullText(file, rootNode)}`;
@ -1374,19 +1401,6 @@ export default class LoomPlugin extends Plugin {
}
async completeCohere(prompt: string) {
if (this.settings.logApiCalls) {
console.log("Cohere request:", {
model: getPreset(this.settings).model,
prompt,
max_tokens: this.settings.maxTokens,
num_generations: this.settings.n,
temperature: this.settings.temperature,
p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
});
}
const tokens = (await cohere.tokenize({ text: prompt })).body.token_strings;
prompt = tokens.slice(-getPreset(this.settings).contextLength).join("");
@ -1401,10 +1415,6 @@ export default class LoomPlugin extends Plugin {
presence_penalty: this.settings.presencePenalty,
});
if (this.settings.logApiCalls) {
console.log("Cohere response:", response);
}
const result: CompletionResult =
response.statusCode === 200
? {
@ -1413,30 +1423,17 @@ export default class LoomPlugin extends Plugin {
(generation) => generation.text
),
}
: {
:
{
ok: false,
status: response.statusCode!,
message: "",
// @ts-expect-error
message: response.body.message,
};
return result;
}
async completeTextSynth(prompt: string) {
const body = {
prompt,
max_tokens: this.settings.maxTokens,
best_of: this.settings.bestOf,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
};
if (this.settings.logApiCalls) {
console.log("TextSynth request:", body);
}
const response = await requestUrl({
url: `https://api.textsynth.com/v1/engines/${
getPreset(this.settings).model
@ -1447,13 +1444,18 @@ export default class LoomPlugin extends Plugin {
Authorization: `Bearer ${getPreset(this.settings).apiKey}`,
},
throw: false,
body: JSON.stringify(body),
body: JSON.stringify({
prompt,
max_tokens: this.settings.maxTokens,
best_of: this.settings.bestOf,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
}),
});
if (this.settings.logApiCalls) {
console.log("TextSynth response:", response);
}
let result: CompletionResult;
if (response.status === 200) {
const completions =
@ -1463,7 +1465,7 @@ export default class LoomPlugin extends Plugin {
result = {
ok: false,
status: response.status,
message: response.json.error || "Unknown error",
message: response.json.error,
};
}
return result;
@ -1477,15 +1479,6 @@ export default class LoomPlugin extends Plugin {
"gpt-3.5-turbo",
"gpt-3.5-turbo-0301",
"gpt-4-base",
// TODO: llama 3.1 has "28k additional multilingual tokens", so cl100k is not exactly right
"meta-llama/llama-3.1-8b",
"meta-llama/llama-3.1-70b",
"meta-llama/llama-3.1-405b",
"meta-llama/llama-3.1-8b-instruct",
"meta-llama/llama-3.1-70b-instruct",
"meta-llama/llama-3.1-405b-instruct",
"meta-llama/Meta-Llama-3.1-405B",
"meta-llama/Meta-Llama-3.1-405B-FP8",
];
const p50kModels = [
"text-davinci-003",
@ -1526,7 +1519,6 @@ export default class LoomPlugin extends Plugin {
if (!url.endsWith("/")) url += "/";
url = url.replace(/v1\//, "");
url += "v1/completions";
let body: any = {
prompt,
model: getPreset(this.settings).model,
@ -1534,22 +1526,16 @@ export default class LoomPlugin extends Plugin {
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
best_of:
this.settings.bestOf > this.settings.n
? this.settings.bestOf
: this.settings.n,
};
if (this.settings.bestOf > this.settings.n) {
body.best_of = this.settings.bestOf;
}
if (this.settings.frequencyPenalty !== 0)
body.frequency_penalty = this.settings.frequencyPenalty;
if (this.settings.presencePenalty !== 0)
body.presence_penalty = this.settings.presencePenalty;
if (this.settings.logApiCalls) {
console.log("OpenAI-compatible API request:", {
url,
body
});
}
const response = await requestUrl({
url,
method: "POST",
@ -1561,10 +1547,6 @@ export default class LoomPlugin extends Plugin {
body: JSON.stringify(body),
});
if (this.settings.logApiCalls) {
console.log("OpenAI-compatible API response:", response);
}
const result: CompletionResult =
response.status === 200
? {
@ -1573,16 +1555,7 @@ export default class LoomPlugin extends Plugin {
(choice: any) => choice.text
),
}
: {
ok: false,
status: response.status,
message: response.json?.error?.message || "Unknown error"
};
if (!result.ok && this.settings.logApiCalls) {
console.error("OpenAI-compatible error:", response);
}
: { ok: false, status: response.status, message: "" };
return result;
}
@ -1607,10 +1580,6 @@ export default class LoomPlugin extends Plugin {
if (this.settings.presencePenalty !== 0)
body.presence_penalty = this.settings.presencePenalty;
if (this.settings.logApiCalls) {
console.log("OpenRouter request:", body);
}
const requests = Array(this.settings.n).fill(null).map(() =>
requestUrl({
url: "https://openrouter.ai/api/v1/completions",
@ -1628,58 +1597,42 @@ export default class LoomPlugin extends Plugin {
const responses = await Promise.all(requests);
if (this.settings.logApiCalls) {
console.log("OpenRouter responses:", responses);
}
const result: CompletionResult = responses.every(response => !response.json.hasOwnProperty('error'))
const result: CompletionResult = responses.every(response => response.status === 200)
? {
ok: true,
completions: responses.map(response => response.json.choices[0].text),
}
: {
ok: false,
status: responses[0].json.error.code,
message: responses[0].json.error.message,
status: responses[0].status,
message: responses[0].text,
};
return result;
}
async completeOpenAI(prompt: string) {
prompt = this.trimOpenAIPrompt(prompt);
const body = {
model: getPreset(this.settings).model,
prompt,
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
};
if (this.settings.logApiCalls) {
console.log("OpenAI request:", body);
}
let result: CompletionResult;
try {
const response = await this.openai.createCompletion(body);
if (this.settings.logApiCalls) {
console.log("OpenAI response:", response);
}
const response = await this.openai.createCompletion({
model: getPreset(this.settings).model,
prompt,
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
});
result = {
ok: true,
completions: response.data.choices.map((choice) => choice.text || ""),
};
} catch (e) {
if (this.settings.logApiCalls) {
console.error("OpenAI error:", e);
}
result = {
ok: false,
status: e.response.status,
message: e.response.data.error.message || "Unknown error",
message: e.response.data.error.message,
};
}
return result;
@ -1687,27 +1640,18 @@ export default class LoomPlugin extends Plugin {
async completeOpenAIChat(prompt: string) {
prompt = this.trimOpenAIPrompt(prompt);
const body = {
model: getPreset(this.settings).model,
messages: [{ role: "assistant" as const, content: prompt }],
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
};
if (this.settings.logApiCalls) {
console.log("OpenAI Chat request:", body);
}
let result: CompletionResult;
try {
const response = await this.openai.createChatCompletion(body);
if (this.settings.logApiCalls) {
console.log("OpenAI Chat response:", response);
}
const response = await this.openai.createChatCompletion({
model: getPreset(this.settings).model,
messages: [{ role: "assistant", content: prompt }],
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
});
result = {
ok: true,
completions: response.data.choices.map(
@ -1715,13 +1659,10 @@ export default class LoomPlugin extends Plugin {
),
};
} catch (e) {
if (this.settings.logApiCalls) {
console.error("OpenAI Chat error:", e);
}
result = {
ok: false,
status: e.response.status,
message: e.response.data.error.message || "Unknown error",
message: e.response.data.error.message,
};
}
return result;
@ -1729,39 +1670,27 @@ export default class LoomPlugin extends Plugin {
async completeAzure(prompt: string) {
prompt = this.trimOpenAIPrompt(prompt);
const body = {
model: getPreset(this.settings).model,
prompt,
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
};
if (this.settings.logApiCalls) {
console.log("Azure request:", body);
}
let result: CompletionResult;
try {
const response = await this.azure.createCompletion(body);
if (this.settings.logApiCalls) {
console.log("Azure response:", response);
}
const response = await this.azure.createCompletion({
model: getPreset(this.settings).model,
prompt,
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
});
result = {
ok: true,
completions: response.data.choices.map((choice) => choice.text || ""),
};
} catch (e) {
if (this.settings.logApiCalls) {
console.error("Azure error:", e);
}
result = {
ok: false,
status: e.response.status,
message: e.response.data.error.message || "Unknown error",
message: e.response.data.error.message,
};
}
return result;
@ -1769,27 +1698,18 @@ export default class LoomPlugin extends Plugin {
async completeAzureChat(prompt: string) {
prompt = this.trimOpenAIPrompt(prompt);
const body = {
model: getPreset(this.settings).model,
messages: [{ role: "assistant" as const, content: prompt }],
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
};
if (this.settings.logApiCalls) {
console.log("Azure Chat request:", body);
}
let result: CompletionResult;
try {
const response = await this.azure.createChatCompletion(body);
if (this.settings.logApiCalls) {
console.log("Azure Chat response:", response);
}
const response = await this.azure.createChatCompletion({
model: getPreset(this.settings).model,
messages: [{ role: "assistant", content: prompt }],
max_tokens: this.settings.maxTokens,
n: this.settings.n,
temperature: this.settings.temperature,
top_p: this.settings.topP,
frequency_penalty: this.settings.frequencyPenalty,
presence_penalty: this.settings.presencePenalty,
});
result = {
ok: true,
completions: response.data.choices.map(
@ -1797,13 +1717,10 @@ export default class LoomPlugin extends Plugin {
),
};
} catch (e) {
if (this.settings.logApiCalls) {
console.error("Azure Chat error:", e);
}
result = {
ok: false,
status: e.response.status,
message: e.response.data.error.message || "Unknown error",
message: e.response.data.error.message,
};
}
return result;
@ -1886,35 +1803,6 @@ export default class LoomPlugin extends Plugin {
await this.saveData({ settings: this.settings, state: this.state });
this.initializeProviders();
}
// Add this new method to properly refresh all views
private refreshViews() {
// Refresh Loom views
this.app.workspace.getLeavesOfType("loom").forEach((leaf) => {
if (leaf.view instanceof LoomView) {
leaf.view.render();
}
});
// Refresh Loom siblings views
this.app.workspace.getLeavesOfType("loom-siblings").forEach((leaf) => {
if (leaf.view instanceof LoomSiblingsView) {
leaf.view.render();
}
});
}
// Update saveAndRender to use the new refresh method
async saveAndRender() {
await this.save();
if (this.rendering) return;
this.rendering = true;
this.refreshViews();
this.rendering = false;
}
}
// this relies on `LoomPlugin`, so it's here, not in `views.ts`
@ -2014,87 +1902,63 @@ class LoomSettingTab extends PluginSettingTab {
});
fillInModelDropdown.createEl("option", {
text: "Llama 3.1 405B (Hyperbolic)",
attr: { value: "llama-3.1-405b-hyperbolic" },
text: "davinci-002",
attr: { value: "davinci-002" },
});
fillInModelDropdown.createEl("option", {
text: "Llama 3.1 405B (OpenRouter)",
attr: { value: "llama-3.1-405b-openrouter" },
text: "code-davinci-002",
attr: { value: "code-davinci-002" },
});
fillInModelDropdown.createEl("option", {
text: "Claude 3 Opus",
attr: { value: "claude-3-opus" },
text: "code-davinci-002 (Proxy)",
attr: { value: "code-davinci-002-proxy" },
});
fillInModelDropdown.createEl("option", {
text: "Claude 3.5 Sonnet",
attr: { value: "claude-3.5-sonnet" },
});
fillInModelDropdown.createEl("option", {
text: "GPT-4 base",
text: "gpt-4-base",
attr: { value: "gpt-4-base" },
});
fillInModelDropdown.createEl("option", {
text: "davinci-002",
attr: { value: "davinci-002" },
text: "claude-3-opus",
attr: { value: "claude-3-opus" },
});
fillInModelDropdown.addEventListener("change", (event) => {
const value = (event.target as HTMLSelectElement).value;
switch (value) {
case "llama-3.1-405b-hyperbolic": {
case "davinci-002": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "openai";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "davinci-002";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 16384;
break;
}
case "code-davinci-002": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "openai";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "code-davinci-002";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 8001;
break;
}
case "code-davinci-002-proxy": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "openai-compat";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
// @ts-expect-error
].url = "https://api.hyperbolic.xyz";
].model = "code-davinci-002";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "meta-llama/Meta-Llama-3.1-405B";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 32768;
break;
}
case "llama-3.1-405b-openrouter": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "openrouter";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
// @ts-expect-error
].quantization = "bf16";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "meta-llama/llama-3.1-405b";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 32768;
break;
}
case "claude-3-opus": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "anthropic";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "claude-3-opus-20240229";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 50000;
break;
}
case "claude-3.5-sonnet": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "anthropic";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "claude-3-5-sonnet-20240620";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 50000;
].contextLength = 8001;
break;
}
case "gpt-4-base": {
@ -2109,16 +1973,16 @@ class LoomSettingTab extends PluginSettingTab {
].contextLength = 8192;
break;
}
case "davinci-002": {
case "claude-3-opus": {
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].provider = "openai";
].provider = "anthropic";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].model = "davinci-002";
].model = "claude-3-opus-20240229";
this.plugin.settings.modelPresets[
this.plugin.settings.modelPreset
].contextLength = 16384;
].contextLength = 20000;
break;
}
}

View file

@ -1,7 +1,7 @@
{
"id": "loom",
"name": "Loom",
"version": "1.22.6",
"version": "1.22.1",
"minAppVersion": "0.15.0",
"description": "Loom in Obsidian",
"author": "celeste",

View file

@ -1,6 +1,6 @@
{
"name": "obsidian-loom",
"version": "1.22.6",
"version": "1.22.1",
"description": "Loom in Obsidian",
"main": "main.js",
"scripts": {