@theokit/sdk 4.15.0 → 4.15.2

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/dist/cron.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import { z, toJSONSchema } from 'zod';
2
2
  import { createRequire } from 'module';
3
3
  import { randomUUID, createHash, randomBytes } from 'crypto';
4
- import { stat, readFile, rename, mkdir, readdir, open, unlink, statfs, access } from 'fs/promises';
4
+ import { readdir, stat, readFile, rename, mkdir, open, unlink, statfs, access } from 'fs/promises';
5
5
  import { join, dirname, resolve, sep, relative, isAbsolute } from 'path';
6
- import { existsSync, realpathSync, mkdirSync, lstatSync, readlinkSync, readFileSync, readdirSync, statSync, chmodSync, openSync, writeFileSync, fsyncSync, closeSync, renameSync, unlinkSync } from 'fs';
6
+ import { existsSync, readFileSync, realpathSync, mkdirSync, lstatSync, readlinkSync, statSync, chmodSync, openSync, writeFileSync, fsyncSync, closeSync, renameSync, unlinkSync, readdirSync } from 'fs';
7
7
  import { AsyncLocalStorage } from 'async_hooks';
8
8
  import { homedir } from 'os';
9
9
  import { spawn } from 'child_process';
10
- import { fileURLToPath } from 'url';
10
+ import { fileURLToPath, pathToFileURL } from 'url';
11
11
  import { Cron as Cron$1 } from 'croner';
12
12
 
13
13
  var __defProp = Object.defineProperty;
