mirror of
https://gitee.com/zhangsken/opencode.git
synced 2026-11-19 13:11:44 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d3a5a8ff45 | |||
| ce9291cf90 | |||
| 16e2c77267 | |||
| 963e957dc9 | |||
| afc18531ca |
@@ -1,25 +1,19 @@
|
||||
export * as CodeModeCatalog from "./catalog.js"
|
||||
|
||||
import type { Namespace } from "@opencode-ai/schema/tool"
|
||||
import { Schema } from "effect"
|
||||
|
||||
export const Tool = Schema.Struct({
|
||||
type: Schema.Literal("tool"),
|
||||
name: Schema.String,
|
||||
path: Schema.String,
|
||||
description: Schema.String,
|
||||
signature: Schema.String,
|
||||
pinned: Schema.optionalKey(Schema.Boolean),
|
||||
})
|
||||
export type Tool = typeof Tool.Type
|
||||
|
||||
export type Namespace = {
|
||||
readonly type: "namespace"
|
||||
readonly name: string
|
||||
readonly description?: string
|
||||
readonly tools: ReadonlyArray<Tool | Namespace>
|
||||
}
|
||||
|
||||
export type Inventory = {
|
||||
readonly tools: ReadonlyArray<Tool | Namespace>
|
||||
readonly tools: ReadonlyArray<Tool>
|
||||
readonly namespaces?: ReadonlyMap<string, Namespace>
|
||||
}
|
||||
|
||||
const Listing = Schema.Struct({
|
||||
@@ -53,15 +47,14 @@ const INLINE_BUDGET = 2_000
|
||||
// considering shorter listings first until the inline budget is exhausted.
|
||||
export function summarize(inventory: Inventory, options: Options = {}): Summary {
|
||||
const budget = options.budget ?? INLINE_BUDGET
|
||||
const flattened = flatten(inventory.tools)
|
||||
const namespaces = [...Map.groupBy(flattened.tools, (tool) => tool.path.split(".", 1)[0] ?? tool.path)]
|
||||
const namespaces = [...Map.groupBy(inventory.tools, (tool) => tool.path.split(".", 1)[0] ?? tool.path)]
|
||||
.sort(([left], [right]) => {
|
||||
if (left < right) return -1
|
||||
if (left > right) return 1
|
||||
return 0
|
||||
})
|
||||
.map(([name, namespaceEntries]) => {
|
||||
const description = flattened.namespaces.get(name)?.description
|
||||
const description = inventory.namespaces?.get(name)?.description
|
||||
const listings = namespaceEntries
|
||||
.map((entry) => {
|
||||
const firstLine = entry.description.split("\n", 1)[0]?.trim() ?? ""
|
||||
@@ -133,35 +126,12 @@ export function summarize(inventory: Inventory, options: Options = {}): Summary
|
||||
entries: namespace.listings.filter((listing) => namespace.selectedListings.has(listing)),
|
||||
}))
|
||||
return {
|
||||
total: flattened.tools.length,
|
||||
total: inventory.tools.length,
|
||||
shown: namespaceSummaries.reduce((total, namespace) => total + namespace.entries.length, 0),
|
||||
namespaces: namespaceSummaries,
|
||||
}
|
||||
}
|
||||
|
||||
function flatten(entries: ReadonlyArray<Tool | Namespace>, path: ReadonlyArray<string> = []) {
|
||||
const tools: Array<Omit<Tool, "name"> & { readonly path: string }> = []
|
||||
const namespaces = new Map<string, Namespace>()
|
||||
for (const entry of entries) {
|
||||
if (entry.type === "tool") {
|
||||
tools.push({
|
||||
type: "tool",
|
||||
path: [...path, entry.name].join("."),
|
||||
description: entry.description,
|
||||
signature: entry.signature,
|
||||
...(entry.pinned === undefined ? {} : { pinned: entry.pinned }),
|
||||
})
|
||||
continue
|
||||
}
|
||||
const next = [...path, entry.name]
|
||||
namespaces.set(next.join("."), entry)
|
||||
const nested = flatten(entry.tools, next)
|
||||
tools.push(...nested.tools)
|
||||
for (const [name, namespace] of nested.namespaces) namespaces.set(name, namespace)
|
||||
}
|
||||
return { tools, namespaces }
|
||||
}
|
||||
|
||||
export function namespaceLine(namespace: typeof NamespaceSummary.Type) {
|
||||
const count = namespace.count === 1 ? "1 tool" : `${namespace.count} tools`
|
||||
const label =
|
||||
|
||||
@@ -40,14 +40,12 @@ type CollectedFiles = {
|
||||
readonly files: Array<typeof ExecuteFile.Type>
|
||||
}
|
||||
|
||||
type Node<T> = {
|
||||
tool?: T
|
||||
type ToolNode = {
|
||||
tool?: Tool.Tool<never>
|
||||
namespace?: ToolNamespace
|
||||
readonly children: Map<string, Node<T>>
|
||||
readonly children: Map<string, ToolNode>
|
||||
}
|
||||
|
||||
type ToolNode = Node<Tool.Tool<never>>
|
||||
|
||||
type Tools = {
|
||||
[name: string]: Tool.Tool<never> | Namespace.Namespace<never> | Tools
|
||||
}
|
||||
@@ -164,43 +162,14 @@ export const catalog = (inventory: Inventory) => {
|
||||
.filter((registration) => registration.options?.pinned === true)
|
||||
.map(qualifiedName),
|
||||
)
|
||||
const root: CatalogNode = { children: new Map() }
|
||||
for (const namespace of inventory.namespaces?.values() ?? []) getNode(root, namespace.name).namespace = namespace
|
||||
for (const tool of runtime(inventory, () => Effect.fail(toolError("Execute context is unavailable"))).catalog())
|
||||
getNode(root, tool.path).tool = {
|
||||
type: "tool",
|
||||
name: tool.path.split(".").at(-1) ?? tool.path,
|
||||
description: tool.description,
|
||||
signature: tool.signature,
|
||||
pinned: pinned.has(tool.path),
|
||||
}
|
||||
return {
|
||||
tools: renderCatalog(root),
|
||||
tools: runtime(inventory, () => Effect.fail(toolError("Execute context is unavailable")))
|
||||
.catalog()
|
||||
.map((tool) => ({ ...tool, pinned: pinned.has(tool.path) })),
|
||||
...(inventory.namespaces === undefined ? {} : { namespaces: inventory.namespaces }),
|
||||
} satisfies CodeModeCatalog.Inventory
|
||||
}
|
||||
|
||||
type CatalogNode = Node<CodeModeCatalog.Tool>
|
||||
|
||||
function renderCatalog(root: CatalogNode): ReadonlyArray<CodeModeCatalog.Tool | CodeModeCatalog.Namespace> {
|
||||
return Array.from(root.children)
|
||||
.toSorted(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
|
||||
.flatMap(([name, node]) => {
|
||||
const tools = renderCatalog(node)
|
||||
const namespace =
|
||||
node.namespace === undefined && tools.length === 0
|
||||
? undefined
|
||||
: {
|
||||
type: "namespace" as const,
|
||||
name,
|
||||
...(node.namespace?.description === undefined ? {} : { description: node.namespace.description }),
|
||||
tools,
|
||||
}
|
||||
if (node.tool === undefined) return namespace === undefined ? [] : [namespace]
|
||||
if (namespace === undefined) return [node.tool]
|
||||
return [node.tool, namespace]
|
||||
})
|
||||
}
|
||||
|
||||
function runtime(
|
||||
inventory: Inventory,
|
||||
executeTool: (name: string, tool: Info, input: unknown) => Effect.Effect<unknown, unknown>,
|
||||
@@ -222,9 +191,9 @@ function runtime(
|
||||
return CodeMode.make<typeof tools>({ tools, ...hooks })
|
||||
}
|
||||
|
||||
function getNode<T>(root: Node<T>, path: string) {
|
||||
function getNode(root: ToolNode, path: string) {
|
||||
return path.split(".").reduce((parent, name) => {
|
||||
const child: Node<T> = parent.children.get(name) ?? { children: new Map() }
|
||||
const child: ToolNode = parent.children.get(name) ?? { children: new Map() }
|
||||
parent.children.set(name, child)
|
||||
return child
|
||||
}, root)
|
||||
|
||||
@@ -10,8 +10,7 @@ describe("CodeMode", () => {
|
||||
it.effect("owns registrations, execute, and catalog materialization", () =>
|
||||
Effect.gen(function* () {
|
||||
const tools = yield* Tool.Service
|
||||
yield* tools.transform((draft) => {
|
||||
draft.namespace({ name: "empty", description: "No tools registered yet" })
|
||||
yield* tools.transform((draft) =>
|
||||
draft.add({
|
||||
name: "echo",
|
||||
description: "Echo text",
|
||||
@@ -19,27 +18,21 @@ describe("CodeMode", () => {
|
||||
output: Schema.String,
|
||||
options: { pinned: true },
|
||||
execute: ({ text }) => Effect.succeed({ output: text }),
|
||||
})
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
const snapshot = yield* tools.snapshot()
|
||||
expect(snapshot.definitions.some((tool) => tool.name === "execute")).toBe(true)
|
||||
expect(snapshot.codeModeCatalog).toStrictEqual({
|
||||
tools: [
|
||||
{
|
||||
type: "tool",
|
||||
name: "echo",
|
||||
path: "echo",
|
||||
description: "Echo text",
|
||||
signature: "tools.echo(input: {\n text: string,\n}): Promise<string>",
|
||||
pinned: true,
|
||||
},
|
||||
{
|
||||
type: "namespace",
|
||||
name: "empty",
|
||||
description: "No tools registered yet",
|
||||
tools: [],
|
||||
},
|
||||
],
|
||||
namespaces: new Map(),
|
||||
})
|
||||
}).pipe(
|
||||
Effect.scoped,
|
||||
|
||||
@@ -3,8 +3,7 @@ import { CodeModeCatalog } from "@opencode-ai/core/codemode/catalog"
|
||||
import { CodeModeInstructions } from "@opencode-ai/core/codemode/instructions"
|
||||
|
||||
const entry = (path: string, description: string, signature?: string, pinned = false): CodeModeCatalog.Tool => ({
|
||||
type: "tool",
|
||||
name: path,
|
||||
path,
|
||||
description,
|
||||
signature: signature ?? `tools.${path}(input: {\n q: string,\n}): Promise<string>`,
|
||||
pinned,
|
||||
@@ -16,12 +15,12 @@ const lookup = entry(
|
||||
"tools.orders.lookup(input: {\n id: string,\n}): Promise<{\n id: string,\n status: string,\n}>",
|
||||
)
|
||||
|
||||
const render = (tools: CodeModeCatalog.Inventory["tools"], budget?: number) =>
|
||||
const render = (tools: ReadonlyArray<CodeModeCatalog.Tool>, budget?: number) =>
|
||||
CodeModeInstructions.render(CodeModeCatalog.summarize({ tools }, budget === undefined ? {} : { budget }))
|
||||
|
||||
const update = (
|
||||
previous: CodeModeCatalog.Inventory["tools"],
|
||||
current: CodeModeCatalog.Inventory["tools"],
|
||||
previous: ReadonlyArray<CodeModeCatalog.Tool>,
|
||||
current: ReadonlyArray<CodeModeCatalog.Tool>,
|
||||
budget?: number,
|
||||
) =>
|
||||
CodeModeInstructions.update(
|
||||
@@ -104,10 +103,10 @@ describe("CodeModeCatalog.summarize", () => {
|
||||
const listingCost = Math.round(` - ${tool.signature} // One`.length / 4)
|
||||
const namespaceCost = Math.round(CodeModeCatalog.namespaceLine({ name: "alpha", count: 1, entries: [] }).length / 4)
|
||||
const description = "A namespace description that stays visible beyond the available tool budget"
|
||||
const namespace = { type: "namespace" as const, name: "alpha", description, tools: [tool] }
|
||||
const namespaces = new Map([["alpha", { name: "alpha", description }]])
|
||||
|
||||
expect(CodeModeCatalog.summarize({ tools: [tool] }, { budget: namespaceCost + listingCost }).shown).toBe(1)
|
||||
const catalog = CodeModeCatalog.summarize({ tools: [namespace] }, { budget: namespaceCost + listingCost })
|
||||
const catalog = CodeModeCatalog.summarize({ tools: [tool], namespaces }, { budget: namespaceCost + listingCost })
|
||||
expect(catalog.shown).toBe(0)
|
||||
expect(catalog.namespaces[0]?.description).toBe(description)
|
||||
expect(CodeModeInstructions.render(catalog)).toContain(`- alpha (1 tool, none shown) // ${description}`)
|
||||
@@ -210,10 +209,12 @@ describe("CodeModeInstructions.update", () => {
|
||||
|
||||
test("restates namespace descriptions when they change", () => {
|
||||
const previous = CodeModeCatalog.summarize({
|
||||
tools: [{ type: "namespace", name: "notes", description: "Old description", tools: [echo] }],
|
||||
tools: [echo],
|
||||
namespaces: new Map([["notes", { name: "notes", description: "Old description" }]]),
|
||||
})
|
||||
const current = CodeModeCatalog.summarize({
|
||||
tools: [{ type: "namespace", name: "notes", description: "New description", tools: [echo] }],
|
||||
tools: [echo],
|
||||
namespaces: new Map([["notes", { name: "notes", description: "New description" }]]),
|
||||
})
|
||||
const text = CodeModeInstructions.update(previous, current)
|
||||
expect(text).toContain("This catalog supersedes the previous Code Mode tool catalog.")
|
||||
|
||||
@@ -10,15 +10,13 @@ import { it } from "../lib/effect"
|
||||
import { readInitial, readUpdate } from "../lib/instructions"
|
||||
|
||||
const echo: CodeModeCatalog.Tool = {
|
||||
type: "tool",
|
||||
name: "notes.echo",
|
||||
path: "notes.echo",
|
||||
description: "Echo text",
|
||||
signature: "tools.notes.echo(input: {\n text: string,\n}): Promise<string>",
|
||||
}
|
||||
|
||||
const lookup: CodeModeCatalog.Tool = {
|
||||
type: "tool",
|
||||
name: "orders.lookup",
|
||||
path: "orders.lookup",
|
||||
description: "Look up an order",
|
||||
signature: "tools.orders.lookup(input: {\n id: string,\n}): Promise<unknown>",
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Agent } from "@opencode-ai/core/agent"
|
||||
import { CodeModeCatalog } from "@opencode-ai/core/codemode/catalog"
|
||||
import type { Permission } from "@opencode-ai/core/permission"
|
||||
import { SessionMessage } from "@opencode-ai/core/session/message"
|
||||
import { toSessionError } from "@opencode-ai/core/session/to-session-error"
|
||||
@@ -17,9 +16,6 @@ export const toolIdentity = {
|
||||
export const toolDefinitions = (registry: Tool.Interface, permissions?: Permission.Ruleset) =>
|
||||
registry.snapshot(permissions).pipe(Effect.map((toolSet) => toolSet.definitions))
|
||||
|
||||
export const codeModeListings = (catalog: CodeModeCatalog.Inventory) =>
|
||||
CodeModeCatalog.summarize(catalog, { budget: Infinity }).namespaces.flatMap((namespace) => namespace.entries)
|
||||
|
||||
export function waitForTool(registry: Tool.Interface, name: string, remaining = 1000): Effect.Effect<void, Error> {
|
||||
return Effect.gen(function* () {
|
||||
if ((yield* toolDefinitions(registry)).some((tool) => tool.name === name)) return
|
||||
@@ -39,8 +35,7 @@ export function waitForCodeModeTool(
|
||||
): Effect.Effect<Tool.Snapshot, Error> {
|
||||
return Effect.gen(function* () {
|
||||
const toolSet = yield* registry.snapshot()
|
||||
if (toolSet.codeModeCatalog && codeModeListings(toolSet.codeModeCatalog).some((tool) => tool.path === path))
|
||||
return toolSet
|
||||
if (toolSet.codeModeCatalog?.tools.some((tool) => tool.path === path)) return toolSet
|
||||
if (remaining === 0) {
|
||||
return yield* Effect.fail(new Error(`Timed out waiting for Code Mode tool: ${path}`))
|
||||
}
|
||||
|
||||
@@ -162,6 +162,6 @@ describe("MCP OAuth", () => {
|
||||
})
|
||||
|
||||
test("rejects an invalid redirect URL", async () => {
|
||||
await expect(authorize("not a URL")).rejects.toThrow("cannot be parsed as a URL")
|
||||
await expect(authorize("not a URL")).rejects.toThrow(TypeError)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -46,7 +46,7 @@ import { imagePassthrough } from "./lib/image"
|
||||
import { location } from "./fixture/location"
|
||||
import { tmpdirScoped } from "./fixture/tmpdir"
|
||||
import { hostEnvironmentLayer, recordingEnvironmentLayer } from "./fixture/environment"
|
||||
import { codeModeListings, executeTool, toolDefinitions, toolIdentity, waitForTool } from "./lib/tool"
|
||||
import { executeTool, toolDefinitions, toolIdentity, waitForTool } from "./lib/tool"
|
||||
|
||||
let assertion: Deferred.Deferred<Permission.AssertInput> | undefined
|
||||
let decision: Effect.Effect<void, Permission.Error> = Effect.void
|
||||
@@ -1764,7 +1764,7 @@ it.effect("advertises MCP output schemas to Code Mode", () =>
|
||||
"direct_media",
|
||||
"execute",
|
||||
])
|
||||
expect(codeModeListings(toolSet.codeModeCatalog!).find((tool) => tool.path === "demo.search")?.line).toContain(
|
||||
expect(toolSet.codeModeCatalog?.tools.find((tool) => tool.path === "demo.search")?.signature).toContain(
|
||||
"ok: boolean",
|
||||
)
|
||||
expect(execute?.description).not.toContain("tools.demo.search")
|
||||
@@ -1784,7 +1784,7 @@ it.effect("forwards the invoking session through direct and Code Mode MCP tools"
|
||||
expect(toolSet.definitions.find((tool) => tool.name === "direct_lookup")?.inputSchema).not.toHaveProperty(
|
||||
"properties.sessionID",
|
||||
)
|
||||
expect(codeModeListings(toolSet.codeModeCatalog!).find((tool) => tool.path === "demo.search")?.line).not.toContain(
|
||||
expect(toolSet.codeModeCatalog?.tools.find((tool) => tool.path === "demo.search")?.signature).not.toContain(
|
||||
"sessionID",
|
||||
)
|
||||
|
||||
@@ -1830,7 +1830,7 @@ it.effect("returns content-only MCP results through Code Mode", () =>
|
||||
yield* registration.flush
|
||||
const toolSet = yield* registry.snapshot()
|
||||
|
||||
expect(codeModeListings(toolSet.codeModeCatalog!).some((tool) => tool.path === "demo.status")).toBe(true)
|
||||
expect(toolSet.codeModeCatalog?.tools.some((tool) => tool.path === "demo.status")).toBe(true)
|
||||
|
||||
const execution = yield* toolSet.execute({
|
||||
sessionID: Session.ID.make("ses_mcp_content_only"),
|
||||
@@ -1916,7 +1916,7 @@ it.effect("waits for permission before calling an MCP tool", () =>
|
||||
const registration = yield* McpTool.Service
|
||||
yield* registration.flush
|
||||
const toolSet = yield* registry.snapshot()
|
||||
expect(codeModeListings(toolSet.codeModeCatalog!).some((tool) => tool.path === "demo.search")).toBe(true)
|
||||
expect(toolSet.codeModeCatalog?.tools.some((tool) => tool.path === "demo.search")).toBe(true)
|
||||
|
||||
const fiber = yield* toolSet
|
||||
.execute({
|
||||
@@ -1960,7 +1960,7 @@ it.effect("does not call MCP when permission is blocked", () =>
|
||||
const registration = yield* McpTool.Service
|
||||
yield* registration.flush
|
||||
const toolSet = yield* registry.snapshot()
|
||||
expect(codeModeListings(toolSet.codeModeCatalog!).some((tool) => tool.path === "demo.search")).toBe(true)
|
||||
expect(toolSet.codeModeCatalog?.tools.some((tool) => tool.path === "demo.search")).toBe(true)
|
||||
|
||||
const execution = yield* toolSet.execute({
|
||||
sessionID: Session.ID.make("ses_mcp_blocked"),
|
||||
|
||||
@@ -66,7 +66,7 @@ async function createRegistryFixture(directory: string) {
|
||||
exports: "./index.js",
|
||||
})
|
||||
await Bun.write(path.join(root, "package", "index.js"), `export const version = "${version}"\n`)
|
||||
await Bun.$`tar -czf ${path.join(root, "package.tgz")} -C ${root} package`
|
||||
await Bun.$`tar -czf package.tgz package`.cwd(root)
|
||||
tarballs.set(version, await Bun.file(path.join(root, "package.tgz")).bytes())
|
||||
}
|
||||
const state = { latest: "1.0.0" }
|
||||
@@ -252,7 +252,7 @@ describe("Npm.add", () => {
|
||||
}
|
||||
}).pipe(Effect.scoped, Effect.provide(npmLayer(cache)), Effect.runPromise)
|
||||
|
||||
expect(entries.added.entrypoint).toEndWith("/index.js")
|
||||
expect(entries.added.entrypoint).toBe(pathToFileURL(path.join(entries.added.directory, "index.js")).href)
|
||||
expect(entries.added.version).toBe(fixture.commit)
|
||||
expect(entries.cached).toEqual(entries.added)
|
||||
expect(entries.resolved).toEqual(entries.added)
|
||||
|
||||
@@ -18,7 +18,6 @@ import { Tool } from "@opencode-ai/core/tool"
|
||||
import { Provider } from "@opencode-ai/core/provider"
|
||||
import { Project } from "@opencode-ai/core/project"
|
||||
import { Workspace } from "@opencode-ai/core/workspace"
|
||||
import { codeModeListings } from "../lib/tool"
|
||||
import { AbsolutePath } from "@opencode-ai/core/schema"
|
||||
import { define } from "@opencode-ai/plugin/promise/plugin"
|
||||
import type { Info } from "@opencode-ai/plugin/promise/tool"
|
||||
@@ -982,7 +981,7 @@ describe("fromPromise", () => {
|
||||
|
||||
const snapshot = yield* registry.snapshot()
|
||||
expect(snapshot.definitions.map((tool) => tool.name)).toEqual(["execute"])
|
||||
expect(codeModeListings(snapshot.codeModeCatalog!).map((tool) => tool.path)).toEqual(["acme.hello"])
|
||||
expect(snapshot.codeModeCatalog?.tools.map((tool) => tool.path)).toEqual(["acme.hello"])
|
||||
expect(original.definitions.map((tool) => tool.name)).toEqual(["acme_hello", "execute"])
|
||||
expect(
|
||||
yield* snapshot.execute({
|
||||
|
||||
@@ -118,8 +118,7 @@ const tools = Layer.mock(Tool.Service, {
|
||||
codeModeCatalog: {
|
||||
tools: [
|
||||
{
|
||||
type: "tool",
|
||||
name: "captured.lookup",
|
||||
path: "captured.lookup",
|
||||
description: "Captured Code Mode catalog",
|
||||
signature: "tools.captured.lookup(input: {}): Promise<string>",
|
||||
},
|
||||
|
||||
@@ -14,7 +14,7 @@ import { State } from "@opencode-ai/core/state"
|
||||
import { Tool } from "@opencode-ai/core/tool"
|
||||
import type { Info } from "@opencode-ai/schema/tool"
|
||||
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
|
||||
import { codeModeListings, executeTool, toolDefinitions } from "./lib/tool"
|
||||
import { executeTool, toolDefinitions } from "./lib/tool"
|
||||
import { Deferred, Effect, Exit, Fiber, Layer, Logger, Schema, SchemaGetter, SchemaIssue, Scope } from "effect"
|
||||
import { TestClock } from "effect/testing"
|
||||
import { z } from "zod"
|
||||
@@ -247,9 +247,7 @@ describe("Tool", () => {
|
||||
yield* update.dispose
|
||||
expect((yield* executeTool(service, call("acme_echo"))).output).toEqual({ text: "refreshed" })
|
||||
yield* Scope.close(scope, Exit.void)
|
||||
expect(codeModeListings((yield* service.snapshot()).codeModeCatalog!).map((tool) => tool.path)).toEqual([
|
||||
"hidden",
|
||||
])
|
||||
expect((yield* service.snapshot()).codeModeCatalog?.tools.map((tool) => tool.path)).toEqual(["hidden"])
|
||||
|
||||
yield* service.transform((draft) =>
|
||||
draft.update("acme_echo", (tool) => {
|
||||
@@ -504,7 +502,7 @@ describe("Tool", () => {
|
||||
|
||||
const snapshot = yield* service.snapshot()
|
||||
expect(snapshot.definitions.map((tool) => tool.name)).toEqual(["execute"])
|
||||
expect(codeModeListings(snapshot.codeModeCatalog!).map((tool) => tool.path)).toEqual([
|
||||
expect(snapshot.codeModeCatalog?.tools.map((tool) => tool.path)).toEqual([
|
||||
"-lookup",
|
||||
"123",
|
||||
"123._private.-tools.2d_get_scene",
|
||||
@@ -544,9 +542,7 @@ describe("Tool", () => {
|
||||
|
||||
const snapshot = yield* service.snapshot()
|
||||
expect(snapshot.definitions.map((tool) => tool.name)).toEqual(["first", "execute"])
|
||||
expect(codeModeListings(snapshot.codeModeCatalog!).map((tool) => tool.path)).toEqual([
|
||||
"invalid__namespace.second",
|
||||
])
|
||||
expect(snapshot.codeModeCatalog?.tools.map((tool) => tool.path)).toEqual(["invalid__namespace.second"])
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -564,32 +560,17 @@ describe("Tool", () => {
|
||||
|
||||
const snapshot = yield* service.snapshot()
|
||||
expect(snapshot.definitions.map((tool) => tool.name)).toEqual(["registry_direct", "execute"])
|
||||
expect(codeModeListings(snapshot.codeModeCatalog!).map((tool) => tool.path)).toEqual([
|
||||
expect(snapshot.codeModeCatalog?.tools.map((tool) => tool.path)).toEqual([
|
||||
"legacy.plain",
|
||||
"registry.search",
|
||||
"registry.search.sales",
|
||||
])
|
||||
expect(snapshot.codeModeCatalog?.tools).toEqual([
|
||||
{
|
||||
type: "namespace",
|
||||
name: "legacy",
|
||||
tools: [expect.objectContaining({ type: "tool", name: "plain" })],
|
||||
},
|
||||
{
|
||||
type: "namespace",
|
||||
name: "registry",
|
||||
description: "Package publishing and discovery",
|
||||
tools: [
|
||||
expect.objectContaining({ type: "tool", name: "search" }),
|
||||
{
|
||||
type: "namespace",
|
||||
name: "search",
|
||||
description: "Pricing operations",
|
||||
tools: [expect.objectContaining({ type: "tool", name: "sales" })],
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
expect(snapshot.codeModeCatalog?.namespaces).toEqual(
|
||||
new Map([
|
||||
["registry", { name: "registry", description: "Package publishing and discovery" }],
|
||||
["registry.search", { name: "registry.search", description: "Pricing operations" }],
|
||||
]),
|
||||
)
|
||||
const result = yield* snapshot.execute({
|
||||
...call("execute"),
|
||||
call: {
|
||||
@@ -634,7 +615,7 @@ describe("Tool", () => {
|
||||
})
|
||||
|
||||
const snapshot = yield* service.snapshot()
|
||||
expect(codeModeListings(snapshot.codeModeCatalog!).map((tool) => tool.path)).toEqual(["pricing", "pricing.sales"])
|
||||
expect(snapshot.codeModeCatalog?.tools.map((tool) => tool.path)).toEqual(["pricing", "pricing.sales"])
|
||||
const result = yield* snapshot.execute({
|
||||
...call("execute"),
|
||||
call: {
|
||||
@@ -683,7 +664,7 @@ describe("Tool", () => {
|
||||
])
|
||||
const snapshot = yield* service.snapshot()
|
||||
expect(snapshot.definitions.map((tool) => tool.name)).toEqual(["healthy", "execute"])
|
||||
expect(codeModeListings(snapshot.codeModeCatalog!).map((tool) => tool.path)).toEqual(["codemode"])
|
||||
expect(snapshot.codeModeCatalog?.tools.map((tool) => tool.path)).toEqual(["codemode"])
|
||||
expect((yield* snapshot.execute(call("phone_type")).pipe(Effect.flip)).message).toBe("Unknown tool: phone_type")
|
||||
}).pipe(Effect.provide(Logger.layer([logger])))
|
||||
})
|
||||
@@ -748,7 +729,7 @@ describe("Tool", () => {
|
||||
|
||||
const snapshot = yield* service.snapshot()
|
||||
expect(snapshot.definitions.map((tool) => tool.name)).toEqual(["execute"])
|
||||
expect(codeModeListings(snapshot.codeModeCatalog!)[0]?.line).toContain("tools.echo")
|
||||
expect(snapshot.codeModeCatalog?.tools[0]?.signature).toContain("tools.echo")
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -1211,7 +1192,7 @@ describe("Tool", () => {
|
||||
}).pipe(Scope.provide(scope))
|
||||
const toolSet = yield* service.snapshot()
|
||||
const execute = toolSet.definitions.find((tool) => tool.name === "execute")
|
||||
expect(codeModeListings(toolSet.codeModeCatalog!)[0]?.line).toContain("tools.echo")
|
||||
expect(toolSet.codeModeCatalog?.tools[0]?.signature).toContain("tools.echo")
|
||||
expect(execute?.description).toContain("confined Code Mode runtime")
|
||||
expect(execute?.description).not.toContain("Echo text")
|
||||
yield* Scope.close(scope, Exit.void)
|
||||
|
||||
@@ -3,7 +3,9 @@ import { createServer } from "node:http"
|
||||
import { makeMemoryDriver } from "@opencode-ai/core/environment/index"
|
||||
import { Workspace } from "@opencode-ai/core/workspace"
|
||||
import { WorkspaceDriver } from "@opencode-ai/core/workspace/driver"
|
||||
import { Effect } from "effect"
|
||||
import { Agent } from "@opencode-ai/schema/agent"
|
||||
import { Integration } from "@opencode-ai/schema/integration"
|
||||
import { Effect, Schedule, Schema } from "effect"
|
||||
import { tmpdir } from "../../core/test/fixture/tmpdir"
|
||||
import { it } from "../../core/test/lib/effect"
|
||||
import { ServerFetch } from "../src/fetch"
|
||||
@@ -58,19 +60,35 @@ function occupy(port: number, cancel = false) {
|
||||
})
|
||||
}
|
||||
|
||||
const ready = (handler: Handler) =>
|
||||
Effect.promise(() => handler(new Request("http://opencode.local/api/model/default")))
|
||||
|
||||
const connectOpenAI = (handler: Handler) =>
|
||||
Effect.promise(() =>
|
||||
handler(
|
||||
new Request("http://opencode.local/api/integration/openai/connect/oauth", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ methodID: "chatgpt-browser" }),
|
||||
}),
|
||||
),
|
||||
)
|
||||
Effect.gen(function* () {
|
||||
// Catalog endpoints no longer wait for plugin initialization.
|
||||
yield* Effect.gen(function* () {
|
||||
const response = yield* Effect.promise(() => handler(new Request("http://opencode.local/api/integration")))
|
||||
expect(response.status).toBe(200)
|
||||
const body = Schema.decodeUnknownSync(Schema.Struct({ data: Schema.Array(Integration.Info) }))(
|
||||
yield* Effect.promise(() => response.json()),
|
||||
)
|
||||
return body.data.some(
|
||||
(integration) =>
|
||||
integration.id === "openai" &&
|
||||
integration.methods.some((method) => method.type === "oauth" && method.id === "chatgpt-browser"),
|
||||
)
|
||||
}).pipe(
|
||||
Effect.filterOrFail((ready) => ready),
|
||||
Effect.retry(Schedule.spaced("10 millis")),
|
||||
Effect.timeout("2 seconds"),
|
||||
)
|
||||
return yield* Effect.promise(() =>
|
||||
handler(
|
||||
new Request("http://opencode.local/api/integration/openai/connect/oauth", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ methodID: "chatgpt-browser" }),
|
||||
}),
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
const workspaceDriver = WorkspaceDriver.make({
|
||||
create: ({ workspaceID }) => Effect.succeed({ binding: { workspaceID } }),
|
||||
@@ -189,7 +207,6 @@ it.live("cancels a stale OpenAI OAuth callback server before falling back", () =
|
||||
Effect.gen(function* () {
|
||||
const requests = yield* occupy(1455, true)
|
||||
const handler = yield* ServerFetch.make(options)
|
||||
yield* ready(handler)
|
||||
const response = yield* connectOpenAI(handler)
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
@@ -203,7 +220,6 @@ it.live("falls back to port 1457 when OpenAI OAuth port 1455 remains busy", () =
|
||||
Effect.gen(function* () {
|
||||
const requests = yield* occupy(1455)
|
||||
const handler = yield* ServerFetch.make(options)
|
||||
yield* ready(handler)
|
||||
const response = yield* connectOpenAI(handler)
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
@@ -220,7 +236,6 @@ it.live(
|
||||
yield* occupy(1455)
|
||||
yield* occupy(1457)
|
||||
const handler = yield* ServerFetch.make(options)
|
||||
yield* ready(handler)
|
||||
const response = yield* connectOpenAI(handler)
|
||||
|
||||
expect(response.status).toBe(400)
|
||||
@@ -439,7 +454,22 @@ it.live("routes pending requests by Session without loading an instance", () =>
|
||||
expect(yield* Effect.promise(() => globalForms.json())).toMatchObject({ data: [{ title: "Global form" }] })
|
||||
|
||||
// Agent permission policy is installed by plugin activation.
|
||||
expect((yield* ready(handler)).status).toBe(200)
|
||||
yield* Effect.gen(function* () {
|
||||
const response = yield* Effect.promise(() => handler(new Request("http://opencode.local/api/agent")))
|
||||
expect(response.status).toBe(200)
|
||||
const body = Schema.decodeUnknownSync(Schema.Struct({ data: Schema.Array(Agent.Info) }))(
|
||||
yield* Effect.promise(() => response.json()),
|
||||
)
|
||||
return body.data.some(
|
||||
(agent) =>
|
||||
agent.id === "build" &&
|
||||
agent.permissions.some((rule) => rule.action === "shell" && rule.resource === "*" && rule.effect === "ask"),
|
||||
)
|
||||
}).pipe(
|
||||
Effect.filterOrFail((ready) => ready),
|
||||
Effect.retry(Schedule.spaced("10 millis")),
|
||||
Effect.timeout("2 seconds"),
|
||||
)
|
||||
const createdPermission = yield* Effect.promise(() =>
|
||||
handler(
|
||||
new Request(`http://opencode.local/api/session/${created.data.id}/permission`, {
|
||||
|
||||
@@ -367,6 +367,7 @@
|
||||
[data-component="tool-trigger"] [data-slot="basic-tool-tool-subtitle"].webfetch-link {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
color: var(--v2-text-text-accent);
|
||||
text-decoration: none;
|
||||
overflow: visible;
|
||||
@@ -392,6 +393,18 @@
|
||||
line-height: inherit;
|
||||
}
|
||||
|
||||
.webfetch-link-icon {
|
||||
display: inline-flex;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
flex-shrink: 0;
|
||||
color: var(--v2-icon-icon-accent, var(--v2-text-text-accent));
|
||||
|
||||
@media (hover: hover) {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
&:hover {
|
||||
color: var(--v2-text-text-accent);
|
||||
text-decoration: none;
|
||||
@@ -400,6 +413,10 @@
|
||||
text-decoration: underline;
|
||||
text-underline-offset: 2px;
|
||||
}
|
||||
|
||||
.webfetch-link-icon {
|
||||
display: inline-flex;
|
||||
}
|
||||
}
|
||||
|
||||
&:focus-visible {
|
||||
@@ -410,5 +427,9 @@
|
||||
text-decoration: underline;
|
||||
text-underline-offset: 2px;
|
||||
}
|
||||
|
||||
.webfetch-link-icon {
|
||||
display: inline-flex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
a[target="_blank"]::after {
|
||||
content: "";
|
||||
display: inline-block;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
margin-inline-start: 2px;
|
||||
flex-shrink: 0;
|
||||
vertical-align: -3px;
|
||||
background: currentColor;
|
||||
mask: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M10 8.56V4H5.44M10 4L4 10' stroke='black'/%3E%3C/svg%3E") center / 16px 16px no-repeat;
|
||||
-webkit-mask: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M10 8.56V4H5.44M10 4L4 10' stroke='black'/%3E%3C/svg%3E") center / 16px 16px no-repeat;
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
@layer theme, base, components, utilities;
|
||||
|
||||
@import "../components/basic-tool.css" layer(components);
|
||||
@import "../components/external-link.css" layer(components);
|
||||
@import "../components/file.css" layer(components);
|
||||
@import "../components/markdown.css" layer(components);
|
||||
@import "../components/message-part.css" layer(components);
|
||||
|
||||
@@ -465,6 +465,7 @@ function ExaOutput(props: { output?: string }) {
|
||||
onClick={(event) => event.stopPropagation()}
|
||||
>
|
||||
<span data-slot="webfetch-link-text">{url}</span>
|
||||
<Icon name="outline-square-arrow" class="webfetch-link-icon" />
|
||||
</a>
|
||||
)}
|
||||
</For>
|
||||
@@ -1267,6 +1268,7 @@ ToolRegistry.register({
|
||||
onClick={(event) => event.stopPropagation()}
|
||||
>
|
||||
<span data-slot="webfetch-link-text">{url()}</span>
|
||||
<Icon name="outline-square-arrow" class="webfetch-link-icon" />
|
||||
</a>
|
||||
</Show>
|
||||
</div>
|
||||
|
||||
@@ -293,7 +293,8 @@ const layer = Layer.effect(
|
||||
installedNameValue,
|
||||
installed?.path ?? path.join(staging, "node_modules", installedNameValue),
|
||||
target,
|
||||
subpaths,
|
||||
// Resolve installed entrypoints after rename so Bun cannot hold the staging directory open on Windows.
|
||||
installed ? [] : subpaths,
|
||||
)
|
||||
if (!installed && !result.entrypoint) return yield* new InstallFailedError({ add: [pkg], dir: staging })
|
||||
return { name: installedNameValue, result }
|
||||
|
||||
Reference in New Issue
Block a user