@ax-llm/ax 9.0.24 → 9.0.26
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/ai/anthropic/api.d.ts +1 -1
- package/ai/azure-openai/api.d.ts +1 -1
- package/ai/cohere/api.d.ts +1 -1
- package/ai/deepseek/api.d.ts +1 -1
- package/ai/google-gemini/api.d.ts +1 -1
- package/ai/google-gemini/types.d.ts +1 -1
- package/ai/groq/api.d.ts +1 -1
- package/ai/huggingface/api.d.ts +1 -1
- package/ai/mistral/api.d.ts +1 -1
- package/ai/ollama/api.d.ts +1 -1
- package/ai/openai/api.d.ts +1 -1
- package/ai/together/api.d.ts +1 -1
- package/cjs/ai/anthropic/api.d.ts +1 -1
- package/cjs/ai/azure-openai/api.d.ts +1 -1
- package/cjs/ai/cohere/api.d.ts +1 -1
- package/cjs/ai/deepseek/api.d.ts +1 -1
- package/cjs/ai/google-gemini/api.d.ts +1 -1
- package/cjs/ai/google-gemini/types.d.ts +1 -1
- package/cjs/ai/groq/api.d.ts +1 -1
- package/cjs/ai/huggingface/api.d.ts +1 -1
- package/cjs/ai/mistral/api.d.ts +1 -1
- package/cjs/ai/ollama/api.d.ts +1 -1
- package/cjs/ai/openai/api.d.ts +1 -1
- package/cjs/ai/together/api.d.ts +1 -1
- package/cjs/dsp/functions.js.map +1 -0
- package/cjs/dsp/generate.d.ts +5 -2
- package/cjs/dsp/generate.js +12 -5
- package/cjs/dsp/generate.js.map +1 -1
- package/cjs/dsp/jsonschema.js.map +1 -0
- package/cjs/funcs/code.d.ts +10 -3
- package/cjs/funcs/code.js +50 -54
- package/cjs/funcs/code.js.map +1 -1
- package/cjs/funcs/docker.d.ts +101 -0
- package/cjs/funcs/docker.js +217 -0
- package/cjs/funcs/docker.js.map +1 -0
- package/cjs/funcs/embed.d.ts +16 -5
- package/cjs/funcs/embed.js +38 -29
- package/cjs/funcs/embed.js.map +1 -1
- package/cjs/funcs/index.d.ts +1 -1
- package/cjs/funcs/index.js +1 -1
- package/cjs/funcs/index.js.map +1 -1
- package/cjs/prompts/react.js +7 -2
- package/cjs/prompts/react.js.map +1 -1
- package/dsp/functions.js.map +1 -0
- package/dsp/generate.d.ts +5 -2
- package/dsp/generate.js +12 -5
- package/dsp/generate.js.map +1 -1
- package/dsp/jsonschema.js.map +1 -0
- package/funcs/code.d.ts +10 -3
- package/funcs/code.js +48 -52
- package/funcs/code.js.map +1 -1
- package/funcs/docker.d.ts +101 -0
- package/funcs/docker.js +213 -0
- package/funcs/docker.js.map +1 -0
- package/funcs/embed.d.ts +16 -5
- package/funcs/embed.js +36 -27
- package/funcs/embed.js.map +1 -1
- package/funcs/index.d.ts +1 -1
- package/funcs/index.js +1 -1
- package/funcs/index.js.map +1 -1
- package/package.json +1 -1
- package/prompts/react.js +7 -2
- package/prompts/react.js.map +1 -1
- package/cjs/funcs/functions.js.map +0 -1
- package/cjs/funcs/jsonschema.js.map +0 -1
- package/funcs/functions.js.map +0 -1
- package/funcs/jsonschema.js.map +0 -1
- /package/cjs/{funcs → dsp}/functions.d.ts +0 -0
- /package/cjs/{funcs → dsp}/functions.js +0 -0
- /package/cjs/{funcs → dsp}/jsonschema.d.ts +0 -0
- /package/cjs/{funcs → dsp}/jsonschema.js +0 -0
- /package/{funcs → dsp}/functions.d.ts +0 -0
- /package/{funcs → dsp}/functions.js +0 -0
- /package/{funcs → dsp}/jsonschema.d.ts +0 -0
- /package/{funcs → dsp}/jsonschema.js +0 -0
package/funcs/code.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import type { AxFunction } from '../ai/
|
|
2
|
-
export declare
|
|
3
|
-
export declare enum AxCodeInterpreterPermission {
|
|
1
|
+
import type { AxFunction } from '../ai/types.js';
|
|
2
|
+
export declare enum AxJSInterpreterPermission {
|
|
4
3
|
FS = "node:fs",
|
|
5
4
|
NET = "net",
|
|
6
5
|
OS = "os",
|
|
7
6
|
CRYPTO = "crypto",
|
|
8
7
|
PROCESS = "process"
|
|
9
8
|
}
|
|
9
|
+
export declare class AxJSInterpreter {
|
|
10
|
+
private permissions;
|
|
11
|
+
constructor({ permissions }?: Readonly<{
|
|
12
|
+
permissions?: readonly AxJSInterpreterPermission[];
|
|
13
|
+
}> | undefined);
|
|
14
|
+
private codeInterpreterJavascript;
|
|
15
|
+
toFunction(): AxFunction;
|
|
16
|
+
}
|
package/funcs/code.js
CHANGED
|
@@ -5,59 +5,55 @@ import * as _fs from 'node:fs';
|
|
|
5
5
|
import * as _os from 'os';
|
|
6
6
|
import * as _process from 'process';
|
|
7
7
|
import { runInNewContext } from 'vm';
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
},
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
-
func: ({ code }) => {
|
|
23
|
-
return new Promise((resolve) => {
|
|
24
|
-
resolve(codeInterpreterJavascript(code, permissions));
|
|
25
|
-
});
|
|
8
|
+
export var AxJSInterpreterPermission;
|
|
9
|
+
(function (AxJSInterpreterPermission) {
|
|
10
|
+
AxJSInterpreterPermission["FS"] = "node:fs";
|
|
11
|
+
AxJSInterpreterPermission["NET"] = "net";
|
|
12
|
+
AxJSInterpreterPermission["OS"] = "os";
|
|
13
|
+
AxJSInterpreterPermission["CRYPTO"] = "crypto";
|
|
14
|
+
AxJSInterpreterPermission["PROCESS"] = "process";
|
|
15
|
+
})(AxJSInterpreterPermission || (AxJSInterpreterPermission = {}));
|
|
16
|
+
export class AxJSInterpreter {
|
|
17
|
+
permissions;
|
|
18
|
+
constructor({ permissions = [] } = {}) {
|
|
19
|
+
this.permissions = permissions ?? [];
|
|
26
20
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
21
|
+
codeInterpreterJavascript(code) {
|
|
22
|
+
const context = { console };
|
|
23
|
+
if (this.permissions.includes(AxJSInterpreterPermission.FS)) {
|
|
24
|
+
context.fs = _fs;
|
|
25
|
+
}
|
|
26
|
+
if (this.permissions.includes(AxJSInterpreterPermission.NET)) {
|
|
27
|
+
context.http = _http;
|
|
28
|
+
context.https = _https;
|
|
29
|
+
}
|
|
30
|
+
if (this.permissions.includes(AxJSInterpreterPermission.OS)) {
|
|
31
|
+
context.os = _os;
|
|
32
|
+
}
|
|
33
|
+
if (this.permissions.includes(AxJSInterpreterPermission.CRYPTO)) {
|
|
34
|
+
context.crypto = _crypto;
|
|
35
|
+
}
|
|
36
|
+
if (this.permissions.includes(AxJSInterpreterPermission.PROCESS)) {
|
|
37
|
+
context.process = _process;
|
|
38
|
+
}
|
|
39
|
+
return runInNewContext(`(function() { ${code} })()`, context);
|
|
46
40
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
41
|
+
toFunction() {
|
|
42
|
+
return {
|
|
43
|
+
name: 'javascriptInterpreter',
|
|
44
|
+
description: 'Use this function to run Javascript code and get any expected return value.',
|
|
45
|
+
parameters: {
|
|
46
|
+
type: 'object',
|
|
47
|
+
properties: {
|
|
48
|
+
code: {
|
|
49
|
+
type: 'string',
|
|
50
|
+
description: 'JS code with a return value in the end.'
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
required: ['code']
|
|
54
|
+
},
|
|
55
|
+
func: ({ code }) => this.codeInterpreterJavascript(code)
|
|
56
|
+
};
|
|
50
57
|
}
|
|
51
|
-
|
|
52
|
-
context.os = _os;
|
|
53
|
-
}
|
|
54
|
-
if (permissions.includes(AxCodeInterpreterPermission.CRYPTO)) {
|
|
55
|
-
context.crypto = _crypto;
|
|
56
|
-
}
|
|
57
|
-
if (permissions.includes(AxCodeInterpreterPermission.PROCESS)) {
|
|
58
|
-
context.process = _process;
|
|
59
|
-
}
|
|
60
|
-
// executing code within the sandbox
|
|
61
|
-
return runInNewContext(`(function() { ${code} })()`, context);
|
|
62
|
-
};
|
|
58
|
+
}
|
|
63
59
|
//# sourceMappingURL=code.js.map
|
package/funcs/code.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code.js","sourceRoot":"","sources":["../../funcs/code.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,QAAQ,CAAC;AAClC,OAAO,KAAK,KAAK,MAAM,MAAM,CAAC;AAC9B,OAAO,KAAK,MAAM,MAAM,OAAO,CAAC;AAChC,OAAO,KAAK,GAAG,MAAM,SAAS,CAAC;AAC/B,OAAO,KAAK,GAAG,MAAM,IAAI,CAAC;AAC1B,OAAO,KAAK,QAAQ,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,eAAe,EAAE,MAAM,IAAI,CAAC;AAIrC,MAAM,
|
|
1
|
+
{"version":3,"file":"code.js","sourceRoot":"","sources":["../../funcs/code.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,QAAQ,CAAC;AAClC,OAAO,KAAK,KAAK,MAAM,MAAM,CAAC;AAC9B,OAAO,KAAK,MAAM,MAAM,OAAO,CAAC;AAChC,OAAO,KAAK,GAAG,MAAM,SAAS,CAAC;AAC/B,OAAO,KAAK,GAAG,MAAM,IAAI,CAAC;AAC1B,OAAO,KAAK,QAAQ,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,eAAe,EAAE,MAAM,IAAI,CAAC;AAIrC,MAAM,CAAN,IAAY,yBAMX;AAND,WAAY,yBAAyB;IACnC,2CAAc,CAAA;IACd,wCAAW,CAAA;IACX,sCAAS,CAAA;IACT,8CAAiB,CAAA;IACjB,gDAAmB,CAAA;AACrB,CAAC,EANW,yBAAyB,KAAzB,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EAAE;wBACJ,IAAI,EAAE,QAAQ;wBACd,WAAW,EAAE,yCAAyC;qBACvD;iBACF;gBACD,QAAQ,EAAE,CAAC,MAAM,CAAC;aACnB;YAED,IAAI,EAAE,CAAC,EAAE,IAAI,EAA8B,EAAE,EAAE,CAC7C,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC;SACvC,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { AxFunction } from '../ai/types.js';
|
|
2
|
+
export interface AxDockerContainer {
|
|
3
|
+
Id: string;
|
|
4
|
+
Names: string[];
|
|
5
|
+
Image: string;
|
|
6
|
+
ImageID: string;
|
|
7
|
+
Command: string;
|
|
8
|
+
Created: number;
|
|
9
|
+
State: {
|
|
10
|
+
Status: string;
|
|
11
|
+
Running: boolean;
|
|
12
|
+
Paused: boolean;
|
|
13
|
+
Restarting: boolean;
|
|
14
|
+
OOMKilled: boolean;
|
|
15
|
+
Dead: boolean;
|
|
16
|
+
Pid: number;
|
|
17
|
+
ExitCode: number;
|
|
18
|
+
Error: string;
|
|
19
|
+
StartedAt: Date;
|
|
20
|
+
FinishedAt: Date;
|
|
21
|
+
};
|
|
22
|
+
Status: string;
|
|
23
|
+
Ports: Array<{
|
|
24
|
+
IP: string;
|
|
25
|
+
PrivatePort: number;
|
|
26
|
+
PublicPort: number;
|
|
27
|
+
Type: string;
|
|
28
|
+
}>;
|
|
29
|
+
Labels: {
|
|
30
|
+
[key: string]: string;
|
|
31
|
+
};
|
|
32
|
+
SizeRw: number;
|
|
33
|
+
SizeRootFs: number;
|
|
34
|
+
HostConfig: {
|
|
35
|
+
NetworkMode: string;
|
|
36
|
+
};
|
|
37
|
+
NetworkSettings: {
|
|
38
|
+
Networks: {
|
|
39
|
+
[key: string]: {
|
|
40
|
+
IPAddress: string;
|
|
41
|
+
IPPrefixLen: number;
|
|
42
|
+
Gateway: string;
|
|
43
|
+
MacAddress: string;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
Mounts: Array<{
|
|
48
|
+
Type: string;
|
|
49
|
+
Source: string;
|
|
50
|
+
Destination: string;
|
|
51
|
+
Mode: string;
|
|
52
|
+
RW: boolean;
|
|
53
|
+
Propagation: string;
|
|
54
|
+
}>;
|
|
55
|
+
}
|
|
56
|
+
export declare class AxDockerSession {
|
|
57
|
+
private readonly apiUrl;
|
|
58
|
+
private containerId;
|
|
59
|
+
constructor(apiUrl?: string);
|
|
60
|
+
pullImage(imageName: string): Promise<void>;
|
|
61
|
+
createContainer({ imageName, volumes, doNotPullImage, tag }: Readonly<{
|
|
62
|
+
imageName: string;
|
|
63
|
+
volumes?: Array<{
|
|
64
|
+
hostPath: string;
|
|
65
|
+
containerPath: string;
|
|
66
|
+
}>;
|
|
67
|
+
doNotPullImage?: boolean;
|
|
68
|
+
tag?: string;
|
|
69
|
+
}>): Promise<{
|
|
70
|
+
Id: string;
|
|
71
|
+
}>;
|
|
72
|
+
findOrCreateContainer({ imageName, volumes, doNotPullImage, tag }: Readonly<{
|
|
73
|
+
imageName: string;
|
|
74
|
+
volumes?: Array<{
|
|
75
|
+
hostPath: string;
|
|
76
|
+
containerPath: string;
|
|
77
|
+
}>;
|
|
78
|
+
doNotPullImage?: boolean;
|
|
79
|
+
tag: string;
|
|
80
|
+
}>): Promise<{
|
|
81
|
+
Id: string;
|
|
82
|
+
isNew: boolean;
|
|
83
|
+
}>;
|
|
84
|
+
startContainer(): Promise<void>;
|
|
85
|
+
connectToContainer(containerId: string): Promise<void>;
|
|
86
|
+
stopContainers({ tag, remove, timeout }: Readonly<{
|
|
87
|
+
tag?: string;
|
|
88
|
+
remove?: boolean;
|
|
89
|
+
timeout?: number;
|
|
90
|
+
}>): Promise<Array<{
|
|
91
|
+
Id: string;
|
|
92
|
+
Action: 'stopped' | 'removed';
|
|
93
|
+
}>>;
|
|
94
|
+
listContainers(all?: boolean): Promise<AxDockerContainer[]>;
|
|
95
|
+
getContainerLogs(): Promise<string>;
|
|
96
|
+
executeCommand(command: string): Promise<string>;
|
|
97
|
+
private getContainerInfo;
|
|
98
|
+
private waitForContainerToBeRunning;
|
|
99
|
+
private fetchDockerAPI;
|
|
100
|
+
toFunction(): AxFunction;
|
|
101
|
+
}
|
package/funcs/docker.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
export class AxDockerSession {
|
|
2
|
+
apiUrl;
|
|
3
|
+
containerId = null;
|
|
4
|
+
constructor(apiUrl = 'http://localhost:2375') {
|
|
5
|
+
this.apiUrl = apiUrl;
|
|
6
|
+
}
|
|
7
|
+
async pullImage(imageName) {
|
|
8
|
+
const response = await this.fetchDockerAPI(`/images/create?fromImage=${encodeURIComponent(imageName)}`, {
|
|
9
|
+
method: 'POST'
|
|
10
|
+
});
|
|
11
|
+
if (!response.ok) {
|
|
12
|
+
throw new Error(`Failed to pull image: ${response.statusText}`);
|
|
13
|
+
}
|
|
14
|
+
// Wait for the pull to complete
|
|
15
|
+
await response.text();
|
|
16
|
+
}
|
|
17
|
+
async createContainer({ imageName, volumes = [], doNotPullImage, tag }) {
|
|
18
|
+
const binds = volumes.map((v) => `${v.hostPath}:${v.containerPath}`);
|
|
19
|
+
if (!doNotPullImage) {
|
|
20
|
+
await this.pullImage(imageName);
|
|
21
|
+
}
|
|
22
|
+
const containerConfig = {
|
|
23
|
+
Image: imageName,
|
|
24
|
+
Tty: true,
|
|
25
|
+
OpenStdin: false,
|
|
26
|
+
AttachStdin: false,
|
|
27
|
+
AttachStdout: false,
|
|
28
|
+
AttachStderr: false,
|
|
29
|
+
HostConfig: { Binds: binds },
|
|
30
|
+
Labels: {}
|
|
31
|
+
};
|
|
32
|
+
if (tag) {
|
|
33
|
+
containerConfig.Labels['com.example.tag'] = tag;
|
|
34
|
+
}
|
|
35
|
+
const response = await this.fetchDockerAPI(`/containers/create`, {
|
|
36
|
+
method: 'POST',
|
|
37
|
+
headers: { 'Content-Type': 'application/json' },
|
|
38
|
+
body: JSON.stringify(containerConfig)
|
|
39
|
+
});
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
throw new Error(`Failed to create container: ${response.statusText}`);
|
|
42
|
+
}
|
|
43
|
+
const data = (await response.json());
|
|
44
|
+
this.containerId = data.Id;
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
async findOrCreateContainer({ imageName, volumes = [], doNotPullImage, tag }) {
|
|
48
|
+
// First, try to find existing containers with the given tag
|
|
49
|
+
const existingContainers = await this.listContainers(true);
|
|
50
|
+
const matchingContainers = existingContainers.filter((container) => container.Labels && container.Labels['com.example.tag'] === tag);
|
|
51
|
+
if (matchingContainers && matchingContainers.length > 0) {
|
|
52
|
+
// Randomly select a container from the matching ones
|
|
53
|
+
const randomIndex = Math.floor(Math.random() * matchingContainers.length);
|
|
54
|
+
const selectedContainer = matchingContainers[randomIndex];
|
|
55
|
+
if (selectedContainer) {
|
|
56
|
+
// Connect to the selected container
|
|
57
|
+
await this.connectToContainer(selectedContainer.Id);
|
|
58
|
+
return { Id: selectedContainer.Id, isNew: false };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// If no container with the tag exists, create a new one
|
|
62
|
+
const newContainer = await this.createContainer({
|
|
63
|
+
imageName,
|
|
64
|
+
volumes,
|
|
65
|
+
doNotPullImage,
|
|
66
|
+
tag
|
|
67
|
+
});
|
|
68
|
+
return { Id: newContainer.Id, isNew: true };
|
|
69
|
+
}
|
|
70
|
+
async startContainer() {
|
|
71
|
+
if (!this.containerId) {
|
|
72
|
+
throw new Error('No container created or connected');
|
|
73
|
+
}
|
|
74
|
+
const response = await this.fetchDockerAPI(`/containers/${this.containerId}/start`, {
|
|
75
|
+
method: 'POST'
|
|
76
|
+
});
|
|
77
|
+
if (!response.ok) {
|
|
78
|
+
throw new Error(`Failed to start container: ${response.statusText}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async connectToContainer(containerId) {
|
|
82
|
+
const response = await this.fetchDockerAPI(`/containers/${containerId}/json`);
|
|
83
|
+
if (!response.ok) {
|
|
84
|
+
throw new Error(`Failed to connect to container: ${response.statusText}`);
|
|
85
|
+
}
|
|
86
|
+
this.containerId = containerId;
|
|
87
|
+
}
|
|
88
|
+
async stopContainers({ tag, remove, timeout = 10 }) {
|
|
89
|
+
const results = [];
|
|
90
|
+
// List all containers
|
|
91
|
+
const containers = await this.listContainers(true);
|
|
92
|
+
// Filter containers by tag if provided
|
|
93
|
+
const targetContainers = tag
|
|
94
|
+
? containers.filter((container) => container.Labels['com.example.tag'] === tag)
|
|
95
|
+
: containers;
|
|
96
|
+
for (const container of targetContainers) {
|
|
97
|
+
// Stop the container if it's running
|
|
98
|
+
if (container.State.Status === 'running') {
|
|
99
|
+
const stopResponse = await this.fetchDockerAPI(`/containers/${container.Id}/stop?t=${timeout}`, { method: 'POST' });
|
|
100
|
+
if (!stopResponse.ok) {
|
|
101
|
+
console.warn(`Failed to stop container ${container.Id}: ${stopResponse.statusText}`);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
results.push({ Id: container.Id, Action: 'stopped' });
|
|
105
|
+
}
|
|
106
|
+
// Remove the container if the remove flag is set
|
|
107
|
+
if (remove) {
|
|
108
|
+
const removeResponse = await this.fetchDockerAPI(`/containers/${container.Id}`, { method: 'DELETE' });
|
|
109
|
+
if (!removeResponse.ok) {
|
|
110
|
+
console.warn(`Failed to remove container ${container.Id}: ${removeResponse.statusText}`);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
results.push({ Id: container.Id, Action: 'removed' });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return results;
|
|
117
|
+
}
|
|
118
|
+
async listContainers(all = false) {
|
|
119
|
+
const response = await this.fetchDockerAPI(`/containers/json?all=${all}`, {
|
|
120
|
+
method: 'GET'
|
|
121
|
+
});
|
|
122
|
+
return response.json();
|
|
123
|
+
}
|
|
124
|
+
async getContainerLogs() {
|
|
125
|
+
if (!this.containerId) {
|
|
126
|
+
throw new Error('No container created or connected');
|
|
127
|
+
}
|
|
128
|
+
const response = await this.fetchDockerAPI(`/containers/${this.containerId}/logs?stdout=true&stderr=true`, { method: 'GET' });
|
|
129
|
+
return response.text();
|
|
130
|
+
}
|
|
131
|
+
async executeCommand(command) {
|
|
132
|
+
console.log('Executing command:', command);
|
|
133
|
+
if (!this.containerId) {
|
|
134
|
+
throw new Error('No container created or connected');
|
|
135
|
+
}
|
|
136
|
+
// Check container state
|
|
137
|
+
const containerInfo = await this.getContainerInfo(this.containerId);
|
|
138
|
+
if (containerInfo.State.Status !== 'running') {
|
|
139
|
+
await this.startContainer();
|
|
140
|
+
// Wait for the container to be in the "running" state
|
|
141
|
+
await this.waitForContainerToBeRunning(this.containerId);
|
|
142
|
+
}
|
|
143
|
+
// Create exec instance
|
|
144
|
+
const createResponse = await this.fetchDockerAPI(`/containers/${this.containerId}/exec`, {
|
|
145
|
+
method: 'POST',
|
|
146
|
+
headers: { 'Content-Type': 'application/json' },
|
|
147
|
+
body: JSON.stringify({
|
|
148
|
+
Cmd: ['sh', '-c', command],
|
|
149
|
+
AttachStdout: true,
|
|
150
|
+
AttachStderr: true
|
|
151
|
+
})
|
|
152
|
+
});
|
|
153
|
+
if (!createResponse.ok) {
|
|
154
|
+
throw new Error(`Failed to create exec instance: ${createResponse.statusText}`);
|
|
155
|
+
}
|
|
156
|
+
const execData = (await createResponse.json());
|
|
157
|
+
// Start exec instance
|
|
158
|
+
const startResponse = await this.fetchDockerAPI(`/exec/${execData.Id}/start`, {
|
|
159
|
+
method: 'POST',
|
|
160
|
+
headers: { 'Content-Type': 'application/json' },
|
|
161
|
+
body: JSON.stringify({
|
|
162
|
+
Detach: false,
|
|
163
|
+
Tty: false
|
|
164
|
+
})
|
|
165
|
+
});
|
|
166
|
+
if (!startResponse.ok) {
|
|
167
|
+
throw new Error(`Failed to start exec instance: ${startResponse.statusText}`);
|
|
168
|
+
}
|
|
169
|
+
// Return the output
|
|
170
|
+
return await startResponse.text();
|
|
171
|
+
}
|
|
172
|
+
// Add these new methods to the class:
|
|
173
|
+
async getContainerInfo(containerId) {
|
|
174
|
+
const response = await this.fetchDockerAPI(`/containers/${containerId}/json`);
|
|
175
|
+
if (!response.ok) {
|
|
176
|
+
throw new Error(`Failed to get container info: ${response.statusText}`);
|
|
177
|
+
}
|
|
178
|
+
return response.json();
|
|
179
|
+
}
|
|
180
|
+
async waitForContainerToBeRunning(containerId, timeout = 30000) {
|
|
181
|
+
const startTime = Date.now();
|
|
182
|
+
while (Date.now() - startTime < timeout) {
|
|
183
|
+
const containerInfo = await this.getContainerInfo(containerId);
|
|
184
|
+
if (containerInfo.State.Status === 'running') {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second before checking again
|
|
188
|
+
}
|
|
189
|
+
throw new Error('Timeout waiting for container to start');
|
|
190
|
+
}
|
|
191
|
+
async fetchDockerAPI(endpoint, options) {
|
|
192
|
+
const url = new URL(endpoint, this.apiUrl).toString();
|
|
193
|
+
return await fetch(url, options);
|
|
194
|
+
}
|
|
195
|
+
toFunction() {
|
|
196
|
+
return {
|
|
197
|
+
name: 'commandExecution',
|
|
198
|
+
description: 'Use this function to execute shell commands, scripts, and programs. This function enables interaction with the file system, running system utilities, and performing tasks that require a shell interface.',
|
|
199
|
+
parameters: {
|
|
200
|
+
type: 'object',
|
|
201
|
+
properties: {
|
|
202
|
+
command: {
|
|
203
|
+
type: 'string',
|
|
204
|
+
description: 'Shell command to execute. eg. `ls -l` or `echo "Hello, World!"`.'
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
required: ['command']
|
|
208
|
+
},
|
|
209
|
+
func: async ({ command }) => await this.executeCommand(command)
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=docker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"docker.js","sourceRoot":"","sources":["../../funcs/docker.ts"],"names":[],"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EAAiC,EAAE,EAAE,CACzD,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC;SACrC,CAAC;IACJ,CAAC;CACF"}
|
package/funcs/embed.d.ts
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import type { AxAIService, AxAIServiceActionOptions, AxFunction } from '../ai/index.js';
|
|
2
|
-
export declare
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
export declare class AxEmbeddingAdapter {
|
|
3
|
+
private aiService;
|
|
4
|
+
private info;
|
|
5
|
+
private func;
|
|
6
|
+
constructor({ ai, info, func }: Readonly<{
|
|
7
|
+
ai: AxAIService;
|
|
8
|
+
info: Readonly<{
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
argumentDescription: string;
|
|
12
|
+
}>;
|
|
13
|
+
func: (args: readonly number[], extra?: Readonly<AxAIServiceActionOptions>) => Promise<unknown>;
|
|
14
|
+
}>);
|
|
15
|
+
private embedAdapter;
|
|
16
|
+
toFunction(): AxFunction;
|
|
17
|
+
}
|
package/funcs/embed.js
CHANGED
|
@@ -1,29 +1,38 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
description: info.argumentDescription
|
|
10
|
-
}
|
|
11
|
-
},
|
|
12
|
-
required: ['text']
|
|
13
|
-
},
|
|
14
|
-
func: ({ text }, extra
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
-
) => {
|
|
17
|
-
return new Promise((resolve) => {
|
|
18
|
-
resolve(embedAdapter(ai, text, func, extra));
|
|
19
|
-
});
|
|
1
|
+
export class AxEmbeddingAdapter {
|
|
2
|
+
aiService;
|
|
3
|
+
info;
|
|
4
|
+
func;
|
|
5
|
+
constructor({ ai, info, func }) {
|
|
6
|
+
this.aiService = ai;
|
|
7
|
+
this.info = info;
|
|
8
|
+
this.func = func;
|
|
20
9
|
}
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
10
|
+
async embedAdapter(text, extra) {
|
|
11
|
+
const embedRes = await this.aiService.embed({ texts: [text] }, { sessionId: extra?.sessionId });
|
|
12
|
+
const embeds = embedRes.embeddings.at(0);
|
|
13
|
+
if (!embeds) {
|
|
14
|
+
throw new Error('Failed to embed text');
|
|
15
|
+
}
|
|
16
|
+
return this.func.length === 2
|
|
17
|
+
? this.func(embeds, extra)
|
|
18
|
+
: this.func(embeds);
|
|
19
|
+
}
|
|
20
|
+
toFunction() {
|
|
21
|
+
return {
|
|
22
|
+
name: this.info.name,
|
|
23
|
+
description: this.info.description,
|
|
24
|
+
parameters: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
properties: {
|
|
27
|
+
text: {
|
|
28
|
+
type: 'string',
|
|
29
|
+
description: this.info.argumentDescription
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
required: ['text']
|
|
33
|
+
},
|
|
34
|
+
func: ({ text }, options) => this.embedAdapter(text, options)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
29
38
|
//# sourceMappingURL=embed.js.map
|
package/funcs/embed.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"embed.js","sourceRoot":"","sources":["../../funcs/embed.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"embed.js","sourceRoot":"","sources":["../../funcs/embed.ts"],"names":[],"mappings":"AAMA,MAAM,OAAO,kBAAkB;IACrB,SAAS,CAAc;IACvB,IAAI,CAIV;IACM,IAAI,CAGU;IAEtB,YAAY,EACV,EAAE,EACF,IAAI,EACJ,IAAI,EAYJ;QACA,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,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oot":"","sources":["../../funcs/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../funcs/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
|
package/package.json
CHANGED
package/prompts/react.js
CHANGED
|
@@ -5,8 +5,13 @@ export class AxReAct extends AxChainOfThought {
|
|
|
5
5
|
if (!options?.functions || options.functions.length === 0) {
|
|
6
6
|
throw new Error('No functions provided');
|
|
7
7
|
}
|
|
8
|
-
const
|
|
9
|
-
|
|
8
|
+
const fnNames = options.functions.map((f) => {
|
|
9
|
+
if ('toFunction' in f) {
|
|
10
|
+
return f.toFunction().name;
|
|
11
|
+
}
|
|
12
|
+
return f.name;
|
|
13
|
+
});
|
|
14
|
+
const funcList = fnNames.map((fname) => `'${fname}'`).join(', ');
|
|
10
15
|
const sig = new AxSignature(signature);
|
|
11
16
|
sig.setDescription(`Use the provided functions ${funcList} to complete the task and return the result if any.`);
|
|
12
17
|
// sig.addInputField({
|
package/prompts/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.js","sourceRoot":"","sources":["../../prompts/react.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C,MAAM,OAAO,OAGX,SAAQ,gBAAyB;IACjC,YACE,EAAe,EACf,SAAyC,EACzC,OAAoC;QAEpC,IAAI,CAAC,OAAO,EAAE,SAAS,IAAI,OAAO,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1D,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;QAC3C,CAAC;QAED,MAAM,
|
|
1
|
+
{"version":3,"file":"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