gestalt-sdk 0.0.1-alpha.26

Rust SDK scaffolding and generated protocol bindings for Gestalt executable providers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Typed workflow-definition authoring helpers.
#![allow(missing_docs)]

use std::collections::BTreeMap;

use serde_json::{Map, Value};

use crate::agent::AgentOutput;
use crate::app::AgentToolRef;
use crate::workflow::{
    BoundWorkflowTarget, WorkflowActivation, WorkflowActivationTrigger, WorkflowAgentMessage,
    WorkflowDefinitionSpec, WorkflowEventActivation, WorkflowEventMatch,
    WorkflowScheduleActivation, WorkflowStep, WorkflowStepAction, WorkflowStepAgentTurn,
    WorkflowStepAppCall, WorkflowStepWhen, WorkflowText, WorkflowValue, WorkflowValueKind,
};

type WorkflowMap = BTreeMap<String, WorkflowValue>;
type WorkflowMapFn = Box<dyn Fn() -> WorkflowMap + Send + Sync>;

fn value(kind: WorkflowValueKind) -> WorkflowValue {
    WorkflowValue { kind: Some(kind) }
}

/// References a run-input path.
pub fn input(path: impl Into<String>) -> WorkflowValue {
    value(WorkflowValueKind::Input(
        crate::workflow::WorkflowPathSource { path: path.into() },
    ))
}

/// References an activation signal path.
pub fn signal(path: impl Into<String>) -> WorkflowValue {
    value(WorkflowValueKind::Signal(
        crate::workflow::WorkflowPathSource { path: path.into() },
    ))
}

/// References a prior step output path.
pub fn step_output(step_id: impl Into<String>, path: impl Into<String>) -> WorkflowValue {
    value(WorkflowValueKind::StepOutput(
        crate::workflow::WorkflowStepOutputSource {
            step_id: step_id.into(),
            path: path.into(),
        },
    ))
}

/// References a prior step input path.
pub fn step_input(step_id: impl Into<String>, path: impl Into<String>) -> WorkflowValue {
    value(WorkflowValueKind::StepInput(
        crate::workflow::WorkflowStepInputSource {
            step_id: step_id.into(),
            path: path.into(),
        },
    ))
}

/// Builds a literal workflow value.
pub fn literal(literal: Value) -> crate::Result<WorkflowValue> {
    Ok(value(WorkflowValueKind::Literal(literal)))
}

/// Builds a workflow template value.
pub fn template(template: impl Into<String>) -> WorkflowValue {
    value(WorkflowValueKind::Template(WorkflowText {
        template: template.into(),
    }))
}

/// Builds an object workflow value.
pub fn object(fields: WorkflowMap) -> WorkflowValue {
    value(WorkflowValueKind::Object(crate::workflow::WorkflowObject {
        fields,
    }))
}

/// Builds an array workflow value.
pub fn array(values: Vec<WorkflowValue>) -> WorkflowValue {
    value(WorkflowValueKind::Array(crate::workflow::WorkflowArray {
        values,
    }))
}

#[derive(Clone, Debug)]
pub struct WorkflowBuilder {
    id: String,
    run_as: String,
    paused: bool,
    activations: Vec<WorkflowActivation>,
    steps: Vec<WorkflowStep>,
}

/// Starts a fluent workflow definition builder.
pub fn define_workflow(
    id: impl Into<String>,
    run_as: impl Into<String>,
) -> crate::Result<WorkflowBuilder> {
    let id = id.into();
    let run_as = run_as.into();
    if id.trim().is_empty() {
        return Err(crate::Error::bad_request("define_workflow requires id"));
    }
    if run_as.trim().is_empty() {
        return Err(crate::Error::bad_request("define_workflow requires run_as"));
    }
    Ok(WorkflowBuilder {
        id,
        run_as,
        paused: false,
        activations: Vec::new(),
        steps: Vec::new(),
    })
}

impl WorkflowBuilder {
    /// Sets whether the definition starts paused.
    pub fn paused(mut self, paused: bool) -> Self {
        self.paused = paused;
        self
    }

