@llumiverse/core 1.3.0 → 1.4.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 (145) hide show
  1. package/lib/CompletionStream.d.ts +48 -0
  2. package/lib/CompletionStream.d.ts.map +1 -0
  3. package/lib/{esm/CompletionStream.js → CompletionStream.js} +142 -67
  4. package/lib/CompletionStream.js.map +1 -0
  5. package/lib/{types/Driver.d.ts → Driver.d.ts} +38 -5
  6. package/lib/Driver.d.ts.map +1 -0
  7. package/lib/{esm/Driver.js → Driver.js} +151 -106
  8. package/lib/Driver.js.map +1 -0
  9. package/lib/{types/async.d.ts → async.d.ts} +3 -3
  10. package/lib/async.d.ts.map +1 -0
  11. package/lib/{esm/async.js → async.js} +4 -4
  12. package/lib/async.js.map +1 -0
  13. package/lib/{types/conversation-utils.d.ts → conversation-utils.d.ts} +9 -0
  14. package/lib/conversation-utils.d.ts.map +1 -0
  15. package/lib/{esm/conversation-utils.js → conversation-utils.js} +78 -15
  16. package/lib/conversation-utils.js.map +1 -0
  17. package/lib/embeddings.d.ts +58 -0
  18. package/lib/embeddings.d.ts.map +1 -0
  19. package/lib/embeddings.js +131 -0
  20. package/lib/embeddings.js.map +1 -0
  21. package/lib/formatters/commons.d.ts +3 -0
  22. package/lib/formatters/commons.d.ts.map +1 -0
  23. package/lib/formatters/commons.js +4 -0
  24. package/lib/formatters/commons.js.map +1 -0
  25. package/lib/{types/formatters → formatters}/generic.d.ts +2 -2
  26. package/lib/formatters/generic.d.ts.map +1 -0
  27. package/lib/{esm/formatters → formatters}/generic.js +8 -8
  28. package/lib/formatters/generic.js.map +1 -0
  29. package/lib/formatters/index.d.ts +4 -0
  30. package/lib/formatters/index.d.ts.map +1 -0
  31. package/lib/formatters/index.js +4 -0
  32. package/lib/formatters/index.js.map +1 -0
  33. package/lib/{types/formatters → formatters}/nova.d.ts +4 -4
  34. package/lib/formatters/nova.d.ts.map +1 -0
  35. package/lib/{esm/formatters → formatters}/nova.js +19 -17
  36. package/lib/formatters/nova.js.map +1 -0
  37. package/lib/http-agent.d.ts +53 -0
  38. package/lib/http-agent.d.ts.map +1 -0
  39. package/lib/http-agent.js +141 -0
  40. package/lib/http-agent.js.map +1 -0
  41. package/lib/index.d.ts +8 -0
  42. package/lib/index.d.ts.map +1 -0
  43. package/lib/index.js +7 -0
  44. package/lib/index.js.map +1 -0
  45. package/lib/{types/json.d.ts → json.d.ts} +1 -1
  46. package/lib/json.d.ts.map +1 -0
  47. package/lib/{esm/json.js → json.js} +4 -3
  48. package/lib/json.js.map +1 -0
  49. package/lib/logger.d.ts +3 -0
  50. package/lib/logger.d.ts.map +1 -0
  51. package/lib/logger.js +42 -0
  52. package/lib/logger.js.map +1 -0
  53. package/lib/resolver.d.ts +2 -0
  54. package/lib/resolver.d.ts.map +1 -0
  55. package/lib/{esm/resolver.js → resolver.js} +7 -4
  56. package/lib/resolver.js.map +1 -0
  57. package/lib/stream.d.ts.map +1 -0
  58. package/lib/stream.js.map +1 -0
  59. package/lib/{types/validation.d.ts → validation.d.ts} +1 -1
  60. package/lib/validation.d.ts.map +1 -0
  61. package/lib/validation.js +100 -0
  62. package/lib/validation.js.map +1 -0
  63. package/package.json +28 -38
  64. package/src/CompletionStream.ts +208 -115
  65. package/src/Driver.error.test.ts +101 -72
  66. package/src/Driver.ts +208 -152
  67. package/src/async.ts +30 -30
  68. package/src/conversation-utils.ts +92 -16
  69. package/src/embeddings.ts +157 -0
  70. package/src/formatters/commons.ts +2 -2
  71. package/src/formatters/generic.ts +20 -17
  72. package/src/formatters/index.ts +3 -3
  73. package/src/formatters/nova.ts +53 -51
  74. package/src/http-agent.test.ts +281 -0
  75. package/src/http-agent.ts +177 -0
  76. package/src/index.ts +7 -5
  77. package/src/json.ts +6 -5
  78. package/src/logger.ts +42 -0
  79. package/src/resolver.ts +12 -9
  80. package/src/stream.ts +4 -5
  81. package/src/validation.ts +42 -29
  82. package/lib/cjs/CompletionStream.js +0 -293
  83. package/lib/cjs/CompletionStream.js.map +0 -1
  84. package/lib/cjs/Driver.js +0 -300
  85. package/lib/cjs/Driver.js.map +0 -1
  86. package/lib/cjs/async.js +0 -131
  87. package/lib/cjs/async.js.map +0 -1
  88. package/lib/cjs/conversation-utils.js +0 -579
  89. package/lib/cjs/conversation-utils.js.map +0 -1
  90. package/lib/cjs/formatters/commons.js +0 -7
  91. package/lib/cjs/formatters/commons.js.map +0 -1
  92. package/lib/cjs/formatters/generic.js +0 -59
  93. package/lib/cjs/formatters/generic.js.map +0 -1
  94. package/lib/cjs/formatters/index.js +0 -20
  95. package/lib/cjs/formatters/index.js.map +0 -1
  96. package/lib/cjs/formatters/nova.js +0 -99
  97. package/lib/cjs/formatters/nova.js.map +0 -1
  98. package/lib/cjs/index.js +0 -22
  99. package/lib/cjs/index.js.map +0 -1
  100. package/lib/cjs/json.js +0 -29
  101. package/lib/cjs/json.js.map +0 -1
  102. package/lib/cjs/package.json +0 -3
  103. package/lib/cjs/resolver.js +0 -45
  104. package/lib/cjs/resolver.js.map +0 -1
  105. package/lib/cjs/stream.js +0 -30
  106. package/lib/cjs/stream.js.map +0 -1
  107. package/lib/cjs/validation.js +0 -96
  108. package/lib/cjs/validation.js.map +0 -1
  109. package/lib/esm/CompletionStream.js.map +0 -1
  110. package/lib/esm/Driver.js.map +0 -1
  111. package/lib/esm/async.js.map +0 -1
  112. package/lib/esm/conversation-utils.js.map +0 -1
  113. package/lib/esm/formatters/commons.js +0 -4
  114. package/lib/esm/formatters/commons.js.map +0 -1
  115. package/lib/esm/formatters/generic.js.map +0 -1
  116. package/lib/esm/formatters/index.js +0 -4
  117. package/lib/esm/formatters/index.js.map +0 -1
  118. package/lib/esm/formatters/nova.js.map +0 -1
  119. package/lib/esm/index.js +0 -6
  120. package/lib/esm/index.js.map +0 -1
  121. package/lib/esm/json.js.map +0 -1
  122. package/lib/esm/resolver.js.map +0 -1
  123. package/lib/esm/stream.js.map +0 -1
  124. package/lib/esm/validation.js +0 -88
  125. package/lib/esm/validation.js.map +0 -1
  126. package/lib/types/CompletionStream.d.ts +0 -20
  127. package/lib/types/CompletionStream.d.ts.map +0 -1
  128. package/lib/types/Driver.d.ts.map +0 -1
  129. package/lib/types/async.d.ts.map +0 -1
  130. package/lib/types/conversation-utils.d.ts.map +0 -1
  131. package/lib/types/formatters/commons.d.ts +0 -3
  132. package/lib/types/formatters/commons.d.ts.map +0 -1
  133. package/lib/types/formatters/generic.d.ts.map +0 -1
  134. package/lib/types/formatters/index.d.ts +0 -4
  135. package/lib/types/formatters/index.d.ts.map +0 -1
  136. package/lib/types/formatters/nova.d.ts.map +0 -1
  137. package/lib/types/index.d.ts +0 -6
  138. package/lib/types/index.d.ts.map +0 -1
  139. package/lib/types/json.d.ts.map +0 -1
  140. package/lib/types/resolver.d.ts +0 -2
  141. package/lib/types/resolver.d.ts.map +0 -1
  142. package/lib/types/stream.d.ts.map +0 -1
  143. package/lib/types/validation.d.ts.map +0 -1
  144. /package/lib/{types/stream.d.ts → stream.d.ts} +0 -0
  145. /package/lib/{esm/stream.js → stream.js} +0 -0
