@@ -20,8 +20,7 @@ import {
2020} from "jsonc-parser"
2121import { Instance , type InstanceContext } from "../project/instance"
2222import * as LSPServer from "../lsp/server"
23- import { Installation } from "@/installation"
24- import { InstallationVersion } from "@/installation/version"
23+ import { InstallationLocal , InstallationVersion } from "@/installation/version"
2524import * as ConfigMarkdown from "./markdown"
2625import { existsSync } from "fs"
2726import { Bus } from "@/bus"
@@ -38,8 +37,8 @@ import { Context, Duration, Effect, Exit, Fiber, Layer, Option } from "effect"
3837import { EffectFlock } from "@opencode-ai/shared/util/effect-flock"
3938
4039import { isPathPluginSpec , parsePluginSpecifier , resolvePathPluginTarget } from "@/plugin/shared"
41- import { Npm } from "../npm"
4240import { InstanceRef } from "@/effect/instance-ref"
41+ import { Npm } from "@opencode-ai/shared/npm"
4342
4443const ModelId = z . string ( ) . meta ( { $ref : "https://models.dev/model-schema.json#/$defs/Model" } )
4544const PluginOptions = z . record ( z . string ( ) , z . unknown ( ) )
@@ -141,10 +140,6 @@ export type InstallInput = {
141140 waitTick ?: ( input : { dir : string ; attempt : number ; delay : number ; waited : number } ) => void | Promise < void >
142141}
143142
144- type Package = {
145- dependencies ?: Record < string , string >
146- }
147-
148143function rel ( item : string , patterns : string [ ] ) {
149144 const normalizedItem = item . replaceAll ( "\\" , "/" )
150145 for ( const pattern of patterns ) {
@@ -1059,7 +1054,6 @@ export interface Interface {
10591054 readonly get : ( ) => Effect . Effect < Info >
10601055 readonly getGlobal : ( ) => Effect . Effect < Info >
10611056 readonly getConsoleState : ( ) => Effect . Effect < ConsoleState >
1062- readonly installDependencies : ( dir : string , input ?: InstallInput ) => Effect . Effect < void , AppFileSystem . Error >
10631057 readonly update : ( config : Info ) => Effect . Effect < void >
10641058 readonly updateGlobal : ( config : Info ) => Effect . Effect < Info >
10651059 readonly invalidate : ( wait ?: boolean ) => Effect . Effect < void >
@@ -1146,18 +1140,14 @@ export const ConfigDirectoryTypoError = NamedError.create(
11461140 } ) ,
11471141)
11481142
1149- export const layer : Layer . Layer <
1150- Service ,
1151- never ,
1152- AppFileSystem . Service | Auth . Service | Account . Service | Env . Service | EffectFlock . Service
1153- > = Layer . effect (
1143+ export const layer = Layer . effect (
11541144 Service ,
11551145 Effect . gen ( function * ( ) {
11561146 const fs = yield * AppFileSystem . Service
11571147 const authSvc = yield * Auth . Service
11581148 const accountSvc = yield * Account . Service
11591149 const env = yield * Env . Service
1160- const flock = yield * EffectFlock . Service
1150+ const npmSvc = yield * Npm . Service
11611151
11621152 const readConfigFile = Effect . fnUntraced ( function * ( filepath : string ) {
11631153 return yield * fs . readFileString ( filepath ) . pipe (
@@ -1263,53 +1253,18 @@ export const layer: Layer.Layer<
12631253 return yield * cachedGlobal
12641254 } )
12651255
1266- const install = Effect . fn ( "Config.install" ) ( function * ( dir : string ) {
1267- const pkg = path . join ( dir , "package.json" )
1256+ const setupConfigDir = Effect . fnUntraced ( function * ( dir : string ) {
12681257 const gitignore = path . join ( dir , ".gitignore" )
1269- const plugin = path . join ( dir , "node_modules" , "@opencode-ai" , "plugin" , "package.json" )
1270- const target = Installation . isLocal ( ) ? "*" : InstallationVersion
1271- const json = yield * fs . readJson ( pkg ) . pipe (
1272- Effect . catch ( ( ) => Effect . succeed ( { } satisfies Package ) ) ,
1273- Effect . map ( ( x ) : Package => ( isRecord ( x ) ? ( x as Package ) : { } ) ) ,
1274- )
1275- const hasDep = json . dependencies ?. [ "@opencode-ai/plugin" ] === target
12761258 const hasIgnore = yield * fs . existsSafe ( gitignore )
1277- const hasPkg = yield * fs . existsSafe ( plugin )
1278-
1279- if ( ! hasDep ) {
1280- yield * fs . writeJson ( pkg , {
1281- ...json ,
1282- dependencies : {
1283- ...json . dependencies ,
1284- "@opencode-ai/plugin" : target ,
1285- } ,
1286- } )
1287- }
1288-
12891259 if ( ! hasIgnore ) {
12901260 yield * fs . writeFileString (
12911261 gitignore ,
12921262 [ "node_modules" , "package.json" , "package-lock.json" , "bun.lock" , ".gitignore" ] . join ( "\n" ) ,
12931263 )
12941264 }
1295-
1296- if ( hasDep && hasIgnore && hasPkg ) return
1297-
1298- yield * Effect . promise ( ( ) => Npm . install ( dir ) )
1299- } )
1300-
1301- const installDependencies = Effect . fn ( "Config.installDependencies" ) ( function * ( dir : string , _input ?: InstallInput ) {
1302- if (
1303- ! ( yield * fs . access ( dir , { writable : true } ) . pipe (
1304- Effect . as ( true ) ,
1305- Effect . orElseSucceed ( ( ) => false ) ,
1306- ) )
1307- )
1308- return
1309-
1310- const key = process . platform === "win32" ? "config-install:win32" : `config-install:${ AppFileSystem . resolve ( dir ) } `
1311-
1312- yield * flock . withLock ( install ( dir ) , key ) . pipe ( Effect . orDie )
1265+ yield * npmSvc . install ( dir , {
1266+ add : [ "@opencode-ai/plugin" + ( InstallationLocal ? "" : "@" + InstallationVersion ) ] ,
1267+ } )
13131268 } )
13141269
13151270 const loadInstanceState = Effect . fn ( "Config.loadInstanceState" ) ( function * ( ctx : InstanceContext ) {
@@ -1404,7 +1359,7 @@ export const layer: Layer.Layer<
14041359 }
14051360 }
14061361
1407- const dep = yield * installDependencies ( dir ) . pipe (
1362+ const dep = yield * setupConfigDir ( dir ) . pipe (
14081363 Effect . exit ,
14091364 Effect . tap ( ( exit ) =>
14101365 Exit . isFailure ( exit )
@@ -1611,7 +1566,6 @@ export const layer: Layer.Layer<
16111566 get,
16121567 getGlobal,
16131568 getConsoleState,
1614- installDependencies,
16151569 update,
16161570 updateGlobal,
16171571 invalidate,
@@ -1627,4 +1581,5 @@ export const defaultLayer = layer.pipe(
16271581 Layer . provide ( Env . defaultLayer ) ,
16281582 Layer . provide ( Auth . defaultLayer ) ,
16291583 Layer . provide ( Account . defaultLayer ) ,
1584+ Layer . provide ( Npm . defaultLayer ) ,
16301585)
0 commit comments