@opencode-ai/sdk 0.0.0-dev-202608212222 → 0.0.0-dev-17880
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -0
- package/dist/index.d.ts +30 -10
- package/dist/index.js +29 -16
- package/dist/logging.d.ts +15 -0
- package/dist/logging.js +46 -0
- package/dist/opencode.d.ts +63 -0
- package/dist/opencode.js +36 -0
- package/dist/tool.d.ts +3 -0
- package/dist/tool.js +2 -0
- package/dist/workerd.d.ts +29 -0
- package/dist/workerd.js +9 -0
- package/package.json +32 -44
- package/dist/client.d.ts +0 -7
- package/dist/client.js +0 -53
- package/dist/error-interceptor.d.ts +0 -15
- package/dist/error-interceptor.js +0 -43
- package/dist/gen/client/client.gen.d.ts +0 -2
- package/dist/gen/client/client.gen.js +0 -165
- package/dist/gen/client/index.d.ts +0 -7
- package/dist/gen/client/index.js +0 -5
- package/dist/gen/client/types.gen.d.ts +0 -127
- package/dist/gen/client/types.gen.js +0 -2
- package/dist/gen/client/utils.gen.d.ts +0 -38
- package/dist/gen/client/utils.gen.js +0 -226
- package/dist/gen/client.gen.d.ts +0 -12
- package/dist/gen/client.gen.js +0 -5
- package/dist/gen/core/auth.gen.d.ts +0 -18
- package/dist/gen/core/auth.gen.js +0 -14
- package/dist/gen/core/bodySerializer.gen.d.ts +0 -17
- package/dist/gen/core/bodySerializer.gen.js +0 -57
- package/dist/gen/core/params.gen.d.ts +0 -33
- package/dist/gen/core/params.gen.js +0 -89
- package/dist/gen/core/pathSerializer.gen.d.ts +0 -33
- package/dist/gen/core/pathSerializer.gen.js +0 -106
- package/dist/gen/core/queryKeySerializer.gen.d.ts +0 -18
- package/dist/gen/core/queryKeySerializer.gen.js +0 -93
- package/dist/gen/core/serverSentEvents.gen.d.ts +0 -59
- package/dist/gen/core/serverSentEvents.gen.js +0 -117
- package/dist/gen/core/types.gen.d.ts +0 -78
- package/dist/gen/core/types.gen.js +0 -2
- package/dist/gen/core/utils.gen.d.ts +0 -14
- package/dist/gen/core/utils.gen.js +0 -69
- package/dist/gen/sdk.gen.d.ts +0 -403
- package/dist/gen/sdk.gen.js +0 -881
- package/dist/gen/types.gen.d.ts +0 -3383
- package/dist/gen/types.gen.js +0 -2
- package/dist/process.d.ts +0 -3
- package/dist/process.js +0 -33
- package/dist/server.d.ts +0 -23
- package/dist/server.js +0 -105
- package/dist/v2/client.d.ts +0 -9
- package/dist/v2/client.js +0 -80
- package/dist/v2/data.d.ts +0 -9
- package/dist/v2/data.js +0 -22
- package/dist/v2/gen/client/client.gen.d.ts +0 -2
- package/dist/v2/gen/client/client.gen.js +0 -232
- package/dist/v2/gen/client/index.d.ts +0 -8
- package/dist/v2/gen/client/index.js +0 -6
- package/dist/v2/gen/client/types.gen.d.ts +0 -117
- package/dist/v2/gen/client/types.gen.js +0 -2
- package/dist/v2/gen/client/utils.gen.d.ts +0 -33
- package/dist/v2/gen/client/utils.gen.js +0 -226
- package/dist/v2/gen/client.gen.d.ts +0 -12
- package/dist/v2/gen/client.gen.js +0 -3
- package/dist/v2/gen/core/auth.gen.d.ts +0 -18
- package/dist/v2/gen/core/auth.gen.js +0 -14
- package/dist/v2/gen/core/bodySerializer.gen.d.ts +0 -25
- package/dist/v2/gen/core/bodySerializer.gen.js +0 -57
- package/dist/v2/gen/core/params.gen.d.ts +0 -43
- package/dist/v2/gen/core/params.gen.js +0 -102
- package/dist/v2/gen/core/pathSerializer.gen.d.ts +0 -33
- package/dist/v2/gen/core/pathSerializer.gen.js +0 -106
- package/dist/v2/gen/core/queryKeySerializer.gen.d.ts +0 -18
- package/dist/v2/gen/core/queryKeySerializer.gen.js +0 -93
- package/dist/v2/gen/core/serverSentEvents.gen.d.ts +0 -71
- package/dist/v2/gen/core/serverSentEvents.gen.js +0 -133
- package/dist/v2/gen/core/types.gen.d.ts +0 -78
- package/dist/v2/gen/core/types.gen.js +0 -2
- package/dist/v2/gen/core/utils.gen.d.ts +0 -19
- package/dist/v2/gen/core/utils.gen.js +0 -87
- package/dist/v2/gen/sdk.gen.d.ts +0 -2302
- package/dist/v2/gen/sdk.gen.js +0 -4456
- package/dist/v2/gen/types.gen.d.ts +0 -11653
- package/dist/v2/gen/types.gen.js +0 -2
- package/dist/v2/index.d.ts +0 -11
- package/dist/v2/index.js +0 -17
- package/dist/v2/server.d.ts +0 -23
- package/dist/v2/server.js +0 -105
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# @opencode-ai/sdk
|
|
2
|
+
|
|
3
|
+
Effect-native scoped OpenCode host for in-process applications.
|
|
4
|
+
|
|
5
|
+
The SDK executes Server's assembled HTTP router in memory. It opens no listener and performs no network I/O, while preserving the same routing, middleware, handlers, codecs, and errors as the network client.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { OpenCode } from "@opencode-ai/sdk"
|
|
9
|
+
|
|
10
|
+
const opencode = yield * OpenCode.create()
|
|
11
|
+
const session = yield * opencode.sessions.get({ sessionID })
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
It also exports `Tool` for plugins that add tools with `ctx.tool.transform(...)`. Embedded plugins run through the ordinary discovery flow and register tools into each Location's `ToolRegistry` through the normal `Tools.Service.register(...)` path. Closing the owning Effect Scope releases router resources, location services, fibers, and scoped tool registrations.
|
|
15
|
+
|
|
16
|
+
Embedded hosts are silent by default. Set `log` to receive structured log entries at the selected minimum level:
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
const opencode =
|
|
20
|
+
yield *
|
|
21
|
+
OpenCode.create({
|
|
22
|
+
log: {
|
|
23
|
+
level: "warn",
|
|
24
|
+
emit: (entry) => console.error(entry.message, entry.attributes, entry.cause),
|
|
25
|
+
},
|
|
26
|
+
})
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`sessions.events({ sessionID, after })` replays durable events after the optional aggregate sequence, then emits newly committed durable events. `sessions.interrupt(...)` targets execution owned by this host, and `sessions.message(...)` retrieves one projected Session message.
|
|
30
|
+
|
|
31
|
+
The same constructor is available as a service Layer:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
const program = Effect.gen(function* () {
|
|
35
|
+
const opencode = yield* OpenCode.Service
|
|
36
|
+
return yield* opencode.sessions.get({ sessionID })
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
yield * program.pipe(Effect.provide(OpenCode.layer()))
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`OpenCode.layer(options)` adapts the scoped `OpenCode.create(options)` convenience constructor for dependency injection.
|
|
43
|
+
|
|
44
|
+
Workspace providers are host infrastructure configured when the SDK is constructed. Workspace lifecycle operations remain on the typed facade:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const opencode = yield * OpenCode.create({ workspaceProviders: { modal: modalWorkspaceProvider } })
|
|
48
|
+
const workspace = yield * opencode.workspace.create({ provider: "modal" })
|
|
49
|
+
yield * opencode.workspace.destroy({ workspaceID: workspace.id })
|
|
50
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
1
|
+
export * as OpenCode from "./opencode";
|
|
2
|
+
export * as Tool from "./tool";
|
|
3
|
+
export { ClientError } from "@opencode-ai/client/effect";
|
|
4
|
+
export type { OpenCodeEvent } from "@opencode-ai/client/effect";
|
|
5
|
+
export { Agent } from "@opencode-ai/schema/agent";
|
|
6
|
+
export { Command } from "@opencode-ai/schema/command";
|
|
7
|
+
export { Config } from "@opencode-ai/schema/config";
|
|
8
|
+
export { Credential } from "@opencode-ai/schema/credential";
|
|
9
|
+
export { Event } from "@opencode-ai/schema/event";
|
|
10
|
+
export { FileSystem } from "@opencode-ai/schema/filesystem";
|
|
11
|
+
export { Integration } from "@opencode-ai/schema/integration";
|
|
12
|
+
export { Location } from "@opencode-ai/schema/location";
|
|
13
|
+
export { Model } from "@opencode-ai/schema/model";
|
|
14
|
+
export { Permission } from "@opencode-ai/schema/permission";
|
|
15
|
+
export { PermissionSaved } from "@opencode-ai/schema/permission-saved";
|
|
16
|
+
export { Project } from "@opencode-ai/schema/project";
|
|
17
|
+
export { Worktree } from "@opencode-ai/schema/worktree";
|
|
18
|
+
export { Prompt } from "@opencode-ai/schema/prompt";
|
|
19
|
+
export { PromptInput } from "@opencode-ai/schema/prompt-input";
|
|
20
|
+
export { Provider } from "@opencode-ai/schema/provider";
|
|
21
|
+
export { Pty } from "@opencode-ai/schema/pty";
|
|
22
|
+
export { Question } from "@opencode-ai/schema/question";
|
|
23
|
+
export { Reference } from "@opencode-ai/schema/reference";
|
|
24
|
+
export { WebSearch } from "@opencode-ai/schema/websearch";
|
|
25
|
+
export { AbsolutePath, RelativePath } from "@opencode-ai/schema/schema";
|
|
26
|
+
export { Session } from "@opencode-ai/schema/session";
|
|
27
|
+
export { SessionInbox } from "@opencode-ai/schema/session-inbox";
|
|
28
|
+
export { SessionMessage } from "@opencode-ai/schema/session-message";
|
|
29
|
+
export { Skill } from "@opencode-ai/schema/skill";
|
|
30
|
+
export { Workspace } from "@opencode-ai/schema/workspace";
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
1
|
+
export * as OpenCode from "./opencode";
|
|
2
|
+
export * as Tool from "./tool";
|
|
3
|
+
export { ClientError } from "@opencode-ai/client/effect";
|
|
4
|
+
export { Agent } from "@opencode-ai/schema/agent";
|
|
5
|
+
export { Command } from "@opencode-ai/schema/command";
|
|
6
|
+
export { Config } from "@opencode-ai/schema/config";
|
|
7
|
+
export { Credential } from "@opencode-ai/schema/credential";
|
|
8
|
+
export { Event } from "@opencode-ai/schema/event";
|
|
9
|
+
export { FileSystem } from "@opencode-ai/schema/filesystem";
|
|
10
|
+
export { Integration } from "@opencode-ai/schema/integration";
|
|
11
|
+
export { Location } from "@opencode-ai/schema/location";
|
|
12
|
+
export { Model } from "@opencode-ai/schema/model";
|
|
13
|
+
export { Permission } from "@opencode-ai/schema/permission";
|
|
14
|
+
export { PermissionSaved } from "@opencode-ai/schema/permission-saved";
|
|
15
|
+
export { Project } from "@opencode-ai/schema/project";
|
|
16
|
+
export { Worktree } from "@opencode-ai/schema/worktree";
|
|
17
|
+
export { Prompt } from "@opencode-ai/schema/prompt";
|
|
18
|
+
export { PromptInput } from "@opencode-ai/schema/prompt-input";
|
|
19
|
+
export { Provider } from "@opencode-ai/schema/provider";
|
|
20
|
+
export { Pty } from "@opencode-ai/schema/pty";
|
|
21
|
+
export { Question } from "@opencode-ai/schema/question";
|
|
22
|
+
export { Reference } from "@opencode-ai/schema/reference";
|
|
23
|
+
export { WebSearch } from "@opencode-ai/schema/websearch";
|
|
24
|
+
export { AbsolutePath, RelativePath } from "@opencode-ai/schema/schema";
|
|
25
|
+
export { Session } from "@opencode-ai/schema/session";
|
|
26
|
+
export { SessionInbox } from "@opencode-ai/schema/session-inbox";
|
|
27
|
+
export { SessionMessage } from "@opencode-ai/schema/session-message";
|
|
28
|
+
export { Skill } from "@opencode-ai/schema/skill";
|
|
29
|
+
export { Workspace } from "@opencode-ai/schema/workspace";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Context, Layer } from "effect";
|
|
2
|
+
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";
|
|
3
|
+
export type LogEntry = {
|
|
4
|
+
readonly level: LogLevel;
|
|
5
|
+
readonly message: string;
|
|
6
|
+
readonly attributes?: Readonly<Record<string, unknown>>;
|
|
7
|
+
readonly cause?: unknown;
|
|
8
|
+
};
|
|
9
|
+
export type LogWriter = (entry: LogEntry) => void;
|
|
10
|
+
export type LogOptions = {
|
|
11
|
+
readonly level?: LogLevel;
|
|
12
|
+
readonly emit: LogWriter;
|
|
13
|
+
};
|
|
14
|
+
export declare function layer(log?: LogOptions): Layer.Layer<never, never, never>;
|
|
15
|
+
export declare function context(source: Context.Context<never>): Context.Context<never>;
|
package/dist/logging.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Context, Formatter, Layer, Logger, References } from "effect";
|
|
2
|
+
const levels = {
|
|
3
|
+
trace: "Trace",
|
|
4
|
+
debug: "Debug",
|
|
5
|
+
info: "Info",
|
|
6
|
+
warn: "Warn",
|
|
7
|
+
error: "Error",
|
|
8
|
+
fatal: "Fatal"
|
|
9
|
+
}, levelNames = new Map([
|
|
10
|
+
[levels.trace, "trace"],
|
|
11
|
+
[levels.debug, "debug"],
|
|
12
|
+
[levels.info, "info"],
|
|
13
|
+
[levels.warn, "warn"],
|
|
14
|
+
[levels.error, "error"],
|
|
15
|
+
[levels.fatal, "fatal"]
|
|
16
|
+
]);
|
|
17
|
+
function normalizeLevel(level) {
|
|
18
|
+
return levelNames.get(level);
|
|
19
|
+
}
|
|
20
|
+
export function layer(log) {
|
|
21
|
+
const logger = Logger.make((options) => {
|
|
22
|
+
if (!log)
|
|
23
|
+
return;
|
|
24
|
+
const level = normalizeLevel(options.logLevel);
|
|
25
|
+
if (!level)
|
|
26
|
+
return;
|
|
27
|
+
const entry = Logger.formatStructured.log(options), values = Array.isArray(entry.message) ? entry.message : [entry.message], [message, ...data] = values, details = data.length === 1 && !Array.isArray(data[0]) ? data[0] : void 0, { cause: detailCause, ...detailAttributes } = details ?? {}, attributes = {
|
|
28
|
+
...entry.annotations,
|
|
29
|
+
...detailAttributes,
|
|
30
|
+
...Object.keys(entry.spans).length > 0 ? { spans: entry.spans } : {},
|
|
31
|
+
...!details && data.length > 0 ? { data: data.length === 1 ? data[0] : data } : {}
|
|
32
|
+
};
|
|
33
|
+
try {
|
|
34
|
+
log.emit({
|
|
35
|
+
level,
|
|
36
|
+
message: typeof message === "string" ? message : Formatter.format(message),
|
|
37
|
+
...Object.keys(attributes).length > 0 ? { attributes } : {},
|
|
38
|
+
...entry.cause === void 0 && detailCause === void 0 ? {} : { cause: entry.cause ?? detailCause }
|
|
39
|
+
});
|
|
40
|
+
} catch {}
|
|
41
|
+
});
|
|
42
|
+
return Layer.merge(Logger.layer([logger], { mergeWithExisting: !1 }), Layer.succeed(References.MinimumLogLevel, levels[log?.level ?? "info"]));
|
|
43
|
+
}
|
|
44
|
+
export function context(source) {
|
|
45
|
+
return Context.make(Logger.CurrentLoggers, Context.get(source, Logger.CurrentLoggers)).pipe(Context.add(References.MinimumLogLevel, Context.get(source, References.MinimumLogLevel)));
|
|
46
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type OpenCodeClient } from "@opencode-ai/client/effect";
|
|
2
|
+
import type { Database } from "@opencode-ai/core/database/database";
|
|
3
|
+
import type { ModelsDev } from "@opencode-ai/core/models-dev";
|
|
4
|
+
import { SdkPlugins } from "@opencode-ai/core/plugin/sdk";
|
|
5
|
+
import { Workspace } from "@opencode-ai/core/workspace";
|
|
6
|
+
import { WorkspaceDriver } from "@opencode-ai/core/workspace/driver";
|
|
7
|
+
import type { LayerNode } from "@opencode-ai/util/effect/layer-node";
|
|
8
|
+
import { Config, Context, Effect, Layer, Scope } from "effect";
|
|
9
|
+
export type { LogEntry, LogLevel, LogOptions, LogWriter } from "./logging";
|
|
10
|
+
import type { LogOptions } from "./logging";
|
|
11
|
+
export interface CreateOptions {
|
|
12
|
+
readonly app?: {
|
|
13
|
+
readonly name?: string;
|
|
14
|
+
readonly version?: string;
|
|
15
|
+
readonly channel?: string;
|
|
16
|
+
};
|
|
17
|
+
readonly hostname?: string;
|
|
18
|
+
readonly port?: number;
|
|
19
|
+
readonly password?: string;
|
|
20
|
+
readonly simulation?: boolean;
|
|
21
|
+
readonly database?: Database.Options;
|
|
22
|
+
readonly events?: {
|
|
23
|
+
readonly persist?: boolean;
|
|
24
|
+
};
|
|
25
|
+
readonly models?: ModelsDev.Options;
|
|
26
|
+
readonly config?: {
|
|
27
|
+
readonly directory?: string;
|
|
28
|
+
readonly project?: boolean;
|
|
29
|
+
readonly file?: string;
|
|
30
|
+
readonly content?: string;
|
|
31
|
+
};
|
|
32
|
+
readonly windows?: {
|
|
33
|
+
readonly gitbash?: string;
|
|
34
|
+
};
|
|
35
|
+
readonly fs?: {
|
|
36
|
+
readonly filewatcher?: boolean;
|
|
37
|
+
readonly fff?: boolean;
|
|
38
|
+
};
|
|
39
|
+
readonly log?: LogOptions;
|
|
40
|
+
readonly workspaceProviders?: Readonly<Record<string, WorkspaceDriver.Interface>>;
|
|
41
|
+
}
|
|
42
|
+
/** Host hooks for embedding opencode on a non-default runtime profile (e.g. workerd). */
|
|
43
|
+
export interface EmbedOptions {
|
|
44
|
+
readonly overrides?: LayerNode.Replacements;
|
|
45
|
+
}
|
|
46
|
+
export type Interface = Omit<OpenCodeClient, "plugin" | "workspace"> & {
|
|
47
|
+
readonly sessions: OpenCodeClient["session"];
|
|
48
|
+
readonly events: OpenCodeClient["event"];
|
|
49
|
+
readonly workspace: {
|
|
50
|
+
readonly create: (options: {
|
|
51
|
+
readonly provider: string;
|
|
52
|
+
}) => ReturnType<Workspace.Interface["create"]>;
|
|
53
|
+
readonly destroy: (options: {
|
|
54
|
+
readonly workspaceID: Workspace.ID;
|
|
55
|
+
}) => ReturnType<Workspace.Interface["destroy"]>;
|
|
56
|
+
};
|
|
57
|
+
readonly plugin: SdkPlugins.Interface["register"] & OpenCodeClient["plugin"];
|
|
58
|
+
};
|
|
59
|
+
export declare const create: (options?: CreateOptions, embed?: EmbedOptions) => Effect.Effect<Interface, Config.ConfigError | Error, Scope.Scope>;
|
|
60
|
+
declare const Service_base: Context.ServiceClass<Service, "@opencode-ai/sdk/OpenCode", Interface>;
|
|
61
|
+
export declare class Service extends Service_base {
|
|
62
|
+
}
|
|
63
|
+
export declare const layer: (options?: CreateOptions) => Layer.Layer<Service, Config.ConfigError | Error>;
|
package/dist/opencode.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { OpenCode } from "@opencode-ai/client/effect";
|
|
2
|
+
import { SdkPlugins } from "@opencode-ai/core/plugin/sdk";
|
|
3
|
+
import { SessionRestart } from "@opencode-ai/core/session/execution/restart";
|
|
4
|
+
import { Workspace } from "@opencode-ai/core/workspace";
|
|
5
|
+
import { WorkspaceDriver } from "@opencode-ai/core/workspace/driver";
|
|
6
|
+
import { createEmbeddedRoutes } from "@opencode-ai/server/routes";
|
|
7
|
+
import { Config, Context, Effect, Layer, ManagedRuntime, Scope } from "effect";
|
|
8
|
+
import { FetchHttpClient, HttpEffect, HttpRouter, HttpServer, HttpServerRequest } from "effect/unstable/http";
|
|
9
|
+
import * as Logging from "./logging";
|
|
10
|
+
export const create = Effect.fn("OpenCode.create")(function* (options = {}, embed = {}) {
|
|
11
|
+
const { log, workspaceProviders, ...server } = options, context = yield* (yield* Effect.acquireRelease(Effect.sync(() => ManagedRuntime.make(createEmbeddedRoutes({
|
|
12
|
+
...server,
|
|
13
|
+
app: { ...server.app, name: server.app?.name ?? "sdk" },
|
|
14
|
+
database: { path: ":memory:", ...server.database }
|
|
15
|
+
}, workspaceProviders ? [...embed.overrides ?? [], [WorkspaceDriver.node, WorkspaceDriver.registryNode(workspaceProviders)]] : embed.overrides).pipe(Layer.provide(HttpServer.layerServices), Layer.provideMerge(Logging.layer(log))))), (runtime) => runtime.disposeEffect)).contextEffect;
|
|
16
|
+
yield* Effect.forkDetach(Context.get(context, SessionRestart.Service).resumeSuspendedSessions);
|
|
17
|
+
const plugins = Context.get(context, SdkPlugins.Service), workspace = Context.get(context, Workspace.Service), router = Context.get(context, HttpRouter.HttpRouter), handler = HttpEffect.toWebHandlerWith(Logging.context(context))(router.asHttpEffect()), fetch = Object.assign((input, init) => handler(new Request(input, init)), {
|
|
18
|
+
preconnect: () => {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
}), client = yield* OpenCode.make({ baseUrl: "http://opencode.local" }).pipe(Effect.provide(FetchHttpClient.layer.pipe(Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetch)), Layer.fresh)));
|
|
22
|
+
return {
|
|
23
|
+
...client,
|
|
24
|
+
sessions: client.session,
|
|
25
|
+
events: client.event,
|
|
26
|
+
workspace: {
|
|
27
|
+
create: ({ provider }) => workspace.create(provider),
|
|
28
|
+
destroy: ({ workspaceID }) => workspace.destroy(workspaceID)
|
|
29
|
+
},
|
|
30
|
+
plugin: Object.assign(plugins.register, client.plugin)
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export class Service extends Context.Service()("@opencode-ai/sdk/OpenCode") {
|
|
35
|
+
}
|
|
36
|
+
export const layer = (options = {}) => Layer.effect(Service, create(options));
|
package/dist/tool.d.ts
ADDED
package/dist/tool.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export * as OpenCodeWorkerd from "./workerd";
|
|
2
|
+
import type { DurableObjectStorage } from "@opencode-ai/core/database/sqlite.workerd";
|
|
3
|
+
import { Config, Effect, Layer, Scope } from "effect";
|
|
4
|
+
import * as OpenCode from "./opencode";
|
|
5
|
+
export interface CreateOptions extends Pick<OpenCode.CreateOptions, "log" | "workspaceProviders"> {
|
|
6
|
+
readonly storage: DurableObjectStorage;
|
|
7
|
+
readonly app?: OpenCode.CreateOptions["app"];
|
|
8
|
+
readonly password?: string;
|
|
9
|
+
readonly config?: {
|
|
10
|
+
readonly content?: string;
|
|
11
|
+
};
|
|
12
|
+
readonly models?: OpenCode.CreateOptions["models"];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Boots the embedded opencode SDK on the workerd runtime profile: the full
|
|
16
|
+
* application graph inside a Cloudflare Durable Object, with the database on
|
|
17
|
+
* the injected `DurableObjectStorage` SQLite and every intentionally-local
|
|
18
|
+
* service replaced or disabled (see `ServerWorkerd.replacements`).
|
|
19
|
+
*
|
|
20
|
+
* Suspended Sessions resume on boot (as on every runtime) because a Durable
|
|
21
|
+
* Object can be evicted mid-turn with no teardown; the write-ahead execution
|
|
22
|
+
* claim marks the turn and the boot-time sweep replays it.
|
|
23
|
+
*
|
|
24
|
+
* Returns the same typed `OpenCode.Interface` as `OpenCode.create` — typed
|
|
25
|
+
* session operations plus the live `events.subscribe()` stream — served over
|
|
26
|
+
* an in-process fetch transport, so no request leaves the isolate.
|
|
27
|
+
*/
|
|
28
|
+
export declare const create: (options: CreateOptions) => Effect.Effect<OpenCode.Interface, Config.ConfigError | Error, Scope.Scope>;
|
|
29
|
+
export declare const layer: (options: CreateOptions) => Layer.Layer<OpenCode.Service, Config.ConfigError | Error>;
|
package/dist/workerd.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * as OpenCodeWorkerd from "./workerd";
|
|
2
|
+
import { ServerWorkerd } from "@opencode-ai/server/workerd";
|
|
3
|
+
import { Config, Effect, Layer, Scope } from "effect";
|
|
4
|
+
import * as OpenCode from "./opencode";
|
|
5
|
+
export const create = ({
|
|
6
|
+
log,
|
|
7
|
+
workspaceProviders,
|
|
8
|
+
...options
|
|
9
|
+
}) => OpenCode.create({ ...ServerWorkerd.serverOptions(options), log, workspaceProviders }, { overrides: ServerWorkerd.replacements(options) }), layer = (options) => Layer.effect(OpenCode.Service, create(options));
|
package/package.json
CHANGED
|
@@ -1,60 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
+
"version": "0.0.0-dev-17880",
|
|
3
4
|
"name": "@opencode-ai/sdk",
|
|
4
|
-
"version": "0.0.0-dev-202608212222",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/anomalyco/opencode.git",
|
|
10
|
+
"directory": "packages/sdk"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
11
14
|
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
12
18
|
"exports": {
|
|
13
19
|
".": {
|
|
14
20
|
"import": "./dist/index.js",
|
|
15
21
|
"types": "./dist/index.d.ts"
|
|
16
22
|
},
|
|
17
|
-
"./
|
|
18
|
-
"import": "./dist/
|
|
19
|
-
"types": "./dist/
|
|
20
|
-
},
|
|
21
|
-
"./server": {
|
|
22
|
-
"import": "./dist/server.js",
|
|
23
|
-
"types": "./dist/server.d.ts"
|
|
24
|
-
},
|
|
25
|
-
"./v2": {
|
|
26
|
-
"import": "./dist/v2/index.js",
|
|
27
|
-
"types": "./dist/v2/index.d.ts"
|
|
28
|
-
},
|
|
29
|
-
"./v2/client": {
|
|
30
|
-
"import": "./dist/v2/client.js",
|
|
31
|
-
"types": "./dist/v2/client.d.ts"
|
|
32
|
-
},
|
|
33
|
-
"./v2/gen/client": {
|
|
34
|
-
"import": "./dist/v2/gen/client/index.js",
|
|
35
|
-
"types": "./dist/v2/gen/client/index.d.ts"
|
|
36
|
-
},
|
|
37
|
-
"./v2/server": {
|
|
38
|
-
"import": "./dist/v2/server.js",
|
|
39
|
-
"types": "./dist/v2/server.d.ts"
|
|
40
|
-
},
|
|
41
|
-
"./v2/types": {
|
|
42
|
-
"import": "./dist/v2/gen/types.gen.js",
|
|
43
|
-
"types": "./dist/v2/gen/types.gen.d.ts"
|
|
23
|
+
"./workerd": {
|
|
24
|
+
"import": "./dist/workerd.js",
|
|
25
|
+
"types": "./dist/workerd.d.ts"
|
|
44
26
|
}
|
|
45
27
|
},
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"@hey-api/openapi-ts": "0.90.10",
|
|
51
|
-
"@tsconfig/node22": "22.0.2",
|
|
52
|
-
"@types/cross-spawn": "6.0.6",
|
|
53
|
-
"@types/node": "24.12.2",
|
|
54
|
-
"@typescript/native-preview": "7.0.0-dev.20251207.1",
|
|
55
|
-
"typescript": "5.8.2"
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "bun run script/build.ts",
|
|
30
|
+
"test": "bun test --timeout 5000",
|
|
31
|
+
"typecheck": "tsgo -b"
|
|
56
32
|
},
|
|
57
33
|
"dependencies": {
|
|
58
|
-
"
|
|
34
|
+
"@opencode-ai/client": "0.0.0-dev-17880",
|
|
35
|
+
"@opencode-ai/core": "0.0.0-dev-17880",
|
|
36
|
+
"@opencode-ai/schema": "0.0.0-dev-17880",
|
|
37
|
+
"@opencode-ai/server": "0.0.0-dev-17880",
|
|
38
|
+
"@opencode-ai/util": "0.0.0-dev-17880",
|
|
39
|
+
"effect": "4.0.0-rc.110"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@opencode-ai/httpapi-codegen": "0.0.0-dev-17880",
|
|
43
|
+
"@opencode-ai/protocol": "0.0.0-dev-17880",
|
|
44
|
+
"@tsconfig/bun": "1.0.9",
|
|
45
|
+
"@types/bun": "1.3.13",
|
|
46
|
+
"@typescript/native-preview": "7.0.0-dev.20251207.1"
|
|
59
47
|
}
|
|
60
|
-
}
|
|
48
|
+
}
|
package/dist/client.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export * from "./gen/types.gen.js";
|
|
2
|
-
import { type Config } from "./gen/client/types.gen.js";
|
|
3
|
-
import { OpencodeClient } from "./gen/sdk.gen.js";
|
|
4
|
-
export { type Config as OpencodeClientConfig, OpencodeClient };
|
|
5
|
-
export declare function createOpencodeClient(config?: Config & {
|
|
6
|
-
directory?: string;
|
|
7
|
-
}): OpencodeClient;
|
package/dist/client.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
export * from "./gen/types.gen.js";
|
|
2
|
-
import { createClient } from "./gen/client/client.gen.js";
|
|
3
|
-
import { OpencodeClient } from "./gen/sdk.gen.js";
|
|
4
|
-
import { wrapClientError } from "./error-interceptor.js";
|
|
5
|
-
export { OpencodeClient };
|
|
6
|
-
function pick(value, fallback) {
|
|
7
|
-
if (!value)
|
|
8
|
-
return;
|
|
9
|
-
if (!fallback)
|
|
10
|
-
return value;
|
|
11
|
-
if (value === fallback)
|
|
12
|
-
return fallback;
|
|
13
|
-
if (value === encodeURIComponent(fallback))
|
|
14
|
-
return fallback;
|
|
15
|
-
return value;
|
|
16
|
-
}
|
|
17
|
-
function rewrite(request, directory) {
|
|
18
|
-
if (request.method !== "GET" && request.method !== "HEAD")
|
|
19
|
-
return request;
|
|
20
|
-
const value = pick(request.headers.get("x-opencode-directory"), directory);
|
|
21
|
-
if (!value)
|
|
22
|
-
return request;
|
|
23
|
-
const url = new URL(request.url);
|
|
24
|
-
if (!url.searchParams.has("directory")) {
|
|
25
|
-
url.searchParams.set("directory", value);
|
|
26
|
-
}
|
|
27
|
-
const next = new Request(url, request);
|
|
28
|
-
next.headers.delete("x-opencode-directory");
|
|
29
|
-
return next;
|
|
30
|
-
}
|
|
31
|
-
export function createOpencodeClient(config) {
|
|
32
|
-
if (!config?.fetch) {
|
|
33
|
-
const customFetch = (req) => {
|
|
34
|
-
// @ts-ignore
|
|
35
|
-
req.timeout = false;
|
|
36
|
-
return fetch(req);
|
|
37
|
-
};
|
|
38
|
-
config = {
|
|
39
|
-
...config,
|
|
40
|
-
fetch: customFetch,
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
if (config?.directory) {
|
|
44
|
-
config.headers = {
|
|
45
|
-
...config.headers,
|
|
46
|
-
"x-opencode-directory": encodeURIComponent(config.directory),
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
const client = createClient(config);
|
|
50
|
-
client.interceptors.request.use((request) => rewrite(request, config?.directory));
|
|
51
|
-
client.interceptors.error.use(wrapClientError);
|
|
52
|
-
return new OpencodeClient({ client });
|
|
53
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Wrap whatever the generated client decoded from a non-2xx error body
|
|
3
|
-
* into a real `Error` so downstream formatters (TUI, plugins) get a
|
|
4
|
-
* useful `.message` instead of `[object Object]` or blank. The original
|
|
5
|
-
* parsed body and status live under `.cause` for callers that need
|
|
6
|
-
* structured fields.
|
|
7
|
-
*
|
|
8
|
-
* Only fires when the caller used `{ throwOnError: true }`. Callers that
|
|
9
|
-
* read `result.error` directly (the result-tuple path) get the parsed
|
|
10
|
-
* body unchanged so existing field-level reads (`.error.name`,
|
|
11
|
-
* `JSON.stringify(error)`, etc.) are byte-for-byte identical to before.
|
|
12
|
-
*/
|
|
13
|
-
export declare function wrapClientError(error: unknown, response: Response | undefined, request: Request | undefined, opts: {
|
|
14
|
-
throwOnError?: boolean;
|
|
15
|
-
} | undefined): unknown;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Wrap whatever the generated client decoded from a non-2xx error body
|
|
3
|
-
* into a real `Error` so downstream formatters (TUI, plugins) get a
|
|
4
|
-
* useful `.message` instead of `[object Object]` or blank. The original
|
|
5
|
-
* parsed body and status live under `.cause` for callers that need
|
|
6
|
-
* structured fields.
|
|
7
|
-
*
|
|
8
|
-
* Only fires when the caller used `{ throwOnError: true }`. Callers that
|
|
9
|
-
* read `result.error` directly (the result-tuple path) get the parsed
|
|
10
|
-
* body unchanged so existing field-level reads (`.error.name`,
|
|
11
|
-
* `JSON.stringify(error)`, etc.) are byte-for-byte identical to before.
|
|
12
|
-
*/
|
|
13
|
-
export function wrapClientError(error, response, request, opts) {
|
|
14
|
-
if (!opts?.throwOnError)
|
|
15
|
-
return error;
|
|
16
|
-
if (error instanceof Error)
|
|
17
|
-
return error;
|
|
18
|
-
// NamedError-shaped responses (the common case for opencode 4xx) come
|
|
19
|
-
// through as POJOs — extract a useful message first, then wrap.
|
|
20
|
-
if (typeof error === "object" && error !== null && Object.keys(error).length > 0) {
|
|
21
|
-
const obj = error;
|
|
22
|
-
const message = (typeof obj.data?.message === "string" && obj.data.message) ||
|
|
23
|
-
(typeof obj.message === "string" && obj.message) ||
|
|
24
|
-
(typeof obj.name === "string" && obj.name) ||
|
|
25
|
-
describe(request, response);
|
|
26
|
-
return new Error(message, { cause: { body: error, status: response?.status } });
|
|
27
|
-
}
|
|
28
|
-
if (typeof error === "string" && error.length > 0) {
|
|
29
|
-
return new Error(error, { cause: { body: error, status: response?.status } });
|
|
30
|
-
}
|
|
31
|
-
// Empty body / network failure / undefined / null / empty object.
|
|
32
|
-
const reason = response ? "(empty response body)" : "network error (no response)";
|
|
33
|
-
return new Error(`opencode server ${describe(request, response)}: ${reason}`, {
|
|
34
|
-
cause: { body: error, status: response?.status },
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
function describe(request, response) {
|
|
38
|
-
const method = request?.method ?? "?";
|
|
39
|
-
const url = request?.url ?? "?";
|
|
40
|
-
const status = response?.status;
|
|
41
|
-
const statusText = response?.statusText;
|
|
42
|
-
return `${method} ${url}${status ? " → " + status : ""}${statusText ? " " + statusText : ""}`;
|
|
43
|
-
}
|