@qianxude/ai 0.2.1

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 (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +599 -0
  3. package/dist/client/client.d.ts +56 -0
  4. package/dist/client/client.d.ts.map +1 -0
  5. package/dist/client/client.js +285 -0
  6. package/dist/client/client.js.map +1 -0
  7. package/dist/client/config.d.ts +34 -0
  8. package/dist/client/config.d.ts.map +1 -0
  9. package/dist/client/config.js +141 -0
  10. package/dist/client/config.js.map +1 -0
  11. package/dist/client/index.d.ts +9 -0
  12. package/dist/client/index.d.ts.map +1 -0
  13. package/dist/client/index.js +13 -0
  14. package/dist/client/index.js.map +1 -0
  15. package/dist/client/providers.d.ts +27 -0
  16. package/dist/client/providers.d.ts.map +1 -0
  17. package/dist/client/providers.js +235 -0
  18. package/dist/client/providers.js.map +1 -0
  19. package/dist/client/task.d.ts +59 -0
  20. package/dist/client/task.d.ts.map +1 -0
  21. package/dist/client/task.js +179 -0
  22. package/dist/client/task.js.map +1 -0
  23. package/dist/index.d.ts +3 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +5 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/types/base.d.ts +38 -0
  28. package/dist/types/base.d.ts.map +1 -0
  29. package/dist/types/base.js +6 -0
  30. package/dist/types/base.js.map +1 -0
  31. package/dist/types/client.d.ts +47 -0
  32. package/dist/types/client.d.ts.map +1 -0
  33. package/dist/types/client.js +2 -0
  34. package/dist/types/client.js.map +1 -0
  35. package/dist/types/common.d.ts +19 -0
  36. package/dist/types/common.d.ts.map +1 -0
  37. package/dist/types/common.js +31 -0
  38. package/dist/types/common.js.map +1 -0
  39. package/dist/types/config.d.ts +26 -0
  40. package/dist/types/config.d.ts.map +1 -0
  41. package/dist/types/config.js +2 -0
  42. package/dist/types/config.js.map +1 -0
  43. package/dist/types/index.d.ts +14 -0
  44. package/dist/types/index.d.ts.map +1 -0
  45. package/dist/types/index.js +7 -0
  46. package/dist/types/index.js.map +1 -0
  47. package/dist/types/message.d.ts +25 -0
  48. package/dist/types/message.d.ts.map +1 -0
  49. package/dist/types/message.js +2 -0
  50. package/dist/types/message.js.map +1 -0
  51. package/dist/types/mod.d.ts +2 -0
  52. package/dist/types/mod.d.ts.map +1 -0
  53. package/dist/types/mod.js +2 -0
  54. package/dist/types/mod.js.map +1 -0
  55. package/dist/types/options.d.ts +31 -0
  56. package/dist/types/options.d.ts.map +1 -0
  57. package/dist/types/options.js +5 -0
  58. package/dist/types/options.js.map +1 -0
  59. package/dist/types/provider.d.ts +22 -0
  60. package/dist/types/provider.d.ts.map +1 -0
  61. package/dist/types/provider.js +2 -0
  62. package/dist/types/provider.js.map +1 -0
  63. package/dist/types/response.d.ts +19 -0
  64. package/dist/types/response.d.ts.map +1 -0
  65. package/dist/types/response.js +5 -0
  66. package/dist/types/response.js.map +1 -0
  67. package/dist/types/task.d.ts +28 -0
  68. package/dist/types/task.d.ts.map +1 -0
  69. package/dist/types/task.js +2 -0
  70. package/dist/types/task.js.map +1 -0
  71. package/package.json +42 -0
@@ -0,0 +1,235 @@
1
+ // oxlint-disable no-unused-expressions -- Used for conditional property assignment pattern in API request body
2
+ /**
3
+ * Provider-specific API implementations for LLM services
4
+ */
5
+ import { logger } from '@qianxude/shared';
6
+ import { t } from '../types/mod.js';
7
+ const DEFAULT_PROVIDER_TIMEOUT = 60000;
8
+ /**
9
+ * OpenAI-compatible API client implementation
10
+ * Works with any provider using OpenAI-compatible protocol (CE, SiliconFlow, etc.)
11
+ */
12
+ export class OpenAICompatibleProvider {
13
+ logger;
14
+ constructor(lg) {
15
+ this.logger = lg ?? logger;
16
+ }
17
+ logDebug(label, data) {
18
+ this.logger.debug(`[Provider:${label}]`, { data });
19
+ }
20
+ async complete(config, call) {
21
+ const baseUrl = config.baseUrl.replace(/\/+$/, '');
22
+ const url = `${baseUrl}/chat/completions`;
23
+ // Convert prompt/systemPrompt to messages if messages not provided
24
+ const messages = this.buildMessages(call);
25
+ // get merged llm options from call and config
26
+ const apiOptions = {
27
+ temperature: call.temperature ?? config.temperature,
28
+ maxTokens: call.maxTokens ?? config.maxTokens,
29
+ topK: call.topK ?? config.topK,
30
+ topP: call.topP ?? config.topP,
31
+ };
32
+ // Log provider info
33
+ this.logDebug('Provider', config.provider);
34
+ this.logDebug('Model', config.model);
35
+ this.logDebug('URL', baseUrl);
36
+ this.logDebug('Params', apiOptions);
37
+ this.logDebug('Messages', messages);
38
+ // Always log messages at debug level for troubleshooting
39
+ this.logDebug('Request Messages', { messages });
40
+ const body = {
41
+ model: config.model,
42
+ stream: false,
43
+ messages,
44
+ };
45
+ // Skip parameters that are in excludeCaps
46
+ const excluded = new Set(config.excludeCaps || []);
47
+ !excluded.has('temperature') && apiOptions.temperature !== undefined && (body.temperature = apiOptions.temperature);
48
+ !excluded.has('topP') && apiOptions.topP !== undefined && (body.top_p = apiOptions.topP);
49
+ !excluded.has('topK') && apiOptions.topK !== undefined && (body.top_k = apiOptions.topK);
50
+ !excluded.has('maxTokens') && apiOptions.maxTokens !== undefined && (body.max_tokens = apiOptions.maxTokens);
51
+ this.logDebug('Body', body);
52
+ // Setup timeout handling
53
+ const controller = new AbortController();
54
+ const timeout = call.timeout ?? DEFAULT_PROVIDER_TIMEOUT;
55
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
56
+ try {
57
+ const response = await fetch(url, {
58
+ method: 'POST',
59
+ headers: {
60
+ 'Content-Type': 'application/json',
61
+ Authorization: `Bearer ${config.apiKey}`,
62
+ },
63
+ body: JSON.stringify(body),
64
+ signal: controller.signal,
65
+ });
66
+ if (!response.ok) {
67
+ const errorText = await response.text();
68
+ throw new t.ProviderError(`API error: ${response.status} ${response.statusText} - ${errorText}`, config.provider);
69
+ }
70
+ const data = (await response.json());
71
+ const content = data.choices[0]?.message?.content;
72
+ if (!content) {
73
+ throw new t.ProviderError('Empty response from API', config.provider);
74
+ }
75
+ const result = {
76
+ content,
77
+ usage: data.usage
78
+ ? {
79
+ promptTokens: data.usage.prompt_tokens,
80
+ completionTokens: data.usage.completion_tokens,
81
+ totalTokens: data.usage.total_tokens,
82
+ }
83
+ : undefined,
84
+ model: data.model || config.model,
85
+ };
86
+ this.logDebug('Response', {
87
+ content: result.content,
88
+ usage: result.usage,
89
+ });
90
+ // Always log response content at debug level for troubleshooting
91
+ this.logDebug('Response Content', { content: result.content });
92
+ return result;
93
+ }
94
+ catch (err) {
95
+ if (err instanceof t.ProviderError)
96
+ throw err;
97
+ if (err instanceof Error && err.name === 'AbortError') {
98
+ throw new t.ProviderError('Request timeout', config.provider, err);
99
+ }
100
+ this.logger.error('[Provider] Complete request failed', {
101
+ error: String(err),
102
+ provider: config.provider,
103
+ model: config.model,
104
+ });
105
+ throw new t.ProviderError(`Failed to complete request: ${String(err)}`, config.provider, err);
106
+ }
107
+ finally {
108
+ clearTimeout(timeoutId);
109
+ }
110
+ }
111
+ async *stream(config, call) {
112
+ const baseUrl = config.baseUrl.replace(/\/+$/, '');
113
+ const url = `${baseUrl}/chat/completions`;
114
+ // Convert prompt/systemPrompt to messages if messages not provided
115
+ const messages = this.buildMessages(call);
116
+ // Skip parameters that are in excludeCaps
117
+ const excludedCaps = new Set(config.excludeCaps || []);
118
+ // Always log messages at debug level for troubleshooting
119
+ this.logDebug('Stream Request Messages', { messages });
120
+ const body = {
121
+ model: config.model,
122
+ messages,
123
+ stream: true,
124
+ };
125
+ // Only add temperature if not excluded
126
+ if (!excludedCaps.has('temperature') && (call.temperature !== undefined || config.temperature !== undefined)) {
127
+ body.temperature = call.temperature ?? config.temperature;
128
+ }
129
+ // Only add top_p if not excluded
130
+ if (!excludedCaps.has('topP') && (call.topP !== undefined || config.topP !== undefined)) {
131
+ body.top_p = call.topP ?? config.topP;
132
+ }
133
+ // Only add top_k if not excluded
134
+ if (!excludedCaps.has('topK') && (call.topK !== undefined || config.topK !== undefined)) {
135
+ body.top_k = call.topK ?? config.topK;
136
+ }
137
+ // Only add max_tokens if not excluded
138
+ if (!excludedCaps.has('maxTokens') && (call.maxTokens !== undefined || config.maxTokens !== undefined)) {
139
+ body.max_tokens = call.maxTokens ?? config.maxTokens;
140
+ }
141
+ // Setup timeout handling
142
+ const controller = new AbortController();
143
+ const timeoutId = setTimeout(() => controller.abort(), call.timeout ?? 60000);
144
+ try {
145
+ const response = await fetch(url, {
146
+ method: 'POST',
147
+ headers: {
148
+ 'Content-Type': 'application/json',
149
+ Authorization: `Bearer ${config.apiKey}`,
150
+ },
151
+ body: JSON.stringify(body),
152
+ signal: controller.signal,
153
+ });
154
+ if (!response.ok) {
155
+ const errorText = await response.text();
156
+ throw new t.ProviderError(`API error: ${response.status} ${response.statusText} - ${errorText}`, config.provider);
157
+ }
158
+ const reader = response.body?.getReader();
159
+ if (!reader) {
160
+ throw new t.ProviderError('No response body', config.provider);
161
+ }
162
+ const decoder = new TextDecoder();
163
+ let buffer = '';
164
+ while (true) {
165
+ const { done, value } = await reader.read();
166
+ if (done)
167
+ break;
168
+ buffer += decoder.decode(value, { stream: true });
169
+ const lines = buffer.split('\n');
170
+ buffer = lines.pop() || '';
171
+ for (const line of lines) {
172
+ if (line.startsWith('data: ')) {
173
+ const data = line.slice(6);
174
+ if (data === '[DONE]')
175
+ continue;
176
+ try {
177
+ const chunk = JSON.parse(data);
178
+ yield {
179
+ content: chunk.choices[0]?.delta?.content ?? '',
180
+ finishReason: chunk.choices[0]?.finish_reason,
181
+ };
182
+ }
183
+ catch (parseError) {
184
+ // Log parse errors at debug level for troubleshooting
185
+ this.logDebug('Failed to parse stream chunk', { data, error: String(parseError) });
186
+ }
187
+ }
188
+ }
189
+ }
190
+ }
191
+ catch (err) {
192
+ if (err instanceof t.ProviderError)
193
+ throw err;
194
+ if (err instanceof Error && err.name === 'AbortError') {
195
+ throw new t.ProviderError('Request timeout', config.provider, err);
196
+ }
197
+ this.logger.error('[Provider] Stream request failed', {
198
+ error: String(err),
199
+ provider: config.provider,
200
+ model: config.model,
201
+ });
202
+ throw new t.ProviderError(`Failed to stream request: ${String(err)}`, config.provider, err);
203
+ }
204
+ finally {
205
+ clearTimeout(timeoutId);
206
+ }
207
+ }
208
+ /**
209
+ * Build messages array from call input
210
+ * Converts prompt/systemPrompt to messages if messages not provided
211
+ */
212
+ buildMessages(call) {
213
+ // If messages provided, use them directly
214
+ if (call.messages && call.messages.length > 0) {
215
+ return call.messages;
216
+ }
217
+ // Otherwise, build from prompt/systemPrompt
218
+ const messages = [];
219
+ if (call.systemPrompt) {
220
+ messages.push({ role: 'system', content: call.systemPrompt });
221
+ }
222
+ if (call.prompt) {
223
+ messages.push({ role: 'user', content: call.prompt });
224
+ }
225
+ return messages;
226
+ }
227
+ }
228
+ /**
229
+ * Factory for creating provider clients
230
+ * All providers use OpenAI-compatible API protocol
231
+ */
232
+ export function createProvider(logger) {
233
+ return new OpenAICompatibleProvider(logger);
234
+ }
235
+ //# sourceMappingURL=providers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providers.js","sourceRoot":"","sources":["../../src/client/providers.ts"],"names":[],"mappings":"AAAA,+GAA+G;AAC/G;;GAEG;AACH,OAAO,EAAU,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,CAAC,EAAE,MAAM,iBAAiB,CAAC;AAEpC,MAAM,wBAAwB,GAAG,KAAK,CAAC;AAEvC;;;GAGG;AACH,MAAM,OAAO,wBAAwB;IAC3B,MAAM,CAAS;IAEvB,YAAY,EAAW;QACrB,IAAI,CAAC,MAAM,GAAG,EAAE,IAAI,MAAM,CAAC;IAC7B,CAAC;IAEO,QAAQ,CAAC,KAAa,EAAE,IAAa;QAC3C,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,KAAK,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;IACrD,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,MAA6B,EAAE,IAAkB;QAC9D,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,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,CAAC;QAC9F,CAAC;gBAAS,CAAC;YACT,YAAY,CAAC,SAAS,CAAC,CAAC;QAC1B,CAAC;IACH,CAAC;IAED;;;OAGG;IACK,aAAa,CAAC,IAAkB;QACtC,0CAA0C;QAC1C,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC9C,OAAO,IAAI,CAAC,QAAQ,CAAC;QACvB,CAAC;QAED,4CAA4C;QAC5C,MAAM,QAAQ,GAAmB,EAAE,CAAC;QAEpC,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACtB,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,CAAC,YAAY,EAAE,CAAC,CAAC;QAChE,CAAC;QAED,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QACxD,CAAC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,MAAe;IAC5C,OAAO,IAAI,wBAAwB,CAAC,MAAM,CAAC,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Task-based LLM client implementation
3
+ * Implements the contextual task-based LLM access pattern with three levels:
4
+ * 1. Client Level - Base configuration and defaults
5
+ * 2. Task Level - Task-specific configuration with ResolvedModelConfig as internal state
6
+ * 3. Call Level - Per-call overrides (call options like timeout, etc)
7
+ */
8
+ import { Logger } from '@qianxude/shared';
9
+ import { t } from '../types/mod.js';
10
+ /**
11
+ * Optional dependencies for testing
12
+ * @internal
13
+ */
14
+ export interface TaskDependencies {
15
+ createProvider: (logger?: Logger) => t.ProviderClient;
16
+ logger?: Logger;
17
+ }
18
+ /**
19
+ * Implementation of LLMTaskCall
20
+ * Created by task with call-specific options that override task-level options
21
+ */
22
+ export declare class LLMTaskCall implements t.LLMTaskCall {
23
+ readonly task: t.LLMTask;
24
+ readonly callOptions: Required<t.LLMCallOptions>;
25
+ private providerFactory;
26
+ private logger;
27
+ constructor(task: t.LLMTask, overrideOptions?: Partial<t.LLMCallOptions>, dependencies?: TaskDependencies);
28
+ /**
29
+ * Execute a completion request
30
+ * @param input - Either a prompt string or an array of LLMMessage objects
31
+ */
32
+ complete(input: string | t.LLMMessage[]): Promise<t.LLMResponse>;
33
+ /**
34
+ * Execute a streaming request
35
+ * @param input - Either a prompt string or an array of LLMMessage objects
36
+ */
37
+ stream(input: string | t.LLMMessage[]): AsyncGenerator<t.LLMStreamChunk>;
38
+ /**
39
+ * Convert input to messages array
40
+ */
41
+ private convertInputToMessages;
42
+ }
43
+ /**
44
+ * Implementation of LLMTask
45
+ * Created by client with ResolvedModelConfig as internal state
46
+ */
47
+ export declare class LLMTask implements t.LLMTask {
48
+ readonly config: t.ResolvedModelConfig;
49
+ readonly callOptions: Required<t.LLMCallOptions>;
50
+ private dependencies?;
51
+ private logger;
52
+ constructor(config: t.ResolvedModelConfig, taskOptions?: t.LLMTaskOptions, dependencies?: TaskDependencies, lg?: Logger);
53
+ /**
54
+ * Create a call with optional call-level overrides
55
+ * @param overrideOptions - Optional call-level overrides for debug and timeout
56
+ */
57
+ createCall(overrideOptions?: Partial<t.LLMCallOptions>): t.LLMTaskCall;
58
+ }
59
+ //# sourceMappingURL=task.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task.d.ts","sourceRoot":"","sources":["../../src/client/task.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,MAAM,EAAU,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,CAAC,EAAE,MAAM,iBAAiB,CAAC;AAGpC;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,cAAc,EAAE,CAAC,MAAM,CAAC,EAAE,MAAM,KAAK,CA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,CAAC,CAAC,WAAW;CAUvE"}
@@ -0,0 +1,179 @@
1
+ /**
2
+ * Task-based LLM client implementation
3
+ * Implements the contextual task-based LLM access pattern with three levels:
4
+ * 1. Client Level - Base configuration and defaults
5
+ * 2. Task Level - Task-specific configuration with ResolvedModelConfig as internal state
6
+ * 3. Call Level - Per-call overrides (call options like timeout, etc)
7
+ */
8
+ import { logger } from '@qianxude/shared';
9
+ import { createProvider } from './providers.js';
10
+ /**
11
+ * Default call options
12
+ */
13
+ const DEFAULT_CALL_OPTIONS = {
14
+ timeout: 60000, // 60 seconds default timeout
15
+ };
16
+ /**
17
+ * Merge call options with defaults
18
+ */
19
+ function mergeCallOptions(base, override) {
20
+ return {
21
+ timeout: override?.timeout ?? base.timeout ?? DEFAULT_CALL_OPTIONS.timeout,
22
+ };
23
+ }
24
+ /**
25
+ * Implementation of LLMTaskCall
26
+ * Created by task with call-specific options that override task-level options
27
+ */
28
+ export class LLMTaskCall {
29
+ task;
30
+ callOptions;
31
+ providerFactory;
32
+ logger;
33
+ constructor(task, overrideOptions, dependencies) {
34
+ this.task = task;
35
+ this.callOptions = mergeCallOptions(task.callOptions, overrideOptions);
36
+ this.providerFactory = dependencies?.createProvider ?? createProvider;
37
+ this.logger = dependencies?.logger ?? logger;
38
+ }
39
+ /**
40
+ * Execute a completion request
41
+ * @param input - Either a prompt string or an array of LLMMessage objects
42
+ */
43
+ async complete(input) {
44
+ const config = this.task.config;
45
+ const provider = this.providerFactory(this.logger);
46
+ // Convert input to messages array
47
+ const messages = this.convertInputToMessages(input);
48
+ // Build LLMRequest with both prompt and messages fields
49
+ const call = {
50
+ messages,
51
+ temperature: config.temperature,
52
+ maxTokens: config.maxTokens,
53
+ topK: config.topK,
54
+ topP: config.topP,
55
+ timeout: this.callOptions.timeout,
56
+ };
57
+ // If input was a string, also populate prompt field
58
+ if (typeof input === 'string') {
59
+ call.prompt = input;
60
+ }
61
+ this.logger.debug('[TaskCall] Starting completion', {
62
+ provider: config.provider,
63
+ model: config.model,
64
+ messageCount: messages.length,
65
+ timeout: this.callOptions.timeout,
66
+ });
67
+ const startTime = Date.now();
68
+ try {
69
+ const response = await provider.complete(config, call);
70
+ const duration = Date.now() - startTime;
71
+ this.logger.debug('[TaskCall] Completion completed', {
72
+ duration,
73
+ model: response.model,
74
+ usage: response.usage,
75
+ });
76
+ return response;
77
+ }
78
+ catch (error) {
79
+ this.logger.error('[TaskCall] Completion failed', {
80
+ error: error instanceof Error ? error.message : String(error),
81
+ duration: Date.now() - startTime,
82
+ provider: config.provider,
83
+ model: config.model,
84
+ });
85
+ throw error;
86
+ }
87
+ }
88
+ /**
89
+ * Execute a streaming request
90
+ * @param input - Either a prompt string or an array of LLMMessage objects
91
+ */
92
+ async *stream(input) {
93
+ const config = this.task.config;
94
+ const provider = this.providerFactory(this.logger);
95
+ // Convert input to messages array
96
+ const messages = this.convertInputToMessages(input);
97
+ // Build LLMRequest with both prompt and messages fields
98
+ const call = {
99
+ messages,
100
+ temperature: config.temperature,
101
+ maxTokens: config.maxTokens,
102
+ topK: config.topK,
103
+ topP: config.topP,
104
+ timeout: this.callOptions.timeout,
105
+ };
106
+ // If input was a string, also populate prompt field
107
+ if (typeof input === 'string') {
108
+ call.prompt = input;
109
+ }
110
+ this.logger.debug('[TaskCall] Starting stream', {
111
+ provider: config.provider,
112
+ model: config.model,
113
+ messageCount: messages.length,
114
+ timeout: this.callOptions.timeout,
115
+ });
116
+ const startTime = Date.now();
117
+ try {
118
+ yield* provider.stream(config, call);
119
+ this.logger.debug('[TaskCall] Stream completed', {
120
+ duration: Date.now() - startTime,
121
+ provider: config.provider,
122
+ model: config.model,
123
+ });
124
+ }
125
+ catch (error) {
126
+ this.logger.error('[TaskCall] Stream failed', {
127
+ error: error instanceof Error ? error.message : String(error),
128
+ duration: Date.now() - startTime,
129
+ provider: config.provider,
130
+ model: config.model,
131
+ });
132
+ throw error;
133
+ }
134
+ }
135
+ /**
136
+ * Convert input to messages array
137
+ */
138
+ convertInputToMessages(input) {
139
+ if (typeof input === 'string') {
140
+ return [{ role: 'user', content: input }];
141
+ }
142
+ return input;
143
+ }
144
+ }
145
+ /**
146
+ * Implementation of LLMTask
147
+ * Created by client with ResolvedModelConfig as internal state
148
+ */
149
+ export class LLMTask {
150
+ config;
151
+ callOptions;
152
+ dependencies;
153
+ logger;
154
+ constructor(config, taskOptions, dependencies, lg) {
155
+ this.config = config;
156
+ this.callOptions = mergeCallOptions(taskOptions?.callOptions ?? {}, undefined);
157
+ this.dependencies = dependencies;
158
+ this.logger = lg ?? logger;
159
+ this.logger.debug('[Task] Task created', {
160
+ provider: config.provider,
161
+ model: config.model,
162
+ callOptions: this.callOptions,
163
+ });
164
+ }
165
+ /**
166
+ * Create a call with optional call-level overrides
167
+ * @param overrideOptions - Optional call-level overrides for debug and timeout
168
+ */
169
+ createCall(overrideOptions) {
170
+ this.logger.debug('[Task] Creating call', {
171
+ overrideOptions,
172
+ });
173
+ return new LLMTaskCall(this, overrideOptions, {
174
+ createProvider: this.dependencies?.createProvider ?? createProvider,
175
+ logger: this.logger,
176
+ });
177
+ }
178
+ }
179
+ //# sourceMappingURL=task.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task.js","sourceRoot":"","sources":["../../src/client/task.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAU,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAGlD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAWhD;;GAEG;AACH,MAAM,oBAAoB,GAA+B;IACvD,OAAO,EAAE,KAAK,EAAE,6BAA6B;CAC9C,CAAC;AAEF;;GAEG;AACH,SAAS,gBAAgB,CAAC,IAAsB,EAAE,QAAoC;IACpF,OAAO;QACL,OAAO,EAAE,QAAQ,EAAE,OAAO,IAAI,IAAI,CAAC,OAAO,IAAI,oBAAoB,CAAC,OAAO;KAC3E,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,WAAW;IACb,IAAI,CA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@
1
+ export * from './types/mod.js';
2
+ export { LLMClient, createLLMClient, ConfigLoader, OpenAICompatibleProvider, createProvider, LLMTask, LLMTaskCall, } from './client/index.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAE/B,OAAO,EACL,SAAS,EACT,eAAe,EACf,YAAY,EACZ,wBAAwB,EACxB,cAAc,EAEd,OAAO,EACP,WAAW,GACZ,MAAM,mBAAmB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from './types/mod.js';
2
+ export { LLMClient, createLLMClient, ConfigLoader, OpenAICompatibleProvider, createProvider,
3
+ // Task-based implementations
4
+ LLMTask, LLMTaskCall, } from './client/index.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAE/B,OAAO,EACL,SAAS,EACT,eAAe,EACf,YAAY,EACZ,wBAAwB,EACxB,cAAc;AACd,6BAA6B;AAC7B,OAAO,EACP,WAAW,GACZ,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Base types for raw OpenAI-compatible API responses
3
+ * These types represent the snake_case structure returned by OpenAI-compatible APIs
4
+ */
5
+ /** Message in OpenAI-compatible API response */
6
+ export interface OpenAIMessage {
7
+ content: string;
8
+ }
9
+ /** Choice containing message in OpenAI-compatible API response */
10
+ export interface OpenAIChoice {
11
+ message: OpenAIMessage;
12
+ }
13
+ /** Token usage in OpenAI-compatible API response (snake_case) */
14
+ export interface OpenAIUsage {
15
+ prompt_tokens: number;
16
+ completion_tokens: number;
17
+ total_tokens: number;
18
+ }
19
+ /** Full OpenAI-compatible API response structure */
20
+ export interface OpenAIResponse {
21
+ choices: OpenAIChoice[];
22
+ usage?: OpenAIUsage;
23
+ model: string;
24
+ }
25
+ /** Delta content in streaming response */
26
+ export interface OpenAIDelta {
27
+ content?: string;
28
+ }
29
+ /** Choice in streaming response */
30
+ export interface OpenAIStreamChoice {
31
+ delta?: OpenAIDelta;
32
+ finish_reason?: 'stop' | 'length' | 'content_filter';
33
+ }
34
+ /** Streaming chunk structure */
35
+ export interface OpenAIStreamChunk {
36
+ choices: OpenAIStreamChoice[];
37
+ }
38
+ //# sourceMappingURL=base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/types/base.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,gDAAgD;AAChD,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,kEAAkE;AAClE,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,aAAa,CAAC;CACxB;AAED,iEAAiE;AACjE,MAAM,WAAW,WAAW;IAC1B,aAAa,EAAE,MAAM,CAAC;IACtB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,oDAAoD;AACpD,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,YAAY,EAAE,CAAC;IACxB,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,0CAA0C;AAC1C,MAAM,WAAW,WAAW;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,mCAAmC;AACnC,MAAM,WAAW,kBAAkB;IACjC,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,gBAAgB,CAAC;CACtD;AAED,gCAAgC;AAChC,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,kBAAkB,EAAE,CAAC;CAC/B"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Base types for raw OpenAI-compatible API responses
3
+ * These types represent the snake_case structure returned by OpenAI-compatible APIs
4
+ */
5
+ export {};
6
+ //# sourceMappingURL=base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/types/base.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Client types for LLM package
3
+ */
4
+ import type { Logger } from '@qianxude/shared';
5
+ import type { LLMConfig } from './config.js';
6
+ import type { LLMMessage } from './message.js';
7
+ import type { CompletionOptions, LLMTaskOptions } from './options.js';
8
+ import type { LLMResponse, LLMStreamChunk } from './response.js';
9
+ import type { LLMTask } from './task.js';
10
+ /**
11
+ * New LLM client interface with provider-model-task architecture
12
+ * This is the CEKB-style interface with initialize() and task-based selection
13
+ */
14
+ export interface TaskBasedLLMClient {
15
+ /** Initialize the client by loading configuration */
16
+ initialize(): Promise<void>;
17
+ /**
18
+ * Complete a request with either a prompt string or messages array
19
+ * @param input - Either a prompt string or an array of LLMMessage objects
20
+ * @param options - Optional completion options
21
+ */
22
+ complete(input: string | LLMMessage[], options?: CompletionOptions): Promise<LLMResponse>;
23
+ /**
24
+ * Stream a request with either a prompt string or messages array
25
+ * @param input - Either a prompt string or an array of LLMMessage objects
26
+ * @param options - Optional completion options
27
+ */
28
+ stream(input: string | LLMMessage[], options?: CompletionOptions): AsyncGenerator<LLMStreamChunk>;
29
+ /** Get the model ID for a specific task */
30
+ getModelForTask(task: string): string;
31
+ /** Get the raw configuration */
32
+ getConfig(): LLMConfig;
33
+ /**
34
+ * Create a task with resolved configuration
35
+ * @param taskName - The task name to resolve configuration for
36
+ * @param options - Optional task-level options
37
+ */
38
+ createTask(taskName: string, options?: LLMTaskOptions): LLMTask;
39
+ }
40
+ /** Options for creating the new CEKB-style LLM client */
41
+ export interface LLMClientOptions {
42
+ /** Path to llm.manifest.json configuration file (default: <package-root>/llm.manifest.json) */
43
+ manifestPath?: string;
44
+ /** Logger for debugging */
45
+ logger?: Logger;
46
+ }
47
+ //# sourceMappingUR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+1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/types/client.ts"],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Common types and error classes for LLM package
3
+ */
4
+ /** Base LLM error */
5
+ export declare class LLMError extends Error {
6
+ readonly code: string;
7
+ readonly cause?: unknown | undefined;
8
+ constructor(message: string, code: string, cause?: unknown | undefined);
9
+ }
10
+ /** Configuration-related errors */
11
+ export declare class ConfigurationError extends LLMError {
12
+ constructor(message: string, cause?: unknown);
13
+ }
14
+ /** Provider API errors */
15
+ export declare class ProviderError extends LLMError {
16
+ readonly provider: string;
17
+ constructor(message: string, provider: string, cause?: unknown);
18
+ }
19
+ //# sourceMappingURL=common.d.ts.map