Skip to content
Merged
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
93 changes: 58 additions & 35 deletions packages/core/src/codemode/tool.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export * as CodeModeTool from "./tool.js"

import { CodeMode, Namespace, Tool, toolError } from "@opencode/codemode"
import { CodeMode, Namespace, Tool } from "@opencode/codemode"
import type {
Content,
Context,
Context as ToolContext,
Error,
Info,
Metadata,
Namespace as ToolNamespace,
Result,
} from "@opencode/schema/tool"
import { Effect, Ref, Schema, Semaphore } from "effect"
import { Context, Effect, Ref, Schema, Semaphore } from "effect"
import { definition, normalizedName } from "../tool/runtime.js"
import { CodeModeCatalog } from "./catalog.js"
import { CodeModeWeb } from "./web.js"
Expand Down Expand Up @@ -47,12 +47,21 @@ type Node<T> = {
readonly children: Map<string, Node<T>>
}

type ToolNode = Node<Tool.Tool<never>>
type ToolNode = Node<Tool.Tool<Run>>

type Tools = {
[name: string]: Tool.Tool<never> | Namespace.Namespace<never> | Tools
[name: string]: Tool.Tool<Run> | Namespace.Namespace<Run> | Tools
}

// Per-execution parts of a runtime shared by every execution against the same inventory.
class Run extends Context.Service<
Run,
{
readonly executeTool: (name: string, tool: Info, input: unknown) => Effect.Effect<unknown, unknown>
readonly hooks: CodeMode.Hooks
}
>()("@opencode/CodeModeTool/Run") {}

