@promptbook/remote-server 0.100.0-1 โ†’ 0.100.0-11

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/esm/index.es.js CHANGED
@@ -33,7 +33,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
33
33
  * @generated
34
34
  * @see https://github.com/webgptorg/promptbook
35
35
  */
36
- const PROMPTBOOK_ENGINE_VERSION = '0.100.0-1';
36
+ const PROMPTBOOK_ENGINE_VERSION = '0.100.0-11';
37
37
  /**
38
38
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
39
39
  * Note: [๐Ÿ’ž] Ignore a discrepancy between file name and entity name
@@ -3822,12 +3822,58 @@ async function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
3822
3822
  // <- TODO: [๐Ÿฅฌ] Encapsulate sha256 to some private utility function
3823
3823
  const rootDirname = join(process.cwd(), DEFAULT_DOWNLOAD_CACHE_DIRNAME);
3824
3824
  const filepath = join(...nameToSubfolderPath(hash /* <- TODO: [๐ŸŽŽ] Maybe add some SHA256 prefix */), `${basename.substring(0, MAX_FILENAME_LENGTH)}.${mimeTypeToExtension(mimeType)}`);
3825
- await tools.fs.mkdir(dirname(join(rootDirname, filepath)), { recursive: true });
3825
+ // Note: Try to create cache directory, but don't fail if filesystem has issues
3826
+ try {
3827
+ await tools.fs.mkdir(dirname(join(rootDirname, filepath)), { recursive: true });
3828
+ }
3829
+ catch (error) {
3830
+ // Note: If we can't create cache directory, we'll handle it when trying to write the file
3831
+ // This handles read-only filesystems, permission issues, and missing parent directories
3832
+ if (error instanceof Error && (error.message.includes('EROFS') ||
3833
+ error.message.includes('read-only') ||
3834
+ error.message.includes('EACCES') ||
3835
+ error.message.includes('EPERM') ||
3836
+ error.message.includes('ENOENT'))) ;
3837
+ else {
3838
+ // Re-throw other unexpected errors
3839
+ throw error;
3840
+ }
3841
+ }
3826
3842
  const fileContent = Buffer.from(await response.arrayBuffer());
3827
3843
  if (fileContent.length > DEFAULT_MAX_FILE_SIZE /* <- TODO: Allow to pass different value to remote server */) {
3828
3844
  throw new LimitReachedError(`File is too large (${Math.round(fileContent.length / 1024 / 1024)}MB). Maximum allowed size is ${Math.round(DEFAULT_MAX_FILE_SIZE / 1024 / 1024)}MB.`);
3829
3845
  }
3830
- await tools.fs.writeFile(join(rootDirname, filepath), fileContent);
3846
+ // Note: Try to cache the downloaded file, but don't fail if the filesystem is read-only
3847
+ try {
3848
+ await tools.fs.writeFile(join(rootDirname, filepath), fileContent);
3849
+ }
3850
+ catch (error) {
3851
+ // Note: If we can't write to cache, we'll process the file directly from memory
3852
+ // This handles read-only filesystems like Vercel
3853
+ if (error instanceof Error && (error.message.includes('EROFS') ||
3854
+ error.message.includes('read-only') ||
3855
+ error.message.includes('EACCES') ||
3856
+ error.message.includes('EPERM') ||
3857
+ error.message.includes('ENOENT'))) {
3858
+ // Return a handler that works directly with the downloaded content
3859
+ return {
3860
+ source: name,
3861
+ filename: null,
3862
+ url,
3863
+ mimeType,
3864
+ async asJson() {
3865
+ return JSON.parse(fileContent.toString('utf-8'));
3866
+ },
3867
+ async asText() {
3868
+ return fileContent.toString('utf-8');
3869
+ },
3870
+ };
3871
+ }
3872
+ else {
3873
+ // Re-throw other unexpected errors
3874
+ throw error;
3875
+ }
3876
+ }
3831
3877
  // TODO: [๐Ÿ’ต] Check the file security
3832
3878
  // TODO: [๐Ÿงน][๐Ÿง ] Delete the file after the scraping is done
3833
3879
  return makeKnowledgeSourceHandler({ name, knowledgeSourceContent: filepath }, tools, {
@@ -8061,152 +8107,6 @@ function startRemoteServer(options) {
8061
8107
  response.status(400).send({ error: serializeError(error) });
8062
8108
  }
8063
8109
  });
