This commit is contained in:
2026-04-25 16:36:34 +08:00
commit db90e7579b
1876 changed files with 189777 additions and 0 deletions

1
apps/README.md Normal file
View File

@@ -0,0 +1 @@
用于存放非Tailchat核心功能的应用

106
apps/cli/.gitignore vendored Normal file
View File

@@ -0,0 +1,106 @@
lib
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port

2
apps/cli/.npmrc Normal file
View File

@@ -0,0 +1,2 @@
# https://npmmirror.com/
registry = https://registry.npmmirror.com

21
apps/cli/LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 MsgByte
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

16
apps/cli/README.md Normal file
View File

@@ -0,0 +1,16 @@
# tailchat-cli
A Command line interface of tailchat
```bash
tailchat <command>
Commands:
tailchat create [template] 创建 Tailchat 项目代码
tailchat connect 连接到 Tailchat 节点网络
tailchat bench 压力测试
tailchat declaration <source> Tailchat 插件类型声明
Options:
--version Show version number [boolean]
-h, --help Show help [boolean]
```

3
apps/cli/bin/cli Executable file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/env node
require('../lib')

87
apps/cli/package.json Normal file
View File

@@ -0,0 +1,87 @@
{
"name": "tailchat-cli",
"version": "1.5.14",
"description": "A Command line interface of tailchat",
"bin": {
"tailchat": "./bin/cli"
},
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"files": [
"lib",
"bin",
"templates"
],
"scripts": {
"dev": "cross-env NODE_ENV=development ts-node ./src/index.ts",
"build": "rimraf -rf lib && tsc",
"prepare": "npm run build",
"release": "npm publish --registry https://registry.npmjs.com/",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/msgbyte/tailchat-cli.git"
},
"keywords": [
"tailchat"
],
"author": "moonrailgun",
"license": "MIT",
"bugs": {
"url": "https://github.com/msgbyte/tailchat-cli/issues"
},
"homepage": "https://github.com/msgbyte/tailchat-cli#readme",
"dependencies": {
"@types/dockerode": "^3.3.10",
"@types/pidusage": "^2.0.2",
"as-table": "^1.0.55",
"chalk": "4.1.2",
"crypto-random-string": "3.3.1",
"dockerode": "^3.3.4",
"dotenv": "^16.0.0",
"filesize": "^8.0.7",
"find-process": "^1.4.7",
"fs-extra": "^10.1.0",
"glob": "^8.1.0",
"got": "11.8.5",
"ink": "^3.2.0",
"ink-tab": "^4.2.0",
"ink-text-input": "^4.0.3",
"inquirer": "^8.2.2",
"lodash": "^4.17.21",
"node-plop": "^0.26.3",
"nodemailer": "^6.7.2",
"ora": "5.4.1",
"p-all": "2.1.0",
"p-map": "^4.0.0",
"p-series": "2.1.0",
"pidusage": "^3.0.2",
"plop": "^3.0.5",
"pretty-ms": "7.0.1",
"react": "18.2.0",
"rimraf": "^3.0.2",
"socket.io-client": "^4.6.2",
"socket.io-msgpack-parser": "^3.0.2",
"spinnies": "^0.5.1",
"tailchat-server-sdk": "^0.0.12",
"update-notifier": "5.1.0",
"yargs": "^17.4.0"
},
"devDependencies": {
"@types/fs-extra": "^9.0.13",
"@types/glob": "^8.0.0",
"@types/inquirer": "^8.2.1",
"@types/lodash": "^4.14.170",
"@types/node": "^18.13.0",
"@types/nodemailer": "^6.4.4",
"@types/react": "18.0.20",
"@types/spinnies": "^0.5.0",
"@types/update-notifier": "^6.0.1",
"@types/yargs": "^17.0.10",
"cross-env": "^7.0.3",
"tailchat-shared": "workspace:*",
"ts-node": "^10.7.0",
"typescript": "^4.6.3"
}
}

46
apps/cli/src/app/App.tsx Normal file
View File

@@ -0,0 +1,46 @@
import React, { useLayoutEffect, useState } from 'react';
import { Box, Text, useStdout } from 'ink';
import { Tabs, Tab } from 'ink-tab';
import TextInput from 'ink-text-input';
import { useScreenSize } from './hooks/useScreenSize';
export const App: React.FC = React.memo(() => {
const [text, setText] = useState('');
const { height, width } = useScreenSize();
const { stdout } = useStdout();
useLayoutEffect(() => {
stdout?.write('\x1b[?1049h');
return () => {
stdout?.write('\x1b[?1049l');
};
}, [stdout]);
return (
<Box
height={height}
width={width}
borderStyle="round"
borderColor="green"
flexDirection="column"
>
<Box>
<TextInput value={text} onChange={setText} />
</Box>
<Box>
<Tabs flexDirection="column" onChange={() => {}}>
{/* Temporary comments due to react version issues */}
{/* <Tab name="tab1">
<Text>Foo</Text>
</Tab>
<Tab name="tab2">
<Text>Bar</Text>
</Tab> */}
</Tabs>
</Box>
</Box>
);
});
App.displayName = 'App';

View File

@@ -0,0 +1,25 @@
import { useCallback, useEffect, useState } from 'react';
import { useStdout } from 'ink';
export function useScreenSize() {
const { stdout } = useStdout();
const getSize = useCallback(
() => ({
height: stdout?.rows ?? 0,
width: stdout?.columns ?? 0,
}),
[stdout]
);
const [size, setSize] = useState(getSize);
useEffect(() => {
const onResize = () => setSize(getSize());
stdout?.on('resize', onResize);
return () => {
stdout?.off('resize', onResize);
};
}, [stdout, getSize]);
return size;
}

View File

@@ -0,0 +1,9 @@
import React from 'react';
import { App } from './App';
import { render } from 'ink';
export function run() {
const ins = render(<App />);
return ins;
}

View File

@@ -0,0 +1,12 @@
import { CommandModule } from 'yargs';
import { run } from '../app';
import { isDev } from '../utils';
export const appCommand: CommandModule = {
command: 'app',
describe: isDev() ? false : 'Tailchat cli(WIP)',
builder: undefined,
async handler() {
await run();
},
};

View File

@@ -0,0 +1,173 @@
import { CommandModule } from 'yargs';
import { io, Socket } from 'socket.io-client';
import msgpackParser from 'socket.io-msgpack-parser';
import fs from 'fs-extra';
import ora from 'ora';
import randomString from 'crypto-random-string';
import pMap from 'p-map';
const CLIENT_CREATION_INTERVAL_IN_MS = 5;
export const benchmarkConnectionsCommand: CommandModule = {
command: 'connections <url>',
describe: 'Test Tailchat Connections',
builder: (yargs) =>
yargs
.demandOption('url', 'Backend Url')
.option('file', {
describe: 'Account Token Path',
demandOption: true,
type: 'string',
default: './accounts',
})
.option('concurrency', {
describe: 'Concurrency when create connection',
type: 'number',
default: 1,
})
.option('groupId', {
describe: 'Group Id which send Message',
type: 'string',
})
.option('converseId', {
describe: 'Converse Id which send Message',
type: 'string',
})
.option('messageNum', {
describe: 'Times which send Message',
type: 'number',
default: 1,
}),
async handler(args) {
const url = args.url as string;
const file = args.file as string;
const groupId = args.groupId as string;
const converseId = args.converseId as string;
const messageNum = args.messageNum as number;
const concurrency = args.concurrency as number;
console.log('Reading account tokens from', file);
const account = await fs.readFile(file as string, {
encoding: 'utf8',
});
const sockets = await createClients(
url as string,
account.split('\n').map((s) => s.trim()),
concurrency
);
if (groupId && converseId) {
// send message test
for (let i = 0; i < messageNum; i++) {
console.log('Start send message test:', i + 1);
await sendMessage(sockets, groupId, converseId);
}
}
},
};
async function createClients(
url: string,
accountTokens: string[],
concurrency: number
): Promise<Socket[]> {
const maxCount = accountTokens.length;
const spinner = ora().info(`Create Client Connection to ${url}`).start();
let i = 0;
const sockets: Socket[] = [];
await pMap(
accountTokens,
async (token) => {
await sleep(CLIENT_CREATION_INTERVAL_IN_MS);
const socket = await createClient(url, token);
spinner.text = `Progress: ${++i}/${maxCount}`;
sockets.push(socket);
},
{
concurrency,
}
);
spinner.succeed(`${maxCount} clients has been create.`);
return sockets;
}
function createClient(url: string, token: string): Promise<Socket> {
return new Promise<Socket>((resolve, reject) => {
const socket = io(url, {
transports: ['websocket'],
auth: {
token,
},
forceNew: true,
parser: msgpackParser,
});
socket.once('connect', () => {
// 连接成功
resolve(socket);
});
socket.once('error', () => {
reject();
});
socket.on('disconnect', (reason) => {
console.log(`disconnect due to ${reason}`);
});
}).then(async (socket) => {
await socket.emitWithAck('chat.converse.findAndJoinRoom', {});
return socket;
});
}
async function sendMessage(
sockets: Socket[],
groupId: string,
converseId: string
) {
return new Promise<void>((resolve) => {
const randomMessage = randomString({ length: 16 });
const spinner = ora()
.info(`Start message receive test, message: ${randomMessage}`)
.start();
const start = Date.now();
let receiveCount = 0;
const len = sockets.length;
function receivedCallback() {
receiveCount += 1;
spinner.text = `Receive: ${receiveCount}/${len}`;
if (receiveCount === len) {
spinner.succeed(`All client received, usage: ${Date.now() - start}ms`);
resolve();
}
}
sockets.forEach((socket) => {
socket.on('notify:chat.message.add', (message) => {
const content = message.content;
if (message.converseId === converseId && randomMessage === content) {
socket.off('notify:chat.message.add');
receivedCallback();
}
});
});
sockets[0].emit('chat.message.sendMessage', {
groupId,
converseId,
content: randomMessage,
});
});
}
function sleep(milliseconds: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
}

View File

@@ -0,0 +1,18 @@
import { CommandModule } from 'yargs';
import { benchmarkConnectionsCommand } from './connections';
import { benchmarkMessageCommand } from './message';
import { benchmarkRegisterCommand } from './register';
// https://docs.docker.com/engine/api/v1.41/
export const benchmarkCommand: CommandModule = {
command: 'benchmark',
describe: 'Tailchat Benchmark Test',
builder: (yargs) =>
yargs
.command(benchmarkMessageCommand)
.command(benchmarkConnectionsCommand)
.command(benchmarkRegisterCommand)
.demandCommand(),
handler(args) {},
};

View File