    pub fn on(mut self, activation: WorkflowActivationConfig) -> Self {
        let (id, paused, trigger, input) = match activation.kind {
            WorkflowActivationKind::Event {
                type_name,
                map_input,
            } => (
                type_name.clone(),
                false,
                WorkflowActivationTrigger::Event(WorkflowEventActivation {
                    r#match: Some(WorkflowEventMatch {
                        r#type: type_name,
                        ..Default::default()
                    }),
                }),
                Some(object(map_input())),
            ),
            WorkflowActivationKind::Schedule { cron, map_input } => (
                cron.clone(),
                false,
                WorkflowActivationTrigger::Schedule(WorkflowScheduleActivation {
                    cron,
                    ..Default::default()
                }),
                Some(object(map_input())),
            ),
        };
        self.activations.push(WorkflowActivation {
            id,
            paused,
            trigger: Some(trigger),
            input,
        });
        self
    }

    pub fn step(mut self, step_id: impl Into<String>, config: WorkflowStepConfig) -> Self {
        assert!(
            !(config.app.is_some() && config.agent.is_some()),
            "workflow step cannot configure both app and agent actions"
        );
        let mut step = WorkflowStep {
            id: step_id.into(),
            ..Default::default()
        };
        if let Some(inputs) = config.inputs {
            step.inputs = inputs();
        }
        if let Some(app) = config.app {
            step.action = Some(WorkflowStepAction::App(WorkflowStepAppCall {
                name: app.name,
                operation: app.operation,
                input: app.input.map(|callback| object(callback())),
                connection: app.connection,
                instance: app.instance,
                credential_mode: app.credential_mode,
            }));
        }
        if let Some(agent) = config.agent {
            step.action = Some(WorkflowStepAction::Agent(WorkflowStepAgentTurn {
                provider: agent.provider,
                model: agent.model,
                session_key: agent.session_key,
                prompt: Some(agent.prompt),
                messages: agent
                    .messages
                    .into_iter()
                    .map(|message| WorkflowAgentMessage {
                        role: message.role,
                        text: Some(WorkflowText {
                            template: message.text,
                        }),
                        ..Default::default()
                    })
                    .collect(),
                tools: agent.tools,
                output: agent.output,
                model_options: agent.model_options,
            }));
        }
        step.when = config.when.map(|when| WorkflowStepWhen {
            value: Some(when.value),
            equals: when.equals,
        });
        step.timeout_seconds = config.timeout_seconds;
        step.metadata = config.metadata;
        self.steps.push(step);
        self
    }

    /// Consumes the builder and returns the generated workflow definition spec.
    pub fn to_spec(self) -> WorkflowDefinitionSpec {
        WorkflowDefinitionSpec {
            id: self.id,
            run_as: self.run_as,
            paused: self.paused,
            activations: self.activations,
            target: (!self.steps.is_empty()).then_some(BoundWorkflowTarget { steps: self.steps }),
        }
    }
}

pub struct WorkflowActivationConfig {
    kind: WorkflowActivationKind,
}

enum WorkflowActivationKind {
    Event {
        type_name: String,
        map_input: WorkflowMapFn,
    },
    Schedule {
        cron: String,
        map_input: WorkflowMapFn,
    },
}

/// Creates an event activation for [`WorkflowBuilder::on`].
pub fn event<F>(type_name: impl Into<String>, map_input: F) -> WorkflowActivationConfig
where
    F: Fn() -> BTreeMap<String, WorkflowValue> + Send + Sync + 'static,
{
    WorkflowActivationConfig {
        kind: WorkflowActivationKind::Event {
            type_name: type_name.into(),
            map_input: Box::new(map_input),
        },
    }
}

/// Creates a schedule activation for [`WorkflowBuilder::on`].
pub fn schedule<F>(cron: impl Into<String>, map_input: F) -> WorkflowActivationConfig
where
    F: Fn() -> BTreeMap<String, WorkflowValue> + Send + Sync + 'static,
{
    WorkflowActivationConfig {
        kind: WorkflowActivationKind::Schedule {
            cron: cron.into(),
            map_input: Box::new(map_input),
        },
    }
}

pub struct WorkflowStepAppConfig {
    pub name: String,
    pub operation: String,
    input: Option<WorkflowMapFn>,
    pub connection: String,
    pub instance: String,
    pub credential_mode: String,
}

impl WorkflowStepAppConfig {
    /// Creates an app step configuration. Add mapped input with [`Self::with_input`].
    pub fn new(name: impl Into<String>, operation: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            operation: operation.into(),
            input: None,
            connection: String::new(),
            instance: String::new(),
            credential_mode: String::new(),
        }
    }

    /// Sets a zero-argument input mapper; the closure is boxed by the SDK.
    pub fn with_input<F>(mut self, input: F) -> Self
    where
        F: Fn() -> BTreeMap<String, WorkflowValue> + Send + Sync + 'static,
    {
        self.input = Some(Box::new(input));
        self
    }
}

impl Default for WorkflowStepAppConfig {
    fn default() -> Self {
        Self::new("", "")
    }
}

