field-service-agent
Version:
Field Service AI Agent - NPM package for integrating AI-powered field service management into mobile apps
153 lines • 6.97 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
function isFollowUpQuestion(message) {
return message.trim().endsWith('?');
}
export class EnhancedAgent {
constructor(config) {
this.context = null;
this.sdk = config.sdk;
this.currentScreenType = config.currentScreenType;
this.currentRecordId = config.currentRecordId;
this.debugMode = config.debugMode || false;
this.chatHistory = config.chatHistory || [];
this.language = config.language || 'en-US';
this.voiceName = config.voiceName;
}
setCurrentScreen(screenType, recordId) {
console.log('setting current screen', screenType, recordId);
const screenChanged = this.currentScreenType !== screenType || this.currentRecordId !== recordId;
this.currentScreenType = screenType;
this.currentRecordId = recordId;
if (screenChanged && this.context) {
console.log('Screen changed, clearing context cache');
}
}
setCurrentRecordId(recordId) {
const recordChanged = this.currentRecordId !== recordId;
this.currentRecordId = recordId;
if (recordChanged && this.context) {
console.log('Record changed, clearing context cache');
}
}
setLanguage(language) {
this.language = language;
}
setVoiceName(voiceName) {
this.voiceName = voiceName;
}
refreshContext() {
return __awaiter(this, arguments, void 0, function* (forceRefresh = false) {
console.log('refreshContext - fetching fresh data');
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const context = {
currentScreenType: this.currentScreenType,
currentRecordId: this.currentRecordId,
timeZone
};
this.context = context;
console.log('refreshContext context', context);
return context;
});
}
processCommand(input) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
const context = yield this.refreshContext();
if (!context) {
throw new Error('Failed to get current context');
}
const messages = [
...this.chatHistory.map(msg => ({
role: msg.isUser ? 'user' : 'assistant',
content: msg.text
}))
];
console.log('agent sending to api voice', this.voiceName);
console.log('agent sending to api language', this.language);
console.log('agent sending to api screen', this.currentScreenType);
console.log('agent sending to api record', this.currentRecordId);
console.log('agent sending to api chat history', this.chatHistory);
let response;
if (typeof input === 'string') {
response = yield this.sdk.vertexChatCompletion({
model: "google/gemini-2.0-flash-001",
messages: messages,
returnVoice: true,
voiceName: this.voiceName,
languageCode: this.language,
currentScreenType: this.currentScreenType,
currentRecordId: this.currentRecordId
});
}
else if (typeof input === 'object' && 'audioContent' in input) {
const { audioContent, inputEncoding = 'WEBM_OPUS', inputSampleRate = 48000 } = input;
response = yield this.sdk.vertexChatCompletion({
model: "google/gemini-2.0-flash-001",
messages: messages,
returnVoice: true,
voiceName: this.voiceName,
languageCode: this.language,
audioContent: audioContent,
inputEncoding: inputEncoding,
inputSampleRate: inputSampleRate,
currentScreenType: this.currentScreenType,
currentRecordId: this.currentRecordId
});
}
console.log('[agent] response ---', response);
return {
navigationActions: ((_a = response.data.vertexChatCompletion.result) === null || _a === void 0 ? void 0 : _a.navigationActions) || [],
success: true,
message: ((_b = response.data.vertexChatCompletion.result) === null || _b === void 0 ? void 0 : _b.content) || '',
audioContent: ((_c = response.data.vertexChatCompletion.result) === null || _c === void 0 ? void 0 : _c.audioContent) || '',
followUpQuestion: isFollowUpQuestion(((_d = response.data.vertexChatCompletion.result) === null || _d === void 0 ? void 0 : _d.content) || ''),
transcribedText: this.currentTranscribedText,
};
});
}
catch(error) {
if (error instanceof Error) {
if (error.message.includes('network')) {
return {
success: false,
message: 'Network error while processing command. Please check your internet connection.',
audioContent: '',
transcribedText: this.currentTranscribedText,
navigationActions: []
};
}
if (error.message.includes('timeout')) {
return {
success: false,
message: 'Request timed out while processing command. Please try again.',
audioContent: '',
transcribedText: this.currentTranscribedText,
navigationActions: []
};
}
}
return {
success: false,
message: error instanceof Error ? error.message : 'An unexpected error occurred',
audioContent: '',
transcribedText: this.currentTranscribedText,
navigationActions: []
};
}
updateChatHistory(history) {
this.chatHistory = history;
}
updateSDK(sdk) {
console.log('[EnhancedAgent] Updating SDK instance');
this.sdk = sdk;
}
}
//# sourceMappingURL=enhancedAgent.js.map