@theokit/sdk 2.24.0 → 2.25.0

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.cjs CHANGED
@@ -3462,7 +3462,8 @@ function stripSecretsFromOptions(options) {
3462
3462
  local: serializeLocal(options.local),
3463
3463
  cloud: serializeCloud(options.cloud),
3464
3464
  memory: serializeMemory(options.memory),
3465
- skills: serializeEnabledList(options.skills),
3465
+ // SE22 — a SkillsResolver function isn't serializable; persist only the static form.
3466
+ skills: serializeEnabledList(typeof options.skills === "function" ? void 0 : options.skills),
3466
3467
  // Code-`Plugin` objects are closures and cannot be persisted (like custom
3467
3468
  // tools); only the named-enable settings form is serialized.
3468
3469
  plugins: serializeEnabledList(asPluginsSettings(options.plugins)),
@@ -3770,6 +3771,7 @@ function serializeCloud2(cloud) {
3770
3771
  return result;
3771
3772
  }
3772
3773
  function serializeSkills(skills) {
3774
+ if (typeof skills === "function") return void 0;
3773
3775
  if (skills?.enabled === void 0 || skills.enabled.length === 0) return void 0;
3774
3776
  return { enabled: [...skills.enabled] };
3775
3777
  }
@@ -5241,6 +5243,7 @@ init_errors();
5241
5243
  function validateCloudToolParity(options) {
5242
5244
  if (options.cloud === void 0) return;
5243
5245
  rejectFunctionSystemPrompt(options);
5246
+ rejectFunctionSkills(options);
5244
5247
  rejectStdioMcpLocalPaths(options);
5245
5248
  }
5246
5249
  function rejectFunctionSystemPrompt(options) {
@@ -5251,6 +5254,14 @@ function rejectFunctionSystemPrompt(options) {
5251
5254
  );
5252
5255
  }
5253
5256
  }
