openloops 0.1.33 → 0.1.35
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/clients/llm-client.d.ts +2 -0
- package/dist/clients/llm-client.js +15 -3
- package/dist/clients/llm-client.mjs +16 -4
- package/dist/clients/llm-error.d.ts +2 -1
- package/dist/clients/llm-error.js +10 -0
- package/dist/clients/llm-error.mjs +9 -0
- package/dist/common/base/base.entity.d.ts +2 -1
- package/dist/harness/agent.js +2 -0
- package/dist/harness/agent.mjs +3 -1
- package/dist/loops/agi/loop.js +2 -3
- package/dist/loops/agi/loop.mjs +2 -3
- package/dist/services/chats/entities/chat.entity.js +3 -1
- package/dist/services/chats/entities/chat.entity.mjs +3 -1
- package/dist/services/chats/lambdas/update-chat/update-chat.use-case.js +5 -0
- package/dist/services/chats/lambdas/update-chat/update-chat.use-case.mjs +5 -0
- package/dist/services/tasks/entities/agent-task.entity.d.ts +4 -4
- package/dist/services/tasks/entities/agent-task.entity.js +7 -7
- package/dist/services/tasks/entities/agent-task.entity.mjs +7 -7
- package/package.json +5 -2
|
@@ -2,12 +2,14 @@ import type { KnownProvider, Message, AssistantMessage } from '@earendil-works/p
|
|
|
2
2
|
import { CurrentUser } from '../common/base/index.js';
|
|
3
3
|
import { LLMChatParams, LLMResponse } from './llm-types';
|
|
4
4
|
export declare class LLMClient {
|
|
5
|
+
private static readonly modelsWithoutTemperature;
|
|
5
6
|
private readonly logger;
|
|
6
7
|
private readonly langfuse?;
|
|
7
8
|
private readonly langfuseEnabled;
|
|
8
9
|
constructor();
|
|
9
10
|
chat(params: LLMChatParams): Promise<LLMResponse>;
|
|
10
11
|
complete(params: LLMCompletionParams): Promise<AssistantMessage>;
|
|
12
|
+
private withoutRejectedTemperature;
|
|
11
13
|
private waitingOnRateLimit;
|
|
12
14
|
private traced;
|
|
13
15
|
private postResponse;
|
|
@@ -44,6 +44,7 @@ const DEFAULT_TEMPERATURE = 0.7;
|
|
|
44
44
|
const MAX_RETRIES = 4;
|
|
45
45
|
const MAX_RATE_LIMIT_WAITS = 2;
|
|
46
46
|
class LLMClient {
|
|
47
|
+
static modelsWithoutTemperature = /* @__PURE__ */ new Set();
|
|
47
48
|
logger = new import_logger.Logger();
|
|
48
49
|
langfuse;
|
|
49
50
|
langfuseEnabled;
|
|
@@ -78,11 +79,14 @@ class LLMClient {
|
|
|
78
79
|
const model = models.getModel(provider, modelName);
|
|
79
80
|
if (!model) throw new Error("[LLM Client] model not found");
|
|
80
81
|
const context = (0, import_pi_ai_adapter.toPiContext)({ messages, tools }, model);
|
|
81
|
-
const
|
|
82
|
+
const send = (omitTemperature) => {
|
|
83
|
+
const options = { ...omitTemperature ? {} : { temperature }, maxRetries: MAX_RETRIES, ...sessionId ? { sessionId } : {} };
|
|
84
|
+
return toolChoice ? models.completeSimple(model, context, { ...options, toolChoice }) : models.complete(model, context, options);
|
|
85
|
+
};
|
|
82
86
|
const raw = await this.traced(
|
|
83
87
|
{ origin, sessionId, answerId, currentUser, modelName, provider, temperature },
|
|
84
88
|
context,
|
|
85
|
-
() => this.
|
|
89
|
+
() => this.withoutRejectedTemperature(`${provider}/${modelName}`, send)
|
|
86
90
|
);
|
|
87
91
|
if (raw.stopReason === "error" || raw.stopReason === "aborted") {
|
|
88
92
|
throw import_llm_error.LLMError.fromProvider(raw.errorMessage ?? `request ${raw.stopReason}`);
|
|
@@ -115,9 +119,17 @@ class LLMClient {
|
|
|
115
119
|
return this.traced(
|
|
116
120
|
{ origin, sessionId, answerId, currentUser, modelName, provider, temperature },
|
|
117
121
|
context,
|
|
118
|
-
() => this.
|
|
122
|
+
() => this.withoutRejectedTemperature(`${provider}/${modelName}`, (omitTemperature) => models.complete(model, context, { ...omitTemperature ? {} : { temperature }, maxRetries: MAX_RETRIES }))
|
|
119
123
|
);
|
|
120
124
|
}
|
|
125
|
+
async withoutRejectedTemperature(key, send) {
|
|
126
|
+
const known = LLMClient.modelsWithoutTemperature.has(key);
|
|
127
|
+
const response = await this.waitingOnRateLimit(() => send(known));
|
|
128
|
+
if (known || response.stopReason !== "error" || !(0, import_llm_error.rejectsSamplingParameter)(response.errorMessage)) return response;
|
|
129
|
+
LLMClient.modelsWithoutTemperature.add(key);
|
|
130
|
+
this.logger.warn({ model: key, error: response.errorMessage }, "[LLM Service] The model rejected the temperature setting, retrying without it");
|
|
131
|
+
return this.waitingOnRateLimit(() => send(true));
|
|
132
|
+
}
|
|
121
133
|
async waitingOnRateLimit(run) {
|
|
122
134
|
let response = await run();
|
|
123
135
|
for (let waits = 0; waits < MAX_RATE_LIMIT_WAITS && response.stopReason === "error"; waits++) {
|
|
@@ -5,13 +5,14 @@ import { Logger } from "../common/logger/index.js";
|
|
|
5
5
|
import { extractJSON } from "../common/helpers/index.js";
|
|
6
6
|
import { createLLMCall } from "../services/llmcalls/index.js";
|
|
7
7
|
import { toPiContext, fromPiMessage } from "./pi-ai-adapter";
|
|
8
|
-
import { LLMError, retryDelayMs } from "./llm-error";
|
|
8
|
+
import { LLMError, retryDelayMs, rejectsSamplingParameter } from "./llm-error";
|
|
9
9
|
const DEFAULT_PROVIDER = "openai";
|
|
10
10
|
const DEFAULT_MODEL = "gpt-4o-mini";
|
|
11
11
|
const DEFAULT_TEMPERATURE = 0.7;
|
|
12
12
|
const MAX_RETRIES = 4;
|
|
13
13
|
const MAX_RATE_LIMIT_WAITS = 2;
|
|
14
14
|
class LLMClient {
|
|
15
|
+
static modelsWithoutTemperature = /* @__PURE__ */ new Set();
|
|
15
16
|
logger = new Logger();
|
|
16
17
|
langfuse;
|
|
17
18
|
langfuseEnabled;
|
|
@@ -46,11 +47,14 @@ class LLMClient {
|
|
|
46
47
|
const model = models.getModel(provider, modelName);
|
|
47
48
|
if (!model) throw new Error("[LLM Client] model not found");
|
|
48
49
|
const context = toPiContext({ messages, tools }, model);
|
|
49
|
-
const
|
|
50
|
+
const send = (omitTemperature) => {
|
|
51
|
+
const options = { ...omitTemperature ? {} : { temperature }, maxRetries: MAX_RETRIES, ...sessionId ? { sessionId } : {} };
|
|
52
|
+
return toolChoice ? models.completeSimple(model, context, { ...options, toolChoice }) : models.complete(model, context, options);
|
|
53
|
+
};
|
|
50
54
|
const raw = await this.traced(
|
|
51
55
|
{ origin, sessionId, answerId, currentUser, modelName, provider, temperature },
|
|
52
56
|
context,
|
|
53
|
-
() => this.
|
|
57
|
+
() => this.withoutRejectedTemperature(`${provider}/${modelName}`, send)
|
|
54
58
|
);
|
|
55
59
|
if (raw.stopReason === "error" || raw.stopReason === "aborted") {
|
|
56
60
|
throw LLMError.fromProvider(raw.errorMessage ?? `request ${raw.stopReason}`);
|
|
@@ -83,9 +87,17 @@ class LLMClient {
|
|
|
83
87
|
return this.traced(
|
|
84
88
|
{ origin, sessionId, answerId, currentUser, modelName, provider, temperature },
|
|
85
89
|
context,
|
|
86
|
-
() => this.
|
|
90
|
+
() => this.withoutRejectedTemperature(`${provider}/${modelName}`, (omitTemperature) => models.complete(model, context, { ...omitTemperature ? {} : { temperature }, maxRetries: MAX_RETRIES }))
|
|
87
91
|
);
|
|
88
92
|
}
|
|
93
|
+
async withoutRejectedTemperature(key, send) {
|
|
94
|
+
const known = LLMClient.modelsWithoutTemperature.has(key);
|
|
95
|
+
const response = await this.waitingOnRateLimit(() => send(known));
|
|
96
|
+
if (known || response.stopReason !== "error" || !rejectsSamplingParameter(response.errorMessage)) return response;
|
|
97
|
+
LLMClient.modelsWithoutTemperature.add(key);
|
|
98
|
+
this.logger.warn({ model: key, error: response.errorMessage }, "[LLM Service] The model rejected the temperature setting, retrying without it");
|
|
99
|
+
return this.waitingOnRateLimit(() => send(true));
|
|
100
|
+
}
|
|
89
101
|
async waitingOnRateLimit(run) {
|
|
90
102
|
let response = await run();
|
|
91
103
|
for (let waits = 0; waits < MAX_RATE_LIMIT_WAITS && response.stopReason === "error"; waits++) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type LLMErrorCode = 'LLM_RATE_LIMITED' | 'LLM_QUOTA_EXCEEDED' | 'LLM_UNAVAILABLE' | 'LLM_FAILED';
|
|
1
|
+
export type LLMErrorCode = 'LLM_RATE_LIMITED' | 'LLM_QUOTA_EXCEEDED' | 'LLM_UNAVAILABLE' | 'LLM_UNSUPPORTED_PARAMETER' | 'LLM_FAILED';
|
|
2
2
|
export declare class LLMError extends Error {
|
|
3
3
|
readonly code: LLMErrorCode;
|
|
4
4
|
readonly isRetryable: boolean;
|
|
@@ -6,4 +6,5 @@ export declare class LLMError extends Error {
|
|
|
6
6
|
constructor(code: LLMErrorCode, detail: string);
|
|
7
7
|
static fromProvider(detail: string): LLMError;
|
|
8
8
|
}
|
|
9
|
+
export declare function rejectsSamplingParameter(detail?: string): boolean;
|
|
9
10
|
export declare function retryDelayMs(detail?: string): number | undefined;
|
|
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var llm_error_exports = {};
|
|
20
20
|
__export(llm_error_exports, {
|
|
21
21
|
LLMError: () => LLMError,
|
|
22
|
+
rejectsSamplingParameter: () => rejectsSamplingParameter,
|
|
22
23
|
retryDelayMs: () => retryDelayMs
|
|
23
24
|
});
|
|
24
25
|
module.exports = __toCommonJS(llm_error_exports);
|
|
@@ -26,15 +27,19 @@ const USER_MESSAGES = {
|
|
|
26
27
|
LLM_RATE_LIMITED: "The model is receiving too many requests right now. Please try again in a few seconds.",
|
|
27
28
|
LLM_QUOTA_EXCEEDED: "The usage quota of the model provider has been exhausted. Please check the plan and billing of the provider.",
|
|
28
29
|
LLM_UNAVAILABLE: "The model provider is temporarily unavailable. Please try again in a moment.",
|
|
30
|
+
LLM_UNSUPPORTED_PARAMETER: "The model does not accept one of the request settings. Please try again.",
|
|
29
31
|
LLM_FAILED: "The model request failed. Please try again."
|
|
30
32
|
};
|
|
31
33
|
const RETRYABLE = {
|
|
32
34
|
LLM_RATE_LIMITED: true,
|
|
33
35
|
LLM_QUOTA_EXCEEDED: false,
|
|
34
36
|
LLM_UNAVAILABLE: true,
|
|
37
|
+
LLM_UNSUPPORTED_PARAMETER: true,
|
|
35
38
|
LLM_FAILED: true
|
|
36
39
|
};
|
|
37
40
|
const MAX_WAIT_MS = 6e4;
|
|
41
|
+
const SAMPLING_PARAMETER = /\b(?:temperature|top_p|top_k)\b/i;
|
|
42
|
+
const REJECTION = /unsupported|not supported|does not support|doesn't support|deprecated|not allowed|only the default|no longer|removed|cannot be|can't be|must be|invalid/i;
|
|
38
43
|
class LLMError extends Error {
|
|
39
44
|
code;
|
|
40
45
|
isRetryable;
|
|
@@ -49,7 +54,11 @@ class LLMError extends Error {
|
|
|
49
54
|
return new LLMError(classify(detail), detail);
|
|
50
55
|
}
|
|
51
56
|
}
|
|
57
|
+
function rejectsSamplingParameter(detail) {
|
|
58
|
+
return Boolean(detail) && SAMPLING_PARAMETER.test(detail) && REJECTION.test(detail);
|
|
59
|
+
}
|
|
52
60
|
function classify(detail) {
|
|
61
|
+
if (rejectsSamplingParameter(detail)) return "LLM_UNSUPPORTED_PARAMETER";
|
|
53
62
|
if (/PerDay|per day|billing|insufficient_quota|exceeded your current quota[\s\S]*(daily|day)/i.test(detail) && !/per.?min/i.test(detail)) return "LLM_QUOTA_EXCEEDED";
|
|
54
63
|
if (/\b429\b|rate.?limit|RESOURCE_EXHAUSTED|too many requests/i.test(detail)) return "LLM_RATE_LIMITED";
|
|
55
64
|
if (/\b50[0-9]\b|unavailable|overloaded|timeout|timed out|econn|network/i.test(detail)) return "LLM_UNAVAILABLE";
|
|
@@ -65,5 +74,6 @@ function retryDelayMs(detail) {
|
|
|
65
74
|
// Annotate the CommonJS export names for ESM import in node:
|
|
66
75
|
0 && (module.exports = {
|
|
67
76
|
LLMError,
|
|
77
|
+
rejectsSamplingParameter,
|
|
68
78
|
retryDelayMs
|
|
69
79
|
});
|
|
@@ -3,15 +3,19 @@ const USER_MESSAGES = {
|
|
|
3
3
|
LLM_RATE_LIMITED: "The model is receiving too many requests right now. Please try again in a few seconds.",
|
|
4
4
|
LLM_QUOTA_EXCEEDED: "The usage quota of the model provider has been exhausted. Please check the plan and billing of the provider.",
|
|
5
5
|
LLM_UNAVAILABLE: "The model provider is temporarily unavailable. Please try again in a moment.",
|
|
6
|
+
LLM_UNSUPPORTED_PARAMETER: "The model does not accept one of the request settings. Please try again.",
|
|
6
7
|
LLM_FAILED: "The model request failed. Please try again."
|
|
7
8
|
};
|
|
8
9
|
const RETRYABLE = {
|
|
9
10
|
LLM_RATE_LIMITED: true,
|
|
10
11
|
LLM_QUOTA_EXCEEDED: false,
|
|
11
12
|
LLM_UNAVAILABLE: true,
|
|
13
|
+
LLM_UNSUPPORTED_PARAMETER: true,
|
|
12
14
|
LLM_FAILED: true
|
|
13
15
|
};
|
|
14
16
|
const MAX_WAIT_MS = 6e4;
|
|
17
|
+
const SAMPLING_PARAMETER = /\b(?:temperature|top_p|top_k)\b/i;
|
|
18
|
+
const REJECTION = /unsupported|not supported|does not support|doesn't support|deprecated|not allowed|only the default|no longer|removed|cannot be|can't be|must be|invalid/i;
|
|
15
19
|
class LLMError extends Error {
|
|
16
20
|
code;
|
|
17
21
|
isRetryable;
|
|
@@ -26,7 +30,11 @@ class LLMError extends Error {
|
|
|
26
30
|
return new LLMError(classify(detail), detail);
|
|
27
31
|
}
|
|
28
32
|
}
|
|
33
|
+
function rejectsSamplingParameter(detail) {
|
|
34
|
+
return Boolean(detail) && SAMPLING_PARAMETER.test(detail) && REJECTION.test(detail);
|
|
35
|
+
}
|
|
29
36
|
function classify(detail) {
|
|
37
|
+
if (rejectsSamplingParameter(detail)) return "LLM_UNSUPPORTED_PARAMETER";
|
|
30
38
|
if (/PerDay|per day|billing|insufficient_quota|exceeded your current quota[\s\S]*(daily|day)/i.test(detail) && !/per.?min/i.test(detail)) return "LLM_QUOTA_EXCEEDED";
|
|
31
39
|
if (/\b429\b|rate.?limit|RESOURCE_EXHAUSTED|too many requests/i.test(detail)) return "LLM_RATE_LIMITED";
|
|
32
40
|
if (/\b50[0-9]\b|unavailable|overloaded|timeout|timed out|econn|network/i.test(detail)) return "LLM_UNAVAILABLE";
|
|
@@ -41,5 +49,6 @@ function retryDelayMs(detail) {
|
|
|
41
49
|
}
|
|
42
50
|
export {
|
|
43
51
|
LLMError,
|
|
52
|
+
rejectsSamplingParameter,
|
|
44
53
|
retryDelayMs
|
|
45
54
|
};
|
|
@@ -15,6 +15,7 @@ export interface BaseEntityProps {
|
|
|
15
15
|
archivedAt?: string | Date;
|
|
16
16
|
archivedBy?: string | ObjectId;
|
|
17
17
|
sharedWith?: string[] | ObjectId[];
|
|
18
|
+
toUnset?: Record<string, unknown>;
|
|
18
19
|
}
|
|
19
20
|
export interface Document extends BaseEntityProps {
|
|
20
21
|
toUnset?: Record<string, unknown>;
|
|
@@ -33,7 +34,7 @@ export declare class BaseEntity {
|
|
|
33
34
|
archivedAt?: Date;
|
|
34
35
|
archivedBy?: ObjectId;
|
|
35
36
|
sharedWith?: ObjectId[];
|
|
36
|
-
|
|
37
|
+
toUnset?: Record<string, unknown>;
|
|
37
38
|
static toObjectId(id?: string | ObjectId): ObjectId | undefined;
|
|
38
39
|
private static toDate;
|
|
39
40
|
private static toNumber;
|
package/dist/harness/agent.js
CHANGED
|
@@ -235,6 +235,8 @@ class Agent {
|
|
|
235
235
|
this.snapshots.set(`task:${task.id}`, this.fingerprint(task));
|
|
236
236
|
}
|
|
237
237
|
this.currentChat.tasks = tasks;
|
|
238
|
+
const finished = [import_agent_task.TaskStatus.COMPLETED, import_agent_task.TaskStatus.FAILED, import_agent_task.TaskStatus.CANCELLED, import_agent_task.TaskStatus.ABANDONED];
|
|
239
|
+
if (this.currentChat.activeTask?.status && finished.includes(this.currentChat.activeTask.status)) this.currentChat.state?.setActiveTaskId(void 0);
|
|
238
240
|
const activeTask = this.currentChat.activeTask;
|
|
239
241
|
if (activeTask) await this.hydrateTask(activeTask);
|
|
240
242
|
}
|
package/dist/harness/agent.mjs
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
updateAgentAction,
|
|
15
15
|
listAgentActionsByTask
|
|
16
16
|
} from "../services/tasks/index.js";
|
|
17
|
-
import { AgentTask } from "../services/tasks/entities/agent-task.entity.js";
|
|
17
|
+
import { AgentTask, TaskStatus } from "../services/tasks/entities/agent-task.entity.js";
|
|
18
18
|
import { AgentAction } from "../services/tasks/entities/agent-action.entity.js";
|
|
19
19
|
import { createAgentTraces } from "../services/traces/index.js";
|
|
20
20
|
import { Tool } from "../tools/tool.js";
|
|
@@ -220,6 +220,8 @@ class Agent {
|
|
|
220
220
|
this.snapshots.set(`task:${task.id}`, this.fingerprint(task));
|
|
221
221
|
}
|
|
222
222
|
this.currentChat.tasks = tasks;
|
|
223
|
+
const finished = [TaskStatus.COMPLETED, TaskStatus.FAILED, TaskStatus.CANCELLED, TaskStatus.ABANDONED];
|
|
224
|
+
if (this.currentChat.activeTask?.status && finished.includes(this.currentChat.activeTask.status)) this.currentChat.state?.setActiveTaskId(void 0);
|
|
223
225
|
const activeTask = this.currentChat.activeTask;
|
|
224
226
|
if (activeTask) await this.hydrateTask(activeTask);
|
|
225
227
|
}
|
package/dist/loops/agi/loop.js
CHANGED
|
@@ -33,7 +33,6 @@ const MAX_SAME_TOOL = 6;
|
|
|
33
33
|
const MAX_ANSWER_REJECTIONS = 1;
|
|
34
34
|
const WARN_WORKSPACE_TURNS = 3;
|
|
35
35
|
const MAX_WORKSPACE_TURNS = 7;
|
|
36
|
-
const SUMMARY_CHARS = 400;
|
|
37
36
|
const BLOCK_DUPLICATES = false;
|
|
38
37
|
class AgiLoop extends import_harness.AgentLoop {
|
|
39
38
|
get initialNode() {
|
|
@@ -124,7 +123,7 @@ class AgiLoop extends import_harness.AgentLoop {
|
|
|
124
123
|
if (stopReason) {
|
|
125
124
|
const answer = task.forceAnswer(`I could not finish the task: ${stopReason}.`);
|
|
126
125
|
ctx.reply(answer);
|
|
127
|
-
task.summary = answer
|
|
126
|
+
task.summary = answer;
|
|
128
127
|
ctx.chat.failTask(task.id);
|
|
129
128
|
ctx.chat.state?.setStatus(import_harness.AgentStatus.IDLE);
|
|
130
129
|
ctx.setNextNode(this.nodes.fast_responder);
|
|
@@ -162,7 +161,7 @@ class AgiLoop extends import_harness.AgentLoop {
|
|
|
162
161
|
}
|
|
163
162
|
task.deliverAnswer();
|
|
164
163
|
ctx.reply(answer);
|
|
165
|
-
ctx.chat.completeTask(task.id, answer
|
|
164
|
+
ctx.chat.completeTask(task.id, answer);
|
|
166
165
|
ctx.chat.state?.setStatus(import_harness.AgentStatus.IDLE);
|
|
167
166
|
ctx.setNextNode(this.nodes.fast_responder);
|
|
168
167
|
return;
|
package/dist/loops/agi/loop.mjs
CHANGED
|
@@ -11,7 +11,6 @@ const MAX_SAME_TOOL = 6;
|
|
|
11
11
|
const MAX_ANSWER_REJECTIONS = 1;
|
|
12
12
|
const WARN_WORKSPACE_TURNS = 3;
|
|
13
13
|
const MAX_WORKSPACE_TURNS = 7;
|
|
14
|
-
const SUMMARY_CHARS = 400;
|
|
15
14
|
const BLOCK_DUPLICATES = false;
|
|
16
15
|
class AgiLoop extends AgentLoop {
|
|
17
16
|
get initialNode() {
|
|
@@ -102,7 +101,7 @@ class AgiLoop extends AgentLoop {
|
|
|
102
101
|
if (stopReason) {
|
|
103
102
|
const answer = task.forceAnswer(`I could not finish the task: ${stopReason}.`);
|
|
104
103
|
ctx.reply(answer);
|
|
105
|
-
task.summary = answer
|
|
104
|
+
task.summary = answer;
|
|
106
105
|
ctx.chat.failTask(task.id);
|
|
107
106
|
ctx.chat.state?.setStatus(AgentStatus.IDLE);
|
|
108
107
|
ctx.setNextNode(this.nodes.fast_responder);
|
|
@@ -140,7 +139,7 @@ class AgiLoop extends AgentLoop {
|
|
|
140
139
|
}
|
|
141
140
|
task.deliverAnswer();
|
|
142
141
|
ctx.reply(answer);
|
|
143
|
-
ctx.chat.completeTask(task.id, answer
|
|
142
|
+
ctx.chat.completeTask(task.id, answer);
|
|
144
143
|
ctx.chat.state?.setStatus(AgentStatus.IDLE);
|
|
145
144
|
ctx.setNextNode(this.nodes.fast_responder);
|
|
146
145
|
return;
|
|
@@ -113,7 +113,7 @@ class Chat extends import_base.BaseEntity {
|
|
|
113
113
|
previousTaskId: opts?.continuesFrom,
|
|
114
114
|
loopName: this.settings?.loopName
|
|
115
115
|
});
|
|
116
|
-
newTask.threadId ??= newTask.
|
|
116
|
+
newTask.threadId ??= newTask._id;
|
|
117
117
|
this.tasks.push(newTask);
|
|
118
118
|
this.state?.setActiveTaskId(newTask.id);
|
|
119
119
|
return newTask;
|
|
@@ -143,12 +143,14 @@ class Chat extends import_base.BaseEntity {
|
|
|
143
143
|
clearActiveTaskIdIfMatches(task) {
|
|
144
144
|
if (task?.id === this.state?.activeTaskId) {
|
|
145
145
|
this.state?.setActiveTaskId(void 0);
|
|
146
|
+
this.unset(["state.activeTaskId"]);
|
|
146
147
|
}
|
|
147
148
|
}
|
|
148
149
|
switchLoop(loopName) {
|
|
149
150
|
if (this.settings?.loopName && this.settings.loopName !== loopName) {
|
|
150
151
|
this.activeTask?.pause();
|
|
151
152
|
this.state?.setActiveTaskId(void 0);
|
|
153
|
+
this.unset(["state.activeTaskId"]);
|
|
152
154
|
}
|
|
153
155
|
this.settings?.setLoopName(loopName);
|
|
154
156
|
}
|
|
@@ -91,7 +91,7 @@ class Chat extends BaseEntity {
|
|
|
91
91
|
previousTaskId: opts?.continuesFrom,
|
|
92
92
|
loopName: this.settings?.loopName
|
|
93
93
|
});
|
|
94
|
-
newTask.threadId ??= newTask.
|
|
94
|
+
newTask.threadId ??= newTask._id;
|
|
95
95
|
this.tasks.push(newTask);
|
|
96
96
|
this.state?.setActiveTaskId(newTask.id);
|
|
97
97
|
return newTask;
|
|
@@ -121,12 +121,14 @@ class Chat extends BaseEntity {
|
|
|
121
121
|
clearActiveTaskIdIfMatches(task) {
|
|
122
122
|
if (task?.id === this.state?.activeTaskId) {
|
|
123
123
|
this.state?.setActiveTaskId(void 0);
|
|
124
|
+
this.unset(["state.activeTaskId"]);
|
|
124
125
|
}
|
|
125
126
|
}
|
|
126
127
|
switchLoop(loopName) {
|
|
127
128
|
if (this.settings?.loopName && this.settings.loopName !== loopName) {
|
|
128
129
|
this.activeTask?.pause();
|
|
129
130
|
this.state?.setActiveTaskId(void 0);
|
|
131
|
+
this.unset(["state.activeTaskId"]);
|
|
130
132
|
}
|
|
131
133
|
this.settings?.setLoopName(loopName);
|
|
132
134
|
}
|
|
@@ -50,6 +50,11 @@ let UpdateChatUseCase = class extends import_base.BaseUseCase {
|
|
|
50
50
|
}
|
|
51
51
|
const chat = import_entities.Chat.fromJSON({ ...payload, _id: id });
|
|
52
52
|
chat.setUpdatedBy(currentUser);
|
|
53
|
+
if (payload.toUnset) {
|
|
54
|
+
const flat = chat.toFlatDocument();
|
|
55
|
+
const fields = Object.keys(payload.toUnset).filter((field) => flat[field] === void 0);
|
|
56
|
+
if (fields.length) chat.unset(fields);
|
|
57
|
+
}
|
|
53
58
|
const result = await this.chatRepository?.update(chat);
|
|
54
59
|
if (!result) {
|
|
55
60
|
throw new Error("Chat not found or update failed");
|
|
@@ -22,6 +22,11 @@ let UpdateChatUseCase = class extends BaseUseCase {
|
|
|
22
22
|
}
|
|
23
23
|
const chat = Chat.fromJSON({ ...payload, _id: id });
|
|
24
24
|
chat.setUpdatedBy(currentUser);
|
|
25
|
+
if (payload.toUnset) {
|
|
26
|
+
const flat = chat.toFlatDocument();
|
|
27
|
+
const fields = Object.keys(payload.toUnset).filter((field) => flat[field] === void 0);
|
|
28
|
+
if (fields.length) chat.unset(fields);
|
|
29
|
+
}
|
|
25
30
|
const result = await this.chatRepository?.update(chat);
|
|
26
31
|
if (!result) {
|
|
27
32
|
throw new Error("Chat not found or update failed");
|
|
@@ -17,7 +17,7 @@ export interface AgentTaskProps extends BaseEntityProps {
|
|
|
17
17
|
status?: TaskStatus;
|
|
18
18
|
goal?: string;
|
|
19
19
|
nextNode?: string;
|
|
20
|
-
threadId?: string;
|
|
20
|
+
threadId?: string | ObjectId;
|
|
21
21
|
previousTaskId?: string;
|
|
22
22
|
summary?: string;
|
|
23
23
|
loopName?: string;
|
|
@@ -85,7 +85,7 @@ export declare class AgentTask extends BaseEntity {
|
|
|
85
85
|
status?: TaskStatus | undefined;
|
|
86
86
|
goal?: string | undefined;
|
|
87
87
|
nextNode?: string | undefined;
|
|
88
|
-
threadId?:
|
|
88
|
+
threadId?: ObjectId | undefined;
|
|
89
89
|
previousTaskId?: string | undefined;
|
|
90
90
|
summary?: string | undefined;
|
|
91
91
|
loopName?: string | undefined;
|
|
@@ -102,7 +102,7 @@ export declare class AgentTask extends BaseEntity {
|
|
|
102
102
|
static collectRefs(value: any): CollectedRefs;
|
|
103
103
|
actions: AgentAction[];
|
|
104
104
|
hydrated: boolean;
|
|
105
|
-
constructor(props: AgentTaskProps, chatId?: ObjectId | undefined, status?: TaskStatus | undefined, goal?: string | undefined, nextNode?: string | undefined, threadId?:
|
|
105
|
+
constructor(props: AgentTaskProps, chatId?: ObjectId | undefined, status?: TaskStatus | undefined, goal?: string | undefined, nextNode?: string | undefined, threadId?: ObjectId | undefined, previousTaskId?: string | undefined, summary?: string | undefined, loopName?: string | undefined, planCount?: number, planOffset?: number, executedOffset?: number, scratch?: Record<string, any> | undefined, opening?: string | undefined, turns?: TurnRecord[], known?: Record<string, string> | undefined, schemas?: Record<string, ToolSchemaSummary> | undefined);
|
|
106
106
|
toJSON(): AgentTaskProps;
|
|
107
107
|
get id(): string;
|
|
108
108
|
get clearedFields(): Record<string, ''>;
|
|
@@ -149,9 +149,9 @@ export declare class AgentTask extends BaseEntity {
|
|
|
149
149
|
get discoveredToolNames(): string[];
|
|
150
150
|
get failedActions(): AgentAction[];
|
|
151
151
|
get executedActions(): AgentAction[];
|
|
152
|
-
get lastExecutedAction(): AgentAction | undefined;
|
|
153
152
|
get replanCount(): number;
|
|
154
153
|
get executedSinceOpen(): number;
|
|
154
|
+
get lastExecutedAction(): AgentAction | undefined;
|
|
155
155
|
executionHistory(maxChars?: number): {
|
|
156
156
|
ref: string;
|
|
157
157
|
tool: string | undefined;
|
|
@@ -262,7 +262,7 @@ class AgentTask extends import_base.BaseEntity {
|
|
|
262
262
|
props.status,
|
|
263
263
|
props.goal,
|
|
264
264
|
props.nextNode,
|
|
265
|
-
props.threadId,
|
|
265
|
+
import_base.BaseEntity.toObjectId(props.threadId),
|
|
266
266
|
props.previousTaskId,
|
|
267
267
|
props.summary,
|
|
268
268
|
props.loopName,
|
|
@@ -292,9 +292,9 @@ class AgentTask extends import_base.BaseEntity {
|
|
|
292
292
|
previousTaskId: this.previousTaskId,
|
|
293
293
|
summary: this.summary,
|
|
294
294
|
loopName: this.loopName,
|
|
295
|
-
planCount: this.planCount,
|
|
296
|
-
planOffset: this.planOffset,
|
|
297
|
-
executedOffset: this.executedOffset,
|
|
295
|
+
planCount: this.planCount || void 0,
|
|
296
|
+
planOffset: this.planOffset || void 0,
|
|
297
|
+
executedOffset: this.executedOffset || void 0,
|
|
298
298
|
scratch: this.scratch,
|
|
299
299
|
opening: this.opening,
|
|
300
300
|
turns: this.turns.length ? this.turns : void 0,
|
|
@@ -578,15 +578,15 @@ class AgentTask extends import_base.BaseEntity {
|
|
|
578
578
|
get executedActions() {
|
|
579
579
|
return this.actions.filter((a) => a.isTerminal);
|
|
580
580
|
}
|
|
581
|
-
get lastExecutedAction() {
|
|
582
|
-
return this.actions.slice().reverse().find((a) => a.isTerminal);
|
|
583
|
-
}
|
|
584
581
|
get replanCount() {
|
|
585
582
|
return Math.max(0, this.planCount - this.planOffset - 1);
|
|
586
583
|
}
|
|
587
584
|
get executedSinceOpen() {
|
|
588
585
|
return Math.max(0, this.executedActions.length - this.executedOffset);
|
|
589
586
|
}
|
|
587
|
+
get lastExecutedAction() {
|
|
588
|
+
return this.actions.slice().reverse().find((a) => a.isTerminal);
|
|
589
|
+
}
|
|
590
590
|
executionHistory(maxChars = 1500) {
|
|
591
591
|
return this.executedActions.map((a) => a.promptView(maxChars));
|
|
592
592
|
}
|
|
@@ -230,7 +230,7 @@ class AgentTask extends BaseEntity {
|
|
|
230
230
|
props.status,
|
|
231
231
|
props.goal,
|
|
232
232
|
props.nextNode,
|
|
233
|
-
props.threadId,
|
|
233
|
+
BaseEntity.toObjectId(props.threadId),
|
|
234
234
|
props.previousTaskId,
|
|
235
235
|
props.summary,
|
|
236
236
|
props.loopName,
|
|
@@ -260,9 +260,9 @@ class AgentTask extends BaseEntity {
|
|
|
260
260
|
previousTaskId: this.previousTaskId,
|
|
261
261
|
summary: this.summary,
|
|
262
262
|
loopName: this.loopName,
|
|
263
|
-
planCount: this.planCount,
|
|
264
|
-
planOffset: this.planOffset,
|
|
265
|
-
executedOffset: this.executedOffset,
|
|
263
|
+
planCount: this.planCount || void 0,
|
|
264
|
+
planOffset: this.planOffset || void 0,
|
|
265
|
+
executedOffset: this.executedOffset || void 0,
|
|
266
266
|
scratch: this.scratch,
|
|
267
267
|
opening: this.opening,
|
|
268
268
|
turns: this.turns.length ? this.turns : void 0,
|
|
@@ -546,15 +546,15 @@ class AgentTask extends BaseEntity {
|
|
|
546
546
|
get executedActions() {
|
|
547
547
|
return this.actions.filter((a) => a.isTerminal);
|
|
548
548
|
}
|
|
549
|
-
get lastExecutedAction() {
|
|
550
|
-
return this.actions.slice().reverse().find((a) => a.isTerminal);
|
|
551
|
-
}
|
|
552
549
|
get replanCount() {
|
|
553
550
|
return Math.max(0, this.planCount - this.planOffset - 1);
|
|
554
551
|
}
|
|
555
552
|
get executedSinceOpen() {
|
|
556
553
|
return Math.max(0, this.executedActions.length - this.executedOffset);
|
|
557
554
|
}
|
|
555
|
+
get lastExecutedAction() {
|
|
556
|
+
return this.actions.slice().reverse().find((a) => a.isTerminal);
|
|
557
|
+
}
|
|
558
558
|
executionHistory(maxChars = 1500) {
|
|
559
559
|
return this.executedActions.map((a) => a.promptView(maxChars));
|
|
560
560
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openloops",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.35",
|
|
4
4
|
"description": "Framework to build AI Agents",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Jose Barcos",
|
|
@@ -72,7 +72,10 @@
|
|
|
72
72
|
"prepublishOnly": "yarn build",
|
|
73
73
|
"check-size": "npm pack --dry-run",
|
|
74
74
|
"release": "npm version patch && npm publish --access public",
|
|
75
|
-
"push": "git push --follow-tags"
|
|
75
|
+
"push": "git push --follow-tags",
|
|
76
|
+
"test": "jest",
|
|
77
|
+
"test:watch": "jest --watch",
|
|
78
|
+
"test:coverage": "jest --coverage"
|
|
76
79
|
},
|
|
77
80
|
"dependencies": {
|
|
78
81
|
"@earendil-works/pi-ai": "^1.0.4",
|