@easynet/agent-llm 1.0.49 → 1.0.50

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.
Files changed (94) hide show
  1. package/README.md +18 -6
  2. package/dist/api/create-agent-llm.d.ts +2 -2
  3. package/dist/api/create-agent-llm.d.ts.map +1 -1
  4. package/dist/api/create-embed-fn.d.ts +10 -0
  5. package/dist/api/create-embed-fn.d.ts.map +1 -0
  6. package/dist/api/get-default-llm-config.d.ts +3 -3
  7. package/dist/api/get-default-llm-config.d.ts.map +1 -1
  8. package/dist/chunk-36PPGV4O.js +1 -0
  9. package/dist/chunk-36PPGV4O.js.map +1 -0
  10. package/dist/chunk-6EQCGQTV.js +154 -0
  11. package/dist/chunk-6EQCGQTV.js.map +1 -0
  12. package/dist/chunk-7VIJ4MFU.js +139 -0
  13. package/dist/chunk-7VIJ4MFU.js.map +1 -0
  14. package/dist/chunk-AUQEXHUP.js +105 -0
  15. package/dist/chunk-AUQEXHUP.js.map +1 -0
  16. package/dist/chunk-FVQ3PKER.js +354 -0
  17. package/dist/chunk-FVQ3PKER.js.map +1 -0
  18. package/dist/chunk-FZWXYGR6.js +191 -0
  19. package/dist/chunk-FZWXYGR6.js.map +1 -0
  20. package/dist/chunk-G7MKWPEI.js +14 -0
  21. package/dist/chunk-G7MKWPEI.js.map +1 -0
  22. package/dist/chunk-KE7IMUSA.js +223 -0
  23. package/dist/chunk-KE7IMUSA.js.map +1 -0
  24. package/dist/chunk-SPDXNDDD.js +114 -0
  25. package/dist/chunk-SPDXNDDD.js.map +1 -0
  26. package/dist/chunk-WK6P4EOC.js +147 -0
  27. package/dist/chunk-WK6P4EOC.js.map +1 -0
  28. package/dist/cli/index.js +18 -3
  29. package/dist/cli/index.js.map +1 -1
  30. package/dist/config/index.d.ts +1 -0
  31. package/dist/config/index.d.ts.map +1 -1
  32. package/dist/config/index.js +40 -0
  33. package/dist/config/index.js.map +1 -0
  34. package/dist/config/loader.d.ts +15 -0
  35. package/dist/config/loader.d.ts.map +1 -1
  36. package/dist/config/parser.d.ts +2 -2
  37. package/dist/config/parser.d.ts.map +1 -1
  38. package/dist/config/yaml-utils.d.ts +13 -0
  39. package/dist/config/yaml-utils.d.ts.map +1 -0
  40. package/dist/connectivity/check.d.ts +18 -0
  41. package/dist/connectivity/check.d.ts.map +1 -0
  42. package/dist/connectivity/index.d.ts +3 -0
  43. package/dist/connectivity/index.d.ts.map +1 -0
  44. package/dist/connectivity/index.js +11 -0
  45. package/dist/connectivity/index.js.map +1 -0
  46. package/dist/connectivity/types.d.ts +13 -0
  47. package/dist/connectivity/types.d.ts.map +1 -0
  48. package/dist/extensions/index.js +36 -0
  49. package/dist/extensions/index.js.map +1 -0
  50. package/dist/extensions/npm-protocol.d.ts +1 -1
  51. package/dist/extensions/npm-protocol.d.ts.map +1 -1
  52. package/dist/index.d.ts +4 -11
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +46 -26
  55. package/dist/index.js.map +1 -1
  56. package/dist/langchain/index.js +11 -0
  57. package/dist/langchain/index.js.map +1 -0
  58. package/dist/model/chat.d.ts +30 -0
  59. package/dist/model/chat.d.ts.map +1 -0
  60. package/dist/model/embed-parser.d.ts +25 -0
  61. package/dist/model/embed-parser.d.ts.map +1 -0
  62. package/dist/model/embedding.d.ts +25 -0
  63. package/dist/model/embedding.d.ts.map +1 -0
  64. package/dist/model/hub.d.ts +26 -3
  65. package/dist/model/hub.d.ts.map +1 -1
  66. package/dist/model/index.d.ts +13 -0
  67. package/dist/model/index.d.ts.map +1 -0
  68. package/dist/model/index.js +17 -0
  69. package/dist/model/index.js.map +1 -0
  70. package/dist/model/llm-parser.d.ts +10 -0
  71. package/dist/model/llm-parser.d.ts.map +1 -0
  72. package/dist/model/types.d.ts +31 -0
  73. package/dist/model/types.d.ts.map +1 -0
  74. package/dist/npm/command.d.ts +37 -0
  75. package/dist/npm/command.d.ts.map +1 -0
  76. package/dist/npm/index.d.ts +5 -0
  77. package/dist/npm/index.d.ts.map +1 -0
  78. package/dist/npm/index.js +39 -0
  79. package/dist/npm/index.js.map +1 -0
  80. package/dist/npm/install.d.ts +9 -0
  81. package/dist/npm/install.d.ts.map +1 -0
  82. package/dist/npm/provider.d.ts +15 -0
  83. package/dist/npm/provider.d.ts.map +1 -0
  84. package/dist/npm/version.d.ts +12 -0
  85. package/dist/npm/version.d.ts.map +1 -0
  86. package/dist/registry/chat-model.d.ts +1 -1
  87. package/dist/registry/chat-model.d.ts.map +1 -1
  88. package/dist/registry/index.js +9 -0
  89. package/dist/registry/index.js.map +1 -0
  90. package/dist/types.d.ts +2 -2
  91. package/dist/types.d.ts.map +1 -1
  92. package/package.json +39 -1
  93. package/dist/chunk-YK4EBPJ3.js +0 -578
  94. package/dist/chunk-YK4EBPJ3.js.map +0 -1
