cognitive-modules-cli 2.2.0 → 2.2.5
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/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/README.md +35 -29
- package/dist/cli.js +572 -28
- package/dist/commands/add.d.ts +33 -14
- package/dist/commands/add.js +222 -13
- package/dist/commands/compose.d.ts +31 -0
- package/dist/commands/compose.js +185 -0
- package/dist/commands/index.d.ts +5 -0
- package/dist/commands/index.js +5 -0
- package/dist/commands/init.js +23 -1
- package/dist/commands/migrate.d.ts +30 -0
- package/dist/commands/migrate.js +650 -0
- package/dist/commands/pipe.d.ts +1 -0
- package/dist/commands/pipe.js +31 -11
- package/dist/commands/remove.js +33 -2
- package/dist/commands/run.d.ts +1 -0
- package/dist/commands/run.js +37 -27
- package/dist/commands/search.d.ts +28 -0
- package/dist/commands/search.js +143 -0
- package/dist/commands/test.d.ts +65 -0
- package/dist/commands/test.js +454 -0
- package/dist/commands/update.d.ts +1 -0
- package/dist/commands/update.js +106 -14
- package/dist/commands/validate.d.ts +36 -0
- package/dist/commands/validate.js +97 -0
- package/dist/errors/index.d.ts +218 -0
- package/dist/errors/index.js +412 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +5 -1
- package/dist/mcp/server.js +84 -79
- package/dist/modules/composition.d.ts +251 -0
- package/dist/modules/composition.js +1330 -0
- package/dist/modules/index.d.ts +2 -0
- package/dist/modules/index.js +2 -0
- package/dist/modules/loader.d.ts +22 -2
- package/dist/modules/loader.js +171 -6
- package/dist/modules/runner.d.ts +422 -1
- package/dist/modules/runner.js +1472 -71
- package/dist/modules/subagent.d.ts +6 -1
- package/dist/modules/subagent.js +20 -13
- package/dist/modules/validator.d.ts +28 -0
- package/dist/modules/validator.js +637 -0
- package/dist/providers/anthropic.d.ts +15 -0
- package/dist/providers/anthropic.js +147 -5
- package/dist/providers/base.d.ts +11 -0
- package/dist/providers/base.js +18 -0
- package/dist/providers/gemini.d.ts +15 -0
- package/dist/providers/gemini.js +122 -5
- package/dist/providers/ollama.d.ts +15 -0
- package/dist/providers/ollama.js +111 -3
- package/dist/providers/openai.d.ts +11 -0
- package/dist/providers/openai.js +133 -0
- package/dist/registry/client.d.ts +204 -0
- package/dist/registry/client.js +356 -0
- package/dist/registry/index.d.ts +4 -0
- package/dist/registry/index.js +4 -0
- package/dist/server/http.js +173 -42
- package/dist/types.d.ts +123 -8
- package/dist/types.js +4 -1
- package/dist/version.d.ts +1 -0
- package/dist/version.js +4 -0
- package/package.json +32 -7
- package/src/cli.ts +0 -410
- package/src/commands/add.ts +0 -315
- package/src/commands/index.ts +0 -12
- package/src/commands/init.ts +0 -94
- package/src/commands/list.ts +0 -33
- package/src/commands/pipe.ts +0 -76
- package/src/commands/remove.ts +0 -57
- package/src/commands/run.ts +0 -80
- package/src/commands/update.ts +0 -130
- package/src/commands/versions.ts +0 -79
- package/src/index.ts +0 -55
- package/src/mcp/index.ts +0 -5
- package/src/mcp/server.ts +0 -403
- package/src/modules/index.ts +0 -7
- package/src/modules/loader.ts +0 -318
- package/src/modules/runner.ts +0 -495
- package/src/modules/subagent.ts +0 -275
- package/src/providers/anthropic.ts +0 -89
- package/src/providers/base.ts +0 -29
- package/src/providers/deepseek.ts +0 -83
- package/src/providers/gemini.ts +0 -117
- package/src/providers/index.ts +0 -78
- package/src/providers/minimax.ts +0 -81
- package/src/providers/moonshot.ts +0 -82
- package/src/providers/ollama.ts +0 -83
- package/src/providers/openai.ts +0 -84
- package/src/providers/qwen.ts +0 -82
- package/src/server/http.ts +0 -316
- package/src/server/index.ts +0 -6
- package/src/types.ts +0 -495
- package/tsconfig.json +0 -17
package/src/providers/minimax.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MiniMax Provider - MiniMax API
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { BaseProvider } from './base.js';
|
|
6
|
-
import type { InvokeParams, InvokeResult } from '../types.js';
|
|
7
|
-
|
|
8
|
-
export class MiniMaxProvider extends BaseProvider {
|
|
9
|
-
name = 'minimax';
|
|
10
|
-
private apiKey: string;
|
|
11
|
-
private model: string;
|
|
12
|
-
private baseUrl = 'https://api.minimax.chat/v1';
|
|
13
|
-
|
|
14
|
-
constructor(apiKey?: string, model = 'MiniMax-M2.1') {
|
|
15
|
-
super();
|
|
16
|
-
this.apiKey = apiKey || process.env.MINIMAX_API_KEY || '';
|
|
17
|
-
this.model = model;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
isConfigured(): boolean {
|
|
21
|
-
return !!this.apiKey;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async invoke(params: InvokeParams): Promise<InvokeResult> {
|
|
25
|
-
if (!this.isConfigured()) {
|
|
26
|
-
throw new Error('MiniMax API key not configured. Set MINIMAX_API_KEY environment variable.');
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const url = `${this.baseUrl}/text/chatcompletion_v2`;
|
|
30
|
-
|
|
31
|
-
const body: Record<string, unknown> = {
|
|
32
|
-
model: this.model,
|
|
33
|
-
messages: params.messages.map(m => ({ role: m.role, content: m.content })),
|
|
34
|
-
temperature: params.temperature ?? 0.7,
|
|
35
|
-
max_tokens: params.maxTokens ?? 4096,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// Add JSON mode if schema provided
|
|
39
|
-
if (params.jsonSchema) {
|
|
40
|
-
const lastUserIdx = params.messages.findLastIndex(m => m.role === 'user');
|
|
41
|
-
if (lastUserIdx >= 0) {
|
|
42
|
-
const messages = [...params.messages];
|
|
43
|
-
messages[lastUserIdx] = {
|
|
44
|
-
...messages[lastUserIdx],
|
|
45
|
-
content: messages[lastUserIdx].content + this.buildJsonPrompt(params.jsonSchema),
|
|
46
|
-
};
|
|
47
|
-
body.messages = messages.map(m => ({ role: m.role, content: m.content }));
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const response = await fetch(url, {
|
|
52
|
-
method: 'POST',
|
|
53
|
-
headers: {
|
|
54
|
-
'Content-Type': 'application/json',
|
|
55
|
-
'Authorization': `Bearer ${this.apiKey}`,
|
|
56
|
-
},
|
|
57
|
-
body: JSON.stringify(body),
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
if (!response.ok) {
|
|
61
|
-
const error = await response.text();
|
|
62
|
-
throw new Error(`MiniMax API error: ${response.status} - ${error}`);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const data = await response.json() as {
|
|
66
|
-
choices?: Array<{ message?: { content?: string } }>;
|
|
67
|
-
usage?: { prompt_tokens?: number; completion_tokens?: number; total_tokens?: number };
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const content = data.choices?.[0]?.message?.content || '';
|
|
71
|
-
|
|
72
|
-
return {
|
|
73
|
-
content,
|
|
74
|
-
usage: data.usage ? {
|
|
75
|
-
promptTokens: data.usage.prompt_tokens || 0,
|
|
76
|
-
completionTokens: data.usage.completion_tokens || 0,
|
|
77
|
-
totalTokens: data.usage.total_tokens || 0,
|
|
78
|
-
} : undefined,
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Moonshot Provider - Moonshot AI (Kimi) API
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { BaseProvider } from './base.js';
|
|
6
|
-
import type { InvokeParams, InvokeResult } from '../types.js';
|
|
7
|
-
|
|
8
|
-
export class MoonshotProvider extends BaseProvider {
|
|
9
|
-
name = 'moonshot';
|
|
10
|
-
private apiKey: string;
|
|
11
|
-
private model: string;
|
|
12
|
-
private baseUrl = 'https://api.moonshot.cn/v1';
|
|
13
|
-
|
|
14
|
-
constructor(apiKey?: string, model = 'kimi-k2.5') {
|
|
15
|
-
super();
|
|
16
|
-
this.apiKey = apiKey || process.env.MOONSHOT_API_KEY || '';
|
|
17
|
-
this.model = model;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
isConfigured(): boolean {
|
|
21
|
-
return !!this.apiKey;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async invoke(params: InvokeParams): Promise<InvokeResult> {
|
|
25
|
-
if (!this.isConfigured()) {
|
|
26
|
-
throw new Error('Moonshot API key not configured. Set MOONSHOT_API_KEY environment variable.');
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const url = `${this.baseUrl}/chat/completions`;
|
|
30
|
-
|
|
31
|
-
const body: Record<string, unknown> = {
|
|
32
|
-
model: this.model,
|
|
33
|
-
messages: params.messages.map(m => ({ role: m.role, content: m.content })),
|
|
34
|
-
temperature: params.temperature ?? 0.7,
|
|
35
|
-
max_tokens: params.maxTokens ?? 4096,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// Add JSON mode if schema provided
|
|
39
|
-
if (params.jsonSchema) {
|
|
40
|
-
body.response_format = { type: 'json_object' };
|
|
41
|
-
const lastUserIdx = params.messages.findLastIndex(m => m.role === 'user');
|
|
42
|
-
if (lastUserIdx >= 0) {
|
|
43
|
-
const messages = [...params.messages];
|
|
44
|
-
messages[lastUserIdx] = {
|
|
45
|
-
...messages[lastUserIdx],
|
|
46
|
-
content: messages[lastUserIdx].content + this.buildJsonPrompt(params.jsonSchema),
|
|
47
|
-
};
|
|
48
|
-
body.messages = messages.map(m => ({ role: m.role, content: m.content }));
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const response = await fetch(url, {
|
|
53
|
-
method: 'POST',
|
|
54
|
-
headers: {
|
|
55
|
-
'Content-Type': 'application/json',
|
|
56
|
-
'Authorization': `Bearer ${this.apiKey}`,
|
|
57
|
-
},
|
|
58
|
-
body: JSON.stringify(body),
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
if (!response.ok) {
|
|
62
|
-
const error = await response.text();
|
|
63
|
-
throw new Error(`Moonshot API error: ${response.status} - ${error}`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const data = await response.json() as {
|
|
67
|
-
choices?: Array<{ message?: { content?: string } }>;
|
|
68
|
-
usage?: { prompt_tokens?: number; completion_tokens?: number; total_tokens?: number };
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const content = data.choices?.[0]?.message?.content || '';
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
content,
|
|
75
|
-
usage: data.usage ? {
|
|
76
|
-
promptTokens: data.usage.prompt_tokens || 0,
|
|
77
|
-
completionTokens: data.usage.completion_tokens || 0,
|
|
78
|
-
totalTokens: data.usage.total_tokens || 0,
|
|
79
|
-
} : undefined,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
}
|
package/src/providers/ollama.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Ollama Provider - Local LLM via Ollama
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { BaseProvider } from './base.js';
|
|
6
|
-
import type { InvokeParams, InvokeResult } from '../types.js';
|
|
7
|
-
|
|
8
|
-
export class OllamaProvider extends BaseProvider {
|
|
9
|
-
name = 'ollama';
|
|
10
|
-
private model: string;
|
|
11
|
-
private baseUrl: string;
|
|
12
|
-
|
|
13
|
-
constructor(model = 'llama4', baseUrl = 'http://localhost:11434') {
|
|
14
|
-
super();
|
|
15
|
-
this.model = process.env.OLLAMA_MODEL || model;
|
|
16
|
-
this.baseUrl = process.env.OLLAMA_HOST || baseUrl;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
isConfigured(): boolean {
|
|
20
|
-
return true; // Ollama doesn't need API key
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async invoke(params: InvokeParams): Promise<InvokeResult> {
|
|
24
|
-
const url = `${this.baseUrl}/api/chat`;
|
|
25
|
-
|
|
26
|
-
let messages = params.messages.map(m => ({ role: m.role, content: m.content }));
|
|
27
|
-
|
|
28
|
-
// Add JSON mode if schema provided
|
|
29
|
-
if (params.jsonSchema) {
|
|
30
|
-
const lastUserIdx = messages.findLastIndex(m => m.role === 'user');
|
|
31
|
-
if (lastUserIdx >= 0) {
|
|
32
|
-
messages = [...messages];
|
|
33
|
-
messages[lastUserIdx] = {
|
|
34
|
-
...messages[lastUserIdx],
|
|
35
|
-
content: messages[lastUserIdx].content + this.buildJsonPrompt(params.jsonSchema),
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const body: Record<string, unknown> = {
|
|
41
|
-
model: this.model,
|
|
42
|
-
messages,
|
|
43
|
-
stream: false,
|
|
44
|
-
options: {
|
|
45
|
-
temperature: params.temperature ?? 0.7,
|
|
46
|
-
num_predict: params.maxTokens ?? 4096,
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
// Request JSON format
|
|
51
|
-
if (params.jsonSchema) {
|
|
52
|
-
body.format = 'json';
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const response = await fetch(url, {
|
|
56
|
-
method: 'POST',
|
|
57
|
-
headers: { 'Content-Type': 'application/json' },
|
|
58
|
-
body: JSON.stringify(body),
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
if (!response.ok) {
|
|
62
|
-
const error = await response.text();
|
|
63
|
-
throw new Error(`Ollama API error: ${response.status} - ${error}`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const data = await response.json() as {
|
|
67
|
-
message?: { content?: string };
|
|
68
|
-
prompt_eval_count?: number;
|
|
69
|
-
eval_count?: number;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const content = data.message?.content || '';
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
content,
|
|
76
|
-
usage: {
|
|
77
|
-
promptTokens: data.prompt_eval_count || 0,
|
|
78
|
-
completionTokens: data.eval_count || 0,
|
|
79
|
-
totalTokens: (data.prompt_eval_count || 0) + (data.eval_count || 0),
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
}
|
package/src/providers/openai.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenAI Provider - OpenAI API (and compatible APIs)
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { BaseProvider } from './base.js';
|
|
6
|
-
import type { InvokeParams, InvokeResult } from '../types.js';
|
|
7
|
-
|
|
8
|
-
export class OpenAIProvider extends BaseProvider {
|
|
9
|
-
name = 'openai';
|
|
10
|
-
private apiKey: string;
|
|
11
|
-
private model: string;
|
|
12
|
-
private baseUrl: string;
|
|
13
|
-
|
|
14
|
-
constructor(apiKey?: string, model = 'gpt-5.2', baseUrl = 'https://api.openai.com/v1') {
|
|
15
|
-
super();
|
|
16
|
-
this.apiKey = apiKey || process.env.OPENAI_API_KEY || '';
|
|
17
|
-
this.model = model;
|
|
18
|
-
this.baseUrl = baseUrl;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
isConfigured(): boolean {
|
|
22
|
-
return !!this.apiKey;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async invoke(params: InvokeParams): Promise<InvokeResult> {
|
|
26
|
-
if (!this.isConfigured()) {
|
|
27
|
-
throw new Error('OpenAI API key not configured. Set OPENAI_API_KEY environment variable.');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const url = `${this.baseUrl}/chat/completions`;
|
|
31
|
-
|
|
32
|
-
const body: Record<string, unknown> = {
|
|
33
|
-
model: this.model,
|
|
34
|
-
messages: params.messages,
|
|
35
|
-
temperature: params.temperature ?? 0.7,
|
|
36
|
-
max_tokens: params.maxTokens ?? 4096,
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// Add JSON mode if schema provided
|
|
40
|
-
if (params.jsonSchema) {
|
|
41
|
-
body.response_format = { type: 'json_object' };
|
|
42
|
-
// Append schema instruction to last user message
|
|
43
|
-
const lastUserIdx = params.messages.findLastIndex(m => m.role === 'user');
|
|
44
|
-
if (lastUserIdx >= 0) {
|
|
45
|
-
const messages = [...params.messages];
|
|
46
|
-
messages[lastUserIdx] = {
|
|
47
|
-
...messages[lastUserIdx],
|
|
48
|
-
content: messages[lastUserIdx].content + this.buildJsonPrompt(params.jsonSchema),
|
|
49
|
-
};
|
|
50
|
-
body.messages = messages;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const response = await fetch(url, {
|
|
55
|
-
method: 'POST',
|
|
56
|
-
headers: {
|
|
57
|
-
'Content-Type': 'application/json',
|
|
58
|
-
'Authorization': `Bearer ${this.apiKey}`,
|
|
59
|
-
},
|
|
60
|
-
body: JSON.stringify(body),
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
if (!response.ok) {
|
|
64
|
-
const error = await response.text();
|
|
65
|
-
throw new Error(`OpenAI API error: ${response.status} - ${error}`);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const data = await response.json() as {
|
|
69
|
-
choices?: Array<{ message?: { content?: string } }>;
|
|
70
|
-
usage?: { prompt_tokens?: number; completion_tokens?: number; total_tokens?: number };
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const content = data.choices?.[0]?.message?.content || '';
|
|
74
|
-
|
|
75
|
-
return {
|
|
76
|
-
content,
|
|
77
|
-
usage: data.usage ? {
|
|
78
|
-
promptTokens: data.usage.prompt_tokens || 0,
|
|
79
|
-
completionTokens: data.usage.completion_tokens || 0,
|
|
80
|
-
totalTokens: data.usage.total_tokens || 0,
|
|
81
|
-
} : undefined,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
}
|
package/src/providers/qwen.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Qwen Provider - Alibaba Tongyi Qianwen (通义千问) via DashScope API
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { BaseProvider } from './base.js';
|
|
6
|
-
import type { InvokeParams, InvokeResult } from '../types.js';
|
|
7
|
-
|
|
8
|
-
export class QwenProvider extends BaseProvider {
|
|
9
|
-
name = 'qwen';
|
|
10
|
-
private apiKey: string;
|
|
11
|
-
private model: string;
|
|
12
|
-
private baseUrl = 'https://dashscope.aliyuncs.com/compatible-mode/v1';
|
|
13
|
-
|
|
14
|
-
constructor(apiKey?: string, model = 'qwen3-max') {
|
|
15
|
-
super();
|
|
16
|
-
this.apiKey = apiKey || process.env.DASHSCOPE_API_KEY || process.env.QWEN_API_KEY || '';
|
|
17
|
-
this.model = model;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
isConfigured(): boolean {
|
|
21
|
-
return !!this.apiKey;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async invoke(params: InvokeParams): Promise<InvokeResult> {
|
|
25
|
-
if (!this.isConfigured()) {
|
|
26
|
-
throw new Error('Qwen API key not configured. Set DASHSCOPE_API_KEY or QWEN_API_KEY environment variable.');
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const url = `${this.baseUrl}/chat/completions`;
|
|
30
|
-
|
|
31
|
-
const body: Record<string, unknown> = {
|
|
32
|
-
model: this.model,
|
|
33
|
-
messages: params.messages.map(m => ({ role: m.role, content: m.content })),
|
|
34
|
-
temperature: params.temperature ?? 0.7,
|
|
35
|
-
max_tokens: params.maxTokens ?? 4096,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// Add JSON mode if schema provided
|
|
39
|
-
if (params.jsonSchema) {
|
|
40
|
-
body.response_format = { type: 'json_object' };
|
|
41
|
-
const lastUserIdx = params.messages.findLastIndex(m => m.role === 'user');
|
|
42
|
-
if (lastUserIdx >= 0) {
|
|
43
|
-
const messages = [...params.messages];
|
|
44
|
-
messages[lastUserIdx] = {
|
|
45
|
-
...messages[lastUserIdx],
|
|
46
|
-
content: messages[lastUserIdx].content + this.buildJsonPrompt(params.jsonSchema),
|
|
47
|
-
};
|
|
48
|
-
body.messages = messages.map(m => ({ role: m.role, content: m.content }));
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const response = await fetch(url, {
|
|
53
|
-
method: 'POST',
|
|
54
|
-
headers: {
|
|
55
|
-
'Content-Type': 'application/json',
|
|
56
|
-
'Authorization': `Bearer ${this.apiKey}`,
|
|
57
|
-
},
|
|
58
|
-
body: JSON.stringify(body),
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
if (!response.ok) {
|
|
62
|
-
const error = await response.text();
|
|
63
|
-
throw new Error(`Qwen API error: ${response.status} - ${error}`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const data = await response.json() as {
|
|
67
|
-
choices?: Array<{ message?: { content?: string } }>;
|
|
68
|
-
usage?: { prompt_tokens?: number; completion_tokens?: number; total_tokens?: number };
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const content = data.choices?.[0]?.message?.content || '';
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
content,
|
|
75
|
-
usage: data.usage ? {
|
|
76
|
-
promptTokens: data.usage.prompt_tokens || 0,
|
|
77
|
-
completionTokens: data.usage.completion_tokens || 0,
|
|
78
|
-
totalTokens: data.usage.total_tokens || 0,
|
|
79
|
-
} : undefined,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
}
|