@langchain/langgraph-cli 1.1.8 → 1.1.10
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/dist/cli/build.mjs +52 -0
- package/dist/cli/cli.mjs +13 -0
- package/dist/cli/cloudflare.mjs +172 -0
- package/dist/cli/dev.mjs +143 -0
- package/dist/cli/dev.python.mjs +129 -0
- package/dist/cli/docker.mjs +114 -0
- package/dist/cli/new.mjs +13 -0
- package/dist/cli/sysinfo.mjs +63 -0
- package/dist/cli/up.mjs +139 -0
- package/dist/cli/utils/analytics.mjs +39 -0
- package/dist/cli/utils/builder.mjs +7 -0
- package/dist/cli/utils/ipc/server.mjs +93 -0
- package/dist/cli/utils/ipc/utils/get-pipe-path.mjs +29 -0
- package/dist/cli/utils/ipc/utils/temporary-directory.mjs +40 -0
- package/dist/cli/utils/project.mjs +18 -0
- package/dist/cli/utils/stream.mjs +90 -0
- package/dist/cli/utils/version.mjs +13 -0
- package/dist/docker/compose.mjs +185 -0
- package/dist/docker/docker.mjs +390 -0
- package/dist/docker/shell.mjs +62 -0
- package/dist/utils/config.mjs +104 -0
- package/dist/utils/logging.mjs +96 -0
- package/package.json +17 -17
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { builder } from "./utils/builder.mjs";
|
|
2
|
+
import { detect } from "package-manager-detector";
|
|
3
|
+
import { $ } from "execa";
|
|
4
|
+
builder
|
|
5
|
+
.command("sysinfo")
|
|
6
|
+
.description("Print system information")
|
|
7
|
+
.action(async () => {
|
|
8
|
+
const manager = await detect();
|
|
9
|
+
if (!manager)
|
|
10
|
+
throw new Error("No package manager detected");
|
|
11
|
+
console.log("Node version:", process.version);
|
|
12
|
+
console.log("Operating system:", process.platform, process.arch);
|
|
13
|
+
console.log("Package manager:", manager.name);
|
|
14
|
+
console.log("Package manager version:", manager.version ?? "N/A");
|
|
15
|
+
console.log("-".repeat(20));
|
|
16
|
+
const output = await (async () => {
|
|
17
|
+
switch (manager.name) {
|
|
18
|
+
case "npm":
|
|
19
|
+
return await $ `npm ls --depth=4`;
|
|
20
|
+
case "yarn":
|
|
21
|
+
if (manager.version === "berry") {
|
|
22
|
+
return await $ `yarn info`;
|
|
23
|
+
}
|
|
24
|
+
return await $ `yarn list --depth=4`;
|
|
25
|
+
case "pnpm":
|
|
26
|
+
return await $ `pnpm ls --depth=4`;
|
|
27
|
+
case "bun":
|
|
28
|
+
return await $ `bun pm ls`;
|
|
29
|
+
default:
|
|
30
|
+
return await $ `npm ls`;
|
|
31
|
+
}
|
|
32
|
+
})();
|
|
33
|
+
const gatherMatch = (str, regex) => {
|
|
34
|
+
return [...new Set([...str.matchAll(regex)].map((match) => match[0]))];
|
|
35
|
+
};
|
|
36
|
+
const packages = gatherMatch(output.stdout, /(@langchain\/[^\s@]+|langsmith|langchain|zod|zod-to-json-schema)/g);
|
|
37
|
+
async function getPackageInfo(packageName) {
|
|
38
|
+
switch (manager?.name) {
|
|
39
|
+
case "npm":
|
|
40
|
+
return (await $ `npm explain ${packageName}`).stdout;
|
|
41
|
+
case "yarn":
|
|
42
|
+
return (await $ `yarn why ${packageName}`).stdout;
|
|
43
|
+
case "pnpm":
|
|
44
|
+
return (await $ `pnpm why ${packageName}`).stdout;
|
|
45
|
+
case "bun":
|
|
46
|
+
return (await $ `bun why ${packageName}`).stdout;
|
|
47
|
+
default:
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function escapeRegExp(text) {
|
|
52
|
+
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
|
53
|
+
}
|
|
54
|
+
for (const pkg of packages) {
|
|
55
|
+
const info = await getPackageInfo(pkg);
|
|
56
|
+
if (!info)
|
|
57
|
+
continue;
|
|
58
|
+
const targetRegex = new RegExp(escapeRegExp(pkg) + "[@\\s][^\\s]*", "g");
|
|
59
|
+
console.log(pkg, "->", gatherMatch(info, targetRegex)
|
|
60
|
+
.map((i) => i.slice(pkg.length).trim())
|
|
61
|
+
.join(", "));
|
|
62
|
+
}
|
|
63
|
+
});
|
package/dist/cli/up.mjs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { builder } from "./utils/builder.mjs";
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { getConfig } from "../utils/config.mjs";
|
|
5
|
+
import { getProjectPath } from "./utils/project.mjs";
|
|
6
|
+
import { logger } from "../utils/logging.mjs";
|
|
7
|
+
import { createCompose, getDockerCapabilities } from "../docker/compose.mjs";
|
|
8
|
+
import { configToCompose, getBaseImage } from "../docker/docker.mjs";
|
|
9
|
+
import { getExecaOptions } from "../docker/shell.mjs";
|
|
10
|
+
import { $ } from "execa";
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import dedent from "dedent";
|
|
13
|
+
import { withAnalytics } from "./utils/analytics.mjs";
|
|
14
|
+
import { gracefulExit } from "exit-hook";
|
|
15
|
+
const sha256 = (input) => createHash("sha256").update(input).digest("hex");
|
|
16
|
+
const getProjectName = (configPath) => {
|
|
17
|
+
const cwd = path.dirname(configPath).toLocaleLowerCase();
|
|
18
|
+
return `${path.basename(cwd)}-${sha256(cwd)}`;
|
|
19
|
+
};
|
|
20
|
+
const stream = (proc) => {
|
|
21
|
+
logger.debug(`Running "${proc.spawnargs.join(" ")}"`);
|
|
22
|
+
return proc;
|
|
23
|
+
};
|
|
24
|
+
const waitForHealthcheck = async (port) => {
|
|
25
|
+
const now = Date.now();
|
|
26
|
+
while (Date.now() - now < 10_000) {
|
|
27
|
+
const ok = await fetch(`http://localhost:${port}/ok`).then((res) => res.ok, () => false);
|
|
28
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
29
|
+
if (ok)
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
throw new Error("Healthcheck timed out");
|
|
33
|
+
};
|
|
34
|
+
builder
|
|
35
|
+
.command("up")
|
|
36
|
+
.description("Launch LangGraph API server.")
|
|
37
|
+
.option("-c, --config <path>", "Path to configuration file", process.cwd())
|
|
38
|
+
.option("-d, --docker-compose <path>", "Advanced: Path to docker-compose.yml file with additional services to launch")
|
|
39
|
+
.option("-p, --port <port>", "Port to run the server on", "8123")
|
|
40
|
+
.option("--recreate", "Force recreate containers and volumes", false)
|
|
41
|
+
.option("--no-pull", "Running the server with locally-built images. By default LangGraph will pull the latest images from the registry")
|
|
42
|
+
.option("--watch", "Restart on file changes", false)
|
|
43
|
+
.option("--wait", "Wait for services to start before returning. Implies --detach", false)
|
|
44
|
+
.option("--postgres-uri <uri>", "Postgres URI to use for the database. Defaults to launching a local database")
|
|
45
|
+
.exitOverride((error) => gracefulExit(error.exitCode))
|
|
46
|
+
.hook("preAction", withAnalytics((command) => ({
|
|
47
|
+
config: command.opts().config !== process.cwd(),
|
|
48
|
+
port: command.opts().port !== "8123",
|
|
49
|
+
postgres_uri: !!command.opts().postgresUri,
|
|
50
|
+
docker_compose: !!command.opts().dockerCompose,
|
|
51
|
+
recreate: command.opts().recreate,
|
|
52
|
+
pull: command.opts().pull,
|
|
53
|
+
watch: command.opts().watch,
|
|
54
|
+
wait: command.opts().wait,
|
|
55
|
+
})))
|
|
56
|
+
.action(async (params) => {
|
|
57
|
+
logger.info("Starting LangGraph API server...");
|
|
58
|
+
logger.warn(dedent `
|
|
59
|
+
For local dev, requires env var LANGSMITH_API_KEY with access to LangSmith Deployment.
|
|
60
|
+
For production use, requires a license key in env var LANGGRAPH_CLOUD_LICENSE_KEY.
|
|
61
|
+
`);
|
|
62
|
+
const configPath = await getProjectPath(params.config);
|
|
63
|
+
const config = getConfig(await fs.readFile(configPath, "utf-8"));
|
|
64
|
+
const cwd = path.dirname(configPath);
|
|
65
|
+
const capabilities = await getDockerCapabilities();
|
|
66
|
+
const fullRestartFiles = [configPath];
|
|
67
|
+
if (typeof config.env === "string") {
|
|
68
|
+
fullRestartFiles.push(path.resolve(cwd, config.env));
|
|
69
|
+
}
|
|
70
|
+
const { apiDef } = await configToCompose(configPath, config, {
|
|
71
|
+
watch: capabilities.watchAvailable,
|
|
72
|
+
});
|
|
73
|
+
const name = getProjectName(configPath);
|
|
74
|
+
const execOpts = await getExecaOptions({
|
|
75
|
+
cwd,
|
|
76
|
+
stdout: "inherit",
|
|
77
|
+
stderr: "inherit",
|
|
78
|
+
});
|
|
79
|
+
const exec = $(execOpts);
|
|
80
|
+
if (!config._INTERNAL_docker_tag && params.pull) {
|
|
81
|
+
// pull the image
|
|
82
|
+
logger.info(`Pulling image ${getBaseImage(config)}...`);
|
|
83
|
+
await stream(exec `docker pull ${getBaseImage(config)}`);
|
|
84
|
+
}
|
|
85
|
+
// remove dangling images
|
|
86
|
+
logger.info(`Pruning dangling images...`);
|
|
87
|
+
await stream(exec `docker image prune -f --filter ${`label=com.docker.compose.project=${name}`}`);
|
|
88
|
+
// remove stale containers
|
|
89
|
+
logger.info(`Pruning stale containers...`);
|
|
90
|
+
await stream(exec `docker container prune -f --filter ${`label=com.docker.compose.project=${name}`}`);
|
|
91
|
+
const input = createCompose(capabilities, {
|
|
92
|
+
port: +params.port,
|
|
93
|
+
postgresUri: params.postgresUri,
|
|
94
|
+
apiDef,
|
|
95
|
+
});
|
|
96
|
+
const args = ["--remove-orphans"];
|
|
97
|
+
if (params.recreate) {
|
|
98
|
+
args.push("--force-recreate", "--renew-anon-volumes");
|
|
99
|
+
try {
|
|
100
|
+
await stream(exec `docker volume rm langgraph-data`);
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
// ignore
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (params.watch) {
|
|
107
|
+
if (capabilities.watchAvailable) {
|
|
108
|
+
args.push("--watch");
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
logger.warn("Watch mode is not available. Please upgrade your Docker Engine.");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else if (params.wait) {
|
|
115
|
+
args.push("--wait");
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
args.push("--abort-on-container-exit");
|
|
119
|
+
}
|
|
120
|
+
logger.info(`Launching docker-compose...`);
|
|
121
|
+
const cmd = capabilities.composeType === "plugin"
|
|
122
|
+
? ["docker", "compose"]
|
|
123
|
+
: ["docker-compose"];
|
|
124
|
+
cmd.push("--project-directory", cwd, "--project-name", name);
|
|
125
|
+
const userCompose = params.dockerCompose || config.docker_compose_file;
|
|
126
|
+
if (userCompose)
|
|
127
|
+
cmd.push("-f", userCompose);
|
|
128
|
+
cmd.push("-f", "-");
|
|
129
|
+
const up = stream($({ ...execOpts, input }) `${cmd} up ${args}`);
|
|
130
|
+
waitForHealthcheck(+params.port).then(() => {
|
|
131
|
+
logger.info(`
|
|
132
|
+
Ready!
|
|
133
|
+
- API: http://localhost:${params.port}
|
|
134
|
+
- Docs: http://localhost:${params.port}/docs
|
|
135
|
+
- LangGraph Studio: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:${params.port}
|
|
136
|
+
`);
|
|
137
|
+
}, () => void 0);
|
|
138
|
+
await up.catch(() => void 0);
|
|
139
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import { version } from "./version.mjs";
|
|
3
|
+
const SUPABASE_PUBLIC_API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imt6cmxwcG9qaW5wY3l5YWlweG5iIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MTkyNTc1NzksImV4cCI6MjAzNDgzMzU3OX0.kkVOlLz3BxemA5nP-vat3K4qRtrDuO4SwZSR_htcX9c";
|
|
4
|
+
const SUPABASE_URL = "https://kzrlppojinpcyyaipxnb.supabase.co";
|
|
5
|
+
async function logData(data) {
|
|
6
|
+
try {
|
|
7
|
+
await fetch(`${SUPABASE_URL}/rest/v1/js_logs`, {
|
|
8
|
+
method: "POST",
|
|
9
|
+
headers: {
|
|
10
|
+
"Content-Type": "application/json",
|
|
11
|
+
apikey: SUPABASE_PUBLIC_API_KEY,
|
|
12
|
+
"User-Agent": "Mozilla/5.0",
|
|
13
|
+
},
|
|
14
|
+
body: JSON.stringify(data),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
// pass
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
let analyticsPromise = Promise.resolve();
|
|
22
|
+
export function withAnalytics(fn, options) {
|
|
23
|
+
if (process.env.LANGGRAPH_CLI_NO_ANALYTICS === "1") {
|
|
24
|
+
return () => void 0;
|
|
25
|
+
}
|
|
26
|
+
return function (actionCommand) {
|
|
27
|
+
analyticsPromise = analyticsPromise.then(() => logData({
|
|
28
|
+
os: os.platform(),
|
|
29
|
+
os_version: os.release(),
|
|
30
|
+
node_version: process.version,
|
|
31
|
+
cli_version: version,
|
|
32
|
+
cli_command: options?.name ?? actionCommand.name(),
|
|
33
|
+
params: fn?.(actionCommand) ?? {},
|
|
34
|
+
}).catch(() => { }));
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export async function flushAnalytics() {
|
|
38
|
+
await analyticsPromise;
|
|
39
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// MIT License
|
|
2
|
+
//
|
|
3
|
+
// Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>
|
|
4
|
+
//
|
|
5
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
// in the Software without restriction, including without limitation the rights
|
|
8
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
// furnished to do so, subject to the following conditions:
|
|
11
|
+
//
|
|
12
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
// copies or substantial portions of the Software.
|
|
14
|
+
//
|
|
15
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
// SOFTWARE.
|
|
22
|
+
//
|
|
23
|
+
// https://github.com/privatenumber/tsx/tree/28a3e7d2b8fd72b683aab8a98dd1fcee4624e4cb
|
|
24
|
+
import net from "node:net";
|
|
25
|
+
import fs from "node:fs";
|
|
26
|
+
import { tmpdir } from "./utils/temporary-directory.mjs";
|
|
27
|
+
import { getPipePath } from "./utils/get-pipe-path.mjs";
|
|
28
|
+
const bufferData = (onMessage) => {
|
|
29
|
+
let buffer = Buffer.alloc(0);
|
|
30
|
+
return (data) => {
|
|
31
|
+
buffer = Buffer.concat([buffer, data]);
|
|
32
|
+
while (buffer.length > 4) {
|
|
33
|
+
const messageLength = buffer.readInt32BE(0);
|
|
34
|
+
if (buffer.length >= 4 + messageLength) {
|
|
35
|
+
const message = buffer.slice(4, 4 + messageLength);
|
|
36
|
+
onMessage(message);
|
|
37
|
+
buffer = buffer.slice(4 + messageLength);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
export const createIpcServer = async () => {
|
|
46
|
+
const server = net.createServer((socket) => {
|
|
47
|
+
socket.on("data", bufferData((message) => {
|
|
48
|
+
const data = JSON.parse(message.toString());
|
|
49
|
+
server.emit("data", data);
|
|
50
|
+
}));
|
|
51
|
+
});
|
|
52
|
+
const pipePath = getPipePath(process.pid);
|
|
53
|
+
await fs.promises.mkdir(tmpdir, { recursive: true });
|
|
54
|
+
/**
|
|
55
|
+
* Fix #457 (https://github.com/privatenumber/tsx/issues/457)
|
|
56
|
+
*
|
|
57
|
+
* Avoid the error "EADDRINUSE: address already in use"
|
|
58
|
+
*
|
|
59
|
+
* If the pipe file already exists, it means that the previous process has been closed abnormally.
|
|
60
|
+
*
|
|
61
|
+
* We can safely delete the pipe file, the previous process must has been closed,
|
|
62
|
+
* as pid is unique at the same.
|
|
63
|
+
*/
|
|
64
|
+
await fs.promises.rm(pipePath, { force: true });
|
|
65
|
+
await new Promise((resolve, reject) => {
|
|
66
|
+
server.listen(pipePath, resolve);
|
|
67
|
+
server.on("error", reject);
|
|
68
|
+
});
|
|
69
|
+
// Prevent Node from waiting for this socket to close before exiting
|
|
70
|
+
server.unref();
|
|
71
|
+
process.on("exit", () => {
|
|
72
|
+
server.close();
|
|
73
|
+
/**
|
|
74
|
+
* Only clean on Unix
|
|
75
|
+
*
|
|
76
|
+
* https://nodejs.org/api/net.html#ipc-support:
|
|
77
|
+
* On Windows, the local domain is implemented using a named pipe.
|
|
78
|
+
* The path must refer to an entry in \\?\pipe\ or \\.\pipe\.
|
|
79
|
+
* Any characters are permitted, but the latter may do some processing
|
|
80
|
+
* of pipe names, such as resolving .. sequences. Despite how it might
|
|
81
|
+
* look, the pipe namespace is flat. Pipes will not persist. They are
|
|
82
|
+
* removed when the last reference to them is closed. Unlike Unix domain
|
|
83
|
+
* sockets, Windows will close and remove the pipe when the owning process exits.
|
|
84
|
+
*/
|
|
85
|
+
if (process.platform !== "win32") {
|
|
86
|
+
try {
|
|
87
|
+
fs.rmSync(pipePath);
|
|
88
|
+
}
|
|
89
|
+
catch { }
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
return [process.pid, server];
|
|
93
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// MIT License
|
|
2
|
+
//
|
|
3
|
+
// Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>
|
|
4
|
+
//
|
|
5
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
// in the Software without restriction, including without limitation the rights
|
|
8
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
// furnished to do so, subject to the following conditions:
|
|
11
|
+
//
|
|
12
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
// copies or substantial portions of the Software.
|
|
14
|
+
//
|
|
15
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
// SOFTWARE.
|
|
22
|
+
//
|
|
23
|
+
// https://github.com/privatenumber/tsx/tree/28a3e7d2b8fd72b683aab8a98dd1fcee4624e4cb
|
|
24
|
+
import path from "node:path";
|
|
25
|
+
import { tmpdir } from "./temporary-directory.mjs";
|
|
26
|
+
export const getPipePath = (processId) => {
|
|
27
|
+
const pipePath = path.join(tmpdir, `${processId}.pipe`);
|
|
28
|
+
return process.platform === "win32" ? `\\\\?\\pipe\\${pipePath}` : pipePath;
|
|
29
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// MIT License
|
|
2
|
+
//
|
|
3
|
+
// Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>
|
|
4
|
+
//
|
|
5
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
// in the Software without restriction, including without limitation the rights
|
|
8
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
// furnished to do so, subject to the following conditions:
|
|
11
|
+
//
|
|
12
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
// copies or substantial portions of the Software.
|
|
14
|
+
//
|
|
15
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
// SOFTWARE.
|
|
22
|
+
//
|
|
23
|
+
// https://github.com/privatenumber/tsx/tree/28a3e7d2b8fd72b683aab8a98dd1fcee4624e4cb
|
|
24
|
+
import path from "node:path";
|
|
25
|
+
import os from "node:os";
|
|
26
|
+
/**
|
|
27
|
+
* Cache directory is based on the user's identifier
|
|
28
|
+
* to avoid permission issues when accessed by a different user
|
|
29
|
+
*/
|
|
30
|
+
const { geteuid } = process;
|
|
31
|
+
const userId = geteuid
|
|
32
|
+
? // For Linux users with virtual users on CI (e.g. Docker)
|
|
33
|
+
geteuid()
|
|
34
|
+
: // Use username on Windows because it doesn't have id
|
|
35
|
+
os.userInfo().username;
|
|
36
|
+
/**
|
|
37
|
+
* This ensures that the cache directory is unique per user
|
|
38
|
+
* and has the appropriate permissions
|
|
39
|
+
*/
|
|
40
|
+
export const tmpdir = path.join(os.tmpdir(), `tsx-${userId}`);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as url from "node:url";
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
export async function getProjectPath(key) {
|
|
5
|
+
const configPathOrFile = key.startsWith("file://")
|
|
6
|
+
? url.fileURLToPath(key)
|
|
7
|
+
: path.resolve(process.cwd(), key);
|
|
8
|
+
let configPath = undefined;
|
|
9
|
+
if ((await fs.stat(configPathOrFile)).isDirectory()) {
|
|
10
|
+
configPath = path.join(configPathOrFile, "langgraph.json");
|
|
11
|
+
}
|
|
12
|
+
else if (path.basename(configPathOrFile) === "langgraph.json") {
|
|
13
|
+
configPath = configPathOrFile;
|
|
14
|
+
}
|
|
15
|
+
if (!configPath)
|
|
16
|
+
throw new Error("Invalid path");
|
|
17
|
+
return configPath;
|
|
18
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Transform } from "node:stream";
|
|
2
|
+
const CR = "\r".charCodeAt(0);
|
|
3
|
+
const LF = "\n".charCodeAt(0);
|
|
4
|
+
const TRAILING_NEWLINE = [CR, LF];
|
|
5
|
+
function joinArrays(data) {
|
|
6
|
+
const totalLength = data.reduce((acc, curr) => acc + curr.length, 0);
|
|
7
|
+
let merged = new Uint8Array(totalLength);
|
|
8
|
+
let offset = 0;
|
|
9
|
+
for (const c of data) {
|
|
10
|
+
merged.set(c, offset);
|
|
11
|
+
offset += c.length;
|
|
12
|
+
}
|
|
13
|
+
return merged;
|
|
14
|
+
}
|
|
15
|
+
export class BytesLineDecoder extends TransformStream {
|
|
16
|
+
constructor() {
|
|
17
|
+
let buffer = [];
|
|
18
|
+
let trailingCr = false;
|
|
19
|
+
super({
|
|
20
|
+
start() {
|
|
21
|
+
buffer = [];
|
|
22
|
+
trailingCr = false;
|
|
23
|
+
},
|
|
24
|
+
transform(chunk, controller) {
|
|
25
|
+
// See https://docs.python.org/3/glossary.html#term-universal-newlines
|
|
26
|
+
let text = chunk;
|
|
27
|
+
// Handle trailing CR from previous chunk
|
|
28
|
+
if (trailingCr) {
|
|
29
|
+
text = joinArrays([[CR], text]);
|
|
30
|
+
trailingCr = false;
|
|
31
|
+
}
|
|
32
|
+
// Check for trailing CR in current chunk
|
|
33
|
+
if (text.length > 0 && text.at(-1) === CR) {
|
|
34
|
+
trailingCr = true;
|
|
35
|
+
text = text.subarray(0, -1);
|
|
36
|
+
}
|
|
37
|
+
if (!text.length)
|
|
38
|
+
return;
|
|
39
|
+
const trailingNewline = TRAILING_NEWLINE.includes(text.at(-1));
|
|
40
|
+
const lastIdx = text.length - 1;
|
|
41
|
+
const { lines } = text.reduce((acc, cur, idx) => {
|
|
42
|
+
if (acc.from > idx)
|
|
43
|
+
return acc;
|
|
44
|
+
if (cur === CR || cur === LF) {
|
|
45
|
+
acc.lines.push(text.subarray(acc.from, idx));
|
|
46
|
+
if (cur === CR && text[idx + 1] === LF) {
|
|
47
|
+
acc.from = idx + 2;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
acc.from = idx + 1;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (idx === lastIdx && acc.from <= lastIdx) {
|
|
54
|
+
acc.lines.push(text.subarray(acc.from));
|
|
55
|
+
}
|
|
56
|
+
return acc;
|
|
57
|
+
}, { lines: [], from: 0 });
|
|
58
|
+
if (lines.length === 1 && !trailingNewline) {
|
|
59
|
+
buffer.push(lines[0]);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (buffer.length) {
|
|
63
|
+
// Include existing buffer in first line
|
|
64
|
+
buffer.push(lines[0]);
|
|
65
|
+
lines[0] = joinArrays(buffer);
|
|
66
|
+
buffer = [];
|
|
67
|
+
}
|
|
68
|
+
if (!trailingNewline) {
|
|
69
|
+
// If the last segment is not newline terminated,
|
|
70
|
+
// buffer it for the next chunk
|
|
71
|
+
if (lines.length)
|
|
72
|
+
buffer = [lines.pop()];
|
|
73
|
+
}
|
|
74
|
+
// Enqueue complete lines
|
|
75
|
+
for (const line of lines) {
|
|
76
|
+
controller.enqueue(line);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
flush(controller) {
|
|
80
|
+
if (buffer.length) {
|
|
81
|
+
controller.enqueue(joinArrays(buffer));
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
fromWeb() {
|
|
87
|
+
// @ts-expect-error invalid types for response.body
|
|
88
|
+
return Transform.fromWeb(this);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as fs from "node:fs/promises";
|
|
2
|
+
import * as url from "node:url";
|
|
3
|
+
async function getVersion() {
|
|
4
|
+
try {
|
|
5
|
+
const packageJson = url.fileURLToPath(new URL("../../../package.json", import.meta.url));
|
|
6
|
+
const { version } = JSON.parse(await fs.readFile(packageJson, "utf-8"));
|
|
7
|
+
return version + "+js";
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return "0.0.0-unknown";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export const version = await getVersion();
|