@semiont/inference 0.4.3 → 0.4.4
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.
- package/dist/index.d.ts +10 -2
- package/dist/index.js +18 -14
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,10 @@ interface InferenceResponse {
|
|
|
5
5
|
stopReason: 'end_turn' | 'max_tokens' | 'stop_sequence' | string;
|
|
6
6
|
}
|
|
7
7
|
interface InferenceClient {
|
|
8
|
+
/** Provider type identifier (e.g. 'anthropic', 'ollama') */
|
|
9
|
+
readonly type: string;
|
|
10
|
+
/** Model identifier used for generation (e.g. 'claude-opus-4-6', 'llama3') */
|
|
11
|
+
readonly modelId: string;
|
|
8
12
|
/**
|
|
9
13
|
* Generate text from a prompt (simple interface)
|
|
10
14
|
* @param prompt - The input prompt
|
|
@@ -34,8 +38,9 @@ interface InferenceClientConfig {
|
|
|
34
38
|
declare function createInferenceClient(config: InferenceClientConfig, logger?: Logger): InferenceClient;
|
|
35
39
|
|
|
36
40
|
declare class AnthropicInferenceClient implements InferenceClient {
|
|
41
|
+
readonly type: "anthropic";
|
|
42
|
+
readonly modelId: string;
|
|
37
43
|
private client;
|
|
38
|
-
private model;
|
|
39
44
|
private logger?;
|
|
40
45
|
constructor(apiKey: string, model: string, baseURL?: string, logger?: Logger);
|
|
41
46
|
generateText(prompt: string, maxTokens: number, temperature: number): Promise<string>;
|
|
@@ -43,8 +48,9 @@ declare class AnthropicInferenceClient implements InferenceClient {
|
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
declare class OllamaInferenceClient implements InferenceClient {
|
|
51
|
+
readonly type: "ollama";
|
|
52
|
+
readonly modelId: string;
|
|
46
53
|
private baseURL;
|
|
47
|
-
private model;
|
|
48
54
|
private logger?;
|
|
49
55
|
constructor(model: string, baseURL?: string, logger?: Logger);
|
|
50
56
|
generateText(prompt: string, maxTokens: number, temperature: number): Promise<string>;
|
|
@@ -52,6 +58,8 @@ declare class OllamaInferenceClient implements InferenceClient {
|
|
|
52
58
|
}
|
|
53
59
|
|
|
54
60
|
declare class MockInferenceClient implements InferenceClient {
|
|
61
|
+
readonly type: "mock";
|
|
62
|
+
readonly modelId: "mock-model";
|
|
55
63
|
private responses;
|
|
56
64
|
private responseIndex;
|
|
57
65
|
private stopReasons;
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
// src/implementations/anthropic.ts
|
|
2
2
|
import Anthropic from "@anthropic-ai/sdk";
|
|
3
3
|
var AnthropicInferenceClient = class {
|
|
4
|
+
type = "anthropic";
|
|
5
|
+
modelId;
|
|
4
6
|
client;
|
|
5
|
-
model;
|
|
6
7
|
logger;
|
|
7
8
|
constructor(apiKey, model, baseURL, logger) {
|
|
8
9
|
this.client = new Anthropic({
|
|
9
10
|
apiKey,
|
|
10
11
|
baseURL: baseURL || "https://api.anthropic.com"
|
|
11
12
|
});
|
|
12
|
-
this.
|
|
13
|
+
this.modelId = model;
|
|
13
14
|
this.logger = logger;
|
|
14
15
|
}
|
|
15
16
|
async generateText(prompt, maxTokens, temperature) {
|
|
@@ -18,13 +19,13 @@ var AnthropicInferenceClient = class {
|
|
|
18
19
|
}
|
|
19
20
|
async generateTextWithMetadata(prompt, maxTokens, temperature) {
|
|
20
21
|
this.logger?.debug("Generating text with inference client", {
|
|
21
|
-
model: this.
|
|
22
|
+
model: this.modelId,
|
|
22
23
|
promptLength: prompt.length,
|
|
23
24
|
maxTokens,
|
|
24
25
|
temperature
|
|
25
26
|
});
|
|
26
27
|
const response = await this.client.messages.create({
|
|
27
|
-
model: this.
|
|
28
|
+
model: this.modelId,
|
|
28
29
|
max_tokens: maxTokens,
|
|
29
30
|
temperature,
|
|
30
31
|
messages: [
|
|
@@ -35,20 +36,20 @@ var AnthropicInferenceClient = class {
|
|
|
35
36
|
]
|
|
36
37
|
});
|
|
37
38
|
this.logger?.debug("Inference response received", {
|
|
38
|
-
model: this.
|
|
39
|
+
model: this.modelId,
|
|
39
40
|
contentBlocks: response.content.length,
|
|
40
41
|
stopReason: response.stop_reason
|
|
41
42
|
});
|
|
42
43
|
const textContent = response.content.find((c) => c.type === "text");
|
|
43
44
|
if (!textContent || textContent.type !== "text") {
|
|
44
45
|
this.logger?.error("No text content in inference response", {
|
|
45
|
-
model: this.
|
|
46
|
+
model: this.modelId,
|
|
46
47
|
contentTypes: response.content.map((c) => c.type)
|
|
47
48
|
});
|
|
48
49
|
throw new Error("No text content in inference response");
|
|
49
50
|
}
|
|
50
51
|
this.logger?.info("Text generation completed", {
|
|
51
|
-
model: this.
|
|
52
|
+
model: this.modelId,
|
|
52
53
|
textLength: textContent.text.length,
|
|
53
54
|
stopReason: response.stop_reason
|
|
54
55
|
});
|
|
@@ -61,12 +62,13 @@ var AnthropicInferenceClient = class {
|
|
|
61
62
|
|
|
62
63
|
// src/implementations/ollama.ts
|
|
63
64
|
var OllamaInferenceClient = class {
|
|
65
|
+
type = "ollama";
|
|
66
|
+
modelId;
|
|
64
67
|
baseURL;
|
|
65
|
-
model;
|
|
66
68
|
logger;
|
|
67
69
|
constructor(model, baseURL, logger) {
|
|
68
70
|
this.baseURL = (baseURL || "http://localhost:11434").replace(/\/+$/, "");
|
|
69
|
-
this.
|
|
71
|
+
this.modelId = model;
|
|
70
72
|
this.logger = logger;
|
|
71
73
|
}
|
|
72
74
|
async generateText(prompt, maxTokens, temperature) {
|
|
@@ -75,7 +77,7 @@ var OllamaInferenceClient = class {
|
|
|
75
77
|
}
|
|
76
78
|
async generateTextWithMetadata(prompt, maxTokens, temperature) {
|
|
77
79
|
this.logger?.debug("Generating text with Ollama", {
|
|
78
|
-
model: this.
|
|
80
|
+
model: this.modelId,
|
|
79
81
|
promptLength: prompt.length,
|
|
80
82
|
maxTokens,
|
|
81
83
|
temperature
|
|
@@ -85,7 +87,7 @@ var OllamaInferenceClient = class {
|
|
|
85
87
|
method: "POST",
|
|
86
88
|
headers: { "Content-Type": "application/json" },
|
|
87
89
|
body: JSON.stringify({
|
|
88
|
-
model: this.
|
|
90
|
+
model: this.modelId,
|
|
89
91
|
prompt,
|
|
90
92
|
stream: false,
|
|
91
93
|
options: {
|
|
@@ -97,7 +99,7 @@ var OllamaInferenceClient = class {
|
|
|
97
99
|
if (!res.ok) {
|
|
98
100
|
const body = await res.text();
|
|
99
101
|
this.logger?.error("Ollama API error", {
|
|
100
|
-
model: this.
|
|
102
|
+
model: this.modelId,
|
|
101
103
|
status: res.status,
|
|
102
104
|
body
|
|
103
105
|
});
|
|
@@ -105,12 +107,12 @@ var OllamaInferenceClient = class {
|
|
|
105
107
|
}
|
|
106
108
|
const data = await res.json();
|
|
107
109
|
if (!data.response) {
|
|
108
|
-
this.logger?.error("Empty response from Ollama", { model: this.
|
|
110
|
+
this.logger?.error("Empty response from Ollama", { model: this.modelId });
|
|
109
111
|
throw new Error("Empty response from Ollama");
|
|
110
112
|
}
|
|
111
113
|
const stopReason = mapStopReason(data.done_reason);
|
|
112
114
|
this.logger?.info("Text generation completed", {
|
|
113
|
-
model: this.
|
|
115
|
+
model: this.modelId,
|
|
114
116
|
textLength: data.response.length,
|
|
115
117
|
stopReason
|
|
116
118
|
});
|
|
@@ -159,6 +161,8 @@ function createInferenceClient(config, logger) {
|
|
|
159
161
|
|
|
160
162
|
// src/implementations/mock.ts
|
|
161
163
|
var MockInferenceClient = class {
|
|
164
|
+
type = "mock";
|
|
165
|
+
modelId = "mock-model";
|
|
162
166
|
responses = [];
|
|
163
167
|
responseIndex = 0;
|
|
164
168
|
stopReasons = [];
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/implementations/anthropic.ts","../src/implementations/ollama.ts","../src/factory.ts","../src/implementations/mock.ts"],"sourcesContent":["// Anthropic Claude implementation of InferenceClient interface\n\nimport Anthropic from '@anthropic-ai/sdk';\nimport type { Logger } from '@semiont/core';\nimport { InferenceClient, InferenceResponse } from '../interface.js';\n\nexport class AnthropicInferenceClient implements InferenceClient {\n private client: Anthropic;\n private model: string;\n private logger?: Logger;\n\n constructor(apiKey: string, model: string, baseURL?: string, logger?: Logger) {\n this.client = new Anthropic({\n apiKey,\n baseURL: baseURL || 'https://api.anthropic.com',\n });\n this.model = model;\n this.logger = logger;\n }\n\n async generateText(prompt: string, maxTokens: number, temperature: number): Promise<string> {\n const response = await this.generateTextWithMetadata(prompt, maxTokens, temperature);\n return response.text;\n }\n\n async generateTextWithMetadata(prompt: string, maxTokens: number, temperature: number): Promise<InferenceResponse> {\n this.logger?.debug('Generating text with inference client', {\n model: this.model,\n promptLength: prompt.length,\n maxTokens,\n temperature\n });\n\n const response = await this.client.messages.create({\n model: this.model,\n max_tokens: maxTokens,\n temperature,\n messages: [\n {\n role: 'user',\n content: prompt\n }\n ]\n });\n\n this.logger?.debug('Inference response received', {\n model: this.model,\n contentBlocks: response.content.length,\n stopReason: response.stop_reason\n });\n\n const textContent = response.content.find(c => c.type === 'text');\n\n if (!textContent || textContent.type !== 'text') {\n this.logger?.error('No text content in inference response', {\n model: this.model,\n contentTypes: response.content.map(c => c.type)\n });\n throw new Error('No text content in inference response');\n }\n\n this.logger?.info('Text generation completed', {\n model: this.model,\n textLength: textContent.text.length,\n stopReason: response.stop_reason\n });\n\n return {\n text: textContent.text,\n stopReason: response.stop_reason || 'unknown'\n };\n }\n}\n","// Ollama implementation of InferenceClient interface\n// Uses native Ollama HTTP API (no SDK dependency)\n\nimport type { Logger } from '@semiont/core';\nimport { InferenceClient, InferenceResponse } from '../interface.js';\n\ninterface OllamaGenerateResponse {\n response: string;\n done: boolean;\n done_reason?: string;\n}\n\nexport class OllamaInferenceClient implements InferenceClient {\n private baseURL: string;\n private model: string;\n private logger?: Logger;\n\n constructor(model: string, baseURL?: string, logger?: Logger) {\n this.baseURL = (baseURL || 'http://localhost:11434').replace(/\\/+$/, '');\n this.model = model;\n this.logger = logger;\n }\n\n async generateText(prompt: string, maxTokens: number, temperature: number): Promise<string> {\n const response = await this.generateTextWithMetadata(prompt, maxTokens, temperature);\n return response.text;\n }\n\n async generateTextWithMetadata(prompt: string, maxTokens: number, temperature: number): Promise<InferenceResponse> {\n this.logger?.debug('Generating text with Ollama', {\n model: this.model,\n promptLength: prompt.length,\n maxTokens,\n temperature\n });\n\n const url = `${this.baseURL}/api/generate`;\n\n const res = await fetch(url, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n model: this.model,\n prompt,\n stream: false,\n options: {\n num_predict: maxTokens,\n temperature,\n },\n }),\n });\n\n if (!res.ok) {\n const body = await res.text();\n this.logger?.error('Ollama API error', {\n model: this.model,\n status: res.status,\n body,\n });\n throw new Error(`Ollama API error (${res.status}): ${body}`);\n }\n\n const data = await res.json() as OllamaGenerateResponse;\n\n if (!data.response) {\n this.logger?.error('Empty response from Ollama', { model: this.model });\n throw new Error('Empty response from Ollama');\n }\n\n const stopReason = mapStopReason(data.done_reason);\n\n this.logger?.info('Text generation completed', {\n model: this.model,\n textLength: data.response.length,\n stopReason,\n });\n\n return {\n text: data.response,\n stopReason,\n };\n }\n}\n\nfunction mapStopReason(doneReason: string | undefined): string {\n switch (doneReason) {\n case 'stop': return 'end_turn';\n case 'length': return 'max_tokens';\n default: return doneReason || 'unknown';\n }\n}\n","// Factory for creating inference client instances based on configuration\n\nimport type { Logger } from '@semiont/core';\nimport { InferenceClient } from './interface.js';\nimport { AnthropicInferenceClient } from './implementations/anthropic.js';\nimport { OllamaInferenceClient } from './implementations/ollama.js';\n\nexport type InferenceClientType = 'anthropic' | 'ollama';\n\nexport interface InferenceClientConfig {\n type: InferenceClientType;\n apiKey?: string;\n model: string;\n endpoint?: string;\n baseURL?: string;\n}\n\nexport function createInferenceClient(config: InferenceClientConfig, logger?: Logger): InferenceClient {\n switch (config.type) {\n case 'anthropic': {\n if (!config.apiKey || config.apiKey.trim() === '') {\n throw new Error('apiKey is required for Anthropic inference client');\n }\n return new AnthropicInferenceClient(\n config.apiKey,\n config.model,\n config.endpoint || config.baseURL,\n logger\n );\n }\n\n case 'ollama': {\n return new OllamaInferenceClient(\n config.model,\n config.endpoint || config.baseURL,\n logger\n );\n }\n\n default:\n throw new Error(`Unsupported inference client type: ${config.type}`);\n }\n}\n","// Mock implementation of InferenceClient for testing\n\nimport { InferenceClient, InferenceResponse } from '../interface.js';\n\nexport class MockInferenceClient implements InferenceClient {\n private responses: string[] = [];\n private responseIndex: number = 0;\n private stopReasons: string[] = [];\n public calls: Array<{ prompt: string; maxTokens: number; temperature: number }> = [];\n\n constructor(responses: string[] = ['Mock response'], stopReasons?: string[]) {\n this.responses = responses;\n this.stopReasons = stopReasons || responses.map(() => 'end_turn');\n }\n\n async generateText(prompt: string, maxTokens: number, temperature: number): Promise<string> {\n const response = await this.generateTextWithMetadata(prompt, maxTokens, temperature);\n return response.text;\n }\n\n async generateTextWithMetadata(prompt: string, maxTokens: number, temperature: number): Promise<InferenceResponse> {\n this.calls.push({ prompt, maxTokens, temperature });\n\n const text = this.responses[this.responseIndex];\n const stopReason = this.stopReasons[this.responseIndex] || 'end_turn';\n\n if (this.responseIndex < this.responses.length - 1) {\n this.responseIndex++;\n }\n\n return { text, stopReason };\n }\n\n // Test helper methods\n reset(): void {\n this.calls = [];\n this.responseIndex = 0;\n }\n\n setResponses(responses: string[], stopReasons?: string[]): void {\n this.responses = responses;\n this.stopReasons = stopReasons || responses.map(() => 'end_turn');\n this.responseIndex = 0;\n }\n}\n"],"mappings":";AAEA,OAAO,eAAe;AAIf,IAAM,2BAAN,MAA0D;AAAA,EACvD;AAAA,EACA;AAAA,EACA;AAAA,EAER,YAAY,QAAgB,OAAe,SAAkB,QAAiB;AAC5E,SAAK,SAAS,IAAI,UAAU;AAAA,MAC1B;AAAA,MACA,SAAS,WAAW;AAAA,IACtB,CAAC;AACD,SAAK,QAAQ;AACb,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,MAAM,aAAa,QAAgB,WAAmB,aAAsC;AAC1F,UAAM,WAAW,MAA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AAC;AAElD,UAAM,OAAO,KAAK,UAAU,KAAK,aAAa;AAC9C,UAAM,aAAa,KAAK,YAAY,KAAK,aAAa,KAAK;AAE3D,QAAI,KAAK,gBAAgB,KAAK,UAAU,SAAS,GAAG;AAClD,WAAK;AAAA,IACP;AAEA,WAAO,EAAE,MAAM,WAAW;AAAA,EAC5B;AAAA;AAAA,EAGA,QAAc;AACZ,SAAK,QAAQ,CAAC;AACd,SAAK,gBAAgB;AAAA,EACvB;AAAA,EAEA,aAAa,WAAqB,aAA8B;AAC9D,SAAK,YAAY;AACjB,SAAK,cAAc,eAAe,UAAU,IAAI,MAAM,UAAU;AAChE,SAAK,gBAAgB;AAAA,EACvB;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/implementations/anthropic.ts","../src/implementations/ollama.ts","../src/factory.ts","../src/implementations/mock.ts"],"sourcesContent":["// Anthropic Claude implementation of InferenceClient interface\n\nimport Anthropic from '@anthropic-ai/sdk';\nimport type { Logger } from '@semiont/core';\nimport { InferenceClient, InferenceResponse } from '../interface.js';\n\nexport class AnthropicInferenceClient implements InferenceClient {\n readonly type = 'anthropic' as const;\n readonly modelId: string;\n private client: Anthropic;\n private logger?: Logger;\n\n constructor(apiKey: string, model: string, baseURL?: string, logger?: Logger) {\n this.client = new Anthropic({\n apiKey,\n baseURL: baseURL || 'https://api.anthropic.com',\n });\n this.modelId = model;\n this.logger = logger;\n }\n\n async generateText(prompt: string, maxTokens: number, temperature: number): Promise<string> {\n const response = await this.generateTextWithMetadata(prompt, maxTokens, temperature);\n return response.text;\n }\n\n async generateTextWithMetadata(prompt: string, maxTokens: number, temperature: number): Promise<InferenceResponse> {\n this.logger?.debug('Generating text with inference client', {\n model: this.modelId,\n promptLength: prompt.length,\n maxTokens,\n temperature\n });\n\n const response = await this.client.messages.create({\n model: this.modelId,\n max_tokens: maxTokens,\n temperature,\n messages: [\n {\n role: 'user',\n content: prompt\n }\n ]\n });\n\n this.logger?.debug('Inference response received', {\n model: this.modelId,\n contentBlocks: response.content.length,\n stopReason: response.stop_reason\n });\n\n const textContent = response.content.find(c => c.type === 'text');\n\n if (!textContent || textContent.type !== 'text') {\n this.logger?.error('No text content in inference response', {\n model: this.modelId,\n contentTypes: response.content.map(c => c.type)\n });\n throw new Error('No text content in inference response');\n }\n\n this.logger?.info('Text generation completed', {\n model: this.modelId,\n textLength: textContent.text.length,\n stopReason: response.stop_reason\n });\n\n return {\n text: textContent.text,\n stopReason: response.stop_reason || 'unknown'\n };\n }\n}\n","// Ollama implementation of InferenceClient interface\n// Uses native Ollama HTTP API (no SDK dependency)\n\nimport type { Logger } from '@semiont/core';\nimport { InferenceClient, InferenceResponse } from '../interface.js';\n\ninterface OllamaGenerateResponse {\n response: string;\n done: boolean;\n done_reason?: string;\n}\n\nexport class OllamaInferenceClient implements InferenceClient {\n readonly type = 'ollama' as const;\n readonly modelId: string;\n private baseURL: string;\n private logger?: Logger;\n\n constructor(model: string, baseURL?: string, logger?: Logger) {\n this.baseURL = (baseURL || 'http://localhost:11434').replace(/\\/+$/, '');\n this.modelId = model;\n this.logger = logger;\n }\n\n async generateText(prompt: string, maxTokens: number, temperature: number): Promise<string> {\n const response = await this.generateTextWithMetadata(prompt, maxTokens, temperature);\n return response.text;\n }\n\n async generateTextWithMetadata(prompt: string, maxTokens: number, temperature: number): Promise<InferenceResponse> {\n this.logger?.debug('Generating text with Ollama', {\n model: this.modelId,\n promptLength: prompt.length,\n maxTokens,\n temperature\n });\n\n const url = `${this.baseURL}/api/generate`;\n\n const res = await fetch(url, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n model: this.modelId,\n prompt,\n stream: false,\n options: {\n num_predict: maxTokens,\n temperature,\n },\n }),\n });\n\n if (!res.ok) {\n const body = await res.text();\n this.logger?.error('Ollama API error', {\n model: this.modelId,\n status: res.status,\n body,\n });\n throw new Error(`Ollama API error (${res.status}): ${body}`);\n }\n\n const data = await res.json() as OllamaGenerateResponse;\n\n if (!data.response) {\n this.logger?.error('Empty response from Ollama', { model: this.modelId });\n throw new Error('Empty response from Ollama');\n }\n\n const stopReason = mapStopReason(data.done_reason);\n\n this.logger?.info('Text generation completed', {\n model: this.modelId,\n textLength: data.response.length,\n stopReason,\n });\n\n return {\n text: data.response,\n stopReason,\n };\n }\n}\n\nfunction mapStopReason(doneReason: string | undefined): string {\n switch (doneReason) {\n case 'stop': return 'end_turn';\n case 'length': return 'max_tokens';\n default: return doneReason || 'unknown';\n }\n}\n","// Factory for creating inference client instances based on configuration\n\nimport type { Logger } from '@semiont/core';\nimport { InferenceClient } from './interface.js';\nimport { AnthropicInferenceClient } from './implementations/anthropic.js';\nimport { OllamaInferenceClient } from './implementations/ollama.js';\n\nexport type InferenceClientType = 'anthropic' | 'ollama';\n\nexport interface InferenceClientConfig {\n type: InferenceClientType;\n apiKey?: string;\n model: string;\n endpoint?: string;\n baseURL?: string;\n}\n\nexport function createInferenceClient(config: InferenceClientConfig, logger?: Logger): InferenceClient {\n switch (config.type) {\n case 'anthropic': {\n if (!config.apiKey || config.apiKey.trim() === '') {\n throw new Error('apiKey is required for Anthropic inference client');\n }\n return new AnthropicInferenceClient(\n config.apiKey,\n config.model,\n config.endpoint || config.baseURL,\n logger\n );\n }\n\n case 'ollama': {\n return new OllamaInferenceClient(\n config.model,\n config.endpoint || config.baseURL,\n logger\n );\n }\n\n default:\n throw new Error(`Unsupported inference client type: ${config.type}`);\n }\n}\n","// Mock implementation of InferenceClient for testing\n\nimport { InferenceClient, InferenceResponse } from '../interface.js';\n\nexport class MockInferenceClient implements InferenceClient {\n readonly type = 'mock' as const;\n readonly modelId = 'mock-model' as const;\n private responses: string[] = [];\n private responseIndex: number = 0;\n private stopReasons: string[] = [];\n public calls: Array<{ prompt: string; maxTokens: number; temperature: number }> = [];\n\n constructor(responses: string[] = ['Mock response'], stopReasons?: string[]) {\n this.responses = responses;\n this.stopReasons = stopReasons || responses.map(() => 'end_turn');\n }\n\n async generateText(prompt: string, maxTokens: number, temperature: number): Promise<string> {\n const response = await this.generateTextWithMetadata(prompt, maxTokens, temperature);\n return response.text;\n }\n\n async generateTextWithMetadata(prompt: string, maxTokens: number, temperature: number): Promise<InferenceResponse> {\n this.calls.push({ prompt, maxTokens, temperature });\n\n const text = this.responses[this.responseIndex];\n const stopReason = this.stopReasons[this.responseIndex] || 'end_turn';\n\n if (this.responseIndex < this.responses.length - 1) {\n this.responseIndex++;\n }\n\n return { text, stopReason };\n }\n\n // Test helper methods\n reset(): void {\n this.calls = [];\n this.responseIndex = 0;\n }\n\n setResponses(responses: string[], stopReasons?: string[]): void {\n this.responses = responses;\n this.stopReasons = stopReasons || responses.map(() => 'end_turn');\n this.responseIndex = 0;\n }\n}\n"],"mappings":";AAEA,OAAO,eAAe;AAIf,IAAM,2BAAN,MAA0D;AAAA,EACtD,OAAO;AAAA,EACP;AAAA,EACD;AAAA,EACA;AAAA,EAER,YAAY,QAAgB,OAAe,SAAkB,QAAiB;AAC5E,SAAK,SAAS,IAAI,UAAU;AAAA,MAC1B;AAAA,MACA,SAAS,WAAW;AAAA,IACtB,CAAC;AACD,SAAK,UAAU;AACf,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,MAAM,aAAa,QAAgB,WAAmB,aAAsC;AAC1F,UAAM,WAAW,MAAM,KAAK,yBAAyB,QAAQ,WAAW,WAAW;AACnF,WAAO,SAAS;AAAA,EAClB;AAAA,EAEA,MAAM,yBAAyB,QAAgB,WAAmB,aAAiD;AACjH,SAAK,QAAQ,MAAM,yCAAyC;AAAA,MAC1D,OAAO,KAAK;AAAA,MACZ,cAAc,OAAO;AAAA,MACrB;AAAA,MACA;AAAA,IACF,CAAC;AAED,UAAM,WAAW,MAAM,KAAK,OAAO,SAAS,OAAO;AAAA,MACjD,OAAO,KAAK;AAAA,MACZ,YAAY;AAAA,MACZ;AAAA,MACA,UAAU;AAAA,QACR;AAAA,UACE,MAAM;AAAA,UACN,SAAS;AAAA,QACX;AAAA,MACF;AAAA,IACF,CAAC;AAED,S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