impl WorkflowStepAppConfig {
    pub fn with_connection(mut self, connection: impl Into<String>) -> Self {
        self.connection = connection.into();
        self
    }

    pub fn with_instance(mut self, instance: impl Into<String>) -> Self {
        self.instance = instance.into();
        self
    }

    pub fn with_credential_mode(mut self, credential_mode: impl Into<String>) -> Self {
        self.credential_mode = credential_mode.into();
        self
    }
}

#[derive(Clone)]
pub struct WorkflowStepAgentMessageConfig {
    pub role: String,
    pub text: String,
}

#[derive(Clone, Default)]
pub struct WorkflowStepAgentConfig {
    pub provider: String,
    pub model: String,
    pub session_key: String,
    pub prompt: WorkflowText,
    pub messages: Vec<WorkflowStepAgentMessageConfig>,
    pub tools: Vec<AgentToolRef>,
    pub output: Option<AgentOutput>,
    pub model_options: Option<Map<String, Value>>,
}

impl WorkflowStepAgentConfig {
    pub fn new(provider: impl Into<String>) -> Self {
        Self {
            provider: provider.into(),
            ..Default::default()
        }
    }

    pub fn with_prompt(mut self, prompt: WorkflowText) -> Self {
        self.prompt = prompt;
        self
    }
}

#[derive(Clone)]
pub struct WorkflowStepWhenConfig {
    pub value: WorkflowValue,
    pub equals: Option<Value>,
}

#[derive(Default)]
pub struct WorkflowStepConfig {
    inputs: Option<WorkflowMapFn>,
    pub app: Option<WorkflowStepAppConfig>,
    pub agent: Option<WorkflowStepAgentConfig>,
    pub when: Option<WorkflowStepWhenConfig>,
    pub timeout_seconds: i32,
    pub metadata: Option<Map<String, Value>>,
}

impl WorkflowStepConfig {
    pub fn with_inputs<F>(mut self, inputs: F) -> Self
    where
        F: Fn() -> BTreeMap<String, WorkflowValue> + Send + Sync + 'static,
    {
        self.inputs = Some(Box::new(inputs));
        self
    }
}

fn value_placeholder(value: &WorkflowValue) -> crate::Result<String> {
    let result = match value.kind.as_ref() {
        Some(WorkflowValueKind::Input(source)) => format!("${{{{ input.{} }}}}", source.path),
        Some(WorkflowValueKind::Signal(source)) => format!("${{{{ signal.{} }}}}", source.path),
        Some(WorkflowValueKind::StepOutput(source)) => {
            format!(
                "${{{{ steps.{}.outputs.{} }}}}",
                source.step_id, source.path
            )
        }
        Some(WorkflowValueKind::StepInput(source)) => {
            format!("${{{{ steps.{}.inputs.{} }}}}", source.step_id, source.path)
        }
        _ => {
            return Err(crate::Error::bad_request(
                "text references must be path values",
            ));
        }
    };
    Ok(result)
}

/// Composes workflow prompt text from workflow value references.
pub fn text(parts: &[WorkflowValue]) -> crate::Result<WorkflowText> {
    let mut template = String::new();
    for part in parts {
        template.push_str(&value_placeholder(part)?);
    }
    Ok(WorkflowText { template })
}

pub enum WorkflowDefinitionSpecOrBuilder {
    Spec(WorkflowDefinitionSpec),
    Builder(WorkflowBuilder),
}

impl From<WorkflowBuilder> for WorkflowDefinitionSpecOrBuilder {
    fn from(builder: WorkflowBuilder) -> Self {
        Self::Builder(builder)
    }
}

impl From<WorkflowDefinitionSpec> for WorkflowDefinitionSpecOrBuilder {
    fn from(spec: WorkflowDefinitionSpec) -> Self {
        Self::Spec(spec)
    }
}

impl WorkflowDefinitionSpecOrBuilder {
    fn into_spec(self) -> WorkflowDefinitionSpec {
        match self {
            Self::Spec(spec) => spec,
            Self::Builder(builder) => builder.to_spec(),
        }
    }
}

/// Applies a workflow definition from a raw spec or an authored builder.
pub async fn apply_workflow_definition(
    workflow: &mut crate::workflow::Workflow,
    provider: String,
    idempotency_key: String,
    spec: Option<impl Into<WorkflowDefinitionSpecOrBuilder>>,
) -> Result<crate::workflow::WorkflowDefinition, crate::rpc_support::GestaltError> {
    workflow
        .apply_definition(
            provider,
            idempotency_key,
            spec.map(|value| value.into().into_spec()),
        )
        .await
}