@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/llm.js
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
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/llm.ts
|
|
21
|
+
var llm_exports = {};
|
|
22
|
+
__export(llm_exports, {
|
|
23
|
+
AuthError: () => AuthError,
|
|
24
|
+
CapabilityNotSupportedError: () => CapabilityNotSupportedError,
|
|
25
|
+
FallbackExhaustedError: () => FallbackExhaustedError,
|
|
26
|
+
LLMError: () => LLMError,
|
|
27
|
+
ModelError: () => ModelError,
|
|
28
|
+
ProviderNotFoundError: () => ProviderNotFoundError,
|
|
29
|
+
RateLimitError: () => RateLimitError,
|
|
30
|
+
clearRegistry: () => clearRegistry,
|
|
31
|
+
createLLM: () => createLLM,
|
|
32
|
+
isFallbackable: () => isFallbackable,
|
|
33
|
+
loadConfig: () => loadConfig,
|
|
34
|
+
parseURI: () => parseURI,
|
|
35
|
+
register: () => register,
|
|
36
|
+
resolve: () => resolve
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(llm_exports);
|
|
39
|
+
var LLMError = class extends Error {
|
|
40
|
+
constructor(message) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.name = "LLMError";
|
|
43
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var ProviderNotFoundError = class extends LLMError {
|
|
47
|
+
constructor(scheme) {
|
|
48
|
+
super(`provider not found: ${scheme}`);
|
|
49
|
+
this.name = "ProviderNotFoundError";
|
|
50
|
+
this.scheme = scheme;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
var CapabilityNotSupportedError = class extends LLMError {
|
|
54
|
+
constructor(capability, provider) {
|
|
55
|
+
super(
|
|
56
|
+
`capability "${capability}" not supported by provider "${provider}"`
|
|
57
|
+
);
|
|
58
|
+
this.name = "CapabilityNotSupportedError";
|
|
59
|
+
this.capability = capability;
|
|
60
|
+
this.provider = provider;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var AuthError = class extends LLMError {
|
|
64
|
+
constructor(provider) {
|
|
65
|
+
super(`authentication failed for provider: ${provider}`);
|
|
66
|
+
this.name = "AuthError";
|
|
67
|
+
this.provider = provider;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var RateLimitError = class extends LLMError {
|
|
71
|
+
constructor(provider, retryAfter) {
|
|
72
|
+
super(`rate limit exceeded for provider: ${provider}`);
|
|
73
|
+
this.name = "RateLimitError";
|
|
74
|
+
this.provider = provider;
|
|
75
|
+
this.retryAfter = retryAfter;
|
|
76
|
+
this.statusCode = 429;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
var ModelError = class extends LLMError {
|
|
80
|
+
constructor(model, provider) {
|
|
81
|
+
super(`model "${model}" not available on provider "${provider}"`);
|
|
82
|
+
this.name = "ModelError";
|
|
83
|
+
this.model = model;
|
|
84
|
+
this.provider = provider;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var FallbackExhaustedError = class extends LLMError {
|
|
88
|
+
constructor(errors) {
|
|
89
|
+
super(
|
|
90
|
+
`all providers exhausted (${errors.length} failures)`
|
|
91
|
+
);
|
|
92
|
+
this.name = "FallbackExhaustedError";
|
|
93
|
+
this.errors = errors;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
function isFallbackable(err) {
|
|
97
|
+
if (err instanceof RateLimitError) return true;
|
|
98
|
+
if (err instanceof LLMError) {
|
|
99
|
+
if (err.code && isConnectionError(err.code)) return true;
|
|
100
|
+
if (err.statusCode !== void 0 && err.statusCode >= 500) return true;
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
const code = err.code;
|
|
104
|
+
if (code && isConnectionError(code)) return true;
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
var CONNECTION_CODES = /* @__PURE__ */ new Set([
|
|
108
|
+
"ECONNREFUSED",
|
|
109
|
+
"ETIMEDOUT",
|
|
110
|
+
"ECONNRESET",
|
|
111
|
+
"ENETUNREACH",
|
|
112
|
+
"EHOSTUNREACH"
|
|
113
|
+
]);
|
|
114
|
+
function isConnectionError(code) {
|
|
115
|
+
return CONNECTION_CODES.has(code);
|
|
116
|
+
}
|
|
117
|
+
function parseURI(raw) {
|
|
118
|
+
const sep = "://";
|
|
119
|
+
const idx = raw.indexOf(sep);
|
|
120
|
+
if (idx < 0) {
|
|
121
|
+
throw new LLMError(`invalid LLM URI (missing "://"): ${raw}`);
|
|
122
|
+
}
|
|
123
|
+
const scheme = raw.slice(0, idx);
|
|
124
|
+
if (!scheme) {
|
|
125
|
+
throw new LLMError(`invalid LLM URI (empty scheme): ${raw}`);
|
|
126
|
+
}
|
|
127
|
+
let rest = raw.slice(idx + sep.length);
|
|
128
|
+
if (!rest) {
|
|
129
|
+
throw new LLMError(`invalid LLM URI (empty model): ${raw}`);
|
|
130
|
+
}
|
|
131
|
+
let params;
|
|
132
|
+
const qIdx = rest.indexOf("?");
|
|
133
|
+
if (qIdx >= 0) {
|
|
134
|
+
const qs = rest.slice(qIdx + 1);
|
|
135
|
+
rest = rest.slice(0, qIdx);
|
|
136
|
+
params = {};
|
|
137
|
+
for (const pair of qs.split("&")) {
|
|
138
|
+
const eqIdx = pair.indexOf("=");
|
|
139
|
+
if (eqIdx > 0) {
|
|
140
|
+
params[pair.slice(0, eqIdx)] = decodeURIComponent(
|
|
141
|
+
pair.slice(eqIdx + 1)
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (!rest) {
|
|
147
|
+
throw new LLMError(`invalid LLM URI (empty model): ${raw}`);
|
|
148
|
+
}
|
|
149
|
+
let host;
|
|
150
|
+
let model;
|
|
151
|
+
const hostPortMatch = rest.match(/^([^/]+:\d+)\/(.+)$/);
|
|
152
|
+
if (hostPortMatch) {
|
|
153
|
+
host = hostPortMatch[1];
|
|
154
|
+
model = hostPortMatch[2];
|
|
155
|
+
} else {
|
|
156
|
+
model = rest;
|
|
157
|
+
}
|
|
158
|
+
const uri = { scheme, model };
|
|
159
|
+
if (host) uri.host = host;
|
|
160
|
+
if (params && Object.keys(params).length > 0) uri.params = params;
|
|
161
|
+
return uri;
|
|
162
|
+
}
|
|
163
|
+
function loadConfig(uriStr) {
|
|
164
|
+
const effective = uriStr || process.env["LLM_PROVIDER"];
|
|
165
|
+
if (!effective) {
|
|
166
|
+
throw new LLMError(
|
|
167
|
+
"no LLM URI provided and LLM_PROVIDER env var is not set"
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
const uri = parseURI(effective);
|
|
171
|
+
const fallbackEnv = process.env["LLM_FALLBACK"];
|
|
172
|
+
const fallbacks = fallbackEnv ? fallbackEnv.split(",").map((s) => s.trim()).filter(Boolean) : [];
|
|
173
|
+
const provider = {
|
|
174
|
+
model: uri.model
|
|
175
|
+
};
|
|
176
|
+
if (uri.params) provider.params = { ...uri.params };
|
|
177
|
+
if (uri.params?.api_key) {
|
|
178
|
+
provider.apiKey = uri.params.api_key;
|
|
179
|
+
}
|
|
180
|
+
if (uri.params?.base_url) {
|
|
181
|
+
provider.baseURL = uri.params.base_url;
|
|
182
|
+
}
|
|
183
|
+
if (uri.host) {
|
|
184
|
+
provider.baseURL = `http://${uri.host}`;
|
|
185
|
+
}
|
|
186
|
+
const envApiKey = process.env["LLM_API_KEY"] || void 0;
|
|
187
|
+
const envBaseURL = process.env["LLM_BASE_URL"] || void 0;
|
|
188
|
+
if (envApiKey) provider.apiKey = envApiKey;
|
|
189
|
+
if (envBaseURL) provider.baseURL = envBaseURL;
|
|
190
|
+
return { uri, provider, fallbacks };
|
|
191
|
+
}
|
|
192
|
+
var registry = /* @__PURE__ */ new Map();
|
|
193
|
+
function register(scheme, factory) {
|
|
194
|
+
if (registry.has(scheme)) {
|
|
195
|
+
throw new LLMError(`provider already registered for scheme: ${scheme}`);
|
|
196
|
+
}
|
|
197
|
+
registry.set(scheme, factory);
|
|
198
|
+
}
|
|
199
|
+
function resolve(uriStr) {
|
|
200
|
+
const cfg = loadConfig(uriStr);
|
|
201
|
+
const factory = registry.get(cfg.uri.scheme);
|
|
202
|
+
if (!factory) {
|
|
203
|
+
throw new ProviderNotFoundError(cfg.uri.scheme);
|
|
204
|
+
}
|
|
205
|
+
return factory(cfg);
|
|
206
|
+
}
|
|
207
|
+
function clearRegistry() {
|
|
208
|
+
registry.clear();
|
|
209
|
+
}
|
|
210
|
+
function createLLM(uri, opts) {
|
|
211
|
+
const cfg = loadConfig(uri);
|
|
212
|
+
const fallbackURIs = opts?.fallback ?? cfg.fallbacks;
|
|
213
|
+
const primary = resolve(uri);
|
|
214
|
+
const allURIs = [uri, ...fallbackURIs];
|
|
215
|
+
const resolvedProviders = /* @__PURE__ */ new Map();
|
|
216
|
+
resolvedProviders.set(0, primary);
|
|
217
|
+
function resolveAt(index) {
|
|
218
|
+
let p = resolvedProviders.get(index);
|
|
219
|
+
if (!p) {
|
|
220
|
+
p = resolve(allURIs[index]);
|
|
221
|
+
resolvedProviders.set(index, p);
|
|
222
|
+
}
|
|
223
|
+
return p;
|
|
224
|
+
}
|
|
225
|
+
async function withFallback(op, fn) {
|
|
226
|
+
const errors = [];
|
|
227
|
+
for (let i = 0; i < allURIs.length; i++) {
|
|
228
|
+
try {
|
|
229
|
+
const p = resolveAt(i);
|
|
230
|
+
if (!(op in p) || typeof p[op] !== "function") {
|
|
231
|
+
throw new CapabilityNotSupportedError(
|
|
232
|
+
op,
|
|
233
|
+
parseURI(allURIs[i]).scheme
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
return await fn(p);
|
|
237
|
+
} catch (err) {
|
|
238
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
239
|
+
errors.push(e);
|
|
240
|
+
opts?.onError?.(e);
|
|
241
|
+
const canContinue = isFallbackable(e) || e instanceof CapabilityNotSupportedError;
|
|
242
|
+
if (!canContinue || i === allURIs.length - 1) {
|
|
243
|
+
if (errors.length > 1) {
|
|
244
|
+
throw new FallbackExhaustedError(errors);
|
|
245
|
+
}
|
|
246
|
+
throw e;
|
|
247
|
+
}
|
|
248
|
+
opts?.onFallback?.(i, i + 1, e);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
throw new FallbackExhaustedError(errors);
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
async complete(request) {
|
|
255
|
+
opts?.onRequest?.(request);
|
|
256
|
+
const start = Date.now();
|
|
257
|
+
const resp = await withFallback(
|
|
258
|
+
"complete",
|
|
259
|
+
(p) => p.complete(request)
|
|
260
|
+
);
|
|
261
|
+
opts?.onResponse?.(resp, Date.now() - start);
|
|
262
|
+
return resp;
|
|
263
|
+
},
|
|
264
|
+
stream(request) {
|
|
265
|
+
return {
|
|
266
|
+
[Symbol.asyncIterator]() {
|
|
267
|
+
let inner = null;
|
|
268
|
+
let initialized = false;
|
|
269
|
+
return {
|
|
270
|
+
async next() {
|
|
271
|
+
if (!initialized) {
|
|
272
|
+
initialized = true;
|
|
273
|
+
opts?.onRequest?.(request);
|
|
274
|
+
const errors = [];
|
|
275
|
+
for (let i = 0; i < allURIs.length; i++) {
|
|
276
|
+
try {
|
|
277
|
+
const p = resolveAt(i);
|
|
278
|
+
if (!("stream" in p) || typeof p.stream !== "function") {
|
|
279
|
+
throw new CapabilityNotSupportedError(
|
|
280
|
+
"stream",
|
|
281
|
+
parseURI(allURIs[i]).scheme
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
const iterable = p.stream(request);
|
|
285
|
+
const iter = iterable[Symbol.asyncIterator]();
|
|
286
|
+
const first = await iter.next();
|
|
287
|
+
inner = iter;
|
|
288
|
+
return first;
|
|
289
|
+
} catch (err) {
|
|
290
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
291
|
+
errors.push(e);
|
|
292
|
+
opts?.onError?.(e);
|
|
293
|
+
const canContinue = isFallbackable(e) || e instanceof CapabilityNotSupportedError;
|
|
294
|
+
if (!canContinue || i === allURIs.length - 1) {
|
|
295
|
+
if (errors.length > 1)
|
|
296
|
+
throw new FallbackExhaustedError(errors);
|
|
297
|
+
throw e;
|
|
298
|
+
}
|
|
299
|
+
opts?.onFallback?.(i, i + 1, e);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
throw new FallbackExhaustedError(errors);
|
|
303
|
+
}
|
|
304
|
+
return inner.next();
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
},
|
|
310
|
+
async callWithTools(request, tools) {
|
|
311
|
+
opts?.onRequest?.(request);
|
|
312
|
+
const start = Date.now();
|
|
313
|
+
const resp = await withFallback(
|
|
314
|
+
"callWithTools",
|
|
315
|
+
(p) => p.callWithTools(request, tools)
|
|
316
|
+
);
|
|
317
|
+
opts?.onResponse?.(
|
|
318
|
+
{ content: resp.content, role: "assistant" },
|
|
319
|
+
Date.now() - start
|
|
320
|
+
);
|
|
321
|
+
return resp;
|
|
322
|
+
},
|
|
323
|
+
capabilities() {
|
|
324
|
+
const caps = [];
|
|
325
|
+
if (primary.complete) caps.push("complete");
|
|
326
|
+
if (primary.stream) caps.push("stream");
|
|
327
|
+
if (primary.callWithTools) caps.push("callWithTools");
|
|
328
|
+
return caps;
|
|
329
|
+
},
|
|
330
|
+
provider() {
|
|
331
|
+
return primary;
|
|
332
|
+
},
|
|
333
|
+
close() {
|
|
334
|
+
for (const p of resolvedProviders.values()) {
|
|
335
|
+
p.close();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
341
|
+
0 && (module.exports = {
|
|
342
|
+
AuthError,
|
|
343
|
+
CapabilityNotSupportedError,
|
|
344
|
+
FallbackExhaustedError,
|
|
345
|
+
LLMError,
|
|
346
|
+
ModelError,
|
|
347
|
+
ProviderNotFoundError,
|
|
348
|
+
RateLimitError,
|
|
349
|
+
clearRegistry,
|
|
350
|
+
createLLM,
|
|
351
|
+
isFallbackable,
|
|
352
|
+
loadConfig,
|
|
353
|
+
parseURI,
|
|
354
|
+
register,
|
|
355
|
+
resolve
|
|
356
|
+
});
|
package/dist/output.d.ts
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output/formatter
|
|
3
|
+
*
|
|
4
|
+
* Core Formatter interface + Options/OptionSpec/ColumnSpec types and
|
|
5
|
+
* parseOptions helper. Mirrors Go's go/console/output Formatter interface.
|
|
6
|
+
*/
|
|
7
|
+
/** Kinds of values an OptionSpec accepts. */
|
|
8
|
+
type OptionType = 'string' | 'int' | 'bool' | 'enum';
|
|
9
|
+
/** Describes one option accepted by a Formatter via --format-opt key=value. */
|
|
10
|
+
interface OptionSpec {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly type: OptionType;
|
|
13
|
+
readonly default?: string | number | boolean;
|
|
14
|
+
readonly usage: string;
|
|
15
|
+
readonly enum?: readonly string[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Validated map of option values produced by parseOptions. Keys are
|
|
19
|
+
* OptionSpec.name; values are coerced to the spec's declared type.
|
|
20
|
+
*/
|
|
21
|
+
type Options = Readonly<Record<string, string | number | boolean>>;
|
|
22
|
+
/**
|
|
23
|
+
* Column metadata used by table/csv/text formatters and --cols validation.
|
|
24
|
+
*
|
|
25
|
+
* - `header` is the user-visible label and the value matched against --cols.
|
|
26
|
+
* - `key` is the property name on the row object.
|
|
27
|
+
* - `priority` (optional) controls hide-on-overflow for table; higher wins.
|
|
28
|
+
*/
|
|
29
|
+
interface ColumnSpec {
|
|
30
|
+
readonly header: string;
|
|
31
|
+
readonly key: string;
|
|
32
|
+
readonly priority?: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A Formatter encodes structured data to a writable stream.
|
|
36
|
+
*
|
|
37
|
+
* Implementations declare their key, file extensions, and the option keys
|
|
38
|
+
* they accept. The dispatch helper validates --format-opt input against
|
|
39
|
+
* options() before invoking render(), so render() may trust opts to only
|
|
40
|
+
* contain declared keys with values coerced to declared types.
|
|
41
|
+
*/
|
|
42
|
+
interface Formatter<T = unknown> {
|
|
43
|
+
/** Unique format identifier exposed via --format <key>. */
|
|
44
|
+
readonly key: string;
|
|
45
|
+
/**
|
|
46
|
+
* File extensions (with leading dot, e.g. ".csv") that map to this
|
|
47
|
+
* formatter for --output extension inference. May be empty.
|
|
48
|
+
*/
|
|
49
|
+
readonly extensions: readonly string[];
|
|
50
|
+
/** Option specs accepted by this formatter via --format-opt key=value. */
|
|
51
|
+
readonly options: readonly OptionSpec[];
|
|
52
|
+
/**
|
|
53
|
+
* Renders data to out.
|
|
54
|
+
*
|
|
55
|
+
* @param out Destination writable stream.
|
|
56
|
+
* @param data Single row or readonly array of rows.
|
|
57
|
+
* @param opts Validated options; only declared keys with coerced values.
|
|
58
|
+
* @param cols User-requested column projection; empty means "all columns".
|
|
59
|
+
*/
|
|
60
|
+
render(out: NodeJS.WritableStream, data: T | readonly T[], opts: Options, cols: readonly string[]): Promise<void> | void;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Validates raw `key=value` pairs against specs and returns a coerced
|
|
64
|
+
* Options map. Unknown keys, type errors, and out-of-enum values throw an
|
|
65
|
+
* Error listing the offending key and the valid set.
|
|
66
|
+
*
|
|
67
|
+
* A pair without `=` (e.g. `"no-header"`) is treated as bool true; only
|
|
68
|
+
* valid when the matching spec has type `'bool'`.
|
|
69
|
+
*
|
|
70
|
+
* Defaults from specs fill in any keys not present in pairs.
|
|
71
|
+
*/
|
|
72
|
+
declare function parseOptions(pairs: readonly string[], specs: readonly OptionSpec[]): Options;
|
|
73
|
+
/** Pretty-print an OptionType. */
|
|
74
|
+
declare function optionTypeName(t: OptionType): string;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @module output/registry
|
|
78
|
+
*
|
|
79
|
+
* Registry holds Formatter implementations keyed by Formatter.key. Mirrors
|
|
80
|
+
* Go's *Registry. Built-in formatters register against `defaultRegistry` at
|
|
81
|
+
* module-load time.
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Registry holds Formatter implementations.
|
|
86
|
+
*
|
|
87
|
+
* - `register` throws on duplicate keys (init-time fail-loud).
|
|
88
|
+
* - `override` quietly replaces — use when intentionally swapping a built-in.
|
|
89
|
+
* - `lookup` returns the formatter for a key, or undefined.
|
|
90
|
+
* - `keys` returns sorted format identifiers.
|
|
91
|
+
* - `formatters` returns sorted formatters.
|
|
92
|
+
* - `extensionMap` returns ext→key (e.g. ".csv" → "csv"); later
|
|
93
|
+
* registrations win on collision (alphabetical key order is the tie-break).
|
|
94
|
+
*/
|
|
95
|
+
declare class Registry {
|
|
96
|
+
private readonly byKey;
|
|
97
|
+
/**
|
|
98
|
+
* Adds f to the registry. Throws Error on duplicate key or empty key.
|
|
99
|
+
* Use `override` to intentionally replace.
|
|
100
|
+
*/
|
|
101
|
+
register<T>(f: Formatter<T>): void;
|
|
102
|
+
/** Replaces (or registers) the formatter for f.key. */
|
|
103
|
+
override<T>(f: Formatter<T>): void;
|
|
104
|
+
/** Returns the formatter registered under key, if any. */
|
|
105
|
+
lookup(key: string): Formatter | undefined;
|
|
106
|
+
/** Returns all registered format keys, sorted alphabetically. */
|
|
107
|
+
keys(): readonly string[];
|
|
108
|
+
/** Returns all registered formatters in key order. */
|
|
109
|
+
formatters(): readonly Formatter[];
|
|
110
|
+
/**
|
|
111
|
+
* Returns extension→key mappings (e.g. ".csv" → "csv") across all
|
|
112
|
+
* registered formatters. Later registrations win on collision; iteration
|
|
113
|
+
* is in sorted-key order so resolution is deterministic.
|
|
114
|
+
*/
|
|
115
|
+
extensionMap(): ReadonlyMap<string, string>;
|
|
116
|
+
}
|
|
117
|
+
/** Module-level Registry. Built-ins register here at module-load time. */
|
|
118
|
+
declare const defaultRegistry: Registry;
|
|
119
|
+
/** Returns a new, empty Registry — no built-ins registered. */
|
|
120
|
+
declare function newRegistry(): Registry;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @module output/formatters/json
|
|
124
|
+
*
|
|
125
|
+
* Built-in JSON formatter. Honors `indent` option (default 2) + trailing
|
|
126
|
+
* newline. When `cols` is non-empty, projects rows to header-keyed objects.
|
|
127
|
+
*/
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Build the JSON formatter. The `columns` ref is optional; when provided
|
|
131
|
+
* (via dispatch with a ColumnSpec list), rows are projected before encode.
|
|
132
|
+
*/
|
|
133
|
+
declare const jsonFormatter: Formatter;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @module output/formatters/yaml
|
|
137
|
+
*
|
|
138
|
+
* Built-in YAML formatter. Honors `flow-level` option (default -1, mapping
|
|
139
|
+
* to js-yaml's default block style). When `cols` is non-empty, projects
|
|
140
|
+
* rows to header-keyed objects.
|
|
141
|
+
*/
|
|
142
|
+
|
|
143
|
+
declare const yamlFormatter: Formatter;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @module output/formatters/table
|
|
147
|
+
*
|
|
148
|
+
* Built-in table formatter. Hand-rolled aligner (matches the original
|
|
149
|
+
* output.ts behaviour). Honors `cols` to filter columns; preserves first-
|
|
150
|
+
* row key order otherwise.
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
declare const tableFormatter: Formatter;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @module output
|
|
157
|
+
* @package @hop-top/kit
|
|
158
|
+
*
|
|
159
|
+
* Output rendering — writes structured values to a writable stream in one of
|
|
160
|
+
* the registered formats: table, JSON, YAML, CSV, text, etc.
|
|
161
|
+
*
|
|
162
|
+
* ## Quick start
|
|
163
|
+
*
|
|
164
|
+
* ```ts
|
|
165
|
+
* import { render, TABLE_FORMAT, JSON_FORMAT, YAML_FORMAT } from '@hop-top/kit/output';
|
|
166
|
+
*
|
|
167
|
+
* // Write a table to stdout
|
|
168
|
+
* render(process.stdout, TABLE_FORMAT, [{ id: '1', name: 'Alice' }]);
|
|
169
|
+
*
|
|
170
|
+
* // Write indented JSON
|
|
171
|
+
* render(process.stdout, JSON_FORMAT, { ok: true });
|
|
172
|
+
*
|
|
173
|
+
* // Write YAML
|
|
174
|
+
* render(process.stdout, YAML_FORMAT, { version: 1 });
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* ## Programmatic Formatter API
|
|
178
|
+
*
|
|
179
|
+
* For typed flag wiring + custom formatters use the Formatter / Registry
|
|
180
|
+
* surface from `output/formatter` and `output/registry`. The `render()`
|
|
181
|
+
* helper exported here is a thin shim over `defaultRegistry`.
|
|
182
|
+
*
|
|
183
|
+
* ## Backward compatibility
|
|
184
|
+
*
|
|
185
|
+
* `render(w, format, v)` keeps its original signature. New per-format
|
|
186
|
+
* options + column projection are accessed via `dispatch()`.
|
|
187
|
+
*/
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Supported output formats. Includes the original three for backward-compat
|
|
191
|
+
* + new built-ins (csv, text) registered on the default registry.
|
|
192
|
+
*/
|
|
193
|
+
type Format = string;
|
|
194
|
+
/** Constant for the JSON output format. */
|
|
195
|
+
declare const JSON_FORMAT: "json";
|
|
196
|
+
/** Constant for the YAML output format. */
|
|
197
|
+
declare const YAML_FORMAT: "yaml";
|
|
198
|
+
/** Constant for the table output format. */
|
|
199
|
+
declare const TABLE_FORMAT: "table";
|
|
200
|
+
/** Constant for the CSV output format. */
|
|
201
|
+
declare const CSV_FORMAT: "csv";
|
|
202
|
+
/** Constant for the text output format. */
|
|
203
|
+
declare const TEXT_FORMAT: "text";
|
|
204
|
+
/**
|
|
205
|
+
* Renders `v` to `w` in the requested `format`.
|
|
206
|
+
*
|
|
207
|
+
* Backward-compatible thin shim over `defaultRegistry.lookup(format).render`.
|
|
208
|
+
*
|
|
209
|
+
* @throws {Error} For unknown format strings.
|
|
210
|
+
*/
|
|
211
|
+
declare function render(w: NodeJS.WritableStream, format: Format, v: unknown): void;
|
|
212
|
+
|
|
213
|
+
export { CSV_FORMAT, type ColumnSpec, type Format, type Formatter, JSON_FORMAT, type OptionSpec, type OptionType, type Options, Registry, TABLE_FORMAT, TEXT_FORMAT, YAML_FORMAT, defaultRegistry, jsonFormatter, newRegistry, optionTypeName, parseOptions, render, tableFormatter, yamlFormatter };
|