Search
Code3,018
import { readFile } from "https://esm.town/v/std/utils/index.ts";import { createAnthropic } from "npm:@ai-sdk/anthropic@1.2.12";import { convertToCoreMessages, } = await c.req.json(); const apiKey = Deno.env.get("ANTHROPIC_API_KEY"); if (await hasInsufficientCredits({ bearerToken })) { }); const anthropic = createAnthropic({ apiKey }); let tracedModel = anthropic(model); if (Deno.env.get("POSTHOG_PROJECT_API_KEY")) { const traceId = `townie_${rowid}_${Date.now()}`; // Wrap the Anthropic model with PostHog tracing tracedModel = withTracing(anthropic(model), phClient, { posthogDistinctId: user.id, posthogTraceId: traceId, // @ts-ignore lastMessage.content.at(-1).providerOptions = { anthropic: { cacheControl: { type: "ephemeral" } }, }; } output_tokens: result.usage.completionTokens, cache_read_tokens: result.providerMetadata.anthropic.cacheReadInputTokens, cache_write_tokens: result.providerMetadata.anthropic.cacheCreationInputTokens, }); output_tokens: result.usage.completionTokens, cache_read_tokens: result.providerMetadata.anthropic.cacheReadInputTokens, cache_write_tokens: result.providerMetadata.anthropic.cacheCreationInputTokens, }); },Townie is fully open-source and itself runs on Val Town. Pull requests welcome!To get Townie running in your Val Town account, click the **Remix** button and then add your ANTHROPIC_API_KEY. You can leave all the other environment variables blank.Authentication in Townie is handled via Val Town Oauth. However, we have not yet opened up our OAuth to anyone else, which currently makes it very awkward to use your own Townie. Here is a temporary workaround: </ul> <p> The application proxies requests to the Anthropic API and Val Town API, allowing Claude to view and edit your project files directly. </p> - name: Run Claude Code Review id: claude-review uses: anthropics/claude-code-action@v1 with: claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }} Use `gh pr comment` with your Bash tool to leave your review as a comment on the PR. # See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md # or https://docs.claude.com/en/docs/claude-code/sdk#command-line for available options claude_args: '--allowed-tools "Bash(gh issue view:*),Bash(gh search:*),Bash(gh issue list:*),Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*),Bash(gh pr list:*)"' - name: Run Claude Code id: claude uses: anthropics/claude-code-action@v1 with: claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }} # Optional: Add claude_args to customize behavior and configuration # See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md # or https://docs.claude.com/en/docs/claude-code/sdk#command-line for available options # claude_args: '--model claude-opus-4-1-20250805 --allowed-tools Bash(gh pr:*)'import Anthropic from "npm:@anthropic-ai/sdk";import type { GenericMessageEvent } from "npm:@slack/web-api";import { TOOLS_LIST, TOOLS_OBJECT } from "../tools/index.ts";const MAX_STEPS = 20;const anthropic = new Anthropic({ apiKey: Deno.env.get("CLAUDE_API_KEY") });const TOOL_RESULTS = {};export async function reply( event: GenericMessageEvent, messages: Anthropic.MessageParam[],) { // reply in thread or start one const thread_ts = event.thread_ts || event.ts; const input: Anthropic.MessageParam[] = [...messages]; let conversationLogLinkSent = messages.some((message) => { for (let step = 0; step < MAX_STEPS; step++) { const response = await anthropic.messages.create({ model: "claude-sonnet-4-20250514", system: await getFullSystemPrompt(event), tools: TOOLS_LIST, // betas: ["context-1m-2025-08-07"], // Requires usage tier 4: https://docs.anthropic.com/en/docs/build-with-claude/context-windows#1m-token-context-window });import { generateText, tool } from "npm:ai";import { anthropic } from "npm:@ai-sdk/anthropic";import { z } from "npm:zod";import { type Video } from "./db.ts";export async function roast(video: Video) { const response = await generateText({ model: anthropic("claude-4-sonnet-20250514"), tools: { getThumbnail: getThumbnail(video) }, messages: [We charge a 50% markup on top of raw LLM costs. If you use $10 in Towniecredits, Anthropic will get $6.66 and we'll get $3.33. We think this is fair,sustainable, and transparent. We don't want to be in the business of havingmurky limits, obfuscated credits, or unsustainable margins.
import { Bot } from "https://deno.land/x/grammy@v1.35.0/mod.ts";import { DateTime } from "https://esm.sh/luxon@3.4.4";import Anthropic from "npm:@anthropic-ai/sdk@0.24.3";import { backstory } from "../backstory.ts";import {} from "../memoryUtils.ts";async function generateBriefingContent(anthropic, memories, today, isSunday) { try { const weekdaysHelp = generateWeekDays(today); }; console.log("Sending prompt to Anthropic...", userMessage); const response = await anthropic.messages.create({ model: "claude-3-7-sonnet-latest", max_tokens: 30000,export async function sendDailyBriefing(chatId?: string, today?: DateTime) { // Get API keys from environment const apiKey = Deno.env.get("ANTHROPIC_API_KEY"); const telegramToken = Deno.env.get("TELEGRAM_TOKEN"); if (!apiKey) { console.error("Anthropic API key is not configured."); return; } } // Initialize Anthropic client const anthropic = new Anthropic({ apiKey }); // Initialize Telegram bot // Generate briefing content const content = await generateBriefingContent( anthropic, memories, today,You'll need to set up some environment variables to make it run.- `ANTHROPIC_API_KEY` for LLM calls- You'll need to follow [these instructions](https://docs.val.town/integrations/telegram/) to make a telegram bot, and set `TELEGRAM_TOKEN`. You'll also need to get a `TELEGRAM_CHAT_ID` in order to have the bot remember chat contents.- For the Google Calendar integration you'll need `GOOGLE_CALENDAR_ACCOUNT_ID` and `GOOGLE_CALENDAR_CALENDAR_ID`. See [these instuctions](https://www.val.town/v/stevekrouse/pipedream) for details.