@@ -8,12 +8,14 @@ import type {
88 Memory ,
99 State ,
1010} from "@elizaos/core" ;
11- import { logger , ModelType , parseToonKeyValue } from "@elizaos/core" ;
11+ import { logger , ModelType } from "@elizaos/core" ;
1212import {
1313 SHOPIFY_SERVICE_TYPE ,
1414 type ShopifyService ,
1515} from "../services/ShopifyService.js" ;
1616import type { Customer } from "../types.js" ;
17+ import { getActionOptions } from "./confirmation.js" ;
18+ import { parseJsonObject } from "./json.js" ;
1719
1820// ---------------------------------------------------------------------------
1921// Helpers
@@ -40,36 +42,49 @@ type CustomerIntent =
4042 | { action : "list" ; query : string | null }
4143 | { action : "search" ; query : string } ;
4244
45+ function readNullableString ( value : unknown ) : string | null {
46+ return typeof value === "string" &&
47+ value . trim ( ) . length > 0 &&
48+ value . trim ( ) . toLowerCase ( ) !== "null"
49+ ? value . trim ( )
50+ : null ;
51+ }
52+
53+ function readCustomerIntent ( options ?: HandlerOptions ) : CustomerIntent | null {
54+ const params = getActionOptions ( options ) ;
55+ const candidate =
56+ params . intent && typeof params . intent === "object"
57+ ? ( params . intent as Record < string , unknown > )
58+ : params ;
59+ const action = candidate . action ;
60+ const query = readNullableString ( candidate . query ) ;
61+ if ( action === "list" ) {
62+ return { action, query } ;
63+ }
64+ if ( action === "search" && query ) {
65+ return { action, query } ;
66+ }
67+ return null ;
68+ }
69+
4370async function classifyIntent (
4471 runtime : IAgentRuntime ,
4572 text : string ,
4673) : Promise < CustomerIntent | null > {
4774 const prompt = `Analyze the user message and determine what customer action they want.
48- Respond with TOON only in one of these shapes:
49- action: list
50- query:
75+ Respond with JSON only in one of these shapes:
76+ {"action":"list","query":null}
5177
52- action: search
53- query: customer name, email, or other search term
78+ {"action":"search","query":"customer name, email, or other search term"}
5479
5580User message: "${ text } "
5681` ;
5782
5883 for ( let i = 0 ; i < 2 ; i ++ ) {
5984 const response = await runtime . useModel ( ModelType . TEXT_SMALL , { prompt } ) ;
60- const parsed = parseToonKeyValue < Record < string , unknown > > ( response ) ;
61- const action = parsed ?. action ;
62- const query =
63- typeof parsed ?. query === "string" &&
64- parsed . query . trim ( ) . length > 0 &&
65- parsed . query . trim ( ) . toLowerCase ( ) !== "null"
66- ? parsed . query . trim ( )
67- : null ;
68- if ( action === "list" ) {
69- return { action, query } ;
70- }
71- if ( action === "search" && query ) {
72- return { action, query } ;
85+ const parsed = parseJsonObject < Record < string , unknown > > ( response ) ;
86+ if ( parsed ?. action ) {
87+ return readCustomerIntent ( parsed as HandlerOptions ) ;
7388 }
7489 }
7590 return null ;
@@ -123,7 +138,7 @@ export const manageCustomersAction: Action = {
123138 runtime : IAgentRuntime ,
124139 message : Memory ,
125140 _state ?: State ,
126- _options ?: HandlerOptions ,
141+ options ?: HandlerOptions ,
127142 callback ?: HandlerCallback ,
128143 ) : Promise < ActionResult | undefined > => {
129144 const svc = runtime . getService < ShopifyService > ( SHOPIFY_SERVICE_TYPE ) ;
@@ -136,7 +151,8 @@ export const manageCustomersAction: Action = {
136151
137152 const text =
138153 typeof message . content ?. text === "string" ? message . content . text : "" ;
139- const intent = await classifyIntent ( runtime , text ) ;
154+ const intent =
155+ readCustomerIntent ( options ) ?? ( await classifyIntent ( runtime , text ) ) ;
140156
141157 if ( ! intent ) {
142158 await callback ?.( {
@@ -191,5 +207,20 @@ export const manageCustomersAction: Action = {
191207 }
192208 } ,
193209
210+ parameters : [
211+ {
212+ name : "action" ,
213+ description : "Customer action. One of: list, search." ,
214+ required : false ,
215+ schema : { type : "string" as const , enum : [ "list" , "search" ] } ,
216+ } ,
217+ {
218+ name : "query" ,
219+ description : "Customer name, email, or other Shopify customer search term." ,
220+ required : false ,
221+ schema : { type : "string" as const } ,
222+ } ,
223+ ] ,
224+
194225 examples,
195226} ;
0 commit comments