Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

logger.ts

47 lines1.4 KB

1import type { DebugCategories, Logger } from '@tanstack/ai'
2​
3/**
4 * `logger`, but a method that throws, or whose promise rejects, does
5 * nothing. A broken logger must not stop the harness.
6 */
7export function safeLogger(logger: Logger) {
8 const guard = (call: () => unknown) => {
9 try {
10 const result = call()
11 // An async logger can reject. Unhandled, that can stop the process.
12 if (result instanceof Promise) result.catch(() => {})
13 } catch {
14 // ponytail: the line is lost. There is no safe place to report it.
15 }
16 }
17 return {
18 debug: (...args: Parameters<Logger['debug']>) =>
19 guard(() => logger.debug(...args)),
20 info: (...args: Parameters<Logger['info']>) =>
21 guard(() => logger.info(...args)),
22 warn: (...args: Parameters<Logger['warn']>) =>
23 guard(() => logger.warn(...args)),
24 error: (...args: Parameters<Logger['error']>) =>
25 guard(() => logger.error(...args)),
26 }
28​
29/**
30 * The `debug` option of the chat() calls of the harness: only the `errors`
31 * category, to `logger`. Each category has a value, so a new category of
32 * `@tanstack/ai` is a type error here.
33 */
34export function errorsOnlyDebug(logger: Logger) {
35 return {
36 logger,
37 errors: true,
38 provider: false,
39 output: false,
40 middleware: false,
41 tools: false,
42 agentLoop: false,
43 config: false,
44 request: false,
45 sandbox: false,
46 } satisfies Required<DebugCategories> & { logger: Logger }