universal-llm-client 3.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 (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +414 -0
  3. package/dist/ai-model.d.ts +53 -0
  4. package/dist/ai-model.d.ts.map +1 -0
  5. package/dist/ai-model.js +159 -0
  6. package/dist/ai-model.js.map +1 -0
  7. package/dist/auditor.d.ts +78 -0
  8. package/dist/auditor.d.ts.map +1 -0
  9. package/dist/auditor.js +104 -0
  10. package/dist/auditor.js.map +1 -0
  11. package/dist/client.d.ts +75 -0
  12. package/dist/client.d.ts.map +1 -0
  13. package/dist/client.js +240 -0
  14. package/dist/client.js.map +1 -0
  15. package/dist/http.d.ts +47 -0
  16. package/dist/http.d.ts.map +1 -0
  17. package/dist/http.js +186 -0
  18. package/dist/http.js.map +1 -0
  19. package/dist/index.d.ts +16 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +41 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/interfaces.d.ts +324 -0
  24. package/dist/interfaces.d.ts.map +1 -0
  25. package/dist/interfaces.js +63 -0
  26. package/dist/interfaces.js.map +1 -0
  27. package/dist/mcp.d.ts +85 -0
  28. package/dist/mcp.d.ts.map +1 -0
  29. package/dist/mcp.js +255 -0
  30. package/dist/mcp.js.map +1 -0
  31. package/dist/providers/google.d.ts +33 -0
  32. package/dist/providers/google.d.ts.map +1 -0
  33. package/dist/providers/google.js +426 -0
  34. package/dist/providers/google.js.map +1 -0
  35. package/dist/providers/index.d.ts +7 -0
  36. package/dist/providers/index.d.ts.map +1 -0
  37. package/dist/providers/index.js +7 -0
  38. package/dist/providers/index.js.map +1 -0
  39. package/dist/providers/ollama.d.ts +26 -0
  40. package/dist/providers/ollama.d.ts.map +1 -0
  41. package/dist/providers/ollama.js +304 -0
  42. package/dist/providers/ollama.js.map +1 -0
  43. package/dist/providers/openai.d.ts +20 -0
  44. package/dist/providers/openai.d.ts.map +1 -0
  45. package/dist/providers/openai.js +251 -0
  46. package/dist/providers/openai.js.map +1 -0
  47. package/dist/router.d.ts +87 -0
  48. package/dist/router.d.ts.map +1 -0
  49. package/dist/router.js +260 -0
  50. package/dist/router.js.map +1 -0
  51. package/dist/stream-decoder.d.ts +112 -0
  52. package/dist/stream-decoder.d.ts.map +1 -0
  53. package/dist/stream-decoder.js +238 -0
  54. package/dist/stream-decoder.js.map +1 -0
  55. package/dist/tools.d.ts +78 -0
  56. package/dist/tools.d.ts.map +1 -0
  57. package/dist/tools.js +207 -0
  58. package/dist/tools.js.map +1 -0
  59. package/package.json +91 -0
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Universal LLM Client v3 — Auditor (Observability)
3
+ *
4
+ * Every LLM interaction (request, response, tool call, retry, failover)
5
+ * is recorded through the Auditor interface. Frameworks inject their own
6
+ * Auditor for dashboards, cost tracking, or behavioral scoring.
7
+ */
8
+ import type { TokenUsageInfo, ToolExecutionResult } from './interfaces.js';
9
+ export type AuditEventType = 'request' | 'response' | 'stream_start' | 'stream_end' | 'tool_call' | 'tool_result' | 'error' | 'retry' | 'failover';
10
+ export interface AuditEvent {
11
+ /** Unix timestamp in ms */
12
+ timestamp: number;
13
+ /** Event type */
14
+ type: AuditEventType;
15
+ /** Provider that generated this event */
16
+ provider?: string;
17
+ /** Model name */
18
+ model?: string;
19
+ /** Duration in ms (for request/response pairs) */
20
+ duration?: number;
21
+ /** Token usage (for response events) */
22
+ usage?: TokenUsageInfo;
23
+ /** Tool execution details (for tool_call/tool_result events) */
24
+ toolExecution?: ToolExecutionResult;
25
+ /** Error message (for error/retry events) */
26
+ error?: string;
27
+ /** Arbitrary metadata for framework-specific data */
28
+ metadata?: Record<string, unknown>;
29
+ }
30
+ /**
31
+ * Interface for LLM observability.
32
+ *
33
+ * Implement this to capture all LLM lifecycle events.
34
+ * The library calls `record()` at every interaction point.
35
+ */
36
+ export interface Auditor {
37
+ /** Record an audit event */
38
+ record(event: AuditEvent): void;
39
+ /** Flush any buffered events (optional) */
40
+ flush?(): Promise<void>;
41
+ }
42
+ /**
43
+ * Zero-overhead auditor that discards all events.
44
+ * Used as the default when no auditor is configured.
45
+ */
46
+ export declare class NoopAuditor implements Auditor {
47
+ record(_event: AuditEvent): void;
48
+ }
49
+ /**
50
+ * Structured console logging auditor.
51
+ * Useful for development and debugging.
52
+ */
53
+ export declare class ConsoleAuditor implements Auditor {
54
+ private prefix;
55
+ constructor(prefix?: string);
56
+ record(event: AuditEvent): void;
57
+ }
58
+ /**
59
+ * Buffered auditor that collects events for batch processing.
60
+ * Useful for custom sinks (OpenTelemetry, DataDog, databases, etc.)
61
+ */
62
+ export declare class BufferedAuditor implements Auditor {
63
+ private events;
64
+ private maxBufferSize;
65
+ private onFlush?;
66
+ constructor(options?: {
67
+ maxBufferSize?: number;
68
+ onFlush?: (events: AuditEvent[]) => Promise<void>;
69
+ });
70
+ record(event: AuditEvent): void;
71
+ /** Get all buffered events */
72
+ getEvents(): ReadonlyArray<AuditEvent>;
73
+ /** Flush buffered events to the configured sink */
74
+ flush(): Promise<void>;
75
+ /** Clear all buffered events without flushing */
76
+ clear(): void;
77
+ }
78
+ //# sourceMappingURL=auditor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auditor.d.ts","sourceRoot":"","sources":["../src/auditor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAM3E,MAAM,MAAM,cAAc,GACpB,SAAS,GACT,UAAU,GACV,cAAc,GACd,YAAY,GACZ,WAAW,GACX,aAAa,GACb,OAAO,GACP,OAAO,GACP,UAAU,CAAC;AAEjB,MAAM,WAAW,UAAU;IACvB,2BAA2B;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,iBAAiB;IACjB,IAAI,EAAE,cAAc,CAAC;IACrB,yCAAyC;IACzC,QAAQ,CAAC,EAAE,MAAM,CAAC;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O,CAAC,IAAI,CAAC;IAQ5B,iDAAiD;IACjD,KAAK,IAAI,IAAI;CAGhB"}
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Universal LLM Client v3 — Auditor (Observability)
3
+ *
4
+ * Every LLM interaction (request, response, tool call, retry, failover)
5
+ * is recorded through the Auditor interface. Frameworks inject their own
6
+ * Auditor for dashboards, cost tracking, or behavioral scoring.
7
+ */
8
+ // ============================================================================
9
+ // Built-in Auditors
10
+ // ============================================================================
11
+ /**
12
+ * Zero-overhead auditor that discards all events.
13
+ * Used as the default when no auditor is configured.
14
+ */
15
+ export class NoopAuditor {
16
+ record(_event) {
17
+ // Intentionally empty
18
+ }
19
+ }
20
+ /**
21
+ * Structured console logging auditor.
22
+ * Useful for development and debugging.
23
+ */
24
+ export class ConsoleAuditor {
25
+ prefix;
26
+ constructor(prefix = '[LLM]') {
27
+ this.prefix = prefix;
28
+ }
29
+ record(event) {
30
+ const parts = [
31
+ this.prefix,
32
+ event.type.toUpperCase(),
33
+ event.provider ? `[${event.provider}]` : '',
34
+ event.model ? `(${event.model})` : '',
35
+ ].filter(Boolean);
36
+ switch (event.type) {
37
+ case 'request':
38
+ console.log(parts.join(' '), '→');
39
+ break;
40
+ case 'response':
41
+ console.log(parts.join(' '), event.duration ? `${event.duration}ms` : '', event.usage ? `${event.usage.totalTokens} tokens` : '');
42
+ break;
43
+ case 'stream_start':
44
+ console.log(parts.join(' '), 'streaming...');
45
+ break;
46
+ case 'stream_end':
47
+ console.log(parts.join(' '), 'done', event.duration ? `${event.duration}ms` : '');
48
+ break;
49
+ case 'tool_call':
50
+ console.log(parts.join(' '), event.toolExecution?.tool_call_id ?? '');
51
+ break;
52
+ case 'tool_result':
53
+ console.log(parts.join(' '), event.toolExecution?.error ? '❌' : '✅', event.toolExecution?.duration ? `${event.toolExecution.duration}ms` : '');
54
+ break;
55
+ case 'error':
56
+ console.error(parts.join(' '), event.error ?? 'Unknown error');
57
+ break;
58
+ case 'retry':
59
+ console.warn(parts.join(' '), event.error ?? '', event.metadata ?? '');
60
+ break;
61
+ case 'failover':
62
+ console.warn(parts.join(' '), '→', event.metadata?.['nextProvider'] ?? '');
63
+ break;
64
+ }
65
+ }
66
+ }
67
+ /**
68
+ * Buffered auditor that collects events for batch processing.
69
+ * Useful for custom sinks (OpenTelemetry, DataDog, databases, etc.)
70
+ */
71
+ export class BufferedAuditor {
72
+ events = [];
73
+ maxBufferSize;
74
+ onFlush;
75
+ constructor(options = {}) {
76
+ this.maxBufferSize = options.maxBufferSize ?? 1000;
77
+ this.onFlush = options.onFlush;
78
+ }
79
+ record(event) {
80
+ this.events.push(event);
81
+ if (this.events.length >= this.maxBufferSize) {
82
+ // Auto-flush when buffer is full (fire and forget)
83
+ this.flush().catch(() => { });
84
+ }
85
+ }
86
+ /** Get all buffered events */
87
+ getEvents() {
88
+ return this.events;
89
+ }
90
+ /** Flush buffered events to the configured sink */
91
+ async flush() {
92
+ if (this.events.length === 0)
93
+ return;
94
+ const batch = this.events.splice(0);
95
+ if (this.onFlush) {
96
+ await this.onFlush(batch);
97
+ }
98
+ }
99
+ /** Clear all buffered events without flushing */
100
+ clear() {
101
+ this.events.length = 0;
102
+ }
103
+ }
104
+ //# sourceMappingURL=auditor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auditor.js","sourceRoot":"","sources":["../src/auditor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAyDH,+EAA+E;AAC/E,oBAAoB;AACpB,+EAA+E;AAE/E;;;GAGG;AACH,MAAM,OAAO,WAAW;IACpB,MAAM,CAAC,MAAkB;QACrB,sBAAsB;IAC1B,CAAC;CACJ;AAED;;;GAGG;AACH,MAAM,OAAO,cAAc;IACf,MAAM,CAAS;IAEvB,YAAY,SAAiB,OAAO;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;IAED,MAAM,CAAC,KAAiB;QACpB,MAAM,KAAK,GAAG;YACV,IAAI,CAAC,MAAM;YACX,KAAK,CAAC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AAC,MAAM,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YAC3C,mDAAmD;YACnD,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QACjC,CAAC;IACL,CAAC;IAED,8BAA8B;IAC9B,SAAS;QACL,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAED,mDAAmD;IACnD,KAAK,CAAC,KAAK;QACP,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO;QACrC,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACpC,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAC9B,CAAC;IACL,CAAC;IAED,iDAAiD;IACjD,KAAK;QACD,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;IAC3B,CAAC;CACJ"}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Universal LLM Client v3 — Base LLM Client
3
+ *
4
+ * Abstract base class for all LLM providers.
5
+ * Handles tool registration, execution, and the autonomous
6
+ * multi-turn tool execution loop.
7
+ */
8
+ import type { LLMClientOptions, LLMChatMessage, LLMChatResponse, LLMToolDefinition, LLMToolCall, LLMFunction, ToolRegistry, ToolHandler, ToolExecutionResult, ChatOptions, ModelMetadata } from './interfaces.js';
9
+ import type { DecodedEvent } from './stream-decoder.js';
10
+ import type { Auditor } from './auditor.js';
11
+ export declare abstract class BaseLLMClient {
12
+ protected options: LLMClientOptions;
13
+ protected toolRegistry: ToolRegistry;
14
+ protected auditor: Auditor;
15
+ protected debug: boolean;
16
+ constructor(options: LLMClientOptions, auditor?: Auditor);
17
+ /** Send a chat request and get a response */
18
+ abstract chat(messages: LLMChatMessage[], options?: ChatOptions): Promise<LLMChatResponse>;
19
+ /** Stream a chat response as decoded events */
20
+ abstract chatStream(messages: LLMChatMessage[], options?: ChatOptions): AsyncGenerator<DecodedEvent, LLMChatResponse | void, unknown>;
21
+ /** Get available models */
22
+ abstract getModels(): Promise<string[]>;
23
+ /** Generate embeddings for text */
24
+ abstract embed(text: string): Promise<number[]>;
25
+ /** Generate embeddings for multiple texts */
26
+ embedArray(texts: string[]): Promise<number[][]>;
27
+ /**
28
+ * Get metadata about a model (context length, architecture, etc.)
29
+ * Override per-provider for accurate data.
30
+ */
31
+ getModelInfo(_modelName?: string): Promise<ModelMetadata>;
32
+ /**
33
+ * Sanitize tool name for LLM compatibility.
34
+ * LLM APIs require function names matching [a-zA-Z0-9_-].
35
+ * Module-prefixed names like "@core/computer:list_windows" are cleaned.
36
+ */
37
+ private sanitizeToolName;
38
+ /** Register a tool/function callable by the model */
39
+ registerTool(name: string, description: string, parameters: LLMFunction['parameters'], handler: ToolHandler): void;
40
+ /** Register multiple tools at once */
41
+ registerTools(tools: Array<{
42
+ name: string;
43
+ description: string;
44
+ parameters: LLMFunction['parameters'];
45
+ handler: ToolHandler;
46
+ }>): void;
47
+ /** Get all registered tool definitions (deduplicated by sanitized name) */
48
+ getToolDefinitions(): LLMToolDefinition[];
49
+ /** Execute a single tool call with fuzzy name matching */
50
+ executeTool(toolCall: LLMToolCall): Promise<ToolExecutionResult>;
51
+ /** Execute multiple tool calls in parallel */
52
+ executeTools(toolCalls: LLMToolCall[]): Promise<ToolExecutionResult[]>;
53
+ /**
54
+ * Chat with automatic tool execution.
55
+ * Continues until the model stops calling tools or max iterations reached.
56
+ * Returns the complete execution trace in `toolExecutions`.
57
+ */
58
+ chatWithTools(messages: LLMChatMessage[], options?: ChatOptions & {
59
+ maxIterations?: number;
60
+ }): Promise<LLMChatResponse>;
61
+ /** Set the model name at runtime */
62
+ setModel(modelName: string): void;
63
+ /** Get the current model name */
64
+ get model(): string;
65
+ /** Get the base URL */
66
+ get url(): string;
67
+ /** Set the auditor instance */
68
+ setAuditor(auditor: Auditor): void;
69
+ protected debugLog(message: string, data?: unknown): void;
70
+ /**
71
+ * Generate a unique ID for tool calls when the provider doesn't provide one.
72
+ */
73
+ protected generateToolCallId(): string;
74
+ }
75
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACR,gBAAgB,EAChB,cAAc,EACd,eAAe,EACf,iBAAiB,EACjB,WAAW,EACX,WAAW,EACX,YAAY,EACZ,WAAW,EACX,mBAAmB,EACnB,WAAW,EACX,aAAa,EAChB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAO5C,8BAAsB,aAAa;IAC/B,SAAS,CAAC,OAAO,EAAE,gBAAgB,CAAC;IACpC,SAAS,CAAC,YAAY,EAAE,YAAY,CAAM;IAC1C,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC;IAC3B,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC;gBAEb,OAAO,EAAE,gBAAgB,EAAE,OAAO,CAAC,EAAE,OAAO;IAUxD,6CAA6C;IAC7C,QAAQ,CAAC,IAAI,CACT,QAAQ,EAAE,cAAc,EAAE,EAC1B,OAAO,CAAC,EAAE,WAAW,GACtB,OAAO,CAAC,eAAe,CAAC;IAE3B,+CAA+C;IAC/C,QAAQ,CAAC,UAAU,CACf,QAAQ,EAAE,cAAc,EAAE,EAC1B,OAAO,CAAC,EAAE,WAAW,GACtB,cAAc,CAAC,YAAY,EAAE,eAAe,GAAG,IAAI,EAAE,OAAO,CAAC;IAEhE,2BAA2B;IAC3B,QAAQ,CAAC,SAAS,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAEvC,mCAAmC;IACnC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,C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OAAO,GAAG,IAAI;IAIlC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI;IAMzD;;OAEG;IACH,SAAS,CAAC,kBAAkB,IAAI,MAAM;CAGzC"}
package/dist/client.js ADDED
@@ -0,0 +1,240 @@
1
+ /**
2
+ * Universal LLM Client v3 — Base LLM Client
3
+ *
4
+ * Abstract base class for all LLM providers.
5
+ * Handles tool registration, execution, and the autonomous
6
+ * multi-turn tool execution loop.
7
+ */
8
+ import { NoopAuditor } from './auditor.js';
9
+ // ============================================================================
10
+ // Abstract Base Client
11
+ // ============================================================================
12
+ export class BaseLLMClient {
13
+ options;
14
+ toolRegistry = {};
15
+ auditor;
16
+ debug;
17
+ constructor(options, auditor) {
18
+ this.options = options;
19
+ this.auditor = auditor ?? new NoopAuditor();
20
+ this.debug = options.debug ?? false;
21
+ }
22
+ /** Generate embeddings for multiple texts */
23
+ async embedArray(texts) {
24
+ return Promise.all(texts.map(t => this.embed(t)));
25
+ }
26
+ /**
27
+ * Get metadata about a model (context length, architecture, etc.)
28
+ * Override per-provider for accurate data.
29
+ */
30
+ async getModelInfo(_modelName) {
31
+ return { contextLength: 8192 }; // Conservative default
32
+ }
33
+ // ========================================================================
34
+ // Tool Registration
35
+ // ========================================================================
36
+ /**
37
+ * Sanitize tool name for LLM compatibility.
38
+ * LLM APIs require function names matching [a-zA-Z0-9_-].
39
+ * Module-prefixed names like "@core/computer:list_windows" are cleaned.
40
+ */
41
+ sanitizeToolName(name) {
42
+ return name
43
+ .replace(/^@[^:]+:/, '') // Strip module prefix
44
+ .replace(/[^a-zA-Z0-9_-]/g, '_') // Replace illegal chars
45
+ .replace(/_+/g, '_') // Collapse
46
+ .replace(/^_|_$/g, ''); // Trim
47
+ }
48
+ /** Register a tool/function callable by the model */
49
+ registerTool(name, description, parameters, handler) {
50
+ const safeName = this.sanitizeToolName(name);
51
+ this.toolRegistry[name] = {
52
+ definition: { name: safeName, description, parameters },
53
+ handler,
54
+ };
55
+ // Index by sanitized name for reverse lookup
56
+ if (safeName !== name && !this.toolRegistry[safeName]) {
57
+ this.toolRegistry[safeName] = this.toolRegistry[name];
58
+ }
59
+ this.debugLog(`Registered tool: ${name} (LLM name: ${safeName})`);
60
+ }
61
+ /** Register multiple tools at once */
62
+ registerTools(tools) {
63
+ for (const tool of tools) {
64
+ this.registerTool(tool.name, tool.description, tool.parameters, tool.handler);
65
+ }
66
+ }
67
+ /** Get all registered tool definitions (deduplicated by sanitized name) */
68
+ getToolDefinitions() {
69
+ const seen = new Set();
70
+ const defs = [];
71
+ for (const { definition } of Object.values(this.toolRegistry)) {
72
+ if (seen.has(definition.name))
73
+ continue;
74
+ seen.add(definition.name);
75
+ defs.push({ type: 'function', function: definition });
76
+ }
77
+ return defs;
78
+ }
79
+ // ========================================================================
80
+ // Tool Execution
81
+ // ========================================================================
82
+ /** Execute a single tool call with fuzzy name matching */
83
+ async executeTool(toolCall) {
84
+ const toolName = toolCall.function.name;
85
+ const start = Date.now();
86
+ let tool = this.toolRegistry[toolName];
87
+ // Fuzzy lookup: try suffix match (LLM stripped module prefix)
88
+ if (!tool) {
89
+ const entries = Object.entries(this.toolRegistry);
90
+ const bySuffix = entries.find(([k]) => k.endsWith(`:${toolName}`));
91
+ if (bySuffix) {
92
+ tool = bySuffix[1];
93
+ this.debugLog(`Fuzzy tool match: "${toolName}" → "${bySuffix[0]}"`);
94
+ }
95
+ // Try prefix match: if only one tool in that module, use it
96
+ if (!tool) {
97
+ const byPrefix = entries.filter(([k]) => k.startsWith(`${toolName}:`));
98
+ if (byPrefix.length === 1) {
99
+ tool = byPrefix[0][1];
100
+ this.debugLog(`Fuzzy tool match (single): "${toolName}" → "${byPrefix[0][0]}"`);
101
+ }
102
+ }
103
+ }
104
+ if (!tool) {
105
+ const result = {
106
+ tool_call_id: toolCall.id,
107
+ output: null,
108
+ error: `Unknown tool: ${toolName}`,
109
+ duration: Date.now() - start,
110
+ };
111
+ this.auditor.record({
112
+ timestamp: Date.now(),
113
+ type: 'tool_result',
114
+ toolExecution: result,
115
+ error: result.error,
116
+ });
117
+ return result;
118
+ }
119
+ this.auditor.record({
120
+ timestamp: Date.now(),
121
+ type: 'tool_call',
122
+ metadata: { toolName, arguments: toolCall.function.arguments },
123
+ });
124
+ try {
125
+ const args = JSON.parse(toolCall.function.arguments);
126
+ const output = await tool.handler(args);
127
+ const result = {
128
+ tool_call_id: toolCall.id,
129
+ output,
130
+ duration: Date.now() - start,
131
+ };
132
+ this.auditor.record({
133
+ timestamp: Date.now(),
134
+ type: 'tool_result',
135
+ toolExecution: result,
136
+ });
137
+ return result;
138
+ }
139
+ catch (error) {
140
+ const result = {
141
+ tool_call_id: toolCall.id,
142
+ output: null,
143
+ error: error instanceof Error ? error.message : String(error),
144
+ duration: Date.now() - start,
145
+ };
146
+ this.auditor.record({
147
+ timestamp: Date.now(),
148
+ type: 'tool_result',
149
+ toolExecution: result,
150
+ error: result.error,
151
+ });
152
+ return result;
153
+ }
154
+ }
155
+ /** Execute multiple tool calls in parallel */
156
+ async executeTools(toolCalls) {
157
+ return Promise.all(toolCalls.map(tc => this.executeTool(tc)));
158
+ }
159
+ // ========================================================================
160
+ // Chat with Tools (multi-turn autonomous loop)
161
+ // ========================================================================
162
+ /**
163
+ * Chat with automatic tool execution.
164
+ * Continues until the model stops calling tools or max iterations reached.
165
+ * Returns the complete execution trace in `toolExecutions`.
166
+ */
167
+ async chatWithTools(messages, options) {
168
+ const maxIterations = options?.maxIterations ?? 10;
169
+ const conversationMessages = [...messages];
170
+ const allToolExecutions = [];
171
+ let iterations = 0;
172
+ while (iterations < maxIterations) {
173
+ const response = await this.chat(conversationMessages, {
174
+ ...options,
175
+ tools: this.getToolDefinitions(),
176
+ });
177
+ // If no tool calls, return with full trace
178
+ if (!response.message.tool_calls?.length) {
179
+ return {
180
+ ...response,
181
+ toolExecutions: allToolExecutions.length > 0 ? allToolExecutions : undefined,
182
+ };
183
+ }
184
+ // Add assistant message with tool calls
185
+ conversationMessages.push(response.message);
186
+ // Execute tools in parallel
187
+ const toolResults = await this.executeTools(response.message.tool_calls);
188
+ allToolExecutions.push(...toolResults);
189
+ // Add tool results as messages
190
+ for (const result of toolResults) {
191
+ conversationMessages.push({
192
+ role: 'tool',
193
+ content: typeof result.output === 'string'
194
+ ? result.output
195
+ : JSON.stringify(result.output),
196
+ tool_call_id: result.tool_call_id,
197
+ });
198
+ }
199
+ iterations++;
200
+ }
201
+ // Max iterations — final call without tools
202
+ const finalResponse = await this.chat(conversationMessages);
203
+ return {
204
+ ...finalResponse,
205
+ toolExecutions: allToolExecutions,
206
+ };
207
+ }
208
+ // ========================================================================
209
+ // Helpers
210
+ // ========================================================================
211
+ /** Set the model name at runtime */
212
+ setModel(modelName) {
213
+ this.options.model = modelName;
214
+ this.debugLog(`Model switched to: ${modelName}`);
215
+ }
216
+ /** Get the current model name */
217
+ get model() {
218
+ return this.options.model;
219
+ }
220
+ /** Get the base URL */
221
+ get url() {
222
+ return this.options.url;
223
+ }
224
+ /** Set the auditor instance */
225
+ setAuditor(auditor) {
226
+ this.auditor = auditor;
227
+ }
228
+ debugLog(message, data) {
229
+ if (this.debug) {
230
+ console.log(`[LLM:${this.options.model}] ${message}`, data ?? '');
231
+ }
232
+ }
233
+ /**
234
+ * Generate a unique ID for tool calls when the provider doesn't provide one.
235
+ */
236
+ generateToolCallId() {
237
+ return `call_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
238
+ }
239
+ }
240
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAiBH,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,+EAA+E;AAC/E,uBAAuB;AACvB,+EAA+E;AAE/E,MAAM,OAAgB,aAAa;IACrB,OAAO,CAAmB;IAC1B,YAAY,GAAiB,EAAE,CAAC;IAChC,OA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UAAU,CAAC,OAAgB;QACvB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;IAES,QAAQ,CAAC,OAAe,EAAE,IAAc;QAC9C,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACb,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,CAAC,OAAO,CAAC,KAAK,KAAK,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC,CAAC;QACtE,CAAC;IACL,CAAC;IAED;;OAEG;IACO,kBAAkB;QACxB,OAAO,QAAQ,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;IAC1E,CAAC;CACJ"}
package/dist/http.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Universal LLM Client v3 — HTTP Utilities
3
+ *
4
+ * Zero-dependency HTTP layer using native fetch.
5
+ * Works on Node 22+, Bun, Deno, and browsers.
6
+ */
7
+ import type { LLMClientOptions } from './interfaces.js';
8
+ export interface HttpRequestOptions {
9
+ method?: 'GET' | 'POST';
10
+ headers?: Record<string, string>;
11
+ body?: unknown;
12
+ timeout?: number;
13
+ signal?: AbortSignal;
14
+ }
15
+ export interface HttpResponse<T = unknown> {
16
+ ok: boolean;
17
+ status: number;
18
+ data: T;
19
+ }
20
+ /**
21
+ * Make an HTTP request with timeout and error handling.
22
+ * Uses native fetch (available in all target runtimes).
23
+ */
24
+ export declare function httpRequest<T = unknown>(url: string, options?: HttpRequestOptions): Promise<HttpResponse<T>>;
25
+ /**
26
+ * Make a streaming HTTP request.
27
+ * Yields raw string chunks as they arrive.
28
+ */
29
+ export declare function httpStream(url: string, options?: HttpRequestOptions): AsyncGenerator<string, void, unknown>;
30
+ /**
31
+ * Parse NDJSON (newline-delimited JSON) stream.
32
+ * Used by Ollama's streaming API.
33
+ */
34
+ export declare function parseNDJSON<T = unknown>(stream: AsyncGenerator<string>): AsyncGenerator<T, void, unknown>;
35
+ /**
36
+ * Parse Server-Sent Events stream.
37
+ * Used by OpenAI-compatible APIs and LlamaCpp/vLLM.
38
+ */
39
+ export declare function parseSSE(stream: AsyncGenerator<string>): AsyncGenerator<{
40
+ event?: string;
41
+ data: string;
42
+ }, void, unknown>;
43
+ /**
44
+ * Build standard headers for LLM API requests.
45
+ */
46
+ export declare function buildHeaders(options: LLMClientOptions): Record<string, string>;
47
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAMxD,MAAM,WAAW,kBAAkB;IAC/B,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,WAAW,CAAC;CACxB;AAED,MAAM,WAAW,YAAY,CAAC,CAAC,GAAG,OAAO;IACrC,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,CAAC,CAAC;CACX;AAMD;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,GAAG,OAAO,EACzC,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,kBAAuB,GACjC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CA4C1B;AAMD;;;GAGG;AACH,wBAAuB,UAAU,CAC7B,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,kBAAuB,GACjC,cAAc,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAoDvC;AAMD;;;GAGG;AACH,wBAAuB,WAAW,CAAC,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