@agenthead/graph 0.0.0-stage → 0.2.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.
@@ -0,0 +1,233 @@
1
+ import { spawn } from "node:child_process";
2
+ export class LspError extends Error {
3
+ reason;
4
+ name = "LspError";
5
+ constructor(reason, message) {
6
+ super(message);
7
+ this.reason = reason;
8
+ }
9
+ }
10
+ // A real header is a few dozen bytes. The body limit bounds what a misbehaving
11
+ // server can make the client buffer.
12
+ const maxHeaderBytes = 8 * 1024;
13
+ const maxMessageBytes = 32 * 1024 * 1024;
14
+ function isRecord(value) {
15
+ return typeof value === "object" && value !== null && !Array.isArray(value);
16
+ }
17
+ function contentLength(header) {
18
+ let length;
19
+ for (const line of header.split("\r\n")) {
20
+ const colon = line.indexOf(":");
21
+ if (colon < 0)
22
+ return undefined;
23
+ if (line.slice(0, colon).trim().toLowerCase() !== "content-length")
24
+ continue;
25
+ const value = line.slice(colon + 1).trim();
26
+ if (!/^\d{1,9}$/.test(value))
27
+ return undefined;
28
+ length = Number(value);
29
+ }
30
+ return length;
31
+ }
32
+ /** Settle when `promise` does or after `ms`, clearing the timer either way. */
33
+ function within(promise, ms) {
34
+ return new Promise((resolve) => {
35
+ const timer = setTimeout(resolve, ms);
36
+ void promise.then(() => {
37
+ clearTimeout(timer);
38
+ resolve();
39
+ });
40
+ });
41
+ }
42
+ /**
43
+ * One language server process, spoken to with JSON-RPC 2.0 and Content-Length
44
+ * framing over stdio. A request that misses its deadline fails with "timeout"
45
+ * and is cancelled with `$/cancelRequest`; the server stays. A server must
46
+ * still answer a cancelled request, so one that has not within another
47
+ * deadline has hung. That, an exit or a broken frame stops the server's whole
48
+ * process group and fails every pending and later request. The client answers
49
+ * requests from the server with MethodNotFound and ignores its notifications.
50
+ */
51
+ export class LspClient {
52
+ child;
53
+ pending = new Map();
54
+ nextId = 1;
55
+ chunks = [];
56
+ size = 0;
57
+ needed = 0;
58
+ failure;
59
+ /** Settles when the process exits or fails to start. */
60
+ exited;
61
+ constructor(command, args, cwd, env) {
62
+ this.child = spawn(command, args, {
63
+ cwd,
64
+ env,
65
+ stdio: ["pipe", "pipe", "ignore"],
66
+ // Lead a process group, so stopping the server also stops helpers it
67
+ // started, such as tsserver.
68
+ detached: process.platform !== "win32",
69
+ windowsHide: true,
70
+ });
71
+ this.exited = new Promise((resolve) => {
72
+ this.child.once("exit", () => resolve());
73
+ this.child.once("error", () => resolve());
74
+ });
75
+ this.child.on("error", (error) => this.fail(this.child.pid === undefined ? "unavailable" : "crashed", `Language server failed: ${error.message}`));
76
+ this.child.on("exit", (code, signal) => this.fail("crashed", `Language server exited (${signal ?? code})`));
77
+ const stdin = this.child.stdin;
78
+ const stdout = this.child.stdout;
79
+ stdin.on("error", () => this.fail("crashed", "Language server input closed"));
80
+ stdout.on("data", (chunk) => this.receive(chunk));
81
+ // Unhandled stream errors would be thrown in the caller's process.
82
+ for (const event of ["end", "error"])
83
+ stdout.on(event, () => this.fail("crashed", "Language server output ended"));
84
+ // An idle server never keeps Node running. If the process exits without
85
+ // close(), the server reads end of input and exits too.
86
+ this.child.unref();
87
+ stdin.unref();
88
+ stdout.unref();
89
+ }
90
+ request(method, params, timeoutMs) {
91
+ if (this.failure)
92
+ return Promise.reject(this.failure);
93
+ const id = this.nextId++;
94
+ const result = new Promise((resolve, reject) => {
95
+ const timer = setTimeout(() => this.cancel(id, method, timeoutMs), timeoutMs);
96
+ this.pending.set(id, { resolve, reject, timer });
97
+ });
98
+ this.send({ jsonrpc: "2.0", id, method, params });
99
+ return result;
100
+ }
101
+ /** True once the process is stopped: every request fails at once. */
102
+ get stopped() {
103
+ return this.failure !== undefined;
104
+ }
105
+ notify(method, params) {
106
+ if (!this.failure)
107
+ this.send({ jsonrpc: "2.0", method, params });
108
+ }
109
+ /** Stop the process group now and fail every pending request. */
110
+ kill() {
111
+ this.fail("crashed", "Language server stopped");
112
+ }
113
+ /** Ask for a clean shutdown, then stop the process group regardless. */
114
+ async stop(timeoutMs) {
115
+ if (!this.failure) {
116
+ try {
117
+ await this.request("shutdown", undefined, timeoutMs);
118
+ this.notify("exit");
119
+ this.child.stdin.end();
120
+ await within(this.exited, timeoutMs);
121
+ }
122
+ catch {
123
+ // It timed out or exited; it is stopped below either way.
124
+ }
125
+ }
126
+ this.kill();
127
+ await within(this.exited, timeoutMs);
128
+ }
129
+ /** Fail a late request now, then stop the server if it never answers. */
130
+ cancel(id, method, timeoutMs) {
131
+ const pending = this.pending.get(id);
132
+ if (!pending)
133
+ return;
134
+ pending.reject(new LspError("timeout", `${method} got no answer within ${timeoutMs} ms`));
135
+ this.notify("$/cancelRequest", { id });
136
+ // The entry stays to take the answer; settling the promise again is a no-op.
137
+ pending.timer = setTimeout(() => this.fail("timeout", `${method} was cancelled and got no answer within another ${timeoutMs} ms`), timeoutMs);
138
+ }
139
+ send(message) {
140
+ const body = Buffer.from(JSON.stringify(message), "utf8");
141
+ const stdin = this.child.stdin;
142
+ stdin.write(`Content-Length: ${body.length}\r\n\r\n`);
143
+ stdin.write(body);
144
+ }
145
+ fail(reason, message) {
146
+ if (this.failure)
147
+ return;
148
+ this.failure = new LspError(reason, message);
149
+ for (const pending of this.pending.values()) {
150
+ clearTimeout(pending.timer);
151
+ pending.reject(this.failure);
152
+ }
153
+ this.pending.clear();
154
+ const pid = this.child.pid;
155
+ if (pid === undefined)
156
+ return;
157
+ try {
158
+ if (process.platform === "win32")
159
+ this.child.kill("SIGKILL");
160
+ else
161
+ process.kill(-pid, "SIGKILL");
162
+ }
163
+ catch {
164
+ // Already gone.
165
+ }
166
+ }
167
+ receive(chunk) {
168
+ if (this.failure)
169
+ return;
170
+ this.chunks.push(chunk);
171
+ this.size += chunk.length;
172
+ // Join chunks only once a whole header or message can have arrived.
173
+ if (this.size < this.needed)
174
+ return;
175
+ let data = Buffer.concat(this.chunks, this.size);
176
+ for (;;) {
177
+ const end = data.indexOf("\r\n\r\n");
178
+ if (end < 0 || end > maxHeaderBytes) {
179
+ if (data.length > maxHeaderBytes)
180
+ return this.fail("crashed", "Invalid message header");
181
+ this.needed = data.length + 1;
182
+ break;
183
+ }
184
+ const length = contentLength(data.subarray(0, end).toString("latin1"));
185
+ if (length === undefined || length > maxMessageBytes)
186
+ return this.fail("crashed", "Invalid message header");
187
+ const total = end + 4 + length;
188
+ if (data.length < total) {
189
+ this.needed = total;
190
+ break;
191
+ }
192
+ const body = data.subarray(end + 4, total).toString("utf8");
193
+ data = data.subarray(total);
194
+ let message;
195
+ try {
196
+ message = JSON.parse(body);
197
+ }
198
+ catch {
199
+ return this.fail("crashed", "Invalid message body");
200
+ }
201
+ this.handle(message);
202
+ if (this.failure)
203
+ return;
204
+ }
205
+ this.chunks = [data];
206
+ this.size = data.length;
207
+ }
208
+ handle(message) {
209
+ if (!isRecord(message))
210
+ return;
211
+ const { id, method } = message;
212
+ if (typeof method === "string") {
213
+ // This client registers no methods: answer requests, ignore notifications.
214
+ if (typeof id === "number" || typeof id === "string")
215
+ this.send({
216
+ jsonrpc: "2.0",
217
+ id,
218
+ error: { code: -32601, message: `Unsupported method ${method}` },
219
+ });
220
+ return;
221
+ }
222
+ const pending = typeof id === "number" && this.pending.get(id);
223
+ if (!pending)
224
+ return;
225
+ this.pending.delete(id);
226
+ clearTimeout(pending.timer);
227
+ if (message.error !== undefined && message.error !== null)
228
+ pending.reject(new LspError("error", "Language server returned an error"));
229
+ else
230
+ pending.resolve(message.result);
231
+ }
232
+ }
233
+ //# sourceMappingURL=lsp-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lsp-client.js","sourceRoot":"","sources":["../src/lsp-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAqB,MAAM,oBAAoB,CAAC;AAM9D,MAAM,OAAO,QAAS,SAAQ,KAAK;IAGtB;IAFO,IAAI,GAAG,UAAU,CAAC;IACpC,YACW,MAAkB,EAC3B,OAAe;QAEf,KAAK,CAAC,OAAO,CAAC,CAAC;QAHN,WAAM,GAAN,M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3E,IAAI,OAAO,EAAE,KAAK,QAAQ,IAAI,OAAO,EAAE,KAAK,QAAQ;gBAClD,IAAI,CAAC,IAAI,CAAC;oBACR,OAAO,EAAE,KAAK;oBACd,EAAE;oBACF,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,sBAAsB,MAAM,EAAE,EAAE;iBACjE,CAAC,CAAC;YACL,OAAO;QACT,CAAC;QACD,MAAM,OAAO,GAAG,OAAO,EAAE,KAAK,QAAQ,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC/D,IAAI,CAAC,OAAO;YAAE,OAAO;QACrB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAY,CAAC,CAAC;QAClC,YAAY,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAC5B,IAAI,OAAO,CAAC,KAAK,KAAK,SAAS,IAAI,OAAO,CAAC,KAAK,KAAK,IAAI;YACvD,OAAO,CAAC,MAAM,CACZ,IAAI,QAAQ,CAAC,OAAO,EAAE,mCAAmC,CAAC,CAC3D,CAAC;;YACC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ import type Database from "better-sqlite3";
2
+ export declare const schemaVersion = 1;
3
+ export declare function migrate(db: Database.Database): void;
@@ -0,0 +1,32 @@
1
+ export const schemaVersion = 1;
2
+ export function migrate(db) {
3
+ db.transaction(() => {
4
+ const version = db.pragma("user_version", { simple: true });
5
+ if (version < 0 || version > schemaVersion)
6
+ throw new Error(`Unsupported graph database version ${version}`);
7
+ if (version === 0) {
8
+ db.exec(`
9
+ CREATE TABLE graph_files (
10
+ path TEXT PRIMARY KEY,
11
+ hash TEXT NOT NULL,
12
+ language TEXT NOT NULL,
13
+ parser_version TEXT NOT NULL,
14
+ has_errors INTEGER NOT NULL CHECK (has_errors IN (0, 1))
15
+ ) STRICT;
16
+ CREATE TABLE graph_entries (
17
+ id INTEGER PRIMARY KEY,
18
+ file TEXT NOT NULL REFERENCES graph_files(path) ON DELETE CASCADE,
19
+ kind TEXT NOT NULL CHECK (kind IN ('symbol', 'import', 'call', 'reference')),
20
+ name TEXT NOT NULL,
21
+ target TEXT,
22
+ data TEXT NOT NULL
23
+ ) STRICT;
24
+ CREATE INDEX graph_entry_name ON graph_entries(kind, name, file);
25
+ CREATE INDEX graph_entry_file ON graph_entries(file);
26
+ CREATE INDEX graph_import_target ON graph_entries(target) WHERE kind = 'import';
27
+ `);
28
+ db.pragma(`user_version = ${schemaVersion}`);
29
+ }
30
+ }).immediate();
31
+ }
32
+ //# sourceMappingURL=migrations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrations.js","sourceRoot":"","sources":["../src/migrations.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC;AAE/B,MAAM,UAAU,OAAO,CAAC,EAAqB;IAC3C,EAAE,CAAC,WAAW,CAAC,GAAG,EAAE;QAClB,MAAM,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC,cAAc,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAW,CAAC;QACtE,IAAI,OAAO,GAAG,CAAC,IAAI,OAAO,GAAG,aAAa;YACxC,MAAM,IAAI,KAAK,CAAC,sCAAsC,OAAO,EAAE,CAAC,CAAC;QACnE,IAAI,OAAO,KAAK,CAAC,EAAE,CAAC;YAClB,EAAE,CAAC,IAAI,CAAC;;;;;;;;;;;;;;;;;;;OAmBP,CAAC,CAAC;YACH,EAAE,CAAC,MAAM,CAAC,kBAAkB,aAAa,EAAE,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;AACjB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { FileGraph, Language } from "./types.js";
2
+ export declare function languageFor(file: string): Language | undefined;
3
+ export declare function parseFile(file: string, source: string, language: Language, hash: string): Promise<FileGraph>;