@langinsight/ai-sdk 0.0.2 → 0.0.3

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/README.md CHANGED
@@ -17,6 +17,10 @@ const handler = new LangInsight.CallbackHandler({
17
17
  apiKey: LANGINSIGHT_API_KEY,
18
18
  endpoint: LANGINSIGHT_ENDPOINT,
19
19
  metadata: { userId: "admin", sessionId: "admin" },
20
+ onSuccess: (trace) => {
21
+ // traceId の取得・DB永続化はここで
22
+ console.log("Trace sent:", trace.id);
23
+ },
20
24
  });
21
25
 
22
26
  const prompt = "Hello!";
@@ -26,8 +30,7 @@ const result = await generateText({
26
30
  prompt,
27
31
  });
28
32
 
29
- // Report the result to LangInsight (returns output trace id or undefined)
30
- const traceId = await handler.report(result, { input: prompt });
33
+ await handler.report(result, { input: prompt });
31
34
  ```
32
35
 
33
36
  ### streamText
@@ -62,15 +65,21 @@ for await (const chunk of result.textStream) {
62
65
 
63
66
  ### Feedback (score)
64
67
 
65
- After sending traces, you can submit feedback for the last output trace using `score()` or the trace id from `onSuccess` / `report()`.
68
+ Trace ID の取得や DB 永続化は `onSuccess` コールバックで受け取った `trace` を使って行い、その `trace.id` `score()` に渡してフィードバックを送信する。
66
69
 
67
70
  ```typescript
68
- // Using lastTraceId (set after report() or onFinish)
69
- await handler.score({ traceId: handler.lastTraceId, value: 1 }); // 👍
70
- await handler.score({ traceId: handler.lastTraceId, value: -1 }); // 👎
71
+ let lastOutputTraceId: string | undefined;
71
72
 
72
- // Or using the trace id from report() / onSuccess
73
- await handler.score({ traceId, value: 1 });
73
+ const handler = new LangInsight.CallbackHandler({
74
+ ...options,
75
+ onSuccess: (trace) => {
76
+ lastOutputTraceId = trace.id; // DB 永続化などもここで
77
+ },
78
+ });
79
+
80
+ // report() または onFinish の後
81
+ await handler.score({ traceId: lastOutputTraceId!, value: 1 }); // 👍
82
+ await handler.score({ traceId: lastOutputTraceId!, value: -1 }); // 👎
74
83
  ```
75
84
 
76
85
  ## Options
@@ -84,12 +93,11 @@ await handler.score({ traceId, value: 1 });
84
93
  | `metadata.sessionId`| `string` | Yes | Session ID (e.g. conversation id) |
85
94
  | `metadata.modelName` | `string` | No | Model name (defaults to result response when not set) |
86
95
  | `metadata.*` | `any` | No | Additional key-value pairs |
87
- | `onSuccess` | `(trace: Trace) => void` | No | Callback invoked with the output trace when trace submission succeeds |
96
+ | `onSuccess` | `(trace: Trace) => void` | No | Callback invoked with the output trace when trace submission succeeds. Use this to get `trace.id` and persist to DB. |
88
97
  | `onFailure` | `(error: Error) => void` | No | Callback invoked with the error when trace submission fails |
89
98
 
90
99
  ## API
91
100
 
92
- - **`report(result, { input })`** — Sends traces for a `generateText` result. Returns the output trace id, or `undefined` on failure.
93
- - **`onFinish({ input })`** — Returns a function suitable for `streamText`’s `onFinish`. Sends traces when the stream finishes.
94
- - **`score({ traceId, value })`** — Submits feedback (1 = thumbs up, -1 = thumbs down) for the given trace.
95
- - **`lastTraceId`** — The id of the last output trace sent by this handler (set after `report()` or `onFinish()`).
101
+ - **`report(result, { input })`** — Sends traces for a `generateText` result. Trace is passed to `onSuccess` on success.
102
+ - **`onFinish({ input })`** — Returns a function suitable for `streamText`’s `onFinish`. Sends traces when the stream finishes; trace is passed to `onSuccess`.
103
+ - **`score({ traceId, value })`** — Submits feedback (1 = thumbs up, -1 = thumbs down) for the given trace. Use the `trace.id` received in `onSuccess` as `traceId`.
package/dist/index.cjs CHANGED
@@ -323,8 +323,6 @@ var LangInsight;
323
323
  this.client = hc(options.endpoint, { fetch });
324
324
  }
325
325
  client;
326
- /** 最後に送信した output trace の ID */
327
- lastTraceId;
328
326
  /**
329
327
  * streamText の onFinish コールバックとして使用
330
328
  *
@@ -349,35 +347,36 @@ var LangInsight;
349
347
  }
350
348
  /**
351
349
  * generateText の結果を送信
352
- *
353
- * @returns output trace の traceId(送信失敗時は undefined)
350
+ * traceId の取得・DB永続化は onSuccess コールバックで trace を受け取り行う。
354
351
  *
355
352
  * @example
356
353
  * ```ts
