@oh-my-pi/pi-utils 17.2.9 → 17.2.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/CHANGELOG.md +21 -0
- package/dist/types/acp/connection.d.ts +118 -0
- package/dist/types/acp/protocol.d.ts +526 -0
- package/dist/types/acp/schema.d.ts +41 -0
- package/dist/types/acp/stream.d.ts +8 -0
- package/dist/types/acp/transport.d.ts +81 -0
- package/dist/types/acp.d.ts +6 -0
- package/dist/types/browsers.d.ts +68 -0
- package/dist/types/chalk.d.ts +125 -0
- package/dist/types/dates.d.ts +7 -0
- package/dist/types/docx/converter.d.ts +46 -0
- package/dist/types/docx/xml.d.ts +26 -0
- package/dist/types/docx/zip.d.ts +6 -0
- package/dist/types/docx.d.ts +11 -0
- package/dist/types/dom/core.d.ts +431 -0
- package/dist/types/dom/parser.d.ts +7 -0
- package/dist/types/dom/selector.d.ts +5 -0
- package/dist/types/dom.d.ts +5 -0
- package/dist/types/headers.d.ts +34 -0
- package/dist/types/logger/rotating-file.d.ts +18 -0
- package/dist/types/lru.d.ts +46 -0
- package/dist/types/marked/core.d.ts +445 -0
- package/dist/types/marked.d.ts +2 -0
- package/dist/types/postmortem.d.ts +14 -0
- package/dist/types/procmgr.d.ts +16 -0
- package/dist/types/prompt.d.ts +2 -2
- package/dist/types/readability/readability.d.ts +9 -0
- package/dist/types/readability/readerable.d.ts +10 -0
- package/dist/types/readability/types.d.ts +70 -0
- package/dist/types/readability.d.ts +4 -0
- package/dist/types/template.d.ts +62 -0
- package/dist/types/turndown/gfm.d.ts +11 -0
- package/dist/types/turndown/html.d.ts +5 -0
- package/dist/types/turndown/service.d.ts +21 -0
- package/dist/types/turndown/types.d.ts +70 -0
- package/dist/types/turndown.d.ts +4 -0
- package/dist/types/vterm/buffer.d.ts +99 -0
- package/dist/types/vterm/terminal.d.ts +44 -0
- package/dist/types/vterm.d.ts +8 -0
- package/dist/types/xml.d.ts +31 -0
- package/package.json +2 -5
- package/src/acp/connection.ts +344 -0
- package/src/acp/protocol.ts +466 -0
- package/src/acp/schema.ts +160 -0
- package/src/acp/stream.ts +82 -0
- package/src/acp/transport.ts +213 -0
- package/src/acp.ts +6 -0
- package/src/browsers.ts +501 -0
- package/src/chalk.ts +312 -0
- package/src/dates.ts +194 -0
- package/src/docx/converter.ts +681 -0
- package/src/docx/xml.ts +166 -0
- package/src/docx/zip.ts +87 -0
- package/src/docx.ts +20 -0
- package/src/dom/core.ts +1254 -0
- package/src/dom/parser.ts +370 -0
- package/src/dom/selector.ts +290 -0
- package/src/dom.ts +33 -0
- package/src/headers.ts +167 -0
- package/src/logger/rotating-file.ts +149 -0
- package/src/logger.ts +12 -28
- package/src/lru.ts +185 -0
- package/src/marked/core.ts +1576 -0
- package/src/marked.ts +2 -0
- package/src/postmortem.ts +44 -1
- package/src/procmgr.ts +21 -4
- package/src/prompt.ts +5 -22
- package/src/readability/readability.ts +533 -0
- package/src/readability/readerable.ts +51 -0
- package/src/readability/types.ts +72 -0
- package/src/readability.ts +11 -0
- package/src/template.ts +586 -0
- package/src/turndown/gfm.ts +106 -0
- package/src/turndown/html.ts +257 -0
- package/src/turndown/service.ts +334 -0
- package/src/turndown/types.ts +81 -0
- package/src/turndown.ts +5 -0
- package/src/vterm/buffer.ts +218 -0
- package/src/vterm/terminal.ts +773 -0
- package/src/vterm.ts +8 -0
- package/src/xml.ts +313 -0
- package/src/winston-daily-rotate-file.d.ts +0 -6
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import type { MaybePromise } from "./protocol";
|
|
2
|
+
import type { Stream } from "./stream";
|
|
3
|
+
|
|
4
|
+
/** JSON-RPC request identifier. */
|
|
5
|
+
export type JsonRpcId = string | number | null;
|
|
6
|
+
/** JSON-RPC request. */
|
|
7
|
+
export interface AnyRequest {
|
|
8
|
+
jsonrpc: "2.0";
|
|
9
|
+
id: JsonRpcId;
|
|
10
|
+
method: string;
|
|
11
|
+
params?: unknown;
|
|
12
|
+
}
|
|
13
|
+
/** JSON-RPC notification. */
|
|
14
|
+
export interface AnyNotification {
|
|
15
|
+
jsonrpc: "2.0";
|
|
16
|
+
method: string;
|
|
17
|
+
params?: unknown;
|
|
18
|
+
}
|
|
19
|
+
/** JSON-RPC error payload. */
|
|
20
|
+
export interface ErrorResponse {
|
|
21
|
+
code: number;
|
|
22
|
+
message: string;
|
|
23
|
+
data?: unknown;
|
|
24
|
+
}
|
|
25
|
+
/** JSON-RPC response. */
|
|
26
|
+
export type AnyResponse = { jsonrpc: "2.0"; id: JsonRpcId } & ({ result: unknown } | { error: ErrorResponse });
|
|
27
|
+
/** Any JSON-RPC wire message. */
|
|
28
|
+
export type AnyMessage = AnyRequest | AnyNotification | AnyResponse;
|
|
29
|
+
|
|
30
|
+
/** Error returned by a JSON-RPC peer or handler. */
|
|
31
|
+
export class RequestError extends Error {
|
|
32
|
+
/** JSON-RPC error code. */
|
|
33
|
+
readonly code: number;
|
|
34
|
+
/** Optional protocol error data. */
|
|
35
|
+
readonly data?: unknown;
|
|
36
|
+
|
|
37
|
+
constructor(code: number, message: string, data?: unknown) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "RequestError";
|
|
40
|
+
this.code = code;
|
|
41
|
+
this.data = data;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Creates a JSON parse error. */
|
|
45
|
+
static parseError(data?: unknown, additionalMessage?: string): RequestError {
|
|
46
|
+
return createStandardError(-32700, "Parse error", data, additionalMessage);
|
|
47
|
+
}
|
|
48
|
+
/** Creates an invalid-request error. */
|
|
49
|
+
static invalidRequest(data?: unknown, additionalMessage?: string): RequestError {
|
|
50
|
+
return createStandardError(-32600, "Invalid request", data, additionalMessage);
|
|
51
|
+
}
|
|
52
|
+
/** Creates a method-not-found error. */
|
|
53
|
+
static methodNotFound(method: string): RequestError {
|
|
54
|
+
return new RequestError(-32601, `"Method not found": ${method}`, { method });
|
|
55
|
+
}
|
|
56
|
+
/** Creates an invalid-parameters error. */
|
|
57
|
+
static invalidParams(data?: unknown, additionalMessage?: string): RequestError {
|
|
58
|
+
return createStandardError(-32602, "Invalid params", data, additionalMessage);
|
|
59
|
+
}
|
|
60
|
+
/** Creates an internal error. */
|
|
61
|
+
static internalError(data?: unknown, additionalMessage?: string): RequestError {
|
|
62
|
+
return createStandardError(-32603, "Internal error", data, additionalMessage);
|
|
63
|
+
}
|
|
64
|
+
/** Creates a cancellation error. */
|
|
65
|
+
static requestCancelled(data?: unknown, additionalMessage?: string): RequestError {
|
|
66
|
+
return createStandardError(-32800, "Request cancelled", data, additionalMessage);
|
|
67
|
+
}
|
|
68
|
+
/** Creates an authentication-required error. */
|
|
69
|
+
static authRequired(data?: unknown, additionalMessage?: string): RequestError {
|
|
70
|
+
return createStandardError(-32000, "Authentication required", data, additionalMessage);
|
|
71
|
+
}
|
|
72
|
+
/** Creates a resource-not-found error. */
|
|
73
|
+
static resourceNotFound(uri?: string): RequestError {
|
|
74
|
+
return new RequestError(
|
|
75
|
+
-32002,
|
|
76
|
+
uri === undefined ? "Resource not found" : `Resource not found: ${uri}`,
|
|
77
|
+
uri === undefined ? undefined : { uri },
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
/** Converts this error into a JSON-RPC result. */
|
|
81
|
+
toResult(): { error: ErrorResponse } {
|
|
82
|
+
return { error: this.toErrorResponse() };
|
|
83
|
+
}
|
|
84
|
+
/** Converts this error into a JSON-RPC error payload. */
|
|
85
|
+
toErrorResponse(): ErrorResponse {
|
|
86
|
+
return { code: this.code, message: this.message, ...(this.data === undefined ? {} : { data: this.data }) };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function createStandardError(
|
|
91
|
+
code: number,
|
|
92
|
+
message: string,
|
|
93
|
+
data: unknown,
|
|
94
|
+
additionalMessage: string | undefined,
|
|
95
|
+
): RequestError {
|
|
96
|
+
return new RequestError(code, additionalMessage ? `${message}: ${additionalMessage}` : message, data);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
type Dispatcher = (method: string, params: unknown, notification: boolean) => MaybePromise<unknown>;
|
|
100
|
+
type Pending = { resolve(value: unknown): void; reject(reason: unknown): void };
|
|
101
|
+
|
|
102
|
+
/** Correlated bidirectional JSON-RPC connection. */
|
|
103
|
+
export class RpcConnection {
|
|
104
|
+
#nextId = 0;
|
|
105
|
+
#pending = new Map<JsonRpcId, Pending>();
|
|
106
|
+
#writable: WritableStream<AnyMessage>;
|
|
107
|
+
#writeTail: Promise<void> = Promise.resolve();
|
|
108
|
+
#abort = new AbortController();
|
|
109
|
+
#closed = Promise.withResolvers<void>();
|
|
110
|
+
#dispatcher: Dispatcher;
|
|
111
|
+
|
|
112
|
+
constructor(stream: Stream, dispatcher: Dispatcher) {
|
|
113
|
+
this.#writable = stream.writable;
|
|
114
|
+
this.#dispatcher = dispatcher;
|
|
115
|
+
void this.#read(stream.readable);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Signal aborted when the stream closes. */
|
|
119
|
+
get signal(): AbortSignal {
|
|
120
|
+
return this.#abort.signal;
|
|
121
|
+
}
|
|
122
|
+
/** Promise resolved when the stream closes. */
|
|
123
|
+
get closed(): Promise<void> {
|
|
124
|
+
return this.#closed.promise;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Sends a correlated JSON-RPC request. */
|
|
128
|
+
request<Response>(method: string, params?: unknown): Promise<Response> {
|
|
129
|
+
if (this.signal.aborted) return Promise.reject(new Error("Connection closed"));
|
|
130
|
+
const id = this.#nextId++;
|
|
131
|
+
const deferred = Promise.withResolvers<unknown>();
|
|
132
|
+
this.#pending.set(id, deferred);
|
|
133
|
+
void this.#write({ jsonrpc: "2.0", id, method, ...(params === undefined ? {} : { params }) }).catch(error => {
|
|
134
|
+
this.#pending.delete(id);
|
|
135
|
+
deferred.reject(error);
|
|
136
|
+
});
|
|
137
|
+
return deferred.promise as Promise<Response>;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Sends a JSON-RPC notification. */
|
|
141
|
+
async notify(method: string, params?: unknown): Promise<void> {
|
|
142
|
+
await this.#write({ jsonrpc: "2.0", method, ...(params === undefined ? {} : { params }) });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Closes the connection and rejects outstanding requests. */
|
|
146
|
+
close(error?: unknown): void {
|
|
147
|
+
if (this.signal.aborted) return;
|
|
148
|
+
this.#abort.abort(error);
|
|
149
|
+
for (const pending of this.#pending.values()) pending.reject(error ?? new Error("Connection closed"));
|
|
150
|
+
this.#pending.clear();
|
|
151
|
+
this.#closed.resolve();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
#write(message: AnyMessage): Promise<void> {
|
|
155
|
+
const write = this.#writeTail.then(async () => {
|
|
156
|
+
const writer = this.#writable.getWriter();
|
|
157
|
+
try {
|
|
158
|
+
await writer.write(message);
|
|
159
|
+
} finally {
|
|
160
|
+
writer.releaseLock();
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
this.#writeTail = write.catch(() => {});
|
|
164
|
+
return write;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async #read(readable: ReadableStream<AnyMessage>): Promise<void> {
|
|
168
|
+
const reader = readable.getReader();
|
|
169
|
+
try {
|
|
170
|
+
while (true) {
|
|
171
|
+
const next = await reader.read();
|
|
172
|
+
if (next.done) break;
|
|
173
|
+
void this.#handle(next.value).catch(error => this.close(error));
|
|
174
|
+
}
|
|
175
|
+
this.close();
|
|
176
|
+
} catch (error) {
|
|
177
|
+
this.close(error);
|
|
178
|
+
} finally {
|
|
179
|
+
reader.releaseLock();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async #handle(message: AnyMessage): Promise<void> {
|
|
184
|
+
if ("id" in message && !("method" in message)) {
|
|
185
|
+
const pending = this.#pending.get(message.id);
|
|
186
|
+
if (!pending) return;
|
|
187
|
+
this.#pending.delete(message.id);
|
|
188
|
+
if ("error" in message)
|
|
189
|
+
pending.reject(new RequestError(message.error.code, message.error.message, message.error.data));
|
|
190
|
+
else pending.resolve(message.result);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (!("method" in message)) return;
|
|
194
|
+
if (!("id" in message)) {
|
|
195
|
+
try {
|
|
196
|
+
await this.#dispatcher(message.method, message.params, true);
|
|
197
|
+
} catch {
|
|
198
|
+
/* Notifications have no error channel. */
|
|
199
|
+
}
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
const result = await this.#dispatcher(message.method, message.params, false);
|
|
204
|
+
await this.#write({ jsonrpc: "2.0", id: message.id, result: result ?? {} });
|
|
205
|
+
} catch (error) {
|
|
206
|
+
const protocolError =
|
|
207
|
+
error instanceof RequestError
|
|
208
|
+
? error
|
|
209
|
+
: RequestError.internalError({ details: error instanceof Error ? error.message : String(error) });
|
|
210
|
+
await this.#write({ jsonrpc: "2.0", id: message.id, error: protocolError.toErrorResponse() });
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
package/src/acp.ts
ADDED