clanka 0.0.29 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Agent.d.ts +87 -40
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +152 -94
- package/dist/Agent.js.map +1 -1
- package/dist/AgentExecutor.d.ts +165 -0
- package/dist/AgentExecutor.d.ts.map +1 -0
- package/dist/AgentExecutor.js +121 -0
- package/dist/AgentExecutor.js.map +1 -0
- package/dist/AgentTools.d.ts +14 -132
- package/dist/AgentTools.d.ts.map +1 -1
- package/dist/AgentTools.js +10 -12
- package/dist/AgentTools.js.map +1 -1
- package/dist/OutputFormatter.d.ts.map +1 -1
- package/dist/OutputFormatter.js +18 -0
- package/dist/OutputFormatter.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/Agent.ts +292 -216
- package/src/AgentExecutor.ts +245 -0
- package/src/AgentTools.ts +25 -22
- package/src/OutputFormatter.ts +18 -0
- package/src/index.ts +4 -4
- package/dist/Agent.test.d.ts +0 -2
- package/dist/Agent.test.d.ts.map +0 -1
- package/dist/Agent.test.js +0 -111
- package/dist/Agent.test.js.map +0 -1
- package/dist/Executor.d.ts +0 -20
- package/dist/Executor.d.ts.map +0 -1
- package/dist/Executor.js +0 -98
- package/dist/Executor.js.map +0 -1
- package/src/Agent.test.ts +0 -159
- package/src/Executor.ts +0 -151
package/dist/Agent.d.ts
CHANGED
|
@@ -1,20 +1,48 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 1.0.0
|
|
3
3
|
*/
|
|
4
|
-
import { Effect,
|
|
4
|
+
import { Effect, Layer, Schema, Scope, ServiceMap, Stream } from "effect";
|
|
5
5
|
import { AiError, LanguageModel, Prompt, Tool, Toolkit } from "effect/unstable/ai";
|
|
6
|
-
import { AgentTools } from "./AgentTools.ts";
|
|
7
|
-
import { Executor } from "./Executor.ts";
|
|
8
|
-
import { ToolkitRenderer } from "./ToolkitRenderer.ts";
|
|
9
6
|
import { ModelName, ProviderName } from "effect/unstable/ai/Model";
|
|
10
|
-
import
|
|
11
|
-
import type {
|
|
7
|
+
import * as AgentExecutor from "./AgentExecutor.ts";
|
|
8
|
+
import type { Path } from "effect/Path";
|
|
9
|
+
import type { ChildProcessSpawner } from "effect/unstable/process/ChildProcessSpawner";
|
|
10
|
+
import type { HttpClient } from "effect/unstable/http/HttpClient";
|
|
11
|
+
import type { CurrentDirectory, SubagentExecutor, TaskCompleter } from "./AgentTools.ts";
|
|
12
|
+
import type { FileSystem } from "effect/FileSystem";
|
|
13
|
+
/**
|
|
14
|
+
* @since 1.0.0
|
|
15
|
+
* @category Models
|
|
16
|
+
*/
|
|
17
|
+
export type TypeId = "~clanka/Agent";
|
|
18
|
+
/**
|
|
19
|
+
* @since 1.0.0
|
|
20
|
+
* @category Models
|
|
21
|
+
*/
|
|
22
|
+
export declare const TypeId: TypeId;
|
|
12
23
|
/**
|
|
13
24
|
* @since 1.0.0
|
|
14
25
|
* @category Models
|
|
15
26
|
*/
|
|
16
27
|
export interface Agent {
|
|
17
|
-
readonly
|
|
28
|
+
readonly [TypeId]: TypeId;
|
|
29
|
+
/**
|
|
30
|
+
* Send a prompt to the agent and receive a stream of output.
|
|
31
|
+
*/
|
|
32
|
+
send(options: {
|
|
33
|
+
/**
|
|
34
|
+
* The prompt to send to the agent.
|
|
35
|
+
*/
|
|
36
|
+
readonly prompt: Prompt.RawInput;
|
|
37
|
+
/**
|
|
38
|
+
* Provide additional system instructions, or a function that generates
|
|
39
|
+
* system instructions based on the tool instructions
|
|
40
|
+
*/
|
|
41
|
+
readonly system?: string | ((options: {
|
|
42
|
+
readonly toolInstructions: string;
|
|
43
|
+
readonly agentsMd: string;
|
|
44
|
+
}) => string) | undefined;
|
|
45
|
+
}): Effect.Effect<Stream.Stream<Output, AgentFinished | AiError.AiError>, never, Scope.Scope | LanguageModel.LanguageModel | ProviderName | ModelName>;
|
|
18
46
|
/**
|
|
19
47
|
* Send a message to the agent to steer its behavior. This is useful for
|
|
20
48
|
* providing feedback or new instructions while the agent is running.
|
|
@@ -26,29 +54,42 @@ export interface Agent {
|
|
|
26
54
|
steer(message: string): Effect.Effect<void>;
|
|
27
55
|
}
|
|
28
56
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
57
|
+
* @since 1.0.0
|
|
58
|
+
* @category Service
|
|
59
|
+
*/
|
|
60
|
+
export declare const Agent: ServiceMap.Service<Agent, Agent>;
|
|
61
|
+
/**
|
|
31
62
|
* @since 1.0.0
|
|
32
63
|
* @category Constructors
|
|
33
64
|
*/
|
|
34
|
-
export declare const make: <
|
|
35
|
-
|
|
65
|
+
export declare const make: Effect.Effect<Agent, never, Scope.Scope | AgentExecutor.AgentExecutor>;
|
|
66
|
+
/**
|
|
67
|
+
* @since 1.0.0
|
|
68
|
+
* @category Layers
|
|
69
|
+
*/
|
|
70
|
+
export declare const layer: Layer.Layer<Agent, never, AgentExecutor.AgentExecutor>;
|
|
71
|
+
/**
|
|
72
|
+
* Create an Agent layer that uses a local AgentExecutor.
|
|
73
|
+
*
|
|
74
|
+
* @since 1.0.0
|
|
75
|
+
* @category Layers
|
|
76
|
+
*/
|
|
77
|
+
export declare const layerLocal: <Toolkit extends Toolkit.Any = never>(options: {
|
|
36
78
|
readonly directory: string;
|
|
37
|
-
/** The prompt to use for the agent */
|
|
38
|
-
readonly prompt: Prompt.RawInput;
|
|
39
|
-
/**
|
|
40
|
-
* Provide additional system instructions, or a function that generates
|
|
41
|
-
* system instructions based on the tool instructions
|
|
42
|
-
*/
|
|
43
|
-
readonly system?: string | ((options: {
|
|
44
|
-
readonly toolInstructions: string;
|
|
45
|
-
readonly agentsMd: string;
|
|
46
|
-
}) => string) | undefined;
|
|
47
|
-
/** Additional tools to provide to the agent */
|
|
48
79
|
readonly tools?: Toolkit | undefined;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
80
|
+
}) => Layer.Layer<Agent, never, FileSystem | Path | ChildProcessSpawner | HttpClient | Exclude<Toolkit extends Toolkit.Toolkit<infer T> ? Tool.HandlersFor<T> | Tool.HandlerServices<T[keyof T]> : never, CurrentDirectory | SubagentExecutor | TaskCompleter>>;
|
|
81
|
+
declare const SubagentModel_base: ServiceMap.ServiceClass<SubagentModel, "clanka/Agent/SubagentModel", ServiceMap.ServiceMap<LanguageModel.LanguageModel | ProviderName | ModelName>>;
|
|
82
|
+
/**
|
|
83
|
+
* @since 1.0.0
|
|
84
|
+
* @category Subagent model
|
|
85
|
+
*/
|
|
86
|
+
export declare class SubagentModel extends SubagentModel_base {
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* @since 1.0.0
|
|
90
|
+
* @category Subagent model
|
|
91
|
+
*/
|
|
92
|
+
export declare const layerSubagentModel: <E, R>(layer: Layer.Layer<LanguageModel.LanguageModel | ProviderName | ModelName, E, R>) => Layer.Layer<SubagentModel, E, R>;
|
|
52
93
|
declare const AgentModelConfig_base: ServiceMap.Reference<{
|
|
53
94
|
readonly systemPromptTransform?: <A, E, R>(system: string, effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
|
|
54
95
|
readonly supportsAssistantPrefill?: boolean | undefined;
|
|
@@ -61,13 +102,19 @@ declare const AgentModelConfig_base: ServiceMap.Reference<{
|
|
|
61
102
|
export declare class AgentModelConfig extends AgentModelConfig_base {
|
|
62
103
|
static readonly layer: (options: typeof AgentModelConfig.Service) => Layer.Layer<never, never, never>;
|
|
63
104
|
}
|
|
105
|
+
declare const AgentStart_base: Schema.ExtendableClass<AgentStart, Schema.TaggedStruct<"AgentStart", {
|
|
106
|
+
readonly id: Schema.Number;
|
|
107
|
+
readonly prompt: Schema.Codec<Prompt.Prompt, Prompt.PromptEncoded, never, never>;
|
|
108
|
+
readonly provider: Schema.String;
|
|
109
|
+
readonly model: Schema.String;
|
|
110
|
+
}>, {}>;
|
|
64
111
|
/**
|
|
65
|
-
* A layer that provides most of the common services needed to run an agent.
|
|
66
|
-
*
|
|
67
112
|
* @since 1.0.0
|
|
68
|
-
* @category
|
|
113
|
+
* @category Output
|
|
69
114
|
*/
|
|
70
|
-
export declare
|
|
115
|
+
export declare class AgentStart extends AgentStart_base {
|
|
116
|
+
get modelAndProvider(): string;
|
|
117
|
+
}
|
|
71
118
|
declare const ReasoningStart_base: Schema.ExtendableClass<ReasoningStart, Schema.TaggedStruct<"ReasoningStart", {}>, {}>;
|
|
72
119
|
/**
|
|
73
120
|
* @since 1.0.0
|
|
@@ -123,15 +170,6 @@ declare const ScriptOutput_base: Schema.ExtendableClass<ScriptOutput, Schema.Tag
|
|
|
123
170
|
*/
|
|
124
171
|
export declare class ScriptOutput extends ScriptOutput_base {
|
|
125
172
|
}
|
|
126
|
-
declare const AgentFinished_base: Schema.ErrorClass<AgentFinished, Schema.TaggedStruct<"AgentFinished", {
|
|
127
|
-
readonly summary: Schema.String;
|
|
128
|
-
}>, import("effect/Cause").YieldableError>;
|
|
129
|
-
/**
|
|
130
|
-
* @since 1.0.0
|
|
131
|
-
* @category Output
|
|
132
|
-
*/
|
|
133
|
-
export declare class AgentFinished extends AgentFinished_base {
|
|
134
|
-
}
|
|
135
173
|
declare const SubagentStart_base: Schema.ExtendableClass<SubagentStart, Schema.TaggedStruct<"SubagentStart", {
|
|
136
174
|
readonly id: Schema.Number;
|
|
137
175
|
readonly prompt: Schema.String;
|
|
@@ -171,11 +209,20 @@ export declare class SubagentPart extends SubagentPart_base {
|
|
|
171
209
|
* @since 1.0.0
|
|
172
210
|
* @category Output
|
|
173
211
|
*/
|
|
174
|
-
export type Output = ContentPart | SubagentStart | SubagentComplete | SubagentPart;
|
|
212
|
+
export type Output = AgentStart | ContentPart | SubagentStart | SubagentComplete | SubagentPart;
|
|
213
|
+
/**
|
|
214
|
+
* @since 1.0.0
|
|
215
|
+
* @category Output
|
|
216
|
+
*/
|
|
217
|
+
export declare const Output: Schema.Union<readonly [typeof AgentStart, typeof ReasoningStart, typeof ReasoningDelta, typeof ReasoningEnd, typeof ScriptStart, typeof ScriptDelta, typeof ScriptEnd, typeof ScriptOutput, typeof SubagentStart, typeof SubagentComplete, typeof SubagentPart]>;
|
|
218
|
+
declare const AgentFinished_base: Schema.ErrorClass<AgentFinished, Schema.TaggedStruct<"AgentFinished", {
|
|
219
|
+
readonly summary: Schema.String;
|
|
220
|
+
}>, import("effect/Cause").YieldableError>;
|
|
175
221
|
/**
|
|
176
222
|
* @since 1.0.0
|
|
177
223
|
* @category Output
|
|
178
224
|
*/
|
|
179
|
-
export declare
|
|
225
|
+
export declare class AgentFinished extends AgentFinished_base {
|
|
226
|
+
}
|
|
180
227
|
export {};
|
|
181
228
|
//# sourceMappingURL=Agent.d.ts.map
|
package/dist/Agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Agent.d.ts","sourceRoot":"","sources":["../src/Agent.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,
|
|
1
|
+
{"version":3,"file":"Agent.d.ts","sourceRoot":"","sources":["../src/Agent.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAEL,MAAM,EAEN,KAAK,EAKL,MAAM,EACN,KAAK,EAEL,UAAU,EACV,MAAM,EACP,MAAM,QAAQ,CAAA;AACf,OAAO,EACL,OAAO,EACP,aAAa,EACb,MAAM,EACN,IAAI,EACJ,OAAO,EACR,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAElE,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,6CAA6C,CAAA;AACtF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AACjE,OAAO,KAAK,EACV,gBAAgB,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} from "effect";
|
|
5
5
|
import { AiError, LanguageModel, Prompt, Tool, Toolkit, } from "effect/unstable/ai";
|
|
6
|
-
import { AgentToolHandlers, AgentTools, CurrentDirectory, SubagentContext, TaskCompleteDeferred, } from "./AgentTools.js";
|
|
7
|
-
import { Executor } from "./Executor.js";
|
|
8
|
-
import { ToolkitRenderer } from "./ToolkitRenderer.js";
|
|
9
6
|
import { ModelName, ProviderName } from "effect/unstable/ai/Model";
|
|
10
7
|
import {} from "effect/unstable/ai/Response";
|
|
8
|
+
import * as AgentExecutor from "./AgentExecutor.js";
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
* @category Models
|
|
12
|
+
*/
|
|
13
|
+
export const TypeId = "~clanka/Agent";
|
|
14
|
+
/**
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
* @category Service
|
|
17
|
+
*/
|
|
18
|
+
export const Agent = ServiceMap.Service("clanka/Agent");
|
|
11
19
|
/**
|
|
12
|
-
* Start an agent in the given directory with the given prompt and tools.
|
|
13
|
-
*
|
|
14
20
|
* @since 1.0.0
|
|
15
21
|
* @category Constructors
|
|
16
22
|
*/
|
|
17
|
-
export const make = Effect.
|
|
18
|
-
const
|
|
19
|
-
const pathService = yield* Path.Path;
|
|
20
|
-
const executor = yield* Executor;
|
|
21
|
-
const renderer = yield* ToolkitRenderer;
|
|
22
|
-
const generateSystem = typeof options.system === "function" ? options.system : defaultSystem;
|
|
23
|
-
const allTools = Toolkit.merge(AgentTools, options.tools ?? Toolkit.empty);
|
|
24
|
-
const allToolsDts = renderer.render(allTools);
|
|
25
|
-
const tools = yield* allTools;
|
|
23
|
+
export const make = Effect.gen(function* () {
|
|
24
|
+
const executor = yield* AgentExecutor.AgentExecutor;
|
|
26
25
|
const singleTool = yield* SingleTools.asEffect().pipe(Effect.provide(SingleToolHandlers));
|
|
27
|
-
const
|
|
26
|
+
const toolsDts = yield* executor.toolsDts;
|
|
28
27
|
const pendingMessages = new Set();
|
|
29
|
-
const agentsMd = yield* pipe(
|
|
28
|
+
const agentsMd = yield* pipe(executor.agentsMd, Effect.map(Option.map((content) => `# AGENTS.md
|
|
30
29
|
|
|
31
30
|
The following instructions are from ./AGENTS.md in the current directory.
|
|
32
31
|
You do not need to read this file again.
|
|
@@ -35,29 +34,36 @@ You do not need to read this file again.
|
|
|
35
34
|
|
|
36
35
|
<!-- AGENTS.md start -->
|
|
37
36
|
${content}
|
|
38
|
-
<!-- AGENTS.md end -->`)
|
|
37
|
+
<!-- AGENTS.md end -->`)));
|
|
39
38
|
let agentCounter = 0;
|
|
40
39
|
const outputBuffer = new Map();
|
|
41
40
|
let currentOutputAgent = null;
|
|
42
|
-
|
|
41
|
+
let history = MutableRef.make(Prompt.empty);
|
|
42
|
+
const spawn = Effect.fnUntraced(function* (opts) {
|
|
43
|
+
const agentId = opts.agentId;
|
|
43
44
|
const ai = yield* LanguageModel.LanguageModel;
|
|
45
|
+
const subagentModel = yield* Effect.serviceOption(SubagentModel);
|
|
44
46
|
const modelConfig = yield* AgentModelConfig;
|
|
47
|
+
const services = yield* Effect.services();
|
|
48
|
+
let finalSummary = Option.none();
|
|
45
49
|
const singleToolMode = modelConfig.supportsNoTools !== true;
|
|
46
|
-
const deferred = yield* Deferred.make();
|
|
47
50
|
const output = yield* Queue.make();
|
|
48
|
-
const
|
|
51
|
+
const prompt = opts.disableHistory ? MutableRef.make(Prompt.empty) : history;
|
|
52
|
+
MutableRef.update(prompt, Prompt.concat(opts.prompt));
|
|
53
|
+
const generateSystem = typeof opts.system === "function" ? opts.system : defaultSystem;
|
|
54
|
+
const toolInstructions = generateSystemTools(toolsDts, !singleToolMode);
|
|
49
55
|
let system = generateSystem({
|
|
50
56
|
toolInstructions,
|
|
51
57
|
agentsMd: Option.getOrElse(agentsMd, () => ""),
|
|
52
58
|
});
|
|
53
|
-
if (typeof
|
|
54
|
-
system += `\n${
|
|
59
|
+
if (typeof opts.system === "string") {
|
|
60
|
+
system += `\n${opts.system}\n`;
|
|
55
61
|
}
|
|
56
62
|
function maybeSend(options) {
|
|
57
|
-
if (currentOutputAgent === null || currentOutputAgent === agentId) {
|
|
63
|
+
if (currentOutputAgent === null || currentOutputAgent === opts.agentId) {
|
|
58
64
|
Queue.offerUnsafe(output, options.part);
|
|
59
65
|
if (options.acquire) {
|
|
60
|
-
currentOutputAgent = agentId;
|
|
66
|
+
currentOutputAgent = opts.agentId;
|
|
61
67
|
}
|
|
62
68
|
if (options.release) {
|
|
63
69
|
currentOutputAgent = null;
|
|
@@ -74,92 +80,105 @@ ${content}
|
|
|
74
80
|
}
|
|
75
81
|
return;
|
|
76
82
|
}
|
|
77
|
-
let state = outputBuffer.get(agentId);
|
|
83
|
+
let state = outputBuffer.get(opts.agentId);
|
|
78
84
|
if (!state) {
|
|
79
85
|
state = [];
|
|
80
|
-
outputBuffer.set(agentId, state);
|
|
86
|
+
outputBuffer.set(opts.agentId, state);
|
|
81
87
|
}
|
|
82
88
|
state.push(options.part);
|
|
83
89
|
return;
|
|
84
90
|
}
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
91
|
+
const spawnSubagent = Effect.fnUntraced(function* (prompt) {
|
|
92
|
+
let id = agentCounter++;
|
|
93
|
+
const stream = spawn({
|
|
94
|
+
agentId: id,
|
|
95
|
+
prompt: Prompt.make(`You have been asked using the "delegate" function to complete the following task. Try to avoid using the "delegate" function yourself unless strictly necessary:
|
|
89
96
|
|
|
90
|
-
${prompt}`)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
},
|
|
121
|
-
}
|
|
97
|
+
${prompt}`),
|
|
98
|
+
system: opts.system,
|
|
99
|
+
disableHistory: true,
|
|
100
|
+
});
|
|
101
|
+
const provider = yield* ProviderName;
|
|
102
|
+
const model = yield* ModelName;
|
|
103
|
+
maybeSend({
|
|
104
|
+
agentId: opts.agentId,
|
|
105
|
+
part: new SubagentStart({ id, prompt, model, provider }),
|
|
106
|
+
release: true,
|
|
107
|
+
});
|
|
108
|
+
return yield* stream.pipe(Stream.runForEachArray((parts) => {
|
|
109
|
+
for (const part of parts) {
|
|
110
|
+
switch (part._tag) {
|
|
111
|
+
case "AgentStart":
|
|
112
|
+
break;
|
|
113
|
+
case "SubagentStart":
|
|
114
|
+
case "SubagentComplete":
|
|
115
|
+
case "SubagentPart":
|
|
116
|
+
Queue.offerUnsafe(output, part);
|
|
117
|
+
break;
|
|
118
|
+
default:
|
|
119
|
+
Queue.offerUnsafe(output, new SubagentPart({ id, part }));
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return Effect.void;
|
|
124
|
+
}), Effect.as(""), Effect.catchTag("AgentFinished", (finished) => {
|
|
125
|
+
Queue.offerUnsafe(output, new SubagentComplete({ id, summary: finished.summary }));
|
|
126
|
+
return Effect.succeed(finished.summary);
|
|
127
|
+
}), Effect.orDie);
|
|
128
|
+
}, Option.isSome(subagentModel)
|
|
129
|
+
? Effect.provideServices(subagentModel.value)
|
|
130
|
+
: Effect.provideServices(services));
|
|
122
131
|
const executeScript = Effect.fnUntraced(function* (script) {
|
|
123
132
|
maybeSend({ agentId, part: new ScriptEnd(), release: true });
|
|
124
|
-
const output = yield* pipe(executor.execute({
|
|
133
|
+
const output = yield* pipe(executor.execute({
|
|
134
|
+
script,
|
|
135
|
+
onSubagent: spawnSubagent,
|
|
136
|
+
onTaskComplete: (summary) => Effect.sync(() => {
|
|
137
|
+
finalSummary = Option.some(summary);
|
|
138
|
+
}),
|
|
139
|
+
}), Stream.mkString);
|
|
125
140
|
maybeSend({ agentId, part: new ScriptOutput({ output }) });
|
|
126
141
|
return output;
|
|
127
142
|
});
|
|
128
143
|
if (!modelConfig.systemPromptTransform) {
|
|
129
|
-
prompt
|
|
144
|
+
MutableRef.update(prompt, Prompt.setSystem(system));
|
|
130
145
|
}
|
|
131
146
|
let currentScript = "";
|
|
132
147
|
yield* Effect.gen(function* () {
|
|
133
148
|
while (true) {
|
|
134
149
|
if (!singleToolMode && currentScript.length > 0) {
|
|
135
150
|
const result = yield* executeScript(currentScript);
|
|
136
|
-
prompt
|
|
151
|
+
MutableRef.update(prompt, Prompt.concat([
|
|
137
152
|
{
|
|
138
|
-
role: modelConfig.supportsAssistantPrefill
|
|
153
|
+
role: modelConfig.supportsAssistantPrefill
|
|
154
|
+
? "assistant"
|
|
155
|
+
: "user",
|
|
139
156
|
content: `Javascript output:\n\n${result}`,
|
|
140
157
|
},
|
|
141
|
-
]);
|
|
158
|
+
]));
|
|
142
159
|
currentScript = "";
|
|
143
160
|
}
|
|
144
|
-
if (
|
|
145
|
-
yield* Queue.fail(output, new AgentFinished({ summary:
|
|
161
|
+
if (Option.isSome(finalSummary)) {
|
|
162
|
+
yield* Queue.fail(output, new AgentFinished({ summary: finalSummary.value }));
|
|
146
163
|
return;
|
|
147
164
|
}
|
|
148
165
|
if (pendingMessages.size > 0) {
|
|
149
|
-
prompt
|
|
166
|
+
MutableRef.update(prompt, Prompt.concat(Array.Array.from(pendingMessages, ({ message, resume }) => {
|
|
150
167
|
resume(Effect.void);
|
|
151
168
|
return {
|
|
152
169
|
role: "user",
|
|
153
170
|
content: message,
|
|
154
171
|
};
|
|
155
|
-
}));
|
|
172
|
+
})));
|
|
156
173
|
pendingMessages.clear();
|
|
157
174
|
}
|
|
158
175
|
// oxlint-disable-next-line typescript/no-explicit-any
|
|
159
176
|
let response = Array.empty();
|
|
160
177
|
let reasoningStarted = false;
|
|
161
178
|
let hadReasoningDelta = false;
|
|
162
|
-
yield* pipe(ai.streamText(singleToolMode
|
|
179
|
+
yield* pipe(ai.streamText(singleToolMode
|
|
180
|
+
? { prompt: prompt.current, toolkit: singleTool }
|
|
181
|
+
: { prompt: prompt.current }), Stream.takeUntil((part) => {
|
|
163
182
|
if (!singleToolMode &&
|
|
164
183
|
part.type === "text-end" &&
|
|
165
184
|
currentScript.trim().length > 0) {
|
|
@@ -269,28 +288,36 @@ ${prompt}`));
|
|
|
269
288
|
}), modelConfig.systemPromptTransform
|
|
270
289
|
? (effect) => modelConfig.systemPromptTransform(system, effect)
|
|
271
290
|
: identity);
|
|
272
|
-
prompt
|
|
291
|
+
MutableRef.update(prompt, Prompt.concat(Prompt.fromResponseParts(response)));
|
|
273
292
|
currentScript = currentScript.trim();
|
|
274
293
|
}
|
|
275
|
-
}).pipe(Effect.
|
|
294
|
+
}).pipe(Effect.provideService(ScriptExecutor, (script) => {
|
|
276
295
|
maybeSend({ agentId, part: new ScriptStart() });
|
|
277
296
|
maybeSend({ agentId, part: new ScriptDelta({ delta: script }) });
|
|
278
297
|
return executeScript(script);
|
|
279
|
-
})
|
|
298
|
+
}), Effect.catchCause((cause) => Queue.failCause(output, cause)), Effect.forkScoped);
|
|
299
|
+
yield* Queue.offer(output, new AgentStart({
|
|
300
|
+
id: opts.agentId,
|
|
301
|
+
prompt: opts.prompt,
|
|
302
|
+
provider: yield* ProviderName,
|
|
303
|
+
model: yield* ModelName,
|
|
304
|
+
}));
|
|
280
305
|
return Stream.fromQueue(output);
|
|
281
306
|
}, Stream.unwrap);
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
307
|
+
const sendLock = Semaphore.makeUnsafe(1);
|
|
308
|
+
return Agent.of({
|
|
309
|
+
[TypeId]: TypeId,
|
|
310
|
+
send: (options) => spawn({
|
|
311
|
+
agentId: agentCounter++,
|
|
312
|
+
prompt: Prompt.make(options.prompt),
|
|
313
|
+
system: options.system,
|
|
314
|
+
}).pipe(Stream.broadcast({ capacity: "unbounded", replay: 1 }), sendLock.withPermit),
|
|
287
315
|
steer: (message) => Effect.callback((resume) => {
|
|
288
316
|
const entry = { message, resume };
|
|
289
317
|
pendingMessages.add(entry);
|
|
290
318
|
return Effect.sync(() => pendingMessages.delete(entry));
|
|
291
319
|
}),
|
|
292
320
|
});
|
|
293
|
-
// oxlint-disable-next-line typescript/no-explicit-any
|
|
294
321
|
});
|
|
295
322
|
const defaultSystem = (options) => `You are a world-class software engineer: precise, rigorous, thoughtful, and relentlessly careful. You fully understand the task, verify assumptions, and produce minimal, correct, maintainable solutions. You make no mistakes.
|
|
296
323
|
|
|
@@ -384,6 +411,29 @@ const SingleToolHandlers = SingleTools.toLayer({
|
|
|
384
411
|
return yield* execute(script);
|
|
385
412
|
}),
|
|
386
413
|
});
|
|
414
|
+
/**
|
|
415
|
+
* @since 1.0.0
|
|
416
|
+
* @category Layers
|
|
417
|
+
*/
|
|
418
|
+
export const layer = Layer.effect(Agent, make);
|
|
419
|
+
/**
|
|
420
|
+
* Create an Agent layer that uses a local AgentExecutor.
|
|
421
|
+
*
|
|
422
|
+
* @since 1.0.0
|
|
423
|
+
* @category Layers
|
|
424
|
+
*/
|
|
425
|
+
export const layerLocal = (options) => layer.pipe(Layer.provide(AgentExecutor.layerLocal(options)));
|
|
426
|
+
/**
|
|
427
|
+
* @since 1.0.0
|
|
428
|
+
* @category Subagent model
|
|
429
|
+
*/
|
|
430
|
+
export class SubagentModel extends ServiceMap.Service()("clanka/Agent/SubagentModel") {
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* @since 1.0.0
|
|
434
|
+
* @category Subagent model
|
|
435
|
+
*/
|
|
436
|
+
export const layerSubagentModel = (layer) => Layer.effect(SubagentModel, Layer.build(layer));
|
|
387
437
|
/**
|
|
388
438
|
* @since 1.0.0
|
|
389
439
|
* @category System prompts
|
|
@@ -394,12 +444,19 @@ export class AgentModelConfig extends ServiceMap.Reference("clanka/Agent/SystemP
|
|
|
394
444
|
static layer = (options) => Layer.succeed(AgentModelConfig, options);
|
|
395
445
|
}
|
|
396
446
|
/**
|
|
397
|
-
* A layer that provides most of the common services needed to run an agent.
|
|
398
|
-
*
|
|
399
447
|
* @since 1.0.0
|
|
400
|
-
* @category
|
|
448
|
+
* @category Output
|
|
401
449
|
*/
|
|
402
|
-
export
|
|
450
|
+
export class AgentStart extends Schema.TaggedClass()("AgentStart", {
|
|
451
|
+
id: Schema.Number,
|
|
452
|
+
prompt: Prompt.Prompt,
|
|
453
|
+
provider: Schema.String,
|
|
454
|
+
model: Schema.String,
|
|
455
|
+
}) {
|
|
456
|
+
get modelAndProvider() {
|
|
457
|
+
return `${this.provider}/${this.model}`;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
403
460
|
/**
|
|
404
461
|
* @since 1.0.0
|
|
405
462
|
* @category Output
|
|
@@ -448,14 +505,6 @@ export class ScriptOutput extends Schema.TaggedClass()("ScriptOutput", {
|
|
|
448
505
|
output: Schema.String,
|
|
449
506
|
}) {
|
|
450
507
|
}
|
|
451
|
-
/**
|
|
452
|
-
* @since 1.0.0
|
|
453
|
-
* @category Output
|
|
454
|
-
*/
|
|
455
|
-
export class AgentFinished extends Schema.TaggedErrorClass()("AgentFinished", {
|
|
456
|
-
summary: Schema.String,
|
|
457
|
-
}) {
|
|
458
|
-
}
|
|
459
508
|
/**
|
|
460
509
|
* @since 1.0.0
|
|
461
510
|
* @category Output
|
|
@@ -502,6 +551,7 @@ export class SubagentPart extends Schema.TaggedClass()("SubagentPart", {
|
|
|
502
551
|
* @category Output
|
|
503
552
|
*/
|
|
504
553
|
export const Output = Schema.Union([
|
|
554
|
+
AgentStart,
|
|
505
555
|
ReasoningStart,
|
|
506
556
|
ReasoningDelta,
|
|
507
557
|
ReasoningEnd,
|
|
@@ -513,4 +563,12 @@ export const Output = Schema.Union([
|
|
|
513
563
|
SubagentComplete,
|
|
514
564
|
SubagentPart,
|
|
515
565
|
]);
|
|
566
|
+
/**
|
|
567
|
+
* @since 1.0.0
|
|
568
|
+
* @category Output
|
|
569
|
+
*/
|
|
570
|
+
export class AgentFinished extends Schema.TaggedErrorClass()("AgentFinished", {
|
|
571
|
+
summary: Schema.String,
|
|
572
|
+
}) {
|
|
573
|
+
}
|
|
516
574
|
//# sourceMappingURL=Agent.js.map
|