@looopy-ai/core 2.1.9 → 2.1.12
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/core/agent.d.ts +3 -21
- package/dist/core/agent.js +3 -5
- package/dist/core/index.d.ts +1 -2
- package/dist/core/index.js +0 -1
- package/dist/core/iteration.d.ts +3 -3
- package/dist/core/iteration.js +30 -34
- package/dist/core/loop.d.ts +3 -3
- package/dist/core/tools.d.ts +1 -1
- package/dist/core/tools.js +26 -19
- package/dist/events/utils.d.ts +19 -53
- package/dist/events/utils.js +0 -34
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/observability/spans/iteration.d.ts +1 -1
- package/dist/observability/spans/llm-call.d.ts +5 -5
- package/dist/observability/spans/llm-call.js +8 -4
- package/dist/observability/spans/tool.d.ts +3 -3
- package/dist/plugins/agent-academy.d.ts +9 -0
- package/dist/plugins/agent-academy.js +137 -0
- package/dist/plugins/index.d.ts +2 -0
- package/dist/plugins/index.js +2 -0
- package/dist/plugins/system-prompt.d.ts +3 -0
- package/dist/plugins/system-prompt.js +20 -0
- package/dist/providers/litellm-provider.d.ts +2 -2
- package/dist/providers/litellm-provider.js +1 -1
- package/dist/stores/artifacts/internal-event-artifact-store.d.ts +2 -2
- package/dist/stores/artifacts/internal-event-artifact-store.js +0 -6
- package/dist/stores/filesystem/filesystem-message-store.d.ts +5 -5
- package/dist/stores/messages/hybrid-message-store.d.ts +7 -7
- package/dist/stores/messages/interfaces.d.ts +8 -8
- package/dist/stores/messages/mem0-message-store.d.ts +5 -5
- package/dist/stores/messages/memory-message-store.d.ts +5 -5
- package/dist/tools/agent-tool-provider.d.ts +5 -5
- package/dist/tools/agent-tool-provider.js +14 -10
- package/dist/tools/artifact-tools.d.ts +2 -2
- package/dist/tools/artifact-tools.js +14 -14
- package/dist/tools/index.d.ts +0 -1
- package/dist/tools/index.js +0 -1
- package/dist/tools/local-tools.d.ts +4 -3
- package/dist/tools/local-tools.js +8 -8
- package/dist/tools/mcp-tool-provider.d.ts +5 -4
- package/dist/tools/mcp-tool-provider.js +7 -7
- package/dist/tools/tool-result-events.d.ts +3 -4
- package/dist/tools/tool-result-events.js +2 -8
- package/dist/types/a2a.d.ts +2 -2
- package/dist/types/agent.d.ts +15 -1
- package/dist/types/core.d.ts +56 -0
- package/dist/types/core.js +8 -0
- package/dist/types/event.d.ts +6 -5
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/llm.d.ts +3 -3
- package/dist/types/message.d.ts +5 -5
- package/dist/types/prettify.d.ts +3 -0
- package/dist/types/state.d.ts +2 -2
- package/dist/types/tools.d.ts +3 -12
- package/dist/types/tools.js +2 -2
- package/dist/utils/prompt.d.ts +5 -8
- package/dist/utils/prompt.js +12 -10
- package/package.json +5 -3
- package/dist/core/types.d.ts +0 -31
- package/dist/skills/index.d.ts +0 -1
- package/dist/skills/index.js +0 -1
- package/dist/skills/registry.d.ts +0 -14
- package/dist/skills/registry.js +0 -61
- package/dist/tools/client-tool-provider.d.ts +0 -25
- package/dist/tools/client-tool-provider.js +0 -143
- /package/dist/{core/types.js → types/prettify.js} +0 -0
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import type { LoopContext } from '../../core
|
|
1
|
+
import type { LoopContext } from '../../types/core';
|
|
2
2
|
import type { AnyEvent } from '../../types/event';
|
|
3
|
-
import type {
|
|
3
|
+
import type { LLMMessage } from '../../types/message';
|
|
4
4
|
import type { ToolDefinition } from '../../types/tools';
|
|
5
|
-
import type {
|
|
5
|
+
import type { SystemPrompts } from '../../utils/prompt';
|
|
6
6
|
export interface LLMCallSpanParams {
|
|
7
7
|
agentId: string;
|
|
8
8
|
taskId: string;
|
|
9
|
-
messages:
|
|
9
|
+
messages: LLMMessage[];
|
|
10
10
|
parentContext: import('@opentelemetry/api').Context;
|
|
11
11
|
}
|
|
12
|
-
export declare const startLLMCallSpan: <AuthContext>(context: LoopContext<AuthContext>,
|
|
12
|
+
export declare const startLLMCallSpan: <AuthContext>(context: LoopContext<AuthContext>, systemPrompts: SystemPrompts, messages: LLMMessage[], tools: ToolDefinition[]) => {
|
|
13
13
|
span: import("@opentelemetry/api").Span;
|
|
14
14
|
traceContext: import("@opentelemetry/api").Context;
|
|
15
15
|
tapFinish: import("rxjs").MonoTypeOperatorFunction<AnyEvent>;
|
|
@@ -2,17 +2,21 @@ import { SpanStatusCode, trace } from '@opentelemetry/api';
|
|
|
2
2
|
import { tap } from 'rxjs/internal/operators/tap';
|
|
3
3
|
import { isChildTaskEvent } from '../../events/utils';
|
|
4
4
|
import { SpanAttributes, SpanNames } from '../tracing';
|
|
5
|
-
export const startLLMCallSpan = (context,
|
|
5
|
+
export const startLLMCallSpan = (context, systemPrompts, messages, tools) => {
|
|
6
6
|
const tracer = trace.getTracer('looopy');
|
|
7
|
+
const source = systemPrompts.before
|
|
8
|
+
.concat(systemPrompts.after)
|
|
9
|
+
.filter((sp) => sp.source?.providerName === 'langfuse')
|
|
10
|
+
.at(0)?.source;
|
|
7
11
|
const span = tracer.startSpan(SpanNames.LLM_CALL, {
|
|
8
12
|
attributes: {
|
|
9
13
|
[SpanAttributes.AGENT_ID]: context.agentId,
|
|
10
14
|
[SpanAttributes.TASK_ID]: context.taskId,
|
|
11
15
|
[SpanAttributes.GEN_AI_PROMPT]: JSON.stringify(messages),
|
|
12
16
|
[SpanAttributes.LANGFUSE_OBSERVATION_TYPE]: 'generation',
|
|
13
|
-
[SpanAttributes.LANGFUSE_PROMPT_NAME]:
|
|
14
|
-
[SpanAttributes.LANGFUSE_PROMPT_VERSION]:
|
|
15
|
-
'tools.available': tools.map((t) => t.
|
|
17
|
+
[SpanAttributes.LANGFUSE_PROMPT_NAME]: source?.promptName,
|
|
18
|
+
[SpanAttributes.LANGFUSE_PROMPT_VERSION]: source?.promptVersion,
|
|
19
|
+
'tools.available': tools.map((t) => t.id),
|
|
16
20
|
},
|
|
17
21
|
}, context.parentContext);
|
|
18
22
|
const traceContext = trace.setSpan(context.parentContext, span);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { IterationContext } from '../../core
|
|
2
|
-
import type { ContextAnyEvent, ToolCallEvent } from '../../types/event';
|
|
1
|
+
import type { IterationContext } from '../../types/core';
|
|
2
|
+
import type { AnyEvent, ContextAnyEvent, ToolCallEvent } from '../../types/event';
|
|
3
3
|
import type { ToolCall } from '../../types/tools';
|
|
4
4
|
export interface ToolExecutionSpanParams {
|
|
5
5
|
agentId: string;
|
|
@@ -10,5 +10,5 @@ export interface ToolExecutionSpanParams {
|
|
|
10
10
|
export declare const startToolExecuteSpan: <AuthContext>(context: IterationContext<AuthContext>, toolStart: ToolCallEvent) => {
|
|
11
11
|
span: import("@opentelemetry/api").Span;
|
|
12
12
|
traceContext: import("@opentelemetry/api").Context;
|
|
13
|
-
tapFinish: import("rxjs").MonoTypeOperatorFunction<ContextAnyEvent>;
|
|
13
|
+
tapFinish: import("rxjs").MonoTypeOperatorFunction<AnyEvent | ContextAnyEvent>;
|
|
14
14
|
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Skill } from '../types';
|
|
2
|
+
import type { IterationContext, Plugin, SystemPrompt } from '../types/core';
|
|
3
|
+
export declare const learnSkillToolName = "learn_skill";
|
|
4
|
+
export declare const getInstruction: (instruction: string | (() => Promise<string>)) => Promise<string>;
|
|
5
|
+
export declare const skill: (definition: Skill) => Skill;
|
|
6
|
+
export type AgentAcademyOptions<AuthContext> = {
|
|
7
|
+
learnSkillPrompt?: (skills: Skill[], context: IterationContext<AuthContext>) => Promise<string | SystemPrompt>;
|
|
8
|
+
};
|
|
9
|
+
export declare function agentAcademy<AuthContext>(skills: Skill[], options?: AgentAcademyOptions<AuthContext>): Plugin<AuthContext>;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { catchError, defer, mergeMap, of } from 'rxjs';
|
|
2
|
+
import z from 'zod';
|
|
3
|
+
import { toolErrorEvent, toolResultToEvents } from '../tools/tool-result-events';
|
|
4
|
+
export const learnSkillToolName = 'learn_skill';
|
|
5
|
+
export const getInstruction = async (instruction) => {
|
|
6
|
+
if (typeof instruction === 'string') {
|
|
7
|
+
return instruction;
|
|
8
|
+
}
|
|
9
|
+
return await instruction();
|
|
10
|
+
};
|
|
11
|
+
export const skill = (definition) => {
|
|
12
|
+
return { ...definition };
|
|
13
|
+
};
|
|
14
|
+
export function agentAcademy(skills, options) {
|
|
15
|
+
const skillMap = new Map();
|
|
16
|
+
for (const skill of skills) {
|
|
17
|
+
if (skillMap.has(skill.name)) {
|
|
18
|
+
throw new Error(`Duplicate skill name: ${skill.name}`);
|
|
19
|
+
}
|
|
20
|
+
skillMap.set(skill.name, skill);
|
|
21
|
+
}
|
|
22
|
+
const learnSkillToolDefinition = {
|
|
23
|
+
id: learnSkillToolName,
|
|
24
|
+
description: 'Learns a new skill from the available agent academy.',
|
|
25
|
+
icon: 'lucide:graduation-cap',
|
|
26
|
+
parameters: {
|
|
27
|
+
type: 'object',
|
|
28
|
+
properties: {
|
|
29
|
+
name: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
description: 'The name of the skill to learn.',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
required: ['name'],
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
const learnSkillPromptFn = options?.learnSkillPrompt ?? defaultPrompt;
|
|
38
|
+
return {
|
|
39
|
+
name: 'agent-academy',
|
|
40
|
+
generateSystemPrompts: async (context) => {
|
|
41
|
+
if (!learnSkillPromptFn) {
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
const prompt = await learnSkillPromptFn(skills, context);
|
|
45
|
+
if (typeof prompt === 'string') {
|
|
46
|
+
return [
|
|
47
|
+
{
|
|
48
|
+
content: prompt,
|
|
49
|
+
position: 'before',
|
|
50
|
+
positionSequence: 100,
|
|
51
|
+
},
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
return [
|
|
55
|
+
{
|
|
56
|
+
...prompt,
|
|
57
|
+
positionSequence: prompt.positionSequence ?? 100,
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
},
|
|
61
|
+
listTools: async () => {
|
|
62
|
+
return [learnSkillToolDefinition];
|
|
63
|
+
},
|
|
64
|
+
getTool: async (toolId) => {
|
|
65
|
+
if (toolId === learnSkillToolName) {
|
|
66
|
+
return learnSkillToolDefinition;
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
},
|
|
70
|
+
executeTool: (toolCall, _context) => defer(async () => {
|
|
71
|
+
if (toolCall.function.name !== learnSkillToolName) {
|
|
72
|
+
return {
|
|
73
|
+
toolCallId: toolCall.id,
|
|
74
|
+
toolName: toolCall.function.name,
|
|
75
|
+
success: false,
|
|
76
|
+
result: null,
|
|
77
|
+
error: `Tool ${toolCall.function.name} not found`,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const schema = z.object({
|
|
82
|
+
name: z.string().describe('The name of the skill to learn.'),
|
|
83
|
+
});
|
|
84
|
+
const validatedParams = schema.parse(toolCall.function.arguments);
|
|
85
|
+
const foundSkill = skillMap.get(validatedParams.name);
|
|
86
|
+
if (!foundSkill) {
|
|
87
|
+
const availableSkills = Array.from(skillMap.values())
|
|
88
|
+
.map((s) => `'${s.name}'`)
|
|
89
|
+
.join(', ');
|
|
90
|
+
return {
|
|
91
|
+
toolCallId: toolCall.id,
|
|
92
|
+
toolName: toolCall.function.name,
|
|
93
|
+
success: false,
|
|
94
|
+
result: null,
|
|
95
|
+
error: `Skill '${validatedParams.name}' not found. Available skills are: ${availableSkills}`,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const systemMessage = {
|
|
99
|
+
role: 'system',
|
|
100
|
+
content: `You have learned the following skill:\n\n**${foundSkill.name}**\n${await getInstruction(foundSkill.instruction)}`,
|
|
101
|
+
};
|
|
102
|
+
return {
|
|
103
|
+
toolCallId: toolCall.id,
|
|
104
|
+
toolName: toolCall.function.name,
|
|
105
|
+
success: true,
|
|
106
|
+
result: `Successfully learned the '${validatedParams.name}' skill.`,
|
|
107
|
+
messages: [systemMessage],
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
if (error instanceof z.ZodError) {
|
|
112
|
+
return {
|
|
113
|
+
toolCallId: toolCall.id,
|
|
114
|
+
toolName: toolCall.function.name,
|
|
115
|
+
success: false,
|
|
116
|
+
result: null,
|
|
117
|
+
error: `Invalid arguments: ${error.issues.map((e) => `${e.path.join('.')}: ${e.message}`).join(', ')}`,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
121
|
+
return {
|
|
122
|
+
toolCallId: toolCall.id,
|
|
123
|
+
toolName: toolCall.function.name,
|
|
124
|
+
success: false,
|
|
125
|
+
result: null,
|
|
126
|
+
error: err.message,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}).pipe(mergeMap((result) => toolResultToEvents(result)), catchError((error) => of(toolErrorEvent(toolCall, error instanceof Error ? error.message : String(error))))),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const defaultPrompt = (skills) => {
|
|
133
|
+
const skillList = skills
|
|
134
|
+
.map((skill) => `- **${skill.name}**: ${typeof skill.instruction === 'string' ? skill.instruction : 'A useful skill.'}`)
|
|
135
|
+
.join('\n');
|
|
136
|
+
return `You can learn new skills using the learn_skill tool. The available skills are:\n\n${skillList}\n\nTo learn a skill, call the learn_skill tool with the name of the skill you want to learn.`;
|
|
137
|
+
};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { IterationContext, Plugin, SystemPrompt } from '../types/core';
|
|
2
|
+
export declare const literalPrompt: <AuthContext = any>(content: string) => Plugin<AuthContext>;
|
|
3
|
+
export declare const asyncPrompt: <AuthContext>(content: (context: IterationContext<AuthContext>) => Promise<string | SystemPrompt>) => Plugin<AuthContext>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const literalPrompt = (content) => {
|
|
2
|
+
return {
|
|
3
|
+
name: 'literal-prompt',
|
|
4
|
+
generateSystemPrompts: async () => {
|
|
5
|
+
return [{ content, position: 'before' }];
|
|
6
|
+
},
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export const asyncPrompt = (content) => {
|
|
10
|
+
return {
|
|
11
|
+
name: 'async-prompt',
|
|
12
|
+
generateSystemPrompts: async (context) => {
|
|
13
|
+
const prompt = await content(context);
|
|
14
|
+
if (typeof prompt === 'string') {
|
|
15
|
+
return [{ content: prompt, position: 'before' }];
|
|
16
|
+
}
|
|
17
|
+
return [prompt];
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
2
|
import type { AnyEvent } from '../types/event';
|
|
3
3
|
import type { LLMProvider } from '../types/llm';
|
|
4
|
-
import type {
|
|
4
|
+
import type { LLMMessage } from '../types/message';
|
|
5
5
|
import type { ToolDefinition } from '../types/tools';
|
|
6
6
|
export interface LiteLLMConfig {
|
|
7
7
|
baseUrl: string;
|
|
@@ -20,7 +20,7 @@ export declare class LiteLLMProvider implements LLMProvider {
|
|
|
20
20
|
private readonly logger;
|
|
21
21
|
constructor(config: LiteLLMConfig);
|
|
22
22
|
call(request: {
|
|
23
|
-
messages:
|
|
23
|
+
messages: LLMMessage[];
|
|
24
24
|
tools?: ToolDefinition[];
|
|
25
25
|
sessionId?: string;
|
|
26
26
|
metadata?: Record<string, unknown>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ArtifactStore, DatasetSchema, StoredArtifact } from '../../types/artifact';
|
|
2
|
-
import type {
|
|
2
|
+
import type { AnyEvent } from '../../types/event';
|
|
3
3
|
export interface InternalEventEmitter {
|
|
4
|
-
emit(event:
|
|
4
|
+
emit(event: AnyEvent): void;
|
|
5
5
|
}
|
|
6
6
|
export interface InternalEventArtifactStoreConfig {
|
|
7
7
|
delegate: ArtifactStore;
|
|
@@ -18,8 +18,6 @@ export class InternalEventArtifactStore {
|
|
|
18
18
|
if (artifact && artifact.type === 'file') {
|
|
19
19
|
const chunkIndex = artifact.chunks.length - 1;
|
|
20
20
|
const eventOptions = {
|
|
21
|
-
contextId: artifact.contextId,
|
|
22
|
-
taskId: artifact.taskId,
|
|
23
21
|
artifactId,
|
|
24
22
|
data: chunk,
|
|
25
23
|
index: chunkIndex,
|
|
@@ -50,8 +48,6 @@ export class InternalEventArtifactStore {
|
|
|
50
48
|
const artifact = await this.delegate.getArtifact(contextId, artifactId);
|
|
51
49
|
if (artifact && artifact.type === 'data') {
|
|
52
50
|
const eventOptions = {
|
|
53
|
-
contextId: artifact.contextId,
|
|
54
|
-
taskId: artifact.taskId,
|
|
55
51
|
artifactId,
|
|
56
52
|
data,
|
|
57
53
|
name: artifact.name,
|
|
@@ -78,8 +74,6 @@ export class InternalEventArtifactStore {
|
|
|
78
74
|
if (artifact && artifact.type === 'dataset') {
|
|
79
75
|
const batchIndex = artifact.totalChunks - 1;
|
|
80
76
|
const eventOptions = {
|
|
81
|
-
contextId: artifact.contextId,
|
|
82
|
-
taskId: artifact.taskId,
|
|
83
77
|
artifactId,
|
|
84
78
|
rows,
|
|
85
79
|
index: batchIndex,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { LLMMessage } from '../../types/message';
|
|
2
2
|
import type { CompactionOptions, CompactionResult, MessageStore } from '../messages/interfaces';
|
|
3
3
|
export interface FileSystemMessageStoreConfig {
|
|
4
4
|
basePath?: string;
|
|
@@ -9,14 +9,14 @@ export declare class FileSystemMessageStore implements MessageStore {
|
|
|
9
9
|
private agentId;
|
|
10
10
|
private messageIndex;
|
|
11
11
|
constructor(config: FileSystemMessageStoreConfig);
|
|
12
|
-
append(contextId: string, messages:
|
|
12
|
+
append(contextId: string, messages: LLMMessage[]): Promise<void>;
|
|
13
13
|
getRecent(contextId: string, options?: {
|
|
14
14
|
maxMessages?: number;
|
|
15
15
|
maxTokens?: number;
|
|
16
|
-
}): Promise<
|
|
17
|
-
getAll(contextId: string): Promise<
|
|
16
|
+
}): Promise<LLMMessage[]>;
|
|
17
|
+
getAll(contextId: string): Promise<LLMMessage[]>;
|
|
18
18
|
getCount(contextId: string): Promise<number>;
|
|
19
|
-
getRange(contextId: string, startIndex: number, endIndex: number): Promise<
|
|
19
|
+
getRange(contextId: string, startIndex: number, endIndex: number): Promise<LLMMessage[]>;
|
|
20
20
|
compact(contextId: string, options?: CompactionOptions): Promise<CompactionResult>;
|
|
21
21
|
clear(contextId: string): Promise<void>;
|
|
22
22
|
private getMessagesDir;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { LLMMessage } from '../../types/message';
|
|
2
2
|
import type { CompactionOptions, CompactionResult, MessageStore } from './interfaces';
|
|
3
3
|
export type SyncStrategy = 'immediate' | 'batch' | 'end-of-session';
|
|
4
4
|
export interface HybridConfig {
|
|
@@ -10,20 +10,20 @@ export interface HybridConfig {
|
|
|
10
10
|
export declare class HybridMessageStore implements MessageStore {
|
|
11
11
|
private config;
|
|
12
12
|
constructor(config: HybridConfig);
|
|
13
|
-
append(contextId: string, messages:
|
|
13
|
+
append(contextId: string, messages: LLMMessage[]): Promise<void>;
|
|
14
14
|
getRecent(contextId: string, options?: {
|
|
15
15
|
maxMessages?: number;
|
|
16
16
|
maxTokens?: number;
|
|
17
|
-
}): Promise<
|
|
18
|
-
getAll(contextId: string): Promise<
|
|
17
|
+
}): Promise<LLMMessage[]>;
|
|
18
|
+
getAll(contextId: string): Promise<LLMMessage[]>;
|
|
19
19
|
getCount(contextId: string): Promise<number>;
|
|
20
|
-
getRange(contextId: string, startIndex: number, endIndex: number): Promise<
|
|
20
|
+
getRange(contextId: string, startIndex: number, endIndex: number): Promise<LLMMessage[]>;
|
|
21
21
|
compact(contextId: string, options?: CompactionOptions): Promise<CompactionResult>;
|
|
22
22
|
clear(contextId: string): Promise<void>;
|
|
23
23
|
syncToMemory(contextId: string): Promise<void>;
|
|
24
|
-
getMemoryContext(contextId: string): Promise<
|
|
24
|
+
getMemoryContext(contextId: string): Promise<LLMMessage[]>;
|
|
25
25
|
getRawMessages(contextId: string, options?: {
|
|
26
26
|
maxMessages?: number;
|
|
27
27
|
maxTokens?: number;
|
|
28
|
-
}): Promise<
|
|
28
|
+
}): Promise<LLMMessage[]>;
|
|
29
29
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export type StoredMessage =
|
|
1
|
+
import type { LLMMessage } from '../../types/message';
|
|
2
|
+
export type StoredMessage = LLMMessage & {
|
|
3
3
|
id: string;
|
|
4
4
|
contextId: string;
|
|
5
5
|
index: number;
|
|
@@ -13,14 +13,14 @@ export type StoredMessage = Message & {
|
|
|
13
13
|
};
|
|
14
14
|
};
|
|
15
15
|
export interface MessageStore {
|
|
16
|
-
append(contextId: string, messages:
|
|
16
|
+
append(contextId: string, messages: LLMMessage[]): Promise<void>;
|
|
17
17
|
getRecent(contextId: string, options?: {
|
|
18
18
|
maxMessages?: number;
|
|
19
19
|
maxTokens?: number;
|
|
20
|
-
}): Promise<
|
|
21
|
-
getAll(contextId: string): Promise<
|
|
20
|
+
}): Promise<LLMMessage[]>;
|
|
21
|
+
getAll(contextId: string): Promise<LLMMessage[]>;
|
|
22
22
|
getCount(contextId: string): Promise<number>;
|
|
23
|
-
getRange(contextId: string, startIndex: number, endIndex: number): Promise<
|
|
23
|
+
getRange(contextId: string, startIndex: number, endIndex: number): Promise<LLMMessage[]>;
|
|
24
24
|
compact(contextId: string, options?: CompactionOptions): Promise<CompactionResult>;
|
|
25
25
|
clear(contextId: string): Promise<void>;
|
|
26
26
|
}
|
|
@@ -31,7 +31,7 @@ export interface CompactionOptions {
|
|
|
31
31
|
summaryPrompt?: string;
|
|
32
32
|
}
|
|
33
33
|
export interface CompactionResult {
|
|
34
|
-
summaryMessages:
|
|
34
|
+
summaryMessages: LLMMessage[];
|
|
35
35
|
compactedRange: {
|
|
36
36
|
start: number;
|
|
37
37
|
end: number;
|
|
@@ -39,4 +39,4 @@ export interface CompactionResult {
|
|
|
39
39
|
tokensSaved: number;
|
|
40
40
|
}
|
|
41
41
|
export declare function estimateTokens(content: string | unknown): number;
|
|
42
|
-
export declare function trimToTokenBudget<T extends
|
|
42
|
+
export declare function trimToTokenBudget<T extends LLMMessage>(messages: T[], maxTokens: number): T[];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { LLMMessage } from '../../types/message';
|
|
2
2
|
import type { CompactionOptions, CompactionResult, MessageStore } from './interfaces';
|
|
3
3
|
type MemoryClient = any;
|
|
4
4
|
export type Mem0MemoryLevel = 'conversation' | 'session' | 'user' | 'org';
|
|
@@ -15,16 +15,16 @@ export declare class Mem0MessageStore implements MessageStore {
|
|
|
15
15
|
private config;
|
|
16
16
|
private messageCache;
|
|
17
17
|
constructor(config: Mem0Config);
|
|
18
|
-
append(contextId: string, messages:
|
|
18
|
+
append(contextId: string, messages: LLMMessage[]): Promise<void>;
|
|
19
19
|
getRecent(contextId: string, options?: {
|
|
20
20
|
maxMessages?: number;
|
|
21
21
|
maxTokens?: number;
|
|
22
|
-
}): Promise<
|
|
22
|
+
}): Promise<LLMMessage[]>;
|
|
23
23
|
private searchMemories;
|
|
24
24
|
private formatMemoriesAsContext;
|
|
25
|
-
getAll(contextId: string): Promise<
|
|
25
|
+
getAll(contextId: string): Promise<LLMMessage[]>;
|
|
26
26
|
getCount(contextId: string): Promise<number>;
|
|
27
|
-
getRange(contextId: string, startIndex: number, endIndex: number): Promise<
|
|
27
|
+
getRange(contextId: string, startIndex: number, endIndex: number): Promise<LLMMessage[]>;
|
|
28
28
|
compact(contextId: string, options?: CompactionOptions): Promise<CompactionResult>;
|
|
29
29
|
clear(contextId: string): Promise<void>;
|
|
30
30
|
private extractUserId;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { LLMProvider } from '../../types/llm';
|
|
2
|
-
import type {
|
|
2
|
+
import type { LLMMessage } from '../../types/message';
|
|
3
3
|
import type { CompactionOptions, CompactionResult, MessageStore } from './interfaces';
|
|
4
4
|
export interface InMemoryMessageStoreConfig {
|
|
5
5
|
llmProvider?: LLMProvider;
|
|
@@ -9,14 +9,14 @@ export declare class InMemoryMessageStore implements MessageStore {
|
|
|
9
9
|
private messages;
|
|
10
10
|
private config;
|
|
11
11
|
constructor(config?: InMemoryMessageStoreConfig);
|
|
12
|
-
append(contextId: string, messages:
|
|
12
|
+
append(contextId: string, messages: LLMMessage[]): Promise<void>;
|
|
13
13
|
getRecent(contextId: string, options?: {
|
|
14
14
|
maxMessages?: number;
|
|
15
15
|
maxTokens?: number;
|
|
16
|
-
}): Promise<
|
|
17
|
-
getAll(contextId: string): Promise<
|
|
16
|
+
}): Promise<LLMMessage[]>;
|
|
17
|
+
getAll(contextId: string): Promise<LLMMessage[]>;
|
|
18
18
|
getCount(contextId: string): Promise<number>;
|
|
19
|
-
getRange(contextId: string, startIndex: number, endIndex: number): Promise<
|
|
19
|
+
getRange(contextId: string, startIndex: number, endIndex: number): Promise<LLMMessage[]>;
|
|
20
20
|
compact(contextId: string, options?: CompactionOptions): Promise<CompactionResult>;
|
|
21
21
|
clear(contextId: string): Promise<void>;
|
|
22
22
|
private compactSlidingWindow;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
2
|
import z from 'zod';
|
|
3
|
-
import type {
|
|
4
|
-
import type { ToolCall, ToolDefinition
|
|
3
|
+
import type { AnyEvent, ExecutionContext, Plugin } from '../types';
|
|
4
|
+
import type { ToolCall, ToolDefinition } from '../types/tools';
|
|
5
5
|
export type HeaderFactory<AuthContext> = (context: ExecutionContext<AuthContext>, card: AgentCard) => Promise<Record<string, string | undefined>>;
|
|
6
6
|
declare const cardSchema: z.ZodObject<{
|
|
7
7
|
name: z.ZodString;
|
|
@@ -19,7 +19,7 @@ declare const cardSchema: z.ZodObject<{
|
|
|
19
19
|
}, z.core.$strip>>;
|
|
20
20
|
}, z.core.$strip>;
|
|
21
21
|
type AgentCard = z.infer<typeof cardSchema>;
|
|
22
|
-
export declare class AgentToolProvider<AuthContext> implements
|
|
22
|
+
export declare class AgentToolProvider<AuthContext> implements Plugin<AuthContext> {
|
|
23
23
|
readonly card: AgentCard;
|
|
24
24
|
readonly getHeaders?: HeaderFactory<AuthContext> | undefined;
|
|
25
25
|
static fromUrl: <AuthContext_1>(cardUrl: string, getHeaders?: HeaderFactory<AuthContext_1>) => Promise<AgentToolProvider<AuthContext_1>>;
|
|
@@ -30,7 +30,7 @@ export declare class AgentToolProvider<AuthContext> implements ToolProvider<Auth
|
|
|
30
30
|
private readonly logger;
|
|
31
31
|
constructor(card: AgentCard, getHeaders?: HeaderFactory<AuthContext> | undefined);
|
|
32
32
|
getTool(toolName: string): Promise<ToolDefinition | undefined>;
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
listTools(): Promise<ToolDefinition[]>;
|
|
34
|
+
executeTool(toolCall: ToolCall, context: ExecutionContext<AuthContext>): Observable<AnyEvent>;
|
|
35
35
|
}
|
|
36
36
|
export {};
|
|
@@ -50,7 +50,7 @@ export class AgentToolProvider {
|
|
|
50
50
|
this.logger = getLogger({ component: 'agent-tool-provider', agentName: this.agentName });
|
|
51
51
|
this.tools = [
|
|
52
52
|
{
|
|
53
|
-
|
|
53
|
+
id: `${this.name}__invoke`,
|
|
54
54
|
description: `Invoke the ${card.name} agent.\n\n${card.description}` ||
|
|
55
55
|
`Invoke the ${card.name} agent`,
|
|
56
56
|
icon: card.icon,
|
|
@@ -68,13 +68,13 @@ export class AgentToolProvider {
|
|
|
68
68
|
];
|
|
69
69
|
}
|
|
70
70
|
getTool(toolName) {
|
|
71
|
-
const tool = this.tools.find((t) => t.
|
|
71
|
+
const tool = this.tools.find((t) => t.id === toolName);
|
|
72
72
|
return Promise.resolve(tool);
|
|
73
73
|
}
|
|
74
|
-
|
|
74
|
+
listTools() {
|
|
75
75
|
return Promise.resolve(this.tools);
|
|
76
76
|
}
|
|
77
|
-
|
|
77
|
+
executeTool(toolCall, context) {
|
|
78
78
|
const logger = this.logger.child({
|
|
79
79
|
taskId: context.taskId,
|
|
80
80
|
toolCallId: toolCall.id,
|
|
@@ -86,14 +86,14 @@ export class AgentToolProvider {
|
|
|
86
86
|
const tool = await this.getTool(toolCall.function.name);
|
|
87
87
|
if (!tool) {
|
|
88
88
|
logger.error({ toolName: toolCall.function.name }, 'Tool not found');
|
|
89
|
-
subscriber.next(toolErrorEvent(
|
|
89
|
+
subscriber.next(toolErrorEvent(toolCall, `Tool not found: ${toolCall.function.name}`));
|
|
90
90
|
subscriber.complete();
|
|
91
91
|
return;
|
|
92
92
|
}
|
|
93
93
|
const prompt = toolCall.function.arguments.prompt;
|
|
94
94
|
if (!prompt || typeof prompt !== 'string') {
|
|
95
95
|
logger.error('Invalid tool call arguments');
|
|
96
|
-
subscriber.next(toolErrorEvent(
|
|
96
|
+
subscriber.next(toolErrorEvent(toolCall, 'Tool argument must include "prompt" and it must be a string'));
|
|
97
97
|
subscriber.complete();
|
|
98
98
|
return;
|
|
99
99
|
}
|
|
@@ -111,14 +111,14 @@ export class AgentToolProvider {
|
|
|
111
111
|
});
|
|
112
112
|
if (!res.ok) {
|
|
113
113
|
logger.error({ status: res.status, statusText: res.statusText }, 'Agent call failed');
|
|
114
|
-
subscriber.next(toolErrorEvent(
|
|
114
|
+
subscriber.next(toolErrorEvent(toolCall, `Agent endpoint responded with ${res.status} ${res.statusText}`));
|
|
115
115
|
subscriber.complete();
|
|
116
116
|
return;
|
|
117
117
|
}
|
|
118
118
|
const body = res.body;
|
|
119
119
|
if (!body) {
|
|
120
120
|
logger.error('Agent response has no body');
|
|
121
|
-
subscriber.next(toolErrorEvent(
|
|
121
|
+
subscriber.next(toolErrorEvent(toolCall, 'Agent returned no response body'));
|
|
122
122
|
subscriber.complete();
|
|
123
123
|
return;
|
|
124
124
|
}
|
|
@@ -131,6 +131,12 @@ export class AgentToolProvider {
|
|
|
131
131
|
kind: e.event,
|
|
132
132
|
parentTaskId: context.taskId,
|
|
133
133
|
...data,
|
|
134
|
+
path: [
|
|
135
|
+
`agent:${this.card.name}`,
|
|
136
|
+
...('path' in data && Array.isArray(data.path)
|
|
137
|
+
? data.path.filter((p) => typeof p === 'string' && p)
|
|
138
|
+
: []),
|
|
139
|
+
],
|
|
134
140
|
});
|
|
135
141
|
if (e.event === 'task-complete') {
|
|
136
142
|
content = data.content;
|
|
@@ -140,8 +146,6 @@ export class AgentToolProvider {
|
|
|
140
146
|
if (!subscriber.closed) {
|
|
141
147
|
const toolCompleteEvent = {
|
|
142
148
|
kind: 'tool-complete',
|
|
143
|
-
contextId: context.contextId,
|
|
144
|
-
taskId: context.taskId,
|
|
145
149
|
toolCallId: toolCall.id,
|
|
146
150
|
toolName: toolCall.function.name,
|
|
147
151
|
success: true,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { ArtifactStore } from '../types/artifact';
|
|
2
|
+
import type { Plugin } from '../types/core';
|
|
2
3
|
import type { TaskStateStore } from '../types/state';
|
|
3
|
-
|
|
4
|
-
export declare function createArtifactTools<AuthContext>(artifactStore: ArtifactStore, taskStateStore: TaskStateStore): ToolProvider<AuthContext>;
|
|
4
|
+
export declare function createArtifactTools<AuthContext>(artifactStore: ArtifactStore, taskStateStore: TaskStateStore): Plugin<AuthContext>;
|