@llumiverse/core 1.0.0-dev.20260202.145450Z → 1.0.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 (86) hide show
  1. package/lib/cjs/CompletionStream.js +278 -0
  2. package/lib/cjs/CompletionStream.js.map +1 -0
  3. package/lib/cjs/Driver.js +300 -0
  4. package/lib/cjs/Driver.js.map +1 -0
  5. package/lib/cjs/async.js +131 -0
  6. package/lib/cjs/async.js.map +1 -0
  7. package/lib/cjs/conversation-utils.js +553 -0
  8. package/lib/cjs/conversation-utils.js.map +1 -0
  9. package/lib/cjs/formatters/commons.js +7 -0
  10. package/lib/cjs/formatters/commons.js.map +1 -0
  11. package/lib/cjs/formatters/generic.js +59 -0
  12. package/lib/cjs/formatters/generic.js.map +1 -0
  13. package/lib/cjs/formatters/index.js +20 -0
  14. package/lib/cjs/formatters/index.js.map +1 -0
  15. package/lib/cjs/formatters/nova.js +99 -0
  16. package/lib/cjs/formatters/nova.js.map +1 -0
  17. package/lib/cjs/index.js +22 -0
  18. package/lib/cjs/index.js.map +1 -0
  19. package/lib/cjs/json.js +29 -0
  20. package/lib/cjs/json.js.map +1 -0
  21. package/lib/cjs/package.json +3 -0
  22. package/lib/cjs/resolver.js +45 -0
  23. package/lib/cjs/resolver.js.map +1 -0
  24. package/lib/cjs/stream.js +30 -0
  25. package/lib/cjs/stream.js.map +1 -0
  26. package/lib/cjs/validation.js +96 -0
  27. package/lib/cjs/validation.js.map +1 -0
  28. package/lib/esm/CompletionStream.js +273 -0
  29. package/lib/esm/CompletionStream.js.map +1 -0
  30. package/lib/esm/Driver.js +295 -0
  31. package/lib/esm/Driver.js.map +1 -0
  32. package/lib/esm/async.js +123 -0
  33. package/lib/esm/async.js.map +1 -0
  34. package/lib/esm/conversation-utils.js +542 -0
  35. package/lib/esm/conversation-utils.js.map +1 -0
  36. package/lib/esm/formatters/commons.js +4 -0
  37. package/lib/esm/formatters/commons.js.map +1 -0
  38. package/lib/esm/formatters/generic.js +55 -0
  39. package/lib/esm/formatters/generic.js.map +1 -0
  40. package/lib/esm/formatters/index.js +4 -0
  41. package/lib/esm/formatters/index.js.map +1 -0
  42. package/lib/esm/formatters/nova.js +96 -0
  43. package/lib/esm/formatters/nova.js.map +1 -0
  44. package/lib/esm/index.js +6 -0
  45. package/lib/esm/index.js.map +1 -0
  46. package/lib/esm/json.js +25 -0
  47. package/lib/esm/json.js.map +1 -0
  48. package/lib/esm/resolver.js +42 -0
  49. package/lib/esm/resolver.js.map +1 -0
  50. package/lib/esm/stream.js +25 -0
  51. package/lib/esm/stream.js.map +1 -0
  52. package/lib/esm/validation.js +88 -0
  53. package/lib/esm/validation.js.map +1 -0
  54. package/lib/types/CompletionStream.d.ts +20 -0
  55. package/lib/types/CompletionStream.d.ts.map +1 -0
  56. package/lib/types/Driver.d.ts +121 -0
  57. package/lib/types/Driver.d.ts.map +1 -0
  58. package/lib/types/async.d.ts +27 -0
  59. package/lib/types/async.d.ts.map +1 -0
  60. package/lib/types/conversation-utils.d.ts +129 -0
  61. package/lib/types/conversation-utils.d.ts.map +1 -0
  62. package/lib/types/formatters/commons.d.ts +3 -0
  63. package/lib/types/formatters/commons.d.ts.map +1 -0
  64. package/lib/types/formatters/generic.d.ts +13 -0
  65. package/lib/types/formatters/generic.d.ts.map +1 -0
  66. package/lib/types/formatters/index.d.ts +4 -0
  67. package/lib/types/formatters/index.d.ts.map +1 -0
  68. package/lib/types/formatters/nova.d.ts +40 -0
  69. package/lib/types/formatters/nova.d.ts.map +1 -0
  70. package/lib/types/index.d.ts +6 -0
  71. package/lib/types/index.d.ts.map +1 -0
  72. package/lib/types/json.d.ts +4 -0
  73. package/lib/types/json.d.ts.map +1 -0
  74. package/lib/types/resolver.d.ts +2 -0
  75. package/lib/types/resolver.d.ts.map +1 -0
  76. package/lib/types/stream.d.ts +4 -0
  77. package/lib/types/stream.d.ts.map +1 -0
  78. package/lib/types/validation.d.ts +7 -0
  79. package/lib/types/validation.d.ts.map +1 -0
  80. package/package.json +7 -7
  81. package/src/CompletionStream.ts +54 -12
  82. package/src/Driver.error.test.ts +261 -0
  83. package/src/Driver.ts +121 -10
  84. package/src/conversation-utils.ts +105 -5
  85. package/src/index.ts +2 -2
  86. package/src/validation.ts +2 -2
