basic-agents-server

command
v1.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

This sample demonstrates serving agents as plain HTTP endpoints.

Agents are bidirectional streaming actions, but the standard action handler also runs them one turn per request: "data" carries the user message, and the optional "init" carries the session source that spans requests.

Two agents show the two ways to hold session state:

  • chat has a session store, so the server keeps the state. Each turn persists a snapshot and the response carries sessionId and snapshotId; resume with {"init": {"sessionId": ...}}. The store also brings the companion actions, served under the agent's own path as getSnapshot, waitForSnapshot, and abort.
  • statelessChat has no store, so the client keeps the state. The response carries the whole thing; send it back as {"init": {"state": ...}}.

Failures come in two tiers. A failed turn still returns 200, reporting finishReason "failed" with a structured error and the last-good state, so a client can retry without losing the conversation. A rejected init (unknown session, or state sent to a store-backed agent) fails the request with a 4xx before any turn runs.

Run it:

go run .

Or with the Dev UI, to call the agents from a browser and read a trace of every turn at http://localhost:4000/traces:

curl -sL cli.genkit.dev | bash    # install the Genkit CLI, once
genkit start -- go run .

Or over HTTP. Start a conversation, then continue it with the sessionId the response carried:

curl -X POST http://localhost:8080/agents/chat \
  -H "Content-Type: application/json" \
  -d '{"data": {"message": {"role": "user", "content": [{"text": "My name is Alex and I am planning a trip to Japan."}]}}}'

curl -X POST http://localhost:8080/agents/chat \
  -H "Content-Type: application/json" \
  -d '{"data": {"message": {"role": "user", "content": [{"text": "What is my name?"}]}}, "init": {"sessionId": "SESSION_ID"}}'

Stream a turn's chunks and lifecycle events as server-sent events:

curl -N -X POST 'http://localhost:8080/agents/chat?stream=true' \
  -H "Content-Type: application/json" \
  -d '{"data": {"message": {"role": "user", "content": [{"text": "Suggest three day trips from Tokyo."}]}}}'

Or detach, which returns immediately with finishReason "detached" and a pending snapshotId while the turn keeps running. Read where it stands with getSnapshot, block until it settles with waitForSnapshot, or abort it:

curl -X POST http://localhost:8080/agents/chat \
  -H "Content-Type: application/json" \
  -d '{"data": {"message": {"role": "user", "content": [{"text": "Plan a two-week Japan itinerary."}]}, "detach": true}}'

curl -X POST http://localhost:8080/agents/chat/getSnapshot \
  -H "Content-Type: application/json" \
  -d '{"data": {"snapshotId": "SNAPSHOT_ID"}}'

waitForSnapshot takes the same payload but answers only once the turn has settled, so one request replaces a polling loop:

curl -X POST http://localhost:8080/agents/chat/waitForSnapshot \
  -H "Content-Type: application/json" \
  -d '{"data": {"snapshotId": "SNAPSHOT_ID"}}'

Or stop the background turn instead, which finalizes it as "aborted":

curl -X POST http://localhost:8080/agents/chat/abort \
  -H "Content-Type: application/json" \
  -d '{"data": {"snapshotId": "SNAPSHOT_ID"}}'

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL