create-efforge 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +88 -2
- package/dist/addon.js +115 -0
- package/dist/bin.js +13 -0
- package/dist/cli.js +28 -0
- package/dist/commands/add.js +640 -0
- package/dist/commands/create.js +319 -0
- package/dist/commands/list.js +80 -0
- package/dist/diff.js +72 -0
- package/dist/exec.js +33 -0
- package/dist/generate.js +181 -0
- package/dist/manifest.js +14 -0
- package/dist/projectName.js +41 -0
- package/dist/prompts/flow.js +122 -0
- package/dist/prompts/navigable.js +193 -0
- package/dist/routeTree.js +7 -0
- package/dist/rules.js +27 -0
- package/dist/slots.js +116 -0
- package/dist/stack.js +68 -0
- package/dist/transforms/addAlchemyResource.js +71 -0
- package/dist/transforms/addArrayItem.js +31 -0
- package/dist/transforms/addAuthPlugin.js +77 -0
- package/dist/transforms/addDevVar.js +18 -0
- package/dist/transforms/addExport.js +46 -0
- package/dist/transforms/addImport.js +11 -0
- package/dist/transforms/addLayer.js +31 -0
- package/dist/transforms/addServerFnMiddleware.js +62 -0
- package/dist/transforms/addWorkflowSecret.js +43 -0
- package/dist/transforms/addWranglerBinding.js +61 -0
- package/dist/transforms/appendAgentsSection.js +26 -0
- package/dist/transforms/errors.js +7 -0
- package/dist/transforms/index.js +40 -0
- package/dist/transforms/setWranglerValue.js +40 -0
- package/dist/transforms/util.js +219 -0
- package/dist/tree.js +1 -0
- package/dist/versions.js +73 -0
- package/dist/write.js +28 -0
- package/package.json +63 -4
- package/templates/ai/effect-ai-byok/addon.json +619 -0
- package/templates/ai/effect-ai-byok/files/core/llm-crypto.test.ts +52 -0
- package/templates/ai/effect-ai-byok/files/core/llm-crypto.ts +76 -0
- package/templates/ai/effect-ai-byok/files/core/llm-store.ts +21 -0
- package/templates/ai/effect-ai-byok/files/core/llm.ts +32 -0
- package/templates/ai/effect-ai-byok/files/drizzle/d1/20261018161056_llm_credential/migration.sql +9 -0
- package/templates/ai/effect-ai-byok/files/drizzle/d1/20261018161056_llm_credential/snapshot.json +944 -0
- package/templates/ai/effect-ai-byok/files/drizzle/pg/20261018161056_llm_credential/migration.sql +10 -0
- package/templates/ai/effect-ai-byok/files/drizzle/pg/20261018161056_llm_credential/snapshot.json +1232 -0
- package/templates/ai/effect-ai-byok/files/routes/ai.tsx +193 -0
- package/templates/ai/effect-ai-byok/files/schema/d1.ts +16 -0
- package/templates/ai/effect-ai-byok/files/schema/pg.ts +14 -0
- package/templates/ai/effect-ai-byok/files/server/ai-settings.ts +129 -0
- package/templates/ai/effect-ai-byok/files/server/ai.ts +25 -0
- package/templates/ai/effect-ai-byok/files/server/llm-env.ts +48 -0
- package/templates/ai/effect-ai-byok/files/server/llm-orgs.ts +146 -0
- package/templates/ai/effect-ai-byok/files/server/llm-provider.ts +56 -0
- package/templates/ai/effect-ai-byok/files/server/llm-store-drizzle.ts +48 -0
- package/templates/ai/effect-ai-byok/files/server/llm-store-sql-d1.ts +41 -0
- package/templates/ai/effect-ai-byok/files/server/llm-store-sql-pg.ts +41 -0
- package/templates/ai/effect-ai-byok/files/sql/d1.sql +8 -0
- package/templates/ai/effect-ai-byok/files/sql/pg.sql +8 -0
- package/templates/auth/better-auth/addon.json +424 -0
- package/templates/auth/better-auth/files/core/Auth.ts +18 -0
- package/templates/auth/better-auth/files/drizzle/d1/20261018161054_auth/migration.sql +51 -0
- package/templates/auth/better-auth/files/drizzle/d1/20261018161054_auth/snapshot.json +522 -0
- package/templates/auth/better-auth/files/drizzle/pg/20261018161053_auth/migration.sql +51 -0
- package/templates/auth/better-auth/files/drizzle/pg/20261018161053_auth/snapshot.json +680 -0
- package/templates/auth/better-auth/files/lib/auth-client.ts +4 -0
- package/templates/auth/better-auth/files/routes/app.tsx +54 -0
- package/templates/auth/better-auth/files/schema/d1/auth-schema.ts +86 -0
- package/templates/auth/better-auth/files/schema/pg/auth-schema.ts +73 -0
- package/templates/auth/better-auth/files/server/auth-live.ts +47 -0
- package/templates/auth/better-auth/files/server/session.ts +14 -0
- package/templates/auth/better-auth/files/shared/components/auth-card.tsx +32 -0
- package/templates/auth/better-auth/files/shared/components/user-menu.tsx +44 -0
- package/templates/auth/better-auth/files/shared/drizzle/d1/drizzle.config.ts +7 -0
- package/templates/auth/better-auth/files/shared/drizzle/pg/drizzle.config.ts +10 -0
- package/templates/auth/better-auth/files/shared/lib/form.ts +5 -0
- package/templates/auth/better-auth/files/shared/lib/redirect.ts +6 -0
- package/templates/auth/better-auth/files/shared/routes/app/index.tsx +54 -0
- package/templates/auth/better-auth/files/shared/routes/sign-in.tsx +85 -0
- package/templates/auth/better-auth/files/shared/routes/sign-up.tsx +91 -0
- package/templates/auth/better-auth/files/shared/server/auth-route.ts +15 -0
- package/templates/auth/better-auth/files/shared/server/auth.ts +22 -0
- package/templates/auth/better-auth/files/shared/server/db/d1-drizzle.ts +25 -0
- package/templates/auth/better-auth/files/shared/server/db/d1-effect-sql.ts +17 -0
- package/templates/auth/better-auth/files/shared/server/db/pg-drizzle.ts +32 -0
- package/templates/auth/better-auth/files/shared/server/db/pg-effect-sql.ts +29 -0
- package/templates/auth/better-auth/files/shared/server/require-session.ts +20 -0
- package/templates/auth/better-auth/files/sql/d1/0002_auth.sql +13 -0
- package/templates/auth/better-auth/files/sql/pg/0002_auth.sql +13 -0
- package/templates/auth/better-auth-orgs/addon.json +463 -0
- package/templates/auth/better-auth-orgs/files/components/org-switcher.tsx +65 -0
- package/templates/auth/better-auth-orgs/files/core/Auth.ts +20 -0
- package/templates/auth/better-auth-orgs/files/drizzle/d1/20261018161055_auth/migration.sql +88 -0
- package/templates/auth/better-auth-orgs/files/drizzle/d1/20261018161055_auth/snapshot.json +862 -0
- package/templates/auth/better-auth-orgs/files/drizzle/pg/20261018161055_auth/migration.sql +88 -0
- package/templates/auth/better-auth-orgs/files/drizzle/pg/20261018161055_auth/snapshot.json +1127 -0
- package/templates/auth/better-auth-orgs/files/lib/auth-client.ts +5 -0
- package/templates/auth/better-auth-orgs/files/routes/accept-invite.$id.tsx +68 -0
- package/templates/auth/better-auth-orgs/files/routes/app/settings/index.tsx +27 -0
- package/templates/auth/better-auth-orgs/files/routes/app/settings/members.tsx +202 -0
- package/templates/auth/better-auth-orgs/files/routes/app/settings.tsx +30 -0
- package/templates/auth/better-auth-orgs/files/routes/app.tsx +58 -0
- package/templates/auth/better-auth-orgs/files/routes/onboarding.tsx +66 -0
- package/templates/auth/better-auth-orgs/files/schema/d1/auth-schema.ts +139 -0
- package/templates/auth/better-auth-orgs/files/schema/pg/auth-schema.ts +124 -0
- package/templates/auth/better-auth-orgs/files/server/auth-live.ts +52 -0
- package/templates/auth/better-auth-orgs/files/server/session.ts +44 -0
- package/templates/auth/better-auth-orgs/files/sql/d1/0002_auth.sql +27 -0
- package/templates/auth/better-auth-orgs/files/sql/pg/0002_auth.sql +27 -0
- package/templates/base/addon.json +216 -0
- package/templates/base/files/AGENTS.md +82 -0
- package/templates/base/files/CLAUDE.md +1 -0
- package/templates/base/files/README.md +32 -0
- package/templates/base/files/core/package.json +13 -0
- package/templates/base/files/core/src/index.ts +2 -0
- package/templates/base/files/core/test/core.test.ts +4 -0
- package/templates/base/files/core/tsconfig.json +6 -0
- package/templates/base/files/gitignore +11 -0
- package/templates/base/files/pnpm-workspace.yaml +11 -0
- package/templates/base/files/root/lint/effect-tests.js +45 -0
- package/templates/base/files/root/package.json +16 -0
- package/templates/base/files/root/tsconfig.base.json +19 -0
- package/templates/base/files/root/turbo.json +9 -0
- package/templates/base/files/root/vite.config.ts +86 -0
- package/templates/base/files/web/components.json +21 -0
- package/templates/base/files/web/package.json +17 -0
- package/templates/base/files/web/src/components/theme-provider.tsx +57 -0
- package/templates/base/files/web/src/components/theme-toggle.tsx +34 -0
- package/templates/base/files/web/src/components/ui/avatar.tsx +93 -0
- package/templates/base/files/web/src/components/ui/button.tsx +61 -0
- package/templates/base/files/web/src/components/ui/card.tsx +74 -0
- package/templates/base/files/web/src/components/ui/dialog.tsx +141 -0
- package/templates/base/files/web/src/components/ui/dropdown-menu.tsx +225 -0
- package/templates/base/files/web/src/components/ui/input.tsx +20 -0
- package/templates/base/files/web/src/components/ui/label.tsx +18 -0
- package/templates/base/files/web/src/components/ui/sonner.tsx +45 -0
- package/templates/base/files/web/src/components/widgets.tsx +4 -0
- package/templates/base/files/web/src/lib/utils.ts +6 -0
- package/templates/base/files/web/src/router.tsx +12 -0
- package/templates/base/files/web/src/routes/__root.tsx +42 -0
- package/templates/base/files/web/src/routes/app/index.tsx +27 -0
- package/templates/base/files/web/src/routes/app/settings.tsx +29 -0
- package/templates/base/files/web/src/routes/app.tsx +47 -0
- package/templates/base/files/web/src/routes/index.tsx +51 -0
- package/templates/base/files/web/src/server/greeting.ts +12 -0
- package/templates/base/files/web/src/server/layers.ts +10 -0
- package/templates/base/files/web/src/server/runtime.ts +5 -0
- package/templates/base/files/web/src/styles.css +81 -0
- package/templates/base/files/web/tsconfig.json +11 -0
- package/templates/base/files/web/vite.config.ts +9 -0
- package/templates/database/d1/addon.json +118 -0
- package/templates/database/d1/files/package.json +12 -0
- package/templates/database/d1/files/tsconfig.json +5 -0
- package/templates/database/postgres/addon.json +142 -0
- package/templates/database/postgres/files/docker-compose.yml +21 -0
- package/templates/database/postgres/files/package.json +12 -0
- package/templates/database/postgres/files/runtime.ts +12 -0
- package/templates/database/postgres/files/tsconfig.json +5 -0
- package/templates/extras/browser/addon.json +173 -0
- package/templates/extras/browser/files/BrowserContainer.ts +14 -0
- package/templates/extras/browser/files/app/Dockerfile +15 -0
- package/templates/extras/browser/files/app/dockerignore +1 -0
- package/templates/extras/browser/files/app/package-lock +605 -0
- package/templates/extras/browser/files/app/package.json +9 -0
- package/templates/extras/browser/files/app/src/server.ts +78 -0
- package/templates/extras/browser/files/app/src/url-policy.ts +115 -0
- package/templates/extras/browser/files/app/tsconfig.json +6 -0
- package/templates/extras/browser/files/browser.ts +34 -0
- package/templates/extras/browser/files/core-browser.ts +21 -0
- package/templates/extras/browser/files/run-browser.ts +25 -0
- package/templates/extras/email/addon.json +182 -0
- package/templates/extras/email/files/core-mailer.ts +28 -0
- package/templates/extras/email/files/invitation-email.ts +38 -0
- package/templates/extras/email/files/mailer.ts +58 -0
- package/templates/extras/realtime/addon.json +135 -0
- package/templates/extras/realtime/files/Room.ts +46 -0
- package/templates/extras/realtime/files/core-realtime.ts +19 -0
- package/templates/extras/realtime/files/realtime-demo.tsx +40 -0
- package/templates/extras/realtime/files/realtime-limits.test.ts +32 -0
- package/templates/extras/realtime/files/realtime-limits.ts +35 -0
- package/templates/extras/realtime/files/realtime-route.ts +39 -0
- package/templates/extras/realtime/files/realtime.ts +39 -0
- package/templates/extras/realtime/files/use-room.ts +67 -0
- package/templates/extras/storage/addon.json +132 -0
- package/templates/extras/storage/files/core-storage.ts +64 -0
- package/templates/extras/storage/files/storage-route.ts +50 -0
- package/templates/extras/storage/files/storage-security.test.ts +71 -0
- package/templates/extras/storage/files/storage-security.ts +92 -0
- package/templates/extras/storage/files/storage.ts +84 -0
- package/templates/extras/workflows/addon.json +95 -0
- package/templates/extras/workflows/files/Example.ts +42 -0
- package/templates/extras/workflows/files/core-jobs.ts +45 -0
- package/templates/extras/workflows/files/example-job.ts +36 -0
- package/templates/extras/workflows/files/jobs.ts +34 -0
- package/templates/infra/alchemy/addon.json +37 -0
- package/templates/infra/alchemy/files/README.md +27 -0
- package/templates/infra/alchemy/files/alchemy.run.ts +36 -0
- package/templates/infra/alchemy/files/github/deploy.yml +63 -0
- package/templates/infra/alchemy/files/tsconfig.json +5 -0
- package/templates/infra/alchemy/files/vite.config.ts +30 -0
- package/templates/orm/drizzle/addon.json +188 -0
- package/templates/orm/drizzle/files/d1/Database.ts +7 -0
- package/templates/orm/drizzle/files/d1/drizzle/20261018154417_init/migration.sql +10 -0
- package/templates/orm/drizzle/files/d1/drizzle/20261018154417_init/snapshot.json +60 -0
- package/templates/orm/drizzle/files/d1/drizzle.config.ts +7 -0
- package/templates/orm/drizzle/files/d1/layer.ts +13 -0
- package/templates/orm/drizzle/files/d1/migrate.ts +37 -0
- package/templates/orm/drizzle/files/d1/schema.ts +15 -0
- package/templates/orm/drizzle/files/pg/Database.ts +7 -0
- package/templates/orm/drizzle/files/pg/drizzle/20261018154416_init/migration.sql +10 -0
- package/templates/orm/drizzle/files/pg/drizzle/20261018154416_init/snapshot.json +75 -0
- package/templates/orm/drizzle/files/pg/drizzle.config.ts +10 -0
- package/templates/orm/drizzle/files/pg/layer.ts +16 -0
- package/templates/orm/drizzle/files/pg/migrate.ts +24 -0
- package/templates/orm/drizzle/files/pg/schema.ts +11 -0
- package/templates/orm/drizzle/files/shared/index.ts +2 -0
- package/templates/orm/drizzle/files/shared/notes.ts +15 -0
- package/templates/orm/drizzle/files/shared/page.tsx +50 -0
- package/templates/orm/effect-sql/addon.json +124 -0
- package/templates/orm/effect-sql/files/d1/layer.ts +13 -0
- package/templates/orm/effect-sql/files/d1/migrate.ts +20 -0
- package/templates/orm/effect-sql/files/d1/schema.ts +7 -0
- package/templates/orm/effect-sql/files/d1-sql/0001_init.sql +10 -0
- package/templates/orm/effect-sql/files/pg/layer.ts +16 -0
- package/templates/orm/effect-sql/files/pg/migrate.ts +66 -0
- package/templates/orm/effect-sql/files/pg/schema.ts +7 -0
- package/templates/orm/effect-sql/files/pg-sql/0001_init.sql +10 -0
- package/templates/orm/effect-sql/files/shared/Database.ts +7 -0
- package/templates/orm/effect-sql/files/shared/index.ts +2 -0
- package/templates/orm/effect-sql/files/shared/notes.ts +18 -0
- package/templates/orm/effect-sql/files/shared/page.tsx +50 -0
- package/templates/runtime/cloudflare/addon.json +52 -0
- package/templates/runtime/cloudflare/files/cloudflare.ts +10 -0
- package/templates/runtime/cloudflare/files/routes.ts +5 -0
- package/templates/runtime/cloudflare/files/server.ts +22 -0
- package/templates/runtime/cloudflare/files/tsconfig.json +12 -0
- package/templates/runtime/cloudflare/files/vite.config.ts +20 -0
- package/templates/runtime/cloudflare/files/wrangler.jsonc +8 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { createFileRoute, useRouter } from "@tanstack/react-router"
|
|
2
|
+
import { type SubmitEvent, useState } from "react"
|
|
3
|
+
import { toast } from "sonner"
|
|
4
|
+
import { Button } from "@/components/ui/button"
|
|
5
|
+
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
|
6
|
+
import { Input } from "@/components/ui/input"
|
|
7
|
+
import { Label } from "@/components/ui/label"
|
|
8
|
+
import { formField } from "@/lib/form"
|
|
9
|
+
import {
|
|
10
|
+
clearLlmSettings,
|
|
11
|
+
getLlmSettings,
|
|
12
|
+
saveLlmSettings,
|
|
13
|
+
testLlmSettings,
|
|
14
|
+
} from "@/server/ai-settings"
|
|
15
|
+
|
|
16
|
+
export const Route = createFileRoute("/app/settings/ai")({
|
|
17
|
+
loader: () => getLlmSettings(),
|
|
18
|
+
component: AiSettings,
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const providers = [
|
|
22
|
+
{ id: "anthropic", label: "Anthropic", model: "claude-sonnet-5-5" },
|
|
23
|
+
{ id: "openai", label: "OpenAI", model: "gpt-5" },
|
|
24
|
+
{ id: "openrouter", label: "OpenRouter", model: "anthropic/claude-sonnet-5-5" },
|
|
25
|
+
{ id: "ollama", label: "Ollama (OpenAI-compatible)", model: "llama3.2" },
|
|
26
|
+
] as const
|
|
27
|
+
|
|
28
|
+
type ProviderId = (typeof providers)[number]["id"]
|
|
29
|
+
|
|
30
|
+
const selectClass =
|
|
31
|
+
"h-9 w-full rounded-md border bg-transparent px-2 text-sm shadow-xs outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50"
|
|
32
|
+
|
|
33
|
+
const isProviderId = (value: string): value is ProviderId =>
|
|
34
|
+
providers.some((provider) => provider.id === value)
|
|
35
|
+
|
|
36
|
+
async function test() {
|
|
37
|
+
const result = await testLlmSettings()
|
|
38
|
+
if (result.ok) toast.success(`The model answered: ${result.value}`)
|
|
39
|
+
else toast.error(result.message)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function AiSettings() {
|
|
43
|
+
const settings = Route.useLoaderData()
|
|
44
|
+
const router = useRouter()
|
|
45
|
+
const [provider, setProvider] = useState<ProviderId>(
|
|
46
|
+
settings?.provider !== null &&
|
|
47
|
+
settings?.provider !== undefined &&
|
|
48
|
+
isProviderId(settings.provider)
|
|
49
|
+
? settings.provider
|
|
50
|
+
: "anthropic",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
if (settings === null)
|
|
54
|
+
return <p className="text-sm text-muted-foreground">Select an organization to set up AI.</p>
|
|
55
|
+
|
|
56
|
+
const { canManage, configured, last4 } = settings
|
|
57
|
+
const needsUrl = provider === "ollama" || provider === "openai"
|
|
58
|
+
|
|
59
|
+
async function save(event: SubmitEvent<HTMLFormElement>) {
|
|
60
|
+
event.preventDefault()
|
|
61
|
+
const form = event.currentTarget
|
|
62
|
+
const data = new FormData(form)
|
|
63
|
+
const result = await saveLlmSettings({
|
|
64
|
+
data: {
|
|
65
|
+
provider,
|
|
66
|
+
model: formField(data, "model"),
|
|
67
|
+
apiKey: formField(data, "apiKey"),
|
|
68
|
+
baseUrl: formField(data, "baseUrl"),
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
if (!result.ok) {
|
|
72
|
+
toast.error(result.message)
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
form.reset()
|
|
76
|
+
toast.success("AI settings saved")
|
|
77
|
+
await router.invalidate()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function clear() {
|
|
81
|
+
const result = await clearLlmSettings()
|
|
82
|
+
if (!result.ok) toast.error(result.message)
|
|
83
|
+
else toast.success("API key removed")
|
|
84
|
+
await router.invalidate()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<Card className="max-w-xl">
|
|
89
|
+
<CardHeader>
|
|
90
|
+
<CardTitle>AI provider</CardTitle>
|
|
91
|
+
<CardDescription>
|
|
92
|
+
Bring your own key. It is encrypted before it is stored and never sent back to the
|
|
93
|
+
browser.
|
|
94
|
+
</CardDescription>
|
|
95
|
+
</CardHeader>
|
|
96
|
+
<CardContent className="space-y-4">
|
|
97
|
+
{configured ? (
|
|
98
|
+
<p className="text-sm" data-testid="ai-status">
|
|
99
|
+
Using {settings.provider} / {settings.model}
|
|
100
|
+
{last4 === null ? (
|
|
101
|
+
""
|
|
102
|
+
) : (
|
|
103
|
+
<>
|
|
104
|
+
{" "}
|
|
105
|
+
with the key ending in <code>{last4}</code>
|
|
106
|
+
</>
|
|
107
|
+
)}
|
|
108
|
+
.
|
|
109
|
+
</p>
|
|
110
|
+
) : (
|
|
111
|
+
<p className="text-sm text-muted-foreground" data-testid="ai-status">
|
|
112
|
+
No key is set for this organization yet.
|
|
113
|
+
</p>
|
|
114
|
+
)}
|
|
115
|
+
{canManage ? (
|
|
116
|
+
<form onSubmit={(event) => void save(event)} className="space-y-4">
|
|
117
|
+
<div className="space-y-2">
|
|
118
|
+
<Label htmlFor="ai-provider">Provider</Label>
|
|
119
|
+
<select
|
|
120
|
+
id="ai-provider"
|
|
121
|
+
className={selectClass}
|
|
122
|
+
value={provider}
|
|
123
|
+
onChange={(event) => {
|
|
124
|
+
if (isProviderId(event.target.value)) setProvider(event.target.value)
|
|
125
|
+
}}
|
|
126
|
+
>
|
|
127
|
+
{providers.map(({ id, label }) => (
|
|
128
|
+
<option key={id} value={id}>
|
|
129
|
+
{label}
|
|
130
|
+
</option>
|
|
131
|
+
))}
|
|
132
|
+
</select>
|
|
133
|
+
</div>
|
|
134
|
+
<div className="space-y-2">
|
|
135
|
+
<Label htmlFor="ai-model">Model</Label>
|
|
136
|
+
<Input
|
|
137
|
+
id="ai-model"
|
|
138
|
+
name="model"
|
|
139
|
+
required
|
|
140
|
+
defaultValue={settings.model ?? undefined}
|
|
141
|
+
placeholder={providers.find((p) => p.id === provider)?.model}
|
|
142
|
+
key={provider}
|
|
143
|
+
/>
|
|
144
|
+
</div>
|
|
145
|
+
<div className="space-y-2">
|
|
146
|
+
<Label htmlFor="ai-key">API key</Label>
|
|
147
|
+
<Input
|
|
148
|
+
id="ai-key"
|
|
149
|
+
name="apiKey"
|
|
150
|
+
type="password"
|
|
151
|
+
autoComplete="off"
|
|
152
|
+
placeholder={configured ? "Leave empty to keep the current key" : "sk-..."}
|
|
153
|
+
/>
|
|
154
|
+
</div>
|
|
155
|
+
{needsUrl ? (
|
|
156
|
+
<div className="space-y-2">
|
|
157
|
+
<Label htmlFor="ai-base-url">Base URL</Label>
|
|
158
|
+
<Input
|
|
159
|
+
id="ai-base-url"
|
|
160
|
+
name="baseUrl"
|
|
161
|
+
type="url"
|
|
162
|
+
defaultValue={settings.baseUrl ?? undefined}
|
|
163
|
+
placeholder={
|
|
164
|
+
provider === "ollama"
|
|
165
|
+
? "http://localhost:11434/v1"
|
|
166
|
+
: "https://api.openai.com/v1"
|
|
167
|
+
}
|
|
168
|
+
/>
|
|
169
|
+
</div>
|
|
170
|
+
) : null}
|
|
171
|
+
<div className="flex gap-2">
|
|
172
|
+
<Button type="submit">Save</Button>
|
|
173
|
+
{configured ? (
|
|
174
|
+
<>
|
|
175
|
+
<Button type="button" variant="outline" onClick={() => void test()}>
|
|
176
|
+
Test
|
|
177
|
+
</Button>
|
|
178
|
+
<Button type="button" variant="outline" onClick={() => void clear()}>
|
|
179
|
+
Remove key
|
|
180
|
+
</Button>
|
|
181
|
+
</>
|
|
182
|
+
) : null}
|
|
183
|
+
</div>
|
|
184
|
+
</form>
|
|
185
|
+
) : (
|
|
186
|
+
<p className="text-sm text-muted-foreground">
|
|
187
|
+
Ask an owner or admin of this organization to change these settings.
|
|
188
|
+
</p>
|
|
189
|
+
)}
|
|
190
|
+
</CardContent>
|
|
191
|
+
</Card>
|
|
192
|
+
)
|
|
193
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"
|
|
2
|
+
import { organization } from "./auth-schema.ts"
|
|
3
|
+
|
|
4
|
+
/** One AI provider key per organization, AES-GCM encrypted (see packages/core/src/llm-crypto.ts). */
|
|
5
|
+
export const llmCredential = sqliteTable("llm_credential", {
|
|
6
|
+
orgId: text("org_id")
|
|
7
|
+
.primaryKey()
|
|
8
|
+
.references(() => organization.id, { onDelete: "cascade" }),
|
|
9
|
+
provider: text().notNull(),
|
|
10
|
+
model: text().notNull(),
|
|
11
|
+
encryptedKey: text("encrypted_key").notNull(),
|
|
12
|
+
baseUrl: text("base_url"),
|
|
13
|
+
updatedAt: integer("updated_at", { mode: "timestamp_ms" })
|
|
14
|
+
.notNull()
|
|
15
|
+
.$defaultFn(() => new Date()),
|
|
16
|
+
})
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { pgTable, text, timestamp } from "drizzle-orm/pg-core"
|
|
2
|
+
import { organization } from "./auth-schema.ts"
|
|
3
|
+
|
|
4
|
+
/** One AI provider key per organization, AES-GCM encrypted (see packages/core/src/llm-crypto.ts). */
|
|
5
|
+
export const llmCredential = pgTable("llm_credential", {
|
|
6
|
+
orgId: text("org_id")
|
|
7
|
+
.primaryKey()
|
|
8
|
+
.references(() => organization.id, { onDelete: "cascade" }),
|
|
9
|
+
provider: text().notNull(),
|
|
10
|
+
model: text().notNull(),
|
|
11
|
+
encryptedKey: text("encrypted_key").notNull(),
|
|
12
|
+
baseUrl: text("base_url"),
|
|
13
|
+
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
|
14
|
+
})
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { Auth } from "@repo/core/Auth"
|
|
2
|
+
import { Llm, LlmNotConfigured, llmProviders } from "@repo/core/llm"
|
|
3
|
+
import { createServerFn } from "@tanstack/react-start"
|
|
4
|
+
import { getRequestHeaders } from "@tanstack/react-start/server"
|
|
5
|
+
import { Data, Effect, Option, Schema } from "effect"
|
|
6
|
+
import { BetterAuth } from "./auth-live"
|
|
7
|
+
import { LlmCredentials } from "./llm"
|
|
8
|
+
import { runtime } from "./runtime"
|
|
9
|
+
|
|
10
|
+
class Forbidden extends Data.TaggedError("Forbidden")<{ readonly message: string }> {}
|
|
11
|
+
|
|
12
|
+
/** The signed-in user's active organization and whether they may change its AI settings (owner or admin). */
|
|
13
|
+
const currentOrganization = Effect.gen(function* () {
|
|
14
|
+
const auth = yield* Auth
|
|
15
|
+
const session = yield* auth.currentSession
|
|
16
|
+
const orgId = session.activeOrganizationId
|
|
17
|
+
if (orgId === null)
|
|
18
|
+
return yield* new LlmNotConfigured({ message: "Select an organization first." })
|
|
19
|
+
const betterAuth = yield* BetterAuth
|
|
20
|
+
const member = yield* Effect.promise(() =>
|
|
21
|
+
betterAuth.api.getActiveMember({ headers: getRequestHeaders() }),
|
|
22
|
+
)
|
|
23
|
+
const roles = member?.role.split(",") ?? []
|
|
24
|
+
return { orgId, canManage: roles.includes("owner") || roles.includes("admin") }
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const manager = Effect.gen(function* () {
|
|
28
|
+
const org = yield* currentOrganization
|
|
29
|
+
if (!org.canManage)
|
|
30
|
+
return yield* new Forbidden({
|
|
31
|
+
message: "Only organization owners and admins can change AI settings.",
|
|
32
|
+
})
|
|
33
|
+
return org.orgId
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
/** User-facing failure: the message, never the cause (which may carry request details). */
|
|
37
|
+
const report = <A, R>(effect: Effect.Effect<A, { readonly message: string }, R>) =>
|
|
38
|
+
effect.pipe(
|
|
39
|
+
Effect.map((value) => ({ ok: true as const, value })),
|
|
40
|
+
Effect.catch((error) => Effect.succeed({ ok: false as const, message: error.message })),
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
const baseUrlField = Schema.String.check(Schema.isMaxLength(2048))
|
|
44
|
+
|
|
45
|
+
const SaveInput = Schema.Struct({
|
|
46
|
+
provider: Schema.Literals(llmProviders),
|
|
47
|
+
model: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(200)),
|
|
48
|
+
apiKey: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(1000))),
|
|
49
|
+
baseUrl: Schema.optionalKey(baseUrlField),
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
/** Only OpenAI-compatible providers take a custom endpoint, and it must be an http(s) URL. */
|
|
53
|
+
const parseBaseUrl = (provider: string, value: string | undefined) =>
|
|
54
|
+
Effect.gen(function* () {
|
|
55
|
+
const text = value?.trim() ?? ""
|
|
56
|
+
if (text === "" || (provider !== "openai" && provider !== "ollama")) return null
|
|
57
|
+
const url = yield* Effect.try({
|
|
58
|
+
try: () => new URL(text),
|
|
59
|
+
catch: () => new LlmNotConfigured({ message: "Base URL must be a valid URL." }),
|
|
60
|
+
})
|
|
61
|
+
if (url.protocol !== "https:" && url.protocol !== "http:")
|
|
62
|
+
return yield* new LlmNotConfigured({
|
|
63
|
+
message: "Base URL must start with http:// or https://.",
|
|
64
|
+
})
|
|
65
|
+
return url.toString()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
/** This organization's AI settings. The key never leaves the server: only its last four characters do (to managers). */
|
|
69
|
+
export const getLlmSettings = createServerFn({ method: "GET" }).handler(() =>
|
|
70
|
+
runtime.runPromise(
|
|
71
|
+
Effect.gen(function* () {
|
|
72
|
+
const { orgId, canManage } = yield* currentOrganization
|
|
73
|
+
const credentials = yield* LlmCredentials
|
|
74
|
+
const summary = Option.getOrNull(yield* credentials.summary(orgId))
|
|
75
|
+
return {
|
|
76
|
+
canManage,
|
|
77
|
+
configured: summary !== null,
|
|
78
|
+
provider: summary?.provider ?? null,
|
|
79
|
+
model: summary?.model ?? null,
|
|
80
|
+
baseUrl: summary?.baseUrl ?? null,
|
|
81
|
+
last4: canManage ? (summary?.last4 ?? null) : null,
|
|
82
|
+
}
|
|
83
|
+
}).pipe(Effect.orElseSucceed(() => null)),
|
|
84
|
+
),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
export const saveLlmSettings = createServerFn({ method: "POST" })
|
|
88
|
+
.validator(Schema.toStandardSchemaV1(SaveInput))
|
|
89
|
+
.handler(({ data }) =>
|
|
90
|
+
runtime.runPromise(
|
|
91
|
+
report(
|
|
92
|
+
Effect.gen(function* () {
|
|
93
|
+
const orgId = yield* manager
|
|
94
|
+
const credentials = yield* LlmCredentials
|
|
95
|
+
const apiKey = data.apiKey?.trim()
|
|
96
|
+
yield* credentials.save(orgId, {
|
|
97
|
+
provider: data.provider,
|
|
98
|
+
model: data.model.trim(),
|
|
99
|
+
apiKey: apiKey === "" ? undefined : apiKey,
|
|
100
|
+
baseUrl: yield* parseBaseUrl(data.provider, data.baseUrl),
|
|
101
|
+
})
|
|
102
|
+
}),
|
|
103
|
+
),
|
|
104
|
+
),
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
export const clearLlmSettings = createServerFn({ method: "POST" }).handler(() =>
|
|
108
|
+
runtime.runPromise(
|
|
109
|
+
report(
|
|
110
|
+
Effect.gen(function* () {
|
|
111
|
+
const orgId = yield* manager
|
|
112
|
+
yield* (yield* LlmCredentials).clear(orgId)
|
|
113
|
+
}),
|
|
114
|
+
),
|
|
115
|
+
),
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
/** Sends a one-word prompt with the saved key to check that it works. */
|
|
119
|
+
export const testLlmSettings = createServerFn({ method: "POST" }).handler(() =>
|
|
120
|
+
runtime.runPromise(
|
|
121
|
+
report(
|
|
122
|
+
Effect.gen(function* () {
|
|
123
|
+
yield* manager
|
|
124
|
+
const llm = yield* Llm
|
|
125
|
+
return (yield* llm.generateText("Reply with the single word: ok")).slice(0, 200)
|
|
126
|
+
}),
|
|
127
|
+
),
|
|
128
|
+
),
|
|
129
|
+
)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Llm } from "@repo/core/llm"
|
|
2
|
+
import { createServerFn } from "@tanstack/react-start"
|
|
3
|
+
import { Effect, Schema } from "effect"
|
|
4
|
+
import { runtime } from "./runtime"
|
|
5
|
+
|
|
6
|
+
const GenerateInput = Schema.Struct({
|
|
7
|
+
prompt: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(4000)),
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Demo: sends `prompt` to the configured model and returns its text, or a message saying why not.
|
|
12
|
+
* Gated by `requireSession` when the project has auth. Call it from a component or a loader.
|
|
13
|
+
*/
|
|
14
|
+
export const generateText = createServerFn({ method: "POST" })
|
|
15
|
+
.validator(Schema.toStandardSchemaV1(GenerateInput))
|
|
16
|
+
.handler(({ data }) =>
|
|
17
|
+
runtime.runPromise(
|
|
18
|
+
Effect.gen(function* () {
|
|
19
|
+
const llm = yield* Llm
|
|
20
|
+
return { ok: true as const, text: yield* llm.generateText(data.prompt) }
|
|
21
|
+
}).pipe(
|
|
22
|
+
Effect.catch((error) => Effect.succeed({ ok: false as const, message: error.message })),
|
|
23
|
+
),
|
|
24
|
+
),
|
|
25
|
+
)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type LlmProvider, Llm, LlmNotConfigured, llmProviders } from "@repo/core/llm"
|
|
2
|
+
import { Config, type ConfigProvider, Effect, Layer, Option, Schema } from "effect"
|
|
3
|
+
import { WorkerConfig } from "effect-cf"
|
|
4
|
+
import { WorkerEnvLive } from "./cloudflare"
|
|
5
|
+
import { generateWith, type LlmSettings } from "./llm-provider"
|
|
6
|
+
|
|
7
|
+
const decodeProvider = Schema.decodeUnknownEffect(Schema.Literals(llmProviders))
|
|
8
|
+
|
|
9
|
+
const notConfigured = (message: string) => new LlmNotConfigured({ message })
|
|
10
|
+
|
|
11
|
+
/** Reads LLM_PROVIDER, LLM_MODEL, LLM_API_KEY and LLM_BASE_URL on each call, so a missing one fails that call only. */
|
|
12
|
+
const loadSettings = (provider: ConfigProvider.ConfigProvider) =>
|
|
13
|
+
Effect.gen(function* () {
|
|
14
|
+
const config = yield* Effect.all({
|
|
15
|
+
provider: Config.String("LLM_PROVIDER").parse(provider),
|
|
16
|
+
model: Config.String("LLM_MODEL").parse(provider),
|
|
17
|
+
apiKey: Config.option(Config.Redacted("LLM_API_KEY")).parse(provider),
|
|
18
|
+
baseUrl: Config.option(Config.String("LLM_BASE_URL")).parse(provider),
|
|
19
|
+
}).pipe(
|
|
20
|
+
Effect.mapError(() => notConfigured("Set LLM_PROVIDER and LLM_MODEL (and LLM_API_KEY).")),
|
|
21
|
+
)
|
|
22
|
+
const name: LlmProvider = yield* decodeProvider(config.provider).pipe(
|
|
23
|
+
Effect.mapError(() =>
|
|
24
|
+
notConfigured(`LLM_PROVIDER must be one of: ${llmProviders.join(", ")}.`),
|
|
25
|
+
),
|
|
26
|
+
)
|
|
27
|
+
if (name !== "ollama" && Option.isNone(config.apiKey))
|
|
28
|
+
return yield* notConfigured("LLM_API_KEY is not set.")
|
|
29
|
+
const settings: LlmSettings = {
|
|
30
|
+
provider: name,
|
|
31
|
+
model: config.model,
|
|
32
|
+
apiKey: Option.getOrUndefined(config.apiKey),
|
|
33
|
+
baseUrl: Option.getOrUndefined(Option.filter(config.baseUrl, (url) => url !== "")),
|
|
34
|
+
}
|
|
35
|
+
return settings
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
/** `Llm` configured from the environment: LLM_PROVIDER, LLM_MODEL, LLM_API_KEY, optional LLM_BASE_URL. */
|
|
39
|
+
export const LlmLive = Layer.effect(
|
|
40
|
+
Llm,
|
|
41
|
+
Effect.gen(function* () {
|
|
42
|
+
const provider = yield* WorkerConfig.provider
|
|
43
|
+
return Llm.of({
|
|
44
|
+
generateText: (prompt) =>
|
|
45
|
+
loadSettings(provider).pipe(Effect.flatMap((settings) => generateWith(settings, prompt))),
|
|
46
|
+
})
|
|
47
|
+
}),
|
|
48
|
+
).pipe(Layer.provide(WorkerEnvLive))
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { Auth } from "@repo/core/Auth"
|
|
2
|
+
import { Llm, LlmError, LlmNotConfigured, type LlmProvider, llmProviders } from "@repo/core/llm"
|
|
3
|
+
import { decryptSecret, encryptSecret, loadEncryptionKey } from "@repo/core/llm-crypto"
|
|
4
|
+
import { LlmCredentialStore } from "@repo/core/llm-store"
|
|
5
|
+
import { DatabaseLayer } from "@repo/db"
|
|
6
|
+
import { Config, Context, Effect, Layer, Option, Redacted } from "effect"
|
|
7
|
+
import { WorkerConfig } from "effect-cf"
|
|
8
|
+
import { AuthLive } from "./auth-live"
|
|
9
|
+
import { WorkerEnvLive } from "./cloudflare"
|
|
10
|
+
import { generateWith, type LlmSettings } from "./llm-provider"
|
|
11
|
+
import { LlmCredentialStoreLive } from "./llm-store"
|
|
12
|
+
|
|
13
|
+
/** What the settings page may see: never the key, only its last four characters. */
|
|
14
|
+
export interface LlmCredentialSummary {
|
|
15
|
+
readonly provider: LlmProvider
|
|
16
|
+
readonly model: string
|
|
17
|
+
readonly baseUrl: string | null
|
|
18
|
+
readonly last4: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface LlmCredentialInput {
|
|
22
|
+
readonly provider: LlmProvider
|
|
23
|
+
readonly model: string
|
|
24
|
+
/** Leave out to keep the stored key (same provider only). */
|
|
25
|
+
readonly apiKey: string | undefined
|
|
26
|
+
readonly baseUrl: string | null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const isProvider = (value: string): value is LlmProvider =>
|
|
30
|
+
llmProviders.some((provider) => provider === value)
|
|
31
|
+
|
|
32
|
+
/** Per-organization model settings with the key encrypted at rest. */
|
|
33
|
+
export class LlmCredentials extends Context.Service<
|
|
34
|
+
LlmCredentials,
|
|
35
|
+
{
|
|
36
|
+
readonly summary: (
|
|
37
|
+
orgId: string,
|
|
38
|
+
) => Effect.Effect<Option.Option<LlmCredentialSummary>, LlmError>
|
|
39
|
+
readonly save: (
|
|
40
|
+
orgId: string,
|
|
41
|
+
input: LlmCredentialInput,
|
|
42
|
+
) => Effect.Effect<void, LlmError | LlmNotConfigured>
|
|
43
|
+
readonly clear: (orgId: string) => Effect.Effect<void, LlmError>
|
|
44
|
+
/** The decrypted settings, for making a request. */
|
|
45
|
+
readonly resolve: (orgId: string) => Effect.Effect<LlmSettings, LlmError | LlmNotConfigured>
|
|
46
|
+
}
|
|
47
|
+
>()("LlmCredentials") {}
|
|
48
|
+
|
|
49
|
+
const none = () =>
|
|
50
|
+
new LlmNotConfigured({
|
|
51
|
+
message: "No API key is set for this organization. Add one in Settings > AI.",
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const LlmCredentialsLive = Layer.effect(
|
|
55
|
+
LlmCredentials,
|
|
56
|
+
Effect.gen(function* () {
|
|
57
|
+
const store = yield* LlmCredentialStore
|
|
58
|
+
const provider = yield* WorkerConfig.provider
|
|
59
|
+
// built with the layer: a missing or malformed LLM_ENCRYPTION_KEY stops the app at startup
|
|
60
|
+
const key = yield* loadEncryptionKey(
|
|
61
|
+
Option.getOrUndefined(
|
|
62
|
+
yield* Config.option(Config.String("LLM_ENCRYPTION_KEY")).parse(provider),
|
|
63
|
+
),
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
const open = (row: { orgId: string; encryptedKey: string }) =>
|
|
67
|
+
decryptSecret(key, row.encryptedKey, row.orgId)
|
|
68
|
+
|
|
69
|
+
return LlmCredentials.of({
|
|
70
|
+
summary: (orgId) =>
|
|
71
|
+
Effect.gen(function* () {
|
|
72
|
+
const found = yield* store.find(orgId)
|
|
73
|
+
if (Option.isNone(found) || !isProvider(found.value.provider)) return Option.none()
|
|
74
|
+
const apiKey = yield* open(found.value)
|
|
75
|
+
return Option.some({
|
|
76
|
+
provider: found.value.provider,
|
|
77
|
+
model: found.value.model,
|
|
78
|
+
baseUrl: found.value.baseUrl,
|
|
79
|
+
last4: apiKey.slice(-4),
|
|
80
|
+
})
|
|
81
|
+
}),
|
|
82
|
+
save: (orgId, input) =>
|
|
83
|
+
Effect.gen(function* () {
|
|
84
|
+
const existing = yield* store.find(orgId)
|
|
85
|
+
const keep =
|
|
86
|
+
input.apiKey === undefined &&
|
|
87
|
+
Option.isSome(existing) &&
|
|
88
|
+
existing.value.provider === input.provider
|
|
89
|
+
if (input.apiKey === undefined && input.provider !== "ollama" && !keep)
|
|
90
|
+
return yield* new LlmNotConfigured({ message: "Enter an API key." })
|
|
91
|
+
const encryptedKey =
|
|
92
|
+
keep && Option.isSome(existing)
|
|
93
|
+
? existing.value.encryptedKey
|
|
94
|
+
: yield* encryptSecret(key, input.apiKey ?? "", orgId)
|
|
95
|
+
return yield* store.upsert({
|
|
96
|
+
orgId,
|
|
97
|
+
provider: input.provider,
|
|
98
|
+
model: input.model,
|
|
99
|
+
encryptedKey,
|
|
100
|
+
baseUrl: input.baseUrl,
|
|
101
|
+
})
|
|
102
|
+
}),
|
|
103
|
+
clear: (orgId) => store.remove(orgId),
|
|
104
|
+
resolve: (orgId) =>
|
|
105
|
+
Effect.gen(function* () {
|
|
106
|
+
const found = yield* store.find(orgId)
|
|
107
|
+
if (Option.isNone(found) || !isProvider(found.value.provider)) return yield* none()
|
|
108
|
+
const apiKey = yield* open(found.value)
|
|
109
|
+
return {
|
|
110
|
+
provider: found.value.provider,
|
|
111
|
+
model: found.value.model,
|
|
112
|
+
apiKey: apiKey === "" ? undefined : Redacted.make(apiKey),
|
|
113
|
+
baseUrl: found.value.baseUrl ?? undefined,
|
|
114
|
+
}
|
|
115
|
+
}),
|
|
116
|
+
})
|
|
117
|
+
}),
|
|
118
|
+
).pipe(
|
|
119
|
+
Layer.provide(LlmCredentialStoreLive),
|
|
120
|
+
Layer.provide(DatabaseLayer),
|
|
121
|
+
Layer.provide(WorkerEnvLive),
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* `Llm` using the key the current organization saved in Settings > AI. Provides `LlmCredentials`
|
|
126
|
+
* too (the settings server functions use it). Needs `Auth` to know whose organization is calling.
|
|
127
|
+
*/
|
|
128
|
+
export const LlmLive = Layer.effect(
|
|
129
|
+
Llm,
|
|
130
|
+
Effect.gen(function* () {
|
|
131
|
+
const auth = yield* Auth
|
|
132
|
+
const credentials = yield* LlmCredentials
|
|
133
|
+
return Llm.of({
|
|
134
|
+
generateText: (prompt) =>
|
|
135
|
+
Effect.gen(function* () {
|
|
136
|
+
const session = yield* auth.currentSession.pipe(
|
|
137
|
+
Effect.mapError(() => new LlmNotConfigured({ message: "Sign in to use AI." })),
|
|
138
|
+
)
|
|
139
|
+
if (session.activeOrganizationId === null)
|
|
140
|
+
return yield* new LlmNotConfigured({ message: "Select an organization first." })
|
|
141
|
+
const settings = yield* credentials.resolve(session.activeOrganizationId)
|
|
142
|
+
return yield* generateWith(settings, prompt)
|
|
143
|
+
}),
|
|
144
|
+
})
|
|
145
|
+
}),
|
|
146
|
+
).pipe(Layer.provideMerge(LlmCredentialsLive), Layer.provide(AuthLive))
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { AnthropicClient, AnthropicLanguageModel } from "@effect/ai-anthropic"
|
|
2
|
+
import { OpenAiClient, OpenAiLanguageModel } from "@effect/ai-openai"
|
|
3
|
+
import { OpenRouterClient, OpenRouterLanguageModel } from "@effect/ai-openrouter"
|
|
4
|
+
import { LlmError, type LlmProvider } from "@repo/core/llm"
|
|
5
|
+
import { Effect, Layer, type Redacted } from "effect"
|
|
6
|
+
import { LanguageModel } from "effect/ai"
|
|
7
|
+
import { FetchHttpClient, type HttpClient } from "effect/http"
|
|
8
|
+
|
|
9
|
+
export interface LlmSettings {
|
|
10
|
+
readonly provider: LlmProvider
|
|
11
|
+
/** Not needed for a local Ollama. */
|
|
12
|
+
readonly apiKey: Redacted.Redacted | undefined
|
|
13
|
+
readonly model: string
|
|
14
|
+
/** OpenAI-compatible endpoint (`openai`, `ollama`); other providers ignore it. */
|
|
15
|
+
readonly baseUrl?: string | undefined
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Where Ollama listens by default; it speaks the OpenAI API under `/v1`. */
|
|
19
|
+
export const OLLAMA_URL = "http://localhost:11434/v1"
|
|
20
|
+
|
|
21
|
+
type ProviderLayer = Layer.Layer<LanguageModel.LanguageModel, never, HttpClient.HttpClient>
|
|
22
|
+
|
|
23
|
+
/** One entry per provider: the model layer on top of that provider's client. */
|
|
24
|
+
const providerLayers: Record<LlmProvider, (settings: LlmSettings) => ProviderLayer> = {
|
|
25
|
+
anthropic: ({ apiKey, model }) =>
|
|
26
|
+
AnthropicLanguageModel.layer({ model }).pipe(Layer.provide(AnthropicClient.layer({ apiKey }))),
|
|
27
|
+
openrouter: ({ apiKey, model }) =>
|
|
28
|
+
OpenRouterLanguageModel.layer({ model }).pipe(
|
|
29
|
+
Layer.provide(OpenRouterClient.layer({ apiKey })),
|
|
30
|
+
),
|
|
31
|
+
openai: ({ apiKey, model, baseUrl }) =>
|
|
32
|
+
OpenAiLanguageModel.layer({ model }).pipe(
|
|
33
|
+
Layer.provide(OpenAiClient.layer({ apiKey, apiUrl: baseUrl })),
|
|
34
|
+
),
|
|
35
|
+
ollama: ({ apiKey, model, baseUrl }) =>
|
|
36
|
+
OpenAiLanguageModel.layer({ model }).pipe(
|
|
37
|
+
Layer.provide(OpenAiClient.layer({ apiKey, apiUrl: baseUrl ?? OLLAMA_URL })),
|
|
38
|
+
),
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The `LanguageModel` for `{ provider, apiKey, model, baseUrl }`. Switching provider changes this
|
|
43
|
+
* layer and nothing else: app code calls `LanguageModel.generateText` (or the `Llm` service) either way.
|
|
44
|
+
*/
|
|
45
|
+
export const makeLlmLayer = (settings: LlmSettings): Layer.Layer<LanguageModel.LanguageModel> =>
|
|
46
|
+
providerLayers[settings.provider](settings).pipe(Layer.provide(FetchHttpClient.layer))
|
|
47
|
+
|
|
48
|
+
/** One `generateText` call against `settings`. */
|
|
49
|
+
export const generateWith = (settings: LlmSettings, prompt: string) =>
|
|
50
|
+
LanguageModel.generateText({ prompt }).pipe(
|
|
51
|
+
Effect.map((response) => response.text),
|
|
52
|
+
// the layer depends on this call's settings, so it is built here rather than at an entry point
|
|
53
|
+
// oxlint-disable-next-line effecttsgo/strict-effect-provide
|
|
54
|
+
Effect.provide(makeLlmLayer(settings)),
|
|
55
|
+
Effect.mapError((cause) => new LlmError({ operation: "generateText", cause })),
|
|
56
|
+
)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Database } from "@repo/core/Database"
|
|
2
|
+
import { LlmError } from "@repo/core/llm"
|
|
3
|
+
import { LlmCredentialStore } from "@repo/core/llm-store"
|
|
4
|
+
import { llmCredential } from "@repo/db"
|
|
5
|
+
import { eq } from "drizzle-orm"
|
|
6
|
+
import { Effect, Layer, Option } from "effect"
|
|
7
|
+
|
|
8
|
+
const fail = (operation: string) => (cause: unknown) => new LlmError({ operation, cause })
|
|
9
|
+
|
|
10
|
+
/** `LlmCredentialStore` over the `llm_credential` table, through the Database service. */
|
|
11
|
+
export const LlmCredentialStoreLive = Layer.effect(
|
|
12
|
+
LlmCredentialStore,
|
|
13
|
+
Effect.gen(function* () {
|
|
14
|
+
const { db } = yield* Database
|
|
15
|
+
return LlmCredentialStore.of({
|
|
16
|
+
find: (orgId) =>
|
|
17
|
+
db
|
|
18
|
+
.select()
|
|
19
|
+
.from(llmCredential)
|
|
20
|
+
.where(eq(llmCredential.orgId, orgId))
|
|
21
|
+
.limit(1)
|
|
22
|
+
.pipe(
|
|
23
|
+
Effect.map((rows) => Option.fromNullishOr(rows[0])),
|
|
24
|
+
Effect.mapError(fail("find credential")),
|
|
25
|
+
),
|
|
26
|
+
upsert: (row) =>
|
|
27
|
+
db
|
|
28
|
+
.insert(llmCredential)
|
|
29
|
+
.values(row)
|
|
30
|
+
.onConflictDoUpdate({
|
|
31
|
+
target: llmCredential.orgId,
|
|
32
|
+
set: {
|
|
33
|
+
provider: row.provider,
|
|
34
|
+
model: row.model,
|
|
35
|
+
encryptedKey: row.encryptedKey,
|
|
36
|
+
baseUrl: row.baseUrl,
|
|
37
|
+
updatedAt: new Date(),
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
.pipe(Effect.asVoid, Effect.mapError(fail("save credential"))),
|
|
41
|
+
remove: (orgId) =>
|
|
42
|
+
db
|
|
43
|
+
.delete(llmCredential)
|
|
44
|
+
.where(eq(llmCredential.orgId, orgId))
|
|
45
|
+
.pipe(Effect.asVoid, Effect.mapError(fail("clear credential"))),
|
|
46
|
+
})
|
|
47
|
+
}),
|
|
48
|
+
)
|