357
- * const handler = new LangInsight.CallbackHandler(options);
358
- *
359
- * const result = await generateText({
360
- * model: openai("gpt-4o"),
361
- * prompt: "Hello!",
354
+ * const handler = new LangInsight.CallbackHandler({
355
+ * ...options,
356
+ * onSuccess: (trace) => {
357
+ * saveTraceToDb(trace); // traceId = trace.id
358
+ * },
362
359
  * });
363
- *
364
- * const traceId = await handler.report(result, { input: "Hello!" });
360
+ * const result = await generateText({ model: openai("gpt-4o"), prompt: "Hello!" });
361
+ * await handler.report(result, { input: "Hello!" });
365
362
  * ```
366
363
  */
367
364
  // biome-ignore lint/suspicious/noExplicitAny: should accept any output
368
365
  async report(result, params) {
369
- return await this.sendTraces({
366
+ await this.sendTraces({
370
367
  input: params.input,
371
368
  result
372
369
  });
373
370
  }
374
371
  /**
375
372
  * フィードバック(👍/👎)を送信
373
+ * traceId は onSuccess コールバックで受け取った trace.id を使用する。
376
374
  *
377
375
  * @example
378
376
  * ```ts
379
- * await handler.score({ traceId, value: 1 }); // 👍
380
- * await handler.score({ traceId, value: -1 }); // 👎
377
+ * onSuccess: (trace) => { lastOutputTraceId = trace.id; },
378
+ * // ...
379
+ * await handler.score({ traceId: lastOutputTraceId, value: 1 }); // 👍
381
380
  * ```
382
381
  */
383
382
  async score(params) {
@@ -443,14 +442,10 @@ var LangInsight;
443
442
  throw new Error("Failed to send traces");
444
443
  }
445
444
  const outputTrace = await res[1].json();
446
- const traceId = outputTrace.id;
447
- this.lastTraceId = traceId;
448
445
  this.options.onSuccess?.(outputTrace);
449
- return traceId;
450
446
  } catch (error) {
451
447
  console.error(error);
452
448
  this.options.onFailure?.(error instanceof Error ? error : new Error(String(error)));
453
- return void 0;
454
449
  }
455
450
  }
456
451
  }
package/dist/index.d.cts CHANGED
@@ -129,8 +129,6 @@ declare namespace LangInsight {
129
129
  class CallbackHandler {
130
130
  private readonly options;
131
131
  private readonly client;
132
- /** 最後に送信した output trace の ID */
133
- lastTraceId: string | undefined;
134
132
  constructor(options: Options);
135
133
  /**
136
134
  * streamText の onFinish コールバックとして使用
@@ -154,31 +152,32 @@ declare namespace LangInsight {
154
152
  }) => Promise<void>;
155
153
  /**
156
154
  * generateText の結果を送信
157
- *
158
- * @returns output trace の traceId(送信失敗時は undefined)
155
+ * traceId の取得・DB永続化は onSuccess コールバックで trace を受け取り行う。
159
156
  *
160
157
  * @example
161
158
  * ```ts
162
- * const handler = new LangInsight.CallbackHandler(options);
163
- *
164
- * const result = await generateText({
165
- * model: openai("gpt-4o"),
166
- * prompt: "Hello!",
159
+ * const handler = new LangInsight.CallbackHandler({
160
+ * ...options,
161
+ * onSuccess: (trace) => {
162
+ * saveTraceToDb(trace); // traceId = trace.id
163
+ * },
167
164
  * });
168
- *
169
- * const traceId = await handler.report(result, { input: "Hello!" });
165
+ * const result = await generateText({ model: openai("gpt-4o"), prompt: "Hello!" });
166
+ * await handler.report(result, { input: "Hello!" });
170
167
  * ```
171
168
  */
172
169
  report(result: GenerateTextResult<ToolSet, any>, params: {
173
170
  input: string;
174
- }): Promise<string | undefined>;
171
+ }): Promise<void>;
175
172
  /**
176
173
  * フィードバック(👍/👎)を送信
174
+ * traceId は onSuccess コールバックで受け取った trace.id を使用する。
177
175
  *
178
176
  * @example
179
177
  * ```ts
180
- * await handler.score({ traceId, value: 1 }); // 👍
181
- * await handler.score({ traceId, value: -1 }); // 👎
178
+ * onSuccess: (trace) => { lastOutputTraceId = trace.id; },
179
+ * // ...
180
+ * await handler.score({ traceId: lastOutputTraceId, value: 1 }); // 👍
182
181
  * ```
183
182
  */
184
183
  score(params: {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@langinsight/ai-sdk",
3
3
  "module": "dist/index.js",
4
4
  "types": "dist/index.d.ts",
5
- "version": "0.0.2",
5
+ "version": "0.0.3",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/bun": "1.3.8",
26
- "ai": "^6.0.73",
26
+ "ai": "^6.0.77",
27
27
  "tsup": "^8.5.1",
28
28
  "typescript": "^5.9.3"
29
29
  }