@promptbook/cli 0.103.0-47 → 0.103.0-49

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.
Files changed (133) hide show
  1. package/apps/agents-server/README.md +1 -1
  2. package/apps/agents-server/TODO.txt +6 -5
  3. package/apps/agents-server/config.ts +130 -0
  4. package/apps/agents-server/next.config.ts +1 -1
  5. package/apps/agents-server/public/fonts/OpenMoji-black-glyf.woff2 +0 -0
  6. package/apps/agents-server/public/fonts/download-font.js +22 -0
  7. package/apps/agents-server/src/app/[agentName]/[...rest]/page.tsx +6 -0
  8. package/apps/agents-server/src/app/[agentName]/page.tsx +1 -0
  9. package/apps/agents-server/src/app/actions.ts +37 -2
  10. package/apps/agents-server/src/app/agents/[agentName]/AgentChatWrapper.tsx +68 -0
  11. package/apps/agents-server/src/app/agents/[agentName]/AgentQrCode.tsx +55 -0
  12. package/apps/agents-server/src/app/agents/[agentName]/AgentUrlCopy.tsx +4 -5
  13. package/apps/agents-server/src/app/agents/[agentName]/CopyField.tsx +44 -0
  14. package/apps/agents-server/src/app/agents/[agentName]/api/book/route.ts +8 -8
  15. package/apps/agents-server/src/app/agents/[agentName]/api/chat/route.ts +100 -18
  16. package/apps/agents-server/src/app/agents/[agentName]/api/feedback/route.ts +54 -0
  17. package/apps/agents-server/src/app/agents/[agentName]/api/modelRequirements/route.ts +6 -6
  18. package/apps/agents-server/src/app/agents/[agentName]/api/modelRequirements/systemMessage/route.ts +3 -3
  19. package/apps/agents-server/src/app/agents/[agentName]/api/profile/route.ts +6 -7
  20. package/apps/agents-server/src/app/agents/[agentName]/book/BookEditorWrapper.tsx +6 -7
  21. package/apps/agents-server/src/app/agents/[agentName]/book/page.tsx +9 -2
  22. package/apps/agents-server/src/app/agents/[agentName]/book+chat/AgentBookAndChat.tsx +23 -0
  23. package/apps/agents-server/src/app/agents/[agentName]/book+chat/{AgentBookAndChatComponent.tsx → AgentBookAndChatComponent.tsx.todo} +6 -8
  24. package/apps/agents-server/src/app/agents/[agentName]/book+chat/page.tsx +28 -17
  25. package/apps/agents-server/src/app/agents/[agentName]/book+chat/page.tsx.todo +21 -0
  26. package/apps/agents-server/src/app/agents/[agentName]/chat/AgentChatWrapper.tsx +34 -4
  27. package/apps/agents-server/src/app/agents/[agentName]/chat/page.tsx +4 -1
  28. package/apps/agents-server/src/app/agents/[agentName]/generateAgentMetadata.ts +42 -0
  29. package/apps/agents-server/src/app/agents/[agentName]/page.tsx +111 -108
  30. package/apps/agents-server/src/app/agents/page.tsx +1 -1
  31. package/apps/agents-server/src/app/api/auth/login/route.ts +65 -0
  32. package/apps/agents-server/src/app/api/auth/logout/route.ts +7 -0
  33. package/apps/agents-server/src/app/api/metadata/route.ts +116 -0
  34. package/apps/agents-server/src/app/api/upload/route.ts +7 -7
  35. package/apps/agents-server/src/app/api/users/[username]/route.ts +75 -0
  36. package/apps/agents-server/src/app/api/users/route.ts +71 -0
  37. package/apps/agents-server/src/app/globals.css +35 -1
  38. package/apps/agents-server/src/app/layout.tsx +43 -23
  39. package/apps/agents-server/src/app/metadata/MetadataClient.tsx +271 -0
  40. package/apps/agents-server/src/app/metadata/page.tsx +13 -0
  41. package/apps/agents-server/src/app/not-found.tsx +5 -0
  42. package/apps/agents-server/src/app/page.tsx +84 -46
  43. package/apps/agents-server/src/components/Auth/AuthControls.tsx +123 -0
  44. package/apps/agents-server/src/components/ErrorPage/ErrorPage.tsx +33 -0
  45. package/apps/agents-server/src/components/ForbiddenPage/ForbiddenPage.tsx +15 -0
  46. package/apps/agents-server/src/components/Header/Header.tsx +146 -0
  47. package/apps/agents-server/src/components/LayoutWrapper/LayoutWrapper.tsx +27 -0
  48. package/apps/agents-server/src/components/LoginDialog/LoginDialog.tsx +40 -0
  49. package/apps/agents-server/src/components/LoginForm/LoginForm.tsx +109 -0
  50. package/apps/agents-server/src/components/NotFoundPage/NotFoundPage.tsx +17 -0
  51. package/apps/agents-server/src/components/UsersList/UsersList.tsx +190 -0
  52. package/apps/agents-server/src/components/VercelDeploymentCard/VercelDeploymentCard.tsx +60 -0
  53. package/apps/agents-server/src/database/$getTableName.ts +18 -0
  54. package/apps/agents-server/src/database/$provideSupabase.ts +29 -0
  55. package/apps/agents-server/src/{supabase/getSupabaseForBrowser.ts → database/$provideSupabaseForBrowser.ts} +9 -5
  56. package/apps/agents-server/src/{supabase/getSupabaseForServer.ts → database/$provideSupabaseForServer.ts} +7 -7
  57. package/apps/agents-server/src/{supabase/getSupabaseForWorker.ts → database/$provideSupabaseForWorker.ts} +5 -4
  58. package/apps/agents-server/src/database/getMetadata.ts +31 -0
  59. package/apps/agents-server/src/database/metadataDefaults.ts +32 -0
  60. package/apps/agents-server/src/database/schema.sql +179 -0
  61. package/apps/agents-server/src/database/schema.ts +251 -0
  62. package/apps/agents-server/src/middleware.ts +162 -0
  63. package/apps/agents-server/src/tools/$provideAgentCollectionForServer.ts +14 -10
  64. package/apps/agents-server/src/tools/$provideCdnForServer.ts +1 -1
  65. package/apps/agents-server/src/tools/$provideExecutionToolsForServer.ts +11 -13
  66. package/apps/agents-server/src/tools/$provideOpenAiAssistantExecutionToolsForServer.ts +7 -7
  67. package/apps/agents-server/src/tools/$provideServer.ts +39 -0
  68. package/apps/agents-server/src/utils/auth.ts +33 -0
  69. package/apps/agents-server/src/utils/cdn/utils/getUserFileCdnKey.ts +2 -1
  70. package/apps/agents-server/src/utils/cdn/utils/nameToSubfolderPath.ts +1 -1
  71. package/apps/agents-server/src/utils/getCurrentUser.ts +32 -0
  72. package/apps/agents-server/src/utils/isIpAllowed.ts +101 -0
  73. package/apps/agents-server/src/utils/isUserAdmin.ts +31 -0
  74. package/apps/agents-server/src/utils/session.ts +50 -0
  75. package/apps/agents-server/tailwind.config.ts +2 -0
  76. package/esm/index.es.js +310 -49
  77. package/esm/index.es.js.map +1 -1
  78. package/esm/typings/servers.d.ts +1 -0
  79. package/esm/typings/src/_packages/core.index.d.ts +6 -0
  80. package/esm/typings/src/_packages/types.index.d.ts +4 -0
  81. package/esm/typings/src/_packages/utils.index.d.ts +2 -0
  82. package/esm/typings/src/book-2.0/agent-source/AgentBasicInformation.d.ts +17 -3
  83. package/esm/typings/src/book-2.0/agent-source/AgentSourceParseResult.d.ts +2 -1
  84. package/esm/typings/src/book-2.0/agent-source/computeAgentHash.d.ts +8 -0
  85. package/esm/typings/src/book-2.0/agent-source/computeAgentHash.test.d.ts +1 -0
  86. package/esm/typings/src/book-2.0/agent-source/createDefaultAgentName.d.ts +8 -0
  87. package/esm/typings/src/book-2.0/agent-source/normalizeAgentName.d.ts +9 -0
  88. package/esm/typings/src/book-2.0/agent-source/normalizeAgentName.test.d.ts +1 -0
  89. package/esm/typings/src/book-2.0/agent-source/parseAgentSourceWithCommitments.d.ts +1 -1
  90. package/esm/typings/src/collection/agent-collection/constructors/agent-collection-in-supabase/AgentCollectionInSupabase.d.ts +14 -8
  91. package/esm/typings/src/collection/agent-collection/constructors/agent-collection-in-supabase/AgentCollectionInSupabaseOptions.d.ts +10 -0
  92. package/esm/typings/src/collection/agent-collection/constructors/agent-collection-in-supabase/AgentsDatabaseSchema.d.ts +57 -32
  93. package/esm/typings/src/commitments/MESSAGE/InitialMessageCommitmentDefinition.d.ts +28 -0
  94. package/esm/typings/src/commitments/index.d.ts +2 -1
  95. package/esm/typings/src/config.d.ts +1 -0
  96. package/esm/typings/src/errors/DatabaseError.d.ts +2 -2
  97. package/esm/typings/src/errors/WrappedError.d.ts +2 -2
  98. package/esm/typings/src/execution/ExecutionTask.d.ts +2 -2
  99. package/esm/typings/src/execution/LlmExecutionTools.d.ts +6 -1
  100. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsForWizardOrCli.d.ts +2 -2
  101. package/esm/typings/src/llm-providers/_common/utils/assertUniqueModels.d.ts +12 -0
  102. package/esm/typings/src/llm-providers/agent/Agent.d.ts +17 -4
  103. package/esm/typings/src/llm-providers/agent/AgentLlmExecutionTools.d.ts +10 -1
  104. package/esm/typings/src/llm-providers/agent/RemoteAgent.d.ts +6 -2
  105. package/esm/typings/src/llm-providers/openai/OpenAiAssistantExecutionTools.d.ts +30 -4
  106. package/esm/typings/src/llm-providers/openai/openai-models.test.d.ts +4 -0
  107. package/esm/typings/src/remote-server/startAgentServer.d.ts +2 -2
  108. package/esm/typings/src/remote-server/startRemoteServer.d.ts +1 -2
  109. package/esm/typings/src/transpilers/openai-sdk/register.d.ts +1 -1
  110. package/esm/typings/src/types/typeAliases.d.ts +6 -0
  111. package/esm/typings/src/utils/color/Color.d.ts +7 -0
  112. package/esm/typings/src/utils/color/Color.test.d.ts +1 -0
  113. package/esm/typings/src/utils/environment/$getGlobalScope.d.ts +2 -2
  114. package/esm/typings/src/utils/misc/computeHash.d.ts +11 -0
  115. package/esm/typings/src/utils/misc/computeHash.test.d.ts +1 -0
  116. package/esm/typings/src/utils/normalization/normalize-to-kebab-case.d.ts +2 -0
  117. package/esm/typings/src/utils/normalization/normalizeTo_PascalCase.d.ts +3 -0
  118. package/esm/typings/src/utils/normalization/normalizeTo_camelCase.d.ts +2 -0
  119. package/esm/typings/src/utils/normalization/titleToName.d.ts +2 -0
  120. package/esm/typings/src/utils/organization/$sideEffect.d.ts +2 -2
  121. package/esm/typings/src/utils/organization/$side_effect.d.ts +2 -2
  122. package/esm/typings/src/utils/organization/TODO_USE.d.ts +2 -2
  123. package/esm/typings/src/utils/organization/keepUnused.d.ts +2 -2
  124. package/esm/typings/src/utils/organization/preserve.d.ts +3 -3
  125. package/esm/typings/src/utils/organization/really_any.d.ts +7 -0
  126. package/esm/typings/src/utils/serialization/asSerializable.d.ts +2 -2
  127. package/esm/typings/src/version.d.ts +1 -1
  128. package/package.json +1 -1
  129. package/umd/index.umd.js +311 -50
  130. package/umd/index.umd.js.map +1 -1
  131. package/apps/agents-server/config.ts.todo +0 -312
  132. package/apps/agents-server/src/supabase/TODO.txt +0 -1
  133. package/apps/agents-server/src/supabase/getSupabase.ts +0 -25