export type Inventory = {
readonly tools: ReadonlyMap<string, Info>
readonly namespaces?: ReadonlyMap<string, ToolNamespace>
Expand All @@ -70,7 +79,7 @@ const description = [

export const create = (
inventory: Inventory,
executeTool: (name: string, tool: Info, input: unknown, context: Context) => Effect.Effect<Result, Error>,
executeTool: (name: string, tool: Info, input: unknown, context: ToolContext) => Effect.Effect<Result, Error>,
) => {
return {
name: "execute",
Expand All @@ -87,25 +96,28 @@ export const create = (
lock.withPermit(
Ref.updateAndGet(calls, update).pipe(Effect.tap((toolCalls) => context.progress({ toolCalls }))),
)
const result = yield* runtime(
inventory,
(name, tool, input) =>
Effect.gen(function* () {
const index = yield* Ref.getAndUpdate(callIndex, (index) => index + 1)
const executed = yield* executeTool(name, tool, input, context)
const content =
typeof executed.content === "string"
? [{ type: "text" as const, text: executed.content }]
: (executed.content ?? [])
const outputFileParts = outputFiles(content)
if (outputFileParts.length > 0)
yield* Ref.update(files, (items) => [...items, { index, files: outputFileParts }])
if (executed.output !== undefined) return executed.output
const text = content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n")
return text === "" ? null : text
const result = yield* runtime(inventory)
.execute(code)
.pipe(
Effect.provideService(Run, {
executeTool: (name, tool, input) =>
Effect.gen(function* () {
const index = yield* Ref.getAndUpdate(callIndex, (index) => index + 1)
const executed = yield* executeTool(name, tool, input, context)
const content =
typeof executed.content === "string"
? [{ type: "text" as const, text: executed.content }]
: (executed.content ?? [])
const outputFileParts = outputFiles(content)
if (outputFileParts.length > 0)
yield* Ref.update(files, (items) => [...items, { index, files: outputFileParts }])
if (executed.output !== undefined) return executed.output
const text = content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n")
return text === "" ? null : text
}),
hooks: progressHooks(record),
}),
progressHooks(record),
).execute(code)
)
const toolCalls = yield* Ref.get(calls)
const collected = (yield* Ref.get(files))
.toSorted((left, right) => left.index - right.index)
Expand Down Expand Up @@ -183,7 +195,7 @@ export const catalog = (inventory: Inventory) => {
)
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)
for (const tool of runtime(inventory).catalog)
getNode(root, tool.path).tool = {
type: "tool",
name: tool.path.split(".").at(-1) ?? tool.path,
Expand Down Expand Up @@ -218,11 +230,13 @@ function renderCatalog(root: CatalogNode): ReadonlyArray<CodeModeCatalog.Tool |
})
}

function runtime(
inventory: Inventory,
executeTool: (name: string, tool: Info, input: unknown) => Effect.Effect<unknown, unknown>,
hooks?: CodeMode.Hooks,
) {
// Inventories are immutable snapshots, so tool definitions, schemas, and the search index are built
// once per inventory and shared by its catalog and every execution.
const runtimes = new WeakMap<Inventory, CodeMode.Runtime<Run>>()

function runtime(inventory: Inventory) {
const cached = runtimes.get(inventory)
if (cached) return cached
// A path may carry namespace metadata, a callable tool, child tools, or all three.
const root: ToolNode = { children: new Map() }
for (const namespace of inventory.namespaces?.values() ?? []) getNode(root, namespace.name).namespace = namespace
Expand All @@ -232,13 +246,22 @@ function runtime(
description: child.description,
input: child.inputSchema,
output: child.outputSchema ?? Schema.NullOr(Schema.String),
execute: (input) => executeTool(name, registration, input),
execute: (input) => Run.use((run) => run.executeTool(name, registration, input)),
})
}
const tools = renderTools(root)
return CodeMode.make<typeof tools>({ tools, extensions: [CodeModeWeb.extension], hooks })
const created = CodeMode.make<typeof tools>({ tools, extensions: [CodeModeWeb.extension], hooks: runHooks })
runtimes.set(inventory, created)
return created
}

const runHooks = {
"tool.before": (call) => Run.use((run) => run.hooks["tool.before"]?.(call) ?? Effect.void),
"tool.after": (call, result) => Run.use((run) => run.hooks["tool.after"]?.(call, result) ?? Effect.void),
"extension.before": (call) => Run.use((run) => run.hooks["extension.before"]?.(call) ?? Effect.void),
"extension.after": (call, result) => Run.use((run) => run.hooks["extension.after"]?.(call, result) ?? Effect.void),
} satisfies CodeMode.Hooks<Run>

function getNode<T>(root: Node<T>, path: string) {
return path.split(".").reduce((parent, name) => {
const child: Node<T> = parent.children.get(name) ?? { children: new Map() }
Expand All @@ -248,13 +271,13 @@ function getNode<T>(root: Node<T>, path: string) {
}

function renderTools(root: ToolNode) {
const callables = new Map<string, Tool.Tool<never>>()
const callables = new Map<string, Tool.Tool<Run>>()
const tools = renderChildren(root, [], callables)
for (const [path, tool] of callables) tools[path] = tool
return tools
}

function renderChildren(node: ToolNode, path: ReadonlyArray<string>, callables: Map<string, Tool.Tool<never>>): Tools {
function renderChildren(node: ToolNode, path: ReadonlyArray<string>, callables: Map<string, Tool.Tool<Run>>): Tools {
return Object.fromEntries(
Array.from(node.children).flatMap(([name, child]) => {
const next = [...path, name]
Expand All @@ -272,7 +295,7 @@ function renderChildren(node: ToolNode, path: ReadonlyArray<string>, callables:
function renderEntry(
node: ToolNode,
path: ReadonlyArray<string>,
callables: Map<string, Tool.Tool<never>>,
callables: Map<string, Tool.Tool<Run>>,
): Tools[string] {
const tools = renderChildren(node, path, callables)
// CodeMode merges this dotted tool path with the nested namespace entry.
Expand Down
21 changes: 11 additions & 10 deletions packages/core/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ const layer = Layer.effect(
}
})

let catalog: { data: Data; names: string; value: CodeModeCatalog.Inventory } | undefined
let catalog:
| { data: Data; names: string; inventory: CodeModeTool.Inventory; value: CodeModeCatalog.Inventory }
| undefined
const state = State.create<Data, Editor>({
name: "tool",
initial: () => ({
Expand Down Expand Up @@ -233,25 +235,24 @@ const layer = Layer.effect(
}
const direct = new Map(Array.from(active).filter(([, tool]) => tool.options?.codemode === false))
const codeModeTools = new Map(Array.from(active).filter(([, tool]) => tool.options?.codemode !== false))
const namespaces = data.namespaces
const codeModeInventory = { tools: codeModeTools, namespaces }
const names = Array.from(codeModeTools.keys()).join("\0")
const codeModeEnabled = !whollyDisabled("execute", rules)
// Discovery is immutable for a registry revision and visible tool set. Keep request
// definitions/executors fresh, but share the inventory, whose prepared runtime and much
// larger rendered catalog are cached by identity, across steps.
const cached = catalog?.data === data && catalog.names === names ? catalog : undefined
const codeModeInventory = cached?.inventory ?? { tools: codeModeTools, namespaces: data.namespaces }
const codeModeTool = codeModeEnabled
? CodeModeTool.create(codeModeInventory, (name, tool, input, context) =>
beforeExecute(name, input, context).pipe(
Effect.flatMap((event) => executeTool(tool, name, event.input, context)),
),
)
: undefined
const names = Array.from(codeModeTools.keys()).join("\0")
// Discovery is immutable for a registry revision and visible tool set. Keep request
// definitions/executors fresh, but share the much larger rendered catalog across steps.
const codeModeCatalog = !codeModeEnabled
? undefined
: catalog?.data === data && catalog.names === names
? catalog.value
: CodeModeTool.catalog(codeModeInventory)
if (codeModeCatalog) catalog = { data, names, value: codeModeCatalog }
: (cached?.value ?? CodeModeTool.catalog(codeModeInventory))
if (codeModeCatalog) catalog = { data, names, inventory: codeModeInventory, value: codeModeCatalog }
return {
...(codeModeCatalog === undefined ? {} : { codeModeCatalog }),
definitions: [
Expand Down
56 changes: 56 additions & 0 deletions packages/core/test/tool-execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,59 @@ test("execute supports callable namespace tools", async () => {
})
expect(result.content).toEqual([{ type: "text", text: '[\n "admin",\n "created"\n]' }])
})

test("executions share the prepared runtime of an inventory while keeping per-call state", async () => {
let conversions = 0
const echo: Info = {
name: "echo",
description: "Echo a value",
input: {
"~standard": {
version: 1,
vendor: "test",
validate: (value: unknown) => ({ value }),
jsonSchema: {
input: () => {
conversions++
return { type: "object", properties: { value: { type: "string" } }, required: ["value"] }
},
output: () => ({}),
},
},
},
execute: (input, context) => Effect.succeed({ content: `${context.id}:${(input as { value: string }).value}` }),
}
const inventory = { tools: new Map([["echo", echo]]) }
const codeMode = CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context))
const run = (id: string) => {
const progress: Array<Tool.Metadata> = []
return CodeModeTool.create(inventory, (_, tool, input, context) => execute(tool, input, context))
.execute(
{ code: `return await tools.echo({ value: "${id}" })` },
{ ...context, id: Tool.CallID.make(id), progress: (update) => Effect.sync(() => progress.push(update)) },
)
.pipe(Effect.map((result) => ({ output: result.output.output, progress })))
}

expect(CodeModeTool.catalog(inventory).tools).toHaveLength(1)
const results = await Effect.runPromise(Effect.all([run("first"), run("second")], { concurrency: "unbounded" }))
expect<unknown>(results).toEqual(
["first", "second"].map((id) => ({
output: `${id}:${id}`,
progress: [
{ toolCalls: [{ tool: "echo", status: "running", input: { value: id } }] },
{ toolCalls: [{ tool: "echo", status: "completed", input: { value: id } }] },
],
})),
)
expect(
(await Effect.runPromise(codeMode.execute({ code: 'return await tools.echo({ value: "x" })' }, context))).output
.output,
).toBe("call_execute:x")
expect(conversions).toBe(1)

await Effect.runPromise(
createCodeMode(new Map(inventory.tools)).execute({ code: 'return await tools.echo({ value: "next" })' }, context),
)
expect(conversions).toBe(2)
})
Loading