@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.d.cts CHANGED
@@ -1187,11 +1187,13 @@ type StreamTextResult = {
1187
1187
  }>;
1188
1188
  [key: string]: unknown;
1189
1189
  };
1190
+ /** String or function that returns a string (for dynamic per-request values) */
1191
+ type StringOrFn = string | (() => string | undefined);
1190
1192
  /** Per-instance config passed through closures (not global) */
1191
1193
  type WrapperConfig = {
1192
1194
  defaultAgent?: string;
1193
- userId?: string;
1194
- convoId?: string;
1195
+ userId?: StringOrFn;
1196
+ convoId?: StringOrFn;
1195
1197
  };
1196
1198
  /**
1197
1199
  * Configure the Sentrial SDK for Vercel AI SDK integration.
@@ -1211,8 +1213,8 @@ declare function configureVercel(config: {
1211
1213
  apiKey?: string;
1212
1214
  apiUrl?: string;
1213
1215
  defaultAgent?: string;
1214
- userId?: string;
1215
- convoId?: string;
1216
+ userId?: StringOrFn;
1217
+ convoId?: StringOrFn;
1216
1218
  failSilently?: boolean;
1217
1219
  }): void;
1218
1220
  declare function wrapGenerateText(originalFn: Function, client: SentrialClient, config: WrapperConfig): (params: GenerateTextParams) => Promise<GenerateTextResult>;
@@ -1246,8 +1248,8 @@ declare function wrapStreamObject(originalFn: Function, client: SentrialClient,
1246
1248
  declare function wrapAISDK(ai: AIModule, options?: {
1247
1249
  client?: SentrialClient;
1248
1250
  defaultAgent?: string;
1249
- userId?: string;
1250
- convoId?: string;
1251
+ userId?: StringOrFn;
1252
+ convoId?: StringOrFn;
1251
1253
  }): {
1252
1254
  generateText: ReturnType<typeof wrapGenerateText>;
1253
1255
  streamText: ReturnType<typeof wrapStreamText>;
package/dist/index.d.ts CHANGED
@@ -1187,11 +1187,13 @@ type StreamTextResult = {
1187
1187
  }>;
1188
1188
  [key: string]: unknown;
1189
1189
  };
1190
+ /** String or function that returns a string (for dynamic per-request values) */
1191
+ type StringOrFn = string | (() => string | undefined);
1190
1192
  /** Per-instance config passed through closures (not global) */
1191
1193
  type WrapperConfig = {
1192
1194
  defaultAgent?: string;
1193
- userId?: string;
1194
- convoId?: string;
1195
+ userId?: StringOrFn;
1196
+ convoId?: StringOrFn;
1195
1197
  };
