work-agent 0.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.
Files changed (245) hide show
  1. package/README.md +234 -0
  2. package/app/(admin)/approvals/page.tsx +16 -0
  3. package/app/(admin)/audit/page.tsx +18 -0
  4. package/app/(admin)/layout.tsx +47 -0
  5. package/app/(admin)/scheduled-tasks/page.tsx +17 -0
  6. package/app/(admin)/settings/page.tsx +46 -0
  7. package/app/(admin)/skills/[name]/page.tsx +378 -0
  8. package/app/(admin)/skills/page.tsx +406 -0
  9. package/app/(admin)/statistics/page.tsx +416 -0
  10. package/app/(admin)/tickets/[id]/page.tsx +348 -0
  11. package/app/(admin)/tickets/new/page.tsx +309 -0
  12. package/app/(admin)/tickets/page.tsx +27 -0
  13. package/app/api/audit/route.ts +30 -0
  14. package/app/api/auth/feishu/callback/route.ts +72 -0
  15. package/app/api/auth/feishu/login/route.ts +17 -0
  16. package/app/api/auth/feishu/sso/route.ts +78 -0
  17. package/app/api/auth/login/route.ts +85 -0
  18. package/app/api/auth/oauth/route.ts +168 -0
  19. package/app/api/config/providers/route.ts +105 -0
  20. package/app/api/config/route.ts +115 -0
  21. package/app/api/config/status/route.ts +56 -0
  22. package/app/api/config/test/route.ts +212 -0
  23. package/app/api/documents/[id]/route.ts +88 -0
  24. package/app/api/documents/route.ts +53 -0
  25. package/app/api/health/route.ts +32 -0
  26. package/app/api/knowledge/[id]/route.ts +152 -0
  27. package/app/api/knowledge/from-session/route.ts +27 -0
  28. package/app/api/knowledge/route.ts +100 -0
  29. package/app/api/market/knowledge/[id]/route.ts +92 -0
  30. package/app/api/market/knowledge/route.ts +130 -0
  31. package/app/api/marketplace/skills/[id]/approve/route.ts +68 -0
  32. package/app/api/marketplace/skills/[id]/certify/route.ts +54 -0
  33. package/app/api/marketplace/skills/[id]/install/route.ts +180 -0
  34. package/app/api/marketplace/skills/[id]/promote-to-system/route.ts +219 -0
  35. package/app/api/marketplace/skills/[id]/rate/route.ts +90 -0
  36. package/app/api/marketplace/skills/[id]/ratings/route.ts +55 -0
  37. package/app/api/marketplace/skills/[id]/reject/route.ts +68 -0
  38. package/app/api/marketplace/skills/[id]/route.ts +177 -0
  39. package/app/api/marketplace/skills/route.ts +235 -0
  40. package/app/api/memory/route.ts +40 -0
  41. package/app/api/my/files/[id]/route.ts +52 -0
  42. package/app/api/my/files/route.ts +230 -0
  43. package/app/api/my/knowledge/route.ts +36 -0
  44. package/app/api/pi-chat/route.ts +443 -0
  45. package/app/api/recommend/route.ts +38 -0
  46. package/app/api/scheduled-tasks/[id]/execute/route.ts +132 -0
  47. package/app/api/scheduled-tasks/[id]/route.ts +165 -0
  48. package/app/api/scheduled-tasks/[id]/toggle/route.ts +53 -0
  49. package/app/api/scheduled-tasks/route.ts +101 -0
  50. package/app/api/sessions/[id]/messages/route.ts +212 -0
  51. package/app/api/sessions/route.ts +101 -0
  52. package/app/api/share/file/[id]/route.ts +37 -0
  53. package/app/api/skills/[name]/execute/route.ts +121 -0
  54. package/app/api/skills/[name]/route.ts +167 -0
  55. package/app/api/skills/create/route.ts +65 -0
  56. package/app/api/skills/generate/route.ts +405 -0
  57. package/app/api/skills/installed/route.ts +151 -0
  58. package/app/api/skills/route.ts +174 -0
  59. package/app/api/skills/translate/route.ts +40 -0
  60. package/app/api/skills/user/[name]/route.ts +159 -0
  61. package/app/api/skills/user/route.ts +90 -0
  62. package/app/api/statistics/route.ts +94 -0
  63. package/app/api/task-executions/[id]/route.ts +34 -0
  64. package/app/api/task-executions/route.ts +29 -0
  65. package/app/api/tickets/[id]/approve/route.ts +129 -0
  66. package/app/api/tickets/[id]/execute/route.ts +201 -0
  67. package/app/api/tickets/[id]/route.ts +127 -0
  68. package/app/api/tickets/route.ts +103 -0
  69. package/app/api/user/skills/route.ts +175 -0
  70. package/app/api/users/route.ts +80 -0
  71. package/app/chat/page.tsx +5 -0
  72. package/app/globals.css +84 -0
  73. package/app/h5/layout.tsx +5 -0
  74. package/app/h5/mobile-approvals-page.tsx +167 -0
  75. package/app/h5/mobile-chat-page.tsx +951 -0
  76. package/app/h5/mobile-profile-page.tsx +147 -0
  77. package/app/h5/mobile-tickets-page.tsx +121 -0
  78. package/app/h5/page.tsx +23 -0
  79. package/app/h5/ticket-action-buttons.tsx +80 -0
  80. package/app/layout.tsx +26 -0
  81. package/app/login/page.tsx +318 -0
  82. package/app/market/knowledge/[id]/page.tsx +77 -0
  83. package/app/market/knowledge/page.tsx +358 -0
  84. package/app/market/layout.tsx +29 -0
  85. package/app/market/page.tsx +18 -0
  86. package/app/market/skills/page.tsx +397 -0
  87. package/app/my/files/page.tsx +511 -0
  88. package/app/my/knowledge/[id]/page.tsx +271 -0
  89. package/app/my/knowledge/new/page.tsx +234 -0
  90. package/app/my/knowledge/page.tsx +248 -0
  91. package/app/my/layout.tsx +32 -0
  92. package/app/my/memory/page.tsx +164 -0
  93. package/app/my/page.tsx +18 -0
  94. package/app/my/scheduled-tasks/[id]/edit/page.tsx +290 -0
  95. package/app/my/scheduled-tasks/[id]/executions/page.tsx +275 -0
  96. package/app/my/scheduled-tasks/[id]/page.tsx +284 -0
  97. package/app/my/scheduled-tasks/new/page.tsx +230 -0
  98. package/app/my/scheduled-tasks/page.tsx +27 -0
  99. package/app/my/skills/[name]/page.tsx +320 -0
  100. package/app/my/skills/new/page.tsx +394 -0
  101. package/app/my/skills/page.tsx +303 -0
  102. package/app/page.tsx +2288 -0
  103. package/app/share/[sessionId]/page.tsx +226 -0
  104. package/app/share/file/[id]/page.tsx +140 -0
  105. package/bin/README.md +63 -0
  106. package/bin/generate-api-system +300 -0
  107. package/bin/postinstall.js +95 -0
  108. package/bin/work-agent.js +173 -0
  109. package/components/ai-elements/agent.tsx +142 -0
  110. package/components/ai-elements/artifact.tsx +149 -0
  111. package/components/ai-elements/attachments.tsx +427 -0
  112. package/components/ai-elements/audio-player.tsx +232 -0
  113. package/components/ai-elements/canvas.tsx +26 -0
  114. package/components/ai-elements/chain-of-thought.tsx +223 -0
  115. package/components/ai-elements/checkpoint.tsx +72 -0
  116. package/components/ai-elements/code-block.tsx +555 -0
  117. package/components/ai-elements/commit.tsx +449 -0
  118. package/components/ai-elements/confirmation.tsx +173 -0
  119. package/components/ai-elements/connection.tsx +28 -0
  120. package/components/ai-elements/context.tsx +410 -0
  121. package/components/ai-elements/controls.tsx +19 -0
  122. package/components/ai-elements/conversation.tsx +167 -0
  123. package/components/ai-elements/edge.tsx +144 -0
  124. package/components/ai-elements/environment-variables.tsx +325 -0
  125. package/components/ai-elements/file-tree.tsx +298 -0
  126. package/components/ai-elements/image.tsx +25 -0
  127. package/components/ai-elements/inline-citation.tsx +294 -0
  128. package/components/ai-elements/jsx-preview.tsx +250 -0
  129. package/components/ai-elements/message.tsx +367 -0
  130. package/components/ai-elements/mic-selector.tsx +372 -0
  131. package/components/ai-elements/model-selector.tsx +214 -0
  132. package/components/ai-elements/node.tsx +72 -0
  133. package/components/ai-elements/open-in-chat.tsx +367 -0
  134. package/components/ai-elements/package-info.tsx +235 -0
  135. package/components/ai-elements/panel.tsx +16 -0
  136. package/components/ai-elements/persona.tsx +280 -0
  137. package/components/ai-elements/plan.tsx +144 -0
  138. package/components/ai-elements/prompt-input.tsx +1341 -0
  139. package/components/ai-elements/queue.tsx +275 -0
  140. package/components/ai-elements/reasoning.tsx +355 -0
  141. package/components/ai-elements/sandbox.tsx +133 -0
  142. package/components/ai-elements/schema-display.tsx +473 -0
  143. package/components/ai-elements/shimmer.tsx +78 -0
  144. package/components/ai-elements/snippet.tsx +141 -0
  145. package/components/ai-elements/sources.tsx +78 -0
  146. package/components/ai-elements/speech-input.tsx +324 -0
  147. package/components/ai-elements/stack-trace.tsx +531 -0
  148. package/components/ai-elements/suggestion.tsx +58 -0
  149. package/components/ai-elements/task.tsx +88 -0
  150. package/components/ai-elements/terminal.tsx +277 -0
  151. package/components/ai-elements/test-results.tsx +497 -0
  152. package/components/ai-elements/tool.tsx +174 -0
  153. package/components/ai-elements/toolbar.tsx +17 -0
  154. package/components/ai-elements/transcription.tsx +126 -0
  155. package/components/ai-elements/voice-selector.tsx +525 -0
  156. package/components/ai-elements/web-preview.tsx +282 -0
  157. package/components/audit-log-list.tsx +114 -0
  158. package/components/chat/EmptyPreviewState.tsx +12 -0
  159. package/components/chat/KnowledgePickerDialog.tsx +464 -0
  160. package/components/chat/KnowledgePreview.tsx +70 -0
  161. package/components/chat/KnowledgePreviewPanel.tsx +86 -0
  162. package/components/chat/MentionInput.tsx +309 -0
  163. package/components/chat/OrganizeDialog.tsx +258 -0
  164. package/components/chat/RecommendationBanner.tsx +94 -0
  165. package/components/chat/SaveToKnowledgeDialog.tsx +193 -0
  166. package/components/chat/SkillSelector.tsx +305 -0
  167. package/components/chat/SkillSwitcher.tsx +163 -0
  168. package/components/client-layout.tsx +15 -0
  169. package/components/knowledge/KnowledgeMetadataPanel.tsx +293 -0
  170. package/components/layout-wrapper.tsx +18 -0
  171. package/components/mobile-layout.tsx +62 -0
  172. package/components/scheduled-task-list.tsx +356 -0
  173. package/components/setup-guide.tsx +484 -0
  174. package/components/sub-nav.tsx +54 -0
  175. package/components/ticket-detail-content.tsx +383 -0
  176. package/components/ticket-list.tsx +366 -0
  177. package/components/top-nav.tsx +132 -0
  178. package/components/ui/accordion.tsx +58 -0
  179. package/components/ui/alert.tsx +59 -0
  180. package/components/ui/avatar.tsx +50 -0
  181. package/components/ui/badge.tsx +36 -0
  182. package/components/ui/button-group.tsx +83 -0
  183. package/components/ui/button.tsx +57 -0
  184. package/components/ui/card.tsx +91 -0
  185. package/components/ui/carousel.tsx +262 -0
  186. package/components/ui/collapsible.tsx +11 -0
  187. package/components/ui/command.tsx +153 -0
  188. package/components/ui/dialog.tsx +122 -0
  189. package/components/ui/dropdown-menu.tsx +200 -0
  190. package/components/ui/hover-card.tsx +29 -0
  191. package/components/ui/input-group.tsx +170 -0
  192. package/components/ui/input.tsx +22 -0
  193. package/components/ui/label.tsx +26 -0
  194. package/components/ui/popover.tsx +31 -0
  195. package/components/ui/progress.tsx +28 -0
  196. package/components/ui/scroll-area.tsx +48 -0
  197. package/components/ui/select.tsx +174 -0
  198. package/components/ui/separator.tsx +31 -0
  199. package/components/ui/spinner.tsx +16 -0
  200. package/components/ui/switch.tsx +29 -0
  201. package/components/ui/table.tsx +120 -0
  202. package/components/ui/tabs.tsx +55 -0
  203. package/components/ui/textarea.tsx +22 -0
  204. package/components/ui/tooltip.tsx +30 -0
  205. package/components/welcome-guide.tsx +182 -0
  206. package/components.json +24 -0
  207. package/lib/command-parser.ts +331 -0
  208. package/lib/dangerous-commands.ts +672 -0
  209. package/lib/db.ts +2250 -0
  210. package/lib/feishu-auth.ts +135 -0
  211. package/lib/file-storage.ts +306 -0
  212. package/lib/file-tool.ts +583 -0
  213. package/lib/knowledge-tool.ts +152 -0
  214. package/lib/knowledge-types.ts +66 -0
  215. package/lib/market-client.ts +313 -0
  216. package/lib/market-db.ts +736 -0
  217. package/lib/market-types.ts +51 -0
  218. package/lib/memory-tool.ts +211 -0
  219. package/lib/memory.ts +197 -0
  220. package/lib/pi-config.ts +436 -0
  221. package/lib/pi-session.ts +799 -0
  222. package/lib/pinyin.ts +13 -0
  223. package/lib/recommendation.ts +227 -0
  224. package/lib/risk-estimator.ts +350 -0
  225. package/lib/scheduled-task-tool.ts +184 -0
  226. package/lib/scheduler-init.ts +43 -0
  227. package/lib/scheduler.ts +416 -0
  228. package/lib/secure-bash-tool.ts +413 -0
  229. package/lib/skill-engine.ts +396 -0
  230. package/lib/skill-generator.ts +269 -0
  231. package/lib/skill-loader.ts +234 -0
  232. package/lib/skill-tool.ts +188 -0
  233. package/lib/skill-types.ts +82 -0
  234. package/lib/skills-init.ts +58 -0
  235. package/lib/ticket-tool.ts +246 -0
  236. package/lib/user-skill-types.ts +30 -0
  237. package/lib/user-skills.ts +362 -0
  238. package/lib/utils.ts +6 -0
  239. package/lib/workflow.ts +154 -0
  240. package/lib/zip-tool.ts +191 -0
  241. package/next.config.js +8 -0
  242. package/package.json +106 -0
  243. package/public/.gitkeep +1 -0
  244. package/public/icon.svg +1 -0
  245. package/tsconfig.json +42 -0
