hazo_llm_api 1.0.1 → 1.0.3

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 (55) hide show
  1. package/dist/index.d.ts +4 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +8 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/lib/database/index.d.ts +1 -1
  6. package/dist/lib/database/index.d.ts.map +1 -1
  7. package/dist/lib/database/index.js +3 -1
  8. package/dist/lib/database/index.js.map +1 -1
  9. package/dist/lib/database/init_database.d.ts +56 -0
  10. package/dist/lib/database/init_database.d.ts.map +1 -1
  11. package/dist/lib/database/init_database.js +88 -0
  12. package/dist/lib/database/init_database.js.map +1 -1
  13. package/dist/lib/llm_api/hazo_llm_image_image.d.ts.map +1 -1
  14. package/dist/lib/llm_api/hazo_llm_image_image.js +3 -1
  15. package/dist/lib/llm_api/hazo_llm_image_image.js.map +1 -1
  16. package/dist/lib/llm_api/hazo_llm_image_image_text.d.ts.map +1 -1
  17. package/dist/lib/llm_api/hazo_llm_image_image_text.js +3 -1
  18. package/dist/lib/llm_api/hazo_llm_image_image_text.js.map +1 -1
  19. package/dist/lib/llm_api/hazo_llm_image_text.d.ts.map +1 -1
  20. package/dist/lib/llm_api/hazo_llm_image_text.js +3 -1
  21. package/dist/lib/llm_api/hazo_llm_image_text.js.map +1 -1
  22. package/dist/lib/llm_api/hazo_llm_text_image.d.ts.map +1 -1
  23. package/dist/lib/llm_api/hazo_llm_text_image.js +3 -1
  24. package/dist/lib/llm_api/hazo_llm_text_image.js.map +1 -1
  25. package/dist/lib/llm_api/hazo_llm_text_image_text.d.ts.map +1 -1
  26. package/dist/lib/llm_api/hazo_llm_text_image_text.js +3 -1
  27. package/dist/lib/llm_api/hazo_llm_text_image_text.js.map +1 -1
  28. package/dist/lib/llm_api/hazo_llm_text_text.d.ts.map +1 -1
  29. package/dist/lib/llm_api/hazo_llm_text_text.js +3 -1
  30. package/dist/lib/llm_api/hazo_llm_text_text.js.map +1 -1
  31. package/dist/lib/llm_api/index.d.ts +109 -17
  32. package/dist/lib/llm_api/index.d.ts.map +1 -1
  33. package/dist/lib/llm_api/index.js +248 -32
  34. package/dist/lib/llm_api/index.js.map +1 -1
  35. package/dist/lib/llm_api/provider_helper.d.ts +61 -5
  36. package/dist/lib/llm_api/provider_helper.d.ts.map +1 -1
  37. package/dist/lib/llm_api/provider_helper.js +197 -10
  38. package/dist/lib/llm_api/provider_helper.js.map +1 -1
  39. package/dist/lib/llm_api/types.d.ts +211 -38
  40. package/dist/lib/llm_api/types.d.ts.map +1 -1
  41. package/dist/lib/llm_api/types.js +42 -1
  42. package/dist/lib/llm_api/types.js.map +1 -1
  43. package/dist/lib/providers/registry.d.ts +3 -3
  44. package/dist/lib/providers/registry.d.ts.map +1 -1
  45. package/dist/lib/providers/registry.js +1 -1
  46. package/dist/lib/providers/registry.js.map +1 -1
  47. package/dist/lib/providers/types.d.ts +51 -1
  48. package/dist/lib/providers/types.d.ts.map +1 -1
  49. package/dist/lib/providers/types.js +18 -0
  50. package/dist/lib/providers/types.js.map +1 -1
  51. package/dist/server.d.ts +8 -3
  52. package/dist/server.d.ts.map +1 -1
  53. package/dist/server.js +19 -2
  54. package/dist/server.js.map +1 -1
  55. package/package.json +1 -1
@@ -12,15 +12,68 @@
12
12
  *
13
13
  * Database is auto-initialized on module import using config defaults.
14
14
  */