@@ -0,0 +1,178 @@
import { CommandModule } from 'yargs';
import { TcBroker } from 'tailchat-server-sdk';
import defaultBrokerConfig from 'tailchat-server-sdk/dist/runner/moleculer.config';
import { config } from 'dotenv';
import _ from 'lodash';
import os from 'os';
import pAll from 'p-all';
import pSeries from 'p-series';
import ora from 'ora';
import prettyMs from 'pretty-ms';
import filesize from 'filesize';
export const benchmarkMessageCommand: CommandModule = {
command: 'message',
describe:
'Stress testing through Tailchat network requests (suitable for pure business testing)',
builder: (yargs) =>
yargs
.option('groupId', {
describe: 'Group ID',
demandOption: true,
type: 'string',
})
.option('converseId', {
describe: 'Converse ID',
demandOption: true,
type: 'string',
})
.option('userId', {
describe: 'User ID',
demandOption: true,
type: 'string',
})
.option('num', {
describe: 'Test Num',
type: 'number',
default: 100,
})
.option('parallel', {
describe: 'Is Parallel',
type: 'boolean',
default: false,
})
.option('parallelLimit', {
describe: 'Parallel Limit',
type: 'number',
default: Infinity,
}),
async handler(args) {
config(); // 加载环境变量
const broker = new TcBroker({
...defaultBrokerConfig,
transporter: process.env.TRANSPORTER,
logger: false,
});
await broker.start();
printSystemInfo();
console.log('===============');
await startBenchmark<number>({
parallel: args.parallel as boolean,
parallelLimit: args.parallelLimit as number,
number: args.num as number,
task: async (i) => {
const start = process.hrtime();
await broker.call(
'chat.message.sendMessage',
{
converseId: args.converseId,
groupId: args.groupId,
content: `benchmessage ${i + 1}`,
},
{
meta: {
userId: args.userId,
},
}
);
const usage = calcUsage(start);
return usage;
},
onCompleted: (res) => {
console.log(`Test Num: \t${res.length}`);
console.log(`Max Usage: \t${prettyMs(Math.max(...res, 0))}`);
console.log(`Min Usage: \t${prettyMs(Math.min(...res, 0))}`);
console.log(`Average time: \t${prettyMs(_.mean(res))}`);
},
});
await broker.stop();
},
};
/**
* 打印系统信息
*/
function printSystemInfo() {
console.log(`Host: \t${os.hostname()}`);
console.log(`System: \t${os.type()} - ${os.release()}`);
console.log(`Architecture: \t${os.arch()} - ${os.version()}`);
console.log(`CPU: \t${os.cpus().length}`);
console.log(`Memory: \t${filesize(os.totalmem(), { base: 2 })}`);
}
function calcUsage(startTime: [number, number]) {
const diff = process.hrtime(startTime);
const usage = (diff[0] + diff[1] / 1e9) * 1000;
return usage;
}
interface BenchmarkOptions<T> {
parallel: boolean; // 是否并发
parallelLimit?: number; // 并发上限, 默认不限制(Infinity)
task: (index: number) => Promise<T>;
number?: number;
onCompleted: (res: T[]) => void;
}
/**
* 开始一次基准测试
*/
async function startBenchmark<T>(options: BenchmarkOptions<T>) {
const {
parallel,
parallelLimit = Infinity,
task,
number = 100,
onCompleted,
} = options;
const spinner = ora();
spinner.info(
`Test mode: ${
parallel ? `parallel, parallel limit ${parallelLimit}` : `serial`
}`
);
spinner.info(`Number of tasks to execute: ${number}`);
spinner.start('Benchmark in progress...');
try {
const startTime = process.hrtime();
let res: (T | false)[] = [];
if (parallel) {
res = await pAll<T | false>(
[
...Array.from({ length: number }).map(
(_, i) => () => task(i).catch(() => false as const)
),
],
{
concurrency: parallelLimit,
}
);
} else {
res = await pSeries<T | false>([
...Array.from({ length: number }).map(
(_, i) => () => task(i).catch(() => false as const)
),
]);
}
const allUsage = calcUsage(startTime);
const succeed = res.filter((i): i is T => Boolean(i));
const failed = res.filter((i) => !Boolean(i));
spinner.succeed(`Benchmarking is complete, usage: ${prettyMs(allUsage)}`);
console.log(`Success/Failed: ${succeed.length}/${failed.length}`);
console.log(`TPS: ${res.length / (allUsage / 1000)}`);
onCompleted(succeed);
} catch (err) {
console.error(err);
spinner.fail(`A problem with benchmarking`).stop();
}
}

View File

@@ -0,0 +1,116 @@
import { CommandModule } from 'yargs';
import fs from 'fs-extra';
import got from 'got';
import ora from 'ora';
import pMap from 'p-map';
export const benchmarkRegisterCommand: CommandModule = {
command: 'register <url>',
describe: 'Create Tailchat temporary account and output token',
builder: (yargs) =>
yargs
.example(
'$0 benchmark register http://127.0.0.1:11000',
'Register account in local server'
)
.demandOption('url', 'Backend Url')
.option('file', {
describe: 'Account Token Path',
demandOption: true,
type: 'string',
default: './accounts',
})
.option('count', {
describe: 'Register Count',
demandOption: true,
type: 'number',
default: 100,
})
.option('concurrency', {
describe: 'Concurrency when send request',
type: 'number',
default: 1,
})
.option('invite', {
describe: 'Invite Code',
type: 'string',
})
.option('append', {
describe: 'Append mode',
type: 'boolean',
}),
async handler(args) {
const url = args.url as string;
const file = args.file as string;
const count = args.count as number;
const concurrency = args.concurrency as number;
const invite = args.invite as string | undefined;
const append = (args.append ?? false) as boolean;
const tokens: string[] = [];
const start = Date.now();
const spinner = ora().info(`Register temporary account`).start();
let i = 0;
spinner.text = `Progress: ${i}/${count}`;
await pMap(
Array.from({ length: count }),
async () => {
const token = await registerTemporaryAccount(url, `benchUser-${i}`);
if (invite) {
// Apply group invite
await applyGroupInviteCode(url, token, invite);
}
if (append) {
await fs.appendFile(file, `\n${token}`);
}
spinner.text = `Progress: ${++i}/${count}`;
tokens.push(token);
},
{
concurrency,
}
);
if (!append) {
spinner.info(`Writing tokens into path: ${file}`);
await fs.writeFile(file, tokens.join('\n'));
}
spinner.succeed(`Register completed! Usage: ${Date.now() - start}ms`);
},
};
async function registerTemporaryAccount(
url: string,
nickname: string
): Promise<string> {
const res = await got
.post(`${url}/api/user/createTemporaryUser`, {
json: {
nickname,
},
retry: 5,
})
.json<{ data: { token: string } }>();
return res.data.token;
}
async function applyGroupInviteCode(
url: string,
token: string,
inviteCode: string
) {
await got.post(`${url}/api/group/invite/applyInvite`, {
json: {
code: inviteCode,
},
retry: 5,
headers: {
'X-Token': token,
},
});
}

View File

@@ -0,0 +1,20 @@
import { CommandModule } from 'yargs';
import { TcBroker } from 'tailchat-server-sdk';
import defaultBrokerConfig from 'tailchat-server-sdk/dist/runner/moleculer.config';
import { config } from 'dotenv';
export const connectCommand: CommandModule = {
command: 'connect',
describe: 'Connect to Tailchat network',
builder: undefined,
async handler(args) {
config();
const broker = new TcBroker({
...defaultBrokerConfig,
transporter: process.env.TRANSPORTER,
});
await broker.start();
broker.repl();
},
};

View File

@@ -0,0 +1,51 @@
import { CommandModule } from 'yargs';
import nodePlop from 'node-plop';
import path from 'path';
import inquirer from 'inquirer';
const plop = nodePlop(path.resolve(__dirname, '../../templates/plopfile.js'));
export const createCommand: CommandModule = {
command: 'create [template]',
describe: 'Create Tailchat repo code',
builder: (yargs) =>
yargs.positional('template', {
demandOption: true,
description: 'Template Name',
type: 'string',
choices: plop.getGeneratorList().map((v) => v.name),
}),
async handler(args) {
let template: string;
if (!args.template) {
const res = await inquirer.prompt([
{
type: 'list',
name: 'template',
message: 'Choose Template',
choices: plop.getGeneratorList().map((v) => ({
name: `${v.name} (${v.description})`,
value: v.name,
})),
},
]);
template = String(res.template);
} else {
template = String(args.template);
}
const basic = plop.getGenerator(template);
const answers = await basic.runPrompts();
const results = await basic.runActions(answers);
console.log('Changes:');
console.log(results.changes.map((change) => change.path).join('\n'));
if (results.failures.length > 0) {
console.log('Operation failed:');
console.log(results.failures);
}
},
};

View File

@@ -0,0 +1,67 @@
import inquirer from 'inquirer';
import { CommandModule } from 'yargs';
import fs, { mkdirp } from 'fs-extra';
import path from 'path';
import ora from 'ora';
import got from 'got';
const onlineDeclarationUrl =
'https://raw.githubusercontent.com/msgbyte/tailchat/master/client/web/tailchat.d.ts';
export const declarationCommand: CommandModule = {
command: 'declaration <source>',
describe: 'Tailchat plugin type declaration',
builder: (yargs) =>
yargs.positional('source', {
demandOption: true,
description: 'Declaration Type Source',
type: 'string',
choices: ['empty', 'github'],
}),
async handler(args) {
let source = String(args.source);
if (!source) {
const res = await inquirer.prompt([
{
type: 'list',
name: 'source',
message: 'Select type source',
choices: [
{
name: 'Empty',
value: 'empty',
},
{
name: 'Download the full statement from Github',
value: 'github',
},
],
},
]);
source = String(res.source);
}
let content = '';
if (source === 'empty') {
content =
"declare module '@capital/common';\ndeclare module '@capital/component';\n";
} else if (source === 'github') {
const url = onlineDeclarationUrl;
const spinner = ora(
`Downloading plugin type declarations from Github: ${url}`
).start();
content = await got.get(url).then((res) => res.body);
spinner.succeed('The declaration file has been downloaded');
}
if (content !== '') {
const target = path.resolve(process.cwd(), './types/tailchat.d.ts');
await mkdirp(path.dirname(target));
await fs.writeFile(target, content);
console.log('Writing type file complete:', target);
}
},
};

View File

