weflayr 0.3.2 → 0.5.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.
package/index.d.ts CHANGED
@@ -36,6 +36,12 @@ export interface MethodConfig {
36
36
  call: string;
37
37
  middleware?: MiddlewareFn;
38
38
  streamMiddleware?: StreamMiddlewareFn;
39
+ /**
40
+ * Property name on the response object that holds the async iterable stream.
41
+ * Use when the SDK returns `{ [streamPath]: AsyncIterable }` instead of a direct AsyncIterable.
42
+ * e.g. `'stream'` for AWS Bedrock's `ConverseStreamCommand`.
43
+ */
44
+ streamPath?: string;
39
45
  }
40
46
 
41
47
  /** Full configuration object passed to `weflayr_setup`. */
package/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "name": "weflayr",
3
- "version": "0.3.2",
3
+ "version": "0.5.1",
4
4
  "description": "Weflayr Node.js SDK — instrument any LLM client via JS Proxy",
5
5
  "main": "src/index.js",
6
6
  "types": "index.d.ts",
7
7
  "scripts": {
8
8
  "test": "node --test tests/index.test.js"
9
9
  },
10
- "keywords": ["llm", "observability", "weflayr"],
10
+ "keywords": [
11
+ "llm",
12
+ "observability",
13
+ "weflayr"
14
+ ],
11
15
  "author": "Weflayr <contact@weflayr.com>",
12
16
  "license": "Elastic-2.0",
13
17
  "engines": {
package/src/instrument.js CHANGED
@@ -87,6 +87,12 @@ function _wrapFn(fn, methodName, methodConfig) {
87
87
  return _wrapStream(result, methodName, tags, requestArgs, settings, startTime, methodConfig, eventId);
88
88
  }
89
89
 
90
+ if (methodConfig.streamPath && result && _isAsyncIterable(result[methodConfig.streamPath])) {
91
+ _sendEvent('stream_start', { event_id: eventId, method: methodName, tags });
92
+ const wrappedStream = _wrapStream(result[methodConfig.streamPath], methodName, tags, requestArgs, settings, startTime, methodConfig, eventId);
93
+ return { ...result, [methodConfig.streamPath]: wrappedStream };
94
+ }
95
+
90
96
  const middlewareData = methodConfig.middleware
91
97
  ? methodConfig.middleware(requestArgs, result) || {}
92
98
  : {};
@@ -166,14 +172,23 @@ async function* _wrapStream(stream, methodName, tags, requestArgs, settings, sta
166
172
 
167
173
  function _extractTags(args) {
168
174
  let tags = {};
169
- const cleanArgs = args.map(arg => {
170
- if (arg && typeof arg === 'object' && !Array.isArray(arg) && '__weflayr_tags' in arg) {
171
- const { __weflayr_tags, ...rest } = arg;
172
- tags = __weflayr_tags || {};
173
- return rest;
175
+
176
+ function strip(obj) {
177
+ if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return;
178
+ if ('__weflayr_tags' in obj) {
179
+ tags = obj.__weflayr_tags || {};
180
+ delete obj.__weflayr_tags;
174
181
  }
182
+ for (const key of Object.keys(obj)) {
183
+ strip(obj[key]);
184
+ }
185
+ }
186
+
187
+ const cleanArgs = args.map(arg => {
188
+ if (arg && typeof arg === 'object' && !Array.isArray(arg)) strip(arg);
175
189
  return arg;
176
190
  });
191
+
177
192
  return { tags, cleanArgs };
178
193
  }
179
194
 
@@ -0,0 +1,6 @@
1
+ import { MethodConfig, StreamAccumulator } from '../../../index';
2
+
3
+ export function createStreamAccumulator(): StreamAccumulator;
4
+
5
+ /** Ready-to-use method config for `client.send(new ConverseStreamCommand(...))`. */
6
+ export const converseStream: MethodConfig;
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ function createStreamAccumulator() {
4
+ let input_tokens = null;
5
+ let output_tokens = null;
6
+
7
+ return {
8
+ onChunk(chunk) {
9
+ if (chunk.metadata?.usage) {
10
+ input_tokens = chunk.metadata.usage.inputTokens ?? null;
11
+ output_tokens = chunk.metadata.usage.outputTokens ?? null;
12
+ return true;
13
+ }
14
+ return false;
15
+ },
16
+ finalize() {
17
+ return { input_tokens, output_tokens };
18
+ },
19
+ };
20
+ }
21
+
22
+ const converseStream = {
23
+ call: 'send',
24
+ streamPath: 'stream',
25
+ streamMiddleware: createStreamAccumulator,
26
+ };
27
+
28
+ module.exports = { converseStream, createStreamAccumulator };