@sembl/core 0.2.0 → 0.3.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/index.cjs CHANGED
@@ -45,6 +45,7 @@ __export(index_exports, {
45
45
  field: () => field,
46
46
  isCoerceInput: () => isCoerceInput,
47
47
  isSource: () => isSource,
48
+ normalizeInstructions: () => normalizeInstructions,
48
49
  partialCoerce: () => partialCoerce,
49
50
  partialCoerceWithProvenance: () => partialCoerceWithProvenance,
50
51
  provenanceInstructions: () => provenanceInstructions,
@@ -507,6 +508,14 @@ var SOURCE_INSTRUCTIONS = [
507
508
  ].join("\n");
508
509
 
509
510
  // src/coerce/prompt-builder.ts
511
+ function normalizeInstructions(instructions) {
512
+ if (instructions === void 0) return [];
513
+ const list = typeof instructions === "string" ? [instructions] : instructions;
514
+ if (!Array.isArray(list) || list.some((entry) => typeof entry !== "string")) {
515
+ throw new RangeError("instructions must be a string or an array of strings");
516
+ }
517
+ return list.map((entry) => entry.trim()).filter((entry) => entry.length > 0);
518
+ }
510
519
  function describeConstraints(constraints) {
511
520
  if (!constraints) {
512
521
  return [];
@@ -629,6 +638,14 @@ function buildPrompt(schema, bundle, options = {}) {
629
638
  lines.push("- Interpret the user's input semantically \u2014 infer meaning, don't just pattern match.");
630
639
  lines.push("- Respect every stated limit exactly; truncate or drop lower-priority content to stay within it.");
631
640
  lines.push("- Return only the structured JSON output matching the schema.");
641
+ const instructions = normalizeInstructions(options.instructions);
642
+ if (instructions.length > 0) {
643
+ lines.push("");
644
+ lines.push("Additional guidance for this extraction:");
645
+ for (const instruction of instructions) {
646
+ lines.push(`- ${instruction}`);
647
+ }
648
+ }
632
649
  return lines.join("\n");
633
650
  }
634
651
 
@@ -1303,6 +1320,7 @@ async function runCoercion(input, options, { mode, provenance }) {
1303
1320
  `maxRepairAttempts must be a non-negative integer, got ${String(options.maxRepairAttempts)}`
1304
1321
  );
1305
1322
  }
1323
+ const instructions = normalizeInstructions(options.instructions);
1306
1324
  const onInvalidField = options.onInvalidField ?? "throw";
1307
1325
  if (!INVALID_FIELD_POLICIES.includes(onInvalidField)) {
1308
1326
  throw new RangeError(
@@ -1333,11 +1351,12 @@ async function runCoercion(input, options, { mode, provenance }) {
1333
1351
  rootSpan
1334
1352
  );
1335
1353
  const promptSpan = tracer.startSpan("buildPrompt", {}, rootSpan);
1336
- const basePrompt = buildPrompt(schema, bundle, { resolvedEnums });
1354
+ const basePrompt = buildPrompt(schema, bundle, { resolvedEnums, instructions });
1337
1355
  const systemPrompt = provenance ? `${basePrompt}
1338
1356
  ${provenanceInstructions({ sourceLabels })}` : basePrompt;
1339
1357
  tracer.addEvent(promptSpan, "promptBuilt", {
1340
- promptLength: systemPrompt.length
1358
+ promptLength: systemPrompt.length,
1359
+ instructionCount: instructions.length
1341
1360
  });
1342
1361
  tracer.endSpan(promptSpan);
1343
1362
  const request = provenance ? toProvenanceSchema(schema, bundle, { sourceLabels }) : { schema, bundle };
@@ -1623,6 +1642,7 @@ function resolveConfig(callConfig) {
1623
1642
  traceSinks: callConfig?.traceSinks ?? global.traceSinks,
1624
1643
  maxRepairAttempts: callConfig?.maxRepairAttempts ?? global.maxRepairAttempts,
1625
1644
  onInvalidField: callConfig?.onInvalidField ?? global.onInvalidField,
1645
+ instructions: callConfig?.instructions ?? global.instructions,
1626
1646
  maxInputChars: callConfig?.maxInputChars ?? global.maxInputChars,
1627
1647
  truncate: callConfig?.truncate ?? global.truncate,
1628
1648
  preprocess: callConfig?.preprocess ?? global.preprocess
@@ -1656,6 +1676,7 @@ var Coercible = class _Coercible {
1656
1676
  traceSinks: this._config.traceSinks,
1657
1677
  maxRepairAttempts: this._config.maxRepairAttempts,
1658
1678
  onInvalidField: this._config.onInvalidField,
1679
+ instructions: this._config.instructions,
1659
1680
  maxInputChars: this._config.maxInputChars,
1660
1681
  truncate: this._config.truncate,
1661
1682
  preprocess: this._config.preprocess
@@ -1741,6 +1762,7 @@ var ConsoleSink = class {
1741
1762
  field,
1742
1763
  isCoerceInput,
1743
1764
  isSource,
1765
+ normalizeInstructions,
1744
1766
  partialCoerce,
1745
1767
  partialCoerceWithProvenance,
1746
1768
  provenanceInstructions,