thinkwell 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.
@@ -0,0 +1,35 @@
1
+ import type { JsonSchema, SchemaProvider } from "@thinkwell/acp";
2
+ /**
3
+ * Creates a SchemaProvider from a raw JSON Schema object.
4
+ *
5
+ * This is a convenience function for users who want to pass a JSON schema
6
+ * directly without using a schema library like Zod or TypeBox.
7
+ *
8
+ * @typeParam T - The TypeScript type that this schema describes
9
+ * @param schema - A JSON Schema object describing the expected output structure
10
+ * @returns A SchemaProvider that wraps the given schema
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * interface Summary {
15
+ * title: string;
16
+ * points: string[];
17
+ * }
18
+ *
19
+ * const result = await patchwork
20
+ * .think(schemaOf<Summary>({
21
+ * type: "object",
22
+ * properties: {
23
+ * title: { type: "string" },
24
+ * points: { type: "array", items: { type: "string" } }
25
+ * },
26
+ * required: ["title", "points"]
27
+ * }))
28
+ * .text("Summarize this document")
29
+ * .run();
30
+ *
31
+ * // result is typed as Summary
32
+ * ```
33
+ */
34
+ export declare function schemaOf<T>(schema: JsonSchema): SchemaProvider<T>;
35
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,UAAU,GAAG,cAAc,CAAC,CAAC,CAAC,CAIjE"}
package/dist/schema.js ADDED
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Creates a SchemaProvider from a raw JSON Schema object.
3
+ *
4
+ * This is a convenience function for users who want to pass a JSON schema
5
+ * directly without using a schema library like Zod or TypeBox.
6
+ *
7
+ * @typeParam T - The TypeScript type that this schema describes
8
+ * @param schema - A JSON Schema object describing the expected output structure
9
+ * @returns A SchemaProvider that wraps the given schema
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * interface Summary {
14
+ * title: string;
15
+ * points: string[];
16
+ * }
17
+ *
18
+ * const result = await patchwork
19
+ * .think(schemaOf<Summary>({
20
+ * type: "object",
21
+ * properties: {
22
+ * title: { type: "string" },
23
+ * points: { type: "array", items: { type: "string" } }
24
+ * },
25
+ * required: ["title", "points"]
26
+ * }))
27
+ * .text("Summarize this document")
28
+ * .run();
29
+ *
30
+ * // result is typed as Summary
31
+ * ```
32
+ */
33
+ export function schemaOf(schema) {
34
+ return {
35
+ toJsonSchema: () => schema,
36
+ };
37
+ }
38
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,UAAU,QAAQ,CAAI,MAAkB;IAC5C,OAAO;QACL,YAAY,EAAE,GAAG,EAAE,CAAC,MAAM;KAC3B,CAAC;AACJ,CAAC"}
@@ -0,0 +1,78 @@
1
+ import type { AgentConnection, SessionOptions } from "./agent.js";
2
+ import type { SchemaProvider } from "@thinkwell/acp";
3
+ import { ThinkBuilder } from "./think-builder.js";
4
+ /**
5
+ * A session for multi-turn conversations with an agent.
6
+ *
7
+ * Sessions maintain conversation context across multiple `think()` calls,
8
+ * allowing the agent to remember previous interactions. Create sessions
9
+ * using `agent.createSession()`.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const session = await agent.createSession({ cwd: "/my/project" });
14
+ *
15
+ * // First turn
16
+ * const analysis = await session
17
+ * .think(AnalysisSchema)
18
+ * .text("Analyze this codebase")
19
+ * .run();
20
+ *
21
+ * // Second turn - agent remembers context
22
+ * const fixes = await session
23
+ * .think(FixesSchema)
24
+ * .text("Suggest fixes for the top issues")
25
+ * .run();
26
+ *
27
+ * session.close();
28
+ * ```
29
+ */
30
+ export declare class Session {
31
+ private readonly _conn;
32
+ private readonly _sessionId;
33
+ private readonly _options;
34
+ private _closed;
35
+ /**
36
+ * @internal
37
+ */
38
+ constructor(conn: AgentConnection, sessionId: string, options?: SessionOptions);
39
+ /**
40
+ * The unique session identifier
41
+ */
42
+ get sessionId(): string;
43
+ /**
44
+ * Create a new think builder for constructing a prompt with tools.
45
+ *
46
+ * Unlike `agent.think()`, prompts sent through a session maintain
47
+ * conversation context - the agent remembers previous interactions.
48
+ *
49
+ * @param schema - A SchemaProvider that defines the expected output structure
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const result = await session
54
+ * .think(schemaOf<{ answer: string }>({
55
+ * type: "object",
56
+ * properties: { answer: { type: "string" } },
57
+ * required: ["answer"]
58
+ * }))
59
+ * .text("What was the first thing I asked you?")
60
+ * .run();
61
+ * ```
62
+ */
63
+ think<Output>(schema: SchemaProvider<Output>): ThinkBuilder<Output>;
64
+ /**
65
+ * Create a new think builder without a schema.
66
+ *
67
+ * @deprecated Use `think(schemaOf<T>(schema))` instead to provide a typed schema.
68
+ */
69
+ think<Output>(): ThinkBuilder<Output>;
70
+ /**
71
+ * Close the session.
72
+ *
73
+ * After closing, no more prompts can be sent through this session.
74
+ * The agent connection remains open for other sessions.
75
+ */
76
+ close(): void;
77
+ }
78
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAClE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,OAAO;IAClB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAkB;IACxC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA6B;IACtD,OAAO,CAAC,OAAO,CAAkB;IAEjC;;OAEG;gBACS,IAAI,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc;IAM9E;;OAEG;IACH,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,cAAc,CAAC,MAAM,CAAC,GAAG,YAAY,CAAC,MAAM,CAAC;IAEnE;;;;OAIG;IACH,KAAK,CAAC,MAAM,KAAK,YAAY,CAAC,MAAM,CAAC;IASrC;;;;;OAKG;IACH,KAAK,IAAI,IAAI;CAKd"}
@@ -0,0 +1,65 @@
1
+ import { ThinkBuilder } from "./think-builder.js";
2
+ /**
3
+ * A session for multi-turn conversations with an agent.
4
+ *
5
+ * Sessions maintain conversation context across multiple `think()` calls,
6
+ * allowing the agent to remember previous interactions. Create sessions
7
+ * using `agent.createSession()`.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const session = await agent.createSession({ cwd: "/my/project" });
12
+ *
13
+ * // First turn
14
+ * const analysis = await session
15
+ * .think(AnalysisSchema)
16
+ * .text("Analyze this codebase")
17
+ * .run();
18
+ *
19
+ * // Second turn - agent remembers context
20
+ * const fixes = await session
21
+ * .think(FixesSchema)
22
+ * .text("Suggest fixes for the top issues")
23
+ * .run();
24
+ *
25
+ * session.close();
26
+ * ```
27
+ */
28
+ export class Session {
29
+ _conn;
30
+ _sessionId;
31
+ _options;
32
+ _closed = false;
33
+ /**
34
+ * @internal
35
+ */
36
+ constructor(conn, sessionId, options) {
37
+ this._conn = conn;
38
+ this._sessionId = sessionId;
39
+ this._options = options;
40
+ }
41
+ /**
42
+ * The unique session identifier
43
+ */
44
+ get sessionId() {
45
+ return this._sessionId;
46
+ }
47
+ think(schema) {
48
+ if (this._closed) {
49
+ throw new Error("Session is closed");
50
+ }
51
+ return new ThinkBuilder(this._conn, schema, this._sessionId);
52
+ }
53
+ /**
54
+ * Close the session.
55
+ *
56
+ * After closing, no more prompts can be sent through this session.
57
+ * The agent connection remains open for other sessions.
58
+ */
59
+ close() {
60
+ this._closed = true;
61
+ // Note: ACP doesn't have an explicit session close message,
62
+ // we just stop using the session ID
63
+ }
64
+ }
65
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,OAAO,OAAO;IACD,KAAK,CAAkB;IACvB,UAAU,CAAS;IACnB,QAAQ,CAA6B;IAC9C,OAAO,GAAY,KAAK,CAAC;IAEjC;;OAEG;IACH,YAAY,IAAqB,EAAE,SAAiB,EAAE,OAAwB;QAC5E,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC5B,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IA+BD,KAAK,CAAS,MAA+B;QAC3C,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;QACvC,CAAC;QACD,OAAO,IAAI,YAAY,CAAS,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IACvE,CAAC;IAED;;;;;OAKG;IACH,KAAK;QACH,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,4DAA4D;QAC5D,oCAAoC;IACtC,CAAC;CACF"}
@@ -0,0 +1,150 @@
1
+ import { type JsonSchema, type SchemaProvider } from "@thinkwell/acp";
2
+ import type { AgentConnection } from "./agent.js";
3
+ /**
4
+ * Fluent builder for composing prompts with tools.
5
+ *
6
+ * ThinkBuilder provides a chainable API for:
7
+ * - Adding literal text to the prompt
8
+ * - Interpolating values
9
+ * - Registering tools the LLM can call
10
+ * - Executing the prompt and returning a typed result
11
+ */
12
+ export declare class ThinkBuilder<Output> {
13
+ private readonly _conn;
14
+ private _promptParts;
15
+ private _tools;
16
+ private _schemaProvider;
17
+ private _cwd;
18
+ private _existingSessionId;
19
+ constructor(conn: AgentConnection, schema?: SchemaProvider<Output>, existingSessionId?: string);
20
+ /**
21
+ * Add literal text to the prompt
22
+ */
23
+ text(content: string): this;
24
+ /**
25
+ * Add a line of text with newline
26
+ */
27
+ textln(content: string): this;
28
+ /**
29
+ * Quote some content delimited by XML-style tags.
30
+ */
31
+ quote(content: string, tag?: string): this;
32
+ /**
33
+ * Quote some content as a Markdown-style code block.
34
+ */
35
+ code(content: string, language?: string): this;
36
+ /**
37
+ * Interpolate a value using toString()
38
+ *
39
+ * @deprecated Use `text()` or `quote()` instead for clearer intent.
40
+ */
41
+ display(value: unknown): this;
42
+ /**
43
+ * Register a tool and reference it in the prompt.
44
+ *
45
+ * The tool will be mentioned in the prompt text to help the LLM
46
+ * understand that it's available.
47
+ *
48
+ * @param name - The tool name
49
+ * @param description - A description of what the tool does
50
+ * @param inputSchema - A SchemaProvider describing the expected input structure
51
+ * @param outputSchema - A SchemaProvider describing the output structure
52
+ * @param handler - The function to execute when the tool is called
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * import { schemaOf } from "@dherman/patchwork";
57
+ *
58
+ * interface SearchInput {
59
+ * query: string;
60
+ * limit?: number;
61
+ * }
62
+ *
63
+ * interface SearchResult {
64
+ * matches: string[];
65
+ * total: number;
66
+ * }
67
+ *
68
+ * agent.think(outputSchema)
69
+ * .tool(
70
+ * "search",
71
+ * "Search for documents",
72
+ * schemaOf<SearchInput>({
73
+ * type: "object",
74
+ * properties: {
75
+ * query: { type: "string" },
76
+ * limit: { type: "number" }
77
+ * },
78
+ * required: ["query"]
79
+ * }),
80
+ * schemaOf<SearchResult>({
81
+ * type: "object",
82
+ * properties: {
83
+ * matches: { type: "array", items: { type: "string" } },
84
+ * total: { type: "number" }
85
+ * },
86
+ * required: ["matches", "total"]
87
+ * }),
88
+ * async (input: SearchInput) => { ... }
89
+ * )
90
+ * .run();
91
+ * ```
92
+ */
93
+ tool<I, O>(name: string, description: string, inputSchema: SchemaProvider<I>, outputSchema: SchemaProvider<O>, handler: (input: I) => Promise<O>): this;
94
+ /**
95
+ * Register a tool with only an input schema.
96
+ */
97
+ tool<I>(name: string, description: string, inputSchema: SchemaProvider<I>, handler: (input: I) => Promise<unknown>): this;
98
+ /**
99
+ * Register a tool without schemas.
100
+ */
101
+ tool(name: string, description: string, handler: (input: unknown) => Promise<unknown>): this;
102
+ /**
103
+ * Register a tool without adding a prompt reference.
104
+ *
105
+ * Use this for tools that should be available but don't need
106
+ * to be explicitly mentioned in the prompt.
107
+ *
108
+ * @param name - The tool name
109
+ * @param description - A description of what the tool does
110
+ * @param inputSchema - A SchemaProvider describing the expected input structure
111
+ * @param outputSchema - A SchemaProvider describing the output structure
112
+ * @param handler - The function to execute when the tool is called
113
+ */
114
+ defineTool<I, O>(name: string, description: string, inputSchema: SchemaProvider<I>, outputSchema: SchemaProvider<O>, handler: (input: I) => Promise<O>): this;
115
+ /**
116
+ * Register a tool with only an input schema (no prompt reference).
117
+ */
118
+ defineTool<I>(name: string, description: string, inputSchema: SchemaProvider<I>, handler: (input: I) => Promise<unknown>): this;
119
+ /**
120
+ * Register a tool without schemas (no prompt reference).
121
+ */
122
+ defineTool(name: string, description: string, handler: (input: unknown) => Promise<unknown>): this;
123
+ /**
124
+ * Set the expected output schema.
125
+ *
126
+ * This generates a return_result tool that the LLM must call
127
+ * to provide the final output.
128
+ *
129
+ * @deprecated Use `agent.think(schemaOf<T>(schema))` instead to provide a typed schema at construction time.
130
+ */
131
+ outputSchema(schema: JsonSchema): this;
132
+ /**
133
+ * Set the working directory for the session
134
+ */
135
+ cwd(path: string): this;
136
+ /**
137
+ * Execute the prompt and return the result.
138
+ *
139
+ * This method:
140
+ * 1. Builds the final prompt from all text parts
141
+ * 2. Creates an MCP server with all registered tools
142
+ * 3. Adds a return_result tool for the output
143
+ * 4. Sends the prompt to the agent
144
+ * 5. Handles tool calls until the agent returns a result
145
+ * 6. Returns the typed result
146
+ */
147
+ run(): Promise<Output>;
148
+ private _executeRun;
149
+ }
150
+ //# sourceMappingURL=think-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"think-builder.d.ts","sourceRoot":"","sources":["../src/think-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,cAAc,EAEpB,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,eAAe,EAAkB,MAAM,YAAY,CAAC;AA2ElE;;;;;;;;GAQG;AACH,qBAAa,YAAY,CAAC,MAAM;IAC9B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAkB;IACxC,OAAO,CAAC,YAAY,CAAgB;IACpC,OAAO,CAAC,MAAM,CAA0C;IACxD,OAAO,CAAC,eAAe,CAAqC;IAC5D,OAAO,CAAC,IAAI,CAAqB;IACjC,OAAO,CAAC,kBAAkB,CAAqB;gBAG7C,IAAI,EAAE,eAAe,EACrB,MAAM,CAAC,EAAE,cAAc,CAAC,MAAM,CAAC,EAC/B,iBAAiB,CAAC,EAAE,MAAM;IAO5B;;OAEG;IACH,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAK3B;;OAEG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAK7B;;OAEG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,GAAE,MAAgB,GAAG,IAAI;IAWnD;;OAEG;IACH,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,GAAE,MAAW,GAAG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