@hop-top/kit 0.5.0-alpha.12 → 0.5.0-alpha.14
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 +51 -0
- package/README.md +5 -5
- package/dist/bus-D2bAOPYz.d.ts +55 -0
- package/dist/bus.d.ts +1 -0
- package/dist/bus.js +13 -0
- package/dist/chunk-74E6RNOI.js +82 -0
- package/dist/chunk-B7XTFDBB.js +17 -0
- package/dist/chunk-CAONBSPO.js +497 -0
- package/dist/chunk-F7RSL4N4.js +88 -0
- package/dist/chunk-I6XDPSXQ.js +75 -0
- package/dist/chunk-IDM6YZXN.js +204 -0
- package/dist/{chunk-BXJ2J27B.js → chunk-IGKBRNEW.js} +10 -10
- package/dist/{chunk-VWGP4NVJ.js → chunk-MDVGMOZO.js} +45 -39
- package/dist/{chunk-4FMALWYB.js → chunk-MEF7W3MG.js} +45 -45
- package/dist/{chunk-VGOA5Y7S.js → chunk-Q547MHNB.js} +11 -11
- package/dist/{chunk-K7E34Q3A.js → chunk-TUU2BY5I.js} +136 -47
- package/dist/chunk-VKQCOVKI.js +880 -0
- package/dist/cli.js +3 -3
- package/dist/index-BqZ7WGQf.d.ts +731 -0
- package/dist/index.d.ts +8 -5
- package/dist/index.js +32 -18
- package/dist/{llm-DYuR7_uX.d.ts → llm-BazFKc_2.d.ts} +95 -9
- package/dist/{llm-anthropic-Cd_I2xXB.d.ts → llm-anthropic-BcAitoDV.d.ts} +1 -1
- package/dist/llm-anthropic.d.ts +2 -2
- package/dist/llm-anthropic.js +4 -4
- package/dist/{llm-openai-DyFrucmV.d.ts → llm-openai-CSDvuRH6.d.ts} +1 -1
- package/dist/llm-openai.d.ts +2 -2
- package/dist/llm-openai.js +4 -4
- package/dist/llm.d.ts +1 -1
- package/dist/llm.js +8 -2
- package/dist/{routellm-B0nv59mH.d.ts → routellm-B9UrONPr.d.ts} +1 -1
- package/dist/routellm.d.ts +2 -2
- package/dist/routellm.js +5 -5
- package/dist/upgrade/drivers/configfile.d.ts +45 -0
- package/dist/upgrade/drivers/configfile.js +78 -0
- package/dist/upgrade/drivers/fsdir.d.ts +61 -0
- package/dist/upgrade/drivers/fsdir.js +170 -0
- package/dist/upgrade/drivers/postgres.d.ts +121 -0
- package/dist/upgrade/drivers/postgres.js +195 -0
- package/dist/upgrade/drivers/sqlite.d.ts +101 -0
- package/dist/upgrade/drivers/sqlite.js +241 -0
- package/dist/upgrade/index.d.ts +2 -0
- package/dist/upgrade/index.js +73 -0
- package/dist/uri-IJ_AoY3d.d.ts +144 -0
- package/dist/uri.d.ts +1 -0
- package/dist/uri.js +37 -0
- package/dist/version-DEJ3pbiO.d.ts +10 -0
- package/dist/wizard-Bf_qHc31.d.ts +159 -0
- package/dist/wizard.d.ts +1 -0
- package/dist/wizard.js +39 -0
- package/package.json +76 -17
- package/skills/hop-top-kit-ts/.claude-plugin/plugin.json +19 -0
- package/skills/hop-top-kit-ts/SKILL.md +117 -0
- package/skills/hop-top-kit-ts/assets/snippets/mcp/mcp.expected +2 -0
- package/skills/hop-top-kit-ts/assets/snippets/mcp/mcp.ts +20 -0
- package/skills/hop-top-kit-ts/assets/snippets/quickstart/quickstart.expected +13 -0
- package/skills/hop-top-kit-ts/assets/snippets/quickstart/quickstart.ts +26 -0
- package/skills/hop-top-kit-ts/gym.json +5 -0
- package/skills/hop-top-kit-ts/references/ai-and-agents.md +45 -0
- package/skills/hop-top-kit-ts/references/cli-and-console.md +63 -0
- package/skills/hop-top-kit-ts/references/coverage.md +56 -0
- package/skills/hop-top-kit-ts/references/guardrails-and-trust.md +60 -0
- package/skills/hop-top-kit-ts/references/other-ports.md +31 -0
- package/skills/hop-top-kit-ts/references/serving-and-transports.md +49 -0
- package/skills/hop-top-kit-ts/scripts/detect-port.sh +168 -0
- package/src/index.test.ts +48 -2
- package/src/index.ts +6 -1
- package/src/llm-anthropic.conformance.test.ts +7 -0
- package/src/llm-boundkey.test.ts +260 -0
- package/src/llm-choice-hermetic.test.ts +1 -1
- package/src/llm-config.test.ts +2 -4
- package/src/llm-config.ts +5 -1
- package/src/llm-endpoint.ts +25 -5
- package/src/llm-keys.ts +47 -15
- package/src/llm-openai.test.ts +10 -4
- package/src/llm-openai.ts +7 -1
- package/src/llm-spec.test.ts +347 -0
- package/src/llm-spec.ts +80 -0
- package/src/llm-uri.ts +96 -12
- package/src/llm.test.ts +2 -2
- package/src/llm.ts +10 -0
- package/src/published-types.test.ts +8 -0
- package/src/serve.stopped-order.test.ts +156 -0
- package/src/upgrade/README.md +42 -0
- package/src/upgrade/backup.ts +174 -0
- package/src/upgrade/conformance.test.ts +310 -0
- package/src/upgrade/document.ts +229 -0
- package/src/upgrade/driver.ts +66 -0
- package/src/upgrade/drivers/README.md +47 -0
- package/src/upgrade/drivers/configfile.test.ts +112 -0
- package/src/upgrade/drivers/configfile.ts +107 -0
- package/src/upgrade/drivers/fsdir.test.ts +188 -0
- package/src/upgrade/drivers/fsdir.ts +216 -0
- package/src/upgrade/drivers/isolation.test.ts +18 -0
- package/src/upgrade/drivers/postgres.integration.test.ts +395 -0
- package/src/upgrade/drivers/postgres.test.ts +272 -0
- package/src/upgrade/drivers/postgres.ts +284 -0
- package/src/upgrade/drivers/sqlite.integration.test.ts +492 -0
- package/src/upgrade/drivers/sqlite.ts +300 -0
- package/src/upgrade/drivers/version.ts +21 -0
- package/src/upgrade/errors.ts +117 -0
- package/src/upgrade/events.ts +117 -0
- package/src/{upgrade.test.ts → upgrade/index.test.ts} +1 -1
- package/src/{upgrade.ts → upgrade/index.ts} +20 -0
- package/src/upgrade/migration.ts +89 -0
- package/src/upgrade/migrator.test.ts +391 -0
- package/src/upgrade/migrator.ts +527 -0
- package/src/upgrade/options.ts +59 -0
- package/src/upgrade/semver.ts +90 -0
- package/test/llm/conformance-helpers.ts +3 -0
- package/test/skill-snippets.test.ts +60 -0
- package/test/upgrade/conformance-migrator.ts +439 -0
- package/test/upgrade/conformance-support.ts +44 -0
- package/test/upgrade/driver-support.ts +44 -0
- package/test/upgrade/pg-container.ts +64 -0
- package/test/upgrade/pg-race-worker.ts +48 -0
- package/test/upgrade/race-helper.ts +36 -0
- package/test/upgrade/sqlite-support.ts +41 -0
- package/test/upgrade/testdata/go-fsdir-mydata.tar.gz +0 -0
- package/vitest.config.ts +26 -0
- package/dist/chunk-X4E5QGEB.js +0 -171
- package/dist/upgrade-BJ2z6Oqf.d.ts +0 -156
- package/dist/upgrade.d.ts +0 -1
- package/dist/upgrade.js +0 -9
- package/dist/{chunk-MFVUENZR.js → chunk-KV7FYEAF.js} +2 -2
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkI6XDPSXQjs = require('./chunk-I6XDPSXQ.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunk4UE5TCMQjs = require('./chunk-4UE5TCMQ.js');
|
|
8
|
+
|
|
9
|
+
// src/upgrade/semver.ts
|
|
10
|
+
function leadingInt(s) {
|
|
11
|
+
const m = /^\d+/.exec(s);
|
|
12
|
+
return m ? Number(m[0]) : 0;
|
|
13
|
+
}
|
|
14
|
+
function parseSemver(s) {
|
|
15
|
+
const v = { major: 0, minor: 0, patch: 0, pre: "" };
|
|
16
|
+
let rest = s;
|
|
17
|
+
const plus = rest.indexOf("+");
|
|
18
|
+
if (plus >= 0) rest = rest.slice(0, plus);
|
|
19
|
+
const dash = rest.indexOf("-");
|
|
20
|
+
if (dash >= 0) {
|
|
21
|
+
v.pre = rest.slice(dash + 1);
|
|
22
|
+
rest = rest.slice(0, dash);
|
|
23
|
+
}
|
|
24
|
+
const parts = rest.split(".");
|
|
25
|
+
v.major = leadingInt(_nullishCoalesce(parts[0], () => ( "")));
|
|
26
|
+
v.minor = leadingInt(_nullishCoalesce(parts[1], () => ( "")));
|
|
27
|
+
v.patch = leadingInt(parts.slice(2).join("."));
|
|
28
|
+
return v;
|
|
29
|
+
}
|
|
30
|
+
function sign(n) {
|
|
31
|
+
return n < 0 ? -1 : n > 0 ? 1 : 0;
|
|
32
|
+
}
|
|
33
|
+
var NUMERIC = /^(0|[1-9]\d*)$/;
|
|
34
|
+
function comparePreIdent(a, b) {
|
|
35
|
+
const aNum = NUMERIC.test(a);
|
|
36
|
+
const bNum = NUMERIC.test(b);
|
|
37
|
+
if (aNum && bNum) return sign(Number(a) - Number(b));
|
|
38
|
+
if (aNum) return -1;
|
|
39
|
+
if (bNum) return 1;
|
|
40
|
+
return Buffer.compare(Buffer.from(a, "utf8"), Buffer.from(b, "utf8"));
|
|
41
|
+
}
|
|
42
|
+
function comparePreRelease(a, b) {
|
|
43
|
+
const ap = a.split(".");
|
|
44
|
+
const bp = b.split(".");
|
|
45
|
+
const n = Math.min(ap.length, bp.length);
|
|
46
|
+
for (let i = 0; i < n; i++) {
|
|
47
|
+
const d = comparePreIdent(ap[i], bp[i]);
|
|
48
|
+
if (d !== 0) return d;
|
|
49
|
+
}
|
|
50
|
+
return sign(ap.length - bp.length);
|
|
51
|
+
}
|
|
52
|
+
function compareSemver(a, b) {
|
|
53
|
+
const d = a.major - b.major || a.minor - b.minor || a.patch - b.patch;
|
|
54
|
+
if (d !== 0) return sign(d);
|
|
55
|
+
if (a.pre === "" && b.pre === "") return 0;
|
|
56
|
+
if (a.pre === "") return 1;
|
|
57
|
+
if (b.pre === "") return -1;
|
|
58
|
+
return sign(comparePreRelease(a.pre, b.pre));
|
|
59
|
+
}
|
|
60
|
+
function compareVersions(a, b) {
|
|
61
|
+
return compareSemver(parseSemver(trimV(a)), parseSemver(trimV(b)));
|
|
62
|
+
}
|
|
63
|
+
function trimV(s) {
|
|
64
|
+
return s.startsWith("v") ? s.slice(1) : s;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/upgrade/backup.ts
|
|
68
|
+
var _fs = require('fs');
|
|
69
|
+
var _path = require('path'); var path = _interopRequireWildcard(_path);
|
|
70
|
+
var DEFAULT_RETENTION = 5;
|
|
71
|
+
var PRE_RUN_SUFFIX = ".version";
|
|
72
|
+
function defaultDataDir(tool) {
|
|
73
|
+
return _chunk4UE5TCMQjs.dataDir.call(void 0, path.join("hop", tool));
|
|
74
|
+
}
|
|
75
|
+
async function readVersionFile(dataDir2, schema) {
|
|
76
|
+
try {
|
|
77
|
+
return (await _fs.promises.readFile(path.join(dataDir2, schema, "version"), "utf8")).trim();
|
|
78
|
+
} catch (e) {
|
|
79
|
+
if (e.code === "ENOENT") return "";
|
|
80
|
+
throw e;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async function writeVersionFile(dataDir2, schema, version) {
|
|
84
|
+
const dir = path.join(dataDir2, schema);
|
|
85
|
+
try {
|
|
86
|
+
await _fs.promises.mkdir(dir, { recursive: true, mode: 488 });
|
|
87
|
+
} catch (e) {
|
|
88
|
+
throw new Error(`create version dir: ${e.message}`, { cause: e });
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
await _fs.promises.writeFile(path.join(dir, "version"), version, { mode: 384 });
|
|
92
|
+
} catch (e) {
|
|
93
|
+
throw new Error(`write version file: ${e.message}`, { cause: e });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async function backupDirs(schemaDir) {
|
|
97
|
+
const entries = await _fs.promises.readdir(schemaDir, { withFileTypes: true });
|
|
98
|
+
const dirs = [];
|
|
99
|
+
for (const e of entries) {
|
|
100
|
+
if (!e.isDirectory() || !e.name.startsWith("pre-")) continue;
|
|
101
|
+
dirs.push({ name: e.name, v: parseSemver(trimV(e.name.slice("pre-".length))) });
|
|
102
|
+
}
|
|
103
|
+
return dirs.sort((a, b) => compareSemver(b.v, a.v));
|
|
104
|
+
}
|
|
105
|
+
var isMissing = (e) => e.code === "ENOENT";
|
|
106
|
+
var BackupStore = class {
|
|
107
|
+
constructor(schema, base, retention) {
|
|
108
|
+
this.schema = schema;
|
|
109
|
+
this.base = base;
|
|
110
|
+
this.retention = retention <= 0 ? DEFAULT_RETENTION : retention;
|
|
111
|
+
}
|
|
112
|
+
get schemaDir() {
|
|
113
|
+
return path.join(this.base, "backups", this.schema);
|
|
114
|
+
}
|
|
115
|
+
/** Creates and returns the directory of the backup taken before `version`. */
|
|
116
|
+
async backupDir(version) {
|
|
117
|
+
const dir = path.join(this.schemaDir, "pre-" + version);
|
|
118
|
+
try {
|
|
119
|
+
await _fs.promises.mkdir(dir, { recursive: true, mode: 488 });
|
|
120
|
+
} catch (e) {
|
|
121
|
+
throw new Error(`create backup dir: ${e.message}`, { cause: e });
|
|
122
|
+
}
|
|
123
|
+
return dir;
|
|
124
|
+
}
|
|
125
|
+
async hasBackup(version) {
|
|
126
|
+
try {
|
|
127
|
+
await _fs.promises.stat(path.join(this.schemaDir, "pre-" + version));
|
|
128
|
+
return true;
|
|
129
|
+
} catch (e2) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** Removes a backup directory (after an unsupported backup). */
|
|
134
|
+
async discard(backupPath) {
|
|
135
|
+
await _fs.promises.rm(backupPath, { recursive: true, force: true });
|
|
136
|
+
}
|
|
137
|
+
/** Saves, beside the backup, the version the run started at ("" is an empty file). */
|
|
138
|
+
async savePreRun(backupPath, version) {
|
|
139
|
+
try {
|
|
140
|
+
await _fs.promises.writeFile(backupPath + PRE_RUN_SUFFIX, version, { mode: 384 });
|
|
141
|
+
} catch (e) {
|
|
142
|
+
throw new Error(`write pre-run version: ${e.message}`, { cause: e });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/** The version saved beside `backupPath`; `recorded` is false for a backup taken before it was saved. */
|
|
146
|
+
async readPreRun(backupPath) {
|
|
147
|
+
try {
|
|
148
|
+
const data = await _fs.promises.readFile(backupPath + PRE_RUN_SUFFIX, "utf8");
|
|
149
|
+
return { version: data.trim(), recorded: true };
|
|
150
|
+
} catch (e) {
|
|
151
|
+
if (isMissing(e)) return { version: "", recorded: false };
|
|
152
|
+
throw new Error(`read pre-run version: ${e.message}`, { cause: e });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** The most recent backup directory by semver. */
|
|
156
|
+
async latestBackup() {
|
|
157
|
+
let dirs;
|
|
158
|
+
try {
|
|
159
|
+
dirs = await backupDirs(this.schemaDir);
|
|
160
|
+
} catch (e) {
|
|
161
|
+
if (isMissing(e)) throw this.noBackups();
|
|
162
|
+
throw new Error(`read backup dir: ${e.message}`, { cause: e });
|
|
163
|
+
}
|
|
164
|
+
if (dirs.length === 0) throw this.noBackups();
|
|
165
|
+
return path.join(this.schemaDir, dirs[0].name);
|
|
166
|
+
}
|
|
167
|
+
/** No backup at all: not restored, with unsupported as its cause. */
|
|
168
|
+
noBackups() {
|
|
169
|
+
return new (0, _chunkI6XDPSXQjs.NotRestoredError)("no backups found for " + this.schema, {
|
|
170
|
+
cause: new (0, _chunkI6XDPSXQjs.BackupUnsupportedError)()
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/** Keeps the `retention` most recent backups by semver, removing the rest and their sidecars. */
|
|
174
|
+
async prune() {
|
|
175
|
+
let dirs;
|
|
176
|
+
try {
|
|
177
|
+
dirs = await backupDirs(this.schemaDir);
|
|
178
|
+
} catch (e) {
|
|
179
|
+
if (isMissing(e)) return;
|
|
180
|
+
throw new Error(`read backup dir: ${e.message}`, { cause: e });
|
|
181
|
+
}
|
|
182
|
+
for (const d of dirs.slice(this.retention).reverse()) {
|
|
183
|
+
const p = path.join(this.schemaDir, d.name);
|
|
184
|
+
try {
|
|
185
|
+
await _fs.promises.rm(p, { recursive: true, force: true });
|
|
186
|
+
await _fs.promises.rm(p + PRE_RUN_SUFFIX, { force: true });
|
|
187
|
+
} catch (e) {
|
|
188
|
+
throw new Error(`prune backup ${d.name}: ${e.message}`, { cause: e });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
exports.parseSemver = parseSemver; exports.compareSemver = compareSemver; exports.compareVersions = compareVersions; exports.trimV = trimV; exports.DEFAULT_RETENTION = DEFAULT_RETENTION; exports.defaultDataDir = defaultDataDir; exports.readVersionFile = readVersionFile; exports.writeVersionFile = writeVersionFile; exports.BackupStore = BackupStore;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
var
|
|
6
|
+
var _chunkTUU2BY5Ijs = require('./chunk-TUU2BY5I.js');
|
|
7
7
|
|
|
8
8
|
// src/llm-http.ts
|
|
9
9
|
var DEFAULT_MAX_BODY_BYTES = 32 << 20;
|
|
@@ -21,7 +21,7 @@ function limitsFrom(provider, params) {
|
|
|
21
21
|
const t = v.trim();
|
|
22
22
|
const n = Number(t);
|
|
23
23
|
if (!/^\d+$/.test(t) || !Number.isSafeInteger(n) || n <= 0) {
|
|
24
|
-
throw new (0,
|
|
24
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(`${provider}: ${name} must be a positive byte count`);
|
|
25
25
|
}
|
|
26
26
|
l[key] = n;
|
|
27
27
|
}
|
|
@@ -48,7 +48,7 @@ async function post(opts, url, headers, body) {
|
|
|
48
48
|
}
|
|
49
49
|
if (resp.status >= 300 && resp.status < 400) {
|
|
50
50
|
await _optionalChain([resp, 'access', _2 => _2.body, 'optionalAccess', _3 => _3.cancel, 'call', _4 => _4(), 'access', _5 => _5.catch, 'call', _6 => _6(() => void 0)]);
|
|
51
|
-
throw new (0,
|
|
51
|
+
throw new (0, _chunkTUU2BY5Ijs.HTTPStatusError)(resp.status, `${opts.provider}: redirect not followed`);
|
|
52
52
|
}
|
|
53
53
|
if (resp.ok) return resp;
|
|
54
54
|
const text = await readBody(resp, opts.limits.body, opts.provider).catch(
|
|
@@ -65,7 +65,7 @@ async function post(opts, url, headers, body) {
|
|
|
65
65
|
status: resp.status,
|
|
66
66
|
statusText: resp.statusText,
|
|
67
67
|
headers: resp.headers,
|
|
68
|
-
body:
|
|
68
|
+
body: _chunkTUU2BY5Ijs.scrubSecrets.call(void 0, text, opts.secrets)
|
|
69
69
|
}),
|
|
70
70
|
opts.secrets
|
|
71
71
|
);
|
|
@@ -114,14 +114,14 @@ function sleep(ms) {
|
|
|
114
114
|
function networkError(provider, err) {
|
|
115
115
|
const cause = err.cause;
|
|
116
116
|
const detail = _nullishCoalesce(_nullishCoalesce(_optionalChain([cause, 'optionalAccess', _7 => _7.message]), () => ( _optionalChain([err, 'optionalAccess', _8 => _8.message]))), () => ( String(err)));
|
|
117
|
-
const e = new (0,
|
|
117
|
+
const e = new (0, _chunkTUU2BY5Ijs.LLMError)(`${provider}: request failed: ${detail}`);
|
|
118
118
|
if (_optionalChain([cause, 'optionalAccess', _9 => _9.code])) e.code = cause.code;
|
|
119
119
|
return e;
|
|
120
120
|
}
|
|
121
121
|
function scrubError(err, secrets) {
|
|
122
122
|
if (!(err instanceof Error) || secrets.length === 0) return err;
|
|
123
|
-
err.message =
|
|
124
|
-
if (err.stack) err.stack =
|
|
123
|
+
err.message = _chunkTUU2BY5Ijs.scrubSecrets.call(void 0, err.message, secrets);
|
|
124
|
+
if (err.stack) err.stack = _chunkTUU2BY5Ijs.scrubSecrets.call(void 0, err.stack, secrets);
|
|
125
125
|
const cause = err.cause;
|
|
126
126
|
if (cause instanceof Error && cause !== err) scrubError(cause, secrets);
|
|
127
127
|
return err;
|
|
@@ -138,7 +138,7 @@ async function readBody(resp, max, provider) {
|
|
|
138
138
|
read += value.byteLength;
|
|
139
139
|
if (read > max) {
|
|
140
140
|
await reader.cancel().catch(() => void 0);
|
|
141
|
-
throw new (0,
|
|
141
|
+
throw new (0, _chunkTUU2BY5Ijs.ResponseTooLargeError)(provider, "body", max);
|
|
142
142
|
}
|
|
143
143
|
chunks.push(value);
|
|
144
144
|
}
|
|
@@ -177,7 +177,7 @@ async function* sseEvents(resp, max, provider) {
|
|
|
177
177
|
continue;
|
|
178
178
|
}
|
|
179
179
|
eventBytes += nonCR(line) + 1;
|
|
180
|
-
if (eventBytes > max) throw new (0,
|
|
180
|
+
if (eventBytes > max) throw new (0, _chunkTUU2BY5Ijs.ResponseTooLargeError)(provider, "event", max);
|
|
181
181
|
const text = decoder.decode(line);
|
|
182
182
|
if (text.startsWith(":")) continue;
|
|
183
183
|
const colon = text.indexOf(":");
|
|
@@ -188,7 +188,7 @@ async function* sseEvents(resp, max, provider) {
|
|
|
188
188
|
else if (field === "data") data.push(v);
|
|
189
189
|
}
|
|
190
190
|
if (eventBytes + nonCR(buf) > max) {
|
|
191
|
-
throw new (0,
|
|
191
|
+
throw new (0, _chunkTUU2BY5Ijs.ResponseTooLargeError)(provider, "event", max);
|
|
192
192
|
}
|
|
193
193
|
if (done) {
|
|
194
194
|
finished = true;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _chunkIGKBRNEWjs = require('./chunk-IGKBRNEW.js');
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
@@ -23,7 +23,7 @@ var _chunkBXJ2J27Bjs = require('./chunk-BXJ2J27B.js');
|
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
|
|
26
|
-
var
|
|
26
|
+
var _chunkTUU2BY5Ijs = require('./chunk-TUU2BY5I.js');
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
var _chunkR5U7XKVJjs = require('./chunk-R5U7XKVJ.js');
|
|
@@ -65,11 +65,11 @@ var OpenAIAdapter = class {
|
|
|
65
65
|
constructor(cfg, opts = {}) {
|
|
66
66
|
this.scheme = _optionalChain([cfg, 'access', _2 => _2.uri, 'optionalAccess', _3 => _3.scheme]) || "openai";
|
|
67
67
|
this.label = _nullishCoalesce(opts.label, () => ( this.scheme));
|
|
68
|
-
this.limits =
|
|
68
|
+
this.limits = _chunkIGKBRNEWjs.limitsFrom.call(void 0, this.label, cfg.provider.params);
|
|
69
69
|
try {
|
|
70
|
-
this.toolNames =
|
|
70
|
+
this.toolNames = _chunkTUU2BY5Ijs.toolNameModeOf.call(void 0, cfg.provider.params);
|
|
71
71
|
} catch (err) {
|
|
72
|
-
throw new (0,
|
|
72
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(`${this.label}: ${err.message}`);
|
|
73
73
|
}
|
|
74
74
|
let base = cfg.provider.baseURL || OPENAI_BASE_URLS[this.scheme] || OPENAI_DEFAULT_BASE_URL;
|
|
75
75
|
if (this.scheme === "lmstudio" || this.scheme === "openai" && isHostForm(cfg)) {
|
|
@@ -78,14 +78,14 @@ var OpenAIAdapter = class {
|
|
|
78
78
|
try {
|
|
79
79
|
new URL(base);
|
|
80
80
|
} catch (e2) {
|
|
81
|
-
throw new (0,
|
|
82
|
-
`${this.label}: invalid base URL: ${JSON.stringify(
|
|
81
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(
|
|
82
|
+
`${this.label}: invalid base URL: ${JSON.stringify(_chunkTUU2BY5Ijs.redactURI.call(void 0, base))}`
|
|
83
83
|
);
|
|
84
84
|
}
|
|
85
85
|
this.baseURL = base;
|
|
86
86
|
this.model = cfg.provider.model || _optionalChain([cfg, 'access', _4 => _4.uri, 'optionalAccess', _5 => _5.model]) || "";
|
|
87
87
|
const key = _nullishCoalesce(cfg.provider.apiKey, () => ( ""));
|
|
88
|
-
|
|
88
|
+
_chunkTUU2BY5Ijs.checkKeyEndpoint.call(void 0, this.scheme, key, base);
|
|
89
89
|
this.headers = {
|
|
90
90
|
"content-type": "application/json",
|
|
91
91
|
accept: "application/json"
|
|
@@ -131,10 +131,10 @@ var OpenAIAdapter = class {
|
|
|
131
131
|
}
|
|
132
132
|
// ── private ────────────────────────────────────────────────────
|
|
133
133
|
toolNameMap(tools) {
|
|
134
|
-
return
|
|
134
|
+
return _chunkTUU2BY5Ijs.ToolNames.build(_chunkTUU2BY5Ijs.OPENAI_TOOL_NAMES, this.toolNames, tools);
|
|
135
135
|
}
|
|
136
136
|
url() {
|
|
137
|
-
return
|
|
137
|
+
return _chunkIGKBRNEWjs.joinURL.call(void 0, this.baseURL, "chat/completions");
|
|
138
138
|
}
|
|
139
139
|
transport(req) {
|
|
140
140
|
const model = req.model || this.model;
|
|
@@ -146,14 +146,14 @@ var OpenAIAdapter = class {
|
|
|
146
146
|
};
|
|
147
147
|
}
|
|
148
148
|
async send(body, req) {
|
|
149
|
-
const resp = await
|
|
150
|
-
const text = await
|
|
151
|
-
throw
|
|
149
|
+
const resp = await _chunkIGKBRNEWjs.post.call(void 0, this.transport(req), this.url(), this.headers, body);
|
|
150
|
+
const text = await _chunkIGKBRNEWjs.readBody.call(void 0, resp, this.limits.body, this.label).catch((e) => {
|
|
151
|
+
throw _chunkIGKBRNEWjs.scrubError.call(void 0, e, this.secrets);
|
|
152
152
|
});
|
|
153
153
|
try {
|
|
154
154
|
return JSON.parse(text);
|
|
155
155
|
} catch (e3) {
|
|
156
|
-
throw new (0,
|
|
156
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(`${this.label}: response is not valid JSON`);
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
openStream(req, tools) {
|
|
@@ -174,13 +174,13 @@ var OpenAIAdapter = class {
|
|
|
174
174
|
if (req.streamEvents) {
|
|
175
175
|
body["stream_options"] = { include_usage: true };
|
|
176
176
|
}
|
|
177
|
-
const resp = await
|
|
177
|
+
const resp = await _chunkIGKBRNEWjs.post.call(void 0, this.transport(req), this.url(), {
|
|
178
178
|
...this.headers,
|
|
179
179
|
accept: "text/event-stream"
|
|
180
180
|
}, body);
|
|
181
181
|
const acc = new Accumulator(names, req.streamEvents === true);
|
|
182
182
|
try {
|
|
183
|
-
for await (const ev of
|
|
183
|
+
for await (const ev of _chunkIGKBRNEWjs.sseEvents.call(void 0, resp, this.limits.event, this.label)) {
|
|
184
184
|
if (ev.data === "[DONE]") {
|
|
185
185
|
done(acc.outcome());
|
|
186
186
|
yield { content: "", done: true };
|
|
@@ -190,11 +190,11 @@ var OpenAIAdapter = class {
|
|
|
190
190
|
try {
|
|
191
191
|
chunk = JSON.parse(ev.data);
|
|
192
192
|
} catch (e4) {
|
|
193
|
-
throw new (0,
|
|
193
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(`${this.label}: stream event is not valid JSON`);
|
|
194
194
|
}
|
|
195
195
|
if (chunk.error) {
|
|
196
196
|
const e = chunk.error;
|
|
197
|
-
throw new (0,
|
|
197
|
+
throw new (0, _chunkTUU2BY5Ijs.APIError)(
|
|
198
198
|
this.label,
|
|
199
199
|
0,
|
|
200
200
|
_nullishCoalesce(e.type, () => ( "")),
|
|
@@ -204,9 +204,9 @@ var OpenAIAdapter = class {
|
|
|
204
204
|
yield* acc.add(chunk);
|
|
205
205
|
}
|
|
206
206
|
} catch (err) {
|
|
207
|
-
throw
|
|
207
|
+
throw _chunkIGKBRNEWjs.scrubError.call(void 0, err, this.secrets);
|
|
208
208
|
}
|
|
209
|
-
if (!acc.finished()) throw new (0,
|
|
209
|
+
if (!acc.finished()) throw new (0, _chunkTUU2BY5Ijs.StreamTruncatedError)(this.label);
|
|
210
210
|
done(acc.outcome());
|
|
211
211
|
yield { content: "", done: true };
|
|
212
212
|
}
|
|
@@ -216,9 +216,9 @@ var OpenAIAdapter = class {
|
|
|
216
216
|
*/
|
|
217
217
|
async buildBody(req, names, tools) {
|
|
218
218
|
try {
|
|
219
|
-
|
|
219
|
+
_chunkTUU2BY5Ijs.checkToolChoice.call(void 0, req.toolChoice, tools);
|
|
220
220
|
} catch (err) {
|
|
221
|
-
throw new (0,
|
|
221
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(`${this.label}: ${err.message}`);
|
|
222
222
|
}
|
|
223
223
|
const messages = [];
|
|
224
224
|
for (const m of req.messages) {
|
|
@@ -247,21 +247,21 @@ function apiRootV1(base) {
|
|
|
247
247
|
}
|
|
248
248
|
async function mapMessage(m, names, label) {
|
|
249
249
|
if (m.role === "tool" && !m.toolCallId) {
|
|
250
|
-
throw new (0,
|
|
250
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(`${label}: tool result needs toolCallId`);
|
|
251
251
|
}
|
|
252
|
-
|
|
252
|
+
_chunkTUU2BY5Ijs.checkToolLinkage.call(void 0, m);
|
|
253
253
|
if (m.role === "tool") {
|
|
254
254
|
return { role: "tool", content: m.content, tool_call_id: m.toolCallId };
|
|
255
255
|
}
|
|
256
256
|
if (_optionalChain([m, 'access', _31 => _31.toolCalls, 'optionalAccess', _32 => _32.length])) {
|
|
257
257
|
if (_optionalChain([m, 'access', _33 => _33.parts, 'optionalAccess', _34 => _34.length])) {
|
|
258
|
-
throw new (0,
|
|
258
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(`${label}: assistant tool calls do not support content parts`);
|
|
259
259
|
}
|
|
260
260
|
const out = { role: "assistant" };
|
|
261
261
|
if (m.content) out["content"] = m.content;
|
|
262
262
|
out["tool_calls"] = m.toolCalls.map((tc) => {
|
|
263
263
|
if (!tc.id) {
|
|
264
|
-
throw new (0,
|
|
264
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(`${label}: tool call ${JSON.stringify(tc.name)} needs an id`);
|
|
265
265
|
}
|
|
266
266
|
return {
|
|
267
267
|
id: tc.id,
|
|
@@ -276,7 +276,7 @@ async function mapMessage(m, names, label) {
|
|
|
276
276
|
}
|
|
277
277
|
if (!_optionalChain([m, 'access', _35 => _35.parts, 'optionalAccess', _36 => _36.length])) return { role: m.role, content: m.content };
|
|
278
278
|
if (m.role !== "" && m.role !== "user") {
|
|
279
|
-
throw new (0,
|
|
279
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(
|
|
280
280
|
`${label}: message role ${JSON.stringify(m.role)} does not support content parts`
|
|
281
281
|
);
|
|
282
282
|
}
|
|
@@ -291,7 +291,7 @@ function encodeArguments(tc, label) {
|
|
|
291
291
|
try {
|
|
292
292
|
JSON.parse(args);
|
|
293
293
|
} catch (e5) {
|
|
294
|
-
throw new (0,
|
|
294
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(
|
|
295
295
|
`${label}: tool call ${JSON.stringify(tc.id)} arguments are not valid JSON`
|
|
296
296
|
);
|
|
297
297
|
}
|
|
@@ -309,7 +309,7 @@ async function mapPart(p, label) {
|
|
|
309
309
|
const url = _optionalChain([p, 'access', _40 => _40.source, 'optionalAccess', _41 => _41.url, 'call', _42 => _42()]);
|
|
310
310
|
if (url) return { type: "image_url", image_url: { url } };
|
|
311
311
|
if (!mime) {
|
|
312
|
-
throw new (0,
|
|
312
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(`${label}: image part needs a MIME type or URL`);
|
|
313
313
|
}
|
|
314
314
|
return {
|
|
315
315
|
type: "image_url",
|
|
@@ -317,11 +317,11 @@ async function mapPart(p, label) {
|
|
|
317
317
|
};
|
|
318
318
|
}
|
|
319
319
|
default:
|
|
320
|
-
throw new (0,
|
|
320
|
+
throw new (0, _chunkTUU2BY5Ijs.LLMError)(`${label}: unsupported modality ${JSON.stringify(p.type)}`);
|
|
321
321
|
}
|
|
322
322
|
}
|
|
323
323
|
async function readBase64(p, label) {
|
|
324
|
-
if (!p.source) throw new (0,
|
|
324
|
+
if (!p.source) throw new (0, _chunkTUU2BY5Ijs.LLMError)(`${label}: nil media source`);
|
|
325
325
|
return Buffer.from(await p.source.read()).toString("base64");
|
|
326
326
|
}
|
|
327
327
|
function mapTools(defs) {
|
|
@@ -471,18 +471,18 @@ function mapErrorResponse(r, label, scheme, model) {
|
|
|
471
471
|
}
|
|
472
472
|
const detail = e.message !== void 0 ? `${_nullishCoalesce(e.type, () => ( "error"))}: ${e.message}` : r.body.slice(0, 2048);
|
|
473
473
|
const message = `${label}: ${r.status} ${detail}`.trim();
|
|
474
|
-
const api = new (0,
|
|
474
|
+
const api = new (0, _chunkTUU2BY5Ijs.APIError)(scheme, r.status, _nullishCoalesce(e.type, () => ( "")), message);
|
|
475
475
|
switch (r.status) {
|
|
476
476
|
case 401:
|
|
477
|
-
return new (0,
|
|
477
|
+
return new (0, _chunkTUU2BY5Ijs.AuthError)(scheme, api);
|
|
478
478
|
case 403:
|
|
479
|
-
return new (0,
|
|
479
|
+
return new (0, _chunkTUU2BY5Ijs.PermissionDeniedError)(scheme, api);
|
|
480
480
|
case 429:
|
|
481
|
-
return new (0,
|
|
481
|
+
return new (0, _chunkTUU2BY5Ijs.RateLimitError)(scheme);
|
|
482
482
|
case 404:
|
|
483
|
-
return new (0,
|
|
483
|
+
return new (0, _chunkTUU2BY5Ijs.ModelError)(model, scheme);
|
|
484
484
|
}
|
|
485
|
-
if (r.status >= 500) return new (0,
|
|
485
|
+
if (r.status >= 500) return new (0, _chunkTUU2BY5Ijs.HTTPStatusError)(r.status, message);
|
|
486
486
|
return api;
|
|
487
487
|
}
|
|
488
488
|
function openaiFactory(cfg) {
|
|
@@ -496,8 +496,14 @@ function openaiFactory(cfg) {
|
|
|
496
496
|
};
|
|
497
497
|
}
|
|
498
498
|
for (const s of OPENAI_SCHEMES) {
|
|
499
|
-
|
|
500
|
-
|
|
499
|
+
const base = _nullishCoalesce(OPENAI_BASE_URLS[s], () => ( OPENAI_DEFAULT_BASE_URL));
|
|
500
|
+
_chunkTUU2BY5Ijs.register.call(void 0, s, openaiFactory, {
|
|
501
|
+
endpoints: [base],
|
|
502
|
+
// The hosted key belongs to the hosted endpoint: a server named by
|
|
503
|
+
// another base URL (vLLM, llama.cpp, LiteLLM) gets only a key set
|
|
504
|
+
// for it.
|
|
505
|
+
baseURL: base,
|
|
506
|
+
keyBoundToDefault: true,
|
|
501
507
|
// LM Studio's local server takes no key: none is resolved for it.
|
|
502
508
|
...s === "lmstudio" ? { key: { envVars: [], optional: true } } : {}
|
|
503
509
|
});
|