@@ -0,0 +1,32 @@
import { CommandModule } from 'yargs';
import Docker from 'dockerode';
import asTable from 'as-table';
import filesize from 'filesize';
export const dockerDoctorCommand: CommandModule = {
command: 'doctor',
describe: 'docker environment check',
builder: undefined,
async handler(args) {
const docker = new Docker();
const images = await docker.listImages();
const tailchatImages = images.filter((image) =>
(image.RepoTags ?? []).some((tag) => tag.includes('tailchat'))
);
console.log('Tailchat image list:');
console.log(
asTable.configure({ delimiter: ' | ' })(
tailchatImages.map((image) => ({
tag: (image.RepoTags ?? []).join(','),
id: image.Id.substring(0, 12),
size: filesize(image.Size),
}))
)
);
const info = await docker.info();
console.log('info', info);
},
};

View File

@@ -0,0 +1,18 @@
import { CommandModule } from 'yargs';
import { dockerInitCommand } from './init';
import { dockerUpdateCommand } from './update';
import { dockerDoctorCommand } from './doctor';
// https://docs.docker.com/engine/api/v1.41/
export const dockerCommand: CommandModule = {
command: 'docker',
describe: 'Tailchat image management',
builder: (yargs) =>
yargs
.command(dockerInitCommand)
.command(dockerDoctorCommand)
.command(dockerUpdateCommand)
.demandCommand(),
handler(args) {},
};

View File

@@ -0,0 +1,185 @@
import { CommandModule } from 'yargs';
import ora from 'ora';
import fs from 'fs-extra';
import got from 'got';
import path from 'path';
import chalk from 'chalk';
import inquirer from 'inquirer';
import randomString from 'crypto-random-string';
import { withGhProxy } from '../../utils';
// https://docs.docker.com/engine/api/v1.41/
const initWelcome = `================
Initializing Tailchat configuration and environment variables for you
A complete list of environment variables can be accessed at: ${chalk.underline(
'https://tailchat.msgbyte.com/docs/deployment/environment'
)} to learn more
================`;
const initCompleted = (dir: string) =>
chalk.green(`================
Congratulations, you have successfully completed the configuration initialization, your configuration file is ready, and you are one step away from a successful deployment!
Your tailchat configuration files are stored in: ${chalk.underline(
path.resolve(process.cwd(), dir)
)}
Run the following command to complete the image download and start:
- ${chalk.bold(`cd ${dir}`)} ${chalk.gray(
'# Move to the installation directory'
)}
- ${chalk.bold('tailchat docker update')} ${chalk.gray(
'# Download/update the official mirror'
)}
- ${chalk.bold('docker compose up -d')} ${chalk.gray('# Start service')}
================`);
const envUrl =
'https://raw.githubusercontent.com/msgbyte/tailchat/master/docker-compose.env';
const configUrl =
'https://raw.githubusercontent.com/msgbyte/tailchat/master/docker-compose.yml';
export const dockerInitCommand: CommandModule = {
command: 'init',
describe: 'Initialize Tailchat with docker configuration',
builder: undefined,
async handler(args) {
const spinner = ora();
try {
console.log(initWelcome);
const { dir, secret, apiUrl, fileLimit } = await inquirer.prompt([
{
name: 'dir',
type: 'input',
default: './tailchat',
message: 'Configurate storage directory',
},
{
name: 'secret',
type: 'input',
default: randomString({ length: 16 }),
message:
'(SECRET)Please enter any string, which will be used as the secret key for Tailchat to sign the user identity. Leaking this string will cause the risk of identity forgery. By default, a 16-digit string is randomly generated',
},
{
name: 'apiUrl',
type: 'input',
message:
'(API_URL)Please configure an address that can be accessed by the external network, which will affect the storage path and access address of the file, example: https://tailchat.example.com',
},
{
name: 'fileLimit',
type: 'number',
default: 1048576,
message:
'(FILE_LIMIT)File upload volume limit, the default is 1048576(1m)',
},
]);
spinner.start('Start downloading the latest configuration file');
// eslint-disable-next-line prefer-const
let [rawEnv, rawConfig] = await Promise.all([
got(envUrl).then((res) => res.body),
got(configUrl).then((res) => res.body),
]);
spinner.info('The configuration file is downloaded');
if (secret) {
rawEnv = setEnvValue(rawEnv, 'SECRET', secret);
}
if (apiUrl) {
rawEnv = setEnvValue(rawEnv, 'API_URL', apiUrl);
}
if (fileLimit) {
rawEnv = setEnvValue(rawEnv, 'FILE_LIMIT', fileLimit);
}
if (
await promptConfirm(
'Do you need to configure the email service? The email service can be used for functions such as password retrieval and email notification.'
)
) {
const { stmpURI, stmpSender } = await inquirer.prompt([
{
name: 'stmpURI',
type: 'input',
message:
'(SMTP_URI) Please configure the SMTP connection address of the mail service, for example: smtps://username:password@example.mailserver.com/?pool=true',
},
{
name: 'stmpSender',
type: 'input',
message:
'(SMTP_SENDER) Email sender, example: "Tailchat" tailchat@example.mailserver.com',
},
]);
if (stmpURI) {
rawEnv = setEnvValue(rawEnv, 'SMTP_URI', stmpURI);
}
if (stmpSender) {
rawEnv = setEnvValue(rawEnv, 'SMTP_SENDER', stmpSender);
}
if (stmpURI && stmpSender) {
if (
await promptConfirm(
'Do you need to enable email verification? After it is enabled, when the user registers, it is necessary to verify the email address and pass it before continuing to register'
)
) {
rawEnv = setEnvValue(rawEnv, 'EMAIL_VERIFY', 'true');
}
}
}
spinner.info(`Creating directory ${dir} ...`);
await fs.mkdirp(dir);
spinner.info('Writing configuration file ...');
await Promise.all([
fs.writeFile(path.join(dir, 'docker-compose.env'), rawEnv),
fs.writeFile(path.join(dir, 'docker-compose.yml'), rawConfig),
]);
spinner.succeed('The configuration is initialized');
console.log(initCompleted(dir));
} catch (err) {
spinner.fail('Unexpected initialization of Tailchat with docker');
console.error(err);
}
},
};
/**
* 设置环境变量值
*/
function setEnvValue(text: string, key: string, value: string): string {
const re = new RegExp(`${key}=(.*?)\n`);
if (re.test(text)) {
// 配置文件已经有了
return text.replace(re, `${key}=${value}\n`);
}
// 配置文件还没有
return text + `\n${key}=${value}\n`;
}
/**
* 设置更多配置的确认项
*/
async function promptConfirm(message: string): Promise<boolean> {
const { res } = await inquirer.prompt([
{
name: 'res',
type: 'confirm',
message,
default: false,
},
]);
return res;
}

View File

@@ -0,0 +1,108 @@
import { CommandModule } from 'yargs';
import ora from 'ora';
import Docker from 'dockerode';
import Spinnies from 'spinnies';
const remoteImageName = 'moonrailgun/tailchat:latest';
const targetImage = {
repo: 'tailchat',
tag: 'latest',
};
const targetImageName = targetImage.repo + targetImage.tag;
export const dockerUpdateCommand: CommandModule = {
command: 'update',
describe: 'Update Tailchat image version',
builder: undefined,
async handler(args) {
const docker = new Docker();
const spinner = ora().start('Start updating the image');
try {
const pullSpinnies = new Spinnies();
await new Promise<void>((resolve, reject) => {
const taskMap = new Map<string, Spinnies.SpinnerOptions>();
// 这里有个类型问题,不会返回任何值
docker.pull(remoteImageName, {}, (err, stream) => {
if (err) {
reject(err);
return;
}
spinner.info('The remote image has been found, start downloading');
docker.modem.followProgress(
stream,
(err, output) => {
// onFinish
// console.log('finish', err, output);
if (err) {
pullSpinnies.stopAll('stopped');
reject(err);
} else {
pullSpinnies.stopAll('succeed');
spinner.succeed(output[1]?.status);
spinner.succeed(output[2]?.status);
resolve();
}
},
(event) => {
if (!event.id) {
console.log(event.status); // 可能是完成后的信息打印,直接输出
return;
}
const text = `[${event.id}] ${event.status}${
event.progress ? ':' : ''
} ${event.progress ?? ''}`;
// onProcess
if (taskMap.has(event.id)) {
pullSpinnies.update(event.id, {
text,
});
if (event.status === 'Pull complete') {
pullSpinnies.succeed(event.id);
}
} else {
taskMap.set(
event.id,
pullSpinnies.add(event.id, {
text,
})
);
}
}
);
});
});
spinner.succeed('Download image complete');
const image = docker.getImage(remoteImageName);
if (!image) {
spinner.fail(
'An exception occurred, the downloaded image was not found'
);
return;
}
spinner.info('Updating image tags');
await image.tag(targetImage);
spinner.succeed('The image tag has been updated');
} catch (err) {
spinner.fail(
'An update error occurred, please check the network configuration'
);
console.error(err);
}
},
};

View File

@@ -0,0 +1,62 @@
import { CommandModule } from 'yargs';
import path from 'path';
import glob from 'glob';
import inquirer from 'inquirer';
import fs from 'fs-extra';
const feRegistryPath = path.join(process.cwd(), './client/web/registry.json');
export const registryConfigCommand: CommandModule = {
command: 'config',
describe:
'config tailchat registry which can display in Tailchat, run it in tailchat root path',
builder: (yargs) =>
yargs
.option('fe', {
describe: 'Config FE Plugin List',
type: 'boolean',
})
.option('verbose', {
describe: 'Show plugin manifest path list',
type: 'boolean',
}),
async handler(args) {
const feplugins = glob.sync(
path.join(process.cwd(), './client/web/plugins/*/manifest.json')
);
const beplugins = glob.sync(
path.join(process.cwd(), './server/plugins/*/web/plugins/*/manifest.json')
);
if (args.verbose) {
console.log('feplugins', feplugins);
console.log('beplugins', beplugins);
}
console.log(
`Scan plugins: fe(count: ${feplugins.length}) be(count: ${beplugins.length})`
);
if (args.fe) {
const alreadySelected = await fs.readJSON(feRegistryPath);
const feInfos = await Promise.all(feplugins.map((p) => fs.readJSON(p)));
const { selected: selectedInfo } = await inquirer.prompt([
{
name: 'selected',
type: 'checkbox',
default: alreadySelected
.map((item: any) => feInfos.find((info) => info.name === item.name))
.filter(Boolean),
choices: feInfos.map((info) => ({
name: `${info.name}(${info.version})`,
value: info,
})),
},
]);
console.log(`Selected ${selectedInfo.length} plugin.`);
await fs.writeJSON(feRegistryPath, selectedInfo, { spaces: 2 });
}
},
};

View File

