@hop-top/kit 0.4.0-alpha.1
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 +86 -0
- package/LICENSE +25 -0
- package/README.md +189 -0
- package/dist/aim.d.ts +116 -0
- package/dist/aim.js +417 -0
- package/dist/alias.d.ts +73 -0
- package/dist/alias.js +207 -0
- package/dist/api.d.ts +62 -0
- package/dist/api.js +165 -0
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +41 -0
- package/dist/cli.d.ts +309 -0
- package/dist/cli.js +885 -0
- package/dist/config.d.ts +97 -0
- package/dist/config.js +79 -0
- package/dist/errcorrect.d.ts +37 -0
- package/dist/errcorrect.js +69 -0
- package/dist/llm.d.ts +188 -0
- package/dist/llm.js +356 -0
- package/dist/output.d.ts +213 -0
- package/dist/output.js +447 -0
- package/dist/progress.d.ts +30 -0
- package/dist/progress.js +56 -0
- package/dist/provenance.d.ts +24 -0
- package/dist/provenance.js +32 -0
- package/dist/routellm.d.ts +64 -0
- package/dist/routellm.js +258 -0
- package/dist/rpc.d.ts +25 -0
- package/dist/rpc.js +101 -0
- package/dist/safety.d.ts +24 -0
- package/dist/safety.js +44 -0
- package/dist/scope.d.ts +121 -0
- package/dist/scope.js +380 -0
- package/dist/sqlstore.d.ts +84 -0
- package/dist/sqlstore.js +85 -0
- package/dist/stream.d.ts +35 -0
- package/dist/stream.js +52 -0
- package/dist/tui/index.d.ts +300 -0
- package/dist/tui/index.js +332 -0
- package/dist/upgrade.d.ts +95 -0
- package/dist/upgrade.js +115 -0
- package/dist/xdg.d.ts +169 -0
- package/dist/xdg.js +129 -0
- package/eslint.config.mjs +17 -0
- package/package.json +133 -0
- package/src/README.md +3 -0
- package/src/aim.test.ts +331 -0
- package/src/aim.ts +388 -0
- package/src/alias.test.ts +423 -0
- package/src/alias.ts +256 -0
- package/src/api.test.ts +190 -0
- package/src/api.ts +214 -0
- package/src/auth.test.ts +24 -0
- package/src/auth.ts +37 -0
- package/src/bus.test.ts +178 -0
- package/src/bus.ts +114 -0
- package/src/cli.test.ts +690 -0
- package/src/cli.ts +741 -0
- package/src/completion-values.test.ts +202 -0
- package/src/completion-values.ts +173 -0
- package/src/completion.test.ts +212 -0
- package/src/completion.ts +158 -0
- package/src/compliance.test.ts +134 -0
- package/src/compliance.ts +669 -0
- package/src/config.test.ts +110 -0
- package/src/config.ts +164 -0
- package/src/errcorrect.test.ts +134 -0
- package/src/errcorrect.ts +84 -0
- package/src/flagregister.test.ts +103 -0
- package/src/flagregister.ts +78 -0
- package/src/gen/crud_connect.ts +76 -0
- package/src/gen/crud_pb.ts +269 -0
- package/src/hint.test.ts +315 -0
- package/src/hint.ts +178 -0
- package/src/llm.test.ts +612 -0
- package/src/llm.ts +577 -0
- package/src/log.test.ts +145 -0
- package/src/log.ts +155 -0
- package/src/output/builtins.ts +34 -0
- package/src/output/dispatch.ts +195 -0
- package/src/output/flags.ts +151 -0
- package/src/output/format_help.ts +80 -0
- package/src/output/formatter.ts +167 -0
- package/src/output/formatters/csv.ts +72 -0
- package/src/output/formatters/json.ts +44 -0
- package/src/output/formatters/table.ts +44 -0
- package/src/output/formatters/text.ts +109 -0
- package/src/output/formatters/yaml.ts +47 -0
- package/src/output/projection.ts +126 -0
- package/src/output/registry.ts +88 -0
- package/src/output/template.ts +63 -0
- package/src/output.test.ts +117 -0
- package/src/output.ts +90 -0
- package/src/progress.test.ts +93 -0
- package/src/progress.ts +58 -0
- package/src/provenance.test.ts +59 -0
- package/src/provenance.ts +26 -0
- package/src/routellm.test.ts +179 -0
- package/src/routellm.ts +365 -0
- package/src/router/bert.test.ts +180 -0
- package/src/router/bert.ts +145 -0
- package/src/router/controller.test.ts +183 -0
- package/src/router/controller.ts +165 -0
- package/src/router/integration.test.ts +309 -0
- package/src/router/intent.test.ts +187 -0
- package/src/router/intent.ts +208 -0
- package/src/router/mf.test.ts +66 -0
- package/src/router/mf.ts +52 -0
- package/src/router/random.test.ts +23 -0
- package/src/router/random.ts +13 -0
- package/src/router/router.test.ts +31 -0
- package/src/router/router.ts +62 -0
- package/src/router/server.test.ts +193 -0
- package/src/router/server.ts +134 -0
- package/src/router/sw.test.ts +199 -0
- package/src/router/sw.ts +232 -0
- package/src/rpc.test.ts +47 -0
- package/src/rpc.ts +91 -0
- package/src/safety.test.ts +53 -0
- package/src/safety.ts +42 -0
- package/src/scope-defaults.json +72 -0
- package/src/scope.test.ts +177 -0
- package/src/scope.ts +382 -0
- package/src/setflag.test.ts +110 -0
- package/src/setflag.ts +102 -0
- package/src/sqlstore.test.ts +110 -0
- package/src/sqlstore.ts +144 -0
- package/src/stream.test.ts +69 -0
- package/src/stream.ts +47 -0
- package/src/textflag.test.ts +105 -0
- package/src/textflag.ts +68 -0
- package/src/toolspec.test.ts +187 -0
- package/src/toolspec.ts +279 -0
- package/src/triton/client.test.ts +205 -0
- package/src/triton/client.ts +261 -0
- package/src/tui/anim.test.ts +146 -0
- package/src/tui/anim.ts +134 -0
- package/src/tui/badge.test.ts +41 -0
- package/src/tui/badge.ts +55 -0
- package/src/tui/confirm.test.ts +68 -0
- package/src/tui/confirm.ts +49 -0
- package/src/tui/index.ts +29 -0
- package/src/tui/list.test.ts +84 -0
- package/src/tui/list.ts +72 -0
- package/src/tui/parity.ts +41 -0
- package/src/tui/pills.test.ts +40 -0
- package/src/tui/pills.ts +43 -0
- package/src/tui/progress.test.ts +164 -0
- package/src/tui/progress.ts +120 -0
- package/src/tui/spinner.test.ts +111 -0
- package/src/tui/spinner.ts +96 -0
- package/src/tui/status.test.ts +68 -0
- package/src/tui/status.ts +66 -0
- package/src/upgrade.test.ts +298 -0
- package/src/upgrade.ts +205 -0
- package/src/uri.ts +199 -0
- package/src/wizard.test.ts +441 -0
- package/src/wizard.ts +662 -0
- package/src/xdg.test.ts +202 -0
- package/src/xdg.ts +280 -0
- package/test/output/cols.test.ts +148 -0
- package/test/output/csv.test.ts +129 -0
- package/test/output/dispatch.test.ts +194 -0
- package/test/output/format_help.test.ts +131 -0
- package/test/output/formatter.test.ts +137 -0
- package/test/output/output_flag.test.ts +124 -0
- package/test/output/registry.test.ts +127 -0
- package/test/output/smoke-capture.ts +83 -0
- package/test/output/smoke.test.ts +142 -0
- package/test/output/template.test.ts +95 -0
- package/test/output/text.test.ts +75 -0
- package/test/testdata/api-fixture.json +115 -0
- package/test/testdata/query-vectors.json +187 -0
- package/tsconfig.json +12 -0
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Layered YAML configuration loader — TypeScript port of hop.top/kit/config.
|
|
5
|
+
*
|
|
6
|
+
* Merges configuration from up to three file layers in priority order:
|
|
7
|
+
* system → user → project → envOverride
|
|
8
|
+
*
|
|
9
|
+
* Each later layer's values overwrite earlier ones via {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign | Object.assign}.
|
|
10
|
+
* Missing files are silently skipped. A file that exists but contains invalid
|
|
11
|
+
* YAML causes {@link load} to throw.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { load } from "@hop-top/kit/config";
|
|
16
|
+
*
|
|
17
|
+
* interface AppConfig {
|
|
18
|
+
* host: string;
|
|
19
|
+
* port: number;
|
|
20
|
+
* debug: boolean;
|
|
21
|
+
* token: string;
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* const cfg: AppConfig = { host: "localhost", port: 3000, debug: false, token: "" };
|
|
25
|
+
*
|
|
26
|
+
* load(cfg, {
|
|
27
|
+
* systemConfigPath: "/etc/mytool/config.yaml",
|
|
28
|
+
* userConfigPath: `${process.env.HOME}/.config/mytool/config.yaml`,
|
|
29
|
+
* projectConfigPath: ".mytool.yaml",
|
|
30
|
+
* envOverride(c) {
|
|
31
|
+
* if (process.env.HOST) c.host = process.env.HOST;
|
|
32
|
+
* if (process.env.PORT) c.port = Number(process.env.PORT);
|
|
33
|
+
* if (process.env.TOKEN) c.token = process.env.TOKEN;
|
|
34
|
+
* },
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* Configuration sources for {@link load}.
|
|
40
|
+
*
|
|
41
|
+
* All path fields are optional; an empty or omitted path is silently skipped.
|
|
42
|
+
* {@link Options.envOverride} is called last, after all file layers, so it can
|
|
43
|
+
* apply environment-variable overrides on top of file values.
|
|
44
|
+
*/
|
|
45
|
+
interface Options<T extends object = object> {
|
|
46
|
+
/**
|
|
47
|
+
* Path to the system-wide config file, e.g. `/etc/mytool/config.yaml`.
|
|
48
|
+
* Applied first (lowest priority).
|
|
49
|
+
*/
|
|
50
|
+
systemConfigPath?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Path to the per-user config file, e.g. `~/.config/mytool/config.yaml`.
|
|
53
|
+
* Applied second; overwrites system values for shared keys.
|
|
54
|
+
*/
|
|
55
|
+
userConfigPath?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Path to the project-level config file, e.g. `.mytool.yaml`.
|
|
58
|
+
* Applied third; overwrites system and user values for shared keys.
|
|
59
|
+
*/
|
|
60
|
+
projectConfigPath?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Optional callback invoked after all file layers have been merged.
|
|
63
|
+
* Receives the fully-merged `dst` object so the caller can apply
|
|
64
|
+
* environment-variable overrides last (highest priority).
|
|
65
|
+
*
|
|
66
|
+
* @param cfg - The merged config object (same reference as `dst`).
|
|
67
|
+
*/
|
|
68
|
+
envOverride?: (cfg: T) => void;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Merges layered YAML configuration into `dst` and returns it.
|
|
72
|
+
*
|
|
73
|
+
* Merge order: `systemConfigPath` → `userConfigPath` → `projectConfigPath` →
|
|
74
|
+
* `envOverride`.
|
|
75
|
+
*
|
|
76
|
+
* - Missing files (ENOENT) are silently skipped.
|
|
77
|
+
* - A file that exists but is not valid YAML causes this function to throw
|
|
78
|
+
* a descriptive error wrapping the parse failure.
|
|
79
|
+
* - `envOverride`, when provided, is called last with the merged `dst`.
|
|
80
|
+
* - Returns `dst` (the same object reference passed in).
|
|
81
|
+
*
|
|
82
|
+
* @param dst - Destination object to merge config into. Must be a non-null
|
|
83
|
+
* object; mutated in place.
|
|
84
|
+
* @param opts - Config sources. All fields are optional.
|
|
85
|
+
* @returns The mutated `dst` object.
|
|
86
|
+
* @throws {Error} When a file exists but cannot be parsed as YAML.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* const cfg = { host: "localhost", port: 8080 };
|
|
91
|
+
* load(cfg, { projectConfigPath: ".tool.yaml" });
|
|
92
|
+
* console.log(cfg.host); // value from .tool.yaml, or "localhost" if file missing
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare function load<T extends object>(dst: T, opts?: Options<T>): T;
|
|
96
|
+
|
|
97
|
+
export { type Options, load };
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/config.ts
|
|
31
|
+
var config_exports = {};
|
|
32
|
+
__export(config_exports, {
|
|
33
|
+
load: () => load2
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(config_exports);
|
|
36
|
+
var fs = __toESM(require("fs"));
|
|
37
|
+
var yaml = __toESM(require("js-yaml"));
|
|
38
|
+
function load2(dst, opts = {}) {
|
|
39
|
+
const paths = [
|
|
40
|
+
opts.systemConfigPath,
|
|
41
|
+
opts.userConfigPath,
|
|
42
|
+
opts.projectConfigPath
|
|
43
|
+
];
|
|
44
|
+
for (const p of paths) {
|
|
45
|
+
if (!p) continue;
|
|
46
|
+
mergeFile(dst, p);
|
|
47
|
+
}
|
|
48
|
+
if (opts.envOverride) {
|
|
49
|
+
opts.envOverride(dst);
|
|
50
|
+
}
|
|
51
|
+
return dst;
|
|
52
|
+
}
|
|
53
|
+
function mergeFile(dst, filePath) {
|
|
54
|
+
let raw;
|
|
55
|
+
try {
|
|
56
|
+
raw = fs.readFileSync(filePath, "utf8");
|
|
57
|
+
} catch (err) {
|
|
58
|
+
if (isEnoent(err)) return;
|
|
59
|
+
throw err;
|
|
60
|
+
}
|
|
61
|
+
let parsed;
|
|
62
|
+
try {
|
|
63
|
+
parsed = yaml.load(raw);
|
|
64
|
+
} catch (err) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`config: failed to parse ${filePath}: ${err instanceof Error ? err.message : String(err)}`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
if (parsed !== null && parsed !== void 0 && typeof parsed === "object") {
|
|
70
|
+
Object.assign(dst, parsed);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function isEnoent(err) {
|
|
74
|
+
return typeof err === "object" && err !== null && "code" in err && err.code === "ENOENT";
|
|
75
|
+
}
|
|
76
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
77
|
+
0 && (module.exports = {
|
|
78
|
+
load
|
|
79
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module errcorrect
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Factor 4 — Corrective Error Model.
|
|
6
|
+
*
|
|
7
|
+
* Structured errors with machine-readable code, human-readable message,
|
|
8
|
+
* likely cause, suggested fix, and alternative commands.
|
|
9
|
+
*/
|
|
10
|
+
interface CorrectedError {
|
|
11
|
+
code: string;
|
|
12
|
+
message: string;
|
|
13
|
+
cause?: string;
|
|
14
|
+
fix?: string;
|
|
15
|
+
alternatives?: string[];
|
|
16
|
+
retryable?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates an Error instance augmented with CorrectedError fields.
|
|
20
|
+
* Serializes cleanly to JSON (only defined fields).
|
|
21
|
+
*/
|
|
22
|
+
declare function createCorrectedError(opts: CorrectedError): Error & CorrectedError;
|
|
23
|
+
/**
|
|
24
|
+
* Formats a CorrectedError for terminal display.
|
|
25
|
+
*
|
|
26
|
+
* ```
|
|
27
|
+
* ERROR mission not found
|
|
28
|
+
* Cause: no mission matches "bogux"
|
|
29
|
+
* Fix: spaced mission list
|
|
30
|
+
* Try: spaced mission search bogux
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare function formatError(err: CorrectedError, opts?: {
|
|
34
|
+
noColor?: boolean;
|
|
35
|
+
}): string;
|
|
36
|
+
|
|
37
|
+
export { type CorrectedError, createCorrectedError, formatError };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/errcorrect.ts
|
|
21
|
+
var errcorrect_exports = {};
|
|
22
|
+
__export(errcorrect_exports, {
|
|
23
|
+
createCorrectedError: () => createCorrectedError,
|
|
24
|
+
formatError: () => formatError
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(errcorrect_exports);
|
|
27
|
+
function createCorrectedError(opts) {
|
|
28
|
+
const err = new Error(opts.message);
|
|
29
|
+
err.code = opts.code;
|
|
30
|
+
if (opts.cause !== void 0) err.cause = opts.cause;
|
|
31
|
+
if (opts.fix !== void 0) err.fix = opts.fix;
|
|
32
|
+
if (opts.alternatives !== void 0) err.alternatives = opts.alternatives;
|
|
33
|
+
if (opts.retryable !== void 0) err.retryable = opts.retryable;
|
|
34
|
+
err.toJSON = () => {
|
|
35
|
+
const o = {
|
|
36
|
+
code: err.code,
|
|
37
|
+
message: err.message
|
|
38
|
+
};
|
|
39
|
+
if (err.cause !== void 0) o.cause = err.cause;
|
|
40
|
+
if (err.fix !== void 0) o.fix = err.fix;
|
|
41
|
+
if (err.alternatives !== void 0) o.alternatives = err.alternatives;
|
|
42
|
+
if (err.retryable !== void 0) o.retryable = err.retryable;
|
|
43
|
+
return o;
|
|
44
|
+
};
|
|
45
|
+
return err;
|
|
46
|
+
}
|
|
47
|
+
function formatError(err, opts) {
|
|
48
|
+
const red = opts?.noColor ? (s) => s : (s) => `\x1B[31m${s}\x1B[0m`;
|
|
49
|
+
const bold = opts?.noColor ? (s) => s : (s) => `\x1B[1m${s}\x1B[0m`;
|
|
50
|
+
const lines = [];
|
|
51
|
+
lines.push(` ${red(bold("ERROR"))} ${err.message}`);
|
|
52
|
+
if (err.cause) {
|
|
53
|
+
lines.push(` Cause: ${err.cause}`);
|
|
54
|
+
}
|
|
55
|
+
if (err.fix) {
|
|
56
|
+
lines.push(` Fix: ${err.fix}`);
|
|
57
|
+
}
|
|
58
|
+
if (err.alternatives && err.alternatives.length > 0) {
|
|
59
|
+
for (const alt of err.alternatives) {
|
|
60
|
+
lines.push(` Try: ${alt}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return lines.join("\n");
|
|
64
|
+
}
|
|
65
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
66
|
+
0 && (module.exports = {
|
|
67
|
+
createCorrectedError,
|
|
68
|
+
formatError
|
|
69
|
+
});
|
package/dist/llm.d.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* LLM client facade — mirrors Go `llm` package interfaces as TS types.
|
|
5
|
+
*
|
|
6
|
+
* Provides URI parsing, config loading, provider registry, Client facade
|
|
7
|
+
* with fallback, and typed errors. Adapter implementations (OpenAI,
|
|
8
|
+
* Anthropic, Ollama) are deferred; register them via {@link register}.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { createLLM, register } from '@hop-top/kit/llm';
|
|
13
|
+
*
|
|
14
|
+
* register('mock', (cfg) => ({ complete: async (r) => ({ content: 'hi', role: 'assistant' }), close() {} }));
|
|
15
|
+
* const client = createLLM('mock://model-1');
|
|
16
|
+
* const resp = await client.complete({ messages: [{ role: 'user', content: 'hello' }] });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
interface Message {
|
|
20
|
+
role: string;
|
|
21
|
+
content: string;
|
|
22
|
+
}
|
|
23
|
+
interface Request {
|
|
24
|
+
messages: Message[];
|
|
25
|
+
model?: string;
|
|
26
|
+
temperature?: number;
|
|
27
|
+
maxTokens?: number;
|
|
28
|
+
stopSequences?: string[];
|
|
29
|
+
extensions?: Record<string, unknown>;
|
|
30
|
+
}
|
|
31
|
+
interface Response {
|
|
32
|
+
content: string;
|
|
33
|
+
role: string;
|
|
34
|
+
usage?: Usage;
|
|
35
|
+
finishReason?: string;
|
|
36
|
+
}
|
|
37
|
+
interface Usage {
|
|
38
|
+
promptTokens: number;
|
|
39
|
+
completionTokens: number;
|
|
40
|
+
totalTokens: number;
|
|
41
|
+
}
|
|
42
|
+
interface Token {
|
|
43
|
+
content: string;
|
|
44
|
+
done: boolean;
|
|
45
|
+
}
|
|
46
|
+
interface ToolDef {
|
|
47
|
+
name: string;
|
|
48
|
+
description: string;
|
|
49
|
+
parameters: unknown;
|
|
50
|
+
}
|
|
51
|
+
interface ToolCall {
|
|
52
|
+
id: string;
|
|
53
|
+
name: string;
|
|
54
|
+
arguments: unknown;
|
|
55
|
+
}
|
|
56
|
+
interface ToolResponse {
|
|
57
|
+
content: string;
|
|
58
|
+
toolCalls: ToolCall[];
|
|
59
|
+
}
|
|
60
|
+
interface Completer {
|
|
61
|
+
complete(req: Request): Promise<Response>;
|
|
62
|
+
}
|
|
63
|
+
interface Streamer {
|
|
64
|
+
stream(req: Request): AsyncIterable<Token>;
|
|
65
|
+
}
|
|
66
|
+
interface ToolCaller {
|
|
67
|
+
callWithTools(req: Request, tools: ToolDef[]): Promise<ToolResponse>;
|
|
68
|
+
}
|
|
69
|
+
interface Provider extends Partial<Completer & Streamer & ToolCaller> {
|
|
70
|
+
close(): void;
|
|
71
|
+
}
|
|
72
|
+
interface URI {
|
|
73
|
+
scheme: string;
|
|
74
|
+
model: string;
|
|
75
|
+
host?: string;
|
|
76
|
+
params?: Record<string, string>;
|
|
77
|
+
}
|
|
78
|
+
interface ProviderConfig {
|
|
79
|
+
apiKey?: string;
|
|
80
|
+
baseURL?: string;
|
|
81
|
+
model: string;
|
|
82
|
+
params?: Record<string, string>;
|
|
83
|
+
extras?: Record<string, unknown>;
|
|
84
|
+
}
|
|
85
|
+
interface ResolvedConfig {
|
|
86
|
+
uri: URI;
|
|
87
|
+
provider: ProviderConfig;
|
|
88
|
+
fallbacks: string[];
|
|
89
|
+
}
|
|
90
|
+
declare class LLMError extends Error {
|
|
91
|
+
statusCode?: number;
|
|
92
|
+
code?: string;
|
|
93
|
+
constructor(message: string);
|
|
94
|
+
}
|
|
95
|
+
declare class ProviderNotFoundError extends LLMError {
|
|
96
|
+
scheme: string;
|
|
97
|
+
constructor(scheme: string);
|
|
98
|
+
}
|
|
99
|
+
declare class CapabilityNotSupportedError extends LLMError {
|
|
100
|
+
capability: string;
|
|
101
|
+
provider: string;
|
|
102
|
+
constructor(capability: string, provider: string);
|
|
103
|
+
}
|
|
104
|
+
declare class AuthError extends LLMError {
|
|
105
|
+
provider: string;
|
|
106
|
+
constructor(provider: string);
|
|
107
|
+
}
|
|
108
|
+
declare class RateLimitError extends LLMError {
|
|
109
|
+
provider: string;
|
|
110
|
+
retryAfter?: number;
|
|
111
|
+
constructor(provider: string, retryAfter?: number);
|
|
112
|
+
}
|
|
113
|
+
declare class ModelError extends LLMError {
|
|
114
|
+
model: string;
|
|
115
|
+
provider: string;
|
|
116
|
+
constructor(model: string, provider: string);
|
|
117
|
+
}
|
|
118
|
+
declare class FallbackExhaustedError extends LLMError {
|
|
119
|
+
errors: Error[];
|
|
120
|
+
constructor(errors: Error[]);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Returns true when `err` should trigger a fallback attempt.
|
|
124
|
+
*
|
|
125
|
+
* Fallbackable conditions:
|
|
126
|
+
* - LLMError with statusCode >= 500
|
|
127
|
+
* - RateLimitError (429)
|
|
128
|
+
* - Connection/timeout errors (ECONNREFUSED, ETIMEDOUT, ECONNRESET)
|
|
129
|
+
*/
|
|
130
|
+
declare function isFallbackable(err: Error): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Parses an LLM URI string into its components.
|
|
133
|
+
*
|
|
134
|
+
* Format: `scheme://[host[:port]/]model[?params]`
|
|
135
|
+
*
|
|
136
|
+
* When the path portion contains a host:port prefix (detected by the
|
|
137
|
+
* presence of a numeric port), the first segment is treated as host.
|
|
138
|
+
* Otherwise the entire path is the model name.
|
|
139
|
+
*/
|
|
140
|
+
declare function parseURI(raw: string): URI;
|
|
141
|
+
/**
|
|
142
|
+
* Builds a {@link ResolvedConfig} from a URI string + env vars.
|
|
143
|
+
*
|
|
144
|
+
* When `uriStr` is empty/undefined, falls back to `LLM_PROVIDER`
|
|
145
|
+
* env var as a full default URI. If neither is provided, throws.
|
|
146
|
+
*
|
|
147
|
+
* URI query params `api_key` and `base_url` populate provider
|
|
148
|
+
* config. If `uri.host` is set, it derives a baseURL. Env vars
|
|
149
|
+
* `LLM_API_KEY` and `LLM_BASE_URL` override last.
|
|
150
|
+
*
|
|
151
|
+
* Env vars:
|
|
152
|
+
* - `LLM_PROVIDER` — full default URI when no arg provided
|
|
153
|
+
* - `LLM_API_KEY` — overrides provider API key
|
|
154
|
+
* - `LLM_BASE_URL` — overrides provider base URL
|
|
155
|
+
* - `LLM_FALLBACK` — comma-separated fallback URIs
|
|
156
|
+
*/
|
|
157
|
+
declare function loadConfig(uriStr?: string): ResolvedConfig;
|
|
158
|
+
type Factory = (cfg: ResolvedConfig) => Provider;
|
|
159
|
+
/** Register a provider factory for the given URI scheme. */
|
|
160
|
+
declare function register(scheme: string, factory: Factory): void;
|
|
161
|
+
/** Resolve a URI to a Provider instance via the registry. */
|
|
162
|
+
declare function resolve(uriStr: string): Provider;
|
|
163
|
+
/** Clear the registry (for tests). */
|
|
164
|
+
declare function clearRegistry(): void;
|
|
165
|
+
interface ClientOptions {
|
|
166
|
+
fallback?: string[];
|
|
167
|
+
onRequest?: (req: Request) => void;
|
|
168
|
+
onResponse?: (resp: Response, durationMs: number) => void;
|
|
169
|
+
onError?: (err: Error) => void;
|
|
170
|
+
onFallback?: (from: number, to: number, err: Error) => void;
|
|
171
|
+
}
|
|
172
|
+
interface Client {
|
|
173
|
+
complete(req: Request): Promise<Response>;
|
|
174
|
+
stream(req: Request): AsyncIterable<Token>;
|
|
175
|
+
callWithTools(req: Request, tools: ToolDef[]): Promise<ToolResponse>;
|
|
176
|
+
capabilities(): string[];
|
|
177
|
+
provider(): Provider;
|
|
178
|
+
close(): void;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Create an LLM client with fallback support and event hooks.
|
|
182
|
+
*
|
|
183
|
+
* The returned Client delegates to the provider resolved from `uri`.
|
|
184
|
+
* On fallbackable errors, it tries each fallback URI in order.
|
|
185
|
+
*/
|
|
186
|
+
declare function createLLM(uri: string, opts?: ClientOptions): Client;
|
|
187
|
+
|
|
188
|
+
export { AuthError, CapabilityNotSupportedError, type Client, type ClientOptions, type Completer, type Factory, FallbackExhaustedError, LLMError, type Message, ModelError, type Provider, type ProviderConfig, ProviderNotFoundError, RateLimitError, type Request, type ResolvedConfig, type Response, type Streamer, type Token, type ToolCall, type ToolCaller, type ToolDef, type ToolResponse, type URI, type Usage, clearRegistry, createLLM, isFallbackable, loadConfig, parseURI, register, resolve };
|