(function(Scratch) {
'use strict';
class GLMChat {
constructor() {
this.apiKey = '';
this.currentModel = 'glm-4-flash';
this.webSearchEnabled = false;
this.lastResponse = '';
this.lastThinkingResult = '';
this.lastThinkingTime = 0;
this.isProcessing = false;
this.modelMap = {
'GLM-4-Flash': 'glm-4-flash',
'GLM-4-Long': 'glm-4-long',
'GLM-4-Air': 'glm-4-air',
'GLM-4-AirX': 'glm-4-airx',
'GLM-4-Plus': 'glm-4-plus',
'GLM-4-0520': 'glm-4-0520',
'GLM-4-0520-实验版': 'glm-4-0520-experimental',
'GLM-4V': 'glm-4v',
'GLM-4V-Plus': 'glm-4v-plus',
'GLM-4V-0520': 'glm-4v-0520'
};
this.colors = {
primary: '#2D5CF6',
secondary: '#1A3FD9',
accent: '#5B82F8'
};
}
getInfo() {
return {
id: 'kjcxglm',
name: '慕然科技的智谱清言',
color1: this.colors.primary,
color2: this.colors.secondary,
color3: this.colors.accent,
docsURI: 'https://open.bigmodel.cn/',
blocks: [
{
opcode: 'setApiKey',
blockType: Scratch.BlockType.COMMAND,
text: '设置API密钥 [KEY]',
arguments: {
KEY: {
type: Scratch.ArgumentType.STRING,
defaultValue: ''
}
}
},
{
opcode: 'setModel',
blockType: Scratch.BlockType.COMMAND,
text: '使用模型 [MODEL]',
arguments: {
MODEL: {
type: Scratch.ArgumentType.STRING,
menu: 'models'
}
}
},
{
opcode: 'setWebSearch',
blockType: Scratch.BlockType.COMMAND,
text: '联网搜索 [ENABLED]',
arguments: {
ENABLED: {
type: Scratch.ArgumentType.STRING,
menu: 'switch'
}
}
},
'---',
{
opcode: 'getThinkingResult',
blockType: Scratch.BlockType.REPORTER,
text: '思考结果',
disableMonitor: true
},
{
opcode: 'getThinkingTime',
blockType: Scratch.BlockType.REPORTER,
text: '思考用时(秒)',
disableMonitor: true
},
{
opcode: 'getLastOutput',
blockType: Scratch.BlockType.REPORTER,
text: '输出结果',
disableMonitor: true
},
{
opcode: 'getCurrentModel',
blockType: Scratch.BlockType.REPORTER,
text: '当前模型',
disableMonitor: true
},
'---',
{
opcode: 'sendMessage',
blockType: Scratch.BlockType.REPORTER,
text: '发送消息 [MESSAGE]',
arguments: {
MESSAGE: {
type: Scratch.ArgumentType.STRING,
defaultValue: '你好!请介绍一下你自己'
}
}
},
{
opcode: 'reset',
blockType: Scratch.BlockType.COMMAND,
text: '重置对话'
}
],
menus: {
models: {
acceptReporters: false,
items: [
'GLM-4-Flash',
'GLM-4-Long',
'GLM-4-Air',
'GLM-4-AirX',
'GLM-4-Plus',
'GLM-4-0520',
'GLM-4-0520-实验版',
'GLM-4V',
'GLM-4V-Plus',
'GLM-4V-0520'
]
},
switch: {
acceptReporters: false,
items: ['打开', '关闭']
}
}
};
}
setApiKey(args) {
const key = args.KEY.trim();
if (key) {
this.apiKey = key;
console.log('API密钥已设置');
}
}
setModel(args) {
const modelKey = args.MODEL;
this.currentModel = this.modelMap[modelKey] || this.modelMap['GLM-4-Flash'];
console.log(`模型已设置为: ${modelKey} (${this.currentModel})`);
}
setWebSearch(args) {
this.webSearchEnabled = (args.ENABLED === '打开');
console.log(`联网搜索: ${this.webSearchEnabled ? '开启' : '关闭'}`);
}
getThinkingResult() {
if (!this.lastThinkingResult) return '暂无思考结果';
return this.lastThinkingResult;
}
getThinkingTime() {
if (this.lastThinkingTime === 0) return '0';
return (Math.round(this.lastThinkingTime / 100) / 10).toString();
}
getLastOutput() {
return this.lastResponse || '暂无输出结果';
}
getCurrentModel() {
for (const [displayName, modelId] of Object.entries(this.modelMap)) {
if (modelId === this.currentModel) {
return displayName;
}
}
return this.currentModel;
}
reset() {
this.lastResponse = '';
this.lastThinkingResult = '';
this.lastThinkingTime = 0;
this.isProcessing = false;
console.log('对话已重置');
}
sendMessage(args) {
if (this.isProcessing) {
return Promise.resolve('错误:上一个请求正在处理中,请稍后再试');
}
if (!this.apiKey) {
return Promise.resolve('错误:请先设置API密钥');
}
const message = args.MESSAGE.trim();
if (!message) {
return Promise.resolve('错误:消息内容不能为空');
}
this.isProcessing = true;
const requestData = {
model: this.currentModel,
messages: [
{
role: "user",
content: message
}
],
stream: false
};
if (this.webSearchEnabled) {
requestData.tools = [
{
type: "web_search",
web_search: {
enable: true,
search_result: true
}
}
];
}
const startTime = Date.now();
return fetch('https://open.bigmodel.cn/api/paas/v4/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + this.apiKey
},
body: JSON.stringify(requestData)
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP错误: ${response.status}`);
}
return response.json();
})
.then(data => {
const endTime = Date.now();
this.lastThinkingTime = endTime - startTime;
if (data.error) {
throw new Error(data.error.message || 'API返回错误');
}
if (data.choices && data.choices.length > 0) {
const message = data.choices[0].message;
if (Array.isArray(message.content)) {
let textContent = '';
for (const item of message.content) {
if (item.type === 'text') {
textContent += item.text;
}
}
this.lastResponse = textContent;
} else {
this.lastResponse = message.content;
}
let thinkingInfo = '';
if (data.usage) {
thinkingInfo = `Tokens: 输入${data.usage.prompt_tokens} 输出${data.usage.completion_tokens} 总计${data.usage.total_tokens}`;
}
if (data.choices[0].finish_reason) {
thinkingInfo += thinkingInfo ? ` | 完成原因: ${data.choices[0].finish_reason}` : `完成原因: ${data.choices[0].finish_reason}`;
}
this.lastThinkingResult = thinkingInfo || '无元数据信息';
return this.lastResponse;
} else {
throw new Error('API返回数据异常');
}
})
.catch(error => {
console.error('智谱清言API错误:', error);
this.lastResponse = '';
this.lastThinkingResult = '';
return `错误:${error.message}`;
})
.finally(() => {
this.isProcessing = false;
});
}
}
Scratch.extensions.register(new GLMChat());
})(Scratch);