mirror of
https://github.com/aljazceru/opencode.git
synced 2026-01-23 17:54:54 +01:00
fix: resolve @file references in slash commands with subagents (#4221)
This commit is contained in:
@@ -145,6 +145,54 @@ export namespace SessionPrompt {
|
|||||||
),
|
),
|
||||||
})
|
})
|
||||||
export type PromptInput = z.infer<typeof PromptInput>
|
export type PromptInput = z.infer<typeof PromptInput>
|
||||||
|
|
||||||
|
export async function resolvePromptParts(template: string): Promise<PromptInput["parts"]> {
|
||||||
|
const parts: PromptInput["parts"] = [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: template,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
const files = ConfigMarkdown.files(template)
|
||||||
|
await Promise.all(
|
||||||
|
files.map(async (match) => {
|
||||||
|
const name = match[1]
|
||||||
|
const filepath = name.startsWith("~/")
|
||||||
|
? path.join(os.homedir(), name.slice(2))
|
||||||
|
: path.resolve(Instance.worktree, name)
|
||||||
|
|
||||||
|
const stats = await fs.stat(filepath).catch(() => undefined)
|
||||||
|
if (!stats) {
|
||||||
|
const agent = await Agent.get(name)
|
||||||
|
if (agent) {
|
||||||
|
parts.push({
|
||||||
|
type: "agent",
|
||||||
|
name: agent.name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stats.isDirectory()) {
|
||||||
|
parts.push({
|
||||||
|
type: "file",
|
||||||
|
url: `file://${filepath}`,
|
||||||
|
filename: name,
|
||||||
|
mime: "application/x-directory",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
parts.push({
|
||||||
|
type: "file",
|
||||||
|
url: `file://${filepath}`,
|
||||||
|
filename: name,
|
||||||
|
mime: "text/plain",
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
return parts
|
||||||
|
}
|
||||||
export async function prompt(input: PromptInput): Promise<MessageV2.WithParts> {
|
export async function prompt(input: PromptInput): Promise<MessageV2.WithParts> {
|
||||||
const l = log.clone().tag("session", input.sessionID)
|
const l = log.clone().tag("session", input.sessionID)
|
||||||
l.info("prompt")
|
l.info("prompt")
|
||||||
@@ -1605,51 +1653,7 @@ export namespace SessionPrompt {
|
|||||||
}
|
}
|
||||||
template = template.trim()
|
template = template.trim()
|
||||||
|
|
||||||
const parts = [
|
const parts = await resolvePromptParts(template)
|
||||||
{
|
|
||||||
type: "text",
|
|
||||||
text: template,
|
|
||||||
},
|
|
||||||
] as PromptInput["parts"]
|
|
||||||
|
|
||||||
const files = ConfigMarkdown.files(template)
|
|
||||||
await Promise.all(
|
|
||||||
files.map(async (match) => {
|
|
||||||
const name = match[1]
|
|
||||||
const filepath = name.startsWith("~/")
|
|
||||||
? path.join(os.homedir(), name.slice(2))
|
|
||||||
: path.resolve(Instance.worktree, name)
|
|
||||||
|
|
||||||
const stats = await fs.stat(filepath).catch(() => undefined)
|
|
||||||
if (!stats) {
|
|
||||||
const agent = await Agent.get(name)
|
|
||||||
if (agent) {
|
|
||||||
parts.push({
|
|
||||||
type: "agent",
|
|
||||||
name: agent.name,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stats.isDirectory()) {
|
|
||||||
parts.push({
|
|
||||||
type: "file",
|
|
||||||
url: `file://${filepath}`,
|
|
||||||
filename: name,
|
|
||||||
mime: "application/x-directory",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
parts.push({
|
|
||||||
type: "file",
|
|
||||||
url: `file://${filepath}`,
|
|
||||||
filename: name,
|
|
||||||
mime: "text/plain",
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
const model = await (async () => {
|
const model = await (async () => {
|
||||||
if (command.model) {
|
if (command.model) {
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ export const TaskTool = Tool.define("task", async () => {
|
|||||||
ctx.abort.addEventListener("abort", () => {
|
ctx.abort.addEventListener("abort", () => {
|
||||||
SessionLock.abort(session.id)
|
SessionLock.abort(session.id)
|
||||||
})
|
})
|
||||||
|
const promptParts = await SessionPrompt.resolvePromptParts(params.prompt)
|
||||||
const result = await SessionPrompt.prompt({
|
const result = await SessionPrompt.prompt({
|
||||||
messageID,
|
messageID,
|
||||||
sessionID: session.id,
|
sessionID: session.id,
|
||||||
@@ -79,13 +80,7 @@ export const TaskTool = Tool.define("task", async () => {
|
|||||||
task: false,
|
task: false,
|
||||||
...agent.tools,
|
...agent.tools,
|
||||||
},
|
},
|
||||||
parts: [
|
parts: promptParts,
|
||||||
{
|
|
||||||
id: Identifier.ascending("part"),
|
|
||||||
type: "text",
|
|
||||||
text: params.prompt,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})
|
})
|
||||||
unsub()
|
unsub()
|
||||||
let all
|
let all
|
||||||
|
|||||||
Reference in New Issue
Block a user