Files
dexhorthy a3d286a7a7 Add state preservation support to FunctionCallSpec and HumanContactSpec
What I did
-----------

- Enabled state preservation in `FunctionCallSpec` and `HumanContactSpec`, allowing applications to maintain context across request lifecycles without storing state.
- Updated documentation and examples to demonstrate the new state preservation feature.

How I did it
-----------

- Added an optional `state` field to the models.
- Created new examples and updated existing ones to showcase state usage.
- Wrote tests to verify state is preserved across requests.
- Refactored session management in `async_cloud.py` for simplicity.
- [x] I have ensured `make check test` passes

How to verify it
-----------

- Run `make check test` to ensure all tests pass.
- Review the `app-statehooks.py` example for state preservation without webhooks.
- Check the updated documentation under the **State Preservation** section.

Description for the changelog
--------------

- Add state preservation support to `FunctionCallSpec` and `HumanContactSpec`, enabling applications to maintain context across requests without storing state.
2024-12-18 13:52:00 -08:00
..

Human Layer Curl Example

This example demonstrates how to use Human Layer's API with simple curl commands to create and check function call approvals.

Prerequisites

  • curl
  • jq (for JSON parsing)
  • Valid HUMANLAYER_API_KEY environment variable set with your API key from Human Layer

Basics

to create an approval request, run a curl like:

curl -X POST https://api.humanlayer.dev/humanlayer/v1/function_calls \
-H "Authorization: Bearer ${HUMANLAYER_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
  "run_id": "curl-example",
  "call_id": "1234-5678-...",
  "spec": {
    "fn": "send_email",
    "kwargs": {
      "to": "user@example.com",
      "subject": "Hello",
      "body": "This is a test email"
    }
  }
}'

Usage w/ Makefile

Test Large Context Size Limit

Test the context size limit by sending a large payload:

export HUMANLAYER_API_KEY=<your-api-key>
make test-large-context

This will send a function call with a large array in the state field to test the context size limits.

1. Create an Approval Request

Create a new function call approval request:

export HUMANLAYER_API_KEY=<your-api-key>
make create-approval

This will create a new approval request with a randomly generated UUID. The output will look like:

{
  "run_id": "curl-example",
  "call_id": "1234-5678-...",
  "spec": {
    "fn": "send_email",
    "kwargs": {
      "to": "user@example.com",
      "subject": "Hello",
      "body": "This is a test email"
    }
  },
  "status": {
    "approved": false,
    "comment": null
  }
}

2. Check Approval Status

Check the status of your approval request, using the call_id from the approval request:

make check-approval random_id=1234-5678-...

If not yet approved, you'll see:

{
  "run_id": "curl-example",
  "call_id": "1234-5678-...",
  "spec": {
    "fn": "send_email",
    "kwargs": {
      "to": "user@example.com",
      "subject": "Hello",
      "body": "This is a test email"
    }
  },
  "status": {
    "approved": false,
    "comment": null
  }
}

Once approved:

{
  "run_id": "curl-example",
  "call_id": "1234-5678-...",
  "spec": {
    "fn": "send_email",
    "kwargs": {
      "to": "user@example.com",
      "subject": "Hello",
      "body": "This is a test email"
    }
  },
  "status": {
    "approved": true,
    "comment": "Approved by reviewer"
  }
}

3. Run Complete Flow

To create a request and wait for approval:

make run-agent

This will:

  1. Create an approval request
  2. Poll for approval status every 3 seconds
  3. Output the approval comment once approved

Example output while waiting:

waiting for approval
waiting for approval
waiting for approval

Once approved:

approval granted with comment "Approved by reviewer"