Skip to content

NOJIRA-Aicalls-add-messages-endpoint - #1142

Merged
pchero merged 22 commits into
mainfrom
NOJIRA-Aicalls-add-messages-endpoint
Jul 29, 2026
Merged

NOJIRA-Aicalls-add-messages-endpoint#1142
pchero merged 22 commits into
mainfrom
NOJIRA-Aicalls-add-messages-endpoint

Conversation

@pchero

@pchero pchero commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Make reference_type=contact_case AIcalls behave correctly end-to-end through the existing service_agents API surface, powering the new Case Insight Assistant panel in square-admin without any new backend endpoints.

  • bin-ai-manager: startReferenceTypeContactCase now triggers the first AI turn on genuine creation and advances the AIcall to Progressing, matching every other reference type's lifecycle
  • bin-ai-manager: startReferenceTypeContactCase terminates idle-expired contact_case AIcalls instead of reusing them forever, with precedence over the existing stuck-Initiating retry recovery path
  • bin-ai-manager: Add a per-AIcall send cooldown (aicall_send_cooldown_seconds, default 3s) to bound LLM spend from rapid repeated sends, enforced uniformly across both call and non-call reference types
  • bin-openapi-manager: assistance_id is no longer unconditionally required on POST /service_agents/aicalls -- omit it for assistance_type=ai + reference_type=contact_case to get server-side Insight AI resolution
  • bin-api-manager: Nil-safe handling of the now-optional assistance_id field, regenerated in sync with bin-openapi-manager
  • bin-api-manager: ServiceAgentAIcallCreate now rejects contact_case references belonging to a foreign customer, resolves assistance_id automatically via the customer's own type=insight AI when omitted, and deletes its freshly-created activeflow whenever AIV1AIcallStart returns a reused aicall
  • bin-ai-manager: ai.FieldStruct was silently dropping the type filter, which broke server-side Insight AI resolution's type-scoping -- add it
  • bin-api-manager: Document the new assistance_id-omitted resolution behavior and the send cooldown in the service_agents RST docs

pchero added 22 commits July 29, 2026 04:28
- bin-ai-manager, bin-api-manager, bin-openapi-manager: Add 8-task TDD implementation plan for the Case Insight Assistant backend fixes
…an review

- Fix Task 1/2 gomock expectations (missing PublishWebhookEvent, wrong AIcallUpdate fields, existing test cases broken by the new code path)
- Rewrite Tasks 5-7 to match the real flat-struct Test_ServiceAgentAIcallCreate instead of a nonexistent mockSetup-closure convention
…dings

- Fix Task 5-7 test cases: set ActiveflowID on non-reused responseAIcall fixtures so the reuse-detection condition doesn't false-positive
- Fix Task 1: cover all four pre-existing successful-create test cases, not just two
- Resolve Task 8's file-selection ambiguity with a concrete decision
- Include regenerated gen.go in the Tasks 5-7 commit
…dings

- Fix TimeNow() pointer type mismatch in Task 2's new test (utilHandler.TimeNow() returns *time.Time)
- Renumber Task 1/2's new-case UUID prefixes to avoid visual collision with pre-existing cases
…assistance_id in the HTTP handler

- Task 4 now fixes server/service_agents_aicalls.go's now-broken direct UUID cast after the openapi spec change, with a request-body-level test proving the omitted field reaches the service handler as uuid.Nil
- Add the affected files to the File Structure table
…e both codegen targets

- Task 4 now regenerates bin-openapi-manager/gens/models/gen.go in addition to bin-api-manager's copy, in the correct order, per bin-openapi-manager/CLAUDE.md
- Add a consumer-safety grep check and update File Structure table + commit file list
…ng type filter field

- ai.FieldStruct was silently dropping the 'type' filter, making resolveInsightAIID return any AI instead of only Insight-typed ones
- Update File Structure table, Tasks 5-7 commit, and Architecture summary
…type comparison in filters_test.go

- ConvertFilters returns the underlying string for named string-kind fields, not the named type; assert against string(ai.TypeInsight)
…n approved

- Plan review loop closed after 13 rounds (2 consecutive approvals, rounds 12-13)
…t_case AIcall creation

- bin-ai-manager: startReferenceTypeContactCase now starts a pipecatcall and advances to Progressing on genuine creation, matching every other reference type's lifecycle
…tact_case pipecatcall start

- bin-ai-manager: In startReferenceTypeContactCase, treat a failed
  PipecatV1PipecatcallTerminateWithDelay as best-effort (log and
  continue) instead of returning a hard error, matching the sibling
  startReferenceTypeConversation. The AIcall row and pipecatcall are
  already created at that point, so failing the call left the AIcall
  stuck at StatusInitiating with an orphaned pipecatcall and no
  scheduled termination.
