@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.d.mts CHANGED
@@ -1049,6 +1049,7 @@ type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
1049
1049
  interface LLMMessages {
1050
1050
  sys: SystemPrompt;
1051
1051
  user: string;
1052
+ prefill?: string;
1052
1053
  }
1053
1054
  interface LLMOptions {
1054
1055
  model?: string;
package/dist/index.d.ts CHANGED
@@ -1049,6 +1049,7 @@ type SystemPrompt = string | Anthropic.Messages.TextBlockParam[];
1049
1049
  interface LLMMessages {
1050
1050
  sys: SystemPrompt;
1051
1051
  user: string;
1052
+ prefill?: string;
1052
1053
  }
1053
1054
  interface LLMOptions {
1054
1055
  model?: string;
package/dist/index.js CHANGED
@@ -3585,18 +3585,26 @@ var LLM = class {
3585
3585
  const client = new import_sdk.default({
3586
3586
  apiKey
3587
3587
  });
3588
+ const apiMessages = [{
3589
+ role: "user",
3590
+ content: messages.user
3591
+ }];
3592
+ const prefill = messages.prefill || (json ? "{" : void 0);
3593
+ if (prefill) {
3594
+ apiMessages.push({
3595
+ role: "assistant",
3596
+ content: prefill
3597
+ });
3598
+ }
3588
3599
  const stream = await client.messages.create({
3589
3600
  model: modelName,
3590
3601
  max_tokens: options.maxTokens || 1e3,
3591
3602
  temperature: options.temperature,
3592
3603
  system: this._normalizeSystemPrompt(messages.sys),
3593
- messages: [{
3594
- role: "user",
3595
- content: messages.user
3596
- }],
3604
+ messages: apiMessages,
3597
3605
  stream: true
3598
3606
  });
3599
- let fullText = "";
3607
+ let fullText = prefill || "";
3600
3608
  let usage = null;
3601
3609
  for await (const chunk of stream) {
3602
3610
  if (chunk.type === "content_block_delta" && chunk.delta.type === "text_delta") {
@@ -4120,27 +4128,68 @@ var LLM = class {
4120
4128
  }
4121
4129
  const firstBrace = jsonText.indexOf("{");
4122
4130
  const firstBracket = jsonText.indexOf("[");
4123
- const lastBrace = jsonText.lastIndexOf("}");
4124
- const lastBracket = jsonText.lastIndexOf("]");
4125
4131
  let startIdx = -1;
4126
- let endIdx = -1;
4132
+ let openChar = "";
4133
+ let closeChar = "";
4127
4134
  if (firstBrace !== -1 && (firstBracket === -1 || firstBrace < firstBracket)) {
4128
4135
  startIdx = firstBrace;
4129
- endIdx = lastBrace;
4136
+ openChar = "{";
4137
+ closeChar = "}";
4130
4138
  } else if (firstBracket !== -1) {
4131
4139
  startIdx = firstBracket;
4132
- endIdx = lastBracket;
4140
+ openChar = "[";
4141
+ closeChar = "]";
4133
4142
  }
4134
- if (startIdx !== -1 && endIdx !== -1 && startIdx < endIdx) {
4143
+ if (startIdx === -1) {
4144
+ const preview = text.length > 500 ? text.substring(0, 500) + "..." : text;
4145
+ throw new Error(`No JSON found in response. LLM returned plain text instead of JSON.
4146
+
4147
+ Full response:
4148
+ ${preview}`);
4149
+ }
4150
+ let depth = 0;
4151
+ let inString = false;
4152
+ let endIdx = -1;
4153
+ for (let i = startIdx; i < jsonText.length; i++) {
4154
+ const char = jsonText[i];
4155
+ const prevChar = i > 0 ? jsonText[i - 1] : "";
4156
+ if (char === '"' && prevChar !== "\\") {
4157
+ inString = !inString;
4158
+ continue;
4159
+ }
4160
+ if (!inString) {
4161
+ if (char === openChar) {
4162
+ depth++;
4163
+ } else if (char === closeChar) {
4164
+ depth--;
4165
+ if (depth === 0) {
4166
+ endIdx = i;
4167
+ break;
4168
+ }
4169
+ }
4170
+ }
4171
+ }
4172
+ if (endIdx !== -1) {
4135
4173
  jsonText = jsonText.substring(startIdx, endIdx + 1);
4174
+ } else {
4175
+ const preview = text.length > 500 ? text.substring(0, 500) + "..." : text;
4176
+ throw new Error(`Incomplete JSON - no matching closing ${closeChar} found.
4177
+
4178
+ Full response:
4179
+ ${preview}`);
4136
4180
  }
4137
4181
  try {
4138
4182
  const repairedJson = (0, import_jsonrepair.jsonrepair)(jsonText);
4139
4183
  return JSON.parse(repairedJson);
4140
4184
  } catch (error) {
4185
+ const preview = text.length > 500 ? text.substring(0, 500) + "..." : text;
4141
4186
  throw new Error(`Failed to parse JSON: ${error instanceof Error ? error.message : String(error)}
4142
4187
 
4143
- Original text: ${jsonText.substring(0, 200)}...`);
4188
+ Extracted JSON:
4189
+ ${jsonText.substring(0, 300)}...
4190
+
4191
+ Full response:
4192
+ ${preview}`);
4144
4193
  }
4145
4194
  }
4146
4195
  };