@langwatch/server 3.1.0-beta.19 → 3.1.0-beta.20
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/langwatch/prisma/migrations/20260425000000_consolidate_gateway_audit_into_audit_log/migration.sql +44 -0
- package/langwatch/prisma/migrations/20260426150000_drop_redundant_audit_log_org_index/migration.sql +25 -0
- package/langwatch/prisma/schema.prisma +34 -41
- package/langwatch/scripts/seed-gateway-dogfood.ts +26 -26
- package/langwatch/src/app/api/gateway-platform/[[...route]]/app.ts +8 -4
- package/langwatch/src/app/api/middleware/trace-limit.ts +10 -0
- package/langwatch/src/components/MainMenu.tsx +4 -15
- package/langwatch/src/components/SettingsLayout.tsx +1 -1
- package/langwatch/src/components/llmPromptConfigs/__tests__/LLMConfigPopover.test.tsx +45 -0
- package/langwatch/src/components/llmPromptConfigs/parameterRegistry.ts +22 -13
- package/langwatch/src/components/prompts/PromptEditorDrawer.tsx +5 -3
- package/langwatch/src/components/settings/AddCustomModelDialog.tsx +7 -6
- package/langwatch/src/evaluations-v3/__tests__/TableDisplay.integration.test.tsx +90 -5
- package/langwatch/src/evaluations-v3/__tests__/TableSettingsMenu.integration.test.tsx +26 -26
- package/langwatch/src/evaluations-v3/components/DatasetSection/TableCell.tsx +2 -10
- package/langwatch/src/evaluations-v3/components/EvaluationsV3Table.tsx +10 -4
- package/langwatch/src/evaluations-v3/components/TableSettingsMenu.tsx +6 -6
- package/langwatch/src/evaluations-v3/components/TargetSection/EvaluatorChip.tsx +19 -11
- package/langwatch/src/evaluations-v3/components/TargetSection/TargetCell.tsx +55 -42
- package/langwatch/src/evaluations-v3/components/TargetSection/TargetHeader.tsx +2 -1
- package/langwatch/src/evaluations-v3/components/TargetSection/__tests__/EvaluatorChip.test.tsx +57 -0
- package/langwatch/src/evaluations-v3/components/TargetSection/__tests__/TargetCell.test.tsx +199 -11
- package/langwatch/src/evaluations-v3/components/VirtualizedTableBody.tsx +1 -1
- package/langwatch/src/evaluations-v3/constants.ts +9 -0
- package/langwatch/src/evaluations-v3/types/__tests__/persistence.test.ts +1 -1
- package/langwatch/src/evaluations-v3/types.ts +13 -1
- package/langwatch/src/evaluations-v3/utils/__tests__/evaluatorEditorCallbacks.test.ts +32 -0
- package/langwatch/src/evaluations-v3/utils/evaluatorEditorCallbacks.ts +36 -5
- package/langwatch/src/optimization_studio/components/datasets/DatasetSelection.tsx +1 -6
- package/langwatch/src/optimization_studio/components/datasets/__tests__/DatasetSelection.integration.test.tsx +194 -0
- package/langwatch/src/optimization_studio/components/drawers/SignaturePromptEditorBridge.tsx +6 -6
- package/langwatch/src/optimization_studio/components/drawers/__tests__/SignaturePromptEditorBridge.integration.test.tsx +36 -11
- package/langwatch/src/pages/[project]/gateway/budgets/[id].tsx +1 -1
- package/langwatch/src/pages/[project]/gateway/virtual-keys/[id].tsx +1 -1
- package/langwatch/src/pages/settings/audit-log.tsx +82 -59
- package/langwatch/src/routes.tsx +0 -4
- package/langwatch/src/server/agents/agent.service.ts +6 -2
- package/langwatch/src/server/api/__tests__/rbac-integration.test.ts +53 -0
- package/langwatch/src/server/api/__tests__/rbac.auditLog.test.ts +100 -0
- package/langwatch/src/server/api/rbac.ts +12 -0
- package/langwatch/src/server/api/root.ts +0 -2
- package/langwatch/src/server/api/routers/organization.ts +1 -1
- package/langwatch/src/server/app-layer/organizations/repositories/organization.prisma.repository.ts +58 -120
- package/langwatch/src/server/app-layer/organizations/repositories/organization.repository.ts +8 -11
- package/langwatch/src/server/evaluators/evaluator.service.ts +6 -2
- package/langwatch/src/server/gateway/__tests__/auditLog.consolidation.integration.test.ts +237 -0
- package/langwatch/src/server/gateway/__tests__/cacheRule.service.unit.test.ts +2 -2
- package/langwatch/src/server/gateway/__tests__/providerCredential.service.unit.test.ts +6 -6
- package/langwatch/src/server/gateway/__tests__/virtualKey.service.unit.test.ts +6 -6
- package/langwatch/src/server/gateway/auditLog.repository.ts +46 -8
- package/langwatch/src/server/gateway/auditSerializer.ts +2 -2
- package/langwatch/src/server/gateway/budget.service.ts +6 -6
- package/langwatch/src/server/gateway/cacheRule.service.ts +5 -5
- package/langwatch/src/server/gateway/providerCredential.service.ts +5 -5
- package/langwatch/src/server/gateway/virtualKey.service.ts +9 -9
- package/langwatch/src/server/pat/__tests__/auth-middleware.unit.test.ts +138 -3
- package/langwatch/src/server/pat/auth-middleware.ts +68 -13
- package/langwatch/src/server/routes/__tests__/otel.unit.test.ts +241 -0
- package/langwatch/src/server/routes/otel.ts +281 -45
- package/langwatch/src/server/scenarios/adapters/__tests__/http-agent.adapter.test.ts +271 -6
- package/langwatch/src/server/scenarios/adapters/http-agent.adapter.ts +43 -35
- package/langwatch/src/server/scenarios/execution/http-template-engine.ts +124 -0
- package/langwatch/src/server/scenarios/execution/serialized-adapters/__tests__/http-agent.adapter.unit.test.ts +180 -0
- package/langwatch/src/server/scenarios/execution/serialized-adapters/http-agent.adapter.ts +26 -30
- package/langwatch/src/tasks/__tests__/migrateCustomModels.unit.test.ts +0 -2
- package/langwatch/src/tasks/migrateCustomModels.ts +1 -1
- package/langwatch/src/utils/__tests__/dbMultiTenancyProtection.unit.test.ts +0 -17
- package/langwatch/src/utils/permissionsConfig.ts +6 -0
- package/langwatch/src/utils/routes.ts +0 -5
- package/package.json +1 -1
- package/packages/server/dist/cli.cjs +39 -70
- package/langwatch/src/pages/[project]/gateway/audit.tsx +0 -606
- package/langwatch/src/server/api/routers/gatewayAudit.ts +0 -109
- package/langwatch/src/server/gateway/__tests__/audit.service.unit.test.ts +0 -156
- package/langwatch/src/server/gateway/audit.service.ts +0 -104
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
AgentRepository,
|
|
7
7
|
TypedAgent,
|
|
8
8
|
} from "../../../agents/agent.repository";
|
|
9
|
+
import { TemplateRenderError } from "../../execution/http-template-engine";
|
|
9
10
|
import { HttpAgentAdapter } from "../http-agent.adapter";
|
|
10
11
|
|
|
11
12
|
const createAgentInput = (
|
|
@@ -337,7 +338,7 @@ describe("HttpAgentAdapter", () => {
|
|
|
337
338
|
expect(body.input).toBe("User question");
|
|
338
339
|
});
|
|
339
340
|
|
|
340
|
-
it("
|
|
341
|
+
it("renders {{input}} as empty string when messages array is empty", async () => {
|
|
341
342
|
const { ssrfSafeFetch } = await import("~/utils/ssrfProtection");
|
|
342
343
|
const mockFetch = vi.mocked(ssrfSafeFetch);
|
|
343
344
|
mockFetch.mockResolvedValue(
|
|
@@ -359,11 +360,11 @@ describe("HttpAgentAdapter", () => {
|
|
|
359
360
|
await adapter.call(createAgentInput([]));
|
|
360
361
|
|
|
361
362
|
const callArgs = mockFetch.mock.calls[0];
|
|
362
|
-
const body = callArgs?.[1]?.body as string;
|
|
363
|
-
expect(body).toBe(
|
|
363
|
+
const body = JSON.parse(callArgs?.[1]?.body as string);
|
|
364
|
+
expect(body.input).toBe("");
|
|
364
365
|
});
|
|
365
366
|
|
|
366
|
-
it("
|
|
367
|
+
it("renders {{input}} as empty string when no user messages exist", async () => {
|
|
367
368
|
const { ssrfSafeFetch } = await import("~/utils/ssrfProtection");
|
|
368
369
|
const mockFetch = vi.mocked(ssrfSafeFetch);
|
|
369
370
|
mockFetch.mockResolvedValue(
|
|
@@ -390,8 +391,8 @@ describe("HttpAgentAdapter", () => {
|
|
|
390
391
|
);
|
|
391
392
|
|
|
392
393
|
const callArgs = mockFetch.mock.calls[0];
|
|
393
|
-
const body = callArgs?.[1]?.body as string;
|
|
394
|
-
expect(body).toBe(
|
|
394
|
+
const body = JSON.parse(callArgs?.[1]?.body as string);
|
|
395
|
+
expect(body.input).toBe("");
|
|
395
396
|
});
|
|
396
397
|
|
|
397
398
|
it("stringifies non-string content for {{input}}", async () => {
|
|
@@ -750,4 +751,268 @@ describe("HttpAgentAdapter", () => {
|
|
|
750
751
|
expect(adapter.getTraceId()).toBeUndefined();
|
|
751
752
|
});
|
|
752
753
|
});
|
|
754
|
+
|
|
755
|
+
describe("URL template interpolation", () => {
|
|
756
|
+
const setupMockFetch = async () => {
|
|
757
|
+
const { ssrfSafeFetch } = await import("~/utils/ssrfProtection");
|
|
758
|
+
const mockFetch = vi.mocked(ssrfSafeFetch);
|
|
759
|
+
mockFetch.mockResolvedValue(
|
|
760
|
+
new Response(JSON.stringify({ result: "ok" }), {
|
|
761
|
+
headers: { "content-type": "application/json" },
|
|
762
|
+
}),
|
|
763
|
+
);
|
|
764
|
+
return mockFetch;
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
it("renders {{threadId}} in url via Liquid", async () => {
|
|
768
|
+
const mockFetch = await setupMockFetch();
|
|
769
|
+
const agent = createHttpAgent({
|
|
770
|
+
url: "https://api.example.com/conversations/{{threadId}}/messages",
|
|
771
|
+
});
|
|
772
|
+
const adapter = new HttpAgentAdapter({
|
|
773
|
+
agentId: "agent-123",
|
|
774
|
+
projectId: "project-123",
|
|
775
|
+
agentRepository: createMockAgentRepository(agent),
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
await adapter.call(
|
|
779
|
+
createAgentInput([{ role: "user", content: "Hello" }], {
|
|
780
|
+
threadId: "thread-abc-123",
|
|
781
|
+
}),
|
|
782
|
+
);
|
|
783
|
+
|
|
784
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
785
|
+
"https://api.example.com/conversations/thread-abc-123/messages",
|
|
786
|
+
expect.any(Object),
|
|
787
|
+
);
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
it("URL-encodes interpolated values by default", async () => {
|
|
791
|
+
const mockFetch = await setupMockFetch();
|
|
792
|
+
const agent = createHttpAgent({
|
|
793
|
+
url: "https://api.example.com/search/{{input}}",
|
|
794
|
+
});
|
|
795
|
+
const adapter = new HttpAgentAdapter({
|
|
796
|
+
agentId: "agent-123",
|
|
797
|
+
projectId: "project-123",
|
|
798
|
+
agentRepository: createMockAgentRepository(agent),
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
await adapter.call(
|
|
802
|
+
createAgentInput([
|
|
803
|
+
{ role: "user", content: "hello world & friends?" },
|
|
804
|
+
]),
|
|
805
|
+
);
|
|
806
|
+
|
|
807
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
808
|
+
"https://api.example.com/search/hello%20world%20%26%20friends%3F",
|
|
809
|
+
expect.any(Object),
|
|
810
|
+
);
|
|
811
|
+
});
|
|
812
|
+
|
|
813
|
+
it("preserves url literals (slashes) around interpolated values", async () => {
|
|
814
|
+
const mockFetch = await setupMockFetch();
|
|
815
|
+
const agent = createHttpAgent({
|
|
816
|
+
url: "https://api.example.com/conversations/{{threadId}}/messages",
|
|
817
|
+
});
|
|
818
|
+
const adapter = new HttpAgentAdapter({
|
|
819
|
+
agentId: "agent-123",
|
|
820
|
+
projectId: "project-123",
|
|
821
|
+
agentRepository: createMockAgentRepository(agent),
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
await adapter.call(
|
|
825
|
+
createAgentInput([{ role: "user", content: "Hello" }], {
|
|
826
|
+
threadId: "abc",
|
|
827
|
+
}),
|
|
828
|
+
);
|
|
829
|
+
|
|
830
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
831
|
+
"https://api.example.com/conversations/abc/messages",
|
|
832
|
+
expect.any(Object),
|
|
833
|
+
);
|
|
834
|
+
});
|
|
835
|
+
|
|
836
|
+
describe("when using `| raw` escape hatch", () => {
|
|
837
|
+
it("skips URL-encoding for raw-filtered references only", async () => {
|
|
838
|
+
const mockFetch = await setupMockFetch();
|
|
839
|
+
const agent = createHttpAgent({
|
|
840
|
+
url: "https://api.example.com/{{threadId | raw}}/s/{{input}}",
|
|
841
|
+
});
|
|
842
|
+
const adapter = new HttpAgentAdapter({
|
|
843
|
+
agentId: "agent-123",
|
|
844
|
+
projectId: "project-123",
|
|
845
|
+
agentRepository: createMockAgentRepository(agent),
|
|
846
|
+
});
|
|
847
|
+
|
|
848
|
+
await adapter.call(
|
|
849
|
+
createAgentInput([{ role: "user", content: "with space" }], {
|
|
850
|
+
threadId: "path/with/slashes",
|
|
851
|
+
}),
|
|
852
|
+
);
|
|
853
|
+
|
|
854
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
855
|
+
"https://api.example.com/path/with/slashes/s/with%20space",
|
|
856
|
+
expect.any(Object),
|
|
857
|
+
);
|
|
858
|
+
});
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
describe("when url has no interpolation placeholders", () => {
|
|
862
|
+
it("passes url through unchanged", async () => {
|
|
863
|
+
const mockFetch = await setupMockFetch();
|
|
864
|
+
const agent = createHttpAgent({
|
|
865
|
+
url: "https://api.example.com/chat",
|
|
866
|
+
});
|
|
867
|
+
const adapter = new HttpAgentAdapter({
|
|
868
|
+
agentId: "agent-123",
|
|
869
|
+
projectId: "project-123",
|
|
870
|
+
agentRepository: createMockAgentRepository(agent),
|
|
871
|
+
});
|
|
872
|
+
|
|
873
|
+
await adapter.call(
|
|
874
|
+
createAgentInput([{ role: "user", content: "Hello" }]),
|
|
875
|
+
);
|
|
876
|
+
|
|
877
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
878
|
+
"https://api.example.com/chat",
|
|
879
|
+
expect.any(Object),
|
|
880
|
+
);
|
|
881
|
+
});
|
|
882
|
+
});
|
|
883
|
+
|
|
884
|
+
describe("when url contains Liquid conditional", () => {
|
|
885
|
+
it("renders if-branch when condition is truthy", async () => {
|
|
886
|
+
const mockFetch = await setupMockFetch();
|
|
887
|
+
const agent = createHttpAgent({
|
|
888
|
+
url: "https://api.example.com{% if conversationId %}/chat/{{conversationId}}/message{% else %}/chat/start{% endif %}",
|
|
889
|
+
scenarioMappings: {
|
|
890
|
+
conversationId: { type: "value", value: "conv-42" },
|
|
891
|
+
},
|
|
892
|
+
});
|
|
893
|
+
const adapter = new HttpAgentAdapter({
|
|
894
|
+
agentId: "agent-123",
|
|
895
|
+
projectId: "project-123",
|
|
896
|
+
agentRepository: createMockAgentRepository(agent),
|
|
897
|
+
});
|
|
898
|
+
|
|
899
|
+
await adapter.call(
|
|
900
|
+
createAgentInput([{ role: "user", content: "Hi" }]),
|
|
901
|
+
);
|
|
902
|
+
|
|
903
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
904
|
+
"https://api.example.com/chat/conv-42/message",
|
|
905
|
+
expect.any(Object),
|
|
906
|
+
);
|
|
907
|
+
});
|
|
908
|
+
|
|
909
|
+
it("renders else-branch when condition is falsy", async () => {
|
|
910
|
+
const mockFetch = await setupMockFetch();
|
|
911
|
+
const agent = createHttpAgent({
|
|
912
|
+
url: "https://api.example.com{% if conversationId %}/chat/{{conversationId}}/message{% else %}/chat/start{% endif %}",
|
|
913
|
+
});
|
|
914
|
+
const adapter = new HttpAgentAdapter({
|
|
915
|
+
agentId: "agent-123",
|
|
916
|
+
projectId: "project-123",
|
|
917
|
+
agentRepository: createMockAgentRepository(agent),
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
await adapter.call(
|
|
921
|
+
createAgentInput([{ role: "user", content: "Hi" }]),
|
|
922
|
+
);
|
|
923
|
+
|
|
924
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
925
|
+
"https://api.example.com/chat/start",
|
|
926
|
+
expect.any(Object),
|
|
927
|
+
);
|
|
928
|
+
});
|
|
929
|
+
});
|
|
930
|
+
|
|
931
|
+
describe("SSRF regression", () => {
|
|
932
|
+
const setupSsrfMock = async () => {
|
|
933
|
+
const { ssrfSafeFetch } = await import("~/utils/ssrfProtection");
|
|
934
|
+
const mockFetch = vi.mocked(ssrfSafeFetch);
|
|
935
|
+
mockFetch.mockRejectedValue(new Error("Access to private IP denied"));
|
|
936
|
+
return mockFetch;
|
|
937
|
+
};
|
|
938
|
+
|
|
939
|
+
it("passes resolved url (not template) to ssrfSafeFetch", async () => {
|
|
940
|
+
const mockFetch = await setupSsrfMock();
|
|
941
|
+
const agent = createHttpAgent({
|
|
942
|
+
url: "https://{{input | raw}}/metadata",
|
|
943
|
+
});
|
|
944
|
+
const adapter = new HttpAgentAdapter({
|
|
945
|
+
agentId: "agent-123",
|
|
946
|
+
projectId: "project-123",
|
|
947
|
+
agentRepository: createMockAgentRepository(agent),
|
|
948
|
+
});
|
|
949
|
+
|
|
950
|
+
await expect(
|
|
951
|
+
adapter.call(
|
|
952
|
+
createAgentInput([{ role: "user", content: "127.0.0.1" }]),
|
|
953
|
+
),
|
|
954
|
+
).rejects.toThrow("Access to private IP denied");
|
|
955
|
+
|
|
956
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
957
|
+
"https://127.0.0.1/metadata",
|
|
958
|
+
expect.any(Object),
|
|
959
|
+
);
|
|
960
|
+
});
|
|
961
|
+
|
|
962
|
+
it.each([
|
|
963
|
+
["localhost", "localhost"],
|
|
964
|
+
["127.0.0.1", "127.0.0.1"],
|
|
965
|
+
["169.254.169.254", "169.254.169.254"],
|
|
966
|
+
])(
|
|
967
|
+
"passes %s-resolved url to ssrfSafeFetch so it can be rejected",
|
|
968
|
+
async (label, ip) => {
|
|
969
|
+
const mockFetch = await setupSsrfMock();
|
|
970
|
+
const agent = createHttpAgent({
|
|
971
|
+
url: "https://{{input | raw}}/path",
|
|
972
|
+
});
|
|
973
|
+
const adapter = new HttpAgentAdapter({
|
|
974
|
+
agentId: "agent-123",
|
|
975
|
+
projectId: "project-123",
|
|
976
|
+
agentRepository: createMockAgentRepository(agent),
|
|
977
|
+
});
|
|
978
|
+
|
|
979
|
+
await expect(
|
|
980
|
+
adapter.call(createAgentInput([{ role: "user", content: ip }])),
|
|
981
|
+
).rejects.toThrow();
|
|
982
|
+
|
|
983
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
984
|
+
`https://${ip}/path`,
|
|
985
|
+
expect.any(Object),
|
|
986
|
+
);
|
|
987
|
+
expect(label).toBeTruthy();
|
|
988
|
+
},
|
|
989
|
+
);
|
|
990
|
+
});
|
|
991
|
+
|
|
992
|
+
describe("when url template is malformed", () => {
|
|
993
|
+
it("throws TemplateRenderError with field=url", async () => {
|
|
994
|
+
await setupMockFetch();
|
|
995
|
+
const agent = createHttpAgent({
|
|
996
|
+
url: "https://api.example.com/{% if %}/broken",
|
|
997
|
+
});
|
|
998
|
+
const adapter = new HttpAgentAdapter({
|
|
999
|
+
agentId: "agent-123",
|
|
1000
|
+
projectId: "project-123",
|
|
1001
|
+
agentRepository: createMockAgentRepository(agent),
|
|
1002
|
+
});
|
|
1003
|
+
|
|
1004
|
+
await expect(
|
|
1005
|
+
adapter.call(
|
|
1006
|
+
createAgentInput([{ role: "user", content: "Hello" }]),
|
|
1007
|
+
),
|
|
1008
|
+
).rejects.toThrow(TemplateRenderError);
|
|
1009
|
+
|
|
1010
|
+
await expect(
|
|
1011
|
+
adapter.call(
|
|
1012
|
+
createAgentInput([{ role: "user", content: "Hello" }]),
|
|
1013
|
+
),
|
|
1014
|
+
).rejects.toMatchObject({ field: "url" });
|
|
1015
|
+
});
|
|
1016
|
+
});
|
|
1017
|
+
});
|
|
753
1018
|
});
|
|
@@ -9,12 +9,28 @@ import {
|
|
|
9
9
|
AgentRepository,
|
|
10
10
|
type AgentRepository as AgentRepositoryType,
|
|
11
11
|
} from "../../agents/agent.repository";
|
|
12
|
+
import {
|
|
13
|
+
buildTemplateContext,
|
|
14
|
+
renderBodyTemplate,
|
|
15
|
+
renderUrlTemplate,
|
|
16
|
+
} from "../execution/http-template-engine";
|
|
12
17
|
import { injectTraceContextHeaders } from "../execution/trace-context-headers";
|
|
13
18
|
import { applyAuthentication } from "./auth.strategies";
|
|
14
19
|
|
|
15
20
|
const logger = createLogger("HttpAgentAdapter");
|
|
16
21
|
|
|
17
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Extract scheme + host from a URL for logging.
|
|
24
|
+
* Paths and query strings can contain interpolated PII after URL templating;
|
|
25
|
+
* the origin is config-level and safe to emit at any log level.
|
|
26
|
+
*/
|
|
27
|
+
function safeOrigin(url: string): string {
|
|
28
|
+
try {
|
|
29
|
+
return new URL(url).origin;
|
|
30
|
+
} catch {
|
|
31
|
+
return "<unparseable>";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
18
34
|
|
|
19
35
|
interface HttpAgentAdapterParams {
|
|
20
36
|
agentId: string;
|
|
@@ -71,16 +87,31 @@ export class HttpAgentAdapter extends AgentAdapter {
|
|
|
71
87
|
|
|
72
88
|
try {
|
|
73
89
|
const config = await this.fetchAgentConfig();
|
|
90
|
+
const templateContext = buildTemplateContext({
|
|
91
|
+
input,
|
|
92
|
+
scenarioMappings: config.scenarioMappings,
|
|
93
|
+
});
|
|
94
|
+
const url = renderUrlTemplate({ template: config.url, context: templateContext });
|
|
74
95
|
const headers = this.buildRequestHeaders(config);
|
|
75
|
-
const body = this.buildRequestBody(config.bodyTemplate, input);
|
|
76
|
-
const responseData = await this.executeHttpRequest(
|
|
96
|
+
const body = this.buildRequestBody(config.bodyTemplate, input, templateContext);
|
|
97
|
+
const responseData = await this.executeHttpRequest(
|
|
98
|
+
url,
|
|
99
|
+
config.method,
|
|
100
|
+
headers,
|
|
101
|
+
body,
|
|
102
|
+
);
|
|
77
103
|
const result = this.extractResponseContent(
|
|
78
104
|
responseData,
|
|
79
105
|
config.outputPath,
|
|
80
106
|
);
|
|
81
107
|
|
|
82
108
|
logger.info(
|
|
83
|
-
{
|
|
109
|
+
{
|
|
110
|
+
agentId: this.agentId,
|
|
111
|
+
origin: safeOrigin(url),
|
|
112
|
+
urlTemplate: config.url,
|
|
113
|
+
resultLength: result.length,
|
|
114
|
+
},
|
|
84
115
|
"HttpAgentAdapter.call completed",
|
|
85
116
|
);
|
|
86
117
|
|
|
@@ -160,19 +191,17 @@ export class HttpAgentAdapter extends AgentAdapter {
|
|
|
160
191
|
}
|
|
161
192
|
|
|
162
193
|
private async executeHttpRequest(
|
|
163
|
-
|
|
194
|
+
url: string,
|
|
195
|
+
method: HttpComponentConfig["method"],
|
|
164
196
|
headers: Record<string, string>,
|
|
165
197
|
body: string,
|
|
166
198
|
): Promise<unknown> {
|
|
167
|
-
logger.debug(
|
|
168
|
-
{ url: config.url, method: config.method },
|
|
169
|
-
"Making HTTP request",
|
|
170
|
-
);
|
|
199
|
+
logger.debug({ origin: safeOrigin(url), method }, "Making HTTP request");
|
|
171
200
|
|
|
172
|
-
const response = await ssrfSafeFetch(
|
|
173
|
-
method
|
|
201
|
+
const response = await ssrfSafeFetch(url, {
|
|
202
|
+
method,
|
|
174
203
|
headers,
|
|
175
|
-
body:
|
|
204
|
+
body: method !== "GET" ? body : undefined,
|
|
176
205
|
});
|
|
177
206
|
|
|
178
207
|
logger.debug(
|
|
@@ -219,33 +248,12 @@ export class HttpAgentAdapter extends AgentAdapter {
|
|
|
219
248
|
private buildRequestBody(
|
|
220
249
|
template: string | undefined,
|
|
221
250
|
input: AgentInput,
|
|
251
|
+
context: Record<string, unknown>,
|
|
222
252
|
): string {
|
|
223
253
|
if (!template) {
|
|
224
254
|
return JSON.stringify({ messages: input.messages });
|
|
225
255
|
}
|
|
226
256
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
body = body.replace(
|
|
230
|
-
/\{\{\s*messages\s*\}\}/g,
|
|
231
|
-
JSON.stringify(input.messages),
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
body = body.replace(
|
|
235
|
-
/\{\{\s*threadId\s*\}\}/g,
|
|
236
|
-
input.threadId ?? DEFAULT_SCENARIO_THREAD_ID,
|
|
237
|
-
);
|
|
238
|
-
|
|
239
|
-
const lastUserMessage = input.messages.findLast((m) => m.role === "user");
|
|
240
|
-
if (lastUserMessage) {
|
|
241
|
-
body = body.replace(
|
|
242
|
-
/\{\{\s*input\s*\}\}/g,
|
|
243
|
-
typeof lastUserMessage.content === "string"
|
|
244
|
-
? lastUserMessage.content
|
|
245
|
-
: JSON.stringify(lastUserMessage.content),
|
|
246
|
-
);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
return body;
|
|
257
|
+
return renderBodyTemplate({ template, context });
|
|
250
258
|
}
|
|
251
259
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared Liquid template engine for HTTP agent adapters.
|
|
3
|
+
*
|
|
4
|
+
* Two engines with identical filter sets but different output handling:
|
|
5
|
+
* - `urlLiquid`: URL-encodes interpolated values by default. `| raw` opts out.
|
|
6
|
+
* - `bodyLiquid`: renders values verbatim — body templates are JSON, not URLs.
|
|
7
|
+
*
|
|
8
|
+
* Both adapters (DB-backed and serialized) use these engines so HTTP agents
|
|
9
|
+
* render URL and body templates through one render pipeline.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { AgentInput } from "@langwatch/scenario";
|
|
13
|
+
import { Liquid } from "liquidjs";
|
|
14
|
+
import { resolveFieldMappings } from "./resolve-field-mappings";
|
|
15
|
+
import type { FieldMapping } from "./types";
|
|
16
|
+
|
|
17
|
+
const DEFAULT_SCENARIO_THREAD_ID = "scenario-test";
|
|
18
|
+
|
|
19
|
+
export type TemplateField = "url" | "bodyTemplate";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Error thrown when a Liquid template fails to parse or render.
|
|
23
|
+
* Identifies the failing field so callers can surface precise diagnostics.
|
|
24
|
+
*/
|
|
25
|
+
export class TemplateRenderError extends Error {
|
|
26
|
+
readonly field: TemplateField;
|
|
27
|
+
readonly cause: unknown;
|
|
28
|
+
|
|
29
|
+
constructor({
|
|
30
|
+
field,
|
|
31
|
+
cause,
|
|
32
|
+
}: {
|
|
33
|
+
field: TemplateField;
|
|
34
|
+
cause: unknown;
|
|
35
|
+
}) {
|
|
36
|
+
const rootMessage = cause instanceof Error ? cause.message : String(cause);
|
|
37
|
+
super(`Failed to render ${field} template: ${rootMessage}`);
|
|
38
|
+
this.name = "TemplateRenderError";
|
|
39
|
+
this.field = field;
|
|
40
|
+
this.cause = cause;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const identity = <T>(v: T): T => v;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* URL template engine. `outputEscape` URL-encodes every `{{ expr }}` output
|
|
48
|
+
* unless the final filter in the expression is `raw` (registered with
|
|
49
|
+
* `raw: true`, which liquidjs honors by skipping outputEscape).
|
|
50
|
+
*/
|
|
51
|
+
const urlLiquid = new Liquid({
|
|
52
|
+
outputEscape: (value) => encodeURIComponent(String(value ?? "")),
|
|
53
|
+
});
|
|
54
|
+
urlLiquid.registerFilter("raw", { handler: identity, raw: true });
|
|
55
|
+
|
|
56
|
+
/** Body template engine. No default encoding — bodies are JSON. */
|
|
57
|
+
const bodyLiquid = new Liquid();
|
|
58
|
+
bodyLiquid.registerFilter("raw", { handler: identity, raw: true });
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Build the Liquid context shared by `url` and `bodyTemplate` rendering.
|
|
62
|
+
*
|
|
63
|
+
* Base context (always present, derived from AgentInput):
|
|
64
|
+
* - `messages` — JSON-encoded messages array
|
|
65
|
+
* - `threadId` — thread ID or default sentinel
|
|
66
|
+
* - `input` — last user message content (string, JSON-stringified if structured)
|
|
67
|
+
*
|
|
68
|
+
* `scenarioMappings` output is merged last and overrides base keys so users
|
|
69
|
+
* can redefine `input` to be a structured object without breaking existing
|
|
70
|
+
* templates.
|
|
71
|
+
*/
|
|
72
|
+
export function buildTemplateContext({
|
|
73
|
+
input,
|
|
74
|
+
scenarioMappings,
|
|
75
|
+
}: {
|
|
76
|
+
input: AgentInput;
|
|
77
|
+
scenarioMappings?: Record<string, FieldMapping>;
|
|
78
|
+
}): Record<string, unknown> {
|
|
79
|
+
const lastUserMessage = input.messages.findLast((m) => m.role === "user");
|
|
80
|
+
const base: Record<string, unknown> = {
|
|
81
|
+
messages: JSON.stringify(input.messages),
|
|
82
|
+
threadId: input.threadId ?? DEFAULT_SCENARIO_THREAD_ID,
|
|
83
|
+
input:
|
|
84
|
+
lastUserMessage === undefined
|
|
85
|
+
? undefined
|
|
86
|
+
: typeof lastUserMessage.content === "string"
|
|
87
|
+
? lastUserMessage.content
|
|
88
|
+
: JSON.stringify(lastUserMessage.content),
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const mapped = scenarioMappings
|
|
92
|
+
? resolveFieldMappings({ fieldMappings: scenarioMappings, agentInput: input })
|
|
93
|
+
: {};
|
|
94
|
+
|
|
95
|
+
return { ...base, ...mapped };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function renderUrlTemplate({
|
|
99
|
+
template,
|
|
100
|
+
context,
|
|
101
|
+
}: {
|
|
102
|
+
template: string;
|
|
103
|
+
context: Record<string, unknown>;
|
|
104
|
+
}): string {
|
|
105
|
+
try {
|
|
106
|
+
return urlLiquid.parseAndRenderSync(template, context);
|
|
107
|
+
} catch (cause) {
|
|
108
|
+
throw new TemplateRenderError({ field: "url", cause });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function renderBodyTemplate({
|
|
113
|
+
template,
|
|
114
|
+
context,
|
|
115
|
+
}: {
|
|
116
|
+
template: string;
|
|
117
|
+
context: Record<string, unknown>;
|
|
118
|
+
}): string {
|
|
119
|
+
try {
|
|
120
|
+
return bodyLiquid.parseAndRenderSync(template, context);
|
|
121
|
+
} catch (cause) {
|
|
122
|
+
throw new TemplateRenderError({ field: "bodyTemplate", cause });
|
|
123
|
+
}
|
|
124
|
+
}
|