@mastra/client-js 0.1.0-alpha.5 → 0.1.0-alpha.7

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,79 @@
1
+ import type { RequestFunction, RequestOptions, ClientOptions } from '../types';
2
+
3
+ export class BaseResource {
4
+ readonly options: ClientOptions
5
+
6
+ constructor(options: ClientOptions) {
7
+ this.options = options;
8
+ }
9
+
10
+ /**
11
+ * Makes an HTTP request to the API with retries and exponential backoff
12
+ * @param path - The API endpoint path
13
+ * @param options - Optional request configuration
14
+ * @returns Promise containing the response data
15
+ */
16
+ public async request<T>(path: string, options: RequestOptions = {}): Promise<T> {
17
+ let lastError: Error | null = null;
18
+ const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1000, headers = {} } = this.options;
19
+
20
+
21
+ let delay = backoffMs;
22
+
23
+
24
+ for (let attempt = 0; attempt <= retries; attempt++) {
25
+ try {
26
+ const response = await fetch(`${baseUrl}${path}`, {
27
+ ...options,
28
+ headers: {
29
+ 'Content-Type': 'application/json',
30
+ ...headers,
31
+ ...options.headers,
32
+ },
33
+ body: options.body ? JSON.stringify(options.body) : undefined,
34
+ });
35
+
36
+ if (!response.ok) {
37
+ const errorBody = await response.text();
38
+ let errorMessage = `HTTP error! status: ${response.status}`;
39
+ try {
40
+ // Try to parse as JSON for structured error messages
41
+ const errorJson = JSON.parse(errorBody);
42
+ errorMessage += ` - ${JSON.stringify(errorJson)}`;
43
+ } catch {
44
+ // If not JSON, include the raw error body if present
45
+ if (errorBody) {
46
+ errorMessage += ` - ${errorBody}`;
47
+ }
48
+ }
49
+ throw new Error(errorMessage);
50
+ }
51
+
52
+ // For streaming responses, return the stream directly
53
+ if (response.headers.get('Content-Type')?.includes('text/event-stream')) {
54
+ return response.body as T;
55
+ }
56
+
57
+ // For unknown responses, return the response as is
58
+ if (response.headers.get('Content-Type')?.includes('text/x-unknown')) {
59
+ return response as T;
60
+ }
61
+
62
+ const data = await response.json();
63
+ return data as T;
64
+ } catch (error) {
65
+ lastError = error as Error;
66
+
67
+ if (attempt === retries) {
68
+ break;
69
+ }
70
+
71
+ // Wait with exponential backoff
72
+ await new Promise(resolve => setTimeout(resolve, delay));
73
+ delay = Math.min(delay * 2, maxBackoffMs);
74
+ }
75
+ }
76
+
77
+ throw lastError || new Error('Request failed');
78
+ }
79
+ }
@@ -1,5 +1,6 @@
1
1
  export * from './agent';
2
2
  export * from './memory-thread';
3
- export * from './tool';
3
+ export * from './vector';
4
4
  export * from './workflow';
5
- export * from './vector';
5
+ export * from './tool';
6
+ export * from './base';
@@ -1,15 +1,21 @@
1
- import type { StorageThreadType } from '@mastra/core/memory';
1
+ import type { StorageThreadType } from '@mastra/core';
2
2
  import type {
3
+ CreateMemoryThreadParams,
3
4
  GetMemoryThreadMessagesResponse,
5
+ GetMemoryThreadResponse,
6
+ ClientOptions,
7
+ SaveMessageToMemoryParams,
4
8
  UpdateMemoryThreadParams,
5
- RequestFunction
6
9
  } from '../types';
10
+ import { BaseResource } from './base';
7
11
 
8
- export class MemoryThread {
12
+ export class MemoryThread extends BaseResource {
9
13
  constructor(
10
- private request: RequestFunction,
14
+ options: ClientOptions,
11
15
  private threadId: string
12
- ) { }
16
+ ) {
17
+ super(options);
18
+ }
13
19
 
14
20
  /**
15
21
  * Retrieves the memory thread details
@@ -1,10 +1,13 @@
1
- import type { GetToolResponse, RequestFunction } from '../types';
1
+ import type { GetToolResponse, ClientOptions } from '../types';
2
+ import { BaseResource } from './base';
2
3
 
3
- export class Tool {
4
+ export class Tool extends BaseResource {
4
5
  constructor(
5
- private request: RequestFunction,
6
+ options: ClientOptions,
6
7
  private toolId: string
7
- ) { }
8
+ ) {
9
+ super(options);
10
+ }
8
11
 
9
12
  /**
10
13
  * Retrieves details about the tool
@@ -3,15 +3,18 @@ import type {
3
3
  GetVectorIndexResponse,
4
4
  QueryVectorParams,
5
5
  QueryVectorResponse,
6
+ ClientOptions,
6
7
  UpsertVectorParams,
7
- RequestFunction
8
8
  } from '../types';
9
+ import { BaseResource } from './base';
9
10
 
10
- export class Vector {
11
+ export class Vector extends BaseResource {
11
12
  constructor(
12
- private request: RequestFunction,
13
+ options: ClientOptions,
13
14
  private vectorName: string
14
- ) { }
15
+ ) {
16
+ super(options);
17
+ }
15
18
 
16
19
  /**
17
20
  * Retrieves details about a specific vector index
@@ -1,10 +1,13 @@
1
- import type { GetWorkflowResponse, RequestFunction } from '../types';
1
+ import type { GetWorkflowResponse, ClientOptions } from '../types';
2
+ import { BaseResource } from './base';
2
3
 
3
- export class Workflow {
4
+ export class Workflow extends BaseResource {
4
5
  constructor(
5
- private request: RequestFunction,
6
+ options: ClientOptions,
6
7
  private workflowId: string
7
- ) { }
8
+ ) {
9
+ super(options);
10
+ }
8
11
 
9
12
  /**
10
13
  * Retrieves details about the workflow
package/src/types.ts CHANGED
@@ -1,11 +1,17 @@
1
1
  import type { MessageType, AiMessageType, CoreMessage, QueryResult, StepAction, StepGraph, StorageThreadType, BaseLogMessage, OutputType } from "@mastra/core";
2
2
  import type { JSONSchema7 } from 'json-schema';
3
+ import { ZodSchema } from "zod";
3
4
 
4
5
  export interface ClientOptions {
6
+ /** Base URL for API requests */
5
7
  baseUrl: string;
8
+ /** Number of retry attempts for failed requests */
6
9
  retries?: number;
10
+ /** Initial backoff time in milliseconds between retries */
7
11
  backoffMs?: number;
12
+ /** Maximum backoff time in milliseconds between retries */
8
13
  maxBackoffMs?: number;
14
+ /** Custom headers to include with requests */
9
15
  headers?: Record<string, string>;
10
16
  }
11
17
 
@@ -22,14 +28,14 @@ export interface GetAgentResponse {
22
28
  tools: Record<string, GetToolResponse>;
23
29
  }
24
30
 
25
- export interface GenerateParams<T extends JSONSchema7 | undefined = undefined> {
31
+ export interface GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> {
26
32
  messages: MessageType[];
27
33
  threadId?: string;
28
34
  resourceid?: string;
29
35
  output?: OutputType | T
30
36
  }
31
37
 
32
- export interface StreamParams<T extends JSONSchema7 | undefined = undefined> {
38
+ export interface StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> {
33
39
  messages: MessageType[];
34
40
  threadId?: string;
35
41
  resourceid?: string;