@somewhere-cloud/cli 0.38.153 → 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 +11 -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
- package/runtime/DECLARED-DATA-VENDOR.json +5 -5
- package/runtime/PLATFORM-SOURCE.json +1 -1
- package/runtime/SOURCE-LINT-VENDOR.json +2 -2
- package/runtime/declared-data.cjs +37 -19
- package/runtime/source-lint.cjs +1 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# SQL compatibility — what Postgres code ports, and what doesn't
|
|
2
|
+
|
|
3
|
+
Use familiar SQL for joins, JSON and full-text search, and commit related
|
|
4
|
+
writes in atomic batches. The
|
|
5
|
+
platform translates the Postgres forms listed below into the project
|
|
6
|
+
database's dialect. For semantic search, use `sw.search` without a separate
|
|
7
|
+
vector database.
|
|
8
|
+
|
|
9
|
+
Translation differs between developer SQL tools and a SQL-mode function's
|
|
10
|
+
raw `sw.db.query` or `sw.db.server.query`. The sections below name each
|
|
11
|
+
surface and its supported forms. Expression, typing and ordering follow
|
|
12
|
+
the project database's dialect.
|
|
13
|
+
|
|
14
|
+
## Common SQL spellings
|
|
15
|
+
|
|
16
|
+
- `NOW()` becomes `datetime('now')`
|
|
17
|
+
- `TRUE` / `FALSE` become `1` / `0`
|
|
18
|
+
- `ILIKE` becomes `LIKE` (case-insensitive for ASCII)
|
|
19
|
+
- In migrations, `SERIAL` becomes `INTEGER`; declare
|
|
20
|
+
`INTEGER PRIMARY KEY` for generated IDs.
|
|
21
|
+
- `BOOLEAN` becomes `INTEGER`
|
|
22
|
+
- `$1, $2, …` placeholders become positional `?`
|
|
23
|
+
- `col->>'key'` and `col->'key'` become `json_extract(col, '$.key')`
|
|
24
|
+
|
|
25
|
+
`RETURNING` works natively on INSERT, UPDATE and DELETE on write-capable
|
|
26
|
+
surfaces. In managed projects, `sw.db.server.query` accepts read queries
|
|
27
|
+
only; change declared rows through the structured operations. Run schema
|
|
28
|
+
changes through `db_migrate` for hand-managed tables or deploy
|
|
29
|
+
`db/schema.ts` for declared tables.
|
|
30
|
+
|
|
31
|
+
## Query semantics
|
|
32
|
+
|
|
33
|
+
- `SELECT 1 / 0` returns `NULL`; it does not throw a Postgres-style
|
|
34
|
+
division-by-zero error. Guard the divisor explicitly when zero is invalid.
|
|
35
|
+
- Ascending `ORDER BY` places `NULL` first by default. Add `NULLS LAST`
|
|
36
|
+
when that ordering is required.
|
|
37
|
+
- `LIKE` is case-insensitive for ASCII by default. Use explicit normalization
|
|
38
|
+
when case behavior is part of the application contract.
|
|
39
|
+
- Integer operands use integer numeric affinity (`5 / 2` is `2`); use a real
|
|
40
|
+
operand such as `2.0` when fractional division is required.
|
|
41
|
+
|
|
42
|
+
The database has a 30-second hard query ceiling. The runtime `sw.db.query`
|
|
43
|
+
surface does not expose a shorter true-cancellation contract. The external
|
|
44
|
+
`db_query timeout_ms` stops the caller waiting; database work already accepted
|
|
45
|
+
can continue.
|
|
46
|
+
|
|
47
|
+
## Additional translations in developer SQL tools
|
|
48
|
+
|
|
49
|
+
`db_query`, `db_batch`, `db_migrate` and managed
|
|
50
|
+
`sw.db.server.query` also translate the following forms, subject to
|
|
51
|
+
their read/write permissions. For `sw.db.query` or `sw.db.server.query`
|
|
52
|
+
inside a SQL-mode function, write the equivalent form on the right so the
|
|
53
|
+
query works through either the project binding or the SQL API:
|
|
54
|
+
|
|
55
|
+
- Date math: `created_at > NOW() - INTERVAL '7 days'` → `created_at > datetime('now','-7 days')` (units: second, minute, hour, day, month, year)
|
|
56
|
+
- A standalone `SELECT generate_series(1, 10)` → `WITH RECURSIVE s(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM s WHERE n<10) SELECT n FROM s`
|
|
57
|
+
- `CREATE TYPE … AS ENUM('a','b')` → `col TEXT CHECK(col IN ('a','b'))` (auto-translated when the CREATE TYPE and the table using it are in the same migration)
|
|
58
|
+
- `array_agg(tag)` → `json_group_array(tag)`
|
|
59
|
+
|
|
60
|
+
JSONB containment `data @> '{"k":"v"}'` auto-translates to `json_extract(data,'$.k') = 'v'` for the **flat case** — one or more keys whose values are scalar strings or numbers (multiple keys are AND-ed together, matching containment). Nested objects, array values, and boolean/null values are NOT auto-translated — they would risk silently matching the wrong rows, so they fail loud; write the explicit `json_extract` conditions yourself for those.
|
|
61
|
+
|
|
62
|
+
## Forms to replace
|
|
63
|
+
|
|
64
|
+
The developer SQL tools reject these forms with a corrective error. Raw
|
|
65
|
+
queries through a function's project database binding return the database
|
|
66
|
+
error; queries routed through the SQL API receive the corrective error.
|
|
67
|
+
|
|
68
|
+
- `DISTINCT ON (user_id) … ORDER BY user_id, created_at DESC` → use
|
|
69
|
+
`ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY created_at DESC)`
|
|
70
|
+
and keep `rn = 1`.
|
|
71
|
+
- `tags TEXT[]` (any array type) → store as JSON text: `tags TEXT DEFAULT '[]'`, query with `WHERE tag IN (SELECT value FROM json_each(tags))`
|
|
72
|
+
- `BEGIN; … COMMIT;` in one query → use `sw.db.tx([...])` for declared tables (atomic, all-or-nothing); in a SQL-mode project `sw.db.batch([{ sql, params }, …])` is the raw equivalent
|
|
73
|
+
- `GEOMETRY`, `GEOGRAPHY`, `TSVECTOR`, `HSTORE`, `INET`, `BYTEA`, … → use full-text / `sw.search` / `sw.fs`; otherwise move the workload to Postgres
|
|
74
|
+
|
|
75
|
+
Arrays are the most common: store them as a JSON text column and unroll with `json_each()` for querying.
|
|
76
|
+
|
|
77
|
+
## Built-in alternatives
|
|
78
|
+
|
|
79
|
+
- Full-text search — native: `CREATE VIRTUAL TABLE docs USING fts5(title, body)`, then `WHERE docs MATCH 'term'`. Or `sw.search` for embeddings + ranking.
|
|
80
|
+
- Vector / semantic search — `sw.search` (no separate vector database).
|
|
81
|
+
- Per-user row security — the structured builder
|
|
82
|
+
`sw.db.from/insert/update/remove` on a declared `scoped` table, instead of
|
|
83
|
+
CREATE POLICY.
|
|
84
|
+
|
|
85
|
+
## Not available on the managed database
|
|
86
|
+
|
|
87
|
+
PL/pgSQL (write logic in your server function), PostGIS, table
|
|
88
|
+
partitioning, and custom engine index types (GIN/GiST/BRIN — use an
|
|
89
|
+
app-managed index table + FTS5). For genuinely Postgres-only needs you can
|
|
90
|
+
attach your own PostgreSQL database and query it alongside `sw.db` —
|
|
91
|
+
`docs({ topic: 'postgres' })` — or export and move the workload out entirely.
|
|
92
|
+
Either way the platform does not provide a managed Postgres adapter or an
|
|
93
|
+
automatic migration service: an attached database is pass-through, and moving
|
|
94
|
+
existing data into it is yours to do.
|
|
95
|
+
|
|
96
|
+
Related: `database-engine`, `sw.db`, `postgres`, `portability`.
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# Somewhere agent loop — sw.agent
|
|
2
|
+
|
|
3
|
+
`sw.agent` runs bounded model-and-tool work in your deployed function.
|
|
4
|
+
Use `run` to wait inline; it stays in the current invocation and is not a
|
|
5
|
+
resumable background job. Use `start` for checkpointed durable work with
|
|
6
|
+
`status` and `cancel`. Durable callbacks re-enter the then-current deployed
|
|
7
|
+
function, so a run can cross a deploy; tools and hooks must be recreated there.
|
|
8
|
+
|
|
9
|
+
Set `maxSteps` and `maxSpendCents`; spend is checked between billed calls,
|
|
10
|
+
so one call can cross the cap. On the project's own provider key the platform
|
|
11
|
+
records no cost, so `maxSpendCents` never stops the run: bound it with
|
|
12
|
+
`maxSteps` and `maxTokens` (see `sw.ai` → provider credentials). `prepareStep`, `onStepFinish`, and `stopWhen`
|
|
13
|
+
control each step. Durable tool calls carry a stable `toolCallId` for your
|
|
14
|
+
side-effect deduplication; this does not make side effects exactly once.
|
|
15
|
+
Cancellation prevents later steps and cannot undo work already running.
|
|
16
|
+
|
|
17
|
+
Start with `recipe-ai-agent`; see `recipe-escalation` and `recipe-multi-chat`
|
|
18
|
+
for model escalation and conversation UI. History and compaction use the
|
|
19
|
+
`sw.ai` conversation options passed through each step.
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const result = await sw.agent.run({
|
|
23
|
+
prompt: 'Summarize these notes: the delivery arrived, and the invoice is pending.',
|
|
24
|
+
maxSteps: 4,
|
|
25
|
+
maxSpendCents: 25,
|
|
26
|
+
})
|
|
27
|
+
console.log(result.text, result.completion_reason)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Calling forms
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
await sw.agent(options) // durable compatibility form
|
|
34
|
+
await sw.agent.run(options) // inline: wait for the terminal result
|
|
35
|
+
await sw.agent.start(options) // durable: return after creating the run
|
|
36
|
+
await sw.agent.status(agentId) // read durable status/result
|
|
37
|
+
await sw.agent.cancel(agentId) // request durable cancellation
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
The direct call and `.start` both create durable work. They use the same step
|
|
41
|
+
engine but intentionally return different limit-field names:
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
const direct = await sw.agent(options)
|
|
45
|
+
// { agent_id: string, status: string, max_turns: number }
|
|
46
|
+
|
|
47
|
+
const started = await sw.agent.start(options)
|
|
48
|
+
// { agent_id: string, status: string, max_steps: number }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Both durable forms require `options.model`. Use the returned `agent_id` with
|
|
52
|
+
`.status` and `.cancel`. `sw.agent.run` may omit a model and use the same
|
|
53
|
+
model default as `sw.ai.chat`.
|
|
54
|
+
|
|
55
|
+
## Inline execution: sw.agent.run(options)
|
|
56
|
+
|
|
57
|
+
`run` performs model calls and tool calls in the current function invocation,
|
|
58
|
+
then returns the terminal model response plus agent metrics:
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
// Enumerable fields added by sw.agent:
|
|
62
|
+
result.iterations
|
|
63
|
+
result.tool_calls_made
|
|
64
|
+
result.total_input_tokens
|
|
65
|
+
result.total_output_tokens
|
|
66
|
+
result.completion_reason
|
|
67
|
+
|
|
68
|
+
// Present but deliberately non-enumerable:
|
|
69
|
+
result.steps
|
|
70
|
+
result.total_cost_cents
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The rest of the terminal object is the final `sw.ai.chat` response, including
|
|
74
|
+
fields such as `text`, `content`, `provider`, `model`, `usage`, and
|
|
75
|
+
`stop_reason` when the model response supplies them. Because `steps` and
|
|
76
|
+
`total_cost_cents` are non-enumerable, object spread, `Object.keys`, and
|
|
77
|
+
`JSON.stringify` omit them even though direct property access works.
|
|
78
|
+
|
|
79
|
+
Completion reasons are `model_done`, `output_truncated`, `max_steps`,
|
|
80
|
+
`max_turns` when only that limit name was supplied, `stop_when`,
|
|
81
|
+
`cost_pending`, or the non-empty string returned by `stopWhen`.
|
|
82
|
+
`cost_pending` fires when a spend cap is configured and a delivered step's
|
|
83
|
+
cost is still pending: the loop returns that step and issues no further
|
|
84
|
+
model request against an unknown total. A pending step makes
|
|
85
|
+
`total_cost_cents` null — unknown, not zero — and without a spend cap the
|
|
86
|
+
loop may continue with a null aggregate. A partial tool call in truncated model
|
|
87
|
+
output is not executed. Reaching an agent step limit returns a terminal result;
|
|
88
|
+
it does not throw.
|
|
89
|
+
|
|
90
|
+
## Words as they arrive: onText
|
|
91
|
+
|
|
92
|
+
On Claude models, `sw.agent.run({ ..., onText: (text) => ... })` streams each
|
|
93
|
+
model call and hands every piece of the answer's text to `onText` as it
|
|
94
|
+
arrives. Use it to show the reply while it is still being written. The result
|
|
95
|
+
and the bill are the same as without it. A stream that stops early is billed
|
|
96
|
+
only after review, never estimated.
|
|
97
|
+
|
|
98
|
+
`onToolStart: (name) => ...` streams the same way and is called with a tool's
|
|
99
|
+
name as soon as the model starts writing that tool call, before its input has
|
|
100
|
+
arrived and before the tool runs. Use it to show each step as it begins. Either
|
|
101
|
+
hook turns streaming on; they can be used together or alone.
|
|
102
|
+
|
|
103
|
+
## Durable execution: sw.agent(options) and sw.agent.start(options)
|
|
104
|
+
|
|
105
|
+
A durable call stores the initial messages, creates background work, and returns
|
|
106
|
+
immediately. The platform invokes the same deployed function for each signed
|
|
107
|
+
step callback. Call the same durable form from that handler; the runtime detects
|
|
108
|
+
the callback envelope and advances exactly one step instead of creating another
|
|
109
|
+
run.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
const config: SomewhereAgentDurableOptions = {
|
|
113
|
+
model: 'claude-haiku-4-5',
|
|
114
|
+
prompt: 'Reconcile yesterday’s orders.',
|
|
115
|
+
tools: [
|
|
116
|
+
{
|
|
117
|
+
name: 'mark_reconciled',
|
|
118
|
+
description: 'Mark one order reconciled.',
|
|
119
|
+
input_schema: {
|
|
120
|
+
type: 'object',
|
|
121
|
+
properties: { id: { type: 'string' } },
|
|
122
|
+
required: ['id'],
|
|
123
|
+
},
|
|
124
|
+
execute: async ({ id }, { agentId, turn, toolCallId }) => {
|
|
125
|
+
// Use toolCallId as the idempotency identity for external side effects.
|
|
126
|
+
return { id, agentId, turn, toolCallId }
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const { agent_id, status, max_steps } = await sw.agent.start(config)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Tools, callbacks, and credentials are not serialized into the durable
|
|
136
|
+
checkpoint; the callback re-enters the deployed function and recreates them.
|
|
137
|
+
The tool context carries stable `agentId`, `turn`, and `toolCallId` values
|
|
138
|
+
for retry-safe deduplication. Inline runs set `agentId` to `null`; durable runs
|
|
139
|
+
use the durable id. A durable run can cross a deploy because callbacks execute
|
|
140
|
+
the then-current function code; checkpoint state records every observed
|
|
141
|
+
deployment version.
|
|
142
|
+
|
|
143
|
+
## Status and cancellation
|
|
144
|
+
|
|
145
|
+
`sw.agent.status(agentId)` reads the durable job and returns its complete job
|
|
146
|
+
status payload with a normalized `agent_id` field added:
|
|
147
|
+
|
|
148
|
+
```js
|
|
149
|
+
const state = await sw.agent.status(agent_id)
|
|
150
|
+
// { ...jobStatus, agent_id: jobStatus.job_id || agent_id }
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
The job payload carries the durable status, checkpoint/result or terminal error
|
|
154
|
+
when present, and the persisted agent state. See `docs({ topic: 'sw.jobs' })`
|
|
155
|
+
for the job status fields.
|
|
156
|
+
|
|
157
|
+
`sw.agent.cancel(agentId)` requests cancellation and returns the cancellation
|
|
158
|
+
job payload with the same normalized `agent_id` field:
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
const cancelled = await sw.agent.cancel(agent_id)
|
|
162
|
+
// { ...cancelJobResult, agent_id: cancelJobResult.job_id || agent_id }
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Cancellation prevents later steps. It cannot undo a tool side effect that
|
|
166
|
+
already completed or is already running. Both methods require a non-empty
|
|
167
|
+
string id; otherwise they throw `VALIDATION_ERROR` with status 400.
|
|
168
|
+
|
|
169
|
+
## Shared options
|
|
170
|
+
|
|
171
|
+
Every execution needs one input source: a non-empty `messages` array, a
|
|
172
|
+
`prompt` string, or an `input` string. Messages must be JSON-serializable and
|
|
173
|
+
are capped at 128 KiB of serialized text.
|
|
174
|
+
|
|
175
|
+
- `provider`, `model`, `system`, `systemPrompt`, `serviceTier`, and
|
|
176
|
+
`maxTokens` select the model call. Camel-case `systemPrompt`,
|
|
177
|
+
`serviceTier`, and `maxTokens` map to `system`, `service_tier`, and
|
|
178
|
+
`max_tokens` unless the snake-case field is already present.
|
|
179
|
+
- `conversation_id` and other `sw.ai.chat` history options pass through to
|
|
180
|
+
each model call. A conversation may span multiple inline or durable runs;
|
|
181
|
+
`agent_id` identifies one durable run only.
|
|
182
|
+
- `maxSteps`, `maxIterations`, and `maxTurns` are accepted limit names.
|
|
183
|
+
The first defined name in that order wins. The value must be an integer from
|
|
184
|
+
1 through 20; the default is 8.
|
|
185
|
+
- `maxSpendCents`, when it is a positive number, is checked before each model
|
|
186
|
+
call and again after each billed step: a run whose accumulated cost has
|
|
187
|
+
already reached the cap makes no further call, and a completed call that
|
|
188
|
+
takes the accumulated cost over the cap stops the run. Either throws
|
|
189
|
+
`AI_SPEND_CAP_EXCEEDED` with status 402 and partial metrics on
|
|
190
|
+
`error.metrics`. The cap bounds the run, not the price of any single call.
|
|
191
|
+
|
|
192
|
+
## Tools
|
|
193
|
+
|
|
194
|
+
`tools` accepts a keyed object or an array. Array entries have this shape:
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
const tool: SomewhereAgentTool = {
|
|
198
|
+
name: 'look_up_order',
|
|
199
|
+
description: 'Look up an order by ID.',
|
|
200
|
+
inputSchema: {
|
|
201
|
+
type: 'object',
|
|
202
|
+
properties: { id: { type: 'string' } },
|
|
203
|
+
required: ['id'],
|
|
204
|
+
},
|
|
205
|
+
execute: async (input) => ({ id: input.id }),
|
|
206
|
+
};
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Use `inputSchema` or `input_schema` to describe the input. An executor also
|
|
210
|
+
receives the tool context as its second argument.
|
|
211
|
+
|
|
212
|
+
Tool names must be unique and no longer than 64 characters. Missing schemas
|
|
213
|
+
default to an empty object schema. A thrown tool error becomes an
|
|
214
|
+
`is_error: true` tool result so the next model step can recover. A missing
|
|
215
|
+
tool result and a non-serializable result also become error results. Serialized
|
|
216
|
+
tool-result content is truncated after 24,000 characters.
|
|
217
|
+
|
|
218
|
+
## Step callbacks
|
|
219
|
+
|
|
220
|
+
- `prepareStep(context)` runs before the model call. `context.stepNumber` is
|
|
221
|
+
0 for a run's first model call (it counts the steps already taken). It may return an object
|
|
222
|
+
overriding `messages`, `provider`, `model`, `system`, `tools`,
|
|
223
|
+
`toolChoice`, or `maxTokens` for that step; `null` and `undefined`
|
|
224
|
+
mean no override.
|
|
225
|
+
- `onStepFinish({ step, steps, agentId })` runs after the step trace is recorded.
|
|
226
|
+
`agentId` is the durable run's id (`null` for an inline run), so a durable
|
|
227
|
+
callback can store per-step data against its run. `step.cost_cents` is this
|
|
228
|
+
model call's cost (null while pending); a durable run's stored steps keep it.
|
|
229
|
+
- `stopWhen({ step, steps })` may return false, true, or a non-empty
|
|
230
|
+
completion-reason string.
|
|
231
|
+
- `onStep(event)` is also supported. It receives
|
|
232
|
+
`{ agentId, turn, maxTurns, output, toolCalls, done, stopReason }`; its
|
|
233
|
+
JSON-serializable return value is stored as `step.on_step`.
|
|
234
|
+
|
|
235
|
+
Callback failures throw `AI_PREPARE_STEP_FAILED`, `AI_STEP_HOOK_FAILED`, or
|
|
236
|
+
`AI_STOP_CONDITION_FAILED` with status 422. Their `error.metrics` contains
|
|
237
|
+
the partial token/tool counts; `steps` and `total_cost_cents` on that metrics
|
|
238
|
+
object are non-enumerable.
|
|
239
|
+
## Known inconsistencies
|
|
240
|
+
|
|
241
|
+
- The direct `sw.agent(options)` form returns `max_turns`, while
|
|
242
|
+
`sw.agent.start(options)` returns `max_steps`. Read the field that matches
|
|
243
|
+
the form you called; they are not aliases in the returned object.
|
|
244
|
+
- `steps` and `total_cost_cents` exist on inline results and partial error
|
|
245
|
+
metrics but are non-enumerable. Direct property access works; object spread,
|
|
246
|
+
`Object.keys`, and `JSON.stringify` omit them.
|
|
247
|
+
|
|
248
|
+
These are the supported runtime shapes today, not documentation aliases.
|