ocpipe 0.1.0
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 +334 -0
- package/agent.ts +176 -0
- package/example/correction.ts +85 -0
- package/example/index.ts +31 -0
- package/example/module.ts +20 -0
- package/example/signature.ts +18 -0
- package/index.ts +127 -0
- package/module.ts +50 -0
- package/package.json +48 -0
- package/parsing.ts +865 -0
- package/pipeline.ts +213 -0
- package/predict.ts +271 -0
- package/signature.ts +97 -0
- package/state.ts +39 -0
- package/testing.ts +180 -0
- package/types.ts +260 -0
package/index.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSTS: Declarative Self-Improving TypeScript
|
|
3
|
+
*
|
|
4
|
+
* A DSPy-inspired SDK for building LLM workflow pipelines with OpenCode.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { signature, field, SignatureModule, Pipeline } from './dsts/index.js'
|
|
9
|
+
* import { z } from 'zod'
|
|
10
|
+
*
|
|
11
|
+
* // Define a signature
|
|
12
|
+
* const ParseIntent = signature({
|
|
13
|
+
* doc: 'Parse user intent from description',
|
|
14
|
+
* inputs: {
|
|
15
|
+
* description: field.string('User description'),
|
|
16
|
+
* },
|
|
17
|
+
* outputs: {
|
|
18
|
+
* intent: field.string('Parsed intent'),
|
|
19
|
+
* confidence: field.number('Confidence score'),
|
|
20
|
+
* },
|
|
21
|
+
* })
|
|
22
|
+
*
|
|
23
|
+
* // Create a module (types inferred from signature)
|
|
24
|
+
* class IntentParser extends SignatureModule<typeof ParseIntent> {
|
|
25
|
+
* constructor() {
|
|
26
|
+
* super(ParseIntent)
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* async forward(input, ctx) {
|
|
30
|
+
* const result = await this.predictor.execute(input, ctx)
|
|
31
|
+
* return result.data
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* // Run in a pipeline
|
|
36
|
+
* const pipeline = new Pipeline({
|
|
37
|
+
* name: 'my-workflow',
|
|
38
|
+
* defaultModel: { providerID: 'anthropic', modelID: 'claude-sonnet-4-5' },
|
|
39
|
+
* defaultAgent: 'general',
|
|
40
|
+
* checkpointDir: './ckpt',
|
|
41
|
+
* logDir: './logs',
|
|
42
|
+
* }, createBaseState)
|
|
43
|
+
*
|
|
44
|
+
* const result = await pipeline.run(new IntentParser(), { description: 'Hello world' })
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
// Signature definition
|
|
49
|
+
export { signature, field, buildOutputSchema } from './signature.js'
|
|
50
|
+
|
|
51
|
+
// Predict class
|
|
52
|
+
export { Predict } from './predict.js'
|
|
53
|
+
export type { PredictConfig } from './predict.js'
|
|
54
|
+
|
|
55
|
+
// Module base class
|
|
56
|
+
export { Module, SignatureModule } from './module.js'
|
|
57
|
+
|
|
58
|
+
// Pipeline orchestrator
|
|
59
|
+
export { Pipeline } from './pipeline.js'
|
|
60
|
+
|
|
61
|
+
// State management
|
|
62
|
+
export { createSessionId, createBaseState, extendBaseState } from './state.js'
|
|
63
|
+
|
|
64
|
+
// Agent integration
|
|
65
|
+
export { runAgent, logStep } from './agent.js'
|
|
66
|
+
|
|
67
|
+
// Response parsing
|
|
68
|
+
export {
|
|
69
|
+
parseResponse,
|
|
70
|
+
parseJson,
|
|
71
|
+
parseJsonFromResponse,
|
|
72
|
+
tryParseResponse,
|
|
73
|
+
tryParseJson,
|
|
74
|
+
extractJsonString,
|
|
75
|
+
// jq-style patches
|
|
76
|
+
buildPatchPrompt,
|
|
77
|
+
buildBatchPatchPrompt,
|
|
78
|
+
extractPatch,
|
|
79
|
+
applyJqPatch,
|
|
80
|
+
// JSON Patch (RFC 6902)
|
|
81
|
+
buildJsonPatchPrompt,
|
|
82
|
+
buildBatchJsonPatchPrompt,
|
|
83
|
+
extractJsonPatch,
|
|
84
|
+
applyJsonPatch,
|
|
85
|
+
zodTypeToString,
|
|
86
|
+
JsonParseError,
|
|
87
|
+
ValidationError,
|
|
88
|
+
SchemaValidationError,
|
|
89
|
+
} from './parsing.js'
|
|
90
|
+
export type { JsonPatchOperation } from './parsing.js'
|
|
91
|
+
|
|
92
|
+
// Testing utilities
|
|
93
|
+
export {
|
|
94
|
+
MockAgentBackend,
|
|
95
|
+
createMockContext,
|
|
96
|
+
generateMockOutputs,
|
|
97
|
+
} from './testing.js'
|
|
98
|
+
export type { MockResponse } from './testing.js'
|
|
99
|
+
|
|
100
|
+
// Types
|
|
101
|
+
export type {
|
|
102
|
+
// Core types
|
|
103
|
+
ModelConfig,
|
|
104
|
+
ExecutionContext,
|
|
105
|
+
StepResult,
|
|
106
|
+
StepRecord,
|
|
107
|
+
SubPipelineRecord,
|
|
108
|
+
BaseState,
|
|
109
|
+
PredictResult,
|
|
110
|
+
// Signature types
|
|
111
|
+
FieldConfig,
|
|
112
|
+
SignatureDef,
|
|
113
|
+
InferInputs,
|
|
114
|
+
InferOutputs,
|
|
115
|
+
// Pipeline types
|
|
116
|
+
RetryConfig,
|
|
117
|
+
PipelineConfig,
|
|
118
|
+
RunOptions,
|
|
119
|
+
// Agent types
|
|
120
|
+
RunAgentOptions,
|
|
121
|
+
RunAgentResult,
|
|
122
|
+
// Correction types
|
|
123
|
+
CorrectionMethod,
|
|
124
|
+
CorrectionConfig,
|
|
125
|
+
FieldError,
|
|
126
|
+
TryParseResult,
|
|
127
|
+
} from './types.js'
|
package/module.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSTS SDK Module base class.
|
|
3
|
+
*
|
|
4
|
+
* Modules encapsulate logical units of work that use one or more Predictors.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
ExecutionContext,
|
|
9
|
+
InferInputs,
|
|
10
|
+
InferOutputs,
|
|
11
|
+
SignatureDef,
|
|
12
|
+
} from './types.js'
|
|
13
|
+
import { Predict, type PredictConfig } from './predict.js'
|
|
14
|
+
|
|
15
|
+
/** Module is the abstract base class for composable workflow units. */
|
|
16
|
+
export abstract class Module<I, O> {
|
|
17
|
+
private predictors: Predict<any>[] = []
|
|
18
|
+
|
|
19
|
+
/** predict creates and registers a Predict instance for a signature. */
|
|
20
|
+
protected predict<S extends SignatureDef<any, any>>(
|
|
21
|
+
sig: S,
|
|
22
|
+
config?: PredictConfig,
|
|
23
|
+
): Predict<S> {
|
|
24
|
+
const p = new Predict(sig, config)
|
|
25
|
+
this.predictors.push(p)
|
|
26
|
+
return p
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** forward is the main execution method to be implemented by subclasses. */
|
|
30
|
+
abstract forward(input: I, ctx: ExecutionContext): Promise<O>
|
|
31
|
+
|
|
32
|
+
/** getPredictors returns all registered predictors (for future optimization). */
|
|
33
|
+
getPredictors(): Predict<any>[] {
|
|
34
|
+
return this.predictors
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** SignatureModule is a Module whose types are derived from a Signature. */
|
|
39
|
+
export abstract class SignatureModule<
|
|
40
|
+
S extends SignatureDef<any, any>,
|
|
41
|
+
> extends Module<InferInputs<S>, InferOutputs<S>> {
|
|
42
|
+
protected readonly sig: S
|
|
43
|
+
protected readonly predictor: Predict<S>
|
|
44
|
+
|
|
45
|
+
constructor(sig: S, config?: PredictConfig) {
|
|
46
|
+
super()
|
|
47
|
+
this.sig = sig
|
|
48
|
+
this.predictor = this.predict(sig, config)
|
|
49
|
+
}
|
|
50
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ocpipe",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Declarative Self-Improving TypeScript - A DSPy-inspired SDK for building LLM workflow pipelines with OpenCode",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.ts",
|
|
7
|
+
"types": "index.ts",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/s4wave/dsts"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/s4wave/dsts",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/s4wave/dsts/issues"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": "s4wave",
|
|
18
|
+
"keywords": [
|
|
19
|
+
"dspy",
|
|
20
|
+
"llm",
|
|
21
|
+
"opencode",
|
|
22
|
+
"typescript",
|
|
23
|
+
"ai",
|
|
24
|
+
"workflow",
|
|
25
|
+
"pipeline",
|
|
26
|
+
"bun"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"bun": ">=1.0.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"zod": "^3.24.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"typescript": "^5.0.0",
|
|
36
|
+
"vitest": "^2.0.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"typecheck": "tsc --noEmit",
|
|
40
|
+
"test": "vitest run",
|
|
41
|
+
"test:watch": "vitest"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"*.ts",
|
|
45
|
+
"!*.test.ts",
|
|
46
|
+
"example/"
|
|
47
|
+
]
|
|
48
|
+
}
|