@@ -0,0 +1,48 @@
1
+ import { type CompletionStream, type DriverOptions, type ExecutionOptions, type ExecutionResponse, type ToolUse } from '@llumiverse/common';
2
+ import type { AbstractDriver } from './Driver.js';
3
+ type StreamingToolUse = ToolUse<unknown> & {
4
+ _actual_id?: string;
5
+ };
6
+ /**
7
+ * Merge a single streamed `tool_use` fragment into the accumulator map keyed by tool id.
8
+ *
9
+ * Providers stream a tool call across several chunks. Different fields arrive in
10
+ * different chunks and must be reassembled:
11
+ * - `tool_input` arrives as string pieces (concatenated) or partial objects (merged).
12
+ * - `tool_name` and the provider's real id (`_actual_id`) may only appear in a later chunk.
13
+ * - `thought_signature` (Gemini 2.5+/3.x thinking models) is an opaque base64 byte string
14
+ * that must be passed back verbatim with the assistant turn on the next request.
15
+ *
16
+ * Before this helper existed, the inline accumulator merged `tool_input`/`tool_name`/`_actual_id`
17
+ * but silently dropped `thought_signature` for any chunk after the first for a given tool id.
18
+ * When a provider delivers the signature in pieces (large high-effort signatures are split
19
+ * across chunks) or only on a later chunk, the accumulated signature was left truncated or
20
+ * absent — producing an invalid base64 byte value that the provider rejects with a
21
+ * "Base64 decoding failed" (TYPE_BYTES) error when the assistant turn is threaded back.
22
+ *
23
+ * The signature is therefore reassembled by concatenating fragments in arrival order, exactly
24
+ * like `tool_input` string pieces. When the whole signature arrives in a single chunk there is
25
+ * only one fragment, so concatenation is a no-op and the value round-trips byte-identically.
26
+ *
27
+ * Exported for unit testing the round-trip in isolation (no network / no full driver).
28
+ */
29
+ export declare function accumulateToolUseChunk(accumulatedToolUse: Map<string, StreamingToolUse>, tool: StreamingToolUse): void;
30
+ export declare class DefaultCompletionStream<PromptT = unknown> implements CompletionStream<PromptT> {
31
+ driver: AbstractDriver<DriverOptions, PromptT>;
32
+ prompt: PromptT;
33
+ options: ExecutionOptions;
34
+ chunks: number;
35
+ completion: ExecutionResponse<PromptT> | undefined;
36
+ constructor(driver: AbstractDriver<DriverOptions, PromptT>, prompt: PromptT, options: ExecutionOptions);
37
+ [Symbol.asyncIterator](): AsyncGenerator<string, void, unknown>;
38
+ }
39
+ export declare class FallbackCompletionStream<PromptT = unknown> implements CompletionStream<PromptT> {
40
+ driver: AbstractDriver<DriverOptions, PromptT>;
41
+ prompt: PromptT;
42
+ options: ExecutionOptions;
43
+ completion: ExecutionResponse<PromptT> | undefined;
44
+ constructor(driver: AbstractDriver<DriverOptions, PromptT>, prompt: PromptT, options: ExecutionOptions);
45
+ [Symbol.asyncIterator](): AsyncGenerator<string, void, unknown>;
46
+ }
47
+ export {};
48
+ //# sourceMappingURL=CompletionStream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CompletionStream.d.ts","sourceRoot":"","sources":["../src/CompletionStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EAGtB,KAAK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} from "@llumiverse/common";
1
+ import { LlumiverseError, } from '@llumiverse/common';
2
+ /**
3
+ * Merge a single streamed `tool_use` fragment into the accumulator map keyed by tool id.
4
+ *
5
+ * Providers stream a tool call across several chunks. Different fields arrive in
6
+ * different chunks and must be reassembled:
7
+ * - `tool_input` arrives as string pieces (concatenated) or partial objects (merged).
8
+ * - `tool_name` and the provider's real id (`_actual_id`) may only appear in a later chunk.
9
+ * - `thought_signature` (Gemini 2.5+/3.x thinking models) is an opaque base64 byte string
10
+ * that must be passed back verbatim with the assistant turn on the next request.
11
+ *
12
+ * Before this helper existed, the inline accumulator merged `tool_input`/`tool_name`/`_actual_id`
13
+ * but silently dropped `thought_signature` for any chunk after the first for a given tool id.
14
+ * When a provider delivers the signature in pieces (large high-effort signatures are split
15
+ * across chunks) or only on a later chunk, the accumulated signature was left truncated or
16
+ * absent — producing an invalid base64 byte value that the provider rejects with a
17
+ * "Base64 decoding failed" (TYPE_BYTES) error when the assistant turn is threaded back.
18
+ *
19
+ * The signature is therefore reassembled by concatenating fragments in arrival order, exactly
20
+ * like `tool_input` string pieces. When the whole signature arrives in a single chunk there is
21
+ * only one fragment, so concatenation is a no-op and the value round-trips byte-identically.
22
+ *
23
+ * Exported for unit testing the round-trip in isolation (no network / no full driver).
24
+ */
25
+ export function accumulateToolUseChunk(accumulatedToolUse, tool) {
26
+ const existing = accumulatedToolUse.get(tool.id);
27
+ if (!existing) {
28
+ // New tool call
29
+ accumulatedToolUse.set(tool.id, { ...tool });
30
+ return;
31
+ }
32
+ // Merge tool input (for streaming where arguments come as string pieces)
33
+ if (tool.tool_input !== null && tool.tool_input !== undefined) {
34
+ const existingInput = existing.tool_input;
35
+ const newInput = tool.tool_input;
36
+ if (typeof existingInput === 'string' && typeof newInput === 'string') {
37
+ // Concatenate string arguments
38
+ existing.tool_input = (existingInput + newInput);
39
+ }
40
+ else if (existingInput && typeof existingInput === 'object' && newInput && typeof newInput === 'object') {
41
+ // Merge objects
42
+ existing.tool_input = {
43
+ ...existingInput,
44
+ ...newInput,
45
+ };
46
+ }
47
+ else {
48
+ existing.tool_input = tool.tool_input;
49
+ }
50
+ }
51
+ // Update tool name if provided (might come in later chunk)
52
+ if (tool.tool_name) {
53
+ existing.tool_name = tool.tool_name;
54
+ }
55
+ // Reassemble the thought signature: concatenate fragments in arrival order so a
56
+ // signature split across chunks is restored byte-for-byte instead of being truncated.
57
+ if (tool.thought_signature) {
58
+ existing.thought_signature = (existing.thought_signature ?? '') + tool.thought_signature;
59
+ }
60
+ // Update actual ID if provided (OpenAI sends id only in first chunk)
61
+ if (tool._actual_id) {
62
+ existing._actual_id = tool._actual_id;
63
+ }
64
+ }
2
65
  export class DefaultCompletionStream {
3
66
  driver;
4
67
  prompt;
@@ -19,22 +82,34 @@ export class DefaultCompletionStream {
19
82
  const accumulatedToolUse = new Map(); // Accumulate tool_use by id
20
83
  this.driver.logger.debug(`[${this.driver.provider}] Streaming Execution of ${this.options.model} with prompt`);
21
84
  const start = Date.now();
22
- let finish_reason = undefined;
85
+ let finish_reason;
23
86
  let promptTokens = 0;
24
- let resultTokens = undefined;
25
- let promptCachedTokens = undefined;
26
- let promptCacheWriteTokens = undefined;
27
- let promptNewTokens = undefined;
87
+ let resultTokens;
88
+ let promptCachedTokens;
89
+ let promptCacheWriteTokens;
90
+ let promptNewTokens;
91
+ const httpScope = this.driver.createExecutionHttpAgentScope(this.options);
92
+ let sourceIterator;
93
+ let streamCompleted = false;
28
94
  try {
29
- const stream = await this.driver.requestTextCompletionStream(this.prompt, this.options);
30
- for await (const chunk of stream) {
95
+ const stream = await httpScope.run(() => this.driver.requestTextCompletionStream(this.prompt, this.options));
96
+ const iterator = stream[Symbol.asyncIterator]();
97
+ sourceIterator = iterator;
98
+ while (true) {
99
+ const next = await httpScope.run(() => iterator.next());
100
+ if (next.done) {
101
+ streamCompleted = true;
102
+ break;
103
+ }
104
+ const chunk = next.value;
31
105
  if (chunk) {
32
106
  if (typeof chunk === 'string') {
33
107
  this.chunks++;
34
108
  yield chunk;
35
109
  }
36
110
  else {
37
- if (chunk.finish_reason) { //Do not replace non-null values with null values
111
+ if (chunk.finish_reason) {
112
+ //Do not replace non-null values with null values
38
113
  finish_reason = chunk.finish_reason; //Used to skip empty finish_reason chunks coming after "stop" or "length"
39
114
  }
40
115
  if (chunk.token_usage) {
@@ -54,37 +129,7 @@ export class DefaultCompletionStream {
54
129
  // Note: During streaming, tool_input comes as string chunks that need concatenation
55
130
  if (chunk.tool_use && chunk.tool_use.length > 0) {
56
131
  for (const tool of chunk.tool_use) {
57
- const existing = accumulatedToolUse.get(tool.id);
58
- if (existing) {
59
- // Merge tool input (for streaming where arguments come as string pieces)
60
- if (tool.tool_input !== null && tool.tool_input !== undefined) {
61
- const existingInput = existing.tool_input;
62
- const newInput = tool.tool_input;
63
- if (typeof existingInput === 'string' && typeof newInput === 'string') {
64
- // Concatenate string arguments
65
- existing.tool_input = existingInput + newInput;
66
- }
67
- else if (existingInput && typeof existingInput === 'object' && newInput && typeof newInput === 'object') {
68
- // Merge objects
69
- existing.tool_input = { ...existingInput, ...newInput };
70
- }
71
- else {
72
- existing.tool_input = tool.tool_input;
73
- }
74
- }
75
- // Update tool name if provided (might come in later chunk)
76
- if (tool.tool_name) {
77
- existing.tool_name = tool.tool_name;
78
- }
79
- // Update actual ID if provided (OpenAI sends id only in first chunk)
80
- if (tool._actual_id) {
81
- existing._actual_id = tool._actual_id;
82
- }
83
- }
84
- else {
85
- // New tool call
86
- accumulatedToolUse.set(tool.id, { ...tool });
87
- }
132
+ accumulateToolUseChunk(accumulatedToolUse, tool);
88
133
  }
89
134
  }
90
135
  if (Array.isArray(chunk.result) && chunk.result.length > 0) {
@@ -104,15 +149,21 @@ export class DefaultCompletionStream {
104
149
  try {
105
150
  const lastParsed = lastResult.value;
106
151
  const currentParsed = result.value;
107
- if (lastParsed !== null && typeof lastParsed === 'object' &&
108
- currentParsed !== null && typeof currentParsed === 'object') {
152
+ if (lastParsed !== null &&
153
+ typeof lastParsed === 'object' &&
154
+ currentParsed !== null &&
155
+ typeof currentParsed === 'object') {
109
156
  const combined = { ...lastParsed, ...currentParsed };
110
157
  lastResult.value = combined;
111
158
  }
112
159
  else {
113
160
  // If not objects, convert to string and concatenate
114
- const lastStr = typeof lastParsed === 'string' ? lastParsed : JSON.stringify(lastParsed);
115
- const currentStr = typeof currentParsed === 'string' ? currentParsed : JSON.stringify(currentParsed);
161
+ const lastStr = typeof lastParsed === 'string'
162
+ ? lastParsed
163
+ : JSON.stringify(lastParsed);
164
+ const currentStr = typeof currentParsed === 'string'
165
+ ? currentParsed
166
+ : JSON.stringify(currentParsed);
116
167
  lastResult.value = lastStr + currentStr;
117
168
  }
118
169
  }
@@ -129,20 +180,26 @@ export class DefaultCompletionStream {
129
180
  }
130
181
  // Convert CompletionResult[] to string for streaming
131
182
  // Only yield if we have results to show
132
- const resultText = chunk.result.map(r => {
183
+ const resultText = chunk.result
184
+ .map((r) => {
133
185
  switch (r.type) {
134
186
  case 'text':
135
187
  return r.value;
136
188
  case 'json':
137
189
  return JSON.stringify(r.value);
138
- case 'image':
139
- // Show truncated image placeholder for streaming
140
- const truncatedValue = typeof r.value === 'string' ? r.value.slice(0, 10) : String(r.value).slice(0, 10);
190
+ case 'image': {
191
+ const truncatedValue = typeof r.value === 'string'
192
+ ? r.value.slice(0, 10)
193
+ : String(r.value).slice(0, 10);
141
194
  return `\n[Image: ${truncatedValue}...]\n`;
142
- default:
143
- return String(r.value || '');
195
+ }
196
+ default: {
197
+ const _exhaustive = r;
198
+ return String(_exhaustive);
199
+ }
144
200
  }
145
- }).join('');
201
+ })
202
+ .join('');
146
203
  if (resultText) {
147
204
  this.chunks++;
148
205
  yield resultText;
@@ -163,16 +220,30 @@ export class DefaultCompletionStream {
163
220
  operation: 'stream',
164
221
  });
165
222
  }
223
+ finally {
224
+ if (!streamCompleted && sourceIterator?.return) {
225
+ const returnIterator = sourceIterator.return.bind(sourceIterator);
226
+ try {
227
+ await httpScope.run(() => returnIterator());
228
+ }
229
+ catch {
230
+ /* stream cleanup best-effort */
231
+ }
232
+ }
233
+ await httpScope.close();
234
+ }
166
235
  // Return undefined only if we never received any token data from the provider.
167
236
  // Use !== undefined (not truthiness) because resultTokens === 0 is valid (e.g. empty output with stop).
168
- const tokens = resultTokens !== undefined ? {
169
- prompt: promptTokens,
170
- result: resultTokens,
171
- total: resultTokens + promptTokens,
172
- ...(promptCachedTokens != null && { prompt_cached: promptCachedTokens }),
173
- ...(promptCacheWriteTokens != null && { prompt_cache_write: promptCacheWriteTokens }),
174
- ...(promptNewTokens != null && { prompt_new: promptNewTokens }),
175
- } : undefined;
237
+ const tokens = resultTokens !== undefined
238
+ ? {
239
+ prompt: promptTokens,
240
+ result: resultTokens,
241
+ total: resultTokens + promptTokens,
242
+ ...(promptCachedTokens != null && { prompt_cached: promptCachedTokens }),
243
+ ...(promptCacheWriteTokens != null && { prompt_cache_write: promptCacheWriteTokens }),
244
+ ...(promptNewTokens != null && { prompt_new: promptNewTokens }),
245
+ }
246
+ : undefined;
176
247
  // Convert accumulated tool_use Map to array
177
248
  let toolUseArray = accumulatedToolUse.size > 0 ? Array.from(accumulatedToolUse.values()) : undefined;
178
249
  // Finalize tool calls: restore actual IDs and parse JSON arguments
@@ -200,7 +271,7 @@ export class DefaultCompletionStream {
200
271
  // If finish_reason is "length" (max_tokens hit), drop truncated tool calls entirely —
201
272
  // they were cut off mid-generation and would produce invalid results.
202
273
  if (finish_reason === 'length' && truncatedToolIds.size > 0) {
203
- toolUseArray = toolUseArray.filter(t => !truncatedToolIds.has(t.id));
274
+ toolUseArray = toolUseArray.filter((t) => !truncatedToolIds.has(t.id));
204
275
  if (toolUseArray.length === 0) {
205
276
  toolUseArray = undefined;
206
277
  }
@@ -208,7 +279,7 @@ export class DefaultCompletionStream {
208
279
  }
209
280
  this.completion = {
210
281
  result: accumulatedResults, // Return the accumulated CompletionResult[] instead of text
211
- prompt: this.prompt,
282
+ prompt: this.driver.formatDebugPrompt(this.prompt),
212
283
  execution_time: Date.now() - start,
213
284
  token_usage: tokens,
214
285
  finish_reason: finish_reason,
@@ -255,20 +326,24 @@ export class FallbackCompletionStream {
255
326
  try {
256
327
  const completion = await this.driver._execute(this.prompt, this.options);
257
328
  // For fallback streaming, yield the text content but keep the original completion
258
- const content = completion.result.map(r => {
329
+ const content = completion.result
330
+ .map((r) => {
259
331
  switch (r.type) {
260
332
  case 'text':
261
333
  return r.value;
262
334
  case 'json':
263
335
  return JSON.stringify(r.value);
264
- case 'image':
265
- // Show truncated image placeholder for streaming
336
+ case 'image': {
266
337
  const truncatedValue = typeof r.value === 'string' ? r.value.slice(0, 10) : String(r.value).slice(0, 10);
267
338
  return `[Image: ${truncatedValue}...]`;
268
- default:
269
- return String(r.value || '');
339
+ }
340
+ default: {
341
+ const _exhaustive = r;
342
+ return String(_exhaustive);
343
+ }
270
344
  }
271
- }).join('');
345
+ })
346
+ .join('');
272
347
  yield content;
273
348
  this.completion = completion; // Return the original completion with untouched CompletionResult[]
274
349
  }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CompletionStream.js","sourceRoot":"","sources":["../src/CompletionStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAQH,eAAe,GAElB,MAAM,oBAAoB,CAAC;AAK5B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,sBAAsB,CAClC,kBAAiD,EACjD,IAAsB;IAEtB,MAAM,QAAQ,GAAG,kBAAkB,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACjD,IAAI,CAAC,QAAQ,EAAE,CAAC;QACZ,gBAAgB;QAChB,kBAAkB,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,GAAG,IAAI,EAAE,CAAC,CAAC;QAC7C,OAAO;IACX,CAAC;IACD,yEAAyE;IACzE,IAAI,IAAI,CAAC,UAAU,KAAK,IAAI,IAAI,IAAI,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;QAC5D,MAAM,aAAa,GAAG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BACnB,KAAK,MAAM;wBACP,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;oBACnC,KAAK,OAAO,CAAC,CAAC,CAAC;wBACX,MAAM,cAAc,GAChB,OAAO,CAAC,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;wBACtF,OAAO,WAAW,cAAc,MAAM,CAAC;oBAC3C,CAAC;oBACD,OAAO,CAAC,CAAC,CAAC;wBACN,MAAM,WAAW,GAAU,CAAC,CAAC;wBAC7B,OAAO,MAAM,CAAC,WAAW,CAAC,CAAC;oBAC/B,CAAC;gBACL,CAAC;YACL,CAAC,CAAC;iBACD,IAAI,CAAC,EAAE,CAAC,CAAC;YACd,MAAM,OAAO,CAAC;YACd,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,CAAC,mEAAmE;QACrG,CAAC;QAAC,OAAO,KAAc,EAAE,CAAC;YACtB,0CAA0C;YAC1C,IAAI,eAAe,CAAC,iBAAiB,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC3C,MAAM,KAAK,CAAC;YAChB,CAAC;YACD,MAAM,IAAI,CAAC,MAAM,CAAC,qBAAqB,CAAC,KAAK,EAAE;gBAC3C,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ;gBAC9B,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK;gBACzB,SAAS,EAAE,QAAQ;aACtB,CAAC,CAAC;QACP,CAAC;IACL,CAAC;CACJ"}
@@ -3,8 +3,10 @@
3
3
  * Base abstract class is then implemented by each environment
4
4
  * (eg: OpenAI, HuggingFace, etc.)
5
5
  */
6
- import { AIModel, Completion, CompletionChunkObject, CompletionStream, DataSource, DriverOptions, EmbeddingsOptions, EmbeddingsResult, ExecutionOptions, ExecutionResponse, LlumiverseErrorContext, Logger, ModelSearchPayload, PromptOptions, PromptSegment, Providers, TrainingJob, TrainingOptions, TrainingPromptOptions, LlumiverseError } from "@llumiverse/common";
7
- export declare function createLogger(logger: Logger | "console" | undefined): Logger;
6
+ import { type AIModel, type Completion, type CompletionChunkObject, type CompletionStream, type DataSource, type DriverOptions, type EmbeddingsOptions, type EmbeddingsResult, type ExecutionOptions, type ExecutionResponse, LlumiverseError, type LlumiverseErrorContext, type Logger, type ModelSearchPayload, type PromptOptions, type PromptSegment, type Providers, type TrainingJob, type TrainingOptions, type TrainingPromptOptions } from '@llumiverse/common';
7
+ import type { Agent } from 'undici';
8
+ import { type DriverHttpAgentScope } from './http-agent.js';
9
+ export { createLogger } from './logger.js';
8
10
  export interface Driver<PromptT = unknown> {
9
11
  /**
10
12
  *
@@ -22,6 +24,13 @@ export interface Driver<PromptT = unknown> {
22
24
  listModels(params?: ModelSearchPayload): Promise<AIModel[]>;
23
25
  listTrainableModels(): Promise<AIModel[]>;
24
26
  validateConnection(): Promise<boolean>;
27
+ /**
28
+ * Generate embeddings for one or more inputs.
29
+ * Inputs may be text, image, video, or audio depending on the model and
30
+ * provider. Returns one result item per input, each with one or more
31
+ * output vectors (single-vector for text/image, multi-vector for
32
+ * segmented video/audio or joint-multimodal models).
33
+ */
25
34
  generateEmbeddings(options: EmbeddingsOptions): Promise<EmbeddingsResult>;
26
35
  /**
27
36
  * Optional cleanup method called when the driver is evicted from the cache.
@@ -36,7 +45,28 @@ export declare abstract class AbstractDriver<OptionsT extends DriverOptions = Dr
36
45
  options: OptionsT;
37
46
  logger: Logger;
38
47
  abstract provider: Providers | string;
48
+ private _httpAgent?;
49
+ private _driverFetch?;
39
50
  constructor(opts: OptionsT);
51
+ /**
52
+ * Lazily-created undici `Agent` driven by `options.httpTimeout`.
53
+ * Pools sockets for the lifetime of the driver. Subclasses can
54
+ * either pass this directly to an SDK that accepts a `dispatcher`
55
+ * option (rare), or — much more commonly — use {@link getDriverFetch}
56
+ * to get a fetch implementation backed by it.
57
+ *
58
+ * Released via {@link destroy}.
59
+ */
60
+ protected getHttpAgent(): Agent;
61
+ /**
62
+ * Fetch-compatible function backed by the driver's HTTP agent.
63
+ * Pass to any SDK that accepts a custom `fetch` option (OpenAI,
64
+ * Anthropic, `@google/genai`, Bedrock via Smithy, …) or use as a
65
+ * drop-in replacement for the global `fetch` in drivers that make
66
+ * raw HTTP calls.
67
+ */
68
+ protected getDriverFetch(): typeof fetch;
69
+ createExecutionHttpAgentScope(options: Pick<ExecutionOptions, 'httpTimeout'>): DriverHttpAgentScope;
40
70
  createTrainingPrompt(options: TrainingPromptOptions): Promise<string>;
41
71
  startTraining(_dataset: DataSource, _options: TrainingOptions): Promise<TrainingJob>;
42
72
  cancelTraining(_jobId: string): Promise<TrainingJob>;
@@ -44,6 +74,7 @@ export declare abstract class AbstractDriver<OptionsT extends DriverOptions = Dr
44
74
  validateResult(result: Completion, options: ExecutionOptions): void;
45
75
  execute(segments: PromptSegment[], options: ExecutionOptions): Promise<ExecutionResponse<PromptT>>;
46
76
  _execute(prompt: PromptT, options: ExecutionOptions): Promise<ExecutionResponse<PromptT>>;
77
+ formatDebugPrompt(prompt: PromptT): PromptT;
47
78
  protected isImageModel(_model: string): boolean;
48
79
  stream(segments: PromptSegment[], options: ExecutionOptions): Promise<CompletionStream<PromptT>>;
49
80
  /**
@@ -89,8 +120,8 @@ export declare abstract class AbstractDriver<OptionsT extends DriverOptions = Dr
89
120
  * - Status 429, 408: retryable (rate limit, timeout)
90
121
  * - Status 529: retryable (overloaded)
91
122
  * - Status 5xx: retryable (server errors)
92
- * - Status 4xx (except above): not retryable (client errors)
93
- * - Error messages containing "rate limit", "timeout", etc.: retryable
123
+ * - High-confidence transient messages containing "rate limit", "timeout", etc.: retryable
124
+ * - Status 4xx (except above and transient provider quirks): not retryable (client errors)
94
125
  *
95
126
  * @param error - The error to format
96
127
  * @param context - Context about where the error occurred
@@ -114,7 +145,9 @@ export declare abstract class AbstractDriver<OptionsT extends DriverOptions = Dr
114
145
  abstract generateEmbeddings(options: EmbeddingsOptions): Promise<EmbeddingsResult>;
115
146
  /**
116
147
  * Cleanup method called when the driver is evicted from the cache.
117
- * Override this in driver implementations that need to release resources.
148
+ * Releases the lazily-created HTTP agent socket pool. Override this
149
+ * in driver implementations that need to release additional resources
150
+ * — MUST call `super.destroy()` to avoid leaking sockets.
118
151
  */
119
152
  destroy(): void;
120
153
  }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Driver.d.ts","sourceRoot":"","sources":["../src/Driver.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACH,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,qBAAqB,EAC1B,KAAK,gBAAgB,EACrB,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,eAAe,EACf,KAAK,sBAAsB,EAC3B,KAAK,MAAM,EACX,KAAK,kBAAkB,EACvB,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,KAAK,qBAAqB,EAC7B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAGpC,OAAO,EAIH,KAAK,oBAAoB,EAC5B,MAAM,iBAAiB,CAAC;AAIzB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAS3C,MAAM,WAAW,MAAM,CAAC,OAAO,GAAG,OAAO;IACrC;;;;;OAKG;IACH,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEtE,YAAY,CAAC,QAAQ,EAAE,aAAa,EAAE,EAAE,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC,CAA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