feat: simplify SOP to immediate-effect configuration
This commit is contained in:
121
sdk/dist/index.d.ts
vendored
Normal file
121
sdk/dist/index.d.ts
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
export type ScenarioInput = Record<string, unknown>;
|
||||
export type OutputField = {
|
||||
key: string;
|
||||
name: string;
|
||||
type: string;
|
||||
source: string;
|
||||
value: unknown;
|
||||
};
|
||||
export type ResultField = {
|
||||
key: string;
|
||||
name: string;
|
||||
type: string;
|
||||
required?: boolean;
|
||||
options?: unknown[];
|
||||
fields?: ResultField[];
|
||||
items?: Omit<ResultField, 'key' | 'name'>;
|
||||
};
|
||||
export type ResultSchema = {
|
||||
fields: ResultField[];
|
||||
};
|
||||
export type NodeField = {
|
||||
key: string;
|
||||
name: string;
|
||||
type: string;
|
||||
required: boolean;
|
||||
options: unknown[];
|
||||
validation: Record<string, unknown>;
|
||||
};
|
||||
export type PresentationField = {
|
||||
key: string;
|
||||
label: string;
|
||||
kind: 'text' | 'image';
|
||||
value: unknown;
|
||||
};
|
||||
export type NodePresentation = {
|
||||
summary: PresentationField[];
|
||||
items: Array<{
|
||||
fields: PresentationField[];
|
||||
}>;
|
||||
opening?: {
|
||||
title: string;
|
||||
content: string;
|
||||
};
|
||||
};
|
||||
export type KnowledgeCollectionOption = {
|
||||
value: string;
|
||||
label: string;
|
||||
parents?: string[];
|
||||
suggested?: boolean;
|
||||
};
|
||||
export type KnowledgeCollectionStep = {
|
||||
field_key: string;
|
||||
name: string;
|
||||
required: boolean;
|
||||
multiple: boolean;
|
||||
from_field?: string;
|
||||
options: KnowledgeCollectionOption[];
|
||||
};
|
||||
export type KnowledgeCollection = {
|
||||
context_fields: NodeField[];
|
||||
context_title?: string;
|
||||
context_hint?: string;
|
||||
selection_title?: string;
|
||||
selection_hint?: string;
|
||||
steps: KnowledgeCollectionStep[];
|
||||
};
|
||||
export type NodeView = {
|
||||
node_key: string;
|
||||
type: string;
|
||||
title: string;
|
||||
content: string;
|
||||
fields?: NodeField[];
|
||||
presentation?: NodePresentation;
|
||||
outputs?: unknown[];
|
||||
collection?: KnowledgeCollection;
|
||||
};
|
||||
export type ScenarioState = {
|
||||
run_id: number;
|
||||
external_ref: string;
|
||||
status: string;
|
||||
final_result?: Record<string, unknown> | null;
|
||||
result_schema: ResultSchema;
|
||||
session_token?: string;
|
||||
node: NodeView;
|
||||
outputs: OutputField[];
|
||||
};
|
||||
export type SDKEvent = 'ready' | 'node_change' | 'form_submit' | 'finish' | 'error';
|
||||
export type CreateOptions = {
|
||||
baseURL?: string;
|
||||
publicKey: string;
|
||||
scenarioKey: string;
|
||||
sopId?: number;
|
||||
input?: ScenarioInput;
|
||||
initialValues?: ScenarioInput;
|
||||
externalRef?: string;
|
||||
};
|
||||
export type FinishOptions = {
|
||||
result?: string;
|
||||
finalResult?: Record<string, unknown> | null;
|
||||
};
|
||||
export declare class SalesScenario {
|
||||
private readonly options;
|
||||
private token;
|
||||
private state?;
|
||||
private handlers;
|
||||
constructor(options: CreateOptions);
|
||||
start(): Promise<ScenarioState>;
|
||||
getState(): ScenarioState;
|
||||
getCurrentNode(): Promise<ScenarioState>;
|
||||
submit(values: ScenarioInput): Promise<ScenarioState>;
|
||||
next(): Promise<ScenarioState>;
|
||||
finish(options?: FinishOptions): Promise<ScenarioState>;
|
||||
reset(): Promise<ScenarioState>;
|
||||
destroy(): void;
|
||||
on(event: SDKEvent, handler: (payload: unknown) => void): () => boolean;
|
||||
private update;
|
||||
private setState;
|
||||
private emit;
|
||||
private request;
|
||||
}
|
||||
export declare function create(options: CreateOptions): SalesScenario;
|
||||
104
sdk/dist/index.iife.js
vendored
Normal file
104
sdk/dist/index.iife.js
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
"use strict";
|
||||
var IqudooSalesScenario = (() => {
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||
var __export = (target, all) => {
|
||||
for (var name in all)
|
||||
__defProp(target, name, { get: all[name], enumerable: true });
|
||||
};
|
||||
var __copyProps = (to, from, except, desc) => {
|
||||
if (from && typeof from === "object" || typeof from === "function") {
|
||||
for (let key of __getOwnPropNames(from))
|
||||
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||
}
|
||||
return to;
|
||||
};
|
||||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||
|
||||
// src/index.ts
|
||||
var index_exports = {};
|
||||
__export(index_exports, {
|
||||
SalesScenario: () => SalesScenario,
|
||||
create: () => create
|
||||
});
|
||||
var SalesScenario = class {
|
||||
constructor(options) {
|
||||
this.options = options;
|
||||
}
|
||||
options;
|
||||
token = "";
|
||||
state;
|
||||
handlers = /* @__PURE__ */ new Map();
|
||||
async start() {
|
||||
const state = await this.request(`/public/scenarios/${encodeURIComponent(this.options.scenarioKey)}/runs`, { method: "POST", body: JSON.stringify({ public_key: this.options.publicKey, sop_id: this.options.sopId, input: this.options.input || {}, initial_values: this.options.initialValues || {}, external_ref: this.options.externalRef || "" }) });
|
||||
if (state.session_token) this.token = state.session_token;
|
||||
this.setState(state);
|
||||
this.emit("ready", state);
|
||||
return state;
|
||||
}
|
||||
getState() {
|
||||
if (!this.state) throw new Error("SDK has not started");
|
||||
return this.state;
|
||||
}
|
||||
async getCurrentNode() {
|
||||
return this.update(`/public/runs/${this.getState().run_id}/current`);
|
||||
}
|
||||
async submit(values) {
|
||||
const state = await this.update(`/public/runs/${this.getState().run_id}/submit`, { method: "POST", body: JSON.stringify({ node_key: this.getState().node.node_key, answers: values }) });
|
||||
this.emit("form_submit", { values, state });
|
||||
return state;
|
||||
}
|
||||
async next() {
|
||||
return this.update(`/public/runs/${this.getState().run_id}/next`, { method: "POST" });
|
||||
}
|
||||
async finish(options = {}) {
|
||||
const state = await this.update(`/public/runs/${this.getState().run_id}/finish`, { method: "POST", body: JSON.stringify({ result: options.result || "", final_result: options.finalResult ?? null }) });
|
||||
this.emit("finish", state);
|
||||
return state;
|
||||
}
|
||||
async reset() {
|
||||
return this.update(`/public/runs/${this.getState().run_id}/reset`, { method: "POST" });
|
||||
}
|
||||
destroy() {
|
||||
this.handlers.clear();
|
||||
this.token = "";
|
||||
this.state = void 0;
|
||||
}
|
||||
on(event, handler) {
|
||||
const handlers = this.handlers.get(event) || /* @__PURE__ */ new Set();
|
||||
handlers.add(handler);
|
||||
this.handlers.set(event, handlers);
|
||||
return () => handlers.delete(handler);
|
||||
}
|
||||
async update(path, init = {}) {
|
||||
const state = await this.request(path, init);
|
||||
this.setState(state);
|
||||
return state;
|
||||
}
|
||||
setState(state) {
|
||||
this.state = state;
|
||||
this.emit("node_change", state.node);
|
||||
}
|
||||
emit(event, payload) {
|
||||
this.handlers.get(event)?.forEach((handler) => handler(payload));
|
||||
}
|
||||
async request(path, init) {
|
||||
try {
|
||||
const response = await fetch(`${(this.options.baseURL || "").replace(/\/$/, "")}${path}`, { ...init, headers: { "Content-Type": "application/json", ...this.token ? { Authorization: `Bearer ${this.token}` } : {} } });
|
||||
const body = await response.json();
|
||||
if (!response.ok) throw new Error(body.message || `Request failed: ${response.status}`);
|
||||
return body.data || body;
|
||||
} catch (error) {
|
||||
this.emit("error", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
function create(options) {
|
||||
return new SalesScenario(options);
|
||||
}
|
||||
return __toCommonJS(index_exports);
|
||||
})();
|
||||
35
sdk/dist/index.js
vendored
Normal file
35
sdk/dist/index.js
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
export class SalesScenario {
|
||||
options;
|
||||
token = '';
|
||||
state;
|
||||
handlers = new Map();
|
||||
constructor(options) {
|
||||
this.options = options;
|
||||
}
|
||||
async start() { const state = await this.request(`/public/scenarios/${encodeURIComponent(this.options.scenarioKey)}/runs`, { method: 'POST', body: JSON.stringify({ public_key: this.options.publicKey, sop_id: this.options.sopId, input: this.options.input || {}, initial_values: this.options.initialValues || {}, external_ref: this.options.externalRef || '' }) }); if (state.session_token)
|
||||
this.token = state.session_token; this.setState(state); this.emit('ready', state); return state; }
|
||||
getState() { if (!this.state)
|
||||
throw new Error('SDK has not started'); return this.state; }
|
||||
async getCurrentNode() { return this.update(`/public/runs/${this.getState().run_id}/current`); }
|
||||
async submit(values) { const state = await this.update(`/public/runs/${this.getState().run_id}/submit`, { method: 'POST', body: JSON.stringify({ node_key: this.getState().node.node_key, answers: values }) }); this.emit('form_submit', { values, state }); return state; }
|
||||
async next() { return this.update(`/public/runs/${this.getState().run_id}/next`, { method: 'POST' }); }
|
||||
async finish(options = {}) { const state = await this.update(`/public/runs/${this.getState().run_id}/finish`, { method: 'POST', body: JSON.stringify({ result: options.result || '', final_result: options.finalResult ?? null }) }); this.emit('finish', state); return state; }
|
||||
async reset() { return this.update(`/public/runs/${this.getState().run_id}/reset`, { method: 'POST' }); }
|
||||
destroy() { this.handlers.clear(); this.token = ''; this.state = undefined; }
|
||||
on(event, handler) { const handlers = this.handlers.get(event) || new Set(); handlers.add(handler); this.handlers.set(event, handlers); return () => handlers.delete(handler); }
|
||||
async update(path, init = {}) { const state = await this.request(path, init); this.setState(state); return state; }
|
||||
setState(state) { this.state = state; this.emit('node_change', state.node); }
|
||||
emit(event, payload) { this.handlers.get(event)?.forEach(handler => handler(payload)); }
|
||||
async request(path, init) { try {
|
||||
const response = await fetch(`${(this.options.baseURL || '').replace(/\/$/, '')}${path}`, { ...init, headers: { 'Content-Type': 'application/json', ...(this.token ? { Authorization: `Bearer ${this.token}` } : {}) } });
|
||||
const body = await response.json();
|
||||
if (!response.ok)
|
||||
throw new Error(body.message || `Request failed: ${response.status}`);
|
||||
return (body.data || body);
|
||||
}
|
||||
catch (error) {
|
||||
this.emit('error', error);
|
||||
throw error;
|
||||
} }
|
||||
}
|
||||
export function create(options) { return new SalesScenario(options); }
|
||||
Reference in New Issue
Block a user