@promptbook/cli 0.103.0-48 → 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 (110) 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 -24
  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 +4 -5
  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} +4 -4
  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 +109 -106
  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 -3
  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 +2 -2
  55. package/apps/agents-server/src/database/$provideSupabaseForServer.ts +3 -3
  56. package/apps/agents-server/src/database/getMetadata.ts +31 -0
  57. package/apps/agents-server/src/database/metadataDefaults.ts +32 -0
  58. package/apps/agents-server/src/database/schema.sql +81 -33
  59. package/apps/agents-server/src/database/schema.ts +35 -1
  60. package/apps/agents-server/src/middleware.ts +162 -0
  61. package/apps/agents-server/src/tools/$provideAgentCollectionForServer.ts +11 -7
  62. package/apps/agents-server/src/tools/$provideCdnForServer.ts +1 -1
  63. package/apps/agents-server/src/tools/$provideExecutionToolsForServer.ts +11 -13
  64. package/apps/agents-server/src/tools/$provideOpenAiAssistantExecutionToolsForServer.ts +7 -7
  65. package/apps/agents-server/src/tools/$provideServer.ts +39 -0
  66. package/apps/agents-server/src/utils/auth.ts +33 -0
  67. package/apps/agents-server/src/utils/cdn/utils/nameToSubfolderPath.ts +1 -1
  68. package/apps/agents-server/src/utils/getCurrentUser.ts +32 -0
  69. package/apps/agents-server/src/utils/isIpAllowed.ts +101 -0
  70. package/apps/agents-server/src/utils/isUserAdmin.ts +31 -0
  71. package/apps/agents-server/src/utils/session.ts +50 -0
  72. package/apps/agents-server/tailwind.config.ts +2 -0
  73. package/esm/index.es.js +147 -31
  74. package/esm/index.es.js.map +1 -1
  75. package/esm/typings/servers.d.ts +1 -0
  76. package/esm/typings/src/_packages/types.index.d.ts +2 -0
  77. package/esm/typings/src/_packages/utils.index.d.ts +2 -0
  78. package/esm/typings/src/book-2.0/agent-source/AgentBasicInformation.d.ts +12 -2
  79. package/esm/typings/src/collection/agent-collection/constructors/agent-collection-in-supabase/AgentCollectionInSupabase.d.ts +14 -8
  80. package/esm/typings/src/collection/agent-collection/constructors/agent-collection-in-supabase/AgentCollectionInSupabaseOptions.d.ts +10 -0
  81. package/esm/typings/src/commitments/MESSAGE/InitialMessageCommitmentDefinition.d.ts +28 -0
  82. package/esm/typings/src/commitments/index.d.ts +2 -1
  83. package/esm/typings/src/config.d.ts +1 -0
  84. package/esm/typings/src/errors/DatabaseError.d.ts +2 -2
  85. package/esm/typings/src/errors/WrappedError.d.ts +2 -2
  86. package/esm/typings/src/execution/ExecutionTask.d.ts +2 -2
  87. package/esm/typings/src/execution/LlmExecutionTools.d.ts +6 -1
  88. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsForWizardOrCli.d.ts +2 -2
  89. package/esm/typings/src/llm-providers/agent/Agent.d.ts +11 -3
  90. package/esm/typings/src/llm-providers/agent/AgentLlmExecutionTools.d.ts +6 -1
  91. package/esm/typings/src/llm-providers/agent/RemoteAgent.d.ts +6 -2
  92. package/esm/typings/src/llm-providers/openai/OpenAiAssistantExecutionTools.d.ts +6 -1
  93. package/esm/typings/src/remote-server/startAgentServer.d.ts +2 -2
  94. package/esm/typings/src/utils/color/Color.d.ts +7 -0
  95. package/esm/typings/src/utils/color/Color.test.d.ts +1 -0
  96. package/esm/typings/src/utils/environment/$getGlobalScope.d.ts +2 -2
  97. package/esm/typings/src/utils/misc/computeHash.d.ts +11 -0
  98. package/esm/typings/src/utils/misc/computeHash.test.d.ts +1 -0
  99. package/esm/typings/src/utils/organization/$sideEffect.d.ts +2 -2
  100. package/esm/typings/src/utils/organization/$side_effect.d.ts +2 -2
  101. package/esm/typings/src/utils/organization/TODO_USE.d.ts +2 -2
  102. package/esm/typings/src/utils/organization/keepUnused.d.ts +2 -2
  103. package/esm/typings/src/utils/organization/preserve.d.ts +3 -3
  104. package/esm/typings/src/utils/organization/really_any.d.ts +7 -0
  105. package/esm/typings/src/utils/serialization/asSerializable.d.ts +2 -2
  106. package/esm/typings/src/version.d.ts +1 -1
  107. package/package.json +1 -1
  108. package/umd/index.umd.js +147 -31
  109. package/umd/index.umd.js.map +1 -1
  110. package/apps/agents-server/config.ts.todo +0 -38