1196
1198
  /**
1197
1199
  * Configure the Sentrial SDK for Vercel AI SDK integration.
@@ -1211,8 +1213,8 @@ declare function configureVercel(config: {
1211
1213
  apiKey?: string;
1212
1214
  apiUrl?: string;
1213
1215
  defaultAgent?: string;
1214
- userId?: string;
1215
- convoId?: string;
1216
+ userId?: StringOrFn;
1217
+ convoId?: StringOrFn;
1216
1218
  failSilently?: boolean;
1217
1219
  }): void;
1218
1220
  declare function wrapGenerateText(originalFn: Function, client: SentrialClient, config: WrapperConfig): (params: GenerateTextParams) => Promise<GenerateTextResult>;
@@ -1246,8 +1248,8 @@ declare function wrapStreamObject(originalFn: Function, client: SentrialClient,
1246
1248
  declare function wrapAISDK(ai: AIModule, options?: {
1247
1249
  client?: SentrialClient;
1248
1250
  defaultAgent?: string;
1249
- userId?: string;
1250
- convoId?: string;
1251
+ userId?: StringOrFn;
1252
+ convoId?: StringOrFn;
1251
1253
  }): {
1252
1254
  generateText: ReturnType<typeof wrapGenerateText>;
1253
1255
  streamText: ReturnType<typeof wrapStreamText>;
package/dist/index.js CHANGED
@@ -1565,6 +1565,7 @@ function getClient() {
1565
1565
  }
1566
1566
  function configure(config) {
1567
1567
  defaultClient = new SentrialClient(config);
1568
+ setDefaultClient(defaultClient);
1568
1569
  }
1569
1570
  function begin(params) {
1570
1571
  return getClient().begin(params);
@@ -1583,6 +1584,10 @@ var sentrial = {
1583
1584
  };
1584
1585
 
1585
1586
  // src/vercel.ts
1587
+ function resolveStringOrFn(value, fallback) {
1588
+ if (typeof value === "function") return value() ?? fallback;
1589
+ return value ?? fallback;
1590
+ }
1586
1591
  var _defaultClient2 = null;
1587
1592
  var _globalConfig = {};
1588
1593
  function configureVercel(config) {
@@ -1591,6 +1596,7 @@ function configureVercel(config) {
1591
1596
  apiUrl: config.apiUrl,
1592
1597
  failSilently: config.failSilently ?? true
1593
1598
  });
1599
+ setDefaultClient(_defaultClient2);
1594
1600
  _globalConfig = {
1595
1601
  defaultAgent: config.defaultAgent,
1596
1602
  userId: config.userId,
@@ -1640,14 +1646,26 @@ function calculateCostForCall(provider, modelId, promptTokens, completionTokens)
1640
1646
  return 0;
1641
1647
  }
1642
1648
  }
1649
+ function extractContentText(content) {
1650
+ if (typeof content === "string") return content;
1651
+ if (Array.isArray(content)) {
1652
+ const textParts = content.filter((part) => part && part.type === "text" && typeof part.text === "string").map((part) => part.text);
1653
+ if (textParts.length > 0) return textParts.join("\n");
1654
+ }
1655
+ return JSON.stringify(content);
1656
+ }
1643
1657
  function extractInput(params) {
1644
1658
  if (params.prompt) return params.prompt;
1645
1659
  if (params.messages && params.messages.length > 0) {
1646
1660
  const lastUserMessage = [...params.messages].reverse().find((m) => m.role === "user");
1647
1661
  if (lastUserMessage) {
1648
- return typeof lastUserMessage.content === "string" ? lastUserMessage.content : JSON.stringify(lastUserMessage.content);
1662
+ return extractContentText(lastUserMessage.content);
1649
1663
  }
1650
- return JSON.stringify(params.messages);
1664
+ const lastNonSystem = [...params.messages].reverse().find((m) => m.role !== "system");
1665
+ if (lastNonSystem) {
1666
+ return extractContentText(lastNonSystem.content);
1667
+ }
1668
+ return "";
1651
1669
  }
1652
1670
  return "";
1653
1671
  }
@@ -1761,8 +1779,8 @@ function wrapGenerateText(originalFn, client, config) {
1761
1779
  const sessionId = await client.createSession({
1762
1780
  name: `generateText: ${input.slice(0, 50)}${input.length > 50 ? "..." : ""}`,
1763
1781
  agentName: config.defaultAgent ?? "vercel-ai-sdk",
1764
- userId: config.userId ?? "anonymous",
1765
- convoId: config.convoId,
1782
+ userId: resolveStringOrFn(config.userId, "anonymous"),
1783
+ convoId: resolveStringOrFn(config.convoId),
1766
1784
  metadata: {
1767
1785
  model: modelId,
1768
1786
  provider,
@@ -1862,6 +1880,7 @@ function wrapStreamText(originalFn, client, config) {
1862
1880
  const { modelId, provider } = extractModelInfo(params.model);
1863
1881
  const input = extractInput(params);
1864
1882
  let sessionId = null;
1883
+ let sessionCompleted = false;
1865
1884
  let resolveSessionReady;
1866
1885
  const sessionReady = new Promise((resolve) => {
1867
1886
  resolveSessionReady = resolve;
@@ -1871,8 +1890,8 @@ function wrapStreamText(originalFn, client, config) {
1871
1890
  const id = await client.createSession({
1872
1891
  name: `streamText: ${input.slice(0, 50)}${input.length > 50 ? "..." : ""}`,
1873
1892
  agentName: config.defaultAgent ?? "vercel-ai-sdk",
1874
- userId: config.userId ?? "anonymous",
1875
- convoId: config.convoId,
1893
+ userId: resolveStringOrFn(config.userId, "anonymous"),
1894
+ convoId: resolveStringOrFn(config.convoId),
1876
1895
  metadata: {
1877
1896
  model: modelId,
1878
1897
  provider,
@@ -1928,7 +1947,8 @@ function wrapStreamText(originalFn, client, config) {
1928
1947
  },
1929
1948
  onFinish: async (event) => {
1930
1949
  await sessionReady;
1931
- if (sessionId) {
1950
+ if (sessionId && !sessionCompleted) {
1951
+ sessionCompleted = true;
1932
1952
  const durationMs = Date.now() - startTime;
1933
1953
  const usage = normalizeUsage(event.totalUsage ?? event.usage);
1934
1954
  const resolvedModelId = event.response?.modelId ?? modelId;
@@ -1958,7 +1978,8 @@ function wrapStreamText(originalFn, client, config) {
1958
1978
  },
1959
1979
  onError: async (event) => {
1960
1980
  await sessionReady;
1961
- if (sessionId) {
1981
+ if (sessionId && !sessionCompleted) {
1982
+ sessionCompleted = true;
1962
1983
  const durationMs = Date.now() - startTime;
1963
1984
  const msg = event.error?.message ?? "Unknown error";
1964
1985
  await client.trackError({
@@ -2008,8 +2029,8 @@ function wrapGenerateObject(originalFn, client, config) {
2008
2029
  const sessionId = await client.createSession({
2009
2030
  name: `generateObject: ${input.slice(0, 50)}${input.length > 50 ? "..." : ""}`,
2010
2031
  agentName: config.defaultAgent ?? "vercel-ai-sdk",
2011
- userId: config.userId ?? "anonymous",
2012
- convoId: config.convoId,
2032
+ userId: resolveStringOrFn(config.userId, "anonymous"),
2033
+ convoId: resolveStringOrFn(config.convoId),
2013
2034
  metadata: {
2014
2035
  model: modelId,
2015
2036
  provider,
@@ -2084,8 +2105,8 @@ function wrapStreamObject(originalFn, client, config) {
2084
2105
  const id = await client.createSession({
2085
2106
  name: `streamObject: ${input.slice(0, 50)}${input.length > 50 ? "..." : ""}`,
2086
2107
  agentName: config.defaultAgent ?? "vercel-ai-sdk",
2087
- userId: config.userId ?? "anonymous",
2088
- convoId: config.convoId,
2108
+ userId: resolveStringOrFn(config.userId, "anonymous"),
2109
+ convoId: resolveStringOrFn(config.convoId),
2089
2110
  metadata: {
2090
2111
  model: modelId,
2091
2112
  provider,