@intuned/browser-dev 0.1.16-dev.1 → 0.1.17-dev.1

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.
@@ -8,10 +8,10 @@ var _anthropicModel = require("../models/anthropicModel");
8
8
  var _neverthrow = require("neverthrow");
9
9
  var Errors = _interopRequireWildcard(require("../types/errors"));
10
10
  var _utils = require("./utils");
11
- var _Logger = require("../../common/Logger");
12
11
  var _aiModelsValidations = require("../common/aiModelsValidations");
13
12
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
14
13
  async function extractStructuredDataUsingClaude(input) {
14
+ var _unwrappedResponse$us, _unwrappedResponse$us2;
15
15
  const {
16
16
  entityName,
17
17
  model,
@@ -96,6 +96,9 @@ async function extractStructuredDataUsingClaude(input) {
96
96
  return (0, _neverthrow.err)(response.error);
97
97
  }
98
98
  const unwrappedResponse = response.value.data;
99
+ const costInCents = (0, _utils.parseCostInCents)(response.value.response.headers.get("x-ai-cost-in-cents"));
100
+ const totalTokens = (((_unwrappedResponse$us = unwrappedResponse.usage) === null || _unwrappedResponse$us === void 0 ? void 0 : _unwrappedResponse$us.input_tokens) ?? 0) + (((_unwrappedResponse$us2 = unwrappedResponse.usage) === null || _unwrappedResponse$us2 === void 0 ? void 0 : _unwrappedResponse$us2.output_tokens) ?? 0);
101
+ (0, _utils.logAiCallUsage)(costInCents, totalTokens);
99
102
  if (unwrappedResponse.stop_reason === "max_tokens") {
100
103
  return (0, _neverthrow.err)(Errors.AiCallFailed("response from ai exceeds model maximum output tokens, try to be more specific with what data you need to extract"));
101
104
  }
@@ -117,18 +120,9 @@ async function extractStructuredDataUsingClaude(input) {
117
120
  return (0, _neverthrow.err)(Errors.invalidExtractionResult("the model was not able to extract data correctly"));
118
121
  }
119
122
  const result = (0, _utils.getResultFromOutputSchema)(originalJsonSchema, entityName, tool.input);
120
- const callCost = response.value.response.headers.get("x-ai-cost-in-cents");
121
- if (input.logAiCallCost) {
122
- if (apiKey) {
123
- _Logger.logger.info(`extractor ${input.identifier}: AI cost is not calculated (using custom API key)`);
124
- } else if (callCost) {
125
- const cost = parseFloat(callCost);
126
- if (!isNaN(cost)) {
127
- _Logger.logger.info(`extractor ${input.identifier}: AI cost is $${cost / 100}`);
128
- }
129
- }
130
- }
131
123
  return (0, _neverthrow.ok)({
132
- result
124
+ result,
125
+ costInCents,
126
+ totalTokens
133
127
  });
134
128
  }
@@ -7,11 +7,10 @@ exports.extractStructuredDataUsingOpenAi = extractStructuredDataUsingOpenAi;
7
7
  var _neverthrow = require("neverthrow");
8
8
  var Errors = _interopRequireWildcard(require("../types/errors"));
9
9
  var _utils = require("./utils");
10
- var _Logger = require("../../common/Logger");
11
10
  var _openaiModel = require("../models/openaiModel");
12
11
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
13
12
  async function extractStructuredDataUsingOpenAi(input) {
14
- var _completion$value$dat, _completion$value$dat2;
13
+ var _completion$value$dat, _completion$value$dat2, _completion$value$dat3;
15
14
  const {
16
15
  entityName,
17
16
  model,
@@ -50,13 +49,16 @@ async function extractStructuredDataUsingOpenAi(input) {
50
49
  content.push(...imageContent);
51
50
  }
52
51
  const modelName = input.model;
52
+ const supportsCustomTemperature = !/^(o\d|gpt-5)/i.test(modelName);
53
53
  const toolName = `extract_${entityName}`;
54
54
  const openAiInstance = (0, _openaiModel.createOpenAIInstance)({
55
55
  apiKey
56
56
  });
57
57
  const completion = await (0, _neverthrow.fromPromise)(openAiInstance.chat.completions.create({
58
- max_tokens: 4000,
59
- temperature: 0,
58
+ max_completion_tokens: 4000,
59
+ ...(supportsCustomTemperature ? {
60
+ temperature: 0
61
+ } : {}),
60
62
  model: modelName,
61
63
  messages: [{
62
64
  role: "system",
@@ -90,14 +92,17 @@ async function extractStructuredDataUsingOpenAi(input) {
90
92
  if (completion.isErr()) {
91
93
  return (0, _neverthrow.err)(completion.error);
92
94
  }
95
+ const costInCents = (0, _utils.parseCostInCents)(completion.value.response.headers.get("x-ai-cost-in-cents"));
96
+ const totalTokens = (_completion$value$dat = completion.value.data.usage) === null || _completion$value$dat === void 0 ? void 0 : _completion$value$dat.total_tokens;
97
+ (0, _utils.logAiCallUsage)(costInCents, totalTokens);
93
98
  if (completion.value.data.choices[0].finish_reason === "length") {
94
99
  return (0, _neverthrow.err)(Errors.AiCallFailed("response from ai exceeds model maximum output tokens, try to be more specific with what data you need to extract"));
95
100
  }
96
- const noDataFound = (_completion$value$dat = completion.value.data.choices[0].message.tool_calls) === null || _completion$value$dat === void 0 ? void 0 : _completion$value$dat.some(content => content.type === "function" && content.function.name == "no_data_found");
101
+ const noDataFound = (_completion$value$dat2 = completion.value.data.choices[0].message.tool_calls) === null || _completion$value$dat2 === void 0 ? void 0 : _completion$value$dat2.some(content => content.type === "function" && content.function.name == "no_data_found");
97
102
  if (noDataFound) {
98
103
  return (0, _neverthrow.err)(Errors.NoDataFound("data isn't found in the text or images."));
99
104
  }
100
- let functionCall = (_completion$value$dat2 = completion.value.data.choices[0].message.tool_calls) === null || _completion$value$dat2 === void 0 || (_completion$value$dat2 = _completion$value$dat2.find(t => t.type === "function" && t.function.name === toolName)) === null || _completion$value$dat2 === void 0 ? void 0 : _completion$value$dat2.function;
105
+ let functionCall = (_completion$value$dat3 = completion.value.data.choices[0].message.tool_calls) === null || _completion$value$dat3 === void 0 || (_completion$value$dat3 = _completion$value$dat3.find(t => t.type === "function" && t.function.name === toolName)) === null || _completion$value$dat3 === void 0 ? void 0 : _completion$value$dat3.function;
101
106
  if (!functionCall) {
102
107
  functionCall = completion.value.data.choices[0].message.function_call;
103
108
  if (!functionCall) {
@@ -114,18 +119,9 @@ async function extractStructuredDataUsingOpenAi(input) {
114
119
  }
115
120
  const result = (0, _utils.getResultFromOutputSchema)(originalJsonSchema, entityName, parsedData.value);
116
121
  const formatted = (0, _utils.cleanupAiResult)(result);
117
- const callCost = completion.value.response.headers.get("x-ai-cost-in-cents");
118
- if (input.logAiCallCost) {
119
- if (apiKey) {
120
- _Logger.logger.info(`extractor ${input.identifier}: AI cost is not calculated (using custom API key)`);
121
- } else if (callCost) {
122
- const cost = parseFloat(callCost);
123
- if (!isNaN(cost)) {
124
- _Logger.logger.info(`extractor ${input.identifier}: AI cost is $${cost / 100}`);
125
- }
126
- }
127
- }
128
122
  return (0, _neverthrow.ok)({
129
- result: formatted
123
+ result: formatted,
124
+ costInCents,
125
+ totalTokens
130
126
  });
131
127
  }
@@ -7,11 +7,10 @@ exports.extractStructuredDataUsingAiInstance = extractStructuredDataUsingAiInsta
7
7
  var _neverthrow = require("neverthrow");
8
8
  var Errors = _interopRequireWildcard(require("../types/errors"));
9
9
  var _utils = require("./utils");
10
- var _Logger = require("../../common/Logger");
11
10
  var _ai = require("ai");
12
11
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
13
12
  async function extractStructuredDataUsingAiInstance(input) {
14
- var _apiResult$value$tool, _apiResult$value$tool2, _apiResult$value$resp;
13
+ var _apiResult$value$resp, _apiResult$value$usag, _apiResult$value$tool, _apiResult$value$tool2;
15
14
  const {
16
15
  entityName,
17
16
  model,
@@ -20,9 +19,7 @@ async function extractStructuredDataUsingAiInstance(input) {
20
19
  text,
21
20
  extraUserMessages,
22
21
  images,
23
- apiKey,
24
- apiName,
25
- maxTokens
22
+ apiName
26
23
  } = input;
27
24
  const processedJsonSchema = (0, _utils.processInputSchema)(originalJsonSchema, entityName);
28
25
  const content = [];
@@ -88,6 +85,9 @@ async function extractStructuredDataUsingAiInstance(input) {
88
85
  if (apiResult.isErr()) {
89
86
  return (0, _neverthrow.err)(apiResult.error);
90
87
  }
88
+ const costInCents = (0, _utils.parseCostInCents)((_apiResult$value$resp = apiResult.value.response.headers) === null || _apiResult$value$resp === void 0 ? void 0 : _apiResult$value$resp["x-ai-cost-in-cents"]);
89
+ const totalTokens = (_apiResult$value$usag = apiResult.value.usage) === null || _apiResult$value$usag === void 0 ? void 0 : _apiResult$value$usag.totalTokens;
90
+ (0, _utils.logAiCallUsage)(costInCents, totalTokens);
91
91
  if (apiResult.value.finishReason === "length") {
92
92
  return (0, _neverthrow.err)(Errors.AiCallFailed("response from ai exceeds model maximum output tokens, try to be more specific with what data you need to extract"));
93
93
  }
@@ -105,18 +105,9 @@ async function extractStructuredDataUsingAiInstance(input) {
105
105
  }
106
106
  const result = (0, _utils.getResultFromOutputSchema)(originalJsonSchema, entityName, extractedData);
107
107
  const formatted = (0, _utils.cleanupAiResult)(result);
108
- const callCost = (_apiResult$value$resp = apiResult.value.response.headers) === null || _apiResult$value$resp === void 0 ? void 0 : _apiResult$value$resp["x-ai-cost-in-cents"];
109
- if (input.logAiCallCost) {
110
- if (apiKey) {
111
- _Logger.logger.info(`extractor ${input.identifier}: AI cost is not calculated (using custom API key)`);
112
- } else if (callCost) {
113
- const cost = parseFloat(callCost);
114
- if (!isNaN(cost)) {
115
- _Logger.logger.info(`extractor ${input.identifier}: AI cost is $${cost / 100}`);
116
- }
117
- }
118
- }
119
108
  return (0, _neverthrow.ok)({
120
- result: formatted
109
+ result: formatted,
110
+ costInCents,
111
+ totalTokens
121
112
  });
122
113
  }
@@ -47,9 +47,13 @@ async function extractStructuredDataUsingAi(input) {
47
47
  return (0, _neverthrow.err)(extractionResult.error);
48
48
  }
49
49
  const {
50
- result
50
+ result,
51
+ costInCents,
52
+ totalTokens
51
53
  } = extractionResult.value;
52
54
  return (0, _neverthrow.ok)({
53
- result: (0, _utils.cleanupAiResult)(result)
55
+ result: (0, _utils.cleanupAiResult)(result),
56
+ costInCents,
57
+ totalTokens
54
58
  });
55
59
  }
@@ -6,11 +6,26 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.cleanupAiResult = cleanupAiResult;
7
7
  exports.getRandomItems = getRandomItems;
8
8
  exports.getResultFromOutputSchema = getResultFromOutputSchema;
9
+ exports.logAiCallUsage = logAiCallUsage;
10
+ exports.parseCostInCents = parseCostInCents;
9
11
  exports.processInputSchema = processInputSchema;
12
+ var _Logger = require("../../common/Logger");
10
13
  function getRandomItems(arr, numItems) {
11
14
  const shuffled = arr.sort(() => 0.5 - Math.random());
12
15
  return shuffled.slice(0, numItems);
13
16
  }
17
+ function parseCostInCents(headerValue) {
18
+ if (!headerValue) return undefined;
19
+ const cost = parseFloat(headerValue);
20
+ return isNaN(cost) ? undefined : cost;
21
+ }
22
+ function logAiCallUsage(costInCents, totalTokens) {
23
+ if (costInCents !== undefined) {
24
+ _Logger.logger.info(`Total LLM Cost In Cents: ${costInCents}`);
25
+ } else if (totalTokens !== undefined) {
26
+ _Logger.logger.info(`Total LLM Tokens: ${totalTokens}`);
27
+ }
28
+ }
14
29
  function processInputSchema(originalJsonSchema, entityName) {
15
30
  const internalSchema = structuredClone(originalJsonSchema);
16
31
  delete internalSchema.description;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intuned/browser-dev",
3
- "version": "0.1.16-dev.1",
3
+ "version": "0.1.17-dev.1",
4
4
  "description": "runner package for intuned functions",
5
5
  "types": "./dist/index.d.ts",
6
6
  "typesVersions": {