system-prompt.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { describe, expect, test } from "bun:test"
  2. import { SystemPart } from "@opencode-ai/ai"
  3. import { Agent } from "@opencode-ai/core/agent"
  4. import { Catalog } from "@opencode-ai/core/catalog"
  5. import { Plugin } from "@opencode-ai/core/plugin"
  6. import { PluginHooks } from "@opencode-ai/core/plugin/hooks"
  7. import { PluginHost } from "@opencode-ai/core/plugin/host"
  8. import { SystemPromptPlugin } from "@opencode-ai/core/plugin/system-prompt"
  9. import { Session } from "@opencode-ai/core/session"
  10. import type { SessionHooks } from "@opencode-ai/plugin/effect/session"
  11. import { Model } from "@opencode-ai/schema/model"
  12. import { Provider } from "@opencode-ai/schema/provider"
  13. import { Effect } from "effect"
  14. import { testEffect } from "../lib/effect"
  15. import { PluginTestLayer } from "./fixture"
  16. import PROMPT_META from "../../src/plugin/system-prompt/meta.txt"
  17. import PROMPT_DEFAULT from "../../src/session/runner/prompt/base.txt"
  18. const it = testEffect(PluginTestLayer)
  19. const fallback = PROMPT_DEFAULT
  20. const makeHost = Effect.gen(function* () {
  21. const agents = yield* Agent.Service
  22. const plugins = yield* Plugin.Service
  23. yield* agents.transform((draft) => draft.update(Agent.ID.make("build"), () => {}))
  24. return yield* PluginHost.make(plugins)
  25. })
  26. const context = (id: string, system = fallback): SessionHooks["context"] => ({
  27. sessionID: Session.ID.make("ses_system_prompt"),
  28. agent: Agent.ID.make("build"),
  29. model: Model.Ref.make({ providerID: Provider.ID.make("test"), id: Model.ID.make(id) }),
  30. system: [SystemPart.make(system)],
  31. messages: [],
  32. tools: {},
  33. })
  34. describe("SystemPromptPlugin", () => {
  35. test("uses current vocabulary in the Meta prompt", () => {
  36. expect(PROMPT_META).toContain("`webfetch` tool")
  37. expect(PROMPT_META).toContain("`subagent` tool")
  38. expect(PROMPT_META).toContain("Reserve `shell`")
  39. expect(PROMPT_META).toContain("`read` for reading files")
  40. expect(PROMPT_META).toContain("`edit` for editing")
  41. expect(PROMPT_META).toContain("`write` for creating files")
  42. expect(PROMPT_META).toContain("https://opencode.ai/v2/docs/")
  43. expect(PROMPT_META).not.toMatch(/TodoWrite|Task tool|WebFetch|\bBash\b|https:\/\/opencode\.ai\/docs/)
  44. })
  45. test("uses granular IDs with a common prefix", () => {
  46. expect(SystemPromptPlugin.Plugins.map((plugin) => plugin.id)).toEqual([
  47. "opencode.system-prompt.openai",
  48. "opencode.system-prompt.google",
  49. "opencode.system-prompt.anthropic",
  50. "opencode.system-prompt.kimi",
  51. "opencode.system-prompt.arcee",
  52. "opencode.system-prompt.meta",
  53. ])
  54. })
  55. it.effect("selects model-lab prompts through session context hooks", () =>
  56. Effect.gen(function* () {
  57. const hooks = yield* PluginHooks.Service
  58. const pluginHost = yield* makeHost
  59. yield* Effect.forEach(SystemPromptPlugin.Plugins, (plugin) => plugin.effect(pluginHost), {
  60. discard: true,
  61. })
  62. const cases = [
  63. ["gpt-5", "You are OpenCode, You and the user share the same workspace"],
  64. ["gpt-4.1", "You are OpenCode, You and the user share the same workspace"],
  65. ["o3", "You are OpenCode, You and the user share the same workspace"],
  66. ["gpt-5-codex", "## Editing constraints"],
  67. ["gemini-2.5-pro", "# Core Mandates"],
  68. ["claude-sonnet-4", "# Professional objectivity"],
  69. ["kimi-k2", "# Prompt and Tool Use"],
  70. ["trinity", "what command should I run to list files"],
  71. ["meta/muse-spark-1.1", "powered by Muse Spark"],
  72. ["llama-3.3", "You are opencode, an interactive CLI tool"],
  73. ] as const
  74. yield* Effect.forEach(
  75. cases,
  76. ([id, expected]) => {
  77. const event = context(id)
  78. return hooks
  79. .trigger("session", "context", event)
  80. .pipe(Effect.tap(() => Effect.sync(() => expect(event.system[0]?.text).toContain(expected))))
  81. },
  82. { discard: true },
  83. )
  84. }),
  85. )
  86. it.effect("preserves an explicit agent system prompt", () =>
  87. Effect.gen(function* () {
  88. const agents = yield* Agent.Service
  89. const hooks = yield* PluginHooks.Service
  90. yield* agents.transform((draft) =>
  91. draft.update(Agent.ID.make("build"), (agent) => {
  92. agent.system = "Custom agent prompt"
  93. }),
  94. )
  95. const pluginHost = yield* makeHost
  96. yield* Effect.forEach(SystemPromptPlugin.Plugins, (plugin) => plugin.effect(pluginHost), {
  97. discard: true,
  98. })
  99. const event = context("gpt-5", "Custom agent prompt")
  100. yield* hooks.trigger("session", "context", event)
  101. expect(event.system.map((part) => part.text)).toEqual(["Custom agent prompt"])
  102. }),
  103. )
  104. it.effect("skips the hook when agent lookup fails", () =>
  105. Effect.gen(function* () {
  106. const agents = yield* Agent.Service
  107. const hooks = yield* PluginHooks.Service
  108. const pluginHost = yield* makeHost
  109. yield* SystemPromptPlugin.OpenAIPlugin.effect(pluginHost)
  110. yield* agents.transform((draft) => draft.remove(Agent.ID.make("build")))
  111. const event = context("gpt-5")
  112. yield* hooks.trigger("session", "context", event)
  113. expect(event.system[0]?.text).toBe(fallback)
  114. }),
  115. )
  116. it.effect("allows one model-lab prompt plugin to be enabled independently", () =>
  117. Effect.gen(function* () {
  118. const hooks = yield* PluginHooks.Service
  119. const pluginHost = yield* makeHost
  120. yield* SystemPromptPlugin.GooglePlugin.effect(pluginHost)
  121. const gemini = context("gemini-2.5-pro")
  122. const claude = context("claude-sonnet-4")
  123. yield* hooks.trigger("session", "context", gemini)
  124. yield* hooks.trigger("session", "context", claude)
  125. expect(gemini.system[0]?.text).toContain("# Core Mandates")
  126. expect(claude.system[0]?.text).toBe(fallback)
  127. }),
  128. )
  129. it.effect("selects against the catalog model ID instead of its alias", () =>
  130. Effect.gen(function* () {
  131. const catalog = yield* Catalog.Service
  132. const hooks = yield* PluginHooks.Service
  133. const pluginHost = yield* makeHost
  134. yield* catalog.transform((draft) => {
  135. draft.model.update(Provider.ID.make("test"), Model.ID.make("openai-alias"), (model) => {
  136. model.modelID = Model.ID.make("gpt-5")
  137. })
  138. draft.model.update(Provider.ID.make("test"), Model.ID.make("gpt-5-alias"), (model) => {
  139. model.modelID = Model.ID.make("custom-model")
  140. })
  141. draft.model.update(Provider.ID.make("test"), Model.ID.make("codex-family-alias"), (model) => {
  142. model.modelID = Model.ID.make("custom-deployment")
  143. model.family = Model.Family.make("gpt-codex")
  144. })
  145. })
  146. yield* SystemPromptPlugin.OpenAIPlugin.effect(pluginHost)
  147. const physicalOpenAI = context("openai-alias")
  148. const physicalCustom = context("gpt-5-alias")
  149. const familyOpenAI = context("codex-family-alias")
  150. yield* hooks.trigger("session", "context", physicalOpenAI)
  151. yield* hooks.trigger("session", "context", physicalCustom)
  152. yield* hooks.trigger("session", "context", familyOpenAI)
  153. expect(physicalOpenAI.system[0]?.text).toContain("You are OpenCode, You and the user share the same workspace")
  154. expect(physicalCustom.system[0]?.text).toBe(fallback)
  155. expect(familyOpenAI.system[0]?.text).toContain("## Editing constraints")
  156. }),
  157. )
  158. })