ai.matey.utils 0.2.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.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/conversation-history.js +139 -0
  3. package/dist/cjs/conversation-history.js.map +1 -0
  4. package/dist/cjs/index.js +42 -0
  5. package/dist/cjs/index.js.map +1 -0
  6. package/dist/cjs/model-cache.js +163 -0
  7. package/dist/cjs/model-cache.js.map +1 -0
  8. package/dist/cjs/parameter-normalizer.js +451 -0
  9. package/dist/cjs/parameter-normalizer.js.map +1 -0
  10. package/dist/cjs/streaming-modes.js +277 -0
  11. package/dist/cjs/streaming-modes.js.map +1 -0
  12. package/dist/cjs/streaming.js +892 -0
  13. package/dist/cjs/streaming.js.map +1 -0
  14. package/dist/cjs/structured-output.js +398 -0
  15. package/dist/cjs/structured-output.js.map +1 -0
  16. package/dist/cjs/system-message.js +222 -0
  17. package/dist/cjs/system-message.js.map +1 -0
  18. package/dist/cjs/validation.js +534 -0
  19. package/dist/cjs/validation.js.map +1 -0
  20. package/dist/cjs/warnings.js +301 -0
  21. package/dist/cjs/warnings.js.map +1 -0
  22. package/dist/esm/conversation-history.js +134 -0
  23. package/dist/esm/conversation-history.js.map +1 -0
  24. package/dist/esm/index.js +26 -0
  25. package/dist/esm/index.js.map +1 -0
  26. package/dist/esm/model-cache.js +158 -0
  27. package/dist/esm/model-cache.js.map +1 -0
  28. package/dist/esm/parameter-normalizer.js +434 -0
  29. package/dist/esm/parameter-normalizer.js.map +1 -0
  30. package/dist/esm/streaming-modes.js +265 -0
  31. package/dist/esm/streaming-modes.js.map +1 -0
  32. package/dist/esm/streaming.js +860 -0
  33. package/dist/esm/streaming.js.map +1 -0
  34. package/dist/esm/structured-output.js +387 -0
  35. package/dist/esm/structured-output.js.map +1 -0
  36. package/dist/esm/system-message.js +213 -0
  37. package/dist/esm/system-message.js.map +1 -0
  38. package/dist/esm/validation.js +523 -0
  39. package/dist/esm/validation.js.map +1 -0
  40. package/dist/esm/warnings.js +284 -0
  41. package/dist/esm/warnings.js.map +1 -0
  42. package/dist/types/conversation-history.d.ts +70 -0
  43. package/dist/types/conversation-history.d.ts.map +1 -0
  44. package/dist/types/index.d.ts +17 -0
  45. package/dist/types/index.d.ts.map +1 -0
  46. package/dist/types/model-cache.d.ts +88 -0
  47. package/dist/types/model-cache.d.ts.map +1 -0
  48. package/dist/types/parameter-normalizer.d.ts +154 -0
  49. package/dist/types/parameter-normalizer.d.ts.map +1 -0
  50. package/dist/types/streaming-modes.d.ts +139 -0
  51. package/dist/types/streaming-modes.d.ts.map +1 -0
  52. package/dist/types/streaming.d.ts +384 -0
  53. package/dist/types/streaming.d.ts.map +1 -0
  54. package/dist/types/structured-output.d.ts +157 -0
  55. package/dist/types/structured-output.d.ts.map +1 -0
  56. package/dist/types/system-message.d.ts +78 -0
  57. package/dist/types/system-message.d.ts.map +1 -0
  58. package/dist/types/validation.d.ts +46 -0
  59. package/dist/types/validation.d.ts.map +1 -0
  60. package/dist/types/warnings.d.ts +149 -0
  61. package/dist/types/warnings.d.ts.map +1 -0
  62. package/package.json +75 -0
  63. package/readme.md +280 -0
