shared-schema.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { expect, test } from "bun:test"
  2. import { Schema } from "effect"
  3. import { Agent } from "@opencode-ai/core/agent"
  4. import { Session } from "@opencode-ai/core/session"
  5. import { Location } from "@opencode-ai/schema/location"
  6. import { Model } from "@opencode-ai/schema/model"
  7. import { Provider } from "@opencode-ai/schema/provider"
  8. import { Project } from "@opencode-ai/schema/project"
  9. import { ProjectDirectories } from "@opencode-ai/schema/project-directories"
  10. import { PermissionV1 } from "@opencode-ai/schema/permission-v1"
  11. import { Prompt } from "@opencode-ai/schema/prompt"
  12. import { SessionPending } from "@opencode-ai/schema/session-pending"
  13. import { SessionMessage } from "@opencode-ai/schema/session-message"
  14. import { Workspace } from "@opencode-ai/schema/workspace"
  15. import { Command } from "@opencode-ai/schema/command"
  16. import { Connection } from "@opencode-ai/schema/connection"
  17. import { Credential } from "@opencode-ai/schema/credential"
  18. import { FileSystem } from "@opencode-ai/schema/filesystem"
  19. import { Integration } from "@opencode-ai/schema/integration"
  20. import { AI } from "@opencode-ai/schema/ai"
  21. import { LLM } from "@opencode-ai/schema/llm"
  22. import { Permission } from "@opencode-ai/schema/permission"
  23. import { Pty } from "@opencode-ai/schema/pty"
  24. import { Reference } from "@opencode-ai/schema/reference"
  25. import { Skill } from "@opencode-ai/schema/skill"
  26. import { AbsolutePath, DateTimeUtcFromMillis, optional, statics } from "@opencode-ai/schema/schema"
  27. test("Core reuses the canonical shared schemas", async () => {
  28. const schemaAgent = await import("@opencode-ai/schema/agent")
  29. const schemaSession = await import("@opencode-ai/schema/session")
  30. const [
  31. coreCommand,
  32. coreConnection,
  33. coreCredential,
  34. coreFileSystem,
  35. coreIntegration,
  36. coreLocation,
  37. coreAI,
  38. coreModel,
  39. corePermission,
  40. corePermissionV1,
  41. coreProjectCopy,
  42. corePty,
  43. coreProject,
  44. coreProvider,
  45. coreReference,
  46. coreSessionPending,
  47. coreSessionMessage,
  48. coreSkill,
  49. coreSchema,
  50. coreWorkspace,
  51. ] = await Promise.all([
  52. import("@opencode-ai/core/command"),
  53. import("@opencode-ai/core/integration/connection"),
  54. import("@opencode-ai/core/credential"),
  55. import("@opencode-ai/core/filesystem"),
  56. import("@opencode-ai/core/integration"),
  57. import("@opencode-ai/core/location"),
  58. import("@opencode-ai/ai"),
  59. import("@opencode-ai/core/model"),
  60. import("@opencode-ai/core/permission"),
  61. import("@opencode-ai/core/v1/permission"),
  62. import("@opencode-ai/core/project/copy"),
  63. import("@opencode-ai/core/pty"),
  64. import("@opencode-ai/core/project/schema"),
  65. import("@opencode-ai/core/provider"),
  66. import("@opencode-ai/core/reference"),
  67. import("@opencode-ai/core/session/pending"),
  68. import("@opencode-ai/core/session/message"),
  69. import("@opencode-ai/core/skill"),
  70. import("@opencode-ai/core/schema"),
  71. import("@opencode-ai/core/workspace"),
  72. ])
  73. const schemas = [
  74. [Agent.ID, schemaAgent.Agent.ID],
  75. [Agent.Name, schemaAgent.Agent.Name],
  76. [Agent.Color, schemaAgent.Agent.Color],
  77. [Agent.Info, schemaAgent.Agent.Info],
  78. [coreCommand.Info, Command.Info],
  79. [coreConnection.CredentialInfo, Connection.CredentialInfo],
  80. [coreConnection.EnvInfo, Connection.EnvInfo],
  81. [coreConnection.Info, Connection.Info],
  82. [coreCredential.ID, Credential.ID],
  83. [coreCredential.OAuth, Credential.OAuth],
  84. [coreCredential.Key, Credential.Key],
  85. [coreCredential.Value, Credential.Value],
  86. [coreFileSystem.Entry, FileSystem.Entry],
  87. [coreFileSystem.Submatch, FileSystem.Submatch],
  88. [coreFileSystem.Match, FileSystem.Match],
  89. [coreIntegration.ID, Integration.ID],
  90. [coreIntegration.MethodID, Integration.MethodID],
  91. [coreIntegration.OAuthMethod, Integration.OAuthMethod],
  92. [coreIntegration.KeyMethod, Integration.KeyMethod],
  93. [coreIntegration.EnvMethod, Integration.EnvMethod],
  94. [coreIntegration.Method, Integration.Method],
  95. [coreIntegration.Ref, Integration.Ref],
  96. [coreLocation.Ref, Location.Ref],
  97. [coreAI.ProviderMetadata, AI.ProviderMetadata],
  98. [coreAI.FinishReason, LLM.FinishReason],
  99. [coreModel.ID, Model.ID],
  100. [coreModel.VariantID, Model.VariantID],
  101. [coreModel.Ref, Model.Ref],
  102. [coreModel.Family, Model.Family],
  103. [coreModel.Capabilities, Model.Capabilities],
  104. [coreModel.Cost, Model.Cost],
  105. [coreModel.Info, Model.Info],
  106. [coreProvider.ID, Provider.ID],
  107. [coreProvider.Request, Provider.Request],
  108. [coreProvider.Info, Provider.Info],
  109. [corePermission.Effect, Permission.Effect],
  110. [corePermission.Rule, Permission.Rule],
  111. [corePermission.Ruleset, Permission.Ruleset],
  112. [corePermissionV1.Event, PermissionV1.Event],
  113. [coreProjectCopy.Event, ProjectDirectories.Event],
  114. [corePty.Info, Pty.Info],
  115. [corePty.Event, Pty.Event],
  116. [coreProject.ID, Project.ID],
  117. [coreProject.Current, Project.Current],
  118. [coreProject.Directory, Project.Directory],
  119. [coreProject.DirectoriesInput, Project.DirectoriesInput],
  120. [coreProject.Directories, Project.Directories],
  121. [coreReference.LocalSource, Reference.LocalSource],
  122. [coreReference.GitSource, Reference.GitSource],
  123. [coreReference.Source, Reference.Source],
  124. [Session.ID, schemaSession.Session.ID],
  125. [Session.Info, schemaSession.Session.Info],
  126. [Session.ListAnchor, schemaSession.Session.ListAnchor],
  127. [coreSessionPending.Delivery, SessionPending.Delivery],
  128. [coreSessionPending.Message, SessionPending.Message],
  129. [coreSessionPending.User, SessionPending.User],
  130. [coreSessionPending.Synthetic, SessionPending.Synthetic],
  131. [coreSessionMessage.ID, SessionMessage.ID],
  132. [coreSessionMessage.AssistantRetry, SessionMessage.AssistantRetry],
  133. [coreSessionMessage.AgentSelected, SessionMessage.AgentSelected],
  134. [coreSessionMessage.ModelSelected, SessionMessage.ModelSelected],
  135. [coreSessionMessage.User, SessionMessage.User],
  136. [coreSessionMessage.Synthetic, SessionMessage.Synthetic],
  137. [coreSessionMessage.System, SessionMessage.System],
  138. [coreSessionMessage.Shell, SessionMessage.Shell],
  139. [coreSessionMessage.ToolStateStreaming, SessionMessage.ToolStateStreaming],
  140. [coreSessionMessage.ToolStateRunning, SessionMessage.ToolStateRunning],
  141. [coreSessionMessage.ToolStateCompleted, SessionMessage.ToolStateCompleted],
  142. [coreSessionMessage.ToolStateError, SessionMessage.ToolStateError],
  143. [coreSessionMessage.ToolState, SessionMessage.ToolState],
  144. [coreSessionMessage.AssistantTool, SessionMessage.AssistantTool],
  145. [coreSessionMessage.AssistantText, SessionMessage.AssistantText],
  146. [coreSessionMessage.AssistantReasoning, SessionMessage.AssistantReasoning],
  147. [coreSessionMessage.AssistantContent, SessionMessage.AssistantContent],
  148. [coreSessionMessage.Assistant, SessionMessage.Assistant],
  149. [coreSessionMessage.Compaction, SessionMessage.Compaction],
  150. [coreSessionMessage.Info, SessionMessage.Info],
  151. [coreSkill.DirectorySource, Skill.DirectorySource],
  152. [coreSkill.UrlSource, Skill.UrlSource],
  153. [coreSkill.EmbeddedSource, Skill.EmbeddedSource],
  154. [coreSkill.Source, Skill.Source],
  155. [coreSkill.Info, Skill.Info],
  156. [coreSchema.optional, optional],
  157. [coreSchema.statics, statics],
  158. [coreWorkspace.ID, Workspace.ID],
  159. ]
  160. for (const [core, shared] of schemas) expect(core).toBe(shared)
  161. expect(Agent.Info.default(Agent.ID.make("test"))).toEqual(Agent.Info.default(Agent.ID.make("test")))
  162. expect(coreModel.Info.default(coreProvider.ID.make("test"), coreModel.ID.make("model"))).toEqual(
  163. Model.Info.default(Provider.ID.make("test"), Model.ID.make("model")),
  164. )
  165. expect(coreProvider.Info.empty(coreProvider.ID.make("test"))).toEqual(Provider.Info.empty(Provider.ID.make("test")))
  166. expect(Skill.Source.key(Skill.DirectorySource.make({ type: "directory", path: AbsolutePath.make("/tmp") }))).toBe(
  167. "directory:/tmp",
  168. )
  169. })
  170. test("shared record schemas construct and decode plain objects", () => {
  171. const made = Prompt.make({ text: "hello" })
  172. const decoded = Schema.decodeUnknownSync(Prompt)({ text: "hello" })
  173. const content = Schema.decodeUnknownSync(SessionMessage.AssistantText)({ type: "text", text: "hi" })
  174. expect(Object.getPrototypeOf(made)).toBe(Object.prototype)
  175. expect(Object.getPrototypeOf(decoded)).toBe(Object.prototype)
  176. expect(Object.getPrototypeOf(content)).toBe(Object.prototype)
  177. expect(Prompt.ast.annotations?.identifier).toBe("Prompt")
  178. expect(SessionMessage.AssistantText.ast.annotations?.identifier).toBe("Session.Message.Assistant.Text")
  179. expect(Prompt.equivalence(Prompt.make({ text: "hello" }), decoded)).toBe(true)
  180. expect(Prompt.fromUserMessage({ text: "hello" })).toEqual(made)
  181. expect(Workspace.ID.ascending("")).toStartWith("wrk_")
  182. })