-
Notifications
You must be signed in to change notification settings - Fork 120
feat(chat): enable agent to ask interactive multiple-choice clarifica… #504
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
Klabundo
wants to merge
7
commits into
main
Choose a base branch
from
feature/agent-clarification-questions
base: main
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
7 commits
Select commit
Hold shift + click to select a range
1a6107d
feat(chat): enable agent to ask interactive multiple-choice clarifica…
Klabundo 7b92dc7
Merge branch 'main' into feature/agent-clarification-questions
Klabundo 45e5576
Update src/components/chat/toolOutputRenderModel.ts
Klabundo 215434a
style: format AgentManager and pendingQuestionStore with biome
Klabundo 4b13fdd
fix(test): remove explicit any in askQuestion test
Klabundo e693ccc
Merge branch 'main' into feature/agent-clarification-questions
Klabundo e25f333
Merge branch 'main' into feature/agent-clarification-questions
Klabundo 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
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,85 @@ | ||
| import { tool } from "@langchain/core/tools"; | ||
| import type { RunnableConfig } from "@langchain/core/runnables"; | ||
| import { z } from "zod"; | ||
| import { DEFAULT_TOOLS_CONFIG } from "./builtInToolDefaults"; | ||
| import { resolveToolAgent } from "./toolAgentContext"; | ||
| import { getPendingQuestionStore, type QuestionItem } from "../../stores/pendingQuestionStore.svelte"; | ||
| import { genUUIDv7 } from "../../utils/uuid7Validator"; | ||
| import { Logger } from "../../utils/logging"; | ||
|
|
||
| const questionItemSchema = z.object({ | ||
| id: z | ||
| .string() | ||
| .optional() | ||
| .describe("Optional unique identifier for this question (e.g. 'choice_format' or 'framework')"), | ||
| question: z.string().describe("The question text to present to the user"), | ||
| options: z.array(z.string()).min(2).describe("Selectable options for the user to choose from"), | ||
| is_multi_select: z | ||
| .boolean() | ||
| .optional() | ||
| .describe("Allow user to select multiple options. Default is false (single choice)."), | ||
| allow_custom: z.boolean().optional().describe("Allow user to type in a custom write-in response. Default is true."), | ||
| }); | ||
|
|
||
| export const askQuestionSchema = z.object({ | ||
| questions: z.array(questionItemSchema).min(1).describe("List of multiple-choice questions to ask the user"), | ||
| }); | ||
|
|
||
| export type AskQuestionInput = z.infer<typeof askQuestionSchema>; | ||
|
|
||
| function getAskQuestionToolConfig(agentId: string) { | ||
| const defaultConfig = DEFAULT_TOOLS_CONFIG.ask_question; | ||
| if (!agentId) return defaultConfig; | ||
| const agent = resolveToolAgent(agentId); | ||
| return agent?.toolsConfig?.ask_question ?? defaultConfig; | ||
| } | ||
|
|
||
| function resolveThreadIdFromConfig(config: RunnableConfig | undefined): string { | ||
| const threadId = config?.configurable?.thread_id; | ||
| if (typeof threadId === "string" && threadId.length > 0) { | ||
| return threadId; | ||
| } | ||
| return "default_thread"; | ||
| } | ||
|
|
||
| export function createAskQuestionTool(agentId = "") { | ||
| const toolConfig = getAskQuestionToolConfig(agentId); | ||
|
|
||
| return tool( | ||
| async ({ questions }: AskQuestionInput, config: RunnableConfig & { runId?: string }) => { | ||
| const threadId = resolveThreadIdFromConfig(config); | ||
| const toolCallId = (config as { toolCallId?: string })?.toolCallId ?? config?.runId ?? genUUIDv7(); | ||
|
|
||
| Logger.log("[ask_question] Prompting user with questions:", { | ||
| threadId, | ||
| toolCallId, | ||
| count: questions.length, | ||
| }); | ||
|
|
||
| const formattedQuestions: QuestionItem[] = questions.map((q, index) => ({ | ||
| id: q.id?.trim() || `q_${index + 1}`, | ||
| question: q.question, | ||
| options: q.options, | ||
| isMultiSelect: q.is_multi_select ?? false, | ||
| allowCustom: q.allow_custom ?? true, | ||
| })); | ||
|
|
||
| const store = getPendingQuestionStore(); | ||
| const answers = await store.ask(threadId, toolCallId, formattedQuestions, config?.signal); | ||
|
|
||
| return JSON.stringify( | ||
| { | ||
| status: "answered", | ||
| answers, | ||
| }, | ||
| null, | ||
| 2, | ||
| ); | ||
| }, | ||
| { | ||
| name: toolConfig?.name ?? DEFAULT_TOOLS_CONFIG.ask_question.name, | ||
| description: toolConfig?.description ?? DEFAULT_TOOLS_CONFIG.ask_question.description, | ||
| schema: askQuestionSchema, | ||
| }, | ||
| ); | ||
| } | ||
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.
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.
The schema allows repeated question IDs, including an explicit ID that can collide with a generated
q_<index>fallback. The card then uses each ID as both a Svelte list key and the key for its answer state. A call with duplicate IDs can fail to render or make separate questions share selections, preventing the user from answering them independently. Please enforce uniqueness after fallback IDs are assigned.Prompt To Fix With AI