@fluenterp/runtime 0.1.0 → 1.0.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.
package/README.md CHANGED
@@ -1,25 +1,50 @@
1
- # `@fluenterp/runtime`
1
+ # @fluenterp/runtime
2
2
 
3
- The public contract between Fluent and project-owned document agents. It provides
4
- the declaration helpers, manifest schemas, validated tool/process dispatch, and
5
- runtime-image version check used by both sides of a project deployment.
3
+ What every Fluent project shares with Fluent. Every project depends on this
4
+ package, so a fix reaches all of them with a rebuild, and no project carries
5
+ its own copy to drift or to edit. It has two parts:
6
+
7
+ - **Auth.** How a project's app and Fluent prove who they are to each other.
8
+ - **Documents as code.** The declaration helpers (`defineDocument`,
9
+ `defineRecord`, `field`, `defineSubagent`, the tool and process helpers), the
10
+ manifest schemas, and `createRuntime`, which serves a project's documents
11
+ over HTTP. Fluent does not call this contract today (#5075 removed its side).
12
+ It stays here as the base for project-defined workflows (FLU-1849).
13
+
14
+ ## Auth
6
15
 
7
16
  ```ts
8
- import {
9
- createRuntime,
10
- defineDocument,
11
- defineProcess,
12
- defineRecord,
13
- field,
14
- } from "@fluenterp/runtime";
17
+ import { authenticateFluentRequest, egressAuthHeaders } from "@fluenterp/runtime";
18
+
19
+ // Inbound: who is calling, or null when the request did not come through Fluent.
20
+ const actor = await authenticateFluentRequest(
21
+ (name) => c.req.header(name),
22
+ process.env.FLUENT_PROJECT_PROXY_SECRET,
23
+ );
24
+ if (!actor) return c.json({ detail: "Unauthorized" }, 401);
25
+
26
+ // Outbound: the header that proves this app to Fluent's mail, files, secrets and ERP egress.
27
+ const auth = await egressAuthHeaders();
28
+ await fetch(process.env.FLUENT_MAIL_URL, { method: "POST", headers: { ...auth }, body });
15
29
  ```
16
30
 
17
- This package deliberately contains no credentials, customer document
18
- definitions, connection code, privileged Fluent API clients, or private service
19
- addresses. Project authentication remains at the Fluent proxy, and the matching
20
- OCI base image remains in private ECR.
31
+ ## What it reads
32
+
33
+ Fluent sets these in the sandbox and in deployed apps. All are server-only.
34
+
35
+ | Variable | Used for |
36
+ | ------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
37
+ | `FLUENT_IDENTITY_JWKS_URL`, `FLUENT_IDENTITY_ISSUER`, `FLUENT_PROJECT_ID` | Checking `X-Fluent-Identity`, the caller's identity Fluent signs for this project |
38
+ | `FLUENT_EGRESS_SECRET` | Egress from the sandbox, which has no AWS role |
39
+ | `AWS_CONTAINER_CREDENTIALS_*` | Egress from a deployed app, signed with its own task role |
40
+
41
+ ## Documents as code
21
42
 
22
- ## Repeatable lines
43
+ ```ts
44
+ import { createRuntime, defineDocument, defineProcess, defineRecord, field } from "@fluenterp/runtime";
45
+ ```
46
+
47
+ ### Repeatable lines
23
48
 
24
49
  Records may declare one level of typed collections. Fluent extracts and renders
25
50
  the rows, runs each grounded item field through its project resolver, and blocks
@@ -59,57 +84,12 @@ result is accepted. Process repeats that terminal verification for every
59
84
  grounded field and replaces browser-supplied objects with the canonical verified
60
85
  results before customer write code runs.
61
86
 
62
- ## Release model
63
-
64
- The package and OCI image use the same version. A project declares an exact npm
65
- version, and Fluent builds it on the immutable image tag with that version. The
66
- server calls `assertRuntimeVersion()` at startup so package/image skew fails
67
- before it can process a document.
68
-
69
- Before the first release, let the pull request apply the `dev` Pulumi stack and
70
- let the merge apply `prod`. The authorized `Infra (Prod)` workflow then builds
71
- the image once and pushes the same immutable version to
72
- `fluent-runtime-dev` and `fluent-runtime-prod`.
87
+ ## Releasing
73
88
 
74
- ### One-time npm bootstrap
75
-
76
- npm requires a package to exist before it can trust a GitHub Actions publisher.
77
- After the change is on `main`, build and inspect the exact public artifact:
78
-
79
- ```bash
80
- pnpm install --frozen-lockfile
81
- pnpm --filter @fluenterp/runtime typecheck
82
- pnpm --filter @fluenterp/runtime test --runInBand
83
- pnpm --filter @fluenterp/runtime build
84
- npm pack ./packages/ts/runtime
85
- npm publish ./fluenterp-runtime-0.1.0.tgz --access public
86
- ```
87
-
88
- The first publish is interactive and 2FA-protected. Do not create an `NPM_TOKEN`
89
- GitHub secret. On the new package page, configure **Settings → Trusted Publisher**:
90
-
91
- - Organization or user: `tcpmia`
92
- - Repository: `fluent`
93
- - Workflow filename: `publish-runtime.yml`
94
- - Allowed action: `npm publish`
95
- - Environment: leave blank
96
-
97
- Then set publishing access to require 2FA and disallow tokens. All later npm
98
- publishes use short-lived GitHub OIDC credentials.
99
-
100
- ### Publish a version
101
-
102
- Update `package.json` and `src/version.ts` to the same semantic version and merge
103
- to `main`. Wait for `Infra (Prod)` to publish that image version to both private
104
- ECR repositories, then tag the merge commit:
105
-
106
- ```bash
107
- git tag runtime-v0.1.0
108
- git push origin runtime-v0.1.0
109
- ```
89
+ Bump `version` in `package.json` and merge. Then run the "Publish Project
90
+ Runtime" workflow from `main` with **publish** on. It tests, packs and
91
+ publishes that version. The job runs in the `production` environment, which
92
+ only `main` may use, and npm trusts the workflow only in that environment.
110
93
 
111
- The `Publish Fluent Runtime` workflow checks the matching version, tests and
112
- packs the npm artifact, imports it from a locally built image, and publishes the
113
- public package. Its manual trigger is build-only unless `publish` is explicitly
114
- selected. AWS publishing stays in the existing branch-restricted deployment
115
- workflow; the npm tag workflow receives no AWS credentials.
94
+ Keep `RUNTIME_VERSION` in `src/version.ts` equal to `version`. 1.0.0 keeps
95
+ every export of 0.1.0 and adds the auth.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,46 @@
1
1
  import { z } from 'zod';
2
2
 
3
+ declare const IDENTITY_HEADER = "x-fluent-identity";
4
+ /** Who is calling the app, as Fluent vouches for them. */
5
+ interface Actor {
6
+ id: string;
7
+ teamId: string | null;
8
+ role: string | null;
9
+ scopes: string[];
10
+ /** The same list as `scopes`, for tools written before the rename. */
11
+ capabilities: string[];
12
+ }
13
+ /**
14
+ * The actor Fluent signed into an `X-Fluent-Identity` token, or null when the
15
+ * token is not valid for this project. Throws when the app has not been told
16
+ * where Fluent's keys are.
17
+ */
18
+ declare function verifyFluentIdentity(token: string): Promise<Actor | null>;
19
+
20
+ /**
21
+ * Who a request to the app comes from, or null when it did not come through
22
+ * Fluent's proxy. Call it on every request but the health check, and answer
23
+ * 401 to a null.
24
+ *
25
+ * `header` reads one request header by its lower-case name, so this works with
26
+ * any server: `(name) => c.req.header(name)` in Hono.
27
+ *
28
+ * Fluent signs the caller into `X-Fluent-Identity`. When the header is there
29
+ * the actor comes from its claims, and a token that fails the check is refused
30
+ * whatever else the request carries. A request without it must present the
31
+ * project's proxy secret, and its X-Fluent-* headers are trusted once the
32
+ * secret checks out. This is the one place that decides which proof counts, so
33
+ * a new release of this package can retire the proxy secret for every app.
34
+ */
35
+ declare function authenticateFluentRequest(header: (name: string) => string | undefined | null, proxySecret: string | undefined): Promise<Actor | null>;
36
+
37
+ /**
38
+ * The header that proves this app to Fluent's egress: the task's AWS identity
39
+ * when ECS gave it a role, otherwise FLUENT_EGRESS_SECRET. Null when neither
40
+ * is available. Spread it into the headers of every call to a FLUENT_*_URL.
41
+ */
42
+ declare function egressAuthHeaders(): Promise<Record<string, string> | null>;
43
+
3
44
  /** How a document field's value is produced. */
4
45
  type Strategy = "extract" | "ground" | "choose" | "reconcile" | "revise" | "computed";
5
46
  type FieldKind = "text" | "number" | "collection";
@@ -482,7 +523,7 @@ interface ProjectRuntime {
482
523
  }
483
524
  declare function createRuntime(documents: Record<string, DocumentDefinition>): ProjectRuntime;
484
525
 
485
- declare const RUNTIME_VERSION = "0.1.0";
526
+ declare const RUNTIME_VERSION = "1.0.0";
486
527
  declare function assertRuntimeVersion(imageVersion?: string | undefined): void;
487
528
 
488
- export { type AnyTool, type Dataset, type DocumentDefinition, type DocumentManifest, DocumentManifestSchema, type FieldKind, type FieldManifest, FieldManifestSchema, type FieldSpec, type ItemFieldManifest, type Manifest, ManifestSchema, type ProcessDefinition, type ProjectRuntime, RUNTIME_VERSION, type RecordDefinition, type ResolverManifest, ResolverManifestSchema, type ResolverPresentation, type ResolverPresentationManifest, ResolverPresentationSchema, RuntimeError, type RuntimeErrorType, type SearchTool, type Strategy, type Subagent, type TerminalTool, type ToolManifest, ToolManifestSchema, type ToolResult, type VerifyResult, assertRuntimeVersion, buildDataset, buildManifest, commitBlockers, createRuntime, defineDocument, defineProcess, defineRecord, defineSearchTool, defineSubagent, defineTerminalTool, extractableFields, extractableRecordFields, field, groundedRecordFields, itemResolutionOrder, resolutionOrder, resolverFor, unmappedFields };
529
+ export { type Actor, type AnyTool, type Dataset, type DocumentDefinition, type DocumentManifest, DocumentManifestSchema, type FieldKind, type FieldManifest, FieldManifestSchema, type FieldSpec, IDENTITY_HEADER, type ItemFieldManifest, type Manifest, ManifestSchema, type ProcessDefinition, type ProjectRuntime, RUNTIME_VERSION, type RecordDefinition, type ResolverManifest, ResolverManifestSchema, type ResolverPresentation, type ResolverPresentationManifest, ResolverPresentationSchema, RuntimeError, type RuntimeErrorType, type SearchTool, type Strategy, type Subagent, type TerminalTool, type ToolManifest, ToolManifestSchema, type ToolResult, type VerifyResult, assertRuntimeVersion, authenticateFluentRequest, buildDataset, buildManifest, commitBlockers, createRuntime, defineDocument, defineProcess, defineRecord, defineSearchTool, defineSubagent, defineTerminalTool, egressAuthHeaders, extractableFields, extractableRecordFields, field, groundedRecordFields, itemResolutionOrder, resolutionOrder, resolverFor, unmappedFields, verifyFluentIdentity };
package/dist/index.js CHANGED
@@ -38,6 +38,120 @@ var __async = (__this, __arguments, generator) => {
38
38
  });
