@littlebigbrain/create 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +55 -0
- package/README.md +71 -2
- package/bin/create-littlebigbrain.mjs +9 -0
- package/lib/create.mjs +238 -0
- package/package.json +46 -4
- package/project/setup.ts +18 -0
- package/project/try.ts +30 -0
- package/project/worker.ts +23 -0
- package/templates/_shared/testing.ts +490 -0
- package/templates/documents-link/README.md +144 -0
- package/templates/documents-link/link.ts +580 -0
- package/templates/documents-link/samples/ausstattungsliste.pdf +76 -0
- package/templates/documents-link/samples/baubeschreibung.pdf +54 -0
- package/templates/documents-link/samples/expected.json +404 -0
- package/templates/documents-link/samples/pages/ausstattungsliste.json +1150 -0
- package/templates/documents-link/samples/pages/baubeschreibung.json +802 -0
- package/templates/documents-link/samples/pages/raumblaetter.json +1580 -0
- package/templates/documents-link/samples/raumblaetter.pdf +109 -0
- package/templates/documents-link/samples.ts +31 -0
- package/templates/documents-link/workflow.test.ts +470 -0
- package/templates/documents-link/workflow.ts +457 -0
- package/templates/ontology-fit/README.md +81 -0
- package/templates/ontology-fit/samples/expected.md +88 -0
- package/templates/ontology-fit/samples/texts.json +22 -0
- package/templates/ontology-fit/samples.ts +40 -0
- package/templates/ontology-fit/workflow.test.ts +264 -0
- package/templates/ontology-fit/workflow.ts +413 -0
- package/templates/values-extract/README.md +81 -0
- package/templates/values-extract/samples/expected.json +148 -0
- package/templates/values-extract/samples/records.json +26 -0
- package/templates/values-extract/samples.ts +27 -0
- package/templates/values-extract/workflow.test.ts +256 -0
- package/templates/values-extract/workflow.ts +411 -0
- package/templates/versions.json +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
|
|
5
|
+
http://www.apache.org/licenses/
|
|
6
|
+
|
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
1. Definitions.
|
|
10
|
+
|
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
16
|
+
|
|
17
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
18
|
+
|
|
19
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
20
|
+
|
|
21
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
22
|
+
|
|
23
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
24
|
+
|
|
25
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
26
|
+
|
|
27
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
28
|
+
|
|
29
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
30
|
+
|
|
31
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
32
|
+
|
|
33
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
34
|
+
|
|
35
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
36
|
+
|
|
37
|
+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
38
|
+
|
|
39
|
+
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
40
|
+
|
|
41
|
+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
42
|
+
|
|
43
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
44
|
+
|
|
45
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
46
|
+
|
|
47
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
48
|
+
|
|
49
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
50
|
+
|
|
51
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
52
|
+
|
|
53
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
54
|
+
|
|
55
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
CHANGED
|
@@ -1,3 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @littlebigbrain/create
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Creates a project from a little big brain starter workflow template: a
|
|
4
|
+
workflow that your `WorkflowWorker` runs on a graph, with its test, its
|
|
5
|
+
samples and the scripts that run it.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm create @littlebigbrain -- --template documents-link my-links
|
|
9
|
+
cd my-links
|
|
10
|
+
npm install
|
|
11
|
+
npm test
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Templates
|
|
15
|
+
|
|
16
|
+
| Template | Does |
|
|
17
|
+
| --- | --- |
|
|
18
|
+
| `documents-link` | Finds the identifiers the documents define and the spans that mention them, and links each mention to its entry |
|
|
19
|
+
| `values-extract` | Fills properties of a class from the text of its instances, each value with a verbatim quote |
|
|
20
|
+
| `ontology-fit` | Proposes the ontology changes the texts of a class support, as review items |
|
|
21
|
+
|
|
22
|
+
A template id is the id of its starter workflow, with dashes for dots:
|
|
23
|
+
`documents-link` is the code of `documents.link`.
|
|
24
|
+
|
|
25
|
+
## The project
|
|
26
|
+
|
|
27
|
+
| File | Holds |
|
|
28
|
+
| --- | --- |
|
|
29
|
+
| `src/workflow.ts` | The workflow, its settings, the triggered workflow that sends it the instances (`trigger()`) and the ontology it writes (`ontology()`) |
|
|
30
|
+
| `src/*.ts` | The template's helpers and `samples.ts`, which puts the samples on a graph |
|
|
31
|
+
| `src/workflow.test.ts`, `src/testing.ts` | The workflow on its samples, with mock models and no server |
|
|
32
|
+
| `src/worker.ts`, `src/setup.ts`, `src/try.ts` | `npm start`, `npm run setup` and `npm run try` |
|
|
33
|
+
| `samples/` | The samples and their answer key |
|
|
34
|
+
|
|
35
|
+
`npm run try` creates a graph (`LBB_GRAPH`, default `try-<template>`), puts
|
|
36
|
+
the samples on it and creates the workflow there. `npm run setup` creates the
|
|
37
|
+
workflow on your own graph, and `npm start` serves it. All three read
|
|
38
|
+
`LBB_URL` and `LBB_API_KEY`.
|
|
39
|
+
|
|
40
|
+
## Options
|
|
41
|
+
|
|
42
|
+
| Option | Meaning |
|
|
43
|
+
| --- | --- |
|
|
44
|
+
| `--template <name>` | The template (required) |
|
|
45
|
+
| `--client <spec>` | The `@littlebigbrain/client` dependency of the project. Default: the version the templates are written for |
|
|
46
|
+
| `[dir]` | The new project's directory. Default: the template's name. It must be empty or missing |
|
|
47
|
+
|
|
48
|
+
## Development
|
|
49
|
+
|
|
50
|
+
The package is developed in the little big brain monorepo, and the templates
|
|
51
|
+
live in its `sdk/templates`. There the command reads them from that folder.
|
|
52
|
+
`npm pack` copies them into the package (`prepack`) with the client version,
|
|
53
|
+
and removes the copy afterwards.
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
node sdk/create/bin/create-littlebigbrain.mjs --template documents-link /tmp/my-links
|
|
57
|
+
npm test -w @littlebigbrain/create
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The test creates each project, compiles it against the workspace's client
|
|
61
|
+
and runs its test.
|
|
62
|
+
|
|
63
|
+
The public repository
|
|
64
|
+
[littlebigbrains/lbb-typescript](https://github.com/littlebigbrains/lbb-typescript)
|
|
65
|
+
holds the package in `create/`, with the templates and
|
|
66
|
+
`templates/versions.json` already copied in. There `prepack` keeps that copy.
|
|
67
|
+
A new version in `create/package.json` is published to npm by the
|
|
68
|
+
repository's `release-create.yml` workflow.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
Apache-2.0
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// @ts-check
|
|
3
|
+
// npm create @littlebigbrain -- --template documents-link [dir]
|
|
4
|
+
import { main } from "../lib/create.mjs";
|
|
5
|
+
|
|
6
|
+
main(process.argv.slice(2)).catch((error) => {
|
|
7
|
+
console.error(`create-littlebigbrain: ${error instanceof Error ? error.message : error}`);
|
|
8
|
+
process.exitCode = 1;
|
|
9
|
+
});
|
package/lib/create.mjs
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Create a project from a starter workflow template:
|
|
3
|
+
//
|
|
4
|
+
// npm create @littlebigbrain -- --template documents-link [dir]
|
|
5
|
+
//
|
|
6
|
+
// The project holds the template's workflow, helpers, test and samples, and
|
|
7
|
+
// the scripts that run it: `npm test` (the workflow on its samples, with mock
|
|
8
|
+
// models), `npm run try` (the samples on a graph of your stack), `npm run
|
|
9
|
+
// setup` and `npm start` (your graph). The templates come from `templates/`
|
|
10
|
+
// in the package (copied there when it is packed) or, in the repository,
|
|
11
|
+
// from `sdk/templates`.
|
|
12
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
13
|
+
import { cp, mkdir, readdir, readFile, writeFile } from "node:fs/promises";
|
|
14
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
|
|
17
|
+
/** The templates a project can start from: the folder names under `sdk/templates`. */
|
|
18
|
+
export const TEMPLATES = ["documents-link", "values-extract", "ontology-fit"];
|
|
19
|
+
|
|
20
|
+
const PACKAGE = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
21
|
+
|
|
22
|
+
const USAGE = `Create a project from a little big brain starter workflow template.
|
|
23
|
+
|
|
24
|
+
Usage:
|
|
25
|
+
npm create @littlebigbrain -- --template <name> [dir]
|
|
26
|
+
|
|
27
|
+
Templates: ${TEMPLATES.join(", ")}
|
|
28
|
+
|
|
29
|
+
Options:
|
|
30
|
+
--template <name> the template (required)
|
|
31
|
+
--client <spec> the @littlebigbrain/client dependency (default: ^<the current version>)
|
|
32
|
+
--help this text`;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The options of the command line.
|
|
36
|
+
* @param {string[]} argv
|
|
37
|
+
* @returns {{ template?: string, dir?: string, client?: string, help: boolean }}
|
|
38
|
+
*/
|
|
39
|
+
export function parseArgs(argv) {
|
|
40
|
+
/** @type {{ template?: string, dir?: string, client?: string, help: boolean }} */
|
|
41
|
+
const out = { help: false };
|
|
42
|
+
for (let i = 0; i < argv.length; i++) {
|
|
43
|
+
const arg = argv[i];
|
|
44
|
+
const [flag, inline] = arg.startsWith("--") ? arg.split(/=(.*)/s, 2) : [arg, undefined];
|
|
45
|
+
const value = () => {
|
|
46
|
+
const next = inline ?? argv[++i];
|
|
47
|
+
if (next === undefined || next.startsWith("--")) throw new Error(`${flag} needs a value`);
|
|
48
|
+
return next;
|
|
49
|
+
};
|
|
50
|
+
if (flag === "--help" || flag === "-h") out.help = true;
|
|
51
|
+
else if (flag === "--template" || flag === "-t") out.template = value();
|
|
52
|
+
else if (flag === "--client") out.client = value();
|
|
53
|
+
else if (flag.startsWith("-")) throw new Error(`unknown option ${flag}`);
|
|
54
|
+
else if (out.dir === undefined) out.dir = arg;
|
|
55
|
+
else throw new Error(`one directory, not ${out.dir} and ${arg}`);
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The folder that holds the templates: the package's own copy, else the
|
|
62
|
+
* repository's `sdk/templates`.
|
|
63
|
+
* @param {string} [root] the package folder
|
|
64
|
+
*/
|
|
65
|
+
export function templatesRoot(root = PACKAGE) {
|
|
66
|
+
for (const candidate of [join(root, "templates"), join(root, "..", "templates")])
|
|
67
|
+
if (existsSync(join(candidate, "_shared", "testing.ts"))) return candidate;
|
|
68
|
+
throw new Error(`no templates in ${root}/templates or ${root}/../templates`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The version of @littlebigbrain/client the templates are written for.
|
|
73
|
+
* @param {string} templates the templates folder
|
|
74
|
+
*/
|
|
75
|
+
export function clientVersion(templates) {
|
|
76
|
+
const packed = join(templates, "versions.json");
|
|
77
|
+
if (existsSync(packed)) return JSON.parse(readFileSync(packed, "utf8")).client;
|
|
78
|
+
const repo = join(templates, "..", "typescript", "package.json");
|
|
79
|
+
return JSON.parse(readFileSync(repo, "utf8")).version;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A package name from a directory name. @param {string} dir */
|
|
83
|
+
const packageName = (dir) =>
|
|
84
|
+
basename(resolve(dir))
|
|
85
|
+
.toLowerCase()
|
|
86
|
+
.replace(/[^a-z0-9-._~]+/g, "-")
|
|
87
|
+
.replace(/^[-._]+|[-._]+$/g, "") || "workflow";
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A template's README as a section of the project's: no title, its headings
|
|
91
|
+
* one level down, and its links into the repository as plain text.
|
|
92
|
+
* @param {string} text
|
|
93
|
+
*/
|
|
94
|
+
const projectReadme = (text) =>
|
|
95
|
+
text
|
|
96
|
+
.replace(/\[([^\]]+)\]\((?:\.\.\/)+[^)]*\)/g, "$1")
|
|
97
|
+
.replace(/^# .*\n/, "")
|
|
98
|
+
.replace(/^(#{2,}) /gm, "#$1 ");
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Create the project of `template` in `dir`. Returns the files written,
|
|
102
|
+
* relative to `dir`, sorted.
|
|
103
|
+
* @param {{ template: string, dir: string, client?: string, root?: string }} options
|
|
104
|
+
* @returns {Promise<string[]>}
|
|
105
|
+
*/
|
|
106
|
+
export async function create({ template, dir, client, root = PACKAGE }) {
|
|
107
|
+
if (!TEMPLATES.includes(template))
|
|
108
|
+
throw new Error(`unknown template ${template}; the templates are ${TEMPLATES.join(", ")}`);
|
|
109
|
+
if (existsSync(dir) && (await readdir(dir)).length)
|
|
110
|
+
throw new Error(`${dir} is not empty: name a new directory`);
|
|
111
|
+
const templates = templatesRoot(root);
|
|
112
|
+
const from = join(templates, template);
|
|
113
|
+
const src = join(dir, "src");
|
|
114
|
+
await mkdir(src, { recursive: true });
|
|
115
|
+
|
|
116
|
+
// The template's code, its test and the test support, all under src/.
|
|
117
|
+
for (const file of await readdir(from)) {
|
|
118
|
+
if (!file.endsWith(".ts")) continue;
|
|
119
|
+
const text = await readFile(join(from, file), "utf8");
|
|
120
|
+
await writeFile(join(src, file), text.replaceAll("../_shared/testing.js", "./testing.js"));
|
|
121
|
+
}
|
|
122
|
+
await cp(join(templates, "_shared", "testing.ts"), join(src, "testing.ts"));
|
|
123
|
+
await cp(join(from, "samples"), join(dir, "samples"), { recursive: true });
|
|
124
|
+
for (const file of ["worker.ts", "setup.ts", "try.ts"]) {
|
|
125
|
+
const text = await readFile(join(root, "project", file), "utf8");
|
|
126
|
+
await writeFile(join(src, file), text.replaceAll("__TEMPLATE__", template));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const name = packageName(dir);
|
|
130
|
+
const pkg = {
|
|
131
|
+
name,
|
|
132
|
+
version: "0.1.0",
|
|
133
|
+
private: true,
|
|
134
|
+
type: "module",
|
|
135
|
+
scripts: {
|
|
136
|
+
start: "tsx src/worker.ts",
|
|
137
|
+
setup: "tsx src/setup.ts",
|
|
138
|
+
try: "tsx src/try.ts",
|
|
139
|
+
test: "tsx --test src/workflow.test.ts",
|
|
140
|
+
typecheck: "tsc --noEmit",
|
|
141
|
+
},
|
|
142
|
+
dependencies: { "@littlebigbrain/client": client ?? `^${clientVersion(templates)}` },
|
|
143
|
+
devDependencies: { "@types/node": "^22", tsx: "^4", typescript: "^5.6" },
|
|
144
|
+
};
|
|
145
|
+
await writeFile(join(dir, "package.json"), `${JSON.stringify(pkg, null, 2)}\n`);
|
|
146
|
+
const tsconfig = {
|
|
147
|
+
compilerOptions: {
|
|
148
|
+
target: "ES2022",
|
|
149
|
+
module: "NodeNext",
|
|
150
|
+
moduleResolution: "NodeNext",
|
|
151
|
+
lib: ["ES2022"],
|
|
152
|
+
strict: true,
|
|
153
|
+
noUncheckedIndexedAccess: true,
|
|
154
|
+
skipLibCheck: true,
|
|
155
|
+
types: ["node"],
|
|
156
|
+
noEmit: true,
|
|
157
|
+
},
|
|
158
|
+
include: ["src"],
|
|
159
|
+
};
|
|
160
|
+
await writeFile(join(dir, "tsconfig.json"), `${JSON.stringify(tsconfig, null, 2)}\n`);
|
|
161
|
+
await writeFile(join(dir, ".gitignore"), "node_modules/\ndist/\n.env\n");
|
|
162
|
+
|
|
163
|
+
const readme = await readFile(join(from, "README.md"), "utf8");
|
|
164
|
+
await writeFile(
|
|
165
|
+
join(dir, "README.md"),
|
|
166
|
+
`# ${name}
|
|
167
|
+
|
|
168
|
+
The \`${template}\` starter workflow template of little big brain, as a
|
|
169
|
+
project of its own: a workflow that your \`WorkflowWorker\` runs on a graph.
|
|
170
|
+
|
|
171
|
+
## Commands
|
|
172
|
+
|
|
173
|
+
\`\`\`sh
|
|
174
|
+
npm install
|
|
175
|
+
npm test # the workflow on its samples, with mock models: no server
|
|
176
|
+
npm run try # the samples on a graph of your stack, and the workflow there
|
|
177
|
+
npm run setup && npm start # your graph: create the workflow, then serve it
|
|
178
|
+
\`\`\`
|
|
179
|
+
|
|
180
|
+
\`npm run try\`, \`npm run setup\` and \`npm start\` read \`LBB_URL\` (your
|
|
181
|
+
stack's endpoint), \`LBB_API_KEY\` (a key of the stack) and \`LBB_GRAPH\` (the
|
|
182
|
+
graph; \`npm run try\` creates it, default \`try-${template}\`). After
|
|
183
|
+
\`npm run try\`, serve the workflow with \`LBB_GRAPH=try-${template} npm start\`.
|
|
184
|
+
The server needs the workflow engine, and the models need a plan with managed
|
|
185
|
+
models.
|
|
186
|
+
|
|
187
|
+
## Files
|
|
188
|
+
|
|
189
|
+
- \`src/workflow.ts\`: the workflow, its settings (\`defaults\`), the triggered
|
|
190
|
+
workflow that sends it the instances (\`trigger()\`) and the ontology it
|
|
191
|
+
writes (\`ontology()\`). Change it here.
|
|
192
|
+
- \`src/workflow.test.ts\` and \`src/testing.ts\`: the test, and the mock of
|
|
193
|
+
the activities and the workflow engine it runs on.
|
|
194
|
+
- \`src/samples.ts\` and \`samples/\`: the samples and what \`npm run try\` does
|
|
195
|
+
with them.
|
|
196
|
+
- \`src/worker.ts\`, \`src/setup.ts\`, \`src/try.ts\`: the three commands.
|
|
197
|
+
|
|
198
|
+
## The template
|
|
199
|
+
|
|
200
|
+
${projectReadme(readme).trim()}
|
|
201
|
+
`,
|
|
202
|
+
);
|
|
203
|
+
return listFiles(dir);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Every file under `dir`, relative and sorted. @param {string} dir @returns {Promise<string[]>} */
|
|
207
|
+
async function listFiles(dir, prefix = "") {
|
|
208
|
+
/** @type {string[]} */
|
|
209
|
+
const out = [];
|
|
210
|
+
for (const entry of await readdir(join(dir, prefix), { withFileTypes: true })) {
|
|
211
|
+
const path = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
212
|
+
if (entry.isDirectory()) out.push(...(await listFiles(dir, path)));
|
|
213
|
+
else out.push(path);
|
|
214
|
+
}
|
|
215
|
+
return out.sort();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The command: parse the arguments, create the project, print the next steps.
|
|
220
|
+
* @param {string[]} argv
|
|
221
|
+
* @param {{ log?: (line: string) => void }} [io]
|
|
222
|
+
*/
|
|
223
|
+
export async function main(argv, io = {}) {
|
|
224
|
+
const log = io.log ?? ((line) => console.log(line));
|
|
225
|
+
const options = parseArgs(argv);
|
|
226
|
+
if (options.help || !options.template) {
|
|
227
|
+
log(USAGE);
|
|
228
|
+
if (!options.help) throw new Error("--template is required");
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const dir = options.dir ?? options.template;
|
|
232
|
+
const files = await create({ template: options.template, dir, client: options.client });
|
|
233
|
+
log(`Created ${dir} from the ${options.template} template (${files.length} files). Next:`);
|
|
234
|
+
log(` cd ${dir}`);
|
|
235
|
+
log(" npm install");
|
|
236
|
+
log(" npm test");
|
|
237
|
+
log(` LBB_URL=... LBB_API_KEY=... LBB_GRAPH=try-${options.template} npm run try`);
|
|
238
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@littlebigbrain/create",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Create a project from a little big brain starter workflow template: npm create @littlebigbrain",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/littlebigbrains/lbb-typescript.git",
|
|
9
|
+
"directory": "create"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://docs.littlebigbrain.com/guides/workflow-templates/",
|
|
12
|
+
"bugs": "https://github.com/littlebigbrains/lbb-typescript/issues",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"little-big-brain",
|
|
15
|
+
"workflow",
|
|
16
|
+
"template",
|
|
17
|
+
"create"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"bin": {
|
|
21
|
+
"create-littlebigbrain": "bin/create-littlebigbrain.mjs"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"bin",
|
|
25
|
+
"lib",
|
|
26
|
+
"project",
|
|
27
|
+
"templates",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=18"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"prepack": "node scripts/copy-templates.mjs",
|
|
39
|
+
"postpack": "node scripts/copy-templates.mjs --clean",
|
|
40
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
41
|
+
"test": "node --test \"test/*.test.mjs\"",
|
|
42
|
+
"test:coverage": "npm test"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^22",
|
|
46
|
+
"typescript": "^5.6"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/project/setup.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Create the workflow on your graph: add the classes and properties it writes, then create the
|
|
2
|
+
// triggered workflow that sends it the instances of the class it watches. Each call is safe to
|
|
3
|
+
// repeat.
|
|
4
|
+
//
|
|
5
|
+
// LBB_URL=... LBB_API_KEY=... LBB_GRAPH=... npm run setup
|
|
6
|
+
import { LbbClient, lbbActivities } from "@littlebigbrain/client";
|
|
7
|
+
import { ontology, trigger } from "./workflow.js";
|
|
8
|
+
|
|
9
|
+
const graph = process.env.LBB_GRAPH ?? "main";
|
|
10
|
+
const client = new LbbClient({
|
|
11
|
+
baseUrl: process.env.LBB_URL ?? "http://127.0.0.1:7400",
|
|
12
|
+
apiKey: process.env.LBB_API_KEY,
|
|
13
|
+
graph,
|
|
14
|
+
});
|
|
15
|
+
await lbbActivities(client).evolve(ontology());
|
|
16
|
+
const created = await client.workflows.starters.use(trigger());
|
|
17
|
+
console.log(`Created ${created.name} on the graph ${graph}: ${created.status.state}.`);
|
|
18
|
+
console.log(`Serve it with LBB_GRAPH=${graph} npm start.`);
|
package/project/try.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Try the workflow on its samples: create a graph (default try-__TEMPLATE__), put the samples on
|
|
2
|
+
// it, and create the workflow there. Then serve it with npm start and the same LBB_GRAPH.
|
|
3
|
+
//
|
|
4
|
+
// LBB_URL=... LBB_API_KEY=... LBB_GRAPH=try-__TEMPLATE__ npm run try
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { LbbClient, LbbError, lbbActivities } from "@littlebigbrain/client";
|
|
7
|
+
import { loadSamples } from "./samples.js";
|
|
8
|
+
import { ontology, trigger } from "./workflow.js";
|
|
9
|
+
|
|
10
|
+
const graph = process.env.LBB_GRAPH ?? "try-__TEMPLATE__";
|
|
11
|
+
const client = new LbbClient({
|
|
12
|
+
baseUrl: process.env.LBB_URL ?? "http://127.0.0.1:7400",
|
|
13
|
+
apiKey: process.env.LBB_API_KEY,
|
|
14
|
+
graph,
|
|
15
|
+
});
|
|
16
|
+
try {
|
|
17
|
+
await client.graph(graph).create();
|
|
18
|
+
console.log(`Created the graph ${graph}`);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
// 409: the graph exists already.
|
|
21
|
+
if (!(error instanceof LbbError && error.status === 409)) throw error;
|
|
22
|
+
}
|
|
23
|
+
await lbbActivities(client).evolve(ontology());
|
|
24
|
+
await loadSamples(client, fileURLToPath(new URL("../samples/", import.meta.url)));
|
|
25
|
+
const created = await client.workflows.starters.use(trigger());
|
|
26
|
+
console.log(`Created ${created.name} on the graph ${graph}: ${created.status.state}.`);
|
|
27
|
+
console.log(`Serve it with LBB_GRAPH=${graph} npm start, and follow it with:`);
|
|
28
|
+
console.log(
|
|
29
|
+
` curl "$LBB_URL/v1/workflows/triggered?graph=${graph}&starter=workflow&name=${created.name}" -H "Authorization: Bearer $LBB_API_KEY"`,
|
|
30
|
+
);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Serve the workflow: the worker claims the turns of its trigger messages and runs them, one at a
|
|
2
|
+
// time. Run more workers for more at once.
|
|
3
|
+
//
|
|
4
|
+
// LBB_URL=... LBB_API_KEY=... LBB_GRAPH=... npm start
|
|
5
|
+
import { LbbClient, WorkflowWorker, lbbActivities } from "@littlebigbrain/client";
|
|
6
|
+
import { createWorkflow } from "./workflow.js";
|
|
7
|
+
|
|
8
|
+
const graph = process.env.LBB_GRAPH ?? "main";
|
|
9
|
+
const client = new LbbClient({
|
|
10
|
+
baseUrl: process.env.LBB_URL ?? "http://127.0.0.1:7400",
|
|
11
|
+
apiKey: process.env.LBB_API_KEY,
|
|
12
|
+
graph,
|
|
13
|
+
});
|
|
14
|
+
const workflow = createWorkflow(lbbActivities(client));
|
|
15
|
+
const stop = new AbortController();
|
|
16
|
+
process.once("SIGINT", () => stop.abort());
|
|
17
|
+
process.once("SIGTERM", () => stop.abort());
|
|
18
|
+
console.log(
|
|
19
|
+
`Serving ${workflow.name}@${workflow.version} for the graph ${graph}. Stop with Ctrl-C.`,
|
|
20
|
+
);
|
|
21
|
+
await new WorkflowWorker(client, [workflow], {
|
|
22
|
+
worker: process.env.LBB_WORKER_ID ?? `${workflow.name}-1`,
|
|
23
|
+
}).run({ signal: stop.signal });
|