@prompt-optimizer/mcp-server 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 +669 -0
- package/README.md +267 -0
- package/THIRD_PARTY_NOTICES.txt +4476 -0
- package/bin/prompt-optimizer-desktop-mcp.cjs +10 -0
- package/bin/prompt-optimizer-mcp.cjs +20 -0
- package/dist/browser.cjs +1 -0
- package/dist/browser.js +4202 -0
- package/dist/chunk-3FYJ3FRD.js +57 -0
- package/dist/chunk-E7Z4Y3F4.js +36402 -0
- package/dist/chunk-GBDJTSRW.js +298 -0
- package/dist/chunk-HLCN3DLS.js +19745 -0
- package/dist/chunk-HYVN7FZZ.js +4787 -0
- package/dist/chunk-MEUCFQGU.js +537 -0
- package/dist/chunk-N6AUTPPS.js +10647 -0
- package/dist/chunk-Q52SACHI.js +304 -0
- package/dist/chunk-Q5B3KFB3.js +31 -0
- package/dist/chunk-QSPNNVX3.js +49017 -0
- package/dist/chunk-SO4YJHYW.js +13450 -0
- package/dist/chunk-SV2GETJH.js +65 -0
- package/dist/chunk-TFH2LO2H.js +58 -0
- package/dist/chunk-TFJEYQ3G.js +786 -0
- package/dist/chunk-TLOFCHZI.js +70 -0
- package/dist/chunk-TQGHULC7.js +188 -0
- package/dist/chunk-UTOMPKTK.js +2209 -0
- package/dist/chunk-XXALDAL7.js +1838 -0
- package/dist/chunk-YT3U6B54.js +21 -0
- package/dist/desktop-business.cjs +1 -0
- package/dist/desktop-business.js +1227 -0
- package/dist/desktop-comparison-HDIQ6K45.js +22 -0
- package/dist/desktop-storage-YRABQCLF.js +14 -0
- package/dist/desktop.cjs +1 -0
- package/dist/desktop.js +26 -0
- package/dist/dist-O5VNBF5I.js +622 -0
- package/dist/dist-UMX52UME.js +481 -0
- package/dist/index.cjs +1 -0
- package/dist/index.js +12 -0
- package/dist/legacy.cjs +1 -0
- package/dist/legacy.js +26933 -0
- package/dist/multipart-parser-XE6WGDAU.js +373 -0
- package/dist/node-2RBNZOSK.js +2225 -0
- package/dist/node-4Z3USCHW.js +21 -0
- package/dist/node-QHGLD5ST.js +25 -0
- package/dist/registry-H5F4DGAM-UHR4F6LL.js +13 -0
- package/dist/src-7PBMYCGI.js +1398 -0
- package/dist/start.cjs +1 -0
- package/dist/start.js +16 -0
- package/package.json +30 -0
- package/preload-env.cjs +352 -0
- package/skills/prompt-optimizer-compare/SKILL.md +21 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { createRequire as __mcpCreateRequire } from "node:module";
|
|
2
|
+
const require = __mcpCreateRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
AssetReadError,
|
|
5
|
+
createReadonlyAssetReader
|
|
6
|
+
} from "./chunk-UTOMPKTK.js";
|
|
7
|
+
|
|
8
|
+
// src/desktop-router.ts
|
|
9
|
+
import { randomUUID } from "node:crypto";
|
|
10
|
+
import { isAbsolute, normalize } from "node:path";
|
|
11
|
+
var DesktopSourceError = class extends Error {
|
|
12
|
+
constructor(code) {
|
|
13
|
+
super(`Desktop source request failed (${code}).`);
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.name = "DesktopSourceError";
|
|
16
|
+
}
|
|
17
|
+
code;
|
|
18
|
+
};
|
|
19
|
+
var DesktopComparisonRequestError = class extends Error {
|
|
20
|
+
constructor(code) {
|
|
21
|
+
super(`Desktop comparison request failed (${code}).`);
|
|
22
|
+
this.code = code;
|
|
23
|
+
this.name = "DesktopComparisonRequestError";
|
|
24
|
+
}
|
|
25
|
+
code;
|
|
26
|
+
};
|
|
27
|
+
var identity = { scope: "mcp-process", sourceEpoch: "open-lease", inPlaceReplacementDetection: false, stableAcrossRestart: false };
|
|
28
|
+
var noOpener = async () => {
|
|
29
|
+
throw new DesktopSourceError("DESKTOP_OPENER_UNAVAILABLE");
|
|
30
|
+
};
|
|
31
|
+
function createDesktopRouter(profiles, accessMode, opener) {
|
|
32
|
+
if (!profiles.length || profiles.length > 8) throw new DesktopSourceError("INVALID_INPUT");
|
|
33
|
+
const paths = /* @__PURE__ */ new Set();
|
|
34
|
+
const sources = profiles.map((profile) => {
|
|
35
|
+
if (typeof profile.label !== "string" || !profile.label.trim() || !isAbsolute(profile.userDataPath)) {
|
|
36
|
+
throw new DesktopSourceError("INVALID_INPUT");
|
|
37
|
+
}
|
|
38
|
+
const path = normalize(profile.userDataPath);
|
|
39
|
+
const pathKey = process.platform === "win32" ? path.toLowerCase() : path;
|
|
40
|
+
if (paths.has(pathKey)) throw new DesktopSourceError("INVALID_INPUT");
|
|
41
|
+
paths.add(pathKey);
|
|
42
|
+
return { id: randomUUID(), profile: { label: profile.label, userDataPath: path } };
|
|
43
|
+
});
|
|
44
|
+
const connections = /* @__PURE__ */ new Map();
|
|
45
|
+
const leases = /* @__PURE__ */ new Set();
|
|
46
|
+
let disposed = false;
|
|
47
|
+
let disposal;
|
|
48
|
+
function sourceDescription(source) {
|
|
49
|
+
const connection = source.connection;
|
|
50
|
+
return {
|
|
51
|
+
sourceId: source.id,
|
|
52
|
+
kind: "desktop",
|
|
53
|
+
label: source.profile.label,
|
|
54
|
+
sourceEpoch: connection?.executionSource.sourceEpoch ?? "unobserved",
|
|
55
|
+
connectionGeneration: connection?.executionSource.connectionGeneration ?? "unconnected",
|
|
56
|
+
productVersion: "unknown",
|
|
57
|
+
contractVersion: "po-capabilities/0.2",
|
|
58
|
+
storageVersion: connection ? "legacy-json-migration/1" : "unknown",
|
|
59
|
+
availability: connection?.active ? "connected" : "disconnected",
|
|
60
|
+
readiness: connection?.active ? "ready" : "not-ready",
|
|
61
|
+
...source.failure ? { unavailableReason: source.failure } : {}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function assertOwner() {
|
|
65
|
+
if (disposed) throw new DesktopSourceError("OWNER_CLOSED");
|
|
66
|
+
}
|
|
67
|
+
function assertActive(connection) {
|
|
68
|
+
if (!connection?.active || disposed) throw new DesktopSourceError("CONNECTION_CLOSED");
|
|
69
|
+
}
|
|
70
|
+
function requireConnection(id) {
|
|
71
|
+
assertOwner();
|
|
72
|
+
const connection = connections.get(id);
|
|
73
|
+
assertActive(connection);
|
|
74
|
+
return connection;
|
|
75
|
+
}
|
|
76
|
+
async function releaseLease(lease, pending = /* @__PURE__ */ new Set()) {
|
|
77
|
+
try {
|
|
78
|
+
await lease.comparison?.runner.dispose();
|
|
79
|
+
} finally {
|
|
80
|
+
await Promise.allSettled([...pending]);
|
|
81
|
+
await lease.provider.close();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function closeConnection(connection) {
|
|
85
|
+
if (connection.closePromise) return connection.closePromise;
|
|
86
|
+
connection.active = false;
|
|
87
|
+
connections.delete(connection.id);
|
|
88
|
+
const source = sources.find((source2) => source2.connection === connection);
|
|
89
|
+
connection.closePromise = (async () => {
|
|
90
|
+
try {
|
|
91
|
+
await releaseLease(connection.lease, connection.pending);
|
|
92
|
+
} catch {
|
|
93
|
+
if (source) source.failure = "SOURCE_CLOSE_FAILED";
|
|
94
|
+
throw new DesktopSourceError("SOURCE_CLOSE_FAILED");
|
|
95
|
+
} finally {
|
|
96
|
+
leases.delete(connection);
|
|
97
|
+
if (source) source.connection = void 0;
|
|
98
|
+
}
|
|
99
|
+
})();
|
|
100
|
+
return connection.closePromise;
|
|
101
|
+
}
|
|
102
|
+
async function openSource(source) {
|
|
103
|
+
let lease;
|
|
104
|
+
try {
|
|
105
|
+
const executionSource = {
|
|
106
|
+
kind: "desktop",
|
|
107
|
+
sourceId: source.id,
|
|
108
|
+
label: source.profile.label,
|
|
109
|
+
sourceEpoch: `lease:${randomUUID()}`,
|
|
110
|
+
connectionGeneration: randomUUID()
|
|
111
|
+
};
|
|
112
|
+
lease = await opener(source.profile.userDataPath, executionSource);
|
|
113
|
+
if (lease.migration.version !== 1) throw new DesktopSourceError("SCHEMA_UNSUPPORTED");
|
|
114
|
+
if (accessMode === "compare" !== Boolean(lease.comparison)) throw new DesktopSourceError("SOURCE_NOT_READY");
|
|
115
|
+
assertOwner();
|
|
116
|
+
const connection = {
|
|
117
|
+
id: randomUUID(),
|
|
118
|
+
executionSource,
|
|
119
|
+
active: true,
|
|
120
|
+
lease,
|
|
121
|
+
pending: /* @__PURE__ */ new Set(),
|
|
122
|
+
reader: lease.comparison?.runner.reader ?? createReadonlyAssetReader(lease.provider, executionSource)
|
|
123
|
+
};
|
|
124
|
+
source.connection = connection;
|
|
125
|
+
source.failure = void 0;
|
|
126
|
+
connections.set(connection.id, connection);
|
|
127
|
+
leases.add(connection);
|
|
128
|
+
return connection;
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (lease) {
|
|
131
|
+
try {
|
|
132
|
+
await releaseLease(lease);
|
|
133
|
+
} catch {
|
|
134
|
+
source.failure = "SOURCE_CLOSE_FAILED";
|
|
135
|
+
throw new DesktopSourceError("SOURCE_CLOSE_FAILED");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const safe = error instanceof DesktopSourceError ? error : new DesktopSourceError("SOURCE_OPEN_FAILED");
|
|
139
|
+
source.failure = safe.code;
|
|
140
|
+
throw safe;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function useConnection(id, action) {
|
|
144
|
+
const connection = requireConnection(id);
|
|
145
|
+
let writeStarted = false;
|
|
146
|
+
const beginWrite = () => {
|
|
147
|
+
assertActive(connection);
|
|
148
|
+
writeStarted = true;
|
|
149
|
+
};
|
|
150
|
+
const work = Promise.resolve().then(() => {
|
|
151
|
+
assertActive(connection);
|
|
152
|
+
return action(connection, beginWrite);
|
|
153
|
+
});
|
|
154
|
+
connection.pending.add(work);
|
|
155
|
+
try {
|
|
156
|
+
const result = await work;
|
|
157
|
+
assertActive(connection);
|
|
158
|
+
return result;
|
|
159
|
+
} catch (error) {
|
|
160
|
+
if (!connection.active || disposed) {
|
|
161
|
+
throw new DesktopSourceError(writeStarted ? "WRITE_OUTCOME_UNKNOWN" : "CONNECTION_CLOSED");
|
|
162
|
+
}
|
|
163
|
+
const code = connection.lease.comparison?.errorCode(error);
|
|
164
|
+
if (code && /^[A-Z][A-Z0-9_]{0,63}$/.test(code)) throw new DesktopComparisonRequestError(code);
|
|
165
|
+
throw error;
|
|
166
|
+
} finally {
|
|
167
|
+
connection.pending.delete(work);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function withComparison(id, action) {
|
|
171
|
+
return useConnection(id, (connection, beginWrite) => {
|
|
172
|
+
const session = connection.lease.comparison;
|
|
173
|
+
if (!session) throw new DesktopSourceError("COMPARISON_ACCESS_REQUIRED");
|
|
174
|
+
return action(session, connection, beginWrite);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
getSourceProfile(sourceId) {
|
|
179
|
+
assertOwner();
|
|
180
|
+
return sources.find((source) => source.id === sourceId)?.profile;
|
|
181
|
+
},
|
|
182
|
+
listSources() {
|
|
183
|
+
assertOwner();
|
|
184
|
+
return { sources: sources.map(sourceDescription), identity };
|
|
185
|
+
},
|
|
186
|
+
async open(sourceId, access) {
|
|
187
|
+
assertOwner();
|
|
188
|
+
if (access !== accessMode) throw new DesktopSourceError("INVALID_INPUT");
|
|
189
|
+
const source = sources.find((source2) => source2.id === sourceId);
|
|
190
|
+
if (!source) throw new DesktopSourceError("SOURCE_NOT_FOUND");
|
|
191
|
+
if (source.failure === "SOURCE_CLOSE_FAILED") throw new DesktopSourceError("SOURCE_CLOSE_FAILED");
|
|
192
|
+
if (source.connection?.closePromise) await source.connection.closePromise;
|
|
193
|
+
assertOwner();
|
|
194
|
+
if (!source.connection) {
|
|
195
|
+
source.opening ??= openSource(source);
|
|
196
|
+
try {
|
|
197
|
+
await source.opening;
|
|
198
|
+
} finally {
|
|
199
|
+
source.opening = void 0;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const connection = source.connection;
|
|
203
|
+
assertActive(connection);
|
|
204
|
+
return {
|
|
205
|
+
connectionId: connection.id,
|
|
206
|
+
source: sourceDescription(source),
|
|
207
|
+
schemaVersion: "prompt-model/v1",
|
|
208
|
+
identity,
|
|
209
|
+
capabilities: { read: true, execute: accessMode === "compare", save: accessMode === "compare" }
|
|
210
|
+
};
|
|
211
|
+
},
|
|
212
|
+
async close(connectionId) {
|
|
213
|
+
await closeConnection(requireConnection(connectionId));
|
|
214
|
+
return { closed: true };
|
|
215
|
+
},
|
|
216
|
+
listModels(id, options) {
|
|
217
|
+
return useConnection(id, async ({ reader, lease }) => lease.comparison ? lease.comparison.runner.listModels(options) : reader.listModels(options));
|
|
218
|
+
},
|
|
219
|
+
listAssets(id, options) {
|
|
220
|
+
return useConnection(id, ({ reader }) => reader.listAssets(options));
|
|
221
|
+
},
|
|
222
|
+
listAssetVersions(id, selection, options) {
|
|
223
|
+
return useConnection(id, ({ reader }) => reader.listAssetVersions(selection, options));
|
|
224
|
+
},
|
|
225
|
+
getAssetVersion(id, selection) {
|
|
226
|
+
return useConnection(id, ({ reader }) => reader.getAssetVersion(selection));
|
|
227
|
+
},
|
|
228
|
+
prepare(id, input) {
|
|
229
|
+
return withComparison(id, ({ runner }) => runner.prepare(input));
|
|
230
|
+
},
|
|
231
|
+
execute(id, preparedId, previewRevision) {
|
|
232
|
+
return withComparison(id, ({ runner }) => runner.execute(preparedId, previewRevision));
|
|
233
|
+
},
|
|
234
|
+
getTest(id, preparedId) {
|
|
235
|
+
return withComparison(id, ({ runner }) => runner.get(preparedId));
|
|
236
|
+
},
|
|
237
|
+
cancel(id, preparedId, runId) {
|
|
238
|
+
return withComparison(id, ({ runner }) => runner.cancel(preparedId, runId));
|
|
239
|
+
},
|
|
240
|
+
saveComparison(id, preparedId, saveKey) {
|
|
241
|
+
return withComparison(id, async ({ runner, repository }, connection, beginWrite) => {
|
|
242
|
+
const comparison = await runner.completed(preparedId);
|
|
243
|
+
beginWrite();
|
|
244
|
+
const saved = await repository.save(comparison, saveKey);
|
|
245
|
+
const comparisonId = saved.comparison.id;
|
|
246
|
+
return {
|
|
247
|
+
saved: true,
|
|
248
|
+
comparisonId,
|
|
249
|
+
revision: saved.revision,
|
|
250
|
+
viewTarget: { kind: "saved-comparison", sourceId: connection.executionSource.sourceId, comparisonId },
|
|
251
|
+
visibility: "reload-required"
|
|
252
|
+
};
|
|
253
|
+
});
|
|
254
|
+
},
|
|
255
|
+
listComparisons(id, options) {
|
|
256
|
+
return withComparison(id, ({ repository }) => repository.list(options));
|
|
257
|
+
},
|
|
258
|
+
getComparison(id, comparisonId) {
|
|
259
|
+
return withComparison(id, ({ repository }) => repository.get(comparisonId));
|
|
260
|
+
},
|
|
261
|
+
deleteComparison(id, comparisonId, expectedRevision) {
|
|
262
|
+
return withComparison(id, ({ repository }, _connection, beginWrite) => {
|
|
263
|
+
beginWrite();
|
|
264
|
+
return repository.delete(comparisonId, expectedRevision);
|
|
265
|
+
});
|
|
266
|
+
},
|
|
267
|
+
dispose() {
|
|
268
|
+
if (disposal) return disposal;
|
|
269
|
+
disposed = true;
|
|
270
|
+
disposal = (async () => {
|
|
271
|
+
const active = Promise.allSettled([...leases].map(closeConnection));
|
|
272
|
+
const [, results] = await Promise.all([Promise.allSettled(sources.map((source) => source.opening)), active]);
|
|
273
|
+
if (results.some((result) => result.status === "rejected") || sources.some((source) => source.failure === "SOURCE_CLOSE_FAILED")) {
|
|
274
|
+
throw new DesktopSourceError("SOURCE_CLOSE_FAILED");
|
|
275
|
+
}
|
|
276
|
+
})();
|
|
277
|
+
return disposal;
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
function createDesktopReadonlyRouter(profiles, opener = noOpener) {
|
|
282
|
+
return createDesktopRouter(profiles, "read", (path) => opener(path));
|
|
283
|
+
}
|
|
284
|
+
function createDesktopComparisonRouter(profiles, opener) {
|
|
285
|
+
return createDesktopRouter(profiles, "compare", opener);
|
|
286
|
+
}
|
|
287
|
+
function publicDesktopError(error, access = "read") {
|
|
288
|
+
const code = error instanceof DesktopSourceError || error instanceof AssetReadError || error instanceof DesktopComparisonRequestError ? error.code : access === "compare" ? "COMPARISON_REQUEST_FAILED" : "SOURCE_READ_FAILED";
|
|
289
|
+
const scope = access === "compare" ? "comparison" : "readonly";
|
|
290
|
+
return { code, message: `Desktop ${scope} request failed (${code}).` };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export {
|
|
294
|
+
DesktopSourceError,
|
|
295
|
+
createDesktopReadonlyRouter,
|
|
296
|
+
createDesktopComparisonRouter,
|
|
297
|
+
publicDesktopError
|
|
298
|
+
};
|