- bin-ai-manager: Add a Debugf log after a successful startPipecatcall
  call in startReferenceTypeContactCase for observability parity with
  startReferenceTypeConversation.
- bin-ai-manager: Add test coverage for the startPipecatcall error
  branch and for the corrected log-and-continue behavior of
  PipecatV1PipecatcallTerminateWithDelay in
  Test_startReferenceTypeContactCase.
- bin-ai-manager: Remove the redundant "starts pipecatcall and
  advances to progressing" happy-path test case, which duplicated the
  updated "create succeeds on first attempt" case.
…t Initiating on transient pipecat outage

- bin-ai-manager: In startReferenceTypeContactCase, retry the pipecatcall-start sequence when the duplicate-key reuse branch finds an existing row still at StatusInitiating, instead of returning it as-is
- bin-ai-manager: Extract the start-pipecatcall + terminate-with-delay + advance-to-Progressing sequence into a shared startContactCaseTurn helper used by both the genuine-create path and the new reuse-retry path
- bin-ai-manager: Document that the genuine-create branch intentionally advances to Progressing after starting the pipecatcall, unlike startReferenceTypeConversation
- bin-ai-manager: Add a test case covering the Initiating-status reuse-retry path in Test_startReferenceTypeContactCase
…-Initiating retry branch

- start.go: top-level Flow doc comment now documents the Initiating-retry sub-branch found in round-4 code review
…case AIcalls instead of reusing forever

- bin-ai-manager: startReferenceTypeContactCase now checks isAIcallIdleExpired on the reuse path and explicitly terminates + retries, skipping the recreate rate limit (which does not apply to organic idle expiry)
…er stuck-Initiating retry

- bin-ai-manager: Add test case covering a contact_case AIcall row that is
  both stuck at StatusInitiating and idle-expired, asserting the
  idle-expiry branch (terminate+recreate) wins over the stuck-Initiating
  retry branch (resume via startContactCaseTurn)
- bin-ai-manager: Add inline comment in startReferenceTypeContactCase
  documenting that the idle-expiry check runs before, and takes
  precedence over, the stuck-Initiating retry check
…al increments in idle-expiry tests

- start_test.go: add expectIdleExpiredInc field, following the existing expectRateLimitedInc pattern, and assert it on both idle-expiry test cases so a future regression that drops the Inc() call would be caught
- bin-ai-manager: Send() now rejects a new send within aicall_send_cooldown_seconds (default 3s) of the AIcall's last update, bounding LLM spend from rapid repeated sends
…renceTypeCall path

- bin-ai-manager: Send() now explicitly refreshes TMUpdate on every successful dispatch (both ReferenceTypeCall and other reference types), so the cooldown actually bounds rapid sends on live-call AIcalls, not just contact_case ones
- bin-ai-manager: add test coverage for outside-cooldown success, ReferenceTypeCall cooldown enforcement, and TMUpdate==nil fresh-AIcall pass-through
…y optional for contact_case AI creates

- bin-openapi-manager: assistance_id is no longer unconditionally required on POST /service_agents/aicalls -- omit it for assistance_type=ai + reference_type=contact_case to get server-side Insight AI resolution
- bin-api-manager: fix the generated-type change's fallout in the HTTP handler (nil-safe assistance_id extraction) and add a request-body-level test proving the omitted field reaches the service handler as uuid.Nil
- bin-api-manager: ServiceAgentAIcallCreate now rejects contact_case references belonging to a foreign customer, resolves assistance_id automatically when omitted for assistance_type=ai + reference_type=contact_case, and deletes its freshly-created activeflow whenever AIV1AIcallStart returns a reused aicall
- bin-ai-manager: ai.FieldStruct was silently dropping the type filter, which broke resolveInsightAIID's Insight-only scoping -- add it
… from ownership mismatch

- bin-api-manager: ServiceAgentAIcallCreate's contact_case ownership check no longer collapses a ContactV1CaseGet RPC/lookup failure into ErrPermissionDenied -- only an actual customer_id mismatch returns 403, matching the established Get-then-compare idiom used elsewhere in this package (ai.go's RAG check, activeflows.go's flow check)
…tion and send cooldown in RST docs

- bin-api-manager: Add RST documentation for the new assistance_id-omitted resolution behavior and the send cooldown on service_agents AIcall endpoints
@pchero
pchero merged commit 1a0ff19 into main Jul 29, 2026
10 checks passed
@pchero
pchero deleted the NOJIRA-Aicalls-add-messages-endpoint branch July 29, 2026 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant