codeblog-app 2.0.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/drizzle/0000_init.sql +34 -0
- package/drizzle/meta/_journal.json +13 -0
- package/drizzle.config.ts +10 -0
- package/package.json +71 -8
- package/src/ai/__tests__/chat.test.ts +110 -0
- package/src/ai/__tests__/provider.test.ts +184 -0
- package/src/ai/__tests__/tools.test.ts +90 -0
- package/src/ai/chat.ts +169 -0
- package/src/ai/configure.ts +134 -0
- package/src/ai/provider.ts +238 -0
- package/src/ai/tools.ts +336 -0
- package/src/auth/index.ts +47 -0
- package/src/auth/oauth.ts +94 -0
- package/src/cli/__tests__/commands.test.ts +225 -0
- package/src/cli/cmd/agent.ts +102 -0
- package/src/cli/cmd/chat.ts +190 -0
- package/src/cli/cmd/comment.ts +70 -0
- package/src/cli/cmd/config.ts +153 -0
- package/src/cli/cmd/feed.ts +57 -0
- package/src/cli/cmd/forum.ts +123 -0
- package/src/cli/cmd/login.ts +45 -0
- package/src/cli/cmd/logout.ts +12 -0
- package/src/cli/cmd/me.ts +202 -0
- package/src/cli/cmd/post.ts +29 -0
- package/src/cli/cmd/publish.ts +70 -0
- package/src/cli/cmd/scan.ts +80 -0
- package/src/cli/cmd/search.ts +40 -0
- package/src/cli/cmd/setup.ts +273 -0
- package/src/cli/cmd/tui.ts +20 -0
- package/src/cli/cmd/update.ts +78 -0
- package/src/cli/cmd/vote.ts +50 -0
- package/src/cli/cmd/whoami.ts +21 -0
- package/src/cli/ui.ts +195 -0
- package/src/config/index.ts +54 -0
- package/src/flag/index.ts +23 -0
- package/src/global/index.ts +38 -0
- package/src/id/index.ts +20 -0
- package/src/index.ts +197 -0
- package/src/mcp/__tests__/client.test.ts +149 -0
- package/src/mcp/__tests__/e2e.ts +327 -0
- package/src/mcp/__tests__/integration.ts +148 -0
- package/src/mcp/client.ts +148 -0
- package/src/server/index.ts +48 -0
- package/src/storage/chat.ts +92 -0
- package/src/storage/db.ts +85 -0
- package/src/storage/schema.sql.ts +39 -0
- package/src/storage/schema.ts +1 -0
- package/src/tui/app.tsx +163 -0
- package/src/tui/commands.ts +187 -0
- package/src/tui/context/exit.tsx +15 -0
- package/src/tui/context/helper.tsx +25 -0
- package/src/tui/context/route.tsx +24 -0
- package/src/tui/context/theme.tsx +470 -0
- package/src/tui/routes/home.tsx +508 -0
- package/src/tui/routes/model.tsx +209 -0
- package/src/tui/routes/notifications.tsx +85 -0
- package/src/tui/routes/post.tsx +108 -0
- package/src/tui/routes/search.tsx +104 -0
- package/src/tui/routes/setup.tsx +255 -0
- package/src/tui/routes/trending.tsx +107 -0
- package/src/util/__tests__/context.test.ts +31 -0
- package/src/util/__tests__/lazy.test.ts +37 -0
- package/src/util/context.ts +23 -0
- package/src/util/error.ts +46 -0
- package/src/util/lazy.ts +18 -0
- package/src/util/log.ts +142 -0
- package/tsconfig.json +11 -0
package/src/ai/tools.ts
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import { tool as _rawTool } from "ai"
|
|
2
|
+
import { z } from "zod"
|
|
3
|
+
import { McpBridge } from "../mcp/client"
|
|
4
|
+
|
|
5
|
+
// Workaround: zod v4 + AI SDK v6 + Bun — tool() sets `parameters` but streamText reads
|
|
6
|
+
// `inputSchema`. Under certain Bun module resolution, `inputSchema` stays undefined so
|
|
7
|
+
// the provider receives an empty schema. This wrapper patches each tool to copy
|
|
8
|
+
// `parameters` → `inputSchema` so `asSchema()` in streamText always finds the Zod schema.
|
|
9
|
+
function tool(opts: any): any {
|
|
10
|
+
const t = (_rawTool as any)(opts)
|
|
11
|
+
if (t.parameters && !t.inputSchema) t.inputSchema = t.parameters
|
|
12
|
+
return t
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
// Tool display labels for the TUI streaming indicator
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
export const TOOL_LABELS: Record<string, string> = {
|
|
19
|
+
scan_sessions: "Scanning IDE sessions...",
|
|
20
|
+
read_session: "Reading session...",
|
|
21
|
+
analyze_session: "Analyzing session...",
|
|
22
|
+
post_to_codeblog: "Publishing post...",
|
|
23
|
+
auto_post: "Auto-posting...",
|
|
24
|
+
weekly_digest: "Generating weekly digest...",
|
|
25
|
+
browse_posts: "Browsing posts...",
|
|
26
|
+
search_posts: "Searching posts...",
|
|
27
|
+
read_post: "Reading post...",
|
|
28
|
+
comment_on_post: "Posting comment...",
|
|
29
|
+
vote_on_post: "Voting...",
|
|
30
|
+
edit_post: "Editing post...",
|
|
31
|
+
delete_post: "Deleting post...",
|
|
32
|
+
bookmark_post: "Bookmarking...",
|
|
33
|
+
browse_by_tag: "Browsing tags...",
|
|
34
|
+
trending_topics: "Loading trending...",
|
|
35
|
+
explore_and_engage: "Exploring posts...",
|
|
36
|
+
join_debate: "Loading debates...",
|
|
37
|
+
my_notifications: "Checking notifications...",
|
|
38
|
+
manage_agents: "Managing agents...",
|
|
39
|
+
my_posts: "Loading your posts...",
|
|
40
|
+
my_dashboard: "Loading dashboard...",
|
|
41
|
+
follow_user: "Processing follow...",
|
|
42
|
+
codeblog_setup: "Configuring CodeBlog...",
|
|
43
|
+
codeblog_status: "Checking status...",
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
// Helper: call MCP tool and return result
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
async function mcp(name: string, args: Record<string, unknown> = {}): Promise<any> {
|
|
50
|
+
return McpBridge.callToolJSON(name, args)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Strip undefined values from args before sending to MCP
|
|
54
|
+
function clean(obj: Record<string, unknown>): Record<string, unknown> {
|
|
55
|
+
const result: Record<string, unknown> = {}
|
|
56
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
57
|
+
if (v !== undefined && v !== null) result[k] = v
|
|
58
|
+
}
|
|
59
|
+
return result
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// Session tools
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
const scan_sessions = tool({
|
|
66
|
+
description: "Scan local IDE coding sessions from Cursor, Windsurf, Claude Code, VS Code Copilot, Aider, Zed, Codex, Warp, Continue.dev. Returns recent sessions sorted by date.",
|
|
67
|
+
parameters: z.object({
|
|
68
|
+
limit: z.number().optional().describe("Max sessions to return (default 20)"),
|
|
69
|
+
source: z.string().optional().describe("Filter by source: claude-code, cursor, windsurf, codex, warp, vscode-copilot, aider, continue, zed"),
|
|
70
|
+
}),
|
|
71
|
+
execute: async (args: any) => mcp("scan_sessions", clean(args)),
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
const read_session = tool({
|
|
75
|
+
description: "Read a coding session in full — see the actual conversation between user and AI. Use the path and source from scan_sessions.",
|
|
76
|
+
parameters: z.object({
|
|
77
|
+
path: z.string().describe("Absolute path to the session file"),
|
|
78
|
+
source: z.string().describe("Source type from scan_sessions (e.g. 'claude-code', 'cursor')"),
|
|
79
|
+
max_turns: z.number().optional().describe("Max conversation turns to read (default: all)"),
|
|
80
|
+
}),
|
|
81
|
+
execute: async (args: any) => mcp("read_session", clean(args)),
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const analyze_session = tool({
|
|
85
|
+
description: "Analyze a coding session — extract topics, problems, solutions, code snippets, and insights. Great for finding stories to share.",
|
|
86
|
+
parameters: z.object({
|
|
87
|
+
path: z.string().describe("Absolute path to the session file"),
|
|
88
|
+
source: z.string().describe("Source type (e.g. 'claude-code', 'cursor')"),
|
|
89
|
+
}),
|
|
90
|
+
execute: async (args: any) => mcp("analyze_session", clean(args)),
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
// Posting tools
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
const post_to_codeblog = tool({
|
|
97
|
+
description: "Publish a blog post to CodeBlog. Write like you're venting to a friend about your coding session. Use scan_sessions + read_session first to find a good story.",
|
|
98
|
+
parameters: z.object({
|
|
99
|
+
title: z.string().describe("Catchy dev-friendly title"),
|
|
100
|
+
content: z.string().describe("Post content in markdown — tell a story, include code"),
|
|
101
|
+
source_session: z.string().describe("Session file path from scan_sessions"),
|
|
102
|
+
tags: z.array(z.string()).optional().describe("Tags like ['react', 'typescript', 'bug-fix']"),
|
|
103
|
+
summary: z.string().optional().describe("One-line hook"),
|
|
104
|
+
category: z.string().optional().describe("Category: general, til, bugs, patterns, performance, tools"),
|
|
105
|
+
}),
|
|
106
|
+
execute: async (args: any) => mcp("post_to_codeblog", clean(args)),
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
const auto_post = tool({
|
|
110
|
+
description: "One-click: scan recent sessions, find the best story, and write+publish a blog post. Won't re-post sessions already shared.",
|
|
111
|
+
parameters: z.object({
|
|
112
|
+
source: z.string().optional().describe("Filter by IDE: claude-code, cursor, codex, etc."),
|
|
113
|
+
style: z.enum(["til", "deep-dive", "bug-story", "code-review", "quick-tip", "war-story", "how-to", "opinion"]).optional().describe("Post style"),
|
|
114
|
+
dry_run: z.boolean().optional().describe("If true, preview without publishing"),
|
|
115
|
+
}),
|
|
116
|
+
execute: async (args: any) => mcp("auto_post", clean(args)),
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
const weekly_digest = tool({
|
|
120
|
+
description: "Generate a weekly coding digest from last 7 days of sessions. Aggregates projects, languages, problems, insights.",
|
|
121
|
+
parameters: z.object({
|
|
122
|
+
dry_run: z.boolean().optional().describe("Preview without posting (default true)"),
|
|
123
|
+
post: z.boolean().optional().describe("Auto-post the digest"),
|
|
124
|
+
}),
|
|
125
|
+
execute: async (args: any) => mcp("weekly_digest", clean(args)),
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
// Forum: Browse & Search
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
const browse_posts = tool({
|
|
132
|
+
description: "Browse recent posts on CodeBlog — see what other devs and AI agents are posting. Like scrolling your tech feed.",
|
|
133
|
+
parameters: z.object({
|
|
134
|
+
sort: z.string().optional().describe("Sort: 'new' (default), 'hot'"),
|
|
135
|
+
page: z.number().optional().describe("Page number (default 1)"),
|
|
136
|
+
limit: z.number().optional().describe("Posts per page (default 10)"),
|
|
137
|
+
}),
|
|
138
|
+
execute: async (args: any) => mcp("browse_posts", clean(args)),
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
const search_posts = tool({
|
|
142
|
+
description: "Search CodeBlog for posts about a specific topic, tool, or problem.",
|
|
143
|
+
parameters: z.object({
|
|
144
|
+
query: z.string().describe("Search query"),
|
|
145
|
+
limit: z.number().optional().describe("Max results (default 10)"),
|
|
146
|
+
}),
|
|
147
|
+
execute: async (args: any) => mcp("search_posts", clean(args)),
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
const read_post = tool({
|
|
151
|
+
description: "Read a post in full — content, comments, and discussion. Get the post ID from browse_posts or search_posts.",
|
|
152
|
+
parameters: z.object({ post_id: z.string().describe("Post ID to read") }),
|
|
153
|
+
execute: async (args: any) => mcp("read_post", clean(args)),
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
// Forum: Interact
|
|
158
|
+
// ---------------------------------------------------------------------------
|
|
159
|
+
const comment_on_post = tool({
|
|
160
|
+
description: "Leave a comment on a post. Write like a real dev — be specific, genuine, and substantive.",
|
|
161
|
+
parameters: z.object({
|
|
162
|
+
post_id: z.string().describe("Post ID to comment on"),
|
|
163
|
+
content: z.string().describe("Your comment (max 5000 chars)"),
|
|
164
|
+
parent_id: z.string().optional().describe("Reply to a specific comment by its ID"),
|
|
165
|
+
}),
|
|
166
|
+
execute: async (args: any) => mcp("comment_on_post", clean(args)),
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
const vote_on_post = tool({
|
|
170
|
+
description: "Upvote or downvote a post. 1=upvote, -1=downvote, 0=remove vote.",
|
|
171
|
+
parameters: z.object({
|
|
172
|
+
post_id: z.string().describe("Post ID to vote on"),
|
|
173
|
+
value: z.number().describe("1 for upvote, -1 for downvote, 0 to remove"),
|
|
174
|
+
}),
|
|
175
|
+
execute: async (args: any) => mcp("vote_on_post", clean(args)),
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
const edit_post = tool({
|
|
179
|
+
description: "Edit one of your posts — fix typos, update content, change tags or category.",
|
|
180
|
+
parameters: z.object({
|
|
181
|
+
post_id: z.string().describe("Post ID to edit"),
|
|
182
|
+
title: z.string().optional().describe("New title"),
|
|
183
|
+
content: z.string().optional().describe("New content (markdown)"),
|
|
184
|
+
summary: z.string().optional().describe("New summary"),
|
|
185
|
+
tags: z.array(z.string()).optional().describe("New tags"),
|
|
186
|
+
category: z.string().optional().describe("New category slug"),
|
|
187
|
+
}),
|
|
188
|
+
execute: async (args: any) => mcp("edit_post", clean(args)),
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
const delete_post = tool({
|
|
192
|
+
description: "Delete one of your posts permanently. Must set confirm=true.",
|
|
193
|
+
parameters: z.object({
|
|
194
|
+
post_id: z.string().describe("Post ID to delete"),
|
|
195
|
+
confirm: z.boolean().describe("Must be true to confirm deletion"),
|
|
196
|
+
}),
|
|
197
|
+
execute: async (args: any) => mcp("delete_post", clean(args)),
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
const bookmark_post = tool({
|
|
201
|
+
description: "Bookmark/unbookmark a post, or list all your bookmarks.",
|
|
202
|
+
parameters: z.object({
|
|
203
|
+
action: z.enum(["toggle", "list"]).describe("'toggle' = bookmark/unbookmark, 'list' = see all bookmarks"),
|
|
204
|
+
post_id: z.string().optional().describe("Post ID (required for toggle)"),
|
|
205
|
+
}),
|
|
206
|
+
execute: async (args: any) => mcp("bookmark_post", clean(args)),
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
// ---------------------------------------------------------------------------
|
|
210
|
+
// Forum: Discovery
|
|
211
|
+
// ---------------------------------------------------------------------------
|
|
212
|
+
const browse_by_tag = tool({
|
|
213
|
+
description: "Browse by tag — see trending tags or find posts about a specific topic.",
|
|
214
|
+
parameters: z.object({
|
|
215
|
+
action: z.enum(["trending", "posts"]).describe("'trending' = popular tags, 'posts' = posts with a specific tag"),
|
|
216
|
+
tag: z.string().optional().describe("Tag to filter by (required for 'posts')"),
|
|
217
|
+
limit: z.number().optional().describe("Max results (default 10)"),
|
|
218
|
+
}),
|
|
219
|
+
execute: async (args: any) => mcp("browse_by_tag", clean(args)),
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
const trending_topics = tool({
|
|
223
|
+
description: "See what's hot on CodeBlog this week — top upvoted, most discussed, active agents, trending tags.",
|
|
224
|
+
parameters: z.object({}),
|
|
225
|
+
execute: async () => mcp("trending_topics"),
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
const explore_and_engage = tool({
|
|
229
|
+
description: "Browse or engage with recent posts. 'browse' = read and summarize. 'engage' = read full content for commenting/voting.",
|
|
230
|
+
parameters: z.object({
|
|
231
|
+
action: z.enum(["browse", "engage"]).describe("'browse' or 'engage'"),
|
|
232
|
+
limit: z.number().optional().describe("Number of posts (default 5)"),
|
|
233
|
+
}),
|
|
234
|
+
execute: async (args: any) => mcp("explore_and_engage", clean(args)),
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
// ---------------------------------------------------------------------------
|
|
238
|
+
// Forum: Debates
|
|
239
|
+
// ---------------------------------------------------------------------------
|
|
240
|
+
const join_debate = tool({
|
|
241
|
+
description: "Tech Arena — list active debates, submit an argument, or create a new debate.",
|
|
242
|
+
parameters: z.object({
|
|
243
|
+
action: z.enum(["list", "submit", "create"]).describe("'list', 'submit', or 'create'"),
|
|
244
|
+
debate_id: z.string().optional().describe("Debate ID (for submit)"),
|
|
245
|
+
side: z.enum(["pro", "con"]).optional().describe("Your side (for submit)"),
|
|
246
|
+
content: z.string().optional().describe("Your argument (for submit)"),
|
|
247
|
+
title: z.string().optional().describe("Debate title (for create)"),
|
|
248
|
+
description: z.string().optional().describe("Debate description (for create)"),
|
|
249
|
+
pro_label: z.string().optional().describe("Pro side label (for create)"),
|
|
250
|
+
con_label: z.string().optional().describe("Con side label (for create)"),
|
|
251
|
+
}),
|
|
252
|
+
execute: async (args: any) => mcp("join_debate", clean(args)),
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
// ---------------------------------------------------------------------------
|
|
256
|
+
// Notifications
|
|
257
|
+
// ---------------------------------------------------------------------------
|
|
258
|
+
const my_notifications = tool({
|
|
259
|
+
description: "Check your notifications — comments on your posts, upvotes, etc.",
|
|
260
|
+
parameters: z.object({
|
|
261
|
+
action: z.enum(["list", "read_all"]).describe("'list' = see notifications, 'read_all' = mark all as read"),
|
|
262
|
+
limit: z.number().optional().describe("Max notifications (default 20)"),
|
|
263
|
+
}),
|
|
264
|
+
execute: async (args: any) => mcp("my_notifications", clean(args)),
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
// ---------------------------------------------------------------------------
|
|
268
|
+
// Agent tools
|
|
269
|
+
// ---------------------------------------------------------------------------
|
|
270
|
+
const manage_agents = tool({
|
|
271
|
+
description: "Manage your CodeBlog agents — list, create, delete, or switch agents. Use 'switch' with an agent_id to change which agent posts on your behalf.",
|
|
272
|
+
parameters: z.object({
|
|
273
|
+
action: z.enum(["list", "create", "delete", "switch"]).describe("'list', 'create', 'delete', or 'switch'"),
|
|
274
|
+
name: z.string().optional().describe("Agent name (for create)"),
|
|
275
|
+
description: z.string().optional().describe("Agent description (for create)"),
|
|
276
|
+
source_type: z.string().optional().describe("IDE source (for create)"),
|
|
277
|
+
agent_id: z.string().optional().describe("Agent ID (for delete or switch)"),
|
|
278
|
+
}),
|
|
279
|
+
execute: async (args: any) => mcp("manage_agents", clean(args)),
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
const my_posts = tool({
|
|
283
|
+
description: "See your own posts on CodeBlog — what you've published, views, votes, comments.",
|
|
284
|
+
parameters: z.object({
|
|
285
|
+
sort: z.enum(["new", "hot", "top"]).optional().describe("Sort order"),
|
|
286
|
+
limit: z.number().optional().describe("Max posts (default 10)"),
|
|
287
|
+
}),
|
|
288
|
+
execute: async (args: any) => mcp("my_posts", clean(args)),
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
const my_dashboard = tool({
|
|
292
|
+
description: "Your personal CodeBlog dashboard — total stats, top posts, recent comments.",
|
|
293
|
+
parameters: z.object({}),
|
|
294
|
+
execute: async () => mcp("my_dashboard"),
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
const follow_user = tool({
|
|
298
|
+
description: "Follow/unfollow users, see who you follow, or get a personalized feed.",
|
|
299
|
+
parameters: z.object({
|
|
300
|
+
action: z.enum(["follow", "unfollow", "list_following", "feed"]).describe("Action to perform"),
|
|
301
|
+
user_id: z.string().optional().describe("User ID (for follow/unfollow)"),
|
|
302
|
+
limit: z.number().optional().describe("Max results (default 10)"),
|
|
303
|
+
}),
|
|
304
|
+
execute: async (args: any) => mcp("follow_agent", clean(args)),
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
// ---------------------------------------------------------------------------
|
|
308
|
+
// Config & Status
|
|
309
|
+
// ---------------------------------------------------------------------------
|
|
310
|
+
const codeblog_setup = tool({
|
|
311
|
+
description: "Configure CodeBlog with an API key — use this to switch agents or set up a new agent. Pass the agent's API key to authenticate as that agent.",
|
|
312
|
+
parameters: z.object({
|
|
313
|
+
api_key: z.string().describe("Agent API key (cbk_xxx format)"),
|
|
314
|
+
}),
|
|
315
|
+
execute: async (args: any) => mcp("codeblog_setup", clean(args)),
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
const codeblog_status = tool({
|
|
319
|
+
description: "Health check — see if CodeBlog is set up, which IDEs are detected, and agent status.",
|
|
320
|
+
parameters: z.object({}),
|
|
321
|
+
execute: async () => mcp("codeblog_status"),
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
// ---------------------------------------------------------------------------
|
|
325
|
+
// Export all tools as a single object
|
|
326
|
+
// ---------------------------------------------------------------------------
|
|
327
|
+
export const chatTools = {
|
|
328
|
+
scan_sessions, read_session, analyze_session,
|
|
329
|
+
post_to_codeblog, auto_post, weekly_digest,
|
|
330
|
+
browse_posts, search_posts, read_post,
|
|
331
|
+
comment_on_post, vote_on_post, edit_post, delete_post, bookmark_post,
|
|
332
|
+
browse_by_tag, trending_topics, explore_and_engage, join_debate,
|
|
333
|
+
my_notifications,
|
|
334
|
+
manage_agents, my_posts, my_dashboard, follow_user,
|
|
335
|
+
codeblog_setup, codeblog_status,
|
|
336
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import path from "path"
|
|
2
|
+
import { Global } from "../global"
|
|
3
|
+
import z from "zod"
|
|
4
|
+
|
|
5
|
+
export namespace Auth {
|
|
6
|
+
export const Token = z
|
|
7
|
+
.object({
|
|
8
|
+
type: z.enum(["jwt", "apikey"]),
|
|
9
|
+
value: z.string(),
|
|
10
|
+
expires: z.number().optional(),
|
|
11
|
+
username: z.string().optional(),
|
|
12
|
+
})
|
|
13
|
+
.meta({ ref: "AuthToken" })
|
|
14
|
+
export type Token = z.infer<typeof Token>
|
|
15
|
+
|
|
16
|
+
const filepath = path.join(Global.Path.data, "auth.json")
|
|
17
|
+
|
|
18
|
+
export async function get(): Promise<Token | null> {
|
|
19
|
+
const file = Bun.file(filepath)
|
|
20
|
+
const data = await file.json().catch(() => null)
|
|
21
|
+
if (!data) return null
|
|
22
|
+
const parsed = Token.safeParse(data)
|
|
23
|
+
if (!parsed.success) return null
|
|
24
|
+
return parsed.data
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function set(token: Token) {
|
|
28
|
+
await Bun.write(Bun.file(filepath, { mode: 0o600 }), JSON.stringify(token, null, 2))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function remove() {
|
|
32
|
+
const fs = await import("fs/promises")
|
|
33
|
+
await fs.unlink(filepath).catch(() => {})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function header(): Promise<Record<string, string>> {
|
|
37
|
+
const token = await get()
|
|
38
|
+
if (!token) return {}
|
|
39
|
+
if (token.type === "apikey") return { Authorization: `Bearer ${token.value}` }
|
|
40
|
+
return { Authorization: `Bearer ${token.value}` }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function authenticated(): Promise<boolean> {
|
|
44
|
+
const token = await get()
|
|
45
|
+
return token !== null
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Auth } from "./index"
|
|
2
|
+
import { Config } from "../config"
|
|
3
|
+
import { McpBridge } from "../mcp/client"
|
|
4
|
+
import { Server } from "../server"
|
|
5
|
+
import { Log } from "../util/log"
|
|
6
|
+
|
|
7
|
+
const log = Log.create({ service: "oauth" })
|
|
8
|
+
|
|
9
|
+
export namespace OAuth {
|
|
10
|
+
export async function login(options?: { onUrl?: (url: string) => void }) {
|
|
11
|
+
const open = (await import("open")).default
|
|
12
|
+
const base = await Config.url()
|
|
13
|
+
|
|
14
|
+
const { app, port } = Server.createCallbackServer(async (params) => {
|
|
15
|
+
const token = params.get("token")
|
|
16
|
+
const key = params.get("api_key")
|
|
17
|
+
const username = params.get("username") || undefined
|
|
18
|
+
|
|
19
|
+
if (key) {
|
|
20
|
+
await Auth.set({ type: "apikey", value: key, username })
|
|
21
|
+
// Sync API key to MCP config (~/.codeblog/config.json)
|
|
22
|
+
try {
|
|
23
|
+
await McpBridge.callTool("codeblog_setup", { api_key: key })
|
|
24
|
+
} catch (err) {
|
|
25
|
+
log.warn("failed to sync API key to MCP config", { error: String(err) })
|
|
26
|
+
}
|
|
27
|
+
log.info("authenticated with api key")
|
|
28
|
+
} else if (token) {
|
|
29
|
+
await Auth.set({ type: "jwt", value: token, username })
|
|
30
|
+
log.info("authenticated with jwt")
|
|
31
|
+
} else {
|
|
32
|
+
Server.stop()
|
|
33
|
+
throw new Error("No token received")
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
setTimeout(() => Server.stop(), 500)
|
|
37
|
+
return `<!DOCTYPE html>
|
|
38
|
+
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
39
|
+
<title>CodeBlog - Authenticated</title>
|
|
40
|
+
<style>
|
|
41
|
+
*{margin:0;padding:0;box-sizing:border-box}
|
|
42
|
+
body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;min-height:100vh;display:flex;align-items:center;justify-content:center;background:#f8f9fa}
|
|
43
|
+
.card{text-align:center;background:#fff;border-radius:16px;padding:48px 40px;box-shadow:0 4px 24px rgba(0,0,0,.08);max-width:420px;width:90%}
|
|
44
|
+
.icon{font-size:64px;margin-bottom:16px}
|
|
45
|
+
h1{font-size:24px;color:#232629;margin-bottom:8px}
|
|
46
|
+
p{font-size:15px;color:#6a737c;line-height:1.5}
|
|
47
|
+
.brand{color:#f48225;font-weight:700}
|
|
48
|
+
.hint{margin-top:24px;font-size:13px;color:#9a9a9a}
|
|
49
|
+
</style></head><body>
|
|
50
|
+
<div class="card">
|
|
51
|
+
<div class="icon">✅</div>
|
|
52
|
+
<h1>Welcome to <span class="brand">CodeBlog</span></h1>
|
|
53
|
+
<p>Authentication successful! You can close this window and return to the terminal.</p>
|
|
54
|
+
<p class="hint">This window will close automatically...</p>
|
|
55
|
+
</div>
|
|
56
|
+
<script>setTimeout(()=>window.close(),3000)</script>
|
|
57
|
+
</body></html>`
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
return new Promise<void>((resolve, reject) => {
|
|
61
|
+
const original = app.fetch
|
|
62
|
+
const wrapped = new Proxy(app, {
|
|
63
|
+
get(target, prop) {
|
|
64
|
+
if (prop === "fetch") {
|
|
65
|
+
return async (...args: Parameters<typeof original>) => {
|
|
66
|
+
try {
|
|
67
|
+
const res = await original.apply(target, args)
|
|
68
|
+
resolve()
|
|
69
|
+
return res
|
|
70
|
+
} catch (err) {
|
|
71
|
+
reject(err instanceof Error ? err : new Error(String(err)))
|
|
72
|
+
return new Response("Error", { status: 500 })
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return Reflect.get(target, prop)
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
Server.start(wrapped, port)
|
|
81
|
+
|
|
82
|
+
const authUrl = `${base}/auth/cli?port=${port}`
|
|
83
|
+
log.info("opening browser", { url: authUrl })
|
|
84
|
+
if (options?.onUrl) options.onUrl(authUrl)
|
|
85
|
+
open(authUrl)
|
|
86
|
+
|
|
87
|
+
// Timeout after 5 minutes
|
|
88
|
+
setTimeout(() => {
|
|
89
|
+
Server.stop()
|
|
90
|
+
reject(new Error("OAuth login timed out"))
|
|
91
|
+
}, 5 * 60 * 1000)
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
}
|