@theokit/sdk 2.19.0 → 2.20.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.
@@ -1,5 +1,5 @@
1
- import type { ZodType } from "zod";
2
- import type { LocalOptions, ModelSelection } from "./types/agent.js";
1
+ import type { z as ZodNamespace, ZodType } from "zod";
2
+ import type { AgentOptions, LocalOptions, ModelSelection, SDKAgent } from "./types/agent.js";
3
3
  import type { ProviderRoutingSettings } from "./types/providers.js";
4
4
  /**
5
5
  * Options accepted by {@link Agent.generateObject}. Returns a typed object
@@ -16,6 +16,13 @@ export interface GenerateObjectOptions<T extends ZodType> {
16
16
  systemPrompt?: string;
17
17
  /** Model selection. Required (transient agents need a model). */
18
18
  model: ModelSelection;
19
+ /**
20
+ * M21 — optional separate model for the STRUCTURING step. When set, `model` first produces a
21
+ * free-text reasoned answer to the prompt (phase 1), then `structuringModel` extracts the
22
+ * schema-matched object by calling the `output` tool over that answer (phase 2). Lets a large
23
+ * model reason while a cheap fast model does the extraction. Absent ⇒ today's single-model flow.
24
+ */
25
+ structuringModel?: ModelSelection;
19
26
  /** API key. Falls back to env (THEOKIT_API_KEY etc). */
20
27
  apiKey?: string;
21
28
  /** Local runtime config (cwd, sandbox). Required to keep the transient agent local-only. */
@@ -76,3 +83,10 @@ export declare class GenerateObjectError extends Error {
76
83
  readonly cause?: unknown;
77
84
  constructor(code: "no_tool_call" | "parse_failed", message: string, cause?: unknown);
78
85
  }
86
+ interface GenerateObjectDeps {
87
+ create: (options: AgentOptions) => Promise<SDKAgent>;
88
+ /** Hard-delete the transient agent from the registry after dispose. */
89
+ delete: (agentId: string) => Promise<void>;
90
+ }
91
+ export declare function generateObjectImpl<T extends ZodType>(options: GenerateObjectOptions<T>, deps: GenerateObjectDeps): Promise<GenerateObjectResult<ZodNamespace.infer<T>>>;
92
+ export {};
package/dist/index.cjs CHANGED
@@ -3813,6 +3813,24 @@ function salvagePartial(schema, raw) {
3813
3813
  }
3814
3814
  return out;
3815
3815
  }
3816
+ async function runReasoningPhase(options, deps) {
3817
+ const reasoningOptions = {
3818
+ model: options.model,
3819
+ local: options.local,
3820
+ ...options.systemPrompt !== void 0 ? { systemPrompt: options.systemPrompt } : {},
3821
+ ...options.apiKey !== void 0 ? { apiKey: options.apiKey } : {},
3822
+ ...options.providers !== void 0 ? { providers: options.providers } : {}
3823
+ };
3824
+ const reasoningAgent = await deps.create(reasoningOptions);
3825
+ try {
3826
+ const run = await reasoningAgent.send(options.prompt);
3827
+ const result = await run.wait();
3828
+ const text = result.result;
3829
+ return typeof text === "string" ? text : options.prompt;
3830
+ } finally {
3831
+ await disposeAndDeleteTransient(reasoningAgent, deps.delete);
3832
+ }
3833
+ }
3816
3834
  async function generateObjectImpl(options, deps) {
3817
3835
  const { jsonSchema, maxRetries, initialUsage } = setupStructuredOutput(
3818
3836
  options.schema,
@@ -3835,8 +3853,11 @@ async function generateObjectImpl(options, deps) {
3835
3853
  }
3836
3854
  throw new CaptureSentinel(input);
3837
3855
  });
