Skip to content

Commit 1d897ca

Browse files
committed
Merge branch 'main' into feat/pg_dump-in-the-browser
2 parents 3335dfc + 5b23ed1 commit 1d897ca

File tree

164 files changed

+5264
-3157
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+5264
-3157
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ How is this possible? [PGlite](https://pglite.dev/), a WASM version of Postgres
2323

2424
This is a monorepo split into the following projects:
2525

26-
- [Web](./apps/postgres-new/): The primary web app built with Next.js
26+
- [Web](./apps/web/): The primary web app built with Next.js
2727
- [Browser proxy](./apps/browser-proxy/): Proxies Postgres TCP connections back to the browser using [pg-gateway](https://github.com/supabase-community/pg-gateway) and Web Sockets
2828
- [Deploy worker](./apps/deploy-worker/): Deploys in-browser databases to database platforms (currently Supabase is supported)
2929

@@ -41,33 +41,33 @@ From the monorepo root:
4141
```shell
4242
npx supabase start
4343
```
44-
3. Store local Supabase URL/anon key in `./apps/postgres-new/.env.local`:
44+
3. Store local Supabase URL/anon key in `./apps/web/.env.local`:
4545
```shell
4646
npx supabase status -o env \
4747
--override-name api.url=NEXT_PUBLIC_SUPABASE_URL \
4848
--override-name auth.anon_key=NEXT_PUBLIC_SUPABASE_ANON_KEY |
49-
grep NEXT_PUBLIC >> ./apps/postgres-new/.env.local
49+
grep NEXT_PUBLIC >> ./apps/web/.env.local
5050
```
51-
4. Create an [OpenAI API key](https://platform.openai.com/api-keys) and save to `./apps/postgres-new/.env.local`:
51+
4. Create an [OpenAI API key](https://platform.openai.com/api-keys) and save to `./apps/web/.env.local`:
5252
```shell
53-
echo 'OPENAI_API_KEY="<openai-api-key>"' >> ./apps/postgres-new/.env.local
53+
echo 'OPENAI_API_KEY="<openai-api-key>"' >> ./apps/web/.env.local
5454
```
5555
5. Store local KV (Redis) vars. Use these exact values:
5656

5757
```shell
58-
echo 'KV_REST_API_URL="http://localhost:8080"' >> ./apps/postgres-new/.env.local
59-
echo 'KV_REST_API_TOKEN="local_token"' >> ./apps/postgres-new/.env.local
58+
echo 'KV_REST_API_URL="http://localhost:8080"' >> ./apps/web/.env.local
59+
echo 'KV_REST_API_TOKEN="local_token"' >> ./apps/web/.env.local
6060
```
6161

6262
6. Start local Redis containers (used for rate limiting). Serves an API on port 8080:
6363

6464
```shell
65-
docker compose -f ./apps/postgres-new/docker-compose.yml up -d
65+
docker compose -f ./apps/web/docker-compose.yml up -d
6666
```
6767

6868
7. Fill in the remaining variables for each app as seen in:
6969

70-
- `./apps/postgres-new/.env.example`
70+
- `./apps/web/.env.example`
7171
- `./apps/browser-proxy/.env.example`
7272
- `./apps/deploy-worker/.env.example`
7373

File renamed without changes.

apps/web/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public/sw.mjs

apps/postgres-new/README.md renamed to apps/web/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# postgres-new
1+
# @database.build/web
22

33
In-browser Postgres sandbox with AI assistance. Built on Next.js.
44

File renamed without changes.
File renamed without changes.
File renamed without changes.

apps/postgres-new/app/api/chat/route.ts renamed to apps/web/app/api/chat/route.ts

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { createOpenAI } from '@ai-sdk/openai'
22
import { Ratelimit } from '@upstash/ratelimit'
33
import { kv } from '@vercel/kv'
44
import { convertToCoreMessages, streamText, ToolInvocation, ToolResultPart } from 'ai'
5-
import { codeBlock } from 'common-tags'
6-
import { convertToCoreTools, maxMessageContext, maxRowLimit, tools } from '~/lib/tools'
5+
import { getSystemPrompt } from '~/lib/system-prompt'
6+
import { convertToCoreTools, maxMessageContext, tools } from '~/lib/tools'
77
import { createClient } from '~/utils/supabase/server'
88
import { ChatInferenceEventToolResult, logEvent } from '~/utils/telemetry'
99

@@ -72,49 +72,8 @@ export async function POST(req: Request) {
7272
const coreMessages = convertToCoreMessages(trimmedMessageContext)
7373
const coreTools = convertToCoreTools(tools)
7474

75-
const result = await streamText({
76-
system: codeBlock`
77-
You are a helpful database assistant. Under the hood you have access to an in-browser Postgres database called PGlite (https://github.com/electric-sql/pglite).
78-
Some special notes about this database:
79-
- foreign data wrappers are not supported
80-
- the following extensions are available:
81-
- plpgsql [pre-enabled]
82-
- vector (https://github.com/pgvector/pgvector) [pre-enabled]
83-
- use <=> for cosine distance (default to this)
84-
- use <#> for negative inner product
85-
- use <-> for L2 distance
86-
- use <+> for L1 distance
87-
- note queried vectors will be truncated/redacted due to their size - export as CSV if the full vector is required
88-
89-
When generating tables, do the following:
90-
- For primary keys, always use "id bigint primary key generated always as identity" (not serial)
91-
- Prefer 'text' over 'varchar'
92-
- Keep explanations brief but helpful
93-
- Don't repeat yourself after creating the table
94-
95-
When creating sample data:
96-
- Make the data realistic, including joined data
97-
- Check for existing records/conflicts in the table
98-
99-
When querying data, limit to 5 by default. The maximum number of rows you're allowed to fetch is ${maxRowLimit} (to protect AI from token abuse).
100-
If the user needs to fetch more than ${maxRowLimit} rows at once, they can export the query as a CSV.
101-
102-
When performing FTS, always use 'simple' (languages aren't available).
103-
104-
When importing CSVs try to solve the problem yourself (eg. use a generic text column, then refine)
105-
vs. asking the user to change the CSV. No need to select rows after importing.
106-
107-
You also know math. All math equations and expressions must be written in KaTex and must be wrapped in double dollar \`$$\`:
108-
- Inline: $$\\sqrt{26}$$
109-
- Multiline:
110-
$$
111-
\\sqrt{26}
112-
$$
113-
114-
No images are allowed. Do not try to generate or link images, including base64 data URLs.
115-
116-
Feel free to suggest corrections for suspected typos.
117-
`,
75+
const result = streamText({
76+
system: getSystemPrompt(),
11877
model: openai(chatModel),
11978
messages: coreMessages,
12079
tools: coreTools,
@@ -158,7 +117,7 @@ export async function POST(req: Request) {
158117
},
159118
})
160119

161-
return result.toAIStreamResponse()
120+
return result.toDataStreamResponse()
162121
}
163122

164123
function getEventToolResult(toolResult: ToolResultPart): ChatInferenceEventToolResult | undefined {
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)