@segmentstream/ontology 0.181.0 → 0.182.0

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.
@@ -260,7 +260,7 @@ export function resultText(result) {
260
260
  * fixed values replace whatever the caller sent for them; the call goes through the toolset's
261
261
  * connection, audited as `<toolset>.<tool>`.
262
262
  */
263
- export function runToolsetToolOf(connections) {
263
+ export function runToolsetToolOf(connections, reportCost) {
264
264
  return (toolset, tool, call) => Effect.gen(function* () {
265
265
  const principal = yield* CurrentPrincipal;
266
266
  const caller = yield* currentCaller;
@@ -278,10 +278,44 @@ export function runToolsetToolOf(connections) {
278
278
  });
279
279
  }
280
280
  const requestId = yield* RequestId;
281
- const transport = connections({
281
+ const invoke = (target, input) => callTool(connections({
282
282
  identityToken: call.identityToken,
283
- audit: { action: `${toolset.name}.${tool.name}`, principalId: principal.id, requestId },
283
+ audit: {
284
+ action: `${toolset.name}.${target.name}`,
285
+ principalId: principal.id,
286
+ requestId,
287
+ },
288
+ }), toolset, target, { ...input, ...target.fixed });
289
+ const handle = tool.handle;
290
+ if (!handle) {
291
+ return yield* invoke(tool, call.input);
292
+ }
293
+ let costIndex = 0;
294
+ return yield* Effect.tryPromise({
295
+ try: (signal) => handle({
296
+ name: tool.name,
297
+ input: call.input,
298
+ principal,
299
+ signal,
300
+ recordCost: async (costUsd) => {
301
+ if (reportCost &&
302
+ principal.kind === "guest" &&
303
+ Number.isFinite(costUsd) &&
304
+ costUsd > 0) {
305
+ await Effect.runPromise(reportCost(principal.id, costUsd, `tool:${requestId}:${++costIndex}`), { signal });
306
+ }
307
+ },
308
+ call: async (name, input) => {
309
+ const target = toolset.tools.find((item) => item.name === name);
310
+ if (!target ||
311
+ (target !== tool && target.access !== "read") ||
312
+ !isAllowed(target.permission, principal)) {
313
+ throw new Error("The tool is unavailable to this caller.");
314
+ }
315
+ return Effect.runPromise(invoke(target, input), { signal });
316
+ },
317
+ }),
318
+ catch: () => new ActionRejected({ message: "The workspace could not complete the tool request." }),
284
319
  });
285
- return yield* callTool(transport, toolset, tool, { ...call.input, ...tool.fixed });
286
320
  });
287
321
  }
package/dist/sign-in.d.ts CHANGED
@@ -51,24 +51,28 @@ export interface ResolvedSignUp {
51
51
  /** The websites whose visitors may sign up on `web`, by origin. */
52
52
  readonly origins?: ReadonlyArray<string>;
53
53
  }
54
- /** The channels a guest talks to the agent through: only `web`, a website's chat, so far. */
55
- export declare const GUEST_CHANNELS: readonly ["web"];
56
- export type GuestChannel = (typeof GUEST_CHANNELS)[number];
54
+ /** Channel kinds with a guest admission path. Named connections select their own policy. */
55
+ export declare const GUEST_CHANNELS: readonly ["web", "telegram", "slack", "whatsapp"];
56
+ export type GuestChannel = (typeof GUEST_CHANNELS)[number] | WorkspaceSignUpChannel;
57
57
  export declare function isGuestChannel(value: unknown): value is GuestChannel;
58
58
  /**
59
59
  * Who may talk to the agent without signing in or signing up: a guest, the counterpart of the
60
60
  * built-in `member`. A guest is a session, not a user: nobody proved who they are, so no user or
61
- * identity record is created for them, and they hold the built-in role `guest` alone. Without
61
+ * identity record is created for them. The channel selects their roles, defaulting to `guest`. Without
62
62
  * `guest`, nobody chats without signing in or signing up.
63
63
  */
64
- export interface GuestOptions<P = unknown> {
65
- /** Where guests talk to the agent: `["web"]`, a declared website's chat. */
64
+ export interface GuestOptions<P = unknown, R = unknown> {
65
+ /** Named connections on which an authorized invitation opens guest questions in a new group. */
66
+ readonly openOnInvite?: ReadonlyArray<string>;
67
+ /** Roles replacing the default guest permissions on a listed channel. */
68
+ readonly byChannel?: Readonly<Partial<Record<GuestChannel, ReadonlyArray<R>>>>;
69
+ /** Websites, admitted Telegram or Slack groups, or a WhatsApp number receiving private messages. */
66
70
  readonly channels: ReadonlyArray<GuestChannel>;
67
71
  /**
68
72
  * The websites whose visitors are guests, as {@link websiteOrigin} accepts them, such as
69
73
  * `https://www.example.com` or `https://*.example.com`.
70
74
  */
71
- readonly origins: ReadonlyArray<string>;
75
+ readonly origins?: ReadonlyArray<string>;
72
76
  /**
73
77
  * What every guest may do, as role permissions: the permissions of the built-in role `guest`.
74
78
  * `check` holds them to the rules of sign-up roles, and a guest has no user record, so a scope
@@ -125,6 +129,9 @@ export type ResolvedGuestBotCheck = {
125
129
  };
126
130
  /** Guests as the manifest carries them; the built-in role `guest` holds their permissions. */
127
131
  export interface ResolvedGuest {
132
+ /** Named connections on which an authorized invitation opens guest questions in a new group. */
133
+ readonly openOnInvite?: ReadonlyArray<string>;
134
+ readonly byChannel?: Readonly<Partial<Record<GuestChannel, ReadonlyArray<string>>>>;
128
135
  readonly channels: ReadonlyArray<GuestChannel>;
129
136
  readonly origins: ReadonlyArray<string>;
130
137
  /** The challenge a new guest passes, when declared. */
package/dist/sign-in.js CHANGED
@@ -23,10 +23,11 @@ export function isSignUpChannel(value) {
23
23
  return (SIGN_UP_CHANNELS.includes(value) ||
24
24
  (typeof value === "string" && WORKSPACE_IDENTITY_PROVIDER.test(value)));
25
25
  }
26
- /** The channels a guest talks to the agent through: only `web`, a website's chat, so far. */
27
- export const GUEST_CHANNELS = ["web"];
26
+ /** Channel kinds with a guest admission path. Named connections select their own policy. */
27
+ export const GUEST_CHANNELS = ["web", "telegram", "slack", "whatsapp"];
28
28
  export function isGuestChannel(value) {
29
- return GUEST_CHANNELS.includes(value);
29
+ return (GUEST_CHANNELS.includes(value) ||
30
+ (typeof value === "string" && WORKSPACE_IDENTITY_PROVIDER.test(value)));
30
31
  }
31
32
  /**
32
33
  * The challenge providers a guest's bot check can verify. `turnstile` is Cloudflare Turnstile: the
package/dist/toolset.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { Schema } from "effect";
2
+ import type { Principal } from "./runtime.js";
3
+ import type { ToolResult } from "./service/toolsets.js";
2
4
  import type { AnyConnectionHandle } from "./connection.js";
3
5
  export declare const ToolsetTypeId: unique symbol;
4
6
  /** The lock format `segmentstream connection mcp lock` writes. */
@@ -50,8 +52,21 @@ export declare const ToolsetLock: Schema.Struct<{
50
52
  }>>;
51
53
  }>;
52
54
  export type ToolsetLock = typeof ToolsetLock.Type;
53
- /** The workspace's own words for one tool, replacing the provider's. */
55
+ /** Trusted workspace code runs after caller authorization, for every MCP entry point.
56
+ * call() may invoke this tool or another permitted read in the same locked toolset.
57
+ * Fixed arguments are applied by the transport. Raw provider results remain inside the hook. */
58
+ export interface ToolInvocation {
59
+ readonly principal: Principal;
60
+ readonly name: string;
61
+ readonly input: Readonly<Record<string, unknown>>;
62
+ readonly signal: AbortSignal;
63
+ readonly call: (name: string, input: Readonly<Record<string, unknown>>) => Promise<ToolResult>;
64
+ readonly recordCost: (costUsd: number) => Promise<void>;
65
+ }
66
+ export type ToolHandler = (invocation: ToolInvocation) => Promise<ToolResult>;
67
+ /** Workspace presentation and handling of an existing provider tool. */
54
68
  export interface ToolsetToolOptions {
69
+ readonly handle?: ToolHandler;
55
70
  readonly description?: string;
56
71
  }
57
72
  export interface ToolsetDefinition {
@@ -99,6 +114,7 @@ export interface ResolvedToolHints {
99
114
  }
100
115
  /** One tool of a checked toolset, as the service serves it. */
101
116
  export interface ResolvedTool {
117
+ readonly handle?: ToolHandler;
102
118
  /** The provider's tool name. */
103
119
  readonly name: string;
104
120
  /** `<toolset>_<tool>`. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.181.0",
3
+ "version": "0.182.0",
4
4
  "description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",
@@ -1,23 +0,0 @@
1
- import { Effect } from "effect";
2
- import type { HttpServerRequest } from "effect/unstable/http";
3
- import { makeOperations } from "../operations.js";
4
- type Resources = Parameters<typeof makeOperations>;
5
- export interface PublicActionOptions {
6
- readonly binding: {
7
- role: string;
8
- entity: string;
9
- action: string;
10
- };
11
- readonly plan: Resources[1];
12
- readonly hooks: Resources[2];
13
- readonly connections: Resources[3];
14
- readonly replicate: Resources[4];
15
- }
16
- /** Stable UUID in the given namespace. A group action's sender is scoped to that chat. */
17
- export declare function groupSubject(parts: readonly string[]): string;
18
- export declare function publicActionForGroup(backend: Resources[0], options: PublicActionOptions): (request: HttpServerRequest.HttpServerRequest, source: {
19
- channel: string;
20
- chat: string;
21
- sender: string;
22
- }, input: Readonly<Record<string, unknown>>) => Effect.Effect<{} | null, import("../../errors.js").ActionError | import("../../connection.js").ConnectionError | import("../../runtime/errors.js").WorkflowRecoveryUnavailable | import("../../runtime/errors.js").TriggerInputInvalid | import("../errors.js").InvalidActionInput, import("../errors.js").RequestId>;
23
- export {};
@@ -1,53 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import { Effect } from "effect";
3
- import { nameToWire } from "../../authoring-names.js";
4
- import { principalScopes, isAllowed } from "../../permissions.js";
5
- import { CurrentPrincipal } from "../authentication.js";
6
- import { makeOperations, identityToken } from "../operations.js";
7
- /** Stable UUID in the given namespace. A group action's sender is scoped to that chat. */
8
- export function groupSubject(parts) {
9
- const bytes = createHash("sha256").update(JSON.stringify(parts)).digest();
10
- bytes[6] = (bytes[6] & 15) | 80;
11
- bytes[8] = (bytes[8] & 63) | 128;
12
- const hex = bytes.toString("hex");
13
- return [
14
- hex.slice(0, 8),
15
- hex.slice(8, 12),
16
- hex.slice(12, 16),
17
- hex.slice(16, 20),
18
- hex.slice(20, 32),
19
- ].join("-");
20
- }
21
- export function publicActionForGroup(backend, options) {
22
- const { plan, binding } = options;
23
- const entity = plan.entities.find((item) => item.name === nameToWire(binding.entity));
24
- const action = entity?.actions.find((item) => !item.generated && item.name === nameToWire(binding.action));
25
- const role = plan.ontology.roles.find((item) => item.name === nameToWire(binding.role));
26
- const operations = makeOperations(backend, plan, options.hooks, options.connections, options.replicate);
27
- return (request, source, input) => Effect.gen(function* () {
28
- if (!entity || !action || action.action.confirm || !role) {
29
- return yield* Effect.die(new Error("Invalid public group action."));
30
- }
31
- const permissions = action.permissions.filter((permission) => role.permissions.includes(permission));
32
- const principal = {
33
- kind: "guest",
34
- id: groupSubject(["public-action", source.channel, source.chat, source.sender]),
35
- roles: [role.name],
36
- permissions,
37
- ...principalScopes([role], [role.name]),
38
- chat: { provider: source.channel, chatId: source.chat },
39
- };
40
- if (!action.permissions.every((p) => isAllowed(p, principal))) {
41
- return yield* Effect.die(new Error("Public group action denied."));
42
- }
43
- const answer = yield* operations
44
- .runAction(entity, action, {
45
- input,
46
- options: {},
47
- validateOnly: false,
48
- identityToken: identityToken(request),
49
- })
50
- .pipe(Effect.provideService(CurrentPrincipal, principal));
51
- return answer.result;
52
- });
53
- }