@immediately-run/sdk 0.2.3 → 0.2.4

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/dist/catalog.cjs CHANGED
@@ -33,9 +33,15 @@ const split = (name) => {
33
33
  if (i <= 0) throw new Error(`invalid catalog method name: ${name}`);
34
34
  return [name.slice(0, i), name.slice(i + 1)];
35
35
  };
36
- const invoke = (name, params = {}) => {
36
+ const invoke = async (name, params = {}) => {
37
37
  const [scheme, method] = split(name);
38
- return (0, import_sandboxUtils.protocolRequest)(scheme, method, [params]);
38
+ const res = await (0, import_sandboxUtils.protocolRequest)(scheme, method, [params]);
39
+ if (!res || res.ok !== true) {
40
+ const err = new Error(res?.message ?? `${name} failed`);
41
+ err.code = res?.code ?? "unknown";
42
+ throw err;
43
+ }
44
+ return res.data;
39
45
  };
40
46
  const bundlerTransport = {
41
47
  send: (msg) => (
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/catalog.ts"],"sourcesContent":["// The method catalog (UI_AS_APPS_SPEC §5.5) — the app's own grant-filtered RPC\n// surface, and a generic way to call it. The host advertises exactly the methods\n// this app may invoke (MCP-tool-shaped); `invoke()` calls one by its catalog name.\n// Handing the catalog to an embedded agent as its tool list confines the agent to\n// the app's authority (agent sandboxing falls out of the capability model, §5.9).\nimport { useEffect, useState } from 'react';\nimport { protocolRequest } from './sandboxUtils';\nimport type { StreamFrame } from './protocolStream';\nimport { consumeStream } from './protocolStream';\n\n/** One advertised method, as the host generated it from its gate table. */\nexport interface ApiMethod {\n /** Catalog name, `protocol-` stripped — e.g. `spaces:share`, `contribute:run`. */\n name: string;\n /** The capability this method requires (already held — it's in your catalog). */\n capability: string;\n /** True when the method STREAMS (use {@link invokeStream}) vs. single-reply. */\n stream?: boolean;\n}\n\n// `scheme:method` → ['scheme', 'method'] (the wire protocol is `protocol-scheme`).\nconst split = (name: string): [string, string] => {\n const i = name.indexOf(':');\n if (i <= 0) throw new Error(`invalid catalog method name: ${name}`);\n return [name.slice(0, i), name.slice(i + 1)];\n};\n\n/**\n * Call a catalog method by name — `invoke('spaces:share', { spaceId, login, role })`.\n * A thin generic over the host protocol: the host validates params and gates the\n * call (an un-granted method → `forbidden`, even if you name it directly). For a\n * STREAMING method (`ApiMethod.stream`), use {@link invokeStream}.\n */\nexport const invoke = <T = unknown>(name: string, params: Record<string, unknown> = {}): Promise<T> => {\n const [scheme, method] = split(name);\n return protocolRequest(scheme, method, [params]) as Promise<T>;\n};\n\nconst bundlerTransport = {\n send: (msg: { type: string; method: string; params: unknown[]; msgId: number; stream: true }) =>\n // @ts-ignore - injected by the sandbox runtime\n module.evaluation.module.bundler.messageBus.sendMessage(msg.type, msg),\n subscribe: (type: string, handler: (msg: { msgId?: number; stream?: StreamFrame }) => void) => {\n // @ts-ignore - injected by the sandbox runtime\n const d = module.evaluation.module.bundler.messageBus.onMessage((m: { type?: string }) => {\n if (m && m.type === type) handler(m as { msgId?: number; stream?: StreamFrame });\n });\n return () => d.dispose();\n },\n};\n\n/** Call a STREAMING catalog method by name, yielding its events. */\nexport function invokeStream<T = unknown, R = unknown>(\n name: string,\n params: Record<string, unknown> = {},\n): AsyncGenerator<T, R, void> {\n const [scheme, method] = split(name);\n return consumeStream<T, R>(bundlerTransport, `protocol-${scheme}`, method, [params]);\n}\n\ninterface CatalogService {\n getCatalog(): ApiMethod[];\n onChange(listener: (catalog: ApiMethod[]) => void): { dispose(): void };\n}\n\n// `module.evaluation.module.bundler.catalog` — injected by the sandbox runtime.\nconst catalogService = (): CatalogService => {\n // @ts-ignore - injected by the sandbox runtime\n return module.evaluation.module.bundler.catalog;\n};\n\n/** The methods this app may call (grant-filtered, §5.5). Poll for a one-off read;\n * use {@link onCatalogChange} / {@link useCatalog} to react. */\nexport const getCatalog = (): ApiMethod[] => catalogService().getCatalog();\n\n/** Subscribe to catalog changes (e.g. a grant added/revoked). Invoked immediately\n * with the current catalog, then on every change. Returns an unsubscribe fn. */\nexport const onCatalogChange = (listener: (catalog: ApiMethod[]) => void): (() => void) => {\n const disposable = catalogService().onChange(listener);\n return () => disposable.dispose();\n};\n\n/** React hook returning this app's method catalog, re-rendering on change. Hand\n * it to an embedded agent as its tool list to confine the agent to the app's\n * authority (§5.9). */\nexport const useCatalog = (): ApiMethod[] => {\n const [catalog, setCatalog] = useState<ApiMethod[]>(getCatalog);\n useEffect(() => onCatalogChange(setCatalog), []);\n return catalog;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,mBAAoC;AACpC,0BAAgC;AAEhC,4BAA8B;AAa9B,MAAM,QAAQ,CAAC,SAAmC;AAChD,QAAM,IAAI,KAAK,QAAQ,GAAG;AAC1B,MAAI,KAAK,EAAG,OAAM,IAAI,MAAM,gCAAgC,IAAI,EAAE;AAClE,SAAO,CAAC,KAAK,MAAM,GAAG,CAAC,GAAG,KAAK,MAAM,IAAI,CAAC,CAAC;AAC7C;AAQO,MAAM,SAAS,CAAc,MAAc,SAAkC,CAAC,MAAkB;AACrG,QAAM,CAAC,QAAQ,MAAM,IAAI,MAAM,IAAI;AACnC,aAAO,qCAAgB,QAAQ,QAAQ,CAAC,MAAM,CAAC;AACjD;AAEA,MAAM,mBAAmB;AAAA,EACvB,MAAM,CAAC;AAAA;AAAA,IAEL,OAAO,WAAW,OAAO,QAAQ,WAAW,YAAY,IAAI,MAAM,GAAG;AAAA;AAAA,EACvE,WAAW,CAAC,MAAc,YAAqE;AAE7F,UAAM,IAAI,OAAO,WAAW,OAAO,QAAQ,WAAW,UAAU,CAAC,MAAyB;AACxF,UAAI,KAAK,EAAE,SAAS,KAAM,SAAQ,CAA6C;AAAA,IACjF,CAAC;AACD,WAAO,MAAM,EAAE,QAAQ;AAAA,EACzB;AACF;AAGO,SAAS,aACd,MACA,SAAkC,CAAC,GACP;AAC5B,QAAM,CAAC,QAAQ,MAAM,IAAI,MAAM,IAAI;AACnC,aAAO,qCAAoB,kBAAkB,YAAY,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC;AACrF;AAQA,MAAM,iBAAiB,MAAsB;AAE3C,SAAO,OAAO,WAAW,OAAO,QAAQ;AAC1C;AAIO,MAAM,aAAa,MAAmB,eAAe,EAAE,WAAW;AAIlE,MAAM,kBAAkB,CAAC,aAA2D;AACzF,QAAM,aAAa,eAAe,EAAE,SAAS,QAAQ;AACrD,SAAO,MAAM,WAAW,QAAQ;AAClC;AAKO,MAAM,aAAa,MAAmB;AAC3C,QAAM,CAAC,SAAS,UAAU,QAAI,uBAAsB,UAAU;AAC9D,8BAAU,MAAM,gBAAgB,UAAU,GAAG,CAAC,CAAC;AAC/C,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/catalog.ts"],"sourcesContent":["// The method catalog (UI_AS_APPS_SPEC §5.5) — the app's own grant-filtered RPC\n// surface, and a generic way to call it. The host advertises exactly the methods\n// this app may invoke (MCP-tool-shaped); `invoke()` calls one by its catalog name.\n// Handing the catalog to an embedded agent as its tool list confines the agent to\n// the app's authority (agent sandboxing falls out of the capability model, §5.9).\nimport { useEffect, useState } from 'react';\nimport { protocolRequest } from './sandboxUtils';\nimport type { StreamFrame } from './protocolStream';\nimport { consumeStream } from './protocolStream';\n\n/** One advertised method, as the host generated it from its gate table. */\nexport interface ApiMethod {\n /** Catalog name, `protocol-` stripped — e.g. `spaces:share`, `contribute:run`. */\n name: string;\n /** The capability this method requires (already held — it's in your catalog). */\n capability: string;\n /** True when the method STREAMS (use {@link invokeStream}) vs. single-reply. */\n stream?: boolean;\n}\n\n// `scheme:method` → ['scheme', 'method'] (the wire protocol is `protocol-scheme`).\nconst split = (name: string): [string, string] => {\n const i = name.indexOf(':');\n if (i <= 0) throw new Error(`invalid catalog method name: ${name}`);\n return [name.slice(0, i), name.slice(i + 1)];\n};\n\n/**\n * Call a catalog method by name — `invoke('spaces:share', { spaceId, login, role })`.\n * A thin generic over the host protocol: the host validates params and gates the\n * call (an un-granted method → `forbidden`, even if you name it directly). For a\n * STREAMING method (`ApiMethod.stream`), use {@link invokeStream}.\n */\nexport const invoke = async <T = unknown>(\n name: string,\n params: Record<string, unknown> = {},\n): Promise<T> => {\n const [scheme, method] = split(name);\n // The host replies with an `{ ok, data } | { ok:false, code }` envelope; unwrap\n // it and THROW on refusal (a `.code` like `forbidden` for an off-catalog call)\n // so callers — and any agent driving `invoke` — see the gate's verdict.\n const res = (await protocolRequest(scheme, method, [params])) as\n | { ok: true; data: unknown }\n | { ok: false; code?: string; message?: string }\n | undefined;\n if (!res || res.ok !== true) {\n const err = new Error(res?.message ?? `${name} failed`) as Error & { code?: string };\n err.code = res?.code ?? 'unknown';\n throw err;\n }\n return res.data as T;\n};\n\nconst bundlerTransport = {\n send: (msg: { type: string; method: string; params: unknown[]; msgId: number; stream: true }) =>\n // @ts-ignore - injected by the sandbox runtime\n module.evaluation.module.bundler.messageBus.sendMessage(msg.type, msg),\n subscribe: (type: string, handler: (msg: { msgId?: number; stream?: StreamFrame }) => void) => {\n // @ts-ignore - injected by the sandbox runtime\n const d = module.evaluation.module.bundler.messageBus.onMessage((m: { type?: string }) => {\n if (m && m.type === type) handler(m as { msgId?: number; stream?: StreamFrame });\n });\n return () => d.dispose();\n },\n};\n\n/** Call a STREAMING catalog method by name, yielding its events. */\nexport function invokeStream<T = unknown, R = unknown>(\n name: string,\n params: Record<string, unknown> = {},\n): AsyncGenerator<T, R, void> {\n const [scheme, method] = split(name);\n return consumeStream<T, R>(bundlerTransport, `protocol-${scheme}`, method, [params]);\n}\n\ninterface CatalogService {\n getCatalog(): ApiMethod[];\n onChange(listener: (catalog: ApiMethod[]) => void): { dispose(): void };\n}\n\n// `module.evaluation.module.bundler.catalog` — injected by the sandbox runtime.\nconst catalogService = (): CatalogService => {\n // @ts-ignore - injected by the sandbox runtime\n return module.evaluation.module.bundler.catalog;\n};\n\n/** The methods this app may call (grant-filtered, §5.5). Poll for a one-off read;\n * use {@link onCatalogChange} / {@link useCatalog} to react. */\nexport const getCatalog = (): ApiMethod[] => catalogService().getCatalog();\n\n/** Subscribe to catalog changes (e.g. a grant added/revoked). Invoked immediately\n * with the current catalog, then on every change. Returns an unsubscribe fn. */\nexport const onCatalogChange = (listener: (catalog: ApiMethod[]) => void): (() => void) => {\n const disposable = catalogService().onChange(listener);\n return () => disposable.dispose();\n};\n\n/** React hook returning this app's method catalog, re-rendering on change. Hand\n * it to an embedded agent as its tool list to confine the agent to the app's\n * authority (§5.9). */\nexport const useCatalog = (): ApiMethod[] => {\n const [catalog, setCatalog] = useState<ApiMethod[]>(getCatalog);\n useEffect(() => onCatalogChange(setCatalog), []);\n return catalog;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,mBAAoC;AACpC,0BAAgC;AAEhC,4BAA8B;AAa9B,MAAM,QAAQ,CAAC,SAAmC;AAChD,QAAM,IAAI,KAAK,QAAQ,GAAG;AAC1B,MAAI,KAAK,EAAG,OAAM,IAAI,MAAM,gCAAgC,IAAI,EAAE;AAClE,SAAO,CAAC,KAAK,MAAM,GAAG,CAAC,GAAG,KAAK,MAAM,IAAI,CAAC,CAAC;AAC7C;AAQO,MAAM,SAAS,OACpB,MACA,SAAkC,CAAC,MACpB;AACf,QAAM,CAAC,QAAQ,MAAM,IAAI,MAAM,IAAI;AAInC,QAAM,MAAO,UAAM,qCAAgB,QAAQ,QAAQ,CAAC,MAAM,CAAC;AAI3D,MAAI,CAAC,OAAO,IAAI,OAAO,MAAM;AAC3B,UAAM,MAAM,IAAI,MAAM,KAAK,WAAW,GAAG,IAAI,SAAS;AACtD,QAAI,OAAO,KAAK,QAAQ;AACxB,UAAM;AAAA,EACR;AACA,SAAO,IAAI;AACb;AAEA,MAAM,mBAAmB;AAAA,EACvB,MAAM,CAAC;AAAA;AAAA,IAEL,OAAO,WAAW,OAAO,QAAQ,WAAW,YAAY,IAAI,MAAM,GAAG;AAAA;AAAA,EACvE,WAAW,CAAC,MAAc,YAAqE;AAE7F,UAAM,IAAI,OAAO,WAAW,OAAO,QAAQ,WAAW,UAAU,CAAC,MAAyB;AACxF,UAAI,KAAK,EAAE,SAAS,KAAM,SAAQ,CAA6C;AAAA,IACjF,CAAC;AACD,WAAO,MAAM,EAAE,QAAQ;AAAA,EACzB;AACF;AAGO,SAAS,aACd,MACA,SAAkC,CAAC,GACP;AAC5B,QAAM,CAAC,QAAQ,MAAM,IAAI,MAAM,IAAI;AACnC,aAAO,qCAAoB,kBAAkB,YAAY,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC;AACrF;AAQA,MAAM,iBAAiB,MAAsB;AAE3C,SAAO,OAAO,WAAW,OAAO,QAAQ;AAC1C;AAIO,MAAM,aAAa,MAAmB,eAAe,EAAE,WAAW;AAIlE,MAAM,kBAAkB,CAAC,aAA2D;AACzF,QAAM,aAAa,eAAe,EAAE,SAAS,QAAQ;AACrD,SAAO,MAAM,WAAW,QAAQ;AAClC;AAKO,MAAM,aAAa,MAAmB;AAC3C,QAAM,CAAC,SAAS,UAAU,QAAI,uBAAsB,UAAU;AAC9D,8BAAU,MAAM,gBAAgB,UAAU,GAAG,CAAC,CAAC;AAC/C,SAAO;AACT;","names":[]}
package/dist/catalog.js CHANGED
@@ -6,9 +6,15 @@ const split = (name) => {
6
6
  if (i <= 0) throw new Error(`invalid catalog method name: ${name}`);
7
7
  return [name.slice(0, i), name.slice(i + 1)];
8
8
  };
9
- const invoke = (name, params = {}) => {
9
+ const invoke = async (name, params = {}) => {
10
10
  const [scheme, method] = split(name);
11
- return protocolRequest(scheme, method, [params]);
11
+ const res = await protocolRequest(scheme, method, [params]);
12
+ if (!res || res.ok !== true) {
13
+ const err = new Error(res?.message ?? `${name} failed`);
14
+ err.code = res?.code ?? "unknown";
15
+ throw err;
16
+ }
17
+ return res.data;
12
18
  };
13
19
  const bundlerTransport = {
14
20
  send: (msg) => (
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/catalog.ts"],"sourcesContent":["// The method catalog (UI_AS_APPS_SPEC §5.5) — the app's own grant-filtered RPC\n// surface, and a generic way to call it. The host advertises exactly the methods\n// this app may invoke (MCP-tool-shaped); `invoke()` calls one by its catalog name.\n// Handing the catalog to an embedded agent as its tool list confines the agent to\n// the app's authority (agent sandboxing falls out of the capability model, §5.9).\nimport { useEffect, useState } from 'react';\nimport { protocolRequest } from './sandboxUtils';\nimport type { StreamFrame } from './protocolStream';\nimport { consumeStream } from './protocolStream';\n\n/** One advertised method, as the host generated it from its gate table. */\nexport interface ApiMethod {\n /** Catalog name, `protocol-` stripped — e.g. `spaces:share`, `contribute:run`. */\n name: string;\n /** The capability this method requires (already held — it's in your catalog). */\n capability: string;\n /** True when the method STREAMS (use {@link invokeStream}) vs. single-reply. */\n stream?: boolean;\n}\n\n// `scheme:method` → ['scheme', 'method'] (the wire protocol is `protocol-scheme`).\nconst split = (name: string): [string, string] => {\n const i = name.indexOf(':');\n if (i <= 0) throw new Error(`invalid catalog method name: ${name}`);\n return [name.slice(0, i), name.slice(i + 1)];\n};\n\n/**\n * Call a catalog method by name — `invoke('spaces:share', { spaceId, login, role })`.\n * A thin generic over the host protocol: the host validates params and gates the\n * call (an un-granted method → `forbidden`, even if you name it directly). For a\n * STREAMING method (`ApiMethod.stream`), use {@link invokeStream}.\n */\nexport const invoke = <T = unknown>(name: string, params: Record<string, unknown> = {}): Promise<T> => {\n const [scheme, method] = split(name);\n return protocolRequest(scheme, method, [params]) as Promise<T>;\n};\n\nconst bundlerTransport = {\n send: (msg: { type: string; method: string; params: unknown[]; msgId: number; stream: true }) =>\n // @ts-ignore - injected by the sandbox runtime\n module.evaluation.module.bundler.messageBus.sendMessage(msg.type, msg),\n subscribe: (type: string, handler: (msg: { msgId?: number; stream?: StreamFrame }) => void) => {\n // @ts-ignore - injected by the sandbox runtime\n const d = module.evaluation.module.bundler.messageBus.onMessage((m: { type?: string }) => {\n if (m && m.type === type) handler(m as { msgId?: number; stream?: StreamFrame });\n });\n return () => d.dispose();\n },\n};\n\n/** Call a STREAMING catalog method by name, yielding its events. */\nexport function invokeStream<T = unknown, R = unknown>(\n name: string,\n params: Record<string, unknown> = {},\n): AsyncGenerator<T, R, void> {\n const [scheme, method] = split(name);\n return consumeStream<T, R>(bundlerTransport, `protocol-${scheme}`, method, [params]);\n}\n\ninterface CatalogService {\n getCatalog(): ApiMethod[];\n onChange(listener: (catalog: ApiMethod[]) => void): { dispose(): void };\n}\n\n// `module.evaluation.module.bundler.catalog` — injected by the sandbox runtime.\nconst catalogService = (): CatalogService => {\n // @ts-ignore - injected by the sandbox runtime\n return module.evaluation.module.bundler.catalog;\n};\n\n/** The methods this app may call (grant-filtered, §5.5). Poll for a one-off read;\n * use {@link onCatalogChange} / {@link useCatalog} to react. */\nexport const getCatalog = (): ApiMethod[] => catalogService().getCatalog();\n\n/** Subscribe to catalog changes (e.g. a grant added/revoked). Invoked immediately\n * with the current catalog, then on every change. Returns an unsubscribe fn. */\nexport const onCatalogChange = (listener: (catalog: ApiMethod[]) => void): (() => void) => {\n const disposable = catalogService().onChange(listener);\n return () => disposable.dispose();\n};\n\n/** React hook returning this app's method catalog, re-rendering on change. Hand\n * it to an embedded agent as its tool list to confine the agent to the app's\n * authority (§5.9). */\nexport const useCatalog = (): ApiMethod[] => {\n const [catalog, setCatalog] = useState<ApiMethod[]>(getCatalog);\n useEffect(() => onCatalogChange(setCatalog), []);\n return catalog;\n};\n"],"mappings":"AAKA,SAAS,WAAW,gBAAgB;AACpC,SAAS,uBAAuB;AAEhC,SAAS,qBAAqB;AAa9B,MAAM,QAAQ,CAAC,SAAmC;AAChD,QAAM,IAAI,KAAK,QAAQ,GAAG;AAC1B,MAAI,KAAK,EAAG,OAAM,IAAI,MAAM,gCAAgC,IAAI,EAAE;AAClE,SAAO,CAAC,KAAK,MAAM,GAAG,CAAC,GAAG,KAAK,MAAM,IAAI,CAAC,CAAC;AAC7C;AAQO,MAAM,SAAS,CAAc,MAAc,SAAkC,CAAC,MAAkB;AACrG,QAAM,CAAC,QAAQ,MAAM,IAAI,MAAM,IAAI;AACnC,SAAO,gBAAgB,QAAQ,QAAQ,CAAC,MAAM,CAAC;AACjD;AAEA,MAAM,mBAAmB;AAAA,EACvB,MAAM,CAAC;AAAA;AAAA,IAEL,OAAO,WAAW,OAAO,QAAQ,WAAW,YAAY,IAAI,MAAM,GAAG;AAAA;AAAA,EACvE,WAAW,CAAC,MAAc,YAAqE;AAE7F,UAAM,IAAI,OAAO,WAAW,OAAO,QAAQ,WAAW,UAAU,CAAC,MAAyB;AACxF,UAAI,KAAK,EAAE,SAAS,KAAM,SAAQ,CAA6C;AAAA,IACjF,CAAC;AACD,WAAO,MAAM,EAAE,QAAQ;AAAA,EACzB;AACF;AAGO,SAAS,aACd,MACA,SAAkC,CAAC,GACP;AAC5B,QAAM,CAAC,QAAQ,MAAM,IAAI,MAAM,IAAI;AACnC,SAAO,cAAoB,kBAAkB,YAAY,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC;AACrF;AAQA,MAAM,iBAAiB,MAAsB;AAE3C,SAAO,OAAO,WAAW,OAAO,QAAQ;AAC1C;AAIO,MAAM,aAAa,MAAmB,eAAe,EAAE,WAAW;AAIlE,MAAM,kBAAkB,CAAC,aAA2D;AACzF,QAAM,aAAa,eAAe,EAAE,SAAS,QAAQ;AACrD,SAAO,MAAM,WAAW,QAAQ;AAClC;AAKO,MAAM,aAAa,MAAmB;AAC3C,QAAM,CAAC,SAAS,UAAU,IAAI,SAAsB,UAAU;AAC9D,YAAU,MAAM,gBAAgB,UAAU,GAAG,CAAC,CAAC;AAC/C,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/catalog.ts"],"sourcesContent":["// The method catalog (UI_AS_APPS_SPEC §5.5) — the app's own grant-filtered RPC\n// surface, and a generic way to call it. The host advertises exactly the methods\n// this app may invoke (MCP-tool-shaped); `invoke()` calls one by its catalog name.\n// Handing the catalog to an embedded agent as its tool list confines the agent to\n// the app's authority (agent sandboxing falls out of the capability model, §5.9).\nimport { useEffect, useState } from 'react';\nimport { protocolRequest } from './sandboxUtils';\nimport type { StreamFrame } from './protocolStream';\nimport { consumeStream } from './protocolStream';\n\n/** One advertised method, as the host generated it from its gate table. */\nexport interface ApiMethod {\n /** Catalog name, `protocol-` stripped — e.g. `spaces:share`, `contribute:run`. */\n name: string;\n /** The capability this method requires (already held — it's in your catalog). */\n capability: string;\n /** True when the method STREAMS (use {@link invokeStream}) vs. single-reply. */\n stream?: boolean;\n}\n\n// `scheme:method` → ['scheme', 'method'] (the wire protocol is `protocol-scheme`).\nconst split = (name: string): [string, string] => {\n const i = name.indexOf(':');\n if (i <= 0) throw new Error(`invalid catalog method name: ${name}`);\n return [name.slice(0, i), name.slice(i + 1)];\n};\n\n/**\n * Call a catalog method by name — `invoke('spaces:share', { spaceId, login, role })`.\n * A thin generic over the host protocol: the host validates params and gates the\n * call (an un-granted method → `forbidden`, even if you name it directly). For a\n * STREAMING method (`ApiMethod.stream`), use {@link invokeStream}.\n */\nexport const invoke = async <T = unknown>(\n name: string,\n params: Record<string, unknown> = {},\n): Promise<T> => {\n const [scheme, method] = split(name);\n // The host replies with an `{ ok, data } | { ok:false, code }` envelope; unwrap\n // it and THROW on refusal (a `.code` like `forbidden` for an off-catalog call)\n // so callers — and any agent driving `invoke` — see the gate's verdict.\n const res = (await protocolRequest(scheme, method, [params])) as\n | { ok: true; data: unknown }\n | { ok: false; code?: string; message?: string }\n | undefined;\n if (!res || res.ok !== true) {\n const err = new Error(res?.message ?? `${name} failed`) as Error & { code?: string };\n err.code = res?.code ?? 'unknown';\n throw err;\n }\n return res.data as T;\n};\n\nconst bundlerTransport = {\n send: (msg: { type: string; method: string; params: unknown[]; msgId: number; stream: true }) =>\n // @ts-ignore - injected by the sandbox runtime\n module.evaluation.module.bundler.messageBus.sendMessage(msg.type, msg),\n subscribe: (type: string, handler: (msg: { msgId?: number; stream?: StreamFrame }) => void) => {\n // @ts-ignore - injected by the sandbox runtime\n const d = module.evaluation.module.bundler.messageBus.onMessage((m: { type?: string }) => {\n if (m && m.type === type) handler(m as { msgId?: number; stream?: StreamFrame });\n });\n return () => d.dispose();\n },\n};\n\n/** Call a STREAMING catalog method by name, yielding its events. */\nexport function invokeStream<T = unknown, R = unknown>(\n name: string,\n params: Record<string, unknown> = {},\n): AsyncGenerator<T, R, void> {\n const [scheme, method] = split(name);\n return consumeStream<T, R>(bundlerTransport, `protocol-${scheme}`, method, [params]);\n}\n\ninterface CatalogService {\n getCatalog(): ApiMethod[];\n onChange(listener: (catalog: ApiMethod[]) => void): { dispose(): void };\n}\n\n// `module.evaluation.module.bundler.catalog` — injected by the sandbox runtime.\nconst catalogService = (): CatalogService => {\n // @ts-ignore - injected by the sandbox runtime\n return module.evaluation.module.bundler.catalog;\n};\n\n/** The methods this app may call (grant-filtered, §5.5). Poll for a one-off read;\n * use {@link onCatalogChange} / {@link useCatalog} to react. */\nexport const getCatalog = (): ApiMethod[] => catalogService().getCatalog();\n\n/** Subscribe to catalog changes (e.g. a grant added/revoked). Invoked immediately\n * with the current catalog, then on every change. Returns an unsubscribe fn. */\nexport const onCatalogChange = (listener: (catalog: ApiMethod[]) => void): (() => void) => {\n const disposable = catalogService().onChange(listener);\n return () => disposable.dispose();\n};\n\n/** React hook returning this app's method catalog, re-rendering on change. Hand\n * it to an embedded agent as its tool list to confine the agent to the app's\n * authority (§5.9). */\nexport const useCatalog = (): ApiMethod[] => {\n const [catalog, setCatalog] = useState<ApiMethod[]>(getCatalog);\n useEffect(() => onCatalogChange(setCatalog), []);\n return catalog;\n};\n"],"mappings":"AAKA,SAAS,WAAW,gBAAgB;AACpC,SAAS,uBAAuB;AAEhC,SAAS,qBAAqB;AAa9B,MAAM,QAAQ,CAAC,SAAmC;AAChD,QAAM,IAAI,KAAK,QAAQ,GAAG;AAC1B,MAAI,KAAK,EAAG,OAAM,IAAI,MAAM,gCAAgC,IAAI,EAAE;AAClE,SAAO,CAAC,KAAK,MAAM,GAAG,CAAC,GAAG,KAAK,MAAM,IAAI,CAAC,CAAC;AAC7C;AAQO,MAAM,SAAS,OACpB,MACA,SAAkC,CAAC,MACpB;AACf,QAAM,CAAC,QAAQ,MAAM,IAAI,MAAM,IAAI;AAInC,QAAM,MAAO,MAAM,gBAAgB,QAAQ,QAAQ,CAAC,MAAM,CAAC;AAI3D,MAAI,CAAC,OAAO,IAAI,OAAO,MAAM;AAC3B,UAAM,MAAM,IAAI,MAAM,KAAK,WAAW,GAAG,IAAI,SAAS;AACtD,QAAI,OAAO,KAAK,QAAQ;AACxB,UAAM;AAAA,EACR;AACA,SAAO,IAAI;AACb;AAEA,MAAM,mBAAmB;AAAA,EACvB,MAAM,CAAC;AAAA;AAAA,IAEL,OAAO,WAAW,OAAO,QAAQ,WAAW,YAAY,IAAI,MAAM,GAAG;AAAA;AAAA,EACvE,WAAW,CAAC,MAAc,YAAqE;AAE7F,UAAM,IAAI,OAAO,WAAW,OAAO,QAAQ,WAAW,UAAU,CAAC,MAAyB;AACxF,UAAI,KAAK,EAAE,SAAS,KAAM,SAAQ,CAA6C;AAAA,IACjF,CAAC;AACD,WAAO,MAAM,EAAE,QAAQ;AAAA,EACzB;AACF;AAGO,SAAS,aACd,MACA,SAAkC,CAAC,GACP;AAC5B,QAAM,CAAC,QAAQ,MAAM,IAAI,MAAM,IAAI;AACnC,SAAO,cAAoB,kBAAkB,YAAY,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC;AACrF;AAQA,MAAM,iBAAiB,MAAsB;AAE3C,SAAO,OAAO,WAAW,OAAO,QAAQ;AAC1C;AAIO,MAAM,aAAa,MAAmB,eAAe,EAAE,WAAW;AAIlE,MAAM,kBAAkB,CAAC,aAA2D;AACzF,QAAM,aAAa,eAAe,EAAE,SAAS,QAAQ;AACrD,SAAO,MAAM,WAAW,QAAQ;AAClC;AAKO,MAAM,aAAa,MAAmB;AAC3C,QAAM,CAAC,SAAS,UAAU,IAAI,SAAsB,UAAU;AAC9D,YAAU,MAAM,gBAAgB,UAAU,GAAG,CAAC,CAAC;AAC/C,SAAO;AACT;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@immediately-run/sdk",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Runtime SDK for code executing inside an immediately.run sandbox.",
5
5
  "license": "MIT",
6
6
  "repository": "github:immediately-run/immediately-run-sdk",