@rudderjs/ai 1.6.0 → 1.6.2

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.
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Detect an async generator (the value returned by `async function*` or any
3
+ * object implementing the AsyncGenerator protocol). We use a structural check
4
+ * because the executor may not be authored as a literal `async function*`
5
+ * (e.g. wrapped or returned from a factory).
6
+ */
7
+ export function isAsyncGenerator(value) {
8
+ if (value === null || typeof value !== 'object')
9
+ return false;
10
+ const v = value;
11
+ return typeof v.next === 'function'
12
+ && typeof v.return === 'function'
13
+ && typeof v[Symbol.asyncIterator] === 'function';
14
+ }
15
+ /**
16
+ * Uniformly iterate a tool's `execute`, whether it returns a value, a
17
+ * promise, or an async generator.
18
+ *
19
+ * The helper is itself an async generator: each `yield` is a preliminary
20
+ * tool-update payload (only generator-style executes produce these), and the
21
+ * generator's `return` value is the final tool result.
22
+ *
23
+ * Streaming callers iterate and emit `tool-update` chunks live as updates
24
+ * arrive. Non-streaming callers iterate and discard yields, capturing only
25
+ * the final return value — same tool definition works in both modes.
26
+ */
27
+ export async function* executeMaybeStreaming(tool, args, ctx) {
28
+ const execute = tool.execute;
29
+ if (!execute) {
30
+ throw new Error('Tool has no execute function');
31
+ }
32
+ const ret = execute(args, ctx);
33
+ if (isAsyncGenerator(ret)) {
34
+ while (true) {
35
+ const step = await ret.next();
36
+ if (step.done)
37
+ return step.value;
38
+ yield step.value;
39
+ }
40
+ }
41
+ return await ret;
42
+ }
43
+ /**
44
+ * Validate a tool call's arguments against the tool's `inputSchema`. On
45
+ * success, the parsed value is returned — zod transforms (`.transform`,
46
+ * `.default`, type coercion) are applied, so `execute` receives the
47
+ * canonical shape the schema describes. On failure, a structured error
48
+ * suitable for feeding back to the model is returned.
49
+ */
50
+ export function validateToolArgs(tool, args) {
51
+ const parsed = tool.definition.inputSchema.safeParse(args);
52
+ if (parsed.success) {
53
+ return { ok: true, value: parsed.data };
54
+ }
55
+ return {
56
+ ok: false,
57
+ error: {
58
+ error: 'invalid_arguments',
59
+ message: `Tool "${tool.definition.name}" received arguments that did not match its inputSchema.`,
60
+ issues: parsed.error.issues.map(i => ({
61
+ path: i.path.map(seg => String(seg)).join('.'),
62
+ message: i.message,
63
+ })),
64
+ },
65
+ };
66
+ }
67
+ /**
68
+ * Default stringification used for the `tool` role message content when a
69
+ * tool has no `toModelOutput` transform: pass through strings, JSON-encode
70
+ * everything else.
71
+ */
72
+ export function defaultStringify(value) {
73
+ return typeof value === 'string' ? value : JSON.stringify(value);
74
+ }
75
+ /**
76
+ * Convert a tool's structured `result` into the string the **model** will
77
+ * see on its next step. Honors `tool.toModelOutput` when present, falling
78
+ * back to {@link defaultStringify}.
79
+ *
80
+ * Per R6 in the ai-loop-parity plan: a throwing `toModelOutput` MUST NOT
81
+ * crash the loop. We swallow the error, route it through `onError`
82
+ * middleware so it stays observable, and use the default stringification
83
+ * as a safety net.
84
+ */
85
+ export async function applyToModelOutput(tool, result, onError) {
86
+ if (tool.toModelOutput) {
87
+ try {
88
+ return await tool.toModelOutput(result);
89
+ }
90
+ catch (err) {
91
+ if (onError)
92
+ await onError(err);
93
+ }
94
+ }
95
+ return defaultStringify(result);
96
+ }
97
+ /**
98
+ * Resolve `needsApproval` for a tool call, taking into account the
99
+ * client-supplied `approvedToolCallIds` / `rejectedToolCallIds` lists.
100
+ *
101
+ * Returns:
102
+ * - `'allow'` — execute the tool normally (default; also when approved)
103
+ * - `'pending'` — needsApproval is truthy and the call has not been approved
104
+ * - `'rejected'` — the call appears in `rejectedToolCallIds`
105
+ */
106
+ export async function evaluateApproval(tool, tc, options) {
107
+ const needs = tool.definition.needsApproval;
108
+ const requires = typeof needs === 'function' ? await needs(tc.arguments) : !!needs;
109
+ if (!requires)
110
+ return 'allow';
111
+ if (options?.rejectedToolCallIds?.includes(tc.id))
112
+ return 'rejected';
113
+ if (options?.approvedToolCallIds?.includes(tc.id))
114
+ return 'allow';
115
+ return 'pending';
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;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,IAAU,EACV,EAAY,EACZ,OAAuC;IAEvC,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,aAAa,CAAA;IAC3C,MAAM,QAAQ,GAAG,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;IAClF,IAAI,CAAC,QAAQ;QAAE,OAAO,OAAO,CAAA;IAE7B,IAAI,OAAO,EAAE,mBAAmB,EAAE,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;QAAE,OAAO,UAAU,CAAA;IACpE,IAAI,OAAO,EAAE,mBAAmB,EAAE,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;QAAE,OAAO,OAAO,CAAA;IACjE,OAAO,SAAS,CAAA;AAClB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rudderjs/ai",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "description": "AI engine — providers, agents, tools, streaming, middleware",
5
5
  "rudderjs": {
6
6
  "provider": "AiProvider",
@@ -76,7 +76,7 @@
76
76
  "peerDependencies": {
77
77
  "@modelcontextprotocol/sdk": "^1.13.0",
78
78
  "@rudderjs/core": "^1.1.3",
79
- "@rudderjs/orm": "^1.9.0"
79
+ "@rudderjs/orm": "^1.9.1"
80
80
  },
81
81
  "peerDependenciesMeta": {
82
82
  "@rudderjs/core": {
@@ -102,7 +102,7 @@
102
102
  "typescript": "^5.4.0",
103
103
  "@rudderjs/core": "^1.1.3",
104
104
  "@rudderjs/console": "^1.0.1",
105
- "@rudderjs/orm": "^1.9.0"
105
+ "@rudderjs/orm": "^1.9.1"
106
106
  },
107
107
  "author": "Suleiman Shahbari",
108
108
  "scripts": {