Skip to content

Commit af01101

Browse files
committed
Implement Neo.ai.provider.Base #7962
1 parent 6948036 commit af01101

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

ai/provider/Base.mjs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import Base from '../../src/core/Base.mjs';
2+
3+
/**
4+
* Abstract base class for AI model providers.
5+
* Defines the standard interface for interacting with LLMs (Gemini, OpenAI, etc.).
6+
*
7+
* @class Neo.ai.provider.Base
8+
* @extends Neo.core.Base
9+
* @abstract
10+
*/
11+
class BaseProvider extends Base {
12+
static config = {
13+
/**
14+
* @member {String} className='Neo.ai.provider.Base'
15+
* @protected
16+
*/
17+
className: 'Neo.ai.provider.Base',
18+
/**
19+
* The default model name to use.
20+
* @member {String|null} modelName=null
21+
*/
22+
modelName: null,
23+
/**
24+
* List of required environment variables for this provider.
25+
* @member {String[]} requiredEnv=[]
26+
*/
27+
requiredEnv: []
28+
}
29+
30+
/**
31+
* Validates that all required environment variables are present.
32+
* @param {Object} config
33+
* @throws {Error} If any required variable is missing.
34+
*/
35+
construct(config) {
36+
super.construct(config);
37+
38+
if (this.requiredEnv.length > 0) {
39+
const missing = this.requiredEnv.filter(key => !process.env[key]);
40+
if (missing.length > 0) {
41+
throw new Error(`[${this.className}] Missing required environment variables: ${missing.join(', ')}`);
42+
}
43+
}
44+
}
45+
46+
/**
47+
* Generates a text completion for the given prompt.
48+
* Must be implemented by subclasses.
49+
*
50+
* @param {String|Object|Array} input The prompt or message history.
51+
* @param {Object} [options] Additional generation options (temperature, maxTokens, etc.).
52+
* @returns {Promise<Object>} The provider response (standardized).
53+
* @abstract
54+
*/
55+
async generate(input, options) {
56+
throw new Error('Abstract method generate() must be implemented by subclass.');
57+
}
58+
59+
/**
60+
* Streams text completion for the given prompt.
61+
* Must be implemented by subclasses.
62+
*
63+
* @param {String|Object|Array} input The prompt or message history.
64+
* @param {Object} [options] Additional generation options.
65+
* @returns {AsyncGenerator} An async generator yielding chunks of the response.
66+
* @abstract
67+
*/
68+
async *stream(input, options) {
69+
throw new Error('Abstract method stream() must be implemented by subclass.');
70+
}
71+
}
72+
73+
export default Neo.setupClass(BaseProvider);

0 commit comments

Comments
 (0)