@tanstack/ai 0.35.0 → 0.36.0
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/package.json +2 -2
- package/skills/ai-core/tool-calling/SKILL.md +26 -1
- package/src/activities/chat/index.ts +8 -0
- package/src/activities/chat/tools/lazy-tool-manager.ts +12 -4
- package/src/activities/chat/tools/lazy-tools.ts +33 -0
- package/src/index.ts +5 -0
- package/src/types.ts +23 -1
- package/dist/esm/activities/chat/adapter.d.ts +0 -163
- package/dist/esm/activities/chat/adapter.js +0 -17
- package/dist/esm/activities/chat/adapter.js.map +0 -1
- package/dist/esm/activities/chat/agent-loop-strategies.d.ts +0 -59
- package/dist/esm/activities/chat/agent-loop-strategies.js +0 -23
- package/dist/esm/activities/chat/agent-loop-strategies.js.map +0 -1
- package/dist/esm/activities/chat/index.d.ts +0 -264
- package/dist/esm/activities/chat/index.js +0 -1723
- package/dist/esm/activities/chat/index.js.map +0 -1
- package/dist/esm/activities/chat/mcp/manager.d.ts +0 -25
- package/dist/esm/activities/chat/mcp/manager.js +0 -71
- package/dist/esm/activities/chat/mcp/manager.js.map +0 -1
- package/dist/esm/activities/chat/mcp/types.d.ts +0 -56
- package/dist/esm/activities/chat/messages.d.ts +0 -78
- package/dist/esm/activities/chat/messages.js +0 -374
- package/dist/esm/activities/chat/messages.js.map +0 -1
- package/dist/esm/activities/chat/middleware/builder.d.ts +0 -46
- package/dist/esm/activities/chat/middleware/builder.js +0 -17
- package/dist/esm/activities/chat/middleware/builder.js.map +0 -1
- package/dist/esm/activities/chat/middleware/capabilities.d.ts +0 -93
- package/dist/esm/activities/chat/middleware/capabilities.js +0 -45
- package/dist/esm/activities/chat/middleware/capabilities.js.map +0 -1
- package/dist/esm/activities/chat/middleware/compose.d.ts +0 -87
- package/dist/esm/activities/chat/middleware/compose.js +0 -510
- package/dist/esm/activities/chat/middleware/compose.js.map +0 -1
- package/dist/esm/activities/chat/middleware/define.d.ts +0 -20
- package/dist/esm/activities/chat/middleware/define.js +0 -7
- package/dist/esm/activities/chat/middleware/define.js.map +0 -1
- package/dist/esm/activities/chat/middleware/index.d.ts +0 -10
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.d.ts +0 -89
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js +0 -76
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js.map +0 -1
- package/dist/esm/activities/chat/middleware/types.d.ts +0 -405
- package/dist/esm/activities/chat/middleware/validate.d.ts +0 -19
- package/dist/esm/activities/chat/middleware/validate.js +0 -30
- package/dist/esm/activities/chat/middleware/validate.js.map +0 -1
- package/dist/esm/activities/chat/runtime-context-types.d.ts +0 -43
- package/dist/esm/activities/chat/stream/index.d.ts +0 -11
- package/dist/esm/activities/chat/stream/json-parser.d.ts +0 -38
- package/dist/esm/activities/chat/stream/json-parser.js +0 -28
- package/dist/esm/activities/chat/stream/json-parser.js.map +0 -1
- package/dist/esm/activities/chat/stream/message-updaters.d.ts +0 -81
- package/dist/esm/activities/chat/stream/message-updaters.js +0 -253
- package/dist/esm/activities/chat/stream/message-updaters.js.map +0 -1
- package/dist/esm/activities/chat/stream/processor.d.ts +0 -439
- package/dist/esm/activities/chat/stream/processor.js +0 -1426
- package/dist/esm/activities/chat/stream/processor.js.map +0 -1
- package/dist/esm/activities/chat/stream/strategies.d.ts +0 -43
- package/dist/esm/activities/chat/stream/strategies.js +0 -54
- package/dist/esm/activities/chat/stream/strategies.js.map +0 -1
- package/dist/esm/activities/chat/stream/types.d.ts +0 -90
- package/dist/esm/activities/chat/tools/lazy-tool-manager.d.ts +0 -81
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js +0 -188
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js.map +0 -1
- package/dist/esm/activities/chat/tools/schema-converter.d.ts +0 -140
- package/dist/esm/activities/chat/tools/schema-converter.js +0 -167
- package/dist/esm/activities/chat/tools/schema-converter.js.map +0 -1
- package/dist/esm/activities/chat/tools/tool-calls.d.ts +0 -140
- package/dist/esm/activities/chat/tools/tool-calls.js +0 -512
- package/dist/esm/activities/chat/tools/tool-calls.js.map +0 -1
- package/dist/esm/activities/chat/tools/tool-definition.d.ts +0 -135
- package/dist/esm/activities/chat/tools/tool-definition.js +0 -25
- package/dist/esm/activities/chat/tools/tool-definition.js.map +0 -1
- package/dist/esm/activities/error-payload.d.ts +0 -30
- package/dist/esm/activities/error-payload.js +0 -54
- package/dist/esm/activities/error-payload.js.map +0 -1
- package/dist/esm/activities/generateAudio/adapter.d.ts +0 -62
- package/dist/esm/activities/generateAudio/adapter.js +0 -16
- package/dist/esm/activities/generateAudio/adapter.js.map +0 -1
- package/dist/esm/activities/generateAudio/index.d.ts +0 -85
- package/dist/esm/activities/generateAudio/index.js +0 -114
- package/dist/esm/activities/generateAudio/index.js.map +0 -1
- package/dist/esm/activities/generateImage/adapter.d.ts +0 -78
- package/dist/esm/activities/generateImage/adapter.js +0 -16
- package/dist/esm/activities/generateImage/adapter.js.map +0 -1
- package/dist/esm/activities/generateImage/index.d.ts +0 -134
- package/dist/esm/activities/generateImage/index.js +0 -121
- package/dist/esm/activities/generateImage/index.js.map +0 -1
- package/dist/esm/activities/generateSpeech/adapter.d.ts +0 -62
- package/dist/esm/activities/generateSpeech/adapter.js +0 -16
- package/dist/esm/activities/generateSpeech/adapter.js.map +0 -1
- package/dist/esm/activities/generateSpeech/index.d.ts +0 -96
- package/dist/esm/activities/generateSpeech/index.js +0 -119
- package/dist/esm/activities/generateSpeech/index.js.map +0 -1
- package/dist/esm/activities/generateTranscription/adapter.d.ts +0 -62
- package/dist/esm/activities/generateTranscription/adapter.js +0 -16
- package/dist/esm/activities/generateTranscription/adapter.js.map +0 -1
- package/dist/esm/activities/generateTranscription/index.d.ts +0 -109
- package/dist/esm/activities/generateTranscription/index.js +0 -109
- package/dist/esm/activities/generateTranscription/index.js.map +0 -1
- package/dist/esm/activities/generateVideo/adapter.d.ts +0 -145
- package/dist/esm/activities/generateVideo/adapter.js +0 -30
- package/dist/esm/activities/generateVideo/adapter.js.map +0 -1
- package/dist/esm/activities/generateVideo/index.d.ts +0 -223
- package/dist/esm/activities/generateVideo/index.js +0 -295
- package/dist/esm/activities/generateVideo/index.js.map +0 -1
- package/dist/esm/activities/generateVideo/snap.d.ts +0 -14
- package/dist/esm/activities/generateVideo/snap.js +0 -54
- package/dist/esm/activities/generateVideo/snap.js.map +0 -1
- package/dist/esm/activities/index.d.ts +0 -27
- package/dist/esm/activities/index.js +0 -43
- package/dist/esm/activities/index.js.map +0 -1
- package/dist/esm/activities/middleware/index.d.ts +0 -2
- package/dist/esm/activities/middleware/run.d.ts +0 -20
- package/dist/esm/activities/middleware/run.js +0 -42
- package/dist/esm/activities/middleware/run.js.map +0 -1
- package/dist/esm/activities/middleware/types.d.ts +0 -118
- package/dist/esm/activities/stream-generation-result.d.ts +0 -15
- package/dist/esm/activities/stream-generation-result.js +0 -49
- package/dist/esm/activities/stream-generation-result.js.map +0 -1
- package/dist/esm/activities/summarize/adapter.d.ts +0 -74
- package/dist/esm/activities/summarize/adapter.js +0 -16
- package/dist/esm/activities/summarize/adapter.js.map +0 -1
- package/dist/esm/activities/summarize/chat-stream-summarize.d.ts +0 -45
- package/dist/esm/activities/summarize/chat-stream-summarize.js +0 -212
- package/dist/esm/activities/summarize/chat-stream-summarize.js.map +0 -1
- package/dist/esm/activities/summarize/index.d.ts +0 -108
- package/dist/esm/activities/summarize/index.js +0 -113
- package/dist/esm/activities/summarize/index.js.map +0 -1
- package/dist/esm/adapter-internals.d.ts +0 -5
- package/dist/esm/adapter-internals.js +0 -10
- package/dist/esm/adapter-internals.js.map +0 -1
- package/dist/esm/client.d.ts +0 -44
- package/dist/esm/client.js +0 -67
- package/dist/esm/client.js.map +0 -1
- package/dist/esm/extend-adapter.d.ts +0 -152
- package/dist/esm/extend-adapter.js +0 -21
- package/dist/esm/extend-adapter.js.map +0 -1
- package/dist/esm/index.d.ts +0 -44
- package/dist/esm/index.js +0 -106
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/logger/console-logger.d.ts +0 -29
- package/dist/esm/logger/console-logger.js +0 -83
- package/dist/esm/logger/console-logger.js.map +0 -1
- package/dist/esm/logger/internal-logger.d.ts +0 -41
- package/dist/esm/logger/internal-logger.js +0 -86
- package/dist/esm/logger/internal-logger.js.map +0 -1
- package/dist/esm/logger/resolve.d.ts +0 -14
- package/dist/esm/logger/resolve.js +0 -54
- package/dist/esm/logger/resolve.js.map +0 -1
- package/dist/esm/logger/types.d.ts +0 -75
- package/dist/esm/middlewares/content-guard.d.ts +0 -77
- package/dist/esm/middlewares/content-guard.js +0 -156
- package/dist/esm/middlewares/content-guard.js.map +0 -1
- package/dist/esm/middlewares/index.d.ts +0 -2
- package/dist/esm/middlewares/index.js +0 -7
- package/dist/esm/middlewares/index.js.map +0 -1
- package/dist/esm/middlewares/otel.d.ts +0 -81
- package/dist/esm/middlewares/otel.js +0 -745
- package/dist/esm/middlewares/otel.js.map +0 -1
- package/dist/esm/middlewares/tool-cache.d.ts +0 -1
- package/dist/esm/middlewares/usage-attributes.d.ts +0 -24
- package/dist/esm/middlewares/usage-attributes.js +0 -43
- package/dist/esm/middlewares/usage-attributes.js.map +0 -1
- package/dist/esm/realtime/index.d.ts +0 -28
- package/dist/esm/realtime/index.js +0 -8
- package/dist/esm/realtime/index.js.map +0 -1
- package/dist/esm/realtime/types.d.ts +0 -282
- package/dist/esm/stream-to-response.d.ts +0 -102
- package/dist/esm/stream-to-response.js +0 -121
- package/dist/esm/stream-to-response.js.map +0 -1
- package/dist/esm/strip-to-spec-middleware.d.ts +0 -18
- package/dist/esm/strip-to-spec-middleware.js +0 -20
- package/dist/esm/strip-to-spec-middleware.js.map +0 -1
- package/dist/esm/system-prompts.d.ts +0 -66
- package/dist/esm/system-prompts.js +0 -23
- package/dist/esm/system-prompts.js.map +0 -1
- package/dist/esm/tool-registry.d.ts +0 -81
- package/dist/esm/tool-registry.js +0 -49
- package/dist/esm/tool-registry.js.map +0 -1
- package/dist/esm/tools/provider-tool.d.ts +0 -30
- package/dist/esm/tools/provider-tool.js +0 -7
- package/dist/esm/tools/provider-tool.js.map +0 -1
- package/dist/esm/types.d.ts +0 -1573
- package/dist/esm/utilities/ag-ui-wire.d.ts +0 -44
- package/dist/esm/utilities/ag-ui-wire.js +0 -107
- package/dist/esm/utilities/ag-ui-wire.js.map +0 -1
- package/dist/esm/utilities/chat-params.d.ts +0 -85
- package/dist/esm/utilities/chat-params.js +0 -100
- package/dist/esm/utilities/chat-params.js.map +0 -1
- package/dist/esm/utilities/errors.d.ts +0 -13
- package/dist/esm/utilities/errors.js +0 -22
- package/dist/esm/utilities/errors.js.map +0 -1
- package/dist/esm/utilities/media-prompt.d.ts +0 -35
- package/dist/esm/utilities/media-prompt.js +0 -43
- package/dist/esm/utilities/media-prompt.js.map +0 -1
- package/dist/esm/utilities/numbers.d.ts +0 -8
- package/dist/esm/utilities/numbers.js +0 -12
- package/dist/esm/utilities/numbers.js.map +0 -1
- package/dist/esm/utilities/sampling-keys.d.ts +0 -20
- package/dist/esm/utilities/sampling-keys.js +0 -20
- package/dist/esm/utilities/sampling-keys.js.map +0 -1
- package/dist/esm/utilities/tool-result.d.ts +0 -21
- package/dist/esm/utilities/tool-result.js +0 -37
- package/dist/esm/utilities/tool-result.js.map +0 -1
- package/dist/esm/utilities/usage.d.ts +0 -31
- package/dist/esm/utilities/usage.js +0 -11
- package/dist/esm/utilities/usage.js.map +0 -1
- package/dist/esm/utils.d.ts +0 -17
- package/dist/esm/utils.js +0 -20
- package/dist/esm/utils.js.map +0 -1
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { ConsoleLogger } from "./console-logger.js";
|
|
2
|
-
import { InternalLogger } from "./internal-logger.js";
|
|
3
|
-
const ALL_OFF = {
|
|
4
|
-
provider: false,
|
|
5
|
-
output: false,
|
|
6
|
-
middleware: false,
|
|
7
|
-
tools: false,
|
|
8
|
-
agentLoop: false,
|
|
9
|
-
config: false,
|
|
10
|
-
errors: false,
|
|
11
|
-
request: false
|
|
12
|
-
};
|
|
13
|
-
const ALL_ON = {
|
|
14
|
-
provider: true,
|
|
15
|
-
output: true,
|
|
16
|
-
middleware: true,
|
|
17
|
-
tools: true,
|
|
18
|
-
agentLoop: true,
|
|
19
|
-
config: true,
|
|
20
|
-
errors: true,
|
|
21
|
-
request: true
|
|
22
|
-
};
|
|
23
|
-
const errorsOnlyCategories = () => ({
|
|
24
|
-
...ALL_OFF,
|
|
25
|
-
errors: true
|
|
26
|
-
});
|
|
27
|
-
const resolveCategoriesFromPartial = (partial) => ({
|
|
28
|
-
provider: partial.provider ?? true,
|
|
29
|
-
output: partial.output ?? true,
|
|
30
|
-
middleware: partial.middleware ?? true,
|
|
31
|
-
tools: partial.tools ?? true,
|
|
32
|
-
agentLoop: partial.agentLoop ?? true,
|
|
33
|
-
config: partial.config ?? true,
|
|
34
|
-
errors: partial.errors ?? true,
|
|
35
|
-
request: partial.request ?? true
|
|
36
|
-
});
|
|
37
|
-
function resolveDebugOption(debug) {
|
|
38
|
-
if (debug === void 0) {
|
|
39
|
-
return new InternalLogger(new ConsoleLogger(), errorsOnlyCategories());
|
|
40
|
-
}
|
|
41
|
-
if (debug === true) {
|
|
42
|
-
return new InternalLogger(new ConsoleLogger(), ALL_ON);
|
|
43
|
-
}
|
|
44
|
-
if (debug === false) {
|
|
45
|
-
return new InternalLogger(new ConsoleLogger(), ALL_OFF);
|
|
46
|
-
}
|
|
47
|
-
const { logger, ...cats } = debug;
|
|
48
|
-
const userLogger = logger ?? new ConsoleLogger();
|
|
49
|
-
return new InternalLogger(userLogger, resolveCategoriesFromPartial(cats));
|
|
50
|
-
}
|
|
51
|
-
export {
|
|
52
|
-
resolveDebugOption
|
|
53
|
-
};
|
|
54
|
-
//# sourceMappingURL=resolve.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"resolve.js","sources":["../../../src/logger/resolve.ts"],"sourcesContent":["import { ConsoleLogger } from './console-logger'\nimport { InternalLogger } from './internal-logger'\nimport type { ResolvedCategories } from './internal-logger'\nimport type { DebugCategories, DebugConfig, DebugOption, Logger } from './types'\n\nconst ALL_OFF: ResolvedCategories = {\n provider: false,\n output: false,\n middleware: false,\n tools: false,\n agentLoop: false,\n config: false,\n errors: false,\n request: false,\n}\n\nconst ALL_ON: ResolvedCategories = {\n provider: true,\n output: true,\n middleware: true,\n tools: true,\n agentLoop: true,\n config: true,\n errors: true,\n request: true,\n}\n\nconst errorsOnlyCategories = (): ResolvedCategories => ({\n ...ALL_OFF,\n errors: true,\n})\n\nconst resolveCategoriesFromPartial = (\n partial: DebugCategories,\n): ResolvedCategories => ({\n provider: partial.provider ?? true,\n output: partial.output ?? true,\n middleware: partial.middleware ?? true,\n tools: partial.tools ?? true,\n agentLoop: partial.agentLoop ?? true,\n config: partial.config ?? true,\n errors: partial.errors ?? true,\n request: partial.request ?? true,\n})\n\n/**\n * Normalize a `DebugOption` into an `InternalLogger` ready to be threaded\n * through the library's activities and adapters. See the `DebugOption`\n * resolution table in the spec for the complete rules.\n *\n * - `undefined`: only the `errors` category is enabled; default `ConsoleLogger`.\n * - `true`: all categories enabled; default `ConsoleLogger`.\n * - `false`: all categories disabled (including `errors`); default `ConsoleLogger`.\n * - `DebugConfig`: each unspecified category defaults to `true`; an optional\n * `logger` replaces the default `ConsoleLogger`.\n */\nexport function resolveDebugOption(\n debug: DebugOption | undefined,\n): InternalLogger {\n if (debug === undefined) {\n return new InternalLogger(new ConsoleLogger(), errorsOnlyCategories())\n }\n if (debug === true) {\n return new InternalLogger(new ConsoleLogger(), ALL_ON)\n }\n if (debug === false) {\n return new InternalLogger(new ConsoleLogger(), ALL_OFF)\n }\n const { logger, ...cats }: DebugConfig = debug\n const userLogger: Logger = logger ?? new ConsoleLogger()\n return new InternalLogger(userLogger, resolveCategoriesFromPartial(cats))\n}\n"],"names":[],"mappings":";;AAKA,MAAM,UAA8B;AAAA,EAClC,UAAU;AAAA,EACV,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,OAAO;AAAA,EACP,WAAW;AAAA,EACX,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,SAAS;AACX;AAEA,MAAM,SAA6B;AAAA,EACjC,UAAU;AAAA,EACV,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,OAAO;AAAA,EACP,WAAW;AAAA,EACX,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,SAAS;AACX;AAEA,MAAM,uBAAuB,OAA2B;AAAA,EACtD,GAAG;AAAA,EACH,QAAQ;AACV;AAEA,MAAM,+BAA+B,CACnC,aACwB;AAAA,EACxB,UAAU,QAAQ,YAAY;AAAA,EAC9B,QAAQ,QAAQ,UAAU;AAAA,EAC1B,YAAY,QAAQ,cAAc;AAAA,EAClC,OAAO,QAAQ,SAAS;AAAA,EACxB,WAAW,QAAQ,aAAa;AAAA,EAChC,QAAQ,QAAQ,UAAU;AAAA,EAC1B,QAAQ,QAAQ,UAAU;AAAA,EAC1B,SAAS,QAAQ,WAAW;AAC9B;AAaO,SAAS,mBACd,OACgB;AAChB,MAAI,UAAU,QAAW;AACvB,WAAO,IAAI,eAAe,IAAI,cAAA,GAAiB,sBAAsB;AAAA,EACvE;AACA,MAAI,UAAU,MAAM;AAClB,WAAO,IAAI,eAAe,IAAI,cAAA,GAAiB,MAAM;AAAA,EACvD;AACA,MAAI,UAAU,OAAO;AACnB,WAAO,IAAI,eAAe,IAAI,cAAA,GAAiB,OAAO;AAAA,EACxD;AACA,QAAM,EAAE,QAAQ,GAAG,KAAA,IAAsB;AACzC,QAAM,aAAqB,UAAU,IAAI,cAAA;AACzC,SAAO,IAAI,eAAe,YAAY,6BAA6B,IAAI,CAAC;AAC1E;"}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pluggable logger interface consumed by every `@tanstack/ai` activity when `debug` is enabled. Supply a custom implementation via `debug: { logger }` on `chat()`, `summarize()`, `generateImage()`, etc. The four methods correspond to log levels: use `debug` for chunk-level diagnostic output, `info`/`warn` for notable events, `error` for caught exceptions.
|
|
3
|
-
*/
|
|
4
|
-
export interface Logger {
|
|
5
|
-
/**
|
|
6
|
-
* Called for chunk-level diagnostic output (raw provider chunks, per-chunk output, agent-loop iteration markers).
|
|
7
|
-
* @param meta Structured data forwarded to the underlying logger. Loggers like pino will preserve this as a structured record; the default `ConsoleLogger` renders it with a runtime-appropriate strategy (depth-unlimited `console.dir` on Node, JSON appended to the message on Cloudflare Workers, a second `console.<level>` argument elsewhere).
|
|
8
|
-
*/
|
|
9
|
-
debug: (message: string, meta?: Record<string, unknown>) => void;
|
|
10
|
-
/**
|
|
11
|
-
* Called for notable informational events (outgoing requests, tool invocations, middleware transitions).
|
|
12
|
-
* @param meta Structured data forwarded to the underlying logger. Loggers like pino will preserve this as a structured record; the default `ConsoleLogger` renders it with a runtime-appropriate strategy (depth-unlimited `console.dir` on Node, JSON appended to the message on Cloudflare Workers, a second `console.<level>` argument elsewhere).
|
|
13
|
-
*/
|
|
14
|
-
info: (message: string, meta?: Record<string, unknown>) => void;
|
|
15
|
-
/**
|
|
16
|
-
* Called for notable warnings that don't halt execution (deprecations, recoverable anomalies).
|
|
17
|
-
* @param meta Structured data forwarded to the underlying logger. Loggers like pino will preserve this as a structured record; the default `ConsoleLogger` renders it with a runtime-appropriate strategy (depth-unlimited `console.dir` on Node, JSON appended to the message on Cloudflare Workers, a second `console.<level>` argument elsewhere).
|
|
18
|
-
*/
|
|
19
|
-
warn: (message: string, meta?: Record<string, unknown>) => void;
|
|
20
|
-
/**
|
|
21
|
-
* Called for caught exceptions throughout the pipeline.
|
|
22
|
-
* @param meta Structured data forwarded to the underlying logger. Loggers like pino will preserve this as a structured record; the default `ConsoleLogger` renders it with a runtime-appropriate strategy (depth-unlimited `console.dir` on Node, JSON appended to the message on Cloudflare Workers, a second `console.<level>` argument elsewhere).
|
|
23
|
-
*/
|
|
24
|
-
error: (message: string, meta?: Record<string, unknown>) => void;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Per-category toggles for debug logging. Each flag enables or disables one class of log message. Unspecified flags default to `true` when `DebugConfig` is partially specified; `undefined` on the `debug` option defaults all flags to `false` except `errors`.
|
|
28
|
-
*/
|
|
29
|
-
export interface DebugCategories {
|
|
30
|
-
/**
|
|
31
|
-
* Raw chunks/frames received from a provider SDK (OpenAI, Anthropic, Gemini, Ollama, Grok, Groq, OpenRouter, fal, ElevenLabs). Emitted inside every streaming adapter's chunk loop.
|
|
32
|
-
*/
|
|
33
|
-
provider?: boolean;
|
|
34
|
-
/**
|
|
35
|
-
* Chunks/results yielded to the consumer after all middleware. For streaming activities this fires per chunk; for non-streaming activities it fires once per result.
|
|
36
|
-
*/
|
|
37
|
-
output?: boolean;
|
|
38
|
-
/**
|
|
39
|
-
* Inputs and outputs around each middleware hook invocation. Chat-only.
|
|
40
|
-
*/
|
|
41
|
-
middleware?: boolean;
|
|
42
|
-
/**
|
|
43
|
-
* Before/after tool-call execution in the chat agent loop. Chat-only.
|
|
44
|
-
*/
|
|
45
|
-
tools?: boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Iteration markers and phase transitions in the chat agent loop. Chat-only.
|
|
48
|
-
*/
|
|
49
|
-
agentLoop?: boolean;
|
|
50
|
-
/**
|
|
51
|
-
* Config transforms returned by middleware `onConfig` hooks. Chat-only.
|
|
52
|
-
*/
|
|
53
|
-
config?: boolean;
|
|
54
|
-
/**
|
|
55
|
-
* Caught errors throughout the pipeline. Unlike other categories, defaults to `true` even when `debug` is unspecified. Explicitly set `errors: false` or `debug: false` to silence.
|
|
56
|
-
*/
|
|
57
|
-
errors?: boolean;
|
|
58
|
-
/**
|
|
59
|
-
* Outgoing call metadata (provider, model, message/tool counts) emitted before each adapter SDK call.
|
|
60
|
-
*/
|
|
61
|
-
request?: boolean;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Granular debug configuration combining per-category toggles with an optional custom logger. Any unspecified category flag defaults to `true`.
|
|
65
|
-
*/
|
|
66
|
-
export interface DebugConfig extends DebugCategories {
|
|
67
|
-
/**
|
|
68
|
-
* Custom `Logger` implementation. When omitted, a default `ConsoleLogger` routes output to `console.debug`/`info`/`warn`/`error`.
|
|
69
|
-
*/
|
|
70
|
-
logger?: Logger;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* The shape accepted by the `debug` option on every `@tanstack/ai` activity. Pass `true` to enable all categories with the default console logger; `false` to silence everything including errors; an object for granular control.
|
|
74
|
-
*/
|
|
75
|
-
export type DebugOption = boolean | DebugConfig;
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { ChatMiddleware } from '../activities/chat/middleware/types.js';
|
|
2
|
-
/**
|
|
3
|
-
* A content guard rule — either a regex pattern with replacement, or a transform function.
|
|
4
|
-
*/
|
|
5
|
-
export type ContentGuardRule = {
|
|
6
|
-
pattern: RegExp;
|
|
7
|
-
replacement: string;
|
|
8
|
-
} | {
|
|
9
|
-
fn: (text: string) => string;
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* Information passed to the onFiltered callback.
|
|
13
|
-
*/
|
|
14
|
-
export interface ContentFilteredInfo {
|
|
15
|
-
/** The message ID being filtered */
|
|
16
|
-
messageId: string;
|
|
17
|
-
/** The original text before filtering */
|
|
18
|
-
original: string;
|
|
19
|
-
/** The filtered text after rules applied */
|
|
20
|
-
filtered: string;
|
|
21
|
-
/** Which strategy was used */
|
|
22
|
-
strategy: 'delta' | 'buffered';
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Options for the content guard middleware.
|
|
26
|
-
*/
|
|
27
|
-
export interface ContentGuardMiddlewareOptions {
|
|
28
|
-
/**
|
|
29
|
-
* Rules to apply to text content. Each rule is either a regex pattern
|
|
30
|
-
* with a replacement string, or a custom transform function.
|
|
31
|
-
* Rules are applied in order. Each rule receives the output of the previous.
|
|
32
|
-
*/
|
|
33
|
-
rules: Array<ContentGuardRule>;
|
|
34
|
-
/**
|
|
35
|
-
* Matching strategy:
|
|
36
|
-
* - 'delta': Apply rules to each delta as it arrives. Fast, real-time,
|
|
37
|
-
* but patterns spanning chunk boundaries may be missed.
|
|
38
|
-
* - 'buffered': Accumulate content and apply rules to settled portions,
|
|
39
|
-
* holding back a look-behind buffer to catch cross-boundary patterns.
|
|
40
|
-
*
|
|
41
|
-
* @default 'buffered'
|
|
42
|
-
*/
|
|
43
|
-
strategy?: 'delta' | 'buffered';
|
|
44
|
-
/**
|
|
45
|
-
* Number of characters to hold back before emitting (buffered strategy only).
|
|
46
|
-
* Should be at least as long as the longest pattern you expect to match.
|
|
47
|
-
* Buffer is flushed when the stream ends.
|
|
48
|
-
*
|
|
49
|
-
* @default 50
|
|
50
|
-
*/
|
|
51
|
-
bufferSize?: number;
|
|
52
|
-
/**
|
|
53
|
-
* If true, drop the entire chunk when any rule changes the content.
|
|
54
|
-
* @default false
|
|
55
|
-
*/
|
|
56
|
-
blockOnMatch?: boolean;
|
|
57
|
-
/**
|
|
58
|
-
* Callback when content is filtered by any rule.
|
|
59
|
-
*/
|
|
60
|
-
onFiltered?: (info: ContentFilteredInfo) => void;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Creates a middleware that filters or transforms streamed text content.
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```ts
|
|
67
|
-
* import { contentGuardMiddleware } from '@tanstack/ai/middlewares'
|
|
68
|
-
*
|
|
69
|
-
* const guard = contentGuardMiddleware({
|
|
70
|
-
* rules: [
|
|
71
|
-
* { pattern: /\b\d{3}-\d{2}-\d{4}\b/g, replacement: '[SSN REDACTED]' },
|
|
72
|
-
* ],
|
|
73
|
-
* strategy: 'buffered',
|
|
74
|
-
* })
|
|
75
|
-
* ```
|
|
76
|
-
*/
|
|
77
|
-
export declare function contentGuardMiddleware(options: ContentGuardMiddlewareOptions): ChatMiddleware;
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
function applyRules(text, rules) {
|
|
2
|
-
let result = text;
|
|
3
|
-
for (const rule of rules) {
|
|
4
|
-
if ("pattern" in rule) {
|
|
5
|
-
result = result.replace(rule.pattern, rule.replacement);
|
|
6
|
-
} else {
|
|
7
|
-
result = rule.fn(result);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
return result;
|
|
11
|
-
}
|
|
12
|
-
function contentGuardMiddleware(options) {
|
|
13
|
-
const {
|
|
14
|
-
rules,
|
|
15
|
-
strategy = "buffered",
|
|
16
|
-
bufferSize = 50,
|
|
17
|
-
blockOnMatch = false,
|
|
18
|
-
onFiltered
|
|
19
|
-
} = options;
|
|
20
|
-
if (strategy === "delta") {
|
|
21
|
-
return createDeltaStrategy(rules, blockOnMatch, onFiltered);
|
|
22
|
-
}
|
|
23
|
-
return createBufferedStrategy(rules, bufferSize, blockOnMatch, onFiltered);
|
|
24
|
-
}
|
|
25
|
-
function createDeltaStrategy(rules, blockOnMatch, onFiltered) {
|
|
26
|
-
return {
|
|
27
|
-
name: "content-guard",
|
|
28
|
-
onChunk(_ctx, chunk) {
|
|
29
|
-
if (chunk.type !== "TEXT_MESSAGE_CONTENT") return;
|
|
30
|
-
const original = chunk.delta;
|
|
31
|
-
const filtered = applyRules(original, rules);
|
|
32
|
-
if (filtered === original) return;
|
|
33
|
-
if (onFiltered) {
|
|
34
|
-
onFiltered({
|
|
35
|
-
messageId: chunk.messageId,
|
|
36
|
-
original,
|
|
37
|
-
filtered,
|
|
38
|
-
strategy: "delta"
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
if (blockOnMatch) return null;
|
|
42
|
-
const { content: _strippedContent, ...rest } = chunk;
|
|
43
|
-
return {
|
|
44
|
-
...rest,
|
|
45
|
-
delta: filtered
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
function createBufferedStrategy(rules, bufferSize, blockOnMatch, onFiltered) {
|
|
51
|
-
let rawAccumulated = "";
|
|
52
|
-
let emittedFilteredLength = 0;
|
|
53
|
-
let lastMessageId = "";
|
|
54
|
-
function resetState() {
|
|
55
|
-
rawAccumulated = "";
|
|
56
|
-
emittedFilteredLength = 0;
|
|
57
|
-
lastMessageId = "";
|
|
58
|
-
}
|
|
59
|
-
function flushBuffer() {
|
|
60
|
-
if (rawAccumulated.length === 0) return null;
|
|
61
|
-
const filtered = applyRules(rawAccumulated, rules);
|
|
62
|
-
if (blockOnMatch && filtered !== rawAccumulated) {
|
|
63
|
-
if (onFiltered) {
|
|
64
|
-
onFiltered({
|
|
65
|
-
messageId: lastMessageId,
|
|
66
|
-
original: rawAccumulated,
|
|
67
|
-
filtered,
|
|
68
|
-
strategy: "buffered"
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
resetState();
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
const remaining = filtered.slice(emittedFilteredLength);
|
|
75
|
-
if (remaining.length > 0) {
|
|
76
|
-
if (filtered !== rawAccumulated && onFiltered) {
|
|
77
|
-
onFiltered({
|
|
78
|
-
messageId: lastMessageId,
|
|
79
|
-
original: rawAccumulated,
|
|
80
|
-
filtered,
|
|
81
|
-
strategy: "buffered"
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
const flushed = {
|
|
85
|
-
type: "TEXT_MESSAGE_CONTENT",
|
|
86
|
-
messageId: lastMessageId,
|
|
87
|
-
delta: remaining,
|
|
88
|
-
content: filtered,
|
|
89
|
-
timestamp: Date.now()
|
|
90
|
-
};
|
|
91
|
-
resetState();
|
|
92
|
-
return flushed;
|
|
93
|
-
}
|
|
94
|
-
resetState();
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
return {
|
|
98
|
-
name: "content-guard",
|
|
99
|
-
onStart() {
|
|
100
|
-
resetState();
|
|
101
|
-
},
|
|
102
|
-
onChunk(_ctx, chunk) {
|
|
103
|
-
if (chunk.type === "TEXT_MESSAGE_END" || chunk.type === "RUN_FINISHED") {
|
|
104
|
-
const flushed = flushBuffer();
|
|
105
|
-
if (flushed) return [flushed, chunk];
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
if (chunk.type !== "TEXT_MESSAGE_CONTENT") return;
|
|
109
|
-
const pending = [];
|
|
110
|
-
if (lastMessageId && chunk.messageId !== lastMessageId) {
|
|
111
|
-
const flushed = flushBuffer();
|
|
112
|
-
if (flushed) pending.push(flushed);
|
|
113
|
-
}
|
|
114
|
-
rawAccumulated += chunk.delta;
|
|
115
|
-
lastMessageId = chunk.messageId;
|
|
116
|
-
const filtered = applyRules(rawAccumulated, rules);
|
|
117
|
-
const safeFilteredEnd = Math.max(0, filtered.length - bufferSize);
|
|
118
|
-
if (safeFilteredEnd <= emittedFilteredLength) {
|
|
119
|
-
return pending.length > 0 ? pending : null;
|
|
120
|
-
}
|
|
121
|
-
if (blockOnMatch && filtered !== rawAccumulated) {
|
|
122
|
-
if (onFiltered) {
|
|
123
|
-
onFiltered({
|
|
124
|
-
messageId: chunk.messageId,
|
|
125
|
-
original: rawAccumulated,
|
|
126
|
-
filtered,
|
|
127
|
-
strategy: "buffered"
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
return pending.length > 0 ? pending : null;
|
|
131
|
-
}
|
|
132
|
-
const newDelta = filtered.slice(emittedFilteredLength, safeFilteredEnd);
|
|
133
|
-
if (filtered !== rawAccumulated && onFiltered) {
|
|
134
|
-
onFiltered({
|
|
135
|
-
messageId: chunk.messageId,
|
|
136
|
-
original: rawAccumulated,
|
|
137
|
-
filtered,
|
|
138
|
-
strategy: "buffered"
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
emittedFilteredLength = safeFilteredEnd;
|
|
142
|
-
const emitChunk = {
|
|
143
|
-
...chunk,
|
|
144
|
-
delta: newDelta,
|
|
145
|
-
content: filtered.slice(0, safeFilteredEnd)
|
|
146
|
-
};
|
|
147
|
-
const wasEmpty = pending.length === 0;
|
|
148
|
-
pending.push(emitChunk);
|
|
149
|
-
return wasEmpty ? emitChunk : pending;
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
export {
|
|
154
|
-
contentGuardMiddleware
|
|
155
|
-
};
|
|
156
|
-
//# sourceMappingURL=content-guard.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"content-guard.js","sources":["../../../src/middlewares/content-guard.ts"],"sourcesContent":["import type {\n ChatMiddleware,\n ChatMiddlewareContext,\n} from '../activities/chat/middleware/types'\nimport type { StreamChunk } from '../types'\n\n/**\n * A content guard rule — either a regex pattern with replacement, or a transform function.\n */\nexport type ContentGuardRule =\n | { pattern: RegExp; replacement: string }\n | { fn: (text: string) => string }\n\n/**\n * Information passed to the onFiltered callback.\n */\nexport interface ContentFilteredInfo {\n /** The message ID being filtered */\n messageId: string\n /** The original text before filtering */\n original: string\n /** The filtered text after rules applied */\n filtered: string\n /** Which strategy was used */\n strategy: 'delta' | 'buffered'\n}\n\n/**\n * Options for the content guard middleware.\n */\nexport interface ContentGuardMiddlewareOptions {\n /**\n * Rules to apply to text content. Each rule is either a regex pattern\n * with a replacement string, or a custom transform function.\n * Rules are applied in order. Each rule receives the output of the previous.\n */\n rules: Array<ContentGuardRule>\n\n /**\n * Matching strategy:\n * - 'delta': Apply rules to each delta as it arrives. Fast, real-time,\n * but patterns spanning chunk boundaries may be missed.\n * - 'buffered': Accumulate content and apply rules to settled portions,\n * holding back a look-behind buffer to catch cross-boundary patterns.\n *\n * @default 'buffered'\n */\n strategy?: 'delta' | 'buffered'\n\n /**\n * Number of characters to hold back before emitting (buffered strategy only).\n * Should be at least as long as the longest pattern you expect to match.\n * Buffer is flushed when the stream ends.\n *\n * @default 50\n */\n bufferSize?: number\n\n /**\n * If true, drop the entire chunk when any rule changes the content.\n * @default false\n */\n blockOnMatch?: boolean\n\n /**\n * Callback when content is filtered by any rule.\n */\n onFiltered?: (info: ContentFilteredInfo) => void\n}\n\n/**\n * Apply all rules to a string, returning the transformed result.\n */\nfunction applyRules(text: string, rules: Array<ContentGuardRule>): string {\n let result = text\n for (const rule of rules) {\n if ('pattern' in rule) {\n result = result.replace(rule.pattern, rule.replacement)\n } else {\n result = rule.fn(result)\n }\n }\n return result\n}\n\n/**\n * Creates a middleware that filters or transforms streamed text content.\n *\n * @example\n * ```ts\n * import { contentGuardMiddleware } from '@tanstack/ai/middlewares'\n *\n * const guard = contentGuardMiddleware({\n * rules: [\n * { pattern: /\\b\\d{3}-\\d{2}-\\d{4}\\b/g, replacement: '[SSN REDACTED]' },\n * ],\n * strategy: 'buffered',\n * })\n * ```\n */\nexport function contentGuardMiddleware(\n options: ContentGuardMiddlewareOptions,\n): ChatMiddleware {\n const {\n rules,\n strategy = 'buffered',\n bufferSize = 50,\n blockOnMatch = false,\n onFiltered,\n } = options\n\n if (strategy === 'delta') {\n return createDeltaStrategy(rules, blockOnMatch, onFiltered)\n }\n return createBufferedStrategy(rules, bufferSize, blockOnMatch, onFiltered)\n}\n\nfunction createDeltaStrategy(\n rules: Array<ContentGuardRule>,\n blockOnMatch: boolean,\n onFiltered?: (info: ContentFilteredInfo) => void,\n): ChatMiddleware {\n return {\n name: 'content-guard',\n\n onChunk(_ctx: ChatMiddlewareContext, chunk: StreamChunk) {\n if (chunk.type !== 'TEXT_MESSAGE_CONTENT') return\n\n const original = chunk.delta\n const filtered = applyRules(original, rules)\n\n if (filtered === original) return // unchanged, pass through\n\n if (onFiltered) {\n onFiltered({\n messageId: chunk.messageId,\n original,\n filtered,\n strategy: 'delta',\n })\n }\n\n if (blockOnMatch) return null // drop chunk\n\n // Strip out the previous `content` field by destructuring it away — with\n // `exactOptionalPropertyTypes` we can't assign `content: undefined`\n // against `content?: string`. The replacement event carries only the\n // filtered delta.\n const { content: _strippedContent, ...rest } = chunk\n void _strippedContent\n return {\n ...rest,\n delta: filtered,\n }\n },\n }\n}\n\nfunction createBufferedStrategy(\n rules: Array<ContentGuardRule>,\n bufferSize: number,\n blockOnMatch: boolean,\n onFiltered?: (info: ContentFilteredInfo) => void,\n): ChatMiddleware {\n let rawAccumulated = ''\n let emittedFilteredLength = 0\n let lastMessageId = ''\n\n function resetState() {\n rawAccumulated = ''\n emittedFilteredLength = 0\n lastMessageId = ''\n }\n\n function flushBuffer(): StreamChunk | null {\n if (rawAccumulated.length === 0) return null\n\n const filtered = applyRules(rawAccumulated, rules)\n\n if (blockOnMatch && filtered !== rawAccumulated) {\n if (onFiltered) {\n onFiltered({\n messageId: lastMessageId,\n original: rawAccumulated,\n filtered,\n strategy: 'buffered',\n })\n }\n resetState()\n return null\n }\n\n const remaining = filtered.slice(emittedFilteredLength)\n if (remaining.length > 0) {\n if (filtered !== rawAccumulated && onFiltered) {\n onFiltered({\n messageId: lastMessageId,\n original: rawAccumulated,\n filtered,\n strategy: 'buffered',\n })\n }\n\n const flushed = {\n type: 'TEXT_MESSAGE_CONTENT',\n messageId: lastMessageId,\n delta: remaining,\n content: filtered,\n timestamp: Date.now(),\n } as StreamChunk\n\n resetState()\n return flushed\n }\n\n resetState()\n return null\n }\n\n return {\n name: 'content-guard',\n\n onStart() {\n resetState()\n },\n\n onChunk(_ctx: ChatMiddlewareContext, chunk: StreamChunk) {\n // Flush buffer on stream end events\n if (chunk.type === 'TEXT_MESSAGE_END' || chunk.type === 'RUN_FINISHED') {\n const flushed = flushBuffer()\n if (flushed) return [flushed, chunk]\n return // pass through end event\n }\n\n if (chunk.type !== 'TEXT_MESSAGE_CONTENT') return // pass through\n\n // Flush buffer on message boundary change\n const pending: Array<StreamChunk> = []\n if (lastMessageId && chunk.messageId !== lastMessageId) {\n const flushed = flushBuffer()\n if (flushed) pending.push(flushed)\n }\n\n rawAccumulated += chunk.delta\n lastMessageId = chunk.messageId\n\n // Apply rules to full accumulated text, buffer in filtered space\n const filtered = applyRules(rawAccumulated, rules)\n const safeFilteredEnd = Math.max(0, filtered.length - bufferSize)\n\n if (safeFilteredEnd <= emittedFilteredLength) {\n return pending.length > 0 ? pending : null\n }\n\n if (blockOnMatch && filtered !== rawAccumulated) {\n if (onFiltered) {\n onFiltered({\n messageId: chunk.messageId,\n original: rawAccumulated,\n filtered,\n strategy: 'buffered',\n })\n }\n return pending.length > 0 ? pending : null\n }\n\n const newDelta = filtered.slice(emittedFilteredLength, safeFilteredEnd)\n\n if (filtered !== rawAccumulated && onFiltered) {\n onFiltered({\n messageId: chunk.messageId,\n original: rawAccumulated,\n filtered,\n strategy: 'buffered',\n })\n }\n\n emittedFilteredLength = safeFilteredEnd\n\n const emitChunk = {\n ...chunk,\n delta: newDelta,\n content: filtered.slice(0, safeFilteredEnd),\n } as StreamChunk\n\n // `pending` was empty before this push iff `emitChunk` is now the only\n // entry — return it directly without re-indexing through `pending[0]`.\n const wasEmpty = pending.length === 0\n pending.push(emitChunk)\n return wasEmpty ? emitChunk : pending\n },\n }\n}\n"],"names":[],"mappings":"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"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export { toolCacheMiddleware, type ToolCacheMiddlewareOptions, type ToolCacheStorage, type ToolCacheEntry, } from './tool-cache.js';
|
|
2
|
-
export { contentGuardMiddleware, type ContentGuardMiddlewareOptions, type ContentGuardRule, type ContentFilteredInfo, } from './content-guard.js';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { AttributeValue, Meter, Span, SpanOptions, Tracer } from '@opentelemetry/api';
|
|
2
|
-
import { ChatMiddleware, ChatMiddlewareContext } from '../activities/chat/middleware/types.js';
|
|
3
|
-
import { GenerationMiddleware, GenerationMiddlewareContext } from '../activities/middleware/types.js';
|
|
4
|
-
/**
|
|
5
|
-
* Scope (role) of an OTel span emitted by this middleware.
|
|
6
|
-
*
|
|
7
|
-
* - `chat` — the root span for a single `chat()` call
|
|
8
|
-
* - `iteration` — one per agent-loop iteration (one model call)
|
|
9
|
-
* - `tool` — one per tool execution inside an iteration
|
|
10
|
-
* - `generation` — the single span for a media activity call
|
|
11
|
-
* (`generateImage`, `generateVideo`, `generateSpeech`, …)
|
|
12
|
-
*/
|
|
13
|
-
export type OtelSpanScope = 'chat' | 'iteration' | 'tool' | 'generation';
|
|
14
|
-
/**
|
|
15
|
-
* Alias retained for backwards compatibility. Prefer {@link OtelSpanScope}.
|
|
16
|
-
*
|
|
17
|
-
* @deprecated Use `OtelSpanScope` instead — the name shadows OTel's built-in
|
|
18
|
-
* `SpanKind` which is also imported by integrations of this middleware.
|
|
19
|
-
*/
|
|
20
|
-
export type OtelSpanKind = OtelSpanScope;
|
|
21
|
-
/**
|
|
22
|
-
* Span metadata passed to `spanNameFormatter`, `attributeEnricher`,
|
|
23
|
-
* `onBeforeSpanStart`, and `onSpanEnd`. Discriminated by `kind` so that
|
|
24
|
-
* tool-only fields narrow automatically inside callback bodies.
|
|
25
|
-
*/
|
|
26
|
-
export type OtelSpanInfo<TScope extends OtelSpanScope = OtelSpanScope> = TScope extends 'chat' ? {
|
|
27
|
-
kind: 'chat';
|
|
28
|
-
ctx: ChatMiddlewareContext;
|
|
29
|
-
} : TScope extends 'iteration' ? {
|
|
30
|
-
kind: 'iteration';
|
|
31
|
-
ctx: ChatMiddlewareContext;
|
|
32
|
-
iteration: number;
|
|
33
|
-
} : TScope extends 'tool' ? {
|
|
34
|
-
kind: 'tool';
|
|
35
|
-
ctx: ChatMiddlewareContext;
|
|
36
|
-
iteration: number;
|
|
37
|
-
toolName: string;
|
|
38
|
-
toolCallId: string;
|
|
39
|
-
} : TScope extends 'generation' ? {
|
|
40
|
-
kind: 'generation';
|
|
41
|
-
ctx: GenerationMiddlewareContext;
|
|
42
|
-
} : never;
|
|
43
|
-
export interface OtelMiddlewareOptions {
|
|
44
|
-
/** OTel `Tracer` used to start root, iteration, and tool spans. */
|
|
45
|
-
tracer: Tracer;
|
|
46
|
-
/**
|
|
47
|
-
* Optional OTel `Meter`. When provided, the middleware records
|
|
48
|
-
* `gen_ai.client.operation.duration` and `gen_ai.client.token.usage`
|
|
49
|
-
* histograms. Omit to disable metrics without disabling tracing.
|
|
50
|
-
*/
|
|
51
|
-
meter?: Meter;
|
|
52
|
-
/**
|
|
53
|
-
* When `true`, prompt and completion content is attached to iteration spans
|
|
54
|
-
* as `gen_ai.*.message` / `gen_ai.choice` events. Defaults to `false` so
|
|
55
|
-
* that PII never lands on a span by accident.
|
|
56
|
-
*/
|
|
57
|
-
captureContent?: boolean;
|
|
58
|
-
/**
|
|
59
|
-
* Invoked on every captured content string before it lands on a span.
|
|
60
|
-
* Return a redacted version. If this function throws, the middleware emits
|
|
61
|
-
* the literal sentinel `"[redaction_failed]"` instead of the original text
|
|
62
|
-
* — it never falls back to raw content.
|
|
63
|
-
*/
|
|
64
|
-
redact?: (text: string) => string;
|
|
65
|
-
/**
|
|
66
|
-
* Maximum characters kept in the per-iteration assistant text buffer used
|
|
67
|
-
* to emit `gen_ai.choice` events. Extra characters are truncated with a
|
|
68
|
-
* trailing `"…"` marker. Defaults to 100 000. Set to `0` to disable the
|
|
69
|
-
* cap. Exporters typically truncate long attribute values anyway.
|
|
70
|
-
*/
|
|
71
|
-
maxContentLength?: number;
|
|
72
|
-
/** Override the default span name for each `kind`. */
|
|
73
|
-
spanNameFormatter?: (info: OtelSpanInfo) => string;
|
|
74
|
-
/** Add extra attributes to each span. */
|
|
75
|
-
attributeEnricher?: (info: OtelSpanInfo) => Record<string, AttributeValue>;
|
|
76
|
-
/** Mutate `SpanOptions` immediately before `tracer.startSpan(...)`. */
|
|
77
|
-
onBeforeSpanStart?: (info: OtelSpanInfo, options: SpanOptions) => SpanOptions;
|
|
78
|
-
/** Fires just before every `span.end()`. */
|
|
79
|
-
onSpanEnd?: (info: OtelSpanInfo, span: Span) => void;
|
|
80
|
-
}
|
|
81
|
-
export declare function otelMiddleware(options: OtelMiddlewareOptions): GenerationMiddleware & ChatMiddleware;
|