@niscorp/cortex 0.1.1 → 0.1.4

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/index.js CHANGED
@@ -207,8 +207,8 @@ var envelopeLooseWireSchema = (spec) => {
207
207
  return z.object({
208
208
  reasoning: z.string().nullish().describe(REASONING_DESCRIPTION),
209
209
  response,
210
- // A typed union, not z.unknown(): an empty {} schema gives the
211
- // model zero type signal and invites stringified payloads.
210
+ // Not z.unknown(): an empty {} schema tells the model no type, and it
211
+ // sends the payload as a string.
212
212
  data: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]).describe(LOOSE_DATA_DESCRIPTION)
213
213
  });
214
214
  }
@@ -219,12 +219,13 @@ var envelopeLooseWireSchema = (spec) => {
219
219
  };
220
220
  var envelopeAcceptSchema = (_spec) => z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]);
221
221
  var KNOWN_KEYS = /* @__PURE__ */ new Set(["response", "data", "reasoning"]);
222
- var decodeData = decodeJsonish;
223
- var tryUnwrap = (record, spec) => {
222
+ var takesAnyObject = (schema) => schema.safeParse({ "cortex:no-such-field": true }).success;
223
+ var asBarePayload = (record, spec) => {
224
224
  if (!spec.schema || spec.responseMode === "required") return void 0;
225
225
  const keys = Object.keys(record);
226
226
  const looksWrapped = keys.length > 0 && keys.every((key) => KNOWN_KEYS.has(key));
227
227
  if (looksWrapped) return void 0;
228
+ if (takesAnyObject(spec.schema)) return void 0;
228
229
  const parsed = spec.schema.safeParse(record);
229
230
  if (!parsed.success) return void 0;
230
231
  return { data: parsed.data };
@@ -239,8 +240,8 @@ var validateEnvelope = (raw, spec) => {
239
240
  if (!KNOWN_KEYS.has(key)) issues.push(`unknown key "${key}" \u2014 only response, data, reasoning are allowed`);
240
241
  }
241
242
  if (issues.length > 0) {
242
- const unwrapped = tryUnwrap(record, spec);
243
- if (unwrapped !== void 0) return { ok: true, envelope: unwrapped };
243
+ const bare = asBarePayload(record, spec);
244
+ if (bare !== void 0) return { ok: true, envelope: bare };
244
245
  }
245
246
  const response = record["response"] ?? void 0;
246
247
  if (spec.responseMode === "required" && typeof response !== "string") {
@@ -253,7 +254,7 @@ var validateEnvelope = (raw, spec) => {
253
254
  issues.push("reasoning: must be a string");
254
255
  }
255
256
  if (spec.schema) {
256
- const data = decodeData(record["data"]);
257
+ const data = decodeJsonish(record["data"]);
257
258
  let parsed = spec.schema.safeParse(data);
258
259
  if (!parsed.success) {
259
260
  const repaired = spec.schema.safeParse(deepDecodeJsonish(data));
@@ -363,13 +364,9 @@ var stringify = (value) => {
363
364
  }
364
365
  };
365
366
  var buildToolDescriptor = (tool) => ({
366
- // The model sees the tool's NAME — the field designed for it (prompts
367
- // say "call `query`"). The id stays the gate/policy/observation
368
- // identity; the loop resolves calls by either.
369
367
  name: tool.config.name,
370
368
  description: tool.config.description,
371
- // The tool's schema converts itself (Standard JSON Schema) — it may come from
372
- // the caller's zod copy, not ours.
369
+ // Converted by the schema's own zod, not by cortex's: it may come from another copy of zod.
373
370
  parameters: tool.config.input["~standard"].jsonSchema.output({ target: "draft-07" })
374
371
  });
375
372
  var runLoop = async (cfg) => {
@@ -581,7 +578,7 @@ var runLoop = async (cfg) => {
581
578
  return verdict;
582
579
  };
583
580
  const streamEmittedOutput = cfg.resolved.transport !== "respond";
584
- const streamOneStep = async (llm, request) => {
581
+ const oneStep = async (llm, request) => {
585
582
  if (cfg.transport === "step") {
586
583
  try {
587
584
  return ok(await llm.step(request));
@@ -687,7 +684,7 @@ var runLoop = async (cfg) => {
687
684
  ...cfg.resolved.outputToolName !== void 0 && { outputTool: cfg.resolved.outputToolName }
688
685
  }
689
686
  };
690
- const stepOutcome = await streamOneStep(llm, request);
687
+ const stepOutcome = await oneStep(llm, request);
691
688
  if (!stepOutcome.ok) return fail(stepOutcome.error);
692
689
  const result = stepOutcome.data;
693
690
  s.usage = addUsage(s.usage, result.usage);
@@ -889,6 +886,7 @@ var wireRun = (wiring) => {
889
886
  if (options?.onEvent) channel.onEvent(options.onEvent);
890
887
  const approvals = createApprovalBridge();
891
888
  const controller = new AbortController();
889
+ let abortReason;
892
890
  const externalSignal = options?.signal;
893
891
  const onExternalAbort = () => controller.abort();
894
892
  if (externalSignal) {
@@ -988,6 +986,9 @@ var wireRun = (wiring) => {
988
986
  }
989
987
  };
990
988
  }
989
+ if (!outcome.ok && outcome.error.code === "aborted" && abortReason !== void 0) {
990
+ outcome = { ...outcome, error: { ...outcome.error, cause: abortReason } };
991
+ }
991
992
  channel.emit({ type: "run-end", result: outcome, meta: outcome.meta });
992
993
  channel.close();
993
994
  if (externalSignal) externalSignal.removeEventListener("abort", onExternalAbort);
@@ -1013,7 +1014,10 @@ var wireRun = (wiring) => {
1013
1014
  elapsedMs: state.elapsedBase + (Date.now() - state.startedAt),
1014
1015
  ...state.pending && { pending: state.pending }
1015
1016
  }),
1016
- abort: () => controller.abort()
1017
+ abort: (reason) => {
1018
+ if (!controller.signal.aborted) abortReason = reason;
1019
+ controller.abort();
1020
+ }
1017
1021
  };
1018
1022
  };
1019
1023
  var createRun = (definition, input, options) => wireRun({ definition, input, options, runId: newRunId() });
@@ -1090,8 +1094,6 @@ var previewAgent = async (definition, input, options) => {
1090
1094
  ...inputMessages(input)
1091
1095
  ];
1092
1096
  const tools = [
1093
- // The run's own builder — the preview shows the model-visible NAME,
1094
- // not the id, or it would explain a request that never goes out.
1095
1097
  ...allTools.map(buildToolDescriptor),
1096
1098
  ...resolved.respondDescriptor ? [resolved.respondDescriptor] : []
1097
1099
  ];