> ## Documentation Index
> Fetch the complete documentation index at: https://docs.onecortex.io/llms.txt
> Use this file to discover all available pages before exploring further.

# A TypeScript function

> Deploy any TypeScript or JavaScript agent as an exported function that returns or yields its reply, with its own tool calls if you want them.

## What is supported

Any TypeScript or JavaScript on Node.js 22 or 20. A function is the documented path for TypeScript agents, and the way to deploy any library Onecortex does not recognise.

## A complete agent

```yaml agent.yml theme={null}
apiVersion: v1
runtime: node22
entrypoint: agent.ts#agent
dependencies: package.json
```

```json package.json theme={null}
{
  "type": "module",
  "dependencies": {}
}
```

```ts agent.ts theme={null}
export async function agent(prompt: string): Promise<string> {
  return `You said: ${prompt}`
}
```

Call any model you like from inside the function, with its key added as a secret on the agent's **Config** tab. See [Configuration and secrets](/build/configuration).

<Card title="A complete A TypeScript function example" icon="github" href="https://github.com/onecortex-io/examples/tree/main/order-status">
  `order-status`: A plain function with a lookup tool of its own, reported as real tool events.
</Card>

## The prompt and the reply

The function receives the prompt, and a context as its second argument: `{ sessionId, request }`, where `request` holds `prompt`, `params`, `surface` and `messages`. What it may return:

| Shape | Reply |
| - | - |
| A string, or a promise of one | The string, once |
| An async generator yielding strings | Each string as it is yielded |
| An object with `invoke(prompt)` | What `invoke` returns |

## Events it reports

| Events | From |
| - | - |
| `text` | Each string you return or yield |
| Any event | Yield an object with a `type`, and it is sent as that event, checked first |
| `done` or `error` | The end of the run: always exactly one |

See [Streaming and events](/build/streaming).

## The caller's fields

In `context.request.params`:

```ts theme={null}
export async function agent(prompt: string, context: { request: { params: Record<string, unknown> } }) {
  const model = String(context.request.params['model'] ?? 'gpt-5')
  return callMyModel(model, prompt)
}
```

## Report tool calls

Yield event objects alongside your text:

```ts theme={null}
export async function* agent(prompt: string) {
  yield { type: 'tool_call_start', id: 'call_1', name: 'lookup_order' }
  yield { type: 'tool_call_args', id: 'call_1', delta: JSON.stringify({ orderId: '1042' }) }
  yield { type: 'tool_call_end', id: 'call_1' }
  yield { type: 'tool_result', id: 'call_1', output: '{"status":"shipped"}' }
  yield 'Order 1042 has shipped.'
}
```

Each object needs the fields of its [event type](/build/streaming). `v` is filled in for you, and Onecortex adds the closing `done`.

## Known limits

* An object with a `type` key is always read as an event. Yield a string to send data that has one.

## Troubleshooting

| You see | Do this |
| - | - |
| `Your agent yielded an event Onecortex cannot read: <problem>.` | Fix the field it names. |
| A file read fails with `ENOENT` | Paths are relative to the agent folder, which is the working directory. Read the file from there. |

More on [Troubleshooting](/production/troubleshooting) and [Errors](/production/errors).
