mirror of
https://github.com/anomalyco/opencode.git
synced 2026-08-15 17:08:21 -04:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ed3b2a0d79 |
@@ -7,6 +7,7 @@ import { IntegrationSchema } from "./integration/schema"
|
|||||||
import { NonNegativeInt, withStatics } from "./schema"
|
import { NonNegativeInt, withStatics } from "./schema"
|
||||||
import { Identifier } from "./util/identifier"
|
import { Identifier } from "./util/identifier"
|
||||||
import { CredentialTable } from "./credential/sql"
|
import { CredentialTable } from "./credential/sql"
|
||||||
|
import { LayerNode } from "./effect/layer-node"
|
||||||
|
|
||||||
export const ID = Schema.String.pipe(
|
export const ID = Schema.String.pipe(
|
||||||
Schema.brand("Credential.ID"),
|
Schema.brand("Credential.ID"),
|
||||||
@@ -150,3 +151,4 @@ export const layer = Layer.effect(
|
|||||||
)
|
)
|
||||||
|
|
||||||
export const defaultLayer = layer.pipe(Layer.provide(Database.defaultLayer))
|
export const defaultLayer = layer.pipe(Layer.provide(Database.defaultLayer))
|
||||||
|
export const node = LayerNode.make(layer, [Database.node])
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import { Location } from "./location"
|
|||||||
import { AbsolutePath } from "./schema"
|
import { AbsolutePath } from "./schema"
|
||||||
import { SystemContext } from "./system-context/index"
|
import { SystemContext } from "./system-context/index"
|
||||||
import { SystemContextRegistry } from "./system-context/registry"
|
import { SystemContextRegistry } from "./system-context/registry"
|
||||||
import { LayerNode } from "./effect/layer-node"
|
|
||||||
|
|
||||||
class File extends Schema.Class<File>("InstructionContext.File")({
|
class File extends Schema.Class<File>("InstructionContext.File")({
|
||||||
path: AbsolutePath,
|
path: AbsolutePath,
|
||||||
@@ -88,9 +87,6 @@ export const layer = Layer.effectDiscard(
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const node = (location: LayerNode.Node<Location.Service>) =>
|
|
||||||
LayerNode.make(layer, [FSUtil.node, Global.node, location, SystemContextRegistry.node])
|
|
||||||
|
|
||||||
function render(files: ReadonlyArray<File>) {
|
function render(files: ReadonlyArray<File>) {
|
||||||
return files.map((file) => `Instructions from: ${file.path}\n${file.content}`).join("\n\n")
|
return files.map((file) => `Instructions from: ${file.path}\n${file.content}`).join("\n\n")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
export * as SystemContextRegistry from "./registry"
|
export * as SystemContextRegistry from "./registry"
|
||||||
|
|
||||||
import { Context, Effect, Layer, Ref, Scope } from "effect"
|
import { Context, Effect, Layer, Ref, Scope } from "effect"
|
||||||
import { LayerNode } from "../effect/layer-node"
|
|
||||||
import { SystemContext } from "./index"
|
import { SystemContext } from "./index"
|
||||||
|
|
||||||
export interface Entry {
|
export interface Entry {
|
||||||
@@ -45,5 +44,3 @@ export const layer = Layer.effect(
|
|||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const node = LayerNode.make(layer, [])
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { Context, Effect, Layer, Scope } from "effect"
|
|||||||
import { enableMapSet } from "immer"
|
import { enableMapSet } from "immer"
|
||||||
import { State } from "../state"
|
import { State } from "../state"
|
||||||
import { Tool } from "./tool"
|
import { Tool } from "./tool"
|
||||||
|
import { LayerNode } from "../effect/layer-node"
|
||||||
|
|
||||||
type Data = {
|
type Data = {
|
||||||
readonly entries: Map<string, Entry>
|
readonly entries: Map<string, Entry>
|
||||||
@@ -56,3 +57,5 @@ export const layer = Layer.effect(
|
|||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
export const node = LayerNode.make(layer, [])
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import path from "path"
|
|||||||
import { FSUtil } from "@opencode-ai/core/fs-util"
|
import { FSUtil } from "@opencode-ai/core/fs-util"
|
||||||
import { Global } from "@opencode-ai/core/global"
|
import { Global } from "@opencode-ai/core/global"
|
||||||
import { InstructionContext } from "@opencode-ai/core/instruction-context"
|
import { InstructionContext } from "@opencode-ai/core/instruction-context"
|
||||||
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
||||||
import { Location } from "@opencode-ai/core/location"
|
import { Location } from "@opencode-ai/core/location"
|
||||||
import { AbsolutePath } from "@opencode-ai/core/schema"
|
import { AbsolutePath } from "@opencode-ai/core/schema"
|
||||||
import { SystemContext } from "@opencode-ai/core/system-context"
|
import { SystemContext } from "@opencode-ai/core/system-context"
|
||||||
@@ -14,28 +13,7 @@ import { location } from "./fixture/location"
|
|||||||
import { tmpdir } from "./fixture/tmpdir"
|
import { tmpdir } from "./fixture/tmpdir"
|
||||||
import { testEffect } from "./lib/effect"
|
import { testEffect } from "./lib/effect"
|
||||||
|
|
||||||
const it = testEffect(LayerNode.buildLayer(FSUtil.node))
|
const it = testEffect(Layer.empty)
|
||||||
|
|
||||||
function provide(options: {
|
|
||||||
global: string
|
|
||||||
location: Location.Interface
|
|
||||||
filesystem?: FSUtil.Interface
|
|
||||||
}) {
|
|
||||||
const location = LayerNode.make(
|
|
||||||
Layer.succeed(Location.Service, Location.Service.of(options.location)),
|
|
||||||
[],
|
|
||||||
)
|
|
||||||
return Effect.provide(
|
|
||||||
LayerNode.buildLayer(LayerNode.group([InstructionContext.node(location), SystemContextRegistry.node]), {
|
|
||||||
replacements: [
|
|
||||||
LayerNode.replace(Global.node, Global.layerWith({ config: options.global })),
|
|
||||||
...(options.filesystem
|
|
||||||
? [LayerNode.replace(FSUtil.node, Layer.succeed(FSUtil.Service, FSUtil.Service.of(options.filesystem)))]
|
|
||||||
: []),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("InstructionContext", () => {
|
describe("InstructionContext", () => {
|
||||||
it.live("loads global and upward project AGENTS.md files as one aggregate context", () =>
|
it.live("loads global and upward project AGENTS.md files as one aggregate context", () =>
|
||||||
@@ -63,13 +41,20 @@ describe("InstructionContext", () => {
|
|||||||
|
|
||||||
const load = SystemContextRegistry.Service.pipe(
|
const load = SystemContextRegistry.Service.pipe(
|
||||||
Effect.flatMap((service) => service.load()),
|
Effect.flatMap((service) => service.load()),
|
||||||
provide({
|
Effect.provide(InstructionContext.layer.pipe(Layer.provideMerge(SystemContextRegistry.layer))),
|
||||||
global,
|
Effect.provide(FSUtil.defaultLayer),
|
||||||
location: location(
|
Effect.provide(Global.layerWith({ config: global })),
|
||||||
{ directory: AbsolutePath.make(directory) },
|
Effect.provide(
|
||||||
{ projectDirectory: AbsolutePath.make(project) },
|
Layer.succeed(
|
||||||
|
Location.Service,
|
||||||
|
Location.Service.of(
|
||||||
|
location(
|
||||||
|
{ directory: AbsolutePath.make(directory) },
|
||||||
|
{ projectDirectory: AbsolutePath.make(project) },
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
}),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
const initialized = yield* SystemContext.initialize(yield* load)
|
const initialized = yield* SystemContext.initialize(yield* load)
|
||||||
@@ -122,10 +107,15 @@ describe("InstructionContext", () => {
|
|||||||
yield* Effect.promise(() => fs.writeFile(file, ""))
|
yield* Effect.promise(() => fs.writeFile(file, ""))
|
||||||
const context = yield* SystemContextRegistry.Service.pipe(
|
const context = yield* SystemContextRegistry.Service.pipe(
|
||||||
Effect.flatMap((service) => service.load()),
|
Effect.flatMap((service) => service.load()),
|
||||||
provide({
|
Effect.provide(InstructionContext.layer.pipe(Layer.provideMerge(SystemContextRegistry.layer))),
|
||||||
global: path.join(tmp.path, "global"),
|
Effect.provide(FSUtil.defaultLayer),
|
||||||
location: location({ directory: AbsolutePath.make(tmp.path) }),
|
Effect.provide(Global.layerWith({ config: path.join(tmp.path, "global") })),
|
||||||
}),
|
Effect.provide(
|
||||||
|
Layer.succeed(
|
||||||
|
Location.Service,
|
||||||
|
Location.Service.of(location({ directory: AbsolutePath.make(tmp.path) })),
|
||||||
|
),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
expect((yield* SystemContext.initialize(context)).baseline).toBe(`Instructions from: ${file}\n`)
|
expect((yield* SystemContext.initialize(context)).baseline).toBe(`Instructions from: ${file}\n`)
|
||||||
@@ -136,14 +126,22 @@ describe("InstructionContext", () => {
|
|||||||
|
|
||||||
it.effect("preserves admitted instructions while observation is unavailable", () =>
|
it.effect("preserves admitted instructions while observation is unavailable", () =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const fs = yield* FSUtil.Service
|
const failingFS = Layer.effect(
|
||||||
|
FSUtil.Service,
|
||||||
|
FSUtil.Service.pipe(
|
||||||
|
Effect.map((fs) =>
|
||||||
|
FSUtil.Service.of({ ...fs, up: () => Effect.fail(new FSUtil.FileSystemError({ method: "up" })) }),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
).pipe(Layer.provide(FSUtil.defaultLayer))
|
||||||
const context = yield* SystemContextRegistry.Service.pipe(
|
const context = yield* SystemContextRegistry.Service.pipe(
|
||||||
Effect.flatMap((service) => service.load()),
|
Effect.flatMap((service) => service.load()),
|
||||||
provide({
|
Effect.provide(InstructionContext.layer.pipe(Layer.provideMerge(SystemContextRegistry.layer))),
|
||||||
global: "/global",
|
Effect.provide(failingFS),
|
||||||
location: location({ directory: AbsolutePath.make("/repo") }),
|
Effect.provide(Global.layerWith({ config: "/global" })),
|
||||||
filesystem: { ...fs, up: () => Effect.fail(new FSUtil.FileSystemError({ method: "up" })) },
|
Effect.provide(
|
||||||
}),
|
Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make("/repo") }))),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
@@ -160,18 +158,26 @@ describe("InstructionContext", () => {
|
|||||||
it.effect("preserves admitted instructions when a discovered file disappears before read", () =>
|
it.effect("preserves admitted instructions when a discovered file disappears before read", () =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const file = AbsolutePath.make("/repo/AGENTS.md")
|
const file = AbsolutePath.make("/repo/AGENTS.md")
|
||||||
const fs = yield* FSUtil.Service
|
const racingFS = Layer.effect(
|
||||||
|
FSUtil.Service,
|
||||||
|
FSUtil.Service.pipe(
|
||||||
|
Effect.map((fs) =>
|
||||||
|
FSUtil.Service.of({
|
||||||
|
...fs,
|
||||||
|
up: () => Effect.succeed([file]),
|
||||||
|
readFileStringSafe: () => Effect.succeed(undefined),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
).pipe(Layer.provide(FSUtil.defaultLayer))
|
||||||
const context = yield* SystemContextRegistry.Service.pipe(
|
const context = yield* SystemContextRegistry.Service.pipe(
|
||||||
Effect.flatMap((service) => service.load()),
|
Effect.flatMap((service) => service.load()),
|
||||||
provide({
|
Effect.provide(InstructionContext.layer.pipe(Layer.provideMerge(SystemContextRegistry.layer))),
|
||||||
global: "/global",
|
Effect.provide(racingFS),
|
||||||
location: location({ directory: AbsolutePath.make("/repo") }),
|
Effect.provide(Global.layerWith({ config: "/global" })),
|
||||||
filesystem: {
|
Effect.provide(
|
||||||
...fs,
|
Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make("/repo") }))),
|
||||||
up: () => Effect.succeed([file]),
|
),
|
||||||
readFileStringSafe: () => Effect.succeed(undefined),
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
@@ -188,25 +194,35 @@ describe("InstructionContext", () => {
|
|||||||
it.effect("canonicalizes upward discovery boundaries", () =>
|
it.effect("canonicalizes upward discovery boundaries", () =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
let observed: { targets: string[]; start: string; stop?: string } | undefined
|
let observed: { targets: string[]; start: string; stop?: string } | undefined
|
||||||
const fs = yield* FSUtil.Service
|
const observingFS = Layer.effect(
|
||||||
|
FSUtil.Service,
|
||||||
|
FSUtil.Service.pipe(
|
||||||
|
Effect.map((fs) =>
|
||||||
|
FSUtil.Service.of({
|
||||||
|
...fs,
|
||||||
|
up: (options) =>
|
||||||
|
Effect.sync(() => {
|
||||||
|
observed = options
|
||||||
|
return []
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
).pipe(Layer.provide(FSUtil.defaultLayer))
|
||||||
|
|
||||||
yield* SystemContextRegistry.Service.pipe(
|
yield* SystemContextRegistry.Service.pipe(
|
||||||
Effect.flatMap((service) => service.load()),
|
Effect.flatMap((service) => service.load()),
|
||||||
provide({
|
Effect.provide(InstructionContext.layer.pipe(Layer.provideMerge(SystemContextRegistry.layer))),
|
||||||
global: "/global",
|
Effect.provide(observingFS),
|
||||||
location: location(
|
Effect.provide(Global.layerWith({ config: "/global" })),
|
||||||
{ directory: AbsolutePath.make("/repo/") },
|
Effect.provide(
|
||||||
{ projectDirectory: AbsolutePath.make("/repo") },
|
Layer.succeed(
|
||||||
|
Location.Service,
|
||||||
|
Location.Service.of(
|
||||||
|
location({ directory: AbsolutePath.make("/repo/") }, { projectDirectory: AbsolutePath.make("/repo") }),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
filesystem: {
|
),
|
||||||
...fs,
|
|
||||||
up: (options) =>
|
|
||||||
Effect.sync(() => {
|
|
||||||
observed = options
|
|
||||||
return []
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(observed).toEqual({
|
expect(observed).toEqual({
|
||||||
@@ -222,15 +238,22 @@ describe("InstructionContext", () => {
|
|||||||
const previous = process.env.OPENCODE_DISABLE_PROJECT_CONFIG
|
const previous = process.env.OPENCODE_DISABLE_PROJECT_CONFIG
|
||||||
let scanned = false
|
let scanned = false
|
||||||
process.env.OPENCODE_DISABLE_PROJECT_CONFIG = "1"
|
process.env.OPENCODE_DISABLE_PROJECT_CONFIG = "1"
|
||||||
const fs = yield* FSUtil.Service
|
|
||||||
|
|
||||||
yield* SystemContextRegistry.Service.pipe(
|
yield* SystemContextRegistry.Service.pipe(
|
||||||
Effect.flatMap((service) => service.load()),
|
Effect.flatMap((service) => service.load()),
|
||||||
provide({
|
Effect.provide(InstructionContext.layer.pipe(Layer.provideMerge(SystemContextRegistry.layer))),
|
||||||
global: "/global",
|
Effect.provide(
|
||||||
location: location({ directory: AbsolutePath.make("/repo") }),
|
Layer.effect(
|
||||||
filesystem: { ...fs, up: () => Effect.sync(() => ((scanned = true), [])) },
|
FSUtil.Service,
|
||||||
}),
|
FSUtil.Service.pipe(
|
||||||
|
Effect.map((fs) => FSUtil.Service.of({ ...fs, up: () => Effect.sync(() => ((scanned = true), [])) })),
|
||||||
|
),
|
||||||
|
).pipe(Layer.provide(FSUtil.defaultLayer)),
|
||||||
|
),
|
||||||
|
Effect.provide(Global.layerWith({ config: "/global" })),
|
||||||
|
Effect.provide(
|
||||||
|
Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make("/repo") }))),
|
||||||
|
),
|
||||||
Effect.ensuring(
|
Effect.ensuring(
|
||||||
Effect.sync(() => {
|
Effect.sync(() => {
|
||||||
if (previous === undefined) delete process.env.OPENCODE_DISABLE_PROJECT_CONFIG
|
if (previous === undefined) delete process.env.OPENCODE_DISABLE_PROJECT_CONFIG
|
||||||
@@ -246,17 +269,26 @@ describe("InstructionContext", () => {
|
|||||||
it.effect("does not discover project instructions outside the canonical project root", () =>
|
it.effect("does not discover project instructions outside the canonical project root", () =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
let scanned = false
|
let scanned = false
|
||||||
const fs = yield* FSUtil.Service
|
|
||||||
yield* SystemContextRegistry.Service.pipe(
|
yield* SystemContextRegistry.Service.pipe(
|
||||||
Effect.flatMap((service) => service.load()),
|
Effect.flatMap((service) => service.load()),
|
||||||
provide({
|
Effect.provide(InstructionContext.layer.pipe(Layer.provideMerge(SystemContextRegistry.layer))),
|
||||||
global: "/global",
|
Effect.provide(
|
||||||
location: location(
|
Layer.effect(
|
||||||
{ directory: AbsolutePath.make("/outside") },
|
FSUtil.Service,
|
||||||
{ projectDirectory: AbsolutePath.make("/repo") },
|
FSUtil.Service.pipe(
|
||||||
|
Effect.map((fs) => FSUtil.Service.of({ ...fs, up: () => Effect.sync(() => ((scanned = true), [])) })),
|
||||||
|
),
|
||||||
|
).pipe(Layer.provide(FSUtil.defaultLayer)),
|
||||||
|
),
|
||||||
|
Effect.provide(Global.layerWith({ config: "/global" })),
|
||||||
|
Effect.provide(
|
||||||
|
Layer.succeed(
|
||||||
|
Location.Service,
|
||||||
|
Location.Service.of(
|
||||||
|
location({ directory: AbsolutePath.make("/outside") }, { projectDirectory: AbsolutePath.make("/repo") }),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
filesystem: { ...fs, up: () => Effect.sync(() => ((scanned = true), [])) },
|
),
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(scanned).toBe(false)
|
expect(scanned).toBe(false)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { describe, expect } from "bun:test"
|
|||||||
import { Effect, Equal, Hash, Layer, Schema } from "effect"
|
import { Effect, Equal, Hash, Layer, Schema } from "effect"
|
||||||
import { Tool } from "@opencode-ai/core/public"
|
import { Tool } from "@opencode-ai/core/public"
|
||||||
import { Catalog } from "@opencode-ai/core/catalog"
|
import { Catalog } from "@opencode-ai/core/catalog"
|
||||||
|
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
||||||
import { LocationServiceMap } from "@opencode-ai/core/location-layer"
|
import { LocationServiceMap } from "@opencode-ai/core/location-layer"
|
||||||
import { Location } from "@opencode-ai/core/location"
|
import { Location } from "@opencode-ai/core/location"
|
||||||
import { PluginBoot } from "@opencode-ai/core/plugin/boot"
|
import { PluginBoot } from "@opencode-ai/core/plugin/boot"
|
||||||
@@ -24,26 +25,20 @@ import { Reference } from "../src/reference"
|
|||||||
import { ToolRegistry } from "../src/tool/registry"
|
import { ToolRegistry } from "../src/tool/registry"
|
||||||
import { ApplicationTools } from "../src/tool/application-tools"
|
import { ApplicationTools } from "../src/tool/application-tools"
|
||||||
|
|
||||||
const applicationTools = ApplicationTools.layer
|
const locationServices = LayerNode.make(LocationServiceMap.layer, [
|
||||||
|
ApplicationTools.node,
|
||||||
|
Project.node,
|
||||||
|
EventV2.node,
|
||||||
|
Credential.node,
|
||||||
|
Npm.node,
|
||||||
|
ModelsDev.node,
|
||||||
|
FSUtil.node,
|
||||||
|
Global.node,
|
||||||
|
])
|
||||||
const it = testEffect(
|
const it = testEffect(
|
||||||
Layer.merge(
|
LayerNode.buildLayer(LayerNode.group([ApplicationTools.node, Database.node, EventV2.node, locationServices]), {
|
||||||
Layer.mergeAll(applicationTools, Database.defaultLayer, EventV2.defaultLayer),
|
replacements: [LayerNode.replace(Database.node, Database.layerFromPath(":memory:").pipe(Layer.fresh))],
|
||||||
LocationServiceMap.layer.pipe(
|
}),
|
||||||
Layer.provide(applicationTools),
|
|
||||||
Layer.provide(
|
|
||||||
Layer.mergeAll(
|
|
||||||
Project.defaultLayer,
|
|
||||||
EventV2.defaultLayer,
|
|
||||||
Credential.defaultLayer,
|
|
||||||
Credential.layer.pipe(Layer.provide(Database.layerFromPath(":memory:").pipe(Layer.fresh))),
|
|
||||||
Npm.defaultLayer,
|
|
||||||
ModelsDev.defaultLayer,
|
|
||||||
FSUtil.defaultLayer,
|
|
||||||
Global.defaultLayer,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
describe("LocationServiceMap", () => {
|
describe("LocationServiceMap", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user