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
2 changes: 1 addition & 1 deletion packages/core/src/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { Agent } from "./agent"
export { Model } from "./model"
export { OpenCode } from "./opencode"
export { Session } from "./session"
export * as Tool from "./tool"
export { Tool } from "./tool"
export { Location } from "./location"
export { Prompt } from "../session/prompt"
export { AbsolutePath } from "../schema"
2 changes: 1 addition & 1 deletion packages/core/src/public/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Effect, Scope } from "effect"
import type { AnyTool, RegistrationError } from "../tool/tool"

export { Failure, RegistrationError, make } from "../tool/tool"
export type { AnyTool, Content, Context } from "../tool/tool"
export type { AnyTool, Content, Context, Definition } from "../tool/tool"

export interface Interface {
/**
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/session/runner/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ export const layer = Layer.effect(
yield* FiberSet.clear(toolFibers)
yield* withPublication(publisher.failUnsettledTools("Tool execution interrupted"))
}
if (settled._tag === "Failure" && !Cause.hasInterrupts(settled.cause)) {
const failure = Cause.squash(settled.cause)
const message = failure instanceof Error ? failure.message : String(failure)
yield* withPublication(publisher.failUnsettledTools(`Tool execution failed: ${message}`))
}
if (publisher.hasProviderError())
yield* withPublication(publisher.failUnsettledTools("Tool execution interrupted"))
if (stream._tag === "Success" && !publisher.hasProviderError())
Expand Down
36 changes: 23 additions & 13 deletions packages/core/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,40 @@ export function create<State extends Objectish, Editor>(options: Options<State,
state = next
})

const rebuild = Effect.fn("State.rebuild")(function* () {
const rebuild = Effect.fnUntraced(function* () {
const next = options.initial()
const api = options.editor(next as Draft<State>)
for (const transform of transforms)
yield* Effect.sync(() => transform.update(api)).pipe(Effect.withSpan("State.rebuild.update", {}))
yield* commit(next)
}, semaphore.withPermit)
})

return {
get: () => state,
transform: Effect.fn("State.transform")(function* () {
const transform = { update: (_editor: Editor) => {} }
transforms = [...transforms, transform]
const scope = yield* Scope.Scope
yield* Scope.addFinalizer(
scope,
Effect.sync(() => {
transforms = transforms.filter((item) => item !== transform)
}).pipe(Effect.andThen(rebuild())),
return yield* Effect.uninterruptible(
Effect.gen(function* () {
const transform = { update: (_editor: Editor) => {} }
transforms = [...transforms, transform]
yield* Scope.addFinalizer(
scope,
semaphore.withPermit(
Effect.sync(() => {
transforms = transforms.filter((item) => item !== transform)
}).pipe(Effect.andThen(rebuild())),
),
)
return (update: Transform<Editor>) =>
Effect.uninterruptible(
semaphore.withPermit(
Effect.sync(() => {
transform.update = update
}).pipe(Effect.andThen(rebuild())),
),
)
}),
)
return Effect.fnUntraced(function* (update: Transform<Editor>) {
transform.update = update
yield* rebuild()
})
}),
update: Effect.fn("State.update")(function* (update, reason) {
const api = options.editor(state as Draft<State>)
Expand Down
45 changes: 17 additions & 28 deletions packages/core/src/tool-output-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { ToolOutput } from "@opencode-ai/llm"

export const MAX_LINES = 2_000
export const MAX_BYTES = 50 * 1024
export const MAX_INLINE_MEDIA_BYTES = 5 * 1024 * 1024
export const RETENTION = Duration.days(7)

export const MANAGED_DIRECTORY = "tool-output"
Expand All @@ -32,13 +31,7 @@ export class StorageError extends Schema.TaggedErrorClass<StorageError>()("ToolO
cause: Schema.Defect,
}) {}

export class MediaLimitError extends Schema.TaggedErrorClass<MediaLimitError>()("ToolOutputStore.MediaLimitError", {
mime: Schema.String,
bytes: Schema.Int,
limit: Schema.Int,
}) {}

export type Error = StorageError | MediaLimitError
export type Error = StorageError

export interface Interface {
readonly limits: () => Effect.Effect<{ readonly maxLines: number; readonly maxBytes: number }>
Expand Down Expand Up @@ -139,37 +132,33 @@ export const layer = Layer.effect(
const bound = Effect.fn("ToolOutputStore.bound")(function* (input: BoundInput) {
const outputLimits = yield* limits()
const media = input.output.content.filter((item) => item.type === "file")
let mediaBytes = 0
for (const item of media) {
if (item.source.type !== "data") continue
mediaBytes += Buffer.byteLength(item.source.data, "utf-8")
if (mediaBytes > MAX_INLINE_MEDIA_BYTES)
return yield* new MediaLimitError({ mime: item.mime, bytes: mediaBytes, limit: MAX_INLINE_MEDIA_BYTES })
}
const contextual = {
structured: media.length > 0 ? {} : input.output.structured,
content: input.output.content.filter((item) => item.type === "text"),
}
const encoded = yield* Effect.try({
try: () => JSON.stringify(contextual, null, 2),
catch: (cause) => new StorageError({ operation: "encode", cause }),
})
if (lineCount(encoded) <= outputLimits.maxLines && Buffer.byteLength(encoded, "utf-8") <= outputLimits.maxBytes)
const text = input.output.content.filter((item) => item.type === "text")
const contextual =
input.output.content.length === 0
? yield* Effect.try({
try: () => JSON.stringify(input.output.structured, null, 2) ?? String(input.output.structured),
catch: (cause) => new StorageError({ operation: "encode", cause }),
})
: text.map((item) => item.text).join("")
if (
lineCount(contextual) <= outputLimits.maxLines &&
Buffer.byteLength(contextual, "utf-8") <= outputLimits.maxBytes
)
return {
output: { structured: contextual.structured, content: input.output.content },
output: input.output,
outputPaths: [],
}

const outputPath = yield* write(encoded)
const outputPath = yield* write(contextual)
const marker = `... output truncated; full content saved to ${outputPath} ...`

return {
output: {
structured: {},
structured: input.output.structured,
content: [
{
type: "text" as const,
text: boundedPreview(encoded, marker, outputLimits.maxLines, outputLimits.maxBytes),
text: boundedPreview(contextual, marker, outputLimits.maxLines, outputLimits.maxBytes),
},
...media,
],
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/tool/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ Producer capture limits are separate. For example, Bash keeps `AppProcess.maxOut

- Plugin boot has not been redesigned to register canonical tools through `Tools.Service`; do not redesign it as part of leaf migrations.
- MCP and future Session-scoped registrations still need an explicit canonical registration design.
- The public Session result shape currently exposes managed `outputPaths`; full storage encapsulation requires a future opaque managed-output reference design.
12 changes: 6 additions & 6 deletions packages/core/src/tool/apply-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Tools } from "./tools"

export const name = "apply_patch"

export const Parameters = Schema.Struct({
export const Input = Schema.Struct({
patchText: Schema.String.annotate({
description: "The full patch text describing add, update, and delete operations",
}),
Expand All @@ -24,10 +24,10 @@ export const Applied = Schema.Struct({
target: Schema.String,
})

export const Success = Schema.Struct({ applied: Schema.Array(Applied) })
export type Success = typeof Success.Type
export const Output = Schema.Struct({ applied: Schema.Array(Applied) })
export type Output = typeof Output.Type

export const toModelOutput = (output: Success) =>
export const toModelOutput = (output: Output) =>
[
"Applied patch sequentially:",
...output.applied.map(
Expand Down Expand Up @@ -57,8 +57,8 @@ export const layer = Layer.effectDiscard(
Tool.make({
description:
"Apply one patch containing add, update, and delete file operations. All targets are resolved and approved before target contents are read. Operations apply sequentially; if a later operation fails, earlier operations remain applied and the failure reports them explicitly. Moves and atomic rollback are not supported yet.",
input: Parameters,
output: Success,
input: Input,
output: Output,
toModelOutput: ({ output }) => [toolText({ type: "text", text: toModelOutput(output) })],
execute: (input, context) => {
const applied: Array<typeof Applied.Type> = []
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/tool/bash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const DEFAULT_TIMEOUT_MS = 2 * 60 * 1_000
export const MAX_TIMEOUT_MS = 10 * 60 * 1_000
export const MAX_CAPTURE_BYTES = 1024 * 1024

export const Parameters = Schema.Struct({
export const Input = Schema.Struct({
command: Schema.String.annotate({ description: "Shell command string to execute" }),
workdir: Schema.String.pipe(Schema.optional).annotate({
description: "Working directory. Defaults to the active Location; relative paths resolve from that Location.",
Expand All @@ -33,7 +33,7 @@ export const Parameters = Schema.Struct({
}),
})

const Success = Schema.Struct({
const Output = Schema.Struct({
command: Schema.String,
cwd: Schema.String,
exitCode: Schema.Number.pipe(Schema.optional),
Expand All @@ -46,7 +46,7 @@ const Success = Schema.Struct({
warnings: Schema.Array(Schema.String).pipe(Schema.optional),
})

type Success = typeof Success.Type
type Output = typeof Output.Type

const defaultShell = () => (process.platform === "win32" ? (process.env.COMSPEC ?? "cmd.exe") : "/bin/sh")

Expand All @@ -62,7 +62,7 @@ const captureNotice = (stdoutTruncated: boolean, stderrTruncated: boolean) => {
return undefined
}

const modelOutput = (output: Success) => {
const modelOutput = (output: Output) => {
const warnings = output.warnings?.length
? `\n\nWarnings:\n${output.warnings.map((warning) => `- ${warning}`).join("\n")}`
: ""
Expand Down Expand Up @@ -117,8 +117,8 @@ export const layer = Layer.effectDiscard(
.register({
[name]: Tool.make({
description: `Execute one shell command string with the host user's filesystem, process, and network authority. The active Location is the default working directory. Relative workdir values resolve from that Location. External workdir values require external_directory approval; best-effort command-argument path warnings are advisory only. Timeout values are milliseconds (default: ${DEFAULT_TIMEOUT_MS}; maximum: ${MAX_TIMEOUT_MS}). Uses the configured shell when set; otherwise uses /bin/sh on POSIX and COMSPEC or cmd.exe on Windows.`,
input: Parameters,
output: Success,
input: Input,
output: Output,
toModelOutput: ({ output }) => [toolText({ type: "text", text: modelOutput(output) })],
execute: (input, context) =>
Effect.gen(function* () {
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/tool/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Tools } from "./tools"

export const name = "edit"

export const Parameters = Schema.Struct({
export const Input = Schema.Struct({
path: Schema.String.annotate({
description:
"File path to edit. Relative paths resolve within the active Location. Absolute paths inside that Location are accepted; external absolute paths require external_directory approval. Named project references are read-oriented and are not accepted.",
Expand All @@ -30,14 +30,14 @@ export const Parameters = Schema.Struct({
}),
})

export const Success = Schema.Struct({
export const Output = Schema.Struct({
operation: Schema.Literal("write"),
target: Schema.String,
resource: Schema.String,
existed: Schema.Boolean,
replacements: Schema.Number,
})
export type Success = typeof Success.Type
export type Output = typeof Output.Type

const normalizeLineEndings = (text: string) => text.replaceAll("\r\n", "\n")
const detectLineEnding = (text: string): "\n" | "\r\n" => (text.includes("\r\n") ? "\r\n" : "\n")
Expand Down Expand Up @@ -70,7 +70,7 @@ const previewLines = (value: string, prefix: "+" | "-") => {
return shown
}

export const toModelOutput = (output: Success, oldString: string, newString: string) =>
export const toModelOutput = (output: Output, oldString: string, newString: string) =>
[
`Edited file successfully: ${output.resource}`,
`Replacements: ${output.replacements}`,
Expand Down Expand Up @@ -101,8 +101,8 @@ export const layer = Layer.effectDiscard(
Tool.make({
description:
"Replace exact text in one file. Relative paths resolve within the active Location. Absolute paths inside the Location are accepted. Explicit external absolute paths require external_directory approval before edit approval. Named project references are read-oriented and are not accepted.",
input: Parameters,
output: Success,
input: Input,
output: Output,
toModelOutput: ({ input, output }) => [
toolText({ type: "text", text: toModelOutput(output, input.oldString, input.newString) }),
],
Expand Down Expand Up @@ -188,7 +188,7 @@ export const layer = Layer.effectDiscard(
content: joinBom(next.text, source.bom || next.bom),
}),
)
return { ...result, replacements } satisfies Success
return { ...result, replacements } satisfies Output
})
},
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/tool/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Tools } from "./tools"

export const name = "glob"

export const Parameters = Schema.Struct({
export const Input = Schema.Struct({
pattern: LocationSearch.FilesInput.fields.pattern.annotate({ description: "Glob pattern to match files against" }),
path: LocationSearch.FilesInput.fields.path.annotate({
description: "Relative directory to search. Defaults to the active Location.",
Expand Down Expand Up @@ -56,7 +56,7 @@ export const layer = Layer.effectDiscard(
[name]: Tool.make({
description:
"Find files by glob pattern within the active Location or a named project reference. Returns concise relative file resources. Use a relative path to narrow the search and limit to bound the result count.",
input: Parameters,
input: Input,
output: LocationSearch.FilesResult,
toModelOutput: ({ output }) => [toolText({ type: "text", text: toModelOutput(output) })],
execute: (input, context) =>
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/tool/grep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Tools } from "./tools"

export const name = "grep"

export const Parameters = Schema.Struct({
export const Input = Schema.Struct({
pattern: LocationSearch.GrepInput.fields.pattern.annotate({
description: "Regex pattern to search for in file contents",
}),
Expand All @@ -29,10 +29,10 @@ export const Parameters = Schema.Struct({
}),
})

type Success = typeof LocationSearch.GrepResult.Encoded
type Output = typeof LocationSearch.GrepResult.Encoded

/** Format raw Location search matches into the familiar concise model output. */
export const toModelOutput = (output: Success) => {
export const toModelOutput = (output: Output) => {
const lines = output.items.length === 0 ? ["No files found"] : [`Found ${output.items.length} matches`]
let current = ""
for (const match of output.items) {
Expand Down Expand Up @@ -71,7 +71,7 @@ export const layer = Layer.effectDiscard(
[name]: Tool.make({
description:
"Search file contents by regular expression within the active Location, a named project reference, or an absolute managed tool-output file. Use a path to narrow the search, include to filter files by glob, and limit to bound the match count. Returns concise file resources, line numbers, and bounded line previews.",
input: Parameters,
input: Input,
output: LocationSearch.GrepResult,
toModelOutput: ({ output }) => [toolText({ type: "text", text: toModelOutput(output) })],
execute: (input, context) =>
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/tool/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ Usage notes:
- Answers are returned as arrays of labels; set \`multiple: true\` to allow selecting more than one
- If you recommend a specific option, make that the first option in the list and add "(Recommended)" at the end of the label`

export const Parameters = Schema.Struct({
export const Input = Schema.Struct({
questions: Schema.Array(QuestionV2.Prompt).annotate({ description: "Questions to ask" }),
})

export const Success = Schema.Struct({
export const Output = Schema.Struct({
answers: Schema.Array(QuestionV2.Answer),
})
export type Success = typeof Success.Type
export type Output = typeof Output.Type

export const toModelOutput = (
questions: ReadonlyArray<QuestionV2.Prompt>,
Expand All @@ -52,8 +52,8 @@ export const layer = Layer.effectDiscard(
.register({
[name]: Tool.make({
description,
input: Parameters,
output: Success,
input: Input,
output: Output,
toModelOutput: ({ input, output }) => [
toolText({ type: "text", text: toModelOutput(input.questions, output.answers) }),
],
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/tool/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const LocationInput = Schema.Struct({
}),
})
const Input = LocationInput
const Success = Schema.Union([FileSystem.Content, FileSystem.TextPage, FileSystem.ListPage])
const Output = Schema.Union([FileSystem.Content, FileSystem.TextPage, FileSystem.ListPage])

export const layer = Layer.effectDiscard(
Effect.gen(function* () {
Expand All @@ -35,7 +35,7 @@ export const layer = Layer.effectDiscard(
description:
"Read a text file or supported image, page through a large UTF-8 text file by line offset, or list a directory page relative to the current location. Absolute paths are accepted only for managed tool-output files.",
input: Input,
output: Success,
output: Output,
toModelOutput: ({ input, output }) => {
if (!("type" in output) || output.type !== "binary" || !SUPPORTED_IMAGE_MIMES.has(output.mime)) return []
return [
Expand Down
Loading
Loading