5257
+ function rejectFunctionSkills(options) {
5258
+ if (typeof options.skills === "function") {
5259
+ throw new ConfigurationError(
5260
+ "Cloud agents require skills as a static settings object. SkillsResolver functions can't run on PaaS \u2014 resolve to a SkillsSettings object before Agent.create() or move the dynamic logic into a hook rule.",
5261
+ { code: "cloud_incompatible_function_resolver" }
5262
+ );
5263
+ }
5264
+ }
5254
5265
  function rejectStdioMcpLocalPaths(options) {
5255
5266
  if (options.mcpServers === void 0) return;
5256
5267
  for (const [name, config] of Object.entries(options.mcpServers)) {
@@ -7107,9 +7118,223 @@ function parseFrontmatterFields(frontmatter) {
7107
7118
  return out;
7108
7119
  }
7109
7120
 
7121
+ // src/internal/runtime/skills/discover-skills.ts
7122
+ init_errors();
7123
+
7124
+ // src/internal/runtime/skills/skill-frontmatter.ts
7125
+ init_errors();
7126
+ init_yaml_frontmatter();
7127
+ function asString(v) {
7128
+ return typeof v === "string" ? v : void 0;
7129
+ }
7130
+ function toStringFields(raw) {
7131
+ const out = {};
7132
+ for (const [k, v] of Object.entries(raw)) out[k] = asString(v);
7133
+ return out;
7134
+ }
7135
+ function parseSkillFrontmatter(raw, fallbackName) {
7136
+ const fields = extractAndParseFrontmatter(raw, fallbackName);
7137
+ const name = resolveName(fields, fallbackName);
7138
+ ensureRequiredFields(fields, name);
7139
+ return buildFrontmatter(fields, name);
7140
+ }
7141
+ function stripSkillFrontmatter(raw) {
7142
+ const match = /^---\s*\n[\s\S]*?\n---\s*\n/.exec(raw);
7143
+ return (match === null ? raw : raw.slice(match[0].length)).trim();
7144
+ }
7145
+ function extractAndParseFrontmatter(raw, fallbackName) {
7146
+ const match = /^---\s*\n([\s\S]*?)\n---\s*\n/.exec(raw);
7147
+ if (match === null) {
7148
+ throw new ConfigurationError(`Skill ${fallbackName} is missing frontmatter`, {
7149
+ code: "missing_frontmatter"
7150
+ });
7151
+ }
7152
+ const frontmatter = match[1] ?? "";
7153
+ try {
7154
+ return toStringFields(parseSimpleYaml(frontmatter));
7155
+ } catch (cause) {
7156
+ const detail = cause instanceof Error ? cause.message : String(cause);
7157
+ throw new ConfigurationError(
7158
+ `Skill ${fallbackName} has malformed YAML frontmatter: ${detail}`,
7159
+ { code: "schema_invalid", cause }
7160
+ );
7161
+ }
7162
+ }
7163
+ function resolveName(fields, fallbackName) {
7164
+ if (hasContent(fields.name)) return fields.name;
7165
+ if (hasContent(fallbackName)) return fallbackName;
7166
+ throw new ConfigurationError("Skill at unknown path is missing required field: name", {
7167
+ code: "schema_invalid"
7168
+ });
7169
+ }
7170
+ function ensureRequiredFields(fields, name) {
7171
+ if (!hasContent(fields.description)) {
7172
+ throw new ConfigurationError(`Skill ${name} is missing required field: description`, {
7173
+ code: "schema_invalid"
7174
+ });
7175
+ }
7176
+ }
7177
+ function buildFrontmatter(fields, name) {
7178
+ const description = fields.description;
7179
+ if (description === void 0) {
7180
+ throw new ConfigurationError(`Skill ${name} missing description`, { code: "schema_invalid" });
7181
+ }
7182
+ const result = { name, description };
7183
+ if (hasContent(fields.category)) result.category = fields.category;
7184
+ const deps = parseDependencies(fields.dependencies);
7185
+ if (deps !== void 0) result.dependencies = deps;
7186
+ return result;
7187
+ }
7188
+ function parseDependencies(raw) {
7189
+ if (!hasContent(raw)) return void 0;
7190
+ const deps = raw.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
7191
+ return deps.length > 0 ? deps : void 0;
7192
+ }
7193
+ function hasContent(value) {
7194
+ return value !== void 0 && value.trim().length > 0;
7195
+ }
7196
+
7197
+ // src/internal/runtime/skills/discover-skills.ts
7198
+ async function discoverSkills(dir, options) {
7199
+ let entries;
7200
+ try {
7201
+ entries = await readWorkspaceDir(dir, "skills_read_error", "skills directory");
7202
+ } catch {
7203
+ return [];
7204
+ }
7205
+ const skills = [];
7206
+ for (const entry of entries) {
7207
+ if (!entry.isDirectory()) continue;
7208
+ let skillDir;
7209
+ try {
7210
+ skillDir = safePathJoin(dir, entry.name);
7211
+ assertNoSymlinkEscape(skillDir, dir);
7212
+ } catch {
7213
+ continue;
7214
+ }
7215
+ const skillPath = path.join(skillDir, "SKILL.md");
7216
+ let raw;
7217
+ try {
7218
+ raw = await promises.readFile(skillPath, "utf8");
7219
+ } catch {
7220
+ continue;
7221
+ }
7222
+ const skill = tryParseSkill(raw, entry.name, skillPath, options);
7223
+ if (skill !== void 0) skills.push(skill);
7224
+ }
7225
+ return skills;
7226
+ }
7227
+ function tryParseSkill(raw, fallbackName, source, options) {
7228
+ try {
7229
+ const frontmatter = parseSkillFrontmatter(raw, fallbackName);
7230
+ const skill = {
7231
+ name: frontmatter.name,
7232
+ description: frontmatter.description,
7233
+ source
7234
+ };
7235
+ if (frontmatter.category !== void 0) skill.category = frontmatter.category;
7236
+ if (frontmatter.dependencies !== void 0) skill.dependencies = frontmatter.dependencies;
7237
+ return skill;
7238
+ } catch (cause) {
7239
+ if (cause instanceof ConfigurationError) {
7240
+ options?.onInvalidSkill?.({
7241
+ name: fallbackName,
7242
+ source,
7243
+ code: cause.code ?? "unknown",
7244
+ message: cause.message
7245
+ });
7246
+ return void 0;
7247
+ }
7248
+ throw cause;
7249
+ }
7250
+ }
7251
+
7252
+ // src/internal/runtime/skills/skills-manager.ts
7253
+ var SkillsManager = class {
7254
+ constructor(cwd, _enabled, settingSourcesIncludeProject, skillsDir, inline) {
7255
+ this.cwd = cwd;
7256
+ this.settingSourcesIncludeProject = settingSourcesIncludeProject;
7257
+ this.skillsDir = skillsDir;
7258
+ this.inline = inline;
7259
+ }
7260
+ cwd;
7261
+ settingSourcesIncludeProject;
7262
+ skillsDir;
7263
+ inline;
7264
+ skills = [];
7265
+ async initialize() {
7266
+ if (!this.settingSourcesIncludeProject) {
7267
+ this.skills = this.mergeInline([]);
7268
+ return;
7269
+ }
7270
+ await this.refresh();
7271
+ }
7272
+ async refresh() {
7273
+ const skillsRoot = this.skillsDir ?? path.join(this.cwd, ".theokit", "skills");
7274
+ const discovered = await discoverSkills(skillsRoot, {
7275
+ onInvalidSkill: (info) => {
7276
+ process.stderr.write(
7277
+ `[theokit-sdk] skill ${info.name} skipped (${info.code}): ${info.message}
7278
+ `
7279
+ );
7280
+ }
7281
+ });
7282
+ this.skills = this.mergeInline(discovered);
7283
+ }
7284
+ /** M22 — merge inline skills over discovered ones; inline wins on a name conflict. */
7285
+ mergeInline(discovered) {
7286
+ if (this.inline === void 0 || this.inline.length === 0) return discovered;
7287
+ const inlineNames = new Set(this.inline.map((s) => s.name));
7288
+ return [...discovered.filter((s) => !inlineNames.has(s.name)), ...this.inline];
7289
+ }
7290
+ list() {
7291
+ return Promise.resolve(this.skills);
7292
+ }
7293
+ /**
7294
+ * SE20 — resolve a skill by name INCLUDING its body. Inline (`createSkill`)
7295
+ * skills carry `instructions` on the object; filesystem skills read the body
7296
+ * from their `source` SKILL.md (frontmatter stripped). `undefined` when no
7297
+ * enabled skill matches (malformed skills were already excluded at discovery).
7298
+ */
7299
+ async get(name) {
7300
+ const skill = this.skills.find((s) => s.name === name);
7301
+ if (skill === void 0) return void 0;
7302
+ const instructions = typeof skill.instructions === "string" ? skill.instructions : stripSkillFrontmatter(await promises.readFile(skill.source, "utf8"));
7303
+ const references = skill.references;
7304
+ return {
7305
+ name: skill.name,
7306
+ description: skill.description,
7307
+ instructions,
7308
+ ...references !== void 0 ? { references } : {}
7309
+ };
7310
+ }
7311
+ };
7312
+
7110
7313
  // src/internal/runtime/system-prompt/local-assembly.ts
7111
- async function buildSystemPromptContext(inputs, userText, memoryFacts) {
7112
- const skills = inputs.skillsManager !== void 0 ? await inputs.skillsManager.list() : [];
7314
+ async function resolveSendSkills(inputs, userText, memoryFacts) {
7315
+ const skills = inputs.options.skills;
7316
+ if (typeof skills !== "function") {
7317
+ return { manager: inputs.skillsManager, autoInject: skills?.autoInject ?? true };
7318
+ }
7319
+ const settings = await skills({
7320
+ agentId: inputs.agentId,
7321
+ cwd: inputs.workspaceCwd,
7322
+ model: inputs.model,
7323
+ userMessage: userText,
7324
+ memory: memoryFacts.map((fact) => ({ text: fact.text }))
7325
+ });
7326
+ const manager = new SkillsManager(
7327
+ inputs.workspaceCwd,
7328
+ settings.enabled,
7329
+ inputs.settingSourcesIncludeProject,
7330
+ settings.skillsDir,
7331
+ settings.inline
7332
+ );
7333
+ await manager.initialize();
7334
+ return { manager, autoInject: settings.autoInject ?? true };
7335
+ }
7336
+ async function buildSystemPromptContext(inputs, userText, memoryFacts, manager = inputs.skillsManager) {
7337
+ const skills = manager !== void 0 ? await manager.list() : [];
7113
7338
  return {
7114
7339
  agentId: inputs.agentId,
7115
7340
  cwd: inputs.workspaceCwd,
@@ -7120,10 +7345,11 @@ async function buildSystemPromptContext(inputs, userText, memoryFacts) {
7120
7345
  };
7121
7346
  }
7122
7347
  async function buildAssemblyContext(inputs, userText, baseSystemPrompt, memoryFacts, activeMemorySummary) {
7123
- const baseCtx = await buildSystemPromptContext(inputs, userText, memoryFacts);
7348
+ const resolved = await resolveSendSkills(inputs, userText, memoryFacts);
7349
+ const baseCtx = await buildSystemPromptContext(inputs, userText, memoryFacts, resolved.manager);
7124
7350
  const assemblyCtx = {
7125
7351
  ...baseCtx,
7126
- skillsAutoInject: inputs.options.skills?.autoInject ?? true,
7352
+ skillsAutoInject: resolved.autoInject,
7127
7353
  memoryAutoInject: inputs.options.memory?.autoInject ?? true
7128
7354
  };
7129
7355
  if (baseSystemPrompt !== void 0) assemblyCtx.baseSystemPrompt = baseSystemPrompt;
@@ -8375,176 +8601,6 @@ async function loadPluginManifestFromMarkdown(pluginsRoot, folderName) {
8375
8601
  return metadata;
8376
8602
  }
8377
8603
 
8378
- // src/internal/runtime/skills/discover-skills.ts
8379
- init_errors();
8380
-
8381
- // src/internal/runtime/skills/skill-frontmatter.ts
8382
- init_errors();
8383
- init_yaml_frontmatter();
8384
- function asString(v) {
8385
- return typeof v === "string" ? v : void 0;
8386
- }
8387
- function toStringFields(raw) {
8388
- const out = {};
8389
- for (const [k, v] of Object.entries(raw)) out[k] = asString(v);
8390
- return out;
8391
- }
8392
- function parseSkillFrontmatter(raw, fallbackName) {
8393
- const fields = extractAndParseFrontmatter(raw, fallbackName);
8394
- const name = resolveName(fields, fallbackName);
8395
- ensureRequiredFields(fields, name);
8396
- return buildFrontmatter(fields, name);
8397
- }
8398
- function extractAndParseFrontmatter(raw, fallbackName) {
8399
- const match = /^---\s*\n([\s\S]*?)\n---\s*\n/.exec(raw);
8400
- if (match === null) {
8401
- throw new ConfigurationError(`Skill ${fallbackName} is missing frontmatter`, {
8402
- code: "missing_frontmatter"
8403
- });
8404
- }
8405
- const frontmatter = match[1] ?? "";
8406
- try {
8407
- return toStringFields(parseSimpleYaml(frontmatter));
8408
- } catch (cause) {
8409
- const detail = cause instanceof Error ? cause.message : String(cause);
8410
- throw new ConfigurationError(
8411
- `Skill ${fallbackName} has malformed YAML frontmatter: ${detail}`,
8412
- { code: "schema_invalid", cause }
8413
- );
8414
- }
8415
- }
8416
- function resolveName(fields, fallbackName) {
8417
- if (hasContent(fields.name)) return fields.name;
8418
- if (hasContent(fallbackName)) return fallbackName;
8419
- throw new ConfigurationError("Skill at unknown path is missing required field: name", {
8420
- code: "schema_invalid"
8421
- });
8422
- }
8423
- function ensureRequiredFields(fields, name) {
8424
- if (!hasContent(fields.description)) {
8425
- throw new ConfigurationError(`Skill ${name} is missing required field: description`, {
8426
- code: "schema_invalid"
8427
- });
8428
- }
8429
- }
8430
- function buildFrontmatter(fields, name) {
8431
- const description = fields.description;
8432
- if (description === void 0) {
8433
- throw new ConfigurationError(`Skill ${name} missing description`, { code: "schema_invalid" });
8434
- }
8435
- const result = { name, description };
8436
- if (hasContent(fields.category)) result.category = fields.category;
8437
- const deps = parseDependencies(fields.dependencies);
8438
- if (deps !== void 0) result.dependencies = deps;
8439
- return result;
8440
- }
8441
- function parseDependencies(raw) {
8442
- if (!hasContent(raw)) return void 0;
8443
- const deps = raw.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
8444
- return deps.length > 0 ? deps : void 0;
8445
- }
8446
- function hasContent(value) {
8447
- return value !== void 0 && value.trim().length > 0;
8448
- }
8449
-
8450
- // src/internal/runtime/skills/discover-skills.ts
8451
- async function discoverSkills(dir, options) {
8452
- let entries;
8453
- try {
8454
- entries = await readWorkspaceDir(dir, "skills_read_error", "skills directory");
8455
- } catch {
8456
- return [];
8457
- }
8458
- const skills = [];
8459
- for (const entry of entries) {
8460
- if (!entry.isDirectory()) continue;
8461
- let skillDir;
8462
- try {
8463
- skillDir = safePathJoin(dir, entry.name);
8464
- assertNoSymlinkEscape(skillDir, dir);
8465
- } catch {
8466
- continue;
8467
- }
8468
- const skillPath = path.join(skillDir, "SKILL.md");
8469
- let raw;
8470
- try {
8471
- raw = await promises.readFile(skillPath, "utf8");
8472
- } catch {
8473
- continue;
8474
- }
8475
- const skill = tryParseSkill(raw, entry.name, skillPath, options);
8476
- if (skill !== void 0) skills.push(skill);
8477
- }
8478
- return skills;
8479
- }
8480
- function tryParseSkill(raw, fallbackName, source, options) {
8481
- try {
8482
- const frontmatter = parseSkillFrontmatter(raw, fallbackName);
8483
- const skill = {
8484
- name: frontmatter.name,
8485
- description: frontmatter.description,
8486
- source
8487
- };
8488
- if (frontmatter.category !== void 0) skill.category = frontmatter.category;
8489
- if (frontmatter.dependencies !== void 0) skill.dependencies = frontmatter.dependencies;
8490
- return skill;
8491
- } catch (cause) {
8492
- if (cause instanceof ConfigurationError) {
8493
- options?.onInvalidSkill?.({
8494
- name: fallbackName,
8495
- source,
8496
- code: cause.code ?? "unknown",
8497
- message: cause.message
8498
- });
8499
- return void 0;
8500
- }
8501
- throw cause;
8502
- }
8503
- }
8504
-
8505
- // src/internal/runtime/skills/skills-manager.ts
8506
- var SkillsManager = class {
8507
- constructor(cwd, _enabled, settingSourcesIncludeProject, skillsDir, inline) {
8508
- this.cwd = cwd;
8509
- this.settingSourcesIncludeProject = settingSourcesIncludeProject;
8510
- this.skillsDir = skillsDir;
8511
- this.inline = inline;
8512
- }
8513
- cwd;
8514
- settingSourcesIncludeProject;
8515
- skillsDir;
8516
- inline;
8517
- skills = [];
8518
- async initialize() {
8519
- if (!this.settingSourcesIncludeProject) {
8520
- this.skills = this.mergeInline([]);
8521
- return;
8522
- }
8523
- await this.refresh();
8524
- }
8525
- async refresh() {
8526
- const skillsRoot = this.skillsDir ?? path.join(this.cwd, ".theokit", "skills");
8527
- const discovered = await discoverSkills(skillsRoot, {
8528
- onInvalidSkill: (info) => {
8529
- process.stderr.write(
8530
- `[theokit-sdk] skill ${info.name} skipped (${info.code}): ${info.message}
8531
- `
8532
- );
8533
- }
8534
- });
8535
- this.skills = this.mergeInline(discovered);
8536
- }
8537
- /** M22 — merge inline skills over discovered ones; inline wins on a name conflict. */
8538
- mergeInline(discovered) {
8539
- if (this.inline === void 0 || this.inline.length === 0) return discovered;
8540
- const inlineNames = new Set(this.inline.map((s) => s.name));
8541
- return [...discovered.filter((s) => !inlineNames.has(s.name)), ...this.inline];
8542
- }
8543
- list() {
8544
- return Promise.resolve(this.skills);
8545
- }
8546
- };
8547
-
8548
8604
  // src/internal/runtime/local-agent/local-agent-bootstrap.ts
8549
8605
  function registerLocalAgent(args) {
8550
8606
  registerAgent({
@@ -8580,16 +8636,23 @@ function bootstrapSubmanagers(args) {
8580
8636
  );
8581
8637
  }
8582
8638
  if (args.options.skills !== void 0 || args.settingSourcesIncludeProject) {
8639
+ const staticSkills = typeof args.options.skills === "function" ? void 0 : args.options.skills;
8583
8640
  out.skillsManager = new SkillsManager(
8584
8641
  args.workspaceCwd,
8585
- args.options.skills?.enabled,
8642
+ staticSkills?.enabled,
8586
8643
  args.settingSourcesIncludeProject,
8587
8644
  // M22 — custom skills directory + inline (code-defined) skills.
8588
- args.options.skills?.skillsDir,
8589
- args.options.skills?.inline
8645
+ staticSkills?.skillsDir,
8646
+ staticSkills?.inline
8590
8647
  );
8591
8648
  const localSkills = out.skillsManager;
8592
- out.skills = { list: () => localSkills.list() };
8649
+ out.skills = {
8650
+ // Project to the public shape (name + description only). Inline skills carry
8651
+ // their body + references on the object; `list()` must never leak them —
8652
+ // the body is reachable exclusively through `get()`.
8653
+ list: async () => (await localSkills.list()).map((s) => ({ name: s.name, description: s.description })),
8654
+ get: (name) => localSkills.get(name)
8655
+ };
8593
8656
  }
8594
8657
  if (args.options.plugins !== void 0 || args.settingSourcesIncludePlugins) {
8595
8658
  out.pluginsManager = new PluginsManager(
@@ -16435,7 +16498,7 @@ var LocalAgent = class {
16435
16498
  }
16436
16499
  // biome-ignore format: G8 budget — thin accessor for the assembly inputs.
16437
16500
  assemblyInputs() {
16438
- return { agentId: this.agentId, workspaceCwd: this.workspaceCwd, model: this.model, options: this.options, context: this.context, skillsManager: this.skillsManager, systemPromptPipeline: this.systemPromptPipeline };
16501
+ return { agentId: this.agentId, workspaceCwd: this.workspaceCwd, model: this.model, options: this.options, context: this.context, skillsManager: this.skillsManager, settingSourcesIncludeProject: this.settingSourcesIncludeProject, systemPromptPipeline: this.systemPromptPipeline };
16439
16502
  }
16440
16503
  async resolveSystemPrompt(userText, options, memoryFacts) {
16441
16504
  const base = await resolveSystemPromptForSend(