@meistrari/agent-sdk 0.1.3 → 0.3.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { z } from 'zod';
1
+ import { E as ExecuteAgentRequest, a as ExecuteAgentResponse, U as UpdateAgentModelRequest, b as UpdateAgentModelResponse, S as SessionTimelineResponse, c as SessionStreamEvent } from './shared/agent-sdk.4ad2c746.js';
2
+ export { A as AgentInput, d as SessionStatus, T as TimelineEvent, e as TimelineMetrics, f as TimelinePrompt, g as TimelineRunTurnMetrics, h as TimelineSpan, i as TimelineToolResult } from './shared/agent-sdk.4ad2c746.js';
3
+ import 'zod';
2
4
 
3
5
  interface AuthStrategy {
4
6
  getHeaders: () => Headers;
@@ -14,107 +16,6 @@ declare class APIKeyAuthStrategy implements AuthStrategy {
14
16
  getHeaders(): Headers;
15
17
  }
16
18
 
17
- declare const sessionStatusSchema: z.ZodEnum<{
18
- pending: "pending";
19
- running: "running";
20
- completed: "completed";
21
- failed: "failed";
22
- waiting_messages: "waiting_messages";
23
- cancelled: "cancelled";
24
- }>;
25
- type SessionStatus = z.infer<typeof sessionStatusSchema>;
26
- declare const agentInputSchema: z.ZodObject<{
27
- vaultRef: z.ZodString;
28
- filename: z.ZodString;
29
- metadata: z.ZodOptional<z.ZodString>;
30
- }, z.core.$strict>;
31
- type AgentInput = z.infer<typeof agentInputSchema>;
32
- declare const executeAgentRequestSchema: z.ZodObject<{
33
- sessionId: z.ZodOptional<z.ZodString>;
34
- organizationName: z.ZodOptional<z.ZodString>;
35
- repository: z.ZodOptional<z.ZodString>;
36
- ref: z.ZodOptional<z.ZodString>;
37
- message: z.ZodOptional<z.ZodString>;
38
- inputs: z.ZodOptional<z.ZodArray<z.ZodObject<{
39
- vaultRef: z.ZodString;
40
- filename: z.ZodString;
41
- metadata: z.ZodOptional<z.ZodString>;
42
- }, z.core.$strict>>>;
43
- environmentVariables: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
44
- recover: z.ZodOptional<z.ZodBoolean>;
45
- }, z.core.$strict>;
46
- type ExecuteAgentRequest = z.infer<typeof executeAgentRequestSchema>;
47
- declare const executeAgentResponseSchema: z.ZodObject<{
48
- success: z.ZodBoolean;
49
- sessionId: z.ZodOptional<z.ZodString>;
50
- error: z.ZodOptional<z.ZodString>;
51
- }, z.core.$strip>;
52
- type ExecuteAgentResponse = z.infer<typeof executeAgentResponseSchema>;
53
- declare const sessionStreamEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
54
- kind: z.ZodLiteral<"status">;
55
- sessionId: z.ZodString;
56
- status: z.ZodEnum<{
57
- pending: "pending";
58
- running: "running";
59
- completed: "completed";
60
- failed: "failed";
61
- waiting_messages: "waiting_messages";
62
- cancelled: "cancelled";
63
- }>;
64
- error: z.ZodOptional<z.ZodString>;
65
- createdAt: z.ZodNumber;
66
- updatedAt: z.ZodNumber;
67
- }, z.core.$strip>, z.ZodObject<{
68
- kind: z.ZodLiteral<"steps">;
69
- sessionId: z.ZodString;
70
- status: z.ZodEnum<{
71
- pending: "pending";
72
- running: "running";
73
- completed: "completed";
74
- failed: "failed";
75
- waiting_messages: "waiting_messages";
76
- cancelled: "cancelled";
77
- }>;
78
- steps: z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
79
- nextCursor: z.ZodNullable<z.ZodNumber>;
80
- createdAt: z.ZodNumber;
81
- updatedAt: z.ZodNumber;
82
- }, z.core.$strip>, z.ZodObject<{
83
- kind: z.ZodLiteral<"result">;
84
- sessionId: z.ZodString;
85
- status: z.ZodEnum<{
86
- completed: "completed";
87
- waiting_messages: "waiting_messages";
88
- }>;
89
- result: z.ZodRecord<z.ZodString, z.ZodUnknown>;
90
- nextCursor: z.ZodNullable<z.ZodNumber>;
91
- createdAt: z.ZodNumber;
92
- updatedAt: z.ZodNumber;
93
- }, z.core.$strip>, z.ZodObject<{
94
- kind: z.ZodLiteral<"timeline-finalize">;
95
- sessionId: z.ZodString;
96
- status: z.ZodEnum<{
97
- pending: "pending";
98
- running: "running";
99
- completed: "completed";
100
- failed: "failed";
101
- waiting_messages: "waiting_messages";
102
- cancelled: "cancelled";
103
- }>;
104
- metrics: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
105
- prompt: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
106
- spans: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>>>;
107
- events: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>>>;
108
- continuation: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
109
- createdAt: z.ZodNumber;
110
- updatedAt: z.ZodNumber;
111
- }, z.core.$strip>, z.ZodObject<{
112
- kind: z.ZodLiteral<"error">;
113
- sessionId: z.ZodString;
114
- error: z.ZodString;
115
- }, z.core.$strip>], "kind">;
116
- type SessionStreamEvent = z.infer<typeof sessionStreamEventSchema>;
117
-
118
19
  interface AgentClientConfig {
119
20
  baseUrl: string;
120
21
  authStrategy: AuthStrategy;
@@ -124,6 +25,9 @@ interface StreamSessionOptions {
124
25
  cursor?: number;
125
26
  signal?: AbortSignal;
126
27
  }
28
+ interface FetchTimelineOptions {
29
+ signal?: AbortSignal;
30
+ }
127
31
  type ResolveReferenceAs = 'bytes' | 'stream' | 'json';
128
32
  interface ResolveReferenceOptions {
129
33
  as?: ResolveReferenceAs;
@@ -132,6 +36,8 @@ interface ResolveReferenceOptions {
132
36
 
133
37
  declare function agentClient(config: AgentClientConfig): {
134
38
  executeAgent: (input: ExecuteAgentRequest) => Promise<ExecuteAgentResponse>;
39
+ updateAgentModel: (input: UpdateAgentModelRequest) => Promise<UpdateAgentModelResponse>;
40
+ fetchTimeline: (sessionId: string, options?: FetchTimelineOptions) => Promise<SessionTimelineResponse>;
135
41
  streamSession: (sessionId: string, options?: StreamSessionOptions) => Promise<AsyncIterable<SessionStreamEvent>>;
136
42
  resolveReference: {
137
43
  (reference: string, options?: {
@@ -155,11 +61,22 @@ declare class FetchError extends Error {
155
61
  readonly url: string;
156
62
  readonly method: string;
157
63
  readonly response: Response;
64
+ readonly kind: "http";
158
65
  constructor(message: string, url: string, method: string, response: Response);
159
66
  static from(url: string, method: string, response: Response): Promise<FetchError>;
160
67
  }
68
+ declare class NetworkError extends Error {
69
+ readonly url: string;
70
+ readonly method: string;
71
+ readonly cause: unknown;
72
+ readonly kind: "network";
73
+ constructor(url: string, method: string, cause: unknown);
74
+ }
75
+ type AgentRequestError = FetchError | NetworkError;
76
+ declare function isFetchError(error: unknown): error is FetchError;
77
+ declare function isNetworkError(error: unknown): error is NetworkError;
161
78
 
162
79
  declare function parseSessionStream(stream: ReadableStream<Uint8Array>): AsyncIterable<SessionStreamEvent>;
163
80
 
164
- export { APIKeyAuthStrategy, DataTokenAuthStrategy, FetchError, agentClient, agentInputSchema, executeAgentRequestSchema, executeAgentResponseSchema, parseSessionStream, sessionStatusSchema, sessionStreamEventSchema };
165
- export type { AgentClient, AgentClientConfig, AgentInput, AuthStrategy, ExecuteAgentRequest, ExecuteAgentResponse, ResolveReferenceAs, ResolveReferenceOptions, SessionStatus, SessionStreamEvent, StreamSessionOptions };
81
+ export { APIKeyAuthStrategy, DataTokenAuthStrategy, ExecuteAgentRequest, ExecuteAgentResponse, FetchError, NetworkError, SessionStreamEvent, SessionTimelineResponse, UpdateAgentModelRequest, UpdateAgentModelResponse, agentClient, isFetchError, isNetworkError, parseSessionStream };
82
+ export type { AgentClient, AgentClientConfig, AgentRequestError, AuthStrategy, FetchTimelineOptions, ResolveReferenceAs, ResolveReferenceOptions, StreamSessionOptions };
package/dist/index.mjs CHANGED
@@ -1,15 +1,16 @@
1
1
  import { VaultFile } from '@meistrari/vault-sdk';
2
- import { z } from 'zod';
2
+ import { SESSION_STREAM_EVENT_KINDS, sessionStreamEventSchema, executeAgentRequestSchema, executeAgentResponseSchema, updateAgentModelRequestSchema, updateAgentModelResponseSchema, sessionTimelineIdSchema, sessionTimelineResponseSchema } from './schemas.mjs';
3
+ import 'zod';
3
4
 
4
- var __defProp = Object.defineProperty;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __publicField = (obj, key, value) => {
7
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ var __defProp$1 = Object.defineProperty;
6
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __publicField$1 = (obj, key, value) => {
8
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
8
9
  return value;
9
10
  };
10
11
  class DataTokenAuthStrategy {
11
12
  constructor(dataToken) {
12
- __publicField(this, "dataToken");
13
+ __publicField$1(this, "dataToken");
13
14
  this.dataToken = dataToken;
14
15
  }
15
16
  getHeaders() {
@@ -20,7 +21,7 @@ class DataTokenAuthStrategy {
20
21
  }
21
22
  class APIKeyAuthStrategy {
22
23
  constructor(apiKey) {
23
- __publicField(this, "apiKey");
24
+ __publicField$1(this, "apiKey");
24
25
  this.apiKey = apiKey;
25
26
  }
26
27
  getHeaders() {
@@ -30,6 +31,12 @@ class APIKeyAuthStrategy {
30
31
  }
31
32
  }
32
33
 
34
+ var __defProp = Object.defineProperty;
35
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
36
+ var __publicField = (obj, key, value) => {
37
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
38
+ return value;
39
+ };
33
40
  class FetchError extends Error {
34
41
  constructor(message, url, method, response) {
35
42
  super(message);
@@ -37,6 +44,7 @@ class FetchError extends Error {
37
44
  this.url = url;
38
45
  this.method = method;
39
46
  this.response = response;
47
+ __publicField(this, "kind", "http");
40
48
  this.name = "FetchError";
41
49
  }
42
50
  static async from(url, method, response) {
@@ -52,104 +60,22 @@ ${text}`,
52
60
  );
53
61
  }
54
62
  }
55
-
56
- const sessionStatusSchema = z.enum([
57
- "pending",
58
- "running",
59
- "completed",
60
- "failed",
61
- "waiting_messages",
62
- "cancelled"
63
- ]);
64
- const vaultReferenceSchema = z.string().regex(/^vault:\/\/\S+$/, "vaultRef must start with vault:// and cannot contain whitespace");
65
- const agentInputSchema = z.object({
66
- vaultRef: vaultReferenceSchema,
67
- filename: z.string().min(1, "Filename is required").max(255, "Filename must be 255 characters or less"),
68
- metadata: z.string().max(16384, "Metadata must be 16,384 characters or less").optional()
69
- }).strict();
70
- const executeAgentRequestSchema = z.object({
71
- sessionId: z.string().uuid().optional(),
72
- organizationName: z.string().min(1).max(200).optional(),
73
- repository: z.string().min(1).max(200).optional(),
74
- ref: z.string().max(200).optional(),
75
- message: z.string().min(1).max(8e5).optional(),
76
- inputs: z.array(agentInputSchema).optional(),
77
- environmentVariables: z.record(z.string(), z.string()).optional(),
78
- recover: z.boolean().optional()
79
- }).strict().superRefine((data, ctx) => {
80
- if (!data.sessionId) {
81
- if (!data.organizationName) {
82
- ctx.addIssue({ code: "custom", message: "organizationName is required for new sessions", path: ["organizationName"] });
83
- }
84
- if (!data.repository) {
85
- ctx.addIssue({ code: "custom", message: "repository is required for new sessions", path: ["repository"] });
86
- }
87
- if (!data.message) {
88
- ctx.addIssue({ code: "custom", message: "message is required for new sessions", path: ["message"] });
89
- }
90
- } else if (!data.recover && !data.message) {
91
- ctx.addIssue({ code: "custom", message: "message is required for session continuations", path: ["message"] });
92
- }
93
- if (data.recover && !data.sessionId) {
94
- ctx.addIssue({ code: "custom", message: "sessionId is required when recover is true", path: ["sessionId"] });
63
+ class NetworkError extends Error {
64
+ constructor(url, method, cause) {
65
+ super(`Failed to ${method} ${url}: ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
66
+ this.url = url;
67
+ this.method = method;
68
+ this.cause = cause;
69
+ __publicField(this, "kind", "network");
70
+ this.name = "NetworkError";
95
71
  }
96
- });
97
- const executeAgentResponseSchema = z.object({
98
- success: z.boolean(),
99
- sessionId: z.string().optional(),
100
- error: z.string().optional()
101
- });
102
- const sessionStatusEventSchema = z.object({
103
- kind: z.literal("status"),
104
- sessionId: z.string(),
105
- status: sessionStatusSchema,
106
- error: z.string().optional(),
107
- createdAt: z.number(),
108
- updatedAt: z.number()
109
- });
110
- const sessionStepsEventSchema = z.object({
111
- kind: z.literal("steps"),
112
- sessionId: z.string(),
113
- status: sessionStatusSchema,
114
- steps: z.array(z.record(z.string(), z.unknown())),
115
- nextCursor: z.number().nullable(),
116
- createdAt: z.number(),
117
- updatedAt: z.number()
118
- });
119
- const sessionResultEventSchema = z.object({
120
- kind: z.literal("result"),
121
- sessionId: z.string(),
122
- status: z.enum(["completed", "waiting_messages"]),
123
- result: z.record(z.string(), z.unknown()),
124
- nextCursor: z.number().nullable(),
125
- createdAt: z.number(),
126
- updatedAt: z.number()
127
- });
128
- const sessionErrorEventSchema = z.object({
129
- kind: z.literal("error"),
130
- sessionId: z.string(),
131
- error: z.string()
132
- });
133
- const sessionTimelineFinalizeEventSchema = z.object({
134
- kind: z.literal("timeline-finalize"),
135
- sessionId: z.string(),
136
- status: sessionStatusSchema,
137
- metrics: z.record(z.string(), z.unknown()).nullable().optional(),
138
- prompt: z.record(z.string(), z.unknown()).nullable().optional(),
139
- spans: z.array(z.record(z.string(), z.unknown())).nullable().optional(),
140
- events: z.array(z.record(z.string(), z.unknown())).nullable().optional(),
141
- continuation: z.record(z.string(), z.unknown()).nullable().optional(),
142
- createdAt: z.number(),
143
- updatedAt: z.number()
144
- });
145
- const sessionStreamEventSchema = z.discriminatedUnion("kind", [
146
- sessionStatusEventSchema,
147
- sessionStepsEventSchema,
148
- sessionResultEventSchema,
149
- sessionTimelineFinalizeEventSchema,
150
- sessionErrorEventSchema
151
- ]);
152
- const SESSION_STREAM_EVENT_KINDS = /* @__PURE__ */ new Set(["status", "steps", "result", "timeline-finalize", "error"]);
72
+ }
73
+ function isFetchError(error) {
74
+ return error instanceof FetchError;
75
+ }
76
+ function isNetworkError(error) {
77
+ return error instanceof NetworkError;
78
+ }
153
79
 
154
80
  function normalizeStreamBuffer(buffer) {
155
81
  return buffer.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
@@ -259,12 +185,19 @@ function agentClient(config) {
259
185
  headers.set("Accept", params.accept ?? "application/json");
260
186
  if (params.body !== void 0)
261
187
  headers.set("Content-Type", "application/json");
262
- const response = await fetch(url, {
263
- method: params.method,
264
- headers,
265
- body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
266
- signal: params.signal
267
- });
188
+ let response;
189
+ try {
190
+ response = await fetch(url, {
191
+ method: params.method,
192
+ headers,
193
+ body: params.body !== void 0 ? JSON.stringify(params.body) : void 0,
194
+ signal: params.signal
195
+ });
196
+ } catch (error) {
197
+ if (error instanceof DOMException && error.name === "AbortError")
198
+ throw error;
199
+ throw new NetworkError(url.toString(), params.method, error);
200
+ }
268
201
  if (!response.ok)
269
202
  throw await FetchError.from(url.toString(), params.method, response);
270
203
  return response;
@@ -278,6 +211,24 @@ function agentClient(config) {
278
211
  });
279
212
  return executeAgentResponseSchema.parse(await response.json());
280
213
  }
214
+ async function updateAgentModel(input) {
215
+ const { repository, ...body } = updateAgentModelRequestSchema.parse(input);
216
+ const response = await request({
217
+ method: "PUT",
218
+ path: `/v4/agents/${encodeURIComponent(repository)}/model`,
219
+ body
220
+ });
221
+ return updateAgentModelResponseSchema.parse(await response.json());
222
+ }
223
+ async function fetchTimeline(sessionId, options) {
224
+ const parsedSessionId = sessionTimelineIdSchema.parse(sessionId);
225
+ const response = await request({
226
+ method: "GET",
227
+ path: `/v4/sessions/${encodeURIComponent(parsedSessionId)}/timeline`,
228
+ signal: options?.signal
229
+ });
230
+ return sessionTimelineResponseSchema.parse(await response.json());
231
+ }
281
232
  async function streamSession(sessionId, options) {
282
233
  const response = await request({
283
234
  method: "GET",
@@ -314,7 +265,7 @@ function agentClient(config) {
314
265
  return JSON.parse(await vaultFile.content.text());
315
266
  return vaultFile.content.arrayBuffer();
316
267
  }
317
- return { executeAgent, streamSession, resolveReference };
268
+ return { executeAgent, updateAgentModel, fetchTimeline, streamSession, resolveReference };
318
269
  }
319
270
 
320
- export { APIKeyAuthStrategy, DataTokenAuthStrategy, FetchError, agentClient, agentInputSchema, executeAgentRequestSchema, executeAgentResponseSchema, parseSessionStream, sessionStatusSchema, sessionStreamEventSchema };
271
+ export { APIKeyAuthStrategy, DataTokenAuthStrategy, FetchError, NetworkError, agentClient, isFetchError, isNetworkError, parseSessionStream };