@@ -8937,14 +8937,14 @@ var PluginsManager = class {
8937
8937
  // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: MD-first + JSON fallback + both-present detection per plugin folder — branch table is clearer inlined.
8938
8938
  async refresh() {
8939
8939
  this.plugins = [];
8940
- const pluginsRoot = join(this.cwd, ".theokit", "plugins");
8941
- const entries = await readWorkspaceDir(pluginsRoot, "plugins_read_error", "plugins directory");
8940
+ const pluginsRoot2 = join(this.cwd, ".theokit", "plugins");
8941
+ const entries = await readWorkspaceDir(pluginsRoot2, "plugins_read_error", "plugins directory");
8942
8942
  for (const entry of entries) {
8943
8943
  if (!entry.isDirectory()) continue;
8944
8944
  const folderName = entry.name;
8945
- const mdPath = join(pluginsRoot, folderName, "PLUGIN.md");
8946
- const jsonPath = join(pluginsRoot, folderName, "plugin.json");
8947
- const metadata = existsSync(mdPath) ? await loadPluginManifestFromMarkdown(pluginsRoot, folderName) : await loadPluginManifestFromJson(jsonPath, folderName);
8945
+ const mdPath = join(pluginsRoot2, folderName, "PLUGIN.md");
8946
+ const jsonPath = join(pluginsRoot2, folderName, "plugin.json");
8947
+ const metadata = existsSync(mdPath) ? await loadPluginManifestFromMarkdown(pluginsRoot2, folderName) : await loadPluginManifestFromJson(jsonPath, folderName);
8948
8948
  if (existsSync(mdPath) && existsSync(jsonPath)) {
8949
8949
  warnOnce(
8950
8950
  `plugin-${folderName}-both`,
@@ -9023,8 +9023,8 @@ async function loadPluginManifestFromJson(manifestPath, folderName) {
9023
9023
  if (typeof record.entry === "string") metadata.entry = record.entry;
9024
9024
  return metadata;
9025
9025
  }
9026
- async function loadPluginManifestFromMarkdown(pluginsRoot, folderName) {
9027
- const mdPath = join(pluginsRoot, folderName, "PLUGIN.md");
9026
+ async function loadPluginManifestFromMarkdown(pluginsRoot2, folderName) {
9027
+ const mdPath = join(pluginsRoot2, folderName, "PLUGIN.md");
9028
9028
  let raw;
9029
9029
  try {
9030
9030
  raw = await readFile(mdPath, "utf8");
@@ -9034,7 +9034,7 @@ async function loadPluginManifestFromMarkdown(pluginsRoot, folderName) {
9034
9034
  cause
9035
9035
  });
9036
9036
  }
9037
- const fakeDir = join(pluginsRoot, folderName);
9037
+ const fakeDir = join(pluginsRoot2, folderName);
9038
9038
  const entities = await loadMarkdownEntities({
9039
9039
  dir: fakeDir,
9040
9040
  schema: PluginFrontmatterSchema,
@@ -12033,6 +12033,103 @@ function registerBuiltins() {
12033
12033
  registerProvider(CEREBRAS);
12034
12034
  registerCatalogProviders();
12035
12035
  }
12036
+ function globalSingleton3(key, create) {
12037
+ const g = globalThis;
12038
+ const sym = Symbol.for(key);
12039
+ if (g[sym] === void 0) g[sym] = create();
12040
+ return g[sym];
12041
+ }
12042
+ var discoveryState = globalSingleton3("theokit-sdk.providers.discovered", () => ({
12043
+ done: false
12044
+ }));
12045
+ function pluginsRoot() {
12046
+ return join(homedir(), ".theokit", "plugins", "model-providers");
12047
+ }
12048
+ function trustFilePath() {
12049
+ return join(homedir(), ".theokit", "plugins", "trusted-providers.json");
12050
+ }
12051
+ function loadTrustedNames() {
12052
+ const trusted = /* @__PURE__ */ new Set();
12053
+ const path = trustFilePath();
12054
+ if (existsSync(path)) {
12055
+ try {
12056
+ const parsed = JSON.parse(readFileSync(path, "utf-8"));
12057
+ if (Array.isArray(parsed)) {
12058
+ let discarded = 0;
12059
+ for (const name of parsed) {
12060
+ if (typeof name === "string" && name.length > 0) trusted.add(name);
12061
+ else discarded += 1;
12062
+ }
12063
+ if (discarded > 0) {
12064
+ process.stderr.write(
12065
+ `[theokit-sdk] WARN: ${path} contains ${discarded} non-string entr${discarded === 1 ? "y" : "ies"} \u2014 discarded (entries must be plugin-name strings)
12066
+ `
12067
+ );
12068
+ }
12069
+ } else {
12070
+ process.stderr.write(
12071
+ `[theokit-sdk] WARN: ${path} is not a JSON array \u2014 trusting NO provider plugins (fail-closed)
12072
+ `
12073
+ );
12074
+ }
12075
+ } catch {
12076
+ process.stderr.write(
12077
+ `[theokit-sdk] WARN: ${path} is not valid JSON \u2014 trusting NO provider plugins (fail-closed)
12078
+ `
12079
+ );
12080
+ }
12081
+ }
12082
+ for (const name of (process.env.THEOKIT_TRUSTED_PROVIDERS ?? "").split(",")) {
12083
+ const trimmed = name.trim();
12084
+ if (trimmed.length > 0) trusted.add(trimmed);
12085
+ }
12086
+ return trusted;
12087
+ }
12088
+ async function discoverProviderPlugins() {
12089
+ if (discoveryState.done) return;
12090
+ discoveryState.done = true;
12091
+ const root = pluginsRoot();
12092
+ if (!existsSync(root)) return;
12093
+ let entries;
12094
+ try {
12095
+ entries = await readdir(root);
12096
+ } catch {
12097
+ return;
12098
+ }
12099
+ const trusted = loadTrustedNames();
12100
+ for (const entry of entries) {
12101
+ if (!trusted.has(entry)) {
12102
+ process.stderr.write(
12103
+ `[theokit-sdk] WARN: provider plugin "${entry}" is present but NOT trusted \u2014 skipped. To trust it, add "${entry}" to ${trustFilePath()} (JSON array of names) or THEOKIT_TRUSTED_PROVIDERS (comma-separated), then restart the process.
12104
+ `
12105
+ );
12106
+ continue;
12107
+ }
12108
+ await loadOne(join(root, entry), entry);
12109
+ }
12110
+ }
12111
+ async function loadOne(dir, entryName) {
12112
+ for (const ext of ["index.mjs", "index.js"]) {
12113
+ const indexPath = join(dir, ext);
12114
+ if (!existsSync(indexPath)) continue;
12115
+ try {
12116
+ const mod = await import(pathToFileURL(indexPath).href);
12117
+ const plugin = mod.default ?? mod[entryName] ?? mod;
12118
+ if (plugin !== null && typeof plugin === "object" && plugin.kind === "model-provider") {
12119
+ const profile = plugin.profile;
12120
+ if (profile !== void 0 && typeof profile === "object") {
12121
+ registerProvider(profile);
12122
+ }
12123
+ }
12124
+ return;
12125
+ } catch (err) {
12126
+ const msg = err instanceof Error ? err.message : String(err);
12127
+ process.stderr.write(`[theokit-sdk] failed to load provider plugin "${entryName}": ${msg}
12128
+ `);
12129
+ return;
12130
+ }
12131
+ }
12132
+ }
12036
12133
 
12037
12134
  // src/internal/llm/anthropic.ts
12038
12135
  init_errors();
@@ -18581,6 +18678,7 @@ function resolveAgentPersistenceCwd(options) {
18581
18678
  return process.cwd();
18582
18679
  }
18583
18680
  async function rehydrateExistingAgent(agentId, existing, options) {
18681
+ await discoverProviderPlugins();
18584
18682
  await validateRehydratedAgent(agentId, existing);
18585
18683
  const mergedLocal = options.local !== void 0 && existing.options.local !== void 0 ? { ...existing.options.local, ...options.local } : options.local ?? existing.options.local;
18586
18684
  const mergedOptions = {
@@ -18710,6 +18808,7 @@ var Agent = class _Agent {
18710
18808
  pluginCount: enabledPluginNames(options.plugins).length
18711
18809
  });
18712
18810
  try {
18811
+ await discoverProviderPlugins();
18713
18812
  const agent = await runCreateUnderSpan(options, span);
18714
18813
  return agent;
18715
18814
  } catch (err) {