@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.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") {
@@ -4132,40 +4140,56 @@ var LLM = class {
4132
4140
  openChar = "[";
4133
4141
  closeChar = "]";
4134
4142
  }
4135
- if (startIdx !== -1) {
4136
- let depth = 0;
4137
- let inString = false;
4138
- let endIdx = -1;
4139
- for (let i = startIdx; i < jsonText.length; i++) {
4140
- const char = jsonText[i];
4141
- const prevChar = i > 0 ? jsonText[i - 1] : "";
4142
- if (char === '"' && prevChar !== "\\") {
4143
- inString = !inString;
4144
- continue;
4145
- }
4146
- if (!inString) {
4147
- if (char === openChar) {
4148
- depth++;
4149
- } else if (char === closeChar) {
4150
- depth--;
4151
- if (depth === 0) {
4152
- endIdx = i;
4153
- break;
4154
- }
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;
4155
4168
  }
4156
4169
  }
4157
4170
  }
4158
- if (endIdx !== -1) {
4159
- jsonText = jsonText.substring(startIdx, endIdx + 1);
4160
- }
4171
+ }
4172
+ if (endIdx !== -1) {
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}`);
4161
4180
  }
4162
4181
  try {
4163
4182
  const repairedJson = (0, import_jsonrepair.jsonrepair)(jsonText);
4164
4183
  return JSON.parse(repairedJson);
4165
4184
  } catch (error) {
4185
+ const preview = text.length > 500 ? text.substring(0, 500) + "..." : text;
4166
4186
  throw new Error(`Failed to parse JSON: ${error instanceof Error ? error.message : String(error)}
4167
4187
 
4168
- Original text: ${jsonText.substring(0, 200)}...`);
4188
+ Extracted JSON:
4189
+ ${jsonText.substring(0, 300)}...
4190
+
4191
+ Full response:
4192
+ ${preview}`);
4169
4193
  }
4170
4194
  }
4171
4195
  };