forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔧 chore(workflow): Add agent/plugin readme list sync workflow
- Loading branch information
1 parent
438b7d0
commit 81b3b2a
Showing
11 changed files
with
228 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export interface DataItem { | ||
author: string; | ||
createAt: string; | ||
homepage: string; | ||
identifier: string; | ||
meta: { avatar: string; description: string; tags: string[]; title: string }; | ||
} | ||
|
||
export const MARKET_URL = 'https://chat-preview.lobehub.com/market'; | ||
export const AGENT_EN_URL = 'https://chat-agents.lobehub.com/index.json'; | ||
export const AGENT_CN_URL = 'https://chat-agents.lobehub.com/index.zh-CN.json'; | ||
export const AGENT_REPO = 'https://github.com/lobehub/lobe-chat-agents'; | ||
export const PLUGIN_EN_URL = 'https://chat-plugins.lobehub.com/index.json'; | ||
export const PLUGIN_CN_URL = 'https://chat-plugins.lobehub.com/index.zh-CN.json'; | ||
export const PLUGIN_REPO = 'https://github.com/lobehub/lobe-chat-plugins'; | ||
|
||
export const AGENT_SPLIT = '<!-- AGENT LIST -->'; | ||
export const PLUGIN_SPLIT = '<!-- PLUGIN LIST -->'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { consola } from 'consola'; | ||
|
||
import syncAgentIndex from './syncAgentIndex'; | ||
import syncPluginIndex from './syncPluginIndex'; | ||
|
||
const runSync = async () => { | ||
consola.start('Start sync readme workflow...'); | ||
await syncAgentIndex(); | ||
await syncPluginIndex(); | ||
}; | ||
|
||
runSync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { consola } from 'consola'; | ||
import { markdownTable } from 'markdown-table'; | ||
import qs from 'query-string'; | ||
|
||
import { AGENT_REPO, AGENT_SPLIT, DataItem, MARKET_URL } from './const'; | ||
import { fetchAgentIndex, genLink, readReadme, updateReadme, writeReadme } from './utlis'; | ||
|
||
const genAgentTable = (data: DataItem[], lang: string) => { | ||
const isCN = lang === 'zh-CN'; | ||
const content = data.slice(0, 4).map((item) => [ | ||
[ | ||
genLink( | ||
item.meta.title, | ||
qs.stringifyUrl({ | ||
query: { agent: item.identifier }, | ||
url: MARKET_URL, | ||
}), | ||
), | ||
`<sup>By **${genLink(item.author, item.homepage)}** on **${item.createAt}**</sup>`, | ||
].join('<br/>'), | ||
[ | ||
item.meta.description, | ||
item.meta.tags | ||
.filter(Boolean) | ||
.map((tag) => `\`${tag}\``) | ||
.join(' '), | ||
].join('<br/>'), | ||
]); | ||
return markdownTable([ | ||
[isCN ? '最近新增' : 'Recent Submits', isCN ? '助手说明' : 'Description'], | ||
...content, | ||
]); | ||
}; | ||
|
||
const runAgentTable = async (lang: string) => { | ||
const data = await fetchAgentIndex(lang); | ||
const md = readReadme(lang); | ||
const mdTable = genAgentTable(data, lang); | ||
const newMd = updateReadme( | ||
AGENT_SPLIT, | ||
md, | ||
[mdTable, `> 📊 Total agents: ${genLink(`<kbd>**${data.length}**</kbd> `, AGENT_REPO)}`].join( | ||
'\n\n', | ||
), | ||
); | ||
writeReadme(newMd, lang); | ||
consola.success('Sync agent index success!'); | ||
}; | ||
|
||
export default async () => { | ||
await runAgentTable('en-US'); | ||
await runAgentTable('zh-CN'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { consola } from 'consola'; | ||
import { markdownTable } from 'markdown-table'; | ||
|
||
import { DataItem, PLUGIN_REPO, PLUGIN_SPLIT } from './const'; | ||
import { fetchPluginIndex, genLink, readReadme, updateReadme, writeReadme } from './utlis'; | ||
|
||
const genPluginTable = (data: DataItem[], lang: string) => { | ||
const isCN = lang === 'zh-CN'; | ||
const content = data | ||
.filter((item) => item.author === 'LobeHub') | ||
.map((item) => [genLink(item.meta.title, item.homepage), item.meta.description]); | ||
return markdownTable([ | ||
[isCN ? '官方插件' : 'Official Plugin', isCN ? '插件说明' : 'Description'], | ||
...content, | ||
]); | ||
}; | ||
|
||
const runPluginTable = async (lang: string) => { | ||
const data = await fetchPluginIndex(lang); | ||
const md = readReadme(lang); | ||
const mdTable = genPluginTable(data, lang); | ||
const newMd = updateReadme( | ||
PLUGIN_SPLIT, | ||
md, | ||
[mdTable, `> 📊 Total plugins: ${genLink(`<kbd>**${data.length}**</kbd>`, PLUGIN_REPO)}`].join( | ||
'\n\n', | ||
), | ||
); | ||
writeReadme(newMd, lang); | ||
consola.success('Sync plugin index success!'); | ||
}; | ||
|
||
export default async () => { | ||
await runPluginTable('en-US'); | ||
await runPluginTable('zh-CN'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { readFileSync, writeFileSync } from 'node:fs'; | ||
import { resolve } from 'node:path'; | ||
|
||
import { AGENT_CN_URL, AGENT_EN_URL, PLUGIN_CN_URL, PLUGIN_EN_URL } from './const'; | ||
|
||
const fetchIndex = async (url: string) => { | ||
const res = await fetch(url); | ||
return await res.json(); | ||
}; | ||
|
||
export const fetchAgentIndex = async (lang: string) => { | ||
const isCN = lang === 'zh-CN'; | ||
const url = isCN ? AGENT_CN_URL : AGENT_EN_URL; | ||
const data = await fetchIndex(url); | ||
return data.agents; | ||
}; | ||
|
||
export const fetchPluginIndex = async (lang: string) => { | ||
const isCN = lang === 'zh-CN'; | ||
const url = isCN ? PLUGIN_CN_URL : PLUGIN_EN_URL; | ||
const data = await fetchIndex(url); | ||
return data.plugins; | ||
}; | ||
|
||
export const genLink = (title: string, url: string) => `[${title}](${url})`; | ||
|
||
const getReadmePath = (lang: string) => { | ||
const isCN = lang === 'zh-CN'; | ||
return resolve(__dirname, '../../', isCN ? `./README.zh-CN.md` : `./README.md`); | ||
}; | ||
|
||
export const readReadme = (lang: string): string => { | ||
return readFileSync(getReadmePath(lang), 'utf8'); | ||
}; | ||
|
||
export const writeReadme = (content: string, lang: string) => { | ||
writeFileSync(getReadmePath(lang), content, 'utf8'); | ||
}; | ||
|
||
export const updateReadme = (split: string, md: string, content: string): string => { | ||
const mds = md.split(split); | ||
mds[1] = [' ', content, ' '].join('\n\n'); | ||
|
||
return mds.join(split); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters