@tanstack/ai 0.36.0 → 0.38.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/dist/esm/activities/chat/adapter.d.ts +163 -0
- package/dist/esm/activities/chat/adapter.js +17 -0
- package/dist/esm/activities/chat/adapter.js.map +1 -0
- package/dist/esm/activities/chat/agent-loop-strategies.d.ts +59 -0
- package/dist/esm/activities/chat/agent-loop-strategies.js +23 -0
- package/dist/esm/activities/chat/agent-loop-strategies.js.map +1 -0
- package/dist/esm/activities/chat/index.d.ts +270 -0
- package/dist/esm/activities/chat/index.js +1724 -0
- package/dist/esm/activities/chat/index.js.map +1 -0
- package/dist/esm/activities/chat/mcp/manager.d.ts +25 -0
- package/dist/esm/activities/chat/mcp/manager.js +80 -0
- package/dist/esm/activities/chat/mcp/manager.js.map +1 -0
- package/dist/esm/activities/chat/mcp/types.d.ts +78 -0
- package/dist/esm/activities/chat/messages.d.ts +78 -0
- package/dist/esm/activities/chat/messages.js +374 -0
- package/dist/esm/activities/chat/messages.js.map +1 -0
- package/dist/esm/activities/chat/middleware/builder.d.ts +46 -0
- package/dist/esm/activities/chat/middleware/builder.js +17 -0
- package/dist/esm/activities/chat/middleware/builder.js.map +1 -0
- package/dist/esm/activities/chat/middleware/capabilities.d.ts +93 -0
- package/dist/esm/activities/chat/middleware/capabilities.js +45 -0
- package/dist/esm/activities/chat/middleware/capabilities.js.map +1 -0
- package/dist/esm/activities/chat/middleware/compose.d.ts +87 -0
- package/dist/esm/activities/chat/middleware/compose.js +510 -0
- package/dist/esm/activities/chat/middleware/compose.js.map +1 -0
- package/dist/esm/activities/chat/middleware/define.d.ts +20 -0
- package/dist/esm/activities/chat/middleware/define.js +7 -0
- package/dist/esm/activities/chat/middleware/define.js.map +1 -0
- package/dist/esm/activities/chat/middleware/index.d.ts +10 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.d.ts +89 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js +76 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js.map +1 -0
- package/dist/esm/activities/chat/middleware/types.d.ts +405 -0
- package/dist/esm/activities/chat/middleware/validate.d.ts +19 -0
- package/dist/esm/activities/chat/middleware/validate.js +30 -0
- package/dist/esm/activities/chat/middleware/validate.js.map +1 -0
- package/dist/esm/activities/chat/runtime-context-types.d.ts +43 -0
- package/dist/esm/activities/chat/stream/index.d.ts +11 -0
- package/dist/esm/activities/chat/stream/json-parser.d.ts +38 -0
- package/dist/esm/activities/chat/stream/json-parser.js +28 -0
- package/dist/esm/activities/chat/stream/json-parser.js.map +1 -0
- package/dist/esm/activities/chat/stream/message-updaters.d.ts +81 -0
- package/dist/esm/activities/chat/stream/message-updaters.js +253 -0
- package/dist/esm/activities/chat/stream/message-updaters.js.map +1 -0
- package/dist/esm/activities/chat/stream/processor.d.ts +439 -0
- package/dist/esm/activities/chat/stream/processor.js +1449 -0
- package/dist/esm/activities/chat/stream/processor.js.map +1 -0
- package/dist/esm/activities/chat/stream/strategies.d.ts +43 -0
- package/dist/esm/activities/chat/stream/strategies.js +54 -0
- package/dist/esm/activities/chat/stream/strategies.js.map +1 -0
- package/dist/esm/activities/chat/stream/types.d.ts +90 -0
- package/dist/esm/activities/chat/tools/lazy-tool-manager.d.ts +82 -0
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js +194 -0
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js.map +1 -0
- package/dist/esm/activities/chat/tools/lazy-tools.d.ts +15 -0
- package/dist/esm/activities/chat/tools/lazy-tools.js +16 -0
- package/dist/esm/activities/chat/tools/lazy-tools.js.map +1 -0
- package/dist/esm/activities/chat/tools/schema-converter.d.ts +140 -0
- package/dist/esm/activities/chat/tools/schema-converter.js +167 -0
- package/dist/esm/activities/chat/tools/schema-converter.js.map +1 -0
- package/dist/esm/activities/chat/tools/tool-calls.d.ts +145 -0
- package/dist/esm/activities/chat/tools/tool-calls.js +548 -0
- package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -0
- package/dist/esm/activities/chat/tools/tool-definition.d.ts +135 -0
- package/dist/esm/activities/chat/tools/tool-definition.js +25 -0
- package/dist/esm/activities/chat/tools/tool-definition.js.map +1 -0
- package/dist/esm/activities/error-payload.d.ts +30 -0
- package/dist/esm/activities/error-payload.js +54 -0
- package/dist/esm/activities/error-payload.js.map +1 -0
- package/dist/esm/activities/generateAudio/adapter.d.ts +62 -0
- package/dist/esm/activities/generateAudio/adapter.js +16 -0
- package/dist/esm/activities/generateAudio/adapter.js.map +1 -0
- package/dist/esm/activities/generateAudio/index.d.ts +85 -0
- package/dist/esm/activities/generateAudio/index.js +114 -0
- package/dist/esm/activities/generateAudio/index.js.map +1 -0
- package/dist/esm/activities/generateImage/adapter.d.ts +78 -0
- package/dist/esm/activities/generateImage/adapter.js +16 -0
- package/dist/esm/activities/generateImage/adapter.js.map +1 -0
- package/dist/esm/activities/generateImage/index.d.ts +134 -0
- package/dist/esm/activities/generateImage/index.js +121 -0
- package/dist/esm/activities/generateImage/index.js.map +1 -0
- package/dist/esm/activities/generateSpeech/adapter.d.ts +62 -0
- package/dist/esm/activities/generateSpeech/adapter.js +16 -0
- package/dist/esm/activities/generateSpeech/adapter.js.map +1 -0
- package/dist/esm/activities/generateSpeech/index.d.ts +96 -0
- package/dist/esm/activities/generateSpeech/index.js +119 -0
- package/dist/esm/activities/generateSpeech/index.js.map +1 -0
- package/dist/esm/activities/generateTranscription/adapter.d.ts +62 -0
- package/dist/esm/activities/generateTranscription/adapter.js +16 -0
- package/dist/esm/activities/generateTranscription/adapter.js.map +1 -0
- package/dist/esm/activities/generateTranscription/index.d.ts +109 -0
- package/dist/esm/activities/generateTranscription/index.js +109 -0
- package/dist/esm/activities/generateTranscription/index.js.map +1 -0
- package/dist/esm/activities/generateVideo/adapter.d.ts +145 -0
- package/dist/esm/activities/generateVideo/adapter.js +30 -0
- package/dist/esm/activities/generateVideo/adapter.js.map +1 -0
- package/dist/esm/activities/generateVideo/index.d.ts +223 -0
- package/dist/esm/activities/generateVideo/index.js +295 -0
- package/dist/esm/activities/generateVideo/index.js.map +1 -0
- package/dist/esm/activities/generateVideo/snap.d.ts +14 -0
- package/dist/esm/activities/generateVideo/snap.js +54 -0
- package/dist/esm/activities/generateVideo/snap.js.map +1 -0
- package/dist/esm/activities/index.d.ts +27 -0
- package/dist/esm/activities/index.js +43 -0
- package/dist/esm/activities/index.js.map +1 -0
- package/dist/esm/activities/middleware/index.d.ts +2 -0
- package/dist/esm/activities/middleware/run.d.ts +20 -0
- package/dist/esm/activities/middleware/run.js +42 -0
- package/dist/esm/activities/middleware/run.js.map +1 -0
- package/dist/esm/activities/middleware/types.d.ts +118 -0
- package/dist/esm/activities/stream-generation-result.d.ts +15 -0
- package/dist/esm/activities/stream-generation-result.js +49 -0
- package/dist/esm/activities/stream-generation-result.js.map +1 -0
- package/dist/esm/activities/summarize/adapter.d.ts +74 -0
- package/dist/esm/activities/summarize/adapter.js +16 -0
- package/dist/esm/activities/summarize/adapter.js.map +1 -0
- package/dist/esm/activities/summarize/chat-stream-summarize.d.ts +45 -0
- package/dist/esm/activities/summarize/chat-stream-summarize.js +212 -0
- package/dist/esm/activities/summarize/chat-stream-summarize.js.map +1 -0
- package/dist/esm/activities/summarize/index.d.ts +108 -0
- package/dist/esm/activities/summarize/index.js +113 -0
- package/dist/esm/activities/summarize/index.js.map +1 -0
- package/dist/esm/adapter-internals.d.ts +5 -0
- package/dist/esm/adapter-internals.js +10 -0
- package/dist/esm/adapter-internals.js.map +1 -0
- package/dist/esm/client.d.ts +44 -0
- package/dist/esm/client.js +67 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/extend-adapter.d.ts +152 -0
- package/dist/esm/extend-adapter.js +21 -0
- package/dist/esm/extend-adapter.js.map +1 -0
- package/dist/esm/index.d.ts +45 -0
- package/dist/esm/index.js +109 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/logger/console-logger.d.ts +29 -0
- package/dist/esm/logger/console-logger.js +83 -0
- package/dist/esm/logger/console-logger.js.map +1 -0
- package/dist/esm/logger/internal-logger.d.ts +41 -0
- package/dist/esm/logger/internal-logger.js +86 -0
- package/dist/esm/logger/internal-logger.js.map +1 -0
- package/dist/esm/logger/resolve.d.ts +14 -0
- package/dist/esm/logger/resolve.js +54 -0
- package/dist/esm/logger/resolve.js.map +1 -0
- package/dist/esm/logger/types.d.ts +75 -0
- package/dist/esm/middlewares/content-guard.d.ts +77 -0
- package/dist/esm/middlewares/content-guard.js +156 -0
- package/dist/esm/middlewares/content-guard.js.map +1 -0
- package/dist/esm/middlewares/index.d.ts +2 -0
- package/dist/esm/middlewares/index.js +7 -0
- package/dist/esm/middlewares/index.js.map +1 -0
- package/dist/esm/middlewares/otel.d.ts +81 -0
- package/dist/esm/middlewares/otel.js +745 -0
- package/dist/esm/middlewares/otel.js.map +1 -0
- package/dist/esm/middlewares/tool-cache.d.ts +1 -0
- package/dist/esm/middlewares/usage-attributes.d.ts +24 -0
- package/dist/esm/middlewares/usage-attributes.js +43 -0
- package/dist/esm/middlewares/usage-attributes.js.map +1 -0
- package/dist/esm/realtime/index.d.ts +28 -0
- package/dist/esm/realtime/index.js +8 -0
- package/dist/esm/realtime/index.js.map +1 -0
- package/dist/esm/realtime/types.d.ts +282 -0
- package/dist/esm/stream-to-response.d.ts +102 -0
- package/dist/esm/stream-to-response.js +121 -0
- package/dist/esm/stream-to-response.js.map +1 -0
- package/dist/esm/strip-to-spec-middleware.d.ts +18 -0
- package/dist/esm/strip-to-spec-middleware.js +20 -0
- package/dist/esm/strip-to-spec-middleware.js.map +1 -0
- package/dist/esm/system-prompts.d.ts +66 -0
- package/dist/esm/system-prompts.js +23 -0
- package/dist/esm/system-prompts.js.map +1 -0
- package/dist/esm/tool-registry.d.ts +81 -0
- package/dist/esm/tool-registry.js +49 -0
- package/dist/esm/tool-registry.js.map +1 -0
- package/dist/esm/tools/provider-tool.d.ts +30 -0
- package/dist/esm/tools/provider-tool.js +7 -0
- package/dist/esm/tools/provider-tool.js.map +1 -0
- package/dist/esm/types.d.ts +1627 -0
- package/dist/esm/utilities/ag-ui-wire.d.ts +44 -0
- package/dist/esm/utilities/ag-ui-wire.js +107 -0
- package/dist/esm/utilities/ag-ui-wire.js.map +1 -0
- package/dist/esm/utilities/chat-params.d.ts +85 -0
- package/dist/esm/utilities/chat-params.js +100 -0
- package/dist/esm/utilities/chat-params.js.map +1 -0
- package/dist/esm/utilities/errors.d.ts +13 -0
- package/dist/esm/utilities/errors.js +22 -0
- package/dist/esm/utilities/errors.js.map +1 -0
- package/dist/esm/utilities/media-prompt.d.ts +35 -0
- package/dist/esm/utilities/media-prompt.js +43 -0
- package/dist/esm/utilities/media-prompt.js.map +1 -0
- package/dist/esm/utilities/numbers.d.ts +8 -0
- package/dist/esm/utilities/numbers.js +12 -0
- package/dist/esm/utilities/numbers.js.map +1 -0
- package/dist/esm/utilities/sampling-keys.d.ts +20 -0
- package/dist/esm/utilities/sampling-keys.js +20 -0
- package/dist/esm/utilities/sampling-keys.js.map +1 -0
- package/dist/esm/utilities/tool-result.d.ts +21 -0
- package/dist/esm/utilities/tool-result.js +37 -0
- package/dist/esm/utilities/tool-result.js.map +1 -0
- package/dist/esm/utilities/usage.d.ts +31 -0
- package/dist/esm/utilities/usage.js +11 -0
- package/dist/esm/utilities/usage.js.map +1 -0
- package/dist/esm/utils.d.ts +17 -0
- package/dist/esm/utils.js +20 -0
- package/dist/esm/utils.js.map +1 -0
- package/package.json +3 -3
- package/skills/ai-core/chat-experience/SKILL.md +30 -2
- package/skills/ai-core/media-generation/SKILL.md +13 -0
- package/skills/ai-core/tool-calling/SKILL.md +10 -3
- package/src/activities/chat/mcp/manager.ts +31 -1
- package/src/activities/chat/mcp/types.ts +23 -0
- package/src/activities/chat/messages.ts +5 -0
- package/src/activities/chat/stream/processor.ts +42 -0
- package/src/activities/chat/tools/tool-calls.ts +96 -2
- package/src/client.ts +1 -0
- package/src/types.ts +31 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { chat, createChatOptions } from "./activities/chat/index.js";
|
|
2
|
+
import { createSummarizeOptions, summarize } from "./activities/summarize/index.js";
|
|
3
|
+
import { createImageOptions, generateImage } from "./activities/generateImage/index.js";
|
|
4
|
+
import { createAudioOptions, generateAudio } from "./activities/generateAudio/index.js";
|
|
5
|
+
import { createVideoOptions, generateVideo, getVideoJobStatus } from "./activities/generateVideo/index.js";
|
|
6
|
+
import { createSpeechOptions, generateSpeech } from "./activities/generateSpeech/index.js";
|
|
7
|
+
import { createTranscriptionOptions, generateTranscription } from "./activities/generateTranscription/index.js";
|
|
8
|
+
import { toolDefinition } from "./activities/chat/tools/tool-definition.js";
|
|
9
|
+
import { MCPDuplicateToolNameError } from "./activities/chat/mcp/manager.js";
|
|
10
|
+
import { StandardSchemaValidationError, convertSchemaToJsonSchema, isStandardSchema, parseWithStandardSchema } from "./activities/chat/tools/schema-converter.js";
|
|
11
|
+
import { streamToText, toHttpResponse, toHttpStream, toServerSentEventsResponse, toServerSentEventsStream } from "./stream-to-response.js";
|
|
12
|
+
import { ToolCallManager } from "./activities/chat/tools/tool-calls.js";
|
|
13
|
+
import { DISCOVERY_TOOL_NAME } from "./activities/chat/tools/lazy-tool-manager.js";
|
|
14
|
+
import { brandProviderTool } from "./tools/provider-tool.js";
|
|
15
|
+
import { combineStrategies, maxIterations, untilFinishReason } from "./activities/chat/agent-loop-strategies.js";
|
|
16
|
+
import { createFrozenRegistry, createToolRegistry } from "./tool-registry.js";
|
|
17
|
+
import { firstSentence, renderLazyCatalogEntry } from "./activities/chat/tools/lazy-tools.js";
|
|
18
|
+
import { buildBaseUsage } from "./utilities/usage.js";
|
|
19
|
+
import { resolveMediaPrompt } from "./utilities/media-prompt.js";
|
|
20
|
+
import { normalizeSystemPrompts } from "./system-prompts.js";
|
|
21
|
+
import { detectImageMimeType } from "./utils.js";
|
|
22
|
+
import { realtimeToken } from "./realtime/index.js";
|
|
23
|
+
import { convertMessagesToModelMessages, generateMessageId, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, uiMessageToModelMessages } from "./activities/chat/messages.js";
|
|
24
|
+
import { chatParamsFromRequest, chatParamsFromRequestBody, mergeAgentTools } from "./utilities/chat-params.js";
|
|
25
|
+
import { uiMessagesToWire } from "./utilities/ag-ui-wire.js";
|
|
26
|
+
import { isContentPart, isContentPartArray, normalizeToolResult } from "./utilities/tool-result.js";
|
|
27
|
+
import { createModel, extendAdapter } from "./extend-adapter.js";
|
|
28
|
+
import { ConsoleLogger } from "./logger/console-logger.js";
|
|
29
|
+
import { BatchStrategy, CompositeStrategy, ImmediateStrategy, PunctuationStrategy, WordBoundaryStrategy } from "./activities/chat/stream/strategies.js";
|
|
30
|
+
import { EventType } from "@ag-ui/core";
|
|
31
|
+
import { PartialJSONParser, defaultJSONParser, parsePartialJSON } from "./activities/chat/stream/json-parser.js";
|
|
32
|
+
import { StreamProcessor, createReplayStream } from "./activities/chat/stream/processor.js";
|
|
33
|
+
import { createCapability } from "./activities/chat/middleware/capabilities.js";
|
|
34
|
+
import { createChatMiddleware } from "./activities/chat/middleware/builder.js";
|
|
35
|
+
import { defineChatMiddleware } from "./activities/chat/middleware/define.js";
|
|
36
|
+
export {
|
|
37
|
+
BatchStrategy,
|
|
38
|
+
CompositeStrategy,
|
|
39
|
+
ConsoleLogger,
|
|
40
|
+
DISCOVERY_TOOL_NAME,
|
|
41
|
+
EventType,
|
|
42
|
+
ImmediateStrategy,
|
|
43
|
+
MCPDuplicateToolNameError,
|
|
44
|
+
PartialJSONParser,
|
|
45
|
+
PunctuationStrategy,
|
|
46
|
+
StandardSchemaValidationError,
|
|
47
|
+
StreamProcessor,
|
|
48
|
+
ToolCallManager,
|
|
49
|
+
WordBoundaryStrategy,
|
|
50
|
+
brandProviderTool,
|
|
51
|
+
buildBaseUsage,
|
|
52
|
+
chat,
|
|
53
|
+
chatParamsFromRequest,
|
|
54
|
+
chatParamsFromRequestBody,
|
|
55
|
+
combineStrategies,
|
|
56
|
+
convertMessagesToModelMessages,
|
|
57
|
+
convertSchemaToJsonSchema,
|
|
58
|
+
createAudioOptions,
|
|
59
|
+
createCapability,
|
|
60
|
+
createChatMiddleware,
|
|
61
|
+
createChatOptions,
|
|
62
|
+
createFrozenRegistry,
|
|
63
|
+
createImageOptions,
|
|
64
|
+
createModel,
|
|
65
|
+
createReplayStream,
|
|
66
|
+
createSpeechOptions,
|
|
67
|
+
createSummarizeOptions,
|
|
68
|
+
createToolRegistry,
|
|
69
|
+
createTranscriptionOptions,
|
|
70
|
+
createVideoOptions,
|
|
71
|
+
defaultJSONParser,
|
|
72
|
+
defineChatMiddleware,
|
|
73
|
+
detectImageMimeType,
|
|
74
|
+
extendAdapter,
|
|
75
|
+
firstSentence,
|
|
76
|
+
generateAudio,
|
|
77
|
+
generateImage,
|
|
78
|
+
generateMessageId,
|
|
79
|
+
generateSpeech,
|
|
80
|
+
generateTranscription,
|
|
81
|
+
generateVideo,
|
|
82
|
+
getVideoJobStatus,
|
|
83
|
+
isContentPart,
|
|
84
|
+
isContentPartArray,
|
|
85
|
+
isStandardSchema,
|
|
86
|
+
maxIterations,
|
|
87
|
+
mergeAgentTools,
|
|
88
|
+
modelMessageToUIMessage,
|
|
89
|
+
modelMessagesToUIMessages,
|
|
90
|
+
normalizeSystemPrompts,
|
|
91
|
+
normalizeToUIMessage,
|
|
92
|
+
normalizeToolResult,
|
|
93
|
+
parsePartialJSON,
|
|
94
|
+
parseWithStandardSchema,
|
|
95
|
+
realtimeToken,
|
|
96
|
+
renderLazyCatalogEntry,
|
|
97
|
+
resolveMediaPrompt,
|
|
98
|
+
streamToText,
|
|
99
|
+
summarize,
|
|
100
|
+
toHttpResponse,
|
|
101
|
+
toHttpStream,
|
|
102
|
+
toServerSentEventsResponse,
|
|
103
|
+
toServerSentEventsStream,
|
|
104
|
+
toolDefinition,
|
|
105
|
+
uiMessageToModelMessages,
|
|
106
|
+
uiMessagesToWire,
|
|
107
|
+
untilFinishReason
|
|
108
|
+
};
|
|
109
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Logger } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Default `Logger` implementation that routes each level to the matching
|
|
4
|
+
* `console` method:
|
|
5
|
+
*
|
|
6
|
+
* - `debug` → `console.debug`
|
|
7
|
+
* - `info` → `console.info`
|
|
8
|
+
* - `warn` → `console.warn`
|
|
9
|
+
* - `error` → `console.error`
|
|
10
|
+
*
|
|
11
|
+
* When a `meta` object is supplied it is rendered with the strategy that
|
|
12
|
+
* actually surfaces it on the current runtime (see {@link MetaStrategy}):
|
|
13
|
+
* depth-unlimited `console.dir` on Node, circular-safe JSON on Cloudflare
|
|
14
|
+
* Workers, and an extra console argument everywhere else.
|
|
15
|
+
*
|
|
16
|
+
* This is the logger used when `debug` is enabled on any activity and no
|
|
17
|
+
* custom `logger` is supplied via `debug: { logger }`.
|
|
18
|
+
*/
|
|
19
|
+
export declare class ConsoleLogger implements Logger {
|
|
20
|
+
/** Log a debug-level message; forwards to `console.debug`. */
|
|
21
|
+
debug(message: string, meta?: Record<string, unknown>): void;
|
|
22
|
+
/** Log an info-level message; forwards to `console.info`. */
|
|
23
|
+
info(message: string, meta?: Record<string, unknown>): void;
|
|
24
|
+
/** Log a warning-level message; forwards to `console.warn`. */
|
|
25
|
+
warn(message: string, meta?: Record<string, unknown>): void;
|
|
26
|
+
/** Log an error-level message; forwards to `console.error`. */
|
|
27
|
+
error(message: string, meta?: Record<string, unknown>): void;
|
|
28
|
+
private emit;
|
|
29
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const DIR_OPTIONS = { depth: null, colors: true };
|
|
2
|
+
function resolveMetaStrategy() {
|
|
3
|
+
try {
|
|
4
|
+
if (globalThis.navigator?.userAgent === "Cloudflare-Workers") return "json";
|
|
5
|
+
} catch {
|
|
6
|
+
}
|
|
7
|
+
if (typeof process !== "undefined" && // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- a partial process global (bundler shims) may lack versions
|
|
8
|
+
typeof process.versions?.node === "string") {
|
|
9
|
+
return "dir";
|
|
10
|
+
}
|
|
11
|
+
return "arg";
|
|
12
|
+
}
|
|
13
|
+
function stringifyMetaSafely(value) {
|
|
14
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
15
|
+
try {
|
|
16
|
+
return JSON.stringify(
|
|
17
|
+
value,
|
|
18
|
+
(_key, entry) => {
|
|
19
|
+
if (typeof entry === "bigint") return entry.toString();
|
|
20
|
+
if (entry instanceof Error) {
|
|
21
|
+
return {
|
|
22
|
+
name: entry.name,
|
|
23
|
+
message: entry.message,
|
|
24
|
+
stack: entry.stack
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (typeof entry === "object" && entry !== null) {
|
|
28
|
+
if (seen.has(entry)) return "[Circular]";
|
|
29
|
+
seen.add(entry);
|
|
30
|
+
}
|
|
31
|
+
return entry;
|
|
32
|
+
},
|
|
33
|
+
2
|
|
34
|
+
);
|
|
35
|
+
} catch {
|
|
36
|
+
try {
|
|
37
|
+
return String(value);
|
|
38
|
+
} catch {
|
|
39
|
+
return "[Unserializable meta]";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
class ConsoleLogger {
|
|
44
|
+
/** Log a debug-level message; forwards to `console.debug`. */
|
|
45
|
+
debug(message, meta) {
|
|
46
|
+
this.emit("debug", message, meta);
|
|
47
|
+
}
|
|
48
|
+
/** Log an info-level message; forwards to `console.info`. */
|
|
49
|
+
info(message, meta) {
|
|
50
|
+
this.emit("info", message, meta);
|
|
51
|
+
}
|
|
52
|
+
/** Log a warning-level message; forwards to `console.warn`. */
|
|
53
|
+
warn(message, meta) {
|
|
54
|
+
this.emit("warn", message, meta);
|
|
55
|
+
}
|
|
56
|
+
/** Log an error-level message; forwards to `console.error`. */
|
|
57
|
+
error(message, meta) {
|
|
58
|
+
this.emit("error", message, meta);
|
|
59
|
+
}
|
|
60
|
+
emit(level, message, meta) {
|
|
61
|
+
if (meta === void 0) {
|
|
62
|
+
console[level](message);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
switch (resolveMetaStrategy()) {
|
|
66
|
+
case "dir":
|
|
67
|
+
console[level](message);
|
|
68
|
+
console.dir(meta, DIR_OPTIONS);
|
|
69
|
+
break;
|
|
70
|
+
case "json":
|
|
71
|
+
console[level](`${message}
|
|
72
|
+
${stringifyMetaSafely(meta)}`);
|
|
73
|
+
break;
|
|
74
|
+
case "arg":
|
|
75
|
+
console[level](message, meta);
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
ConsoleLogger
|
|
82
|
+
};
|
|
83
|
+
//# sourceMappingURL=console-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"console-logger.js","sources":["../../../src/logger/console-logger.ts"],"sourcesContent":["import type { Logger } from './types'\n\n/**\n * `util.inspect` options used with `console.dir` on Node so deeply nested\n * structures (e.g. provider chunk payloads with `usage`, `output`,\n * `reasoning`, `tools`) render in full instead of truncating to\n * `[Object]` / `[Array]`.\n */\nconst DIR_OPTIONS = { depth: null, colors: true } as const\n\n/**\n * How `meta` should be rendered on the current runtime:\n *\n * - `dir` — Node. `console.dir(meta, { depth: null, colors: true })` gives a\n * depth-unlimited, colored inspect dump.\n * - `json` — Cloudflare Workers / workerd. workerd never forwards\n * `console.dir` output to the terminal (with or without options), and its\n * own inspect of extra console arguments truncates nested objects, so the\n * payload is appended as circular-safe pretty-printed JSON instead.\n * - `arg` — everything else (browsers, Deno, Bun). `meta` is passed as an\n * extra console argument: devtools keep collapsible object trees and the\n * runtime's inspect handles circular references natively.\n */\ntype MetaStrategy = 'dir' | 'json' | 'arg'\n\nfunction resolveMetaStrategy(): MetaStrategy {\n // workerd must be detected before the Node check: under the `nodejs_compat`\n // flag it emulates `process.versions.node`, but still drops `console.dir`.\n try {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- navigator is missing on Node < 21 despite the DOM lib typing it as always present\n if (globalThis.navigator?.userAgent === 'Cloudflare-Workers') return 'json'\n } catch {\n // A locked-down runtime with a throwing `userAgent` getter is not workerd;\n // fall through to the remaining checks rather than crash the log call.\n }\n if (\n typeof process !== 'undefined' &&\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- a partial process global (bundler shims) may lack versions\n typeof process.versions?.node === 'string'\n ) {\n return 'dir'\n }\n return 'arg'\n}\n\n/**\n * `JSON.stringify` hardened for debug payloads: circular references collapse\n * to `\"[Circular]\"`, `Error` instances expand to `name`/`message`/`stack`\n * (they would otherwise stringify to `{}`), and `bigint` values become\n * strings (they would otherwise throw). Never throws — falls back to\n * `String(value)` and, if even that coercion throws, a placeholder.\n */\nfunction stringifyMetaSafely(value: unknown): string {\n const seen = new WeakSet<object>()\n try {\n return JSON.stringify(\n value,\n (_key, entry: unknown) => {\n if (typeof entry === 'bigint') return entry.toString()\n if (entry instanceof Error) {\n return {\n name: entry.name,\n message: entry.message,\n stack: entry.stack,\n }\n }\n if (typeof entry === 'object' && entry !== null) {\n if (seen.has(entry)) return '[Circular]'\n seen.add(entry)\n }\n return entry\n },\n 2,\n )\n } catch {\n try {\n return String(value)\n } catch {\n return '[Unserializable meta]'\n }\n }\n}\n\n/**\n * Default `Logger` implementation that routes each level to the matching\n * `console` method:\n *\n * - `debug` → `console.debug`\n * - `info` → `console.info`\n * - `warn` → `console.warn`\n * - `error` → `console.error`\n *\n * When a `meta` object is supplied it is rendered with the strategy that\n * actually surfaces it on the current runtime (see {@link MetaStrategy}):\n * depth-unlimited `console.dir` on Node, circular-safe JSON on Cloudflare\n * Workers, and an extra console argument everywhere else.\n *\n * This is the logger used when `debug` is enabled on any activity and no\n * custom `logger` is supplied via `debug: { logger }`.\n */\nexport class ConsoleLogger implements Logger {\n /** Log a debug-level message; forwards to `console.debug`. */\n debug(message: string, meta?: Record<string, unknown>): void {\n this.emit('debug', message, meta)\n }\n\n /** Log an info-level message; forwards to `console.info`. */\n info(message: string, meta?: Record<string, unknown>): void {\n this.emit('info', message, meta)\n }\n\n /** Log a warning-level message; forwards to `console.warn`. */\n warn(message: string, meta?: Record<string, unknown>): void {\n this.emit('warn', message, meta)\n }\n\n /** Log an error-level message; forwards to `console.error`. */\n error(message: string, meta?: Record<string, unknown>): void {\n this.emit('error', message, meta)\n }\n\n private emit(\n level: 'debug' | 'info' | 'warn' | 'error',\n message: string,\n meta?: Record<string, unknown>,\n ): void {\n if (meta === undefined) {\n console[level](message)\n return\n }\n switch (resolveMetaStrategy()) {\n case 'dir':\n console[level](message)\n console.dir(meta, DIR_OPTIONS)\n break\n case 'json':\n console[level](`${message}\\n${stringifyMetaSafely(meta)}`)\n break\n case 'arg':\n console[level](message, meta)\n break\n }\n }\n}\n"],"names":[],"mappings":"AAQA,MAAM,cAAc,EAAE,OAAO,MAAM,QAAQ,KAAA;AAiB3C,SAAS,sBAAoC;AAG3C,MAAI;AAEF,QAAI,WAAW,WAAW,cAAc,qBAAsB,QAAO;AAAA,EACvE,QAAQ;AAAA,EAGR;AACA,MACE,OAAO,YAAY;AAAA,EAEnB,OAAO,QAAQ,UAAU,SAAS,UAClC;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;AASA,SAAS,oBAAoB,OAAwB;AACnD,QAAM,2BAAW,QAAA;AACjB,MAAI;AACF,WAAO,KAAK;AAAA,MACV;AAAA,MACA,CAAC,MAAM,UAAmB;AACxB,YAAI,OAAO,UAAU,SAAU,QAAO,MAAM,SAAA;AAC5C,YAAI,iBAAiB,OAAO;AAC1B,iBAAO;AAAA,YACL,MAAM,MAAM;AAAA,YACZ,SAAS,MAAM;AAAA,YACf,OAAO,MAAM;AAAA,UAAA;AAAA,QAEjB;AACA,YAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,cAAI,KAAK,IAAI,KAAK,EAAG,QAAO;AAC5B,eAAK,IAAI,KAAK;AAAA,QAChB;AACA,eAAO;AAAA,MACT;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ,QAAQ;AACN,QAAI;AACF,aAAO,OAAO,KAAK;AAAA,IACrB,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AACF;AAmBO,MAAM,cAAgC;AAAA;AAAA,EAE3C,MAAM,SAAiB,MAAsC;AAC3D,SAAK,KAAK,SAAS,SAAS,IAAI;AAAA,EAClC;AAAA;AAAA,EAGA,KAAK,SAAiB,MAAsC;AAC1D,SAAK,KAAK,QAAQ,SAAS,IAAI;AAAA,EACjC;AAAA;AAAA,EAGA,KAAK,SAAiB,MAAsC;AAC1D,SAAK,KAAK,QAAQ,SAAS,IAAI;AAAA,EACjC;AAAA;AAAA,EAGA,MAAM,SAAiB,MAAsC;AAC3D,SAAK,KAAK,SAAS,SAAS,IAAI;AAAA,EAClC;AAAA,EAEQ,KACN,OACA,SACA,MACM;AACN,QAAI,SAAS,QAAW;AACtB,cAAQ,KAAK,EAAE,OAAO;AACtB;AAAA,IACF;AACA,YAAQ,uBAAoB;AAAA,MAC1B,KAAK;AACH,gBAAQ,KAAK,EAAE,OAAO;AACtB,gBAAQ,IAAI,MAAM,WAAW;AAC7B;AAAA,MACF,KAAK;AACH,gBAAQ,KAAK,EAAE,GAAG,OAAO;AAAA,EAAK,oBAAoB,IAAI,CAAC,EAAE;AACzD;AAAA,MACF,KAAK;AACH,gBAAQ,KAAK,EAAE,SAAS,IAAI;AAC5B;AAAA,IAAA;AAAA,EAEN;AACF;"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DebugCategories, Logger } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Fully-resolved categories map. Every flag is a definite boolean (never
|
|
4
|
+
* undefined), produced by `resolveDebugOption` from a `DebugOption`.
|
|
5
|
+
*/
|
|
6
|
+
export type ResolvedCategories = Required<DebugCategories>;
|
|
7
|
+
export declare class InternalLogger {
|
|
8
|
+
private readonly logger;
|
|
9
|
+
private readonly categories;
|
|
10
|
+
constructor(logger: Logger, categories: ResolvedCategories);
|
|
11
|
+
/** Whether a category is enabled. Cheap, safe to call on hot paths. */
|
|
12
|
+
isEnabled(category: keyof ResolvedCategories): boolean;
|
|
13
|
+
private emit;
|
|
14
|
+
/** Log a raw chunk/frame received from a provider SDK. */
|
|
15
|
+
provider(message: string, meta?: Record<string, unknown>): void;
|
|
16
|
+
/** Log a chunk/result yielded to the consumer after middleware. */
|
|
17
|
+
output(message: string, meta?: Record<string, unknown>): void;
|
|
18
|
+
/** Log inputs/outputs around a middleware hook invocation. Chat-only. */
|
|
19
|
+
middleware(message: string, meta?: Record<string, unknown>): void;
|
|
20
|
+
/** Log before/after a tool-call execution. Chat-only. */
|
|
21
|
+
tools(message: string, meta?: Record<string, unknown>): void;
|
|
22
|
+
/** Log an agent-loop iteration marker or phase transition. Chat-only. */
|
|
23
|
+
agentLoop(message: string, meta?: Record<string, unknown>): void;
|
|
24
|
+
/** Log a config transform returned by a middleware `onConfig` hook. Chat-only. */
|
|
25
|
+
config(message: string, meta?: Record<string, unknown>): void;
|
|
26
|
+
/**
|
|
27
|
+
* Log a caught error. Defaults to on even when `debug` is unspecified.
|
|
28
|
+
* Uses the underlying logger's `error` level.
|
|
29
|
+
*/
|
|
30
|
+
errors(message: string, meta?: Record<string, unknown>): void;
|
|
31
|
+
/** Log outgoing request metadata before an adapter SDK call. */
|
|
32
|
+
request(message: string, meta?: Record<string, unknown>): void;
|
|
33
|
+
/**
|
|
34
|
+
* Log a non-fatal misconfiguration or recoverable anomaly. Gated by the
|
|
35
|
+
* `errors` category — on by default (and when `debug` is unspecified), so
|
|
36
|
+
* silent-drop conditions surface, but still silenced by `debug: false`,
|
|
37
|
+
* which honors the "disable everything including errors" contract. Routes to
|
|
38
|
+
* the underlying logger's `warn` level.
|
|
39
|
+
*/
|
|
40
|
+
warn(message: string, meta?: Record<string, unknown>): void;
|
|
41
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const CATEGORY_EMOJI = {
|
|
2
|
+
request: "📤",
|
|
3
|
+
provider: "📥",
|
|
4
|
+
output: "📨",
|
|
5
|
+
middleware: "🧩",
|
|
6
|
+
tools: "🔧",
|
|
7
|
+
agentLoop: "🔁",
|
|
8
|
+
config: "⚙️",
|
|
9
|
+
errors: "❌"
|
|
10
|
+
};
|
|
11
|
+
class InternalLogger {
|
|
12
|
+
constructor(logger, categories) {
|
|
13
|
+
this.logger = logger;
|
|
14
|
+
this.categories = categories;
|
|
15
|
+
}
|
|
16
|
+
logger;
|
|
17
|
+
categories;
|
|
18
|
+
/** Whether a category is enabled. Cheap, safe to call on hot paths. */
|
|
19
|
+
isEnabled(category) {
|
|
20
|
+
return this.categories[category];
|
|
21
|
+
}
|
|
22
|
+
emit(level, category, message, meta) {
|
|
23
|
+
if (!this.categories[category]) return;
|
|
24
|
+
const emoji = CATEGORY_EMOJI[category];
|
|
25
|
+
const prefixed = `${emoji} [tanstack-ai:${category}] ${emoji} ${message}`;
|
|
26
|
+
try {
|
|
27
|
+
if (level === "error") this.logger.error(prefixed, meta);
|
|
28
|
+
else this.logger.debug(prefixed, meta);
|
|
29
|
+
} catch {
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** Log a raw chunk/frame received from a provider SDK. */
|
|
33
|
+
provider(message, meta) {
|
|
34
|
+
this.emit("debug", "provider", message, meta);
|
|
35
|
+
}
|
|
36
|
+
/** Log a chunk/result yielded to the consumer after middleware. */
|
|
37
|
+
output(message, meta) {
|
|
38
|
+
this.emit("debug", "output", message, meta);
|
|
39
|
+
}
|
|
40
|
+
/** Log inputs/outputs around a middleware hook invocation. Chat-only. */
|
|
41
|
+
middleware(message, meta) {
|
|
42
|
+
this.emit("debug", "middleware", message, meta);
|
|
43
|
+
}
|
|
44
|
+
/** Log before/after a tool-call execution. Chat-only. */
|
|
45
|
+
tools(message, meta) {
|
|
46
|
+
this.emit("debug", "tools", message, meta);
|
|
47
|
+
}
|
|
48
|
+
/** Log an agent-loop iteration marker or phase transition. Chat-only. */
|
|
49
|
+
agentLoop(message, meta) {
|
|
50
|
+
this.emit("debug", "agentLoop", message, meta);
|
|
51
|
+
}
|
|
52
|
+
/** Log a config transform returned by a middleware `onConfig` hook. Chat-only. */
|
|
53
|
+
config(message, meta) {
|
|
54
|
+
this.emit("debug", "config", message, meta);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Log a caught error. Defaults to on even when `debug` is unspecified.
|
|
58
|
+
* Uses the underlying logger's `error` level.
|
|
59
|
+
*/
|
|
60
|
+
errors(message, meta) {
|
|
61
|
+
this.emit("error", "errors", message, meta);
|
|
62
|
+
}
|
|
63
|
+
/** Log outgoing request metadata before an adapter SDK call. */
|
|
64
|
+
request(message, meta) {
|
|
65
|
+
this.emit("debug", "request", message, meta);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Log a non-fatal misconfiguration or recoverable anomaly. Gated by the
|
|
69
|
+
* `errors` category — on by default (and when `debug` is unspecified), so
|
|
70
|
+
* silent-drop conditions surface, but still silenced by `debug: false`,
|
|
71
|
+
* which honors the "disable everything including errors" contract. Routes to
|
|
72
|
+
* the underlying logger's `warn` level.
|
|
73
|
+
*/
|
|
74
|
+
warn(message, meta) {
|
|
75
|
+
if (!this.categories.errors) return;
|
|
76
|
+
const prefixed = `⚠️ [tanstack-ai:warn] ⚠️ ${message}`;
|
|
77
|
+
try {
|
|
78
|
+
this.logger.warn(prefixed, meta);
|
|
79
|
+
} catch {
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
InternalLogger
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=internal-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal-logger.js","sources":["../../../src/logger/internal-logger.ts"],"sourcesContent":["import type { DebugCategories, Logger } from './types'\n\n/**\n * Fully-resolved categories map. Every flag is a definite boolean (never\n * undefined), produced by `resolveDebugOption` from a `DebugOption`.\n */\nexport type ResolvedCategories = Required<DebugCategories>\n\n/**\n * Package-internal logger wrapper used by every activity and adapter in\n * `@tanstack/ai`. Wraps a user-supplied (or default `ConsoleLogger`) `Logger`\n * plus a fully-resolved per-category map. Each category has a dedicated\n * method that no-ops when its flag is `false`, or prepends a\n * `[tanstack-ai:<category>] ` prefix and calls the underlying logger's\n * `error` (for the `errors` category) or `debug` (for everything else).\n *\n * Not exported from the package root. Adapter packages consume it via the\n * `@tanstack/ai/adapter-internals` subpath export.\n */\n/**\n * Emoji marker per category — bracketing the `[tanstack-ai:<cat>]` tag on\n * both sides makes it trivial to visually pick out a category when scanning\n * dense streaming logs.\n */\nconst CATEGORY_EMOJI: Record<keyof ResolvedCategories, string> = {\n request: '📤',\n provider: '📥',\n output: '📨',\n middleware: '🧩',\n tools: '🔧',\n agentLoop: '🔁',\n config: '⚙️',\n errors: '❌',\n}\n\nexport class InternalLogger {\n constructor(\n private readonly logger: Logger,\n private readonly categories: ResolvedCategories,\n ) {}\n\n /** Whether a category is enabled. Cheap, safe to call on hot paths. */\n isEnabled(category: keyof ResolvedCategories): boolean {\n return this.categories[category]\n }\n\n private emit(\n level: 'debug' | 'error',\n category: keyof ResolvedCategories,\n message: string,\n meta?: Record<string, unknown>,\n ): void {\n if (!this.categories[category]) return\n const emoji = CATEGORY_EMOJI[category]\n const prefixed = `${emoji} [tanstack-ai:${category}] ${emoji} ${message}`\n try {\n if (level === 'error') this.logger.error(prefixed, meta)\n else this.logger.debug(prefixed, meta)\n } catch {\n // User-supplied logger threw; swallow so we never mask the original\n // error that triggered this log call.\n }\n }\n\n /** Log a raw chunk/frame received from a provider SDK. */\n provider(message: string, meta?: Record<string, unknown>): void {\n this.emit('debug', 'provider', message, meta)\n }\n\n /** Log a chunk/result yielded to the consumer after middleware. */\n output(message: string, meta?: Record<string, unknown>): void {\n this.emit('debug', 'output', message, meta)\n }\n\n /** Log inputs/outputs around a middleware hook invocation. Chat-only. */\n middleware(message: string, meta?: Record<string, unknown>): void {\n this.emit('debug', 'middleware', message, meta)\n }\n\n /** Log before/after a tool-call execution. Chat-only. */\n tools(message: string, meta?: Record<string, unknown>): void {\n this.emit('debug', 'tools', message, meta)\n }\n\n /** Log an agent-loop iteration marker or phase transition. Chat-only. */\n agentLoop(message: string, meta?: Record<string, unknown>): void {\n this.emit('debug', 'agentLoop', message, meta)\n }\n\n /** Log a config transform returned by a middleware `onConfig` hook. Chat-only. */\n config(message: string, meta?: Record<string, unknown>): void {\n this.emit('debug', 'config', message, meta)\n }\n\n /**\n * Log a caught error. Defaults to on even when `debug` is unspecified.\n * Uses the underlying logger's `error` level.\n */\n errors(message: string, meta?: Record<string, unknown>): void {\n this.emit('error', 'errors', message, meta)\n }\n\n /** Log outgoing request metadata before an adapter SDK call. */\n request(message: string, meta?: Record<string, unknown>): void {\n this.emit('debug', 'request', message, meta)\n }\n\n /**\n * Log a non-fatal misconfiguration or recoverable anomaly. Gated by the\n * `errors` category — on by default (and when `debug` is unspecified), so\n * silent-drop conditions surface, but still silenced by `debug: false`,\n * which honors the \"disable everything including errors\" contract. Routes to\n * the underlying logger's `warn` level.\n */\n warn(message: string, meta?: Record<string, unknown>): void {\n if (!this.categories.errors) return\n const prefixed = `⚠️ [tanstack-ai:warn] ⚠️ ${message}`\n try {\n this.logger.warn(prefixed, meta)\n } catch {\n // User-supplied logger threw; swallow so a broken logger never masks the\n // condition we were trying to surface.\n }\n }\n}\n"],"names":[],"mappings":"AAwBA,MAAM,iBAA2D;AAAA,EAC/D,SAAS;AAAA,EACT,UAAU;AAAA,EACV,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,OAAO;AAAA,EACP,WAAW;AAAA,EACX,QAAQ;AAAA,EACR,QAAQ;AACV;AAEO,MAAM,eAAe;AAAA,EAC1B,YACmB,QACA,YACjB;AAFiB,SAAA,SAAA;AACA,SAAA,aAAA;AAAA,EAChB;AAAA,EAFgB;AAAA,EACA;AAAA;AAAA,EAInB,UAAU,UAA6C;AACrD,WAAO,KAAK,WAAW,QAAQ;AAAA,EACjC;AAAA,EAEQ,KACN,OACA,UACA,SACA,MACM;AACN,QAAI,CAAC,KAAK,WAAW,QAAQ,EAAG;AAChC,UAAM,QAAQ,eAAe,QAAQ;AACrC,UAAM,WAAW,GAAG,KAAK,iBAAiB,QAAQ,KAAK,KAAK,IAAI,OAAO;AACvE,QAAI;AACF,UAAI,UAAU,QAAS,MAAK,OAAO,MAAM,UAAU,IAAI;AAAA,UAClD,MAAK,OAAO,MAAM,UAAU,IAAI;AAAA,IACvC,QAAQ;AAAA,IAGR;AAAA,EACF;AAAA;AAAA,EAGA,SAAS,SAAiB,MAAsC;AAC9D,SAAK,KAAK,SAAS,YAAY,SAAS,IAAI;AAAA,EAC9C;AAAA;AAAA,EAGA,OAAO,SAAiB,MAAsC;AAC5D,SAAK,KAAK,SAAS,UAAU,SAAS,IAAI;AAAA,EAC5C;AAAA;AAAA,EAGA,WAAW,SAAiB,MAAsC;AAChE,SAAK,KAAK,SAAS,cAAc,SAAS,IAAI;AAAA,EAChD;AAAA;AAAA,EAGA,MAAM,SAAiB,MAAsC;AAC3D,SAAK,KAAK,SAAS,SAAS,SAAS,IAAI;AAAA,EAC3C;AAAA;AAAA,EAGA,UAAU,SAAiB,MAAsC;AAC/D,SAAK,KAAK,SAAS,aAAa,SAAS,IAAI;AAAA,EAC/C;AAAA;AAAA,EAGA,OAAO,SAAiB,MAAsC;AAC5D,SAAK,KAAK,SAAS,UAAU,SAAS,IAAI;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,SAAiB,MAAsC;AAC5D,SAAK,KAAK,SAAS,UAAU,SAAS,IAAI;AAAA,EAC5C;AAAA;AAAA,EAGA,QAAQ,SAAiB,MAAsC;AAC7D,SAAK,KAAK,SAAS,WAAW,SAAS,IAAI;AAAA,EAC7C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,KAAK,SAAiB,MAAsC;AAC1D,QAAI,CAAC,KAAK,WAAW,OAAQ;AAC7B,UAAM,WAAW,4BAA4B,OAAO;AACpD,QAAI;AACF,WAAK,OAAO,KAAK,UAAU,IAAI;AAAA,IACjC,QAAQ;AAAA,IAGR;AAAA,EACF;AACF;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { InternalLogger } from './internal-logger.js';
|
|
2
|
+
import { DebugOption } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Normalize a `DebugOption` into an `InternalLogger` ready to be threaded
|
|
5
|
+
* through the library's activities and adapters. See the `DebugOption`
|
|
6
|
+
* resolution table in the spec for the complete rules.
|
|
7
|
+
*
|
|
8
|
+
* - `undefined`: only the `errors` category is enabled; default `ConsoleLogger`.
|
|
9
|
+
* - `true`: all categories enabled; default `ConsoleLogger`.
|
|
10
|
+
* - `false`: all categories disabled (including `errors`); default `ConsoleLogger`.
|
|
11
|
+
* - `DebugConfig`: each unspecified category defaults to `true`; an optional
|
|
12
|
+
* `logger` replaces the default `ConsoleLogger`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveDebugOption(debug: DebugOption | undefined): InternalLogger;
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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;"}
|
|
@@ -0,0 +1,75 @@
|
|
|
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;
|
|
@@ -0,0 +1,77 @@
|
|
|
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;
|