From 4885d53e0088f7d4e808f8c31de0f06da2c78c6b Mon Sep 17 00:00:00 2001 From: socketteer Date: Sat, 6 Apr 2024 10:25:24 -0700 Subject: [PATCH] multiple completions for Claude --- main.ts | 66 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/main.ts b/main.ts index e30ce94..e4444c8 100644 --- a/main.ts +++ b/main.ts @@ -195,14 +195,14 @@ export default class LoomPlugin extends Plugin { newNode(text: string, parentId: string | null, unread: boolean = false): [string, Node] { const id = uuidv4(); - const node: Node = { - text, - parentId, - collapsed: false, - unread, - bookmarked: false, - searchResultState: null, - }; + const node: Node = { + text, + parentId, + collapsed: false, + unread, + bookmarked: false, + searchResultState: null, + }; return [id, node]; } @@ -1275,7 +1275,7 @@ export default class LoomPlugin extends Plugin { const completionMethods: Record Promise> = { cohere: this.completeCohere, textsynth: this.completeTextSynth, - ocp: this.completeOCP, + ocp: this.completeOCP, openai: this.completeOpenAI, "openai-chat": this.completeOpenAIChat, azure: this.completeAzure, @@ -1284,7 +1284,7 @@ export default class LoomPlugin extends Plugin { }; let result; try { - result = await completionMethods[getPreset(this.settings).provider].bind(this)(prompt); + result = await completionMethods[getPreset(this.settings).provider].bind(this)(prompt); } catch (e) { new Notice(`Error: ${e}`); this.state[file.path].generating = null; @@ -1324,19 +1324,28 @@ export default class LoomPlugin extends Plugin { // create a child of the current node for each completion let ids = []; for (let completion of completions) { - const [id, node] = this.newNode(completion, rootNode, true); - state.nodes[id] = node; - ids.push(id); + const [id, node] = this.newNode(completion, rootNode, true); + state.nodes[id] = node; + ids.push(id); } - // switch to the first completion - this.app.workspace.trigger("loom:switch-to", ids[0]); + // switch to the first completion if currently at rootNode + // or if rootNode is in the ancestry of the current node + if (rootNode && (rootNode === state.current || this.ancestors(file, state.current).includes(rootNode))) { + this.app.workspace.trigger("loom:switch-to", ids[0]); + } this.state[file.path].generating = null; this.saveAndRender(); this.statusBarItem.style.display = "none"; } + addNode(file: TFile, text: string, parentId: string | null) { + const state = this.state[file.path]; + const [id, node] = this.newNode(text, parentId, true); + state.nodes[id] = node; + } + async completeCohere(prompt: string) { const tokens = (await cohere.tokenize({ text: prompt })).body.token_strings; prompt = tokens.slice(-getPreset(this.settings).contextLength).join(""); @@ -1514,8 +1523,8 @@ export default class LoomPlugin extends Plugin { n: this.settings.n, temperature: this.settings.temperature, top_p: this.settings.topP, - frequency_penalty: this.settings.frequencyPenalty, - presence_penalty: this.settings.presencePenalty, + frequency_penalty: this.settings.frequencyPenalty, + presence_penalty: this.settings.presencePenalty, }); result = { ok: true, completions: response.data.choices.map((choice) => choice.message?.content || "") }; } catch (e) { @@ -1525,10 +1534,15 @@ export default class LoomPlugin extends Plugin { } async completeAnthropic(prompt: string) { - prompt = this.trimOpenAIPrompt(prompt); - // let result: CompletionResult; + const completions = await Promise.all([...Array(this.settings.n).keys()].map(async () => { return await this.getAnthropicResponse(prompt);} )); + const result: CompletionResult = { ok: true, completions }; + return result; + } + async getAnthropicResponse(prompt: string) { + prompt = this.trimOpenAIPrompt(prompt); + // let result: CompletionResult; try { // console.log("prompt", prompt); const response = await requestUrl({ @@ -1551,18 +1565,22 @@ export default class LoomPlugin extends Plugin { }), }); + if(response.status !== 200) { + console.error("response", response); + return null; + } - const result: CompletionResult = response.status === 200 - ? { ok: true, completions: [response.json.content[0]?.text || ""] } - : { ok: false, status: response.status, message: "" }; + const result = response.json.content[0]?.text || ""; + + // ? { ok: true, completions: [response.json.content[0]?.text || ""] } + // : { ok: false, status: response.status, message: "" }; // console.log("response", result); return result; } catch (e) { console.error(e) - const result: CompletionResult = { ok: false, status: 400, message: "an error was encountered" }; - return result; + return null; } }