@typescript-agent-harness/agent 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/loop.d.ts +10 -0
- package/dist/loop.d.ts.map +1 -0
- package/dist/loop.js +131 -0
- package/dist/loop.js.map +1 -0
- package/dist/plugin.d.ts +9 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +99 -0
- package/dist/plugin.js.map +1 -0
- package/dist/session.d.ts +49 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +231 -0
- package/dist/session.js.map +1 -0
- package/dist/subagent-tool.d.ts +10 -0
- package/dist/subagent-tool.d.ts.map +1 -0
- package/dist/subagent-tool.js +31 -0
- package/dist/subagent-tool.js.map +1 -0
- package/dist/types.d.ts +81 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 typescript-agent-harness contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { agentPlugin, SESSION, type AgentPluginOptions } from "./plugin.js";
|
|
2
|
+
export { subagentTool } from "./subagent-tool.js";
|
|
3
|
+
export { createDefaultLoop } from "./loop.js";
|
|
4
|
+
export { MemorySession } from "./session.js";
|
|
5
|
+
export type { AgentLoop, AgentLoopEvent, AgentStep, RunResult, Session, SessionEvent, SessionService, SessionState, SessionSummary, } from "./types.js";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAC5E,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,YAAY,EACV,SAAS,EACT,cAAc,EACd,SAAS,EACT,SAAS,EACT,OAAO,EACP,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,cAAc,GACf,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,OAAO,EAA2B,MAAM,aAAa,CAAC;AAC5E,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/loop.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Context } from "@typescript-agent-harness/core";
|
|
2
|
+
import type { LLMService } from "@typescript-agent-harness/llm";
|
|
3
|
+
import type { ToolService } from "@typescript-agent-harness/tools";
|
|
4
|
+
import type { AgentLoop } from "./types.js";
|
|
5
|
+
export declare function createDefaultLoop(deps: {
|
|
6
|
+
llm: LLMService;
|
|
7
|
+
tools: ToolService;
|
|
8
|
+
ctx: Pick<Context, "get" | "tryGet" | "emit">;
|
|
9
|
+
}): AgentLoop;
|
|
10
|
+
//# sourceMappingURL=loop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loop.d.ts","sourceRoot":"","sources":["../src/loop.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAC;AAC9D,OAAO,KAAK,EAA4B,UAAU,EAAY,MAAM,+BAA+B,CAAC;AACpG,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,KAAK,EACV,SAAS,EAGV,MAAM,YAAY,CAAC;AAkBpB,wBAAgB,iBAAiB,CAAC,IAAI,EAAE;IACtC,GAAG,EAAE,UAAU,CAAC;IAChB,KAAK,EAAE,WAAW,CAAC;IACnB,GAAG,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC,CAAC;CAC/C,GAAG,SAAS,CAsHZ"}
|
package/dist/loop.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
function pendingToolCalls(messages) {
|
|
2
|
+
for (let i = messages.length - 1; i >= 0; i -= 1) {
|
|
3
|
+
const msg = messages[i];
|
|
4
|
+
if (msg?.role === "assistant" && msg.toolCalls?.length) {
|
|
5
|
+
const done = new Set();
|
|
6
|
+
for (let j = i + 1; j < messages.length; j += 1) {
|
|
7
|
+
const later = messages[j];
|
|
8
|
+
if (later?.role === "tool")
|
|
9
|
+
done.add(later.toolCallId);
|
|
10
|
+
}
|
|
11
|
+
return msg.toolCalls.filter((c) => !done.has(c.id));
|
|
12
|
+
}
|
|
13
|
+
if (msg?.role === "user")
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
export function createDefaultLoop(deps) {
|
|
19
|
+
return {
|
|
20
|
+
async *run(session) {
|
|
21
|
+
const pending = pendingToolCalls(session.messages);
|
|
22
|
+
if (pending.length) {
|
|
23
|
+
for (const call of pending) {
|
|
24
|
+
session.signal.throwIfAborted();
|
|
25
|
+
yield { type: "tool.started", call };
|
|
26
|
+
const toolStepId = `step_tool_${call.id}`;
|
|
27
|
+
session.addStep({
|
|
28
|
+
type: "tool",
|
|
29
|
+
id: toolStepId,
|
|
30
|
+
tool: call.name,
|
|
31
|
+
input: call.arguments,
|
|
32
|
+
status: "pending",
|
|
33
|
+
});
|
|
34
|
+
const toolResult = await deps.tools.execute(call, {
|
|
35
|
+
sessionId: session.id,
|
|
36
|
+
callId: call.id,
|
|
37
|
+
signal: session.signal,
|
|
38
|
+
runtime: deps.ctx,
|
|
39
|
+
});
|
|
40
|
+
session.updateToolStep(toolStepId, toolResult.ok, toolResult.output);
|
|
41
|
+
session.append({
|
|
42
|
+
role: "tool",
|
|
43
|
+
toolCallId: call.id,
|
|
44
|
+
content: JSON.stringify(toolResult.output),
|
|
45
|
+
});
|
|
46
|
+
yield { type: "tool.finished", call, output: toolResult.output };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
let steps = 0;
|
|
50
|
+
while (steps < session.maxSteps) {
|
|
51
|
+
steps += 1;
|
|
52
|
+
session.signal.throwIfAborted();
|
|
53
|
+
const request = {
|
|
54
|
+
messages: [...session.messages],
|
|
55
|
+
tools: deps.tools.listSchemas(),
|
|
56
|
+
};
|
|
57
|
+
const llmStepId = `step_llm_${steps}`;
|
|
58
|
+
session.addStep({
|
|
59
|
+
type: "llm",
|
|
60
|
+
id: llmStepId,
|
|
61
|
+
request,
|
|
62
|
+
status: "pending",
|
|
63
|
+
});
|
|
64
|
+
let response;
|
|
65
|
+
for await (const part of deps.llm.stream({
|
|
66
|
+
...request,
|
|
67
|
+
signal: session.signal,
|
|
68
|
+
})) {
|
|
69
|
+
if (part.type === "text-delta") {
|
|
70
|
+
yield { type: "llm.delta", text: part.text };
|
|
71
|
+
}
|
|
72
|
+
else if (part.type === "done") {
|
|
73
|
+
response = part.response;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (!response) {
|
|
77
|
+
throw new Error("LLM stream ended without a response");
|
|
78
|
+
}
|
|
79
|
+
session.updateLlmStep(llmStepId, response);
|
|
80
|
+
session.append(response.message);
|
|
81
|
+
yield { type: "llm.completed", response };
|
|
82
|
+
const calls = response.toolCalls ?? [];
|
|
83
|
+
if (calls.length === 0) {
|
|
84
|
+
const result = {
|
|
85
|
+
sessionId: session.id,
|
|
86
|
+
text: response.message.content ?? "",
|
|
87
|
+
finishReason: response.finishReason,
|
|
88
|
+
steps: [],
|
|
89
|
+
};
|
|
90
|
+
yield { type: "agent.finished", result };
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
for (const call of calls) {
|
|
94
|
+
session.signal.throwIfAborted();
|
|
95
|
+
yield { type: "tool.started", call };
|
|
96
|
+
const toolStepId = `step_tool_${call.id}`;
|
|
97
|
+
session.addStep({
|
|
98
|
+
type: "tool",
|
|
99
|
+
id: toolStepId,
|
|
100
|
+
tool: call.name,
|
|
101
|
+
input: call.arguments,
|
|
102
|
+
status: "pending",
|
|
103
|
+
});
|
|
104
|
+
const toolResult = await deps.tools.execute(call, {
|
|
105
|
+
sessionId: session.id,
|
|
106
|
+
callId: call.id,
|
|
107
|
+
signal: session.signal,
|
|
108
|
+
runtime: deps.ctx,
|
|
109
|
+
});
|
|
110
|
+
session.updateToolStep(toolStepId, toolResult.ok, toolResult.output);
|
|
111
|
+
session.append({
|
|
112
|
+
role: "tool",
|
|
113
|
+
toolCallId: call.id,
|
|
114
|
+
content: JSON.stringify(toolResult.output),
|
|
115
|
+
});
|
|
116
|
+
yield { type: "tool.finished", call, output: toolResult.output };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
yield {
|
|
120
|
+
type: "agent.finished",
|
|
121
|
+
result: {
|
|
122
|
+
sessionId: session.id,
|
|
123
|
+
text: "",
|
|
124
|
+
finishReason: "max_steps",
|
|
125
|
+
steps: [],
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=loop.js.map
|
package/dist/loop.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loop.js","sourceRoot":"","sources":["../src/loop.ts"],"names":[],"mappings":"AASA,SAAS,gBAAgB,CAAC,QAAuB;IAC/C,KAAK,IAAI,CAAC,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACjD,MAAM,GAAG,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;QACxB,IAAI,GAAG,EAAE,IAAI,KAAK,WAAW,IAAI,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC;YACvD,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;YAC/B,KAAK,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;gB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,IAAI;wBACf,KAAK,EAAE,IAAI,CAAC,SAAS;wBACrB,MAAM,EAAE,SAAS;qBAClB,CAAC,CAAC;oBACH,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE;wBAChD,SAAS,EAAE,OAAO,CAAC,EAAE;wBACrB,MAAM,EAAE,IAAI,CAAC,EAAE;wBACf,MAAM,EAAE,OAAO,CAAC,MAAM;wBACtB,OAAO,EAAE,IAAI,CAAC,GAAG;qBAClB,CAAC,CAAC;oBACH,OAAO,CAAC,cAAc,CAAC,UAAU,EAAE,UAAU,CAAC,EAAE,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;oBACrE,OAAO,CAAC,MAAM,CAAC;wBACb,IAAI,EAAE,MAAM;wBACZ,UAAU,EAAE,IAAI,CAAC,EAAE;wBACnB,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,MAAM,CAAC;qBAC3C,CAAC,CAAC;oBACH,MAAM,EAAE,IAAI,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,MAAM,EAAE,CAAC;gBACnE,CAAC;YACH,CAAC;YAED,MAAM;gBACJ,IAAI,EAAE,gBAAgB;gBACtB,MAAM,EAAE;oBACN,SAAS,EAAE,OAAO,CAAC,EAAE;oBACrB,IAAI,EAAE,EAAE;oBACR,YAAY,EAAE,WAAW;oBACzB,KAAK,EAAE,EAAE;iBACV;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Plugin } from "@typescript-agent-harness/core";
|
|
2
|
+
import type { SessionService } from "./types.js";
|
|
3
|
+
export declare const SESSION: import("@typescript-agent-harness/core").ServiceKey<SessionService>;
|
|
4
|
+
export type AgentPluginOptions = {
|
|
5
|
+
systemPrompt?: string;
|
|
6
|
+
maxSteps?: number;
|
|
7
|
+
};
|
|
8
|
+
export declare function agentPlugin(options?: AgentPluginOptions): Plugin;
|
|
9
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAM/E,OAAO,KAAK,EAIV,cAAc,EAGf,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,OAAO,qEAA8C,CAAC;AAEnE,MAAM,MAAM,kBAAkB,GAAG;IAC/B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAeF,wBAAgB,WAAW,CAAC,OAAO,GAAE,kBAAuB,GAAG,MAAM,CAkFpE"}
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createServiceKey } from "@typescript-agent-harness/core";
|
|
2
|
+
import { LLM } from "@typescript-agent-harness/llm";
|
|
3
|
+
import { STORAGE } from "@typescript-agent-harness/storage";
|
|
4
|
+
import { TOOLS } from "@typescript-agent-harness/tools";
|
|
5
|
+
import { createDefaultLoop } from "./loop.js";
|
|
6
|
+
import { MemorySession } from "./session.js";
|
|
7
|
+
export const SESSION = createServiceKey("session");
|
|
8
|
+
function asState(value) {
|
|
9
|
+
if (value === "idle" ||
|
|
10
|
+
value === "running" ||
|
|
11
|
+
value === "completed" ||
|
|
12
|
+
value === "failed" ||
|
|
13
|
+
value === "cancelled") {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
return "idle";
|
|
17
|
+
}
|
|
18
|
+
export function agentPlugin(options = {}) {
|
|
19
|
+
const maxSteps = options.maxSteps ?? 8;
|
|
20
|
+
const systemPrompt = options.systemPrompt ??
|
|
21
|
+
"You are a helpful agent. Use tools when they help answer the user.";
|
|
22
|
+
return {
|
|
23
|
+
name: "agent",
|
|
24
|
+
setup(ctx) {
|
|
25
|
+
const llm = ctx.get(LLM);
|
|
26
|
+
const tools = ctx.get(TOOLS);
|
|
27
|
+
const storage = ctx.tryGet(STORAGE);
|
|
28
|
+
const loop = createDefaultLoop({ llm, tools, ctx });
|
|
29
|
+
const sessions = new Map();
|
|
30
|
+
async function hydrate(id) {
|
|
31
|
+
const hit = sessions.get(id);
|
|
32
|
+
if (hit)
|
|
33
|
+
return hit;
|
|
34
|
+
if (!storage)
|
|
35
|
+
return undefined;
|
|
36
|
+
const row = await storage.getSession(id);
|
|
37
|
+
if (!row)
|
|
38
|
+
return undefined;
|
|
39
|
+
const storedEvents = await storage.listEvents(id);
|
|
40
|
+
const session = new MemorySession({
|
|
41
|
+
id: row.id,
|
|
42
|
+
loop,
|
|
43
|
+
bus: ctx,
|
|
44
|
+
maxSteps: row.maxSteps,
|
|
45
|
+
storage,
|
|
46
|
+
restored: {
|
|
47
|
+
state: asState(row.status),
|
|
48
|
+
messages: JSON.parse(row.messagesJson),
|
|
49
|
+
steps: JSON.parse(row.stepsJson),
|
|
50
|
+
events: storedEvents.map((e) => ({
|
|
51
|
+
id: e.id,
|
|
52
|
+
type: e.type,
|
|
53
|
+
payload: JSON.parse(e.payloadJson),
|
|
54
|
+
createdAt: e.createdAt,
|
|
55
|
+
})),
|
|
56
|
+
createdAt: row.createdAt,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
sessions.set(session.id, session);
|
|
60
|
+
return session;
|
|
61
|
+
}
|
|
62
|
+
const service = {
|
|
63
|
+
async create() {
|
|
64
|
+
const session = new MemorySession({
|
|
65
|
+
loop,
|
|
66
|
+
bus: ctx,
|
|
67
|
+
maxSteps,
|
|
68
|
+
systemPrompt,
|
|
69
|
+
...(storage ? { storage } : {}),
|
|
70
|
+
});
|
|
71
|
+
sessions.set(session.id, session);
|
|
72
|
+
await session.persist();
|
|
73
|
+
await ctx.emit("session.created", { sessionId: session.id });
|
|
74
|
+
return session;
|
|
75
|
+
},
|
|
76
|
+
async get(sessionId) {
|
|
77
|
+
return hydrate(sessionId);
|
|
78
|
+
},
|
|
79
|
+
async list() {
|
|
80
|
+
if (storage) {
|
|
81
|
+
const rows = await storage.listSessions();
|
|
82
|
+
return rows.map((row) => ({
|
|
83
|
+
id: row.id,
|
|
84
|
+
state: asState(row.status),
|
|
85
|
+
messageCount: JSON.parse(row.messagesJson).length,
|
|
86
|
+
}));
|
|
87
|
+
}
|
|
88
|
+
return [...sessions.values()].map((s) => ({
|
|
89
|
+
id: s.id,
|
|
90
|
+
state: s.state,
|
|
91
|
+
messageCount: s.messages.length,
|
|
92
|
+
}));
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
ctx.provide(SESSION, service);
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAe,MAAM,gCAAgC,CAAC;AAC/E,OAAO,EAAE,GAAG,EAAoB,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAC5D,OAAO,EAAE,KAAK,EAAE,MAAM,iCAAiC,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAU7C,MAAM,CAAC,MAAM,OAAO,GAAG,gBAAgB,CAAiB,SAAS,CAAC,CAAC;AAOnE,SAAS,OAAO,CAAC,KAAa;IAC5B,IACE,KAAK,KAAK,MAAM;QAChB,KAAK,KAAK,SAAS;QACnB,KAAK,KAAK,WAAW;QACrB,KAAK,KAAK,QAAQ;QAClB,KAAK,KAAK,WAAW,EACrB,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC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IAAI,OAAO,EAAE,CAAC;wBACZ,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,YAAY,EAAE,CAAC;wBAC1C,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;4BACxB,EAAE,EAAE,GAAG,CAAC,EAAE;4BACV,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC;4BAC1B,YAAY,EAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,YAAY,CAAe,CAAC,MAAM;yBACjE,CAAC,CAAC,CAAC;oBACN,CAAC;oBACD,OAAO,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;wBACxC,EAAE,EAAE,CAAC,CAAC,EAAE;wBACR,KAAK,EAAE,CAAC,CAAC,KAAK;wBACd,YAAY,EAAE,CAAC,CAAC,QAAQ,CAAC,MAAM;qBAChC,CAAC,CAAC,CAAC;gBACN,CAAC;aACF,CAAC;YAEF,GAAG,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAChC,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Context } from "@typescript-agent-harness/core";
|
|
2
|
+
import type { ChatMessage } from "@typescript-agent-harness/llm";
|
|
3
|
+
import type { StorageService } from "@typescript-agent-harness/storage";
|
|
4
|
+
import type { AgentLoop } from "./types.js";
|
|
5
|
+
import type { AgentStep, RunResult, Session, SessionEvent, SessionState } from "./types.js";
|
|
6
|
+
export type SessionInit = {
|
|
7
|
+
id?: string;
|
|
8
|
+
loop: AgentLoop;
|
|
9
|
+
bus: Pick<Context, "emit">;
|
|
10
|
+
maxSteps: number;
|
|
11
|
+
systemPrompt?: string;
|
|
12
|
+
storage?: StorageService | undefined;
|
|
13
|
+
restored?: {
|
|
14
|
+
state: SessionState;
|
|
15
|
+
messages: ChatMessage[];
|
|
16
|
+
steps: AgentStep[];
|
|
17
|
+
events: SessionEvent[];
|
|
18
|
+
createdAt: number;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export declare class MemorySession implements Session {
|
|
22
|
+
readonly id: string;
|
|
23
|
+
private _state;
|
|
24
|
+
private readonly _messages;
|
|
25
|
+
private readonly _steps;
|
|
26
|
+
private readonly _events;
|
|
27
|
+
private abort;
|
|
28
|
+
private lastResult;
|
|
29
|
+
private readonly createdAt;
|
|
30
|
+
private persistedEventIds;
|
|
31
|
+
private readonly loop;
|
|
32
|
+
private readonly bus;
|
|
33
|
+
private readonly maxSteps;
|
|
34
|
+
private readonly storage;
|
|
35
|
+
constructor(init: SessionInit);
|
|
36
|
+
get state(): SessionState;
|
|
37
|
+
get messages(): ChatMessage[];
|
|
38
|
+
get steps(): AgentStep[];
|
|
39
|
+
get events(): SessionEvent[];
|
|
40
|
+
run(input: string): Promise<RunResult>;
|
|
41
|
+
resume(): Promise<RunResult>;
|
|
42
|
+
cancel(): Promise<void>;
|
|
43
|
+
private handle;
|
|
44
|
+
private record;
|
|
45
|
+
private checkpointMeta;
|
|
46
|
+
persist(checkpoint?: boolean): Promise<void>;
|
|
47
|
+
private drive;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAC;AAC9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACxE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,KAAK,EACV,SAAS,EACT,SAAS,EACT,OAAO,EACP,YAAY,EAEZ,YAAY,EACb,MAAM,YAAY,CAAC;AAQ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AAE,CAE3B;IAEK,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAWtC,MAAM,IAAI,OAAO,CAAC,SAAS,CAAC;IAW5B,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAI7B,OAAO,CAAC,MAAM;IAyBd,OAAO,CAAC,MAAM;IAWd,OAAO,CAAC,cAAc;IAMhB,OAAO,CAAC,UAAU,UAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;YA2ClC,KAAK;CA2DpB"}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
let seq = 0;
|
|
2
|
+
function nextId(prefix) {
|
|
3
|
+
seq += 1;
|
|
4
|
+
return `${prefix}_${seq}_${Date.now().toString(36)}`;
|
|
5
|
+
}
|
|
6
|
+
export class MemorySession {
|
|
7
|
+
id;
|
|
8
|
+
_state = "idle";
|
|
9
|
+
_messages = [];
|
|
10
|
+
_steps = [];
|
|
11
|
+
_events = [];
|
|
12
|
+
abort = new AbortController();
|
|
13
|
+
lastResult;
|
|
14
|
+
createdAt;
|
|
15
|
+
persistedEventIds = new Set();
|
|
16
|
+
loop;
|
|
17
|
+
bus;
|
|
18
|
+
maxSteps;
|
|
19
|
+
storage;
|
|
20
|
+
constructor(init) {
|
|
21
|
+
this.loop = init.loop;
|
|
22
|
+
this.bus = init.bus;
|
|
23
|
+
this.maxSteps = init.maxSteps;
|
|
24
|
+
this.storage = init.storage;
|
|
25
|
+
this.id = init.id ?? nextId("session");
|
|
26
|
+
this.createdAt = init.restored?.createdAt ?? Date.now();
|
|
27
|
+
if (init.restored) {
|
|
28
|
+
this._state =
|
|
29
|
+
init.restored.state === "running" ? "failed" : init.restored.state;
|
|
30
|
+
this._messages.push(...init.restored.messages);
|
|
31
|
+
this._steps.push(...init.restored.steps.filter((s) => s.status !== "pending"));
|
|
32
|
+
this._events.push(...init.restored.events);
|
|
33
|
+
for (const e of this._events)
|
|
34
|
+
this.persistedEventIds.add(e.id);
|
|
35
|
+
const last = [...this._messages].reverse().find((m) => m.role === "assistant");
|
|
36
|
+
if (last?.content && this._state === "completed") {
|
|
37
|
+
this.lastResult = {
|
|
38
|
+
sessionId: this.id,
|
|
39
|
+
text: last.content,
|
|
40
|
+
finishReason: "stop",
|
|
41
|
+
steps: [...this._steps],
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else if (init.systemPrompt) {
|
|
46
|
+
this._messages.push({ role: "system", content: init.systemPrompt });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
get state() {
|
|
50
|
+
return this._state;
|
|
51
|
+
}
|
|
52
|
+
get messages() {
|
|
53
|
+
return this._messages;
|
|
54
|
+
}
|
|
55
|
+
get steps() {
|
|
56
|
+
return this._steps;
|
|
57
|
+
}
|
|
58
|
+
get events() {
|
|
59
|
+
return this._events;
|
|
60
|
+
}
|
|
61
|
+
async run(input) {
|
|
62
|
+
if (this._state === "running") {
|
|
63
|
+
throw new Error(`session ${this.id} is already running`);
|
|
64
|
+
}
|
|
65
|
+
this.abort = new AbortController();
|
|
66
|
+
this._messages.push({ role: "user", content: input });
|
|
67
|
+
this.record("user.message", { content: input });
|
|
68
|
+
await this.persist();
|
|
69
|
+
return this.drive();
|
|
70
|
+
}
|
|
71
|
+
async resume() {
|
|
72
|
+
if (this._state === "running") {
|
|
73
|
+
throw new Error(`session ${this.id} is already running`);
|
|
74
|
+
}
|
|
75
|
+
if (this._state === "completed" && this.lastResult) {
|
|
76
|
+
return this.lastResult;
|
|
77
|
+
}
|
|
78
|
+
this.abort = new AbortController();
|
|
79
|
+
return this.drive();
|
|
80
|
+
}
|
|
81
|
+
async cancel() {
|
|
82
|
+
this.abort.abort();
|
|
83
|
+
}
|
|
84
|
+
handle() {
|
|
85
|
+
return {
|
|
86
|
+
id: this.id,
|
|
87
|
+
messages: this._messages,
|
|
88
|
+
signal: this.abort.signal,
|
|
89
|
+
maxSteps: this.maxSteps,
|
|
90
|
+
append: (message) => this._messages.push(message),
|
|
91
|
+
addStep: (step) => this._steps.push(step),
|
|
92
|
+
updateLlmStep: (id, response) => {
|
|
93
|
+
const step = this._steps.find((s) => s.id === id);
|
|
94
|
+
if (step && step.type === "llm") {
|
|
95
|
+
step.response = response;
|
|
96
|
+
step.status = "completed";
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
updateToolStep: (id, ok, output) => {
|
|
100
|
+
const step = this._steps.find((s) => s.id === id);
|
|
101
|
+
if (step && step.type === "tool") {
|
|
102
|
+
step.output = output;
|
|
103
|
+
step.status = ok ? "completed" : "failed";
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
record(type, payload) {
|
|
109
|
+
const event = {
|
|
110
|
+
id: nextId("evt"),
|
|
111
|
+
type,
|
|
112
|
+
payload,
|
|
113
|
+
createdAt: Date.now(),
|
|
114
|
+
};
|
|
115
|
+
this._events.push(event);
|
|
116
|
+
return event;
|
|
117
|
+
}
|
|
118
|
+
checkpointMeta() {
|
|
119
|
+
const last = [...this._steps].reverse().find((s) => s.status !== "pending");
|
|
120
|
+
if (!last)
|
|
121
|
+
return { stepId: null, stepType: null };
|
|
122
|
+
return { stepId: last.id, stepType: last.type };
|
|
123
|
+
}
|
|
124
|
+
async persist(checkpoint = false) {
|
|
125
|
+
if (!this.storage)
|
|
126
|
+
return;
|
|
127
|
+
const now = Date.now();
|
|
128
|
+
await this.storage.saveSession({
|
|
129
|
+
id: this.id,
|
|
130
|
+
status: this._state,
|
|
131
|
+
maxSteps: this.maxSteps,
|
|
132
|
+
messagesJson: JSON.stringify(this._messages),
|
|
133
|
+
stepsJson: JSON.stringify(this._steps),
|
|
134
|
+
createdAt: this.createdAt,
|
|
135
|
+
updatedAt: now,
|
|
136
|
+
});
|
|
137
|
+
for (const event of this._events) {
|
|
138
|
+
if (this.persistedEventIds.has(event.id))
|
|
139
|
+
continue;
|
|
140
|
+
await this.storage.appendEvent({
|
|
141
|
+
id: event.id,
|
|
142
|
+
sessionId: this.id,
|
|
143
|
+
type: event.type,
|
|
144
|
+
payloadJson: JSON.stringify(event.payload),
|
|
145
|
+
createdAt: event.createdAt,
|
|
146
|
+
});
|
|
147
|
+
this.persistedEventIds.add(event.id);
|
|
148
|
+
}
|
|
149
|
+
if (checkpoint) {
|
|
150
|
+
const meta = this.checkpointMeta();
|
|
151
|
+
const row = {
|
|
152
|
+
id: nextId("ckpt"),
|
|
153
|
+
sessionId: this.id,
|
|
154
|
+
stepId: meta.stepId,
|
|
155
|
+
stepType: meta.stepType,
|
|
156
|
+
messageCount: this._messages.length,
|
|
157
|
+
createdAt: now,
|
|
158
|
+
};
|
|
159
|
+
await this.storage.saveCheckpoint(row);
|
|
160
|
+
await this.bus.emit("storage.checkpoint", {
|
|
161
|
+
sessionId: this.id,
|
|
162
|
+
checkpointId: row.id,
|
|
163
|
+
stepId: meta.stepId,
|
|
164
|
+
stepType: meta.stepType,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async drive() {
|
|
169
|
+
this._state = "running";
|
|
170
|
+
const turnId = nextId("turn");
|
|
171
|
+
await this.bus.emit("session.turn.start", { sessionId: this.id, turnId });
|
|
172
|
+
await this.bus.emit("agent.started", { sessionId: this.id });
|
|
173
|
+
this.record("agent.started", { sessionId: this.id });
|
|
174
|
+
await this.persist();
|
|
175
|
+
try {
|
|
176
|
+
let finished;
|
|
177
|
+
for await (const event of this.loop.run(this.handle())) {
|
|
178
|
+
if (event.type === "llm.delta") {
|
|
179
|
+
await this.bus.emit("agent.assistant-stream", {
|
|
180
|
+
sessionId: this.id,
|
|
181
|
+
text: event.text,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
else if (event.type === "llm.completed") {
|
|
185
|
+
this.record("llm.completed", { finishReason: event.response.finishReason });
|
|
186
|
+
await this.persist(true);
|
|
187
|
+
}
|
|
188
|
+
else if (event.type === "tool.finished") {
|
|
189
|
+
this.record("tool.finished", { tool: event.call.name });
|
|
190
|
+
await this.persist(true);
|
|
191
|
+
}
|
|
192
|
+
else if (event.type === "agent.finished") {
|
|
193
|
+
finished = {
|
|
194
|
+
...event.result,
|
|
195
|
+
steps: [...this._steps],
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (!finished) {
|
|
200
|
+
throw new Error("agent loop ended without a result");
|
|
201
|
+
}
|
|
202
|
+
this.lastResult = finished;
|
|
203
|
+
this._state = finished.finishReason === "max_steps" ? "failed" : "completed";
|
|
204
|
+
await this.bus.emit("agent.finished", {
|
|
205
|
+
sessionId: this.id,
|
|
206
|
+
text: finished.text,
|
|
207
|
+
finishReason: finished.finishReason,
|
|
208
|
+
});
|
|
209
|
+
await this.bus.emit("session.turn.end", { sessionId: this.id, turnId });
|
|
210
|
+
this.record("agent.finished", { finishReason: finished.finishReason });
|
|
211
|
+
await this.persist(true);
|
|
212
|
+
return finished;
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
if (this.abort.signal.aborted) {
|
|
216
|
+
this._state = "cancelled";
|
|
217
|
+
await this.bus.emit("agent.cancelled", { sessionId: this.id });
|
|
218
|
+
await this.persist();
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
this._state = "failed";
|
|
222
|
+
await this.bus.emit("agent.failed", {
|
|
223
|
+
sessionId: this.id,
|
|
224
|
+
error: error instanceof Error ? error.message : String(error),
|
|
225
|
+
});
|
|
226
|
+
await this.persist();
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAaA,IAAI,GAAG,GAAG,CAAC,CAAC;AACZ,SAAS,MAAM,CAAC,MAAc;IAC5B,GAAG,IAAI,CAAC,CAAC;IACT,OAAO,GAAG,MAAM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S,EAAE,IAAI,CAAC,EAAE;gBAClB,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;aAC9D,CAAC,CAAC;YACH,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;YACrB,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Tool } from "@typescript-agent-harness/tools";
|
|
2
|
+
/**
|
|
3
|
+
* Subagent is just another Tool. The child Session shares the Runtime;
|
|
4
|
+
* recursion is avoided by not exposing this tool on nested runs if you
|
|
5
|
+
* omit it from the parent allowlist — or by the model simply not calling it.
|
|
6
|
+
*/
|
|
7
|
+
export declare function subagentTool(): Tool<{
|
|
8
|
+
task: string;
|
|
9
|
+
}, unknown>;
|
|
10
|
+
//# sourceMappingURL=subagent-tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subagent-tool.d.ts","sourceRoot":"","sources":["../src/subagent-tool.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iCAAiC,CAAC;AAE5D;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,IAAI,CAAC;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,EAAE,OAAO,CAAC,CAuB9D"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { SESSION } from "./plugin.js";
|
|
2
|
+
/**
|
|
3
|
+
* Subagent is just another Tool. The child Session shares the Runtime;
|
|
4
|
+
* recursion is avoided by not exposing this tool on nested runs if you
|
|
5
|
+
* omit it from the parent allowlist — or by the model simply not calling it.
|
|
6
|
+
*/
|
|
7
|
+
export function subagentTool() {
|
|
8
|
+
return {
|
|
9
|
+
name: "run_subagent",
|
|
10
|
+
description: "Run a child agent session on a subtask and return its final text.",
|
|
11
|
+
inputSchema: {
|
|
12
|
+
type: "object",
|
|
13
|
+
properties: {
|
|
14
|
+
task: { type: "string", description: "Instructions for the child agent" },
|
|
15
|
+
},
|
|
16
|
+
required: ["task"],
|
|
17
|
+
additionalProperties: false,
|
|
18
|
+
},
|
|
19
|
+
async execute(input, ctx) {
|
|
20
|
+
const sessions = ctx.runtime.get(SESSION);
|
|
21
|
+
const child = await sessions.create();
|
|
22
|
+
const result = await child.run(input.task);
|
|
23
|
+
return {
|
|
24
|
+
sessionId: child.id,
|
|
25
|
+
text: result.text,
|
|
26
|
+
finishReason: result.finishReason,
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=subagent-tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subagent-tool.js","sourceRoot":"","sources":["../src/subagent-tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAGtC;;;;GAIG;AACH,MAAM,UAAU,YAAY;IAC1B,OAAO;QACL,IAAI,EAAE,cAAc;QACpB,WAAW,EAAE,mEAAmE;QAChF,WAAW,EAAE;YACX,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,kCAAkC,EAAE;aAC1E;YACD,QAAQ,EAAE,CAAC,MAAM,CAAC;YAClB,oBAAoB,EAAE,KAAK;SAC5B;QACD,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG;YACtB,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YAC1C,MAAM,KAAK,GAAG,MAAM,QAAQ,CAAC,MAAM,EAAE,CAAC;YACtC,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YAC3C,OAAO;gBACL,SAAS,EAAE,KAAK,CAAC,EAAE;gBACnB,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,YAAY,EAAE,MAAM,CAAC,YAAY;aAClC,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { LLMRequest, LLMResponse, ToolCall } from "@typescript-agent-harness/llm";
|
|
2
|
+
import type { ChatMessage } from "@typescript-agent-harness/llm";
|
|
3
|
+
export type SessionState = "idle" | "running" | "completed" | "failed" | "cancelled";
|
|
4
|
+
export type AgentStep = {
|
|
5
|
+
type: "llm";
|
|
6
|
+
id: string;
|
|
7
|
+
request: LLMRequest;
|
|
8
|
+
response?: LLMResponse;
|
|
9
|
+
status: "pending" | "completed" | "failed";
|
|
10
|
+
} | {
|
|
11
|
+
type: "tool";
|
|
12
|
+
id: string;
|
|
13
|
+
tool: string;
|
|
14
|
+
input: unknown;
|
|
15
|
+
output?: unknown;
|
|
16
|
+
status: "pending" | "completed" | "failed";
|
|
17
|
+
};
|
|
18
|
+
export type SessionEvent = {
|
|
19
|
+
id: string;
|
|
20
|
+
type: string;
|
|
21
|
+
payload: unknown;
|
|
22
|
+
createdAt: number;
|
|
23
|
+
};
|
|
24
|
+
export type RunResult = {
|
|
25
|
+
sessionId: string;
|
|
26
|
+
text: string;
|
|
27
|
+
finishReason: LLMResponse["finishReason"] | "max_steps";
|
|
28
|
+
steps: AgentStep[];
|
|
29
|
+
};
|
|
30
|
+
export type SessionSummary = {
|
|
31
|
+
id: string;
|
|
32
|
+
state: SessionState;
|
|
33
|
+
messageCount: number;
|
|
34
|
+
};
|
|
35
|
+
export interface Session {
|
|
36
|
+
readonly id: string;
|
|
37
|
+
readonly state: SessionState;
|
|
38
|
+
readonly messages: ChatMessage[];
|
|
39
|
+
readonly steps: AgentStep[];
|
|
40
|
+
readonly events: SessionEvent[];
|
|
41
|
+
run(input: string): Promise<RunResult>;
|
|
42
|
+
resume(): Promise<RunResult>;
|
|
43
|
+
cancel(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
export type SessionService = {
|
|
46
|
+
create(): Promise<Session>;
|
|
47
|
+
get(sessionId: string): Promise<Session | undefined>;
|
|
48
|
+
list(): Promise<SessionSummary[]>;
|
|
49
|
+
};
|
|
50
|
+
export type AgentLoopEvent = {
|
|
51
|
+
type: "llm.delta";
|
|
52
|
+
text: string;
|
|
53
|
+
} | {
|
|
54
|
+
type: "llm.completed";
|
|
55
|
+
response: LLMResponse;
|
|
56
|
+
} | {
|
|
57
|
+
type: "tool.started";
|
|
58
|
+
call: ToolCall;
|
|
59
|
+
} | {
|
|
60
|
+
type: "tool.finished";
|
|
61
|
+
call: ToolCall;
|
|
62
|
+
output: unknown;
|
|
63
|
+
} | {
|
|
64
|
+
type: "agent.finished";
|
|
65
|
+
result: RunResult;
|
|
66
|
+
};
|
|
67
|
+
export type AgentLoop = {
|
|
68
|
+
run(session: SessionHandle): AsyncIterable<AgentLoopEvent>;
|
|
69
|
+
};
|
|
70
|
+
/** Internal handle the loop uses — implementation lives in session.ts */
|
|
71
|
+
export type SessionHandle = {
|
|
72
|
+
id: string;
|
|
73
|
+
messages: ChatMessage[];
|
|
74
|
+
signal: AbortSignal;
|
|
75
|
+
maxSteps: number;
|
|
76
|
+
append(message: ChatMessage): void;
|
|
77
|
+
addStep(step: AgentStep): void;
|
|
78
|
+
updateLlmStep(id: string, response: LLMResponse): void;
|
|
79
|
+
updateToolStep(id: string, ok: boolean, output: unknown): void;
|
|
80
|
+
};
|
|
81
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AACvF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAEjE,MAAM,MAAM,YAAY,GACpB,MAA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AG,IAAI,CAAC;IAC/B,aAAa,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,GAAG,IAAI,CAAC;IACvD,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC;CAChE,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@typescript-agent-harness/agent",
|
|
3
|
+
"version": "0.15.0",
|
|
4
|
+
"description": "Session, DefaultLoop, and agent orchestration",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@typescript-agent-harness/core": "0.15.0",
|
|
19
|
+
"@typescript-agent-harness/storage": "0.15.0",
|
|
20
|
+
"@typescript-agent-harness/llm": "0.15.0",
|
|
21
|
+
"@typescript-agent-harness/tools": "0.15.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/node": "^22.18.0",
|
|
25
|
+
"typescript": "^5.9.2"
|
|
26
|
+
},
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public",
|
|
33
|
+
"registry": "https://registry.npmjs.org/"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/MichaelVendor/typescript-agent-harness.git",
|
|
38
|
+
"directory": "packages/agent"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://MichaelVendor.github.io/typescript-agent-harness/",
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/MichaelVendor/typescript-agent-harness/issues"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc -p tsconfig.json",
|
|
46
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
47
|
+
"test": "node --test --experimental-strip-types test/*.test.ts"
|
|
48
|
+
}
|
|
49
|
+
}
|