Skip to content
Open
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
519 changes: 519 additions & 0 deletions packages/core/src/codemode/bun.ts

Large diffs are not rendered by default.

33 changes: 27 additions & 6 deletions packages/core/src/codemode/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ import { CallID } from "@opencode/schema/tool"
import { Clock, Context, Effect, Ref, Schema, Scope, Semaphore } from "effect"
import { toSessionError } from "../session/to-session-error.js"
import { definition, normalizedName } from "../tool/runtime.js"
import { Environment } from "../environment/index.js"
import { FileAccess } from "../file-access.js"
import { FileMutation } from "../file-mutation.js"
import { FileWrite } from "../file-write.js"
import { Location } from "../location.js"
import { Permission } from "../permission.js"
import { CodeModeBun } from "./bun.js"
import { CodeModeCatalog } from "./catalog.js"
import { CodeModeExecution } from "./execution.js"
import { CodeModeFetch } from "./fetch.js"
Expand Down Expand Up @@ -70,6 +75,15 @@ class Run extends Context.Service<
}
>()("@opencode/CodeModeTool/Run") {}

/** The session's Location and file services, which a run with the experimental `path` and `Bun` globals acts through. */
export type Services = Location.Service | CodeModeBun.Services

/** Those services alone, so a run they are given to keeps its own scope and the rest of its context. */
export const services = Effect.map(
Effect.context<Services>(),
Context.pick(Location.Service, Environment.Service, FileAccess.Service, FileMutation.Service, FileWrite.Service),
)

