diff --git a/packages/core/test/tool-read.test.ts b/packages/core/test/tool-read.test.ts index 10cc0fd9754..c19a4e642aa 100644 --- a/packages/core/test/tool-read.test.ts +++ b/packages/core/test/tool-read.test.ts @@ -4,7 +4,6 @@ import path from "path" import { pathToFileURL } from "url" import { Effect, Exit, Layer, Result } from "effect" import { Config } from "@opencode/core/config" -import { Document, Info } from "@opencode/schema/config" import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" import { LayerNode } from "@opencode/util/effect/layer-node" import { FSUtil } from "@opencode/util/fs-util" @@ -28,7 +27,7 @@ import { SessionInstructions } from "@opencode/core/session/instructions" import { Environment } from "@opencode/core/environment/index" import { testEffect } from "./lib/effect" import { permissionLayer } from "./lib/permission" -import { toolIdentity, executeTool, registerToolPlugin, toolDefinitions, codeModeListings } from "./lib/tool" +import { toolIdentity, executeTool, registerToolPlugin, toolDefinitions } from "./lib/tool" const readToolNode = makeLocationNode({ name: "test/read-tool-plugin", @@ -114,8 +113,6 @@ const permission = permissionLayer({ allows: (input) => Effect.succeed(allow && !input.resources.some((resource) => resource === deniedResource)), }) const config = Config.testLayer() -const nativeDescription = - "Read the contents of a file or directory. Supports text files, images, and PDFs. Each text line is prefixed by its 1-based line number as : . The prefix is for reference and is not part of the file content. Directory entries are returned one per line. Use offset and limit to read large files or directories in sections. Prefer one larger read over many small slices, and use grep to find specific content in large files." const imageLayer = AppNodeBuilder.build(Image.node) const locationLayer = Layer.succeed( Location.Service, @@ -124,28 +121,22 @@ const locationLayer = Layer.succeed( const unavailableImage = Layer.mock(Image.Service, { normalize: () => Effect.fail(new Image.ResizerUnavailableError()), }) -const readLayer = (imageLayer: Layer.Layer, configuration = config) => +const readLayer = (imageLayer: Layer.Layer) => Layer.mergeAll( AppNodeBuilder.build(LayerNode.group([Tool.node, readToolNode, Skill.node]), [ ReadToolFileSystem.node.replace(reader), Environment.node.replace(hostEnvironmentLayer), Permission.node.replace(permission), - Config.node.replace(configuration), + Config.node.replace(config), Image.node.replace(imageLayer), Location.node.replace(locationLayer), Global.node.replace(Global.layerWith({ data: Global.Path.data })), ]), // Merge by reference so Config.Test and Image.Service resolve to the memoized instances. - configuration, + config, imageLayer, ) const it = testEffect(readLayer(imageLayer)) -const codeModeIt = testEffect( - readLayer( - imageLayer, - Config.testLayer([new Document({ type: "document", info: new Info({ experimental: { full_codemode: true } }) })]), - ), -) // Real-file tests provide their own layer per temp folder. const live = testEffect(Layer.empty) // Reads through the real filesystem with the location at `directory`. @@ -198,82 +189,6 @@ describe("ReadTool", () => { readOverride = undefined }) - it.effect("keeps read native with line instructions and no image guidance without full_codemode", () => - Effect.gen(function* () { - const registry = yield* Tool.Service - const snapshot = yield* registry.snapshot() - const read = snapshot.definitions.find((tool) => tool.name === "read") - - expect(read?.description).toBe(nativeDescription) - expect(codeModeListings(snapshot.codeModeCatalog!).map((entry) => entry.path)).not.toContain("read") - }), - ) - - codeModeIt.effect("moves read into Code Mode with attachment guidance and no native line instructions", () => - Effect.gen(function* () { - const registry = yield* Tool.Service - const snapshot = yield* registry.snapshot() - const read = codeModeListings(snapshot.codeModeCatalog!).find((entry) => entry.path === "read") - - expect(snapshot.definitions.map((tool) => tool.name)).toEqual(["execute"]) - expect(read?.line).toContain("Reading an image or PDF automatically attaches it to the execute result") - expect(read?.line).toContain("even if the program doesn't return or print the media reference") - const registered = (yield* registry.list()).find((tool) => tool.id === "read") - expect(registered?.description).toBe( - "Read the contents of a file or directory. Supports text files, images, and PDFs. Reading an image or PDF automatically attaches it to the execute result, even if the program doesn't return or print the media reference. Use offset and limit to read large files or directories in sections. Prefer one larger read over many small slices, and use grep to find specific content in large files.", - ) - expect(registered?.description).not.toContain("Each text line is prefixed") - expect(registered?.description).not.toContain("Directory entries are returned one per line") - - const result = yield* executeTool(registry, { - sessionID, - ...toolIdentity, - call: { - type: "tool-call", - id: "call-read-code-mode", - name: "execute", - input: { code: 'return await tools.read({ path: "README.md" })' }, - }, - }) - expect(result).toMatchObject({ - status: "completed", - output: { output: JSON.stringify({ type: "text", content: "hello", offset: 1, lineCount: 1 }) }, - }) - }), - ) - - codeModeIt.effect("attaches Code Mode image and PDF reads even when their references are not returned", () => - Effect.gen(function* () { - const registry = yield* Tool.Service - for (const file of [ - { - name: "pixel.png", - mime: "image/png", - data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=", - }, - { name: "document.pdf", mime: "application/pdf", data: Buffer.from("%PDF-1.7\ncontent").toString("base64") }, - ]) { - readResult = mediaResult(file.name, file.mime, file.data) - const result = yield* executeTool(registry, { - sessionID, - ...toolIdentity, - call: { - type: "tool-call", - id: `call-read-${file.name}`, - name: "execute", - input: { code: `await tools.read({ path: ${JSON.stringify(file.name)} }); return "done"` }, - }, - }) - - expect(result).toMatchObject({ status: "completed", output: { output: "done" } }) - expect(result.content).toEqual([ - { type: "text", text: "done" }, - { type: "file", uri: `data:${file.mime};base64,${file.data}`, mime: file.mime, name: file.name }, - ]) - } - }), - ) - it.effect("registers, authorizes, and reads through the location filesystem", () => Effect.gen(function* () { const registry = yield* Tool.Service diff --git a/packages/core/test/tool-search.test.ts b/packages/core/test/tool-search.test.ts index d25a8c3c9b2..bd946a418eb 100644 --- a/packages/core/test/tool-search.test.ts +++ b/packages/core/test/tool-search.test.ts @@ -1,10 +1,7 @@ import { describe, expect } from "bun:test" import fs from "fs/promises" import path from "path" -import { Context, Effect, Layer, PubSub, Stream } from "effect" -import { Document, Info, type Entry } from "@opencode/schema/config" -import { Event } from "@opencode/schema/event" -import { ConfigExperimental } from "@opencode/schema/config/experimental" +import { Effect, Layer } from "effect" import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" import { Config } from "@opencode/core/config" import { makeLocationNode } from "@opencode/util/effect/app-node" @@ -25,63 +22,50 @@ import { location } from "./fixture/location" import { tmpdir, tmpdirScoped, withTempDir } from "./fixture/tmpdir" import { it } from "./lib/effect" import { permissionLayer } from "./lib/permission" -import { codeModeListings, executeTool, registerToolPlugin, toolIdentity, waitForTool } from "./lib/tool" +import { executeTool, registerToolPlugin, toolIdentity } from "./lib/tool" const sessionID = Session.ID.make("ses_search_tool_test") -const configUpdated = () => ({ id: Event.ID.create(), created: Date.now(), type: "config.updated" as const, data: {} }) +const searchToolsNode = makeLocationNode({ + name: "test/search-tool-plugins", + layer: Layer.effectDiscard(Effect.all([registerToolPlugin(GlobTool.Plugin), registerToolPlugin(GrepTool.Plugin)])), + deps: [ + Config.node, + Environment.node, + FileAccess.node, + Flag.node, + Location.node, + Permission.node, + Ripgrep.node, + Tool.node, + ], +}) const withTools = ( directory: string, - body: (registry: Tool.Interface, configure: (entries: Entry[]) => Effect.Effect) => Effect.Effect, + body: (registry: Tool.Interface) => Effect.Effect, assertions?: Permission.AssertInput[], - entries: Entry[] = [], ) => Effect.gen(function* () { - const config = yield* Layer.build(Config.testLayer(entries)) - const updates = yield* PubSub.unbounded>() - const event = { subscribe: () => Stream.fromPubSub(updates) } - const configure = (next: Entry[]) => - Context.get(config, Config.Test) - .setEntries(next) - .pipe(Effect.andThen(PubSub.publish(updates, configUpdated())), Effect.asVoid) - const searchToolsNode = makeLocationNode({ - name: "test/search-tool-plugins", - layer: Layer.effectDiscard( - Effect.all([registerToolPlugin(GlobTool.Plugin, { event }), registerToolPlugin(GrepTool.Plugin, { event })]), - ), - deps: [ - Config.node, - Environment.node, - FileAccess.node, - Flag.node, - Location.node, - Permission.node, - Ripgrep.node, - Tool.node, - ], - }) - return yield* Effect.gen(function* () { - const registry = yield* Tool.Service - return yield* body(registry, configure) - }).pipe( - Effect.provide( - AppNodeBuilder.build(LayerNode.group([Tool.node, searchToolsNode]), [ - Config.node.replace(Layer.succeedContext(config)), - Location.node.replace( - Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))), - ), - Permission.node.replace( - permissionLayer({ - assert: (input) => - Effect.sync(() => { - assertions?.push(input) - }), - }), - ), - ]), - ), - ) - }).pipe(Effect.scoped) + const registry = yield* Tool.Service + return yield* body(registry) + }).pipe( + Effect.provide( + AppNodeBuilder.build(LayerNode.group([Tool.node, searchToolsNode]), [ + Config.node.replace(Config.testLayer()), + Location.node.replace( + Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))), + ), + Permission.node.replace( + permissionLayer({ + assert: (input) => + Effect.sync(() => { + assertions?.push(input) + }), + }), + ), + ]), + ), + ) const call = (name: "glob" | "grep", input: unknown) => ({ sessionID, @@ -89,44 +73,7 @@ const call = (name: "glob" | "grep", input: unknown) => ({ call: { type: "tool-call" as const, id: `call-${name}`, name, input }, }) -const fullCodeMode = new Document({ - type: "document", - info: new Info({ experimental: new ConfigExperimental.Info({ full_codemode: true }) }), -}) - -const exposed = (registry: Tool.Interface) => - registry - .snapshot() - .pipe( - Effect.map((snapshot) => [ - ...snapshot.definitions.map((tool) => tool.name), - ...(snapshot.codeModeCatalog ? codeModeListings(snapshot.codeModeCatalog).map((entry) => entry.path) : []), - ]), - ) - describe("search tools", () => { - it.live("are absent with full_codemode and return when it is turned off", () => - withTempDir((tmp) => - withTools( - tmp.path, - (registry, configure) => - Effect.gen(function* () { - const names = yield* exposed(registry) - expect(names).not.toContain("glob") - expect(names).not.toContain("grep") - expect(yield* executeTool(registry, call("grep", { pattern: "needle" }))).toMatchObject({ status: "error" }) - - yield* configure([]) - yield* waitForTool(registry, "grep") - yield* waitForTool(registry, "glob") - expect(yield* exposed(registry)).toEqual(expect.arrayContaining(["glob", "grep"])) - }), - undefined, - [fullCodeMode], - ), - ), - ) - for (const name of ["glob", "grep"] as const) { it.live(`${name} distinguishes a malformed filename glob from an empty search`, () => withTempDir((tmp) => diff --git a/packages/core/test/tool-shell.test.ts b/packages/core/test/tool-shell.test.ts index 0c8e7acd5d2..bbc4a19ccf8 100644 --- a/packages/core/test/tool-shell.test.ts +++ b/packages/core/test/tool-shell.test.ts @@ -52,7 +52,7 @@ import { offlineModels } from "./fixture/models" import { testEffect } from "./lib/effect" import { permissionLayer } from "./lib/permission" import { Expected } from "./lib/session-message" -import { codeModeListings, toolIdentity, executeTool, registerToolPlugin, toolDefinitions } from "./lib/tool" +import { toolIdentity, executeTool, registerToolPlugin, toolDefinitions } from "./lib/tool" const sessionID = Session.ID.make("ses_shell_tool_test") const sessionModel = Model.Ref.make({ id: Model.ID.make("test"), providerID: Provider.ID.make("test") }) @@ -858,38 +858,34 @@ describe("ShellTool ordinary shell syntax", () => { }) describe("ShellTool", () => { - it.live("moves shell into Code Mode with full_codemode and returns both parallel results", () => + it.live("returns both parallel CodeMode shell results", () => withTempDir((tmp) => { reset() - return Effect.promise(() => - Bun.write(path.join(tmp.path, "opencode.json"), JSON.stringify({ experimental: { full_codemode: true } })), - ).pipe( - Effect.andThen( - withSession(tmp.path, (registry) => - Effect.gen(function* () { - const snapshot = yield* registry.snapshot() - expect(snapshot.definitions.map((tool) => tool.name)).toEqual(["execute"]) - expect(codeModeListings(snapshot.codeModeCatalog!).map((entry) => entry.path)).toEqual(["shell"]) - const command = isWindows ? helloCommand : `${helloCommand}; sleep 0.1` - const inputs = ["one", "two"].map((text) => JSON.stringify({ command: command.replace("hello", text) })) - const result = yield* executeTool(registry, { - sessionID, - ...toolIdentity, - call: { - type: "tool-call", - id: "call-parallel-shells", - name: "execute", - input: { code: `return await Promise.all([tools.shell(${inputs[0]}), tools.shell(${inputs[1]})])` }, - }, - }).pipe(Effect.timeout("3 seconds")) - expect(result.status).toBe("completed") - expect(JSON.parse(result.output.output)).toEqual([ - { output: "one", exitCode: 0, wallTimeSeconds: expect.any(Number) }, - { output: "two", exitCode: 0, wallTimeSeconds: expect.any(Number) }, - ]) + return withSession(tmp.path, (registry) => + Effect.gen(function* () { + yield* registry.transform((editor) => + editor.update("shell", (tool) => { + tool.options = { ...tool.options, codemode: true } }), - ), - ), + ) + const command = isWindows ? helloCommand : `${helloCommand}; sleep 0.1` + const inputs = ["one", "two"].map((text) => JSON.stringify({ command: command.replace("hello", text) })) + const result = yield* executeTool(registry, { + sessionID, + ...toolIdentity, + call: { + type: "tool-call", + id: "call-parallel-shells", + name: "execute", + input: { code: `return await Promise.all([tools.shell(${inputs[0]}), tools.shell(${inputs[1]})])` }, + }, + }).pipe(Effect.timeout("3 seconds")) + expect(result.status).toBe("completed") + expect(JSON.parse(result.output.output)).toEqual([ + { output: "one", exitCode: 0, wallTimeSeconds: expect.any(Number) }, + { output: "two", exitCode: 0, wallTimeSeconds: expect.any(Number) }, + ]) + }), ) }), ) diff --git a/packages/core/test/tool-webfetch.test.ts b/packages/core/test/tool-webfetch.test.ts index 0928c4b5d2a..5c37b5a010f 100644 --- a/packages/core/test/tool-webfetch.test.ts +++ b/packages/core/test/tool-webfetch.test.ts @@ -1,14 +1,10 @@ import { describe, expect, test } from "bun:test" -import { Context, Duration, Effect, Fiber, Layer, PubSub, Schema, Stream } from "effect" +import { Duration, Effect, Fiber, Layer, Schema } from "effect" import * as TestClock from "effect/testing/TestClock" import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http" -import { Document, Info } from "@opencode/schema/config" -import { ConfigExperimental } from "@opencode/schema/config/experimental" -import { Event } from "@opencode/schema/event" import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" import { Config } from "@opencode/core/config" import { Flag } from "@opencode/core/flag" -import { Image } from "@opencode/core/image" import { LayerNode } from "@opencode/util/effect/layer-node" import { LayerNodePlatform } from "@opencode/util/effect/app-node-platform" import { Permission } from "@opencode/core/permission" @@ -17,17 +13,8 @@ import { Tool } from "@opencode/core/tool" import { WebFetchTool } from "@opencode/core/tool/plugin/webfetch" import { makeLocationNode } from "@opencode/util/effect/app-node" import { testEffect } from "./lib/effect" -import { imagePassthrough } from "./lib/image" import { permissionLayer } from "./lib/permission" -import { - codeModeListings, - toolIdentity, - executeTool, - registerToolPlugin, - toolDefinitions, - toolNodeReplacements, - waitForTool, -} from "./lib/tool" +import { toolIdentity, executeTool, registerToolPlugin, toolDefinitions, toolNodeReplacements } from "./lib/tool" const webFetchToolNode = makeLocationNode({ name: "test/webfetch-tool-plugin", @@ -36,7 +23,6 @@ const webFetchToolNode = makeLocationNode({ }) const sessionID = Session.ID.make("ses_webfetch_test") -const configUpdated = () => ({ id: Event.ID.create(), created: Date.now(), type: "config.updated" as const, data: {} }) const webFetchUserAgent = "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; OpenCode-User/1.0; +https://opencode.ai" const requests: Array<{ readonly url: string; readonly headers: Record }> = [] @@ -75,8 +61,6 @@ const toolLayer = (replacements: LayerNode.Replacements = []) => ]) const it = testEffect(toolLayer([LayerNodePlatform.httpClient.replace(http)])) const live = testEffect(toolLayer()) -// For tests that build their own tool graph, so the shared one above is not in scope. -const standalone = testEffect(Layer.empty) const reset = () => { requests.length = 0 @@ -751,69 +735,4 @@ describe("WebFetchTool registration", () => { }) }), ) - - standalone.live("is absent with full_codemode while Code Mode fetch still works, and returns when it is turned off", () => - Effect.acquireUseRelease( - Effect.sync(() => - Bun.serve({ port: 0, fetch: () => new Response("served", { headers: { "content-type": "text/plain" } }) }), - ), - (server) => - Effect.gen(function* () { - reset() - const config = yield* Layer.build( - Config.testLayer([ - new Document({ - type: "document", - info: new Info({ experimental: new ConfigExperimental.Info({ full_codemode: true }) }), - }), - ]), - ) - const updates = yield* PubSub.unbounded>() - const node = makeLocationNode({ - name: "test/webfetch-tool-plugin-observed", - layer: Layer.effectDiscard( - registerToolPlugin(WebFetchTool.Plugin, { event: { subscribe: () => Stream.fromPubSub(updates) } }), - ), - deps: [Tool.node, Config.node, Flag.node, Permission.node, LayerNodePlatform.httpClient], - }) - yield* Effect.gen(function* () { - const registry = yield* Tool.Service - const snapshot = yield* registry.snapshot() - expect([ - ...snapshot.definitions.map((tool) => tool.name), - ...(snapshot.codeModeCatalog ? codeModeListings(snapshot.codeModeCatalog).map((entry) => entry.path) : []), - ]).not.toContain("webfetch") - const url = new URL("/page", server.url).toString() - expect(yield* executeTool(registry, call({ url, format: "text" }))).toMatchObject({ status: "error" }) - - const result = yield* executeTool(registry, { - sessionID, - ...toolIdentity, - call: { - type: "tool-call", - id: "call-execute", - name: "execute", - input: { code: `return await (await fetch(${JSON.stringify(url)})).text()` }, - }, - }) - expect(result.status).toBe("completed") - expect(JSON.stringify(result.content)).toContain("served") - expect(assertions).toMatchObject([{ action: "webfetch", resources: [url] }]) - - yield* Context.get(config, Config.Test).setEntries([]) - yield* PubSub.publish(updates, configUpdated()) - yield* waitForTool(registry, "webfetch") - }).pipe( - Effect.provide( - AppNodeBuilder.build(LayerNode.group([Tool.node, node]), [ - Config.node.replace(Layer.succeedContext(config)), - Image.node.replace(imagePassthrough), - Permission.node.replace(permission), - ]), - ), - ) - }).pipe(Effect.scoped), - (server) => Effect.promise(() => server.stop(true)), - ), - ) })