@superatomai/sdk-node 0.0.34 → 0.0.36

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.mjs CHANGED
@@ -3541,18 +3541,26 @@ var LLM = class {
3541
3541
  const client = new Anthropic({
3542
3542
  apiKey
3543
3543
  });
3544
+ const apiMessages = [{
3545
+ role: "user",
3546
+ content: messages.user
3547
+ }];
3548
+ const prefill = messages.prefill || (json ? "{" : void 0);
3549
+ if (prefill) {
3550
+ apiMessages.push({
3551
+ role: "assistant",
3552
+ content: prefill
3553
+ });
3554
+ }
3544
3555
  const stream = await client.messages.create({
3545
3556
  model: modelName,
3546
3557
  max_tokens: options.maxTokens || 1e3,
3547
3558
  temperature: options.temperature,
3548
3559
  system: this._normalizeSystemPrompt(messages.sys),
3549
- messages: [{
3550
- role: "user",
3551
- content: messages.user
3552
- }],
3560
+ messages: apiMessages,
3553
3561
  stream: true
3554
3562
  });
3555
- let fullText = "";
3563
+ let fullText = prefill || "";
3556
3564
  let usage = null;
3557
3565
  for await (const chunk of stream) {
3558
3566
  if (chunk.type === "content_block_delta" && chunk.delta.type === "text_delta") {
@@ -4076,27 +4084,68 @@ var LLM = class {
4076
4084
  }
4077
4085
  const firstBrace = jsonText.indexOf("{");
4078
4086
  const firstBracket = jsonText.indexOf("[");
4079
- const lastBrace = jsonText.lastIndexOf("}");
4080
- const lastBracket = jsonText.lastIndexOf("]");
4081
4087
  let startIdx = -1;
4082
- let endIdx = -1;
4088
+ let openChar = "";
4089
+ let closeChar = "";
4083
4090
  if (firstBrace !== -1 && (firstBracket === -1 || firstBrace < firstBracket)) {
4084
4091
  startIdx = firstBrace;
4085
- endIdx = lastBrace;
4092
+ openChar = "{";
4093
+ closeChar = "}";
4086
4094
  } else if (firstBracket !== -1) {
4087
4095
  startIdx = firstBracket;
4088
- endIdx = lastBracket;
4096
+ openChar = "[";
4097
+ closeChar = "]";
4089
4098
  }
4090
- if (startIdx !== -1 && endIdx !== -1 && startIdx < endIdx) {
4099
+ if (startIdx === -1) {
4100
+ const preview = text.length > 500 ? text.substring(0, 500) + "..." : text;
4101
+ throw new Error(`No JSON found in response. LLM returned plain text instead of JSON.
4102
+
4103
+ Full response:
4104
+ ${preview}`);
4105
+ }
4106
+ let depth = 0;
4107
+ let inString = false;
4108
+ let endIdx = -1;
4109
+ for (let i = startIdx; i < jsonText.length; i++) {
4110
+ const char = jsonText[i];
4111
+ const prevChar = i > 0 ? jsonText[i - 1] : "";
4112
+ if (char === '"' && prevChar !== "\\") {
4113
+ inString = !inString;
4114
+ continue;
4115
+ }
4116
+ if (!inString) {
4117
+ if (char === openChar) {
4118
+ depth++;
4119
+ } else if (char === closeChar) {
4120
+ depth--;
4121
+ if (depth === 0) {
4122
+ endIdx = i;
4123
+ break;
4124
+ }
4125
+ }
4126
+ }
4127
+ }
4128
+ if (endIdx !== -1) {
4091
4129
  jsonText = jsonText.substring(startIdx, endIdx + 1);
4130
+ } else {
4131
+ const preview = text.length > 500 ? text.substring(0, 500) + "..." : text;
4132
+ throw new Error(`Incomplete JSON - no matching closing ${closeChar} found.
4133
+
4134
+ Full response:
4135
+ ${preview}`);
4092
4136
  }
4093
4137
  try {
4094
4138
  const repairedJson = jsonrepair(jsonText);
4095
4139
  return JSON.parse(repairedJson);
4096
4140
  } catch (error) {
4141
+ const preview = text.length > 500 ? text.substring(0, 500) + "..." : text;
4097
4142
  throw new Error(`Failed to parse JSON: ${error instanceof Error ? error.message : String(error)}
4098
4143
 
4099
- Original text: ${jsonText.substring(0, 200)}...`);
4144
+ Extracted JSON:
4145
+ ${jsonText.substring(0, 300)}...
4146
+
4147
+ Full response:
4148
+ ${preview}`);
4100
4149
  }
4101
4150
  }
4102
4151
  };