15
- import type { LLMApiConfig, LLMApiClient, LLMResponse, TextTextParams, ImageTextParams, TextImageParams, ImageImageParams, TextImageTextParams, ImageImageTextParams } from './types.js';
15
+ import type { LLMApiConfig, LLMApiClient, LLMResponse, LLMStreamResponse, TextTextParams, ImageTextParams, TextImageParams, ImageImageParams, TextImageTextParams, ImageImageTextParams, Logger } from './types.js';
16
+ import type { ProviderName } from '../providers/types.js';
17
+ /**
18
+ * Default console logger used when no custom logger is provided
19
+ * Can be used directly or as a fallback in functions
20
+ */
21
+ export declare const default_logger: Logger;
22
+ /**
23
+ * Get the current logger instance
24
+ * Returns the stored logger (set during initialization) or default logger
25
+ *
26
+ * @returns Current logger instance
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * import { get_logger } from 'hazo_llm_api/server';
31
+ *
32
+ * const logger = get_logger();
33
+ * logger.info('My message');
34
+ * ```
35
+ */
36
+ export declare function get_logger(): Logger;
37
+ /**
38
+ * Set the logger instance
39
+ * Called internally during initialization, but can also be called directly
40
+ *
41
+ * @param logger - Logger instance to use
42
+ */
43
+ export declare function set_logger(logger: Logger): void;
44
+ /**
45
+ * Get the current hooks configuration
46
+ *
47
+ * @returns Current hooks configuration
48
+ */
49
+ export declare function get_hooks(): import('./types.js').LLMHooks;
50
+ /**
51
+ * Set the hooks configuration
52
+ * Called internally during initialization, but can also be called directly
53
+ *
54
+ * @param hooks - Hooks configuration to use
55
+ */
56
+ export declare function set_hooks(hooks: import('./types.js').LLMHooks): void;
16
57
  /**
17
58
  * Initialize the LLM API with the given configuration
18
59
  * Creates/connects to the database and prepares the client for use
19
60
  *
20
- * @param config - Configuration options for the LLM API
61
+ * @param config - Configuration options for the LLM API (all fields optional)
21
62
  * @returns Initialized LLM API client
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * // Minimal initialization (uses defaults)
67
+ * const api = await initialize_llm_api({});
68
+ *
69
+ * // With custom logger
70
+ * const api = await initialize_llm_api({ logger: myLogger });
71
+ *
72
+ * // With custom database path
73
+ * const api = await initialize_llm_api({ sqlite_path: '~/data/prompts.db' });
74
+ * ```
22
75
  */
23
- export declare function initialize_llm_api(config: LLMApiConfig): Promise<LLMApiClient>;
76
+ export declare function initialize_llm_api(config?: LLMApiConfig): Promise<LLMApiClient>;
24
77
  /**
25
78
  * Check if database has been initialized (either auto or manual)
26
79
  * @returns true if database is ready for use
@@ -36,64 +89,103 @@ export declare function ensure_database_ready(): Promise<boolean>;
36
89
  * Standard text generation using LLM
37
90
  *
38
91
  * @param params - Text input parameters
39
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
92
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
40
93
  * @returns LLM response with generated text
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * import { hazo_llm_text_text, LLM_PROVIDERS } from 'hazo_llm_api/server';
98
+ *
99
+ * const response = await hazo_llm_text_text({ prompt: 'Hello' }, LLM_PROVIDERS.GEMINI);
100
+ * ```
41
101
  */
42
- export declare function hazo_llm_text_text(params: TextTextParams, llm?: string): Promise<LLMResponse>;
102
+ export declare function hazo_llm_text_text(params: TextTextParams, llm?: ProviderName): Promise<LLMResponse>;
43
103
  /**
44
104
  * Image input → Text output
45
105
  * Analyze an image and get text description
46
106
  *
47
107
  * @param params - Image input parameters
48
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
108
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
49
109
  * @returns LLM response with text description
50
110
  */
51
- export declare function hazo_llm_image_text(params: ImageTextParams, llm?: string): Promise<LLMResponse>;
111
+ export declare function hazo_llm_image_text(params: ImageTextParams, llm?: ProviderName): Promise<LLMResponse>;
52
112
  /**
53
113
  * Text input → Image output
54
114
  * Generate an image from text description
55
115
  *
56
116
  * @param params - Text input parameters for image generation
57
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
117
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
58
118
  * @returns LLM response with generated image
59
119
  */
60
- export declare function hazo_llm_text_image(params: TextImageParams, llm?: string): Promise<LLMResponse>;
120
+ export declare function hazo_llm_text_image(params: TextImageParams, llm?: ProviderName): Promise<LLMResponse>;
61
121
  /**
62
122
  * Image input → Image output
63
123
  * Transform/edit an image based on instructions
64
124
  *
65
125
  * @param params - Image input parameters with transformation instructions
66
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
126
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
67
127
  * @returns LLM response with transformed image
68
128
  */
