system-prompt.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { describe, expect, test } from "bun:test"
  2. import { SystemPart } from "@opencode-ai/ai"
  3. import { AgentV2 } from "@opencode-ai/core/agent"
  4. import { Catalog } from "@opencode-ai/core/catalog"
  5. import { PluginV2 } 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 { SessionV2 } from "@opencode-ai/core/session"
  10. import type { SessionHooks } from "@opencode-ai/plugin/v2/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 plugins = yield* PluginV2.Service
  22. return yield* PluginHost.make(plugins)
  23. })
  24. const context = (id: string, system = fallback): SessionHooks["context"] => ({
  25. sessionID: SessionV2.ID.make("ses_system_prompt"),
  26. agent: AgentV2.ID.make("build"),
  27. model: Model.Ref.make({ providerID: Provider.ID.make("test"), id: Model.ID.make(id) }),
  28. system: [SystemPart.make(system)],
  29. messages: [],
  30. tools: {},
  31. })
  32. describe("SystemPromptPlugin", () => {
  33. test("uses V2 vocabulary in the Meta prompt", () => {
  34. expect(PROMPT_META).toContain("webfetch tool")
  35. expect(PROMPT_META).toContain("subagent tool")
  36. expect(PROMPT_META).toContain("shell tool")
  37. expect(PROMPT_META).toContain("read for reading files")
  38. expect(PROMPT_META).toContain("edit for editing")
  39. expect(PROMPT_META).toContain("write for creating files")
  40. expect(PROMPT_META).toContain("https://v2.opencode.ai/llms.txt")
  41. expect(PROMPT_META).not.toMatch(
  42. /TodoWrite|Task tool|WebFetch|\bBash\b|Read for reading files|Edit for editing|Write for creating files|https:\/\/opencode\.ai\/docs/,
  43. )
  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", "OpenCode powered by Meta 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* AgentV2.Service
  89. const hooks = yield* PluginHooks.Service
  90. yield* agents.transform((draft) =>
  91. draft.update(AgentV2.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("allows one model-lab prompt plugin to be enabled independently", () =>
  105. Effect.gen(function* () {
  106. const hooks = yield* PluginHooks.Service
  107. const pluginHost = yield* makeHost
  108. yield* SystemPromptPlugin.GooglePlugin.effect(pluginHost)
  109. const gemini = context("gemini-2.5-pro")
  110. const claude = context("claude-sonnet-4")
  111. yield* hooks.trigger("session", "context", gemini)
  112. yield* hooks.trigger("session", "context", claude)
  113. expect(gemini.system[0]?.text).toContain("# Core Mandates")
  114. expect(claude.system[0]?.text).toBe(fallback)
  115. }),
  116. )
  117. it.effect("selects against the catalog model ID instead of its alias", () =>
  118. Effect.gen(function* () {
  119. const catalog = yield* Catalog.Service
  120. const hooks = yield* PluginHooks.Service
  121. const pluginHost = yield* makeHost
  122. yield* catalog.transform((draft) => {
  123. draft.model.update(Provider.ID.make("test"), Model.ID.make("openai-alias"), (model) => {
  124. model.modelID = Model.ID.make("gpt-5")
  125. })
  126. draft.model.update(Provider.ID.make("test"), Model.ID.make("gpt-5-alias"), (model) => {
  127. model.modelID = Model.ID.make("custom-model")
  128. })
  129. draft.model.update(Provider.ID.make("test"), Model.ID.make("codex-family-alias"), (model) => {
  130. model.modelID = Model.ID.make("custom-deployment")
  131. model.family = Model.Family.make("gpt-codex")
  132. })
  133. })
  134. yield* SystemPromptPlugin.OpenAIPlugin.effect(pluginHost)
  135. const physicalOpenAI = context("openai-alias")
  136. const physicalCustom = context("gpt-5-alias")
  137. const familyOpenAI = context("codex-family-alias")
  138. yield* hooks.trigger("session", "context", physicalOpenAI)
  139. yield* hooks.trigger("session", "context", physicalCustom)
  140. yield* hooks.trigger("session", "context", familyOpenAI)
  141. expect(physicalOpenAI.system[0]?.text).toContain("You are OpenCode, You and the user share the same workspace")
  142. expect(physicalCustom.system[0]?.text).toBe(fallback)
  143. expect(familyOpenAI.system[0]?.text).toContain("## Editing constraints")
  144. }),
  145. )
  146. })