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
3 changes: 2 additions & 1 deletion packages/opencode/src/control-plane/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export type Target =

export type Adaptor = {
configure(input: WorkspaceInfo): WorkspaceInfo | Promise<WorkspaceInfo>
create(config: WorkspaceInfo, from?: WorkspaceInfo): Promise<void>
// from is reserved for future workspace copy flows; core does not pass it today.
create(config: WorkspaceInfo, env?: Record<string, string>, from?: WorkspaceInfo): Promise<void>
Comment thread
Astro-Han marked this conversation as resolved.
remove(config: WorkspaceInfo): Promise<void>
target(config: WorkspaceInfo): Target | Promise<Target>
}
22 changes: 18 additions & 4 deletions packages/opencode/src/control-plane/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Filesystem } from "@/util/filesystem"
import { ProjectID } from "@/project/schema"
import { Instance } from "@/project/instance"
import { Plugin } from "@/plugin"
import { Auth } from "@/auth"
import { AppRuntime } from "@/effect/app-runtime"
import { WorkspaceTable } from "./workspace.sql"
import { getAdaptor, getBuiltinAdaptor, ownerKey } from "./adaptors"
import { WorkspaceInfo } from "./types"
Expand Down Expand Up @@ -92,8 +94,8 @@ export namespace Workspace {

const candidates = [
...new Set(
[input.hint, input.owner, projectWorktree, ...project.sandboxes].filter(
(value): value is string => Boolean(value),
[input.hint, input.owner, projectWorktree, ...project.sandboxes].filter((value): value is string =>
Boolean(value),
),
),
]
Expand Down Expand Up @@ -137,7 +139,9 @@ export namespace Workspace {
throw lastError
}

export async function resolveAdaptor(input: Pick<StoredInfo, "projectID" | "type" | "owner"> & { hint?: string | null }) {
export async function resolveAdaptor(
input: Pick<StoredInfo, "projectID" | "type" | "owner"> & { hint?: string | null },
) {
const hint =
input.hint ??
(() => {
Expand Down Expand Up @@ -200,7 +204,17 @@ export namespace Workspace {
.run()
})

await adaptor.create(config)
const env = Object.fromEntries(
Object.entries({
OPENCODE_AUTH_CONTENT: JSON.stringify(await AppRuntime.runPromise(Auth.Service.use((auth) => auth.all()))),
Comment thread
Astro-Han marked this conversation as resolved.
OPENCODE_WORKSPACE_ID: info.id,
OPENCODE_EXPERIMENTAL_WORKSPACES: "true",
Comment thread
coderabbitai[bot] marked this conversation as resolved.
OTEL_EXPORTER_OTLP_HEADERS: process.env.OTEL_EXPORTER_OTLP_HEADERS,
OTEL_EXPORTER_OTLP_ENDPOINT: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_RESOURCE_ATTRIBUTES: process.env.OTEL_RESOURCE_ATTRIBUTES,
}).filter(([, value]) => value !== undefined),
) as Record<string, string>
await adaptor.create(config, env)

startSync({ space: info })

Expand Down
10 changes: 9 additions & 1 deletion packages/opencode/src/plugin/github-copilot/copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MessageV2 } from "@/session/message-v2"
const log = Log.create({ service: "plugin.copilot" })

const CLIENT_ID = "Ov23li8tweQw6odWQebz"
const COPILOT_ANTHROPIC_NPM = "@ai-sdk/anthropic"
// Add a small safety buffer when polling to avoid hitting the server
// slightly too early due to clock skew / timer drift.
const OAUTH_POLLING_SAFETY_MARGIN_MS = 3000 // 3 seconds
Expand Down Expand Up @@ -329,16 +330,23 @@ export async function CopilotAuthPlugin(input: PluginInput): Promise<Hooks> {
},
"chat.params": async (incoming, output) => {
if (!incoming.model.providerID.includes("github-copilot")) return
const isCopilotAnthropic = incoming.model.api.npm === COPILOT_ANTHROPIC_NPM

// Match github copilot cli, omit maxOutputTokens for gpt models
if (incoming.model.api.id.includes("gpt")) {
output.maxOutputTokens = undefined
}

// Copilot's /v1/messages shim rejects the eager_input_streaming field.
if (isCopilotAnthropic) {
output.options.toolStreaming = false
Comment thread
Astro-Han marked this conversation as resolved.
}
},
"chat.headers": async (incoming, output) => {
if (!incoming.model.providerID.includes("github-copilot")) return
const isCopilotAnthropic = incoming.model.api.npm === COPILOT_ANTHROPIC_NPM

if (incoming.model.api.npm === "@ai-sdk/anthropic") {
if (isCopilotAnthropic) {
output.headers["anthropic-beta"] = "interleaved-thinking-2025-05-14"
}

Expand Down
6 changes: 2 additions & 4 deletions packages/opencode/src/session/message-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import type { Provider } from "@/provider"
import { ModelID, ProviderID } from "@/provider/schema"
import { Effect } from "effect"
import { EffectLogger } from "@/effect"
import { isMedia } from "@/util/media"
export { isMedia } from "@/util/media"

/** Error shape thrown by Bun's fetch() when gzip/br decompression fails mid-stream */
interface FetchDecompressionError extends Error {
Expand All @@ -26,10 +28,6 @@ interface FetchDecompressionError extends Error {

export const SYNTHETIC_ATTACHMENT_PROMPT = "Attached image(s) from tool result:"

export function isMedia(mime: string) {
return mime.startsWith("image/") || mime === "application/pdf"
}

export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
export const AbortedError = NamedError.create("MessageAbortedError", z.object({ message: z.string() }))
export const StructuredOutputError = NamedError.create(
Expand Down
90 changes: 59 additions & 31 deletions packages/opencode/src/tool/read.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import z from "zod"
import { Effect, Scope } from "effect"
import { Effect, Option, Scope } from "effect"
import { createReadStream } from "fs"
import { open } from "fs/promises"
import * as path from "path"
import { createInterface } from "readline"
import { Tool } from "./tool"
Expand All @@ -11,12 +10,16 @@ import DESCRIPTION from "./read.txt"
import { Instance } from "../project/instance"
import { assertExternalDirectoryEffect } from "./external-directory"
import { Instruction } from "../session/instruction"
import { isImageAttachment, isPdfAttachment, sniffAttachmentMime } from "../util/media"

const DEFAULT_READ_LIMIT = 2000
const MAX_LINE_LENGTH = 2000
const MAX_LINE_SUFFIX = `... (line truncated to ${MAX_LINE_LENGTH} chars)`
const MAX_BYTES = 50 * 1024
const MAX_BYTES_LABEL = `${MAX_BYTES / 1024} KB`
const MAX_ATTACHMENT_BYTES = 5 * 1024 * 1024
const MAX_ATTACHMENT_BYTES_LABEL = `${MAX_ATTACHMENT_BYTES / 1024 / 1024} MB`
const SAMPLE_BYTES = 4096

const parameters = z.object({
filePath: z.string().describe("The absolute path to the file or directory to read"),
Expand Down Expand Up @@ -77,6 +80,18 @@ export const ReadTool = Tool.define(
yield* lsp.touchFile(filepath, false).pipe(Effect.ignore, Effect.forkIn(scope))
})

const readSample = Effect.fn("ReadTool.sample")(function* (filepath: string, fileSize: number) {
if (fileSize === 0) return new Uint8Array()

return yield* Effect.scoped(
Effect.gen(function* () {
const file = yield* fs.open(filepath, { flag: "r" })
const bytes = yield* file.readAlloc(Math.min(SAMPLE_BYTES, fileSize))
return Option.getOrElse(bytes, () => new Uint8Array())
}),
)
})

const run = Effect.fn("ReadTool.execute")(function* (params: z.infer<typeof parameters>, ctx: Tool.Context) {
if (params.offset !== undefined && params.offset < 1) {
return yield* Effect.fail(new Error("offset must be greater than or equal to 1"))
Expand Down Expand Up @@ -142,10 +157,23 @@ export const ReadTool = Tool.define(

const loaded = yield* instruction.resolve(ctx.messages, filepath, ctx.messageID)

const mime = AppFileSystem.mimeType(filepath)
const isImage = mime.startsWith("image/") && mime !== "image/svg+xml" && mime !== "image/vnd.fastbidsheet"
const isPdf = mime === "application/pdf"
if (isBinaryByExt(filepath) && !shouldSniffBeforeBinaryExt(filepath)) {
Comment thread
Astro-Han marked this conversation as resolved.
return yield* Effect.fail(
new Error(`Cannot read binary file (extension: ${path.extname(filepath).toLowerCase()}): ${filepath}`),
)
}

const sample = yield* readSample(filepath, Number(stat.size))
Comment thread
Astro-Han marked this conversation as resolved.
Comment thread
Astro-Han marked this conversation as resolved.
const mime = sniffAttachmentMime(sample, AppFileSystem.mimeType(filepath))
const isImage = isImageAttachment(mime)
const isPdf = isPdfAttachment(mime)
if (isImage || isPdf) {
if (Number(stat.size) > MAX_ATTACHMENT_BYTES) {
return yield* Effect.fail(
new Error(`Cannot read attachment larger than ${MAX_ATTACHMENT_BYTES_LABEL}: ${filepath}`),
)
}

const msg = `${isImage ? "Image" : "PDF"} read successfully`
return {
title,
Expand All @@ -165,8 +193,8 @@ export const ReadTool = Tool.define(
}
}

if (yield* Effect.promise(() => isBinaryFile(filepath, Number(stat.size)))) {
return yield* Effect.fail(new Error(`Cannot read binary file: ${filepath}`))
if (isBinaryFile(filepath, sample)) {
return yield* Effect.fail(new Error(`Cannot read binary file (content inspection): ${filepath}`))
}

const file = yield* Effect.promise(() =>
Expand Down Expand Up @@ -262,7 +290,29 @@ async function lines(filepath: string, opts: { limit: number; offset: number })
return { raw, count, cut, more, offset: opts.offset }
}

async function isBinaryFile(filepath: string, fileSize: number): Promise<boolean> {
function isBinaryFile(filepath: string, sample: Uint8Array): boolean {
if (isBinaryByExt(filepath)) return true

if (sample.byteLength === 0) return false

let nonPrintableCount = 0
for (let i = 0; i < sample.byteLength; i++) {
if (sample[i] === 0) return true
if (sample[i] < 9 || (sample[i] > 13 && sample[i] < 32)) {
nonPrintableCount++
}
}
// If >30% non-printable characters, consider it binary
return nonPrintableCount / sample.byteLength > 0.3
}

function shouldSniffBeforeBinaryExt(filepath: string): boolean {
Comment thread
Astro-Han marked this conversation as resolved.
const ext = path.extname(filepath).toLowerCase()
// These common generic binary extensions may still contain renamed image/PDF attachments.
return ext === ".bin" || ext === ".dat"
}

function isBinaryByExt(filepath: string): boolean {
const ext = path.extname(filepath).toLowerCase()
// binary check for common non-text extensions
switch (ext) {
Expand Down Expand Up @@ -296,28 +346,6 @@ async function isBinaryFile(filepath: string, fileSize: number): Promise<boolean
case ".pyo":
return true
default:
break
}

if (fileSize === 0) return false

const fh = await open(filepath, "r")
try {
const sampleSize = Math.min(4096, fileSize)
const bytes = Buffer.alloc(sampleSize)
const result = await fh.read(bytes, 0, sampleSize, 0)
if (result.bytesRead === 0) return false

let nonPrintableCount = 0
for (let i = 0; i < result.bytesRead; i++) {
if (bytes[i] === 0) return true
if (bytes[i] < 9 || (bytes[i] > 13 && bytes[i] < 32)) {
nonPrintableCount++
}
}
// If >30% non-printable characters, consider it binary
return nonPrintableCount / result.bytesRead > 0.3
} finally {
await fh.close()
return false
}
}
2 changes: 1 addition & 1 deletion packages/opencode/src/tool/skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const SkillTool = Tool.define(
const base = pathToFileURL(dir).href

const limit = 10
const files = yield* rg.files({ cwd: dir, follow: false, hidden: true }).pipe(
const files = yield* rg.files({ cwd: dir, follow: false, hidden: true, signal: ctx.abort }).pipe(
Stream.filter((file) => !file.includes("SKILL.md")),
Stream.map((file) => path.resolve(dir, file)),
Stream.take(limit),
Expand Down
3 changes: 2 additions & 1 deletion packages/opencode/src/tool/webfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { HttpClient, HttpClientRequest } from "effect/unstable/http"
import * as Tool from "./tool"
import TurndownService from "turndown"
import DESCRIPTION from "./webfetch.txt"
import { isImageAttachment } from "../util/media"

const MAX_RESPONSE_SIZE = 5 * 1024 * 1024 // 5MB
const DEFAULT_TIMEOUT = 30 * 1000 // 30 seconds
Expand Down Expand Up @@ -105,7 +106,7 @@ export const WebFetchTool = Tool.define(
const title = `${params.url} (${contentType})`

// Check if response is an image
const isImage = mime.startsWith("image/") && mime !== "image/svg+xml" && mime !== "image/vnd.fastbidsheet"
const isImage = isImageAttachment(mime)

if (isImage) {
const base64Content = Buffer.from(arrayBuffer).toString("base64")
Expand Down
49 changes: 49 additions & 0 deletions packages/opencode/src/util/media.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const startsWith = (bytes: Uint8Array, prefix: number[]) =>
bytes.length >= prefix.length && prefix.every((value, index) => bytes[index] === value)
const startsWithAt = (bytes: Uint8Array, offset: number, prefix: number[]) =>
bytes.length >= offset + prefix.length && prefix.every((value, index) => bytes[offset + index] === value)

const ascii = (value: string) => [...value].map((char) => char.charCodeAt(0))
const brand = (bytes: Uint8Array, offset: number) => String.fromCharCode(...bytes.slice(offset, offset + 4))
const u32be = (bytes: Uint8Array, offset: number) =>
bytes.length >= offset + 4
? ((bytes[offset] << 24) | (bytes[offset + 1] << 16) | (bytes[offset + 2] << 8) | bytes[offset + 3]) >>> 0
: 0

export function isPdfAttachment(mime: string) {
return mime === "application/pdf"
}

export function isMedia(mime: string) {
return mime.startsWith("image/") || isPdfAttachment(mime)
}

export function isImageAttachment(mime: string) {
return mime.startsWith("image/") && mime !== "image/svg+xml" && mime !== "image/vnd.fastbidsheet"
}

export function sniffAttachmentMime(bytes: Uint8Array, fallback: string) {
if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return "image/png"
if (startsWith(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg"
if (startsWith(bytes, [0x47, 0x49, 0x46, 0x38])) return "image/gif"
if (startsWith(bytes, [0x42, 0x4d])) return "image/bmp"
if (startsWith(bytes, [0x49, 0x49, 0x2a, 0x00]) || startsWith(bytes, [0x4d, 0x4d, 0x00, 0x2a])) return "image/tiff"
if (startsWith(bytes, [0x25, 0x50, 0x44, 0x46, 0x2d])) return "application/pdf"
if (
startsWith(bytes, [0x52, 0x49, 0x46, 0x46]) &&
bytes.length >= 12 &&
startsWith(bytes.slice(8, 12), [0x57, 0x45, 0x42, 0x50])
)
return "image/webp"
Comment thread
Astro-Han marked this conversation as resolved.
if (startsWithAt(bytes, 4, ascii("ftyp"))) {
const boxSize = u32be(bytes, 0)
const limit = Math.min(boxSize > 0 ? boxSize : bytes.length, bytes.length)
const brands = []
for (let offset = 8; offset + 4 <= limit; offset += 4) {
brands.push(brand(bytes, offset))
}
if (brands.some((item) => item === "avif" || item === "avis")) return "image/avif"
if (brands.some((item) => ["heic", "heix", "hevc", "hevx", "mif1", "msf1"].includes(item))) return "image/heic"
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return fallback
}
Loading
Loading