Skip to content

Commit 7ef9ffa

Browse files
authored
Assistant: disable tools in invalid contexts (#8603)
### Summary - addresses #8078 - inspectVariables: now filtered out when no variables in session - getPlot: now filtered out if no session or no current plot - getTableSummary: now filtered out if no session and no variables in session - projectTreeTool: filtering already covered in #8264 ### Release Notes #### New Features - Assistant: tools are now disabled if the context doesn't include the info needed for the tool (#8078) ### QA Notes @:assistant #### test cases | tool | enabled when | disabled when | |--------|--------|--------| | inspectVariables | session(s) exist and at least 1 variable is defined | no sessions; or sessions exist but no variables defined | | getPlot | session(s) exist and there is a plot currently visible | no sessions; or sessions exist but no plot visible | | getTableSummary | for now, only when Python session(s) exist (this tool is not yet implemented for R #8343) and at least 1 variable is defined | for now, if no sessions or if only R sessions exist; or Python sessions exist but no variables defined | #### how to check - try to invoke the disabled tool via chat and notice that the tool cannot be called; or - check the Output pane for Assistant with log level Debug or Trace for the message with `[tools] Available tools for participant` - if the tool is not in the list, it is disabled/filtered out
1 parent 5ee1b99 commit 7ef9ffa

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

extensions/positron-assistant/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@
357357
"modelDescription": "View the current active plot if one exists. Don't invoke this tool if there are no plots in the session.",
358358
"canBeReferencedInPrompt": false,
359359
"tags": [
360-
"positron-assistant"
360+
"positron-assistant",
361+
"requires-session"
361362
]
362363
},
363364
{
@@ -390,7 +391,8 @@
390391
]
391392
},
392393
"tags": [
393-
"positron-assistant"
394+
"positron-assistant",
395+
"requires-session"
394396
]
395397
},
396398
{

extensions/positron-assistant/src/participants.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,26 @@ abstract class PositronAssistantParticipant implements IPositronAssistantPartici
188188
const positronContext = await positron.ai.getPositronChatContext(request);
189189
log.debug(`[context] Positron context for request ${request.id}:\n${JSON.stringify(positronContext, null, 2)}`);
190190

191-
// Build a list of languages for which we have active sessions.
192-
//
193191
// See IChatRuntimeSessionContext for the structure of the active
194192
// session context objects
195193
const activeSessions: Set<string> = new Set();
194+
let hasVariables = false;
196195
let hasConsoleSessions = false;
197196
for (const reference of request.references) {
198197
const value = reference.value as any;
198+
199+
// Build a list of languages for which we have active sessions.
199200
if (value.activeSession) {
200201
activeSessions.add(value.activeSession.languageId);
201202
if (value.activeSession.mode === positron.LanguageRuntimeSessionMode.Console) {
202203
hasConsoleSessions = true;
203204
}
204205
}
206+
207+
// Check if there are variables defined in the session.
208+
if (value.variables && value.variables.length > 0) {
209+
hasVariables = true;
210+
}
205211
}
206212

207213
// List of tools for use by the language model.
@@ -274,9 +280,16 @@ abstract class PositronAssistantParticipant implements IPositronAssistantPartici
274280
return inChatPane && (isEditMode || isAgentMode);
275281
// Only include the getTableSummary tool for Python sessions until supported in R
276282
case PositronAssistantToolName.GetTableSummary:
277-
// TODO: Remove this restriction when the tool is supported in R https://github.com/posit-dev/positron/issues/8343
283+
// TODO: Remove the python-specific restriction when the tool is supported in R https://github.com/posit-dev/positron/issues/8343
278284
// The logic above with TOOL_TAG_REQUIRES_ACTIVE_SESSION will handle checking for active sessions once this is removed.
279-
return activeSessions.has('python');
285+
// We'll still want to check that variables are defined.
286+
return activeSessions.has('python') && hasVariables;
287+
// Only include the getPlot tool if there is a plot available.
288+
case PositronAssistantToolName.GetPlot:
289+
return positronContext.plots?.hasPlots === true;
290+
// Only include the inspectVariables tool if there are variables defined.
291+
case PositronAssistantToolName.InspectVariables:
292+
return hasVariables;
280293
// Otherwise, include the tool if it is tagged for use with Positron Assistant.
281294
// Allow all tools in Agent mode.
282295
default:

0 commit comments

Comments
 (0)