cerevox
Version:
TypeScript SDK for browser automation and secure command execution in highly available and scalable micro computer environments
157 lines • 5.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyToken = applyToken;
exports.uploadFile = uploadFile;
exports.transferToCoze = transferToCoze;
exports.getWorkflowInfo = getWorkflowInfo;
exports.runWorkflow = runWorkflow;
exports.queryWorkflowRunHistory = queryWorkflowRunHistory;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const axios_1 = __importDefault(require("axios"));
const form_data_1 = __importDefault(require("form-data"));
const mime_1 = __importDefault(require("mime"));
const sandbox_1 = require("../core/sandbox");
let tokenBuffer = null;
const BASE_URL = 'https://api.coze.cn';
async function applyToken(apiKey) {
const now = Date.now();
if (tokenBuffer && tokenBuffer.expires_in * 1000 > now) {
return tokenBuffer;
}
// eslint-disable-next-line custom/no-fetch-in-src
const res = await fetch(`https://${sandbox_1.ADMIN_HOST}/coze-token`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
apiKey,
}),
});
tokenBuffer = (await res.json());
return tokenBuffer;
}
async function uploadFile(localFile) {
const buffer = node_fs_1.default.readFileSync(localFile);
const filename = node_path_1.default.basename(localFile);
return transferToCoze(buffer, filename);
}
async function transferToCoze(buffer, filename, apiKey = process.env.CEREVOX_API_KEY) {
const jwtToken = (await applyToken(apiKey)).access_token;
const ext = node_path_1.default.extname(filename);
// 构建 form-data
const form = new form_data_1.default();
form.append('file', buffer, {
filename,
contentType: mime_1.default.getType(ext) || 'application/octet-stream',
});
const baseURL = BASE_URL;
// 提交请求
const response = await axios_1.default.post(`${baseURL}/v1/files/upload`, form, {
headers: {
'Content-Type': 'multipart/form-data',
Authorization: `Bearer ${jwtToken}`,
},
});
const data = response.data;
// console.log({ jwtToken, data });
const file_id = data.data.id;
const workflowApi = 'https://api.coze.cn/v1/workflow/run';
const headers = {
Authorization: `Bearer ${jwtToken}`,
'Content-Type': 'application/json',
};
const body = {
workflow_id: '7559932155398520851',
parameters: {
file: JSON.stringify({ file_id }),
},
};
// eslint-disable-next-line custom/no-fetch-in-src
const ret = await fetch(workflowApi, {
method: 'POST',
headers,
body: JSON.stringify(body),
});
const resData = await ret.json();
if (resData.code > 299) {
throw new Error(resData.msg);
}
const { url } = JSON.parse(resData.data);
return {
url,
};
}
async function getWorkflowInfo(workflowId, apiKey = process.env.CEREVOX_API_KEY) {
const jwtToken = (await applyToken(apiKey)).access_token;
const endpoint = `https://api.coze.cn/v1/workflows/${workflowId}`;
const headers = {
Authorization: `Bearer ${jwtToken}`,
'Content-Type': 'application/json',
};
// eslint-disable-next-line custom/no-fetch-in-src
const ret = await fetch(endpoint, {
method: 'GET',
headers,
});
const resData = await ret.json();
if (resData.code > 299) {
throw new Error(resData.msg);
}
return resData;
}
async function runWorkflow(workflowId, parameters, isAsync = false, apiKey = process.env.CEREVOX_API_KEY) {
const jwtToken = (await applyToken(apiKey)).access_token;
const workflowApi = 'https://api.coze.cn/v1/workflow/run';
const headers = {
Authorization: `Bearer ${jwtToken}`,
'Content-Type': 'application/json',
};
const body = {
workflow_id: workflowId,
parameters,
is_async: isAsync,
};
// eslint-disable-next-line custom/no-fetch-in-src
const ret = await fetch(workflowApi, {
method: 'POST',
headers,
body: JSON.stringify(body),
});
const resData = await ret.json();
if (resData.code > 299) {
throw new Error(resData.msg);
}
return resData;
}
async function queryWorkflowRunHistory(taskUrl, // `https://api.coze.cn/v1/workflows/${workflowId}/run_histories/${executeId}`;
apiKey = process.env.CEREVOX_API_KEY) {
const jwtToken = (await applyToken(apiKey)).access_token;
const headers = {
Authorization: `Bearer ${jwtToken}`,
'Content-Type': 'application/json',
};
// eslint-disable-next-line custom/no-fetch-in-src
const ret = await fetch(taskUrl, {
method: 'GET',
headers,
});
const res = await ret.json();
if (res.code > 299) {
throw new Error(res.msg);
}
const resData = res.data[0];
return {
execute_id: resData.execute_id,
status: resData.execute_status,
data: JSON.parse(resData.output),
log_id: resData.logid,
debug_url: resData.debug_url,
error_msg: resData.error_msg || res.msg,
};
}
//# sourceMappingURL=coze.js.map