koishi-plugin-openai-compatible 1.0.5 → 1.0.6
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 +153 -52
- package/lib/api.d.ts +7 -0
- package/lib/api.js +32 -0
- package/lib/config.d.ts +23 -0
- package/lib/config.js +54 -0
- package/lib/emotion.d.ts +9 -0
- package/lib/emotion.js +32 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +157 -0
- package/lib/locales/zh.d.ts +44 -0
- package/lib/locales/zh.js +45 -0
- package/lib/storage.d.ts +10 -0
- package/lib/storage.js +25 -0
- package/lib/types.d.ts +32 -0
- package/lib/types.js +2 -0
- package/package.json +1 -1
- package/src/api.ts +40 -0
- package/src/config.ts +91 -0
- package/src/emotion.ts +37 -0
- package/src/index.ts +157 -99
- package/src/locales/zh.ts +43 -0
- package/src/storage.ts +27 -0
- package/src/types.ts +34 -0
- package/tsconfig.json +12 -5
- package/dist/index.js +0 -97
package/dist/index.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Config = exports.name = void 0;
|
|
4
|
-
exports.apply = apply;
|
|
5
|
-
const koishi_1 = require("koishi");
|
|
6
|
-
exports.name = 'openai-compatible';
|
|
7
|
-
exports.Config = koishi_1.Schema.object({
|
|
8
|
-
apiEndpoint: koishi_1.Schema.string().required().description('API endpoint URL, e.g., "https://api.openai.com/v1"'),
|
|
9
|
-
apiKey: koishi_1.Schema.string().required().description('API key'),
|
|
10
|
-
model: koishi_1.Schema.string().required().description('Model name, e.g., "gpt-3.5-turbo"'),
|
|
11
|
-
temperature: koishi_1.Schema.number().min(0).max(2).default(1).description('Controls randomness of responses (0-2)'),
|
|
12
|
-
maxTokens: koishi_1.Schema.number().min(1).default(2048).description('Maximum number of tokens to generate'),
|
|
13
|
-
systemPrompt: koishi_1.Schema.string().description('System prompt (optional)'),
|
|
14
|
-
commandName: koishi_1.Schema.string().description('Custom command name (optional)'),
|
|
15
|
-
triggerPrefix: koishi_1.Schema.string().description('Trigger prefix (optional, used when no commandName is set)'),
|
|
16
|
-
userPrompts: koishi_1.Schema.dict(koishi_1.Schema.string()).description('User-specific prompts (optional)'),
|
|
17
|
-
thinkingPrompt: koishi_1.Schema.string().description('Thinking prompt (optional)'),
|
|
18
|
-
blacklist: koishi_1.Schema.array(koishi_1.Schema.string()).description('List of blacklisted user IDs (optional)'),
|
|
19
|
-
blacklistResponse: koishi_1.Schema.string().description('Response for blacklisted users (text or image URL)'),
|
|
20
|
-
});
|
|
21
|
-
function apply(ctx, config) {
|
|
22
|
-
const { apiEndpoint, apiKey, model, temperature, maxTokens, systemPrompt, commandName, triggerPrefix, userPrompts, thinkingPrompt, blacklist, blacklistResponse } = config;
|
|
23
|
-
// Register command if commandName is provided
|
|
24
|
-
if (commandName) {
|
|
25
|
-
ctx.command(commandName, 'Chat with OpenAI-compatible API')
|
|
26
|
-
.action(async ({ session }, ...args) => {
|
|
27
|
-
if (!session || !session.userId || blacklist?.includes(session.userId)) {
|
|
28
|
-
return blacklistResponse || 'You are not allowed to use this command.';
|
|
29
|
-
}
|
|
30
|
-
const userPrompt = !session || !session.userId ? '' : userPrompts?.[session.userId] || '';
|
|
31
|
-
const prompt = args.join(' ');
|
|
32
|
-
const fullPrompt = systemPrompt ? `${systemPrompt}\n${userPrompt}\n${prompt}` : `${userPrompt}\n${prompt}`;
|
|
33
|
-
if (thinkingPrompt) {
|
|
34
|
-
if (session)
|
|
35
|
-
session.send(thinkingPrompt);
|
|
36
|
-
}
|
|
37
|
-
try {
|
|
38
|
-
const response = await ctx.http.post(`${apiEndpoint}/chat/completions`, {
|
|
39
|
-
model,
|
|
40
|
-
messages: [
|
|
41
|
-
{ role: 'user', content: fullPrompt },
|
|
42
|
-
],
|
|
43
|
-
temperature,
|
|
44
|
-
max_tokens: maxTokens,
|
|
45
|
-
}, {
|
|
46
|
-
headers: {
|
|
47
|
-
'Authorization': `Bearer ${apiKey}`,
|
|
48
|
-
'Content-Type': 'application/json',
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
return response.choices[0].message.content;
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
ctx.logger.error('OpenAI API error:', error);
|
|
55
|
-
return 'Failed to get response from the API.';
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
// Handle all messages if no commandName is provided
|
|
61
|
-
ctx.middleware(async (session, next) => {
|
|
62
|
-
if (!session || !session.userId || blacklist?.includes(session.userId)) {
|
|
63
|
-
return blacklistResponse || 'You are not allowed to use this command.';
|
|
64
|
-
}
|
|
65
|
-
const content = session.content;
|
|
66
|
-
if (!content || (triggerPrefix && !content.startsWith(triggerPrefix)))
|
|
67
|
-
return next();
|
|
68
|
-
const userPrompt = !session || !session.userId ? '' : userPrompts?.[session.userId] || '';
|
|
69
|
-
const prompt = !content ? '' : triggerPrefix ? content.slice(triggerPrefix.length) : content;
|
|
70
|
-
const fullPrompt = systemPrompt ? `${systemPrompt}\n${userPrompt}\n${prompt}` : `${userPrompt}\n${prompt}`;
|
|
71
|
-
if (thinkingPrompt) {
|
|
72
|
-
if (session)
|
|
73
|
-
session.send(thinkingPrompt);
|
|
74
|
-
}
|
|
75
|
-
try {
|
|
76
|
-
const response = await ctx.http.post(`${apiEndpoint}/chat/completions`, {
|
|
77
|
-
model,
|
|
78
|
-
messages: [
|
|
79
|
-
{ role: 'user', content: fullPrompt },
|
|
80
|
-
],
|
|
81
|
-
temperature,
|
|
82
|
-
max_tokens: maxTokens,
|
|
83
|
-
}, {
|
|
84
|
-
headers: {
|
|
85
|
-
'Authorization': `Bearer ${apiKey}`,
|
|
86
|
-
'Content-Type': 'application/json',
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
return response.choices[0].message.content;
|
|
90
|
-
}
|
|
91
|
-
catch (error) {
|
|
92
|
-
ctx.logger.error('OpenAI API error:', error);
|
|
93
|
-
return 'Failed to get response from the API.';
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}
|