-
Notifications
You must be signed in to change notification settings - Fork 710
feat(go): add DefineSessionFlow and DefineSessionFlowFromPrompt
#4462
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
apascal07
wants to merge
45
commits into
ap/go-bidi
Choose a base branch
from
ap/go-session-flow
base: ap/go-bidi
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
45 commits
Select commit
Hold shift + click to select a range
1382035
added `SessionFlow` and related
apascal07 fe91d76
Update main.go
apascal07 ad323d2
Update main.go
apascal07 3913771
moved files
apascal07 15880f5
added `DefineSessionFlowFromPrompt`
apascal07 e94bca1
removed stream type param
apascal07 a77fa33
updates
apascal07 e83af30
cleaned up API naming and behavior
apascal07 db37102
Update action.go
apascal07 f4c4ec1
added stream capturing to output
apascal07 08b09e4
stream out interrupt chunks
apascal07 c2f55ab
Update genkit.go
apascal07 30b4afd
Update agent_flow.go
apascal07 22be814
removed get snapshot action
apascal07 a009a1b
tagged prompt messages and excluded them
apascal07 ea742d9
fixed PromptInput -> InputVariables
apascal07 2ae4bb8
added AgentFlowResult to output final artifacts
apascal07 787f61a
Update agent_flow.go
apascal07 d1281d5
removed turn index from snapshot
apascal07 d6cae44
exposed InputCh and TurnIndex on AgentSession
apascal07 10bbd03
improvements to API
apascal07 a687eb6
minor fixes
apascal07 7c535c4
Update session.go
apascal07 26f8f7d
added shared schemas for agent types
apascal07 d9c335a
Update typing.py
apascal07 fb7f254
various renames
apascal07 971b668
helper for input variables conversion
apascal07 3491761
DefinePromptAgent takes in prompt name instead of resolved prompt
apascal07 b46acce
fixed interrupts streaming
apascal07 6676882
Update genkit.go
apascal07 8d99639
added `SetMessages`
apascal07 3cbec28
added `AgentSession.Result()`
apascal07 6ac5510
moved from `ai/x` to `ai/exp`
apascal07 ac39ef9
Update agent.go
apascal07 5a671bb
added `AgentFlow.Run()` and `AgentFlow.RunText()`
apascal07 49a19eb
dedupe consecutive identical snapshots
apascal07 aca712c
renamed agent flow et al to session flow
apascal07 9c0d629
Update genkit-schema.json
apascal07 668fc31
renamed files
apascal07 000b300
Update agent.ts
apascal07 d82c1a8
Update schemas.config
apascal07 8e0a8d6
Merge ap/go-bidi and refactor session flow for new type params
apascal07 b6f5dc0
refactored order of type params
apascal07 c298aca
Update action.go
apascal07 aabe1ad
Update session_flow_test.go
apascal07 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,120 @@ | ||
| /** | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { z } from 'zod'; | ||
| import { MessageSchema, ModelResponseChunkSchema } from './model'; | ||
| import { PartSchema } from './parts'; | ||
|
|
||
| /** | ||
| * Zod schema for an artifact produced during a session. | ||
| */ | ||
| export const ArtifactSchema = z.object({ | ||
| /** Name identifies the artifact (e.g., "generated_code.go", "diagram.png"). */ | ||
| name: z.string().optional(), | ||
| /** Parts contains the artifact content (text, media, etc.). */ | ||
| parts: z.array(PartSchema), | ||
| /** Metadata contains additional artifact-specific data. */ | ||
| metadata: z.record(z.any()).optional(), | ||
| }); | ||
| export type Artifact = z.infer<typeof ArtifactSchema>; | ||
|
|
||
| /** | ||
| * Zod schema for snapshot event. | ||
| */ | ||
| export const SnapshotEventSchema = z.enum(['turnEnd', 'invocationEnd']); | ||
| export type SnapshotEvent = z.infer<typeof SnapshotEventSchema>; | ||
|
|
||
| /** | ||
| * Zod schema for session state. | ||
| */ | ||
| export const SessionStateSchema = z.object({ | ||
| /** Conversation history (user/model exchanges). */ | ||
| messages: z.array(MessageSchema).optional(), | ||
| /** User-defined state associated with this conversation. */ | ||
| custom: z.any().optional(), | ||
| /** Named collections of parts produced during the conversation. */ | ||
| artifacts: z.array(ArtifactSchema).optional(), | ||
| /** Input used for session flows that require input variables. */ | ||
| inputVariables: z.any().optional(), | ||
| }); | ||
| export type SessionState = z.infer<typeof SessionStateSchema>; | ||
|
|
||
| /** | ||
| * Zod schema for session flow input (per-turn). | ||
| */ | ||
| export const SessionFlowInputSchema = z.object({ | ||
| /** User's input messages for this turn. */ | ||
| messages: z.array(MessageSchema).optional(), | ||
| /** Tool request parts to re-execute interrupted tools. */ | ||
| toolRestarts: z.array(PartSchema).optional(), | ||
| }); | ||
| export type SessionFlowInput = z.infer<typeof SessionFlowInputSchema>; | ||
|
|
||
| /** | ||
| * Zod schema for session flow initialization. | ||
| */ | ||
| export const SessionFlowInitSchema = z.object({ | ||
| /** Loads state from a persisted snapshot. Mutually exclusive with state. */ | ||
| snapshotId: z.string().optional(), | ||
| /** Direct state for the invocation. Mutually exclusive with snapshotId. */ | ||
| state: SessionStateSchema.optional(), | ||
| }); | ||
| export type SessionFlowInit = z.infer<typeof SessionFlowInitSchema>; | ||
|
|
||
| /** | ||
| * Zod schema for session flow result. | ||
| */ | ||
| export const SessionFlowResultSchema = z.object({ | ||
| /** Last model response message from the conversation. */ | ||
| message: MessageSchema.optional(), | ||
| /** Artifacts produced during the session. */ | ||
| artifacts: z.array(ArtifactSchema).optional(), | ||
| }); | ||
| export type SessionFlowResult = z.infer<typeof SessionFlowResultSchema>; | ||
|
|
||
| /** | ||
| * Zod schema for session flow output. | ||
| */ | ||
| export const SessionFlowOutputSchema = z.object({ | ||
| /** ID of the snapshot created at the end of this invocation. */ | ||
| snapshotId: z.string().optional(), | ||
| /** Final conversation state (only when client-managed). */ | ||
| state: SessionStateSchema.optional(), | ||
| /** Last model response message from the conversation. */ | ||
| message: MessageSchema.optional(), | ||
| /** Artifacts produced during the session. */ | ||
| artifacts: z.array(ArtifactSchema).optional(), | ||
| }); | ||
| export type SessionFlowOutput = z.infer<typeof SessionFlowOutputSchema>; | ||
|
|
||
| /** | ||
| * Zod schema for session flow stream chunk. | ||
| */ | ||
| export const SessionFlowStreamChunkSchema = z.object({ | ||
| /** Generation tokens from the model. */ | ||
| modelChunk: ModelResponseChunkSchema.optional(), | ||
| /** User-defined structured status information. */ | ||
| status: z.any().optional(), | ||
| /** A newly produced artifact. */ | ||
| artifact: ArtifactSchema.optional(), | ||
| /** ID of a snapshot that was just persisted. */ | ||
| snapshotId: z.string().optional(), | ||
| /** Signals that the session flow has finished processing the current input. */ | ||
| endTurn: z.boolean().optional(), | ||
| }); | ||
| export type SessionFlowStreamChunk = z.infer< | ||
| typeof SessionFlowStreamChunkSchema | ||
| >; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // This file was generated by jsonschemagen. DO NOT EDIT. | ||
|
|
||
| package exp | ||
|
|
||
| import ( | ||
| "github.com/firebase/genkit/go/ai" | ||
| ) | ||
|
|
||
| // SessionFlowInit is the input for starting an session flow invocation. | ||
| // Provide either SnapshotID (to load from store) or State (direct state). | ||
| type SessionFlowInit[State any] struct { | ||
| // SnapshotID loads state from a persisted snapshot. | ||
| // Mutually exclusive with State. | ||
| SnapshotID string `json:"snapshotId,omitempty"` | ||
| // State provides direct state for the invocation. | ||
| // Mutually exclusive with SnapshotID. | ||
| State *SessionState[State] `json:"state,omitempty"` | ||
| } | ||
|
|
||
| // SessionFlowInput is the input sent to an session flow during a conversation turn. | ||
| type SessionFlowInput struct { | ||
| // Messages contains the user's input for this turn. | ||
| Messages []*ai.Message `json:"messages,omitempty"` | ||
| // ToolRestarts contains tool request parts to re-execute interrupted tools. | ||
| // Use [ai.ToolDef.RestartWith] to create these parts from an interrupted | ||
| // tool request. When set, the generate call resumes with these restarts | ||
| // instead of treating Messages as tool responses. | ||
| ToolRestarts []*ai.Part `json:"toolRestarts,omitempty"` | ||
| } | ||
|
|
||
| // SessionFlowOutput is the output when an session flow invocation completes. | ||
| // It wraps SessionFlowResult with framework-managed fields. | ||
| type SessionFlowOutput[State any] struct { | ||
| // Artifacts contains artifacts produced during the session. | ||
| Artifacts []*Artifact `json:"artifacts,omitempty"` | ||
| // Message is the last model response message from the conversation. | ||
| Message *ai.Message `json:"message,omitempty"` | ||
| // SnapshotID is the ID of the snapshot created at the end of this invocation. | ||
| // Empty if no snapshot was created (callback returned false or no store configured). | ||
| SnapshotID string `json:"snapshotId,omitempty"` | ||
| // State contains the final conversation state. | ||
| // Only populated when state is client-managed (no store configured). | ||
| State *SessionState[State] `json:"state,omitempty"` | ||
| } | ||
|
|
||
| // SessionFlowResult is the return value from an SessionFlowFunc. | ||
| // It contains the user-specified outputs of the agent invocation. | ||
| type SessionFlowResult struct { | ||
| // Artifacts contains artifacts produced during the session. | ||
| Artifacts []*Artifact `json:"artifacts,omitempty"` | ||
| // Message is the last model response message from the conversation. | ||
| Message *ai.Message `json:"message,omitempty"` | ||
| } | ||
|
|
||
| // SessionFlowStreamChunk represents a single item in the session flow's output stream. | ||
| // Multiple fields can be populated in a single chunk. | ||
| type SessionFlowStreamChunk[Stream any] struct { | ||
| // Artifact contains a newly produced artifact. | ||
| Artifact *Artifact `json:"artifact,omitempty"` | ||
| // EndTurn signals that the session flow has finished processing the current input. | ||
| // When true, the client should stop iterating and may send the next input. | ||
| EndTurn bool `json:"endTurn,omitempty"` | ||
| // ModelChunk contains generation tokens from the model. | ||
| ModelChunk *ai.ModelResponseChunk `json:"modelChunk,omitempty"` | ||
| // SnapshotID contains the ID of a snapshot that was just persisted. | ||
| SnapshotID string `json:"snapshotId,omitempty"` | ||
| // Status contains user-defined structured status information. | ||
| // The Stream type parameter defines the shape of this data. | ||
| Status Stream `json:"status,omitempty"` | ||
| } | ||
|
|
||
| // Artifact represents a named collection of parts produced during a session. | ||
| // Examples: generated files, images, code snippets, diagrams, etc. | ||
| type Artifact struct { | ||
| // Metadata contains additional artifact-specific data. | ||
| Metadata map[string]any `json:"metadata,omitempty"` | ||
| // Name identifies the artifact (e.g., "generated_code.go", "diagram.png"). | ||
| Name string `json:"name,omitempty"` | ||
| // Parts contains the artifact content (text, media, etc.). | ||
| Parts []*ai.Part `json:"parts"` | ||
| } | ||
|
|
||
| // SessionState is the portable conversation state that flows between client | ||
| // and server. It contains only the data needed for conversation continuity. | ||
| type SessionState[State any] struct { | ||
| // Artifacts are named collections of parts produced during the conversation. | ||
| Artifacts []*Artifact `json:"artifacts,omitempty"` | ||
| // Custom is the user-defined state associated with this conversation. | ||
| Custom State `json:"custom,omitempty"` | ||
| // InputVariables is the input used for session flows that require input variables | ||
| // (e.g. prompt-backed session flows). | ||
| InputVariables any `json:"inputVariables,omitempty"` | ||
| // Messages is the conversation history (user/model exchanges). | ||
| // Does NOT include prompt-rendered messages — those are rendered fresh each turn. | ||
| Messages []*ai.Message `json:"messages,omitempty"` | ||
| } | ||
|
|
||
| // SnapshotEvent identifies what triggered a snapshot. | ||
| type SnapshotEvent string | ||
|
|
||
| const ( | ||
| // TurnEnd indicates the snapshot was triggered at the end of a turn. | ||
| SnapshotEventTurnEnd SnapshotEvent = "turnEnd" | ||
| // InvocationEnd indicates the snapshot was triggered at the end of the invocation. | ||
| SnapshotEventInvocationEnd SnapshotEvent = "invocationEnd" | ||
| ) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this need to be in the shared schemas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do have existing enums defined in shared schemas, probably doesn't hurt, but I don't know if we have a specific need for it in Dev UI at the moment. Do you want me to take it out?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We define things here only if we expect things to be consistent across languages. If it's something that can be language specific... then I'd say leave it out.