@gaia-ai/addon-workspace 0.11.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 keytec GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @gaia-ai/addon-workspace
2
+
3
+ GAIA connection addon: fills the ambient workspace (gaia_workspace) into a create payload transparently, via dropsh's alterRequest hook.
4
+
5
+ Part of the GAIA CLI. Install the meta package `@gaia-ai/gaia` to get the `gaia` CLI with all addons. Source: https://git.key-tec.de/keytec/gaia (gaia-cli/).
@@ -0,0 +1,51 @@
1
+ import type { DropSHPlugin } from 'dropsh/plugin';
2
+ /**
3
+ * The entity types whose workspace is a DIRECT `workspace_id` field, and which
4
+ * this addon may therefore fill from context.
5
+ *
6
+ * `gaia_project` and `gaia_ticket` are deliberately ABSENT. Their workspace
7
+ * anchor is `group_id` / `project_id` — a genuine choice the caller must make
8
+ * (which group? which project?), not an ambient fact. The server narrows the
9
+ * offer for those two (`GroupInActiveWorkspaceSelection` /
10
+ * `ProjectInActiveWorkspaceSelection`) and `GaiaWorkspaceScope` gates the write;
11
+ * guessing one here would be inventing data, not supplying context.
12
+ *
13
+ * `gaia_term` joined in GAIA-339. It meets the criterion exactly — a direct
14
+ * `workspace_id`, and which workspace a label belongs to is ambient rather than
15
+ * chosen — and it is not merely a convenience: `@gaia/qualify-ticket` creates a
16
+ * missing `work:*` term through dropsh, so without this entry that step answers
17
+ * 422 on `workspace_id` on every instance where the term is not seeded yet.
18
+ */
19
+ export declare const DEFAULT_WORKSPACE_ENTITY_TYPES: readonly string[];
20
+ /** Options for the workspace addon — normally the connection config's globals. */
21
+ export interface WorkspaceOptions {
22
+ /**
23
+ * The UUID of the `gaia_workspace` every create is bound to. REQUIRED at the
24
+ * top level of the connection config; `loadGaiaConfig` rejects a config
25
+ * without it, so by the time this addon runs the value is present.
26
+ */
27
+ workspace_id?: string;
28
+ /**
29
+ * Override the entity types whose `workspace_id` is filled from context.
30
+ * Defaults to `DEFAULT_WORKSPACE_ENTITY_TYPES`; configurable so a site that
31
+ * adds a further directly-bound type does not need a new addon release.
32
+ */
33
+ entity_types?: string[];
34
+ }
35
+ /** No workspace configured, but a create needed one. Named so `PluginError` reads. */
36
+ export declare class WorkspaceConfigError extends Error {
37
+ readonly name = "WorkspaceConfigError";
38
+ }
39
+ /** The create body was not parseable JSON — fail loudly, never half-parse. */
40
+ export declare class WorkspacePayloadError extends Error {
41
+ readonly name = "WorkspacePayloadError";
42
+ }
43
+ /**
44
+ * The GAIA workspace plugin. Constructed by the `./preset` accumulator from the
45
+ * connection config's global options (via `@gaia-ai/addon-essentials`, which
46
+ * lists this addon as a preset child), or directly by dropsh from a legacy
47
+ * `plugins: []` descriptor (this module default-exports the factory, so no
48
+ * `export:` is needed either way).
49
+ */
50
+ export declare function workspacePlugin(opts?: WorkspaceOptions): DropSHPlugin;
51
+ export default workspacePlugin;
@@ -0,0 +1,107 @@
1
+ /**
2
+ * The entity types whose workspace is a DIRECT `workspace_id` field, and which
3
+ * this addon may therefore fill from context.
4
+ *
5
+ * `gaia_project` and `gaia_ticket` are deliberately ABSENT. Their workspace
6
+ * anchor is `group_id` / `project_id` — a genuine choice the caller must make
7
+ * (which group? which project?), not an ambient fact. The server narrows the
8
+ * offer for those two (`GroupInActiveWorkspaceSelection` /
9
+ * `ProjectInActiveWorkspaceSelection`) and `GaiaWorkspaceScope` gates the write;
10
+ * guessing one here would be inventing data, not supplying context.
11
+ *
12
+ * `gaia_term` joined in GAIA-339. It meets the criterion exactly — a direct
13
+ * `workspace_id`, and which workspace a label belongs to is ambient rather than
14
+ * chosen — and it is not merely a convenience: `@gaia/qualify-ticket` creates a
15
+ * missing `work:*` term through dropsh, so without this entry that step answers
16
+ * 422 on `workspace_id` on every instance where the term is not seeded yet.
17
+ */
18
+ export const DEFAULT_WORKSPACE_ENTITY_TYPES = [
19
+ 'gaia_group',
20
+ 'gaia_team',
21
+ 'gaia_term',
22
+ ];
23
+ /** The JSON:API resource type of the workspace a `workspace_id` names. */
24
+ const WORKSPACE_RESOURCE_TYPE = 'gaia_workspace--gaia_workspace';
25
+ /** The relationship this addon fills. */
26
+ const WORKSPACE_FIELD = 'workspace_id';
27
+ /** No workspace configured, but a create needed one. Named so `PluginError` reads. */
28
+ export class WorkspaceConfigError extends Error {
29
+ name = 'WorkspaceConfigError';
30
+ }
31
+ /** The create body was not parseable JSON — fail loudly, never half-parse. */
32
+ export class WorkspacePayloadError extends Error {
33
+ name = 'WorkspacePayloadError';
34
+ }
35
+ function isRecord(v) {
36
+ return typeof v === 'object' && v !== null && !Array.isArray(v);
37
+ }
38
+ /**
39
+ * The GAIA workspace plugin. Constructed by the `./preset` accumulator from the
40
+ * connection config's global options (via `@gaia-ai/addon-essentials`, which
41
+ * lists this addon as a preset child), or directly by dropsh from a legacy
42
+ * `plugins: []` descriptor (this module default-exports the factory, so no
43
+ * `export:` is needed either way).
44
+ */
45
+ export function workspacePlugin(opts = {}) {
46
+ const workspaceId = typeof opts.workspace_id === 'string' ? opts.workspace_id.trim() : '';
47
+ const entityTypes = new Set(opts.entity_types ?? [...DEFAULT_WORKSPACE_ENTITY_TYPES]);
48
+ return {
49
+ id: 'gaia-workspace',
50
+ requiredModules: [],
51
+ async alterRequest(req, ctx) {
52
+ // (1) Creates only. An `update` that re-binds an existing row to another
53
+ // workspace is a real decision governed by the server's own rule — never
54
+ // something to slip in ambiently — so PATCH is passed through untouched.
55
+ if (ctx.operation !== 'create')
56
+ return req;
57
+ // (2) Only the types whose workspace is a direct `workspace_id` field.
58
+ if (ctx.entityType === undefined || !entityTypes.has(ctx.entityType)) {
59
+ return req;
60
+ }
61
+ // (3) `upload` bodies are binary; nothing to parse.
62
+ if (typeof req.body !== 'string')
63
+ return req;
64
+ // (6) A create that needs a workspace with none configured must not go out
65
+ // unbound — the server would answer 422 and the cause would be invisible.
66
+ // dropsh wraps this in a PluginError carrying `gaia-workspace`. Normally
67
+ // unreachable: `workspace_id` is required at config load.
68
+ if (workspaceId === '') {
69
+ throw new WorkspaceConfigError(`@gaia-ai/addon-workspace: creating a "${ctx.entityType}" needs an ambient workspace, but none is configured. ` +
70
+ `Set the top-level \`${WORKSPACE_FIELD}: '<gaia_workspace uuid>'\` in your connection config (.gaia/gaia.config.js).`);
71
+ }
72
+ // (7) Malformed JSON must fail by name, never be swallowed into a request
73
+ // that is half-rewritten.
74
+ let doc;
75
+ try {
76
+ doc = JSON.parse(req.body);
77
+ }
78
+ catch (cause) {
79
+ throw new WorkspacePayloadError(`@gaia-ai/addon-workspace: the "${ctx.entityType}" create body is not valid JSON, so the ambient workspace could not be applied: ${String(cause)}`);
80
+ }
81
+ if (!isRecord(doc) || !isRecord(doc.data))
82
+ return req;
83
+ const relationships = isRecord(doc.data.relationships)
84
+ ? doc.data.relationships
85
+ : undefined;
86
+ // (4) An explicit value ALWAYS wins over the ambient one.
87
+ if (relationships && WORKSPACE_FIELD in relationships)
88
+ return req;
89
+ // (5) Inject. Headers are untouched by construction — only `body` is
90
+ // replaced, so `Authorization` (applied before this hook) survives verbatim.
91
+ const next = {
92
+ ...doc,
93
+ data: {
94
+ ...doc.data,
95
+ relationships: {
96
+ ...(relationships ?? {}),
97
+ [WORKSPACE_FIELD]: {
98
+ data: { type: WORKSPACE_RESOURCE_TYPE, id: workspaceId },
99
+ },
100
+ },
101
+ },
102
+ };
103
+ return { ...req, body: JSON.stringify(next) };
104
+ },
105
+ };
106
+ }
107
+ export default workspacePlugin;
@@ -0,0 +1,3 @@
1
+ import type { GaiaPreset } from '@gaia-ai/core';
2
+ import { type WorkspaceOptions } from './index.js';
3
+ export declare const connectionPlugins: GaiaPreset<WorkspaceOptions>['connectionPlugins'];
@@ -0,0 +1,8 @@
1
+ import { workspacePlugin } from './index.js';
2
+ export const connectionPlugins = (acc, opts) => {
3
+ const o = (opts ?? {});
4
+ if (typeof o.workspace_id !== 'string' || o.workspace_id.trim() === '') {
5
+ return acc;
6
+ }
7
+ return [...acc, workspacePlugin(o)];
8
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@gaia-ai/addon-workspace",
3
+ "version": "0.11.0",
4
+ "description": "GAIA connection addon: fills the ambient workspace (gaia_workspace) into a create payload transparently, via dropsh's alterRequest hook.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "exports": {
8
+ ".": "./dist/src/index.js",
9
+ "./preset": "./dist/src/preset.js"
10
+ },
11
+ "files": [
12
+ "dist/src"
13
+ ],
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://git.key-tec.de/keytec/gaia.git",
20
+ "directory": "gaia-cli/addons/workspace"
21
+ },
22
+ "dependencies": {
23
+ "dropsh": "^0.6.1"
24
+ },
25
+ "peerDependencies": {
26
+ "@gaia-ai/core": "^0.11.0"
27
+ }
28
+ }