@firststep-studio/sdk 0.1.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.
@@ -0,0 +1,438 @@
1
+ /**
2
+ * FirstStep Protocol SDK Types
3
+ *
4
+ * Implement this interface to communicate with FirstStep Studio.
5
+ * GuidedForm is the built-in implementation of this interface.
6
+ */
7
+ interface ProtocolRequest {
8
+ /**
9
+ * User message.
10
+ * Optional - if not provided, this is a session initialization request
11
+ * and the handler should return the initial message (welcome/form).
12
+ */
13
+ message?: string;
14
+ /** Session ID (new session created if not provided) */
15
+ sessionId?: string;
16
+ /** Deployment slug */
17
+ deploymentSlug: string;
18
+ /** Additional metadata */
19
+ metadata?: Record<string, unknown>;
20
+ }
21
+ interface ProtocolResponse {
22
+ /** AI response message */
23
+ message: string;
24
+ /** Session ID */
25
+ sessionId: string;
26
+ /** Current agent ID */
27
+ agentId: string;
28
+ /** Session status */
29
+ sessionStatus: SessionStatus;
30
+ /** Form rendering info (optional) */
31
+ form?: ProtocolForm;
32
+ /** Response metadata */
33
+ metadata?: Record<string, unknown>;
34
+ }
35
+ type SessionStatus = 'active' | 'completed' | 'error';
36
+ interface ProtocolForm {
37
+ /** Form type */
38
+ type: 'question' | 'closing' | 'custom';
39
+ /** Question ID */
40
+ questionId?: string;
41
+ /** Form fields */
42
+ fields: ProtocolFormField[];
43
+ /** Form metadata */
44
+ metadata?: Record<string, unknown>;
45
+ }
46
+ interface ProtocolFormField {
47
+ /** Field ID */
48
+ id: string;
49
+ /** Field type */
50
+ type: 'text' | 'textarea' | 'select' | 'multiselect' | 'date' | 'number' | 'email' | 'phone';
51
+ /** Display label */
52
+ label: string;
53
+ /** Placeholder text */
54
+ placeholder?: string;
55
+ /** Options for select/multiselect */
56
+ options?: ProtocolFormOption[];
57
+ /** Required flag */
58
+ required?: boolean;
59
+ /** Validation rules */
60
+ validation?: ProtocolFieldValidation;
61
+ }
62
+ interface ProtocolFormOption {
63
+ value: string;
64
+ label: string;
65
+ }
66
+ interface ProtocolFieldValidation {
67
+ pattern?: string;
68
+ minLength?: number;
69
+ maxLength?: number;
70
+ min?: number;
71
+ max?: number;
72
+ message?: string;
73
+ }
74
+ interface ProtocolStreamChunk {
75
+ /** Chunk type */
76
+ type: 'text' | 'form' | 'status' | 'error' | 'metadata';
77
+ /** Chunk content */
78
+ content: string | ProtocolForm | SessionStatus | ProtocolError | Record<string, unknown>;
79
+ }
80
+ interface ProtocolError {
81
+ code: string;
82
+ message: string;
83
+ details?: Record<string, unknown>;
84
+ }
85
+ /**
86
+ * Protocol Handler Interface
87
+ *
88
+ * Implement this interface to work as a FirstStep Studio Protocol.
89
+ * GuidedForm is the built-in implementation of this interface.
90
+ */
91
+ interface ProtocolHandler {
92
+ /**
93
+ * Handle message (non-streaming)
94
+ */
95
+ handleMessage(request: ProtocolRequest, context: ProtocolContext): Promise<ProtocolResponse>;
96
+ /**
97
+ * Handle message (streaming)
98
+ */
99
+ handleStream?(request: ProtocolRequest, context: ProtocolContext): AsyncGenerator<ProtocolStreamChunk>;
100
+ /**
101
+ * Declare handler capabilities
102
+ */
103
+ getCapabilities(): ProtocolCapabilities;
104
+ /**
105
+ * Handler metadata returned during handshake.
106
+ * Used to auto-fill project name and description on registration.
107
+ */
108
+ getHandlerInfo?(): HandlerInfo;
109
+ }
110
+ interface HandlerInfo {
111
+ /** Handler display name */
112
+ name: string;
113
+ /** Handler description */
114
+ description?: string;
115
+ }
116
+ interface ProtocolCapabilities {
117
+ /** Streaming support */
118
+ streaming: boolean;
119
+ /** Form questions support */
120
+ formQuestions: boolean;
121
+ /** Knowledge actions usage */
122
+ knowledgeActions: boolean;
123
+ /** Integrations usage */
124
+ integrations: boolean;
125
+ /** Custom capabilities */
126
+ custom?: Record<string, boolean>;
127
+ }
128
+ /**
129
+ * Platform Context
130
+ *
131
+ * Interface for handlers to access FirstStep Studio features:
132
+ * - Session management
133
+ * - Knowledge base queries
134
+ * - Integration calls
135
+ * - Analytics tracking
136
+ * - Logging
137
+ */
138
+ interface ProtocolContext {
139
+ /** Session management */
140
+ session: SessionContext;
141
+ /** Knowledge base access */
142
+ knowledge: KnowledgeContext;
143
+ /** Integration access */
144
+ integrations: IntegrationContext;
145
+ /** Analytics tracking */
146
+ analytics: AnalyticsContext;
147
+ /** Logging */
148
+ logger: LoggerContext;
149
+ /** Deployment info */
150
+ deployment: DeploymentInfo;
151
+ /** Chatbot info (for GuidedForm) */
152
+ chatbot?: ChatbotInfo;
153
+ }
154
+ interface SessionContext {
155
+ /** Session ID */
156
+ sessionId: string;
157
+ /** Get session state */
158
+ getState(): Promise<SessionState>;
159
+ /** Update session state */
160
+ updateState(updates: Partial<SessionState>): Promise<void>;
161
+ /** Get message history */
162
+ getHistory(): Promise<ChatMessage[]>;
163
+ /** Save message */
164
+ saveMessage(message: ChatMessage): Promise<void>;
165
+ /** Complete session */
166
+ complete(): Promise<void>;
167
+ /** Get all collected form data */
168
+ getFormData(): Promise<FormData>;
169
+ /** Update a single form field */
170
+ updateFormField(fieldId: string, value: FormFieldValue): Promise<void>;
171
+ /** Update multiple form fields */
172
+ updateFormData(fields: Record<string, FormFieldValue>): Promise<void>;
173
+ }
174
+ interface SessionState {
175
+ sessionId: string;
176
+ deploymentId: string;
177
+ currentAgentId?: string;
178
+ /** Current question being asked (for proper answer tracking) */
179
+ currentQuestionId?: string;
180
+ /** Internal form data storage (handler-specific format) */
181
+ formData?: Record<string, unknown>;
182
+ metadata?: SessionMetadata;
183
+ createdAt: Date;
184
+ updatedAt: Date;
185
+ }
186
+ interface ChatMessage {
187
+ role: 'user' | 'assistant' | 'system';
188
+ content: string;
189
+ agentId?: string;
190
+ timestamp?: Date;
191
+ metadata?: Record<string, unknown>;
192
+ }
193
+ /**
194
+ * FormData - Collected user responses
195
+ *
196
+ * Standard format for all protocol handlers.
197
+ * Used for KPI calculation and session analytics.
198
+ */
199
+ interface FormData {
200
+ [fieldId: string]: FormFieldValue;
201
+ }
202
+ interface FormFieldValue {
203
+ /** The collected value */
204
+ value: string | string[] | number | boolean | null;
205
+ /** When this field was answered */
206
+ answeredAt?: Date;
207
+ /** Which agent collected this (for GuidedForm) */
208
+ agentId?: string;
209
+ /** Raw user response before processing */
210
+ rawResponse?: string;
211
+ }
212
+ /**
213
+ * RoutingLog - Record of routing decisions
214
+ *
215
+ * Stored automatically when context.logger.logRouting() is called.
216
+ */
217
+ interface RoutingLog {
218
+ /** When the routing decision was made */
219
+ timestamp: Date;
220
+ /** Decision type (e.g., 'FIRST_MESSAGE', 'CLASSIFIER_ROUTING') */
221
+ decision: string;
222
+ /** Human-readable reason */
223
+ reason: string;
224
+ /** Source agent */
225
+ fromAgent?: string;
226
+ /** Target agent */
227
+ toAgent: string;
228
+ /** Classification result if applicable */
229
+ classification?: {
230
+ category?: string;
231
+ level?: string;
232
+ confidence?: number;
233
+ };
234
+ }
235
+ /**
236
+ * SessionMetadata - Session-level analytics data
237
+ *
238
+ * Automatically tracked by the platform.
239
+ */
240
+ interface SessionMetadata {
241
+ turnCount?: number;
242
+ totalMessages?: number;
243
+ userMessageCount?: number;
244
+ durationSeconds?: number;
245
+ startedAt?: Date;
246
+ endedAt?: Date;
247
+ lastActivityAt?: Date;
248
+ userAgent?: string;
249
+ ipAddress?: string;
250
+ referrer?: string;
251
+ source?: string;
252
+ language?: string;
253
+ timezone?: string;
254
+ knowledgeActionExecuted?: boolean;
255
+ helplineActionExecuted?: boolean;
256
+ executedKnowledgeIds?: string[];
257
+ helplineSmsClicked?: string;
258
+ helplineCallClicked?: string;
259
+ helplineWebsiteClicked?: string;
260
+ knowledgeLinkClicked?: string;
261
+ announcementCtaClicked?: boolean;
262
+ zendeskTicketId?: string;
263
+ zendeskTicketUrl?: string;
264
+ completed?: boolean;
265
+ completedAt?: string;
266
+ custom?: Record<string, unknown>;
267
+ }
268
+ interface KnowledgeContext {
269
+ /** Query database knowledge */
270
+ queryDatabase(knowledgeId: string, query: string): Promise<KnowledgeResult>;
271
+ /** Search page knowledge */
272
+ searchPages(knowledgeId: string, query: string): Promise<KnowledgeResult>;
273
+ }
274
+ interface KnowledgeResult {
275
+ success: boolean;
276
+ records?: Record<string, unknown>[];
277
+ error?: string;
278
+ }
279
+ interface IntegrationContext {
280
+ /** Execute integration */
281
+ execute(integrationId: string, params: Record<string, unknown>): Promise<IntegrationResult>;
282
+ /** Search helplines (Throughline) */
283
+ searchHelplines?(query: string, options?: HelplineSearchOptions): Promise<HelplineResult>;
284
+ }
285
+ interface IntegrationResult {
286
+ success: boolean;
287
+ data?: Record<string, unknown>;
288
+ error?: string;
289
+ }
290
+ interface HelplineSearchOptions {
291
+ country?: string;
292
+ topics?: string[];
293
+ limit?: number;
294
+ }
295
+ interface HelplineResult {
296
+ success: boolean;
297
+ helplines?: Helpline[];
298
+ error?: string;
299
+ }
300
+ interface Helpline {
301
+ name: string;
302
+ phone?: string;
303
+ website?: string;
304
+ description?: string;
305
+ }
306
+ interface LoggerContext {
307
+ debug(message: string, data?: Record<string, unknown>): void;
308
+ info(message: string, data?: Record<string, unknown>): void;
309
+ warn(message: string, data?: Record<string, unknown>): void;
310
+ error(message: string, data?: Record<string, unknown>): void;
311
+ /** Log routing decision */
312
+ logRouting(decision: RoutingDecision): void;
313
+ /** Log tool usage */
314
+ logToolUse(tool: string, params: Record<string, unknown>, result: unknown): void;
315
+ }
316
+ interface RoutingDecision {
317
+ decision: string;
318
+ reason: string;
319
+ fromAgent?: string;
320
+ toAgent: string;
321
+ timestamp: Date;
322
+ }
323
+ /**
324
+ * Analytics Context
325
+ *
326
+ * Track user interactions and action executions.
327
+ * Data is stored in SessionMetadata for KPI/reporting.
328
+ */
329
+ interface AnalyticsContext {
330
+ /**
331
+ * Log action execution
332
+ * Tracks when knowledge, helpline, or integration actions are executed.
333
+ */
334
+ logActionExecuted(type: 'knowledge' | 'helpline' | 'integration', actionId: string, metadata?: Record<string, unknown>): void;
335
+ /**
336
+ * Log user interaction
337
+ * Tracks clicks on helplines, knowledge links, CTAs, etc.
338
+ */
339
+ logInteraction(event: InteractionEvent): void;
340
+ /**
341
+ * Log custom event
342
+ * For handler-specific analytics.
343
+ */
344
+ logCustomEvent(eventName: string, data?: Record<string, unknown>): void;
345
+ }
346
+ interface InteractionEvent {
347
+ /** Event type */
348
+ type: InteractionEventType;
349
+ /** Target identifier (e.g., helpline name, knowledge ID) */
350
+ targetId?: string;
351
+ /** Additional event data */
352
+ metadata?: Record<string, unknown>;
353
+ /** Event timestamp (auto-filled if not provided) */
354
+ timestamp?: Date;
355
+ }
356
+ type InteractionEventType = 'helpline_sms_click' | 'helpline_call_click' | 'helpline_website_click' | 'knowledge_link_click' | 'announcement_cta_click' | 'card_click' | 'form_submit' | 'custom';
357
+ /**
358
+ * FormSchema - Defines what data a protocol handler collects
359
+ *
360
+ * Registered when a protocol is set up.
361
+ * Used for analytics, validation, and dashboard display.
362
+ */
363
+ interface FormSchema {
364
+ /** Schema version for migrations */
365
+ version?: string;
366
+ /** Field definitions */
367
+ fields: FormFieldDefinition[];
368
+ }
369
+ interface FormFieldDefinition {
370
+ /** Unique field identifier */
371
+ id: string;
372
+ /** Display name */
373
+ name: string;
374
+ /** Field type */
375
+ type: FormFieldType;
376
+ /** Options for select/multiselect */
377
+ options?: string[];
378
+ /** Whether this field is required */
379
+ required?: boolean;
380
+ /** Which agent owns this field (for GuidedForm) */
381
+ agentId?: string;
382
+ /** Field description for documentation */
383
+ description?: string;
384
+ /** Display order */
385
+ order?: number;
386
+ }
387
+ type FormFieldType = 'text' | 'number' | 'date' | 'boolean' | 'select' | 'multiselect' | 'email' | 'phone' | 'url';
388
+ /**
389
+ * Protocol Registration
390
+ *
391
+ * Configuration for registering a protocol handler.
392
+ */
393
+ interface ProtocolRegistration {
394
+ /** Unique slug identifier */
395
+ slug: string;
396
+ /** Display name */
397
+ name: string;
398
+ /** Description */
399
+ description?: string;
400
+ /** External handler endpoint URL (for external handlers) */
401
+ endpoint?: string;
402
+ /** Form schema - what data this handler collects */
403
+ formSchema?: FormSchema;
404
+ /** Handler capabilities */
405
+ capabilities?: ProtocolCapabilities;
406
+ /** Whether this is a built-in handler */
407
+ builtIn?: boolean;
408
+ }
409
+ interface DeploymentInfo {
410
+ id: string;
411
+ slug: string;
412
+ name: string;
413
+ protocolType: string;
414
+ metadata?: Record<string, unknown>;
415
+ }
416
+ interface ChatbotInfo {
417
+ id: string;
418
+ name: string;
419
+ /** GuidedForm only - full config including agents, questions, etc. */
420
+ config?: Record<string, unknown>;
421
+ }
422
+ /**
423
+ * Classifier configuration for protocol handlers
424
+ *
425
+ * Classifiers can be configured as:
426
+ * - Local: classifierId points to a classifier in the FirstStep database
427
+ * - Remote: classifierId is a full UCP endpoint URL
428
+ */
429
+ interface ClassifierConfig {
430
+ /** Classifier ID or UCP endpoint URL */
431
+ classifierId: string;
432
+ /** API key for remote UCP endpoints (optional) */
433
+ apiKey?: string;
434
+ /** Whether classification is required for routing */
435
+ required?: boolean;
436
+ }
437
+
438
+ export type { AnalyticsContext as A, FormFieldType as B, ChatMessage as C, DeploymentInfo as D, ProtocolRegistration as E, FormData as F, HandlerInfo as H, IntegrationContext as I, KnowledgeContext as K, LoggerContext as L, ProtocolRequest as P, RoutingDecision as R, SessionStatus as S, ProtocolResponse as a, ProtocolForm as b, ProtocolFormField as c, ProtocolFormOption as d, ProtocolFieldValidation as e, ProtocolStreamChunk as f, ProtocolError as g, ProtocolHandler as h, ProtocolCapabilities as i, ProtocolContext as j, SessionContext as k, SessionState as l, KnowledgeResult as m, IntegrationResult as n, HelplineSearchOptions as o, HelplineResult as p, Helpline as q, ChatbotInfo as r, ClassifierConfig as s, FormFieldValue as t, RoutingLog as u, SessionMetadata as v, InteractionEvent as w, InteractionEventType as x, FormSchema as y, FormFieldDefinition as z };