@langinsight/ai-sdk 0.0.2 → 0.0.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/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: (input, output) => {
21
+ // traceId の取得・DB永続化はここで
22
+ console.log("Trace sent:", output.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
@@ -43,7 +46,7 @@ const handler = new LangInsight.CallbackHandler({
43
46
  apiKey: LANGINSIGHT_API_KEY,
44
47
  endpoint: LANGINSIGHT_ENDPOINT,
45
48
  metadata: { userId: "admin", sessionId: "admin" },
46
- onSuccess: (trace) => console.log("Trace sent:", trace.id),
49
+ onSuccess: (input, output) => console.log("Trace sent:", output.id),
47
50
  onFailure: (err) => console.error("Trace failed:", err),
48
51
  });
49
52
 
@@ -62,15 +65,25 @@ 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` コールバックで受け取った `output` を使って行い、その `output.id` `score()` に渡してフィードバックを送信する。`handler.result` await することでも取得可能。
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: (input, output) => {
76
+ lastOutputTraceId = output.id; // DB 永続化などもここで
77
+ },
78
+ });
79
+
80
+ // report() または onFinish の後
81
+ await handler.score({ traceId: lastOutputTraceId!, value: 1 }); // 👍
82
+ await handler.score({ traceId: lastOutputTraceId!, value: -1 }); // 👎
83
+
84
+ // または handler.result を使用
85
+ const { input, output } = await handler.result;
86
+ console.log("Input trace:", input.id, "Output trace:", output.id);
74
87
  ```
75
88
 
76
89
  ## Options
@@ -84,12 +97,12 @@ await handler.score({ traceId, value: 1 });
84
97
  | `metadata.sessionId`| `string` | Yes | Session ID (e.g. conversation id) |
85
98
  | `metadata.modelName` | `string` | No | Model name (defaults to result response when not set) |
86
99
  | `metadata.*` | `any` | No | Additional key-value pairs |
87
- | `onSuccess` | `(trace: Trace) => void` | No | Callback invoked with the output trace when trace submission succeeds |
100
+ | `onSuccess` | `(input: Trace, output: Trace) => void` | No | Callback invoked with both input and output traces when trace submission succeeds. Use `output.id` to get traceId and persist to DB. |
88
101
  | `onFailure` | `(error: Error) => void` | No | Callback invoked with the error when trace submission fails |
89
102
 
90
103
  ## API
91
104
 
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()`).
105
+ - **`report(result, { input })`** — Sends traces for a `generateText` result. Both input and output traces are passed to `onSuccess` on success.
106
+ - **`onFinish({ input })`** — Returns a function suitable for `streamText`'s `onFinish`. Sends traces when the stream finishes; both traces are passed to `onSuccess`.
107
+ - **`score({ traceId, value })`** — Submits feedback (1 = thumbs up, -1 = thumbs down) for the given trace. Use the `output.id` received in `onSuccess` as `traceId`.
108
+ - **`result`** — A promise that resolves with `{ input: Trace, output: Trace }` when traces are sent successfully.
package/dist/index.cjs CHANGED
@@ -321,10 +321,15 @@ var LangInsight;
321
321
  constructor(options) {
322
322
  this.options = options;
323
323
  this.client = hc(options.endpoint, { fetch });
324
+ this.result = new Promise((resolve, reject) => {
325
+ this.resolve = resolve;
326
+ this.reject = reject;
327
+ });
324
328
  }
325
329
  client;
326
- /** 最後に送信した output trace の ID */
327
- lastTraceId;
330
+ resolve = null;
331
+ reject = null;
332
+ result;
328
333
  /**
329
334
  * streamText の onFinish コールバックとして使用
330
335
  *
@@ -349,35 +354,36 @@ var LangInsight;
349
354
  }
350
355
  /**
351
356
  * generateText の結果を送信
352
- *
353
- * @returns output trace の traceId(送信失敗時は undefined)
357
+ * traceId の取得・DB永続化は onSuccess コールバックで trace を受け取り行う。
354
358
  *
355
359
  * @example
356
360
  * ```ts
357
- * const handler = new LangInsight.CallbackHandler(options);
358
- *
359
- * const result = await generateText({
360
- * model: openai("gpt-4o"),
361
- * prompt: "Hello!",
361
+ * const handler = new LangInsight.CallbackHandler({
362
+ * ...options,
363
+ * onSuccess: (input, output) => {
364
+ * saveTraceToDb(output); // traceId = output.id
365
+ * },
362
366
  * });
363
- *
364
- * const traceId = await handler.report(result, { input: "Hello!" });
367
+ * const result = await generateText({ model: openai("gpt-4o"), prompt: "Hello!" });
368
+ * await handler.report(result, { input: "Hello!" });
365
369
  * ```
366
370
  */
367
371
  // biome-ignore lint/suspicious/noExplicitAny: should accept any output
368
372
  async report(result, params) {
369
- return await this.sendTraces({
373
+ await this.sendTraces({
370
374
  input: params.input,
371
375
  result
372
376
  });
373
377
  }
374
378
  /**
375
379
  * フィードバック(👍/👎)を送信
380
+ * traceId は onSuccess コールバックで受け取った output.id を使用する。
376
381
  *
377
382
  * @example
378
383
  * ```ts
379
- * await handler.score({ traceId, value: 1 }); // 👍
380
- * await handler.score({ traceId, value: -1 }); // 👎
384
+ * onSuccess: (input, output) => { lastOutputTraceId = output.id; },
385
+ * // ...
386
+ * await handler.score({ traceId: lastOutputTraceId, value: 1 }); // 👍
381
387
  * ```
382
388
  */
383
389
  async score(params) {
@@ -439,18 +445,19 @@ var LangInsight;
439
445
  })
440
446
  ]);
441
447
  if (!res.every((r) => r.ok)) {
442
- console.error(await Promise.all(res.map((r) => r.text())));
448
+ const [a, b] = await Promise.all(res.map((r) => r.text()));
449
+ console.error(a, b);
443
450
  throw new Error("Failed to send traces");
444
451
  }
452
+ const inputTrace = await res[0].json();
445
453
  const outputTrace = await res[1].json();
446
- const traceId = outputTrace.id;
447
- this.lastTraceId = traceId;
448
- this.options.onSuccess?.(outputTrace);
449
- return traceId;
454
+ this.resolve?.({ input: inputTrace, output: outputTrace });
455
+ this.options.onSuccess?.(inputTrace, outputTrace);
450
456
  } catch (error) {
451
- console.error(error);
452
- this.options.onFailure?.(error instanceof Error ? error : new Error(String(error)));
453
- return void 0;
457
+ const err = error instanceof Error ? error : new Error(String(error));
458
+ this.reject?.(err);
459
+ console.error(err);
460
+ this.options.onFailure?.(err);
454
461
  }
455
462
  }
456
463
  }
package/dist/index.d.cts CHANGED
@@ -123,14 +123,18 @@ declare namespace LangInsight {
123
123
  sessionId: string;
124
124
  [key: string]: any;
125
125
  };
126
- onSuccess?: (trace: TraceEntity) => void;
126
+ onSuccess?: (input: TraceEntity, output: TraceEntity) => void;
127
127
  onFailure?: (error: Error) => void;
128
128
  }
129
129
  class CallbackHandler {
130
130
  private readonly options;
131
131
  private readonly client;
132
- /** 最後に送信した output trace の ID */
133
- lastTraceId: string | undefined;
132
+ private resolve;
133
+ private reject;
134
+ readonly result: Promise<{
135
+ input: TraceEntity;
136
+ output: TraceEntity;
137
+ }>;
134
138
  constructor(options: Options);
135
139
  /**
136
140
  * streamText の onFinish コールバックとして使用
@@ -154,31 +158,32 @@ declare namespace LangInsight {
154
158
  }) => Promise<void>;
155
159
  /**
156
160
  * generateText の結果を送信
157
- *
158
- * @returns output trace の traceId(送信失敗時は undefined)
161
+ * traceId の取得・DB永続化は onSuccess コールバックで trace を受け取り行う。
159
162
  *
160
163
  * @example
161
164
  * ```ts
162
- * const handler = new LangInsight.CallbackHandler(options);
163
- *
164
- * const result = await generateText({
165
- * model: openai("gpt-4o"),
166
- * prompt: "Hello!",
165
+ * const handler = new LangInsight.CallbackHandler({
166
+ * ...options,
167
+ * onSuccess: (input, output) => {
168
+ * saveTraceToDb(output); // traceId = output.id
169
+ * },
167
170
  * });
168
- *
169
- * const traceId = await handler.report(result, { input: "Hello!" });
171
+ * const result = await generateText({ model: openai("gpt-4o"), prompt: "Hello!" });
172
+ * await handler.report(result, { input: "Hello!" });
170
173
  * ```
171
174
  */
172
175
  report(result: GenerateTextResult<ToolSet, any>, params: {
173
176
  input: string;
174
- }): Promise<string | undefined>;
177
+ }): Promise<void>;
175
178
  /**
176
179
  * フィードバック(👍/👎)を送信
180
+ * traceId は onSuccess コールバックで受け取った output.id を使用する。
177
181
  *
178
182
  * @example
179
183
  * ```ts
180
- * await handler.score({ traceId, value: 1 }); // 👍
181
- * await handler.score({ traceId, value: -1 }); // 👎
184
+ * onSuccess: (input, output) => { lastOutputTraceId = output.id; },
185
+ * // ...
186
+ * await handler.score({ traceId: lastOutputTraceId, value: 1 }); // 👍
182
187
  * ```
183
188
  */
184
189
  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.4",
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
  }