@sembl/core 0.4.0 → 0.5.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
@@ -36,6 +36,7 @@ __export(index_exports, {
36
36
  ValuesFrom: () => ValuesFrom,
37
37
  budgetSources: () => budgetSources,
38
38
  buildPrompt: () => buildPrompt,
39
+ buildRepairCorrection: () => buildRepairCorrection,
39
40
  buildRepairInput: () => buildRepairInput,
40
41
  bundleOf: () => bundleOf,
41
42
  coerce: () => coerce,
@@ -390,7 +391,7 @@ async function resolveEnumSources(schema, resolver, bundle) {
390
391
  await Promise.all(
391
392
  [...usages].map(async ([sourceId, usage]) => {
392
393
  try {
393
- const values = await resolver(sourceId);
394
+ const values = await resolver(sourceId, { sourceId, schema, ...usage });
394
395
  if (!values || values.length === 0) {
395
396
  failures.push({ sourceId, reason: "empty", ...usage });
396
397
  return;
@@ -828,7 +829,7 @@ function renderReceived(received) {
828
829
  return text.length > MAX_RECEIVED_LENGTH ? `${text.slice(0, MAX_RECEIVED_LENGTH)}\u2026 (truncated)` : text;
829
830
  }
830
831
  function buildRepairInput(originalInput, rejected, issues) {
831
- const lines = [
832
+ return [
832
833
  originalInput,
833
834
  "",
834
835
  "---",
@@ -837,9 +838,11 @@ function buildRepairInput(originalInput, rejected, issues) {
837
838
  "",
838
839
  JSON.stringify(rejected, null, 2),
839
840
  "",
840
- "It was rejected because:",
841
- ""
842
- ];
841
+ buildRepairCorrection(issues)
842
+ ].join("\n");
843
+ }
844
+ function buildRepairCorrection(issues) {
845
+ const lines = ["The output was rejected because:", ""];
843
846
  for (const issue of issues) {
844
847
  lines.push(`- ${issue.path}: ${issue.message} (received: ${renderReceived(issue.received)})`);
845
848
  }
@@ -1633,11 +1636,21 @@ async function runCoercion(input, options, { mode, provenance, traceAttributes }
1633
1636
  let issues = [];
1634
1637
  let run = { data: {}, provenance: {}, issues: [], usage };
1635
1638
  let emptyRetries = 0;
1639
+ const multiTurn = provider.supportsHistory === true;
1640
+ const history = [];
1641
+ const followUp = (rejected, text, folded) => {
1642
+ if (multiTurn) {
1643
+ history.push({ role: "assistant", data: rejected }, { role: "user", text });
1644
+ } else {
1645
+ userInput = folded;
1646
+ }
1647
+ };
1636
1648
  for (let attempt = 0; attempt <= maxRepairAttempts; attempt++) {
1637
- const llmSpan = tracer.startSpan("llmCall", { attempt }, rootSpan);
1649
+ const llmSpan = tracer.startSpan("llmCall", { attempt, turns: history.length }, rootSpan);
1638
1650
  const response = await provider.complete({
1639
1651
  systemPrompt,
1640
1652
  userInput,
1653
+ ...history.length > 0 ? { history: [...history] } : {},
1641
1654
  jsonSchema,
1642
1655
  schema: prepared.schema,
1643
1656
  bundle: prepared.bundle,
@@ -1650,11 +1663,11 @@ async function runCoercion(input, options, { mode, provenance, traceAttributes }
1650
1663
  if (hasInput && emptyRetries < retryOnEmpty && isEmptyResult(run.data)) {
1651
1664
  emptyRetries += 1;
1652
1665
  tracer.addEvent(rootSpan, "emptyRetry", { retry: emptyRetries });
1653
- userInput = `${renderedInput}
1666
+ followUp(response.data, EMPTY_RETRY_NOTE, `${renderedInput}
1654
1667
 
1655
1668
  ---
1656
1669
 
1657
- ${EMPTY_RETRY_NOTE}`;
1670
+ ${EMPTY_RETRY_NOTE}`);
1658
1671
  attempt -= 1;
1659
1672
  continue;
1660
1673
  }
@@ -1695,7 +1708,11 @@ ${EMPTY_RETRY_NOTE}`;
1695
1708
  issueCount: issues.length,
1696
1709
  paths: issues.map((issue) => issue.path)
1697
1710
  });
1698
- userInput = buildRepairInput(renderedInput, run.data, issues);
1711
+ followUp(
1712
+ response.data,
1713
+ buildRepairCorrection(issues),
1714
+ buildRepairInput(renderedInput, run.data, issues)
1715
+ );
1699
1716
  }
1700
1717
  }
1701
1718
  throw new CoerceError(issues);
@@ -2115,6 +2132,7 @@ var ConsoleSink = class {
2115
2132
  ValuesFrom,
2116
2133
  budgetSources,
2117
2134
  buildPrompt,
2135
+ buildRepairCorrection,
2118
2136
  buildRepairInput,
2119
2137
  bundleOf,
2120
2138
  coerce,