# Ask your data demo: source

> The code that makes the Ask your data demo work, walked through snippet by snippet.

A function is your own logic, run by the platform whenever the agent asks a question of the data.

## What the function takes

Its inputs are the schema the model sees, so each description is written for the model.

```json file=demos/ask-your-data/functions/ask-your-data-query-sales.json
"kind": "declarative",
"input": {
  "metric": {
    "type": "string",
    …
  },
  …
```

## Steps, not a server

Sample rows sit in the first step. Later steps group and total them, with no database and no model call.

```json file=demos/ask-your-data/functions/ask-your-data-query-sales.json
{
  "id": "groups",
  "heading": "Group the rows by the chosen dimension or pair",
  "do": {
    "$groupBy": {
      "items": {
        "$bind": "steps.labelled.result"
      },
      "as": "r",
      "key": "label"
    }
  }
},
```

## Granted to the agent

The agent can call these two functions and nothing else that reaches data.

```json file=demos/ask-your-data/demo.json
"functions": [
  { "handle": "ask-your-data-query-sales", "idempotencyKey": "spec-701-ask-your-data-query-sales-create-v1", "file": "functions/ask-your-data-query-sales.json" },
  { "handle": "ask-your-data-growth", "idempotencyKey": "spec-701-ask-your-data-growth-create-v1", "file": "functions/ask-your-data-growth.json" }
],
```

## Charted as it lands

The page turns each finished call into a chart before the agent has written its answer.

```ts file=src/demos/ask-your-data/insights.ts
export function functionInsight(row: { id: string; name: string; status: string; output?: string }): Insight | null {
  if (!(FUNCTION_TOOLS as readonly string[]).includes(row.name) || row.status !== 'done' || !row.output) return null
  …
  const r = isObj(parsed.result) ? parsed.result : parsed
  if (!Array.isArray(r.groups)) return null
  …
```