export type Inventory = {
readonly tools: ReadonlyMap<string, Info>
readonly namespaces?: ReadonlyMap<string, ToolNamespace>
Expand All @@ -90,8 +104,8 @@ export const create = (
inventory: Inventory,
executeTool: (name: string, tool: Info, input: unknown, context: ToolContext) => Effect.Effect<Result, Error>,
permission: Permission.Interface,
/** With `full_codemode`, the session's Location, which turns on the experimental `path` global. */
location?: Location.Interface,
/** With `full_codemode`, the session's services, which turn on the experimental `path` and `Bun` globals. */
services?: Context.Context<Services>,
) => {
return {
name: "execute",
Expand Down Expand Up @@ -128,7 +142,9 @@ export const create = (
)
const result = yield* runtime(inventory)
.execute(code, undefined, {
extensions: location ? [CodeModeFetch.extension, CodeModePath.extension] : [CodeModeFetch.extension],
extensions: services
? [CodeModeFetch.extension, CodeModePath.extension, CodeModeBun.extension]
: [CodeModeFetch.extension],
})
.pipe(
Effect.provideService(Run, {
Expand Down Expand Up @@ -165,7 +181,7 @@ export const create = (
// Readers outlive the fetch call that returned them, up to the end of this program's scope.
Effect.provideService(CodeModeExecution.Service, { ...context, signal: yield* Effect.abortSignal }),
Effect.provideService(Permission.Service, permission),
(program) => (location ? Effect.provideService(program, Location.Service, location) : program),
(program) => (services ? Effect.provideContext(program, services) : program),
)
const toolCalls = yield* Ref.get(calls)
const collected = (yield* Ref.get(files))
Expand Down Expand Up @@ -229,6 +245,8 @@ function progressHooks(record: (update: (items: Array<ExecuteCall>) => Array<Exe
case "fetch":
return start(call, { tool: call.name, status: "running", input: CodeModeFetch.display(call.args) })
default:
if (call.extension === "bun")
return start(call, { tool: "Bun", status: "running", input: CodeModeBun.display(call.name, call.args) })
return Effect.void
}
},
Expand Down Expand Up @@ -291,9 +309,12 @@ function runtime(inventory: Inventory) {
if (cached) return cached
const tools = inventoryTools(inventory)
// Every extension a run may get; `create` picks each run's.
const created = CodeMode.make<typeof tools, [typeof CodeModeFetch.extension, typeof CodeModePath.extension]>({
const created = CodeMode.make<
typeof tools,
[typeof CodeModeFetch.extension, typeof CodeModePath.extension, typeof CodeModeBun.extension]
>({
tools,
extensions: [CodeModeFetch.extension, CodeModePath.extension],
extensions: [CodeModeFetch.extension, CodeModePath.extension, CodeModeBun.extension],
hooks: runHooks,
})
runtimes.set(inventory, created)
Expand Down
15 changes: 10 additions & 5 deletions packages/core/src/file-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface Interface {
targets: ReadonlyArray<string>,
) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
readonly write: (input: WriteInput) => Effect.Effect<WriteResult, Environment.Failed>
/** Add bytes to the end of a file, creating it when missing. */
readonly append: (input: WriteInput) => Effect.Effect<WriteResult, Environment.Failed>
/** Write text while retaining an existing UTF-8 BOM and emitting at most one BOM. */
readonly writeTextPreservingBom: (
input: TextWriteInput,
Expand Down Expand Up @@ -93,21 +95,24 @@ const layer = Layer.effect(
existed,
})

const write = Effect.fn("FileMutation.write")((input: WriteInput) =>
const put = (input: WriteInput, store: Files["write"]) =>
withTargetLock(input.target)(
Effect.gen(function* () {
const existed = yield* environment.files.stat(input.target.absolute).pipe(
Effect.as(true),
Effect.catchTag("Environment.NotFound", () => Effect.succeed(false)),
)
yield* environment.files.write(
yield* store(
input.target.absolute,
typeof input.content === "string" ? new TextEncoder().encode(input.content) : input.content,
)
return writeResult(input.target, existed)
}),
),
)
)

const write = Effect.fn("FileMutation.write")((input: WriteInput) => put(input, environment.files.write))

const append = Effect.fn("FileMutation.append")((input: WriteInput) => put(input, environment.files.append))

const writeTextPreservingBom = Effect.fn("FileMutation.writeTextPreservingBom")((input: TextWriteInput) =>
withTargetLock(input.target)(
Expand All @@ -124,7 +129,7 @@ const layer = Layer.effect(
),
)

return Service.of({ withLock, write, writeTextPreservingBom })
return Service.of({ withLock, write, append, writeTextPreservingBom })
}),
)

Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/session/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { Context, Effect, Layer } from "effect"
import { Agent } from "../agent.js"
import type { SessionRequestKind } from "@opencode/plugin/effect/session"
import { CodeModeInstructions } from "../codemode/instructions.js"
import { CodeModeTool } from "../codemode/tool.js"
import { Database } from "../database/database.js"
import { Environment } from "../environment/index.js"
import { FileAccess } from "../file-access.js"
import { FileMutation } from "../file-mutation.js"
import { FileWrite } from "../file-write.js"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import { InstructionDiscovery } from "../instruction-discovery.js"
import { Instructions } from "../instructions/index.js"
Expand Down Expand Up @@ -90,6 +95,7 @@ const layer = Layer.effect(
const skillInstructions = yield* SkillInstructions.Service
const store = yield* SessionStore.Service
const registry = yield* Tool.Service
const codeMode = yield* CodeModeTool.services

const resolveModel = (
session: SessionSchema.Info,
Expand Down Expand Up @@ -146,7 +152,7 @@ const layer = Layer.effect(
const permissions = Permission.merge(agent.info.permissions, session.permissions ?? [])
const loaded = yield* Effect.all(
{
tools: registry.snapshot(permissions, location),
tools: registry.snapshot(permissions, codeMode),
builtins: builtins.load(),
discovery: discovery.load(),
skills: skillInstructions.load(permissions),
Expand Down Expand Up @@ -219,6 +225,10 @@ export const node = makeLocationNode({
Agent.node,
Model.node,
Database.node,
Environment.node,
FileAccess.node,
FileMutation.node,
FileWrite.node,
InstructionBuiltIns.node,
InstructionDiscovery.node,
InstructionEntry.node,
Expand Down
23 changes: 14 additions & 9 deletions packages/core/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { CodeModeJob } from "./codemode/job.js"
import { CodeModeTool } from "./codemode/tool.js"
import { Flag } from "./flag.js"
import { Image } from "./image.js"
import type { Location } from "./location.js"
import { Permission } from "./permission.js"
import { PluginHooks } from "./plugin/hooks.js"
import { SessionMessage } from "./session/message.js"
Expand Down Expand Up @@ -46,8 +45,14 @@ type Data = {

export interface Interface extends State.Transformable<Editor> {
readonly list: () => Effect.Effect<ReadonlyArray<Tool.Info & { readonly id: string }>>
/** With `full_codemode`, Code Mode gets the experimental `path` global, resolving against `location`'s directory. */
readonly snapshot: (permissions?: Permission.Ruleset, location?: Location.Interface) => Effect.Effect<Snapshot>
/**
* With `full_codemode`, Code Mode gets the experimental `path` and `Bun` globals, which act through `codeMode`: the
* session's Location and file services.
*/
readonly snapshot: (
permissions?: Permission.Ruleset,
codeMode?: Context.Context<CodeModeTool.Services>,
) => Effect.Effect<Snapshot>
/**
* Runs a Code Mode program with no model step over only the Code Mode tools named in `tools` (still subject to
* whole-tool permission filtering and each tool's own executor policy), with no `fetch`.
Expand Down Expand Up @@ -204,7 +209,7 @@ const layer = Layer.effect(
| {
data: Data
names: string
path: boolean
full: boolean
inventory: CodeModeTool.Inventory
value: CodeModeCatalog.Inventory
definition: ToolDefinition
Expand Down Expand Up @@ -268,9 +273,9 @@ const layer = Layer.effect(
transform: state.transform,
reload: state.reload,
list: () => Effect.sync(() => Array.from(state.get().tools.values())),
snapshot: Effect.fn("Tool.snapshot")((permissions, location) =>
snapshot: Effect.fn("Tool.snapshot")((permissions, codeMode) =>
Effect.map(flags.enabled("full_codemode"), (enabled) => {
const path = enabled && location !== undefined
const full = enabled && codeMode !== undefined
const data = state.get()
const active = new Map<string, Tool.Info>()
const rules = permissions ?? []
Expand All @@ -289,14 +294,14 @@ const layer = Layer.effect(
// fresh, but share the inventory, whose prepared runtime and much larger rendered catalog
// are cached by identity, and the execute tool's definition across steps.
const cached =
catalog?.data === data && catalog.names === names && catalog.path === path ? catalog : undefined
catalog?.data === data && catalog.names === names && catalog.full === full ? catalog : undefined
const codeModeInventory = cached?.inventory ?? { tools: codeModeTools, namespaces: data.namespaces }
const codeModeTool = codeModeEnabled
? CodeModeTool.create(
codeModeInventory,
(name, tool, input, context) => run(codeModeInventory.tools, tool, name, input, context),
permission,
path ? location : undefined,
full ? codeMode : undefined,
)
: undefined
const codeModeCatalog = !codeModeEnabled
Expand All @@ -307,7 +312,7 @@ const layer = Layer.effect(
catalog = {
data,
names,
path,
full,
inventory: codeModeInventory,
value: codeModeCatalog,
definition: codeModeDefinition,
Expand Down
18 changes: 18 additions & 0 deletions packages/core/test/file-mutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ describe("FileMutation", () => {
),
)

it.live("appends bytes to a file, creating it when missing", () =>
withTempDir(({ path: directory }) =>
Effect.gen(function* () {
const access = yield* FileAccess.Service
const files = yield* FileMutation.Service
const target = yield* access.resolve({ path: "log.txt" })

expect(yield* files.append({ target, content: "one\n" })).toMatchObject({ existed: false })
expect(yield* files.append({ target, content: new Uint8Array([0xff, 0x0a]) })).toMatchObject({ existed: true })
expect([...(yield* Effect.promise(() => fs.readFile(target.absolute)))]).toEqual([
...Buffer.from("one\n"),
0xff,
0x0a,
])
}).pipe(provide(directory)),
),
)

it.live("preserves exactly one BOM for text writes and normalizes created text", () =>
withTempDir(({ path: directory }) =>
Effect.gen(function* () {
Expand Down
42 changes: 42 additions & 0 deletions packages/core/test/fixture/codemode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { CodeModeTool } from "@opencode/core/codemode/tool"
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
import { Environment } from "@opencode/core/environment/index"
import { FileAccess } from "@opencode/core/file-access"
import { FileMutation } from "@opencode/core/file-mutation"
import { FileWrite } from "@opencode/core/file-write"
import { Formatter } from "@opencode/core/formatter"
import { Location } from "@opencode/core/location"
import { Permission } from "@opencode/core/permission"
import { AbsolutePath } from "@opencode/core/schema"
import { LayerNode } from "@opencode/util/effect/layer-node"
import { Effect, Layer } from "effect"
import { permissionLayer } from "../lib/permission"
import { hostEnvironmentLayer } from "./environment"
import { location } from "./location"

/** What a session at `directory` gives Code Mode with `full_codemode`: its Location and file services on the host. */
export const codeModeServices = (
directory: string,
permission: Partial<Permission.Interface> = { assert: () => Effect.void },
formatter: Formatter.Interface["file"] = () => Effect.succeed(false),
) =>
Effect.runSync(
CodeModeTool.services.pipe(
Effect.provide(
AppNodeBuilder.build(
LayerNode.group([Environment.node, FileAccess.node, FileMutation.node, FileWrite.node, Location.node]),
[
Environment.node.replace(hostEnvironmentLayer),
Location.node.replace(
Layer.succeed(
Location.Service,
Location.Service.of(location({ directory: AbsolutePath.make(directory) })),
),
),
Formatter.node.replace(Layer.mock(Formatter.Service, { file: formatter })),
Permission.node.replace(permissionLayer(permission)),
],
),
),
),
)
Loading
Loading