@sentrial/sdk 0.4.3 → 0.4.4

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.cjs CHANGED
@@ -1638,6 +1638,7 @@ function getClient() {
1638
1638
  }
1639
1639
  function configure(config) {
1640
1640
  defaultClient = new SentrialClient(config);
1641
+ setDefaultClient(defaultClient);
1641
1642
  }
1642
1643
  function begin(params) {
1643
1644
  return getClient().begin(params);
@@ -1656,6 +1657,10 @@ var sentrial = {
1656
1657
  };
1657
1658
 
1658
1659
  // src/vercel.ts
1660
+ function resolveStringOrFn(value, fallback) {
1661
+ if (typeof value === "function") return value() ?? fallback;
1662
+ return value ?? fallback;
1663
+ }
1659
1664
  var _defaultClient2 = null;
1660
1665
  var _globalConfig = {};
1661
1666
  function configureVercel(config) {
@@ -1664,6 +1669,7 @@ function configureVercel(config) {
1664
1669
  apiUrl: config.apiUrl,
1665
1670
  failSilently: config.failSilently ?? true
1666
1671
  });
1672
+ setDefaultClient(_defaultClient2);
1667
1673
  _globalConfig = {
1668
1674
  defaultAgent: config.defaultAgent,
1669
1675
  userId: config.userId,
@@ -1713,14 +1719,26 @@ function calculateCostForCall(provider, modelId, promptTokens, completionTokens)
1713
1719
  return 0;
1714
1720
  }
1715
1721
  }
1722
+ function extractContentText(content) {
1723
+ if (typeof content === "string") return content;
1724
+ if (Array.isArray(content)) {
1725
+ const textParts = content.filter((part) => part && part.type === "text" && typeof part.text === "string").map((part) => part.text);
1726
+ if (textParts.length > 0) return textParts.join("\n");
1727
+ }
1728
+ return JSON.stringify(content);
1729
+ }
1716
1730
  function extractInput(params) {
1717
1731
  if (params.prompt) return params.prompt;
1718
1732
  if (params.messages && params.messages.length > 0) {
1719
1733
  const lastUserMessage = [...params.messages].reverse().find((m) => m.role === "user");
1720
1734
  if (lastUserMessage) {
1721
- return typeof lastUserMessage.content === "string" ? lastUserMessage.content : JSON.stringify(lastUserMessage.content);
1735
+ return extractContentText(lastUserMessage.content);
1722
1736
  }
1723
- return JSON.stringify(params.messages);
1737
+ const lastNonSystem = [...params.messages].reverse().find((m) => m.role !== "system");
1738
+ if (lastNonSystem) {
1739
+ return extractContentText(lastNonSystem.content);
1740
+ }
1741
+ return "";
1724
1742
  }
1725
1743
  return "";
1726
1744
  }
@@ -1834,8 +1852,8 @@ function wrapGenerateText(originalFn, client, config) {
1834
1852
  const sessionId = await client.createSession({
1835
1853
  name: `generateText: ${input.slice(0, 50)}${input.length > 50 ? "..." : ""}`,
1836
1854
  agentName: config.defaultAgent ?? "vercel-ai-sdk",
1837
- userId: config.userId ?? "anonymous",
1838
- convoId: config.convoId,
1855
+ userId: resolveStringOrFn(config.userId, "anonymous"),
1856
+ convoId: resolveStringOrFn(config.convoId),
1839
1857
  metadata: {
1840
1858
  model: modelId,
1841
1859
  provider,
@@ -1935,6 +1953,7 @@ function wrapStreamText(originalFn, client, config) {
1935
1953
  const { modelId, provider } = extractModelInfo(params.model);
1936
1954
  const input = extractInput(params);
1937
1955
  let sessionId = null;
1956
+ let sessionCompleted = false;
1938
1957
  let resolveSessionReady;
1939
1958
  const sessionReady = new Promise((resolve) => {
1940
1959
  resolveSessionReady = resolve;
@@ -1944,8 +1963,8 @@ function wrapStreamText(originalFn, client, config) {
1944
1963
  const id = await client.createSession({
1945
1964
  name: `streamText: ${input.slice(0, 50)}${input.length > 50 ? "..." : ""}`,
1946
1965
  agentName: config.defaultAgent ?? "vercel-ai-sdk",
1947
- userId: config.userId ?? "anonymous",
1948
- convoId: config.convoId,
1966
+ userId: resolveStringOrFn(config.userId, "anonymous"),
1967
+ convoId: resolveStringOrFn(config.convoId),
1949
1968
  metadata: {
1950
1969
  model: modelId,
1951
1970
  provider,
@@ -2001,7 +2020,8 @@ function wrapStreamText(originalFn, client, config) {
2001
2020
  },
2002
2021
  onFinish: async (event) => {
2003
2022
  await sessionReady;
2004
- if (sessionId) {
2023
+ if (sessionId && !sessionCompleted) {
2024
+ sessionCompleted = true;
2005
2025
  const durationMs = Date.now() - startTime;
2006
2026
  const usage = normalizeUsage(event.totalUsage ?? event.usage);
2007
2027
  const resolvedModelId = event.response?.modelId ?? modelId;
@@ -2031,7 +2051,8 @@ function wrapStreamText(originalFn, client, config) {
2031
2051
  },
2032
2052
  onError: async (event) => {
2033
2053
  await sessionReady;
2034
- if (sessionId) {
2054
+ if (sessionId && !sessionCompleted) {
2055
+ sessionCompleted = true;
2035
2056
  const durationMs = Date.now() - startTime;
2036
2057
  const msg = event.error?.message ?? "Unknown error";
2037
2058
  await client.trackError({
@@ -2081,8 +2102,8 @@ function wrapGenerateObject(originalFn, client, config) {
2081
2102
  const sessionId = await client.createSession({
2082
2103
  name: `generateObject: ${input.slice(0, 50)}${input.length > 50 ? "..." : ""}`,
2083
2104
  agentName: config.defaultAgent ?? "vercel-ai-sdk",
2084
- userId: config.userId ?? "anonymous",
2085
- convoId: config.convoId,
2105
+ userId: resolveStringOrFn(config.userId, "anonymous"),
2106
+ convoId: resolveStringOrFn(config.convoId),
2086
2107
  metadata: {
2087
2108
  model: modelId,
2088
2109
  provider,
@@ -2157,8 +2178,8 @@ function wrapStreamObject(originalFn, client, config) {
2157
2178
  const id = await client.createSession({
2158
2179
  name: `streamObject: ${input.slice(0, 50)}${input.length > 50 ? "..." : ""}`,
2159
2180
  agentName: config.defaultAgent ?? "vercel-ai-sdk",
2160
- userId: config.userId ?? "anonymous",
2161
- convoId: config.convoId,
2181
+ userId: resolveStringOrFn(config.userId, "anonymous"),
2182
+ convoId: resolveStringOrFn(config.convoId),
2162
2183
  metadata: {
2163
2184
  model: modelId,
2164
2185
  provider,