39
39
  };
40
40
 
41
+ // src/auth.ts
42
+ import { timingSafeEqual } from "crypto";
43
+
44
+ // src/identity.ts
45
+ import { createRemoteJWKSet, jwtVerify } from "jose";
46
+ var IDENTITY_HEADER = "x-fluent-identity";
47
+ var jwks;
48
+ function verifyFluentIdentity(token) {
49
+ return __async(this, null, function* () {
50
+ const jwksUrl = process.env.FLUENT_IDENTITY_JWKS_URL;
51
+ const issuer = process.env.FLUENT_IDENTITY_ISSUER;
52
+ const projectId = process.env.FLUENT_PROJECT_ID;
53
+ if (!jwksUrl || !issuer || !projectId) {
54
+ throw new Error(
55
+ "FLUENT_IDENTITY_JWKS_URL, FLUENT_IDENTITY_ISSUER and FLUENT_PROJECT_ID are required to check X-Fluent-Identity"
56
+ );
57
+ }
58
+ if ((jwks == null ? void 0 : jwks.url) !== jwksUrl) jwks = { url: jwksUrl, keys: createRemoteJWKSet(new URL(jwksUrl)) };
59
+ try {
60
+ const { payload } = yield jwtVerify(token, jwks.keys, {
61
+ issuer,
62
+ audience: projectId,
63
+ algorithms: ["ES256"]
64
+ });
65
+ if (typeof payload.sub !== "string") return null;
66
+ const scopes = Array.isArray(payload.scopes) ? payload.scopes.filter((s) => typeof s === "string") : [];
67
+ return {
68
+ id: payload.sub,
69
+ teamId: typeof payload.team === "string" ? payload.team : null,
70
+ role: typeof payload.role === "string" ? payload.role : null,
71
+ scopes,
72
+ capabilities: scopes
73
+ };
74
+ } catch (e) {
75
+ return null;
76
+ }
77
+ });
78
+ }
79
+
80
+ // src/auth.ts
81
+ function authenticateFluentRequest(header, proxySecret) {
82
+ return __async(this, null, function* () {
83
+ var _a, _b, _c, _d;
84
+ const identity = header(IDENTITY_HEADER);
85
+ if (identity) return verifyFluentIdentity(identity);
86
+ const presented = header("x-fluent-proxy-secret");
87
+ if (!presented || !proxySecret || !sameSecret(presented, proxySecret)) return null;
88
+ const scopes = ((_a = header("x-fluent-token-scopes")) != null ? _a : "").split(/\s+/).filter((s) => s.length > 0);
89
+ return {
90
+ id: (_b = header("x-fluent-user-id")) != null ? _b : "system:anonymous",
91
+ teamId: (_c = header("x-fluent-team-id")) != null ? _c : null,
92
+ role: (_d = header("x-fluent-role")) != null ? _d : null,
93
+ scopes,
94
+ capabilities: scopes
95
+ };
96
+ });
97
+ }
98
+ function sameSecret(a, b) {
99
+ const left = Buffer.from(a);
100
+ const right = Buffer.from(b);
101
+ return left.length === right.length && timingSafeEqual(left, right);
102
+ }
103
+
104
+ // src/egress.ts
105
+ import { Sha256 } from "@aws-crypto/sha256-js";
106
+ import { fromContainerMetadata } from "@smithy/credential-provider-imds";
107
+ import { HttpRequest } from "@smithy/protocol-http";
108
+ import { SignatureV4 } from "@smithy/signature-v4";
109
+ var STS_HOST = "sts.us-east-2.amazonaws.com";
110
+ var EXPIRES_SECONDS = 15 * 60;
111
+ var REUSE_MS = 4 * 60 * 1e3;
112
+ var signer;
113
+ var cached;
114
+ function taskIdentityToken() {
115
+ return __async(this, null, function* () {
116
+ var _a;
117
+ if (cached && Date.now() - cached.mintedAt < REUSE_MS) return cached.token;
118
+ signer != null ? signer : signer = new SignatureV4({
119
+ credentials: fromContainerMetadata(),
120
+ region: "us-east-2",
121
+ service: "sts",
122
+ sha256: Sha256
123
+ });
124
+ const signed = yield signer.presign(
125
+ new HttpRequest({
126
+ method: "GET",
127
+ protocol: "https:",
128
+ hostname: STS_HOST,
129
+ path: "/",
130
+ query: { Action: "GetCallerIdentity", Version: "2011-06-15" },
131
+ // Signed in, so the request can't be replayed to another service.
132
+ headers: { host: STS_HOST, "x-fl-audience": "fluent" }
133
+ }),
134
+ { expiresIn: EXPIRES_SECONDS }
135
+ );
136
+ const url = new URL(`https://${STS_HOST}/`);
137
+ for (const [key, value] of Object.entries((_a = signed.query) != null ? _a : {})) {
138
+ if (typeof value === "string") url.searchParams.set(key, value);
139
+ }
140
+ const token = Buffer.from(url.toString()).toString("base64url");
141
+ cached = { token, mintedAt: Date.now() };
142
+ return token;
143
+ });
144
+ }
145
+ function egressAuthHeaders() {
146
+ return __async(this, null, function* () {
147
+ if (process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI || process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI) {
148
+ return { "x-fl-aws-identity": yield taskIdentityToken() };
149
+ }
150
+ const secret = process.env.FLUENT_EGRESS_SECRET;
151
+ return secret ? { "x-fluent-egress-auth": secret } : null;
152
+ });
153
+ }
154
+
41
155
  // src/record.ts
