@theokit/sdk 2.18.1 → 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.
@@ -22,6 +22,7 @@ export interface DefineToolSpec<T extends ZodType> {
22
22
  */
23
23
  handler: (input: ZodNamespace.infer<T>, ctx?: {
24
24
  signal?: AbortSignal;
25
+ context?: unknown;
25
26
  }) => string | Promise<string>;
26
27
  /**
27
28
  * Sanitize the raw model-emitted args BEFORE schema validation (`@theokit/sdk/sanitize`).
@@ -1,4 +1,4 @@
1
- import { p as RunOperation } from './run-pE-34AAo.js';
1
+ import { p as RunOperation } from './run-BgfBWX-z.js';
2
2
 
3
3
  /**
4
4
  * Public type contract for the Budget enforcement primitive
@@ -1,4 +1,4 @@
1
- import { p as RunOperation } from './run-pE-34AAo.cjs';
1
+ import { p as RunOperation } from './run-BgfBWX-z.cjs';
2
2
 
3
3
  /**
4
4
  * Public type contract for the Budget enforcement primitive
package/dist/errors.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- export { A as AgentDisposedError, c as AgentRunError, d as AgentRunErrorCode, e as AuthenticationError, g as BudgetExceededError, C as ConfigurationError, u as CredentialPoolExhaustedError, E as ErrorCode, m as ErrorMetadata, I as IntegrationNotConnectedError, n as InvalidTaskIdError, K as KnownAgentRunErrorCode, M as MemoryAdapterError, o as MemoryAdapterErrorCode, N as NetworkError, R as RateLimitError, p as TaskNotFoundError, T as TheokitAgentError, U as UnknownAgentError, q as UnsupportedBudgetOperationError, r as UnsupportedRunOperationError, s as UnsupportedTaskOperationError, t as isTransientError } from './errors-1tVcX3Fq.cjs';
2
- import './run-pE-34AAo.cjs';
1
+ export { A as AgentDisposedError, c as AgentRunError, d as AgentRunErrorCode, e as AuthenticationError, g as BudgetExceededError, C as ConfigurationError, u as CredentialPoolExhaustedError, E as ErrorCode, m as ErrorMetadata, I as IntegrationNotConnectedError, n as InvalidTaskIdError, K as KnownAgentRunErrorCode, M as MemoryAdapterError, o as MemoryAdapterErrorCode, N as NetworkError, R as RateLimitError, p as TaskNotFoundError, T as TheokitAgentError, U as UnknownAgentError, q as UnsupportedBudgetOperationError, r as UnsupportedRunOperationError, s as UnsupportedTaskOperationError, t as isTransientError } from './errors-CE-lMBi2.cjs';
2
+ import './run-BgfBWX-z.cjs';
package/dist/eval.cjs CHANGED
@@ -2255,6 +2255,37 @@ __export(generate_object_exports, {
2255
2255
  GenerateObjectError: () => GenerateObjectError,
2256
2256
  generateObjectImpl: () => generateObjectImpl
2257
2257
  });
2258
+ function salvagePartial(schema, raw) {
2259
+ if (typeof raw !== "object" || raw === null) return raw;
2260
+ const shape = schema.shape;
2261
+ if (shape === void 0 || typeof shape !== "object") return raw;
2262
+ const rawObj = raw;
2263
+ const out = {};
2264
+ for (const [key, fieldSchema] of Object.entries(shape)) {
2265
+ if (typeof fieldSchema?.safeParse !== "function") continue;
2266
+ const parsed = fieldSchema.safeParse(rawObj[key]);
2267
+ if (parsed.success) out[key] = parsed.data;
2268
+ }
2269
+ return out;
2270
+ }
2271
+ async function runReasoningPhase(options, deps) {
2272
+ const reasoningOptions = {
2273
+ model: options.model,
2274
+ local: options.local,
2275
+ ...options.systemPrompt !== void 0 ? { systemPrompt: options.systemPrompt } : {},
2276
+ ...options.apiKey !== void 0 ? { apiKey: options.apiKey } : {},
2277
+ ...options.providers !== void 0 ? { providers: options.providers } : {}
2278
+ };
2279
+ const reasoningAgent = await deps.create(reasoningOptions);
2280
+ try {
2281
+ const run = await reasoningAgent.send(options.prompt);
2282
+ const result = await run.wait();
2283
+ const text = result.result;
2284
+ return typeof text === "string" ? text : options.prompt;
2285
+ } finally {
2286
+ await disposeAndDeleteTransient(reasoningAgent, deps.delete);
2287
+ }
2288
+ }
2258
2289
  async function generateObjectImpl(options, deps) {
2259
2290
  const { jsonSchema, maxRetries, initialUsage } = setupStructuredOutput(
2260
2291
  options.schema,
@@ -2277,8 +2308,11 @@ async function generateObjectImpl(options, deps) {
2277
2308
  }
2278
2309
  throw new CaptureSentinel(input);
2279
2310
  });
2311
+ const reasoningText = options.structuringModel !== void 0 ? await runReasoningPhase(options, deps) : void 0;
2312
+ const structuringModel = options.structuringModel ?? options.model;
2313
+ const structuringPrompt = reasoningText ?? options.prompt;
2280
2314
  const agentOptions = buildTransientAgentOptions({
2281
- model: options.model,
2315
+ model: structuringModel,
2282
2316
  local: options.local,
2283
2317
  outputTool,
2284
2318
  ...options.systemPrompt !== void 0 ? { systemPrompt: options.systemPrompt } : {},
@@ -2287,7 +2321,7 @@ async function generateObjectImpl(options, deps) {
2287
2321
  });
2288
2322
  const agent = await deps.create(agentOptions);
2289
2323
  try {
2290
- const userMessage = buildToolPrompt(options.prompt);
2324
+ const userMessage = buildToolPrompt(structuringPrompt);
2291
2325
  let lastParseError;
2292
2326
  for (let attempt = 0; attempt <= maxRetries; attempt += 1) {
2293
2327
  capturedRaw = void 0;
@@ -2321,6 +2355,22 @@ async function generateObjectImpl(options, deps) {
2321
2355
  }
2322
2356
  lastParseError = parsed.error;
2323
2357
  }
2358
+ if (options.errorStrategy === "return-raw") {
2359
+ return {
2360
+ object: capturedRaw,
2361
+ raw: capturedRaw,
2362
+ usage: lastUsage,
2363
+ finishReason: "tool_use"
2364
+ };
2365
+ }
2366
+ if (options.errorStrategy === "return-partial") {
2367
+ return {
2368
+ object: salvagePartial(options.schema, capturedRaw),
2369
+ raw: capturedRaw,
2370
+ usage: lastUsage,
2371
+ finishReason: "tool_use"
2372
+ };
2373
+ }
2324
2374
  throw new GenerateObjectError(
2325
2375
  "parse_failed",
2326
2376
  "Schema parse failed after all retries.",
@@ -8307,23 +8357,27 @@ function tryParseSkill(raw, fallbackName, source, options) {
8307
8357
 
8308
8358
  // src/internal/runtime/skills/skills-manager.ts
8309
8359
  var SkillsManager = class {
8310
- constructor(cwd, _enabled, settingSourcesIncludeProject) {
8360
+ constructor(cwd, _enabled, settingSourcesIncludeProject, skillsDir, inline) {
8311
8361
  this.cwd = cwd;
8312
8362
  this.settingSourcesIncludeProject = settingSourcesIncludeProject;
8363
+ this.skillsDir = skillsDir;
8364
+ this.inline = inline;
8313
8365
  }
8314
8366
  cwd;
8315
8367
  settingSourcesIncludeProject;
8368
+ skillsDir;
8369
+ inline;
8316
8370
  skills = [];
8317
8371
  async initialize() {
8318
8372
  if (!this.settingSourcesIncludeProject) {
8319
- this.skills = [];
8373
+ this.skills = this.mergeInline([]);
8320
8374
  return;
8321
8375
  }
8322
8376
  await this.refresh();
8323
8377
  }
8324
8378
  async refresh() {
8325
- const skillsRoot = path.join(this.cwd, ".theokit", "skills");
8326
- this.skills = await discoverSkills(skillsRoot, {
8379
+ const skillsRoot = this.skillsDir ?? path.join(this.cwd, ".theokit", "skills");
8380
+ const discovered = await discoverSkills(skillsRoot, {
8327
8381
  onInvalidSkill: (info) => {
8328
8382
  process.stderr.write(
8329
8383
  `[theokit-sdk] skill ${info.name} skipped (${info.code}): ${info.message}
@@ -8331,6 +8385,13 @@ var SkillsManager = class {
8331
8385
  );
8332
8386
  }
8333
8387
  });
8388
+ this.skills = this.mergeInline(discovered);
8389
+ }
8390
+ /** M22 — merge inline skills over discovered ones; inline wins on a name conflict. */
8391
+ mergeInline(discovered) {
8392
+ if (this.inline === void 0 || this.inline.length === 0) return discovered;
8393
+ const inlineNames = new Set(this.inline.map((s) => s.name));
8394
+ return [...discovered.filter((s) => !inlineNames.has(s.name)), ...this.inline];
8334
8395
  }
8335
8396
  list() {
8336
8397
  return Promise.resolve(this.skills);
@@ -8375,7 +8436,10 @@ function bootstrapSubmanagers(args) {
8375
8436
  out.skillsManager = new SkillsManager(
8376
8437
  args.workspaceCwd,
8377
8438
  args.options.skills?.enabled,
8378
- args.settingSourcesIncludeProject
8439
+ args.settingSourcesIncludeProject,
8440
+ // M22 — custom skills directory + inline (code-defined) skills.
8441
+ args.options.skills?.skillsDir,
8442
+ args.options.skills?.inline
8379
8443
  );
8380
8444
  const localSkills = out.skillsManager;
8381
8445
  out.skills = { list: () => localSkills.list() };
@@ -9215,22 +9279,23 @@ async function executeTool(inputs, resolved, call) {
9215
9279
  return { stdout: "", stderr: `Unknown tool ${call.name}`, exitCode: 127 };
9216
9280
  }
9217
9281
  if (resolved.origin === "shell") return runShellTool(inputs, call);
9218
- if (resolved.origin === "memory") return runMemoryTool(resolved, call);
9219
- if (resolved.origin === "custom") return runCustomTool(resolved, call, inputs.signal);
9282
+ if (resolved.origin === "memory") return runMemoryTool(resolved, call, inputs.context);
9283
+ if (resolved.origin === "custom")
9284
+ return runCustomTool(resolved, call, inputs.signal, inputs.context);
9220
9285
  return runMcpTool(inputs, resolved, call);
9221
9286
  }
9222
- async function runMemoryTool(resolved, call) {
9223
- return runHandlerTool("memory", resolved.memoryHandler, call);
9287
+ async function runMemoryTool(resolved, call, context) {
9288
+ return runHandlerTool("memory", resolved.memoryHandler, call, void 0, context);
9224
9289
  }
9225
- async function runCustomTool(resolved, call, signal) {
9226
- return runHandlerTool("custom", resolved.customHandler, call, signal);
9290
+ async function runCustomTool(resolved, call, signal, context) {
9291
+ return runHandlerTool("custom", resolved.customHandler, call, signal, context);
9227
9292
  }
9228
- async function runHandlerTool(kind, handler, call, signal) {
9293
+ async function runHandlerTool(kind, handler, call, signal, context) {
9229
9294
  if (handler === void 0) {
9230
9295
  return { stdout: "", stderr: `${kind} tool ${call.name} has no handler`, exitCode: 127 };
9231
9296
  }
9232
9297
  try {
9233
- const stdout = await handler(call.input, { signal });
9298
+ const stdout = await handler(call.input, { signal, context });
9234
9299
  return { stdout, stderr: "", exitCode: 0 };
9235
9300
  } catch (cause) {
9236
9301
  const message = cause instanceof Error ? cause.message : String(cause);
@@ -13113,6 +13178,9 @@ function buildLoopInputs(options, runId, userText) {
13113
13178
  // D318 — forward SendOptions.signal to the agent loop so streamLlmTurn
13114
13179
  // can attach it to the LLM `fetch({ signal })` call.
13115
13180
  ...options.sendOptions.signal !== void 0 ? { signal: options.sendOptions.signal } : {},
13181
+ // M7 — forward SendOptions.context to the loop so every tool handler receives
13182
+ // it on `ctx.context` (shared run config set once, e.g. projectRoot).
13183
+ ...options.sendOptions.context !== void 0 ? { context: options.sendOptions.context } : {},
13116
13184
  // #58 / #57 — forward the per-tool timeout + tool-result guard so a consumer
13117
13185
  // can enable them via SendOptions (not only internal AgentLoopInputs).
13118
13186
  ...options.sendOptions.perToolTimeoutMs !== void 0 ? { perToolTimeoutMs: options.sendOptions.perToolTimeoutMs } : {},