@dudousxd/nestjs-agent-core 0.37.0 → 0.38.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/ag-ui/index.cjs.map +1 -1
- package/dist/ag-ui/index.js.map +1 -1
- package/dist/index.cjs +243 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +142 -1
- package/dist/index.d.ts +142 -1
- package/dist/index.js +242 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -107,6 +107,13 @@ interface SinkWriter {
|
|
|
107
107
|
* — so the transport can surface a typed failure frame instead of leaking it as assistant text.
|
|
108
108
|
*/
|
|
109
109
|
fail(error: StreamError): void | Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* OPTIONAL: write out anything this writer is still holding back, without ending the stream. Only
|
|
112
|
+
* a sink that gathers frames before writing them (the SQL sink coalesces text) has anything to do
|
|
113
|
+
* here; a delegated run's child writer calls it where it would have ended the stream, so nothing
|
|
114
|
+
* of the delegate's is still held back when its parent — possibly on another replica — writes next.
|
|
115
|
+
*/
|
|
116
|
+
flush?(): void | Promise<void>;
|
|
110
117
|
}
|
|
111
118
|
/** A machine-readable stream failure. `code` is a stable slug; `message` is human-facing. */
|
|
112
119
|
interface StreamError {
|
|
@@ -3815,4 +3822,138 @@ interface ConfirmedTool {
|
|
|
3815
3822
|
handler: ToolHandler;
|
|
3816
3823
|
}
|
|
3817
3824
|
|
|
3818
|
-
|
|
3825
|
+
/** One stored row of a run's stream. `frame` is `null` on the terminal row. */
|
|
3826
|
+
interface StreamFrameRow {
|
|
3827
|
+
seq: number;
|
|
3828
|
+
/** One NDJSON line (`{...}\n`) as the writer wrote it; `null` marks the end of the run. */
|
|
3829
|
+
frame: string | null;
|
|
3830
|
+
/** On the terminal row of a FAILED run: the `StreamError` as JSON. `null` otherwise. */
|
|
3831
|
+
error: string | null;
|
|
3832
|
+
}
|
|
3833
|
+
/**
|
|
3834
|
+
* The table a {@link SqlTokenStreamSink} keeps its rows in — the only part of it that speaks SQL, so
|
|
3835
|
+
* a store package implements this and inherits the rest (coalescing, ordering, polling, TTL).
|
|
3836
|
+
* `@dudousxd/nestjs-agent-store-drizzle` and `-store-mikro-orm` ship one each, over
|
|
3837
|
+
* `agent_stream_frame` (`run_id`, `seq`, `frame`, `error`, `created_at`; primary key
|
|
3838
|
+
* `(run_id, seq)`).
|
|
3839
|
+
*/
|
|
3840
|
+
interface StreamFrameTable {
|
|
3841
|
+
/**
|
|
3842
|
+
* Insert the run's NEXT row: `seq = MAX(seq) + 1` for the run, taken in the SAME statement as the
|
|
3843
|
+
* insert, so the `(run_id, seq)` primary key settles two writers that ask at once. Throws on that
|
|
3844
|
+
* collision ({@link isUniqueViolation} says which errors are one) — the sink retries.
|
|
3845
|
+
*/
|
|
3846
|
+
append(runId: string, row: {
|
|
3847
|
+
frame: string | null;
|
|
3848
|
+
error: string | null;
|
|
3849
|
+
createdAt: number;
|
|
3850
|
+
}): Promise<void>;
|
|
3851
|
+
/** The run's rows with `seq > after`, ascending, at most `limit`. */
|
|
3852
|
+
read(runId: string, after: number, limit: number): Promise<StreamFrameRow[]>;
|
|
3853
|
+
/** Does the table hold any row for the run? */
|
|
3854
|
+
has(runId: string): Promise<boolean>;
|
|
3855
|
+
/** Delete every row of these runs. */
|
|
3856
|
+
remove(runIds: readonly string[]): Promise<void>;
|
|
3857
|
+
/** Runs whose LAST row was written before `cutoff` (epoch-ms). */
|
|
3858
|
+
lapsedRuns(cutoff: number): Promise<string[]>;
|
|
3859
|
+
/** Is this error a lost race for a `(run_id, seq)` — and nothing else (a missing table, a dead connection)? */
|
|
3860
|
+
isUniqueViolation(error: unknown): boolean;
|
|
3861
|
+
}
|
|
3862
|
+
interface SqlTokenStreamSinkOptions {
|
|
3863
|
+
/**
|
|
3864
|
+
* Gap (ms) between two reads of a run a subscriber is following — the most a frame waits in the
|
|
3865
|
+
* table before a browser sees it. Default 250.
|
|
3866
|
+
*/
|
|
3867
|
+
pollIntervalMs?: number;
|
|
3868
|
+
/**
|
|
3869
|
+
* Gap (ms) between two reads once the run has written nothing for five seconds (parked on a
|
|
3870
|
+
* person, a slow tool), so an open SSE connection on a quiet run costs one query a second. The
|
|
3871
|
+
* first frame to arrive puts the subscriber back on `pollIntervalMs`. Default 1000; never below
|
|
3872
|
+
* `pollIntervalMs`.
|
|
3873
|
+
*/
|
|
3874
|
+
idlePollIntervalMs?: number;
|
|
3875
|
+
/**
|
|
3876
|
+
* Window (ms) consecutive `text` frames are gathered over and written as ONE row — a model
|
|
3877
|
+
* streams token by token, and a row per token is more writes than a database should take for a
|
|
3878
|
+
* chat answer. Any other frame, `end()`, `fail()` and `flush()` write what is gathered first, so
|
|
3879
|
+
* order is kept. Default 50. `0` writes every frame as its own row.
|
|
3880
|
+
*/
|
|
3881
|
+
flushMs?: number;
|
|
3882
|
+
/**
|
|
3883
|
+
* TTL (seconds) of a run's rows, counted from its LAST write — a long run stays, a run that
|
|
3884
|
+
* crashed without ending still lapses. {@link SqlTokenStreamSink.purgeExpired} deletes them.
|
|
3885
|
+
* Default 3600 (1h). `0` keeps rows until `close()`.
|
|
3886
|
+
*/
|
|
3887
|
+
ttlSeconds?: number;
|
|
3888
|
+
/**
|
|
3889
|
+
* Purge lapsed runs as a side effect of ending a run, at most once a minute, so the table stays
|
|
3890
|
+
* bounded in an app that never schedules {@link SqlTokenStreamSink.purgeExpired}. Default `true`.
|
|
3891
|
+
*/
|
|
3892
|
+
autoPurge?: boolean;
|
|
3893
|
+
}
|
|
3894
|
+
/**
|
|
3895
|
+
* A multi-replica {@link TokenStreamSink} over the app's SQL database — for a deployment with
|
|
3896
|
+
* several replicas and NO Redis. The replica running the turn appends each frame as a row; any
|
|
3897
|
+
* replica serves the run's SSE by reading the rows past its cursor, in order, until the terminal
|
|
3898
|
+
* row. A late subscriber replays from the first row, as with the in-process and Redis sinks, and a
|
|
3899
|
+
* run that `fail()`ed throws its {@link AgentStreamError} after the replay.
|
|
3900
|
+
*
|
|
3901
|
+
* Two things differ from the Redis sink, both the price of having no broker:
|
|
3902
|
+
*
|
|
3903
|
+
* - **Delivery is polled** — a frame waits up to `pollIntervalMs` before a browser sees it.
|
|
3904
|
+
* - **Text is coalesced on the way in.** Consecutive `text` frames written within `flushMs` are
|
|
3905
|
+
* stored as ONE `text` frame. The stored rows ARE the run's stream: every subscriber, on every
|
|
3906
|
+
* replica, reads the same rows in the same order, so the SSE ids `chat/:runId/stream` numbers
|
|
3907
|
+
* them with — and an `?after=` cursor — stay exact across replicas.
|
|
3908
|
+
*
|
|
3909
|
+
* Sequence numbers are per run, start at 1 and have no gaps (see {@link StreamFrameTable.append}).
|
|
3910
|
+
* Text a writer is still gathering lives in the process for at most `flushMs`; a process that dies
|
|
3911
|
+
* in that window loses it from the LIVE stream (the persisted message is unaffected).
|
|
3912
|
+
*
|
|
3913
|
+
* Framework- and dialect-free: the SQL is the {@link StreamFrameTable}'s. Use
|
|
3914
|
+
* `DrizzleTokenStreamSink` / `MikroOrmTokenStreamSink` from the store packages.
|
|
3915
|
+
*/
|
|
3916
|
+
declare class SqlTokenStreamSink implements TokenStreamSink {
|
|
3917
|
+
private readonly table;
|
|
3918
|
+
private readonly pollIntervalMs;
|
|
3919
|
+
private readonly idlePollIntervalMs;
|
|
3920
|
+
private readonly flushMs;
|
|
3921
|
+
private readonly ttlSeconds;
|
|
3922
|
+
private readonly autoPurge;
|
|
3923
|
+
private readonly writes;
|
|
3924
|
+
private lastAutoPurge;
|
|
3925
|
+
constructor(table: StreamFrameTable, options?: SqlTokenStreamSinkOptions);
|
|
3926
|
+
private runWrites;
|
|
3927
|
+
/** Forget a run's write state once nothing is gathered, queued or left to report. */
|
|
3928
|
+
private release;
|
|
3929
|
+
/** Queue `work` behind every write already queued for the run, so rows land in call order. */
|
|
3930
|
+
private enqueue;
|
|
3931
|
+
/** Queue the gathered text (if any) as one `text` row. */
|
|
3932
|
+
private flushText;
|
|
3933
|
+
/** Raise, once, a gathered write that failed with nobody awaiting it. */
|
|
3934
|
+
private raiseFailure;
|
|
3935
|
+
/** Append one row — a frame, or the terminal row (`frame: null`, with `error` on a failure). */
|
|
3936
|
+
private append;
|
|
3937
|
+
/** Write out what is gathered, then a terminal row; raise a failure nobody saw. */
|
|
3938
|
+
private terminate;
|
|
3939
|
+
/**
|
|
3940
|
+
* The run's writer. Writers opened for the same run in one process share what they are gathering,
|
|
3941
|
+
* so a delegated run's text (forwarded through a child writer) and its parent's next frame keep
|
|
3942
|
+
* the order they were written in.
|
|
3943
|
+
*/
|
|
3944
|
+
open(runId: string): SinkWriter;
|
|
3945
|
+
subscribe(runId: string): AsyncIterable<Uint8Array>;
|
|
3946
|
+
/** Does the table hold anything for the run — a frame, or its end? */
|
|
3947
|
+
has(runId: string): Promise<boolean>;
|
|
3948
|
+
close(runId: string): Promise<void>;
|
|
3949
|
+
/**
|
|
3950
|
+
* Delete every run whose last write is older than `ttlSeconds` at `now` (epoch-ms) — ended runs
|
|
3951
|
+
* past their replay window and runs that crashed without ending alike. Returns how many runs went.
|
|
3952
|
+
* Safe from any replica, and from several at once. A no-op under `ttlSeconds: 0`.
|
|
3953
|
+
*/
|
|
3954
|
+
purgeExpired(now?: number): Promise<number>;
|
|
3955
|
+
/** {@link purgeExpired}, unawaited and at most once a minute. Its failure is not the run's. */
|
|
3956
|
+
private purgeInBackground;
|
|
3957
|
+
}
|
|
3958
|
+
|
|
3959
|
+
export { AGENT_ACTOR_DIRECTORY, AGENT_ACTOR_RESOLVER, AGENT_APPROVAL_PORT, AGENT_ATTACHMENT_STAGING, AGENT_CONFIRM_TOKEN_STORE, AGENT_DEPS_FACTORY, AGENT_DIAGNOSTIC_EVENTS, AGENT_DURABLE_RUNNER, AGENT_EMBEDDING_PROVIDER, AGENT_GOVERNANCE_QUERIES, AGENT_MEMORY, AGENT_MODEL, AGENT_MODEL_CATALOG, AGENT_OPTIONS, AGENT_PRICING_STORE, AGENT_PROMPT_CONTRIBUTORS, AGENT_QUOTA_PROVIDER, AGENT_QUOTA_STORE, AGENT_REGISTRY, AGENT_RETRIEVER, AGENT_ROLES_POLICY, AGENT_RUNNER, AGENT_SINK, AGENT_SKILLS, AGENT_SKILL_SOURCES, AGENT_SPAN_EVENTS, AGENT_STORE, AGENT_TOOL_REGISTRY, APPROVAL_EXPIRED_REASON, Actor, type ActorDirectory, type ActorResolver, type ActorSpendRow, type AgentApprovalPort, AgentDefinition, type AgentDelegated, AgentDelegation, type AgentDiagnosticEvent, type AgentDiagnosticKey, type AgentFollowUpsSpan, type AgentGovernanceQueries, AgentIntake, type AgentLlmTurnSpan, type AgentLoopDeps, type AgentLoopHooks, type AgentLoopResult, type AgentMemoryResolved, type AgentMemoryWritten, type AgentMessageEvent, type AgentPricingStore, type AgentQuotaExceeded, AgentRegistry, type AgentRetrievalSpan, type AgentRetrieved, type AgentRunFailed, type AgentRunFinished, AgentRunInput, type AgentRunStartOptions, type AgentRunStarted, type AgentRunner, type AgentSkillsResolved, type AgentSpanEvent, AgentStore, AgentStreamError, AgentStreamEvent, type AgentStructuredOutputSpan, type AgentToolCallEvent, type AgentToolExecutionSpan, type AgentToolRetry, AgentUiComponent, AiToolCtx, AppendMessageInput, type ApprovalDecisionRef, type ApprovalPolicy, type ApprovalRequirement, type ApprovalThreadRef, type ApprovalToolRef, type ApprovalWhere, type AttachmentRef, type AttachmentStagingDescription, type AttachmentStagingStore, type BufferedModelTurnResult, type BuildMemoryBlockInput, CONFIRM_JSON_SCHEMA_PROPERTIES, ChatQueueStore, ClosedRolesPolicy, type ConfirmFields, type ConfirmTokenClaim, ConfirmTokenError, type ConfirmTokenStore, type ConfirmTokenSubject, type ConfirmedTool, type ConfirmedToolDone, type ConfirmedToolMessages, type ConfirmedToolOptions, type ConfirmedToolOutcome, type ConfirmedToolPreview, type ConfirmedToolResult, type ConfirmedToolSteps, type CostUsage, CreateThreadInput, type CurrentModelPrice, DEFAULT_CONFIRM_TTL_MS, DEFAULT_HISTORY_SUMMARY_INSTRUCTION, DEFAULT_MAX_FACT_CHARS, DEFAULT_MAX_MEMORIES, DEFAULT_MAX_SKILLS, DEFAULT_REFUSAL_REASON, DEFAULT_STRUCTURED_OUTPUT_INSTRUCTION, Decision, DefaultApprovalPolicy, DefaultRolesPolicy, type DetachedDelegationOutcome, type DetachedDelegationReceipt, DetachedDelivery, type DetailThreadRef, ElicitationRequest, type EmbeddingProvider, type EmitUi, type EmptyRoles, EnqueueMessageInput, type ForgetMemoryInput, type FrameBuffer, GLOBAL_SCOPE, type GovernanceMessageRow, type GovernancePage, type GovernancePageQuery, type GovernancePendingApprovalRow, type GovernanceRange, type GovernanceRunDetail, type GovernanceRunRow, type GovernanceThreadDetail, type GovernanceThreadDetailQuery, type GovernanceThreadRow, type GovernanceToolCallRow, type GovernanceUsageInput, type GovernanceUsageRow, HistoryPolicy, HumanReply, InMemoryAgentStore, InMemoryConfirmTokenStore, type IncrementalGate, InputProcessor, type ListMemoriesInput, type ListSkillsInput, type ListStagedAttachmentsInput, LlmStepEnvelope, type LoadSkillInput, type MemoryAuthor, type MemoryConfig, type MemoryDigest, type MemoryDigestEntry, type MemoryFact, type MemoryForgetRequest, type MemoryOrigin, type MemoryProvider, type MemoryRecord, type MemoryVerdict, type MemoryWriteOutcome, type MemoryWriteRequest, MessageAttachment, MessageFeedback, MessageUsage, ModelAnswer, type ModelCatalog, type ModelCatalogEntry, type ModelCatalogLock, type ModelCatalogProviderGroup, type ModelCatalogQuery, type ModelCatalogView, ModelMessage, type ModelPrice, type ModelPriceInput, type ModelProvider, type ModelSpendRow, type ModelTurnArgs, type ModelTurnResult, type ObservedTurnFrames, type OfferMemoriesInput, type OutputGateMode, type OutputGateResult, OutputProcessor, type OverriddenMemory, PageContext, type Passage, type PendingApprovalRow, ProcessedPrompt, ProcessorContext, PromptBuilder, PromptContributor, QueuePause, QueuedMessage, QueuedMessagePatch, type QuotaBlock, QuotaExceededError, type QuotaPeriod, type QuotaProvider, type QuotaQuery, type QuotaReport, QuotaState, type QuotaStore, type QuotaWarning, type QuotaWindow, REMEMBER_TOOL_DESCRIPTION, REMEMBER_TOOL_NAME, REQUESTER_APPROVER, RUN_FAILED_MESSAGE, RUN_NOT_ACTIVE_CODE, RUN_NOT_ACTIVE_MESSAGE, RUN_NO_LONGER_RUNNING, type RecentRunRow, RecordRunStartInput, RecordToolCallInput, RecordUsageInput, type RememberToolInput, type RerankOptions, type Reranker, type ResolveAttachmentInput, type ResolveMemoryDigestInput, type ResolvedDelegation, type RetrieveOptions, type Retriever, type RolesPolicy, type RolesPolicyOptions, type RunAgentBreakdownRow, RunCancelledError, type RunErrorBreakdownRow, type RunMetrics, type RunToolCallRow, type RunTrendPoint, type RunWhere, SCHEMA_EXTENSION, SKILL_TOOL_DESCRIPTION, SKILL_TOOL_NAME, type SchemaExtension, type ScopeContext, type ScopeResolver, type SearchMemoriesInput, type SettleDeadRunInput, type SettledTask, type SinkWriter, type Skill, type SkillAuthor, type SkillCatalogEntry, type SkillContext, type SkillLoadOutcome, type SkillOffer, type SkillProvider, type SkillSummary, type SkillToolInput, type SkillWriteRequest, type SkillWriteVerdict, type SkillsConfig, SqlTokenStreamSink, type SqlTokenStreamSinkOptions, type StageAttachmentInput, type StagedAttachment, type StoreMemoryInput, StoredMessage, type StreamError, type StreamFrameRow, type StreamFrameTable, type StructuredOutcome, StructuredOutputError, THREAD_DETAIL_CONTENT_CHARS, type ThreadActivityRow, ThreadDetail, type ThreadMessageRow, type ThreadMeta, type ThreadSpendRow, ThreadSummary, type ThreadUsageRollup, type ThreadWhere, type TokenStreamSink, type ToolCallActivityRow, ToolCallApproval, type ToolCallApprovalColumns, ToolCallApprovalState, ToolCallOutcome, ToolCallRequest, ToolCallStatus, type ToolCallWhere, ToolDefinition, ToolDescribeScope, ToolDisabledError, ToolForbiddenError, ToolHandler, ToolInputInvalidError, ToolKind, type ToolKindDeps, ToolNotFoundError, ToolRegistry, ToolResult, ToolSpec, type ToolStatRow, ToolStepEnvelope, ToolTransientRetrySetting, type TurnFrameSummary, type UiCollector, UpdateThreadInput, UpdateToolCallInput, type UsageTrendPoint, type WindowHistoryOptions, type WithMemoryToolInput, type WriteMemoryInput, actorScope, agentDiagnosticKey, agentFailureCode, bucketByActor, bucketByModel, bucketByThread, bucketUsageTrend, buildMemoryBlock, buildSkillsBlock, canActorUseTool, canonicalJson, closeEmptyRoles, compositeSkillProvider, confirmTokenExpiry, createFrameBuffer, createIncrementalGate, createNoopEmitUi, createUiCollector, dayBoundsUtc, defaultCanDecide, defaultScopeResolver, defineConfirmedTool, detachedDelivered, detachedStarted, detachedUnsettled, estimateCost, estimateMessageTokens, exhaustedWindow, exposeStreamErrorDetails, extractJson, filterToolsByAllowList, filterToolsByCanUse, filterToolsByEnabled, filterToolsByRole, findCatalogModel, gateFollowUps, gateTail, hashConfirmToken, isControlFlowSignal, isReplayIntegrityError, isToolEnabled, loadSkill, mayDecideApproval, memoryForgetVerdict, memoryWriteVerdict, mergeUi, normalizeDelegation, observeTurnFrames, offerMemories, offerSkills, publishAgentDelegated, publishAgentMemoryResolved, publishAgentMemoryWritten, publishAgentMessage, publishAgentQuotaExceeded, publishAgentRetrieved, publishAgentRunFailed, publishAgentRunFinished, publishAgentRunStarted, publishAgentSkillsResolved, publishAgentToolCall, publishAgentToolRetry, quotaPeriodRange, quotaUsedRatio, quotaWarning, releaseGatedFrames, rememberInputSchema, rememberToolDefinition, repairInstruction, resolveGateLookback, resolveMemoryDigest, resolveOutputGateMode, resolveSkillCatalog, rollupThreadUsage, runAgentLoop, runInputProcessors, runOutputProcessors, schemaExtensionOf, seedModelPrices, settleAll, settleDeadRun, settleUnsettledDelegation, signConfirmToken, skillInputSchema, skillToolDefinition, skillWriteVerdict, stampToolKinds, staticModelCatalog, staticSkillProvider, streamFailure, summarizeWithModel, tenantScope, toolCallApprovalFromRow, toolCallContext, traceLlmTurn, traceToolExecution, truncateDetailContent, unwrapToolStepOutput, validateStructured, verifyConfirmToken, windowHistory, withAskTool, withConfirmFields, withMemoryTool, withSelectedModel, withSkillTool, withToolTimeout, withTurnFrames, wrapToolStepOutput, writeMemory };
|
package/dist/index.d.ts
CHANGED
|
@@ -107,6 +107,13 @@ interface SinkWriter {
|
|
|
107
107
|
* — so the transport can surface a typed failure frame instead of leaking it as assistant text.
|
|
108
108
|
*/
|
|
109
109
|
fail(error: StreamError): void | Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* OPTIONAL: write out anything this writer is still holding back, without ending the stream. Only
|
|
112
|
+
* a sink that gathers frames before writing them (the SQL sink coalesces text) has anything to do
|
|
113
|
+
* here; a delegated run's child writer calls it where it would have ended the stream, so nothing
|
|
114
|
+
* of the delegate's is still held back when its parent — possibly on another replica — writes next.
|
|
115
|
+
*/
|
|
116
|
+
flush?(): void | Promise<void>;
|
|
110
117
|
}
|
|
111
118
|
/** A machine-readable stream failure. `code` is a stable slug; `message` is human-facing. */
|
|
112
119
|
interface StreamError {
|
|
@@ -3815,4 +3822,138 @@ interface ConfirmedTool {
|
|
|
3815
3822
|
handler: ToolHandler;
|
|
3816
3823
|
}
|
|
3817
3824
|
|
|
3818
|
-
|
|
3825
|
+
/** One stored row of a run's stream. `frame` is `null` on the terminal row. */
|
|
3826
|
+
interface StreamFrameRow {
|
|
3827
|
+
seq: number;
|
|
3828
|
+
/** One NDJSON line (`{...}\n`) as the writer wrote it; `null` marks the end of the run. */
|
|
3829
|
+
frame: string | null;
|
|
3830
|
+
/** On the terminal row of a FAILED run: the `StreamError` as JSON. `null` otherwise. */
|
|
3831
|
+
error: string | null;
|
|
3832
|
+
}
|
|
3833
|
+
/**
|
|
3834
|
+
* The table a {@link SqlTokenStreamSink} keeps its rows in — the only part of it that speaks SQL, so
|
|
3835
|
+
* a store package implements this and inherits the rest (coalescing, ordering, polling, TTL).
|
|
3836
|
+
* `@dudousxd/nestjs-agent-store-drizzle` and `-store-mikro-orm` ship one each, over
|
|
3837
|
+
* `agent_stream_frame` (`run_id`, `seq`, `frame`, `error`, `created_at`; primary key
|
|
3838
|
+
* `(run_id, seq)`).
|
|
3839
|
+
*/
|
|
3840
|
+
interface StreamFrameTable {
|
|
3841
|
+
/**
|
|
3842
|
+
* Insert the run's NEXT row: `seq = MAX(seq) + 1` for the run, taken in the SAME statement as the
|
|
3843
|
+
* insert, so the `(run_id, seq)` primary key settles two writers that ask at once. Throws on that
|
|
3844
|
+
* collision ({@link isUniqueViolation} says which errors are one) — the sink retries.
|
|
3845
|
+
*/
|
|
3846
|
+
append(runId: string, row: {
|
|
3847
|
+
frame: string | null;
|
|
3848
|
+
error: string | null;
|
|
3849
|
+
createdAt: number;
|
|
3850
|
+
}): Promise<void>;
|
|
3851
|
+
/** The run's rows with `seq > after`, ascending, at most `limit`. */
|
|
3852
|
+
read(runId: string, after: number, limit: number): Promise<StreamFrameRow[]>;
|
|
3853
|
+
/** Does the table hold any row for the run? */
|
|
3854
|
+
has(runId: string): Promise<boolean>;
|
|
3855
|
+
/** Delete every row of these runs. */
|
|
3856
|
+
remove(runIds: readonly string[]): Promise<void>;
|
|
3857
|
+
/** Runs whose LAST row was written before `cutoff` (epoch-ms). */
|
|
3858
|
+
lapsedRuns(cutoff: number): Promise<string[]>;
|
|
3859
|
+
/** Is this error a lost race for a `(run_id, seq)` — and nothing else (a missing table, a dead connection)? */
|
|
3860
|
+
isUniqueViolation(error: unknown): boolean;
|
|
3861
|
+
}
|
|
3862
|
+
interface SqlTokenStreamSinkOptions {
|
|
3863
|
+
/**
|
|
3864
|
+
* Gap (ms) between two reads of a run a subscriber is following — the most a frame waits in the
|
|
3865
|
+
* table before a browser sees it. Default 250.
|
|
3866
|
+
*/
|
|
3867
|
+
pollIntervalMs?: number;
|
|
3868
|
+
/**
|
|
3869
|
+
* Gap (ms) between two reads once the run has written nothing for five seconds (parked on a
|
|
3870
|
+
* person, a slow tool), so an open SSE connection on a quiet run costs one query a second. The
|
|
3871
|
+
* first frame to arrive puts the subscriber back on `pollIntervalMs`. Default 1000; never below
|
|
3872
|
+
* `pollIntervalMs`.
|
|
3873
|
+
*/
|
|
3874
|
+
idlePollIntervalMs?: number;
|
|
3875
|
+
/**
|
|
3876
|
+
* Window (ms) consecutive `text` frames are gathered over and written as ONE row — a model
|
|
3877
|
+
* streams token by token, and a row per token is more writes than a database should take for a
|
|
3878
|
+
* chat answer. Any other frame, `end()`, `fail()` and `flush()` write what is gathered first, so
|
|
3879
|
+
* order is kept. Default 50. `0` writes every frame as its own row.
|
|
3880
|
+
*/
|
|
3881
|
+
flushMs?: number;
|
|
3882
|
+
/**
|
|
3883
|
+
* TTL (seconds) of a run's rows, counted from its LAST write — a long run stays, a run that
|
|
3884
|
+
* crashed without ending still lapses. {@link SqlTokenStreamSink.purgeExpired} deletes them.
|
|
3885
|
+
* Default 3600 (1h). `0` keeps rows until `close()`.
|
|
3886
|
+
*/
|
|
3887
|
+
ttlSeconds?: number;
|
|
3888
|
+
/**
|
|
3889
|
+
* Purge lapsed runs as a side effect of ending a run, at most once a minute, so the table stays
|
|
3890
|
+
* bounded in an app that never schedules {@link SqlTokenStreamSink.purgeExpired}. Default `true`.
|
|
3891
|
+
*/
|
|
3892
|
+
autoPurge?: boolean;
|
|
3893
|
+
}
|
|
3894
|
+
/**
|
|
3895
|
+
* A multi-replica {@link TokenStreamSink} over the app's SQL database — for a deployment with
|
|
3896
|
+
* several replicas and NO Redis. The replica running the turn appends each frame as a row; any
|
|
3897
|
+
* replica serves the run's SSE by reading the rows past its cursor, in order, until the terminal
|
|
3898
|
+
* row. A late subscriber replays from the first row, as with the in-process and Redis sinks, and a
|
|
3899
|
+
* run that `fail()`ed throws its {@link AgentStreamError} after the replay.
|
|
3900
|
+
*
|
|
3901
|
+
* Two things differ from the Redis sink, both the price of having no broker:
|
|
3902
|
+
*
|
|
3903
|
+
* - **Delivery is polled** — a frame waits up to `pollIntervalMs` before a browser sees it.
|
|
3904
|
+
* - **Text is coalesced on the way in.** Consecutive `text` frames written within `flushMs` are
|
|
3905
|
+
* stored as ONE `text` frame. The stored rows ARE the run's stream: every subscriber, on every
|
|
3906
|
+
* replica, reads the same rows in the same order, so the SSE ids `chat/:runId/stream` numbers
|
|
3907
|
+
* them with — and an `?after=` cursor — stay exact across replicas.
|
|
3908
|
+
*
|
|
3909
|
+
* Sequence numbers are per run, start at 1 and have no gaps (see {@link StreamFrameTable.append}).
|
|
3910
|
+
* Text a writer is still gathering lives in the process for at most `flushMs`; a process that dies
|
|
3911
|
+
* in that window loses it from the LIVE stream (the persisted message is unaffected).
|
|
3912
|
+
*
|
|
3913
|
+
* Framework- and dialect-free: the SQL is the {@link StreamFrameTable}'s. Use
|
|
3914
|
+
* `DrizzleTokenStreamSink` / `MikroOrmTokenStreamSink` from the store packages.
|
|
3915
|
+
*/
|
|
3916
|
+
declare class SqlTokenStreamSink implements TokenStreamSink {
|
|
3917
|
+
private readonly table;
|
|
3918
|
+
private readonly pollIntervalMs;
|
|
3919
|
+
private readonly idlePollIntervalMs;
|
|
3920
|
+
private readonly flushMs;
|
|
3921
|
+
private readonly ttlSeconds;
|
|
3922
|
+
private readonly autoPurge;
|
|
3923
|
+
private readonly writes;
|
|
3924
|
+
private lastAutoPurge;
|
|
3925
|
+
constructor(table: StreamFrameTable, options?: SqlTokenStreamSinkOptions);
|
|
3926
|
+
private runWrites;
|
|
3927
|
+
/** Forget a run's write state once nothing is gathered, queued or left to report. */
|
|
3928
|
+
private release;
|
|
3929
|
+
/** Queue `work` behind every write already queued for the run, so rows land in call order. */
|
|
3930
|
+
private enqueue;
|
|
3931
|
+
/** Queue the gathered text (if any) as one `text` row. */
|
|
3932
|
+
private flushText;
|
|
3933
|
+
/** Raise, once, a gathered write that failed with nobody awaiting it. */
|
|
3934
|
+
private raiseFailure;
|
|
3935
|
+
/** Append one row — a frame, or the terminal row (`frame: null`, with `error` on a failure). */
|
|
3936
|
+
private append;
|
|
3937
|
+
/** Write out what is gathered, then a terminal row; raise a failure nobody saw. */
|
|
3938
|
+
private terminate;
|
|
3939
|
+
/**
|
|
3940
|
+
* The run's writer. Writers opened for the same run in one process share what they are gathering,
|
|
3941
|
+
* so a delegated run's text (forwarded through a child writer) and its parent's next frame keep
|
|
3942
|
+
* the order they were written in.
|
|
3943
|
+
*/
|
|
3944
|
+
open(runId: string): SinkWriter;
|
|
3945
|
+
subscribe(runId: string): AsyncIterable<Uint8Array>;
|
|
3946
|
+
/** Does the table hold anything for the run — a frame, or its end? */
|
|
3947
|
+
has(runId: string): Promise<boolean>;
|
|
3948
|
+
close(runId: string): Promise<void>;
|
|
3949
|
+
/**
|
|
3950
|
+
* Delete every run whose last write is older than `ttlSeconds` at `now` (epoch-ms) — ended runs
|
|
3951
|
+
* past their replay window and runs that crashed without ending alike. Returns how many runs went.
|
|
3952
|
+
* Safe from any replica, and from several at once. A no-op under `ttlSeconds: 0`.
|
|
3953
|
+
*/
|
|
3954
|
+
purgeExpired(now?: number): Promise<number>;
|
|
3955
|
+
/** {@link purgeExpired}, unawaited and at most once a minute. Its failure is not the run's. */
|
|
3956
|
+
private purgeInBackground;
|
|
3957
|
+
}
|
|
3958
|
+
|
|
3959
|
+
export { AGENT_ACTOR_DIRECTORY, AGENT_ACTOR_RESOLVER, AGENT_APPROVAL_PORT, AGENT_ATTACHMENT_STAGING, AGENT_CONFIRM_TOKEN_STORE, AGENT_DEPS_FACTORY, AGENT_DIAGNOSTIC_EVENTS, AGENT_DURABLE_RUNNER, AGENT_EMBEDDING_PROVIDER, AGENT_GOVERNANCE_QUERIES, AGENT_MEMORY, AGENT_MODEL, AGENT_MODEL_CATALOG, AGENT_OPTIONS, AGENT_PRICING_STORE, AGENT_PROMPT_CONTRIBUTORS, AGENT_QUOTA_PROVIDER, AGENT_QUOTA_STORE, AGENT_REGISTRY, AGENT_RETRIEVER, AGENT_ROLES_POLICY, AGENT_RUNNER, AGENT_SINK, AGENT_SKILLS, AGENT_SKILL_SOURCES, AGENT_SPAN_EVENTS, AGENT_STORE, AGENT_TOOL_REGISTRY, APPROVAL_EXPIRED_REASON, Actor, type ActorDirectory, type ActorResolver, type ActorSpendRow, type AgentApprovalPort, AgentDefinition, type AgentDelegated, AgentDelegation, type AgentDiagnosticEvent, type AgentDiagnosticKey, type AgentFollowUpsSpan, type AgentGovernanceQueries, AgentIntake, type AgentLlmTurnSpan, type AgentLoopDeps, type AgentLoopHooks, type AgentLoopResult, type AgentMemoryResolved, type AgentMemoryWritten, type AgentMessageEvent, type AgentPricingStore, type AgentQuotaExceeded, AgentRegistry, type AgentRetrievalSpan, type AgentRetrieved, type AgentRunFailed, type AgentRunFinished, AgentRunInput, type AgentRunStartOptions, type AgentRunStarted, type AgentRunner, type AgentSkillsResolved, type AgentSpanEvent, AgentStore, AgentStreamError, AgentStreamEvent, type AgentStructuredOutputSpan, type AgentToolCallEvent, type AgentToolExecutionSpan, type AgentToolRetry, AgentUiComponent, AiToolCtx, AppendMessageInput, type ApprovalDecisionRef, type ApprovalPolicy, type ApprovalRequirement, type ApprovalThreadRef, type ApprovalToolRef, type ApprovalWhere, type AttachmentRef, type AttachmentStagingDescription, type AttachmentStagingStore, type BufferedModelTurnResult, type BuildMemoryBlockInput, CONFIRM_JSON_SCHEMA_PROPERTIES, ChatQueueStore, ClosedRolesPolicy, type ConfirmFields, type ConfirmTokenClaim, ConfirmTokenError, type ConfirmTokenStore, type ConfirmTokenSubject, type ConfirmedTool, type ConfirmedToolDone, type ConfirmedToolMessages, type ConfirmedToolOptions, type ConfirmedToolOutcome, type ConfirmedToolPreview, type ConfirmedToolResult, type ConfirmedToolSteps, type CostUsage, CreateThreadInput, type CurrentModelPrice, DEFAULT_CONFIRM_TTL_MS, DEFAULT_HISTORY_SUMMARY_INSTRUCTION, DEFAULT_MAX_FACT_CHARS, DEFAULT_MAX_MEMORIES, DEFAULT_MAX_SKILLS, DEFAULT_REFUSAL_REASON, DEFAULT_STRUCTURED_OUTPUT_INSTRUCTION, Decision, DefaultApprovalPolicy, DefaultRolesPolicy, type DetachedDelegationOutcome, type DetachedDelegationReceipt, DetachedDelivery, type DetailThreadRef, ElicitationRequest, type EmbeddingProvider, type EmitUi, type EmptyRoles, EnqueueMessageInput, type ForgetMemoryInput, type FrameBuffer, GLOBAL_SCOPE, type GovernanceMessageRow, type GovernancePage, type GovernancePageQuery, type GovernancePendingApprovalRow, type GovernanceRange, type GovernanceRunDetail, type GovernanceRunRow, type GovernanceThreadDetail, type GovernanceThreadDetailQuery, type GovernanceThreadRow, type GovernanceToolCallRow, type GovernanceUsageInput, type GovernanceUsageRow, HistoryPolicy, HumanReply, InMemoryAgentStore, InMemoryConfirmTokenStore, type IncrementalGate, InputProcessor, type ListMemoriesInput, type ListSkillsInput, type ListStagedAttachmentsInput, LlmStepEnvelope, type LoadSkillInput, type MemoryAuthor, type MemoryConfig, type MemoryDigest, type MemoryDigestEntry, type MemoryFact, type MemoryForgetRequest, type MemoryOrigin, type MemoryProvider, type MemoryRecord, type MemoryVerdict, type MemoryWriteOutcome, type MemoryWriteRequest, MessageAttachment, MessageFeedback, MessageUsage, ModelAnswer, type ModelCatalog, type ModelCatalogEntry, type ModelCatalogLock, type ModelCatalogProviderGroup, type ModelCatalogQuery, type ModelCatalogView, ModelMessage, type ModelPrice, type ModelPriceInput, type ModelProvider, type ModelSpendRow, type ModelTurnArgs, type ModelTurnResult, type ObservedTurnFrames, type OfferMemoriesInput, type OutputGateMode, type OutputGateResult, OutputProcessor, type OverriddenMemory, PageContext, type Passage, type PendingApprovalRow, ProcessedPrompt, ProcessorContext, PromptBuilder, PromptContributor, QueuePause, QueuedMessage, QueuedMessagePatch, type QuotaBlock, QuotaExceededError, type QuotaPeriod, type QuotaProvider, type QuotaQuery, type QuotaReport, QuotaState, type QuotaStore, type QuotaWarning, type QuotaWindow, REMEMBER_TOOL_DESCRIPTION, REMEMBER_TOOL_NAME, REQUESTER_APPROVER, RUN_FAILED_MESSAGE, RUN_NOT_ACTIVE_CODE, RUN_NOT_ACTIVE_MESSAGE, RUN_NO_LONGER_RUNNING, type RecentRunRow, RecordRunStartInput, RecordToolCallInput, RecordUsageInput, type RememberToolInput, type RerankOptions, type Reranker, type ResolveAttachmentInput, type ResolveMemoryDigestInput, type ResolvedDelegation, type RetrieveOptions, type Retriever, type RolesPolicy, type RolesPolicyOptions, type RunAgentBreakdownRow, RunCancelledError, type RunErrorBreakdownRow, type RunMetrics, type RunToolCallRow, type RunTrendPoint, type RunWhere, SCHEMA_EXTENSION, SKILL_TOOL_DESCRIPTION, SKILL_TOOL_NAME, type SchemaExtension, type ScopeContext, type ScopeResolver, type SearchMemoriesInput, type SettleDeadRunInput, type SettledTask, type SinkWriter, type Skill, type SkillAuthor, type SkillCatalogEntry, type SkillContext, type SkillLoadOutcome, type SkillOffer, type SkillProvider, type SkillSummary, type SkillToolInput, type SkillWriteRequest, type SkillWriteVerdict, type SkillsConfig, SqlTokenStreamSink, type SqlTokenStreamSinkOptions, type StageAttachmentInput, type StagedAttachment, type StoreMemoryInput, StoredMessage, type StreamError, type StreamFrameRow, type StreamFrameTable, type StructuredOutcome, StructuredOutputError, THREAD_DETAIL_CONTENT_CHARS, type ThreadActivityRow, ThreadDetail, type ThreadMessageRow, type ThreadMeta, type ThreadSpendRow, ThreadSummary, type ThreadUsageRollup, type ThreadWhere, type TokenStreamSink, type ToolCallActivityRow, ToolCallApproval, type ToolCallApprovalColumns, ToolCallApprovalState, ToolCallOutcome, ToolCallRequest, ToolCallStatus, type ToolCallWhere, ToolDefinition, ToolDescribeScope, ToolDisabledError, ToolForbiddenError, ToolHandler, ToolInputInvalidError, ToolKind, type ToolKindDeps, ToolNotFoundError, ToolRegistry, ToolResult, ToolSpec, type ToolStatRow, ToolStepEnvelope, ToolTransientRetrySetting, type TurnFrameSummary, type UiCollector, UpdateThreadInput, UpdateToolCallInput, type UsageTrendPoint, type WindowHistoryOptions, type WithMemoryToolInput, type WriteMemoryInput, actorScope, agentDiagnosticKey, agentFailureCode, bucketByActor, bucketByModel, bucketByThread, bucketUsageTrend, buildMemoryBlock, buildSkillsBlock, canActorUseTool, canonicalJson, closeEmptyRoles, compositeSkillProvider, confirmTokenExpiry, createFrameBuffer, createIncrementalGate, createNoopEmitUi, createUiCollector, dayBoundsUtc, defaultCanDecide, defaultScopeResolver, defineConfirmedTool, detachedDelivered, detachedStarted, detachedUnsettled, estimateCost, estimateMessageTokens, exhaustedWindow, exposeStreamErrorDetails, extractJson, filterToolsByAllowList, filterToolsByCanUse, filterToolsByEnabled, filterToolsByRole, findCatalogModel, gateFollowUps, gateTail, hashConfirmToken, isControlFlowSignal, isReplayIntegrityError, isToolEnabled, loadSkill, mayDecideApproval, memoryForgetVerdict, memoryWriteVerdict, mergeUi, normalizeDelegation, observeTurnFrames, offerMemories, offerSkills, publishAgentDelegated, publishAgentMemoryResolved, publishAgentMemoryWritten, publishAgentMessage, publishAgentQuotaExceeded, publishAgentRetrieved, publishAgentRunFailed, publishAgentRunFinished, publishAgentRunStarted, publishAgentSkillsResolved, publishAgentToolCall, publishAgentToolRetry, quotaPeriodRange, quotaUsedRatio, quotaWarning, releaseGatedFrames, rememberInputSchema, rememberToolDefinition, repairInstruction, resolveGateLookback, resolveMemoryDigest, resolveOutputGateMode, resolveSkillCatalog, rollupThreadUsage, runAgentLoop, runInputProcessors, runOutputProcessors, schemaExtensionOf, seedModelPrices, settleAll, settleDeadRun, settleUnsettledDelegation, signConfirmToken, skillInputSchema, skillToolDefinition, skillWriteVerdict, stampToolKinds, staticModelCatalog, staticSkillProvider, streamFailure, summarizeWithModel, tenantScope, toolCallApprovalFromRow, toolCallContext, traceLlmTurn, traceToolExecution, truncateDetailContent, unwrapToolStepOutput, validateStructured, verifyConfirmToken, windowHistory, withAskTool, withConfirmFields, withMemoryTool, withSelectedModel, withSkillTool, withToolTimeout, withTurnFrames, wrapToolStepOutput, writeMemory };
|
package/dist/index.js
CHANGED
|
@@ -88,7 +88,7 @@ __name(decodeStreamEvent, "decodeStreamEvent");
|
|
|
88
88
|
|
|
89
89
|
// src/turn-frames.ts
|
|
90
90
|
function observeTurnFrames(writer, now = Date.now) {
|
|
91
|
-
const
|
|
91
|
+
const decoder3 = new TextDecoder();
|
|
92
92
|
let pending = "";
|
|
93
93
|
let reasoning = "";
|
|
94
94
|
let reasoningMs = 0;
|
|
@@ -128,7 +128,7 @@ function observeTurnFrames(writer, now = Date.now) {
|
|
|
128
128
|
}
|
|
129
129
|
__name(observeLine, "observeLine");
|
|
130
130
|
function observe(chunk) {
|
|
131
|
-
pending +=
|
|
131
|
+
pending += decoder3.decode(chunk, {
|
|
132
132
|
stream: true
|
|
133
133
|
});
|
|
134
134
|
let newline = pending.indexOf("\n");
|
|
@@ -6097,6 +6097,245 @@ function defineConfirmedTool(options, steps) {
|
|
|
6097
6097
|
};
|
|
6098
6098
|
}
|
|
6099
6099
|
__name(defineConfirmedTool, "defineConfirmedTool");
|
|
6100
|
+
|
|
6101
|
+
// src/sql-token-stream-sink.ts
|
|
6102
|
+
var DEFAULT_TTL_SECONDS = 3600;
|
|
6103
|
+
var DEFAULT_POLL_INTERVAL_MS = 250;
|
|
6104
|
+
var DEFAULT_IDLE_POLL_INTERVAL_MS = 1e3;
|
|
6105
|
+
var IDLE_AFTER_MS = 5e3;
|
|
6106
|
+
var DEFAULT_FLUSH_MS = 50;
|
|
6107
|
+
var READ_PAGE = 500;
|
|
6108
|
+
var PURGE_CHUNK = 200;
|
|
6109
|
+
var AUTO_PURGE_EVERY_MS = 6e4;
|
|
6110
|
+
var APPEND_ATTEMPTS = 8;
|
|
6111
|
+
var decoder2 = new TextDecoder();
|
|
6112
|
+
var encoder2 = new TextEncoder();
|
|
6113
|
+
var sleep = /* @__PURE__ */ __name((ms) => new Promise((resolve) => setTimeout(resolve, ms)), "sleep");
|
|
6114
|
+
function textOf(line) {
|
|
6115
|
+
if (!line.startsWith('{"kind":"text"')) return null;
|
|
6116
|
+
try {
|
|
6117
|
+
const parsed = JSON.parse(line);
|
|
6118
|
+
if (typeof parsed === "object" && parsed !== null && Object.keys(parsed).length === 2 && parsed.kind === "text" && typeof parsed.text === "string") {
|
|
6119
|
+
return parsed.text;
|
|
6120
|
+
}
|
|
6121
|
+
} catch {
|
|
6122
|
+
}
|
|
6123
|
+
return null;
|
|
6124
|
+
}
|
|
6125
|
+
__name(textOf, "textOf");
|
|
6126
|
+
var SqlTokenStreamSink = class {
|
|
6127
|
+
static {
|
|
6128
|
+
__name(this, "SqlTokenStreamSink");
|
|
6129
|
+
}
|
|
6130
|
+
table;
|
|
6131
|
+
pollIntervalMs;
|
|
6132
|
+
idlePollIntervalMs;
|
|
6133
|
+
flushMs;
|
|
6134
|
+
ttlSeconds;
|
|
6135
|
+
autoPurge;
|
|
6136
|
+
writes = /* @__PURE__ */ new Map();
|
|
6137
|
+
lastAutoPurge = 0;
|
|
6138
|
+
constructor(table, options = {}) {
|
|
6139
|
+
this.table = table;
|
|
6140
|
+
this.pollIntervalMs = Math.max(1, options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS);
|
|
6141
|
+
this.idlePollIntervalMs = Math.max(this.pollIntervalMs, options.idlePollIntervalMs ?? DEFAULT_IDLE_POLL_INTERVAL_MS);
|
|
6142
|
+
this.flushMs = Math.max(0, options.flushMs ?? DEFAULT_FLUSH_MS);
|
|
6143
|
+
this.ttlSeconds = options.ttlSeconds ?? DEFAULT_TTL_SECONDS;
|
|
6144
|
+
this.autoPurge = options.autoPurge ?? true;
|
|
6145
|
+
}
|
|
6146
|
+
runWrites(runId) {
|
|
6147
|
+
let state = this.writes.get(runId);
|
|
6148
|
+
if (state === void 0) {
|
|
6149
|
+
state = {
|
|
6150
|
+
text: null,
|
|
6151
|
+
timer: void 0,
|
|
6152
|
+
tail: Promise.resolve(),
|
|
6153
|
+
pending: 0,
|
|
6154
|
+
failure: void 0
|
|
6155
|
+
};
|
|
6156
|
+
this.writes.set(runId, state);
|
|
6157
|
+
}
|
|
6158
|
+
return state;
|
|
6159
|
+
}
|
|
6160
|
+
/** Forget a run's write state once nothing is gathered, queued or left to report. */
|
|
6161
|
+
release(runId, state) {
|
|
6162
|
+
if (state.pending === 0 && state.text === null && state.failure === void 0 && this.writes.get(runId) === state) {
|
|
6163
|
+
this.writes.delete(runId);
|
|
6164
|
+
}
|
|
6165
|
+
}
|
|
6166
|
+
/** Queue `work` behind every write already queued for the run, so rows land in call order. */
|
|
6167
|
+
enqueue(runId, state, work) {
|
|
6168
|
+
state.pending += 1;
|
|
6169
|
+
const next = state.tail.then(work);
|
|
6170
|
+
state.tail = next.then(() => void 0, () => void 0).then(() => {
|
|
6171
|
+
state.pending -= 1;
|
|
6172
|
+
this.release(runId, state);
|
|
6173
|
+
});
|
|
6174
|
+
return next;
|
|
6175
|
+
}
|
|
6176
|
+
/** Queue the gathered text (if any) as one `text` row. */
|
|
6177
|
+
flushText(runId, state) {
|
|
6178
|
+
if (state.timer !== void 0) {
|
|
6179
|
+
clearTimeout(state.timer);
|
|
6180
|
+
state.timer = void 0;
|
|
6181
|
+
}
|
|
6182
|
+
if (state.text === null) return Promise.resolve();
|
|
6183
|
+
const line = decoder2.decode(encodeStreamEvent({
|
|
6184
|
+
kind: "text",
|
|
6185
|
+
text: state.text
|
|
6186
|
+
}));
|
|
6187
|
+
state.text = null;
|
|
6188
|
+
return this.enqueue(runId, state, () => this.append(runId, line, null));
|
|
6189
|
+
}
|
|
6190
|
+
/** Raise, once, a gathered write that failed with nobody awaiting it. */
|
|
6191
|
+
raiseFailure(state) {
|
|
6192
|
+
const failure = state.failure;
|
|
6193
|
+
if (failure !== void 0) {
|
|
6194
|
+
state.failure = void 0;
|
|
6195
|
+
throw failure.error;
|
|
6196
|
+
}
|
|
6197
|
+
}
|
|
6198
|
+
/** Append one row — a frame, or the terminal row (`frame: null`, with `error` on a failure). */
|
|
6199
|
+
async append(runId, frame, error) {
|
|
6200
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
6201
|
+
try {
|
|
6202
|
+
await this.table.append(runId, {
|
|
6203
|
+
frame,
|
|
6204
|
+
error,
|
|
6205
|
+
createdAt: Date.now()
|
|
6206
|
+
});
|
|
6207
|
+
return;
|
|
6208
|
+
} catch (caught) {
|
|
6209
|
+
if (attempt >= APPEND_ATTEMPTS || !this.table.isUniqueViolation(caught)) throw caught;
|
|
6210
|
+
}
|
|
6211
|
+
}
|
|
6212
|
+
}
|
|
6213
|
+
/** Write out what is gathered, then a terminal row; raise a failure nobody saw. */
|
|
6214
|
+
async terminate(runId, error) {
|
|
6215
|
+
const state = this.runWrites(runId);
|
|
6216
|
+
const flushed = this.flushText(runId, state);
|
|
6217
|
+
const ended = this.enqueue(runId, state, () => this.append(runId, null, error));
|
|
6218
|
+
await Promise.all([
|
|
6219
|
+
flushed,
|
|
6220
|
+
ended
|
|
6221
|
+
]);
|
|
6222
|
+
this.raiseFailure(state);
|
|
6223
|
+
this.release(runId, state);
|
|
6224
|
+
this.purgeInBackground();
|
|
6225
|
+
}
|
|
6226
|
+
/**
|
|
6227
|
+
* The run's writer. Writers opened for the same run in one process share what they are gathering,
|
|
6228
|
+
* so a delegated run's text (forwarded through a child writer) and its parent's next frame keep
|
|
6229
|
+
* the order they were written in.
|
|
6230
|
+
*/
|
|
6231
|
+
open(runId) {
|
|
6232
|
+
return {
|
|
6233
|
+
write: /* @__PURE__ */ __name(async (chunk) => {
|
|
6234
|
+
const state = this.runWrites(runId);
|
|
6235
|
+
this.raiseFailure(state);
|
|
6236
|
+
const line = decoder2.decode(chunk);
|
|
6237
|
+
const text = this.flushMs > 0 ? textOf(line.trimEnd()) : null;
|
|
6238
|
+
if (text !== null) {
|
|
6239
|
+
state.text = (state.text ?? "") + text;
|
|
6240
|
+
if (state.timer === void 0) {
|
|
6241
|
+
state.timer = setTimeout(() => {
|
|
6242
|
+
state.timer = void 0;
|
|
6243
|
+
this.flushText(runId, state).catch((error) => {
|
|
6244
|
+
state.failure = {
|
|
6245
|
+
error
|
|
6246
|
+
};
|
|
6247
|
+
});
|
|
6248
|
+
}, this.flushMs);
|
|
6249
|
+
}
|
|
6250
|
+
return;
|
|
6251
|
+
}
|
|
6252
|
+
const flushed = this.flushText(runId, state);
|
|
6253
|
+
const appended = this.enqueue(runId, state, () => this.append(runId, line, null));
|
|
6254
|
+
await Promise.all([
|
|
6255
|
+
flushed,
|
|
6256
|
+
appended
|
|
6257
|
+
]);
|
|
6258
|
+
}, "write"),
|
|
6259
|
+
flush: /* @__PURE__ */ __name(async () => {
|
|
6260
|
+
const state = this.runWrites(runId);
|
|
6261
|
+
const flushed = this.flushText(runId, state);
|
|
6262
|
+
await Promise.all([
|
|
6263
|
+
flushed,
|
|
6264
|
+
state.tail
|
|
6265
|
+
]);
|
|
6266
|
+
this.raiseFailure(state);
|
|
6267
|
+
this.release(runId, state);
|
|
6268
|
+
}, "flush"),
|
|
6269
|
+
end: /* @__PURE__ */ __name(() => this.terminate(runId, null), "end"),
|
|
6270
|
+
fail: /* @__PURE__ */ __name((error) => this.terminate(runId, JSON.stringify({
|
|
6271
|
+
code: error.code,
|
|
6272
|
+
message: error.message
|
|
6273
|
+
})), "fail")
|
|
6274
|
+
};
|
|
6275
|
+
}
|
|
6276
|
+
async *subscribe(runId) {
|
|
6277
|
+
let cursor = 0;
|
|
6278
|
+
let quietSince = Date.now();
|
|
6279
|
+
while (true) {
|
|
6280
|
+
const rows = await this.table.read(runId, cursor, READ_PAGE);
|
|
6281
|
+
for (const row of rows) {
|
|
6282
|
+
cursor = row.seq;
|
|
6283
|
+
if (row.frame === null) {
|
|
6284
|
+
if (row.error !== null) {
|
|
6285
|
+
throw new AgentStreamError(JSON.parse(row.error));
|
|
6286
|
+
}
|
|
6287
|
+
return;
|
|
6288
|
+
}
|
|
6289
|
+
yield encoder2.encode(row.frame);
|
|
6290
|
+
}
|
|
6291
|
+
if (rows.length > 0) {
|
|
6292
|
+
quietSince = Date.now();
|
|
6293
|
+
if (rows.length >= READ_PAGE) continue;
|
|
6294
|
+
}
|
|
6295
|
+
await sleep(Date.now() - quietSince >= IDLE_AFTER_MS ? this.idlePollIntervalMs : this.pollIntervalMs);
|
|
6296
|
+
}
|
|
6297
|
+
}
|
|
6298
|
+
/** Does the table hold anything for the run — a frame, or its end? */
|
|
6299
|
+
has(runId) {
|
|
6300
|
+
return this.table.has(runId);
|
|
6301
|
+
}
|
|
6302
|
+
async close(runId) {
|
|
6303
|
+
const state = this.writes.get(runId);
|
|
6304
|
+
if (state !== void 0) {
|
|
6305
|
+
if (state.timer !== void 0) clearTimeout(state.timer);
|
|
6306
|
+
state.timer = void 0;
|
|
6307
|
+
state.text = null;
|
|
6308
|
+
state.failure = void 0;
|
|
6309
|
+
await state.tail;
|
|
6310
|
+
this.writes.delete(runId);
|
|
6311
|
+
}
|
|
6312
|
+
await this.table.remove([
|
|
6313
|
+
runId
|
|
6314
|
+
]);
|
|
6315
|
+
}
|
|
6316
|
+
/**
|
|
6317
|
+
* Delete every run whose last write is older than `ttlSeconds` at `now` (epoch-ms) — ended runs
|
|
6318
|
+
* past their replay window and runs that crashed without ending alike. Returns how many runs went.
|
|
6319
|
+
* Safe from any replica, and from several at once. A no-op under `ttlSeconds: 0`.
|
|
6320
|
+
*/
|
|
6321
|
+
async purgeExpired(now = Date.now()) {
|
|
6322
|
+
if (this.ttlSeconds <= 0) return 0;
|
|
6323
|
+
const lapsed = await this.table.lapsedRuns(now - this.ttlSeconds * 1e3);
|
|
6324
|
+
for (let index = 0; index < lapsed.length; index += PURGE_CHUNK) {
|
|
6325
|
+
await this.table.remove(lapsed.slice(index, index + PURGE_CHUNK));
|
|
6326
|
+
}
|
|
6327
|
+
return lapsed.length;
|
|
6328
|
+
}
|
|
6329
|
+
/** {@link purgeExpired}, unawaited and at most once a minute. Its failure is not the run's. */
|
|
6330
|
+
purgeInBackground() {
|
|
6331
|
+
if (!this.autoPurge || this.ttlSeconds <= 0) return;
|
|
6332
|
+
const now = Date.now();
|
|
6333
|
+
if (now - this.lastAutoPurge < AUTO_PURGE_EVERY_MS) return;
|
|
6334
|
+
this.lastAutoPurge = now;
|
|
6335
|
+
this.purgeExpired(now).catch(() => {
|
|
6336
|
+
});
|
|
6337
|
+
}
|
|
6338
|
+
};
|
|
6100
6339
|
export {
|
|
6101
6340
|
AGENT_ACTOR_DIRECTORY,
|
|
6102
6341
|
AGENT_ACTOR_RESOLVER,
|
|
@@ -6168,6 +6407,7 @@ export {
|
|
|
6168
6407
|
SCHEMA_EXTENSION,
|
|
6169
6408
|
SKILL_TOOL_DESCRIPTION,
|
|
6170
6409
|
SKILL_TOOL_NAME,
|
|
6410
|
+
SqlTokenStreamSink,
|
|
6171
6411
|
StructuredOutputError,
|
|
6172
6412
|
THREAD_DETAIL_CONTENT_CHARS,
|
|
6173
6413
|
ToolDisabledError,
|