# Local development, logs and streaming
Source: https://sovaria.in/docs/functions-local-development

Run functions locally exactly as in production, tail logs live, set secrets per environment, stream responses and size memory.

## Run functions locally

> **Note:** TypeScript functions need [Bun](https://bun.sh) and Go functions need [Go](https://go.dev/dl) installed locally. Go is built like production on every call: the generated `main`, no module downloads (`vendor/` only), no cgo — so a build that works here builds on deploy.

`sov functions serve` runs a function folder (or a folder of functions) the way Sovaria runs them in production: the JSON body is the event, `context` has the same keys, generators stream, and your plan's memory and timeout apply. Every request runs the code on disk, so there is nothing to restart; saving a file with a syntax error is reported at once.

CLI:
```bash
sov functions serve ./functions --env-file .env.local
# Serving 2 function(s) — same runtime as production, reloads on every call
#   hello (python, main.py)  POST http://127.0.0.1:8787/functions/v1/hello
#   chat  (node, index.mjs)  POST http://127.0.0.1:8787/functions/v1/chat

curl -X POST http://127.0.0.1:8787/functions/v1/hello -d '{"name":"Ada"}'
```

Secrets come from `.env` and `.env.local` in the folder, `--env-file` and `--env NAME=value`; nothing is sent to Sovaria. Options: `--timeout`, `--memory`, `--port`, `--no-watch`.

## Logs

CLI:
```bash
sov functions logs                          # every function, newest first
sov functions logs send-receipt --status error --since 2h
sov functions logs --source trigger -q "Stripe"
sov functions logs send-receipt --follow    # live tail (Server-Sent Events) until Ctrl+C
```

In the console, **Edge Functions → Triggers & logs → Invocations** filters by function, status, how the run started (API call, streamed call, trigger, schedule, background) and text, and updates live. Logs are kept as long as your plan's function log retention.

## Secrets per environment

Environment secrets reach every function in one environment (production, staging, an API version). A function's own secrets win over them, and they win over project secrets. Values are encrypted and never shown again.

CLI:
```bash
sov functions secrets set STRIPE_KEY=sk_test_… --env staging
sov functions secrets set --env-file .env.production          # production when --env is omitted
sov functions secrets list --env staging
sov functions secrets unset STRIPE_KEY --env staging
```

## Streamed responses

A handler that yields (Python generator, JavaScript `async function*`) streams each item to callers that ask for it with `Accept: text/event-stream` (Server-Sent Events) or `?stream=ndjson`. Without it, the items come back joined (strings) or as a list. The run is still one invocation with one log entry.

Python:
```python
def handler(event, context):
    for token in ["Hello", ", ", "world"]:
        yield token
```

JavaScript:
```js
export async function* handler(event) {
  for await (const chunk of model.stream(event.prompt)) yield chunk.text;
}
```

Browser:
```js
const res = await fetch('https://<ref>.sovarias.in/functions/v1/chat', {
  method: 'POST', headers: { Authorization: `Bearer ${token}`, Accept: 'text/event-stream' },
  body: JSON.stringify({ prompt: 'Hi' }),
})
const reader = res.body.getReader()   // data: <item> per chunk, then "event: done"
```

CLI:
```bash
sov functions stream chat --data '{"prompt":"Hi"}'
```

## Memory and timeout per function

Each function runs with your plan's maximum memory and run time unless you set less. Less memory also lowers its compute bill, which is measured at the memory it runs with.

CLI:
```bash
sov functions limits resize-image --memory 512 --timeout 30
sov functions limits resize-image            # show setting, effective values and plan maximums
sov functions limits resize-image --reset
```

## Cold starts

Sovaria keeps sandboxes started and waiting, so a call skips interpreter start-up. Python functions go further: after a call, the same sandbox is kept for the next call of the same function version. In production this brought a simple function from a median of about 400–900 ms to about 100 ms.

A kept sandbox serves only one version of one function in one project and environment; it is never shared with another function, version, project or customer. It is replaced after 100 calls, after 60 seconds idle, after any failed or timed-out call, or when it uses too much memory or CPU.

Because of that, module-level variables and files in /tmp can still be there on the next call of the same version, as on AWS Lambda. Use them for caches and clients, never for one caller's data: keep anything per user or per request inside the handler.