@@ -1,578 +0,0 @@
1
- // src/registry/chat-model.ts
2
- var CHAT_MODEL_FACTORIES = /* @__PURE__ */ new Map();
3
- function registerChatModelProvider(providerName, factory) {
4
- CHAT_MODEL_FACTORIES.set(providerName.toLowerCase(), factory);
5
- }
6
- function getChatModelFactory(providerName) {
7
- return CHAT_MODEL_FACTORIES.get(providerName.toLowerCase());
8
- }
9
-
10
- // src/langchain/openai-compatible.ts
11
- import { ChatOpenAI } from "@langchain/openai";
12
- function createChatOpenAI(options) {
13
- const {
14
- baseURL,
15
- model,
16
- temperature = 0,
17
- apiKey,
18
- defaultHeaders,
19
- defaultQuery,
20
- httpAgent
21
- } = options;
22
- const config = {};
23
- if (baseURL) config.baseURL = baseURL;
24
- if (defaultHeaders) config.defaultHeaders = defaultHeaders;
25
- if (defaultQuery) config.defaultQuery = defaultQuery;
26
- if (httpAgent) config.httpAgent = httpAgent;
27
- return new ChatOpenAI({
28
- model,
29
- temperature,
30
- ...apiKey ? { apiKey } : {},
31
- ...Object.keys(config).length > 0 ? { configuration: config } : {}
32
- });
33
- }
34
-
35
- // src/langchain/tool-choice.ts
36
- function hasTools(model, options) {
37
- const optTools = options?.tools;
38
- if (Array.isArray(optTools) && optTools.length > 0) return true;
39
- const defaultTools = model.defaultOptions?.tools;
40
- return Array.isArray(defaultTools) && defaultTools.length > 0;
41
- }
42
- function sanitizeOptions(model, options) {
43
- if (!options) return options;
44
- if (options.tool_choice !== "none") return options;
45
- if (!hasTools(model, options)) return options;
46
- return { ...options, tool_choice: "auto" };
47
- }
48
- function applyToolChoiceAuto(model) {
49
- const m = model;
50
- if (m.__agentLlmToolChoicePatched) return;
51
- m.__agentLlmToolChoicePatched = true;
52
- const origBindTools = m.bindTools?.bind(model);
53
- if (origBindTools) {
54
- m.bindTools = function(tools, opts) {
55
- const bound = origBindTools(tools, { ...opts, tool_choice: "auto" });
56
- applyToolChoiceAuto(
57
- bound
58
- );
59
- return bound;
60
- };
61
- }
62
- const origWithConfig = m.withConfig?.bind(model);
63
- if (origWithConfig) {
64
- m.withConfig = function(config) {
65
- const sanitized = sanitizeOptions(this, config) ?? config;
66
- const next = origWithConfig(sanitized);
67
- applyToolChoiceAuto(
68
- next
69
- );
70
- return next;
71
- };
72
- }
73
- const origInvoke = m.invoke?.bind(model);
74
- if (origInvoke) {
75
- m.invoke = function(input, options) {
76
- return origInvoke(input, sanitizeOptions(this, options));
77
- };
78
- }
79
- const origStream = m.stream?.bind(model);
80
- if (origStream) {
81
- m.stream = function(input, options) {
82
- return origStream(input, sanitizeOptions(this, options));
83
- };
84
- }
85
- }
86
-
87
- // src/langchain/index.ts
88
- import { parseLlmSection } from "@easynet/agent-common";
89
- var DEFAULT_MODEL = "gpt-4o-mini";
90
- function normalizeError(e, context) {
91
- if (e instanceof Error) return new Error(`${context}: ${e.message}`, { cause: e });
92
- return new Error(`${context}: ${String(e)}`);
93
- }
94
- function createChatModelFromLlmConfig(options = {}) {
95
- const { llmSection, modelEnv, apiKeyEnv } = options;
96
- let defaultId;
97
- let configs;
98
- try {
99
- const parsed = parseLlmSection(llmSection ?? null);
100
- defaultId = parsed.defaultId;
101
- configs = parsed.configs;
102
- } catch (e) {
103
- throw normalizeError(e, "Failed to parse llm section");
104
- }
105
- const config = configs.find((c) => c.id === defaultId) ?? configs[0];
106
- if (!config) {
107
- const model2 = modelEnv ?? process.env.OPENAI_MODEL ?? DEFAULT_MODEL;
108
- const apiKey2 = apiKeyEnv ?? process.env.OPENAI_API_KEY;
109
- return createChatOpenAI({
110
- model: model2,
111
- temperature: 0,
112
- ...apiKey2 ? { apiKey: apiKey2 } : {}
113
- });
114
- }
115
- const provider = config.provider ?? "openai";
116
- const factory = getChatModelFactory(provider);
117
- if (factory) {
118
- try {
119
- return factory({
120
- ...config,
121
- model: modelEnv ?? config.model ?? DEFAULT_MODEL,
122
- temperature: typeof config.temperature === "number" ? config.temperature : 0
123
- });
124
- } catch (e) {
125
- throw normalizeError(e, `Failed to create ChatModel for provider "${provider}"`);
126
- }
127
- }
128
- const model = modelEnv ?? config.model ?? process.env.OPENAI_MODEL ?? DEFAULT_MODEL;
129
- let apiKey = apiKeyEnv ?? config.apiKey ?? process.env.OPENAI_API_KEY;
130
- let baseURL = config.baseURL;
131
- if (baseURL && !baseURL.replace(/\/$/, "").endsWith("/v1")) {
132
- baseURL = baseURL.replace(/\/$/, "") + "/v1";
133
- }
134
- if (baseURL && !apiKey) {
135
- apiKey = "not-needed";
136
- }
137
- const temperature = typeof config.temperature === "number" ? config.temperature : 0;
138
- const opts = config.options;
139
- const defaultHeaders = opts?.defaultHeaders;
140
- const defaultQuery = opts?.defaultQuery;
141
- const httpAgent = opts?.httpAgent;
142
- return createChatOpenAI({
143
- model,
144
- temperature,
145
- baseURL,
146
- apiKey,
147
- defaultHeaders,
148
- defaultQuery,
149
- httpAgent
150
- });
151
- }
152
-
153
- // src/types.ts
154
- var AGENT_LLM_PROVIDER_FIELD = "agentLlmProvider";
155
-
156
- // src/config/loader.ts
157
- import { loadYamlFileSync, parseYamlContent } from "@easynet/agent-common";
158
- function loadLlmConfig(filePath, options = {}) {
159
- if (typeof filePath !== "string" || filePath.trim().length === 0) {
160
- throw new Error("agent-llm: loadLlmConfig requires a non-empty file path");
161
- }
162
- try {
163
- const parsed = loadYamlFileSync(filePath, {
164
- substituteEnv: options.substituteEnv !== false,
165
- missingEnv: "keep",
166
- cache: true
167
- });
168
- if (parsed == null) return null;
169
- const llm = parsed.llm;
170
- return llm ?? null;
171
- } catch (e) {
172
- const msg = e instanceof Error ? e.message : String(e);
173
- throw new Error(`agent-llm: failed to parse config file ${filePath}: ${msg}`, { cause: e });
174
- }
175
- }
176
-
177
- // src/api/create-agent-llm.ts
178
- import { join as join2 } from "path";
179
- import {
180
- checkEndpointConnectivity,
181
- buildUnreachableError,
182
- parseLlmSection as parseLlmSection2
183
- } from "@easynet/agent-common";
184
-
185
- // src/extensions/loader.ts
186
- import { readdirSync, readFileSync, existsSync } from "fs";
187
- import { createRequire } from "module";
188
- import { join } from "path";
189
- import { pathToFileURL } from "url";
190
- var loadedPackages = /* @__PURE__ */ new Set();
191
- function isLLMExtension(m) {
192
- if (m == null || typeof m !== "object") return false;
193
- const e = m;
194
- if (typeof e.providerName !== "string") return false;
195
- const hasRegister = typeof e.register === "function";
196
- const hasChatModel = typeof e.createChatModel === "function";
197
- return hasRegister || hasChatModel;
198
- }
199
- function registerExtension(ext) {
200
- if (typeof ext.createChatModel === "function") {
201
- registerChatModelProvider(ext.providerName, ext.createChatModel);
202
- return;
203
- }
204
- if (typeof ext.register === "function") {
205
- ext.register();
206
- }
207
- }
208
- function getExtensionFromModule(m) {
209
- if (isLLMExtension(m)) return m;
210
- if (typeof m.getLLMExtension === "function") {
211
- const ext = m.getLLMExtension();
212
- return isLLMExtension(ext) ? ext : null;
213
- }
214
- if (isLLMExtension(m.default)) return m.default;
215
- return null;
216
- }
217
- async function loadModuleFromPath(pkgDir) {
218
- const pkgJsonPath = join(pkgDir, "package.json");
219
- if (!existsSync(pkgJsonPath)) {
220
- throw new Error(`package.json not found in ${pkgDir}`);
221
- }
222
- const pkgJson = JSON.parse(readFileSync(pkgJsonPath, "utf-8"));
223
- const main = pkgJson?.main ?? "index.js";
224
- const entryPath = join(pkgDir, main);
225
- if (!existsSync(entryPath)) {
226
- throw new Error(`Entry ${main} not found in ${pkgDir}`);
227
- }
228
- const entryUrl = pathToFileURL(entryPath).href;
229
- return await import(
230
- /* @vite-ignore */
231
- entryUrl
232
- );
233
- }
234
- function resolvePackageEntryFromCwd(pkg, cwd) {
235
- const pkgDir = join(cwd, "node_modules", pkg);
236
- const pkgJsonPath = join(pkgDir, "package.json");
237
- if (!existsSync(pkgJsonPath)) return null;
238
- try {
239
- const req = createRequire(pkgJsonPath);
240
- return req.resolve(pkg);
241
- } catch {
242
- return null;
243
- }
244
- }
245
- async function loadLLMExtensions(extensionPackages, options) {
246
- const packages = Array.isArray(extensionPackages) ? extensionPackages.filter((p) => typeof p === "string" && String(p).trim().length > 0) : [];
247
- const cwd = options?.cwd ?? process.cwd();
248
- for (const pkg of packages) {
249
- if (loadedPackages.has(pkg)) continue;
250
- loadedPackages.add(pkg);
251
- let loaded = false;
252
- const cwdPkgDir = join(cwd, "node_modules", pkg);
253
- const cwdIsProject = existsSync(join(cwd, "package.json")) && (() => {
254
- try {
255
- const name = JSON.parse(readFileSync(join(cwd, "package.json"), "utf-8")).name;
256
- return name === pkg;
257
- } catch {
258
- return false;
259
- }
260
- })();
261
- const dirsToTry = [cwdPkgDir, ...cwdIsProject ? [cwd] : []];
262
- for (const pkgDir of dirsToTry) {
263
- if (!existsSync(join(pkgDir, "package.json"))) continue;
264
- try {
265
- const m = await loadModuleFromPath(pkgDir);
266
- const ext = getExtensionFromModule(m);
267
- if (ext) {
268
- registerExtension(ext);
269
- loaded = true;
270
- break;
271
- }
272
- if (typeof m.registerLLMExtension === "function") {
273
- m.registerLLMExtension();
274
- loaded = true;
275
- break;
276
- }
277
- } catch {
278
- }
279
- }
280
- if (loaded) continue;
281
- const resolvedEntry = resolvePackageEntryFromCwd(pkg, cwd);
282
- if (resolvedEntry) {
283
- try {
284
- const entryUrl = pathToFileURL(resolvedEntry).href;
285
- const m = await import(
286
- /* @vite-ignore */
287
- entryUrl
288
- );
289
- const ext = getExtensionFromModule(m);
290
- if (ext) {
291
- registerExtension(ext);
292
- continue;
293
- }
294
- if (typeof m.registerLLMExtension === "function") {
295
- m.registerLLMExtension();
296
- continue;
297
- }
298
- } catch {
299
- }
300
- }
301
- try {
302
- const m = await import(
303
- /* @vite-ignore */
304
- pkg
305
- );
306
- const ext = getExtensionFromModule(m);
307
- if (ext) {
308
- registerExtension(ext);
309
- continue;
310
- }
311
- if (typeof m.registerLLMExtension === "function") {
312
- m.registerLLMExtension();
313
- }
314
- } catch (e) {
315
- const msg = e instanceof Error ? e.message : String(e);
316
- if (typeof process !== "undefined" && process.emitWarning) {
317
- process.emitWarning(`[agent-llm] Failed to load extension "${pkg}": ${msg}`, { code: "AGENT_LLM_EXTENSION_LOAD" });
318
- }
319
- }
320
- }
321
- }
322
-
323
- // src/extensions/npm-protocol.ts
324
- import {
325
- NPM_PROTOCOL_PREFIX,
326
- parseNpmProviderSpec,
327
- isNpmProviderSpec,
328
- resolveLatestVersionFromRegistry,
329
- getInstalledVersion as getInstalledVersionFromCommon,
330
- ensureNpmPackageInstalled as ensureNpmPackageInstalledFromCommon
331
- } from "@easynet/agent-common/npm";
332
- function isModuleNotFoundError(err) {
333
- const msg = err instanceof Error ? err.message : String(err);
334
- return msg.includes("Cannot find module") || msg.includes("Cannot find package") || msg.includes("MODULE_NOT_FOUND") || msg.includes("ERR_MODULE_NOT_FOUND") || msg.includes("Failed to load url") || msg.includes("Does the file exist");
335
- }
336
- async function ensureNpmPackageInstalled(packageName, options = {}) {
337
- await ensureNpmPackageInstalledFromCommon(packageName, {
338
- version: options.version,
339
- cwd: options.cwd,
340
- stdio: "inherit",
341
- noPackageLock: true,
342
- logPrefix: "[agent-llm]"
343
- });
344
- }
345
- function normalizeError2(e, context) {
346
- if (e instanceof Error) return new Error(`${context}: ${e.message}`, { cause: e });
347
- return new Error(`${context}: ${String(e)}`);
348
- }
349
- async function resolveNpmProvider(spec, options = {}) {
350
- if (typeof spec !== "string" || spec.trim().length === 0) return null;
351
- const parsed = parseNpmProviderSpec(spec);
352
- if (!parsed) return null;
353
- const { packageName, version, provider: fragmentProvider } = parsed;
354
- const installNpmIfMissing = options.installNpmIfMissing !== false;
355
- const cwd = options.cwd ?? process.cwd();
356
- const load = async () => {
357
- await loadLLMExtensions([packageName], { cwd });
358
- };
359
- if (installNpmIfMissing) {
360
- await ensureNpmPackageInstalled(packageName, { version, cwd });
361
- }
362
- try {
363
- await load();
364
- } catch (err) {
365
- if (installNpmIfMissing && isModuleNotFoundError(err)) {
366
- try {
367
- await ensureNpmPackageInstalled(packageName, { version, cwd });
368
- await load();
369
- } catch (installErr) {
370
- throw normalizeError2(installErr, `Failed to install or load npm provider "${packageName}"`);
371
- }
372
- } else {
373
- throw normalizeError2(err, `Failed to load npm provider "${packageName}"`);
374
- }
375
- }
376
- if (fragmentProvider && !getChatModelFactory(fragmentProvider)) {
377
- if (installNpmIfMissing) {
378
- await ensureNpmPackageInstalled(packageName, { version, cwd });
379
- await load();
380
- }
381
- if (!getChatModelFactory(fragmentProvider)) {
382
- throw new Error(`Package "${packageName}" was installed but did not register provider "${fragmentProvider}". Check that the package exports getLLMExtension() or registerLLMExtension().`);
383
- }
384
- }
385
- if (fragmentProvider) return fragmentProvider;
386
- try {
387
- const m = await import(
388
- /* @vite-ignore */
389
- packageName
390
- );
391
- if (typeof m.getDefaultProviderName === "function") {
392
- return m.getDefaultProviderName();
393
- }
394
- } catch {
395
- }
396
- throw new Error(
397
- `Provider spec ${spec} has no #provider fragment and the package does not export getDefaultProviderName(). Use e.g. npm:${packageName}#<provider-name>.`
398
- );
399
- }
400
- async function resolveLlmSectionWithNpm(llmSection, options = {}) {
401
- if (llmSection == null) return llmSection;
402
- if (Array.isArray(llmSection)) {
403
- const out = [];
404
- for (let i = 0; i < llmSection.length; i++) {
405
- try {
406
- out.push(await resolveLlmSectionWithNpm(llmSection[i], options));
407
- } catch (e) {
408
- throw normalizeError2(e, `Failed to resolve llm section at index ${i}`);
409
- }
410
- }
411
- return out;
412
- }
413
- if (typeof llmSection === "object") {
414
- const out = {};
415
- for (const [k, v] of Object.entries(llmSection)) {
416
- if (k === "provider" && isNpmProviderSpec(v)) {
417
- try {
418
- const resolved = await resolveNpmProvider(v, options);
419
- out[k] = resolved ?? v;
420
- } catch (e) {
421
- throw normalizeError2(e, `Failed to resolve provider "${String(v)}"`);
422
- }
423
- continue;
424
- }
425
- try {
426
- out[k] = await resolveLlmSectionWithNpm(v, options);
427
- } catch (e) {
428
- throw normalizeError2(e, `Failed to resolve llm section key "${k}"`);
429
- }
430
- }
431
- return out;
432
- }
433
- return llmSection;
434
- }
435
-
436
- // src/api/create-agent-llm.ts
437
- function applyDefaultToolChoice(model) {
438
- const m = model;
439
- const orig = m.bindTools?.bind(model);
440
- if (!orig) return;
441
- m.bindTools = function(tools, opts) {
442
- return orig(tools, { ...opts, tool_choice: "auto" });
443
- };
444
- }
445
- var CIS_DEFAULT_RESOLVE_HOST = "s0010-ml-https.s0010.us-west-2.awswd";
446
- var CIS_DEFAULT_RESOLVE_IP = "10.210.98.124";
447
- function buildEndpointConnectivityOptions(config) {
448
- const opts = config.options ?? config;
449
- const provider = typeof config.provider === "string" ? config.provider : "";
450
- const baseURL = config.baseURL;
451
- const isCis = provider === "cis" || provider.includes("cis");
452
- const useCisDefault = isCis && baseURL.includes(CIS_DEFAULT_RESOLVE_HOST) && opts?.resolveHost == null;
453
- const resolveHost = opts?.resolveHost != null && typeof opts.resolveHost.from === "string" ? opts.resolveHost : useCisDefault ? { from: CIS_DEFAULT_RESOLVE_HOST, to: CIS_DEFAULT_RESOLVE_IP } : void 0;
454
- const host = typeof opts?.host === "string" ? opts.host : resolveHost ? resolveHost.from : void 0;
455
- if (resolveHost == null && host == null) return void 0;
456
- const verifySSL = opts?.verifySSL === true;
457
- const bypassAuth = opts?.bypassAuth !== false;
458
- return {
459
- resolveHost,
460
- host,
461
- verifySSL: resolveHost != null ? false : verifySSL ? true : void 0,
462
- bypassAuth: bypassAuth ? true : void 0,
463
- featureKey: typeof opts?.featureKey === "string" ? opts.featureKey : void 0
464
- };
465
- }
466
- function resolveDefaultConfigPath() {
467
- return join2(process.cwd(), "llm.yaml");
468
- }
469
- function normalizeOptions(configPathOrOptions) {
470
- if (configPathOrOptions == null) return {};
471
- if (typeof configPathOrOptions === "string") return { configPath: configPathOrOptions };
472
- return configPathOrOptions;
473
- }
474
- function normalizeError3(e, context) {
475
- if (e instanceof Error) return new Error(`${context}: ${e.message}`, { cause: e });
476
- return new Error(`${context}: ${String(e)}`);
477
- }
478
- async function ensureConnectivity(resolvedLlmSection, options) {
479
- let configs;
480
- try {
481
- const parsed = parseLlmSection2(resolvedLlmSection ?? null);
482
- configs = parsed.configs.filter(
483
- (c) => typeof c.baseURL === "string" && c.baseURL.length > 0 && (c.baseURL.startsWith("http://") || c.baseURL.startsWith("https://")) && !c.baseURL.includes("${")
484
- );
485
- } catch {
486
- return;
487
- }
488
- const shouldCheck = options.checkConnectivity !== false && configs.length > 0;
489
- if (!shouldCheck) return;
490
- const report = (status) => options.onConnectionStatus?.(status);
491
- const timeoutMs = options.connectivityTimeoutMs ?? 8e3;
492
- for (const config of configs) {
493
- const { id, baseURL } = config;
494
- report({
495
- phase: "checking",
496
- endpointId: id,
497
- baseURL,
498
- message: "Checking connection..."
499
- });
500
- const endpointOpts = buildEndpointConnectivityOptions(config);
501
- const result = await checkEndpointConnectivity(baseURL, {
502
- timeoutMs,
503
- ...endpointOpts
504
- });
505
- if (result.reachable) {
506
- report({
507
- phase: "reachable",
508
- endpointId: id,
509
- baseURL,
510
- message: result.message ?? "Connected"
511
- });
512
- } else {
513
- report({
514
- phase: "unreachable",
515
- endpointId: id,
516
- baseURL,
517
- message: result.message ?? "Unreachable"
518
- });
519
- throw new Error(buildUnreachableError(id, baseURL, result.message));
520
- }
521
- }
522
- }
523
- async function createAgentLlM(configPathOrOptions) {
524
- try {
525
- const options = normalizeOptions(configPathOrOptions);
526
- const configPath = options.configPath ?? resolveDefaultConfigPath();
527
- const llmSection = loadLlmConfig(configPath);
528
- if (llmSection == null) {
529
- throw new Error(
530
- `No LLM config at ${configPath}. Add llm.yaml in the current directory, or pass configPath.`
531
- );
532
- }
533
- const resolvedSection = await resolveLlmSectionWithNpm(llmSection, {
534
- installNpmIfMissing: options.installNpmIfMissing !== false,
535
- cwd: process.cwd()
536
- });
537
- await ensureConnectivity(resolvedSection, {
538
- checkConnectivity: options.checkConnectivity,
539
- onConnectionStatus: options.onConnectionStatus,
540
- connectivityTimeoutMs: options.connectivityTimeoutMs
541
- });
542
- const model = createChatModelFromLlmConfig({ llmSection: resolvedSection });
543
- applyDefaultToolChoice(model);
544
- return model;
545
- } catch (e) {
546
- if (e instanceof Error && e.message.includes("No LLM config")) throw e;
547
- if (e instanceof Error && e.message.includes("Cannot connect to")) throw e;
548
- throw normalizeError3(e, "createAgentLlM failed");
549
- }
550
- }
551
-
552
- // src/cli/utils.ts
553
- function messageContentToString(content) {
554
- if (typeof content === "string") return content;
555
- if (Array.isArray(content)) {
556
- return content.map((c) => "text" in c && c.text ? c.text : "").join("");
557
- }
558
- return String(content ?? "");
559
- }
560
- function exitWithError(err, code = 1) {
561
- console.error("Error:", err instanceof Error ? err.message : String(err));
562
- process.exit(code);
563
- }
564
-
565
- export {
566
- registerChatModelProvider,
567
- getChatModelFactory,
568
- createChatOpenAI,
569
- applyToolChoiceAuto,
570
- createChatModelFromLlmConfig,
571
- AGENT_LLM_PROVIDER_FIELD,
572
- resolveLlmSectionWithNpm,
573
- loadLlmConfig,
574
- createAgentLlM,
575
- messageContentToString,
576
- exitWithError
577
- };
578
- //# sourceMappingURL=chunk-YK4EBPJ3.js.map