@moxxy/sdk 0.1.0 → 0.1.3
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/README.md +239 -0
- package/dist/compactor-helpers.d.ts +4 -1
- package/dist/compactor-helpers.d.ts.map +1 -1
- package/dist/compactor-helpers.js +50 -21
- package/dist/compactor-helpers.js.map +1 -1
- package/dist/define.d.ts +4 -0
- package/dist/define.d.ts.map +1 -1
- package/dist/define.js +6 -0
- package/dist/define.js.map +1 -1
- package/dist/embedding.d.ts +17 -0
- package/dist/embedding.d.ts.map +1 -1
- package/dist/errors.d.ts +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/fs-utils.d.ts +27 -0
- package/dist/fs-utils.d.ts.map +1 -0
- package/dist/fs-utils.js +44 -0
- package/dist/fs-utils.js.map +1 -0
- package/dist/http-utils.d.ts +16 -0
- package/dist/http-utils.d.ts.map +1 -0
- package/dist/http-utils.js +40 -0
- package/dist/http-utils.js.map +1 -0
- package/dist/index.d.ts +12 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -4
- package/dist/index.js.map +1 -1
- package/dist/mode-helpers.d.ts +58 -0
- package/dist/mode-helpers.d.ts.map +1 -1
- package/dist/mode-helpers.js +119 -0
- package/dist/mode-helpers.js.map +1 -1
- package/dist/mode.d.ts +8 -0
- package/dist/mode.d.ts.map +1 -1
- package/dist/mutex.d.ts +15 -0
- package/dist/mutex.d.ts.map +1 -0
- package/dist/mutex.js +15 -0
- package/dist/mutex.js.map +1 -0
- package/dist/plugin.d.ts +27 -1
- package/dist/plugin.d.ts.map +1 -1
- package/dist/provider-utils.d.ts +6 -0
- package/dist/provider-utils.d.ts.map +1 -1
- package/dist/provider-utils.js +8 -0
- package/dist/provider-utils.js.map +1 -1
- package/dist/resolvers.d.ts +52 -0
- package/dist/resolvers.d.ts.map +1 -0
- package/dist/resolvers.js +123 -0
- package/dist/resolvers.js.map +1 -0
- package/dist/session-like.d.ts +85 -1
- package/dist/session-like.d.ts.map +1 -1
- package/dist/voice.d.ts +36 -0
- package/dist/voice.d.ts.map +1 -0
- package/dist/voice.js +82 -0
- package/dist/voice.js.map +1 -0
- package/dist/workflow.d.ts +144 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +14 -0
- package/dist/workflow.js.map +1 -0
- package/package.json +38 -2
- package/src/compactor-helpers.test.ts +41 -0
- package/src/compactor-helpers.ts +53 -19
- package/src/define.ts +10 -0
- package/src/embedding.ts +18 -0
- package/src/errors.ts +3 -0
- package/src/fs-utils.test.ts +61 -0
- package/src/fs-utils.ts +55 -0
- package/src/http-utils.test.ts +36 -0
- package/src/http-utils.ts +40 -0
- package/src/index.ts +58 -3
- package/src/mode-helpers.ts +169 -0
- package/src/mode.ts +8 -0
- package/src/mutex.test.ts +36 -0
- package/src/mutex.ts +31 -0
- package/src/plugin.ts +27 -1
- package/src/provider-utils.ts +9 -0
- package/src/resolvers.test.ts +45 -0
- package/src/resolvers.ts +164 -0
- package/src/session-like.ts +87 -1
- package/src/stuck-loop.test.ts +42 -0
- package/src/voice.ts +103 -0
- package/src/workflow.ts +165 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createStuckLoopDetector } from './mode-helpers.js';
|
|
3
|
+
|
|
4
|
+
describe('createStuckLoopDetector', () => {
|
|
5
|
+
it('trips on exact-input repeats at repeatThreshold', () => {
|
|
6
|
+
const d = createStuckLoopDetector(); // repeatThreshold 3
|
|
7
|
+
const input = { x: 1 };
|
|
8
|
+
expect(d.record('Read', input).stuck).toBe(false);
|
|
9
|
+
expect(d.record('Read', input).stuck).toBe(false);
|
|
10
|
+
const sig = d.record('Read', input);
|
|
11
|
+
expect(sig).toMatchObject({ stuck: true, count: 3, kind: 'exact' });
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('trips on same-target near-dups even when volatile args vary', () => {
|
|
15
|
+
const d = createStuckLoopDetector(); // nearThreshold 5
|
|
16
|
+
const url = 'https://example.com/big';
|
|
17
|
+
// Same url, different maxBytes each time — exact check never fires.
|
|
18
|
+
for (let i = 0; i < 4; i++) {
|
|
19
|
+
expect(d.record('web_fetch', { url, maxBytes: 1000 * (i + 1) }).stuck).toBe(false);
|
|
20
|
+
}
|
|
21
|
+
const sig = d.record('web_fetch', { url, maxBytes: 99_000 });
|
|
22
|
+
expect(sig).toMatchObject({ stuck: true, kind: 'near' });
|
|
23
|
+
expect(sig.count).toBeGreaterThanOrEqual(5);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('does NOT trip on distinct targets (legit multi-source fetching)', () => {
|
|
27
|
+
const d = createStuckLoopDetector();
|
|
28
|
+
for (let i = 0; i < 7; i++) {
|
|
29
|
+
const sig = d.record('web_fetch', { url: `https://example.com/page-${i}`, maxBytes: 8000 });
|
|
30
|
+
expect(sig.stuck).toBe(false);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('ignores near-dups for tools with no identity arg', () => {
|
|
35
|
+
const d = createStuckLoopDetector();
|
|
36
|
+
// No url/path/command field — near tracking is skipped; only exact applies.
|
|
37
|
+
for (let i = 0; i < 6; i++) {
|
|
38
|
+
const sig = d.record('think', { note: `step ${i}` });
|
|
39
|
+
expect(sig.stuck).toBe(false);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
package/src/voice.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Voice / transcription helpers shared across surfaces.
|
|
3
|
+
*
|
|
4
|
+
* The TUI's voice-input infrastructure used to inline the same logic
|
|
5
|
+
* with a `Codex`-specific name baked in. Pulled out here as
|
|
6
|
+
* *agnostic* helpers that take a transcriber name as input, so the
|
|
7
|
+
* desktop, TUI, and any future channel can mirror the same flow:
|
|
8
|
+
*
|
|
9
|
+
* - Is the session ready to transcribe? Check via the requirements
|
|
10
|
+
* API for a named transcriber. (`checkTranscriberReady`)
|
|
11
|
+
* - Activate any registered transcriber lazily. Returns the active
|
|
12
|
+
* transcriber instance. (`resolveTranscriber`)
|
|
13
|
+
* - "Just give me whatever works" — try a list of candidates in
|
|
14
|
+
* order, or fall back to the first registered one. (`pickFirstAvailableTranscriber`)
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { ClientSession } from './client-session.js';
|
|
18
|
+
import type {
|
|
19
|
+
MoxxyRequirement,
|
|
20
|
+
RequirementCheck,
|
|
21
|
+
RequirementIssue,
|
|
22
|
+
} from './requirements.js';
|
|
23
|
+
import type { Transcriber } from './transcriber.js';
|
|
24
|
+
|
|
25
|
+
/** Probe whether a *named* transcriber is ready: registered, with any
|
|
26
|
+
* declared upstream requirements satisfied. The optional `requires`
|
|
27
|
+
* list lets channels gate on additional provider / auth runtimes
|
|
28
|
+
* (the Codex transcriber e.g. depends on the `openai-codex` provider
|
|
29
|
+
* + its OAuth runtime). */
|
|
30
|
+
export function checkTranscriberReady(
|
|
31
|
+
session: ClientSession,
|
|
32
|
+
transcriberName: string,
|
|
33
|
+
requires: ReadonlyArray<MoxxyRequirement> = [],
|
|
34
|
+
): RequirementCheck {
|
|
35
|
+
const baseline: ReadonlyArray<MoxxyRequirement> = [
|
|
36
|
+
{ kind: 'transcriber', name: transcriberName },
|
|
37
|
+
...requires,
|
|
38
|
+
];
|
|
39
|
+
const check = session.requirements.check(baseline);
|
|
40
|
+
const activeName = session.transcribers.getActiveName();
|
|
41
|
+
if (!activeName || activeName === transcriberName) return check;
|
|
42
|
+
|
|
43
|
+
const conflict: RequirementIssue = {
|
|
44
|
+
requirement: {
|
|
45
|
+
kind: 'transcriber',
|
|
46
|
+
name: transcriberName,
|
|
47
|
+
state: 'active',
|
|
48
|
+
hint: `Switch active transcriber to ${transcriberName}.`,
|
|
49
|
+
},
|
|
50
|
+
code: 'inactive',
|
|
51
|
+
message: `Required active transcriber ${transcriberName}; active is ${activeName}`,
|
|
52
|
+
hint: `Switch active transcriber to ${transcriberName}.`,
|
|
53
|
+
};
|
|
54
|
+
return { ready: false, issues: [conflict, ...check.issues] };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Activate a transcriber by name, lazily. Returns the active instance
|
|
58
|
+
* ready to `.transcribe(...)`. Throws if no such transcriber is
|
|
59
|
+
* registered, or a *different* one is already active. */
|
|
60
|
+
export function resolveTranscriber(
|
|
61
|
+
session: ClientSession,
|
|
62
|
+
transcriberName: string,
|
|
63
|
+
): Transcriber {
|
|
64
|
+
const activeName = session.transcribers.getActiveName();
|
|
65
|
+
if (activeName && activeName !== transcriberName) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`Another transcriber is already active: ${activeName}.`,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
if (activeName === transcriberName) return session.transcribers.getActive();
|
|
71
|
+
if (session.transcribers.has(transcriberName)) {
|
|
72
|
+
return session.transcribers.setActive(transcriberName);
|
|
73
|
+
}
|
|
74
|
+
throw new Error(
|
|
75
|
+
`No transcriber registered as ${transcriberName}. Configure one via your moxxy plugins.`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** "Just pick a transcriber that works."
|
|
80
|
+
*
|
|
81
|
+
* Tries each name in `candidates` in order — first one that can be
|
|
82
|
+
* activated wins. Returns null if none can be activated, so callers
|
|
83
|
+
* can degrade gracefully (hide their mic button, show a "no voice
|
|
84
|
+
* configured" tip, …) instead of throwing. */
|
|
85
|
+
export function pickFirstAvailableTranscriber(
|
|
86
|
+
session: ClientSession,
|
|
87
|
+
candidates: ReadonlyArray<string>,
|
|
88
|
+
): Transcriber | null {
|
|
89
|
+
// If something's already active, just hand that back — never fight
|
|
90
|
+
// a user-chosen activation.
|
|
91
|
+
const existing = session.transcribers.tryGetActive();
|
|
92
|
+
if (existing) return existing;
|
|
93
|
+
for (const name of candidates) {
|
|
94
|
+
try {
|
|
95
|
+
return resolveTranscriber(session, name);
|
|
96
|
+
} catch {
|
|
97
|
+
// Wrong-active errors don't apply here (we just returned early),
|
|
98
|
+
// so any throw is "this candidate isn't registered" — keep
|
|
99
|
+
// trying.
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
package/src/workflow.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workflows — saved, parameterized, schedulable/event-triggered DAGs whose
|
|
3
|
+
* steps run a skill, a free-form prompt, a tool, or a nested workflow, piping
|
|
4
|
+
* each step's output into the next. A workflow is an *artifact* (authored like
|
|
5
|
+
* a skill, discovered from disk); the strategy that *executes* it is a
|
|
6
|
+
* swappable block — {@link WorkflowExecutorDef} — registered into the
|
|
7
|
+
* `WorkflowExecutorRegistry` and selected by name, mirroring modes/compactors.
|
|
8
|
+
*
|
|
9
|
+
* These are the shared structural types. The zod schema that validates
|
|
10
|
+
* on-disk YAML lives in `@moxxy/plugin-workflows`; its parsed output is
|
|
11
|
+
* assignable to {@link Workflow}.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { Skill } from './skill.js';
|
|
15
|
+
import type { SubagentSpawner } from './subagent.js';
|
|
16
|
+
|
|
17
|
+
/** What fires a workflow. Omit `on` entirely for an on-demand-only workflow. */
|
|
18
|
+
export interface WorkflowTrigger {
|
|
19
|
+
/** Cron / one-shot time trigger, dispatched by `@moxxy/plugin-scheduler`. */
|
|
20
|
+
readonly schedule?: {
|
|
21
|
+
readonly cron?: string;
|
|
22
|
+
readonly runAt?: number | string;
|
|
23
|
+
readonly timeZone?: string;
|
|
24
|
+
};
|
|
25
|
+
/** Run when the named workflow(s) complete successfully (EventLog-driven). */
|
|
26
|
+
readonly afterWorkflow?: string | ReadonlyArray<string>;
|
|
27
|
+
/** Run when files matching the glob(s) under cwd change (fs.watch-driven). */
|
|
28
|
+
readonly fileChanged?: string | ReadonlyArray<string>;
|
|
29
|
+
/** Named webhook provider whose delivery fires this workflow. */
|
|
30
|
+
readonly webhook?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** How a failed step is handled: abort the workflow, skip past it, or retry. */
|
|
34
|
+
export type WorkflowStepErrorMode = 'fail' | 'continue' | 'retry';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* One node in the DAG. Exactly one *action* key is set
|
|
38
|
+
* (`skill` | `prompt` | `tool` | `workflow`). `input` is the templated prompt
|
|
39
|
+
* for skill/prompt actions; `args` are the templated arguments for
|
|
40
|
+
* tool/workflow actions. `needs` are the upstream step ids this step depends on.
|
|
41
|
+
*/
|
|
42
|
+
export interface WorkflowStep {
|
|
43
|
+
readonly id: string;
|
|
44
|
+
readonly skill?: string;
|
|
45
|
+
readonly prompt?: string;
|
|
46
|
+
readonly tool?: string;
|
|
47
|
+
readonly workflow?: string;
|
|
48
|
+
readonly input?: string;
|
|
49
|
+
readonly args?: Record<string, unknown>;
|
|
50
|
+
readonly needs: ReadonlyArray<string>;
|
|
51
|
+
/** Condition DSL; when it evaluates false the step is skipped. */
|
|
52
|
+
readonly when?: string;
|
|
53
|
+
readonly onError: WorkflowStepErrorMode;
|
|
54
|
+
readonly retries: number;
|
|
55
|
+
readonly label?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface WorkflowInputSpec {
|
|
59
|
+
readonly default?: unknown;
|
|
60
|
+
readonly description?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface WorkflowDelivery {
|
|
64
|
+
/** Soft hint for delivery target — e.g. "telegram", "inbox". */
|
|
65
|
+
readonly channel?: string;
|
|
66
|
+
/** Also drop the final output into `~/.moxxy/inbox/`. Default true. */
|
|
67
|
+
readonly inbox: boolean;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface Workflow {
|
|
71
|
+
readonly name: string;
|
|
72
|
+
readonly description: string;
|
|
73
|
+
readonly version: number;
|
|
74
|
+
/**
|
|
75
|
+
* When false the workflow is inert: triggers (schedule/event) never fire it
|
|
76
|
+
* and it is excluded from auto-runs, but it stays listable and can still be
|
|
77
|
+
* run explicitly. Toggled from the `/workflows` command.
|
|
78
|
+
*/
|
|
79
|
+
readonly enabled: boolean;
|
|
80
|
+
readonly inputs: Record<string, WorkflowInputSpec>;
|
|
81
|
+
readonly on?: WorkflowTrigger;
|
|
82
|
+
readonly delivery?: WorkflowDelivery;
|
|
83
|
+
/** Max steps to run concurrently in one ready-set round. */
|
|
84
|
+
readonly concurrency: number;
|
|
85
|
+
readonly steps: ReadonlyArray<WorkflowStep>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Minimal tool surface a step needs — structurally a subset of `ToolRegistry`. */
|
|
89
|
+
export interface WorkflowToolRunner {
|
|
90
|
+
get(name: string): unknown | undefined;
|
|
91
|
+
execute(name: string, input: unknown, signal: AbortSignal): Promise<unknown>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Look up sibling artifacts by name during a run. */
|
|
95
|
+
export interface WorkflowLookup {
|
|
96
|
+
skill(name: string): Skill | undefined;
|
|
97
|
+
workflow(name: string): Workflow | undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Lifecycle subtypes an executor emits as `plugin_event`s (mirrors plan_*). */
|
|
101
|
+
export type WorkflowEventSubtype =
|
|
102
|
+
| 'workflow_started'
|
|
103
|
+
| 'workflow_step_started'
|
|
104
|
+
| 'workflow_step_completed'
|
|
105
|
+
| 'workflow_step_skipped'
|
|
106
|
+
| 'workflow_step_failed'
|
|
107
|
+
| 'workflow_completed'
|
|
108
|
+
| 'workflow_failed';
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Everything an executor needs to run a workflow, supplied by the caller.
|
|
112
|
+
* The in-turn `workflow_run` tool wires `spawner` from `ctx.subagents`; the
|
|
113
|
+
* autonomous runner builds one from an isolated session. Kept free of core
|
|
114
|
+
* imports so the executor stays in a plugin.
|
|
115
|
+
*/
|
|
116
|
+
export interface WorkflowRunDeps {
|
|
117
|
+
readonly spawner: SubagentSpawner;
|
|
118
|
+
readonly tools: WorkflowToolRunner;
|
|
119
|
+
readonly lookup: WorkflowLookup;
|
|
120
|
+
readonly signal: AbortSignal;
|
|
121
|
+
/** Resolved input values (defaults already applied) for this run. */
|
|
122
|
+
readonly inputs?: Record<string, unknown>;
|
|
123
|
+
/** Free-form description of what fired the run (for `{{ trigger }}`). */
|
|
124
|
+
readonly trigger?: string;
|
|
125
|
+
/** Wall-clock source. Injected so tests are deterministic. */
|
|
126
|
+
readonly now?: () => number;
|
|
127
|
+
/** Emit a workflow lifecycle event. No-op when omitted. */
|
|
128
|
+
readonly emit?: (subtype: WorkflowEventSubtype, payload: unknown) => void | Promise<void>;
|
|
129
|
+
readonly logger?: {
|
|
130
|
+
warn?(msg: string, meta?: Record<string, unknown>): void;
|
|
131
|
+
info?(msg: string, meta?: Record<string, unknown>): void;
|
|
132
|
+
};
|
|
133
|
+
/** Nested-workflow recursion depth; executors guard against runaway nesting. */
|
|
134
|
+
readonly depth?: number;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export type WorkflowStepStatus = 'completed' | 'skipped' | 'failed';
|
|
138
|
+
|
|
139
|
+
export interface WorkflowStepResult {
|
|
140
|
+
readonly id: string;
|
|
141
|
+
readonly status: WorkflowStepStatus;
|
|
142
|
+
readonly output: string;
|
|
143
|
+
readonly error?: string;
|
|
144
|
+
readonly startedAt: number;
|
|
145
|
+
readonly endedAt: number;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface WorkflowRunResult {
|
|
149
|
+
readonly ok: boolean;
|
|
150
|
+
readonly steps: ReadonlyArray<WorkflowStepResult>;
|
|
151
|
+
/** Output of the terminal (sink) step(s) — what delivery sends. */
|
|
152
|
+
readonly output: string;
|
|
153
|
+
readonly error?: string;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* A swappable workflow-execution strategy. v1 ships one (`dag`); a plugin can
|
|
158
|
+
* register alternatives (parallel-heavy, dry-run, …) and the active one is
|
|
159
|
+
* selected by name via `session.workflowExecutors.setActive(name)`.
|
|
160
|
+
*/
|
|
161
|
+
export interface WorkflowExecutorDef {
|
|
162
|
+
readonly name: string;
|
|
163
|
+
readonly description?: string;
|
|
164
|
+
run(workflow: Workflow, deps: WorkflowRunDeps): Promise<WorkflowRunResult>;
|
|
165
|
+
}
|