Arc¶
Arc is the Python client for Arc, the AI assistant built into the CX Agent Studio console. It drives an assistant session on an app the way the console does, over the public v1beta REST API: streamed turns (StreamChatAiAssistant), confirmation-card answers, clarification option panels, client-managed long-running operations (evaluation runs are polled and the session is resumed automatically), session CRUD and session artifacts.
Each turn returns a TurnStreamEvents aggregate that you can render as markdown or inspect field by field. Confirmation cards are answered by a ConfirmationPolicy (manual, click, mixed, scripted) or explicitly with send_confirmation_response. The cxas arc CLI (reference) is a thin wrapper around this class.
Quick Example¶
from cxas_scrapi.core.arc import Arc
from cxas_scrapi.utils.arc_confirmation_policy import make_policy
with Arc(app_name="projects/my-project/locations/us/apps/my-app") as arc:
session_id = "arc-sdk-demo"
# Ask a question; cards stay pending with the default manual policy.
events = arc.chat(
session_id, "What does this app do? Summarize its agents and tools."
)
print(events.format_markdown())
# Build something, clicking through confirmation cards while declining destructive tools.
events = arc.chat(
session_id,
"Add a guardrail that blocks requests for medical advice.",
confirmation_policy=make_policy("click", deny_tools=("*delete*",)),
timeout_seconds=1800,
)
print(events.text)
for action in events.confirmation_actions:
print(action.tool, action.action, "typed" if action.typed else "clicked")
# Anything still waiting for a human decision?
for card in events.pending_confirmation_requests:
print(card.confirmation_id, card.tool, card.context)
# e.g. arc.send_confirmation_response(session_id, card.confirmation_id, confirmed=True)
# Artifacts recorded on the session (gcsUri pointers), e.g. an exported bundle.
for artifact in arc.session_artifacts(session_id):
print(artifact.get("displayName"), artifact.get("gcsUri"))
Errors surface as ArcApiError (HTTP status and decoded message), ArcStreamTimeout (idle stream or turn deadline; .partial holds what streamed so far), or ArcStreamInterrupted (connection lost after exhausting re-attach attempts; .partial holds what streamed so far).
Reference¶
arc ¶
Client for the CX Agent Studio AI assistant (Arc).
Arc is the conversational builder assistant embedded in the CX Agent Studio console. This module drives it the way the console does, over the public v1beta REST API:
SessionService.StreamChatAiAssistant(server streaming) for turns, confirmation-card answers and client-managed LRO resumption;SessionService.CancelAssistantTurn/GenerateOnboardingSuggestions;AssistantServicesession CRUD (assistantSessions);google.longrunning.OperationsandEvaluationRunsfor LRO polling.
Example::
from cxas_scrapi.core.arc import Arc
arc = Arc(app_name="projects/my-proj/locations/us/apps/my-app")
events = arc.chat("arc-cli-demo", "What does this app do?")
print(events.format_markdown())
Arc ¶
Arc(app_name, *, client_profile=CLIENT_PROFILE_CONSOLE, api_endpoint=None, stream_idle_timeout=CONSOLE_STREAM_IDLE_TIMEOUT_SECONDS, max_reattach_attempts=2, rate_limiter=None, http_session=None, progress=None, **kwargs)
Bases: Common
Drives an assistant session on a CX Agent Studio app.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_name | str | Full app resource name ( | required |
client_profile | str |
| CLIENT_PROFILE_CONSOLE |
api_endpoint | str | None | API host; defaults to | None |
stream_idle_timeout | float | Seconds without any streamed event before the turn is abandoned client-side. | CONSOLE_STREAM_IDLE_TIMEOUT_SECONDS |
max_reattach_attempts | int | How many times a dropped connection is re-attached ( | 2 |
rate_limiter | RateLimiter | None | Optional :class: | None |
http_session | Session | None | Optional | None |
progress | Callable[[str], None] | None | Optional callback for short progress messages. | None |
**kwargs | Any | Forwarded to :class: | {} |
Source code in src/cxas_scrapi/core/arc.py
active_turn_events property ¶
Events of the turn currently in flight (for partial rendering).
close ¶
Closes HTTP sessions owned by this client.
session_name ¶
Returns the full assistantSessions resource name.
Source code in src/cxas_scrapi/core/arc.py
list_sessions ¶
Lists assistant sessions on the app (first page_size only).
Source code in src/cxas_scrapi/core/arc.py
get_session ¶
Gets an assistant session (messages, artifacts, active turn).
create_session ¶
Explicitly creates an assistant session (streams create lazily).
Source code in src/cxas_scrapi/core/arc.py
delete_session ¶
cancel_turn ¶
Cancels the in-flight turn of a session.
Source code in src/cxas_scrapi/core/arc.py
generate_onboarding_suggestions ¶
Returns starter suggestions the console shows on an empty session.
Source code in src/cxas_scrapi/core/arc.py
session_artifacts ¶
Artifacts (gcsUri pointers) recorded on the session.
get_operation ¶
get_evaluation_run ¶
list_evaluation_runs ¶
Lists the most recent evaluation runs on the app.
Source code in src/cxas_scrapi/core/arc.py
stream_turn ¶
stream_turn(session_id, message='', *, attached_gcs_uris=(), confirmation_response=None, operation_completed_event=None, resume_from_sequence_number=None, enable_confirmation_cards=None, enable_client_managed_lro=True, timeout_seconds=1800, on_event=None, into=None)
Sends one StreamChatAiAssistant request and consumes the stream.
This is the low-level primitive: no confirmation policy and no LRO resumption. See :meth:chat for the console-equivalent turn loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Short id or full session name. | required |
message | str | User text (may be empty for a pure card click). | '' |
attached_gcs_uris | Sequence[str] |
| () |
confirmation_response | dict[str, Any] | None |
| None |
operation_completed_event | dict[str, Any] | None | Client-managed LRO completion event. | None |
resume_from_sequence_number | int | None | Replay from this sequence number. | None |
enable_confirmation_cards | bool | None | Override the profile's card setting. | None |
enable_client_managed_lro | bool | Advertise | True |
timeout_seconds | float | Overall deadline for this stream. | 1800 |
on_event | Callable[[dict[str, Any]], None] | None | Callback for every raw JSON event. | None |
into | TurnStreamEvents | None | Merge the sub-stream into this aggregate instead of a new one. | None |
Returns:
| Type | Description |
|---|---|
TurnStreamEvents | The events of this stream ( |
Source code in src/cxas_scrapi/core/arc.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | |
chat ¶
chat(session_id, message, *, attached_gcs_uris=(), confirmation_policy=None, max_confirmation_rounds=5, enable_confirmation_cards=None, timeout_seconds=1800, auto_resolve_lro=True, lro_timeout_seconds=1800, lro_poll_interval_seconds=5, max_lro_iterations=10)
Sends a user message and runs the console-equivalent turn loop.
Pending LROs are polled and resumed (auto_resolve_lro) and confirmation cards are answered by confirmation_policy (none or a ManualPolicy leaves them pending for the caller).
Source code in src/cxas_scrapi/core/arc.py
send_confirmation_response ¶
send_confirmation_response(session_id, confirmation_id, *, confirmed=None, answer_text=None, tool='', confirmation_policy=None, max_confirmation_rounds=5, timeout_seconds=1800, auto_resolve_lro=True, lro_timeout_seconds=1800, lro_poll_interval_seconds=5, max_lro_iterations=10)
Answers a confirmation card the way a console click does.
Source code in src/cxas_scrapi/core/arc.py
poll_evaluation_run ¶
Polls an evaluation run until it reaches a terminal state.
Source code in src/cxas_scrapi/core/arc.py
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 | |
poll_operation ¶
Polls an operation; delegates to the evaluation run when present.
Source code in src/cxas_scrapi/core/arc.py
poll_detached_job ¶
Polls the session for a detached assistant job's terminal status.
Source code in src/cxas_scrapi/core/arc.py
ArcError ¶
Bases: RuntimeError
Base error for Arc client failures.
ArcApiError ¶
Bases: ArcError
An HTTP error returned by the API, with the decoded status if any.
Source code in src/cxas_scrapi/core/arc.py
ArcStreamTimeout ¶
ArcStreamInterrupted ¶
iter_stream_json ¶
Yields JSON objects from a streaming REST response.
Handles both ?alt=sse (data: {...} lines) and the default One Platform JSON-array framing ([{...},{...}]).
Source code in src/cxas_scrapi/core/arc.py
TurnStreamEvents dataclass ¶
TurnStreamEvents(text='', thoughts=list(), statuses=list(), errors=list(), tool_calls=list(), tool_responses=list(), artifacts=list(), option_questions=list(), confirmation_requests=list(), confirmation_actions=list(), contract_metadata=None, initiated_lros=list(), handoff=None, turn_completed=None, last_sequence_number=None)
Aggregates all structured events emitted during an assistant turn.
pending_confirmation_requests property ¶
Cards raised in this turn that the client has not answered yet.
pending_option_questions property ¶
Standalone option questions not owned by a confirmation card.
merge ¶
Merges events from a subsequent stream (e.g. LRO resumption).
Source code in src/cxas_scrapi/utils/arc_events.py
to_dict ¶
format_markdown ¶
Renders the turn as markdown sections, mirroring the console.
Source code in src/cxas_scrapi/utils/arc_events.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
arc_confirmation_policy ¶
Decides how a programmatic Arc client answers confirmation cards.
The CXAS Console renders every AssistantConfirmationRequest from the CXAS AI assistant as a card. A human can click Confirm/Decline, pick options on a question (ask_user) card, skip the questions, or ignore the card and type in the composer, which the server treats as a reply to the paused tool call. A ConfirmationPolicy reproduces those choices for scripts, the cxas arc CLI and test harnesses, so that programmatic traffic exercises the same server paths as the Console.
Answer text for question cards uses the Console's wire format for submitted option choices.
ConfirmationPolicy ¶
ManualPolicy ¶
Bases: ConfirmationPolicy
Surfaces cards without answering them (the caller decides).
ClickPolicy ¶
Bases: ConfirmationPolicy
Clicks like an attentive user: confirm gates, answer question cards.
Question cards are answered by, in order: an explicit answers entry keyed by confirmation id or tool, the question_answerer callback (e.g. a test harness's deterministic responder), then the first option of every question.
Initializes ClickPolicy with explicit answers and deny globs.
Source code in src/cxas_scrapi/utils/arc_confirmation_policy.py
MixedPolicy ¶
MixedPolicy(typed_reply_probability=DEFAULT_TYPED_REPLY_PROBABILITY, seed=None, typed_confirm_text=DEFAULT_TYPED_CONFIRM_TEXT, typed_deny_text=DEFAULT_TYPED_DENY_TEXT, **click_kwargs)
Bases: ClickPolicy
Like click, but types the answer instead with probability p.
In production most users type a reply instead of clicking the card, which exercises a different server path than a click. The draw is seeded and logged so a failing run can be replayed.
Initializes MixedPolicy with a seeded typed-reply probability.
Source code in src/cxas_scrapi/utils/arc_confirmation_policy.py
decide ¶
See base class.
Source code in src/cxas_scrapi/utils/arc_confirmation_policy.py
describe ¶
ScriptedPolicy ¶
ScriptedPolicy(rules=(), default='click', typed_reply_probability=0.0, seed=None, typed_confirm_text=DEFAULT_TYPED_CONFIRM_TEXT, typed_deny_text=DEFAULT_TYPED_DENY_TEXT, question_answerer=None, answers=None, deny_tools=())
Bases: ConfirmationPolicy
Answers cards from an ordered rule list, falling back to click.
Script schema (dict or YAML):
default: click # click | manual
typed_reply_probability: 0.0
seed: 0
typed_confirm_text: "Yes"
rules:
- {tool: "deploy*", action: type, text: "yes"}
- {kind: question, action: skip_all}
Initializes ScriptedPolicy with ordered rules and a fallback.
Source code in src/cxas_scrapi/utils/arc_confirmation_policy.py
reset ¶
from_dict classmethod ¶
Builds a policy from a parsed script (see the class docstring).
Source code in src/cxas_scrapi/utils/arc_confirmation_policy.py
from_yaml classmethod ¶
Builds a policy from YAML (optionally under confirmation_script:).
The yaml package is imported lazily so the module stays importable without it.
Source code in src/cxas_scrapi/utils/arc_confirmation_policy.py
decide ¶
See base class.
Source code in src/cxas_scrapi/utils/arc_confirmation_policy.py
describe ¶
Returns the rule count, default and fallback policy.
make_policy ¶
make_policy(name, typed_reply_probability=None, seed=None, script=None, question_answerer=None, answers=None, deny_tools=())
Builds a policy from its CLI/config name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | None | Policy name ("manual", "click", "mixed", or "scripted"). None or empty defaults to "manual". | required |
typed_reply_probability | float | None | Probability of typing instead of clicking for "mixed" or "scripted" policies. None uses DEFAULT_TYPED_REPLY_PROBABILITY for "mixed". | None |
seed | int | None | Optional RNG seed for "mixed" or "scripted" policies; None picks a time-based seed for "mixed" or 0 for "scripted". | None |
script | Mapping[str, Any] | None | Script configuration mapping for "scripted" policy; None uses an empty script. | None |
question_answerer | QuestionAnswerer | None | Optional callback returning an answer for question cards. | None |
answers | Mapping[str, str] | None | Optional explicit answer overrides keyed by confirmation ID or tool name. | None |
deny_tools | Sequence[str] | Optional fnmatch globs of gated tools to automatically decline under "click", "mixed", or "scripted" fallback. | () |
Returns:
| Type | Description |
|---|---|
ConfirmationPolicy | The configured ConfirmationPolicy instance. |
Raises:
| Type | Description |
|---|---|
ValueError | If |