diff options
| author | Frank <[email protected]> | 2025-08-08 13:22:54 -0400 |
|---|---|---|
| committer | Frank <[email protected]> | 2025-08-08 13:24:32 -0400 |
| commit | 183e0911b76025a1f2a82e979d9834fec2131d0e (patch) | |
| tree | 9987c1753bd64d1ce1d174ab397f1a8c681f642c /cloud/core/src/actor.ts | |
| parent | c7bb19ad0712469063eab35589aa5d3602b0c5b1 (diff) | |
| download | opencode-183e0911b76025a1f2a82e979d9834fec2131d0e.tar.gz opencode-183e0911b76025a1f2a82e979d9834fec2131d0e.zip | |
wip: gateway
Diffstat (limited to 'cloud/core/src/actor.ts')
| -rw-r--r-- | cloud/core/src/actor.ts | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/cloud/core/src/actor.ts b/cloud/core/src/actor.ts new file mode 100644 index 000000000..beb292bb8 --- /dev/null +++ b/cloud/core/src/actor.ts @@ -0,0 +1,75 @@ +import { Context } from "./context" +import { Log } from "./util/log" + +export namespace Actor { + interface Account { + type: "account" + properties: { + accountID: string + email: string + } + } + + interface Public { + type: "public" + properties: {} + } + + interface User { + type: "user" + properties: { + userID: string + workspaceID: string + email: string + } + } + + interface System { + type: "system" + properties: { + workspaceID: string + } + } + + export type Info = Account | Public | User | System + + const ctx = Context.create<Info>() + export const use = ctx.use + + const log = Log.create().tag("namespace", "actor") + + export function provide<R, T extends Info["type"]>( + type: T, + properties: Extract<Info, { type: T }>["properties"], + cb: () => R, + ) { + return ctx.provide( + { + type, + properties, + } as any, + () => { + return Log.provide({ ...properties }, () => { + log.info("provided") + return cb() + }) + }, + ) + } + + export function assert<T extends Info["type"]>(type: T) { + const actor = use() + if (actor.type !== type) { + throw new Error(`Expected actor type ${type}, got ${actor.type}`) + } + return actor as Extract<Info, { type: T }> + } + + export function workspace() { + const actor = use() + if ("workspaceID" in actor.properties) { + return actor.properties.workspaceID + } + throw new Error(`actor of type "${actor.type}" is not associated with a workspace`) + } +} |