@@ -0,0 +1,11 @@
import { CommandModule } from 'yargs';
import { registryConfigCommand } from './config';
// https://docs.docker.com/engine/api/v1.41/
export const registryCommand: CommandModule = {
command: 'registry',
describe: 'Tailchat registry config',
builder: (yargs) => yargs.command(registryConfigCommand).demandCommand(),
handler(args) {},
};

View File

@@ -0,0 +1,103 @@
import { CommandModule } from 'yargs';
import { config } from 'dotenv';
import inquirer from 'inquirer';
import nodemailer from 'nodemailer';
import { parseConnectionUrl } from 'nodemailer/lib/shared';
export const smtpCommand: CommandModule = {
command: 'smtp',
describe: 'SMTP Service',
builder: (yargs) =>
yargs
.command(
'verify',
'Verify smtp sender service',
(yargs) => {},
async (args) => {
config(); // 加载环境变量
console.log(
'This command will verify SMTP URI which use in tailchat, please put your URI which same like in tailchat env'
);
const { uri } = await inquirer.prompt([
{
type: 'input',
name: 'uri',
message: 'SMTP_URI',
default: process.env.SMTP_URI,
validate: isValidStr,
},
]);
const transporter = nodemailer.createTransport(
parseConnectionUrl(uri)
);
try {
const verify = await transporter.verify();
console.log('Verify Result:', verify);
} catch (err) {
console.log('Verify Failed:', String(err));
}
}
)
.command(
'test',
'Send test email with smtp service',
(yargs) => {},
async (args) => {
config(); // 加载环境变量
console.log(
'This command will send test email to your own email, please put your info which same like in tailchat env'
);
const { sender, uri, target } = await inquirer.prompt([
{
type: 'input',
name: 'sender',
message: 'SMTP_SENDER',
default: process.env.SMTP_SENDER,
validate: isValidStr,
},
{
type: 'input',
name: 'uri',
message: 'SMTP_URI',
default: process.env.SMTP_URI,
validate: isValidStr,
},
{
type: 'input',
name: 'target',
message: 'Email address which wanna send',
validate: isValidStr,
},
]);
const transporter = nodemailer.createTransport(
parseConnectionUrl(uri)
);
try {
const res = await transporter.sendMail({
from: sender,
to: target,
subject: `Test email send in ${new Date().toLocaleDateString()}`,
text: `This is a test email send by tailchat-cli at ${new Date().toLocaleString()}`,
});
console.log('Send Result:', res);
} catch (err) {
console.log('Send Failed:', String(err));
} finally {
transporter.close();
}
}
)
.demandCommand(),
handler() {},
};
function isValidStr(input: any): boolean {
return typeof input === 'string' && input.length > 0;
}

View File

@@ -0,0 +1,52 @@
import { CommandModule } from 'yargs';
import inquirer from 'inquirer';
import find from 'find-process';
import pidusage from 'pidusage';
export const usageCommand: CommandModule = {
command: 'usage [pid]',
describe: 'View Tailchat process usage',
builder: (yargs) =>
yargs.positional('pid', {
demandOption: false,
description: 'process id',
type: 'number',
}),
async handler(args) {
let pidList: number[] = [];
if (!args.pid) {
const list = await find('name', 'tailchat');
const processList = list.filter((item) => item.pid !== process.pid);
const res = await inquirer.prompt([
{
type: 'checkbox',
name: 'process',
message: 'Select the process to view',
choices: processList.map((item) => ({
name: `(${item.pid})${item.cmd}`,
value: item.pid,
})),
},
]);
pidList = res.process;
} else {
if (Array.isArray(args.pid)) {
pidList = args.pid;
} else {
pidList = [args.pid as number];
}
}
const stats = await pidusage(pidList);
const res = Object.entries(stats).map(([pid, info]) => ({
pid,
cpu: info.cpu,
memory: `${info.memory / 1024 / 1024} MB`,
}));
console.table(res);
},
};

26
apps/cli/src/index.ts Normal file
View File

@@ -0,0 +1,26 @@
import './update';
import yargs from 'yargs';
import { createCommand } from './commands/create';
import { connectCommand } from './commands/connect';
import { appCommand } from './commands/app';
import { declarationCommand } from './commands/declaration';
import { benchmarkCommand } from './commands/benchmark';
import { dockerCommand } from './commands/docker';
import { usageCommand } from './commands/usage';
import { registryCommand } from './commands/registry';
import { smtpCommand } from './commands/smtp';
yargs
.demandCommand()
.command(createCommand)
.command(connectCommand)
.command(appCommand)
.command(benchmarkCommand)
.command(declarationCommand)
.command(dockerCommand)
.command(registryCommand)
.command(smtpCommand)
.command(usageCommand)
.alias('h', 'help')
.scriptName('tailchat')
.parse();

7
apps/cli/src/update.ts Normal file
View File

@@ -0,0 +1,7 @@
import updateNotifier from 'update-notifier';
const packageJson = require('../package.json');
updateNotifier({
pkg: packageJson,
shouldNotifyInNpmScript: true,
}).notify();

15
apps/cli/src/utils.ts Normal file
View File

@@ -0,0 +1,15 @@
/**
* Determine whether it is a development environment
*/
export function isDev(): boolean {
return process.env.NODE_ENV === 'development';
}
/**
* Add github resource proxy to optimize chinese access speed
*
* @deprecated this website is down
*/
export function withGhProxy(url: string): string {
return `https://ghproxy.com/${url}`;
}

View File

@@ -0,0 +1,9 @@
{
"label": "{{name}}",
"name": "{{id}}",
"url": "/plugins/{{id}}/index.js",
"version": "0.0.0",
"author": "{{author}}",
"description": "{{desc}}",
"requireRestart": true
}

View File

@@ -0,0 +1,16 @@
{
"name": "@plugins/{{id}}",
"main": "src/index.tsx",
"version": "0.0.0",
"description": "{{desc}}",
"private": true,
"scripts": {
"sync:declaration": "tailchat declaration github"
},
"dependencies": {},
"devDependencies": {
"@types/styled-components": "^5.1.26",
"react": "18.2.0",
"styled-components": "^5.3.6"
}
}

View File

@@ -0,0 +1,4 @@
const PLUGIN_ID = '{{id}}';
const PLUGIN_NAME = '{{name}}';
console.log(`Plugin ${PLUGIN_NAME}(${PLUGIN_ID}) is loaded`);

View File

@@ -0,0 +1,8 @@
import { localTrans } from '@capital/common';
export const Translate = {
name: localTrans({
'zh-CN': '{{name}}',
'en-US': '{{name}}',
}),
};

View File

@@ -0,0 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"jsx": "react",
"importsNotUsedAsValues": "error"
}
}

View File

@@ -0,0 +1,2 @@
declare module '@capital/common';
declare module '@capital/component';

View File

@@ -0,0 +1,132 @@
const path = require('path');
const _ = require('lodash')
function pickPluginName(text) {
const [_1, _2, ...others] = text.split('.');
return others.join('-');
}
function upperFirst(text) {
return _.upperFirst(_.camelCase(text));
}
module.exports = function (
/** @type {import('plop').NodePlopAPI} */
plop
) {
plop.setHelper('pickPluginName', pickPluginName);
plop.setHelper('pickPluginNameUp', (text) => {
return upperFirst(pickPluginName(text));
});
const namePrompts = [
{
type: 'input',
name: 'name',
require: true,
message: 'Plugin Name',
}
]
const serverPrompts = [
{
type: 'input',
name: 'id',
require: true,
default: 'com.msgbyte.example',
message: 'Plugin unique id, a unique string in reverse domain name format',
},
{
type: 'input',
name: 'author',
message: 'Plugin Author',
default: 'anonymous',
},
{
type: 'input',
name: 'desc',
message: 'Plugin description',
default: '',
},
];
// 服务端插件的前端模板代码
plop.setGenerator('client-plugin', {
description: 'Pure frontend plugin template',
prompts: [
...namePrompts,
...serverPrompts,
],
actions: [
{
type: 'addMany',
destination: path.resolve(process.cwd(), './plugins'),
base: './client-plugin',
templateFiles: [
'./client-plugin/**/*',
],
skipIfExists: true,
globOptions: {},
},
],
});
// 服务端插件的前端模板代码
plop.setGenerator('server-plugin', {
description: 'Pure backtend plugin template',
prompts: serverPrompts,
actions: [
{
type: 'addMany',
destination: path.resolve(process.cwd(), './plugins'),
base: './server-plugin',
templateFiles: ['./server-plugin/**/*'],
skipIfExists: true,
globOptions: {},
},
],
});
// 服务端插件的前端模板代码
plop.setGenerator('server-plugin-web', {
description: 'web plugin in backtend plugin template',
prompts: [
...namePrompts,
...serverPrompts,
],
actions: [
{
type: 'addMany',
destination: path.resolve(process.cwd(), './plugins'),
base: './server-plugin-web',
templateFiles: [
'./server-plugin-web/**/*',
'./server-plugin-web/*/.ministarrc.js',
],
skipIfExists: true,
globOptions: {},
},
],
});
// 服务端插件的前端模板代码
plop.setGenerator('server-plugin-full', {
description: 'Full backend plugin template',
prompts: [
...namePrompts,
...serverPrompts,
],
actions: [
{
type: 'addMany',
destination: path.resolve(process.cwd(), './plugins'),
base: './server-plugin-full',
templateFiles: [
'./server-plugin-full/**/*',
'./server-plugin-full/*/.ministarrc.js',
],
skipIfExists: true,
globOptions: {},
},
],
});
};

View File

@@ -0,0 +1,17 @@
const path = require('path');
const pluginRoot = path.resolve(__dirname, './web');
const outDir = path.resolve(__dirname, '../../public');
module.exports = {
externalDeps: [
'react',
'react-router',
'axios',
'styled-components',
'zustand',
'zustand/middleware/immer'
],
pluginRoot,
outDir,
};

View File

@@ -0,0 +1,20 @@
import { db } from 'tailchat-server-sdk';
const { getModelForClass, prop, modelOptions, TimeStamps } = db;
@modelOptions({
options: {
customName: 'p_{{pickPluginName id}}',
},
})
export class {{pickPluginNameUp id}} extends TimeStamps implements db.Base {
_id: db.Types.ObjectId;
id: string;
}
export type {{pickPluginNameUp id}}Document = db.DocumentType<{{pickPluginNameUp id}}>;
const model = getModelForClass({{pickPluginNameUp id}});
export type {{pickPluginNameUp id}}Model = typeof model;
export default model;

View File

@@ -0,0 +1,20 @@
{
"name": "tailchat-plugin-{{pickPluginName id}}",
"version": "1.0.0",
"main": "index.js",
"author": "{{author}}",
"description": "{{desc}}",
"license": "MIT",
"private": true,
"scripts": {
"build:web": "ministar buildPlugin all",
"build:web:watch": "ministar watchPlugin all"
},
"devDependencies": {
"@types/react": "18.0.20",
"mini-star": "*"
},
"dependencies": {
"tailchat-server-sdk": "*"
}
}