69
- export declare function hazo_llm_image_image(params: ImageImageParams, llm?: string): Promise<LLMResponse>;
129
+ export declare function hazo_llm_image_image(params: ImageImageParams, llm?: ProviderName): Promise<LLMResponse>;
70
130
  /**
71
131
  * Text → Image → Text (Chained)
72
132
  * Generate an image from prompt_image, then analyze it with prompt_text
73
133
  *
74
134
  * @param params - Parameters with two prompts: one for image gen, one for analysis
75
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
135
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
76
136
  * @returns LLM response with generated image and analysis text
77
137
  */
78
- export declare function hazo_llm_text_image_text(params: TextImageTextParams, llm?: string): Promise<LLMResponse>;
138
+ export declare function hazo_llm_text_image_text(params: TextImageTextParams, llm?: ProviderName): Promise<LLMResponse>;
79
139
  /**
80
140
  * Images → Image → Text (Chained)
81
141
  * Chain multiple image transformations, then describe the final result
82
142
  *
83
143
  * @param params - Parameters with images, prompts, and description prompt
84
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
144
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
85
145
  * @returns LLM response with final image and description text
86
146
  */
87
- export declare function hazo_llm_image_image_text(params: ImageImageTextParams, llm?: string): Promise<LLMResponse>;
147
+ export declare function hazo_llm_image_image_text(params: ImageImageTextParams, llm?: ProviderName): Promise<LLMResponse>;
148
+ /**
149
+ * Text input → Text output (Streaming)
150
+ * Generate text from a prompt with streaming response
151
+ *
152
+ * @param params - Text input parameters
153
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified)
154
+ * @returns Async generator yielding text chunks
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * const stream = await hazo_llm_text_text_stream({ prompt: 'Tell me a story' });
159
+ *
160
+ * for await (const chunk of stream) {
161
+ * if (chunk.error) {
162
+ * console.error(chunk.error);
163
+ * break;
164
+ * }
165
+ * process.stdout.write(chunk.text);
166
+ * if (chunk.done) break;
167
+ * }
168
+ * ```
169
+ */
170
+ export declare function hazo_llm_text_text_stream(params: TextTextParams, llm?: ProviderName): LLMStreamResponse;
171
+ /**
172
+ * Image input → Text output (Streaming)
173
+ * Analyze an image and stream text description
174
+ *
175
+ * @param params - Image input parameters
176
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified)
177
+ * @returns Async generator yielding text chunks
178
+ */
179
+ export declare function hazo_llm_image_text_stream(params: ImageTextParams, llm?: ProviderName): LLMStreamResponse;
88
180
  /**
89
181
  * Check if the LLM API has been initialized
90
182
  * @returns true if initialized
91
183
  */
92
184
  export declare function is_initialized(): boolean;
93
185
  /**
94
- * Get the current configuration (without sensitive data)
186
+ * Get the current configuration (without sensitive logger)
95
187
  * @returns Current configuration or null if not initialized
96
188
  */
97
- export declare function get_current_config(): Omit<LLMApiConfig, 'api_key' | 'logger'> | null;
189
+ export declare function get_current_config(): Omit<LLMApiConfig, 'logger'> | null;
98
190
  export type { LLMApiConfig, LLMApiClient, LLMResponse, TextTextParams, ImageTextParams, TextImageParams, ImageImageParams, TextImageTextParams, ImageImageTextParams, ChainImage, Logger, PromptVariable, PromptVariables, Base64Data, PromptTextMode, PromptRecord, CallLLMParams, GeminiGenerationConfig, GeminiApiGenerationConfig, } from './types.js';
99
191
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/llm_api/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,cAAc,EACd,eAAe,EACf,eAAe,EACf,gBAAgB,EAChB,mBAAmB,EACnB,oBAAoB,EAGrB,MAAM,YAAY,CAAC;AAqwBpB;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CA8FpF;AAgBD;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI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ACb,sBAAsB,EACtB,yBAAyB,GAC1B,MAAM,YAAY,CAAC"}
@@ -12,6 +12,7 @@
12
12
  *
13
13
  * Database is auto-initialized on module import using config defaults.
