@minpeter/pss-runtime 0.0.5 → 0.0.7
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 +42 -0
- package/dist/agent-loop.d.ts +3 -0
- package/dist/agent-loop.js +20 -1
- package/dist/agent-loop.js.map +1 -1
- package/dist/agent.d.ts +6 -1
- package/dist/agent.js +4 -1
- package/dist/agent.js.map +1 -1
- package/dist/hooks.d.ts +32 -0
- package/dist/index.d.ts +5 -4
- package/dist/llm.d.ts +4 -2
- package/dist/llm.js +2 -1
- package/dist/llm.js.map +1 -1
- package/dist/session/events.d.ts +36 -2
- package/dist/session/history.js +2 -2
- package/dist/session/history.js.map +1 -1
- package/dist/session/mapping.js +47 -1
- package/dist/session/mapping.js.map +1 -1
- package/dist/session/session.d.ts +5 -4
- package/dist/session/session.js +54 -13
- package/dist/session/session.js.map +1 -1
- package/dist/session/snapshot.js +20 -0
- package/dist/session/snapshot.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,11 +32,53 @@ for await (const event of run.stream()) {
|
|
|
32
32
|
|
|
33
33
|
`agent.send(...)` is shorthand for `agent.session("default").send(...)`.
|
|
34
34
|
|
|
35
|
+
For model providers that support multimodal input, send JSON-serializable content
|
|
36
|
+
parts through the same API. String input and `readonly string[]` remain supported
|
|
37
|
+
shortcuts for text-only turns.
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
const run = await agent.send([
|
|
41
|
+
{ type: "text", text: "Describe this UI screenshot." },
|
|
42
|
+
{
|
|
43
|
+
type: "image",
|
|
44
|
+
image: "data:image/png;base64,iVBORw0KGgo...",
|
|
45
|
+
mediaType: "image/png",
|
|
46
|
+
},
|
|
47
|
+
]);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
File parts use the same JSON-serializable shape when the selected model supports
|
|
51
|
+
file input:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
await agent.send([
|
|
55
|
+
{ type: "text", text: "Summarize the attached report." },
|
|
56
|
+
{
|
|
57
|
+
type: "file",
|
|
58
|
+
data: "data:application/pdf;base64,JVBERi0x...",
|
|
59
|
+
filename: "report.pdf",
|
|
60
|
+
mediaType: "application/pdf",
|
|
61
|
+
},
|
|
62
|
+
]);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The runtime normalizes and persists these content parts as session continuation
|
|
66
|
+
state; it does not fetch remote media, decode files, or guarantee provider support
|
|
67
|
+
for every media type.
|
|
68
|
+
|
|
69
|
+
The public transcript protocol is `AgentEvent`: live runs emit runtime-defined
|
|
70
|
+
events through `run.stream()`. Provider/model message history is internal
|
|
71
|
+
continuation state, not a public history API.
|
|
72
|
+
|
|
35
73
|
## Session storage and portability
|
|
36
74
|
|
|
37
75
|
The runtime owns full session state encoding and history compaction semantics.
|
|
38
76
|
Adapters own persistence only through `SessionStore`:
|
|
39
77
|
|
|
78
|
+
Stored session state is an opaque, versioned runtime snapshot for continuation.
|
|
79
|
+
Do not inspect it as a replay log; exact replay should be modeled separately as
|
|
80
|
+
an `AgentEvent` log if that capability is added later.
|
|
81
|
+
|
|
40
82
|
```ts
|
|
41
83
|
import type { SessionStore } from "@minpeter/pss-runtime";
|
|
42
84
|
import { MemorySessionStore } from "@minpeter/pss-runtime/session-store/memory";
|
package/dist/agent-loop.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Llm } from "./llm.js";
|
|
2
2
|
import { AgentEventListener } from "./session/events.js";
|
|
3
|
+
import { AgentHooks } from "./hooks.js";
|
|
3
4
|
import { ModelMessage } from "ai";
|
|
4
5
|
|
|
5
6
|
//#region src/agent-loop.d.ts
|
|
@@ -10,6 +11,7 @@ interface ModelHistory {
|
|
|
10
11
|
interface RunAgentLoopOptions {
|
|
11
12
|
emit: AgentEventListener;
|
|
12
13
|
history: ModelHistory;
|
|
14
|
+
hooks?: AgentHooks;
|
|
13
15
|
llm: Llm;
|
|
14
16
|
signal?: AbortSignal;
|
|
15
17
|
}
|
|
@@ -17,6 +19,7 @@ type AgentLoopResult = "completed" | "aborted";
|
|
|
17
19
|
declare function runAgentLoop({
|
|
18
20
|
emit,
|
|
19
21
|
history,
|
|
22
|
+
hooks,
|
|
20
23
|
llm,
|
|
21
24
|
signal
|
|
22
25
|
}: RunAgentLoopOptions): Promise<AgentLoopResult>;
|
package/dist/agent-loop.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { modelMessageToAgentEvents } from "./session/mapping.js";
|
|
2
2
|
//#region src/agent-loop.ts
|
|
3
|
-
async function runAgentLoop({ emit, history, llm, signal = new AbortController().signal }) {
|
|
3
|
+
async function runAgentLoop({ emit, history, hooks, llm, signal = new AbortController().signal }) {
|
|
4
|
+
let stepIndex = 0;
|
|
4
5
|
while (true) {
|
|
6
|
+
if (signal.aborted) return "aborted";
|
|
7
|
+
await hooks?.beforeStep?.({
|
|
8
|
+
history: history.modelSnapshot(),
|
|
9
|
+
signal,
|
|
10
|
+
stepIndex
|
|
11
|
+
});
|
|
5
12
|
if (signal.aborted) return "aborted";
|
|
6
13
|
emit({ type: "step-start" });
|
|
7
14
|
const output = await readLlmOutput({
|
|
@@ -17,10 +24,22 @@ async function runAgentLoop({ emit, history, llm, signal = new AbortController()
|
|
|
17
24
|
signal
|
|
18
25
|
});
|
|
19
26
|
if (result === "aborted") return "aborted";
|
|
27
|
+
await runAfterStepHook(hooks, {
|
|
28
|
+
history: history.modelSnapshot(),
|
|
29
|
+
result,
|
|
30
|
+
signal,
|
|
31
|
+
stepIndex
|
|
32
|
+
});
|
|
20
33
|
emit({ type: "step-end" });
|
|
21
34
|
if (result === "completed") return "completed";
|
|
35
|
+
stepIndex += 1;
|
|
22
36
|
}
|
|
23
37
|
}
|
|
38
|
+
async function runAfterStepHook(hooks, context) {
|
|
39
|
+
const hook = hooks?.afterStep;
|
|
40
|
+
if (!hook) return;
|
|
41
|
+
await Promise.allSettled([Promise.resolve().then(() => hook(context))]);
|
|
42
|
+
}
|
|
24
43
|
async function readLlmOutput({ history, llm, signal }) {
|
|
25
44
|
try {
|
|
26
45
|
return await llm({
|
package/dist/agent-loop.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-loop.js","names":[],"sources":["../src/agent-loop.ts"],"sourcesContent":["import type { ModelMessage } from \"ai\";\nimport type { Llm, LlmOutput } from \"./llm\";\nimport type { AgentEventListener } from \"./session/events\";\nimport { modelMessageToAgentEvents } from \"./session/mapping\";\n\ninterface ModelHistory {\n appendModelMessage(message: ModelMessage): void;\n modelSnapshot(): ModelMessage[];\n}\n\ninterface RunAgentLoopOptions {\n emit: AgentEventListener;\n history: ModelHistory;\n llm: Llm;\n signal?: AbortSignal;\n}\n\nexport type AgentLoopResult = \"completed\" | \"aborted\";\ntype StepOutputResult =
|
|
1
|
+
{"version":3,"file":"agent-loop.js","names":[],"sources":["../src/agent-loop.ts"],"sourcesContent":["import type { ModelMessage } from \"ai\";\nimport type { AgentHooks, AgentStepResult } from \"./hooks\";\nimport type { Llm, LlmOutput } from \"./llm\";\nimport type { AgentEventListener } from \"./session/events\";\nimport { modelMessageToAgentEvents } from \"./session/mapping\";\n\ninterface ModelHistory {\n appendModelMessage(message: ModelMessage): void;\n modelSnapshot(): ModelMessage[];\n}\n\ninterface RunAgentLoopOptions {\n emit: AgentEventListener;\n history: ModelHistory;\n hooks?: AgentHooks;\n llm: Llm;\n signal?: AbortSignal;\n}\n\nexport type AgentLoopResult = \"completed\" | \"aborted\";\ntype StepOutputResult = AgentStepResult | \"aborted\";\n\nexport async function runAgentLoop({\n emit,\n history,\n hooks,\n llm,\n signal = new AbortController().signal,\n}: RunAgentLoopOptions): Promise<AgentLoopResult> {\n let stepIndex = 0;\n\n while (true) {\n if (signal.aborted) {\n return \"aborted\";\n }\n\n await hooks?.beforeStep?.({\n history: history.modelSnapshot(),\n signal,\n stepIndex,\n });\n\n if (signal.aborted) {\n return \"aborted\";\n }\n\n emit({ type: \"step-start\" });\n const output = await readLlmOutput({ history, llm, signal });\n\n if (output === \"aborted\") {\n return \"aborted\";\n }\n\n const result = appendStepOutput({ emit, history, output, signal });\n\n if (result === \"aborted\") {\n return \"aborted\";\n }\n\n await runAfterStepHook(hooks, {\n history: history.modelSnapshot(),\n result,\n signal,\n stepIndex,\n });\n emit({ type: \"step-end\" });\n\n if (result === \"completed\") {\n return \"completed\";\n }\n\n stepIndex += 1;\n }\n}\n\nasync function runAfterStepHook(\n hooks: AgentHooks | undefined,\n context: Parameters<NonNullable<AgentHooks[\"afterStep\"]>>[0]\n): Promise<void> {\n const hook = hooks?.afterStep;\n if (!hook) {\n return;\n }\n\n await Promise.allSettled([Promise.resolve().then(() => hook(context))]);\n}\n\nasync function readLlmOutput({\n history,\n llm,\n signal,\n}: Pick<RunAgentLoopOptions, \"history\" | \"llm\"> & {\n signal: AbortSignal;\n}): Promise<LlmOutput | \"aborted\"> {\n try {\n return await llm({ history: history.modelSnapshot(), signal });\n } catch (error) {\n if (signal.aborted) {\n return \"aborted\";\n }\n\n throw error;\n }\n}\n\nfunction appendStepOutput({\n emit,\n history,\n output,\n signal,\n}: Pick<RunAgentLoopOptions, \"emit\" | \"history\"> & {\n output: LlmOutput;\n signal: AbortSignal;\n}): StepOutputResult {\n if (signal.aborted) {\n return \"aborted\";\n }\n\n let shouldContinue = false;\n\n for (const message of output) {\n if (signal.aborted) {\n return \"aborted\";\n }\n\n history.appendModelMessage(message);\n const events = modelMessageToAgentEvents(message);\n\n for (const event of events) {\n emit(event);\n }\n\n if (events.some((event) => event.type === \"tool-call\")) {\n shouldContinue = true;\n }\n }\n\n return shouldContinue ? \"continue\" : \"completed\";\n}\n"],"mappings":";;AAsBA,eAAsB,aAAa,EACjC,MACA,SACA,OACA,KACA,SAAS,IAAI,gBAAgB,EAAE,UACiB;CAChD,IAAI,YAAY;CAEhB,OAAO,MAAM;EACX,IAAI,OAAO,SACT,OAAO;EAGT,MAAM,OAAO,aAAa;GACxB,SAAS,QAAQ,cAAc;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Llm } from "./llm.js";
|
|
1
|
+
import { AgentToolChoice, AgentTools, Llm } from "./llm.js";
|
|
2
2
|
import { AgentRun } from "./session/run.js";
|
|
3
3
|
import { SessionStore } from "./session/store/types.js";
|
|
4
4
|
import { AgentInput } from "./session/session.js";
|
|
5
|
+
import { AgentHooks } from "./hooks.js";
|
|
5
6
|
import { LanguageModel } from "ai";
|
|
6
7
|
|
|
7
8
|
//#region src/agent.d.ts
|
|
8
9
|
interface AgentModelOptions {
|
|
10
|
+
hooks?: AgentHooks;
|
|
9
11
|
instructions?: string;
|
|
10
12
|
llm?: never;
|
|
11
13
|
model: LanguageModel;
|
|
12
14
|
sessions?: AgentSessionOptions;
|
|
15
|
+
toolChoice?: AgentToolChoice;
|
|
13
16
|
tools?: AgentTools;
|
|
14
17
|
}
|
|
15
18
|
interface AgentLlmOptions {
|
|
19
|
+
hooks?: AgentHooks;
|
|
16
20
|
instructions?: never;
|
|
17
21
|
llm: Llm;
|
|
18
22
|
model?: never;
|
|
19
23
|
sessions?: AgentSessionOptions;
|
|
24
|
+
toolChoice?: never;
|
|
20
25
|
tools?: never;
|
|
21
26
|
}
|
|
22
27
|
interface AgentSessionOptions {
|
package/dist/agent.js
CHANGED
|
@@ -3,15 +3,18 @@ import { AgentSession } from "./session/session.js";
|
|
|
3
3
|
import { MemorySessionStore } from "./session/store/memory.js";
|
|
4
4
|
//#region src/agent.ts
|
|
5
5
|
var Agent = class Agent {
|
|
6
|
+
#hooks;
|
|
6
7
|
#llm;
|
|
7
8
|
#sessions = /* @__PURE__ */ new Map();
|
|
8
9
|
#store;
|
|
9
10
|
constructor(options) {
|
|
10
11
|
assertAgentOptions(options);
|
|
11
12
|
this.#store = options.sessions?.store ?? new MemorySessionStore();
|
|
13
|
+
this.#hooks = options.hooks;
|
|
12
14
|
this.#llm = hasCustomLlm(options) ? options.llm : createLlm({
|
|
13
15
|
instructions: options.instructions,
|
|
14
16
|
model: options.model,
|
|
17
|
+
toolChoice: options.toolChoice,
|
|
15
18
|
tools: options.tools
|
|
16
19
|
});
|
|
17
20
|
}
|
|
@@ -27,7 +30,7 @@ var Agent = class Agent {
|
|
|
27
30
|
const session = new AgentSession(this.#llm, {
|
|
28
31
|
key,
|
|
29
32
|
store: this.#store
|
|
30
|
-
});
|
|
33
|
+
}, this.#hooks);
|
|
31
34
|
const handle = {
|
|
32
35
|
interrupt: () => session.interrupt(),
|
|
33
36
|
kill: () => {
|
package/dist/agent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.js","names":["#llm","#sessions","#store"],"sources":["../src/agent.ts"],"sourcesContent":["import type { LanguageModel } from \"ai\";\nimport { type AgentTools
|
|
1
|
+
{"version":3,"file":"agent.js","names":["#hooks","#llm","#sessions","#store"],"sources":["../src/agent.ts"],"sourcesContent":["import type { LanguageModel } from \"ai\";\nimport type { AgentHooks } from \"./hooks\";\nimport {\n type AgentToolChoice,\n type AgentTools,\n createLlm,\n type Llm,\n} from \"./llm\";\nimport type { AgentRun } from \"./session/run\";\nimport { type AgentInput, AgentSession } from \"./session/session\";\nimport { MemorySessionStore } from \"./session/store/memory\";\nimport type { SessionStore } from \"./session/store/types\";\n\ninterface AgentModelOptions {\n hooks?: AgentHooks;\n instructions?: string;\n llm?: never;\n model: LanguageModel;\n sessions?: AgentSessionOptions;\n toolChoice?: AgentToolChoice;\n tools?: AgentTools;\n}\n\ninterface AgentLlmOptions {\n hooks?: AgentHooks;\n instructions?: never;\n llm: Llm;\n model?: never;\n sessions?: AgentSessionOptions;\n toolChoice?: never;\n tools?: never;\n}\n\nexport interface AgentSessionOptions {\n store?: SessionStore;\n}\n\nexport interface SessionHandle {\n interrupt(): void;\n kill(): void;\n send(input: AgentInput): Promise<AgentRun>;\n}\n\nexport type AgentOptions = AgentModelOptions | AgentLlmOptions;\n\nexport class Agent {\n readonly #hooks?: AgentHooks;\n readonly #llm: Llm;\n readonly #sessions = new Map<string, SessionHandle>();\n readonly #store: SessionStore;\n\n private constructor(options: AgentOptions) {\n assertAgentOptions(options);\n\n this.#store = options.sessions?.store ?? new MemorySessionStore();\n this.#hooks = options.hooks;\n this.#llm = hasCustomLlm(options)\n ? options.llm\n : createLlm({\n instructions: options.instructions,\n model: options.model,\n toolChoice: options.toolChoice,\n tools: options.tools,\n });\n }\n\n static create(options: AgentOptions): Promise<Agent> {\n return Promise.resolve().then(() => new Agent(options));\n }\n\n send(input: AgentInput): Promise<AgentRun> {\n return this.session(\"default\").send(input);\n }\n\n session(key: string): SessionHandle {\n const existing = this.#sessions.get(key);\n if (existing) {\n return existing;\n }\n\n const session = new AgentSession(\n this.#llm,\n { key, store: this.#store },\n this.#hooks\n );\n const handle: SessionHandle = {\n interrupt: () => session.interrupt(),\n kill: () => {\n session.kill();\n this.#sessions.delete(key);\n },\n send: (input) => session.send(input),\n };\n this.#sessions.set(key, handle);\n return handle;\n }\n}\n\nfunction assertAgentOptions(options: unknown): asserts options is AgentOptions {\n if (options === null || typeof options !== \"object\") {\n throw new TypeError(\n \"Agent options are required. Provide either { model } or { llm }.\"\n );\n }\n\n const hasLlm = hasCustomLlm(options);\n const hasModel =\n \"model\" in options && options.model !== undefined && options.model !== null;\n\n if (hasLlm && hasModel) {\n throw new TypeError(\n \"Agent.create: provide either options.llm or options.model, not both.\"\n );\n }\n\n if (\"llm\" in options && options.llm !== undefined && !hasLlm) {\n throw new TypeError(\"Agent.create: invalid options.llm.\");\n }\n\n if (!(hasLlm || hasModel)) {\n throw new TypeError(\"Agent.create: missing options.model.\");\n }\n}\n\nfunction hasCustomLlm(options: object): options is AgentLlmOptions {\n return \"llm\" in options && typeof options.llm === \"function\";\n}\n"],"mappings":";;;;AA6CA,IAAa,QAAb,MAAa,MAAM;CACjB;CACA;CACA,4BAAqB,IAAI,IAA2B;CACpD;CAEA,YAAoB,SAAuB;EACzC,mBAAmB,OAAO;EAE1B,KAAKG,SAAS,QAAQ,UAAU,SAAS,IAAI,mBAAmB;EAChE,KAAKH,SAAS,QAAQ;EACtB,K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1
|
+
import { RuntimeLlmContext } from "./llm.js";
|
|
2
|
+
import { UserInput } from "./session/session.js";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks.d.ts
|
|
5
|
+
type AgentTurnResult = "aborted" | "completed";
|
|
6
|
+
type AgentStepResult = "completed" | "continue";
|
|
7
|
+
type MaybePromise<T> = PromiseLike<T> | T;
|
|
8
|
+
interface AgentBeforeTurnContext {
|
|
9
|
+
readonly history: RuntimeLlmContext["history"];
|
|
10
|
+
readonly input: UserInput;
|
|
11
|
+
readonly signal: AbortSignal;
|
|
12
|
+
}
|
|
13
|
+
interface AgentAfterTurnContext extends AgentBeforeTurnContext {
|
|
14
|
+
readonly result: AgentTurnResult;
|
|
15
|
+
}
|
|
16
|
+
interface AgentBeforeStepContext {
|
|
17
|
+
readonly history: RuntimeLlmContext["history"];
|
|
18
|
+
readonly signal: AbortSignal;
|
|
19
|
+
readonly stepIndex: number;
|
|
20
|
+
}
|
|
21
|
+
interface AgentAfterStepContext extends AgentBeforeStepContext {
|
|
22
|
+
readonly result: AgentStepResult;
|
|
23
|
+
}
|
|
24
|
+
interface AgentHooks {
|
|
25
|
+
afterStep?(context: AgentAfterStepContext): MaybePromise<void>;
|
|
26
|
+
afterTurn?(context: AgentAfterTurnContext): MaybePromise<void>;
|
|
27
|
+
beforeStep?(context: AgentBeforeStepContext): MaybePromise<void>;
|
|
28
|
+
beforeTurn?(context: AgentBeforeTurnContext): MaybePromise<void>;
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { AgentAfterStepContext, AgentAfterTurnContext, AgentBeforeStepContext, AgentBeforeTurnContext, AgentHooks, AgentStepResult, AgentTurnResult };
|
|
32
|
+
//# sourceMappingURL=hooks.d.ts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, ToolCall, ToolResult, UserText, UserTextContent } from "./session/events.js";
|
|
1
|
+
import { AgentModel, AgentTool, AgentToolChoice, AgentToolExecute, AgentToolExecutionOptions, AgentTools, LlmOutputPart, RuntimeCreateLlmOptions, RuntimeLlm, RuntimeLlmContext, RuntimeLlmOutput, createLlm } from "./llm.js";
|
|
2
|
+
import { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, ToolCall, ToolResult, UserMessage, UserMessageContent, UserMessageContentPart, UserMessageFileData, UserMessageFilePart, UserMessageImagePart, UserMessageTextPart, UserText, UserTextContent } from "./session/events.js";
|
|
3
3
|
import { AgentRun } from "./session/run.js";
|
|
4
4
|
import { CommitResult, ExpectedSessionVersion, SessionStore, StoredSession } from "./session/store/types.js";
|
|
5
|
-
import { AgentInput } from "./session/session.js";
|
|
5
|
+
import { AgentInput, SessionInput, UserInput } from "./session/session.js";
|
|
6
|
+
import { AgentAfterStepContext, AgentAfterTurnContext, AgentBeforeStepContext, AgentBeforeTurnContext, AgentHooks, AgentStepResult, AgentTurnResult } from "./hooks.js";
|
|
6
7
|
import { Agent, AgentOptions, AgentSessionOptions, SessionHandle } from "./agent.js";
|
|
7
8
|
import { AgentLoopResult, runAgentLoop } from "./agent-loop.js";
|
|
8
|
-
export { Agent, type AgentEvent, type AgentEventListener, type
|
|
9
|
+
export { Agent, type AgentAfterStepContext, type AgentAfterTurnContext, type AgentBeforeStepContext, type AgentBeforeTurnContext, type AgentEvent, type AgentEventListener, type AgentHooks, type AgentInput, type AgentLoopResult, type AgentModel, type AgentOptions, type AgentRun, type AgentSessionOptions, type AgentStepResult, type AgentTool, type AgentToolChoice, type AgentToolExecute, type AgentToolExecutionOptions, type AgentTools, type AgentTurnResult, type AssistantReasoning, type AssistantText, type CommitResult, type ExpectedSessionVersion, type LlmOutputPart, type RuntimeCreateLlmOptions, type RuntimeLlm, type RuntimeLlmContext, type RuntimeLlmOutput, type SessionHandle, type SessionInput, type SessionStore, type StoredSession, type ToolCall, type ToolResult, type UserInput, type UserMessage, type UserMessageContent, type UserMessageContentPart, type UserMessageFileData, type UserMessageFilePart, type UserMessageImagePart, type UserMessageTextPart, type UserText, type UserTextContent, createLlm, runAgentLoop };
|
package/dist/llm.d.ts
CHANGED
|
@@ -5,8 +5,8 @@ type AgentToolExecutionOptions = ToolExecutionOptions<unknown>;
|
|
|
5
5
|
type AgentToolExecute = NonNullable<Tool["execute"]>;
|
|
6
6
|
type AgentTool = Tool;
|
|
7
7
|
type AgentTools = ToolSet;
|
|
8
|
+
type AgentToolChoice = "auto" | "required";
|
|
8
9
|
type AgentModel = LanguageModel;
|
|
9
|
-
type AgentMessage = ModelMessage;
|
|
10
10
|
type LlmOutput = Awaited<ReturnType<typeof generateText>>["responseMessages"];
|
|
11
11
|
type LlmOutputPart = LlmOutput[number];
|
|
12
12
|
interface LlmContext {
|
|
@@ -17,6 +17,7 @@ type Llm = (context: LlmContext) => Promise<LlmOutput>;
|
|
|
17
17
|
interface CreateLlmOptions {
|
|
18
18
|
instructions?: string;
|
|
19
19
|
model: LanguageModel;
|
|
20
|
+
toolChoice?: AgentToolChoice;
|
|
20
21
|
tools?: AgentTools;
|
|
21
22
|
}
|
|
22
23
|
type RuntimeCreateLlmOptions = CreateLlmOptions;
|
|
@@ -26,8 +27,9 @@ type RuntimeLlmOutput = LlmOutput;
|
|
|
26
27
|
declare function createLlm({
|
|
27
28
|
model,
|
|
28
29
|
instructions,
|
|
30
|
+
toolChoice,
|
|
29
31
|
tools
|
|
30
32
|
}: CreateLlmOptions): Llm;
|
|
31
33
|
//#endregion
|
|
32
|
-
export {
|
|
34
|
+
export { AgentModel, AgentTool, AgentToolChoice, AgentToolExecute, AgentToolExecutionOptions, AgentTools, Llm, LlmOutputPart, RuntimeCreateLlmOptions, RuntimeLlm, RuntimeLlmContext, RuntimeLlmOutput, createLlm };
|
|
33
35
|
//# sourceMappingURL=llm.d.ts.map
|
package/dist/llm.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { generateText } from "ai";
|
|
2
2
|
//#region src/llm.ts
|
|
3
|
-
function createLlm({ model, instructions, tools }) {
|
|
3
|
+
function createLlm({ model, instructions, toolChoice, tools }) {
|
|
4
4
|
return async ({ history, signal }) => {
|
|
5
5
|
const { responseMessages } = await generateText({
|
|
6
6
|
abortSignal: signal,
|
|
7
7
|
instructions,
|
|
8
8
|
messages: [...history],
|
|
9
9
|
model,
|
|
10
|
+
toolChoice,
|
|
10
11
|
tools
|
|
11
12
|
});
|
|
12
13
|
return responseMessages;
|
package/dist/llm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm.js","names":[],"sources":["../src/llm.ts"],"sourcesContent":["import type {\n LanguageModel,\n ModelMessage,\n Tool,\n ToolExecutionOptions,\n ToolSet,\n} from \"ai\";\nimport { generateText } from \"ai\";\n\nexport type AgentToolExecutionOptions = ToolExecutionOptions<unknown>;\nexport type AgentToolExecute = NonNullable<Tool[\"execute\"]>;\nexport type AgentTool = Tool;\nexport type AgentTools = ToolSet;\nexport type AgentModel = LanguageModel;\nexport type AgentMessage = ModelMessage;\nexport type LlmOutput = Awaited<\n ReturnType<typeof generateText>\n>[\"responseMessages\"];\nexport type LlmOutputPart = LlmOutput[number];\n\nexport interface LlmContext {\n history: readonly ModelMessage[];\n signal: AbortSignal;\n}\n\nexport type Llm = (context: LlmContext) => Promise<LlmOutput>;\n\nexport interface CreateLlmOptions {\n instructions?: string;\n model: LanguageModel;\n tools?: AgentTools;\n}\n\nexport type RuntimeCreateLlmOptions = CreateLlmOptions;\nexport type RuntimeLlm = Llm;\nexport type RuntimeLlmContext = LlmContext;\nexport type RuntimeLlmOutput = LlmOutput;\n\nexport function createLlm({\n model,\n instructions,\n tools,\n}: CreateLlmOptions): Llm {\n return async ({ history, signal }) => {\n const { responseMessages } = await generateText({\n abortSignal: signal,\n instructions,\n messages: [...history],\n model,\n tools,\n });\n\n return responseMessages;\n };\n}\n"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"llm.js","names":[],"sources":["../src/llm.ts"],"sourcesContent":["import type {\n LanguageModel,\n ModelMessage,\n Tool,\n ToolExecutionOptions,\n ToolSet,\n} from \"ai\";\nimport { generateText } from \"ai\";\n\nexport type AgentToolExecutionOptions = ToolExecutionOptions<unknown>;\nexport type AgentToolExecute = NonNullable<Tool[\"execute\"]>;\nexport type AgentTool = Tool;\nexport type AgentTools = ToolSet;\nexport type AgentToolChoice = \"auto\" | \"required\";\nexport type AgentModel = LanguageModel;\nexport type AgentMessage = ModelMessage;\nexport type LlmOutput = Awaited<\n ReturnType<typeof generateText>\n>[\"responseMessages\"];\nexport type LlmOutputPart = LlmOutput[number];\n\nexport interface LlmContext {\n history: readonly ModelMessage[];\n signal: AbortSignal;\n}\n\nexport type Llm = (context: LlmContext) => Promise<LlmOutput>;\n\nexport interface CreateLlmOptions {\n instructions?: string;\n model: LanguageModel;\n toolChoice?: AgentToolChoice;\n tools?: AgentTools;\n}\n\nexport type RuntimeCreateLlmOptions = CreateLlmOptions;\nexport type RuntimeLlm = Llm;\nexport type RuntimeLlmContext = LlmContext;\nexport type RuntimeLlmOutput = LlmOutput;\n\nexport function createLlm({\n model,\n instructions,\n toolChoice,\n tools,\n}: CreateLlmOptions): Llm {\n return async ({ history, signal }) => {\n const { responseMessages } = await generateText({\n abortSignal: signal,\n instructions,\n messages: [...history],\n model,\n toolChoice,\n tools,\n });\n\n return responseMessages;\n };\n}\n"],"mappings":";;AAwCA,SAAgB,UAAU,EACxB,OACA,cACA,YACA,SACwB;CACxB,OAAO,OAAO,EAAE,SAAS,aAAa;EACpC,MAAM,EAAE,qBAAqB,MAAM,aAAa;GAC9C,aAAa;GACb;GACA,UAAU,CAAC,GAAG,OAAO;GACrB;GACA;GACA;EACF,CAAC;EAED,OAAO;CACT;AACF"}
|
package/dist/session/events.d.ts
CHANGED
|
@@ -4,6 +4,40 @@ interface UserText {
|
|
|
4
4
|
text: UserTextContent;
|
|
5
5
|
type: "user-text";
|
|
6
6
|
}
|
|
7
|
+
interface UserMessageTextPart {
|
|
8
|
+
text: string;
|
|
9
|
+
type: "text";
|
|
10
|
+
}
|
|
11
|
+
interface UserMessageImagePart {
|
|
12
|
+
image: string;
|
|
13
|
+
mediaType?: string;
|
|
14
|
+
type: "image";
|
|
15
|
+
}
|
|
16
|
+
type UserMessageFileData = string | {
|
|
17
|
+
data: string;
|
|
18
|
+
type: "data";
|
|
19
|
+
} | {
|
|
20
|
+
reference: Record<string, string>;
|
|
21
|
+
type: "reference";
|
|
22
|
+
} | {
|
|
23
|
+
text: string;
|
|
24
|
+
type: "text";
|
|
25
|
+
} | {
|
|
26
|
+
type: "url";
|
|
27
|
+
url: string;
|
|
28
|
+
};
|
|
29
|
+
interface UserMessageFilePart {
|
|
30
|
+
data: UserMessageFileData;
|
|
31
|
+
filename?: string;
|
|
32
|
+
mediaType: string;
|
|
33
|
+
type: "file";
|
|
34
|
+
}
|
|
35
|
+
type UserMessageContentPart = UserMessageFilePart | UserMessageImagePart | UserMessageTextPart;
|
|
36
|
+
type UserMessageContent = readonly UserMessageContentPart[];
|
|
37
|
+
interface UserMessage {
|
|
38
|
+
content: UserMessageContent;
|
|
39
|
+
type: "user-message";
|
|
40
|
+
}
|
|
7
41
|
interface AssistantText {
|
|
8
42
|
text: string;
|
|
9
43
|
type: "assistant-text";
|
|
@@ -24,7 +58,7 @@ interface ToolResult {
|
|
|
24
58
|
toolName: string;
|
|
25
59
|
type: "tool-result";
|
|
26
60
|
}
|
|
27
|
-
type AgentEvent = /** User input was accepted into the session queue. */UserText /** A queued user input started running as a turn. */ | {
|
|
61
|
+
type AgentEvent = /** User input was accepted into the session queue. */UserMessage | UserText /** User multipart input was accepted into the session queue. */ | UserMessage /** A queued user input started running as a turn. */ | {
|
|
28
62
|
type: "turn-start";
|
|
29
63
|
} /** The active turn was interrupted before normal completion. */ | {
|
|
30
64
|
type: "turn-abort";
|
|
@@ -40,5 +74,5 @@ type AgentEvent = /** User input was accepted into the session queue. */UserText
|
|
|
40
74
|
};
|
|
41
75
|
type AgentEventListener = (event: AgentEvent) => void;
|
|
42
76
|
//#endregion
|
|
43
|
-
export { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, ToolCall, ToolResult, UserText, UserTextContent };
|
|
77
|
+
export { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, ToolCall, ToolResult, UserMessage, UserMessageContent, UserMessageContentPart, UserMessageFileData, UserMessageFilePart, UserMessageImagePart, UserMessageTextPart, UserText, UserTextContent };
|
|
44
78
|
//# sourceMappingURL=events.d.ts.map
|
package/dist/session/history.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { userInputToModelMessage } from "./mapping.js";
|
|
2
2
|
//#region src/session/history.ts
|
|
3
3
|
var AgentModelHistory = class {
|
|
4
4
|
#modelHistory = [];
|
|
@@ -11,7 +11,7 @@ var AgentModelHistory = class {
|
|
|
11
11
|
return structuredClone(this.#modelHistory);
|
|
12
12
|
}
|
|
13
13
|
appendUserInput(input) {
|
|
14
|
-
this.#modelHistory.push(
|
|
14
|
+
this.#modelHistory.push(userInputToModelMessage(input));
|
|
15
15
|
this.#triggerChange();
|
|
16
16
|
}
|
|
17
17
|
appendModelMessage(message) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.js","names":["#modelHistory","#onChange","#triggerChange"],"sources":["../../src/session/history.ts"],"sourcesContent":["import type { ModelMessage } from \"ai\";\nimport
|
|
1
|
+
{"version":3,"file":"history.js","names":["#modelHistory","#onChange","#triggerChange"],"sources":["../../src/session/history.ts"],"sourcesContent":["import type { ModelMessage } from \"ai\";\nimport { userInputToModelMessage } from \"./mapping\";\nimport type { UserInput } from \"./session\";\n\nexport class AgentModelHistory {\n readonly #modelHistory: ModelMessage[] = [];\n readonly #onChange?: (snapshot: ModelMessage[]) => void;\n\n constructor(\n history?: ModelMessage[],\n onChange?: (snapshot: ModelMessage[]) => void\n ) {\n if (history) {\n this.#modelHistory = structuredClone(history);\n }\n this.#onChange = onChange;\n }\n\n modelSnapshot(): ModelMessage[] {\n return structuredClone(this.#modelHistory);\n }\n\n appendUserInput(input: UserInput): void {\n this.#modelHistory.push(userInputToModelMessage(input));\n this.#triggerChange();\n }\n\n appendModelMessage(message: ModelMessage): void {\n this.#modelHistory.push(structuredClone(message));\n this.#triggerChange();\n }\n\n rollback(snapshot: ModelMessage[]): void {\n this.#modelHistory.length = 0;\n this.#modelHistory.push(...structuredClone(snapshot));\n this.#triggerChange();\n }\n\n #triggerChange(): void {\n this.#onChange?.(this.modelSnapshot());\n }\n}\n"],"mappings":";;AAIA,IAAa,oBAAb,MAA+B;CAC7B,gBAAyC,CAAC;CAC1C;CAEA,YACE,SACA,UACA;EACA,IAAI,SACF,KAAKA,gBAAgB,gBAAgB,OAAO;EAE9C,KAAKC,YAAY;CACnB;CAEA,gBAAgC;EAC9B,OAAO,gBAAgB,KAAKD,aAAa;CAC3C;CAEA,gBAAgB,OAAwB;EACtC,KAAKA,cAAc,KAAK,wBAAwB,KAAK,CAAC;EACtD,KAAKE,eAAe;CACtB;CAEA,mBAAmB,SAA6B;EAC9C,KAAKF,cAAc,KAAK,gBAAgB,OAAO,CAAC;EAChD,KAAKE,eAAe;CACtB;CAEA,SAAS,UAAgC;EACvC,KAAKF,cAAc,SAAS;EAC5B,KAAKA,cAAc,KAAK,GAAG,gBAAgB,QAAQ,CAAC;EACpD,KAAKE,eAAe;CACtB;CAEA,iBAAuB;EACrB,KAAKD,YAAY,KAAK,cAAc,CAAC;CACvC;AACF"}
|
package/dist/session/mapping.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
//#region src/session/mapping.ts
|
|
2
|
+
function userInputToModelMessage(input) {
|
|
3
|
+
if (input.type === "user-message") return userMessageToModelMessage(input);
|
|
4
|
+
return userTextToModelMessage(input);
|
|
5
|
+
}
|
|
2
6
|
function userTextToModelMessage(input) {
|
|
3
7
|
return {
|
|
4
8
|
role: "user",
|
|
@@ -12,6 +16,48 @@ function userTextContentToUserContent(text) {
|
|
|
12
16
|
text: part
|
|
13
17
|
}));
|
|
14
18
|
}
|
|
19
|
+
function userMessageToModelMessage(input) {
|
|
20
|
+
return {
|
|
21
|
+
role: "user",
|
|
22
|
+
content: userMessageContentToUserContent(input.content)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function userMessageContentToUserContent(content) {
|
|
26
|
+
return content.map(userMessageContentPartToUserContentPart);
|
|
27
|
+
}
|
|
28
|
+
function userMessageContentPartToUserContentPart(part) {
|
|
29
|
+
if (part.type === "text") return {
|
|
30
|
+
type: "text",
|
|
31
|
+
text: part.text
|
|
32
|
+
};
|
|
33
|
+
if (part.type === "image") return {
|
|
34
|
+
type: "file",
|
|
35
|
+
data: part.image,
|
|
36
|
+
mediaType: part.mediaType ?? "image"
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
type: "file",
|
|
40
|
+
data: userMessageFileDataToFileData(part.data),
|
|
41
|
+
mediaType: part.mediaType,
|
|
42
|
+
...part.filename === void 0 ? {} : { filename: part.filename }
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function userMessageFileDataToFileData(data) {
|
|
46
|
+
if (typeof data === "string") return data;
|
|
47
|
+
if (data.type === "url") return data.url;
|
|
48
|
+
if (data.type === "data") return {
|
|
49
|
+
type: "data",
|
|
50
|
+
data: data.data
|
|
51
|
+
};
|
|
52
|
+
if (data.type === "reference") return {
|
|
53
|
+
type: "reference",
|
|
54
|
+
reference: { ...data.reference }
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
type: "text",
|
|
58
|
+
text: data.text
|
|
59
|
+
};
|
|
60
|
+
}
|
|
15
61
|
function modelMessageToAgentEvents(message) {
|
|
16
62
|
if (message.role === "assistant") return assistantReasoningFirstParts(assistantContentParts(message)).flatMap(assistantContentPartToEvents);
|
|
17
63
|
if (message.role === "tool") return message.content.flatMap(toolContentPartToEvents);
|
|
@@ -56,6 +102,6 @@ function toolResultPartToEvents(part) {
|
|
|
56
102
|
}];
|
|
57
103
|
}
|
|
58
104
|
//#endregion
|
|
59
|
-
export { modelMessageToAgentEvents,
|
|
105
|
+
export { modelMessageToAgentEvents, userInputToModelMessage };
|
|
60
106
|
|
|
61
107
|
//# sourceMappingURL=mapping.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mapping.js","names":[],"sources":["../../src/session/mapping.ts"],"sourcesContent":["import type {\n AssistantContent,\n AssistantModelMessage,\n ModelMessage,\n ToolModelMessage,\n UserModelMessage,\n} from \"ai\";\nimport type {\n AssistantReasoning,\n AssistantText,\n ToolCall,\n ToolResult,\n UserText,\n UserTextContent,\n} from \"./events\";\n\ntype AssistantContentPart = Exclude<AssistantContent, string>[number];\ntype ToolContentPart = ToolModelMessage[\"content\"][number];\ntype ModelEvent = AssistantReasoning | AssistantText | ToolCall | ToolResult;\n\n//
|
|
1
|
+
{"version":3,"file":"mapping.js","names":[],"sources":["../../src/session/mapping.ts"],"sourcesContent":["import type {\n AssistantContent,\n AssistantModelMessage,\n ModelMessage,\n ToolModelMessage,\n UserModelMessage,\n} from \"ai\";\nimport type {\n AssistantReasoning,\n AssistantText,\n ToolCall,\n ToolResult,\n UserMessage,\n UserMessageContent,\n UserMessageContentPart,\n UserMessageFileData,\n UserText,\n UserTextContent,\n} from \"./events\";\nimport type { UserInput } from \"./session\";\n\ntype AssistantContentPart = Exclude<AssistantContent, string>[number];\ntype ToolContentPart = ToolModelMessage[\"content\"][number];\ntype ModelEvent = AssistantReasoning | AssistantText | ToolCall | ToolResult;\n\n// UserInput -> AI SDK UserModelMessage\nexport function userInputToModelMessage(input: UserInput): UserModelMessage {\n if (input.type === \"user-message\") {\n return userMessageToModelMessage(input);\n }\n\n return userTextToModelMessage(input);\n}\n\nexport function userTextToModelMessage(input: UserText): UserModelMessage {\n return { role: \"user\", content: userTextContentToUserContent(input.text) };\n}\n\nfunction userTextContentToUserContent(\n text: UserTextContent\n): UserModelMessage[\"content\"] {\n if (typeof text === \"string\") {\n return text;\n }\n\n return text.map((part) => ({ type: \"text\", text: part }));\n}\n\nexport function userMessageToModelMessage(\n input: UserMessage\n): UserModelMessage {\n return {\n role: \"user\",\n content: userMessageContentToUserContent(input.content),\n };\n}\n\nfunction userMessageContentToUserContent(\n content: UserMessageContent\n): Exclude<UserModelMessage[\"content\"], string> {\n return content.map(userMessageContentPartToUserContentPart);\n}\n\nfunction userMessageContentPartToUserContentPart(\n part: UserMessageContentPart\n): Exclude<UserModelMessage[\"content\"], string>[number] {\n if (part.type === \"text\") {\n return { type: \"text\", text: part.text };\n }\n\n if (part.type === \"image\") {\n return {\n type: \"file\",\n data: part.image,\n mediaType: part.mediaType ?? \"image\",\n };\n }\n\n return {\n type: \"file\",\n data: userMessageFileDataToFileData(part.data),\n mediaType: part.mediaType,\n ...(part.filename === undefined ? {} : { filename: part.filename }),\n };\n}\n\nfunction userMessageFileDataToFileData(\n data: UserMessageFileData\n): Extract<\n Exclude<UserModelMessage[\"content\"], string>[number],\n { type: \"file\" }\n>[\"data\"] {\n if (typeof data === \"string\") {\n return data;\n }\n\n if (data.type === \"url\") {\n return data.url;\n }\n\n if (data.type === \"data\") {\n return { type: \"data\", data: data.data };\n }\n\n if (data.type === \"reference\") {\n return { type: \"reference\", reference: { ...data.reference } };\n }\n\n return { type: \"text\", text: data.text };\n}\n\n// AI SDK ModelMessage -> public agent events\nexport function modelMessageToAgentEvents(message: ModelMessage): ModelEvent[] {\n if (message.role === \"assistant\") {\n return assistantReasoningFirstParts(assistantContentParts(message)).flatMap(\n assistantContentPartToEvents\n );\n }\n\n if (message.role === \"tool\") {\n return message.content.flatMap(toolContentPartToEvents);\n }\n\n return [];\n}\n\nfunction assistantContentParts(\n message: AssistantModelMessage\n): AssistantContentPart[] {\n return typeof message.content === \"string\"\n ? [{ type: \"text\", text: message.content }]\n : message.content;\n}\n\nfunction assistantReasoningFirstParts(\n parts: AssistantContentPart[]\n): AssistantContentPart[] {\n return [\n ...parts.filter((part) => part.type === \"reasoning\"),\n ...parts.filter((part) => part.type !== \"reasoning\"),\n ];\n}\n\nfunction assistantContentPartToEvents(\n part: AssistantContentPart\n): ModelEvent[] {\n if (part.type === \"text\") {\n return part.text ? [{ type: \"assistant-text\", text: part.text }] : [];\n }\n\n if (part.type === \"reasoning\") {\n return part.text ? [{ type: \"assistant-reasoning\", text: part.text }] : [];\n }\n\n if (part.type === \"tool-call\") {\n return [\n {\n type: \"tool-call\",\n input: part.input,\n toolCallId: part.toolCallId,\n toolName: part.toolName,\n },\n ];\n }\n\n return [];\n}\n\nfunction toolContentPartToEvents(part: ToolContentPart): ModelEvent[] {\n if (part.type === \"tool-result\") {\n return toolResultPartToEvents(part);\n }\n\n return [];\n}\n\nfunction toolResultPartToEvents(part: {\n output: unknown;\n toolCallId: string;\n toolName: string;\n type: \"tool-result\";\n}): ModelEvent[] {\n return [\n {\n type: \"tool-result\",\n output: part.output,\n toolCallId: part.toolCallId,\n toolName: part.toolName,\n },\n ];\n}\n"],"mappings":";AA0BA,SAAgB,wBAAwB,OAAoC;CAC1E,IAAI,MAAM,SAAS,gBACjB,OAAO,0BAA0B,KAAK;CAGxC,OAAO,uBAAuB,KAAK;AACrC;AAEA,SAAgB,uBAAuB,OAAmC;CACxE,OAAO;EAAE,MAAM;EAAQ,SAAS,6BAA6B,MAAM,IAAI;CAAE;AAC3E;AAEA,SAAS,6BACP,MAC6B;CAC7B,IAAI,OAAO,SAAS,UAClB,OAAO;CAGT,OAAO,KAAK,KAAK,UAAU;EAAE,MAAM;EAAQ,MAAM;CAAK,EAAE;AAC1D;AAEA,SAAgB,0BACd,OACkB;CAClB,OAAO;EACL,MAAM;EACN,SAAS,gCAAgC,MAAM,OAAO;CACxD;AACF;AAEA,SAAS,gCACP,SAC8C;CAC9C,OAAO,QAAQ,IAAI,uCAAuC;AAC5D;AAEA,SAAS,wCACP,MACsD;CACtD,IAAI,KAAK,SAAS,QAChB,OAAO;EAAE,MAAM;EAAQ,MAAM,KAAK;CAAK;CAGzC,IAAI,KAAK,SAAS,SAChB,OAAO;E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AAK;CAAK,CAAC,IAAI,CAAC;CAG3E,IAAI,KAAK,SAAS,aAChB,OAAO,CACL;EACE,MAAM;EACN,OAAO,KAAK;EACZ,YAAY,KAAK;EACjB,UAAU,KAAK;CACjB,CACF;CAGF,OAAO,CAAC;AACV;AAEA,SAAS,wBAAwB,MAAqC;CACpE,IAAI,KAAK,SAAS,eAChB,OAAO,uBAAuB,IAAI;CAGpC,OAAO,CAAC;AACV;AAEA,SAAS,uBAAuB,MAKf;CACf,OAAO,CACL;EACE,MAAM;EACN,QAAQ,KAAK;EACb,YAAY,KAAK;EACjB,UAAU,KAAK;CACjB,CACF;AACF"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { UserText } from "./events.js";
|
|
1
|
+
import { UserMessage, UserMessageContentPart, UserText } from "./events.js";
|
|
2
2
|
import { AgentRun } from "./run.js";
|
|
3
|
-
|
|
4
3
|
//#region src/session/session.d.ts
|
|
5
|
-
type
|
|
4
|
+
type UserInput = UserMessage | UserText;
|
|
5
|
+
type AgentInput = readonly string[] | readonly UserMessageContentPart[] | string | UserInput;
|
|
6
|
+
type SessionInput = AgentInput;
|
|
6
7
|
//#endregion
|
|
7
|
-
export { AgentInput };
|
|
8
|
+
export { AgentInput, SessionInput, UserInput };
|
|
8
9
|
//# sourceMappingURL=session.d.ts.map
|
package/dist/session/session.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { runAgentLoop } from "../agent-loop.js";
|
|
2
2
|
import { AgentModelHistory } from "./history.js";
|
|
3
3
|
import { BufferedAgentRun } from "./run.js";
|
|
4
|
+
import { decodeStoredSessionSnapshot, encodeSessionSnapshot } from "./snapshot.js";
|
|
4
5
|
//#region src/session/session.ts
|
|
5
6
|
var AgentSession = class {
|
|
7
|
+
#hooks;
|
|
6
8
|
#inputQueue = [];
|
|
7
9
|
#llm;
|
|
8
10
|
#persistence;
|
|
@@ -13,7 +15,8 @@ var AgentSession = class {
|
|
|
13
15
|
#loaded = false;
|
|
14
16
|
#running = false;
|
|
15
17
|
#storeVersion;
|
|
16
|
-
constructor(llm, persistence) {
|
|
18
|
+
constructor(llm, persistence, hooks) {
|
|
19
|
+
this.#hooks = hooks;
|
|
17
20
|
this.#llm = llm;
|
|
18
21
|
this.#persistence = persistence;
|
|
19
22
|
}
|
|
@@ -71,7 +74,7 @@ var AgentSession = class {
|
|
|
71
74
|
async #replaceWithStoredSession() {
|
|
72
75
|
const stored = await this.#persistence.store.load(this.#persistence.key);
|
|
73
76
|
this.#storeVersion = stored?.version;
|
|
74
|
-
this.#history = new AgentModelHistory(
|
|
77
|
+
this.#history = new AgentModelHistory(decodeStoredSessionSnapshot(stored));
|
|
75
78
|
}
|
|
76
79
|
async #drainInputQueue() {
|
|
77
80
|
if (this.#running) return;
|
|
@@ -89,16 +92,28 @@ var AgentSession = class {
|
|
|
89
92
|
this.#activeAbort = new AbortController();
|
|
90
93
|
const historySnapshot = this.#history.modelSnapshot();
|
|
91
94
|
try {
|
|
95
|
+
await this.#hooks?.beforeTurn?.({
|
|
96
|
+
history: this.#history.modelSnapshot(),
|
|
97
|
+
input,
|
|
98
|
+
signal: this.#activeAbort.signal
|
|
99
|
+
});
|
|
92
100
|
run.emit({ type: "turn-start" });
|
|
93
101
|
this.#history.appendUserInput(input);
|
|
94
102
|
await this.#commitHistory();
|
|
95
103
|
const result = await runAgentLoop({
|
|
96
104
|
emit: (event) => run.emit(event),
|
|
97
105
|
history: this.#history,
|
|
106
|
+
hooks: this.#hooks,
|
|
98
107
|
llm: this.#llm,
|
|
99
108
|
signal: this.#activeAbort.signal
|
|
100
109
|
});
|
|
101
110
|
await this.#commitHistory();
|
|
111
|
+
await runAfterTurnHook(this.#hooks, {
|
|
112
|
+
history: this.#history.modelSnapshot(),
|
|
113
|
+
input,
|
|
114
|
+
result,
|
|
115
|
+
signal: this.#activeAbort.signal
|
|
116
|
+
});
|
|
102
117
|
run.emit({ type: result === "aborted" ? "turn-abort" : "turn-end" });
|
|
103
118
|
} catch (error) {
|
|
104
119
|
if (error instanceof SessionCommitConflictError) {
|
|
@@ -129,7 +144,7 @@ var AgentSession = class {
|
|
|
129
144
|
}
|
|
130
145
|
async #commitHistory() {
|
|
131
146
|
const result = await this.#persistence.store.commit(this.#persistence.key, {
|
|
132
|
-
state:
|
|
147
|
+
state: encodeSessionSnapshot(this.#history.modelSnapshot()),
|
|
133
148
|
version: this.#storeVersion
|
|
134
149
|
}, { expectedVersion: this.#storeVersion ?? null });
|
|
135
150
|
if (!result.ok) {
|
|
@@ -139,6 +154,11 @@ var AgentSession = class {
|
|
|
139
154
|
this.#storeVersion = result.version;
|
|
140
155
|
}
|
|
141
156
|
};
|
|
157
|
+
async function runAfterTurnHook(hooks, context) {
|
|
158
|
+
const hook = hooks?.afterTurn;
|
|
159
|
+
if (!hook) return;
|
|
160
|
+
await Promise.allSettled([Promise.resolve().then(() => hook(context))]);
|
|
161
|
+
}
|
|
142
162
|
function normalizeAgentInput(input) {
|
|
143
163
|
if (typeof input === "string") return {
|
|
144
164
|
type: "user-text",
|
|
@@ -148,22 +168,43 @@ function normalizeAgentInput(input) {
|
|
|
148
168
|
type: "user-text",
|
|
149
169
|
text: structuredClone(input)
|
|
150
170
|
};
|
|
171
|
+
if (isArrayInput(input)) {
|
|
172
|
+
assertUserMessageContent(input);
|
|
173
|
+
return {
|
|
174
|
+
type: "user-message",
|
|
175
|
+
content: structuredClone(input)
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
if (isUserMessage(input)) assertUserMessageContent(input.content);
|
|
151
179
|
return structuredClone(input);
|
|
152
180
|
}
|
|
153
181
|
function isStringArrayInput(input) {
|
|
182
|
+
return isArrayInput(input) && input.every((part) => typeof part === "string");
|
|
183
|
+
}
|
|
184
|
+
function isArrayInput(input) {
|
|
154
185
|
return Array.isArray(input);
|
|
155
186
|
}
|
|
156
|
-
function
|
|
157
|
-
return
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
};
|
|
187
|
+
function isUserMessage(input) {
|
|
188
|
+
return input.type === "user-message";
|
|
189
|
+
}
|
|
190
|
+
function assertUserMessageContent(input) {
|
|
191
|
+
for (const part of input) if (!isUserMessageContentPart(part)) throw new TypeError("Agent input content parts must be { type: \"text\", text }, { type: \"image\", image }, or { type: \"file\", data, mediaType }.");
|
|
192
|
+
}
|
|
193
|
+
function isUserMessageContentPart(part) {
|
|
194
|
+
if (part === null || typeof part !== "object" || !("type" in part)) return false;
|
|
195
|
+
if (part.type === "text") return "text" in part && typeof part.text === "string";
|
|
196
|
+
if (part.type === "image") return "image" in part && typeof part.image === "string" && (!("mediaType" in part) || typeof part.mediaType === "string");
|
|
197
|
+
if (part.type === "file") return "data" in part && isUserMessageFileData(part.data) && "mediaType" in part && typeof part.mediaType === "string" && (!("filename" in part) || typeof part.filename === "string");
|
|
198
|
+
return false;
|
|
161
199
|
}
|
|
162
|
-
function
|
|
163
|
-
if (
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
|
|
200
|
+
function isUserMessageFileData(data) {
|
|
201
|
+
if (typeof data === "string") return true;
|
|
202
|
+
if (data === null || typeof data !== "object" || !("type" in data)) return false;
|
|
203
|
+
if (data.type === "data") return "data" in data && typeof data.data === "string";
|
|
204
|
+
if (data.type === "reference") return "reference" in data && data.reference !== null && typeof data.reference === "object" && Object.values(data.reference).every((value) => typeof value === "string");
|
|
205
|
+
if (data.type === "text") return "text" in data && typeof data.text === "string";
|
|
206
|
+
if (data.type === "url") return "url" in data && typeof data.url === "string";
|
|
207
|
+
return false;
|
|
167
208
|
}
|
|
168
209
|
function errorMessage(error) {
|
|
169
210
|
if (error instanceof Error) return error.message;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","names":["#inputQueue","#llm","#persistence","#killed","#ensureLoaded","#drainInputQueue","#activeAbort","#loaded","#loadPromise","#loadSessionState","#replaceWithStoredSession","#storeVersion","#history","#running","#processQueuedInput","#commitHistory"],"sources":["../../src/session/session.ts"],"sourcesContent":["import { runAgentLoop } from \"../agent-loop\";\nimport type { AgentMessage, Llm } from \"../llm\";\nimport type { UserText } from \"./events\";\nimport { AgentModelHistory } from \"./history\";\nimport type { AgentRun } from \"./run\";\nimport { BufferedAgentRun } from \"./run\";\nimport type { SessionStore, StoredSession } from \"./store/types\";\n\nexport type AgentInput = string | readonly string[] | UserText;\nexport type SessionInput = AgentInput;\nexport type { AgentRun } from \"./run\";\n\ninterface SessionPersistenceOptions {\n readonly key: string;\n readonly store: SessionStore;\n}\n\ninterface QueuedInput {\n readonly input: UserText;\n readonly run: BufferedAgentRun;\n}\n\ninterface EncodedSessionState {\n readonly history: AgentMessage[];\n readonly schemaVersion: 1;\n}\n\nexport class AgentSession {\n readonly #inputQueue: QueuedInput[] = [];\n readonly #llm: Llm;\n readonly #persistence: SessionPersistenceOptions;\n #activeAbort?: AbortController;\n #history = new AgentModelHistory();\n #killed = false;\n #loadPromise?: Promise<void>;\n #loaded = false;\n #running = false;\n #storeVersion: string | undefined;\n\n constructor(llm: Llm, persistence: SessionPersistenceOptions) {\n this.#llm = llm;\n this.#persistence = persistence;\n }\n\n async send(input: AgentInput): Promise<AgentRun> {\n if (this.#killed) {\n throw sessionKilledError();\n }\n\n await this.#ensureLoaded();\n\n if (this.#killed) {\n throw sessionKilledError();\n }\n\n const acceptedInput = normalizeAgentInput(input);\n const run = new BufferedAgentRun();\n run.emit(acceptedInput);\n this.#inputQueue.push({ input: structuredClone(acceptedInput), run });\n this.#drainInputQueue().catch((error: unknown) => {\n run.emit({ type: \"turn-error\", message: errorMessage(error) });\n run.close();\n });\n return run;\n }\n\n interrupt(): void {\n this.#activeAbort?.abort();\n }\n\n kill(): void {\n if (this.#killed) {\n return;\n }\n\n this.#killed = true;\n this.#activeAbort?.abort();\n\n while (this.#inputQueue.length > 0) {\n const item = this.#inputQueue.shift();\n item?.run.emit({\n type: \"turn-error\",\n message: sessionKilledError().message,\n });\n item?.run.close();\n }\n }\n\n async #ensureLoaded(): Promise<void> {\n if (this.#loaded) {\n return;\n }\n\n this.#loadPromise ??= this.#loadSessionState();\n try {\n await this.#loadPromise;\n } catch (error) {\n this.#loadPromise = undefined;\n throw error;\n }\n }\n\n async #loadSessionState(): Promise<void> {\n if (this.#loaded) {\n return;\n }\n\n await this.#replaceWithStoredSession();\n this.#loaded = true;\n }\n\n async #replaceWithStoredSession(): Promise<void> {\n const stored = await this.#persistence.store.load(this.#persistence.key);\n this.#storeVersion = stored?.version;\n this.#history = new AgentModelHistory(decodeStoredHistory(stored));\n }\n\n async #drainInputQueue(): Promise<void> {\n if (this.#running) {\n return;\n }\n\n this.#running = true;\n try {\n while (!this.#killed && this.#inputQueue.length > 0) {\n const item = this.#inputQueue.shift();\n if (item) {\n await this.#processQueuedInput(item);\n }\n }\n } finally {\n this.#running = false;\n }\n }\n\n async #processQueuedInput({ input, run }: QueuedInput): Promise<void> {\n this.#activeAbort = new AbortController();\n const historySnapshot = this.#history.modelSnapshot();\n\n try {\n run.emit({ type: \"turn-start\" });\n this.#history.appendUserInput(input);\n await this.#commitHistory();\n\n const result = await runAgentLoop({\n emit: (event) => run.emit(event),\n history: this.#history,\n llm: this.#llm,\n signal: this.#activeAbort.signal,\n });\n\n await this.#commitHistory();\n run.emit({ type: result === \"aborted\" ? \"turn-abort\" : \"turn-end\" });\n } catch (error) {\n if (error instanceof SessionCommitConflictError) {\n run.emit({ type: \"turn-error\", message: error.message });\n return;\n }\n\n this.#history.rollback(historySnapshot);\n try {\n await this.#commitHistory();\n } catch (rollbackError) {\n run.emit({\n type: \"turn-error\",\n message: `${errorMessage(error)}; history rollback persistence failed: ${errorMessage(\n rollbackError\n )}`,\n });\n return;\n }\n run.emit({ type: \"turn-error\", message: errorMessage(error) });\n } finally {\n run.close();\n this.#activeAbort = undefined;\n }\n }\n\n async #commitHistory(): Promise<void> {\n const result = await this.#persistence.store.commit(\n this.#persistence.key,\n {\n state: encodeHistory(this.#history.modelSnapshot()),\n version: this.#storeVersion,\n },\n { expectedVersion: this.#storeVersion ?? null }\n );\n\n if (!result.ok) {\n await this.#replaceWithStoredSession();\n throw new SessionCommitConflictError(this.#persistence.key);\n }\n\n this.#storeVersion = result.version;\n }\n}\n\nexport function normalizeAgentInput(input: AgentInput): UserText {\n if (typeof input === \"string\") {\n return {\n type: \"user-text\",\n text: input,\n };\n }\n\n if (isStringArrayInput(input)) {\n return {\n type: \"user-text\",\n text: structuredClone(input) as readonly string[],\n };\n }\n\n return structuredClone(input);\n}\n\nfunction isStringArrayInput(input: AgentInput): input is readonly string[] {\n return Array.isArray(input);\n}\n\nfunction encodeHistory(history: AgentMessage[]): EncodedSessionState {\n return { schemaVersion: 1, history };\n}\n\nfunction decodeStoredHistory(stored: StoredSession | null): AgentMessage[] {\n if (!stored) {\n return [];\n }\n\n const state = stored.state;\n if (\n state !== null &&\n typeof state === \"object\" &&\n \"schemaVersion\" in state &&\n state.schemaVersion === 1 &&\n \"history\" in state &&\n Array.isArray(state.history)\n ) {\n return structuredClone(state.history) as AgentMessage[];\n }\n\n throw new Error(\"Unsupported stored session state\");\n}\n\nfunction errorMessage(error: unknown): string {\n if (error instanceof Error) {\n return error.message;\n }\n\n return String(error);\n}\n\nfunction sessionKilledError(): Error {\n return new Error(\"Session killed\");\n}\n\nclass SessionCommitConflictError extends Error {\n constructor(key: string) {\n super(`Session ${JSON.stringify(key)} commit conflict`);\n }\n}\n"],"mappings":";;;;AA2BA,IAAa,eAAb,MAA0B;CACxB,cAAsC,CAAC;CACvC;CACA;CACA;CACA,WAAW,IAAI,kBAAkB;CACjC,UAAU;CACV;CACA,UAAU;CACV,WAAW;CACX;CAEA,YAAY,KAAU,aAAwC;EAC5D,KAAKC,OAAO;EACZ,KAAKC,eAAe;CACtB;CAEA,MAAM,KAAK,OAAsC;EAC/C,IAAI,KAAKC,SACP,MAAM,mBAAmB;EAG3B,MAAM,KAAKC,cAAc;EAEzB,IAAI,KAAKD,SACP,MAAM,mBAAmB;EAG3B,MAAM,gBAAgB,oBAAoB,KAAK;EAC/C,MAAM,MAAM,IAAI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A2B,KAAKR,aAAa,GAAG;EAC5D;EAEA,KAAKS,gBAAgB,OAAO;CAC9B;AACF;AAEA,SAAgB,oBAAoB,OAA6B;CAC/D,IAAI,OAAO,UAAU,UACnB,OAAO;EACL,MAAM;EACN,MAAM;CACR;CAGF,IAAI,mBAAmB,KAAK,GAC1B,OAAO;EACL,MAAM;EACN,MAAM,gBAAgB,KAAK;CAC7B;CAGF,OAAO,gBAAgB,KAAK;AAC9B;AAEA,SAAS,mBAAmB,OAA+C;CACzE,OAAO,MAAM,QAAQ,KAAK;AAC5B;AAEA,SAAS,cAAc,SAA8C;CACnE,OAAO;EAAE,eAAe;EAAG;CAAQ;AACrC;AAEA,SAAS,oBAAoB,QAA8C;CACzE,IAAI,CAAC,QACH,OAAO,CAAC;CAGV,MAAM,QAAQ,OAAO;CACrB,IACE,UAAU,QACV,OAAO,UAAU,YACjB,mBAAmB,SACnB,MAAM,kBAAkB,KACxB,aAAa,SACb,MAAM,QAAQ,MAAM,OAAO,GAE3B,OAAO,gBAAgB,MAAM,OAAO;CAGtC,MAAM,IAAI,MAAM,kCAAkC;AACpD;AAEA,SAAS,aAAa,OAAwB;CAC5C,IAAI,iBAAiB,OACnB,OAAO,MAAM;CAGf,OAAO,OAAO,KAAK;AACrB;AAEA,SAAS,qBAA4B;CACnC,uBAAO,IAAI,MAAM,gBAAgB;AACnC;AAEA,IAAM,6BAAN,cAAyC,MAAM;CAC7C,YAAY,KAAa;EACvB,MAAM,WAAW,KAAK,UAAU,GAAG,EAAE,iBAAiB;CACxD;AACF"}
|
|
1
|
+
{"version":3,"file":"session.js","names":["#hooks","#inputQueue","#llm","#persistence","#killed","#ensureLoaded","#drainInputQueue","#activeAbort","#loaded","#loadPromise","#loadSessionState","#replaceWithStoredSession","#storeVersion","#history","#running","#processQueuedInput","#commitHistory"],"sources":["../../src/session/session.ts"],"sourcesContent":["import { runAgentLoop } from \"../agent-loop\";\nimport type { AgentHooks } from \"../hooks\";\nimport type { Llm } from \"../llm\";\nimport type { UserMessage, UserMessageContentPart, UserText } from \"./events\";\nimport { AgentModelHistory } from \"./history\";\nimport type { AgentRun } from \"./run\";\nimport { BufferedAgentRun } from \"./run\";\nimport { decodeStoredSessionSnapshot, encodeSessionSnapshot } from \"./snapshot\";\nimport type { SessionStore } from \"./store/types\";\n\nexport type UserInput = UserMessage | UserText;\nexport type AgentInput =\n | readonly string[]\n | readonly UserMessageContentPart[]\n | string\n | UserInput;\nexport type SessionInput = AgentInput;\nexport type { AgentRun } from \"./run\";\n\ninterface SessionPersistenceOptions {\n readonly key: string;\n readonly store: SessionStore;\n}\n\ninterface QueuedInput {\n readonly input: UserInput;\n readonly run: BufferedAgentRun;\n}\n\nexport class AgentSession {\n readonly #hooks?: AgentHooks;\n readonly #inputQueue: QueuedInput[] = [];\n readonly #llm: Llm;\n readonly #persistence: SessionPersistenceOptions;\n #activeAbort?: AbortController;\n #history = new AgentModelHistory();\n #killed = false;\n #loadPromise?: Promise<void>;\n #loaded = false;\n #running = false;\n #storeVersion: string | undefined;\n\n constructor(\n llm: Llm,\n persistence: SessionPersistenceOptions,\n hooks?: AgentHooks\n ) {\n this.#hooks = hooks;\n this.#llm = llm;\n this.#persistence = persistence;\n }\n\n async send(input: AgentInput): Promise<AgentRun> {\n if (this.#killed) {\n throw sessionKilledError();\n }\n\n await this.#ensureLoaded();\n\n if (this.#killed) {\n throw sessionKilledError();\n }\n\n const acceptedInput = normalizeAgentInput(input);\n const run = new BufferedAgentRun();\n run.emit(acceptedInput);\n this.#inputQueue.push({ input: structuredClone(acceptedInput), run });\n this.#drainInputQueue().catch((error: unknown) => {\n run.emit({ type: \"turn-error\", message: errorMessage(error) });\n run.close();\n });\n return run;\n }\n\n interrupt(): void {\n this.#activeAbort?.abort();\n }\n\n kill(): void {\n if (this.#killed) {\n return;\n }\n\n this.#killed = true;\n this.#activeAbort?.abort();\n\n while (this.#inputQueue.length > 0) {\n const item = this.#inputQueue.shift();\n item?.run.emit({\n type: \"turn-error\",\n message: sessionKilledError().message,\n });\n item?.run.close();\n }\n }\n\n async #ensureLoaded(): Promise<void> {\n if (this.#loaded) {\n return;\n }\n\n this.#loadPromise ??= this.#loadSessionState();\n try {\n await this.#loadPromise;\n } catch (error) {\n this.#loadPromise = undefined;\n throw error;\n }\n }\n\n async #loadSessionState(): Promise<void> {\n if (this.#loaded) {\n return;\n }\n\n await this.#replaceWithStoredSession();\n this.#loaded = true;\n }\n\n async #replaceWithStoredSession(): Promise<void> {\n const stored = await this.#persistence.store.load(this.#persistence.key);\n this.#storeVersion = stored?.version;\n this.#history = new AgentModelHistory(decodeStoredSessionSnapshot(stored));\n }\n\n async #drainInputQueue(): Promise<void> {\n if (this.#running) {\n return;\n }\n\n this.#running = true;\n try {\n while (!this.#killed && this.#inputQueue.length > 0) {\n const item = this.#inputQueue.shift();\n if (item) {\n await this.#processQueuedInput(item);\n }\n }\n } finally {\n this.#running = false;\n }\n }\n\n async #processQueuedInput({ input, run }: QueuedInput): Promise<void> {\n this.#activeAbort = new AbortController();\n const historySnapshot = this.#history.modelSnapshot();\n\n try {\n await this.#hooks?.beforeTurn?.({\n history: this.#history.modelSnapshot(),\n input,\n signal: this.#activeAbort.signal,\n });\n run.emit({ type: \"turn-start\" });\n this.#history.appendUserInput(input);\n await this.#commitHistory();\n\n const result = await runAgentLoop({\n emit: (event) => run.emit(event),\n history: this.#history,\n hooks: this.#hooks,\n llm: this.#llm,\n signal: this.#activeAbort.signal,\n });\n\n await this.#commitHistory();\n await runAfterTurnHook(this.#hooks, {\n history: this.#history.modelSnapshot(),\n input,\n result,\n signal: this.#activeAbort.signal,\n });\n run.emit({ type: result === \"aborted\" ? \"turn-abort\" : \"turn-end\" });\n } catch (error) {\n if (error instanceof SessionCommitConflictError) {\n run.emit({ type: \"turn-error\", message: error.message });\n return;\n }\n\n this.#history.rollback(historySnapshot);\n try {\n await this.#commitHistory();\n } catch (rollbackError) {\n run.emit({\n type: \"turn-error\",\n message: `${errorMessage(error)}; history rollback persistence failed: ${errorMessage(\n rollbackError\n )}`,\n });\n return;\n }\n run.emit({ type: \"turn-error\", message: errorMessage(error) });\n } finally {\n run.close();\n this.#activeAbort = undefined;\n }\n }\n\n async #commitHistory(): Promise<void> {\n const result = await this.#persistence.store.commit(\n this.#persistence.key,\n {\n state: encodeSessionSnapshot(this.#history.modelSnapshot()),\n version: this.#storeVersion,\n },\n { expectedVersion: this.#storeVersion ?? null }\n );\n\n if (!result.ok) {\n await this.#replaceWithStoredSession();\n throw new SessionCommitConflictError(this.#persistence.key);\n }\n\n this.#storeVersion = result.version;\n }\n}\n\nasync function runAfterTurnHook(\n hooks: AgentHooks | undefined,\n context: Parameters<NonNullable<AgentHooks[\"afterTurn\"]>>[0]\n): Promise<void> {\n const hook = hooks?.afterTurn;\n if (!hook) {\n return;\n }\n\n await Promise.allSettled([Promise.resolve().then(() => hook(context))]);\n}\n\nexport function normalizeAgentInput(input: AgentInput): UserInput {\n if (typeof input === \"string\") {\n return {\n type: \"user-text\",\n text: input,\n };\n }\n\n if (isStringArrayInput(input)) {\n return {\n type: \"user-text\",\n text: structuredClone(input) as readonly string[],\n };\n }\n\n if (isArrayInput(input)) {\n assertUserMessageContent(input);\n return {\n type: \"user-message\",\n content: structuredClone(input) as readonly UserMessageContentPart[],\n };\n }\n\n if (isUserMessage(input)) {\n assertUserMessageContent(input.content);\n }\n\n return structuredClone(input);\n}\n\nfunction isStringArrayInput(input: AgentInput): input is readonly string[] {\n return isArrayInput(input) && input.every((part) => typeof part === \"string\");\n}\n\nfunction isArrayInput(\n input: AgentInput\n): input is readonly string[] | readonly UserMessageContentPart[] {\n return Array.isArray(input);\n}\n\nfunction isUserMessage(input: UserInput): input is UserMessage {\n return input.type === \"user-message\";\n}\n\nfunction assertUserMessageContent(\n input: readonly unknown[]\n): asserts input is readonly UserMessageContentPart[] {\n for (const part of input) {\n if (!isUserMessageContentPart(part)) {\n throw new TypeError(\n 'Agent input content parts must be { type: \"text\", text }, { type: \"image\", image }, or { type: \"file\", data, mediaType }.'\n );\n }\n }\n}\n\nfunction isUserMessageContentPart(\n part: unknown\n): part is UserMessageContentPart {\n if (part === null || typeof part !== \"object\" || !(\"type\" in part)) {\n return false;\n }\n\n if (part.type === \"text\") {\n return \"text\" in part && typeof part.text === \"string\";\n }\n\n if (part.type === \"image\") {\n return (\n \"image\" in part &&\n typeof part.image === \"string\" &&\n (!(\"mediaType\" in part) || typeof part.mediaType === \"string\")\n );\n }\n\n if (part.type === \"file\") {\n return (\n \"data\" in part &&\n isUserMessageFileData(part.data) &&\n \"mediaType\" in part &&\n typeof part.mediaType === \"string\" &&\n (!(\"filename\" in part) || typeof part.filename === \"string\")\n );\n }\n\n return false;\n}\n\nfunction isUserMessageFileData(data: unknown): boolean {\n if (typeof data === \"string\") {\n return true;\n }\n\n if (data === null || typeof data !== \"object\" || !(\"type\" in data)) {\n return false;\n }\n\n if (data.type === \"data\") {\n return \"data\" in data && typeof data.data === \"string\";\n }\n\n if (data.type === \"reference\") {\n return (\n \"reference\" in data &&\n data.reference !== null &&\n typeof data.reference === \"object\" &&\n Object.values(data.reference).every((value) => typeof value === \"string\")\n );\n }\n\n if (data.type === \"text\") {\n return \"text\" in data && typeof data.text === \"string\";\n }\n\n if (data.type === \"url\") {\n return \"url\" in data && typeof data.url === \"string\";\n }\n\n return false;\n}\n\nfunction errorMessage(error: unknown): string {\n if (error instanceof Error) {\n return error.message;\n }\n\n return String(error);\n}\n\nfunction sessionKilledError(): Error {\n return new Error(\"Session killed\");\n}\n\nclass SessionCommitConflictError extends Error {\n constructor(key: string) {\n super(`Session ${JSON.stringify(key)} commit conflict`);\n }\n}\n"],"mappings":";;;;;AA6BA,IAAa,eAAb,MAA0B;CACxB;CACA,cAAsC,CAAC;CACvC;CACA;CACA;CACA,WAAW,IAAI,kBAAkB;CACjC,UAAU;CACV;CACA,UAAU;CACV,WAAW;CACX;CAEA,YACE,KACA,aACA,OACA;EACA,KAAKA,SAAS;EACd,KAAKE,OAAO;EACZ,KAAKC,eAAe;CAC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AM,gBAAgB;AACnC;AAEA,IAAM,6BAAN,cAAyC,MAAM;CAC7C,YAAY,KAAa;EACvB,MAAM,WAAW,KAAK,UAAU,GAAG,EAAE,iBAAiB;CACxD;AACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/session/snapshot.ts
|
|
2
|
+
function encodeSessionSnapshot(history) {
|
|
3
|
+
return {
|
|
4
|
+
schemaVersion: 1,
|
|
5
|
+
history: structuredClone(history)
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
function decodeStoredSessionSnapshot(stored) {
|
|
9
|
+
if (!stored) return [];
|
|
10
|
+
const snapshot = stored.state;
|
|
11
|
+
if (isSessionSnapshotV1(snapshot)) return structuredClone(snapshot.history);
|
|
12
|
+
throw new Error("Unsupported stored session state");
|
|
13
|
+
}
|
|
14
|
+
function isSessionSnapshotV1(value) {
|
|
15
|
+
return value !== null && typeof value === "object" && "schemaVersion" in value && value.schemaVersion === 1 && "history" in value && Array.isArray(value.history);
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { decodeStoredSessionSnapshot, encodeSessionSnapshot };
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=snapshot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snapshot.js","names":[],"sources":["../../src/session/snapshot.ts"],"sourcesContent":["import type { AgentMessage } from \"../llm\";\nimport type { StoredSession } from \"./store/types\";\n\nexport interface AgentSessionSnapshotV1 {\n readonly history: AgentMessage[];\n readonly schemaVersion: 1;\n}\n\nexport type AgentSessionSnapshot = AgentSessionSnapshotV1;\n\nexport function encodeSessionSnapshot(\n history: AgentMessage[]\n): AgentSessionSnapshot {\n return { schemaVersion: 1, history: structuredClone(history) };\n}\n\nexport function decodeStoredSessionSnapshot(\n stored: StoredSession | null\n): AgentMessage[] {\n if (!stored) {\n return [];\n }\n\n const snapshot = stored.state;\n if (isSessionSnapshotV1(snapshot)) {\n return structuredClone(snapshot.history);\n }\n\n throw new Error(\"Unsupported stored session state\");\n}\n\nfunction isSessionSnapshotV1(value: unknown): value is AgentSessionSnapshotV1 {\n return (\n value !== null &&\n typeof value === \"object\" &&\n \"schemaVersion\" in value &&\n value.schemaVersion === 1 &&\n \"history\" in value &&\n Array.isArray(value.history)\n );\n}\n"],"mappings":";AAUA,SAAgB,sBACd,SACsB;CACtB,OAAO;EAAE,eAAe;EAAG,SAAS,gBAAgB,OAAO;CAAE;AAC/D;AAEA,SAAgB,4BACd,QACgB;CAChB,IAAI,CAAC,QACH,OAAO,CAAC;CAGV,MAAM,WAAW,OAAO;CACxB,IAAI,oBAAoB,QAAQ,GAC9B,OAAO,gBAAgB,SAAS,OAAO;CAGzC,MAAM,IAAI,MAAM,kCAAkC;AACpD;AAEA,SAAS,oBAAoB,OAAiD;CAC5E,OACE,UAAU,QACV,OAAO,UAAU,YACjB,mBAAmB,SACnB,MAAM,kBAAkB,KACxB,aAAa,SACb,MAAM,QAAQ,MAAM,OAAO;AAE/B"}
|