mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
fix(walkthrough): address review of agent-opened walkthrough
- Wait for the model catalog once per walkthrough generation instead of before every model call - Report open_walkthrough as requested, not opened, and drop its unused metadata - Share the ocm tool and action names through @opencode-manager/shared - Remove the unused source-request nonce and duplicate session check - Scan only new messages when auto-opening the walkthrough - Assert the request action cannot reach the internal change-walkthroughs route
This commit is contained in:
1 parent
aa1da8d608
commit
c497df5161
14 files changed
+146
-122
No files matched your search
@@ -12,12 +12,11 @@ import {
|
||||
fileExists,
|
||||
ensureDirectoryExists,
|
||||
} from './file-operations'
|
||||
import { ASSISTANT_NOTIFICATION_LIMITS, AssistantSettingsPatchSchema, OpenCodeConfigSchema, DEFAULT_KEYBOARD_SHORTCUTS, DEFAULT_LEADER_KEY } from '@opencode-manager/shared/schemas'
|
||||
import { ASSISTANT_NOTIFICATION_LIMITS, AssistantSettingsPatchSchema, OpenCodeConfigSchema, DEFAULT_KEYBOARD_SHORTCUTS, DEFAULT_LEADER_KEY, MANAGER_TOOL_NAME } from '@opencode-manager/shared/schemas'
|
||||
import { MANAGER_COLOR_THEME_ID, OPENCODE_THEMES } from '@opencode-manager/shared/themes'
|
||||
import { ASSISTANT_REPO_ID, ASSISTANT_REPO_PATH, ASSISTANT_OPENCODE_DIR_NAME } from '@opencode-manager/shared/utils'
|
||||
import { getAssistantModePath, getReposPath } from '@opencode-manager/shared/config/env'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import { MANAGER_TOOL_NAME } from './opencode-manager-tool-plugin'
|
||||
import { ensureAssistantRepo } from '../db/queries'
|
||||
|
||||
|
||||
|
||||
@@ -1297,14 +1297,25 @@ export class ChangeWalkthroughService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the model for one generation, waiting once for OpenCode's lazily-loaded global catalog so an explicit
|
||||
* walkthrough model is registered before the first call. A failed wait is swallowed because generation proceeds and
|
||||
* lets OpenCode surface its own error; the wait is per generation, never per stop.
|
||||
*/
|
||||
private async resolveWalkthroughModel(session: SessionInfo): Promise<ModelRef> {
|
||||
const configured = this.settingsService.getSettings().preferences.walkthroughModel?.trim()
|
||||
const parsed = configured ? parseOpenCodeModelRef(configured) : undefined
|
||||
const preferred = parsed ?? session.model
|
||||
if (preferred) {
|
||||
try {
|
||||
await resolveOpenCodeModel(this.openCodeClient, getOpenCodeGlobalConfigPath(), {
|
||||
preferredModel: formatOpenCodeModelRef(preferred),
|
||||
})
|
||||
} catch {
|
||||
return preferred
|
||||
}
|
||||
return preferred
|
||||
}
|
||||
|
||||
let resolved
|
||||
try {
|
||||
resolved = await resolveOpenCodeModel(this.openCodeClient, getOpenCodeGlobalConfigPath())
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { z } from 'zod'
|
||||
import { ASSISTANT_NOTIFICATION_LIMITS, WalkthroughSourceSchema } from '@opencode-manager/shared/schemas'
|
||||
|
||||
export const MANAGER_TOOL_NAME = 'ocm'
|
||||
import { ASSISTANT_NOTIFICATION_LIMITS, MANAGER_TOOL_ACTIONS, MANAGER_TOOL_NAME, WalkthroughSourceSchema } from '@opencode-manager/shared/schemas'
|
||||
|
||||
const MANAGER_TOOL_REQUEST_TIMEOUT_MS = 60000
|
||||
|
||||
@@ -40,8 +38,6 @@ export const MANAGER_TOOL_ALLOWED_ROUTES = [
|
||||
|
||||
const MANAGER_TOOL_ALLOWED_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'] as const
|
||||
|
||||
const MANAGER_TOOL_ACTION_NAMES = ['send_notification', 'request', 'open_walkthrough'] as const
|
||||
|
||||
const ManagerToolNotificationParamsSchema = z
|
||||
.object({
|
||||
title: z.string().min(1).max(ASSISTANT_NOTIFICATION_LIMITS.TITLE_MAX).describe('The notification title.'),
|
||||
@@ -67,9 +63,9 @@ const ManagerToolWalkthroughParamsSchema = z
|
||||
source: WalkthroughSourceSchema.optional().describe('The change source to walk through. Defaults to the changes in this session.'),
|
||||
})
|
||||
.strict()
|
||||
.describe("Open the change Walkthrough for this session in the user's Manager window and start generating it if needed.")
|
||||
.describe("Request the change Walkthrough for this session, generating it if needed. It opens in the user's Manager window when this session is open on desktop; otherwise the user can open it from this tool call.")
|
||||
|
||||
const MANAGER_TOOL_ACTION_PARAMS_SCHEMAS: Record<(typeof MANAGER_TOOL_ACTION_NAMES)[number], z.ZodType> = {
|
||||
const MANAGER_TOOL_ACTION_PARAMS_SCHEMAS: Record<(typeof MANAGER_TOOL_ACTIONS)[number], z.ZodType> = {
|
||||
send_notification: ManagerToolNotificationParamsSchema,
|
||||
request: ManagerToolRequestParamsSchema,
|
||||
open_walkthrough: ManagerToolWalkthroughParamsSchema,
|
||||
@@ -100,7 +96,7 @@ function buildManagerToolInputJsonSchema(): Record<string, unknown> {
|
||||
const jsonSchema: Record<string, unknown> = z.toJSONSchema(
|
||||
z
|
||||
.object({
|
||||
action: z.enum(MANAGER_TOOL_ACTION_NAMES).describe('The OpenCode Manager action to perform.'),
|
||||
action: z.enum(MANAGER_TOOL_ACTIONS).describe('The OpenCode Manager action to perform.'),
|
||||
params: z
|
||||
.union([
|
||||
MANAGER_TOOL_ACTION_PARAMS_SCHEMAS.send_notification,
|
||||
@@ -116,7 +112,7 @@ function buildManagerToolInputJsonSchema(): Record<string, unknown> {
|
||||
}
|
||||
|
||||
function buildManagerToolActionRequiredKeys(): Record<string, string[]> {
|
||||
return Object.fromEntries(MANAGER_TOOL_ACTION_NAMES.map((name) => [name, requiredKeysOf(MANAGER_TOOL_ACTION_PARAMS_SCHEMAS[name])]))
|
||||
return Object.fromEntries(MANAGER_TOOL_ACTIONS.map((name) => [name, requiredKeysOf(MANAGER_TOOL_ACTION_PARAMS_SCHEMAS[name])]))
|
||||
}
|
||||
|
||||
function buildManagerToolDescription(): string {
|
||||
@@ -126,7 +122,7 @@ function buildManagerToolDescription(): string {
|
||||
'Actions:',
|
||||
'- send_notification: send a push notification to every device the user has registered.',
|
||||
'- request: call an allow-listed internal API route to read and manage settings, the OpenCode configuration file, repos (list, inspect, clone from a git URL with POST /repos and body { repoUrl, branch?, directoryName? }), OpenCode workspaces, sessions (list, create, follow up, read the latest reply, fork), and schedules.',
|
||||
"- open_walkthrough: open the change Walkthrough panel for the current session in the user's Manager window, generating it if needed; use when the user asks to walk through, review or explain the changes.",
|
||||
"- open_walkthrough: request the change Walkthrough for the current session, generating it if needed. It opens in the user's Manager window when this session is open on desktop; otherwise the user opens it from this tool call. Use when the user asks to walk through, review or explain the changes.",
|
||||
'Allowed request routes:',
|
||||
]
|
||||
.concat(MANAGER_TOOL_ALLOWED_ROUTES.map((route) => `- ${route}`))
|
||||
@@ -142,7 +138,7 @@ var ALLOWED_MATCHERS = ${JSON.stringify(buildRouteMatchers())}.map(function (mat
|
||||
return { method: matcher.method, pattern: new RegExp(matcher.source) }
|
||||
})
|
||||
|
||||
var ACTION_NAMES = ${JSON.stringify(MANAGER_TOOL_ACTION_NAMES)}
|
||||
var ACTION_NAMES = ${JSON.stringify(MANAGER_TOOL_ACTIONS)}
|
||||
|
||||
var ACTION_REQUIRED_KEYS = ${JSON.stringify(buildManagerToolActionRequiredKeys())}
|
||||
|
||||
@@ -238,21 +234,15 @@ var ACTIONS = {
|
||||
run: async function (params, context) {
|
||||
var body = params.source === undefined ? undefined : { source: params.source }
|
||||
var state = await postInternalApi('/change-walkthroughs/' + encodeURIComponent(context.sessionID), body, context.signal)
|
||||
var metadata = { openPanel: 'walkthrough' }
|
||||
if (params.source !== undefined) {
|
||||
metadata.source = params.source
|
||||
}
|
||||
var hint = ' It opens in the Manager window when this session is open on desktop; otherwise the user can open it from this tool call.'
|
||||
if (state.generating === true) {
|
||||
return {
|
||||
content: 'Opened the walkthrough. It is generating; the user can page through it as stops are explained.',
|
||||
metadata: metadata,
|
||||
}
|
||||
return 'Requested the walkthrough; it is generating.' + hint
|
||||
}
|
||||
var stops = state.walkthrough && Array.isArray(state.walkthrough.stops) ? state.walkthrough.stops.length : 0
|
||||
if (state.walkthrough) {
|
||||
return { content: 'Opened the walkthrough (' + stops + ' stops).', metadata: metadata }
|
||||
return 'Requested the walkthrough (' + stops + ' stops).' + hint
|
||||
}
|
||||
return { content: 'Opened the walkthrough.', metadata: metadata }
|
||||
return 'Requested the walkthrough.' + hint
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -291,11 +281,7 @@ export default {
|
||||
input: INPUT_SCHEMA,
|
||||
options: { codemode: false },
|
||||
execute: async function (input, context) {
|
||||
var result = await runAction(input, context)
|
||||
if (result !== null && typeof result === 'object' && typeof result.content === 'string') {
|
||||
return result
|
||||
}
|
||||
return { content: result }
|
||||
return { content: await runAction(input, context) }
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { ModelRef } from '@opencode-manager/shared/opencode'
|
||||
import { formatOpenCodeModelRef } from '@opencode-manager/shared/opencode'
|
||||
import { getOpenCodeGlobalConfigPath } from '@opencode-manager/shared/config/env'
|
||||
import type { OpenCodeClient } from './client'
|
||||
import { resolveOpenCodeModel } from '../opencode-models'
|
||||
@@ -38,34 +37,10 @@ async function generateText(
|
||||
input: { prompt: string; model?: ModelRef },
|
||||
signal: AbortSignal,
|
||||
): Promise<{ text: string }> {
|
||||
if (input.model) {
|
||||
await waitForGenerateModel(client, input.model, signal)
|
||||
return client.api.generate.text({ prompt: input.prompt, model: input.model }, { signal })
|
||||
}
|
||||
|
||||
const model = await resolveGenerateModel(client, signal)
|
||||
const model = input.model ?? await resolveGenerateModel(client, signal)
|
||||
return client.api.generate.text({ prompt: input.prompt, model }, { signal })
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for an explicitly requested model to appear in OpenCode's global config-location catalog, which loads lazily after OpenCode starts.
|
||||
* Generation proceeds even if the wait times out or the model stays unconfigured, so OpenCode can surface its own error; an abort still propagates.
|
||||
*/
|
||||
async function waitForGenerateModel(
|
||||
client: OpenCodeClient,
|
||||
model: ModelRef,
|
||||
signal: AbortSignal,
|
||||
): Promise<void> {
|
||||
try {
|
||||
await resolveOpenCodeModel(client, getOpenCodeGlobalConfigPath(), {
|
||||
signal,
|
||||
preferredModel: formatOpenCodeModelRef(model),
|
||||
})
|
||||
} catch {
|
||||
signal.throwIfAborted()
|
||||
}
|
||||
}
|
||||
|
||||
/** OpenCode serves generation from its global config location, so the model must be resolved there once its catalog has loaded. */
|
||||
async function resolveGenerateModel(client: OpenCodeClient, signal: AbortSignal): Promise<ModelRef> {
|
||||
const { providerID, id, variant } = await resolveOpenCodeModel(client, getOpenCodeGlobalConfigPath(), { signal })
|
||||
|
||||
Reference in new issue
Block a user