@loopstack/observability-examples 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 +67 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/observability-examples.module.d.ts +3 -0
- package/dist/observability-examples.module.d.ts.map +1 -0
- package/dist/observability-examples.module.js +28 -0
- package/dist/observability-examples.module.js.map +1 -0
- package/dist/workflows/quota/quota-example.workflow.d.ts +22 -0
- package/dist/workflows/quota/quota-example.workflow.d.ts.map +1 -0
- package/dist/workflows/quota/quota-example.workflow.js +78 -0
- package/dist/workflows/quota/quota-example.workflow.js.map +1 -0
- package/package.json +47 -0
- package/src/index.ts +2 -0
- package/src/observability-examples.module.ts +15 -0
- package/src/workflows/quota/quota-example.workflow.ts +67 -0
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Observability Examples
|
|
3
|
+
description: Workflow examples for observability in Loopstack — opt-in quota tracking and enforcement
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# @loopstack/observability-examples
|
|
7
|
+
|
|
8
|
+
> Observability workflow examples for the [Loopstack](https://loopstack.ai) automation framework.
|
|
9
|
+
|
|
10
|
+
Workflow examples for tracking and enforcing operational limits.
|
|
11
|
+
|
|
12
|
+
## Install as Source (Recommended)
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npx giget@latest gh:loopstack-ai/loopstack/registry/examples/observability-examples src/observability-examples
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Register the module:
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { Module } from '@nestjs/common';
|
|
22
|
+
import { LoopstackModule } from '@loopstack/loopstack-module';
|
|
23
|
+
import { ObservabilityExamplesModule } from './observability-examples/observability-examples.module';
|
|
24
|
+
|
|
25
|
+
@Module({
|
|
26
|
+
imports: [LoopstackModule.forRoot(), ObservabilityExamplesModule],
|
|
27
|
+
})
|
|
28
|
+
export class AppModule {}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Install as a Dependency
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install @loopstack/observability-examples
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { ObservabilityExamplesModule } from '@loopstack/observability-examples';
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Examples
|
|
42
|
+
|
|
43
|
+
| Example | Studio title | Description |
|
|
44
|
+
| --------------- | ------------------------------- | ----------------------------------------------------------------------- |
|
|
45
|
+
| [Quota](#quota) | `Observability - Quota Example` | Check + report usage with `@loopstack/quota`, Redis-backed when enabled |
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Quota
|
|
50
|
+
|
|
51
|
+
Demonstrates opt-in quota tracking using `@loopstack/quota`:
|
|
52
|
+
|
|
53
|
+
1. `quotaClient.checkQuota(userId, quotaType)` — verify available budget
|
|
54
|
+
2. Perform the (placeholder) operation if allowed
|
|
55
|
+
3. `quotaClient.report(userId, quotaType, amount)` — charge against the quota
|
|
56
|
+
|
|
57
|
+
When `QuotaModule.forRoot({ enabled: false })`, both calls no-op. Set `enabled: true` plus Redis connection options to enforce real limits.
|
|
58
|
+
|
|
59
|
+
### Files
|
|
60
|
+
|
|
61
|
+
- `quota-example.workflow.ts` — workflow class
|
|
62
|
+
|
|
63
|
+
## About
|
|
64
|
+
|
|
65
|
+
Author: [Jakob Klippel](https://www.linkedin.com/in/jakob-klippel/)
|
|
66
|
+
|
|
67
|
+
License: MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC;AAChD,cAAc,0CAA0C,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./observability-examples.module"), exports);
|
|
18
|
+
__exportStar(require("./workflows/quota/quota-example.workflow"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kEAAgD;AAChD,2EAAyD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observability-examples.module.d.ts","sourceRoot":"","sources":["../src/observability-examples.module.ts"],"names":[],"mappings":"AAKA,qBASa,2BAA2B;CAAG"}
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.ObservabilityExamplesModule = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const common_2 = require("@loopstack/common");
|
|
12
|
+
const quota_1 = require("@loopstack/quota");
|
|
13
|
+
const quota_example_workflow_1 = require("./workflows/quota/quota-example.workflow");
|
|
14
|
+
let ObservabilityExamplesModule = class ObservabilityExamplesModule {
|
|
15
|
+
};
|
|
16
|
+
exports.ObservabilityExamplesModule = ObservabilityExamplesModule;
|
|
17
|
+
exports.ObservabilityExamplesModule = ObservabilityExamplesModule = __decorate([
|
|
18
|
+
(0, common_2.StudioApp)({
|
|
19
|
+
title: 'Observability Examples',
|
|
20
|
+
workflows: [quota_example_workflow_1.QuotaExampleWorkflow],
|
|
21
|
+
}),
|
|
22
|
+
(0, common_1.Module)({
|
|
23
|
+
imports: [quota_1.QuotaModule.forRoot({ enabled: false })],
|
|
24
|
+
providers: [quota_example_workflow_1.QuotaExampleWorkflow],
|
|
25
|
+
exports: [quota_example_workflow_1.QuotaExampleWorkflow],
|
|
26
|
+
})
|
|
27
|
+
], ObservabilityExamplesModule);
|
|
28
|
+
//# sourceMappingURL=observability-examples.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observability-examples.module.js","sourceRoot":"","sources":["../src/observability-examples.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAwC;AACxC,8CAA8C;AAC9C,4CAA+C;AAC/C,qFAAgF;AAWzE,IAAM,2BAA2B,GAAjC,MAAM,2BAA2B;CAAG,CAAA;AAA9B,kEAA2B;sCAA3B,2BAA2B;IATvC,IAAA,kBAAS,EAAC;QACT,KAAK,EAAE,wBAAwB;QAC/B,SAAS,EAAE,CAAC,6CAAoB,CAAC;KAClC,CAAC;IACD,IAAA,eAAM,EAAC;QACN,OAAO,EAAE,CAAC,mBAAW,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;QAClD,SAAS,EAAE,CAAC,6CAAoB,CAAC;QACjC,OAAO,EAAE,CAAC,6CAAoB,CAAC;KAChC,CAAC;GACW,2BAA2B,CAAG"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { BaseWorkflow } from '@loopstack/common';
|
|
3
|
+
import type { RunContext } from '@loopstack/common';
|
|
4
|
+
import { QuotaClientService } from '@loopstack/quota';
|
|
5
|
+
interface QuotaExampleState {
|
|
6
|
+
hasQuota?: boolean;
|
|
7
|
+
used?: number;
|
|
8
|
+
limit?: number;
|
|
9
|
+
}
|
|
10
|
+
declare const QuotaExampleArgsSchema: z.ZodObject<{
|
|
11
|
+
quotaType: z.ZodDefault<z.ZodString>;
|
|
12
|
+
reportAmount: z.ZodDefault<z.ZodNumber>;
|
|
13
|
+
}, z.core.$strip>;
|
|
14
|
+
type QuotaExampleArgs = z.infer<typeof QuotaExampleArgsSchema>;
|
|
15
|
+
export declare class QuotaExampleWorkflow extends BaseWorkflow<QuotaExampleArgs> {
|
|
16
|
+
private readonly quotaClient;
|
|
17
|
+
constructor(quotaClient: QuotaClientService);
|
|
18
|
+
checkQuota(_state: QuotaExampleState, ctx: RunContext<QuotaExampleArgs>): Promise<void>;
|
|
19
|
+
reportUsage(_state: QuotaExampleState, ctx: RunContext<QuotaExampleArgs>): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=quota-example.workflow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quota-example.workflow.d.ts","sourceRoot":"","sources":["../../../src/workflows/quota/quota-example.workflow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,YAAY,EAA0C,MAAM,mBAAmB,CAAC;AACzF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEtD,UAAU,iBAAiB;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,QAAA,MAAM,sBAAsB;;;iBAG1B,CAAC;AAEH,KAAK,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAE/D,qBAMa,oBAAqB,SAAQ,YAAY,CAAC,gBAAgB,CAAC;IAC1D,OAAO,CAAC,QAAQ,CAAC,WAAW;gBAAX,WAAW,EAAE,kBAAkB;IAKtD,UAAU,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,EAAE,UAAU,CAAC,gBAAgB,CAAC;IAkBvE,WAAW,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,EAAE,UAAU,CAAC,gBAAgB,CAAC;CAkB/E"}
|
|
@@ -0,0 +1,78 @@
|
|
|
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 __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.QuotaExampleWorkflow = void 0;
|
|
13
|
+
const zod_1 = require("zod");
|
|
14
|
+
const common_1 = require("@loopstack/common");
|
|
15
|
+
const quota_1 = require("@loopstack/quota");
|
|
16
|
+
const QuotaExampleArgsSchema = zod_1.z.object({
|
|
17
|
+
quotaType: zod_1.z.string().default('ai_generate_text'),
|
|
18
|
+
reportAmount: zod_1.z.number().default(1000),
|
|
19
|
+
});
|
|
20
|
+
let QuotaExampleWorkflow = class QuotaExampleWorkflow extends common_1.BaseWorkflow {
|
|
21
|
+
quotaClient;
|
|
22
|
+
constructor(quotaClient) {
|
|
23
|
+
super();
|
|
24
|
+
this.quotaClient = quotaClient;
|
|
25
|
+
}
|
|
26
|
+
async checkQuota(_state, ctx) {
|
|
27
|
+
const result = await this.quotaClient.checkQuota(ctx.userId ?? 'anonymous', ctx.args.quotaType);
|
|
28
|
+
const allowed = !result.exceeded;
|
|
29
|
+
await this.documentStore.save(common_1.MarkdownDocument, {
|
|
30
|
+
markdown: [
|
|
31
|
+
'## Quota Check',
|
|
32
|
+
'',
|
|
33
|
+
`- Allowed: \`${allowed}\``,
|
|
34
|
+
`- Used: \`${result.used}\``,
|
|
35
|
+
`- Limit: \`${result.limit}\``,
|
|
36
|
+
].join('\n'),
|
|
37
|
+
});
|
|
38
|
+
this.assignState({ hasQuota: allowed, used: result.used, limit: result.limit });
|
|
39
|
+
}
|
|
40
|
+
async reportUsage(_state, ctx) {
|
|
41
|
+
if (!_state.hasQuota) {
|
|
42
|
+
await this.documentStore.save(common_1.MarkdownDocument, {
|
|
43
|
+
markdown: '## Skipped\n\nQuota exhausted — operation not performed.',
|
|
44
|
+
});
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
await this.quotaClient.report(ctx.userId ?? 'anonymous', ctx.args.quotaType, ctx.args.reportAmount);
|
|
48
|
+
await this.documentStore.save(common_1.MarkdownDocument, {
|
|
49
|
+
markdown: [
|
|
50
|
+
'## Reported',
|
|
51
|
+
'',
|
|
52
|
+
`Charged \`${ctx.args.reportAmount}\` units of \`${ctx.args.quotaType}\` to the user's quota.`,
|
|
53
|
+
].join('\n'),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
exports.QuotaExampleWorkflow = QuotaExampleWorkflow;
|
|
58
|
+
__decorate([
|
|
59
|
+
(0, common_1.Transition)({ to: 'quota_checked' }),
|
|
60
|
+
__metadata("design:type", Function),
|
|
61
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
62
|
+
__metadata("design:returntype", Promise)
|
|
63
|
+
], QuotaExampleWorkflow.prototype, "checkQuota", null);
|
|
64
|
+
__decorate([
|
|
65
|
+
(0, common_1.Transition)({ from: 'quota_checked', to: 'end' }),
|
|
66
|
+
__metadata("design:type", Function),
|
|
67
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
68
|
+
__metadata("design:returntype", Promise)
|
|
69
|
+
], QuotaExampleWorkflow.prototype, "reportUsage", null);
|
|
70
|
+
exports.QuotaExampleWorkflow = QuotaExampleWorkflow = __decorate([
|
|
71
|
+
(0, common_1.Workflow)({
|
|
72
|
+
title: 'Observability - Quota Example',
|
|
73
|
+
description: 'Demonstrates opt-in quota tracking with @loopstack/quota — check available budget before an operation, then report usage afterward. Backed by Redis when enabled in QuotaModule.forRoot.',
|
|
74
|
+
schema: QuotaExampleArgsSchema,
|
|
75
|
+
}),
|
|
76
|
+
__metadata("design:paramtypes", [quota_1.QuotaClientService])
|
|
77
|
+
], QuotaExampleWorkflow);
|
|
78
|
+
//# sourceMappingURL=quota-example.workflow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quota-example.workflow.js","sourceRoot":"","sources":["../../../src/workflows/quota/quota-example.workflow.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6BAAwB;AACxB,8CAAyF;AAEzF,4CAAsD;AAQtD,MAAM,sBAAsB,GAAG,OAAC,CAAC,MAAM,CAAC;IACtC,SAAS,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,kBAAkB,CAAC;IACjD,YAAY,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;CACvC,CAAC,CAAC;AAUI,IAAM,oBAAoB,GAA1B,MAAM,oBAAqB,SAAQ,qBAA8B;IACzC;IAA7B,YAA6B,WAA+B;QAC1D,KAAK,EAAE,CAAC;QADmB,gBAAW,GAAX,WAAW,CAAoB;IAE5D,CAAC;IAGK,AAAN,KAAK,CAAC,UAAU,CAAC,MAAyB,EAAE,GAAiC;QAC3E,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,IAAI,WAAW,EAAE,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAEhG,MAAM,OAAO,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC;QACjC,MAAM,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,yBAAgB,EAAE;YAC9C,QAAQ,EAAE;gBACR,gBAAgB;gBAChB,EAAE;gBACF,gBAAgB,OAAO,IAAI;gBAC3B,aAAa,MAAM,CAAC,IAAI,IAAI;gBAC5B,cAAc,MAAM,CAAC,KAAK,IAAI;aAC/B,CAAC,IAAI,CAAC,IAAI,CAAC;SACb,CAAC,CAAC;QAEH,IAAI,CAAC,WAAW,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;IAClF,CAAC;IAGK,AAAN,KAAK,CAAC,WAAW,CAAC,MAAyB,EAAE,GAAiC;QAC5E,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;YACrB,MAAM,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,yBAAgB,EAAE;gBAC9C,QAAQ,EAAE,0DAA0D;aACrE,CAAC,CAAC;YACH,OAAO;QACT,CAAC;QAED,MAAM,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,IAAI,WAAW,EAAE,GAAG,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QAEpG,MAAM,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,yBAAgB,EAAE;YAC9C,QAAQ,EAAE;gBACR,aAAa;gBACb,EAAE;gBACF,aAAa,GAAG,CAAC,IAAI,CAAC,YAAY,iBAAiB,GAAG,CAAC,IAAI,CAAC,SAAS,yBAAyB;aAC/F,CAAC,IAAI,CAAC,IAAI,CAAC;SACb,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AA1CY,oDAAoB;AAMzB;IADL,IAAA,mBAAU,EAAC,EAAE,EAAE,EAAE,eAAe,EAAE,CAAC;;;;sDAgBnC;AAGK;IADL,IAAA,mBAAU,EAAC,EAAE,IAAI,EAAE,eAAe,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC;;;;uDAkBhD;+BAzCU,oBAAoB;IANhC,IAAA,iBAAQ,EAAC;QACR,KAAK,EAAE,+BAA+B;QACtC,WAAW,EACT,0LAA0L;QAC5L,MAAM,EAAE,sBAAsB;KAC/B,CAAC;qCAE0C,0BAAkB;GADjD,oBAAoB,CA0ChC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@loopstack/observability-examples",
|
|
3
|
+
"displayName": "Loopstack Observability Examples",
|
|
4
|
+
"description": "Workflow examples for observability in Loopstack — opt-in quota tracking and enforcement.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"loopstack",
|
|
7
|
+
"example",
|
|
8
|
+
"observability",
|
|
9
|
+
"quota",
|
|
10
|
+
"workflow"
|
|
11
|
+
],
|
|
12
|
+
"version": "0.1.0",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": {
|
|
15
|
+
"name": "Jakob Klippel",
|
|
16
|
+
"url": "https://www.linkedin.com/in/jakob-klippel/"
|
|
17
|
+
},
|
|
18
|
+
"main": "dist/index.js",
|
|
19
|
+
"types": "dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./dist/index.js",
|
|
22
|
+
"./src/*": "./src/*"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "nest build",
|
|
26
|
+
"compile": "tsc --noEmit",
|
|
27
|
+
"format": "prettier --write .",
|
|
28
|
+
"lint": "eslint .",
|
|
29
|
+
"test": "vitest run --passWithNoTests",
|
|
30
|
+
"watch": "nest build --watch"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@loopstack/common": "*",
|
|
34
|
+
"@loopstack/quota": "*",
|
|
35
|
+
"@nestjs/common": "^11.1.19",
|
|
36
|
+
"zod": "^4.3.6"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist",
|
|
40
|
+
"src"
|
|
41
|
+
],
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"vitest": "^4.1.6",
|
|
44
|
+
"@swc/core": "^1.15.33",
|
|
45
|
+
"unplugin-swc": "^1.5.9"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
import { StudioApp } from '@loopstack/common';
|
|
3
|
+
import { QuotaModule } from '@loopstack/quota';
|
|
4
|
+
import { QuotaExampleWorkflow } from './workflows/quota/quota-example.workflow';
|
|
5
|
+
|
|
6
|
+
@StudioApp({
|
|
7
|
+
title: 'Observability Examples',
|
|
8
|
+
workflows: [QuotaExampleWorkflow],
|
|
9
|
+
})
|
|
10
|
+
@Module({
|
|
11
|
+
imports: [QuotaModule.forRoot({ enabled: false })],
|
|
12
|
+
providers: [QuotaExampleWorkflow],
|
|
13
|
+
exports: [QuotaExampleWorkflow],
|
|
14
|
+
})
|
|
15
|
+
export class ObservabilityExamplesModule {}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { BaseWorkflow, MarkdownDocument, Transition, Workflow } from '@loopstack/common';
|
|
3
|
+
import type { RunContext } from '@loopstack/common';
|
|
4
|
+
import { QuotaClientService } from '@loopstack/quota';
|
|
5
|
+
|
|
6
|
+
interface QuotaExampleState {
|
|
7
|
+
hasQuota?: boolean;
|
|
8
|
+
used?: number;
|
|
9
|
+
limit?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const QuotaExampleArgsSchema = z.object({
|
|
13
|
+
quotaType: z.string().default('ai_generate_text'),
|
|
14
|
+
reportAmount: z.number().default(1000),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
type QuotaExampleArgs = z.infer<typeof QuotaExampleArgsSchema>;
|
|
18
|
+
|
|
19
|
+
@Workflow({
|
|
20
|
+
title: 'Observability - Quota Example',
|
|
21
|
+
description:
|
|
22
|
+
'Demonstrates opt-in quota tracking with @loopstack/quota — check available budget before an operation, then report usage afterward. Backed by Redis when enabled in QuotaModule.forRoot.',
|
|
23
|
+
schema: QuotaExampleArgsSchema,
|
|
24
|
+
})
|
|
25
|
+
export class QuotaExampleWorkflow extends BaseWorkflow<QuotaExampleArgs> {
|
|
26
|
+
constructor(private readonly quotaClient: QuotaClientService) {
|
|
27
|
+
super();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@Transition({ to: 'quota_checked' })
|
|
31
|
+
async checkQuota(_state: QuotaExampleState, ctx: RunContext<QuotaExampleArgs>) {
|
|
32
|
+
const result = await this.quotaClient.checkQuota(ctx.userId ?? 'anonymous', ctx.args.quotaType);
|
|
33
|
+
|
|
34
|
+
const allowed = !result.exceeded;
|
|
35
|
+
await this.documentStore.save(MarkdownDocument, {
|
|
36
|
+
markdown: [
|
|
37
|
+
'## Quota Check',
|
|
38
|
+
'',
|
|
39
|
+
`- Allowed: \`${allowed}\``,
|
|
40
|
+
`- Used: \`${result.used}\``,
|
|
41
|
+
`- Limit: \`${result.limit}\``,
|
|
42
|
+
].join('\n'),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
this.assignState({ hasQuota: allowed, used: result.used, limit: result.limit });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@Transition({ from: 'quota_checked', to: 'end' })
|
|
49
|
+
async reportUsage(_state: QuotaExampleState, ctx: RunContext<QuotaExampleArgs>) {
|
|
50
|
+
if (!_state.hasQuota) {
|
|
51
|
+
await this.documentStore.save(MarkdownDocument, {
|
|
52
|
+
markdown: '## Skipped\n\nQuota exhausted — operation not performed.',
|
|
53
|
+
});
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
await this.quotaClient.report(ctx.userId ?? 'anonymous', ctx.args.quotaType, ctx.args.reportAmount);
|
|
58
|
+
|
|
59
|
+
await this.documentStore.save(MarkdownDocument, {
|
|
60
|
+
markdown: [
|
|
61
|
+
'## Reported',
|
|
62
|
+
'',
|
|
63
|
+
`Charged \`${ctx.args.reportAmount}\` units of \`${ctx.args.quotaType}\` to the user's quota.`,
|
|
64
|
+
].join('\n'),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|