Skip to content

Migrate Worker to Cloudflare-native AI; fix truncated replies; auto-close threads on send#30

Draft
Copilot wants to merge 11 commits into
mainfrom
copilot/evaluate-cloudflare-agents
Draft

Migrate Worker to Cloudflare-native AI; fix truncated replies; auto-close threads on send#30
Copilot wants to merge 11 commits into
mainfrom
copilot/evaluate-cloudflare-agents

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 1, 2026

The AI pipeline was truncating long multi-part answers (1 024-token ceiling), using Gemini for both agents (external API dependency + cost), and leaving threads open after autopilot auto-sent a reply.

Truncation fix

  • Research Agent: maxOutputTokens 1 024 → 2 048
  • Writing Agent: maxOutputTokens 1 024 → MAX_WRITING_TOKENS (4 096)

Writing Agent → @cf/zai-org/glm-4.7-flash

Replaced geminiGenerateContent call with cfAiGenerateText() via the Workers AI AI binding. Added [ai] binding = "AI" to wrangler.toml. No external API key needed.

Research Agent → Workers AI + HTMLRewriter

Gemini's proprietary urlContext tool replaced with manual URL fetching fed into the same CF model. Up to 5 deduplicated datasource URLs are fetched in parallel (5 s timeout, 3 000 chars/URL cap).

Script/style blocks are stripped with HTMLRewriter (Cloudflare's built-in streaming HTML parser) before a catch-all tag regex runs — avoids the js/incomplete-multi-character-sanitization CodeQL alerts that regex-only stripping triggers:

const withoutCode = new HTMLRewriter()
  .on('script', { element(el) { el.remove(); } })
  .on('style',  { element(el) { el.remove(); } })
  .transform(response);
const html = await withoutCode.text();

Confidence is URL_FETCH_SUCCESS_CONFIDENCE (0.7) when at least one URL returned content — same value as the previous Gemini URL-context fallback. Citations become the list of successfully fetched URLs.

Removed entirely

geminiGenerateContent, GEMINI_API_BASE, computeConfidence, extractCitations, GEMINI_API_KEY from Env, MAX_GEMINI_URLS, deduplicateForGemini. No external AI service or API keys required by the Worker.

Thread auto-close on send

After autopilot sends a reply, the thread is PATCHed to status: 'closed'. This is fire-and-forget (.catch() logs the error) so a Supabase hiccup at this step cannot retroactively revert a reply that was already delivered to the customer. Threads are already re-opened when a customer replies again, so the lifecycle is complete.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 1, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
answerify Ready Ready Preview, Comment Mar 1, 2026 0:34am

Co-authored-by: harshithpabbati <43822585+harshithpabbati@users.noreply.github.com>
Co-authored-by: harshithpabbati <43822585+harshithpabbati@users.noreply.github.com>
…ler, sub-address reply routing

Co-authored-by: harshithpabbati <43822585+harshithpabbati@users.noreply.github.com>
…tags

Co-authored-by: harshithpabbati <43822585+harshithpabbati@users.noreply.github.com>
Copilot AI changed the title [WIP] Assess potential benefits of Cloudflare agents Add Cloudflare Agents Worker with native Email Routing integration Mar 1, 2026
Co-authored-by: harshithpabbati <43822585+harshithpabbati@users.noreply.github.com>
Copilot AI changed the title Add Cloudflare Agents Worker with native Email Routing integration Use Cloudflare native email.reply() for autopilot sends Mar 1, 2026
…cf/zai-org/glm-4.7-flash

Co-authored-by: harshithpabbati <43822585+harshithpabbati@users.noreply.github.com>
…URL fetching

Co-authored-by: harshithpabbati <43822585+harshithpabbati@users.noreply.github.com>
Copilot AI changed the title Use Cloudflare native email.reply() for autopilot sends Replace Gemini + Resend with Cloudflare-native primitives throughout the Worker Mar 1, 2026
Co-authored-by: harshithpabbati <43822585+harshithpabbati@users.noreply.github.com>
Copilot AI changed the title Replace Gemini + Resend with Cloudflare-native primitives throughout the Worker Migrate Worker to Cloudflare-native AI; fix truncated replies; auto-close threads on send Mar 1, 2026
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.

2 participants