@palettelab/sdk 0.1.15 → 0.1.17
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 +36 -0
- package/dist/components/index.d.mts +1 -1
- package/dist/components/index.d.ts +1 -1
- package/dist/hooks/index.d.mts +2 -2
- package/dist/hooks/index.d.ts +2 -2
- package/dist/{index-BDVWt7DE.d.ts → index-D0i35Hem.d.ts} +1 -1
- package/dist/{index-Bu5EQGYo.d.mts → index-DsfTFnZb.d.mts} +1 -1
- package/dist/index.d.mts +36 -4
- package/dist/index.d.ts +36 -4
- package/dist/index.js +291 -27
- package/dist/index.mjs +288 -26
- package/dist/{plugin-UV46q1mU.d.mts → plugin-dpLzOtF6.d.mts} +47 -2
- package/dist/{plugin-UV46q1mU.d.ts → plugin-dpLzOtF6.d.ts} +47 -2
- package/dist/router/index.d.mts +346 -0
- package/dist/router/index.d.ts +346 -0
- package/dist/router/index.js +247 -0
- package/dist/router/index.mjs +225 -0
- package/dist/types/index.d.mts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/package.json +6 -1
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// src/router.tsx
|
|
2
|
+
import {
|
|
3
|
+
Component,
|
|
4
|
+
createContext as createContext2,
|
|
5
|
+
createElement,
|
|
6
|
+
useCallback,
|
|
7
|
+
useContext as useContext2,
|
|
8
|
+
useEffect,
|
|
9
|
+
useMemo,
|
|
10
|
+
useState
|
|
11
|
+
} from "react";
|
|
12
|
+
|
|
13
|
+
// src/hooks/use-platform.ts
|
|
14
|
+
import { createContext, useContext } from "react";
|
|
15
|
+
var PlatformCtx = createContext(null);
|
|
16
|
+
function usePlatform() {
|
|
17
|
+
const ctx = useContext(PlatformCtx);
|
|
18
|
+
if (!ctx) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
"usePlatform must be used within a PlatformProvider. This is provided automatically when your plugin runs on the Palette platform."
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
return ctx;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// src/router.tsx
|
|
27
|
+
var RouterCtx = createContext2(null);
|
|
28
|
+
var NOT_FOUND = /* @__PURE__ */ Symbol.for("palette.router.not-found");
|
|
29
|
+
function notFound() {
|
|
30
|
+
throw Object.assign(new Error("Palette route not found"), { code: NOT_FOUND });
|
|
31
|
+
}
|
|
32
|
+
function normalizePath(path) {
|
|
33
|
+
const clean = String(path || "/").split("#")[0].split("?")[0] || "/";
|
|
34
|
+
return `/${clean.replace(/^\/+/, "").replace(/\/+$/, "")}`.replace(/^\/$/, "/");
|
|
35
|
+
}
|
|
36
|
+
function splitPath(path) {
|
|
37
|
+
return normalizePath(path).split("/").filter(Boolean).map(decodeURIComponent);
|
|
38
|
+
}
|
|
39
|
+
function routeScore(route) {
|
|
40
|
+
if (typeof route.score === "number") return route.score;
|
|
41
|
+
return route.segments.reduce((score, segment) => {
|
|
42
|
+
if (segment.kind === "static") return score + 10;
|
|
43
|
+
if (segment.kind === "dynamic") return score + 5;
|
|
44
|
+
return score + (segment.optional ? 1 : 2);
|
|
45
|
+
}, route.segments.length);
|
|
46
|
+
}
|
|
47
|
+
function matchRoute(route, path) {
|
|
48
|
+
const parts = splitPath(path);
|
|
49
|
+
const params = {};
|
|
50
|
+
let index = 0;
|
|
51
|
+
for (const segment of route.segments) {
|
|
52
|
+
if (segment.kind === "catchAll") {
|
|
53
|
+
const rest = parts.slice(index);
|
|
54
|
+
if (!segment.optional && rest.length === 0) return null;
|
|
55
|
+
params[segment.name] = rest;
|
|
56
|
+
index = parts.length;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
const value = parts[index];
|
|
60
|
+
if (value === void 0) return null;
|
|
61
|
+
if (segment.kind === "static") {
|
|
62
|
+
if (value !== segment.value) return null;
|
|
63
|
+
} else {
|
|
64
|
+
params[segment.name] = value;
|
|
65
|
+
}
|
|
66
|
+
index += 1;
|
|
67
|
+
}
|
|
68
|
+
return index === parts.length ? params : null;
|
|
69
|
+
}
|
|
70
|
+
function currentPluginPath(pluginId, routePath) {
|
|
71
|
+
if (routePath) return normalizePath(routePath);
|
|
72
|
+
if (typeof window === "undefined") return "/";
|
|
73
|
+
const path = window.location.pathname;
|
|
74
|
+
if (pluginId) {
|
|
75
|
+
const prefix = `/apps/${pluginId}`;
|
|
76
|
+
if (path === prefix) return "/";
|
|
77
|
+
if (path.startsWith(`${prefix}/`)) return normalizePath(path.slice(prefix.length));
|
|
78
|
+
}
|
|
79
|
+
return normalizePath(path);
|
|
80
|
+
}
|
|
81
|
+
function currentSearchParams() {
|
|
82
|
+
if (typeof window === "undefined") return new URLSearchParams();
|
|
83
|
+
return new URLSearchParams(window.location.search);
|
|
84
|
+
}
|
|
85
|
+
var PaletteRouteErrorBoundary = class extends Component {
|
|
86
|
+
constructor() {
|
|
87
|
+
super(...arguments);
|
|
88
|
+
this.state = { error: null };
|
|
89
|
+
this.reset = () => this.setState({ error: null });
|
|
90
|
+
}
|
|
91
|
+
static getDerivedStateFromError(error) {
|
|
92
|
+
return { error };
|
|
93
|
+
}
|
|
94
|
+
componentDidCatch(_error, _info) {
|
|
95
|
+
}
|
|
96
|
+
render() {
|
|
97
|
+
const error = this.state.error;
|
|
98
|
+
if (!error) return this.props.children;
|
|
99
|
+
if (error.code === NOT_FOUND && this.props.notFound) {
|
|
100
|
+
return createElement(this.props.notFound);
|
|
101
|
+
}
|
|
102
|
+
if (this.props.fallback) {
|
|
103
|
+
return createElement(this.props.fallback, { error, reset: this.reset });
|
|
104
|
+
}
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
function renderRoute(route) {
|
|
109
|
+
const page = createElement(route.page);
|
|
110
|
+
const wrapped = (route.layouts || []).reduceRight(
|
|
111
|
+
(children, Layout) => createElement(Layout, null, children),
|
|
112
|
+
page
|
|
113
|
+
);
|
|
114
|
+
return createElement(
|
|
115
|
+
PaletteRouteErrorBoundary,
|
|
116
|
+
{ fallback: route.error, notFound: route.notFound },
|
|
117
|
+
wrapped
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
function PaletteAppRouter({
|
|
121
|
+
routes,
|
|
122
|
+
notFound: NotFound
|
|
123
|
+
}) {
|
|
124
|
+
const platform = usePlatform();
|
|
125
|
+
const [location, setLocation] = useState(() => ({
|
|
126
|
+
pathname: currentPluginPath(platform.pluginId, platform.routePath),
|
|
127
|
+
searchParams: currentSearchParams()
|
|
128
|
+
}));
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
setLocation({
|
|
131
|
+
pathname: currentPluginPath(platform.pluginId, platform.routePath),
|
|
132
|
+
searchParams: currentSearchParams()
|
|
133
|
+
});
|
|
134
|
+
}, [platform.pluginId, platform.routePath]);
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
const sync = () => setLocation({
|
|
137
|
+
pathname: currentPluginPath(platform.pluginId, platform.routePath),
|
|
138
|
+
searchParams: currentSearchParams()
|
|
139
|
+
});
|
|
140
|
+
window.addEventListener("popstate", sync);
|
|
141
|
+
return () => window.removeEventListener("popstate", sync);
|
|
142
|
+
}, [platform.pluginId, platform.routePath]);
|
|
143
|
+
const sortedRoutes = useMemo(
|
|
144
|
+
() => [...routes].sort((a, b) => routeScore(b) - routeScore(a)),
|
|
145
|
+
[routes]
|
|
146
|
+
);
|
|
147
|
+
const matched = useMemo(() => {
|
|
148
|
+
for (const route of sortedRoutes) {
|
|
149
|
+
const params = matchRoute(route, location.pathname);
|
|
150
|
+
if (params) return { route, params };
|
|
151
|
+
}
|
|
152
|
+
return null;
|
|
153
|
+
}, [location.pathname, sortedRoutes]);
|
|
154
|
+
const navigate = useCallback((target, replace = false) => {
|
|
155
|
+
const [pathPart, queryPart = ""] = String(target || "/").split("?");
|
|
156
|
+
const pathname = normalizePath(pathPart);
|
|
157
|
+
const next = `${pathname}${queryPart ? `?${queryPart}` : ""}`;
|
|
158
|
+
setLocation({ pathname, searchParams: new URLSearchParams(queryPart) });
|
|
159
|
+
const currentPath = typeof window === "undefined" ? "" : window.location.pathname;
|
|
160
|
+
const inPalettePath = platform.pluginId && (currentPath.startsWith(`/apps/${platform.pluginId}`) || platform.routePath !== void 0);
|
|
161
|
+
const osPath = inPalettePath && platform.pluginId ? `/apps/${platform.pluginId}${pathname === "/" ? "" : pathname}${queryPart ? `?${queryPart}` : ""}` : next;
|
|
162
|
+
if (replace && typeof window !== "undefined") window.history.replaceState(null, "", osPath);
|
|
163
|
+
else platform.navigate(osPath);
|
|
164
|
+
}, [platform]);
|
|
165
|
+
const state = useMemo(() => ({
|
|
166
|
+
pathname: location.pathname,
|
|
167
|
+
searchParams: location.searchParams,
|
|
168
|
+
params: matched?.params || {},
|
|
169
|
+
push: (path) => navigate(path, false),
|
|
170
|
+
replace: (path) => navigate(path, true)
|
|
171
|
+
}), [location.pathname, location.searchParams, matched?.params, navigate]);
|
|
172
|
+
const content = matched ? renderRoute(matched.route) : NotFound ? createElement(NotFound) : createElement("div", null, "Page not found");
|
|
173
|
+
return createElement(RouterCtx.Provider, { value: state }, content);
|
|
174
|
+
}
|
|
175
|
+
function useRouterState() {
|
|
176
|
+
const value = useContext2(RouterCtx);
|
|
177
|
+
if (!value) throw new Error("Palette app router hooks must be used inside PaletteAppRouter");
|
|
178
|
+
return value;
|
|
179
|
+
}
|
|
180
|
+
function useRouter() {
|
|
181
|
+
const { push, replace } = useRouterState();
|
|
182
|
+
return { push, replace, back: () => window.history.back(), forward: () => window.history.forward() };
|
|
183
|
+
}
|
|
184
|
+
function usePathname() {
|
|
185
|
+
return useRouterState().pathname;
|
|
186
|
+
}
|
|
187
|
+
function useSearchParams() {
|
|
188
|
+
return useRouterState().searchParams;
|
|
189
|
+
}
|
|
190
|
+
function useParams() {
|
|
191
|
+
return useRouterState().params;
|
|
192
|
+
}
|
|
193
|
+
function Link({
|
|
194
|
+
href,
|
|
195
|
+
replace,
|
|
196
|
+
onClick,
|
|
197
|
+
children,
|
|
198
|
+
...props
|
|
199
|
+
}) {
|
|
200
|
+
const router = useRouter();
|
|
201
|
+
return createElement(
|
|
202
|
+
"a",
|
|
203
|
+
{
|
|
204
|
+
...props,
|
|
205
|
+
href,
|
|
206
|
+
onClick: (event) => {
|
|
207
|
+
onClick?.(event);
|
|
208
|
+
if (event.defaultPrevented || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
|
|
209
|
+
event.preventDefault();
|
|
210
|
+
if (replace) router.replace(href);
|
|
211
|
+
else router.push(href);
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
children
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
export {
|
|
218
|
+
Link,
|
|
219
|
+
PaletteAppRouter,
|
|
220
|
+
notFound,
|
|
221
|
+
useParams,
|
|
222
|
+
usePathname,
|
|
223
|
+
useRouter,
|
|
224
|
+
useSearchParams
|
|
225
|
+
};
|
package/dist/types/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as Agent, a as AgentConfig, b as AppCategory, c as AuthContextValue, O as OrgSummary, d as PaletteLanguage, P as PlatformContext, e as PluginAgentDefinition, f as PluginComponentProps, g as
|
|
1
|
+
export { A as Agent, a as AgentConfig, b as AppCategory, c as AuthContextValue, O as OrgSummary, d as PaletteLanguage, P as PlatformContext, e as PluginAgentDefinition, f as PluginComponentProps, g as PluginConnectionDefinition, h as PluginManifest, i as PluginToolDefinition, U as User } from '../plugin-dpLzOtF6.mjs';
|
|
2
2
|
export { C as ChatAttachment, c as ChatMessage, d as ChatThread, D as DataRoom, b as DataRoomFile, a as DataRoomFolder, e as DataRoomPermission, T as Task, f as TaskAgentSnippet, g as TaskCreatePayload, h as TaskPriority, i as TaskStats, j as TaskStatus, k as TaskType, l as TaskUpdatePayload } from '../data-room-Dtd9LLHf.mjs';
|
|
3
3
|
|
|
4
4
|
interface AgentResource {
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as Agent, a as AgentConfig, b as AppCategory, c as AuthContextValue, O as OrgSummary, d as PaletteLanguage, P as PlatformContext, e as PluginAgentDefinition, f as PluginComponentProps, g as
|
|
1
|
+
export { A as Agent, a as AgentConfig, b as AppCategory, c as AuthContextValue, O as OrgSummary, d as PaletteLanguage, P as PlatformContext, e as PluginAgentDefinition, f as PluginComponentProps, g as PluginConnectionDefinition, h as PluginManifest, i as PluginToolDefinition, U as User } from '../plugin-dpLzOtF6.js';
|
|
2
2
|
export { C as ChatAttachment, c as ChatMessage, d as ChatThread, D as DataRoom, b as DataRoomFile, a as DataRoomFolder, e as DataRoomPermission, T as Task, f as TaskAgentSnippet, g as TaskCreatePayload, h as TaskPriority, i as TaskStats, j as TaskStatus, k as TaskType, l as TaskUpdatePayload } from '../data-room-Dtd9LLHf.js';
|
|
3
3
|
|
|
4
4
|
interface AgentResource {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@palettelab/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.17",
|
|
4
4
|
"description": "Palette Platform SDK for building plugins and apps",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -24,6 +24,11 @@
|
|
|
24
24
|
"types": "./dist/index.d.ts",
|
|
25
25
|
"import": "./dist/index.mjs",
|
|
26
26
|
"require": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./router": {
|
|
29
|
+
"types": "./dist/router/index.d.ts",
|
|
30
|
+
"import": "./dist/router/index.mjs",
|
|
31
|
+
"require": "./dist/router/index.js"
|
|
27
32
|
}
|
|
28
33
|
},
|
|
29
34
|
"files": [
|