@polymorphism-tech/morph-spec 4.15.0 → 4.17.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 (100) hide show
  1. package/README.md +2 -2
  2. package/bin/morph-spec.js +4 -0
  3. package/bin/task-manager.js +999 -962
  4. package/claude-plugin.json +1 -1
  5. package/docs/CHEATSHEET.md +1 -1
  6. package/docs/QUICKSTART.md +1 -1
  7. package/framework/agents/ai-agents/vector-search-expert.md +247 -233
  8. package/framework/agents/backend/ms-agent-expert.md +2 -1
  9. package/framework/agents/backend/neon-expert.md +397 -0
  10. package/framework/agents/frontend/gsap-animator.md +131 -0
  11. package/framework/agents.json +1175 -1077
  12. package/framework/commands/morph-preflight.md +200 -192
  13. package/framework/hooks/claude-code/session-start/inject-morph-context.js +1 -1
  14. package/framework/rules/gsap-standards.md +13 -0
  15. package/framework/rules/neon-standards.md +25 -0
  16. package/framework/skills/level-0-meta/mcp-registry.json +1 -1
  17. package/framework/skills/level-0-meta/morph-agent-teams/SKILL.md +472 -472
  18. package/framework/skills/level-0-meta/morph-init/SKILL.md +30 -11
  19. package/framework/skills/level-0-meta/morph-post-implementation/SKILL.md +5 -5
  20. package/framework/skills/level-0-meta/morph-tool-usage-guide/SKILL.md +5 -5
  21. package/framework/skills/level-0-meta/morph-tool-usage-guide/references/tools-per-phase.md +7 -7
  22. package/framework/skills/level-1-workflows/morph-phase-codebase-analysis/SKILL.md +24 -24
  23. package/framework/skills/level-1-workflows/morph-phase-design/SKILL.md +12 -12
  24. package/framework/skills/level-1-workflows/morph-phase-implement/SKILL.md +9 -9
  25. package/framework/skills/level-1-workflows/morph-phase-plan/SKILL.md +1 -1
  26. package/framework/skills/level-1-workflows/morph-phase-tasks/SKILL.md +1 -1
  27. package/framework/skills/level-1-workflows/morph-phase-tasks/references/task-planning-patterns.md +143 -143
  28. package/framework/skills/level-1-workflows/morph-phase-uiux/SKILL.md +1 -1
  29. package/framework/skills/level-1-workflows/morph-scope-escalation/SKILL.md +1 -1
  30. package/framework/standards/STANDARDS.json +184 -107
  31. package/framework/standards/ai-agents/production.md +1 -0
  32. package/framework/standards/ai-agents/setup.md +3 -2
  33. package/framework/standards/ai-agents/workflows.md +1 -0
  34. package/framework/standards/architecture/vertical-slice/vertical-slice.md +4 -4
  35. package/framework/standards/backend/database/postgresql/database.md +58 -78
  36. package/framework/standards/backend/dotnet/scalar-ui.md +172 -172
  37. package/framework/standards/backend/dotnet/vsa-handler-patterns.md +274 -274
  38. package/framework/standards/backend/integrations/asaas/asaas-api.md +1 -1
  39. package/framework/standards/backend/integrations/clerk/clerk-auth.md +23 -21
  40. package/framework/standards/frontend/design-system/animations.md +9 -0
  41. package/framework/standards/frontend/gsap/gsap-core.md +463 -0
  42. package/framework/standards/frontend/gsap/gsap-plugins.md +294 -0
  43. package/framework/standards/frontend/gsap/gsap-scrolltrigger.md +318 -0
  44. package/framework/standards/frontend/nextjs/motion-patterns.md +1 -0
  45. package/framework/standards/frontend/nextjs/nextjs-patterns.md +47 -37
  46. package/framework/standards/infrastructure/docker/easypanel-deploy.md +4 -8
  47. package/framework/standards/infrastructure/neon/neon-pgrag.md +239 -0
  48. package/framework/standards/infrastructure/neon/neon-pgvector.md +236 -0
  49. package/framework/standards/infrastructure/neon/neon-semantic-kernel.md +310 -0
  50. package/framework/standards/infrastructure/neon/neon-setup.md +186 -0
  51. package/framework/standards/integration/mcp/mcp-tools.md +32 -34
  52. package/framework/templates/README.md +18 -18
  53. package/framework/templates/REGISTRY.json +49 -96
  54. package/framework/templates/code/dotnet/abstractions/api-endpoint.cs +7 -7
  55. package/framework/templates/code/dotnet/backend/repository.cs +148 -148
  56. package/framework/templates/code/dotnet/contracts/contracts-vsa.cs +2 -2
  57. package/framework/templates/code/dotnet/database/vector-entity.cs +93 -0
  58. package/framework/templates/code/dotnet/feature/endpoint.cs.hbs +1 -1
  59. package/framework/templates/code/sql/{supabase-migration.sql → postgresql-migration.sql} +8 -17
  60. package/framework/templates/code/sql/{supabase-migration.template.sql → postgresql-migration.template.sql} +10 -13
  61. package/framework/templates/code/sql/{rls-policy.sql → postgresql-rls-policy.sql} +8 -8
  62. package/framework/templates/code/typescript/contracts.ts +167 -168
  63. package/framework/templates/context/CONTEXT.md +3 -3
  64. package/framework/templates/docs/proposal.md +6 -6
  65. package/framework/templates/docs/ui-components.md +1 -1
  66. package/framework/templates/feature/decisions.md +121 -121
  67. package/framework/templates/feature/recap.md +145 -145
  68. package/framework/templates/feature/tasks.md +199 -199
  69. package/framework/templates/frontend/nextjs/gsap-scroll-section.tsx.hbs +44 -0
  70. package/framework/templates/infrastructure/docker/docker-compose.template.yml +50 -67
  71. package/framework/templates/infrastructure/docker/dockerfile-api.dockerfile +38 -38
  72. package/framework/templates/infrastructure/docker/dockerfile-web.dockerfile +48 -48
  73. package/framework/templates/infrastructure/docker/easypanel.template.json +46 -54
  74. package/framework/templates/infrastructure/github/README.md +56 -56
  75. package/framework/templates/integrations/semantic-kernel-neon.cs +125 -0
  76. package/framework/workflows/README.md +1 -1
  77. package/framework/workflows/docs/full-morph.md +69 -69
  78. package/package.json +1 -1
  79. package/src/commands/agents/dispatch-agents.js +19 -15
  80. package/src/commands/project/setup-infra-cmd.js +1 -1
  81. package/src/commands/state/advance-phase.js +5 -0
  82. package/src/commands/tasks/task.js +3 -0
  83. package/src/core/state/state-manager.js +45 -1
  84. package/src/core/templates/template-registry.js +1 -66
  85. package/src/lib/detectors/index.js +2 -2
  86. package/src/lib/generators/architecture-scanner.js +640 -640
  87. package/src/lib/installers/mcp-installer.js +1 -1
  88. package/src/lib/tasks/task-classifier.js +15 -1
  89. package/src/lib/tasks/test-runner.js +5 -1
  90. package/src/lib/validators/content/content-validator.js +9 -10
  91. package/src/lib/validators/validation-runner.js +7 -5
  92. package/src/scripts/setup-infra.js +60 -30
  93. package/framework/agents/backend/supabase-expert.md +0 -256
  94. package/framework/rules/supabase-standards.md +0 -29
  95. package/framework/standards/infrastructure/supabase/mcp-setup.md +0 -142
  96. package/framework/standards/infrastructure/supabase/supabase-auth.md +0 -176
  97. package/framework/standards/infrastructure/supabase/supabase-custom-jwt.md +0 -206
  98. package/framework/standards/infrastructure/supabase/supabase-pgvector.md +0 -169
  99. package/framework/standards/infrastructure/supabase/supabase-rls.md +0 -184
  100. package/framework/standards/infrastructure/supabase/supabase-storage.md +0 -153
