This commit is contained in:
Taylor Hulsmans 2023-10-23 15:54:41 -04:00
parent 36dbeac9fc
commit 62eb71f696
7 changed files with 345 additions and 247 deletions

67
desci-nodes/getDpid.ts Normal file
View file

@ -0,0 +1,67 @@
import { requestUrl} from 'obsidian'
import type ObsidianDecsiPlugin from '../main'
import {
createNode,
placeholderNoteHeight,
assistantColor,
getNodeText
} from '../utils/canvas-util'
export const getDpid = async function () {
console.log('this', this)
if (this.unloaded) return
this.logDebug("fetching dpid json from desci nodes")
const canvas = this.getActiveCanvas()
if (!canvas) {
this.logDebug('No active canvas')
return
}
const selection = canvas.selection
if (selection?.size !== 1) return
const values = Array.from(selection.values())
const node = values[0]
if (node) {
await canvas.requestSave()
await sleep(200)
const settings = this.settings
//const nodeData = node.getData()
let nodeText = await getNodeText(node) || ''
if (nodeText.length == 0) {
console.log('no node text')
this.logDebug('no node Text found')
return
}
const created = createNode(canvas, node,
{
text: `fetching dpid: ${nodeText}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
try {
const json = await requestUrl(`http://beta.dpid.org/${nodeText}?jsonld`)
console.log('json: ', json)
const ipfsFetchNode = createNode(canvas, created,
{
text: `${JSON.stringify(json)}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
} catch (e) {
created.setText(`error :( ${e}`)
this.logDebug(`error :( : ${e}`)
return
}
}
}

78
ipfs/dagGet.ts Normal file
View file

@ -0,0 +1,78 @@
import { requestUrl } from 'obsidian'
import {
createNode,
placeholderNoteHeight,
assistantColor,
getNodeText
} from '../utils/canvas-util'
export const dagGet = async function () {
if (this.unloaded) return
const canvas = this.getActiveCanvas()
if (!canvas) {
this.logDebug('No active canvas')
return
}
const selection = canvas.selection
if (selection?.size !== 1) return
const values = Array.from(selection.values())
const node = values[0]
if (node) {
await canvas.requestSave()
await sleep(200)
const settings = this.settings
const nodeData = node.getData()
let nodeText = await getNodeText(node) || ''
if (nodeText.length == 0) {
this.logDebug('no node Text found')
return
}
const created = createNode(canvas, node,
{
text: `attempting to fetch ${nodeText} from ipfs`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
try {
console.log('waiting for ipfs. . .')
let res = await requestUrl(`http://localhost:3000/dag?cid=${nodeText}`)
console.log('res', res)
res.json.Links.forEach((link: any) => {
const name = createNode(canvas, created,
{
text: `${link.Name}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
createNode(canvas, name,
{
text: `${link.Hash['/']}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
})
created.setText('success~')
} catch (e) {
this.logDebug(e)
console.log('ipfs fetch error: ', e)
created.setText(`error :( : ${e}`)
return
}
}
}

89
ipfs/kuboFetch.ts Normal file
View file

@ -0,0 +1,89 @@
import { requestUrl } from 'obsidian'
import {
createNode,
placeholderNoteHeight,
assistantColor,
getNodeText
} from '../utils/canvas-util'
export const kuboFetch = async function () {
if (this.unloaded) return
this.logDebug("attempting to fetch from kubo node")
const canvas = this.getActiveCanvas()
if (!canvas) {
this.logDebug('No active canvas')
return
}
const selection = canvas.selection
if (selection?.size !== 1) return
const values = Array.from(selection.values())
const node = values[0]
if (node) {
await canvas.requestSave()
await sleep(200)
const settings = this.settings
const nodeData = node.getData()
let nodeText = await getNodeText(node) || ''
if (nodeText.length == 0) {
this.logDebug('no node Text found')
return
}
const created = createNode(canvas, node,
{
text: `attempting to fetch ${nodeText} from ipfs`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
try {
const res = await requestUrl({
url: `http://localhost:3000/gateway?cid=${nodeText}`,
method: 'POST'
})
console.log('res', res)
function _arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
const cidNode = createNode(canvas, created,
{
text: `<img src="data:image/png;base64,${_arrayBufferToBase64(res.arrayBuffer)}" />`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
} catch (e) {
const cideNodeError = createNode(canvas, created,
{
text: `error at ${e}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
}
}
}

83
lilypad/runSdxl.ts Normal file
View file

@ -0,0 +1,83 @@
import {
createNode,
placeholderNoteHeight,
assistantColor,
getNodeText
} from '../utils/canvas-util'
import { ethers } from 'ethers'
export const runSdxl = async function () {
if (this.unloaded) return
this.logDebug("Running sdxl")
const canvas = this.getActiveCanvas()
if (!canvas) {
this.logDebug('No active canvas')
return
}
const selection = canvas.selection
if (selection?.size !== 1) return
const values = Array.from(selection.values())
const node = values[0]
if (node) {
await canvas.requestSave()
await sleep(200)
const settings = this.settings
//const nodeData = node.getData()
let nodeText = await getNodeText(node) || ''
if (nodeText.length == 0) {
console.log('no node text')
this.logDebug('no node Text found')
return
}
const created = createNode(canvas, node,
{
text: `Calling Lilpad sdxl with ${nodeText}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
try {
console.log('waiting for sdxl run')
console.log('signer', this.signer)
const tx = await this.exampleClient.runSDXL(
nodeText, {
value: ethers.parseUnits('4', 'ether')
}
)
const receipt = await tx.wait()
console.log('receipt', receipt)
console.log('tx', tx)
created.setText(`success! tx hash: ${tx.hash}, listening for job completion`)
this.exampleClient.on("ReceivedJobResults", (jobId, cid) => {
//const res = await this.exampleClient.fetchAllResults()
//console.log('res', res)
//const ipfsio = res[res.length -1][2]
const ipfsFetchNode = createNode(canvas, created,
{
text: `${cid}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
})
/*
*/
} catch (e) {
created.setText(`error :( ${e}`)
this.logDebug(`error :( : ${e}`)
return
}
}
}

254
main.tsx
View file

@ -5,8 +5,12 @@ import {
createNode,
placeholderNoteHeight,
assistantColor,
getNodeText
} from './utils/canvas-util'
import { getDpid} from './desci-nodes/getDpid'
import { runSdxl} from './lilypad/runSdxl'
import {dagGet} from './ipfs/dagGet'
import {kuboFetch} from './ipfs/kuboFetch'
import { ethers, Signer, Provider, JsonRpcProvider, Wallet } from 'ethers';
import ExampleClient from './artifacts/ExampleClient.json'
@ -48,16 +52,12 @@ export default class ObsidianDesci extends Plugin {
this.addCommand({
id: 'runSDXL',
name: 'runSDXL - execute stable diffusion on a text node',
callback: () => {
this.runSDXL()
}
callback: runSdxl.bind(this)
});
this.addCommand({
id: 'getDpid',
name: 'getDpid - retreive the json of a desci nodes research object',
callback: () => {
this.getDpid()
}
callback: getDpid.bind(this)
});
this.addCommand({
id: 'ipfsCat',
@ -69,9 +69,7 @@ export default class ObsidianDesci extends Plugin {
this.addCommand({
id: 'ipfsDagGet',
name: 'ipfsDagGet - fetch json behind a persistent identifer',
callback: () => {
this.ipfsDagGet()
}
callback: dagGet.bind(this)
});
this.addCommand({
id: 'ipfsAdd',
@ -83,9 +81,7 @@ export default class ObsidianDesci extends Plugin {
this.addCommand({
id: 'ipfsKuboFetch',
name: 'ipfsKuboFetch - fetch a Cid from a Kubo node ',
callback: () => {
this.ipfsKuboFetch()
}
callback: kuboFetch.bind(this)
});
// This creates an icon in the left ribbon.
@ -121,142 +117,6 @@ export default class ObsidianDesci extends Plugin {
onunload() {
}
async getDpid() {
if (this.unloaded) return
this.logDebug("fetching dpid json from desci nodes")
const canvas = this.getActiveCanvas()
if (!canvas) {
this.logDebug('No active canvas')
return
}
const selection = canvas.selection
if (selection?.size !== 1) return
const values = Array.from(selection.values())
const node = values[0]
if (node) {
await canvas.requestSave()
await sleep(200)
const settings = this.settings
//const nodeData = node.getData()
let nodeText = await getNodeText(node) || ''
if (nodeText.length == 0) {
console.log('no node text')
this.logDebug('no node Text found')
return
}
const created = createNode(canvas, node,
{
text: `fetching dpid: ${nodeText}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
try {
const json = await requestUrl(`http://beta.dpid.org/${nodeText}?jsonld`)
console.log('json: ', json)
const ipfsFetchNode = createNode(canvas, created,
{
text: `${JSON.stringify(json)}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
} catch (e) {
created.setText(`error :( ${e}`)
this.logDebug(`error :( : ${e}`)
return
}
}
}
async runSDXL() {
if (this.unloaded) return
this.logDebug("Running sdxl")
const canvas = this.getActiveCanvas()
if (!canvas) {
this.logDebug('No active canvas')
return
}
const selection = canvas.selection
if (selection?.size !== 1) return
const values = Array.from(selection.values())
const node = values[0]
if (node) {
await canvas.requestSave()
await sleep(200)
const settings = this.settings
//const nodeData = node.getData()
let nodeText = await getNodeText(node) || ''
if (nodeText.length == 0) {
console.log('no node text')
this.logDebug('no node Text found')
return
}
const created = createNode(canvas, node,
{
text: `Calling Lilpad sdxl with ${nodeText}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
try {
console.log('waiting for sdxl run')
console.log('signer', this.signer)
const tx = await this.exampleClient.runSDXL(
nodeText, {
value: ethers.parseUnits('4', 'ether')
}
)
const receipt = await tx.wait()
console.log('receipt', receipt)
console.log('tx', tx)
created.setText(`success! tx hash: ${tx.hash}, listening for job completion`)
this.exampleClient.on("ReceivedJobResults", (jobId, cid) => {
//const res = await this.exampleClient.fetchAllResults()
//console.log('res', res)
//const ipfsio = res[res.length -1][2]
const ipfsFetchNode = createNode(canvas, created,
{
text: `${cid}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
})
/*
*/
} catch (e) {
created.setText(`error :( ${e}`)
this.logDebug(`error :( : ${e}`)
return
}
}
}
async cat() {
if (this.unloaded) return
@ -325,85 +185,6 @@ export default class ObsidianDesci extends Plugin {
}
}
}
async ipfsKuboFetch() {
if (this.unloaded) return
this.logDebug("attempting to fetch from kubo node")
const canvas = this.getActiveCanvas()
if (!canvas) {
this.logDebug('No active canvas')
return
}
const selection = canvas.selection
if (selection?.size !== 1) return
const values = Array.from(selection.values())
const node = values[0]
if (node) {
await canvas.requestSave()
await sleep(200)
const settings = this.settings
const nodeData = node.getData()
let nodeText = await getNodeText(node) || ''
if (nodeText.length == 0) {
this.logDebug('no node Text found')
return
}
const created = createNode(canvas, node,
{
text: `attempting to fetch ${nodeText} from ipfs`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
try {
const res = await requestUrl({
url: `http://localhost:3000/gateway?cid=${nodeText}`,
method: 'POST'
})
console.log('res', res)
function _arrayBufferToBase64( buffer ) {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}
const cidNode = createNode(canvas, created,
{
text: `<img src="data:image/png;base64,${_arrayBufferToBase64(res.arrayBuffer)}" />`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
} catch (e) {
const cideNodeError = createNode(canvas, created,
{
text: `error at ${e}`,
size: { height: placeholderNoteHeight }
},
{
color: assistantColor,
chat_role: 'assistant'
}
)
}
}
}
async ipfsAdd() {
if (this.unloaded) return
@ -650,20 +431,3 @@ export default class ObsidianDesci extends Plugin {
}
}
async function getNodeText(node: CanvasNode) {
const nodeData = node.getData()
switch (nodeData.type) {
case 'text':
return nodeData.text
case 'file':
return readFile(nodeData.file)
}
}
async function readFile(path: string) {
const file = this.app.vault.getAbstractFileByPath(path)
if (file instanceof TFile) {
const body = await app.vault.read(file)
return `## ${file.basename}\n${body}`
}
}

View file

@ -1,4 +1,4 @@
import { ItemView } from 'obsidian'
import { ItemView, TFile } from 'obsidian'
import { Canvas, CanvasNode, CreateNodeOptions } from './canvas-internal'
import { AllCanvasNodeData } from 'obsidian/canvas'
@ -164,4 +164,21 @@ export const createNode = (
})
return newNode
}
}
async function readFile(path: string) {
const file = this.app.vault.getAbstractFileByPath(path)
if (file instanceof TFile) {
const body = await app.vault.read(file)
return `## ${file.basename}\n${body}`
}
}
export async function getNodeText(node: CanvasNode) {
const nodeData = node.getData()
switch (nodeData.type) {
case 'text':
return nodeData.text
case 'file':
return readFile(nodeData.file)
}
}