66 lines
2.5 KiB
TypeScript
66 lines
2.5 KiB
TypeScript
import { app } from './index.ts';
|
||
|
||
import { useIssueEnv, useCommentEnv, useRepoInfoEnv } from '../src/index.ts'
|
||
import { pick } from 'es-toolkit';
|
||
|
||
const writeToProcess = (message: string) => {
|
||
if (process.send) {
|
||
process.send(message);
|
||
} else {
|
||
console.log(message);
|
||
}
|
||
}
|
||
const main = async () => {
|
||
const repoInfoEnv = useRepoInfoEnv();
|
||
const commentEnv = useCommentEnv();
|
||
const issueEnv = useIssueEnv();
|
||
const pickCommentEnv = pick(commentEnv, ['commentId', 'commentIdLabel']);
|
||
const pickIssueEnv = pick(issueEnv, ['issueId', 'issueIdLabel', 'issueIid', 'issueIidLabel', 'issueTitle', 'issueTitleLabel', 'issueDescription', 'issueDescriptionLabel']);
|
||
const pickRepoInfoEnv = pick(repoInfoEnv, ['repoId', 'repoIdLabel', 'repoName', 'repoNameLabel', 'repoSlug', 'repoSlugLabel']);
|
||
const issueLabels = issueEnv.issueLabels || [];
|
||
const isComment = !!commentEnv.commentId;
|
||
const envList = [
|
||
...Object.entries(pickRepoInfoEnv).map(([key, value]) => `${key}: ${value}`),
|
||
...Object.entries(pickIssueEnv).map(([key, value]) => `${key}: ${value}`),
|
||
...Object.entries(pickCommentEnv).map(([key, value]) => `${key}: ${value}`),
|
||
]
|
||
writeToProcess('当前环境变量:');
|
||
envList.forEach(item => writeToProcess(item));
|
||
if (!isComment && !issueLabels.includes('Run')) {
|
||
writeToProcess('当前 Issue 不包含 Run 标签,跳过执行');
|
||
process.exit(0);
|
||
}
|
||
const messages = [
|
||
{
|
||
role: 'system',
|
||
content: `你是一个智能的代码助手, 根据用户提供的上下文信息,提供有用的建议和帮助, 如果用户的要求和执行工具不一致,请说出你不能这么做。并把最后的结果提交一个评论到对应的issue中,提交的内容必须不能包含 @ 提及。用户提供的上下文信息如下:`
|
||
},
|
||
{
|
||
role: 'system',
|
||
content: `相关变量:${JSON.stringify({ ...pickCommentEnv, ...pickIssueEnv, ...pickRepoInfoEnv })}`
|
||
}, {
|
||
role: 'user',
|
||
content: commentEnv.commentBody || pickIssueEnv.issueDescription || '无'
|
||
}
|
||
]
|
||
writeToProcess('输入消息:');
|
||
writeToProcess(JSON.stringify(messages, null, 2));
|
||
const result = await app.run({
|
||
path: 'cnb',
|
||
key: 'chat',
|
||
payload: {
|
||
messages
|
||
}
|
||
}, { appId: app.appId })
|
||
if (result.code === 200) {
|
||
let _message = result.data.message || []
|
||
writeToProcess('执行完成')
|
||
writeToProcess(JSON.stringify(_message, null, 2))
|
||
process.exit(0)
|
||
} else {
|
||
writeToProcess(result.message || '执行错误')
|
||
process.exit(1)
|
||
}
|
||
}
|
||
|
||
main(); |