# Human in the loop

> Tasks, transitions and the task stream.

Source: https://docs.sommatic.ai/en/api/reference/human-in-the-loop/

## Create a new Task.

`POST /work-management/task/`

Returns the created task.

Authentication: Bearer token

### Request body

Task object.

- `organization_id` (string, required): Identifier for the organization this task belongs to.
- `project_id` (string): Optional project ID (tenant scope) that owns the task.
- `title` (string, required): Short human-readable task title (the goal).
- `details` (string): Actionable instructions describing what the human must do and why.
- `type` (WorkManagementTaskType, required): Task Type object.
Defines the category of human intervention required.

Available Types (Phase 1):
- approval: Human approval/rejection with reason
- data_completion: Fill missing structured data
- review: Human verification/review of something
- resolution: Human resolves a detected issue/blocker
- delegation: Human assigns/responsibilizes next steps
  - `id` (integer, required): Numeric type identifier.
  - `name` (string, required): Machine-readable type name.
  - `title` (string, required): Human-readable label.
  - `color` (string): Optional UI color for the type badge.
- `priority` (WorkManagementTaskPriority, required): Task Priority object.
Used for inbox ordering and escalation decisions.

Available Priorities (Phase 1):
- low, medium, high, critical
  - `id` (integer, required): Numeric priority identifier.
  - `name` (string, required): Machine-readable priority name.
  - `title` (string, required): Human-readable label.
  - `color` (string): Optional UI color for the priority badge.
- `assignee` (WorkManagementTaskAssignee): Assignment information.
Stores intended responsibility (user or group). Enforcement is outside the entity.
  - `assignee_type` (string, required): Defines whether assignment targets a user or group. Allowed values: `user`, `group`.
  - `user` (object): Assigned user reference when assignee_type is user.
    - `id` (string): User id.
    - `name` (string): Display name.
  - `group` (object): Assigned group reference when assignee_type is group.
    - `id` (string): Group id.
    - `name` (string): Group name.
- `ownership` (WorkManagementTaskOwnership): Ownership (claim) information.
Distinct from assignee: assignee is who should do it; ownership is who took it.
  - `is_claimed` (boolean, required): Indicates whether the task is claimed.
  - `claimed_by` (object): User reference who claimed the task.
    - `id` (string): User id.
    - `name` (string): Display name.
  - `claimed_at` (string): Unix timestamp in string format when claimed.
- `sla` (WorkManagementTaskSLA): Task SLA object (NOT a type).
This is a timing/constraint object that defines due dates and service expectations.

SLA is stored as an object because it is a set of time-related properties,
not a categorical type.
  - `due_at` (string, required): Unix timestamp in string format indicating when the task is due.
  - `sla_ms` (integer, required): SLA duration in milliseconds from task creation.
  - `is_overdue` (boolean, required): Computed at read time. True if current time exceeds due_at.
- `required_output` (WorkManagementTaskRequiredOutput, required): Defines what the human must provide to complete the task.
Stored as a JSON-schema-like object to enable deterministic validation.
  - `schema` (object, required): JSON-schema-like description of required outputs.
  - `ui_hint` (object): Optional UI hints for rendering a default completion form.
- `outputs` (object): Outputs provided by the human when completing the task.
Must match required_output.schema when BaseModel.status becomes "completed".
- `payload` (WorkManagementTaskPayload): Task operational payload.
Stores evidence blocks and soft references required by the human.
This is the correct place for task context/evidence (NOT BaseModel.context).
  - `summary` (string): Short summary describing what triggered the task.
  - `evidence` (array<object>): Controlled evidence blocks (sanitized).
    - `kind` (string): Evidence kind label.
    - `payload` (object): Evidence payload content.
  - `linked_entities` (array<object>): Soft references to related entities (not hard dependencies).
    - `system_domain` (string): Related domain.
    - `system_entity` (string): Related entity name.
    - `system_entity_id` (string): Related entity id.
  - `hitl` (object): Human-in-the-Loop control payload. Free-form object consumed by the App Engine app
declared in `task.execution.app_slug`. The shape is determined by `hitl.approval_type.name`
(binary_approval, tool_approval, plan_review, single_select_decision,
multi_select_decision, review_edit_reject).
- `execution` (WorkManagementTaskExecution): Task execution context for HITL (Human-in-the-Loop) integration.
Defines how the task is completed: via an App Engine app or via the default output form.

When app_slug is provided, the task delegates output collection to an App Engine app.
The app receives task context via input_payload and returns structured outputs via submitOutput().

