feat: expand companion platform, device controls, and memoir workflows
ci / verify (push) Canceled after 0s
ci / verify (push) Canceled after 0s
Add customer and operator login, BLE provisioning, device fleet controls, call memory, memoir generation, and refreshed family and maintenance portals. Include API contracts, migrations, firmware sources, tests, assets, and deployment documentation.
This commit is contained in:
@@ -4,21 +4,21 @@ package vikingmemory
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"data-terminal-cloud/internal/memoryidentity"
|
||||
"data-terminal-cloud/internal/outbox"
|
||||
)
|
||||
|
||||
const AssistantID = "platform_companion_v1"
|
||||
const AssistantID = memoryidentity.AssistantID
|
||||
|
||||
type Options struct {
|
||||
BaseURL, ResourceID, CollectionName, APIKey, EventType, ProfileType string
|
||||
@@ -60,8 +60,7 @@ func NewClient(options Options) (*Client, error) {
|
||||
return &Client{options: options, http: &http.Client{Timeout: options.Timeout, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}}, nil
|
||||
}
|
||||
func UserID(tenantID, subjectID string) string {
|
||||
digest := sha256.Sum256([]byte(tenantID + ":" + subjectID))
|
||||
return "u_" + hex.EncodeToString(digest[:])
|
||||
return memoryidentity.UserID(tenantID, subjectID)
|
||||
}
|
||||
func SessionID(conversationID string, batch int) string {
|
||||
return "c_" + strings.ReplaceAll(conversationID, "-", "") + fmt.Sprintf("_%d", batch)
|
||||
@@ -94,7 +93,12 @@ func (c *Client) call(ctx context.Context, path string, payload map[string]any,
|
||||
code = *result.Code
|
||||
}
|
||||
failure := fmt.Errorf("Viking %s HTTP %d code %d", path, resp.StatusCode, code)
|
||||
if resp.StatusCode == 429 || resp.StatusCode >= 500 || (resp.StatusCode >= 200 && resp.StatusCode < 300) {
|
||||
// A valid nonzero business response requires classification/operator review.
|
||||
// Only an unreadable successful response has an uncertain write outcome.
|
||||
if resp.StatusCode == http.StatusTooManyRequests || code == 1000029 {
|
||||
return outbox.RetryAfter(failure, vikingRetryDelay(resp.Header.Get("Retry-After"), time.Now()))
|
||||
}
|
||||
if resp.StatusCode >= 500 || (resp.StatusCode >= 200 && resp.StatusCode < 300 && (err != nil || result.Code == nil)) {
|
||||
return outbox.Retryable(failure)
|
||||
}
|
||||
return failure
|
||||
@@ -106,6 +110,18 @@ func (c *Client) call(ctx context.Context, path string, payload map[string]any,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func vikingRetryDelay(header string, now time.Time) time.Duration {
|
||||
// Rate limits without a usable Retry-After get a full cooldown minute.
|
||||
// This prevents the short generic retry schedule exhausting the job.
|
||||
if seconds, err := strconv.ParseInt(strings.TrimSpace(header), 10, 64); err == nil && seconds > 0 {
|
||||
return time.Duration(min(seconds, int64((24*time.Hour)/time.Second))) * time.Second
|
||||
}
|
||||
if deadline, err := http.ParseTime(header); err == nil && deadline.After(now) {
|
||||
return min(deadline.Sub(now), 24*time.Hour)
|
||||
}
|
||||
return time.Minute
|
||||
}
|
||||
func (c *Client) AddSession(ctx context.Context, sessionID, userID string, messages []Message, when int64) error {
|
||||
if sessionID == "" || userID == "" || len(messages) == 0 || len(messages) > 100 {
|
||||
return errors.New("invalid Viking session")
|
||||
|
||||
Reference in New Issue
Block a user