14
14
  */
15
+ import { LLM_ERROR_CODES } from './types.js';
15
16
  import { initialize_database, get_database } from '../database/init_database.js';
16
17
  import { hazo_llm_text_text as hazo_llm_text_text_internal } from './hazo_llm_text_text.js';
17
18
  import { hazo_llm_image_text as hazo_llm_image_text_internal } from './hazo_llm_image_text.js';
@@ -19,7 +20,7 @@ import { hazo_llm_text_image as hazo_llm_text_image_internal } from './hazo_llm_
19
20
  import { hazo_llm_image_image as hazo_llm_image_image_internal } from './hazo_llm_image_image.js';
20
21
  import { hazo_llm_text_image_text as hazo_llm_text_image_text_internal } from './hazo_llm_text_image_text.js';
21
22
  import { hazo_llm_image_image_text as hazo_llm_image_image_text_internal } from './hazo_llm_image_image_text.js';
22
- import { register_provider, set_enabled_llms, set_primary_llm, get_primary_llm, get_registered_providers, } from '../providers/registry.js';
23
+ import { register_provider, set_enabled_llms, set_primary_llm, get_primary_llm, get_registered_providers, get_provider, } from '../providers/registry.js';
23
24
  import { GeminiProvider } from '../providers/gemini/index.js';
24
25
  import { QwenProvider } from '../providers/qwen/index.js';
25
26
  import { SERVICE_TYPES } from '../providers/types.js';
@@ -33,12 +34,13 @@ let initialized = false;
33
34
  let db_auto_initialized = false;
34
35
  let current_config = null;
35
36
  // =============================================================================
36
- // Default Logger (for auto-initialization)
37
+ // Default Logger
37
38
  // =============================================================================
38
39
  /**
39
- * Default console logger for auto-initialization when no custom logger provided
40
+ * Default console logger used when no custom logger is provided
41
+ * Can be used directly or as a fallback in functions
40
42
  */