View File

@@ -0,0 +1,22 @@
import { TcService, TcDbService } from 'tailchat-server-sdk';
import type { {{pickPluginNameUp id}}Document, {{pickPluginNameUp id}}Model } from '../models/{{pickPluginName id}}';
/**
* {{name}}
*
* {{desc}}
*/
interface {{pickPluginNameUp id}}Service
extends TcService,
TcDbService<{{pickPluginNameUp id}}Document, {{pickPluginNameUp id}}Model> {}
class {{pickPluginNameUp id}}Service extends TcService {
get serviceName() {
return 'plugin:{{id}}';
}
onInit() {
this.registerLocalDb(require('../models/{{pickPluginName id}}').default);
}
}
export default {{pickPluginNameUp id}}Service;

View File

@@ -0,0 +1,9 @@
{
"label": "{{name}}",
"name": "{{id}}",
"url": "{BACKEND}/plugins/{{id}}/index.js",
"version": "0.0.0",
"author": "{{author}}",
"description": "{{desc}}",
"requireRestart": true
}

View File

@@ -0,0 +1,16 @@
{
"name": "@plugins/{{id}}",
"main": "src/index.tsx",
"version": "0.0.0",
"description": "{{desc}}",
"private": true,
"scripts": {
"sync:declaration": "tailchat declaration github"
},
"dependencies": {},
"devDependencies": {
"@types/styled-components": "^5.1.26",
"react": "18.2.0",
"styled-components": "^5.3.6"
}
}

View File

@@ -0,0 +1,4 @@
const PLUGIN_ID = '{{id}}';
const PLUGIN_NAME = '{{name}}';
console.log(`Plugin ${PLUGIN_NAME}(${PLUGIN_ID}) is loaded`);

View File

@@ -0,0 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"jsx": "react",
"importsNotUsedAsValues": "error"
}
}

View File

@@ -0,0 +1,2 @@
declare module '@capital/common';
declare module '@capital/component';

View File

@@ -0,0 +1,17 @@
const path = require('path');
const pluginRoot = path.resolve(__dirname, './web');
const outDir = path.resolve(__dirname, '../../public');
module.exports = {
externalDeps: [
'react',
'react-router',
'axios',
'styled-components',
'zustand',
'zustand/middleware/immer'
],
pluginRoot,
outDir,
};

View File

@@ -0,0 +1,9 @@
{
"label": "{{name}}",
"name": "{{id}}",
"url": "{BACKEND}/plugins/{{id}}/index.js",
"version": "0.0.0",
"author": "{{author}}",
"description": "{{desc}}",
"requireRestart": true
}

View File

@@ -0,0 +1,16 @@
{
"name": "@plugins/{{id}}",
"main": "src/index.tsx",
"version": "0.0.0",
"description": "{{desc}}",
"private": true,
"scripts": {
"sync:declaration": "tailchat declaration github"
},
"dependencies": {},
"devDependencies": {
"@types/styled-components": "^5.1.26",
"react": "18.2.0",
"styled-components": "^5.3.6"
}
}

View File

@@ -0,0 +1,4 @@
const PLUGIN_ID = '{{id}}';
const PLUGIN_NAME = '{{name}}';
console.log(`Plugin ${PLUGIN_NAME}(${PLUGIN_ID}) is loaded`);

View File

@@ -0,0 +1,8 @@
import { localTrans } from '@capital/common';
export const Translate = {
name: localTrans({
'zh-CN': '{{name}}',
'en-US': '{{name}}',
}),
};

View File

@@ -0,0 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"jsx": "react",
"importsNotUsedAsValues": "error"
}
}

View File

@@ -0,0 +1,2 @@
declare module '@capital/common';
declare module '@capital/component';

View File

@@ -0,0 +1,20 @@
import { db } from 'tailchat-server-sdk';
const { getModelForClass, prop, modelOptions, TimeStamps } = db;
@modelOptions({
options: {
customName: 'p_{{pickPluginName id}}',
},
})
export class {{pickPluginNameUp id}} extends TimeStamps implements db.Base {
_id: db.Types.ObjectId;
id: string;
}
export type {{pickPluginNameUp id}}Document = db.DocumentType<{{pickPluginNameUp id}}>;
const model = getModelForClass({{pickPluginNameUp id}});
export type {{pickPluginNameUp id}}Model = typeof model;
export default model;

View File

@@ -0,0 +1,14 @@
{
"name": "tailchat-plugin-{{pickPluginName id}}",
"version": "1.0.0",
"main": "index.js",
"author": "{{author}}",
"description": "{{desc}}",
"license": "MIT",
"private": true,
"scripts": {},
"devDependencies": {},
"dependencies": {
"tailchat-server-sdk": "*"
}
}

View File

@@ -0,0 +1,20 @@
import { TcService, TcDbService } from 'tailchat-server-sdk';
import type { {{pickPluginNameUp id}}Document, {{pickPluginNameUp id}}Model } from '../models/{{pickPluginName id}}';
/**
* {{desc}}
*/
interface {{pickPluginNameUp id}}Service
extends TcService,
TcDbService<{{pickPluginNameUp id}}Document, {{pickPluginNameUp id}}Model> {}
class {{pickPluginNameUp id}}Service extends TcService {
get serviceName() {
return 'plugin:{{id}}';
}
onInit() {
this.registerLocalDb(require('../models/{{pickPluginName id}}').default);
}
}
export default {{pickPluginNameUp id}}Service;

16
apps/cli/tsconfig.json Normal file
View File

@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"rootDir": "src",
"outDir": "lib",
"jsx": "react",
"allowSyntheticDefaultImports": true,
"strict": true,
"esModuleInterop": true,
"moduleResolution": "node",
"skipLibCheck": true,
"noEmit": false
},
"exclude": ["./templates"]
}

View File

