express-genix 3.0.0 → 4.1.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.
- package/README.md +122 -11
- package/index.js +160 -1
- package/lib/ai-cli.js +133 -0
- package/lib/features.js +2 -0
- package/lib/generator.js +57 -7
- package/package.json +1 -1
- package/templates/agents/graph.js.ejs +85 -0
- package/templates/config/ai.js.ejs +54 -0
- package/templates/controllers/aiController.js.ejs +102 -0
- package/templates/core/env.ejs +13 -0
- package/templates/core/env.example.ejs +13 -0
- package/templates/core/package.json.ejs +11 -3
- package/templates/mcp/mcp-config.json.ejs +9 -0
- package/templates/mcp/server.js.ejs +151 -0
- package/templates/middleware/auditLog.js.ejs +5 -3
- package/templates/routes/aiRoutes.js.ejs +126 -0
- package/templates/routes/index.js.ejs +2 -0
- package/templates/services/aiService.js.ejs +80 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const { getModel } = require('../config/ai');
|
|
2
|
+
const { HumanMessage, SystemMessage, AIMessage } = require('@langchain/core/messages');
|
|
3
|
+
const { StringOutputParser } = require('@langchain/core/output_parsers');
|
|
4
|
+
const { ChatPromptTemplate } = require('@langchain/core/prompts');
|
|
5
|
+
const logger = require('../utils/logger');
|
|
6
|
+
|
|
7
|
+
const DEFAULT_SYSTEM_PROMPT = 'You are a helpful AI assistant.';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Send a chat message and get a complete response.
|
|
11
|
+
*/
|
|
12
|
+
const chat = async (message, options = {}) => {
|
|
13
|
+
const model = getModel(options);
|
|
14
|
+
const messages = [
|
|
15
|
+
new SystemMessage(options.systemPrompt || DEFAULT_SYSTEM_PROMPT),
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
if (Array.isArray(options.history)) {
|
|
19
|
+
for (const msg of options.history) {
|
|
20
|
+
if (msg.role === 'user') messages.push(new HumanMessage(msg.content));
|
|
21
|
+
else if (msg.role === 'assistant') messages.push(new AIMessage(msg.content));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
messages.push(new HumanMessage(message));
|
|
26
|
+
|
|
27
|
+
const response = await model.invoke(messages);
|
|
28
|
+
|
|
29
|
+
logger.info('AI chat completed', {
|
|
30
|
+
provider: options.provider || process.env.AI_PROVIDER || 'openai',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
content: response.content,
|
|
35
|
+
model: response.response_metadata?.model || 'unknown',
|
|
36
|
+
usage: response.usage_metadata || null,
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Stream a chat response. Returns an async generator yielding text chunks.
|
|
42
|
+
*/
|
|
43
|
+
const stream = async function* (message, options = {}) {
|
|
44
|
+
const model = getModel(options);
|
|
45
|
+
const messages = [
|
|
46
|
+
new SystemMessage(options.systemPrompt || DEFAULT_SYSTEM_PROMPT),
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
if (Array.isArray(options.history)) {
|
|
50
|
+
for (const msg of options.history) {
|
|
51
|
+
if (msg.role === 'user') messages.push(new HumanMessage(msg.content));
|
|
52
|
+
else if (msg.role === 'assistant') messages.push(new AIMessage(msg.content));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
messages.push(new HumanMessage(message));
|
|
57
|
+
|
|
58
|
+
const streamResponse = await model.stream(messages);
|
|
59
|
+
|
|
60
|
+
for await (const chunk of streamResponse) {
|
|
61
|
+
if (chunk.content) {
|
|
62
|
+
yield chunk.content;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Run a prompt template chain with variable substitution.
|
|
69
|
+
*/
|
|
70
|
+
const chain = async (template, variables = {}, options = {}) => {
|
|
71
|
+
const model = getModel(options);
|
|
72
|
+
const prompt = ChatPromptTemplate.fromTemplate(template);
|
|
73
|
+
const outputParser = new StringOutputParser();
|
|
74
|
+
const pipeline = prompt.pipe(model).pipe(outputParser);
|
|
75
|
+
|
|
76
|
+
const result = await pipeline.invoke(variables);
|
|
77
|
+
return result;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
module.exports = { chat, stream, chain };
|