When app_slug is null/absent, the task uses the auto-generated output form
derived from required_output.schema.

App Engine sessions already support execution_context_type: "task" and task_id natively.
  - `app_slug` (string): Slug of the App Engine app to open for HITL output collection.
If null, the task uses the default output form (auto-generated from required_output.schema).
  - `app_session_id` (string): Session ID created by App Engine when the app is opened.
Populated at runtime when the user opens the app, not at task creation.
  - `launch_mode` (string): App Engine launch mode for the app.
  - `input_payload` (object): Additional data passed to the app on launch.
Merged with task.payload when opening the app.

### Responses

- `200`: OK.

### Example request

```bash
curl -X POST "https://api.sommatic.ai/work-management/task/" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "organization_id": "<organization_id>",
       "title": "<title>",
       "type": {
         "id": 0,
         "name": "<name>",
         "title": "<title>"
       },
       "priority": {
         "id": 0,
         "name": "<name>",
         "title": "<title>"
       },
       "required_output": {
         "schema": {}
       }
     }'
```

## Update a Task.

`PATCH /work-management/task/`

Updates task data.

Authentication: Bearer token

### Request body

Task object update.

- `organization_id` (string, required): Identifier for the organization this task belongs to.
- `project_id` (string): Optional project ID (tenant scope) that owns the task.
- `title` (string, required): Short human-readable task title (the goal).
- `details` (string): Actionable instructions describing what the human must do and why.
- `type` (WorkManagementTaskType, required): Task Type object.
Defines the category of human intervention required.

Available Types (Phase 1):
- approval: Human approval/rejection with reason
- data_completion: Fill missing structured data
- review: Human verification/review of something
- resolution: Human resolves a detected issue/blocker
- delegation: Human assigns/responsibilizes next steps
  - `id` (integer, required): Numeric type identifier.
  - `name` (string, required): Machine-readable type name.
  - `title` (string, required): Human-readable label.
  - `color` (string): Optional UI color for the type badge.
- `priority` (WorkManagementTaskPriority, required): Task Priority object.
Used for inbox ordering and escalation decisions.

Available Priorities (Phase 1):
- low, medium, high, critical
  - `id` (integer, required): Numeric priority identifier.
  - `name` (string, required): Machine-readable priority name.
  - `title` (string, required): Human-readable label.
  - `color` (string): Optional UI color for the priority badge.
- `assignee` (WorkManagementTaskAssignee): Assignment information.
Stores intended responsibility (user or group). Enforcement is outside the entity.
  - `assignee_type` (string, required): Defines whether assignment targets a user or group. Allowed values: `user`, `group`.
  - `user` (object): Assigned user reference when assignee_type is user.
    - `id` (string): User id.
    - `name` (string): Display name.
  - `group` (object): Assigned group reference when assignee_type is group.
    - `id` (string): Group id.
    - `name` (string): Group name.
- `ownership` (WorkManagementTaskOwnership): Ownership (claim) information.
Distinct from assignee: assignee is who should do it; ownership is who took it.
  - `is_claimed` (boolean, required): Indicates whether the task is claimed.
  - `claimed_by` (object): User reference who claimed the task.
    - `id` (string): User id.
    - `name` (string): Display name.
  - `claimed_at` (string): Unix timestamp in string format when claimed.
- `sla` (WorkManagementTaskSLA): Task SLA object (NOT a type).
This is a timing/constraint object that defines due dates and service expectations.

SLA is stored as an object because it is a set of time-related properties,
not a categorical type.
  - `due_at` (string, required): Unix timestamp in string format indicating when the task is due.
  - `sla_ms` (integer, required): SLA duration in milliseconds from task creation.
  - `is_overdue` (boolean, required): Computed at read time. True if current time exceeds due_at.
- `required_output` (WorkManagementTaskRequiredOutput, required): Defines what the human must provide to complete the task.
Stored as a JSON-schema-like object to enable deterministic validation.
  - `schema` (object, required): JSON-schema-like description of required outputs.
  - `ui_hint` (object): Optional UI hints for rendering a default completion form.
- `outputs` (object): Outputs provided by the human when completing the task.
Must match required_output.schema when BaseModel.status becomes "completed".
- `payload` (WorkManagementTaskPayload): Task operational payload.
Stores evidence blocks and soft references required by the human.
This is the correct place for task context/evidence (NOT BaseModel.context).
  - `summary` (string): Short summary describing what triggered the task.
  - `evidence` (array<object>): Controlled evidence blocks (sanitized).
    - `kind` (string): Evidence kind label.
    - `payload` (object): Evidence payload content.
  - `linked_entities` (array<object>): Soft references to related entities (not hard dependencies).
    - `system_domain` (string): Related domain.
    - `system_entity` (string): Related entity name.
    - `system_entity_id` (string): Related entity id.
  - `hitl` (object): Human-in-the-Loop control payload. Free-form object consumed by the App Engine app
