MCPcopy Create free account
hub / github.com/anomalyco/opencode / fromPromise

Function fromPromise

packages/core/src/plugin/promise.ts:20–93  ·  view source on GitHub ↗
(plugin: Plugin)

Source from the content-addressed store, hash-verified

18 * into one reload per domain.
19 */
20export function fromPromise(plugin: Plugin) {
21 return define({
22 id: plugin.id,
23 effect: (host) =>
24 Effect.gen(function* () {
25 const scope = yield* Scope.Scope
26 const context = yield* Effect.context<Scope.Scope>()
27
28 // Run a hook registration on the plugin scope and resolve once it is registered.
29 const register = (effect: Effect.Effect<HostRegistration, never, Scope.Scope>): Promise<Registration> =>
30 Effect.runPromiseWith(context)(Scope.provide(scope)(effect)).then((registration) => ({
31 dispose: () => Effect.runPromiseWith(context)(registration.dispose),
32 }))
33
34 const run = (effect: Effect.Effect<void>) => Effect.runPromiseWith(context)(effect)
35
36 const transform =
37 <Draft>(domain: {
38 transform: (
39 callback: (draft: Draft) => Effect.Effect<void> | void,
40 ) => Effect.Effect<HostRegistration, never, Scope.Scope>
41 }) =>
42 (callback: (draft: Draft) => Promise<void> | void) =>
43 register(domain.transform((draft) => Effect.promise(() => Promise.resolve(callback(draft)))))
44
45 const context2: PluginContext = {
46 options: host.options,
47 agent: {
48 transform: transform(host.agent),
49 reload: () => run(host.agent.reload()),
50 },
51 aisdk: {
52 sdk: (callback) =>
53 register(host.aisdk.sdk((event) => Effect.promise(() => Promise.resolve(callback(event))))),
54 language: (callback) =>
55 register(host.aisdk.language((event) => Effect.promise(() => Promise.resolve(callback(event))))),
56 },
57 catalog: {
58 transform: transform(host.catalog),
59 reload: () => run(host.catalog.reload()),
60 },
61 command: {
62 transform: transform(host.command),
63 reload: () => run(host.command.reload()),
64 },
65 integration: {
66 transform: transform(host.integration),
67 reload: () => run(host.integration.reload()),
68 connection: {
69 active: (id) => Effect.runPromiseWith(context)(host.integration.connection.active(id)),
70 resolve: (connection) => Effect.runPromiseWith(context)(host.integration.connection.resolve(connection)),
71 },
72 },
73 plugin: {
74 add: (input) => {
75 const child = fromPromise(input)
76 return run(host.plugin.add(child))
77 },

Callers

nothing calls this directly

Calls 9

defineFunction · 0.90
transformFunction · 0.85
contextMethod · 0.80
activeMethod · 0.80
runFunction · 0.70
registerFunction · 0.70
addMethod · 0.65
removeMethod · 0.65
callbackFunction · 0.50

Tested by

no test coverage detected