8064
- // OAuth Authentication Endpoints
8065
- // These endpoints provide social authentication support (Facebook, Google, etc.)
8066
- app.get('/auth/:provider', async (request, response) => {
8067
- const { provider } = request.params;
8068
- if (!isApplicationModeAllowed) {
8069
- response.status(400).json({
8070
- error: 'Application mode is not allowed',
8071
- message: 'Social authentication requires application mode to be enabled'
8072
- });
8073
- return;
8074
- }
8075
- try {
8076
- // Get OAuth configuration from query params or environment
8077
- const { redirectUri, clientId, appId } = request.query;
8078
- if (!redirectUri || !clientId) {
8079
- response.status(400).json({
8080
- error: 'Missing OAuth parameters',
8081
- message: 'redirectUri and clientId are required for OAuth flow'
8082
- });
8083
- return;
8084
- }
8085
- let authUrl;
8086
- const state = Buffer.from(JSON.stringify({
8087
- appId: appId || 'default',
8088
- timestamp: Date.now()
8089
- })).toString('base64');
8090
- switch (provider.toLowerCase()) {
8091
- case 'facebook':
8092
- authUrl = `https://www.facebook.com/v18.0/dialog/oauth?` +
8093
- `client_id=${encodeURIComponent(clientId)}&` +
8094
- `redirect_uri=${encodeURIComponent(redirectUri)}&` +
8095
- `scope=email,public_profile&` +
8096
- `response_type=code&` +
8097
- `state=${encodeURIComponent(state)}`;
8098
- break;
8099
- case 'google':
8100
- authUrl = `https://accounts.google.com/o/oauth2/v2/auth?` +
8101
- `client_id=${encodeURIComponent(clientId)}&` +
8102
- `redirect_uri=${encodeURIComponent(redirectUri)}&` +
8103
- `scope=openid%20email%20profile&` +
8104
- `response_type=code&` +
8105
- `state=${encodeURIComponent(state)}`;
8106
- break;
8107
- default:
8108
- response.status(400).json({
8109
- error: 'Unsupported provider',
8110
- message: `Social authentication provider '${provider}' is not supported. Supported providers: facebook, google`
8111
- });
8112
- return;
8113
- }
8114
- // Log the OAuth attempt for debugging
8115
- if (isVerbose) {
8116
- console.info(colors.cyan(`OAuth ${provider} flow started for app ${appId || 'default'}`));
8117
- }
8118
- response.json({
8119
- authUrl,
8120
- provider,
8121
- state,
8122
- message: `Redirect user to authUrl to complete ${provider} authentication`
8123
- });
8124
- }
8125
- catch (error) {
8126
- assertsError(error);
8127
- console.warn(`OAuth ${provider} initialization failed:`, error);
8128
- response.status(500).json({
8129
- error: 'OAuth initialization failed',
8130
- message: error.message
8131
- });
8132
- }
8133
- });
8134
- app.post('/auth/:provider/callback', async (request, response) => {
8135
- const { provider } = request.params;
8136
- if (!isApplicationModeAllowed || login === null) {
8137
- response.status(400).json({
8138
- error: 'Application mode is not allowed',
8139
- message: 'Social authentication requires application mode and login handler to be configured'
8140
- });
8141
- return;
8142
- }
8143
- try {
8144
- const { code, state, error: oauthError } = request.body;
8145
- if (oauthError) {
8146
- response.status(400).json({
8147
- isSuccess: false,
8148
- error: 'OAuth authorization failed',
8149
- message: `${provider} authentication was denied or failed: ${oauthError}`
8150
- });
8151
- return;
8152
- }
8153
- if (!code || !state) {
8154
- response.status(400).json({
8155
- isSuccess: false,
8156
- error: 'Missing OAuth callback parameters',
8157
- message: 'code and state parameters are required'
8158
- });
8159
- return;
8160
- }
8161
- // Decode state to get app information
8162
- let appInfo;
8163
- try {
8164
- appInfo = JSON.parse(Buffer.from(state, 'base64').toString());
8165
- }
8166
- catch (_a) {
8167
- response.status(400).json({
8168
- isSuccess: false,
8169
- error: 'Invalid state parameter',
8170
- message: 'The OAuth state parameter is malformed'
8171
- });
8172
- return;
8173
- }
8174
- // Log the OAuth callback for debugging
8175
- if (isVerbose) {
8176
- console.info(colors.cyan(`OAuth ${provider} callback received for app ${appInfo.appId}`));
8177
- }
8178
- // Note: In a real implementation, you would:
8179
- // 1. Exchange the code for an access token with the OAuth provider
8180
- // 2. Use the access token to get user information
8181
- // 3. Create or find the user in your system
8182
- // 4. Call the login function with the user's information
8183
- // For now, we provide a framework that the implementer can extend
8184
- const mockUserInfo = {
8185
- username: `${provider}_user_${code.substring(0, 8)}`,
8186
- password: '',
8187
- appId: appInfo.appId
8188
- };
8189
- const loginResult = await login({
8190
- ...mockUserInfo,
8191
- rawRequest: request,
8192
- rawResponse: response,
8193
- });
8194
- response.status(200).json({
8195
- ...loginResult,
8196
- provider,
8197
- message: loginResult.message || `${provider} authentication completed`,
8198
- });
8199
- }
8200
- catch (error) {
8201
- assertsError(error);
8202
- console.warn(`OAuth ${provider} callback failed:`, error);
8203
- response.status(500).json({
8204
- isSuccess: false,
8205
- error: 'OAuth callback processing failed',
8206
- message: error.message
8207
- });
8208
- }
8209
- });
8210
8110
  app.get(`/books`, async (request, response) => {
8211
8111
  if (collection === null) {
8212
8112
  response.status(500).send('No collection available');