@minpeter/pss-runtime 0.0.3 → 0.0.5
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 +53 -107
- package/dist/agent-loop.d.ts +20 -12
- package/dist/agent-loop.js +45 -48
- package/dist/agent-loop.js.map +1 -1
- package/dist/agent.d.ts +34 -17
- package/dist/agent.js +50 -31
- package/dist/agent.js.map +1 -1
- package/dist/index.d.ts +8 -7
- package/dist/index.js +4 -5
- package/dist/llm.d.ts +30 -31
- package/dist/llm.js +15 -12
- package/dist/llm.js.map +1 -1
- package/dist/session/events.d.ts +42 -61
- package/dist/session/history.js +31 -29
- package/dist/session/history.js.map +1 -1
- package/dist/session/mapping.js +47 -53
- package/dist/session/mapping.js.map +1 -1
- package/dist/session/run.d.ts +9 -0
- package/dist/session/run.js +78 -0
- package/dist/session/run.js.map +1 -0
- package/dist/session/session.d.ts +7 -16
- package/dist/session/session.js +172 -321
- package/dist/session/session.js.map +1 -1
- package/dist/session/store/file.d.ts +14 -0
- package/dist/session/store/file.js +106 -0
- package/dist/session/store/file.js.map +1 -0
- package/dist/session/store/memory.d.ts +13 -0
- package/dist/session/store/memory.js +31 -0
- package/dist/session/store/memory.js.map +1 -0
- package/dist/session/store/types.d.ts +22 -0
- package/package.json +16 -2
- package/dist/agent-loop.d.ts.map +0 -1
- package/dist/agent.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/llm.d.ts.map +0 -1
- package/dist/session/events.d.ts.map +0 -1
- package/dist/session/events.js +0 -2
- package/dist/session/events.js.map +0 -1
- package/dist/session/history.d.ts +0 -11
- package/dist/session/history.d.ts.map +0 -1
- package/dist/session/mapping.d.ts +0 -7
- package/dist/session/mapping.d.ts.map +0 -1
- package/dist/session/session.d.ts.map +0 -1
package/dist/llm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm.js","
|
|
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":";;AAsCA,SAAgB,UAAU,EACxB,OACA,cACA,SACwB;CACxB,OAAO,OAAO,EAAE,SAAS,aAAa;EACpC,MAAM,EAAE,qBAAqB,MAAM,aAAa;GAC9C,aAAa;GACb;GACA,UAAU,CAAC,GAAG,OAAO;GACrB;GACA;EACF,CAAC;EAED,OAAO;CACT;AACF"}
|
package/dist/session/events.d.ts
CHANGED
|
@@ -1,63 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
/** User input was accepted into the session queue. */
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
| {
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
| {
|
|
39
|
-
|
|
40
|
-
message: string;
|
|
41
|
-
}
|
|
42
|
-
/** The active turn completed normally. */
|
|
43
|
-
| {
|
|
44
|
-
type: "turn-end";
|
|
45
|
-
}
|
|
46
|
-
/** One model/tool-loop iteration started within the active turn. */
|
|
47
|
-
| {
|
|
48
|
-
type: "step-start";
|
|
49
|
-
}
|
|
50
|
-
/** The model produced reasoning content. */
|
|
51
|
-
| AssistantReasoning
|
|
52
|
-
/** The model produced visible assistant text. */
|
|
53
|
-
| AssistantText
|
|
54
|
-
/** The model requested a tool call. */
|
|
55
|
-
| ToolCall
|
|
56
|
-
/** A tool call returned a result. */
|
|
57
|
-
| ToolResult
|
|
58
|
-
/** One model/tool-loop iteration finished within the active turn. */
|
|
59
|
-
| {
|
|
60
|
-
type: "step-end";
|
|
1
|
+
//#region src/session/events.d.ts
|
|
2
|
+
type UserTextContent = string | readonly string[];
|
|
3
|
+
interface UserText {
|
|
4
|
+
text: UserTextContent;
|
|
5
|
+
type: "user-text";
|
|
6
|
+
}
|
|
7
|
+
interface AssistantText {
|
|
8
|
+
text: string;
|
|
9
|
+
type: "assistant-text";
|
|
10
|
+
}
|
|
11
|
+
interface AssistantReasoning {
|
|
12
|
+
text: string;
|
|
13
|
+
type: "assistant-reasoning";
|
|
14
|
+
}
|
|
15
|
+
interface ToolCall {
|
|
16
|
+
input: unknown;
|
|
17
|
+
toolCallId: string;
|
|
18
|
+
toolName: string;
|
|
19
|
+
type: "tool-call";
|
|
20
|
+
}
|
|
21
|
+
interface ToolResult {
|
|
22
|
+
output: unknown;
|
|
23
|
+
toolCallId: string;
|
|
24
|
+
toolName: string;
|
|
25
|
+
type: "tool-result";
|
|
26
|
+
}
|
|
27
|
+
type AgentEvent = /** User input was accepted into the session queue. */UserText /** A queued user input started running as a turn. */ | {
|
|
28
|
+
type: "turn-start";
|
|
29
|
+
} /** The active turn was interrupted before normal completion. */ | {
|
|
30
|
+
type: "turn-abort";
|
|
31
|
+
} /** The active turn hit an unrecoverable runtime failure. */ | {
|
|
32
|
+
type: "turn-error";
|
|
33
|
+
message: string;
|
|
34
|
+
} /** The active turn completed normally. */ | {
|
|
35
|
+
type: "turn-end";
|
|
36
|
+
} /** One model/tool-loop iteration started within the active turn. */ | {
|
|
37
|
+
type: "step-start";
|
|
38
|
+
} /** 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. */ | {
|
|
39
|
+
type: "step-end";
|
|
61
40
|
};
|
|
62
|
-
|
|
41
|
+
type AgentEventListener = (event: AgentEvent) => void;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, ToolCall, ToolResult, UserText, UserTextContent };
|
|
63
44
|
//# sourceMappingURL=events.d.ts.map
|
package/dist/session/history.js
CHANGED
|
@@ -1,31 +1,33 @@
|
|
|
1
1
|
import { userTextToModelMessage } from "./mapping.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
2
|
+
//#region src/session/history.ts
|
|
3
|
+
var AgentModelHistory = class {
|
|
4
|
+
#modelHistory = [];
|
|
5
|
+
#onChange;
|
|
6
|
+
constructor(history, onChange) {
|
|
7
|
+
if (history) this.#modelHistory = structuredClone(history);
|
|
8
|
+
this.#onChange = onChange;
|
|
9
|
+
}
|
|
10
|
+
modelSnapshot() {
|
|
11
|
+
return structuredClone(this.#modelHistory);
|
|
12
|
+
}
|
|
13
|
+
appendUserInput(input) {
|
|
14
|
+
this.#modelHistory.push(userTextToModelMessage(input));
|
|
15
|
+
this.#triggerChange();
|
|
16
|
+
}
|
|
17
|
+
appendModelMessage(message) {
|
|
18
|
+
this.#modelHistory.push(structuredClone(message));
|
|
19
|
+
this.#triggerChange();
|
|
20
|
+
}
|
|
21
|
+
rollback(snapshot) {
|
|
22
|
+
this.#modelHistory.length = 0;
|
|
23
|
+
this.#modelHistory.push(...structuredClone(snapshot));
|
|
24
|
+
this.#triggerChange();
|
|
25
|
+
}
|
|
26
|
+
#triggerChange() {
|
|
27
|
+
this.#onChange?.(this.modelSnapshot());
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
export { AgentModelHistory };
|
|
32
|
+
|
|
31
33
|
//# sourceMappingURL=history.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.js","
|
|
1
|
+
{"version":3,"file":"history.js","names":["#modelHistory","#onChange","#triggerChange"],"sources":["../../src/session/history.ts"],"sourcesContent":["import type { ModelMessage } from \"ai\";\nimport type { UserText } from \"./events\";\nimport { userTextToModelMessage } from \"./mapping\";\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: UserText): void {\n this.#modelHistory.push(userTextToModelMessage(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,OAAuB;EACrC,KAAKA,cAAc,KAAK,uBAAuB,KAAK,CAAC;EACrD,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,67 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
//#region src/session/mapping.ts
|
|
2
|
+
function userTextToModelMessage(input) {
|
|
3
|
+
return {
|
|
4
|
+
role: "user",
|
|
5
|
+
content: userTextContentToUserContent(input.text)
|
|
6
|
+
};
|
|
4
7
|
}
|
|
5
8
|
function userTextContentToUserContent(text) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
if (typeof text === "string") return text;
|
|
10
|
+
return text.map((part) => ({
|
|
11
|
+
type: "text",
|
|
12
|
+
text: part
|
|
13
|
+
}));
|
|
10
14
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
if (message.role === "tool") {
|
|
17
|
-
return message.content.flatMap(toolContentPartToEvents);
|
|
18
|
-
}
|
|
19
|
-
return [];
|
|
15
|
+
function modelMessageToAgentEvents(message) {
|
|
16
|
+
if (message.role === "assistant") return assistantReasoningFirstParts(assistantContentParts(message)).flatMap(assistantContentPartToEvents);
|
|
17
|
+
if (message.role === "tool") return message.content.flatMap(toolContentPartToEvents);
|
|
18
|
+
return [];
|
|
20
19
|
}
|
|
21
20
|
function assistantContentParts(message) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
return typeof message.content === "string" ? [{
|
|
22
|
+
type: "text",
|
|
23
|
+
text: message.content
|
|
24
|
+
}] : message.content;
|
|
25
25
|
}
|
|
26
26
|
function assistantReasoningFirstParts(parts) {
|
|
27
|
-
|
|
28
|
-
...parts.filter((part) => part.type === "reasoning"),
|
|
29
|
-
...parts.filter((part) => part.type !== "reasoning"),
|
|
30
|
-
];
|
|
27
|
+
return [...parts.filter((part) => part.type === "reasoning"), ...parts.filter((part) => part.type !== "reasoning")];
|
|
31
28
|
}
|
|
32
29
|
function assistantContentPartToEvents(part) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
return [];
|
|
30
|
+
if (part.type === "text") return part.text ? [{
|
|
31
|
+
type: "assistant-text",
|
|
32
|
+
text: part.text
|
|
33
|
+
}] : [];
|
|
34
|
+
if (part.type === "reasoning") return part.text ? [{
|
|
35
|
+
type: "assistant-reasoning",
|
|
36
|
+
text: part.text
|
|
37
|
+
}] : [];
|
|
38
|
+
if (part.type === "tool-call") return [{
|
|
39
|
+
type: "tool-call",
|
|
40
|
+
input: part.input,
|
|
41
|
+
toolCallId: part.toolCallId,
|
|
42
|
+
toolName: part.toolName
|
|
43
|
+
}];
|
|
44
|
+
return [];
|
|
50
45
|
}
|
|
51
46
|
function toolContentPartToEvents(part) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
return [];
|
|
47
|
+
if (part.type === "tool-result") return toolResultPartToEvents(part);
|
|
48
|
+
return [];
|
|
56
49
|
}
|
|
57
50
|
function toolResultPartToEvents(part) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
51
|
+
return [{
|
|
52
|
+
type: "tool-result",
|
|
53
|
+
output: part.output,
|
|
54
|
+
toolCallId: part.toolCallId,
|
|
55
|
+
toolName: part.toolName
|
|
56
|
+
}];
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { modelMessageToAgentEvents, userTextToModelMessage };
|
|
60
|
+
|
|
67
61
|
//# sourceMappingURL=mapping.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mapping.js","
|
|
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// UserText -> AI SDK UserModelMessage\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\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":";AAqBA,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;AAGA,SAAgB,0BAA0B,SAAqC;CAC7E,IAAI,QAAQ,SAAS,aACnB,OAAO,6BAA6B,sBAAsB,OAAO,CAAC,EAAE,QAClE,4BACF;CAGF,IAAI,QAAQ,SAAS,QACnB,OAAO,QAAQ,QAAQ,QAAQ,uBAAuB;CAGxD,OAAO,CAAC;AACV;AAEA,SAAS,sBACP,SACwB;CACxB,OAAO,OAAO,QAAQ,YAAY,WAC9B,CAAC;EAAE,MAAM;EAAQ,MAAM,QAAQ;CAAQ,CAAC,IACxC,QAAQ;AACd;AAEA,SAAS,6BACP,OACwB;CACxB,OAAO,CACL,GAAG,MAAM,QAAQ,SAAS,KAAK,SAAS,WAAW,GACnD,GAAG,MAAM,QAAQ,SAAS,KAAK,SAAS,WAAW,CACrD;AACF;AAEA,SAAS,6BACP,MACc;CACd,IAAI,KAAK,SAAS,QAChB,OAAO,KAAK,OAAO,CAAC;EAAE,MAAM;EAAkB,MAAM,KAAK;CAAK,CAAC,IAAI,CAAC;CAGtE,IAAI,KAAK,SAAS,aAChB,OAAO,KAAK,OAAO,CAAC;EAAE,MAAM;EAAuB,MAAM,KAAK;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"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
//#region src/session/run.ts
|
|
2
|
+
var BufferedAgentRun = class {
|
|
3
|
+
#events = [];
|
|
4
|
+
#waiters = [];
|
|
5
|
+
#closed = false;
|
|
6
|
+
#error;
|
|
7
|
+
#streamStarted = false;
|
|
8
|
+
emit(event) {
|
|
9
|
+
if (this.#closed) return;
|
|
10
|
+
const waiter = this.#waiters.shift();
|
|
11
|
+
if (waiter) {
|
|
12
|
+
waiter.resolve({
|
|
13
|
+
done: false,
|
|
14
|
+
value: event
|
|
15
|
+
});
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
this.#events.push(structuredClone(event));
|
|
19
|
+
}
|
|
20
|
+
close(error) {
|
|
21
|
+
if (this.#closed) return;
|
|
22
|
+
this.#closed = true;
|
|
23
|
+
this.#error = error;
|
|
24
|
+
while (this.#waiters.length > 0) {
|
|
25
|
+
const waiter = this.#waiters.shift();
|
|
26
|
+
if (!waiter) continue;
|
|
27
|
+
if (error) waiter.reject(error);
|
|
28
|
+
else waiter.resolve({
|
|
29
|
+
done: true,
|
|
30
|
+
value: void 0
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
stream() {
|
|
35
|
+
if (this.#streamStarted) throw new Error("AgentRun.stream() can only be consumed once");
|
|
36
|
+
this.#streamStarted = true;
|
|
37
|
+
const iterator = {
|
|
38
|
+
next: () => this.#next(),
|
|
39
|
+
return: () => {
|
|
40
|
+
this.#cancel();
|
|
41
|
+
return Promise.resolve({
|
|
42
|
+
done: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
[Symbol.asyncIterator]: () => iterator
|
|
47
|
+
};
|
|
48
|
+
return iterator;
|
|
49
|
+
}
|
|
50
|
+
#cancel() {
|
|
51
|
+
this.#events.length = 0;
|
|
52
|
+
this.close();
|
|
53
|
+
}
|
|
54
|
+
#next() {
|
|
55
|
+
const event = this.#events.shift();
|
|
56
|
+
if (event) return Promise.resolve({
|
|
57
|
+
done: false,
|
|
58
|
+
value: event
|
|
59
|
+
});
|
|
60
|
+
if (this.#closed) {
|
|
61
|
+
if (this.#error) return Promise.reject(this.#error);
|
|
62
|
+
return Promise.resolve({
|
|
63
|
+
done: true,
|
|
64
|
+
value: void 0
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
this.#waiters.push({
|
|
69
|
+
reject,
|
|
70
|
+
resolve
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
//#endregion
|
|
76
|
+
export { BufferedAgentRun };
|
|
77
|
+
|
|
78
|
+
//# sourceMappingURL=run.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.js","names":["#events","#waiters","#closed","#error","#streamStarted","#next","#cancel"],"sources":["../../src/session/run.ts"],"sourcesContent":["import type { AgentEvent } from \"./events\";\n\nexport interface AgentRun {\n stream(): AsyncIterable<AgentEvent>;\n}\n\nexport class BufferedAgentRun implements AgentRun {\n readonly #events: AgentEvent[] = [];\n readonly #waiters: Array<{\n reject: (error: unknown) => void;\n resolve: (value: IteratorResult<AgentEvent>) => void;\n }> = [];\n #closed = false;\n #error: unknown;\n #streamStarted = false;\n\n emit(event: AgentEvent): void {\n if (this.#closed) {\n return;\n }\n\n const waiter = this.#waiters.shift();\n if (waiter) {\n waiter.resolve({ done: false, value: event });\n return;\n }\n\n this.#events.push(structuredClone(event));\n }\n\n close(error?: unknown): void {\n if (this.#closed) {\n return;\n }\n\n this.#closed = true;\n this.#error = error;\n\n while (this.#waiters.length > 0) {\n const waiter = this.#waiters.shift();\n if (!waiter) {\n continue;\n }\n\n if (error) {\n waiter.reject(error);\n } else {\n waiter.resolve({ done: true, value: undefined });\n }\n }\n }\n\n stream(): AsyncIterable<AgentEvent> {\n if (this.#streamStarted) {\n throw new Error(\"AgentRun.stream() can only be consumed once\");\n }\n this.#streamStarted = true;\n\n const iterator: AsyncIterableIterator<AgentEvent> = {\n next: () => this.#next(),\n return: () => {\n this.#cancel();\n return Promise.resolve({ done: true, value: undefined });\n },\n [Symbol.asyncIterator]: () => iterator,\n };\n return iterator;\n }\n\n #cancel(): void {\n this.#events.length = 0;\n this.close();\n }\n\n #next(): Promise<IteratorResult<AgentEvent>> {\n const event = this.#events.shift();\n if (event) {\n return Promise.resolve({ done: false, value: event });\n }\n\n if (this.#closed) {\n if (this.#error) {\n return Promise.reject(this.#error);\n }\n return Promise.resolve({ done: true, value: undefined });\n }\n\n return new Promise((resolve, reject) => {\n this.#waiters.push({ reject, resolve });\n });\n }\n}\n"],"mappings":";AAMA,IAAa,mBAAb,MAAkD;CAChD,UAAiC,CAAC;CAClC,WAGK,CAAC;CACN,UAAU;CACV;CACA,iBAAiB;CAEjB,KAAK,OAAyB;EAC5B,IAAI,KAAKE,SACP;EAGF,MAAM,SAAS,KAAKD,SAAS,MAAM;EACnC,IAAI,QAAQ;GACV,OAAO,QAAQ;IAAE,MAAM;IAAO,OAAO;GAAM,CAAC;GAC5C;EACF;EAEA,KAAKD,QAAQ,KAAK,gBAAgB,KAAK,CAAC;CAC1C;CAEA,MAAM,OAAuB;EAC3B,IAAI,KAAKE,SACP;EAGF,KAAKA,UAAU;EACf,KAAKC,SAAS;EAEd,OAAO,KAAKF,SAAS,SAAS,GAAG;GAC/B,MAAM,SAAS,KAAKA,SAAS,MAAM;GACnC,IAAI,CAAC,QACH;GAGF,IAAI,OACF,OAAO,OAAO,KAAK;QAEnB,OAAO,QAAQ;IAAE,MAAM;IAAM,OAAO,KAAA;GAAU,CAAC;EAEnD;CACF;CAEA,SAAoC;EAClC,IAAI,KAAKG,gBACP,MAAM,IAAI,MAAM,6CAA6C;EAE/D,KAAKA,iBAAiB;EAEtB,MAAM,WAA8C;GAClD,YAAY,KAAKC,MAAM;GACvB,cAAc;IACZ,KAAKC,QAAQ;IACb,OAAO,QAAQ,QAAQ;KAAE,MAAM;KAAM,OAAO,KAAA;IAAU,CAAC;GACzD;IACC,OAAO,sBAAsB;EAChC;EACA,OAAO;CACT;CAEA,UAAgB;EACd,KAAKN,QAAQ,SAAS;EACtB,KAAK,MAAM;CACb;CAEA,QAA6C;EAC3C,MAAM,QAAQ,KAAKA,QAAQ,MAAM;EACjC,IAAI,OACF,OAAO,QAAQ,QAAQ;GAAE,MAAM;GAAO,OAAO;EAAM,CAAC;EAGtD,IAAI,KAAKE,SAAS;GAChB,IAAI,KAAKC,QACP,OAAO,QAAQ,OAAO,KAAKA,MAAM;GAEnC,OAAO,QAAQ,QAAQ;IAAE,MAAM;IAAM,OAAO,KAAA;GAAU,CAAC;EACzD;EAEA,OAAO,IAAI,SAAS,SAAS,WAAW;GACtC,KAAKF,SAAS,KAAK;IAAE;IAAQ;GAAQ,CAAC;EACxC,CAAC;CACH;AACF"}
|
|
@@ -1,17 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
export declare class AgentSession {
|
|
9
|
-
#private;
|
|
10
|
-
constructor(llm: Llm, options?: SessionOptions);
|
|
11
|
-
getHistory(): AgentMessage[];
|
|
12
|
-
subscribe(listener: AgentEventListener): () => void;
|
|
13
|
-
submit(input: SessionInput): Promise<void>;
|
|
14
|
-
interrupt(): void;
|
|
15
|
-
kill(): void;
|
|
16
|
-
}
|
|
1
|
+
import { UserText } from "./events.js";
|
|
2
|
+
import { AgentRun } from "./run.js";
|
|
3
|
+
|
|
4
|
+
//#region src/session/session.d.ts
|
|
5
|
+
type AgentInput = string | readonly string[] | UserText;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { AgentInput };
|
|
17
8
|
//# sourceMappingURL=session.d.ts.map
|