3856
+ const reasoningText = options.structuringModel !== void 0 ? await runReasoningPhase(options, deps) : void 0;
3857
+ const structuringModel = options.structuringModel ?? options.model;
3858
+ const structuringPrompt = reasoningText ?? options.prompt;
3838
3859
  const agentOptions = buildTransientAgentOptions({
3839
- model: options.model,
3860
+ model: structuringModel,
3840
3861
  local: options.local,
3841
3862
  outputTool,
3842
3863
  ...options.systemPrompt !== void 0 ? { systemPrompt: options.systemPrompt } : {},
@@ -3845,7 +3866,7 @@ async function generateObjectImpl(options, deps) {
3845
3866
  });
3846
3867
  const agent = await deps.create(agentOptions);
3847
3868
  try {
3848
- const userMessage = buildToolPrompt(options.prompt);
3869
+ const userMessage = buildToolPrompt(structuringPrompt);
3849
3870
  let lastParseError;
3850
3871
  for (let attempt = 0; attempt <= maxRetries; attempt += 1) {
3851
3872
  capturedRaw = void 0;
@@ -10803,23 +10824,27 @@ function tryParseSkill(raw, fallbackName, source, options) {
10803
10824
 
10804
10825
  // src/internal/runtime/skills/skills-manager.ts
10805
10826
  var SkillsManager = class {
10806
- constructor(cwd, _enabled, settingSourcesIncludeProject) {
10827
+ constructor(cwd, _enabled, settingSourcesIncludeProject, skillsDir, inline) {
10807
10828
  this.cwd = cwd;
10808
10829
  this.settingSourcesIncludeProject = settingSourcesIncludeProject;
10830
+ this.skillsDir = skillsDir;
10831
+ this.inline = inline;
10809
10832
  }
10810
10833
  cwd;
10811
10834
  settingSourcesIncludeProject;
10835
+ skillsDir;
10836
+ inline;
10812
10837
  skills = [];
10813
10838
  async initialize() {
10814
10839
  if (!this.settingSourcesIncludeProject) {
10815
- this.skills = [];
10840
+ this.skills = this.mergeInline([]);
10816
10841
  return;
10817
10842
  }
10818
10843
  await this.refresh();
10819
10844
  }
10820
10845
  async refresh() {
10821
- const skillsRoot = path.join(this.cwd, ".theokit", "skills");
10822
- this.skills = await discoverSkills(skillsRoot, {
10846
+ const skillsRoot = this.skillsDir ?? path.join(this.cwd, ".theokit", "skills");
10847
+ const discovered = await discoverSkills(skillsRoot, {
10823
10848
  onInvalidSkill: (info) => {
10824
10849
  process.stderr.write(
10825
10850
  `[theokit-sdk] skill ${info.name} skipped (${info.code}): ${info.message}
@@ -10827,6 +10852,13 @@ var SkillsManager = class {
10827
10852
  );
10828
10853
  }
10829
10854
  });
10855
+ this.skills = this.mergeInline(discovered);
10856
+ }
10857
+ /** M22 — merge inline skills over discovered ones; inline wins on a name conflict. */
10858
+ mergeInline(discovered) {
10859
+ if (this.inline === void 0 || this.inline.length === 0) return discovered;
10860
+ const inlineNames = new Set(this.inline.map((s) => s.name));
10861
+ return [...discovered.filter((s) => !inlineNames.has(s.name)), ...this.inline];
10830
10862
  }
10831
10863
  list() {
10832
10864
  return Promise.resolve(this.skills);
@@ -10871,7 +10903,10 @@ function bootstrapSubmanagers(args) {
10871
10903
  out.skillsManager = new SkillsManager(
10872
10904
  args.workspaceCwd,
10873
10905
  args.options.skills?.enabled,
10874
- args.settingSourcesIncludeProject
10906
+ args.settingSourcesIncludeProject,
10907
+ // M22 — custom skills directory + inline (code-defined) skills.
10908
+ args.options.skills?.skillsDir,
10909
+ args.options.skills?.inline
10875
10910
  );
10876
10911
  const localSkills = out.skillsManager;
10877
10912
  out.skills = { list: () => localSkills.list() };
@@ -18773,6 +18808,20 @@ var Budget = class {
18773
18808
  }
18774
18809
  };
18775
18810
 
18811
+ // src/create-skill.ts
18812
+ function createSkill(spec) {
18813
+ if (!spec.name) throw new Error("createSkill: `name` is required.");
18814
+ if (!spec.description) throw new Error("createSkill: `description` is required.");
18815
+ return {
18816
+ name: spec.name,
18817
+ description: spec.description,
18818
+ source: `inline://${spec.name}`,
18819
+ instructions: spec.instructions,
18820
+ ...spec.category !== void 0 ? { category: spec.category } : {},
18821
+ ...spec.dependencies !== void 0 ? { dependencies: spec.dependencies } : {}
18822
+ };
18823
+ }
18824
+
18776
18825
  // src/cron.ts
18777
18826
  init_errors();
18778
18827
 
@@ -20127,6 +20176,53 @@ function createPermissionPlugin(engine, opts = {}) {
20127
20176
  });
20128
20177
  }
20129
20178
 
20179
+ // src/schema-normalizer.ts
20180
+ init_to_json_schema();
20181
+ function isJsonSchemaObject(s) {
20182
+ if (typeof s !== "object" || s === null) return false;
20183
+ const o = s;
20184
+ if ("$schema" in o) return true;
20185
+ return o.type === "object" && typeof o.properties === "object" && o.properties !== null;
20186
+ }
20187
+ function hasToJsonSchemaMethod(s) {
20188
+ return typeof s === "object" && s !== null && typeof s.toJsonSchema === "function";
20189
+ }
20190
+ function isZodSchema(s) {
20191
+ if (typeof s !== "object" || s === null) return false;
20192
+ const o = s;
20193
+ return typeof o.safeParse === "function" && ("_def" in o || "def" in o);
20194
+ }
20195
+ function isValibotSchema(s) {
20196
+ if (typeof s !== "object" || s === null) return false;
20197
+ const o = s;
20198
+ return o.kind === "schema" && "type" in o && typeof o.toJsonSchema !== "function";
20199
+ }
20200
+ async function normalizeSchema(schema) {
20201
+ if (isJsonSchemaObject(schema)) return schema;
20202
+ if (isValibotSchema(schema)) {
20203
+ const specifier = "@valibot/to-json-schema";
20204
+ try {
20205
+ const mod = await import(specifier);
20206
+ return mod.toJsonSchema(schema);
20207
+ } catch (err) {
20208
+ const missing = err instanceof Error && /Cannot find|Cannot resolve|ERR_MODULE_NOT_FOUND/.test(err.message);
20209
+ if (missing) {
20210
+ throw new Error(
20211
+ "normalizeSchema: a Valibot schema requires the optional '@valibot/to-json-schema' package. Install it, or use a Zod schema (the default recommendation)."
20212
+ );
20213
+ }
20214
+ throw err;
20215
+ }
20216
+ }
20217
+ if (isZodSchema(schema)) {
20218
+ return toJsonSchema(schema, { unrepresentable: "any" });
20219
+ }
20220
+ if (hasToJsonSchemaMethod(schema)) return schema.toJsonSchema();
20221
+ throw new Error(
20222
+ "normalizeSchema: unsupported schema. Supported: Zod (default), JSON Schema, ArkType (.toJsonSchema()), Valibot (with @valibot/to-json-schema)."
20223
+ );
20224
+ }
20225
+
20130
20226
  // src/security.ts
20131
20227
  init_security();
20132
20228
  var Security = class {
@@ -20925,6 +21021,7 @@ exports.createAgentFactory = createAgentFactory;
20925
21021
  exports.createCounterBudgetTracker = createCounterBudgetTracker;
20926
21022
  exports.createNoopMemoryProvider = createNoopMemoryProvider;
20927
21023
  exports.createPermissionPlugin = createPermissionPlugin;
21024
+ exports.createSkill = createSkill;
20928
21025
  exports.createSquad = createSquad;
20929
21026
  exports.definePlugin = definePlugin;
20930
21027
  exports.defineProvider = defineProvider;
@@ -20935,6 +21032,7 @@ exports.inferApiMode = inferApiMode;
20935
21032
  exports.isTransientError = isTransientError;
20936
21033
  exports.migrateSqliteToLance = migrateSqliteToLance2;
20937
21034
  exports.mkMemoryId = mkMemoryId;
21035
+ exports.normalizeSchema = normalizeSchema;
20938
21036
  exports.normalizeUsage = normalizeUsage;
20939
21037
  exports.preflightCheck = preflightCheck;
20940
21038
  exports.scopedConversationId = scopedConversationId;