@somewhere-cloud/cli 0.38.154 → 0.38.155
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/docs/adopt.md +39 -0
- package/dist/docs/analytics.md +161 -0
- package/dist/docs/api-reference.md +339 -0
- package/dist/docs/api-surface.md +2067 -0
- package/dist/docs/app-mcp.md +162 -0
- package/dist/docs/architecture-patterns.md +334 -0
- package/dist/docs/auth-client.md +531 -0
- package/dist/docs/billing.md +76 -0
- package/dist/docs/browser.md +639 -0
- package/dist/docs/calendar.md +298 -0
- package/dist/docs/changes.md +323 -0
- package/dist/docs/cli.md +407 -0
- package/dist/docs/common-mistakes.md +199 -0
- package/dist/docs/connect-agent.md +72 -0
- package/dist/docs/cors.md +114 -0
- package/dist/docs/cron.md +167 -0
- package/dist/docs/database-engine.md +167 -0
- package/dist/docs/declared-data.md +1656 -0
- package/dist/docs/declared-files.md +475 -0
- package/dist/docs/deploy-intelligence.md +188 -0
- package/dist/docs/deploy.md +733 -0
- package/dist/docs/design-system.md +273 -0
- package/dist/docs/dev-environments.md +496 -0
- package/dist/docs/domain-ownership.md +31 -0
- package/dist/docs/domains.md +370 -0
- package/dist/docs/feedback.md +88 -0
- package/dist/docs/file-tools.md +116 -0
- package/dist/docs/functions.md +489 -0
- package/dist/docs/getting-started.md +238 -0
- package/dist/docs/github.md +95 -0
- package/dist/docs/google-calendar.md +212 -0
- package/dist/docs/groups.md +836 -0
- package/dist/docs/guarantees.md +58 -0
- package/dist/docs/handled-for-you.md +224 -0
- package/dist/docs/inspect.md +171 -0
- package/dist/docs/limits.md +68 -0
- package/dist/docs/live-data.md +197 -0
- package/dist/docs/live.md +92 -0
- package/dist/docs/local-dev.md +30 -0
- package/dist/docs/migration-supabase.md +58 -0
- package/dist/docs/payments.md +1102 -0
- package/dist/docs/portability.md +194 -0
- package/dist/docs/postgres.md +301 -0
- package/dist/docs/pricing-comparison.md +42 -0
- package/dist/docs/pricing.md +39 -0
- package/dist/docs/project-groups.md +107 -0
- package/dist/docs/projects.md +380 -0
- package/dist/docs/recipe-ai-agent.md +112 -0
- package/dist/docs/recipe-ai-features.md +227 -0
- package/dist/docs/recipe-ai-receipts.md +783 -0
- package/dist/docs/recipe-background-jobs.md +369 -0
- package/dist/docs/recipe-booking.md +348 -0
- package/dist/docs/recipe-csv-import.md +390 -0
- package/dist/docs/recipe-ecommerce.md +127 -0
- package/dist/docs/recipe-email.md +282 -0
- package/dist/docs/recipe-escalation.md +133 -0
- package/dist/docs/recipe-file-uploads.md +159 -0
- package/dist/docs/recipe-import-app.md +110 -0
- package/dist/docs/recipe-login.md +457 -0
- package/dist/docs/recipe-multi-chat.md +198 -0
- package/dist/docs/recipe-paid-api.md +161 -0
- package/dist/docs/recipe-pdfs.md +205 -0
- package/dist/docs/recipe-push-notifications.md +107 -0
- package/dist/docs/recipe-seo.md +200 -0
- package/dist/docs/recipe-signed-in-app.md +330 -0
- package/dist/docs/render.md +99 -0
- package/dist/docs/rooms.md +554 -0
- package/dist/docs/sdk.md +287 -0
- package/dist/docs/sdks.md +24 -0
- package/dist/docs/search.md +199 -0
- package/dist/docs/security-model.md +168 -0
- package/dist/docs/setup.md +212 -0
- package/dist/docs/smoke.md +102 -0
- package/dist/docs/speed.md +298 -0
- package/dist/docs/sql-compatibility.md +96 -0
- package/dist/docs/sw.agent.md +248 -0
- package/dist/docs/sw.ai.md +1291 -0
- package/dist/docs/sw.auth.md +1811 -0
- package/dist/docs/sw.billing.md +171 -0
- package/dist/docs/sw.db.md +2128 -0
- package/dist/docs/sw.email.md +661 -0
- package/dist/docs/sw.endpoint.md +187 -0
- package/dist/docs/sw.env.md +303 -0
- package/dist/docs/sw.fetch.md +115 -0
- package/dist/docs/sw.fs.md +424 -0
- package/dist/docs/sw.groups.md +836 -0
- package/dist/docs/sw.image.md +69 -0
- package/dist/docs/sw.jobs.md +520 -0
- package/dist/docs/sw.logs.md +61 -0
- package/dist/docs/sw.mail.md +106 -0
- package/dist/docs/sw.notifications.md +78 -0
- package/dist/docs/sw.push.md +161 -0
- package/dist/docs/sw.rateLimit.md +67 -0
- package/dist/docs/sw.web.md +128 -0
- package/dist/docs/tasks.md +269 -0
- package/dist/docs/test.md +220 -0
- package/dist/docs/tool-reference-account.md +7 -0
- package/dist/docs/tool-reference-ai.md +121 -0
- package/dist/docs/tool-reference-analytics.md +37 -0
- package/dist/docs/tool-reference-api.md +19 -0
- package/dist/docs/tool-reference-artifacts.md +35 -0
- package/dist/docs/tool-reference-auth.md +163 -0
- package/dist/docs/tool-reference-browser.md +114 -0
- package/dist/docs/tool-reference-code.md +26 -0
- package/dist/docs/tool-reference-connect.md +21 -0
- package/dist/docs/tool-reference-cron.md +48 -0
- package/dist/docs/tool-reference-db.md +282 -0
- package/dist/docs/tool-reference-domains.md +107 -0
- package/dist/docs/tool-reference-email.md +55 -0
- package/dist/docs/tool-reference-env.md +19 -0
- package/dist/docs/tool-reference-feedback.md +56 -0
- package/dist/docs/tool-reference-fs.md +265 -0
- package/dist/docs/tool-reference-github.md +57 -0
- package/dist/docs/tool-reference-groups.md +85 -0
- package/dist/docs/tool-reference-help.md +29 -0
- package/dist/docs/tool-reference-jobs.md +39 -0
- package/dist/docs/tool-reference-live.md +48 -0
- package/dist/docs/tool-reference-logs.md +16 -0
- package/dist/docs/tool-reference-payments.md +44 -0
- package/dist/docs/tool-reference-project.md +706 -0
- package/dist/docs/tool-reference-push.md +78 -0
- package/dist/docs/tool-reference-rate-limit.md +18 -0
- package/dist/docs/tool-reference-search.md +68 -0
- package/dist/docs/tool-reference-security.md +42 -0
- package/dist/docs/tool-reference-smoke.md +17 -0
- package/dist/docs/tool-reference-stock-photos.md +20 -0
- package/dist/docs/tool-reference-tasks.md +73 -0
- package/dist/docs/tool-reference-telegram.md +32 -0
- package/dist/docs/tool-reference-usage.md +9 -0
- package/dist/docs/tool-reference-web.md +71 -0
- package/dist/docs/tool-reference-webhooks.md +9 -0
- package/dist/docs/troubleshooting.md +411 -0
- package/dist/docs/typed-functions.md +94 -0
- package/dist/docs/verify-before-deploy.md +67 -0
- package/dist/docs/vs-supabase.md +75 -0
- package/dist/docs/vs-vercel.md +103 -0
- package/dist/lib/bundle.js +303 -221
- package/dist/lib/bundle.js.map +3 -3
- package/dist/types/lib/test-session.d.ts +8 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +3 -2
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# Recipe — AI features: suggest a title
|
|
2
|
+
|
|
3
|
+
A signed-in user writes a note and asks for a title. Your function calls a
|
|
4
|
+
model with `sw.ai.chat`, with no provider key, and the page shows the
|
|
5
|
+
suggestion or says plainly that none is available. Then pick the next
|
|
6
|
+
capability from the table at the end.
|
|
7
|
+
|
|
8
|
+
## Choose the shape and the model
|
|
9
|
+
|
|
10
|
+
- **A chatbot, tools or several steps:** start from `recipe-ai-agent`, which
|
|
11
|
+
runs `sw.agent` with conversation history and tools. Don't assemble that
|
|
12
|
+
loop from single calls.
|
|
13
|
+
- **A label, routing choice or score:** use `sw.ai.evaluate` (`sw.ai` →
|
|
14
|
+
`typed-decisions`). Text uses Jev; images use Clef-flash.
|
|
15
|
+
- **One prose answer from one input,** such as a title or a summary: one
|
|
16
|
+
`sw.ai.chat` call, as in this recipe.
|
|
17
|
+
|
|
18
|
+
Choose the model for each call with two questions: is the task easy, and is
|
|
19
|
+
a person waiting? If either answer is yes, use a fast model. If the task is
|
|
20
|
+
hard and nobody is waiting, use a reasoning model and run the call in a job
|
|
21
|
+
(`recipe-background-jobs`).
|
|
22
|
+
|
|
23
|
+
- **Fast:** omit `provider` and `model`. That's the free default. It uses
|
|
24
|
+
your account's included AI allowance and refuses instead of charging when
|
|
25
|
+
the allowance is used up.
|
|
26
|
+
- **Reasoning:** choose a `complete` entry in `ai_catalog` whose `label`
|
|
27
|
+
says reasoning, and pass its `provider` and `model`. That's the paid path:
|
|
28
|
+
it uses eligible included allowance, then prepaid balance, and counts toward
|
|
29
|
+
the project's AI spend cap. Calls using your own provider key are billed by
|
|
30
|
+
that provider and sit outside this cap.
|
|
31
|
+
|
|
32
|
+
`ai_catalog` lists which models are available now, with each one's price and
|
|
33
|
+
notes; the supplier's documentation has the rest. Tell the app's owner your
|
|
34
|
+
choice and its trade-off in one sentence, for example: "Titles use the free
|
|
35
|
+
default model because someone is waiting and the task is easy; when the
|
|
36
|
+
included allowance runs out, suggestions pause instead of charging."
|
|
37
|
+
|
|
38
|
+
## 1. The function
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
// api/suggest-title.ts — POST { text } → { title }
|
|
42
|
+
export default sw.endpoint({
|
|
43
|
+
auth: 'none', // the handler applies its own access check below
|
|
44
|
+
handler: async ({ request }, sw) => {
|
|
45
|
+
if (request.method !== 'POST') {
|
|
46
|
+
return new Response('Use POST.', { status: 405, headers: { Allow: 'POST' } });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const user = await sw.auth.fromRequest(request);
|
|
50
|
+
if (!user) {
|
|
51
|
+
return Response.json({ error: 'Sign in to get a suggestion.' }, { status: 401 });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const input: unknown = await request.json().catch(() => null);
|
|
55
|
+
const text = typeof input === 'object' && input !== null && 'text' in input ? input.text : undefined;
|
|
56
|
+
if (typeof text !== 'string' || !text.trim() || text.length > 4000) {
|
|
57
|
+
return Response.json({ error: 'Send up to 4,000 characters of text.' }, { status: 400 });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 20 per hour per user is this example's choice. It runs before any model call.
|
|
61
|
+
const allowance = await sw.rateLimit.check(`suggest-title:${user.id}`, 20, 3600);
|
|
62
|
+
if (!allowance.allowed) {
|
|
63
|
+
return Response.json({ error: 'Too many suggestions. Try again later.' }, { status: 429 });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const unavailable = (code: string) =>
|
|
67
|
+
Response.json({ error: 'No suggestion right now. Try again later.', code }, { status: 503 });
|
|
68
|
+
try {
|
|
69
|
+
// No provider or model: the free default.
|
|
70
|
+
const reply = await sw.ai.chat({
|
|
71
|
+
system: 'Reply with one short title for the text. No quotes.',
|
|
72
|
+
messages: [{ role: 'user', content: text }],
|
|
73
|
+
});
|
|
74
|
+
const title = reply.text.trim();
|
|
75
|
+
return title ? Response.json({ title }) : unavailable('EMPTY_SUGGESTION');
|
|
76
|
+
} catch (error) {
|
|
77
|
+
const code = error instanceof Error && 'code' in error ? String(error.code) : '';
|
|
78
|
+
const status = error instanceof Error && 'status' in error ? Number(error.status) : 0;
|
|
79
|
+
// A plan or project limit (402, 429) or the model provider (5xx).
|
|
80
|
+
if (status === 402 || status === 429 || status >= 500) {
|
|
81
|
+
try {
|
|
82
|
+
await sw.logs.warn('Title suggestion unavailable', { code, status });
|
|
83
|
+
} catch (logError) {
|
|
84
|
+
console.error('Could not write AI availability log.', logError);
|
|
85
|
+
}
|
|
86
|
+
return unavailable(code);
|
|
87
|
+
}
|
|
88
|
+
throw error; // Other failures reach the platform's normal error handling.
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The checks run in order: sign-in, input, then this user's limit, so a
|
|
95
|
+
signed-out or oversized request never reaches a model.
|
|
96
|
+
|
|
97
|
+
When the model can't answer, the function says so with 503 and keeps the
|
|
98
|
+
platform's `code` in the response for you. It also writes the code and status
|
|
99
|
+
to your project's logs without including the user's text. If logging fails,
|
|
100
|
+
the same unavailable response still reaches the page. The page never shows
|
|
101
|
+
billing details. The free
|
|
102
|
+
default answers 429 for each of its limits: `RATE_LIMITED` for its request
|
|
103
|
+
limit, which is shared across the owner's projects, `AI_ALLOWANCE_EXHAUSTED`
|
|
104
|
+
for the included allowance and `AI_SPEND_CAP_REACHED` for the project's
|
|
105
|
+
monthly cap. A provider failure answers 5xx. On the paid path a balance
|
|
106
|
+
refusal answers 402. When the project reaches its spend cap, the platform also
|
|
107
|
+
writes one line to the project's logs and emails you, once that month.
|
|
108
|
+
|
|
109
|
+
Nothing retries the model call, and nothing switches to a paid model. Any
|
|
110
|
+
other error is thrown again, so the platform logs it as a function error
|
|
111
|
+
instead of hiding a bug behind "unavailable". `sw.ai` → `error-codes` lists
|
|
112
|
+
every code.
|
|
113
|
+
|
|
114
|
+
## 2. Call it from the page
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
// src/services/suggest-title.ts
|
|
118
|
+
import { auth } from './auth';
|
|
119
|
+
|
|
120
|
+
export type Suggestion =
|
|
121
|
+
| { kind: 'title'; title: string } // the model's suggestion
|
|
122
|
+
| { kind: 'refused'; error: string } // sign in, shorten the text, or wait
|
|
123
|
+
| { kind: 'unavailable' }; // no suggestion this time
|
|
124
|
+
|
|
125
|
+
export async function suggestTitle(text: string): Promise<Suggestion> {
|
|
126
|
+
let response: Response;
|
|
127
|
+
try {
|
|
128
|
+
response = await auth.fetch('/api/suggest-title', {
|
|
129
|
+
method: 'POST',
|
|
130
|
+
headers: { 'content-type': 'application/json' },
|
|
131
|
+
body: JSON.stringify({ text }),
|
|
132
|
+
});
|
|
133
|
+
} catch {
|
|
134
|
+
return { kind: 'unavailable' };
|
|
135
|
+
}
|
|
136
|
+
const result: unknown = await response.json().catch(() => null);
|
|
137
|
+
const fields = typeof result === 'object' && result !== null ? result : {};
|
|
138
|
+
if (response.ok && 'title' in fields && typeof fields.title === 'string' && fields.title) {
|
|
139
|
+
return { kind: 'title', title: fields.title };
|
|
140
|
+
}
|
|
141
|
+
if ([400, 401, 429].includes(response.status) && 'error' in fields && typeof fields.error === 'string') {
|
|
142
|
+
return { kind: 'refused', error: fields.error };
|
|
143
|
+
}
|
|
144
|
+
return { kind: 'unavailable' };
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
// src/pages/TitleSuggestion.tsx
|
|
150
|
+
import { useState } from 'react';
|
|
151
|
+
import { suggestTitle, type Suggestion } from '../services/suggest-title';
|
|
152
|
+
|
|
153
|
+
export function TitleSuggestion() {
|
|
154
|
+
const [text, setText] = useState('');
|
|
155
|
+
const [pending, setPending] = useState(false);
|
|
156
|
+
const [result, setResult] = useState<Suggestion | null>(null);
|
|
157
|
+
|
|
158
|
+
async function suggest() {
|
|
159
|
+
setPending(true);
|
|
160
|
+
setResult(null);
|
|
161
|
+
setResult(await suggestTitle(text)); // never throws: every outcome is a Suggestion
|
|
162
|
+
setPending(false);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return (
|
|
166
|
+
<section className="panel" aria-labelledby="suggest-title-heading">
|
|
167
|
+
<h2 id="suggest-title-heading">Name your note</h2>
|
|
168
|
+
<div className="field">
|
|
169
|
+
<label htmlFor="note-text">
|
|
170
|
+
Note <span className="hint">{text.length}/4,000</span>
|
|
171
|
+
</label>
|
|
172
|
+
<textarea id="note-text" rows={6} maxLength={4000} value={text} onChange={(event) => setText(event.target.value)} />
|
|
173
|
+
</div>
|
|
174
|
+
<button className="button" onClick={suggest} disabled={pending || !text.trim()}>
|
|
175
|
+
{pending ? 'Suggesting…' : 'Suggest a title'}
|
|
176
|
+
</button>
|
|
177
|
+
<div aria-live="polite">
|
|
178
|
+
{result?.kind === 'title' && <p>Suggested title: <strong>{result.title}</strong></p>}
|
|
179
|
+
{result?.kind === 'refused' && <p className="error" role="alert">{result.error}</p>}
|
|
180
|
+
{result?.kind === 'unavailable' && (
|
|
181
|
+
<p className="muted">Suggestions aren't available right now. Your note is unchanged; try again later.</p>
|
|
182
|
+
)}
|
|
183
|
+
</div>
|
|
184
|
+
</section>
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Render `<TitleSuggestion />` inside `SignedInLayout` on the starter's
|
|
190
|
+
`src/pages/HomePage.tsx`. It uses the starter's `.panel`, `.field`,
|
|
191
|
+
`.button`, `.hint`, `.error` and `.muted` classes, so it carries your
|
|
192
|
+
`tokens.css` look.
|
|
193
|
+
|
|
194
|
+
- **`title`**: show it as a suggestion. The user's note isn't changed until
|
|
195
|
+
they choose to use it.
|
|
196
|
+
- **`refused`**: show the function's message. The user can sign in again,
|
|
197
|
+
shorten the note, or wait for their hourly limit.
|
|
198
|
+
- **`unavailable`**: say that suggestions aren't available right now. Never
|
|
199
|
+
show a made-up title or billing terms. The user can press the button again
|
|
200
|
+
later; nothing retries on its own.
|
|
201
|
+
|
|
202
|
+
## 3. Check it
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
somewhere typecheck
|
|
206
|
+
somewhere deploy
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Sign in, write a few sentences and press **Suggest a title**. The button reads
|
|
210
|
+
"Suggesting…", then the panel shows the title. Each suggestion uses the
|
|
211
|
+
included AI allowance. `ai_catalog` → `free_complete_default.limits` shows the
|
|
212
|
+
default's request and token limits.
|
|
213
|
+
|
|
214
|
+
## 4. Going further
|
|
215
|
+
|
|
216
|
+
| Need | Use |
|
|
217
|
+
|---|---|
|
|
218
|
+
| A validated object instead of text | `sw.ai` → `structured-output-responseschema` |
|
|
219
|
+
| Streaming | `stream: true` on `provider: 'anthropic'` only; the free default refuses it. Today the whole answer arrives as one event-stream payload: `sw.ai` → `cost-on-the-result-settled-or-pending-openai-and-anthropic-completions` |
|
|
220
|
+
| Search a user's own documents by meaning | `sw.ai` → `search-a-users-documents` |
|
|
221
|
+
| Bill your own Anthropic or OpenAI account | `sw.ai` → `provider-credentials-optional-byok` |
|
|
222
|
+
| Background work at about half the token price | `service_tier: 'flex'` on `provider: 'openai'`, run in a job: `sw.ai` → `flex-service-tier-50-discount`; `recipe-background-jobs` |
|
|
223
|
+
| Conversations and per-user memory | `sw.ai` → `conversation-history-all-providers`, `per-user-memory-swaiusermemory` |
|
|
224
|
+
| Check text before you use it | `sw.ai` → `content-moderation` |
|
|
225
|
+
| Images, speech and transcription | `sw.ai` → `image-generation-models`, `text-to-speech`, `other-ai-surfaces-also-on-swai` |
|
|
226
|
+
| The project's monthly AI spend cap | `sw.ai` → `monthly-ai-spend-cap-per-project` |
|
|
227
|
+
| Every model, price and capability | `ai_catalog` (`sw.ai` → `catalog-discovery`) |
|