@flowajs/chat-service 0.0.1

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.
Files changed (111) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +210 -0
  3. package/dist/artifact.d.ts +87 -0
  4. package/dist/artifact.d.ts.map +1 -0
  5. package/dist/artifact.js +99 -0
  6. package/dist/artifact.js.map +1 -0
  7. package/dist/audit.d.ts +28 -0
  8. package/dist/audit.d.ts.map +1 -0
  9. package/dist/audit.js +73 -0
  10. package/dist/audit.js.map +1 -0
  11. package/dist/auth/jwt.d.ts +18 -0
  12. package/dist/auth/jwt.d.ts.map +1 -0
  13. package/dist/auth/jwt.js +23 -0
  14. package/dist/auth/jwt.js.map +1 -0
  15. package/dist/auth/oidc.d.ts +30 -0
  16. package/dist/auth/oidc.d.ts.map +1 -0
  17. package/dist/auth/oidc.js +58 -0
  18. package/dist/auth/oidc.js.map +1 -0
  19. package/dist/chat.d.ts +161 -0
  20. package/dist/chat.d.ts.map +1 -0
  21. package/dist/chat.js +636 -0
  22. package/dist/chat.js.map +1 -0
  23. package/dist/cli.d.ts +6 -0
  24. package/dist/cli.d.ts.map +1 -0
  25. package/dist/cli.js +47 -0
  26. package/dist/cli.js.map +1 -0
  27. package/dist/config.d.ts +18 -0
  28. package/dist/config.d.ts.map +1 -0
  29. package/dist/config.js +80 -0
  30. package/dist/config.js.map +1 -0
  31. package/dist/index.d.ts +19 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +19 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/instrumentation.d.ts +31 -0
  36. package/dist/instrumentation.d.ts.map +1 -0
  37. package/dist/instrumentation.js +151 -0
  38. package/dist/instrumentation.js.map +1 -0
  39. package/dist/llm/anthropic.d.ts +22 -0
  40. package/dist/llm/anthropic.d.ts.map +1 -0
  41. package/dist/llm/anthropic.js +40 -0
  42. package/dist/llm/anthropic.js.map +1 -0
  43. package/dist/llm/bedrock.d.ts +34 -0
  44. package/dist/llm/bedrock.d.ts.map +1 -0
  45. package/dist/llm/bedrock.js +54 -0
  46. package/dist/llm/bedrock.js.map +1 -0
  47. package/dist/llm/factory.d.ts +3 -0
  48. package/dist/llm/factory.d.ts.map +1 -0
  49. package/dist/llm/factory.js +59 -0
  50. package/dist/llm/factory.js.map +1 -0
  51. package/dist/llm/google-gla.d.ts +22 -0
  52. package/dist/llm/google-gla.d.ts.map +1 -0
  53. package/dist/llm/google-gla.js +29 -0
  54. package/dist/llm/google-gla.js.map +1 -0
  55. package/dist/llm/google-vertex.d.ts +21 -0
  56. package/dist/llm/google-vertex.d.ts.map +1 -0
  57. package/dist/llm/google-vertex.js +28 -0
  58. package/dist/llm/google-vertex.js.map +1 -0
  59. package/dist/llm/interface.d.ts +45 -0
  60. package/dist/llm/interface.d.ts.map +1 -0
  61. package/dist/llm/interface.js +2 -0
  62. package/dist/llm/interface.js.map +1 -0
  63. package/dist/llm/openai.d.ts +19 -0
  64. package/dist/llm/openai.d.ts.map +1 -0
  65. package/dist/llm/openai.js +25 -0
  66. package/dist/llm/openai.js.map +1 -0
  67. package/dist/prompts.d.ts +7 -0
  68. package/dist/prompts.d.ts.map +1 -0
  69. package/dist/prompts.js +17 -0
  70. package/dist/prompts.js.map +1 -0
  71. package/dist/server.d.ts +39 -0
  72. package/dist/server.d.ts.map +1 -0
  73. package/dist/server.js +106 -0
  74. package/dist/server.js.map +1 -0
  75. package/dist/session.d.ts +68 -0
  76. package/dist/session.d.ts.map +1 -0
  77. package/dist/session.js +245 -0
  78. package/dist/session.js.map +1 -0
  79. package/dist/storage/factory.d.ts +28 -0
  80. package/dist/storage/factory.d.ts.map +1 -0
  81. package/dist/storage/factory.js +33 -0
  82. package/dist/storage/factory.js.map +1 -0
  83. package/dist/storage/fs.d.ts +14 -0
  84. package/dist/storage/fs.d.ts.map +1 -0
  85. package/dist/storage/fs.js +116 -0
  86. package/dist/storage/fs.js.map +1 -0
  87. package/dist/storage/gcs.d.ts +27 -0
  88. package/dist/storage/gcs.d.ts.map +1 -0
  89. package/dist/storage/gcs.js +81 -0
  90. package/dist/storage/gcs.js.map +1 -0
  91. package/dist/storage/interface.d.ts +33 -0
  92. package/dist/storage/interface.d.ts.map +1 -0
  93. package/dist/storage/interface.js +12 -0
  94. package/dist/storage/interface.js.map +1 -0
  95. package/dist/storage/s3.d.ts +29 -0
  96. package/dist/storage/s3.d.ts.map +1 -0
  97. package/dist/storage/s3.js +109 -0
  98. package/dist/storage/s3.js.map +1 -0
  99. package/dist/storage-keys.d.ts +33 -0
  100. package/dist/storage-keys.d.ts.map +1 -0
  101. package/dist/storage-keys.js +76 -0
  102. package/dist/storage-keys.js.map +1 -0
  103. package/dist/telemetry.d.ts +29 -0
  104. package/dist/telemetry.d.ts.map +1 -0
  105. package/dist/telemetry.js +116 -0
  106. package/dist/telemetry.js.map +1 -0
  107. package/dist/yaml.d.ts +42 -0
  108. package/dist/yaml.d.ts.map +1 -0
  109. package/dist/yaml.js +121 -0
  110. package/dist/yaml.js.map +1 -0
  111. package/package.json +124 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bedrock.js","sourceRoot":"","sources":["../../src/llm/bedrock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAuB7D;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,OAA+B;IAE/B,IAAI,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC5B,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,mEAAmE;QACnE,mEAAmE;QACnE,wEAAwE;QACxE,sEAAsE;QACtE,mEAAmE;QACnE,8DAA8D;QAC9D,oEAAoE;QACpE,MAAM,EAAE,qBAAqB,EAAE,GAC7B,MAAM,MAAM,CAAC,+BAA+B,CAAC,CAAC;QAChD,MAAM,GAAG,mBAAmB,CAAC;YAC3B,kBAAkB,EAAE,qBAAqB,EAAE;SAC5C,CAAC,CAAC;IACL,CAAC;IACD,MAAM,SAAS,GAAG,OAAO,CAAC,gBAAgB,IAAI,OAAO,CAAC,OAAO,CAAC;IAC9D,OAAO;QACL,IAAI,EAAE,aAAa;QACnB,KAAK,EAAE,MAAM,CAAC,SAAS,CAAC;QACxB,eAAe,EAAE;YACf,OAAO,EAAE;gBACP,4BAA4B,EAAE;oBAC5B,QAAQ,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE;oBAC9B,aAAa,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE;iBACpC;aACF;SACF;QACD,WAAW,EAAE,CAAC,EAAE,QAAQ,EAAgC,EAAE,EAAE,CAAC,CAAC;YAC5D,QAAQ,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAChC,CAAC,KAAK,QAAQ,CAAC,MAAM,GAAG,CAAC;gBACvB,CAAC,CAAC;oBACE,GAAG,GAAG;oBACN,eAAe,EAAE;wBACf,GAAG,CAAC,GAAG,CAAC,eAAe,IAAI,EAAE,CAAC;wBAC9B,OAAO,EAAE,EAAE,UAAU,EAAE,EAAE,IAAI,EAAE,SAAkB,EAAE,EAAE;qBACtD;iBACF;gBACH,CAAC,CAAC,GAAG,CACR;SACF,CAAC;KACH,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { LlmProvider } from "./interface.js";
