Skip to content

feat: AI search and chat - #379

Open
anareyna wants to merge 17 commits into
mainfrom
feat-ai-search
Open

anareyna wants to merge 17 commits into
mainfrom
feat-ai-search

Conversation

@anareyna

@anareyna anareyna commented May 19, 2026

Copy link
Copy Markdown
Contributor

Adds the indexer and query API for AI search and chat. UI in the next PR.

What's added

  • scripts/index-ai-search.ts (run via npm run index:ai-search) walks the sitemap, fetches each page as markdown from the existing /api/content/*.md endpoint, uploads to Cloudflare AI Search.

  • .github/workflows/index-ai-search.yml runs the indexer automatically after every successful production deploy. No manual work once secrets are set.

  • POST /api/ai-search proxies the question to Cloudflare AI Search and hides the API token. Streams the answer back by default. Pass ?format=json for a single { answer, sources } object that's easy to read in curl.

  • KV hash cache so re-runs skip pages that haven't changed. Optional. Leave the KV namespace ID empty to fall back to "re-upload everything."

    KV namespace AI_SEARCH_HASHES showing one entry per indexed page, with the page path as the key and a small JSON value containing the hash and item id
  • Docs: new "Enable AI Search" section in docs/getting-started.md.

How to test

1. For easy testing use the Cloudflare Playground. No setup. Dashboard → AI → AI Search → head-start-site → Playground → Chat tab. Type a question, read the answer. Best way to check if the AI's answers actually make sense.

The Playground has two tabs. Use Chat to test what our endpoint does (question in, AI answer out, plus the sources it used). The Search tab returns raw matching content with no AI answer. Useful for debugging retrieval, not what we ship.

head-start-site AI Search instance in the Cloudflare dashboard, showing the indexed items list with titles and metadata Screenshot 2026-05-20 at 14 49 29

2. Curl the JSON mode against a preview deploy (once env vars are added, see below):

curl -s -X POST 'https://<preview>.head-start.pages.dev/api/ai-search?format=json' \
  -H 'Content-Type: application/json' \
  -d '{"query": "How do I add a new locale?"}'

Returns:

{
  "answer": "To add a new locale, ...",
  "sources": [
    { "url": "...", "title": "Internationalization (i18n)" }
  ]
}

The default response (no ?format=json) is a streaming SSE response. That's what the UI will consume in a next PR. Hard to read by eye, so use JSON mode for manual testing.

3. Run it locally. Setup in docs/getting-started.md. Reuse the head-start-site instance and AI_SEARCH_HASHES KV namespace already on the Voorhoede CF account (don't create duplicates).

What still needs to happen (not code)

  • GitHub repo secrets for the auto-indexer workflow. Four values listed in the docs.
  • Cloudflare Pages env vars (prod + preview) for the live proxy. Three values listed in the docs.

Until these are done, the auto-indexer and the live preview proxy won't work. Local + Playground work today.

What's NOT in this PR (by design)

  • No UI. Next PR.
  • No Vercel AI SDK abstraction. Will revisit when the UI exists.
  • No multi-turn chat. Same backend can power it later.

Decision log

docs/decision-log/2026-05-20-ai-search-prototype.md covers the why.

Checklist

  • Self-reviewed
  • Easy to review (small, commented)
  • Docs updated (docs/getting-started.md)
  • Decision log entry added
  • Notified a reviewer

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented May 19, 2026

Copy link
Copy Markdown

Deploying head-start with  Cloudflare Pages  Cloudflare Pages

Latest commit: ebb690d
Status: ✅  Deploy successful!
Preview URL: https://1232ca60.head-start.pages.dev
Branch Preview URL: https://feat-ai-search.head-start.pages.dev

View logs

@anareyna anareyna changed the title Feat ai search feat: ai search prototype with post-deploy indexing May 19, 2026
@anareyna
anareyna marked this pull request as draft May 19, 2026 10:16
@anareyna anareyna changed the title feat: ai search prototype with post-deploy indexing feat: ai search prototype: indexer + query proxy May 21, 2026
@anareyna
anareyna marked this pull request as ready for review May 21, 2026 08:25
@anareyna anareyna mentioned this pull request May 26, 2026
3 tasks
@anareyna anareyna self-assigned this Jun 4, 2026
@JopMolenaar
JopMolenaar requested a review from Copilot June 26, 2026 12:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an AI Search prototype to Head Start by introducing (1) a post-deploy indexer that uploads site markdown into Cloudflare AI Search (with optional KV hash caching) and (2) a server-side /api/ai-search proxy to query AI Search without exposing the API token.

Changes:

  • Add /api/ai-search POST proxy with SSE streaming by default and optional ?format=json response reshaping.
  • Add scripts/index-ai-search.ts + GitHub Actions workflow to crawl the sitemap and (re)index pages in Cloudflare AI Search, optionally skipping unchanged pages via KV.
  • Add parseFrontmatter() (with tests) to extract metadata from the markdown frontmatter for AI Search item metadata.

Reviewed changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/pages/api/ai-search.ts Adds the AI Search query proxy endpoint (streaming + JSON mode).
src/lib/frontmatter.ts Adds parseFrontmatter() to read back fields emitted by buildFrontmatter().
src/lib/frontmatter.test.ts Adds unit tests covering parseFrontmatter() round-trips/escaping.
scripts/index-ai-search.ts Adds the sitemap crawler + uploader indexer with optional KV hash cache and pruning.
package.json Adds index:ai-search script entry.
env.d.ts Adds typings for new AI Search-related env vars.
docs/getting-started.md Documents setup steps for enabling AI Search and the indexer workflow.
docs/decision-log/2026-05-20-ai-search-prototype.md Records the rationale/architecture for the prototype.
astro.config.ts Declares new server-side secret env schema for the runtime proxy.
.github/workflows/node-ci.yml Adds empty placeholders for new env vars in CI jobs.
.github/workflows/index-ai-search.yml Adds post-deploy indexing workflow triggered by deployment_status.
.env.example Documents new optional env vars for AI Search.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/pages/api/ai-search.ts Outdated
Comment thread scripts/index-ai-search.ts Outdated
Comment thread scripts/index-ai-search.ts
Comment thread .env.example
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 8, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
head-start-worker ebb690d Jul 22 2026, 07:50 AM

@JopMolenaar
JopMolenaar self-requested a review July 10, 2026 07:50
JopMolenaar added a commit that referenced this pull request Jul 10, 2026
Depends on #379

Adds the "Ask AI" page so people can actually use the AI search we wired
up in the previous PR. Type a question, get an answer with sources.

<img width="1471" height="991" alt="Screenshot 2026-05-27 at 09 21 26"
src="https://github.com/user-attachments/assets/342f03f2-d239-4f43-8fae-7fdcbb40f6ca"
/>


## What's added

- A new **Ask AI** page at `/[locale]/ask/`. Question box at the top,
answer streams in below as it's written, sources show up alongside it.
- An **Ask AI** link in the main nav, right next to Search. Same link in
the mobile nav.
- The answer renders as proper markdown while it's still streaming, so
code blocks and lists look right from the first chunk, not just when it
finishes.
- You can share a question via URL: `/en/ask/?query=...` runs the search
on page load, and submitting the form keeps the URL in sync. Good for
linking people to "here's the answer I got".

## How to test

Check cloudfare preview link
https://feat-ai-search-component.head-start.pages.dev/en/ask or

1. Run the app locally (env vars from the [previous
PR](ttps://github.com//pull/379) need to be set).
2. Visit `/en/ask/`. Ask something the site would know about. You should
see "Thinking…", then the answer appear word by word, with the sources
it used.
3. Try a deep link like
`/en/ask/?query=How%20do%20I%20add%20a%20new%20locale%3F`. It should run
on its own.
4. Check the item is in header nav: "Ask AI" 



## Checklist

- [x] Self-reviewed
- [x] Easy to review (one component, one page, one nav link)
- [ ] Notified a reviewer

---------

Co-authored-by: Jop Molenaar <jopmolenaar@icloud.com>
@JopMolenaar
JopMolenaar requested a review from Copilot July 10, 2026 08:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 31 out of 33 changed files in this pull request and generated 5 comments.

Comment thread docs/getting-started.md
Comment thread docs/getting-started.md
Comment thread docs/getting-started.md
Comment thread src/pages/api/ai-search.ts Outdated
Comment thread src/pages/api/ai-chat.ts Outdated
@JopMolenaar JopMolenaar changed the title feat: ai search prototype: indexer + query proxy feat: AI search and chat Jul 10, 2026
JopMolenaar and others added 7 commits July 22, 2026 09:48
Depends on #379

Adds the "Ask AI" page so people can actually use the AI search we wired
up in the previous PR. Type a question, get an answer with sources.

<img width="1471" height="991" alt="Screenshot 2026-05-27 at 09 21 26"
src="https://github.com/user-attachments/assets/342f03f2-d239-4f43-8fae-7fdcbb40f6ca"
/>

- A new **Ask AI** page at `/[locale]/ask/`. Question box at the top,
answer streams in below as it's written, sources show up alongside it.
- An **Ask AI** link in the main nav, right next to Search. Same link in
the mobile nav.
- The answer renders as proper markdown while it's still streaming, so
code blocks and lists look right from the first chunk, not just when it
finishes.
- You can share a question via URL: `/en/ask/?query=...` runs the search
on page load, and submitting the form keeps the URL in sync. Good for
linking people to "here's the answer I got".

Check cloudfare preview link
https://feat-ai-search-component.head-start.pages.dev/en/ask or

1. Run the app locally (env vars from the [previous
PR](ttps://github.com//pull/379) need to be set).
2. Visit `/en/ask/`. Ask something the site would know about. You should
see "Thinking…", then the answer appear word by word, with the sources
it used.
3. Try a deep link like
`/en/ask/?query=How%20do%20I%20add%20a%20new%20locale%3F`. It should run
on its own.
4. Check the item is in header nav: "Ask AI"

- [x] Self-reviewed
- [x] Easy to review (one component, one page, one nav link)
- [ ] Notified a reviewer

---------

Co-authored-by: Jop Molenaar <jopmolenaar@icloud.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants