Standard library - agent

module agent
  // module was added in PR#822

  type agent
  type options = map(data)

  // create agent descriptors
  def localAgent : string -> t
  def localPersistentAgent : string -> t
  def remoteAgent : string -> string -> string -> t
  def remotePersistentAgent : string -> string -> string -> t

  // change identity
  case currentToken
  case thisToken(data)
  type credentials = currentToken | thisToken
  def asUser : string -> credentials -> t -> t

  // call agents
  def call : t -> options -> data -> result(string, data)
module end

Create agent descriptors

  def localAgent : string -> t
  def localPersistentAgent : string -> t
  def remoteAgent : string -> string -> string -> t
  def remotePersistentAgent : string -> string -> string -> t

In the following, a local agent is from the same app as the caller, and a remote agent is from a different app. You can describe the normal (one-shot) version of the agent with

Here, the name is the module name, and baseURL designates the server where the agent can be found (see the env module for functions helping to construct base URLs). The app is the name of the application from which the agent is taken. These agents are “one-shot” agents, i.e. when they are called, a session is created for running the request, and after retrieving the result the session is shut down again. Because of this, the spreadsheet of this type of agent cannot store data across requests (you need to use the db or another external storage system for saving data).

There are also persistent agents:

This type of agent does not shut the session down. Each time the agent is invoked, the request is fed into the same spreadsheet.

Change identity

  case currentToken
  case thisToken(data)
  type credentials = currentToken | thisToken

  def asUser : string -> credentials -> t -> t

The function asUser(userName, userCreds, descriptor) allows it to change the identity of the caller to userName and to pass credentials as proof.

Call agents

After creating the descriptor, you can call the agent with:

call(descriptor, opts, param)

Here, opts is a map with options (often empty, {}). The param is the single parameter the agent needs as input (often a map with a couple of fields).

Options:

Examples

One-shot local agent:

agent.call(agent.localAgent("myTest"), {}, {x:"hello"})
 |> result.get

Call the local agent as somebody else (where token is the OAuth token of myFriend):