2
+ export declare function createProvider(model: string): Promise<LlmProvider>;
3
+ //# sourceMappingURL=factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../src/llm/factory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAmBlD,wBAAsB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CA2CxE"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Parse `LLM_MODEL=<provider>:<model>` (matching pydantic-ai's prefix
3
+ * convention; also used by `flowa.settings.ModelConfig.name`) and
4
+ * dispatch to the matching provider module via dynamic import. The
5
+ * underlying `@ai-sdk/<provider>` package is loaded only for the
6
+ * selected provider; missing peers fail with a clear error naming the
7
+ * unresolvable package.
8
+ */
9
+ const KNOWN_PROVIDERS = [
10
+ "anthropic",
11
+ "bedrock",
12
+ "google-gla",
13
+ "google-vertex",
14
+ "openai",
15
+ ];
16
+ export async function createProvider(model) {
17
+ const colon = model.indexOf(":");
18
+ if (colon < 0) {
19
+ throw new Error(`Invalid LLM_MODEL: ${JSON.stringify(model)}. Expected "<provider>:<model>", e.g. "bedrock:au.anthropic.claude-sonnet-4-6".`);
20
+ }
21
+ const prefix = model.slice(0, colon);
22
+ const modelId = model.slice(colon + 1);
23
+ if (!modelId) {
24
+ throw new Error(`LLM_MODEL has empty model id after prefix: ${prefix}:`);
25
+ }
26
+ if (!isKnownProvider(prefix)) {
27
+ throw new Error(`Unknown LLM provider prefix: ${JSON.stringify(prefix)}. Valid prefixes: ${KNOWN_PROVIDERS.join(", ")}.`);
28
+ }
29
+ switch (prefix) {
30
+ case "anthropic": {
31
+ const { createAnthropicProvider } = await import("./anthropic.js");
32
+ return createAnthropicProvider({ modelId });
33
+ }
34
+ case "bedrock": {
35
+ const { createBedrockProvider } = await import("./bedrock.js");
36
+ const inferenceProfile = process.env.BEDROCK_INFERENCE_PROFILE;
37
+ return createBedrockProvider({
38
+ modelId,
39
+ ...(inferenceProfile ? { inferenceProfile } : {}),
40
+ });
41
+ }
42
+ case "google-gla": {
43
+ const { createGoogleGlaProvider } = await import("./google-gla.js");
44
+ return createGoogleGlaProvider({ modelId });
45
+ }
46
+ case "google-vertex": {
47
+ const { createGoogleVertexProvider } = await import("./google-vertex.js");
48
+ return createGoogleVertexProvider({ modelId });
49
+ }
50
+ case "openai": {
51
+ const { createOpenAIProvider } = await import("./openai.js");
52
+ return createOpenAIProvider({ modelId });
53
+ }
54
+ }
55
+ }
56
+ function isKnownProvider(s) {
57
+ return KNOWN_PROVIDERS.includes(s);
58
+ }
59
+ //# sourceMappingURL=factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factory.js","sourceRoot":"","sources":["../../src/llm/factory.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,MAAM,eAAe,GAAG;IACtB,WAAW;IACX,SAAS;IACT,YAAY;IACZ,eAAe;IACf,QAAQ;CACA,CAAC;AAGX,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,KAAa;IAChD,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACjC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CACb,sBAAsB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,iFAAiF,CAC7H,CAAC;IACJ,CAAC;IACD,MAAM,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IACrC,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;IACvC,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,8CAA8C,MAAM,GAAG,CAAC,CAAC;IAC3E,CAAC;IACD,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CACb,gCAAgC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,qBAAqB,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CACzG,CAAC;IACJ,CAAC;IACD,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,WAAW,CAAC,CAAC,CAAC;YACjB,MAAM,EAAE,uBAAuB,EAAE,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAAC,CAAC;YACnE,OAAO,uBAAuB,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;QAC9C,CAAC;QACD,KAAK,SAAS,CAAC,CAAC,CAAC;YACf,MAAM,EAAE,qBAAqB,EAAE,GAAG,MAAM,MAAM,CAAC,cAAc,CAAC,CAAC;YAC/D,MAAM,gBAAgB,GAAG,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC;YAC/D,OAAO,qBAAqB,CAAC;gBAC3B,OAAO;gBACP,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAClD,CAAC,CAAC;QACL,CAAC;QACD,KAAK,YAAY,CAAC,CAAC,CAAC;YAClB,MAAM,EAAE,uBAAuB,EAAE,GAAG,MAAM,MAAM,CAAC,iBAAiB,CAAC,CAAC;YACpE,OAAO,uBAAuB,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;QAC9C,CAAC;QACD,KAAK,eAAe,CAAC,CAAC,CAAC;YACrB,MAAM,EAAE,0BAA0B,EAAE,GAAG,MAAM,MAAM,CAAC,oBAAoB,CAAC,CAAC;YAC1E,OAAO,0BAA0B,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;QACjD,CAAC;QACD,KAAK,QAAQ,CAAC,CAAC,CAAC;YACd,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,CAAC;YAC7D,OAAO,oBAAoB,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;QAC3C,CAAC;IACH,CAAC;AACH,CAAC;AAED,SAAS,eAAe,CAAC,CAAS;IAChC,OAAQ,eAAqC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAC5D,CAAC"}
@@ -0,0 +1,22 @@
1
+ import { createGoogleGenerativeAI } from "@ai-sdk/google";
2
+ import type { LlmProvider } from "./interface.js";
3
+ export interface GoogleGlaProviderOptions {
4
+ modelId: string;
5
+ /** Optional pre-built provider client. */
6
+ client?: ReturnType<typeof createGoogleGenerativeAI>;
7
+ }
8
+ /**
9
+ * Google Generative Language API (`google-gla`) — Gemini via the public
10
+ * key-based API (`GOOGLE_API_KEY`). For Vertex AI, use `./google-vertex`.
11
+ *
12
+ * `providerOptions.google.thinkingConfig.includeThoughts = true` surfaces
13
+ * Gemini's reasoning trace.
14
+ *
15
+ * No `prepareStep` here: Google's prompt caching is via separate
16
+ * `CachedContent` API references rather than per-message markers, so it
17
+ * doesn't fit the per-step injection shape that `prepareStep` provides.
18
+ * Implicit caching for prompts above ~32K tokens (Gemini 2.5+) still
19
+ * applies automatically.
20
+ */
21
+ export declare function createGoogleGlaProvider(options: GoogleGlaProviderOptions): LlmProvider;
22
+ //# sourceMappingURL=google-gla.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google-gla.d.ts","sourceRoot":"","sources":["../../src/llm/google-gla.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAElD,MAAM,WAAW,wBAAwB;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,0CAA0C;IAC1C,MAAM,CAAC,EAAE,UAAU,CAAC,OAAO,wBAAwB,CAAC,CAAC;CACtD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,wBAAwB,GAChC,WAAW,CAab"}
@@ -0,0 +1,29 @@
1
+ import { createGoogleGenerativeAI } from "@ai-sdk/google";
2
+ /**
3
+ * Google Generative Language API (`google-gla`) — Gemini via the public
4
+ * key-based API (`GOOGLE_API_KEY`). For Vertex AI, use `./google-vertex`.
5
+ *
6
+ * `providerOptions.google.thinkingConfig.includeThoughts = true` surfaces
7
+ * Gemini's reasoning trace.
8
+ *
9
+ * No `prepareStep` here: Google's prompt caching is via separate
10
+ * `CachedContent` API references rather than per-message markers, so it
11
+ * doesn't fit the per-step injection shape that `prepareStep` provides.
12
+ * Implicit caching for prompts above ~32K tokens (Gemini 2.5+) still
13
+ * applies automatically.
14
+ */
15
+ export function createGoogleGlaProvider(options) {
16
+ const client = options.client ?? createGoogleGenerativeAI();
17
+ return {
18
+ name: "gcp.gemini",
19
+ model: client(options.modelId),
20
+ providerOptions: {
21
+ google: {
22
+ thinkingConfig: {
23
+ includeThoughts: true,
24
+ },
25
+ },
26
+ },
27
+ };
28
+ }
29
+ //# sourceMappingURL=google-gla.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google-gla.js","sourceRoot":"","sources":["../../src/llm/google-gla.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAS1D;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,uBAAuB,CACrC,OAAiC;IAEjC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,wBAAwB,EAAE,CAAC;IAC5D,OAAO;QACL,IAAI,EAAE,YAAY;QAClB,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC;QAC9B,eAAe,EAAE;YACf,MAAM,EAAE;gBACN,cAAc,EAAE;oBACd,eAAe,EAAE,IAAI;iBACtB;aACF;SACF;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { createVertex } from "@ai-sdk/google-vertex";
2
+ import type { LlmProvider } from "./interface.js";
3
+ export interface GoogleVertexProviderOptions {
4
+ modelId: string;
5
+ /** Optional pre-built provider client. */
6
+ client?: ReturnType<typeof createVertex>;
7
+ }
8
+ /**
9
+ * Google Vertex AI provider — Gemini via Vertex (GCP service-account
10
+ * auth). For the public key-based API, use `./google-gla`.
11
+ *
12
+ * Verified against `@ai-sdk/google-vertex` v4
13
+ * (https://ai-sdk.dev/providers/ai-sdk-providers/google-vertex):
14
+ * the namespace key is `vertex` (not `google`); the `thinkingConfig`
15
+ * shape matches.
16
+ *
17
+ * No `prepareStep`: same reasoning as `./google-gla` — Vertex's explicit
18
+ * caching is via `CachedContent` references, not per-message markers.
19
+ */
20
+ export declare function createGoogleVertexProvider(options: GoogleVertexProviderOptions): LlmProvider;
21
+ //# sourceMappingURL=google-vertex.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google-vertex.d.ts","sourceRoot":"","sources":["../../src/llm/google-vertex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAElD,MAAM,WAAW,2BAA2B;IAC1C,OAAO,EAAE,MAAM,CAAC;IAChB,0CAA0C;IAC1C,MAAM,CAAC,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;CAC1C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,0BAA0B,CACxC,OAAO,EAAE,2BAA2B,GACnC,WAAW,CAab"}
@@ -0,0 +1,28 @@
1
+ import { createVertex } from "@ai-sdk/google-vertex";
2
+ /**
3
+ * Google Vertex AI provider — Gemini via Vertex (GCP service-account
4
+ * auth). For the public key-based API, use `./google-gla`.
5
+ *
6
+ * Verified against `@ai-sdk/google-vertex` v4
7
+ * (https://ai-sdk.dev/providers/ai-sdk-providers/google-vertex):
8
+ * the namespace key is `vertex` (not `google`); the `thinkingConfig`
9
+ * shape matches.
10
+ *
11
+ * No `prepareStep`: same reasoning as `./google-gla` — Vertex's explicit
12
+ * caching is via `CachedContent` references, not per-message markers.
13
+ */
14
+ export function createGoogleVertexProvider(options) {
15
+ const client = options.client ?? createVertex();
16
+ return {
17
+ name: "gcp.gemini",
18
+ model: client(options.modelId),
19
+ providerOptions: {
20
+ vertex: {
21
+ thinkingConfig: {
22
+ includeThoughts: true,
23
+ },
24
+ },
25
+ },
26
+ };
27
+ }
28
+ //# sourceMappingURL=google-vertex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google-vertex.js","sourceRoot":"","sources":["../../src/llm/google-vertex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AASrD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,0BAA0B,CACxC,OAAoC;IAEpC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,YAAY,EAAE,CAAC;IAChD,OAAO;QACL,IAAI,EAAE,YAAY;QAClB,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC;QAC9B,eAAe,EAAE;YACf,MAAM,EAAE;gBACN,cAAc,EAAE;oBACd,eAAe,EAAE,IAAI;iBACtB;aACF;SACF;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,45 @@
1
+ import type { JSONValue, LanguageModel, ModelMessage } from "ai";
2
+ /**
3
+ * Chat-service's view of an LLM provider. Wraps a Vercel AI SDK
4
+ * `LanguageModel` with two optional knobs:
5
+ *
6
+ * - `providerOptions`: per-provider thinking/reasoning configuration that
7
+ * chat-service merges into every `streamText` / `generateText` call.
8
+ * - `prepareStep`: per-step messages transformation. Used by the bedrock
9
+ * provider to inject a `cachePoint` on the last message for prompt
10
+ * caching; other providers omit it. chat-service calls this on every
11
+ * tool-loop step if defined.
12
+ */
13
+ export interface LlmProvider {
14
+ /**
15
+ * Provider name following OpenTelemetry GenAI semantic conventions:
16
+ * `aws.bedrock`, `anthropic`, `gcp.gemini`, `openai`. Used to label
17
+ * telemetry attributes.
18
+ */
19
+ readonly name: string;
20
+ /** The configured Vercel AI SDK `LanguageModel`. */
21
+ readonly model: LanguageModel;
22
+ /**
23
+ * Provider-specific options merged into `streamText` / `generateText`
24
+ * calls. Shape is provider-specific (see each provider module);
25
+ * matches the AI SDK's `SharedV3ProviderOptions` shape (a record keyed
26
+ * by provider name, with each value a JSON object of provider-specific
27
+ * fields).
28
+ */
29
+ readonly providerOptions: Record<string, Record<string, JSONValue>>;
30
+ /**
31
+ * Optional per-step messages transformation. The bedrock and anthropic
32
+ * providers use this to inject a cache marker on the last message for
33
+ * prompt caching; google and openai omit it.
34
+ *
35
+ * Receives the full step options from the AI SDK (only `messages` is
36
+ * surfaced; the rest of the options pass through). Return a new
37
+ * `messages` array with provider-specific markers applied.
38
+ */
39
+ readonly prepareStep?: (options: {
40
+ messages: ModelMessage[];
41
+ }) => {
42
+ messages: ModelMessage[];
43
+ };
44
+ }
45
+ //# sourceMappingURL=interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../src/llm/interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAEjE;;;;;;;;;;GAUG;AACH,MAAM,WAAW,WAAW;IAC1B;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,oDAAoD;IACpD,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAE9B;;;;;;OAMG;IACH,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;IAEpE;;;;;;;;OAQG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE;QAAE,QAAQ,EAAE,YAAY,EAAE,CAAA;KAAE,KAAK;QAChE,QAAQ,EAAE,YAAY,EAAE,CAAC;KAC1B,CAAC;CACH"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interface.js","sourceRoot":"","sources":["../../src/llm/interface.ts"],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
1
+ import { createOpenAI } from "@ai-sdk/openai";
2
+ import type { LlmProvider } from "./interface.js";
3
+ export interface OpenAIProviderOptions {
4
+ modelId: string;
5
+ /** Optional pre-built provider client. */
6
+ client?: ReturnType<typeof createOpenAI>;
7
+ }
8
+ /**
9
+ * OpenAI provider with reasoning enabled.
10
+ *
11
+ * `providerOptions.openai.reasoningEffort = "medium"` and
12
+ * `reasoningSummary = "detailed"` match the Responses API shape for the
13
+ * GPT-5 series.
14
+ *
15
+ * No `prepareStep`: OpenAI's prompt caching is automatic for prompts
16
+ * above ~1024 tokens — there is no per-message marker to inject.
17
+ */
18
+ export declare function createOpenAIProvider(options: OpenAIProviderOptions): LlmProvider;
19
+ //# sourceMappingURL=openai.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/llm/openai.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAElD,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,MAAM,CAAC;IAChB,0CAA0C;IAC1C,MAAM,CAAC,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;CAC1C;AAED;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,qBAAqB,GAC7B,WAAW,CAYb"}
@@ -0,0 +1,25 @@
1
+ import { createOpenAI } from "@ai-sdk/openai";
2
+ /**
3
+ * OpenAI provider with reasoning enabled.
4
+ *
5
+ * `providerOptions.openai.reasoningEffort = "medium"` and
6
+ * `reasoningSummary = "detailed"` match the Responses API shape for the
7
+ * GPT-5 series.
8
+ *
9
+ * No `prepareStep`: OpenAI's prompt caching is automatic for prompts
10
+ * above ~1024 tokens — there is no per-message marker to inject.
11
+ */
12
+ export function createOpenAIProvider(options) {
13
+ const client = options.client ?? createOpenAI();
14
+ return {
15
+ name: "openai",
16
+ model: client(options.modelId),
17
+ providerOptions: {
18
+ openai: {
19
+ reasoningEffort: "medium",
20
+ reasoningSummary: "detailed",
21
+ },
22
+ },
23
+ };
24
+ }
25
+ //# sourceMappingURL=openai.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/llm/openai.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAS9C;;;;;;;;;GASG;AACH,MAAM,UAAU,oBAAoB,CAClC,OAA8B;IAE9B,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,YAAY,EAAE,CAAC;IAChD,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC;QAC9B,eAAe,EAAE;YACf,MAAM,EAAE;gBACN,eAAe,EAAE,QAAQ;gBACzB,gBAAgB,EAAE,UAAU;aAC7B;SACF;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Load `aggregation_edit_prompt.txt` from `promptDir`. Cached per directory
3
+ * for the process lifetime; the file is small and rarely changes between
4
+ * deploys.
5
+ */
6
+ export declare function loadEditPromptTemplate(promptDir: string): string;
7
+ //# sourceMappingURL=prompts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompts.d.ts","sourceRoot":"","sources":["../src/prompts.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAUhE"}
@@ -0,0 +1,17 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ const cache = new Map();
4
+ /**
5
+ * Load `aggregation_edit_prompt.txt` from `promptDir`. Cached per directory
6
+ * for the process lifetime; the file is small and rarely changes between
7
+ * deploys.
8
+ */
9
+ export function loadEditPromptTemplate(promptDir) {
10
+ let cached = cache.get(promptDir);
11
+ if (!cached) {
12
+ cached = readFileSync(join(promptDir, "aggregation_edit_prompt.txt"), "utf-8");
13
+ cache.set(promptDir, cached);
14
+ }
15
+ return cached;
16
+ }
17
+ //# sourceMappingURL=prompts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompts.js","sourceRoot":"","sources":["../src/prompts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAC;AAExC;;;;GAIG;AACH,MAAM,UAAU,sBAAsB,CAAC,SAAiB;IACtD,IAAI,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAClC,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,GAAG,YAAY,CACnB,IAAI,CAAC,SAAS,EAAE,6BAA6B,CAAC,EAC9C,OAAO,CACR,CAAC;QACF,KAAK,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,39 @@
1
+ /** Programmatic API: construct a Hono app from injected storage / LLM /
2
+ * schema. The default env-driven entry in `index.ts` calls this. */
3
+ import { Hono } from "hono";
4
+ import { z } from "zod";
5
+ import type { Storage } from "./storage/interface.js";
6
+ import type { LlmProvider } from "./llm/interface.js";
7
+ import { type Artifact } from "./artifact.js";
8
+ export interface CreateAppOptions {
9
+ storage: Storage;
10
+ provider: LlmProvider;
11
+ /**
12
+ * Zod schema for the deployment's full artifact. Must extend the
13
+ * citation-grounded core (see `artifactFields` in `./artifact.js`). Use
14
+ * `ArtifactSchema` exported from this package as the default — that
15
+ * schema is just the citation-grounded core itself.
16
+ */
17
+ schema?: z.ZodType<Artifact>;
18
+ jwtSecret: string;
19
+ /** Directory containing `aggregation_edit_prompt.txt`. */
20
+ promptDir: string;
21
+ /** Session token lifetime in seconds. Default: 4 hours. */
22
+ jwtTtlSeconds?: number;
23
+ /** Allowed CORS origins. Default: any. */
24
+ corsOrigins?: string[];
25
+ }
26
+ /**
27
+ * Construct a Hono app exposing chat-service's HTTP routes:
28
+ *
29
+ * - `GET /health` → `{ status: "ok" }`
30
+ * - `POST /sessions` → creates an edit session, returns a session JWT.
31
+ * - `POST /chat/:sessionId` → SSE stream of chat output.
32
+ *
33
+ * The app does **no upstream-IDP authentication** on `/sessions` — that
34
+ * is a separate concern. Add the OIDC middleware (`createOidcMiddleware`
35
+ * from `./auth/oidc`) in front of `/sessions` if your deployment needs
36
+ * it. The session JWT on `/chat/:id` is enforced internally.
37
+ */
38
+ export declare function createApp(options: CreateAppOptions): Hono;
39
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA;qEACqE;AAErE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,eAAe,CAAC;AAU9D,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,WAAW,CAAC;IACtB;;;;;OAKG;IACH,MAAM,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,0DAA0D;IAC1D,SAAS,EAAE,MAAM,CAAC;IAClB,2DAA2D;IAC3D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,0CAA0C;IAC1C,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CACxB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAwGzD"}
package/dist/server.js ADDED
@@ -0,0 +1,106 @@
1
+ /** Programmatic API: construct a Hono app from injected storage / LLM /
2
+ * schema. The default env-driven entry in `index.ts` calls this. */
3
+ import { Hono } from "hono";
4
+ import { cors } from "hono/cors";
5
+ import { z } from "zod";
6
+ import { ArtifactSchema } from "./artifact.js";
7
+ import { createEditSession, getCachedSession, rebuildSession, } from "./session.js";
8
+ import { handleChat } from "./chat.js";
9
+ import { verifySessionToken } from "./auth/jwt.js";
10
+ /**
11
+ * Construct a Hono app exposing chat-service's HTTP routes:
12
+ *
13
+ * - `GET /health` → `{ status: "ok" }`
14
+ * - `POST /sessions` → creates an edit session, returns a session JWT.
15
+ * - `POST /chat/:sessionId` → SSE stream of chat output.
16
+ *
17
+ * The app does **no upstream-IDP authentication** on `/sessions` — that
18
+ * is a separate concern. Add the OIDC middleware (`createOidcMiddleware`
19
+ * from `./auth/oidc`) in front of `/sessions` if your deployment needs
20
+ * it. The session JWT on `/chat/:id` is enforced internally.
21
+ */
22
+ export function createApp(options) {
23
+ const schema = (options.schema ?? ArtifactSchema);
24
+ const sessionConfig = {
25
+ storage: options.storage,
26
+ schema,
27
+ promptDir: options.promptDir,
28
+ jwtSecret: options.jwtSecret,
29
+ jwtTtlSeconds: options.jwtTtlSeconds ?? 14400,
30
+ };
31
+ const app = new Hono();
32
+ app.use("*", cors({
33
+ origin: options.corsOrigins ?? "*",
34
+ allowMethods: ["GET", "POST", "OPTIONS"],
35
+ allowHeaders: ["Content-Type", "Authorization"],
36
+ }));
37
+ app.get("/health", (c) => c.json({ status: "ok" }));
38
+ const createSessionBody = z.object({
39
+ variant_id: z.string(),
40
+ user_id: z.string(),
41
+ category: z.string(),
42
+ initial_artifact: z.string(),
43
+ initial_version: z.number().int().nonnegative(),
44
+ });
45
+ app.post("/sessions", async (c) => {
46
+ const body = createSessionBody.safeParse(await c.req.json());
47
+ if (!body.success) {
48
+ return c.json({ error: "Invalid request body", details: body.error.issues }, 400);
49
+ }
50
+ try {
51
+ const result = await createEditSession(sessionConfig, {
52
+ variantId: body.data.variant_id,
53
+ userId: body.data.user_id,
54
+ category: body.data.category,
55
+ initialArtifact: body.data.initial_artifact,
56
+ initialVersion: body.data.initial_version,
57
+ });
58
+ return c.json({
59
+ session_id: result.session.id,
60
+ token: result.token,
61
+ expires_at: result.expiresAt.toISOString(),
62
+ });
63
+ }
64
+ catch (error) {
65
+ const message = error instanceof Error ? error.message : "Session creation failed";
66
+ return c.json({ error: message }, 422);
67
+ }
68
+ });
69
+ app.post("/chat/:sessionId", async (c) => {
70
+ const sessionId = c.req.param("sessionId");
71
+ const authHeader = c.req.header("Authorization");
72
+ if (!authHeader?.startsWith("Bearer ")) {
73
+ return c.json({ error: "Unauthorized" }, 401);
74
+ }
75
+ let claims;
76
+ try {
77
+ claims = await verifySessionToken(authHeader.slice(7), {
78
+ secret: sessionConfig.jwtSecret,
79
+ ttlSeconds: sessionConfig.jwtTtlSeconds,
80
+ });
81
+ }
82
+ catch {
83
+ return c.json({ error: "Invalid or expired session token" }, 401);
84
+ }
85
+ if (claims.session_id !== sessionId) {
86
+ return c.json({ error: "Session ID mismatch" }, 403);
87
+ }
88
+ let session = getCachedSession(sessionId);
89
+ if (!session) {
90
+ try {
91
+ session = await rebuildSession(sessionConfig, claims, claims.expiresAt);
92
+ }
93
+ catch (error) {
94
+ const message = error instanceof Error ? error.message : "Failed to rebuild session";
95
+ return c.json({ error: message }, 500);
96
+ }
97
+ }
98
+ return handleChat({
99
+ storage: options.storage,
100
+ provider: options.provider,
101
+ schema,
102
+ }, c.req.raw, session);
103
+ });
104
+ return app;
105
+ }
106
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA;qEACqE;AAErE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,cAAc,EAAiB,MAAM,eAAe,CAAC;AAC9D,OAAO,EAEL,iBAAiB,EACjB,gBAAgB,EAChB,cAAc,GACf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,C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AE,OAAO,CAAC,OAAO;YACxB,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,MAAM;SACP,EACD,CAAC,CAAC,GAAG,CAAC,GAAG,EACT,OAAO,CACR,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,68 @@
1
+ /** Session management: creation, caching, and context building. */
2
+ import type { z } from "zod";
3
+ import { type SessionClaims } from "./auth/jwt.js";
4
+ import { type Artifact } from "./artifact.js";
5
+ import type { Storage } from "./storage/interface.js";
6
+ import { type BboxCache } from "./yaml.js";
7
+ export interface SessionContext {
8
+ id: string;
9
+ variantId: string;
10
+ userId: string;
11
+ /** Paper ID (e.g. "Miyata2018") → DOI / paper-key understood by storage. */
12
+ paperIds: Record<string, string>;
13
+ systemPrompt: string;
14
+ expiresAt: Date;
15
+ /** Current artifact as YAML (mutable via str_replace / insert / write). */
16
+ artifactYaml: string;
17
+ /** Current version number in the draft chain. */
18
+ artifactVersion: number;
19
+ /** Set to true when an edit modifies the artifact during a turn. */
20
+ artifactDirty: boolean;
21
+ /** Result selector (`category` in flowa-generic terms). */
22
+ category: string;
23
+ /** All categories present in the aggregate (for rename-conflict detection). */
24
+ aggregateCategories: string[];
25
+ /** Cached bboxes from the initial artifact, keyed by (paperId, quote). */
26
+ bboxCache: BboxCache;
27
+ }
28
+ export interface SessionConfig {
29
+ /** Storage backend for aggregates / papers / edit drafts / audit log. */
30
+ storage: Storage;
31
+ /** Zod schema for the deployment's full artifact (extends the citation-grounded core). */
32
+ schema: z.ZodType<Artifact>;
33
+ /** Directory containing `aggregation_edit_prompt.txt`. */
34
+ promptDir: string;
35
+ /** Session JWT signing config. */
36
+ jwtSecret: string;
37
+ jwtTtlSeconds: number;
38
+ }
39
+ export declare function getCachedSession(sessionId: string): SessionContext | undefined;
40
+ /** For tests / shutdown: clear the in-memory cache. */
41
+ export declare function clearSessionCache(): void;
42
+ /**
43
+ * Rebuild session context from JWT claims (after restart or cache eviction).
44
+ * Loads the latest draft from storage to restore artifact state. This loses
45
+ * session-specific position if another session has written newer versions
46
+ * in the meantime — acceptable for now.
47
+ */
48
+ export declare function rebuildSession(config: SessionConfig, claims: SessionClaims, expiresAt: Date): Promise<SessionContext>;
49
+ export interface EditSessionResult {
50
+ session: SessionContext;
51
+ token: string;
52
+ expiresAt: Date;
53
+ }
54
+ export interface CreateEditSessionInput {
55
+ variantId: string;
56
+ userId: string;
57
+ category: string;
58
+ initialArtifact: string;
59
+ initialVersion: number;
60
+ }
61
+ /**
62
+ * Create a new edit session bound to a specific artifact version. The caller
63
+ * passes the initial artifact JSON verbatim and the version number it came
64
+ * from — chat-service stores these as-is.
65
+ */
66
+ export declare function createEditSession(config: SessionConfig, input: CreateEditSessionInput): Promise<EditSessionResult>;
67
+ export declare const CITATION_INSTRUCTIONS = "## Citations\n\nWhen referencing a finding from a paper, use inline Markdown citation links with a verbatim quote: `[link text](#cite:paperId \"verbatim quote\")`.\n\n- The **link text** is free-form \u2014 use a descriptive phrase or specific claim, whatever reads naturally.\n- The **title attribute** (in quotes after the href) must be a verbatim passage from the paper text \u2014 enough context to validate the claim (typically a sentence or key clause). The quote will be highlighted in the PDF for reviewers, so avoid quoting isolated values or single words that could match multiple locations \u2014 include surrounding context.\n- When re-citing a finding from a paper's extraction results (loaded via loadPaperExtracts), reuse the exact quote string from the extraction's citation \u2014 do not rephrase or shorten it.\n- When citing from a paper's full text (loaded via loadFullPaper or queryPapers), quote the relevant passage verbatim.\n- The href format `#cite:paperId` is required exactly \u2014 the application uses it to identify the paper.\n- Be generous with links: whenever a factual claim can be traced to a specific passage, link it.";
68
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,mEAAmE;AAInE,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,EAAoB,KAAK,aAAa,EAAE,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