@theoremjs/web 0.3.5-next.980.g83e419d → 0.3.5-next.981.gf4729de
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/README.md +269 -5
- package/client/chat.js +136 -21
- package/client/index.d.ts +1 -1
- package/client/run-session.d.ts +12 -2
- package/client/run-session.js +3 -3
- package/client/transcript-schema.d.ts +4 -0
- package/client/transcript-schema.js +9 -1
- package/client/transport.d.ts +33 -5
- package/client/transport.js +17 -5
- package/client/turn-client.d.ts +5 -1
- package/client/turn-client.js +1 -0
- package/package.json +2 -2
- package/server/conversation-core.d.ts +264 -0
- package/server/conversation-core.js +779 -0
- package/server/conversation-store.d.ts +25 -1
- package/server/conversation-store.js +8 -2
- package/server/conversation.d.ts +61 -2
- package/server/conversation.js +225 -37
- package/server/create-conversation-core.d.ts +28 -0
- package/server/create-conversation-core.js +46 -0
- package/server/handler.d.ts +62 -17
- package/server/handler.js +200 -906
- package/server/mod.d.ts +8 -3
- package/server/mod.js +2 -0
- package/server/session-authority.d.ts +3 -1
- package/server/session-authority.js +23 -0
- package/server/session-calls.d.ts +122 -0
- package/server/session-calls.js +512 -0
- package/server/steer-inbox.d.ts +3 -4
- package/server/steer-inbox.js +6 -5
- package/server/turn-wiring.d.ts +19 -4
- package/server/turn-wiring.js +8 -1
package/server/mod.d.ts
CHANGED
|
@@ -8,21 +8,26 @@
|
|
|
8
8
|
export { theoremHostCallRequestSchema } from '../client/host-transport.ts';
|
|
9
9
|
export type { LiveClientMessage, LiveOpenMessage } from '../client/live-messages.ts';
|
|
10
10
|
export { type StatedTurn, theoremConversationRequestSchema, theoremInvokeRequestSchema, theoremNotifyRequestSchema, theoremReplaySchema, theoremRewindRequestSchema, theoremStartRequestSchema, theoremSteerRequestSchema, theoremStopRequestSchema, theoremTurnRequestSchema, theoremWakeRequestSchema, } from '../client/transport.ts';
|
|
11
|
-
export { answerReply, type ConversationHost, type ConversationUse, conversationView, type IssuedWalkAway, messageReply, type OpenedConversation, openConversation, type PostedMessage, postedMessage, type ReplyLine, type ReplyRunner, type ReplyTurn, readerConversationHost, rewindConversation, startConversation, statelessRefusal, type WalkedAwayCall, waitingCalls, wakeReply, } from './conversation.ts';
|
|
12
|
-
export {
|
|
11
|
+
export { answerReply, type ConversationHost, type ConversationUse, conversationView, type IssuedWalkAway, messageReply, type OpenedConversation, openConversation, type PostedMessage, postedMessage, type ReplyLine, type ReplyRunner, type ReplySaving, type ReplyToSave, type ReplyTurn, readerConversationHost, rewindConversation, type ShapedReply, startConversation, statelessRefusal, type WalkedAwayCall, waitingCalls, wakeReply, } from './conversation.ts';
|
|
12
|
+
export type { AppInbox, ContinueChoice, ConversationCore, FollowUpArgs, Generation, HoldStop, MessageArgs, Reply, ReplyPrepare, ReplyWiring, RunEnd, SaveHooks, WakeArgs, } from './conversation-core.ts';
|
|
13
|
+
export { followedUp } from './conversation-core.ts';
|
|
14
|
+
export { type ConversationSnapshot, type ConversationWriteResult, createMemoryConversationStore, createSealedConversationStore, emptyConversation, type PausedReply, type ReplyEnd, type SealedConversationStoreOptions, type StoredConversation, storedConversationSchema, type TheoremConversationStore, type UncompactedBase, unknownConversation, type WriteContext, } from './conversation-store.ts';
|
|
15
|
+
export { createTheoremConversationCore, type TheoremConversationCoreOptions, } from './create-conversation-core.ts';
|
|
13
16
|
export { createMemoryCredentialStore, type TheoremCredentialStore, type TheoremCredentials, } from './credential-store.ts';
|
|
14
17
|
export { createTheoremDecisionHandler, type DecisionQuestionsBuilder, isQuestionsBuilder, type TheoremDecisionHandlerOptions, } from './decision-handler.ts';
|
|
15
18
|
export { createTheoremEmbeddingHandler, embedBodySchema, embedRequest, type TheoremEmbeddingHandlerOptions, } from './embedding-handler.ts';
|
|
16
|
-
export { createTheoremHandler, createTheoremHostHandler, readBody, type TheoremHandlerOptions, type TheoremHostHandlerOptions, type TheoremRequestContext, theoremSession, theoremSessionId, } from './handler.ts';
|
|
19
|
+
export { createTheoremHandler, createTheoremHostHandler, type MessageIntake, readBody, type TheoremHandlerOptions, type TheoremHostHandlerOptions, type TheoremRequestContext, theoremSession, theoremSessionId, } from './handler.ts';
|
|
17
20
|
export { createTheoremMcpHandler, type TheoremMcpHandlerOptions } from './mcp-handler.ts';
|
|
18
21
|
export { createMemoryNotifier, type TheoremNotifier } from './notifier.ts';
|
|
19
22
|
export { createMemoryPageToolBroker, type PageToolBroker, type PageToolResult, pageToolResultSchema, sessionPageDispatch, } from './page-dispatch.ts';
|
|
20
23
|
export { checkRequest, parseLiveClientMessage, parseLiveOpenMessage } from './request-check.ts';
|
|
21
24
|
export { sessionStateStore } from './session-authority.ts';
|
|
22
25
|
export { createTheoremSessionBoundary, type PreparedLiveOpen, type SessionBoundaryOptions, type TheoremSessionBoundary, } from './session-boundary.ts';
|
|
26
|
+
export type { CoreContext } from './session-calls.ts';
|
|
23
27
|
export type { TheoremSessionCapacity } from './session-limits.ts';
|
|
24
28
|
export { createMemorySessionStore, DEFAULT_OPERATION_RETENTION_MS, type MemorySessionStoreOptions, type PendingToolGate, type SessionSnapshot, type SessionToolOperation, type SessionWriteResult, type TheoremSessionState, type TheoremSessionStore, theoremSessionStateSchema, } from './session-store.ts';
|
|
25
29
|
export { createMemorySteerInbox, type SteerInbox, type SteerUnit, steerStage, steerUnitOf, stopOnRequest, } from './steer-inbox.ts';
|
|
26
30
|
export { tellEveryResult } from './tell-every-result.ts';
|
|
27
31
|
export { sessionToolCoordinator } from './tool-operations.ts';
|
|
28
32
|
export { kernelTurnInput, liveSessionOpen } from './turn-input.ts';
|
|
33
|
+
export type { ReplyRefusal } from './turn-wiring.ts';
|
package/server/mod.js
CHANGED
|
@@ -8,7 +8,9 @@
|
|
|
8
8
|
export { theoremHostCallRequestSchema } from "../client/host-transport.js";
|
|
9
9
|
export { theoremConversationRequestSchema, theoremInvokeRequestSchema, theoremNotifyRequestSchema, theoremReplaySchema, theoremRewindRequestSchema, theoremStartRequestSchema, theoremSteerRequestSchema, theoremStopRequestSchema, theoremTurnRequestSchema, theoremWakeRequestSchema, } from "../client/transport.js";
|
|
10
10
|
export { answerReply, conversationView, messageReply, openConversation, postedMessage, readerConversationHost, rewindConversation, startConversation, statelessRefusal, waitingCalls, wakeReply, } from "./conversation.js";
|
|
11
|
+
export { followedUp } from "./conversation-core.js";
|
|
11
12
|
export { createMemoryConversationStore, createSealedConversationStore, emptyConversation, storedConversationSchema, unknownConversation, } from "./conversation-store.js";
|
|
13
|
+
export { createTheoremConversationCore, } from "./create-conversation-core.js";
|
|
12
14
|
export { createMemoryCredentialStore, } from "./credential-store.js";
|
|
13
15
|
export { createTheoremDecisionHandler, isQuestionsBuilder, } from "./decision-handler.js";
|
|
14
16
|
export { createTheoremEmbeddingHandler, embedBodySchema, embedRequest, } from "./embedding-handler.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type TheoremSessionCapacity } from './session-limits.ts';
|
|
2
|
-
import { type TheoremSessionState, type TheoremSessionStore } from './session-store.ts';
|
|
2
|
+
import { type PerSessionStore, type TheoremSessionState, type TheoremSessionStore } from './session-store.ts';
|
|
3
3
|
export type MutableStore<T> = {
|
|
4
4
|
read(sessionId: string): Promise<T>;
|
|
5
5
|
mutate<R>(sessionId: string, change: (value: T) => R | Promise<R>): Promise<R>;
|
|
@@ -10,3 +10,5 @@ export type SessionStateStore = MutableStore<TheoremSessionState> & {
|
|
|
10
10
|
};
|
|
11
11
|
/** Session state, with expired gates dropped as it loads so they can't be answered. */
|
|
12
12
|
export declare function sessionStateStore(store: TheoremSessionStore, gateTtlMs: number | undefined, operationRetentionMs: number): SessionStateStore;
|
|
13
|
+
/** A store read and changed one session at a time: each change waits for the one before it. */
|
|
14
|
+
export declare function lockedStore<T>(store: PerSessionStore<T>, loaded: (value: T | undefined) => T): MutableStore<T>;
|
|
@@ -82,3 +82,26 @@ export function sessionStateStore(store, gateTtlMs, operationRetentionMs) {
|
|
|
82
82
|
},
|
|
83
83
|
};
|
|
84
84
|
}
|
|
85
|
+
/** A store read and changed one session at a time: each change waits for the one before it. */
|
|
86
|
+
export function lockedStore(store, loaded) {
|
|
87
|
+
const locks = new Map();
|
|
88
|
+
const read = async (sessionId) => loaded(await store.load(sessionId));
|
|
89
|
+
return {
|
|
90
|
+
read,
|
|
91
|
+
mutate(sessionId, change) {
|
|
92
|
+
const next = (locks.get(sessionId) ?? Promise.resolve()).then(async () => {
|
|
93
|
+
const value = await read(sessionId);
|
|
94
|
+
const result = change(value);
|
|
95
|
+
await store.save(sessionId, value);
|
|
96
|
+
return result;
|
|
97
|
+
});
|
|
98
|
+
const settled = next.catch(() => { });
|
|
99
|
+
locks.set(sessionId, settled);
|
|
100
|
+
void settled.then(() => {
|
|
101
|
+
if (locks.get(sessionId) === settled)
|
|
102
|
+
locks.delete(sessionId);
|
|
103
|
+
});
|
|
104
|
+
return next;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A session's tool calls, with no `Request`: each call recorded in the session, its gates saved
|
|
3
|
+
* as they pause, an answered call run on, and a background call announced as it ends. The HTTP
|
|
4
|
+
* handlers and the conversation core both run their calls through here.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { type GateReference, type Profile, type StageHandler, type ToolGate, type TurnEvent, type TurnInput, type TurnWiring } from '@theoremjs/agents';
|
|
9
|
+
import { type ToolBackgroundService, type ToolCredentialSource } from '@theoremjs/agents/kernel';
|
|
10
|
+
import type { TheoremHostCallRequest } from '../client/host-transport.ts';
|
|
11
|
+
import type { TheoremInvokeRequest } from '../client/transport.ts';
|
|
12
|
+
import { type IssuedWalkAway } from './conversation.ts';
|
|
13
|
+
import { type SettledOperation } from './conversation-inbox.ts';
|
|
14
|
+
import type { StoredConversation } from './conversation-store.ts';
|
|
15
|
+
import { type TheoremCredentialStore, type TheoremCredentials } from './credential-store.ts';
|
|
16
|
+
import type { TheoremNotifier } from './notifier.ts';
|
|
17
|
+
import { type PageToolBroker } from './page-dispatch.ts';
|
|
18
|
+
import { type MutableStore, type SessionStateStore } from './session-authority.ts';
|
|
19
|
+
import { type SessionToolOperation, type TheoremSessionState, type TheoremSessionStore } from './session-store.ts';
|
|
20
|
+
import { type AppPrepare } from './turn-wiring.ts';
|
|
21
|
+
/** What a run reads in place of the request that started it. */
|
|
22
|
+
export type CoreContext = {
|
|
23
|
+
sessionId: string;
|
|
24
|
+
/**
|
|
25
|
+
* The app's context for tool handlers (`ctx.host`). A promise is awaited where a run reads it.
|
|
26
|
+
*/
|
|
27
|
+
host?: unknown;
|
|
28
|
+
/** Aborts the run. */
|
|
29
|
+
signal: AbortSignal;
|
|
30
|
+
};
|
|
31
|
+
/** What a session's tool calls read from whoever serves them. */
|
|
32
|
+
export type CallsContext = {
|
|
33
|
+
profile: Profile;
|
|
34
|
+
sessions: SessionStateStore;
|
|
35
|
+
credentials: MutableStore<TheoremCredentials>;
|
|
36
|
+
pageToolBroker: PageToolBroker;
|
|
37
|
+
options: {
|
|
38
|
+
prepare?: AppPrepare;
|
|
39
|
+
onStage?: StageHandler;
|
|
40
|
+
pageToolTimeoutMs?: number;
|
|
41
|
+
notifier?: TheoremNotifier;
|
|
42
|
+
lateResultsDecider?: TurnWiring['lateResultsDecider'];
|
|
43
|
+
/** The decider's wake, with no page: a reply to no message on a run of its own. */
|
|
44
|
+
wake: (at: CoreContext, ref: string) => Promise<void>;
|
|
45
|
+
} & CallHooks;
|
|
46
|
+
};
|
|
47
|
+
/** The app's own steps that a session's tool calls run, each for the session of `at`. */
|
|
48
|
+
export type CallHooks = {
|
|
49
|
+
/** The service for tools with a `background` setting, for the session of `at`. */
|
|
50
|
+
background?: (at: CoreContext) => ToolBackgroundService;
|
|
51
|
+
/** Check current resource access before disclosing a saved tool result. Read-only. */
|
|
52
|
+
authorizeToolReplay?: (at: CoreContext, operation: SettledOperation) => boolean | Promise<boolean>;
|
|
53
|
+
/** The sign-in URL for an OAuth gate. */
|
|
54
|
+
authorizationUrl?: (challenge: Extract<ToolGate, {
|
|
55
|
+
kind: 'auth';
|
|
56
|
+
}>['authChallenge'], at: CoreContext) => string | Promise<string>;
|
|
57
|
+
/** Called with any error a run reports and does not throw. */
|
|
58
|
+
onError?: (err: unknown, at: CoreContext) => void;
|
|
59
|
+
};
|
|
60
|
+
/** The stores and limits a session's calls run on; each is the handler option of the same name. */
|
|
61
|
+
export type SessionOptions = {
|
|
62
|
+
sessionStore?: TheoremSessionStore;
|
|
63
|
+
credentialStore?: TheoremCredentialStore;
|
|
64
|
+
gateTtlMs?: number;
|
|
65
|
+
operationRetentionMs?: number;
|
|
66
|
+
pageToolBroker?: PageToolBroker;
|
|
67
|
+
pageToolTimeoutMs?: number;
|
|
68
|
+
notifyRetryMs?: number;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* The profile, registered, and the session's stores as its calls read them. `name` is the
|
|
72
|
+
* function the app called, for the error a wrong option throws.
|
|
73
|
+
*/
|
|
74
|
+
export declare function sessionParts(name: string, profile: Profile, options: SessionOptions): Pick<CallsContext, 'profile' | 'sessions' | 'credentials' | 'pageToolBroker'>;
|
|
75
|
+
/** What a turn's saved outcome records beside its events. */
|
|
76
|
+
export type OutcomeContext = {
|
|
77
|
+
profileId: string;
|
|
78
|
+
turnInput: TurnInput;
|
|
79
|
+
model?: string;
|
|
80
|
+
promoted?: string[];
|
|
81
|
+
};
|
|
82
|
+
export declare function recorded(sessions: MutableStore<TheoremSessionState>, sessionId: string, events: AsyncIterable<TurnEvent>, context: OutcomeContext, seed?: TurnEvent[]): AsyncGenerator<TurnEvent>;
|
|
83
|
+
/**
|
|
84
|
+
* The session's credentials as a call reads them: one slot when a tool needs it,
|
|
85
|
+
* saved the moment the kernel sets one, so a rotated refresh token is never lost
|
|
86
|
+
* to a closed stream.
|
|
87
|
+
*/
|
|
88
|
+
export declare function sessionCredentials(ctx: CallsContext, sessionId: string): ToolCredentialSource;
|
|
89
|
+
/** Every OAuth gate in the stream, given the host's sign-in URL. */
|
|
90
|
+
export declare function withAuthorizationUrls(ctx: CallsContext, at: CoreContext, events: AsyncIterable<TurnEvent>): AsyncGenerator<TurnEvent>;
|
|
91
|
+
/** {@link stillWaits} for `conversation`'s paused reply, read from the session. Writes nothing. */
|
|
92
|
+
export declare function pausedWaits(ctx: CallsContext, at: CoreContext, conversation: StoredConversation): Promise<{
|
|
93
|
+
until?: number;
|
|
94
|
+
} | undefined>;
|
|
95
|
+
/**
|
|
96
|
+
* Take the calls of a paused reply whose gates have all lapsed out of the session, each settled
|
|
97
|
+
* as `expired`. Undefined, with nothing taken, while one still waits ({@link stillWaits}).
|
|
98
|
+
*/
|
|
99
|
+
export declare function takeLapsed(ctx: CallsContext, at: CoreContext, conversation: StoredConversation): Promise<IssuedWalkAway[] | undefined>;
|
|
100
|
+
/**
|
|
101
|
+
* Take the calls a message or a rewind walks away from out of the session,
|
|
102
|
+
* answered `abandon`: every one of them, or none when one is neither waiting
|
|
103
|
+
* nor settled. Each comes back with the run that settles it.
|
|
104
|
+
*/
|
|
105
|
+
export declare function takeWalkedAway(ctx: CallsContext, at: CoreContext, abandon: readonly GateReference[], conversation: StoredConversation): Promise<IssuedWalkAway[]>;
|
|
106
|
+
/** The host's service for a call in this session, for a request that runs a tool. */
|
|
107
|
+
export declare function backgroundWiring(ctx: CallsContext, at: CoreContext): {
|
|
108
|
+
background?: ToolBackgroundService;
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* `service`, telling the session's open pages when each of its calls ends, and the decider's
|
|
112
|
+
* `ended`, with the wake and the notify it may use.
|
|
113
|
+
*/
|
|
114
|
+
export declare function announced(ctx: CallsContext, at: CoreContext, service: ToolBackgroundService): ToolBackgroundService;
|
|
115
|
+
/** Whether this request may read a settled call's result: the host's `authorizeToolReplay`. */
|
|
116
|
+
export declare function replayAllowed(ctx: CallsContext, at: CoreContext, operation: Extract<SessionToolOperation, {
|
|
117
|
+
status: 'settled';
|
|
118
|
+
}>): Promise<boolean>;
|
|
119
|
+
/** One call of one of a host's tools, run as the page asked it; a gate it pauses on waits in the session. */
|
|
120
|
+
export declare function callEvents(ctx: CallsContext, at: CoreContext, body: TheoremHostCallRequest): AsyncGenerator<TurnEvent>;
|
|
121
|
+
/** The answered call's events: its saved outcome again, or the call run as answered. */
|
|
122
|
+
export declare function settlingOf(ctx: CallsContext, at: CoreContext, body: TheoremInvokeRequest): Promise<() => AsyncIterable<TurnEvent>>;
|