GeezerVison is showing github.com — fix(core): register a new ChatGPT client when adding an account · anomalyco/opencode@46df5d6
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/automation.ts +14-3Lines changed: 14 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -254,14 +254,25 @@ const layer = Layer.effect( | 254 | 254 | const doorbell = yield* Latch.make() | 255 | 255 | const watches = yield* FiberMap.make<ID>() | 256 | 256 |
| | 257 | + /** | | 258 | + * Runs `effect` with `owner`'s Session capabilities. A plugin provides its instance's selector, which can serve the | | 259 | + * instance while it is still booting. | | 260 | + */ | | 261 | + const inSession = | | 262 | + (owner: SessionSchema.Info) => | | 263 | + <A, E, R>(effect: Effect.Effect<A, E, R>) => | | 264 | + Effect.flatMap(Effect.serviceOption(Instance.Service), (selector) => | | 265 | + Option.getOrElse(selector, () => instances).provide(owner)(effect), | | 266 | + ) | | 267 | + | 257 | 268 | /** | 258 | 269 | * The agent a run's child Session starts with, if `owner` could start it with the subagent tool: not hidden, not | 259 | 270 | * primary-only, within the depth limit and already allowed in the owner's current Location. The code/tools | 260 | 271 | * authorized when the automation was created do not grant permission to start a child. | 261 | 272 | */ | 262 | 273 | const subagent = (owner: SessionSchema.Info, id: Agent.ID, invocation?: Invocation) => | 263 | 274 | Effect.gen(function* () { | 264 | | - yield* Plugin.awaitActivation | | 275 | + yield* AutomationCode.awaitPlugins.pipe(Effect.mapError((reason) => new InvalidError({ reason }))) | 265 | 276 | const location = yield* Location.Service | 266 | 277 | if (location.project.id !== owner.projectID) | 267 | 278 | return yield* new InvalidError({ reason: "The creating session's location is no longer in its project" }) | @@ -296,7 +307,7 @@ const layer = Layer.effect( | 296 | 307 | ), | 297 | 308 | ) | 298 | 309 | return agent | 299 | | - }).pipe(instances.provide(owner)) | | 310 | + }).pipe(inSession(owner)) | 300 | 311 |
| 301 | 312 | /** Runs `effect` in the Location of `directory`; fails with DirectoryNotFoundError when the folder is gone. */ | 302 | 313 | const inDirectory = | @@ -1096,7 +1107,7 @@ const layer = Layer.effect( | 1096 | 1107 | if (action.type === "shell") { | 1097 | 1108 | if (!owner) return yield* new InvalidError({ reason: "A shell action needs a session" }) | 1098 | 1109 | yield* AutomationShell.authorize(owner, input.name, trigger, action, invocation).pipe( | 1099 | | - instances.provide(owner), | | 1110 | + inSession(owner), | 1100 | 1111 | Effect.mapError((error) => (typeof error === "string" ? new InvalidError({ reason: error }) : error)), | 1101 | 1112 | ) | 1102 | 1113 | return action | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/automation/code.ts +20-2Lines changed: 20 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,7 @@ export * as AutomationCode from "./code.js" | 2 | 2 |
| 3 | 3 | import { CodeMode } from "@opencode/codemode" | 4 | 4 | import { Automation } from "@opencode/schema/automation" | 5 | | -import { Array, Effect, Option, Result, Schema } from "effect" | | 5 | +import { Array, Context, Effect, Option, Result, Schema } from "effect" | 6 | 6 | import { Agent } from "../agent.js" | 7 | 7 | import { CodeModeTool } from "../codemode/tool.js" | 8 | 8 | import { Permission } from "../permission.js" | @@ -83,6 +83,24 @@ function scanProgram(code: string): Result.Result<ProgramScan, string> { | 83 | 83 | return Result.succeed({ tools: scanned.success.tools, actions: extensionActions(scanned.success) }) | 84 | 84 | } | 85 | 85 |
| | 86 | +/** The calling plugin's registry. SDK instances and the host can have separate registries at the same Location. */ | | 87 | +export const FromPlugin = Context.Reference<Plugin.Interface | undefined>("@opencode/AutomationCode/FromPlugin", { | | 88 | + defaultValue: () => undefined, | | 89 | +}) | | 90 | + | | 91 | +/** | | 92 | + * Waits for this registry's plugins. A create from a plugin in this same registry is refused instead while it activates: | | 93 | + * its activation may be waiting on that plugin's setup, and configured permissions are applied by plugins that load later. | | 94 | + */ | | 95 | +export const awaitPlugins = Effect.gen(function* () { | | 96 | + const plugins = yield* Plugin.Service | | 97 | + if ((yield* FromPlugin) !== plugins) return yield* plugins.awaitActivation | | 98 | + if (yield* plugins.activated) return | | 99 | + return yield* Effect.fail( | | 100 | + "Plugins in this location are still loading, so configured permissions are not applied yet. Create this automation after plugin setup, for example from an event handler.", | | 101 | + ) | | 102 | +}) | | 103 | + | 86 | 104 | /** | 87 | 105 | * Runs in the creating Session's Location while the user is present: asks once for tool and extension permissions (and for | 88 | 106 | * `execute` if none of those needed approval), without saving, so every run can be allowed unattended. | @@ -95,7 +113,7 @@ export const authorize = Effect.fnUntraced(function* ( | 95 | 113 | invocation?: Invocation, | 96 | 114 | ) { | 97 | 115 | const scanned = yield* Effect.fromResult(scan(action.code)) | 98 | | - yield* Plugin.awaitActivation | | 116 | + yield* awaitPlugins | 99 | 117 | const permission = yield* Permission.Service | 100 | 118 | const agents = yield* Agent.Service | 101 | 119 | const agent = yield* agents.select(session.agent) | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/automation/shell.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ import { Shell } from "../shell.js" | 12 | 12 | import { ShellPermissions } from "../shell/permissions.js" | 13 | 13 | import { ShellSelect } from "../shell/select.js" | 14 | 14 | import { type Invocation, matchesAuthor } from "./author.js" | | 15 | +import { AutomationCode } from "./code.js" | 15 | 16 |
| 16 | 17 | export const authorize = Effect.fnUntraced(function* ( | 17 | 18 | owner: SessionSchema.Info, | @@ -20,7 +21,7 @@ export const authorize = Effect.fnUntraced(function* ( | 20 | 21 | action: Automation.ShellAction, | 21 | 22 | invocation?: Invocation, | 22 | 23 | ) { | 23 | | - if (invocation) yield* Plugin.awaitActivation | | 24 | + if (invocation || (yield* AutomationCode.FromPlugin)) yield* AutomationCode.awaitPlugins | 24 | 25 | const agents = yield* Agent.Service | 25 | 26 | const agent = yield* agents.select(owner.agent) | 26 | 27 | if (invocation && !matchesAuthor(owner, agent.info, invocation)) | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/environment/environment.ts +13-23Lines changed: 13 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { CrossSpawnSpawner } from "@opencode/util/cross-spawn-spawner" | 2 | 2 | import { makeLocationNode } from "@opencode/util/effect/app-node" | 3 | | -import { Global } from "@opencode/util/global" | 4 | 3 | import { Context, Effect, Layer, PlatformError, Schema } from "effect" | 5 | 4 | import { ChildProcessSpawner } from "effect/process" | 6 | 5 | import { posix, type PlatformPath } from "node:path" | @@ -27,40 +26,33 @@ export const Place = Schema.Struct({ | 27 | 26 | }) | 28 | 27 | export type Place = typeof Place.Type | 29 | 28 |
| 30 | | -/** Target properties supplied by whoever constructs an Environment. */ | 31 | | -export interface Options { | 32 | | - /** | 33 | | - * True only when commands and files live on the server's own OS process (false on workerd and all remotes): | 34 | | - * commands start from the server's variables, cwds resolve here, the process daemon can hold them, and host | 35 | | - * formatters and worktree strategies reach its files. | 36 | | - */ | 37 | | - readonly local?: boolean | | 29 | +/** What an environment says about itself, set by whoever makes it. Absent means no, or the server's own. */ | | 30 | +export interface Facts { | 38 | 31 | /** The path rules of its files, when they are not the server's. */ | 39 | 32 | readonly path?: PlatformPath | 40 | 33 | /** The shell every command there runs under, when it is not the configured one. */ | 41 | 34 | readonly shell?: string | 42 | | - /** Resolves `~` in this environment when available (host homedir locally, `hello.home` on paired machines). */ | 43 | | - readonly home?: Effect.Effect<string, unknown> | 44 | 35 | /** | 45 | | - * Ripgrep command to spawn in this environment (`undefined` means search is unavailable). | 46 | | - * Local environments (`local: true`) use the bundled `RipgrepBinary` when `rg` is set. | | 36 | + * Whether this is the computer the server itself runs on, so a command there is the server's own child: it starts | | 37 | + * from the server's variables, its cwd is made absolute here, the server's process daemon can hold it, and the | | 38 | + * server's formatters reach its files. | 47 | 39 | */ | 48 | | - readonly rg?: Effect.Effect<string, unknown> | | 40 | + readonly hostsServer?: boolean | | 41 | + /** Whether a search can run over its files. It goes when the search tools no longer need a ripgrep binary there. */ | | 42 | + readonly searchable?: boolean | 49 | 43 | } | 50 | 44 |
| 51 | | -export interface Interface extends Options { | | 45 | +export interface Interface extends Facts { | 52 | 46 | readonly id: ID | 53 | | - readonly local: boolean | 54 | 47 | readonly files: Files | 55 | 48 | readonly spawner: ChildProcessSpawner.ChildProcessSpawner["Service"] | 56 | 49 | } | 57 | 50 |
| 58 | 51 | export class Service extends Context.Service<Service, Interface>()("@opencode/Environment") {} | 59 | 52 |
| 60 | | -export const make = (id: ID, driver: Driver, options?: Options): Interface => ({ | 61 | | - ...options, | | 53 | +export const make = (id: ID, driver: Driver, facts?: Facts): Interface => ({ | | 54 | + ...facts, | 62 | 55 | id, | 63 | | - local: options?.local === true, | 64 | 56 | files: makeFiles(driver), | 65 | 57 | spawner: driver.spawner, | 66 | 58 | }) | @@ -71,14 +63,12 @@ export const local = (spawner: ChildProcessSpawner.ChildProcessSpawner["Service" | 71 | 63 | localID, | 72 | 64 | makeLocalDriver(spawner), | 73 | 65 | // Runtimes without local process execution (such as workerd) cannot host local commands or search. | 74 | | - spawner === EnvironmentUnavailable.spawner | 75 | | - ? {} | 76 | | - : { local: true, home: Effect.sync(() => Global.Path.home), rg: Effect.succeed("rg") }, | | 66 | + spawner === EnvironmentUnavailable.spawner ? {} : { hostsServer: true, searchable: true }, | 77 | 67 | ) | 78 | 68 |
| 79 | 69 | /** A workspace sandbox environment; runs ripgrep through its own spawner and uses POSIX path rules. */ | 80 | 70 | export const workspace = (id: Workspace.ID, driver: Driver) => | 81 | | - make(ID.make(id), driver, { path: posix, rg: Effect.succeed("rg") }) | | 71 | + make(ID.make(id), driver, { path: posix, searchable: true }) | 82 | 72 |
| 83 | 73 | /** | 84 | 74 | * A total environment handle that fails every file and command operation loudly with `cause` instead of | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/environment/environments.ts -6Lines changed: 0 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,8 +20,6 @@ export interface Interface { | 20 | 20 | * Fails immediately with NotFound when a machine or workspace does not exist, without waking lazy sandboxes. | 21 | 21 | */ | 22 | 22 | readonly lookup: (id: Environment.ID) => Effect.Effect<Environment.Interface, Machines.NotFound | Workspace.NotFound> | 23 | | - /** Whether an environment prefix is known (local, any workspace ID, or a registered paired machine). */ | 24 | | - readonly has: (id: Environment.ID) => Effect.Effect<boolean> | 25 | 23 | /** Whether an environment is currently reachable in memory without waking a cold/lazy sandbox. */ | 26 | 24 | readonly ready: (id: Environment.ID) => Effect.Effect<boolean> | 27 | 25 | } | @@ -58,10 +56,6 @@ const layer = Layer.effect( | 58 | 56 | } | 59 | 57 | return yield* machines.get(id) | 60 | 58 | }), | 61 | | - has: Effect.fn("Environments.has")(function* (id: Environment.ID) { | 62 | | - if (id === Environment.localID || Workspace.isID(id)) return true | 63 | | - return (yield* machines.list()).some((info) => info.id === id) | 64 | | - }), | 65 | 59 | ready: Effect.fn("Environments.ready")(function* (id: Environment.ID) { | 66 | 60 | if (id === Environment.localID) return true | 67 | 61 | if (Workspace.isID(id)) return yield* workspaces.connected(id) | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/environment/index.ts -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,7 +24,6 @@ export { | 24 | 24 | localID, | 25 | 25 | make, | 26 | 26 | node, | 27 | | - type Options, | 28 | 27 | Place, | 29 | 28 | Service, | 30 | 29 | unavailable, | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/environment/path.ts +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,12 +53,17 @@ export const parse = (text: string): Parsed | undefined => { | 53 | 53 | * Non-rooted `name:relative` text is refused for reserved environment prefixes (`local:`, `wrk_...:`) and | 54 | 54 | * known paired machines so typos never silently misroute to another environment's relative path. | 55 | 55 | */ | 56 | | -export const typed = Effect.fnUntraced(function* (text: string, has: (id: Environment.ID) => Effect.Effect<boolean>) { | | 56 | +export const typed = Effect.fnUntraced(function* (text: string, machines: Machines.Interface) { | 57 | 57 | const parsed = parse(text) | 58 | 58 | if (parsed) return parsed | 59 | 59 | const named = split(text) | 60 | 60 | if (!named || rooted(named)) return | 61 | | - if (yield* has(named.environment)) return yield* new NotAbsolute({ machine: named.environment }) | | 61 | + if ( | | 62 | + named.environment === Environment.localID || | | 63 | + Workspace.isID(named.environment) || | | 64 | + (yield* machines.list()).some((info) => info.id === named.environment) | | 65 | + ) | | 66 | + return yield* new NotAbsolute({ machine: named.environment }) | 62 | 67 | }) | 63 | 68 |
| 64 | 69 | /** An environment path's text: what `parse` reads back. */ | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/file-access.ts +16-14Lines changed: 16 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,11 +10,13 @@ import { Environment } from "./environment/index.js" | 10 | 10 | import { EnvironmentPath } from "./environment/path.js" | 11 | 11 | import { Flag } from "./flag.js" | 12 | 12 | import { Location } from "./location.js" | | 13 | +import { Machines } from "./machine/machines.js" | 13 | 14 | import { Permission } from "./permission.js" | 14 | 15 | import { Project } from "./project.js" | 15 | 16 | import { AbsolutePath } from "./schema.js" | 16 | 17 | import type { SessionErrors } from "./session/error.js" | 17 | 18 | import type { Tool } from "./tool.js" | | 19 | +import { Workspace } from "./workspace.js" | 18 | 20 |
| 19 | 21 | export const Kind = Schema.Literals(["file", "directory"]) | 20 | 22 | export type Kind = typeof Kind.Type | @@ -132,6 +134,7 @@ const layer = Layer.effect( | 132 | 134 | const permission = yield* Permission.Service | 133 | 135 | const flags = yield* Flag.Service | 134 | 136 | const environments = yield* Environments.Service | | 137 | + const machines = yield* Machines.Service | 135 | 138 |
| 136 | 139 | // The flag and the working directory are read once per operation by `locate` and `resolve`, which both call this. | 137 | 140 | const place = Effect.fnUntraced(function* ( | @@ -146,7 +149,7 @@ const layer = Layer.effect( | 146 | 149 | const crossEnvironment = enabled && environment.id === Environment.localID | 147 | 150 | const parsed = | 148 | 151 | !base && crossEnvironment | 149 | | - ? yield* EnvironmentPath.typed(text, environments.has).pipe( | | 152 | + ? yield* EnvironmentPath.typed(text, machines).pipe( | 150 | 153 | Effect.mapError((cause) => new Environment.Failed({ path: text, cause })), | 151 | 154 | ) | 152 | 155 | : undefined | @@ -161,24 +164,23 @@ const layer = Layer.effect( | 161 | 164 | } satisfies Environment.Place | 162 | 165 | } | 163 | 166 | // Another environment's paths follow its own path rules (falling back to POSIX when unknown). Only `~` and `~/` | 164 | | - // are expanded when the target environment exposes `home` (e.g. a paired machine's hello or a sandbox with home). | | 167 | + // are expanded: paired machines report their home in hello, while sandboxes do not expose a home directory. | 165 | 168 | // With the flag off no home is asked for, so a base on another machine refuses every `~`. | 166 | | - const written = parsed?.path ?? text | 167 | | - const isHome = written === "~" || written.startsWith("~/") | 168 | 169 | const target = crossEnvironment | 169 | | - ? yield* environments | 170 | | - .lookup(id) | 171 | | - .pipe( | 172 | | - Effect.catch((cause) => | 173 | | - isHome ? Effect.fail(new Environment.Failed({ path: text, cause })) : Effect.undefined, | 174 | | - ), | 175 | | - ) | | 170 | + ? yield* environments.lookup(id).pipe( | | 171 | + Effect.catchTags({ | | 172 | + "Machines.NotFound": () => Effect.undefined, | | 173 | + "Workspace.NotFound": () => Effect.undefined, | | 174 | + }), | | 175 | + ) | 176 | 176 | : undefined | 177 | 177 | const rules = target ? (target.path ?? path) : path.posix | | 178 | + const written = parsed?.path ?? text | | 179 | + const isHome = written === "~" || written.startsWith("~/") | 178 | 180 | const rooted = | 179 | | - crossEnvironment && isHome && target?.home | | 181 | + crossEnvironment && isHome && !Workspace.isID(id) | 180 | 182 | ? rules.join( | 181 | | - yield* target.home.pipe(Effect.mapError((cause) => new Environment.Failed({ path: text, cause }))), | | 183 | + yield* machines.home(id).pipe(Effect.mapError((cause) => new Environment.Failed({ path: text, cause }))), | 182 | 184 | written.slice(1), | 183 | 185 | ) | 184 | 186 | : written | @@ -348,5 +350,5 @@ const layer = Layer.effect( | 348 | 350 | export const node = makeLocationNode({ | 349 | 351 | service: Service, | 350 | 352 | layer, | 351 | | - deps: [Environment.node, Environments.node, Flag.node, Location.node, Permission.node], | | 353 | + deps: [Environment.node, Environments.node, Flag.node, Location.node, Machines.node, Permission.node], | 352 | 354 | }) |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/filesystem.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -119,7 +119,7 @@ const baseLayer = Layer.effect( | 119 | 119 | // configured directory as canonical; host placements keep symlink | 120 | 120 | // canonicalization. This skip is boot-only: resolve/read/list below still | 121 | 121 | // access the host filesystem per operation (tracked in #44568). | 122 | | - const root = environment.local | | 122 | + const root = environment.hostsServer | 123 | 123 | ? yield* fs.realPath(location.directory).pipe( | 124 | 124 | // Linux realpath succeeds on an unreadable directory, so probe readability explicitly. | 125 | 125 | Effect.tap((real) => fs.access(real, { readable: true })), | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/filesystem/search.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -231,7 +231,7 @@ const layer = (options?: Options) => | 231 | 231 | const environment = yield* Environment.Service | 232 | 232 | // fff indexes a directory on the server's own disk in-process, so only the server's own environment can use it. | 233 | 233 | // Ripgrep routes through the Location environment spawner. | 234 | | - if (!environment.local) return ripgrepLayer | | 234 | + if (!environment.hostsServer) return ripgrepLayer | 235 | 235 | if (options?.fff === false || (options?.fff === undefined && process.platform === "win32") || !Fff.available()) | 236 | 236 | return ripgrepLayer | 237 | 237 | // Non-VCS locations can contain many repositories, so avoid eagerly content-indexing the entire aggregate tree. | |
|