@steerable/agent-shell 0.12.8 → 0.14.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.
- package/dist/attachments/image-normalizer.d.ts +39 -0
- package/dist/attachments/image-normalizer.js +131 -0
- package/dist/auth/credentials.d.ts +83 -0
- package/dist/auth/credentials.js +426 -0
- package/dist/auth/index.d.ts +33 -9
- package/dist/auth/index.js +85 -0
- package/dist/host/external-pack-loader.d.ts +40 -0
- package/dist/host/external-pack-loader.js +247 -0
- package/dist/host/http-routes.d.ts +6 -2
- package/dist/host/http-routes.js +5 -2
- package/dist/host/pack-assembly.d.ts +9 -1
- package/dist/host/runtime.js +54 -1
- package/dist/image-attachment.js +10 -5
- package/dist/local-backend/coreloop-stream.d.ts +4 -0
- package/dist/local-backend/coreloop-stream.js +4 -0
- package/dist/local-backend/fact-chain.d.ts +38 -0
- package/dist/local-backend/fact-chain.js +145 -0
- package/dist/local-backend/router.d.ts +3 -1
- package/dist/local-backend/router.js +602 -45
- package/dist/local-backend/state-reconcile.d.ts +42 -0
- package/dist/local-backend/state-reconcile.js +81 -0
- package/dist/local-backend/title-refresh.d.ts +39 -0
- package/dist/local-backend/title-refresh.js +80 -0
- package/dist/local-backend/turn-security-context.d.ts +42 -0
- package/dist/local-backend/turn-security-context.js +62 -0
- package/dist/local-backend/ui-surface.d.ts +75 -0
- package/dist/local-backend/ui-surface.js +368 -0
- package/dist/local-executor.d.ts +3 -0
- package/dist/local-executor.js +40 -1
- package/dist/memory/authoring-candidates.d.ts +26 -0
- package/dist/memory/authoring-candidates.js +73 -0
- package/dist/memory/injector.d.ts +38 -0
- package/dist/memory/injector.js +122 -0
- package/dist/memory/routes.d.ts +16 -0
- package/dist/memory/routes.js +146 -0
- package/dist/memory/store.d.ts +176 -0
- package/dist/memory/store.js +623 -0
- package/dist/memory/tools.d.ts +83 -0
- package/dist/memory/tools.js +143 -0
- package/dist/present-files.d.ts +25 -1
- package/dist/present-files.js +170 -2
- package/dist/raster-image.d.ts +7 -1
- package/dist/raster-image.js +34 -0
- package/dist/scenario/applications.d.ts +52 -0
- package/dist/scenario/applications.js +143 -0
- package/dist/scenario/pack.d.ts +1 -1
- package/dist/security/policy-digest.d.ts +28 -0
- package/dist/security/policy-digest.js +104 -0
- package/dist/server/http-server.js +23 -6
- package/dist/sidecar/boot.d.ts +1 -0
- package/dist/sidecar/boot.js +2 -0
- package/dist/sidecar/index.d.ts +1 -1
- package/dist/sidecar/rejected-before-effect.d.ts +22 -0
- package/dist/sidecar/rejected-before-effect.js +63 -0
- package/dist/sidecar/reverse-approval.d.ts +2 -0
- package/dist/sidecar/reverse-approval.js +28 -5
- package/dist/sidecar/reverse-tools.d.ts +3 -0
- package/dist/sidecar/reverse-tools.js +146 -18
- package/dist/sidecar/supervisor.d.ts +31 -1
- package/dist/sidecar/supervisor.js +47 -0
- package/dist/sidecar/types.d.ts +72 -0
- package/dist/storage/fde-fallback.d.ts +4 -0
- package/dist/storage/fde-fallback.js +354 -0
- package/dist/storage/fde-types.d.ts +215 -0
- package/dist/storage/fde-types.js +1 -0
- package/dist/storage/index.d.ts +113 -1
- package/dist/storage/index.js +797 -0
- package/dist/storage/insights-redact.d.ts +4 -1
- package/dist/storage/insights-redact.js +6 -1
- package/dist/storage/process-locks.d.ts +1 -1
- package/dist/storage/process-locks.js +1 -1
- package/dist/storage/reliability-summary.d.ts +1 -1
- package/dist/storage/scoped-store.d.ts +3 -1
- package/dist/tool-presentation.js +20 -0
- package/dist/tool-router.d.ts +20 -1
- package/dist/tool-router.js +76 -12
- package/package.json +5 -5
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* State Ownership Projection Reconciliation & Pin Invariants (R2-1).
|
|
3
|
+
*
|
|
4
|
+
* Implements the rules documented in `docs/spec/state-ownership.md`:
|
|
5
|
+
* 1. Sidecar `sessions.db` is authoritative for turn history (`LedgerTurnEntry[]`);
|
|
6
|
+
* host `chat_messages` is a projection that backfills missing entries on
|
|
7
|
+
* reconnect without duplicating already-projected turns.
|
|
8
|
+
* 2. Session `Pin` bindings (`workspaceRoot`, `agentId`, `applicationId`) are
|
|
9
|
+
* captured at session creation and never drift when global defaults change.
|
|
10
|
+
*/
|
|
11
|
+
import type { ScopedStore } from '../storage/scoped-store.js';
|
|
12
|
+
export interface LedgerHistoryItem {
|
|
13
|
+
readonly ledgerSeq: number;
|
|
14
|
+
readonly role: 'user' | 'assistant' | 'system';
|
|
15
|
+
readonly content: string;
|
|
16
|
+
readonly metadata?: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
export interface PinnedSessionSpec {
|
|
19
|
+
readonly workspaceRoot: string | null;
|
|
20
|
+
readonly agentId: string;
|
|
21
|
+
readonly applicationId: string;
|
|
22
|
+
readonly pinnedAt: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Reconciles host `chat_messages` projection against authoritative sidecar
|
|
26
|
+
* ledger items (`sessions.db`). Missing items are appended in `ledgerSeq`
|
|
27
|
+
* order with `ledgerSeq` recorded in `message_metadata`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function reconcileChatProjectionFromLedger(store: ScopedStore, chatId: string, ledgerItems: readonly LedgerHistoryItem[]): Promise<{
|
|
30
|
+
backfilledCount: number;
|
|
31
|
+
totalProjected: number;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* Captures or reads the immutable `PinnedSessionSpec` for `chatId` so subsequent
|
|
35
|
+
* changes to global default agent or active application never mutate an existing
|
|
36
|
+
* session's bound environment (`Pin` category in `state-ownership.md`).
|
|
37
|
+
*/
|
|
38
|
+
export declare function ensurePinnedSessionSpec(store: ScopedStore, chatId: string, globalDefaults: {
|
|
39
|
+
readonly workspaceRoot?: string | null;
|
|
40
|
+
readonly defaultAgentId: string;
|
|
41
|
+
readonly activeApplicationId: string;
|
|
42
|
+
}): Promise<PinnedSessionSpec>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reconciles host `chat_messages` projection against authoritative sidecar
|
|
3
|
+
* ledger items (`sessions.db`). Missing items are appended in `ledgerSeq`
|
|
4
|
+
* order with `ledgerSeq` recorded in `message_metadata`.
|
|
5
|
+
*/
|
|
6
|
+
export async function reconcileChatProjectionFromLedger(store, chatId, ledgerItems) {
|
|
7
|
+
const existing = await store.listMessages(chatId);
|
|
8
|
+
const seenLedgerSeqs = new Set();
|
|
9
|
+
const existingUserAssistantPairs = new Set();
|
|
10
|
+
for (const msg of existing) {
|
|
11
|
+
if (msg.messageMetadata) {
|
|
12
|
+
try {
|
|
13
|
+
const parsed = JSON.parse(msg.messageMetadata);
|
|
14
|
+
if (typeof parsed.ledgerSeq === 'number') {
|
|
15
|
+
seenLedgerSeqs.add(parsed.ledgerSeq);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
// Ignore malformed legacy metadata
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
existingUserAssistantPairs.add(`${msg.role}\u0000${msg.content}`);
|
|
23
|
+
}
|
|
24
|
+
const sorted = [...ledgerItems].sort((a, b) => a.ledgerSeq - b.ledgerSeq);
|
|
25
|
+
let backfilledCount = 0;
|
|
26
|
+
for (const item of sorted) {
|
|
27
|
+
if (seenLedgerSeqs.has(item.ledgerSeq))
|
|
28
|
+
continue;
|
|
29
|
+
// Avoid duplicating pre-existing messages written before crash without ledgerSeq
|
|
30
|
+
if (seenLedgerSeqs.size === 0 &&
|
|
31
|
+
existingUserAssistantPairs.has(`${item.role}\u0000${item.content}`)) {
|
|
32
|
+
existingUserAssistantPairs.delete(`${item.role}\u0000${item.content}`);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const mergedMeta = {
|
|
36
|
+
...(item.metadata ?? {}),
|
|
37
|
+
ledgerSeq: item.ledgerSeq,
|
|
38
|
+
reconciledFromLedger: true,
|
|
39
|
+
};
|
|
40
|
+
await store.addMessage(chatId, item.role, item.content, JSON.stringify(mergedMeta));
|
|
41
|
+
seenLedgerSeqs.add(item.ledgerSeq);
|
|
42
|
+
backfilledCount += 1;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
backfilledCount,
|
|
46
|
+
totalProjected: existing.length + backfilledCount,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Captures or reads the immutable `PinnedSessionSpec` for `chatId` so subsequent
|
|
51
|
+
* changes to global default agent or active application never mutate an existing
|
|
52
|
+
* session's bound environment (`Pin` category in `state-ownership.md`).
|
|
53
|
+
*/
|
|
54
|
+
export async function ensurePinnedSessionSpec(store, chatId, globalDefaults) {
|
|
55
|
+
const key = `chat_pin:${chatId}`;
|
|
56
|
+
if (typeof store.getCustomSetting === 'function') {
|
|
57
|
+
const raw = await store.getCustomSetting(key);
|
|
58
|
+
if (raw) {
|
|
59
|
+
try {
|
|
60
|
+
const parsed = JSON.parse(raw);
|
|
61
|
+
if (parsed && typeof parsed.agentId === 'string' && typeof parsed.applicationId === 'string') {
|
|
62
|
+
return Object.freeze(parsed);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// Re-pin if corrupted
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const chat = await store.getChat(chatId);
|
|
71
|
+
const pinned = Object.freeze({
|
|
72
|
+
workspaceRoot: globalDefaults.workspaceRoot ?? null,
|
|
73
|
+
agentId: chat?.agentId ?? globalDefaults.defaultAgentId,
|
|
74
|
+
applicationId: globalDefaults.activeApplicationId,
|
|
75
|
+
pinnedAt: new Date().toISOString(),
|
|
76
|
+
});
|
|
77
|
+
if (typeof store.setCustomSetting === 'function') {
|
|
78
|
+
await store.setCustomSetting(key, JSON.stringify(pinned));
|
|
79
|
+
}
|
|
80
|
+
return pinned;
|
|
81
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Background title drift refresh via controlled `SideCallPort` guardrails (Phase 9 R7-3).
|
|
3
|
+
*
|
|
4
|
+
* Strictly preserves Phase 1 `title_source` ownership:
|
|
5
|
+
* - Whenever `chat.titleSource === 'user'` (manually renamed by user), returns
|
|
6
|
+
* immediately with ZERO sidecall invocations and NEVER overwrites the title.
|
|
7
|
+
* - For `chat.titleSource === 'auto'`, checks every `intervalAssistantTurns` (default 6)
|
|
8
|
+
* using scrubbed `2,000` head + `8,000` tail context and a `15s` hard timeout.
|
|
9
|
+
*/
|
|
10
|
+
import { SecretRedactor } from '../auth/credentials.js';
|
|
11
|
+
import type { ScopedStore } from '../storage/scoped-store.js';
|
|
12
|
+
export declare const TITLE_REFRESH_HEAD_CHARS = 2000;
|
|
13
|
+
export declare const TITLE_REFRESH_TAIL_CHARS = 8000;
|
|
14
|
+
export declare const TITLE_REFRESH_TIMEOUT_MS = 15000;
|
|
15
|
+
export declare const TITLE_REFRESH_TURN_INTERVAL = 6;
|
|
16
|
+
export interface TitleDriftDecision {
|
|
17
|
+
readonly drifted: boolean;
|
|
18
|
+
readonly newTitle?: string;
|
|
19
|
+
}
|
|
20
|
+
export type TitleDriftEvaluator = (input: {
|
|
21
|
+
readonly caller: 'sidecall:title_refresh';
|
|
22
|
+
readonly currentTitle: string;
|
|
23
|
+
readonly scrubbedHead: string;
|
|
24
|
+
readonly scrubbedTail: string;
|
|
25
|
+
readonly signal: AbortSignal;
|
|
26
|
+
}) => Promise<TitleDriftDecision>;
|
|
27
|
+
export declare function maybeRefreshDriftedChatTitle(options: {
|
|
28
|
+
readonly store: ScopedStore;
|
|
29
|
+
readonly chatId: string;
|
|
30
|
+
readonly evaluateDrift: TitleDriftEvaluator;
|
|
31
|
+
readonly redactor?: SecretRedactor;
|
|
32
|
+
readonly parentSignal?: AbortSignal;
|
|
33
|
+
readonly intervalAssistantTurns?: number;
|
|
34
|
+
readonly timeoutMs?: number;
|
|
35
|
+
}): Promise<{
|
|
36
|
+
readonly updated: boolean;
|
|
37
|
+
readonly skippedReason?: 'chat_not_found' | 'user_locked_title' | 'below_interval' | 'no_drift' | 'degraded';
|
|
38
|
+
readonly title?: string;
|
|
39
|
+
}>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Background title drift refresh via controlled `SideCallPort` guardrails (Phase 9 R7-3).
|
|
3
|
+
*
|
|
4
|
+
* Strictly preserves Phase 1 `title_source` ownership:
|
|
5
|
+
* - Whenever `chat.titleSource === 'user'` (manually renamed by user), returns
|
|
6
|
+
* immediately with ZERO sidecall invocations and NEVER overwrites the title.
|
|
7
|
+
* - For `chat.titleSource === 'auto'`, checks every `intervalAssistantTurns` (default 6)
|
|
8
|
+
* using scrubbed `2,000` head + `8,000` tail context and a `15s` hard timeout.
|
|
9
|
+
*/
|
|
10
|
+
import { SecretRedactor } from '../auth/credentials.js';
|
|
11
|
+
export const TITLE_REFRESH_HEAD_CHARS = 2_000;
|
|
12
|
+
export const TITLE_REFRESH_TAIL_CHARS = 8_000;
|
|
13
|
+
export const TITLE_REFRESH_TIMEOUT_MS = 15_000;
|
|
14
|
+
export const TITLE_REFRESH_TURN_INTERVAL = 6;
|
|
15
|
+
export async function maybeRefreshDriftedChatTitle(options) {
|
|
16
|
+
const chat = await options.store.getChat(options.chatId);
|
|
17
|
+
if (!chat) {
|
|
18
|
+
return { updated: false, skippedReason: 'chat_not_found' };
|
|
19
|
+
}
|
|
20
|
+
// Invariant (T-SC-05): user-renamed chats are NEVER touched or evaluated
|
|
21
|
+
if (chat.titleSource === 'user') {
|
|
22
|
+
return { updated: false, skippedReason: 'user_locked_title', title: chat.title };
|
|
23
|
+
}
|
|
24
|
+
const messages = await options.store.listMessages(options.chatId, 200);
|
|
25
|
+
const assistantMessages = messages.filter((m) => m.role === 'assistant');
|
|
26
|
+
const interval = options.intervalAssistantTurns ?? TITLE_REFRESH_TURN_INTERVAL;
|
|
27
|
+
if (assistantMessages.length < interval || assistantMessages.length % interval !== 0) {
|
|
28
|
+
return { updated: false, skippedReason: 'below_interval', title: chat.title };
|
|
29
|
+
}
|
|
30
|
+
const firstUser = messages.find((m) => m.role === 'user')?.content ?? '';
|
|
31
|
+
const recentTailText = messages
|
|
32
|
+
.slice(-6)
|
|
33
|
+
.map((m) => `${m.role}: ${m.content}`)
|
|
34
|
+
.join('\n');
|
|
35
|
+
const redactor = options.redactor ?? new SecretRedactor();
|
|
36
|
+
const scrubbedHead = redactor.redact(firstUser.slice(0, TITLE_REFRESH_HEAD_CHARS));
|
|
37
|
+
const scrubbedTail = redactor.redact(recentTailText.slice(-TITLE_REFRESH_TAIL_CHARS));
|
|
38
|
+
const controller = new AbortController();
|
|
39
|
+
const timeoutMs = options.timeoutMs ?? TITLE_REFRESH_TIMEOUT_MS;
|
|
40
|
+
const timer = setTimeout(() => controller.abort(new Error('title_refresh_timeout')), timeoutMs);
|
|
41
|
+
const onParentAbort = () => controller.abort(options.parentSignal?.reason);
|
|
42
|
+
if (options.parentSignal) {
|
|
43
|
+
if (options.parentSignal.aborted) {
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
return { updated: false, skippedReason: 'degraded', title: chat.title };
|
|
46
|
+
}
|
|
47
|
+
options.parentSignal.addEventListener('abort', onParentAbort, { once: true });
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const decision = await options.evaluateDrift({
|
|
51
|
+
caller: 'sidecall:title_refresh',
|
|
52
|
+
currentTitle: chat.title,
|
|
53
|
+
scrubbedHead,
|
|
54
|
+
scrubbedTail,
|
|
55
|
+
signal: controller.signal,
|
|
56
|
+
});
|
|
57
|
+
const candidate = decision.newTitle?.trim();
|
|
58
|
+
if (!decision.drifted || !candidate || candidate === chat.title) {
|
|
59
|
+
return { updated: false, skippedReason: 'no_drift', title: chat.title };
|
|
60
|
+
}
|
|
61
|
+
// Re-check chat.titleSource right before writing in case user renamed concurrently
|
|
62
|
+
const freshChat = await options.store.getChat(options.chatId);
|
|
63
|
+
if (!freshChat || freshChat.titleSource === 'user') {
|
|
64
|
+
return { updated: false, skippedReason: 'user_locked_title', title: freshChat?.title };
|
|
65
|
+
}
|
|
66
|
+
await options.store.updateChat(options.chatId, {
|
|
67
|
+
title: candidate.slice(0, 80),
|
|
68
|
+
titleSource: 'auto',
|
|
69
|
+
});
|
|
70
|
+
return { updated: true, title: candidate.slice(0, 80) };
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// Guardrail #4: silent fail-safe degradation
|
|
74
|
+
return { updated: false, skippedReason: 'degraded', title: chat.title };
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
options.parentSignal?.removeEventListener('abort', onParentAbort);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authoritative in-memory turn security context & host activity tracker.
|
|
3
|
+
*
|
|
4
|
+
* `Principal` and `PackCredentialClient` stay inside the trusted Node host
|
|
5
|
+
* process keyed by `chatId`; the sidecar/model never supplies or mutates
|
|
6
|
+
* `principal`, preventing prompt-injected `args.principal` escalation.
|
|
7
|
+
*/
|
|
8
|
+
import type { OriginChannel, PackCredentialClient, Principal } from '@steerable/pack-sdk';
|
|
9
|
+
import { SecretRedactor } from '../auth/credentials.js';
|
|
10
|
+
import type { ScopedStore } from '../storage/scoped-store.js';
|
|
11
|
+
export interface ActiveTurnSecurityContext {
|
|
12
|
+
readonly chatId: string;
|
|
13
|
+
readonly principal: Principal;
|
|
14
|
+
readonly originChannel: OriginChannel;
|
|
15
|
+
readonly store: ScopedStore;
|
|
16
|
+
readonly redactor: SecretRedactor;
|
|
17
|
+
readonly credentials: PackCredentialClient;
|
|
18
|
+
readonly startedAtMs: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function touchHostActivity(): void;
|
|
21
|
+
export declare function bindHostPendingCounters(counters: {
|
|
22
|
+
pendingApprovals: () => number;
|
|
23
|
+
pendingQuestions?: () => number;
|
|
24
|
+
}): void;
|
|
25
|
+
export declare function beginTurnSecurityContext(input: {
|
|
26
|
+
chatId: string;
|
|
27
|
+
principal?: Principal;
|
|
28
|
+
originChannel?: OriginChannel;
|
|
29
|
+
store: ScopedStore;
|
|
30
|
+
}): ActiveTurnSecurityContext;
|
|
31
|
+
export declare function getTurnSecurityContext(chatId: string | undefined): ActiveTurnSecurityContext | undefined;
|
|
32
|
+
export declare function endTurnSecurityContext(chatId: string, expected?: ActiveTurnSecurityContext): void;
|
|
33
|
+
export interface HostActivitySnapshot {
|
|
34
|
+
readonly state: 'running_turn' | 'awaiting_human' | 'idle';
|
|
35
|
+
readonly activeTurns: number;
|
|
36
|
+
readonly pendingApprovals: number;
|
|
37
|
+
readonly lastActivityAt: string;
|
|
38
|
+
}
|
|
39
|
+
export declare function getHostActivitySnapshot(overrides?: {
|
|
40
|
+
readonly pendingApprovals?: number;
|
|
41
|
+
readonly pendingReconciliations?: number;
|
|
42
|
+
}): HostActivitySnapshot;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { LOCAL_DEFAULT_PRINCIPAL } from '../auth/index.js';
|
|
2
|
+
import { SecretRedactor, defaultCredentialBroker, } from '../auth/credentials.js';
|
|
3
|
+
const activeTurns = new Map();
|
|
4
|
+
let lastActivityAtMs = Date.now();
|
|
5
|
+
let pendingApprovalsGetter = null;
|
|
6
|
+
let pendingQuestionsGetter = null;
|
|
7
|
+
export function touchHostActivity() {
|
|
8
|
+
lastActivityAtMs = Date.now();
|
|
9
|
+
}
|
|
10
|
+
export function bindHostPendingCounters(counters) {
|
|
11
|
+
pendingApprovalsGetter = counters.pendingApprovals;
|
|
12
|
+
pendingQuestionsGetter = counters.pendingQuestions ?? null;
|
|
13
|
+
}
|
|
14
|
+
export function beginTurnSecurityContext(input) {
|
|
15
|
+
touchHostActivity();
|
|
16
|
+
const principal = input.principal ?? LOCAL_DEFAULT_PRINCIPAL;
|
|
17
|
+
const originChannel = input.originChannel ?? 'web';
|
|
18
|
+
const redactor = new SecretRedactor();
|
|
19
|
+
const credentials = defaultCredentialBroker.createClient(input.store, principal, redactor);
|
|
20
|
+
const ctx = {
|
|
21
|
+
chatId: input.chatId,
|
|
22
|
+
principal,
|
|
23
|
+
originChannel,
|
|
24
|
+
store: input.store,
|
|
25
|
+
redactor,
|
|
26
|
+
credentials,
|
|
27
|
+
startedAtMs: Date.now(),
|
|
28
|
+
};
|
|
29
|
+
activeTurns.set(input.chatId, ctx);
|
|
30
|
+
return ctx;
|
|
31
|
+
}
|
|
32
|
+
export function getTurnSecurityContext(chatId) {
|
|
33
|
+
if (!chatId)
|
|
34
|
+
return undefined;
|
|
35
|
+
return activeTurns.get(chatId);
|
|
36
|
+
}
|
|
37
|
+
export function endTurnSecurityContext(chatId, expected) {
|
|
38
|
+
touchHostActivity();
|
|
39
|
+
const current = activeTurns.get(chatId);
|
|
40
|
+
if (!current)
|
|
41
|
+
return;
|
|
42
|
+
if (!expected || current === expected) {
|
|
43
|
+
activeTurns.delete(chatId);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function getHostActivitySnapshot(overrides) {
|
|
47
|
+
const runningCount = activeTurns.size;
|
|
48
|
+
const approvals = (overrides?.pendingApprovals ??
|
|
49
|
+
(pendingApprovalsGetter?.() ?? 0) + (pendingQuestionsGetter?.() ?? 0)) +
|
|
50
|
+
(overrides?.pendingReconciliations ?? 0);
|
|
51
|
+
const state = runningCount > 0
|
|
52
|
+
? 'running_turn'
|
|
53
|
+
: approvals > 0
|
|
54
|
+
? 'awaiting_human'
|
|
55
|
+
: 'idle';
|
|
56
|
+
return {
|
|
57
|
+
state,
|
|
58
|
+
activeTurns: runningCount,
|
|
59
|
+
pendingApprovals: approvals,
|
|
60
|
+
lastActivityAt: new Date(lastActivityAtMs).toISOString(),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { UiSurfacePayload } from '@steerable/agent-protocol';
|
|
2
|
+
import { type OriginChannel, type Principal } from '../auth/index.js';
|
|
3
|
+
import type { ScopedStore } from '../storage/scoped-store.js';
|
|
4
|
+
import type { ToolRouter } from '../tool-router.js';
|
|
5
|
+
export declare const UI_SURFACE_RENDER_TOOL_NAME = "ui_surface_render";
|
|
6
|
+
export declare const MAX_UI_SURFACE_BYTES: number;
|
|
7
|
+
export declare const MAX_UI_SURFACE_TABLE_ROWS = 500;
|
|
8
|
+
export declare const ALLOWED_UI_SURFACE_COMPONENT_KINDS: Set<string>;
|
|
9
|
+
export declare const UI_SURFACE_RENDER_SCHEMA: {
|
|
10
|
+
name: string;
|
|
11
|
+
description: string;
|
|
12
|
+
mode: "ui";
|
|
13
|
+
inputSchema: {
|
|
14
|
+
type: string;
|
|
15
|
+
required: string[];
|
|
16
|
+
properties: {
|
|
17
|
+
surfaceId: {
|
|
18
|
+
type: string;
|
|
19
|
+
};
|
|
20
|
+
title: {
|
|
21
|
+
type: string;
|
|
22
|
+
};
|
|
23
|
+
subtitle: {
|
|
24
|
+
type: string;
|
|
25
|
+
};
|
|
26
|
+
placement: {
|
|
27
|
+
type: string;
|
|
28
|
+
enum: string[];
|
|
29
|
+
};
|
|
30
|
+
data: {
|
|
31
|
+
type: string;
|
|
32
|
+
};
|
|
33
|
+
components: {
|
|
34
|
+
type: string;
|
|
35
|
+
minItems: number;
|
|
36
|
+
};
|
|
37
|
+
actions: {
|
|
38
|
+
type: string;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
export declare function canonicalJsonSha256(value: unknown): string;
|
|
44
|
+
export declare function evaluateUiSurfaceDataBindings(data: Record<string, unknown> | undefined, ctx: {
|
|
45
|
+
readonly principal: Principal;
|
|
46
|
+
readonly chatId: string;
|
|
47
|
+
}): Promise<{
|
|
48
|
+
resolvedData: Record<string, unknown>;
|
|
49
|
+
sourceHashes: Record<string, string>;
|
|
50
|
+
}>;
|
|
51
|
+
export declare function validateUiSurfaceRawInput(raw: Record<string, unknown>): void;
|
|
52
|
+
export declare function renderAndPersistUiSurface(options: {
|
|
53
|
+
readonly store: ScopedStore;
|
|
54
|
+
readonly chatId: string;
|
|
55
|
+
readonly principal?: Principal;
|
|
56
|
+
readonly raw: Record<string, unknown>;
|
|
57
|
+
}): Promise<UiSurfacePayload>;
|
|
58
|
+
export declare function refreshPersistedUiSurface(options: {
|
|
59
|
+
readonly store: ScopedStore;
|
|
60
|
+
readonly chatId: string;
|
|
61
|
+
readonly surfaceId: string;
|
|
62
|
+
readonly principal?: Principal;
|
|
63
|
+
}): Promise<UiSurfacePayload | null>;
|
|
64
|
+
export declare function dispatchUiSurfaceAction(options: {
|
|
65
|
+
readonly store: ScopedStore;
|
|
66
|
+
readonly toolRouter: ToolRouter;
|
|
67
|
+
readonly chatId: string;
|
|
68
|
+
readonly surfaceId: string;
|
|
69
|
+
readonly principal?: Principal;
|
|
70
|
+
readonly originChannel?: OriginChannel;
|
|
71
|
+
readonly body: Record<string, unknown>;
|
|
72
|
+
}): Promise<{
|
|
73
|
+
status: number;
|
|
74
|
+
data: unknown;
|
|
75
|
+
}>;
|