42
156
  import { z } from "zod";
43
157
  function defineRecord(input) {
@@ -634,7 +748,7 @@ function reverifyGroundedValue(definition, path, resolverName, value) {
634
748
  }
635
749
 
636
750
  // src/version.ts
637
- var RUNTIME_VERSION = "0.1.0";
751
+ var RUNTIME_VERSION = "1.0.0";
638
752
  function assertRuntimeVersion(imageVersion = process.env.FLUENT_RUNTIME_IMAGE_VERSION) {
639
753
  if (!imageVersion || imageVersion === RUNTIME_VERSION) return;
640
754
  throw new Error(
@@ -644,6 +758,7 @@ function assertRuntimeVersion(imageVersion = process.env.FLUENT_RUNTIME_IMAGE_VE
644
758
  export {
645
759
  DocumentManifestSchema,
646
760
  FieldManifestSchema,
761
+ IDENTITY_HEADER,
647
762
  ManifestSchema,
648
763
  RUNTIME_VERSION,
649
764
  ResolverManifestSchema,
@@ -651,6 +766,7 @@ export {
651
766
  RuntimeError,
652
767
  ToolManifestSchema,
653
768
  assertRuntimeVersion,
769
+ authenticateFluentRequest,
654
770
  buildDataset,
655
771
  buildManifest,
656
772
  commitBlockers,
@@ -661,6 +777,7 @@ export {
661
777
  defineSearchTool,
662
778
  defineSubagent,
663
779
  defineTerminalTool,
780
+ egressAuthHeaders,
664
781
  extractableFields,
665
782
  extractableRecordFields,
666
783
  field,
@@ -668,5 +785,6 @@ export {
668
785
  itemResolutionOrder,
669
786
  resolutionOrder,
670
787
  resolverFor,
671
- unmappedFields
788
+ unmappedFields,
789
+ verifyFluentIdentity
672
790
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fluenterp/runtime",
3
- "version": "0.1.0",
4
- "description": "Runtime contract for Fluent project agents",
3
+ "version": "1.0.0",
4
+ "description": "What every Fluent project shares with Fluent: auth both ways, and documents as code",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
7
7
  "type": "git",
@@ -9,31 +9,35 @@
9
9
  "directory": "packages/ts/runtime"
10
10
  },
11
11
  "files": [
12
+ "README.md",
12
13
  "dist/**"
13
14
  ],
14
15
  "type": "module",
15
- "main": "./dist/index.js",
16
- "types": "./dist/index.d.ts",
17
16
  "exports": {
18
17
  ".": {
19
18
  "types": "./dist/index.d.ts",
20
- "import": "./dist/index.js",
21
- "default": "./dist/index.js"
19
+ "import": "./dist/index.js"
22
20
  }
23
21
  },
24
22
  "publishConfig": {
25
23
  "access": "public"
26
24
  },
27
25
  "dependencies": {
28
- "zod": "4.4.3"
26
+ "@aws-crypto/sha256-js": "^5.2.0",
27
+ "@smithy/credential-provider-imds": "^4.5.2",
28
+ "@smithy/protocol-http": "^5.6.2",
29
+ "@smithy/signature-v4": "^5.7.5",
30
+ "jose": "^6.2.2",
31
+ "zod": "^4.6.7"
29
32
  },
30
33
  "devDependencies": {
31
- "@jest/globals": "^30.2.0",
34
+ "@jest/globals": "^30.5.2",
32
35
  "@types/jest": "^30.0.0",
33
36
  "@types/node": "^24.13.3",
34
- "jest": "^29.7.0",
35
- "ts-jest": "^29.1.2",
36
- "tsup": "^8.0.2",
37
+ "jest": "^30.5.2",
38
+ "ts-jest": "^29.4.15",
39
+ "ts-node": "^10.9.2",
40
+ "tsup": "^8.5.1",
37
41
  "typescript": "^5.9.3",
38
42
  "@fluent/typescript-config": "0.1.0"
39
43
  },
@@ -43,7 +47,7 @@
43
47
  "scripts": {
44
48
  "build": "tsup",
45
49
  "lint": "oxlint src/",
46
- "test": "NODE_OPTIONS='--experimental-vm-modules' jest",
50
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest",
47
51
  "typecheck": "tsgo --noEmit"
48
52
  }
49
53
  }