@superatomai/sdk-node 0.0.35 → 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") {
@@ -4088,40 +4096,56 @@ var LLM = class {
4088
4096
  openChar = "[";
4089
4097
  closeChar = "]";
4090
4098
  }
4091
- if (startIdx !== -1) {
4092
- let depth = 0;
4093
- let inString = false;
4094
- let endIdx = -1;
4095
- for (let i = startIdx; i < jsonText.length; i++) {
4096
- const char = jsonText[i];
4097
- const prevChar = i > 0 ? jsonText[i - 1] : "";
4098
- if (char === '"' && prevChar !== "\\") {
4099
- inString = !inString;
4100
- continue;
4101
- }
4102
- if (!inString) {
4103
- if (char === openChar) {
4104
- depth++;
4105
- } else if (char === closeChar) {
4106
- depth--;
4107
- if (depth === 0) {
4108
- endIdx = i;
4109
- break;
4110
- }
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;
4111
4124
  }
4112
4125
  }
4113
4126
  }
4114
- if (endIdx !== -1) {
4115
- jsonText = jsonText.substring(startIdx, endIdx + 1);
4116
- }
4127
+ }
4128
+ if (endIdx !== -1) {
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}`);
4117
4136
  }
4118
4137
  try {
4119
4138
  const repairedJson = jsonrepair(jsonText);
4120
4139
  return JSON.parse(repairedJson);
4121
4140
  } catch (error) {
4141
+ const preview = text.length > 500 ? text.substring(0, 500) + "..." : text;
4122
4142
  throw new Error(`Failed to parse JSON: ${error instanceof Error ? error.message : String(error)}
4123
4143
 
4124
- Original text: ${jsonText.substring(0, 200)}...`);
4144
+ Extracted JSON:
4145
+ ${jsonText.substring(0, 300)}...
4146
+
4147
+ Full response:
4148
+ ${preview}`);
4125
4149
  }
4126
4150
  }
4127
4151
  };