@@ -0,0 +1,278 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FallbackCompletionStream = exports.DefaultCompletionStream = void 0;
4
+ const common_1 = require("@llumiverse/common");
5
+ class DefaultCompletionStream {
6
+ driver;
7
+ prompt;
8
+ options;
9
+ chunks; // Counter for number of chunks instead of storing strings
10
+ completion;
11
+ constructor(driver, prompt, options) {
12
+ this.driver = driver;
13
+ this.prompt = prompt;
14
+ this.options = options;
15
+ this.chunks = 0;
16
+ }
17
+ async *[Symbol.asyncIterator]() {
18
+ // reset state
19
+ this.completion = undefined;
20
+ this.chunks = 0;
21
+ const accumulatedResults = []; // Accumulate CompletionResult[] from chunks
22
+ const accumulatedToolUse = new Map(); // Accumulate tool_use by id
23
+ this.driver.logger.debug(`[${this.driver.provider}] Streaming Execution of ${this.options.model} with prompt`);
24
+ const start = Date.now();
25
+ let finish_reason = undefined;
26
+ let promptTokens = 0;
27
+ let resultTokens = undefined;
28
+ try {
29
+ const stream = await this.driver.requestTextCompletionStream(this.prompt, this.options);
30
+ for await (const chunk of stream) {
31
+ if (chunk) {
32
+ if (typeof chunk === 'string') {
33
+ this.chunks++;
34
+ yield chunk;
35
+ }
36
+ else {
37
+ if (chunk.finish_reason) { //Do not replace non-null values with null values
38
+ finish_reason = chunk.finish_reason; //Used to skip empty finish_reason chunks coming after "stop" or "length"
39
+ }
40
+ if (chunk.token_usage) {
41
+ //Tokens returned include prior parts of stream,
42
+ //so overwrite rather than accumulate
43
+ //Math.max used as some models report final token count at beginning of stream
44
+ promptTokens = Math.max(promptTokens, chunk.token_usage.prompt ?? 0);
45
+ resultTokens = Math.max(resultTokens ?? 0, chunk.token_usage.result ?? 0);
46
+ }
47
+ // Accumulate tool_use from chunks
48
+ // Note: During streaming, tool_input comes as string chunks that need concatenation
49
+ if (chunk.tool_use && chunk.tool_use.length > 0) {
50
+ for (const tool of chunk.tool_use) {
51
+ const existing = accumulatedToolUse.get(tool.id);
52
+ if (existing) {
53
+ // Merge tool input (for streaming where arguments come as string pieces)
54
+ if (tool.tool_input !== null && tool.tool_input !== undefined) {
55
+ const existingInput = existing.tool_input;
56
+ const newInput = tool.tool_input;
57
+ if (typeof existingInput === 'string' && typeof newInput === 'string') {
58
+ // Concatenate string arguments
59
+ existing.tool_input = existingInput + newInput;
60
+ }
61
+ else if (existingInput && typeof existingInput === 'object' && newInput && typeof newInput === 'object') {
62
+ // Merge objects
63
+ existing.tool_input = { ...existingInput, ...newInput };
64
+ }
65
+ else {
66
+ existing.tool_input = tool.tool_input;
67
+ }
68
+ }
69
+ // Update tool name if provided (might come in later chunk)
70
+ if (tool.tool_name) {
71
+ existing.tool_name = tool.tool_name;
72
+ }
73
+ // Update actual ID if provided (OpenAI sends id only in first chunk)
74
+ if (tool._actual_id) {
75
+ existing._actual_id = tool._actual_id;
76
+ }
77
+ }
78
+ else {
79
+ // New tool call
80
+ accumulatedToolUse.set(tool.id, { ...tool });
81
+ }
82
+ }
83
+ }
84
+ if (Array.isArray(chunk.result) && chunk.result.length > 0) {
85
+ // Process each result in the chunk, combining consecutive text/JSON
86
+ for (const result of chunk.result) {
87
+ // Check if we can combine with the last accumulated result
88
+ const lastResult = accumulatedResults[accumulatedResults.length - 1];
89
+ if (lastResult &&
90
+ ((lastResult.type === 'text' && result.type === 'text') ||
91
+ (lastResult.type === 'json' && result.type === 'json'))) {
92
+ // Combine consecutive text or JSON results
93
+ if (result.type === 'text') {
94
+ lastResult.value += result.value;
95
+ }
96
+ else if (result.type === 'json') {
97
+ // For JSON, combine the parsed objects directly
98
+ try {
99
+ const lastParsed = lastResult.value;
100
+ const currentParsed = result.value;
101
+ if (lastParsed !== null && typeof lastParsed === 'object' &&
102
+ currentParsed !== null && typeof currentParsed === 'object') {
103
+ const combined = { ...lastParsed, ...currentParsed };
104
+ lastResult.value = combined;
105
+ }
106
+ else {
107
+ // If not objects, convert to string and concatenate
108
+ const lastStr = typeof lastParsed === 'string' ? lastParsed : JSON.stringify(lastParsed);
109
+ const currentStr = typeof currentParsed === 'string' ? currentParsed : JSON.stringify(currentParsed);
110
+ lastResult.value = lastStr + currentStr;
111
+ }
112
+ }
113
+ catch {
114
+ // If anything fails, just concatenate string representations
115
+ lastResult.value = String(lastResult.value) + String(result.value);
116
+ }
117
+ }
118
+ }
119
+ else {
120
+ // Add as new result
121
+ accumulatedResults.push(result);
122
+ }
123
+ }
124
+ // Convert CompletionResult[] to string for streaming
125
+ // Only yield if we have results to show
126
+ const resultText = chunk.result.map(r => {
127
+ switch (r.type) {
128
+ case 'text':
129
+ return r.value;
130
+ case 'json':
131
+ return JSON.stringify(r.value);
132
+ case 'image':
133
+ // Show truncated image placeholder for streaming
134
+ const truncatedValue = typeof r.value === 'string' ? r.value.slice(0, 10) : String(r.value).slice(0, 10);
135
+ return `\n[Image: ${truncatedValue}...]\n`;
136
+ default:
137
+ return String(r.value || '');
138
+ }
139
+ }).join('');
140
+ if (resultText) {
141
+ this.chunks++;
142
+ yield resultText;
143
+ }
144
+ }
145
+ }
146
+ }
147
+ }
148
+ }
149
+ catch (error) {
150
+ // Don't wrap if already a LlumiverseError
151
+ if (common_1.LlumiverseError.isLlumiverseError(error)) {
152
+ throw error;
153
+ }
154
+ throw this.driver.formatLlumiverseError(error, {
155
+ provider: this.driver.provider,
156
+ model: this.options.model,
157
+ operation: 'stream',
158
+ });
159
+ }
160
+ // Return undefined only if we never received any token data from the provider.
161
+ // Use !== undefined (not truthiness) because resultTokens === 0 is valid (e.g. empty output with stop).
162
+ const tokens = resultTokens !== undefined ?
163
+ { prompt: promptTokens, result: resultTokens, total: resultTokens + promptTokens, } : undefined;
164
+ // Convert accumulated tool_use Map to array
165
+ let toolUseArray = accumulatedToolUse.size > 0 ? Array.from(accumulatedToolUse.values()) : undefined;
166
+ // Finalize tool calls: restore actual IDs and parse JSON arguments
167
+ if (toolUseArray) {
168
+ const truncatedToolIds = new Set();
169
+ for (const tool of toolUseArray) {
170
+ // Restore actual ID from OpenAI (was stored in _actual_id during streaming)
171
+ if (tool._actual_id) {
172
+ tool.id = tool._actual_id;
173
+ delete tool._actual_id;
174
+ }
175
+ // Parse tool_input strings as JSON if needed (streaming sends arguments as string chunks)
176
+ if (typeof tool.tool_input === 'string') {
177
+ try {
178
+ tool.tool_input = JSON.parse(tool.tool_input);
179
+ }
180
+ catch {
181
+ // JSON parse failed — tool_input was likely truncated by max_tokens.
182
+ // Set to empty object to prevent string tool_input from corrupting the conversation.
183
+ tool.tool_input = {};
184
+ truncatedToolIds.add(tool.id);
185
+ }
186
+ }
187
+ }
188
+ // If finish_reason is "length" (max_tokens hit), drop truncated tool calls entirely —
189
+ // they were cut off mid-generation and would produce invalid results.
190
+ if (finish_reason === 'length' && truncatedToolIds.size > 0) {
191
+ toolUseArray = toolUseArray.filter(t => !truncatedToolIds.has(t.id));
192
+ if (toolUseArray.length === 0) {
193
+ toolUseArray = undefined;
194
+ }
195
+ }
196
+ }
197
+ this.completion = {
198
+ result: accumulatedResults, // Return the accumulated CompletionResult[] instead of text
199
+ prompt: this.prompt,
200
+ execution_time: Date.now() - start,
201
+ token_usage: tokens,
202
+ finish_reason: finish_reason,
203
+ chunks: this.chunks,
204
+ tool_use: toolUseArray,
205
+ };
206
+ // Build conversation context for multi-turn support
207
+ const conversation = this.driver.buildStreamingConversation(this.prompt, accumulatedResults, toolUseArray, this.options);
208
+ if (conversation !== undefined) {
209
+ this.completion.conversation = conversation;
210
+ }
211
+ try {
212
+ if (this.completion) {
213
+ this.driver.validateResult(this.completion, this.options);
214
+ }
215
+ }
216
+ catch (error) {
217
+ // Don't wrap if already a LlumiverseError
218
+ if (common_1.LlumiverseError.isLlumiverseError(error)) {
219
+ throw error;
220
+ }
221
+ throw this.driver.formatLlumiverseError(error, {
222
+ provider: this.driver.provider,
223
+ model: this.options.model,
224
+ operation: 'stream',
225
+ });
226
+ }
227
+ }
228
+ }
229
+ exports.DefaultCompletionStream = DefaultCompletionStream;
230
+ class FallbackCompletionStream {
231
+ driver;
232
+ prompt;
233
+ options;
234
+ completion;
235
+ constructor(driver, prompt, options) {
236
+ this.driver = driver;
237
+ this.prompt = prompt;
238
+ this.options = options;
239
+ }
240
+ async *[Symbol.asyncIterator]() {
241
+ // reset state
242
+ this.completion = undefined;
243
+ this.driver.logger.debug(`[${this.driver.provider}] Streaming is not supported, falling back to blocking execution`);
244
+ try {
245
+ const completion = await this.driver._execute(this.prompt, this.options);
246
+ // For fallback streaming, yield the text content but keep the original completion
247
+ const content = completion.result.map(r => {
248
+ switch (r.type) {
249
+ case 'text':
250
+ return r.value;
251
+ case 'json':
252
+ return JSON.stringify(r.value);
253
+ case 'image':
254
+ // Show truncated image placeholder for streaming
255
+ const truncatedValue = typeof r.value === 'string' ? r.value.slice(0, 10) : String(r.value).slice(0, 10);
256
+ return `[Image: ${truncatedValue}...]`;
257
+ default:
258
+ return String(r.value || '');
259
+ }
260
+ }).join('');
261
+ yield content;
262
+ this.completion = completion; // Return the original completion with untouched CompletionResult[]
263
+ }
264
+ catch (error) {
265
+ // Don't wrap if already a LlumiverseError
266
+ if (common_1.LlumiverseError.isLlumiverseError(error)) {
267
+ throw error;
268
+ }
269
+ throw this.driver.formatLlumiverseError(error, {
270
+ provider: this.driver.provider,
271
+ model: this.options.model,
272
+ operation: 'stream',
273
+ });
274
+ }
275
+ }
276
+ }
277
+ exports.FallbackCompletionStream = FallbackCompletionStream;
278
+ //# sourceMappingURL=CompletionStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CompletionStream.js","sourceRoot":"","sources":["../../src/CompletionStream.ts"],"names":[],"mappings":";;;AAAA,+CAQ4B;AAG5B,MAAa,uBAAuB;IAKb;IACR;IACA;IALX,MAAM,CAAS,CAAC,0DAA0D;I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AAM,CAAC;oBAC3C;wBACI,OAAO,MAAM,CAAE,CAAS,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;gBAC9C,CAAC;YACL,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACZ,MAAM,OAAO,CAAC;YACd,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,CAAC,mEAAmE;QACrG,CAAC;QAAC,OAAO,KAAU,EAAE,CAAC;YAClB,0CAA0C;YAC1C,IAAI,wBAAe,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;AA9CD,4DA8CC"}
@@ -0,0 +1,300 @@
1
+ "use strict";
2
+ /**
3
+ * Classes to handle the execution of an interaction in an execution environment.
4
+ * Base abstract class is then implemented by each environment
5
+ * (eg: OpenAI, HuggingFace, etc.)
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.AbstractDriver = void 0;
9
+ exports.createLogger = createLogger;
10
+ const common_1 = require("@llumiverse/common");
11
+ const CompletionStream_js_1 = require("./CompletionStream.js");
12
+ const index_js_1 = require("./formatters/index.js");
13
+ const validation_js_1 = require("./validation.js");
14
+ // Helper to create logger methods that support both message-only and object-first signatures
15
+ function createConsoleLoggerMethod(consoleMethod) {
16
+ return ((objOrMsg, msgOrNever, ...args) => {
17
+ if (typeof objOrMsg === 'string') {
18
+ // Message-only: logger.info("message", ...args)
19
+ consoleMethod(objOrMsg, msgOrNever, ...args);
20
+ }
21
+ else if (msgOrNever !== undefined) {
22
+ // Object-first: logger.info({ obj }, "message", ...args)
23
+ consoleMethod(msgOrNever, objOrMsg, ...args);
24
+ }
25
+ else {
26
+ // Object-only: logger.info({ obj })
27
+ consoleMethod(objOrMsg, ...args);
28
+ }
29
+ });
30
+ }
31
+ const ConsoleLogger = {
32
+ debug: createConsoleLoggerMethod(console.debug.bind(console)),
33
+ info: createConsoleLoggerMethod(console.info.bind(console)),
34
+ warn: createConsoleLoggerMethod(console.warn.bind(console)),
35
+ error: createConsoleLoggerMethod(console.error.bind(console)),
36
+ };
37
+ const noop = () => void 0;
38
+ const NoopLogger = {
39
+ debug: noop,
40
+ info: noop,
41
+ warn: noop,
42
+ error: noop,
43
+ };
44
+ function createLogger(logger) {
45
+ if (logger === "console") {
46
+ return ConsoleLogger;
47
+ }
48
+ else if (logger) {
49
+ return logger;
50
+ }
51
+ else {
52
+ return NoopLogger;
53
+ }
54
+ }
55
+ /**
56
+ * To be implemented by each driver
57
+ */
58
+ class AbstractDriver {
59
+ options;
60
+ logger;
61
+ constructor(opts) {
62
+ this.options = opts;
63
+ this.logger = createLogger(opts.logger);
64
+ }
65
+ async createTrainingPrompt(options) {
66
+ const prompt = await this.createPrompt(options.segments, { result_schema: options.schema, model: options.model });
67
+ return JSON.stringify({
68
+ prompt,
69
+ completion: typeof options.completion === 'string' ? options.completion : JSON.stringify(options.completion)
70
+ });
71
+ }
72
+ startTraining(_dataset, _options) {
73
+ throw new Error("Method not implemented.");
74
+ }
75
+ cancelTraining(_jobId) {
76
+ throw new Error("Method not implemented.");
77
+ }
78
+ getTrainingJob(_jobId) {
79
+ throw new Error("Method not implemented.");
80
+ }
81
+ validateResult(result, options) {
82
+ if (!result.tool_use && !result.error && options.result_schema) {
83
+ try {
84
+ result.result = (0, validation_js_1.validateResult)(result.result, options.result_schema);
85
+ }
86
+ catch (error) {
87
+ const errorMessage = `[${this.provider}] [${options.model}] ${error.code ? '[' + error.code + '] ' : ''}Result validation error: ${error.message}`;
88
+ this.logger.error({ err: error, data: result.result }, errorMessage);
89
+ result.error = {
90
+ code: error.code || error.name,
91
+ message: error.message,
92
+ data: result.result,
93
+ };
94
+ }
95
+ }
96
+ }
97
+ async execute(segments, options) {
98
+ const prompt = await this.createPrompt(segments, options);
99
+ return this._execute(prompt, options).catch((error) => {
100
+ // Don't wrap if already a LlumiverseError
101
+ if (common_1.LlumiverseError.isLlumiverseError(error)) {
102
+ throw error;
103
+ }
104
+ throw this.formatLlumiverseError(error, {
105
+ provider: this.provider,
106
+ model: options.model,
107
+ operation: 'execute',
108
+ });
109
+ });
110
+ }
111
+ async _execute(prompt, options) {
112
+ try {
113
+ const start = Date.now();
114
+ let result;
115
+ if (this.isImageModel(options.model)) {
116
+ this.logger.debug(`[${this.provider}] Executing prompt on ${options.model}, image pathway.`);
117
+ result = await this.requestImageGeneration(prompt, options);
118
+ }
119
+ else {
120
+ this.logger.debug(`[${this.provider}] Executing prompt on ${options.model}, text pathway.`);
121
+ result = await this.requestTextCompletion(prompt, options);
122
+ this.validateResult(result, options);
123
+ }
124
+ const execution_time = Date.now() - start;
125
+ return { ...result, prompt, execution_time };
126
+ }
127
+ catch (error) {
128
+ // Don't wrap if already a LlumiverseError
129
+ if (common_1.LlumiverseError.isLlumiverseError(error)) {
130
+ throw error;
131
+ }
132
+ // Log the original error for debugging
133
+ this.logger.error({ err: error, data: { provider: this.provider, model: options.model, operation: 'execute', prompt } }, `Error during execution in provider ${this.provider}:`);
134
+ throw this.formatLlumiverseError(error, {
135
+ provider: this.provider,
136
+ model: options.model,
137
+ operation: 'execute',
138
+ });
139
+ }
140
+ }
141
+ isImageModel(_model) {
142
+ return false;
143
+ }
144
+ // by default no stream is supported. we block and we return all at once
145
+ async stream(segments, options) {
146
+ this.logger.info(options, `Executing prompt with provider ${this.provider} with options: ${JSON.stringify(options)}`);
147
+ const prompt = await this.createPrompt(segments, options);
148
+ const canStream = await this.canStream(options);
149
+ if (canStream) {
150
+ return new CompletionStream_js_1.DefaultCompletionStream(this, prompt, options);
151
+ }
152
+ else if (this.isImageModel(options.model)) {
153
+ return new CompletionStream_js_1.FallbackCompletionStream(this, prompt, options);
154
+ }
155
+ else {
156
+ return new CompletionStream_js_1.FallbackCompletionStream(this, prompt, options);
157
+ }
158
+ }
159
+ /**
160
+ * Override this method to provide a custom prompt formatter
161
+ * @param segments
162
+ * @param options
163
+ * @returns
164
+ */
165
+ async formatPrompt(segments, opts) {
166
+ return (0, index_js_1.formatTextPrompt)(segments, opts.result_schema);
167
+ }
168
+ async createPrompt(segments, opts) {
169
+ return await (opts.format ? opts.format(segments, opts.result_schema) : this.formatPrompt(segments, opts));
170
+ }
171
+ /**
172
+ * Must be overridden if the implementation cannot stream.
173
+ * Some implementation may be able to stream for certain models but not for others.
174
+ * You must overwrite and return false if the current model doesn't support streaming.
175
+ * The default implementation returns true, so it is assumed that the streaming can be done.
176
+ * If this method returns false then the streaming execution will fallback on a blocking execution streaming the entire response as a single event.
177
+ * @param options the execution options containing the target model name.
178
+ * @returns true if the execution can be streamed false otherwise.
179
+ */
180
+ canStream(_options) {
181
+ return Promise.resolve(true);
182
+ }
183
+ /**
184
+ * Get a list of models that can be trained.
185
+ * The default is to return an empty array
186
+ * @returns
187
+ */
188
+ async listTrainableModels() {
189
+ return [];
190
+ }
191
+ /**
192
+ * Build the conversation context after streaming completion.
193
+ * Override this in driver implementations that support multi-turn conversations.
194
+ *
195
+ * @param prompt - The prompt that was sent (includes prior conversation context)
196
+ * @param result - The completion results from the streamed response
197
+ * @param toolUse - The tool calls from the streamed response (if any)
198
+ * @param options - The execution options
199
+ * @returns The updated conversation context, or undefined if not supported
200
+ */
201
+ buildStreamingConversation(_prompt, _result, _toolUse, _options) {
202
+ // Default implementation returns undefined - drivers can override
203
+ return undefined;
204
+ }
205
+ /**
206
+ * Format an error into LlumiverseError. Override in driver implementations
207
+ * to provide provider-specific error parsing.
208
+ *
209
+ * The default implementation uses common patterns:
210
+ * - Status 429, 408: retryable (rate limit, timeout)
211
+ * - Status 529: retryable (overloaded)
212
+ * - Status 5xx: retryable (server errors)
213
+ * - Status 4xx (except above): not retryable (client errors)
214
+ * - Error messages containing "rate limit", "timeout", etc.: retryable
215
+ *
216
+ * @param error - The error to format
217
+ * @param context - Context about where the error occurred
218
+ * @returns A standardized LlumiverseError
219
+ */
220
+ formatLlumiverseError(error, context) {
221
+ // Extract status code from common locations (only if numeric)
222
+ let code;
223
+ const rawCode = error?.status
224
+ || error?.statusCode
225
+ || error?.code;
226
+ if (typeof rawCode === 'number') {
227
+ code = rawCode;
228
+ }
229
+ // Extract error name if available
230
+ const errorName = error?.name;
231
+ // Extract message
232
+ const message = error instanceof Error
233
+ ? error.message
234
+ : String(error);
235
+ // Determine retryability
236
+ const retryable = this.isRetryableError(code, message);
237
+ return new common_1.LlumiverseError(`[${this.provider}] ${message}`, retryable, context, error, code, errorName);
238
+ }
239
+ /**
240
+ * Determine if an error is retryable based on status code and message.
241
+ * Can be overridden by drivers for provider-specific logic.
242
+ *
243
+ * @param statusCode - The HTTP status code (if available)
244
+ * @param message - The error message
245
+ * @returns True if retryable, false if not retryable, undefined if unknown
246
+ */
247
+ isRetryableError(statusCode, message) {
248
+ // Numeric status codes
249
+ if (statusCode !== undefined) {
250
+ if (statusCode === 429 || statusCode === 408)
251
+ return true; // Rate limit, timeout
252
+ if (statusCode === 529)
253
+ return true; // Overloaded
254
+ if (statusCode >= 500 && statusCode < 600)
255
+ return true; // Server errors
256
+ return false; // 4xx client errors not retryable
257
+ }
258
+ // Message-based detection for non-HTTP errors
259
+ const lowerMessage = message.toLowerCase();
260
+ // Rate limit variations
261
+ if (lowerMessage.includes('rate') && lowerMessage.includes('limit'))
262
+ return true;
263
+ // Timeout variations (timeout, timed out, time out)
264
+ if (lowerMessage.includes('timeout'))
265
+ return true;
266
+ if (lowerMessage.includes('timed') && lowerMessage.includes('out'))
267
+ return true;
268
+ if (lowerMessage.includes('time') && lowerMessage.includes('out'))
269
+ return true;
270
+ // Resource exhausted variations
271
+ if (lowerMessage.includes('resource') && lowerMessage.includes('exhaust'))
272
+ return true;
273
+ // Other retryable patterns
274
+ if (lowerMessage.includes('retry'))
275
+ return true;
276
+ if (lowerMessage.includes('overload'))
277
+ return true;
278
+ if (lowerMessage.includes('throttl'))
279
+ return true;
280
+ if (lowerMessage.includes('429'))
281
+ return true;
282
+ if (lowerMessage.includes('529'))
283
+ return true;
284
+ // Unknown errors - let consumer decide retry strategy
285
+ return undefined;
286
+ }
287
+ async requestImageGeneration(_prompt, _options) {
288
+ throw new Error("Image generation not implemented.");
289
+ //Cannot be made abstract, as abstract methods are required in the derived class
290
+ }
291
+ /**
292
+ * Cleanup method called when the driver is evicted from the cache.
293
+ * Override this in driver implementations that need to release resources.
294
+ */
295
+ destroy() {
296
+ // No-op by default
297
+ }
298
+ }
299
+ exports.AbstractDriver = AbstractDriver;
300
+ //# sourceMappingURL=Driver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Driver.js","sourceRoot":"","sources":["../../src/Driver.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AA2DH,oCAQC;AAjED,+CAqB4B;AAC5B,+DAA0F;AAC1F,oDAAyD;AACzD,mDAAiD;AAEjD,6FAA6F;AAC7F,SAAS,yBAAyB,CAAC,aAA2C;IAC1E,OAAO,CAAC,CAAC,QAAa,EAAE,UAAgB,EAAE,GAAG,IAAmC,EAAE,EAAE;QAChF,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;YAC/B,gDAAgD;YAChD,aAAa,CAAC,QAAQ,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC,CAAC;QACjD,CAAC;aAAM,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;YAClC,yDAAyD;YACzD,aAAa,CAAC,UAAU,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,CAAC;QACjD,CAAC;aAAM,CAAC;YACJ,oCAAoC;YACpC,aAAa,CAAC,QAAQ,EAAE,GAAG,IAAI,CAAC,CAAC;QACrC,CAAC;IACL,CAAC,CAAmB,CAAC;AACzB,CAAC;AAED,MAAM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