@palettelab/sdk 0.1.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.
@@ -0,0 +1,246 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/hooks/index.ts
21
+ var hooks_exports = {};
22
+ __export(hooks_exports, {
23
+ PlatformCtx: () => PlatformCtx,
24
+ usePlatform: () => usePlatform,
25
+ usePluginChat: () => usePluginChat,
26
+ usePluginDataRooms: () => usePluginDataRooms,
27
+ usePluginTasks: () => usePluginTasks
28
+ });
29
+ module.exports = __toCommonJS(hooks_exports);
30
+
31
+ // src/hooks/use-platform.ts
32
+ var import_react = require("react");
33
+ var PlatformCtx = (0, import_react.createContext)(null);
34
+ function usePlatform() {
35
+ const ctx = (0, import_react.useContext)(PlatformCtx);
36
+ if (!ctx) {
37
+ throw new Error(
38
+ "usePlatform must be used within a PlatformProvider. This is provided automatically when your plugin runs on the Palette platform."
39
+ );
40
+ }
41
+ return ctx;
42
+ }
43
+
44
+ // src/hooks/use-plugin-tasks.ts
45
+ var import_react2 = require("react");
46
+ function usePluginTasks(agentId) {
47
+ const { apiFetch } = usePlatform();
48
+ const [tasks, setTasks] = (0, import_react2.useState)([]);
49
+ const [stats, setStats] = (0, import_react2.useState)(null);
50
+ const [loading, setLoading] = (0, import_react2.useState)(true);
51
+ const fetchTasks = (0, import_react2.useCallback)(async () => {
52
+ try {
53
+ const params = new URLSearchParams();
54
+ if (agentId) params.set("agent_id", String(agentId));
55
+ const res = await apiFetch(`/api/v1/tasks?${params}`);
56
+ setTasks(await res.json());
57
+ } catch {
58
+ }
59
+ }, [apiFetch, agentId]);
60
+ const fetchStats = (0, import_react2.useCallback)(async () => {
61
+ try {
62
+ const res = await apiFetch("/api/v1/tasks/stats");
63
+ setStats(await res.json());
64
+ } catch {
65
+ }
66
+ }, [apiFetch]);
67
+ (0, import_react2.useEffect)(() => {
68
+ setLoading(true);
69
+ Promise.all([fetchTasks(), fetchStats()]).finally(() => setLoading(false));
70
+ }, [fetchTasks, fetchStats]);
71
+ const createTask = (0, import_react2.useCallback)(async (payload) => {
72
+ const res = await apiFetch("/api/v1/tasks", {
73
+ method: "POST",
74
+ body: JSON.stringify(payload)
75
+ });
76
+ const task = await res.json();
77
+ await Promise.all([fetchTasks(), fetchStats()]);
78
+ return task;
79
+ }, [apiFetch, fetchTasks, fetchStats]);
80
+ const updateTask = (0, import_react2.useCallback)(async (taskId, payload) => {
81
+ const res = await apiFetch(`/api/v1/tasks/${taskId}`, {
82
+ method: "PATCH",
83
+ body: JSON.stringify(payload)
84
+ });
85
+ const task = await res.json();
86
+ await Promise.all([fetchTasks(), fetchStats()]);
87
+ return task;
88
+ }, [apiFetch, fetchTasks, fetchStats]);
89
+ const deleteTask = (0, import_react2.useCallback)(async (taskId) => {
90
+ await apiFetch(`/api/v1/tasks/${taskId}`, { method: "DELETE" });
91
+ await Promise.all([fetchTasks(), fetchStats()]);
92
+ }, [apiFetch, fetchTasks, fetchStats]);
93
+ return { tasks, stats, loading, createTask, updateTask, deleteTask, refetch: fetchTasks };
94
+ }
95
+
96
+ // src/hooks/use-plugin-data-rooms.ts
97
+ var import_react3 = require("react");
98
+ function usePluginDataRooms() {
99
+ const { apiFetch } = usePlatform();
100
+ const [rooms, setRooms] = (0, import_react3.useState)([]);
101
+ const [loading, setLoading] = (0, import_react3.useState)(true);
102
+ const fetchRooms = (0, import_react3.useCallback)(async () => {
103
+ try {
104
+ const res = await apiFetch("/api/v1/data-rooms");
105
+ setRooms(await res.json());
106
+ } catch {
107
+ }
108
+ }, [apiFetch]);
109
+ (0, import_react3.useEffect)(() => {
110
+ setLoading(true);
111
+ fetchRooms().finally(() => setLoading(false));
112
+ }, [fetchRooms]);
113
+ const fetchFolder = (0, import_react3.useCallback)(async (roomId, folderId) => {
114
+ const path = folderId ? `/api/v1/data-rooms/${roomId}/folders/${folderId}` : `/api/v1/data-rooms/${roomId}`;
115
+ const res = await apiFetch(path);
116
+ return res.json();
117
+ }, [apiFetch]);
118
+ return { rooms, loading, fetchFolder, refetch: fetchRooms };
119
+ }
120
+
121
+ // src/hooks/use-plugin-chat.ts
122
+ var import_react4 = require("react");
123
+
124
+ // src/api.ts
125
+ var _baseUrl = process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:8000";
126
+ function getBaseUrl() {
127
+ return _baseUrl;
128
+ }
129
+
130
+ // src/hooks/use-plugin-chat.ts
131
+ function usePluginChat(agentId) {
132
+ const { apiFetch } = usePlatform();
133
+ const [threads, setThreads] = (0, import_react4.useState)([]);
134
+ const [messages, setMessages] = (0, import_react4.useState)([]);
135
+ const [streaming, setStreaming] = (0, import_react4.useState)(false);
136
+ const [activeThreadId, setActiveThreadId] = (0, import_react4.useState)(null);
137
+ const abortRef = (0, import_react4.useRef)(null);
138
+ const fetchThreads = (0, import_react4.useCallback)(async () => {
139
+ const res = await apiFetch(`/api/v1/chat/${agentId}/threads`);
140
+ setThreads(await res.json());
141
+ }, [apiFetch, agentId]);
142
+ const createThread = (0, import_react4.useCallback)(async () => {
143
+ const res = await apiFetch(`/api/v1/chat/${agentId}/threads`, { method: "POST" });
144
+ const thread = await res.json();
145
+ setActiveThreadId(thread.id);
146
+ setMessages([]);
147
+ await fetchThreads();
148
+ return thread;
149
+ }, [apiFetch, agentId, fetchThreads]);
150
+ const fetchMessages = (0, import_react4.useCallback)(async (threadId) => {
151
+ const res = await apiFetch(`/api/v1/chat/threads/${threadId}/messages`);
152
+ const msgs = await res.json();
153
+ setMessages(msgs);
154
+ setActiveThreadId(threadId);
155
+ }, [apiFetch]);
156
+ const sendMessage = (0, import_react4.useCallback)(async (threadId, content) => {
157
+ setStreaming(true);
158
+ abortRef.current = new AbortController();
159
+ const userMsg = {
160
+ id: crypto.randomUUID(),
161
+ thread_id: threadId,
162
+ role: "user",
163
+ content,
164
+ attachments: null,
165
+ created_at: (/* @__PURE__ */ new Date()).toISOString()
166
+ };
167
+ setMessages((prev) => [...prev, userMsg]);
168
+ let assistantContent = "";
169
+ const assistantMsg = {
170
+ id: crypto.randomUUID(),
171
+ thread_id: threadId,
172
+ role: "assistant",
173
+ content: "",
174
+ attachments: null,
175
+ created_at: (/* @__PURE__ */ new Date()).toISOString()
176
+ };
177
+ setMessages((prev) => [...prev, assistantMsg]);
178
+ try {
179
+ const res = await fetch(`${getBaseUrl()}/api/v1/chat/threads/${threadId}/send`, {
180
+ method: "POST",
181
+ credentials: "include",
182
+ headers: { "Content-Type": "application/json" },
183
+ body: JSON.stringify({ content }),
184
+ signal: abortRef.current.signal
185
+ });
186
+ const reader = res.body?.getReader();
187
+ if (!reader) return;
188
+ const decoder = new TextDecoder();
189
+ let buffer = "";
190
+ while (true) {
191
+ const { done, value } = await reader.read();
192
+ if (done) break;
193
+ buffer += decoder.decode(value, { stream: true });
194
+ const lines = buffer.split("\n");
195
+ buffer = lines.pop() ?? "";
196
+ for (const line of lines) {
197
+ if (!line.startsWith("data: ")) continue;
198
+ try {
199
+ const event = JSON.parse(line.slice(6));
200
+ if (event.event === "token") {
201
+ assistantContent += event.content;
202
+ setMessages((prev) => {
203
+ const copy = [...prev];
204
+ copy[copy.length - 1] = { ...copy[copy.length - 1], content: assistantContent };
205
+ return copy;
206
+ });
207
+ }
208
+ if (event.event === "done" && event.message_id) {
209
+ setMessages((prev) => {
210
+ const copy = [...prev];
211
+ copy[copy.length - 1] = { ...copy[copy.length - 1], id: event.message_id };
212
+ return copy;
213
+ });
214
+ }
215
+ } catch {
216
+ }
217
+ }
218
+ }
219
+ } finally {
220
+ setStreaming(false);
221
+ abortRef.current = null;
222
+ }
223
+ }, []);
224
+ const stopStreaming = (0, import_react4.useCallback)(() => {
225
+ abortRef.current?.abort();
226
+ }, []);
227
+ return {
228
+ threads,
229
+ messages,
230
+ streaming,
231
+ activeThreadId,
232
+ fetchThreads,
233
+ createThread,
234
+ fetchMessages,
235
+ sendMessage,
236
+ stopStreaming
237
+ };
238
+ }
239
+ // Annotate the CommonJS export names for ESM import in node:
240
+ 0 && (module.exports = {
241
+ PlatformCtx,
242
+ usePlatform,
243
+ usePluginChat,
244
+ usePluginDataRooms,
245
+ usePluginTasks
246
+ });
@@ -0,0 +1,215 @@
1
+ // src/hooks/use-platform.ts
2
+ import { createContext, useContext } from "react";
3
+ var PlatformCtx = createContext(null);
4
+ function usePlatform() {
5
+ const ctx = useContext(PlatformCtx);
6
+ if (!ctx) {
7
+ throw new Error(
8
+ "usePlatform must be used within a PlatformProvider. This is provided automatically when your plugin runs on the Palette platform."
9
+ );
10
+ }
11
+ return ctx;
12
+ }
13
+
14
+ // src/hooks/use-plugin-tasks.ts
15
+ import { useCallback, useEffect, useState } from "react";
16
+ function usePluginTasks(agentId) {
17
+ const { apiFetch } = usePlatform();
18
+ const [tasks, setTasks] = useState([]);
19
+ const [stats, setStats] = useState(null);
20
+ const [loading, setLoading] = useState(true);
21
+ const fetchTasks = useCallback(async () => {
22
+ try {
23
+ const params = new URLSearchParams();
24
+ if (agentId) params.set("agent_id", String(agentId));
25
+ const res = await apiFetch(`/api/v1/tasks?${params}`);
26
+ setTasks(await res.json());
27
+ } catch {
28
+ }
29
+ }, [apiFetch, agentId]);
30
+ const fetchStats = useCallback(async () => {
31
+ try {
32
+ const res = await apiFetch("/api/v1/tasks/stats");
33
+ setStats(await res.json());
34
+ } catch {
35
+ }
36
+ }, [apiFetch]);
37
+ useEffect(() => {
38
+ setLoading(true);
39
+ Promise.all([fetchTasks(), fetchStats()]).finally(() => setLoading(false));
40
+ }, [fetchTasks, fetchStats]);
41
+ const createTask = useCallback(async (payload) => {
42
+ const res = await apiFetch("/api/v1/tasks", {
43
+ method: "POST",
44
+ body: JSON.stringify(payload)
45
+ });
46
+ const task = await res.json();
47
+ await Promise.all([fetchTasks(), fetchStats()]);
48
+ return task;
49
+ }, [apiFetch, fetchTasks, fetchStats]);
50
+ const updateTask = useCallback(async (taskId, payload) => {
51
+ const res = await apiFetch(`/api/v1/tasks/${taskId}`, {
52
+ method: "PATCH",
53
+ body: JSON.stringify(payload)
54
+ });
55
+ const task = await res.json();
56
+ await Promise.all([fetchTasks(), fetchStats()]);
57
+ return task;
58
+ }, [apiFetch, fetchTasks, fetchStats]);
59
+ const deleteTask = useCallback(async (taskId) => {
60
+ await apiFetch(`/api/v1/tasks/${taskId}`, { method: "DELETE" });
61
+ await Promise.all([fetchTasks(), fetchStats()]);
62
+ }, [apiFetch, fetchTasks, fetchStats]);
63
+ return { tasks, stats, loading, createTask, updateTask, deleteTask, refetch: fetchTasks };
64
+ }
65
+
66
+ // src/hooks/use-plugin-data-rooms.ts
67
+ import { useCallback as useCallback2, useEffect as useEffect2, useState as useState2 } from "react";
68
+ function usePluginDataRooms() {
69
+ const { apiFetch } = usePlatform();
70
+ const [rooms, setRooms] = useState2([]);
71
+ const [loading, setLoading] = useState2(true);
72
+ const fetchRooms = useCallback2(async () => {
73
+ try {
74
+ const res = await apiFetch("/api/v1/data-rooms");
75
+ setRooms(await res.json());
76
+ } catch {
77
+ }
78
+ }, [apiFetch]);
79
+ useEffect2(() => {
80
+ setLoading(true);
81
+ fetchRooms().finally(() => setLoading(false));
82
+ }, [fetchRooms]);
83
+ const fetchFolder = useCallback2(async (roomId, folderId) => {
84
+ const path = folderId ? `/api/v1/data-rooms/${roomId}/folders/${folderId}` : `/api/v1/data-rooms/${roomId}`;
85
+ const res = await apiFetch(path);
86
+ return res.json();
87
+ }, [apiFetch]);
88
+ return { rooms, loading, fetchFolder, refetch: fetchRooms };
89
+ }
90
+
91
+ // src/hooks/use-plugin-chat.ts
92
+ import { useCallback as useCallback3, useRef, useState as useState3 } from "react";
93
+
94
+ // src/api.ts
95
+ var _baseUrl = process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:8000";
96
+ function getBaseUrl() {
97
+ return _baseUrl;
98
+ }
99
+
100
+ // src/hooks/use-plugin-chat.ts
101
+ function usePluginChat(agentId) {
102
+ const { apiFetch } = usePlatform();
103
+ const [threads, setThreads] = useState3([]);
104
+ const [messages, setMessages] = useState3([]);
105
+ const [streaming, setStreaming] = useState3(false);
106
+ const [activeThreadId, setActiveThreadId] = useState3(null);
107
+ const abortRef = useRef(null);
108
+ const fetchThreads = useCallback3(async () => {
109
+ const res = await apiFetch(`/api/v1/chat/${agentId}/threads`);
110
+ setThreads(await res.json());
111
+ }, [apiFetch, agentId]);
112
+ const createThread = useCallback3(async () => {
113
+ const res = await apiFetch(`/api/v1/chat/${agentId}/threads`, { method: "POST" });
114
+ const thread = await res.json();
115
+ setActiveThreadId(thread.id);
116
+ setMessages([]);
117
+ await fetchThreads();
118
+ return thread;
119
+ }, [apiFetch, agentId, fetchThreads]);
120
+ const fetchMessages = useCallback3(async (threadId) => {
121
+ const res = await apiFetch(`/api/v1/chat/threads/${threadId}/messages`);
122
+ const msgs = await res.json();
123
+ setMessages(msgs);
124
+ setActiveThreadId(threadId);
125
+ }, [apiFetch]);
126
+ const sendMessage = useCallback3(async (threadId, content) => {
127
+ setStreaming(true);
128
+ abortRef.current = new AbortController();
129
+ const userMsg = {
130
+ id: crypto.randomUUID(),
131
+ thread_id: threadId,
132
+ role: "user",
133
+ content,
134
+ attachments: null,
135
+ created_at: (/* @__PURE__ */ new Date()).toISOString()
136
+ };
137
+ setMessages((prev) => [...prev, userMsg]);
138
+ let assistantContent = "";
139
+ const assistantMsg = {
140
+ id: crypto.randomUUID(),
141
+ thread_id: threadId,
142
+ role: "assistant",
143
+ content: "",
144
+ attachments: null,
145
+ created_at: (/* @__PURE__ */ new Date()).toISOString()
146
+ };
147
+ setMessages((prev) => [...prev, assistantMsg]);
148
+ try {
149
+ const res = await fetch(`${getBaseUrl()}/api/v1/chat/threads/${threadId}/send`, {
150
+ method: "POST",
151
+ credentials: "include",
152
+ headers: { "Content-Type": "application/json" },
153
+ body: JSON.stringify({ content }),
154
+ signal: abortRef.current.signal
155
+ });
156
+ const reader = res.body?.getReader();
157
+ if (!reader) return;
158
+ const decoder = new TextDecoder();
159
+ let buffer = "";
160
+ while (true) {
161
+ const { done, value } = await reader.read();
162
+ if (done) break;
163
+ buffer += decoder.decode(value, { stream: true });
164
+ const lines = buffer.split("\n");
165
+ buffer = lines.pop() ?? "";
166
+ for (const line of lines) {
167
+ if (!line.startsWith("data: ")) continue;
168
+ try {
169
+ const event = JSON.parse(line.slice(6));
170
+ if (event.event === "token") {
171
+ assistantContent += event.content;
172
+ setMessages((prev) => {
173
+ const copy = [...prev];
174
+ copy[copy.length - 1] = { ...copy[copy.length - 1], content: assistantContent };
175
+ return copy;
176
+ });
177
+ }
178
+ if (event.event === "done" && event.message_id) {
179
+ setMessages((prev) => {
180
+ const copy = [...prev];
181
+ copy[copy.length - 1] = { ...copy[copy.length - 1], id: event.message_id };
182
+ return copy;
183
+ });
184
+ }
185
+ } catch {
186
+ }
187
+ }
188
+ }
189
+ } finally {
190
+ setStreaming(false);
191
+ abortRef.current = null;
192
+ }
193
+ }, []);
194
+ const stopStreaming = useCallback3(() => {
195
+ abortRef.current?.abort();
196
+ }, []);
197
+ return {
198
+ threads,
199
+ messages,
200
+ streaming,
201
+ activeThreadId,
202
+ fetchThreads,
203
+ createThread,
204
+ fetchMessages,
205
+ sendMessage,
206
+ stopStreaming
207
+ };
208
+ }
209
+ export {
210
+ PlatformCtx,
211
+ usePlatform,
212
+ usePluginChat,
213
+ usePluginDataRooms,
214
+ usePluginTasks
215
+ };
@@ -0,0 +1,31 @@
1
+ export { A as Agent, a as AgentConfig, b as AppCategory, c as AuthContextValue, O as OrgSummary, P as PlatformContext, d as PluginAgentDefinition, e as PluginComponentProps, f as PluginManifest, g as PluginToolDefinition, U as User } from './plugin-CQH23f5N.mjs';
2
+ export { C as ChatAttachment, a as ChatMessage, b as ChatThread, D as DataRoom, c as DataRoomFile, d 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-BP0PjYoe.mjs';
3
+ export { AgentResource, ResourcesByGroup } from './types/index.mjs';
4
+ export { PlatformCtx, usePlatform, usePluginChat, usePluginDataRooms, usePluginTasks } from './hooks/index.mjs';
5
+ export { PluginProvider } from './components/index.mjs';
6
+ import 'react';
7
+ import 'react/jsx-runtime';
8
+
9
+ /**
10
+ * API fetch wrapper for plugin developers.
11
+ * Handles credentials, JSON content type, token refresh, and error extraction.
12
+ *
13
+ * Usage:
14
+ * import { apiFetch } from "@palettelab/sdk"
15
+ * const res = await apiFetch("/api/v1/tasks")
16
+ * const data = await res.json()
17
+ */
18
+ /** Override the base URL (called by platform runtime, not plugins) */
19
+ declare function setBaseUrl(url: string): void;
20
+ declare function getBaseUrl(): string;
21
+ /**
22
+ * Authenticated API fetch with automatic token refresh.
23
+ * Throws on non-2xx responses with the server's error detail.
24
+ */
25
+ declare function apiFetch(path: string, init?: RequestInit): Promise<Response>;
26
+ /**
27
+ * Upload a file via multipart form data.
28
+ */
29
+ declare function apiUpload(path: string, file: File, fieldName?: string, extraFields?: Record<string, string>): Promise<Response>;
30
+
31
+ export { apiFetch, apiUpload, getBaseUrl, setBaseUrl };
@@ -0,0 +1,31 @@
1
+ export { A as Agent, a as AgentConfig, b as AppCategory, c as AuthContextValue, O as OrgSummary, P as PlatformContext, d as PluginAgentDefinition, e as PluginComponentProps, f as PluginManifest, g as PluginToolDefinition, U as User } from './plugin-CQH23f5N.js';
2
+ export { C as ChatAttachment, a as ChatMessage, b as ChatThread, D as DataRoom, c as DataRoomFile, d 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-BP0PjYoe.js';
3
+ export { AgentResource, ResourcesByGroup } from './types/index.js';
4
+ export { PlatformCtx, usePlatform, usePluginChat, usePluginDataRooms, usePluginTasks } from './hooks/index.js';
5
+ export { PluginProvider } from './components/index.js';
6
+ import 'react';
7
+ import 'react/jsx-runtime';
8
+
9
+ /**
10
+ * API fetch wrapper for plugin developers.
11
+ * Handles credentials, JSON content type, token refresh, and error extraction.
12
+ *
13
+ * Usage:
14
+ * import { apiFetch } from "@palettelab/sdk"
15
+ * const res = await apiFetch("/api/v1/tasks")
16
+ * const data = await res.json()
17
+ */
18
+ /** Override the base URL (called by platform runtime, not plugins) */
19
+ declare function setBaseUrl(url: string): void;
20
+ declare function getBaseUrl(): string;
21
+ /**
22
+ * Authenticated API fetch with automatic token refresh.
23
+ * Throws on non-2xx responses with the server's error detail.
24
+ */
25
+ declare function apiFetch(path: string, init?: RequestInit): Promise<Response>;
26
+ /**
27
+ * Upload a file via multipart form data.
28
+ */
29
+ declare function apiUpload(path: string, file: File, fieldName?: string, extraFields?: Record<string, string>): Promise<Response>;
30
+
31
+ export { apiFetch, apiUpload, getBaseUrl, setBaseUrl };