41
- const default_logger = {
43
+ export const default_logger = {
42
44
  error: (message, meta) => {
43
45
  console.error(`[HAZO_LLM_API ERROR] ${message}`, meta ? JSON.stringify(meta, null, 2) : '');
44
46
  },
@@ -52,6 +54,57 @@ const default_logger = {
52
54
  console.debug(`[HAZO_LLM_API DEBUG] ${message}`, meta ? JSON.stringify(meta, null, 2) : '');
53
55
  },
54
56
  };
57
+ /**
58
+ * Stored logger instance - set during initialization
59
+ */
60
+ let stored_logger = default_logger;
61
+ /**
62
+ * Stored hooks instance - set during initialization
63
+ */
64
+ let stored_hooks = {};
65
+ /**
66
+ * Get the current logger instance
67
+ * Returns the stored logger (set during initialization) or default logger
68
+ *
69
+ * @returns Current logger instance
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * import { get_logger } from 'hazo_llm_api/server';
74
+ *
75
+ * const logger = get_logger();
76
+ * logger.info('My message');
77
+ * ```
78
+ */
79
+ export function get_logger() {
80
+ return stored_logger;
81
+ }
82
+ /**
83
+ * Set the logger instance
84
+ * Called internally during initialization, but can also be called directly
85
+ *
86
+ * @param logger - Logger instance to use
87
+ */
88
+ export function set_logger(logger) {
89
+ stored_logger = logger;
90
+ }
91
+ /**
92
+ * Get the current hooks configuration
93
+ *
94
+ * @returns Current hooks configuration
95
+ */
96
+ export function get_hooks() {
97
+ return stored_hooks;
98
+ }
99
+ /**
100
+ * Set the hooks configuration
101
+ * Called internally during initialization, but can also be called directly
102
+ *
103
+ * @param hooks - Hooks configuration to use
104
+ */
105
+ export function set_hooks(hooks) {
106
+ stored_hooks = hooks;
107
+ }
55
108
  // =============================================================================
56
109
  // Config Reader
57
110
  // =============================================================================
@@ -387,13 +440,14 @@ function load_gemini_provider_from_config(logger) {
387
440
  const config_content = fs.readFileSync(config_path, 'utf-8');
388
441
  const config = ini.parse(config_content);
389
442
  const gemini_section = config.llm_gemini || {};
390
- // Load API key from environment
391
- const api_key = load_api_key_from_env('gemini');
443
+ // Support custom env var name via api_key_env config option
444
+ const env_var_name = gemini_section.api_key_env || 'GEMINI_API_KEY';
445
+ const api_key = process.env[env_var_name];
392
446
  if (!api_key) {
393
- logger.error('GEMINI_API_KEY not found in environment variables', {
447
+ logger.error(`${env_var_name} not found in environment variables`, {
394
448
  file: 'index.ts',
395
449
  line: 352,
396
- data: { config_path },
450
+ data: { config_path, env_var_name },
397
451
  });
398
452
  return null;
399
453
  }
@@ -544,13 +598,14 @@ function load_qwen_provider_from_config(logger) {
544
598
  const config_content = fs.readFileSync(config_path, 'utf-8');
545
599
  const config = ini.parse(config_content);
546
600
  const qwen_section = config.llm_qwen || {};
547
- // Load API key from environment
548
- const api_key = load_api_key_from_env('qwen');
601
+ // Support custom env var name via api_key_env config option
602
+ const env_var_name = qwen_section.api_key_env || 'QWEN_API_KEY';
603
+ const api_key = process.env[env_var_name];
549
604
  if (!api_key) {
550
- logger.error('QWEN_API_KEY not found in environment variables', {
605
+ logger.error(`${env_var_name} not found in environment variables`, {
551
606
  file: 'index.ts',
552
607
  line: 512,
553
- data: { config_path },
608
+ data: { config_path, env_var_name },
554
609
  });
555
610
  return null;
556
611
  }
@@ -697,12 +752,31 @@ void auto_initialize_database();
697
752
  * Initialize the LLM API with the given configuration
698
753
  * Creates/connects to the database and prepares the client for use
699
754
  *
700
- * @param config - Configuration options for the LLM API
755
+ * @param config - Configuration options for the LLM API (all fields optional)
701
756
  * @returns Initialized LLM API client
757
+ *
758
+ * @example
759
+ * ```typescript
760
+ * // Minimal initialization (uses defaults)
761
+ * const api = await initialize_llm_api({});
762
+ *
763
+ * // With custom logger
764
+ * const api = await initialize_llm_api({ logger: myLogger });
765
+ *
766
+ * // With custom database path
767
+ * const api = await initialize_llm_api({ sqlite_path: '~/data/prompts.db' });
768
+ * ```
702
769
  */
703
- export async function initialize_llm_api(config) {
770
+ export async function initialize_llm_api(config = {}) {
704
771
  const file_name = 'index.ts (llm_api)';
705
- const logger = config.logger;
772
+ // Use provided logger or default
773
+ const logger = config.logger || default_logger;
774
+ // Store the logger for use by other functions
775
+ set_logger(logger);
776
+ // Store hooks if provided
777
+ if (config.hooks) {
778
+ set_hooks(config.hooks);
779
+ }
706
780
  // Get global config from file
707
781
  const global_config = get_llm_global_config();
708
782
  // Use provided sqlite_path or fall back to config file value
@@ -728,15 +802,11 @@ export async function initialize_llm_api(config) {
728
802
  primary_llm: primary_llm_name,
729
803
  },
730
804
  });
731
- // Set final config for backward compatibility (simplified)
805
+ // Set final config
732
806
  const final_config = {
733
- logger: config.logger,
807
+ logger,
734
808
  sqlite_path,
735
- // Legacy fields kept for backward compatibility but not used by providers
736
- api_url: config.api_url,
737
- api_url_image: config.api_url_image,
738
- api_key: config.api_key,
739
- llm_model: primary_llm_name, // Use primary_llm as default
809
+ hooks: config.hooks,
740
810
  };
741
811
  // Initialize the database (async)
742
812
  try {
@@ -791,12 +861,17 @@ export async function initialize_llm_api(config) {
791
861
  // =============================================================================
792
862
  /**
793
863
  * Helper to check full LLM API initialization (required for LLM calls)
864
+ * Ensures logger is always present in returned config
794
865
  */
795
866
  function check_initialized() {
796
867
  if (!initialized || !current_config) {
797
868
  throw new Error('LLM API not initialized. Call initialize_llm_api first.');
798
869
  }
799
- return current_config;
870
+ // Ensure logger is always present
871
+ return {
872
+ ...current_config,
873
+ logger: current_config.logger || get_logger(),
874
+ };
800
875
  }
801
876
  /**
802
877
  * Check if database has been initialized (either auto or manual)
@@ -822,8 +897,15 @@ export async function ensure_database_ready() {
822
897
  * Standard text generation using LLM
823
898
  *
824
899
  * @param params - Text input parameters
825
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
900
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
826
901
  * @returns LLM response with generated text
902
+ *
903
+ * @example
904
+ * ```typescript
905
+ * import { hazo_llm_text_text, LLM_PROVIDERS } from 'hazo_llm_api/server';
906
+ *
907
+ * const response = await hazo_llm_text_text({ prompt: 'Hello' }, LLM_PROVIDERS.GEMINI);
908
+ * ```
827
909
  */
828
910
  export async function hazo_llm_text_text(params, llm) {
829
911
  try {
@@ -840,7 +922,7 @@ export async function hazo_llm_text_text(params, llm) {
840
922
  * Analyze an image and get text description
841
923
  *
842
924
  * @param params - Image input parameters
843
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
925
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
844
926
  * @returns LLM response with text description
845
927
  */
846
928
  export async function hazo_llm_image_text(params, llm) {
@@ -858,7 +940,7 @@ export async function hazo_llm_image_text(params, llm) {
858
940
  * Generate an image from text description
859
941
  *
860
942
  * @param params - Text input parameters for image generation
861
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
943
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
862
944
  * @returns LLM response with generated image
863
945
  */
864
946
  export async function hazo_llm_text_image(params, llm) {
@@ -876,7 +958,7 @@ export async function hazo_llm_text_image(params, llm) {
876
958
  * Transform/edit an image based on instructions
877
959
  *
878
960
  * @param params - Image input parameters with transformation instructions
879
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
961
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
880
962
  * @returns LLM response with transformed image
881
963
  */
882
964
  export async function hazo_llm_image_image(params, llm) {
@@ -894,7 +976,7 @@ export async function hazo_llm_image_image(params, llm) {
894
976
  * Generate an image from prompt_image, then analyze it with prompt_text
895
977
  *
896
978
  * @param params - Parameters with two prompts: one for image gen, one for analysis
897
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
979
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
898
980
  * @returns LLM response with generated image and analysis text
899
981
  */
900
982
  export async function hazo_llm_text_image_text(params, llm) {
@@ -912,7 +994,7 @@ export async function hazo_llm_text_image_text(params, llm) {
912
994
  * Chain multiple image transformations, then describe the final result
913
995
  *
914
996
  * @param params - Parameters with images, prompts, and description prompt
915
- * @param llm - Optional LLM provider name (uses primary LLM if not specified)
997
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified). Use LLM_PROVIDERS constants for type safety.
916
998
  * @returns LLM response with final image and description text
917
999
  */
918
1000
  export async function hazo_llm_image_image_text(params, llm) {
@@ -926,6 +1008,141 @@ export async function hazo_llm_image_image_text(params, llm) {
926
1008
  }
927
1009
  }
928
1010
  // =============================================================================
1011
+ // Streaming Functions
1012
+ // =============================================================================
1013
+ /**
1014
+ * Text input → Text output (Streaming)
1015
+ * Generate text from a prompt with streaming response
1016
+ *
1017
+ * @param params - Text input parameters
1018
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified)
1019
+ * @returns Async generator yielding text chunks
1020
+ *
1021
+ * @example
1022
+ * ```typescript
1023
+ * const stream = await hazo_llm_text_text_stream({ prompt: 'Tell me a story' });
1024
+ *
1025
+ * for await (const chunk of stream) {
1026
+ * if (chunk.error) {
1027
+ * console.error(chunk.error);
1028
+ * break;
1029
+ * }
1030
+ * process.stdout.write(chunk.text);
1031
+ * if (chunk.done) break;
1032
+ * }
1033
+ * ```
1034
+ */
1035
+ export async function* hazo_llm_text_text_stream(params, llm) {
1036
+ try {
1037
+ const config = check_initialized();
1038
+ const logger = config.logger || get_logger();
1039
+ // Get provider
1040
+ const provider = get_provider(llm, logger);
1041
+ if (!provider) {
1042
+ yield {
1043
+ text: '',
1044
+ done: true,
1045
+ error: `Provider "${llm || 'primary'}" not found`,
1046
+ error_info: {
1047
+ code: LLM_ERROR_CODES.PROVIDER_NOT_FOUND,
1048
+ message: `Provider "${llm || 'primary'}" not found`,
1049
+ retryable: false,
1050
+ },
1051
+ };
1052
+ return;
1053
+ }
1054
+ // Check if provider supports streaming
1055
+ if (!provider.text_text_stream) {
1056
+ yield {
1057
+ text: '',
1058
+ done: true,
1059
+ error: `Provider "${provider.get_name()}" does not support streaming`,
1060
+ error_info: {
1061
+ code: LLM_ERROR_CODES.CAPABILITY_NOT_SUPPORTED,
1062
+ message: `Provider "${provider.get_name()}" does not support streaming for text_text`,
1063
+ retryable: false,
1064
+ },
1065
+ };
1066
+ return;
1067
+ }
1068
+ // Call streaming method
1069
+ const stream = await provider.text_text_stream(params, logger);
1070
+ yield* stream;
1071
+ }
1072
+ catch (error) {
1073
+ const error_message = error instanceof Error ? error.message : String(error);
1074
+ yield {
1075
+ text: '',
1076
+ done: true,
1077
+ error: error_message,
1078
+ error_info: {
1079
+ code: LLM_ERROR_CODES.UNKNOWN,
1080
+ message: error_message,
1081
+ retryable: false,
1082
+ },
1083
+ };
1084
+ }
1085
+ }
1086
+ /**
1087
+ * Image input → Text output (Streaming)
1088
+ * Analyze an image and stream text description
1089
+ *
1090
+ * @param params - Image input parameters
1091
+ * @param llm - Optional LLM provider name (uses primary LLM if not specified)
1092
+ * @returns Async generator yielding text chunks
1093
+ */
1094
+ export async function* hazo_llm_image_text_stream(params, llm) {
1095
+ try {
1096
+ const config = check_initialized();
1097
+ const logger = config.logger || get_logger();
1098
+ // Get provider
1099
+ const provider = get_provider(llm, logger);
1100
+ if (!provider) {
1101
+ yield {
1102
+ text: '',
1103
+ done: true,
1104
+ error: `Provider "${llm || 'primary'}" not found`,
1105
+ error_info: {
1106
+ code: LLM_ERROR_CODES.PROVIDER_NOT_FOUND,
1107
+ message: `Provider "${llm || 'primary'}" not found`,
1108
+ retryable: false,
1109
+ },
1110
+ };
1111
+ return;
1112
+ }
1113
+ // Check if provider supports streaming
1114
+ if (!provider.image_text_stream) {
1115
+ yield {
1116
+ text: '',
1117
+ done: true,
1118
+ error: `Provider "${provider.get_name()}" does not support streaming`,
1119
+ error_info: {
1120
+ code: LLM_ERROR_CODES.CAPABILITY_NOT_SUPPORTED,
1121
+ message: `Provider "${provider.get_name()}" does not support streaming for image_text`,
1122
+ retryable: false,
1123
+ },
1124
+ };
1125
+ return;
1126
+ }
1127
+ // Call streaming method
1128
+ const stream = await provider.image_text_stream(params, logger);
1129
+ yield* stream;
1130
+ }
1131
+ catch (error) {
1132
+ const error_message = error instanceof Error ? error.message : String(error);
1133
+ yield {
1134
+ text: '',
1135
+ done: true,
1136
+ error: error_message,
1137
+ error_info: {
1138
+ code: LLM_ERROR_CODES.UNKNOWN,
1139
+ message: error_message,
1140
+ retryable: false,
1141
+ },
1142
+ };
1143
+ }
1144
+ }
1145
+ // =============================================================================
929
1146
  // Utility Functions
930
1147
  // =============================================================================
931
1148
  /**
@@ -936,7 +1153,7 @@ export function is_initialized() {
936
1153
  return initialized;
937
1154
  }
938
1155
  /**
939
- * Get the current configuration (without sensitive data)
1156
+ * Get the current configuration (without sensitive logger)
940
1157
  * @returns Current configuration or null if not initialized
941
1158
  */
942
1159
  export function get_current_config() {
@@ -944,9 +1161,8 @@ export function get_current_config() {
944
1161
  return null;
945
1162
  }
946
1163
  return {
947
- llm_model: current_config.llm_model,
948
1164
  sqlite_path: current_config.sqlite_path,
949
- api_url: current_config.api_url,
1165
+ hooks: current_config.hooks,
950
1166
  };
951
1167
  }
952
1168
  //# sourceMappingURL=index.js.map