-
Notifications
You must be signed in to change notification settings - Fork 652
feat(cli,js): implemented reflection API v2 in CLI and JS #4295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pavelgj
wants to merge
9
commits into
pj/bidi-actions
Choose a base branch
from
pj/reflection-api-v2
base: pj/bidi-actions
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2d49663
feat: implemented reflection API v2
pavelgj 5b944fa
reflection api protocol cleanup
pavelgj 5390d77
fmt
pavelgj 03c7b12
fix
pavelgj f615c87
fix build
pavelgj 210745a
Update genkit-tools/common/src/manager/manager-v2.ts
pavelgj a093772
feat: Add null checks and non-null assertions for runtime IDs and upd…
pavelgj 5038523
fmt
pavelgj d6e76c1
Merge branch 'pj/bidi-actions' into pj/reflection-api-v2
pavelgj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| # Genkit Reflection Protocol V2 (WebSocket) | ||
|
|
||
| This document outlines the design for the V2 Reflection API, which uses WebSockets for bidirectional communication between the Genkit CLI (Runtime Manager) and Genkit Runtimes (User Applications). | ||
|
|
||
| ## Overview | ||
|
|
||
| In V2, the connection direction is reversed compared to V1: | ||
| - **Server**: The Genkit CLI (`RuntimeManagerV2`) starts a WebSocket server. | ||
| - **Client**: The Genkit Runtime connects to the CLI's WebSocket server. | ||
|
|
||
| This architecture allows the CLI to easily manage multiple runtimes (e.g., for multi-service projects) and eliminates the need for runtimes to manage their own HTTP servers and ports for reflection. | ||
|
|
||
| ## Transport | ||
|
|
||
| | Feature | Specification | | ||
| | :--- | :--- | | ||
| | **Protocol** | WebSocket | | ||
| | **Data Format** | JSON | | ||
| | **Message Structure** | JSON-RPC 2.0 (modified for streaming) | | ||
|
|
||
| ## Message Format | ||
|
|
||
| All messages follow the JSON-RPC 2.0 specification. | ||
|
|
||
| ### Request | ||
| ```json | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "method": "methodName", | ||
| "params": { ... }, | ||
| "id": 1 | ||
| } | ||
| ``` | ||
| *Note: The `id` is generated by the sender (Manager). It can be a number (auto-incrementing) or a string (UUID). It must be unique for the pending request within the WebSocket session.* | ||
|
|
||
| ### Response (Success) | ||
| ```json | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "result": { ... }, | ||
| "id": 1 | ||
| } | ||
| ``` | ||
|
|
||
| ### Response (Error) | ||
| ```json | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "error": { | ||
| "code": -32000, | ||
| "message": "Error message", | ||
| "data": { | ||
| "code": 13, | ||
| "message": "Error message", | ||
| "details": { | ||
| "traceId": "...", | ||
| "stack": "..." | ||
| } | ||
| } | ||
| }, | ||
| "id": 1 | ||
| } | ||
| ``` | ||
|
|
||
| The `data` field contains a `Status` object (matching V1 API) with: | ||
| - **`code`**: Genkit canonical status code (e.g., 13 for INTERNAL, 3 for INVALID_ARGUMENT). | ||
| - **`message`**: The error message. | ||
| - **`details`**: Additional context, including `traceId` and `stack` trace. | ||
|
|
||
| ### Notification | ||
| A request without an `id`. | ||
| ```json | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "method": "methodName", | ||
| "params": { ... } | ||
| } | ||
| ``` | ||
|
|
||
| ## Streaming Extension | ||
|
|
||
| JSON-RPC 2.0 does not natively support streaming. We extend it by using Notifications from the Runtime to the Manager associated with a specific Request ID. | ||
|
|
||
| | Message Type | Method | Direction | Description | | ||
| | :--- | :--- | :--- | :--- | | ||
| | **Stream Chunk** | `streamChunk` | Runtime -> Manager | Sent by the Runtime during a streaming `runAction` request. | | ||
| | **State Update** | `runActionState` | Runtime -> Manager | Sent by the Runtime to provide status updates (e.g., trace ID) before the result. | | ||
|
|
||
| ### Stream Chunk Notification | ||
| ```json | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "method": "streamChunk", | ||
| "params": { | ||
| "requestId": 1, | ||
| "chunk": { ... } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Run Action State Notification | ||
| ```json | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "method": "runActionState", | ||
| "params": { | ||
| "requestId": 1, | ||
| "state": { "traceId": "..." } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Protocol Methods Summary | ||
|
|
||
| | Method | Direction | Type | Description | | ||
| | :--- | :--- | :--- | :--- | | ||
| | **`register`** | Runtime -> Manager | Request | Registers the runtime with the Manager. | | ||
| | **`configure`** | Manager -> Runtime | Notification | Pushes configuration updates to the Runtime. | | ||
| | **`listActions`** | Manager -> Runtime | Request | Retrieves the list of available actions. | | ||
| | **`listValues`** | Manager -> Runtime | Request | Retrieves the list of values (prompts, schemas, etc.). | | ||
| | **`runAction`** | Manager -> Runtime | Request | Executes an action. | | ||
| | **`cancelAction`** | Manager -> Runtime | Request | Cancels a running action. | | ||
|
|
||
| ## Detailed API | ||
|
|
||
| ### 1. Registration | ||
| **Direction:** Runtime -> Manager | ||
| **Type:** Request | ||
|
|
||
| **Parameters:** | ||
| | Field | Type | Description | | ||
| | :--- | :--- | :--- | | ||
| | `id` | `string` | Unique Runtime ID. | | ||
| | `pid` | `number` | Process ID. | | ||
| | `name` | `string` | App name (optional). | | ||
| | `genkitVersion` | `string` | e.g., "0.9.0". | | ||
| | `reflectionApiSpecVersion` | `number` | Protocol version. | | ||
| | `envs` | `string[]` | Configured environments (optional). | | ||
|
|
||
| **Result:** `void` | ||
|
|
||
| ### 2. Configuration | ||
| **Direction:** Manager -> Runtime | ||
| **Type:** Notification | ||
|
|
||
| **Parameters:** | ||
| | Field | Type | Description | | ||
| | :--- | :--- | :--- | | ||
| | `telemetryServerUrl` | `string` | URL of the telemetry server (optional). | | ||
|
|
||
| ### 3. List Actions | ||
| **Direction:** Manager -> Runtime | ||
| **Type:** Request | ||
|
|
||
| **Parameters:** `void` | ||
|
|
||
| **Result:** | ||
| | Type | Description | | ||
| | :--- | :--- | | ||
| | `Record<string, Action>` | Map of action keys to Action definitions. (Same schema as V1 `/api/actions`) | | ||
|
|
||
| ### 4. List Values | ||
| **Direction:** Manager -> Runtime | ||
| **Type:** Request | ||
|
|
||
| **Parameters:** | ||
| | Field | Type | Description | | ||
| | :--- | :--- | :--- | | ||
| | `type` | `string` | The type of value to list (e.g., "model", "prompt", "schema"). | | ||
|
|
||
| **Result:** | ||
| | Type | Description | | ||
| | :--- | :--- | | ||
| | `Record<string, any>` | Map of value keys to value definitions. | | ||
|
|
||
| ### 5. Run Action | ||
| **Direction:** Manager -> Runtime | ||
| **Type:** Request | ||
|
|
||
| **Parameters:** | ||
| | Field | Type | Description | | ||
| | :--- | :--- | :--- | | ||
| | `key` | `string` | Action key (e.g., "/flow/myFlow"). | | ||
| | `input` | `any` | Input payload. | | ||
| | `context` | `any` | Context data (optional). | | ||
| | `telemetryLabels` | `Record<string, string>` | Telemetry labels (optional). | | ||
| | `stream` | `boolean` | Whether to stream results. | | ||
| | `streamInput` | `boolean` | Whether to stream input (for bidi actions). | | ||
|
|
||
| **Result (Non-Streaming):** | ||
| | Field | Type | Description | | ||
| | :--- | :--- | :--- | | ||
| | `result` | `any` | The return value. | | ||
| | `telemetry` | `object` | Telemetry metadata (e.g., `{ traceId: string }`). | | ||
|
|
||
| **Streaming Flow:** | ||
| 1. Runtime sends optional `runActionState` notifications. | ||
| 2. Runtime sends `streamChunk` notifications. | ||
| 3. Runtime sends final response with `result` (same structure as non-streaming). | ||
|
|
||
| **Bidirectional Streaming Flow (if `streamInput: true`):** | ||
| 1. Manager sends `streamInputChunk` notifications. | ||
| 2. Manager sends `endStreamInput` notification. | ||
| 3. Runtime behaves as per Streaming Flow. | ||
|
|
||
| ### 6. Cancel Action | ||
| **Direction:** Manager -> Runtime | ||
| **Type:** Request | ||
|
|
||
| **Parameters:** | ||
| | Field | Type | Description | | ||
| | :--- | :--- | :--- | | ||
| | `traceId` | `string` | The trace ID of the action to cancel. | | ||
|
|
||
| **Result:** | ||
| | Field | Type | Description | | ||
| | :--- | :--- | :--- | | ||
| | `message` | `string` | Confirmation message. | | ||
|
|
||
| ## Health Checks | ||
|
|
||
| | Check Type | Description | | ||
| | :--- | :--- | | ||
| | **Connection State** | The WebSocket connection state itself serves as a basic health check. | | ||
| | **Heartbeats** | Standard WebSocket Ping/Pong frames should be used to maintain the connection and detect timeouts. | | ||
|
|
||
| ## Compatibility | ||
|
|
||
| | Version | Architecture | | ||
| | :--- | :--- | | ||
| | **V1** | HTTP Server on Runtime, Polling/Request from CLI. | | ||
| | **V2** | WebSocket Server on CLI, Persistent Connection from Runtime. | | ||
|
|
||
| The CLI will determine which mode to use based on configuration (e.g., `--experimental-reflection-v2`). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.