-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcaret_canvas.ts
117 lines (104 loc) · 3.31 KB
/
caret_canvas.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
import CaretPlugin from "main";
import { View } from "obsidian";
import { CaretPluginSettings, Edge, Node, SparkleConfig } from "./types";
export class CaretCanvas {
nodes: Node[];
edges: Edge[];
canvas: any;
canvasKeybinds: {
[command: string]: {
keybind: string;
description: string;
enabled: boolean;
};
};
constructor(readonly canvas_view: View) {
this.canvasKeybinds = {
NavigateUp: {
keybind: "ArrowUp",
description: "Move up in the canvas",
enabled: true,
},
NavigateDown: {
keybind: "ArrowDown",
description: "Move down in the canvas",
enabled: true,
},
NavigateLeft: {
keybind: "ArrowLeft",
description: "Move left in the canvas",
enabled: true,
},
NavigateRight: {
keybind: "ArrowRight",
description: "Move right in the canvas",
enabled: true,
},
};
// @ts-ignore
if (!canvas_view || !canvas_view.canvas) {
return;
}
// @ts-ignore
const canvas = canvas_view.canvas;
this.canvas = canvas;
// node.unknownData.role = "user";
const canvas_data = canvas.getData();
const { edges, nodes } = canvas_data;
this.nodes = nodes;
this.edges = edges;
}
textById() {
const res: { [k: string]: string } = {};
this.nodes.forEach((node) => {
res[node.id] = node.text;
});
return res;
}
getNode(nodeId: string) {
const [res] = this.nodes.filter((node) => node.id === nodeId);
return new CaretNode(res, this);
}
getLongestLineage(node_id: string) {
return CaretPlugin.getLongestLineage(this.nodes, this.edges, node_id);
}
static fromPlugin(plugin: CaretPlugin) {
return new CaretCanvas(plugin.app.workspace.getMostRecentLeaf()!.view);
}
}
export function mergeSettingsAndSparkleConfig(
settings: CaretPluginSettings,
sparkle_config: SparkleConfig
): SparkleConfig {
let model = settings.model;
let provider = settings.llm_provider;
let temperature = settings.temperature;
let context_window: string | number = settings.context_window;
if (sparkle_config.model !== "default") {
model = sparkle_config.model;
}
if (sparkle_config.provider !== "default") {
provider = sparkle_config.provider;
}
if (sparkle_config.temperature !== settings.temperature) {
temperature = sparkle_config.temperature;
}
if (sparkle_config.context_window !== "default") {
context_window = sparkle_config.context_window;
}
return { model, provider, temperature, context_window };
}
export class CaretNode {
constructor(readonly node: Node, readonly canvas_nodes: CaretCanvas) {}
outgoingNodes() {
return this.canvas_nodes.edges
.filter((edge) => edge.fromNode === this.node.id)
.map((edge) => this.canvas_nodes.getNode(edge.toNode));
}
get id() {
return this.node.id;
}
getLongestLineage() {
return this.canvas_nodes.getLongestLineage(this.node.id);
}
}