Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions migrations/0002_ai_generate.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- POST /api/generate (worker/generate.ts, worker/ai-budget.ts): the AI pipeline builder's
-- daily spend, per-IP daily counts and answer cache. Pruned daily (worker/index.ts, scheduled):
-- ip rows after 2 days, spend rows after 92 days, cached answers after 30 days.

-- One row per UTC day. reserved_micro is in micro-dollars: each call reserves its worst case
-- here before it runs and is corrected to its actual cost afterwards.
CREATE TABLE ai_spend (
day TEXT PRIMARY KEY,
reserved_micro INTEGER NOT NULL DEFAULT 0,
calls INTEGER NOT NULL DEFAULT 0
);

-- Model calls per client per UTC day. ip_hash is an HMAC (secret AI_IP_SALT) of the day and the
-- client's address bucket, so the address is never stored and hashes do not link across days.
CREATE TABLE ai_ip_daily (
day TEXT NOT NULL,
ip_hash TEXT NOT NULL,
count INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (day, ip_hash)
);

-- Generated steps by a SHA-256 of the normalised description, model id and catalog version;
-- the description itself is not stored. created_at is Unix milliseconds.
CREATE TABLE ai_answer_cache (
key TEXT PRIMARY KEY,
steps TEXT NOT NULL,
notes TEXT NOT NULL DEFAULT '',
created_at INTEGER NOT NULL
);

CREATE INDEX ai_answer_cache_created ON ai_answer_cache (created_at);
73 changes: 73 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"release": "vite-node scripts/release.ts"
},
"dependencies": {
"@anthropic-ai/sdk": "^1.14.0",
"@codemirror/lang-html": "^6.4.12",
"@codemirror/lang-javascript": "^6.2.5",
"@codemirror/lang-json": "^6.0.2",
Expand Down
3 changes: 2 additions & 1 deletion src/app/commands/commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import {
commands, commandById, listShortcuts, TOOL_COMMAND_EVENT, EVENT_MAGIC, EVENT_OPEN_SHARE,
EVENT_OPEN_LIBRARY, EVENT_OPEN_PRESETS, EVENT_TOGGLE_THEME, EVENT_OPEN_SHORTCUTS,
EVENT_OPEN_LIBRARY, EVENT_OPEN_PRESETS, EVENT_OPEN_GENERATE, EVENT_TOGGLE_THEME, EVENT_OPEN_SHORTCUTS,
attachToolBridge, hasToolBridge, queueToolCommand, type ToolCommandDetail,
} from './commands'

Expand Down Expand Up @@ -63,6 +63,7 @@ describe('commands', () => {
['share', EVENT_OPEN_SHARE],
['library', EVENT_OPEN_LIBRARY],
['presets', EVENT_OPEN_PRESETS],
['generate', EVENT_OPEN_GENERATE],
]

it.each(TOOL_DIALOGS)('%s fires the %s window event straight away while an editor is on screen', (id, eventName) => {
Expand Down
3 changes: 3 additions & 0 deletions src/app/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const EVENT_MAGIC = 'sub:magic'
export const EVENT_OPEN_SHARE = 'sub:open-share'
export const EVENT_OPEN_LIBRARY = 'sub:open-library'
export const EVENT_OPEN_PRESETS = 'sub:open-presets'
/** Opens the describe dialog (the AI pipeline builder, `src/app/generate/`). */
export const EVENT_OPEN_GENERATE = 'sub:open-generate'
export const EVENT_TOGGLE_THEME = 'sub:toggle-theme'
export const EVENT_OPEN_SHORTCUTS = 'sub:open-shortcuts'
export const EVENT_OPEN_PALETTE = 'sub:open-palette'
Expand Down Expand Up @@ -142,6 +144,7 @@ export const commands: Command[] = [
{ id: 'share', title: 'Share', group: 'Pipeline', keywords: ['share', 'link', 'url'], run: fireInTool(EVENT_OPEN_SHARE) },
{ id: 'library', title: 'Library', group: 'Pipeline', keywords: ['library', 'save', 'load', 'pipelines'], run: fireInTool(EVENT_OPEN_LIBRARY) },
{ id: 'presets', title: 'Start from a preset', group: 'Pipeline', keywords: ['presets', 'recipes', 'examples', 'templates', 'gallery'], run: fireInTool(EVENT_OPEN_PRESETS) },
{ id: 'generate', title: 'Describe a pipeline (AI)', group: 'Pipeline', keywords: ['ai', 'describe', 'generate', 'build', 'prompt', 'natural language'], run: fireInTool(EVENT_OPEN_GENERATE) },
{ id: 'toggle-theme', title: 'Toggle theme', group: 'View', keywords: ['theme', 'dark', 'light', 'mode'], run: fire(EVENT_TOGGLE_THEME) },
{ id: 'shortcuts', title: 'Keyboard shortcuts', group: 'Help', keywords: ['shortcuts', 'help', 'keys'], run: fire(EVENT_OPEN_SHORTCUTS) },
{ id: 'goto-tool', title: 'Go to Tool', group: 'Navigate', keywords: ['tool', 'home', 'pipeline'], run: goToTool },
Expand Down
51 changes: 51 additions & 0 deletions src/app/generate/GenerateButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React, { Suspense, lazy, useEffect, useRef, useState } from 'react'
import { Sparkles } from 'lucide-react'
import { EVENT_OPEN_GENERATE } from '@/app/commands/commands'
import { useT } from '@/app/i18n/useT'
import Dialog from '@/app/library/Dialog'
import type { GenerateDialogProps } from './GenerateDialog'

/** What the dialog shows when its chunk cannot be fetched (offline, or a stale tab after a deploy). */
function GenerateUnavailable({ onClose, returnFocus }: GenerateDialogProps) {
const { t } = useT()
return (
<Dialog title={t('generate.title')} onClose={onClose} returnFocus={returnFocus}>
<p className="m-0 text-[13px] text-muted text-pretty">{t('generate.error.offline')}</p>
</Dialog>
)
}

// Lazy, so the dialog stays out of the editor's entry chunk until someone opens it.
const GenerateDialog = lazy(() =>
import('./GenerateDialog').catch(() => ({ default: GenerateUnavailable })),
)

/**
* Opens the describe dialog (REQ-027) from beside the step list. Also opens on the
* `sub:open-generate` window event, which the command palette fires.
*/
export default function GenerateButton({ className = 'btn h-[30px] px-2.5' }: { className?: string }) {
const { t } = useT()
const [open, setOpen] = useState(false)
const buttonRef = useRef<HTMLButtonElement>(null)

useEffect(() => {
const onOpen = () => setOpen(true)
window.addEventListener(EVENT_OPEN_GENERATE, onOpen)
return () => window.removeEventListener(EVENT_OPEN_GENERATE, onOpen)
}, [])

return (
<>
<button ref={buttonRef} type="button" className={className} aria-haspopup="dialog"
title={t('generate.buttonTitle')} onClick={() => setOpen(true)}>
<Sparkles size={14} aria-hidden="true" /> {t('generate.button')}
</button>
{open && (
<Suspense fallback={null}>
<GenerateDialog onClose={() => setOpen(false)} returnFocus={buttonRef} />
</Suspense>
)}
</>
)
}
Loading