@mrclrchtr/supi-lsp 1.5.0 → 1.7.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/node_modules/@mrclrchtr/supi-core/package.json +6 -2
- package/node_modules/@mrclrchtr/supi-core/src/api.ts +12 -1
- package/node_modules/@mrclrchtr/supi-core/src/config/config.ts +1 -1
- package/node_modules/@mrclrchtr/supi-core/src/index.ts +12 -1
- package/node_modules/@mrclrchtr/supi-core/src/tool-framework.ts +116 -0
- package/package.json +10 -3
- package/src/client/client.ts +8 -5
- package/src/client/transport.ts +79 -190
- package/src/config/server-config.ts +38 -0
- package/src/config/types.ts +61 -387
- package/src/format.ts +16 -8
- package/src/lsp.ts +2 -2
- package/src/manager/manager-project-info.ts +1 -1
- package/src/pattern-matcher.ts +11 -184
- package/src/session/lsp-state.ts +1 -1
- package/src/tool/guidance.ts +1 -1
- package/src/tool/tool-specs.ts +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mrclrchtr/supi-core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "SuPi core — shared infrastructure for SuPi extensions (XML context tags, config system)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
],
|
|
21
21
|
"peerDependencies": {
|
|
22
22
|
"@earendil-works/pi-coding-agent": "*",
|
|
23
|
-
"@earendil-works/pi-tui": "*"
|
|
23
|
+
"@earendil-works/pi-tui": "*",
|
|
24
|
+
"typebox": "*"
|
|
24
25
|
},
|
|
25
26
|
"peerDependenciesMeta": {
|
|
26
27
|
"@earendil-works/pi-coding-agent": {
|
|
@@ -28,6 +29,9 @@
|
|
|
28
29
|
},
|
|
29
30
|
"@earendil-works/pi-tui": {
|
|
30
31
|
"optional": true
|
|
32
|
+
},
|
|
33
|
+
"typebox": {
|
|
34
|
+
"optional": true
|
|
31
35
|
}
|
|
32
36
|
},
|
|
33
37
|
"main": "src/api.ts",
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// supi-core — shared infrastructure for SuPi extensions.
|
|
2
2
|
// Provides XML context tag wrapping, unified config system, context-message utilities,
|
|
3
|
-
//
|
|
3
|
+
// settings registry for supi-wide TUI settings, and a shared tool-spec/registration framework.
|
|
4
4
|
|
|
5
5
|
export type { SupiConfigLocation, SupiConfigOptions } from "./config/config.ts";
|
|
6
6
|
export {
|
|
7
7
|
loadSupiConfig,
|
|
8
8
|
loadSupiConfigForScope,
|
|
9
|
+
readJsonFile,
|
|
9
10
|
removeSupiConfigKey,
|
|
10
11
|
writeSupiConfig,
|
|
11
12
|
} from "./config/config.ts";
|
|
@@ -83,3 +84,13 @@ export {
|
|
|
83
84
|
signalWaiting,
|
|
84
85
|
WAITING_SYMBOL,
|
|
85
86
|
} from "./terminal.ts";
|
|
87
|
+
export type { SuiPiToolPromptSurface, SuiPiToolSpec, ToolExecuteFn } from "./tool-framework.ts";
|
|
88
|
+
export {
|
|
89
|
+
CharacterParam,
|
|
90
|
+
derivePromptSurface,
|
|
91
|
+
FileParam,
|
|
92
|
+
LineParam,
|
|
93
|
+
MaxResultsParam,
|
|
94
|
+
registerSuiPiTools,
|
|
95
|
+
SymbolParam,
|
|
96
|
+
} from "./tool-framework.ts";
|
|
@@ -20,7 +20,7 @@ function getProjectConfigPath(cwd: string): string {
|
|
|
20
20
|
return path.join(cwd, PROJECT_CONFIG_DIR, CONFIG_FILE);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
function readJsonFile(filePath: string): Record<string, unknown> | null {
|
|
23
|
+
export function readJsonFile(filePath: string): Record<string, unknown> | null {
|
|
24
24
|
let content: string;
|
|
25
25
|
try {
|
|
26
26
|
content = fs.readFileSync(filePath, "utf-8");
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// supi-core — shared infrastructure for SuPi extensions.
|
|
2
2
|
// Provides XML context tag wrapping, unified config system, context-message utilities,
|
|
3
|
-
//
|
|
3
|
+
// settings registry for supi-wide TUI settings, and a shared tool-spec/registration framework.
|
|
4
4
|
|
|
5
5
|
export type { SupiConfigLocation, SupiConfigOptions } from "./config/config.ts";
|
|
6
6
|
export {
|
|
7
7
|
loadSupiConfig,
|
|
8
8
|
loadSupiConfigForScope,
|
|
9
|
+
readJsonFile,
|
|
9
10
|
removeSupiConfigKey,
|
|
10
11
|
writeSupiConfig,
|
|
11
12
|
} from "./config/config.ts";
|
|
@@ -83,3 +84,13 @@ export {
|
|
|
83
84
|
signalWaiting,
|
|
84
85
|
WAITING_SYMBOL,
|
|
85
86
|
} from "./terminal.ts";
|
|
87
|
+
export type { SuiPiToolPromptSurface, SuiPiToolSpec, ToolExecuteFn } from "./tool-framework.ts";
|
|
88
|
+
export {
|
|
89
|
+
CharacterParam,
|
|
90
|
+
derivePromptSurface,
|
|
91
|
+
FileParam,
|
|
92
|
+
LineParam,
|
|
93
|
+
MaxResultsParam,
|
|
94
|
+
registerSuiPiTools,
|
|
95
|
+
SymbolParam,
|
|
96
|
+
} from "./tool-framework.ts";
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Shared tool framework for SuPi extensions.
|
|
2
|
+
//
|
|
3
|
+
// Provides a standard ToolSpec→PromptSurface→registerTool pipeline so
|
|
4
|
+
// individual packages do not duplicate spec interfaces, guidance derivation,
|
|
5
|
+
// registration loops, or common TypeBox parameter schemas.
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
AgentToolResult,
|
|
9
|
+
AgentToolUpdateCallback,
|
|
10
|
+
ExtensionAPI,
|
|
11
|
+
ExtensionContext,
|
|
12
|
+
} from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import { type TSchema, Type } from "typebox";
|
|
14
|
+
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
// Types
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
|
|
19
|
+
/** Minimum contract for a SuPi tool definition. */
|
|
20
|
+
export interface SuiPiToolSpec {
|
|
21
|
+
name: string;
|
|
22
|
+
label: string;
|
|
23
|
+
description: string;
|
|
24
|
+
promptSnippet: string;
|
|
25
|
+
promptGuidelines: string[];
|
|
26
|
+
parameters: TSchema;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Derived prompt surface — what pi flattens into the system prompt. */
|
|
30
|
+
export interface SuiPiToolPromptSurface {
|
|
31
|
+
description: string;
|
|
32
|
+
promptSnippet: string;
|
|
33
|
+
promptGuidelines: string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// Guidance derivation
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Static derivation: copies spec fields into a prompt surface.
|
|
42
|
+
*
|
|
43
|
+
* Packages that need dynamic guidance (e.g. server-coverage injection) should
|
|
44
|
+
* build their own surfaces, optionally starting from the output of this helper.
|
|
45
|
+
*/
|
|
46
|
+
export function derivePromptSurface(spec: SuiPiToolSpec): SuiPiToolPromptSurface {
|
|
47
|
+
return {
|
|
48
|
+
description: spec.description,
|
|
49
|
+
promptSnippet: spec.promptSnippet,
|
|
50
|
+
promptGuidelines: [...spec.promptGuidelines],
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
// Registration
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
|
|
58
|
+
// biome-ignore lint/complexity/useMaxParams: matches pi ToolDefinition.execute signature
|
|
59
|
+
export type ToolExecuteFn = (
|
|
60
|
+
toolCallId: string,
|
|
61
|
+
params: unknown,
|
|
62
|
+
signal: AbortSignal | undefined,
|
|
63
|
+
onUpdate: AgentToolUpdateCallback<Record<string, unknown>> | undefined,
|
|
64
|
+
ctx: ExtensionContext,
|
|
65
|
+
) => Promise<AgentToolResult<Record<string, unknown>>>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Register a set of tools from specs + pre-derived surfaces.
|
|
69
|
+
*
|
|
70
|
+
* `createExecute` receives the spec and returns a pi-compatible execute
|
|
71
|
+
* function. This keeps execute-logic package-local while the framework owns
|
|
72
|
+
* the declarative surface and registration boilerplate.
|
|
73
|
+
*/
|
|
74
|
+
export function registerSuiPiTools(
|
|
75
|
+
pi: ExtensionAPI,
|
|
76
|
+
specs: readonly SuiPiToolSpec[],
|
|
77
|
+
surfaces: Record<string, SuiPiToolPromptSurface>,
|
|
78
|
+
createExecute: (spec: SuiPiToolSpec) => ToolExecuteFn,
|
|
79
|
+
): void {
|
|
80
|
+
for (const spec of specs) {
|
|
81
|
+
const surface = surfaces[spec.name];
|
|
82
|
+
pi.registerTool({
|
|
83
|
+
name: spec.name,
|
|
84
|
+
label: spec.label,
|
|
85
|
+
description: surface?.description ?? spec.description,
|
|
86
|
+
promptSnippet: surface?.promptSnippet ?? spec.promptSnippet,
|
|
87
|
+
promptGuidelines: surface?.promptGuidelines ?? [...spec.promptGuidelines],
|
|
88
|
+
parameters: spec.parameters,
|
|
89
|
+
execute: createExecute(spec),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
// Shared parameter builders
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
|
|
98
|
+
/** File path (relative or absolute). */
|
|
99
|
+
export const FileParam = Type.String({ description: "File path (relative or absolute)" });
|
|
100
|
+
|
|
101
|
+
/** 1-based line number. */
|
|
102
|
+
export const LineParam = Type.Number({ description: "1-based line number", minimum: 1 });
|
|
103
|
+
|
|
104
|
+
/** 1-based character column (UTF-16). */
|
|
105
|
+
export const CharacterParam = Type.Number({
|
|
106
|
+
description: "1-based column number (UTF-16)",
|
|
107
|
+
minimum: 1,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
/** Symbol name for discovery-based resolution. */
|
|
111
|
+
export const SymbolParam = Type.String({
|
|
112
|
+
description: "Symbol name for discovery-based resolution",
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
/** Maximum results to return. */
|
|
116
|
+
export const MaxResultsParam = Type.Number({ description: "Maximum results to return" });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mrclrchtr/supi-lsp",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "SuPi LSP extension — Language Server Protocol integration for pi",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -21,11 +21,18 @@
|
|
|
21
21
|
"!__tests__"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
+
"ignore": "^7.0.5",
|
|
24
25
|
"typescript": "6.0.3",
|
|
25
|
-
"
|
|
26
|
+
"vscode-jsonrpc": "^8.2.1",
|
|
27
|
+
"vscode-languageserver-protocol": "^3.17.5",
|
|
28
|
+
"vscode-languageserver-types": "^3.17.5",
|
|
29
|
+
"@mrclrchtr/supi-core": "1.7.0"
|
|
26
30
|
},
|
|
27
31
|
"bundledDependencies": [
|
|
28
|
-
"@mrclrchtr/supi-core"
|
|
32
|
+
"@mrclrchtr/supi-core",
|
|
33
|
+
"vscode-jsonrpc",
|
|
34
|
+
"vscode-languageserver-protocol",
|
|
35
|
+
"vscode-languageserver-types"
|
|
29
36
|
],
|
|
30
37
|
"peerDependencies": {
|
|
31
38
|
"@earendil-works/pi-ai": "*",
|
package/src/client/client.ts
CHANGED
|
@@ -173,7 +173,13 @@ export class LspClient {
|
|
|
173
173
|
setTimeout(() => reject(new Error("shutdown timeout")), SHUTDOWN_TIMEOUT_MS),
|
|
174
174
|
),
|
|
175
175
|
]);
|
|
176
|
-
|
|
176
|
+
// Flush the final exit notification before disposing the transport.
|
|
177
|
+
await Promise.race([
|
|
178
|
+
this.rpc.sendNotification("exit"),
|
|
179
|
+
new Promise((_, reject) =>
|
|
180
|
+
setTimeout(() => reject(new Error("exit notification timeout")), SHUTDOWN_TIMEOUT_MS),
|
|
181
|
+
),
|
|
182
|
+
]);
|
|
177
183
|
} catch {
|
|
178
184
|
// Timeout or error — force kill
|
|
179
185
|
}
|
|
@@ -333,10 +339,7 @@ export class LspClient {
|
|
|
333
339
|
|
|
334
340
|
/** Check if server supports pull diagnostics. */
|
|
335
341
|
get hasDiagnosticProvider(): boolean {
|
|
336
|
-
return
|
|
337
|
-
this.capabilities?.diagnosticProvider !== undefined &&
|
|
338
|
-
this.capabilities.diagnosticProvider !== false
|
|
339
|
-
);
|
|
342
|
+
return this.capabilities?.diagnosticProvider !== undefined;
|
|
340
343
|
}
|
|
341
344
|
|
|
342
345
|
/** Notify the server that watched workspace files changed. */
|
package/src/client/transport.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
// JSON-RPC 2.0 transport
|
|
1
|
+
// JSON-RPC 2.0 transport — thin wrapper around vscode-jsonrpc.
|
|
2
|
+
// Handles Content-Length framing, request/response correlation, timeouts,
|
|
3
|
+
// and notification/request dispatching through vscode-jsonrpc's MessageConnection.
|
|
2
4
|
|
|
3
5
|
import type { Readable, Writable } from "node:stream";
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
import {
|
|
7
|
+
CancellationTokenSource,
|
|
8
|
+
createMessageConnection,
|
|
9
|
+
type MessageConnection,
|
|
10
|
+
NullLogger,
|
|
11
|
+
ResponseError,
|
|
12
|
+
StreamMessageReader,
|
|
13
|
+
StreamMessageWriter,
|
|
14
|
+
} from "vscode-jsonrpc/node";
|
|
15
|
+
|
|
14
16
|
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
15
17
|
|
|
16
18
|
// ── Types ─────────────────────────────────────────────────────────────
|
|
@@ -18,29 +20,15 @@ const DEFAULT_TIMEOUT_MS = 30_000;
|
|
|
18
20
|
export type NotificationHandler = (method: string, params: unknown) => void;
|
|
19
21
|
export type RequestHandler = (method: string, params: unknown) => Promise<unknown> | unknown;
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
reject: (error: Error) => void;
|
|
24
|
-
timer: ReturnType<typeof setTimeout>;
|
|
25
|
-
}
|
|
23
|
+
/** Re-export ResponseError so callers don't need a separate vscode-jsonrpc import. */
|
|
24
|
+
const JsonRpcRequestError = ResponseError;
|
|
26
25
|
|
|
27
|
-
export
|
|
28
|
-
constructor(
|
|
29
|
-
readonly code: number,
|
|
30
|
-
message: string,
|
|
31
|
-
readonly data?: unknown,
|
|
32
|
-
) {
|
|
33
|
-
super(message);
|
|
34
|
-
this.name = "JsonRpcRequestError";
|
|
35
|
-
}
|
|
36
|
-
}
|
|
26
|
+
export { JsonRpcRequestError };
|
|
37
27
|
|
|
38
28
|
// ── JsonRpcClient ─────────────────────────────────────────────────────
|
|
39
29
|
|
|
40
30
|
export class JsonRpcClient {
|
|
41
|
-
private
|
|
42
|
-
private buffer = Buffer.alloc(0);
|
|
43
|
-
private pending = new Map<JsonRpcId, PendingRequest>();
|
|
31
|
+
private connection: MessageConnection | null = null;
|
|
44
32
|
private notificationHandler: NotificationHandler | null = null;
|
|
45
33
|
private requestHandler: RequestHandler | null = null;
|
|
46
34
|
private closed = false;
|
|
@@ -52,9 +40,31 @@ export class JsonRpcClient {
|
|
|
52
40
|
options?: { timeoutMs?: number },
|
|
53
41
|
) {
|
|
54
42
|
this.timeoutMs = options?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
43
|
+
|
|
44
|
+
const reader = new StreamMessageReader(this.input);
|
|
45
|
+
const writer = new StreamMessageWriter(this.output);
|
|
46
|
+
|
|
47
|
+
this.connection = createMessageConnection(reader, writer, NullLogger);
|
|
48
|
+
|
|
49
|
+
// Register catch-all notification handler
|
|
50
|
+
this.connection.onNotification((method, params) => {
|
|
51
|
+
this.notificationHandler?.(method, params);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Register catch-all request handler for server-initiated requests
|
|
55
|
+
this.connection.onRequest(async (method, params, _token) => {
|
|
56
|
+
if (!this.requestHandler) {
|
|
57
|
+
throw new JsonRpcRequestError(-32601, `Method not found: ${method}`);
|
|
58
|
+
}
|
|
59
|
+
return this.requestHandler(method, params);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Handle connection close
|
|
63
|
+
this.connection.onClose(() => {
|
|
64
|
+
this.closed = true;
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
this.connection.listen();
|
|
58
68
|
}
|
|
59
69
|
|
|
60
70
|
/** Register a handler for server notifications (no id). */
|
|
@@ -73,176 +83,55 @@ export class JsonRpcClient {
|
|
|
73
83
|
params?: unknown,
|
|
74
84
|
options?: { timeoutMs?: number },
|
|
75
85
|
): Promise<unknown> {
|
|
76
|
-
if (this.closed) {
|
|
86
|
+
if (this.closed || !this.connection) {
|
|
77
87
|
return Promise.reject(new Error("JSON-RPC client is closed"));
|
|
78
88
|
}
|
|
79
89
|
|
|
80
|
-
const id = this.nextId++;
|
|
81
90
|
const timeoutMs = options?.timeoutMs ?? this.timeoutMs;
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
const tokenSource = new CancellationTokenSource();
|
|
92
|
+
|
|
93
|
+
const timer = setTimeout(() => tokenSource.cancel(), timeoutMs);
|
|
94
|
+
|
|
95
|
+
const request = this.connection.sendRequest(method, params, tokenSource.token);
|
|
96
|
+
|
|
97
|
+
// Race the request against a timeout so callers don't hang forever.
|
|
98
|
+
// The CancellationToken is also passed to sendRequest so the connection
|
|
99
|
+
// can short-circuit writes and cleanup when the token fires.
|
|
100
|
+
const promise = Promise.race([
|
|
101
|
+
request,
|
|
102
|
+
new Promise<never>((_, reject) =>
|
|
103
|
+
setTimeout(
|
|
104
|
+
() => reject(new Error(`Request ${method} timed out after ${timeoutMs}ms`)),
|
|
105
|
+
timeoutMs,
|
|
106
|
+
),
|
|
107
|
+
),
|
|
108
|
+
]).finally(() => clearTimeout(timer));
|
|
109
|
+
|
|
110
|
+
// Prevent unhandled rejection when dispose() cancels requests
|
|
95
111
|
promise.catch(() => {});
|
|
96
112
|
return promise;
|
|
97
113
|
}
|
|
98
114
|
|
|
99
|
-
/**
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Send a notification (no response expected).
|
|
117
|
+
*
|
|
118
|
+
* Returns the underlying write promise so ordering-sensitive cleanup paths
|
|
119
|
+
* can await the final flush. A no-op catch is still attached to prevent
|
|
120
|
+
* unhandled rejections when callers intentionally fire-and-forget.
|
|
121
|
+
*/
|
|
122
|
+
sendNotification(method: string, params?: unknown): Promise<void> {
|
|
123
|
+
if (this.closed || !this.connection) return Promise.resolve();
|
|
124
|
+
const promise = this.connection.sendNotification(method, params);
|
|
125
|
+
promise.catch(() => {});
|
|
126
|
+
return promise;
|
|
104
127
|
}
|
|
105
128
|
|
|
106
|
-
/** Clean up
|
|
129
|
+
/** Clean up the connection. */
|
|
107
130
|
dispose(): void {
|
|
108
131
|
this.closed = true;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
this.pending.delete(id);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// ── Private ───────────────────────────────────────────────────────
|
|
117
|
-
|
|
118
|
-
private writeMessage(msg: JsonRpcMessage): void {
|
|
119
|
-
const body = JSON.stringify(msg);
|
|
120
|
-
const contentLength = Buffer.byteLength(body, "utf-8");
|
|
121
|
-
const header = `${CONTENT_LENGTH}${contentLength}${HEADER_DELIMITER}`;
|
|
122
|
-
this.output.write(header + body);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
private onData(chunk: Buffer): void {
|
|
126
|
-
this.buffer = Buffer.concat([this.buffer, chunk]);
|
|
127
|
-
this.processBuffer();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
private processBuffer(): void {
|
|
131
|
-
while (true) {
|
|
132
|
-
// Look for header delimiter
|
|
133
|
-
const headerEnd = this.buffer.indexOf(HEADER_DELIMITER);
|
|
134
|
-
if (headerEnd === -1) return;
|
|
135
|
-
|
|
136
|
-
// Parse Content-Length from headers
|
|
137
|
-
const headerText = this.buffer.subarray(0, headerEnd).toString("utf-8");
|
|
138
|
-
const contentLength = parseContentLength(headerText);
|
|
139
|
-
if (contentLength === null) {
|
|
140
|
-
// Malformed header — skip past delimiter and try again
|
|
141
|
-
this.buffer = this.buffer.subarray(headerEnd + HEADER_DELIMITER.length);
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// Check if we have the full body
|
|
146
|
-
const bodyStart = headerEnd + HEADER_DELIMITER.length;
|
|
147
|
-
const messageEnd = bodyStart + contentLength;
|
|
148
|
-
if (this.buffer.length < messageEnd) {
|
|
149
|
-
return; // Need more data — partial message
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Extract and parse the body
|
|
153
|
-
const body = this.buffer.subarray(bodyStart, messageEnd).toString("utf-8");
|
|
154
|
-
this.buffer = this.buffer.subarray(messageEnd);
|
|
155
|
-
|
|
156
|
-
try {
|
|
157
|
-
const msg = JSON.parse(body) as JsonRpcMessage;
|
|
158
|
-
this.handleMessage(msg);
|
|
159
|
-
} catch {
|
|
160
|
-
// Malformed JSON — skip
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
private handleMessage(msg: JsonRpcMessage): void {
|
|
166
|
-
// Response (has id, has result or error)
|
|
167
|
-
if ("id" in msg && msg.id != null && ("result" in msg || "error" in msg)) {
|
|
168
|
-
const response = msg as JsonRpcResponse;
|
|
169
|
-
const id = response.id;
|
|
170
|
-
if (id === null) return;
|
|
171
|
-
const pending = this.pending.get(id);
|
|
172
|
-
if (pending) {
|
|
173
|
-
this.pending.delete(id);
|
|
174
|
-
clearTimeout(pending.timer);
|
|
175
|
-
if (response.error) {
|
|
176
|
-
pending.reject(new Error(`LSP error ${response.error.code}: ${response.error.message}`));
|
|
177
|
-
} else {
|
|
178
|
-
pending.resolve(response.result);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// Notification (no id)
|
|
185
|
-
if ("method" in msg && !("id" in msg)) {
|
|
186
|
-
const notification = msg as JsonRpcNotification;
|
|
187
|
-
this.notificationHandler?.(notification.method, notification.params);
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// Request from server (has id + method)
|
|
192
|
-
if ("method" in msg && "id" in msg && msg.id != null) {
|
|
193
|
-
void this.handleInboundRequest(msg as JsonRpcRequest);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
private async handleInboundRequest(request: JsonRpcRequest): Promise<void> {
|
|
198
|
-
if (this.closed) return;
|
|
199
|
-
|
|
200
|
-
try {
|
|
201
|
-
if (!this.requestHandler) {
|
|
202
|
-
throw new JsonRpcRequestError(-32601, `Method not found: ${request.method}`);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const result = await this.requestHandler(request.method, request.params);
|
|
206
|
-
this.writeMessage({
|
|
207
|
-
jsonrpc: "2.0",
|
|
208
|
-
id: request.id,
|
|
209
|
-
result: result ?? null,
|
|
210
|
-
} satisfies JsonRpcResponse);
|
|
211
|
-
} catch (error) {
|
|
212
|
-
const failure =
|
|
213
|
-
error instanceof JsonRpcRequestError
|
|
214
|
-
? error
|
|
215
|
-
: new JsonRpcRequestError(-32603, error instanceof Error ? error.message : String(error));
|
|
216
|
-
this.writeMessage({
|
|
217
|
-
jsonrpc: "2.0",
|
|
218
|
-
id: request.id,
|
|
219
|
-
error: {
|
|
220
|
-
code: failure.code,
|
|
221
|
-
message: failure.message,
|
|
222
|
-
...(failure.data !== undefined ? { data: failure.data } : {}),
|
|
223
|
-
},
|
|
224
|
-
} satisfies JsonRpcResponse);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
private onClose(): void {
|
|
229
|
-
this.closed = true;
|
|
230
|
-
for (const [id, p] of this.pending) {
|
|
231
|
-
clearTimeout(p.timer);
|
|
232
|
-
p.reject(new Error("JSON-RPC connection closed"));
|
|
233
|
-
this.pending.delete(id);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// ── Helpers ───────────────────────────────────────────────────────────
|
|
239
|
-
|
|
240
|
-
function parseContentLength(header: string): number | null {
|
|
241
|
-
for (const line of header.split("\r\n")) {
|
|
242
|
-
if (line.startsWith(CONTENT_LENGTH)) {
|
|
243
|
-
const value = parseInt(line.slice(CONTENT_LENGTH.length), 10);
|
|
244
|
-
if (Number.isFinite(value) && value >= 0) return value;
|
|
132
|
+
if (this.connection) {
|
|
133
|
+
this.connection.dispose();
|
|
134
|
+
this.connection = null;
|
|
245
135
|
}
|
|
246
136
|
}
|
|
247
|
-
return null;
|
|
248
137
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// SuPi-specific server configuration types — not part of the LSP specification.
|
|
2
|
+
// These are our own types for server discovery, configuration, and status tracking.
|
|
3
|
+
|
|
4
|
+
export interface ServerConfig {
|
|
5
|
+
command: string;
|
|
6
|
+
args?: string[];
|
|
7
|
+
fileTypes: string[];
|
|
8
|
+
rootMarkers: string[];
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
initializationOptions?: unknown;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** LSP configuration keyed by language name (e.g. `typescript`, `python`). */
|
|
14
|
+
export interface LspConfig {
|
|
15
|
+
servers: Record<string, ServerConfig>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface DetectedProjectServer {
|
|
19
|
+
name: string;
|
|
20
|
+
root: string;
|
|
21
|
+
fileTypes: string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ProjectServerInfo extends DetectedProjectServer {
|
|
25
|
+
status: "running" | "error" | "unavailable";
|
|
26
|
+
supportedActions: string[];
|
|
27
|
+
openFiles: string[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A language whose source files are present but the server binary is missing. */
|
|
31
|
+
export interface MissingServer {
|
|
32
|
+
/** Language name (e.g. "python", "rust"). */
|
|
33
|
+
name: string;
|
|
34
|
+
/** Server command that was not found on PATH. */
|
|
35
|
+
command: string;
|
|
36
|
+
/** File extensions found in the project (subset of server.fileTypes). */
|
|
37
|
+
foundExtensions: string[];
|
|
38
|
+
}
|