Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
liamgriffiths committed Aug 2, 2024
1 parent cfa1dec commit 93f722d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
34 changes: 20 additions & 14 deletions examples/package.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env -S npx ts-node --transpileOnly

import { Substrate, Box, Package, sb } from "substrate";
import { Substrate, Box, Module, sb } from "substrate";

async function main() {
const SUBSTRATE_API_KEY = process.env["SUBSTRATE_API_KEY"];
Expand All @@ -17,38 +17,44 @@ async function main() {
const y = sb.input({ type: "string" });
const z = sb.input({ type: "object", properties: {} });

const a = new Box({ value: { a: x, z: z, liam: [x, x, x] } }, { id: "BoxA" });
const a = new Box({ value: { a: x, z: z, liam: [x, x, x] } }, { id: "A" });
const b = new Box(
{ value: { b: sb.interpolate`x=${a.future.value.get("a")}, y=${y}` } },
{ id: "BoxB" },
{ id: "B" },
);

const packageable = substrate.createPackageable([a, b], { x, y, z });
// console.log("packageable", JSON.stringify(packageable, null, 2));

const p = new Package(
// const publication = await substrate.module.publish({
// name: "my reusable graph",
// nodes: [a, b],
// inputs: { x, y, z },
// });

const p = new Module(
{
package_json: packageable.toJSON(),
json: substrate.module.serialize({
nodes: [a, b],
inputs: { x, y, z },
}),
inputs: {
// when commented will use "hello" because it is defined as the default above
// x: "xxx",
y: "yyy",
z: {
// string: "hi",
// obj: { a: 123 },
arr: ["123"],
},
},
}, { id: "Package" }
},
{ id: "Module" },
);

const c = new Box(
{
value: {
"1": p.future.get("BoxB.value.b"),
"2": p.future.get("BoxA.value.z.arr[0]"),
"1": p.future.get("A.value.z.arr[0]"),
"2": p.future.get("B.value.b"),
},
},
{ id: "BoxC" },
{ id: "C" },
);

const res = await substrate.run(p, c);
Expand Down
31 changes: 26 additions & 5 deletions src/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,35 @@ export function packager(substrate: Substrate) {
};
}

type PackageIn = {
package_json: any;
inputs: Record<string, any>;
};
type PackageIn =
| {
package_json: any;
inputs: Record<string, any>;
}
| {
module_json: any;
inputs: Record<string, any>;
}
| {
id: any;
inputs: Record<string, any>;
}
| {
uri: any;
inputs: Record<string, any>;
}
| {
json: any;

inputs: Record<string, any>;
};

export class Package extends Node {
constructor(args: PackageIn, options?: Options) {
super(args, options);
// @ts-ignore
let myargs = { package_json: args.json || args.package_json || args.module_json, inputs: args.inputs };

super(myargs, options);
this.node = "Package";
}
}
12 changes: 12 additions & 0 deletions src/Substrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,16 @@ export class Substrate {

return headers;
}

module = {
serialize: ({ nodes, inputs }: { nodes: Node[], inputs: any }) => {
let p = packager(this);
return p(nodes, inputs).toJSON();
},

publish: async (publishable: any) => {
console.log("not implemented yet");
return publishable;
}
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export {
DeleteVectors,
} from "substrate/Nodes";

export { Package } from "substrate/Package";
export { Package, Package as Module } from "substrate/Package";

export { sb } from "substrate/sb";
export { Substrate };
Expand Down

0 comments on commit 93f722d

Please sign in to comment.