@@ -1,176 +0,0 @@
1
- # Supabase Authentication Standard
2
-
3
- > **Scope:** nextjs-supabase
4
- > **Layer:** 2 (on keyword)
5
- > **Keywords:** supabase, auth, rls, user, authentication
6
- > **Load When:** supabase auth keywords detected
7
-
8
- Stack: Next.js 15 + Supabase + .NET Backend
9
-
10
- ## Core Rules
11
-
12
- - NEVER use `supabase.auth.getSession()` on server -- reads from cookies without validation
13
- - ALWAYS use `supabase.auth.getUser()` on server -- validates JWT with Supabase
14
- - NEVER expose `service_role` key on frontend -- bypasses RLS
15
- - ALWAYS use `@supabase/ssr` for Next.js -- not `@supabase/auth-helpers-nextjs` (deprecated)
16
- - ALWAYS use PKCE flow for SSR auth
17
-
18
- ## Client Setup
19
-
20
- ### Browser Client
21
-
22
- ```ts
23
- // lib/supabase/client.ts
24
- import { createBrowserClient } from "@supabase/ssr";
25
-
26
- export function createClient() {
27
- return createBrowserClient(
28
- process.env.NEXT_PUBLIC_SUPABASE_URL!,
29
- process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
30
- );
31
- }
32
- ```
33
-
34
- ### Server Client
35
-
36
- ```ts
37
- // lib/supabase/server.ts
38
- import { createServerClient } from "@supabase/ssr";
39
- import { cookies } from "next/headers";
40
-
41
- export async function createClient() {
42
- const cookieStore = await cookies();
43
- return createServerClient(
44
- process.env.NEXT_PUBLIC_SUPABASE_URL!,
45
- process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
46
- {
47
- cookies: {
48
- getAll() { return cookieStore.getAll(); },
49
- setAll(cookiesToSet) {
50
- cookiesToSet.forEach(({ name, value, options }) =>
51
- cookieStore.set(name, value, options));
52
- },
53
- },
54
- }
55
- );
56
- }
57
- ```
58
-
59
- ## Auth Flows
60
-
61
- ```ts
62
- // Email/Password sign up
63
- await supabase.auth.signUp({ email, password,
64
- options: { emailRedirectTo: `${origin}/auth/callback` } });
65
-
66
- // Email/Password sign in
67
- await supabase.auth.signInWithPassword({ email, password });
68
-
69
- // OAuth (Google / GitHub)
70
- await supabase.auth.signInWithOAuth({
71
- provider: "google", // or "github"
72
- options: { redirectTo: `${origin}/auth/callback`,
73
- queryParams: { access_type: "offline", prompt: "consent" } } // Google-specific
74
- });
75
-
76
- // Magic Link
77
- await supabase.auth.signInWithOtp({ email,
78
- options: { emailRedirectTo: `${origin}/auth/callback` } });
79
- ```
80
-
81
- ## Auth Callback Route (PKCE)
82
-
83
- ```ts
84
- // app/auth/callback/route.ts
85
- import { createClient } from "@/lib/supabase/server";
86
- import { NextResponse } from "next/server";
87
-
88
- export async function GET(request: Request) {
89
- const { searchParams, origin } = new URL(request.url);
90
- const code = searchParams.get("code");
91
- const next = searchParams.get("next") ?? "/dashboard";
92
- if (code) {
93
- const supabase = await createClient();
94
- const { error } = await supabase.auth.exchangeCodeForSession(code);
95
- if (!error) return NextResponse.redirect(`${origin}${next}`);
96
- }
97
- return NextResponse.redirect(`${origin}/auth/error`);
98
- }
99
- ```
100
-
101
- ## Middleware Pattern
102
-
103
- ```ts
104
- // middleware.ts
105
- import { createServerClient } from "@supabase/ssr";
106
- import { NextResponse, type NextRequest } from "next/server";
107
-
108
- export async function middleware(request: NextRequest) {
109
- let supabaseResponse = NextResponse.next({ request });
110
- const supabase = createServerClient(
111
- process.env.NEXT_PUBLIC_SUPABASE_URL!,
112
- process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
113
- {
114
- cookies: {
115
- getAll() { return request.cookies.getAll(); },
116
- setAll(cookiesToSet) {
117
- cookiesToSet.forEach(({ name, value, options }) => {
118
- request.cookies.set(name, value);
119
- supabaseResponse.cookies.set(name, value, options);
120
- });
121
- },
122
- },
123
- }
124
- );
125
- const { data: { user } } = await supabase.auth.getUser();
126
- if (!user && request.nextUrl.pathname.startsWith("/dashboard"))
127
- return NextResponse.redirect(new URL("/login", request.url));
128
- return supabaseResponse;
129
- }
130
-
131
- export const config = {
132
- matcher: ["/((?!_next/static|_next/image|favicon.ico|api/webhooks).*)"],
133
- };
134
- ```
135
-
136
- ## .NET JWT Validation
137
-
138
- ```csharp
139
- builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
140
- .AddJwtBearer(options => {
141
- options.TokenValidationParameters = new TokenValidationParameters {
142
- ValidateIssuer = true,
143
- ValidIssuer = $"https://{supabaseProjectRef}.supabase.co/auth/v1",
144
- ValidateAudience = true,
145
- ValidAudience = "authenticated",
146
- ValidateIssuerSigningKey = true,
147
- IssuerSigningKey = new SymmetricSecurityKey(
148
- Encoding.UTF8.GetBytes(supabaseJwtSecret)),
149
- ValidateLifetime = true,
150
- ClockSkew = TimeSpan.FromSeconds(30)
151
- };
152
- });
153
-
154
- // Extract user ID: maps to auth.uid()
155
- var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
156
- ```
157
-
158
- ## Environment Variables
159
-
160
- | Variable | Where | Purpose |
161
- |----------|-------|---------|
162
- | `NEXT_PUBLIC_SUPABASE_URL` | Frontend | Supabase project URL |
163
- | `NEXT_PUBLIC_SUPABASE_ANON_KEY` | Frontend | Public anon key (respects RLS) |
164
- | `SUPABASE_SERVICE_ROLE_KEY` | Backend ONLY | Bypasses RLS -- NEVER on frontend |
165
- | `SUPABASE_JWT_SECRET` | Backend ONLY | JWT validation secret |
166
-
167
- ## Common Mistakes
168
-
169
- | Wrong | Right | Why |
170
- |-------|-------|-----|
171
- | `getSession()` on server | `getUser()` on server | getSession reads unvalidated cookie data |
172
- | `@supabase/auth-helpers-nextjs` | `@supabase/ssr` | auth-helpers is deprecated |
173
- | `service_role` in `NEXT_PUBLIC_*` | `anon` key in `NEXT_PUBLIC_*` | service_role bypasses all RLS |
174
- | Implicit flow for SSR | PKCE flow with code exchange | Implicit exposes tokens in URL fragments |
175
- | Auth only in page components | Auth check in middleware.ts | Middleware prevents flash of content |
176
- | Missing `setAll` in cookie config | Both `getAll` and `setAll` | Session refresh silently fails without setAll |
@@ -1,206 +0,0 @@
1
- # Supabase Custom JWT Standard
2
-
3
- > **Scope:** dotnet, nextjs
4
- > **Layer:** 2 (on keyword)
5
- > **Keywords:** JWT, custom JWT, JWT signing, token creation, JWT claims, custom claims
6
- > **Load When:** custom JWT patterns detected (e.g., custom claims, manual token creation)
7
-
8
- ## JWT Token Creation
9
-
10
- ### Node.js (jose)
11
-
12
- ```ts
13
- import { SignJWT } from "jose";
14
-
15
- const secret = new TextEncoder().encode(process.env.SUPABASE_JWT_SECRET!);
16
-
17
- export async function createCustomJWT(userId: string, claims: Record<string, unknown>) {
18
- return new SignJWT({
19
- sub: userId,
20
- role: "authenticated",
21
- app_metadata: claims,
22
- aud: "authenticated",
23
- })
24
- .setProtectedHeader({ alg: "HS256", typ: "JWT" })
25
- .setIssuedAt()
26
- .setExpirationTime("1h")
27
- .sign(secret);
28
- }
29
- ```
30
-
31
- ### .NET (System.IdentityModel.Tokens.Jwt)
32
-
33
- ```csharp
34
- using System.IdentityModel.Tokens.Jwt;
35
- using System.Security.Claims;
36
- using Microsoft.IdentityModel.Tokens;
37
-
38
- public static string CreateCustomJWT(string userId, Dictionary<string, string> appMetadata)
39
- {
40
- var key = new SymmetricSecurityKey(
41
- Encoding.UTF8.GetBytes(Environment.GetEnvironmentVariable("SUPABASE_JWT_SECRET")!));
42
-
43
- var claims = new List<Claim>
44
- {
45
- new(JwtRegisteredClaimNames.Sub, userId),
46
- new("role", "authenticated"),
47
- new("aud", "authenticated"),
48
- };
49
-
50
- // Add app_metadata claims
51
- foreach (var (k, v) in appMetadata)
52
- claims.Add(new Claim($"app_metadata.{k}", v));
53
-
54
- var token = new JwtSecurityToken(
55
- claims: claims,
56
- expires: DateTime.UtcNow.AddHours(1),
57
- signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256));
58
-
59
- return new JwtSecurityTokenHandler().WriteToken(token);
60
- }
61
- ```
62
-
63
- ## Custom Claims Structure
64
-
65
- ### Required Claims
66
-
67
- | Claim | Description | Example |
68
- |-------|-------------|---------|
69
- | `sub` | User ID (UUID) | `"a1b2c3d4-..."` |
70
- | `role` | Supabase role | `"authenticated"` |
71
- | `aud` | Audience | `"authenticated"` |
72
- | `exp` | Expiration (epoch) | `1710000000` |
73
-
74
- ### Recommended App Metadata Claims
75
-
76
- | Claim | Purpose | Used By |
77
- |-------|---------|---------|
78
- | `tenant_id` | Multi-tenant RLS filtering | RLS policies |
79
- | `org_id` | Organization scoping | RLS policies |
80
- | `permissions` | Fine-grained access | Application logic |
81
-
82
- ### Structure
83
-
84
- ```json
85
- {
86
- "sub": "user-uuid",
87
- "role": "authenticated",
88
- "aud": "authenticated",
89
- "iat": 1710000000,
90
- "exp": 1710003600,
91
- "app_metadata": {
92
- "tenant_id": "tenant-uuid",
93
- "org_id": "org-uuid",
94
- "permissions": ["read", "write"]
95
- }
96
- }
97
- ```
98
-
99
- **RLS Integration:** Always include `tenant_id` in `app_metadata` for RLS policies. Access in SQL via:
100
-
101
- ```sql
102
- -- In RLS policy
103
- (auth.jwt() -> 'app_metadata' ->> 'tenant_id')::uuid = tenant_id
104
- ```
105
-
106
- Cross-ref: [supabase-rls.md](supabase-rls.md)
107
-
108
- ## JWT Refresh Patterns
109
-
110
- ### Token Rotation (Recommended)
111
-
112
- 1. Issue short-lived access tokens (1h max)
113
- 2. Use refresh tokens with rotation enabled
114
- 3. Invalidate old refresh token on each rotation
115
- 4. Store refresh tokens server-side only — never expose to client JS
116
-
117
- ### Node.js Refresh Flow
118
-
119
- ```ts
120
- import { createClient } from "@supabase/supabase-js";
121
-
122
- const supabase = createClient(url, anonKey);
123
-
124
- // On auth state change, handle token refresh
125
- supabase.auth.onAuthStateChange((event, session) => {
126
- if (event === "TOKEN_REFRESHED" && session) {
127
- // Update server-side session cookie
128
- updateSessionCookie(session.access_token, session.refresh_token);
129
- }
130
- });
131
- ```
132
-
133
- ### .NET Refresh Flow
134
-
135
- ```csharp
136
- // Validate and refresh on each request via middleware
137
- public async Task InvokeAsync(HttpContext context)
138
- {
139
- var token = context.Request.Headers.Authorization.ToString().Replace("Bearer ", "");
140
-
141
- var handler = new JwtSecurityTokenHandler();
142
- var jwt = handler.ReadJwtToken(token);
143
-
144
- // If token expires within 5 minutes, trigger refresh
145
- if (jwt.ValidTo < DateTime.UtcNow.AddMinutes(5))
146
- {
147
- var newToken = await RefreshTokenAsync(jwt.Subject);
148
- context.Response.Headers.Append("X-New-Token", newToken);
149
- }
150
-
151
- await _next(context);
152
- }
153
- ```
154
-
155
- ## Validation Setup
156
-
157
- ### Environment Variables
158
-
159
- ```env
160
- # Required — never commit to source control
161
- SUPABASE_JWT_SECRET=your-jwt-secret-from-supabase-dashboard
162
- ```
163
-
164
- - **Always** store JWT secret in environment variables or a secrets manager
165
- - **Never** hardcode secrets in source code
166
- - **Use HS256** for shared secrets (default Supabase setup)
167
- - **Use RS256** for asymmetric keys (advanced: separate sign/verify keys)
168
-
169
- ### Validation in .NET
170
-
171
- ```csharp
172
- builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
173
- .AddJwtBearer(options =>
174
- {
175
- options.TokenValidationParameters = new TokenValidationParameters
176
- {
177
- ValidateIssuerSigningKey = true,
178
- IssuerSigningKey = new SymmetricSecurityKey(
179
- Encoding.UTF8.GetBytes(builder.Configuration["Supabase:JwtSecret"]!)),
180
- ValidateIssuer = false,
181
- ValidateAudience = true,
182
- ValidAudience = "authenticated",
183
- ClockSkew = TimeSpan.FromSeconds(30),
184
- };
185
- });
186
- ```
187
-
188
- ### Validation in Node.js
189
-
190
- ```ts
191
- import { jwtVerify } from "jose";
192
-
193
- const secret = new TextEncoder().encode(process.env.SUPABASE_JWT_SECRET!);
194
-
195
- export async function verifyJWT(token: string) {
196
- const { payload } = await jwtVerify(token, secret, {
197
- audience: "authenticated",
198
- });
199
- return payload;
200
- }
201
- ```
202
-
203
- ## Cross-References
204
-
205
- - [Supabase Auth](supabase-auth.md) — authentication flows and client setup
206
- - [Supabase RLS](supabase-rls.md) — row-level security policies using JWT claims
@@ -1,169 +0,0 @@
1
- # Supabase pgvector Standard
2
-
3
- > **Scope:** nextjs-supabase
4
- > **Layer:** 2 (on keyword)
5
- > **Keywords:** supabase, pgvector, embedding, similarity, vector search
6
- > **Load When:** supabase pgvector keywords detected
7
-
8
- Stack: Next.js 15 + Supabase + .NET Backend
9
-
10
- ## Core Rules
11
-
12
- - ALWAYS use HNSW indexes for production (faster queries, no training required)
13
- - ALWAYS match dimensions to embedding model (e.g., 1536 for text-embedding-3-small)
14
- - NEVER store embeddings without an index -- full table scan at query time
15
- - Use `halfvec` for large datasets to halve storage (16-bit vs 32-bit per dimension)
16
- - ALWAYS use RLS on tables containing embeddings
17
-
18
- ## Setup and Table Design
19
-
20
- ```sql
21
- CREATE EXTENSION IF NOT EXISTS vector;
22
-
23
- CREATE TABLE documents (
24
- id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
25
- user_id UUID NOT NULL REFERENCES auth.users(id),
26
- title TEXT NOT NULL,
27
- content TEXT NOT NULL,
28
- metadata JSONB DEFAULT '{}',
29
- embedding vector(1536),
30
- created_at TIMESTAMPTZ DEFAULT now()
31
- );
32
-
33
- ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
34
- CREATE POLICY "owner_access" ON documents FOR ALL
35
- USING (user_id = auth.uid()) WITH CHECK (user_id = auth.uid());
36
- CREATE INDEX idx_documents_user_id ON documents (user_id);
37
- ```
38
-
39
- ### halfvec Optimization
40
-
41
- | Type | Storage/dim | 1536-dim | Best for |
42
- |------|------------|----------|----------|
43
- | `vector` | 4 bytes | 6 KB | High precision, small datasets |
44
- | `halfvec` | 2 bytes | 3 KB | Large datasets, cost optimization |
45
-
46
- ## Index Types
47
-
48
- ```sql
49
- -- HNSW (recommended)
50
- CREATE INDEX idx_docs_embedding ON documents
51
- USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 64);
52
-
53
- -- IVFFlat (legacy, requires existing data)
54
- CREATE INDEX idx_docs_ivf ON documents
55
- USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);
56
- ```
57
-
58
- | Feature | HNSW | IVFFlat |
59
- |---------|------|---------|
60
- | Query speed | Faster | Slower |
61
- | Requires training | No | Yes |
62
- | Recall quality | Higher | Lower |
63
- | Recommended | Yes | Only for very large datasets |
64
-
65
- ## HNSW Parameters
66
-
67
- | Parameter | Default | Tuning |
68
- |-----------|---------|--------|
69
- | `m` | 16 | Higher = better recall, more memory |
70
- | `ef_construction` | 64 | Higher = better index, slower build |
71
- | `ef_search` | 40 | `SET hnsw.ef_search = 100;` per session |
72
-
73
- ## Distance Functions
74
-
75
- | Operator | Function | Index Ops | Use Case |
76
- |----------|----------|-----------|----------|
77
- | `<=>` | Cosine distance | `vector_cosine_ops` | Normalized embeddings (most common) |
78
- | `<->` | L2 (Euclidean) | `vector_l2_ops` | Spatial/positional data |
79
- | `<#>` | Inner product (neg) | `vector_ip_ops` | Pre-normalized, max similarity |
80
-
81
- ## Similarity Search
82
-
83
- ```sql
84
- CREATE OR REPLACE FUNCTION match_documents(
85
- query_embedding vector(1536),
86
- match_threshold float DEFAULT 0.78,
87
- match_count int DEFAULT 10,
88
- p_user_id uuid DEFAULT auth.uid()
89
- ) RETURNS TABLE (id uuid, title text, content text, similarity float)
90
- LANGUAGE sql STABLE AS $$
91
- SELECT d.id, d.title, d.content,
92
- 1 - (d.embedding <=> query_embedding) AS similarity
93
- FROM documents d
94
- WHERE d.user_id = p_user_id
95
- AND 1 - (d.embedding <=> query_embedding) > match_threshold
96
- ORDER BY d.embedding <=> query_embedding
97
- LIMIT match_count;
98
- $$;
99
- ```
100
-
101
- ## Hybrid Search (Vector + Full-Text)
102
-
103
- ```sql
104
- CREATE OR REPLACE FUNCTION hybrid_search(
105
- query_text text, query_embedding vector(1536),
106
- match_count int DEFAULT 10,
107
- text_weight float DEFAULT 0.3, vector_weight float DEFAULT 0.7
108
- ) RETURNS TABLE (id uuid, title text, content text, score float)
109
- LANGUAGE sql STABLE AS $$
110
- WITH vector_results AS (
111
- SELECT id, title, content,
112
- 1 - (embedding <=> query_embedding) AS vector_score
113
- FROM documents WHERE user_id = auth.uid()
114
- ORDER BY embedding <=> query_embedding LIMIT match_count * 2
115
- ),
116
- text_results AS (
117
- SELECT id, title, content,
118
- ts_rank(to_tsvector('english', content), plainto_tsquery('english', query_text)) AS text_score
119
- FROM documents WHERE user_id = auth.uid()
120
- AND to_tsvector('english', content) @@ plainto_tsquery('english', query_text)
121
- LIMIT match_count * 2
122
- )
123
- SELECT COALESCE(v.id, t.id), COALESCE(v.title, t.title), COALESCE(v.content, t.content),
124
- (COALESCE(v.vector_score, 0) * vector_weight + COALESCE(t.text_score, 0) * text_weight)
125
- FROM vector_results v FULL OUTER JOIN text_results t ON v.id = t.id
126
- ORDER BY score DESC LIMIT match_count;
127
- $$;
128
- ```
129
-
130
- ## .NET Integration (Npgsql)
131
-
132
- ```csharp
133
- public sealed class DocumentRepository(AppDbContext db)
134
- {
135
- public async Task StoreEmbeddingAsync(
136
- Guid documentId, float[] embedding, CancellationToken ct = default)
137
- {
138
- await db.Database.ExecuteSqlInterpolatedAsync(
139
- $"UPDATE documents SET embedding = {new Vector(embedding)} WHERE id = {documentId}", ct);
140
- }
141
-
142
- public async Task<List<DocumentMatch>> SearchSimilarAsync(
143
- float[] queryEmbedding, int limit = 10, float threshold = 0.78f,
144
- CancellationToken ct = default)
145
- {
146
- return await db.Database.SqlQuery<DocumentMatch>($"""
147
- SELECT id, title, content,
148
- 1 - (embedding <=> {new Vector(queryEmbedding)}::vector) AS similarity
149
- FROM documents
150
- WHERE 1 - (embedding <=> {new Vector(queryEmbedding)}::vector) > {threshold}
151
- ORDER BY embedding <=> {new Vector(queryEmbedding)}::vector LIMIT {limit}
152
- """).ToListAsync(ct);
153
- }
154
- }
155
-
156
- // EF Core registration
157
- builder.Services.AddDbContext<AppDbContext>(o =>
158
- o.UseNpgsql(connectionString, npg => npg.UseVector()));
159
- ```
160
-
161
- ## Common Mistakes
162
-
163
- | Wrong | Right | Why |
164
- |-------|-------|-----|
165
- | No index on embedding column | HNSW index | Full table scan, extremely slow |
166
- | `ORDER BY similarity DESC` | `ORDER BY embedding <=> query ASC` | Operator returns distance, not similarity |
167
- | Mixing embedding dimensions | Consistent dimensions per column | Dimension mismatch causes runtime errors |
168
- | Full-precision for millions of rows | `halfvec` for large datasets | 2x storage savings, minimal quality loss |
169
- | Missing RLS on embedding tables | RLS with user/tenant policies | Embeddings contain sensitive content context |