Skip to content

Commit

Permalink
Add subcannolis
Browse files Browse the repository at this point in the history
  • Loading branch information
blindmansion committed Sep 25, 2024
1 parent 99fe93b commit 0d9976e
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 5 deletions.
11 changes: 8 additions & 3 deletions packages/cannoli-core/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,10 @@ export class CannoliFactory {

if (node.type === "file") {
node = node as CannoliCanvasFileData;
// If the node is a file, the "file" property is a path. Get the text after the final "/" and remove the extension
const fileName = node.file.split("/").pop();
universalText = fileName?.split(".").shift();
// // If the node is a file, the "file" property is a path. Get the text after the final "/" and remove the extension
// const fileName = node.file.split("/").pop();
// universalText = fileName?.split(".").shift();
universalText = node.file;

// Then, prepend "{{[[" and append "]]}}" to the text to match the file reference format
universalText = `{{[[${universalText}]]}}`;
Expand Down Expand Up @@ -865,6 +866,10 @@ export class CannoliFactory {
): ContentNodeType | null {
// If its a file node, return reference
if (node.type === "file") {
node = node as CannoliCanvasFileData;
if (node.file && node.file.endsWith(".canvas") && node.color && node.color === "2") {
return ContentNodeType.Subcannoli;
}
return ContentNodeType.Reference;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/cannoli-core/src/fileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface FileManager {
isMock: boolean,
): Promise<ArrayBuffer | null>;

getCanvas(
fileName: string,
isMock: boolean,
): Promise<string | null>;

editSelection(newContent: string, isMock: boolean): void;

getPropertyOfNote(
Expand Down
11 changes: 11 additions & 0 deletions packages/cannoli-core/src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { ReferenceNode } from "./graph/objects/vertices/nodes/content/ReferenceN
import { SearchNode } from "./graph/objects/vertices/nodes/content/SearchNode";
import { ContentNode } from "./graph/objects/vertices/nodes/ContentNode";
import { FloatingNode } from "./graph/objects/FloatingNode";
import { SubcannoliNode } from "./graph/objects/vertices/nodes/content/SubcannoliNote";

export enum CannoliObjectKind {
Node = "node",
Expand Down Expand Up @@ -82,6 +83,7 @@ export enum ContentNodeType {
Formatter = "formatter",
Http = "http",
Search = "search",
Subcannoli = "subcannoli",
}

export enum FloatingNodeType {
Expand Down Expand Up @@ -304,6 +306,15 @@ export class CannoliGraph {
);
break;
}
case ContentNodeType.Subcannoli: {
const subcannoliNode =
node as VerifiedCannoliCanvasFileData;
this.graph[node.id] = new SubcannoliNode(
subcannoliNode,
this.cannoliCanvasData
);
break;
}
case ContentNodeType.Search: {
const searchNode =
node as VerifiedCannoliCanvasTextData;
Expand Down
16 changes: 16 additions & 0 deletions packages/cannoli-core/src/graph/objects/vertices/CannoliNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@ export class CannoliNode extends CannoliVertex {
return note;
}

async getContentFromCanvas(reference: Reference): Promise<unknown | null> {
if (!this.run.fileManager) {
throw new Error("No fileManager found");
}

const canvas = await this.run.fileManager.getCanvas(reference.name, this.run.isMock);

if (canvas === null) {
return null;
}

const canvasObject = JSON.parse(canvas);

return canvasObject;
}

getContentFromFloatingNode(name: string): string | null {
for (const object of Object.values(this.graph)) {
if (object instanceof FloatingNode && object.getName() === name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ContentNode } from "../ContentNode";
import { Reference, ReferenceType, VerifiedCannoliCanvasData, VerifiedCannoliCanvasFileData, VerifiedCannoliCanvasLinkData, VerifiedCannoliCanvasTextData } from "src/graph";

export class SubcannoliNode extends ContentNode {
reference: Reference;

constructor(
nodeData:
| VerifiedCannoliCanvasFileData
| VerifiedCannoliCanvasLinkData
| VerifiedCannoliCanvasTextData,
fullCanvasData: VerifiedCannoliCanvasData
) {
super(nodeData, fullCanvasData);

this.reference = {
name: nodeData.cannoliData.text.replace("{{[[", "").replace("]]}}", ""),
shouldExtract: false,
type: ReferenceType.Note,
includeLink: false,
includeName: false,
includeProperties: false,
subpath: "",
}
}

async execute(): Promise<void> {
this.executing();

const content = await this.getContentFromCanvas(this.reference);

if (content === null) {
this.error("Could not find subcannoli canvas.");
return;
}

const variableValues = this.getVariableValues(false);

const args: Record<string, string> = {};

for (const variable of variableValues) {
args[variable.name] = variable.content;
}

let results: Record<string, string> = {};

try {
results = await this.run.subcannoliCallback(content, args, this.run.isMock);
} catch (error) {
this.error(`Error executing subcannoli: ${error}`);
return;
}

const resultsString = JSON.stringify(results, null, 2);

const outgoingEdges = this.getOutgoingEdges();

for (const edge of outgoingEdges) {
if (edge.text in results) {
edge.load({ content: results[edge.text] });
} else {
edge.load({ content: resultsString });
}
}

this.completed();
}
}
22 changes: 21 additions & 1 deletion packages/cannoli-core/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { CannoliVertex } from "./graph/objects/CannoliVertex";
import { CannoliGroup } from "./graph/objects/vertices/CannoliGroup";
import { CannoliNode } from "./graph/objects/vertices/CannoliNode";
import { parseNamedNode } from "./utility";
import { resultsRun } from "./cannoli";

export interface HttpTemplate {
id: string;
Expand Down Expand Up @@ -150,6 +151,8 @@ export class Run {
currentNote: string | null = null;
selection: string | null = null;

subcannoliCallback: (cannoli: unknown, inputVariables: Record<string, string>, scIsMock: boolean) => Promise<Record<string, string>>;

usage: Record<string, ModelUsage>;

forEachTracker: Map<string, number> = new Map();
Expand Down Expand Up @@ -251,7 +254,8 @@ export class Run {
const { name } = parseNamedNode(argNode.cannoliData.text);
argNode.cannoliData.text = `[${name}]\n${value}`;
});
} else {
}
else {
throw new Error(`Argument key "${key}" not found in input nodes.`);
}
}
Expand All @@ -267,6 +271,22 @@ export class Run {
for (const object of Object.values(this.graph)) {
object.setRun(this);
}

this.subcannoliCallback = (cannoli: unknown, inputVariables: Record<string, string>, scIsMock: boolean) => {
return resultsRun({
cannoli,
llmConfigs,
args: { ...inputVariables, obsidianCurrentNote: this.currentNote ?? "", obsidianSelection: this.selection ?? "" },
fileManager,
actions,
httpTemplates,
isMock: scIsMock,
config,
secrets,
fetcher,
replacers
});
};
}

async start() {
Expand Down
2 changes: 1 addition & 1 deletion packages/cannoli-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cannoli-plugin",
"version": "2.1.1",
"version": "2.1.2",
"description": "Create LLM Chat flows using the Obsidian Canvas.",
"private": true,
"scripts": {
Expand Down
15 changes: 15 additions & 0 deletions packages/cannoli-plugin/src/vault_interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ export class VaultInterface implements FileManager {
return await this.cannoli.app.vault.readBinary(file);
}

async getCanvas(fileName: string, isMock: boolean): Promise<string | null> {
const file = this.cannoli.app.metadataCache.getFirstLinkpathDest(
fileName,
""
);

if (!file) {
return null;
}

const content = await this.cannoli.app.vault.read(file);

return content;
}

async getNote(
reference: Reference,
isMock: boolean,
Expand Down

0 comments on commit 0d9976e

Please sign in to comment.