@niscorp/cortex 0.1.3 → 0.1.5

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.d.cts CHANGED
@@ -432,6 +432,8 @@ type SchemaIssue = {
432
432
  path: PropertyKey[];
433
433
  message: string;
434
434
  errors?: SchemaIssue[][];
435
+ issues?: SchemaIssue[];
436
+ origin?: string;
435
437
  };
436
438
  declare const flattenSchemaIssues: (issues: SchemaIssue[], prefix?: PropertyKey[]) => SchemaIssue[];
437
439
 
package/dist/index.d.ts CHANGED
@@ -432,6 +432,8 @@ type SchemaIssue = {
432
432
  path: PropertyKey[];
433
433
  message: string;
434
434
  errors?: SchemaIssue[][];
435
+ issues?: SchemaIssue[];
436
+ origin?: string;
435
437
  };
436
438
  declare const flattenSchemaIssues: (issues: SchemaIssue[], prefix?: PropertyKey[]) => SchemaIssue[];
437
439
 
package/dist/index.js CHANGED
@@ -175,6 +175,9 @@ var pickBranch = (branches) => branches.reduce((best, branch) => {
175
175
  });
176
176
  var flattenSchemaIssues = (issues, prefix = []) => issues.flatMap((issue) => {
177
177
  const path = [...prefix, ...issue.path];
178
+ if (issue.code === "invalid_key" && issue.origin === "record" && issue.issues && issue.issues.length > 0) {
179
+ return flattenSchemaIssues(issue.issues, path);
180
+ }
178
181
  if (issue.code !== "invalid_union" || !issue.errors || issue.errors.length === 0) return [{ ...issue, path }];
179
182
  return flattenSchemaIssues(pickBranch(issue.errors), path);
180
183
  });
@@ -207,8 +210,8 @@ var envelopeLooseWireSchema = (spec) => {
207
210
  return z.object({
208
211
  reasoning: z.string().nullish().describe(REASONING_DESCRIPTION),
209
212
  response,
210
- // A typed union, not z.unknown(): an empty {} schema gives the
211
- // model zero type signal and invites stringified payloads.
213
+ // Not z.unknown(): an empty {} schema tells the model no type, and it
214
+ // sends the payload as a string.
212
215
  data: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]).describe(LOOSE_DATA_DESCRIPTION)
213
216
  });
214
217
  }
@@ -219,12 +222,13 @@ var envelopeLooseWireSchema = (spec) => {
219
222
  };
220
223
  var envelopeAcceptSchema = (_spec) => z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]);
221
224
  var KNOWN_KEYS = /* @__PURE__ */ new Set(["response", "data", "reasoning"]);
222
- var decodeData = decodeJsonish;
223
- var tryUnwrap = (record, spec) => {
225
+ var takesAnyObject = (schema) => schema.safeParse({ "cortex:no-such-field": true }).success;
226
+ var asBarePayload = (record, spec) => {
224
227
  if (!spec.schema || spec.responseMode === "required") return void 0;
225
228
  const keys = Object.keys(record);
226
229
  const looksWrapped = keys.length > 0 && keys.every((key) => KNOWN_KEYS.has(key));
227
230
  if (looksWrapped) return void 0;
231
+ if (takesAnyObject(spec.schema)) return void 0;
228
232
  const parsed = spec.schema.safeParse(record);
229
233
  if (!parsed.success) return void 0;
230
234
  return { data: parsed.data };
@@ -239,8 +243,8 @@ var validateEnvelope = (raw, spec) => {
239
243
  if (!KNOWN_KEYS.has(key)) issues.push(`unknown key "${key}" \u2014 only response, data, reasoning are allowed`);
240
244
  }
241
245
  if (issues.length > 0) {
242
- const unwrapped = tryUnwrap(record, spec);
243
- if (unwrapped !== void 0) return { ok: true, envelope: unwrapped };
246
+ const bare = asBarePayload(record, spec);
247
+ if (bare !== void 0) return { ok: true, envelope: bare };
244
248
  }
245
249
  const response = record["response"] ?? void 0;
246
250
  if (spec.responseMode === "required" && typeof response !== "string") {
@@ -253,7 +257,7 @@ var validateEnvelope = (raw, spec) => {
253
257
  issues.push("reasoning: must be a string");
254
258
  }
255
259
  if (spec.schema) {
256
- const data = decodeData(record["data"]);
260
+ const data = decodeJsonish(record["data"]);
257
261
  let parsed = spec.schema.safeParse(data);
258
262
  if (!parsed.success) {
259
263
  const repaired = spec.schema.safeParse(deepDecodeJsonish(data));
@@ -363,13 +367,9 @@ var stringify = (value) => {
363
367
  }
364
368
  };
365
369
  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
370
  name: tool.config.name,
370
371
  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.
372
+ // Converted by the schema's own zod, not by cortex's: it may come from another copy of zod.
373
373
  parameters: tool.config.input["~standard"].jsonSchema.output({ target: "draft-07" })
374
374
  });
375
375
  var runLoop = async (cfg) => {
@@ -505,7 +505,7 @@ var runLoop = async (cfg) => {
505
505
  if (repaired.success) parsedArgs = repaired;
506
506
  }
507
507
  if (!parsedArgs.success) {
508
- const issues = parsedArgs.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ");
508
+ const issues = flattenSchemaIssues(parsedArgs.error.issues).map((i) => `${i.path.join(".")}: ${i.message}`).join("; ");
509
509
  const hint = typeof args === "string" ? ' (your arguments arrived as ONE STRING \u2014 call again with a plain JSON object, e.g. {"key": "value"})' : isEmptyObject(args) ? ` (your arguments arrived EMPTY \u2014 nothing was sent. Call ${toolId} again with its arguments filled in, as its parameters describe)` : "";
510
510
  record(
511
511
  call,
@@ -581,7 +581,7 @@ var runLoop = async (cfg) => {
581
581
  return verdict;
582
582
  };
583
583
  const streamEmittedOutput = cfg.resolved.transport !== "respond";
584
- const streamOneStep = async (llm, request) => {
584
+ const oneStep = async (llm, request) => {
585
585
  if (cfg.transport === "step") {
586
586
  try {
587
587
  return ok(await llm.step(request));
@@ -687,7 +687,7 @@ var runLoop = async (cfg) => {
687
687
  ...cfg.resolved.outputToolName !== void 0 && { outputTool: cfg.resolved.outputToolName }
688
688
  }
689
689
  };
690
- const stepOutcome = await streamOneStep(llm, request);
690
+ const stepOutcome = await oneStep(llm, request);
691
691
  if (!stepOutcome.ok) return fail(stepOutcome.error);
692
692
  const result = stepOutcome.data;
693
693
  s.usage = addUsage(s.usage, result.usage);
@@ -1097,8 +1097,6 @@ var previewAgent = async (definition, input, options) => {
1097
1097
  ...inputMessages(input)
1098
1098
  ];
1099
1099
  const tools = [
1100
- // The run's own builder — the preview shows the model-visible NAME,
1101
- // not the id, or it would explain a request that never goes out.
1102
1100
  ...allTools.map(buildToolDescriptor),
1103
1101
  ...resolved.respondDescriptor ? [resolved.respondDescriptor] : []
1104
1102
  ];