@browser-ai/core 2.1.10 → 2.1.12

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
@@ -101,11 +101,35 @@ ${payloads.join("\n")}
101
101
  var DEFAULT_OPTIONS = {
102
102
  supportXmlTags: true,
103
103
  supportPythonStyle: true,
104
- supportParametersField: true
104
+ supportParametersField: true,
105
+ supportCallColonStyle: true
105
106
  };
106
107
  function generateToolCallId() {
107
108
  return `call_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
108
109
  }
110
+ function parseCallColonParams(params) {
111
+ const args = {};
112
+ if (!params || !params.trim()) return args;
113
+ const pairs = params.split(",").map((s) => s.trim());
114
+ for (const pair of pairs) {
115
+ const colonIndex = pair.indexOf(":");
116
+ if (colonIndex > 0) {
117
+ const key = pair.substring(0, colonIndex).trim();
118
+ const rawValue = pair.substring(colonIndex + 1).trim();
119
+ if (rawValue === "true") {
120
+ args[key] = true;
121
+ } else if (rawValue === "false") {
122
+ args[key] = false;
123
+ } else if (rawValue === "null") {
124
+ args[key] = null;
125
+ } else {
126
+ const numValue = Number(rawValue);
127
+ args[key] = !isNaN(numValue) && rawValue !== "" ? numValue : rawValue;
128
+ }
129
+ }
130
+ }
131
+ return args;
132
+ }
109
133
  function buildRegex(options) {
110
134
  const patterns = [];
111
135
  patterns.push("```tool[_-]?call\\s*([\\s\\S]*?)```");
@@ -115,6 +139,9 @@ function buildRegex(options) {
115
139
  if (options.supportPythonStyle) {
116
140
  patterns.push("\\[(\\w+)\\(([^)]*)\\)\\]");
117
141
  }
142
+ if (options.supportCallColonStyle) {
143
+ patterns.push("<\\|tool_call>\\s*([\\s\\S]*?)\\s*<tool_call\\|>");
144
+ }
118
145
  return new RegExp(patterns.join("|"), "gi");
119
146
  }
120
147
  function parseJsonFunctionCalls(response, options = DEFAULT_OPTIONS) {
@@ -159,7 +186,20 @@ function parseJsonFunctionCalls(response, options = DEFAULT_OPTIONS) {
159
186
  continue;
160
187
  }
161
188
  }
162
- const innerContent = match[1] || match[2] || "";
189
+ if (mergedOptions.supportCallColonStyle) {
190
+ const callMatch = fullMatch.match(/call:(\w+)\{([^}]*)\}/);
191
+ if (callMatch) {
192
+ const [, funcName, params] = callMatch;
193
+ toolCalls.push({
194
+ type: "tool-call",
195
+ toolCallId: generateToolCallId(),
196
+ toolName: funcName,
197
+ args: parseCallColonParams(params)
198
+ });
199
+ continue;
200
+ }
201
+ }
202
+ const innerContent = match.slice(1).find((g) => g !== void 0) || "";
163
203
  const trimmed = innerContent.trim();
164
204
  if (!trimmed) continue;
165
205
  try {
@@ -225,6 +265,11 @@ var EXTENDED_FENCE_PATTERNS = [
225
265
  start: "<tool_call>",
226
266
  end: "</tool_call>",
227
267
  reconstructStart: "<tool_call>"
268
+ },
269
+ {
270
+ start: "<|tool_call>",
271
+ end: "<tool_call|>",
272
+ reconstructStart: "<|tool_call>"
228
273
  }
229
274
  ];
230
275
  var ToolCallFenceDetector = class {
@@ -460,6 +505,10 @@ function extractToolName(content) {
460
505
  if (jsonMatch) {
461
506
  return jsonMatch[1];
462
507
  }
508
+ const callColonMatch = content.match(/call:(\w+)\{/);
509
+ if (callColonMatch) {
510
+ return callColonMatch[1];
511
+ }
463
512
  return null;
464
513
  }
465
514
  var ARGUMENTS_FIELD_REGEX = /"arguments"\s*:\s*/g;
@@ -1311,17 +1360,19 @@ var BrowserAIChatLanguageModel = class {
1311
1360
  if (responseFormat?.type === "json") {
1312
1361
  promptOptions.responseConstraint = responseFormat.schema;
1313
1362
  }
1363
+ const sessionOptions = {};
1314
1364
  if (temperature !== void 0) {
1315
- promptOptions.temperature = temperature;
1365
+ sessionOptions.temperature = temperature;
1316
1366
  }
1317
1367
  if (topK !== void 0) {
1318
- promptOptions.topK = topK;
1368
+ sessionOptions.topK = topK;
1319
1369
  }
1320
1370
  return {
1321
1371
  systemMessage,
1322
1372
  messages,
1323
1373
  warnings,
1324
1374
  promptOptions,
1375
+ sessionOptions,
1325
1376
  hasMultiModalInput,
1326
1377
  expectedInputs,
1327
1378
  functionTools
@@ -1341,6 +1392,7 @@ var BrowserAIChatLanguageModel = class {
1341
1392
  messages,
1342
1393
  warnings,
1343
1394
  promptOptions,
1395
+ sessionOptions,
1344
1396
  expectedInputs,
1345
1397
  functionTools
1346
1398
  } = converted;
@@ -1352,11 +1404,15 @@ var BrowserAIChatLanguageModel = class {
1352
1404
  }
1353
1405
  );
1354
1406
  const session = await this.getSession(
1355
- void 0,
1407
+ { ...sessionOptions, signal: options.abortSignal },
1356
1408
  expectedInputs,
1357
1409
  systemPrompt || void 0
1358
1410
  );
1359
- const rawResponse = await session.prompt(messages, promptOptions);
1411
+ const promptCallOptions = {
1412
+ ...promptOptions,
1413
+ signal: options.abortSignal
1414
+ };
1415
+ const rawResponse = await session.prompt(messages, promptCallOptions);
1360
1416
  const { toolCalls, textContent } = parseJsonFunctionCalls(rawResponse);
1361
1417
  if (toolCalls.length > 0) {
1362
1418
  const toolCallsToEmit = toolCalls.slice(0, 1);
@@ -1488,6 +1544,7 @@ var BrowserAIChatLanguageModel = class {
1488
1544
  messages,
1489
1545
  warnings,
1490
1546
  promptOptions,
1547
+ sessionOptions,
1491
1548
  expectedInputs,
1492
1549
  functionTools
1493
1550
  } = converted;
@@ -1499,7 +1556,7 @@ var BrowserAIChatLanguageModel = class {
1499
1556
  }
1500
1557
  );
1501
1558
  const session = await this.getSession(
1502
- void 0,
1559
+ { ...sessionOptions, signal: options.abortSignal },
1503
1560
  expectedInputs,
1504
1561
  systemPrompt || void 0
1505
1562
  );