@@ -0,0 +1,78 @@
1
+ /**
2
+ * System Message Normalization Utilities
3
+ *
4
+ * Handles different provider strategies for system messages:
5
+ * - separate-parameter: System as separate parameter (Anthropic)
6
+ * - in-messages: System messages in message array (OpenAI, Gemini)
7
+ * - prepend-user: Prepend system to first user message (some providers)
8
+ * - not-supported: Strip system messages (rare)
9
+ *
10
+ * @module
11
+ */
12
+ import type { IRMessage, SystemMessageStrategy } from 'ai.matey.types';
13
+ /**
14
+ * Extract system messages from message array.
15
+ *
16
+ * Returns both the system messages and the remaining non-system messages.
17
+ *
18
+ * @param messages Message array
19
+ * @returns Object with system messages and remaining messages
20
+ */
21
+ export declare function extractSystemMessages(messages: readonly IRMessage[]): {
22
+ systemMessages: IRMessage[];
23
+ otherMessages: IRMessage[];
24
+ };
25
+ /**
26
+ * Combine multiple system messages into a single string.
27
+ *
28
+ * @param systemMessages Array of system messages
29
+ * @param separator Separator between messages (default: double newline)
30
+ * @returns Combined system message content
31
+ */
32
+ export declare function combineSystemMessages(systemMessages: readonly IRMessage[], separator?: string): string;
33
+ /**
34
+ * Get first system message as string (for providers that only support one).
35
+ *
36
+ * @param messages Message array
37
+ * @returns First system message content or undefined
38
+ */
39
+ export declare function getFirstSystemMessage(messages: readonly IRMessage[]): string | undefined;
40
+ /**
41
+ * Normalize system messages according to provider strategy.
42
+ *
43
+ * @param messages Original message array
44
+ * @param strategy System message strategy
45
+ * @param supportsMultiple Whether provider supports multiple system messages
46
+ * @returns Normalized messages and optional system parameter
47
+ */
48
+ export declare function normalizeSystemMessages(messages: readonly IRMessage[], strategy: SystemMessageStrategy, supportsMultiple?: boolean): {
49
+ messages: IRMessage[];
50
+ systemParameter?: string;
51
+ };
52
+ /**
53
+ * Add system message to message array.
54
+ *
55
+ * Handles prepending or replacing existing system messages based on strategy.
56
+ *
57
+ * @param messages Existing message array
58
+ * @param systemContent System message content to add
59
+ * @param strategy System message strategy
60
+ * @param supportsMultiple Whether provider supports multiple system messages
61
+ * @returns New message array with system message added
62
+ */
63
+ export declare function addSystemMessage(messages: readonly IRMessage[], systemContent: string, strategy?: SystemMessageStrategy, supportsMultiple?: boolean): IRMessage[];
64
+ /**
65
+ * Check if messages contain any system messages.
66
+ *
67
+ * @param messages Message array
68
+ * @returns true if any message has role 'system'
69
+ */
70
+ export declare function hasSystemMessages(messages: readonly IRMessage[]): boolean;
71
+ /**
72
+ * Count system messages in array.
73
+ *
74
+ * @param messages Message array
75
+ * @returns Number of system messages
76
+ */
77
+ export declare function countSystemMessages(messages: readonly IRMessage[]): number;
78
+ //# sourceMappingURL=system-message.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"system-message.d.ts","sourceRoot":"","sources":["../../src/system-message.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAMvE;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,SAAS,SAAS,EAAE,GAAG;IACrE,cAAc,EAAE,SAAS,EAAE,CAAC;IAC5B,aAAa,EAAE,SAAS,EAAE,CAAC;CAC5B,CAaA;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,cAAc,EAAE,SAAS,SAAS,EAAE,EACpC,SAAS,SAAS,GACjB,MAAM,CAcR;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,SAAS,SAAS,EAAE,GAAG,MAAM,GAAG,SAAS,CAiBxF;AAMD;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,SAAS,SAAS,EAAE,EAC9B,QAAQ,EAAE,qBAAqB,EAC/B,gBAAgB,GAAE,OAAe,GAChC;IACD,QAAQ,EAAE,SAAS,EAAE,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CA8EA;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,SAAS,SAAS,EAAE,EAC9B,aAAa,EAAE,MAAM,EACrB,QAAQ,GAAE,qBAAqC,EAC/C,gBAAgB,GAAE,OAAe,GAChC,SAAS,EAAE,CA8Bb;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,SAAS,SAAS,EAAE,GAAG,OAAO,CAEzE;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,SAAS,SAAS,EAAE,GAAG,MAAM,CAE1E"}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Validation Utilities
3
+ *
4
+ * Core validation functions for IR requests, messages, and parameters.
5
+ *
6
+ * @module
7
+ */
8
+ import type { IRChatRequest, IRMessage, IRParameters, MessageRole } from 'ai.matey.types';
9
+ import type { ErrorProvenance } from 'ai.matey.types';
10
+ /**
11
+ * Validate message role.
12
+ */
13
+ export declare function isValidMessageRole(role: string): role is MessageRole;
14
+ /**
15
+ * Validate message content.
16
+ */
17
+ export declare function validateMessageContent(content: unknown, provenance?: ErrorProvenance): void;
18
+ /**
19
+ * Validate a single message.
20
+ */
21
+ export declare function validateMessage(message: unknown, provenance?: ErrorProvenance): asserts message is IRMessage;
22
+ /**
23
+ * Validate messages array.
24
+ */
25
+ export declare function validateMessages(messages: unknown, provenance?: ErrorProvenance): asserts messages is IRMessage[];
26
+ /**
27
+ * Validate temperature parameter.
28
+ */
29
+ export declare function validateTemperature(temperature: unknown, provenance?: ErrorProvenance): void;
30
+ /**
31
+ * Validate maxTokens parameter.
32
+ */
33
+ export declare function validateMaxTokens(maxTokens: unknown, provenance?: ErrorProvenance): void;
34
+ /**
35
+ * Validate topP parameter.
36
+ */
37
+ export declare function validateTopP(topP: unknown, provenance?: ErrorProvenance): void;
38
+ /**
39
+ * Validate parameters object.
40
+ */
41
+ export declare function validateParameters(parameters: unknown, provenance?: ErrorProvenance): asserts parameters is IRParameters;
42
+ /**
43
+ * Validate IR chat request.
44
+ */
45
+ export declare function validateIRChatRequest(request: unknown, provenance?: ErrorProvenance): asserts request is IRChatRequest;
46
+ //# sourceMappingURL=validation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/validation.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,aAAa,EACb,SAAS,EACT,YAAY,EAEZ,WAAW,EACZ,MAAM,gBAAgB,CAAC;AAExB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAMtD;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,WAAW,CAEpE;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,eAAe,GAAG,IAAI,CAuF3F;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,OAAO,EAChB,UAAU,CAAC,EAAE,eAAe,GAC3B,OAAO,CAAC,OAAO,IAAI,SAAS,CAsE9B;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,OAAO,EACjB,UAAU,CAAC,EAAE,eAAe,GAC3B,OAAO,CAAC,QAAQ,IAAI,SAAS,EAAE,CAqDjC;AAMD;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,eAAe,GAAG,IAAI,CAoC5F;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,eAAe,GAAG,IAAI,CAoCxF;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,eAAe,GAAG,IAAI,CAoC9E;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,OAAO,EACnB,UAAU,CAAC,EAAE,eAAe,GAC3B,OAAO,CAAC,UAAU,IAAI,YAAY,CA2FpC;AAMD;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,OAAO,EAChB,UAAU,CAAC,EAAE,eAAe,GAC3B,OAAO,CAAC,OAAO,IAAI,aAAa,CA2FlC"}
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Warning Utilities
3
+ *
4
+ * Helpers for creating, formatting, and managing semantic drift warnings.
5
+ *
6
+ * @module
7
+ */
8
+ import type { IRWarning, WarningSeverity, WarningCategory } from 'ai.matey.types';
9
+ /**
10
+ * Create a warning object.
11
+ *
12
+ * @param category Warning category
13
+ * @param message Human-readable message
14
+ * @param options Additional warning options
15
+ * @returns Warning object
16
+ */
17
+ export declare function createWarning(category: WarningCategory, message: string, options?: {
18
+ severity?: WarningSeverity;
19
+ field?: string;
20
+ originalValue?: unknown;
21
+ transformedValue?: unknown;
22
+ source?: string;
23
+ details?: Record<string, unknown>;
24
+ }): IRWarning;
25
+ /**
26
+ * Create a parameter normalization warning.
27
+ *
28
+ * @param field Parameter field name
29
+ * @param originalValue Original value
30
+ * @param transformedValue Transformed value
31
+ * @param message Optional custom message
32
+ * @param source Source adapter
33
+ * @returns Warning object
34
+ */
35
+ export declare function createNormalizationWarning(field: string, originalValue: unknown, transformedValue: unknown, message?: string, source?: string): IRWarning;
36
+ /**
37
+ * Create a parameter clamping warning.
38
+ *
39
+ * @param field Parameter field name
40
+ * @param originalValue Original value
41
+ * @param clampedValue Clamped value
42
+ * @param min Minimum allowed value
43
+ * @param max Maximum allowed value
44
+ * @param source Source adapter
45
+ * @returns Warning object
46
+ */
47
+ export declare function createClampingWarning(field: string, originalValue: unknown, clampedValue: unknown, min: number, max: number, source?: string): IRWarning;
48
+ /**
49
+ * Create an unsupported parameter warning.
50
+ *
51
+ * @param field Parameter field name
52
+ * @param value Parameter value
53
+ * @param source Source adapter
54
+ * @returns Warning object
55
+ */
56
+ export declare function createUnsupportedParameterWarning(field: string, value: unknown, source?: string): IRWarning;
57
+ /**
58
+ * Create a capability unsupported warning.
59
+ *
60
+ * @param capability Capability name
61
+ * @param source Source adapter
62
+ * @returns Warning object
63
+ */
64
+ export declare function createUnsupportedCapabilityWarning(capability: string, source?: string): IRWarning;
65
+ /**
66
+ * Create a token limit exceeded warning.
67
+ *
68
+ * @param estimatedTokens Estimated token count
69
+ * @param maxTokens Maximum tokens allowed
70
+ * @param source Source adapter
71
+ * @returns Warning object
72
+ */
73
+ export declare function createTokenLimitWarning(estimatedTokens: number, maxTokens: number, source?: string): IRWarning;
74
+ /**
75
+ * Create a stop sequences truncated warning.
76
+ *
77
+ * @param originalCount Original number of stop sequences
78
+ * @param maxCount Maximum allowed
79
+ * @param source Source adapter
80
+ * @returns Warning object
81
+ */
82
+ export declare function createStopSequencesTruncatedWarning(originalCount: number, maxCount: number, source?: string): IRWarning;
83
+ /**
84
+ * Merge warning arrays, removing duplicates.
85
+ *
86
+ * @param warningArrays Arrays of warnings to merge
87
+ * @returns Merged and deduplicated warnings
88
+ */
89
+ export declare function mergeWarnings(...warningArrays: Array<readonly IRWarning[] | undefined>): IRWarning[];
90
+ /**
91
+ * Filter warnings by severity.
92
+ *
93
+ * @param warnings Warnings to filter
94
+ * @param minSeverity Minimum severity level
95
+ * @returns Filtered warnings
96
+ */
97
+ export declare function filterWarningsBySeverity(warnings: readonly IRWarning[], minSeverity: WarningSeverity): IRWarning[];
98
+ /**
99
+ * Filter warnings by category.
100
+ *
101
+ * @param warnings Warnings to filter
102
+ * @param categories Categories to include
103
+ * @returns Filtered warnings
104
+ */
105
+ export declare function filterWarningsByCategory(warnings: readonly IRWarning[], categories: readonly WarningCategory[]): IRWarning[];
106
+ /**
107
+ * Group warnings by category.
108
+ *
109
+ * @param warnings Warnings to group
110
+ * @returns Map of category to warnings
111
+ */
112
+ export declare function groupWarningsByCategory(warnings: readonly IRWarning[]): Map<WarningCategory, IRWarning[]>;
113
+ /**
114
+ * Check if warnings array contains a specific warning type.
115
+ *
116
+ * @param warnings Warnings to check
117
+ * @param category Warning category to look for
118
+ * @param field Optional field name to match
119
+ * @returns true if warning exists
120
+ */
121
+ export declare function hasWarning(warnings: readonly IRWarning[] | undefined, category: WarningCategory, field?: string): boolean;
122
+ /**
123
+ * Format a single warning as a string.
124
+ *
125
+ * @param warning Warning to format
126
+ * @param includeDetails Include full details
127
+ * @returns Formatted warning string
128
+ */
129
+ export declare function formatWarning(warning: IRWarning, includeDetails?: boolean): string;
130
+ /**
131
+ * Format multiple warnings as a string.
132
+ *
133
+ * @param warnings Warnings to format
134
+ * @param includeDetails Include full details
135
+ * @returns Formatted warnings string
136
+ */
137
+ export declare function formatWarnings(warnings: readonly IRWarning[], includeDetails?: boolean): string;
138
+ /**
139
+ * Get a summary of warnings by category.
140
+ *
141
+ * @param warnings Warnings to summarize
142
+ * @returns Summary object with counts
143
+ */
144
+ export declare function getWarningSummary(warnings: readonly IRWarning[]): {
145
+ total: number;
146
+ byCategory: Record<WarningCategory, number>;
147
+ bySeverity: Record<WarningSeverity, number>;
148
+ };
149
+ //# sourceMappingURL=warnings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"warnings.d.ts","sourceRoot":"","sources":["../../src/warnings.ts"],"names":[],"mappings":"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"}
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "ai.matey.utils",
3
+ "version": "0.2.0",
4
+ "description": "Utility functions for AI Matey - Universal AI Adapter System",
5
+ "type": "module",
6
+ "main": "./dist/cjs/index.js",
7
+ "module": "./dist/esm/index.js",
8
+ "types": "./dist/types/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/types/index.d.ts",
13
+ "default": "./dist/esm/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/types/index.d.ts",
17
+ "default": "./dist/cjs/index.js"
18
+ }
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "readme.md",
24
+ "CHANGELOG.md",
25
+ "LICENSE"
26
+ ],
27
+ "scripts": {
28
+ "build": "npm run build:esm && npm run build:cjs && npm run build:types",
29
+ "build:esm": "tsc -p tsconfig.esm.json",
30
+ "build:cjs": "tsc -p tsconfig.cjs.json",
31
+ "build:types": "tsc -p tsconfig.types.json",
32
+ "clean": "rm -rf dist",
33
+ "typecheck": "tsc --noEmit",
34
+ "lint": "eslint src --ext .ts",
35
+ "lint:fix": "eslint src --ext .ts --fix",
36
+ "test": "vitest run",
37
+ "test:watch": "vitest"
38
+ },
39
+ "dependencies": {
40
+ "ai.matey.errors": "*",
41
+ "ai.matey.types": "*"
42
+ },
43
+ "peerDependencies": {
44
+ "zod": "^3.0.0 || ^4.0.0"
45
+ },
46
+ "peerDependenciesMeta": {
47
+ "zod": {
48
+ "optional": true
49
+ }
50
+ },
51
+ "devDependencies": {
52
+ "typescript": "^5.9.3",
53
+ "vitest": "^3.2.4"
54
+ },
55
+ "keywords": [
56
+ "ai",
57
+ "llm",
58
+ "utilities",
59
+ "ai-matey"
60
+ ],
61
+ "author": "AI Matey",
62
+ "license": "MIT",
63
+ "homepage": "https://github.com/johnhenry/ai.matey#readme",
64
+ "bugs": {
65
+ "url": "https://github.com/johnhenry/ai.matey/issues"
66
+ },
67
+ "repository": {
68
+ "type": "git",
69
+ "url": "git+https://github.com/johnhenry/ai.matey.git",
70
+ "directory": "packages/ai.matey.utils"
71
+ },
72
+ "engines": {
73
+ "node": ">=18.0.0"
74
+ }
75
+ }
package/readme.md ADDED
@@ -0,0 +1,280 @@
1
+ # ai.matey.utils
2
+
3
+ Shared utility functions for streaming, validation, and conversions.
4
+
5
+ Part of the [ai.matey](https://github.com/johnhenry/ai.matey) monorepo.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install ai.matey.utils
11
+ ```
12
+
13
+ ## Stream Utilities
14
+
15
+ Comprehensive utilities for working with IR chat streams.
16
+
17
+ ### Basic Stream Operations
18
+
19
+ ```typescript
20
+ import {
21
+ collectStream, // Collect chunks into array
22
+ collectStreamFull, // Collect with rich metadata
23
+ streamToText, // Get accumulated text
24
+ streamToResponse, // Convert to IR response
25
+ } from 'ai.matey.utils';
26
+
27
+ // Collect all chunks
28
+ const chunks = await collectStream(stream);
29
+
30
+ // Collect with full metadata
31
+ const result = await collectStreamFull(stream);
32
+ console.log(result.content); // Full text
33
+ console.log(result.message); // IR message
34
+ console.log(result.finishReason); // 'stop', 'length', etc.
35
+ console.log(result.usage); // Token counts
36
+
37
+ // Get just the text
38
+ const text = await streamToText(stream);
39
+ ```
40
+
41
+ ### Processing Streams
42
+
43
+ ```typescript
44
+ import { processStream, streamToLines, streamToTextIterator } from 'ai.matey.utils';
45
+
46
+ // Process with callbacks
47
+ const result = await processStream(stream, {
48
+ onStart: (requestId) => console.log('Started:', requestId),
49
+ onContent: (delta, accumulated) => process.stdout.write(delta),
50
+ onDone: (result) => console.log('\nTokens:', result.usage?.totalTokens),
51
+ onError: (error) => console.error('Error:', error),
52
+ });
53
+
54
+ // Iterate over text chunks
55
+ for await (const text of streamToTextIterator(stream)) {
56
+ process.stdout.write(text);
57
+ }
58
+
59
+ // Buffer and yield complete lines
60
+ for await (const line of streamToLines(stream)) {
61
+ console.log('Line:', line);
62
+ }
63
+ ```
64
+
65
+ ### Transforming Streams
66
+
67
+ ```typescript
68
+ import {
69
+ transformStream,
70
+ filterStream,
71
+ mapStream,
72
+ tapStream,
73
+ } from 'ai.matey.utils';
74
+
75
+ // Transform chunks
76
+ const transformed = transformStream(stream, (chunk) => ({
77
+ ...chunk,
78
+ delta: chunk.type === 'content' ? chunk.delta.toUpperCase() : chunk.delta,
79
+ }));
80
+
81
+ // Filter chunks
82
+ const contentOnly = filterStream(stream, (chunk) => chunk.type === 'content');
83
+
84
+ // Map chunks
85
+ const textOnly = mapStream(stream, (chunk) =>
86
+ chunk.type === 'content' ? chunk.delta : ''
87
+ );
88
+
89
+ // Tap for side effects (logging, etc.)
90
+ const logged = tapStream(stream, (chunk) => console.log('Chunk:', chunk.type));
91
+ ```
92
+
93
+ ### Stream Control
94
+
95
+ ```typescript
96
+ import {
97
+ throttleStream,
98
+ rateLimitStream,
99
+ teeStream,
100
+ splitStream,
101
+ } from 'ai.matey.utils';
102
+
103
+ // Throttle updates (batches content chunks)
104
+ // Great for limiting UI update frequency
105
+ for await (const chunk of throttleStream(stream, 50)) {
106
+ updateUI(chunk); // Max every 50ms
107
+ }
108
+
109
+ // Rate limit (delays chunks to max N/second)
110
+ const limited = rateLimitStream(stream, 10); // Max 10 chunks/second
111
+
112
+ // Split stream for parallel processing
113
+ const [stream1, stream2] = teeStream(stream, 2);
114
+
115
+ await Promise.all([
116
+ processStream(stream1, { onContent: (d) => logger.log(d) }),
117
+ processStream(stream2, { onContent: (d) => ui.append(d) }),
118
+ ]);
119
+ ```
120
+
121
+ ### Stream Validation
122
+
123
+ ```typescript
124
+ import { validateStream, validateChunkSequence } from 'ai.matey.utils';
125
+
126
+ // Validate stream structure
127
+ const validated = validateStream(stream, {
128
+ requireStart: true,
129
+ requireDone: true,
130
+ requireContent: true,
131
+ });
132
+
133
+ // Check if chunk sequence is valid
134
+ const chunks = await collectStream(stream);
135
+ const isValid = validateChunkSequence(chunks);
136
+ ```
137
+
138
+ ### Error Handling
139
+
140
+ ```typescript
141
+ import { catchStreamErrors, streamWithTimeout, createStreamError } from 'ai.matey.utils';
142
+
143
+ // Catch and handle errors
144
+ const safe = catchStreamErrors(stream, (error) => {
145
+ console.error('Stream error:', error);
146
+ });
147
+
148
+ // Add timeout
149
+ const withTimeout = streamWithTimeout(stream, 30000); // 30s timeout
150
+
151
+ // Create error chunks
152
+ const errorChunk = createStreamError(new Error('Something went wrong'));
153
+ ```
154
+
155
+ ## Stream Accumulator
156
+
157
+ Build responses incrementally from chunks:
158
+
159
+ ```typescript
160
+ import {
161
+ createStreamAccumulator,
162
+ accumulateChunk,
163
+ accumulatorToMessage,
164
+ accumulatorToResponse,
165
+ } from 'ai.matey.utils';
166
+
167
+ const accumulator = createStreamAccumulator();
168
+
169
+ for await (const chunk of stream) {
170
+ accumulateChunk(accumulator, chunk);
171
+ console.log('Current text:', accumulator.content);
172
+ }
173
+
174
+ const message = accumulatorToMessage(accumulator);
175
+ const response = accumulatorToResponse(accumulator);
176
+ ```
177
+
178
+ ## Content Utilities
179
+
180
+ ```typescript
181
+ import { getContentDeltas, isContentChunk, isDoneChunk, isErrorChunk } from 'ai.matey.utils';
182
+
183
+ // Get just the text deltas
184
+ for await (const delta of getContentDeltas(stream)) {
185
+ process.stdout.write(delta);
186
+ }
187
+
188
+ // Type guards
189
+ for await (const chunk of stream) {
190
+ if (isContentChunk(chunk)) {
191
+ console.log('Content:', chunk.delta);
192
+ } else if (isDoneChunk(chunk)) {
193
+ console.log('Done:', chunk.finishReason);
194
+ } else if (isErrorChunk(chunk)) {
195
+ console.error('Error:', chunk.error);
196
+ }
197
+ }
198
+ ```
199
+
200
+ ## Web Stream Conversion
201
+
202
+ ```typescript
203
+ import {
204
+ asyncGeneratorToReadableStream,
205
+ readableStreamToAsyncGenerator,
206
+ } from 'ai.matey.utils';
207
+
208
+ // Convert async generator to Web ReadableStream
209
+ const readable = asyncGeneratorToReadableStream(stream);
210
+
211
+ // Convert back to async generator
212
+ const generator = readableStreamToAsyncGenerator(readable);
213
+ ```
214
+
215
+ ## Types
216
+
217
+ ```typescript
218
+ import type {
219
+ CollectedStream, // Rich result from collectStreamFull
220
+ ProcessStreamOptions, // Options for processStream
221
+ StreamValidationOptions,
222
+ } from 'ai.matey.utils';
223
+ ```
224
+
225
+ ## API Reference
226
+
227
+ ### Collection Functions
228
+
229
+ | Function | Description |
230
+ |----------|-------------|
231
+ | `collectStream(stream)` | Collect chunks into array |
232
+ | `collectStreamFull(stream)` | Collect with rich metadata |
233
+ | `streamToText(stream)` | Get accumulated text |
234
+ | `streamToResponse(stream)` | Convert to IR response |
235
+
236
+ ### Processing Functions
237
+
238
+ | Function | Description |
239
+ |----------|-------------|
240
+ | `processStream(stream, options)` | Process with callbacks |
241
+ | `streamToLines(stream)` | Yield complete lines |
242
+ | `streamToTextIterator(stream)` | Yield text deltas |
243
+ | `getContentDeltas(stream)` | Yield only content |
244
+
245
+ ### Transform Functions
246
+
247
+ | Function | Description |
248
+ |----------|-------------|
249
+ | `transformStream(stream, fn)` | Transform each chunk |
250
+ | `filterStream(stream, predicate)` | Filter chunks |
251
+ | `mapStream(stream, fn)` | Map chunks to new values |
252
+ | `tapStream(stream, fn)` | Side effects without modification |
253
+
254
+ ### Control Functions
255
+
256
+ | Function | Description |
257
+ |----------|-------------|
258
+ | `throttleStream(stream, ms)` | Batch chunks by time interval |
259
+ | `rateLimitStream(stream, rate)` | Limit chunks per second |
260
+ | `teeStream(stream, count)` | Split into multiple streams |
261
+ | `splitStream(stream, count)` | Split into multiple streams |
262
+
263
+ ### Validation Functions
264
+
265
+ | Function | Description |
266
+ |----------|-------------|
267
+ | `validateStream(stream, options)` | Validate stream structure |
268
+ | `validateChunkSequence(chunks)` | Check sequence validity |
269
+
270
+ ### Error Functions
271
+
272
+ | Function | Description |
273
+ |----------|-------------|
274
+ | `catchStreamErrors(stream, handler)` | Handle errors gracefully |
275
+ | `streamWithTimeout(stream, ms)` | Add timeout |
276
+ | `createStreamError(error)` | Create error chunk |
277
+
278
+ ## License
279
+
280
+ MIT - see [LICENSE](./LICENSE) for details.