@@ -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
+ }
@@ -1,9 +1,9 @@
1
- import type { AgentsDatabaseSchema } from '@promptbook-local/types';
2
1
  import { $isRunningInBrowser, $isRunningInNode, $isRunningInWebWorker } from '@promptbook-local/utils';
3
2
  import type { SupabaseClient } from '@supabase/supabase-js';
4
3
  import { $provideSupabaseForBrowser } from './$provideSupabaseForBrowser';
5
4
  import { $provideSupabaseForServer } from './$provideSupabaseForServer';
6
5
  import { $provideSupabaseForWorker } from './$provideSupabaseForWorker';
6
+ import { AgentsServerDatabase } from './schema';
7
7
 
8
8
  /**
9
9
  * Get supabase client in any environment
@@ -12,7 +12,7 @@ import { $provideSupabaseForWorker } from './$provideSupabaseForWorker';
12
12
  *
13
13
  * @returns instance of supabase client
14
14
  */
15
- export function $provideSupabase(): SupabaseClient<AgentsDatabaseSchema> {
15
+ export function $provideSupabase(): SupabaseClient<AgentsServerDatabase> {
16
16
  if ($isRunningInNode()) {
17
17
  return $provideSupabaseForServer();
18
18
  } else if ($isRunningInBrowser()) {
@@ -1,6 +1,6 @@
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
6
  * Internal cache for `$provideSupabaseForServer`
@@ -8,7 +8,7 @@ import { createClient, SupabaseClient } from '@supabase/supabase-js';
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
@@ -27,7 +27,7 @@ export function $provideSupabaseForServer(): typeof supabase {
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
  {
@@ -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;
@@ -4,17 +4,20 @@
4
4
  -- To update, search for [💽]
5
5
 
6
6
 
7
-
8
- CREATE TABLE IF NOT EXISTS "EnvironmentVariable" (
7
+ CREATE TABLE IF NOT EXISTS "prefix_Metadata" (
9
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(),
10
11
  "key" TEXT NOT NULL,
11
12
  "value" TEXT NOT NULL,
12
13
  "note" TEXT NULL,
13
- CONSTRAINT EnvironmentVariable_key_key UNIQUE ("key")
14
+ CONSTRAINT prefix_Metadata_key_key UNIQUE ("key")
14
15
  );
15
16
 
17
+ ALTER TABLE "prefix_Metadata" ENABLE ROW LEVEL SECURITY;
16
18
 
17
- CREATE TABLE IF NOT EXISTS "Agent" (
19
+
20
+ CREATE TABLE IF NOT EXISTS "prefix_Agent" (
18
21
  "id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
19
22
  "agentName" TEXT NOT NULL,
20
23
 
@@ -33,18 +36,20 @@ CREATE TABLE IF NOT EXISTS "Agent" (
33
36
  "preparedExternals" JSONB NULL
34
37
  );
35
38
  -- Ensure uniqueness of agentName even on repeated schema application without duplicate constraint creation
36
- CREATE UNIQUE INDEX IF NOT EXISTS agent_agentname_key ON "Agent" ("agentName");
37
- COMMENT ON COLUMN "Agent"."agentName" IS 'The unique name of the agent derived from the first line of the `agentSource`, automatically updated on `agentSource` change';
38
- COMMENT ON COLUMN "Agent"."agentHash" IS 'The hash of the `agentSource`, automatically updated on `agentSource` change';
39
- COMMENT ON COLUMN "Agent"."agentSource" IS 'The source code of the agent';
40
- COMMENT ON COLUMN "Agent"."agentProfile" IS 'The profile of the agent generated from the `agentSource`, automatically updated on `agentSource` change <- TODO: [🕛]';
41
- COMMENT ON COLUMN "Agent"."promptbookEngineVersion" IS 'The version of the Promptbook engine used for last update of the agent';
42
- COMMENT ON COLUMN "Agent"."usage" IS 'Usage and spending statistics for the agent';
43
- COMMENT ON COLUMN "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';
44
- COMMENT ON COLUMN "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';
45
-
46
-
47
- CREATE TABLE IF NOT EXISTS "AgentHistory" (
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" (
48
53
  "id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
49
54
  "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
50
55
 
@@ -56,11 +61,13 @@ CREATE TABLE IF NOT EXISTS "AgentHistory" (
56
61
 
57
62
  );
58
63
 
59
- CREATE INDEX IF NOT EXISTS "AgentHistory_agentName_idx" ON "AgentHistory" ("agentName");
60
- CREATE INDEX IF NOT EXISTS "AgentHistory_agentHash_idx" ON "AgentHistory" ("agentHash");
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");
61
66
 
67
+ ALTER TABLE "prefix_AgentHistory" ENABLE ROW LEVEL SECURITY;
62
68
 
63
- CREATE TABLE IF NOT EXISTS "ChatHistory" (
69
+
70
+ CREATE TABLE IF NOT EXISTS "prefix_ChatHistory" (
64
71
  "id" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
65
72
  "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
66
73
 
@@ -78,14 +85,16 @@ CREATE TABLE IF NOT EXISTS "ChatHistory" (
78
85
  "language" TEXT NULL,
79
86
  "platform" TEXT NULL
80
87
  );
81
- COMMENT ON COLUMN "ChatHistory"."url" IS 'The URL where the chat was happening';
82
- COMMENT ON COLUMN "ChatHistory"."ip" IS 'The IP address of the user';
83
- COMMENT ON COLUMN "ChatHistory"."userAgent" IS 'The user agent (browser) of the user';
84
- COMMENT ON COLUMN "ChatHistory"."language" IS 'The language (from the browser) of the user';
85
- COMMENT ON COLUMN "ChatHistory"."platform" IS 'The platform of the user';
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;
86
95
 
87
96
 
88
- CREATE TABLE IF NOT EXISTS "ChatFeedback" (
97
+ CREATE TABLE IF NOT EXISTS "prefix_ChatFeedback" (
89
98
  "id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
90
99
  "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
91
100
 
@@ -105,15 +114,31 @@ CREATE TABLE IF NOT EXISTS "ChatFeedback" (
105
114
  "language" TEXT NULL,
106
115
  "platform" TEXT NULL
107
116
  );
108
- COMMENT ON COLUMN "ChatFeedback"."url" IS 'The URL where the chat was happening';
109
- COMMENT ON COLUMN "ChatFeedback"."ip" IS 'The IP address of the user';
110
- COMMENT ON COLUMN "ChatFeedback"."userAgent" IS 'The user agent (browser) of the user';
111
- COMMENT ON COLUMN "ChatFeedback"."language" IS 'The language (from the browser) of the user';
112
- COMMENT ON COLUMN "ChatFeedback"."platform" IS 'The platform of the user';
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;
113
138
 
114
139
 
115
140
  /*
116
- CREATE TABLE IF NOT EXISTS "AgentActionHistory" (
141
+ CREATE TABLE IF NOT EXISTS "prefix_AgentActionHistory" (
117
142
  id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
118
143
  createdAt TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
119
144
 
@@ -122,10 +147,33 @@ createdAt TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
122
147
 
123
148
 
124
149
  /*
125
- CREATE TABLE IF NOT EXISTS "Xxx" (
150
+ CREATE TABLE IF NOT EXISTS "prefix_Xxx" (
126
151
  id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
127
152
  );
128
153
  */
129
154
 
130
155
 
131
- -- TODO: !!!! Create propper database migrations
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
@@ -11,23 +11,30 @@ export type Json = string | number | boolean | null | { [key: string]: Json | un
11
11
 
12
12
  // Public schema database interface (Supabase convention)
13
13
  export type AgentsServerDatabase = {
14
+ // <- TODO: [🧠][🕜] Better naming
14
15
  public: {
15
16
  Tables: {
16
- EnvironmentVariable: {
17
+ Metadata: {
17
18
  Row: {
18
19
  id: number;
20
+ createdAt: string;
21
+ updatedAt: string;
19
22
  key: string;
20
23
  value: string;
21
24
  note: string | null;
22
25
  };
23
26
  Insert: {
24
27
  id?: number;
28
+ createdAt?: string;
29
+ updatedAt?: string;
25
30
  key: string;
26
31
  value: string;
27
32
  note?: string | null;
28
33
  };
29
34
  Update: {
30
35
  id?: number;
36
+ createdAt?: string;
37
+ updatedAt?: string;
31
38
  key?: string;
32
39
  value?: string;
33
40
  note?: string | null;
@@ -208,6 +215,33 @@ export type AgentsServerDatabase = {
208
215
  };
209
216
  Relationships: [];
210
217
  };
218
+ User: {
219
+ Row: {
220
+ id: number;
221
+ createdAt: string;
222
+ updatedAt: string;
223
+ username: string;
224
+ passwordHash: string;
225
+ isAdmin: boolean;
226
+ };
227
+ Insert: {
228
+ id?: number;
229
+ createdAt?: string;
230
+ updatedAt?: string;
231
+ username: string;
232
+ passwordHash: string;
233
+ isAdmin?: boolean;
234
+ };
235
+ Update: {
236
+ id?: number;
237
+ createdAt?: string;
238
+ updatedAt?: string;
239
+ username?: string;
240
+ passwordHash?: string;
241
+ isAdmin?: boolean;
242
+ };
243
+ Relationships: [];
244
+ };
211
245
  };
212
246
  Views: Record<string, never>;
213
247
  Functions: Record<string, never>;