@@ -0,0 +1,190 @@
1
+ 'use client';
2
+
3
+ import { useState, useEffect } from 'react';
4
+
5
+ type User = {
6
+ id: number;
7
+ username: string;
8
+ createdAt: string;
9
+ updatedAt: string;
10
+ isAdmin: boolean;
11
+ };
12
+
13
+ export function UsersList() {
14
+ const [users, setUsers] = useState<User[]>([]);
15
+ const [loading, setLoading] = useState(true);
16
+ const [error, setError] = useState<string | null>(null);
17
+ const [newUsername, setNewUsername] = useState('');
18
+ const [newPassword, setNewPassword] = useState('');
19
+ const [newIsAdmin, setNewIsAdmin] = useState(false);
20
+
21
+ const fetchUsers = async () => {
22
+ try {
23
+ setLoading(true);
24
+ const response = await fetch('/api/users');
25
+ if (!response.ok) {
26
+ if (response.status === 401) {
27
+ // Not authorized, maybe session expired
28
+ return;
29
+ }
30
+ throw new Error('Failed to fetch users');
31
+ }
32
+ const data = await response.json();
33
+ setUsers(data);
34
+ } catch (err) {
35
+ setError(err instanceof Error ? err.message : 'An error occurred');
36
+ } finally {
37
+ setLoading(false);
38
+ }
39
+ };
40
+
41
+ useEffect(() => {
42
+ fetchUsers();
43
+ }, []);
44
+
45
+ const handleCreateUser = async (e: React.FormEvent) => {
46
+ e.preventDefault();
47
+ setError(null);
48
+
49
+ try {
50
+ const response = await fetch('/api/users', {
51
+ method: 'POST',
52
+ headers: { 'Content-Type': 'application/json' },
53
+ body: JSON.stringify({
54
+ username: newUsername,
55
+ password: newPassword,
56
+ isAdmin: newIsAdmin,
57
+ }),
58
+ });
59
+
60
+ if (!response.ok) {
61
+ const data = await response.json();
62
+ throw new Error(data.error || 'Failed to create user');
63
+ }
64
+
65
+ setNewUsername('');
66
+ setNewPassword('');
67
+ setNewIsAdmin(false);
68
+ fetchUsers();
69
+ } catch (err) {
70
+ setError(err instanceof Error ? err.message : 'An error occurred');
71
+ }
72
+ };
73
+
74
+ const handleDeleteUser = async (username: string) => {
75
+ if (!confirm(`Are you sure you want to delete user ${username}?`)) return;
76
+
77
+ try {
78
+ const response = await fetch(`/api/users/${username}`, {
79
+ method: 'DELETE',
80
+ });
81
+
82
+ if (!response.ok) {
83
+ const data = await response.json();
84
+ throw new Error(data.error || 'Failed to delete user');
85
+ }
86
+ fetchUsers();
87
+ } catch (err) {
88
+ setError(err instanceof Error ? err.message : 'An error occurred');
89
+ }
90
+ };
91
+
92
+ const handleToggleAdmin = async (username: string, currentIsAdmin: boolean) => {
93
+ try {
94
+ const response = await fetch(`/api/users/${username}`, {
95
+ method: 'PATCH',
96
+ headers: { 'Content-Type': 'application/json' },
97
+ body: JSON.stringify({ isAdmin: !currentIsAdmin }),
98
+ });
99
+
100
+ if (!response.ok) {
101
+ const data = await response.json();
102
+ throw new Error(data.error || 'Failed to update user');
103
+ }
104
+ fetchUsers();
105
+ } catch (err) {
106
+ setError(err instanceof Error ? err.message : 'An error occurred');
107
+ }
108
+ };
109
+
110
+ if (loading) return <div>Loading users...</div>;
111
+
112
+ return (
113
+ <div className="space-y-6">
114
+ <h2 className="text-3xl text-gray-900 mt-16 mb-4">Users ({users.length})</h2>
115
+
116
+ {error && <div className="bg-red-100 text-red-700 p-3 rounded">{error}</div>}
117
+
118
+ <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
119
+ {users.map((user) => (
120
+ <div key={user.id} className="block p-6 bg-white rounded-lg shadow-md border border-gray-200">
121
+ <div className="flex justify-between items-start">
122
+ <div>
123
+ <h3 className="text-xl font-semibold text-gray-900">{user.username}</h3>
124
+ {user.isAdmin && <span className="inline-block bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded mt-1">Admin</span>}
125
+ <p className="text-gray-500 text-sm mt-2">Created: {new Date(user.createdAt).toLocaleDateString()}</p>
126
+ </div>
127
+ <div className="space-x-2">
128
+ <button
129
+ onClick={() => handleToggleAdmin(user.username, user.isAdmin)}
130
+ className="text-sm text-blue-600 hover:text-blue-800"
131
+ >
132
+ {user.isAdmin ? 'Remove Admin' : 'Make Admin'}
133
+ </button>
134
+ <button
135
+ onClick={() => handleDeleteUser(user.username)}
136
+ className="text-sm text-red-600 hover:text-red-800"
137
+ >
138
+ Delete
139
+ </button>
140
+ </div>
141
+ </div>
142
+ </div>
143
+ ))}
144
+
145
+ {/* Create User Form */}
146
+ <div className="block p-6 bg-gray-50 rounded-lg border border-dashed border-gray-300">
147
+ <h3 className="text-lg font-semibold text-gray-700 mb-4">Add New User</h3>
148
+ <form onSubmit={handleCreateUser} className="space-y-3">
149
+ <div>
150
+ <input
151
+ type="text"
152
+ placeholder="Username"
153
+ value={newUsername}
154
+ onChange={(e) => setNewUsername(e.target.value)}
155
+ className="w-full p-2 border border-gray-300 rounded"
156
+ required
157
+ />
158
+ </div>
159
+ <div>
160
+ <input
161
+ type="password"
162
+ placeholder="Password"
163
+ value={newPassword}
164
+ onChange={(e) => setNewPassword(e.target.value)}
165
+ className="w-full p-2 border border-gray-300 rounded"
166
+ required
167
+ />
168
+ </div>
169
+ <div className="flex items-center">
170
+ <input
171
+ type="checkbox"
172
+ id="newIsAdmin"
173
+ checked={newIsAdmin}
174
+ onChange={(e) => setNewIsAdmin(e.target.checked)}
175
+ className="mr-2"
176
+ />
177
+ <label htmlFor="newIsAdmin" className="text-gray-700">Is Admin</label>
178
+ </div>
179
+ <button
180
+ type="submit"
181
+ className="w-full bg-blue-600 text-white p-2 rounded hover:bg-blue-700 transition-colors"
182
+ >
183
+ Create User
184
+ </button>
185
+ </form>
186
+ </div>
187
+ </div>
188
+ </div>
189
+ );
190
+ }
@@ -0,0 +1,60 @@
1
+ import {
2
+ NEXT_PUBLIC_VERCEL_BRANCH_URL,
3
+ NEXT_PUBLIC_VERCEL_ENV,
4
+ NEXT_PUBLIC_VERCEL_GIT_COMMIT_AUTHOR_LOGIN,
5
+ NEXT_PUBLIC_VERCEL_GIT_COMMIT_AUTHOR_NAME,
6
+ NEXT_PUBLIC_VERCEL_GIT_COMMIT_MESSAGE,
7
+ NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF,
8
+ NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA,
9
+ NEXT_PUBLIC_VERCEL_GIT_PREVIOUS_SHA,
10
+ NEXT_PUBLIC_VERCEL_GIT_PROVIDER,
11
+ NEXT_PUBLIC_VERCEL_GIT_PULL_REQUEST_ID,
12
+ NEXT_PUBLIC_VERCEL_GIT_REPO_ID,
13
+ NEXT_PUBLIC_VERCEL_GIT_REPO_OWNER,
14
+ NEXT_PUBLIC_VERCEL_GIT_REPO_SLUG,
15
+ NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL,
16
+ NEXT_PUBLIC_VERCEL_TARGET_ENV,
17
+ NEXT_PUBLIC_VERCEL_URL,
18
+ } from '@/config';
19
+ import Link from 'next/link';
20
+
21
+ /**
22
+ * [♐️] Expose Vercel environment variables to indentify the deployment
23
+ */
24
+ export default function VercelDeploymentCard() {
25
+ return (
26
+ <Link
27
+ href="#"
28
+ className="block p-6 bg-white rounded-lg shadow-md hover:shadow-xl transition-shadow duration-300 border border-gray-200 hover:border-blue-400"
29
+ >
30
+ <h2 className="text-2xl font-semibold text-gray-900 mb-4">Vercel Deployment</h2>
31
+
32
+ <p className="text-gray-600">NEXT_PUBLIC_VERCEL_ENV: {NEXT_PUBLIC_VERCEL_ENV}</p>
33
+ <p className="text-gray-600">NEXT_PUBLIC_VERCEL_TARGET_ENV: {NEXT_PUBLIC_VERCEL_TARGET_ENV}</p>
34
+ <p className="text-gray-600">NEXT_PUBLIC_VERCEL_URL: {NEXT_PUBLIC_VERCEL_URL}</p>
35
+ <p className="text-gray-600">NEXT_PUBLIC_VERCEL_BRANCH_URL: {NEXT_PUBLIC_VERCEL_BRANCH_URL}</p>
36
+ <p className="text-gray-600">
37
+ NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL: {NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL}
38
+ </p>
39
+ <p className="text-gray-600">NEXT_PUBLIC_VERCEL_GIT_PROVIDER: {NEXT_PUBLIC_VERCEL_GIT_PROVIDER}</p>
40
+ <p className="text-gray-600">NEXT_PUBLIC_VERCEL_GIT_REPO_OWNER: {NEXT_PUBLIC_VERCEL_GIT_REPO_OWNER}</p>
41
+ <p className="text-gray-600">NEXT_PUBLIC_VERCEL_GIT_REPO_SLUG: {NEXT_PUBLIC_VERCEL_GIT_REPO_SLUG}</p>
42
+ <p className="text-gray-600">NEXT_PUBLIC_VERCEL_GIT_REPO_ID: {NEXT_PUBLIC_VERCEL_GIT_REPO_ID}</p>
43
+ <p className="text-gray-600">NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA: {NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA}</p>
44
+ <p className="text-gray-600">
45
+ NEXT_PUBLIC_VERCEL_GIT_COMMIT_MESSAGE: {NEXT_PUBLIC_VERCEL_GIT_COMMIT_MESSAGE}
46
+ </p>
47
+ <p className="text-gray-600">NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF: {NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF}</p>
48
+ <p className="text-gray-600">
49
+ NEXT_PUBLIC_VERCEL_GIT_COMMIT_AUTHOR_NAME: {NEXT_PUBLIC_VERCEL_GIT_COMMIT_AUTHOR_NAME}
50
+ </p>
51
+ <p className="text-gray-600">
52
+ NEXT_PUBLIC_VERCEL_GIT_COMMIT_AUTHOR_LOGIN: {NEXT_PUBLIC_VERCEL_GIT_COMMIT_AUTHOR_LOGIN}
53
+ </p>
54
+ <p className="text-gray-600">NEXT_PUBLIC_VERCEL_GIT_PREVIOUS_SHA: {NEXT_PUBLIC_VERCEL_GIT_PREVIOUS_SHA}</p>
55
+ <p className="text-gray-600">
56
+ NEXT_PUBLIC_VERCEL_GIT_PULL_REQUEST_ID: {NEXT_PUBLIC_VERCEL_GIT_PULL_REQUEST_ID}
57
+ </p>
58
+ </Link>
59
+ );
60
+ }
@@ -0,0 +1,18 @@
1
+ import { $provideServer } from '../tools/$provideServer';
2
+ import { AgentsServerDatabase } from './schema';
3
+
4
+ /**
5
+ * Type representing the non-prefixed table names in the AgentsServerDatabase public schema
6
+ */
7
+ type string_table_name = keyof AgentsServerDatabase['public']['Tables'];
8
+
9
+ /**
10
+ * Get the Supabase table name with prefix if it is configured
11
+ *
12
+ * @param tableName - The original table name
13
+ * @returns The prefixed table name
14
+ */
15
+ export async function $getTableName<TTable extends string_table_name>(tableName: TTable): Promise<TTable> {
16
+ const { tablePrefix } = await $provideServer();
17
+ return `${tablePrefix}${tableName}` as TTable;
18
+ }
@@ -0,0 +1,29 @@
1
+ import { $isRunningInBrowser, $isRunningInNode, $isRunningInWebWorker } from '@promptbook-local/utils';
2
+ import type { SupabaseClient } from '@supabase/supabase-js';
3
+ import { $provideSupabaseForBrowser } from './$provideSupabaseForBrowser';
4
+ import { $provideSupabaseForServer } from './$provideSupabaseForServer';
5
+ import { $provideSupabaseForWorker } from './$provideSupabaseForWorker';
6
+ import { AgentsServerDatabase } from './schema';
7
+
8
+ /**
9
+ * Get supabase client in any environment
10
+ *
11
+ * Note: The client is cached, so it's safe to call this function multiple times
12
+ *
13
+ * @returns instance of supabase client
14
+ */
15
+ export function $provideSupabase(): SupabaseClient<AgentsServerDatabase> {
16
+ if ($isRunningInNode()) {
17
+ return $provideSupabaseForServer();
18
+ } else if ($isRunningInBrowser()) {
19
+ return $provideSupabaseForBrowser();
20
+ } else if ($isRunningInWebWorker()) {
21
+ return $provideSupabaseForWorker();
22
+ } else {
23
+ throw new Error('Unknown environment, cannot determine how to get Supabase client');
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Note: [🎇] Promptbook Agents Server is not using Supabase in Browser so maybe remove this file
29
+ */
@@ -1,9 +1,9 @@
1
- import { createClient, SupabaseClient } from '@supabase/supabase-js';
2
1
  import { AgentsDatabaseSchema } from '@promptbook-local/types';
3
2
  import { $isRunningInBrowser } from '@promptbook-local/utils';
3
+ import { createClient, SupabaseClient } from '@supabase/supabase-js';
4
4
 
5
5
  /**
6
- * Internal cache for `getSupabaseForBrowser`
6
+ * Internal cache for `$provideSupabaseForBrowser`
7
7
  *
8
8
  * @private
9
9
  * @singleton
@@ -14,14 +14,14 @@ let supabase: SupabaseClient<AgentsDatabaseSchema>;
14
14
  * Get supabase client
15
15
  *
16
16
  * Note: The client is cached, so it's safe to call this function multiple times
17
- * Note: This function is available ONLY in browser, use getSupabaseForServer in node
17
+ * Note: This function is available ONLY in browser, use $provideSupabaseForServer in node
18
18
  *
19
19
  * @returns instance of supabase client
20
20
  */
21
- export function getSupabaseForBrowser(): typeof supabase {
21
+ export function $provideSupabaseForBrowser(): typeof supabase {
22
22
  if (!$isRunningInBrowser()) {
23
23
  throw new Error(
24
- 'Function `getSupabaseForBrowser` can not be used on server or worker, use `getSupabaseForServer` or `getSupabaseForWorker` instead.',
24
+ 'Function `$provideSupabaseForBrowser` can not be used on server or worker, use `$provideSupabaseForServer` or `$provideSupabaseForWorker` instead.',
25
25
  );
26
26
  }
27
27
 
@@ -35,3 +35,7 @@ export function getSupabaseForBrowser(): typeof supabase {
35
35
 
36
36
  return supabase;
37
37
  }
38
+
39
+ /**
40
+ * Note: [🎇] Promptbook Agents Server is not using Supabase in Browser so maybe remove this file
41
+ */
@@ -1,33 +1,33 @@
1
- import { AgentsDatabaseSchema } from '@promptbook-local/types';
2
1
  import { $isRunningInNode } from '@promptbook-local/utils';
3
2
  import { createClient, SupabaseClient } from '@supabase/supabase-js';
3
+ import { AgentsServerDatabase } from './schema';
4
4
 
5
5
  /**
6
- * Internal cache for `getSupabaseForServer`
6
+ * Internal cache for `$provideSupabaseForServer`
7
7
  *
8
8
  * @private
9
9
  * @singleton
10
10
  */
11
- let supabase: SupabaseClient<AgentsDatabaseSchema>;
11
+ let supabase: SupabaseClient<AgentsServerDatabase>;
12
12
 
13
13
  /**
14
14
  * Get supabase client
15
15
  *
16
16
  * Note: The client is cached, so it's safe to call this function multiple times
17
- * Note: This function is available ONLY on server/node, use getSupabaseForClient in browser
17
+ * Note: This function is available ONLY on server/node, use $provideSupabaseForClient in browser
18
18
  *
19
19
  * @returns instance of supabase client
20
20
  */
21
- export function getSupabaseForServer(): typeof supabase {
21
+ export function $provideSupabaseForServer(): typeof supabase {
22
22
  if (!$isRunningInNode()) {
23
23
  throw new Error(
24
- 'Function `getSupabaseForServer` can not be used in browser, use `getSupabaseForBrowser` instead.',
24
+ 'Function `$provideSupabaseForServer` can not be used in browser, use `$provideSupabaseForBrowser` instead.',
25
25
  );
26
26
  }
27
27
 
28
28
  if (!supabase) {
29
29
  // Create a single supabase client for interacting with your database
30
- supabase = createClient<AgentsDatabaseSchema>(
30
+ supabase = createClient<AgentsServerDatabase>(
31
31
  process.env.NEXT_PUBLIC_SUPABASE_URL!,
32
32
  process.env.SUPABASE_SERVICE_ROLE_KEY!,
33
33
  {
@@ -3,7 +3,7 @@ import { $isRunningInWebWorker } from '@promptbook-local/utils';
3
3
  import { createClient, SupabaseClient } from '@supabase/supabase-js';
4
4
 
5
5
  /**
6
- * Internal cache for `getSupabaseForWorker`
6
+ * Internal cache for `$provideSupabaseForWorker`
7
7
  *
8
8
  * @private
9
9
  * @singleton
@@ -14,14 +14,14 @@ let supabase: SupabaseClient<AgentsDatabaseSchema>;
14
14
  * Get supabase client
15
15
  *
16
16
  * Note: The client is cached, so it's safe to call this function multiple times
17
- * Note: This function is available ONLY in worker, use getSupabaseForBrowser for main thread
17
+ * Note: This function is available ONLY in worker, use $provideSupabaseForBrowser for main thread
18
18
  *
19
19
  * @returns instance of supabase client
20
20
  */
21
- export function getSupabaseForWorker(): typeof supabase {
21
+ export function $provideSupabaseForWorker(): typeof supabase {
22
22
  if (!$isRunningInWebWorker) {
23
23
  throw new Error(
24
- 'Function `getSupabaseForWorker` can not be used in browser, use `getSupabaseForBrowser` instead.',
24
+ 'Function `$provideSupabaseForWorker` can not be used in browser, use `$provideSupabaseForBrowser` instead.',
25
25
  );
26
26
  }
27
27
 
@@ -37,6 +37,7 @@ export function getSupabaseForWorker(): typeof supabase {
37
37
  }
38
38
 
39
39
  /**
40
+ * Note: [🎇] Promptbook Agents Server is not using Supabase in Browser so maybe remove this file
40
41
  * TODO: Fix> No storage option exists to persist the session, which may result in unexpected behavior when using auth.
41
42
  If you want to set persistSession to true, please provide a storage option or you may set persistSession to false to disable this warning.
42
43
  */
@@ -0,0 +1,31 @@
1
+ import { $getTableName } from './$getTableName';
2
+ import { $provideSupabase } from './$provideSupabase';
3
+ import { metadataDefaults } from './metadataDefaults';
4
+
5
+ /**
6
+ * Get metadata value by key
7
+ *
8
+ * @param key - The key of the metadata
9
+ * @returns The value of the metadata or default value if not found
10
+ */
11
+ export async function getMetadata(key: string): Promise<string | null> {
12
+ const supabase = $provideSupabase();
13
+ const table = await $getTableName('Metadata');
14
+
15
+ const { data } = await supabase
16
+ .from(table)
17
+ .select('value')
18
+ .eq('key', key)
19
+ .single();
20
+
21
+ if (data) {
22
+ return data.value;
23
+ }
24
+
25
+ const defaultValue = metadataDefaults.find((m) => m.key === key);
26
+ if (defaultValue) {
27
+ return defaultValue.value;
28
+ }
29
+
30
+ return null;
31
+ }
@@ -0,0 +1,32 @@
1
+ export const metadataDefaults = [
2
+ {
3
+ key: 'SERVER_NAME',
4
+ value: 'Promptbook Agents Server',
5
+ note: 'The name of the server displayed in the heading bar',
6
+ },
7
+ {
8
+ key: 'SERVER_DESCRIPTION',
9
+ value: 'Agents server powered by Promptbook',
10
+ note: 'The description of the server displayed in the search engine results',
11
+ },
12
+ {
13
+ key: 'SERVER_URL',
14
+ value: 'https://ptbk.io',
15
+ note: 'The URL of the server',
16
+ },
17
+ {
18
+ key: 'SERVER_LOGO_URL',
19
+ value: '',
20
+ note: 'The URL of the logo displayed in the heading bar',
21
+ },
22
+ {
23
+ key: 'SERVER_FAVICON_URL',
24
+ value: '',
25
+ note: 'The URL of the favicon',
26
+ },
27
+ {
28
+ key: 'RESTRICT_IP',
29
+ value: '',
30
+ note: 'Comma separated list of allowed IPs or CIDR ranges. If set, only clients from these IPs are allowed to access the server.',
31
+ },
32
+ ] as const;
@@ -0,0 +1,179 @@
1
+
2
+ -- Note: This is primary source of truth for the database schema
3
+ -- In future we want to be compatible with more then Supabase so we keep SQL as main schema definition
4
+ -- To update, search for [💽]
5
+
6
+
7
+ CREATE TABLE IF NOT EXISTS "prefix_Metadata" (
8
+ "id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
9
+ "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
10
+ "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
11
+ "key" TEXT NOT NULL,
12
+ "value" TEXT NOT NULL,
13
+ "note" TEXT NULL,
14
+ CONSTRAINT prefix_Metadata_key_key UNIQUE ("key")
15
+ );
16
+
17
+ ALTER TABLE "prefix_Metadata" ENABLE ROW LEVEL SECURITY;
18
+
19
+
20
+ CREATE TABLE IF NOT EXISTS "prefix_Agent" (
21
+ "id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
22
+ "agentName" TEXT NOT NULL,
23
+
24
+
25
+ "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
26
+ "updatedAt" TIMESTAMP WITH TIME ZONE NULL DEFAULT NULL,
27
+
28
+
29
+ "agentHash" TEXT NOT NULL,
30
+ "agentSource" TEXT NOT NULL,
31
+ "agentProfile" JSONB NOT NULL,
32
+ "promptbookEngineVersion" TEXT NOT NULL,
33
+
34
+ "usage" JSONB NULL,
35
+ "preparedModelRequirements" JSONB NULL,
36
+ "preparedExternals" JSONB NULL
37
+ );
38
+ -- Ensure uniqueness of agentName even on repeated schema application without duplicate constraint creation
39
+ CREATE UNIQUE INDEX IF NOT EXISTS prefix_agent_agentname_key ON "prefix_Agent" ("agentName");
40
+ COMMENT ON COLUMN "prefix_Agent"."agentName" IS 'The unique name of the agent derived from the first line of the `agentSource`, automatically updated on `agentSource` change';
41
+ COMMENT ON COLUMN "prefix_Agent"."agentHash" IS 'The hash of the `agentSource`, automatically updated on `agentSource` change';
42
+ COMMENT ON COLUMN "prefix_Agent"."agentSource" IS 'The source code of the agent';
43
+ COMMENT ON COLUMN "prefix_Agent"."agentProfile" IS 'The profile of the agent generated from the `agentSource`, automatically updated on `agentSource` change <- TODO: [🕛]';
44
+ COMMENT ON COLUMN "prefix_Agent"."promptbookEngineVersion" IS 'The version of the Promptbook engine used for last update of the agent';
45
+ COMMENT ON COLUMN "prefix_Agent"."usage" IS 'Usage and spending statistics for the agent';
46
+ COMMENT ON COLUMN "prefix_Agent"."preparedModelRequirements" IS 'The prepared model requirements for the agent, generated from the `agentSource` but not after every change (only on explicit request), there is `agentHash` to identify the version of the `agentSource` this was prepared from';
47
+ COMMENT ON COLUMN "prefix_Agent"."preparedExternals" IS 'The prepared externals for the agent, generated from the `agentSource` but not after every change (only on explicit request), there is `agentHash` to identify the version of the `agentSource` this was prepared from';
48
+
49
+ ALTER TABLE "prefix_Agent" ENABLE ROW LEVEL SECURITY;
50
+
51
+
52
+ CREATE TABLE IF NOT EXISTS "prefix_AgentHistory" (
53
+ "id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
54
+ "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
55
+
56
+ "agentName" TEXT NOT NULL,
57
+ "agentHash" TEXT NOT NULL,
58
+ "previousAgentHash" TEXT NULL,
59
+ "agentSource" TEXT NOT NULL,
60
+ "promptbookEngineVersion" TEXT NOT NULL
61
+
62
+ );
63
+
64
+ CREATE INDEX IF NOT EXISTS "prefix_AgentHistory_agentName_idx" ON "prefix_AgentHistory" ("agentName");
65
+ CREATE INDEX IF NOT EXISTS "prefix_AgentHistory_agentHash_idx" ON "prefix_AgentHistory" ("agentHash");
66
+
67
+ ALTER TABLE "prefix_AgentHistory" ENABLE ROW LEVEL SECURITY;
68
+
69
+
70
+ CREATE TABLE IF NOT EXISTS "prefix_ChatHistory" (
71
+ "id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
72
+ "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
73
+
74
+ "messageHash" TEXT NOT NULL,
75
+ "previousMessageHash" TEXT NULL,
76
+ "agentName" TEXT NOT NULL,
77
+ "agentHash" TEXT NOT NULL,
78
+ "message" JSONB NOT NULL,
79
+
80
+ -- Telemetry:
81
+ "promptbookEngineVersion" TEXT NULL,
82
+ "url" TEXT NULL,
83
+ "ip" TEXT NULL,
84
+ "userAgent" TEXT NULL,
85
+ "language" TEXT NULL,
86
+ "platform" TEXT NULL
87
+ );
88
+ COMMENT ON COLUMN "prefix_ChatHistory"."url" IS 'The URL where the chat was happening';
89
+ COMMENT ON COLUMN "prefix_ChatHistory"."ip" IS 'The IP address of the user';
90
+ COMMENT ON COLUMN "prefix_ChatHistory"."userAgent" IS 'The user agent (browser) of the user';
91
+ COMMENT ON COLUMN "prefix_ChatHistory"."language" IS 'The language (from the browser) of the user';
92
+ COMMENT ON COLUMN "prefix_ChatHistory"."platform" IS 'The platform of the user';
93
+
94
+ ALTER TABLE "prefix_ChatHistory" ENABLE ROW LEVEL SECURITY;
95
+
96
+
97
+ CREATE TABLE IF NOT EXISTS "prefix_ChatFeedback" (
98
+ "id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
99
+ "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
100
+
101
+ "agentName" TEXT NOT NULL,
102
+ "agentHash" TEXT NOT NULL,
103
+ "rating" TEXT NULL,
104
+ "textRating" TEXT NULL,
105
+ "chatThread" TEXT NULL,
106
+ "userNote" TEXT NULL,
107
+ "expectedAnswer" TEXT NULL,
108
+
109
+ -- Telemetry:
110
+ "promptbookEngineVersion" TEXT NULL,
111
+ "url" TEXT NULL,
112
+ "ip" TEXT NULL,
113
+ "userAgent" TEXT NULL,
114
+ "language" TEXT NULL,
115
+ "platform" TEXT NULL
116
+ );
117
+ COMMENT ON COLUMN "prefix_ChatFeedback"."url" IS 'The URL where the chat was happening';
118
+ COMMENT ON COLUMN "prefix_ChatFeedback"."ip" IS 'The IP address of the user';
119
+ COMMENT ON COLUMN "prefix_ChatFeedback"."userAgent" IS 'The user agent (browser) of the user';
120
+ COMMENT ON COLUMN "prefix_ChatFeedback"."language" IS 'The language (from the browser) of the user';
121
+ COMMENT ON COLUMN "prefix_ChatFeedback"."platform" IS 'The platform of the user';
122
+
123
+ ALTER TABLE "prefix_ChatFeedback" ENABLE ROW LEVEL SECURITY;
124
+
125
+
126
+ CREATE TABLE IF NOT EXISTS "prefix_User" (
127
+ "id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
128
+ "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
129
+ "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
130
+
131
+ "username" TEXT NOT NULL,
132
+ "passwordHash" TEXT NOT NULL,
133
+ "isAdmin" BOOLEAN NOT NULL DEFAULT FALSE
134
+ );
135
+ CREATE UNIQUE INDEX IF NOT EXISTS "prefix_User_username_idx" ON "prefix_User" ("username");
136
+
137
+ ALTER TABLE "prefix_User" ENABLE ROW LEVEL SECURITY;
138
+
139
+
140
+ /*
141
+ CREATE TABLE IF NOT EXISTS "prefix_AgentActionHistory" (
142
+ id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
143
+ createdAt TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
144
+
145
+ );
146
+ */
147
+
148
+
149
+ /*
150
+ CREATE TABLE IF NOT EXISTS "prefix_Xxx" (
151
+ id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
152
+ );
153
+ */
154
+
155
+
156
+ -- Foreign key relationships
157
+ -- Note: Using agentName as the foreign key reference since it's the unique identifier used across tables
158
+ ALTER TABLE "prefix_AgentHistory"
159
+ DROP CONSTRAINT IF EXISTS "prefix_AgentHistory_agentName_fkey",
160
+ ADD CONSTRAINT "prefix_AgentHistory_agentName_fkey"
161
+ FOREIGN KEY ("agentName")
162
+ REFERENCES "prefix_Agent"("agentName")
163
+ ON DELETE CASCADE;
164
+
165
+ ALTER TABLE "prefix_ChatHistory"
166
+ DROP CONSTRAINT IF EXISTS "prefix_ChatHistory_agentName_fkey",
167
+ ADD CONSTRAINT "prefix_ChatHistory_agentName_fkey"
168
+ FOREIGN KEY ("agentName")
169
+ REFERENCES "prefix_Agent"("agentName")
170
+ ON DELETE CASCADE;
171
+
172
+ ALTER TABLE "prefix_ChatFeedback"
173
+ DROP CONSTRAINT IF EXISTS "prefix_ChatFeedback_agentName_fkey",
174
+ ADD CONSTRAINT "prefix_ChatFeedback_agentName_fkey"
175
+ FOREIGN KEY ("agentName")
176
+ REFERENCES "prefix_Agent"("agentName")
177
+ ON DELETE CASCADE;
178
+
179
+ -- TODO: [🐱‍🚀] Create propper database migrations