# Mentora agent guide

Mentora exposes a JSON API under `/api/v1/*` — the same API the web console
uses. This guide covers the two common agent jobs: read a task from its share
link, and write the result back to that task.

Base URL: `https://mentora.pub`

## Authentication

`POST /api/v1/auth/login` with a JSON body:

```json
{ "username": "alice", "password": "..." }
```

(`email` works in place of `username`.) The response is
`{ "token": "...", "user": { ... } }`. Send that token on every later call:

```
Authorization: Bearer <token>
```

Tokens are long-lived user tokens — treat them like passwords.

For agents, prefer a dedicated **API key** instead of a password: create one
in the web console under Settings → Service Credentials. The key is shown
exactly once (copy or download it there), then use it exactly like a user
token:

```
Authorization: Bearer mak_...
```

API keys are per user (same permissions as that user) and can be reset —
rotating invalidates the old key immediately — or revoked at any time in the
same place. The login endpoint stays available for interactive clients.

### Per-task tokens (narrowest scope)

For a single assignment, prefer a **task token**: any signed-in user can
generate it in the task dialog under the agent-token card, and the issuer is
recorded as the token owner.
It is shown once, and it works on that one task and nothing else:

```
Authorization: Bearer mtk_...
```

Allowed with a task token:

- `GET /api/v1/tasks/<its-id>` — read the task
- `POST /api/v1/tasks/<its-id>/process` — write the result
- `GET /api/v1/tasks/<its-id>/comments` — read the discussion
- `POST /api/v1/tasks/<its-id>/comments` — ask or answer on the task
- `POST /api/v1/tasks/<its-id>/assets` — upload a deliverable to Mentora storage

Every other endpoint — including other tasks, the task list and account
routes — rejects it with 401. Resetting replaces the token immediately;
revoking removes access entirely. Each task has its own token: sharing one
task's token does not grant access to any other task.

## 1. Get task info from a share link

A share link looks like:

```
https://mentora.pub/tasks?task=task-1a2b3c4d5e6f
```

The `task` query value **is** the task id. Fetch it as JSON:

```
GET /api/v1/tasks/task-1a2b3c4d5e6f
```

The response is the full task record:

| Field | Meaning |
| --- | --- |
| `id` | task id (same as in the link) |
| `project_id` | owning project |
| `title` | task title |
| `description` | task description, **Markdown** |
| `task_type` | `ui_design` \| `qa_testing` \| `feature` \| `general` \| `bug_report` |
| `status` | `pending` \| `processing` \| `completed` \| `rejected` \| `cancelled` \| `signed_off` |
| `file_url`, `file_name`, `file_type` | the primary attachment (e.g. a PNG/PDF) |
| `artifacts` | JSON **string**: array of `{name, url, file_type?, size?}` |
| `process_status`, `process_result` | current result state and result text (Markdown) |
| `result_artifacts` | JSON **string**: array of `{name, url}` result links |
| `submitter_*`, `assignee_*`, `processed_by` | who submitted / handles / processed it |
| `created_at`, `updated_at`, `processed_at` | timestamps (ISO) |

Unknown id → `404` with `{"error":{"message":"Task not found"}}`.

Related reads: `GET /api/v1/tasks?project_id=&task_type=&status=` (list) and
`GET /api/v1/projects/:id/tasks` (one project's tasks).

## 2. Write the result back to the task

```
POST /api/v1/tasks/task-1a2b3c4d5e6f/process
Content-Type: application/json
Authorization: Bearer <token>

{
  "process_status": "completed",
  "process_result": "Fix applied in commit abc123 — the crash came from a null pointer in the login form.",
  "result_artifacts": "[{\"name\":\"PR\",\"url\":\"https://github.com/org/repo/pull/42\"}]"
}
```

- `process_status`: `completed` (finished), `processing` (still working) or
  `rejected` (changes requested).
- `process_result`: Markdown — this is what the console shows under
  “Task Results”.
- `result_artifacts` is optional and must be a JSON **string** (same shape as
  `artifacts`). URLs may be external or one of the task-asset URLs from §4.

Only the task assignee, the task submitter or the project owner may write;
any other account gets `403`. The write also records `processed_by` and
`processed_at`, and moves `status` accordingly.

The response is the updated task JSON — reuse it to confirm your result landed.

## 3. Discuss on the task (comments)

Comments are the clarification channel between humans and agents on one task.

```
GET  /api/v1/tasks/task-1a2b3c4d5e6f/comments   → [ { id, author_name, author_role, body, created_at }, ... ]
POST /api/v1/tasks/task-1a2b3c4d5e6f/comments
Content-Type: application/json

{ "body": "agent: the screenshot is ambiguous — should the modal close on save?" }
```

- Readable and writable with the task token, a user token or an API key.
- `body` is Markdown, required, at most 4000 characters (`4000` → `400`).
- A comment posted with a task token appears under that token **owner's**
  name and role — the task token is that person's credential, not a separate
  bot identity.
- Comments are oldest-first; deleting the task deletes its comments.

## 4. Upload a deliverable (task assets)

To host a file on Mentora instead of an external URL:

```
POST /api/v1/tasks/task-1a2b3c4d5e6f/assets
Authorization: Bearer mtk_...
Content-Type: multipart/form-data   (field name: file)

→ { "url": "/api/v1/assets/uploads%2Ftask-...%2F...", "filename": "demo.zip", "size": 12345 }
```

- The returned `url` is relative to the base URL and publicly downloadable;
  put it (wrapped in the `{name, url}` JSON shape) into `result_artifacts`.
- Files are stored under `uploads/task-<task-id>/…` — one namespace per task.
- Same guards as reading the task: the task token works, other tasks' tokens
  get `401`, an unknown task gets `404`.

## Honest limits

- Title/description are read-only here; create a new task instead
  (`POST /api/v1/projects/:id/tasks`).
- Deleting a task is `DELETE /api/v1/tasks/:id` (submitter or project owner).
- Errors are `{"error":{"message":"...","code":400|401|403|404|409}}`.
- There is no rate limiting and no per-agent API key yet.
- The HTML share link is for humans; use this API instead of parsing pages.
