🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
✨ feat(cli): add `lh read <url>` to read shared topics via the API (#21514)
Arvin Xue3fa8681d9a5
diff --git a/apps/cli/src/commands/read.test.ts b/apps/cli/src/commands/read.test.tsnew file mode 100644index 00000000000..26a28e5982d--- /dev/null+++ b/apps/cli/src/commands/read.test.ts@@ -0,0 +1,91 @@+import { Command } from 'commander';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { log } from '../utils/logger';+import { parseShareTopicUrl, registerReadCommand } from './read';++const { mockGetTrpcClient, mockQuery } = vi.hoisted(() => ({+ mockGetTrpcClient: vi.fn(),+ mockQuery: vi.fn(),+}));++vi.mock('../api/client', () => ({ getTrpcClient: mockGetTrpcClient }));+vi.mock('../utils/logger', () => ({+ log: { debug: vi.fn(), error: vi.fn(), info: vi.fn(), warn: vi.fn() },+ setVerbose: vi.fn(),+}));++describe('parseShareTopicUrl', () => {+ it.each([+ ['https://lobehub.com/share/t/Fy1SWilu', 'Fy1SWilu'],+ ['https://lobehub.com/share/t/Fy1SWilu/llm.txt', 'Fy1SWilu'],+ ['https://lobehub.com/share/t/Fy1SWilu/llm.txt?format=text', 'Fy1SWilu'],+ [' https://www.lobehub.com/share/t/Fy1SWilu?x=1 ', 'Fy1SWilu'],+ ])('extracts the id from %s', (input, expected) => {+ expect(parseShareTopicUrl(input)).toBe(expected);+ });++ it.each(['', 'Fy1SWilu', 'https://lobehub.com/topic/abc', 'https://'])('rejects %j', (input) => {+ expect(parseShareTopicUrl(input)).toBeUndefined();+ });+});++describe('read', () => {+ let exitSpy: ReturnType<typeof vi.spyOn>;+ let consoleSpy: ReturnType<typeof vi.spyOn>;++ beforeEach(() => {+ vi.clearAllMocks();+ exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => {}) as any);+ consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});+ mockGetTrpcClient.mockResolvedValue({+ share: { getSharedTopicText: { query: mockQuery } },+ });+ });++ afterEach(() => {+ exitSpy.mockRestore();+ consoleSpy.mockRestore();+ });++ const run = async (...args: string[]) => {+ const program = new Command().exitOverride();+ registerReadCommand(program);+ await program.parseAsync(['node', 'test', 'read', ...args]);+ };++ it('prints the llm.txt layout for a share URL', async () => {+ mockQuery.mockResolvedValue({ text: 'user: hi', title: 'My Topic' });++ await run('https://lobehub.com/share/t/Fy1SWilu');++ expect(mockQuery).toHaveBeenCalledWith({ shareId: 'Fy1SWilu' });+ expect(consoleSpy).toHaveBeenCalledWith('# My Topic\n\nuser: hi');+ });++ it('omits the heading when the share has no title', async () => {+ mockQuery.mockResolvedValue({ text: 'user: hi', title: null });++ await run('https://lobehub.com/share/t/Fy1SWilu');++ expect(consoleSpy).toHaveBeenCalledWith('user: hi');+ });++ it('outputs JSON with --json', async () => {+ mockQuery.mockResolvedValue({ text: 'user: hi', title: 'T' });++ await run('https://lobehub.com/share/t/Fy1SWilu', '--json');++ expect(consoleSpy).toHaveBeenCalledWith(+ JSON.stringify({ text: 'user: hi', title: 'T' }, null, 2),+ );+ });++ it('fails without calling the server for an unsupported link', async () => {+ await run('https://lobehub.com/topic/abc');++ expect(log.error).toHaveBeenCalled();+ expect(exitSpy).toHaveBeenCalledWith(1);+ expect(mockQuery).not.toHaveBeenCalled();+ });+});diff --git a/apps/cli/src/commands/read.ts b/apps/cli/src/commands/read.tsnew file mode 100644index 00000000000..b370c06ccdf--- /dev/null+++ b/apps/cli/src/commands/read.ts@@ -0,0 +1,44 @@+import type { Command } from 'commander';++import { getTrpcClient } from '../api/client';+import { log } from '../utils/logger';++/** Extract the share id from a `.../share/t/<id>[/llm.txt]` URL. */+export const parseShareTopicUrl = (input: string): string | undefined => {+ try {+ const match = new URL(input.trim()).pathname.match(/\/share\/t\/([^/]+)/);+ return match ? decodeURIComponent(match[1]) : undefined;+ } catch {+ return undefined;+ }+};++export function registerReadCommand(program: Command) {+ program+ .command('read <url>')+ .description(+ 'Read a LobeHub link as plain text via the API (e.g. a shared topic: https://lobehub.com/share/t/<id>), bypassing the Cloudflare-protected web page',+ )+ .option('--json', 'Output JSON ({ title, text })')+ .action(async (url: string, options: { json?: boolean }) => {+ const shareId = parseShareTopicUrl(url);+ if (!shareId) {+ log.error(+ `Unsupported link "${url}". Supported: shared topic (https://lobehub.com/share/t/<id>).`,+ );+ process.exit(1);+ return;+ }++ const client = await getTrpcClient();+ const { text, title } = await client.share.getSharedTopicText.query({ shareId });++ if (options.json) {+ console.log(JSON.stringify({ text, title }, null, 2));+ return;+ }++ // Same layout as the web `share/t/:id/llm.txt` route.+ console.log([title ? `# ${title}` : '', text].filter(Boolean).join('\n\n'));+ });+}diff --git a/apps/cli/src/program.ts b/apps/cli/src/program.tsindex 2e9924ff5b8..5e972c6cd79 100644--- a/apps/cli/src/program.ts+++ b/apps/cli/src/program.ts@@ -31,6 +31,7 @@ import { registerNotifyCommand } from './commands/notify';import { registerPluginCommand } from './commands/plugin';import { registerProjectCommand } from './commands/project';import { registerProviderCommand } from './commands/provider';+import { registerReadCommand } from './commands/read';import { registerSearchCommand } from './commands/search';import { registerSessionGroupCommand } from './commands/session-group';import { registerSkillCommand } from './commands/skill';@@ -86,6 +87,7 @@ export function createProgram() {registerDoctorCommand(program);registerDocCommand(program);registerSearchCommand(program);+ registerReadCommand(program);registerKbCommand(program);registerMemoryCommand(program);registerAgentCommand(program);