@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/utils.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var r=Symbol("stop");var s=Symbol.for("skip_event");var
|
|
1
|
+
var r=Symbol("stop");var s=Symbol.for("skip_event");var g=t=>{t.onModelInvocation(async()=>({role:"assistant",content:""}))};function u(t){return t.constructor.name==="AsyncFunction"}var y=t=>{let{choices:o}=t;if(!o.length&&t.usage)return!1;let e=o[0].finish_reason;return e===null||e==="length"},d=(t,o={})=>{let e=structuredClone(o);if(t.usage||!t.choices||t.choices.length==0)return e;if(t.choices[0].delta?.role?e.role=t.choices[0].delta.role:o.role||(e.role="assistant"),t.choices[0].delta?.content?e.content=(o.content||"")+t.choices[0].delta.content:o.content||(e.content=""),t.choices[0].delta?.tool_calls&&e.role==="assistant"){e.tool_calls||(e.tool_calls=[]);let n=t.choices[0].delta.tool_calls.at(-1);if(n)if(n.id)e.tool_calls.push({id:n.id,type:"function",function:{name:n.function?.name||"",arguments:n.function?.arguments||""}});else{let a=e.tool_calls.at(-1);a&&a.type=="function"&&a.function&&n.function?.arguments&&(a.function={...a.function,arguments:a.function.arguments+n.function.arguments})}}return e},i=t=>typeof t=="object"&&t[s]==!0,S=t=>typeof t=="object"&&t[r]==!0,T=async(t,o)=>i(t)?await o():t;export{u as isAsyncFunction,y as isChunkPartial,i as isSkipEvent,S as isStopEvent,g as noCompletion,T as skipEventFallback,d as streamChunkToMessage};
|
|
2
2
|
//# sourceMappingURL=utils.js.map
|
package/dist/src/utils.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/agentContext.ts", "../../src/event.ts", "../../src/utils.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>;", "import type OpenAI from \"openai\";\nimport { SKIP_EVENT } from \"./event\";\nimport type { FragolaHook } from \"./hook/index\";\nimport { STOP } from \"./agentContext\";\n\nexport const noCompletion: FragolaHook = (agent) => {\n agent.onModelInvocation(async () => ({role: \"assistant\", content: \"\"}))\n}\n\nexport function isAsyncFunction(fn: Function): boolean {\n return fn.constructor.name === 'AsyncFunction';\n}\n\nexport const isChunkPartial = (chunk: OpenAI.Chat.Completions.ChatCompletionChunk): boolean => {\n const { choices } = chunk;\n\n if (!choices.length && chunk.usage)\n return false;\n const finish_reason = choices[0].finish_reason;\n return finish_reason === null || finish_reason === 'length';\n}\n\nexport const streamChunkToMessage = (chunk: OpenAI.Chat.Completions.ChatCompletionChunk, message: Partial<OpenAI.Chat.ChatCompletionMessageParam> = {} as Partial<OpenAI.Chat.ChatCompletionMessageParam>) => {\n const updatedMessage = structuredClone(message);\n if (chunk.usage || !chunk[\"choices\"] || chunk.choices.length == 0)\n return updatedMessage;\n // Handle role if present in delta\n if (chunk.choices[0].delta?.role) {\n updatedMessage.role = chunk.choices[0].delta.role;\n } else if (!message.role)\n updatedMessage.role = \"assistant\";\n\n // Handle content if present in delta\n if (chunk.choices[0].delta?.content) {\n updatedMessage.content = (message.content || '') + chunk.choices[0].delta.content;\n } else if (!message.content)\n updatedMessage.content = \"\";\n\n // Handle tool_calls if present in delta\n if (chunk.choices[0].delta?.tool_calls && updatedMessage.role === \"assistant\") {\n if (!updatedMessage.tool_calls)\n updatedMessage.tool_calls = [];\n const toolCall = chunk.choices[0].delta.tool_calls.at(-1);\n if (toolCall) {\n if (toolCall.id) {\n updatedMessage.tool_calls.push({\n id: toolCall.id,\n type: \"function\",\n function: {\n name: toolCall.function?.name || \"\",\n arguments: toolCall.function?.arguments || \"\"\n },\n })\n } else {\n const lastToolCallRef = updatedMessage.tool_calls.at(-1);\n if (lastToolCallRef && lastToolCallRef.type == \"function\" && lastToolCallRef.function && toolCall.function?.arguments) {\n lastToolCallRef.function = {\n ...lastToolCallRef.function,\n arguments: lastToolCallRef.function.arguments + toolCall.function.arguments\n }\n }\n }\n }\n }\n\n return updatedMessage;\n}\n\nexport const isSkipEvent = (data: any) => {\n return typeof data == \"object\" && (data as any)[SKIP_EVENT] == true\n}\nexport const isStopEvent = (data: any) => {\n return typeof data == \"object\" && (data as any)[STOP] == true\n}\nexport const skipEventFallback = async <T>(data: any, fallback: () => Promise<T>): Promise<T> => {\n if (isSkipEvent(data))\n return await fallback();\n return data as T;\n}"],
|
|
5
5
|
"mappings": "AAMO,IAAMA,EAAO,OAAO,MAAM,ECI1B,IAAMC,EAAa,OAAO,IAAI,YAAY,ECL1C,IAAMC,EAA6BC,GAAU,CAChDA,EAAM,kBAAkB,UAAa,CAAC,KAAM,YAAa,QAAS,EAAE,EAAE,CAC1E,EAEO,SAASC,EAAgBC,EAAuB,CACnD,OAAOA,EAAG,YAAY,OAAS,eACnC,CAEO,IAAMC,EAAkBC,GAAgE,CAC3F,GAAM,CAAE,QAAAC,CAAQ,EAAID,EAEpB,GAAI,CAACC,EAAQ,QAAUD,EAAM,MACzB,MAAO,GACX,IAAME,EAAgBD,EAAQ,CAAC,EAAE,cACjC,OAAOC,IAAkB,MAAQA,IAAkB,QACvD,EAEaC,EAAuB,CAACH,EAAoDI,EAA2D,CAAC,IAAyD,CAC1M,IAAMC,EAAiB,gBAAgBD,CAAO,EAC9C,GAAIJ,EAAM,OAAS,CAACA,EAAM,SAAcA,EAAM,QAAQ,QAAU,EAC5D,OAAOK,EAcX,GAZIL,EAAM,QAAQ,CAAC,EAAE,OAAO,KACxBK,EAAe,KAAOL,EAAM,QAAQ,CAAC,EAAE,MAAM,KACrCI,EAAQ,OAChBC,EAAe,KAAO,aAGtBL,EAAM,QAAQ,CAAC,EAAE,OAAO,QACxBK,EAAe,SAAWD,EAAQ,SAAW,IAAMJ,EAAM,QAAQ,CAAC,EAAE,MAAM,QAClEI,EAAQ,UAChBC,EAAe,QAAU,IAGzBL,EAAM,QAAQ,CAAC,EAAE,OAAO,YAAcK,EAAe,OAAS,YAAa,CACtEA,EAAe,aAChBA,EAAe,WAAa,CAAC,GACjC,IAAMC,EAAWN,EAAM,QAAQ,CAAC,EAAE,MAAM,WAAW,GAAG,EAAE,EACxD,GAAIM,EACA,GAAIA,EAAS,GACTD,EAAe,WAAW,KAAK,CAC3B,GAAIC,EAAS,GACb,KAAM,WACN,SAAU,CACN,KAAMA,EAAS,UAAU,MAAQ,GACjC,UAAWA,EAAS,UAAU,WAAa,EAC/C,CACJ,CAAC,MACE,CACH,IAAMC,EAAkBF,EAAe,WAAW,GAAG,EAAE,EACnDE,GAAoBA,EAAgB,MAAQ,YAAcA,EAAgB,UAAYD,EAAS,UAAU,YACzGC,EAAgB,SAAW,CACvB,GAAGA,EAAgB,SACnB,UAAWA,EAAgB,SAAS,UAAYD,EAAS,SAAS,SACtE,EAER,CAER,CAEA,OAAOD,CACX,EAEaG,EAAeC,GACjB,OAAOA,GAAQ,UAAaA,EAAaC,CAAU,GAAK,GAEtDC,EAAeF,GACjB,OAAOA,GAAQ,UAAaA,EAAaG,CAAI,GAAK,GAEhDC,EAAoB,MAAUJ,EAAWK,IAC9CN,EAAYC,CAAI,EACT,MAAMK,EAAS,EACnBL",
|
|
6
6
|
"names": ["STOP", "SKIP_EVENT", "noCompletion", "agent", "isAsyncFunction", "fn", "isChunkPartial", "chunk", "choices", "finish_reason", "streamChunkToMessage", "message", "updatedMessage", "toolCall", "lastToolCallRef", "isSkipEvent", "data", "SKIP_EVENT", "isStopEvent", "STOP", "skipEventFallback", "fallback"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fragola-ai/agent",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "An event-driven SDK for building AI agents. Interoperable with OpenAI SDK.",
|
|
5
5
|
"main": "./dist/package/fragola.index.js",
|
|
6
6
|
"module": "./dist/package/fragola.index.js",
|
|
@@ -59,6 +59,9 @@
|
|
|
59
59
|
"publishConfig": {
|
|
60
60
|
"access": "public"
|
|
61
61
|
},
|
|
62
|
+
"workspaces": [
|
|
63
|
+
"hook.presets/*"
|
|
64
|
+
],
|
|
62
65
|
"engines": {
|
|
63
66
|
"node": ">=18"
|
|
64
67
|
},
|
|
@@ -72,7 +75,6 @@
|
|
|
72
75
|
"@types/bun": "latest",
|
|
73
76
|
"@types/puppeteer": "^7.0.4",
|
|
74
77
|
"esbuild": "^0.25.9",
|
|
75
|
-
"portkey-ai": "^3.0.1",
|
|
76
78
|
"tsc-alias": "^1.8.10",
|
|
77
79
|
"typescript": "^5.9.2",
|
|
78
80
|
"vitest": "^4.0.1"
|
|
@@ -90,6 +92,7 @@
|
|
|
90
92
|
"json-schema-to-zod": "^2.6.1",
|
|
91
93
|
"nanoid": "^5.1.5",
|
|
92
94
|
"openai": "6.9.0",
|
|
95
|
+
"portkey-ai": "^3.1.0",
|
|
93
96
|
"puppeteer": "^24.27.0",
|
|
94
97
|
"typescript-eslint": "^8.42.0",
|
|
95
98
|
"zod": "^3.25.67"
|
|
@@ -98,6 +101,7 @@
|
|
|
98
101
|
"lint": "bunx eslint .",
|
|
99
102
|
"clean": "rm -rf dist",
|
|
100
103
|
"build": "bun run build.ts",
|
|
104
|
+
"hook-preset": "bun run scripts/create-hook-preset/create.ts",
|
|
101
105
|
"test:build": "bun run build && cd tests && rm -rf node_modules && bun i && bunx vitest --run",
|
|
102
106
|
"test": "cd tests && bunx vitest --run",
|
|
103
107
|
"test:watch": "cd tests && bunx vitest",
|