Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ export namespace SessionPrompt {
await using _ = defer(async () => {
await processor.end()
})

// Dynamically append plugin prompts for this turn
const dynamicPrompts = await appendPluginPrompts(input.sessionID, agent, msgs)
const turnSystem = [...system, ...dynamicPrompts]

const doStream = () =>
streamText({
onError(error) {
Expand Down Expand Up @@ -308,7 +313,7 @@ export namespace SessionPrompt {
temperature: params.temperature,
topP: params.topP,
messages: [
...system.map(
...turnSystem.map(
(x): ModelMessage => ({
role: "system",
content: x,
Expand Down Expand Up @@ -483,6 +488,38 @@ export namespace SessionPrompt {
return Provider.defaultModel()
}

async function appendPluginPrompts(
sessionID: string,
agent: Agent.Info,
history: MessageV2.WithParts[],
): Promise<string[]> {
const appendedPrompts: string[] = []
try {
const output = { prompt: [] as string[] }
await Plugin.trigger(
"system.prompt.append",
{
sessionID,
agent,
// Convert internal MessageV2 types to SDK types for plugin boundary
history: history.map((m) => ({
info: m.info as any, // Cast to SDK message types
parts: m.parts,
})),
},
output,
)
// Basic validation
if (Array.isArray(output.prompt)) {
appendedPrompts.push(...output.prompt.filter((p) => typeof p === "string"))
}
} catch (error) {
console.error(`Error triggering 'system.prompt.append' hook:`, error)
// Gracefully continue without plugin content
}
return appendedPrompts
}

async function resolveSystemPrompt(input: {
system?: string
agent: Agent.Info
Expand Down
19 changes: 19 additions & 0 deletions packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import type {
Provider,
Permission,
UserMessage,
AssistantMessage,
Part,
Auth,
Config,
Agent,
} from "@opencode-ai/sdk"

import type { BunShell } from "./shell"
import { type ToolDefinition } from "./tool"

export * from "./tool"

export type MessageWithParts = { info: UserMessage | AssistantMessage; parts: Part[] }

export type PluginInput = {
client: ReturnType<typeof createOpencodeClient>
project: Project
Expand Down Expand Up @@ -167,4 +171,19 @@ export interface Hooks {
metadata: any
},
) => Promise<void>
/**
* Append text to the system prompt.
* This hook is called at the beginning of each step in a turn, allowing plugins
* to dynamically add context to the system prompt based on the latest history.
*/
"system.prompt.append"?: (
context: {
sessionID: string
agent: Agent
history: MessageWithParts[]
},
output: {
prompt: string[]
},
) => Promise<void>
}