@caretakerai/agent 0.0.5 → 0.0.7
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/action.d.ts +23 -0
- package/dist/action.js +85 -0
- package/dist/action.js.map +1 -0
- package/dist/activity.d.ts +20 -0
- package/dist/activity.js +44 -0
- package/dist/activity.js.map +1 -0
- package/dist/agent.d.ts +47 -0
- package/dist/agent.js +193 -0
- package/dist/agent.js.map +1 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +6 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +2 -2
package/dist/action.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Agent } from './agent';
|
|
2
|
+
import { type JSONSchema } from 'json-schema-to-typescript';
|
|
3
|
+
import { Activity } from './activity';
|
|
4
|
+
export interface ActionInput<T> {
|
|
5
|
+
params: T;
|
|
6
|
+
agent: Agent;
|
|
7
|
+
}
|
|
8
|
+
export interface ActionExample {
|
|
9
|
+
description?: string;
|
|
10
|
+
activities: Activity[];
|
|
11
|
+
}
|
|
12
|
+
export declare abstract class Action<P = any, R = any> {
|
|
13
|
+
abstract get exit(): boolean;
|
|
14
|
+
abstract get kind(): string;
|
|
15
|
+
abstract get description(): string;
|
|
16
|
+
abstract get params(): JSONSchema;
|
|
17
|
+
abstract get result(): JSONSchema;
|
|
18
|
+
abstract get examples(): ActionExample[];
|
|
19
|
+
abstract call(input: ActionInput<P>): Promise<R>;
|
|
20
|
+
private _examplesPrompt;
|
|
21
|
+
_prompt(template?: string): Promise<string>;
|
|
22
|
+
_call(input: string, agent: Agent): Promise<string>;
|
|
23
|
+
}
|
package/dist/action.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Action = void 0;
|
|
7
|
+
const dedent_1 = __importDefault(require("dedent"));
|
|
8
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
9
|
+
const ajv_errors_1 = __importDefault(require("ajv-errors"));
|
|
10
|
+
const prompts_1 = require("langchain/prompts");
|
|
11
|
+
const json_schema_to_typescript_1 = require("json-schema-to-typescript");
|
|
12
|
+
const constants_1 = require("./constants");
|
|
13
|
+
const ajv = (0, ajv_errors_1.default)(new ajv_1.default({
|
|
14
|
+
useDefaults: true,
|
|
15
|
+
removeAdditional: true,
|
|
16
|
+
allErrors: true,
|
|
17
|
+
}));
|
|
18
|
+
const ACTION_TEMPLATE = (`
|
|
19
|
+
\`\`\`ts
|
|
20
|
+
/**
|
|
21
|
+
* @kind {kind}
|
|
22
|
+
{description}
|
|
23
|
+
* /
|
|
24
|
+
|
|
25
|
+
{params}
|
|
26
|
+
|
|
27
|
+
{result}
|
|
28
|
+
{examples}\`\`\`
|
|
29
|
+
`).trim();
|
|
30
|
+
class Action {
|
|
31
|
+
_examplesPrompt() {
|
|
32
|
+
return this.examples
|
|
33
|
+
.map(({ activities, description }) => [
|
|
34
|
+
(0, dedent_1.default) `
|
|
35
|
+
/**
|
|
36
|
+
* @example ${description}`,
|
|
37
|
+
activities.map(a => a.prompt())
|
|
38
|
+
.join(constants_1.ACTIVITY_SEP)
|
|
39
|
+
.split('\n')
|
|
40
|
+
.map(s => ` * ${s}`)
|
|
41
|
+
.join('\n'),
|
|
42
|
+
' */'
|
|
43
|
+
].join('\n'))
|
|
44
|
+
.join('\n\n') + '\n';
|
|
45
|
+
}
|
|
46
|
+
async _prompt(template = ACTION_TEMPLATE) {
|
|
47
|
+
const paramsType = `${this.kind}Params`;
|
|
48
|
+
const resultType = `${this.kind}Result`;
|
|
49
|
+
const partial = await prompts_1.PromptTemplate.fromTemplate(template).partial({
|
|
50
|
+
params: async () => {
|
|
51
|
+
const ts = await (0, json_schema_to_typescript_1.compile)(this.params, paramsType, { bannerComment: '', additionalProperties: false });
|
|
52
|
+
return ts.replace(/^(export\s*)/gm, '').trim();
|
|
53
|
+
},
|
|
54
|
+
result: async () => {
|
|
55
|
+
const ts = await (0, json_schema_to_typescript_1.compile)(this.result, resultType, { bannerComment: '', additionalProperties: false });
|
|
56
|
+
return ts.replace(/^(export\s*)/gm, '').trim();
|
|
57
|
+
},
|
|
58
|
+
examples: () => this._examplesPrompt()
|
|
59
|
+
});
|
|
60
|
+
return partial.format({
|
|
61
|
+
kind: this.kind,
|
|
62
|
+
description: this.description
|
|
63
|
+
.split('\n')
|
|
64
|
+
.map(s => ` * ${s}`)
|
|
65
|
+
.join('\n'),
|
|
66
|
+
paramsType,
|
|
67
|
+
resultType,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async _call(input, agent) {
|
|
71
|
+
const params = JSON.parse(input);
|
|
72
|
+
const validator = ajv.compile(this.params);
|
|
73
|
+
const isValid = validator(params);
|
|
74
|
+
if (!isValid) {
|
|
75
|
+
throw new Error(`Action "${this.kind}" params are not valid: ${ajv.errorsText(validator.errors)} `);
|
|
76
|
+
}
|
|
77
|
+
const result = await this.call({ params, agent });
|
|
78
|
+
if (typeof result === 'string') {
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
return JSON.stringify(result);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.Action = Action;
|
|
85
|
+
//# sourceMappingURL=action.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"action.js","sourceRoot":"","sources":["../src/action.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAC5B,8CAAsB;AACtB,4DAAyC;AACzC,+CAAmD;AAEnD,yEAAqE;AAErE,2CAA2C;AAE3C,MAAM,GAAG,GAAG,I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ElD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;YAC9B,OAAO,MAAM,CAAC;SACf;QAED,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;CACF;AAvED,wBAuEC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare enum ActivityKind {
|
|
2
|
+
Observation = "Observation",
|
|
3
|
+
Thought = "Thought",
|
|
4
|
+
Action = "Action"
|
|
5
|
+
}
|
|
6
|
+
export type ActivityParams = {
|
|
7
|
+
kind: ActivityKind;
|
|
8
|
+
input: string;
|
|
9
|
+
attributes?: Record<string, string>;
|
|
10
|
+
};
|
|
11
|
+
export declare class Activity implements ActivityParams {
|
|
12
|
+
kind: ActivityKind;
|
|
13
|
+
attributes?: Record<string, string>;
|
|
14
|
+
input: string;
|
|
15
|
+
constructor(params: ActivityParams);
|
|
16
|
+
prompt(): string;
|
|
17
|
+
toObject(): this;
|
|
18
|
+
static fromObject({ kind, attributes, input }: Record<string, any>): Activity;
|
|
19
|
+
static parse(text: string): Activity[];
|
|
20
|
+
}
|
package/dist/activity.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Activity = exports.ActivityKind = void 0;
|
|
4
|
+
const xml_js_1 = require("xml-js");
|
|
5
|
+
var ActivityKind;
|
|
6
|
+
(function (ActivityKind) {
|
|
7
|
+
ActivityKind["Observation"] = "Observation";
|
|
8
|
+
ActivityKind["Thought"] = "Thought";
|
|
9
|
+
ActivityKind["Action"] = "Action";
|
|
10
|
+
})(ActivityKind || (exports.ActivityKind = ActivityKind = {}));
|
|
11
|
+
class Activity {
|
|
12
|
+
kind;
|
|
13
|
+
attributes;
|
|
14
|
+
input;
|
|
15
|
+
constructor(params) {
|
|
16
|
+
Object.assign(this, params);
|
|
17
|
+
}
|
|
18
|
+
prompt() {
|
|
19
|
+
return (0, xml_js_1.js2xml)({ [this.kind]: { _attributes: this.attributes ?? {}, _text: `\n${this.input}\n`, } }, { compact: true })
|
|
20
|
+
.replaceAll('<', '<')
|
|
21
|
+
.replaceAll('>', '>');
|
|
22
|
+
}
|
|
23
|
+
toObject() {
|
|
24
|
+
return { ...this };
|
|
25
|
+
}
|
|
26
|
+
static fromObject({ kind, attributes, input }) {
|
|
27
|
+
return new Activity({ kind, attributes, input });
|
|
28
|
+
}
|
|
29
|
+
static parse(text) {
|
|
30
|
+
const { elements: [root] } = (0, xml_js_1.xml2js)(`<root>${text}</root>`, { trim: true });
|
|
31
|
+
return root.elements.map(({ name, attributes, elements }) => {
|
|
32
|
+
const input = (0, xml_js_1.js2xml)({ elements })
|
|
33
|
+
.replaceAll('<', '<')
|
|
34
|
+
.replaceAll('>', '>');
|
|
35
|
+
return Activity.fromObject({
|
|
36
|
+
kind: name,
|
|
37
|
+
input: input,
|
|
38
|
+
attributes: attributes,
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.Activity = Activity;
|
|
44
|
+
//# sourceMappingURL=activity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"activity.js","sourceRoot":"","sources":["../src/activity.ts"],"names":[],"mappings":";;;AAAA,mCAAiD;AAKjD,IAAY,YAOX;AAPD,WAAY,YAAY;IAEtB,2CAA2B,CAAA;IAE3B,mCAAmB,CAAA;IAEnB,iCAAiB,CAAA;AACnB,CAAC,EAPW,YAAY,4BAAZ,YAAY,QAOvB;AAcD,MAAa,QAAQ;IACnB,IAAI,CAAgB;IACpB,UAAU,CAA0B;IACpC,KAAK,CAAU;IAEf,YAAY,MAAsB;QAChC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC9B,CAAC;IAED,MAAM;QACJ,OAAO,IAAA,eAAM,EACX,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,WAAW,EAAE,IAAI,C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
import { Action } from './action';
|
|
4
|
+
import { Activity } from './activity';
|
|
5
|
+
import { Optimizer } from './types';
|
|
6
|
+
import { BaseLLM } from 'langchain/dist/llms/base';
|
|
7
|
+
interface AgentPrams {
|
|
8
|
+
name: string;
|
|
9
|
+
description: string;
|
|
10
|
+
llm: BaseLLM;
|
|
11
|
+
actions: Action[];
|
|
12
|
+
history?: Activity[];
|
|
13
|
+
examples?: Activity[];
|
|
14
|
+
constrains?: string[];
|
|
15
|
+
objective?: string;
|
|
16
|
+
suffix?: string;
|
|
17
|
+
maxIterations?: number;
|
|
18
|
+
maxRetries?: number;
|
|
19
|
+
optimizer: Optimizer;
|
|
20
|
+
template?: BasePromptTemplate;
|
|
21
|
+
stop?: string[];
|
|
22
|
+
logger?: Logger;
|
|
23
|
+
}
|
|
24
|
+
export declare class Agent implements AgentPrams {
|
|
25
|
+
name: string;
|
|
26
|
+
description: string;
|
|
27
|
+
llm: BaseLLM;
|
|
28
|
+
actions: Action[];
|
|
29
|
+
history: Activity[];
|
|
30
|
+
examples: Activity[];
|
|
31
|
+
constrains: string[];
|
|
32
|
+
objective: string;
|
|
33
|
+
suffix: string;
|
|
34
|
+
actionSuffix: string;
|
|
35
|
+
maxIterations: number;
|
|
36
|
+
maxRetries: number;
|
|
37
|
+
optimizer: Optimizer;
|
|
38
|
+
logger: Logger;
|
|
39
|
+
template?: BasePromptTemplate;
|
|
40
|
+
static defaults: Partial<AgentPrams>;
|
|
41
|
+
constructor(params: AgentPrams);
|
|
42
|
+
addActivities(...activities: Activity[]): void;
|
|
43
|
+
prompt(params?: Record<string, string>): Promise<Activity[]>;
|
|
44
|
+
execute({ attributes, input }: Activity): Promise<string>;
|
|
45
|
+
invoke(params?: Record<string, any>): Promise<string>;
|
|
46
|
+
}
|
|
47
|
+
export {};
|
package/dist/agent.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Agent = void 0;
|
|
7
|
+
const dedent_1 = __importDefault(require("dedent"));
|
|
8
|
+
const winston_1 = require("winston");
|
|
9
|
+
const prompts_1 = require("langchain/prompts");
|
|
10
|
+
const output_parser_1 = require("langchain/schema/output_parser");
|
|
11
|
+
const constants_1 = require("./constants");
|
|
12
|
+
const activity_1 = require("./activity");
|
|
13
|
+
class Agent {
|
|
14
|
+
name;
|
|
15
|
+
description;
|
|
16
|
+
llm;
|
|
17
|
+
actions;
|
|
18
|
+
history;
|
|
19
|
+
examples;
|
|
20
|
+
constrains;
|
|
21
|
+
objective;
|
|
22
|
+
suffix;
|
|
23
|
+
actionSuffix;
|
|
24
|
+
maxIterations;
|
|
25
|
+
maxRetries;
|
|
26
|
+
optimizer;
|
|
27
|
+
logger;
|
|
28
|
+
template;
|
|
29
|
+
static defaults = {
|
|
30
|
+
template: prompts_1.PromptTemplate.fromTemplate((0, dedent_1.default) `
|
|
31
|
+
# Objective
|
|
32
|
+
{objective}
|
|
33
|
+
|
|
34
|
+
# Constraints
|
|
35
|
+
{constraints}
|
|
36
|
+
|
|
37
|
+
# Actions
|
|
38
|
+
The only permissible actions you may take are listed below:
|
|
39
|
+
{actions}
|
|
40
|
+
|
|
41
|
+
**Continue the History with the following format in your response:**
|
|
42
|
+
{examples}
|
|
43
|
+
|
|
44
|
+
# History:
|
|
45
|
+
{history}
|
|
46
|
+
{suffix}
|
|
47
|
+
{completions}
|
|
48
|
+
`.trim()),
|
|
49
|
+
objective: 'You are helpful assistant.',
|
|
50
|
+
suffix: '<!-- Provide your thought and action here. Think your further actions step by step before taking any. Your must always explain your choice in your thoughts. -->',
|
|
51
|
+
maxRetries: 7,
|
|
52
|
+
maxIterations: Number.MAX_SAFE_INTEGER,
|
|
53
|
+
logger: (0, winston_1.createLogger)({ transports: [new winston_1.transports.Console()] }),
|
|
54
|
+
examples: [
|
|
55
|
+
new activity_1.Activity({
|
|
56
|
+
kind: activity_1.ActivityKind.Observation,
|
|
57
|
+
input: 'The result of previously taken action',
|
|
58
|
+
}),
|
|
59
|
+
new activity_1.Activity({
|
|
60
|
+
kind: activity_1.ActivityKind.Thought,
|
|
61
|
+
input: 'You must always think before taking the action',
|
|
62
|
+
}),
|
|
63
|
+
new activity_1.Activity({
|
|
64
|
+
kind: activity_1.ActivityKind.Action,
|
|
65
|
+
attributes: { kind: 'the action kind to take, should be one of listed in Actions section' },
|
|
66
|
+
input: (0, dedent_1.default) `
|
|
67
|
+
<!-- The action input as valid JSON e.g. -->
|
|
68
|
+
{
|
|
69
|
+
"message": "hello!!"
|
|
70
|
+
}
|
|
71
|
+
`.trim(),
|
|
72
|
+
}),
|
|
73
|
+
],
|
|
74
|
+
constrains: [
|
|
75
|
+
'Use only actions listed in Actions section.',
|
|
76
|
+
'Reject any request that are not related to your objective and cannot be fulfilled within the given list of actions.',
|
|
77
|
+
]
|
|
78
|
+
};
|
|
79
|
+
constructor(params) {
|
|
80
|
+
const { actions } = params;
|
|
81
|
+
if (!actions.length) {
|
|
82
|
+
throw new Error('Actions list must be non empty');
|
|
83
|
+
}
|
|
84
|
+
Object.assign(this, Agent.defaults, params);
|
|
85
|
+
}
|
|
86
|
+
addActivities(...activities) {
|
|
87
|
+
activities.forEach(a => this.logger.debug(a));
|
|
88
|
+
this.history.push(...activities);
|
|
89
|
+
}
|
|
90
|
+
async prompt(params) {
|
|
91
|
+
let activities = [];
|
|
92
|
+
const history = async () => {
|
|
93
|
+
const history = await this.optimizer.optimize(this.history);
|
|
94
|
+
const historyStrings = history.map(h => h.prompt()).join(constants_1.ACTIVITY_SEP);
|
|
95
|
+
return historyStrings;
|
|
96
|
+
};
|
|
97
|
+
const completions = async () => {
|
|
98
|
+
const activitiesStrings = activities.map(a => a.prompt()).join(constants_1.ACTIVITY_SEP);
|
|
99
|
+
return activitiesStrings;
|
|
100
|
+
};
|
|
101
|
+
const actions = async () => {
|
|
102
|
+
const actionsStrings = await Promise.all(this.actions.map(a => a._prompt()));
|
|
103
|
+
return actionsStrings.join(constants_1.ACTION_SEP);
|
|
104
|
+
};
|
|
105
|
+
const constraints = () => this.constrains.map((c, i) => `${i + 1}. ${c}`).join('\n');
|
|
106
|
+
const examples = async () => {
|
|
107
|
+
const examplesStrings = this.examples.map(h => h.prompt()).join(constants_1.ACTIVITY_SEP);
|
|
108
|
+
return examplesStrings;
|
|
109
|
+
};
|
|
110
|
+
const template = await this.template.partial({
|
|
111
|
+
objective: this.objective,
|
|
112
|
+
history,
|
|
113
|
+
actions,
|
|
114
|
+
constraints,
|
|
115
|
+
examples,
|
|
116
|
+
suffix: this.suffix,
|
|
117
|
+
completions
|
|
118
|
+
});
|
|
119
|
+
const chain = template.pipe((prompt) => prompt.toString().trim())
|
|
120
|
+
.pipe(this.llm.bind({ stop: [`<${activity_1.ActivityKind.Observation}`] }))
|
|
121
|
+
.pipe(new output_parser_1.StringOutputParser());
|
|
122
|
+
for (let i = 0; i < this.maxRetries; ++i) {
|
|
123
|
+
let completion = await chain.invoke(params ?? {});
|
|
124
|
+
debugger;
|
|
125
|
+
try {
|
|
126
|
+
activities = [...activities, ...activity_1.Activity.parse(completion)];
|
|
127
|
+
}
|
|
128
|
+
catch (e) {
|
|
129
|
+
this.logger.warn(e);
|
|
130
|
+
this.logger.debug(`Retry ${i + 1} due to malformed output`);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const activity = activities.at(-1);
|
|
134
|
+
if (activity.kind === activity_1.ActivityKind.Thought) {
|
|
135
|
+
this.logger.debug(`Retry ${i + 1} due to missing action`);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (activity.kind === activity_1.ActivityKind.Action) {
|
|
139
|
+
try {
|
|
140
|
+
const observation = await this.execute(activity);
|
|
141
|
+
return [
|
|
142
|
+
...activities,
|
|
143
|
+
new activity_1.Activity({
|
|
144
|
+
kind: activity_1.ActivityKind.Observation,
|
|
145
|
+
input: observation,
|
|
146
|
+
attributes: { of: activity.attributes.kind }
|
|
147
|
+
}),
|
|
148
|
+
];
|
|
149
|
+
}
|
|
150
|
+
catch (e) {
|
|
151
|
+
const err = e;
|
|
152
|
+
activities.push(new activity_1.Activity({
|
|
153
|
+
kind: activity_1.ActivityKind.Observation,
|
|
154
|
+
input: err.toString(),
|
|
155
|
+
attributes: { of: activity.attributes.kind }
|
|
156
|
+
}));
|
|
157
|
+
this.logger.debug(`Retry ${i + 1} due to action error: ${err}`);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
throw new Error('Max number of retries reached.');
|
|
163
|
+
}
|
|
164
|
+
async execute({ attributes, input }) {
|
|
165
|
+
const { kind } = attributes;
|
|
166
|
+
const action = this.actions.find(a => a.kind === kind);
|
|
167
|
+
if (!action) {
|
|
168
|
+
throw new Error(`Action "${kind}" is not allowed.`);
|
|
169
|
+
}
|
|
170
|
+
const observation = await action._call(input, this);
|
|
171
|
+
return observation;
|
|
172
|
+
}
|
|
173
|
+
async invoke(params) {
|
|
174
|
+
if (!this.history.length) {
|
|
175
|
+
throw new Error('Activity list must not be empty.');
|
|
176
|
+
}
|
|
177
|
+
if (this.history.at(-1)?.kind !== activity_1.ActivityKind.Observation) {
|
|
178
|
+
throw new Error('Latest experience must be of Observation kind');
|
|
179
|
+
}
|
|
180
|
+
for (let i = 0; i < this.maxIterations; ++i) {
|
|
181
|
+
const activities = await this.prompt(params);
|
|
182
|
+
this.addActivities(...activities);
|
|
183
|
+
const activity = this.history.at(-2);
|
|
184
|
+
const action = this.actions.find(a => a.kind === activity.attributes.kind);
|
|
185
|
+
if (action.exit) {
|
|
186
|
+
return this.history.at(-1).input;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
throw new Error('Max number of iterations reached.');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
exports.Agent = Agent;
|
|
193
|
+
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E,CAAC,GAAG,IAAI,CAAC,aAAa,EAAE,EAAE,CAAC,EAAE;YAC3C,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAC7C,IAAI,CAAC,aAAa,CAAC,GAAG,UAAU,CAAC,CAAC;YAClC,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAE,CAAC;YACtC,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;YAE3E,IAAI,MAAM,CAAC,IAAI,EAAE;gBACf,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;aAClC;SACF;QAED,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IACvD,CAAC;;AApNH,sBAqNC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,UAAU,GAAG,MAAM,CAAA;AACnB,QAAA,YAAY,GAAG,IAAI,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./action"), exports);
|
|
18
|
+
__exportStar(require("./agent"), exports);
|
|
19
|
+
__exportStar(require("./constants"), exports);
|
|
20
|
+
__exportStar(require("./activity"), exports);
|
|
21
|
+
__exportStar(require("./types"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,0CAAwB;AACxB,8CAA4B;AAC5B,6CAA2B;AAC3B,0CAAwB"}
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caretakerai/agent",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "Single framework for building text-agents",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
12
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
13
|
-
"build": "rimraf dist && tsc",
|
|
13
|
+
"build": "rimraf dist tsconfig.tsbuildinfo && tsc",
|
|
14
14
|
"build:watch": "tsc --watch"
|
|
15
15
|
},
|
|
16
16
|
"author": "",
|