diff --git a/config/prompt_templates/agent_system_prompt.yaml b/config/prompt_templates/agent_system_prompt.yaml index 2e2105ad..72b42d94 100644 --- a/config/prompt_templates/agent_system_prompt.yaml +++ b/config/prompt_templates/agent_system_prompt.yaml @@ -434,8 +434,9 @@ templates: 1. Read the SKILL.md text in the user message and work out what has to be installed. Dependencies are often described in prose rather than a requirements file — read carefully. 2. Inspect the skill directory with `ls -la` before installing, so you know which of requirements.txt / pyproject.toml / package.json actually exist. 3. Install dependencies INTO THE SKILL DIRECTORY (see ), including on-demand extras (see ). - 4. PROVE the imports resolve by running them (see ). Do not skip this: nothing else can check it. - 5. Reply with a short summary: what you installed, where it went, and anything you had to change in the skill's own files. + 4. PROVE the imports resolve by running them (see ). Also identify and verify external CLI prerequisites, even for Markdown-only skills, following linked official setup guides when necessary. "Installed separately" does not mean no dependency. + 5. Write the runtime prerequisite report requested in the user message. Record unresolved external setup or sandbox incompatibility as blockers; never claim readiness just because there are no package manifests. + 6. Reply with a short summary: what you installed, where it went, and anything you had to change in the skill's own files. diff --git a/frontend/src/api/system/index.ts b/frontend/src/api/system/index.ts index 528e1444..b33b400c 100644 --- a/frontend/src/api/system/index.ts +++ b/frontend/src/api/system/index.ts @@ -1133,10 +1133,11 @@ export function installConfigSkillFromSource( export function reinstallConfigSkill( configId: string, skillId: string, + instructions = '', ): Promise<{ data: { skill_id: string } }> { return post( `/api/v1/sandbox-configs/${configId}/skills/${skillId}/reinstall`, - {}, + { instructions }, ) as unknown as Promise<{ data: { skill_id: string } }> } @@ -1230,3 +1231,18 @@ export function getConfigSkillFile( params: { path }, }) as unknown as Promise<{ data: ConfigSkillFileContent }> } + +export interface SkillInstallGuidanceState { + accepting: boolean + messages: Array<{ id: string; content: string; status: 'pending' | 'injected' | 'unprocessed' }> +} + +export function getConfigSkillGuidance(configId: string, skillId: string) { + return get(`/api/v1/sandbox-configs/${configId}/skills/${skillId}/guidance`) as unknown as Promise<{ data: SkillInstallGuidanceState }> +} + +export function steerConfigSkill(configId: string, skillId: string, payload: { + expected_message_id: string; steer_id: string; content: string +}) { + return post(`/api/v1/sandbox-configs/${configId}/skills/${skillId}/guidance`, payload) +} diff --git a/frontend/src/components/SandboxSkillsPanel.vue b/frontend/src/components/SandboxSkillsPanel.vue index 9a6527f0..1d8425cb 100644 --- a/frontend/src/components/SandboxSkillsPanel.vue +++ b/frontend/src/components/SandboxSkillsPanel.vue @@ -255,6 +255,8 @@ :session-id="managedSkill.install_session_id || ''" :message-id="managedSkill.install_message_id || ''" :live="managedSkill.status === 'installing'" + :can-retry="managedSkill.status === 'ready' || managedSkill.status === 'failed'" + @restarted="loadSkills()" /> @@ -495,6 +497,8 @@ :session-id="skill.install_session_id || ''" :message-id="skill.install_message_id || ''" :live="skill.status === 'installing'" + :can-retry="skill.status === 'ready' || skill.status === 'failed'" + @restarted="loadSkills()" /> diff --git a/frontend/src/components/SkillInstallTimeline.test.mjs b/frontend/src/components/SkillInstallTimeline.test.mjs new file mode 100644 index 00000000..49e0911d --- /dev/null +++ b/frontend/src/components/SkillInstallTimeline.test.mjs @@ -0,0 +1,98 @@ +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import vm from 'node:vm' +import test from 'node:test' +import ts from 'typescript' + +const source = readFileSync(new URL('./SkillInstallTimeline.vue', import.meta.url), 'utf8') +const logic = source.slice(source.indexOf('const emit = defineEmits'), source.indexOf('const messages = reactive')) +const compiled = ts.transpileModule(logic, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText +const deferred = () => { + let resolve, reject + const promise = new Promise((yes, no) => { resolve = yes; reject = no }) + return { promise, resolve, reject } +} +function harness(overrides = {}) { + const watches = [], emitted = [] + let id = 0 + const state = { + props: { configId: 'cfg', skillId: 'skill', messageId: 'run', live: true, canRetry: false }, + ref: value => ({ value }), defineEmits: () => name => emitted.push(name), + watch: (_source, callback) => watches.push(callback), onUnmounted() {}, + setTimeout: () => 1, clearTimeout() {}, makeSteerClientId: () => `id-${++id}`, + i18n: { global: { t: key => key } }, + getConfigSkillGuidance: async () => ({ data: { accepting: true, messages: [] } }), + steerConfigSkill: async () => {}, reinstallConfigSkill: async () => {}, + ...overrides, + } + const actions = vm.runInNewContext(`${compiled}\n({ sendGuidance, refreshGuidance, guidance, guidanceText, guidanceError, sendingGuidance })`, state) + actions.guidance.value.accepting = true + return { ...actions, state, watches, emitted } +} + +test('uncertain sends preserve the draft and retry the same message ID', async () => { + const calls = [] + const h = harness({ steerConfigSkill: async (...args) => { + calls.push(args) + if (calls.length === 1) throw new Error('network disconnected') + } }) + h.guidanceText.value = 'Install the CLI' + await h.sendGuidance() + assert.equal(h.guidanceText.value, 'Install the CLI') + assert.equal(h.guidanceError.value, 'network disconnected') + await h.sendGuidance() + assert.equal(calls[0][2].steer_id, calls[1][2].steer_id) + assert.equal(calls[1][2].expected_message_id, 'run') + assert.equal(h.guidanceText.value, '') + assert.equal(h.guidance.value.messages.length, 1) +}) + +test('a success needs no extra read to acknowledge and clear the draft', async () => { + const h = harness({ getConfigSkillGuidance: async () => { throw new Error('read failed') } }) + h.guidanceText.value = 'Verify readiness' + await h.sendGuidance() + assert.equal(h.guidanceText.value, '') + assert.equal(h.guidanceError.value, '') + assert.equal(h.guidance.value.messages[0].status, 'pending') +}) + +test('stale polling results cannot enable input for a different run', async () => { + const request = deferred() + let calls = 0 + const h = harness({ getConfigSkillGuidance: () => ++calls === 1 ? request.promise : Promise.resolve({ data: { accepting: true, messages: [] } }) }) + const poll = h.refreshGuidance(0) + h.state.props.messageId = 'new-run' + h.watches[0]() + await Promise.resolve() + request.resolve({ data: { accepting: false, messages: [] } }) + await poll + assert.equal(h.guidance.value.accepting, true) +}) + +test('a run switch while sending preserves the draft instead of clearing another run', async () => { + const request = deferred() + const h = harness({ steerConfigSkill: () => request.promise }) + h.guidanceText.value = 'Guidance for old run' + const sending = h.sendGuidance() + h.state.props.messageId = 'new-run' + h.watches[0]() + request.resolve() + await sending + assert.equal(h.guidanceText.value, 'Guidance for old run') + assert.equal(h.sendingGuidance.value, false) +}) + +test('verification refuses live sends and a finished run reinstalls with instructions', async () => { + const calls = [] + const h = harness({ reinstallConfigSkill: async (...args) => calls.push(args) }) + h.guidance.value.accepting = false + h.guidanceText.value = 'Install from the official guide' + await h.sendGuidance() + assert.equal(h.guidanceText.value, 'Install from the official guide') + h.state.props.live = false + h.state.props.canRetry = true + await h.sendGuidance() + assert.equal(calls.length, 1) + assert.equal(calls[0][2], 'Install from the official guide') + assert.deepEqual(h.emitted, ['restarted']) +}) diff --git a/frontend/src/components/SkillInstallTimeline.vue b/frontend/src/components/SkillInstallTimeline.vue index c9bf6d21..9aad02f3 100644 --- a/frontend/src/components/SkillInstallTimeline.vue +++ b/frontend/src/components/SkillInstallTimeline.vue @@ -18,6 +18,31 @@ /> +
+
+ {{ $t(`settings.sandbox.skillGuidance.${item.status}`) }} +

{{ item.content }}

+
+ +
@@ -26,9 +51,10 @@ import { onUnmounted, reactive, ref, watch } from 'vue' import { fetchEventSource } from '@microsoft/fetch-event-source' import { useChatStreamHandler } from '@/composables/useChatStreamHandler' import { getMessageList } from '@/api/chat' -import { configSkillTranscriptUrl } from '@/api/system' +import { configSkillTranscriptUrl, getConfigSkillGuidance, steerConfigSkill, reinstallConfigSkill, type SkillInstallGuidanceState } from '@/api/system' import { getApiBaseUrl } from '@/utils/api-base' import { generateRandomString } from '@/utils/index' +import { makeSteerClientId } from '@/utils/steerId' import AgentStreamDisplay from '@/views/chat/components/AgentStreamDisplay.vue' import i18n from '@/i18n' @@ -43,8 +69,80 @@ const props = defineProps<{ // not hit /transcript. live?: boolean compact?: boolean + canRetry?: boolean }>() +const emit = defineEmits<{ restarted: [] }>() +const guidance = ref({ accepting: false, messages: [] }) +const guidanceText = ref('') +const guidanceError = ref('') +const sendingGuidance = ref(false) +let guidanceEpoch = 0 +let guidanceTimer: ReturnType | undefined +// Keep the same ID after an uncertain response so Retry cannot enqueue twice. +let pendingSend: { messageId: string; content: string; id: string } | undefined + +async function refreshGuidance(epoch: number) { + try { + const res = await getConfigSkillGuidance(props.configId, props.skillId) + if (epoch !== guidanceEpoch) return + guidance.value = res.data + } catch { + if (epoch === guidanceEpoch) guidance.value.accepting = false + } + if (epoch === guidanceEpoch && props.live) { + guidanceTimer = setTimeout(() => void refreshGuidance(epoch), 1500) + } +} + +async function sendGuidance() { + const content = guidanceText.value.trim() + if (!content || sendingGuidance.value || (props.live ? !guidance.value.accepting : !props.canRetry)) return + const epoch = guidanceEpoch + const target = { configId: props.configId, skillId: props.skillId, messageId: props.messageId } + sendingGuidance.value = true + guidanceError.value = '' + try { + if (props.live) { + if (!pendingSend || pendingSend.messageId !== target.messageId || pendingSend.content !== content) { + pendingSend = { messageId: target.messageId, content, id: makeSteerClientId() } + } + await steerConfigSkill(target.configId, target.skillId, { + expected_message_id: target.messageId, steer_id: pendingSend.id, content, + }) + if (epoch !== guidanceEpoch) return + // A failed refresh after a successful POST must not turn Retry into a duplicate send. + if (!guidance.value.messages.some(item => item.id === pendingSend!.id)) { + guidance.value.messages.push({ id: pendingSend.id, content, status: 'pending' }) + } + pendingSend = undefined + } else { + await reinstallConfigSkill(target.configId, target.skillId, content) + if (epoch !== guidanceEpoch) return + emit('restarted') + } + guidanceText.value = '' + } catch (err: any) { + if (epoch === guidanceEpoch) { + guidanceError.value = err?.response?.data?.error?.message || err?.message || i18n.global.t('settings.sandbox.skillGuidance.failed') + } + } finally { + sendingGuidance.value = false + } +} + +watch( + () => [props.configId, props.skillId, props.messageId, props.live] as const, + () => { + const epoch = ++guidanceEpoch + clearTimeout(guidanceTimer) + guidance.value = { accepting: false, messages: [] } + if (props.configId && props.skillId) void refreshGuidance(epoch) + }, + { immediate: true }, +) +onUnmounted(() => { guidanceEpoch++; clearTimeout(guidanceTimer) }) + const messages = reactive([]) const loading = ref(false) const isReplying = ref(false) @@ -233,6 +331,25 @@ onUnmounted(stop)