@fragola-ai/agent 0.1.0 → 0.1.1
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/package/agent.index.js.map +2 -2
- package/dist/package/event.index.js.map +1 -1
- package/dist/package/fragola.index.js +16 -16
- package/dist/package/fragola.index.js.map +3 -3
- package/dist/package/hook.index.js +16 -16
- package/dist/package/hook.index.js.map +3 -3
- package/dist/src/agent.d.ts +28 -14
- package/dist/src/agent.js +16 -16
- package/dist/src/agent.js.map +3 -3
- package/dist/src/agentContext.d.ts +5 -3
- package/dist/src/agentContext.js.map +2 -2
- package/dist/src/applyEvent.js +16 -16
- package/dist/src/applyEvent.js.map +3 -3
- package/dist/src/event.js.map +1 -1
- package/dist/src/eventAfter.d.ts +3 -2
- package/dist/src/eventBefore.d.ts +3 -2
- package/dist/src/eventDefault.d.ts +10 -1
- package/dist/src/fragola.d.ts +29 -0
- package/dist/src/fragola.js +16 -16
- package/dist/src/fragola.js.map +3 -3
- package/dist/src/hook/index.d.ts +2 -2
- package/dist/src/hook/index.js +16 -16
- package/dist/src/hook/index.js.map +3 -3
- package/dist/src/utils.js +1 -1
- package/dist/src/utils.js.map +1 -1
- package/package.json +6 -2
package/dist/src/event.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/agentContext.ts", "../../src/event.ts"],
|
|
4
|
-
"sourcesContent": ["import type { AgentOptions, AgentState, ContextRaw, SetOptionsParams } from \"./agent\";\nimport type { DefineMetaData, Fragola, Tool } from \"./fragola\";\nimport type { StoreLike } from \"./types\";\nimport type { Store } from \"./store\";\nimport type { MessagesParser } from \"./stateUtils\";\n\nexport const STOP = Symbol('stop');\nexport abstract class AgentContext<TMetaData extends DefineMetaData<any> = {}, TGlobalStore extends StoreLike<any> = {}, TStore extends StoreLike<any> = {}> {\n /** The current state of the agent. */\n abstract get state(): AgentState<TMetaData>;\n /** The configuration options for the agent. */\n abstract get options(): AgentOptions;\n /** Raw methods for advanced context manipulation */\n abstract get raw(): ContextRaw<TMetaData>;\n /** Acess the agent's default local store. */\n abstract get store(): Store<TStore>;\n /** Live parser helpers bound to the current `state.messages`. */\n abstract get messagesParser(): MessagesParser<TMetaData>;\n /**\n * Add a store that has a namespace. Can be accessed with `getStore` method.\n * @param store - The store to add\n */\n abstract addStore(store: Store<any>): void;\n\n /**\n * Updates the agent's tool list using a callback.\n *\n * @param callback - Function that receives the current tools and returns the updated list.\n *\n * @example\n * agent.context.updateTools(prev => [...prev, newTool]);\n * agent.context.updateTools(prev => prev.filter(tool => tool.name !== \"oldTool\"));\n */\n abstract updateTools(callback: (prev: Tool[]) => Tool[]): void;\n /**\n * Remove a store by its namespace.\n * @param namespace - The namespace of the store to remove\n */\n abstract removeStore(namespace: string): void;\n /** Return the Fragola instance which created this agent */\n abstract get instance(): Fragola<TGlobalStore>;\n /**\n * Returns the agent's local store or namespace store casted as T. Recommended when accessing the store from a hook.\n * @param namespace - The namespace of the store to access (optional).\n */\n abstract getStore<T extends StoreLike<any> = {}>(namespace?: string): Store<T> | undefined;\n\n /**\n * Sets the current instructions for the agent.\n * @param instructions - The new instructions as a string.\n */\n abstract setInstructions(instructions: string, scope?: string): void;\n\n /**\n * Returns the
|
|
4
|
+
"sourcesContent": ["import type { AgentOptions, AgentState, ContextRaw, SetOptionsParams } from \"./agent\";\nimport type { DefineMetaData, Fragola, Tool } from \"./fragola\";\nimport type { StoreLike } from \"./types\";\nimport type { Store } from \"./store\";\nimport type { MessagesParser } from \"./stateUtils\";\n\nexport const STOP = Symbol('stop');\nexport abstract class AgentContext<TMetaData extends DefineMetaData<any> = {}, TGlobalStore extends StoreLike<any> = {}, TStore extends StoreLike<any> = {}> {\n /** The current state of the agent. */\n abstract get state(): AgentState<TMetaData>;\n /** The configuration options for the agent. */\n abstract get options(): AgentOptions;\n /** Raw methods for advanced context manipulation */\n abstract get raw(): ContextRaw<TMetaData>;\n /** Acess the agent's default local store. */\n abstract get store(): Store<TStore>;\n /** Live parser helpers bound to the current `state.messages`. */\n abstract get messagesParser(): MessagesParser<TMetaData>;\n /**\n * Add a store that has a namespace. Can be accessed with `getStore` method.\n * @param store - The store to add\n */\n abstract addStore(store: Store<any>): void;\n\n /**\n * Updates the agent's tool list using a callback.\n *\n * @param callback - Function that receives the current tools and returns the updated list.\n *\n * @example\n * agent.context.updateTools(prev => [...prev, newTool]);\n * agent.context.updateTools(prev => prev.filter(tool => tool.name !== \"oldTool\"));\n */\n abstract updateTools(callback: (prev: Tool[]) => Tool[]): void;\n /**\n * Remove a store by its namespace.\n * @param namespace - The namespace of the store to remove\n */\n abstract removeStore(namespace: string): void;\n /** Return the Fragola instance which created this agent */\n abstract get instance(): Fragola<TGlobalStore>;\n /**\n * Returns the agent's local store or namespace store casted as T. Recommended when accessing the store from a hook.\n * @param namespace - The namespace of the store to access (optional).\n */\n abstract getStore<T extends StoreLike<any> = {}>(namespace?: string): Store<T> | undefined;\n\n /** Returns the agent system prompt exactly as sent to the llm. The system prompt is the result of all the instructions scopes merged */\n abstract get systemPrompt(): string;\n\n /**\n * Sets the current instructions for the agent.\n * @param instructions - The new instructions as a string.\n */\n abstract setInstructions(instructions: string, scope?: string): void;\n\n /**\n * Returns the instructions for a given scope.\n * @param scope - The instructions scope, leave empty to get the default scope (optional)\n */\n abstract instructions(scope?: string): string | undefined;\n\n /**\n * Remove the instructions for a given scope. \n * @param scope - The instructions scope to remove\n * @returns a boolean, true = removed, false = scope do not exist\n */\n abstract removeInstructions(scope: string): boolean\n /**\n * Updates the agent's options.\n * **note**: the `name`, `fork` and `messages` properties are ommited\n * @param options - The new options to set, as a SetOptionsParams object.\n */\n abstract setOptions(options: SetOptionsParams): void;\n /** Stop the agent execution */\n abstract stop(): Promise<{[STOP]: true}>;\n /** Stop the agent execution - Sync */\n abstract stopSync(): {[STOP]: true}\n}", "import type { AgentContext } from \"@src/agentContext\";\nimport { STOP } from \"./agentContext\";\nimport type { StoreLike, maybePromise } from \"./types\";\nimport type { DefineMetaData } from \"./fragola\";\nimport type { AgentAfterEventId } from \"./eventAfter\";\nimport type { AgentBeforeEventId } from \"./eventBefore\";\n\nexport type AgentDefaultEventId =\n \"modelInvocation\" | \"toolCall\" | \"aiMessage\" | \"userMessage\";\n\nexport const SKIP_EVENT = Symbol.for('skip_event');\n/**\n * When returned from an event handler, the event will be ignored.\n * \n * @returns An object with the SKIP_EVENT symbol that signals the event system to skip this event\n */\nexport const skip = () => ({[SKIP_EVENT]: true});\nexport const stop = () => ({[STOP]: true});\n\nexport type eventResult<T> = T | ReturnType<typeof skip> | ReturnType<typeof stop>; //TODO: fix stop not imported\n\nexport type AgentEventId = AgentDefaultEventId | AgentAfterEventId | AgentBeforeEventId;\n\nexport type EventDefaultCallback<TMetaData extends DefineMetaData<any>, TGlobalStore extends StoreLike<any>, TStore extends StoreLike<any>> = (\n context: AgentContext<TMetaData, TGlobalStore, TStore>\n) => maybePromise<void>;"],
|
|
5
5
|
"mappings": "AAMO,IAAMA,EAAO,OAAO,MAAM,ECI1B,IAAMC,EAAa,OAAO,IAAI,YAAY,EAMpCC,EAAO,KAAO,CAAC,CAACD,CAAU,EAAG,EAAI,GACjCE,EAAO,KAAO,CAAC,CAACC,CAAI,EAAG,EAAI",
|
|
6
6
|
"names": ["STOP", "SKIP_EVENT", "skip", "stop", "STOP"]
|
|
7
7
|
}
|
package/dist/src/eventAfter.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import type { maybePromise, StoreLike } from "./types";
|
|
2
2
|
import type { AgentDefaultEventId, EventDefaultCallback } from "./event";
|
|
3
3
|
import type { AgentContext } from "./agentContext";
|
|
4
|
-
import type { ChatCompletionAssistantMessageParam, ChatCompletionMessageParam, DefineMetaData, Tool
|
|
4
|
+
import type { ChatCompletionAssistantMessageParam, ChatCompletionMessageParam, DefineMetaData, Tool } from "./fragola";
|
|
5
|
+
import type { ToolCallPayload } from "./eventDefault";
|
|
5
6
|
import type { StepOptions } from "./agent";
|
|
6
7
|
export type AgentAfterEventExclusive = "after:stateUpdate" | "after:step";
|
|
7
8
|
export type AgentAfterEventId = `after:${AgentDefaultEventId}` | AgentAfterEventExclusive;
|
|
8
9
|
export type EventAfterStateUpdate<TMetaData extends DefineMetaData<any>, TGlobalStore extends StoreLike<any>, TStore extends StoreLike<any>> = EventDefaultCallback<TMetaData, TGlobalStore, TStore>;
|
|
9
10
|
export type EventAfterStep<TMetaData extends DefineMetaData<any>, TGlobalStore extends StoreLike<any>, TStore extends StoreLike<any>> = (options: Required<StepOptions>, newMessages: ChatCompletionMessageParam<TMetaData>[], stepsTaken: number, context: AgentContext<TMetaData, TGlobalStore, TStore>) => maybePromise<void>;
|
|
10
11
|
export type EventAfterModelInvocation<TMetaData extends DefineMetaData<any>, TGlobalStore extends StoreLike<any>, TStore extends StoreLike<any>> = (message: ChatCompletionAssistantMessageParam<TMetaData>, context: AgentContext<TMetaData, TGlobalStore, TStore>) => maybePromise<void>;
|
|
11
|
-
export type EventAfterToolCall<TParams = Record<any, any>, TMetaData extends DefineMetaData<any> = {}, TGlobalStore extends StoreLike<any> = {}, TStore extends StoreLike<any> = {}> = (result:
|
|
12
|
+
export type EventAfterToolCall<TParams = Record<any, any>, TMetaData extends DefineMetaData<any> = {}, TGlobalStore extends StoreLike<any> = {}, TStore extends StoreLike<any> = {}> = (result: ToolCallPayload, params: TParams, tool: Tool<any>, context: AgentContext<TMetaData, TGlobalStore, TStore>) => maybePromise<void>;
|
|
12
13
|
export type callbackMap<TMetaData extends DefineMetaData<any>, TGlobalStore extends StoreLike<any>, TStore extends StoreLike<any>> = {
|
|
13
14
|
[K in AgentAfterEventId]: K extends "after:stateUpdate" ? EventAfterStateUpdate<TMetaData, TGlobalStore, TStore> : K extends "after:step" ? EventAfterStep<TMetaData, TGlobalStore, TStore> : K extends "after:modelInvocation" ? EventAfterModelInvocation<TMetaData, TGlobalStore, TStore> : K extends "after:toolCall" ? EventAfterToolCall<any, TMetaData, TGlobalStore, TStore> : never;
|
|
14
15
|
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { AgentDefaultEventId, eventResult } from "./event";
|
|
2
2
|
import type { maybePromise, StoreLike } from "./types";
|
|
3
3
|
import type { AgentContext } from "./agentContext";
|
|
4
|
-
import type { OpenaiClientOptions, DefineMetaData, Tool, ChatCompletionAssistantMessageParam
|
|
4
|
+
import type { OpenaiClientOptions, DefineMetaData, Tool, ChatCompletionAssistantMessageParam } from "./fragola";
|
|
5
|
+
import type { ToolCallPayload } from "./eventDefault";
|
|
5
6
|
import type { CreateAgentOptions, StepOptions } from "./agent";
|
|
6
7
|
import type OpenAI from "openai";
|
|
7
8
|
import type { APIPromise } from "openai";
|
|
@@ -22,7 +23,7 @@ export type EventBeforeModelInvocation<TMetaData extends DefineMetaData<any>, TG
|
|
|
22
23
|
export type ToolCallConfig<TParams = Record<any, any>> = {
|
|
23
24
|
params: TParams;
|
|
24
25
|
} | {
|
|
25
|
-
|
|
26
|
+
injectConfig: ToolCallPayload;
|
|
26
27
|
};
|
|
27
28
|
export type EventBeforeToolCall<TParams = Record<any, any>, TMetaData extends DefineMetaData<any> = {}, TGlobalStore extends StoreLike<any> = {}, TStore extends StoreLike<any> = {}> = (config: ToolCallConfig<TParams>, tool: Tool<any>, context: AgentContext<TMetaData, TGlobalStore, TStore>) => maybePromise<ToolCallConfig<TParams>>;
|
|
28
29
|
export type callbackMap<TMetaData extends DefineMetaData<any>, TGlobalStore extends StoreLike<any>, TStore extends StoreLike<any>> = {
|
|
@@ -40,8 +40,17 @@ export type ModelInvocationPayload<TMetaData extends DefineMetaData<any>> = Mode
|
|
|
40
40
|
kind: "completion";
|
|
41
41
|
data: ChatCompletionAssistantMessageParam<TMetaData>;
|
|
42
42
|
};
|
|
43
|
+
export type ToolCallPayload = {
|
|
44
|
+
success: false;
|
|
45
|
+
isValidationError?: boolean;
|
|
46
|
+
error?: unknown;
|
|
47
|
+
data: ToolHandlerReturnTypeNonAsync;
|
|
48
|
+
} | {
|
|
49
|
+
success: true;
|
|
50
|
+
data: ToolHandlerReturnTypeNonAsync;
|
|
51
|
+
};
|
|
43
52
|
export type EventModelInvocation<TMetaData extends DefineMetaData<any>, TGlobalStore extends StoreLike<any>, TStore extends StoreLike<any>> = (payload: ModelInvocationPayload<TMetaData>, context: AgentContext<TMetaData, TGlobalStore, TStore>) => maybePromise<eventResult<ModelInvocationChunkResult | ChatCompletionAssistantMessageParam<TMetaData>>>;
|
|
44
|
-
export type EventToolCall<TParams = Record<any, any>, TMetaData extends DefineMetaData<any> = {}, TGlobalStore extends StoreLike<any> = {}, TStore extends StoreLike<any> = {}> = (result:
|
|
53
|
+
export type EventToolCall<TParams = Record<any, any>, TMetaData extends DefineMetaData<any> = {}, TGlobalStore extends StoreLike<any> = {}, TStore extends StoreLike<any> = {}> = (result: ToolCallPayload, params: TParams, tool: Tool<any>, context: AgentContext<TMetaData, TGlobalStore, TStore>) => maybePromise<eventResult<ToolCallPayload>>;
|
|
45
54
|
export type EventAiMessage<TMetaData extends DefineMetaData<any>, TGlobalStore extends StoreLike<any> = {}, TStore extends StoreLike<any> = {}> = (message: ChatCompletionAssistantMessageParam<TMetaData>, finish_reason: OpenAI.Chat.Completions.ChatCompletionChunk.Choice['finish_reason'], usage: OpenAI.Chat.Completions.ChatCompletionChunk['usage'], context: AgentContext<TMetaData, TGlobalStore, TStore>) => maybePromise<eventResult<ChatCompletionAssistantMessageParam>>;
|
|
46
55
|
export type EventUserMessage<TMetaData extends DefineMetaData<any>, TGlobalStore extends StoreLike<any> = {}, TStore extends StoreLike<any> = {}> = (message: ChatCompletionUserMessageParam<TMetaData>, context: AgentContext<TMetaData, TGlobalStore, TStore>) => maybePromise<eventResult<ChatCompletionUserMessageParam>>;
|
|
47
56
|
export type callbackMap<TMetaData extends DefineMetaData<any>, TGlobalStore extends StoreLike<any>, TStore extends StoreLike<any>> = {
|
package/dist/src/fragola.d.ts
CHANGED
|
@@ -55,6 +55,34 @@ export interface Tool<T extends z.ZodType<any, any> | string = any> {
|
|
|
55
55
|
*/
|
|
56
56
|
schema?: T;
|
|
57
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Create a tool for your agent using Zod schema or JSON schema.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* // Example: Weather tool using Zod schema
|
|
63
|
+
* import z from "zod";
|
|
64
|
+
* import { tool } from "./fragola";
|
|
65
|
+
*
|
|
66
|
+
* const getWeather = tool({
|
|
67
|
+
* name: "getWeather",
|
|
68
|
+
* description: "Get the current weather for a city.",
|
|
69
|
+
* schema: z.object({ city: z.string() }),
|
|
70
|
+
* handler: async ({ city }) => {
|
|
71
|
+
* // Replace with real API call
|
|
72
|
+
* return { city, temperature: 22, unit: "C" };
|
|
73
|
+
* },
|
|
74
|
+
* });
|
|
75
|
+
*
|
|
76
|
+
* // Example: Weather tool using JSON schema string
|
|
77
|
+
* const getWeatherStringSchema = tool({
|
|
78
|
+
* name: "getWeather",
|
|
79
|
+
* description: "Get the current weather for a city.",
|
|
80
|
+
* schema: '{ "type": "object", "properties": { "city": { "type": "string" } }, "required": ["city"] }',
|
|
81
|
+
* handler: async ({ city }: { city: string }) => {
|
|
82
|
+
* return { city, temperature: 22, unit: "C" };
|
|
83
|
+
* },
|
|
84
|
+
* });
|
|
85
|
+
*/
|
|
58
86
|
export declare const tool: <T extends z.ZodType<any, any> | string>(params: Tool<T>) => Tool<T>;
|
|
59
87
|
export declare function stripMeta<T extends object>(data: (T & {
|
|
60
88
|
meta?: any;
|
|
@@ -103,6 +131,7 @@ export interface FragolaEvents {
|
|
|
103
131
|
export type ClientOptions = OpenaiClientOptions & PreferedModel & {
|
|
104
132
|
events?: FragolaEvents;
|
|
105
133
|
};
|
|
134
|
+
/** Creates a fragola instance */
|
|
106
135
|
export declare class Fragola<TGlobalStore extends StoreLike<any> = {}> {
|
|
107
136
|
#private;
|
|
108
137
|
private clientOptions;
|