agentscape3d 0.0.0-stage → 1.0.0-beta.2
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 +21 -0
- package/README.md +29 -2
- package/dist/core.d.ts +68 -0
- package/dist/core.js +209 -0
- package/dist/core.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +215 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AgentScape contributors
|
|
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
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# AgentScape 3D Core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**agentscape3d** is a small, unrestricted semantic and action extension for host-owned Three.js scenes. It does not require Studio, a renderer, WorldRuntime, MCP, an AI provider, an approval workflow, a sandbox or a physics engine.
|
|
4
|
+
|
|
5
|
+
~~~js
|
|
6
|
+
import * as THREE from 'three';
|
|
7
|
+
import { attach } from 'agentscape3d';
|
|
8
|
+
|
|
9
|
+
const scene = new THREE.Scene();
|
|
10
|
+
const door = new THREE.Group();
|
|
11
|
+
scene.add(door);
|
|
12
|
+
const world = attach(scene);
|
|
13
|
+
world.register(door, {
|
|
14
|
+
id: 'door', type: 'custom-door',
|
|
15
|
+
read: () => ({ angle: door.rotation.y }),
|
|
16
|
+
actions: { open: () => { door.rotation.y = -Math.PI / 2; } }
|
|
17
|
+
});
|
|
18
|
+
await world.invoke('door', 'open');
|
|
19
|
+
world.dispose(); // Does not dispose the scene or door
|
|
20
|
+
~~~
|
|
21
|
+
|
|
22
|
+
The same API works on objects imported through GLTFLoader or created in JavaScript. Use world.use(plugin) for optional extensions; there are no automatically installed plugins.
|
|
23
|
+
|
|
24
|
+
Optional npm packages:
|
|
25
|
+
- **agentscape3d-engine**: legacy full WorldRuntime, physics, assets and the explicit MCP integration
|
|
26
|
+
- **agentscape3d-studio**: standalone Studio and static website
|
|
27
|
+
|
|
28
|
+
See docs/sdk-packages.md and docs/semantic-core.md in the repository.
|
|
29
|
+
|
|
30
|
+
Beta 2 changes the default package entry: the former all-in-one runtime moves to agentscape3d-engine. Install explicitly with npm install agentscape3d@beta three. Code executes with the permissions of the host JavaScript environment; Core does not impose or claim a security boundary.
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-independent Three.js semantics. The scene and Object3D remain
|
|
3
|
+
* owned and rendered by the host application.
|
|
4
|
+
*/
|
|
5
|
+
export interface SemanticObject3D {
|
|
6
|
+
readonly isObject3D: boolean;
|
|
7
|
+
readonly uuid?: string;
|
|
8
|
+
readonly name?: string;
|
|
9
|
+
readonly parent?: SemanticObject3D | null;
|
|
10
|
+
}
|
|
11
|
+
export interface SemanticScene extends SemanticObject3D {
|
|
12
|
+
readonly isScene: boolean;
|
|
13
|
+
}
|
|
14
|
+
export type SemanticTarget = string | SemanticObject3D;
|
|
15
|
+
export interface SemanticActionContext {
|
|
16
|
+
readonly object: SemanticObject3D;
|
|
17
|
+
readonly world: SemanticWorld;
|
|
18
|
+
readonly signal?: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
export type SemanticActionFn = (args: unknown, context: SemanticActionContext) => unknown | Promise<unknown>;
|
|
21
|
+
export type SemanticAction = SemanticActionFn | {
|
|
22
|
+
execute: SemanticActionFn;
|
|
23
|
+
description?: string;
|
|
24
|
+
available?: (context: SemanticActionContext) => boolean;
|
|
25
|
+
};
|
|
26
|
+
export interface SemanticDefinition {
|
|
27
|
+
id?: string;
|
|
28
|
+
type?: string;
|
|
29
|
+
label?: string;
|
|
30
|
+
description?: string;
|
|
31
|
+
properties?: unknown;
|
|
32
|
+
read?: () => unknown;
|
|
33
|
+
actions?: Record<string, SemanticAction>;
|
|
34
|
+
}
|
|
35
|
+
export interface SemanticEntity {
|
|
36
|
+
id: string;
|
|
37
|
+
name: string;
|
|
38
|
+
type: string;
|
|
39
|
+
label: string;
|
|
40
|
+
description: string;
|
|
41
|
+
properties: unknown;
|
|
42
|
+
attached: boolean;
|
|
43
|
+
state: unknown;
|
|
44
|
+
actions: Array<{ name: string; description: string; available: boolean }>;
|
|
45
|
+
}
|
|
46
|
+
export interface SemanticEvent {
|
|
47
|
+
type: 'entity.registered' | 'entity.unregistered' | 'action.registered' | 'action.unregistered' | 'action.invoked' | 'disposed';
|
|
48
|
+
id: string | null;
|
|
49
|
+
world: SemanticWorld;
|
|
50
|
+
}
|
|
51
|
+
export interface SemanticPlugin {
|
|
52
|
+
install(world: SemanticWorld, options?: unknown): void | (() => void) | { dispose(): void };
|
|
53
|
+
}
|
|
54
|
+
export interface SemanticWorld {
|
|
55
|
+
readonly scene: SemanticScene;
|
|
56
|
+
register(object: SemanticObject3D, definition?: SemanticDefinition): () => boolean;
|
|
57
|
+
unregister(target: SemanticTarget): boolean;
|
|
58
|
+
registerAction(target: SemanticTarget, name: string, action: SemanticAction): () => boolean;
|
|
59
|
+
getObject(target: SemanticTarget): SemanticObject3D | null;
|
|
60
|
+
inspect(target: SemanticTarget): SemanticEntity | null;
|
|
61
|
+
list(options?: { attachedOnly?: boolean }): SemanticEntity[];
|
|
62
|
+
invoke(target: SemanticTarget, name: string, args?: unknown, options?: { signal?: AbortSignal }): Promise<unknown>;
|
|
63
|
+
subscribe(listener: (event: SemanticEvent) => void): () => boolean;
|
|
64
|
+
use(plugin: SemanticPlugin | ((world: SemanticWorld, options?: unknown) => ReturnType<SemanticPlugin['install']>), options?: unknown): () => boolean;
|
|
65
|
+
dispose(): void;
|
|
66
|
+
}
|
|
67
|
+
/** Attach without renderer takeover, Studio, task compiler, MCP, permissions or sandbox. */
|
|
68
|
+
export declare function attach(scene: SemanticScene): SemanticWorld;
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// src/core.js
|
|
2
|
+
/**
|
|
3
|
+
* A framework-agnostic semantic extension for a user-owned Three.js scene.
|
|
4
|
+
* It never creates a renderer, starts a loop, rewrites Object3D or executes code.
|
|
5
|
+
* Policies, transports, persistence, physics and agent tooling belong to plugins.
|
|
6
|
+
*/
|
|
7
|
+
const isObject = (object) => object?.isObject3D === true;
|
|
8
|
+
const fail = (message) => {
|
|
9
|
+
throw new TypeError(message);
|
|
10
|
+
};
|
|
11
|
+
function actionOf(value) {
|
|
12
|
+
if (typeof value === "function") return {
|
|
13
|
+
execute: value,
|
|
14
|
+
description: "",
|
|
15
|
+
available: null
|
|
16
|
+
};
|
|
17
|
+
if (!value || typeof value.execute !== "function") fail("Action requires a function or { execute }");
|
|
18
|
+
if (value.available != null && typeof value.available !== "function") fail("Action available must be a function");
|
|
19
|
+
return {
|
|
20
|
+
execute: value.execute,
|
|
21
|
+
description: value.description || "",
|
|
22
|
+
available: value.available || null
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function parseActions(actions) {
|
|
26
|
+
if (actions == null) return /* @__PURE__ */ new Map();
|
|
27
|
+
if (typeof actions !== "object" || Array.isArray(actions)) fail("Actions must be a name-to-handler object");
|
|
28
|
+
const result = /* @__PURE__ */ new Map();
|
|
29
|
+
for (const [name, action] of Object.entries(actions)) {
|
|
30
|
+
if (!name.trim()) fail("Action name must be non-empty");
|
|
31
|
+
result.set(name, actionOf(action));
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
function inside(root, object) {
|
|
36
|
+
for (let current = object; current; current = current.parent) if (current === root) return true;
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Attach semantic metadata and callable actions to an existing THREE.Scene.
|
|
41
|
+
* Native Three.js behavior and ownership remain entirely with the caller.
|
|
42
|
+
*
|
|
43
|
+
* @param {import('./core.d.ts').SemanticScene} scene
|
|
44
|
+
* @returns {import('./core.d.ts').SemanticWorld}
|
|
45
|
+
*/
|
|
46
|
+
function attach(scene) {
|
|
47
|
+
if (!isObject(scene) || scene.isScene !== true) fail("attach(scene) requires a THREE.Scene");
|
|
48
|
+
const entries = /* @__PURE__ */ new Map();
|
|
49
|
+
const objects = /* @__PURE__ */ new WeakMap();
|
|
50
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
51
|
+
const plugins = [];
|
|
52
|
+
let disposed = false;
|
|
53
|
+
const ensureOpen = () => {
|
|
54
|
+
if (disposed) throw new Error("SEMANTIC_WORLD_DISPOSED");
|
|
55
|
+
};
|
|
56
|
+
let world;
|
|
57
|
+
const find = (value) => typeof value === "string" ? entries.get(value) : objects.get(value);
|
|
58
|
+
const emit = (type, id) => {
|
|
59
|
+
for (const listener of [...listeners]) try {
|
|
60
|
+
listener({
|
|
61
|
+
type,
|
|
62
|
+
id,
|
|
63
|
+
world
|
|
64
|
+
});
|
|
65
|
+
} catch {}
|
|
66
|
+
};
|
|
67
|
+
const describe = (entry) => ({
|
|
68
|
+
id: entry.id,
|
|
69
|
+
name: entry.object.name || "",
|
|
70
|
+
type: entry.type,
|
|
71
|
+
label: entry.label,
|
|
72
|
+
description: entry.description,
|
|
73
|
+
properties: entry.properties,
|
|
74
|
+
attached: inside(scene, entry.object),
|
|
75
|
+
state: entry.read ? entry.read() : {},
|
|
76
|
+
actions: [...entry.actions].map(([name, action]) => ({
|
|
77
|
+
name,
|
|
78
|
+
description: action.description,
|
|
79
|
+
available: action.available ? Boolean(action.available({
|
|
80
|
+
object: entry.object,
|
|
81
|
+
world
|
|
82
|
+
})) : true
|
|
83
|
+
}))
|
|
84
|
+
});
|
|
85
|
+
function unregister(target) {
|
|
86
|
+
ensureOpen();
|
|
87
|
+
const entry = find(target);
|
|
88
|
+
if (!entry) return false;
|
|
89
|
+
entries.delete(entry.id);
|
|
90
|
+
objects.delete(entry.object);
|
|
91
|
+
emit("entity.unregistered", entry.id);
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
function register(object, definition = {}) {
|
|
95
|
+
ensureOpen();
|
|
96
|
+
if (!isObject(object)) fail("register(object) requires a THREE.Object3D");
|
|
97
|
+
if (!definition || typeof definition !== "object" || Array.isArray(definition)) fail("Invalid semantic definition");
|
|
98
|
+
const id = definition.id ?? object.uuid;
|
|
99
|
+
if (typeof id !== "string" || !id.trim()) fail("Entity id must be a non-empty string");
|
|
100
|
+
if (entries.has(id) || objects.has(object)) fail("SEMANTIC_ENTITY_CONFLICT");
|
|
101
|
+
if (definition.read != null && typeof definition.read !== "function") fail("Entity read must be a function");
|
|
102
|
+
const actions = parseActions(definition.actions);
|
|
103
|
+
const entry = {
|
|
104
|
+
id,
|
|
105
|
+
object,
|
|
106
|
+
type: definition.type || "",
|
|
107
|
+
label: definition.label || object.name || id,
|
|
108
|
+
description: definition.description || "",
|
|
109
|
+
properties: definition.properties ?? {},
|
|
110
|
+
read: definition.read || null,
|
|
111
|
+
actions
|
|
112
|
+
};
|
|
113
|
+
entries.set(id, entry);
|
|
114
|
+
objects.set(object, entry);
|
|
115
|
+
emit("entity.registered", id);
|
|
116
|
+
return () => entries.get(id) === entry && unregister(id);
|
|
117
|
+
}
|
|
118
|
+
function registerAction(target, name, value) {
|
|
119
|
+
ensureOpen();
|
|
120
|
+
const entry = find(target);
|
|
121
|
+
if (!entry) fail("SEMANTIC_ENTITY_NOT_FOUND");
|
|
122
|
+
if (typeof name !== "string" || !name.trim()) fail("Action name must be non-empty");
|
|
123
|
+
if (entry.actions.has(name)) fail("SEMANTIC_ACTION_CONFLICT");
|
|
124
|
+
const action = actionOf(value);
|
|
125
|
+
entry.actions.set(name, action);
|
|
126
|
+
emit("action.registered", entry.id);
|
|
127
|
+
return () => {
|
|
128
|
+
if (entries.get(entry.id) !== entry || entry.actions.get(name) !== action) return false;
|
|
129
|
+
entry.actions.delete(name);
|
|
130
|
+
emit("action.unregistered", entry.id);
|
|
131
|
+
return true;
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
async function invoke(target, name, args = {}, options = {}) {
|
|
135
|
+
ensureOpen();
|
|
136
|
+
const entry = find(target);
|
|
137
|
+
if (!entry) fail("SEMANTIC_ENTITY_NOT_FOUND");
|
|
138
|
+
const action = entry.actions.get(name);
|
|
139
|
+
if (!action) fail("SEMANTIC_ACTION_NOT_FOUND");
|
|
140
|
+
const context = {
|
|
141
|
+
object: entry.object,
|
|
142
|
+
world,
|
|
143
|
+
signal: options.signal
|
|
144
|
+
};
|
|
145
|
+
if (action.available && !action.available(context)) fail("SEMANTIC_ACTION_UNAVAILABLE");
|
|
146
|
+
const value = await action.execute(args, context);
|
|
147
|
+
emit("action.invoked", entry.id);
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
function uninstall(pluginEntry) {
|
|
151
|
+
if (!pluginEntry.active) return false;
|
|
152
|
+
pluginEntry.active = false;
|
|
153
|
+
plugins.splice(plugins.indexOf(pluginEntry), 1);
|
|
154
|
+
pluginEntry.teardown?.();
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
world = {
|
|
158
|
+
scene,
|
|
159
|
+
register,
|
|
160
|
+
unregister,
|
|
161
|
+
registerAction,
|
|
162
|
+
getObject(target) {
|
|
163
|
+
ensureOpen();
|
|
164
|
+
return find(target)?.object || null;
|
|
165
|
+
},
|
|
166
|
+
inspect(target) {
|
|
167
|
+
ensureOpen();
|
|
168
|
+
const entry = find(target);
|
|
169
|
+
return entry ? describe(entry) : null;
|
|
170
|
+
},
|
|
171
|
+
list({ attachedOnly = false } = {}) {
|
|
172
|
+
ensureOpen();
|
|
173
|
+
return [...entries.values()].filter((entry) => !attachedOnly || inside(scene, entry.object)).map(describe);
|
|
174
|
+
},
|
|
175
|
+
invoke,
|
|
176
|
+
subscribe(listener) {
|
|
177
|
+
ensureOpen();
|
|
178
|
+
if (typeof listener !== "function") fail("Listener must be a function");
|
|
179
|
+
listeners.add(listener);
|
|
180
|
+
return () => listeners.delete(listener);
|
|
181
|
+
},
|
|
182
|
+
use(plugin, options) {
|
|
183
|
+
ensureOpen();
|
|
184
|
+
const install = typeof plugin === "function" ? plugin : plugin?.install?.bind(plugin);
|
|
185
|
+
if (typeof install !== "function") fail("Plugin requires an install(world, options) function");
|
|
186
|
+
const result = install(world, options);
|
|
187
|
+
const entry = {
|
|
188
|
+
active: true,
|
|
189
|
+
teardown: typeof result === "function" ? result : result?.dispose?.bind(result)
|
|
190
|
+
};
|
|
191
|
+
plugins.push(entry);
|
|
192
|
+
return () => uninstall(entry);
|
|
193
|
+
},
|
|
194
|
+
dispose() {
|
|
195
|
+
if (disposed) return;
|
|
196
|
+
for (const entry of [...plugins].reverse()) try {
|
|
197
|
+
uninstall(entry);
|
|
198
|
+
} catch {}
|
|
199
|
+
for (const id of [...entries.keys()]) unregister(id);
|
|
200
|
+
disposed = true;
|
|
201
|
+
emit("disposed", null);
|
|
202
|
+
listeners.clear();
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
return world;
|
|
206
|
+
}
|
|
207
|
+
export { attach };
|
|
208
|
+
|
|
209
|
+
//# sourceMappingURL=core.js.map
|
package/dist/core.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.js","names":[],"sources":["../src/core.js"],"sourcesContent":["/**\n * A framework-agnostic semantic extension for a user-owned Three.js scene.\n * It never creates a renderer, starts a loop, rewrites Object3D or executes code.\n * Policies, transports, persistence, physics and agent tooling belong to plugins.\n */\nconst isObject = object => object?.isObject3D === true;\nconst fail = message => { throw new TypeError(message); };\n\nfunction actionOf(value) {\n if (typeof value === 'function') return { execute: value, description: '', available: null };\n if (!value || typeof value.execute !== 'function') fail('Action requires a function or { execute }');\n if (value.available != null && typeof value.available !== 'function') fail('Action available must be a function');\n return { execute: value.execute, description: value.description || '', available: value.available || null };\n}\n\nfunction parseActions(actions) {\n if (actions == null) return new Map();\n if (typeof actions !== 'object' || Array.isArray(actions)) fail('Actions must be a name-to-handler object');\n const result = new Map();\n for (const [name, action] of Object.entries(actions)) {\n if (!name.trim()) fail('Action name must be non-empty');\n result.set(name, actionOf(action));\n }\n return result;\n}\n\nfunction inside(root, object) {\n for (let current = object; current; current = current.parent) {\n if (current === root) return true;\n }\n return false;\n}\n\n/**\n * Attach semantic metadata and callable actions to an existing THREE.Scene.\n * Native Three.js behavior and ownership remain entirely with the caller.\n *\n * @param {import('./core.d.ts').SemanticScene} scene\n * @returns {import('./core.d.ts').SemanticWorld}\n */\nexport function attach(scene) {\n if (!isObject(scene) || scene.isScene !== true) fail('attach(scene) requires a THREE.Scene');\n const entries = new Map();\n const objects = new WeakMap();\n const listeners = new Set();\n const plugins = [];\n let disposed = false;\n const ensureOpen = () => { if (disposed) throw new Error('SEMANTIC_WORLD_DISPOSED'); };\n let world;\n\n const find = value => typeof value === 'string' ? entries.get(value) : objects.get(value);\n const emit = (type, id) => {\n for (const listener of [...listeners]) {\n try { listener({ type, id, world }); }\n catch { /* A plugin must not break its host scene. */ }\n }\n };\n const describe = entry => ({\n id: entry.id,\n name: entry.object.name || '',\n type: entry.type,\n label: entry.label,\n description: entry.description,\n properties: entry.properties,\n attached: inside(scene, entry.object),\n state: entry.read ? entry.read() : {},\n actions: [...entry.actions].map(([name, action]) => ({\n name,\n description: action.description,\n available: action.available ? Boolean(action.available({ object: entry.object, world })) : true\n }))\n });\n\n function unregister(target) {\n ensureOpen();\n const entry = find(target);\n if (!entry) return false;\n entries.delete(entry.id);\n objects.delete(entry.object);\n emit('entity.unregistered', entry.id);\n return true;\n }\n\n function register(object, definition = {}) {\n ensureOpen();\n if (!isObject(object)) fail('register(object) requires a THREE.Object3D');\n if (!definition || typeof definition !== 'object' || Array.isArray(definition)) fail('Invalid semantic definition');\n const id = definition.id ?? object.uuid;\n if (typeof id !== 'string' || !id.trim()) fail('Entity id must be a non-empty string');\n if (entries.has(id) || objects.has(object)) fail('SEMANTIC_ENTITY_CONFLICT');\n if (definition.read != null && typeof definition.read !== 'function') fail('Entity read must be a function');\n const actions = parseActions(definition.actions);\n const entry = {\n id, object,\n type: definition.type || '',\n label: definition.label || object.name || id,\n description: definition.description || '',\n properties: definition.properties ?? {},\n read: definition.read || null,\n actions\n };\n entries.set(id, entry);\n objects.set(object, entry);\n emit('entity.registered', id);\n return () => entries.get(id) === entry && unregister(id);\n }\n\n function registerAction(target, name, value) {\n ensureOpen();\n const entry = find(target);\n if (!entry) fail('SEMANTIC_ENTITY_NOT_FOUND');\n if (typeof name !== 'string' || !name.trim()) fail('Action name must be non-empty');\n if (entry.actions.has(name)) fail('SEMANTIC_ACTION_CONFLICT');\n const action = actionOf(value);\n entry.actions.set(name, action);\n emit('action.registered', entry.id);\n return () => {\n if (entries.get(entry.id) !== entry || entry.actions.get(name) !== action) return false;\n entry.actions.delete(name);\n emit('action.unregistered', entry.id);\n return true;\n };\n }\n\n async function invoke(target, name, args = {}, options = {}) {\n ensureOpen();\n const entry = find(target);\n if (!entry) fail('SEMANTIC_ENTITY_NOT_FOUND');\n const action = entry.actions.get(name);\n if (!action) fail('SEMANTIC_ACTION_NOT_FOUND');\n const context = { object: entry.object, world, signal: options.signal };\n if (action.available && !action.available(context)) fail('SEMANTIC_ACTION_UNAVAILABLE');\n const value = await action.execute(args, context);\n emit('action.invoked', entry.id);\n return value;\n }\n\n function uninstall(pluginEntry) {\n if (!pluginEntry.active) return false;\n pluginEntry.active = false;\n plugins.splice(plugins.indexOf(pluginEntry), 1);\n pluginEntry.teardown?.();\n return true;\n }\n\n world = {\n scene,\n register,\n unregister,\n registerAction,\n getObject(target) {\n ensureOpen();\n return find(target)?.object || null;\n },\n inspect(target) {\n ensureOpen();\n const entry = find(target);\n return entry ? describe(entry) : null;\n },\n list({ attachedOnly = false } = {}) {\n ensureOpen();\n return [...entries.values()].filter(entry => !attachedOnly || inside(scene, entry.object)).map(describe);\n },\n invoke,\n subscribe(listener) {\n ensureOpen();\n if (typeof listener !== 'function') fail('Listener must be a function');\n listeners.add(listener);\n return () => listeners.delete(listener);\n },\n use(plugin, options) {\n ensureOpen();\n const install = typeof plugin === 'function' ? plugin : plugin?.install?.bind(plugin);\n if (typeof install !== 'function') fail('Plugin requires an install(world, options) function');\n const result = install(world, options);\n const teardown = typeof result === 'function' ? result : result?.dispose?.bind(result);\n const entry = { active: true, teardown };\n plugins.push(entry);\n return () => uninstall(entry);\n },\n dispose() {\n if (disposed) return;\n for (const entry of [...plugins].reverse()) {\n try { uninstall(entry); } catch { /* Host scene disposal must remain reliable. */ }\n }\n for (const id of [...entries.keys()]) unregister(id);\n disposed = true;\n emit('disposed', null);\n listeners.clear();\n }\n };\n return world;\n}\n"],"mappings":";;;;;;AAKA,MAAM,YAAW,WAAU,QAAQ,eAAe;AAClD,MAAM,QAAO,YAAW;CAAE,MAAM,IAAI,UAAU,OAAO;AAAG;AAExD,SAAS,SAAS,OAA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AM,SAAS,CAAC,GAAG,OAAO,CAAC,CAAC,QAAQ,GACvC,IAAI;IAAE,UAAU,KAAK;GAAG,QAAQ,CAAkD;GAEpF,KAAK,MAAM,MAAM,CAAC,GAAG,QAAQ,KAAK,CAAC,GAAG,WAAW,EAAE;GACnD,WAAW;GACX,KAAK,YAAY,IAAI;GACrB,UAAU,MAAM;EAClB;CACF;CACA,OAAO;AACT"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { attach } from './core.js';
|
|
2
|
+
export type {
|
|
3
|
+
SemanticWorld, SemanticScene, SemanticObject3D, SemanticTarget,
|
|
4
|
+
SemanticAction, SemanticActionFn, SemanticActionContext, SemanticDefinition,
|
|
5
|
+
SemanticEntity, SemanticEvent, SemanticPlugin
|
|
6
|
+
} from './core.js';
|
|
7
|
+
export declare const AgentScape: {
|
|
8
|
+
readonly attach: typeof import('./core.js').attach;
|
|
9
|
+
readonly extend: typeof import('./core.js').attach;
|
|
10
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// src/core.js
|
|
2
|
+
/**
|
|
3
|
+
* A framework-agnostic semantic extension for a user-owned Three.js scene.
|
|
4
|
+
* It never creates a renderer, starts a loop, rewrites Object3D or executes code.
|
|
5
|
+
* Policies, transports, persistence, physics and agent tooling belong to plugins.
|
|
6
|
+
*/
|
|
7
|
+
const isObject = (object) => object?.isObject3D === true;
|
|
8
|
+
const fail = (message) => {
|
|
9
|
+
throw new TypeError(message);
|
|
10
|
+
};
|
|
11
|
+
function actionOf(value) {
|
|
12
|
+
if (typeof value === "function") return {
|
|
13
|
+
execute: value,
|
|
14
|
+
description: "",
|
|
15
|
+
available: null
|
|
16
|
+
};
|
|
17
|
+
if (!value || typeof value.execute !== "function") fail("Action requires a function or { execute }");
|
|
18
|
+
if (value.available != null && typeof value.available !== "function") fail("Action available must be a function");
|
|
19
|
+
return {
|
|
20
|
+
execute: value.execute,
|
|
21
|
+
description: value.description || "",
|
|
22
|
+
available: value.available || null
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function parseActions(actions) {
|
|
26
|
+
if (actions == null) return /* @__PURE__ */ new Map();
|
|
27
|
+
if (typeof actions !== "object" || Array.isArray(actions)) fail("Actions must be a name-to-handler object");
|
|
28
|
+
const result = /* @__PURE__ */ new Map();
|
|
29
|
+
for (const [name, action] of Object.entries(actions)) {
|
|
30
|
+
if (!name.trim()) fail("Action name must be non-empty");
|
|
31
|
+
result.set(name, actionOf(action));
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
function inside(root, object) {
|
|
36
|
+
for (let current = object; current; current = current.parent) if (current === root) return true;
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Attach semantic metadata and callable actions to an existing THREE.Scene.
|
|
41
|
+
* Native Three.js behavior and ownership remain entirely with the caller.
|
|
42
|
+
*
|
|
43
|
+
* @param {import('./core.d.ts').SemanticScene} scene
|
|
44
|
+
* @returns {import('./core.d.ts').SemanticWorld}
|
|
45
|
+
*/
|
|
46
|
+
function attach(scene) {
|
|
47
|
+
if (!isObject(scene) || scene.isScene !== true) fail("attach(scene) requires a THREE.Scene");
|
|
48
|
+
const entries = /* @__PURE__ */ new Map();
|
|
49
|
+
const objects = /* @__PURE__ */ new WeakMap();
|
|
50
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
51
|
+
const plugins = [];
|
|
52
|
+
let disposed = false;
|
|
53
|
+
const ensureOpen = () => {
|
|
54
|
+
if (disposed) throw new Error("SEMANTIC_WORLD_DISPOSED");
|
|
55
|
+
};
|
|
56
|
+
let world;
|
|
57
|
+
const find = (value) => typeof value === "string" ? entries.get(value) : objects.get(value);
|
|
58
|
+
const emit = (type, id) => {
|
|
59
|
+
for (const listener of [...listeners]) try {
|
|
60
|
+
listener({
|
|
61
|
+
type,
|
|
62
|
+
id,
|
|
63
|
+
world
|
|
64
|
+
});
|
|
65
|
+
} catch {}
|
|
66
|
+
};
|
|
67
|
+
const describe = (entry) => ({
|
|
68
|
+
id: entry.id,
|
|
69
|
+
name: entry.object.name || "",
|
|
70
|
+
type: entry.type,
|
|
71
|
+
label: entry.label,
|
|
72
|
+
description: entry.description,
|
|
73
|
+
properties: entry.properties,
|
|
74
|
+
attached: inside(scene, entry.object),
|
|
75
|
+
state: entry.read ? entry.read() : {},
|
|
76
|
+
actions: [...entry.actions].map(([name, action]) => ({
|
|
77
|
+
name,
|
|
78
|
+
description: action.description,
|
|
79
|
+
available: action.available ? Boolean(action.available({
|
|
80
|
+
object: entry.object,
|
|
81
|
+
world
|
|
82
|
+
})) : true
|
|
83
|
+
}))
|
|
84
|
+
});
|
|
85
|
+
function unregister(target) {
|
|
86
|
+
ensureOpen();
|
|
87
|
+
const entry = find(target);
|
|
88
|
+
if (!entry) return false;
|
|
89
|
+
entries.delete(entry.id);
|
|
90
|
+
objects.delete(entry.object);
|
|
91
|
+
emit("entity.unregistered", entry.id);
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
function register(object, definition = {}) {
|
|
95
|
+
ensureOpen();
|
|
96
|
+
if (!isObject(object)) fail("register(object) requires a THREE.Object3D");
|
|
97
|
+
if (!definition || typeof definition !== "object" || Array.isArray(definition)) fail("Invalid semantic definition");
|
|
98
|
+
const id = definition.id ?? object.uuid;
|
|
99
|
+
if (typeof id !== "string" || !id.trim()) fail("Entity id must be a non-empty string");
|
|
100
|
+
if (entries.has(id) || objects.has(object)) fail("SEMANTIC_ENTITY_CONFLICT");
|
|
101
|
+
if (definition.read != null && typeof definition.read !== "function") fail("Entity read must be a function");
|
|
102
|
+
const actions = parseActions(definition.actions);
|
|
103
|
+
const entry = {
|
|
104
|
+
id,
|
|
105
|
+
object,
|
|
106
|
+
type: definition.type || "",
|
|
107
|
+
label: definition.label || object.name || id,
|
|
108
|
+
description: definition.description || "",
|
|
109
|
+
properties: definition.properties ?? {},
|
|
110
|
+
read: definition.read || null,
|
|
111
|
+
actions
|
|
112
|
+
};
|
|
113
|
+
entries.set(id, entry);
|
|
114
|
+
objects.set(object, entry);
|
|
115
|
+
emit("entity.registered", id);
|
|
116
|
+
return () => entries.get(id) === entry && unregister(id);
|
|
117
|
+
}
|
|
118
|
+
function registerAction(target, name, value) {
|
|
119
|
+
ensureOpen();
|
|
120
|
+
const entry = find(target);
|
|
121
|
+
if (!entry) fail("SEMANTIC_ENTITY_NOT_FOUND");
|
|
122
|
+
if (typeof name !== "string" || !name.trim()) fail("Action name must be non-empty");
|
|
123
|
+
if (entry.actions.has(name)) fail("SEMANTIC_ACTION_CONFLICT");
|
|
124
|
+
const action = actionOf(value);
|
|
125
|
+
entry.actions.set(name, action);
|
|
126
|
+
emit("action.registered", entry.id);
|
|
127
|
+
return () => {
|
|
128
|
+
if (entries.get(entry.id) !== entry || entry.actions.get(name) !== action) return false;
|
|
129
|
+
entry.actions.delete(name);
|
|
130
|
+
emit("action.unregistered", entry.id);
|
|
131
|
+
return true;
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
async function invoke(target, name, args = {}, options = {}) {
|
|
135
|
+
ensureOpen();
|
|
136
|
+
const entry = find(target);
|
|
137
|
+
if (!entry) fail("SEMANTIC_ENTITY_NOT_FOUND");
|
|
138
|
+
const action = entry.actions.get(name);
|
|
139
|
+
if (!action) fail("SEMANTIC_ACTION_NOT_FOUND");
|
|
140
|
+
const context = {
|
|
141
|
+
object: entry.object,
|
|
142
|
+
world,
|
|
143
|
+
signal: options.signal
|
|
144
|
+
};
|
|
145
|
+
if (action.available && !action.available(context)) fail("SEMANTIC_ACTION_UNAVAILABLE");
|
|
146
|
+
const value = await action.execute(args, context);
|
|
147
|
+
emit("action.invoked", entry.id);
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
function uninstall(pluginEntry) {
|
|
151
|
+
if (!pluginEntry.active) return false;
|
|
152
|
+
pluginEntry.active = false;
|
|
153
|
+
plugins.splice(plugins.indexOf(pluginEntry), 1);
|
|
154
|
+
pluginEntry.teardown?.();
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
world = {
|
|
158
|
+
scene,
|
|
159
|
+
register,
|
|
160
|
+
unregister,
|
|
161
|
+
registerAction,
|
|
162
|
+
getObject(target) {
|
|
163
|
+
ensureOpen();
|
|
164
|
+
return find(target)?.object || null;
|
|
165
|
+
},
|
|
166
|
+
inspect(target) {
|
|
167
|
+
ensureOpen();
|
|
168
|
+
const entry = find(target);
|
|
169
|
+
return entry ? describe(entry) : null;
|
|
170
|
+
},
|
|
171
|
+
list({ attachedOnly = false } = {}) {
|
|
172
|
+
ensureOpen();
|
|
173
|
+
return [...entries.values()].filter((entry) => !attachedOnly || inside(scene, entry.object)).map(describe);
|
|
174
|
+
},
|
|
175
|
+
invoke,
|
|
176
|
+
subscribe(listener) {
|
|
177
|
+
ensureOpen();
|
|
178
|
+
if (typeof listener !== "function") fail("Listener must be a function");
|
|
179
|
+
listeners.add(listener);
|
|
180
|
+
return () => listeners.delete(listener);
|
|
181
|
+
},
|
|
182
|
+
use(plugin, options) {
|
|
183
|
+
ensureOpen();
|
|
184
|
+
const install = typeof plugin === "function" ? plugin : plugin?.install?.bind(plugin);
|
|
185
|
+
if (typeof install !== "function") fail("Plugin requires an install(world, options) function");
|
|
186
|
+
const result = install(world, options);
|
|
187
|
+
const entry = {
|
|
188
|
+
active: true,
|
|
189
|
+
teardown: typeof result === "function" ? result : result?.dispose?.bind(result)
|
|
190
|
+
};
|
|
191
|
+
plugins.push(entry);
|
|
192
|
+
return () => uninstall(entry);
|
|
193
|
+
},
|
|
194
|
+
dispose() {
|
|
195
|
+
if (disposed) return;
|
|
196
|
+
for (const entry of [...plugins].reverse()) try {
|
|
197
|
+
uninstall(entry);
|
|
198
|
+
} catch {}
|
|
199
|
+
for (const id of [...entries.keys()]) unregister(id);
|
|
200
|
+
disposed = true;
|
|
201
|
+
emit("disposed", null);
|
|
202
|
+
listeners.clear();
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
return world;
|
|
206
|
+
}
|
|
207
|
+
// src/index.ts
|
|
208
|
+
/** The default package only extends a host-owned Three.js scene. */
|
|
209
|
+
const AgentScape = Object.freeze({
|
|
210
|
+
attach,
|
|
211
|
+
extend: attach
|
|
212
|
+
});
|
|
213
|
+
export { AgentScape, attach };
|
|
214
|
+
|
|
215
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/core.js","../src/index.ts"],"sourcesContent":["/**\n * A framework-agnostic semantic extension for a user-owned Three.js scene.\n * It never creates a renderer, starts a loop, rewrites Object3D or executes code.\n * Policies, transports, persistence, physics and agent tooling belong to plugins.\n */\nconst isObject = object => object?.isObject3D === true;\nconst fail = message => { throw new TypeError(message); };\n\nfunction actionOf(value) {\n if (typeof value === 'function') return { execute: value, description: '', available: null };\n if (!value || typeof value.execute !== 'function') fail('Action requires a function or { execute }');\n if (value.available != null && typeof value.available !== 'function') fail('Action available must be a function');\n return { execute: value.execute, description: value.description || '', available: value.available || null };\n}\n\nfunction parseActions(actions) {\n if (actions == null) return new Map();\n if (typeof actions !== 'object' || Array.isArray(actions)) fail('Actions must be a name-to-handler object');\n const result = new Map();\n for (const [name, action] of Object.entries(actions)) {\n if (!name.trim()) fail('Action name must be non-empty');\n result.set(name, actionOf(action));\n }\n return result;\n}\n\nfunction inside(root, object) {\n for (let current = object; current; current = current.parent) {\n if (current === root) return true;\n }\n return false;\n}\n\n/**\n * Attach semantic metadata and callable actions to an existing THREE.Scene.\n * Native Three.js behavior and ownership remain entirely with the caller.\n *\n * @param {import('./core.d.ts').SemanticScene} scene\n * @returns {import('./core.d.ts').SemanticWorld}\n */\nexport function attach(scene) {\n if (!isObject(scene) || scene.isScene !== true) fail('attach(scene) requires a THREE.Scene');\n const entries = new Map();\n const objects = new WeakMap();\n const listeners = new Set();\n const plugins = [];\n let disposed = false;\n const ensureOpen = () => { if (disposed) throw new Error('SEMANTIC_WORLD_DISPOSED'); };\n let world;\n\n const find = value => typeof value === 'string' ? entries.get(value) : objects.get(value);\n const emit = (type, id) => {\n for (const listener of [...listeners]) {\n try { listener({ type, id, world }); }\n catch { /* A plugin must not break its host scene. */ }\n }\n };\n const describe = entry => ({\n id: entry.id,\n name: entry.object.name || '',\n type: entry.type,\n label: entry.label,\n description: entry.description,\n properties: entry.properties,\n attached: inside(scene, entry.object),\n state: entry.read ? entry.read() : {},\n actions: [...entry.actions].map(([name, action]) => ({\n name,\n description: action.description,\n available: action.available ? Boolean(action.available({ object: entry.object, world })) : true\n }))\n });\n\n function unregister(target) {\n ensureOpen();\n const entry = find(target);\n if (!entry) return false;\n entries.delete(entry.id);\n objects.delete(entry.object);\n emit('entity.unregistered', entry.id);\n return true;\n }\n\n function register(object, definition = {}) {\n ensureOpen();\n if (!isObject(object)) fail('register(object) requires a THREE.Object3D');\n if (!definition || typeof definition !== 'object' || Array.isArray(definition)) fail('Invalid semantic definition');\n const id = definition.id ?? object.uuid;\n if (typeof id !== 'string' || !id.trim()) fail('Entity id must be a non-empty string');\n if (entries.has(id) || objects.has(object)) fail('SEMANTIC_ENTITY_CONFLICT');\n if (definition.read != null && typeof definition.read !== 'function') fail('Entity read must be a function');\n const actions = parseActions(definition.actions);\n const entry = {\n id, object,\n type: definition.type || '',\n label: definition.label || object.name || id,\n description: definition.description || '',\n properties: definition.properties ?? {},\n read: definition.read || null,\n actions\n };\n entries.set(id, entry);\n objects.set(object, entry);\n emit('entity.registered', id);\n return () => entries.get(id) === entry && unregister(id);\n }\n\n function registerAction(target, name, value) {\n ensureOpen();\n const entry = find(target);\n if (!entry) fail('SEMANTIC_ENTITY_NOT_FOUND');\n if (typeof name !== 'string' || !name.trim()) fail('Action name must be non-empty');\n if (entry.actions.has(name)) fail('SEMANTIC_ACTION_CONFLICT');\n const action = actionOf(value);\n entry.actions.set(name, action);\n emit('action.registered', entry.id);\n return () => {\n if (entries.get(entry.id) !== entry || entry.actions.get(name) !== action) return false;\n entry.actions.delete(name);\n emit('action.unregistered', entry.id);\n return true;\n };\n }\n\n async function invoke(target, name, args = {}, options = {}) {\n ensureOpen();\n const entry = find(target);\n if (!entry) fail('SEMANTIC_ENTITY_NOT_FOUND');\n const action = entry.actions.get(name);\n if (!action) fail('SEMANTIC_ACTION_NOT_FOUND');\n const context = { object: entry.object, world, signal: options.signal };\n if (action.available && !action.available(context)) fail('SEMANTIC_ACTION_UNAVAILABLE');\n const value = await action.execute(args, context);\n emit('action.invoked', entry.id);\n return value;\n }\n\n function uninstall(pluginEntry) {\n if (!pluginEntry.active) return false;\n pluginEntry.active = false;\n plugins.splice(plugins.indexOf(pluginEntry), 1);\n pluginEntry.teardown?.();\n return true;\n }\n\n world = {\n scene,\n register,\n unregister,\n registerAction,\n getObject(target) {\n ensureOpen();\n return find(target)?.object || null;\n },\n inspect(target) {\n ensureOpen();\n const entry = find(target);\n return entry ? describe(entry) : null;\n },\n list({ attachedOnly = false } = {}) {\n ensureOpen();\n return [...entries.values()].filter(entry => !attachedOnly || inside(scene, entry.object)).map(describe);\n },\n invoke,\n subscribe(listener) {\n ensureOpen();\n if (typeof listener !== 'function') fail('Listener must be a function');\n listeners.add(listener);\n return () => listeners.delete(listener);\n },\n use(plugin, options) {\n ensureOpen();\n const install = typeof plugin === 'function' ? plugin : plugin?.install?.bind(plugin);\n if (typeof install !== 'function') fail('Plugin requires an install(world, options) function');\n const result = install(world, options);\n const teardown = typeof result === 'function' ? result : result?.dispose?.bind(result);\n const entry = { active: true, teardown };\n plugins.push(entry);\n return () => uninstall(entry);\n },\n dispose() {\n if (disposed) return;\n for (const entry of [...plugins].reverse()) {\n try { uninstall(entry); } catch { /* Host scene disposal must remain reliable. */ }\n }\n for (const id of [...entries.keys()]) unregister(id);\n disposed = true;\n emit('disposed', null);\n listeners.clear();\n }\n };\n return world;\n}\n","import { attach } from './core.js';\nexport { attach } from './core.js';\n\n/** The default package only extends a host-owned Three.js scene. */\nexport const AgentSc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,UADb,OAAO,WAAW,aAAa,SAAS,QAAQ,SAAS,KAAK,MAAM;GAC9C;GACvC,QAAQ,KAAK,KAAK;GAClB,aAAa,UAAU,KAAK;EAC9B;EACA,UAAU;GACR,IAAI,UAAU;GACd,KAAK,MAAM,SAAS,CAAC,GAAG,OAAO,CAAC,CAAC,QAAQ,GACvC,IAAI;IAAE,UAAU,KAAK;GAAG,QAAQ,CAAkD;GAEpF,KAAK,MAAM,MAAM,CAAC,GAAG,QAAQ,KAAK,CAAC,GAAG,WAAW,EAAE;GACnD,WAAW;GACX,KAAK,YAAY,IAAI;GACrB,UAAU,MAAM;EAClB;CACF;CACA,OAAO;AACT;;;AC5LA,MAAa,aAAa,OAAO,OAAO;CAAE;CAAQ,QAAQ;AAAO,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agentscape3d",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "A lightweight, open semantic and action extension for Three.js scenes",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./core": {
|
|
16
|
+
"types": "./dist/core.d.ts",
|
|
17
|
+
"import": "./dist/core.js",
|
|
18
|
+
"default": "./dist/core.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22.12"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "node ../../dev/build-sdk.mjs --core",
|
|
31
|
+
"prepack": "npm run build"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"three": "^0.187.0"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"registry": "https://registry.npmjs.org/",
|
|
39
|
+
"tag": "beta"
|
|
40
|
+
}
|
|
41
|
+
}
|