@loopstack/claude-module 0.21.0 → 0.22.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/dist/claude.module.d.ts.map +1 -1
- package/dist/claude.module.js +5 -12
- package/dist/claude.module.js.map +1 -1
- package/dist/documents/claude-message-document.d.ts +3 -1
- package/dist/documents/claude-message-document.d.ts.map +1 -1
- package/dist/documents/claude-message-document.js +6 -18
- package/dist/documents/claude-message-document.js.map +1 -1
- package/dist/documents/claude-message-document.yaml +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/services/claude-tools-helper.service.d.ts.map +1 -1
- package/dist/services/claude-tools-helper.service.js +6 -8
- package/dist/services/claude-tools-helper.service.js.map +1 -1
- package/dist/tools/claude-generate-document.tool.d.ts +3 -6
- package/dist/tools/claude-generate-document.tool.d.ts.map +1 -1
- package/dist/tools/claude-generate-document.tool.js +9 -31
- package/dist/tools/claude-generate-document.tool.js.map +1 -1
- package/dist/tools/claude-generate-object.tool.d.ts +4 -5
- package/dist/tools/claude-generate-object.tool.d.ts.map +1 -1
- package/dist/tools/claude-generate-object.tool.js +7 -17
- package/dist/tools/claude-generate-object.tool.js.map +1 -1
- package/dist/tools/claude-generate-text.tool.d.ts +5 -4
- package/dist/tools/claude-generate-text.tool.d.ts.map +1 -1
- package/dist/tools/claude-generate-text.tool.js +27 -13
- package/dist/tools/claude-generate-text.tool.js.map +1 -1
- package/dist/tools/delegate-tool-calls.tool.d.ts +23 -0
- package/dist/tools/delegate-tool-calls.tool.d.ts.map +1 -0
- package/dist/tools/delegate-tool-calls.tool.js +117 -0
- package/dist/tools/delegate-tool-calls.tool.js.map +1 -0
- package/dist/tools/index.d.ts +2 -1
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +2 -1
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/update-tool-result.tool.d.ts +19 -0
- package/dist/tools/update-tool-result.tool.d.ts.map +1 -0
- package/dist/tools/update-tool-result.tool.js +89 -0
- package/dist/tools/update-tool-result.tool.js.map +1 -0
- package/dist/types/claude.types.d.ts +19 -0
- package/dist/types/claude.types.d.ts.map +1 -1
- package/dist/types/claude.types.js.map +1 -1
- package/package.json +3 -3
- package/src/claude.module.ts +12 -15
- package/src/documents/claude-message-document.ts +6 -12
- package/src/documents/claude-message-document.yaml +2 -2
- package/src/index.ts +1 -0
- package/src/services/claude-tools-helper.service.ts +6 -8
- package/src/tools/claude-generate-document.tool.ts +11 -48
- package/src/tools/claude-generate-object.tool.ts +13 -29
- package/src/tools/claude-generate-text.tool.ts +33 -25
- package/src/tools/delegate-tool-calls.tool.ts +139 -0
- package/src/tools/index.ts +2 -1
- package/src/tools/update-tool-result.tool.ts +99 -0
- package/src/types/claude.types.ts +26 -0
- package/dist/tools/claude-delegate-tool-call.tool.d.ts +0 -19
- package/dist/tools/claude-delegate-tool-call.tool.d.ts.map +0 -1
- package/dist/tools/claude-delegate-tool-call.tool.js +0 -102
- package/dist/tools/claude-delegate-tool-call.tool.js.map +0 -1
- package/src/tools/claude-delegate-tool-call.tool.ts +0 -125
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var DelegateToolCalls_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.DelegateToolCalls = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const zod_1 = require("zod");
|
|
13
|
+
const common_2 = require("@loopstack/common");
|
|
14
|
+
const DelegateToolCallsSchema = zod_1.z.object({
|
|
15
|
+
message: zod_1.z.object({
|
|
16
|
+
id: zod_1.z.string().optional(),
|
|
17
|
+
content: zod_1.z.array(zod_1.z.any()),
|
|
18
|
+
}),
|
|
19
|
+
document: zod_1.z
|
|
20
|
+
.custom((val) => typeof val === 'function' && (0, common_2.getBlockTypeFromMetadata)(val) === 'document')
|
|
21
|
+
.optional(),
|
|
22
|
+
skipResponseMessage: zod_1.z.boolean().optional(),
|
|
23
|
+
callback: zod_1.z
|
|
24
|
+
.object({
|
|
25
|
+
transition: zod_1.z.string(),
|
|
26
|
+
})
|
|
27
|
+
.optional(),
|
|
28
|
+
});
|
|
29
|
+
let DelegateToolCalls = DelegateToolCalls_1 = class DelegateToolCalls extends common_2.BaseTool {
|
|
30
|
+
logger = new common_1.Logger(DelegateToolCalls_1.name);
|
|
31
|
+
async call(args) {
|
|
32
|
+
const contentBlocks = args.message.content;
|
|
33
|
+
const toolUseBlocks = contentBlocks.filter((b) => b.type === 'tool_use');
|
|
34
|
+
if (toolUseBlocks.length === 0) {
|
|
35
|
+
return {
|
|
36
|
+
data: {
|
|
37
|
+
allCompleted: true,
|
|
38
|
+
toolResults: [],
|
|
39
|
+
message: args.message,
|
|
40
|
+
pendingCount: 0,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const results = await Promise.all(toolUseBlocks.map((block) => {
|
|
45
|
+
const toolCallback = args.callback
|
|
46
|
+
? {
|
|
47
|
+
transition: args.callback.transition,
|
|
48
|
+
metadata: { toolUseId: block.id, toolName: block.name },
|
|
49
|
+
}
|
|
50
|
+
: undefined;
|
|
51
|
+
return this.executeTool(block, toolCallback ? { callback: toolCallback } : undefined);
|
|
52
|
+
}));
|
|
53
|
+
let pendingCount = 0;
|
|
54
|
+
const toolResults = [];
|
|
55
|
+
for (let i = 0; i < toolUseBlocks.length; i++) {
|
|
56
|
+
const block = toolUseBlocks[i];
|
|
57
|
+
const result = results[i];
|
|
58
|
+
if (result.pending) {
|
|
59
|
+
pendingCount++;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
toolResults.push({
|
|
63
|
+
type: 'tool_result',
|
|
64
|
+
tool_use_id: block.id,
|
|
65
|
+
content: result.data ? JSON.stringify(result.data, null, 2) : '',
|
|
66
|
+
is_error: !!result.error,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (args.document && !args.skipResponseMessage) {
|
|
71
|
+
await this.createResponseDocument(args.document, args, toolResults);
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
data: {
|
|
75
|
+
allCompleted: pendingCount === 0,
|
|
76
|
+
toolResults,
|
|
77
|
+
message: args.message,
|
|
78
|
+
pendingCount,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
async executeTool(block, options) {
|
|
83
|
+
try {
|
|
84
|
+
const tool = (0, common_2.getBlockTool)(this.ctx.parent, block.name);
|
|
85
|
+
if (!tool) {
|
|
86
|
+
throw new Error(`Tool with name ${block.name} not found.`);
|
|
87
|
+
}
|
|
88
|
+
return await tool.call(block.input, options);
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
92
|
+
this.logger.error(`Tool "${block.name}" failed: ${errorMessage}`);
|
|
93
|
+
return {
|
|
94
|
+
data: errorMessage,
|
|
95
|
+
error: errorMessage,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async createResponseDocument(document, args, toolResults) {
|
|
100
|
+
await this.repository.save(document, {
|
|
101
|
+
role: 'assistant',
|
|
102
|
+
content: args.message.content,
|
|
103
|
+
toolResults,
|
|
104
|
+
}, { id: args.message.id, validate: 'skip' });
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
exports.DelegateToolCalls = DelegateToolCalls;
|
|
108
|
+
exports.DelegateToolCalls = DelegateToolCalls = DelegateToolCalls_1 = __decorate([
|
|
109
|
+
(0, common_1.Injectable)(),
|
|
110
|
+
(0, common_2.Tool)({
|
|
111
|
+
uiConfig: {
|
|
112
|
+
description: 'Delegate tool calls from an LLM response. Handles both sync tools and async task tools.',
|
|
113
|
+
},
|
|
114
|
+
schema: DelegateToolCallsSchema,
|
|
115
|
+
})
|
|
116
|
+
], DelegateToolCalls);
|
|
117
|
+
//# sourceMappingURL=delegate-tool-calls.tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delegate-tool-calls.tool.js","sourceRoot":"","sources":["../../src/tools/delegate-tool-calls.tool.ts"],"names":[],"mappings":";;;;;;;;;;AACA,2CAAoD;AACpD,6BAAwB;AACxB,8CAQ2B;AAG3B,MAAM,uBAAuB,GAAG,OAAC,CAAC,MAAM,CAAC;IACvC,OAAO,EAAE,OAAC,CAAC,MAAM,CAAC;QAChB,EAAE,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;QACzB,OAAO,EAAE,OAAC,CAAC,KAAK,CAAC,OAAC,CAAC,GAAG,EAAE,CAAC;KAC1B,CAAC;IACF,QAAQ,EAAE,OAAC;SACR,MAAM,CAAgB,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,GAAG,KAAK,UAAU,IAAI,IAAA,iCAAwB,EAAC,GAAa,CAAC,KAAK,UAAU,CAAC;SACnH,QAAQ,EAAE;IACb,mBAAmB,EAAE,OAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE;IAC3C,QAAQ,EAAE,OAAC;SACR,MAAM,CAAC;QACN,UAAU,EAAE,OAAC,CAAC,MAAM,EAAE;KACvB,CAAC;SACD,QAAQ,EAAE;CACd,CAAC,CAAC;AAWI,IAAM,iBAAiB,yBAAvB,MAAM,iBAAkB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AAC,KAAgC,EAAE,OAAO,CAAC,CAAC;QAC1E,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,YAAY,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC5E,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,KAAK,CAAC,IAAI,aAAa,YAAY,EAAE,CAAC,CAAC;YAClE,OAAO;gBACL,IAAI,EAAE,YAAY;gBAClB,KAAK,EAAE,YAAY;aACpB,CAAC;QACJ,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,sBAAsB,CAClC,QAAuB,EACvB,IAA2B,EAC3B,WAAsC;QAEtC,MAAM,IAAI,CAAC,UAAU,CAAC,IAAI,CACxB,QAAQ,EACR;YACE,IAAI,EAAE,WAAW;YACjB,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO;YAC7B,WAAW;SACZ,EACD,EAAE,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAC1C,CAAC;IACJ,CAAC;CACF,CAAA;AAnGY,8CAAiB;4BAAjB,iBAAiB;IAP7B,IAAA,mBAAU,GAAE;IACZ,IAAA,aAAI,EAAC;QACJ,QAAQ,EAAE;YACR,WAAW,EAAE,yFAAyF;SACvG;QACD,MAAM,EAAE,uBAAuB;KAChC,CAAC;GACW,iBAAiB,CAmG7B"}
|
package/dist/tools/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './claude-generate-text.tool';
|
|
2
2
|
export * from './claude-generate-object.tool';
|
|
3
3
|
export * from './claude-generate-document.tool';
|
|
4
|
-
export * from './
|
|
4
|
+
export * from './delegate-tool-calls.tool';
|
|
5
|
+
export * from './update-tool-result.tool';
|
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iCAAiC,CAAC;AAChD,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iCAAiC,CAAC;AAChD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,2BAA2B,CAAC"}
|
package/dist/tools/index.js
CHANGED
|
@@ -17,5 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./claude-generate-text.tool"), exports);
|
|
18
18
|
__exportStar(require("./claude-generate-object.tool"), exports);
|
|
19
19
|
__exportStar(require("./claude-generate-document.tool"), exports);
|
|
20
|
-
__exportStar(require("./
|
|
20
|
+
__exportStar(require("./delegate-tool-calls.tool"), exports);
|
|
21
|
+
__exportStar(require("./update-tool-result.tool"), exports);
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
package/dist/tools/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8DAA4C;AAC5C,gEAA8C;AAC9C,kEAAgD;AAChD,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8DAA4C;AAC5C,gEAA8C;AAC9C,kEAAgD;AAChD,6DAA2C;AAC3C,4DAA0C"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { BaseTool, DocumentClass, ToolResult } from '@loopstack/common';
|
|
3
|
+
declare const UpdateToolResultSchema: z.ZodObject<{
|
|
4
|
+
delegateResult: z.ZodObject<{
|
|
5
|
+
allCompleted: z.ZodBoolean;
|
|
6
|
+
toolResults: z.ZodArray<z.ZodAny>;
|
|
7
|
+
message: z.ZodAny;
|
|
8
|
+
pendingCount: z.ZodNumber;
|
|
9
|
+
}, z.core.$strip>;
|
|
10
|
+
completedTool: z.ZodAny;
|
|
11
|
+
document: z.ZodOptional<z.ZodCustom<DocumentClass, DocumentClass>>;
|
|
12
|
+
}, z.core.$strip>;
|
|
13
|
+
type UpdateToolResultArgs = z.infer<typeof UpdateToolResultSchema>;
|
|
14
|
+
export declare class UpdateToolResult extends BaseTool {
|
|
15
|
+
private readonly logger;
|
|
16
|
+
call(args: UpdateToolResultArgs): Promise<ToolResult>;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=update-tool-result.tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"update-tool-result.tool.d.ts","sourceRoot":"","sources":["../../src/tools/update-tool-result.tool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAQ,UAAU,EAA0C,MAAM,mBAAmB,CAAC;AAEtH,QAAA,MAAM,sBAAsB;;;;;;;;;iBAW1B,CAAC;AAEH,KAAK,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAEnE,qBAOa,gBAAiB,SAAQ,QAAQ;IAC5C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqC;IAEtD,IAAI,CAAC,IAAI,EAAE,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC;CAqE5D"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var UpdateToolResult_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.UpdateToolResult = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const zod_1 = require("zod");
|
|
13
|
+
const common_2 = require("@loopstack/common");
|
|
14
|
+
const UpdateToolResultSchema = zod_1.z.object({
|
|
15
|
+
delegateResult: zod_1.z.object({
|
|
16
|
+
allCompleted: zod_1.z.boolean(),
|
|
17
|
+
toolResults: zod_1.z.array(zod_1.z.any()),
|
|
18
|
+
message: zod_1.z.any(),
|
|
19
|
+
pendingCount: zod_1.z.number(),
|
|
20
|
+
}),
|
|
21
|
+
completedTool: zod_1.z.any(),
|
|
22
|
+
document: zod_1.z
|
|
23
|
+
.custom((val) => typeof val === 'function' && (0, common_2.getBlockTypeFromMetadata)(val) === 'document')
|
|
24
|
+
.optional(),
|
|
25
|
+
});
|
|
26
|
+
let UpdateToolResult = UpdateToolResult_1 = class UpdateToolResult extends common_2.BaseTool {
|
|
27
|
+
logger = new common_1.Logger(UpdateToolResult_1.name);
|
|
28
|
+
async call(args) {
|
|
29
|
+
const { delegateResult } = args;
|
|
30
|
+
const completedToolRecord = args.completedTool;
|
|
31
|
+
const subscriberMetadata = completedToolRecord._subscriberMetadata;
|
|
32
|
+
if (!subscriberMetadata?.toolUseId || !subscriberMetadata?.toolName) {
|
|
33
|
+
throw new Error('Callback payload is missing _subscriberMetadata with toolUseId and toolName. ' +
|
|
34
|
+
'Ensure the event subscriber was registered with metadata by delegateToolCalls.');
|
|
35
|
+
}
|
|
36
|
+
const { toolUseId, toolName } = subscriberMetadata;
|
|
37
|
+
const tool = (0, common_2.getBlockTool)(this.ctx.parent, toolName);
|
|
38
|
+
if (!tool) {
|
|
39
|
+
throw new Error(`Tool with name ${toolName} not found.`);
|
|
40
|
+
}
|
|
41
|
+
let toolResult;
|
|
42
|
+
try {
|
|
43
|
+
toolResult = await tool.complete(completedToolRecord);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
47
|
+
this.logger.error(`Tool "${toolName}" complete() failed: ${errorMessage}`);
|
|
48
|
+
toolResult = {
|
|
49
|
+
data: errorMessage,
|
|
50
|
+
error: errorMessage,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
const completedEntry = {
|
|
54
|
+
type: 'tool_result',
|
|
55
|
+
tool_use_id: toolUseId,
|
|
56
|
+
content: toolResult.data ? JSON.stringify(toolResult.data, null, 2) : '',
|
|
57
|
+
is_error: !!toolResult.error,
|
|
58
|
+
};
|
|
59
|
+
const updatedResults = [...delegateResult.toolResults, completedEntry];
|
|
60
|
+
const pendingCount = delegateResult.pendingCount - 1;
|
|
61
|
+
const allCompleted = pendingCount === 0;
|
|
62
|
+
if (args.document) {
|
|
63
|
+
await this.repository.save(args.document, {
|
|
64
|
+
role: 'assistant',
|
|
65
|
+
content: delegateResult.message.content,
|
|
66
|
+
toolResults: updatedResults,
|
|
67
|
+
}, { id: delegateResult.message.id, validate: 'skip' });
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
data: {
|
|
71
|
+
...delegateResult,
|
|
72
|
+
toolResults: updatedResults,
|
|
73
|
+
allCompleted,
|
|
74
|
+
pendingCount,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
exports.UpdateToolResult = UpdateToolResult;
|
|
80
|
+
exports.UpdateToolResult = UpdateToolResult = UpdateToolResult_1 = __decorate([
|
|
81
|
+
(0, common_1.Injectable)(),
|
|
82
|
+
(0, common_2.Tool)({
|
|
83
|
+
uiConfig: {
|
|
84
|
+
description: 'Handle async tool completion callback. Updates the response document and tracks completion state.',
|
|
85
|
+
},
|
|
86
|
+
schema: UpdateToolResultSchema,
|
|
87
|
+
})
|
|
88
|
+
], UpdateToolResult);
|
|
89
|
+
//# sourceMappingURL=update-tool-result.tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"update-tool-result.tool.js","sourceRoot":"","sources":["../../src/tools/update-tool-result.tool.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAoD;AACpD,6BAAwB;AACxB,8CAAsH;AAEtH,MAAM,sBAAsB,GAAG,OAAC,CAAC,MAAM,CAAC;IACtC,cAAc,EAAE,OAAC,CAAC,MAAM,CAAC;QACvB,YAAY,EAAE,OAAC,CAAC,OAAO,EAAE;QACzB,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,OAAC,CAAC,GAAG,EAAE,CAAC;QAC7B,OAAO,EAAE,OAAC,CAAC,GAAG,EAAE;QAChB,YAAY,EAAE,OAAC,CAAC,MAAM,EAAE;KACzB,CAAC;IACF,aAAa,EAAE,OAAC,CAAC,GAAG,EAAE;IACtB,QAAQ,EAAE,OAAC;SACR,MAAM,CAAgB,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,GAAG,KAAK,UAAU,IAAI,IAAA,iCAAwB,EAAC,GAAa,CAAC,KAAK,UAAU,CAAC;SACnH,QAAQ,EAAE;CACd,CAAC,CAAC;AAWI,IAAM,gBAAgB,wBAAtB,MAAM,gBAAiB,SAAQ,iBAAQ;IAC3B,MAAM,GAAG,IAAI,eAAM,CAAC,kBAAgB,CAAC,IAAI,CAAC,CAAC;IAE5D,KAAK,CAAC,IAAI,CAAC,IAA0B;QACnC,MAAM,EAAE,cAAc,EAAE,GAAG,IAAI,CAAC;QAChC,MAAM,mBAAmB,GAAG,IAAI,CAAC,aAAwC,CAAC;QAG1E,MAAM,kBAAk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cAAc,GAAG,CAAC,GAAI,cAAc,CAAC,WAAyC,EAAE,cAAc,CAAC,CAAC;QAGtG,MAAM,YAAY,GAAG,cAAc,CAAC,YAAY,GAAG,CAAC,CAAC;QACrD,MAAM,YAAY,GAAG,YAAY,KAAK,CAAC,CAAC;QAGxC,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,MAAM,IAAI,CAAC,UAAU,CAAC,IAAI,CACxB,IAAI,CAAC,QAAQ,EACb;gBACE,IAAI,EAAE,WAAW;gBACjB,OAAO,EAAG,cAAc,CAAC,OAAmC,CAAC,OAAO;gBACpE,WAAW,EAAE,cAAc;aAC5B,EACD,EAAE,EAAE,EAAG,cAAc,CAAC,OAAmC,CAAC,EAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,CAC3F,CAAC;QACJ,CAAC;QAED,OAAO;YACL,IAAI,EAAE;gBACJ,GAAG,cAAc;gBACjB,WAAW,EAAE,cAAc;gBAC3B,YAAY;gBACZ,YAAY;aACb;SACF,CAAC;IACJ,CAAC;CACF,CAAA;AAxEY,4CAAgB;2BAAhB,gBAAgB;IAP5B,IAAA,mBAAU,GAAE;IACZ,IAAA,aAAI,EAAC;QACJ,QAAQ,EAAE;YACR,WAAW,EAAE,mGAAmG;SACjH;QACD,MAAM,EAAE,sBAAsB;KAC/B,CAAC;GACW,gBAAgB,CAwE5B"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
+
import type { ToolCallsMap } from '@loopstack/common';
|
|
2
3
|
export interface ClaudeModelConfig {
|
|
3
4
|
model?: string;
|
|
4
5
|
envApiKey?: string;
|
|
@@ -15,5 +16,23 @@ export interface ClaudeToolDefinition {
|
|
|
15
16
|
description: string;
|
|
16
17
|
input_schema: Anthropic.Tool['input_schema'];
|
|
17
18
|
}
|
|
19
|
+
export interface ClaudeGenerateTextResult extends Anthropic.Message {
|
|
20
|
+
toolCalls?: ToolCallsMap;
|
|
21
|
+
}
|
|
22
|
+
export interface DelegateToolCallsResult {
|
|
23
|
+
allCompleted: boolean;
|
|
24
|
+
toolResults: DelegateToolResultEntry[];
|
|
25
|
+
message: {
|
|
26
|
+
id?: string;
|
|
27
|
+
content: Anthropic.ContentBlock[];
|
|
28
|
+
};
|
|
29
|
+
pendingCount: number;
|
|
30
|
+
}
|
|
31
|
+
export interface DelegateToolResultEntry {
|
|
32
|
+
type: 'tool_result';
|
|
33
|
+
tool_use_id: string;
|
|
34
|
+
content?: string;
|
|
35
|
+
is_error?: boolean;
|
|
36
|
+
}
|
|
18
37
|
export { default as Anthropic } from '@anthropic-ai/sdk';
|
|
19
38
|
//# sourceMappingURL=claude.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claude.types.d.ts","sourceRoot":"","sources":["../../src/types/claude.types.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"claude.types.d.ts","sourceRoot":"","sources":["../../src/types/claude.types.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAC1C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAKtD,MAAM,WAAW,iBAAiB;IAEhC,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAKD,MAAM,WAAW,qBAAqB;IACpC,QAAQ,EAAE,SAAS,CAAC,YAAY,EAAE,CAAC;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,SAAS,CAAC,IAAI,EAAE,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,SAAS,CAAC,mBAAmB,CAAC,aAAa,CAAC,CAAC;CAC3D;AAKD,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,SAAS,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;CAC9C;AAMD,MAAM,WAAW,wBAAyB,SAAQ,SAAS,CAAC,OAAO;IACjE,SAAS,CAAC,EAAE,YAAY,CAAC;CAC1B;AAKD,MAAM,WAAW,uBAAuB;IACtC,YAAY,EAAE,OAAO,CAAC;IACtB,WAAW,EAAE,uBAAuB,EAAE,CAAC;IACvC,OAAO,EAAE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,SAAS,CAAC,YAAY,EAAE,CAAA;KAAE,CAAC;IAC5D,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,aAAa,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAGD,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claude.types.js","sourceRoot":"","sources":["../../src/types/claude.types.ts"],"names":[],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"claude.types.js","sourceRoot":"","sources":["../../src/types/claude.types.ts"],"names":[],"mappings":";;;;;;AA2DA,yCAAyD;AAAhD,iHAAA,OAAO,OAAa"}
|
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"loopstack",
|
|
11
11
|
"tool"
|
|
12
12
|
],
|
|
13
|
-
"version": "0.
|
|
13
|
+
"version": "0.22.0",
|
|
14
14
|
"license": "Apache-2.0",
|
|
15
15
|
"author": {
|
|
16
16
|
"name": "Jakob Klippel",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@anthropic-ai/sdk": "^0.39.0",
|
|
35
|
-
"@loopstack/common": "^0.
|
|
36
|
-
"@loopstack/core
|
|
35
|
+
"@loopstack/common": "^0.25.0",
|
|
36
|
+
"@loopstack/core": "^0.25.0",
|
|
37
37
|
"@nestjs/common": "^11.1.14",
|
|
38
38
|
"@nestjs/config": "^4.0.3",
|
|
39
39
|
"@nestjs/core": "^11.1.14",
|
package/src/claude.module.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { Module } from '@nestjs/common';
|
|
2
|
-
import {
|
|
3
|
-
import { ClaudeMessageDocument } from './documents';
|
|
2
|
+
import { LoopCoreModule } from '@loopstack/core';
|
|
4
3
|
import { ClaudeClientService, ClaudeMessagesHelperService, ClaudeToolsHelperService } from './services';
|
|
5
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
ClaudeGenerateDocument,
|
|
6
|
+
ClaudeGenerateObject,
|
|
7
|
+
ClaudeGenerateText,
|
|
8
|
+
DelegateToolCalls,
|
|
9
|
+
UpdateToolResult,
|
|
10
|
+
} from './tools';
|
|
6
11
|
|
|
7
12
|
@Module({
|
|
8
|
-
imports: [
|
|
13
|
+
imports: [LoopCoreModule],
|
|
9
14
|
providers: [
|
|
10
15
|
// services
|
|
11
16
|
ClaudeClientService,
|
|
@@ -16,17 +21,9 @@ import { ClaudeDelegateToolCall, ClaudeGenerateDocument, ClaudeGenerateObject, C
|
|
|
16
21
|
ClaudeGenerateText,
|
|
17
22
|
ClaudeGenerateObject,
|
|
18
23
|
ClaudeGenerateDocument,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// documents
|
|
22
|
-
ClaudeMessageDocument,
|
|
23
|
-
],
|
|
24
|
-
exports: [
|
|
25
|
-
ClaudeGenerateText,
|
|
26
|
-
ClaudeGenerateObject,
|
|
27
|
-
ClaudeGenerateDocument,
|
|
28
|
-
ClaudeDelegateToolCall,
|
|
29
|
-
ClaudeMessageDocument,
|
|
24
|
+
DelegateToolCalls,
|
|
25
|
+
UpdateToolResult,
|
|
30
26
|
],
|
|
27
|
+
exports: [ClaudeGenerateText, ClaudeGenerateObject, ClaudeGenerateDocument, DelegateToolCalls, UpdateToolResult],
|
|
31
28
|
})
|
|
32
29
|
export class ClaudeModule {}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
2
1
|
import { z } from 'zod';
|
|
3
|
-
import { Document
|
|
2
|
+
import { Document } from '@loopstack/common';
|
|
4
3
|
|
|
5
4
|
export const ClaudeMessageDocumentSchema = z.object({
|
|
6
5
|
role: z.enum(['user', 'assistant']),
|
|
@@ -10,17 +9,12 @@ export const ClaudeMessageDocumentSchema = z.object({
|
|
|
10
9
|
|
|
11
10
|
export type ClaudeMessageDocumentContentType = z.infer<typeof ClaudeMessageDocumentSchema>;
|
|
12
11
|
|
|
13
|
-
@Injectable()
|
|
14
12
|
@Document({
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
description: 'Claude Message Document. Stores messages in Anthropic native format.',
|
|
18
|
-
},
|
|
19
|
-
configFile: __dirname + '/claude-message-document.yaml',
|
|
13
|
+
schema: ClaudeMessageDocumentSchema,
|
|
14
|
+
uiConfig: __dirname + '/claude-message-document.yaml',
|
|
20
15
|
})
|
|
21
16
|
export class ClaudeMessageDocument {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
content: z.infer<typeof ClaudeMessageDocumentSchema>;
|
|
17
|
+
role: 'user' | 'assistant';
|
|
18
|
+
content: string | any[];
|
|
19
|
+
toolResults?: any[];
|
|
26
20
|
}
|
package/src/index.ts
CHANGED
|
@@ -22,15 +22,13 @@ export class ClaudeToolsHelperService {
|
|
|
22
22
|
throw new Error(`Block ${tool.constructor.name} is missing @BlockConfig decorator`);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
const jsonSchema = toJSONSchema(inputSchema);
|
|
25
|
+
const jsonSchema = inputSchema ? toJSONSchema(inputSchema) : { type: 'object' };
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
27
|
+
toolDefinitions.push({
|
|
28
|
+
name: toolName,
|
|
29
|
+
description: config.description ?? '',
|
|
30
|
+
input_schema: jsonSchema as Anthropic.Tool['input_schema'],
|
|
31
|
+
});
|
|
34
32
|
}
|
|
35
33
|
|
|
36
34
|
return toolDefinitions.length ? toolDefinitions : undefined;
|
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
InjectTool,
|
|
3
|
-
Input,
|
|
4
|
-
RunContext,
|
|
5
|
-
Tool,
|
|
6
|
-
ToolInterface,
|
|
7
|
-
ToolResult,
|
|
8
|
-
WorkflowInterface,
|
|
9
|
-
WorkflowMetadataInterface,
|
|
10
|
-
getBlockTool,
|
|
11
|
-
} from '@loopstack/common';
|
|
12
|
-
import { CreateDocument } from '@loopstack/core-ui-module';
|
|
1
|
+
import { BaseTool, InjectTool, Tool, ToolResult } from '@loopstack/common';
|
|
13
2
|
import {
|
|
14
3
|
ClaudeGenerateObject,
|
|
15
4
|
ClaudeGenerateObjectArgsType,
|
|
@@ -17,49 +6,23 @@ import {
|
|
|
17
6
|
} from './claude-generate-object.tool';
|
|
18
7
|
|
|
19
8
|
@Tool({
|
|
20
|
-
|
|
9
|
+
uiConfig: {
|
|
21
10
|
description: 'Generates a structured object using the Anthropic Claude API and creates it as a document',
|
|
22
11
|
},
|
|
12
|
+
schema: ClaudeGenerateObjectSchema,
|
|
23
13
|
})
|
|
24
|
-
export class ClaudeGenerateDocument
|
|
14
|
+
export class ClaudeGenerateDocument extends BaseTool {
|
|
25
15
|
@InjectTool() private claudeGenerateObject!: ClaudeGenerateObject;
|
|
26
|
-
@InjectTool() private createDocument!: CreateDocument;
|
|
27
|
-
|
|
28
|
-
@Input({
|
|
29
|
-
schema: ClaudeGenerateObjectSchema,
|
|
30
|
-
})
|
|
31
|
-
args: ClaudeGenerateObjectArgsType;
|
|
32
|
-
|
|
33
|
-
private getRequiredTool(name: string): ToolInterface {
|
|
34
|
-
const tool = getBlockTool<ToolInterface>(this, name);
|
|
35
|
-
if (tool === undefined) {
|
|
36
|
-
throw new Error(`Tool "${name}" is not available`);
|
|
37
|
-
}
|
|
38
|
-
return tool;
|
|
39
|
-
}
|
|
40
16
|
|
|
41
|
-
async
|
|
42
|
-
args
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const generateResult = await this.getRequiredTool('claudeGenerateObject').execute(args, ctx, parent, metadata);
|
|
48
|
-
const documentResult = await this.getRequiredTool('createDocument').execute(
|
|
49
|
-
{
|
|
50
|
-
id: args.response.id,
|
|
51
|
-
document: args.response.document,
|
|
52
|
-
update: {
|
|
53
|
-
content: generateResult.data as unknown,
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
ctx,
|
|
57
|
-
parent,
|
|
58
|
-
metadata,
|
|
59
|
-
);
|
|
17
|
+
async call(args: ClaudeGenerateObjectArgsType): Promise<ToolResult> {
|
|
18
|
+
const generateResult = await this.claudeGenerateObject.call(args);
|
|
19
|
+
const entity = await this.repository.save(args.response.document, generateResult.data as Record<string, unknown>, {
|
|
20
|
+
id: args.response.id,
|
|
21
|
+
validate: 'skip',
|
|
22
|
+
});
|
|
60
23
|
|
|
61
24
|
return {
|
|
62
|
-
|
|
25
|
+
data: entity,
|
|
63
26
|
metadata: generateResult.metadata,
|
|
64
27
|
};
|
|
65
28
|
}
|
|
@@ -2,16 +2,12 @@ import Anthropic from '@anthropic-ai/sdk';
|
|
|
2
2
|
import { Inject } from '@nestjs/common';
|
|
3
3
|
import { toJSONSchema, z } from 'zod';
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
RunContext,
|
|
5
|
+
BaseTool,
|
|
6
|
+
DocumentClass,
|
|
8
7
|
Tool,
|
|
9
|
-
ToolInterface,
|
|
10
8
|
ToolResult,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
getBlockArgsSchema,
|
|
14
|
-
getBlockDocument,
|
|
9
|
+
getBlockTypeFromMetadata,
|
|
10
|
+
getDocumentSchema,
|
|
15
11
|
} from '@loopstack/common';
|
|
16
12
|
import { ClaudeGenerateToolBaseSchema } from '../schemas/claude-generate-tool-base.schema';
|
|
17
13
|
import { ClaudeClientService } from '../services';
|
|
@@ -21,7 +17,9 @@ import { applyCacheBreakpoints } from '../utils/cache.utils';
|
|
|
21
17
|
export const ClaudeGenerateObjectSchema = ClaudeGenerateToolBaseSchema.extend({
|
|
22
18
|
response: z.object({
|
|
23
19
|
id: z.string().optional(),
|
|
24
|
-
document: z.
|
|
20
|
+
document: z.custom<DocumentClass>(
|
|
21
|
+
(val) => typeof val === 'function' && getBlockTypeFromMetadata(val as object) === 'document',
|
|
22
|
+
),
|
|
25
23
|
}),
|
|
26
24
|
}).strict();
|
|
27
25
|
|
|
@@ -30,27 +28,18 @@ export type ClaudeGenerateObjectArgsType = z.infer<typeof ClaudeGenerateObjectSc
|
|
|
30
28
|
const STRUCTURED_OUTPUT_TOOL_NAME = 'structured_output';
|
|
31
29
|
|
|
32
30
|
@Tool({
|
|
33
|
-
|
|
31
|
+
uiConfig: {
|
|
34
32
|
description: 'Generates a structured object using the Anthropic Claude API',
|
|
35
33
|
},
|
|
34
|
+
schema: ClaudeGenerateObjectSchema,
|
|
36
35
|
})
|
|
37
|
-
export class ClaudeGenerateObject
|
|
36
|
+
export class ClaudeGenerateObject extends BaseTool {
|
|
38
37
|
@Inject()
|
|
39
38
|
private readonly claudeClientService: ClaudeClientService;
|
|
40
39
|
@Inject()
|
|
41
40
|
private readonly claudeMessagesHelperService: ClaudeMessagesHelperService;
|
|
42
41
|
|
|
43
|
-
|
|
44
|
-
schema: ClaudeGenerateObjectSchema,
|
|
45
|
-
})
|
|
46
|
-
args: ClaudeGenerateObjectArgsType;
|
|
47
|
-
|
|
48
|
-
async execute(
|
|
49
|
-
args: ClaudeGenerateObjectArgsType,
|
|
50
|
-
ctx: RunContext,
|
|
51
|
-
parent: WorkflowInterface,
|
|
52
|
-
metadata: WorkflowMetadataInterface,
|
|
53
|
-
): Promise<ToolResult> {
|
|
42
|
+
async call(args: ClaudeGenerateObjectArgsType): Promise<ToolResult> {
|
|
54
43
|
const client = this.claudeClientService.getClient(args.claude);
|
|
55
44
|
const model = this.claudeClientService.getModel(args.claude);
|
|
56
45
|
|
|
@@ -59,19 +48,14 @@ export class ClaudeGenerateObject implements ToolInterface<ClaudeGenerateObjectA
|
|
|
59
48
|
if (args.prompt) {
|
|
60
49
|
messages.push({ role: 'user', content: args.prompt });
|
|
61
50
|
} else {
|
|
62
|
-
const resolved = this.claudeMessagesHelperService.getMessages(
|
|
51
|
+
const resolved = this.claudeMessagesHelperService.getMessages(this.ctx.runtime.documents, {
|
|
63
52
|
messages: args.messages as Anthropic.MessageParam[],
|
|
64
53
|
messagesSearchTag: args.messagesSearchTag,
|
|
65
54
|
});
|
|
66
55
|
messages.push(...resolved);
|
|
67
56
|
}
|
|
68
57
|
|
|
69
|
-
const
|
|
70
|
-
if (!document) {
|
|
71
|
-
throw new Error(`Document with name "${args.response.document}" not found in tool execution context.`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const responseSchema = getBlockArgsSchema(document);
|
|
58
|
+
const responseSchema = getDocumentSchema(args.response.document);
|
|
75
59
|
if (!responseSchema) {
|
|
76
60
|
throw new Error('Claude object generation source document must have a schema.');
|
|
77
61
|
}
|