getHostFunctionContext

API Reference for getHostFunctionContext.

getHostFunctionContext()

Get the context for the currently executing host function.

import { getHostFunctionContext, run } from 'run';

await run({
  source: 'return await users.find("user_1");',
  hostFunctions: {
    users: {
      find: async (id: string) => {
        const { abortSignal } = getHostFunctionContext();
        return database.users.find(id, { signal: abortSignal });
      },
    },
  },
});

Import

import { getHostFunctionContext } from 'run';

API Signature

Parameters

[]

Returns

[
  {
    "name": "context",
    "type": "HostFunctionContext",
    "description": "The context for the active host function call.",
    "properties": [
      {
        "type": "HostFunctionContext",
        "parameters": [
          {
            "name": "abortSignal",
            "type": "AbortSignal",
            "description": "A signal aborted when the run is aborted, times out, or fails."
          },
          {
            "name": "invocationId",
            "type": "string",
            "description": "The stable identifier for the current run attempt."
          },
          {
            "name": "logicalRunId",
            "type": "string",
            "description": "The stable identifier for the logical run across replay attempts."
          },
          {
            "name": "requestId",
            "type": "string",
            "description": "The stable identifier for this host function request in the current attempt."
          },
          {
            "name": "requestIndex",
            "type": "number",
            "description": "The one-based host function request order in the current attempt."
          },
          {
            "name": "hostFunctionName",
            "type": "string",
            "description": "The fully qualified host function path, for example tools.search."
          },
          {
            "name": "interrupt",
            "type": "(payload: unknown) => never",
            "description": "Interrupts the host function and suspends the run."
          },
          {
            "name": "resume",
            "type": "HostFunctionResumeContext",
            "isOptional": true,
            "description": "Resume data available when an interrupted host function is invoked again.",
            "properties": [
              {
                "type": "HostFunctionResumeContext",
                "parameters": [
                  {
                    "name": "interruptionId",
                    "type": "string",
                    "description": "The ID of the interruption being resolved."
                  },
                  {
                    "name": "payload",
                    "type": "unknown",
                    "description": "The original interruption payload."
                  },
                  {
                    "name": "resolution",
                    "type": "unknown",
                    "description": "The application-defined resolution value."
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]