@usestratus/sdk 0.12.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/dist/azure/chat-completions-model.d.ts +23 -0
- package/dist/azure/chat-completions-model.d.ts.map +1 -0
- package/dist/azure/chat-completions-model.js +293 -0
- package/dist/azure/chat-completions-model.js.map +1 -0
- package/dist/azure/endpoint.d.ts +18 -0
- package/dist/azure/endpoint.d.ts.map +1 -0
- package/dist/azure/endpoint.js +57 -0
- package/dist/azure/endpoint.js.map +1 -0
- package/dist/azure/index.d.ts +5 -0
- package/dist/azure/index.d.ts.map +1 -0
- package/dist/azure/index.js +3 -0
- package/dist/azure/index.js.map +1 -0
- package/dist/azure/responses-model.d.ts +25 -0
- package/dist/azure/responses-model.d.ts.map +1 -0
- package/dist/azure/responses-model.js +557 -0
- package/dist/azure/responses-model.js.map +1 -0
- package/dist/azure/sse-parser.d.ts +2 -0
- package/dist/azure/sse-parser.d.ts.map +1 -0
- package/dist/azure/sse-parser.js +39 -0
- package/dist/azure/sse-parser.js.map +1 -0
- package/dist/core/agent.d.ts +47 -0
- package/dist/core/agent.d.ts.map +1 -0
- package/dist/core/agent.js +74 -0
- package/dist/core/agent.js.map +1 -0
- package/dist/core/builtin-tools.d.ts +41 -0
- package/dist/core/builtin-tools.d.ts.map +1 -0
- package/dist/core/builtin-tools.js +80 -0
- package/dist/core/builtin-tools.js.map +1 -0
- package/dist/core/codemode/executor.d.ts +62 -0
- package/dist/core/codemode/executor.d.ts.map +1 -0
- package/dist/core/codemode/executor.js +188 -0
- package/dist/core/codemode/executor.js.map +1 -0
- package/dist/core/codemode/index.d.ts +62 -0
- package/dist/core/codemode/index.d.ts.map +1 -0
- package/dist/core/codemode/index.js +104 -0
- package/dist/core/codemode/index.js.map +1 -0
- package/dist/core/codemode/types.d.ts +24 -0
- package/dist/core/codemode/types.d.ts.map +1 -0
- package/dist/core/codemode/types.js +405 -0
- package/dist/core/codemode/types.js.map +1 -0
- package/dist/core/context.d.ts +10 -0
- package/dist/core/context.d.ts.map +1 -0
- package/dist/core/context.js +32 -0
- package/dist/core/context.js.map +1 -0
- package/dist/core/cost.d.ts +9 -0
- package/dist/core/cost.d.ts.map +1 -0
- package/dist/core/cost.js +14 -0
- package/dist/core/cost.js.map +1 -0
- package/dist/core/errors.d.ts +48 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +85 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/guardrails.d.ts +39 -0
- package/dist/core/guardrails.d.ts.map +1 -0
- package/dist/core/guardrails.js +40 -0
- package/dist/core/guardrails.js.map +1 -0
- package/dist/core/handoff.d.ts +35 -0
- package/dist/core/handoff.d.ts.map +1 -0
- package/dist/core/handoff.js +39 -0
- package/dist/core/handoff.js.map +1 -0
- package/dist/core/hooks.d.ts +154 -0
- package/dist/core/hooks.d.ts.map +1 -0
- package/dist/core/hooks.js +2 -0
- package/dist/core/hooks.js.map +1 -0
- package/dist/core/hosted-tool.d.ts +11 -0
- package/dist/core/hosted-tool.d.ts.map +1 -0
- package/dist/core/hosted-tool.js +7 -0
- package/dist/core/hosted-tool.js.map +1 -0
- package/dist/core/index.d.ts +35 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +18 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/model.d.ts +56 -0
- package/dist/core/model.d.ts.map +1 -0
- package/dist/core/model.js +2 -0
- package/dist/core/model.js.map +1 -0
- package/dist/core/result.d.ts +34 -0
- package/dist/core/result.d.ts.map +1 -0
- package/dist/core/result.js +31 -0
- package/dist/core/result.js.map +1 -0
- package/dist/core/run.d.ts +52 -0
- package/dist/core/run.d.ts.map +1 -0
- package/dist/core/run.js +972 -0
- package/dist/core/run.js.map +1 -0
- package/dist/core/session.d.ts +77 -0
- package/dist/core/session.d.ts.map +1 -0
- package/dist/core/session.js +160 -0
- package/dist/core/session.js.map +1 -0
- package/dist/core/subagent.d.ts +30 -0
- package/dist/core/subagent.d.ts.map +1 -0
- package/dist/core/subagent.js +52 -0
- package/dist/core/subagent.js.map +1 -0
- package/dist/core/todo.d.ts +56 -0
- package/dist/core/todo.d.ts.map +1 -0
- package/dist/core/todo.js +76 -0
- package/dist/core/todo.js.map +1 -0
- package/dist/core/tool.d.ts +26 -0
- package/dist/core/tool.d.ts.map +1 -0
- package/dist/core/tool.js +23 -0
- package/dist/core/tool.js.map +1 -0
- package/dist/core/tracing.d.ts +31 -0
- package/dist/core/tracing.d.ts.map +1 -0
- package/dist/core/tracing.js +62 -0
- package/dist/core/tracing.js.map +1 -0
- package/dist/core/types.d.ts +106 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +2 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/utils/zod.d.ts +5 -0
- package/dist/core/utils/zod.d.ts.map +1 -0
- package/dist/core/utils/zod.js +7 -0
- package/dist/core/utils/zod.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { InputGuardrail, OutputGuardrail } from "./guardrails";
|
|
3
|
+
import { type Handoff } from "./handoff";
|
|
4
|
+
import type { AgentHooks } from "./hooks";
|
|
5
|
+
import type { AgentTool } from "./hosted-tool";
|
|
6
|
+
import type { Model } from "./model";
|
|
7
|
+
import type { SubAgent } from "./subagent";
|
|
8
|
+
import type { ModelSettings, ResponseFormat, ToolUseBehavior } from "./types";
|
|
9
|
+
export type Instructions<TContext> = string | ((context: TContext) => string | Promise<string>);
|
|
10
|
+
export type HandoffInput<TContext> = Agent<TContext> | Handoff<TContext>;
|
|
11
|
+
export interface AgentConfig<TContext, TOutput = undefined> {
|
|
12
|
+
name: string;
|
|
13
|
+
instructions?: Instructions<TContext>;
|
|
14
|
+
model?: Model;
|
|
15
|
+
tools?: AgentTool[];
|
|
16
|
+
subagents?: SubAgent[];
|
|
17
|
+
modelSettings?: ModelSettings;
|
|
18
|
+
responseFormat?: ResponseFormat;
|
|
19
|
+
outputType?: z.ZodType<TOutput>;
|
|
20
|
+
handoffs?: HandoffInput<TContext>[];
|
|
21
|
+
handoffDescription?: string;
|
|
22
|
+
inputGuardrails?: InputGuardrail<TContext>[];
|
|
23
|
+
outputGuardrails?: OutputGuardrail<TContext>[];
|
|
24
|
+
hooks?: AgentHooks<TContext>;
|
|
25
|
+
toolUseBehavior?: ToolUseBehavior;
|
|
26
|
+
}
|
|
27
|
+
export declare class Agent<TContext = unknown, TOutput = undefined> {
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly instructions?: Instructions<TContext>;
|
|
30
|
+
readonly model?: Model;
|
|
31
|
+
readonly tools: AgentTool[];
|
|
32
|
+
readonly subagents: SubAgent[];
|
|
33
|
+
readonly modelSettings?: ModelSettings;
|
|
34
|
+
readonly responseFormat?: ResponseFormat;
|
|
35
|
+
readonly outputType?: z.ZodType<TOutput>;
|
|
36
|
+
readonly handoffs: Handoff<TContext>[];
|
|
37
|
+
readonly handoffDescription?: string;
|
|
38
|
+
readonly inputGuardrails: InputGuardrail<TContext>[];
|
|
39
|
+
readonly outputGuardrails: OutputGuardrail<TContext>[];
|
|
40
|
+
readonly hooks: AgentHooks<TContext>;
|
|
41
|
+
readonly toolUseBehavior: ToolUseBehavior;
|
|
42
|
+
constructor(config: AgentConfig<TContext, TOutput>);
|
|
43
|
+
getSystemPrompt(context: TContext): Promise<string | undefined>;
|
|
44
|
+
getResponseFormat(): ResponseFormat | undefined;
|
|
45
|
+
clone(overrides: Partial<AgentConfig<TContext, TOutput>>): Agent<TContext, TOutput>;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/core/agent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE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AAiB,IAAI,cAAc,GAAG,SAAS;IAc/C,KAAK,CAAC,SAAS,EAAE,OAAO,CAAC,WAAW,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,GAAG,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC;CAmBnF"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { handoff as normalizeHandoff } from "./handoff";
|
|
2
|
+
import { zodToJsonSchema } from "./utils/zod";
|
|
3
|
+
export class Agent {
|
|
4
|
+
name;
|
|
5
|
+
instructions;
|
|
6
|
+
model;
|
|
7
|
+
tools;
|
|
8
|
+
subagents;
|
|
9
|
+
modelSettings;
|
|
10
|
+
responseFormat;
|
|
11
|
+
outputType;
|
|
12
|
+
handoffs;
|
|
13
|
+
handoffDescription;
|
|
14
|
+
inputGuardrails;
|
|
15
|
+
outputGuardrails;
|
|
16
|
+
hooks;
|
|
17
|
+
toolUseBehavior;
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.name = config.name;
|
|
20
|
+
this.instructions = config.instructions;
|
|
21
|
+
this.model = config.model;
|
|
22
|
+
this.tools = config.tools ?? [];
|
|
23
|
+
this.subagents = config.subagents ?? [];
|
|
24
|
+
this.modelSettings = config.modelSettings;
|
|
25
|
+
this.responseFormat = config.responseFormat;
|
|
26
|
+
this.outputType = config.outputType;
|
|
27
|
+
this.handoffs = (config.handoffs ?? []).map((h) => h instanceof Agent ? normalizeHandoff(h) : h);
|
|
28
|
+
this.handoffDescription = config.handoffDescription;
|
|
29
|
+
this.inputGuardrails = config.inputGuardrails ?? [];
|
|
30
|
+
this.outputGuardrails = config.outputGuardrails ?? [];
|
|
31
|
+
this.hooks = config.hooks ?? {};
|
|
32
|
+
this.toolUseBehavior = config.toolUseBehavior ?? "run_llm_again";
|
|
33
|
+
}
|
|
34
|
+
async getSystemPrompt(context) {
|
|
35
|
+
if (this.instructions === undefined)
|
|
36
|
+
return undefined;
|
|
37
|
+
if (typeof this.instructions === "string")
|
|
38
|
+
return this.instructions;
|
|
39
|
+
return this.instructions(context);
|
|
40
|
+
}
|
|
41
|
+
getResponseFormat() {
|
|
42
|
+
if (this.outputType) {
|
|
43
|
+
return {
|
|
44
|
+
type: "json_schema",
|
|
45
|
+
json_schema: {
|
|
46
|
+
name: "response",
|
|
47
|
+
schema: zodToJsonSchema(this.outputType),
|
|
48
|
+
strict: true,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return this.responseFormat;
|
|
53
|
+
}
|
|
54
|
+
clone(overrides) {
|
|
55
|
+
return new Agent({
|
|
56
|
+
name: this.name,
|
|
57
|
+
instructions: this.instructions,
|
|
58
|
+
model: this.model,
|
|
59
|
+
tools: this.tools,
|
|
60
|
+
subagents: this.subagents,
|
|
61
|
+
modelSettings: this.modelSettings,
|
|
62
|
+
responseFormat: this.responseFormat,
|
|
63
|
+
outputType: this.outputType,
|
|
64
|
+
handoffs: this.handoffs,
|
|
65
|
+
handoffDescription: this.handoffDescription,
|
|
66
|
+
inputGuardrails: this.inputGuardrails,
|
|
67
|
+
outputGuardrails: this.outputGuardrails,
|
|
68
|
+
hooks: this.hooks,
|
|
69
|
+
toolUseBehavior: this.toolUseBehavior,
|
|
70
|
+
...overrides,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../../src/core/agent.ts"],"names":[],"mappings":"AAEA,OAAO,EAAgB,OAAO,IAAI,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAMtE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAuB9C,MAAM,OAAO,KAAK;IACR,IAAI,CAAS;IACb,YAAY,CAA0B;IACtC,KAAK,CAAS;IACd,KAAK,CAAc;IACnB,SAAS,CAAa;IACtB,aAAa,CAAiB;IAC9B,cAAc,CAAkB;IAChC,UAAU,CAAsB;IAChC,QAAQ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"approximate";
|
|
5
|
+
city?: string;
|
|
6
|
+
state?: string;
|
|
7
|
+
country?: string;
|
|
8
|
+
region?: string;
|
|
9
|
+
};
|
|
10
|
+
searchContextSize?: "low" | "medium" | "high";
|
|
11
|
+
}
|
|
12
|
+
export declare function webSearchTool(config?: WebSearchToolConfig): HostedTool;
|
|
13
|
+
export interface CodeInterpreterToolConfig {
|
|
14
|
+
container?: {
|
|
15
|
+
type: "auto" | string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export declare function codeInterpreterTool(config?: CodeInterpreterToolConfig): HostedTool;
|
|
19
|
+
export interface McpToolConfig {
|
|
20
|
+
serverLabel: string;
|
|
21
|
+
serverUrl: string;
|
|
22
|
+
requireApproval?: "always" | "never" | {
|
|
23
|
+
always?: string[];
|
|
24
|
+
never?: string[];
|
|
25
|
+
};
|
|
26
|
+
headers?: Record<string, string>;
|
|
27
|
+
}
|
|
28
|
+
export declare function mcpTool(config: McpToolConfig): HostedTool;
|
|
29
|
+
export declare function imageGenerationTool(): HostedTool;
|
|
30
|
+
export interface FileSearchToolConfig {
|
|
31
|
+
vectorStoreIds: string[];
|
|
32
|
+
maxNumResults?: number;
|
|
33
|
+
}
|
|
34
|
+
export declare function fileSearchTool(config: FileSearchToolConfig): HostedTool;
|
|
35
|
+
export interface ComputerUseToolConfig {
|
|
36
|
+
displayWidth: number;
|
|
37
|
+
displayHeight: number;
|
|
38
|
+
environment?: "windows" | "mac" | "linux";
|
|
39
|
+
}
|
|
40
|
+
export declare function computerUseTool(config: ComputerUseToolConfig): HostedTool;
|
|
41
|
+
//# sourceMappingURL=builtin-tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builtin-tools.d.ts","sourceRoot":"","sources":["../../src/core/builtin-tools.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGhD,MAAM,WAAW,mBAAmB;IACnC,YAAY,CAAC,EAAE;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ED,wBAAgB,eAAe,CAAC,MAAM,EAAE,qBAAqB,GAAG,UAAU,CAWzE"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export function webSearchTool(config) {
|
|
2
|
+
const definition = {
|
|
3
|
+
type: "web_search_preview",
|
|
4
|
+
};
|
|
5
|
+
if (config?.userLocation) {
|
|
6
|
+
definition.user_location = config.userLocation;
|
|
7
|
+
}
|
|
8
|
+
if (config?.searchContextSize) {
|
|
9
|
+
definition.search_context_size = config.searchContextSize;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
type: "hosted",
|
|
13
|
+
name: "web_search_preview",
|
|
14
|
+
definition,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export function codeInterpreterTool(config) {
|
|
18
|
+
return {
|
|
19
|
+
type: "hosted",
|
|
20
|
+
name: "code_interpreter",
|
|
21
|
+
definition: {
|
|
22
|
+
type: "code_interpreter",
|
|
23
|
+
container: config?.container ?? { type: "auto" },
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export function mcpTool(config) {
|
|
28
|
+
const definition = {
|
|
29
|
+
type: "mcp",
|
|
30
|
+
server_label: config.serverLabel,
|
|
31
|
+
server_url: config.serverUrl,
|
|
32
|
+
};
|
|
33
|
+
if (config.requireApproval !== undefined) {
|
|
34
|
+
definition.require_approval = config.requireApproval;
|
|
35
|
+
}
|
|
36
|
+
if (config.headers) {
|
|
37
|
+
definition.headers = config.headers;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
type: "hosted",
|
|
41
|
+
name: `mcp:${config.serverLabel}`,
|
|
42
|
+
definition,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export function imageGenerationTool() {
|
|
46
|
+
return {
|
|
47
|
+
type: "hosted",
|
|
48
|
+
name: "image_generation",
|
|
49
|
+
definition: {
|
|
50
|
+
type: "image_generation",
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export function fileSearchTool(config) {
|
|
55
|
+
const definition = {
|
|
56
|
+
type: "file_search",
|
|
57
|
+
vector_store_ids: config.vectorStoreIds,
|
|
58
|
+
};
|
|
59
|
+
if (config.maxNumResults !== undefined) {
|
|
60
|
+
definition.max_num_results = config.maxNumResults;
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
type: "hosted",
|
|
64
|
+
name: "file_search",
|
|
65
|
+
definition,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export function computerUseTool(config) {
|
|
69
|
+
return {
|
|
70
|
+
type: "hosted",
|
|
71
|
+
name: "computer_use_preview",
|
|
72
|
+
definition: {
|
|
73
|
+
type: "computer_use_preview",
|
|
74
|
+
display_width: config.displayWidth,
|
|
75
|
+
display_height: config.displayHeight,
|
|
76
|
+
environment: config.environment ?? "linux",
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=builtin-tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builtin-tools.js","sourceRoot":"","sources":["../../src/core/builtin-tools.ts"],"names":[],"mappings":"AAcA,MAAM,UAAU,aAAa,CAAC,MAA4B;IACzD,MAAM,UAAU,GAAyB;QACxC,IAAI,EAAE,oBAAoB;KAC1B,CAAC;IACF,IAAI,MAAM,EAAE,YAAY,EAAE,CAAC;QAC1B,UAAU,CAAC,aAAa,GAAG,MAAM,CAAC,YAAY,CAAC;IAChD,CAAC;IACD,IAAI,MAAM,EAAE,iBAAiB,EAAE,CAAC;QAC/B,UAAU,CAAC,mBAAmB,GAAG,MAAM,CAAC,iBAAiB,CAAC;IAC3D,CAAC;IACD,OAAO;QACN,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,oBAAoB;QAC1B,UAAU;KACV,CAAC;AACH,CAAC;AAQD,MAAM,UAAU,mBAAmB,CAAC,MAAkC;IACrE,OAAO;QACN,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,kBAAkB;Q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QAAQ;QACd,IAAI,EAAE,sBAAsB;QAC5B,UAAU,EAAE;YACX,IAAI,EAAE,sBAAsB;YAC5B,aAAa,EAAE,MAAM,CAAC,YAAY;YAClC,cAAc,EAAE,MAAM,CAAC,aAAa;YACpC,WAAW,EAAE,MAAM,CAAC,WAAW,IAAI,OAAO;SAC1C;KACD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code mode executor: runs LLM-generated code in a sandbox with tool access.
|
|
3
|
+
*
|
|
4
|
+
* The Executor interface is deliberately minimal — implement it to run code
|
|
5
|
+
* in any sandbox (Node VM, Bun, QuickJS, containers, Cloudflare Workers, etc.).
|
|
6
|
+
*
|
|
7
|
+
* Two built-in executors:
|
|
8
|
+
* - FunctionExecutor: fast, same-process (NOT sandboxed)
|
|
9
|
+
* - WorkerExecutor: isolated via worker_threads (separate V8 context, no host access)
|
|
10
|
+
*/
|
|
11
|
+
export interface ExecuteResult {
|
|
12
|
+
result: unknown;
|
|
13
|
+
error?: string;
|
|
14
|
+
logs?: string[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* An executor runs LLM-generated code in a sandbox, making the provided
|
|
18
|
+
* tool functions callable as `codemode.*` inside the sandbox.
|
|
19
|
+
*
|
|
20
|
+
* Implementations should never throw — errors are returned in `ExecuteResult.error`.
|
|
21
|
+
*/
|
|
22
|
+
export interface Executor {
|
|
23
|
+
execute(code: string, fns: Record<string, (...args: unknown[]) => Promise<unknown>>): Promise<ExecuteResult>;
|
|
24
|
+
}
|
|
25
|
+
export interface FunctionExecutorOptions {
|
|
26
|
+
/** Timeout in milliseconds for code execution. Defaults to 30000 (30s). */
|
|
27
|
+
timeout?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Executes code using AsyncFunction (works in Node.js and Bun).
|
|
31
|
+
* Tool calls are injected via the `codemode` parameter.
|
|
32
|
+
*
|
|
33
|
+
* This is NOT a secure sandbox — it runs in the same V8 isolate.
|
|
34
|
+
* For production use with untrusted code, implement a custom Executor
|
|
35
|
+
* using isolated-vm, Cloudflare Workers, or containers.
|
|
36
|
+
*/
|
|
37
|
+
export declare class FunctionExecutor implements Executor {
|
|
38
|
+
#private;
|
|
39
|
+
constructor(options?: FunctionExecutorOptions);
|
|
40
|
+
execute(code: string, fns: Record<string, (...args: unknown[]) => Promise<unknown>>): Promise<ExecuteResult>;
|
|
41
|
+
}
|
|
42
|
+
export interface WorkerExecutorOptions {
|
|
43
|
+
/** Timeout in milliseconds for code execution. Defaults to 30000 (30s). */
|
|
44
|
+
timeout?: number;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Executes code in an isolated worker thread via `worker_threads`.
|
|
48
|
+
* Each execution spawns a fresh worker — the code runs in a separate
|
|
49
|
+
* V8 context with no access to the host's globals, `require`, or filesystem.
|
|
50
|
+
*
|
|
51
|
+
* Tool calls are dispatched back to the parent via `postMessage` and
|
|
52
|
+
* handled by the host, which calls the real tool functions and sends
|
|
53
|
+
* results back.
|
|
54
|
+
*
|
|
55
|
+
* Works in both Bun and Node.js.
|
|
56
|
+
*/
|
|
57
|
+
export declare class WorkerExecutor implements Executor {
|
|
58
|
+
#private;
|
|
59
|
+
constructor(options?: WorkerExecutorOptions);
|
|
60
|
+
execute(code: string, fns: Record<string, (...args: unknown[]) => Promise<unknown>>): Promise<ExecuteResult>;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../../src/core/codemode/executor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,MAAM,WAAW,aAAa;IAC7B,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CAChB;AAED;;;;;GAKG;AACH,MAAM,WAAW,QAAQ;IACxB,OAAO,CACN,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC,GAC3D,OAAO,CAAC,aAAa,CAAC,CAAC;CAC1B;AAED,MAAM,WAAW,uBAAuB;IACvC,2EAA2E;IAC3E,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;GAOG;AACH,qBAAa,gBAAiB,YAAW,QAAQ;;gBAGpC,OAAO,CAAC,EAAE,uBAAuB;IAIvC,OAAO,CACZ,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC,GAC3D,OAAO,CAAC,aAAa,CAAC;CA6CzB;AAID,MAAM,WAAW,qBAAqB;IACrC,2EAA2E;IAC3E,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAgDD;;;;;;;;;;GAUG;AACH,qBAAa,cAAe,YAAW,QAAQ;;gBAGlC,OAAO,CAAC,EAAE,qBAAqB;IAIrC,OAAO,CACZ,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC,GAC3D,OAAO,CAAC,aAAa,CAAC;CAwEzB"}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code mode executor: runs LLM-generated code in a sandbox with tool access.
|
|
3
|
+
*
|
|
4
|
+
* The Executor interface is deliberately minimal — implement it to run code
|
|
5
|
+
* in any sandbox (Node VM, Bun, QuickJS, containers, Cloudflare Workers, etc.).
|
|
6
|
+
*
|
|
7
|
+
* Two built-in executors:
|
|
8
|
+
* - FunctionExecutor: fast, same-process (NOT sandboxed)
|
|
9
|
+
* - WorkerExecutor: isolated via worker_threads (separate V8 context, no host access)
|
|
10
|
+
*/
|
|
11
|
+
import { Worker } from "node:worker_threads";
|
|
12
|
+
/**
|
|
13
|
+
* Executes code using AsyncFunction (works in Node.js and Bun).
|
|
14
|
+
* Tool calls are injected via the `codemode` parameter.
|
|
15
|
+
*
|
|
16
|
+
* This is NOT a secure sandbox — it runs in the same V8 isolate.
|
|
17
|
+
* For production use with untrusted code, implement a custom Executor
|
|
18
|
+
* using isolated-vm, Cloudflare Workers, or containers.
|
|
19
|
+
*/
|
|
20
|
+
export class FunctionExecutor {
|
|
21
|
+
#timeout;
|
|
22
|
+
constructor(options) {
|
|
23
|
+
this.#timeout = options?.timeout ?? 30_000;
|
|
24
|
+
}
|
|
25
|
+
async execute(code, fns) {
|
|
26
|
+
const logs = [];
|
|
27
|
+
// Create a console proxy that captures output
|
|
28
|
+
const consoleProxy = {
|
|
29
|
+
log: (...args) => {
|
|
30
|
+
logs.push(args.map(String).join(" "));
|
|
31
|
+
},
|
|
32
|
+
warn: (...args) => {
|
|
33
|
+
logs.push(`[warn] ${args.map(String).join(" ")}`);
|
|
34
|
+
},
|
|
35
|
+
error: (...args) => {
|
|
36
|
+
logs.push(`[error] ${args.map(String).join(" ")}`);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
try {
|
|
40
|
+
// biome-ignore lint/security/noGlobalEval: Required for code mode execution
|
|
41
|
+
const AsyncFunction = Object.getPrototypeOf(async () => { }).constructor;
|
|
42
|
+
const fn = new AsyncFunction("codemode", "console", `return await (${code})()`);
|
|
43
|
+
const timeoutMs = this.#timeout;
|
|
44
|
+
const result = await Promise.race([
|
|
45
|
+
fn(fns, consoleProxy),
|
|
46
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error("Execution timed out")), timeoutMs)),
|
|
47
|
+
]);
|
|
48
|
+
return { result, logs };
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
return {
|
|
52
|
+
result: undefined,
|
|
53
|
+
error: err instanceof Error ? err.message : String(err),
|
|
54
|
+
logs,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The code that runs inside the worker thread. It:
|
|
61
|
+
* 1. Creates a `codemode` Proxy that dispatches tool calls to the parent via postMessage
|
|
62
|
+
* 2. Overrides console.log/warn/error to capture output
|
|
63
|
+
* 3. Executes the LLM-generated code via `new Function()`
|
|
64
|
+
* 4. Sends back the result (or error) + captured logs
|
|
65
|
+
*/
|
|
66
|
+
const WORKER_BOOTSTRAP = `
|
|
67
|
+
const { parentPort } = require("worker_threads");
|
|
68
|
+
|
|
69
|
+
const codemode = new Proxy({}, {
|
|
70
|
+
get: (_, name) => async (args) => {
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
const callId = String(Math.random());
|
|
73
|
+
parentPort.postMessage({ type: "tool_call", callId, name: String(name), args });
|
|
74
|
+
const handler = (msg) => {
|
|
75
|
+
if (msg.callId === callId) {
|
|
76
|
+
parentPort.off("message", handler);
|
|
77
|
+
if (msg.error) reject(new Error(msg.error));
|
|
78
|
+
else resolve(msg.result);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
parentPort.on("message", handler);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const __logs = [];
|
|
87
|
+
const __console = {
|
|
88
|
+
log: (...a) => __logs.push(a.map(String).join(" ")),
|
|
89
|
+
warn: (...a) => __logs.push("[warn] " + a.map(String).join(" ")),
|
|
90
|
+
error: (...a) => __logs.push("[error] " + a.map(String).join(" ")),
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
(async () => {
|
|
94
|
+
try {
|
|
95
|
+
const __code = process.env.__CODEMODE_CODE;
|
|
96
|
+
const fn = new Function("codemode", "console", "return (" + __code + ")()");
|
|
97
|
+
const result = await fn(codemode, __console);
|
|
98
|
+
parentPort.postMessage({ type: "done", result, logs: __logs });
|
|
99
|
+
} catch (err) {
|
|
100
|
+
parentPort.postMessage({ type: "done", error: err instanceof Error ? err.message : String(err), logs: __logs });
|
|
101
|
+
}
|
|
102
|
+
})();
|
|
103
|
+
`;
|
|
104
|
+
/**
|
|
105
|
+
* Executes code in an isolated worker thread via `worker_threads`.
|
|
106
|
+
* Each execution spawns a fresh worker — the code runs in a separate
|
|
107
|
+
* V8 context with no access to the host's globals, `require`, or filesystem.
|
|
108
|
+
*
|
|
109
|
+
* Tool calls are dispatched back to the parent via `postMessage` and
|
|
110
|
+
* handled by the host, which calls the real tool functions and sends
|
|
111
|
+
* results back.
|
|
112
|
+
*
|
|
113
|
+
* Works in both Bun and Node.js.
|
|
114
|
+
*/
|
|
115
|
+
export class WorkerExecutor {
|
|
116
|
+
#timeout;
|
|
117
|
+
constructor(options) {
|
|
118
|
+
this.#timeout = options?.timeout ?? 30_000;
|
|
119
|
+
}
|
|
120
|
+
async execute(code, fns) {
|
|
121
|
+
return new Promise((resolve) => {
|
|
122
|
+
const timeoutMs = this.#timeout;
|
|
123
|
+
let settled = false;
|
|
124
|
+
const worker = new Worker(WORKER_BOOTSTRAP, {
|
|
125
|
+
eval: true,
|
|
126
|
+
env: { __CODEMODE_CODE: code },
|
|
127
|
+
});
|
|
128
|
+
const timer = setTimeout(() => {
|
|
129
|
+
if (!settled) {
|
|
130
|
+
settled = true;
|
|
131
|
+
worker.terminate();
|
|
132
|
+
resolve({ result: undefined, error: "Execution timed out" });
|
|
133
|
+
}
|
|
134
|
+
}, timeoutMs);
|
|
135
|
+
worker.on("message", async (msg) => {
|
|
136
|
+
if (msg.type === "tool_call") {
|
|
137
|
+
const { callId, name, args } = msg;
|
|
138
|
+
const fn = fns[name];
|
|
139
|
+
if (!fn) {
|
|
140
|
+
worker.postMessage({ callId, error: `Tool "${name}" not found` });
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
const result = await fn(args);
|
|
145
|
+
worker.postMessage({ callId, result });
|
|
146
|
+
}
|
|
147
|
+
catch (err) {
|
|
148
|
+
worker.postMessage({
|
|
149
|
+
callId,
|
|
150
|
+
error: err instanceof Error ? err.message : String(err),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else if (msg.type === "done") {
|
|
155
|
+
if (!settled) {
|
|
156
|
+
settled = true;
|
|
157
|
+
clearTimeout(timer);
|
|
158
|
+
worker.terminate();
|
|
159
|
+
if (msg.error) {
|
|
160
|
+
resolve({
|
|
161
|
+
result: undefined,
|
|
162
|
+
error: msg.error,
|
|
163
|
+
logs: msg.logs,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
resolve({
|
|
168
|
+
result: msg.result,
|
|
169
|
+
logs: msg.logs,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
worker.on("error", (err) => {
|
|
176
|
+
if (!settled) {
|
|
177
|
+
settled = true;
|
|
178
|
+
clearTimeout(timer);
|
|
179
|
+
resolve({
|
|
180
|
+
result: undefined,
|
|
181
|
+
error: err instanceof Error ? err.message : String(err),
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.js","sourceRoot":"","sources":["../../../src/core/codemode/executor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AA0B7C;;;;;;;GAOG;AACH,MAAM,OAAO,gBAAgB;IACnB,QAAQ,CAAS;IAE1B,YAAY,OAAiC;QAC5C,IAAI,CAAC,QAAQ,GAAG,OAAO,EAAE,OAAO,IAAI,MAAM,CAAC;IAC5C,CAAC;IAED,KAAK,CAAC,OAAO,CACZ,IAAY,EACZ,GAA6D;QAE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A4B;6BACtC,CAAC,CAAC;wBACJ,CAAC;oBACF,CAAC;gBACF,CAAC;YACF,CAAC,CAAC,CAAC;YAEH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;gBAC1B,IAAI,CAAC,OAAO,EAAE,CAAC;oBACd,OAAO,GAAG,IAAI,CAAC;oBACf,YAAY,CAAC,KAAK,CAAC,CAAC;oBACpB,OAAO,CAAC;wBACP,MAAM,EAAE,SAAS;wBACjB,KAAK,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;qBACvD,CAAC,CAAC;gBACJ,CAAC;YACF,CAAC,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACJ,CAAC;CACD"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code mode: let LLMs write code that orchestrates tools instead of calling them one at a time.
|
|
3
|
+
*
|
|
4
|
+
* Inspired by Cloudflare's Code Mode and CodeAct — LLMs are better at writing code
|
|
5
|
+
* than making individual tool calls because they've seen millions of lines of real-world
|
|
6
|
+
* TypeScript but only contrived tool-calling examples.
|
|
7
|
+
*/
|
|
8
|
+
import type { FunctionTool } from "../tool";
|
|
9
|
+
import type { AgentTool } from "../hosted-tool";
|
|
10
|
+
import type { Executor } from "./executor";
|
|
11
|
+
export type { Executor, ExecuteResult } from "./executor";
|
|
12
|
+
export { FunctionExecutor, WorkerExecutor } from "./executor";
|
|
13
|
+
export type { FunctionExecutorOptions, WorkerExecutorOptions } from "./executor";
|
|
14
|
+
export { generateTypes, normalizeCode, sanitizeToolName } from "./types";
|
|
15
|
+
export interface CodeModeToolOptions {
|
|
16
|
+
/** The tools to make available inside the code sandbox. Hosted tools are filtered out. */
|
|
17
|
+
tools: AgentTool[];
|
|
18
|
+
/** The executor to run generated code in. */
|
|
19
|
+
executor: Executor;
|
|
20
|
+
/** Custom tool description. Use `{{types}}` as a placeholder for the generated type definitions. */
|
|
21
|
+
description?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface CodeModeOutput {
|
|
24
|
+
code: string;
|
|
25
|
+
result: unknown;
|
|
26
|
+
logs?: string[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create a code mode tool that allows LLMs to write and execute code
|
|
30
|
+
* with access to your tools in a sandboxed environment.
|
|
31
|
+
*
|
|
32
|
+
* Returns a Stratus `FunctionTool` that can be added to any agent's tools array.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { Agent, tool } from "stratus";
|
|
37
|
+
* import { createCodeModeTool, FunctionExecutor } from "stratus/core/codemode";
|
|
38
|
+
*
|
|
39
|
+
* const weatherTool = tool({
|
|
40
|
+
* name: "get_weather",
|
|
41
|
+
* description: "Get weather for a location",
|
|
42
|
+
* parameters: z.object({ location: z.string() }),
|
|
43
|
+
* execute: async (ctx, { location }) => `72°F, sunny in ${location}`,
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* const executor = new FunctionExecutor({ timeout: 10_000 });
|
|
47
|
+
* const codemode = createCodeModeTool({
|
|
48
|
+
* tools: [weatherTool],
|
|
49
|
+
* executor,
|
|
50
|
+
* });
|
|
51
|
+
*
|
|
52
|
+
* const agent = new Agent({
|
|
53
|
+
* name: "assistant",
|
|
54
|
+
* instructions: "You are a helpful assistant.",
|
|
55
|
+
* tools: [codemode],
|
|
56
|
+
* });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function createCodeModeTool<TContext = unknown>(options: CodeModeToolOptions): FunctionTool<{
|
|
60
|
+
code: string;
|
|
61
|
+
}, TContext>;
|
|
62
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/codemode/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C,YAAY,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC9D,YAAY,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACjF,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAiBzE,MAAM,WAAW,mBAAmB;IACnC,0FAA0F;IAC1F,KAAK,EAAE,SAAS,EAAE,CAAC;IACnB,6CAA6C;IAC7C,QAAQ,EAAE,QAAQ,CAAC;IACnB,oGAAoG;IACpG,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CAChB;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,GAAG,OAAO,EACpD,OAAO,EAAE,mBAAmB,GAC1B,YAAY,CAAC;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,EAAE,QAAQ,CAAC,CA+C1C"}
|