Skip to content

Commit e6fe52a

Browse files
committed
cleaning
1 parent 6e0d98b commit e6fe52a

File tree

3 files changed

+43
-70
lines changed

3 files changed

+43
-70
lines changed

frontend/src/lib/components/copilot/chat/flow/core.ts

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
getLangContext,
1313
SUPPORTED_CHAT_SCRIPT_LANGUAGES
1414
} from '../script/core'
15-
import { createSearchHubScriptsTool, createToolDef, type Tool, executeTestRun } from '../shared'
15+
import { createSearchHubScriptsTool, createToolDef, type Tool, executeTestRun, buildSchemaForTool } from '../shared'
1616
import type { ExtendedOpenFlow } from '$lib/components/flows/types'
1717
import { copilotSessionModel } from '$lib/stores'
1818
import { get } from 'svelte/store'
@@ -354,7 +354,8 @@ const testRunFlowToolDef = createToolDef(
354354
const testRunStepSchema = z.object({
355355
stepId: z.string().describe('The id of the step to test'),
356356
args: z
357-
.record(z.any())
357+
.object({})
358+
.nullable()
358359
.optional()
359360
.describe('Arguments to pass to the step (optional, uses default step inputs if not provided)')
360361
})
@@ -592,32 +593,17 @@ export const flowTools: Tool<FlowAIChatHelpers>[] = [
592593
})
593594
},
594595
setSchema: async function(helpers: FlowAIChatHelpers) {
595-
try {
596-
if (this.def?.function?.parameters) {
597-
const flowInputsSchema = await helpers.getFlowInputsSchema()
598-
this.def.function.parameters = { ...flowInputsSchema, additionalProperties: false }
599-
// OPEN AI models don't support strict mode well with schema with complex properties, so we disable it
600-
const model = get(copilotSessionModel)?.provider
601-
if (model === 'openai' || model === 'azure_openai') {
602-
this.def.function.strict = false
603-
}
604-
}
605-
} catch (e) {
606-
console.error('Error setting schema for test_run_flow tool', e)
607-
// fallback to schema with any properties
608-
this.def.function.parameters = {
609-
type: 'object',
610-
properties: {},
611-
additionalProperties: true,
612-
strict: false
613-
}
614-
}
596+
await buildSchemaForTool(this.def, async () => {
597+
const flowInputsSchema = await helpers.getFlowInputsSchema()
598+
return flowInputsSchema
599+
})
615600
},
616601
requiresConfirmation: true,
617602
showDetails: true
618603
},
619604
{
620-
def: testRunStepToolDef,
605+
// set strict to false to avoid issues with open ai models
606+
def: { ...testRunStepToolDef, function: { ...testRunStepToolDef.function, strict: false } },
621607
fn: async ({ args, workspace, helpers, toolCallbacks, toolId }) => {
622608
const { flow } = helpers.getFlowAndSelectedId()
623609

@@ -661,11 +647,7 @@ export const flowTools: Tool<FlowAIChatHelpers>[] = [
661647
requestBody: {
662648
content: moduleValue.content ?? '',
663649
language: moduleValue.language,
664-
args:
665-
module.id === 'preprocessor'
666-
? { _ENTRYPOINT_OVERRIDE: 'preprocessor', ...stepArgs }
667-
: stepArgs,
668-
tag: flow.tag || moduleValue.tag
650+
args: module.id === 'preprocessor' ? { _ENTRYPOINT_OVERRIDE: 'preprocessor', ...stepArgs } : stepArgs
669651
}
670652
}),
671653
workspace,
@@ -676,18 +658,15 @@ export const flowTools: Tool<FlowAIChatHelpers>[] = [
676658
})
677659
} else if (moduleValue.type === 'script') {
678660
// Test script step - need to get the script content
679-
let script
680-
if (moduleValue.hash) {
681-
script = await ScriptService.getScriptByHash({
682-
workspace: workspace,
683-
hash: moduleValue.hash
684-
})
685-
} else {
686-
script = await ScriptService.getScriptByPath({
687-
workspace: workspace,
688-
path: moduleValue.path
689-
})
690-
}
661+
const script = moduleValue.hash
662+
? await ScriptService.getScriptByHash({
663+
workspace: workspace,
664+
hash: moduleValue.hash
665+
})
666+
: await ScriptService.getScriptByPath({
667+
workspace: workspace,
668+
path: moduleValue.path
669+
})
691670

692671
return executeTestRun({
693672
jobStarter: () =>
@@ -696,12 +675,7 @@ export const flowTools: Tool<FlowAIChatHelpers>[] = [
696675
requestBody: {
697676
content: script.content,
698677
language: script.language,
699-
args:
700-
module.id === 'preprocessor'
701-
? { _ENTRYPOINT_OVERRIDE: 'preprocessor', ...stepArgs }
702-
: stepArgs,
703-
tag: flow.tag || (moduleValue.tag_override ? moduleValue.tag_override : script.tag),
704-
lock: script.lock
678+
args: module.id === 'preprocessor' ? { _ENTRYPOINT_OVERRIDE: 'preprocessor', ...stepArgs } : stepArgs,
705679
}
706680
}),
707681
workspace,
@@ -745,8 +719,7 @@ export function prepareFlowSystemMessage(): ChatCompletionSystemMessageParam {
745719
Follow the user instructions carefully.
746720
Go step by step, and explain what you're doing as you're doing it.
747721
DO NOT wait for user confirmation before performing an action. Only do it if the user explicitly asks you to wait in their initial instructions.
748-
ALWAYS use the \`test_run_flow\` tool to test the flow, and iterate on the flow until it works as expected. If the user cancels the test run, do not try again and wait for the next user instruction.
749-
Use the \`test_run_step\` tool to test individual steps of the flow when debugging or validating specific step functionality.
722+
ALWAYS test your modifications. You have access to the \`test_run_flow\` and \`test_run_step\` tools to test the flow and steps. If you only modified a single step, use the \`test_run_step\` tool to test it. If you modified the flow, use the \`test_run_flow\` tool to test it. If the user cancels the test run, do not try again and wait for the next user instruction.
750723
751724
## Understanding User Requests
752725

frontend/src/lib/components/copilot/chat/script/core.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { scriptLangToEditorLang } from '$lib/scripts'
1313
import { getDbSchemas } from '$lib/components/apps/components/display/dbtable/utils'
1414
import type { CodePieceElement, ContextElement } from '../context'
1515
import { PYTHON_PREPROCESSOR_MODULE_CODE, TS_PREPROCESSOR_MODULE_CODE } from '$lib/script_helpers'
16-
import { createSearchHubScriptsTool, type Tool, executeTestRun } from '../shared'
16+
import { createSearchHubScriptsTool, type Tool, executeTestRun, buildSchemaForTool } from '../shared'
1717
import { setupTypeAcquisition, type DepsToGet } from '$lib/ata'
1818
import { getModelContextWindow } from '../../lib'
1919
import { inferArgs } from '$lib/infer'
@@ -888,9 +888,6 @@ export const testRunScriptTool: Tool<ScriptChatHelpers> = {
888888
content: codeToTest,
889889
args,
890890
language: scriptOptions.lang as ScriptLang,
891-
tag: undefined,
892-
lock: undefined,
893-
script_hash: undefined
894891
}
895892
}),
896893
workspace,
@@ -901,7 +898,7 @@ export const testRunScriptTool: Tool<ScriptChatHelpers> = {
901898
})
902899
},
903900
setSchema: async function(helpers: ScriptChatHelpers) {
904-
try {
901+
await buildSchemaForTool(this.def, async () => {
905902
const scriptOptions = helpers.getScriptOptions()
906903
const code = scriptOptions?.code
907904
const lang = scriptOptions?.lang
@@ -911,23 +908,10 @@ export const testRunScriptTool: Tool<ScriptChatHelpers> = {
911908
if (codeToTest) {
912909
const newSchema = emptySchema()
913910
await inferArgs(lang, codeToTest, newSchema)
914-
this.def.function.parameters = { ...newSchema, additionalProperties: false }
915-
// OPEN AI models don't support strict mode well with schema with complex properties, so we disable it
916-
const model = get(copilotSessionModel)?.provider
917-
if (model === 'openai' || model === 'azure_openai') {
918-
this.def.function.strict = false
919-
}
920-
}
921-
} catch (e) {
922-
console.error('Error setting schema for test_run_script tool', e)
923-
// fallback to schema with any properties
924-
this.def.function.parameters = {
925-
type: 'object',
926-
properties: {},
927-
additionalProperties: true,
928-
strict: false
911+
return newSchema
929912
}
930-
}
913+
return emptySchema()
914+
})
931915
},
932916
requiresConfirmation: true,
933917
showDetails: true,

frontend/src/lib/components/copilot/chat/shared.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
} from 'openai/resources/chat/completions.mjs'
66
import { get } from 'svelte/store'
77
import type { ContextElement } from './context'
8-
import { workspaceStore } from '$lib/stores'
8+
import { copilotSessionModel, workspaceStore } from '$lib/stores'
99
import type { ExtendedOpenFlow } from '$lib/components/flows/types'
1010
import type { FunctionParameters } from 'openai/resources/shared.mjs'
1111
import { zodToJsonSchema } from 'zod-to-json-schema'
@@ -253,6 +253,22 @@ export const createSearchHubScriptsTool = (withContent: boolean = false) => ({
253253
}
254254
})
255255

256+
export async function buildSchemaForTool(toolDef: ChatCompletionTool, schemaBuilder: () => Promise<FunctionParameters>): Promise<void> {
257+
try {
258+
const schema = await schemaBuilder()
259+
toolDef.function.parameters = { ...schema, additionalProperties: false }
260+
// OPEN AI models don't support strict mode well with schema with complex properties, so we disable it
261+
const model = get(copilotSessionModel)?.provider
262+
if (model === 'openai' || model === 'azure_openai') {
263+
toolDef.function.strict = false
264+
}
265+
} catch (error) {
266+
console.error('Error building schema for tool', error)
267+
// fallback to schema with any properties
268+
toolDef.function.parameters = { type: 'object', properties: {}, additionalProperties: true, strict: false }
269+
}
270+
}
271+
256272
// Constants for result formatting
257273
const MAX_RESULT_LENGTH = 12000
258274
const MAX_LOG_LENGTH = 4000

0 commit comments

Comments
 (0)