@@ -0,0 +1,410 @@
1
+ "use client";
2
+
3
+ import type { LanguageModelUsage } from "ai";
4
+ import type { ComponentProps } from "react";
5
+
6
+ import { Button } from "@/components/ui/button";
7
+ import {
8
+ HoverCard,
9
+ HoverCardContent,
10
+ HoverCardTrigger,
11
+ } from "@/components/ui/hover-card";
12
+ import { Progress } from "@/components/ui/progress";
13
+ import { cn } from "@/lib/utils";
14
+ import { createContext, useContext, useMemo } from "react";
15
+ import { getUsage } from "tokenlens";
16
+
17
+ const PERCENT_MAX = 100;
18
+ const ICON_RADIUS = 10;
19
+ const ICON_VIEWBOX = 24;
20
+ const ICON_CENTER = 12;
21
+ const ICON_STROKE_WIDTH = 2;
22
+
23
+ type ModelId = string;
24
+
25
+ interface ContextSchema {
26
+ usedTokens: number;
27
+ maxTokens: number;
28
+ usage?: LanguageModelUsage;
29
+ modelId?: ModelId;
30
+ }
31
+
32
+ const ContextContext = createContext<ContextSchema | null>(null);
33
+
34
+ const useContextValue = () => {
35
+ const context = useContext(ContextContext);
36
+
37
+ if (!context) {
38
+ throw new Error("Context components must be used within Context");
39
+ }
40
+
41
+ return context;
42
+ };
43
+
44
+ export type ContextProps = ComponentProps<typeof HoverCard> & ContextSchema;
45
+
46
+ export const Context = ({
47
+ usedTokens,
48
+ maxTokens,
49
+ usage,
50
+ modelId,
51
+ ...props
52
+ }: ContextProps) => {
53
+ const contextValue = useMemo(
54
+ () => ({ maxTokens, modelId, usage, usedTokens }),
55
+ [maxTokens, modelId, usage, usedTokens]
56
+ );
57
+
58
+ return (
59
+ <ContextContext.Provider value={contextValue}>
60
+ <HoverCard closeDelay={0} openDelay={0} {...props} />
61
+ </ContextContext.Provider>
62
+ );
63
+ };
64
+
65
+ const ContextIcon = () => {
66
+ const { usedTokens, maxTokens } = useContextValue();
67
+ const circumference = 2 * Math.PI * ICON_RADIUS;
68
+ const usedPercent = usedTokens / maxTokens;
69
+ const dashOffset = circumference * (1 - usedPercent);
70
+
71
+ return (
72
+ <svg
73
+ aria-label="Model context usage"
74
+ height="20"
75
+ role="img"
76
+ style={{ color: "currentcolor" }}
77
+ viewBox={`0 0 ${ICON_VIEWBOX} ${ICON_VIEWBOX}`}
78
+ width="20"
79
+ >
80
+ <circle
81
+ cx={ICON_CENTER}
82
+ cy={ICON_CENTER}
83
+ fill="none"
84
+ opacity="0.25"
85
+ r={ICON_RADIUS}
86
+ stroke="currentColor"
87
+ strokeWidth={ICON_STROKE_WIDTH}
88
+ />
89
+ <circle
90
+ cx={ICON_CENTER}
91
+ cy={ICON_CENTER}
92
+ fill="none"
93
+ opacity="0.7"
94
+ r={ICON_RADIUS}
95
+ stroke="currentColor"
96
+ strokeDasharray={`${circumference} ${circumference}`}
97
+ strokeDashoffset={dashOffset}
98
+ strokeLinecap="round"
99
+ strokeWidth={ICON_STROKE_WIDTH}
100
+ style={{ transform: "rotate(-90deg)", transformOrigin: "center" }}
101
+ />
102
+ </svg>
103
+ );
104
+ };
105
+
106
+ export type ContextTriggerProps = ComponentProps<typeof Button>;
107
+
108
+ export const ContextTrigger = ({ children, ...props }: ContextTriggerProps) => {
109
+ const { usedTokens, maxTokens } = useContextValue();
110
+ const usedPercent = usedTokens / maxTokens;
111
+ const renderedPercent = new Intl.NumberFormat("en-US", {
112
+ maximumFractionDigits: 1,
113
+ style: "percent",
114
+ }).format(usedPercent);
115
+
116
+ return (
117
+ <HoverCardTrigger asChild>
118
+ {children ?? (
119
+ <Button type="button" variant="ghost" {...props}>
120
+ <span className="font-medium text-muted-foreground">
121
+ {renderedPercent}
122
+ </span>
123
+ <ContextIcon />
124
+ </Button>
125
+ )}
126
+ </HoverCardTrigger>
127
+ );
128
+ };
129
+
130
+ export type ContextContentProps = ComponentProps<typeof HoverCardContent>;
131
+
132
+ export const ContextContent = ({
133
+ className,
134
+ ...props
135
+ }: ContextContentProps) => (
136
+ <HoverCardContent
137
+ className={cn("min-w-60 divide-y overflow-hidden p-0", className)}
138
+ {...props}
139
+ />
140
+ );
141
+
142
+ export type ContextContentHeaderProps = ComponentProps<"div">;
143
+
144
+ export const ContextContentHeader = ({
145
+ children,
146
+ className,
147
+ ...props
148
+ }: ContextContentHeaderProps) => {
149
+ const { usedTokens, maxTokens } = useContextValue();
150
+ const usedPercent = usedTokens / maxTokens;
151
+ const displayPct = new Intl.NumberFormat("en-US", {
152
+ maximumFractionDigits: 1,
153
+ style: "percent",
154
+ }).format(usedPercent);
155
+ const used = new Intl.NumberFormat("en-US", {
156
+ notation: "compact",
157
+ }).format(usedTokens);
158
+ const total = new Intl.NumberFormat("en-US", {
159
+ notation: "compact",
160
+ }).format(maxTokens);
161
+
162
+ return (
163
+ <div className={cn("w-full space-y-2 p-3", className)} {...props}>
164
+ {children ?? (
165
+ <>
166
+ <div className="flex items-center justify-between gap-3 text-xs">
167
+ <p>{displayPct}</p>
168
+ <p className="font-mono text-muted-foreground">
169
+ {used} / {total}
170
+ </p>
171
+ </div>
172
+ <div className="space-y-2">
173
+ <Progress className="bg-muted" value={usedPercent * PERCENT_MAX} />
174
+ </div>
175
+ </>
176
+ )}
177
+ </div>
178
+ );
179
+ };
180
+
181
+ export type ContextContentBodyProps = ComponentProps<"div">;
182
+
183
+ export const ContextContentBody = ({
184
+ children,
185
+ className,
186
+ ...props
187
+ }: ContextContentBodyProps) => (
188
+ <div className={cn("w-full p-3", className)} {...props}>
189
+ {children}
190
+ </div>
191
+ );
192
+
193
+ export type ContextContentFooterProps = ComponentProps<"div">;
194
+
195
+ export const ContextContentFooter = ({
196
+ children,
197
+ className,
198
+ ...props
199
+ }: ContextContentFooterProps) => {
200
+ const { modelId, usage } = useContextValue();
201
+ const costUSD = modelId
202
+ ? getUsage({
203
+ modelId,
204
+ usage: {
205
+ input: usage?.inputTokens ?? 0,
206
+ output: usage?.outputTokens ?? 0,
207
+ },
208
+ }).costUSD?.totalUSD
209
+ : undefined;
210
+ const totalCost = new Intl.NumberFormat("en-US", {
211
+ currency: "USD",
212
+ style: "currency",
213
+ }).format(costUSD ?? 0);
214
+
215
+ return (
216
+ <div
217
+ className={cn(
218
+ "flex w-full items-center justify-between gap-3 bg-secondary p-3 text-xs",
219
+ className
220
+ )}
221
+ {...props}
222
+ >
223
+ {children ?? (
224
+ <>
225
+ <span className="text-muted-foreground">Total cost</span>
226
+ <span>{totalCost}</span>
227
+ </>
228
+ )}
229
+ </div>
230
+ );
231
+ };
232
+
233
+ export type ContextInputUsageProps = ComponentProps<"div">;
234
+
235
+ export const ContextInputUsage = ({
236
+ className,
237
+ children,
238
+ ...props
239
+ }: ContextInputUsageProps) => {
240
+ const { usage, modelId } = useContextValue();
241
+ const inputTokens = usage?.inputTokens ?? 0;
242
+
243
+ if (children) {
244
+ return children;
245
+ }
246
+
247
+ if (!inputTokens) {
248
+ return null;
249
+ }
250
+
251
+ const inputCost = modelId
252
+ ? getUsage({
253
+ modelId,
254
+ usage: { input: inputTokens, output: 0 },
255
+ }).costUSD?.totalUSD
256
+ : undefined;
257
+ const inputCostText = new Intl.NumberFormat("en-US", {
258
+ currency: "USD",
259
+ style: "currency",
260
+ }).format(inputCost ?? 0);
261
+
262
+ return (
263
+ <div
264
+ className={cn("flex items-center justify-between text-xs", className)}
265
+ {...props}
266
+ >
267
+ <span className="text-muted-foreground">Input</span>
268
+ <TokensWithCost costText={inputCostText} tokens={inputTokens} />
269
+ </div>
270
+ );
271
+ };
272
+
273
+ export type ContextOutputUsageProps = ComponentProps<"div">;
274
+
275
+ export const ContextOutputUsage = ({
276
+ className,
277
+ children,
278
+ ...props
279
+ }: ContextOutputUsageProps) => {
280
+ const { usage, modelId } = useContextValue();
281
+ const outputTokens = usage?.outputTokens ?? 0;
282
+
283
+ if (children) {
284
+ return children;
285
+ }
286
+
287
+ if (!outputTokens) {
288
+ return null;
289
+ }
290
+
291
+ const outputCost = modelId
292
+ ? getUsage({
293
+ modelId,
294
+ usage: { input: 0, output: outputTokens },
295
+ }).costUSD?.totalUSD
296
+ : undefined;
297
+ const outputCostText = new Intl.NumberFormat("en-US", {
298
+ currency: "USD",
299
+ style: "currency",
300
+ }).format(outputCost ?? 0);
301
+
302
+ return (
303
+ <div
304
+ className={cn("flex items-center justify-between text-xs", className)}
305
+ {...props}
306
+ >
307
+ <span className="text-muted-foreground">Output</span>
308
+ <TokensWithCost costText={outputCostText} tokens={outputTokens} />
309
+ </div>
310
+ );
311
+ };
312
+
313
+ export type ContextReasoningUsageProps = ComponentProps<"div">;
314
+
315
+ export const ContextReasoningUsage = ({
316
+ className,
317
+ children,
318
+ ...props
319
+ }: ContextReasoningUsageProps) => {
320
+ const { usage, modelId } = useContextValue();
321
+ const reasoningTokens = usage?.reasoningTokens ?? 0;
322
+
323
+ if (children) {
324
+ return children;
325
+ }
326
+
327
+ if (!reasoningTokens) {
328
+ return null;
329
+ }
330
+
331
+ const reasoningCost = modelId
332
+ ? getUsage({
333
+ modelId,
334
+ usage: { reasoningTokens },
335
+ }).costUSD?.totalUSD
336
+ : undefined;
337
+ const reasoningCostText = new Intl.NumberFormat("en-US", {
338
+ currency: "USD",
339
+ style: "currency",
340
+ }).format(reasoningCost ?? 0);
341
+
342
+ return (
343
+ <div
344
+ className={cn("flex items-center justify-between text-xs", className)}
345
+ {...props}
346
+ >
347
+ <span className="text-muted-foreground">Reasoning</span>
348
+ <TokensWithCost costText={reasoningCostText} tokens={reasoningTokens} />
349
+ </div>
350
+ );
351
+ };
352
+
353
+ export type ContextCacheUsageProps = ComponentProps<"div">;
354
+
355
+ export const ContextCacheUsage = ({
356
+ className,
357
+ children,
358
+ ...props
359
+ }: ContextCacheUsageProps) => {
360
+ const { usage, modelId } = useContextValue();
361
+ const cacheTokens = usage?.cachedInputTokens ?? 0;
362
+
363
+ if (children) {
364
+ return children;
365
+ }
366
+
367
+ if (!cacheTokens) {
368
+ return null;
369
+ }
370
+
371
+ const cacheCost = modelId
372
+ ? getUsage({
373
+ modelId,
374
+ usage: { cacheReads: cacheTokens, input: 0, output: 0 },
375
+ }).costUSD?.totalUSD
376
+ : undefined;
377
+ const cacheCostText = new Intl.NumberFormat("en-US", {
378
+ currency: "USD",
379
+ style: "currency",
380
+ }).format(cacheCost ?? 0);
381
+
382
+ return (
383
+ <div
384
+ className={cn("flex items-center justify-between text-xs", className)}
385
+ {...props}
386
+ >
387
+ <span className="text-muted-foreground">Cache</span>
388
+ <TokensWithCost costText={cacheCostText} tokens={cacheTokens} />
389
+ </div>
390
+ );
391
+ };
392
+
393
+ const TokensWithCost = ({
394
+ tokens,
395
+ costText,
396
+ }: {
397
+ tokens?: number;
398
+ costText?: string;
399
+ }) => (
400
+ <span>
401
+ {tokens === undefined
402
+ ? "—"
403
+ : new Intl.NumberFormat("en-US", {
404
+ notation: "compact",
405
+ }).format(tokens)}
406
+ {costText ? (
407
+ <span className="ml-2 text-muted-foreground">• {costText}</span>
408
+ ) : null}
409
+ </span>
410
+ );
@@ -0,0 +1,19 @@
1
+ "use client";
2
+
3
+ import type { ComponentProps } from "react";
4
+
5
+ import { cn } from "@/lib/utils";
6
+ import { Controls as ControlsPrimitive } from "@xyflow/react";
7
+
8
+ export type ControlsProps = ComponentProps<typeof ControlsPrimitive>;
9
+
10
+ export const Controls = ({ className, ...props }: ControlsProps) => (
11
+ <ControlsPrimitive
12
+ className={cn(
13
+ "gap-px overflow-hidden rounded-md border bg-card p-1 shadow-none!",
14
+ "[&>button]:rounded-md [&>button]:border-none! [&>button]:bg-transparent! [&>button]:hover:bg-secondary!",
15
+ className
16
+ )}
17
+ {...props}
18
+ />
19
+ );
@@ -0,0 +1,167 @@
1
+ "use client";
2
+
3
+ import type { ComponentProps } from "react";
4
+
5
+ import { Button } from "@/components/ui/button";
6
+ import { cn } from "@/lib/utils";
7
+ import { ArrowDownIcon, DownloadIcon } from "lucide-react";
8
+ import { useCallback } from "react";
9
+ import { StickToBottom, useStickToBottomContext } from "use-stick-to-bottom";
10
+
11
+ export type ConversationProps = ComponentProps<typeof StickToBottom>;
12
+
13
+ export const Conversation = ({ className, ...props }: ConversationProps) => (
14
+ <StickToBottom
15
+ className={cn("relative flex-1 overflow-y-hidden", className)}
16
+ initial="smooth"
17
+ resize="smooth"
18
+ role="log"
19
+ {...props}
20
+ />
21
+ );
22
+
23
+ export type ConversationContentProps = ComponentProps<
24
+ typeof StickToBottom.Content
25
+ >;
26
+
27
+ export const ConversationContent = ({
28
+ className,
29
+ ...props
30
+ }: ConversationContentProps) => (
31
+ <StickToBottom.Content
32
+ className={cn("flex flex-col gap-8 p-4", className)}
33
+ {...props}
34
+ />
35
+ );
36
+
37
+ export type ConversationEmptyStateProps = ComponentProps<"div"> & {
38
+ title?: string;
39
+ description?: string;
40
+ icon?: React.ReactNode;
41
+ };
42
+
43
+ export const ConversationEmptyState = ({
44
+ className,
45
+ title = "No messages yet",
46
+ description = "Start a conversation to see messages here",
47
+ icon,
48
+ children,
49
+ ...props
50
+ }: ConversationEmptyStateProps) => (
51
+ <div
52
+ className={cn(
53
+ "flex size-full flex-col items-center justify-center gap-3 p-8 text-center",
54
+ className
55
+ )}
56
+ {...props}
57
+ >
58
+ {children ?? (
59
+ <>
60
+ {icon && <div className="text-muted-foreground">{icon}</div>}
61
+ <div className="space-y-1">
62
+ <h3 className="font-medium text-sm">{title}</h3>
63
+ {description && (
64
+ <p className="text-muted-foreground text-sm">{description}</p>
65
+ )}
66
+ </div>
67
+ </>
68
+ )}
69
+ </div>
70
+ );
71
+
72
+ export type ConversationScrollButtonProps = ComponentProps<typeof Button>;
73
+
74
+ export const ConversationScrollButton = ({
75
+ className,
76
+ ...props
77
+ }: ConversationScrollButtonProps) => {
78
+ const { isAtBottom, scrollToBottom } = useStickToBottomContext();
79
+
80
+ const handleScrollToBottom = useCallback(() => {
81
+ scrollToBottom();
82
+ }, [scrollToBottom]);
83
+
84
+ return (
85
+ !isAtBottom && (
86
+ <Button
87
+ className={cn(
88
+ "absolute bottom-4 left-[50%] translate-x-[-50%] rounded-full dark:bg-background dark:hover:bg-muted",
89
+ className
90
+ )}
91
+ onClick={handleScrollToBottom}
92
+ size="icon"
93
+ type="button"
94
+ variant="outline"
95
+ {...props}
96
+ >
97
+ <ArrowDownIcon className="size-4" />
98
+ </Button>
99
+ )
100
+ );
101
+ };
102
+
103
+ export interface ConversationMessage {
104
+ role: "user" | "assistant" | "system" | "data" | "tool";
105
+ content: string;
106
+ }
107
+
108
+ export type ConversationDownloadProps = Omit<
109
+ ComponentProps<typeof Button>,
110
+ "onClick"
111
+ > & {
112
+ messages: ConversationMessage[];
113
+ filename?: string;
114
+ formatMessage?: (message: ConversationMessage, index: number) => string;
115
+ };
116
+
117
+ const defaultFormatMessage = (message: ConversationMessage): string => {
118
+ const roleLabel =
119
+ message.role.charAt(0).toUpperCase() + message.role.slice(1);
120
+ return `**${roleLabel}:** ${message.content}`;
121
+ };
122
+
123
+ export const messagesToMarkdown = (
124
+ messages: ConversationMessage[],
125
+ formatMessage: (
126
+ message: ConversationMessage,
127
+ index: number
128
+ ) => string = defaultFormatMessage
129
+ ): string => messages.map((msg, i) => formatMessage(msg, i)).join("\n\n");
130
+
131
+ export const ConversationDownload = ({
132
+ messages,
133
+ filename = "conversation.md",
134
+ formatMessage = defaultFormatMessage,
135
+ className,
136
+ children,
137
+ ...props
138
+ }: ConversationDownloadProps) => {
139
+ const handleDownload = useCallback(() => {
140
+ const markdown = messagesToMarkdown(messages, formatMessage);
141
+ const blob = new Blob([markdown], { type: "text/markdown" });
142
+ const url = URL.createObjectURL(blob);
143
+ const link = document.createElement("a");
144
+ link.href = url;
145
+ link.download = filename;
146
+ document.body.append(link);
147
+ link.click();
148
+ link.remove();
149
+ URL.revokeObjectURL(url);
150
+ }, [messages, filename, formatMessage]);
151
+
152
+ return (
153
+ <Button
154
+ className={cn(
155
+ "absolute top-4 right-4 rounded-full dark:bg-background dark:hover:bg-muted",
156
+ className
157
+ )}
158
+ onClick={handleDownload}
159
+ size="icon"
160
+ type="button"
161
+ variant="outline"
162
+ {...props}
163
+ >
164
+ {children ?? <DownloadIcon className="size-4" />}
165
+ </Button>
166
+ );
167
+ };