@@ -0,0 +1,12 @@
**/node_modules/
**/.git
**/README.md
**/LICENSE
**/.vscode
**/npm-debug.log
**/coverage
**/.env
**/.editorconfig
**/dist
**/*.pem
Dockerfile

View File

@@ -0,0 +1,16 @@
# The ID of your GitHub App
APP_ID=
WEBHOOK_SECRET=development
PRIVATE_KEY=
# Use `trace` to get verbose logging or `info` to show less
LOG_LEVEL=debug
# Go to https://smee.io/new set this to the URL that you are redirected to.
WEBHOOK_PROXY_URL=
# Tailchat
TAILCHAT_WEB_URL=https://nightly.paw.msgbyte.com
TAILCHAT_API_URL=https://tailchat-nightly.moonrailgun.com
TAILCHAT_APP_ID=
TAILCHAT_APP_SECRET=

7
apps/github-app/.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
node_modules
npm-debug.log
*.pem
!mock-cert.pem
.env
coverage
lib

View File

@@ -0,0 +1,73 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
education, socio-economic status, nationality, personal appearance, race,
religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
- The use of sexualized language or imagery and unwelcome sexual attention or
advances
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic
address, without explicit permission
- Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at . All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
[homepage]: https://www.contributor-covenant.org

View File

@@ -0,0 +1,39 @@
## Contributing
[fork]: /fork
[pr]: /compare
[code-of-conduct]: CODE_OF_CONDUCT.md
Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great.
Please note that this project is released with a [Contributor Code of Conduct][code-of-conduct]. By participating in this project you agree to abide by its terms.
## Issues and PRs
If you have suggestions for how this project could be improved, or want to report a bug, open an issue! We'd love all and any contributions. If you have questions, too, we'd love to hear them.
We'd also love PRs. If you're thinking of a large PR, we advise opening up an issue first to talk about it, though! Look at the links below if you're not sure how to open a PR.
## Submitting a pull request
1. [Fork][fork] and clone the repository.
1. Configure and install the dependencies: `npm install`.
1. Make sure the tests pass on your machine: `npm test`, note: these tests also apply the linter, so there's no need to lint separately.
1. Create a new branch: `git checkout -b my-branch-name`.
1. Make your change, add tests, and make sure the tests still pass.
1. Push to your fork and [submit a pull request][pr].
1. Pat your self on the back and wait for your pull request to be reviewed and merged.
Here are a few things you can do that will increase the likelihood of your pull request being accepted:
- Write and update tests.
- Keep your changes as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests.
- Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html).
Work in Progress pull requests are also welcome to get feedback early on, or if there is something blocked you.
## Resources
- [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
- [Using Pull Requests](https://help.github.com/articles/about-pull-requests/)
- [GitHub Help](https://help.github.com)

View File

@@ -0,0 +1,8 @@
FROM node:12-slim
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm ci --production
RUN npm cache clean --force
ENV NODE_ENV="production"
COPY . .
CMD [ "npm", "start" ]

15
apps/github-app/LICENSE Normal file
View File

@@ -0,0 +1,15 @@
ISC License
Copyright (c) 2022, moonrailgun
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

33
apps/github-app/README.md Normal file
View File

@@ -0,0 +1,33 @@
# github-app
> A GitHub App built with [Probot](https://github.com/probot/probot) that Tailchat github integrations
## Setup
```sh
# Install dependencies
npm install
# Run the bot
npm start
```
## Docker
```sh
# 1. Build container
docker build -t github-app .
# 2. Start container
docker run -e APP_ID=<app-id> -e PRIVATE_KEY=<pem-value> github-app
```
## Contributing
If you have suggestions for how github-app could be improved, or want to report a bug, open an issue! We'd love all and any contributions.
For more, check out the [Contributing Guide](CONTRIBUTING.md).
## License
[ISC](LICENSE) © 2022 moonrailgun

View File

@@ -0,0 +1,10 @@
// For vercel
// Reference: https://probot.github.io/docs/deployment/#vercel
const { createNodeMiddleware, createProbot } = require('probot');
const appFn = require('../../../src/app').appFn;
module.exports = createNodeMiddleware(appFn, {
probot: createProbot(),
webhooksPath: '/api/github/webhooks',
});

View File

@@ -0,0 +1,11 @@
const app = require('express')();
const { v4 } = require('uuid');
const { createProbot } = require('probot');
const { appFn, buildRouter } = require('../src/app');
const probot = createProbot();
probot.load(appFn, {
getRouter: (path) => app,
});
module.exports = app;

137
apps/github-app/app.yml Normal file
View File

@@ -0,0 +1,137 @@
# This is a GitHub App Manifest. These settings will be used by default when
# initially configuring your GitHub App.
#
# NOTE: changing this file will not update your GitHub App settings.
# You must visit github.com/settings/apps/your-app-name to edit them.
#
# Read more about configuring your GitHub App:
# https://probot.github.io/docs/development/#configuring-a-github-app
#
# Read more about GitHub App Manifests:
# https://developer.github.com/apps/building-github-apps/creating-github-apps-from-a-manifest/
# The list of events the GitHub App subscribes to.
# Uncomment the event names below to enable them.
default_events:
# - check_run
# - check_suite
# - commit_comment
# - create
# - delete
# - deployment
# - deployment_status
# - fork
# - gollum
# - issue_comment
- issues
# - label
# - milestone
# - member
# - membership
# - org_block
# - organization
# - page_build
# - project
# - project_card
# - project_column
# - public
# - pull_request
# - pull_request_review
# - pull_request_review_comment
# - push
# - release
# - repository
# - repository_import
# - status
# - team
# - team_add
# - watch
# The set of permissions needed by the GitHub App. The format of the object uses
# the permission name for the key (for example, issues) and the access type for
# the value (for example, write).
# Valid values are `read`, `write`, and `none`
default_permissions:
# Repository creation, deletion, settings, teams, and collaborators.
# https://developer.github.com/v3/apps/permissions/#permission-on-administration
# administration: read
# Checks on code.
# https://developer.github.com/v3/apps/permissions/#permission-on-checks
# checks: read
# Repository contents, commits, branches, downloads, releases, and merges.
# https://developer.github.com/v3/apps/permissions/#permission-on-contents
# contents: read
# Deployments and deployment statuses.
# https://developer.github.com/v3/apps/permissions/#permission-on-deployments
# deployments: read
# Issues and related comments, assignees, labels, and milestones.
# https://developer.github.com/v3/apps/permissions/#permission-on-issues
issues: write
# Search repositories, list collaborators, and access repository metadata.
# https://developer.github.com/v3/apps/permissions/#metadata-permissions
metadata: read
# Retrieve Pages statuses, configuration, and builds, as well as create new builds.
# https://developer.github.com/v3/apps/permissions/#permission-on-pages
# pages: read
# Pull requests and related comments, assignees, labels, milestones, and merges.
# https://developer.github.com/v3/apps/permissions/#permission-on-pull-requests
# pull_requests: read
# Manage the post-receive hooks for a repository.
# https://developer.github.com/v3/apps/permissions/#permission-on-repository-hooks
# repository_hooks: read
# Manage repository projects, columns, and cards.
# https://developer.github.com/v3/apps/permissions/#permission-on-repository-projects
# repository_projects: read
# Retrieve security vulnerability alerts.
# https://developer.github.com/v4/object/repositoryvulnerabilityalert/
# vulnerability_alerts: read
# Commit statuses.
# https://developer.github.com/v3/apps/permissions/#permission-on-statuses
# statuses: read
# Organization members and teams.
# https://developer.github.com/v3/apps/permissions/#permission-on-members
# members: read
# View and manage users blocked by the organization.
# https://developer.github.com/v3/apps/permissions/#permission-on-organization-user-blocking
# organization_user_blocking: read
# Manage organization projects, columns, and cards.
# https://developer.github.com/v3/apps/permissions/#permission-on-organization-projects
# organization_projects: read
# Manage team discussions and related comments.
# https://developer.github.com/v3/apps/permissions/#permission-on-team-discussions
# team_discussions: read
# Manage the post-receive hooks for an organization.
# https://developer.github.com/v3/apps/permissions/#permission-on-organization-hooks
# organization_hooks: read
# Get notified of, and update, content references.
# https://developer.github.com/v3/apps/permissions/
# organization_administration: read
# The name of the GitHub App. Defaults to the name specified in package.json
# name: My Probot App
# The homepage of your GitHub App.
# url: https://example.com/
# A description of the GitHub App.
# description: A description of my awesome app
# Set to true when your GitHub App is available to the public or false when it is only accessible to the owner of the app.
# Default: true
# public: false

View File

@@ -0,0 +1,9 @@
module.exports = {
roots: ['<rootDir>/src/', '<rootDir>/test/'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|\\.(test|spec))\\.[tj]sx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testEnvironment: 'node',
};

View File

@@ -0,0 +1,45 @@
{
"name": "tailchat-github-app",
"version": "1.0.0",
"private": true,
"description": "Tailchat github integrations",
"author": "moonrailgun",
"license": "ISC",
"homepage": "https://github.com/msgbyte/tailchat",
"keywords": [
"probot",
"github",
"probot-app"
],
"scripts": {
"dev": "nodemon --watch \"src/**\" --ext \"ts\" --ignore \"src/**/*.spec.ts\" --exec \"ts-node --transpile-only src/index.ts\"",
"build": "tsc",
"prepare": "tsc",
"test": "jest"
},
"dependencies": {
"axios": "^0.21.1",
"body-parser": "^1.20.1",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"lodash": "^4.17.21",
"probot": "^12.3.0",
"probot-metadata": "^2.1.0"
},
"devDependencies": {
"@types/body-parser": "^1.19.2",
"@types/jest": "27.5.2",
"@types/lodash": "^4.14.191",
"@types/node": "^18.13.0",
"jest": "27.5.1",
"nock": "^13.0.5",
"nodemon": "^2.0.18",
"smee-client": "^1.2.2",
"ts-jest": "27.1.4",
"ts-node": "^10.9.1",
"typescript": "^4.8.2"
},
"engines": {
"node": ">= 10.13.0"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

281
apps/github-app/src/app.ts Normal file
View File

@@ -0,0 +1,281 @@
import { ApplicationFunction, Probot } from 'probot';
import metadata from 'probot-metadata';
import { TailchatClient } from './client';
import {
configPath,
generateErrorBlock,
generateTopicCommentCreateContent,
generateTopicCreateContent,
} from './utils';
import bodyParser from 'body-parser';
import _ from 'lodash';
import * as dotenv from 'dotenv'; // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
dotenv.config();
const LABEL = 'tailchat-topic';
const TOPIC_KEY = 'tailchatTopicId';
if (
!process.env.TAILCHAT_API_URL ||
!process.env.TAILCHAT_APP_ID ||
!process.env.TAILCHAT_APP_SECRET
) {
throw new Error(
'Require env: TAILCHAT_API_URL, TAILCHAT_APP_ID, TAILCHAT_APP_SECRET'
);
}
const defaultTailchatApiUrl = process.env.TAILCHAT_API_URL;
const tailchatAppId = process.env.TAILCHAT_APP_ID;
const tailchatAppSecret = process.env.TAILCHAT_APP_SECRET;
const tailchatWebUrl =
process.env.TAILCHAT_WEB_URL || process.env.TAILCHAT_API_URL;
export const appFn: ApplicationFunction = (app, { getRouter }) => {
app.on('issues.opened', async (ctx) => {
if (ctx.isBot) {
return;
}
try {
const { data } = await ctx.octokit.repos.getContent(
ctx.repo({
path: configPath,
})
);
if (!(!Array.isArray(data) && 'content' in data)) {
throw new Error('config file type error');
}
// 是配置文件
const { tailchatClient, groupId, panelId } =
createTailchatContextWithConfig(data.content);
console.log('配置信息', { tailchatClient, groupId, panelId });
// 发送到tailchat
const { data: topic } = await tailchatClient.call(
'plugin:com.msgbyte.topic.create',
{
groupId,
panelId,
content: generateTopicCreateContent(
ctx.payload.issue.user.login,
ctx.payload.issue.title,
ctx.payload.issue.body ?? '',
ctx.payload.issue.html_url
),
meta: {
tailchatHost: tailchatClient.url,
installationId: ctx.payload.installation?.id,
githubRepoOwner: ctx.payload.repository.owner.login,
githubRepoName: ctx.payload.repository.name,
githubIssueNumber: ctx.payload.issue.number,
},
}
);
console.log('Tailchat Topic 创建成功', topic);
await Promise.all([
ctx.octokit.issues.createComment(
ctx.issue({
body: `Thanks for opening this issue! Tailchat topic is created in ${tailchatWebUrl}/main/group/${groupId}/${panelId}!`,
})
),
ctx.octokit.issues.addLabels(
ctx.repo({
issue_number: ctx.payload.issue.number,
labels: [LABEL],
})
),
metadata(ctx).set(TOPIC_KEY, topic._id),
]);
console.log('发送相关信息到 Github 完毕');
} catch (err) {
console.error(err);
await ctx.octokit.issues.createComment(
ctx.issue({
body: generateErrorBlock(err),
})
);
}
});
app.on('issue_comment.created', async (ctx) => {
if (ctx.isBot) {
console.error('This comment created by Bot, Skip!');
return;
}
// 发送到tailchat
try {
const topicId = await metadata(ctx).get(TOPIC_KEY);
if (!topicId) {
console.error('Not found topic id, Skip!');
return;
}
const { data } = await ctx.octokit.repos.getContent(
ctx.repo({
path: configPath,
})
);
if (!(!Array.isArray(data) && 'content' in data)) {
throw new Error('Config file type error');
}
// 是配置文件
const { tailchatClient, groupId, panelId } =
createTailchatContextWithConfig(data.content);
// 发送到tailchat
await tailchatClient.call('plugin:com.msgbyte.topic.createComment', {
groupId,
panelId,
topicId,
content: generateTopicCommentCreateContent(
ctx.payload.comment.user.login,
ctx.payload.comment.body ?? '',
ctx.payload.comment.html_url
),
});
} catch (err) {
console.error(err);
await ctx.octokit.issues.createComment(
ctx.issue({
body: generateErrorBlock(err),
})
);
}
});
// app.on('installation.created', async (ctx) => {
// const installationId = ctx.payload.installation.id;
// const installationTargetName = ctx.payload.installation.account.login;
// const installationTargetRepositories = ctx.payload.repositories;
// console.log('installation.created', {
// installationId,
// installationTargetName,
// installationTargetRepositories,
// });
// });
buildRouter(app, getRouter);
};
/**
* 从配置文件中创建上下文
*
* 因为考虑serverless服务因此不能全局管理
*/
function createTailchatContextWithConfig(githubRaw: string) {
const content = Buffer.from(githubRaw, 'base64').toString();
const json = JSON.parse(content);
const tailchatHost = json['tailchatHost'] ?? defaultTailchatApiUrl;
const groupId = json['groupId'];
const panelId = json['panelId'];
if (!groupId || !panelId) {
throw new Error('config format error');
}
const tailchatClient = createTailchatClient(tailchatHost);
return {
tailchatClient,
groupId,
panelId,
};
}
function createTailchatClient(tailchatHost = defaultTailchatApiUrl) {
const tailchatClient = new TailchatClient(
tailchatHost,
tailchatAppId,
tailchatAppSecret
);
return tailchatClient;
}
export function buildRouter(
app: Probot,
getRouter: Parameters<ApplicationFunction>[1]['getRouter']
) {
if (getRouter) {
getRouter('/')
.get('/api', (_req, res) => {
res.send('Hello World! Github app api server is working!');
})
.get('/api/message/webhook', (_req, res) => {
res.send('Please use POST method');
})
.post('/api/message/webhook', bodyParser.json(), (req, res) => {
(async () => {
try {
// 根据收件箱内容向 Github Issue 创建话题
const inboxItem = req.body ?? {};
if (inboxItem.type !== 'plugin:com.msgbyte.topic.comment') {
// 如果不是回复消息,则跳过
return;
}
const newComment: any =
_.last(_.get(inboxItem, ['payload', 'comments'])) ?? {};
const meta = _.get(inboxItem, ['payload', 'meta']) ?? {};
if (
!meta.installationId ||
!meta.githubRepoOwner ||
!meta.githubRepoName ||
!meta.githubIssueNumber
) {
console.warn('Cannot pass meta info check:', { meta });
return;
}
if (!newComment.author || !newComment.content) {
console.warn(
'Cannot get "newComment.author" or "newComment.content"'
);
return;
}
// 发送到github comment
const octokit = await app.auth(Number(meta.installationId));
const tailchatClient = createTailchatClient(meta.tailchatHost);
const userInfo = await tailchatClient.call('user.getUserInfo', {
userId: newComment.author,
});
const payload = {
owner: meta.githubRepoOwner,
repo: meta.githubRepoName,
issue_number: meta.githubIssueNumber,
body: `[${_.get(userInfo, [
'data',
'nickname',
])}] 在 Tailchat 回复:\n\`\`\`\n${
newComment.content ?? ''
}\n\`\`\``,
};
console.log('正在向Github Issue创建回复:', payload);
await octokit.issues.createComment(payload);
} catch (err) {
console.error(err);
}
})();
res.send('Success!');
});
}
}

View File

@@ -0,0 +1,97 @@
import axios, { AxiosInstance } from 'axios';
import crypto from 'crypto';
export class TailchatClient {
request: AxiosInstance;
jwt: string | null = null;
userId: string | null = null;
loginP: Promise<void>;
constructor(
public url: string,
public appId: string,
public appSecret: string
) {
if (!url || !appId || !appSecret) {
throw new Error(
'Require params: apiUrl, appId, appSecret. You can set it with env'
);
}
this.request = axios.create({
baseURL: url,
});
this.request.interceptors.request.use(async (val) => {
if (
this.jwt &&
['post', 'get'].includes(String(val.method).toLowerCase()) &&
!val.headers['X-Token']
) {
// 任何请求都尝试增加token
val.headers['X-Token'] = this.jwt;
}
return val;
});
this.loginP = this.login();
}
async login() {
try {
console.log('正在登录...');
const { data } = await this.request.post('/api/openapi/bot/login', {
appId: this.appId,
token: this.getBotToken(),
});
// NOTICE: 注意有30天过期时间需要定期重新登录以换取新的token
// 这里先不换
this.jwt = data.data?.jwt;
console.log('tailchat openapp login success!');
// 尝试调用函数
// this.whoami().then(console.log);
} catch (err) {
console.error(err);
throw new Error('登录失败, 请检查应用凭证');
}
}
async call(action: string, params = {}) {
try {
await Promise.resolve(this.loginP);
console.log('正在调用服务:', action);
const { data } = await this.request.post(
'/api/' + action.replace(/\./g, '/'),
params
);
return data;
} catch (err: any) {
console.error('服务调用失败:', err);
const data: string = err?.response?.data;
if (data) {
throw new Error(
JSON.stringify({
action,
data,
})
);
} else {
throw err;
}
}
}
async whoami() {
return this.call('user.whoami');
}
getBotToken() {
return crypto
.createHash('md5')
.update(this.appId + this.appSecret)
.digest('hex');
}
}

View File

@@ -0,0 +1,4 @@
import { run } from 'probot';
import { appFn } from './app';
run(appFn);

View File

@@ -0,0 +1,43 @@
export const configPath = '.tailchat/topic.json';
/**
* 输入错误对象
* 返回一个详细信息的markdown字段
*/
export function generateErrorBlock(err: unknown) {
const detail = err instanceof Error ? err : new Error(String(err));
const codesign = '```';
const errorBlock = `${codesign}\n${detail.name}${detail.message}\n${detail.stack}\n${codesign}`;
return `Tailchat occur error, please checkout your config in \`${configPath}\`! \n${errorBlock}`;
}
/**
* 生成创建话题的内容
*/
export function generateTopicCreateContent(
user: string,
title: string,
body: string,
url: string
) {
return `[b]${user}[/b] create Issue:
[b]Title: ${title}[/b]
[markdown]${body}[/markdown]
Website: ${url}`;
}
/**
* 生成创建话题的内容
*/
export function generateTopicCommentCreateContent(
user: string,
body: string,
url: string
) {
return `[b]${user}[/b] reply Issue:
[markdown]${body}[/markdown]
Website: ${url}`;
}

View File

@@ -0,0 +1,18 @@
{
"action": "opened",
"issue": {
"number": 1,
"user": {
"login": "hiimbex"
}
},
"repository": {
"name": "testing-things",
"owner": {
"login": "hiimbex"
}
},
"installation": {
"id": 2
}
}

View File

@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAli7V49NdZe+XYC1pLaHM0te8kiDmZBJ1u2HJHN8GdbROB6NO
VpC3xK7NxQn6xpvZ9ux20NvcDvGle+DOptZztBH+np6h2jZQ1/kD1yG1eQvVH4th
/9oqHuIjmIfO8lIe4Hyd5Fw5xHkGqVETTGR+0c7kdZIlHmkOregUGtMYZRUi4YG+
q0w+uFemiHpGKXbeCIAvkq7aIkisEzvPWfSyYdA6WJHpxFk7tD7D8VkzABLVRHCq
AuyqPG39BhGZcGLXx5rGK56kDBJkyTR1t3DkHpwX+JKNG5UYNwOG4LcQj1fteeta
TdkYUMjIyWbanlMYyC+dq7B5fe7el99jXQ1gXwIDAQABAoIBADKfiPOpzKLOtzzx
MbHzB0LO+75aHq7+1faayJrVxqyoYWELuB1P3NIMhknzyjdmU3t7S7WtVqkm5Twz
lBUC1q+NHUHEgRQ4GNokExpSP4SU63sdlaQTmv0cBxmkNarS6ZuMBgDy4XoLvaYX
MSUf/uukDLhg0ehFS3BteVFtdJyllhDdTenF1Nb1rAeN4egt8XLsE5NQDr1szFEG
xH5lb+8EDtzgsGpeIddWR64xP0lDIKSZWst/toYKWiwjaY9uZCfAhvYQ1RsO7L/t
sERmpYgh+rAZUh/Lr98EI8BPSPhzFcSHmtqzzejvC5zrZPHcUimz0CGA3YBiLoJX
V1OrxmECgYEAxkd8gpmVP+LEWB3lqpSvJaXcGkbzcDb9m0OPzHUAJDZtiIIf0UmO
nvL68/mzbCHSj+yFjZeG1rsrAVrOzrfDCuXjAv+JkEtEx0DIevU1u60lGnevOeky
r8Be7pmymFB9/gzQAd5ezIlTv/COgoO986a3h1yfhzrrzbqSiivw308CgYEAwecI
aZZwqH3GifR+0+Z1B48cezA5tC8LZt5yObGzUfxKTWy30d7lxe9N59t0KUVt/QL5
qVkd7mqGzsUMyxUN2U2HVnFTWfUFMhkn/OnCnayhILs8UlCTD2Xxoy1KbQH/9FIr
xf0pbMNJLXeGfyRt/8H+BzSZKBw9opJBWE4gqfECgYBp9FdvvryHuBkt8UQCRJPX
rWsRy6pY47nf11mnazpZH5Cmqspv3zvMapF6AIxFk0leyYiQolFWvAv+HFV5F6+t
Si1mM8GCDwbA5zh6pEBDewHhw+UqMBh63HSeUhmi1RiOwrAA36CO8i+D2Pt+eQHv
ir52IiPJcs4BUNrv5Q1BdwKBgBHgVNw3LGe8QMOTMOYkRwHNZdjNl2RPOgPf2jQL
d/bFBayhq0jD/fcDmvEXQFxVtFAxKAc+2g2S8J67d/R5Gm/AQAvuIrsWZcY6n38n
pfOXaLt1x5fnKcevpFlg4Y2vM4O416RHNLx8PJDehh3Oo/2CSwMrDDuwbtZAGZok
icphAoGBAI74Tisfn+aeCZMrO8KxaWS5r2CD1KVzddEMRKlJvSKTY+dOCtJ+XKj1
OsZdcDvDC5GtgcywHsYeOWHldgDWY1S8Z/PUo4eK9qBXYBXp3JEZQ1dqzFdz+Txi
rBn2WsFLsxV9j2/ugm0PqWVBcU2bPUCwvaRu3SOms2teaLwGCkhr
-----END RSA PRIVATE KEY-----

View File

@@ -0,0 +1,74 @@
// You can import your modules
// import index from '../src/index'
import nock from 'nock';
// Requiring our app implementation
import myProbotApp from '../src';
import { Probot, ProbotOctokit } from 'probot';
// Requiring our fixtures
import payload from './fixtures/issues.opened.json';
const issueCreatedBody = { body: 'Thanks for opening this issue!' };
const fs = require('fs');
const path = require('path');
const privateKey = fs.readFileSync(
path.join(__dirname, 'fixtures/mock-cert.pem'),
'utf-8'
);
describe('My Probot app', () => {
let probot: any;
beforeEach(() => {
nock.disableNetConnect();
probot = new Probot({
appId: 123,
privateKey,
// disable request throttling and retries for testing
Octokit: ProbotOctokit.defaults({
retry: { enabled: false },
throttle: { enabled: false },
}),
});
// Load our app into probot
probot.load(myProbotApp);
});
test('creates a comment when an issue is opened', async () => {
const mock = nock('https://api.github.com')
// Test that we correctly return a test token
.post('/app/installations/2/access_tokens')
.reply(200, {
token: 'test',
permissions: {
issues: 'write',
},
})
// Test that a comment is posted
.post('/repos/hiimbex/testing-things/issues/1/comments', (body: any) => {
expect(body).toMatchObject(issueCreatedBody);
return true;
})
.reply(200);
// Receive a webhook event
await probot.receive({ name: 'issues', payload });
expect(mock.pendingMocks()).toStrictEqual([]);
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
});
// For more information about testing with Jest see:
// https://facebook.github.io/jest/
// For more information about using TypeScript in your tests, Jest recommends:
// https://github.com/kulshekhar/ts-jest
// For more information about testing with Nock see:
// https://github.com/nock/nock

View File

@@ -0,0 +1,74 @@
{
"compilerOptions": {
/* Basic Options */
"incremental": true /* Enable incremental compilation */,
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": [
"es2015",
"es2017"
] /* Specify library files to be included in the compilation. */,
"allowJs": true /* Allow javascript files to be compiled. */,
"checkJs": true /* Report errors in .js files. */,
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true /* Generates corresponding '.d.ts' file. */,
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true /* Generates corresponding '.map' file. */,
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./lib" /* Redirect output structure to the directory. */,
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./" /* Specify file to store incremental compilation information */,
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
"noUnusedLocals": true /* Report errors on unused locals. */,
"noUnusedParameters": true /* Report errors on unused parameters. */,
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
/* Module Resolution Options */
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
// "baseUrl": "./src" /* Base directory to resolve non-absolute module names. */,
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just type checking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
"resolveJsonModule": true,
"pretty": false,
"skipLibCheck": true
},
"include": ["src/"],
"compileOnSave": false
}

View File

@@ -0,0 +1,6 @@
{
"rewrites": [
{ "source": "/api/(.*)", "destination": "/api" },
{ "source": "/api/github/webhooks", "destination": "/api/github/webhooks" }
]
}

11
apps/oauth-demo/app.html Normal file
View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>OAuth Demo</title>
</head>
<body>
<a href="<API>/open/auth?client_id=<clientId>&redirect_uri=<clientUrl>/cb&scope=openid profile&response_type=code&state=45535116436">登录</a>
</body>
</html>

63
apps/oauth-demo/index.ts Normal file
View File

@@ -0,0 +1,63 @@
import express from 'express';
import path from 'path';
import fs from 'fs-extra';
import { OAuthClient } from 'tailchat-server-sdk';
const app = express();
const port = 8080;
const API = process.env.API || 'http://localhost:11001';
const clientUrl = `http://localhost:${port}`;
const clientId = process.env.ID;
const clientSecret = process.env.SECRET;
if (!clientId || !clientSecret) {
throw new Error('环境变量缺失, 请设置环境变量 ID 和 SECRET');
}
console.log('config:', {
API,
clientUrl,
clientId,
});
const tailchatClient = new OAuthClient(API, clientId, clientSecret);
app.get('/', async (req, res) => {
let html = (
await fs.readFile(path.resolve(__dirname, './app.html'))
).toString();
html = html
.replace('<API>', API)
.replace('<clientId>', clientId)
.replace('<clientUrl>', clientUrl);
res.send(html);
});
app.get('/cb', async (req, res, next) => {
try {
const { code, state } = req.query;
console.log('code', code);
// 根据获取到的code获取授权码
const { access_token } = await tailchatClient.getToken(
String(code),
`${clientUrl}/cb`
);
console.log('access_token', access_token);
const { data: userInfo } = await tailchatClient.getUserInfo(access_token);
res.json({ userInfo });
} catch (err: any) {
console.error(err.response.data);
next(err);
}
});
app.listen(port, () => {
console.log(`请确保回调已经被注册在OIDC服务端的白名单中: ${clientUrl}/cb`);
console.log(`测试服务地址: http://127.0.0.1:${port}`);
});

View File

@@ -0,0 +1,25 @@
{
"name": "oauth-demo",
"version": "1.0.0",
"private": true,
"description": "",
"main": "index.js",
"scripts": {
"start": "ts-node ./index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "moonrailgun <moonrailgun@gmail.com>",
"license": "MIT",
"dependencies": {
"express": "^4.18.2",
"fs-extra": "^11.1.0",
"tailchat-server-sdk": "workspace:^"
},
"devDependencies": {
"@types/express": "^4.17.15",
"@types/fs-extra": "^9.0.13",
"@types/node": "^18.13.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
}
}

View File

@@ -0,0 +1,9 @@
{
"compilerOptions": {
"esModuleInterop": true,
"isolatedModules": true,
"strict": true,
"importsNotUsedAsValues": "error",
"experimentalDecorators": true,
}
}

1
apps/widget/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
lib/

3
apps/widget/.yarnrc Normal file
View File

@@ -0,0 +1,3 @@
registry "https://registry.npmjs.org"
version-tag-prefix "widget-v"
version-git-message "widget-v%s"

1
apps/widget/demo/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.parcel-cache/

View File

@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailchat Widget Demo</title>
</head>
<body>
<script type="module">
import { createTailchatWidget } from '../lib/index.js';
createTailchatWidget({
groupId: '611e4e18d3e846001bd0223a',
panelId: '611e4e18d3e846001bd02239',
});
</script>
</body>
</html>

View File

@@ -0,0 +1,19 @@
{
"name": "tailchat-widget-demo",
"version": "1.0.0",
"source": "index.html",
"main": "dist/index.js",
"repository": "https://github.com/msgbyte/tailchat.git",
"author": "moonrailgun <moonrailgun@gmail.com>",
"license": "MIT",
"private": true,
"scripts": {
"start": "parcel serve index.html --no-cache"
},
"devDependencies": {
"typescript": "^4.4.4"
},
"dependencies": {
"parcel": "^2.0.0"
}
}

4535
apps/widget/demo/yarn.lock Normal file

File diff suppressed because it is too large Load Diff

20
apps/widget/package.json Normal file
View File

@@ -0,0 +1,20 @@
{
"name": "tailchat-widget",
"version": "0.0.8",
"main": "lib/index.js",
"repository": "https://github.com/msgbyte/tailchat.git",
"author": "moonrailgun <moonrailgun@gmail.com>",
"license": "MIT",
"files": [
"lib/index.js",
"lib/index.d.ts"
],
"scripts": {
"watch": "tsc --watch",
"build": "tsc",
"release": "yarn publish --patch"
},
"devDependencies": {
"typescript": "^4.8.2"
}
}

136
apps/widget/src/index.ts Normal file
View File

@@ -0,0 +1,136 @@
export interface TailchatWidgetOptions {
/**
* @default https://nightly.paw.msgbyte.com/
*/
host?: string;
groupId: string;
panelId: string;
widgetStyle?: Partial<CSSStyleDeclaration>;
iconStyle?: Partial<CSSStyleDeclaration>;
frameStyle?: Partial<CSSStyleDeclaration>;
}
const defaultTailchatWidgetOptions: Partial<TailchatWidgetOptions> = {
host: 'https://nightly.paw.msgbyte.com',
};
const defaultWidgetStyle: Partial<CSSStyleDeclaration> = {
position: 'absolute',
right: '20px',
bottom: '20px',
};
const iconContainerSize = 48;
const defaultIconContainerStyle: Partial<CSSStyleDeclaration> = {
width: `${iconContainerSize}px`,
height: `${iconContainerSize}px`,
boxShadow: '0 1px 4px rgba(0, 0, 0, 0.2)',
borderRadius: '50%',
cursor: 'pointer',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
};
const defaultFrameStyle: Partial<CSSStyleDeclaration> = {
width: '414px',
height: '736px',
border: '0',
borderRadius: '3px',
boxShadow: '0 1px 4px rgba(0, 0, 0, 0.2)',
};
const iconSize = 32;
const iconSvg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="${iconSize}" height="${iconSize}" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M12 3C6.5 3 2 6.58 2 11a7.218 7.218 0 0 0 2.75 5.5c0 .6-.42 2.17-2.75 4.5c2.37-.11 4.64-1 6.47-2.5c1.14.33 2.34.5 3.53.5c5.5 0 10-3.58 10-8s-4.5-8-10-8m0 14c-4.42 0-8-2.69-8-6s3.58-6 8-6s8 2.69 8 6s-3.58 6-8 6m5-5v-2h-2v2h2m-4 0v-2h-2v2h2m-4 0v-2H7v2h2z" fill="currentColor"/></svg>`;
const closeIconSvg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="${iconSize}" height="${iconSize}" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" style="display: block;"><path d="M11 4h2v12l5.5-5.5l1.42 1.42L12 19.84l-7.92-7.92L5.5 10.5L11 16V4z" fill="currentColor"/></svg>`;
/**
* 创建聊天小部件
*/
export function createTailchatWidget(_options: TailchatWidgetOptions) {
const options = { ...defaultTailchatWidgetOptions, ..._options };
const url = `${options.host}/panel/group/${options.groupId}/${options.panelId}`;
// 容器
const container = document.createElement('div');
applyStyle(container, {
...defaultWidgetStyle,
..._options.widgetStyle,
});
// 图标
const iconContainer = document.createElement('div');
applyStyle(iconContainer, {
...defaultIconContainerStyle,
..._options.iconStyle,
});
iconContainer.innerHTML = iconSvg;
container.appendChild(iconContainer);
// Iframe 容器
let frameContainer: HTMLDivElement | null = null;
iconContainer.addEventListener('click', () => {
// 展开iframe
if (!frameContainer) {
// 元素不存在
// 容器
const _frameContainer = document.createElement('div');
frameContainer = _frameContainer;
// Iframe
const frameEl = document.createElement('iframe');
frameEl.src = url;
applyStyle(frameEl, {
...defaultFrameStyle,
..._options.frameStyle,
});
// closeBtn
const closeBtnEl = document.createElement('div');
closeBtnEl.innerHTML = closeIconSvg;
applyStyle(closeBtnEl, {
position: 'absolute',
right: '0',
top: `-${iconSize}px`,
backgroundColor: 'white',
cursor: 'pointer',
boxShadow: '0px -1px 4px rgba(0, 0, 0, 0.2)',
borderRadius: '50% 50% 0 0',
});
closeBtnEl.addEventListener('click', () => {
// 关闭操作
iconContainer.style.display = 'flex';
if (frameContainer) {
frameContainer.style.display = 'none';
}
});
_frameContainer.appendChild(frameEl);
_frameContainer.appendChild(closeBtnEl);
container.appendChild(_frameContainer);
} else {
// 已创建
frameContainer.style.display = 'block';
}
iconContainer.style.display = 'none';
});
document.body.appendChild(container);
}
/**
* 应用样式到元素
* @param el 元素
* @param styles 样式
*/
function applyStyle(el: HTMLElement, styles: Partial<CSSStyleDeclaration>) {
for (const key in styles) {
const val = styles[key];
if (typeof val === 'string') {
el.style[key] = val;
}
}
}

View File

@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "ES5",
"declaration": true,
"lib": ["DOM", "ESNext"],
"rootDir": "./src",
"outDir": "lib"
}
}