declared in `task.execution.app_slug`. The shape is determined by `hitl.approval_type.name`
(binary_approval, tool_approval, plan_review, single_select_decision,
multi_select_decision, review_edit_reject).
- `execution` (WorkManagementTaskExecution): Task execution context for HITL (Human-in-the-Loop) integration.
Defines how the task is completed: via an App Engine app or via the default output form.

When app_slug is provided, the task delegates output collection to an App Engine app.
The app receives task context via input_payload and returns structured outputs via submitOutput().

When app_slug is null/absent, the task uses the auto-generated output form
derived from required_output.schema.

App Engine sessions already support execution_context_type: "task" and task_id natively.
  - `app_slug` (string): Slug of the App Engine app to open for HITL output collection.
If null, the task uses the default output form (auto-generated from required_output.schema).
  - `app_session_id` (string): Session ID created by App Engine when the app is opened.
Populated at runtime when the user opens the app, not at task creation.
  - `launch_mode` (string): App Engine launch mode for the app.
  - `input_payload` (object): Additional data passed to the app on launch.
Merged with task.payload when opening the app.

### Responses

- `200`: OK.

### Example request

```bash
curl -X PATCH "https://api.sommatic.ai/work-management/task/" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "organization_id": "<organization_id>",
       "title": "<title>",
       "type": {
         "id": 0,
         "name": "<name>",
         "title": "<title>"
       },
       "priority": {
         "id": 0,
         "name": "<name>",
         "title": "<title>"
       },
       "required_output": {
         "schema": {}
       }
     }'
```

## Archive a Task.

`DELETE /work-management/task/`

Authentication: Bearer token

### Request body

ID of the task to delete/archive

- `id` (string)

### Responses

- `200`: OK.

### Example request

```bash
curl -X DELETE "https://api.sommatic.ai/work-management/task/" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "id": "<id>"
     }'
```

## Get tasks by query selector.

`GET /work-management/task/{queryselector}`

Returns task information. Use `id` with `search` for a single task, or `all` with pagination and status filters.

Authentication: Bearer token

### Parameters

- `queryselector` (path, enum, required): Filter selector Allowed values: `id`, `inbox`, `list`, `all`, `statuses`, `types`, `priorities`, `transitions`.
- `search` (query, string): Task ID when queryselector is `id`.
- `organization_id` (query, string): Filter tasks by organization ID.
- `include_status` (query, string): Comma-separated status names to include (e.g. queued,assigned,claimed).
- `exclude_status` (query, string): Comma-separated status names to exclude (e.g. deleted).
- `page` (query, integer): Page number for pagination (when queryselector is `all`).
- `pageSize` (query, integer): Number of records per page (when queryselector is `all`).

### Responses

- `200`: OK.

### Example request

```bash
curl -X GET "https://api.sommatic.ai/work-management/task/<queryselector>" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN"
```

## SSE stream for real-time task notifications

`GET /work-management/task/stream`

Server-Sent Events stream filtered by user identity. Pushes task-assigned events when tasks are created for the connected user.

Authentication: Bearer token

### Parameters

- `user[identity]` (query, string, required): User identity for filtering notifications

### Responses

- `200`: SSE event stream (Content-Type text/event-stream)

### Example request

```bash
curl -X GET "https://api.sommatic.ai/work-management/task/stream?user[identity]=<user[identity]>" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -N
```

## Apply a lifecycle transition to a Task.

`PATCH /work-management/task/transition/`

Transitions a task to a new status (assign, claim, start, complete, reject, invalidate, expire).

Authentication: Bearer token

### Request body

- `id` (string, required): Task ID
- `transition_name` (string, required):  Allowed values: `assign`, `claim`, `start`, `complete`, `reject`, `invalidate`, `expire`.
- `payload` (object): Extra data for the transition (e.g. assignee, outputs, claimed_by)

### Responses

- `200`: OK.
- `500`: Something was wrong while performing this action.

### Example request

```bash
curl -X PATCH "https://api.sommatic.ai/work-management/task/transition/" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "id": "<id>",
       "transition_name": "assign"
     }'
```
