@minpeter/pss-runtime 0.1.0-next.2 → 0.1.0-next.4
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 +172 -85
- package/dist/agent-host-session-store.js +2 -4
- package/dist/agent-host-session-store.js.map +1 -1
- package/dist/agent-loop.js +9 -8
- package/dist/agent-loop.js.map +1 -1
- package/dist/agent-namespace.js +3 -7
- package/dist/agent-namespace.js.map +1 -1
- package/dist/agent-options.d.ts +4 -19
- package/dist/agent-options.js +2 -8
- package/dist/agent-options.js.map +1 -1
- package/dist/agent-resume.js +2 -82
- package/dist/agent-resume.js.map +1 -1
- package/dist/agent-session-entry.d.ts +1 -1
- package/dist/agent.d.ts +4 -4
- package/dist/agent.js +19 -89
- package/dist/agent.js.map +1 -1
- package/dist/cloudflare/cloudflare-agent-context.d.ts +40 -0
- package/dist/cloudflare/cloudflare-agent-context.js +37 -0
- package/dist/cloudflare/cloudflare-agent-context.js.map +1 -0
- package/dist/cloudflare/cloudflare-alarm-budget.d.ts +18 -0
- package/dist/cloudflare/cloudflare-alarm-budget.js +77 -0
- package/dist/cloudflare/cloudflare-alarm-budget.js.map +1 -0
- package/dist/cloudflare/cloudflare-alarm-drainer.d.ts +45 -0
- package/dist/cloudflare/cloudflare-alarm-drainer.js +103 -0
- package/dist/cloudflare/cloudflare-alarm-drainer.js.map +1 -0
- package/dist/cloudflare/cloudflare-alarm-run-drain.d.ts +13 -0
- package/dist/cloudflare/cloudflare-alarm-run-drain.js +81 -0
- package/dist/cloudflare/cloudflare-alarm-run-drain.js.map +1 -0
- package/dist/cloudflare/cloudflare-alarm-work.js +110 -0
- package/dist/cloudflare/cloudflare-alarm-work.js.map +1 -0
- package/dist/cloudflare/cloudflare-checkpoint-store.js +39 -0
- package/dist/cloudflare/cloudflare-checkpoint-store.js.map +1 -0
- package/dist/cloudflare/cloudflare-durable-object-fetch.d.ts +21 -0
- package/dist/cloudflare/cloudflare-durable-object-fetch.js +11 -0
- package/dist/cloudflare/cloudflare-durable-object-fetch.js.map +1 -0
- package/dist/cloudflare/cloudflare-event-store.js +33 -0
- package/dist/cloudflare/cloudflare-event-store.js.map +1 -0
- package/dist/cloudflare/cloudflare-execution-session-store.js +40 -0
- package/dist/cloudflare/cloudflare-execution-session-store.js.map +1 -0
- package/dist/cloudflare/cloudflare-execution-store.js +35 -0
- package/dist/cloudflare/cloudflare-execution-store.js.map +1 -0
- package/dist/cloudflare/cloudflare-host.d.ts +61 -0
- package/dist/cloudflare/cloudflare-host.js +113 -0
- package/dist/cloudflare/cloudflare-host.js.map +1 -0
- package/dist/cloudflare/cloudflare-notification-store.js +59 -0
- package/dist/cloudflare/cloudflare-notification-store.js.map +1 -0
- package/dist/cloudflare/cloudflare-run-store.js +81 -0
- package/dist/cloudflare/cloudflare-run-store.js.map +1 -0
- package/dist/cloudflare/cloudflare-store-utils.js +43 -0
- package/dist/cloudflare/cloudflare-store-utils.js.map +1 -0
- package/dist/cloudflare/durable-object-storage.d.ts +20 -0
- package/dist/cloudflare/durable-object-storage.js +76 -0
- package/dist/cloudflare/durable-object-storage.js.map +1 -0
- package/dist/cloudflare/index.d.ts +7 -0
- package/dist/cloudflare/index.js +6 -0
- package/dist/execution/capabilities.d.ts +40 -0
- package/dist/execution/host.d.ts +9 -0
- package/dist/execution/host.js +49 -1
- package/dist/execution/host.js.map +1 -1
- package/dist/execution/index.d.ts +3 -1
- package/dist/execution/index.js +2 -1
- package/dist/execution/memory.js +1 -1
- package/dist/execution/memory.js.map +1 -1
- package/dist/execution/types.d.ts +5 -10
- package/dist/index.d.ts +9 -5
- package/dist/index.js +6 -2
- package/dist/llm-tool-execution.js.map +1 -1
- package/dist/llm.d.ts +1 -21
- package/dist/llm.js +12 -14
- package/dist/llm.js.map +1 -1
- package/dist/plugins.d.ts +27 -5
- package/dist/plugins.js +35 -6
- package/dist/plugins.js.map +1 -1
- package/dist/session/delegate-input.d.ts +9 -0
- package/dist/session/delegate-input.js +16 -0
- package/dist/session/delegate-input.js.map +1 -0
- package/dist/session/events.d.ts +43 -25
- package/dist/session/events.js +41 -0
- package/dist/session/events.js.map +1 -0
- package/dist/session/input-meta-types.d.ts +10 -0
- package/dist/session/input-meta.d.ts +13 -0
- package/dist/session/input-meta.js +45 -0
- package/dist/session/input-meta.js.map +1 -0
- package/dist/session/input.d.ts +4 -0
- package/dist/session/mapping.js +4 -2
- package/dist/session/mapping.js.map +1 -1
- package/dist/session/runtime-input-emit.js +41 -0
- package/dist/session/runtime-input-emit.js.map +1 -0
- package/dist/session/runtime-input.js +5 -1
- package/dist/session/runtime-input.js.map +1 -1
- package/dist/session/session-events.js +20 -6
- package/dist/session/session-events.js.map +1 -1
- package/dist/session/session-notification.js +3 -2
- package/dist/session/session-notification.js.map +1 -1
- package/dist/session/session-runtime-drain.js +3 -9
- package/dist/session/session-runtime-drain.js.map +1 -1
- package/dist/session/session-turn-processor.js +10 -20
- package/dist/session/session-turn-processor.js.map +1 -1
- package/dist/session/session.js +15 -8
- package/dist/session/session.js.map +1 -1
- package/package.json +6 -1
- package/dist/agent-child-runs.js +0 -16
- package/dist/agent-child-runs.js.map +0 -1
- package/dist/agent-host-capabilities.js +0 -9
- package/dist/agent-host-capabilities.js.map +0 -1
- package/dist/agent-validation.js +0 -35
- package/dist/agent-validation.js.map +0 -1
- package/dist/child-session-cleanups.js +0 -61
- package/dist/child-session-cleanups.js.map +0 -1
- package/dist/execution/run.js +0 -55
- package/dist/execution/run.js.map +0 -1
- package/dist/subagent-background-child-run-state.js +0 -51
- package/dist/subagent-background-child-run-state.js.map +0 -1
- package/dist/subagent-background-child-run.js +0 -103
- package/dist/subagent-background-child-run.js.map +0 -1
- package/dist/subagent-background-in-process.js +0 -98
- package/dist/subagent-background-in-process.js.map +0 -1
- package/dist/subagent-background-notification-inbox.js +0 -106
- package/dist/subagent-background-notification-inbox.js.map +0 -1
- package/dist/subagent-background-notify.js +0 -136
- package/dist/subagent-background-notify.js.map +0 -1
- package/dist/subagent-background-resume-group.js +0 -99
- package/dist/subagent-background-resume-group.js.map +0 -1
- package/dist/subagent-background-runner.js +0 -115
- package/dist/subagent-background-runner.js.map +0 -1
- package/dist/subagent-background-schedule.js +0 -43
- package/dist/subagent-background-schedule.js.map +0 -1
- package/dist/subagent-child-run.js +0 -68
- package/dist/subagent-child-run.js.map +0 -1
- package/dist/subagent-job-cancel.js +0 -84
- package/dist/subagent-job-cancel.js.map +0 -1
- package/dist/subagent-job-observer.js +0 -19
- package/dist/subagent-job-observer.js.map +0 -1
- package/dist/subagent-job-output.js +0 -87
- package/dist/subagent-job-output.js.map +0 -1
- package/dist/subagent-job-state.js +0 -66
- package/dist/subagent-job-state.js.map +0 -1
- package/dist/subagent-jobs.js +0 -96
- package/dist/subagent-jobs.js.map +0 -1
- package/dist/subagent-prompt-schema.js +0 -114
- package/dist/subagent-prompt-schema.js.map +0 -1
- package/dist/subagent-run.js +0 -111
- package/dist/subagent-run.js.map +0 -1
- package/dist/subagents.js +0 -125
- package/dist/subagents.js.map +0 -1
package/dist/execution/memory.js
CHANGED
|
@@ -2,7 +2,7 @@ import { InMemoryExecutionStore } from "./memory-store.js";
|
|
|
2
2
|
//#region src/execution/memory.ts
|
|
3
3
|
function createInMemoryExecutionHost() {
|
|
4
4
|
return {
|
|
5
|
-
capabilities: {
|
|
5
|
+
capabilities: {},
|
|
6
6
|
store: new InMemoryExecutionStore(),
|
|
7
7
|
scheduler: new InMemoryExecutionScheduler()
|
|
8
8
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.js","names":["#queuedRunIds","#resumedSessions"],"sources":["../../src/execution/memory.ts"],"sourcesContent":["import { InMemoryExecutionStore } from \"./memory-store\";\nimport type {\n ExecutionHost,\n ExecutionScheduler,\n ResumeSessionOptions,\n} from \"./types\";\n\nexport function createInMemoryExecutionHost(): ExecutionHost {\n return {\n capabilities: {
|
|
1
|
+
{"version":3,"file":"memory.js","names":["#queuedRunIds","#resumedSessions"],"sources":["../../src/execution/memory.ts"],"sourcesContent":["import { InMemoryExecutionStore } from \"./memory-store\";\nimport type {\n ExecutionHost,\n ExecutionScheduler,\n ResumeSessionOptions,\n} from \"./types\";\n\nexport function createInMemoryExecutionHost(): ExecutionHost {\n return {\n capabilities: {},\n store: new InMemoryExecutionStore(),\n scheduler: new InMemoryExecutionScheduler(),\n };\n}\n\nclass InMemoryExecutionScheduler implements ExecutionScheduler {\n readonly #queuedRunIds: string[] = [];\n readonly #resumedSessions: {\n readonly options: ResumeSessionOptions;\n readonly sessionKey: string;\n }[] = [];\n\n enqueueRun(runId: string): Promise<void> {\n this.#queuedRunIds.push(runId);\n return Promise.resolve();\n }\n\n resumeSession(\n sessionKey: string,\n options: ResumeSessionOptions\n ): Promise<void> {\n this.#resumedSessions.push({ options, sessionKey });\n return Promise.resolve();\n }\n}\n"],"mappings":";;AAOA,SAAgB,8BAA6C;CAC3D,OAAO;EACL,cAAc,CAAC;EACf,OAAO,IAAI,uBAAuB;EAClC,WAAW,IAAI,2BAA2B;CAC5C;AACF;AAEA,IAAM,6BAAN,MAA+D;CAC7D,gBAAmC,CAAC;CACpC,mBAGM,CAAC;CAEP,WAAW,OAA8B;EACvC,KAAKA,cAAc,KAAK,KAAK;EAC7B,OAAO,QAAQ,QAAQ;CACzB;CAEA,cACE,YACA,SACe;EACf,KAAKC,iBAAiB,KAAK;GAAE;GAAS;EAAW,CAAC;EAClD,OAAO,QAAQ,QAAQ;CACzB;AACF"}
|
|
@@ -1,21 +1,16 @@
|
|
|
1
|
+
import { SessionStore } from "../session/store/types.js";
|
|
1
2
|
import { UserInput } from "../session/input.js";
|
|
2
3
|
import { AgentEvent } from "../session/events.js";
|
|
3
|
-
import {
|
|
4
|
+
import { DurableBackgroundHost, SessionHost } from "./capabilities.js";
|
|
4
5
|
|
|
5
6
|
//#region src/execution/types.d.ts
|
|
6
|
-
type AgentHost =
|
|
7
|
-
interface SessionHost {
|
|
8
|
-
readonly capabilities?: AgentHostCapabilities;
|
|
9
|
-
readonly sessionStore?: SessionStore;
|
|
10
|
-
}
|
|
7
|
+
type AgentHost = DurableBackgroundHost | ExecutionHost | SessionHost;
|
|
11
8
|
interface ExecutionHost {
|
|
12
9
|
readonly capabilities: AgentHostCapabilities;
|
|
13
10
|
readonly scheduler: ExecutionScheduler;
|
|
14
11
|
readonly store: ExecutionStore;
|
|
15
12
|
}
|
|
16
|
-
|
|
17
|
-
readonly backgroundSubagents?: "durable" | "in-process";
|
|
18
|
-
}
|
|
13
|
+
type AgentHostCapabilities = Record<string, never>;
|
|
19
14
|
interface ExecutionScheduler {
|
|
20
15
|
enqueueRun(runId: string, options?: {
|
|
21
16
|
readonly runAfterMs?: number;
|
|
@@ -27,7 +22,7 @@ interface ResumeSessionOptions {
|
|
|
27
22
|
readonly notificationId?: string;
|
|
28
23
|
readonly runId: string;
|
|
29
24
|
}
|
|
30
|
-
type RunKind = "
|
|
25
|
+
type RunKind = "notification" | "tool-recovery" | "user-turn";
|
|
31
26
|
type RunStatus = "cancelled" | "completed" | "error" | "leased" | "needs-recovery" | "queued" | "running" | "suspended";
|
|
32
27
|
interface RunLease {
|
|
33
28
|
readonly attempt: number;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import { AgentToolChoice
|
|
2
|
-
import { AgentInput, SessionInput, UserInput, UserMessage, UserMessageContent, UserMessageContentPart, UserMessageFileData, UserMessageFilePart, UserMessageImagePart, UserMessageTextPart, UserText, UserTextContent } from "./session/input.js";
|
|
3
|
-
import { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, RuntimeInput, ToolCall, ToolResult } from "./session/events.js";
|
|
1
|
+
import { AgentToolChoice } from "./llm.js";
|
|
4
2
|
import { CommitResult, ExpectedSessionVersion, SessionStore, SessionStoreCommit, StoredSession } from "./session/store/types.js";
|
|
3
|
+
import { InputEventMeta, InputSource } from "./session/input-meta-types.js";
|
|
4
|
+
import { AgentInput, SessionInput, UserInput, UserMessage, UserMessageContent, UserMessageContentPart, UserMessageFileData, UserMessageFilePart, UserMessageImagePart, UserMessageTextPart, UserText, UserTextContent } from "./session/input.js";
|
|
5
|
+
import { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, ControlAgentEvent, LifecycleAgentEvent, RuntimeInput, TelemetryAgentEvent, ToolAgentEvent, ToolCall, ToolResult, VisibleAgentEvent, isControlAgentEvent, isLifecycleAgentEvent, isTelemetryAgentEvent, isToolAgentEvent, isVisibleAgentEvent } from "./session/events.js";
|
|
5
6
|
import { AgentHost } from "./execution/types.js";
|
|
6
|
-
import {
|
|
7
|
+
import { executionHost } from "./execution/host.js";
|
|
8
|
+
import { AgentEventContext, AgentPlugin, AgentPluginInterceptResult, AgentPluginResult, InterceptableAgentEvent, PluginPipelineResult, runPluginsForEvent } from "./plugins.js";
|
|
7
9
|
import { AgentOptions } from "./agent-options.js";
|
|
8
10
|
import { AgentRun } from "./session/run.js";
|
|
9
11
|
import { SessionHandle } from "./agent-session-entry.js";
|
|
10
12
|
import { Agent } from "./agent.js";
|
|
11
|
-
|
|
13
|
+
import { delegateUserInput } from "./session/delegate-input.js";
|
|
14
|
+
import { attachInputMeta, attachRuntimeInputMeta, stripEventMeta, stripInputMeta, userInputFromEvent } from "./session/input-meta.js";
|
|
15
|
+
export { Agent, type AgentEvent, type AgentEventContext, type AgentEventListener, type AgentHost, type AgentInput, type AgentOptions, type AgentPlugin, type AgentPluginInterceptResult, type AgentPluginResult, type AgentRun, type AgentToolChoice, type AssistantReasoning, type AssistantText, type CommitResult, type ControlAgentEvent, type ExpectedSessionVersion, type InputEventMeta, type InputSource, type InterceptableAgentEvent, type LifecycleAgentEvent, type PluginPipelineResult, type RuntimeInput, type SessionHandle, type SessionInput, type SessionStore, type SessionStoreCommit, type StoredSession, type TelemetryAgentEvent, type ToolAgentEvent, type ToolCall, type ToolResult, type UserInput, type UserMessage, type UserMessageContent, type UserMessageContentPart, type UserMessageFileData, type UserMessageFilePart, type UserMessageImagePart, type UserMessageTextPart, type UserText, type UserTextContent, type VisibleAgentEvent, attachInputMeta, attachRuntimeInputMeta, delegateUserInput, executionHost, isControlAgentEvent, isLifecycleAgentEvent, isTelemetryAgentEvent, isToolAgentEvent, isVisibleAgentEvent, runPluginsForEvent, stripEventMeta, stripInputMeta, userInputFromEvent };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { executionHost } from "./execution/host.js";
|
|
2
|
+
import { attachInputMeta, attachRuntimeInputMeta, stripEventMeta, stripInputMeta, userInputFromEvent } from "./session/input-meta.js";
|
|
3
|
+
import { runPluginsForEvent } from "./plugins.js";
|
|
2
4
|
import { Agent } from "./agent.js";
|
|
3
|
-
|
|
5
|
+
import { delegateUserInput } from "./session/delegate-input.js";
|
|
6
|
+
import { isControlAgentEvent, isLifecycleAgentEvent, isTelemetryAgentEvent, isToolAgentEvent, isVisibleAgentEvent } from "./session/events.js";
|
|
7
|
+
export { Agent, attachInputMeta, attachRuntimeInputMeta, delegateUserInput, executionHost, isControlAgentEvent, isLifecycleAgentEvent, isTelemetryAgentEvent, isToolAgentEvent, isVisibleAgentEvent, runPluginsForEvent, stripEventMeta, stripInputMeta, userInputFromEvent };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm-tool-execution.js","names":[],"sources":["../src/llm-tool-execution.ts"],"sourcesContent":["import type { AssistantModelMessage, ToolModelMessage, ToolSet } from \"ai\";\nimport type { RuntimeLlmOutput } from \"./llm\";\n\nconst toolCallIdPrefix = \"call_\";\nconst publicToolCallIdPattern = /^call[-_]/;\n\ntype RuntimeLlmMessage = RuntimeLlmOutput[number];\n\nexport type RuntimeToolRetryPolicy = \"idempotent\" | \"manual-recovery\" | \"pure\";\n\nexport interface RuntimeToolExecutionCheckpointMetadata {\n readonly attempt: number;\n readonly idempotencyKey: string;\n readonly policy: RuntimeToolRetryPolicy;\n readonly toolCallId: string;\n readonly toolName: string;\n}\n\nexport interface RuntimeToolExecutionCheckpoint\n extends RuntimeToolExecutionCheckpointMetadata {\n readonly input: unknown;\n}\n\nexport type RuntimePersistedToolExecutionCheckpoint =\n RuntimeToolExecutionCheckpointMetadata;\n\nexport type RuntimeToolExecutionDecision =\n | { readonly status: \"needs-recovery\" }\n | undefined;\n\nexport interface RuntimeToolExecutionContext {\n readonly afterTool?: (\n checkpoint: RuntimeToolExecutionCheckpoint & { readonly output: unknown }\n ) => Promise<void> | void;\n readonly attempt: number;\n readonly beforeTool?: (\n checkpoint: RuntimeToolExecutionCheckpoint\n ) => Promise<RuntimeToolExecutionDecision> | RuntimeToolExecutionDecision;\n readonly runId: string;\n}\n\nexport class ToolExecutionNeedsRecoveryError extends Error {\n readonly idempotencyKey: string;\n readonly status = \"needs-recovery\";\n readonly toolCallId: string;\n readonly toolName: string;\n\n constructor(checkpoint: RuntimeToolExecutionCheckpointMetadata) {\n super(\n `Tool ${checkpoint.toolName} requires manual recovery for ${checkpoint.idempotencyKey}`\n );\n this.idempotencyKey = checkpoint.idempotencyKey;\n this.name = \"ToolExecutionNeedsRecoveryError\";\n this.toolCallId = checkpoint.toolCallId;\n this.toolName = checkpoint.toolName;\n }\n}\n\nexport function persistedToolExecutionCheckpoint(\n checkpoint: RuntimeToolExecutionCheckpointMetadata\n): RuntimePersistedToolExecutionCheckpoint {\n return {\n attempt: checkpoint.attempt,\n idempotencyKey: checkpoint.idempotencyKey,\n policy: checkpoint.policy,\n toolCallId: checkpoint.toolCallId,\n toolName: checkpoint.toolName,\n };\n}\n\nexport function normalizeToolCallIds(\n tools: ToolSet | undefined,\n toolCallIds: Map<string, string>,\n toolExecution: RuntimeToolExecutionContext | undefined\n): ToolSet | undefined {\n if (!tools) {\n return;\n }\n\n return Object.fromEntries(\n Object.entries(tools).map(([name, candidate]) => [\n name,\n wrapToolExecute(name, candidate, toolCallIds, toolExecution),\n ])\n ) as ToolSet;\n}\n\nexport function rewriteMessageToolCallIds(\n message: RuntimeLlmMessage,\n toolCallIds: Map<string, string>\n): RuntimeLlmMessage {\n if (message.role === \"assistant\") {\n return rewriteAssistantToolCallIds(message, toolCallIds);\n }\n\n if (message.role === \"tool\") {\n return rewriteToolResultCallIds(message, toolCallIds);\n }\n\n return message;\n}\n\nfunction wrapToolExecute(\n toolName: string,\n toolDefinition: unknown,\n toolCallIds: Map<string, string>,\n toolExecution: RuntimeToolExecutionContext | undefined\n): unknown {\n if (!isExecutableToolDefinition(toolDefinition)) {\n return toolDefinition;\n }\n\n const { execute } = toolDefinition;\n if (!toolExecution) {\n return {\n ...toolDefinition,\n execute: (input: unknown, options: ToolExecutionOptionsLike) =>\n execute(input, {\n ...options,\n toolCallId: publicToolCallId(options.toolCallId, toolCallIds),\n }),\n };\n }\n\n return {\n ...toolDefinition,\n execute: async (input: unknown, options: ToolExecutionOptionsLike) => {\n const toolCallId = publicToolCallId(options.toolCallId, toolCallIds);\n const checkpoint = toolCheckpoint({\n input,\n policy: toolRetryPolicy(toolDefinition),\n toolCallId,\n toolExecution,\n toolName,\n });\n const decision = await toolExecution.beforeTool?.(checkpoint);\n if (decision?.status === \"needs-recovery\") {\n throw new ToolExecutionNeedsRecoveryError(checkpoint);\n }\n\n const output = await execute(input, {\n ...options,\n attempt: checkpoint.attempt,\n idempotencyKey: checkpoint.idempotencyKey,\n retryPolicy: checkpoint.policy,\n ...(options.abortSignal === undefined\n ? {}\n : { signal: options.abortSignal }),\n toolCallId,\n });\n await toolExecution.afterTool?.({ ...checkpoint, output });\n return output;\n },\n };\n}\n\nfunction isExecutableToolDefinition(value: unknown): value is {\n readonly execute: (\n input: unknown,\n options: RuntimeToolExecutionOptionsLike | ToolExecutionOptionsLike\n ) => unknown;\n} {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"execute\" in value &&\n typeof value.execute === \"function\"\n );\n}\n\ninterface ToolExecutionOptionsLike {\n readonly abortSignal?: AbortSignal;\n readonly toolCallId: string;\n}\n\ninterface RuntimeToolExecutionOptionsLike extends ToolExecutionOptionsLike {\n readonly attempt: number;\n readonly idempotencyKey: string;\n readonly retryPolicy: RuntimeToolRetryPolicy;\n readonly signal?: AbortSignal;\n}\n\nfunction toolCheckpoint({\n input,\n policy,\n toolCallId,\n toolExecution,\n toolName,\n}: {\n readonly input: unknown;\n readonly policy: RuntimeToolRetryPolicy;\n readonly toolCallId: string;\n readonly toolExecution: RuntimeToolExecutionContext;\n readonly toolName: string;\n}): RuntimeToolExecutionCheckpoint {\n return {\n attempt: toolExecution.attempt,\n idempotencyKey: `${toolExecution.runId}:${toolCallId}`,\n input,\n policy,\n toolCallId,\n toolName,\n };\n}\n\nfunction toolRetryPolicy(toolDefinition: unknown): RuntimeToolRetryPolicy {\n if (\n typeof toolDefinition === \"object\" &&\n toolDefinition !== null &&\n \"retryPolicy\" in toolDefinition &&\n isToolRetryPolicy(toolDefinition.retryPolicy)\n ) {\n return toolDefinition.retryPolicy;\n }\n\n return \"manual-recovery\";\n}\n\nfunction isToolRetryPolicy(value: unknown): value is RuntimeToolRetryPolicy {\n return (\n value === \"idempotent\" || value === \"manual-recovery\" || value === \"pure\"\n );\n}\n\nfunction rewriteAssistantToolCallIds(\n message: AssistantModelMessage,\n toolCallIds: Map<string, string>\n): AssistantModelMessage {\n if (typeof message.content === \"string\") {\n return message;\n }\n\n return {\n ...message,\n content: message.content.map((part) =>\n \"toolCallId\" in part\n ? {\n ...part,\n toolCallId: publicToolCallId(part.toolCallId, toolCallIds),\n }\n : part\n ),\n };\n}\n\nfunction rewriteToolResultCallIds(\n message: ToolModelMessage,\n toolCallIds: Map<string, string>\n): ToolModelMessage {\n return {\n ...message,\n content: message.content.map((part) =>\n \"toolCallId\" in part\n ? {\n ...part,\n toolCallId: publicToolCallId(part.toolCallId, toolCallIds),\n }\n : part\n ),\n };\n}\n\nfunction publicToolCallId(\n toolCallId: string,\n toolCallIds: Map<string, string>\n): string {\n if (publicToolCallIdPattern.test(toolCallId)) {\n return toolCallId;\n }\n\n const existing = toolCallIds.get(toolCallId);\n if (existing) {\n return existing;\n }\n\n const generated = createToolCallId();\n toolCallIds.set(toolCallId, generated);\n return generated;\n}\n\nfunction createToolCallId(): string {\n return `${toolCallIdPrefix}${crypto.randomUUID().replaceAll(\"-\", \"\")}`;\n}\n"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"llm-tool-execution.js","names":[],"sources":["../src/llm-tool-execution.ts"],"sourcesContent":["import type { AssistantModelMessage, ToolModelMessage, ToolSet } from \"ai\";\nimport type { ModelStepOutput } from \"./llm\";\n\nconst toolCallIdPrefix = \"call_\";\nconst publicToolCallIdPattern = /^call[-_]/;\n\ntype ModelStepMessage = ModelStepOutput[number];\n\nexport type RuntimeToolRetryPolicy = \"idempotent\" | \"manual-recovery\" | \"pure\";\n\nexport interface RuntimeToolExecutionCheckpointMetadata {\n readonly attempt: number;\n readonly idempotencyKey: string;\n readonly policy: RuntimeToolRetryPolicy;\n readonly toolCallId: string;\n readonly toolName: string;\n}\n\nexport interface RuntimeToolExecutionCheckpoint\n extends RuntimeToolExecutionCheckpointMetadata {\n readonly input: unknown;\n}\n\nexport type RuntimePersistedToolExecutionCheckpoint =\n RuntimeToolExecutionCheckpointMetadata;\n\nexport type RuntimeToolExecutionDecision =\n | { readonly status: \"needs-recovery\" }\n | undefined;\n\nexport interface RuntimeToolExecutionContext {\n readonly afterTool?: (\n checkpoint: RuntimeToolExecutionCheckpoint & { readonly output: unknown }\n ) => Promise<void> | void;\n readonly attempt: number;\n readonly beforeTool?: (\n checkpoint: RuntimeToolExecutionCheckpoint\n ) => Promise<RuntimeToolExecutionDecision> | RuntimeToolExecutionDecision;\n readonly runId: string;\n}\n\nexport class ToolExecutionNeedsRecoveryError extends Error {\n readonly idempotencyKey: string;\n readonly status = \"needs-recovery\";\n readonly toolCallId: string;\n readonly toolName: string;\n\n constructor(checkpoint: RuntimeToolExecutionCheckpointMetadata) {\n super(\n `Tool ${checkpoint.toolName} requires manual recovery for ${checkpoint.idempotencyKey}`\n );\n this.idempotencyKey = checkpoint.idempotencyKey;\n this.name = \"ToolExecutionNeedsRecoveryError\";\n this.toolCallId = checkpoint.toolCallId;\n this.toolName = checkpoint.toolName;\n }\n}\n\nexport function persistedToolExecutionCheckpoint(\n checkpoint: RuntimeToolExecutionCheckpointMetadata\n): RuntimePersistedToolExecutionCheckpoint {\n return {\n attempt: checkpoint.attempt,\n idempotencyKey: checkpoint.idempotencyKey,\n policy: checkpoint.policy,\n toolCallId: checkpoint.toolCallId,\n toolName: checkpoint.toolName,\n };\n}\n\nexport function normalizeToolCallIds(\n tools: ToolSet | undefined,\n toolCallIds: Map<string, string>,\n toolExecution: RuntimeToolExecutionContext | undefined\n): ToolSet | undefined {\n if (!tools) {\n return;\n }\n\n return Object.fromEntries(\n Object.entries(tools).map(([name, candidate]) => [\n name,\n wrapToolExecute(name, candidate, toolCallIds, toolExecution),\n ])\n ) as ToolSet;\n}\n\nexport function rewriteMessageToolCallIds(\n message: ModelStepMessage,\n toolCallIds: Map<string, string>\n): ModelStepMessage {\n if (message.role === \"assistant\") {\n return rewriteAssistantToolCallIds(message, toolCallIds);\n }\n\n if (message.role === \"tool\") {\n return rewriteToolResultCallIds(message, toolCallIds);\n }\n\n return message;\n}\n\nfunction wrapToolExecute(\n toolName: string,\n toolDefinition: unknown,\n toolCallIds: Map<string, string>,\n toolExecution: RuntimeToolExecutionContext | undefined\n): unknown {\n if (!isExecutableToolDefinition(toolDefinition)) {\n return toolDefinition;\n }\n\n const { execute } = toolDefinition;\n if (!toolExecution) {\n return {\n ...toolDefinition,\n execute: (input: unknown, options: ToolExecutionOptionsLike) =>\n execute(input, {\n ...options,\n toolCallId: publicToolCallId(options.toolCallId, toolCallIds),\n }),\n };\n }\n\n return {\n ...toolDefinition,\n execute: async (input: unknown, options: ToolExecutionOptionsLike) => {\n const toolCallId = publicToolCallId(options.toolCallId, toolCallIds);\n const checkpoint = toolCheckpoint({\n input,\n policy: toolRetryPolicy(toolDefinition),\n toolCallId,\n toolExecution,\n toolName,\n });\n const decision = await toolExecution.beforeTool?.(checkpoint);\n if (decision?.status === \"needs-recovery\") {\n throw new ToolExecutionNeedsRecoveryError(checkpoint);\n }\n\n const output = await execute(input, {\n ...options,\n attempt: checkpoint.attempt,\n idempotencyKey: checkpoint.idempotencyKey,\n retryPolicy: checkpoint.policy,\n ...(options.abortSignal === undefined\n ? {}\n : { signal: options.abortSignal }),\n toolCallId,\n });\n await toolExecution.afterTool?.({ ...checkpoint, output });\n return output;\n },\n };\n}\n\nfunction isExecutableToolDefinition(value: unknown): value is {\n readonly execute: (\n input: unknown,\n options: RuntimeToolExecutionOptionsLike | ToolExecutionOptionsLike\n ) => unknown;\n} {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"execute\" in value &&\n typeof value.execute === \"function\"\n );\n}\n\ninterface ToolExecutionOptionsLike {\n readonly abortSignal?: AbortSignal;\n readonly toolCallId: string;\n}\n\ninterface RuntimeToolExecutionOptionsLike extends ToolExecutionOptionsLike {\n readonly attempt: number;\n readonly idempotencyKey: string;\n readonly retryPolicy: RuntimeToolRetryPolicy;\n readonly signal?: AbortSignal;\n}\n\nfunction toolCheckpoint({\n input,\n policy,\n toolCallId,\n toolExecution,\n toolName,\n}: {\n readonly input: unknown;\n readonly policy: RuntimeToolRetryPolicy;\n readonly toolCallId: string;\n readonly toolExecution: RuntimeToolExecutionContext;\n readonly toolName: string;\n}): RuntimeToolExecutionCheckpoint {\n return {\n attempt: toolExecution.attempt,\n idempotencyKey: `${toolExecution.runId}:${toolCallId}`,\n input,\n policy,\n toolCallId,\n toolName,\n };\n}\n\nfunction toolRetryPolicy(toolDefinition: unknown): RuntimeToolRetryPolicy {\n if (\n typeof toolDefinition === \"object\" &&\n toolDefinition !== null &&\n \"retryPolicy\" in toolDefinition &&\n isToolRetryPolicy(toolDefinition.retryPolicy)\n ) {\n return toolDefinition.retryPolicy;\n }\n\n return \"manual-recovery\";\n}\n\nfunction isToolRetryPolicy(value: unknown): value is RuntimeToolRetryPolicy {\n return (\n value === \"idempotent\" || value === \"manual-recovery\" || value === \"pure\"\n );\n}\n\nfunction rewriteAssistantToolCallIds(\n message: AssistantModelMessage,\n toolCallIds: Map<string, string>\n): AssistantModelMessage {\n if (typeof message.content === \"string\") {\n return message;\n }\n\n return {\n ...message,\n content: message.content.map((part) =>\n \"toolCallId\" in part\n ? {\n ...part,\n toolCallId: publicToolCallId(part.toolCallId, toolCallIds),\n }\n : part\n ),\n };\n}\n\nfunction rewriteToolResultCallIds(\n message: ToolModelMessage,\n toolCallIds: Map<string, string>\n): ToolModelMessage {\n return {\n ...message,\n content: message.content.map((part) =>\n \"toolCallId\" in part\n ? {\n ...part,\n toolCallId: publicToolCallId(part.toolCallId, toolCallIds),\n }\n : part\n ),\n };\n}\n\nfunction publicToolCallId(\n toolCallId: string,\n toolCallIds: Map<string, string>\n): string {\n if (publicToolCallIdPattern.test(toolCallId)) {\n return toolCallId;\n }\n\n const existing = toolCallIds.get(toolCallId);\n if (existing) {\n return existing;\n }\n\n const generated = createToolCallId();\n toolCallIds.set(toolCallId, generated);\n return generated;\n}\n\nfunction createToolCallId(): string {\n return `${toolCallIdPrefix}${crypto.randomUUID().replaceAll(\"-\", \"\")}`;\n}\n"],"mappings":"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"}
|
package/dist/llm.d.ts
CHANGED
|
@@ -3,26 +3,6 @@ import { LanguageModel, ModelMessage, ToolChoice, ToolSet, generateText } from "
|
|
|
3
3
|
|
|
4
4
|
//#region src/llm.d.ts
|
|
5
5
|
type AgentToolChoice = ToolChoice<ToolSet>;
|
|
6
|
-
type RuntimeLlmOutput = Awaited<ReturnType<typeof generateText>>["responseMessages"];
|
|
7
|
-
type RuntimeLlmOutputPart = RuntimeLlmOutput[number];
|
|
8
|
-
interface RuntimeLlmContext {
|
|
9
|
-
history: readonly ModelMessage[];
|
|
10
|
-
signal: AbortSignal;
|
|
11
|
-
toolExecution?: RuntimeToolExecutionContext;
|
|
12
|
-
}
|
|
13
|
-
type RuntimeLlm = (context: RuntimeLlmContext) => Promise<RuntimeLlmOutput>;
|
|
14
|
-
interface RuntimeCreateLlmOptions {
|
|
15
|
-
instructions?: string;
|
|
16
|
-
model: LanguageModel;
|
|
17
|
-
toolChoice?: AgentToolChoice;
|
|
18
|
-
tools?: ToolSet;
|
|
19
|
-
}
|
|
20
|
-
declare function createLlm({
|
|
21
|
-
model,
|
|
22
|
-
instructions,
|
|
23
|
-
toolChoice,
|
|
24
|
-
tools
|
|
25
|
-
}: RuntimeCreateLlmOptions): RuntimeLlm;
|
|
26
6
|
//#endregion
|
|
27
|
-
export { AgentToolChoice
|
|
7
|
+
export { AgentToolChoice };
|
|
28
8
|
//# sourceMappingURL=llm.d.ts.map
|
package/dist/llm.js
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
import { normalizeToolCallIds, rewriteMessageToolCallIds } from "./llm-tool-execution.js";
|
|
2
2
|
import { generateText } from "ai";
|
|
3
3
|
//#region src/llm.ts
|
|
4
|
-
function
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return responseMessages.map((message) => rewriteMessageToolCallIds(message, toolCallIds));
|
|
16
|
-
};
|
|
4
|
+
async function generateModelStep({ history, model, instructions, signal, toolChoice, toolExecution, tools }) {
|
|
5
|
+
const toolCallIds = /* @__PURE__ */ new Map();
|
|
6
|
+
const { responseMessages } = await generateText({
|
|
7
|
+
abortSignal: signal,
|
|
8
|
+
instructions,
|
|
9
|
+
messages: [...history],
|
|
10
|
+
model,
|
|
11
|
+
toolChoice,
|
|
12
|
+
tools: normalizeToolCallIds(tools, toolCallIds, toolExecution)
|
|
13
|
+
});
|
|
14
|
+
return responseMessages.map((message) => rewriteMessageToolCallIds(message, toolCallIds));
|
|
17
15
|
}
|
|
18
16
|
//#endregion
|
|
19
|
-
export {
|
|
17
|
+
export { generateModelStep };
|
|
20
18
|
|
|
21
19
|
//# sourceMappingURL=llm.js.map
|
package/dist/llm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm.js","names":[],"sources":["../src/llm.ts"],"sourcesContent":["import type { LanguageModel, ModelMessage, ToolChoice, ToolSet } from \"ai\";\nimport { generateText } from \"ai\";\nimport type { RuntimeToolExecutionContext } from \"./llm-tool-execution\";\nimport {\n normalizeToolCallIds,\n rewriteMessageToolCallIds,\n} from \"./llm-tool-execution\";\n\nexport type {\n RuntimePersistedToolExecutionCheckpoint,\n RuntimeToolExecutionCheckpoint,\n RuntimeToolExecutionCheckpointMetadata,\n RuntimeToolExecutionContext,\n RuntimeToolExecutionDecision,\n RuntimeToolRetryPolicy,\n} from \"./llm-tool-execution\";\n\nexport type AgentToolChoice = ToolChoice<ToolSet>;\nexport type
|
|
1
|
+
{"version":3,"file":"llm.js","names":[],"sources":["../src/llm.ts"],"sourcesContent":["import type { LanguageModel, ModelMessage, ToolChoice, ToolSet } from \"ai\";\nimport { generateText } from \"ai\";\nimport type { RuntimeToolExecutionContext } from \"./llm-tool-execution\";\nimport {\n normalizeToolCallIds,\n rewriteMessageToolCallIds,\n} from \"./llm-tool-execution\";\n\nexport type {\n RuntimePersistedToolExecutionCheckpoint,\n RuntimeToolExecutionCheckpoint,\n RuntimeToolExecutionCheckpointMetadata,\n RuntimeToolExecutionContext,\n RuntimeToolExecutionDecision,\n RuntimeToolRetryPolicy,\n} from \"./llm-tool-execution\";\n\nexport type AgentToolChoice = ToolChoice<ToolSet>;\nexport type ModelStepOutput = Awaited<\n ReturnType<typeof generateText>\n>[\"responseMessages\"];\nexport type ModelStepOutputPart = ModelStepOutput[number];\n\nexport interface ModelGenerationOptions {\n instructions?: string;\n model: LanguageModel;\n toolChoice?: AgentToolChoice;\n tools?: ToolSet;\n}\n\nexport interface ModelStepOptions extends ModelGenerationOptions {\n history: readonly ModelMessage[];\n signal: AbortSignal;\n toolExecution?: RuntimeToolExecutionContext;\n}\n\nexport async function generateModelStep({\n history,\n model,\n instructions,\n signal,\n toolChoice,\n toolExecution,\n tools,\n}: ModelStepOptions): Promise<ModelStepOutput> {\n const toolCallIds = new Map<string, string>();\n const { responseMessages } = await generateText({\n abortSignal: signal,\n instructions,\n messages: [...history],\n model,\n toolChoice,\n tools: normalizeToolCallIds(tools, toolCallIds, toolExecution),\n });\n\n return responseMessages.map((message) =>\n rewriteMessageToolCallIds(message, toolCallIds)\n );\n}\n"],"mappings":";;;AAoCA,eAAsB,kBAAkB,EACtC,SACA,OACA,cACA,QACA,YACA,eACA,SAC6C;CAC7C,MAAM,8BAAc,IAAI,IAAoB;CAC5C,MAAM,EAAE,qBAAqB,MAAM,aAAa;EAC9C,aAAa;EACb;EACA,UAAU,CAAC,GAAG,OAAO;EACrB;EACA;EACA,OAAO,qBAAqB,OAAO,aAAa,aAAa;CAC/D,CAAC;CAED,OAAO,iBAAiB,KAAK,YAC3B,0BAA0B,SAAS,WAAW,CAChD;AACF"}
|
package/dist/plugins.d.ts
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { InputEventMeta, InputSource } from "./session/input-meta-types.js";
|
|
2
|
+
import { UserMessage, UserText } from "./session/input.js";
|
|
3
|
+
import { AgentEvent, RuntimeInput } from "./session/events.js";
|
|
4
|
+
import { ModelMessage } from "ai";
|
|
3
5
|
|
|
4
6
|
//#region src/plugins.d.ts
|
|
5
7
|
type MaybePromise<T> = PromiseLike<T> | T;
|
|
6
|
-
type AgentEventHistory =
|
|
8
|
+
type AgentEventHistory = readonly ModelMessage[];
|
|
9
|
+
type InterceptableAgentEvent = RuntimeInput | UserMessage | UserText;
|
|
10
|
+
type AgentPluginInterceptResult = {
|
|
11
|
+
readonly action: "continue";
|
|
12
|
+
} | {
|
|
13
|
+
readonly action: "handled";
|
|
14
|
+
} | {
|
|
15
|
+
readonly action: "transform";
|
|
16
|
+
readonly event: InterceptableAgentEvent;
|
|
17
|
+
};
|
|
18
|
+
type AgentPluginResult = AgentPluginInterceptResult | undefined;
|
|
7
19
|
interface AgentEventContext {
|
|
8
20
|
readonly event: AgentEvent;
|
|
9
21
|
readonly history: AgentEventHistory;
|
|
@@ -11,10 +23,20 @@ interface AgentEventContext {
|
|
|
11
23
|
}
|
|
12
24
|
interface AgentPlugin {
|
|
13
25
|
readonly events?: {
|
|
14
|
-
readonly on?: (context: AgentEventContext) => MaybePromise<void>;
|
|
26
|
+
/** @deprecated Use top-level `on`. */readonly on?: (context: AgentEventContext) => MaybePromise<void>;
|
|
15
27
|
};
|
|
16
28
|
readonly name?: string;
|
|
29
|
+
readonly on?: (context: AgentEventContext) => MaybePromise<AgentPluginResult>;
|
|
17
30
|
}
|
|
31
|
+
type PluginPipelineResult = {
|
|
32
|
+
readonly event: AgentEvent;
|
|
33
|
+
readonly kind: "emit";
|
|
34
|
+
} | {
|
|
35
|
+
readonly kind: "handled";
|
|
36
|
+
};
|
|
37
|
+
declare function runPluginsForEvent(plugins: readonly AgentPlugin[], context: AgentEventContext, options?: {
|
|
38
|
+
readonly observeOnly?: boolean;
|
|
39
|
+
}): Promise<PluginPipelineResult>;
|
|
18
40
|
//#endregion
|
|
19
|
-
export { AgentEventContext, AgentPlugin };
|
|
41
|
+
export { AgentEventContext, AgentPlugin, AgentPluginInterceptResult, AgentPluginResult, InterceptableAgentEvent, PluginPipelineResult, runPluginsForEvent };
|
|
20
42
|
//# sourceMappingURL=plugins.d.ts.map
|
package/dist/plugins.js
CHANGED
|
@@ -1,14 +1,43 @@
|
|
|
1
1
|
//#region src/plugins.ts
|
|
2
|
-
function
|
|
3
|
-
return
|
|
2
|
+
function runPluginsForEvent(plugins, context, options = {}) {
|
|
3
|
+
return runPluginPipeline(plugins, context, options.observeOnly === true);
|
|
4
4
|
}
|
|
5
|
-
|
|
5
|
+
function isInterceptableEvent(event) {
|
|
6
|
+
return event.type === "user-text" || event.type === "user-message" || event.type === "runtime-input";
|
|
7
|
+
}
|
|
8
|
+
function resolvePluginHandler(plugin) {
|
|
9
|
+
if (plugin.on) return plugin.on;
|
|
10
|
+
const legacyHandler = plugin.events?.on;
|
|
11
|
+
if (!legacyHandler) return;
|
|
12
|
+
return (legacyContext) => Promise.resolve(legacyHandler(legacyContext)).then(() => void 0);
|
|
13
|
+
}
|
|
14
|
+
function normalizeInterceptResult(result) {
|
|
15
|
+
if (result === void 0) return;
|
|
16
|
+
if (result.action === "continue") return result;
|
|
17
|
+
if (result.action === "handled") return result;
|
|
18
|
+
if (result.action === "transform") return result;
|
|
19
|
+
}
|
|
20
|
+
async function runPluginPipeline(plugins, context, observeOnly) {
|
|
21
|
+
let currentEvent = context.event;
|
|
6
22
|
for (const plugin of plugins) {
|
|
7
|
-
const handler =
|
|
8
|
-
if (handler)
|
|
23
|
+
const handler = resolvePluginHandler(plugin);
|
|
24
|
+
if (!handler) continue;
|
|
25
|
+
const result = await handler({
|
|
26
|
+
...context,
|
|
27
|
+
event: currentEvent
|
|
28
|
+
});
|
|
29
|
+
if (observeOnly || !isInterceptableEvent(currentEvent)) continue;
|
|
30
|
+
const intercept = normalizeInterceptResult(result);
|
|
31
|
+
if (!intercept || intercept.action === "continue") continue;
|
|
32
|
+
if (intercept.action === "handled") return { kind: "handled" };
|
|
33
|
+
currentEvent = intercept.event;
|
|
9
34
|
}
|
|
35
|
+
return {
|
|
36
|
+
kind: "emit",
|
|
37
|
+
event: currentEvent
|
|
38
|
+
};
|
|
10
39
|
}
|
|
11
40
|
//#endregion
|
|
12
|
-
export {
|
|
41
|
+
export { runPluginsForEvent };
|
|
13
42
|
|
|
14
43
|
//# sourceMappingURL=plugins.js.map
|
package/dist/plugins.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugins.js","names":[],"sources":["../src/plugins.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"plugins.js","names":[],"sources":["../src/plugins.ts"],"sourcesContent":["import type { ModelMessage } from \"ai\";\nimport type {\n AgentEvent,\n RuntimeInput,\n UserMessage,\n UserText,\n} from \"./session/events\";\n\nexport type { InputEventMeta, InputSource } from \"./session/input-meta-types\";\n\ntype MaybePromise<T> = PromiseLike<T> | T;\ntype AgentEventHistory = readonly ModelMessage[];\n\nexport type InterceptableAgentEvent = RuntimeInput | UserMessage | UserText;\n\nexport type AgentPluginInterceptResult =\n | { readonly action: \"continue\" }\n | { readonly action: \"handled\" }\n | { readonly action: \"transform\"; readonly event: InterceptableAgentEvent };\n\nexport type AgentPluginResult = AgentPluginInterceptResult | undefined;\n\nexport interface AgentEventContext {\n readonly event: AgentEvent;\n readonly history: AgentEventHistory;\n readonly signal?: AbortSignal;\n}\n\nexport interface AgentPlugin {\n readonly events?: {\n /** @deprecated Use top-level `on`. */\n readonly on?: (context: AgentEventContext) => MaybePromise<void>;\n };\n readonly name?: string;\n readonly on?: (context: AgentEventContext) => MaybePromise<AgentPluginResult>;\n}\n\nexport type PluginPipelineResult =\n | { readonly event: AgentEvent; readonly kind: \"emit\" }\n | { readonly kind: \"handled\" };\n\nexport function runEventPlugins(\n plugins: readonly AgentPlugin[],\n context: AgentEventContext\n): Promise<void> {\n return runPluginsForEvent(plugins, context, { observeOnly: true }).then(\n () => undefined\n );\n}\n\nexport function runPluginsForEvent(\n plugins: readonly AgentPlugin[],\n context: AgentEventContext,\n options: { readonly observeOnly?: boolean } = {}\n): Promise<PluginPipelineResult> {\n return runPluginPipeline(plugins, context, options.observeOnly === true);\n}\n\nfunction isInterceptableEvent(\n event: AgentEvent\n): event is InterceptableAgentEvent {\n return (\n event.type === \"user-text\" ||\n event.type === \"user-message\" ||\n event.type === \"runtime-input\"\n );\n}\n\nfunction resolvePluginHandler(\n plugin: AgentPlugin\n):\n | ((context: AgentEventContext) => MaybePromise<AgentPluginResult>)\n | undefined {\n if (plugin.on) {\n return plugin.on;\n }\n\n const legacyHandler = plugin.events?.on;\n if (!legacyHandler) {\n return;\n }\n\n return (legacyContext) =>\n Promise.resolve(legacyHandler(legacyContext)).then(() => undefined);\n}\n\nfunction normalizeInterceptResult(\n result: AgentPluginResult | undefined\n): AgentPluginInterceptResult | undefined {\n if (result === undefined) {\n return;\n }\n\n if (result.action === \"continue\") {\n return result;\n }\n\n if (result.action === \"handled\") {\n return result;\n }\n\n if (result.action === \"transform\") {\n return result;\n }\n\n return;\n}\n\nasync function runPluginPipeline(\n plugins: readonly AgentPlugin[],\n context: AgentEventContext,\n observeOnly: boolean\n): Promise<PluginPipelineResult> {\n let currentEvent = context.event;\n\n for (const plugin of plugins) {\n const handler = resolvePluginHandler(plugin);\n if (!handler) {\n continue;\n }\n\n const result = await handler({ ...context, event: currentEvent });\n if (observeOnly || !isInterceptableEvent(currentEvent)) {\n continue;\n }\n\n const intercept = normalizeInterceptResult(result);\n if (!intercept || intercept.action === \"continue\") {\n continue;\n }\n\n if (intercept.action === \"handled\") {\n return { kind: \"handled\" };\n }\n\n currentEvent = intercept.event;\n }\n\n return { kind: \"emit\", event: currentEvent };\n}\n"],"mappings":";AAkDA,SAAgB,mBACd,SACA,SACA,UAA8C,CAAC,GAChB;CAC/B,OAAO,kBAAkB,SAAS,SAAS,QAAQ,gBAAgB,IAAI;AACzE;AAEA,SAAS,qBACP,OACkC;CAClC,OACE,MAAM,SAAS,eACf,MAAM,SAAS,kBACf,MAAM,SAAS;AAEnB;AAEA,SAAS,qBACP,QAGY;CACZ,IAAI,OAAO,IACT,OAAO,OAAO;CAGhB,MAAM,gBAAgB,OAAO,QAAQ;CACrC,IAAI,CAAC,eACH;CAGF,QAAQ,kBACN,QAAQ,QAAQ,cAAc,aAAa,CAAC,EAAE,WAAW,KAAA,CAAS;AACtE;AAEA,SAAS,yBACP,QACwC;CACxC,IAAI,WAAW,KAAA,GACb;CAGF,IAAI,OAAO,WAAW,YACpB,OAAO;CAGT,IAAI,OAAO,WAAW,WACpB,OAAO;CAGT,IAAI,OAAO,WAAW,aACpB,OAAO;AAIX;AAEA,eAAe,kBACb,SACA,SACA,aAC+B;CAC/B,IAAI,eAAe,QAAQ;CAE3B,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,UAAU,qBAAqB,MAAM;EAC3C,IAAI,CAAC,SACH;EAGF,MAAM,SAAS,MAAM,QAAQ;GAAE,GAAG;GAAS,OAAO;EAAa,CAAC;EAChE,IAAI,eAAe,CAAC,qBAAqB,YAAY,GACnD;EAGF,MAAM,YAAY,yBAAyB,MAAM;EACjD,IAAI,CAAC,aAAa,UAAU,WAAW,YACrC;EAGF,IAAI,UAAU,WAAW,WACvB,OAAO,EAAE,MAAM,UAAU;EAG3B,eAAe,UAAU;CAC3B;CAEA,OAAO;EAAE,MAAM;EAAQ,OAAO;CAAa;AAC7C"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { UserInput } from "./input.js";
|
|
2
|
+
|
|
3
|
+
//#region src/session/delegate-input.d.ts
|
|
4
|
+
declare function delegateUserInput(prompt: unknown, options?: {
|
|
5
|
+
readonly delegateToolName?: string;
|
|
6
|
+
}): UserInput;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { delegateUserInput };
|
|
9
|
+
//# sourceMappingURL=delegate-input.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { attachInputMeta } from "./input-meta.js";
|
|
2
|
+
//#region src/session/delegate-input.ts
|
|
3
|
+
function delegateUserInput(prompt, options = {}) {
|
|
4
|
+
if (typeof prompt !== "string") throw new TypeError("Delegate prompt must be a plain string.");
|
|
5
|
+
return attachInputMeta({
|
|
6
|
+
type: "user-text",
|
|
7
|
+
text: prompt
|
|
8
|
+
}, {
|
|
9
|
+
delegateToolName: options.delegateToolName,
|
|
10
|
+
source: "delegate"
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { delegateUserInput };
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=delegate-input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delegate-input.js","names":[],"sources":["../../src/session/delegate-input.ts"],"sourcesContent":["import type { UserInput } from \"./input\";\nimport { attachInputMeta } from \"./input-meta\";\n\nexport function delegateUserInput(\n prompt: unknown,\n options: { readonly delegateToolName?: string } = {}\n): UserInput {\n if (typeof prompt !== \"string\") {\n throw new TypeError(\"Delegate prompt must be a plain string.\");\n }\n\n return attachInputMeta(\n {\n type: \"user-text\",\n text: prompt,\n },\n {\n delegateToolName: options.delegateToolName,\n source: \"delegate\",\n }\n );\n}\n"],"mappings":";;AAGA,SAAgB,kBACd,QACA,UAAkD,CAAC,GACxC;CACX,IAAI,OAAO,WAAW,UACpB,MAAM,IAAI,UAAU,yCAAyC;CAG/D,OAAO,gBACL;EACE,MAAM;EACN,MAAM;CACR,GACA;EACE,kBAAkB,QAAQ;EAC1B,QAAQ;CACV,CACF;AACF"}
|
package/dist/session/events.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { InputEventMeta, InputSource } from "./input-meta-types.js";
|
|
1
2
|
import { UserInput, UserMessage, UserMessageContent, UserMessageContentPart, UserMessageFileData, UserMessageFilePart, UserMessageImagePart, UserMessageTextPart, UserText, UserTextContent } from "./input.js";
|
|
2
3
|
|
|
3
4
|
//#region src/session/events.d.ts
|
|
@@ -7,6 +8,7 @@ interface RuntimeInput {
|
|
|
7
8
|
* This is distinct from human-originated user-text and user-message input.
|
|
8
9
|
*/
|
|
9
10
|
input: UserInput;
|
|
11
|
+
meta?: InputEventMeta;
|
|
10
12
|
placement: "turn-start" | "step-start" | "step-end";
|
|
11
13
|
type: "runtime-input";
|
|
12
14
|
}
|
|
@@ -30,7 +32,6 @@ interface ToolResult {
|
|
|
30
32
|
toolName: string;
|
|
31
33
|
type: "tool-result";
|
|
32
34
|
}
|
|
33
|
-
type SubagentJobStatus = "aborted" | "cancelled" | "completed" | "error" | "pending" | "running";
|
|
34
35
|
type AgentEvent = /** User input was accepted into the session queue. */UserText /** User multipart input was accepted into the session queue. */ | UserMessage /** Runtime/API-originated input inserted into the current turn, not human input. */ | RuntimeInput /** A queued user input started running as a turn. */ | {
|
|
35
36
|
type: "turn-start";
|
|
36
37
|
} /** The active turn was interrupted before normal completion. */ | {
|
|
@@ -42,32 +43,49 @@ type AgentEvent = /** User input was accepted into the session queue. */UserText
|
|
|
42
43
|
type: "turn-end";
|
|
43
44
|
} /** One model/tool-loop iteration started within the active turn. */ | {
|
|
44
45
|
type: "step-start";
|
|
45
|
-
} /** The model produced reasoning content. */ | AssistantReasoning /** The model produced visible assistant text. */ | AssistantText /** The model requested a tool call. */ | ToolCall /** A tool call returned a result. */ | ToolResult | {
|
|
46
|
-
description?: string;
|
|
47
|
-
delegateToolCallId?: string;
|
|
48
|
-
run_in_background: boolean;
|
|
49
|
-
subagent: string;
|
|
50
|
-
task_id?: string;
|
|
51
|
-
type: "subagent-job-start";
|
|
52
|
-
} | {
|
|
53
|
-
eventType?: AgentEvent["type"];
|
|
54
|
-
delegateToolCallId?: string;
|
|
55
|
-
status: SubagentJobStatus;
|
|
56
|
-
subagent: string;
|
|
57
|
-
task_id: string;
|
|
58
|
-
type: "subagent-job-update";
|
|
59
|
-
} | {
|
|
60
|
-
error?: string;
|
|
61
|
-
eventCount: number;
|
|
62
|
-
delegateToolCallId?: string;
|
|
63
|
-
status: Exclude<SubagentJobStatus, "pending" | "running">;
|
|
64
|
-
subagent: string;
|
|
65
|
-
task_id?: string;
|
|
66
|
-
type: "subagent-job-end";
|
|
67
|
-
} /** One model/tool-loop iteration finished within the active turn. */ | {
|
|
46
|
+
} /** The model produced reasoning content. */ | AssistantReasoning /** The model produced visible assistant text. */ | AssistantText /** The model requested a tool call. */ | ToolCall /** A tool call returned a result. */ | ToolResult /** One model/tool-loop iteration finished within the active turn. */ | {
|
|
68
47
|
type: "step-end";
|
|
69
48
|
};
|
|
70
49
|
type AgentEventListener = (event: AgentEvent) => void;
|
|
50
|
+
declare const visibleAgentEventTypes: {
|
|
51
|
+
"assistant-text": true;
|
|
52
|
+
"user-message": true;
|
|
53
|
+
"user-text": true;
|
|
54
|
+
};
|
|
55
|
+
declare const lifecycleAgentEventTypes: {
|
|
56
|
+
"step-end": true;
|
|
57
|
+
"step-start": true;
|
|
58
|
+
"turn-abort": true;
|
|
59
|
+
"turn-end": true;
|
|
60
|
+
"turn-error": true;
|
|
61
|
+
"turn-start": true;
|
|
62
|
+
};
|
|
63
|
+
declare const toolAgentEventTypes: {
|
|
64
|
+
"tool-call": true;
|
|
65
|
+
"tool-result": true;
|
|
66
|
+
};
|
|
67
|
+
declare const telemetryAgentEventTypes: {
|
|
68
|
+
"assistant-reasoning": true;
|
|
69
|
+
"runtime-input": true;
|
|
70
|
+
};
|
|
71
|
+
type VisibleAgentEvent = Extract<AgentEvent, {
|
|
72
|
+
type: keyof typeof visibleAgentEventTypes;
|
|
73
|
+
}>;
|
|
74
|
+
type LifecycleAgentEvent = Extract<AgentEvent, {
|
|
75
|
+
type: keyof typeof lifecycleAgentEventTypes;
|
|
76
|
+
}>;
|
|
77
|
+
type ToolAgentEvent = Extract<AgentEvent, {
|
|
78
|
+
type: keyof typeof toolAgentEventTypes;
|
|
79
|
+
}>;
|
|
80
|
+
type TelemetryAgentEvent = Extract<AgentEvent, {
|
|
81
|
+
type: keyof typeof telemetryAgentEventTypes;
|
|
82
|
+
}>;
|
|
83
|
+
type ControlAgentEvent = Exclude<AgentEvent, VisibleAgentEvent>;
|
|
84
|
+
declare function isVisibleAgentEvent(event: AgentEvent): event is VisibleAgentEvent;
|
|
85
|
+
declare function isLifecycleAgentEvent(event: AgentEvent): event is LifecycleAgentEvent;
|
|
86
|
+
declare function isToolAgentEvent(event: AgentEvent): event is ToolAgentEvent;
|
|
87
|
+
declare function isTelemetryAgentEvent(event: AgentEvent): event is TelemetryAgentEvent;
|
|
88
|
+
declare function isControlAgentEvent(event: AgentEvent): event is ControlAgentEvent;
|
|
71
89
|
//#endregion
|
|
72
|
-
export { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, RuntimeInput, ToolCall, ToolResult };
|
|
90
|
+
export { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, ControlAgentEvent, LifecycleAgentEvent, RuntimeInput, TelemetryAgentEvent, ToolAgentEvent, ToolCall, ToolResult, VisibleAgentEvent, isControlAgentEvent, isLifecycleAgentEvent, isTelemetryAgentEvent, isToolAgentEvent, isVisibleAgentEvent };
|
|
73
91
|
//# sourceMappingURL=events.d.ts.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//#region src/session/events.ts
|
|
2
|
+
const visibleAgentEventTypes = {
|
|
3
|
+
"assistant-text": true,
|
|
4
|
+
"user-message": true,
|
|
5
|
+
"user-text": true
|
|
6
|
+
};
|
|
7
|
+
const lifecycleAgentEventTypes = {
|
|
8
|
+
"step-end": true,
|
|
9
|
+
"step-start": true,
|
|
10
|
+
"turn-abort": true,
|
|
11
|
+
"turn-end": true,
|
|
12
|
+
"turn-error": true,
|
|
13
|
+
"turn-start": true
|
|
14
|
+
};
|
|
15
|
+
const toolAgentEventTypes = {
|
|
16
|
+
"tool-call": true,
|
|
17
|
+
"tool-result": true
|
|
18
|
+
};
|
|
19
|
+
const telemetryAgentEventTypes = {
|
|
20
|
+
"assistant-reasoning": true,
|
|
21
|
+
"runtime-input": true
|
|
22
|
+
};
|
|
23
|
+
function isVisibleAgentEvent(event) {
|
|
24
|
+
return event.type in visibleAgentEventTypes;
|
|
25
|
+
}
|
|
26
|
+
function isLifecycleAgentEvent(event) {
|
|
27
|
+
return event.type in lifecycleAgentEventTypes;
|
|
28
|
+
}
|
|
29
|
+
function isToolAgentEvent(event) {
|
|
30
|
+
return event.type in toolAgentEventTypes;
|
|
31
|
+
}
|
|
32
|
+
function isTelemetryAgentEvent(event) {
|
|
33
|
+
return event.type in telemetryAgentEventTypes;
|
|
34
|
+
}
|
|
35
|
+
function isControlAgentEvent(event) {
|
|
36
|
+
return !isVisibleAgentEvent(event);
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { isControlAgentEvent, isLifecycleAgentEvent, isTelemetryAgentEvent, isToolAgentEvent, isVisibleAgentEvent };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","names":[],"sources":["../../src/session/events.ts"],"sourcesContent":["import type { UserInput, UserMessage, UserText } from \"./input\";\nimport type { InputEventMeta } from \"./input-meta-types\";\n\nexport type {\n UserInput,\n UserMessage,\n UserMessageContent,\n UserMessageContentPart,\n UserMessageFileData,\n UserMessageFilePart,\n UserMessageImagePart,\n UserMessageTextPart,\n UserText,\n UserTextContent,\n} from \"./input\";\nexport type { InputEventMeta, InputSource } from \"./input-meta-types\";\n\nexport interface RuntimeInput {\n /**\n * Runtime/API-originated model input inserted into the current turn.\n * This is distinct from human-originated user-text and user-message input.\n */\n input: UserInput;\n meta?: InputEventMeta;\n placement: \"turn-start\" | \"step-start\" | \"step-end\";\n type: \"runtime-input\";\n}\n\nexport interface AssistantText {\n text: string;\n type: \"assistant-text\";\n}\nexport interface AssistantReasoning {\n text: string;\n type: \"assistant-reasoning\";\n}\nexport interface ToolCall {\n input: unknown;\n toolCallId: string;\n toolName: string;\n type: \"tool-call\";\n}\nexport interface ToolResult {\n output: unknown;\n toolCallId: string;\n toolName: string;\n type: \"tool-result\";\n}\n\nexport type AgentEvent =\n /** User input was accepted into the session queue. */\n | UserText\n /** User multipart input was accepted into the session queue. */\n | UserMessage\n /** Runtime/API-originated input inserted into the current turn, not human input. */\n | RuntimeInput\n /** A queued user input started running as a turn. */\n | { type: \"turn-start\" }\n /** The active turn was interrupted before normal completion. */\n | { type: \"turn-abort\" }\n /** The active turn hit an unrecoverable runtime failure. */\n | { type: \"turn-error\"; message: string }\n /** The active turn completed normally. */\n | { type: \"turn-end\" }\n /** One model/tool-loop iteration started within the active turn. */\n | { type: \"step-start\" }\n /** The model produced reasoning content. */\n | AssistantReasoning\n /** The model produced visible assistant text. */\n | AssistantText\n /** The model requested a tool call. */\n | ToolCall\n /** A tool call returned a result. */\n | ToolResult\n /** One model/tool-loop iteration finished within the active turn. */\n | { type: \"step-end\" };\n\nexport type AgentEventListener = (event: AgentEvent) => void;\n\nconst visibleAgentEventTypes = {\n \"assistant-text\": true,\n \"user-message\": true,\n \"user-text\": true,\n} satisfies Partial<Record<AgentEvent[\"type\"], true>>;\n\nconst lifecycleAgentEventTypes = {\n \"step-end\": true,\n \"step-start\": true,\n \"turn-abort\": true,\n \"turn-end\": true,\n \"turn-error\": true,\n \"turn-start\": true,\n} satisfies Partial<Record<AgentEvent[\"type\"], true>>;\n\nconst toolAgentEventTypes = {\n \"tool-call\": true,\n \"tool-result\": true,\n} satisfies Partial<Record<AgentEvent[\"type\"], true>>;\n\nconst telemetryAgentEventTypes = {\n \"assistant-reasoning\": true,\n \"runtime-input\": true,\n} satisfies Partial<Record<AgentEvent[\"type\"], true>>;\n\nexport type VisibleAgentEvent = Extract<\n AgentEvent,\n { type: keyof typeof visibleAgentEventTypes }\n>;\nexport type LifecycleAgentEvent = Extract<\n AgentEvent,\n { type: keyof typeof lifecycleAgentEventTypes }\n>;\nexport type ToolAgentEvent = Extract<\n AgentEvent,\n { type: keyof typeof toolAgentEventTypes }\n>;\nexport type TelemetryAgentEvent = Extract<\n AgentEvent,\n { type: keyof typeof telemetryAgentEventTypes }\n>;\nexport type ControlAgentEvent = Exclude<AgentEvent, VisibleAgentEvent>;\n\nexport function isVisibleAgentEvent(\n event: AgentEvent\n): event is VisibleAgentEvent {\n return event.type in visibleAgentEventTypes;\n}\n\nexport function isLifecycleAgentEvent(\n event: AgentEvent\n): event is LifecycleAgentEvent {\n return event.type in lifecycleAgentEventTypes;\n}\n\nexport function isToolAgentEvent(event: AgentEvent): event is ToolAgentEvent {\n return event.type in toolAgentEventTypes;\n}\n\nexport function isTelemetryAgentEvent(\n event: AgentEvent\n): event is TelemetryAgentEvent {\n return event.type in telemetryAgentEventTypes;\n}\n\nexport function isControlAgentEvent(\n event: AgentEvent\n): event is ControlAgentEvent {\n return !isVisibleAgentEvent(event);\n}\n"],"mappings":";AA+EA,MAAM,yBAAyB;CAC7B,kBAAkB;CAClB,gBAAgB;CAChB,aAAa;AACf;AAEA,MAAM,2BAA2B;CAC/B,YAAY;CACZ,cAAc;CACd,cAAc;CACd,YAAY;CACZ,cAAc;CACd,cAAc;AAChB;AAEA,MAAM,sBAAsB;CAC1B,aAAa;CACb,eAAe;AACjB;AAEA,MAAM,2BAA2B;CAC/B,uBAAuB;CACvB,iBAAiB;AACnB;AAoBA,SAAgB,oBACd,OAC4B;CAC5B,OAAO,MAAM,QAAQ;AACvB;AAEA,SAAgB,sBACd,OAC8B;CAC9B,OAAO,MAAM,QAAQ;AACvB;AAEA,SAAgB,iBAAiB,OAA4C;CAC3E,OAAO,MAAM,QAAQ;AACvB;AAEA,SAAgB,sBACd,OAC8B;CAC9B,OAAO,MAAM,QAAQ;AACvB;AAEA,SAAgB,oBACd,OAC4B;CAC5B,OAAO,CAAC,oBAAoB,KAAK;AACnC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/session/input-meta-types.d.ts
|
|
2
|
+
type InputSource = "delegate" | "notify" | "send" | "steer";
|
|
3
|
+
interface InputEventMeta {
|
|
4
|
+
readonly delegateToolName?: string;
|
|
5
|
+
readonly source: InputSource;
|
|
6
|
+
readonly streaming?: "follow-up" | "steer";
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { InputEventMeta, InputSource };
|
|
10
|
+
//# sourceMappingURL=input-meta-types.d.ts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { InputEventMeta, InputSource } from "./input-meta-types.js";
|
|
2
|
+
import { UserInput, UserMessage, UserText } from "./input.js";
|
|
3
|
+
import { AgentEvent, RuntimeInput } from "./events.js";
|
|
4
|
+
|
|
5
|
+
//#region src/session/input-meta.d.ts
|
|
6
|
+
declare function attachInputMeta(input: UserInput, meta: InputEventMeta): UserText | UserMessage;
|
|
7
|
+
declare function attachRuntimeInputMeta(input: UserInput, placement: RuntimeInput["placement"], meta: InputEventMeta): RuntimeInput;
|
|
8
|
+
declare function stripInputMeta(input: UserInput): UserInput;
|
|
9
|
+
declare function stripEventMeta(event: AgentEvent): AgentEvent;
|
|
10
|
+
declare function userInputFromEvent(event: UserText | UserMessage): UserInput;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { attachInputMeta, attachRuntimeInputMeta, stripEventMeta, stripInputMeta, userInputFromEvent };
|
|
13
|
+
//# sourceMappingURL=input-meta.d.ts.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region src/session/input-meta.ts
|
|
2
|
+
function attachInputMeta(input, meta) {
|
|
3
|
+
if (input.type === "user-text") return {
|
|
4
|
+
...input,
|
|
5
|
+
meta
|
|
6
|
+
};
|
|
7
|
+
return {
|
|
8
|
+
...input,
|
|
9
|
+
meta
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function attachRuntimeInputMeta(input, placement, meta) {
|
|
13
|
+
return {
|
|
14
|
+
input: attachInputMeta(input, meta),
|
|
15
|
+
meta,
|
|
16
|
+
placement,
|
|
17
|
+
type: "runtime-input"
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function stripInputMeta(input) {
|
|
21
|
+
if (input.type === "user-text") {
|
|
22
|
+
const { meta: _meta, ...rest } = input;
|
|
23
|
+
return rest;
|
|
24
|
+
}
|
|
25
|
+
const { meta: _meta, ...rest } = input;
|
|
26
|
+
return rest;
|
|
27
|
+
}
|
|
28
|
+
function stripEventMeta(event) {
|
|
29
|
+
if (event.type === "user-text" || event.type === "user-message") return stripInputMeta(event);
|
|
30
|
+
if (event.type === "runtime-input") {
|
|
31
|
+
const { meta: _meta, ...rest } = event;
|
|
32
|
+
return {
|
|
33
|
+
...rest,
|
|
34
|
+
input: stripInputMeta(event.input)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return event;
|
|
38
|
+
}
|
|
39
|
+
function userInputFromEvent(event) {
|
|
40
|
+
return stripInputMeta(event);
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { attachInputMeta, attachRuntimeInputMeta, stripEventMeta, stripInputMeta, userInputFromEvent };
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=input-meta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input-meta.js","names":[],"sources":["../../src/session/input-meta.ts"],"sourcesContent":["import type { AgentEvent, RuntimeInput } from \"./events\";\nimport type { UserInput, UserMessage, UserText } from \"./input\";\nimport type { InputEventMeta } from \"./input-meta-types\";\n\nexport type { InputEventMeta, InputSource } from \"./input-meta-types\";\n\nexport function attachInputMeta(\n input: UserInput,\n meta: InputEventMeta\n): UserText | UserMessage {\n if (input.type === \"user-text\") {\n return { ...input, meta };\n }\n\n return { ...input, meta };\n}\n\nexport function attachRuntimeInputMeta(\n input: UserInput,\n placement: RuntimeInput[\"placement\"],\n meta: InputEventMeta\n): RuntimeInput {\n return {\n input: attachInputMeta(input, meta),\n meta,\n placement,\n type: \"runtime-input\",\n };\n}\n\nexport function stripInputMeta(input: UserInput): UserInput {\n if (input.type === \"user-text\") {\n const { meta: _meta, ...rest } = input;\n return rest;\n }\n\n const { meta: _meta, ...rest } = input;\n return rest;\n}\n\nexport function stripEventMeta(event: AgentEvent): AgentEvent {\n if (event.type === \"user-text\" || event.type === \"user-message\") {\n return stripInputMeta(event);\n }\n\n if (event.type === \"runtime-input\") {\n const { meta: _meta, ...rest } = event;\n return {\n ...rest,\n input: stripInputMeta(event.input),\n };\n }\n\n return event;\n}\n\nexport function userInputFromEvent(event: UserText | UserMessage): UserInput {\n return stripInputMeta(event);\n}\n"],"mappings":";AAMA,SAAgB,gBACd,OACA,MACwB;CACxB,IAAI,MAAM,SAAS,aACjB,OAAO;EAAE,GAAG;EAAO;CAAK;CAG1B,OAAO;EAAE,GAAG;EAAO;CAAK;AAC1B;AAEA,SAAgB,uBACd,OACA,WACA,MACc;CACd,OAAO;EACL,OAAO,gBAAgB,OAAO,IAAI;EAClC;EACA;EACA,MAAM;CACR;AACF;AAEA,SAAgB,eAAe,OAA6B;CAC1D,IAAI,MAAM,SAAS,aAAa;EAC9B,MAAM,EAAE,MAAM,OAAO,GAAG,SAAS;EACjC,OAAO;CACT;CAEA,MAAM,EAAE,MAAM,OAAO,GAAG,SAAS;CACjC,OAAO;AACT;AAEA,SAAgB,eAAe,OAA+B;CAC5D,IAAI,MAAM,SAAS,eAAe,MAAM,SAAS,gBAC/C,OAAO,eAAe,KAAK;CAG7B,IAAI,MAAM,SAAS,iBAAiB;EAClC,MAAM,EAAE,MAAM,OAAO,GAAG,SAAS;EACjC,OAAO;GACL,GAAG;GACH,OAAO,eAAe,MAAM,KAAK;EACnC;CACF;CAEA,OAAO;AACT;AAEA,SAAgB,mBAAmB,OAA0C;CAC3E,OAAO,eAAe,KAAK;AAC7B"}
|
package/dist/session/input.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { InputEventMeta } from "./input-meta-types.js";
|
|
2
|
+
|
|
1
3
|
//#region src/session/input.d.ts
|
|
2
4
|
type UserTextContent = string | readonly string[];
|
|
3
5
|
interface UserText {
|
|
6
|
+
meta?: InputEventMeta;
|
|
4
7
|
text: UserTextContent;
|
|
5
8
|
type: "user-text";
|
|
6
9
|
}
|
|
@@ -36,6 +39,7 @@ type UserMessageContentPart = UserMessageFilePart | UserMessageImagePart | UserM
|
|
|
36
39
|
type UserMessageContent = readonly UserMessageContentPart[];
|
|
37
40
|
interface UserMessage {
|
|
38
41
|
content: UserMessageContent;
|
|
42
|
+
meta?: InputEventMeta;
|
|
39
43
|
type: "user-message";
|
|
40
44
|
}
|
|
41
45
|
type UserInput = UserMessage | UserText;
|