voiceml 0.8.1 → 0.9.2

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,508 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../models/assistants_v1'
4
+
5
+ module VoiceML
6
+ # `client.assistants_v1` — Twilio AI-Assistants v1 (assistants.twilio.com/v1).
7
+ # Sits outside the /2010-04-01/Accounts/... namespace; account resolved from Basic auth.
8
+ # 7 families / 30 ops:
9
+ # - Assistant (5 CRUD)
10
+ # - Tool (5 CRUD + attach/detach + per-assistant list)
11
+ # - Knowledge (5 CRUD + status fetch + chunks list + attach/detach + per-assistant list)
12
+ # - Session (list + fetch + per-session messages list)
13
+ # - Message send (POST /v1/Assistants/{id}/Messages)
14
+ # - Feedback (list + create under /v1/Assistants/{id}/Feedbacks)
15
+ # - Policy (list /v1/Policies)
16
+ #
17
+ # Wire format: application/json request bodies (snake_case keys) — distinct from
18
+ # the form-urlencoded Conversations/Voice v1 surfaces. Responses are JSON with the
19
+ # shared `meta` envelope (V1Pageable).
20
+ class AssistantsV1Resource
21
+ def initialize(transport)
22
+ @transport = transport
23
+ @assistants_top = AssistantsV1AssistantsResource.new(transport)
24
+ @tools_top = AssistantsV1ToolsResource.new(transport)
25
+ @knowledge_top = AssistantsV1KnowledgeResource.new(transport)
26
+ @sessions_top = AssistantsV1SessionsResource.new(transport)
27
+ @policies_top = AssistantsV1PoliciesResource.new(transport)
28
+ end
29
+
30
+ # Scope factory: with no arg, returns the top-level Assistants resource
31
+ # (list/create/fetch/update/delete). With an `assistant_id`, returns the
32
+ # nested scope exposing `.tools`, `.knowledge`, `.feedbacks`, `.messages`.
33
+ def assistants(assistant_id = nil)
34
+ assistant_id.nil? ? @assistants_top : AssistantsV1AssistantScope.new(@transport, assistant_id)
35
+ end
36
+
37
+ # Top-level Tools resource (list/create/fetch/update/delete on /v1/Tools).
38
+ def tools
39
+ @tools_top
40
+ end
41
+
42
+ # Scope factory: with no arg, returns the top-level Knowledge resource
43
+ # (list/create/fetch/update/delete). With a `knowledge_id`, returns the
44
+ # nested scope exposing `.status` and `.chunks`.
45
+ def knowledge(knowledge_id = nil)
46
+ knowledge_id.nil? ? @knowledge_top : AssistantsV1KnowledgeScope.new(@transport, knowledge_id)
47
+ end
48
+
49
+ # Scope factory: with no arg, returns the top-level Sessions resource
50
+ # (list/fetch on /v1/Sessions). With a `session_id`, returns the nested
51
+ # scope exposing `.messages`.
52
+ def sessions(session_id = nil)
53
+ session_id.nil? ? @sessions_top : AssistantsV1SessionScope.new(@transport, session_id)
54
+ end
55
+
56
+ def policies
57
+ @policies_top
58
+ end
59
+ end
60
+
61
+ # ============================================================================
62
+ # /v1/Assistants — 5 CRUD ops on the top-level Assistant resource.
63
+ # Fetch returns the AssistantWithToolsAndKnowledge shape (tools + knowledge inline).
64
+ # ============================================================================
65
+ class AssistantsV1AssistantsResource
66
+ CREATE_FIELDS = %i[name owner personality_prompt model customer_ai segment_credential].freeze
67
+ UPDATE_FIELDS = %i[name owner personality_prompt model customer_ai segment_credential].freeze
68
+
69
+ def initialize(transport)
70
+ @transport = transport
71
+ end
72
+
73
+ def list(page_size: nil, page: nil, page_token: nil)
74
+ params = {}
75
+ params['PageSize'] = page_size unless page_size.nil?
76
+ params['Page'] = page unless page.nil?
77
+ params['PageToken'] = page_token unless page_token.nil?
78
+ AssistantsV1AssistantList.new(@transport.request(:get, '/v1/Assistants', params: params))
79
+ end
80
+
81
+ def create(name:, **kwargs)
82
+ kwargs[:name] = name
83
+ AssistantsV1Assistant.from_hash(
84
+ @transport.request(:post, '/v1/Assistants', json: build_json(CREATE_FIELDS, kwargs))
85
+ )
86
+ end
87
+
88
+ def fetch(id)
89
+ AssistantsV1AssistantWithToolsAndKnowledge.from_hash(
90
+ @transport.request(:get, "/v1/Assistants/#{id}")
91
+ )
92
+ end
93
+
94
+ def update(id, **kwargs)
95
+ AssistantsV1Assistant.from_hash(
96
+ @transport.request(:put, "/v1/Assistants/#{id}", json: build_json(UPDATE_FIELDS, kwargs))
97
+ )
98
+ end
99
+
100
+ def delete(id)
101
+ @transport.request(:delete, "/v1/Assistants/#{id}")
102
+ nil
103
+ end
104
+
105
+ private
106
+
107
+ def build_json(keys, kwargs)
108
+ out = {}
109
+ keys.each do |k|
110
+ v = kwargs[k]
111
+ next if v.nil?
112
+
113
+ out[k.to_s] = v
114
+ end
115
+ out
116
+ end
117
+ end
118
+
119
+ # ============================================================================
120
+ # Per-Assistant scope — sub-resources under /v1/Assistants/{id}/...:
121
+ # - .tools attach/detach + list
122
+ # - .knowledge attach/detach + list
123
+ # - .feedbacks list + create
124
+ # - .messages create (send)
125
+ # ============================================================================
126
+ class AssistantsV1AssistantScope
127
+ attr_reader :assistant_id
128
+
129
+ def initialize(transport, assistant_id)
130
+ @transport = transport
131
+ @assistant_id = assistant_id
132
+ end
133
+
134
+ def tools
135
+ @tools ||= AssistantsV1AssistantToolsScope.new(@transport, @assistant_id)
136
+ end
137
+
138
+ def knowledge
139
+ @knowledge ||= AssistantsV1AssistantKnowledgeScope.new(@transport, @assistant_id)
140
+ end
141
+
142
+ def feedbacks
143
+ @feedbacks ||= AssistantsV1AssistantFeedbacksScope.new(@transport, @assistant_id)
144
+ end
145
+
146
+ def messages
147
+ @messages ||= AssistantsV1AssistantMessagesScope.new(@transport, @assistant_id)
148
+ end
149
+ end
150
+
151
+ # /v1/Assistants/{id}/Tools — list attached + attach/detach single Tool.
152
+ class AssistantsV1AssistantToolsScope
153
+ def initialize(transport, assistant_id)
154
+ @transport = transport
155
+ @assistant_id = assistant_id
156
+ end
157
+
158
+ def list(page_size: nil)
159
+ params = {}
160
+ params['PageSize'] = page_size unless page_size.nil?
161
+ AssistantsV1ToolList.new(
162
+ @transport.request(:get, "/v1/Assistants/#{@assistant_id}/Tools", params: params)
163
+ )
164
+ end
165
+
166
+ # POST /v1/Assistants/{id}/Tools/{toolId} — 204 No Content on success.
167
+ def attach(tool_id)
168
+ @transport.request(:post, "/v1/Assistants/#{@assistant_id}/Tools/#{tool_id}")
169
+ nil
170
+ end
171
+
172
+ # DELETE /v1/Assistants/{id}/Tools/{toolId} — 204 No Content on success.
173
+ def detach(tool_id)
174
+ @transport.request(:delete, "/v1/Assistants/#{@assistant_id}/Tools/#{tool_id}")
175
+ nil
176
+ end
177
+ end
178
+
179
+ # /v1/Assistants/{id}/Knowledge — list attached + attach/detach single Knowledge.
180
+ class AssistantsV1AssistantKnowledgeScope
181
+ def initialize(transport, assistant_id)
182
+ @transport = transport
183
+ @assistant_id = assistant_id
184
+ end
185
+
186
+ def list(page_size: nil)
187
+ params = {}
188
+ params['PageSize'] = page_size unless page_size.nil?
189
+ AssistantsV1KnowledgeList.new(
190
+ @transport.request(:get, "/v1/Assistants/#{@assistant_id}/Knowledge", params: params)
191
+ )
192
+ end
193
+
194
+ # POST /v1/Assistants/{id}/Knowledge/{knowledgeId} — 204 No Content on success.
195
+ def attach(knowledge_id)
196
+ @transport.request(:post, "/v1/Assistants/#{@assistant_id}/Knowledge/#{knowledge_id}")
197
+ nil
198
+ end
199
+
200
+ # DELETE /v1/Assistants/{id}/Knowledge/{knowledgeId} — 204 No Content on success.
201
+ def detach(knowledge_id)
202
+ @transport.request(:delete, "/v1/Assistants/#{@assistant_id}/Knowledge/#{knowledge_id}")
203
+ nil
204
+ end
205
+ end
206
+
207
+ # /v1/Assistants/{id}/Feedbacks — list + create.
208
+ class AssistantsV1AssistantFeedbacksScope
209
+ CREATE_FIELDS = %i[session_id message_id score text].freeze
210
+
211
+ def initialize(transport, assistant_id)
212
+ @transport = transport
213
+ @assistant_id = assistant_id
214
+ end
215
+
216
+ def list(page_size: nil)
217
+ params = {}
218
+ params['PageSize'] = page_size unless page_size.nil?
219
+ AssistantsV1FeedbackList.new(
220
+ @transport.request(:get, "/v1/Assistants/#{@assistant_id}/Feedbacks", params: params)
221
+ )
222
+ end
223
+
224
+ def create(session_id:, **kwargs)
225
+ kwargs[:session_id] = session_id
226
+ AssistantsV1Feedback.from_hash(
227
+ @transport.request(:post, "/v1/Assistants/#{@assistant_id}/Feedbacks",
228
+ json: build_json(CREATE_FIELDS, kwargs))
229
+ )
230
+ end
231
+
232
+ private
233
+
234
+ def build_json(keys, kwargs)
235
+ out = {}
236
+ keys.each do |k|
237
+ v = kwargs[k]
238
+ next if v.nil?
239
+
240
+ out[k.to_s] = v
241
+ end
242
+ out
243
+ end
244
+ end
245
+
246
+ # /v1/Assistants/{id}/Messages — send (create) a message; returns the model's reply.
247
+ class AssistantsV1AssistantMessagesScope
248
+ CREATE_FIELDS = %i[identity body session_id webhook mode].freeze
249
+
250
+ def initialize(transport, assistant_id)
251
+ @transport = transport
252
+ @assistant_id = assistant_id
253
+ end
254
+
255
+ def create(identity:, body:, **kwargs)
256
+ kwargs[:identity] = identity
257
+ kwargs[:body] = body
258
+ AssistantsV1SendMessageResponse.from_hash(
259
+ @transport.request(:post, "/v1/Assistants/#{@assistant_id}/Messages",
260
+ json: build_json(CREATE_FIELDS, kwargs))
261
+ )
262
+ end
263
+
264
+ private
265
+
266
+ def build_json(keys, kwargs)
267
+ out = {}
268
+ keys.each do |k|
269
+ v = kwargs[k]
270
+ next if v.nil?
271
+
272
+ out[k.to_s] = v
273
+ end
274
+ out
275
+ end
276
+ end
277
+
278
+ # ============================================================================
279
+ # /v1/Tools — 5 CRUD ops on the top-level Tool resource.
280
+ # `list` accepts an optional `assistant_id:` filter (wire name `AssistantId`).
281
+ # Fetch returns the ToolWithPolicies shape (policies inline).
282
+ # ============================================================================
283
+ class AssistantsV1ToolsResource
284
+ CREATE_FIELDS = %i[name type enabled assistant_id description meta].freeze
285
+ UPDATE_FIELDS = %i[name type enabled description meta].freeze
286
+
287
+ def initialize(transport)
288
+ @transport = transport
289
+ end
290
+
291
+ def list(assistant_id: nil, page_size: nil)
292
+ params = {}
293
+ params['AssistantId'] = assistant_id unless assistant_id.nil?
294
+ params['PageSize'] = page_size unless page_size.nil?
295
+ AssistantsV1ToolList.new(@transport.request(:get, '/v1/Tools', params: params))
296
+ end
297
+
298
+ def create(name:, type:, enabled:, **kwargs)
299
+ kwargs[:name] = name
300
+ kwargs[:type] = type
301
+ kwargs[:enabled] = enabled
302
+ AssistantsV1Tool.from_hash(
303
+ @transport.request(:post, '/v1/Tools', json: build_json(CREATE_FIELDS, kwargs))
304
+ )
305
+ end
306
+
307
+ def fetch(id)
308
+ AssistantsV1ToolWithPolicies.from_hash(@transport.request(:get, "/v1/Tools/#{id}"))
309
+ end
310
+
311
+ def update(id, **kwargs)
312
+ AssistantsV1Tool.from_hash(
313
+ @transport.request(:put, "/v1/Tools/#{id}", json: build_json(UPDATE_FIELDS, kwargs))
314
+ )
315
+ end
316
+
317
+ def delete(id)
318
+ @transport.request(:delete, "/v1/Tools/#{id}")
319
+ nil
320
+ end
321
+
322
+ private
323
+
324
+ def build_json(keys, kwargs)
325
+ out = {}
326
+ keys.each do |k|
327
+ v = kwargs[k]
328
+ next if v.nil?
329
+
330
+ out[k.to_s] = v
331
+ end
332
+ out
333
+ end
334
+ end
335
+
336
+ # ============================================================================
337
+ # /v1/Knowledge — 5 CRUD ops on the top-level Knowledge resource.
338
+ # `list` accepts an optional `assistant_id:` filter (wire name `AssistantId`).
339
+ # ============================================================================
340
+ class AssistantsV1KnowledgeResource
341
+ CREATE_FIELDS = %i[name type assistant_id description embedding_model knowledge_source_details].freeze
342
+ UPDATE_FIELDS = %i[name type description embedding_model knowledge_source_details].freeze
343
+
344
+ def initialize(transport)
345
+ @transport = transport
346
+ end
347
+
348
+ def list(assistant_id: nil, page_size: nil)
349
+ params = {}
350
+ params['AssistantId'] = assistant_id unless assistant_id.nil?
351
+ params['PageSize'] = page_size unless page_size.nil?
352
+ AssistantsV1KnowledgeList.new(@transport.request(:get, '/v1/Knowledge', params: params))
353
+ end
354
+
355
+ def create(name:, type:, **kwargs)
356
+ kwargs[:name] = name
357
+ kwargs[:type] = type
358
+ AssistantsV1Knowledge.from_hash(
359
+ @transport.request(:post, '/v1/Knowledge', json: build_json(CREATE_FIELDS, kwargs))
360
+ )
361
+ end
362
+
363
+ def fetch(id)
364
+ AssistantsV1Knowledge.from_hash(@transport.request(:get, "/v1/Knowledge/#{id}"))
365
+ end
366
+
367
+ def update(id, **kwargs)
368
+ AssistantsV1Knowledge.from_hash(
369
+ @transport.request(:put, "/v1/Knowledge/#{id}", json: build_json(UPDATE_FIELDS, kwargs))
370
+ )
371
+ end
372
+
373
+ def delete(id)
374
+ @transport.request(:delete, "/v1/Knowledge/#{id}")
375
+ nil
376
+ end
377
+
378
+ private
379
+
380
+ def build_json(keys, kwargs)
381
+ out = {}
382
+ keys.each do |k|
383
+ v = kwargs[k]
384
+ next if v.nil?
385
+
386
+ out[k.to_s] = v
387
+ end
388
+ out
389
+ end
390
+ end
391
+
392
+ # ============================================================================
393
+ # Per-Knowledge scope — sub-resources under /v1/Knowledge/{id}/...:
394
+ # - .status fetch ingestion status snapshot
395
+ # - .chunks list indexed chunks
396
+ # ============================================================================
397
+ class AssistantsV1KnowledgeScope
398
+ attr_reader :knowledge_id
399
+
400
+ def initialize(transport, knowledge_id)
401
+ @transport = transport
402
+ @knowledge_id = knowledge_id
403
+ end
404
+
405
+ def status
406
+ @status ||= AssistantsV1KnowledgeStatusScope.new(@transport, @knowledge_id)
407
+ end
408
+
409
+ def chunks
410
+ @chunks ||= AssistantsV1KnowledgeChunksScope.new(@transport, @knowledge_id)
411
+ end
412
+ end
413
+
414
+ # /v1/Knowledge/{id}/Status — read-only ingestion status snapshot.
415
+ class AssistantsV1KnowledgeStatusScope
416
+ def initialize(transport, knowledge_id)
417
+ @transport = transport
418
+ @knowledge_id = knowledge_id
419
+ end
420
+
421
+ def fetch
422
+ AssistantsV1KnowledgeStatus.from_hash(
423
+ @transport.request(:get, "/v1/Knowledge/#{@knowledge_id}/Status")
424
+ )
425
+ end
426
+ end
427
+
428
+ # /v1/Knowledge/{id}/Chunks — paged list of indexed chunks.
429
+ class AssistantsV1KnowledgeChunksScope
430
+ def initialize(transport, knowledge_id)
431
+ @transport = transport
432
+ @knowledge_id = knowledge_id
433
+ end
434
+
435
+ def list(page_size: nil)
436
+ params = {}
437
+ params['PageSize'] = page_size unless page_size.nil?
438
+ AssistantsV1KnowledgeChunkList.new(
439
+ @transport.request(:get, "/v1/Knowledge/#{@knowledge_id}/Chunks", params: params)
440
+ )
441
+ end
442
+ end
443
+
444
+ # ============================================================================
445
+ # /v1/Sessions — list + fetch on the top-level Session resource.
446
+ # ============================================================================
447
+ class AssistantsV1SessionsResource
448
+ def initialize(transport)
449
+ @transport = transport
450
+ end
451
+
452
+ def list(page_size: nil)
453
+ params = {}
454
+ params['PageSize'] = page_size unless page_size.nil?
455
+ AssistantsV1SessionList.new(@transport.request(:get, '/v1/Sessions', params: params))
456
+ end
457
+
458
+ def fetch(id)
459
+ AssistantsV1Session.from_hash(@transport.request(:get, "/v1/Sessions/#{id}"))
460
+ end
461
+ end
462
+
463
+ # /v1/Sessions/{id}/Messages — list a session's messages.
464
+ class AssistantsV1SessionScope
465
+ attr_reader :session_id
466
+
467
+ def initialize(transport, session_id)
468
+ @transport = transport
469
+ @session_id = session_id
470
+ end
471
+
472
+ def messages
473
+ @messages ||= AssistantsV1SessionMessagesScope.new(@transport, @session_id)
474
+ end
475
+ end
476
+
477
+ class AssistantsV1SessionMessagesScope
478
+ def initialize(transport, session_id)
479
+ @transport = transport
480
+ @session_id = session_id
481
+ end
482
+
483
+ def list(page_size: nil)
484
+ params = {}
485
+ params['PageSize'] = page_size unless page_size.nil?
486
+ AssistantsV1MessageList.new(
487
+ @transport.request(:get, "/v1/Sessions/#{@session_id}/Messages", params: params)
488
+ )
489
+ end
490
+ end
491
+
492
+ # ============================================================================
493
+ # /v1/Policies — read-only list, filterable by ToolId/KnowledgeId.
494
+ # ============================================================================
495
+ class AssistantsV1PoliciesResource
496
+ def initialize(transport)
497
+ @transport = transport
498
+ end
499
+
500
+ def list(tool_id: nil, knowledge_id: nil, page_size: nil)
501
+ params = {}
502
+ params['ToolId'] = tool_id unless tool_id.nil?
503
+ params['KnowledgeId'] = knowledge_id unless knowledge_id.nil?
504
+ params['PageSize'] = page_size unless page_size.nil?
505
+ AssistantsV1PolicyList.new(@transport.request(:get, '/v1/Policies', params: params))
506
+ end
507
+ end
508
+ end